6a732d08eeb6e2bf2f43f512ee612426cd957e21
[ghc.git] / aclocal.m4
1 # Extra autoconf macros for the Glasgow fptools
2 #
3 # To be a good autoconf citizen, names of local macros have prefixed with FP_ to
4 # ensure we don't clash with any pre-supplied autoconf ones.
5
6
7 AC_DEFUN([GHC_SELECT_FILE_EXTENSIONS],
8 [
9     $2=''
10     $3='.so'
11     case $1 in
12     *-unknown-cygwin32)
13         AC_MSG_WARN([GHC does not support the Cygwin target at the moment])
14         AC_MSG_WARN([I'm assuming you wanted to build for i386-unknown-mingw32])
15         exit 1
16         ;;
17     # examples: i386-unknown-mingw32, i686-w64-mingw32, x86_64-w64-mingw32
18     *-mingw32)
19         windows=YES
20         $2='.exe'
21         $3='.dll'
22         ;;
23     # apple platform uses .dylib (macOS, iOS, ...)
24     *-apple-*)
25         $3='.dylib'
26         ;;
27     esac
28 ])
29
30 # FPTOOLS_SET_PLATFORM_VARS
31 # ----------------------------------
32 # Set the platform variables
33 AC_DEFUN([FPTOOLS_SET_PLATFORM_VARS],
34 [
35     # If no argument was given for a configuration variable, then discard
36     # the guessed canonical system and use the configuration of the
37     # bootstrapping ghc. If an argument was given, map it from gnu format
38     # to ghc format.
39     #
40     # For why we do it this way, see: #3637, #1717, #2951
41     #
42     # In bindists, we haven't called AC_CANONICAL_{BUILD,HOST,TARGET}
43     # so this justs uses $bootstrap_target.
44
45     if test "$build_alias" = ""
46     then
47         if test "$bootstrap_target" != ""
48         then
49             build=$bootstrap_target
50             echo "Build platform inferred as: $build"
51         else
52             echo "Can't work out build platform"
53             exit 1
54         fi
55
56         BuildArch=`echo "$build" | sed 's/-.*//'`
57         BuildVendor=`echo "$build" | sed -e 's/.*-\(.*\)-.*/\1/'`
58         BuildOS=`echo "$build" | sed 's/.*-//'`
59     else
60         GHC_CONVERT_CPU([$build_cpu], [BuildArch])
61         GHC_CONVERT_VENDOR([$build_vendor], [BuildVendor])
62         GHC_CONVERT_OS([$build_os], [$BuildArch], [BuildOS])
63     fi
64
65     if test "$host_alias" = ""
66     then
67         if test "$bootstrap_target" != ""
68         then
69             host=$bootstrap_target
70             echo "Host platform inferred as: $host"
71         else
72             echo "Can't work out host platform"
73             exit 1
74         fi
75
76         HostArch=`echo "$host" | sed 's/-.*//'`
77         HostVendor=`echo "$host" | sed -e 's/.*-\(.*\)-.*/\1/'`
78         HostOS=`echo "$host" | sed 's/.*-//'`
79     else
80         GHC_CONVERT_CPU([$host_cpu], [HostArch])
81         GHC_CONVERT_VENDOR([$host_vendor], [HostVendor])
82         GHC_CONVERT_OS([$host_os], [$HostArch], [HostOS])
83     fi
84
85     if test "$target_alias" = ""
86     then
87         if test "$host_alias" != ""
88         then
89             GHC_CONVERT_CPU([$host_cpu], [TargetArch])
90             GHC_CONVERT_VENDOR([$host_vendor], [TargetVendor])
91             GHC_CONVERT_OS([$host_os], [$TargetArch],[TargetOS])
92         else
93             if test "$bootstrap_target" != ""
94             then
95                 target=$bootstrap_target
96                 echo "Target platform inferred as: $target"
97             else
98                 echo "Can't work out target platform"
99                 exit 1
100             fi
101
102             TargetArch=`echo "$target" | sed 's/-.*//'`
103             TargetVendor=`echo "$target" | sed -e 's/.*-\(.*\)-.*/\1/'`
104             TargetOS=`echo "$target" | sed 's/.*-//'`
105         fi
106     else
107         GHC_CONVERT_CPU([$target_cpu], [TargetArch])
108         GHC_CONVERT_VENDOR([$target_vendor], [TargetVendor])
109         GHC_CONVERT_OS([$target_os], [$TargetArch], [TargetOS])
110     fi
111
112     GHC_SELECT_FILE_EXTENSIONS([$host], [exeext_host], [soext_host])
113     GHC_SELECT_FILE_EXTENSIONS([$target], [exeext_target], [soext_target])
114     windows=NO
115     case $host in
116     *-unknown-mingw32)
117         windows=YES
118         ;;
119     esac
120
121     BuildPlatform="$BuildArch-$BuildVendor-$BuildOS"
122     BuildPlatform_CPP=`echo "$BuildPlatform" | sed -e 's/\./_/g' -e 's/-/_/g'`
123     BuildArch_CPP=`    echo "$BuildArch"     | sed -e 's/\./_/g' -e 's/-/_/g'`
124     BuildVendor_CPP=`  echo "$BuildVendor"   | sed -e 's/\./_/g' -e 's/-/_/g'`
125     BuildOS_CPP=`      echo "$BuildOS"       | sed -e 's/\./_/g' -e 's/-/_/g'`
126
127     HostPlatform="$HostArch-$HostVendor-$HostOS"
128     HostPlatform_CPP=`echo "$HostPlatform" | sed -e 's/\./_/g' -e 's/-/_/g'`
129     HostArch_CPP=`    echo "$HostArch"     | sed -e 's/\./_/g' -e 's/-/_/g'`
130     HostVendor_CPP=`  echo "$HostVendor"   | sed -e 's/\./_/g' -e 's/-/_/g'`
131     HostOS_CPP=`      echo "$HostOS"       | sed -e 's/\./_/g' -e 's/-/_/g'`
132
133     TargetPlatform="$TargetArch-$TargetVendor-$TargetOS"
134     TargetPlatform_CPP=`echo "$TargetPlatform" | sed -e 's/\./_/g' -e 's/-/_/g'`
135     TargetArch_CPP=`    echo "$TargetArch"     | sed -e 's/\./_/g' -e 's/-/_/g'`
136     TargetVendor_CPP=`  echo "$TargetVendor"   | sed -e 's/\./_/g' -e 's/-/_/g'`
137     TargetOS_CPP=`      echo "$TargetOS"       | sed -e 's/\./_/g' -e 's/-/_/g'`
138
139     # we intend to pass trough --targets to llvm as is.
140     LLVMTarget_CPP=`    echo "$target"`
141
142     echo "GHC build  : $BuildPlatform"
143     echo "GHC host   : $HostPlatform"
144     echo "GHC target : $TargetPlatform"
145     echo "LLVM target: $target"
146
147     AC_SUBST(BuildPlatform)
148     AC_SUBST(HostPlatform)
149     AC_SUBST(TargetPlatform)
150     AC_SUBST(HostPlatform_CPP)
151     AC_SUBST(BuildPlatform_CPP)
152     AC_SUBST(TargetPlatform_CPP)
153
154     AC_SUBST(HostArch_CPP)
155     AC_SUBST(BuildArch_CPP)
156     AC_SUBST(TargetArch_CPP)
157
158     AC_SUBST(HostOS_CPP)
159     AC_SUBST(BuildOS_CPP)
160     AC_SUBST(TargetOS_CPP)
161     AC_SUBST(LLVMTarget_CPP)
162
163     AC_SUBST(HostVendor_CPP)
164     AC_SUBST(BuildVendor_CPP)
165     AC_SUBST(TargetVendor_CPP)
166
167     AC_SUBST(exeext_host)
168     AC_SUBST(exeext_target)
169     AC_SUBST(soext_host)
170     AC_SUBST(soext_target)
171 ])
172
173
174 # FPTOOLS_SET_HASKELL_PLATFORM_VARS
175 # ----------------------------------
176 # Set the Haskell platform variables
177 AC_DEFUN([FPTOOLS_SET_HASKELL_PLATFORM_VARS],
178 [
179     checkArch() {
180         case [$]1 in
181         i386)
182             test -z "[$]2" || eval "[$]2=ArchX86"
183             ;;
184         x86_64|amd64)
185             test -z "[$]2" || eval "[$]2=ArchX86_64"
186             ;;
187         powerpc)
188             test -z "[$]2" || eval "[$]2=ArchPPC"
189             ;;
190         powerpc64)
191             test -z "[$]2" || eval "[$]2=\"ArchPPC_64 {ppc_64ABI = ELF_V1}\""
192             ;;
193         powerpc64le)
194             test -z "[$]2" || eval "[$]2=\"ArchPPC_64 {ppc_64ABI = ELF_V2}\""
195             ;;
196         sparc)
197             test -z "[$]2" || eval "[$]2=ArchSPARC"
198             ;;
199         sparc64)
200             test -z "[$]2" || eval "[$]2=ArchSPARC64"
201             ;;
202         arm)
203             GET_ARM_ISA()
204             test -z "[$]2" || eval "[$]2=\"ArchARM {armISA = \$ARM_ISA, armISAExt = \$ARM_ISA_EXT, armABI = \$ARM_ABI}\""
205             ;;
206         aarch64)
207             test -z "[$]2" || eval "[$]2=ArchARM64"
208             ;;
209         alpha)
210             test -z "[$]2" || eval "[$]2=ArchAlpha"
211             ;;
212         mips|mipseb)
213             test -z "[$]2" || eval "[$]2=ArchMipseb"
214             ;;
215         mipsel)
216             test -z "[$]2" || eval "[$]2=ArchMipsel"
217             ;;
218         hppa|hppa1_1|ia64|m68k|rs6000|s390|s390x|sh4|vax)
219             test -z "[$]2" || eval "[$]2=ArchUnknown"
220             ;;
221         *)
222             echo "Unknown arch [$]1"
223             exit 1
224             ;;
225         esac
226     }
227
228     checkVendor() {
229         case [$]1 in
230         dec|none|unknown|hp|apple|next|sun|sgi|ibm|montavista|portbld)
231             ;;
232         *)
233             AC_MSG_WARN([Unknown vendor [$]1])
234             ;;
235         esac
236     }
237
238     checkOS() {
239         case [$]1 in
240         linux)
241             test -z "[$]2" || eval "[$]2=OSLinux"
242             ;;
243         ios)
244             test -z "[$]2" || eval "[$]2=OSiOS"
245             ;;
246         darwin)
247             test -z "[$]2" || eval "[$]2=OSDarwin"
248             ;;
249         solaris2)
250             test -z "[$]2" || eval "[$]2=OSSolaris2"
251             ;;
252         mingw32)
253             test -z "[$]2" || eval "[$]2=OSMinGW32"
254             ;;
255         freebsd)
256             test -z "[$]2" || eval "[$]2=OSFreeBSD"
257             ;;
258         dragonfly)
259             test -z "[$]2" || eval "[$]2=OSDragonFly"
260             ;;
261         kfreebsdgnu)
262             test -z "[$]2" || eval "[$]2=OSKFreeBSD"
263             ;;
264         openbsd)
265             test -z "[$]2" || eval "[$]2=OSOpenBSD"
266             ;;
267         netbsd)
268             test -z "[$]2" || eval "[$]2=OSNetBSD"
269             ;;
270         haiku)
271             test -z "[$]2" || eval "[$]2=OSHaiku"
272             ;;
273         nto-qnx)
274             test -z "[$]2" || eval "[$]2=OSQNXNTO"
275             ;;
276         dragonfly|hpux|linuxaout|freebsd2|gnu|nextstep2|nextstep3|sunos4|ultrix)
277             test -z "[$]2" || eval "[$]2=OSUnknown"
278             ;;
279         aix)
280             test -z "[$]2" || eval "[$]2=OSAIX"
281             ;;
282         linux-android)
283             test -z "[$]2" || eval "[$]2=OSAndroid"
284             ;;
285         *)
286             echo "Unknown OS '[$]1'"
287             exit 1
288             ;;
289         esac
290     }
291
292     dnl ** check for Apple-style dead-stripping support
293     dnl    (.subsections-via-symbols assembler directive)
294
295     AC_MSG_CHECKING(for .subsections_via_symbols)
296     AC_COMPILE_IFELSE(
297         [AC_LANG_PROGRAM([], [__asm__ (".subsections_via_symbols");])],
298         [AC_MSG_RESULT(yes)
299          HaskellHaveSubsectionsViaSymbols=True
300          AC_DEFINE([HAVE_SUBSECTIONS_VIA_SYMBOLS],[1],
301                    [Define to 1 if Apple-style dead-stripping is supported.])
302         ],
303         [HaskellHaveSubsectionsViaSymbols=False
304          AC_MSG_RESULT(no)])
305
306     dnl ** check for .ident assembler directive
307
308     AC_MSG_CHECKING(whether your assembler supports .ident directive)
309     AC_COMPILE_IFELSE(
310         [AC_LANG_SOURCE([__asm__ (".ident \"GHC x.y.z\"");])],
311         [AC_MSG_RESULT(yes)
312          HaskellHaveIdentDirective=True],
313         [AC_MSG_RESULT(no)
314          HaskellHaveIdentDirective=False])
315
316     dnl *** check for GNU non-executable stack note support (ELF only)
317     dnl     (.section .note.GNU-stack,"",@progbits)
318
319     dnl This test doesn't work with "gcc -g" in gcc 4.4 (GHC trac #3889:
320     dnl     Error: can't resolve `.note.GNU-stack' {.note.GNU-stack section} - `.Ltext0' {.text section}
321     dnl so we empty CFLAGS while running this test
322     CFLAGS2="$CFLAGS"
323     CFLAGS=
324     case $TargetArch in
325       arm)
326         dnl See #13937.
327         progbits="%progbits"
328         ;;
329       *)
330         progbits="@progbits"
331         ;;
332     esac
333     AC_MSG_CHECKING(for GNU non-executable stack support)
334     AC_COMPILE_IFELSE(
335         [AC_LANG_PROGRAM([__asm__ (".section .note.GNU-stack,\"\",$progbits");], [0])],
336         [AC_MSG_RESULT(yes)
337          HaskellHaveGnuNonexecStack=True],
338         [AC_MSG_RESULT(no)
339          HaskellHaveGnuNonexecStack=False])
340     CFLAGS="$CFLAGS2"
341
342     checkArch "$BuildArch" "HaskellBuildArch"
343     checkVendor "$BuildVendor"
344     checkOS "$BuildOS" ""
345
346     checkArch "$HostArch" "HaskellHostArch"
347     checkVendor "$HostVendor"
348     checkOS "$HostOS" ""
349
350     checkArch "$TargetArch" "HaskellTargetArch"
351     checkVendor "$TargetVendor"
352     checkOS "$TargetOS" "HaskellTargetOs"
353
354     AC_SUBST(HaskellTargetArch)
355     AC_SUBST(HaskellTargetOs)
356     AC_SUBST(HaskellHaveSubsectionsViaSymbols)
357     AC_SUBST(HaskellHaveIdentDirective)
358     AC_SUBST(HaskellHaveGnuNonexecStack)
359 ])
360
361
362 # GET_ARM_ISA
363 # ----------------------------------
364 # Get info about the ISA on the ARM arch
365 AC_DEFUN([GET_ARM_ISA],
366 [
367     AC_COMPILE_IFELSE([
368         AC_LANG_PROGRAM(
369             [],
370             [#if defined(__ARM_ARCH_2__)  || \
371                  defined(__ARM_ARCH_3__)  || \
372                  defined(__ARM_ARCH_3M__) || \
373                  defined(__ARM_ARCH_4__)  || \
374                  defined(__ARM_ARCH_4T__) || \
375                  defined(__ARM_ARCH_5__)  || \
376                  defined(__ARM_ARCH_5T__) || \
377                  defined(__ARM_ARCH_5E__) || \
378                  defined(__ARM_ARCH_5TE__)
379                  return 0;
380              #else
381                  not pre arm v6
382              #endif]
383         )],
384         [AC_DEFINE(arm_HOST_ARCH_PRE_ARMv6, 1, [ARM pre v6])
385          AC_DEFINE(arm_HOST_ARCH_PRE_ARMv7, 1, [ARM pre v7])
386          changequote(, )dnl
387          ARM_ISA=ARMv5
388          ARM_ISA_EXT="[]"
389          changequote([, ])dnl
390         ],
391         [
392             AC_COMPILE_IFELSE([
393                 AC_LANG_PROGRAM(
394                     [],
395                     [#if defined(__ARM_ARCH_6__)   || \
396                          defined(__ARM_ARCH_6J__)  || \
397                          defined(__ARM_ARCH_6T2__) || \
398                          defined(__ARM_ARCH_6Z__)  || \
399                          defined(__ARM_ARCH_6ZK__) || \
400                          defined(__ARM_ARCH_6K__)  || \
401                          defined(__ARM_ARCH_6KZ__) || \
402                          defined(__ARM_ARCH_6M__)
403                          return 0;
404                      #else
405                          not pre arm v7
406                      #endif]
407                 )],
408                 [AC_DEFINE(arm_HOST_ARCH_PRE_ARMv7, 1, [ARM pre v7])
409                  ARM_ISA=ARMv6
410                  AC_COMPILE_IFELSE([
411                         AC_LANG_PROGRAM(
412                                 [],
413                                 [#if defined(__VFP_FP__)
414                                      return 0;
415                                 #else
416                                      no vfp
417                                 #endif]
418                         )],
419                         [changequote(, )dnl
420                          ARM_ISA_EXT="[VFPv2]"
421                          changequote([, ])dnl
422                         ],
423                         [changequote(, )dnl
424                          ARM_ISA_EXT="[]"
425                          changequote([, ])dnl
426                         ]
427                 )],
428                 [changequote(, )dnl
429                  ARM_ISA=ARMv7
430                  ARM_ISA_EXT="[VFPv3,NEON]"
431                  changequote([, ])dnl
432                 ])
433         ])
434
435         AC_COMPILE_IFELSE(
436                [AC_LANG_PROGRAM(
437                        [],
438                        [#if defined(__SOFTFP__)
439                             return 0;
440                        #else
441                             not softfp
442                        #endif]
443                )],
444                [changequote(, )dnl
445                 ARM_ABI="SOFT"
446                 changequote([, ])dnl
447                ],
448                [AC_COMPILE_IFELSE(
449                     [AC_LANG_PROGRAM(
450                        [],
451                        [#if defined(__ARM_PCS_VFP)
452                             return 0;
453                        #else
454                             no hard float ABI
455                        #endif]
456                     )],
457                     [ARM_ABI="HARD"],
458                     [ARM_ABI="SOFTFP"]
459                )]
460         )
461
462         AC_SUBST(ARM_ISA)
463 ])
464
465
466 # FP_SETTINGS
467 # ----------------------------------
468 # Set the variables used in the settings file
469 AC_DEFUN([FP_SETTINGS],
470 [
471     if test "$windows" = YES -a "$EnableDistroToolchain" = "NO"
472     then
473         mingw_bin_prefix=mingw/bin/
474         SettingsCCompilerCommand="\$topdir/../${mingw_bin_prefix}gcc.exe"
475         SettingsHaskellCPPCommand="\$topdir/../${mingw_bin_prefix}gcc.exe"
476         SettingsHaskellCPPFlags="$HaskellCPPArgs"
477         SettingsLdCommand="\$topdir/../${mingw_bin_prefix}ld.exe"
478         SettingsArCommand="\$topdir/../${mingw_bin_prefix}ar.exe"
479         SettingsPerlCommand='$topdir/../perl/perl.exe'
480         SettingsDllWrapCommand="\$topdir/../${mingw_bin_prefix}dllwrap.exe"
481         SettingsWindresCommand="\$topdir/../${mingw_bin_prefix}windres.exe"
482         SettingsTouchCommand='$topdir/bin/touchy.exe'
483     elif test "$EnableDistroToolchain" = "YES"
484     then
485         SettingsCCompilerCommand="$(basename $CC)"
486         SettingsCCompilerFlags="$CONF_CC_OPTS_STAGE2"
487         SettingsHaskellCPPCommand="$(basename $HaskellCPPCmd)"
488         SettingsHaskellCPPFlags="$HaskellCPPArgs"
489         SettingsLdCommand="$(basename $LdCmd)"
490         SettingsArCommand="$(basename $ArCmd)"
491         SettingsPerlCommand="$(basename $PerlCmd)"
492         SettingsDllWrapCommand="$(basename $DllWrapCmd)"
493         SettingsWindresCommand="$(basename $WindresCmd)"
494         SettingsTouchCommand='$topdir/bin/touchy.exe'
495     else
496         SettingsCCompilerCommand="$CC"
497         SettingsHaskellCPPCommand="$HaskellCPPCmd"
498         SettingsHaskellCPPFlags="$HaskellCPPArgs"
499         SettingsLdCommand="$LdCmd"
500         SettingsArCommand="$ArCmd"
501         SettingsPerlCommand="$PerlCmd"
502         if test -z "$DllWrapCmd"
503         then
504             SettingsDllWrapCommand="/bin/false"
505         else
506             SettingsDllWrapCommand="$DllWrapCmd"
507         fi
508         if test -z "$WindresCmd"
509         then
510             SettingsWindresCommand="/bin/false"
511         else
512             SettingsWindresCommand="$WindresCmd"
513         fi
514        SettingsTouchCommand='touch'
515     fi
516     if test -z "$LibtoolCmd"
517     then
518       SettingsLibtoolCommand="libtool"
519     else
520       SettingsLibtoolCommand="$LibtoolCmd"
521     fi
522     if test -z "$LlcCmd"
523     then
524       SettingsLlcCommand="llc"
525     else
526       SettingsLlcCommand="$LlcCmd"
527     fi
528     if test -z "$OptCmd"
529     then
530       SettingsOptCommand="opt"
531     else
532       SettingsOptCommand="$OptCmd"
533     fi
534     SettingsCCompilerFlags="$CONF_CC_OPTS_STAGE2"
535     SettingsCCompilerLinkFlags="$CONF_GCC_LINKER_OPTS_STAGE2"
536     SettingsCCompilerSupportsNoPie="$CONF_GCC_SUPPORTS_NO_PIE"
537     SettingsLdFlags="$CONF_LD_LINKER_OPTS_STAGE2"
538     AC_SUBST(SettingsCCompilerCommand)
539     AC_SUBST(SettingsHaskellCPPCommand)
540     AC_SUBST(SettingsHaskellCPPFlags)
541     AC_SUBST(SettingsCCompilerFlags)
542     AC_SUBST(SettingsCCompilerLinkFlags)
543     AC_SUBST(SettingsCCompilerSupportsNoPie)
544     AC_SUBST(SettingsLdCommand)
545     AC_SUBST(SettingsLdFlags)
546     AC_SUBST(SettingsArCommand)
547     AC_SUBST(SettingsPerlCommand)
548     AC_SUBST(SettingsDllWrapCommand)
549     AC_SUBST(SettingsWindresCommand)
550     AC_SUBST(SettingsLibtoolCommand)
551     AC_SUBST(SettingsTouchCommand)
552     AC_SUBST(SettingsLlcCommand)
553     AC_SUBST(SettingsOptCommand)
554 ])
555
556 # Helper for cloning a shell variable's state
557 AC_DEFUN([FP_COPY_SHELLVAR],
558 [if test -n "${$1+set}"; then $2="$$1"; else unset $2; fi ])
559
560 # FP_SET_CFLAGS_C99
561 # ----------------------------------
562 # figure out which CFLAGS are needed to place the compiler into C99 mode
563 # $1 is name of CC variable (unmodified)
564 # $2 is name of CC flags variable (augmented if needed)
565 # $3 is name of CPP flags variable (augmented if needed)
566 AC_DEFUN([FP_SET_CFLAGS_C99],
567 [
568     dnl save current state of AC_PROG_CC_C99
569     FP_COPY_SHELLVAR([CC],[fp_save_CC])
570     FP_COPY_SHELLVAR([CFLAGS],[fp_save_CFLAGS])
571     FP_COPY_SHELLVAR([CPPFLAGS],[fp_save_CPPFLAGS])
572     FP_COPY_SHELLVAR([ac_cv_prog_cc_c99],[fp_save_cc_c99])
573     dnl set local state
574     CC="$$1"
575     CFLAGS="$$2"
576     CPPFLAGS="$$3"
577     unset ac_cv_prog_cc_c99
578     dnl perform detection
579     _AC_PROG_CC_C99
580     fp_cc_c99="$ac_cv_prog_cc_c99"
581     case "x$ac_cv_prog_cc_c99" in
582       x)   ;; # noop
583       xno) AC_MSG_ERROR([C99-compatible compiler needed]) ;;
584       *)   $2="$$2 $ac_cv_prog_cc_c99"
585            $3="$$3 $ac_cv_prog_cc_c99"
586            ;;
587     esac
588     dnl restore saved state
589     FP_COPY_SHELLVAR([fp_save_CC],[CC])
590     FP_COPY_SHELLVAR([fp_save_CFLAGS],[CFLAGS])
591     FP_COPY_SHELLVAR([fp_save_CPPFLAGS],[CPPFLAGS])
592     FP_COPY_SHELLVAR([fp_save_cc_c99],[ac_cv_prog_cc_c99])
593     dnl cleanup
594     unset fp_save_CC
595     unset fp_save_CFLAGS
596     unset fp_save_cc_c99
597 ])
598
599 # FPTOOLS_SET_C_LD_FLAGS
600 # ----------------------------------
601 # Set the C, LD and CPP flags for a given platform.
602 # $1 is the platform
603 # $2 is the name of the CC flags variable
604 # $3 is the name of the linker flags variable when linking with gcc
605 # $4 is the name of the linker flags variable when linking with ld
606 # $5 is the name of the CPP flags variable
607 AC_DEFUN([FPTOOLS_SET_C_LD_FLAGS],
608 [
609     AC_MSG_CHECKING([Setting up $2, $3, $4 and $5])
610     case $$1 in
611     i386-*)
612         # Workaround for #7799
613         $2="$$2 -U__i686"
614         ;;
615     esac
616
617     case $$1 in
618     i386-unknown-mingw32)
619         $2="$$2 -march=i686"
620         ;;
621     i386-portbld-freebsd*)
622         $2="$$2 -march=i686"
623         ;;
624     x86_64-unknown-solaris2)
625         $2="$$2 -m64"
626         $3="$$3 -m64"
627         $4="$$4 -m64"
628         $5="$$5 -m64"
629         ;;
630     alpha-*)
631         # For now, to suppress the gcc warning "call-clobbered
632         # register used for global register variable", we simply
633         # disable all warnings altogether using the -w flag. Oh well.
634         $2="$$2 -w -mieee -D_REENTRANT"
635         $3="$$3 -w -mieee -D_REENTRANT"
636         $5="$$5 -w -mieee -D_REENTRANT"
637         ;;
638     hppa*)
639         # ___HPUX_SOURCE, not _HPUX_SOURCE, is #defined if -ansi!
640         # (very nice, but too bad the HP /usr/include files don't agree.)
641         $2="$$2 -D_HPUX_SOURCE"
642         $3="$$3 -D_HPUX_SOURCE"
643         $5="$$5 -D_HPUX_SOURCE"
644         ;;
645     arm*linux*)
646         # On arm/linux and arm/android, tell gcc to generate Arm
647         # instructions (ie not Thumb).
648         $2="$$2 -marm"
649         $3="$$3 -Wl,-z,noexecstack"
650         $4="$$4 -z noexecstack"
651         ;;
652
653     aarch64*linux*)
654         $3="$$3 -Wl,-z,noexecstack"
655         $4="$$4 -z noexecstack"
656         ;;
657
658     powerpc-ibm-aix*)
659         # We need `-D_THREAD_SAFE` to unlock the thread-local `errno`.
660         $2="$$2 -D_THREAD_SAFE"
661         $3="$$3 -D_THREAD_SAFE -Wl,-bnotextro"
662         $4="$$4 -bnotextro"
663         $5="$$5 -D_THREAD_SAFE"
664         ;;
665
666     x86_64-*-openbsd*)
667         # We need -z wxneeded at least to link ghc-stage2 to workaround
668         # W^X issue in GHCi on OpenBSD current (as of Aug 2016)
669         $3="$$3 -Wl,-z,wxneeded"
670         $4="$$4 -z wxneeded"
671         ;;
672
673     esac
674
675     # If gcc knows about the stack protector, turn it off.
676     # Otherwise the stack-smash handler gets triggered.
677     echo 'int main(void) {return 0;}' > conftest.c
678     if $CC -c conftest.c -fno-stack-protector > /dev/null 2>&1
679     then
680         $2="$$2 -fno-stack-protector"
681     fi
682
683     rm -f conftest.c conftest.o
684     AC_MSG_RESULT([done])
685 ])
686
687
688 # FP_VISIBILITY_HIDDEN
689 # ----------------------------------
690 # Is the visibility hidden attribute supported?
691 AC_DEFUN([FP_VISIBILITY_HIDDEN],
692 [
693     AC_MSG_CHECKING([whether __attribute__((visibility("hidden"))) is supported])
694     echo '__attribute__((visibility("hidden"))) void foo(void) {}' > conftest.c
695     if $CC -Wall -Werror -c conftest.c > /dev/null 2>&1
696     then
697         AC_MSG_RESULT([yes])
698         AC_DEFINE(HAS_VISIBILITY_HIDDEN, 1, [Has visibility hidden])
699     else
700         AC_MSG_RESULT([no])
701     fi
702     rm -f conftest.c conftest.o
703 ])
704
705
706 # FPTOOLS_FLOAT_WORD_ORDER_BIGENDIAN
707 # ----------------------------------
708 # Little endian ARM on Linux with some ABIs has big endian word order
709 # in doubles. Define FLOAT_WORDS_BIGENDIAN if this is the case.
710 AC_DEFUN([FPTOOLS_FLOAT_WORD_ORDER_BIGENDIAN],
711   [AC_CACHE_CHECK([whether float word order is big endian], [fptools_cv_float_word_order_bigendian],
712     [AC_COMPILE_IFELSE(
713       [AC_LANG_PROGRAM(
714         [#include <endian.h>],
715         [#if defined(__FLOAT_WORD_ORDER) && __FLOAT_WORD_ORDER == BIG_ENDIAN
716              return 0;
717          #else
718              not float word order big endian
719          #endif]
720       )],
721       [fptools_cv_float_word_order_bigendian=yes],
722       [fptools_cv_float_word_order_bigendian=no])
723     ])
724   case $fptools_cv_float_word_order_bigendian in
725       yes)
726           AC_DEFINE([FLOAT_WORDS_BIGENDIAN], 1,
727           [Define to 1 if your processor stores words of floats with
728            the most significant byte first]) ;;
729   esac
730 ])
731
732
733 # FP_PROG_CONTEXT_DIFF
734 # --------------------
735 # Figure out how to do context diffs. Sets the output variable ContextDiffCmd.
736 #
737 # Note: NeXTStep thinks diff'ing a file against itself is "trouble".
738 AC_DEFUN([FP_PROG_CONTEXT_DIFF],
739 [AC_CACHE_CHECK([for a working context diff], [fp_cv_context_diff],
740 [echo foo > conftest1
741 echo foo > conftest2
742 fp_cv_context_diff=no
743 for fp_var in '-U 1' '-u1' '-C 1' '-c1'
744 do
745   if diff $fp_var conftest1 conftest2 > /dev/null 2>&1; then
746     fp_cv_context_diff="diff $fp_var"
747     break
748   fi
749 done])
750 if test x"$fp_cv_context_diff" = xno; then
751    AC_MSG_ERROR([cannot figure out how to do context diffs])
752 fi
753 AC_SUBST(ContextDiffCmd, [$fp_cv_context_diff])
754 ])# FP_PROG_CONTEXT_DIFF
755
756
757 # FP_COMPUTE_INT(EXPRESSION, VARIABLE, INCLUDES, IF-FAILS)
758 # --------------------------------------------------------
759 # Assign VARIABLE the value of the compile-time EXPRESSION using INCLUDES for
760 # compilation. Execute IF-FAILS when unable to determine the value. Works for
761 # cross-compilation, too.
762 #
763 # Implementation note: We are lazy and use an internal autoconf macro, but it
764 # is supported in autoconf versions 2.50 up to the actual 2.57, so there is
765 # little risk.
766 AC_DEFUN([FP_COMPUTE_INT],
767 [_AC_COMPUTE_INT([$1], [$2], [$3], [$4])[]dnl
768 ])# FP_COMPUTE_INT
769
770
771 # FP_CHECK_ALIGNMENT(TYPE, [IGNORED], [INCLUDES = DEFAULT-INCLUDES])
772 # ------------------------------------------------------------------
773 # A variation of AC_CHECK_SIZEOF for computing the alignment restrictions of a
774 # given type. Defines ALIGNMENT_TYPE.
775 AC_DEFUN([FP_CHECK_ALIGNMENT],
776 [AS_LITERAL_IF(m4_translit([[$1]], [*], [p]), [],
777                [AC_FATAL([$0: requires literal arguments])])[]dnl
778 AC_CHECK_TYPE([$1], [], [], [$3])[]dnl
779 m4_pushdef([fp_Cache], [AS_TR_SH([fp_cv_alignment_$1])])[]dnl
780 AC_CACHE_CHECK([alignment of $1], [fp_Cache],
781 [if test "$AS_TR_SH([ac_cv_type_$1])" = yes; then
782   FP_COMPUTE_INT([offsetof(struct { char c; $1 ty; },ty)],
783                  [fp_Cache],
784                  [AC_INCLUDES_DEFAULT([$3])],
785                  [AC_MSG_ERROR([cannot compute alignment ($1)
786 See `config.log' for more details.], [77])])
787 else
788   fp_Cache=0
789 fi])[]dnl
790 AC_DEFINE_UNQUOTED(AS_TR_CPP(alignment_$1), $fp_Cache, [The alignment of a `$1'.])[]dnl
791 m4_popdef([fp_Cache])[]dnl
792 ])# FP_CHECK_ALIGNMENT
793
794
795
796 # FP_CHECK_SIZEOF_AND_ALIGNMENT(TYPE)
797 # ------------------------------------------------------------------
798 # Combines AC_CHECK_SIZEOF and FP_CHECK_ALIGNMENT.
799 AC_DEFUN([FP_CHECK_SIZEOF_AND_ALIGNMENT],
800 [AC_CHECK_SIZEOF([$1])
801 FP_CHECK_ALIGNMENT([$1])
802 ])# FP_CHECK_SIZEOF_AND_ALIGNMENT
803
804
805 # FP_LEADING_UNDERSCORE
806 # ---------------------
807 # Test for determining whether symbol names have a leading underscore. We assume
808 # that they _haven't_ if anything goes wrong. Sets the output variable
809 # LeadingUnderscore to YES or NO and defines LEADING_UNDERSCORE correspondingly.
810 #
811 # Some nlist implementations seem to try to be compatible by ignoring a leading
812 # underscore sometimes (eg. FreeBSD). We therefore have to work around this by
813 # checking for *no* leading underscore first. Sigh.  --SDM
814 #
815 # Similarly on OpenBSD, but this test doesn't help. -- dons
816 #
817 AC_DEFUN([FP_LEADING_UNDERSCORE],
818 [AC_CHECK_LIB([elf], [nlist], [LIBS="-lelf $LIBS"])
819 AC_CACHE_CHECK([leading underscore in symbol names], [fptools_cv_leading_underscore], [
820 # Hack!: nlist() under Digital UNIX insist on there being an _,
821 # but symbol table listings shows none. What is going on here?!?
822 case $TargetPlatform in
823     # Apples mach-o platforms use leading underscores
824     *-apple-*) fptools_cv_leading_underscore=yes;;
825     *linux-android*) fptools_cv_leading_underscore=no;;
826     *openbsd*) # x86 openbsd is ELF from 3.4 >, meaning no leading uscore
827       case $build in
828         i386-*2\.@<:@0-9@:>@ | i386-*3\.@<:@0-3@:>@ ) fptools_cv_leading_underscore=yes ;;
829         *) fptools_cv_leading_underscore=no ;;
830       esac ;;
831     i386-unknown-mingw32) fptools_cv_leading_underscore=yes;;
832     x86_64-unknown-mingw32) fptools_cv_leading_underscore=no;;
833     *) AC_RUN_IFELSE([AC_LANG_SOURCE([[#ifdef HAVE_NLIST_H
834 #include <nlist.h>
835 struct nlist xYzzY1[] = {{"xYzzY1", 0},{0}};
836 struct nlist xYzzY2[] = {{"_xYzzY2", 0},{0}};
837 #endif
838
839 int main(argc, argv)
840 int argc;
841 char **argv;
842 {
843 #ifdef HAVE_NLIST_H
844     if(nlist(argv[0], xYzzY1) == 0 && xYzzY1[0].n_value != 0)
845         exit(1);
846     if(nlist(argv[0], xYzzY2) == 0 && xYzzY2[0].n_value != 0)
847         exit(0);
848 #endif
849     exit(1);
850 }]])],[fptools_cv_leading_underscore=yes],[fptools_cv_leading_underscore=no],[fptools_cv_leading_underscore=no])
851 ;;
852 esac]);
853 AC_SUBST([LeadingUnderscore], [`echo $fptools_cv_leading_underscore | sed 'y/yesno/YESNO/'`])
854 if test x"$fptools_cv_leading_underscore" = xyes; then
855    AC_DEFINE([LEADING_UNDERSCORE], [1], [Define to 1 if C symbols have a leading underscore added by the compiler.])
856 fi])# FP_LEADING_UNDERSCORE
857
858
859 # FP_COMPARE_VERSIONS(VERSION1, TEST, VERSION2, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
860 # ----------------------------------------------------------------------------------
861 # Compare dotted version numbers VERSION1 and VERSION2 lexicographically according
862 # to TEST (one of -eq, -ne, -lt, -le, -gt, or -ge).
863 AC_DEFUN([FP_COMPARE_VERSIONS],
864 [fp_version1=$1; fp_version2=$3
865 fp_save_IFS=$IFS; IFS='.'
866 while test x"$fp_version1" != x || test x"$fp_version2" != x
867 do
868
869   set dummy $fp_version1; shift
870   fp_num1=""
871   test $[@%:@] = 0 || { fp_num1="[$]1"; shift; }
872   test x"$fp_num1" = x && fp_num1="0"
873   fp_version1="[$]*"
874
875   set dummy $fp_version2; shift
876   fp_num2=""
877   test $[@%:@] = 0 || { fp_num2="[$]1"; shift; }
878   test x"$fp_num2" = x && fp_num2="0"
879   fp_version2="[$]*"
880
881   test "$fp_num1" = "$fp_num2" || break;
882 done
883 IFS=$fp_save_IFS
884 AS_IF([test "$fp_num1" $2 "$fp_num2"], [$4], [$5])[]dnl
885 ])# FP_COMPARE_VERSIONS
886
887
888 dnl
889 dnl Check for Happy and version.
890 dnl If there's no installed Happy, we look
891 dnl for a happy source tree and point the build system at that instead.
892 dnl If you increase the minimum version requirement, please also update:
893 dnl https://ghc.haskell.org/trac/ghc/wiki/Building/Preparation/Tools
894 dnl
895 AC_DEFUN([FPTOOLS_HAPPY],
896 [AC_PATH_PROG(HappyCmd,happy,)
897
898 AC_CACHE_CHECK([for version of happy], fptools_cv_happy_version,
899 changequote(, )dnl
900 [if test x"$HappyCmd" != x; then
901    fptools_cv_happy_version=`"$HappyCmd" -v |
902               grep 'Happy Version' | sed -e 's/Happy Version \([^ ]*\).*/\1/g'` ;
903 else
904    fptools_cv_happy_version="";
905 fi;
906 changequote([, ])dnl
907 ])
908 if test ! -f compiler/parser/Parser.hs || test ! -f compiler/cmm/CmmParse.hs
909 then
910     FP_COMPARE_VERSIONS([$fptools_cv_happy_version],[-lt],[1.19.4],
911       [AC_MSG_ERROR([Happy version 1.19.4 or later is required to compile GHC.])])[]
912 fi
913 HappyVersion=$fptools_cv_happy_version;
914 AC_SUBST(HappyVersion)
915 ])
916
917 dnl
918 dnl Check for Alex and version.
919 dnl If you increase the minimum version requirement, please also update:
920 dnl https://ghc.haskell.org/trac/ghc/wiki/Building/Preparation/Tools
921 dnl
922 AC_DEFUN([FPTOOLS_ALEX],
923 [
924 AC_PATH_PROG(AlexCmd,alex,)
925
926 AC_CACHE_CHECK([for version of alex], fptools_cv_alex_version,
927 changequote(, )dnl
928 [if test x"$AlexCmd" != x; then
929    fptools_cv_alex_version=`"$AlexCmd" -v |
930               grep 'Alex [Vv]ersion' | sed -e 's/Alex [Vv]ersion \([0-9\.]*\).*/\1/g'` ;
931 else
932    fptools_cv_alex_version="";
933 fi;
934 changequote([, ])dnl
935 ])
936 FP_COMPARE_VERSIONS([$fptools_cv_alex_version],[-ge],[3.0],
937   [Alex3=YES],[Alex3=NO])
938 if test ! -f compiler/cmm/CmmLex.hs || test ! -f compiler/parser/Lexer.hs
939 then
940     FP_COMPARE_VERSIONS([$fptools_cv_alex_version],[-lt],[3.1.0],
941       [AC_MSG_ERROR([Alex version 3.1.0 or later is required to compile GHC.])])[]
942 fi
943 AlexVersion=$fptools_cv_alex_version;
944 AC_SUBST(AlexVersion)
945 AC_SUBST(Alex3)
946 ])
947
948
949 # FP_PROG_LD_FLAG
950 # ---------------
951 # Sets the output variable $2 to $1 if ld supports the $1 flag.
952 # Otherwise the variable's value is empty.
953 AC_DEFUN([FP_PROG_LD_FLAG],
954 [
955 AC_CACHE_CHECK([whether ld understands $1], [fp_cv_$2],
956 [echo 'int foo() { return 0; }' > conftest.c
957 ${CC-cc} -c conftest.c
958 if ${LdCmd} -r $1 -o conftest2.o conftest.o > /dev/null 2>&1; then
959    fp_cv_$2=$1
960 else
961    fp_cv_$2=
962 fi
963 rm -rf conftest*])
964 $2=$fp_cv_$2
965 ])# FP_PROG_LD_FLAG
966
967
968 # FP_PROG_LD_BUILD_ID
969 # ------------
970
971 # Sets the output variable LdHasBuildId to YES if ld supports
972 # --build-id, or NO otherwise.
973 AC_DEFUN([FP_PROG_LD_BUILD_ID],
974 [
975 AC_CACHE_CHECK([whether ld understands --build-id], [fp_cv_ld_build_id],
976 [echo 'int foo() { return 0; }' > conftest.c
977 ${CC-cc} -c conftest.c
978 if ${LdCmd} -r --build-id=none -o conftest2.o conftest.o > /dev/null 2>&1; then
979    fp_cv_ld_build_id=yes
980 else
981    fp_cv_ld_build_id=no
982 fi
983 rm -rf conftest*])
984 if test "$fp_cv_ld_build_id" = yes; then
985   LdHasBuildId=YES
986 else
987   LdHasBuildId=NO
988 fi
989 AC_SUBST([LdHasBuildId])
990 ])# FP_PROG_LD_BUILD_ID
991
992
993 # FP_PROG_LD_IS_GNU
994 # -----------------
995 # Sets the output variable LdIsGNULd to YES or NO, depending on whether it is
996 # GNU ld or not.
997 AC_DEFUN([FP_PROG_LD_IS_GNU],[
998 AC_CACHE_CHECK([whether ld is GNU ld], [fp_cv_gnu_ld],
999 [[if ${LdCmd} --version 2> /dev/null | grep "GNU" > /dev/null 2>&1; then
1000   fp_cv_gnu_ld=YES
1001 else
1002   fp_cv_gnu_ld=NO
1003 fi]])
1004 AC_SUBST([LdIsGNULd],["$fp_cv_gnu_ld"])
1005 ])# FP_PROG_LD_IS_GNU
1006
1007
1008 # FP_PROG_LD_NO_COMPACT_UNWIND
1009 # ----------------------------
1010
1011 # Sets the output variable LdHasNoCompactUnwind to YES if ld supports
1012 # -no_compact_unwind, or NO otherwise.
1013 AC_DEFUN([FP_PROG_LD_NO_COMPACT_UNWIND],
1014 [
1015 AC_CACHE_CHECK([whether ld understands -no_compact_unwind], [fp_cv_ld_no_compact_unwind],
1016 [echo 'int foo() { return 0; }' > conftest.c
1017 ${CC-cc} -c conftest.c
1018 if ${LdCmd} -r -no_compact_unwind -o conftest2.o conftest.o > /dev/null 2>&1; then
1019    fp_cv_ld_no_compact_unwind=yes
1020 else
1021    fp_cv_ld_no_compact_unwind=no
1022 fi
1023 rm -rf conftest*])
1024 if test "$fp_cv_ld_no_compact_unwind" = yes; then
1025   LdHasNoCompactUnwind=YES
1026 else
1027   LdHasNoCompactUnwind=NO
1028 fi
1029 AC_SUBST([LdHasNoCompactUnwind])
1030 ])# FP_PROG_LD_NO_COMPACT_UNWIND
1031
1032
1033 # FP_PROG_LD_FILELIST
1034 # -------------------
1035
1036 # Sets the output variable LdHasFilelist to YES if ld supports
1037 # -filelist, or NO otherwise.
1038 AC_DEFUN([FP_PROG_LD_FILELIST],
1039 [
1040 AC_CACHE_CHECK([whether ld understands -filelist], [fp_cv_ld_has_filelist],
1041 [
1042     echo 'int foo() { return 0; }' > conftest1.c
1043     echo 'int bar() { return 0; }' > conftest2.c
1044     ${CC-cc} -c conftest1.c
1045     ${CC-cc} -c conftest2.c
1046     echo conftest1.o  > conftest.o-files
1047     echo conftest2.o >> conftest.o-files
1048     if ${LdCmd} -r -filelist conftest.o-files -o conftest.o > /dev/null 2>&1
1049     then
1050         fp_cv_ld_has_filelist=yes
1051     else
1052         fp_cv_ld_has_filelist=no
1053     fi
1054     rm -rf conftest*
1055 ])
1056 if test "$fp_cv_ld_has_filelist" = yes; then
1057     LdHasFilelist=YES
1058 else
1059     LdHasFilelist=NO
1060 fi
1061 AC_SUBST([LdHasFilelist])
1062 ])# FP_PROG_LD_FILELIST
1063
1064
1065 # FP_PROG_AR
1066 # ----------
1067 # Sets fp_prog_ar to a path to ar. Exits if no ar can be found
1068 AC_DEFUN([FP_PROG_AR],
1069 [if test -z "$fp_prog_ar"; then
1070   AC_PATH_PROG([fp_prog_ar], [ar])
1071 fi
1072 if test -z "$fp_prog_ar"; then
1073   AC_MSG_ERROR([cannot find ar in your PATH, no idea how to make a library])
1074 fi
1075 ])# FP_PROG_AR
1076
1077
1078 # FP_PROG_AR_IS_GNU
1079 # -----------------
1080 # Sets fp_prog_ar_is_gnu to yes or no, depending on whether it is GNU ar or not.
1081 AC_DEFUN([FP_PROG_AR_IS_GNU],
1082 [AC_REQUIRE([FP_PROG_AR])
1083 AC_CACHE_CHECK([whether $fp_prog_ar is GNU ar], [fp_cv_prog_ar_is_gnu],
1084 [if "$fp_prog_ar" --version 2> /dev/null | grep "GNU" > /dev/null 2>&1; then
1085   fp_cv_prog_ar_is_gnu=yes
1086 else
1087   fp_cv_prog_ar_is_gnu=no
1088 fi])
1089 fp_prog_ar_is_gnu=$fp_cv_prog_ar_is_gnu
1090 AC_SUBST([ArIsGNUAr], [`echo $fp_prog_ar_is_gnu | tr 'a-z' 'A-Z'`])
1091 ])# FP_PROG_AR_IS_GNU
1092
1093
1094 # FP_PROG_AR_SUPPORTS_ATFILE
1095 # -----------------
1096 # Sets fp_prog_ar_supports_atfile to yes or no, depending on whether
1097 # or not it supports the @file syntax
1098 AC_DEFUN([FP_PROG_AR_SUPPORTS_ATFILE],
1099 [AC_REQUIRE([FP_PROG_AR])
1100  AC_REQUIRE([FP_PROG_AR_ARGS])
1101 AC_CACHE_CHECK([whether $fp_prog_ar supports @file], [fp_cv_prog_ar_supports_atfile],
1102 [
1103 rm -f conftest*
1104 touch conftest.file
1105 echo conftest.file  > conftest.atfile
1106 echo conftest.file >> conftest.atfile
1107 "$fp_prog_ar" $fp_prog_ar_args conftest.a @conftest.atfile > /dev/null 2>&1
1108 fp_prog_ar_supports_atfile_tmp=`"$fp_prog_ar" t conftest.a 2> /dev/null | grep -c conftest.file`
1109 rm -f conftest*
1110 if test "$fp_prog_ar_supports_atfile_tmp" -eq 2
1111 then
1112   fp_cv_prog_ar_supports_atfile=yes
1113 else
1114   fp_cv_prog_ar_supports_atfile=no
1115 fi])
1116 fp_prog_ar_supports_atfile=$fp_cv_prog_ar_supports_atfile
1117 AC_SUBST([ArSupportsAtFile], [`echo $fp_prog_ar_supports_atfile | tr 'a-z' 'A-Z'`])
1118 ])# FP_PROG_AR_SUPPORTS_ATFILE
1119
1120 # FP_PROG_AR_ARGS
1121 # ---------------
1122 # Sets fp_prog_ar_args to the arguments for ar and the output variable ArCmd
1123 # to an invocation of ar including these arguments.
1124 AC_DEFUN([FP_PROG_AR_ARGS],
1125 [AC_REQUIRE([FP_PROG_AR_IS_GNU])
1126 AC_CACHE_CHECK([for ar arguments], [fp_cv_prog_ar_args],
1127 [
1128 # GNU ar needs special treatment: it appears to have problems with
1129 # object files with the same name if you use the 's' modifier, but
1130 # simple 'ar q' works fine, and doesn't need a separate ranlib.
1131 if test $fp_prog_ar_is_gnu = yes; then
1132   fp_cv_prog_ar_args="q"
1133 else
1134   touch conftest.dummy
1135   for fp_var in clqsZ clqs cqs clq cq ; do
1136      rm -f conftest.a
1137      if "$fp_prog_ar" $fp_var conftest.a conftest.dummy > /dev/null 2> /dev/null; then
1138         fp_cv_prog_ar_args=$fp_var
1139         break
1140      fi
1141   done
1142   rm -f conftest*
1143   if test -z "$fp_cv_prog_ar_args"; then
1144     AC_MSG_ERROR([cannot figure out how to use your $fp_prog_ar])
1145   fi
1146 fi])
1147 fp_prog_ar_args=$fp_cv_prog_ar_args
1148 if test "$HostOS" != "mingw32"; then
1149     ArCmd = "$(cygpath -m $ArCmd)"
1150 fi
1151 AC_SUBST([ArCmd], ["$fp_prog_ar"])
1152 AC_SUBST([ArArgs], ["$fp_prog_ar_args"])
1153
1154 ])# FP_PROG_AR_ARGS
1155
1156
1157 # FP_PROG_AR_NEEDS_RANLIB
1158 # -----------------------
1159 # Sets the output variable RANLIB_CMD to "ranlib" if it is needed and
1160 # found, to "true" otherwise. Sets REAL_RANLIB_CMD to the ranlib program,
1161 # even if we don't need ranlib (libffi might still need it).
1162 AC_DEFUN([FP_PROG_AR_NEEDS_RANLIB],[
1163     AC_REQUIRE([FP_PROG_AR_IS_GNU])
1164     AC_REQUIRE([FP_PROG_AR_ARGS])
1165     AC_REQUIRE([AC_PROG_CC])
1166
1167     AC_PROG_RANLIB
1168
1169     if test $fp_prog_ar_is_gnu = yes
1170     then
1171         fp_cv_prog_ar_needs_ranlib=no
1172     elif test "$TargetVendor_CPP" = "apple"
1173     then
1174         # It's quite tedious to check for Apple's crazy timestamps in
1175         # .a files, so we hardcode it.
1176         fp_cv_prog_ar_needs_ranlib=yes
1177     else
1178         case $fp_prog_ar_args in
1179         *s*)
1180             fp_cv_prog_ar_needs_ranlib=no;;
1181         *)
1182             fp_cv_prog_ar_needs_ranlib=yes;;
1183         esac
1184     fi
1185
1186     # workaround for AC_PROG_RANLIB which sets RANLIB to `:' when
1187     # ranlib is missing on the target OS. The problem is that
1188     # ghc-cabal cannot execute `:' which is a shell built-in but can
1189     # execute `true' which is usually simple program supported by the
1190     # OS.
1191     # Fixes #8795
1192     if test "$RANLIB" = ":"
1193     then
1194         RANLIB="true"
1195     fi
1196     REAL_RANLIB_CMD="$RANLIB"
1197     if test $fp_cv_prog_ar_needs_ranlib = yes
1198     then
1199         RANLIB_CMD="$RANLIB"
1200     else
1201         RANLIB_CMD="true"
1202     fi
1203     AC_SUBST([REAL_RANLIB_CMD])
1204     AC_SUBST([RANLIB_CMD])
1205 ])# FP_PROG_AR_NEEDS_RANLIB
1206
1207
1208 # FP_GCC_VERSION
1209 # -----------
1210 # Extra testing of the result AC_PROG_CC, testing the gcc version no. Sets the
1211 # output variable GccVersion.
1212 AC_DEFUN([FP_GCC_VERSION],
1213 [AC_REQUIRE([AC_PROG_CC])
1214 if test -z "$CC"
1215 then
1216   AC_MSG_ERROR([gcc is required])
1217 fi
1218 GccLT34=NO
1219 GccLT44=NO
1220 GccLT46=NO
1221 AC_CACHE_CHECK([version of gcc], [fp_cv_gcc_version],
1222 [
1223     fp_cv_gcc_version="`$CC -v 2>&1 | grep 'version ' | sed -e 's/.*version [[^0-9]]*\([[0-9.]]*\).*/\1/g'`"
1224     FP_COMPARE_VERSIONS([$fp_cv_gcc_version], [-lt], [3.0],
1225                         [AC_MSG_ERROR([Need at least gcc version 3.0 (3.4+ recommended)])])
1226     # See #2770: gcc 2.95 doesn't work any more, apparently.  There probably
1227     # isn't a very good reason for that, but for now just make configure
1228     # fail.
1229     FP_COMPARE_VERSIONS([$fp_cv_gcc_version], [-lt], [3.4], GccLT34=YES)
1230     FP_COMPARE_VERSIONS([$fp_cv_gcc_version], [-lt], [4.4], GccLT44=YES)
1231     FP_COMPARE_VERSIONS([$fp_cv_gcc_version], [-lt], [4.6], GccLT46=YES)
1232 ])
1233 AC_SUBST([GccVersion], [$fp_cv_gcc_version])
1234 AC_SUBST(GccLT34)
1235 AC_SUBST(GccLT44)
1236 AC_SUBST(GccLT46)
1237 ])# FP_GCC_VERSION
1238
1239 dnl Check to see if the C compiler is clang or llvm-gcc
1240 dnl
1241 GccIsClang=NO
1242 AC_DEFUN([FP_CC_LLVM_BACKEND],
1243 [AC_REQUIRE([AC_PROG_CC])
1244 AC_MSG_CHECKING([whether C compiler is clang])
1245 $CC -x c /dev/null -dM -E > conftest.txt 2>&1
1246 if grep "__clang__" conftest.txt >/dev/null 2>&1; then
1247   AC_SUBST([CC_CLANG_BACKEND], [1])
1248   AC_SUBST([CC_LLVM_BACKEND], [1])
1249   GccIsClang=YES
1250   AC_MSG_RESULT([yes])
1251 else
1252   AC_MSG_RESULT([no])
1253   AC_MSG_CHECKING([whether C compiler has an LLVM back end])
1254   if grep "__llvm__" conftest.txt >/dev/null 2>&1; then
1255     AC_SUBST([CC_CLANG_BACKEND], [0])
1256     AC_SUBST([CC_LLVM_BACKEND], [1])
1257     AC_MSG_RESULT([yes])
1258   else
1259     AC_SUBST([CC_CLANG_BACKEND], [0])
1260     AC_SUBST([CC_LLVM_BACKEND], [0])
1261     AC_MSG_RESULT([no])
1262   fi
1263 fi
1264 AC_SUBST(GccIsClang)
1265
1266 rm -f conftest.txt
1267 ])
1268
1269 # FP_GCC_SUPPORTS_NO_PIE
1270 # ----------------------
1271 # Does gcc support the -no-pie option? If so we should pass it to gcc when
1272 # joining objects since -pie may be enabled by default.
1273 AC_DEFUN([FP_GCC_SUPPORTS_NO_PIE],
1274 [
1275    AC_REQUIRE([AC_PROG_CC])
1276    AC_MSG_CHECKING([whether GCC supports -no-pie])
1277    echo 'int main() { return 0; }' > conftest.c
1278    # Some GCC versions only warn when passed an unrecognized flag.
1279    if $CC -no-pie -x c /dev/null -dM -E > conftest.txt 2>&1 && ! grep -i unrecognized conftest.txt > /dev/null 2>&1; then
1280        CONF_GCC_SUPPORTS_NO_PIE=YES
1281        AC_MSG_RESULT([yes])
1282    else
1283        CONF_GCC_SUPPORTS_NO_PIE=NO
1284        AC_MSG_RESULT([no])
1285    fi
1286    rm -f conftest.c conftest.o conftest
1287 ])
1288
1289 dnl Small feature test for perl version. Assumes PerlCmd
1290 dnl contains path to perl binary.
1291 dnl
1292 dnl (Perl versions prior to v5.6 does not contain the string "v5";
1293 dnl instead they display version strings such as "version 5.005".)
1294 dnl
1295 AC_DEFUN([FPTOOLS_CHECK_PERL_VERSION],
1296 [$PerlCmd -v >conftest.out 2>&1
1297    if grep "v5" conftest.out >/dev/null 2>&1; then
1298       :
1299    else
1300       AC_MSG_ERROR([your version of perl probably won't work, try upgrading it.])
1301    fi
1302 rm -fr conftest*
1303 ])
1304
1305
1306 # FP_CHECK_PROG(VARIABLE, PROG-TO-CHECK-FOR,
1307 #               [VALUE-IF-NOT-FOUND], [PATH], [REJECT])
1308 # -----------------------------------------------------
1309 # HACK: A small wrapper around AC_CHECK_PROG, setting VARIABLE to the full path
1310 # of PROG-TO-CHECK-FOR when found.
1311 AC_DEFUN([FP_CHECK_PROG],
1312 [AC_CHECK_PROG([$1], [$2], [$as_dir/$ac_word$ac_exec_ext], [$3], [$4], [$5])][]dnl
1313 )# FP_CHECK_PROC
1314
1315
1316 # FP_PROG_FIND
1317 # ------------
1318 # Find a non-WinDoze version of the "find" utility.
1319 AC_DEFUN([FP_PROG_FIND],
1320 [AC_PATH_PROGS([fp_prog_find], [gfind find], find)
1321 echo foo > conftest.txt
1322 $fp_prog_find conftest.txt -print > conftest.out 2>&1
1323 if grep '^conftest.txt$' conftest.out > /dev/null 2>&1 ; then
1324   # OK, looks like a real "find".
1325   FindCmd="$fp_prog_find"
1326 else
1327   # Found a poor WinDoze version of "find", ignore it.
1328   AC_MSG_WARN([$fp_prog_find looks like a non-*nix find, ignoring it])
1329   FP_CHECK_PROG([FindCmd], [find], [], [], [$fp_prog_find])
1330 fi
1331 rm -f conftest.txt conftest.out
1332 AC_SUBST([FindCmd])[]dnl
1333 ])# FP_PROG_FIND
1334
1335
1336 # FP_PROG_SORT
1337 # ------------
1338 # Find a Unix-like sort
1339 AC_DEFUN([FP_PROG_SORT],
1340 [AC_PATH_PROG([fp_prog_sort], [sort])
1341 echo conwip > conftest.txt
1342 $fp_prog_sort -f conftest.txt > conftest.out 2>&1
1343 if grep 'conwip' conftest.out > /dev/null 2>&1 ; then
1344   # The goods
1345   SortCmd="$fp_prog_sort"
1346 else
1347   # Summink else..pick next one.
1348   AC_MSG_WARN([$fp_prog_sort looks like a non-*nix sort, ignoring it])
1349   FP_CHECK_PROG([SortCmd], [sort], [], [], [$fp_prog_sort])
1350 fi
1351 rm -f conftest.txt conftest.out
1352 AC_SUBST([SortCmd])[]dnl
1353 ])# FP_PROG_SORT
1354
1355
1356 dnl
1357 dnl FPTOOLS_NOCACHE_CHECK prints a message, then sets the
1358 dnl values of the second argument to the result of running
1359 dnl the commands given by the third. It does not cache its
1360 dnl result, so it is suitable for checks which should be
1361 dnl run every time.
1362 dnl
1363 AC_DEFUN([FPTOOLS_NOCACHE_CHECK],
1364 [AC_MSG_CHECKING([$1])
1365  $3
1366  AC_MSG_RESULT([$][$2])
1367 ])
1368
1369 dnl
1370 dnl FPTOOLS_GHC_VERSION(version)
1371 dnl FPTOOLS_GHC_VERSION(major, minor [, patchlevel])
1372 dnl FPTOOLS_GHC_VERSION(version, major, minor, patchlevel)
1373 dnl
1374 dnl Test for version of installed ghc.  Uses $GHC.
1375 dnl [original version pinched from c2hs]
1376 dnl
1377 AC_DEFUN([FPTOOLS_GHC_VERSION],
1378 [FPTOOLS_NOCACHE_CHECK([version of ghc], [fptools_version_of_ghc],
1379 ["${WithGhc-ghc}" --version > conftestghc 2>&1
1380   cat conftestghc >&AS_MESSAGE_LOG_FD
1381 #Useless Use Of cat award...
1382   fptools_version_of_ghc=`cat conftestghc | sed -n -e 's/, patchlevel *\([[0-9]]\)/.\1/;s/.* version \([[0-9]][[0-9.]]*\).*/\1/p'`
1383   rm -fr conftest*
1384   if test "[$]fptools_version_of_ghc" = ""
1385   then
1386     fptools_version_of_ghc='unknown'
1387   fi
1388 fptools_version_of_ghc[_major]=`echo [$]fptools_version_of_ghc | sed -e 's/^\([[0-9]]\).*/\1/'`
1389 fptools_version_of_ghc[_minor]=`echo [$]fptools_version_of_ghc | sed -e 's/^[[0-9]]\.\([[0-9]]*\).*/\1/'`
1390 fptools_version_of_ghc[_pl]=`echo [$]fptools_version_of_ghc | sed -n -e 's/^[[0-9]]\.[[0-9]]*\.\([[0-9]]*\)/\1/p'`
1391 #
1392 if test "[$]fptools_version_of_ghc[_pl]" = ""
1393 then
1394   fptools_version_of_ghc[_all]="[$]fptools_version_of_ghc[_major].[$]fptools_version_of_ghc[_minor]"
1395   fptools_version_of_ghc[_pl]="0"
1396 else
1397   fptools_version_of_ghc[_all]="[$]fptools_version_of_ghc[_major].[$]fptools_version_of_ghc[_minor].[$]fptools_version_of_ghc[_pl]"
1398 fi
1399 #
1400 ifelse($#, [1], [dnl
1401 [$1]="[$]fptools_version_of_ghc[_all]"
1402 ], $#, [2], [dnl
1403 [$1]="[$]fptools_version_of_ghc[_major]"
1404 [$2]="[$]fptools_version_of_ghc[_minor]"
1405 ], $#, [3], [dnl
1406 [$1]="[$]fptools_version_of_ghc[_major]"
1407 [$2]="[$]fptools_version_of_ghc[_minor]"
1408 [$3]="[$]fptools_version_of_ghc[_pl]"
1409 ], $#, [4], [dnl
1410 [$1]="[$]fptools_version_of_ghc[_all]"
1411 [$2]="[$]fptools_version_of_ghc[_major]"
1412 [$3]="[$]fptools_version_of_ghc[_minor]"
1413 [$4]="[$]fptools_version_of_ghc[_pl]"
1414 ])
1415 ])
1416 ])dnl
1417
1418
1419 # FP_CHECK_FUNC(FUNCTION, PROLOGUE, BODY, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
1420 # ---------------------------------------------------------------------------------
1421 # A variant of AC_CHECK_FUNCS, limited to a single FUNCTION, but with the
1422 # additional flexibility of specifying the PROLOGUE and BODY.
1423 AC_DEFUN([FP_CHECK_FUNC],
1424 [AS_VAR_PUSHDEF([fp_func], [fp_cv_func_$1])dnl
1425 AC_CACHE_CHECK([for $1], fp_func,
1426 [AC_LINK_IFELSE([AC_LANG_PROGRAM([$2], [$3])],
1427                 [AS_VAR_SET(fp_func, yes)],
1428                 [AS_VAR_SET(fp_func, no)])])
1429 AS_IF([test AS_VAR_GET(fp_func) = yes],
1430       [AC_DEFINE(AS_TR_CPP(HAVE_$1), [1],
1431                 [Define to 1 if you have the `]$1[' function.]) $4],
1432       [$5])dnl
1433 AS_VAR_POPDEF([fp_func])dnl
1434 ])# FP_CHECK_FUNC
1435
1436 # FP_PROG_GHC_PKG
1437 # ----------------
1438 # Try to find a ghc-pkg matching the ghc mentioned in the environment variable
1439 # WithGhc. Sets the output variable GhcPkgCmd.
1440 AC_DEFUN([FP_PROG_GHC_PKG],
1441 [AC_CACHE_CHECK([for ghc-pkg matching $WithGhc], fp_cv_matching_ghc_pkg,
1442 [
1443 # If we are told to use ghc-stage2, then we're using an in-tree
1444 # compiler. In this case, we just want ghc-pkg, not ghc-pkg-stage2,
1445 # so we sed off -stage[0-9]$. However, if we are told to use
1446 # ghc-6.12.1 then we want to use ghc-pkg-6.12.1, so we keep any
1447 # other suffix.
1448 fp_ghc_pkg_guess=`echo "$WithGhc" | sed -e 's/-stage@<:@0-9@:>@$//' -e 's,ghc\(@<:@^/\\@:>@*\)$,ghc-pkg\1,'`
1449 if "$fp_ghc_pkg_guess" list > /dev/null 2>&1; then
1450   fp_cv_matching_ghc_pkg=$fp_ghc_pkg_guess
1451 else
1452   AC_MSG_ERROR([Cannot find matching ghc-pkg])
1453 fi])
1454 GhcPkgCmd=$fp_cv_matching_ghc_pkg
1455 AC_SUBST([GhcPkgCmd])
1456 ])# FP_PROG_GHC_PKG
1457
1458
1459 # FP_GCC_EXTRA_FLAGS
1460 # ------------------
1461 # Determine which extra flags we need to pass gcc when we invoke it
1462 # to compile .hc code.
1463 #
1464 # -fwrapv is needed for gcc to emit well-behaved code in the presence of
1465 # integer wrap around. (Trac #952)
1466 #
1467 AC_DEFUN([FP_GCC_EXTRA_FLAGS],
1468 [AC_REQUIRE([FP_GCC_VERSION])
1469 AC_CACHE_CHECK([for extra options to pass gcc when compiling via C], [fp_cv_gcc_extra_opts],
1470 [fp_cv_gcc_extra_opts=
1471  FP_COMPARE_VERSIONS([$fp_cv_gcc_version], [-ge], [3.4],
1472   [fp_cv_gcc_extra_opts="$fp_cv_gcc_extra_opts -fwrapv"],
1473   [])
1474  FP_COMPARE_VERSIONS([$fp_cv_gcc_version], [-ge], [4.0],
1475   [fp_cv_gcc_extra_opts="$fp_cv_gcc_extra_opts -fno-builtin"],
1476   [])
1477 ])
1478 AC_SUBST([GccExtraViaCOpts],$fp_cv_gcc_extra_opts)
1479 ])
1480
1481
1482 # FP_SETUP_PROJECT_VERSION
1483 # ---------------------
1484 AC_DEFUN([FP_SETUP_PROJECT_VERSION],
1485 [
1486 if test "$RELEASE" = "NO"; then
1487     AC_MSG_CHECKING([for GHC version date])
1488     if test -f VERSION_DATE; then
1489         PACKAGE_VERSION=${PACKAGE_VERSION}.`cat VERSION_DATE`
1490         AC_MSG_RESULT(given $PACKAGE_VERSION)
1491     elif test -d .git; then
1492         changequote(, )dnl
1493         ver_posixtime=`git log -1 --pretty=format:%ct`
1494         ver_date=`perl -MPOSIX -e "print strftime('%Y%m%d', gmtime($ver_posixtime));"`
1495         if echo $ver_date | grep '^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$' 2>&1 >/dev/null; then true; else
1496         changequote([, ])dnl
1497                 AC_MSG_ERROR([failed to detect version date: check that git and perl are in your path])
1498         fi
1499         PACKAGE_VERSION=${PACKAGE_VERSION}.$ver_date
1500         AC_MSG_RESULT(inferred $PACKAGE_VERSION)
1501     elif test -f VERSION; then
1502         PACKAGE_VERSION=`cat VERSION`
1503         AC_MSG_RESULT(given $PACKAGE_VERSION)
1504     else
1505         AC_MSG_WARN([cannot determine snapshot version: no .git directory and no VERSION file])
1506         dnl We'd really rather this case didn't happen, but it might
1507         dnl do (in particular, people using lndir trees may find that
1508         dnl the build system can't find any other date). If it does
1509         dnl happen, then we use the current date.
1510         dnl This way we get some idea about how recent a build is.
1511         dnl It also means that packages built for 2 different builds
1512         dnl will probably use different version numbers, so things are
1513         dnl less likely to go wrong.
1514         PACKAGE_VERSION=${PACKAGE_VERSION}.`date +%Y%m%d`
1515     fi
1516 fi
1517
1518     AC_MSG_CHECKING([for GHC Git commit id])
1519     if test -d .git; then
1520         git_commit_id=`git rev-parse HEAD`
1521         if test -n "$git_commit_id" 2>&1 >/dev/null; then true; else
1522             AC_MSG_ERROR([failed to detect revision: check that git is in your path])
1523         fi
1524         PACKAGE_GIT_COMMIT_ID=$git_commit_id
1525         AC_MSG_RESULT(inferred $PACKAGE_GIT_COMMIT_ID)
1526     elif test -f GIT_COMMIT_ID; then
1527         PACKAGE_GIT_COMMIT_ID=`cat GIT_COMMIT_ID`
1528         AC_MSG_RESULT(given $PACKAGE_GIT_COMMIT_ID)
1529     else
1530         AC_MSG_WARN([cannot determine snapshot revision: no .git directory and no 'GIT_COMMIT_ID' file])
1531         PACKAGE_GIT_COMMIT_ID="0000000000000000000000000000000000000000"
1532     fi
1533
1534
1535 # Some renamings
1536 AC_SUBST([ProjectName], [$PACKAGE_NAME])
1537 AC_SUBST([ProjectVersion], [$PACKAGE_VERSION])
1538 AC_SUBST([ProjectGitCommitId], [$PACKAGE_GIT_COMMIT_ID])
1539
1540 # Split PACKAGE_VERSION into (possibly empty) parts
1541 VERSION_MAJOR=`echo $PACKAGE_VERSION | sed 's/^\(@<:@^.@:>@*\)\(\.\{0,1\}\(.*\)\)$/\1'/`
1542 VERSION_TMP=`echo $PACKAGE_VERSION | sed 's/^\(@<:@^.@:>@*\)\(\.\{0,1\}\(.*\)\)$/\3'/`
1543 VERSION_MINOR=`echo $VERSION_TMP | sed 's/^\(@<:@^.@:>@*\)\(\.\{0,1\}\(.*\)\)$/\1'/`
1544 ProjectPatchLevel=`echo $VERSION_TMP | sed 's/^\(@<:@^.@:>@*\)\(\.\{0,1\}\(.*\)\)$/\3'/`
1545
1546 # Calculate project version as an integer, using 2 digits for minor version
1547 case $VERSION_MINOR in
1548   ?) ProjectVersionInt=${VERSION_MAJOR}0${VERSION_MINOR} ;;
1549   ??) ProjectVersionInt=${VERSION_MAJOR}${VERSION_MINOR} ;;
1550   *) AC_MSG_ERROR([bad minor version in $PACKAGE_VERSION]) ;;
1551 esac
1552 AC_SUBST([ProjectVersionInt])
1553
1554 # The project patchlevel is zero unless stated otherwise
1555 test -z "$ProjectPatchLevel" && ProjectPatchLevel=0
1556
1557 # Save split version of ProjectPatchLevel
1558 ProjectPatchLevel1=`echo $ProjectPatchLevel | sed 's/^\(@<:@^.@:>@*\)\(\.\{0,1\}\(.*\)\)$/\1/'`
1559 ProjectPatchLevel2=`echo $ProjectPatchLevel | sed 's/^\(@<:@^.@:>@*\)\(\.\{0,1\}\(.*\)\)$/\3/'`
1560
1561 AC_SUBST([ProjectPatchLevel1])
1562 AC_SUBST([ProjectPatchLevel2])
1563
1564 # Remove dots from the patch level; this allows us to have versions like 6.4.1.20050508
1565 ProjectPatchLevel=`echo $ProjectPatchLevel | sed 's/\.//'`
1566
1567 AC_SUBST([ProjectPatchLevel])
1568
1569 # The version of the GHC package changes every day, since the
1570 # patchlevel is the current date.  We don't want to force
1571 # recompilation of the entire compiler when this happens, so for
1572 # GHC HEAD we omit the patchlevel from the package version number.
1573 #
1574 # The ProjectPatchLevel1 > 20000000 iff GHC HEAD. If it's for a stable
1575 # release like 7.10.1 or for a release candidate such as 7.10.1.20141224
1576 # then we don't omit the patchlevel components.
1577
1578 ProjectVersionMunged="$ProjectVersion"
1579 if test "$ProjectPatchLevel1" -gt 20000000; then
1580   ProjectVersionMunged="${VERSION_MAJOR}.${VERSION_MINOR}"
1581 fi
1582 AC_SUBST([ProjectVersionMunged])
1583 ])# FP_SETUP_PROJECT_VERSION
1584
1585 # Check for a working timer_create().  We need a pretty detailed check
1586 # here, because there exist partially-working implementations of
1587 # timer_create() in certain versions of Linux (see bug #1933).
1588 #
1589 AC_DEFUN([FP_CHECK_TIMER_CREATE],[
1590 AC_CHECK_FUNC([timer_create],[HAVE_timer_create=yes],[HAVE_timer_create=no])
1591
1592 if test "$HAVE_timer_create" = "yes"
1593 then
1594   if test "$cross_compiling" = "yes"
1595   then
1596     # We can't test timer_create when we're cross-compiling, so we
1597     # optimistiaclly assume that it actually works properly.
1598     AC_DEFINE([USE_TIMER_CREATE], 1,  [Define to 1 if we can use timer_create(CLOCK_REALTIME,...)])
1599   else
1600   AC_CACHE_CHECK([for a working timer_create(CLOCK_REALTIME)],
1601     [fptools_cv_timer_create_works],
1602     [AC_TRY_RUN([
1603 #include <stdio.h>
1604 #ifdef HAVE_STDLIB_H
1605 #include <stdlib.h>
1606 #endif
1607 #ifdef HAVE_TIME_H
1608 #include <time.h>
1609 #endif
1610 #ifdef HAVE_SIGNAL_H
1611 #include <signal.h>
1612 #endif
1613 #ifdef HAVE_UNISTD_H
1614 #include <unistd.h>
1615 #endif
1616
1617 static volatile int tock = 0;
1618 static void handler(int i)
1619 {
1620    tock = 1;
1621 }
1622
1623 static void timeout(int i)
1624 {
1625   // timer_settime() has been known to hang, so just in case
1626   // we install a 1-second timeout (see #2257)
1627   exit(99);
1628 }
1629
1630 int main(int argc, char *argv[])
1631 {
1632
1633     struct sigevent ev;
1634     timer_t timer;
1635     struct itimerspec it;
1636     struct sigaction action;
1637     int m,n,count = 0;
1638
1639     ev.sigev_notify = SIGEV_SIGNAL;
1640     ev.sigev_signo  = SIGVTALRM;
1641
1642     action.sa_handler = handler;
1643     action.sa_flags = 0;
1644     sigemptyset(&action.sa_mask);
1645     if (sigaction(SIGVTALRM, &action, NULL) == -1) {
1646         fprintf(stderr,"SIGVTALRM problem\n");
1647         exit(3);
1648     }
1649
1650     action.sa_handler = timeout;
1651     action.sa_flags = 0;
1652     sigemptyset(&action.sa_mask);
1653     if (sigaction(SIGALRM, &action, NULL) == -1) {
1654       fprintf(stderr,"SIGALRM problem\n");
1655       exit(3);
1656     }
1657     alarm(1);
1658
1659     if (timer_create(CLOCK_REALTIME, &ev, &timer) != 0) {
1660         fprintf(stderr,"No CLOCK_REALTIME timer\n");
1661         exit(2);
1662     }
1663
1664     tock = 0;
1665
1666     it.it_value.tv_sec = 0;
1667     it.it_value.tv_nsec = 1000000; // 1ms
1668     it.it_interval = it.it_value;
1669     if (timer_settime(timer, 0, &it, NULL) != 0) {
1670         fprintf(stderr,"settime problem\n");
1671         exit(4);
1672     }
1673
1674     // some environments have coarse scheduler/timer granularity of ~10ms and worse
1675     usleep(100000); // 100ms
1676
1677     if (!tock) {
1678         fprintf(stderr,"no CLOCK_REALTIME signal\n");
1679         exit(5);
1680     }
1681
1682     timer_delete(timer);
1683
1684     exit(0);
1685 }
1686      ],
1687      [fptools_cv_timer_create_works=yes],
1688      [fptools_cv_timer_create_works=no])
1689   ])
1690 case $fptools_cv_timer_create_works in
1691     yes) AC_DEFINE([USE_TIMER_CREATE], 1,
1692                    [Define to 1 if we can use timer_create(CLOCK_REALTIME,...)]);;
1693 esac
1694   fi
1695 fi
1696 ])
1697
1698 # FP_ICONV
1699 # -------------
1700 AC_DEFUN([FP_ICONV],
1701 [
1702   dnl--------------------------------------------------------------------
1703   dnl * Deal with arguments telling us iconv is somewhere odd
1704   dnl--------------------------------------------------------------------
1705
1706   dnl Note: ICONV_LIB_DIRS and ICONV_INCLUDE_DIRS are not predefined
1707   dnl to the empty string to allow them to be overridden from the
1708   dnl environment.
1709
1710   AC_ARG_WITH([iconv-includes],
1711     [AC_HELP_STRING([--with-iconv-includes],
1712       [directory containing iconv.h])],
1713       [ICONV_INCLUDE_DIRS=$withval])
1714
1715   AC_ARG_WITH([iconv-libraries],
1716     [AC_HELP_STRING([--with-iconv-libraries],
1717       [directory containing iconv library])],
1718       [ICONV_LIB_DIRS=$withval])
1719
1720   AC_SUBST(ICONV_INCLUDE_DIRS)
1721   AC_SUBST(ICONV_LIB_DIRS)
1722 ])# FP_ICONV
1723
1724 # FP_GMP
1725 # -------------
1726 AC_DEFUN([FP_GMP],
1727 [
1728   dnl--------------------------------------------------------------------
1729   dnl * Deal with arguments telling us gmp is somewhere odd
1730   dnl--------------------------------------------------------------------
1731
1732   AC_ARG_WITH([gmp-includes],
1733     [AC_HELP_STRING([--with-gmp-includes],
1734       [directory containing gmp.h])],
1735       [GMP_INCLUDE_DIRS=$withval])
1736
1737   AC_ARG_WITH([gmp-libraries],
1738     [AC_HELP_STRING([--with-gmp-libraries],
1739       [directory containing gmp library])],
1740       [GMP_LIB_DIRS=$withval])
1741
1742   AC_SUBST(GMP_INCLUDE_DIRS)
1743   AC_SUBST(GMP_LIB_DIRS)
1744 ])# FP_GMP
1745
1746 # FP_CURSES
1747 # -------------
1748 AC_DEFUN([FP_CURSES],
1749 [
1750   dnl--------------------------------------------------------------------
1751   dnl * Deal with arguments telling us curses is somewhere odd
1752   dnl--------------------------------------------------------------------
1753
1754   AC_ARG_WITH([curses-libraries],
1755     [AC_HELP_STRING([--with-curses-libraries],
1756       [directory containing curses libraries])],
1757       [CURSES_LIB_DIRS=$withval])
1758
1759   AC_SUBST(CURSES_INCLUDE_DIRS)
1760   AC_SUBST(CURSES_LIB_DIRS)
1761 ])# FP_CURSES
1762
1763 # --------------------------------------------------------------
1764 # Calculate absolute path to build tree
1765 # --------------------------------------------------------------
1766
1767 AC_DEFUN([FP_FIND_ROOT],[
1768 AC_MSG_CHECKING(for path to top of build tree)
1769     if test "$windows" = YES
1770     then
1771       dnl Make sure this is a c:/foo/bar (mixed) style path. Some parts of
1772       dnl the build system might depend on it (such as the sed expression
1773       dnl `"s|$(TOP)/||i"` in addCFileDeps in rules/build-dependencies.mk).
1774       hardtop=$(cygpath -m "$(pwd)")
1775     else
1776       hardtop=$(pwd)
1777     fi
1778
1779     dnl Remove common automounter nonsense
1780     hardtop=`echo $hardtop | sed 's|^/tmp_mnt.*\(/local/.*\)$|\1|' | sed 's|^/tmp_mnt/|/|'`
1781
1782     if ! test -d "$hardtop"; then
1783         AC_MSG_ERROR([cannot determine current directory])
1784     fi
1785
1786     dnl We don't support building in directories with spaces.
1787     case "$hardtop" in
1788     *' '*)
1789         AC_MSG_ERROR([
1790         The build system does not support building in a directory
1791         containing space characters.
1792         Suggestion: move the build tree somewhere else.])
1793         ;;
1794     esac
1795
1796     AC_SUBST(hardtop)
1797
1798     AC_MSG_RESULT($hardtop)
1799 ])
1800
1801 # GHC_CONVERT_CPU(cpu, target_var)
1802 # --------------------------------
1803 # converts cpu from gnu to ghc naming, and assigns the result to $target_var
1804 AC_DEFUN([GHC_CONVERT_CPU],[
1805 case "$1" in
1806   aarch64*)
1807     $2="aarch64"
1808     ;;
1809   alpha*)
1810     $2="alpha"
1811     ;;
1812   arm*)
1813     $2="arm"
1814     ;;
1815   hppa1.1*)
1816     $2="hppa1_1"
1817     ;;
1818   hppa*)
1819     $2="hppa"
1820     ;;
1821   i386|i486|i586|i686)
1822     $2="i386"
1823     ;;
1824   ia64)
1825     $2="ia64"
1826     ;;
1827   m68k*)
1828     $2="m68k"
1829     ;;
1830   mipseb*)
1831     $2="mipseb"
1832     ;;
1833   mipsel*)
1834     $2="mipsel"
1835     ;;
1836   mips*)
1837     $2="mips"
1838     ;;
1839   powerpc64le*)
1840     $2="powerpc64le"
1841     ;;
1842   powerpc64*)
1843     $2="powerpc64"
1844     ;;
1845   powerpc*)
1846     $2="powerpc"
1847     ;;
1848   rs6000)
1849     $2="rs6000"
1850     ;;
1851   s390x*)
1852     $2="s390x"
1853     ;;
1854   s390*)
1855     $2="s390"
1856     ;;
1857   sh4)
1858     $2="sh4"
1859     ;;
1860   sparc64*)
1861     $2="sparc64"
1862     ;;
1863   sparc*)
1864     $2="sparc"
1865     ;;
1866   vax)
1867     $2="vax"
1868     ;;
1869   x86_64|amd64)
1870     $2="x86_64"
1871     ;;
1872   *)
1873     echo "Unknown CPU $1"
1874     exit 1
1875     ;;
1876   esac
1877 ])
1878
1879 # GHC_CONVERT_VENDOR(vendor, target_var)
1880 # --------------------------------
1881 # converts vendor from gnu to ghc naming, and assigns the result to $target_var
1882 AC_DEFUN([GHC_CONVERT_VENDOR],[
1883   case "$1" in
1884   pc|gentoo|w64) # like i686-pc-linux-gnu, i686-gentoo-freebsd8, x86_64-w64-mingw32
1885     $2="unknown"
1886     ;;
1887   softfloat) # like armv5tel-softfloat-linux-gnueabi
1888     $2="unknown"
1889     ;;
1890   hardfloat) # like armv7a-hardfloat-linux-gnueabi
1891     $2="unknown"
1892     ;;
1893   *)
1894     #pass thru by default
1895     $2="$1"
1896     ;;
1897   esac
1898 ])
1899
1900 # GHC_CONVERT_OS(os, converted_cpu, target_var)
1901 # --------------------------------
1902 # converts os from gnu to ghc naming, and assigns the result to $target_var
1903 AC_DEFUN([GHC_CONVERT_OS],[
1904     case "$1" in
1905       # watchos and tvos are ios variants as of May 2017.
1906       ios|watchos|tvos)
1907         $3="ios"
1908         ;;
1909       linux-android*)
1910         $3="linux-android"
1911         ;;
1912       linux-*|linux)
1913         $3="linux"
1914         ;;
1915       # As far as I'm aware, none of these have relevant variants
1916       freebsd|netbsd|openbsd|dragonfly|hpux|linuxaout|kfreebsdgnu|freebsd2|solaris2|mingw32|darwin|gnu|nextstep2|nextstep3|sunos4|ultrix|haiku)
1917         $3="$1"
1918         ;;
1919       aix*) # e.g. powerpc-ibm-aix7.1.3.0
1920         $3="aix"
1921         ;;
1922       darwin*) # e.g. aarch64-apple-darwin14
1923         $3="darwin"
1924         ;;
1925       freebsd*) # like i686-gentoo-freebsd7
1926                 #      i686-gentoo-freebsd8
1927                 #      i686-gentoo-freebsd8.2
1928         $3="freebsd"
1929         ;;
1930       nto-qnx*)
1931         $3="nto-qnx"
1932         ;;
1933       *)
1934         echo "Unknown OS $1"
1935         exit 1
1936         ;;
1937       esac
1938 ])
1939
1940 # BOOTSTRAPPING_GHC_INFO_FIELD
1941 # --------------------------------
1942 # Set the variable $1 to the value of the ghc --info field $2.
1943 AC_DEFUN([BOOTSTRAPPING_GHC_INFO_FIELD],[
1944 $1=`"$WithGhc" --info | grep "^ ,(\"$2\"," | sed -e 's/.*","//' -e 's/")$//'`
1945 tmp=${$1#\$topdir/}
1946 if test "${$1}" != "$tmp"
1947 then
1948     topdir=`"$WithGhc" --print-libdir | sed 's#\\\\#/#g'`
1949     $1="$topdir/$tmp"
1950 fi
1951 AC_SUBST($1)
1952 ])
1953
1954 # LIBRARY_VERSION(lib, [dir])
1955 # --------------------------------
1956 # Gets the version number of a library.
1957 # If $1 is ghc-prim, then we define LIBRARY_ghc_prim_VERSION as 1.2.3
1958 # $2 points to the directory under libraries/
1959 AC_DEFUN([LIBRARY_VERSION],[
1960 dir=m4_default([$2],[$1])
1961 LIBRARY_[]translit([$1], [-], [_])[]_VERSION=`grep -i "^version:" libraries/${dir}/$1.cabal | sed "s/.* //"`
1962 AC_SUBST(LIBRARY_[]translit([$1], [-], [_])[]_VERSION)
1963 ])
1964
1965 # XCODE_VERSION()
1966 # --------------------------------
1967 # Gets the version number of XCode, if on a Mac
1968 AC_DEFUN([XCODE_VERSION],[
1969     if test "$TargetVendor_CPP" = "apple"
1970     then
1971         AC_MSG_CHECKING(XCode version)
1972         XCodeVersion=`xcodebuild -version | grep Xcode | sed "s/Xcode //"`
1973         # Old XCode versions don't actually give the XCode version
1974         if test "$XCodeVersion" = ""
1975         then
1976             AC_MSG_RESULT(not found (too old?))
1977             XCodeVersion1=0
1978             XCodeVersion2=0
1979         else
1980             AC_MSG_RESULT($XCodeVersion)
1981             XCodeVersion1=`echo "$XCodeVersion" | sed 's/\..*//'`
1982             changequote(, )dnl
1983             XCodeVersion2=`echo "$XCodeVersion" | sed 's/[^.]*\.\([^.]*\).*/\1/'`
1984             changequote([, ])dnl
1985             AC_MSG_NOTICE(XCode version component 1: $XCodeVersion1)
1986             AC_MSG_NOTICE(XCode version component 2: $XCodeVersion2)
1987         fi
1988     fi
1989 ])
1990
1991 # FIND_LLVM_PROG()
1992 # --------------------------------
1993 # Find where the llvm tools are. We have a special function to handle when they
1994 # are installed with a version suffix (e.g., llc-3.1).
1995 #
1996 # $1 = the variable to set
1997 # $2 = the command to look for
1998 # $3 = the version of the command to look for
1999 #
2000 AC_DEFUN([FIND_LLVM_PROG],[
2001     # Test for program with and without version name.
2002     AC_CHECK_TOOLS([$1], [$2-$3 $2])
2003     if test "$$1" != ":"; then
2004         AC_MSG_CHECKING([$$1 is version $3])
2005         if test `$$1 --version | grep -c "version $3"` -gt 0 ; then
2006             AC_MSG_RESULT(yes)
2007         else
2008             AC_MSG_RESULT(no)
2009             $1=""
2010         fi
2011     fi
2012 ])
2013
2014 # CHECK_LD_COPY_BUG()
2015 # -------------------
2016 # Check for binutils bug #16177 present in some versions of the bfd ld
2017 # implementation affecting ARM relocations.
2018 # https://sourceware.org/bugzilla/show_bug.cgi?id=16177
2019 #
2020 # $1 = the platform
2021 #
2022 AC_DEFUN([CHECK_LD_COPY_BUG],[
2023     case $1 in
2024       arm*linux*)
2025         AC_CHECK_TARGET_TOOL([READELF], [readelf])
2026         AC_CHECK_TARGET_TOOL([AS], [as])
2027         AC_MSG_CHECKING([for ld bug 16177])
2028         cat >actest.s <<-EOF
2029           .globl _start
2030           .p2align 4
2031         _start:
2032           bkpt
2033
2034         .data
2035           .globl data_object
2036         object_reference:
2037           .long data_object
2038           .size object_reference, 4
2039 EOF
2040
2041         cat >aclib.s <<-EOF
2042           .data
2043           .globl data_object
2044           .type data_object, %object
2045           .size data_object, 4
2046         data_object:
2047             .long 123
2048 EOF
2049
2050         $AS -o aclib.o aclib.s
2051         $LD -shared -o aclib.so aclib.o
2052
2053         $AS -o actest.o actest.s
2054         $LD -o actest actest.o aclib.so
2055
2056         if $READELF -r actest | grep R_ARM_COPY > /dev/null; then
2057             AC_MSG_RESULT([affected])
2058             AC_MSG_ERROR(
2059               [Your linker is affected by binutils #16177, which
2060                critically breaks linkage of GHC objects. Please either upgrade
2061                binutils or supply a different linker with the LD environment
2062                variable.])
2063         else
2064             AC_MSG_RESULT([unaffected])
2065         fi
2066
2067         rm -f aclib.s aclib.o aclib.so actest.s actest.o actest
2068         ;;
2069       *)
2070         ;;
2071     esac
2072 ])
2073
2074 # FIND_GHC_BOOTSTRAP_PROG()
2075 # --------------------------------
2076 # Parse the bootstrap GHC's compier settings file for the location of things
2077 # like the `llc` and `opt` commands.
2078 #
2079 # $1 = the variable to set
2080 # $2 = The bootstrap compiler.
2081 # $3 = The string to grep for to find the correct line.
2082 #
2083 AC_DEFUN([FIND_GHC_BOOTSTRAP_PROG],[
2084     BootstrapTmpCmd=`grep $3 $($2 --print-libdir)/settings 2>/dev/null | sed 's/.*", "//;s/".*//'`
2085     if test -n "$BootstrapTmpCmd" && test `basename $BootstrapTmpCmd` = $BootstrapTmpCmd ; then
2086         AC_PATH_PROG([$1], [$BootstrapTmpCmd], "")
2087     else
2088         $1=$BootstrapTmpCmd
2089     fi
2090 ])
2091
2092
2093 AC_DEFUN([MAYBE_OVERRIDE_STAGE0],[
2094   if test ! -z "$With_$1" -a "$CrossCompiling" != "YES"; then
2095       AC_MSG_NOTICE([Not cross-compiling, so --with-$1 also sets $2])
2096       $2=$With_$1
2097   fi
2098 ])
2099
2100
2101 # FP_CPP_CMD_WITH_ARGS()
2102 # ----------------------
2103 # sets CPP command and its arguments
2104 #
2105 # $1 = the variable to set to CPP command
2106 # $2 = the variable to set to CPP command arguments
2107
2108 AC_DEFUN([FP_CPP_CMD_WITH_ARGS],[
2109 dnl ** what cpp to use?
2110 dnl --------------------------------------------------------------
2111 AC_ARG_WITH(hs-cpp,
2112 [AC_HELP_STRING([--with-hs-cpp=ARG],
2113       [Path to the (C) preprocessor for Haskell files [default=autodetect]])],
2114 [
2115     if test "$HostOS" = "mingw32"
2116     then
2117         AC_MSG_WARN([Request to use $withval will be ignored])
2118     else
2119         HS_CPP_CMD=$withval
2120     fi
2121 ],
2122 [
2123
2124     # We can't use $CPP here, since HS_CPP_CMD is expected to be a single
2125     # command (no flags), and AC_PROG_CPP defines CPP as "/usr/bin/gcc -E".
2126     HS_CPP_CMD=$CC
2127
2128     SOLARIS_GCC_CPP_BROKEN=NO
2129     SOLARIS_FOUND_GOOD_CPP=NO
2130     case $host in
2131         i386-*-solaris2)
2132         GCC_MAJOR_MINOR=`$CC --version|grep "gcc (GCC)"|cut -d ' ' -f 3-3|cut -d '.' -f 1-2`
2133         if test "$GCC_MAJOR_MINOR" != "3.4"; then
2134           # this is not 3.4.x release so with broken CPP
2135           SOLARIS_GCC_CPP_BROKEN=YES
2136         fi
2137         ;;
2138     esac
2139
2140     if test "$SOLARIS_GCC_CPP_BROKEN" = "YES"; then
2141       # let's try to find if GNU C 3.4.x is installed
2142       if test -x /usr/sfw/bin/gcc; then
2143         # something executable is in expected path so let's
2144         # see if it's really GNU C
2145         NEW_GCC_MAJOR_MINOR=`/usr/sfw/bin/gcc --version|grep "gcc (GCC)"|cut -d ' ' -f 3-3|cut -d '.' -f 1-2`
2146         if test "$NEW_GCC_MAJOR_MINOR" = "3.4"; then
2147           # this is GNU C 3.4.x which provides non-broken CPP on Solaris
2148           # let's use it as CPP then.
2149           HS_CPP_CMD=/usr/sfw/bin/gcc
2150           SOLARIS_FOUND_GOOD_CPP=YES
2151         fi
2152       fi
2153       if test "$SOLARIS_FOUND_GOOD_CPP" = "NO"; then
2154         AC_MSG_WARN([Your GNU C provides broken CPP and you do not have GNU C 3.4.x installed.])
2155         AC_MSG_WARN([Please install GNU C 3.4.x to solve this issue. It will be used as CPP only.])
2156       fi
2157     fi
2158 ]
2159 )
2160
2161
2162
2163 dnl ** what cpp flags to use?
2164 dnl -----------------------------------------------------------
2165 AC_ARG_WITH(hs-cpp-flags,
2166   [AC_HELP_STRING([--with-hs-cpp-flags=ARG],
2167       [Flags to the (C) preprocessor for Haskell files [default=autodetect]])],
2168   [
2169       if test "$HostOS" = "mingw32"
2170       then
2171           AC_MSG_WARN([Request to use $withval will be ignored])
2172       else
2173           HS_CPP_ARGS=$withval
2174       fi
2175   ],
2176 [
2177   $HS_CPP_CMD -x c /dev/null -dM -E > conftest.txt 2>&1
2178   if grep "__clang__" conftest.txt >/dev/null 2>&1; then
2179     HS_CPP_ARGS="-E -undef -traditional -Wno-invalid-pp-token -Wno-unicode -Wno-trigraphs"
2180   else
2181       $HS_CPP_CMD  -v > conftest.txt 2>&1
2182       if  grep "gcc" conftest.txt >/dev/null 2>&1; then
2183           HS_CPP_ARGS="-E -undef -traditional"
2184         else
2185           $HS_CPP_CMD  --version > conftest.txt 2>&1
2186           if grep "cpphs" conftest.txt >/dev/null 2>&1; then
2187             HS_CPP_ARGS="--cpp -traditional"
2188           else
2189             AC_MSG_WARN([configure can't recognize your CPP program, you may need to set --with-hs-cpp-flags=FLAGS explicitly])
2190             HS_CPP_ARGS=""
2191           fi
2192       fi
2193   fi
2194   ]
2195 )
2196
2197 $1=$HS_CPP_CMD
2198 $2=$HS_CPP_ARGS
2199
2200 ])
2201
2202 # FP_BFD_SUPPORT()
2203 # ----------------------
2204 # whether to use libbfd for debugging RTS
2205 AC_DEFUN([FP_BFD_SUPPORT], [
2206     AC_ARG_ENABLE(bfd-debug,
2207         [AC_HELP_STRING([--enable-bfd-debug],
2208               [Enable symbol resolution for -debug rts ('+RTS -Di') via binutils' libbfd [default=no]])],
2209         [
2210             # don't pollute general LIBS environment
2211             save_LIBS="$LIBS"
2212             AC_CHECK_HEADERS([bfd.h])
2213             dnl ** check whether this machine has BFD and libiberty installed (used for debugging)
2214             dnl    the order of these tests matters: bfd needs libiberty
2215             AC_CHECK_LIB(iberty, xmalloc)
2216             dnl 'bfd_init' is a rare non-macro in libbfd
2217             AC_CHECK_LIB(bfd,    bfd_init)
2218
2219             AC_TRY_LINK([#include <bfd.h>],
2220                         [
2221                                 /* mimic our rts/Printer.c */
2222                                 bfd* abfd;
2223                                 const char * name;
2224                                 char **matching;
2225
2226                                 name = "some.executable";
2227                                 bfd_init();
2228                                 abfd = bfd_openr(name, "default");
2229                                 bfd_check_format_matches (abfd, bfd_object, &matching);
2230                                 {
2231                                     long storage_needed;
2232                                     storage_needed = bfd_get_symtab_upper_bound (abfd);
2233                                 }
2234                                 {
2235                                     asymbol **symbol_table;
2236                                     long number_of_symbols;
2237                                     symbol_info info;
2238
2239                                     number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
2240                                     bfd_get_symbol_info(abfd,symbol_table[0],&info);
2241                                 }
2242                         ],
2243                         [],dnl bfd seems to work
2244                         [AC_MSG_ERROR([can't use 'bfd' library])])
2245             LIBS="$save_LIBS"
2246         ]
2247     )
2248 ])
2249
2250
2251 # FP_CC_LINKER_FLAG_TRY()
2252 # --------------------
2253 # Try a particular linker to see whether we can use it. In particular, determine
2254 # whether we can convince gcc to use it via a -fuse-ld=... flag.
2255 #
2256 # $1 = the name of the linker to try
2257 # $2 = the variable to set with the appropriate GHC flag if the linker is
2258 # found to be usable
2259 AC_DEFUN([FP_CC_LINKER_FLAG_TRY], [
2260     AC_MSG_CHECKING([whether C compiler supports -fuse-ld=$1])
2261     echo 'int main(void) {return 0;}' > conftest.c
2262     if $CC -o conftest.o -fuse-ld=$1 conftest.c > /dev/null 2>&1
2263     then
2264         $2="-fuse-ld=$1"
2265         AC_MSG_RESULT([yes])
2266     else
2267         AC_MSG_RESULT([no])
2268     fi
2269     rm -f conftest.c conftest.o
2270 ])
2271
2272 # FIND_LD
2273 # ---------
2274 # Find the version of `ld` to use and figure out how to get gcc to use it for
2275 # linking (if --enable-ld-override is enabled). This is used in both in the top
2276 # level configure.ac and in distrib/configure.ac.in.
2277 #
2278 # $1 = the platform
2279 # $2 = the variable to set with GHC options to configure gcc to use the chosen linker
2280 #
2281 AC_DEFUN([FIND_LD],[
2282     AC_ARG_ENABLE(ld-override,
2283       [AC_HELP_STRING([--disable-ld-override],
2284         [Prevent GHC from overriding the default linker used by gcc. If ld-override is enabled GHC will try to tell gcc to use whichever linker is selected by the LD environment variable. [default=override enabled]])],
2285       [],
2286       [enable_ld_override=yes])
2287
2288     if test "x$enable_ld_override" = "xyes"; then
2289         TmpLd="$LD" # In case the user set LD
2290         AC_CHECK_TARGET_TOOLS([TmpLd], [ld.lld ld.gold ld])
2291
2292         out=`$TmpLd --version`
2293         case $out in
2294           "GNU ld"*)   FP_CC_LINKER_FLAG_TRY(bfd, $2) ;;
2295           "GNU gold"*) FP_CC_LINKER_FLAG_TRY(gold, $2) ;;
2296           "LLD"*)      FP_CC_LINKER_FLAG_TRY(lld, $2) ;;
2297           *) AC_MSG_NOTICE([unknown linker version $out]) ;;
2298         esac
2299         if test "z$$2" = "z"; then
2300             AC_MSG_NOTICE([unable to convince '$CC' to use linker '$TmpLd'])
2301             AC_CHECK_TARGET_TOOL([LD], [ld])
2302         else
2303             LD="$TmpLd"
2304         fi
2305    else
2306         AC_CHECK_TARGET_TOOL([LD], [ld])
2307    fi
2308
2309    CHECK_LD_COPY_BUG([$1])
2310 ])
2311
2312 # LocalWords:  fi