···11-{ lib, stdenv, pkgsBuildTarget, pkgsHostTarget, targetPackages
22-33-# build-tools
44-, bootPkgs
55-, autoconf, automake, coreutils, fetchpatch, fetchurl, perl, python3, m4, sphinx
66-, bash
77-88-, libiconv ? null, ncurses
99-1010-, # GHC can be built with system libffi or a bundled one.
1111- libffi ? null
1212-1313-, useLLVM ? !(stdenv.targetPlatform.isx86
1414- || stdenv.targetPlatform.isPower
1515- || stdenv.targetPlatform.isSparc)
1616-, # LLVM is conceptually a run-time-only dependency, but for
1717- # non-x86, we need LLVM to bootstrap later stages, so it becomes a
1818- # build-time dependency too.
1919- buildTargetLlvmPackages, llvmPackages
2020-2121-, # If enabled, GHC will be built with the GPL-free but slower integer-simple
2222- # library instead of the faster but GPLed integer-gmp library.
2323- enableIntegerSimple ? !(lib.meta.availableOn stdenv.hostPlatform gmp
2424- && lib.meta.availableOn stdenv.targetPlatform gmp)
2525-, gmp
2626-2727-, # If enabled, use -fPIC when compiling static libs.
2828- enableRelocatedStaticLibs ? stdenv.targetPlatform != stdenv.hostPlatform
2929-3030-, enableProfiledLibs ? true
3131-3232-, # Whether to build dynamic libs for the standard library (on the target
3333- # platform). Static libs are always built.
3434- enableShared ? !stdenv.targetPlatform.isWindows && !stdenv.targetPlatform.useiOSPrebuilt
3535-3636-, # Whether to build terminfo.
3737- enableTerminfo ? !stdenv.targetPlatform.isWindows
3838-3939-, # What flavour to build. An empty string indicates no
4040- # specific flavour and falls back to ghc default values.
4141- ghcFlavour ? lib.optionalString (stdenv.targetPlatform != stdenv.hostPlatform)
4242- (if useLLVM then "perf-cross" else "perf-cross-ncg")
4343-4444-, # Whether to build sphinx documentation.
4545- enableDocs ? (
4646- # Docs disabled for musl and cross because it's a large task to keep
4747- # all `sphinx` dependencies building in those environments.
4848- # `sphinx` pullls in among others:
4949- # Ruby, Python, Perl, Rust, OpenGL, Xorg, gtk, LLVM.
5050- (stdenv.targetPlatform == stdenv.hostPlatform)
5151- && !stdenv.hostPlatform.isMusl
5252- )
5353-5454-, enableHaddockProgram ?
5555- # Disabled for cross; see note [HADDOCK_DOCS].
5656- (stdenv.targetPlatform == stdenv.hostPlatform)
5757-5858-, # Whether to disable the large address space allocator
5959- # necessary fix for iOS: https://www.reddit.com/r/haskell/comments/4ttdz1/building_an_osxi386_to_iosarm64_cross_compiler/d5qvd67/
6060- disableLargeAddressSpace ? stdenv.targetPlatform.isiOS
6161-}:
6262-6363-assert !enableIntegerSimple -> gmp != null;
6464-6565-# Cross cannot currently build the `haddock` program for silly reasons,
6666-# see note [HADDOCK_DOCS].
6767-assert (stdenv.targetPlatform != stdenv.hostPlatform) -> !enableHaddockProgram;
6868-6969-let
7070- inherit (stdenv) buildPlatform hostPlatform targetPlatform;
7171-7272- inherit (bootPkgs) ghc;
7373-7474- # TODO(@Ericson2314) Make unconditional
7575- targetPrefix = lib.optionalString
7676- (targetPlatform != hostPlatform)
7777- "${targetPlatform.config}-";
7878-7979- buildMK = dontStrip: ''
8080- BuildFlavour = ${ghcFlavour}
8181- ifneq \"\$(BuildFlavour)\" \"\"
8282- include mk/flavours/\$(BuildFlavour).mk
8383- endif
8484- BUILD_SPHINX_HTML = ${if enableDocs then "YES" else "NO"}
8585- BUILD_SPHINX_PDF = NO
8686- '' +
8787- # Note [HADDOCK_DOCS]:
8888- # Unfortunately currently `HADDOCK_DOCS` controls both whether the `haddock`
8989- # program is built (which we generally always want to have a complete GHC install)
9090- # and whether it is run on the GHC sources to generate hyperlinked source code
9191- # (which is impossible for cross-compilation); see:
9292- # https://gitlab.haskell.org/ghc/ghc/-/issues/20077
9393- # This implies that currently a cross-compiled GHC will never have a `haddock`
9494- # program, so it can never generate haddocks for any packages.
9595- # If this is solved in the future, we'd like to unconditionally
9696- # build the haddock program (removing the `enableHaddockProgram` option).
9797- ''
9898- HADDOCK_DOCS = ${if enableHaddockProgram then "YES" else "NO"}
9999- DYNAMIC_GHC_PROGRAMS = ${if enableShared then "YES" else "NO"}
100100- INTEGER_LIBRARY = ${if enableIntegerSimple then "integer-simple" else "integer-gmp"}
101101- ''
102102- # We only need to build stage1 on most cross-compilation because
103103- # we will be running the compiler on the native system. In some
104104- # situations, like native Musl compilation, we need the compiler
105105- # to actually link to our new Libc. The iOS simulator is a special
106106- # exception because we can’t actually run simulators binaries
107107- # ourselves.
108108- + lib.optionalString (targetPlatform != hostPlatform) ''
109109- Stage1Only = ${if (targetPlatform.system == hostPlatform.system && !targetPlatform.isiOS) then "NO" else "YES"}
110110- CrossCompilePrefix = ${targetPrefix}
111111- '' + lib.optionalString dontStrip ''
112112- STRIP_CMD = :
113113- '' + lib.optionalString (!enableProfiledLibs) ''
114114- GhcLibWays = "v dyn"
115115- '' + lib.optionalString enableRelocatedStaticLibs ''
116116- GhcLibHcOpts += -fPIC
117117- GhcRtsHcOpts += -fPIC
118118- '' + lib.optionalString targetPlatform.useAndroidPrebuilt ''
119119- EXTRA_CC_OPTS += -std=gnu99
120120- ''
121121- # While split sections are now enabled by default in ghc 8.8 for windows,
122122- # they seem to lead to `too many sections` errors when building base for
123123- # profiling.
124124- + lib.optionalString targetPlatform.isWindows ''
125125- SplitSections = NO
126126- '';
127127-128128- # Splicer will pull out correct variations
129129- libDeps = platform: lib.optional enableTerminfo ncurses
130130- ++ [libffi]
131131- ++ lib.optional (!enableIntegerSimple) gmp
132132- ++ lib.optional (platform.libc != "glibc" && !targetPlatform.isWindows) libiconv;
133133-134134- # TODO(@sternenseemann): is buildTarget LLVM unnecessary?
135135- # GHC doesn't seem to have {LLC,OPT}_HOST
136136- toolsForTarget = [
137137- pkgsBuildTarget.targetPackages.stdenv.cc
138138- ] ++ lib.optional useLLVM buildTargetLlvmPackages.llvm;
139139-140140- targetCC = builtins.head toolsForTarget;
141141-142142- # Use gold either following the default, or to avoid the BFD linker due to some bugs / perf issues.
143143- # But we cannot avoid BFD when using musl libc due to https://sourceware.org/bugzilla/show_bug.cgi?id=23856
144144- # see #84670 and #49071 for more background.
145145- useLdGold = targetPlatform.linker == "gold" ||
146146- (targetPlatform.linker == "bfd" && (targetCC.bintools.bintools.hasGold or false) && !targetPlatform.isMusl);
147147-148148- # Makes debugging easier to see which variant is at play in `nix-store -q --tree`.
149149- variantSuffix = lib.concatStrings [
150150- (lib.optionalString stdenv.hostPlatform.isMusl "-musl")
151151- (lib.optionalString enableIntegerSimple "-integer-simple")
152152- ];
153153-154154-in
155155-156156-# C compiler, bintools and LLVM are used at build time, but will also leak into
157157-# the resulting GHC's settings file and used at runtime. This means that we are
158158-# currently only able to build GHC if hostPlatform == buildPlatform.
159159-assert targetCC == pkgsHostTarget.targetPackages.stdenv.cc;
160160-assert buildTargetLlvmPackages.llvm == llvmPackages.llvm;
161161-assert stdenv.targetPlatform.isDarwin -> buildTargetLlvmPackages.clang == llvmPackages.clang;
162162-163163-stdenv.mkDerivation (rec {
164164- version = "8.8.4";
165165- pname = "${targetPrefix}ghc${variantSuffix}";
166166-167167- src = fetchurl {
168168- url = "https://downloads.haskell.org/ghc/${version}/ghc-${version}-src.tar.xz";
169169- sha256 = "0bgwbxxvdn56l91bp9p5d083gzcfdi6z8l8b17qzjpr3n8w5wl7h";
170170- };
171171-172172- enableParallelBuilding = true;
173173-174174- outputs = [ "out" "doc" ];
175175-176176- patches = [
177177- # Fix docs build with sphinx >= 6.0
178178- # https://gitlab.haskell.org/ghc/ghc/-/issues/22766
179179- ./ghc-8.8.4-sphinx-6.0.patch
180180-181181- # See upstream patch at
182182- # https://gitlab.haskell.org/ghc/ghc/-/merge_requests/4885. Since we build
183183- # from source distributions, the auto-generated configure script needs to be
184184- # patched as well, therefore we use an in-tree patch instead of pulling the
185185- # upstream patch. Don't forget to check backport status of the upstream patch
186186- # when adding new GHC releases in nixpkgs.
187187- ./respect-ar-path.patch
188188- # Fix documentation configuration which causes a syntax error with sphinx 4.*
189189- # See also https://gitlab.haskell.org/ghc/ghc/-/issues/19962
190190- ./sphinx-4-configuration.patch
191191- # cabal passes incorrect --host= when cross-compiling
192192- # https://github.com/haskell/cabal/issues/5887
193193- (fetchpatch {
194194- url = "https://raw.githubusercontent.com/input-output-hk/haskell.nix/122bd81150386867da07fdc9ad5096db6719545a/overlays/patches/ghc/cabal-host.patch";
195195- sha256 = "sha256:0yd0sajgi24sc1w5m55lkg2lp6kfkgpp3lgija2c8y3cmkwfpdc1";
196196- })
197197-198198- # error: 'VirtualAllocExNuma' redeclared as different kind of symbol
199199- # name conflict between rts/win32/OSMem.c and winbase.h from the mingw-w64 runtime package
200200- # Renamed to match ghc8.8:
201201- # https://gitlab.haskell.org/ghc/ghc/-/commit/4b431f334018eaef2cf36de3316025c68c922915#20d64c0bdc272817149d1d5cf20a73a8b5fd637f
202202- ./rename-numa-api-call.patch
203203- ];
204204-205205- postPatch = "patchShebangs .";
206206-207207- # GHC is a bit confused on its cross terminology.
208208- # TODO(@sternenseemann): investigate coreutils dependencies and pass absolute paths
209209- preConfigure =
210210- # Aarch64 allow backward bootstrapping since earlier versions are unstable.
211211- # Same for musl, as earlier versions do not provide a musl bindist for bootstrapping.
212212- lib.optionalString (stdenv.isAarch64 || stdenv.hostPlatform.isMusl) ''
213213- find . -name \*\.cabal\* -exec sed -i -e 's/\(base.*\)4.14/\14.16/' {} \; \
214214- -exec sed -i -e 's/\(prim.*\)0.6/\10.8/' {} \;
215215- ''
216216- + ''
217217- for env in $(env | grep '^TARGET_' | sed -E 's|\+?=.*||'); do
218218- export "''${env#TARGET_}=''${!env}"
219219- done
220220- # GHC is a bit confused on its cross terminology, as these would normally be
221221- # the *host* tools.
222222- export CC="${targetCC}/bin/${targetCC.targetPrefix}cc"
223223- export CXX="${targetCC}/bin/${targetCC.targetPrefix}c++"
224224- # Use gold to work around https://sourceware.org/bugzilla/show_bug.cgi?id=16177
225225- export LD="${targetCC.bintools}/bin/${targetCC.bintools.targetPrefix}ld${lib.optionalString useLdGold ".gold"}"
226226- export AS="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}as"
227227- export AR="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}ar"
228228- export NM="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}nm"
229229- export RANLIB="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}ranlib"
230230- export READELF="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}readelf"
231231- export STRIP="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}strip"
232232- '' + lib.optionalString useLLVM ''
233233- export LLC="${lib.getBin buildTargetLlvmPackages.llvm}/bin/llc"
234234- export OPT="${lib.getBin buildTargetLlvmPackages.llvm}/bin/opt"
235235- '' + lib.optionalString (useLLVM && stdenv.targetPlatform.isDarwin) ''
236236- # LLVM backend on Darwin needs clang: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/codegens.html#llvm-code-generator-fllvm
237237- export CLANG="${buildTargetLlvmPackages.clang}/bin/${buildTargetLlvmPackages.clang.targetPrefix}clang"
238238- '' + ''
239239-240240- echo -n "${buildMK dontStrip}" > mk/build.mk
241241- sed -i -e 's|-isysroot /Developer/SDKs/MacOSX10.5.sdk||' configure
242242- '' + lib.optionalString (!stdenv.isDarwin) ''
243243- export NIX_LDFLAGS+=" -rpath $out/lib/ghc-${version}"
244244- '' + lib.optionalString stdenv.isDarwin ''
245245- export NIX_LDFLAGS+=" -no_dtrace_dof"
246246- '' + lib.optionalString targetPlatform.useAndroidPrebuilt ''
247247- 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
248248- '' + lib.optionalString targetPlatform.isMusl ''
249249- echo "patching llvm-targets for musl targets..."
250250- echo "Cloning these existing '*-linux-gnu*' targets:"
251251- grep linux-gnu llvm-targets | sed 's/^/ /'
252252- echo "(go go gadget sed)"
253253- sed -i 's,\(^.*linux-\)gnu\(.*\)$,\0\n\1musl\2,' llvm-targets
254254- echo "llvm-targets now contains these '*-linux-musl*' targets:"
255255- grep linux-musl llvm-targets | sed 's/^/ /'
256256-257257- echo "And now patching to preserve '-musleabi' as done with '-gnueabi'"
258258- # (aclocal.m4 is actual source, but patch configure as well since we don't re-gen)
259259- for x in configure aclocal.m4; do
260260- substituteInPlace $x \
261261- --replace '*-android*|*-gnueabi*)' \
262262- '*-android*|*-gnueabi*|*-musleabi*)'
263263- done
264264- '';
265265-266266- # TODO(@Ericson2314): Always pass "--target" and always prefix.
267267- configurePlatforms = [ "build" "host" ]
268268- ++ lib.optional (targetPlatform != hostPlatform) "target";
269269-270270- # `--with` flags for libraries needed for RTS linker
271271- configureFlags = [
272272- "--datadir=$doc/share/doc/ghc"
273273- "--with-curses-includes=${ncurses.dev}/include" "--with-curses-libraries=${ncurses.out}/lib"
274274- ] ++ lib.optionals (libffi != null) [
275275- "--with-system-libffi"
276276- "--with-ffi-includes=${targetPackages.libffi.dev}/include"
277277- "--with-ffi-libraries=${targetPackages.libffi.out}/lib"
278278- ] ++ lib.optionals (targetPlatform == hostPlatform && !enableIntegerSimple) [
279279- "--with-gmp-includes=${targetPackages.gmp.dev}/include"
280280- "--with-gmp-libraries=${targetPackages.gmp.out}/lib"
281281- ] ++ lib.optionals (targetPlatform == hostPlatform && hostPlatform.libc != "glibc" && !targetPlatform.isWindows) [
282282- "--with-iconv-includes=${libiconv}/include"
283283- "--with-iconv-libraries=${libiconv}/lib"
284284- ] ++ lib.optionals (targetPlatform != hostPlatform) [
285285- "--enable-bootstrap-with-devel-snapshot"
286286- ] ++ lib.optionals useLdGold [
287287- "CFLAGS=-fuse-ld=gold"
288288- "CONF_GCC_LINKER_OPTS_STAGE1=-fuse-ld=gold"
289289- "CONF_GCC_LINKER_OPTS_STAGE2=-fuse-ld=gold"
290290- ] ++ lib.optionals (disableLargeAddressSpace) [
291291- "--disable-large-address-space"
292292- ];
293293-294294- # Make sure we never relax`$PATH` and hooks support for compatibility.
295295- strictDeps = true;
296296-297297- # Don’t add -liconv to LDFLAGS automatically so that GHC will add it itself.
298298- dontAddExtraLibs = true;
299299-300300- nativeBuildInputs = [
301301- perl autoconf automake m4 python3
302302- ghc bootPkgs.alex bootPkgs.happy bootPkgs.hscolour
303303- ] ++ lib.optionals enableDocs [
304304- sphinx
305305- ];
306306-307307- # For building runtime libs
308308- depsBuildTarget = toolsForTarget;
309309-310310- buildInputs = [ perl bash ] ++ (libDeps hostPlatform);
311311-312312- depsTargetTarget = map lib.getDev (libDeps targetPlatform);
313313- depsTargetTargetPropagated = map (lib.getOutput "out") (libDeps targetPlatform);
314314-315315- # required, because otherwise all symbols from HSffi.o are stripped, and
316316- # that in turn causes GHCi to abort
317317- stripDebugFlags = [ "-S" ] ++ lib.optional (!targetPlatform.isDarwin) "--keep-file-symbols";
318318-319319- checkTarget = "test";
320320-321321- hardeningDisable =
322322- [ "format" ]
323323- # In nixpkgs, musl based builds currently enable `pie` hardening by default
324324- # (see `defaultHardeningFlags` in `make-derivation.nix`).
325325- # But GHC cannot currently produce outputs that are ready for `-pie` linking.
326326- # Thus, disable `pie` hardening, otherwise `recompile with -fPIE` errors appear.
327327- # See:
328328- # * https://github.com/NixOS/nixpkgs/issues/129247
329329- # * https://gitlab.haskell.org/ghc/ghc/-/issues/19580
330330- ++ lib.optional stdenv.targetPlatform.isMusl "pie";
331331-332332- postInstall = ''
333333- # Install the bash completion file.
334334- install -D -m 444 utils/completion/ghc.bash $out/share/bash-completion/completions/${targetPrefix}ghc
335335- '';
336336-337337- passthru = {
338338- inherit bootPkgs targetPrefix;
339339-340340- inherit llvmPackages;
341341- inherit enableShared;
342342-343343- # This is used by the haskell builder to query
344344- # the presence of the haddock program.
345345- hasHaddock = enableHaddockProgram;
346346-347347- # Our Cabal compiler name
348348- haskellCompilerName = "ghc-${version}";
349349- };
350350-351351- meta = {
352352- homepage = "http://haskell.org/ghc";
353353- description = "The Glasgow Haskell Compiler";
354354- maintainers = with lib.maintainers; [
355355- guibou
356356- ] ++ lib.teams.haskell.members;
357357- timeout = 24 * 3600;
358358- inherit (ghc.meta) license;
359359- # hardcode platforms because the bootstrap GHC differs depending on the platform,
360360- # with differing platforms available for each of them; See HACK comment in
361361- # 8.10.2-binary.nix for an explanation of the musl special casing.
362362- platforms = [
363363- "x86_64-linux"
364364- ] ++ lib.optionals (!hostPlatform.isMusl) [
365365- "i686-linux"
366366- "aarch64-linux"
367367- "x86_64-darwin"
368368- ];
369369- # integer-simple builds are broken with musl when bootstrapping using
370370- # GHC 8.10.2 and below, however it is not possible to reverse bootstrap
371371- # GHC 8.8.4 with GHC 8.10.7.
372372- # See https://github.com/NixOS/nixpkgs/pull/138523#issuecomment-927339953
373373- broken = hostPlatform.isMusl && enableIntegerSimple;
374374- };
375375-376376- dontStrip = (targetPlatform.useAndroidPrebuilt || targetPlatform.isWasm);
377377-378378-} // lib.optionalAttrs targetPlatform.useAndroidPrebuilt{
379379- dontPatchELF = true;
380380- noAuditTmpdir = true;
381381-})