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