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