configure.ac 66.4 KB
Newer Older
1
2
# -*- autoconf -*-

okuji's avatar
okuji committed
3
4
# Process this file with autoconf to produce a configure script.

5
# Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010  Free Software Foundation, Inc.
okuji's avatar
okuji committed
6
7
8
9
10
11
12
13
14
15
#
# This configure.ac is free software; the author
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.

16
17
18
19
20
21
22
23
24
25
26
27
28
dnl This configure script is complicated, because GRUB needs to deal
dnl with three potentially different types:
dnl
dnl   build  -- the environment for building GRUB
dnl   host   -- the environment for running utilities
dnl   target -- the environment for running GRUB
dnl
dnl In addition, GRUB needs to deal with a platform specification
dnl which specifies the system running GRUB, such as firmware.
dnl This is necessary because the target type in autoconf does not
dnl describe such a system very well.
dnl
dnl The current strategy is to use variables with no prefix (such as
29
30
31
32
dnl CC, CFLAGS, etc.) for the host type, variables with prefix "BUILD_"
dnl (such as BUILD_CC, BUILD_CFLAGS, etc.) for the build type and variables
dnl with the prefix "TARGET_" (such as TARGET_CC, TARGET_CFLAGS, etc.) are
dnl used for the target type. See INSTALL for full list of variables.
33

Vladimir Serbinenko's avatar
Vladimir Serbinenko committed
34
AC_INIT([GRUB],[2.02~beta3],[bug-grub@gnu.org])
okuji's avatar
okuji committed
35

36
37
AC_CONFIG_AUX_DIR([build-aux])

38
39
# We don't want -g -O2 by default in CFLAGS
: ${CFLAGS=""}
okuji's avatar
okuji committed
40

41
42
# Checks for build, host and target systems.
AC_CANONICAL_BUILD
okuji's avatar
okuji committed
43
AC_CANONICAL_HOST
44
save_program_prefix="${program_prefix}"
45
AC_CANONICAL_TARGET
46
program_prefix="${save_program_prefix}"
okuji's avatar
okuji committed
47

48
AM_INIT_AUTOMAKE([1.10.1])
49
AC_PREREQ(2.60)
50
AC_CONFIG_SRCDIR([include/grub/dl.h])
51
AC_CONFIG_HEADER([config-util.h])
bean's avatar
bean committed
52

53
54
# Program name transformations
AC_ARG_PROGRAM
55
56
57
58
59
60
grub_TRANSFORM([grub-bios-setup])
grub_TRANSFORM([grub-editenv])
grub_TRANSFORM([grub-install])
grub_TRANSFORM([grub-mkconfig])
grub_TRANSFORM([grub-mkfont])
grub_TRANSFORM([grub-mkimage])
61
grub_TRANSFORM([grub-glue-efi])
62
63
64
65
66
67
68
69
70
grub_TRANSFORM([grub-mklayout])
grub_TRANSFORM([grub-mkpasswd-pbkdf2])
grub_TRANSFORM([grub-mkrelpath])
grub_TRANSFORM([grub-mkrescue])
grub_TRANSFORM([grub-probe])
grub_TRANSFORM([grub-reboot])
grub_TRANSFORM([grub-script-check])
grub_TRANSFORM([grub-set-default])
grub_TRANSFORM([grub-sparc64-setup])
71
grub_TRANSFORM([grub-render-label])
72
grub_TRANSFORM([grub-file])
73

74
75
76
77
78
# Optimization flag.  Allow user to override.
if test "x$TARGET_CFLAGS" = x; then
  TARGET_CFLAGS="$TARGET_CFLAGS -Os"
fi

79
80
81
82
83
84
# Default HOST_CPPFLAGS
HOST_CPPFLAGS="$HOST_CPPFLAGS -Wall -W"
HOST_CPPFLAGS="$HOST_CPPFLAGS -DGRUB_UTIL=1"

TARGET_CPPFLAGS="$TARGET_CPPFLAGS -Wall -W"

85
case "$target_cpu" in
86
  i[[3456]]86)	target_cpu=i386 ;;
87
  amd64)	target_cpu=x86_64 ;;
88
  sparc)	target_cpu=sparc64 ;;
89
  mipsel|mips64el)
90
91
                target_cpu=mipsel
		machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_CPU_MIPSEL=1"
phcoder's avatar
phcoder committed
92
		;;
93
  mips|mips64)
94
95
                target_cpu=mips
		machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_CPU_MIPS=1"
phcoder's avatar
phcoder committed
96
		;;
Leif Lindholm's avatar
Leif Lindholm committed
97
  arm*)
98
		target_cpu=arm
Leif Lindholm's avatar
Leif Lindholm committed
99
		;;
Leif Lindholm's avatar
Leif Lindholm committed
100
  aarch64*)
101
		target_cpu=arm64
Leif Lindholm's avatar
Leif Lindholm committed
102
		;;
okuji's avatar
okuji committed
103
104
esac

105
106
107
# Specify the platform (such as firmware).
AC_ARG_WITH([platform],
            AS_HELP_STRING([--with-platform=PLATFORM],
108
                           [select the host platform [[guessed]]]))
109
110
111

# Guess the platform if not specified.
if test "x$with_platform" = x; then
112
  case "$target_cpu"-"$target_vendor" in
113
114
    i386-apple) platform=efi ;;
    i386-*) platform=pc ;;
115
    x86_64-apple) platform=efi ;;
116
    x86_64-*) platform=pc ;;
117
    powerpc-*) platform=ieee1275 ;;
118
    powerpc64-*) platform=ieee1275 ;;
119
    powerpc64le-*) platform=ieee1275 ;;
120
    sparc64-*) platform=ieee1275 ;;
121
122
    mipsel-*) platform=loongson ;;
    mips-*) platform=arc ;;
Robert Millan's avatar
Robert Millan committed
123
    ia64-*) platform=efi ;;
Leif Lindholm's avatar
Leif Lindholm committed
124
    arm-*) platform=uboot ;;
Leif Lindholm's avatar
Leif Lindholm committed
125
    arm64-*) platform=efi ;;
126
127
128
129
    *)
      AC_MSG_WARN([unsupported CPU: "$target_cpu" - only building utilities])
      platform=none
      ;;
130
131
132
133
134
  esac
else
  platform="$with_platform"
fi

135
136
137
case "$target_cpu"-"$platform" in
  x86_64-efi) ;;
  x86_64-emu) ;;
138
  x86_64-xen) ;;
139
  x86_64-none) ;;
140
141
  x86_64-*) target_cpu=i386 ;;
  powerpc64-ieee1275) target_cpu=powerpc ;;
142
  powerpc64le-ieee1275) target_cpu=powerpc ;;
143
esac
bean's avatar
bean committed
144

145
# Check if the platform is supported, make final adjustments.
146
case "$target_cpu"-"$platform" in
147
  i386-efi) ;;
148
  x86_64-efi) ;;
149
150
  i386-xen) ;;
  x86_64-xen) ;;
151
  i386-pc) ;;
152
  i386-multiboot) ;;
153
  i386-coreboot) ;;
154
  i386-linuxbios) platform=coreboot ;;
155
  i386-ieee1275) ;;
156
  i386-qemu) ;;
157
158
  powerpc-ieee1275) ;;
  sparc64-ieee1275) ;;
159
  ia64-efi) ;;
160
  mips-qemu_mips) ;;
161
  mips-qemu-mips) platform=qemu_mips;;
Vladimir 'phcoder' Serbinenko's avatar
Vladimir 'phcoder' Serbinenko committed
162
  mips-arc) ;;
163
  mipsel-arc) ;;
164
165
166
167
168
  mipsel-qemu_mips) ;;
  mipsel-qemu-mips) platform=qemu_mips;;
  mipsel-yeeloong) platform=loongson ;;
  mipsel-fuloong) platform=loongson ;;
  mipsel-loongson) ;;
Leif Lindholm's avatar
Leif Lindholm committed
169
170
  arm-uboot) ;;
  arm-efi) ;;
Leif Lindholm's avatar
Leif Lindholm committed
171
  arm64-efi) ;;
172
  *-emu) ;;
