]> git.proxmox.com Git - grub2.git/blobdiff - configure.ac
* include/grub/x86_64/types.h: Define sizeof (long) as 4 when compiling
[grub2.git] / configure.ac
index b430135b53352eb6a365a942df347feb80cdabf0..59bd8736bd974f9950b638cd219df62c1b363d08 100644 (file)
@@ -41,9 +41,11 @@ AC_CONFIG_AUX_DIR([build-aux])
 
 # Checks for host and target systems.
 AC_CANONICAL_HOST
+save_program_prefix="${program_prefix}"
 AC_CANONICAL_TARGET
+program_prefix="${save_program_prefix}"
 
-AM_INIT_AUTOMAKE()
+AM_INIT_AUTOMAKE([1.10.1])
 AC_PREREQ(2.60)
 AC_CONFIG_SRCDIR([include/grub/dl.h])
 AC_CONFIG_HEADER([config-util.h])
@@ -73,14 +75,10 @@ if test "x$TARGET_CFLAGS" = x; then
   TARGET_CFLAGS="$TARGET_CFLAGS -Os"
 fi
 
-BUILD_CPPFLAGS="$BUILD_CPPFLAGS -DLOCALEDIR=\\\"\$(localedir)\\\""
-
 # Default HOST_CPPFLAGS
 HOST_CPPFLAGS="$HOST_CPPFLAGS -Wall -W"
 HOST_CPPFLAGS="$HOST_CPPFLAGS -I\$(top_builddir)/include"
 HOST_CPPFLAGS="$HOST_CPPFLAGS -DGRUB_UTIL=1"
-HOST_CPPFLAGS="$HOST_CPPFLAGS -DGRUB_LIBDIR=\\\"\$(pkglibdir)\\\""
-HOST_CPPFLAGS="$HOST_CPPFLAGS -DLOCALEDIR=\\\"\$(localedir)\\\""
 
 TARGET_CPPFLAGS="$TARGET_CPPFLAGS -Wall -W"
 TARGET_CPPFLAGS="$TARGET_CPPFLAGS -I\$(top_srcdir)/include"
@@ -101,6 +99,9 @@ case "$target_cpu" in
   arm*)
                target_cpu=arm;
                ;;
+  aarch64*)
+               target_cpu=arm64;
+               ;;
 esac
 
 # Specify the platform (such as firmware).
@@ -122,6 +123,7 @@ if test "x$with_platform" = x; then
     mips-*) platform=arc ;;
     ia64-*) platform=efi ;;
     arm-*) platform=uboot ;;
+    arm64-*) platform=efi ;;
     *) AC_MSG_ERROR([unsupported CPU: "$target_cpu"]) ;;
   esac
 else
@@ -131,6 +133,7 @@ fi
 case "$target_cpu"-"$platform" in
   x86_64-efi) ;;
   x86_64-emu) ;;
+  x86_64-xen) ;;
   x86_64-*) target_cpu=i386 ;;
   powerpc64-ieee1275) target_cpu=powerpc ;;
 esac
@@ -139,6 +142,8 @@ esac
 case "$target_cpu"-"$platform" in
   i386-efi) ;;
   x86_64-efi) ;;
+  i386-xen) ;;
+  x86_64-xen) ;;
   i386-pc) ;;
   i386-multiboot) ;;
   i386-coreboot) ;;
@@ -159,14 +164,21 @@ case "$target_cpu"-"$platform" in
   mipsel-loongson) ;;
   arm-uboot) ;;
   arm-efi) ;;
+  arm64-efi) ;;
   *-emu) ;;
   *) AC_MSG_ERROR([platform "$platform" is not supported for target CPU "$target_cpu"]) ;;
 esac
 
-case "$target_cpu" in
-  i386 | powerpc) target_m32=1 ;;
-  x86_64 | sparc64) target_m64=1 ;;
-esac
+if test x$platform != xemu ; then
+   case "$target_cpu" in
+       i386 | powerpc) target_m32=1 ;;
+       x86_64 | sparc64) target_m64=1 ;;
+   esac
+fi
+
+if test x"$target_cpu-$platform" = xsparc64-emu ; then
+   target_m64=1 ;
+fi
 
 case "$target_os" in
   windows* | mingw32*) target_os=cygwin ;;
@@ -185,10 +197,17 @@ case "$host_os" in
   cygwin | windows* | mingw32*)        host_kernel=windows ;;
 esac
 
+case "$host_os" in
+  cygwin | windows* | mingw32*)        have_exec=n ;;
+  aros*) have_exec=n ;;
+  *) have_exec=y;;
+esac
+
 case "$platform" in
   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" ;;
+  xen)         machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_XEN=1" ;;
   ieee1275)    machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_IEEE1275=1" ;;
   uboot)       machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_UBOOT=1" ;;
   qemu)                machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_QEMU=1" ;;
@@ -200,6 +219,7 @@ case "$platform" in
 esac
 case "$target_cpu" in
   arm)         machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_ARM=1" ;;
+  arm64)       machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_ARM64=1" ;;
   mips |mipsel)        machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_MIPS=1" ;;
   sparc64)      machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_SPARC64=1" ;;
 esac
@@ -329,6 +349,11 @@ AC_SYS_LARGEFILE
 # Identify characteristics of the host architecture.
 unset ac_cv_c_bigendian
 
+if test x"$target_cpu-$platform" = xsparc64-emu ; then
+  CFLAGS="$CFLAGS -m64"
+  HOST_CFLAGS="$HOST_CFLAGS -m64"
+fi
+
 AC_C_BIGENDIAN
 AC_CHECK_SIZEOF(void *)
 AC_CHECK_SIZEOF(long)
@@ -341,12 +366,6 @@ case "$host_os" in
    ;;
 esac
 
-grub_apple_cc
-if test x$grub_cv_apple_cc = xyes ; then
-  HOST_CPPFLAGS="$HOST_CPPFLAGS -fnested-functions"
-  HOST_LDFLAGS="$HOST_LDFLAGS -Wl,-allow_stack_execute"
-fi
-
 if test x$USE_NLS = xno; then
   HOST_CFLAGS="$HOST_CFLAGS -fno-builtin-gettext"
 fi
