1{
2 version,
3 sha256,
4 url ? "https://downloads.haskell.org/ghc/${version}/ghc-${version}-src.tar.xz",
5}:
6
7{
8 lib,
9 stdenv,
10 pkgsBuildTarget,
11 pkgsHostTarget,
12 buildPackages,
13 targetPackages,
14
15 # build-tools
16 bootPkgs,
17 autoconf,
18 automake,
19 coreutils,
20 fetchpatch,
21 fetchurl,
22 perl,
23 python3,
24 m4,
25 sphinx,
26 xattr,
27 autoSignDarwinBinariesHook,
28 bash,
29
30 libiconv ? null,
31 ncurses,
32
33 # GHC can be built with system libffi or a bundled one.
34 libffi ? null,
35
36 useLLVM ? !(import ./common-have-ncg.nix { inherit lib stdenv version; }),
37
38 # LLVM is conceptually a run-time-only dependency, but for
39 # non-x86, we need LLVM to bootstrap later stages, so it becomes a
40 # build-time dependency too.
41 buildTargetLlvmPackages,
42 llvmPackages,
43
44 # If enabled, GHC will be built with the GPL-free but slightly slower native
45 # bignum backend instead of the faster but GPLed gmp backend.
46 enableNativeBignum ?
47 !(lib.meta.availableOn stdenv.hostPlatform gmp && lib.meta.availableOn stdenv.targetPlatform gmp),
48 gmp,
49
50 # If enabled, use -fPIC when compiling static libs.
51 enableRelocatedStaticLibs ? stdenv.targetPlatform != stdenv.hostPlatform,
52
53 # Exceeds Hydra output limit (at the time of writing ~3GB) when cross compiled to riscv64.
54 # A riscv64 cross-compiler fits into the limit comfortably.
55 enableProfiledLibs ? !stdenv.hostPlatform.isRiscV64,
56
57 # Whether to build dynamic libs for the standard library (on the target
58 # platform). Static libs are always built.
59 enableShared ? with stdenv.targetPlatform; !isWindows && !useiOSPrebuilt && !isStatic,
60
61 # Whether to build terminfo.
62 enableTerminfo ?
63 !(
64 stdenv.targetPlatform.isWindows
65 # terminfo can't be built for cross
66 || (stdenv.buildPlatform != stdenv.hostPlatform)
67 || (stdenv.hostPlatform != stdenv.targetPlatform)
68 ),
69
70 # Enable NUMA support in RTS
71 enableNuma ? lib.meta.availableOn stdenv.targetPlatform numactl,
72 numactl,
73
74 # What flavour to build. An empty string indicates no
75 # specific flavour and falls back to ghc default values.
76 ghcFlavour ? lib.optionalString (stdenv.targetPlatform != stdenv.hostPlatform) (
77 if useLLVM then "perf-cross" else "perf-cross-ncg"
78 ),
79
80 # Whether to build sphinx documentation.
81 enableDocs ? (
82 # Docs disabled if we are building on musl because it's a large task to keep
83 # all `sphinx` dependencies building in this environment.
84 !stdenv.buildPlatform.isMusl
85 ),
86
87 enableHaddockProgram ?
88 # Disabled for cross; see note [HADDOCK_DOCS].
89 (stdenv.buildPlatform == stdenv.hostPlatform && stdenv.targetPlatform == stdenv.hostPlatform),
90
91 # Whether to disable the large address space allocator
92 # necessary fix for iOS: https://www.reddit.com/r/haskell/comments/4ttdz1/building_an_osxi386_to_iosarm64_cross_compiler/d5qvd67/
93 disableLargeAddressSpace ? stdenv.targetPlatform.isiOS,
94
95 # Whether to build an unregisterised version of GHC.
96 # GHC will normally auto-detect whether it can do a registered build, but this
97 # option will force it to do an unregistered build when set to true.
98 # See https://gitlab.haskell.org/ghc/ghc/-/wikis/building/unregisterised
99 # Registerised RV64 compiler produces programs that segfault
100 # See https://gitlab.haskell.org/ghc/ghc/-/issues/23957
101 enableUnregisterised ? stdenv.hostPlatform.isRiscV64 || stdenv.targetPlatform.isRiscV64,
102}:
103
104assert !enableNativeBignum -> gmp != null;
105
106# Cross cannot currently build the `haddock` program for silly reasons,
107# see note [HADDOCK_DOCS].
108assert
109 (stdenv.buildPlatform != stdenv.hostPlatform || stdenv.targetPlatform != stdenv.hostPlatform)
110 -> !enableHaddockProgram;
111
112# GHC does not support building when all 3 platforms are different.
113assert stdenv.buildPlatform == stdenv.hostPlatform || stdenv.hostPlatform == stdenv.targetPlatform;
114
115let
116 inherit (stdenv) buildPlatform hostPlatform targetPlatform;
117
118 # TODO(@Ericson2314) Make unconditional
119 targetPrefix = lib.optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-";
120
121 buildMK = ''
122 BuildFlavour = ${ghcFlavour}
123 ifneq \"\$(BuildFlavour)\" \"\"
124 include mk/flavours/\$(BuildFlavour).mk
125 endif
126 BUILD_SPHINX_HTML = ${if enableDocs then "YES" else "NO"}
127 BUILD_SPHINX_PDF = NO
128
129 WITH_TERMINFO = ${if enableTerminfo then "YES" else "NO"}
130 ''
131 +
132 # Note [HADDOCK_DOCS]:
133 # Unfortunately currently `HADDOCK_DOCS` controls both whether the `haddock`
134 # program is built (which we generally always want to have a complete GHC install)
135 # and whether it is run on the GHC sources to generate hyperlinked source code
136 # (which is impossible for cross-compilation); see:
137 # https://gitlab.haskell.org/ghc/ghc/-/issues/20077
138 # This implies that currently a cross-compiled GHC will never have a `haddock`
139 # program, so it can never generate haddocks for any packages.
140 # If this is solved in the future, we'd like to unconditionally
141 # build the haddock program (removing the `enableHaddockProgram` option).
142 ''
143 HADDOCK_DOCS = ${if enableHaddockProgram then "YES" else "NO"}
144 # Build haddocks for boot packages with hyperlinking
145 EXTRA_HADDOCK_OPTS += --hyperlinked-source --quickjump
146
147 DYNAMIC_GHC_PROGRAMS = ${if enableShared then "YES" else "NO"}
148 BIGNUM_BACKEND = ${if enableNativeBignum then "native" else "gmp"}
149 ''
150 + lib.optionalString (targetPlatform != hostPlatform) ''
151 Stage1Only = ${if targetPlatform.system == hostPlatform.system then "NO" else "YES"}
152 CrossCompilePrefix = ${targetPrefix}
153 ''
154 + lib.optionalString (!enableProfiledLibs) ''
155 BUILD_PROF_LIBS = NO
156 ''
157 +
158 # -fexternal-dynamic-refs apparently (because it's not clear from the documentation)
159 # makes the GHC RTS able to load static libraries, which may be needed for TemplateHaskell.
160 # This solution was described in https://www.tweag.io/blog/2020-09-30-bazel-static-haskell
161 lib.optionalString enableRelocatedStaticLibs ''
162 GhcLibHcOpts += -fPIC -fexternal-dynamic-refs
163 GhcRtsHcOpts += -fPIC -fexternal-dynamic-refs
164 ''
165 + lib.optionalString targetPlatform.useAndroidPrebuilt ''
166 EXTRA_CC_OPTS += -std=gnu99
167 '';
168
169 # Splicer will pull out correct variations
170 libDeps =
171 platform:
172 lib.optional enableTerminfo ncurses
173 ++ [ libffi ]
174 ++ lib.optional (!enableNativeBignum) gmp
175 ++ lib.optional (platform.libc != "glibc" && !targetPlatform.isWindows) libiconv;
176
177 # TODO(@sternenseemann): is buildTarget LLVM unnecessary?
178 # GHC doesn't seem to have {LLC,OPT}_HOST
179 toolsForTarget = [
180 pkgsBuildTarget.targetPackages.stdenv.cc
181 ]
182 ++ lib.optional useLLVM buildTargetLlvmPackages.llvm;
183
184 buildCC = buildPackages.stdenv.cc;
185 targetCC = builtins.head toolsForTarget;
186 installCC = pkgsHostTarget.targetPackages.stdenv.cc;
187
188 # toolPath calculates the absolute path to the name tool associated with a
189 # given `stdenv.cc` derivation, i.e. it picks the correct derivation to take
190 # the tool from (cc, cc.bintools, cc.bintools.bintools) and adds the correct
191 # subpath of the tool.
192 toolPath =
193 name: cc:
194 let
195 tools =
196 {
197 "cc" = cc;
198 "c++" = cc;
199 as = cc.bintools;
200
201 ar = cc.bintools;
202 ranlib = cc.bintools;
203 nm = cc.bintools;
204 readelf = cc.bintools;
205 objdump = cc.bintools;
206
207 ld = cc.bintools;
208 "ld.gold" = cc.bintools;
209
210 otool = cc.bintools.bintools;
211
212 # GHC needs install_name_tool on all darwin platforms. The same one can
213 # be used on both platforms. It is safe to use with linker-generated
214 # signatures because it will update the signatures automatically after
215 # modifying the target binary.
216 install_name_tool = cc.bintools.bintools;
217
218 # strip on darwin is wrapped to enable deterministic mode.
219 strip =
220 # TODO(@sternenseemann): also use wrapper if linker == "bfd" or "gold"
221 if stdenv.targetPlatform.isDarwin then cc.bintools else cc.bintools.bintools;
222
223 # clang is used as an assembler on darwin with the LLVM backend
224 clang = cc;
225 }
226 .${name};
227 in
228 getToolExe tools name;
229
230 # targetPrefix aware lib.getExe'
231 getToolExe = drv: name: lib.getExe' drv "${drv.targetPrefix or ""}${name}";
232
233 # Use gold either following the default, or to avoid the BFD linker due to some bugs / perf issues.
234 # But we cannot avoid BFD when using musl libc due to https://sourceware.org/bugzilla/show_bug.cgi?id=23856
235 # see #84670 and #49071 for more background.
236 useLdGold =
237 targetPlatform.linker == "gold"
238 || (
239 targetPlatform.linker == "bfd"
240 && (targetCC.bintools.bintools.hasGold or false)
241 && !targetPlatform.isMusl
242 );
243
244 # Makes debugging easier to see which variant is at play in `nix-store -q --tree`.
245 variantSuffix = lib.concatStrings [
246 (lib.optionalString stdenv.hostPlatform.isMusl "-musl")
247 (lib.optionalString enableNativeBignum "-native-bignum")
248 ];
249
250 # These libraries are library dependencies of the standard libraries bundled
251 # by GHC (core libs) users will link their compiled artifacts again. Thus,
252 # they should be taken from targetPackages.
253 #
254 # We need to use pkgsHostTarget if we are cross compiling a native GHC compiler,
255 # though (when native compiling GHC, pkgsHostTarget == targetPackages):
256 #
257 # 1. targetPackages would be empty(-ish) in this situation since we can't
258 # execute cross compiled compilers in order to obtain the libraries
259 # that would be in targetPackages.
260 # 2. pkgsHostTarget is fine to use since hostPlatform == targetPlatform in this
261 # situation.
262 # 3. The core libs used by the final GHC (stage 2) for user artifacts are also
263 # used to build stage 2 GHC itself, i.e. the core libs are both host and
264 # target.
265 targetLibs = {
266 inherit (if hostPlatform != targetPlatform then targetPackages else pkgsHostTarget)
267 gmp
268 libffi
269 ncurses
270 numactl
271 ;
272 };
273
274in
275
276stdenv.mkDerivation (
277 rec {
278 pname = "${targetPrefix}ghc${variantSuffix}";
279 inherit version;
280
281 src = fetchurl {
282 inherit url sha256;
283 };
284
285 enableParallelBuilding = true;
286
287 outputs = [
288 "out"
289 "doc"
290 ];
291
292 patches = [
293 # Determine size of time related types using hsc2hs instead of assuming CLong.
294 # Prevents failures when e.g. stat(2)ing on 32bit systems with 64bit time_t etc.
295 # https://github.com/haskell/ghcup-hs/issues/1107
296 # https://gitlab.haskell.org/ghc/ghc/-/issues/25095
297 # Note that in normal situations this shouldn't be the case since nixpkgs
298 # doesn't set -D_FILE_OFFSET_BITS=64 and friends (yet).
299 (fetchpatch {
300 name = "unix-fix-ctimeval-size-32-bit.patch";
301 url = "https://github.com/haskell/unix/commit/8183e05b97ce870dd6582a3677cc82459ae566ec.patch";
302 sha256 = "17q5yyigqr5kxlwwzb95sx567ysfxlw6bp3j4ji20lz0947aw6gv";
303 stripLen = 1;
304 extraPrefix = "libraries/unix/";
305 })
306
307 # Fix docs build with Sphinx >= 7 https://gitlab.haskell.org/ghc/ghc/-/issues/24129
308 ./docs-sphinx-7.patch
309
310 # Correctly record libnuma's library and include directories in the
311 # package db. This fixes linking whenever stdenv and propagation won't
312 # quite pass the correct -L flags to the linker, e.g. when using GHC
313 # outside of stdenv/nixpkgs or build->build compilation in pkgsStatic.
314 ./ghc-9.4-rts-package-db-libnuma-dirs.patch
315 ]
316
317 # Before GHC 9.6, GHC, when used to compile C sources (i.e. to drive the CC), would first
318 # invoke the C compiler to generate assembly and later call the assembler on the result of
319 # that operation. Unfortunately, that is brittle in a lot of cases, e.g. when using mismatched
320 # CC / assembler (https://gitlab.haskell.org/ghc/ghc/-/merge_requests/12005). This issue
321 # does not affect us. However, LLVM 18 introduced a check in clang that makes sure no
322 # non private labels occur between .cfi_startproc and .cfi_endproc which causes the
323 # assembly that the same version (!) of clang generates from rts/StgCRun.c to be rejected.
324 # This causes GHC to fail compilation on mach-o platforms ever since we upgraded to
325 # LLVM 19.
326 #
327 # clang compiles the same file without issues whithout the roundtrip via assembly. Thus,
328 # the solution is to backport those changes from GHC 9.6 that skip the intermediate
329 # assembly step.
330 #
331 # https://gitlab.haskell.org/ghc/ghc/-/issues/25608#note_622589
332 # https://gitlab.haskell.org/ghc/ghc/-/merge_requests/6877
333 ++ [
334 # Need to use this patch so the next one applies, passes file location info to the cc phase
335 (fetchpatch {
336 name = "ghc-add-location-to-cc-phase.patch";
337 url = "https://gitlab.haskell.org/ghc/ghc/-/commit/4a7256a75af2fc0318bef771a06949ffb3939d5a.patch";
338 hash = "sha256-DnTI+i1zMebeWvw75D59vMaEEBb2Nr9HusxTyhmdy2M=";
339 })
340 # Makes Cc phase directly generate object files instead of assembly
341 (fetchpatch {
342 name = "ghc-cc-directly-emit-object.patch";
343 url = "https://gitlab.haskell.org/ghc/ghc/-/commit/96811ba491495b601ec7d6a32bef8563b0292109.patch";
344 hash = "sha256-G8u7/MK/tGOEN8Wxccxj/YIOP7mL2G9Co1WKdHXOo6I=";
345 })
346 ]
347
348 ++ [
349 # Don't generate code that doesn't compile when --enable-relocatable is passed to Setup.hs
350 # Can be removed if the Cabal library included with ghc backports the linked fix
351 (fetchpatch {
352 url = "https://github.com/haskell/cabal/commit/6c796218c92f93c95e94d5ec2d077f6956f68e98.patch";
353 stripLen = 1;
354 extraPrefix = "libraries/Cabal/";
355 sha256 = "sha256-yRQ6YmMiwBwiYseC5BsrEtDgFbWvst+maGgDtdD0vAY=";
356 })
357 ]
358
359 # Fixes stack overrun in rts which crashes an process whenever
360 # freeHaskellFunPtr is called with nixpkgs' hardening flags.
361 # https://gitlab.haskell.org/ghc/ghc/-/issues/25485
362 # https://gitlab.haskell.org/ghc/ghc/-/merge_requests/13599
363 # TODO: patch doesn't apply for < 9.4, but may still be necessary?
364 ++ [
365 (fetchpatch {
366 name = "ghc-rts-adjustor-fix-i386-stack-overrun.patch";
367 url = "https://gitlab.haskell.org/ghc/ghc/-/commit/39bb6e583d64738db51441a556d499aa93a4fc4a.patch";
368 sha256 = "0w5fx413z924bi2irsy1l4xapxxhrq158b5gn6jzrbsmhvmpirs0";
369 })
370 ]
371
372 ++ lib.optionals (stdenv.targetPlatform.isDarwin && stdenv.targetPlatform.isAarch64) [
373 # Prevent the paths module from emitting symbols that we don't use
374 # when building with separate outputs.
375 #
376 # These cause problems as they're not eliminated by GHC's dead code
377 # elimination on aarch64-darwin. (see
378 # https://github.com/NixOS/nixpkgs/issues/140774 for details).
379 ./Cabal-at-least-3.6-paths-fix-cycle-aarch64-darwin.patch
380 ];
381
382 postPatch = "patchShebangs .";
383
384 # GHC needs the locale configured during the Haddock phase.
385 LANG = "en_US.UTF-8";
386
387 # GHC is a bit confused on its cross terminology.
388 # TODO(@sternenseemann): investigate coreutils dependencies and pass absolute paths
389 preConfigure = ''
390 for env in $(env | grep '^TARGET_' | sed -E 's|\+?=.*||'); do
391 export "''${env#TARGET_}=''${!env}"
392 done
393 # Stage0 (build->build) which builds stage 1
394 export GHC="${bootPkgs.ghc}/bin/ghc"
395 # GHC is a bit confused on its cross terminology, as these would normally be
396 # the *host* tools.
397 export CC="${toolPath "cc" targetCC}"
398 export CXX="${toolPath "c++" targetCC}"
399 # Use gold to work around https://sourceware.org/bugzilla/show_bug.cgi?id=16177
400 export LD="${toolPath "ld${lib.optionalString useLdGold ".gold"}" targetCC}"
401 export AS="${toolPath "as" targetCC}"
402 export AR="${toolPath "ar" targetCC}"
403 export NM="${toolPath "nm" targetCC}"
404 export RANLIB="${toolPath "ranlib" targetCC}"
405 export READELF="${toolPath "readelf" targetCC}"
406 export STRIP="${toolPath "strip" targetCC}"
407 export OBJDUMP="${toolPath "objdump" targetCC}"
408 ''
409 + lib.optionalString (stdenv.targetPlatform.linker == "cctools") ''
410 export OTOOL="${toolPath "otool" targetCC}"
411 export INSTALL_NAME_TOOL="${toolPath "install_name_tool" targetCC}"
412 ''
413 + lib.optionalString useLLVM ''
414 export LLC="${getToolExe buildTargetLlvmPackages.llvm "llc"}"
415 export OPT="${getToolExe buildTargetLlvmPackages.llvm "opt"}"
416 ''
417 + lib.optionalString (useLLVM && stdenv.targetPlatform.isDarwin) ''
418 # LLVM backend on Darwin needs clang: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/codegens.html#llvm-code-generator-fllvm
419 # The executable we specify via $CLANG is used as an assembler (exclusively, it seems, but this isn't
420 # clarified in any user facing documentation). As such, it'll be called on assembly produced by $CC
421 # which usually comes from the darwin stdenv. To prevent a situation where $CLANG doesn't understand
422 # the assembly it is given, we need to make sure that it matches the LLVM version of $CC if possible.
423 # It is unclear (at the time of writing 2024-09-01) whether $CC should match the LLVM version we use
424 # for llc and opt which would require using a custom darwin stdenv for targetCC.
425 export CLANG="${
426 if targetCC.isClang then
427 toolPath "clang" targetCC
428 else
429 getToolExe buildTargetLlvmPackages.clang "clang"
430 }"
431 ''
432 + ''
433 # No need for absolute paths since these tools only need to work during the build
434 export CC_STAGE0="$CC_FOR_BUILD"
435 export LD_STAGE0="$LD_FOR_BUILD"
436 export AR_STAGE0="$AR_FOR_BUILD"
437
438 echo -n "${buildMK}" > mk/build.mk
439 sed -i -e 's|-isysroot /Developer/SDKs/MacOSX10.5.sdk||' configure
440 ''
441 + lib.optionalString (stdenv.buildPlatform.libc == "glibc") ''
442 export LOCALE_ARCHIVE="${buildPackages.glibcLocales}/lib/locale/locale-archive"
443 ''
444 + lib.optionalString (!stdenv.hostPlatform.isDarwin) ''
445 export NIX_LDFLAGS+=" -rpath $out/lib/ghc-${version}"
446 ''
447 + lib.optionalString stdenv.hostPlatform.isDarwin ''
448 export NIX_LDFLAGS+=" -no_dtrace_dof"
449 ''
450 + lib.optionalString (stdenv.hostPlatform.isDarwin) ''
451
452 # GHC tries the host xattr /usr/bin/xattr by default which fails since it expects python to be 2.7
453 export XATTR=${lib.getBin xattr}/bin/xattr
454 ''
455 + lib.optionalString targetPlatform.useAndroidPrebuilt ''
456 sed -i -e '5i ,("armv7a-unknown-linux-androideabi", ("e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64", "cortex-a8", ""))' llvm-targets
457 ''
458 + lib.optionalString targetPlatform.isMusl ''
459 echo "patching llvm-targets for musl targets..."
460 echo "Cloning these existing '*-linux-gnu*' targets:"
461 grep linux-gnu llvm-targets | sed 's/^/ /'
462 echo "(go go gadget sed)"
463 sed -i 's,\(^.*linux-\)gnu\(.*\)$,\0\n\1musl\2,' llvm-targets
464 echo "llvm-targets now contains these '*-linux-musl*' targets:"
465 grep linux-musl llvm-targets | sed 's/^/ /'
466
467 echo "And now patching to preserve '-musleabi' as done with '-gnueabi'"
468 # (aclocal.m4 is actual source, but patch configure as well since we don't re-gen)
469 for x in configure aclocal.m4; do
470 substituteInPlace $x \
471 --replace '*-android*|*-gnueabi*)' \
472 '*-android*|*-gnueabi*|*-musleabi*)'
473 done
474 '';
475
476 # Although it is usually correct to pass --host, we don't do that here because
477 # GHC's usage of build, host, and target is non-standard.
478 # See https://gitlab.haskell.org/ghc/ghc/-/wikis/building/cross-compiling
479 # TODO(@Ericson2314): Always pass "--target" and always prefix.
480 configurePlatforms = [
481 "build"
482 ]
483 ++ lib.optional (buildPlatform != hostPlatform || targetPlatform != hostPlatform) "target";
484
485 # `--with` flags for libraries needed for RTS linker
486 configureFlags = [
487 "--datadir=$doc/share/doc/ghc"
488 ]
489 ++ lib.optionals enableTerminfo [
490 "--with-curses-includes=${lib.getDev targetLibs.ncurses}/include"
491 "--with-curses-libraries=${lib.getLib targetLibs.ncurses}/lib"
492 ]
493 ++ lib.optionals (libffi != null) [
494 "--with-system-libffi"
495 "--with-ffi-includes=${targetLibs.libffi.dev}/include"
496 "--with-ffi-libraries=${targetLibs.libffi.out}/lib"
497 ]
498 ++ lib.optionals (targetPlatform == hostPlatform && !enableNativeBignum) [
499 "--with-gmp-includes=${targetLibs.gmp.dev}/include"
500 "--with-gmp-libraries=${targetLibs.gmp.out}/lib"
501 ]
502 ++
503 lib.optionals
504 (targetPlatform == hostPlatform && hostPlatform.libc != "glibc" && !targetPlatform.isWindows)
505 [
506 "--with-iconv-includes=${libiconv}/include"
507 "--with-iconv-libraries=${libiconv}/lib"
508 ]
509 ++ lib.optionals (targetPlatform != hostPlatform) [
510 "--enable-bootstrap-with-devel-snapshot"
511 ]
512 ++ lib.optionals useLdGold [
513 "CFLAGS=-fuse-ld=gold"
514 "CONF_GCC_LINKER_OPTS_STAGE1=-fuse-ld=gold"
515 "CONF_GCC_LINKER_OPTS_STAGE2=-fuse-ld=gold"
516 ]
517 ++ lib.optionals (disableLargeAddressSpace) [
518 "--disable-large-address-space"
519 ]
520 ++ lib.optionals enableNuma [
521 "--enable-numa"
522 "--with-libnuma-includes=${lib.getDev targetLibs.numactl}/include"
523 "--with-libnuma-libraries=${lib.getLib targetLibs.numactl}/lib"
524 ]
525 ++ lib.optionals enableUnregisterised [
526 "--enable-unregisterised"
527 ];
528
529 # Make sure we never relax`$PATH` and hooks support for compatibility.
530 strictDeps = true;
531
532 # Don’t add -liconv to LDFLAGS automatically so that GHC will add it itself.
533 dontAddExtraLibs = true;
534
535 nativeBuildInputs = [
536 perl
537 autoconf
538 automake
539 m4
540 python3
541 bootPkgs.alex
542 bootPkgs.happy
543 bootPkgs.hscolour
544 bootPkgs.ghc-settings-edit
545 ]
546 ++ lib.optionals (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isAarch64) [
547 autoSignDarwinBinariesHook
548 ]
549 ++ lib.optionals enableDocs [
550 sphinx
551 ];
552
553 # Everything the stage0 compiler needs to build stage1: CC, bintools, extra libs.
554 # See also GHC, {CC,LD,AR}_STAGE0 in preConfigure.
555 depsBuildBuild = [
556 # N.B. We do not declare bootPkgs.ghc in any of the stdenv.mkDerivation
557 # dependency lists to prevent the bintools setup hook from adding ghc's
558 # lib directory to the linker flags. Instead we tell configure about it
559 # via the GHC environment variable.
560 buildCC
561 # stage0 builds terminfo unconditionally, so we always need ncurses
562 ncurses
563 ];
564 # For building runtime libs
565 depsBuildTarget = toolsForTarget;
566
567 # Prevent stage0 ghc from leaking into the final result. This was an issue
568 # with GHC 9.6.
569 disallowedReferences = [
570 bootPkgs.ghc
571 ];
572
573 buildInputs = [ bash ] ++ (libDeps hostPlatform);
574
575 # stage1 GHC doesn't need to link against libnuma, so it's target specific
576 depsTargetTarget = map lib.getDev (
577 libDeps targetPlatform ++ lib.optionals enableNuma [ targetLibs.numactl ]
578 );
579 depsTargetTargetPropagated = map (lib.getOutput "out") (
580 libDeps targetPlatform ++ lib.optionals enableNuma [ targetLibs.numactl ]
581 );
582
583 # required, because otherwise all symbols from HSffi.o are stripped, and
584 # that in turn causes GHCi to abort
585 stripDebugFlags = [ "-S" ] ++ lib.optional (!targetPlatform.isDarwin) "--keep-file-symbols";
586
587 checkTarget = "test";
588
589 # GHC cannot currently produce outputs that are ready for `-pie` linking.
590 # Thus, disable `pie` hardening, otherwise `recompile with -fPIE` errors appear.
591 # See:
592 # * https://github.com/NixOS/nixpkgs/issues/129247
593 # * https://gitlab.haskell.org/ghc/ghc/-/issues/19580
594 hardeningDisable = [
595 "format"
596 "pie"
597 ];
598
599 # big-parallel allows us to build with more than 2 cores on
600 # Hydra which already warrants a significant speedup
601 requiredSystemFeatures = [ "big-parallel" ];
602
603 # Install occasionally fails due to a race condition in minimal builds.
604 # > /nix/store/wyzpysxwgs3qpvmylm9krmfzh2plicix-coreutils-9.7/bin/install -c -m 755 -d "/nix/store/xzb3390rhvhg2a0cvzmrvjspw1d8nf8h-ghc-riscv64-unknown-linux-gnu-9.4.8/bin"
605 # > install: cannot create regular file '/nix/store/xzb3390rhvhg2a0cvzmrvjspw1d8nf8h-ghc-riscv64-unknown-linux-gnu-9.4.8/lib/ghc-9.4.8': No such file or directory
606 preInstall = ''
607 mkdir -p "$out/lib/${passthru.haskellCompilerName}"
608 '';
609
610 postInstall = ''
611 settingsFile="$out/lib/${targetPrefix}${passthru.haskellCompilerName}/settings"
612
613 # Make the installed GHC use the host->target tools.
614 ghc-settings-edit "$settingsFile" \
615 "C compiler command" "${toolPath "cc" installCC}" \
616 "Haskell CPP command" "${toolPath "cc" installCC}" \
617 "C++ compiler command" "${toolPath "c++" installCC}" \
618 "ld command" "${toolPath "ld${lib.optionalString useLdGold ".gold"}" installCC}" \
619 "Merge objects command" "${toolPath "ld${lib.optionalString useLdGold ".gold"}" installCC}" \
620 "ar command" "${toolPath "ar" installCC}" \
621 "ranlib command" "${toolPath "ranlib" installCC}"
622 ''
623 + lib.optionalString (stdenv.targetPlatform.linker == "cctools") ''
624 ghc-settings-edit "$settingsFile" \
625 "otool command" "${toolPath "otool" installCC}" \
626 "install_name_tool command" "${toolPath "install_name_tool" installCC}"
627 ''
628 + lib.optionalString useLLVM ''
629 ghc-settings-edit "$settingsFile" \
630 "LLVM llc command" "${getToolExe llvmPackages.llvm "llc"}" \
631 "LLVM opt command" "${getToolExe llvmPackages.llvm "opt"}"
632 ''
633 + lib.optionalString (useLLVM && stdenv.targetPlatform.isDarwin) ''
634 ghc-settings-edit "$settingsFile" \
635 "LLVM clang command" "${
636 # See comment for CLANG in preConfigure
637 if installCC.isClang then toolPath "clang" installCC else getToolExe llvmPackages.clang "clang"
638 }"
639 ''
640 + ''
641
642 # Install the bash completion file.
643 install -D -m 444 utils/completion/ghc.bash $out/share/bash-completion/completions/${targetPrefix}ghc
644 '';
645
646 passthru = {
647 inherit bootPkgs targetPrefix;
648
649 inherit llvmPackages;
650 inherit enableShared;
651
652 # This is used by the haskell builder to query
653 # the presence of the haddock program.
654 hasHaddock = enableHaddockProgram;
655
656 # Our Cabal compiler name
657 haskellCompilerName = "ghc-${version}";
658
659 bootstrapAvailable = lib.meta.availableOn stdenv.buildPlatform bootPkgs.ghc;
660 };
661
662 meta = {
663 homepage = "http://haskell.org/ghc";
664 description = "Glasgow Haskell Compiler";
665 maintainers = with lib.maintainers; [
666 guibou
667 ];
668 teams = [ lib.teams.haskell ];
669 timeout = 24 * 3600;
670 platforms = lib.platforms.all;
671 inherit (bootPkgs.ghc.meta) license;
672 # To be fixed by <https://github.com/NixOS/nixpkgs/pull/440774>.
673 broken = useLLVM;
674 };
675
676 }
677 // lib.optionalAttrs targetPlatform.useAndroidPrebuilt {
678 dontStrip = true;
679 dontPatchELF = true;
680 noAuditTmpdir = true;
681 }
682)