-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunghc.hs
153 lines (134 loc) · 5.21 KB
/
runghc.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
-- Copyright (C) 2011 Michael Zuser [email protected]
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
{-# LANGUAGE DeriveDataTypeable #-}
module Main (main) where
import Control.Applicative
import Control.Monad
import Data.List
import Data.Maybe
import System.Console.CmdArgs hiding (help)
import System.Console.CmdArgs.Explicit
import System.Directory
import System.Environment
import System.Exit
import System.FilePath
import System.Info
import System.IO
import System.Process
import System.Time
help = intercalate "\n"
[ "This is a replacement for runghc which keeps compiled files"
, "for fast runtimes when nothing has changed"
, ""
, "Usage: runghc [options] [ghc-options] file [file-arguments]"
, ""
, "Options:"
, " -! --compile-only Do not run the script, just compile it"
, " --exe-dir=DIR Directory for the executable"
, " default=<scr-dir>/.compile"
, " --out-dir=DIR Directory for the intermediate files"
, " default=<exe-dir>/<basename>-intermediate-files"
, " -? --help Display this help message"
, ""
]
data OptDesc = OptDesc
{ fCmpOnly :: Bool
, fExeDir :: Maybe FilePath
, fOutDir :: Maybe FilePath
} deriving (Show, Data, Typeable)
optDesc = OptDesc
{ fCmpOnly = False &= explicit &= name "compile-only" &= name "!"
, fExeDir = def &= explicit &= name "exe-dir"
, fOutDir = def &= explicit &= name "out-dir"
} &= versionArg [ignore]
main :: IO ()
main = do
(caOpts, args) <- getOpts <$> getArgs
when (isJust $ cmdArgsHelp caOpts) $
putStr help >> exitSuccess
let opts = cmdArgsValue caOpts
let (ghcOpts, fileAndArgs) = splitGHCOpts args
when (null fileAndArgs) $
putStr help >> exitSuccess
let (file:fileArgs) = fileAndArgs
let exe = getExe file opts
let outDir = getOutDir file opts
whenM (shouldCompile file exe outDir) $
compile ghcOpts file exe outDir
unless (fCmpOnly opts) $
exitWith =<< rawSystem exe fileArgs
where
splitGHCOpts = break ((== ".hs") . takeExtension)
getOpts :: [String] -> (CmdArgs OptDesc, [String])
getOpts args = (opts, drop (length nopts) args)
where
(Right opts : nopts) = reverse . takeWhile isRight . map processArgs . inits $ args
isRight e = case e of Right _ -> True ; Left _ -> False
processArgs = process (cmdArgsMode optDesc)
shouldCompile :: FilePath -> FilePath -> FilePath -> IO Bool
shouldCompile file exe outDir = ifteM (doesFileExist exe) newerSrcs (return True)
where
modTime = getModificationTime
newerSrcs = do
exeTime <- modTime exe
let isNewer s = ifteM (doesFileExist s) ((exeTime <) <$> modTime s) (return True)
(Nothing /=) <$> (findM isNewer =<< getSrcs file outDir)
getSrcs :: FilePath -> FilePath -> IO [FilePath]
getSrcs file outDir = (file :) . thereIsNoMain . concatMap hss <$> getFiles outDir
where
thereIsNoMain = filter $ (/= "Main") . takeBaseName
inSrcDir src = takeDirectory file </> makeRelative outDir src
asHs = flip replaceExtension "hs"
hss f = case takeExtension f of
".hi" -> [inSrcDir . asHs $ f]
_ -> []
compile :: [String] -> FilePath -> FilePath -> FilePath -> IO ()
compile ghcOpts file exe outDir = do
createDirectoryIfMissing True $ takeDirectory exe
runGHC fullOpts
where
fullOpts = ["--make","-o",exe,"-outputdir",outDir] ++ ghcOpts ++ [file]
runGHC :: [String] -> IO ()
runGHC opts = do
Just ghc <- findExecutable "ghc"
(ec, _, err) <- readProcessWithExitCode ghc opts ""
when (ec /= ExitSuccess) $ do
hPutStr stderr err
exitWith ec
getExeDir, getOutDir, getExe :: FilePath -> OptDesc -> FilePath
getExeDir file opts = fromMaybe def $ fExeDir opts
where def = takeDirectory file </> ".compiled"
getOutDir file opts = fromMaybe def $ fOutDir opts
where def = getExeDir file opts </> takeBaseName file ++ "-intermediate-files"
getExe file opts = getExeDir file opts </> takeBaseName file
-- Utility
ifteM :: Monad m => m Bool -> m a -> m a -> m a
ifteM b x y = b >>= \b -> if b then x else y
whenM :: Monad m => m Bool -> m () -> m ()
whenM b x = ifteM b x $ return ()
findM :: Monad m => (a -> m Bool) -> [a] -> m (Maybe a)
findM pred = go
where
go [] = return Nothing
go (a:as) = ifteM (pred a) (return (Just a)) (go as)
getFiles :: FilePath -> IO [FilePath]
getFiles d
= return . concat
=<< mapM expand
=<< return . map (d </>) . filter notDots
=<< getDirectoryContents d
where
notDots f = all (/= f) [".",".."]
expand f = ifteM (doesDirectoryExist f) (getFiles f) (return [f])