Skip to content

Commit e222728

Browse files
hsyl20hvr
authored andcommitted
Add C-- support bits for cmm-sources feature
1 parent b9fab08 commit e222728

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

Cabal/Distribution/Simple/GHC.hs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -732,9 +732,36 @@ buildOrReplLib mReplFlags verbosity numJobs pkg_descr lbi lib clbi = do
732732
unless forRepl $ whenProfLib (runGhcProgIfNeeded profAsmOpts)
733733
| filename <- asmSources libBi]
734734

735-
-- TODO -- TODO -- TODO -- TODO -- TODO -- TODO
736-
-- TODO: Add missing support for CMM files
737-
--
735+
-- build any Cmm sources
736+
unless (not has_code || null (cmmSources libBi)) $ do
737+
info verbosity "Building C-- Sources..."
738+
sequence_
739+
[ do let baseCmmOpts = Internal.componentCmmGhcOptions verbosity implInfo
740+
lbi libBi clbi libTargetDir filename
741+
vanillaCmmOpts = if isGhcDynamic
742+
-- Dynamic GHC requires C sources to be built
743+
-- with -fPIC for REPL to work. See #2207.
744+
then baseCmmOpts { ghcOptFPic = toFlag True }
745+
else baseCmmOpts
746+
profCmmOpts = vanillaCmmOpts `mappend` mempty {
747+
ghcOptProfilingMode = toFlag True,
748+
ghcOptObjSuffix = toFlag "p_o"
749+
}
750+
sharedCmmOpts = vanillaCmmOpts `mappend` mempty {
751+
ghcOptFPic = toFlag True,
752+
ghcOptDynLinkMode = toFlag GhcDynamicOnly,
753+
ghcOptObjSuffix = toFlag "dyn_o"
754+
}
755+
odir = fromFlag (ghcOptObjDir vanillaCmmOpts)
756+
createDirectoryIfMissingVerbose verbosity True odir
757+
let runGhcProgIfNeeded cmmOpts = do
758+
needsRecomp <- checkNeedsRecompilation filename cmmOpts
759+
when needsRecomp $ runGhcProg cmmOpts
760+
runGhcProgIfNeeded vanillaCmmOpts
761+
unless forRepl $
762+
whenSharedLib forceSharedLib (runGhcProgIfNeeded sharedCmmOpts)
763+
unless forRepl $ whenProfLib (runGhcProgIfNeeded profCmmOpts)
764+
| filename <- cmmSources libBi]
738765

739766
-- TODO: problem here is we need the .c files built first, so we can load them
740767
-- with ghci, but .c files can depend on .h files generated by ghc by ffi

Cabal/Distribution/Simple/GHC/Internal.hs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Distribution.Simple.GHC.Internal (
1919
targetPlatform,
2020
getGhcInfo,
2121
componentCcGhcOptions,
22+
componentCmmGhcOptions,
2223
componentCxxGhcOptions,
2324
componentAsmGhcOptions,
2425
componentGhcOptions,
@@ -433,15 +434,50 @@ componentGhcOptions verbosity implInfo lbi bi clbi odir =
433434
ghcOptExtensionMap = Map.fromList . compilerExtensions $ (compiler lbi)
434435
}
435436
where
436-
toGhcOptimisation NoOptimisation = mempty --TODO perhaps override?
437-
toGhcOptimisation NormalOptimisation = toFlag GhcNormalOptimisation
438-
toGhcOptimisation MaximumOptimisation = toFlag GhcMaximumOptimisation
439-
440437
exe_paths = [ componentBuildDir lbi (targetCLBI exe_tgt)
441438
| uid <- componentExeDeps clbi
442439
-- TODO: Ugh, localPkgDescr
443440
, Just exe_tgt <- [unitIdTarget' (localPkgDescr lbi) lbi uid] ]
444441

442+
toGhcOptimisation :: OptimisationLevel -> Flag GhcOptimisation
443+
toGhcOptimisation NoOptimisation = mempty --TODO perhaps override?
444+
toGhcOptimisation NormalOptimisation = toFlag GhcNormalOptimisation
445+
toGhcOptimisation MaximumOptimisation = toFlag GhcMaximumOptimisation
446+
447+
448+
componentCmmGhcOptions :: Verbosity -> GhcImplInfo -> LocalBuildInfo
449+
-> BuildInfo -> ComponentLocalBuildInfo
450+
-> FilePath -> FilePath
451+
-> GhcOptions
452+
componentCmmGhcOptions verbosity _implInfo lbi bi clbi odir filename =
453+
mempty {
454+
-- Respect -v0, but don't crank up verbosity on GHC if
455+
-- Cabal verbosity is requested. For that, use --ghc-option=-v instead!
456+
ghcOptVerbosity = toFlag (min verbosity normal),
457+
ghcOptMode = toFlag GhcModeCompile,
458+
ghcOptInputFiles = toNubListR [filename],
459+
460+
ghcOptCppIncludePath = toNubListR $ [autogenComponentModulesDir lbi clbi
461+
,autogenPackageModulesDir lbi
462+
,odir]
463+
-- includes relative to the package
464+
++ PD.includeDirs bi
465+
-- potential includes generated by `configure'
466+
-- in the build directory
467+
++ [buildDir lbi </> dir | dir <- PD.includeDirs bi],
468+
ghcOptCppOptions = cppOptions bi,
469+
ghcOptCppIncludes = toNubListR $
470+
[autogenComponentModulesDir lbi clbi </> cppHeaderName],
471+
ghcOptHideAllPackages= toFlag True,
472+
ghcOptPackageDBs = withPackageDB lbi,
473+
ghcOptPackages = toNubListR $ mkGhcOptPackages clbi,
474+
ghcOptOptimisation = toGhcOptimisation (withOptimization lbi),
475+
ghcOptDebugInfo = toFlag (withDebugInfo lbi),
476+
ghcOptExtra = cmmOptions bi,
477+
ghcOptObjDir = toFlag odir
478+
}
479+
480+
445481
-- | Strip out flags that are not supported in ghci
446482
filterGhciFlags :: [String] -> [String]
447483
filterGhciFlags = filter supported

0 commit comments

Comments
 (0)