Skip to content

Commit fb2cc4c

Browse files
jappeace-slothjappeace
authored andcommitted
Fix --host= triple mangling during cross-compilation
issue: #5887 I don't like you've to use nix to cross compile, cabal should be able to do this.
1 parent 71b3c85 commit fb2cc4c

File tree

9 files changed

+98
-3
lines changed

9 files changed

+98
-3
lines changed

Cabal/src/Distribution/Simple.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ import Language.Haskell.Extension
135135

136136
-- Base
137137
import Data.List (unionBy, (\\))
138+
import qualified Data.Map as Map
138139
import System.Directory (removePathForcibly)
139140
import System.Environment (getArgs, getProgName)
140141
import System.IO (hPutStr, hPutStrLn)
@@ -981,12 +982,14 @@ autoconfUserHooks =
981982
let common = configCommonFlags flags
982983
verbosity = mkVerbosity defaultVerbosityHandles (fromFlag $ setupVerbosity common)
983984
mbWorkDir = flagToMaybe $ setupWorkingDir common
985+
targetTriple = Map.lookup "Target platform" (compilerProperties (compiler lbi))
984986
runConfigureScript
985987
defaultVerbosityHandles
986988
flags
987989
(flagAssignment lbi)
988990
(withPrograms lbi)
989991
(hostPlatform lbi)
992+
targetTriple
990993
pbi <- getHookedBuildInfo verbosity mbWorkDir (buildDir lbi)
991994
sanityCheckHookedBuildInfo verbosity pkg_descr pbi
992995
let pkg_descr' = updatePackageDescription pbi pkg_descr
@@ -1055,9 +1058,12 @@ autoconfSetupHooks =
10551058
{ LBC.configFlags = cfg
10561059
, LBC.flagAssignment = flags
10571060
, LBC.hostPlatform = plat
1061+
, LBC.compiler = comp
10581062
}
10591063
}
1060-
) = runConfigureScript defaultVerbosityHandles cfg flags progs plat
1064+
) =
1065+
let targetTriple = Map.lookup "Target platform" (compilerProperties comp)
1066+
in runConfigureScript defaultVerbosityHandles cfg flags progs plat targetTriple
10611067

10621068
pre_conf_comp
10631069
:: SetupHooks.PreConfComponentInputs

Cabal/src/Distribution/Simple/ConfigureScript.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ runConfigureScript
5656
-> ProgramDb
5757
-> Platform
5858
-- ^ host platform
59+
-> Maybe String
60+
-- ^ original target triple from compiler info (e.g. @"x86_64-w64-mingw32"@),
61+
-- used for the @--host=@ flag instead of the pretty-printed 'Platform'
5962
-> IO ()
60-
runConfigureScript verbHandles cfg flags programDb hp = do
63+
runConfigureScript verbHandles cfg flags programDb hp targetTriple = do
6164
let commonCfg = configCommonFlags cfg
6265
verbosity = mkVerbosity verbHandles (fromFlag $ setupVerbosity commonCfg)
6366
dist_dir <- findDistPrefOrDefault $ setupDistPref commonCfg
@@ -183,7 +186,7 @@ runConfigureScript verbHandles cfg flags programDb hp = do
183186
: [("CXXFLAGS", Just (mkFlagsEnv cxxFlags "CXXFLAGS")) | Just cxxFlags <- [mcxxFlags]]
184187
++ [("PATH", Just pathEnv) | not (null extraPath)]
185188
++ cabalFlagEnv
186-
maybeHostFlag = ["--host=" ++ show (pretty hp) | hp /= buildPlatform]
189+
maybeHostFlag = ["--host=" ++ fromMaybe (show (pretty hp)) targetTriple | hp /= buildPlatform]
187190
args' =
188191
configureFile'
189192
: args
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module A where
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Main (main) where
2+
3+
import Distribution.Simple
4+
5+
main :: IO ()
6+
main = defaultMainWithHooks autoconfUserHooks
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
# Minimal configure script that records its arguments.
3+
# We write all args to configure-args.txt so the test can inspect them.
4+
echo "$@" > configure-args.txt
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
3+
# A fake GHC that proxies the real GHC but overrides
4+
# "Target platform" to simulate cross-compilation
5+
# to x86_64-w64-mingw32.
6+
7+
case "$*" in
8+
*--info*)
9+
ghc "$@" | sed 's/("Target platform","[^"]*")/("Target platform","x86_64-w64-mingw32")/'
10+
;;
11+
*)
12+
exec ghc "$@"
13+
;;
14+
esac
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Test.Cabal.Prelude
2+
import Test.Cabal.Script (runghc)
3+
import Control.Monad.IO.Class
4+
import Data.List (isInfixOf)
5+
6+
-- When cross-compiling, Cabal passes a --host= flag to configure scripts.
7+
-- The value should be the original GNU triple (e.g. x86_64-w64-mingw32),
8+
-- not Cabal's canonical platform string (e.g. x86_64-windows).
9+
--
10+
-- This test uses a fake GHC wrapper that reports "x86_64-w64-mingw32"
11+
-- as its Target platform (simulating a cross-compiler), then checks
12+
-- that the configure script receives --host=x86_64-w64-mingw32
13+
-- rather than the mangled --host=x86_64-windows.
14+
main = do
15+
skipIfWindows "uses sh script as fake ghc"
16+
setupTest $ recordMode DoNotRecord $ do
17+
env <- getTestEnv
18+
let cwd = testCurrentDir env
19+
fakeGhc = cwd </> "scripts" </> "fake-ghc.sh"
20+
-- Run Setup.hs configure with our fake cross-compiler.
21+
-- We call runghc ourselves to bypass the test framework's
22+
-- --with-ghc which would override ours.
23+
_ <- liftIO $ runghc
24+
(testScriptEnv env)
25+
(Just $ testTmpDir env)
26+
(testEnvironment env)
27+
("." </> "Setup.hs")
28+
[ "configure"
29+
, "--distdir", testDistDir env
30+
, "--with-compiler", fakeGhc
31+
]
32+
-- The configure script writes its arguments to configure-args.txt
33+
-- in its working directory (dist/build/).
34+
let argsFile = testDistDir env </> "build" </> "configure-args.txt"
35+
args <- liftIO $ readFile argsFile
36+
-- The configure script should receive --host=x86_64-w64-mingw32
37+
-- (the original GNU triple), not --host=x86_64-windows.
38+
unless ("--host=x86_64-w64-mingw32" `isInfixOf` args) $
39+
error $ unlines
40+
[ "Expected --host=x86_64-w64-mingw32 in configure arguments"
41+
, "but got: " ++ args
42+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cabal-version: 2.2
2+
name: test
3+
version: 0
4+
build-type: Configure
5+
6+
library
7+
exposed-modules: A
8+
build-depends: base
9+
default-language: Haskell2010

changelog.d/pr-11718.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
synopsis: Fix --host= triple mangling during cross-compilation#11718
3+
packages: [cabal-install]
4+
prs: 11718
5+
---
6+
7+
repairs (hopefully): https://github.com/haskell/cabal/issues/5887
8+
9+
Adds an integration test which fails
10+
on master and now succeeds.

0 commit comments

Comments
 (0)