From 92eed1ed13bcacbc95ffc2704b10f223cbbddf7e Mon Sep 17 00:00:00 2001
From: Cheng Shao <terrorjack@type.dance>
Date: Thu, 3 Oct 2024 17:05:23 +0000
Subject: [PATCH] hadrian: use targetSupportsRPaths predicate

This commit changes the hostSupportsRPaths predicate to
targetSupportsRPaths and use that to decide whether to pass
RPATH-related link-time options. It's not applied to stage0, we should
just use the default link-time options of stageBoot ghc.

(cherry picked from commit a6a82cdb7162f32a1b73a2a6224d6d9cd208962c)
(cherry picked from commit 7e1f1b0778f581d2b72cfb533c65918b199e6247)
---
 hadrian/src/Oracles/Setting.hs       | 9 ++++-----
 hadrian/src/Settings/Builders/Ghc.hs | 2 +-
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/hadrian/src/Oracles/Setting.hs b/hadrian/src/Oracles/Setting.hs
index 2b6347a2be9..ea91a450fa0 100644
--- a/hadrian/src/Oracles/Setting.hs
+++ b/hadrian/src/Oracles/Setting.hs
@@ -5,7 +5,7 @@ module Oracles.Setting (
     ToolchainSetting (..), settingsFileSetting,
 
     -- * Helpers
-    ghcCanonVersion, cmdLineLengthLimit, hostSupportsRPaths, topDirectory,
+    ghcCanonVersion, cmdLineLengthLimit, targetSupportsRPaths, topDirectory,
     libsuf, ghcVersionStage, bashPath, targetStage,
 
     -- ** Target platform things
@@ -184,15 +184,15 @@ anyTargetOs oss = (`elem` oss) <$> queryTargetTarget (archOS_OS . tgtArchOs)
 isElfTarget :: Action Bool
 isElfTarget = queryTargetTarget (osElfTarget . archOS_OS . tgtArchOs)
 
--- | Check whether the host OS supports the @-rpath@ linker option when
+-- | Check whether the target OS supports the @-rpath@ linker option when
 -- using dynamic linking.
 --
 -- ROMES:TODO: Whether supports -rpath should be determined by ghc-toolchain
 --
 -- TODO: Windows supports lazy binding (but GHC doesn't currently support
 --       dynamic way on Windows anyways).
-hostSupportsRPaths :: Action Bool
-hostSupportsRPaths = queryHostTarget (\t -> let os = archOS_OS (tgtArchOs t)
+targetSupportsRPaths :: Action Bool
+targetSupportsRPaths = queryTargetTarget (\t -> let os = archOS_OS (tgtArchOs t)
                                              in osElfTarget os || osMachOTarget os)
 
 -- | Check whether the target supports GHCi.
@@ -265,4 +265,3 @@ targetStage (Stage0 {}) = getHostTarget
 targetStage (Stage1 {}) = getTargetTarget
 targetStage (Stage2 {}) = getTargetTarget -- the last two only make sense if the target can be executed locally
 targetStage (Stage3 {}) = getTargetTarget
-
diff --git a/hadrian/src/Settings/Builders/Ghc.hs b/hadrian/src/Settings/Builders/Ghc.hs
index 6ac8de3d5f9..c1b0584c591 100644
--- a/hadrian/src/Settings/Builders/Ghc.hs
+++ b/hadrian/src/Settings/Builders/Ghc.hs
@@ -144,7 +144,7 @@ ghcLinkArgs = builder (Ghc LinkHs) ? do
                 [ arg "-dynamic"
                 -- TODO what about windows?
                 , isLibrary pkg ? pure [ "-shared", "-dynload", "deploy" ]
-                , hostSupportsRPaths ? mconcat
+                , notStage0 ? targetSupportsRPaths ? mconcat
                       [ arg ("-optl-Wl,-rpath," ++ rpath)
                       , isProgram pkg ? arg ("-optl-Wl,-rpath," ++ bindistRpath)
                       -- The darwin and Windows linkers don't support/require the -zorigin option
-- 
GitLab