Yet another attempt to get the paths for the installed documentation correct
[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            destdir : pref : idatadir : idocdir : ihtmldir : ghcpkg : ghcpkgconf : args' ->
17                let verbosity = case args' of
18                            [] -> normal
19                            ['-':'v':v] ->
20                                let m = case v of
21                                            "" -> Nothing
22                                            _ -> Just v
23                                in flagToVerbosity m
24                            _ -> error ("Bad arguments: " ++ show args)
25                in doit destdir pref idatadir idocdir ihtmldir ghcpkg ghcpkgconf
26                        verbosity
27            _ ->
28                error "Missing arguments"
29
30 doit :: FilePath -> FilePath -> FilePath -> FilePath -> FilePath -> FilePath -> FilePath
31      -> Verbosity
32      -> IO ()
33 doit destdir pref idatadir idocdir ihtmldir ghcpkg ghcpkgconf verbosity =
34        do let userHooks = simpleUserHooks
35               copyto = if null destdir then NoCopyDest else CopyTo destdir
36               copyFlags = (emptyCopyFlags copyto) {
37                               copyVerbose = verbosity
38                           }
39               registerFlags = emptyRegisterFlags {
40                                   regPackageDB = Just GlobalPackageDB,
41                                   regVerbose = verbosity,
42                                   regGenScript = False,
43                                   regInPlace = False
44                               }
45           lbi <- getConfig verbosity
46           let pd = localPkgDescr lbi
47               i = installDirTemplates lbi
48               -- XXX This is an almighty hack, shadowing the base
49               -- Setup.hs hack
50               mkLib filt = case library pd of
51                            Just lib ->
52                                let ems = filter filt $ exposedModules lib
53                                in lib {
54                                       exposedModules = ems
55                                    }
56                            Nothing ->
57                                error "Expected a library, but none found"
58               -- There's no files for GHC.Prim, so we will fail if we
59               -- try to copy them
60               pd_copy = pd { library = Just (mkLib ("GHC.Prim" /=)) }
61               pd_reg  = pd { library = Just (mkLib (const True)) }
62               -- When coying, we need to actually give a concrete
63               -- directory to copy to rather than "$topdir"
64               i_copy = i { prefixDirTemplate = toPathTemplate pref,
65                            dataDirTemplate   = toPathTemplate idatadir,
66                            docDirTemplate    = toPathTemplate idocdir,
67                            htmlDirTemplate   = toPathTemplate ihtmldir
68                          }
69               lbi_copy = lbi { installDirTemplates = i_copy }
70               -- When we run GHC we give it a $topdir that includes the
71               -- $compiler/lib/ part of libsubdir, so we only want the
72               -- $pkgid part in the package.conf file. This is a bit of
73               -- a hack, really.
74               progs = withPrograms lbi
75               prog = ConfiguredProgram {
76                          programId = programName ghcPkgProgram,
77                          programVersion = Nothing,
78                          programArgs = ["--global-conf", ghcpkgconf],
79                          programLocation = UserSpecified ghcpkg
80                      }
81               progs' = updateProgram prog progs
82               i_reg = i { libSubdirTemplate = toPathTemplate "$pkgid" }
83               lbi_reg = lbi { installDirTemplates = i_reg,
84                               withPrograms = progs' }
85           (copyHook simpleUserHooks) pd_copy lbi_copy userHooks copyFlags
86           (regHook simpleUserHooks)  pd_reg  lbi_reg  userHooks registerFlags
87           return ()
88
89 -- Get the build info, merging the setup-config and buildinfo files.
90 getConfig :: Verbosity -> IO LocalBuildInfo
91 getConfig verbosity = do
92     lbi <- getPersistBuildConfig
93     maybe_infoFile <- defaultHookedPackageDesc
94     case maybe_infoFile of
95         Nothing -> return lbi
96         Just infoFile -> do
97             hbi <- readHookedBuildInfo verbosity infoFile
98             return lbi { localPkgDescr = updatePackageDescription hbi (localPkgDescr lbi)}
99
100