Skip to content

Commit 664e17d

Browse files
authored
For ghc calls pass Cabal's build/ directory after user's src/ directory so that contents of src/ takes precedence (#8690)
* Add test * Pass build/ directory after src/ so that whatever’s in src/ will override any files we autogenerated * Add expected output. Constrait test to run on recent enough ghc that will have Cabal updated * Force test to requre development version of Cabal Otherwise output will not be correct. It’s tricky to pass proper Cabal/ version to this test because it needs it for the custom setup. * Add changelog * Fix other tests
1 parent be4abe1 commit 664e17d

File tree

12 files changed

+156
-6
lines changed

12 files changed

+156
-6
lines changed

Cabal/src/Distribution/Simple/GHC/Internal.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,8 @@ componentGhcOptions verbosity implInfo lbi bi clbi odir =
442442
ghcOptSplitSections = toFlag (splitSections lbi),
443443
ghcOptSplitObjs = toFlag (splitObjs lbi),
444444
ghcOptSourcePathClear = toFlag True,
445-
ghcOptSourcePath = toNubListR $ [odir] ++ (map getSymbolicPath (hsSourceDirs bi))
445+
ghcOptSourcePath = toNubListR $ map getSymbolicPath (hsSourceDirs bi)
446+
++ [odir]
446447
++ [autogenComponentModulesDir lbi clbi]
447448
++ [autogenPackageModulesDir lbi],
448449
ghcOptCppIncludePath = toNubListR $ [autogenComponentModulesDir lbi clbi
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Main (main) where
2+
3+
import Lib (bar)
4+
5+
main :: IO ()
6+
main = do
7+
-- Make sure cabal sees this because this test is about which
8+
-- 'Generated' module the 'Lib' was compiled against.
9+
putStrLn "-----BEGIN CABAL OUTPUT-----"
10+
putStrLn bar
11+
putStrLn "-----END CABAL OUTPUT-----"
12+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
module Main (main) where
3+
4+
import Distribution.Simple
5+
import Distribution.Simple.LocalBuildInfo
6+
import Distribution.Simple.PreProcess
7+
import Distribution.Simple.Program
8+
import Distribution.Types.BuildInfo
9+
import Distribution.Verbosity
10+
11+
import System.Directory
12+
13+
ppHGen :: BuildInfo -> LocalBuildInfo -> ComponentLocalBuildInfo -> PreProcessor
14+
ppHGen _bi lbi _clbi = PreProcessor
15+
{ platformIndependent = True
16+
, ppOrdering = unsorted
17+
, runPreProcessor = mkSimplePreProcessor $ \inFile outFile verbosity ->
18+
copyFile inFile outFile
19+
}
20+
21+
main :: IO ()
22+
main = defaultMainWithHooks simpleUserHooks
23+
{ hookedPreProcessors = ("hgen", ppHGen) : hookedPreProcessors simpleUserHooks
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# cabal v2-run
2+
Resolving dependencies...
3+
Build profile: -w ghc-<GHCVER> -O1
4+
In order, the following will be built:
5+
- test-0.1 (exe:autogen-toggle-test) (first run)
6+
Configuring test-0.1...
7+
Preprocessing library for test-0.1..
8+
Building library for test-0.1..
9+
Preprocessing executable 'autogen-toggle-test' for test-0.1..
10+
Building executable 'autogen-toggle-test' for test-0.1..
11+
The module says: Real module, ship to production
12+
# cabal v2-run
13+
Resolving dependencies...
14+
Build profile: -w ghc-<GHCVER> -O1
15+
In order, the following will be built:
16+
- test-0.1 (exe:autogen-toggle-test) (configuration changed)
17+
Configuring test-0.1...
18+
Preprocessing library for test-0.1..
19+
Building library for test-0.1..
20+
Preprocessing executable 'autogen-toggle-test' for test-0.1..
21+
Building executable 'autogen-toggle-test' for test-0.1..
22+
The module says: Prebuilt module, don't use in production
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages: test.cabal
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Test.Cabal.Prelude
2+
3+
main :: IO ()
4+
main = cabalTest . recordMode RecordMarked $ do
5+
skipUnlessGhcVersion ">= 9.7"
6+
cabal "v2-run" ["-fgenerate", "autogen-toggle-test"]
7+
cabal "v2-run" ["-f-generate", "autogen-toggle-test"]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Generated (foo) where
2+
3+
foo :: String
4+
foo = "Prebuilt module, don't use in production"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Generated (foo) where
2+
3+
foo :: String
4+
foo = "Real module, ship to production"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Lib (bar) where
2+
3+
import Generated (foo)
4+
5+
bar :: String
6+
bar = "The module says: " ++ foo
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
cabal-version: 3.0
2+
3+
name: test
4+
version: 0.1
5+
category: Test
6+
maintainer: S
7+
synopsis: Test input
8+
description: Test input
9+
license: BSD-3-Clause
10+
11+
extra-source-files:
12+
src-inputs/**/*.hgen
13+
14+
build-type: Custom
15+
16+
custom-setup
17+
setup-depends: Cabal >= 3.9, base, directory
18+
19+
flag generate
20+
description:
21+
Generate files instead of using pregenerated ones
22+
default:
23+
False
24+
manual:
25+
True
26+
27+
library
28+
exposed-modules:
29+
Lib
30+
Generated
31+
32+
hs-source-dirs:
33+
src
34+
35+
if flag(generate)
36+
hs-source-dirs:
37+
src-inputs
38+
autogen-modules:
39+
Generated
40+
-- We don’t use any tools in this case but they’ll have to
41+
-- go here
42+
-- build-tool-depends:
43+
-- alex:alex
44+
else
45+
hs-source-dirs:
46+
gen
47+
48+
build-depends: base > 4
49+
default-language: Haskell2010
50+
51+
executable autogen-toggle-test
52+
main-is: Main.hs
53+
hs-source-dirs: .
54+
default-language: Haskell2010
55+
build-depends:
56+
, base > 4
57+
, test

0 commit comments

Comments
 (0)