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