Skip to content

Commit

Permalink
Respect ghc-options and with-gcc when compiling C sources
Browse files Browse the repository at this point in the history
This fix allows users to override the C/C++ compiler, either by passing
--with-gcc to configure or adding -pgmc to ghc-options in their Cabal
file.

Tested manually with a project using C++ sources that require a more
recent version of clang than the default install (macOS). Inspected the
output of `Setup.hs build -v` to confirm that the correct arguments are
being passed to GHC when compiling the C++ sources.

Resolves: #4439
  • Loading branch information
syntheorem authored and sergv committed Jan 8, 2022
1 parent 2ae46a3 commit 25aa866
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cabal/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@
([#5431](https://github.com/haskell/cabal/pull/5431)).
* Pass command line arguments to `hsc2hs` using response files when possible
([#3122](https://github.com/haskell/cabal/issues/3122)).
* `ghc-options` and `--with-gcc` are now passed to GHC when compiling
C and C++ sources ([#4439](https://github.com/haskell/cabal/issues/4439)).

----

Expand Down
10 changes: 8 additions & 2 deletions Cabal/src/Distribution/Simple/GHC/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,10 @@ componentCcGhcOptions verbosity _implInfo lbi bi clbi odir filename =
NormalDebugInfo -> ["-g"]
MaximalDebugInfo -> ["-g3"]) ++
ccOptions bi,
ghcOptObjDir = toFlag odir
ghcOptCcProgram = maybeToFlag $ programPath <$>
lookupProgram gccProgram (withPrograms lbi),
ghcOptObjDir = toFlag odir,
ghcOptExtra = hcOptions GHC bi
}


Expand Down Expand Up @@ -337,7 +340,10 @@ componentCxxGhcOptions verbosity _implInfo lbi bi clbi odir filename =
NormalDebugInfo -> ["-g"]
MaximalDebugInfo -> ["-g3"]) ++
cxxOptions bi,
ghcOptObjDir = toFlag odir
ghcOptCcProgram = maybeToFlag $ programPath <$>
lookupProgram gccProgram (withPrograms lbi),
ghcOptObjDir = toFlag odir,
ghcOptExtra = hcOptions GHC bi
}


Expand Down
4 changes: 4 additions & 0 deletions Cabal/src/Distribution/Simple/Program/GHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@ data GhcOptions = GhcOptions {
-- | Extra header files to include for old-style FFI; the @ghc -#include@ flag.
ghcOptFfiIncludes :: NubListR FilePath,

-- | Program to use for the C and C++ compiler; the @ghc -pgmc@ flag.
ghcOptCcProgram :: Flag FilePath,

----------------------------
-- Language and extensions

Expand Down Expand Up @@ -691,6 +694,7 @@ renderGhcOptions comp _platform@(Platform _arch os) opts
_ -> "-optc"
in [ cxxflag ++ opt | opt <- ghcOptCxxOptions opts]
, [ "-opta" ++ opt | opt <- ghcOptAsmOptions opts]
, concat [ ["-pgmc", cc] | cc <- flag ghcOptCcProgram ]

-----------------
-- Linker stuff
Expand Down

0 comments on commit 25aa866

Please sign in to comment.