3 We need to do some ugly hacks here as base mix of portable and
4 unportable stuff, as well as home to some GHC magic.
7 module Main (main) where
11 import Distribution.Simple
12 import Distribution.PackageDescription
13 import Distribution.PreProcess
14 import Distribution.Setup
15 import Distribution.Simple.Configure
16 import Distribution.Simple.LocalBuildInfo
17 import System.Environment
21 main = do args <- getArgs
22 let (ghcArgs, args') = extractGhcArgs args
23 (configureArgs, args'') = extractConfigureArgs args'
24 hooks = defaultUserHooks {
25 confHook = add_extra_deps
26 $ confHook defaultUserHooks,
27 postConf = add_configure_options configureArgs
28 $ postConf defaultUserHooks,
29 buildHook = add_ghc_options ghcArgs
31 $ buildHook defaultUserHooks,
32 instHook = filter_modules_hook
33 $ instHook defaultUserHooks }
34 withArgs args'' $ defaultMainWithHooks hooks
36 extractGhcArgs :: [String] -> ([String], [String])
37 extractGhcArgs = extractPrefixArgs "--ghc-option="
39 extractConfigureArgs :: [String] -> ([String], [String])
40 extractConfigureArgs = extractPrefixArgs "--configure-option="
42 extractPrefixArgs :: String -> [String] -> ([String], [String])
43 extractPrefixArgs prefix args
45 f (x:xs) = case f xs of
46 (wantedArgs, otherArgs) ->
47 case removePrefix prefix x of
49 (wantedArg:wantedArgs, otherArgs)
51 (wantedArgs, x:otherArgs)
54 removePrefix :: String -> String -> Maybe String
55 removePrefix "" ys = Just ys
56 removePrefix (x:xs) (y:ys)
57 | x == y = removePrefix xs ys
60 type Hook a = PackageDescription -> LocalBuildInfo -> Maybe UserHooks -> a
62 type ConfHook = PackageDescription -> ConfigFlags -> IO LocalBuildInfo
63 type PostConfHook = Args -> ConfigFlags -> PackageDescription -> LocalBuildInfo
66 -- type PDHook = PackageDescription -> ConfigFlags -> IO ()
68 add_ghc_options :: [String] -> Hook a -> Hook a
69 add_ghc_options args f pd lbi muhs x
70 = do let lib' = case library pd of
72 let bi = libBuildInfo lib
73 opts = options bi ++ [(GHC, args)]
74 bi' = bi { options = opts }
75 in lib { libBuildInfo = bi' }
76 Nothing -> error "Expected a library"
77 pd' = pd { library = Just lib' }
80 add_configure_options :: [String] -> PostConfHook -> PostConfHook
81 add_configure_options args f as cfs pd lbi
82 = f (as ++ args) cfs pd lbi
84 filter_modules_hook :: Hook a -> Hook a
85 filter_modules_hook f pd lbi muhs x
86 = let build_filter = case compilerFlavor $ compiler lbi of
89 lib' = case library pd of
91 let ems = filter build_filter (exposedModules lib)
92 in lib { exposedModules = ems }
93 Nothing -> error "Expected a library"
94 pd' = pd { library = Just lib' }
97 isPortableBuild :: String -> Bool
99 | "GHC" `isPrefixOf` s = False
100 | "Data.Generics" `isPrefixOf` s = False
101 | otherwise = s `notElem` ["Foreign.Concurrent", "System.Process"]
103 forGHCBuild :: String -> Bool
104 forGHCBuild = ("GHC.Prim" /=)
106 add_extra_deps :: ConfHook -> ConfHook
107 add_extra_deps f pd cf
109 case compilerFlavor (compiler lbi) of
111 do -- Euch. We should just add the right thing to the lbi
112 -- ourselves rather than rerunning configure.
113 let pd' = pd { buildDepends = Dependency "rts" AnyVersion