173
  *-none) ;;
174
  *) AC_MSG_ERROR([platform "$platform" is not supported for target CPU "$target_cpu"]) ;;
okuji's avatar
okuji committed
175
176
esac

Vladimir Serbinenko's avatar
Vladimir Serbinenko committed
177
if test x$platform != xemu ; then
178
179
180
181
182
   case "$target_cpu" in
	i386 | powerpc) target_m32=1 ;;
	x86_64 | sparc64) target_m64=1 ;;
   esac
fi
183

184
if test x"$target_cpu-$platform" = xsparc64-emu ; then
185
   target_m64=1
186
187
fi

188
case "$target_os" in
189
  windows* | mingw32*) target_os=cygwin ;;
190
191
esac

192
193
194
195
196
197
# This normalizes the names, and creates a new variable ("host_kernel")
# while at it, since the mapping is not always 1:1 (e.g. different OSes
# using the same kernel type).
case "$host_os" in
  gnu*)				host_kernel=hurd ;;
  linux*)			host_kernel=linux ;;
198
  freebsd* | kfreebsd*-gnu)	host_kernel=kfreebsd ;;
199
  netbsd*)			host_kernel=netbsd ;;
200
  solaris*)			host_kernel=illumos ;;
201
  darwin*)			host_kernel=xnu ;;
202
  cygwin | windows* | mingw32*)	host_kernel=windows ;;
203
204
esac

205
206
207
208
209
210
case "$host_os" in
  cygwin | windows* | mingw32*)	have_exec=n ;;
  aros*) have_exec=n ;;
  *) have_exec=y;;
esac

211
case "$platform" in
212
213
214
  coreboot)	machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_COREBOOT=1" ;;
  multiboot)	machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_MULTIBOOT=1" ;;
  efi)		machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_EFI=1" ;;
215
  xen)		machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_XEN=1" ;;
216
  ieee1275)	machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_IEEE1275=1" ;;
Leif Lindholm's avatar
Leif Lindholm committed
217
  uboot)	machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_UBOOT=1" ;;
218
219
220
  qemu)		machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_QEMU=1" ;;
  pc)		machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_PCBIOS=1" ;;
  emu)		machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_EMU=1" ;;
221
222
  loongson)	machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_MIPS_LOONGSON=1" ;;
  qemu_mips)	machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_MIPS_QEMU_MIPS=1" ;;
223
  arc)	machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_ARC=1" ;;
224
esac
225
226
227
if test x${target_cpu} = xmipsel ; then
   machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE=`echo mips_$platform | sed y,abcdefghijklmnopqrstuvwxyz,ABCDEFGHIJKLMNOPQRSTUVWXYZ,`"
else
228
   machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE=`echo ${target_cpu}_$platform | sed y,abcdefghijklmnopqrstuvwxyz,ABCDEFGHIJKLMNOPQRSTUVWXYZ,`"
229
fi
230

231
232
233
234
235
236
237
238
239
case "${target_cpu}-$platform" in
     mips-arc)
        TARGET_LINK_ADDR=0x88200000
        TARGET_DECOMPRESSOR_LINK_ADDR=0x88100000
	;;
     mipsel-arc)
        TARGET_LINK_ADDR=0x80700000
	TARGET_DECOMPRESSOR_LINK_ADDR=0x80600000
	;;
240
     mips*-qemu_mips | mips*-loongson)
241
242
243
244
245
246
247
        TARGET_DECOMPRESSOR_LINK_ADDR=0x80100000
	;;
esac

AC_SUBST(TARGET_LINK_ADDR)
AC_SUBST(TARGET_DECOMPRESSOR_LINK_ADDR)

248
TARGET_CPPFLAGS="$TARGET_CPPFLAGS $machine_CPPFLAGS"
249

250
251
AC_SUBST(host_cpu)
AC_SUBST(host_os)
252
AC_SUBST(host_kernel)
bean's avatar
bean committed
253

254
255
256
AC_SUBST(target_cpu)
AC_SUBST(platform)

257
# Define default variables
258
259
260
261
262
263
264
265
266
267
268
269

have_with_bootdir=n
AC_ARG_WITH([bootdir],
            AS_HELP_STRING([--with-bootdir=DIR],
                           [set the name of /boot directory [[guessed]]]),
			   [have_with_bootdir=y],
			   [have_with_bootdir=n])
if test x$have_with_bootdir = xy; then
   bootdirname="$with_bootdir"
else
   case "$host_os" in
     netbsd* | openbsd*)
270
        # Because /boot is used for the boot block in NetBSD and OpenBSD,
271
272
273
274
275
           bootdirname=''      ;;
     *)    bootdirname='boot'  ;;
   esac
fi

276
AC_SUBST(bootdirname)
277
278
AC_DEFINE_UNQUOTED(GRUB_BOOT_DIR_NAME, "$bootdirname",
    [Default boot directory name]")
279

280
281
282
283
284
285
AC_ARG_WITH([grubdir],
            AS_HELP_STRING([--with-grubdir=DIR],
                           [set the name of grub directory [[guessed]]]),
			   [grubdirname="$with_grubdir"],
			   [grubdirname="$PACKAGE"])

286
AC_SUBST(grubdirname)
287
288
AC_DEFINE_UNQUOTED(GRUB_DIR_NAME, "$grubdirname",
    [Default grub directory name])
289

290
291
292
#
# Checks for build programs.
#
293

294
295
296
297
298
299
300
# Although cmp is listed in the GNU Coding Standards as a command which
# can used directly, OpenBSD lacks cmp in the default installation.
AC_CHECK_PROGS([CMP], [cmp])
if test "x$CMP" = x; then
  AC_MSG_ERROR([cmp is not found])
fi

301
AC_CHECK_PROGS([YACC], [bison])
302
if test "x$YACC" = x; then
303
  AC_MSG_ERROR([bison is not found])
304
305
fi

306
AC_PROG_RANLIB
307
308
AC_PROG_INSTALL
AC_PROG_AWK
309
AC_PROG_LEX
310
AC_PROG_YACC
311
AC_PROG_MAKE_SET
312
AC_PROG_MKDIR_P
Colin Watson's avatar
Colin Watson committed
313
AC_PROG_LN_S
314

315
if test "x$LEX" = "x:"; then
316
317
  AC_MSG_ERROR([flex is not found])
else
318
  version=`$LEX --version | $AWK '{ split($2,x,"."); print x[[1]]*10000+x[[2]]*100+x[[3]]; }'`
BVK Chaitanya's avatar
BVK Chaitanya committed
319
  if test -n "$version" -a "$version" -ge 20535; then
320
321
322
323
324
325
    :
  else
    AC_MSG_ERROR([flex is too old. GRUB requires 2.5.35 or above])
  fi
fi

326
# These are not a "must".
327
AC_PATH_PROGS(MAKEINFO, makeinfo true)
okuji's avatar
okuji committed
328

329
330
331
332
333
#
# Checks for host programs.
#

AC_PROG_CC
334
gl_EARLY
335
AC_PROG_CXX
336
337
338
AM_PROG_CC_C_O
AM_PROG_AS

okuji's avatar
okuji committed
339
340
341
# Must be GCC.
test "x$GCC" = xyes || AC_MSG_ERROR([GCC is required])

342
343
AC_CHECK_PROG(HAVE_CXX, $CXX, yes, no)

344
AC_GNU_SOURCE
345
AM_GNU_GETTEXT([external])
346
347
348
AC_SYS_LARGEFILE

# Identify characteristics of the host architecture.
349
350
unset ac_cv_c_bigendian

351
352
353
354
355
if test x"$target_cpu-$platform" = xsparc64-emu ; then
  CFLAGS="$CFLAGS -m64"
  HOST_CFLAGS="$HOST_CFLAGS -m64"
fi

356
357
358
CPPFLAGS="$CPPFLAGS -D_FILE_OFFSET_BITS=64"
HOST_CPPFLAGS="$HOST_CPPFLAGS -D_FILE_OFFSET_BITS=64"

359
360
361
362
AC_C_BIGENDIAN
AC_CHECK_SIZEOF(void *)
AC_CHECK_SIZEOF(long)

363
364
365
366
367
368
369
370
case "$host_os" in
  cygwin | windows* | mingw32*)
     HOST_CPPFLAGS="$HOST_CPPFLAGS -DUNICODE=1 -D_WIN32_WINNT=0x0500"
     CPPFLAGS="$CPPFLAGS -DUNICODE=1 -D_WIN32_WINNT=0x0500"
     AC_CHECK_SIZEOF(TCHAR,,[#include <windows.h>])
   ;;
esac

371
372
373
374
375
376
377
378
case "$host_os" in
  cygwin | windows* | mingw32* | aros*)
     ;;
  *)
     AC_CHECK_SIZEOF(off_t)
     test x"$ac_cv_sizeof_off_t" = x8 || AC_MSG_ERROR([Large file support is required]);;
