parse (but don't pass on) options for ./configure
[packages/template-haskell.git] / Setup.hs
1
2 module Main (main) where
3
4 import Data.List
5 import Distribution.Simple
6 import Distribution.PackageDescription
7 import Distribution.PreProcess
8 import Distribution.Setup
9 import Distribution.Simple.LocalBuildInfo
10 import System.Environment
11
12 main :: IO ()
13 main = do args <- getArgs
14 let (ghcArgs, args') = extractGhcArgs args
15 (_, args'') = extractConfigureArgs args'
16 hooks = defaultUserHooks {
17 buildHook = add_ghc_options ghcArgs
18 $ buildHook defaultUserHooks }
19 withArgs args'' $ defaultMainWithHooks hooks
20
21 extractGhcArgs :: [String] -> ([String], [String])
22 extractGhcArgs = extractPrefixArgs "--ghc-option="
23
24 extractConfigureArgs :: [String] -> ([String], [String])
25 extractConfigureArgs = extractPrefixArgs "--configure-option="
26
27 extractPrefixArgs :: String -> [String] -> ([String], [String])
28 extractPrefixArgs prefix args
29 = let f [] = ([], [])
30 f (x:xs) = case f xs of
31 (wantedArgs, otherArgs) ->
32 case removePrefix prefix x of
33 Just wantedArg ->
34 (wantedArg:wantedArgs, otherArgs)
35 Nothing ->
36 (wantedArgs, x:otherArgs)
37 in f args
38
39 removePrefix :: String -> String -> Maybe String
40 removePrefix "" ys = Just ys
41 removePrefix (x:xs) (y:ys)
42 | x == y = removePrefix xs ys
43 | otherwise = Nothing
44
45 type Hook a = PackageDescription -> LocalBuildInfo -> Maybe UserHooks -> a
46 -> IO ()
47
48 add_ghc_options :: [String] -> Hook a -> Hook a
49 add_ghc_options args f pd lbi muhs x
50 = do let lib' = case library pd of
51 Just lib ->
52 let bi = libBuildInfo lib
53 opts = options bi ++ [(GHC, args)]
54 bi' = bi { options = opts }
55 in lib { libBuildInfo = bi' }
56 Nothing -> error "Expected a library"
57 pd' = pd { library = Just lib' }
58 f pd' lbi muhs x
59