3 AC_DEFUN([ADD_GHC_TOOLCHAIN_ARG],
7 echo "--$1=$x" >> acargs
12 dnl $2 first variable to try
13 dnl $3 variable to add if the first variable is empty
14 AC_DEFUN([ADD_GHC_TOOLCHAIN_ARG_CHOOSE],
17 ADD_GHC_TOOLCHAIN_ARG([$1],[$3])
19 ADD_GHC_TOOLCHAIN_ARG([$1],[$2])
23 AC_DEFUN([ENABLE_GHC_TOOLCHAIN_ARG],
25 if test "$2" = "YES"; then
26 echo "--enable-$1" >> acargs
27 elif test "$2" = "yes"; then
28 echo "--enable-$1" >> acargs
29 elif test "$2" = "NO"; then
30 echo "--disable-$1" >> acargs
31 elif test "$2" = "no"; then
32 echo "--disable-$1" >> acargs
36 AC_DEFUN([ENABLE_GHC_TOOLCHAIN_NOT_ARG],
38 if test "$2" = "NO"; then
39 echo "--enable-$1" >> acargs
40 elif test "$2" = "YES"; then
41 echo "--disable-$1" >> acargs
42 elif test "$2" = "no"; then
43 echo "--enable-$1" >> acargs
44 elif test "$2" = "yes"; then
45 echo "--disable-$1" >> acargs
49 AC_DEFUN([INVOKE_GHC_TOOLCHAIN],
56 # For now, we don't 'exit' even if ghc-toolchain fails. We don't want to
57 # fail configure due to it, since the target file is still being generated by configure.
58 "$GHC_TOOLCHAIN_BIN" -v2 "[$]@"
62 dnl $1 is the path to the directory where to put the configured default.host.target.ghc-toolchain and default.target.ghc-toolchain
63 AC_DEFUN([FIND_GHC_TOOLCHAIN],
66 # (1) Configure a toolchain for the build and host platform (we require that BUILD=HOST, so one toolchain suffices)
68 echo "--triple=$HostPlatform" >> acargs
69 echo "--output=$1/default.host.target.ghc-toolchain" >> acargs
70 echo "--cc=$CC_STAGE0" >> acargs
71 echo "--cc-link=$CC_STAGE0" >> acargs
72 echo "--ar=$AR_STAGE0" >> acargs
73 dnl The remaining tools we don't configure for the host.
74 dnl See Note [The dummy values in the HOST target description]
76 INVOKE_GHC_TOOLCHAIN()
78 # (2) Configure a toolchain for the target platform (the toolchain is based
79 # on the triple (or manually specified), and runs on the platform
80 # configuring it and produces code for the given target)
81 # We might not find the correct toolchain, and fallback to the default
82 # toolchain. We should handle it more graciously.
84 # We pass the paths to the programs found by configure.
85 # The flags for the toolchain configured by ghc-toolchain will still be
86 # validated against those configured by configure, but ghc-toolchain
87 # doesn't take into account things like environment variables or bundled
88 # (windows) toolchains, so we explicitly pass them as arguments here.
89 # ghc-toolchain is still able to find programs if not explicitly given in
90 # the usual system locations, including the PATH, we are just explicit when
91 # calling it through configure.
94 echo "--triple=$target" >> acargs
95 echo "--output=$1/default.target.ghc-toolchain" >> acargs
96 echo "--llvm-triple=$LlvmTarget" >> acargs
97 echo "--cc=$CC" >> acargs
98 echo "--cxx=$CXX" >> acargs
99 echo "--cpp=$CPPCmd" >> acargs
100 echo "--hs-cpp=$HaskellCPPCmd" >> acargs
101 echo "--js-cpp=$JavaScriptCPPCmd" >> acargs
102 echo "--cmm-cpp=$CmmCPPCommand" >> acargs
103 echo "--cc-link=$CC" >> acargs
104 echo "--ar=$AR" >> acargs
105 echo "--ranlib=$RANLIB" >> acargs
106 echo "--nm=$NM" >> acargs
107 echo "--merge-objs=$MergeObjsCmd" >> acargs
108 echo "--readelf=$READELF" >> acargs
109 echo "--windres=$WindresCmd" >> acargs
111 if test -n "$USER_LD"; then
112 echo "--ld=$USER_LD" >> acargs
115 ENABLE_GHC_TOOLCHAIN_NOT_ARG([locally-executable], [$CrossCompiling])
116 ENABLE_GHC_TOOLCHAIN_ARG([unregisterised], [$Unregisterised])
117 ENABLE_GHC_TOOLCHAIN_ARG([tables-next-to-code], [$TablesNextToCode])
118 ENABLE_GHC_TOOLCHAIN_ARG([ld-override], [$enable_ld_override])
119 ENABLE_GHC_TOOLCHAIN_ARG([libffi-adjustors], [$UseLibffiForAdjustors])
121 dnl We store USER_* variants of all user-specified flags to pass them over to ghc-toolchain.
122 ADD_GHC_TOOLCHAIN_ARG_CHOOSE([cc-opt], [$USER_CONF_CC_OPTS_STAGE2], [$USER_CFLAGS])
123 ADD_GHC_TOOLCHAIN_ARG_CHOOSE([cc-link-opt], [$USER_CONF_GCC_LINKER_OPTS_STAGE2], [$USER_LDFLAGS])
124 ADD_GHC_TOOLCHAIN_ARG([cc-link-opt], [$USER_LIBS])
125 ADD_GHC_TOOLCHAIN_ARG_CHOOSE([cxx-opt], [$USER_CONF_CXX_OPTS_STAGE2], [$USER_CXXFLAGS])
126 ADD_GHC_TOOLCHAIN_ARG([cpp-opt], [$USER_CPP_ARGS])
127 ADD_GHC_TOOLCHAIN_ARG([hs-cpp-opt], [$USER_HS_CPP_ARGS])
128 ADD_GHC_TOOLCHAIN_ARG([js-cpp-opt], [$USER_JS_CPP_ARGS])
129 ADD_GHC_TOOLCHAIN_ARG([cmm-cpp-opt], [$USER_CMM_CPP_ARGS])
131 INVOKE_GHC_TOOLCHAIN()
133 dnl Note: if we weren't passing the paths to the programs explicitly, to make
134 dnl ghc-toolchain use the bundled windows toolchain, simply add it to the search PATH
139 dnl * YES means we're calling GHC_TOOLCHAIN from the bindist configure script,
140 dnl and that ghc-toolchain is already an available binary
141 dnl * NO means we're calling GHC_TOOLCHAIN from the source tree configure script,
142 dnl and that we must compile ghc-toolchain before invoking it
143 AC_DEFUN([FIND_GHC_TOOLCHAIN_BIN],[
146 # We're configuring the bindist, and the binary is already available
147 GHC_TOOLCHAIN_BIN="bin/ghc-toolchain-bin"
150 # We're in the source tree, so compile ghc-toolchain
152 -ilibraries/ghc-platform/src -iutils/ghc-toolchain/src \
153 -XNoImplicitPrelude \
154 -odir actmp-ghc-toolchain -hidir actmp-ghc-toolchain \
156 utils/ghc-toolchain/exe/Main.hs -o acghc-toolchain || AC_MSG_ERROR([Could not compile ghc-toolchain])
157 GHC_TOOLCHAIN_BIN="./acghc-toolchain"
160 AC_MSG_ERROR([In m4/ghc_toolchain.m4, expecting $2 to be either YES or NO.])
166 dnl Note [ghc-toolchain consistency checking]
167 dnl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
168 dnl ghc-toolchain is the brand new way (2023) to configure toolchains for GHC,
169 dnl but this novelty musn't break user's installations, so we still
170 dnl conservatively use the toolchains configured by configure (see also m4/tprep_target_file.m4).
172 dnl However, we already ship and run ghc-toolchain at configure time to /validate/ ghc-toolchain:
173 dnl * PREP_TARGET_FILE substitutes the toolchain into default.target.in and default.host.target.in
174 dnl * FIND_GHC_TOOLCHAIN generates a target description file through ghc-toolchain
175 dnl * VALIDATE_GHC_TOOLCHAIN compares the output of the two, warning about the differences.
177 dnl This is crucial to validate ghc-toolchain and preemptively fix bugs before it is the default.
179 dnl (And the configure flag --enable-ghc-toolchain makes hadrian use the target
180 dnl files generated by ghc-toolchain instead).
182 dnl $1 like "default.target"
183 dnl $2 like "default.target.ghc-toolchain"
184 AC_DEFUN([VALIDATE_GHC_TOOLCHAIN],[
187 "$GHC_TOOLCHAIN_BIN" format --input="$1" --output="$o1"
188 "$GHC_TOOLCHAIN_BIN" format --input="$2" --output="$o2"
189 diff_output=`diff "$o1" "$o2" 2>&1`
191 if test -z "$diff_output"; then
195 There are some differences between the toolchain configured by "configure" ($1) and the toolchain configured by the "ghc-toolchain" program ($2).
199 Don't worry! This won't affect your ghc in any way.
200 However, in a near future, we will move to configuring toolchains with "ghc-toolchain" by default, so you might have discovered a future bug.
201 In light of it, if you've spotted this difference, please report a GHC bug at https://www.haskell.org/ghc/reportabug])
203 case "$EnableStrictGhcToolchainCheck" in
205 AC_MSG_ERROR([Failing due to --enable-strict-ghc-toolchain-check])
210 dnl Note [The dummy values in the HOST target description]
211 dnl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
212 dnl In configure, we don't configure nearly as much tools for
213 dnl the HOST toolchain as we do for the TARGET toolchain. This is because
214 dnl Hadrian only depends on certain properties and tools of the HOST toolchain,
215 dnl and, ultimately, the shipped GHC has in `settings` the TARGET toolchain and
216 dnl properties. (In contrast, ghc-toolchain can as easily configure 1
217 dnl toolchain as it can 100)
219 dnl Unfortunately, we need to produce a valid Target value to write to default.host.target.
220 dnl Since we don't configure the values required to substitute into the
221 dnl toolchain, we simply use /dummy/ values, as conservatively as possible.
222 dnl Regardless of the conservative values, we assume that hadrian will never
223 dnl look at these settings, as they previously didn't exist.
225 dnl In practice, Hadrian should only access the *_STAGE0 settings that were
226 dnl available before the ghc-toolchain: Toolchain Selection commit.