esac

379
380
381
382
if test x$USE_NLS = xno; then
  HOST_CFLAGS="$HOST_CFLAGS -fno-builtin-gettext"
fi

383
384
385
386
387
388
if test "x$cross_compiling" = xyes; then
  AC_MSG_WARN([cannot generate manual pages while cross compiling])
else
  AC_PATH_PROG(HELP2MAN, help2man)
fi

389
# Check for functions and headers.
390
AC_CHECK_FUNCS(posix_memalign memalign getextmntent)
391
AC_CHECK_HEADERS(sys/param.h sys/mount.h sys/mnttab.h sys/mkdev.h limits.h)
392

393
394
395
396
397
398
399
400
AC_CHECK_MEMBERS([struct statfs.f_fstypename],,,[$ac_includes_default
#include <sys/param.h>
#include <sys/mount.h>])

AC_CHECK_MEMBERS([struct statfs.f_mntfromname],,,[$ac_includes_default
#include <sys/param.h>
#include <sys/mount.h>])

401
402
403
404
405
406
407
408
409
410
411
412
413
414
# For opendisk() and getrawpartition() on NetBSD.
# Used in util/deviceiter.c and in util/hostdisk.c.
AC_CHECK_HEADER([util.h], [
  AC_CHECK_LIB([util], [opendisk], [
    LIBUTIL="-lutil"
    AC_DEFINE(HAVE_OPENDISK, 1, [Define if opendisk() in -lutil can be used])
  ])
  AC_CHECK_LIB([util], [getrawpartition], [
    LIBUTIL="-lutil"
    AC_DEFINE(HAVE_GETRAWPARTITION, 1, [Define if getrawpartition() in -lutil can be used])
  ])
])
AC_SUBST([LIBUTIL])

415
AC_CACHE_CHECK([whether -Wtrampolines work], [grub_cv_host_cc_wtrampolines], [
416
  SAVED_CFLAGS="$CFLAGS"
417
  CFLAGS="$HOST_CFLAGS -Wtrampolines -Werror"
418
419
  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <stdarg.h>
int va_arg_func (int fixed, va_list args);]], [[]])],
420
421
      [grub_cv_host_cc_wtrampolines=yes],
      [grub_cv_host_cc_wtrampolines=no])
422
423
424
  CFLAGS="$SAVED_CFLAGS"
])

425
426
if test x"$grub_host_cv_cc_wtrampolines" = xyes ; then
  HOST_CFLAGS="$HOST_CFLAGS -Wtrampolines"
427
428
fi

429
430
431
432
#
# Check for host and build compilers.
#
HOST_CC=$CC
433
434
AC_CHECK_PROGS(BUILD_CC, [gcc egcs cc])
test -z "$BUILD_CC" && AC_MSG_ERROR([none of gcc, egcs and cc is found. set BUILD_CC manually.])
435
BUILD_CPP="$BUILD_CC -E"
436

437
438
439
440
case "$build_os" in
  haiku*)				BUILD_LIBM= ;;
  *) 					BUILD_LIBM=-lm ;;
esac
441
442
443
444
445
446
447
448
449
450

dnl FIXME proper test seems to require too deep dive into Autoconf internals.
dnl For now just list known platforms that we support.

case "$build_os" in
  cygwin*|mingw32*|mingw64*)		BUILD_EXEEXT=.exe ;;
  *)					BUILD_EXEEXT= ;;
esac
AC_SUBST(BUILD_EXEEXT)

451
452
453
# For gnulib.
gl_INIT

454
WARN_FLAGS="-Wall -W -Wshadow -Wpointer-arith -Wundef -Wchar-subscripts -Wcomment -Wdeprecated-declarations -Wdisabled-optimization -Wdiv-by-zero -Wfloat-equal -Wformat-extra-args -Wformat-security -Wformat-y2k -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wmain -Wmissing-braces -Wmissing-format-attribute -Wmultichar -Wparentheses -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wswitch -Wtrigraphs -Wunknown-pragmas -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value  -Wunused-variable -Wwrite-strings -Wnested-externs -Wstrict-prototypes"
455
EXTRA_WARN_FLAGS="-Wextra -Wattributes -Wendif-labels -Winit-self -Wint-to-pointer-cast -Winvalid-pch -Wmissing-field-initializers -Wnonnull -Woverflow -Wvla -Wpointer-to-int-cast -Wstrict-aliasing -Wvariadic-macros -Wvolatile-register-var -Wpointer-sign -Wmissing-include-dirs -Wmissing-prototypes -Wmissing-declarations -Wformat=2"
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473

HOST_CFLAGS="$HOST_CFLAGS $WARN_FLAGS -Wcast-align"

AC_CACHE_CHECK([which extra warnings work], [grub_cv_cc_w_extra_flags], [
  SAVED_CFLAGS="$CFLAGS"
  grub_cv_cc_w_extra_flags=
  for x in $EXTRA_WARN_FLAGS; do
      CFLAGS="$HOST_CFLAGS $x -Werror"
      AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], [[]])], [flag=1], [flag=0])
      if test x$flag = x1 ; then
         grub_cv_cc_w_extra_flags="$grub_cv_cc_w_extra_flags $x"
      fi
  done
  CFLAGS="$SAVED_CFLAGS"
])

HOST_CFLAGS="$HOST_CFLAGS $grub_cv_cc_w_extra_flags"

474
475
476
477
#
# Check for target programs.
#

478
479
# Find tools for the target.
if test "x$target_alias" != x && test "x$host_alias" != "x$target_alias"; then
480
481
482
483
484
  tmp_ac_tool_prefix="$ac_tool_prefix"
  ac_tool_prefix=$target_alias-

  AC_CHECK_TOOLS(TARGET_CC, [gcc egcs cc],
                 [AC_MSG_ERROR([none of gcc, egcs and cc is found. set TARGET_CC manually.])])
485
486
487
  AC_CHECK_TOOL(TARGET_OBJCOPY, objcopy)
  AC_CHECK_TOOL(TARGET_STRIP, strip)
  AC_CHECK_TOOL(TARGET_NM, nm)
488
  AC_CHECK_TOOL(TARGET_RANLIB, ranlib)
489
490
491
492
493
494

  ac_tool_prefix="$tmp_ac_tool_prefix"
else
  if test "x$TARGET_CC" = x; then
    TARGET_CC=$CC
  fi
495
496
497
  AC_CHECK_TOOL(TARGET_OBJCOPY, objcopy)
  AC_CHECK_TOOL(TARGET_STRIP, strip)
  AC_CHECK_TOOL(TARGET_NM, nm)
498
  AC_CHECK_TOOL(TARGET_RANLIB, ranlib)
499
fi
500

501
502
AC_SUBST(HOST_CC)
AC_SUBST(BUILD_CC)
503
504
AC_SUBST(BUILD_CFLAGS)
AC_SUBST(BUILD_CPPFLAGS)
505
AC_SUBST(BUILD_LDFLAGS)
506
AC_SUBST(TARGET_CC)
507
AC_SUBST(TARGET_NM)
508
AC_SUBST(TARGET_RANLIB)
509
510
AC_SUBST(TARGET_STRIP)
AC_SUBST(TARGET_OBJCOPY)
511
512
513
514
515
516

