forked from commercialhaskell/stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetupHooks.hs
103 lines (96 loc) · 4.29 KB
/
SetupHooks.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
{-# LANGUAGE OverloadedStrings #-}
-- | See https://github.com/well-typed/hooks-build-type. As part of their work,
-- Well-Typed reviewed stack-2.13.1 and identified that it used a pre-build hook
-- to generate, for the stack main library component, a module that lists all
-- the dependencies of stack (both library and executable), which is used in
-- `Stack.BuildInfo` to be listed. They also wrote an experimental patch, the
-- source code of which is below (with some reformatting).
--
-- This would be used if Stack's build type was 'Hooks' rather than 'Custom'.
module SetupHooks
( setupHooks
) where
import Data.List ( nub, sortBy )
import Data.Ord ( comparing )
import Distribution.InstalledPackageInfo
( installedUnitId, sourcePackageId )
import Distribution.Package
( PackageId, UnitId, packageName, packageVersion )
import Distribution.PackageDescription
( PackageDescription (..), Executable (..), componentNameRaw
)
import Distribution.Pretty ( prettyShow )
import Distribution.Simple
( UserHooks(..), defaultMainWithHooks, simpleUserHooks )
import Distribution.Simple.BuildPaths ( autogenComponentModulesDir )
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.PackageIndex
( allPackages, dependencyClosure )
import Distribution.Simple.Setup ( BuildFlags (..), fromFlag )
import Distribution.Simple.SetupHooks
import Distribution.Simple.Utils
( createDirectoryIfMissingVerbose, rewriteFileEx )
import Distribution.Types.PackageName ( PackageName, unPackageName )
import Distribution.Types.UnqualComponentName
( unUnqualComponentName )
import Distribution.Verbosity ( Verbosity, normal )
import System.FilePath ( (</>) )
setupHooks :: SetupHooks
setupHooks =
noSetupHooks
{ buildHooks =
noBuildHooks
{ preBuildComponentHook = Just preBuildHook }
}
preBuildHook :: BuildingWhat -> LocalBuildInfo -> TargetInfo -> IO ()
preBuildHook flags lbi tgt
| CLibName LMainLibName <- componentName $ targetComponent tgt =
generateBuildModule (buildingWhatVerbosity flags) (localPkgDescr lbi)
lbi tgt
| otherwise = pure ()
generateBuildModule ::
Verbosity
-> PackageDescription
-> LocalBuildInfo
-> TargetInfo
-> IO ()
generateBuildModule verbosity pkg lbi mainLibTargetInfo = do
-- Generate a module in the stack library component that lists all the
-- dependencies of stack (both the library and the executable).
createDirectoryIfMissingVerbose verbosity True autogenDir
withExeLBI pkg lbi $ \ _ exeCLBI -> do
rewriteFileEx normal buildModulePath $ unlines
[ "module Build_" ++ pkgNm
, " ( deps"
, " ) where"
, ""
, "deps :: [String]"
, "deps = " ++ (show $ formatdeps (transDeps mainLibCLBI exeCLBI))
]
where
mainLibCLBI = targetCLBI mainLibTargetInfo
autogenDir = autogenComponentModulesDir lbi mainLibCLBI
pkgNm :: String
pkgNm = unPackageName' $ package pkg
buildModulePath = autogenDir </> "Build_" ++ pkgNm ++ ".hs"
formatdeps = map formatone . sortBy (comparing unPackageName')
formatone p = unPackageName' p ++ "-" ++ prettyShow (packageVersion p)
unPackageName' = unPackageName . packageName
transDeps xs ys = either
(map sourcePackageId . allPackages)
handleDepClosureFailure $ dependencyClosure allInstPkgsIdx availInstPkgIds
where
allInstPkgsIdx = installedPkgs lbi
allInstPkgIds = map installedUnitId $ allPackages allInstPkgsIdx
-- instPkgIds includes `stack-X.X.X`, which is not a dependency hence is
-- missing from allInstPkgsIdx. Filter that out.
availInstPkgIds = filter (`elem` allInstPkgIds) $ testDeps xs ys
handleDepClosureFailure unsatisfied =
error $
"Computation of transitive dependencies failed."
++ if null unsatisfied
then ""
else " Unresolved dependencies: " ++ show unsatisfied
testDeps :: ComponentLocalBuildInfo -> ComponentLocalBuildInfo -> [UnitId]
testDeps xs ys =
map fst $ nub $ componentPackageDeps xs ++ componentPackageDeps ys