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