Skip to content

Commit 85a76c9

Browse files
committed
Add support for build --assume-deps-up-to-date flag, partial fix to haskell#2775 (haskell#3287)
--assume-deps-up-to-date lets you compile a single component without building its dependencies. Signed-off-by: Edward Z. Yang <[email protected]>
1 parent 351c130 commit 85a76c9

File tree

8 files changed

+82
-4
lines changed

8 files changed

+82
-4
lines changed

Cabal/Cabal.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extra-source-files:
4242
tests/PackageTests/BenchmarkOptions/BenchmarkOptions.cabal
4343
tests/PackageTests/BenchmarkOptions/test-BenchmarkOptions.hs
4444
tests/PackageTests/BenchmarkStanza/my.cabal
45+
tests/PackageTests/BuildAssumeDepsUpToDate/BuildAssumeDepsUpToDate.cabal
4546
tests/PackageTests/BuildDeps/GlobalBuildDepsNotAdditive1/GlobalBuildDepsNotAdditive1.cabal
4647
tests/PackageTests/BuildDeps/GlobalBuildDepsNotAdditive1/MyLibrary.hs
4748
tests/PackageTests/BuildDeps/GlobalBuildDepsNotAdditive2/GlobalBuildDepsNotAdditive2.cabal

Cabal/Distribution/Simple/Build.hs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,36 @@ build :: PackageDescription -- ^ Mostly information from the .cabal file
7575
-> BuildFlags -- ^ Flags that the user passed to build
7676
-> [ PPSuffixHandler ] -- ^ preprocessors to run before compiling
7777
-> IO ()
78-
build pkg_descr lbi flags suffixes = do
79-
let distPref = fromFlag (buildDistPref flags)
80-
verbosity = fromFlag (buildVerbosity flags)
81-
78+
build pkg_descr lbi flags suffixes
79+
| fromFlag (buildAssumeDepsUpToDate flags) = do
80+
-- TODO: if checkBuildTargets ignores a target we may accept
81+
-- a --assume-deps-up-to-date with multiple arguments. Arguably, we should
82+
-- error early in this case.
83+
targets <- readBuildTargets pkg_descr (buildArgs flags)
84+
(cname, _) <- checkBuildTargets verbosity pkg_descr targets >>= \r -> case r of
85+
[] -> die "In --assume-deps-up-to-date mode you must specify a target"
86+
[target'] -> return target'
87+
_ -> die "In --assume-deps-up-to-date mode you can only build a single target"
88+
-- NB: do NOT 'createInternalPackageDB'; we don't want to delete it.
89+
-- But this means we have to be careful about unregistering
90+
-- ourselves.
91+
let dbPath = internalPackageDBPath lbi distPref
92+
internalPackageDB = SpecificPackageDB dbPath
93+
clbi = getComponentLocalBuildInfo lbi cname
94+
comp = getComponent pkg_descr cname
95+
-- TODO: do we need to unregister libraries? In any case, this would
96+
-- need to be done in the buildLib functionality.
97+
-- Do the build
98+
initialBuildSteps distPref pkg_descr lbi clbi verbosity
99+
let bi = componentBuildInfo comp
100+
progs' = addInternalBuildTools pkg_descr lbi bi (withPrograms lbi)
101+
lbi' = lbi {
102+
withPrograms = progs',
103+
withPackageDB = withPackageDB lbi ++ [internalPackageDB]
104+
}
105+
buildComponent verbosity (buildNumJobs flags) pkg_descr
106+
lbi' suffixes comp clbi distPref
107+
| otherwise = do
82108
targets <- readBuildTargets pkg_descr (buildArgs flags)
83109
targets' <- checkBuildTargets verbosity pkg_descr targets
84110
let componentsToBuild = componentsInBuildOrder lbi (map fst targets')
@@ -102,6 +128,9 @@ build pkg_descr lbi flags suffixes = do
102128
}
103129
buildComponent verbosity (buildNumJobs flags) pkg_descr
104130
lbi' suffixes comp clbi distPref
131+
where
132+
distPref = fromFlag (buildDistPref flags)
133+
verbosity = fromFlag (buildVerbosity flags)
105134

106135

107136
repl :: PackageDescription -- ^ Mostly information from the .cabal file

