Commit fb579e15 authored by Ben Gamari's avatar Ben Gamari 🐢 Committed by Marge Bot
Browse files

driver: Introduce pgmcxx

Here we introduce proper support for compilation of C++ objects. This
includes:

 * logic in `configure` to detect the C++ toolchain and propagating this
   information into the `settings` file
 * logic in the driver to use the C++ toolchain when compiling C++
   sources
parent 8dfea078
......@@ -92,6 +92,7 @@ module GHC.Driver.Session (
sPgm_P,
sPgm_F,
sPgm_c,
sPgm_cxx,
sPgm_a,
sPgm_l,
sPgm_lm,
......@@ -130,7 +131,7 @@ module GHC.Driver.Session (
ghcUsagePath, ghciUsagePath, topDir,
versionedAppDir, versionedFilePath,
extraGccViaCFlags, globalPackageDatabasePath,
pgm_L, pgm_P, pgm_F, pgm_c, pgm_a, pgm_l, pgm_lm, pgm_dll, pgm_T,
pgm_L, pgm_P, pgm_F, pgm_c, pgm_cxx, pgm_a, pgm_l, pgm_lm, pgm_dll, pgm_T,
pgm_windres, pgm_ar, pgm_otool, pgm_install_name_tool,
pgm_ranlib, pgm_lo, pgm_lc, pgm_lcc, pgm_i,
opt_L, opt_P, opt_F, opt_c, opt_cxx, opt_a, opt_l, opt_lm, opt_i,
......@@ -823,6 +824,8 @@ pgm_F :: DynFlags -> String
pgm_F dflags = toolSettings_pgm_F $ toolSettings dflags
pgm_c :: DynFlags -> String
pgm_c dflags = toolSettings_pgm_c $ toolSettings dflags
pgm_cxx :: DynFlags -> String
pgm_cxx dflags = toolSettings_pgm_cxx $ toolSettings dflags
pgm_a :: DynFlags -> (String,[Option])
pgm_a dflags = toolSettings_pgm_a $ toolSettings dflags
pgm_l :: DynFlags -> (String,[Option])
......@@ -2163,6 +2166,8 @@ dynamic_flags_deps = [
$ hasArg $ \f -> alterToolSettings $ \s -> s { toolSettings_pgm_F = f }
, make_ord_flag defFlag "pgmc"
$ hasArg $ \f -> alterToolSettings $ \s -> s { toolSettings_pgm_c = f }
, make_ord_flag defFlag "pgmcxx"
$ hasArg $ \f -> alterToolSettings $ \s -> s { toolSettings_pgm_cxx = f }
, (Deprecated, defFlag "pgmc-supports-no-pie"
$ noArgM $ \d -> do
deprecate $ "use -pgml-supports-no-pie instead"
......
......@@ -28,6 +28,7 @@ module GHC.Settings
, sPgm_P
, sPgm_F
, sPgm_c
, sPgm_cxx
, sPgm_a
, sPgm_l
, sPgm_lm
......@@ -98,6 +99,7 @@ data ToolSettings = ToolSettings
, toolSettings_pgm_P :: (String, [Option])
, toolSettings_pgm_F :: String
, toolSettings_pgm_c :: String
, toolSettings_pgm_cxx :: String
, toolSettings_pgm_a :: (String, [Option])
, toolSettings_pgm_l :: (String, [Option])
, toolSettings_pgm_lm :: Maybe (String, [Option])
......@@ -208,6 +210,8 @@ sPgm_F :: Settings -> String
sPgm_F = toolSettings_pgm_F . sToolSettings
sPgm_c :: Settings -> String
sPgm_c = toolSettings_pgm_c . sToolSettings
sPgm_cxx :: Settings -> String
sPgm_cxx = toolSettings_pgm_cxx . sToolSettings
sPgm_a :: Settings -> (String, [Option])
sPgm_a = toolSettings_pgm_a . sToolSettings
sPgm_l :: Settings -> (String, [Option])
......
......@@ -78,6 +78,7 @@ initSettings top_dir = do
targetPlatformString <- getSetting "target platform string"
myExtraGccViaCFlags <- getSetting "GCC extra via C opts"
cc_prog <- getToolSetting "C compiler command"
cxx_prog <- getToolSetting "C++ compiler command"
cc_args_str <- getSetting "C compiler flags"
cxx_args_str <- getSetting "C++ compiler flags"
gccSupportsNoPie <- getBooleanSetting "C compiler supports -no-pie"
......@@ -173,6 +174,7 @@ initSettings top_dir = do
, toolSettings_pgm_P = (cpp_prog, cpp_args)
, toolSettings_pgm_F = ""
, toolSettings_pgm_c = cc_prog
, toolSettings_pgm_cxx = cxx_prog
, toolSettings_pgm_a = (as_prog, as_args)
, toolSettings_pgm_l = (ld_prog, ld_args)
, toolSettings_pgm_lm = ld_r
......
......@@ -81,13 +81,13 @@ runPp logger dflags args = traceToolCommand logger "pp" $ do
-- | Run compiler of C-like languages and raw objects (such as gcc or clang).
runCc :: Maybe ForeignSrcLang -> Logger -> TmpFs -> DynFlags -> [Option] -> IO ()
runCc mLanguage logger tmpfs dflags args = traceToolCommand logger "cc" $ do
let p = pgm_c dflags
args1 = map Option userOpts
let args1 = map Option userOpts
args2 = languageOptions ++ args ++ args1
-- We take care to pass -optc flags in args1 last to ensure that the
-- user can override flags passed by GHC. See #14452.
mb_env <- getGccEnv args2
runSomethingResponseFile logger tmpfs dflags cc_filter "C Compiler" p args2 mb_env
runSomethingResponseFile logger tmpfs dflags cc_filter dbgstring prog args2
mb_env
where
-- discard some harmless warnings from gcc that we can't turn off
cc_filter = unlines . doFilter . lines
......@@ -143,17 +143,23 @@ runCc mLanguage logger tmpfs dflags args = traceToolCommand logger "cc" $ do
-- compiling .hc files, by adding the -x c option.
-- Also useful for plain .c files, just in case GHC saw a
-- -x c option.
(languageOptions, userOpts) = case mLanguage of
Nothing -> ([], userOpts_c)
Just language -> ([Option "-x", Option languageName], opts)
(languageOptions, userOpts, prog, dbgstring) = case mLanguage of
Nothing -> ([], userOpts_c, pgm_c dflags, "C Compiler")
Just language -> ([Option "-x", Option languageName], opts, prog, dbgstr)
where
(languageName, opts) = case language of
LangC -> ("c", userOpts_c)
LangCxx -> ("c++", userOpts_cxx)
LangObjc -> ("objective-c", userOpts_c)
LangObjcxx -> ("objective-c++", userOpts_cxx)
LangAsm -> ("assembler", [])
RawObject -> ("c", []) -- claim C for lack of a better idea
(languageName, opts, prog, dbgstr) = case language of
LangC -> ("c", userOpts_c
,pgm_c dflags, "C Compiler")
LangCxx -> ("c++", userOpts_cxx
,pgm_cxx dflags , "C++ Compiler")
LangObjc -> ("objective-c", userOpts_c
,pgm_c dflags , "Objective C Compiler")
LangObjcxx -> ("objective-c++", userOpts_cxx
,pgm_cxx dflags, "Objective C++ Compiler")
LangAsm -> ("assembler", []
,pgm_c dflags, "Asm Compiler")
RawObject -> ("c", []
,pgm_c dflags, "C Compiler") -- claim C for lack of a better idea
userOpts_c = getOpts dflags opt_c
userOpts_cxx = getOpts dflags opt_cxx
......
......@@ -354,6 +354,7 @@ else
# are screwed up. Configure doesn't think they're ever equal and
# so never tried without the prefix.
AC_PATH_PROG([CC],[gcc], [clang])
AC_PATH_PROG([CXX],[g++], [clang++])
AC_PATH_PROG([NM],[nm])
AC_PATH_PROG([LD],[ld])
AC_PATH_PROG([AR],[ar])
......@@ -472,6 +473,7 @@ AC_ARG_WITH([clang],
dnl detect compiler (prefer gcc over clang) and set $CC (unless CC already set),
dnl later CC is copied to CC_STAGE{1,2,3}
AC_PROG_CC([cc gcc clang])
AC_PROG_CXX([g++ clang++ c++])
MAYBE_OVERRIDE_STAGE0([ar],[AR_STAGE0])
......
......@@ -92,6 +92,7 @@ AC_ARG_ENABLE(distro-toolchain,
dnl ** Which gcc to use?
dnl --------------------------------------------------------------
AC_PROG_CC([gcc clang])
AC_PROG_CXX([g++ clang++ c++])
dnl ** figure out how to invoke the C preprocessor (i.e. `gcc -E`)
AC_PROG_CPP
......
......@@ -38,6 +38,13 @@ given compilation phase:
Use ⟨cmd⟩ as the C compiler.
.. ghc-flag:: -pgmcxx ⟨cmd⟩
:shortdesc: Use ⟨cmd⟩ as the C++ compiler
:type: dynamic
:category: phase-programs
Use ⟨cmd⟩ as the C++ compiler.
.. ghc-flag:: -pgmlo ⟨cmd⟩
:shortdesc: Use ⟨cmd⟩ as the LLVM optimiser
:type: dynamic
......
......@@ -105,6 +105,7 @@ lib/settings :
@echo '[("GCC extra via C opts", "$(GccExtraViaCOpts)")' >> $@
@echo ',("C compiler command", "$(SettingsCCompilerCommand)")' >> $@
@echo ',("C compiler flags", "$(SettingsCCompilerFlags)")' >> $@
@echo ',("C++ compiler command", "$(SettingsCxxCompilerCommand)")' >> $@
@echo ',("C++ compiler flags", "$(SettingsCxxCompilerFlags)")' >> $@
@echo ',("C compiler link flags", "$(SettingsCCompilerLinkFlags)")' >> $@
@echo ',("C compiler supports -no-pie", "$(SettingsCCompilerSupportsNoPie)")' >> $@
......
......@@ -142,6 +142,7 @@ ld-is-gnu-ld = @LdIsGNULd@
ar-args = @ArArgs@
settings-c-compiler-command = @SettingsCCompilerCommand@
settings-cxx-compiler-command = @SettingsCxxCompilerCommand@
settings-haskell-cpp-command = @SettingsHaskellCPPCommand@
settings-haskell-cpp-flags = @SettingsHaskellCPPFlags@
settings-c-compiler-flags = @SettingsCCompilerFlags@
......
......@@ -369,6 +369,7 @@ the right names for them:
argument for `hadrian/ghci` to work
* `cc` refers to C compiler commands
* `cxx` refers to C++ compiler commands
* `c.opts` for commands that call the C compiler on some C files
* `deps.opts` for commands that call the C compiler for figuring out
......
......@@ -106,6 +106,7 @@ data SettingList = ConfCcArgs Stage
-- Eventually much of that local can probably be computed just in Hadrian.
data SettingsFileSetting
= SettingsFileSetting_CCompilerCommand
| SettingsFileSetting_CxxCompilerCommand
| SettingsFileSetting_HaskellCPPCommand
| SettingsFileSetting_HaskellCPPFlags
| SettingsFileSetting_CCompilerFlags
......@@ -198,6 +199,7 @@ settingList key = fmap words $ lookupSystemConfig $ case key of
settingsFileSetting :: SettingsFileSetting -> Action String
settingsFileSetting key = lookupSystemConfig $ case key of
SettingsFileSetting_CCompilerCommand -> "settings-c-compiler-command"
SettingsFileSetting_CxxCompilerCommand -> "settings-cxx-compiler-command"
SettingsFileSetting_HaskellCPPCommand -> "settings-haskell-cpp-command"
SettingsFileSetting_HaskellCPPFlags -> "settings-haskell-cpp-flags"
SettingsFileSetting_CCompilerFlags -> "settings-c-compiler-flags"
......
......@@ -298,6 +298,7 @@ generateSettings = do
[ ("GCC extra via C opts", expr $ lookupSystemConfig "gcc-extra-via-c-opts")
, ("C compiler command", expr $ settingsFileSetting SettingsFileSetting_CCompilerCommand)
, ("C compiler flags", expr $ settingsFileSetting SettingsFileSetting_CCompilerFlags)
, ("C++ compiler command", expr $ settingsFileSetting SettingsFileSetting_CxxCompilerCommand)
, ("C++ compiler flags", expr $ settingsFileSetting SettingsFileSetting_CxxCompilerFlags)
, ("C compiler link flags", expr $ settingsFileSetting SettingsFileSetting_CCompilerLinkFlags)
, ("C compiler supports -no-pie", expr $ settingsFileSetting SettingsFileSetting_CCompilerSupportsNoPie)
......
......@@ -11,6 +11,7 @@ AC_DEFUN([FP_SETTINGS],
mingw_bin_prefix='$$tooldir/mingw/bin/'
SettingsCCompilerCommand="${mingw_bin_prefix}clang.exe"
SettingsCCompilerFlags="$CONF_CC_OPTS_STAGE2"
SettingsCxxCompilerCommand="${mingw_bin_prefix}clang++.exe"
SettingsCxxCompilerFlags="$CONF_CXX_OPTS_STAGE2"
SettingsCCompilerLinkFlags="$CONF_GCC_LINKER_OPTS_STAGE2"
SettingsHaskellCPPCommand="${mingw_bin_prefix}clang.exe"
......@@ -32,6 +33,7 @@ AC_DEFUN([FP_SETTINGS],
SettingsCCompilerCommand="$CC"
SettingsCCompilerFlags="$CONF_CC_OPTS_STAGE2"
SettingsCxxCompilerCommand="$CXX"
SettingsCxxCompilerFlags="$CONF_CXX_OPTS_STAGE2"
SettingsHaskellCPPCommand="$HaskellCPPCmd"
SettingsHaskellCPPFlags="$HaskellCPPArgs"
......@@ -104,6 +106,7 @@ AC_DEFUN([FP_SETTINGS],
SettingsCCompilerSupportsNoPie="$CONF_GCC_SUPPORTS_NO_PIE"
AC_SUBST(SettingsCCompilerCommand)
AC_SUBST(SettingsCxxCompilerCommand)
AC_SUBST(SettingsHaskellCPPCommand)
AC_SUBST(SettingsHaskellCPPFlags)
AC_SUBST(SettingsCCompilerFlags)
......
......@@ -80,6 +80,7 @@ AC_DEFUN([FP_SETUP_WINDOWS_TOOLCHAIN],[
mingwbin="$hardtop/inplace/mingw/bin/"
CC="${mingwbin}clang.exe"
CXX="${mingwbin}clang++.exe"
cflags="--rtlib=compiler-rt"
CFLAGS="$cflags"
CONF_CC_OPTS_STAGE1="$cflags"
......
......@@ -358,7 +358,7 @@ BIN_DIST_TAR_COMP = $(BIN_DIST_NAME)-$(TARGETPLATFORM).tar.$(TAR_COMP_EXT)
# SRC_HC_OPTS is *not* for adding flags that are required to make your
# build work. Examples:
#
# - instead of using -pgmc/-pgma-/-pgml, use the --with-gcc option to configure
# - instead of using -pgmc/-pgmcxx/-pgma-/-pgml, use the --with-gcc option to configure
#
# - if you need -optc, -opta, or -optl flags, the CONF_CC_* and CONF_LD_*
# variables are more appropriate (set via configure)
......@@ -457,6 +457,7 @@ TargetHasLibm = @TargetHasLibm@
TablesNextToCode = @TablesNextToCode@
SettingsCCompilerCommand = @SettingsCCompilerCommand@
SettingsCxxCompilerCommand = @SettingsCxxCompilerCommand@
SettingsHaskellCPPCommand = @SettingsHaskellCPPCommand@
SettingsHaskellCPPFlags = @SettingsHaskellCPPFlags@
SettingsCCompilerFlags = @SettingsCCompilerFlags@
......
......@@ -193,6 +193,7 @@ $(includes_SETTINGS) : rts/include/Makefile | $$(dir $$@)/.
@echo '[("GCC extra via C opts", "$(GccExtraViaCOpts)")' >> $@
@echo ',("C compiler command", "$(SettingsCCompilerCommand)")' >> $@
@echo ',("C compiler flags", "$(SettingsCCompilerFlags)")' >> $@
@echo ',("C++ compiler command", "$(SettingsCxxCompilerCommand)")' >> $@
@echo ',("C++ compiler flags", "$(SettingsCxxCompilerFlags)")' >> $@
@echo ',("C compiler link flags", "$(SettingsCCompilerLinkFlags)")' >> $@
@echo ',("C compiler supports -no-pie", "$(SettingsCCompilerSupportsNoPie)")' >> $@
......
......@@ -35,6 +35,7 @@ main = do
getGhcFieldProgWithDefault fields "LLC" "LLVM llc command" "llc"
getGhcFieldProgWithDefault fields "TEST_CC" "C compiler command" "gcc"
getGhcFieldProgWithDefault fields "TEST_CC_OPTS" "C compiler flags" ""
getGhcFieldProgWithDefault fields "TEST_CXX" "C++ compiler command" "g++"
getGhcFieldOrFail :: [(String,String)] -> String -> String -> IO ()
getGhcFieldOrFail fields mkvar key
......
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