on iko/ghcjs-patch-master help sign in
Support native bignum (including static builds) (#1569)

Co-authored-by: Mikaela Allan <mikaela@vitalbio.com> Co-authored-by: Travis Whitaker <pi.boy.travis@gmail.com>

commit1ccfd295aa4b568d57f54dd04879514295b323e5 authorHamish Mackenzie <Hamish.K.Mackenzie@gmail.com> dateTue 2 Aug 2022, 17:42 (4 years ago) parent2f49f0a
2 files changed+36−10
compiler/ghc/default.nix+24−2 overlays/ghc.nix+12−8
compiler/ghc/default.nix+24−2
@@ -31,6 +31,9 @@ let self =
3131, # If enabled, GHC will be built with the GPL-free but slower integer-simple
3232 # library instead of the faster but GPLed integer-gmp library.
3333 enableIntegerSimple ? !(lib.any (lib.meta.platformMatch stdenv.hostPlatform) gmp.meta.platforms), gmp
34+, # If enabled, GHC will be built with the GPL-free native backend of the
35+ # bignum library that is nearly as fast as GMP
36+ enableNativeBignum ? !((lib.any (lib.meta.platformMatch stdenv.hostPlatform) gmp.meta.platforms) || enableIntegerSimple)
3437
3538, # If enabled, use -fPIC when compiling static libs.
3639 enableRelocatedStaticLibs ? stdenv.targetPlatform != stdenv.hostPlatform && !stdenv.targetPlatform.isAarch32
@@ -71,7 +74,11 @@ let self =
7174, extra-passthru ? {}
7275}@args:
7376
74−assert !enableIntegerSimple -> gmp != null;
77+assert !(enableIntegerSimple || enableNativeBignum) -> gmp != null;
78+
79+# Early check to make sure only one of these is enabled
80+assert enableNativeBignum -> !enableIntegerSimple;
81+assert enableIntegerSimple -> !enableNativeBignum;
7582
7683let
7784 inherit (stdenv) buildPlatform hostPlatform targetPlatform;
@@ -79,6 +86,17 @@ let
7986
8087 inherit (bootPkgs) ghc;
8188
89+ ghcHasNativeBignum = builtins.compareVersions ghc-version "9.0" >= 0;
90+
91+ bignumSpec =
92+ assert ghcHasNativeBignum -> !enableIntegerSimple;
93+ assert !ghcHasNativeBignum -> !enableNativeBignum;
94+ if ghcHasNativeBignum then ''
95+ BIGNUM_BACKEND = ${if enableNativeBignum then "native" else "gmp"}
96+ '' else ''
97+ INTEGER_LIBRARY = ${if enableIntegerSimple then "integer-simple" else "integer-gmp"}
98+ '';
99+
82100 # TODO check if this possible fix for segfaults works or not.
83101 targetLibffi =
84102 # on native platforms targetPlatform.{libffi, gmp} do not exist; thus fall back
@@ -106,7 +124,7 @@ let
106124 include mk/flavours/\$(BuildFlavour).mk
107125 endif
108126 DYNAMIC_GHC_PROGRAMS = ${if enableShared then "YES" else "NO"}
109− INTEGER_LIBRARY = ${if enableIntegerSimple then "integer-simple" else "integer-gmp"}
127+ '' + bignumSpec + ''
110128 EXTRA_HADDOCK_OPTS += --quickjump --hyperlinked-source
111129 '' + lib.optionalString (targetPlatform != hostPlatform) ''
112130 CrossCompilePrefix = ${targetPrefix}
@@ -121,6 +139,10 @@ let
121139 '' + lib.optionalString enableRelocatedStaticLibs ''
122140 GhcLibHcOpts += -fPIC
123141 GhcRtsHcOpts += -fPIC
142+ GhcRtsCcOpts += -fPIC
143+ '' + lib.optionalString (enableRelocatedStaticLibs && targetPlatform.isx86_64 && !targetPlatform.isWindows) ''
144+ GhcLibHcOpts += -fexternal-dynamic-refs
145+ GhcRtsHcOpts += -fexternal-dynamic-refs
124146 '' + lib.optionalString enableDWARF ''
125147 GhcLibHcOpts += -g3
126148 GhcRtsHcOpts += -g3
overlays/ghc.nix+12−8
@@ -1,9 +1,13 @@
11final: prev: with prev;
22 # sadly we need to patch GHC a bit.
3− let
4− ghcPkgOverrides = {
5− enableIntegerSimple = false;
6− };
3+ let
4+ # The new implementation appeared in GHC 9.0
5+ hasNativeBignum = name: !lib.hasPrefix "ghc8" name;
6+
7+ ghcPkgOverrides = name: { enableIntegerSimple = false; } // lib.optionalAttrs (hasNativeBignum name) {
8+ enableNativeBignum = false;
9+ };
10+
711 ghcDrvOverrides = drv: {
812 hardeningDisable = (drv.hardeningDisable or []) ++ [ "stackprotector" "format" ] ++ lib.optionals prev.stdenv.hostPlatform.isAarch32 [ "pic" "pie" ];
913 };
@@ -20,13 +24,13 @@ final: prev: with prev;
2024 && !lib.hasPrefix "ghc82" name
2125 && !lib.hasPrefix "ghcjs" name
2226 && !lib.hasSuffix "Binary" name;
23− overrideCompiler = compiler:
24− ((compiler.override ghcPkgOverrides).overrideAttrs ghcDrvOverrides) // {
25− dwarf = overrideCompiler compiler.dwarf;
27+ overrideCompiler = name: compiler:
28+ ((compiler.override (ghcPkgOverrides name)).overrideAttrs ghcDrvOverrides) // {
29+ dwarf = overrideCompiler name compiler.dwarf;
2630 };
2731 in
2832 lib.recursiveUpdate prev.haskell-nix {
29− compiler = lib.mapAttrs (_name: overrideCompiler)
33+ compiler = lib.mapAttrs overrideCompiler
3034 (lib.filterAttrs (name: _value: needsPatches name) prev.haskell-nix.compiler);
3135 };
3236 }