Cabal/Distribution/Simple/Setup.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,9 @@ data BuildFlags = BuildFlags {
14441444
buildDistPref :: Flag FilePath,
14451445
buildVerbosity :: Flag Verbosity,
14461446
buildNumJobs :: Flag (Maybe Int),
1447+
-- | If this is true, we don't build the dependencies of
1448+
-- 'buildArgs': only the directly referenced components.
1449+
buildAssumeDepsUpToDate :: Flag Bool,
14471450
-- TODO: this one should not be here, it's just that the silly
14481451
-- UserHooks stop us from passing extra info in other ways
14491452
buildArgs :: [String]
@@ -1461,6 +1464,7 @@ defaultBuildFlags = BuildFlags {
14611464
buildDistPref = mempty,
14621465
buildVerbosity = Flag normal,
14631466
buildNumJobs = mempty,
1467+
buildAssumeDepsUpToDate = Flag False,
14641468
buildArgs = []
14651469
}
14661470

@@ -1508,6 +1512,11 @@ buildOptions :: ProgramConfiguration -> ShowOrParseArgs
15081512
buildOptions progConf showOrParseArgs =
15091513
[ optionNumJobs
15101514
buildNumJobs (\v flags -> flags { buildNumJobs = v })
1515+
1516+
, option "" ["assume-deps-up-to-date"]
1517+
"One-shot build"
1518+
buildAssumeDepsUpToDate (\c flags -> flags { buildAssumeDepsUpToDate = c })
1519+
trueArg
15111520
]
15121521

15131522
++ programConfigurationPaths progConf showOrParseArgs
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.hs
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- Initial BuildAssumeDepsUpToDate.cabal generated by cabal init. For further
2+
-- documentation, see http://haskell.org/cabal/users-guide/
3+
4+
name: BuildAssumeDepsUpToDate
5+
version: 0.1.0.0
6+
license: BSD3
7+
author: Edward Z. Yang
8+
maintainer: [email protected]
9+
build-type: Simple
10+
cabal-version: >=1.10
11+
12+
library
13+
exposed-modules: A
14+
build-depends: base
15+
default-language: Haskell2010
16+
17+
executable myprog
18+
main-is: Main.hs
19+
hs-source-dirs: myprog
20+
build-depends: BuildAssumeDepsUpToDate, base
21+
default-language: Haskell2010
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.hs

Cabal/tests/PackageTests/Tests.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,21 @@ tests config = do
408408
cabal "build" []
409409
runExe' "T3294" [] >>= assertOutputContains "bbb"
410410

411+
-- Test build --assume-deps-up-to-date
412+
tc "BuildAssumeDepsUpToDate" $ do
413+
pkg_dir <- packageDir
414+
liftIO $ writeFile (pkg_dir </> "A.hs") "module A where\na = \"a1\""
415+
liftIO $ writeFile (pkg_dir </> "myprog/Main.hs") "import A\nmain = print (a ++ \" b1\")"
416+
cabal_build []
417+
runExe' "myprog" []
418+
>>= assertOutputContains "a1 b1"
419+
ghcFileModDelay
420+
liftIO $ writeFile (pkg_dir </> "A.hs") "module A where\na = \"a2\""
421+
liftIO $ writeFile (pkg_dir </> "myprog/Main.hs") "import A\nmain = print (a ++ \" b2\")"
422+
cabal "build" ["--assume-deps-up-to-date", "myprog"]
423+
runExe' "myprog" []
424+
>>= assertOutputContains "a1 b2"
425+
411426
where
412427
ghc_pkg_guess bin_name = do
413428
cwd <- packageDir

cabal-install/Distribution/Client/ProjectPlanning.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,7 @@ setupHsBuildFlags ElaboratedConfiguredPackage{..} _ verbosity builddir =
19141914
buildProgramArgs = mempty, --unused, set at configure time
19151915
buildVerbosity = toFlag verbosity,
19161916
buildDistPref = toFlag builddir,
1917+
buildAssumeDepsUpToDate = toFlag False,
19171918
buildNumJobs = mempty, --TODO: [nice to have] sometimes want to use toFlag (Just numBuildJobs),
19181919
buildArgs = mempty -- unused, passed via args not flags
19191920
}

0 commit comments

Comments
 (0)