@@ -385,7 +404,7 @@ AC_SUBST([LIBUTIL])
 
 AC_CACHE_CHECK([whether -Wtrampolines work], [grub_cv_host_cc_wtrampolines], [
   SAVED_CFLAGS="$CFLAGS"
-  CFLAGS="$HOST_CFLAGS -Wtrampolines"
+  CFLAGS="$HOST_CFLAGS -Wtrampolines -Werror"
   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <stdarg.h>
 int va_arg_func (int fixed, va_list args);]], [[]])],
       [grub_cv_host_cc_wtrampolines=yes],
@@ -412,6 +431,26 @@ esac
 # For gnulib.
 gl_INIT
 
+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 -Wpointer-arith -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wswitch -Wtrigraphs -Wundef -Wunknown-pragmas -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value  -Wunused-variable -Wwrite-strings -Wnested-externs -Wstrict-prototypes"
+EXTRA_WARN_FLAGS="-Wold-style-definition -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"
+
+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"
+
 #
 # Check for target programs.
 #
@@ -461,33 +500,150 @@ CPPFLAGS="$TARGET_CPPFLAGS"
 LDFLAGS="$TARGET_LDFLAGS"
 LIBS=""
 
-grub_PROG_NM_MINUS_P
-grub_PROG_NM_DEFINED_ONLY
-AC_SUBST(TARGET_NMFLAGS_MINUS_P)
-AC_SUBST(TARGET_NMFLAGS_DEFINED_ONLY)
-
 # debug flags.
-WARN_FLAGS="-Wall -W -Wshadow -Wold-style-definition -Wpointer-arith -Wundef -Wextra -Wattributes -Wcast-align -Wchar-subscripts -Wcomment -Wdeprecated-declarations -Wdisabled-optimization -Wdiv-by-zero -Wendif-labels -Wfloat-equal -Wformat-extra-args -Wformat-security -Wformat-y2k -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Winit-self -Wint-to-pointer-cast -Winvalid-pch -Wmain -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute -Wmultichar -Wnonnull -Woverflow -Wparentheses -Wpointer-arith -Wpointer-to-int-cast -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wstrict-aliasing -Wswitch -Wtrigraphs -Wundef -Wunknown-pragmas -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value  -Wunused-variable -Wvariadic-macros -Wvolatile-register-var -Wwrite-strings -Wnested-externs -Wstrict-prototypes -Wpointer-sign"
-HOST_CFLAGS="$HOST_CFLAGS $WARN_FLAGS"
 TARGET_CFLAGS="$TARGET_CFLAGS $WARN_FLAGS -g -Wredundant-decls -Wmissing-prototypes -Wmissing-declarations"
 TARGET_CCASFLAGS="$TARGET_CCASFLAGS -g"
 
+if test "x$target_cpu" != xi386 && test "x$target_cpu" != xx86_64; then
+TARGET_CFLAGS="$TARGET_CFLAGS -Wcast-align"
+fi
+
+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"
+      AC_LINK_IFELSE([AC_LANG_PROGRAM([[
+asm (".globl start; start:");
+void __main (void);
+void __main (void) {}
+int main (void);
+]], [[]])], [flag=1], [flag=0])
+      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"
+
+AC_CACHE_CHECK([if compiling with clang], [grub_cv_cc_target_clang]
+[
+CFLAGS="$TARGET_CFLAGS"
+AC_COMPILE_IFELSE(
+[AC_LANG_PROGRAM([], [[
+#ifdef __clang__
+#error "is clang"
+#endif
+]])],
+[grub_cv_cc_target_clang=no], [grub_cv_cc_target_clang=yes])])
+
+# on x86 clang doesn't support .code16
+# on arm clang doesn't support .arch directive
+# on mips clang doesn't support privilegied instructions, doubleword store/load
+# and crashes with hand-written assembly
+if test "x$grub_cv_cc_target_clang" = xyes && ( test "x$target_cpu" = xi386 \
+   || test "x$target_cpu" = xx86_64 || test "x$target_cpu" = xarm \
+   || test "x$target_cpu" = xmips || test "x$target_cpu" = xmipsel ); then
+   TARGET_CCASFLAGS="$TARGET_CCASFLAGS -no-integrated-as"
+fi
+
+if test "x$grub_cv_cc_target_clang" = xyes && test "x$target_cpu" = xpowerpc; then
+AC_CACHE_CHECK([if clang can handle ame instruction], [grub_cv_cc_target_clang_ame]
+[
+CFLAGS="$TARGET_CFLAGS"
+AC_COMPILE_IFELSE(
+[AC_LANG_PROGRAM([], [[
+   unsigned int a = 0, b = 0;
+   asm volatile ("{ame|addme} %0,%1" : "=r" (a) : "r" (b));
+   if (a)
+     return 1;
+]])],
+[grub_cv_cc_target_clang_ame=yes], [grub_cv_cc_target_clang_ame=no])])
+   # clang <= 3.3 doesn't handle most of ppc assembly, not even inline assembly
+   # used by gcrypt
+   if test x$grub_cv_cc_target_clang_ame = xno ; then
+     TARGET_CCASFLAGS="$TARGET_CCASFLAGS -no-integrated-as"
+     TARGET_CFLAGS="$TARGET_CFLAGS -no-integrated-as"
+   fi
+fi
+
+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
+
+# 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
+
+
 # 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], [
-    CFLAGS="$CFLAGS -falign-loops=1"
+    CFLAGS="$TARGET_CFLAGS -falign-loops=1 -Werror"
     AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
         [grub_cv_cc_falign_loop=yes],
        [grub_cv_cc_falign_loop=no])
   ])
 
+  AC_CACHE_CHECK([whether -malign-loops works], [grub_cv_cc_malign_loop], [
+    CFLAGS="$TARGET_CFLAGS -malign-loops=1 -Werror"
+    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
+        [grub_cv_cc_malign_loop=yes],
+       [grub_cv_cc_malign_loop=no])
+  ])
+
   if test "x$grub_cv_cc_falign_loop" = xyes; then
     TARGET_CFLAGS="$TARGET_CFLAGS -falign-jumps=1 -falign-loops=1 -falign-functions=1"
-  else
+  elif test "x$grub_cv_cc_malign_loop" = xyes; then
     TARGET_CFLAGS="$TARGET_CFLAGS -malign-jumps=1 -malign-loops=1 -malign-functions=1"
   fi
 fi
 
