on master help sign in
Avoid calling gcc to compile unless you have to (speed up iterations)
1affc8eNeil Mitchell
gen/Main.hs+24−2
@@ -3,12 +3,17 @@ module Main(main) where
3
3
4import Lib
4import Lib
5import Parser
5import Parser
6−import Control.Monad
6+import Control.Monad.Extra
7+import System.Process.Extra
8+import System.Directory
9+import Data.Maybe
10+import System.IO.Extra
11+import System.FilePath
12+import System.Info.Extra
7
13
8
14
9main :: IO ()
15main :: IO ()
10main = do
16main = do
11− writeFile "test.h" $ unlines $ compile html
12 forM_ examples $ \(b, src) -> do
17 forM_ examples $ \(b, src) -> do
13 putStrLn ""
18 putStrLn ""
14 putStrLn src
19 putStrLn src
@@ -16,7 +21,24 @@ main = do
16 print res
21 print res
17 print (trail, err)
22 print (trail, err)
18 putStrLn $ rerender src res
23 putStrLn $ rerender src res
24+ writeFileChanged "test.h" $ unlines $ compile html
25+ buildRule ["test.h","test.c"] ["gen" <.> (if isWindows then "exe" else "")] $
26+ system_ "gcc test.c -o gen"
27+ system_ "gen"
19
28
29+writeFileChanged :: FilePath -> String -> IO ()
30+writeFileChanged file new = do
31+ old <- ifM (doesFileExist file) (readFile' file) (return "")
32+ when (old /= new) $
33+ writeFile file new
34+
35+buildRule :: [FilePath] -> [FilePath] -> IO () -> IO ()
36+buildRule from to act = do
37+ let modTime x = ifM (doesFileExist x) (Just <$> getModificationTime x) (return Nothing)
38+ from <- mapM modTime from
39+ to <- mapM modTime to
40+ when (any isNothing (from ++ to) || maximum from >= minimum to)
41+ act
20
42
21examples :: [(Bool, String)]
43examples :: [(Bool, String)]
22examples =
44examples =