summaryrefslogtreecommitdiff
path: root/project/config/cfgdefs.sh
blob: ecb06257da5abdab8b92dec8838d0135cf2b416d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# in projects where [ $mb_use_custom_cfgdefs = yes ],
# cfgdefs.sh is invoked from within ./configure via
# . $mb_project_dir/project/cfgdefs.sh

# a successful return from cfgdefs.sh will be followed
# by a second invocation of the config_copy() function,
# reflecting any changes to common config variables
# made by cfgdefs.sh.

# finally, cfgdefs.sh may update the contents of the
# config-time generated cfgdefs.mk.


# cfgdefs helper functions
. "$mb_project_dir/project/config/cfghost.sh"


# sofort's config test framework
. "$mb_project_dir/sofort/cfgtest/cfgtest.sh"


pycfg_pydebug=
pycfg_pymalloc=

for arg ; do
	case "$arg" in
		--with-pydebug )
			pycfg_pydebug='d' ;;

		--without-pydebug )
			pycfg_pydebug= ;;

		--with-pymalloc )
			pycfg_pymalloc='m' ;;

		--without-pymalloc )
			pycfg_pymalloc= ;;

		*)
			error_msg ${arg#}: "unsupported config argument."
			exit 2
	esac
done


cfgdefs_set_cfghost_flavors()
{
	cfghost_set_target_cfghost
	cfghost_set_native_cfghost
}


cfgdefs_detect_python_version()
{
	mb_internal_verinfo=$(mktemp)

	if [ -z "$mb_internal_verinfo" ]; then
		exit 2
	fi

	"$mb_native_cc" -E -dM "$mb_source_dir/Include/patchlevel.h" \
		> "$mb_internal_verinfo"

	python_major=$(grep '#define PY_MAJOR_VERSION ' "$mb_internal_verinfo" | cut -d' ' -f3)
	python_minor=$(grep '#define PY_MINOR_VERSION ' "$mb_internal_verinfo" | cut -d' ' -f3)
	python_micro=$(grep '#define PY_MICRO_VERSION ' "$mb_internal_verinfo" | cut -d' ' -f3)

	if [ -z "$python_major" ] || [ -z "$python_minor" ] || [ -z "$python_micro" ]; then
		error_msg "Could not properly parse Python's patchlevel.h"
		exit 2
	fi

	python_ver="$python_major.$python_minor"

	[ -z "$mb_package"  ] && mb_package='python'"$python_ver"
	[ -z "$mb_nickname" ] && mb_nickname="$mb_package"
	[ -z "$mb_pkgname"  ] && mb_pkgname="$mb_package"
	[ -z "$mb_pkgdesc"  ] && mb_pkgdesc="$mb_package"
}


cfgdefs_set_option_variables()
{
	python_abifl=$pycfg_pydebug$pycfg_pymalloc
}


cfgdefs_output_options()
{
	if [ -n "$pycfg_pydebug" ]; then
		cfgtest_cflags_append '-DPy_DEBUG'
	fi

	if [ -n "$pycfg_pymalloc" ]; then
		cfgtest_cflags_append '-DWITH_PYMALLOC'
	fi
}


cfgdefs_output_custom_defs()
{
	sed \
			-e 's/@python_ver@/'"$python_ver"'/g'       \
			-e 's/@python_major@/'"$python_major"'/g'   \
			-e 's/@python_minor@/'"$python_minor"'/g'   \
			-e 's/@python_micro@/'"$python_micro"'/g'   \
			-e 's/@python_abifl@/'"$python_abifl"'/g'   \
		"$mb_project_dir/project/config/cfgdefs.in"         \
			>> "$mb_pwd/cfgdefs.mk"
}


cfgdefs_perform_common_tests()
{
	# headers
	cfgtest_header_absence  'stropts.h'
	cfgtest_header_presence 'stdatomic.h'
	cfgtest_header_presence 'endian.h'
	cfgtest_header_presence 'net/if.h'
	cfgtest_header_presence 'sched.h'
	cfgtest_header_presence 'sys/ioctl.h'
	cfgtest_header_presence 'sys/sendfile.h'
	cfgtest_header_presence 'sys/syscall.h'
	cfgtest_header_presence 'sys/sysmacros.h'
	cfgtest_header_presence 'sys/uio.h'
	cfgtest_header_presence 'sys/xattr.h'
	cfgtest_header_presence 'uuid.h'
	cfgtest_header_presence 'uuid/uuid.h'

	# interfaces
	mb_cfgtest_headers='sys/epoll.h'
	cfgtest_interface_presence 'epoll_create1'

	mb_cfgtest_headers='sched.h'
	cfgtest_interface_presence 'sched_get_priority_max'
	cfgtest_interface_presence 'sched_rr_get_interval'
	cfgtest_interface_presence 'sched_setaffinity'
	cfgtest_interface_presence 'sched_setparam'
	cfgtest_interface_presence 'sched_setscheduler'

	mb_cfgtest_headers='sys/sendfile.h'
	cfgtest_interface_presence 'sendfile'

	# size of system types
	cfgtest_newline
	cfgtest_comment 'size of system types'

	cfgtest_type_size 'short'
	cfgtest_type_size 'int'
	cfgtest_type_size 'long'
	cfgtest_type_size 'long long'
	cfgtest_type_size 'float'
	cfgtest_type_size 'double'
	cfgtest_type_size 'long double'
	cfgtest_type_size 'void *'

	mb_cfgtest_headers='stdint.h'
	cfgtest_type_size 'uintptr_t'

	mb_cfgtest_headers='stdbool.h'
	cfgtest_type_size '_Bool'

	mb_cfgtest_headers='stddef.h'
	cfgtest_type_size 'size_t'
	cfgtest_type_size 'wchar_t'

	mb_cfgtest_headers='unistd.h'
	cfgtest_type_size 'pid_t'
	cfgtest_type_size 'off_t'

	mb_cfgtest_headers='time.h'
	cfgtest_type_size 'time_t'

	mb_cfgtest_headers='stdio.h'
	cfgtest_type_size 'fpos_t'

	mb_cfgtest_headers='pthread.h'
	cfgtest_type_size 'pthread_t'

	# toolchain
	cfgtest_newline
	cfgtest_comment 'toolchain'

	# x87
	mb_code_snippet=''
	mb_code_snippet="$mb_code_snippet "'void fn(void)'
	mb_code_snippet="$mb_code_snippet "'{__asm__ __volatile__ ("fnstcw pcw");'
	mb_code_snippet="$mb_code_snippet "' __asm__ __volatile__ ("fldcw  pcw");}'

	if cfgtest_code_snippet "$mb_code_snippet"; then
		cfgtest_cflags_append '-DHAVE_GCC_ASM_FOR_X87'
	fi

	# x64
	mb_code_snippet=''
	mb_code_snippet="$mb_code_snippet "'void fn(void)'
	mb_code_snippet="$mb_code_snippet "'{__asm__ __volatile__ ("callq *%rax");}'

	if cfgtest_code_snippet "$mb_code_snippet"; then
		cfgtest_cflags_append '-DHAVE_GCC_ASM_FOR_X64'
	fi

	# IEEE 754: little endian / big endian
	if $mb_cfgtest_cc -E -dM - < /dev/null \
			| grep '__BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__' \
			> /dev/null; then
		cfgtest_cflags_append '-DDOUBLE_IS_LITTLE_ENDIAN_IEEE754'

	elif $mb_cfgtest_cc -E -dM - < /dev/null \
			| grep '__BYTE_ORDER__ __ORDER_BIG_ENDIAN__' \
			> /dev/null; then
		cfgtest_cflags_append '-DDOUBLE_IS_BIG_ENDIAN_IEEE754'

	else
		cfgtest_newline
		cfgtest_comment 'cfgdefs.sh: cannot detect little/big endian,'
		cfgtest_comment 'cfgdefs.sh: so defined both macros below.'
		cfgtest_newline

		cfgtest_cflags_append '-DDOUBLE_IS_LITTLE_ENDIAN_IEEE754'
		cfgtest_cflags_append '-DDOUBLE_IS_BIG_ENDIAN_IEEE754'
	fi

	# getrandom: easier to simply test,
	# than to test whether we need to test
	# (and then test).
	cfgtest_newline
	cfgtest_comment 'getrandom'

	mb_cfgtest_headers='sys/syscall.h'

	if cfgtest_decl_presence 'SYS_getrandom'; then
		cfgtest_cflags_append '-DHAVE_GETRANDOM_SYSCALL'
	fi

	# libintl
	cfgtest_newline
	cfgtest_comment 'libintl'

	if cfgtest_library_presence -lintl; then
		cfgtest_ldflags_append '-Wl,--as-needed -lintl -Wl,--no-as-needed'
	fi

	# libuuid
	cfgtest_newline
	cfgtest_comment 'libuuid'

	if cfgtest_library_presence -luuid; then
		cfgtest_ldflags_append '-Wl,--as-needed -luuid -Wl,--no-as-needed'
	fi

	# ncurses
	cfgtest_newline
	cfgtest_comment 'ncurses/ncursesw'
	mb_cfgtest_headers='ncurses.h'

	if [ $mb_cfgtest_cfgtype = 'target' ]; then
		mb_cfgtest_makevar='CFLAGS_NCURSES'
	else
		mb_cfgtest_makevar='NATIVE_CC_CFLAGS'
	fi

	cfgtest_unit_header_presence 'curses.h'
	cfgtest_unit_header_presence 'ncurses.h'

	cfgtest_unit_header_presence 'ncurses/panel.h'  && mb_panel_wrapper='yes'
	cfgtest_unit_header_presence 'ncursesw/panel.h' && mb_panel_wrapper='yes'

	if cfgtest_unit_interface_presence 'is_term_resized'; then
		cfgtest_makevar_append '-DHAVE_CURSES_IS_TERM_RESIZED'
	fi

	if cfgtest_unit_interface_presence 'resizeterm'; then
		cfgtest_makevar_append '-DHAVE_CURSES_RESIZETERM'
	fi

	if cfgtest_unit_interface_presence 'resize_term'; then
		cfgtest_makevar_append '-DHAVE_CURSES_RESIZE_TERM'
	fi

	# ncursesw
	if cfgtest_unit_interface_presence 'mvwget_wch'; then
		cfgtest_makevar_append '-DHAVE_NCURSESW'
	fi

	# ncurses: python refers to members of typedef struct _win_st WINDOW
	cfgtest_makevar_append '-DNCURSES_INTERNALS'

	# <panel.h>
	if [ _${mb_panel_wrapper:-} = _yes ]; then
		cfgtest_makevar_append '-I$(PROJECT_DIR)/wrappers'
		unset mb_panel_wrapper
	fi

	# ncurses libs (common part)
	mb_ncurses_libs='-lpanelw -lncursesw'
	mb_ncurses_tinfo='-ltinfo'

	if cfgtest_library_presence $mb_ncurses_tinfo; then
		mb_ncurses_libs="$mb_ncurses_libs $mb_ncurses_tinfo"
	fi
}


cfgdefs_perform_target_tests()
{
	# init
	cfgtest_target_section

	# common tests
	cfgdefs_perform_common_tests

	# ncurses libs
	mb_cfgtest_makevar='LDFLAGS_NCURSES_LIBS'
	cfgtest_makevar_append $mb_ncurses_libs

	mb_cfgtest_makevar='LDFLAGS_NCURSES'
	cfgtest_makevar_append \
		'-Wl,--as-needed $(LDFLAGS_NCURSES_LIBS)' \
		'-Wl,--no-as-needed'

	mb_cfgtest_makevar='LDFLAGS_NCURSES_STATIC'
	cfgtest_makevar_append '$(LDFLAGS_NCURSES)'

	# openssl
	cfgtest_newline
	cfgtest_comment 'openssl/libressl'

	mb_cfgtest_headers='openssl/x509.h'

	if cfgtest_interface_presence 'X509_NAME_ENTRY_set'; then
		cfgtest_cflags_append '-DOPENSSL_VERSION_1_1'
	fi

	# tcl/tk libc
	cfgtest_newline
	cfgtest_comment 'tcl/tk'
	mb_tcltk_libs=

	for mb_tcltk_ver in 8.9 8.8 8.7 8.6 8.5; do
		if [ -z "$mb_tcltk_libs" ]; then
			mb_tcltk_libs="-ltk$mb_tcltk_ver -ltcl$mb_tcltk_ver"
			cfgtest_library_presence $mb_tcltk_libs \
				|| mb_tcltk_libs=''
		fi
	done

	mb_cfgtest_makevar='LDFLAGS_TCLTK_LIBS'
	cfgtest_makevar_append "$mb_tcltk_libs"

	mb_cfgtest_makevar='CFLAGS_TCLTK'
	cfgtest_makevar_append '-DWITH_APPINIT'

	mb_cfgtest_makevar='LDFLAGS_TCLTK'
	cfgtest_makevar_append '$(LDFLAGS_TCLTK_LIBS)'

	# sqlite
	cfgtest_newline
	cfgtest_comment 'sqlite3'

	mb_cfgtest_makevar='CFLAGS_SQLITE'
	cfgtest_makevar_append '-DMODULE_NAME=\"sqlite3\"'
	cfgtest_makevar_append '-DSQLITE_OMIT_LOAD_EXTENSION'

	mb_cfgtest_makevar='LDFLAGS_SQLITE'
	cfgtest_makevar_append '-lsqlite3'

	mb_cfgtest_makevar='LDFLAGS_SQLITE_STATIC'
	cfgtest_makevar_append '$(LDFLAGS_SQLITE)'

	# expat
	cfgtest_newline
	cfgtest_comment 'expat'

	mb_cfgtest_makevar='CFLAGS_EXPAT'
	cfgtest_makevar_append '-DXML_DEV_URANDOM'

	mb_cfgtest_makevar='LDFLAGS_EXPAT'
	cfgtest_makevar_append '-lexpat'

	mb_cfgtest_makevar='LDFLAGS_EXPAT_STATIC'
	cfgtest_makevar_append '$(LDFLAGS_EXPAT)'

	# ndbm
	cfgtest_newline
	cfgtest_comment 'ndbm'

	mb_cfgtest_makevar='CFLAGS_DBM'
	cfgtest_unit_header_presence 'ndbm.h'

	mb_cfgtest_makevar='LDFLAGS_DBM'
	cfgtest_makevar_append '-lgdbm_compat'

	mb_cfgtest_makevar='LDFLAGS_DBM_STATIC'
	cfgtest_makevar_append '$(LDFLAGS_DBM)'

	# static python library switches
	cfgtest_newline
	cfgtest_comment 'static python library switches'
	mb_cfgtest_makevar='LDFLAGS_PYEXT_STATIC'

	cfgtest_makevar_append '-Wl,--as-needed -lssl -lcrypto -Wl,--no-as-needed'
	cfgtest_makevar_append '-Wl,--as-needed -luuid         -Wl,--no-as-needed'
	cfgtest_makevar_append '-Wl,--as-needed -lbz2          -Wl,--no-as-needed'
	cfgtest_makevar_append '-Wl,--as-needed -lffi          -Wl,--no-as-needed'
	cfgtest_makevar_append '-Wl,--as-needed -lgdbm         -Wl,--no-as-needed'
	cfgtest_makevar_append '-Wl,--as-needed -lreadline     -Wl,--no-as-needed'
	cfgtest_makevar_append '-Wl,--as-needed -lintl         -Wl,--no-as-needed'
	cfgtest_makevar_append '-Wl,--as-needed -lz            -Wl,--no-as-needed'

	[ $python_major = '3' ] && \
	cfgtest_makevar_append '-Wl,--as-needed -llzma         -Wl,--no-as-needed'

	# options
	cfgtest_newline
	cfgtest_comment 'target options'
	cfgdefs_output_options

	# pretty cfgdefs.mk
	cfgtest_newline
}


cfgdefs_perform_native_tests()
{
	# init
	cfgtest_native_section

	# common tests
	cfgdefs_perform_common_tests

	# ncurses libs
	cfgtest_ldflags_append $mb_ncurses_libs

	# options
	cfgtest_newline
	cfgtest_comment '(native) pycompile options'
	cfgdefs_output_options

	# pretty cfgdefs.mk
	cfgtest_newline
}


# cfghost
cfgdefs_set_cfghost_flavors

# python version info
cfgdefs_detect_python_version

# python-specific configuration
cfgdefs_set_option_variables

# cfgdefs.in --> cfgdefs.mk
cfgdefs_output_custom_defs

# strict: some tests might fail
set +e

# target-specific tests
cfgdefs_perform_target_tests

# native system tests
cfgdefs_perform_native_tests

# strict: restore mode
set -e

# all done
return 0