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
16 "register" : "--inplace" :args' ->
17 let verbosity = mkVerbosity args'
18 in doRegisterInplace verbosity
19 "install" : ghcpkg : ghcpkgconf : destdir : topdir :
20 iprefix : ibindir : ilibdir : ilibexecdir : idynlibdir :
21 idatadir : idocdir : ihtmldir : ihaddockdir :
23 let verbosity = mkVerbosity args'
24 in doInstall verbosity ghcpkg ghcpkgconf destdir topdir
25 iprefix ibindir ilibdir ilibexecdir idynlibdir idatadir
26 idocdir ihtmldir ihaddockdir
28 error ("Bad arguments: " ++ show args)
30 mkVerbosity :: [String] -> Verbosity
31 mkVerbosity [] = normal
32 mkVerbosity ['-':'v':v] = let m = case v of
36 mkVerbosity args = error ("Bad arguments: " ++ show args)
38 doRegisterInplace :: Verbosity -> IO ()
39 doRegisterInplace verbosity =
40 do lbi <- getConfig verbosity
41 let registerFlags = defaultRegisterFlags { regInPlace = toFlag True }
42 pd = localPkgDescr lbi
43 pd_reg = if pkgName (package pd) == "ghc-prim"
44 then case library pd of
46 let ems = "GHC.Prim" : exposedModules lib
47 lib' = lib { exposedModules = ems }
48 in pd { library = Just lib' }
50 error "Expected a library, but none found"
52 (regHook simpleUserHooks) pd_reg lbi simpleUserHooks registerFlags
55 doInstall :: Verbosity -> FilePath -> FilePath -> FilePath -> FilePath
56 -> FilePath -> FilePath -> FilePath -> FilePath -> FilePath
57 -> FilePath -> FilePath -> FilePath -> FilePath
59 doInstall verbosity ghcpkg ghcpkgconf destdir topdir
60 iprefix ibindir ilibdir ilibexecdir idynlibdir idatadir
61 idocdir ihtmldir ihaddockdir =
62 do let userHooks = simpleUserHooks
63 copyto = if null destdir then NoCopyDest else CopyTo destdir
64 copyFlags = defaultCopyFlags {
65 copyDest = toFlag copyto,
66 copyVerbose = toFlag verbosity
68 registerFlags = defaultRegisterFlags {
69 regPackageDB = toFlag GlobalPackageDB,
70 regVerbose = toFlag verbosity,
71 regGenScript = toFlag $ False,
72 regInPlace = toFlag $ False
74 lbi <- getConfig verbosity
75 let pd = localPkgDescr lbi
76 i = installDirTemplates lbi
77 -- This is an almighty hack. We need to register
78 -- ghc-prim:GHC.Prim, but it doesn't exist, get built, get
79 -- haddocked, get copied, etc.
80 pd_reg = if pkgName (package pd) == "ghc-prim"
81 then case library pd of
83 let ems = "GHC.Prim" : exposedModules lib
84 lib' = lib { exposedModules = ems }
85 in pd { library = Just lib' }
87 error "Expected a library, but none found"
89 -- When coying, we need to actually give a concrete
90 -- directory to copy to rather than "$topdir"
91 toPathTemplate' = toPathTemplate . replaceTopdir topdir
92 i_copy = i { prefix = toPathTemplate' iprefix,
93 bindir = toPathTemplate' ibindir,
94 libdir = toPathTemplate' ilibdir,
95 dynlibdir = toPathTemplate' idynlibdir,
96 libexecdir = toPathTemplate' ilibexecdir,
97 datadir = toPathTemplate' idatadir,
98 docdir = toPathTemplate' idocdir,
99 htmldir = toPathTemplate' ihtmldir,
100 haddockdir = toPathTemplate' ihaddockdir
102 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 (regHook simpleUserHooks) pd_reg lbi_reg userHooks registerFlags
131 replaceTopdir :: FilePath -> FilePath -> FilePath
132 replaceTopdir topdir ('$':'t':'o':'p':'d':'i':'r':p) = topdir ++ p
133 replaceTopdir topdir ('$':'h':'t':'t':'p':'t':'o':'p':'d':'i':'r':p)
135 replaceTopdir _ p = p
137 -- Get the build info, merging the setup-config and buildinfo files.
138 getConfig :: Verbosity -> IO LocalBuildInfo
139 getConfig verbosity = do
140 lbi <- getPersistBuildConfig
141 maybe_infoFile <- defaultHookedPackageDesc
142 case maybe_infoFile of
143 Nothing -> return lbi
145 hbi <- readHookedBuildInfo verbosity infoFile
146 return lbi { localPkgDescr = updatePackageDescription hbi (localPkgDescr lbi)}