+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
+
 if ( test "x$target_cpu" = xi386 || test "x$target_cpu" = xx86_64 ) && test "x$platform" != xemu; then
   # Some toolchains enable these features by default, but they need
   # registers that aren't set up properly in GRUB.
@@ -499,12 +655,10 @@ fi
 # 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], [
-  SAVE_CFLAGS="$CFLAGS"
-  CFLAGS="$CFLAGS -fno-dwarf2-cfi-asm"
+  CFLAGS="$TARGET_CFLAGS -fno-dwarf2-cfi-asm"
   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
       [grub_cv_cc_fno_dwarf2_cfi_asm=yes],
       [grub_cv_cc_fno_dwarf2_cfi_asm=no])
-  CFLAGS="$SAVE_CFLAGS"
 ])
 
 if test "x$grub_cv_cc_fno_dwarf2_cfi_asm" = xyes; then
@@ -513,7 +667,7 @@ fi
 
 if test x"$target_os" = xcygwin; then
   AC_CACHE_CHECK([whether option -fno-reorder-functions works], grub_cv_cc_no_reorder_functions, [
-    CFLAGS="$CFLAGS -fno-reorder-functions"
+    CFLAGS="$TARGET_CFLAGS -fno-reorder-functions"
     AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
                      [grub_cv_cc_no_reorder_functions=yes],
                      [grub_cv_cc_no_reorder_functions=no])
@@ -529,76 +683,23 @@ fi
 # 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], [
-  SAVE_CFLAGS="$CFLAGS"
-  CFLAGS="$CFLAGS -fno-dwarf2-cfi-asm"
+  CFLAGS="$TARGET_CFLAGS -fno-dwarf2-cfi-asm"
   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
       [grub_cv_cc_fno_asynchronous_unwind_tables=yes],
       [grub_cv_cc_fno_asynchronous_unwind_tables=no])
-  CFLAGS="$SAVE_CFLAGS"
 ])
 
 if test "x$grub_cv_cc_fno_asynchronous_unwind_tables" = xyes; then
   TARGET_CFLAGS="$TARGET_CFLAGS -fno-asynchronous-unwind-tables"
 fi
 
-grub_apple_target_cc
-if test x$grub_cv_apple_target_cc = xyes ; then
-  TARGET_CPPFLAGS="$TARGET_CPPFLAGS -fnested-functions"
-
-  CFLAGS="$CFLAGS -fnested-functions"
-  TARGET_APPLE_CC=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=""
-else
-  TARGET_APPLE_CC=0
-  TARGET_LDFLAGS_OLDMAGIC="-Wl,-N"
-# Use linker script if present, otherwise use builtin -N script.
-if test -f "${srcdir}/${grub_coredir}/conf/${target_cpu}-${platform}-${target_os}-img-ld.sc"; then
-  TARGET_IMG_LDSCRIPT='$(top_srcdir)'"/${grub_coredir}/conf/${target_cpu}-${platform}-${target_os}-img-ld.sc"
-  TARGET_IMG_LDFLAGS="-Wl,-T${TARGET_IMG_LDSCRIPT}"
-  TARGET_IMG_LDFLAGS_AC="-Wl,-T${srcdir}/${grub_coredir}/conf/${target_cpu}-${platform}-${target_os}-img-ld.sc"
-  TARGET_IMG_BASE_LDOPT="-Wl,-Ttext"
-else
-  TARGET_IMG_LDSCRIPT=
-  TARGET_IMG_LDFLAGS='-Wl,-N'
-  TARGET_IMG_LDFLAGS_AC='-Wl,-N'
-  TARGET_IMG_BASE_LDOPT="-Wl,-Ttext"
-fi
-TARGET_IMG_CFLAGS=
-fi
-
-AC_SUBST(TARGET_LDFLAGS_OLDMAGIC)
-
-# For platforms where ELF is not the default link format.
-AC_MSG_CHECKING([for command to convert module to ELF format])
-case "${target_os}" in
-  cygwin) TARGET_OBJ2ELF='./build-grub-pe2elf';
-# FIXME: put proper test here
-  NEED_REGISTER_FRAME_INFO=1
-  ;;
-  *) NEED_REGISTER_FRAME_INFO=0 ;;
-esac
-AC_MSG_RESULT([$TARGET_OBJ2ELF])
-
-
 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"$target_os" = xcygwin ; then
+if test x"$grub_cv_target_cc_link_format" = x-mi386pe ; then
   efiemu_excuse="not available on cygwin"
 fi
 if test x"$target_cpu" != xi386 ; then
@@ -609,17 +710,39 @@ if test x"$platform" = xefi ; then
 fi
 if test x"$efiemu_excuse" = x ; then
   AC_CACHE_CHECK([whether options required for efiemu work], grub_cv_cc_efiemu, [
-    SAVED_CFLAGS="$CFLAGS"
-    CFLAGS="$CFLAGS -m64 -mcmodel=large -mno-red-zone -nostdlib"
+    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])
-    CFLAGS="$SAVED_CFLAGS"
   ])
   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])
 fi
@@ -629,82 +752,97 @@ else
 enable_efiemu=no
 fi
 AC_SUBST([enable_efiemu])
+AC_SUBST([EFIEMU64_LINK_FORMAT])
 
