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