# Test the C compiler for the target environment.
tmp_CC="$CC"
tmp_CFLAGS="$CFLAGS"
tmp_LDFLAGS="$LDFLAGS"
tmp_CPPFLAGS="$CPPFLAGS"
517
tmp_LIBS="$LIBS"
518
519
520
521
CC="$TARGET_CC"
CFLAGS="$TARGET_CFLAGS"
CPPFLAGS="$TARGET_CPPFLAGS"
LDFLAGS="$TARGET_LDFLAGS"
522
LIBS=""
523

524
# debug flags.
525
TARGET_CFLAGS="$TARGET_CFLAGS $WARN_FLAGS -g -Wredundant-decls -Wmissing-prototypes -Wmissing-declarations"
526
TARGET_CCASFLAGS="$TARGET_CCASFLAGS -g"
okuji's avatar
okuji committed
527

528
529
530
531
if test "x$target_cpu" != xi386 && test "x$target_cpu" != xx86_64; then
TARGET_CFLAGS="$TARGET_CFLAGS -Wcast-align"
fi

532
533
TARGET_CC_VERSION="$(LC_ALL=C $TARGET_CC --version | head -n1)"

534
535
536
537
538
539
AC_CACHE_CHECK([which extra warnings work], [grub_cv_target_cc_w_extra_flags], [
  LDFLAGS="$TARGET_LDFLAGS -nostdlib -static"

  grub_cv_target_cc_w_extra_flags=
  for x in $EXTRA_WARN_FLAGS; do
      CFLAGS="$TARGET_CFLAGS $x -Werror"
540
541
542
543
544
545
      AC_LINK_IFELSE([AC_LANG_PROGRAM([[
asm (".globl start; start:");
void __main (void);
void __main (void) {}
int main (void);
]], [[]])], [flag=1], [flag=0])
546
547
548
549
550
551
552
553
      if test x$flag = x1 ; then
         grub_cv_target_cc_w_extra_flags="$grub_cv_target_cc_w_extra_flags $x"
      fi
  done
])

TARGET_CFLAGS="$TARGET_CFLAGS $grub_cv_target_cc_w_extra_flags"

554
AC_CACHE_CHECK([if compiling with clang], [grub_cv_cc_target_clang],
555
556
557
[
CFLAGS="$TARGET_CFLAGS"
AC_COMPILE_IFELSE(
558
559
560
561
562
563
564
[AC_LANG_PROGRAM([], [[
#ifdef __clang__
#error "is clang"
#endif
]])],
[grub_cv_cc_target_clang=no], [grub_cv_cc_target_clang=yes])])

565
if test x$target_cpu = xpowerpc -o x$target_cpu = xmips; then
566
567
  AC_CACHE_CHECK([for options to get big-endian compilation], grub_cv_target_cc_big_endian, [
    grub_cv_target_cc_big_endian=no
568
569
570
    for cand in "-target $target_cpu -Wl,-EB" "-target $target_cpu" \
		"-target $target_cpu-linux-gnu -Wl,-EB" "-target $target_cpu-linux-gnu" \
		"-EB" "-mbig-endian"; do
571
572
573
574
575
576
577
578
579
      if test x"$grub_cv_target_cc_big_endian" != xno ; then
        break
      fi
      CFLAGS="$TARGET_CFLAGS $cand -Werror"
      AC_LINK_IFELSE([AC_LANG_PROGRAM([[
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__ORDER_BIG_ENDIAN__ != __BYTE_ORDER__)
#error still little endian
#endif
asm (".globl start; start:");
580
581
asm (".globl _start; _start:");
asm (".globl __start; __start:");
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
void __main (void);
void __main (void) {}
int main (void);
]], [[]])],
		        [grub_cv_target_cc_big_endian="$cand"], [])
    done
  ])

  if test x"$grub_cv_target_cc_big_endian" = xno ; then
    AC_MSG_ERROR([could not force big-endian])
  fi

  skip_linkflags="$(echo "$grub_cv_target_cc_big_endian"|sed 's@-Wl,-EB@@')"

  TARGET_CFLAGS="$TARGET_CFLAGS $skip_linkflags"
  TARGET_CPPFLAGS="$TARGET_CPPFLAGS $skip_linkflags"
  TARGET_CCASFLAGS="$TARGET_CCASFLAGS $skip_linkflags"
  TARGET_LDFLAGS="$TARGET_LDFLAGS $grub_cv_target_cc_big_endian"
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
elif test x$target_cpu = xmipsel; then
  AC_CACHE_CHECK([for options to get little-endian compilation], grub_cv_target_cc_little_endian, [
    grub_cv_target_cc_little_endian=no
    for cand in "-target $target_cpu -Wl,-EL" "-target $target_cpu" \
		"-target $target_cpu-linux-gnu -Wl,-EL" "-target $target_cpu-linux-gnu" \
		"-EL"; do
      if test x"$grub_cv_target_cc_little_endian" != xno ; then
        break
      fi
      CFLAGS="$TARGET_CFLAGS $cand -Werror"
      AC_LINK_IFELSE([AC_LANG_PROGRAM([[
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__ORDER_BIG_ENDIAN__ == __BYTE_ORDER__)
#error still big endian
#endif
asm (".globl start; start:");
asm (".globl _start; _start:");
asm (".globl __start; __start:");
void __main (void);
void __main (void) {}
int main (void);
]], [[]])],
		        [grub_cv_target_cc_little_endian="$cand"], [])
    done
  ])

  if test x"$grub_cv_target_cc_little_endian" = xno ; then
    AC_MSG_ERROR([could not force little-endian])
  fi

  skip_linkflags="$(echo "$grub_cv_target_cc_little_endian"|sed 's@-Wl,-EL@@')"

  TARGET_CFLAGS="$TARGET_CFLAGS $skip_linkflags"
  TARGET_CPPFLAGS="$TARGET_CPPFLAGS $skip_linkflags"
  TARGET_CCASFLAGS="$TARGET_CCASFLAGS $skip_linkflags"
  TARGET_LDFLAGS="$TARGET_LDFLAGS $grub_cv_target_cc_little_endian"
635
636
fi

637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# GRUB code is N32-compliant but it's experimental and we would prefer to
# avoid having too much variety when it doesn't result in any real improvement.
# Moreover N64 isn't supported.
if test "x$target_cpu" = xmips || test "x$target_cpu" = xmipsel ; then
  AC_CACHE_CHECK([for options to force MIPS o32 ABI], grub_cv_target_cc_mips_o32_abi, [
    grub_cv_target_cc_mips_o32_abi=no
    for arg in "" "-mabi=32" "-target $target_cpu -mabi=32" ; do
      if test x"$grub_cv_target_cc_mips_o32_abi" != xno ; then
        break
      fi
      CFLAGS="$TARGET_CFLAGS $arg -Werror"
      AC_LINK_IFELSE([AC_LANG_PROGRAM([[
#if !defined(_ABIO32) || !defined(_MIPS_SIM) || (_MIPS_SIM != _ABIO32)
#error not o32 ABI
#endif
asm (".globl start; start:");
asm (".globl _start; _start:");
asm (".globl __start; __start:");
void __main (void);
void __main (void) {}
int main (void);
]], [[]])],
		        [grub_cv_target_cc_mips_o32_abi="$arg"], [])
    done
  ])

  if test x"$grub_cv_target_cc_mips_o32_abi" = xno ; then
    AC_MSG_ERROR([could not force MIPS o32 ABI])
  fi

  TARGET_CFLAGS="$TARGET_CFLAGS $grub_cv_target_cc_mips_o32_abi"
  TARGET_CCASFLAGS="$TARGET_CCASFLAGS $grub_cv_target_cc_mips_o32_abi"
fi

671
672
AC_CACHE_CHECK([for options to compile assembly], [grub_cv_cc_target_asm_compile], [
test_program=
673
674
case "x$target_cpu-$platform" in
     xmips-* | xmipsel-*)
675
676
        test_program=mips
	;;
677
678
679
680
     xi386-pc)
       test_program=i386-pc
	;;
     xi386-* | xx86_64-*)
681
682
       test_program=i386
	;;
683
     xpowerpc-* | xsparc64-* | xarm-*)
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
        test_program=$target_cpu
	;;
