-
Notifications
You must be signed in to change notification settings - Fork 2
/
GhcParmake.hs
279 lines (240 loc) · 10.4 KB
/
GhcParmake.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
module Main
where
import Control.Monad (liftM, when)
import Data.List (isPrefixOf)
import Data.Maybe (fromMaybe)
import Data.Version (showVersion)
import System.Environment (getArgs)
import System.Exit (ExitCode (..), exitFailure, exitSuccess, exitWith)
import System.IO (hPutStrLn, stderr, hSetBuffering, BufferMode(LineBuffering))
import GHC.ParMake.Common (maybeRead)
import GHC.ParMake.Util
import qualified GHC.ParMake.BuildPlan as BuildPlan
import qualified GHC.ParMake.Parse as Parse
import qualified GHC.ParMake.Engine as Engine
import qualified Paths_ghc_parmake (version)
-- Argument handling.
data Args = Args {
verbosity :: Verbosity,
printVersion :: Bool,
printUsage :: Bool,
numJobs :: Int,
ghcPath :: String,
ghcServerPath :: String,
extraDepends :: [String],
outputFilename :: Maybe String,
osuf :: String,
hisuf :: String
} deriving Show
defaultArgs :: Args
defaultArgs = Args {
verbosity = normal,
printVersion = False,
printUsage = False,
numJobs = 1,
ghcPath = "ghc",
ghcServerPath = "ghc-server",
extraDepends = [],
outputFilename = Nothing,
osuf = "o",
hisuf = "hi"
}
parseArgs :: [String] -> Args
parseArgs l = go l defaultArgs
where
parseNumJobs n = fromMaybe (fatal "The argument to '-j' must be an integer!")
(liftM abs $ maybeRead n)
parseVerbosity n = fromMaybe verbose (maybeRead n >>= intToVerbosity)
go [] acc = acc
go ("-V":_) acc = acc { printVersion = True }
go ("--help":_) acc = acc { printUsage = True }
go ("-j":n:as) acc = go as $ acc { numJobs = parseNumJobs n }
go (('-':'j':n:[]):as) acc = go as $ acc { numJobs = parseNumJobs [n] }
go (('-':'v':n:[]):as) acc = go as $
acc { verbosity = parseVerbosity [n] }
go (('-':'v':'v':n:[]):as) acc = go as $
acc { verbosity = parseVerbosity [n] }
go ("-v":as) acc = go as $ acc { verbosity = verbose }
go ("-optP-include":as) acc = handleOptPInclude as acc
go ("-o":n:as) acc = go as $ acc { outputFilename = Just n }
go ("-osuf":suf:as) acc = go as $ acc { osuf = suf }
go ("-hisuf":suf:as) acc = go as $ acc { hisuf = suf }
go ("--ghc-path":p:as) acc = go as $ acc { ghcPath = p }
go (a:as) acc
| "--ghc-path=" `isPrefixOf` a = let (o,p') = break (== '=') a in
go (o:(tail p'):as) acc
go ("--ghc-server-path":p:as) acc= go as $ acc { ghcServerPath = p }
go (a:as) acc | "--ghc-server-path=" `isPrefixOf` a
= let (o,p') = break (== '=') a in
go (o:(tail p'):as) acc
go (_:as) acc = go as acc
-- Add '-optP-include -optPmyfile' as extraDepends
handleOptPInclude [] _ = fatal "no path is given after -optP-include"
handleOptPInclude (optPfile:as) acc@Args { extraDepends = ds } =
case splitOffPrefix "-optP" optPfile of
Just path | not (null path) -> go as $ acc { extraDepends = path : ds }
| otherwise -> fatal $
"path given after -optP-include is empty"
_ -> fatal "missing -optP after -optP-include"
splitOffPrefix :: (Eq a) => [a] -> [a] -> Maybe [a]
splitOffPrefix p s = case splitAt (length p) s of
(p', r) | p' == p -> Just r
_ -> Nothing
-- | Processes a list of arguments, returning:
-- * the GHC arguments we want to use in parmake
-- * the files to be compiled
-- * the original GHC arguments with custom parmake arguments removed
-- (thus also contains files)
getGhcArgs :: [String] -> ([String],[String],[String])
getGhcArgs argv = let nonParmakeArgs = rmArgs argv
(args, files) = mkArgs nonParmakeArgs [] []
in (args, files, nonParmakeArgs)
where
pgmSuffixes = ["L", "P", "c", "m", "s", "a", "l", "dll", "F", "windres"]
optsWithArg = [ "-odir", "-hidir", "-ohi", "-stubdir", "-outputdir"
, "-tmpdir", "-osuf", "-hisuf", "-hcsuf"
, "-package", "-package-db", "-package-id", "-hide-package"
, "-ignore-package", "-package-name", "-package-conf", "-f"
, "-framework", "-framework-path"
, "-main-is", "-x"]
++ ["-pgm" ++ str | str <- pgmSuffixes ]
++ ["-opt" ++ str | str <- pgmSuffixes ]
eatOption [] as = ([], as)
eatOption (opt:arg:xs) as
-- Unlike 'ghc --make', 'ghc -c' for some reason does not include -hidir
-- in the interface search path.
| opt == "-hidir" = (xs, ('-':'i':arg):arg:opt:as)
| opt `elem` optsWithArg = (xs, arg:opt:as)
eatOption (x:xs) as = (xs, x:as)
-- Processes GHC args to create GHC style args suiting for parmake,
-- and splitting files apart.
mkArgs [] as fs = (reverse as, reverse fs)
mkArgs ("-o":_:xs) as fs = mkArgs xs as fs
mkArgs ("--make":xs) as fs = mkArgs xs as fs
mkArgs xs@(('-':_):_) as fs = let (xs', as') = eatOption xs as
in mkArgs xs' as' fs
mkArgs (x:xs) as fs = mkArgs xs as (x:fs)
-- Removes parmake args from a list of arguments.
rmArgs [] = []
-- Options not passed to GHC: -o, -j, -vv, --ghc-path, --make.
rmArgs ("-j":_:xs) = rmArgs xs
rmArgs (('-':'v':'v':_:[]):xs) = rmArgs xs
rmArgs ("--ghc-path":_:xs) = rmArgs xs
rmArgs (x:xs)
| "--ghc-path=" `isPrefixOf` x = rmArgs xs
rmArgs (arg:xs) = arg : rmArgs xs
usage :: IO ()
usage =
putStr $ "Usage: ghc-parmake [OPTIONS] FILES\n" ++
"A parallel wrapper around 'ghc --make'.\n\n" ++
"Options: \n" ++
"-j N - Run N jobs in parallel.\n" ++
"--ghc-path=PATH - Set the path to the ghc executable.\n" ++
"--ghc-server-path=PATH - Set the path to the ghc-server executable.\n" ++
"-vv[N] - Set verbosity to N (only for ghc-parmake). " ++
"N is 0-3, default 1.\n" ++
"-v[N] - Set verbosity to N " ++
"(both for GHC and ghc-parmake itself).\n" ++
"--help - Print usage information.\n" ++
"-V - Print version information.\n" ++
"\nOther options are passed to GHC unmodified.\n"
-- TODO: To fully emulate GHC's behaviour, we must know whether the input module
-- set contains a module Main.
--
-- Consider these two invocations:
--
-- $ ghc --make Module.hs Module0.hs
-- [1 of 2] Compiling Module ( Module.hs, t/Module.o )
-- [2 of 2] Compiling Module0 ( Module0.hs, t/Module0.o )
--
-- $ ghc --make Module.hs Main.hs
-- [1 of 2] Compiling Module ( Module.hs, t/Module.o )
-- [2 of 2] Compiling Main ( Main.hs, t/Main.o )
-- Linking Main ...
--
-- In the first case, the linking step is not performed since there is no module
-- called 'Main'.
--
-- Additionally, the module 'Main' can have an arbitrary source file name, not
-- necessary 'Main.hs'. This changes the name of the output executable:
--
-- $ ghc --make Module.hs MyProg.hs
-- [1 of 2] Compiling Module ( Module.hs, t/Module.o )
-- [2 of 2] Compiling Main ( MyProg.hs, t/Main.o )
-- Linking MyProg ...
--
-- We currently solve this problem by the final real GHC pass.
-- | All flags conflicting with `ghc -M`.
-- Obtained from the man page (listed in the same order as they appear there)
-- and ghc/Main.hs, `data PostLoadMode`:
-- All modes that are not `DoMkDependHS` (`-M`) are conflicting
-- (apart from `--make`).
flagsConflictingWithM :: [String]
flagsConflictingWithM =
-- "Help and verbosity options"
[ "?"
, "--supported-extensions"
, "--supported-languages"
, "--info"
, "--version"
, "--numeric-version"
, "--print-libdir"
-- -V and --help are not included here because this program uses them
-- "Which phases to run"
, "-E"
, "-C"
, "-S"
, "-c"
-- "Alternative modes of operation"
, "--interactive"
, "-e"
-- "Interface file options"
, "--show-iface"
-- Undocumented?
, "--abi-hash"
]
-- Program entry point.
main :: IO ()
main =
do -- Set stderr to line buffering to prevent interleaved GHC errors
hSetBuffering stderr LineBuffering
argv <- getArgs
let args = parseArgs argv
let (parmakeGhcArgs, files, nonParmakeArgs) = getGhcArgs argv
let v = verbosity $ args
when (printVersion args) $ putStrLn
("ghc-parmake " ++ showVersion Paths_ghc_parmake.version)
>> exitSuccess
when (printUsage args) $ usage >> exitSuccess
when (null $ ghcPath args) $ fatal "ghc path is invalid" >> exitFailure
-- Cases in which we just want to pass on all arguments to GHC and be
-- as transparent as possible:
--
-- * --numeric-version is used
-- (e.g. cabal does this to determine the GHC version)
-- * No input files are given
-- * An option conflicting with "-M" is given
let passToGhc = exitWith =<<
runProcess defaultOutputHooks Nothing (ghcPath args) nonParmakeArgs
when (any (`elem` parmakeGhcArgs) flagsConflictingWithM) $ passToGhc
-- We must not print this (or any other output) before handling the
-- skip-to-GHC cases above.
debug' v $ "Parsed args: " ++ show args
when (null files) $ passToGhc
debug' v "Running ghc -M (twice)..."
deps <- Parse.getModuleDeps v (ghcPath args) parmakeGhcArgs files
when (null deps) $ do
hPutStrLn stderr "ghc-parmake: no dependencies"
exitFailure
debug' v ("Parsed dependencies:\n" ++ show deps)
let settings = BuildPlan.Settings { BuildPlan.osuf = osuf args
, BuildPlan.hisuf = hisuf args }
plan = BuildPlan.new settings deps (extraDepends args)
debug' v ("Produced a build plan:\n" ++ show plan)
debug' v "Building..."
exitCode <- Engine.compile v plan (numJobs args) (ghcServerPath args)
(ghcPath args) parmakeGhcArgs files (outputFilename args)
when (exitCode /= ExitSuccess) $ exitWith exitCode
debug' v $ "Running final 'ghc --make' pass: "
++ ghcPath args ++ " " ++ unwords nonParmakeArgs
passToGhc