Commit 8a13f3aa authored by Ethan Kiang's avatar Ethan Kiang
Browse files

Pass '-x c++' and '-std=c++11' to `cc` for cpp files, in Hadrian

'-x c++' was found to be required on Darwin Clang 11 and 12.
'-std=c++' was found to be needed on Clang 12 but not 11.
parent 5abf5997
Pipeline #37527 passed with stages
in 314 minutes and 46 seconds
{-# LANGUAGE InstanceSigs, TypeOperators #-}
module Builder (
-- * Data types
ArMode (..), CcMode (..), ConfigurationInfo (..), GhcMode (..),
GhcPkgMode (..), HaddockMode (..), SphinxMode (..), TarMode (..),
Builder (..),
ArMode (..), CcMode (..), ConfigurationInfo (..), DependencyType (..),
GhcMode (..), GhcPkgMode (..), HaddockMode (..), SphinxMode (..),
TarMode (..), Builder (..),
-- * Builder properties
builderProvenance, systemBuilderPath, builderPath, isSpecified, needBuilder,
......@@ -41,12 +41,17 @@ import Packages
-- | C compiler can be used in two different modes:
-- * Compile or preprocess a source file.
-- * Extract source dependencies by passing @-MM@ command line argument.
data CcMode = CompileC | FindCDependencies deriving (Eq, Generic, Show)
data CcMode = CompileC | FindCDependencies DependencyType deriving (Eq, Generic, Show)
data DependencyType = CDep | CxxDep deriving (Eq, Generic, Show)
instance Binary CcMode
instance Hashable CcMode
instance NFData CcMode
instance Binary DependencyType
instance Hashable DependencyType
instance NFData DependencyType
-- | GHC can be used in four different modes:
-- * Compile a Haskell source file.
-- * Compile a C source file.
......
......@@ -391,7 +391,8 @@ builderSetting =
ccBuilder =
[ ("c", CompileC)
, ("deps", FindCDependencies)
-- Not sure how to handle the FindCDependencies CxxDep case
, ("deps", FindCDependencies CDep)
]
stages = map (\stg -> (stageString stg, stg)) [minBound..maxBound]
......
......@@ -239,7 +239,7 @@ compileNonHsObject rs lang path = do
Cmm -> obj2src "cmm" isGeneratedCmmFile ctx path
Cxx -> obj2src "cpp" (const False) ctx path
need [src]
needDependencies ctx src (path <.> "d")
needDependencies lang ctx src (path <.> "d")
buildWithResources rs $ target ctx (builder stage) [src] [path]
-- * Helpers
......@@ -247,11 +247,11 @@ compileNonHsObject rs lang path = do
-- | Discover dependencies of a given source file by iteratively calling @gcc@
-- in the @-MM -MG@ mode and building generated dependencies if they are missing
-- until reaching a fixed point.
needDependencies :: Context -> FilePath -> FilePath -> Action ()
needDependencies context@Context {..} src depFile = discover
needDependencies :: SourceLang -> Context -> FilePath -> FilePath -> Action ()
needDependencies lang context@Context {..} src depFile = discover
where
discover = do
build $ target context (Cc FindCDependencies stage) [src] [depFile]
build $ target context (Cc (FindCDependencies depType) stage) [src] [depFile]
deps <- parseFile depFile
-- Generated dependencies, if not yet built, will not be found and hence
-- will be referred to simply by their file names.
......@@ -266,6 +266,10 @@ needDependencies context@Context {..} src depFile = discover
need todo -- Build newly discovered generated dependencies
discover -- Continue the discovery process
-- We need to pass different flags to cc depending on whether the
-- file to compile is a .c or a .cpp file
depType = if lang == Cxx then CxxDep else CDep
parseFile :: FilePath -> Action [String]
parseFile file = do
input <- liftIO $ readFile file
......
......@@ -17,12 +17,18 @@ ccBuilderArgs = do
, arg "-c", arg =<< getInput
, arg "-o", arg =<< getOutput ]
, builder (Cc FindCDependencies) ? do
, builder (Cc (FindCDependencies CDep)) ? findCDepExpr CDep
, builder (Cc (FindCDependencies CxxDep)) ? findCDepExpr CxxDep
]
where
findCDepExpr depType = do
output <- getOutput
mconcat [ arg "-E"
, arg "-MM", arg "-MG"
, arg "-MF", arg output
, arg "-MT", arg $ dropExtension output -<.> "o"
, case depType of CDep -> mempty; CxxDep -> arg "-std=c++11"
, cIncludeArgs
, arg "-x", arg "c"
, arg =<< getInput ] ]
, arg "-x", arg (case depType of CDep -> "c"; CxxDep -> "c++")
, arg =<< getInput
]
......@@ -374,7 +374,8 @@ rtsPackageArgs = package rts ? do
, if not (null libnumaLibraryDir) then arg ("--extra-lib-dirs="++libnumaLibraryDir) else mempty
, if not (null libnumaIncludeDir) then arg ("--extra-include-dirs="++libnumaIncludeDir) else mempty
]
, builder (Cc FindCDependencies) ? cArgs
, builder (Cc (FindCDependencies CDep)) ? cArgs
, builder (Cc (FindCDependencies CxxDep)) ? cArgs
, builder (Ghc CompileCWithGhc) ? map ("-optc" ++) <$> cArgs
, builder (Ghc CompileCppWithGhc) ? map ("-optcxx" ++) <$> cArgs
, builder Ghc ? ghcArgs
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment