From b45080a3e34200767b76faca495f5aea95bb94f5 Mon Sep 17 00:00:00 2001
From: Cheng Shao <terrorjack@type.dance>
Date: Thu, 2 May 2024 13:07:17 +0000
Subject: [PATCH] hadrian: add the host_fully_static flavour transformer

This commit adds the host_fully_static flavour transformer to hadrian,
which ensures stage0 is fully statically linked while still permitting
stage1 libdir to contain shared libraries. This is intended to be used
by the wasm backend to build portable linux bindists that contain wasm
shared libraries.
---
 hadrian/doc/flavours.md |  4 ++++
 hadrian/src/Flavour.hs  | 18 ++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/hadrian/doc/flavours.md b/hadrian/doc/flavours.md
index 0ab2b634362..c7f02ca4a5f 100644
--- a/hadrian/doc/flavours.md
+++ b/hadrian/doc/flavours.md
@@ -299,6 +299,10 @@ The supported transformers are listed below:
         <td><code>fully_static</code></td>
         <td>Produce fully statically-linked executables and build libraries suitable for static linking.</td>
     </tr>
+    <tr>
+        <td><code>host_fully_static</code></td>
+        <td>Ensure host executables are fully static, while still permitting shared target libraries.</td>
+    </tr>
     <tr>
         <td><code>collect_timings</code></td>
         <td>Collects timings while building the stage2+ compiler by adding the
diff --git a/hadrian/src/Flavour.hs b/hadrian/src/Flavour.hs
index 868284c3317..793a55a036c 100644
--- a/hadrian/src/Flavour.hs
+++ b/hadrian/src/Flavour.hs
@@ -60,6 +60,7 @@ flavourTransformers = M.fromList
     , "omit_pragmas"     =: omitPragmas
     , "ipe"              =: enableIPE
     , "fully_static"     =: fullyStatic
+    , "host_fully_static" =: hostFullyStatic
     , "collect_timings"  =: collectTimings
     , "assertions"       =: enableAssertions
     , "debug_ghc"        =: debugGhc Stage2
@@ -371,6 +372,23 @@ fullyStatic flavour =
         , builder (Ghc LinkHs) ? pure [ "-optl", "-static" ]
         ]
 
+-- | Ensure stage0 executables and libraries are fully static. Useful
+-- for building cross GHC bindists that still contain shared target
+-- libraries.
+hostFullyStatic :: Flavour -> Flavour
+hostFullyStatic flavour =
+    addArgs staticExec $ disableDynamicGhcPrograms flavour
+  where
+    -- Unlike 'fullyStatic', we need to ensure these flags are only
+    -- applied to host code.
+    staticExec :: Args
+    staticExec = stage0 ? mconcat
+        [
+          builder (Ghc CompileHs) ? pure [ "-fPIC", "-static" ]
+        , builder (Ghc CompileCWithGhc) ? pure [ "-fPIC", "-optc", "-static"]
+        , builder (Ghc LinkHs) ? pure [ "-optl", "-static" ]
+        ]
+
 -- | Build stage2 dependencies with options to enable collection of compiler
 -- stats.
 collectTimings :: Flavour -> Flavour
-- 
GitLab