Make some utils -Wall clean
[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                               copyUseWrapper = toFlag enableShellWrappers,
63                               copyDest = toFlag copyto,
64                               copyVerbosity = toFlag verbosity
65                           }
66               registerFlags = defaultRegisterFlags {
67                                   regPackageDB = toFlag GlobalPackageDB,
68                                   regVerbosity = toFlag verbosity,
69                                   regGenScript = toFlag $ False,
70                                   regInPlace = toFlag $ False
71                               }
72           lbi <- getConfig verbosity distPref
73           let pd = localPkgDescr lbi
74               i = installDirTemplates lbi
75               -- This is an almighty hack. We need to register
76               -- ghc-prim:GHC.Prim, but it doesn't exist, get built, get
77               -- haddocked, get copied, etc.
78               pd_reg = if packageName pd == PackageName "ghc-prim"
79                        then case library pd of
80                             Just lib ->
81                                 let ems = fromJust (simpleParse "GHC.Prim")
82                                         : exposedModules lib
83                                     lib' = lib { exposedModules = ems }
84                                 in pd { library = Just lib' }
85                             Nothing ->
86                                 error "Expected a library, but none found"
87                        else pd
88               -- When coying, we need to actually give a concrete
89               -- directory to copy to rather than "$topdir"
90               toPathTemplate' = toPathTemplate . replaceTopdir topdir
91               i_copy = i { prefix       = toPathTemplate' iprefix,
92                            bindir       = toPathTemplate' ibindir,
93                            libdir       = toPathTemplate' ilibdir,
94                            dynlibdir    = toPathTemplate' idynlibdir,
95                            libexecdir   = toPathTemplate' ilibexecdir,
96                            datadir      = toPathTemplate' idatadir,
97                            docdir       = toPathTemplate' idocdir,
98                            htmldir      = toPathTemplate' ihtmldir,
99                            haddockdir   = toPathTemplate' ihaddockdir
100                          }
101               lbi_copy = lbi { installDirTemplates = i_copy,
102                                stripExes = strip }
103               -- When we run GHC we give it a $topdir that includes the
104               -- $compiler/lib/ part of libsubdir, so we only want the
105               -- $pkgid part in the package.conf file. This is a bit of
106               -- a hack, really.
107               progs = withPrograms lbi
108               prog = ConfiguredProgram {
109                          programId = programName ghcPkgProgram,
110                          programVersion = Nothing,
111                          programArgs = ["--force", "--global-conf", ghcpkgconf],
112                          programLocation = UserSpecified ghcpkg
113                      }
114               progs' = updateProgram prog progs
115               i_reg = i { prefix       = toPathTemplate iprefix,
116                           bindir       = toPathTemplate ibindir,
117                           libdir       = toPathTemplate ilibdir,
118                           dynlibdir    = toPathTemplate idynlibdir,
119                           libexecdir   = toPathTemplate ilibexecdir,
120                           datadir      = toPathTemplate idatadir,
121                           docdir       = toPathTemplate idocdir,
122                           htmldir      = toPathTemplate ihtmldir,
123                           haddockdir   = toPathTemplate ihaddockdir
124                         }
125               lbi_reg = lbi { installDirTemplates = i_reg,
126                               withPrograms = progs' }
127           (copyHook simpleUserHooks) pd     lbi_copy userHooks copyFlags
128           -- Cabal prints a scary "Package contains no library to register"
129           -- message if we call register but this is an executable package.
130           -- We therefore don't call it if we don't have a library for it.
131           when (isJust (library pd_reg)) $
132             (regHook simpleUserHooks)  pd_reg lbi_reg  userHooks registerFlags
133           return ()
134
135 replaceTopdir :: FilePath -> FilePath -> FilePath
136 replaceTopdir topdir ('$':'t':'o':'p':'d':'i':'r':p) = topdir ++ p
137 replaceTopdir topdir ('$':'h':'t':'t':'p':'t':'o':'p':'d':'i':'r':p)
138     = topdir ++ p
139 replaceTopdir _ p = p
140
141 -- Get the build info, merging the setup-config and buildinfo files.
142 getConfig :: Verbosity -> FilePath -> IO LocalBuildInfo
143 getConfig verbosity distPref = do
144     lbi <- getPersistBuildConfig distPref
145     maybe_infoFile <- defaultHookedPackageDesc
146     case maybe_infoFile of
147         Nothing -> return lbi
148         Just infoFile -> do
149             hbi <- readHookedBuildInfo verbosity infoFile
150             return lbi { localPkgDescr = updatePackageDescription hbi (localPkgDescr lbi)}
151
152