Obey DESTDIR when installing library docs
[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               copyFlags = (emptyCopyFlags (CopyTo destdir)) {
31                               copyVerbose = verbosity
32                           }
33               registerFlags = emptyRegisterFlags {
34                                   regUser = MaybeUserGlobal,
35                                   regVerbose = verbosity,
36                                   regGenScript = False,
37                                   regInPlace = False
38                               }
39           lbi <- getPersistBuildConfig
40           let pd = localPkgDescr lbi
41               i = installDirTemplates lbi
42               -- XXX This is an almighty hack, shadowing the base
43               -- Setup.hs hack
44               mkLib filt = case library pd of
45                            Just lib ->
46                                let ems = filter filt $ exposedModules lib
47                                in lib {
48                                       exposedModules = ems
49                                    }
50                            Nothing ->
51                                error "Expected a library, but none found"
52               -- There's no files for GHC.Prim, so we will fail if we
53               -- try to copy them
54               pd_copy = pd { library = Just (mkLib ("GHC.Prim" /=)) }
55               pd_reg  = pd { library = Just (mkLib (const True)) }
56               -- When coying, we need to actually give a concrete
57               -- directory to copy to rather than "$topdir"
58               i_copy = i { prefixDirTemplate = toPathTemplate pref }
59               lbi_copy = lbi { installDirTemplates = i_copy }
60               -- When we run GHC we give it a $topdir that includes the
61               -- $compiler/lib/ part of libsubdir, so we only want the
62               -- $pkgid part in the package.conf file. This is a bit of
63               -- a hack, really.
64               progs = withPrograms lbi
65               prog = ConfiguredProgram {
66                          programId = programName ghcPkgProgram,
67                          programVersion = Nothing,
68                          programArgs = ["--global-conf", ghcpkgconf],
69                          programLocation = UserSpecified ghcpkg
70                      }
71               progs' = updateProgram prog progs
72               i_reg = i { libSubdirTemplate = toPathTemplate "$pkgid" }
73               lbi_reg = lbi { installDirTemplates = i_reg,
74                               withPrograms = progs' }
75           (copyHook simpleUserHooks) pd_copy lbi_copy userHooks copyFlags
76           (regHook simpleUserHooks)  pd_reg  lbi_reg  userHooks registerFlags
77           return ()
78