Use Cabal directly in place of ghc-cabal + make build root configurable (#531)
[hadrian.git] / src / Rules / Test.hs
1 module Rules.Test (testRules, runTestGhcFlags, timeoutProgPath) where
2
3 import Base
4 import Expression
5 import GHC.Packages
6 import Oracles.Flag
7 import Oracles.Setting
8 import Target
9 import Utilities
10
11 import System.Environment
12
13 -- TODO: clean up after testing
14 testRules :: Rules ()
15 testRules = do
16
17 root <- buildRootRules
18
19 root -/- timeoutPyPath ~> do
20 copyFile "testsuite/timeout/timeout.py" (root -/- timeoutPyPath)
21
22 -- TODO windows is still not supported.
23 --
24 -- See: https://github.com/ghc/ghc/blob/master/testsuite/timeout/Makefile#L23
25 root -/- timeoutProgPath ~> do
26 python <- builderPath Python
27 need [root -/- timeoutPyPath]
28 let script = unlines
29 [ "#!/usr/bin/env sh"
30 , "exec " ++ python ++ " $0.py \"$@\""
31 ]
32 liftIO $ do
33 writeFile (root -/- timeoutProgPath) script
34 makeExecutable (root -/- timeoutProgPath)
35
36 "validate" ~> do
37 needTestBuilders
38 build $ target (vanillaContext Stage2 compiler) (Make "testsuite/tests") [] []
39
40 "test" ~> do
41 needTestBuilders
42
43 -- Prepare the timeout program.
44 need [ root -/- timeoutProgPath ]
45
46 -- TODO This approach doesn't work.
47 -- Set environment variables for test's Makefile.
48 env <- sequence
49 [ builderEnvironment "MAKE" $ Make ""
50 , builderEnvironment "TEST_HC" $ Ghc CompileHs Stage2
51 , AddEnv "TEST_HC_OPTS" <$> runTestGhcFlags ]
52
53 makePath <- builderPath $ Make ""
54 top <- topDirectory
55 ghcPath <- (top -/-) <$> builderPath (Ghc CompileHs Stage2)
56 ghcFlags <- runTestGhcFlags
57
58 -- Set environment variables for test's Makefile.
59 liftIO $ do
60 setEnv "MAKE" makePath
61 setEnv "TEST_HC" ghcPath
62 setEnv "TEST_HC_OPTS" ghcFlags
63
64 -- Execute the test target.
65 buildWithCmdOptions env $ target (vanillaContext Stage2 compiler) RunTest [] []
66
67 needTestBuilders :: Action ()
68 needTestBuilders = do
69 needBuilder $ Ghc CompileHs Stage2
70 needBuilder $ GhcPkg Update Stage1
71 needBuilder Hp2Ps
72 needBuilder Hpc
73 needBuilder (Hsc2Hs Stage1)
74
75
76 -- | Extra flags to send to the Haskell compiler to run tests.
77 runTestGhcFlags :: Action String
78 runTestGhcFlags = do
79 unregisterised <- flag GhcUnregisterised
80
81 let ifMinGhcVer ver opt = do v <- ghcCanonVersion
82 if ver <= v then pure opt
83 else pure ""
84
85 -- Read extra argument for test from command line, like `-fvectorize`.
86 ghcOpts <- fromMaybe "" <$> (liftIO $ lookupEnv "EXTRA_HC_OPTS")
87
88 -- See: https://github.com/ghc/ghc/blob/master/testsuite/mk/test.mk#L28
89 let ghcExtraFlags = if unregisterised
90 then "-optc-fno-builtin"
91 else ""
92
93 -- Take flags to send to the Haskell compiler from test.mk.
94 -- See: https://github.com/ghc/ghc/blob/master/testsuite/mk/test.mk#L37
95 unwords <$> sequence
96 [ pure " -dcore-lint -dcmm-lint -no-user-package-db -rtsopts"
97 , pure ghcOpts
98 , pure ghcExtraFlags
99 , ifMinGhcVer "711" "-fno-warn-missed-specialisations"
100 , ifMinGhcVer "711" "-fshow-warning-groups"
101 , ifMinGhcVer "801" "-fdiagnostics-color=never"
102 , ifMinGhcVer "801" "-fno-diagnostics-show-caret"
103 , pure "-dno-debug-output"
104 ]
105
106 timeoutPyPath :: FilePath
107 timeoutPyPath = "test/bin/timeout.py"
108
109 timeoutProgPath :: FilePath
110 timeoutProgPath = "test/bin/timeout" <.> exe