33# haskell.nix ships its own version of the ghc expression as it needs more
44# control over the expression to isolate it against varying <nixpkgs> and
55# allow us to customize it to the way haskell.nix works.
6- { stdenv , targetPackages
6+ { stdenv , haskell-nix , targetPackages
77
88# build-tools
99, bootPkgs
3333
3434, # Whether to build dynamic libs for the standard library (on the target
3535 # platform). Static libs are always built.
36- enableShared ? ! stdenv . targetPlatform . isWindows && ! stdenv . targetPlatform . useiOSPrebuilt
36+ enableShared ? ! haskell-nix . haskellLib . isCrossTarget
3737
38- , # Whetherto build terminfo.
39- enableTerminfo ? ! stdenv . targetPlatform . isWindows
38+ , # Whetherto build terminfo. Musl fails to build terminfo as ncurses seems to be linked to glibc
39+ enableTerminfo ? ! stdenv . targetPlatform . isWindows && ! stdenv . targetPlatform . isMusl
4040
4141, # What flavour to build. An empty string indicates no
4242 # specific flavour and falls back to ghc default values.
43- ghcFlavour ? stdenv . lib . optionalString ( stdenv . targetPlatform != stdenv . hostPlatform )
44- ( if useLLVM then "quick-cross" else "perf-cross-ncg" )
43+ ghcFlavour ? stdenv . lib . optionalString haskell-nix . haskellLib . isCrossTarget (
44+ if useLLVM
45+ then (
46+ # TODO check if the issues with qemu and Aarch32 persist. See
47+ # https://github.com/input-output-hk/haskell.nix/pull/411/commits/1986264683067198e7fdc1d665351622b664712e
48+ if stdenv . targetPlatform . isAarch32
49+ then "quick-cross"
50+ else "perf-cross"
51+ )
52+ else "perf-cross-ncg"
53+ )
4554
4655, # Whether to disable the large address space allocator
4756 # necessary fix for iOS: https://www.reddit.com/r/haskell/comments/4ttdz1/building_an_osxi386_to_iosarm64_cross_compiler/d5qvd67/
@@ -59,9 +68,15 @@ assert !enableIntegerSimple -> gmp != null;
5968
6069let
6170 inherit ( stdenv ) buildPlatform hostPlatform targetPlatform ;
71+ inherit ( haskell-nix . haskellLib ) isCrossTarget ;
6272
6373 inherit ( bootPkgs ) ghc ;
6474
75+ # TODO check if this posible fix for segfaults works or not.
76+ libffiStaticEnabled = if libffi == null || ! stdenv . targetPlatform . isMusl
77+ then libffi
78+ else targetPackages . libffi . overrideAttrs ( old : { dontDisableStatic = true ; } ) ;
79+
6580 # TODO(@Ericson2314) Make unconditional
6681 targetPrefix = stdenv . lib . optionalString
6782 ( targetPlatform != hostPlatform )
7590 DYNAMIC_GHC_PROGRAMS = ${ if enableShared then "YES" else "NO" }
7691 INTEGER_LIBRARY = ${ if enableIntegerSimple then "integer-simple" else "integer-gmp" }
7792 '' + stdenv . lib . optionalString ( targetPlatform != hostPlatform ) ''
78- Stage1Only = ${ if targetPlatform . system == hostPlatform . system then "NO" else "YES" }
7993 CrossCompilePrefix = ${ targetPrefix }
94+ '' + stdenv . lib . optionalString isCrossTarget ''
95+ Stage1Only = ${ if targetPlatform . system == hostPlatform . system then "NO" else "YES" }
8096 HADDOCK_DOCS = NO
8197 BUILD_SPHINX_HTML = NO
8298 BUILD_SPHINX_PDF = NO
88104 '' + stdenv . lib . optionalString useLLVM ''
89105 GhcStage2HcOpts += -fast-llvm
90106 GhcLibHcOpts += -fast-llvm
107+ '' + stdenv . lib . optionalString ( ! enableTerminfo ) ''
108+ WITH_TERMINFO=NO
109+ ''
110+ # While split sections are now enabled by default in ghc 8.8 for windows,
111+ # the seem to lead to `too many sections` errors when building base for
112+ # profiling.
113+ + stdenv . lib . optionalString targetPlatform . isWindows ''
114+ SplitSections = NO
91115 '' ;
92116
93117 # Splicer will pull out correct variations
94118 libDeps = platform : stdenv . lib . optional enableTerminfo [ ncurses ]
95- ++ [ libffi ]
119+ ++ [ libffiStaticEnabled ]
96120 ++ stdenv . lib . optional ( ! enableIntegerSimple ) gmp
97121 ++ stdenv . lib . optional ( platform . libc != "glibc" && ! targetPlatform . isWindows ) libiconv ;
98122
@@ -184,7 +208,7 @@ in let configured-src = stdenv.mkDerivation (rec {
184208 configureFlags = [
185209 "--datadir=$doc/share/doc/ghc"
186210 "--with-curses-includes=${ ncurses . dev } /include" "--with-curses-libraries=${ ncurses . out } /lib"
187- ] ++ stdenv . lib . optionals ( libffi != null ) [ "--with-system-libffi" "--with-ffi-includes=${ targetPackages . libffi . dev } /include" "--with-ffi-libraries=${ targetPackages . libffi . out } /lib"
211+ ] ++ stdenv . lib . optionals ( libffiStaticEnabled != null ) [ "--with-system-libffi" "--with-ffi-includes=${ libffiStaticEnabled . dev } /include" "--with-ffi-libraries=${ libffiStaticEnabled . out } /lib"
188212 ] ++ stdenv . lib . optional ( ! enableIntegerSimple ) [
189213 "--with-gmp-includes=${ targetPackages . gmp . dev } /include" "--with-gmp-libraries=${ targetPackages . gmp . out } /lib"
190214 ] ++ stdenv . lib . optional ( targetPlatform == hostPlatform && hostPlatform . libc != "glibc" && ! targetPlatform . isWindows ) [
@@ -298,7 +322,12 @@ in let configured-src = stdenv.mkDerivation (rec {
298322 for i in "$out/bin/"*; do
299323 test ! -h $i || continue
300324 egrep --quiet '^#!' <(head -n 1 $i) || continue
301- sed -i -e '2i export PATH="$PATH:${ stdenv . lib . makeBinPath [ targetPackages . stdenv . cc . bintools coreutils ] } "' $i
325+ # The ghcprog fixup is for musl (where runhaskell script just needs to point to the correct
326+ # ghc program to work).
327+ sed -i \
328+ -e '2i export PATH="$PATH:${ stdenv . lib . makeBinPath [ targetPackages . stdenv . cc . bintools coreutils ] } "' \
329+ -e 's/ghcprog="ghc-/ghcprog="${ targetPrefix } ghc-/' \
330+ $i
302331 done
303332 '' + installDeps targetPrefix ;
304333
0 commit comments