1 -----------------------------------------------------------------------------
3 -- Settings for the driver
5 -- (c) The University of Glasgow 2002
7 -----------------------------------------------------------------------------
9 module DriverState where
11 #include "HsVersions.h"
20 import DATA_IOREF ( IORef, readIORef, writeIORef )
26 import Maybe ( fromJust, isJust )
27 import Directory ( doesDirectoryExist )
29 -----------------------------------------------------------------------------
30 -- non-configured things
32 cHaskell1Version = "5" -- i.e., Haskell 98
34 -----------------------------------------------------------------------------
35 -- GHC modes of operation
38 = DoMkDependHS -- ghc -M
39 | StopBefore Phase -- ghc -E | -C | -S
40 -- StopBefore StopLn is the default
41 | DoMake -- ghc --make
42 | DoInteractive -- ghc --interactive
43 | DoEval String -- ghc -e
46 data GhcLink -- What to do in the link step
47 = -- Only relevant for modes
48 -- DoMake and StopBefore StopLn
49 NoLink -- Don't link at all
50 | StaticLink -- Ordinary linker [the default]
53 GLOBAL_VAR(v_GhcMode, StopBefore StopLn, GhcMode)
54 GLOBAL_VAR(v_GhcModeFlag, "", String)
55 GLOBAL_VAR(v_GhcLink, StaticLink, GhcLink)
57 setMode :: GhcMode -> String -> IO ()
59 old_mode <- readIORef v_GhcMode
60 old_flag <- readIORef v_GhcModeFlag
61 when (notNull old_flag && flag /= old_flag) $
63 ("cannot use `" ++ old_flag ++ "' with `" ++ flag ++ "'"))
64 writeIORef v_GhcMode m
65 writeIORef v_GhcModeFlag flag
67 isInteractiveMode, isInterpretiveMode :: GhcMode -> Bool
68 isMakeMode, isLinkMode, isCompManagerMode :: GhcMode -> Bool
70 isInteractiveMode DoInteractive = True
71 isInteractiveMode _ = False
73 -- isInterpretiveMode: byte-code compiler involved
74 isInterpretiveMode DoInteractive = True
75 isInterpretiveMode (DoEval _) = True
76 isInterpretiveMode _ = False
78 isMakeMode DoMake = True
81 isLinkMode (StopBefore p) = True
82 isLinkMode DoMake = True
85 isCompManagerMode DoMake = True
86 isCompManagerMode DoInteractive = True
87 isCompManagerMode (DoEval _) = True
88 isCompManagerMode _ = False
90 isNoLink :: GhcLink -> Bool
91 isNoLink NoLink = True
92 isNoLink other = False
94 -----------------------------------------------------------------------------
95 -- Global compilation flags
97 -- Default CPP defines in Haskell source
99 [ "-D__HASKELL1__="++cHaskell1Version
100 , "-D__GLASGOW_HASKELL__="++cProjectVersionInt
102 , "-D__CONCURRENT_HASKELL__"
106 -- Keep output from intermediate phases
107 GLOBAL_VAR(v_Keep_hi_diffs, False, Bool)
108 GLOBAL_VAR(v_Keep_hc_files, False, Bool)
109 GLOBAL_VAR(v_Keep_s_files, False, Bool)
110 GLOBAL_VAR(v_Keep_raw_s_files, False, Bool)
111 GLOBAL_VAR(v_Keep_tmp_files, False, Bool)
113 GLOBAL_VAR(v_Keep_il_files, False, Bool)
114 GLOBAL_VAR(v_Keep_ilx_files, False, Bool)
118 GLOBAL_VAR(v_Scale_sizes_by, 1.0, Double)
119 GLOBAL_VAR(v_Static, True, Bool)
120 GLOBAL_VAR(v_NoHsMain, False, Bool)
121 GLOBAL_VAR(v_MainModIs, Nothing, Maybe String)
122 GLOBAL_VAR(v_MainFunIs, Nothing, Maybe String)
123 GLOBAL_VAR(v_Collect_ghc_timing, False, Bool)
124 GLOBAL_VAR(v_Do_asm_mangling, True, Bool)
125 GLOBAL_VAR(v_Excess_precision, False, Bool)
126 GLOBAL_VAR(v_Read_DotGHCi, True, Bool)
128 -- Preprocessor flags
129 GLOBAL_VAR(v_Hs_source_pp_opts, [], [String])
131 -----------------------------------------------------------------------------
132 -- Splitting object files (for libraries)
134 GLOBAL_VAR(v_Split_object_files, False, Bool)
135 GLOBAL_VAR(v_Split_info, ("",0), (String,Int))
136 -- The split prefix and number of files
141 #if defined(i386_TARGET_ARCH) \
142 || defined(alpha_TARGET_ARCH) \
143 || defined(hppa_TARGET_ARCH) \
144 || defined(m68k_TARGET_ARCH) \
145 || defined(mips_TARGET_ARCH) \
146 || defined(powerpc_TARGET_ARCH) \
147 || defined(rs6000_TARGET_ARCH) \
148 || defined(sparc_TARGET_ARCH)
154 -----------------------------------------------------------------------------
155 -- Compiler output options
157 GLOBAL_VAR(v_Output_dir, Nothing, Maybe String)
158 GLOBAL_VAR(v_Output_file, Nothing, Maybe String)
159 GLOBAL_VAR(v_Output_hi, Nothing, Maybe String)
161 -- called to verify that the output files & directories
162 -- point somewhere valid.
164 -- The assumption is that the directory portion of these output
165 -- options will have to exist by the time 'verifyOutputFiles'
168 verifyOutputFiles :: IO ()
169 verifyOutputFiles = do
170 odir <- readIORef v_Output_dir
171 when (isJust odir) $ do
172 let dir = fromJust odir
173 flg <- doesDirectoryExist dir
174 when (not flg) (nonExistentDir "-odir" dir)
175 ofile <- readIORef v_Output_file
176 when (isJust ofile) $ do
177 let fn = fromJust ofile
178 flg <- doesDirNameExist fn
179 when (not flg) (nonExistentDir "-o" fn)
180 ohi <- readIORef v_Output_hi
181 when (isJust ohi) $ do
182 let hi = fromJust ohi
183 flg <- doesDirNameExist hi
184 when (not flg) (nonExistentDir "-ohi" hi)
186 nonExistentDir flg dir =
187 throwDyn (CmdLineError ("error: directory portion of " ++
188 show dir ++ " does not exist (used with " ++
189 show flg ++ " option.)"))
191 GLOBAL_VAR(v_Object_suf, phaseInputExt StopLn, String)
192 GLOBAL_VAR(v_HC_suf, phaseInputExt HCc, String)
193 GLOBAL_VAR(v_Hi_dir, Nothing, Maybe String)
194 GLOBAL_VAR(v_Hi_suf, "hi", String)
196 GLOBAL_VAR(v_Ld_inputs, [], [String])
198 odir_ify :: String -> IO String
200 odir_opt <- readIORef v_Output_dir
203 Just d -> return (replaceFilenameDirectory f d)
205 osuf_ify :: String -> IO String
207 osuf <- readIORef v_Object_suf
208 return (replaceFilenameSuffix f osuf)
210 GLOBAL_VAR(v_StgStats, False, Bool)
212 buildStgToDo :: IO [ StgToDo ]
214 stg_stats <- readIORef v_StgStats
215 let flags1 | stg_stats = [ D_stg_stats ]
219 ways_ <- readIORef v_Ways
220 let flags2 | WayProf `elem` ways_ = StgDoMassageForProfiling : flags1
225 -----------------------------------------------------------------------------
228 split_marker = ':' -- not configurable (ToDo)
230 v_Include_paths, v_Library_paths :: IORef [String]
231 GLOBAL_VAR(v_Include_paths, [], [String])
232 GLOBAL_VAR(v_Library_paths, [], [String])
234 #ifdef darwin_TARGET_OS
235 GLOBAL_VAR(v_Framework_paths, [], [String])
236 GLOBAL_VAR(v_Cmdline_frameworks, [], [String])
239 addToDirList :: IORef [String] -> String -> IO ()
240 addToDirList ref path
241 = do paths <- readIORef ref
242 shiny_new_ones <- splitPathList path
243 writeIORef ref (paths ++ shiny_new_ones)
246 splitPathList :: String -> IO [String]
247 splitPathList s = do ps <- splitUp s; return (filter notNull ps)
248 -- empty paths are ignored: there might be a trailing
249 -- ':' in the initial list, for example. Empty paths can
250 -- cause confusion when they are translated into -I options
251 -- for passing to gcc.
253 #ifdef mingw32_TARGET_OS
254 -- 'hybrid' support for DOS-style paths in directory lists.
256 -- That is, if "foo:bar:baz" is used, this interpreted as
257 -- consisting of three entries, 'foo', 'bar', 'baz'.
258 -- However, with "c:/foo:c:\\foo;x:/bar", this is interpreted
259 -- as four elts, "c:/foo", "c:\\foo", "x", and "/bar" --
260 -- *provided* c:/foo exists and x:/bar doesn't.
262 -- Notice that no attempt is made to fully replace the 'standard'
263 -- split marker ':' with the Windows / DOS one, ';'. The reason being
264 -- that this will cause too much breakage for users & ':' will
265 -- work fine even with DOS paths, if you're not insisting on being silly.
267 splitUp [] = return []
268 splitUp (x:':':div:xs)
269 | div `elem` dir_markers = do
270 let (p,rs) = findNextPath xs
273 Consult the file system to check the interpretation
274 of (x:':':div:p) -- this is arguably excessive, we
275 could skip this test & just say that it is a valid
278 flg <- doesDirectoryExist (x:':':div:p)
280 return ((x:':':div:p):ps)
282 return ([x]:(div:p):ps)
284 let (p,rs) = findNextPath xs
291 -- will be called either when we've consumed nought or the "<Drive>:/" part of
292 -- a DOS path, so splitting is just a Q of finding the next split marker.
294 case break (`elem` split_markers) xs of
298 split_markers :: [Char]
299 split_markers = [':', ';']
301 dir_markers :: [Char]
302 dir_markers = ['/', '\\']
305 splitUp xs = return (split split_marker xs)
308 -----------------------------------------------------------------------------
311 -- The central concept of a "way" is that all objects in a given
312 -- program must be compiled in the same "way". Certain options change
313 -- parameters of the virtual machine, eg. profiling adds an extra word
314 -- to the object header, so profiling objects cannot be linked with
315 -- non-profiling objects.
317 -- After parsing the command-line options, we determine which "way" we
318 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
320 -- We then find the "build-tag" associated with this way, and this
321 -- becomes the suffix used to find .hi files and libraries used in
324 GLOBAL_VAR(v_Build_tag, "", String)
326 -- The RTS has its own build tag, because there are some ways that
327 -- affect the RTS only.
328 GLOBAL_VAR(v_RTS_Build_tag, "", String)
359 GLOBAL_VAR(v_Ways, [] ,[WayName])
361 allowed_combination way = and [ x `allowedWith` y
362 | x <- way, y <- way, x < y ]
364 -- Note ordering in these tests: the left argument is
365 -- <= the right argument, according to the Ord instance
368 -- debug is allowed with everything
369 _ `allowedWith` WayDebug = True
370 WayDebug `allowedWith` _ = True
372 WayThreaded `allowedWith` WayProf = True
373 WayProf `allowedWith` WayUnreg = True
374 WayProf `allowedWith` WaySMP = True
375 WayProf `allowedWith` WayNDP = True
376 _ `allowedWith` _ = False
379 findBuildTag :: IO [String] -- new options
381 way_names <- readIORef v_Ways
382 let ws = sort way_names
383 if not (allowed_combination ws)
384 then throwDyn (CmdLineError $
385 "combination not supported: " ++
386 foldr1 (\a b -> a ++ '/':b)
387 (map (wayName . lkupWay) ws))
388 else let ways = map lkupWay ws
389 tag = mkBuildTag (filter (not.wayRTSOnly) ways)
390 rts_tag = mkBuildTag ways
391 flags = map wayOpts ways
393 writeIORef v_Build_tag tag
394 writeIORef v_RTS_Build_tag rts_tag
395 return (concat flags)
397 mkBuildTag :: [Way] -> String
398 mkBuildTag ways = concat (intersperse "_" (map wayTag ways))
401 case lookup w way_details of
402 Nothing -> error "findBuildTag"
403 Just details -> details
412 way_details :: [ (WayName, Way) ]
414 [ (WayThreaded, Way "thr" True "Threaded" [
415 #if defined(freebsd_TARGET_OS)
421 (WayDebug, Way "debug" True "Debug" [] ),
423 (WayProf, Way "p" False "Profiling"
429 (WayTicky, Way "t" False "Ticky-ticky Profiling"
432 , "-optc-DTICKY_TICKY"
435 (WayUnreg, Way "u" False "Unregisterised"
438 -- optl's below to tell linker where to find the PVM library -- HWL
439 (WayPar, Way "mp" False "Parallel"
441 , "-D__PARALLEL_HASKELL__"
443 , "-package concurrent"
445 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
450 -- at the moment we only change the RTS and could share compiler and libs!
451 (WayPar, Way "mt" False "Parallel ticky profiling"
453 , "-D__PARALLEL_HASKELL__"
456 , "-package concurrent"
458 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
463 (WayPar, Way "md" False "Distributed"
465 , "-D__PARALLEL_HASKELL__"
466 , "-D__DISTRIBUTED_HASKELL__"
469 , "-package concurrent"
471 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
476 (WayGran, Way "mg" False "GranSim"
480 , "-package concurrent"
483 (WaySMP, Way "s" False "SMP"
486 #ifndef freebsd_TARGET_OS
492 (WayNDP, Way "ndp" False "Nested data parallelism"
496 (WayUser_a, Way "a" False "User way 'a'" ["$WAY_a_REAL_OPTS"]),
497 (WayUser_b, Way "b" False "User way 'b'" ["$WAY_b_REAL_OPTS"]),
498 (WayUser_c, Way "c" False "User way 'c'" ["$WAY_c_REAL_OPTS"]),
499 (WayUser_d, Way "d" False "User way 'd'" ["$WAY_d_REAL_OPTS"]),
500 (WayUser_e, Way "e" False "User way 'e'" ["$WAY_e_REAL_OPTS"]),
501 (WayUser_f, Way "f" False "User way 'f'" ["$WAY_f_REAL_OPTS"]),
502 (WayUser_g, Way "g" False "User way 'g'" ["$WAY_g_REAL_OPTS"]),
503 (WayUser_h, Way "h" False "User way 'h'" ["$WAY_h_REAL_OPTS"]),
504 (WayUser_i, Way "i" False "User way 'i'" ["$WAY_i_REAL_OPTS"]),
505 (WayUser_j, Way "j" False "User way 'j'" ["$WAY_j_REAL_OPTS"]),
506 (WayUser_k, Way "k" False "User way 'k'" ["$WAY_k_REAL_OPTS"]),
507 (WayUser_l, Way "l" False "User way 'l'" ["$WAY_l_REAL_OPTS"]),
508 (WayUser_m, Way "m" False "User way 'm'" ["$WAY_m_REAL_OPTS"]),
509 (WayUser_n, Way "n" False "User way 'n'" ["$WAY_n_REAL_OPTS"]),
510 (WayUser_o, Way "o" False "User way 'o'" ["$WAY_o_REAL_OPTS"]),
511 (WayUser_A, Way "A" False "User way 'A'" ["$WAY_A_REAL_OPTS"]),
512 (WayUser_B, Way "B" False "User way 'B'" ["$WAY_B_REAL_OPTS"])
517 , "-optc-DUSE_MINIINTERPRETER"
518 , "-fno-asm-mangling"
522 -----------------------------------------------------------------------------
523 -- Options for particular phases
525 GLOBAL_VAR(v_Opt_dep, [], [String])
526 GLOBAL_VAR(v_Anti_opt_C, [], [String])
527 GLOBAL_VAR(v_Opt_C, [], [String])
528 GLOBAL_VAR(v_Opt_l, [], [String])
529 GLOBAL_VAR(v_Opt_dll, [], [String])
531 getStaticOpts :: IORef [String] -> IO [String]
532 getStaticOpts ref = readIORef ref >>= return . reverse