1 -----------------------------------------------------------------------------
2 -- $Id: DriverFlags.hs,v 1.3 2000/10/11 16:26:04 simonmar Exp $
6 -- (c) Simon Marlow 2000
8 -----------------------------------------------------------------------------
10 module DriverFlags where
12 #include "HsVersions.h"
14 import PackageMaintenance
28 -----------------------------------------------------------------------------
31 -- Flag parsing is now done in stages:
33 -- * parse the initial list of flags and remove any flags understood
34 -- by the driver only. Determine whether we're in multi-compilation
35 -- or single-compilation mode.
37 -- * gather the list of "static" hsc flags, and assign them to the global
38 -- static hsc flags variable.
40 -- * build the inital DynFlags from the remaining flags.
42 -- * complain if we've got any flags left over.
44 -- * for each source file: grab the OPTIONS, and build a new DynFlags
45 -- to pass to the compiler.
47 -----------------------------------------------------------------------------
48 -- Process command-line
51 = NoArg (IO ()) -- flag with no argument
52 | HasArg (String -> IO ()) -- flag has an argument (maybe prefix)
53 | SepArg (String -> IO ()) -- flag has a separate argument
54 | Prefix (String -> IO ()) -- flag is a prefix only
55 | OptPrefix (String -> IO ()) -- flag may be a prefix
56 | AnySuffix (String -> IO ()) -- flag is a prefix, pass whole arg to fn
57 | PassFlag (String -> IO ()) -- flag with no arg, pass flag to fn
58 | PrefixPred (String -> Bool) (String -> IO ())
59 | AnySuffixPred (String -> Bool) (String -> IO ())
61 processArgs :: [(String,OptKind)] -> [String] -> [String]
62 -> IO [String] -- returns spare args
63 processArgs _spec [] spare = return (reverse spare)
64 processArgs spec args@(arg@('-':_):args') spare = do
65 case findArg spec arg of
67 do args' <- processOneArg action rest args
68 processArgs spec args' spare
70 processArgs spec args' (arg:spare)
71 processArgs spec (arg:args) spare =
72 processArgs spec args (arg:spare)
74 processOneArg :: OptKind -> String -> [String] -> IO [String]
75 processOneArg action rest (dash_arg@('-':arg):args) =
79 then io >> return args
80 else unknownFlagErr dash_arg
84 then fio rest >> return args
86 [] -> unknownFlagErr dash_arg
87 (arg1:args1) -> fio arg1 >> return args1
91 [] -> unknownFlagErr dash_arg
92 (arg1:args1) -> fio arg1 >> return args1
96 then fio rest >> return args
97 else unknownFlagErr dash_arg
101 then fio rest >> return args
102 else unknownFlagErr dash_arg
104 OptPrefix fio -> fio rest >> return args
106 AnySuffix fio -> fio dash_arg >> return args
108 AnySuffixPred p fio -> fio dash_arg >> return args
112 then unknownFlagErr dash_arg
113 else fio dash_arg >> return args
115 findArg :: [(String,OptKind)] -> String -> Maybe (String,OptKind)
117 = case [ (remove_spaces rest, k)
118 | (pat,k) <- spec, Just rest <- [my_prefix_match pat arg],
124 arg_ok (NoArg _) rest arg = null rest
125 arg_ok (HasArg _) rest arg = True
126 arg_ok (SepArg _) rest arg = null rest
127 arg_ok (Prefix _) rest arg = not (null rest)
128 arg_ok (PrefixPred p _) rest arg = not (null rest) && p rest
129 arg_ok (OptPrefix _) rest arg = True
130 arg_ok (PassFlag _) rest arg = null rest
131 arg_ok (AnySuffix _) rest arg = not (null rest)
132 arg_ok (AnySuffixPred p _) rest arg = not (null rest) && p arg
134 -----------------------------------------------------------------------------
137 -- note that ordering is important in the following list: any flag which
138 -- is a prefix flag (i.e. HasArg, Prefix, OptPrefix, AnySuffix) will override
139 -- flags further down the list with the same prefix.
142 [ ------- help -------------------------------------------------------
143 ( "?" , NoArg long_usage)
144 , ( "-help" , NoArg long_usage)
147 ------- version ----------------------------------------------------
148 , ( "-version" , NoArg (do hPutStrLn stdout (cProjectName
149 ++ ", version " ++ version_str)
150 exitWith ExitSuccess))
151 , ( "-numeric-version", NoArg (do hPutStrLn stdout version_str
152 exitWith ExitSuccess))
154 ------- verbosity ----------------------------------------------------
155 , ( "v" , NoArg (writeIORef verbose True) )
156 , ( "n" , NoArg (writeIORef dry_run True) )
158 ------- recompilation checker --------------------------------------
159 , ( "recomp" , NoArg (writeIORef recomp True) )
160 , ( "no-recomp" , NoArg (writeIORef recomp False) )
162 ------- ways --------------------------------------------------------
163 , ( "prof" , NoArg (addNoDups ways WayProf) )
164 , ( "unreg" , NoArg (addNoDups ways WayUnreg) )
165 , ( "dll" , NoArg (addNoDups ways WayDll) )
166 , ( "ticky" , NoArg (addNoDups ways WayTicky) )
167 , ( "parallel" , NoArg (addNoDups ways WayPar) )
168 , ( "gransim" , NoArg (addNoDups ways WayGran) )
169 , ( "smp" , NoArg (addNoDups ways WaySMP) )
170 , ( "debug" , NoArg (addNoDups ways WayDebug) )
173 ------ Debugging ----------------------------------------------------
174 , ( "dppr-noprags", PassFlag (add opt_C) )
175 , ( "dppr-debug", PassFlag (add opt_C) )
176 , ( "dppr-user-length", AnySuffix (add opt_C) )
177 -- rest of the debugging flags are dynamic
179 ------- Interface files ---------------------------------------------
180 , ( "hi" , NoArg (writeIORef produceHi True) )
181 , ( "nohi" , NoArg (writeIORef produceHi False) )
183 --------- Profiling --------------------------------------------------
184 , ( "auto-dicts" , NoArg (add opt_C "-fauto-sccs-on-dicts") )
185 , ( "auto-all" , NoArg (add opt_C "-fauto-sccs-on-all-toplevs") )
186 , ( "auto" , NoArg (add opt_C "-fauto-sccs-on-exported-toplevs") )
187 , ( "caf-all" , NoArg (add opt_C "-fauto-sccs-on-individual-cafs") )
188 -- "ignore-sccs" doesn't work (ToDo)
190 , ( "no-auto-dicts" , NoArg (add anti_opt_C "-fauto-sccs-on-dicts") )
191 , ( "no-auto-all" , NoArg (add anti_opt_C "-fauto-sccs-on-all-toplevs") )
192 , ( "no-auto" , NoArg (add anti_opt_C "-fauto-sccs-on-exported-toplevs") )
193 , ( "no-caf-all" , NoArg (add anti_opt_C "-fauto-sccs-on-individual-cafs") )
195 ------- Miscellaneous -----------------------------------------------
196 , ( "no-link-chk" , NoArg (return ()) ) -- ignored for backwards compat
198 ------- Output Redirection ------------------------------------------
199 , ( "odir" , HasArg (writeIORef output_dir . Just) )
200 , ( "o" , SepArg (writeIORef output_file . Just) )
201 , ( "osuf" , HasArg (writeIORef output_suf . Just) )
202 , ( "hisuf" , HasArg (writeIORef hi_suf) )
203 , ( "tmpdir" , HasArg (writeIORef v_TmpDir . (++ "/")) )
204 , ( "ohi" , HasArg (\s -> case s of
205 "-" -> writeIORef hi_on_stdout True
206 _ -> writeIORef output_hi (Just s)) )
209 , ( "keep-hc-file" , AnySuffix (\_ -> writeIORef keep_hc_files True) )
210 , ( "keep-s-file" , AnySuffix (\_ -> writeIORef keep_s_files True) )
211 , ( "keep-raw-s-file", AnySuffix (\_ -> writeIORef keep_raw_s_files True) )
212 , ( "keep-tmp-files" , AnySuffix (\_ -> writeIORef keep_tmp_files True) )
214 , ( "split-objs" , NoArg (if can_split
215 then do writeIORef split_object_files True
216 add opt_C "-fglobalise-toplev-names"
217 -- TODO!!!!! add opt_c "-DUSE_SPLIT_MARKERS"
218 else hPutStrLn stderr
219 "warning: don't know how to split \
220 \object files on this architecture"
223 ------- Include/Import Paths ----------------------------------------
224 , ( "i" , OptPrefix (addToDirList import_paths) )
225 , ( "I" , Prefix (addToDirList include_paths) )
227 ------- Libraries ---------------------------------------------------
228 , ( "L" , Prefix (addToDirList library_paths) )
229 , ( "l" , Prefix (add cmdline_libraries) )
231 ------- Packages ----------------------------------------------------
232 , ( "package-name" , HasArg (\s -> add opt_C ("-inpackage="++s)) )
234 , ( "package" , HasArg (addPackage) )
235 , ( "syslib" , HasArg (addPackage) ) -- for compatibility w/ old vsns
237 , ( "-list-packages" , NoArg (listPackages) )
238 , ( "-add-package" , NoArg (newPackage) )
239 , ( "-delete-package" , SepArg (deletePackage) )
241 ------- Specific phases --------------------------------------------
242 , ( "pgmL" , HasArg (writeIORef pgm_L) )
243 , ( "pgmP" , HasArg (writeIORef pgm_P) )
244 , ( "pgmC" , HasArg (writeIORef pgm_C) )
245 , ( "pgmc" , HasArg (writeIORef pgm_c) )
246 , ( "pgmm" , HasArg (writeIORef pgm_m) )
247 , ( "pgms" , HasArg (writeIORef pgm_s) )
248 , ( "pgma" , HasArg (writeIORef pgm_a) )
249 , ( "pgml" , HasArg (writeIORef pgm_l) )
251 , ( "optdep" , HasArg (add opt_dep) )
252 , ( "optl" , HasArg (add opt_l) )
253 , ( "optdll" , HasArg (add opt_dll) )
255 ------ Warning opts -------------------------------------------------
256 , ( "W" , NoArg (writeIORef warning_opt W_) )
257 , ( "Wall" , NoArg (writeIORef warning_opt W_all) )
258 , ( "Wnot" , NoArg (writeIORef warning_opt W_not) )
259 , ( "w" , NoArg (writeIORef warning_opt W_not) )
261 ----- Linker --------------------------------------------------------
262 , ( "static" , NoArg (writeIORef static True) )
264 ------ Compiler flags -----------------------------------------------
265 , ( "O2-for-C" , NoArg (writeIORef opt_minus_o2_for_C True) )
266 , ( "O" , OptPrefix (setOptLevel) )
268 , ( "fasm" , OptPrefix (\_ -> writeIORef hsc_lang HscAsm) )
270 , ( "fvia-c" , NoArg (writeIORef hsc_lang HscC) )
271 , ( "fvia-C" , NoArg (writeIORef hsc_lang HscC) )
273 , ( "fno-asm-mangling" , NoArg (writeIORef do_asm_mangling False) )
275 , ( "fmax-simplifier-iterations",
276 Prefix (writeIORef opt_MaxSimplifierIterations . read) )
278 , ( "fusagesp" , NoArg (do writeIORef opt_UsageSPInf True
279 add opt_C "-fusagesp-on") )
281 , ( "fexcess-precision" , NoArg (do writeIORef excess_precision True
282 add opt_C "-fexcess-precision"))
284 -- flags that are "active negatives"
285 , ( "fno-implicit-prelude" , PassFlag (add opt_C) )
286 , ( "fno-prune-tydecls" , PassFlag (add opt_C) )
287 , ( "fno-prune-instdecls" , PassFlag (add opt_C) )
288 , ( "fno-pre-inlining" , PassFlag (add opt_C) )
290 -- All other "-fno-<blah>" options cancel out "-f<blah>" on the hsc cmdline
291 , ( "fno-", PrefixPred (\s -> isStaticHscFlag ("f"++s))
292 (\s -> add anti_opt_C ("-f"++s)) )
294 -- Pass all remaining "-f<blah>" options to hsc
295 , ( "f", AnySuffixPred (isStaticHscFlag) (add opt_C) )
298 -----------------------------------------------------------------------------
299 -- parse the dynamic arguments
301 GLOBAL_VAR(v_DynFlags, error "no dynFlags", DynFlags)
304 dfs <- readIORef v_DynFlags
305 writeIORef v_DynFlags dfs{ flags = f : flags dfs }
308 dfs <- readIORef v_DynFlags
309 writeIORef v_DynFlags dfs{ flags = filter (/= f) (flags dfs) }
313 ( "cpp", NoArg (updateState (\s -> s{ cpp_flag = True })) )
314 , ( "#include", HasArg (addCmdlineHCInclude) )
316 , ( "optL", HasArg (addOpt_L) )
317 , ( "optP", HasArg (addOpt_P) )
318 , ( "optc", HasArg (addOpt_c) )
319 , ( "optm", HasArg (addOpt_m) )
320 , ( "opta", HasArg (addOpt_a) )
322 ------ HsCpp opts ---------------------------------------------------
323 , ( "D", Prefix (\s -> addOpt_P ("-D'"++s++"'") ) )
324 , ( "U", Prefix (\s -> addOpt_P ("-U'"++s++"'") ) )
326 ------ Debugging ----------------------------------------------------
327 , ( "dstg-stats", NoArg (writeIORef opt_StgStats True) )
329 , ( "ddump_all", NoArg (setDynFlag Opt_D_dump_all) )
330 , ( "ddump_most", NoArg (setDynFlag Opt_D_dump_most) )
331 , ( "ddump_absC", NoArg (setDynFlag Opt_D_dump_absC) )
332 , ( "ddump_asm", NoArg (setDynFlag Opt_D_dump_asm) )
333 , ( "ddump_cpranal", NoArg (setDynFlag Opt_D_dump_cpranal) )
334 , ( "ddump_deriv", NoArg (setDynFlag Opt_D_dump_deriv) )
335 , ( "ddump_ds", NoArg (setDynFlag Opt_D_dump_ds) )
336 , ( "ddump_flatC", NoArg (setDynFlag Opt_D_dump_flatC) )
337 , ( "ddump_foreign", NoArg (setDynFlag Opt_D_dump_foreign) )
338 , ( "ddump_inlinings", NoArg (setDynFlag Opt_D_dump_inlinings) )
339 , ( "ddump_occur_anal", NoArg (setDynFlag Opt_D_dump_occur_anal) )
340 , ( "ddump_parsed", NoArg (setDynFlag Opt_D_dump_parsed) )
341 , ( "ddump_realC", NoArg (setDynFlag Opt_D_dump_realC) )
342 , ( "ddump_rn", NoArg (setDynFlag Opt_D_dump_rn) )
343 , ( "ddump_simpl", NoArg (setDynFlag Opt_D_dump_simpl) )
344 , ( "ddump_simpl_iterations", NoArg (setDynFlag Opt_D_dump_simpl_iterations) )
345 , ( "ddump_spec", NoArg (setDynFlag Opt_D_dump_spec) )
346 , ( "ddump_stg", NoArg (setDynFlag Opt_D_dump_stg) )
347 , ( "ddump_stranal", NoArg (setDynFlag Opt_D_dump_stranal) )
348 , ( "ddump_tc", NoArg (setDynFlag Opt_D_dump_tc) )
349 , ( "ddump_types", NoArg (setDynFlag Opt_D_dump_types) )
350 , ( "ddump_rules", NoArg (setDynFlag Opt_D_dump_rules) )
351 , ( "ddump_usagesp", NoArg (setDynFlag Opt_D_dump_usagesp) )
352 , ( "ddump_cse", NoArg (setDynFlag Opt_D_dump_cse) )
353 , ( "ddump_worker_wrapper", NoArg (setDynFlag Opt_D_dump_worker_wrapper) )
354 , ( "dshow_passes", NoArg (setDynFlag Opt_D_show_passes) )
355 , ( "ddump_rn_trace", NoArg (setDynFlag Opt_D_dump_rn_trace) )
356 , ( "ddump_rn_stats", NoArg (setDynFlag Opt_D_dump_rn_stats) )
357 , ( "ddump_stix", NoArg (setDynFlag Opt_D_dump_stix) )
358 , ( "ddump_simpl_stats", NoArg (setDynFlag Opt_D_dump_simpl_stats) )
359 , ( "dsource_stats", NoArg (setDynFlag Opt_D_source_stats) )
360 , ( "dverbose_core2core", NoArg (setDynFlag Opt_D_verbose_core2core) )
361 , ( "dverbose_stg2stg", NoArg (setDynFlag Opt_D_verbose_stg2stg) )
362 , ( "ddump_hi_diffs", NoArg (setDynFlag Opt_D_dump_hi_diffs) )
363 , ( "ddump_minimal_imports", NoArg (setDynFlag Opt_D_dump_minimal_imports) )
364 , ( "DoCoreLinting", NoArg (setDynFlag Opt_DoCoreLinting) )
365 , ( "DoStgLinting", NoArg (setDynFlag Opt_DoStgLinting) )
366 , ( "DoUSPLinting", NoArg (setDynFlag Opt_DoUSPLinting) )
368 ------ Machine dependant (-m<blah>) stuff ---------------------------
370 , ( "monly-2-regs", NoArg (updateState (\s -> s{stolen_x86_regs = 2}) ))
371 , ( "monly-3-regs", NoArg (updateState (\s -> s{stolen_x86_regs = 3}) ))
372 , ( "monly-4-regs", NoArg (updateState (\s -> s{stolen_x86_regs = 4}) ))
374 ------ Compiler flags -----------------------------------------------
376 , ( "fglasgow-exts", NoArg (setDynFlag Opt_GlasgowExts) )
378 , ( "fallow-overlapping-instances",
379 NoArg (setDynFlag Opt_AllowOverlappingInstances) )
381 , ( "fallow-undecidable-instances",
382 NoArg (setDynFlag Opt_AllowUndecidableInstances) )
385 -----------------------------------------------------------------------------
386 -- convert sizes like "3.5M" into integers
388 decodeSize :: String -> Integer
390 | c == "" = truncate n
391 | c == "K" || c == "k" = truncate (n * 1000)
392 | c == "M" || c == "m" = truncate (n * 1000 * 1000)
393 | c == "G" || c == "g" = truncate (n * 1000 * 1000 * 1000)
394 | otherwise = throwDyn (OtherError ("can't decode size: " ++ str))
395 where (m, c) = span pred str
397 pred c = isDigit c || c == '.'
399 floatOpt :: IORef Double -> String -> IO ()
401 = writeIORef ref (read str :: Double)
403 -----------------------------------------------------------------------------
404 -- Build the Hsc static command line opts
406 build_hsc_opts :: IO [String]
408 opt_C_ <- getStaticOpts opt_C -- misc hsc opts
411 warn_level <- readIORef warning_opt
412 let warn_opts = case warn_level of
413 W_default -> standardWarnings
415 W_all -> minusWallOpts
419 minus_o <- readIORef opt_level
422 0 -> hsc_minusNoO_flags
423 1 -> hsc_minusO_flags
424 2 -> hsc_minusO2_flags
425 _ -> error "unknown opt level"
429 ways_ <- readIORef ways
430 let stg_massage | WayProf `elem` ways_ = "-fmassage-stg-for-profiling"
433 stg_stats <- readIORef opt_StgStats
434 let stg_stats_flag | stg_stats = "-dstg-stats"
437 let stg_opts = [ stg_massage, stg_stats_flag, "-flet-no-escape" ]
438 -- let-no-escape always on for now
440 -- take into account -fno-* flags by removing the equivalent -f*
441 -- flag from our list.
442 anti_flags <- getStaticOpts anti_opt_C
443 let basic_opts = opt_C_ ++ warn_opts ++ optimisation_opts ++ stg_opts
444 filtered_opts = filter (`notElem` anti_flags) basic_opts
447 let hi_vers = "-fhi-version="++cProjectVersionInt
449 static <- (do s <- readIORef static; if s then return "-static" else return "")
451 l <- readIORef hsc_lang
454 HscAsm -> "-olang=asm"
455 HscJava -> "-olang=java"
457 -- get hi-file suffix
458 hisuf <- readIORef hi_suf
460 -- hi-suffix for packages depends on the build tag.
462 do tag <- readIORef build_tag
465 else return (tag ++ "_hi")
467 import_dirs <- readIORef import_paths
468 package_import_dirs <- getPackageImportPath
470 let hi_map = "-himap=" ++
471 makeHiMap import_dirs hisuf
472 package_import_dirs package_hisuf
475 hi_map_sep = "-himap-sep=" ++ [split_marker]
480 ++ [ hi_vers, static, verb, lang, hi_map, hi_map_sep ]
484 (import_dirs :: [String])
485 (hi_suffix :: String)
486 (package_import_dirs :: [String])
487 (package_hi_suffix :: String)
488 (split_marker :: Char)
489 = foldr (add_dir hi_suffix)
490 (foldr (add_dir package_hi_suffix) "" package_import_dirs)
493 add_dir hisuf dir str = dir ++ "%." ++ hisuf ++ split_marker : str