2 module Main (main) where
5 import Distribution.Simple
6 import Distribution.PackageDescription
7 import Distribution.Setup
8 import Distribution.Simple.LocalBuildInfo
9 import System.Environment
12 main = do args <- getArgs
13 let (ghcArgs, args') = extractGhcArgs args
14 (_, args'') = extractConfigureArgs args'
15 hooks = defaultUserHooks {
16 buildHook = add_ghc_options ghcArgs
17 $ buildHook defaultUserHooks }
18 withArgs args'' $ defaultMainWithHooks hooks
20 extractGhcArgs :: [String] -> ([String], [String])
21 extractGhcArgs = extractPrefixArgs "--ghc-option="
23 extractConfigureArgs :: [String] -> ([String], [String])
24 extractConfigureArgs = extractPrefixArgs "--configure-option="
26 extractPrefixArgs :: String -> [String] -> ([String], [String])
27 extractPrefixArgs the_prefix args
29 f (x:xs) = case f xs of
30 (wantedArgs, otherArgs) ->
31 case removePrefix the_prefix x of
33 (wantedArg:wantedArgs, otherArgs)
35 (wantedArgs, x:otherArgs)
38 removePrefix :: String -> String -> Maybe String
39 removePrefix "" ys = Just ys
40 removePrefix _ "" = Nothing
41 removePrefix (x:xs) (y:ys)
42 | x == y = removePrefix xs ys
45 type Hook a = PackageDescription -> LocalBuildInfo -> UserHooks -> a -> IO ()
47 add_ghc_options :: [String] -> Hook a -> Hook a
48 add_ghc_options args f pd lbi uhs x
49 = do let lib' = case library pd of
51 let bi = libBuildInfo lib
52 opts = options bi ++ [(GHC, args)]
53 bi' = bi { options = opts }
54 in lib { libBuildInfo = bi' }
55 Nothing -> error "Expected a library"
56 pd' = pd { library = Just lib' }