Move installPackage out into its own cabal package under utils/
[ghc.git] / utils / installPackage / installPackage.hs
1
2 import Data.Maybe
3 import Distribution.PackageDescription
4 import Distribution.PackageDescription.Parse
5 import Distribution.ReadE
6 import Distribution.Simple
7 import Distribution.Simple.Configure
8 import Distribution.Simple.LocalBuildInfo
9 import Distribution.Simple.Program
10 import Distribution.Simple.Setup
11 import Distribution.Simple.Utils
12 import Distribution.Text
13 import Distribution.Verbosity
14 import System.Environment
15
16 -- XXX This will need to be changed
17 distPref :: FilePath
18 distPref = defaultDistPref
19
20 main :: IO ()
21 main
22 = do args <- getArgs
23 case args of
24 "install" : ghcpkg : ghcpkgconf : destdir : topdir :
25 iprefix : ibindir : ilibdir : ilibexecdir : idynlibdir :
26 idatadir : idocdir : ihtmldir : ihaddockdir :
27 args' ->
28 let verbosity = mkVerbosity args'
29 in doInstall verbosity ghcpkg ghcpkgconf destdir topdir
30 iprefix ibindir ilibdir ilibexecdir idynlibdir idatadir
31 idocdir ihtmldir ihaddockdir
32 _ ->
33 error ("Bad arguments: " ++ show args)
34
35 mkVerbosity :: [String] -> Verbosity
36 mkVerbosity [] = normal
37 mkVerbosity ['-':'v':v] = readEOrFail flagToVerbosity v
38 mkVerbosity args = error ("Bad arguments: " ++ show args)
39
40 doInstall :: Verbosity -> FilePath -> FilePath -> FilePath -> FilePath
41 -> FilePath -> FilePath -> FilePath -> FilePath -> FilePath
42 -> FilePath -> FilePath -> FilePath -> FilePath
43 -> IO ()
44 doInstall verbosity ghcpkg ghcpkgconf destdir topdir
45 iprefix ibindir ilibdir ilibexecdir idynlibdir idatadir
46 idocdir ihtmldir ihaddockdir =
47 do let userHooks = simpleUserHooks
48 copyto = if null destdir then NoCopyDest else CopyTo destdir
49 copyFlags = defaultCopyFlags {
50 copyDest = toFlag copyto,
51 copyVerbosity = toFlag verbosity
52 }
53 registerFlags = defaultRegisterFlags {
54 regPackageDB = toFlag GlobalPackageDB,
55 regVerbosity = toFlag verbosity,
56 regGenScript = toFlag $ False,
57 regInPlace = toFlag $ False
58 }
59 lbi <- getConfig verbosity
60 let pd = localPkgDescr lbi
61 i = installDirTemplates lbi
62 -- This is an almighty hack. We need to register
63 -- ghc-prim:GHC.Prim, but it doesn't exist, get built, get
64 -- haddocked, get copied, etc.
65 pd_reg = if packageName pd == PackageName "ghc-prim"
66 then case library pd of
67 Just lib ->
68 let ems = fromJust (simpleParse "GHC.Prim")
69 : exposedModules lib
70 lib' = lib { exposedModules = ems }
71 in pd { library = Just lib' }
72 Nothing ->
73 error "Expected a library, but none found"
74 else pd
75 -- When coying, we need to actually give a concrete
76 -- directory to copy to rather than "$topdir"
77 toPathTemplate' = toPathTemplate . replaceTopdir topdir
78 i_copy = i { prefix = toPathTemplate' iprefix,
79 bindir = toPathTemplate' ibindir,
80 libdir = toPathTemplate' ilibdir,
81 dynlibdir = toPathTemplate' idynlibdir,
82 libexecdir = toPathTemplate' ilibexecdir,
83 datadir = toPathTemplate' idatadir,
84 docdir = toPathTemplate' idocdir,
85 htmldir = toPathTemplate' ihtmldir,
86 haddockdir = toPathTemplate' ihaddockdir
87 }
88 lbi_copy = lbi { installDirTemplates = i_copy }
89 -- When we run GHC we give it a $topdir that includes the
90 -- $compiler/lib/ part of libsubdir, so we only want the
91 -- $pkgid part in the package.conf file. This is a bit of
92 -- a hack, really.
93 progs = withPrograms lbi
94 prog = ConfiguredProgram {
95 programId = programName ghcPkgProgram,
96 programVersion = Nothing,
97 programArgs = ["--force", "--global-conf", ghcpkgconf],
98 programLocation = UserSpecified ghcpkg
99 }
100 progs' = updateProgram prog progs
101 i_reg = i { prefix = toPathTemplate iprefix,
102 bindir = toPathTemplate ibindir,
103 libdir = toPathTemplate ilibdir,
104 dynlibdir = toPathTemplate idynlibdir,
105 libexecdir = toPathTemplate ilibexecdir,
106 datadir = toPathTemplate idatadir,
107 docdir = toPathTemplate idocdir,
108 htmldir = toPathTemplate ihtmldir,
109 haddockdir = toPathTemplate ihaddockdir
110 }
111 lbi_reg = lbi { installDirTemplates = i_reg,
112 withPrograms = progs' }
113 (copyHook simpleUserHooks) pd lbi_copy userHooks copyFlags
114 (regHook simpleUserHooks) pd_reg lbi_reg userHooks registerFlags
115 return ()
116
117 replaceTopdir :: FilePath -> FilePath -> FilePath
118 replaceTopdir topdir ('$':'t':'o':'p':'d':'i':'r':p) = topdir ++ p
119 replaceTopdir topdir ('$':'h':'t':'t':'p':'t':'o':'p':'d':'i':'r':p)
120 = topdir ++ p
121 replaceTopdir _ p = p
122
123 -- Get the build info, merging the setup-config and buildinfo files.
124 getConfig :: Verbosity -> IO LocalBuildInfo
125 getConfig verbosity = do
126 lbi <- getPersistBuildConfig distPref
127 maybe_infoFile <- defaultHookedPackageDesc
128 case maybe_infoFile of
129 Nothing -> return lbi
130 Just infoFile -> do
131 hbi <- readHookedBuildInfo verbosity infoFile
132 return lbi { localPkgDescr = updatePackageDescription hbi (localPkgDescr lbi)}
133
134