esac
if test x"$test_program" = x ; then
  grub_cv_cc_target_asm_compile=
else
  found=no
  for arg in "" "-no-integrated-as"; do
    cmdline="$TARGET_CC -c -o /dev/null $TARGET_CCASFLAGS $arg $TARGET_CPPFLAGS $srcdir/asm-tests/$test_program.S"
    echo "Running $cmdline" >&AS_MESSAGE_LOG_FD
    if $cmdline >&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD; then
      grub_cv_cc_target_asm_compile="$arg"
      found=yes
      break
    fi
  done
  if test x"$found" = xno ; then
    AC_MSG_ERROR([could not compile assembly])
  fi
703
fi
704
])
705

706
TARGET_CCASFLAGS="$TARGET_CCASFLAGS $grub_cv_cc_target_asm_compile"
707

708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
if test "x$target_cpu" = xi386 && test "x$platform" != xemu; then
  TARGET_CFLAGS="$TARGET_CFLAGS -march=i386"
fi

if test "x$target_m32" = x1; then
  # Force 32-bit mode.
  TARGET_CFLAGS="$TARGET_CFLAGS -m32"
  TARGET_CCASFLAGS="$TARGET_CCASFLAGS -m32"
  TARGET_CPPFLAGS="$TARGET_CPPFLAGS -m32"
  TARGET_LDFLAGS="$TARGET_LDFLAGS -m32"
  TARGET_MODULE_FORMAT="elf32"
fi

if test "x$target_m64" = x1; then
  # Force 64-bit mode.
  TARGET_CFLAGS="$TARGET_CFLAGS -m64"
  TARGET_CCASFLAGS="$TARGET_CCASFLAGS -m64"
  TARGET_CPPFLAGS="$TARGET_CPPFLAGS -m64"
  TARGET_LDFLAGS="$TARGET_LDFLAGS -m64"
  TARGET_MODULE_FORMAT="elf64"
fi

if test "x$grub_cv_cc_target_clang" = xno && test "x$target_cpu" = xi386 && test "x$platform" != xemu && test "x$platform" != xefi; then
   TARGET_CFLAGS="$TARGET_CFLAGS -mrtd -mregparm=3"
fi

734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
# on mips redirect cache flushing function to non-existant one.
if test "x$target_cpu" = xmips || test "x$target_cpu" = xmipsel ; then
  AC_CACHE_CHECK([whether -mflush-func=grub_red_herring works], [grub_cv_cc_mflush_func], [
    CFLAGS="$TARGET_CFLAGS -mflush-func=grub_red_herring -Werror"
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
        [grub_cv_cc_mflush_func=yes],
	[grub_cv_cc_mflush_func=no])
  ])

  if test "x$grub_cv_cc_mflush_func" = xyes; then
    TARGET_CFLAGS="$TARGET_CFLAGS -mflush-func=grub_red_herring"
  fi
fi


749
750
751
# Force no alignment to save space on i386.
if test "x$target_cpu" = xi386; then
  AC_CACHE_CHECK([whether -falign-loops works], [grub_cv_cc_falign_loop], [
752
    CFLAGS="$TARGET_CFLAGS -falign-loops=1 -Werror"
753
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
754
755
        [grub_cv_cc_falign_loop=yes],
	[grub_cv_cc_falign_loop=no])
okuji's avatar
okuji committed
756
757
  ])

758
  AC_CACHE_CHECK([whether -malign-loops works], [grub_cv_cc_malign_loop], [
759
    CFLAGS="$TARGET_CFLAGS -malign-loops=1 -Werror"
760
761
762
763
764
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
        [grub_cv_cc_malign_loop=yes],
	[grub_cv_cc_malign_loop=no])
  ])

765
766
  if test "x$grub_cv_cc_falign_loop" = xyes; then
    TARGET_CFLAGS="$TARGET_CFLAGS -falign-jumps=1 -falign-loops=1 -falign-functions=1"
767
  elif test "x$grub_cv_cc_malign_loop" = xyes; then
768
    TARGET_CFLAGS="$TARGET_CFLAGS -malign-jumps=1 -malign-loops=1 -malign-functions=1"
okuji's avatar
okuji committed
769
  fi
770
fi
okuji's avatar
okuji committed
771

772
773
774
775
776
777
778
779
780
781
782
AC_CACHE_CHECK([whether -freg-struct-return works], [grub_cv_cc_freg_struct_return], [
    CFLAGS="$TARGET_CFLAGS -freg-struct-return -Werror"
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
        [grub_cv_cc_freg_struct_return=yes],
	[grub_cv_cc_freg_struct_return=no])
])

if test "x$grub_cv_cc_freg_struct_return" = xyes; then
    TARGET_CFLAGS="$TARGET_CFLAGS -freg-struct-return"
fi

783
if ( test "x$target_cpu" = xi386 || test "x$target_cpu" = xx86_64 ) && test "x$platform" != xemu; then
784
785
  # Some toolchains enable these features by default, but they need
  # registers that aren't set up properly in GRUB.
786
  TARGET_CFLAGS="$TARGET_CFLAGS -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow"
787
fi
788

789
790
791
792
# GRUB doesn't use float or doubles at all. Yet some toolchains may decide
# that floats are a good fit to run instead of what's written in the code.
# Given that floating point unit is disabled (if present to begin with)
# when GRUB is running which may result in various hard crashes.
793
794
795
796
if test x"$platform" != xemu ; then
  AC_CACHE_CHECK([for options to get soft-float], grub_cv_target_cc_soft_float, [
    grub_cv_target_cc_soft_float=no
    if test "x$target_cpu" = xarm64; then
797
       CFLAGS="$TARGET_CFLAGS -mgeneral-regs-only -Werror"
798
       AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
799
		         [grub_cv_target_cc_soft_float="-mgeneral-regs-only"], [])
800
    fi
801
802
803
804
805
    if test "x$target_cpu" = xia64; then
       CFLAGS="$TARGET_CFLAGS -mno-inline-float-divide -mno-inline-sqrt -Werror"
       AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
		         [grub_cv_target_cc_soft_float="-mno-inline-float-divide -mno-inline-sqrt"], [])
    fi
806
807
    for cand in "-msoft-float -Xclang -msoft-float -Xclang -no-implicit-float" \
		"-Xclang -msoft-float -Xclang -no-implicit-float" \
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
		"-Xclang -msoft-float" "-msoft-float"; do
      if test x"$grub_cv_target_cc_soft_float" != xno ; then
        break
      fi
      CFLAGS="$TARGET_CFLAGS $cand -Werror"
      AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
		        [grub_cv_target_cc_soft_float="$cand"], [])
    done
  ])

  if test x"$grub_cv_target_cc_soft_float" = xno ; then
    AC_MSG_ERROR([could not force soft-float])
  fi

  case x"$grub_cv_target_cc_soft_float" in
823
    x*"-Xclang"*)
824
825
826
827
828
829
830
      # A trick so that clang doesn't see it on link stаge
      TARGET_CPPFLAGS="$TARGET_CPPFLAGS $grub_cv_target_cc_soft_float"
      ;;
    *)
      TARGET_CFLAGS="$TARGET_CFLAGS $grub_cv_target_cc_soft_float"
      ;;
  esac
831
  TARGET_CCASFLAGS="$TARGET_CCASFLAGS $grub_cv_target_cc_soft_float"
832

833
fi
834

