Avoid the need to rerun configure when we install
[ghc-hetmet.git] / libraries / installPackage.hs
1
2 import Distribution.PackageDescription
3 import Distribution.Setup
4 import Distribution.Simple
5 import Distribution.Simple.Configure
6 import Distribution.Simple.LocalBuildInfo
7 import Distribution.Simple.Utils
8 import Distribution.Verbosity
9 import System.Environment
10
11 main :: IO ()
12 main = do args <- getArgs
13           let verbosity = case args of
14                               [] -> normal
15                               ['-':'v':v] ->
16                                   let m = case v of
17                                               "" -> Nothing
18                                               _ -> Just v
19                                   in flagToVerbosity m
20                               _ -> error ("Bad arguments: " ++ show args)
21               userHooks = simpleUserHooks
22               installFlags = InstallFlags {
23                                  installUserFlags = MaybeUserGlobal,
24                                  installVerbose = verbosity
25                              }
26           pdFile <- defaultPackageDesc verbosity
27           pd <- readPackageDescription verbosity pdFile
28           lbi <- getPersistBuildConfig
29           let -- XXX This is an almighty hack, shadowing the base Setup.hs hack
30               lib' = case library pd of
31                          Just lib ->
32                              lib {
33                                  exposedModules = filter (("GHC.Prim" /=))
34                                                 $ exposedModules lib
35                                  }
36                          Nothing ->
37                              error "Expected a library, but none found"
38               pd' = pd { library = Just lib' }
39               -- When installing we need to use the non-inplace ghc-pkg.
40               -- We also set the compiler to be non-inplace, but that
41               -- probably doesn't matter.
42               c = compiler lbi
43               c' = c { compilerPath = dropInPlace (compilerPath c),
44                        compilerPkgTool = dropInPlace (compilerPkgTool c)
45                      }
46               lbi' = lbi { compiler = c' }
47           (instHook simpleUserHooks) pd' lbi' userHooks installFlags
48
49 dropInPlace :: FilePath -> FilePath
50 dropInPlace "" = ""
51 dropInPlace xs@(x:xs') = case dropPrefix "-inplace" xs of
52                              Nothing -> x : dropInPlace xs'
53                              Just xs'' -> dropInPlace xs''
54
55 dropPrefix :: Eq a => [a] -> [a] -> Maybe [a]
56 dropPrefix [] ys = Just ys
57 dropPrefix (x:xs) (y:ys)
58  | x == y = dropPrefix xs ys
59 dropPrefix _ _ = Nothing
60