1 -----------------------------------------------------------------------------
5 -- Static flags can only be set once, on the command-line. Inside GHC,
6 -- each static flag corresponds to a top-level value, usually of type Bool.
8 -- (c) The University of Glasgow 2005
10 -----------------------------------------------------------------------------
18 WayName(..), v_Ways, v_Build_tag, v_RTS_Build_tag, isRTSWay,
20 -- Output style options
26 opt_AutoSccsOnAllToplevs,
27 opt_AutoSccsOnExportedToplevs,
28 opt_AutoSccsOnIndividualCafs,
37 opt_IrrefutableTuples,
42 opt_SpecInlineJoinPoints,
44 opt_SimplNoPreInlining,
45 opt_SimplExcessPrecision,
49 opt_UF_CreationThreshold,
51 opt_UF_FunAppDiscount,
72 #include "HsVersions.h"
78 import Maybes ( firstJust )
81 import Control.Exception ( throwDyn )
83 import System.IO.Unsafe ( unsafePerformIO )
84 import Control.Monad ( when )
85 import Data.Char ( isDigit )
88 -----------------------------------------------------------------------------
91 parseStaticFlags :: [String] -> IO [String]
92 parseStaticFlags args = do
93 ready <- readIORef v_opt_C_ready
94 when ready $ throwDyn (ProgramError "Too late for parseStaticFlags: call it before newSession")
96 (leftover, errs) <- processArgs static_flags args
97 when (not (null errs)) $ throwDyn (UsageError (unlines errs))
99 -- deal with the way flags: the way (eg. prof) gives rise to
100 -- further flags, some of which might be static.
101 way_flags <- findBuildTag
103 -- if we're unregisterised, add some more flags
104 let unreg_flags | cGhcUnregisterised == "YES" = unregFlags
107 (more_leftover, errs) <- processArgs static_flags (unreg_flags ++ way_flags)
109 -- see sanity code in staticOpts
110 writeIORef v_opt_C_ready True
112 -- TABLES_NEXT_TO_CODE affects the info table layout.
113 -- Be careful to do this *after* all processArgs,
114 -- because evaluating tablesNextToCode involves looking at the global
115 -- static flags. Those pesky global variables...
116 let cg_flags | tablesNextToCode = ["-optc-DTABLES_NEXT_TO_CODE"]
119 -- HACK: -fexcess-precision is both a static and a dynamic flag. If
120 -- the static flag parser has slurped it, we must return it as a
121 -- leftover too. ToDo: make -fexcess-precision dynamic only.
122 let excess_prec | opt_SimplExcessPrecision = ["-fexcess-precision"]
125 when (not (null errs)) $ ghcError (UsageError (unlines errs))
126 return (excess_prec++cg_flags++more_leftover++leftover)
128 initStaticOpts :: IO ()
129 initStaticOpts = writeIORef v_opt_C_ready True
131 static_flags :: [(String, OptKind IO)]
132 -- All the static flags should appear in this list. It describes how each
133 -- static flag should be processed. Two main purposes:
134 -- (a) if a command-line flag doesn't appear in the list, GHC can complain
135 -- (b) a command-line flag may remove, or add, other flags; e.g. the "-fno-X" things
137 -- The common (PassFlag addOpt) action puts the static flag into the bunch of
138 -- things that are searched up by the top-level definitions like
139 -- opt_foo = lookUp (fsLit "-dfoo")
141 -- Note that ordering is important in the following list: any flag which
142 -- is a prefix flag (i.e. HasArg, Prefix, OptPrefix, AnySuffix) will override
143 -- flags further down the list with the same prefix.
146 ------- GHCi -------------------------------------------------------
147 ( "ignore-dot-ghci", PassFlag addOpt )
148 , ( "read-dot-ghci" , NoArg (removeOpt "-ignore-dot-ghci") )
150 ------- ways --------------------------------------------------------
151 , ( "prof" , NoArg (addWay WayProf) )
152 , ( "ticky" , NoArg (addWay WayTicky) )
153 , ( "parallel" , NoArg (addWay WayPar) )
154 , ( "gransim" , NoArg (addWay WayGran) )
155 , ( "smp" , NoArg (addWay WayThreaded) ) -- backwards compat.
156 , ( "debug" , NoArg (addWay WayDebug) )
157 , ( "ndp" , NoArg (addWay WayNDP) )
158 , ( "threaded" , NoArg (addWay WayThreaded) )
161 ------ Debugging ----------------------------------------------------
162 , ( "dppr-debug", PassFlag addOpt )
163 , ( "dsuppress-uniques", PassFlag addOpt )
164 , ( "dppr-user-length", AnySuffix addOpt )
165 -- rest of the debugging flags are dynamic
167 --------- Profiling --------------------------------------------------
168 , ( "auto-all" , NoArg (addOpt "-fauto-sccs-on-all-toplevs") )
169 , ( "auto" , NoArg (addOpt "-fauto-sccs-on-exported-toplevs") )
170 , ( "caf-all" , NoArg (addOpt "-fauto-sccs-on-individual-cafs") )
171 -- "ignore-sccs" doesn't work (ToDo)
173 , ( "no-auto-all" , NoArg (removeOpt "-fauto-sccs-on-all-toplevs") )
174 , ( "no-auto" , NoArg (removeOpt "-fauto-sccs-on-exported-toplevs") )
175 , ( "no-caf-all" , NoArg (removeOpt "-fauto-sccs-on-individual-cafs") )
177 ------- Miscellaneous -----------------------------------------------
178 , ( "no-link-chk" , NoArg (return ()) ) -- ignored for backwards compat
180 ----- Linker --------------------------------------------------------
181 , ( "static" , PassFlag addOpt )
182 , ( "dynamic" , NoArg (removeOpt "-static") )
183 , ( "rdynamic" , NoArg (return ()) ) -- ignored for compat w/ gcc
185 ----- RTS opts ------------------------------------------------------
186 , ( "H" , HasArg (setHeapSize . fromIntegral . decodeSize) )
187 , ( "Rghc-timing" , NoArg (enableTimingStats) )
189 ------ Compiler flags -----------------------------------------------
190 -- All other "-fno-<blah>" options cancel out "-f<blah>" on the hsc cmdline
191 , ( "fno-", PrefixPred (\s -> isStaticFlag ("f"++s))
192 (\s -> removeOpt ("-f"++s)) )
194 -- Pass all remaining "-f<blah>" options to hsc
195 , ( "f", AnySuffixPred (isStaticFlag) addOpt )
198 addOpt :: String -> IO ()
199 addOpt = consIORef v_opt_C
201 addWay :: WayName -> IO ()
202 addWay = consIORef v_Ways
204 removeOpt :: String -> IO ()
206 fs <- readIORef v_opt_C
207 writeIORef v_opt_C $! filter (/= f) fs
209 lookUp :: FastString -> Bool
210 lookup_def_int :: String -> Int -> Int
211 lookup_def_float :: String -> Float -> Float
212 lookup_str :: String -> Maybe String
214 -- holds the static opts while they're being collected, before
215 -- being unsafely read by unpacked_static_opts below.
216 GLOBAL_VAR(v_opt_C, defaultStaticOpts, [String])
217 GLOBAL_VAR(v_opt_C_ready, False, Bool)
219 staticFlags :: [String]
220 staticFlags = unsafePerformIO $ do
221 ready <- readIORef v_opt_C_ready
223 then panic "Static flags have not been initialised!\n Please call GHC.newSession or GHC.parseStaticFlags early enough."
224 else readIORef v_opt_C
226 -- -static is the default
227 defaultStaticOpts :: [String]
228 defaultStaticOpts = ["-static"]
230 packed_static_opts :: [FastString]
231 packed_static_opts = map mkFastString staticFlags
233 lookUp sw = sw `elem` packed_static_opts
235 -- (lookup_str "foo") looks for the flag -foo=X or -fooX,
236 -- and returns the string X
238 = case firstJust (map (startsWith sw) staticFlags) of
239 Just ('=' : str) -> Just str
243 lookup_def_int sw def = case (lookup_str sw) of
244 Nothing -> def -- Use default
245 Just xx -> try_read sw xx
247 lookup_def_float sw def = case (lookup_str sw) of
248 Nothing -> def -- Use default
249 Just xx -> try_read sw xx
252 try_read :: Read a => String -> String -> a
253 -- (try_read sw str) tries to read s; if it fails, it
254 -- bleats about flag sw
257 ((x,_):_) -> x -- Be forgiving: ignore trailing goop, and alternative parses
258 [] -> ghcError (UsageError ("Malformed argument " ++ str ++ " for flag " ++ sw))
259 -- ToDo: hack alert. We should really parse the arugments
260 -- and announce errors in a more civilised way.
264 Putting the compiler options into temporary at-files
265 may turn out to be necessary later on if we turn hsc into
266 a pure Win32 application where I think there's a command-line
267 length limit of 255. unpacked_opts understands the @ option.
269 unpacked_opts :: [String]
273 map unpackFS argv -- NOT ARGV any more: v_Static_hsc_opts
275 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
279 opt_IgnoreDotGhci :: Bool
280 opt_IgnoreDotGhci = lookUp (fsLit "-ignore-dot-ghci")
283 opt_SuppressUniques :: Bool
284 opt_SuppressUniques = lookUp (fsLit "-dsuppress-uniques")
285 opt_PprStyle_Debug :: Bool
286 opt_PprStyle_Debug = lookUp (fsLit "-dppr-debug")
287 opt_PprUserLength :: Int
288 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
291 opt_AutoSccsOnAllToplevs :: Bool
292 opt_AutoSccsOnAllToplevs = lookUp (fsLit "-fauto-sccs-on-all-toplevs")
293 opt_AutoSccsOnExportedToplevs :: Bool
294 opt_AutoSccsOnExportedToplevs = lookUp (fsLit "-fauto-sccs-on-exported-toplevs")
295 opt_AutoSccsOnIndividualCafs :: Bool
296 opt_AutoSccsOnIndividualCafs = lookUp (fsLit "-fauto-sccs-on-individual-cafs")
297 opt_SccProfilingOn :: Bool
298 opt_SccProfilingOn = lookUp (fsLit "-fscc-profiling")
299 opt_DoTickyProfiling :: Bool
300 opt_DoTickyProfiling = WayTicky `elem` (unsafePerformIO $ readIORef v_Ways)
304 opt_Hpc = lookUp (fsLit "-fhpc")
307 opt_DictsStrict :: Bool
308 opt_DictsStrict = lookUp (fsLit "-fdicts-strict")
309 opt_IrrefutableTuples :: Bool
310 opt_IrrefutableTuples = lookUp (fsLit "-firrefutable-tuples")
312 opt_Parallel = lookUp (fsLit "-fparallel")
315 opt_SpecInlineJoinPoints :: Bool
316 opt_SpecInlineJoinPoints = lookUp (fsLit "-fspec-inline-join-points")
317 opt_NoStateHack :: Bool
318 opt_NoStateHack = lookUp (fsLit "-fno-state-hack")
320 opt_CprOff = lookUp (fsLit "-fcpr-off")
321 -- Switch off CPR analysis in the new demand analyser
322 opt_MaxWorkerArgs :: Int
323 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
325 opt_GranMacros :: Bool
326 opt_GranMacros = lookUp (fsLit "-fgransim")
327 opt_HiVersion :: Integer
328 opt_HiVersion = read (cProjectVersionInt ++ cProjectPatchLevel) :: Integer
329 opt_HistorySize :: Int
330 opt_HistorySize = lookup_def_int "-fhistory-size" 20
331 opt_OmitBlackHoling :: Bool
332 opt_OmitBlackHoling = lookUp (fsLit "-dno-black-holing")
334 -- Simplifier switches
335 opt_SimplNoPreInlining :: Bool
336 opt_SimplNoPreInlining = lookUp (fsLit "-fno-pre-inlining")
337 -- NoPreInlining is there just to see how bad things
338 -- get if you don't do it!
339 opt_SimplExcessPrecision :: Bool
340 opt_SimplExcessPrecision = lookUp (fsLit "-fexcess-precision")
343 opt_UF_CreationThreshold :: Int
344 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
345 opt_UF_UseThreshold :: Int
346 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
347 opt_UF_FunAppDiscount :: Int
348 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
349 opt_UF_KeenessFactor :: Float
350 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
353 opt_UF_DearOp = ( 4 :: Int)
356 #if darwin_TARGET_OS && x86_64_TARGET_ARCH
359 opt_PIC = lookUp (fsLit "-fPIC")
362 opt_Static = lookUp (fsLit "-static")
363 opt_Unregisterised :: Bool
364 opt_Unregisterised = lookUp (fsLit "-funregisterised")
366 -- Derived, not a real option. Determines whether we will be compiling
367 -- info tables that reside just before the entry code, or with an
368 -- indirection to the entry code. See TABLES_NEXT_TO_CODE in
369 -- includes/InfoTables.h.
370 tablesNextToCode :: Bool
371 tablesNextToCode = not opt_Unregisterised
372 && cGhcEnableTablesNextToCode == "YES"
374 opt_EmitExternalCore :: Bool
375 opt_EmitExternalCore = lookUp (fsLit "-fext-core")
377 -- Include full span info in error messages, instead of just the start position.
378 opt_ErrorSpans :: Bool
379 opt_ErrorSpans = lookUp (fsLit "-ferror-spans")
382 -- object files and libraries to be linked in are collected here.
383 -- ToDo: perhaps this could be done without a global, it wasn't obvious
384 -- how to do it though --SDM.
385 GLOBAL_VAR(v_Ld_inputs, [], [String])
387 isStaticFlag :: String -> Bool
390 "fauto-sccs-on-all-toplevs",
391 "fauto-sccs-on-exported-toplevs",
392 "fauto-sccs-on-individual-cafs",
395 "fspec-inline-join-points",
396 "firrefutable-tuples",
399 "fno-hi-version-check",
401 "fno-method-sharing",
407 "fhardwire-lib-paths",
415 || any (`isPrefixOf` f) [
416 "fliberate-case-threshold",
419 "funfolding-creation-threshold",
420 "funfolding-use-threshold",
421 "funfolding-fun-discount",
422 "funfolding-keeness-factor"
427 -- Misc functions for command-line options
429 startsWith :: String -> String -> Maybe String
430 -- startsWith pfx (pfx++rest) = Just rest
432 startsWith [] str = Just str
433 startsWith (c:cs) (s:ss)
434 = if c /= s then Nothing else startsWith cs ss
435 startsWith _ [] = Nothing
438 -----------------------------------------------------------------------------
439 -- convert sizes like "3.5M" into integers
441 decodeSize :: String -> Integer
443 | c == "" = truncate n
444 | c == "K" || c == "k" = truncate (n * 1000)
445 | c == "M" || c == "m" = truncate (n * 1000 * 1000)
446 | c == "G" || c == "g" = truncate (n * 1000 * 1000 * 1000)
447 | otherwise = throwDyn (CmdLineError ("can't decode size: " ++ str))
448 where (m, c) = span pred str
450 pred c = isDigit c || c == '.'
453 -----------------------------------------------------------------------------
456 foreign import ccall unsafe "setHeapSize" setHeapSize :: Int -> IO ()
457 foreign import ccall unsafe "enableTimingStats" enableTimingStats :: IO ()
459 -----------------------------------------------------------------------------
462 -- The central concept of a "way" is that all objects in a given
463 -- program must be compiled in the same "way". Certain options change
464 -- parameters of the virtual machine, eg. profiling adds an extra word
465 -- to the object header, so profiling objects cannot be linked with
466 -- non-profiling objects.
468 -- After parsing the command-line options, we determine which "way" we
469 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
471 -- We then find the "build-tag" associated with this way, and this
472 -- becomes the suffix used to find .hi files and libraries used in
475 GLOBAL_VAR(v_Build_tag, "", String)
477 -- The RTS has its own build tag, because there are some ways that
478 -- affect the RTS only.
479 GLOBAL_VAR(v_RTS_Build_tag, "", String)
508 GLOBAL_VAR(v_Ways, [] ,[WayName])
510 allowed_combination :: [WayName] -> Bool
511 allowed_combination way = and [ x `allowedWith` y
512 | x <- way, y <- way, x < y ]
514 -- Note ordering in these tests: the left argument is
515 -- <= the right argument, according to the Ord instance
518 -- debug is allowed with everything
519 _ `allowedWith` WayDebug = True
520 WayDebug `allowedWith` _ = True
522 WayProf `allowedWith` WayNDP = True
523 WayThreaded `allowedWith` WayProf = True
524 _ `allowedWith` _ = False
527 findBuildTag :: IO [String] -- new options
529 way_names <- readIORef v_Ways
530 let ws = sort (nub way_names)
532 if not (allowed_combination ws)
533 then throwDyn (CmdLineError $
534 "combination not supported: " ++
535 foldr1 (\a b -> a ++ '/':b)
536 (map (wayName . lkupWay) ws))
537 else let ways = map lkupWay ws
538 tag = mkBuildTag (filter (not.wayRTSOnly) ways)
539 rts_tag = mkBuildTag ways
540 flags = map wayOpts ways
542 writeIORef v_Build_tag tag
543 writeIORef v_RTS_Build_tag rts_tag
544 return (concat flags)
548 mkBuildTag :: [Way] -> String
549 mkBuildTag ways = concat (intersperse "_" (map wayTag ways))
551 lkupWay :: WayName -> Way
553 case lookup w way_details of
554 Nothing -> error "findBuildTag"
555 Just details -> details
557 isRTSWay :: WayName -> Bool
558 isRTSWay = wayRTSOnly . lkupWay
567 way_details :: [ (WayName, Way) ]
569 [ (WayThreaded, Way "thr" True "Threaded" [
570 #if defined(freebsd_TARGET_OS)
573 -- FreeBSD's default threading library is the KSE-based M:N libpthread,
574 -- which GHC has some problems with. It's currently not clear whether
575 -- the problems are our fault or theirs, but it seems that using the
576 -- alternative 1:1 threading library libthr works around it:
578 #elif defined(solaris2_TARGET_OS)
583 (WayDebug, Way "debug" True "Debug" [] ),
585 (WayProf, Way "p" False "Profiling"
588 , "-optc-DPROFILING" ]),
590 (WayTicky, Way "t" True "Ticky-ticky Profiling"
592 , "-optc-DTICKY_TICKY" ]),
594 -- optl's below to tell linker where to find the PVM library -- HWL
595 (WayPar, Way "mp" False "Parallel"
597 , "-D__PARALLEL_HASKELL__"
599 , "-package concurrent"
601 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
605 -- at the moment we only change the RTS and could share compiler and libs!
606 (WayPar, Way "mt" False "Parallel ticky profiling"
608 , "-D__PARALLEL_HASKELL__"
611 , "-package concurrent"
613 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
617 (WayPar, Way "md" False "Distributed"
619 , "-D__PARALLEL_HASKELL__"
620 , "-D__DISTRIBUTED_HASKELL__"
623 , "-package concurrent"
625 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
629 (WayGran, Way "mg" False "GranSim"
633 , "-package concurrent" ]),
635 (WayNDP, Way "ndp" False "Nested data parallelism"
639 (WayUser_a, Way "a" False "User way 'a'" ["$WAY_a_REAL_OPTS"]),
640 (WayUser_b, Way "b" False "User way 'b'" ["$WAY_b_REAL_OPTS"]),
641 (WayUser_c, Way "c" False "User way 'c'" ["$WAY_c_REAL_OPTS"]),
642 (WayUser_d, Way "d" False "User way 'd'" ["$WAY_d_REAL_OPTS"]),
643 (WayUser_e, Way "e" False "User way 'e'" ["$WAY_e_REAL_OPTS"]),
644 (WayUser_f, Way "f" False "User way 'f'" ["$WAY_f_REAL_OPTS"]),
645 (WayUser_g, Way "g" False "User way 'g'" ["$WAY_g_REAL_OPTS"]),
646 (WayUser_h, Way "h" False "User way 'h'" ["$WAY_h_REAL_OPTS"]),
647 (WayUser_i, Way "i" False "User way 'i'" ["$WAY_i_REAL_OPTS"]),
648 (WayUser_j, Way "j" False "User way 'j'" ["$WAY_j_REAL_OPTS"]),
649 (WayUser_k, Way "k" False "User way 'k'" ["$WAY_k_REAL_OPTS"]),
650 (WayUser_l, Way "l" False "User way 'l'" ["$WAY_l_REAL_OPTS"]),
651 (WayUser_m, Way "m" False "User way 'm'" ["$WAY_m_REAL_OPTS"]),
652 (WayUser_n, Way "n" False "User way 'n'" ["$WAY_n_REAL_OPTS"]),
653 (WayUser_o, Way "o" False "User way 'o'" ["$WAY_o_REAL_OPTS"]),
654 (WayUser_A, Way "A" False "User way 'A'" ["$WAY_A_REAL_OPTS"]),
655 (WayUser_B, Way "B" False "User way 'B'" ["$WAY_B_REAL_OPTS"])
658 unregFlags :: [String]
661 , "-optc-DUSE_MINIINTERPRETER"
662 , "-fno-asm-mangling"