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