Skip to content

Commit 4dbb260

Browse files
committed
Pass CXX and CXXFLAGS argument to ./configure scripts for build-type: Configure
Since the compiler since ghc-9.4.8 requires a C++ toolchain, and that is configured into the compiler --info, it makes sense to pass that information onto the ./configure script. The assumption that the C++ compiler is part of the toolchain is only since ghc-9.4. Therefore, if no C++ compiler is found, then the options won't be passed to the configure script. For compilers older than 9.4, the C++ compiler will always be found as it is configured into the settings file. See https://gitlab.haskell.org/ghc/ghc/-/issues/25767#note_610831 for some discussion about how this would be useful to implement system-cxx-std-lib in userspace. Fixes #10797
1 parent d4d92e9 commit 4dbb260

File tree

13 files changed

+142
-7
lines changed

13 files changed

+142
-7
lines changed

Cabal/src/Distribution/Simple/ConfigureScript.hs

+22-6
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ runConfigureScript cfg flags programDb hp = do
7878
-- We don't try and tell configure which ld to use, as we don't have
7979
-- a way to pass its flags too
8080

81+
-- Do not presume the CXX compiler is available, but it always will be after 9.4.
82+
(mcxxProgShort, mcxxFlags) <- do
83+
mprog <- needProgram verbosity gppProgram programDb
84+
case mprog of
85+
Just (p, _) -> do
86+
let pInv = programInvocation p []
87+
let cxxProg = progInvokePath pInv
88+
let cxxFlags = progInvokeArgs pInv
89+
cxxProgShort <- getShortPathName cxxProg
90+
return (Just cxxProgShort, Just cxxFlags)
91+
Nothing -> return (Nothing, Nothing)
92+
93+
8194
let configureFile' = toUnix configureFile
8295
-- autoconf is fussy about filenames, and has a set of forbidden
8396
-- characters that can't appear in the build directory, etc:
@@ -159,21 +172,24 @@ runConfigureScript cfg flags programDb hp = do
159172
)
160173
]
161174
let extraPath = fromNubList $ configProgramPathExtra cfg
162-
let cflagsEnv =
163-
maybe (unwords ccFlags) (++ (" " ++ unwords ccFlags)) $
164-
lookup "CFLAGS" env
175+
let mkFlagsEnv fs var = maybe (unwords fs) (++ (" " ++ unwords fs)) (lookup var env)
165176
spSep = [FilePath.searchPathSeparator]
166177
pathEnv =
167178
maybe
168179
(intercalate spSep extraPath)
169180
((intercalate spSep extraPath ++ spSep) ++)
170181
$ lookup "PATH" env
171182
overEnv =
172-
("CFLAGS", Just cflagsEnv)
173-
: [("PATH", Just pathEnv) | not (null extraPath)]
183+
("CFLAGS", Just (mkFlagsEnv ccFlags "CFLAGS"))
184+
: [("CXXFLAGS", Just (mkFlagsEnv cxxFlags "CXXFLAGS")) | Just cxxFlags <- [mcxxFlags] ]
185+
++ [("PATH", Just pathEnv) | not (null extraPath)]
174186
++ cabalFlagEnv
175187
maybeHostFlag = if hp == buildPlatform then [] else ["--host=" ++ show (pretty hp)]
176-
args' = configureFile' : args ++ ["CC=" ++ ccProgShort] ++ maybeHostFlag
188+
args' = configureFile' :
189+
args ++
190+
["CC=" ++ ccProgShort ] ++
191+
["CXX=" ++ cxxProgShort | Just cxxProgShort <- [mcxxProgShort] ] ++
192+
maybeHostFlag
177193
shProg = simpleProgram "sh"
178194
progDb <- prependProgramSearchPath verbosity extraPath [] emptyProgramDb
179195
shConfiguredProg <-

Cabal/src/Distribution/Simple/GHC/Internal.hs

