This goes with the patch for #1839, #1463
[ghc-hetmet.git] / libraries / installPackage.hs
1
2 import Distribution.PackageDescription
3 import Distribution.Simple
4 import Distribution.Simple.Configure
5 import Distribution.Simple.LocalBuildInfo
6 import Distribution.Simple.Program
7 import Distribution.Simple.Setup
8 import Distribution.Simple.Utils
9 import Distribution.Verbosity
10 import System.Environment
11
12 main :: IO ()
13 main
14   = do args <- getArgs
15        case args of
16            "register" : "--inplace" :args' ->
17                let verbosity = mkVerbosity args'
18                in doRegisterInplace verbosity
19            "install" : ghcpkg : ghcpkgconf : destdir : topdir :
20                     iprefix : ibindir : ilibdir : ilibexecdir : idynlibdir :
21                     idatadir : idocdir : ihtmldir : ihaddockdir :
22                     args' ->
23                let verbosity = mkVerbosity args'
24                in doInstall verbosity ghcpkg ghcpkgconf destdir topdir
25                             iprefix ibindir ilibdir ilibexecdir idynlibdir idatadir
26                             idocdir ihtmldir ihaddockdir
27            _ ->
28                error ("Bad arguments: " ++ show args)
29
30 mkVerbosity :: [String] -> Verbosity
31 mkVerbosity [] = normal
32 mkVerbosity ['-':'v':v] = let m = case v of
33                                       "" -> Nothing
34                                       _ -> Just v
35                           in flagToVerbosity m
36 mkVerbosity args = error ("Bad arguments: " ++ show args)
37
38 doRegisterInplace :: Verbosity -> IO ()
39 doRegisterInplace verbosity =
40        do lbi <- getConfig verbosity
41           let registerFlags = defaultRegisterFlags { regInPlace = toFlag True }
42               pd = localPkgDescr lbi
43               pd_reg = if pkgName (package pd) == "base"
44                        then case library pd of
45                             Just lib ->
46                                 let ems = "GHC.Prim" : exposedModules lib
47                                     lib' = lib { exposedModules = ems }
48                                 in pd { library = Just lib' }
49                             Nothing ->
50                                 error "Expected a library, but none found"
51                        else pd
52           (regHook simpleUserHooks) pd_reg lbi simpleUserHooks registerFlags
53           return ()
54
55 doInstall :: Verbosity -> FilePath -> FilePath -> FilePath -> FilePath
56           -> FilePath -> FilePath -> FilePath -> FilePath -> FilePath
57           -> FilePath -> FilePath -> FilePath -> FilePath
58           -> IO ()
59 doInstall verbosity ghcpkg ghcpkgconf destdir topdir
60      iprefix ibindir ilibdir ilibexecdir idynlibdir idatadir
61      idocdir ihtmldir ihaddockdir =
62        do let userHooks = simpleUserHooks
63               copyto = if null destdir then NoCopyDest else CopyTo destdir
64               copyFlags = defaultCopyFlags {
65                               copyDest = toFlag copyto,
66                               copyVerbose = toFlag verbosity
67                           }
68               registerFlags = defaultRegisterFlags {
69                                   regPackageDB = toFlag GlobalPackageDB,
70                                   regVerbose = toFlag verbosity,
71                                   regGenScript = toFlag $ False,
72                                   regInPlace = toFlag $ False
73                               }
74           lbi <- getConfig verbosity
75           let pd = localPkgDescr lbi
76               i = installDirTemplates lbi
77               -- This is an almighty hack. We need to register
78               -- base:GHC.Prim, but it doesn't exist, get built, get
79               -- haddocked, get copied, etc.
80               pd_reg = if pkgName (package pd) == "base"
81                        then case library pd of
82                             Just lib ->
83                                 let ems = "GHC.Prim" : exposedModules lib
84                                     lib' = lib { exposedModules = ems }
85                                 in pd { library = Just lib' }
86                             Nothing ->
87                                 error "Expected a library, but none found"
88                        else pd
89               -- When coying, we need to actually give a concrete
90               -- directory to copy to rather than "$topdir"
91               toPathTemplate' = toPathTemplate . replaceTopdir topdir
92               i_copy = i { prefix       = toPathTemplate' iprefix,
93                            bindir       = toPathTemplate' ibindir,
94                            libdir       = toPathTemplate' ilibdir,
95                            dynlibdir    = toPathTemplate' idynlibdir,
96                            libexecdir   = toPathTemplate' ilibexecdir,
97                            datadir      = toPathTemplate' idatadir,
98                            docdir       = toPathTemplate' idocdir,
99                            htmldir      = toPathTemplate' ihtmldir,
100                            haddockdir   = toPathTemplate' ihaddockdir
101                          }
102               lbi_copy = lbi { installDirTemplates = i_copy }
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           (regHook simpleUserHooks)  pd_reg lbi_reg  userHooks registerFlags
129           return ()
130
131 replaceTopdir :: FilePath -> FilePath -> FilePath
132 replaceTopdir topdir ('$':'t':'o':'p':'d':'i':'r':p) = topdir ++ p
133 replaceTopdir topdir ('$':'h':'t':'t':'p':'t':'o':'p':'d':'i':'r':p)
134     = topdir ++ p
135 replaceTopdir _ p = p
136
137 -- Get the build info, merging the setup-config and buildinfo files.
138 getConfig :: Verbosity -> IO LocalBuildInfo
139 getConfig verbosity = do
140     lbi <- getPersistBuildConfig
141     maybe_infoFile <- defaultHookedPackageDesc
142     case maybe_infoFile of
143         Nothing -> return lbi
144         Just infoFile -> do
145             hbi <- readHookedBuildInfo verbosity infoFile
146             return lbi { localPkgDescr = updatePackageDescription hbi (localPkgDescr lbi)}
147
148