···
1
+
{ lib, stdenv, pkgsBuildTarget, pkgsHostTarget, targetPackages
5
+
, autoconf, automake, coreutils, fetchpatch, fetchurl, perl, python3, m4, sphinx
6
+
, xattr, autoSignDarwinBinariesHook
9
+
, libiconv ? null, ncurses
10
+
, glibcLocales ? null
12
+
, # GHC can be built with system libffi or a bundled one.
15
+
, useLLVM ? !(stdenv.targetPlatform.isx86
16
+
|| stdenv.targetPlatform.isPower
17
+
|| stdenv.targetPlatform.isSparc
18
+
|| (stdenv.targetPlatform.isAarch64 && stdenv.targetPlatform.isDarwin))
19
+
, # LLVM is conceptually a run-time-only depedendency, but for
20
+
# non-x86, we need LLVM to bootstrap later stages, so it becomes a
21
+
# build-time dependency too.
22
+
buildTargetLlvmPackages, llvmPackages
24
+
, # If enabled, GHC will be built with the GPL-free but slightly slower native
25
+
# bignum backend instead of the faster but GPLed gmp backend.
26
+
enableNativeBignum ? !(lib.meta.availableOn stdenv.hostPlatform gmp)
29
+
, # If enabled, use -fPIC when compiling static libs.
30
+
enableRelocatedStaticLibs ? stdenv.targetPlatform != stdenv.hostPlatform
32
+
# aarch64 outputs otherwise exceed 2GB limit
33
+
, enableProfiledLibs ? !stdenv.targetPlatform.isAarch64
35
+
, # Whether to build dynamic libs for the standard library (on the target
36
+
# platform). Static libs are always built.
37
+
enableShared ? with stdenv.targetPlatform; !isWindows && !useiOSPrebuilt && !isStatic
39
+
, # Whether to build terminfo.
40
+
enableTerminfo ? !stdenv.targetPlatform.isWindows
42
+
, # What flavour to build. An empty string indicates no
43
+
# specific flavour and falls back to ghc default values.
44
+
ghcFlavour ? lib.optionalString (stdenv.targetPlatform != stdenv.hostPlatform)
45
+
(if useLLVM then "perf-cross" else "perf-cross-ncg")
47
+
, # Whether to build sphinx documentation.
49
+
# Docs disabled for musl and cross because it's a large task to keep
50
+
# all `sphinx` dependencies building in those environments.
51
+
# `sphinx` pulls in among others:
52
+
# Ruby, Python, Perl, Rust, OpenGL, Xorg, gtk, LLVM.
53
+
(stdenv.targetPlatform == stdenv.hostPlatform)
54
+
&& !stdenv.hostPlatform.isMusl
57
+
, enableHaddockProgram ?
58
+
# Disabled for cross; see note [HADDOCK_DOCS].
59
+
(stdenv.targetPlatform == stdenv.hostPlatform)
61
+
, # Whether to disable the large address space allocator
62
+
# necessary fix for iOS: https://www.reddit.com/r/haskell/comments/4ttdz1/building_an_osxi386_to_iosarm64_cross_compiler/d5qvd67/
63
+
disableLargeAddressSpace ? stdenv.targetPlatform.isiOS
66
+
assert !enableNativeBignum -> gmp != null;
68
+
# Cross cannot currently build the `haddock` program for silly reasons,
69
+
# see note [HADDOCK_DOCS].
70
+
assert (stdenv.targetPlatform != stdenv.hostPlatform) -> !enableHaddockProgram;
73
+
inherit (stdenv) buildPlatform hostPlatform targetPlatform;
75
+
inherit (bootPkgs) ghc;
77
+
# TODO(@Ericson2314) Make unconditional
78
+
targetPrefix = lib.optionalString
79
+
(targetPlatform != hostPlatform)
80
+
"${targetPlatform.config}-";
83
+
BuildFlavour = ${ghcFlavour}
84
+
ifneq \"\$(BuildFlavour)\" \"\"
85
+
include mk/flavours/\$(BuildFlavour).mk
87
+
BUILD_SPHINX_HTML = ${if enableDocs then "YES" else "NO"}
88
+
BUILD_SPHINX_PDF = NO
90
+
# Note [HADDOCK_DOCS]:
91
+
# Unfortunately currently `HADDOCK_DOCS` controls both whether the `haddock`
92
+
# program is built (which we generally always want to have a complete GHC install)
93
+
# and whether it is run on the GHC sources to generate hyperlinked source code
94
+
# (which is impossible for cross-compilation); see:
95
+
# https://gitlab.haskell.org/ghc/ghc/-/issues/20077
96
+
# This implies that currently a cross-compiled GHC will never have a `haddock`
97
+
# program, so it can never generate haddocks for any packages.
98
+
# If this is solved in the future, we'd like to unconditionally
99
+
# build the haddock program (removing the `enableHaddockProgram` option).
101
+
HADDOCK_DOCS = ${if enableHaddockProgram then "YES" else "NO"}
102
+
# Build haddocks for boot packages with hyperlinking
103
+
EXTRA_HADDOCK_OPTS += --hyperlinked-source --quickjump
105
+
DYNAMIC_GHC_PROGRAMS = ${if enableShared then "YES" else "NO"}
106
+
BIGNUM_BACKEND = ${if enableNativeBignum then "native" else "gmp"}
107
+
'' + lib.optionalString (targetPlatform != hostPlatform) ''
108
+
Stage1Only = ${if targetPlatform.system == hostPlatform.system then "NO" else "YES"}
109
+
CrossCompilePrefix = ${targetPrefix}
110
+
'' + lib.optionalString (!enableProfiledLibs) ''
111
+
GhcLibWays = "v dyn"
113
+
# -fexternal-dynamic-refs apparently (because it's not clear from the documentation)
114
+
# makes the GHC RTS able to load static libraries, which may be needed for TemplateHaskell.
115
+
# This solution was described in https://www.tweag.io/blog/2020-09-30-bazel-static-haskell
116
+
lib.optionalString enableRelocatedStaticLibs ''
117
+
GhcLibHcOpts += -fPIC -fexternal-dynamic-refs
118
+
GhcRtsHcOpts += -fPIC -fexternal-dynamic-refs
119
+
'' + lib.optionalString targetPlatform.useAndroidPrebuilt ''
120
+
EXTRA_CC_OPTS += -std=gnu99
123
+
# Splicer will pull out correct variations
124
+
libDeps = platform: lib.optional enableTerminfo ncurses
126
+
++ lib.optional (!enableNativeBignum) gmp
127
+
++ lib.optional (platform.libc != "glibc" && !targetPlatform.isWindows) libiconv;
129
+
# TODO(@sternenseemann): is buildTarget LLVM unnecessary?
130
+
# GHC doesn't seem to have {LLC,OPT}_HOST
132
+
pkgsBuildTarget.targetPackages.stdenv.cc
133
+
] ++ lib.optional useLLVM buildTargetLlvmPackages.llvm;
135
+
targetCC = builtins.head toolsForTarget;
137
+
# Sometimes we have to dispatch between the bintools wrapper and the unwrapped
138
+
# derivation for certain tools depending on the platform.
140
+
# GHC needs install_name_tool on all darwin platforms. On aarch64-darwin it is
141
+
# part of the bintools wrapper (due to codesigning requirements), but not on
143
+
install_name_tool =
144
+
if stdenv.targetPlatform.isAarch64
145
+
then targetCC.bintools
146
+
else targetCC.bintools.bintools;
147
+
# Same goes for strip.
149
+
# TODO(@sternenseemann): also use wrapper if linker == "bfd" or "gold"
150
+
if stdenv.targetPlatform.isAarch64 && stdenv.targetPlatform.isDarwin
151
+
then targetCC.bintools
152
+
else targetCC.bintools.bintools;
155
+
# Use gold either following the default, or to avoid the BFD linker due to some bugs / perf issues.
156
+
# But we cannot avoid BFD when using musl libc due to https://sourceware.org/bugzilla/show_bug.cgi?id=23856
157
+
# see #84670 and #49071 for more background.
158
+
useLdGold = targetPlatform.linker == "gold" ||
159
+
(targetPlatform.linker == "bfd" && (targetCC.bintools.bintools.hasGold or false) && !targetPlatform.isMusl);
161
+
# Makes debugging easier to see which variant is at play in `nix-store -q --tree`.
162
+
variantSuffix = lib.concatStrings [
163
+
(lib.optionalString stdenv.hostPlatform.isMusl "-musl")
164
+
(lib.optionalString enableNativeBignum "-native-bignum")
169
+
# C compiler, bintools and LLVM are used at build time, but will also leak into
170
+
# the resulting GHC's settings file and used at runtime. This means that we are
171
+
# currently only able to build GHC if hostPlatform == buildPlatform.
172
+
assert targetCC == pkgsHostTarget.targetPackages.stdenv.cc;
173
+
assert buildTargetLlvmPackages.llvm == llvmPackages.llvm;
174
+
assert stdenv.targetPlatform.isDarwin -> buildTargetLlvmPackages.clang == llvmPackages.clang;
176
+
stdenv.mkDerivation (rec {
178
+
pname = "${targetPrefix}ghc${variantSuffix}";
181
+
url = "https://downloads.haskell.org/ghc/${version}/ghc-${version}-src.tar.xz";
182
+
sha256 = "0606797d1b38e2d88ee2243f38ec6b9a1aa93e9b578e95f0de9a9c0a4144021c";
185
+
enableParallelBuilding = true;
187
+
outputs = [ "out" "doc" ];
190
+
# fix hyperlinked haddock sources: https://github.com/haskell/haddock/pull/1482
192
+
url = "https://patch-diff.githubusercontent.com/raw/haskell/haddock/pull/1482.patch";
193
+
sha256 = "sha256-8w8QUCsODaTvknCDGgTfFNZa8ZmvIKaKS+2ZJZ9foYk=";
194
+
extraPrefix = "utils/haddock/";
199
+
postPatch = "patchShebangs .";
201
+
# GHC needs the locale configured during the Haddock phase.
202
+
LANG = "en_US.UTF-8";
204
+
# GHC is a bit confused on its cross terminology.
205
+
# TODO(@sternenseemann): investigate coreutils dependencies and pass absolute paths
207
+
for env in $(env | grep '^TARGET_' | sed -E 's|\+?=.*||'); do
208
+
export "''${env#TARGET_}=''${!env}"
210
+
# GHC is a bit confused on its cross terminology, as these would normally be
211
+
# the *host* tools.
212
+
export CC="${targetCC}/bin/${targetCC.targetPrefix}cc"
213
+
export CXX="${targetCC}/bin/${targetCC.targetPrefix}c++"
214
+
# Use gold to work around https://sourceware.org/bugzilla/show_bug.cgi?id=16177
215
+
export LD="${targetCC.bintools}/bin/${targetCC.bintools.targetPrefix}ld${lib.optionalString useLdGold ".gold"}"
216
+
export AS="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}as"
217
+
export AR="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}ar"
218
+
export NM="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}nm"
219
+
export RANLIB="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}ranlib"
220
+
export READELF="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}readelf"
221
+
export STRIP="${bintoolsFor.strip}/bin/${bintoolsFor.strip.targetPrefix}strip"
222
+
'' + lib.optionalString (stdenv.targetPlatform.linker == "cctools") ''
223
+
export OTOOL="${targetCC.bintools.bintools}/bin/${targetCC.bintools.targetPrefix}otool"
224
+
export INSTALL_NAME_TOOL="${bintoolsFor.install_name_tool}/bin/${bintoolsFor.install_name_tool.targetPrefix}install_name_tool"
225
+
'' + lib.optionalString useLLVM ''
226
+
export LLC="${lib.getBin buildTargetLlvmPackages.llvm}/bin/llc"
227
+
export OPT="${lib.getBin buildTargetLlvmPackages.llvm}/bin/opt"
228
+
'' + lib.optionalString (useLLVM && stdenv.targetPlatform.isDarwin) ''
229
+
# LLVM backend on Darwin needs clang: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/codegens.html#llvm-code-generator-fllvm
230
+
export CLANG="${buildTargetLlvmPackages.clang}/bin/${buildTargetLlvmPackages.clang.targetPrefix}clang"
232
+
echo -n "${buildMK}" > mk/build.mk
233
+
'' + lib.optionalString (stdenv.isLinux && hostPlatform.libc == "glibc") ''
234
+
export LOCALE_ARCHIVE="${glibcLocales}/lib/locale/locale-archive"
235
+
'' + lib.optionalString (!stdenv.isDarwin) ''
236
+
export NIX_LDFLAGS+=" -rpath $out/lib/ghc-${version}"
237
+
'' + lib.optionalString stdenv.isDarwin ''
238
+
export NIX_LDFLAGS+=" -no_dtrace_dof"
240
+
# GHC tries the host xattr /usr/bin/xattr by default which fails since it expects python to be 2.7
241
+
export XATTR=${lib.getBin xattr}/bin/xattr
242
+
'' + lib.optionalString targetPlatform.useAndroidPrebuilt ''
243
+
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
244
+
'' + lib.optionalString targetPlatform.isMusl ''
245
+
echo "patching llvm-targets for musl targets..."
246
+
echo "Cloning these existing '*-linux-gnu*' targets:"
247
+
grep linux-gnu llvm-targets | sed 's/^/ /'
248
+
echo "(go go gadget sed)"
249
+
sed -i 's,\(^.*linux-\)gnu\(.*\)$,\0\n\1musl\2,' llvm-targets
250
+
echo "llvm-targets now contains these '*-linux-musl*' targets:"
251
+
grep linux-musl llvm-targets | sed 's/^/ /'
253
+
echo "And now patching to preserve '-musleabi' as done with '-gnueabi'"
254
+
# (aclocal.m4 is actual source, but patch configure as well since we don't re-gen)
255
+
for x in configure aclocal.m4; do
256
+
substituteInPlace $x \
257
+
--replace '*-android*|*-gnueabi*)' \
258
+
'*-android*|*-gnueabi*|*-musleabi*)'
262
+
# TODO(@Ericson2314): Always pass "--target" and always prefix.
263
+
configurePlatforms = [ "build" "host" ]
264
+
++ lib.optional (targetPlatform != hostPlatform) "target";
266
+
# `--with` flags for libraries needed for RTS linker
268
+
"--datadir=$doc/share/doc/ghc"
269
+
"--with-curses-includes=${ncurses.dev}/include" "--with-curses-libraries=${ncurses.out}/lib"
270
+
] ++ lib.optionals (libffi != null) [
271
+
"--with-system-libffi"
272
+
"--with-ffi-includes=${targetPackages.libffi.dev}/include"
273
+
"--with-ffi-libraries=${targetPackages.libffi.out}/lib"
274
+
] ++ lib.optionals (targetPlatform == hostPlatform && !enableNativeBignum) [
275
+
"--with-gmp-includes=${targetPackages.gmp.dev}/include"
276
+
"--with-gmp-libraries=${targetPackages.gmp.out}/lib"
277
+
] ++ lib.optionals (targetPlatform == hostPlatform && hostPlatform.libc != "glibc" && !targetPlatform.isWindows) [
278
+
"--with-iconv-includes=${libiconv}/include"
279
+
"--with-iconv-libraries=${libiconv}/lib"
280
+
] ++ lib.optionals (targetPlatform != hostPlatform) [
281
+
"--enable-bootstrap-with-devel-snapshot"
282
+
] ++ lib.optionals useLdGold [
283
+
"CFLAGS=-fuse-ld=gold"
284
+
"CONF_GCC_LINKER_OPTS_STAGE1=-fuse-ld=gold"
285
+
"CONF_GCC_LINKER_OPTS_STAGE2=-fuse-ld=gold"
286
+
] ++ lib.optionals (disableLargeAddressSpace) [
287
+
"--disable-large-address-space"
290
+
# Make sure we never relax`$PATH` and hooks support for compatibility.
293
+
# Don’t add -liconv to LDFLAGS automatically so that GHC will add it itself.
294
+
dontAddExtraLibs = true;
296
+
nativeBuildInputs = [
297
+
perl autoconf automake m4 python3
298
+
ghc bootPkgs.alex bootPkgs.happy bootPkgs.hscolour
299
+
] ++ lib.optionals (stdenv.isDarwin && stdenv.isAarch64) [
300
+
autoSignDarwinBinariesHook
301
+
] ++ lib.optionals enableDocs [
305
+
# For building runtime libs
306
+
depsBuildTarget = toolsForTarget;
308
+
buildInputs = [ perl bash ] ++ (libDeps hostPlatform);
310
+
depsTargetTarget = map lib.getDev (libDeps targetPlatform);
311
+
depsTargetTargetPropagated = map (lib.getOutput "out") (libDeps targetPlatform);
313
+
# required, because otherwise all symbols from HSffi.o are stripped, and
314
+
# that in turn causes GHCi to abort
315
+
stripDebugFlags = [ "-S" ] ++ lib.optional (!targetPlatform.isDarwin) "--keep-file-symbols";
317
+
checkTarget = "test";
321
+
# In nixpkgs, musl based builds currently enable `pie` hardening by default
322
+
# (see `defaultHardeningFlags` in `make-derivation.nix`).
323
+
# But GHC cannot currently produce outputs that are ready for `-pie` linking.
324
+
# Thus, disable `pie` hardening, otherwise `recompile with -fPIE` errors appear.
326
+
# * https://github.com/NixOS/nixpkgs/issues/129247
327
+
# * https://gitlab.haskell.org/ghc/ghc/-/issues/19580
328
+
++ lib.optional stdenv.targetPlatform.isMusl "pie";
330
+
# big-parallel allows us to build with more than 2 cores on
331
+
# Hydra which already warrants a significant speedup
332
+
requiredSystemFeatures = [ "big-parallel" ];
335
+
# Install the bash completion file.
336
+
install -D -m 444 utils/completion/ghc.bash $out/share/bash-completion/completions/${targetPrefix}ghc
340
+
inherit bootPkgs targetPrefix;
342
+
inherit llvmPackages;
343
+
inherit enableShared;
345
+
# This is used by the haskell builder to query
346
+
# the presence of the haddock program.
347
+
hasHaddock = enableHaddockProgram;
349
+
# Our Cabal compiler name
350
+
haskellCompilerName = "ghc-${version}";
354
+
homepage = "http://haskell.org/ghc";
355
+
description = "The Glasgow Haskell Compiler";
356
+
maintainers = with lib.maintainers; [
358
+
] ++ lib.teams.haskell.members;
359
+
timeout = 24 * 3600;
360
+
inherit (ghc.meta) license platforms;
363
+
} // lib.optionalAttrs targetPlatform.useAndroidPrebuilt {
365
+
dontPatchELF = true;
366
+
noAuditTmpdir = true;