Fix SPARC build, missing #include
[ghc-hetmet.git] / utils / installPackage / installPackage.hs
1
2 import Control.Monad
3 import Data.Maybe
4 import Distribution.PackageDescription
5 import Distribution.PackageDescription.Parse
6 import Distribution.ReadE
7 import Distribution.Simple
8 import Distribution.Simple.Configure
9 import Distribution.Simple.LocalBuildInfo
10 import Distribution.Simple.Program
11 import Distribution.Simple.Setup
12 import Distribution.Simple.Utils
13 import Distribution.Text
14 import Distribution.Verbosity
15 import System.Environment
16
17 main :: IO ()
18 main
19   = do args <- getArgs
20        case args of
21            "install" : ghcpkg : ghcpkgconf : destdir : topdir :
22                     iprefix : ibindir : ilibdir : ilibexecdir : idynlibdir :
23                     idatadir : idocdir : ihtmldir : ihaddockdir :
24                     args' ->
25                case parseArgs args' of
26                    (verbosity, distPref, enableShellWrappers, strip) ->
27                        doInstall verbosity distPref enableShellWrappers strip
28                                  ghcpkg ghcpkgconf destdir topdir
29                                  iprefix ibindir ilibdir ilibexecdir
30                                  idynlibdir idatadir idocdir ihtmldir
31                                  ihaddockdir
32            _ ->
33                error ("Bad arguments: " ++ show args)
34
35 -- XXX We should really make Cabal do the hardwork here
36 parseArgs :: [String]
37           -> (Verbosity, -- verbosity
38               FilePath,  -- dist prefix
39               Bool,      -- enable shell wrappers?
40               Bool)      -- strip exe?
41 parseArgs = f normal defaultDistPref False True
42     where f _ dp esw strip (('-':'v':val):args)
43               = f (readEOrFail flagToVerbosity val) dp esw strip args
44           f v _  esw strip ("--distpref":dp:args) = f v dp esw strip args
45           f v dp _   strip ("--enable-shell-wrappers":args) = f v dp True strip args
46           f v dp esw _     ("--disable-executable-stripping":args) = f v dp esw False args
47           f v dp esw strip [] = (v, dp, esw, strip)
48           f _ _  _   _     args = error ("Bad arguments: " ++ show args)
49
50 doInstall :: Verbosity -> FilePath -> Bool -> Bool
51           -> FilePath -> FilePath -> FilePath -> FilePath
52           -> FilePath -> FilePath -> FilePath -> FilePath -> FilePath
53           -> FilePath -> FilePath -> FilePath -> FilePath
54           -> IO ()
55 doInstall verbosity distPref enableShellWrappers strip
56      ghcpkg ghcpkgconf destdir topdir
57      iprefix ibindir ilibdir ilibexecdir idynlibdir idatadir
58      idocdir ihtmldir ihaddockdir =
59        do let userHooks = simpleUserHooks
60               copyto = if null destdir then NoCopyDest else CopyTo destdir
61               copyFlags = defaultCopyFlags {
62                               copyDistPref = toFlag distPref,
63                               copyUseWrapper = toFlag enableShellWrappers,
64                               copyDest = toFlag copyto,
65                               copyVerbosity = toFlag verbosity
66                           }
67               registerFlags = defaultRegisterFlags {
68                                   regDistPref = toFlag distPref,
69                                   regPackageDB = toFlag GlobalPackageDB,
70                                   regVerbosity = toFlag verbosity,
71                                   regGenScript = toFlag $ False,
72                                   regInPlace = toFlag $ False
73                               }
74           lbi <- getConfig verbosity distPref
75           let pd = localPkgDescr lbi
76               i = installDirTemplates lbi
77               -- This is an almighty hack. We need to register
78               -- ghc-prim:GHC.Prim, but it doesn't exist, get built, get
79               -- haddocked, get copied, etc.
80               pd_reg = if packageName pd == PackageName "ghc-prim"
81                        then case library pd of
82                             Just lib ->
83                                 let ems = fromJust (simpleParse "GHC.Prim")
84                                         : exposedModules lib
85                                     lib' = lib { exposedModules = ems }
86                                 in pd { library = Just lib' }
87                             Nothing ->
88                                 error "Expected a library, but none found"
89                        else pd
90               -- When coying, we need to actually give a concrete
91               -- directory to copy to rather than "$topdir"
92               toPathTemplate' = toPathTemplate . replaceTopdir topdir
93               i_copy = i { prefix       = toPathTemplate' iprefix,
94                            bindir       = toPathTemplate' ibindir,
95                            libdir       = toPathTemplate' ilibdir,
96                            dynlibdir    = toPathTemplate' idynlibdir,
97                            libexecdir   = toPathTemplate' ilibexecdir,
98                            datadir      = toPathTemplate' idatadir,
99                            docdir       = toPathTemplate' idocdir,
100                            htmldir      = toPathTemplate' ihtmldir,
101                            haddockdir   = toPathTemplate' ihaddockdir
102                          }
103               lbi_copy = lbi { installDirTemplates = i_copy,
104                                stripExes = strip }
105               -- When we run GHC we give it a $topdir that includes the
106               -- $compiler/lib/ part of libsubdir, so we only want the
107               -- $pkgid part in the package.conf file. This is a bit of
108               -- a hack, really.
109               progs = withPrograms lbi
110               prog = ConfiguredProgram {
111                          programId = programName ghcPkgProgram,
112                          programVersion = Nothing,
113                          programArgs = ["--force", "--global-conf", ghcpkgconf],
114                          programLocation = UserSpecified ghcpkg
115                      }
116               progs' = updateProgram prog progs
117               i_reg = i { prefix       = toPathTemplate iprefix,
118                           bindir       = toPathTemplate ibindir,
119                           libdir       = toPathTemplate ilibdir,
120                           dynlibdir    = toPathTemplate idynlibdir,
121                           libexecdir   = toPathTemplate ilibexecdir,
122                           datadir      = toPathTemplate idatadir,
123                           docdir       = toPathTemplate idocdir,
124                           htmldir      = toPathTemplate ihtmldir,
125                           haddockdir   = toPathTemplate ihaddockdir
126                         }
127               lbi_reg = lbi { installDirTemplates = i_reg,
128                               withPrograms = progs' }
129           (copyHook simpleUserHooks) pd     lbi_copy userHooks copyFlags
130           -- Cabal prints a scary "Package contains no library to register"
131           -- message if we call register but this is an executable package.
132           -- We therefore don't call it if we don't have a library for it.
133           when (isJust (library pd_reg)) $
134             (regHook simpleUserHooks)  pd_reg lbi_reg  userHooks registerFlags
135           return ()
136
137 replaceTopdir :: FilePath -> FilePath -> FilePath
138 replaceTopdir topdir ('$':'t':'o':'p':'d':'i':'r':p) = topdir ++ p
139 replaceTopdir topdir ('$':'h':'t':'t':'p':'t':'o':'p':'d':'i':'r':p)
140     = topdir ++ p
141 replaceTopdir _ p = p
142
143 -- Get the build info, merging the setup-config and buildinfo files.
144 getConfig :: Verbosity -> FilePath -> IO LocalBuildInfo
145 getConfig verbosity distPref = do
146     lbi <- getPersistBuildConfig distPref
147     maybe_infoFile <- defaultHookedPackageDesc
148     case maybe_infoFile of
149         Nothing -> return lbi
150         Just infoFile -> do
151             hbi <- readHookedBuildInfo verbosity infoFile
152             return lbi { localPkgDescr = updatePackageDescription hbi (localPkgDescr lbi)}
153
154