xref: /freebsd/contrib/ntp/sntp/m4/ntp_compiler.m4 (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1dnl ######################################################################
2dnl Common m4sh code for compiler stuff
3AC_DEFUN([NTP_COMPILER], [
4
5AC_USE_SYSTEM_EXTENSIONS
6
7# Ralf Wildenhues: With per-target flags we need CC_C_O
8# AM_PROG_CC_C_O supersets AC_PROG_CC_C_O
9AM_PROG_CC_C_O
10AC_PROG_GCC_TRADITIONAL
11AC_REQUIRE([AC_PROG_CC_STDC])
12dnl AC_REQUIRE([AC_PROG_CC_C89])
13dnl AC_REQUIRE([AC_PROG_CC_C99])
14
15CC_NOFORMAT=
16CFLAGS_NTP=
17CPPFLAGS_NTP=
18LDADD_NTP=
19LDFLAGS_NTP=
20AC_SUBST([CC_NOFORMAT])
21AC_SUBST([CFLAGS_NTP])
22AC_SUBST([CPPFLAGS_NTP])
23AC_SUBST([LDADD_NTP])
24AC_SUBST([LDFLAGS_NTP])
25
26case "$ac_cv_prog_cc_c89" in
27 no)
28    AC_MSG_WARN([ANSI C89/ISO C90 is the minimum to compile NTP]
29		[ version 4.2.5 and higher.])
30    ;;
31esac
32
33AC_CACHE_CHECK(
34    [if $CC can handle @%:@warning],
35    [ntp_cv_cpp_warning],
36    [AC_COMPILE_IFELSE(
37	[AC_LANG_PROGRAM([[]], [[#warning foo]])],
38	[ntp_cv_cpp_warning=yes],
39	[ntp_cv_cpp_warning=no]
40    )]
41)
42case "$ntp_cv_cpp_warning" in
43 no)
44    AC_DEFINE([NO_OPTION_NAME_WARNINGS], [1],
45	[Should we avoid @%:@warning on option name collisions?])
46esac
47
48AC_CACHE_CHECK(
49    [if $CC supports __attribute__((...))],
50    [ntp_cv_cc_attribute],
51    [AC_COMPILE_IFELSE(
52	[AC_LANG_PROGRAM(
53	    [[]],
54	    [[void foo(void) __attribute__((__noreturn__));]]
55	    )],
56	[ntp_cv_cc_attribute=yes],
57	[ntp_cv_cc_attribute=no]
58    )]
59)
60case "$ntp_cv_cc_attribute" in
61 yes)
62    AC_DEFINE([HAVE___ATTRIBUTE__], [],
63	[defined if C compiler supports __attribute__((...))])
64esac
65AH_VERBATIM(
66    [HAVE___ATTRIBUTE___VERBATIM],
67    [
68	/* define away __attribute__() if unsupported */
69	#ifndef HAVE___ATTRIBUTE__
70	# define __attribute__(x) /* empty */
71	#endif
72	#define ISC_PLATFORM_NORETURN_PRE
73	#define ISC_PLATFORM_NORETURN_POST __attribute__((__noreturn__))
74    ]
75)
76
77case "$GCC" in
78 yes)
79    SAVED_CFLAGS="$CFLAGS"
80    CFLAGS="$SAVED_CFLAGS -Wstrict-overflow"
81    AC_CACHE_CHECK(
82	[if $CC can handle -Wstrict-overflow],
83	[ntp_cv_gcc_Wstrict_overflow],
84	[AC_COMPILE_IFELSE(
85	    [AC_LANG_PROGRAM([[]], [[]])],
86	    [ntp_cv_gcc_Wstrict_overflow=yes],
87	    [ntp_cv_gcc_Wstrict_overflow=no]
88	)	]
89    )
90    #
91    # $ntp_cv_gcc_Wstrict_overflow is tested later to add the
92    # flag to CFLAGS.
93    #
94    CFLAGS="$SAVED_CFLAGS -Winit-self"
95    AC_CACHE_CHECK(
96	[if $CC can handle -Winit-self],
97	[ntp_cv_gcc_Winit_self],
98	[
99	    AC_COMPILE_IFELSE(
100		[AC_LANG_PROGRAM([[]], [[]])],
101		[ntp_cv_gcc_Winit_self=yes],
102		[ntp_cv_gcc_Winit_self=no]
103	    )
104	]
105    )
106    #
107    # libopts specifically builds a string with embedded NULs.
108    # This causes a bunch of distracting warnings due to -Wformat.
109    # Let's see if we can figure out how to disable these.
110    #
111    CFLAGS="$SAVED_CFLAGS -Wno-format"
112    AC_CACHE_CHECK(
113	[if $CC can handle -Wno-format],
114	[ntp_cv_gcc_Wno_format],
115	[
116	    AC_COMPILE_IFELSE(
117		[AC_LANG_PROGRAM([[]], [[]])],
118		[ntp_cv_gcc_Wno_format=yes],
119		[ntp_cv_gcc_Wno_format=no]
120	    )
121	]
122    )
123
124    case "$ntp_cv_gcc_Wno_format" in
125     yes)
126	CC_NOFORMAT="$CC_NOFORMAT -Wno-format"
127	;;
128     no)
129	;;
130    esac
131
132    CFLAGS="$SAVED_CFLAGS"
133    AS_UNSET([SAVED_CFLAGS])
134    #
135    # $ntp_cv_gcc_Winit_self is tested later to add the
136    # flag to CFLAGS_NTP.
137    #
138    AC_CACHE_CHECK(
139	[if linker supports omitting unused code and data],
140	[ntp_cv_gc_sections_runs],
141	[
142	    dnl  NetBSD will link but likely not run with --gc-sections
143	    dnl  http://bugs.ntp.org/1844
144	    dnl  http://gnats.netbsd.org/40401
145	    dnl  --gc-sections causes attempt to load as linux elf, with
146	    dnl  wrong syscalls in place.  Test a little gauntlet of
147	    dnl  simple stdio read code checking for errors, expecting
148	    dnl  enough syscall differences that the NetBSD code will
149	    dnl  fail even with Linux emulation working as designed.
150	    dnl  A shorter test could be refined by someone with access
151	    dnl  to a NetBSD host with Linux emulation working.
152	    origCFLAGS="$CFLAGS"
153	    CFLAGS="$CFLAGS -Wl,--gc-sections"
154	    AC_LINK_IFELSE(
155		[AC_LANG_PROGRAM(
156		    [[
157			#include <stdlib.h>
158			#include <stdio.h>
159		    ]],
160		    [[
161			FILE *	fpC;
162			char	buf[32];
163			size_t	cch;
164			int	read_success_once;
165
166			fpC = fopen("conftest.c", "r");
167			if (NULL == fpC)
168				exit(1);
169			do {
170				cch = fread(buf, sizeof(buf), 1, fpC);
171				read_success_once |= (0 != cch);
172			} while (0 != cch);
173			if (!read_success_once)
174				exit(2);
175			if (!feof(fpC))
176				exit(3);
177			if (0 != fclose(fpC))
178				exit(4);
179
180			exit(EXIT_SUCCESS);
181		    ]]
182		)],
183		[
184		    if test "X$cross_compiling" = "Xyes" || grep gc-sections conftest.err ; then
185			ntp_cv_gc_sections_runs=no
186		    else
187			ntp_cv_gc_sections_runs=no
188			./conftest >/dev/null 2>&1 && ntp_cv_gc_sections_runs=yes
189		    fi
190		],
191		[ntp_cv_gc_sections_runs=no]
192	    )
193	    CFLAGS="$origCFLAGS"
194	    AS_UNSET([origCFLAGS])
195	]
196    )
197    case "$ntp_cv_gc_sections_runs" in
198     yes)
199	LDADD_LIBNTP="-Wl,--gc-sections"
200	CFLAGS_NTP="$CFLAGS_NTP -ffunction-sections -fdata-sections"
201	;;
202     no)
203	LDADD_LIBNTP=
204	;;
205    esac
206    CFLAGS_NTP="$CFLAGS_NTP -Wall"
207    CFLAGS_NTP="$CFLAGS_NTP -Wcast-align"
208    CFLAGS_NTP="$CFLAGS_NTP -Wcast-qual"
209    # CFLAGS_NTP="$CFLAGS_NTP -Wconversion"
210    # CFLAGS_NTP="$CFLAGS_NTP -Werror"
211    # CFLAGS_NTP="$CFLAGS_NTP -Wextra"
212    # CFLAGS_NTP="$CFLAGS_NTP -Wfloat-equal"
213    CFLAGS_NTP="$CFLAGS_NTP -Wmissing-prototypes"
214    CFLAGS_NTP="$CFLAGS_NTP -Wpointer-arith"
215    CFLAGS_NTP="$CFLAGS_NTP -Wshadow"
216    # CFLAGS_NTP="$CFLAGS_NTP -Wtraditional"
217    # CFLAGS_NTP="$CFLAGS_NTP -Wwrite-strings"
218    case "$ntp_cv_gcc_Winit_self" in
219     yes)
220	CFLAGS_NTP="$CFLAGS_NTP -Winit-self"
221    esac
222    case "$ntp_cv_gcc_Wstrict_overflow" in
223     yes)
224	CFLAGS_NTP="$CFLAGS_NTP -Wstrict-overflow"
225    esac
226    # -W[no-]strict-prototypes might be added by NTP_OPENSSL
227esac
228
229NTP_OS_CFLAGS
230
231AC_C_BIGENDIAN
232AC_C_VOLATILE
233AC_PROG_CPP
234
235])dnl
236dnl ======================================================================
237