Skip to content

Commit 81d5cb1

Browse files
1 parent 994e845 commit 81d5cb1

File tree

3 files changed

+412
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)