Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support native bignum (including static builds) #1569

Merged
merged 7 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions compiler/ghc/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ let self =
, # If enabled, GHC will be built with the GPL-free but slower integer-simple
# library instead of the faster but GPLed integer-gmp library.
enableIntegerSimple ? !(lib.any (lib.meta.platformMatch stdenv.hostPlatform) gmp.meta.platforms), gmp
, # If enabled, GHC will be built with the GPL-free native backend of the
# bignum library that is nearly as fast as GMP
enableNativeBignum ? !((lib.any (lib.meta.platformMatch stdenv.hostPlatform) gmp.meta.platforms) || enableIntegerSimple)

, # If enabled, use -fPIC when compiling static libs.
enableRelocatedStaticLibs ? stdenv.targetPlatform != stdenv.hostPlatform && !stdenv.targetPlatform.isAarch32
Expand Down Expand Up @@ -71,14 +74,29 @@ let self =
, extra-passthru ? {}
}@args:

assert !enableIntegerSimple -> gmp != null;
assert !(enableIntegerSimple || enableNativeBignum) -> gmp != null;

# Early check to make sure only one of these is enabled
assert enableNativeBignum -> !enableIntegerSimple;
assert enableIntegerSimple -> !enableNativeBignum;

let
inherit (stdenv) buildPlatform hostPlatform targetPlatform;
inherit (haskell-nix.haskellLib) isCrossTarget;

inherit (bootPkgs) ghc;

ghcHasNativeBignum = builtins.compareVersions ghc-version "9.0" >= 0;

bignumSpec =
assert ghcHasNativeBignum -> !enableIntegerSimple;
assert !ghcHasNativeBignum -> !enableNativeBignum;
if ghcHasNativeBignum then ''
BIGNUM_BACKEND = ${if enableNativeBignum then "native" else "gmp"}
'' else ''
INTEGER_LIBRARY = ${if enableIntegerSimple then "integer-simple" else "integer-gmp"}
'';

# TODO check if this possible fix for segfaults works or not.
targetLibffi =
# on native platforms targetPlatform.{libffi, gmp} do not exist; thus fall back
Expand Down Expand Up @@ -106,7 +124,7 @@ let
include mk/flavours/\$(BuildFlavour).mk
endif
DYNAMIC_GHC_PROGRAMS = ${if enableShared then "YES" else "NO"}
INTEGER_LIBRARY = ${if enableIntegerSimple then "integer-simple" else "integer-gmp"}
'' + bignumSpec + ''
EXTRA_HADDOCK_OPTS += --quickjump --hyperlinked-source
'' + lib.optionalString (targetPlatform != hostPlatform) ''
CrossCompilePrefix = ${targetPrefix}
Expand All @@ -121,6 +139,10 @@ let
'' + lib.optionalString enableRelocatedStaticLibs ''
GhcLibHcOpts += -fPIC
GhcRtsHcOpts += -fPIC
GhcRtsCcOpts += -fPIC
'' + lib.optionalString (enableRelocatedStaticLibs && targetPlatform.isx86_64 && !targetPlatform.isWindows) ''
GhcLibHcOpts += -fexternal-dynamic-refs
GhcRtsHcOpts += -fexternal-dynamic-refs
'' + lib.optionalString enableDWARF ''
GhcLibHcOpts += -g3
GhcRtsHcOpts += -g3
Expand Down
20 changes: 12 additions & 8 deletions overlays/ghc.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
final: prev: with prev;
# sadly we need to patch GHC a bit.
let
ghcPkgOverrides = {
enableIntegerSimple = false;
};
let
# The new implementation appeared in GHC 9.0
hasNativeBignum = name: !lib.hasPrefix "ghc8" name;

ghcPkgOverrides = name: { enableIntegerSimple = false; } // lib.optionalAttrs (hasNativeBignum name) {
enableNativeBignum = false;
};

ghcDrvOverrides = drv: {
hardeningDisable = (drv.hardeningDisable or []) ++ [ "stackprotector" "format" ] ++ lib.optionals prev.stdenv.hostPlatform.isAarch32 [ "pic" "pie" ];
};
Expand All @@ -20,13 +24,13 @@ final: prev: with prev;
&& !lib.hasPrefix "ghc82" name
&& !lib.hasPrefix "ghcjs" name
&& !lib.hasSuffix "Binary" name;
overrideCompiler = compiler:
((compiler.override ghcPkgOverrides).overrideAttrs ghcDrvOverrides) // {
dwarf = overrideCompiler compiler.dwarf;
overrideCompiler = name: compiler:
((compiler.override (ghcPkgOverrides name)).overrideAttrs ghcDrvOverrides) // {
dwarf = overrideCompiler name compiler.dwarf;
};
in
lib.recursiveUpdate prev.haskell-nix {
compiler = lib.mapAttrs (_name: overrideCompiler)
compiler = lib.mapAttrs overrideCompiler
(lib.filterAttrs (name: _value: needsPatches name) prev.haskell-nix.compiler);
};
}