-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"
+CFLAGS="$TARGET_CFLAGS"
+
+if test x"$target_cpu" = xi386 || test x"$target_cpu" = xx86_64; then
+  AC_CACHE_CHECK([for target linking format], [grub_cv_target_cc_link_format], [
+    grub_cv_target_cc_link_format=unknown
+    for format in -melf_${target_cpu} -melf_${target_cpu}_fbsd -melf_${target_cpu}_obsd -melf_${target_cpu}_haiku -m${target_cpu}pe -arch,${target_cpu}; do
+      if test x${target_cpu} != xi386 && test x$format = x${target_cpu}pe; then
+        continue
+      fi
+      CFLAGS="$TARGET_CFLAGS"
+      LDFLAGS="$TARGET_LDFLAGS -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_link_format="$format"
+       break;
+      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"
+  if test x"$grub_cv_target_cc_link_format" = x-mi386pe; then
+    TARGET_OBJ2ELF='./build-grub-pe2elf';
+  fi
 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"
+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=""
+elif test x$grub_cv_target_cc_link_format = x-mi386pe; then
+  TARGET_APPLE_LINKER=0
+  TARGET_LDFLAGS_OLDMAGIC="-Wl,-N"
+  TARGET_IMG_LDSCRIPT='$(top_srcdir)'"/${grub_coredir}/conf/i386-cygwin-img-ld.sc"
+  TARGET_IMG_LDFLAGS="-Wl,-T${TARGET_IMG_LDSCRIPT}"
+  TARGET_IMG_LDFLAGS_AC="-Wl,-T${srcdir}/${grub_coredir}/conf/i386-cygwin-img-ld.sc"
+  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
 
-case "$target_os" in
-  cygwin)
-    ;;
-  freebsd)
-    if test x"$target_cpu" = xi386; then
-      TARGET_LDFLAGS="$TARGET_LDFLAGS -Wl,-melf_i386_fbsd"
-    fi
-    if test x"$target_cpu" = xx86_64; then
-      TARGET_LDFLAGS="$TARGET_LDFLAGS -Wl,-melf_x86_64_fbsd"
-    fi
-    ;;
-  openbsd*)
-    if test x"$target_cpu" = xi386; then
-      TARGET_LDFLAGS="$TARGET_LDFLAGS -Wl,-melf_i386_obsd"
-    fi
-    if test x"$target_cpu" = xx86_64; then
-      TARGET_LDFLAGS="$TARGET_LDFLAGS -Wl,-melf_x86_64_obsd"
-    fi
-    ;;
-  haiku*)
-    if test x"$target_cpu" = xi386; then
-      TARGET_LDFLAGS="$TARGET_LDFLAGS -Wl,-melf_i386_haiku"
-    fi
-    if test x"$target_cpu" = xx86_64; then
-      TARGET_LDFLAGS="$TARGET_LDFLAGS -Wl,-melf_x86_64_haiku"
-    fi
-    ;;
-  *)
-    if test x"$target_cpu" = xi386; then
-      TARGET_LDFLAGS="$TARGET_LDFLAGS -Wl,-melf_i386"
-    fi
-    if test x"$target_cpu" = xx86_64; then
-      TARGET_LDFLAGS="$TARGET_LDFLAGS -Wl,-melf_x86_64"
-    fi
-    ;;
-esac
+AC_SUBST(TARGET_LDFLAGS_OLDMAGIC)
 
-if test "$target_cpu" = x86_64; then
+
+LDFLAGS="$TARGET_LDFLAGS"
+
+if test "$target_cpu" = x86_64 || test "$target_cpu-$platform" = sparc64-emu ; then
   # Use large model to support 4G memory
   AC_CACHE_CHECK([whether option -mcmodel=large works], grub_cv_cc_mcmodel, [
-    SAVED_CFLAGS=$CFLAGS
-    CFLAGS="$CFLAGS -m64 -mcmodel=large"
+    CFLAGS="$TARGET_CFLAGS -m64 -mcmodel=large"
     AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
                      [grub_cv_cc_mcmodel=yes],
                      [grub_cv_cc_mcmodel=no])
   ])
-  if test "x$grub_cv_cc_mcmodel" = xno; then
-    AC_MSG_ERROR([-mcmodel=large not supported. Upgrade your gcc.])
-  else
+  if test "x$grub_cv_cc_mcmodel" = xyes; then
     TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=large"
+  elif test "$target_cpu-$platform" = sparc64-emu; then
+    TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
   fi
 fi
 
 if test "$target_cpu"-"$platform" = x86_64-efi; then
   # 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, [
-    CFLAGS="$CFLAGS -m64 -mno-red-zone"
+    CFLAGS="$TARGET_CFLAGS -m64 -mno-red-zone"
     AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
                      [grub_cv_cc_no_red_zone=yes],
                      [grub_cv_cc_no_red_zone=no])
@@ -716,10 +854,24 @@ if test "$target_cpu"-"$platform" = x86_64-efi; then
   TARGET_CFLAGS="$TARGET_CFLAGS -mno-red-zone"
 fi
 
+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
+fi
+
 #
 # Compiler features.
 #
 
+CFLAGS="$TARGET_CFLAGS"
+
 # Position independent executable.
 grub_CHECK_PIE
 [# Need that, because some distributions ship compilers that include
@@ -728,14 +880,25 @@ if [ x"$pie_possible" = xyes ]; then
   TARGET_CFLAGS="$TARGET_CFLAGS -fno-PIE"
 fi]
 
+CFLAGS="$TARGET_CFLAGS"
+
 # Position independent executable.
 grub_CHECK_PIC
