Fix installing the libraries when there is no DESTDIR
[ghc-hetmet.git] / libraries / installPackage.hs
1
2 import Distribution.PackageDescription
3 import Distribution.Program
4 import Distribution.Setup
5 import Distribution.Simple
6 import Distribution.Simple.Configure
7 import Distribution.Simple.LocalBuildInfo
8 import Distribution.Verbosity
9 import System.Environment
10
11 main :: IO ()
12 main = do args <- getArgs
13           case args of
14               destdir : pref : ghcpkg : ghcpkgconf : args' ->
15                   let verbosity = case args' of
16                               [] -> normal
17                               ['-':'v':v] ->
18                                   let m = case v of
19                                               "" -> Nothing
20                                               _ -> Just v
21                                   in flagToVerbosity m
22                               _ -> error ("Bad arguments: " ++ show args)
23                   in doit destdir pref ghcpkg ghcpkgconf verbosity
24               _ ->
25                   error "Missing arguments"
26
27 doit :: FilePath -> FilePath -> FilePath -> FilePath -> Verbosity -> IO ()
28 doit destdir pref ghcpkg ghcpkgconf verbosity =
29        do let userHooks = simpleUserHooks
30               copyto = if null destdir then NoCopyDest else CopyTo destdir
31               copyFlags = (emptyCopyFlags copyto) {
32                               copyVerbose = verbosity
33                           }
34               registerFlags = emptyRegisterFlags {
35                                   regUser = MaybeUserGlobal,
36                                   regVerbose = verbosity,
37                                   regGenScript = False,
38                                   regInPlace = False
39                               }
40           lbi <- getPersistBuildConfig
41           let pd = localPkgDescr lbi
42               i = installDirTemplates lbi
43               -- XXX This is an almighty hack, shadowing the base
44               -- Setup.hs hack
45               mkLib filt = case library pd of
46                            Just lib ->
47                                let ems = filter filt $ exposedModules lib
48                                in lib {
49                                       exposedModules = ems
50                                    }
51                            Nothing ->
52                                error "Expected a library, but none found"
53               -- There's no files for GHC.Prim, so we will fail if we
54               -- try to copy them
55               pd_copy = pd { library = Just (mkLib ("GHC.Prim" /=)) }
56               pd_reg  = pd { library = Just (mkLib (const True)) }
57               -- When coying, we need to actually give a concrete
58               -- directory to copy to rather than "$topdir"
59               i_copy = i { prefixDirTemplate = toPathTemplate pref }
60               lbi_copy = lbi { installDirTemplates = i_copy }
61               -- When we run GHC we give it a $topdir that includes the
62               -- $compiler/lib/ part of libsubdir, so we only want the
63               -- $pkgid part in the package.conf file. This is a bit of
64               -- a hack, really.
65               progs = withPrograms lbi
66               prog = ConfiguredProgram {
67                          programId = programName ghcPkgProgram,
68                          programVersion = Nothing,
69                          programArgs = ["--global-conf", ghcpkgconf],
70                          programLocation = UserSpecified ghcpkg
71                      }
72               progs' = updateProgram prog progs
73               i_reg = i { libSubdirTemplate = toPathTemplate "$pkgid" }
74               lbi_reg = lbi { installDirTemplates = i_reg,
75                               withPrograms = progs' }
76           (copyHook simpleUserHooks) pd_copy lbi_copy userHooks copyFlags
77           (regHook simpleUserHooks)  pd_reg  lbi_reg  userHooks registerFlags
78           return ()
79