Follow Cabal changes in Setup.*hs
[packages/time.git] / Setup.hs
1 module Main (main) where
2
3 import Control.Exception
4 import Data.List
5 import Distribution.Simple
6 import Distribution.PackageDescription
7 import Distribution.Setup
8 import Distribution.Simple.LocalBuildInfo
9 import System.Cmd
10 import System.Directory
11 import System.Environment
12 import System.Info
13
14 main :: IO ()
15 main = do args <- getArgs
16 let (ghcArgs, args') = extractGhcArgs args
17 (_, args'') = extractConfigureArgs args'
18 hooks = defaultUserHooks {
19 confHook = add_Win32_dep
20 $ confHook defaultUserHooks,
21 buildHook = add_ghc_options ghcArgs
22 $ buildHook defaultUserHooks,
23 runTests = runTestScript }
24 withArgs args'' $ defaultMainWithHooks hooks
25
26 withCurrentDirectory :: FilePath -> IO a -> IO a
27 withCurrentDirectory path f = do
28 cur <- getCurrentDirectory
29 setCurrentDirectory path
30 finally f (setCurrentDirectory cur)
31
32 runTestScript :: Args -> Bool -> PackageDescription -> LocalBuildInfo
33 -> IO ()
34 runTestScript _args _flag _pd _lbi
35 = withCurrentDirectory "test" (system "make")
36
37 extractGhcArgs :: [String] -> ([String], [String])
38 extractGhcArgs = extractPrefixArgs "--ghc-option="
39
40 extractConfigureArgs :: [String] -> ([String], [String])
41 extractConfigureArgs = extractPrefixArgs "--configure-option="
42
43 extractPrefixArgs :: String -> [String] -> ([String], [String])
44 extractPrefixArgs the_prefix args
45 = let f [] = ([], [])
46 f (x:xs) = case f xs of
47 (wantedArgs, otherArgs) ->
48 case removePrefix the_prefix x of
49 Just wantedArg ->
50 (wantedArg:wantedArgs, otherArgs)
51 Nothing ->
52 (wantedArgs, x:otherArgs)
53 in f args
54
55 removePrefix :: String -> String -> Maybe String
56 removePrefix "" ys = Just ys
57 removePrefix _ "" = Nothing
58 removePrefix (x:xs) (y:ys)
59 | x == y = removePrefix xs ys
60 | otherwise = Nothing
61
62 type Hook a = PackageDescription -> LocalBuildInfo -> UserHooks -> a -> IO ()
63
64 add_ghc_options :: [String] -> Hook a -> Hook a
65 add_ghc_options args f pd lbi uhs x
66 = do let lib' = case library pd of
67 Just lib ->
68 let bi = libBuildInfo lib
69 opts = options bi ++ [(GHC, args)]
70 bi' = bi { options = opts }
71 in lib { libBuildInfo = bi' }
72 Nothing -> error "Expected a library"
73 pd' = pd { library = Just lib' }
74 f pd' lbi uhs x
75
76 type ConfHook = PackageDescription -> ConfigFlags -> IO LocalBuildInfo
77
78 -- XXX Hideous hack
79 add_Win32_dep :: ConfHook -> ConfHook
80 add_Win32_dep f pd cf
81 = do let pd' = if os == "mingw32"
82 then pd { buildDepends = Dependency "Win32" AnyVersion
83 : buildDepends pd }
84 else pd
85 f pd' cf
86