From 1a5e5db58b45ad5587444371bc5fc8ae76018fe7 Mon Sep 17 00:00:00 2001
From: Cheng Shao <terrorjack@type.dance>
Date: Tue, 7 May 2024 20:35:47 +0000
Subject: [PATCH] hadrian: disable PIC for in-tree GMP on wasm32

This patch disables PIC for in-tree GMP on wasm32 target. Enabling PIC
unconditionally adds undesired code size and runtime overhead for
wasm32.

(cherry picked from commit f9c1ae122ec642c0d9236dffc971bc2d1ca38fba)
(cherry picked from commit 5f728fc4dd3121aa4d2809a68629a6422b53e95c)
(cherry picked from commit f45b02a8f480c19f42a44feb655481a45b64fab8)
---
 hadrian/src/Settings/Builders/Configure.hs | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/hadrian/src/Settings/Builders/Configure.hs b/hadrian/src/Settings/Builders/Configure.hs
index e88a8bde4ae..99c82d6675c 100644
--- a/hadrian/src/Settings/Builders/Configure.hs
+++ b/hadrian/src/Settings/Builders/Configure.hs
@@ -14,7 +14,6 @@ configureBuilderArgs = do
                 targetPlatform <- getSetting TargetPlatform
                 buildPlatform <- getSetting BuildPlatform
                 pure $ [ "--enable-shared=no"
-                     , "--with-pic=yes"
                      , "--host=" ++ targetPlatform    -- GMP's host is our target
                      , "--build=" ++ buildPlatform ]
                      -- Disable GMP's alloca usage on wasm32, it may
@@ -24,6 +23,10 @@ configureBuilderArgs = do
                      -- more detailed explanation of this configure
                      -- option.
                      <> [ "--enable-alloca=malloc-reentrant" | targetArch == "wasm32" ]
+                     -- Enable PIC unless target is wasm32, in which
+                     -- case we don't want libgmp.a to be bloated due
+                     -- to PIC overhead.
+                     <> [ "--with-pic=yes" | targetArch /= "wasm32" ]
 
             , builder (Configure libffiPath) ? do
                 top            <- expr topDirectory
-- 
GitLab