diff --git a/Cabal/src/Distribution/Simple/GHC/Build.hs b/Cabal/src/Distribution/Simple/GHC/Build.hs index 1972e9d903f..c12fb7b2427 100644 --- a/Cabal/src/Distribution/Simple/GHC/Build.hs +++ b/Cabal/src/Distribution/Simple/GHC/Build.hs @@ -12,7 +12,7 @@ import Distribution.Simple.Flag (Flag) import Distribution.Simple.GHC.Build.ExtraSources import Distribution.Simple.GHC.Build.Link import Distribution.Simple.GHC.Build.Modules -import Distribution.Simple.GHC.Build.Utils (isHaskell) +import Distribution.Simple.GHC.Build.Utils (compilerBuildWay, isHaskell) import Distribution.Simple.LocalBuildInfo import Distribution.Simple.Program.Builtin (ghcProgram) import Distribution.Simple.Program.Db (requireProgram) @@ -73,6 +73,7 @@ build numJobs pkg_descr pbci = do verbosity = buildVerbosity pbci isLib = buildIsLib pbci lbi = localBuildInfo pbci + bi = buildBI pbci clbi = buildCLBI pbci isIndef = componentIsIndefinite clbi mbWorkDir = mbWorkDirLBI lbi @@ -111,9 +112,22 @@ build numJobs pkg_descr pbci = do (ghcProg, _) <- liftIO $ requireProgram verbosity ghcProgram (withPrograms lbi) + -- Ways which are wanted from configuration flags let wantedWays@(wantedLibWays, _, wantedExeWay) = buildWays lbi - liftIO $ info verbosity ("Wanted build ways(" ++ show isLib ++ "): " ++ show (if isLib then wantedLibWays isIndef else [wantedExeWay])) + -- Ways which are needed due to the compiler configuration + let doingTH = usesTemplateHaskellOrQQ bi + defaultGhcWay = compilerBuildWay (buildCompiler pbci) + wantedLibBuildWays = + if isLib + then wantedLibWays isIndef + else [wantedExeWay] + finalLibBuildWays = + wantedLibBuildWays + ++ [defaultGhcWay | doingTH && defaultGhcWay `notElem` wantedLibBuildWays] + + liftIO $ info verbosity ("Wanted build ways(" ++ show isLib ++ "): " ++ show wantedLibBuildWays) + liftIO $ info verbosity ("Final lib build ways(" ++ show isLib ++ "): " ++ show finalLibBuildWays) -- We need a separate build and link phase, and C sources must be compiled -- after Haskell modules, because C sources may depend on stub headers -- generated from compiling Haskell modules (#842, #3294). @@ -127,7 +141,7 @@ build numJobs pkg_descr pbci = do | otherwise -> (Nothing, Just mainFile) Nothing -> (Nothing, Nothing) - buildOpts <- buildHaskellModules numJobs ghcProg hsMainFile inputModules buildTargetDir (wantedLibWays isIndef) pbci + buildOpts <- buildHaskellModules numJobs ghcProg hsMainFile inputModules buildTargetDir finalLibBuildWays pbci extraSources <- buildAllExtraSources nonHsMainFile ghcProg buildTargetDir wantedWays pbci linkOrLoadComponent ghcProg diff --git a/cabal-testsuite/PackageTests/BuildWays/p/CHANGELOG.md b/cabal-testsuite/PackageTests/BuildWays/p/CHANGELOG.md new file mode 100644 index 00000000000..9ede8b27d4f --- /dev/null +++ b/cabal-testsuite/PackageTests/BuildWays/p/CHANGELOG.md @@ -0,0 +1,5 @@ +# Revision history for p + +## 0.1.0.0 -- YYYY-mm-dd + +* First version. Released on an unsuspecting world. diff --git a/cabal-testsuite/PackageTests/BuildWays/p/p.cabal b/cabal-testsuite/PackageTests/BuildWays/p/p.cabal new file mode 100644 index 00000000000..687cf16bc0c --- /dev/null +++ b/cabal-testsuite/PackageTests/BuildWays/p/p.cabal @@ -0,0 +1,18 @@ +cabal-version: 3.12 +name: p +version: 0.1.0.0 +license: NONE +author: Matthew Pickering +maintainer: matthewtpickering@gmail.com +build-type: Simple +extra-doc-files: CHANGELOG.md + +common warnings + ghc-options: -Wall + +library + import: warnings + exposed-modules: MyLib + build-depends: base + hs-source-dirs: src + default-language: Haskell2010 diff --git a/cabal-testsuite/PackageTests/BuildWays/p/src/MyLib.hs b/cabal-testsuite/PackageTests/BuildWays/p/src/MyLib.hs new file mode 100644 index 00000000000..e657c4403f6 --- /dev/null +++ b/cabal-testsuite/PackageTests/BuildWays/p/src/MyLib.hs @@ -0,0 +1,4 @@ +module MyLib (someFunc) where + +someFunc :: IO () +someFunc = putStrLn "someFunc" diff --git a/cabal-testsuite/PackageTests/BuildWays/q/CHANGELOG.md b/cabal-testsuite/PackageTests/BuildWays/q/CHANGELOG.md new file mode 100644 index 00000000000..62632c53766 --- /dev/null +++ b/cabal-testsuite/PackageTests/BuildWays/q/CHANGELOG.md @@ -0,0 +1,5 @@ +# Revision history for q + +## 0.1.0.0 -- YYYY-mm-dd + +* First version. Released on an unsuspecting world. diff --git a/cabal-testsuite/PackageTests/BuildWays/q/app/Main.hs b/cabal-testsuite/PackageTests/BuildWays/q/app/Main.hs new file mode 100644 index 00000000000..642b418a547 --- /dev/null +++ b/cabal-testsuite/PackageTests/BuildWays/q/app/Main.hs @@ -0,0 +1,7 @@ +{-# LANGUAGE TemplateHaskell #-} +module Main where + +import MyLib + +main :: IO () +main = someFunc diff --git a/cabal-testsuite/PackageTests/BuildWays/q/q.cabal b/cabal-testsuite/PackageTests/BuildWays/q/q.cabal new file mode 100644 index 00000000000..a4f2a1d65d7 --- /dev/null +++ b/cabal-testsuite/PackageTests/BuildWays/q/q.cabal @@ -0,0 +1,19 @@ +cabal-version: 3.12 +name: q +version: 0.1.0.0 +license: NONE +author: Matthew Pickering +maintainer: matthewtpickering@gmail.com +build-type: Simple +extra-doc-files: CHANGELOG.md + +common warnings + ghc-options: -Wall + +executable q + import: warnings + main-is: Main.hs + build-depends: p, base + hs-source-dirs: app + ghc-options: -dynamic-too + default-language: Haskell2010 diff --git a/cabal-testsuite/PackageTests/BuildWays/setup.test.hs b/cabal-testsuite/PackageTests/BuildWays/setup.test.hs new file mode 100644 index 00000000000..dd4531599e4 --- /dev/null +++ b/cabal-testsuite/PackageTests/BuildWays/setup.test.hs @@ -0,0 +1,10 @@ +import Test.Cabal.Prelude + +opts = ["--enable-shared", "--enable-library-vanilla", "--enable-library-profiling"] + +-- See #10418 +main = setupTest $ recordMode DoNotRecord $ withPackageDb $ do + skipIfNoSharedLibraries + skipIfNoProfiledLibraries + withDirectory "p" $ setup_install opts + withDirectory "q" $ setup_install opts diff --git a/changelog.d/i10418 b/changelog.d/i10418 new file mode 100644 index 00000000000..9a96e47a1e9 --- /dev/null +++ b/changelog.d/i10418 @@ -0,0 +1,13 @@ +synopsis: Fix build ways for modules in executables +packages: Cabal +prs: #10419 +issues: #10418 +significance: significant + +description: { + +- Modules belonging to executables were being built in too many ways. For instance, if you +had configured to build profiled library files then your executable modules would also +be built profiled. Which was a regression in behaviour since `Cabal-3.12`. + +}