-[# Need that, because some distributions ship compilers that include
-# `-fPIC' in the default specs.
-if [ x"$pic_possible" = xyes ]; then
-  TARGET_CFLAGS="$TARGET_CFLAGS -fno-PIC"
+[# 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"
 fi]
 
+CFLAGS="$TARGET_CFLAGS"
+
 # Smashing stack protector.
 grub_CHECK_STACK_PROTECTOR
 # Need that, because some distributions ship compilers that include
@@ -743,6 +906,9 @@ grub_CHECK_STACK_PROTECTOR
 if test "x$ssp_possible" = xyes; then
   TARGET_CFLAGS="$TARGET_CFLAGS -fno-stack-protector"
 fi
+
+CFLAGS="$TARGET_CFLAGS"
+
 grub_CHECK_STACK_ARG_PROBE
 # Cygwin's GCC uses alloca() to probe the stackframe on static
 # stack allocations above some threshold.
@@ -750,97 +916,86 @@ if test x"$sap_possible" = xyes; then
   TARGET_CFLAGS="$TARGET_CFLAGS -mno-stack-arg-probe"
 fi
 
-# -mno-unaligned-access
+CFLAGS="$TARGET_CFLAGS"
+
+# -mno-unaligned-access -mstrict-align
 if test "$target_cpu" = arm; then
-  grub_CHECK_NO_UNALIGNED_ACCESS
-  if test x"$nua_possible" = xyes; then
-    TARGET_CFLAGS="$TARGET_CFLAGS -mno-unaligned-access"
+  AC_CACHE_CHECK([for compile options to get strict alignment], [grub_cv_target_cc_strict_align], [
+    grub_cv_target_cc_strict_align=
+    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"
+       break;
+      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"
+  fi
+  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
 fi
 
-AC_ARG_ENABLE([werror],
-             [AS_HELP_STRING([--disable-werror],
-                             [do not use -Werror when building GRUB])])
-if test x"$enable_werror" != xno ; then
-  TARGET_CFLAGS="$TARGET_CFLAGS -Werror"
-  HOST_CFLAGS="$HOST_CFLAGS -Werror"
-fi
-
-TARGET_CPP="$TARGET_CC -E"
-TARGET_CCAS=$TARGET_CC
-
-GRUB_TARGET_CPU="${target_cpu}"
-GRUB_PLATFORM="${platform}"
-
-AC_SUBST(GRUB_TARGET_CPU)
-AC_SUBST(GRUB_PLATFORM)
-
-AC_SUBST(TARGET_OBJCONV)
-AC_SUBST(TARGET_CPP)
-AC_SUBST(TARGET_CCAS)
-AC_SUBST(TARGET_OBJ2ELF)
-AC_SUBST(TARGET_APPLE_CC)
-AC_SUBST(TARGET_MODULE_FORMAT)
-
-AC_SUBST(TARGET_CFLAGS)
-AC_SUBST(TARGET_LDFLAGS)
-AC_SUBST(TARGET_CPPFLAGS)
-AC_SUBST(TARGET_CCASFLAGS)
-
-AC_SUBST(TARGET_IMG_LDSCRIPT)
-AC_SUBST(TARGET_IMG_LDFLAGS)
-AC_SUBST(TARGET_IMG_CFLAGS)
-AC_SUBST(TARGET_IMG_BASE_LDOPT)
-
-AC_SUBST(HOST_CFLAGS)
-AC_SUBST(HOST_LDFLAGS)
-AC_SUBST(HOST_CPPFLAGS)
-AC_SUBST(HOST_CCASFLAGS)
-
-AC_SUBST(BUILD_LIBM)
-
 # Set them to their new values for the tests below.
 CC="$TARGET_CC"
-if test "x$TARGET_APPLE_CC" = x1 ; then
-CFLAGS="$TARGET_CFLAGS -nostdlib -Wno-error"
+if test "x$TARGET_APPLE_LINKER" = x1 ; then
+CFLAGS="$TARGET_CFLAGS -nostdlib -static -Wno-error"
 else
-CFLAGS="$TARGET_CFLAGS -nostdlib -Wl,--defsym,___main=0x8100 -Wno-error"
+CFLAGS="$TARGET_CFLAGS -nostdlib -Wno-error"
 fi
 CPPFLAGS="$TARGET_CPPFLAGS"
-if test x$target_cpu = xi386 || test x$target_cpu = xx86_64 ; then
-LIBS=
+if test x$target_cpu = xi386 || test x$target_cpu = xx86_64 || test "x$grub_cv_cc_target_clang" = xyes ; then
+TARGET_LIBGCC=
 else
-LIBS=-lgcc
+TARGET_LIBGCC=-lgcc
 fi
 
+LIBS="$TARGET_LIBGCC"
+
 grub_ASM_USCORE
+if test "x$TARGET_APPLE_LINKER" = x0 ; then
 if test x$grub_cv_asm_uscore = xyes; then
-CFLAGS="$CFLAGS -Wl,--defsym,_abort=_main"
+DEFSYM="-Wl,--defsym,_abort=_main -Wl,--defsym,__main=_main"
 else
-CFLAGS="$CFLAGS -Wl,--defsym,abort=main"
+DEFSYM="-Wl,--defsym,abort=main -Wl,--defsym,_main=main -Wl,--defsym,__main=main"
+fi
+CFLAGS="$TARGET_CFLAGS -nostdlib $DEFSYM"
 fi
 
 # Check for libgcc symbols
-AC_CHECK_FUNCS(__bswapsi2 __bswapdi2 __ashldi3 __ashrdi3 __lshrdi3 __ucmpdi2 _restgpr_14_x __udivsi3 __umoddi3 __udivdi3 __divsi3 __modsi3 __umodsi3 __moddi3 __divdi3 __ctzdi2 __ctzsi2)
+AC_CHECK_FUNCS(__bswapsi2 __bswapdi2 __ashldi3 __ashrdi3 __lshrdi3 __ucmpdi2 _restgpr_14_x)
 
-if test "x$TARGET_APPLE_CC" = x1 ; then
-CFLAGS="$TARGET_CFLAGS -nostdlib"
+if test "x$TARGET_APPLE_LINKER" = x1 ; then
+CFLAGS="$TARGET_CFLAGS -nostdlib -static"
 else
-CFLAGS="$TARGET_CFLAGS -nostdlib -Wl,--defsym,___main=0x8100"
+CFLAGS="$TARGET_CFLAGS -nostdlib"
 fi
 LIBS=""
 
 # Defined in aclocal.m4.
 grub_PROG_TARGET_CC
-if test "x$TARGET_APPLE_CC" != x1 ; then
+if test "x$TARGET_APPLE_LINKER" != x1 ; then
 grub_PROG_OBJCOPY_ABSOLUTE
 fi
 grub_PROG_LD_BUILD_ID_NONE
 if test "x$target_cpu" = xi386; then
-  if test "$platform" != emu && test "x$TARGET_APPLE_CC" != x1 ; then
+  if test "$platform" != emu && test "x$TARGET_APPLE_LINKER" != x1 ; then
     if test ! -z "$TARGET_IMG_LDSCRIPT"; then
       # Check symbols provided by linker script.
-      CFLAGS="$TARGET_CFLAGS -nostdlib ${TARGET_IMG_LDFLAGS_AC} ${TARGET_IMG_BASE_LDOPT},8000 -Wl,--defsym,___main=0x8100"
+      CFLAGS="$TARGET_CFLAGS -nostdlib ${TARGET_IMG_LDFLAGS_AC} ${TARGET_IMG_BASE_LDOPT},0x8000"
     fi
     grub_CHECK_BSS_START_SYMBOL
     grub_CHECK_END_SYMBOL
@@ -850,6 +1005,12 @@ if test "x$target_cpu" = xi386; then
   grub_I386_ASM_ADDR32
 fi
 
+grub_PROG_NM_WORKS
+grub_PROG_NM_MINUS_P
+grub_PROG_NM_DEFINED_ONLY
+AC_SUBST(TARGET_NMFLAGS_MINUS_P)
+AC_SUBST(TARGET_NMFLAGS_DEFINED_ONLY)
+
 if test "$platform" != emu; then
 AC_CACHE_CHECK([whether -nostdinc -isystem works], [grub_cv_cc_isystem], [
   SAVED_CPPFLAGS="$CPPFLAGS"
@@ -868,13 +1029,11 @@ fi
 fi
 
 AC_CACHE_CHECK([whether -Wtrampolines work], [grub_cv_cc_wtrampolines], [
-  SAVED_CFLAGS="$CFLAGS"
-  CFLAGS="$TARGET_CFLAGS -Wtrampolines"
+  CFLAGS="$TARGET_CFLAGS -Wtrampolines -Werror"
   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <stdarg.h>
 int va_arg_func (int fixed, va_list args);]], [[]])],
       [grub_cv_cc_wtrampolines=yes],
       [grub_cv_cc_wtrampolines=no])
-  CFLAGS="$SAVED_CFLAGS"
 ])
 
 if test x"$grub_cv_cc_wtrampolines" = xyes ; then
