···1-{ lib, stdenv, pkgsBuildTarget, pkgsHostTarget, targetPackages
2-3-# build-tools
4-, bootPkgs
5-, autoconf, automake, coreutils, fetchpatch, fetchurl, perl, python3, m4, sphinx
6-, bash
7-8-, libiconv ? null, ncurses
9-10-, # GHC can be built with system libffi or a bundled one.
11- libffi ? null
12-13-, useLLVM ? !(stdenv.targetPlatform.isx86
14- || stdenv.targetPlatform.isPower
15- || stdenv.targetPlatform.isSparc)
16-, # LLVM is conceptually a run-time-only dependency, but for
17- # non-x86, we need LLVM to bootstrap later stages, so it becomes a
18- # build-time dependency too.
19- buildTargetLlvmPackages, llvmPackages
20-21-, # If enabled, GHC will be built with the GPL-free but slower integer-simple
22- # library instead of the faster but GPLed integer-gmp library.
23- enableIntegerSimple ? !(lib.meta.availableOn stdenv.hostPlatform gmp
24- && lib.meta.availableOn stdenv.targetPlatform gmp)
25-, gmp
26-27-, # If enabled, use -fPIC when compiling static libs.
28- enableRelocatedStaticLibs ? stdenv.targetPlatform != stdenv.hostPlatform
29-30-, enableProfiledLibs ? true
31-32-, # Whether to build dynamic libs for the standard library (on the target
33- # platform). Static libs are always built.
34- enableShared ? !stdenv.targetPlatform.isWindows && !stdenv.targetPlatform.useiOSPrebuilt
35-36-, # Whether to build terminfo.
37- enableTerminfo ? !stdenv.targetPlatform.isWindows
38-39-, # What flavour to build. An empty string indicates no
40- # specific flavour and falls back to ghc default values.
41- ghcFlavour ? lib.optionalString (stdenv.targetPlatform != stdenv.hostPlatform)
42- (if useLLVM then "perf-cross" else "perf-cross-ncg")
43-44-, # Whether to build sphinx documentation.
45- enableDocs ? (
46- # Docs disabled for musl and cross because it's a large task to keep
47- # all `sphinx` dependencies building in those environments.
48- # `sphinx` pullls in among others:
49- # Ruby, Python, Perl, Rust, OpenGL, Xorg, gtk, LLVM.
50- (stdenv.targetPlatform == stdenv.hostPlatform)
51- && !stdenv.hostPlatform.isMusl
52- )
53-54-, enableHaddockProgram ?
55- # Disabled for cross; see note [HADDOCK_DOCS].
56- (stdenv.targetPlatform == stdenv.hostPlatform)
57-58-, # Whether to disable the large address space allocator
59- # necessary fix for iOS: https://www.reddit.com/r/haskell/comments/4ttdz1/building_an_osxi386_to_iosarm64_cross_compiler/d5qvd67/
60- disableLargeAddressSpace ? stdenv.targetPlatform.isiOS
61-}:
62-63-assert !enableIntegerSimple -> gmp != null;
64-65-# Cross cannot currently build the `haddock` program for silly reasons,
66-# see note [HADDOCK_DOCS].
67-assert (stdenv.targetPlatform != stdenv.hostPlatform) -> !enableHaddockProgram;
68-69-let
70- inherit (stdenv) buildPlatform hostPlatform targetPlatform;
71-72- inherit (bootPkgs) ghc;
73-74- # TODO(@Ericson2314) Make unconditional
75- targetPrefix = lib.optionalString
76- (targetPlatform != hostPlatform)
77- "${targetPlatform.config}-";
78-79- buildMK = dontStrip: ''
80- BuildFlavour = ${ghcFlavour}
81- ifneq \"\$(BuildFlavour)\" \"\"
82- include mk/flavours/\$(BuildFlavour).mk
83- endif
84- BUILD_SPHINX_HTML = ${if enableDocs then "YES" else "NO"}
85- BUILD_SPHINX_PDF = NO
86- '' +
87- # Note [HADDOCK_DOCS]:
88- # Unfortunately currently `HADDOCK_DOCS` controls both whether the `haddock`
89- # program is built (which we generally always want to have a complete GHC install)
90- # and whether it is run on the GHC sources to generate hyperlinked source code
91- # (which is impossible for cross-compilation); see:
92- # https://gitlab.haskell.org/ghc/ghc/-/issues/20077
93- # This implies that currently a cross-compiled GHC will never have a `haddock`
94- # program, so it can never generate haddocks for any packages.
95- # If this is solved in the future, we'd like to unconditionally
96- # build the haddock program (removing the `enableHaddockProgram` option).
97- ''
98- HADDOCK_DOCS = ${if enableHaddockProgram then "YES" else "NO"}
99- DYNAMIC_GHC_PROGRAMS = ${if enableShared then "YES" else "NO"}
100- INTEGER_LIBRARY = ${if enableIntegerSimple then "integer-simple" else "integer-gmp"}
101- ''
102- # We only need to build stage1 on most cross-compilation because
103- # we will be running the compiler on the native system. In some
104- # situations, like native Musl compilation, we need the compiler
105- # to actually link to our new Libc. The iOS simulator is a special
106- # exception because we can’t actually run simulators binaries
107- # ourselves.
108- + lib.optionalString (targetPlatform != hostPlatform) ''
109- Stage1Only = ${if (targetPlatform.system == hostPlatform.system && !targetPlatform.isiOS) then "NO" else "YES"}
110- CrossCompilePrefix = ${targetPrefix}
111- '' + lib.optionalString dontStrip ''
112- STRIP_CMD = :
113- '' + lib.optionalString (!enableProfiledLibs) ''
114- GhcLibWays = "v dyn"
115- '' + lib.optionalString enableRelocatedStaticLibs ''
116- GhcLibHcOpts += -fPIC
117- GhcRtsHcOpts += -fPIC
118- '' + lib.optionalString targetPlatform.useAndroidPrebuilt ''
119- EXTRA_CC_OPTS += -std=gnu99
120- ''
121- # While split sections are now enabled by default in ghc 8.8 for windows,
122- # they seem to lead to `too many sections` errors when building base for
123- # profiling.
124- + lib.optionalString targetPlatform.isWindows ''
125- SplitSections = NO
126- '';
127-128- # Splicer will pull out correct variations
129- libDeps = platform: lib.optional enableTerminfo ncurses
130- ++ [libffi]
131- ++ lib.optional (!enableIntegerSimple) gmp
132- ++ lib.optional (platform.libc != "glibc" && !targetPlatform.isWindows) libiconv;
133-134- # TODO(@sternenseemann): is buildTarget LLVM unnecessary?
135- # GHC doesn't seem to have {LLC,OPT}_HOST
136- toolsForTarget = [
137- pkgsBuildTarget.targetPackages.stdenv.cc
138- ] ++ lib.optional useLLVM buildTargetLlvmPackages.llvm;
139-140- targetCC = builtins.head toolsForTarget;
141-142- # Use gold either following the default, or to avoid the BFD linker due to some bugs / perf issues.
143- # But we cannot avoid BFD when using musl libc due to https://sourceware.org/bugzilla/show_bug.cgi?id=23856
144- # see #84670 and #49071 for more background.
145- useLdGold = targetPlatform.linker == "gold" ||
146- (targetPlatform.linker == "bfd" && (targetCC.bintools.bintools.hasGold or false) && !targetPlatform.isMusl);
147-148- # Makes debugging easier to see which variant is at play in `nix-store -q --tree`.
149- variantSuffix = lib.concatStrings [
150- (lib.optionalString stdenv.hostPlatform.isMusl "-musl")
151- (lib.optionalString enableIntegerSimple "-integer-simple")
152- ];
153-154-in
155-156-# C compiler, bintools and LLVM are used at build time, but will also leak into
157-# the resulting GHC's settings file and used at runtime. This means that we are
158-# currently only able to build GHC if hostPlatform == buildPlatform.
159-assert targetCC == pkgsHostTarget.targetPackages.stdenv.cc;
160-assert buildTargetLlvmPackages.llvm == llvmPackages.llvm;
161-assert stdenv.targetPlatform.isDarwin -> buildTargetLlvmPackages.clang == llvmPackages.clang;
162-163-stdenv.mkDerivation (rec {
164- version = "8.8.4";
165- pname = "${targetPrefix}ghc${variantSuffix}";
166-167- src = fetchurl {
168- url = "https://downloads.haskell.org/ghc/${version}/ghc-${version}-src.tar.xz";
169- sha256 = "0bgwbxxvdn56l91bp9p5d083gzcfdi6z8l8b17qzjpr3n8w5wl7h";
170- };
171-172- enableParallelBuilding = true;
173-174- outputs = [ "out" "doc" ];
175-176- patches = [
177- # Fix docs build with sphinx >= 6.0
178- # https://gitlab.haskell.org/ghc/ghc/-/issues/22766
179- ./ghc-8.8.4-sphinx-6.0.patch
180-181- # See upstream patch at
182- # https://gitlab.haskell.org/ghc/ghc/-/merge_requests/4885. Since we build
183- # from source distributions, the auto-generated configure script needs to be
184- # patched as well, therefore we use an in-tree patch instead of pulling the
185- # upstream patch. Don't forget to check backport status of the upstream patch
186- # when adding new GHC releases in nixpkgs.
187- ./respect-ar-path.patch
188- # Fix documentation configuration which causes a syntax error with sphinx 4.*
189- # See also https://gitlab.haskell.org/ghc/ghc/-/issues/19962
190- ./sphinx-4-configuration.patch
191- # cabal passes incorrect --host= when cross-compiling
192- # https://github.com/haskell/cabal/issues/5887
193- (fetchpatch {
194- url = "https://raw.githubusercontent.com/input-output-hk/haskell.nix/122bd81150386867da07fdc9ad5096db6719545a/overlays/patches/ghc/cabal-host.patch";
195- sha256 = "sha256:0yd0sajgi24sc1w5m55lkg2lp6kfkgpp3lgija2c8y3cmkwfpdc1";
196- })
197-198- # error: 'VirtualAllocExNuma' redeclared as different kind of symbol
199- # name conflict between rts/win32/OSMem.c and winbase.h from the mingw-w64 runtime package
200- # Renamed to match ghc8.8:
201- # https://gitlab.haskell.org/ghc/ghc/-/commit/4b431f334018eaef2cf36de3316025c68c922915#20d64c0bdc272817149d1d5cf20a73a8b5fd637f
202- ./rename-numa-api-call.patch
203- ];
204-205- postPatch = "patchShebangs .";
206-207- # GHC is a bit confused on its cross terminology.
208- # TODO(@sternenseemann): investigate coreutils dependencies and pass absolute paths
209- preConfigure =
210- # Aarch64 allow backward bootstrapping since earlier versions are unstable.
211- # Same for musl, as earlier versions do not provide a musl bindist for bootstrapping.
212- lib.optionalString (stdenv.isAarch64 || stdenv.hostPlatform.isMusl) ''
213- find . -name \*\.cabal\* -exec sed -i -e 's/\(base.*\)4.14/\14.16/' {} \; \
214- -exec sed -i -e 's/\(prim.*\)0.6/\10.8/' {} \;
215- ''
216- + ''
217- for env in $(env | grep '^TARGET_' | sed -E 's|\+?=.*||'); do
218- export "''${env#TARGET_}=''${!env}"
219- done
220- # GHC is a bit confused on its cross terminology, as these would normally be
221- # the *host* tools.
222- export CC="${targetCC}/bin/${targetCC.targetPrefix}cc"
223- export CXX="${targetCC}/bin/${targetCC.targetPrefix}c++"
224- # Use gold to work around https://sourceware.org/bugzilla/show_bug.cgi?id=16177
225- export LD="${targetCC.bintools}/bin/${targetCC.bintools.targetPrefix}ld${lib.optionalString useLdGold ".gold"}"
226- export AS="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}as"
227- export AR="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}ar"
228- export NM="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}nm"
229- export RANLIB="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}ranlib"
230- export READELF="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}readelf"
231- export STRIP="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}strip"
232- '' + lib.optionalString useLLVM ''
233- export LLC="${lib.getBin buildTargetLlvmPackages.llvm}/bin/llc"
234- export OPT="${lib.getBin buildTargetLlvmPackages.llvm}/bin/opt"
235- '' + lib.optionalString (useLLVM && stdenv.targetPlatform.isDarwin) ''
236- # LLVM backend on Darwin needs clang: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/codegens.html#llvm-code-generator-fllvm
237- export CLANG="${buildTargetLlvmPackages.clang}/bin/${buildTargetLlvmPackages.clang.targetPrefix}clang"
238- '' + ''
239-240- echo -n "${buildMK dontStrip}" > mk/build.mk
241- sed -i -e 's|-isysroot /Developer/SDKs/MacOSX10.5.sdk||' configure
242- '' + lib.optionalString (!stdenv.isDarwin) ''
243- export NIX_LDFLAGS+=" -rpath $out/lib/ghc-${version}"
244- '' + lib.optionalString stdenv.isDarwin ''
245- export NIX_LDFLAGS+=" -no_dtrace_dof"
246- '' + lib.optionalString targetPlatform.useAndroidPrebuilt ''
247- 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
248- '' + lib.optionalString targetPlatform.isMusl ''
249- echo "patching llvm-targets for musl targets..."
250- echo "Cloning these existing '*-linux-gnu*' targets:"
251- grep linux-gnu llvm-targets | sed 's/^/ /'
252- echo "(go go gadget sed)"
253- sed -i 's,\(^.*linux-\)gnu\(.*\)$,\0\n\1musl\2,' llvm-targets
254- echo "llvm-targets now contains these '*-linux-musl*' targets:"
255- grep linux-musl llvm-targets | sed 's/^/ /'
256-257- echo "And now patching to preserve '-musleabi' as done with '-gnueabi'"
258- # (aclocal.m4 is actual source, but patch configure as well since we don't re-gen)
259- for x in configure aclocal.m4; do
260- substituteInPlace $x \
261- --replace '*-android*|*-gnueabi*)' \
262- '*-android*|*-gnueabi*|*-musleabi*)'
263- done
264- '';
265-266- # TODO(@Ericson2314): Always pass "--target" and always prefix.
267- configurePlatforms = [ "build" "host" ]
268- ++ lib.optional (targetPlatform != hostPlatform) "target";
269-270- # `--with` flags for libraries needed for RTS linker
271- configureFlags = [
272- "--datadir=$doc/share/doc/ghc"
273- "--with-curses-includes=${ncurses.dev}/include" "--with-curses-libraries=${ncurses.out}/lib"
274- ] ++ lib.optionals (libffi != null) [
275- "--with-system-libffi"
276- "--with-ffi-includes=${targetPackages.libffi.dev}/include"
277- "--with-ffi-libraries=${targetPackages.libffi.out}/lib"
278- ] ++ lib.optionals (targetPlatform == hostPlatform && !enableIntegerSimple) [
279- "--with-gmp-includes=${targetPackages.gmp.dev}/include"
280- "--with-gmp-libraries=${targetPackages.gmp.out}/lib"
281- ] ++ lib.optionals (targetPlatform == hostPlatform && hostPlatform.libc != "glibc" && !targetPlatform.isWindows) [
282- "--with-iconv-includes=${libiconv}/include"
283- "--with-iconv-libraries=${libiconv}/lib"
284- ] ++ lib.optionals (targetPlatform != hostPlatform) [
285- "--enable-bootstrap-with-devel-snapshot"
286- ] ++ lib.optionals useLdGold [
287- "CFLAGS=-fuse-ld=gold"
288- "CONF_GCC_LINKER_OPTS_STAGE1=-fuse-ld=gold"
289- "CONF_GCC_LINKER_OPTS_STAGE2=-fuse-ld=gold"
290- ] ++ lib.optionals (disableLargeAddressSpace) [
291- "--disable-large-address-space"
292- ];
293-294- # Make sure we never relax`$PATH` and hooks support for compatibility.
295- strictDeps = true;
296-297- # Don’t add -liconv to LDFLAGS automatically so that GHC will add it itself.
298- dontAddExtraLibs = true;
299-300- nativeBuildInputs = [
301- perl autoconf automake m4 python3
302- ghc bootPkgs.alex bootPkgs.happy bootPkgs.hscolour
303- ] ++ lib.optionals enableDocs [
304- sphinx
305- ];
306-307- # For building runtime libs
308- depsBuildTarget = toolsForTarget;
309-310- buildInputs = [ perl bash ] ++ (libDeps hostPlatform);
311-312- depsTargetTarget = map lib.getDev (libDeps targetPlatform);
313- depsTargetTargetPropagated = map (lib.getOutput "out") (libDeps targetPlatform);
314-315- # required, because otherwise all symbols from HSffi.o are stripped, and
316- # that in turn causes GHCi to abort
317- stripDebugFlags = [ "-S" ] ++ lib.optional (!targetPlatform.isDarwin) "--keep-file-symbols";
318-319- checkTarget = "test";
320-321- hardeningDisable =
322- [ "format" ]
323- # In nixpkgs, musl based builds currently enable `pie` hardening by default
324- # (see `defaultHardeningFlags` in `make-derivation.nix`).
325- # But GHC cannot currently produce outputs that are ready for `-pie` linking.
326- # Thus, disable `pie` hardening, otherwise `recompile with -fPIE` errors appear.
327- # See:
328- # * https://github.com/NixOS/nixpkgs/issues/129247
329- # * https://gitlab.haskell.org/ghc/ghc/-/issues/19580
330- ++ lib.optional stdenv.targetPlatform.isMusl "pie";
331-332- postInstall = ''
333- # Install the bash completion file.
334- install -D -m 444 utils/completion/ghc.bash $out/share/bash-completion/completions/${targetPrefix}ghc
335- '';
336-337- passthru = {
338- inherit bootPkgs targetPrefix;
339-340- inherit llvmPackages;
341- inherit enableShared;
342-343- # This is used by the haskell builder to query
344- # the presence of the haddock program.
345- hasHaddock = enableHaddockProgram;
346-347- # Our Cabal compiler name
348- haskellCompilerName = "ghc-${version}";
349- };
350-351- meta = {
352- homepage = "http://haskell.org/ghc";
353- description = "The Glasgow Haskell Compiler";
354- maintainers = with lib.maintainers; [
355- guibou
356- ] ++ lib.teams.haskell.members;
357- timeout = 24 * 3600;
358- inherit (ghc.meta) license;
359- # hardcode platforms because the bootstrap GHC differs depending on the platform,
360- # with differing platforms available for each of them; See HACK comment in
361- # 8.10.2-binary.nix for an explanation of the musl special casing.
362- platforms = [
363- "x86_64-linux"
364- ] ++ lib.optionals (!hostPlatform.isMusl) [
365- "i686-linux"
366- "aarch64-linux"
367- "x86_64-darwin"
368- ];
369- # integer-simple builds are broken with musl when bootstrapping using
370- # GHC 8.10.2 and below, however it is not possible to reverse bootstrap
371- # GHC 8.8.4 with GHC 8.10.7.
372- # See https://github.com/NixOS/nixpkgs/pull/138523#issuecomment-927339953
373- broken = hostPlatform.isMusl && enableIntegerSimple;
374- };
375-376- dontStrip = (targetPlatform.useAndroidPrebuilt || targetPlatform.isWasm);
377-378-} // lib.optionalAttrs targetPlatform.useAndroidPrebuilt{
379- dontPatchELF = true;
380- noAuditTmpdir = true;
381-})