Generalise Package Support
[ghc-hetmet.git] / compiler / main / PackageConfig.hs
1 --
2 -- (c) The University of Glasgow, 2004
3 --
4
5 module PackageConfig (
6         -- * PackageId
7         PackageId, 
8         mkPackageId, stringToPackageId, packageIdString, packageConfigId,
9         packageIdFS, fsToPackageId,  unpackPackageId,
10         
11         -- * The PackageConfig type: information about a package
12         PackageConfig,
13         InstalledPackageInfo(..), showPackageId,
14         Version(..),
15         PackageIdentifier(..),
16         defaultPackageConfig,
17
18         -- * Wired-in PackageIds
19         basePackageId,
20         rtsPackageId,
21         haskell98PackageId,
22         thPackageId,
23         mainPackageId
24   ) where
25
26 #include "HsVersions.h"
27
28 import Distribution.InstalledPackageInfo
29 import Distribution.Package
30 import Distribution.Version
31 import FastString
32 import Text.ParserCombinators.ReadP ( readP_to_S )
33
34 -- -----------------------------------------------------------------------------
35 -- Our PackageConfig type is just InstalledPackageInfo from Cabal.  Later we
36 -- might need to extend it with some GHC-specific stuff, but for now it's fine.
37
38 type PackageConfig = InstalledPackageInfo
39 defaultPackageConfig = emptyInstalledPackageInfo
40
41 -- -----------------------------------------------------------------------------
42 -- PackageId (package names with versions)
43
44 -- Mostly the compiler deals in terms of PackageNames, which don't
45 -- have the version suffix.  This is so that we don't need to know the
46 -- version for the -package-name flag, or know the versions of
47 -- wired-in packages like base & rts.  Versions are confined to the
48 -- package sub-system.
49 --
50 -- This means that in theory you could have multiple base packages installed
51 -- (for example), and switch between them using -package/-hide-package.
52 --
53 -- A PackageId is a string of the form <pkg>-<version>.
54
55 newtype PackageId = PId FastString deriving( Eq, Ord )  -- includes the version
56         -- easier not to use a newtype here, because we need instances of
57         -- Binary & Outputable, and we're too early to define them
58
59 fsToPackageId :: FastString -> PackageId
60 fsToPackageId = PId
61
62 packageIdFS :: PackageId -> FastString
63 packageIdFS (PId fs) = fs
64
65 stringToPackageId :: String -> PackageId
66 stringToPackageId = fsToPackageId . mkFastString
67
68 packageIdString :: PackageId -> String
69 packageIdString = unpackFS . packageIdFS
70
71 mkPackageId :: PackageIdentifier -> PackageId
72 mkPackageId = stringToPackageId . showPackageId
73
74 packageConfigId :: PackageConfig -> PackageId
75 packageConfigId = mkPackageId . package
76
77 unpackPackageId :: PackageId -> Maybe PackageIdentifier
78 unpackPackageId p
79   = case [ pid | (pid,"") <- readP_to_S parsePackageId str ] of
80         []      -> Nothing
81         (pid:_) -> Just pid
82   where str = packageIdString p
83
84 -- -----------------------------------------------------------------------------
85 -- Package Ids that are wired in
86
87 -- Certain packages are "known" to the compiler, in that we know about certain
88 -- entities that reside in these packages, and the compiler needs to 
89 -- declare static Modules and Names that refer to these packages.  Hence
90 -- the wired-in packages can't include version numbers, since we don't want
91 -- to bake the version numbers of these packages into GHC.
92 --
93 -- So here's the plan.  Wired-in packages are still versioned as
94 -- normal in the packages database, and you can still have multiple
95 -- versions of them installed.  However, for each invocation of GHC,
96 -- only a single instance of each wired-in package will be recognised
97 -- (the desired one is selected via -package/-hide-package), and GHC
98 -- will use the unversioned PackageId below when referring to it,
99 -- including in .hi files and object file symbols.  Unselected
100 -- versions of wired-in packages will be ignored, as will any other
101 -- package that depends directly or indirectly on it (much as if you
102 -- had used -ignore-package).
103
104 basePackageId      = fsToPackageId FSLIT("base")
105 rtsPackageId       = fsToPackageId FSLIT("rts")
106 haskell98PackageId = fsToPackageId FSLIT("haskell98")
107 thPackageId        = fsToPackageId FSLIT("template-haskell")
108
109 -- This is the package Id for the program.  It is the default package
110 -- Id if you don't specify a package name.  We don't add this prefix
111 -- to symbol name, since there can be only one main package per program.
112 mainPackageId      = fsToPackageId FSLIT("main")
113