r18152: move our AC macros into lib/replace/libreplace_macros.m4
[samba.git] / source4 / build / m4 / check_cc.m4
1 dnl SMB Build Environment CC Checks
2 dnl -------------------------------------------------------
3 dnl  Copyright (C) Stefan (metze) Metzmacher 2004
4 dnl  Released under the GNU GPL
5 dnl -------------------------------------------------------
6 dnl
7
8 # don't let the AC_PROG_CC macro auto set the CFLAGS
9 OLD_CFLAGS="${CFLAGS}"
10 AC_PROG_CC
11 CFLAGS="${OLD_CFLAGS}"
12 if test x"$CC" = x""; then
13         AC_MSG_WARN([No c compiler was not found!])
14         AC_MSG_ERROR([Please Install gcc from http://gcc.gnu.org/])
15 fi
16
17 #
18 # Set the debug symbol option if we have
19 # --enable-*developer or --enable-debug
20 # and the compiler supports it
21 #
22 if test x$ac_cv_prog_cc_g = xyes -a x$debug = xyes; then
23         CFLAGS="${CFLAGS} -g"
24 fi
25
26 dnl needed before AC_TRY_COMPILE
27 AC_ISC_POSIX
28
29 AC_C_INLINE
30
31 AC_CACHE_CHECK([that the C compiler can precompile header files],samba_cv_precompiled_headers, [
32         dnl Check whether the compiler can generate precompiled headers
33         touch conftest.h
34         if ${CC-cc} conftest.h 2> /dev/null && test -f conftest.h.gch; then
35                 precompiled_headers=yes
36         else
37                 precompiled_headers=no
38         fi])
39 AC_SUBST(precompiled_headers)
40
41
42 dnl Check if the C compiler understands volatile (it should, being ANSI).
43 AC_CACHE_CHECK([that the C compiler understands volatile],samba_cv_volatile, [
44         AC_TRY_COMPILE([#include <sys/types.h>],[volatile int i = 0],
45                 samba_cv_volatile=yes,samba_cv_volatile=no)])
46 if test x"$samba_cv_volatile" = x"yes"; then
47         AC_DEFINE(HAVE_VOLATILE, 1, [Whether the C compiler understands volatile])
48 fi
49
50 ############################################
51 # check if the compiler can do immediate structures
52 AC_CACHE_CHECK([for immediate structures],samba_cv_immediate_structures, [
53     AC_TRY_COMPILE([
54 #include <stdio.h>],
55 [
56    typedef struct {unsigned x;} FOOBAR;
57    #define X_FOOBAR(x) ((FOOBAR) { x })
58    #define FOO_ONE X_FOOBAR(1)
59    FOOBAR f = FOO_ONE;   
60    static struct {
61         FOOBAR y; 
62         } f2[] = {
63                 {FOO_ONE}
64         };   
65 ],
66         samba_cv_immediate_structures=yes,samba_cv_immediate_structures=no)])
67 if test x"$samba_cv_immediate_structures" = x"yes"; then
68    AC_DEFINE(HAVE_IMMEDIATE_STRUCTURES,1,[Whether the compiler supports immediate structures])
69 fi
70 AC_SUBST(samba_cv_immediate_structures)
71
72 ############################################
73 # check if the compiler handles c99 struct initialization
74 LIBREPLACE_C99_STRUCT_INIT(samba_cv_c99_struct_initialization=yes,
75                     samba_cv_c99_struct_initialization=no)
76
77 if test x"$samba_cv_c99_struct_initialization" != x"yes"; then
78         AC_MSG_WARN([C compiler does not support c99 struct initialization!])
79         AC_MSG_ERROR([Please Install gcc from http://gcc.gnu.org/])
80 fi
81
82 ############################################
83 # check if the compiler can handle negative enum values
84 # and don't truncate the values to INT_MAX
85 # a runtime test is needed here
86 AC_CACHE_CHECK([that the C compiler understands negative enum values],SMB_BUILD_CC_NEGATIVE_ENUM_VALUES, [
87     AC_TRY_RUN(
88 [
89         #include <stdio.h>
90         enum negative_values { NEGATIVE_VALUE = 0xFFFFFFFF };
91         int main(void) {
92                 enum negative_values v1 = NEGATIVE_VALUE;
93                 unsigned v2 = NEGATIVE_VALUE;
94
95                 if (v1 != 0xFFFFFFFF) {
96                         printf("%u != 0xFFFFFFFF\n", v1);
97                         return 1;
98                 }
99                 if (v2 != 0xFFFFFFFF) {
100                         printf("%u != 0xFFFFFFFF\n", v2);
101                         return 1;
102                 }
103
104                 return 0;
105         }
106 ],
107         SMB_BUILD_CC_NEGATIVE_ENUM_VALUES=yes,SMB_BUILD_CC_NEGATIVE_ENUM_VALUES=no)])
108 if test x"$SMB_BUILD_CC_NEGATIVE_ENUM_VALUES" != x"yes"; then
109         AC_MSG_WARN([using --unit-enums for pidl])
110         PIDL_ARGS="$PIDL_ARGS --uint-enums"
111 fi
112
113 AC_MSG_CHECKING([for test routines])
114 AC_TRY_RUN([#include "${srcdir-.}/build/tests/trivial.c"],
115             AC_MSG_RESULT(yes),
116             AC_MSG_ERROR([cant find test code. Aborting config]),
117             AC_MSG_WARN([cannot run when cross-compiling]))
118
119 #
120 # Check if the compiler support ELF visibility for symbols
121 #
122
123 visibility_attribute=no
124 VISIBILITY_CFLAGS=""
125 if test x"$GCC" = x"yes" ; then
126         AX_CFLAGS_GCC_OPTION([-fvisibility=hidden], VISIBILITY_CFLAGS)
127 fi
128
129 if test -n "$VISIBILITY_CFLAGS"; then
130         AC_MSG_CHECKING([whether the C compiler supports the visibility attribute])
131         OLD_CFLAGS="$CFLAGS"
132
133         CFLAGS="$CFLAGS $VISIBILITY_CFLAGS"
134         AC_TRY_RUN([
135                 void vis_foo1(void) {}
136                 __attribute__((visibility("default"))) void vis_foo2(void) {}
137                 #include "${srcdir-.}/build/tests/trivial.c"
138         ],[
139                 AC_MSG_RESULT(yes)
140                 AC_DEFINE(HAVE_VISIBILITY_ATTR,1,[Whether the C compiler supports the visibility attribute])
141                 visibility_attribute=yes
142         ],[
143                 AC_MSG_RESULT(no)
144         ])
145         CFLAGS="$OLD_CFLAGS"
146 fi
147 AC_SUBST(visibility_attribute)
148
149 #
150 # Check if the compiler can handle the options we selected by
151 # --enable-*developer
152 #
153 DEVELOPER_CFLAGS=""
154 if test x$developer = xyes; then
155         OLD_CFLAGS="${CFLAGS}"
156
157         CFLAGS="${CFLAGS} -D_SAMBA_DEVELOPER_DONNOT_USE_O2_"
158         DEVELOPER_CFLAGS="-DDEBUG_PASSWORD -DDEVELOPER"
159         if test x"$GCC" = x"yes" ; then
160             #
161             # warnings we want...
162             #
163             AX_CFLAGS_GCC_OPTION(-Wall, DEVELOPER_CFLAGS)
164             AX_CFLAGS_GCC_OPTION(-Wshadow, DEVELOPER_CFLAGS)
165             AX_CFLAGS_GCC_OPTION(-Werror-implicit-function-declaration, DEVELOPER_CFLAGS)
166             AX_CFLAGS_GCC_OPTION(-Wstrict-prototypes, DEVELOPER_CFLAGS)
167             AX_CFLAGS_GCC_OPTION(-Wpointer-arith, DEVELOPER_CFLAGS)
168             AX_CFLAGS_GCC_OPTION(-Wcast-qual, DEVELOPER_CFLAGS)
169             AX_CFLAGS_GCC_OPTION(-Wcast-align, DEVELOPER_CFLAGS)
170             AX_CFLAGS_GCC_OPTION(-Wwrite-strings, DEVELOPER_CFLAGS)
171             AX_CFLAGS_GCC_OPTION(-Wmissing-format-attribute, DEVELOPER_CFLAGS)
172             AX_CFLAGS_GCC_OPTION(-Wformat=2, DEVELOPER_CFLAGS)
173             AX_CFLAGS_GCC_OPTION(-Wdeclaration-after-statement, DEVELOPER_CFLAGS)
174             AX_CFLAGS_GCC_OPTION(-Wunused-macros, DEVELOPER_CFLAGS)
175 #           AX_CFLAGS_GCC_OPTION(-Wextra, DEVELOPER_CFLAGS)
176 #           AX_CFLAGS_GCC_OPTION(-Wc++-compat, DEVELOPER_CFLAGS)
177 #           AX_CFLAGS_GCC_OPTION(-Wmissing-prototypes, DEVELOPER_CFLAGS)
178 #           AX_CFLAGS_GCC_OPTION(-Wmissing-declarations, DEVELOPER_CFLAGS)
179 #           AX_CFLAGS_GCC_OPTION(-Wmissing-field-initializers, DEVELOPER_CFLAGS)
180             #
181             # warnings we don't want...
182             #
183             AX_CFLAGS_GCC_OPTION(-Wno-format-y2k, DEVELOPER_CFLAGS)
184             AX_CFLAGS_GCC_OPTION(-Wno-unused-parameter, DEVELOPER_CFLAGS)
185         else
186             AX_CFLAGS_IRIX_OPTION(-fullwarn, DEVELOPER_CFLAGS)
187         fi
188
189         CFLAGS="${OLD_CFLAGS}"
190 fi
191 if test -n "$DEVELOPER_CFLAGS"; then
192         OLD_CFLAGS="${CFLAGS}"
193         CFLAGS="${CFLAGS} ${DEVELOPER_CFLAGS}"
194         AC_MSG_CHECKING([that the C compiler can use the DEVELOPER_CFLAGS])
195         AC_TRY_COMPILE([],[],
196                 AC_MSG_RESULT(yes),
197                 DEVELOPER_CFLAGS=""; AC_MSG_RESULT(no))
198         CFLAGS="${OLD_CFLAGS}"
199 fi
200
201 # allow for --with-hostcc=gcc
202 AC_ARG_WITH(hostcc,[  --with-hostcc=compiler    choose host compiler],
203 [HOSTCC=$withval],
204 [
205 if test z"$cross_compiling" = "yes"; then 
206         HOSTCC=cc
207 else 
208         HOSTCC=$CC
209 fi
210 ])
211 AC_SUBST(HOSTCC)
212
213 AC_PATH_PROG(GCOV,gcov)
214
215 AC_PATH_PROG(MAKE,make)
216
217 AC_CACHE_CHECK([whether we have GNU make], samba_cv_gnu_make, [
218 if $ac_cv_path_MAKE --version | head -1 | grep GNU 2>/dev/null >/dev/null
219 then
220         samba_cv_gnu_make=yes
221 else
222         samba_cv_gnu_make=no
223 fi
224 ])
225
226 GNU_MAKE=$samba_cv_gnu_make
227 AC_SUBST(GNU_MAKE)
228
229 if test "x$GNU_MAKE" = x"yes"; then
230         AC_CACHE_CHECK([GNU make version], samba_cv_gnu_make_version,[
231                 samba_cv_gnu_make_version=`$ac_cv_path_MAKE --version | head -1 | cut -d " " -f 3 2>/dev/null`
232         ])
233         GNU_MAKE_VERSION=$samba_cv_gnu_make_version
234         AC_SUBST(GNU_MAKE_VERSION)
235 fi
236
237
238 new_make=no
239 AC_MSG_CHECKING([for GNU make >= 3.81])
240 if test x$GNU_MAKE = x"yes"; then
241         if $PERL -e " \$_ = '$GNU_MAKE_VERSION'; s/@<:@^\d\.@:>@.*//g; exit (\$_ < 3.81);"; then
242                 new_make=yes
243         fi
244 fi
245 AC_MSG_RESULT($new_make)
246 automatic_dependencies=no
247 AX_CFLAGS_GCC_OPTION([-M -MT conftest.d -MF conftest.o], [], [ automatic_dependencies=$new_make ], [])
248 AC_MSG_CHECKING([Whether to use automatic dependencies])
249 AC_MSG_RESULT($automatic_dependencies)
250 AC_SUBST(automatic_dependencies)