Skip to content

Commit

Permalink
Fix building of executables that use TH with dyn-by-default GHC.
Browse files Browse the repository at this point in the history
Fixes haskell#1252. Makes the test suite pass with GHC HEAD.

(If 'TemplateHaskell/profiling' still fails, you're probably affected by this
GHC bug: http://ghc.haskell.org/trac/ghc/ticket/7978 . Try compiling a more
recent GHC HEAD.)
  • Loading branch information
23Skidoo committed Jul 11, 2013
1 parent 4606f9b commit c92409a
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions Cabal/Distribution/Simple/GHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,8 @@ buildLib verbosity pkg_descr lbi lib clbi = do
"-dynosuf", "dyn_o"]
}

-- TODO: the logic in this fragment is quite convoluted. Should be possible to
-- simplify/refactor.
unless (null (libModules lib)) $
do let vanilla = whenVanillaLib forceVanillaLib (runGhcProg vanillaOpts)
shared = whenSharedLib forceSharedLib (runGhcProg sharedOpts)
Expand Down Expand Up @@ -882,6 +884,7 @@ buildExe verbosity _pkg_descr lbi
-- build executables

srcMainFile <- findFile (exeDir : hsSourceDirs exeBi) modPath
isGhcDynamic <- ghcDynamic verbosity ghcProg

let isHaskellMain = elem (takeExtension srcMainFile) [".hs", ".lhs"]
cSrcs = cSources exeBi ++ [srcMainFile | not isHaskellMain]
Expand Down Expand Up @@ -913,6 +916,21 @@ buildExe verbosity _pkg_descr lbi
compileOpts | withProfExe lbi = profOpts
| withDynExe lbi = dynOpts
| otherwise = staticOpts
withStaticExe = (not $ withProfExe lbi) && (not $ withDynExe lbi)

-- For building exe's that use TH with -prof or -dynamic we actually have
-- to build twice, once without -prof/-dynamic and then again with
-- -prof/-dynamic. This is because the code that TH needs to run at
-- compile time needs to be the vanilla ABI so it can be loaded up and run
-- by the compiler.
-- With dynamic-by-default GHC the TH object files loaded at compile-time
-- need to be .dyn_o instead of .o.
doingTH = EnableExtension TemplateHaskell `elem` allExtensions exeBi
compileTHOpts | isGhcDynamic = dynOpts
| otherwise = staticOpts
compileForTH
| isGhcDynamic = doingTH && (withProfExe lbi || withStaticExe)
| otherwise = doingTH && (withProfExe lbi || withDynExe lbi)

linkOpts = compileOpts `mappend` mempty {
ghcOptLinkOptions = PD.ldOptions exeBi,
Expand All @@ -923,15 +941,10 @@ buildExe verbosity _pkg_descr lbi
ghcOptExtra = ["-no-hs-main" | not isHaskellMain ]
}

-- For building exe's for profiling that use TH we actually
-- have to build twice, once without profiling and then again
-- with profiling. This is because the code that TH needs to
-- run at compile time needs to be the vanilla ABI so it can
-- be loaded up and run by the compiler.
when ((withProfExe lbi || withDynExe lbi) &&
EnableExtension TemplateHaskell `elem` allExtensions exeBi) $
runGhcProg staticOpts { ghcOptNoLink = toFlag True }
--TODO: do we also need to play the static vs dynamic games here?
-- Build static/dynamic object files for TH, if needed.
-- TODO: use -dynamic-too when possible
when compileForTH $
runGhcProg compileTHOpts { ghcOptNoLink = toFlag True }

runGhcProg compileOpts { ghcOptNoLink = toFlag True }

Expand Down

0 comments on commit c92409a

Please sign in to comment.