835
836
837
838
839
840
841
842
843
if test x"$target_cpu" = xsparc64 ; then
  AC_CACHE_CHECK([for options to reserve application registers], grub_cv_target_cc_mno_app_regs, [
    grub_cv_target_cc_mno_app_regs=no
    for cand in "-mllvm -sparc-reserve-app-registers" \
		"-mno-app-regs"; do
      if test x"$grub_cv_target_cc_mno_app_regs" != xno ; then
        break
      fi
      CFLAGS="$TARGET_CFLAGS $cand -Werror"
844
      CPPFLAGS="$TARGET_CPPFLAGS"
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
      AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
		        [grub_cv_target_cc_mno_app_regs="$cand"], [])
    done
  ])

  if test x"$grub_cv_target_cc_mno_app_regs" = xno ; then
    AC_MSG_ERROR([could not reserve application registers])
  fi
  if test x"$grub_cv_target_cc_mno_app_regs" = x"-mllvm -sparc-reserve-app-registers" ; then
    # A trick so that clang doesn't see it on link stаge
    TARGET_CPPFLAGS="$TARGET_CPPFLAGS $grub_cv_target_cc_mno_app_regs"
  else
    TARGET_CFLAGS="$TARGET_CFLAGS $grub_cv_target_cc_mno_app_regs"
  fi

  AC_CACHE_CHECK([for no-relax options], grub_cv_target_cc_mno_relax, [
    grub_cv_target_cc_mno_relax=no
    for cand in "-mno-relax" "-Wl,--no-relax"; do
      if test x"$grub_cv_target_cc_mno_relax" != xno ; then
        break
      fi
      LDFLAGS="$TARGET_LDFLAGS $cand -nostdlib -static"
      CFLAGS="$TARGET_CFLAGS -Werror"
      AC_LINK_IFELSE([AC_LANG_PROGRAM([[
	    asm (".globl start; start:");
	    void __main (void);
	    void __main (void) {}
	    int main (void);
	    ]], [[]])], [grub_cv_target_cc_mno_relax="$cand"], [])
    done
  ])
  LDFLAGS="$TARGET_LDFLAGS"
  CFLAGS="$TARGET_CFLAGS"

  if test x"$grub_cv_target_cc_mno_relax" = xno ; then
    AC_MSG_ERROR([could not find no-relax options])
  fi
  TARGET_LDFLAGS="$TARGET_LDFLAGS $grub_cv_target_cc_mno_relax"
fi

885
886
887
888
889
# By default, GCC 4.4 generates .eh_frame sections containing unwind
# information in some cases where it previously did not. GRUB doesn't need
# these and they just use up vital space. Restore the old compiler
# behaviour.
AC_CACHE_CHECK([whether -fno-dwarf2-cfi-asm works], [grub_cv_cc_fno_dwarf2_cfi_asm], [
890
  CFLAGS="$TARGET_CFLAGS -fno-dwarf2-cfi-asm"
891
892
893
894
  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
      [grub_cv_cc_fno_dwarf2_cfi_asm=yes],
      [grub_cv_cc_fno_dwarf2_cfi_asm=no])
])
895

896
897
if test "x$grub_cv_cc_fno_dwarf2_cfi_asm" = xyes; then
  TARGET_CFLAGS="$TARGET_CFLAGS -fno-dwarf2-cfi-asm"
898
fi
okuji's avatar
okuji committed
899

900
901
if test x"$target_os" = xcygwin; then
  AC_CACHE_CHECK([whether option -fno-reorder-functions works], grub_cv_cc_no_reorder_functions, [
902
    CFLAGS="$TARGET_CFLAGS -fno-reorder-functions"
903
904
905
906
907
908
909
910
911
912
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
		      [grub_cv_cc_no_reorder_functions=yes],
		      [grub_cv_cc_no_reorder_functions=no])
  ])
fi

if test x"$target_os" = xcygwin && test "x$grub_cv_cc_no_reorder_functions" = xyes; then
  TARGET_CFLAGS="$TARGET_CFLAGS -fno-reorder-functions"
fi

913
914
915
916
917
918
919
920
921
922
923
924
AC_CACHE_CHECK([whether -mno-stack-arg-probe works], [grub_cv_cc_mno_stack_arg_probe], [
  CFLAGS="$TARGET_CFLAGS -mno-stack-arg-probe"
  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
      [grub_cv_cc_mno_stack_arg_probe=yes],
      [grub_cv_cc_mno_stack_arg_probe=no])
])

if test "x$grub_cv_cc_mno_stack_arg_probe" = xyes; then
  TARGET_CFLAGS="$TARGET_CFLAGS -mno-stack-arg-probe"
fi


925
926
927
928
929
# By default, GCC 4.6 generates .eh_frame sections containing unwind
# information in some cases where it previously did not. GRUB doesn't need
# these and they just use up vital space. Restore the old compiler
# behaviour.
AC_CACHE_CHECK([whether -fno-asynchronous-unwind-tables works], [grub_cv_cc_fno_asynchronous_unwind_tables], [
930
  CFLAGS="$TARGET_CFLAGS -fno-asynchronous-unwind-tables"
931
932
933
934
935
936
937
938
939
  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
      [grub_cv_cc_fno_asynchronous_unwind_tables=yes],
      [grub_cv_cc_fno_asynchronous_unwind_tables=no])
])

if test "x$grub_cv_cc_fno_asynchronous_unwind_tables" = xyes; then
  TARGET_CFLAGS="$TARGET_CFLAGS -fno-asynchronous-unwind-tables"
fi

940
941
942
943
944
945
946
947
948
949
950
AC_CACHE_CHECK([whether -fno-unwind-tables works], [grub_cv_cc_fno_unwind_tables], [
  CFLAGS="$TARGET_CFLAGS -fno-unwind-tables"
  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
      [grub_cv_cc_fno_unwind_tables=yes],
      [grub_cv_cc_fno_unwind_tables=no])
])

if test "x$grub_cv_cc_fno_unwind_tables" = xyes; then
  TARGET_CFLAGS="$TARGET_CFLAGS -fno-unwind-tables"
fi

Vladimir 'phcoder' Serbinenko's avatar
Vladimir 'phcoder' Serbinenko committed
951

952
953
CFLAGS="$TARGET_CFLAGS"

954
955
956
957
958
959
960
961
962
963
964

if test x"$platform" = xemu ; then
  TARGET_OBJ2ELF=
  grub_cv_target_cc_link_format=
  case "$host_os" in
    *darwin* | *mac*)
       grub_cv_target_cc_link_format="-arch,${target_cpu}"
       TARGET_LDFLAGS="$TARGET_LDFLAGS -Wl,$grub_cv_target_cc_link_format"
        ;;
    *windows* | *cygwin* | *mingw*)
      if test x${target_cpu} = xi386 ; then
965
966
        grub_cv_target_cc_link_format=-mi386pe
	TARGET_OBJ2ELF='./build-grub-pe2elf$(BUILD_EXEEXT)'
967
968
      fi
      if test x${target_cpu} = xx86_64 ; then
969
970
        grub_cv_target_cc_link_format=-mi386pep
	TARGET_OBJ2ELF='./build-grub-pep2elf$(BUILD_EXEEXT)'
971
972
973
974
975
      fi
      TARGET_LDFLAGS="$TARGET_LDFLAGS -Wl,$grub_cv_target_cc_link_format"
      ;;
  esac
elif test x"$target_cpu" = xi386 || test x"$target_cpu" = xx86_64; then
976
  AC_CACHE_CHECK([for target linking format], [grub_cv_target_cc_link_format], [
977
    grub_cv_target_cc_link_format=unknown
978
    for format in -melf_${target_cpu} -melf_${target_cpu}_fbsd -melf_${target_cpu}_obsd -melf_${target_cpu}_haiku -mi386pe -mi386pep -arch,${target_cpu}; do
979
      if test x${target_cpu} != xi386 && test x$format = x-mi386pe; then
980
981
        continue
      fi
982
      if test x${target_cpu} != xx86_64 && test x$format = x-mi386pep; then
983
984
        continue
      fi
985
986
      CFLAGS="$TARGET_CFLAGS"
      LDFLAGS="$TARGET_LDFLAGS -Wl,$format -nostdlib -static"
987
988
989
990
991
992
      AC_LINK_IFELSE([AC_LANG_PROGRAM([[
      asm (".globl start; start:");
      asm (".globl _start; _start:");
      asm (".globl __start; __start:");
      void __main (void);
      void __main (void) {}
993
      ]], [[]])], [flag=1], [flag=0])
994
995
      if test x"$flag" = x1; then
        grub_cv_target_cc_link_format="$format"
996
	break
997
998
999
1000
1001
1002
      fi
    done])
  if test x"$grub_cv_target_cc_link_format" = xunknown; then
    AC_MSG_ERROR([no suitable link format found])
  fi
  TARGET_LDFLAGS="$TARGET_LDFLAGS -Wl,$grub_cv_target_cc_link_format"
