Hack due to time needing Win32 on Windows
[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.Exit
13 import System.Info
14
15 main :: IO ()
16 main = do args <- getArgs
17 let (ghcArgs, args') = extractGhcArgs args
18 (_, args'') = extractConfigureArgs args'
19 hooks = defaultUserHooks {
20 confHook = add_Win32_dep
21 $ confHook defaultUserHooks,
22 buildHook = add_ghc_options ghcArgs
23 $ buildHook defaultUserHooks,
24 runTests = runTestScript }
25 withArgs args'' $ defaultMainWithHooks hooks
26
27 withCurrentDirectory :: FilePath -> IO a -> IO a
28 withCurrentDirectory path f = do
29 cur <- getCurrentDirectory
30 setCurrentDirectory path
31 finally f (setCurrentDirectory cur)
32
33 runTestScript :: Args -> Bool -> PackageDescription -> LocalBuildInfo
34 -> IO ExitCode
35 runTestScript _args _flag _pd _lbi
36 = withCurrentDirectory "test" (system "make")
37
38 extractGhcArgs :: [String] -> ([String], [String])
39 extractGhcArgs = extractPrefixArgs "--ghc-option="
40
41 extractConfigureArgs :: [String] -> ([String], [String])
42 extractConfigureArgs = extractPrefixArgs "--configure-option="
43
44 extractPrefixArgs :: String -> [String] -> ([String], [String])
45 extractPrefixArgs the_prefix args
46 = let f [] = ([], [])
47 f (x:xs) = case f xs of
48 (wantedArgs, otherArgs) ->
49 case removePrefix the_prefix x of
50 Just wantedArg ->
51 (wantedArg:wantedArgs, otherArgs)
52 Nothing ->
53 (wantedArgs, x:otherArgs)
54 in f args
55
56 removePrefix :: String -> String -> Maybe String
57 removePrefix "" ys = Just ys
58 removePrefix _ "" = Nothing
59 removePrefix (x:xs) (y:ys)
60 | x == y = removePrefix xs ys
61 | otherwise = Nothing
62
63 type Hook a = PackageDescription -> LocalBuildInfo -> Maybe UserHooks -> a
64 -> IO ()
65
66 add_ghc_options :: [String] -> Hook a -> Hook a
67 add_ghc_options args f pd lbi muhs x
68 = do let lib' = case library pd of
69 Just lib ->
70 let bi = libBuildInfo lib
71 opts = options bi ++ [(GHC, args)]
72 bi' = bi { options = opts }
73 in lib { libBuildInfo = bi' }
74 Nothing -> error "Expected a library"
75 pd' = pd { library = Just lib' }
76 f pd' lbi muhs x
77
78 type ConfHook = PackageDescription -> ConfigFlags -> IO LocalBuildInfo
79
80 -- XXX Hideous hack
81 add_Win32_dep :: ConfHook -> ConfHook
82 add_Win32_dep f pd cf
83 = do let pd' = if os == "mingw32"
84 then pd { buildDepends = Dependency "Win32" AnyVersion
85 : buildDepends pd }
86 else pd
87 f pd' cf
88