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