PackageDescription.hs 50.5 KB
Newer Older
1
{-# LANGUAGE DeriveDataTypeable #-}
ttuegel's avatar
ttuegel committed
2
3
{-# LANGUAGE DeriveGeneric #-}

simonmar's avatar
simonmar committed
4
5
-----------------------------------------------------------------------------
-- |
6
-- Module      :  Distribution.PackageDescription
ijones's avatar
ijones committed
7
-- Copyright   :  Isaac Jones 2003-2005
8
-- License     :  BSD3
9
--
Duncan Coutts's avatar
Duncan Coutts committed
10
-- Maintainer  :  cabal-devel@haskell.org
ijones's avatar
ijones committed
11
-- Portability :  portable
simonmar's avatar
simonmar committed
12
--
Duncan Coutts's avatar
Duncan Coutts committed
13
-- This defines the data structure for the @.cabal@ file format. There are
ttuegel's avatar
ttuegel committed
14
-- several parts to this structure. It has top level info and then 'Library',
15
16
17
18
-- 'Executable', 'TestSuite', and 'Benchmark' sections each of which have
-- associated 'BuildInfo' data that's used to build the library, exe, test, or
-- benchmark.  To further complicate things there is both a 'PackageDescription'
-- and a 'GenericPackageDescription'. This distinction relates to cabal
ttuegel's avatar
ttuegel committed
19
20
21
-- configurations. When we initially read a @.cabal@ file we get a
-- 'GenericPackageDescription' which has all the conditional sections.
-- Before actually building a package we have to decide
Duncan Coutts's avatar
Duncan Coutts committed
22
23
24
25
-- on each conditional. Once we've done that we get a 'PackageDescription'.
-- It was done this way initially to avoid breaking too much stuff when the
-- feature was introduced. It could probably do with being rationalised at some
-- point to make it simpler.
simonmar's avatar
simonmar committed
26

27
module Distribution.PackageDescription (
ijones's avatar
ijones committed
28
        -- * Package descriptions
ijones's avatar
ijones committed
29
30
        PackageDescription(..),
        emptyPackageDescription,
31
32
        specVersion,
        descCabalVersion,
Simon Marlow's avatar
Simon Marlow committed
33
        BuildType(..),
34
        knownBuildTypes,
Simon Marlow's avatar
Simon Marlow committed
35

36
37
38
39
40
        -- ** Renaming
        ModuleRenaming(..),
        defaultRenaming,
        lookupRenaming,

41
        -- ** Libraries
ijones's avatar
ijones committed
42
        Library(..),
43
        ModuleReexport(..),
44
        emptyLibrary,
ijones's avatar
ijones committed
45
        withLib,
46
        hasPublicLib,
ijones's avatar
ijones committed
47
48
        hasLibs,
        libModules,
Simon Marlow's avatar
Simon Marlow committed
49

50
        -- ** Executables
ijones's avatar
ijones committed
51
        Executable(..),
52
        emptyExecutable,
ijones's avatar
ijones committed
53
        withExe,
54
        hasExes,
ijones's avatar
ijones committed
55
        exeModules,
Simon Marlow's avatar
Simon Marlow committed
56

ttuegel's avatar
ttuegel committed
57
        -- * Tests
58
        TestSuite(..),
59
        TestSuiteInterface(..),
ttuegel's avatar
ttuegel committed
60
        TestType(..),
61
62
        testType,
        knownTestTypes,
63
        emptyTestSuite,
ttuegel's avatar
ttuegel committed
64
        hasTests,
65
        withTest,
ttuegel's avatar
ttuegel committed
66
        testModules,
67
        enabledTests,
ttuegel's avatar
ttuegel committed
68

69
70
71
72
73
74
75
        -- * Benchmarks
        Benchmark(..),
        BenchmarkInterface(..),
        BenchmarkType(..),
        benchmarkType,
        knownBenchmarkTypes,
        emptyBenchmark,
76
        hasBenchmarks,
77
        withBenchmark,
78
        benchmarkModules,
79
        enabledBenchmarks,
80

ijones's avatar
ijones committed
81
82
83
        -- * Build information
        BuildInfo(..),
        emptyBuildInfo,
84
        allBuildInfo,
85
86
87
        allLanguages,
        allExtensions,
        usedExtensions,
88
        hcOptions,
89
90
        hcProfOptions,
        hcSharedOptions,
Simon Marlow's avatar
Simon Marlow committed
91

ijones's avatar
ijones committed
92
        -- ** Supplementary build information
93
94
        ComponentName(..),
        defaultLibName,
ijones's avatar
ijones committed
95
96
97
        HookedBuildInfo,
        emptyHookedBuildInfo,
        updatePackageDescription,
98
99
100

        -- * package configuration
        GenericPackageDescription(..),
101
102
        Flag(..), FlagName(..), FlagAssignment,
        CondTree(..), ConfVar(..), Condition(..),
103
        cNot, cAnd, cOr,
104
105

        -- * Source repositories
106
107
108
109
        SourceRepo(..),
        RepoKind(..),
        RepoType(..),
        knownRepoTypes,
110
111
112

        -- * Custom setup build information
        SetupBuildInfo(..),
simonmar's avatar
simonmar committed
113
114
  ) where

115
import Distribution.Compat.Binary
116
import qualified Distribution.Compat.Semigroup as Semi ((<>))
117
import Distribution.Compat.Semigroup as Semi (Monoid(..), Semigroup, gmempty)
118
119
120
121
122
123
124
125
126
127
128
import qualified Distribution.Compat.ReadP as Parse
import Distribution.Compat.ReadP   ((<++))
import Distribution.Package
import Distribution.ModuleName
import Distribution.Version
import Distribution.License
import Distribution.Compiler
import Distribution.System
import Distribution.Text
import Language.Haskell.Extension

129
130
131
import Data.Data                  (Data)
import Data.List                  (nub, intercalate)
import Data.Maybe                 (fromMaybe, maybeToList)
132
133
import Data.Foldable as Fold      (Foldable(foldMap))
import Data.Traversable as Trav   (Traversable(traverse))
134
import Data.Typeable               ( Typeable )
135
import Control.Applicative as AP   (Alternative(..), Applicative(..))
136
import Control.Monad               (MonadPlus(mplus,mzero), ap)
137
import GHC.Generics                (Generic)
dterei's avatar
dterei committed
138
import Text.PrettyPrint as Disp
139
import qualified Data.Char as Char (isAlphaNum, isDigit, toLower)
140
import qualified Data.Map as Map
141
import Data.Map                    (Map)
simonmar's avatar
simonmar committed
142

Simon Marlow's avatar
Simon Marlow committed
143
144
145
-- -----------------------------------------------------------------------------
-- The PackageDescription type

146
147
-- | This data type is the internal representation of the file @pkg.cabal@.
-- It contains two kinds of information about the package: information
148
149
-- which is needed for all packages, such as the package name and version, and
-- information which is needed for the simple build system only, such as
150
-- the compiler options and library name.
151
--
152
153
154
155
156
data PackageDescription
    =  PackageDescription {
        -- the following are required by all packages:
        package        :: PackageIdentifier,
        license        :: License,
Duncan Coutts's avatar
Duncan Coutts committed
157
        licenseFiles   :: [FilePath],
158
159
160
161
162
163
164
        copyright      :: String,
        maintainer     :: String,
        author         :: String,
        stability      :: String,
        testedWith     :: [(CompilerFlavor,VersionRange)],
        homepage       :: String,
        pkgUrl         :: String,
Duncan Coutts's avatar
Duncan Coutts committed
165
        bugReports     :: String,
166
        sourceRepos    :: [SourceRepo],
167
168
169
        synopsis       :: String, -- ^A one-line summary of this package
        description    :: String, -- ^A more verbose description of this package
        category       :: String,
170
171
        customFieldsPD :: [(String,String)], -- ^Custom fields starting
                                             -- with x-, stored in a
172
                                             -- simple assoc-list.
173

174
175
176
177
178
179
180
181
182
183
184
        -- | YOU PROBABLY DON'T WANT TO USE THIS FIELD. This field is
        -- special! Depending on how far along processing the
        -- PackageDescription we are, the contents of this field are
        -- either nonsense, or the collected dependencies of *all* the
        -- components in this package.  buildDepends is initialized by
        -- 'finalizePackageDescription' and 'flattenPackageDescription';
        -- prior to that, dependency info is stored in the 'CondTree'
        -- built around a 'GenericPackageDescription'.  When this
        -- resolution is done, dependency info is written to the inner
        -- 'BuildInfo' and this field.  This is all horrible, and #2066
        -- tracks progress to get rid of this field.
185
        buildDepends   :: [Dependency],
186
187
188
189
190
        -- | The version of the Cabal spec that this package description uses.
        -- For historical reasons this is specified with a version range but
        -- only ranges of the form @>= v@ make sense. We are in the process of
        -- transitioning to specifying just a single version, not a range.
        specVersionRaw :: Either Version VersionRange,
191
        buildType      :: Maybe BuildType,
192
        setupBuildInfo :: Maybe SetupBuildInfo,
193
        -- components
194
        libraries      :: [Library],
195
        executables    :: [Executable],
196
        testSuites     :: [TestSuite],
197
        benchmarks     :: [Benchmark],
198
        dataFiles      :: [FilePath],
199
        dataDir        :: FilePath,
200
        extraSrcFiles  :: [FilePath],
201
        extraTmpFiles  :: [FilePath],
202
        extraDocFiles  :: [FilePath]
203
    }
ttuegel's avatar
ttuegel committed
204
205
206
    deriving (Generic, Show, Read, Eq, Typeable, Data)

instance Binary PackageDescription
207

208
209
210
instance Package PackageDescription where
  packageId = package

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
-- | The version of the Cabal spec that this package should be interpreted
-- against.
--
-- Historically we used a version range but we are switching to using a single
-- version. Currently we accept either. This function converts into a single
-- version by ignoring upper bounds in the version range.
--
specVersion :: PackageDescription -> Version
specVersion pkg = case specVersionRaw pkg of
  Left  version      -> version
  Right versionRange -> case asVersionIntervals versionRange of
                          []                            -> Version [0] []
                          ((LowerBound version _, _):_) -> version

-- | The range of versions of the Cabal tools that this package is intended to
-- work with.
--
-- This function is deprecated and should not be used for new purposes, only to
-- support old packages that rely on the old interpretation.
--
descCabalVersion :: PackageDescription -> VersionRange
descCabalVersion pkg = case specVersionRaw pkg of
  Left  version      -> orLaterVersion version
  Right versionRange -> versionRange
{-# DEPRECATED descCabalVersion "Use specVersion instead" #-}

237
238
emptyPackageDescription :: PackageDescription
emptyPackageDescription
Duncan Coutts's avatar
Duncan Coutts committed
239
240
241
    =  PackageDescription {
                      package      = PackageIdentifier (PackageName "")
                                                       (Version [] []),
242
                      license      = UnspecifiedLicense,
Duncan Coutts's avatar
Duncan Coutts committed
243
                      licenseFiles = [],
244
                      specVersionRaw = Right anyVersion,
245
246
247
248
249
250
251
252
253
                      buildType    = Nothing,
                      copyright    = "",
                      maintainer   = "",
                      author       = "",
                      stability    = "",
                      testedWith   = [],
                      buildDepends = [],
                      homepage     = "",
                      pkgUrl       = "",
Duncan Coutts's avatar
Duncan Coutts committed
254
                      bugReports   = "",
255
                      sourceRepos  = [],
256
257
258
                      synopsis     = "",
                      description  = "",
                      category     = "",
259
                      customFieldsPD = [],
260
                      setupBuildInfo = Nothing,
261
                      libraries    = [],
262
                      executables  = [],
263
                      testSuites   = [],
264
                      benchmarks   = [],
265
                      dataFiles    = [],
266
                      dataDir      = "",
267
                      extraSrcFiles = [],
268
                      extraTmpFiles = [],
269
                      extraDocFiles = []
270
271
272
273
274
275
276
277
278
279
                     }

-- | The type of build system used by this package.
data BuildType
  = Simple      -- ^ calls @Distribution.Simple.defaultMain@
  | Configure   -- ^ calls @Distribution.Simple.defaultMainWithHooks defaultUserHooks@,
                -- which invokes @configure@ to generate additional build
                -- information used by later phases.
  | Make        -- ^ calls @Distribution.Make.defaultMain@
  | Custom      -- ^ uses user-supplied @Setup.hs@ or @Setup.lhs@ (default)
280
281
282
283
284
  | UnknownBuildType String
                -- ^ a package that uses an unknown build type cannot actually
                --   be built. Doing it this way rather than just giving a
                --   parse error means we get better error messages and allows
                --   you to inspect the rest of the package description.
ttuegel's avatar
ttuegel committed
285
286
287
                deriving (Generic, Show, Read, Eq, Typeable, Data)

instance Binary BuildType
288

289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
knownBuildTypes :: [BuildType]
knownBuildTypes = [Simple, Configure, Make, Custom]

instance Text BuildType where
  disp (UnknownBuildType other) = Disp.text other
  disp other                    = Disp.text (show other)

  parse = do
    name <- Parse.munch1 Char.isAlphaNum
    return $ case name of
      "Simple"    -> Simple
      "Configure" -> Configure
      "Custom"    -> Custom
      "Make"      -> Make
      _           -> UnknownBuildType name

305
306
307
308
309
310
311
312
-- ---------------------------------------------------------------------------
-- The SetupBuildInfo type

-- One can see this as a very cut-down version of BuildInfo below.
-- To keep things simple for tools that compile Setup.hs we limit the
-- options authors can specify to just Haskell package dependencies.

data SetupBuildInfo = SetupBuildInfo {
313
314
315
316
317
318
        setupDepends        :: [Dependency],
        defaultSetupDepends :: Bool
        -- ^ Is this a default 'custom-setup' section added by the cabal-install
        -- code (as opposed to user-provided)? This field is only used
        -- internally, and doesn't correspond to anything in the .cabal
        -- file. See #3199.
319
320
321
322
323
    }
    deriving (Generic, Show, Eq, Read, Typeable, Data)

instance Binary SetupBuildInfo

324
instance Semi.Monoid SetupBuildInfo where
325
  mempty  = SetupBuildInfo [] False
326
327
328
  mappend = (Semi.<>)

instance Semigroup SetupBuildInfo where
329
330
  a <> b = SetupBuildInfo (setupDepends a Semi.<> setupDepends b)
           (defaultSetupDepends a || defaultSetupDepends b)
331

332
333
334
335
336
337
338
339
340
341
-- ---------------------------------------------------------------------------
-- Module renaming

-- | Renaming applied to the modules provided by a package.
-- The boolean indicates whether or not to also include all of the
-- original names of modules.  Thus, @ModuleRenaming False []@ is
-- "don't expose any modules, and @ModuleRenaming True [("Data.Bool", "Bool")]@
-- is, "expose all modules, but also expose @Data.Bool@ as @Bool@".
--
data ModuleRenaming = ModuleRenaming Bool [(ModuleName, ModuleName)]
342
    deriving (Show, Read, Eq, Ord, Typeable, Data, Generic)
343
344
345
346
347

defaultRenaming :: ModuleRenaming
defaultRenaming = ModuleRenaming True []

lookupRenaming :: Package pkg => pkg -> Map PackageName ModuleRenaming -> ModuleRenaming
348
lookupRenaming = Map.findWithDefault defaultRenaming . packageName
349
350
351
352
353

instance Binary ModuleRenaming where

instance Monoid ModuleRenaming where
    mempty = ModuleRenaming False []
354
355
356
357
    mappend = (Semi.<>)

instance Semigroup ModuleRenaming where
    ModuleRenaming b rns <> ModuleRenaming b' rns'
Edward Z. Yang's avatar
Edward Z. Yang committed
358
        = ModuleRenaming (b || b') (rns ++ rns') -- TODO: dedupe?
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393

-- NB: parentheses are mandatory, because later we may extend this syntax
-- to allow "hiding (A, B)" or other modifier words.
instance Text ModuleRenaming where
  disp (ModuleRenaming True []) = Disp.empty
  disp (ModuleRenaming b vs) = (if b then text "with" else Disp.empty) <+> dispRns
    where dispRns = Disp.parens
                         (Disp.hsep
                            (Disp.punctuate Disp.comma (map dispEntry vs)))
          dispEntry (orig, new)
            | orig == new = disp orig
            | otherwise = disp orig <+> text "as" <+> disp new

  parse = do Parse.string "with" >> Parse.skipSpaces
             fmap (ModuleRenaming True) parseRns
         <++ fmap (ModuleRenaming False) parseRns
         <++ return (ModuleRenaming True [])
    where parseRns = do
             rns <- Parse.between (Parse.char '(') (Parse.char ')') parseList
             Parse.skipSpaces
             return rns
          parseList =
            Parse.sepBy parseEntry (Parse.char ',' >> Parse.skipSpaces)
          parseEntry :: Parse.ReadP r (ModuleName, ModuleName)
          parseEntry = do
            orig <- parse
            Parse.skipSpaces
            (do _ <- Parse.string "as"
                Parse.skipSpaces
                new <- parse
                Parse.skipSpaces
                return (orig, new)
             <++
                return (orig, orig))

394
395
396
397
-- ---------------------------------------------------------------------------
-- The Library type

data Library = Library {
398
        libName :: String,
399
        exposedModules    :: [ModuleName],
400
        reexportedModules :: [ModuleReexport],
401
402
        requiredSignatures:: [ModuleName], -- ^ What sigs need implementations?
        exposedSignatures:: [ModuleName], -- ^ What sigs are visible to users?
403
        libExposed        :: Bool, -- ^ Is the lib to be exposed by default?
404
405
        libBuildInfo      :: BuildInfo
    }
ttuegel's avatar
ttuegel committed
406
407
408
    deriving (Generic, Show, Eq, Read, Typeable, Data)

instance Binary Library
409
410

instance Monoid Library where
411
  mempty = Library {
412
    libName = mempty,
413
    exposedModules = mempty,
414
    reexportedModules = mempty,
415
416
    requiredSignatures = mempty,
    exposedSignatures = mempty,
417
    libExposed     = True,
418
419
    libBuildInfo   = mempty
  }
420
421
422
423
  mappend = (Semi.<>)

instance Semigroup Library where
  a <> b = Library {
424
    libName = combine' libName,
425
    exposedModules = combine exposedModules,
426
    reexportedModules = combine reexportedModules,
427
428
    requiredSignatures = combine requiredSignatures,
    exposedSignatures = combine exposedSignatures,
429
    libExposed     = libExposed a && libExposed b, -- so False propagates
430
431
432
    libBuildInfo   = combine libBuildInfo
  }
    where combine field = field a `mappend` field b
433
434
435
436
437
438
          combine' field = case (field a, field b) of
                      ("","") -> ""
                      ("", x) -> x
                      (x, "") -> x
                      (x, y) -> error $ "Ambiguous values for library field: '"
                                  ++ x ++ "' and '" ++ y ++ "'"
439
440

emptyLibrary :: Library
441
emptyLibrary = mempty
442

443
444
445
446
447
448
449
-- | Does this package have a PUBLIC library?
hasPublicLib :: PackageDescription -> Bool
hasPublicLib p = any f (libraries p)
    where f lib = buildable (libBuildInfo lib) &&
                  libName lib == display (packageName (package p))

-- | Does this package have any libraries?
450
hasLibs :: PackageDescription -> Bool
451
hasLibs p = any (buildable . libBuildInfo) (libraries p)
452
453
454

-- |If the package description has a library section, call the given
--  function with the library build info as argument.
455
456
withLib :: PackageDescription -> (Library -> IO ()) -> IO ()
withLib pkg_descr f =
457
   sequence_ [f lib | lib <- libraries pkg_descr, buildable (libBuildInfo lib)]
458

459
-- | Get all the module names from the library (exposed and internal modules)
460
461
-- which need to be compiled.  (This does not include reexports, which
-- do not need to be compiled.)
462
463
464
libModules :: Library -> [ModuleName]
libModules lib = exposedModules lib
              ++ otherModules (libBuildInfo lib)
465
466
467
              ++ exposedSignatures lib
              ++ requiredSignatures lib

468
469
470
471
472
473
474
475
-- -----------------------------------------------------------------------------
-- Module re-exports

data ModuleReexport = ModuleReexport {
       moduleReexportOriginalPackage :: Maybe PackageName,
       moduleReexportOriginalName    :: ModuleName,
       moduleReexportName            :: ModuleName
    }
ttuegel's avatar
ttuegel committed
476
477
478
    deriving (Eq, Generic, Read, Show, Typeable, Data)

instance Binary ModuleReexport
479
480
481
482
483
484
485
486
487
488
489

instance Text ModuleReexport where
    disp (ModuleReexport mpkgname origname newname) =
          maybe Disp.empty (\pkgname -> disp pkgname <> Disp.char ':') mpkgname
       <> disp origname
      <+> if newname == origname
            then Disp.empty
            else Disp.text "as" <+> disp newname

    parse = do
      mpkgname <- Parse.option Nothing $ do
490
                    pkgname <- parse
491
492
493
494
495
496
497
498
499
500
                    _       <- Parse.char ':'
                    return (Just pkgname)
      origname <- parse
      newname  <- Parse.option origname $ do
                    Parse.skipSpaces
                    _ <- Parse.string "as"
                    Parse.skipSpaces
                    parse
      return (ModuleReexport mpkgname origname newname)

501
502
503
504
505
506
507
508
-- ---------------------------------------------------------------------------
-- The Executable type

data Executable = Executable {
        exeName    :: String,
        modulePath :: FilePath,
        buildInfo  :: BuildInfo
    }
ttuegel's avatar
ttuegel committed
509
510
511
    deriving (Generic, Show, Read, Eq, Typeable, Data)

instance Binary Executable
512
513

instance Monoid Executable where
514
  mempty = gmempty
515
516
517
518
  mappend = (Semi.<>)

instance Semigroup Executable where
  a <> b = Executable{
519
520
521
522
523
524
525
526
527
528
529
    exeName    = combine' exeName,
    modulePath = combine modulePath,
    buildInfo  = combine buildInfo
  }
    where combine field = field a `mappend` field b
          combine' field = case (field a, field b) of
                      ("","") -> ""
                      ("", x) -> x
                      (x, "") -> x
                      (x, y) -> error $ "Ambiguous values for executable field: '"
                                  ++ x ++ "' and '" ++ y ++ "'"
530
531

emptyExecutable :: Executable
532
emptyExecutable = mempty
533
534
535
536
537
538
539

-- |does this package have any executables?
hasExes :: PackageDescription -> Bool
hasExes p = any (buildable . buildInfo) (executables p)

-- | Perform the action on each buildable 'Executable' in the package
-- description.
540
withExe :: PackageDescription -> (Executable -> IO ()) -> IO ()
541
542
543
withExe pkg_descr f =
  sequence_ [f exe | exe <- executables pkg_descr, buildable (buildInfo exe)]

544
545
546
-- | Get all the module names from an exe
exeModules :: Executable -> [ModuleName]
exeModules exe = otherModules (buildInfo exe)
547

ttuegel's avatar
ttuegel committed
548
-- ---------------------------------------------------------------------------
549
-- The TestSuite type
ttuegel's avatar
ttuegel committed
550

551
552
-- | A \"test-suite\" stanza in a cabal file.
--
553
data TestSuite = TestSuite {
554
555
        testName      :: String,
        testInterface :: TestSuiteInterface,
556
557
558
559
560
561
562
        testBuildInfo :: BuildInfo,
        testEnabled   :: Bool
        -- TODO: By having a 'testEnabled' field in the PackageDescription, we
        -- are mixing build status information (i.e., arguments to 'configure')
        -- with static package description information. This is undesirable, but
        -- a better solution is waiting on the next overhaul to the
        -- GenericPackageDescription -> PackageDescription resolution process.
ttuegel's avatar
ttuegel committed
563
    }
ttuegel's avatar
ttuegel committed
564
565
566
    deriving (Generic, Show, Read, Eq, Typeable, Data)

instance Binary TestSuite
ttuegel's avatar
ttuegel committed
567

568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
-- | The test suite interfaces that are currently defined. Each test suite must
-- specify which interface it supports.
--
-- More interfaces may be defined in future, either new revisions or totally
-- new interfaces.
--
data TestSuiteInterface =

     -- | Test interface \"exitcode-stdio-1.0\". The test-suite takes the form
     -- of an executable. It returns a zero exit code for success, non-zero for
     -- failure. The stdout and stderr channels may be logged. It takes no
     -- command line parameters and nothing on stdin.
     --
     TestSuiteExeV10 Version FilePath

     -- | Test interface \"detailed-0.9\". The test-suite takes the form of a
     -- library containing a designated module that exports \"tests :: [Test]\".
     --
   | TestSuiteLibV09 Version ModuleName

     -- | A test suite that does not conform to one of the above interfaces for
     -- the given reason (e.g. unknown test type).
     --
   | TestSuiteUnsupported TestType
ttuegel's avatar
ttuegel committed
592
593
594
   deriving (Eq, Generic, Read, Show, Typeable, Data)

instance Binary TestSuiteInterface
ttuegel's avatar
ttuegel committed
595

596
597
instance Monoid TestSuite where
    mempty = TestSuite {
598
599
        testName      = mempty,
        testInterface = mempty,
600
601
        testBuildInfo = mempty,
        testEnabled   = False
ttuegel's avatar
ttuegel committed
602
    }
603
    mappend = (Semi.<>)
ttuegel's avatar
ttuegel committed
604

605
606
instance Semigroup TestSuite where
    a <> b = TestSuite {
607
608
        testName      = combine' testName,
        testInterface = combine  testInterface,
609
610
        testBuildInfo = combine  testBuildInfo,
        testEnabled   = testEnabled a || testEnabled b
ttuegel's avatar
ttuegel committed
611
    }
612
613
        where combine   field = field a `mappend` field b
              combine' f = case (f a, f b) of
ttuegel's avatar
ttuegel committed
614
615
616
617
618
                        ("", x) -> x
                        (x, "") -> x
                        (x, y) -> error "Ambiguous values for test field: '"
                            ++ x ++ "' and '" ++ y ++ "'"

619
620
instance Monoid TestSuiteInterface where
    mempty  =  TestSuiteUnsupported (TestTypeUnknown mempty (Version [] []))
621
622
623
624
625
    mappend = (Semi.<>)

instance Semigroup TestSuiteInterface where
    a <> (TestSuiteUnsupported _) = a
    _ <> b                        = b
626

627
628
emptyTestSuite :: TestSuite
emptyTestSuite = mempty
ttuegel's avatar
ttuegel committed
629

630
-- | Does this package have any test suites?
ttuegel's avatar
ttuegel committed
631
hasTests :: PackageDescription -> Bool
632
hasTests = any (buildable . testBuildInfo) . testSuites
ttuegel's avatar
ttuegel committed
633

634
635
636
637
638
639
640
641
642
-- | Get all the enabled test suites from a package.
enabledTests :: PackageDescription -> [TestSuite]
enabledTests = filter testEnabled . testSuites

-- | Perform an action on each buildable 'TestSuite' in a package.
withTest :: PackageDescription -> (TestSuite -> IO ()) -> IO ()
withTest pkg_descr f =
    mapM_ f $ filter (buildable . testBuildInfo) $ enabledTests pkg_descr

643
644
-- | Get all the module names from a test suite.
testModules :: TestSuite -> [ModuleName]
645
646
647
648
testModules test = (case testInterface test of
                     TestSuiteLibV09 _ m -> [m]
                     _                   -> [])
                ++ otherModules (testBuildInfo test)
ttuegel's avatar
ttuegel committed
649

650
651
652
653
654
-- | The \"test-type\" field in the test suite stanza.
--
data TestType = TestTypeExe Version     -- ^ \"type: exitcode-stdio-x.y\"
              | TestTypeLib Version     -- ^ \"type: detailed-x.y\"
              | TestTypeUnknown String Version -- ^ Some unknown test type e.g. \"type: foo\"
ttuegel's avatar
ttuegel committed
655
656
657
    deriving (Generic, Show, Read, Eq, Typeable, Data)

instance Binary TestType
658
659
660
661
662

knownTestTypes :: [TestType]
knownTestTypes = [ TestTypeExe (Version [1,0] [])
                 , TestTypeLib (Version [0,9] []) ]

663
664
665
666
667
stdParse :: Text ver => (ver -> String -> res) -> Parse.ReadP r res
stdParse f = do
  cs   <- Parse.sepBy1 component (Parse.char '-')
  _    <- Parse.char '-'
  ver  <- parse
668
  let name = intercalate "-" cs
669
670
671
672
673
674
675
676
  return $! f ver (lowercase name)
  where
    component = do
      cs <- Parse.munch1 Char.isAlphaNum
      if all Char.isDigit cs then Parse.pfail else return cs
      -- each component must contain an alphabetic character, to avoid
      -- ambiguity in identifiers like foo-1 (the 1 is the version number).

677
678
679
680
681
instance Text TestType where
  disp (TestTypeExe ver)          = text "exitcode-stdio-" <> disp ver
  disp (TestTypeLib ver)          = text "detailed-"       <> disp ver
  disp (TestTypeUnknown name ver) = text name <> char '-' <> disp ver

682
683
684
685
686
  parse = stdParse $ \ver name -> case name of
    "exitcode-stdio" -> TestTypeExe ver
    "detailed"       -> TestTypeLib ver
    _                -> TestTypeUnknown name ver

687
688
689
690
691
692

testType :: TestSuite -> TestType
testType test = case testInterface test of
  TestSuiteExeV10 ver _         -> TestTypeExe ver
  TestSuiteLibV09 ver _         -> TestTypeLib ver
  TestSuiteUnsupported testtype -> testtype
693

694
695
696
697
698
699
700
701
-- ---------------------------------------------------------------------------
-- The Benchmark type

-- | A \"benchmark\" stanza in a cabal file.
--
data Benchmark = Benchmark {
        benchmarkName      :: String,
        benchmarkInterface :: BenchmarkInterface,
702
703
704
        benchmarkBuildInfo :: BuildInfo,
        benchmarkEnabled   :: Bool
        -- TODO: See TODO for 'testEnabled'.
705
    }
ttuegel's avatar
ttuegel committed
706
707
708
    deriving (Generic, Show, Read, Eq, Typeable, Data)

instance Binary Benchmark
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729

-- | The benchmark interfaces that are currently defined. Each
-- benchmark must specify which interface it supports.
--
-- More interfaces may be defined in future, either new revisions or
-- totally new interfaces.
--
data BenchmarkInterface =

     -- | Benchmark interface \"exitcode-stdio-1.0\". The benchmark
     -- takes the form of an executable. It returns a zero exit code
     -- for success, non-zero for failure. The stdout and stderr
     -- channels may be logged. It takes no command line parameters
     -- and nothing on stdin.
     --
     BenchmarkExeV10 Version FilePath

     -- | A benchmark that does not conform to one of the above
     -- interfaces for the given reason (e.g. unknown benchmark type).
     --
   | BenchmarkUnsupported BenchmarkType
ttuegel's avatar
ttuegel committed
730
731
732
   deriving (Eq, Generic, Read, Show, Typeable, Data)

instance Binary BenchmarkInterface
733
734
735
736
737

instance Monoid Benchmark where
    mempty = Benchmark {
        benchmarkName      = mempty,
        benchmarkInterface = mempty,
738
739
        benchmarkBuildInfo = mempty,
        benchmarkEnabled   = False
740
    }
741
    mappend = (Semi.<>)
742

743
744
instance Semigroup Benchmark where
    a <> b = Benchmark {
745
746
        benchmarkName      = combine' benchmarkName,
        benchmarkInterface = combine  benchmarkInterface,
747
748
        benchmarkBuildInfo = combine  benchmarkBuildInfo,
        benchmarkEnabled   = benchmarkEnabled a || benchmarkEnabled b
749
750
751
752
753
754
755
756
757
758
    }
        where combine   field = field a `mappend` field b
              combine' f = case (f a, f b) of
                        ("", x) -> x
                        (x, "") -> x
                        (x, y) -> error "Ambiguous values for benchmark field: '"
                            ++ x ++ "' and '" ++ y ++ "'"

instance Monoid BenchmarkInterface where
    mempty  =  BenchmarkUnsupported (BenchmarkTypeUnknown mempty (Version [] []))
759
760
761
762
763
    mappend = (Semi.<>)

instance Semigroup BenchmarkInterface where
    a <> (BenchmarkUnsupported _) = a
    _ <> b                        = b
764
765
766
767

emptyBenchmark :: Benchmark
emptyBenchmark = mempty

768
769
770
771
-- | Does this package have any benchmarks?
hasBenchmarks :: PackageDescription -> Bool
hasBenchmarks = any (buildable . benchmarkBuildInfo) . benchmarks

772
773
774
775
776
777
778
779
780
-- | Get all the enabled benchmarks from a package.
enabledBenchmarks :: PackageDescription -> [Benchmark]
enabledBenchmarks = filter benchmarkEnabled . benchmarks

-- | Perform an action on each buildable 'Benchmark' in a package.
withBenchmark :: PackageDescription -> (Benchmark -> IO ()) -> IO ()
withBenchmark pkg_descr f =
    mapM_ f $ filter (buildable . benchmarkBuildInfo) $ enabledBenchmarks pkg_descr

781
782
783
784
785
786
787
788
789
790
-- | Get all the module names from a benchmark.
benchmarkModules :: Benchmark -> [ModuleName]
benchmarkModules benchmark = otherModules (benchmarkBuildInfo benchmark)

-- | The \"benchmark-type\" field in the benchmark stanza.
--
data BenchmarkType = BenchmarkTypeExe Version
                     -- ^ \"type: exitcode-stdio-x.y\"
                   | BenchmarkTypeUnknown String Version
                     -- ^ Some unknown benchmark type e.g. \"type: foo\"
ttuegel's avatar
ttuegel committed
791
792
793
    deriving (Generic, Show, Read, Eq, Typeable, Data)

instance Binary BenchmarkType
794
795
796
797
798
799
800
801

knownBenchmarkTypes :: [BenchmarkType]
knownBenchmarkTypes = [ BenchmarkTypeExe (Version [1,0] []) ]

instance Text BenchmarkType where
  disp (BenchmarkTypeExe ver)          = text "exitcode-stdio-" <> disp ver
  disp (BenchmarkTypeUnknown name ver) = text name <> char '-' <> disp ver

802
803
804
805
  parse = stdParse $ \ver name -> case name of
    "exitcode-stdio" -> BenchmarkTypeExe ver
    _                -> BenchmarkTypeUnknown name ver

806
807
808
809
810
811

benchmarkType :: Benchmark -> BenchmarkType
benchmarkType benchmark = case benchmarkInterface benchmark of
  BenchmarkExeV10 ver _              -> BenchmarkTypeExe ver
  BenchmarkUnsupported benchmarktype -> benchmarktype

812
813
814
815
816
817
818
-- ---------------------------------------------------------------------------
-- The BuildInfo type

-- Consider refactoring into executable and library versions.
data BuildInfo = BuildInfo {
        buildable         :: Bool,      -- ^ component is buildable here
        buildTools        :: [Dependency], -- ^ tools needed to build this bit
819
        cppOptions        :: [String],  -- ^ options for pre-processing Haskell code
820
821
822
823
        ccOptions         :: [String],  -- ^ options for C compiler
        ldOptions         :: [String],  -- ^ options for linker
        pkgconfigDepends  :: [Dependency], -- ^ pkg-config packages that are used
        frameworks        :: [String], -- ^support frameworks for Mac OS X
824
        extraFrameworkDirs:: [String], -- ^ extra locations to find frameworks.
825
        cSources          :: [FilePath],
826
        jsSources         :: [FilePath],
Ian D. Bollinger's avatar
Ian D. Bollinger committed
827
        hsSourceDirs      :: [FilePath], -- ^ where to look for the Haskell module hierarchy
828
        otherModules      :: [ModuleName], -- ^ non-exposed or non-main modules
829
830
831
832
833
834
835

        defaultLanguage   :: Maybe Language,-- ^ language used when not explicitly specified
        otherLanguages    :: [Language],    -- ^ other languages used within the package
        defaultExtensions :: [Extension],   -- ^ language extensions used by all modules
        otherExtensions   :: [Extension],   -- ^ other language extensions used within the package
        oldExtensions     :: [Extension],   -- ^ the old extensions field, treated same as 'defaultExtensions'

836
        extraLibs         :: [String], -- ^ what libraries to link with when compiling a program that uses your package
837
        extraGHCiLibs     :: [String], -- ^ if present, overrides extraLibs when package is loaded with GHCi.
838
839
840
        extraLibDirs      :: [String],
        includeDirs       :: [FilePath], -- ^directories to find .h files
        includes          :: [FilePath], -- ^ The .h files to be found in includeDirs
841
        installIncludes   :: [FilePath], -- ^ .h files to install with the package
842
        options           :: [(CompilerFlavor,[String])],
843
844
        profOptions       :: [(CompilerFlavor,[String])],
        sharedOptions     :: [(CompilerFlavor,[String])],
845
        customFieldsBI    :: [(String,String)], -- ^Custom fields starting
846
                                                -- with x-, stored in a
847
                                                -- simple assoc-list.
848
849
        targetBuildDepends :: [Dependency], -- ^ Dependencies specific to a library or executable target
        targetBuildRenaming :: Map PackageName ModuleRenaming
850
    }
ttuegel's avatar
ttuegel committed
851
852
853
    deriving (Generic, Show, Read, Eq, Typeable, Data)

instance Binary BuildInfo
854

855
instance Monoid BuildInfo where
856
  mempty = BuildInfo {
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
    buildable           = True,
    buildTools          = [],
    cppOptions          = [],
    ccOptions           = [],
    ldOptions           = [],
    pkgconfigDepends    = [],
    frameworks          = [],
    extraFrameworkDirs  = [],
    cSources            = [],
    jsSources           = [],
    hsSourceDirs        = [],
    otherModules        = [],
    defaultLanguage     = Nothing,
    otherLanguages      = [],
    defaultExtensions   = [],
    otherExtensions     = [],
    oldExtensions       = [],
    extraLibs           = [],
    extraGHCiLibs       = [],
    extraLibDirs        = [],
    includeDirs         = [],
    includes            = [],
    installIncludes     = [],
    options             = [],
    profOptions         = [],
    sharedOptions       = [],
    customFieldsBI      = [],
    targetBuildDepends  = [],
885
    targetBuildRenaming = Map.empty
886
  }
887
888
889
890
  mappend = (Semi.<>)

instance Semigroup BuildInfo where
  a <> b = BuildInfo {
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
    buildable           = buildable a && buildable b,
    buildTools          = combine    buildTools,
    cppOptions          = combine    cppOptions,
    ccOptions           = combine    ccOptions,
    ldOptions           = combine    ldOptions,
    pkgconfigDepends    = combine    pkgconfigDepends,
    frameworks          = combineNub frameworks,
    extraFrameworkDirs  = combineNub extraFrameworkDirs,
    cSources            = combineNub cSources,
    jsSources           = combineNub jsSources,
    hsSourceDirs        = combineNub hsSourceDirs,
    otherModules        = combineNub otherModules,
    defaultLanguage     = combineMby defaultLanguage,
    otherLanguages      = combineNub otherLanguages,
    defaultExtensions   = combineNub defaultExtensions,
    otherExtensions     = combineNub otherExtensions,
    oldExtensions       = combineNub oldExtensions,
    extraLibs           = combine    extraLibs,
    extraGHCiLibs       = combine    extraGHCiLibs,
    extraLibDirs        = combineNub extraLibDirs,
    includeDirs         = combineNub includeDirs,
    includes            = combineNub includes,
    installIncludes     = combineNub installIncludes,
    options             = combine    options,
    profOptions         = combine    profOptions,
    sharedOptions       = combine    sharedOptions,
    customFieldsBI      = combine    customFieldsBI,
    targetBuildDepends  = combineNub targetBuildDepends,
919
    targetBuildRenaming = combineMap targetBuildRenaming
920
921
922
923
  }
    where
      combine    field = field a `mappend` field b
      combineNub field = nub (combine field)
924
      combineMby field = field b `mplus` field a
925
      combineMap field = Map.unionWith mappend (field a) (field b)
926

927
emptyBuildInfo :: BuildInfo
928
emptyBuildInfo = mempty
929

930
-- | The 'BuildInfo' for the library (if there is one and it's buildable), and
931
932
-- all buildable executables, test suites and benchmarks.  Useful for gathering
-- dependencies.
933
allBuildInfo :: PackageDescription -> [BuildInfo]
934
allBuildInfo pkg_descr = [ bi | lib <- libraries pkg_descr
935
936
937
938
939
940
941
                              , let bi = libBuildInfo lib
                              , buildable bi ]
                      ++ [ bi | exe <- executables pkg_descr
                              , let bi = buildInfo exe
                              , buildable bi ]
                      ++ [ bi | tst <- testSuites pkg_descr
                              , let bi = testBuildInfo tst
942
943
                              , buildable bi
                              , testEnabled tst ]
944
945
                      ++ [ bi | tst <- benchmarks pkg_descr
                              , let bi = benchmarkBuildInfo tst
946
947
                              , buildable bi
                              , benchmarkEnabled tst ]
948
949
  --FIXME: many of the places where this is used, we actually want to look at
  --       unbuildable bits too, probably need separate functions
950

951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
-- | The 'Language's used by this component
--
allLanguages :: BuildInfo -> [Language]
allLanguages bi = maybeToList (defaultLanguage bi)
               ++ otherLanguages bi

-- | The 'Extension's that are used somewhere by this component
--
allExtensions :: BuildInfo -> [Extension]
allExtensions bi = usedExtensions bi
                ++ otherExtensions bi

-- | The 'Extensions' that are used by all modules in this component
--
usedExtensions :: BuildInfo -> [Extension]
usedExtensions bi = oldExtensions bi
                 ++ defaultExtensions bi

969
970
971
972
973
974
975
976
977
-- Libraries live in a separate namespace, so must distinguish
data ComponentName = CLibName   String
                   | CExeName   String
                   | CTestName  String
                   | CBenchName String
                   deriving (Eq, Generic, Ord, Read, Show)

instance Binary ComponentName

978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
-- Build-target-ish syntax
instance Text ComponentName where
    disp (CLibName str) = Disp.text ("lib:" ++ str)
    disp (CExeName str) = Disp.text ("exe:" ++ str)
    disp (CTestName str) = Disp.text ("test:" ++ str)
    disp (CBenchName str) = Disp.text ("bench:" ++ str)

    parse = do
        ctor <- Parse.choice [ Parse.string "lib:" >> return CLibName
                             , Parse.string "exe:" >> return CExeName
                             , Parse.string "bench:" >> return CBenchName
                             , Parse.string "test:" >> return CTestName ]
        -- For now, component names coincide with package name syntax
        -- (since they can show up in build-depends, which are parsed
        -- as package names.)
        fmap (ctor . unPackageName) parse

995
996
997
998
defaultLibName :: PackageIdentifier -> ComponentName
defaultLibName pid = CLibName (display (pkgName pid))

type HookedBuildInfo = [(ComponentName, BuildInfo)]
999
1000

emptyHookedBuildInfo :: HookedBuildInfo
1001
emptyHookedBuildInfo = []
1002

1003
1004
-- |Select options for a particular Haskell compiler.
hcOptions :: CompilerFlavor -> BuildInfo -> [String]
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
hcOptions = lookupHcOptions options

hcProfOptions :: CompilerFlavor -> BuildInfo -> [String]
hcProfOptions = lookupHcOptions profOptions

hcSharedOptions :: CompilerFlavor -> BuildInfo -> [String]
hcSharedOptions = lookupHcOptions sharedOptions

lookupHcOptions :: (BuildInfo -> [(CompilerFlavor,[String])])
                -> CompilerFlavor -> BuildInfo -> [String]
lookupHcOptions f hc bi = [ opt | (hc',opts) <- f bi
                          , hc' == hc
                          , opt <- opts ]
1018

1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
-- ------------------------------------------------------------
-- * Source repos
-- ------------------------------------------------------------

-- | Information about the source revision control system for a package.
--
-- When specifying a repo it is useful to know the meaning or intention of the
-- information as doing so enables automation. There are two obvious common
-- purposes: one is to find the repo for the latest development version, the
-- other is to find the repo for this specific release. The 'ReopKind'
-- specifies which one we mean (or another custom one).
--
-- A package can specify one or the other kind or both. Most will specify just
-- a head repo but some may want to specify a repo to reconstruct the sources
-- for this package release.
--
-- The required information is the 'RepoType' which tells us if it's using
-- 'Darcs', 'Git' for example. The 'repoLocation' and other details are
-- interpreted according to the repo type.
--
data SourceRepo = SourceRepo {
  -- | The kind of repo. This field is required.
  repoKind     :: RepoKind,

  -- | The type of the source repository system for this repo, eg 'Darcs' or
  -- 'Git'. This field is required.
  repoType     :: Maybe RepoType,

  -- | The location of the repository. For most 'RepoType's this is a URL.
  -- This field is required.
  repoLocation :: Maybe String,

  -- | 'CVS' can put multiple \"modules\" on one server and requires a
  -- module name in addition to the location to identify a particular repo.
  -- Logically this is part of the location but unfortunately has to be
  -- specified separately. This field is required for the 'CVS' 'RepoType' and
  -- should not be given otherwise.
  repoModule   :: Maybe String,

  -- | The name or identifier of the branch, if any. Many source control
  -- systems have the notion of multiple branches in a repo that exist in the
  -- same location. For example 'Git' and 'CVS' use this while systems like
  -- 'Darcs' use different locations for different branches. This field is
  -- optional but should be used if necessary to identify the sources,
  -- especially for the 'RepoThis' repo kind.
  repoBranch   :: Maybe String,

  -- | The tag identify a particular state of the repository. This should be
  -- given for the 'RepoThis' repo kind and not for 'RepoHead' kind.
  --
  repoTag      :: Maybe String,

  -- | Some repositories contain multiple projects in different subdirectories
  -- This field specifies the subdirectory where this packages sources can be
  -- found, eg the subdirectory containing the @.cabal@ file. It is interpreted
  -- relative to the root of the repository. This field is optional. If not
  -- given the default is \".\" ie no subdirectory.
  repoSubdir   :: Maybe FilePath
}
ttuegel's avatar
ttuegel committed
1078
1079
1080
  deriving (Eq, Generic, Read, Show, Typeable, Data)

instance Binary SourceRepo
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094

-- | What this repo info is for, what it represents.
--
data RepoKind =
    -- | The repository for the \"head\" or development version of the project.
    -- This repo is where we should track the latest development activity or
    -- the usual repo people should get to contribute patches.
    RepoHead

    -- | The repository containing the sources for this exact package version
    -- or release. For this kind of repo a tag should be given to give enough
    -- information to re-create the exact sources.
  | RepoThis

1095
  | RepoKindUnknown String
ttuegel's avatar
ttuegel committed
1096
1097
1098
  deriving (Eq, Generic, Ord, Read, Show, Typeable, Data)

instance Binary RepoKind
1099
1100
1101
1102
1103
1104
1105
1106

-- | An enumeration of common source control systems. The fields used in the
-- 'SourceRepo' depend on the type of repo. The tools and methods used to
-- obtain and track the repo depend on the repo type.
--
data RepoType = Darcs | Git | SVN | CVS
              | Mercurial | GnuArch | Bazaar | Monotone
              | OtherRepoType String
ttuegel's avatar
ttuegel committed
1107
1108
1109
  deriving (Eq, Generic, Ord, Read, Show, Typeable, Data)

instance Binary RepoType
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121

knownRepoTypes :: [RepoType]
knownRepoTypes = [Darcs, Git, SVN, CVS
                 ,Mercurial, GnuArch, Bazaar, Monotone]

repoTypeAliases :: RepoType -> [String]
repoTypeAliases Bazaar    = ["bzr"]
repoTypeAliases Mercurial = ["hg"]
repoTypeAliases GnuArch   = ["arch"]
repoTypeAliases _         = []

instance Text RepoKind where
1122
1123
1124
  disp RepoHead                = Disp.text "head"
  disp RepoThis                = Disp.text "this"
  disp (RepoKindUnknown other) = Disp.text other
1125
1126
1127
1128
1129
1130

  parse = do
    name <- ident
    return $ case lowercase name of
      "head" -> RepoHead
      "this" -> RepoThis
1131
      _      -> RepoKindUnknown name
1132
1133
1134
1135
1136
1137
1138
1139

instance Text RepoType where
  disp (OtherRepoType other) = Disp.text other
  disp other                 = Disp.text (lowercase (show other))
  parse = fmap classifyRepoType ident

classifyRepoType :: String -> RepoType
classifyRepoType s =
EyalLotem's avatar
EyalLotem committed
1140
  fromMaybe (OtherRepoType s) $ lookup (lowercase s) repoTypeMap
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
  where
    repoTypeMap = [ (name, repoType')
                  | repoType' <- knownRepoTypes
                  , name <- display repoType' : repoTypeAliases repoType' ]

ident :: Parse.ReadP r String
ident = Parse.munch1 (\c -> Char.isAlphaNum c || c == '_' || c == '-')

lowercase :: String -> String
lowercase = map Char.toLower

simonmar's avatar
simonmar committed
1152
1153
1154
1155
-- ------------------------------------------------------------
-- * Utils
-- ------------------------------------------------------------

1156
updatePackageDescription :: HookedBuildInfo -> PackageDescription -> PackageDescription
1157
1158
1159
1160
1161
1162
updatePackageDescription hooked_bis p
  = p{ executables = updateMany (CExeName . exeName)         updateExecutable (executables p)
     , libraries   = updateMany (CLibName . libName)         updateLibrary    (libraries   p)
     , benchmarks  = updateMany (CBenchName . benchmarkName) updateBenchmark  (benchmarks p)
     , testSuites  = updateMany (CTestName . testName)       updateTestSuite  (testSuites p)
     }
1163
    where
1164
1165
      updateMany :: (a -> ComponentName) -- ^ get 'ComponentName' from @a@
                 -> (BuildInfo -> a -> a) -- ^ @updateExecutable@, @updateLibrary@, etc
1166
1167
                 -> [a]          -- ^list of components to update
                 -> [a]          -- ^list with updated components
1168
      updateMany name update cs' = foldr (updateOne name update) cs' hooked_bis
1169

1170
1171
1172
      updateOne :: (a -> ComponentName) -- ^ get 'ComponentName' from @a@
                -> (BuildInfo -> a -> a) -- ^ @updateExecutable@, @updateLibrary@, etc
                -> (ComponentName, BuildInfo) -- ^(name, new buildinfo)
1173
1174
1175
1176
                -> [a]        -- ^list of components to update
                -> [a]        -- ^list with name component updated
      updateOne _ _ _                 []         = []
      updateOne name_sel update hooked_bi'@(name,bi) (c:cs)
1177
1178
1179
1180
        | name_sel c == name ||
          -- Special case: an empty name means "please update the BuildInfo for
          -- the public library, i.e. the one with the same name as the
          -- package."  See 'parseHookedBuildInfo'.
1181
          name == CLibName "" && name_sel c == defaultLibName (package p)
1182
          = update bi c : cs
1183
1184
1185
1186
        | otherwise          = c : updateOne name_sel update hooked_bi' cs

      updateExecutable bi exe = exe{buildInfo    = bi `mappend` buildInfo exe}
      updateLibrary    bi lib = lib{libBuildInfo = bi `mappend` libBuildInfo lib}
1187
1188
      updateBenchmark  bi ben = ben{benchmarkBuildInfo = bi `mappend` benchmarkBuildInfo ben}
      updateTestSuite  bi test = test{testBuildInfo = bi `mappend` testBuildInfo test}
1189

1190
1191
1192
1193
1194
1195
1196
-- ---------------------------------------------------------------------------
-- The GenericPackageDescription type

data GenericPackageDescription =
    GenericPackageDescription {
        packageDescription :: PackageDescription