Skip to content

Commit

Permalink
ghc: enable static RTS
Browse files Browse the repository at this point in the history
This change allows loading statically compiled libreries into a running
GHC, thus fixing the build of haskell packages that use TemplateHaskell.
See [1] for the details.

Fixes issue NixOS#61575, NixOS#124284.

[1]: https://www.tweag.io/blog/2020-09-30-bazel-static-haskell/
  • Loading branch information
rnhmjoj committed Mar 9, 2022
1 parent 86e22e8 commit 3c5a1f7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 8 additions & 4 deletions pkgs/development/compilers/ghc/9.0.2.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

, # Whether to build dynamic libs for the standard library (on the target
# platform). Static libs are always built.
enableShared ? !stdenv.targetPlatform.isWindows && !stdenv.targetPlatform.useiOSPrebuilt
enableShared ? with stdenv.targetPlatform; !isWindows && !useiOSPrebuilt && !isStatic

, # Whether to build terminfo.
enableTerminfo ? !stdenv.targetPlatform.isWindows
Expand Down Expand Up @@ -105,9 +105,13 @@ let
CrossCompilePrefix = ${targetPrefix}
'' + lib.optionalString (!enableProfiledLibs) ''
GhcLibWays = "v dyn"
'' + lib.optionalString enableRelocatedStaticLibs ''
GhcLibHcOpts += -fPIC
GhcRtsHcOpts += -fPIC
'' +
# -fexternal-dynamic-refs apparently (because it's not clear from the documentation)
# makes the GHC RTS able to load static libraries, which may be needed for TemplateHaskell.
# This solution was described in https://www.tweag.io/blog/2020-09-30-bazel-static-haskell
lib.optionalString enableRelocatedStaticLibs ''
GhcLibHcOpts += -fPIC -fexternal-dynamic-refs
GhcRtsHcOpts += -fPIC -fexternal-dynamic-refs
'' + lib.optionalString targetPlatform.useAndroidPrebuilt ''
EXTRA_CC_OPTS += -std=gnu99
'';
Expand Down
12 changes: 8 additions & 4 deletions pkgs/development/compilers/ghc/9.2.2.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

, # Whether to build dynamic libs for the standard library (on the target
# platform). Static libs are always built.
enableShared ? !stdenv.targetPlatform.isWindows && !stdenv.targetPlatform.useiOSPrebuilt
enableShared ? with stdenv.targetPlatform; !isWindows && !useiOSPrebuilt && !isStatic

, # Whether to build terminfo.
enableTerminfo ? !stdenv.targetPlatform.isWindows
Expand Down Expand Up @@ -106,9 +106,13 @@ let
CrossCompilePrefix = ${targetPrefix}
'' + lib.optionalString (!enableProfiledLibs) ''
GhcLibWays = "v dyn"
'' + lib.optionalString enableRelocatedStaticLibs ''
GhcLibHcOpts += -fPIC
GhcRtsHcOpts += -fPIC
'' +
# -fexternal-dynamic-refs apparently (because it's not clear from the documentation)
# makes the GHC RTS able to load static libraries, which may be needed for TemplateHaskell.
# This solution was described in https://www.tweag.io/blog/2020-09-30-bazel-static-haskell
lib.optionalString enableRelocatedStaticLibs ''
GhcLibHcOpts += -fPIC -fexternal-dynamic-refs
GhcRtsHcOpts += -fPIC -fexternal-dynamic-refs
'' + lib.optionalString targetPlatform.useAndroidPrebuilt ''
EXTRA_CC_OPTS += -std=gnu99
'';
Expand Down

0 comments on commit 3c5a1f7

Please sign in to comment.