1003
  if test x"$grub_cv_target_cc_link_format" = x-mi386pe ; then
1004
    TARGET_OBJ2ELF='./build-grub-pe2elf$(BUILD_EXEEXT)'
1005
  fi
1006
  if test x"$grub_cv_target_cc_link_format" = x-mi386pep ; then
1007
    TARGET_OBJ2ELF='./build-grub-pep2elf$(BUILD_EXEEXT)'
1008
  fi
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
fi

if test x$grub_cv_target_cc_link_format = x-arch,i386 || test x$grub_cv_target_cc_link_format = x-arch,x86_64; then
   TARGET_APPLE_LINKER=1
   AC_CHECK_PROG([TARGET_OBJCONV], [objconv], [objconv], [])
   if test "x$TARGET_OBJCONV" = x ; then
      AC_CHECK_PROG([TARGET_OBJCONV], [objconv], [./objconv], [], [.])
   fi
   if test "x$TARGET_OBJCONV" = x ; then
      AC_MSG_ERROR([objconv not found which is required when building with apple compiler])
   fi
   TARGET_IMG_LDSCRIPT=
   TARGET_IMG_CFLAGS="-static"
   TARGET_IMG_LDFLAGS='-nostdlib -static -Wl,-preload -Wl,-segalign,20'
   TARGET_IMG_LDFLAGS_AC='-nostdlib -static -Wl,-preload -Wl,-segalign,20'
   TARGET_IMG_BASE_LDOPT="-Wl,-image_base"
   TARGET_LDFLAGS_OLDMAGIC=""
1026
elif test x$grub_cv_target_cc_link_format = x-mi386pe || test x$grub_cv_target_cc_link_format = x-mi386pep ; then
1027
1028
  TARGET_APPLE_LINKER=0
  TARGET_LDFLAGS_OLDMAGIC="-Wl,-N"
1029
  TARGET_IMG_LDSCRIPT='$(top_srcdir)'"/${grub_coredir}/conf/i386-cygwin-img-ld.sc"
1030
  TARGET_IMG_LDFLAGS="-Wl,-T${TARGET_IMG_LDSCRIPT}"
1031
  TARGET_IMG_LDFLAGS_AC="-Wl,-T${srcdir}/${grub_coredir}/conf/i386-cygwin-img-ld.sc"
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
  TARGET_IMG_BASE_LDOPT="-Wl,-Ttext"
  TARGET_IMG_CFLAGS=
else
  TARGET_APPLE_LINKER=0
  TARGET_LDFLAGS_OLDMAGIC="-Wl,-N"
  TARGET_IMG_LDSCRIPT=
  TARGET_IMG_LDFLAGS='-Wl,-N'
  TARGET_IMG_LDFLAGS_AC='-Wl,-N'
  TARGET_IMG_BASE_LDOPT="-Wl,-Ttext"
  TARGET_IMG_CFLAGS=
fi

1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
CFLAGS="$TARGET_CFLAGS"

AC_ARG_ENABLE([efiemu],
	      [AS_HELP_STRING([--enable-efiemu],
                             [build and install the efiemu runtimes (default=guessed)])])
if test x"$enable_efiemu" = xno ; then
  efiemu_excuse="explicitly disabled"
fi

if test x"$grub_cv_target_cc_link_format" = x-mi386pe || test x"$grub_cv_target_cc_link_format" = x-mi386pep ; then
  efiemu_excuse="not available on cygwin"
fi
if test x"$target_cpu" != xi386 ; then
  efiemu_excuse="only available on i386"
fi
if test x"$platform" = xefi ; then
  efiemu_excuse="not available on efi"
fi

if test x"$efiemu_excuse" = x ; then
  AC_CACHE_CHECK([whether options required for efiemu work], grub_cv_cc_efiemu, [
    CFLAGS="-m64 -nostdlib -O2 -mcmodel=large -mno-red-zone"
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
		      [grub_cv_cc_efiemu=yes],
		      [grub_cv_cc_efiemu=no])
  ])
  if test x$grub_cv_cc_efiemu = xno; then
     efiemu_excuse="cannot compile with -m64 -mcmodel=large -mno-red-zone -nostdlib"
  fi
fi
if test x"$efiemu_excuse" = x ; then
  AC_CACHE_CHECK([for efiemu64 linking format], [grub_cv_target_cc_efiemu64_link_format], [
    grub_cv_target_cc_efiemu64_link_format=unknown
    for format in -melf_x86_64 -melf_x86_64_fbsd -melf_x86_64_obsd -melf_x86_64_haiku -arch,x86_64; do
      CFLAGS="-m64 -nostdlib -O2 -mcmodel=large -mno-red-zone"
      LDFLAGS="-m64 -Wl,$format -nostdlib -static"
      AC_LINK_IFELSE([AC_LANG_PROGRAM([[
      asm (".globl start; start:");
      asm (".globl _start; _start:");
      asm (".globl __start; __start:");
      void __main (void);
      void __main (void) {}
      ]], [[]])], [flag=1], [flag=0])
      if test x"$flag" = x1; then
        grub_cv_target_cc_efiemu64_link_format="$format"
	break
      fi
    done])
  if test x"$grub_cv_target_cc_efiemu64_link_format" = xunknown; then
    efiemu_excuse="no suitable link format for efiemu64 found"
  else
    EFIEMU64_LINK_FORMAT="-Wl,$grub_cv_target_cc_efiemu64_link_format"
  fi
fi
if test x"$enable_efiemu" = xyes && test x"$efiemu_excuse" != x ; then
  AC_MSG_ERROR([efiemu runtime was explicitly requested but can't be compiled ($efiemu_excuse)])
fi
if test x"$efiemu_excuse" = x ; then
enable_efiemu=yes
else
enable_efiemu=no
fi
AC_SUBST([enable_efiemu])
AC_SUBST([EFIEMU64_LINK_FORMAT])

CFLAGS="$TARGET_CFLAGS"

1111
1112
1113
1114
AC_SUBST(TARGET_LDFLAGS_OLDMAGIC)


LDFLAGS="$TARGET_LDFLAGS"
1115

1116
if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 ; then
bean's avatar
bean committed
1117
  # Use large model to support 4G memory
bean's avatar
bean committed
1118
  AC_CACHE_CHECK([whether option -mcmodel=large works], grub_cv_cc_mcmodel, [
1119
    CFLAGS="$TARGET_CFLAGS -mcmodel=large"
bean's avatar
bean committed
1120
1121
1122
1123
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
		      [grub_cv_cc_mcmodel=yes],
		      [grub_cv_cc_mcmodel=no])
  ])
1124
  if test "x$grub_cv_cc_mcmodel" = xyes; then
1125
    TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=large"
1126
  elif test "$target_cpu" = sparc64; then
1127
    TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
bean's avatar
bean committed
1128
  fi
1129
fi
bean's avatar
bean committed
1130

1131
if test "$target_cpu"-"$platform" = x86_64-efi; then
bean's avatar
bean committed
1132
1133
  # EFI writes to stack below %rsp, we must not use the red zone
  AC_CACHE_CHECK([whether option -mno-red-zone works], grub_cv_cc_no_red_zone, [
1134
    CFLAGS="$TARGET_CFLAGS -mno-red-zone"
bean's avatar
bean committed
1135
1136
1137
1138
1139
1140
1141
1142
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
		      [grub_cv_cc_no_red_zone=yes],
		      [grub_cv_cc_no_red_zone=no])
  ])
  if test "x$grub_cv_cc_no_red_zone" = xno; then
    AC_MSG_ERROR([-mno-red-zone not supported, upgrade your gcc])
  fi

1143
  TARGET_CFLAGS="$TARGET_CFLAGS -mno-red-zone"
bean's avatar
bean committed
1144
1145
fi

