Commit d54b4374 authored by Mikhail Glushenkov's avatar Mikhail Glushenkov
Browse files

Merge pull request #1386 from 23Skidoo/ghc-head-fixes

Fix building of executables that use TH with dyn-by-default GHC. 
parents e70bbe03 c854a53b
......@@ -91,6 +91,10 @@ extra-source-files:
tests/PackageTests/PreProcess/Foo.hsc
tests/PackageTests/PreProcess/Main.hs
tests/PackageTests/PreProcess/my.cabal
tests/PackageTests/TemplateHaskell/vanilla/Exe.hs
tests/PackageTests/TemplateHaskell/vanilla/Lib.hs
tests/PackageTests/TemplateHaskell/vanilla/TH.hs
tests/PackageTests/TemplateHaskell/vanilla/my.cabal
tests/PackageTests/TemplateHaskell/dynamic/Exe.hs
tests/PackageTests/TemplateHaskell/dynamic/Lib.hs
tests/PackageTests/TemplateHaskell/dynamic/TH.hs
......
......@@ -713,10 +713,11 @@ buildLib verbosity pkg_descr lbi lib clbi = do
unless (null (libModules lib)) $
do let vanilla = whenVanillaLib forceVanillaLib (runGhcProg vanillaOpts)
shared = whenSharedLib forceSharedLib (runGhcProg sharedOpts)
if dynamicTooSupported &&
(forceVanillaLib || withVanillaLib lbi) &&
(forceSharedLib || withSharedLib lbi) &&
null (ghcSharedOptions libBi)
useDynToo = dynamicTooSupported &&
(forceVanillaLib || withVanillaLib lbi) &&
(forceSharedLib || withSharedLib lbi) &&
null (ghcSharedOptions libBi)
if useDynToo
then runGhcProg vanillaSharedOpts
else if isGhcDynamic then do shared; vanilla
else do vanilla; shared
......@@ -881,7 +882,9 @@ buildExe verbosity _pkg_descr lbi
-- build executables
srcMainFile <- findFile (exeDir : hsSourceDirs exeBi) modPath
srcMainFile <- findFile (exeDir : hsSourceDirs exeBi) modPath
isGhcDynamic <- ghcDynamic verbosity ghcProg
dynamicTooSupported <- ghcSupportsDynamicToo verbosity ghcProg
let isHaskellMain = elem (takeExtension srcMainFile) [".hs", ".lhs"]
cSrcs = cSources exeBi ++ [srcMainFile | not isHaskellMain]
......@@ -909,12 +912,37 @@ buildExe verbosity _pkg_descr lbi
ghcOptObjSuffix = toFlag "dyn_o",
ghcOptExtra = ghcSharedOptions exeBi
}
compileOpts | withProfExe lbi = profOpts
dynTooOpts = staticOpts `mappend` mempty {
ghcOptExtra = ["-dynamic-too",
"-dynhisuf", "dyn_hi",
"-dynosuf", "dyn_o"]
}
commonOpts | withProfExe lbi = profOpts
| withDynExe lbi = dynOpts
| otherwise = staticOpts
linkOpts = compileOpts `mappend` mempty {
compileOpts | useDynToo = dynTooOpts
| otherwise = commonOpts
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
-- Should we use -dynamic-too instead of compilng twice?
useDynToo = dynamicTooSupported && isGhcDynamic
&& doingTH && withStaticExe && null (ghcSharedOptions exeBi)
compileTHOpts | isGhcDynamic = dynOpts
| otherwise = staticOpts
compileForTH
| useDynToo = False
| isGhcDynamic = doingTH && (withProfExe lbi || withStaticExe)
| otherwise = doingTH && (withProfExe lbi || withDynExe lbi)
linkOpts = commonOpts `mappend` mempty {
ghcOptLinkOptions = PD.ldOptions exeBi,
ghcOptLinkLibs = extraLibs exeBi,
ghcOptLinkLibPath = extraLibDirs exeBi,
......@@ -923,15 +951,9 @@ 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.
when compileForTH $
runGhcProg compileTHOpts { ghcOptNoLink = toFlag True }
runGhcProg compileOpts { ghcOptNoLink = toFlag True }
......
......@@ -71,6 +71,8 @@ tests version inplaceSpec =
, hunit "BenchmarkExeV10/Test"
PackageTests.BenchmarkExeV10.Check.checkBenchmark
, hunit "BenchmarkOptions" PackageTests.BenchmarkOptions.Check.suite
, hunit "TemplateHaskell/vanilla"
PackageTests.TemplateHaskell.Check.vanilla
, hunit "TemplateHaskell/profiling"
PackageTests.TemplateHaskell.Check.profiling
, hunit "TemplateHaskell/dynamic"
......
......@@ -4,6 +4,13 @@ import PackageTests.PackageTester
import System.FilePath
import Test.HUnit
vanilla :: Test
vanilla = TestCase $ do
let spec = PackageSpec ("PackageTests" </>
"TemplateHaskell" </> "vanilla") []
result <- cabal_build spec
assertBuildSucceeded result
profiling :: Test
profiling = TestCase $ do
let flags = ["--enable-library-profiling"
......
......@@ -3,4 +3,4 @@ module Main where
import TH
main = print $(splice)
\ No newline at end of file
main = print $(splice)
......@@ -3,4 +3,4 @@ module Main where
import TH
main = print $(splice)
\ No newline at end of file
main = print $(splice)
{-# LANGUAGE TemplateHaskell #-}
module Main where
import TH
main = print $(splice)
{-# LANGUAGE TemplateHaskell #-}
module Lib where
import TH
val = $(splice)
{-# LANGUAGE TemplateHaskell #-}
module TH where
splice = [| () |]
Name: templateHaskell
Version: 0.1
Build-Type: Simple
Cabal-Version: >= 1.2
Library
Exposed-Modules: Lib
Other-Modules: TH
Build-Depends: base, template-haskell
Extensions: TemplateHaskell
Executable main
Main-is: Exe.hs
Build-Depends: base, template-haskell
Extensions: TemplateHaskell
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