@@ -1018,6 +1177,13 @@ fi
 AC_SUBST([enable_grub_emu_sdl])
 AC_SUBST([enable_grub_emu_usb])
 AC_SUBST([enable_grub_emu_pci])
+
+else
+
+# Ignore --enable-emu-* if platform is not emu
+enable_grub_emu_sdl=no
+enable_grub_emu_usb=no
+enable_grub_emu_pci=no
 fi
 
 AC_ARG_ENABLE([grub-mkfont],
@@ -1042,10 +1208,14 @@ if test x"$grub_mkfont_excuse" = x ; then
   freetype_cflags=`$FREETYPE --cflags`
   freetype_libs=`$FREETYPE --libs`
   SAVED_CPPFLAGS="$CPPFLAGS"
+  SAVED_LIBS="$LIBS"
   CPPFLAGS="$CPPFLAGS $freetype_cflags"
+  LIBS="$LIBS $freetype_libs"
   AC_CHECK_HEADERS([ft2build.h], [],
        [grub_mkfont_excuse=["need freetype2 headers"]])
+  AC_LINK_IFELSE([AC_LANG_CALL([], [FT_Load_Glyph])], [], [grub_mkfont_excuse=["freetype2 library unusable"]])
   CPPFLAGS="$SAVED_CPPFLAGS"
+  LIBS="$SAVED_LIBS"
 fi
 
 if test x"$enable_grub_mkfont" = xyes && test x"$grub_mkfont_excuse" != x ; then
@@ -1097,9 +1267,13 @@ if test x"$grub_build_mkfont_excuse" = x ; then
   build_freetype_cflags=`$BUILD_FREETYPE --cflags`
   build_freetype_libs=`$BUILD_FREETYPE --libs`
   SAVED_CPPFLAGS_2="$CPPFLAGS"
+  SAVED_LIBS="$LIBS"
   CPPFLAGS="$CPPFLAGS $build_freetype_cflags"
+  LIBS="$LIBS $build_freetype_libs"
   AC_CHECK_HEADERS([ft2build.h], [],
        [grub_build_mkfont_excuse=["need freetype2 headers"]])
+  AC_LINK_IFELSE([AC_LANG_CALL([], [FT_Load_Glyph])], [], [grub_build_mkfont_excuse=["freetype2 library unusable"]])
+  LIBS="$SAVED_LIBS"
   CPPFLAGS="$SAVED_CPPFLAGS_2"
 fi
 
@@ -1111,8 +1285,8 @@ if test x"$grub_build_mkfont_excuse" = x ; then
 else
   enable_build_grub_mkfont=no
 fi
-if test x"$enable_build_grub_mkfont" = xno && ( test "x$platform" = xqemu || test "x$platform" = xloongson || test "x$platform" = xqemu_mips || test "x$target_cpu"-"$platform" = xpowerpc-ieee1275 ); then
-  AC_MSG_ERROR([qemu, powerpc-ieee1275 and loongson ports needs build-time grub-mkfont])
+if test x"$enable_build_grub_mkfont" = xno && ( test "x$platform" = xqemu || test "x$platform" = xloongson || test "x$platform" = xqemu_mips || test "x$target_cpu"-"$platform" = xpowerpc-ieee1275 || test "x$platform" = xcoreboot ); then
+  AC_MSG_ERROR([qemu, powerpc-ieee1275, coreboot and loongson ports needs build-time grub-mkfont])
 fi
 
 AC_SUBST([build_freetype_cflags])
@@ -1128,7 +1302,14 @@ DJVU_FONT_SOURCE=
 
 starfield_excuse=
 
-if test x"$enable_build_grub_mkfont" = xno ; then
+AC_ARG_ENABLE([grub-themes],
+             [AS_HELP_STRING([--enable-grub-themes],
+                             [build and install GRUB themes (default=guessed)])])
+if test x"$enable_grub_themes" = xno ; then
+  starfield_excuse="explicitly disabled"
+fi
+
+if test x"$starfield_excuse" = x && test x"$enable_build_grub_mkfont" = xno ; then
   starfield_excuse="No build-time grub-mkfont"
 fi
 
@@ -1147,6 +1328,10 @@ if test x"$starfield_excuse" = x; then
    fi
 fi
 
+if test x"$enable_grub_themes" = xyes &&  test x"$starfield_excuse" != x; then
+   AC_MSG_ERROR([themes were explicitly requested but requirements are not satisfied])
+fi
+
 AC_SUBST([DJVU_FONT_SOURCE])
 
 FONT_SOURCE=
@@ -1154,6 +1339,11 @@ FONT_SOURCE=
 for ext in pcf pcf.gz bdf bdf.gz ttf ttf.gz; do
   for dir in . /usr/src /usr/share/fonts/X11/misc /usr/share/fonts/unifont /usr/share/fonts/uni /usr/share/fonts/truetype/unifont /usr/share/fonts/misc; do
     if test -f "$dir/unifont.$ext"; then
+      md5="$(md5sum "$dir/unifont.$ext"|awk '{ print $1; }')"
+      # PCF and BDF from version 6.3 isn't hanled properly by libfreetype.
+      if test "$md5" = 0a54834d2788c83886a3e1785a6a1e61 || test "$md5" = 28f2565c7a41d8d407e2551159385edb || test "$md5" = dae5e588461b3b92b87b6ffee734f936 || test "$md5" = 4a3d687aa5bb329ed05f4263a1016791 ; then
+        continue;
+      fi
       FONT_SOURCE="$dir/unifont.$ext"
       break 2
     fi
@@ -1164,8 +1354,8 @@ if test x"$enable_build_grub_mkfont" = xno ; then
   FONT_SOURCE=
 fi
 
-if test "x$FONT_SOURCE" = x && ( test "x$platform" = xqemu || test "x$platform" = xloongson || test "x$platform" = xqemu_mips || test "x$target_cpu"-"$platform" = xpowerpc-ieee1275 ); then
-   AC_MSG_ERROR([qemu, powerpc-ieee1275 and loongson ports need unifont])
+if test "x$FONT_SOURCE" = x && ( test "x$platform" = xqemu || test "x$platform" = xloongson || test "x$platform" = xqemu_mips || test "x$target_cpu"-"$platform" = xpowerpc-ieee1275 || test "x$platform" = xcoreboot ); then
+   AC_MSG_ERROR([qemu, powerpc-ieee1275, coreboot and loongson ports need unifont])
 fi
 
 AC_SUBST([FONT_SOURCE])
@@ -1249,10 +1439,32 @@ fi
 
 AC_SUBST([LIBGEOM])
 
+AC_ARG_ENABLE([liblzma],
+              [AS_HELP_STRING([--enable-liblzma],
+                              [enable liblzma integration (default=guessed)])])
+if test x"$enable_liblzma" = xno ; then
+  liblzma_excuse="explicitly disabled"
+fi
+
+if test x"$liblzma_excuse" = x ; then
 AC_CHECK_LIB([lzma], [lzma_code],
-             [LIBLZMA="-llzma"
-              AC_DEFINE([HAVE_LIBLZMA], [1],
-                        [Define to 1 if you have the LZMA library.])],)
+             [],[liblzma_excuse="need lzma library"])
+fi
+if test x"$liblzma_excuse" = x ; then
+AC_CHECK_HEADER([lzma.h], [], [liblzma_excuse="need lzma header"])
+fi
+
+if test x"$enable_liblzma" = xyes && test x"$liblzma_excuse" != x ; then
+  AC_MSG_ERROR([liblzma support was explicitly requested but requirements are not satisfied])
+fi
+
+
+if test x"$liblzma_excuse" = x ; then
+   LIBLZMA="-llzma"
+   AC_DEFINE([USE_LIBLZMA], [1],
+            [Define to 1 if you have the LZMA library.])
+fi
+
 AC_SUBST([LIBLZMA])
 
 AC_ARG_ENABLE([libzfs],
@@ -1301,8 +1513,6 @@ LIBS=""
 AC_SUBST([FONT_SOURCE])
 AS_IF([test x$target_cpu = xi386 -a x$platform = xqemu],
            [AC_SUBST([GRUB_BOOT_MACHINE_LINK_ADDR], 0xffe00)])
-AS_IF([test x$TARGET_APPLE_CC = x1],
-           [AC_SUBST([USE_APPLE_CC_FIXES], yes)])
 
 AC_SUBST(HAVE_ASM_USCORE)
 AC_SUBST(ADDR32)
@@ -1313,11 +1523,60 @@ AC_SUBST(PACKAGE)
 AC_SUBST(VERSION)
 AC_SUBST(NEED_REGISTER_FRAME_INFO)
 
+AC_ARG_ENABLE([werror],
+             [AS_HELP_STRING([--disable-werror],
+                             [do not use -Werror when building GRUB])])
+if test x"$enable_werror" != xno ; then
+  TARGET_CFLAGS="$TARGET_CFLAGS -Werror"
+  HOST_CFLAGS="$HOST_CFLAGS -Werror"
+fi
+
+if test "x$grub_cv_cc_target_clang" = xno; then
+   TARGET_LDFLAGS_STATIC_LIBGCC="-static-libgcc"
+else
+   TARGET_LDFLAGS_STATIC_LIBGCC=
+fi
+
+TARGET_CPP="$TARGET_CC -E"
+TARGET_CCAS=$TARGET_CC
+
+GRUB_TARGET_CPU="${target_cpu}"
+GRUB_PLATFORM="${platform}"
+
+AC_SUBST(GRUB_TARGET_CPU)
+AC_SUBST(GRUB_PLATFORM)
+
+AC_SUBST(TARGET_OBJCONV)
+AC_SUBST(TARGET_LIBGCC)
+AC_SUBST(TARGET_LDFLAGS_STATIC_LIBGCC)
+AC_SUBST(TARGET_CPP)
+AC_SUBST(TARGET_CCAS)
+AC_SUBST(TARGET_OBJ2ELF)
+AC_SUBST(TARGET_MODULE_FORMAT)
+
+AC_SUBST(TARGET_CFLAGS)
+AC_SUBST(TARGET_LDFLAGS)
+AC_SUBST(TARGET_CPPFLAGS)
+AC_SUBST(TARGET_CCASFLAGS)
+
+AC_SUBST(TARGET_IMG_LDFLAGS)
+AC_SUBST(TARGET_IMG_CFLAGS)
+AC_SUBST(TARGET_IMG_BASE_LDOPT)
+AC_SUBST(TARGET_APPLE_LINKER)
+
+AC_SUBST(HOST_CFLAGS)
+AC_SUBST(HOST_LDFLAGS)
+AC_SUBST(HOST_CPPFLAGS)
+AC_SUBST(HOST_CCASFLAGS)
+
+AC_SUBST(BUILD_LIBM)
+
 #
 # Automake conditionals
 #
 
 AM_CONDITIONAL([COND_emu], [test x$platform = xemu])
+AM_CONDITIONAL([COND_clang], [test x$grub_cv_cc_target_clang = xyes])
 AM_CONDITIONAL([COND_i386_pc], [test x$target_cpu = xi386 -a x$platform = xpc])
 AM_CONDITIONAL([COND_i386_efi], [test x$target_cpu = xi386 -a x$platform = xefi])
 AM_CONDITIONAL([COND_ia64_efi], [test x$target_cpu = xia64 -a x$platform = xefi])
@@ -1326,10 +1585,13 @@ AM_CONDITIONAL([COND_i386_ieee1275], [test x$target_cpu = xi386 -a x$platform =
 AM_CONDITIONAL([COND_i386_coreboot], [test x$target_cpu = xi386 -a x$platform = xcoreboot])
 AM_CONDITIONAL([COND_i386_multiboot], [test x$target_cpu = xi386 -a x$platform = xmultiboot])
 AM_CONDITIONAL([COND_x86_64_efi], [test x$target_cpu = xx86_64 -a x$platform = xefi])
+AM_CONDITIONAL([COND_i386_xen], [test x$target_cpu = xi386 -a x$platform = xxen])
+AM_CONDITIONAL([COND_x86_64_xen], [test x$target_cpu = xx86_64 -a x$platform = xxen])
 AM_CONDITIONAL([COND_mips_loongson], [test x$target_cpu = xmipsel -a x$platform = xloongson])
 AM_CONDITIONAL([COND_mips_qemu_mips], [test "(" x$target_cpu = xmips -o x$target_cpu = xmipsel ")"  -a x$platform = xqemu_mips])
 AM_CONDITIONAL([COND_mips_arc], [test "(" x$target_cpu = xmips -o x$target_cpu = xmipsel ")"  -a x$platform = xarc])
 AM_CONDITIONAL([COND_sparc64_ieee1275], [test x$target_cpu = xsparc64 -a x$platform = xieee1275])
+AM_CONDITIONAL([COND_sparc64_emu], [test x$target_cpu = xsparc64 -a x$platform = xemu])
 AM_CONDITIONAL([COND_powerpc_ieee1275], [test x$target_cpu = xpowerpc -a x$platform = xieee1275])
 AM_CONDITIONAL([COND_mips], [test x$target_cpu = xmips -o x$target_cpu = xmipsel])
 AM_CONDITIONAL([COND_mipsel], [test x$target_cpu = xmipsel])
@@ -1337,6 +1599,8 @@ AM_CONDITIONAL([COND_mipseb], [test x$target_cpu = xmips])
 AM_CONDITIONAL([COND_arm], [test x$target_cpu = xarm ])
 AM_CONDITIONAL([COND_arm_uboot], [test x$target_cpu = xarm -a x$platform = xuboot])
 AM_CONDITIONAL([COND_arm_efi], [test x$target_cpu = xarm -a x$platform = xefi])
+AM_CONDITIONAL([COND_arm64], [test x$target_cpu = xarm64 ])
+AM_CONDITIONAL([COND_arm64_efi], [test x$target_cpu = xarm64 -a x$platform = xefi])
 
 AM_CONDITIONAL([COND_HOST_HURD], [test x$host_kernel = xhurd])
 AM_CONDITIONAL([COND_HOST_LINUX], [test x$host_kernel = xlinux])
@@ -1353,7 +1617,13 @@ AM_CONDITIONAL([COND_GRUB_EMU_PCI], [test x$enable_grub_emu_pci = xyes])
 AM_CONDITIONAL([COND_GRUB_MKFONT], [test x$enable_grub_mkfont = xyes])
 AM_CONDITIONAL([COND_GRUB_MOUNT], [test x$enable_grub_mount = xyes])
 AM_CONDITIONAL([COND_HAVE_FONT_SOURCE], [test x$FONT_SOURCE != x])
-AM_CONDITIONAL([COND_APPLE_CC], [test x$TARGET_APPLE_CC = x1])
+if test x$FONT_SOURCE != x ; then
+   HAVE_FONT_SOURCE=1
+else
+   HAVE_FONT_SOURCE=0
+fi
+AC_SUBST(HAVE_FONT_SOURCE)
+AM_CONDITIONAL([COND_APPLE_LINKER], [test x$TARGET_APPLE_LINKER = x1])
 AM_CONDITIONAL([COND_ENABLE_EFIEMU], [test x$enable_efiemu = xyes])
 AM_CONDITIONAL([COND_ENABLE_CACHE_STATS], [test x$DISK_CACHE_STATS = x1])
 AM_CONDITIONAL([COND_ENABLE_BOOT_TIME_STATS], [test x$BOOT_TIME_STATS = x1])
@@ -1361,8 +1631,21 @@ AM_CONDITIONAL([COND_ENABLE_BOOT_TIME_STATS], [test x$BOOT_TIME_STATS = x1])
 AM_CONDITIONAL([COND_HAVE_CXX], [test x$HAVE_CXX = xyes])
 
 AM_CONDITIONAL([COND_HAVE_ASM_USCORE], [test x$HAVE_ASM_USCORE = x1])
-AM_CONDITIONAL([COND_CYGWIN], [test x$target_os = xcygwin])
 AM_CONDITIONAL([COND_STARFIELD], [test "x$starfield_excuse" = x])
+AM_CONDITIONAL([COND_HAVE_EXEC], [test "x$have_exec" = xy])
+
+test "x$prefix" = xNONE && prefix="$ac_default_prefix"
+test "x$exec_prefix" = xNONE && exec_prefix="${prefix}"
+datarootdir="$(eval echo "$datarootdir")"
+grub_libdir="$(eval echo "$libdir")"
+grub_localedir="$(eval echo "$localedir")"
+grub_datadir="$(eval echo "$datadir")"
+grub_sysconfdir="$(eval echo "$sysconfdir")"
+AC_DEFINE_UNQUOTED(LOCALEDIR, "$grub_localedir", [Locale dir])
+AC_DEFINE_UNQUOTED(GRUB_LIBDIR, "$grub_libdir", [Library dir])
+AC_DEFINE_UNQUOTED(GRUB_DATADIR, "$grub_datadir", [Data dir])
+AC_DEFINE_UNQUOTED(GRUB_SYSCONFDIR, "$grub_sysconfdir", [Configuration dir])
+
 
 # Output files.
 cpudir="${target_cpu}"
@@ -1474,8 +1757,8 @@ else
   echo Build-time grub-mkfont: No "($grub_build_mkfont_excuse)"
   echo "Without unifont (no build-time grub-mkfont)"
 fi
-if test "x$LIBLZMA" = x ; then
-echo "Without liblzma (no support for XZ-compressed mips images)"
+if test x"$liblzma_excuse" != x ; then
+echo "Without liblzma (no support for XZ-compressed mips images) ($liblzma_excuse)"
 else
 echo "With liblzma from $LIBLZMA (support for XZ-compressed mips images)"
 fi