1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
if test "x$target_cpu" = xarm; then
  AC_CACHE_CHECK([whether option -mlong-calls works], grub_cv_cc_mlong_calls, [
    CFLAGS="$TARGET_CFLAGS -mlong-calls -Werror"
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
		      [grub_cv_cc_mlong_calls=yes],
		      [grub_cv_cc_mlong_calls=no])
  ])
  if test "x$grub_cv_cc_mlong_calls" = xyes; then
    TARGET_CFLAGS="$TARGET_CFLAGS -mlong-calls"
  fi
1156
1157
1158
1159
1160
1161
1162
1163
  AC_CACHE_CHECK([whether option -mthumb-interwork works], grub_cv_cc_mthumb_interwork, [
    CFLAGS="$TARGET_CFLAGS -mthumb-interwork -Werror"
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
		      [grub_cv_cc_mthumb_interwork=yes],
		      [grub_cv_cc_mthumb_interwork=no])
  ])
  if test "x$grub_cv_cc_mthumb_interwork" = xyes; then
    TARGET_CFLAGS="$TARGET_CFLAGS -mthumb-interwork"
1164
  # Clang defaults to thumb interworking
1165
1166
1167
  elif test "x$grub_cv_cc_target_clang" = xno ; then
    AC_MSG_ERROR([your compiler doesn't support -mthumb-interwork])
  fi
1168
1169
fi

1170
1171
1172
1173
1174
1175
1176
1177
1178
AC_CACHE_CHECK([whether option -Qn works], grub_cv_target_cc_qn, [
  CFLAGS="$TARGET_CFLAGS -Qn -Werror"
  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
		    [grub_cv_target_cc_qn=yes],
		    [grub_cv_target_cc_qn=no])])
if test "x$grub_cv_target_cc_qn" = xyes; then
  TARGET_CFLAGS="$TARGET_CFLAGS -Qn"
fi

1179
1180
1181
1182
#
# Compiler features.
#

1183
1184
CFLAGS="$TARGET_CFLAGS"

1185
1186
1187
# Position independent executable.
grub_CHECK_PIE
[# Need that, because some distributions ship compilers that include
1188
# `-fPIE' or '-fpie' in the default specs.
1189
if [ x"$pie_possible" = xyes ]; then
1190
  TARGET_CFLAGS="$TARGET_CFLAGS -fno-PIE -fno-pie"
1191
1192
fi]

1193
1194
CFLAGS="$TARGET_CFLAGS"

1195
1196
# Position independent executable.
grub_CHECK_PIC
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
[# On most platforms we don't want PIC as it only makes relocations harder
# and code less efficient. On mips we want to have one got table per module
# and reload $gp in every function.
# GCC implements it using symbol __gnu_local_gp in non-PIC as well.
# However with clang we need PIC for this reloading to happen.
# Since default varies across dictributions use either -fPIC or -fno-PIC
# explicitly.
if ( test x$target_cpu = xmips || test x$target_cpu = xmipsel ) && test "x$grub_cv_cc_target_clang" = xyes ; then
   TARGET_CFLAGS="$TARGET_CFLAGS -fPIC"
elif [ x"$pic_possible" = xyes ]; then
   TARGET_CFLAGS="$TARGET_CFLAGS -fno-PIC"
1208
1209
fi]

1210
1211
CFLAGS="$TARGET_CFLAGS"

1212
1213
# Smashing stack protector.
grub_CHECK_STACK_PROTECTOR
1214
# Need that, because some distributions ship compilers that include
1215
# `-fstack-protector' in the default specs.
1216
1217
1218
if test "x$ssp_possible" = xyes; then
  TARGET_CFLAGS="$TARGET_CFLAGS -fno-stack-protector"
fi
1219
1220
1221

CFLAGS="$TARGET_CFLAGS"

bean's avatar
bean committed
1222
1223
1224
1225
1226
1227
grub_CHECK_STACK_ARG_PROBE
# Cygwin's GCC uses alloca() to probe the stackframe on static
# stack allocations above some threshold.
if test x"$sap_possible" = xyes; then
  TARGET_CFLAGS="$TARGET_CFLAGS -mno-stack-arg-probe"
fi
1228

1229
1230
CFLAGS="$TARGET_CFLAGS"

1231
# -mno-unaligned-access -mstrict-align
Leif Lindholm's avatar
Leif Lindholm committed
1232
if test "$target_cpu" = arm; then
1233
  AC_CACHE_CHECK([for compile options to get strict alignment], [grub_cv_target_cc_strict_align], [
1234
    grub_cv_target_cc_strict_align=
1235
1236
1237
1238
1239
1240
    for arg in -mno-unaligned-access "-Xclang -mstrict-align" -mstrict-align; do
      CFLAGS="$TARGET_CFLAGS $arg -Werror"
      LDFLAGS="$TARGET_LDFLAGS"
      AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])], [flag=1], [flag=0])
      if test x"$flag" = x1; then
        grub_cv_target_cc_strict_align="$arg"
1241
	break
1242
1243
1244
1245
1246
1247
      fi
    done])

  TARGET_CFLAGS="$TARGET_CFLAGS $grub_cv_target_cc_strict_align"
  if test x"$grub_cv_target_cc_strict_align" = x"-Xclang -mstrict-align"; then
    TARGET_LDFLAGS="$TARGET_LDFLAGS -Qunused-arguments"
Leif Lindholm's avatar
Leif Lindholm committed
1248
  fi
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
  AC_CACHE_CHECK([if compiler generates unaligned accesses], [grub_cv_cc_target_emits_unaligned],
  [CFLAGS="$TARGET_CFLAGS"
   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [[
#ifdef __ARM_FEATURE_UNALIGNED
#error "unaligned"
#endif
     ]])],
     [grub_cv_cc_target_emits_unaligned=no], [grub_cv_cc_target_emits_unaligned=yes])])
  if test x$grub_cv_cc_target_emits_unaligned = xyes; then
    AC_MSG_ERROR([compiler generates unaligned accesses])
  fi
Leif Lindholm's avatar
Leif Lindholm committed
1260
1261
fi

1262
1263
# Set them to their new values for the tests below.
CC="$TARGET_CC"
1264
1265
1266
if test x"$platform" = xemu ; then
CFLAGS="$TARGET_CFLAGS -Wno-error"
elif test "x$TARGET_APPLE_LINKER" = x1 ; then
1267
CFLAGS="$TARGET_CFLAGS -nostdlib -static -Wno-error"
1268
else
1269
CFLAGS="$TARGET_CFLAGS -nostdlib -Wno-error"
1270
fi
1271
CPPFLAGS="$TARGET_CPPFLAGS"
1272

1273
grub_ASM_USCORE
1274
if test "x$TARGET_APPLE_LINKER" = x0 && test x"$platform" != xemu; then
1275
if test x$grub_cv_asm_uscore = xyes; then
1276
DEFSYM="-Wl,--defsym,_abort=_main -Wl,--defsym,__main=_main"
1277
else
1278
DEFSYM="-Wl,--defsym,abort=main -Wl,--defsym,_main=main -Wl,--defsym,__main=main"
1279
fi
1280
CFLAGS="$TARGET_CFLAGS -nostdlib $DEFSYM"
1281
fi
1282

1283
# Check for libgcc symbols
1284
if test x"$platform" = xemu; then
1285
AC_CHECK_FUNCS(__udivsi3 __umodsi3 __divsi3 __modsi3 __divdi3 __moddi3 __udivdi3 __umoddi3 __ctzdi2 __ctzsi2 __aeabi_uidiv __aeabi_uidivmod __aeabi_idiv __aeabi_idivmod __aeabi_ulcmp __muldi3 __aeabi_lmul __aeabi_memcpy __aeabi_memcpy4 __aeabi_memcpy8 __aeabi_memclr __aeabi_memclr4 __aeabi_memclr8 __aeabi_memset __aeabi_lasr __aeabi_llsl __aeabi_llsr _restgpr_14_x __ucmpdi2 __ashldi3 __ashrdi3 __lshrdi3 __bswapsi2 __bswapdi2 __bzero __register_frame_info __deregister_frame_info ___chkstk_ms __chkstk_ms)
1286
fi
1287

1288
if test "x$TARGET_APPLE_LINKER" = x1 ; then
1289
CFLAGS="$TARGET_CFLAGS -nostdlib -static"
1290
else