4 import Distribution.PackageDescription
5 import Distribution.PackageDescription.Parse
6 import Distribution.ReadE
7 import Distribution.Simple
8 import Distribution.Simple.Configure
9 import Distribution.Simple.LocalBuildInfo
10 import Distribution.Simple.Program
11 import Distribution.Simple.Setup
12 import Distribution.Simple.Utils
13 import Distribution.Text
14 import Distribution.Verbosity
15 import System.Environment
21 "install" : ghcpkg : ghcpkgconf : destdir : topdir :
22 iprefix : ibindir : ilibdir : ilibexecdir : idynlibdir :
23 idatadir : idocdir : ihtmldir : ihaddockdir :
25 case parseArgs args' of
26 (verbosity, distPref, enableShellWrappers, strip) ->
27 doInstall verbosity distPref enableShellWrappers strip
28 ghcpkg ghcpkgconf destdir topdir
29 iprefix ibindir ilibdir ilibexecdir
30 idynlibdir idatadir idocdir ihtmldir
33 error ("Bad arguments: " ++ show args)
35 -- XXX We should really make Cabal do the hardwork here
37 -> (Verbosity, -- verbosity
38 FilePath, -- dist prefix
39 Bool, -- enable shell wrappers?
41 parseArgs = f normal defaultDistPref False True
42 where f _ dp esw strip (('-':'v':val):args)
43 = f (readEOrFail flagToVerbosity val) dp esw strip args
44 f v _ esw strip ("--distpref":dp:args) = f v dp esw strip args
45 f v dp _ strip ("--enable-shell-wrappers":args) = f v dp True strip args
46 f v dp esw _ ("--disable-executable-stripping":args) = f v dp esw False args
47 f v dp esw strip [] = (v, dp, esw, strip)
48 f _ _ _ _ args = error ("Bad arguments: " ++ show args)
50 doInstall :: Verbosity -> FilePath -> Bool -> Bool
51 -> FilePath -> FilePath -> FilePath -> FilePath
52 -> FilePath -> FilePath -> FilePath -> FilePath -> FilePath
53 -> FilePath -> FilePath -> FilePath -> FilePath
55 doInstall verbosity distPref enableShellWrappers strip
56 ghcpkg ghcpkgconf destdir topdir
57 iprefix ibindir ilibdir ilibexecdir idynlibdir idatadir
58 idocdir ihtmldir ihaddockdir =
59 do let userHooks = simpleUserHooks
60 copyto = if null destdir then NoCopyDest else CopyTo destdir
61 copyFlags = defaultCopyFlags {
62 copyUseWrapper = toFlag enableShellWrappers,
63 copyDest = toFlag copyto,
64 copyVerbosity = toFlag verbosity
66 registerFlags = defaultRegisterFlags {
67 regPackageDB = toFlag GlobalPackageDB,
68 regVerbosity = toFlag verbosity,
69 regGenScript = toFlag $ False,
70 regInPlace = toFlag $ False
72 lbi <- getConfig verbosity distPref
73 let pd = localPkgDescr lbi
74 i = installDirTemplates lbi
75 -- This is an almighty hack. We need to register
76 -- ghc-prim:GHC.Prim, but it doesn't exist, get built, get
77 -- haddocked, get copied, etc.
78 pd_reg = if packageName pd == PackageName "ghc-prim"
79 then case library pd of
81 let ems = fromJust (simpleParse "GHC.Prim")
83 lib' = lib { exposedModules = ems }
84 in pd { library = Just lib' }
86 error "Expected a library, but none found"
88 -- When coying, we need to actually give a concrete
89 -- directory to copy to rather than "$topdir"
90 toPathTemplate' = toPathTemplate . replaceTopdir topdir
91 i_copy = i { prefix = toPathTemplate' iprefix,
92 bindir = toPathTemplate' ibindir,
93 libdir = toPathTemplate' ilibdir,
94 dynlibdir = toPathTemplate' idynlibdir,
95 libexecdir = toPathTemplate' ilibexecdir,
96 datadir = toPathTemplate' idatadir,
97 docdir = toPathTemplate' idocdir,
98 htmldir = toPathTemplate' ihtmldir,
99 haddockdir = toPathTemplate' ihaddockdir
101 lbi_copy = lbi { installDirTemplates = i_copy,
103 -- When we run GHC we give it a $topdir that includes the
104 -- $compiler/lib/ part of libsubdir, so we only want the
105 -- $pkgid part in the package.conf file. This is a bit of
107 progs = withPrograms lbi
108 prog = ConfiguredProgram {
109 programId = programName ghcPkgProgram,
110 programVersion = Nothing,
111 programArgs = ["--force", "--global-conf", ghcpkgconf],
112 programLocation = UserSpecified ghcpkg
114 progs' = updateProgram prog progs
115 i_reg = i { prefix = toPathTemplate iprefix,
116 bindir = toPathTemplate ibindir,
117 libdir = toPathTemplate ilibdir,
118 dynlibdir = toPathTemplate idynlibdir,
119 libexecdir = toPathTemplate ilibexecdir,
120 datadir = toPathTemplate idatadir,
121 docdir = toPathTemplate idocdir,
122 htmldir = toPathTemplate ihtmldir,
123 haddockdir = toPathTemplate ihaddockdir
125 lbi_reg = lbi { installDirTemplates = i_reg,
126 withPrograms = progs' }
127 (copyHook simpleUserHooks) pd lbi_copy userHooks copyFlags
128 -- Cabal prints a scary "Package contains no library to register"
129 -- message if we call register but this is an executable package.
130 -- We therefore don't call it if we don't have a library for it.
131 when (isJust (library pd_reg)) $
132 (regHook simpleUserHooks) pd_reg lbi_reg userHooks registerFlags
135 replaceTopdir :: FilePath -> FilePath -> FilePath
136 replaceTopdir topdir ('$':'t':'o':'p':'d':'i':'r':p) = topdir ++ p
137 replaceTopdir topdir ('$':'h':'t':'t':'p':'t':'o':'p':'d':'i':'r':p)
139 replaceTopdir _ p = p
141 -- Get the build info, merging the setup-config and buildinfo files.
142 getConfig :: Verbosity -> FilePath -> IO LocalBuildInfo
143 getConfig verbosity distPref = do
144 lbi <- getPersistBuildConfig distPref
145 maybe_infoFile <- defaultHookedPackageDesc
146 case maybe_infoFile of
147 Nothing -> return lbi
149 hbi <- readHookedBuildInfo verbosity infoFile
150 return lbi { localPkgDescr = updatePackageDescription hbi (localPkgDescr lbi)}