1dnl compiler.m4 --- compiler magic
2dnl
3dnl Copyright (C) 2005-2016 Sebastian Freundt
4dnl Copyright (c) 2005 Steven G. Johnson
5dnl Copyright (c) 2005 Matteo Frigo
6dnl
7dnl Author: Sebastian Freundt <hroptatyr@sxemacs.org>
8dnl
9dnl Redistribution and use in source and binary forms, with or without
10dnl modification, are permitted provided that the following conditions
11dnl are met:
12dnl
13dnl 1. Redistributions of source code must retain the above copyright
14dnl    notice, this list of conditions and the following disclaimer.
15dnl
16dnl 2. Redistributions in binary form must reproduce the above copyright
17dnl    notice, this list of conditions and the following disclaimer in the
18dnl    documentation and/or other materials provided with the distribution.
19dnl
20dnl 3. Neither the name of the author nor the names of any contributors
21dnl    may be used to endorse or promote products derived from this
22dnl    software without specific prior written permission.
23dnl
24dnl THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
25dnl IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26dnl WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27dnl DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28dnl FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29dnl CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30dnl SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31dnl BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32dnl WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33dnl OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34dnl IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35dnl
36dnl This file is part of SXEmacs.
37
38##### http://autoconf-archive.cryp.to/ax_check_compiler_flags.html
39## renamed the prefix to SXE_
40AC_DEFUN([SXE_CHECK_COMPILER_FLAG], [dnl
41dnl SXE_CHECK_COMPILER_FLAG([flag], [action-if-accepted], [action-if-not-accepted])
42	AC_MSG_CHECKING([whether _AC_LANG compiler accepts $1])
43
44	## store werror status, then turn -Werror on
45	save_ac_[]_AC_LANG_ABBREV[]_werror_flag="${ac_[]_AC_LANG_ABBREV[]_werror_flag}"
46	AC_LANG_WERROR
47
48	AC_CACHE_VAL(AS_TR_SH(sxe_cv_[]_AC_LANG_ABBREV[]_flag_$1), [dnl
49		sxe_save_FLAGS="${[]_AC_LANG_PREFIX[]FLAGS}"
50		_AC_LANG_PREFIX[]FLAGS="$1"
51		AC_COMPILE_IFELSE([AC_LANG_PROGRAM($4)],
52			eval AS_TR_SH(sxe_cv_[]_AC_LANG_ABBREV[]_flag_$1)="yes",
53			eval AS_TR_SH(sxe_cv_[]_AC_LANG_ABBREV[]_flag_$1)="no")
54		_AC_LANG_PREFIX[]FLAGS="${sxe_save_FLAGS}"
55	])
56	eval sxe_check_flag=$AS_TR_SH(sxe_cv_[]_AC_LANG_ABBREV[]_flag_$1)
57	ac_[]_AC_LANG_ABBREV[]_werror_flag="${save_ac_[]_AC_LANG_ABBREV[]_werror_flag}"
58
59	AC_MSG_RESULT([${sxe_check_flag}])
60	if test "${sxe_check_flag}" = "yes"; then
61		:
62		$2
63	else
64		:
65		$3
66	fi
67])dnl SXE_CHECK_COMPILER_FLAG
68
69AC_DEFUN([SXE_CHECK_PREPROC_FLAG], [dnl
70dnl SXE_CHECK_CPP_FLAG([flag], [action-if-found], [action-if-not-found])
71	AC_MSG_CHECKING([whether _AC_LANG preprocessor accepts $1])
72
73	## store werror status, then turn -Werror on
74	save_ac_[]_AC_LANG_ABBREV[]_werror_flag="${ac_[]_AC_LANG_ABBREV[]_werror_flag}"
75	AC_LANG_WERROR
76
77	AC_CACHE_VAL(AS_TR_SH(sxe_cv_[]_AC_LANG_ABBREV[]pp_flag_$1), [dnl
78		sxe_save_FLAGS="${[]_AC_LANG_PREFIX[]PPFLAGS}"
79		_AC_LANG_PREFIX[]PPFLAGS="$1"
80		AC_PREPROC_IFELSE([AC_LANG_PROGRAM()],
81			eval AS_TR_SH(sxe_cv_[]_AC_LANG_ABBREV[]pp_flag_$1)="yes",
82			eval AS_TR_SH(sxe_cv_[]_AC_LANG_ABBREV[]pp_flag_$1)="no")
83		_AC_LANG_PREFIX[]PPFLAGS=$sxe_save_FLAGS
84	])
85	eval sxe_check_flag=$AS_TR_SH(sxe_cv_[]_AC_LANG_ABBREV[]pp_flag_$1)
86	ac_[]_AC_LANG_ABBREV[]_werror_flag="${save_ac_[]_AC_LANG_ABBREV[]_werror_flag}"
87
88	AC_MSG_RESULT([${sxe_check_flag}])
89	if test "${sxe_check_flag}" = "yes"; then
90		:
91		$2
92	else
93		:
94		$3
95	fi
96])dnl SXE_CHECK_PREPROC_FLAG
97
98AC_DEFUN([SXE_CHECK_CCLD_FLAG], [dnl
99dnl SXE_CHECK_CCLD_FLAG([flag], [action-if-accepted], [action-if-not-accepted])
100	AC_MSG_CHECKING([whether _AC_LANG linker accepts $1])
101
102	## store werror status, then turn -Werror on
103	save_ac_[]_AC_LANG_ABBREV[]_werror_flag="${ac_[]_AC_LANG_ABBREV[]_werror_flag}"
104	AC_LANG_WERROR
105
106	AC_CACHE_VAL(AS_TR_SH(sxe_cv_[]_AC_LANG_ABBREV[]_flag_$1), [dnl
107		sxe_save_FLAGS="${[]_AC_LANG_PREFIX[]FLAGS}"
108		_AC_LANG_PREFIX[]FLAGS="$1"
109		AC_LINK_IFELSE([AC_LANG_PROGRAM()],
110			eval AS_TR_SH(sxe_cv_[]_AC_LANG_ABBREV[]_flag_$1)="yes",
111			eval AS_TR_SH(sxe_cv_[]_AC_LANG_ABBREV[]_flag_$1)="no")
112		_AC_LANG_PREFIX[]FLAGS="${sxe_save_FLAGS}"
113	])
114	eval sxe_check_flag=$AS_TR_SH(sxe_cv_[]_AC_LANG_ABBREV[]_flag_$1)
115	ac_[]_AC_LANG_ABBREV[]_werror_flag="${save_ac_[]_AC_LANG_ABBREV[]_werror_flag}"
116
117	AC_MSG_RESULT([${sxe_check_flag}])
118	if test "${sxe_check_flag}" = "yes"; then
119		:
120		$2
121	else
122		:
123		$3
124	fi
125])dnl SXE_CHECK_CCLD_FLAG
126
127
128AC_DEFUN([SXE_DEBUGFLAGS], [dnl
129	## distinguish between different compilers, no?
130	SXE_CHECK_COMPILER_FLAG([-g])
131	SXE_CHECK_COMPILER_FLAG([-g3])
132
133	AC_PATH_PROG([DBX], [dbx])
134	if test -n "$ac_cv_path_DBX"; then
135		SXE_CHECK_COMPILER_FLAG([-gstabs])
136		SXE_CHECK_COMPILER_FLAG([-gstabs3])
137		SXE_CHECK_COMPILER_FLAG([-gxcoff])
138		SXE_CHECK_COMPILER_FLAG([-gxcoff3])
139	fi
140
141	AC_PATH_PROG([GDB], [gdb])
142	if test -n "$ac_cv_path_GDB"; then
143		SXE_CHECK_COMPILER_FLAG([-ggdb])
144		SXE_CHECK_COMPILER_FLAG([-ggdb3])
145	fi
146
147	AC_PATH_PROG([SDB], [sdb])
148	if test -n "$ac_cv_path_SDB"; then
149		SXE_CHECK_COMPILER_FLAG([-gcoff])
150		SXE_CHECK_COMPILER_FLAG([-gcoff3])
151	fi
152
153	## final evaluation
154	debugflags=""
155	## gdb
156	if test "$sxe_cv_c_flag__ggdb3" = "yes"; then
157		debugflags="$debugflags -ggdb3"
158	elif test "$sxe_cv_c_flag__ggdb" = "yes"; then
159		debugflags="$debugflags -ggdb"
160	fi
161	## stabs
162	if test "$sxe_cv_c_flag__gstabs3" = "yes"; then
163		debugflags="$debugflags -gstabs3"
164	elif test "$sxe_cv_c_flag__gstabs" = "yes"; then
165		debugflags="$debugflags -gstabs"
166	fi
167	## coff
168	if test "$sxe_cv_c_flag__gcoff3" = "yes"; then
169		debugflags="$debugflags -gcoff3"
170	elif test "$sxe_cv_c_flag__gcoff" = "yes"; then
171		debugflags="$debugflags -gcoff"
172	fi
173	## xcoff
174	if test "$sxe_cv_c_flag__gxcoff3" = "yes"; then
175		debugflags="$debugflags -gxcoff3"
176	elif test "$sxe_cv_c_flag__gxcoff" = "yes"; then
177		debugflags="$debugflags -gxcoff"
178	fi
179
180	if test -z "debugflags" -a \
181		"$sxe_cv_c_flag__g" = "yes"; then
182		debugflags="$debugflags -g"
183	fi
184
185	SXE_CHECK_COMPILER_FLAG([-ftime-report])
186	SXE_CHECK_COMPILER_FLAG([-fmem-report])
187	SXE_CHECK_COMPILER_FLAG([-fvar-tracking])
188	SXE_CHECK_COMPILER_FLAG([-save-temps])
189
190	#if test "$sxe_cv_c_flag__ggdb3" = "yes" -a \
191	#	"$sxe_cv_c_flag__fvar_tracking" = "yes"; then
192	#	debugflags="$debugflags -fvar-tracking"
193	#fi
194
195	AC_MSG_CHECKING([for preferred debugging flags])
196	AC_MSG_RESULT([${debugflags}])
197])dnl SXE_DEBUGFLAGS
198
199AC_DEFUN([SXE_WARNFLAGS], [dnl
200	## Calculate warning flags.  We separate the flags for warnings from
201	## the other flags because we want to force the warnings to be seen
202	## by everyone who doesn't specifically override them.
203
204	## by default we want the -Wall level
205	SXE_CHECK_COMPILER_FLAG([-Wall], [warnflags="-Wall"])
206
207	SXE_CHECK_COMPILER_FLAG([-qinfo], [
208		warnflags="${warnflags} -qinfo"])
209
210	SXE_CHECK_COMPILER_FLAG([-Wextra], [
211		warnflags="${warnflags} -Wextra"])
212
213	## Yuck, bad compares have been worth at
214	## least 3 crashes!
215	## Warnings about char subscripts are pretty
216	## pointless, though,
217	## and we use them in various places.
218	SXE_CHECK_COMPILER_FLAG([-Wsign-compare], [
219		warnflags="$warnflags -Wsign-compare"])
220	SXE_CHECK_COMPILER_FLAG([-Wno-char-subscripts], [
221		warnflags="$warnflags -Wno-char-subscripts"])
222	SXE_CHECK_COMPILER_FLAG([-Wundef], [
223		warnflags="$warnflags -Wundef"])
224
225	## too much at the moment, we rarely define protos
226	#warnflags="$warnflags -Wmissing-prototypes -Wstrict-prototypes"
227
228	## somehow clang seems to think -Wpacked is to inform me
229	## about how unnecessary the packed attr is, so conditionalise ...
230	SXE_CHECK_COMPILER_FLAG([-Wpacked], [
231		warnflags="$warnflags -Wpacked"], [:], [[
232#if defined __clang__
233# error
234#endif  /* __clang__ */
235]])
236
237	## glibc is intentionally not `-Wpointer-arith'-clean.
238	## Ulrich Drepper has rejected patches to fix
239	## the glibc header files.
240	## we dont care
241	SXE_CHECK_COMPILER_FLAG([-Wpointer-arith], [
242		warnflags="$warnflags -Wpointer-arith"])
243
244	SXE_CHECK_COMPILER_FLAG([-Wshadow], [
245		warnflags="$warnflags -Wshadow"])
246
247	## our code lacks declarations almost all the time
248	SXE_CHECK_COMPILER_FLAG([-Wmissing-declarations], [
249		warnflags="$warnflags -Wmissing-declarations"])
250	SXE_CHECK_COMPILER_FLAG([-Wmissing-prototypes], [
251		warnflags="$warnflags -Wmissing-prototypes"])
252
253	## gcc can't practically inline anything, so exclude this
254	case "${CC}" in
255	dnl (
256	*"gcc"*)
257		;;
258	dnl (
259	*)
260		SXE_CHECK_COMPILER_FLAG([-Winline], [
261			warnflags="$warnflags -Winline"])
262		;;
263	esac
264
265	SXE_CHECK_COMPILER_FLAG([-Wbad-function-cast], [
266		warnflags="$warnflags -Wbad-function-cast"])
267	SXE_CHECK_COMPILER_FLAG([-Wcast-qual], [
268		warnflags="$warnflags -Wcast-qual"])
269	SXE_CHECK_COMPILER_FLAG([-Wcast-align], [
270		warnflags="$warnflags -Wcast-align"])
271
272	## warn about incomplete switches
273	## for gcc, see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50422
274	## we used to have -Wswitch-default and -Wswitch-enum but that
275	## set gcc off quite badly in the nested switch case
276	SXE_CHECK_COMPILER_FLAG([-Wno-switch], [
277		warnflags="$warnflags -Wno-switch"])
278
279	SXE_CHECK_COMPILER_FLAG([-Wunused-function], [
280		warnflags="$warnflags -Wunused-function"])
281	SXE_CHECK_COMPILER_FLAG([-Wunused-variable], [
282		warnflags="$warnflags -Wunused-variable"])
283	SXE_CHECK_COMPILER_FLAG([-Wunused-parameter], [
284		warnflags="$warnflags -Wunused-parameter"])
285	SXE_CHECK_COMPILER_FLAG([-Wunused-value], [
286		warnflags="$warnflags -Wunused-value"])
287	SXE_CHECK_COMPILER_FLAG([-Wunused], [
288		warnflags="$warnflags -Wunused"])
289	SXE_CHECK_COMPILER_FLAG([-Wmaybe-uninitialized], [
290		warnflags="${warnflags} -Wmaybe-uninitialized"])
291
292	SXE_CHECK_COMPILER_FLAG([-Wnopragma], [
293		warnflags="$warnflags -Wnopragma"])
294
295	SXE_CHECK_COMPILER_FLAG([-fdiagnostics-show-option], [
296		warnflags="${warnflags} -fdiagnostics-show-option"])
297
298	SXE_CHECK_COMPILER_FLAG([-Wunknown-pragmas], [
299		warnflags="$warnflags -Wunknown-pragmas"])
300	SXE_CHECK_COMPILER_FLAG([-Wuninitialized], [
301		warnflags="$warnflags -Wuninitialized"])
302	SXE_CHECK_COMPILER_FLAG([-Wreorder], [
303		warnflags="$warnflags -Wreorder"])
304	SXE_CHECK_COMPILER_FLAG([-Wdeprecated], [
305		warnflags="$warnflags -Wdeprecated"])
306
307	SXE_CHECK_COMPILER_FLAG([-Wno-parentheses], [
308		warnflags="${warnflags} -Wno-parentheses"])
309
310	## icc specific
311	SXE_CHECK_COMPILER_FLAG([-Wcheck], [
312		warnflags="$warnflags -Wcheck"])
313
314	dnl SXE_CHECK_COMPILER_FLAG([-Wp64], [
315	dnl 	warnflags="$warnflags -Wp64"])
316
317	SXE_CHECK_COMPILER_FLAG([-Wstrict-aliasing], [
318		warnflags="$warnflags -Wstrict-aliasing"])
319
320	SXE_CHECK_COMPILER_FLAG([-w3], [
321		warnflags="$warnflags -w3"])
322
323	SXE_CHECK_COMPILER_FLAG([-diag-disable 10237], [dnl
324		warnflags="${warnflags} -diag-disable 10237"], [
325		SXE_CHECK_COMPILER_FLAG([-wd 10237], [dnl
326			warnflags="${warnflags} -wd 10237"])])
327
328	SXE_CHECK_COMPILER_FLAG([-diag-disable 2102], [dnl
329		warnflags="${warnflags} -diag-disable 2102"], [
330		SXE_CHECK_COMPILER_FLAG([-wd 2102], [dnl
331			warnflags="${warnflags} -wd 2102"])])
332
333	SXE_CHECK_COMPILER_FLAG([-debug inline-debug-info], [
334		warnflags="${warnflags} -debug inline-debug-info"])
335
336	SXE_CHECK_COMPILER_FLAG([-diag-enable remark,vec,par], [
337		warnflags="${warnflags} -diag-enable remark,vec,par"])
338
339	## for dfp754
340	SXE_CHECK_COMPILER_FLAG([-Wunsuffixed-float-constants], [
341		warnflags="$warnflags -Wunsuffixed-float-constants"])
342
343	AC_MSG_CHECKING([for preferred warning flags])
344	AC_MSG_RESULT([${warnflags}])
345])dnl SXE_WARNFLAGS
346
347AC_DEFUN([SXE_OPTIFLAGS], [dnl
348	AC_REQUIRE([SXE_USER_CFLAGS])
349	AC_REQUIRE([SXE_WARNFLAGS])
350
351	case " ${CFLAGS} ${EXTRA_CFLAGS} " in
352	(*" -O"[[0-9]]" "*)
353		;;
354	(*" -Os "*)
355		;;
356	(*" -Og "*)
357		;;
358	(*" -Ofast "*)
359		;;
360	(*" -O "*)
361		;;
362	(*)
363		SXE_CHECK_COMPILER_FLAG([-O3], [
364			optiflags="${optiflags} -O3"])
365		;;
366	esac
367
368	SXE_CHECK_COMPILER_FLAG([-ipo], [
369		optiflags="${optiflags} -ipo"
370
371		AC_CHECK_TOOLS([AR], [xiar ar], [false])
372		AC_CHECK_TOOLS([LD], [xild ld], [false])
373
374		## fiddle with xiar and xild params, kick ansi aliasing warnings
375		if test "${ac_cv_prog_ac_ct_AR}" = "xiar"; then
376			AR="${AR} -qdiag-disable=2102"
377		fi
378		if test "${ac_cv_prog_ac_ct_LD}" = "xild"; then
379			LD="${LD} -qdiag-disable=2102"
380		fi
381	])
382
383	SXE_CHECK_COMPILER_FLAG([-no-prec-div], [
384		optiflags="${optiflags} -no-prec-div"])
385])dnl SXE_OPTIFLAGS
386
387AC_DEFUN([SXE_CC_NATIVE], [dnl
388dnl Usage: SXE_CC_NATIVE([yes|no])
389	AC_ARG_ENABLE([native], [dnl
390AS_HELP_STRING(m4_case([$1], [yes], [--disable-native], [--enable-native]), [
391Use code native to the build machine.])],
392		[enable_native="${enableval}"], [enable_native="$1"])
393
394	## -fast implies -static which is a dream but
395	## packager prefer dynamic binaries
396	dnl SXE_CHECK_COMPILER_FLAG([-fast], [
397	dnl 	optiflags="${optiflags} -fast"])
398
399	## auto-vectorisation
400	dnl SXE_CHECK_COMPILER_FLAG([-axMIC-AVX512,CORE-AVX2,CORE-AVX-I,AVX,SSSE3], [
401	dnl 	optiflags="${optiflags} -axMIC-AVX512,CORE-AVX2,CORE-AVX-I,AVX,SSSE3"])
402
403	if test "${enable_native}" = "yes"; then
404		case " ${CFLAGS} ${EXTRA_CFLAGS}" in
405		(*" -mtune"*)
406			## don't tune
407			;;
408		(*" -march"*)
409			## don't set march
410			;;
411		(*" -m32 "*)
412			## don't bother
413			;;
414		(*" -m64 "*)
415			## don't bother
416			;;
417		(*)
418			SXE_CHECK_COMPILER_FLAG([-xHost], [
419				optiflags="${optiflags} -xHost"], [
420				## non-icc
421				SXE_CHECK_COMPILER_FLAG([-mtune=native -march=native], [
422					optiflags="${optiflags} -mtune=native -march=native"])
423			])
424			;;
425		esac
426	fi
427])dnl SXE_CC_NATIVE
428
429AC_DEFUN([SXE_FEATFLAGS], [dnl
430	## default flags for needed features
431	AC_REQUIRE([SXE_CHECK_COMPILER_XFLAG])
432	XCCFLAG="${XFLAG}"
433
434	## recent gentoos went ballistic again, they compile PIE gcc's
435	## but there's no way to turn that misconduct off ...
436	## however I've got one report about a working PIE build
437	## we'll just check for -nopie here, if it works, we turn it on
438	## (and hence PIE off) and hope bug 16 remains fixed
439	SXE_CHECK_COMPILER_FLAG([-nopie],
440		[featflags="$featflags -nopie"])
441
442	## icc and gcc related
443	## check if some stuff can be staticalised
444	## actually requires SXE_WARNFLAGS so warnings would be disabled
445	## that affect the outcome of the following tests
446	SXE_CHECK_COMPILER_FLAG([-static-intel], [
447		featflags="${featflags} -static-intel"
448		XCCLDFLAGS="${XCCLDFLAGS} \${XCCFLAG} -static-intel"], [:])
449	SXE_CHECK_COMPILER_FLAG([-static-libgcc], [
450		featflags="${featflags} -static-libgcc"
451		XCCLDFLAGS="${XCCLDFLAGS} \${XCCFLAG} -static-libgcc"], [:])
452
453	SXE_CHECK_COMPILER_FLAG([-intel-extensions], [dnl
454		featflags="${featflags} -intel-extensions"])
455
456	## check if ipo needs passing to the linker
457	if test "${sxe_cv_c_flag__ipo}" = "yes"; then
458		XCCLDFLAGS="${XCCLDFLAGS} \${XCCFLAG} -ipo"
459	fi
460	## also pass on some diags to the linker
461	if test "${sxe_cv_c_flag__diag_disable_10237}" = "yes"; then
462		XCCLDFLAGS="${XCCLDFLAGS} \${XCCFLAG} -diag-disable=10237"
463	fi
464	if test "${sxe_cv_c_flag__diag_disable_2102}" = "yes"; then
465		XCCLDFLAGS="${XCCLDFLAGS} \${XCCFLAG} -diag-disable=2102"
466	fi
467
468	AC_SUBST([XCCLDFLAGS])
469	AC_SUBST([XCCFLAG])
470])dnl SXE_FEATFLAGS
471
472AC_DEFUN([SXE_CHECK_COMPILER_XFLAG], [dnl
473	if test "${XFLAG}" = ""; then
474		SXE_CHECK_CCLD_FLAG([-XCClinker -foo], [XFLAG="-XCClinker"])
475	fi
476	if test "${XFLAG}" = ""; then
477		SXE_CHECK_CCLD_FLAG([-Xlinker -foo], [XFLAG="-Xlinker"])
478	fi
479
480	AC_SUBST([XFLAG])
481])dnl SXE_CHECK_COMPILER_XFLAG
482
483AC_DEFUN([SXE_USER_CFLAGS], [dnl
484	AC_MSG_CHECKING([for user provided CFLAGS/EXTRA_CFLAGS])
485
486	CFLAGS="${ac_cv_env_CFLAGS_value}"
487	AC_MSG_RESULT([${CFLAGS} ${EXTRA_CFLAGS}])
488])dnl SXE_USER_CFLAGS
489
490
491AC_DEFUN([SXE_CHECK_CFLAGS], [dnl
492dnl Usage: SXE_CHECK_CFLAGS([option ...])
493dnl valid options include:
494dnl + native[=yes|no]  Emit the --enable-native flag
495
496	## those are passed on to our determined CFLAGS
497	AC_ARG_VAR([EXTRA_CFLAGS], [C compiler flags to be APPENDED.])
498
499	## check for user provided flags
500	AC_REQUIRE([SXE_USER_CFLAGS])
501	## Use either command line flag, environment var, or autodetection
502	SXE_DEBUGFLAGS
503	SXE_WARNFLAGS
504	SXE_OPTIFLAGS
505	m4_foreach_w([opt], [$1], [dnl
506		m4_case(opt,
507			[native], [SXE_CC_NATIVE],
508			[native=yes], [SXE_CC_NATIVE([yes])],
509			[native=no], [SXE_CC_NATIVE([no])])
510	])
511	SXE_CFLAGS="${SXE_CFLAGS} ${debugflags} ${optiflags} ${warnflags}"
512
513	SXE_FEATFLAGS
514	SXE_CFLAGS="${SXE_CFLAGS} ${featflags}"
515
516	save_ac_c_werror_flag="${ac_c_werror_flag}"
517
518	CFLAGS="${CFLAGS} ${SXE_CFLAGS} ${EXTRA_CFLAGS}"
519	AC_MSG_CHECKING([for preferred CFLAGS])
520	AC_MSG_RESULT([${CFLAGS}])
521
522	AC_MSG_NOTICE([
523If you wish to APPEND your own flags you want to stop here and rerun the
524configure script like so:
525  configure EXTRA_CFLAGS=<to-be-added-flags>
526
527If you wish to OVERRIDE these flags you want to stop here too and rerun
528the configure script like this:
529  configure CFLAGS=<the-definitive-flags-I-want>
530
531You can always override the determined CFLAGS, partially or totally,
532using
533  make -C <directory> CFLAGS=<your-own-flags> [target]
534or
535  make CFLAGS=<your-own-flags> [target]
536respectively
537		])
538
539	ac_c_werror_flag="${save_ac_c_werror_flag}"
540])dnl SXE_CHECK_CFLAGS
541
542AC_DEFUN([SXE_CHECK_CC], [dnl
543dnl SXE_CHECK_CC([STANDARDS])
544dnl standards are flavours supported by the compiler chosen with AC_PROG_CC
545	pushdef([stds], m4_default([$1], [gnu11 c11 gnu99 c99]))
546
547	AC_REQUIRE([AC_CANONICAL_HOST])
548	AC_REQUIRE([AC_CANONICAL_BUILD])
549	AC_REQUIRE([AC_PROG_CPP])
550	AC_REQUIRE([AC_PROG_CC])
551
552	AC_HEADER_STDC
553
554	case "${CC}" in dnl (
555	*"-std="*)
556		## user specified a std value already
557		;;
558		dnl (
559	*)
560		for i in []stds[]; do
561			SXE_CHECK_COMPILER_FLAG([-std="${i}"], [
562				std="-std=${i}"
563				save_CC="${CC}"
564				CC="${CC} ${std}"
565				SXE_CHECK_ANON_STRUCTS_DECL
566				CC="${save_CC}"
567				if test "${sxe_cv_have_anon_structs_decl}" \
568					= "yes"; then
569					break
570				fi
571			])
572		done
573
574		AC_MSG_CHECKING([for preferred CC std])
575		AC_MSG_RESULT([${std}])
576		CC="${CC} ${std}"
577
578		## while we're at it, check for anon initialising too
579		SXE_CHECK_ANON_STRUCTS_INIT
580		## oh and sloppy sloppy init
581		SXE_CHECK_SLOPPY_STRUCTS_INIT
582		;;
583	esac
584
585	popdef([stds])
586])dnl SXE_CHECK_CC
587
588AC_DEFUN([SXE_CHECK_ANON_STRUCTS_INIT], [
589	AC_MSG_CHECKING([dnl
590whether C compiler can initialise anonymous structs and unions])
591	AC_LANG_PUSH([C])
592
593	## backup our CFLAGS and unset it
594	save_CFLAGS="${CFLAGS}"
595	CFLAGS=""
596
597	AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
598union __test_u {
599	int i;
600	struct {
601		char c;
602		char padc;
603		short int pads;
604	};
605};
606	]], [[
607	union __test_u tmp = {.c = '4'};
608	]])], [
609		sxe_cv_have_anon_structs_init="yes"
610	], [
611		sxe_cv_have_anon_structs_init="no"
612	])
613	AC_MSG_RESULT([${sxe_cv_have_anon_structs_init}])
614
615	## restore CFLAGS
616	CFLAGS="${save_CFLAGS}"
617
618	if test "${sxe_cv_have_anon_structs_init}" = "yes"; then
619		AC_DEFINE([HAVE_ANON_STRUCTS_INIT], [1], [dnl
620Whether c11 anon struct initialising works])
621		$1
622		:
623	else
624		$2
625		:
626	fi
627	AC_LANG_POP()
628])dnl SXE_CHECK_ANON_STRUCTS_INIT
629
630AC_DEFUN([SXE_CHECK_ANON_STRUCTS_DECL], [
631	AC_MSG_CHECKING([dnl
632whether C compiler can understand anonymous structs and unions])
633	AC_LANG_PUSH([C])
634
635	## backup our CFLAGS and unset it
636	save_CFLAGS="${CFLAGS}"
637	CFLAGS=""
638
639	AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
640union __test_u {
641	int i;
642	struct {
643		char c;
644		char padc;
645		short int pads;
646	};
647};
648	]], [[
649	/* nothing to do really*/
650	union __test_u foo;
651	foo.c = 0;
652	]])], [
653		sxe_cv_have_anon_structs_decl="yes"
654	], [
655		sxe_cv_have_anon_structs_decl="no"
656	])
657	AC_MSG_RESULT([${sxe_cv_have_anon_structs_decl}])
658
659	## restore CFLAGS
660	CFLAGS="${save_CFLAGS}"
661
662	if test "${sxe_cv_have_anon_structs_decl}" = "yes"; then
663		AC_DEFINE([HAVE_ANON_STRUCTS_DECL], [1], [dnl
664Whether c11 anon structs declaring works])
665		$1
666		:
667	else
668		$2
669		:
670	fi
671	AC_LANG_POP()
672])dnl SXE_CHECK_ANON_STRUCTS_DECL
673
674AC_DEFUN([SXE_CHECK_SLOPPY_STRUCTS_INIT], [
675	AC_LANG_PUSH([C])
676
677	## backup our CFLAGS and unset it
678	save_CFLAGS="${CFLAGS}"
679	CFLAGS="-Werror"
680
681	SXE_CHECK_COMPILER_FLAG([-Wmissing-field-initializers], [
682		CFLAGS="${CFLAGS} -Wmissing-field-initializers"])
683
684	AC_MSG_CHECKING([dnl
685whether C compiler can initialise structs and unions in a sloppy way])
686
687	AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
688struct __test_s {
689	int i;
690	int j;
691};
692	]], [[
693	struct __test_s tmp = {};
694	]])], [
695		sxe_cv_have_sloppy_structs_init="yes"
696	], [
697		sxe_cv_have_sloppy_structs_init="no"
698	])
699	AC_MSG_RESULT([${sxe_cv_have_sloppy_structs_init}])
700
701	## restore CFLAGS
702	CFLAGS="${save_CFLAGS}"
703
704	if test "${sxe_cv_have_sloppy_structs_init}" = "yes"; then
705		AC_DEFINE([HAVE_SLOPPY_STRUCTS_INIT], [1], [dnl
706Whether sloppy struct initialising works])
707		$1
708		:
709	else
710		$2
711		:
712	fi
713	AC_LANG_POP()
714])dnl SXE_CHECK_SLOPPY_STRUCTS_INIT
715
716AC_DEFUN([SXE_CHECK_INTRINS], [dnl
717	AC_CHECK_HEADERS([immintrin.h])
718	AC_CHECK_HEADERS([x86intrin.h])
719	AC_CHECK_HEADERS([ia32intrin.h])
720	AC_CHECK_HEADERS([popcntintrin.h])
721	AC_CHECK_TYPES([__m128i], [], [], [[
722#if defined HAVE_X86INTRIN_H
723# include <x86intrin.h>
724#elif defined HAVE_IMMINTRIN_H
725# include <immintrin.h>
726#endif
727]])
728	AC_CHECK_TYPES([__m256i], [], [], [[
729#if defined HAVE_X86INTRIN_H
730# include <x86intrin.h>
731#elif defined HAVE_IMMINTRIN_H
732# include <immintrin.h>
733#endif
734]])
735	AC_CHECK_TYPES([__m512i], [], [], [[
736#if defined HAVE_X86INTRIN_H
737# include <x86intrin.h>
738#elif defined HAVE_IMMINTRIN_H
739# include <immintrin.h>
740#endif
741]])
742	AC_CHECK_TYPES([__mmask64], [], [], [[
743#if defined HAVE_X86INTRIN_H
744# include <x86intrin.h>
745#elif defined HAVE_IMMINTRIN_H
746# include <immintrin.h>
747#endif
748]])
749])dnl SXE_CHECK_INTRINS
750
751AC_DEFUN([SXE_CHECK_SIMD], [dnl
752dnl Usage: SXE_CHECK_SIMD([INTRIN], [[SNIPPET], [IF-FOUND], [IF-NOT-FOUND]])
753	AC_REQUIRE([SXE_CHECK_INTRINS])
754
755	AC_MSG_CHECKING([for SIMD routine $1])
756	AC_LINK_IFELSE([AC_LANG_PROGRAM([[
757#if defined HAVE_IA32INTRIN_H
758# include <ia32intrin.h>
759#endif
760#if defined HAVE_X86INTRIN_H
761# include <x86intrin.h>
762#endif
763#if defined HAVE_IMMINTRIN_H
764# include <immintrin.h>
765#endif
766#if defined HAVE_POPCNTINTRIN_H
767# include <popcntintrin.h>
768#endif
769]], [ifelse([$2],[],[$1(0U)],[$2]);])], [
770	eval AS_TR_SH(ac_cv_func_$1)="yes"
771	AC_DEFINE(AS_TR_CPP([HAVE_$1]), [1], [dnl
772Define to 1 if you have the `$1' simd routine])
773	$3
774], [
775	eval AS_TR_SH(ac_cv_func_$1)="no"
776	$4
777])
778	AC_MSG_RESULT([${ac_cv_func_$1}])
779])dnl SXE_CHECK_SIMD
780
781AC_DEFUN([SXE_CHECK_CILK], [dnl
782dnl Usage: SXE_CHECK_CILK([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
783dnl defines sxe_cv_feat_cilk to "yes" if applicable, "no" otherwise
784dnl also AC_DEFINEs HAVE_CILK
785	AC_CHECK_HEADERS([cilk/cilk.h])
786
787	save_CFLAGS="${CFLAGS}"
788	SXE_CHECK_COMPILER_FLAG([-fcilkplus], [CFLAGS="${CFLAGS} -fcilkplus"])
789
790	AC_MSG_CHECKING([whether Cilk+ keywords work])
791	AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
792#include <stdlib.h>
793#if defined HAVE_CILK_CILK_H
794# include <cilk/cilk.h>
795#else  /* !HAVE_CILK_CILK_H */
796# define cilk_spawn	_Cilk_spawn
797# define cilk_sync	_Cilk_sync
798# define cilk_for	_Cilk_for
799#endif /* HAVE_CILK_CILK_H */
800
801static char *trick;
802
803static int pcmp(const void *x, const void *y)
804{
805	return (const char*)x - (const char*)y;
806}
807]], [
808int x = 0;
809int j;
810
811cilk_spawn qsort(trick, 1, 2, pcmp);
812qsort(trick + 4, 1, 2, pcmp);
813cilk_sync;
814
815cilk_for(j = 0; j < 8; j++) {
816	x++;
817}
818])], [
819	AC_DEFINE([HAVE_CILK], [1], [define when compiler supports Cilk+ keywords])
820	sxe_cv_feat_cilk="yes"
821	$1
822], [
823	CFLAGS="${save_CFLAGS}"
824	sxe_cv_feat_cilk="no"
825	$2
826])
827	AC_MSG_RESULT([${sxe_cv_feat_cilk}])
828])dnl SXE_CHECK_CILK
829
830dnl sxe-compiler.m4 ends here
831