Ditch static flags
[ghc.git] / testsuite / tests / plugins / FrontendPlugin.hs
1 module FrontendPlugin where
2
3 import GhcPlugins
4 import qualified GHC
5 import GHC ( Ghc, LoadHowMuch(..) )
6
7 import DriverPipeline hiding ( hsc_env )
8 import DriverPhases
9 import System.Exit
10 import Control.Monad
11 import Data.List
12
13 frontendPlugin :: FrontendPlugin
14 frontendPlugin = defaultFrontendPlugin {
15 frontend = doMake
16 }
17
18 -- Copypasted from ghc/Main.hs
19 doMake :: [String] -> [(String,Maybe Phase)] -> Ghc ()
20 doMake opts srcs = do
21 liftIO $ print opts
22 let (hs_srcs, non_hs_srcs) = partition haskellish srcs
23
24 haskellish (f,Nothing) =
25 looksLikeModuleName f || isHaskellUserSrcFilename f || '.' `notElem` f
26 haskellish (_,Just phase) =
27 phase `notElem` [ As True, As False, Cc, Cobjc, Cobjcxx, CmmCpp, Cmm
28 , StopLn]
29
30 hsc_env <- GHC.getSession
31
32 -- if we have no haskell sources from which to do a dependency
33 -- analysis, then just do one-shot compilation and/or linking.
34 -- This means that "ghc Foo.o Bar.o -o baz" links the program as
35 -- we expect.
36 if (null hs_srcs)
37 then liftIO (oneShot hsc_env StopLn srcs)
38 else do
39
40 o_files <- mapM (\x -> liftIO $ compileFile hsc_env StopLn x)
41 non_hs_srcs
42 dflags <- GHC.getSessionDynFlags
43 let dflags' = dflags { ldInputs = map (FileOption "") o_files
44 ++ ldInputs dflags }
45 _ <- GHC.setSessionDynFlags dflags'
46
47 targets <- mapM (uncurry GHC.guessTarget) hs_srcs
48 GHC.setTargets targets
49 ok_flag <- GHC.load LoadAllTargets
50
51 when (failed ok_flag) (liftIO $ exitWith (ExitFailure 1))
52 return ()