Updates to work with latest cabal.
[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 : 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 ghcpkg ghcpkgconf
26                        verbosity
27            _ ->
28                error "Missing arguments"
29
30 doit :: FilePath -> FilePath -> FilePath -> FilePath -> FilePath -> FilePath
31      -> Verbosity
32      -> IO ()
33 doit destdir pref idatadir idocdir 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                          }
68               lbi_copy = lbi { installDirTemplates = i_copy }
69               -- When we run GHC we give it a $topdir that includes the
70               -- $compiler/lib/ part of libsubdir, so we only want the
71               -- $pkgid part in the package.conf file. This is a bit of
72               -- a hack, really.
73               progs = withPrograms lbi
74               prog = ConfiguredProgram {
75                          programId = programName ghcPkgProgram,
76                          programVersion = Nothing,
77                          programArgs = ["--global-conf", ghcpkgconf],
78                          programLocation = UserSpecified ghcpkg
79                      }
80               progs' = updateProgram prog progs
81               i_reg = i { libSubdirTemplate = toPathTemplate "$pkgid" }
82               lbi_reg = lbi { installDirTemplates = i_reg,
83                               withPrograms = progs' }
84           (copyHook simpleUserHooks) pd_copy lbi_copy userHooks copyFlags
85           (regHook simpleUserHooks)  pd_reg  lbi_reg  userHooks registerFlags
86           return ()
87
88 -- Get the build info, merging the setup-config and buildinfo files.
89 getConfig :: Verbosity -> IO LocalBuildInfo
90 getConfig verbosity = do
91     lbi <- getPersistBuildConfig
92     maybe_infoFile <- defaultHookedPackageDesc
93     case maybe_infoFile of
94         Nothing -> return lbi
95         Just infoFile -> do
96             hbi <- readHookedBuildInfo verbosity infoFile
97             return lbi { localPkgDescr = updatePackageDescription hbi (localPkgDescr lbi)}
98
99