+20-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ configureToolchain _implInfo ghcProg ghcInfo =
110110
{ programFindLocation = findProg gccProgramName extraGccPath
111111
, programPostConf = configureGcc
112112
}
113+
. addKnownProgram
114+
gppProgram
115+
{ programFindLocation = findProg gppProgramName extraGppPath
116+
, programPostConf = configureGpp
117+
}
113118
. addKnownProgram
114119
ldProgram
115120
{ programFindLocation = findProg ldProgramName extraLdPath
@@ -137,6 +142,7 @@ configureToolchain _implInfo ghcProg ghcInfo =
137142
maybeName prog = maybe (programName prog) (dropExeExtension . takeFileName)
138143

139144
gccProgramName = maybeName gccProgram mbGccLocation
145+
gppProgramName = maybeName gppProgram mbGppLocation
140146
ldProgramName = maybeName ldProgram mbLdLocation
141147
arProgramName = maybeName arProgram mbArLocation
142148
stripProgramName = maybeName stripProgram mbStripLocation
@@ -149,18 +155,20 @@ configureToolchain _implInfo ghcProg ghcInfo =
149155
mbDir = maybeToList . fmap takeDirectory $ mbPath
150156

151157
extraGccPath = mkExtraPath mbGccLocation windowsExtraGccDir
158+
extraGppPath = mkExtraPath mbGppLocation windowsExtraGppDir
152159
extraLdPath = mkExtraPath mbLdLocation windowsExtraLdDir
153160
extraArPath = mkExtraPath mbArLocation windowsExtraArDir
154161
extraStripPath = mkExtraPath mbStripLocation windowsExtraStripDir
155162

156163
-- on Windows finding and configuring ghc's gcc & binutils is a bit special
157164
( windowsExtraGccDir
165+
, windowsExtraGppDir
158166
, windowsExtraLdDir
159167
, windowsExtraArDir
160168
, windowsExtraStripDir
161169
) =
162170
let b = mingwBinDir </> binPrefix
163-
in (b, b, b, b)
171+
in (b, b, b, b, b)
164172

165173
findProg
166174
:: String
@@ -176,11 +184,13 @@ configureToolchain _implInfo ghcProg ghcInfo =
176184
-- Read tool locations from the 'ghc --info' output. Useful when
177185
-- cross-compiling.
178186
mbGccLocation = Map.lookup "C compiler command" ghcInfo
187+
mbGppLocation = Map.lookup "C++ compiler command" ghcInfo
179188
mbLdLocation = Map.lookup "ld command" ghcInfo
180189
mbArLocation = Map.lookup "ar command" ghcInfo
181190
mbStripLocation = Map.lookup "strip command" ghcInfo
182191

183192
ccFlags = getFlags "C compiler flags"
193+
cxxFlags = getFlags "C++ compiler flags"
184194
-- GHC 7.8 renamed "Gcc Linker flags" to "C compiler link flags"
185195
-- and "Ld Linker flags" to "ld flags" (GHC #4862).
186196
gccLinkerFlags = getFlags "Gcc Linker flags" ++ getFlags "C compiler link flags"
@@ -210,6 +220,15 @@ configureToolchain _implInfo ghcProg ghcInfo =
210220
++ gccLinkerFlags
211221
}
212222

223+
configureGpp :: Verbosity -> ConfiguredProgram -> IO ConfiguredProgram
224+
configureGpp _v gppProg = do
225+
return
226+
gppProg
227+
{ programDefaultArgs =
228+
programDefaultArgs gppProg
229+
++ cxxFlags
230+
}
231+
213232
configureLd :: Verbosity -> ConfiguredProgram -> IO ConfiguredProgram
214233
configureLd v ldProg = do
215234
ldProg' <- configureLd' v ldProg

Cabal/src/Distribution/Simple/Program.hs

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ module Distribution.Simple.Program
115115
, jhcProgram
116116
, uhcProgram
117117
, gccProgram
118+
, gppProgram
118119
, arProgram
119120
, stripProgram
120121
, happyProgram

Cabal/src/Distribution/Simple/Program/Builtin.hs

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module Distribution.Simple.Program.Builtin
2626
, haskellSuitePkgProgram
2727
, uhcProgram
2828
, gccProgram
29+
, gppProgram
2930
, arProgram
3031
, stripProgram
3132
, happyProgram
@@ -262,6 +263,12 @@ gccProgram =
262263
{ programFindVersion = findProgramVersion "-dumpversion" id
263264
}
264265

266+
gppProgram :: Program
267+
gppProgram =
268+
(simpleProgram "g++")
269+
{ programFindVersion = findProgramVersion "-dumpversion" id
270+
}
271+
265272
arProgram :: Program
266273
arProgram = simpleProgram "ar"
267274

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Revision history for ConfigureCXX
2+
3+
## 0.1.0.0 -- YYYY-mm-dd
4+
5+
* First version. Released on an unsuspecting world.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
cabal-version: 3.14
2+
name: ConfigureCXX
3+
version: 0.1.0.0
4+
license: NONE
5+
author: Matthew Pickering
6+
maintainer: [email protected]
7+
build-type: Configure
8+
extra-doc-files: CHANGELOG.md
9+
10+
common warnings
11+
ghc-options: -Wall
12+
13+
library
14+
import: warnings
15+
exposed-modules: MyLib
16+
build-depends: base ^>=4.20.0.0
17+
hs-source-dirs: src
18+
default-language: Haskell2010
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Distribution.Simple
2+
3+
main = defaultMainWithHooks autoconfUserHooks
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Setup configure
2+
Configuring ConfigureCXX-0.1.0.0...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Test.Cabal.Prelude
2+
3+
-- Test that configure scripts are passed CXX variable.
4+
main = setupTest $ do
5+
-- 9.4 was the first version which required a C++ compiler.
6+
skipUnlessGhcVersion ">= 9.4"
7+
setup "configure" []
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
for argument; do #syntactic sugar for: for argument in "$@"; do
2+
key=${argument%%=*}
3+
value=${argument#*=}
4+
5+
case "$key" in
6+
7+
CC) MCC=$value;;
8+
CFLAGS) MCFLAGS=$value;;
9+
CXX) MCXX=$value;;
10+
CXXFLAGS) MCXXFLAGS=$value;;
11+
esac
12+
done
13+
14+
echo $PWD
15+
ls $PWD
16+
17+
cat > hello.c << EOF
18+
#include <stdio.h>
19+
int main() {
20+
// printf() displays the string inside quotation
21+
printf("Hello, World!");
22+
return 0;
23+
}
24+
EOF
25+
26+
cat > hello.cpp << EOF
27+
#include <iostream>
28+
29+
int main() {
30+
std::cout << "Hello, World!" << std::endl;
31+
return 0;
32+
}
33+
EOF
34+
35+
# Test the arguments works for C
36+
$MCC $MCFLAGS hello.c
37+
38+
# Test the arguments work for CXX
39+
$MCXX $MCXXFLAGS hello.cpp
40+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <stdio.h>
2+
int main() {
3+
// printf() displays the string inside quotation
4+
printf("Hello, World!");
5+
return 0;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <iostream>
2+
3+
int main() {
4+
std::cout << "Hello, World!" << std::endl;
5+
return 0;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module MyLib (someFunc) where
2+
3+
someFunc :: IO ()
4+
someFunc = putStrLn "someFunc"

0 commit comments

Comments
 (0)