MERGED: Another attempt at getting bindists working everywhere
[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            ghcpkg : ghcpkgconf : destdir : topdir :
17                     iprefix : ibindir : ilibdir : ilibexecdir :
18                     idatadir : idocdir : ihtmldir :
19                     args' ->
20                let verbosity = case args' of
21                            [] -> normal
22                            ['-':'v':v] ->
23                                let m = case v of
24                                            "" -> Nothing
25                                            _ -> Just v
26                                in flagToVerbosity m
27                            _ -> error ("Bad arguments: " ++ show args)
28                in doit verbosity ghcpkg ghcpkgconf destdir topdir
29                        iprefix ibindir ilibdir ilibexecdir idatadir
30                        idocdir ihtmldir
31            _ ->
32                error "Missing arguments"
33
34 doit :: Verbosity -> FilePath -> FilePath -> FilePath -> FilePath
35      -> FilePath -> FilePath -> FilePath -> FilePath -> FilePath
36      -> FilePath -> FilePath
37      -> IO ()
38 doit verbosity ghcpkg ghcpkgconf destdir topdir
39      iprefix ibindir ilibdir ilibexecdir idatadir idocdir ihtmldir =
40        do let userHooks = simpleUserHooks
41               copyto = if null destdir then NoCopyDest else CopyTo destdir
42               copyFlags = (emptyCopyFlags copyto) {
43                               copyVerbose = verbosity
44                           }
45               registerFlags = emptyRegisterFlags {
46                                   regPackageDB = Just GlobalPackageDB,
47                                   regVerbose = verbosity,
48                                   regGenScript = False,
49                                   regInPlace = False
50                               }
51           lbi <- getConfig verbosity
52           let pd = localPkgDescr lbi
53               i = installDirTemplates lbi
54               -- XXX This is an almighty hack, shadowing the base
55               -- Setup.hs hack
56               mkLib filt = case library pd of
57                            Just lib ->
58                                let ems = filter filt $ exposedModules lib
59                                in lib {
60                                       exposedModules = ems
61                                    }
62                            Nothing ->
63                                error "Expected a library, but none found"
64               -- There's no files for GHC.Prim, so we will fail if we
65               -- try to copy them
66               pd_copy = pd { library = Just (mkLib ("GHC.Prim" /=)) }
67               pd_reg  = pd { library = Just (mkLib (const True)) }
68               -- When coying, we need to actually give a concrete
69               -- directory to copy to rather than "$topdir"
70               toPathTemplate' = toPathTemplate . replaceTopdir topdir
71               i_copy = i { prefixDirTemplate  = toPathTemplate' iprefix,
72                            binDirTemplate     = toPathTemplate' ibindir,
73                            libDirTemplate     = toPathTemplate' ilibdir,
74                            libexecDirTemplate = toPathTemplate' ilibexecdir,
75                            dataDirTemplate    = toPathTemplate' idatadir,
76                            docDirTemplate     = toPathTemplate' idocdir,
77                            htmlDirTemplate    = toPathTemplate' ihtmldir
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 iprefix,
93                           binDirTemplate     = toPathTemplate ibindir,
94                           libDirTemplate     = toPathTemplate ilibdir,
95                           libexecDirTemplate = toPathTemplate ilibexecdir,
96                           dataDirTemplate    = toPathTemplate idatadir,
97                           docDirTemplate     = toPathTemplate idocdir,
98                           htmlDirTemplate    = toPathTemplate ihtmldir
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 replaceTopdir :: FilePath -> FilePath -> FilePath
107 replaceTopdir topdir ('$':'t':'o':'p':'d':'i':'r':p) = topdir ++ p
108 replaceTopdir _ p = p
109
110 -- Get the build info, merging the setup-config and buildinfo files.
111 getConfig :: Verbosity -> IO LocalBuildInfo
112 getConfig verbosity = do
113     lbi <- getPersistBuildConfig
114     maybe_infoFile <- defaultHookedPackageDesc
115     case maybe_infoFile of
116         Nothing -> return lbi
117         Just infoFile -> do
118             hbi <- readHookedBuildInfo verbosity infoFile
119             return lbi { localPkgDescr = updatePackageDescription hbi (localPkgDescr lbi)}
120
121