Skip to content

Commit

Permalink
Cabal: Take into account compilerBuildWay when computing final librar…
Browse files Browse the repository at this point in the history
…y ways

In the profiling dynamic patch I made a mistake when computing the
needed ways for a build.

When building an executable, the Haskell modules need to be built

* For the final link way
* For the build way of the compiler if TH is enabled

Before this patch, the modules were being built for all the configured
library ways, which built modules in more configurations than the
previous version of Cabal.

Fixes haskell#10418
  • Loading branch information
mpickering committed Oct 3, 2024
1 parent 96df6f8 commit cb137bf
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 3 deletions.
19 changes: 16 additions & 3 deletions Cabal/src/Distribution/Simple/GHC/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 (isHaskell, compilerBuildWay)
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Program.Builtin (ghcProgram)
import Distribution.Simple.Program.Db (requireProgram)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -111,9 +112,21 @@ 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).
Expand All @@ -127,7 +140,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
Expand Down
5 changes: 5 additions & 0 deletions cabal-testsuite/PackageTests/BuildWays/p/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Revision history for p

## 0.1.0.0 -- YYYY-mm-dd

* First version. Released on an unsuspecting world.
18 changes: 18 additions & 0 deletions cabal-testsuite/PackageTests/BuildWays/p/p.cabal
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions cabal-testsuite/PackageTests/BuildWays/p/src/MyLib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module MyLib (someFunc) where

someFunc :: IO ()
someFunc = putStrLn "someFunc"
5 changes: 5 additions & 0 deletions cabal-testsuite/PackageTests/BuildWays/q/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Revision history for q

## 0.1.0.0 -- YYYY-mm-dd

* First version. Released on an unsuspecting world.
7 changes: 7 additions & 0 deletions cabal-testsuite/PackageTests/BuildWays/q/app/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{-# LANGUAGE TemplateHaskell #-}
module Main where

import MyLib

main :: IO ()
main = someFunc
19 changes: 19 additions & 0 deletions cabal-testsuite/PackageTests/BuildWays/q/q.cabal
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions cabal-testsuite/PackageTests/BuildWays/setup.test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Test.Cabal.Prelude

opts = ["--enable-shared", "--enable-library-vanilla", "--enable-library-profiling"]

-- See #10418
main = setupTest $ recordMode DoNotRecord $ withPackageDb $ do
withDirectory "p" $ setup_install opts
withDirectory "q" $ setup_install opts
13 changes: 13 additions & 0 deletions changelog.d/i10418
Original file line number Diff line number Diff line change
@@ -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`.

}

0 comments on commit cb137bf

Please sign in to comment.