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