Merge mainline into keylayouts
[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   grub_cv_asm_uscore=yes
62 else
63   grub_cv_asm_uscore=no
64 fi
65
66 rm -f conftest*])
67
68 if test "x$grub_cv_asm_uscore" = xyes; then
69   AC_DEFINE_UNQUOTED([HAVE_ASM_USCORE], $grub_cv_asm_uscore,
70     [Define if C symbols get an underscore after compilation])
71 fi
72
73 AC_MSG_RESULT([$grub_cv_asm_uscore])
74 ])
75
76
77 dnl Some versions of `objcopy -O binary' vary their output depending
78 dnl on the link address.
79 AC_DEFUN([grub_PROG_OBJCOPY_ABSOLUTE],
80 [AC_MSG_CHECKING([whether ${OBJCOPY} works for absolute addresses])
81 AC_CACHE_VAL(grub_cv_prog_objcopy_absolute,
82 [cat > conftest.c <<\EOF
83 void cmain (void);
84 void
85 cmain (void)
86 {
87    *((int *) 0x1000) = 2;
88 }
89 EOF
90
91 if AC_TRY_EVAL(ac_compile) && test -s conftest.o; then :
92 else
93   AC_MSG_ERROR([${CC-cc} cannot compile C source code])
94 fi
95 grub_cv_prog_objcopy_absolute=yes
96 for link_addr in 0x2000 0x8000 0x7C00; do
97   if AC_TRY_COMMAND([${CC-cc} ${CFLAGS} -nostdlib ${TARGET_IMG_LDFLAGS_AC}$link_addr conftest.o -o conftest.exec]); then :
98   else
99     AC_MSG_ERROR([${CC-cc} cannot link at address $link_addr])
100   fi
101   if AC_TRY_COMMAND([${OBJCOPY-objcopy} --only-section=.text -O binary conftest.exec conftest]); then :
102   else
103     AC_MSG_ERROR([${OBJCOPY-objcopy} cannot create binary files])
104   fi
105   if test ! -f conftest.old || AC_TRY_COMMAND([cmp -s conftest.old conftest]); then
106     mv -f conftest conftest.old
107   else
108     grub_cv_prog_objcopy_absolute=no
109     break
110   fi
111 done
112 rm -f conftest*])
113 AC_MSG_RESULT([$grub_cv_prog_objcopy_absolute])
114
115 if test "x$grub_cv_prog_objcopy_absolute" = xno; then
116   AC_MSG_ERROR([GRUB requires a working absolute objcopy; upgrade your binutils])
117 fi
118 ])
119
120
121 dnl Supply --build-id=none to ld if building modules.
122 dnl This suppresses warnings from ld on some systems
123 AC_DEFUN([grub_PROG_LD_BUILD_ID_NONE],
124 [AC_MSG_CHECKING([whether linker accepts --build-id=none])
125 AC_CACHE_VAL(grub_cv_prog_ld_build_id_none,
126 [save_LDFLAGS="$LDFLAGS"
127 LDFLAGS="$LDFLAGS -Wl,--build-id=none"
128 AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
129                [grub_cv_prog_ld_build_id_none=yes],
130                [grub_cv_prog_ld_build_id_none=no])
131 LDFLAGS="$save_LDFLAGS"
132 ])
133 AC_MSG_RESULT([$grub_cv_prog_ld_build_id_none])
134
135 if test "x$grub_cv_prog_ld_build_id_none" = xyes; then
136   TARGET_LDFLAGS="$TARGET_LDFLAGS -Wl,--build-id=none"
137 fi
138 ])
139
140
141 dnl Mass confusion!
142 dnl Older versions of GAS interpret `.code16' to mean ``generate 32-bit
143 dnl instructions, but implicitly insert addr32 and data32 bytes so
144 dnl that the code works in real mode''.
145 dnl
146 dnl Newer versions of GAS interpret `.code16' to mean ``generate 16-bit
147 dnl instructions,'' which seems right.  This requires the programmer
148 dnl to explicitly insert addr32 and data32 instructions when they want
149 dnl them.
150 dnl
151 dnl We only support the newer versions, because the old versions cause
152 dnl major pain, by requiring manual assembly to get 16-bit instructions into
153 dnl asm files.
154 AC_DEFUN([grub_I386_ASM_ADDR32],
155 [AC_REQUIRE([AC_PROG_CC])
156 AC_REQUIRE([grub_I386_ASM_PREFIX_REQUIREMENT])
157 AC_MSG_CHECKING([for .code16 addr32 assembler support])
158 AC_CACHE_VAL(grub_cv_i386_asm_addr32,
159 [cat > conftest.s.in <<\EOF
160         .code16
161 l1:     @ADDR32@        movb    %al, l1
162 EOF
163
164 if test "x$grub_cv_i386_asm_prefix_requirement" = xyes; then
165   sed -e s/@ADDR32@/addr32/ < conftest.s.in > conftest.s
166 else
167   sed -e s/@ADDR32@/addr32\;/ < conftest.s.in > conftest.s
168 fi
169
170 if AC_TRY_COMMAND([${CC-cc} ${CFLAGS} -c conftest.s]) && test -s conftest.o; then
171   grub_cv_i386_asm_addr32=yes
172 else
173   grub_cv_i386_asm_addr32=no
174 fi
175
176 rm -f conftest*])
177
178 AC_MSG_RESULT([$grub_cv_i386_asm_addr32])])
179
180 dnl check if our compiler is apple cc
181 dnl because it requires numerous workarounds
182 AC_DEFUN([grub_apple_cc],
183 [AC_REQUIRE([AC_PROG_CC])
184 AC_MSG_CHECKING([whether our compiler is apple cc])
185 AC_CACHE_VAL(grub_cv_apple_cc,
186 [if $CC -v 2>&1 | grep "Apple Inc." > /dev/null; then
187   grub_cv_apple_cc=yes
188 else
189   grub_cv_apple_cc=no
190 fi
191 ])
192
193 AC_MSG_RESULT([$grub_cv_apple_cc])])
194
195 dnl check if our target compiler is apple cc
196 dnl because it requires numerous workarounds
197 AC_DEFUN([grub_apple_target_cc],
198 [AC_REQUIRE([AC_PROG_CC])
199 AC_MSG_CHECKING([whether our target compiler is apple cc])
200 AC_CACHE_VAL(grub_cv_apple_target_cc,
201 [if $CC -v 2>&1 | grep "Apple Inc." > /dev/null; then
202   grub_cv_apple_target_cc=yes
203 else
204   grub_cv_apple_target_cc=no
205 fi
206 ])
207
208 AC_MSG_RESULT([$grub_cv_apple_target_cc])])
209
210
211 dnl Later versions of GAS requires that addr32 and data32 prefixes
212 dnl appear in the same lines as the instructions they modify, while
213 dnl earlier versions requires that they appear in separate lines.
214 AC_DEFUN([grub_I386_ASM_PREFIX_REQUIREMENT],
215 [AC_REQUIRE([AC_PROG_CC])
216 AC_MSG_CHECKING(dnl
217 [whether addr32 must be in the same line as the instruction])
218 AC_CACHE_VAL(grub_cv_i386_asm_prefix_requirement,
219 [cat > conftest.s <<\EOF
220         .code16
221 l1:     addr32  movb    %al, l1
222 EOF
223
224 if AC_TRY_COMMAND([${CC-cc} ${CFLAGS} -c conftest.s]) && test -s conftest.o; then
225   grub_cv_i386_asm_prefix_requirement=yes
226 else
227   grub_cv_i386_asm_prefix_requirement=no
228 fi
229
230 rm -f conftest*])
231
232 if test "x$grub_cv_i386_asm_prefix_requirement" = xyes; then
233   grub_tmp_addr32="addr32"
234   grub_tmp_data32="data32"
235 else
236   grub_tmp_addr32="addr32;"
237   grub_tmp_data32="data32;"
238 fi
239
240 AC_DEFINE_UNQUOTED([ADDR32], $grub_tmp_addr32,
241   [Define it to \"addr32\" or \"addr32;\" to make GAS happy])
242 AC_DEFINE_UNQUOTED([DATA32], $grub_tmp_data32,
243   [Define it to \"data32\" or \"data32;\" to make GAS happy])
244
245 AC_MSG_RESULT([$grub_cv_i386_asm_prefix_requirement])])
246
247
248 dnl Older versions of GAS require that absolute indirect calls/jumps are
249 dnl not prefixed with `*', while later versions warn if not prefixed.
250 AC_DEFUN([grub_I386_ASM_ABSOLUTE_WITHOUT_ASTERISK],
251 [AC_REQUIRE([AC_PROG_CC])
252 AC_MSG_CHECKING(dnl
253 [whether an absolute indirect call/jump must not be prefixed with an asterisk])
254 AC_CACHE_VAL(grub_cv_i386_asm_absolute_without_asterisk,
255 [cat > conftest.s <<\EOF
256         lcall   *(offset)
257 offset:
258         .long   0
259         .word   0
260 EOF
261
262 if AC_TRY_COMMAND([${CC-cc} ${CFLAGS} -c conftest.s]) && test -s conftest.o; then
263   grub_cv_i386_asm_absolute_without_asterisk=no
264 else
265   grub_cv_i386_asm_absolute_without_asterisk=yes
266 fi
267
268 rm -f conftest*])
269
270 if test "x$grub_cv_i386_asm_absolute_without_asterisk" = xyes; then
271   AC_DEFINE([ABSOLUTE_WITHOUT_ASTERISK], 1,
272             [Define it if GAS requires that absolute indirect calls/jumps are not prefixed with an asterisk])
273 fi
274
275 AC_MSG_RESULT([$grub_cv_i386_asm_absolute_without_asterisk])])
276
277
278 dnl Check what symbol is defined as a bss start symbol.
279 dnl Written by Michael Hohmoth and Yoshinori K. Okuji.
280 AC_DEFUN([grub_CHECK_BSS_START_SYMBOL],
281 [AC_REQUIRE([AC_PROG_CC])
282 AC_MSG_CHECKING([if __bss_start is defined by the compiler])
283 AC_CACHE_VAL(grub_cv_check_uscore_uscore_bss_start_symbol,
284 [AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],
285                 [[asm ("incl __bss_start")]])],
286                 [grub_cv_check_uscore_uscore_bss_start_symbol=yes],
287                 [grub_cv_check_uscore_uscore_bss_start_symbol=no])])
288
289 AC_MSG_RESULT([$grub_cv_check_uscore_uscore_bss_start_symbol])
290
291 AC_MSG_CHECKING([if edata is defined by the compiler])
292 AC_CACHE_VAL(grub_cv_check_edata_symbol,
293 [AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],
294                 [[asm ("incl edata")]])],
295                 [grub_cv_check_edata_symbol=yes],
296                 [grub_cv_check_edata_symbol=no])])
297
298 AC_MSG_RESULT([$grub_cv_check_edata_symbol])
299
300 AC_MSG_CHECKING([if _edata is defined by the compiler])
301 AC_CACHE_VAL(grub_cv_check_uscore_edata_symbol,
302 [AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],
303                 [[asm ("incl _edata")]])],
304                 [grub_cv_check_uscore_edata_symbol=yes],
305                 [grub_cv_check_uscore_edata_symbol=no])])
306
307 AC_MSG_RESULT([$grub_cv_check_uscore_edata_symbol])
308
309 AH_TEMPLATE([BSS_START_SYMBOL], [Define it to one of __bss_start, edata and _edata])
310
311 if test "x$grub_cv_check_uscore_uscore_bss_start_symbol" = xyes; then
312   AC_DEFINE([BSS_START_SYMBOL], [__bss_start])
313 elif test "x$grub_cv_check_edata_symbol" = xyes; then
314   AC_DEFINE([BSS_START_SYMBOL], [edata])
315 elif test "x$grub_cv_check_uscore_edata_symbol" = xyes; then
316   AC_DEFINE([BSS_START_SYMBOL], [_edata])
317 else
318   AC_MSG_ERROR([none of __bss_start, edata or _edata is defined])
319 fi
320 ])
321
322 dnl Check what symbol is defined as an end symbol.
323 dnl Written by Yoshinori K. Okuji.
324 AC_DEFUN([grub_CHECK_END_SYMBOL],
325 [AC_REQUIRE([AC_PROG_CC])
326 AC_MSG_CHECKING([if end is defined by the compiler])
327 AC_CACHE_VAL(grub_cv_check_end_symbol,
328 [AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],
329                 [[asm ("incl end")]])],
330                 [grub_cv_check_end_symbol=yes],
331                 [grub_cv_check_end_symbol=no])])
332
333 AC_MSG_RESULT([$grub_cv_check_end_symbol])
334
335 AC_MSG_CHECKING([if _end is defined by the compiler])
336 AC_CACHE_VAL(grub_cv_check_uscore_end_symbol,
337 [AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],
338                 [[asm ("incl _end")]])],
339                 [grub_cv_check_uscore_end_symbol=yes],
340                 [grub_cv_check_uscore_end_symbol=no])])
341
342 AC_MSG_RESULT([$grub_cv_check_uscore_end_symbol])
343
344 AH_TEMPLATE([END_SYMBOL], [Define it to either end or _end])
345
346 if test "x$grub_cv_check_end_symbol" = xyes; then
347   AC_DEFINE([END_SYMBOL], [end])
348 elif test "x$grub_cv_check_uscore_end_symbol" = xyes; then
349   AC_DEFINE([END_SYMBOL], [_end])
350 else
351   AC_MSG_ERROR([neither end nor _end is defined])
352 fi
353 ])
354
355 dnl Check if the C compiler generates calls to `__enable_execute_stack()'.
356 AC_DEFUN([grub_CHECK_ENABLE_EXECUTE_STACK],[
357 AC_MSG_CHECKING([whether `$CC' generates calls to `__enable_execute_stack()'])
358 AC_LANG_CONFTEST([[
359 void f (int (*p) (void));
360 void g (int i)
361 {
362   int nestedfunc (void) { return i; }
363   f (nestedfunc);
364 }
365 ]])
366 if AC_TRY_COMMAND([${CC-cc} ${CFLAGS} -S conftest.c]) && test -s conftest.s; then
367   true
368 else
369   AC_MSG_ERROR([${CC-cc} failed to produce assembly code])
370 fi
371 if grep __enable_execute_stack conftest.s >/dev/null 2>&1; then
372   AC_DEFINE([NEED_ENABLE_EXECUTE_STACK], 1,
373             [Define to 1 if GCC generates calls to __enable_execute_stack()])
374   AC_MSG_RESULT([yes])
375 else
376   AC_MSG_RESULT([no])
377 fi
378 rm -f conftest*
379 ])
380
381 \f
382 dnl Check if the C compiler supports `-fstack-protector'.
383 AC_DEFUN([grub_CHECK_STACK_PROTECTOR],[
384 [# Smashing stack protector.
385 ssp_possible=yes]
386 AC_MSG_CHECKING([whether `$CC' accepts `-fstack-protector'])
387 # Is this a reliable test case?
388 AC_LANG_CONFTEST([[void foo (void) { volatile char a[8]; a[3]; }]])
389 [# `$CC -c -o ...' might not be portable.  But, oh, well...  Is calling
390 # `ac_compile' like this correct, after all?
391 if eval "$ac_compile -S -fstack-protector -o conftest.s" 2> /dev/null; then]
392   AC_MSG_RESULT([yes])
393   [# Should we clear up other files as well, having called `AC_LANG_CONFTEST'?
394   rm -f conftest.s
395 else
396   ssp_possible=no]
397   AC_MSG_RESULT([no])
398 [fi]
399 ])
400
401 dnl Check if the C compiler supports `-mstack-arg-probe' (Cygwin).
402 AC_DEFUN([grub_CHECK_STACK_ARG_PROBE],[
403 [# Smashing stack arg probe.
404 sap_possible=yes]
405 AC_MSG_CHECKING([whether `$CC' accepts `-mstack-arg-probe'])
406 AC_LANG_CONFTEST([[void foo (void) { volatile char a[8]; a[3]; }]])
407 [if eval "$ac_compile -S -mstack-arg-probe -o conftest.s" 2> /dev/null; then]
408   AC_MSG_RESULT([yes])
409   [# Should we clear up other files as well, having called `AC_LANG_CONFTEST'?
410   rm -f conftest.s
411 else
412   sap_possible=no]
413   AC_MSG_RESULT([no])
414 [fi]
415 ])
416
417 dnl Check if ln can handle directories properly (mingw).
418 AC_DEFUN([grub_CHECK_LINK_DIR],[
419 AC_MSG_CHECKING([whether ln can handle directories properly])
420 [mkdir testdir 2>/dev/null
421 case $srcdir in
422 [\\/$]* | ?:[\\/]* ) reldir=$srcdir/include/grub/util ;;
423     *) reldir=../$srcdir/include/grub/util ;;
424 esac
425 if ln -s $reldir testdir/util 2>/dev/null ; then]
426   AC_MSG_RESULT([yes])
427   [link_dir=yes
428 else
429   link_dir=no]
430   AC_MSG_RESULT([no])
431 [fi
432 rm -rf testdir]
433 ])
434
435 dnl Check if the C compiler supports `-fPIE'.
436 AC_DEFUN([grub_CHECK_PIE],[
437 [# Position independent executable.
438 pie_possible=yes]
439 AC_MSG_CHECKING([whether `$CC' has `-fPIE' as default])
440 # Is this a reliable test case?
441 AC_LANG_CONFTEST([[
442 #ifdef __PIE__
443 int main() {
444         return 0;
445 }
446 #else
447 #error NO __PIE__ DEFINED
448 #endif
449 ]])
450
451 [# `$CC -c -o ...' might not be portable.  But, oh, well...  Is calling
452 # `ac_compile' like this correct, after all?
453 if eval "$ac_compile -S -o conftest.s" 2> /dev/null; then]
454   AC_MSG_RESULT([yes])
455   [# Should we clear up other files as well, having called `AC_LANG_CONFTEST'?
456   rm -f conftest.s
457 else
458   pie_possible=no]
459   AC_MSG_RESULT([no])
460 [fi]
461 ])