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,
44 opt_SpecInlineJoinPoints,
46 opt_SimplNoPreInlining,
47 opt_SimplExcessPrecision,
51 opt_UF_CreationThreshold,
53 opt_UF_FunAppDiscount,
74 #include "HsVersions.h"
78 import FastString ( FastString, mkFastString )
80 import Maybes ( firstJust )
83 import Control.Exception ( throwDyn )
85 import System.IO.Unsafe ( unsafePerformIO )
86 import Control.Monad ( when )
87 import Data.Char ( isDigit )
90 -----------------------------------------------------------------------------
93 parseStaticFlags :: [String] -> IO [String]
94 parseStaticFlags args = do
95 ready <- readIORef v_opt_C_ready
96 when ready $ throwDyn (ProgramError "Too late for parseStaticFlags: call it before newSession")
98 (leftover, errs) <- processArgs static_flags args
99 when (not (null errs)) $ throwDyn (UsageError (unlines errs))
101 -- deal with the way flags: the way (eg. prof) gives rise to
102 -- further flags, some of which might be static.
103 way_flags <- findBuildTag
105 -- if we're unregisterised, add some more flags
106 let unreg_flags | cGhcUnregisterised == "YES" = unregFlags
109 (more_leftover, errs) <- processArgs static_flags (unreg_flags ++ way_flags)
111 -- see sanity code in staticOpts
112 writeIORef v_opt_C_ready True
114 -- TABLES_NEXT_TO_CODE affects the info table layout.
115 -- Be careful to do this *after* all processArgs,
116 -- because evaluating tablesNextToCode involves looking at the global
117 -- static flags. Those pesky global variables...
118 let cg_flags | tablesNextToCode = ["-optc-DTABLES_NEXT_TO_CODE"]
121 -- HACK: -fexcess-precision is both a static and a dynamic flag. If
122 -- the static flag parser has slurped it, we must return it as a
123 -- leftover too. ToDo: make -fexcess-precision dynamic only.
124 let excess_prec | opt_SimplExcessPrecision = ["-fexcess-precision"]
127 when (not (null errs)) $ ghcError (UsageError (unlines errs))
128 return (excess_prec++cg_flags++more_leftover++leftover)
130 initStaticOpts :: IO ()
131 initStaticOpts = writeIORef v_opt_C_ready True
133 static_flags :: [(String, OptKind IO)]
134 -- All the static flags should appear in this list. It describes how each
135 -- static flag should be processed. Two main purposes:
136 -- (a) if a command-line flag doesn't appear in the list, GHC can complain
137 -- (b) a command-line flag may remove, or add, other flags; e.g. the "-fno-X" things
139 -- The common (PassFlag addOpt) action puts the static flag into the bunch of
140 -- things that are searched up by the top-level definitions like
141 -- opt_foo = lookUp FSLIT("-dfoo")
143 -- Note that ordering is important in the following list: any flag which
144 -- is a prefix flag (i.e. HasArg, Prefix, OptPrefix, AnySuffix) will override
145 -- flags further down the list with the same prefix.
148 ------- GHCi -------------------------------------------------------
149 ( "ignore-dot-ghci", PassFlag addOpt )
150 , ( "read-dot-ghci" , NoArg (removeOpt "-ignore-dot-ghci") )
152 ------- ways --------------------------------------------------------
153 , ( "prof" , NoArg (addWay WayProf) )
154 , ( "ticky" , NoArg (addWay WayTicky) )
155 , ( "parallel" , NoArg (addWay WayPar) )
156 , ( "gransim" , NoArg (addWay WayGran) )
157 , ( "smp" , NoArg (addWay WayThreaded) ) -- backwards compat.
158 , ( "debug" , NoArg (addWay WayDebug) )
159 , ( "ndp" , NoArg (addWay WayNDP) )
160 , ( "threaded" , NoArg (addWay WayThreaded) )
163 ------ Debugging ----------------------------------------------------
164 , ( "dppr-debug", PassFlag addOpt )
165 , ( "dsuppress-uniques", PassFlag addOpt )
166 , ( "dppr-user-length", AnySuffix addOpt )
167 -- rest of the debugging flags are dynamic
169 --------- Profiling --------------------------------------------------
170 , ( "auto-all" , NoArg (addOpt "-fauto-sccs-on-all-toplevs") )
171 , ( "auto" , NoArg (addOpt "-fauto-sccs-on-exported-toplevs") )
172 , ( "caf-all" , NoArg (addOpt "-fauto-sccs-on-individual-cafs") )
173 -- "ignore-sccs" doesn't work (ToDo)
175 , ( "no-auto-all" , NoArg (removeOpt "-fauto-sccs-on-all-toplevs") )
176 , ( "no-auto" , NoArg (removeOpt "-fauto-sccs-on-exported-toplevs") )
177 , ( "no-caf-all" , NoArg (removeOpt "-fauto-sccs-on-individual-cafs") )
179 ------- Miscellaneous -----------------------------------------------
180 , ( "no-link-chk" , NoArg (return ()) ) -- ignored for backwards compat
182 ----- Linker --------------------------------------------------------
183 , ( "static" , PassFlag addOpt )
184 , ( "dynamic" , NoArg (removeOpt "-static") )
185 , ( "rdynamic" , NoArg (return ()) ) -- ignored for compat w/ gcc
187 ----- RTS opts ------------------------------------------------------
188 , ( "H" , HasArg (setHeapSize . fromIntegral . decodeSize) )
189 , ( "Rghc-timing" , NoArg (enableTimingStats) )
191 ------ Compiler flags -----------------------------------------------
192 -- All other "-fno-<blah>" options cancel out "-f<blah>" on the hsc cmdline
193 , ( "fno-", PrefixPred (\s -> isStaticFlag ("f"++s))
194 (\s -> removeOpt ("-f"++s)) )
196 -- Pass all remaining "-f<blah>" options to hsc
197 , ( "f", AnySuffixPred (isStaticFlag) addOpt )
200 addOpt :: String -> IO ()
201 addOpt = consIORef v_opt_C
203 addWay :: WayName -> IO ()
204 addWay = consIORef v_Ways
206 removeOpt :: String -> IO ()
208 fs <- readIORef v_opt_C
209 writeIORef v_opt_C $! filter (/= f) fs
211 lookUp :: FastString -> Bool
212 lookup_def_int :: String -> Int -> Int
213 lookup_def_float :: String -> Float -> Float
214 lookup_str :: String -> Maybe String
216 -- holds the static opts while they're being collected, before
217 -- being unsafely read by unpacked_static_opts below.
218 GLOBAL_VAR(v_opt_C, defaultStaticOpts, [String])
219 GLOBAL_VAR(v_opt_C_ready, False, Bool)
221 staticFlags :: [String]
222 staticFlags = unsafePerformIO $ do
223 ready <- readIORef v_opt_C_ready
225 then panic "Static flags have not been initialised!\n Please call GHC.newSession or GHC.parseStaticFlags early enough."
226 else readIORef v_opt_C
228 -- -static is the default
229 defaultStaticOpts :: [String]
230 defaultStaticOpts = ["-static"]
232 packed_static_opts :: [FastString]
233 packed_static_opts = map mkFastString staticFlags
235 lookUp sw = sw `elem` packed_static_opts
237 -- (lookup_str "foo") looks for the flag -foo=X or -fooX,
238 -- and returns the string X
240 = case firstJust (map (startsWith sw) staticFlags) of
241 Just ('=' : str) -> Just str
245 lookup_def_int sw def = case (lookup_str sw) of
246 Nothing -> def -- Use default
247 Just xx -> try_read sw xx
249 lookup_def_float sw def = case (lookup_str sw) of
250 Nothing -> def -- Use default
251 Just xx -> try_read sw xx
254 try_read :: Read a => String -> String -> a
255 -- (try_read sw str) tries to read s; if it fails, it
256 -- bleats about flag sw
259 ((x,_):_) -> x -- Be forgiving: ignore trailing goop, and alternative parses
260 [] -> ghcError (UsageError ("Malformed argument " ++ str ++ " for flag " ++ sw))
261 -- ToDo: hack alert. We should really parse the arugments
262 -- and announce errors in a more civilised way.
266 Putting the compiler options into temporary at-files
267 may turn out to be necessary later on if we turn hsc into
268 a pure Win32 application where I think there's a command-line
269 length limit of 255. unpacked_opts understands the @ option.
271 unpacked_opts :: [String]
275 map unpackFS argv -- NOT ARGV any more: v_Static_hsc_opts
277 expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
281 opt_IgnoreDotGhci :: Bool
282 opt_IgnoreDotGhci = lookUp FSLIT("-ignore-dot-ghci")
285 opt_SuppressUniques :: Bool
286 opt_SuppressUniques = lookUp FSLIT("-dsuppress-uniques")
287 opt_PprStyle_Debug :: Bool
288 opt_PprStyle_Debug = lookUp FSLIT("-dppr-debug")
289 opt_PprUserLength :: Int
290 opt_PprUserLength = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
293 opt_AutoSccsOnAllToplevs :: Bool
294 opt_AutoSccsOnAllToplevs = lookUp FSLIT("-fauto-sccs-on-all-toplevs")
295 opt_AutoSccsOnExportedToplevs :: Bool
296 opt_AutoSccsOnExportedToplevs = lookUp FSLIT("-fauto-sccs-on-exported-toplevs")
297 opt_AutoSccsOnIndividualCafs :: Bool
298 opt_AutoSccsOnIndividualCafs = lookUp FSLIT("-fauto-sccs-on-individual-cafs")
299 opt_SccProfilingOn :: Bool
300 opt_SccProfilingOn = lookUp FSLIT("-fscc-profiling")
301 opt_DoTickyProfiling :: Bool
302 opt_DoTickyProfiling = WayTicky `elem` (unsafePerformIO $ readIORef v_Ways)
306 opt_Hpc = lookUp FSLIT("-fhpc")
309 opt_DictsStrict :: Bool
310 opt_DictsStrict = lookUp FSLIT("-fdicts-strict")
311 opt_IrrefutableTuples :: Bool
312 opt_IrrefutableTuples = lookUp FSLIT("-firrefutable-tuples")
314 opt_Parallel = lookUp FSLIT("-fparallel")
317 opt_SpecInlineJoinPoints :: Bool
318 opt_SpecInlineJoinPoints = lookUp FSLIT("-fspec-inline-join-points")
319 opt_NoStateHack :: Bool
320 opt_NoStateHack = lookUp FSLIT("-fno-state-hack")
321 opt_NoMethodSharing :: Bool
322 opt_NoMethodSharing = lookUp FSLIT("-fno-method-sharing")
324 opt_CprOff = lookUp FSLIT("-fcpr-off")
325 -- Switch off CPR analysis in the new demand analyser
326 opt_MaxWorkerArgs :: Int
327 opt_MaxWorkerArgs = lookup_def_int "-fmax-worker-args" (10::Int)
329 opt_GranMacros :: Bool
330 opt_GranMacros = lookUp FSLIT("-fgransim")
331 opt_HiVersion :: Integer
332 opt_HiVersion = read (cProjectVersionInt ++ cProjectPatchLevel) :: Integer
333 opt_HistorySize :: Int
334 opt_HistorySize = lookup_def_int "-fhistory-size" 20
335 opt_OmitBlackHoling :: Bool
336 opt_OmitBlackHoling = lookUp FSLIT("-dno-black-holing")
337 opt_RuntimeTypes :: Bool
338 opt_RuntimeTypes = lookUp FSLIT("-fruntime-types")
340 -- Simplifier switches
341 opt_SimplNoPreInlining :: Bool
342 opt_SimplNoPreInlining = lookUp FSLIT("-fno-pre-inlining")
343 -- NoPreInlining is there just to see how bad things
344 -- get if you don't do it!
345 opt_SimplExcessPrecision :: Bool
346 opt_SimplExcessPrecision = lookUp FSLIT("-fexcess-precision")
349 opt_UF_CreationThreshold :: Int
350 opt_UF_CreationThreshold = lookup_def_int "-funfolding-creation-threshold" (45::Int)
351 opt_UF_UseThreshold :: Int
352 opt_UF_UseThreshold = lookup_def_int "-funfolding-use-threshold" (8::Int) -- Discounts can be big
353 opt_UF_FunAppDiscount :: Int
354 opt_UF_FunAppDiscount = lookup_def_int "-funfolding-fun-discount" (6::Int) -- It's great to inline a fn
355 opt_UF_KeenessFactor :: Float
356 opt_UF_KeenessFactor = lookup_def_float "-funfolding-keeness-factor" (1.5::Float)
359 opt_UF_DearOp = ( 4 :: Int)
362 #if darwin_TARGET_OS && x86_64_TARGET_ARCH
365 opt_PIC = lookUp FSLIT("-fPIC")
368 opt_Static = lookUp FSLIT("-static")
369 opt_Unregisterised :: Bool
370 opt_Unregisterised = lookUp FSLIT("-funregisterised")
372 -- Derived, not a real option. Determines whether we will be compiling
373 -- info tables that reside just before the entry code, or with an
374 -- indirection to the entry code. See TABLES_NEXT_TO_CODE in
375 -- includes/InfoTables.h.
376 tablesNextToCode :: Bool
377 tablesNextToCode = not opt_Unregisterised
378 && cGhcEnableTablesNextToCode == "YES"
380 opt_EmitExternalCore :: Bool
381 opt_EmitExternalCore = lookUp FSLIT("-fext-core")
383 -- Include full span info in error messages, instead of just the start position.
384 opt_ErrorSpans :: Bool
385 opt_ErrorSpans = lookUp FSLIT("-ferror-spans")
388 -- object files and libraries to be linked in are collected here.
389 -- ToDo: perhaps this could be done without a global, it wasn't obvious
390 -- how to do it though --SDM.
391 GLOBAL_VAR(v_Ld_inputs, [], [String])
393 isStaticFlag :: String -> Bool
396 "fauto-sccs-on-all-toplevs",
397 "fauto-sccs-on-exported-toplevs",
398 "fauto-sccs-on-individual-cafs",
401 "fspec-inline-join-points",
402 "firrefutable-tuples",
405 "fno-hi-version-check",
407 "fno-method-sharing",
413 "fhardwire-lib-paths",
421 || any (`isPrefixOf` f) [
422 "fliberate-case-threshold",
425 "funfolding-creation-threshold",
426 "funfolding-use-threshold",
427 "funfolding-fun-discount",
428 "funfolding-keeness-factor"
433 -- Misc functions for command-line options
435 startsWith :: String -> String -> Maybe String
436 -- startsWith pfx (pfx++rest) = Just rest
438 startsWith [] str = Just str
439 startsWith (c:cs) (s:ss)
440 = if c /= s then Nothing else startsWith cs ss
441 startsWith _ [] = Nothing
444 -----------------------------------------------------------------------------
445 -- convert sizes like "3.5M" into integers
447 decodeSize :: String -> Integer
449 | c == "" = truncate n
450 | c == "K" || c == "k" = truncate (n * 1000)
451 | c == "M" || c == "m" = truncate (n * 1000 * 1000)
452 | c == "G" || c == "g" = truncate (n * 1000 * 1000 * 1000)
453 | otherwise = throwDyn (CmdLineError ("can't decode size: " ++ str))
454 where (m, c) = span pred str
456 pred c = isDigit c || c == '.'
459 -----------------------------------------------------------------------------
462 foreign import ccall unsafe "setHeapSize" setHeapSize :: Int -> IO ()
463 foreign import ccall unsafe "enableTimingStats" enableTimingStats :: IO ()
465 -----------------------------------------------------------------------------
468 -- The central concept of a "way" is that all objects in a given
469 -- program must be compiled in the same "way". Certain options change
470 -- parameters of the virtual machine, eg. profiling adds an extra word
471 -- to the object header, so profiling objects cannot be linked with
472 -- non-profiling objects.
474 -- After parsing the command-line options, we determine which "way" we
475 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
477 -- We then find the "build-tag" associated with this way, and this
478 -- becomes the suffix used to find .hi files and libraries used in
481 GLOBAL_VAR(v_Build_tag, "", String)
483 -- The RTS has its own build tag, because there are some ways that
484 -- affect the RTS only.
485 GLOBAL_VAR(v_RTS_Build_tag, "", String)
514 GLOBAL_VAR(v_Ways, [] ,[WayName])
516 allowed_combination :: [WayName] -> Bool
517 allowed_combination way = and [ x `allowedWith` y
518 | x <- way, y <- way, x < y ]
520 -- Note ordering in these tests: the left argument is
521 -- <= the right argument, according to the Ord instance
524 -- debug is allowed with everything
525 _ `allowedWith` WayDebug = True
526 WayDebug `allowedWith` _ = True
528 WayProf `allowedWith` WayNDP = True
529 WayThreaded `allowedWith` WayProf = True
530 _ `allowedWith` _ = False
533 findBuildTag :: IO [String] -- new options
535 way_names <- readIORef v_Ways
536 let ws = sort (nub way_names)
538 if not (allowed_combination ws)
539 then throwDyn (CmdLineError $
540 "combination not supported: " ++
541 foldr1 (\a b -> a ++ '/':b)
542 (map (wayName . lkupWay) ws))
543 else let ways = map lkupWay ws
544 tag = mkBuildTag (filter (not.wayRTSOnly) ways)
545 rts_tag = mkBuildTag ways
546 flags = map wayOpts ways
548 writeIORef v_Build_tag tag
549 writeIORef v_RTS_Build_tag rts_tag
550 return (concat flags)
554 mkBuildTag :: [Way] -> String
555 mkBuildTag ways = concat (intersperse "_" (map wayTag ways))
557 lkupWay :: WayName -> Way
559 case lookup w way_details of
560 Nothing -> error "findBuildTag"
561 Just details -> details
563 isRTSWay :: WayName -> Bool
564 isRTSWay = wayRTSOnly . lkupWay
573 way_details :: [ (WayName, Way) ]
575 [ (WayThreaded, Way "thr" True "Threaded" [
576 #if defined(freebsd_TARGET_OS)
579 -- FreeBSD's default threading library is the KSE-based M:N libpthread,
580 -- which GHC has some problems with. It's currently not clear whether
581 -- the problems are our fault or theirs, but it seems that using the
582 -- alternative 1:1 threading library libthr works around it:
584 #elif defined(solaris2_TARGET_OS)
589 (WayDebug, Way "debug" True "Debug" [] ),
591 (WayProf, Way "p" False "Profiling"
594 , "-optc-DPROFILING" ]),
596 (WayTicky, Way "t" True "Ticky-ticky Profiling"
598 , "-optc-DTICKY_TICKY" ]),
600 -- optl's below to tell linker where to find the PVM library -- HWL
601 (WayPar, Way "mp" False "Parallel"
603 , "-D__PARALLEL_HASKELL__"
605 , "-package concurrent"
607 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
611 -- at the moment we only change the RTS and could share compiler and libs!
612 (WayPar, Way "mt" False "Parallel ticky profiling"
614 , "-D__PARALLEL_HASKELL__"
617 , "-package concurrent"
619 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
623 (WayPar, Way "md" False "Distributed"
625 , "-D__PARALLEL_HASKELL__"
626 , "-D__DISTRIBUTED_HASKELL__"
629 , "-package concurrent"
631 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
635 (WayGran, Way "mg" False "GranSim"
639 , "-package concurrent" ]),
641 (WayNDP, Way "ndp" False "Nested data parallelism"
645 (WayUser_a, Way "a" False "User way 'a'" ["$WAY_a_REAL_OPTS"]),
646 (WayUser_b, Way "b" False "User way 'b'" ["$WAY_b_REAL_OPTS"]),
647 (WayUser_c, Way "c" False "User way 'c'" ["$WAY_c_REAL_OPTS"]),
648 (WayUser_d, Way "d" False "User way 'd'" ["$WAY_d_REAL_OPTS"]),
649 (WayUser_e, Way "e" False "User way 'e'" ["$WAY_e_REAL_OPTS"]),
650 (WayUser_f, Way "f" False "User way 'f'" ["$WAY_f_REAL_OPTS"]),
651 (WayUser_g, Way "g" False "User way 'g'" ["$WAY_g_REAL_OPTS"]),
652 (WayUser_h, Way "h" False "User way 'h'" ["$WAY_h_REAL_OPTS"]),
653 (WayUser_i, Way "i" False "User way 'i'" ["$WAY_i_REAL_OPTS"]),
654 (WayUser_j, Way "j" False "User way 'j'" ["$WAY_j_REAL_OPTS"]),
655 (WayUser_k, Way "k" False "User way 'k'" ["$WAY_k_REAL_OPTS"]),
656 (WayUser_l, Way "l" False "User way 'l'" ["$WAY_l_REAL_OPTS"]),
657 (WayUser_m, Way "m" False "User way 'm'" ["$WAY_m_REAL_OPTS"]),
658 (WayUser_n, Way "n" False "User way 'n'" ["$WAY_n_REAL_OPTS"]),
659 (WayUser_o, Way "o" False "User way 'o'" ["$WAY_o_REAL_OPTS"]),
660 (WayUser_A, Way "A" False "User way 'A'" ["$WAY_A_REAL_OPTS"]),
661 (WayUser_B, Way "B" False "User way 'B'" ["$WAY_B_REAL_OPTS"])
664 unregFlags :: [String]
667 , "-optc-DUSE_MINIINTERPRETER"
668 , "-fno-asm-mangling"