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