* acinclude.m4: Use -Werror on parameter tests.
[grub.git] / acinclude.m4
1 dnl Redefine AC_LANG_PROGRAM with a "-Wstrict-prototypes -Werror"-friendly
2 dnl version.  Patch submitted to bug-autoconf in 2009-09-16.
3 m4_define([AC_LANG_PROGRAM(C)],
4 [$1
5 int
6 main (void)
7 {
8 dnl Do *not* indent the following line: there may be CPP directives.
9 dnl Don't move the `;' right after for the same reason.
10 $2
11   ;
12   return 0;
13 }])
14
15
16 dnl Check whether target compiler is working
17 AC_DEFUN([grub_PROG_TARGET_CC],
18 [AC_MSG_CHECKING([whether target compiler is working])
19 AC_CACHE_VAL(grub_cv_prog_target_cc,
20 [AC_LINK_IFELSE([AC_LANG_PROGRAM([[
21 asm (".globl start; start:");
22 int main (void);
23 ]], [[]])],
24                 [grub_cv_prog_target_cc=yes],
25                 [grub_cv_prog_target_cc=no])
26 ])
27 AC_MSG_RESULT([$grub_cv_prog_target_cc])
28
29 if test "x$grub_cv_prog_target_cc" = xno; then
30   AC_MSG_ERROR([cannot compile for the target])
31 fi
32 ])
33
34
35 dnl grub_ASM_USCORE checks if C symbols get an underscore after
36 dnl compiling to assembler.
37 dnl Written by Pavel Roskin. Based on grub_ASM_EXT_C written by
38 dnl Erich Boleyn and modified by Yoshinori K. Okuji.
39 AC_DEFUN([grub_ASM_USCORE],
40 [AC_REQUIRE([AC_PROG_CC])
41 AC_REQUIRE([AC_PROG_EGREP])
42 AC_MSG_CHECKING([if C symbols get an underscore after compilation])
43 AC_CACHE_VAL(grub_cv_asm_uscore,
44 [cat > conftest.c <<\EOF
45 int func (int *);
46 int
47 func (int *list)
48 {
49   *list = 0;
50   return *list;
51 }
52 EOF
53
54 if AC_TRY_COMMAND([${CC-cc} ${CFLAGS} -S conftest.c]) && test -s conftest.s; then
55   true
56 else
57   AC_MSG_ERROR([${CC-cc} failed to produce assembly code])
58 fi
59
60 if $EGREP '(^|[^_[:alnum]])_func' conftest.s >/dev/null 2>&1; then
61   HAVE_ASM_USCORE=1
62   grub_cv_asm_uscore=yes
63 else
64   HAVE_ASM_USCORE=0
65   grub_cv_asm_uscore=no
66 fi
67
68 rm -f conftest*])
69
70 AC_MSG_RESULT([$grub_cv_asm_uscore])
71 ])
72
73
74 dnl Some versions of `objcopy -O binary' vary their output depending
75 dnl on the link address.
76 AC_DEFUN([grub_PROG_OBJCOPY_ABSOLUTE],
77 [AC_MSG_CHECKING([whether ${TARGET_OBJCOPY} works for absolute addresses])
78 AC_CACHE_VAL(grub_cv_prog_objcopy_absolute,
79 [cat > conftest.c <<\EOF
80 void cmain (void);
81 void
82 cmain (void)
83 {
84    *((int *) 0x1000) = 2;
85 }
86 EOF
87
88 if AC_TRY_EVAL(ac_compile) && test -s conftest.o; then :
89 else
90   AC_MSG_ERROR([${CC-cc} cannot compile C source code])
91 fi
92 grub_cv_prog_objcopy_absolute=yes
93 for link_addr in 0x2000 0x8000 0x7C00; do
94   if AC_TRY_COMMAND([${CC-cc} ${CFLAGS} -nostdlib ${TARGET_IMG_LDFLAGS_AC} ${TARGET_IMG_BASE_LDOPT},$link_addr conftest.o -o conftest.exec]); then :
95   else
96     AC_MSG_ERROR([${CC-cc} cannot link at address $link_addr])
97   fi
98   if AC_TRY_COMMAND([${TARGET_OBJCOPY-objcopy} --only-section=.text -O binary conftest.exec conftest]); then :
99   else
100     AC_MSG_ERROR([${TARGET_OBJCOPY-objcopy} cannot create binary files])
101   fi
102   if test ! -f conftest.old || AC_TRY_COMMAND([cmp -s conftest.old conftest]); then
103     mv -f conftest conftest.old
104   else
105     grub_cv_prog_objcopy_absolute=no
106     break
107   fi
108 done
109 rm -f conftest*])
110 AC_MSG_RESULT([$grub_cv_prog_objcopy_absolute])
111
112 if test "x$grub_cv_prog_objcopy_absolute" = xno; then
113   AC_MSG_ERROR([GRUB requires a working absolute objcopy; upgrade your binutils])
114 fi
115 ])
116
117
118 dnl Supply --build-id=none to ld if building modules.
119 dnl This suppresses warnings from ld on some systems
120 AC_DEFUN([grub_PROG_LD_BUILD_ID_NONE],
121 [AC_MSG_CHECKING([whether linker accepts --build-id=none])
122 AC_CACHE_VAL(grub_cv_prog_ld_build_id_none,
123 [save_LDFLAGS="$LDFLAGS"
124 LDFLAGS="$LDFLAGS -Wl,--build-id=none"
125 AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
126                [grub_cv_prog_ld_build_id_none=yes],
127                [grub_cv_prog_ld_build_id_none=no])
128 LDFLAGS="$save_LDFLAGS"
129 ])
130 AC_MSG_RESULT([$grub_cv_prog_ld_build_id_none])
131
132 if test "x$grub_cv_prog_ld_build_id_none" = xyes; then
133   TARGET_LDFLAGS="$TARGET_LDFLAGS -Wl,--build-id=none"
134 fi
135 ])
136
137 dnl Supply -P to nm
138 AC_DEFUN([grub_PROG_NM_MINUS_P],
139 [AC_MSG_CHECKING([whether nm accepts -P])
140 AC_CACHE_VAL(grub_cv_prog_nm_minus_p,
141 [
142 nm_minus_p_tmp_dir="$(mktemp -d "./confXXXXXX")"
143 AC_LANG_CONFTEST([AC_LANG_PROGRAM([[]], [[]])])
144 $TARGET_CC conftest.c -o "$nm_minus_p_tmp_dir/ef"
145 if $TARGET_NM -P "$nm_minus_p_tmp_dir/ef" 2>&1 > /dev/null; then
146    grub_cv_prog_nm_minus_p=yes
147 else
148    grub_cv_prog_nm_minus_p=no
149 fi
150 rm "$nm_minus_p_tmp_dir/ef"
151 ])
152 AC_MSG_RESULT([$grub_cv_prog_nm_minus_p])
153
154 if test "x$grub_cv_prog_nm_minus_p" = xyes; then
155   TARGET_NMFLAGS_MINUS_P="-P"
156 else
157   TARGET_NMFLAGS_MINUS_P=
158 fi
159 ])
160
161 dnl Supply --defined-only to nm
162 AC_DEFUN([grub_PROG_NM_DEFINED_ONLY],
163 [AC_MSG_CHECKING([whether nm accepts --defined-only])
164 AC_CACHE_VAL(grub_cv_prog_nm_defined_only,
165 [
166 nm_defined_only_tmp_dir="$(mktemp -d "./confXXXXXX")"
167 AC_LANG_CONFTEST([AC_LANG_PROGRAM([[]], [[]])])
168 $TARGET_CC conftest.c -o "$nm_defined_only_tmp_dir/ef"
169 if $TARGET_NM --defined-only "$nm_defined_only_tmp_dir/ef" 2>&1 > /dev/null; then
170    grub_cv_prog_nm_defined_only=yes
171 else
172    grub_cv_prog_nm_defined_only=no
173 fi
174 rm "$nm_defined_only_tmp_dir/ef"
175 ])
176 AC_MSG_RESULT([$grub_cv_prog_nm_defined_only])
177
178 if test "x$grub_cv_prog_nm_defined_only" = xyes; then
179   TARGET_NMFLAGS_DEFINED_ONLY=--defined-only
180 else
181   TARGET_NMFLAGS_DEFINED_ONLY=
182 fi
183 ])
184
185
186 dnl Mass confusion!
187 dnl Older versions of GAS interpret `.code16' to mean ``generate 32-bit
188 dnl instructions, but implicitly insert addr32 and data32 bytes so
189 dnl that the code works in real mode''.
190 dnl
191 dnl Newer versions of GAS interpret `.code16' to mean ``generate 16-bit
192 dnl instructions,'' which seems right.  This requires the programmer
193 dnl to explicitly insert addr32 and data32 instructions when they want
194 dnl them.
195 dnl
196 dnl We only support the newer versions, because the old versions cause
197 dnl major pain, by requiring manual assembly to get 16-bit instructions into
198 dnl asm files.
199 AC_DEFUN([grub_I386_ASM_ADDR32],
200 [AC_REQUIRE([AC_PROG_CC])
201 AC_REQUIRE([grub_I386_ASM_PREFIX_REQUIREMENT])
202 AC_MSG_CHECKING([for .code16 addr32 assembler support])
203 AC_CACHE_VAL(grub_cv_i386_asm_addr32,
204 [cat > conftest.s.in <<\EOF
205         .code16
206 l1:     @ADDR32@        movb    %al, l1
207 EOF
208
209 if test "x$grub_cv_i386_asm_prefix_requirement" = xyes; then
210   sed -e s/@ADDR32@/addr32/ < conftest.s.in > conftest.s
211 else
212   sed -e s/@ADDR32@/addr32\;/ < conftest.s.in > conftest.s
213 fi
214
215 if AC_TRY_COMMAND([${CC-cc} ${TARGET_CCASFLAGS} ${CFLAGS} -c conftest.s]) && test -s conftest.o; then
216   grub_cv_i386_asm_addr32=yes
217 else
218   grub_cv_i386_asm_addr32=no
219 fi
220
221 rm -f conftest*])
222
223 AC_MSG_RESULT([$grub_cv_i386_asm_addr32])])
224
225 dnl check if our compiler is apple cc
226 dnl because it requires numerous workarounds
227 AC_DEFUN([grub_apple_cc],
228 [AC_REQUIRE([AC_PROG_CC])
229 AC_MSG_CHECKING([whether our compiler is apple cc])
230 AC_CACHE_VAL(grub_cv_apple_cc,
231 [if $CC -v 2>&1 | grep "Apple Inc." > /dev/null; then
232   grub_cv_apple_cc=yes
233 else
234   grub_cv_apple_cc=no
235 fi
236 ])
237
238 AC_MSG_RESULT([$grub_cv_apple_cc])])
239
240 dnl check if our target compiler is apple cc
241 dnl because it requires numerous workarounds
242 AC_DEFUN([grub_apple_target_cc],
243 [AC_REQUIRE([AC_PROG_CC])
244 AC_MSG_CHECKING([whether our target compiler is apple cc])
245 AC_CACHE_VAL(grub_cv_apple_target_cc,
246 [if $CC -v 2>&1 | grep "Apple Inc." > /dev/null; then
247   grub_cv_apple_target_cc=yes
248 else
249   grub_cv_apple_target_cc=no
250 fi
251 ])
252
253 AC_MSG_RESULT([$grub_cv_apple_target_cc])])
254
255
256 dnl Later versions of GAS requires that addr32 and data32 prefixes
257 dnl appear in the same lines as the instructions they modify, while
258 dnl earlier versions requires that they appear in separate lines.
259 AC_DEFUN([grub_I386_ASM_PREFIX_REQUIREMENT],
260 [AC_REQUIRE([AC_PROG_CC])
261 AC_MSG_CHECKING(dnl
262 [whether addr32 must be in the same line as the instruction])
263 AC_CACHE_VAL(grub_cv_i386_asm_prefix_requirement,
264 [cat > conftest.s <<\EOF
265         .code16
266 l1:     addr32  movb    %al, l1
267 EOF
268
269 if AC_TRY_COMMAND([${CC-cc} ${TARGET_CCASFLAGS} ${CFLAGS} -c conftest.s]) && test -s conftest.o; then
270   grub_cv_i386_asm_prefix_requirement=yes
271 else
272   grub_cv_i386_asm_prefix_requirement=no
273 fi
274
275 rm -f conftest*])
276
277 if test "x$grub_cv_i386_asm_prefix_requirement" = xyes; then
278   grub_tmp_addr32="addr32"
279   grub_tmp_data32="data32"
280 else
281   grub_tmp_addr32="addr32;"
282   grub_tmp_data32="data32;"
283 fi
284
285 ADDR32=$grub_tmp_addr32
286 DATA32=$grub_tmp_data32
287
288 AC_MSG_RESULT([$grub_cv_i386_asm_prefix_requirement])])
289
290
291 dnl Check what symbol is defined as a bss start symbol.
292 dnl Written by Michael Hohmoth and Yoshinori K. Okuji.
293 AC_DEFUN([grub_CHECK_BSS_START_SYMBOL],
294 [AC_REQUIRE([AC_PROG_CC])
295 AC_MSG_CHECKING([if __bss_start is defined by the compiler])
296 AC_CACHE_VAL(grub_cv_check_uscore_uscore_bss_start_symbol,
297 [AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],
298                 [[asm ("incl __bss_start")]])],
299                 [grub_cv_check_uscore_uscore_bss_start_symbol=yes],
300                 [grub_cv_check_uscore_uscore_bss_start_symbol=no])])
301
302 AC_MSG_RESULT([$grub_cv_check_uscore_uscore_bss_start_symbol])
303
304 AC_MSG_CHECKING([if edata is defined by the compiler])
305 AC_CACHE_VAL(grub_cv_check_edata_symbol,
306 [AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],
307                 [[asm ("incl edata")]])],
308                 [grub_cv_check_edata_symbol=yes],
309                 [grub_cv_check_edata_symbol=no])])
310
311 AC_MSG_RESULT([$grub_cv_check_edata_symbol])
312
313 AC_MSG_CHECKING([if _edata is defined by the compiler])
314 AC_CACHE_VAL(grub_cv_check_uscore_edata_symbol,
315 [AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],
316                 [[asm ("incl _edata")]])],
317                 [grub_cv_check_uscore_edata_symbol=yes],
318                 [grub_cv_check_uscore_edata_symbol=no])])
319
320 AC_MSG_RESULT([$grub_cv_check_uscore_edata_symbol])
321
322 if test "x$grub_cv_check_uscore_uscore_bss_start_symbol" = xyes; then
323   BSS_START_SYMBOL=__bss_start
324 elif test "x$grub_cv_check_edata_symbol" = xyes; then
325   BSS_START_SYMBOL=edata
326 elif test "x$grub_cv_check_uscore_edata_symbol" = xyes; then
327   BSS_START_SYMBOL=_edata
328 else
329   AC_MSG_ERROR([none of __bss_start, edata or _edata is defined])
330 fi
331 ])
332
333 dnl Check what symbol is defined as an end symbol.
334 dnl Written by Yoshinori K. Okuji.
335 AC_DEFUN([grub_CHECK_END_SYMBOL],
336 [AC_REQUIRE([AC_PROG_CC])
337 AC_MSG_CHECKING([if end is defined by the compiler])
338 AC_CACHE_VAL(grub_cv_check_end_symbol,
339 [AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],
340                 [[asm ("incl end")]])],
341                 [grub_cv_check_end_symbol=yes],
342                 [grub_cv_check_end_symbol=no])])
343
344 AC_MSG_RESULT([$grub_cv_check_end_symbol])
345
346 AC_MSG_CHECKING([if _end is defined by the compiler])
347 AC_CACHE_VAL(grub_cv_check_uscore_end_symbol,
348 [AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],
349                 [[asm ("incl _end")]])],
350                 [grub_cv_check_uscore_end_symbol=yes],
351                 [grub_cv_check_uscore_end_symbol=no])])
352
353 AC_MSG_RESULT([$grub_cv_check_uscore_end_symbol])
354
355 if test "x$grub_cv_check_end_symbol" = xyes; then
356   END_SYMBOL=end
357 elif test "x$grub_cv_check_uscore_end_symbol" = xyes; then
358   END_SYMBOL=_end
359 else
360   AC_MSG_ERROR([neither end nor _end is defined])
361 fi
362 ])
363
364 \f
365 dnl Check if the C compiler supports `-fstack-protector'.
366 AC_DEFUN([grub_CHECK_STACK_PROTECTOR],[
367 [# Smashing stack protector.
368 ssp_possible=yes]
369 AC_MSG_CHECKING([whether `$CC' accepts `-fstack-protector'])
370 # Is this a reliable test case?
371 AC_LANG_CONFTEST([AC_LANG_SOURCE([[
372 void foo (void) { volatile char a[8]; a[3]; }
373 ]])])
374 [# `$CC -c -o ...' might not be portable.  But, oh, well...  Is calling
375 # `ac_compile' like this correct, after all?
376 if eval "$ac_compile -S -fstack-protector -o conftest.s" 2> /dev/null; then]
377   AC_MSG_RESULT([yes])
378   [# Should we clear up other files as well, having called `AC_LANG_CONFTEST'?
379   rm -f conftest.s
380 else
381   ssp_possible=no]
382   AC_MSG_RESULT([no])
383 [fi]
384 ])
385
386 dnl Check if the C compiler supports `-mstack-arg-probe' (Cygwin).
387 AC_DEFUN([grub_CHECK_STACK_ARG_PROBE],[
388 [# Smashing stack arg probe.
389 sap_possible=yes]
390 AC_MSG_CHECKING([whether `$CC' accepts `-mstack-arg-probe'])
391 AC_LANG_CONFTEST([AC_LANG_SOURCE([[
392 void foo (void) { volatile char a[8]; a[3]; }
393 ]])])
394 [if eval "$ac_compile -S -mstack-arg-probe -Werror -o conftest.s" 2> /dev/null; then]
395   AC_MSG_RESULT([yes])
396   [# Should we clear up other files as well, having called `AC_LANG_CONFTEST'?
397   rm -f conftest.s
398 else
399   sap_possible=no]
400   AC_MSG_RESULT([no])
401 [fi]
402 ])
403
404 dnl Check if ln can handle directories properly (mingw).
405 AC_DEFUN([grub_CHECK_LINK_DIR],[
406 AC_MSG_CHECKING([whether ln can handle directories properly])
407 [mkdir testdir 2>/dev/null
408 case $srcdir in
409 [\\/$]* | ?:[\\/]* ) reldir=$srcdir/include/grub/util ;;
410     *) reldir=../$srcdir/include/grub/util ;;
411 esac
412 if ln -s $reldir testdir/util 2>/dev/null ; then]
413   AC_MSG_RESULT([yes])
414   [link_dir=yes
415 else
416   link_dir=no]
417   AC_MSG_RESULT([no])
418 [fi
419 rm -rf testdir]
420 ])
421
422 dnl Check if the C compiler supports `-fPIE'.
423 AC_DEFUN([grub_CHECK_PIE],[
424 [# Position independent executable.
425 pie_possible=yes]
426 AC_MSG_CHECKING([whether `$CC' has `-fPIE' as default])
427 # Is this a reliable test case?
428 AC_LANG_CONFTEST([AC_LANG_SOURCE([[
429 #ifdef __PIE__
430 int main() {
431         return 0;
432 }
433 #else
434 #error NO __PIE__ DEFINED
435 #endif
436 ]])])
437
438 [# `$CC -c -o ...' might not be portable.  But, oh, well...  Is calling
439 # `ac_compile' like this correct, after all?
440 if eval "$ac_compile -S -o conftest.s" 2> /dev/null; then]
441   AC_MSG_RESULT([yes])
442   [# Should we clear up other files as well, having called `AC_LANG_CONFTEST'?
443   rm -f conftest.s
444 else
445   pie_possible=no]
446   AC_MSG_RESULT([no])
447 [fi]
448 ])
449
450 dnl Check if the C compiler supports `-fPIC'.
451 AC_DEFUN([grub_CHECK_PIC],[
452 [# Position independent executable.
453 pic_possible=yes]
454 AC_MSG_CHECKING([whether `$CC' has `-fPIC' as default])
455 # Is this a reliable test case?
456 AC_LANG_CONFTEST([AC_LANG_SOURCE([[
457 #ifdef __PIC__
458 int main() {
459         return 0;
460 }
461 #else
462 #error NO __PIC__ DEFINED
463 #endif
464 ]])])
465
466 [# `$CC -c -o ...' might not be portable.  But, oh, well...  Is calling
467 # `ac_compile' like this correct, after all?
468 if eval "$ac_compile -S -o conftest.s" 2> /dev/null; then]
469   AC_MSG_RESULT([yes])
470   [# Should we clear up other files as well, having called `AC_LANG_CONFTEST'?
471   rm -f conftest.s
472 else
473   pic_possible=no]
474   AC_MSG_RESULT([no])
475 [fi]
476 ])
477
478 dnl Create an output variable with the transformed name of a GRUB utility
479 dnl program.
480 AC_DEFUN([grub_TRANSFORM],[dnl
481 AC_SUBST(AS_TR_SH([$1]), [`AS_ECHO([$1]) | sed "$program_transform_name"`])dnl
482 ])
483
484 dnl Check if the C compiler supports `-mno-unaligned-access'.
485 AC_DEFUN([grub_CHECK_NO_UNALIGNED_ACCESS],[
486 [# foobar
487 nua_possible=yes]
488 AC_MSG_CHECKING([whether `$CC' supports `-mno-unaligned-access'])
489 AC_LANG_CONFTEST([AC_LANG_SOURCE([[
490 int main() {
491         return 0;
492 }
493 ]])])
494
495 [if eval "$ac_compile -S -mno-unaligned-access -o conftest.s" 2> /dev/null; then]
496   AC_MSG_RESULT([yes])
497   [rm -f conftest.s
498 else
499   nua_possible=no]
500   AC_MSG_RESULT([no])
501 [fi]
502 ])