Fix parallel make for libffi.
[ghc-hetmet.git] / libraries / cabal-bin.hs
1
2 module Main (main) where
3
4 import Control.Monad
5 import Data.Maybe
6 import Distribution.PackageDescription
7 import Distribution.PackageDescription.Parse
8 import Distribution.Simple
9 import Distribution.Simple.Utils
10 import Distribution.Verbosity
11 import System.Directory
12 import System.Environment
13 import System.FilePath
14
15 import qualified Distribution.Make   as Make
16 import qualified Distribution.Simple as Simple
17
18 setupProg :: FilePath
19 setupProg = "./Setup"
20
21 main :: IO ()
22 main = do
23     unprocessedArgs <- getArgs
24     let verbosity = verbose
25     case unprocessedArgs of
26         ghc : packageConf : useCabalVersion : args ->
27             doit verbosity ghc packageConf useCabalVersion args
28         _ -> die "Bad args"
29
30 doit :: Verbosity -> FilePath -> FilePath -> String -> [String] -> IO ()
31 doit verbosity ghc packageConf useCabalVersion args = do
32     exists <- doesFileExist setupProg
33     if exists then rawSystemExit verbosity setupProg args
34               else do
35         gpdFile <- defaultPackageDesc verbosity
36         gpd <- readPackageDescription verbosity gpdFile
37         let pd = packageDescription gpd
38         case buildType pd of
39             Just Simple    -> Simple.defaultMainArgs                     args
40             Just Make      -> Make.defaultMainArgs                       args
41             Just Configure -> defaultMainWithHooksArgs autoconfUserHooks args
42             _ | packageName pd == PackageName "Cabal" ->
43                               -- Cabal is special...*sigh*
44                               Simple.defaultMainArgs                     args
45               | otherwise  ->
46                 runSetup verbosity ghc packageConf useCabalVersion args
47
48 runSetup :: Verbosity -> FilePath -> FilePath -> String -> [String] -> IO ()
49 runSetup verbosity ghc packageConf useCabalVersion args = do
50     -- Don't bother building Setup if we are cleaning. If we need to
51     -- build Setup in order to build, and Setup isn't built already,
52     -- then there shouldn't be anything to clean anyway.
53     unless cleaning $
54         rawSystemExit verbosity ghc ["-package-conf", packageConf,
55                                      "--make", "Setup",
56                                      "-package", "Cabal-" ++ useCabalVersion,
57                                      "-o", "Setup"]
58     rawSystemExit verbosity "./Setup" args
59   where cleaning = case args of
60                    "clean" : _ -> True
61                    _ -> False
62