ffd5d380916bcd63a01fdd7fb1f82ea2fd5ccb4b
[hadrian.git] / src / Hadrian / Package.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module : Hadrian.Package
4 -- Copyright : (c) Andrey Mokhov 2014-2017
5 -- License : MIT (see the file LICENSE)
6 -- Maintainer : andrey.mokhov@gmail.com
7 -- Stability : experimental
8 --
9 -- A /package/ is a collection of files. We currently only support C and Haskell
10 -- packages and treat a package as either a library or a program. The latter is
11 -- a gross oversimplification as, for example, Haskell packages can be both.
12 -- This works for now, but should be improved in future.
13 -----------------------------------------------------------------------------
14 module Hadrian.Package (
15 -- * Data types
16 Package (..), PackageName, PackageLanguage, PackageType,
17
18 -- * Construction and properties
19 cLibrary, cProgram, hsLibrary, hsProgram,
20 isLibrary, isProgram, isCPackage, isHsPackage,
21
22 -- * Package directory structure
23 pkgCabalFile, unsafePkgCabalFile
24 ) where
25
26 import Data.Maybe
27 import Development.Shake.FilePath
28 import GHC.Stack
29 import Hadrian.Package.Type
30 import Hadrian.Utilities
31
32 -- | Construct a C library package.
33 cLibrary :: PackageName -> FilePath -> Package
34 cLibrary = Package C Library
35
36 -- | Construct a C program package.
37 cProgram :: PackageName -> FilePath -> Package
38 cProgram = Package C Program
39
40 -- | Construct a Haskell library package.
41 hsLibrary :: PackageName -> FilePath -> Package
42 hsLibrary = Package Haskell Library
43
44 -- | Construct a Haskell program package.
45 hsProgram :: PackageName -> FilePath -> Package
46 hsProgram = Package Haskell Program
47
48 -- | Is this a library package?
49 isLibrary :: Package -> Bool
50 isLibrary (Package _ Library _ _) = True
51 isLibrary _ = False
52
53 -- | Is this a program package?
54 isProgram :: Package -> Bool
55 isProgram (Package _ Program _ _) = True
56 isProgram _ = False
57
58 -- | Is this a C package?
59 isCPackage :: Package -> Bool
60 isCPackage (Package C _ _ _) = True
61 isCPackage _ = False
62
63 -- | Is this a Haskell package?
64 isHsPackage :: Package -> Bool
65 isHsPackage (Package Haskell _ _ _) = True
66 isHsPackage _ = False
67
68 -- | The path to the Cabal file of a Haskell package, e.g. @ghc/ghc-bin.cabal@,
69 -- or @Nothing@ if the argument is not a Haskell package.
70 pkgCabalFile :: Package -> Maybe FilePath
71 pkgCabalFile p | isHsPackage p = Just $ pkgPath p -/- pkgName p <.> "cabal"
72 | otherwise = Nothing
73
74 -- | Like 'pkgCabalFile' but raises an error on a non-Haskell package.
75 unsafePkgCabalFile :: HasCallStack => Package -> FilePath
76 unsafePkgCabalFile p = fromMaybe (error msg) (pkgCabalFile p)
77 where
78 msg = "[unsafePkgCabalFile] Not a Haskell package: " ++ show p