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