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 GHC with static RTS #970

Merged
merged 3 commits into from
Jul 10, 2019
Merged

Support GHC with static RTS #970

merged 3 commits into from
Jul 10, 2019

Conversation

aherrmann
Copy link
Member

GHC can be compiled with a static RTS. This allows to use only static Haskell librarires including for GHCi and Template Haskell. This is the default on Windows.

This PR adds support for a static GHC on Unix as well. The mode of GHC (static or dynamic) is detected automatically during toolchain setup.

@aherrmann aherrmann requested review from mboes, Profpatsch and guibou June 28, 2019 12:23
@aherrmann
Copy link
Member Author

To test rules_haskell with a static GHC on Unix you can apply the following patch to define the corresponding Nix override:

diff --git a/nixpkgs/default.nix b/nixpkgs/default.nix
index f4303b0..c2e01ca 100644
--- a/nixpkgs/default.nix
+++ b/nixpkgs/default.nix
@@ -1,5 +1,31 @@
+{ ... }@args:
 import (fetchTarball {
    # Checkout from 2019-05-06.
    url = https://github.com/mboes/nixpkgs/archive/361e9185c83166b44419e05e75fc8473875df6ea.tar.gz;
    sha256 = "0c6izp9i1ln4yy2h9jlm48k94f018h8pw60jfbhq49p076aaafrb";
+}) (args // {
+  overlays = (args.overlays or []) ++ [
+    (_: pkgs: {
+      haskell = pkgs.haskell // {
+        compiler = pkgs.haskell.compiler // {
+          ghc865 = (pkgs.haskell.compiler.ghc865.override {
+            enableRelocatedStaticLibs = true;
+            enableShared = false;
+          }).overrideAttrs (attrs: {
+            # enableRelocatedStaticLibs = true only sets -fPIC. However, with
+            # enableShared = false we also require -fexternal-dynamic-refs,
+            # otherwise GHC still generates R_X86_64_PC32 relocations which
+            # prevents loading these static libraries as PIC.
+            preConfigure = ''
+              ${attrs.preConfigure or ""}
+              cat <<EOF >>mk/build.mk
+              GhcLibHcOpts += -fPIC -fexternal-dynamic-refs
+              GhcRtsHcOpts += -fPIC -fexternal-dynamic-refs
+              EOF
+            '';
+          });
+        };
+      };
+    })
+  ];
 })

@aherrmann aherrmann force-pushed the static-ghc branch 3 times, most recently from dd9384e to 5afbc0d Compare July 5, 2019 08:00
@aherrmann
Copy link
Member Author

Rebased on master and awaiting review.


return ghc

def ghc_is_static(repository_ctx):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't play nicely with RBE, or any cross-compilation for that matter.

@mboes
Copy link
Member

mboes commented Jul 9, 2019

Rather than autodetection, it may be better to ask the user to say whether the toolchain is static or not, since this is going to complicate the cross compilation case. It's like compiler versions. In the case of Nixpkgs, compiler versions in the WORKSPACE file are redundant, since we can auto detect them. But we can only reliably detect them in narrow cases: when the build architecture matches the host architecture. Here, if the target architecture is different from host or build, ghc --interactive fails.

@Profpatsch
Copy link
Contributor

I agree that we shouldn’t do autodetection because of RBE (haven’t reviewed yet though).

aherrmann added a commit that referenced this pull request Jul 10, 2019
Addressing review comment #970 (comment)

The predifined bindists only come with a static GHC on Windows.

Nixpkgs defaults to a dynamic GHC, but the user can override that
default.
@mboes mboes added the merge-queue merge on green CI label Jul 10, 2019
@aherrmann
Copy link
Member Author

I was hoping we could go the opposite direction and also autodetect the version, since a wrong version setting can lead to pretty confusing error messages. However, it seems that wouldn't work RBE either then.

Static GHC seems like a niche use-case, so I'm fine with it requiring some manual setup.

GHC can be compiled with a static RTS. This allows static only linking
with GHC, including for GHCi and Template Haskell.

The RTS mode of GHC is detected automatically on toolchain setup.
Addressing review comment #970 (comment)

The predifined bindists only come with a static GHC on Windows.

Nixpkgs defaults to a dynamic GHC, but the user can override that
default.
Debian seems to have updated the default to libtinfo6, but the GHC
bindist requires libtinfo5.
@mergify mergify bot merged commit 2f5e879 into master Jul 10, 2019
@mergify mergify bot deleted the static-ghc branch July 10, 2019 13:34
@mergify mergify bot removed the merge-queue merge on green CI label Jul 10, 2019
@aherrmann aherrmann mentioned this pull request Aug 13, 2019
4 tasks
@aherrmann aherrmann mentioned this pull request Jun 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants