Skip to content

Commit 5a544cb

Browse files
alt-romessheaf
authored andcommitted
hooks: Implicitly depend on hooks-exe
The `hooks-exe` package enables `SetupHooks` values to be converted into a `Setup.hs` executable which can be executed independently of Cabal. The `Setup.hs` executable wrapping `SetupHooks` is quite important to preserve the interface used by other tools when packages migrate to `Hooks` from `Custom`. Even though `hooks-exe` is an internal dependency required by the `Setup.hs` wrapper around `SetupHooks`, it is a dependency nonetheless. Given the internal nature of `hooks-exe`, we don't want to impose on our users the obligation to add a dependency on `hooks-exe` in their setup-depends field. Instead, we want `hooks-exe` to be implicitly added to the setup dependencies. This commit does that exactly.
1 parent 902ec82 commit 5a544cb

File tree

3 files changed

+73
-22
lines changed

3 files changed

+73
-22
lines changed

cabal-install/src/Distribution/Client/Dependency.hs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ module Distribution.Client.Dependency
6464
, addDefaultSetupDependencies
6565
, addSetupCabalMinVersionConstraint
6666
, addSetupCabalMaxVersionConstraint
67+
, setImplicitSetupInfo
68+
, extendSetupInfoDeps
6769
) where
6870

6971
import Distribution.Client.Compat.Prelude
@@ -591,49 +593,69 @@ removeBound RelaxUpper RelaxDepModNone = removeUpperBound
591593
removeBound RelaxLower RelaxDepModCaret = transformCaretLower
592594
removeBound RelaxUpper RelaxDepModCaret = transformCaretUpper
593595

594-
-- | Supply defaults for packages without explicit Setup dependencies
596+
-- | Supply defaults for packages without explicit Setup dependencies.
597+
-- It also serves to add the implicit dependency on @hooks-exe@ needed to
598+
-- compile the @Setup.hs@ executable produced from 'SetupHooks' when
599+
-- @build-type: Hooks@. The first argument function determines which implicit
600+
-- dependencies are needed (including the one on @hooks-exe@).
595601
--
596602
-- Note: It's important to apply 'addDefaultSetupDepends' after
597603
-- 'addSourcePackages'. Otherwise, the packages inserted by
598604
-- 'addSourcePackages' won't have upper bounds in dependencies relaxed.
599605
addDefaultSetupDependencies
600-
:: (UnresolvedSourcePackage -> Maybe [Dependency])
606+
:: (Maybe [Dependency] -> PD.BuildType -> Maybe PD.SetupBuildInfo -> Maybe PD.SetupBuildInfo)
607+
-- ^ Function to update the SetupBuildInfo of the package using those dependencies
608+
-> (UnresolvedSourcePackage -> Maybe [Dependency])
609+
-- ^ Function to determine extra setup dependencies
601610
-> DepResolverParams
602611
-> DepResolverParams
603-
addDefaultSetupDependencies defaultSetupDeps params =
612+
addDefaultSetupDependencies applyDefaultSetupDeps defaultSetupDeps params =
604613
params
605614
{ depResolverSourcePkgIndex =
606-
fmap applyDefaultSetupDeps (depResolverSourcePkgIndex params)
615+
fmap go (depResolverSourcePkgIndex params)
607616
}
608617
where
609-
applyDefaultSetupDeps :: UnresolvedSourcePackage -> UnresolvedSourcePackage
610-
applyDefaultSetupDeps srcpkg =
618+
go :: UnresolvedSourcePackage -> UnresolvedSourcePackage
619+
go srcpkg =
611620
srcpkg
612621
{ srcpkgDescription =
613622
gpkgdesc
614623
{ PD.packageDescription =
615624
pkgdesc
616-
{ PD.setupBuildInfo =
617-
case PD.setupBuildInfo pkgdesc of
618-
Just sbi -> Just sbi
619-
Nothing -> case defaultSetupDeps srcpkg of
620-
Nothing -> Nothing
621-
Just deps
622-
| isCustom ->
623-
Just
624-
PD.SetupBuildInfo
625-
{ PD.defaultSetupDepends = True
626-
, PD.setupDepends = deps
627-
}
628-
| otherwise -> Nothing
625+
{ PD.setupBuildInfo = applyDefaultSetupDeps (defaultSetupDeps srcpkg) (PD.buildType pkgdesc) (PD.setupBuildInfo pkgdesc)
629626
}
630627
}
631628
}
632629
where
633-
isCustom = PD.buildType pkgdesc == PD.Custom || PD.buildType pkgdesc == PD.Hooks
634630
gpkgdesc = srcpkgDescription srcpkg
635631
pkgdesc = PD.packageDescription gpkgdesc
636632

633+
setImplicitSetupInfo :: Maybe [Dependency] -> PD.BuildType -> Maybe PD.SetupBuildInfo -> Maybe PD.SetupBuildInfo
634+
setImplicitSetupInfo mdeps buildty msetupinfo =
635+
case msetupinfo of
636+
Just sbi -> Just sbi
637+
Nothing -> case mdeps of
638+
Nothing -> Nothing
639+
Just deps
640+
| isCustom ->
641+
Just
642+
PD.SetupBuildInfo
643+
{ PD.defaultSetupDepends = True
644+
, PD.setupDepends = deps
645+
}
646+
| otherwise -> Nothing
647+
where
648+
isCustom = buildty == PD.Custom || buildty == PD.Hooks
649+
650+
extendSetupInfoDeps :: Maybe [Dependency] -> PD.BuildType -> Maybe PD.SetupBuildInfo -> Maybe PD.SetupBuildInfo
651+
extendSetupInfoDeps mDeps buildTy mSetupInfo
652+
| Nothing <- mSetupInfo =
653+
assert
654+
(buildTy /= PD.Hooks) -- Hooks needs explicit setup-depends
655+
Nothing
656+
| Just setupInfo <- mSetupInfo =
657+
Just setupInfo{PD.setupDepends = PD.setupDepends setupInfo ++ fromMaybe [] mDeps}
658+
637659
-- | If a package has a custom setup then we need to add a setup-depends
638660
-- on Cabal.
639661
addSetupCabalMinVersionConstraint
@@ -713,7 +735,7 @@ standardInstallPolicy
713735
-> [PackageSpecifier UnresolvedSourcePackage]
714736
-> DepResolverParams
715737
standardInstallPolicy installedPkgIndex sourcePkgDb pkgSpecifiers =
716-
addDefaultSetupDependencies mkDefaultSetupDeps $
738+
addDefaultSetupDependencies setImplicitSetupInfo mkDefaultSetupDeps $
717739
basicInstallPolicy
718740
installedPkgIndex
719741
sourcePkgDb

cabal-install/src/Distribution/Client/ProjectPlanning.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ import Distribution.Client.ProjectPlanOutput
117117
import Distribution.Client.ProjectPlanning.SetupPolicy
118118
( NonSetupLibDepSolverPlanPackage (..)
119119
, mkDefaultSetupDeps
120+
, mkHooksSetupImplicitDeps
120121
, packageSetupScriptSpecVersion
121122
, packageSetupScriptStyle
122123
)
@@ -1263,6 +1264,13 @@ planPackages
12631264
. removeLowerBounds solverSettingAllowOlder
12641265
. removeUpperBounds solverSettingAllowNewer
12651266
. addDefaultSetupDependencies
1267+
extendSetupInfoDeps
1268+
( mkHooksSetupImplicitDeps
1269+
. PD.packageDescription
1270+
. srcpkgDescription
1271+
)
1272+
. addDefaultSetupDependencies
1273+
setImplicitSetupInfo
12661274
( mkDefaultSetupDeps comp platform
12671275
. PD.packageDescription
12681276
. srcpkgDescription

cabal-install/src/Distribution/Client/ProjectPlanning/SetupPolicy.hs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@
2222
-- In cases 1 and 2 we obviously have to build an external Setup.hs script,
2323
-- while in case 4 we can use the internal library API.
2424
--
25+
-- Since @3.14.0.0@ we must also consider the @Setup.hs@ scripts constructed
26+
-- from 'SetupHooks' values, because these generated @Setup.hs@ scripts depend
27+
-- on the @hooks-exe@ package (which creates an executable from 'SetupHooks').
28+
-- Therefore, 'SetupPolicy' is also concerned with augmenting the setup
29+
-- dependencies with @hooks-exe@ when @build-type: Hooks@.
30+
--
2531
-- @since 3.12.0.0
2632
module Distribution.Client.ProjectPlanning.SetupPolicy
2733
( mkDefaultSetupDeps
34+
, mkHooksSetupImplicitDeps
2835
, packageSetupScriptStyle
2936
, packageSetupScriptSpecVersion
3037
, NonSetupLibDepSolverPlanPackage (..)
@@ -158,6 +165,19 @@ mkDefaultSetupDeps compiler platform pkg =
158165
csvToVersion :: CabalSpecVersion -> Version
159166
csvToVersion = mkVersion . cabalSpecMinimumLibraryVersion
160167

168+
-- | Returns an implicit dependency on @hooks-exe@ needed to create a
169+
-- @Setup.hs@ executable from a 'SetupHooks' value, if @build-type: Hooks@.
170+
--
171+
-- @since 3.14.0.0
172+
mkHooksSetupImplicitDeps
173+
:: PackageDescription
174+
-> Maybe [Dependency]
175+
mkHooksSetupImplicitDeps pkg
176+
| Hooks <- buildType pkg =
177+
Just [Dependency hooksExePkgname anyVersion mainLibSet]
178+
| otherwise =
179+
Nothing
180+
161181
-- | A newtype for 'SolverPlanPackage' for which the
162182
-- dependency graph considers only dependencies on libraries which are
163183
-- NOT from setup dependencies. Used to compute the set
@@ -217,9 +237,10 @@ packageSetupScriptSpecVersion _ pkg libDepGraph deps =
217237
fromMaybe [] $
218238
Graph.closure libDepGraph (CD.setupDeps deps)
219239

220-
cabalPkgname, basePkgname :: PackageName
240+
cabalPkgname, basePkgname, hooksExePkgname :: PackageName
221241
cabalPkgname = mkPackageName "Cabal"
222242
basePkgname = mkPackageName "base"
243+
hooksExePkgname = mkPackageName "hooks-exe"
223244

224245
legacyCustomSetupPkgs :: Compiler -> Platform -> [PackageName]
225246
legacyCustomSetupPkgs compiler (Platform _ os) =

0 commit comments

Comments
 (0)