/
Types.hs
369 lines (317 loc) · 15.3 KB
/
Types.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
{-# LANGUAGE DeriveGeneric, DeriveDataTypeable, GeneralizedNewtypeDeriving #-}
-- | Handling project configuration, types.
--
module Distribution.Client.ProjectConfig.Types (
-- * Types for project config
ProjectConfig(..),
ProjectConfigBuildOnly(..),
ProjectConfigShared(..),
PackageConfig(..),
-- * Resolving configuration
SolverSettings(..),
BuildTimeSettings(..),
-- * Extra useful Monoids
MapLast(..),
MapMappend(..),
) where
import Distribution.Client.Types
( RemoteRepo )
import Distribution.Client.Dependency.Types
( PreSolver )
import Distribution.Client.Targets
( UserConstraint )
import Distribution.Client.BuildReports.Types
( ReportLevel(..) )
import Distribution.Solver.Types.Settings
import Distribution.Solver.Types.ConstraintSource
import Distribution.Package
( PackageName, PackageId, UnitId, Dependency )
import Distribution.Version
( Version )
import Distribution.System
( Platform )
import Distribution.PackageDescription
( FlagAssignment, SourceRepo(..) )
import Distribution.Simple.Compiler
( Compiler, CompilerFlavor
, OptimisationLevel(..), ProfDetailLevel, DebugInfoLevel(..) )
import Distribution.Simple.Setup
( Flag, AllowNewer(..) )
import Distribution.Simple.InstallDirs
( PathTemplate )
import Distribution.Utils.NubList
( NubList )
import Distribution.Verbosity
( Verbosity )
import Data.Map (Map)
import qualified Data.Map as Map
import Distribution.Compat.Binary (Binary)
import Distribution.Compat.Semigroup
import GHC.Generics (Generic)
-------------------------------
-- Project config types
--
-- | This type corresponds directly to what can be written in the
-- @cabal.project@ file. Other sources of configuration can also be injected
-- into this type, such as the user-wide @~/.cabal/config@ file and the
-- command line of @cabal configure@ or @cabal build@.
--
-- Since it corresponds to the external project file it is an instance of
-- 'Monoid' and all the fields can be empty. This also means there has to
-- be a step where we resolve configuration. At a minimum resolving means
-- applying defaults but it can also mean merging information from multiple
-- sources. For example for package-specific configuration the project file
-- can specify configuration that applies to all local packages, and then
-- additional configuration for a specific package.
--
-- Future directions: multiple profiles, conditionals. If we add these
-- features then the gap between configuration as written in the config file
-- and resolved settings we actually use will become even bigger.
--
data ProjectConfig
= ProjectConfig {
-- | Packages in this project, including local dirs, local .cabal files
-- local and remote tarballs. Where these are file globs, they must
-- match something.
projectPackages :: [String],
-- | Like 'projectConfigPackageGlobs' but /optional/ in the sense that
-- file globs are allowed to match nothing. The primary use case for
-- this is to be able to say @optional-packages: */@ to automagically
-- pick up deps that we unpack locally.
projectPackagesOptional :: [String],
-- | Packages in this project from remote source repositories.
projectPackagesRepo :: [SourceRepo],
-- | Packages in this project from hackage repositories.
projectPackagesNamed :: [Dependency],
projectConfigBuildOnly :: ProjectConfigBuildOnly,
projectConfigShared :: ProjectConfigShared,
projectConfigLocalPackages :: PackageConfig,
projectConfigSpecificPackage :: MapMappend PackageName PackageConfig
}
deriving (Eq, Show, Generic)
-- | That part of the project configuration that only affects /how/ we build
-- and not the /value/ of the things we build. This means this information
-- does not need to be tracked for changes since it does not affect the
-- outcome.
--
data ProjectConfigBuildOnly
= ProjectConfigBuildOnly {
projectConfigVerbosity :: Flag Verbosity,
projectConfigDryRun :: Flag Bool,
projectConfigOnlyDeps :: Flag Bool,
projectConfigSummaryFile :: NubList PathTemplate,
projectConfigLogFile :: Flag PathTemplate,
projectConfigBuildReports :: Flag ReportLevel,
projectConfigReportPlanningFailure :: Flag Bool,
projectConfigSymlinkBinDir :: Flag FilePath,
projectConfigOneShot :: Flag Bool,
projectConfigNumJobs :: Flag (Maybe Int),
projectConfigOfflineMode :: Flag Bool,
projectConfigKeepTempFiles :: Flag Bool,
projectConfigHttpTransport :: Flag String,
projectConfigIgnoreExpiry :: Flag Bool,
projectConfigCacheDir :: Flag FilePath,
projectConfigLogsDir :: Flag FilePath,
projectConfigWorldFile :: Flag FilePath
}
deriving (Eq, Show, Generic)
-- | Project configuration that is shared between all packages in the project.
-- In particular this includes configuration that affects the solver.
--
data ProjectConfigShared
= ProjectConfigShared {
projectConfigHcFlavor :: Flag CompilerFlavor,
projectConfigHcPath :: Flag FilePath,
projectConfigHcPkg :: Flag FilePath,
projectConfigHaddockIndex :: Flag PathTemplate,
-- Things that only make sense for manual mode, not --local mode
-- too much control!
--projectConfigUserInstall :: Flag Bool,
--projectConfigInstallDirs :: InstallDirs (Flag PathTemplate),
--TODO: [required eventually] decide what to do with InstallDirs
-- currently we don't allow it to be specified in the config file
--projectConfigPackageDBs :: [Maybe PackageDB],
-- configuration used both by the solver and other phases
projectConfigRemoteRepos :: NubList RemoteRepo, -- ^ Available Hackage servers.
projectConfigLocalRepos :: NubList FilePath,
-- solver configuration
projectConfigConstraints :: [(UserConstraint, ConstraintSource)],
projectConfigPreferences :: [Dependency],
projectConfigCabalVersion :: Flag Version, --TODO: [required eventually] unused
projectConfigSolver :: Flag PreSolver,
projectConfigAllowOlder :: Maybe AllowNewer,
projectConfigAllowNewer :: Maybe AllowNewer,
projectConfigMaxBackjumps :: Flag Int,
projectConfigReorderGoals :: Flag ReorderGoals,
projectConfigStrongFlags :: Flag StrongFlags
-- More things that only make sense for manual mode, not --local mode
-- too much control!
--projectConfigIndependentGoals :: Flag Bool,
--projectConfigShadowPkgs :: Flag Bool,
--projectConfigReinstall :: Flag Bool,
--projectConfigAvoidReinstalls :: Flag Bool,
--projectConfigOverrideReinstall :: Flag Bool,
--projectConfigUpgradeDeps :: Flag Bool
}
deriving (Eq, Show, Generic)
-- | Project configuration that is specific to each package, that is where we
-- can in principle have different values for different packages in the same
-- project.
--
data PackageConfig
= PackageConfig {
packageConfigProgramPaths :: MapLast String FilePath,
packageConfigProgramArgs :: MapMappend String [String],
packageConfigProgramPathExtra :: NubList FilePath,
packageConfigFlagAssignment :: FlagAssignment,
packageConfigVanillaLib :: Flag Bool,
packageConfigSharedLib :: Flag Bool,
packageConfigDynExe :: Flag Bool,
packageConfigProf :: Flag Bool, --TODO: [code cleanup] sort out
packageConfigProfLib :: Flag Bool, -- this duplication
packageConfigProfExe :: Flag Bool, -- and consistency
packageConfigProfDetail :: Flag ProfDetailLevel,
packageConfigProfLibDetail :: Flag ProfDetailLevel,
packageConfigConfigureArgs :: [String],
packageConfigOptimization :: Flag OptimisationLevel,
packageConfigProgPrefix :: Flag PathTemplate,
packageConfigProgSuffix :: Flag PathTemplate,
packageConfigExtraLibDirs :: [FilePath],
packageConfigExtraFrameworkDirs :: [FilePath],
packageConfigExtraIncludeDirs :: [FilePath],
packageConfigGHCiLib :: Flag Bool,
packageConfigSplitObjs :: Flag Bool,
packageConfigStripExes :: Flag Bool,
packageConfigStripLibs :: Flag Bool,
packageConfigTests :: Flag Bool,
packageConfigBenchmarks :: Flag Bool,
packageConfigCoverage :: Flag Bool,
packageConfigRelocatable :: Flag Bool,
packageConfigDebugInfo :: Flag DebugInfoLevel,
packageConfigRunTests :: Flag Bool, --TODO: [required eventually] use this
packageConfigDocumentation :: Flag Bool, --TODO: [required eventually] use this
packageConfigHaddockHoogle :: Flag Bool, --TODO: [required eventually] use this
packageConfigHaddockHtml :: Flag Bool, --TODO: [required eventually] use this
packageConfigHaddockHtmlLocation :: Flag String, --TODO: [required eventually] use this
packageConfigHaddockExecutables :: Flag Bool, --TODO: [required eventually] use this
packageConfigHaddockTestSuites :: Flag Bool, --TODO: [required eventually] use this
packageConfigHaddockBenchmarks :: Flag Bool, --TODO: [required eventually] use this
packageConfigHaddockInternal :: Flag Bool, --TODO: [required eventually] use this
packageConfigHaddockCss :: Flag FilePath, --TODO: [required eventually] use this
packageConfigHaddockHscolour :: Flag Bool, --TODO: [required eventually] use this
packageConfigHaddockHscolourCss :: Flag FilePath, --TODO: [required eventually] use this
packageConfigHaddockContents :: Flag PathTemplate --TODO: [required eventually] use this
}
deriving (Eq, Show, Generic)
instance Binary ProjectConfig
instance Binary ProjectConfigBuildOnly
instance Binary ProjectConfigShared
instance Binary PackageConfig
-- | Newtype wrapper for 'Map' that provides a 'Monoid' instance that takes
-- the last value rather than the first value for overlapping keys.
newtype MapLast k v = MapLast { getMapLast :: Map k v }
deriving (Eq, Show, Functor, Generic, Binary)
instance Ord k => Monoid (MapLast k v) where
mempty = MapLast Map.empty
mappend = (<>)
instance Ord k => Semigroup (MapLast k v) where
MapLast a <> MapLast b = MapLast (flip Map.union a b)
-- rather than Map.union which is the normal Map monoid instance
-- | Newtype wrapper for 'Map' that provides a 'Monoid' instance that
-- 'mappend's values of overlapping keys rather than taking the first.
newtype MapMappend k v = MapMappend { getMapMappend :: Map k v }
deriving (Eq, Show, Functor, Generic, Binary)
instance (Semigroup v, Ord k) => Monoid (MapMappend k v) where
mempty = MapMappend Map.empty
mappend = (<>)
instance (Semigroup v, Ord k) => Semigroup (MapMappend k v) where
MapMappend a <> MapMappend b = MapMappend (Map.unionWith (<>) a b)
-- rather than Map.union which is the normal Map monoid instance
instance Monoid ProjectConfig where
mempty = gmempty
mappend = (<>)
instance Semigroup ProjectConfig where
(<>) = gmappend
instance Monoid ProjectConfigBuildOnly where
mempty = gmempty
mappend = (<>)
instance Semigroup ProjectConfigBuildOnly where
(<>) = gmappend
instance Monoid ProjectConfigShared where
mempty = gmempty
mappend = (<>)
instance Semigroup ProjectConfigShared where
(<>) = gmappend
instance Monoid PackageConfig where
mempty = gmempty
mappend = (<>)
instance Semigroup PackageConfig where
(<>) = gmappend
----------------------------------------
-- Resolving configuration to settings
--
-- | Resolved configuration for the solver. The idea is that this is easier to
-- use than the raw configuration because in the raw configuration everything
-- is optional (monoidial). In the 'BuildTimeSettings' every field is filled
-- in, if only with the defaults.
--
-- Use 'resolveSolverSettings' to make one from the project config (by
-- applying defaults etc).
--
data SolverSettings
= SolverSettings {
solverSettingRemoteRepos :: [RemoteRepo], -- ^ Available Hackage servers.
solverSettingLocalRepos :: [FilePath],
solverSettingConstraints :: [(UserConstraint, ConstraintSource)],
solverSettingPreferences :: [Dependency],
solverSettingFlagAssignment :: FlagAssignment, -- ^ For all local packages
solverSettingFlagAssignments :: Map PackageName FlagAssignment,
solverSettingCabalVersion :: Maybe Version, --TODO: [required eventually] unused
solverSettingSolver :: PreSolver,
solverSettingAllowOlder :: AllowNewer,
solverSettingAllowNewer :: AllowNewer,
solverSettingMaxBackjumps :: Maybe Int,
solverSettingReorderGoals :: ReorderGoals,
solverSettingStrongFlags :: StrongFlags
-- Things that only make sense for manual mode, not --local mode
-- too much control!
--solverSettingIndependentGoals :: Bool,
--solverSettingShadowPkgs :: Bool,
--solverSettingReinstall :: Bool,
--solverSettingAvoidReinstalls :: Bool,
--solverSettingOverrideReinstall :: Bool,
--solverSettingUpgradeDeps :: Bool
}
deriving (Eq, Show, Generic)
instance Binary SolverSettings
-- | Resolved configuration for things that affect how we build and not the
-- value of the things we build. The idea is that this is easier to use than
-- the raw configuration because in the raw configuration everything is
-- optional (monoidial). In the 'BuildTimeSettings' every field is filled in,
-- if only with the defaults.
--
-- Use 'resolveBuildTimeSettings' to make one from the project config (by
-- applying defaults etc).
--
data BuildTimeSettings
= BuildTimeSettings {
buildSettingDryRun :: Bool,
buildSettingOnlyDeps :: Bool,
buildSettingSummaryFile :: [PathTemplate],
buildSettingLogFile :: Maybe (Compiler -> Platform
-> PackageId -> UnitId
-> FilePath),
buildSettingLogVerbosity :: Verbosity,
buildSettingBuildReports :: ReportLevel,
buildSettingReportPlanningFailure :: Bool,
buildSettingSymlinkBinDir :: [FilePath],
buildSettingOneShot :: Bool,
buildSettingNumJobs :: Int,
buildSettingOfflineMode :: Bool,
buildSettingKeepTempFiles :: Bool,
buildSettingRemoteRepos :: [RemoteRepo],
buildSettingLocalRepos :: [FilePath],
buildSettingCacheDir :: FilePath,
buildSettingHttpTransport :: Maybe String,
buildSettingIgnoreExpiry :: Bool
}