1 -----------------------------------------------------------------------------
3 -- Settings for the driver
5 -- (c) The University of Glasgow 2002
7 -----------------------------------------------------------------------------
9 module DriverState where
11 #include "../includes/ghcconfig.h"
12 #include "HsVersions.h"
21 import DATA_IOREF ( IORef, readIORef, writeIORef )
27 import Maybe ( fromJust, isJust )
28 import Directory ( doesDirectoryExist )
30 -----------------------------------------------------------------------------
31 -- non-configured things
33 cHaskell1Version = "5" -- i.e., Haskell 98
35 -----------------------------------------------------------------------------
36 -- GHC modes of operation
39 = DoMkDependHS -- ghc -M
40 | DoMkDLL -- ghc --mk-dll
41 | StopBefore Phase -- ghc -E | -C | -S | -c
42 | DoMake -- ghc --make
43 | DoInteractive -- ghc --interactive
44 | DoLink -- [ the default ]
45 | DoEval String -- ghc -e
48 GLOBAL_VAR(v_GhcMode, DoLink, GhcMode)
49 GLOBAL_VAR(v_GhcModeFlag, "", String)
51 setMode :: GhcMode -> String -> IO ()
53 old_mode <- readIORef v_GhcMode
54 old_flag <- readIORef v_GhcModeFlag
55 when (notNull old_flag && flag /= old_flag) $
57 ("cannot use `" ++ old_flag ++ "' with `" ++ flag ++ "'"))
58 writeIORef v_GhcMode m
59 writeIORef v_GhcModeFlag flag
61 isInteractiveMode, isInterpretiveMode :: GhcMode -> Bool
62 isMakeMode, isLinkMode, isCompManagerMode :: GhcMode -> Bool
64 isInteractiveMode DoInteractive = True
65 isInteractiveMode _ = False
67 -- isInterpretiveMode: byte-code compiler involved
68 isInterpretiveMode DoInteractive = True
69 isInterpretiveMode (DoEval _) = True
70 isInterpretiveMode _ = False
72 isMakeMode DoMake = True
75 isLinkMode DoLink = True
76 isLinkMode DoMkDLL = True
79 isCompManagerMode DoMake = True
80 isCompManagerMode DoInteractive = True
81 isCompManagerMode (DoEval _) = True
82 isCompManagerMode _ = False
84 -----------------------------------------------------------------------------
85 -- Global compilation flags
87 -- Default CPP defines in Haskell source
89 [ "-D__HASKELL1__="++cHaskell1Version
90 , "-D__GLASGOW_HASKELL__="++cProjectVersionInt
92 , "-D__CONCURRENT_HASKELL__"
96 -- Keep output from intermediate phases
97 GLOBAL_VAR(v_Keep_hi_diffs, False, Bool)
98 GLOBAL_VAR(v_Keep_hc_files, False, Bool)
99 GLOBAL_VAR(v_Keep_s_files, False, Bool)
100 GLOBAL_VAR(v_Keep_raw_s_files, False, Bool)
101 GLOBAL_VAR(v_Keep_tmp_files, False, Bool)
103 GLOBAL_VAR(v_Keep_il_files, False, Bool)
104 GLOBAL_VAR(v_Keep_ilx_files, False, Bool)
108 GLOBAL_VAR(v_Scale_sizes_by, 1.0, Double)
109 GLOBAL_VAR(v_Static, True, Bool)
110 GLOBAL_VAR(v_NoLink, False, Bool)
111 GLOBAL_VAR(v_NoHsMain, False, Bool)
112 GLOBAL_VAR(v_MainModIs, Nothing, Maybe String)
113 GLOBAL_VAR(v_MainFunIs, Nothing, Maybe String)
114 GLOBAL_VAR(v_Collect_ghc_timing, False, Bool)
115 GLOBAL_VAR(v_Do_asm_mangling, True, Bool)
116 GLOBAL_VAR(v_Excess_precision, False, Bool)
117 GLOBAL_VAR(v_Read_DotGHCi, True, Bool)
119 -- Preprocessor flags
120 GLOBAL_VAR(v_Hs_source_pp_opts, [], [String])
122 -----------------------------------------------------------------------------
123 -- Splitting object files (for libraries)
125 GLOBAL_VAR(v_Split_object_files, False, Bool)
126 GLOBAL_VAR(v_Split_info, ("",0), (String,Int))
127 -- The split prefix and number of files
131 can_split = prefixMatch "i386" cTARGETPLATFORM
132 || prefixMatch "alpha" cTARGETPLATFORM
133 || prefixMatch "hppa" cTARGETPLATFORM
134 || prefixMatch "m68k" cTARGETPLATFORM
135 || prefixMatch "mips" cTARGETPLATFORM
136 || prefixMatch "powerpc" cTARGETPLATFORM
137 || prefixMatch "rs6000" cTARGETPLATFORM
138 || prefixMatch "sparc" cTARGETPLATFORM
140 -----------------------------------------------------------------------------
141 -- Compiler output options
143 GLOBAL_VAR(v_Output_dir, Nothing, Maybe String)
144 GLOBAL_VAR(v_Output_file, Nothing, Maybe String)
145 GLOBAL_VAR(v_Output_hi, Nothing, Maybe String)
147 -- called to verify that the output files & directories
148 -- point somewhere valid.
150 -- The assumption is that the directory portion of these output
151 -- options will have to exist by the time 'verifyOutputFiles'
154 verifyOutputFiles :: IO ()
155 verifyOutputFiles = do
156 odir <- readIORef v_Output_dir
157 when (isJust odir) $ do
158 let dir = fromJust odir
159 flg <- doesDirectoryExist dir
160 when (not flg) (nonExistentDir "-odir" dir)
161 ofile <- readIORef v_Output_file
162 when (isJust ofile) $ do
163 let fn = fromJust ofile
164 flg <- doesDirNameExist fn
165 when (not flg) (nonExistentDir "-o" fn)
166 ohi <- readIORef v_Output_hi
167 when (isJust ohi) $ do
168 let hi = fromJust ohi
169 flg <- doesDirNameExist hi
170 when (not flg) (nonExistentDir "-ohi" hi)
172 nonExistentDir flg dir =
173 throwDyn (CmdLineError ("error: directory portion of " ++
174 show dir ++ " does not exist (used with " ++
175 show flg ++ " option.)"))
177 GLOBAL_VAR(v_Object_suf, phaseInputExt StopLn, String)
178 GLOBAL_VAR(v_HC_suf, phaseInputExt HCc, String)
179 GLOBAL_VAR(v_Hi_dir, Nothing, Maybe String)
180 GLOBAL_VAR(v_Hi_suf, "hi", String)
182 GLOBAL_VAR(v_Ld_inputs, [], [String])
184 odir_ify :: String -> IO String
186 odir_opt <- readIORef v_Output_dir
189 Just d -> return (replaceFilenameDirectory f d)
191 osuf_ify :: String -> IO String
193 osuf <- readIORef v_Object_suf
194 return (replaceFilenameSuffix f osuf)
196 GLOBAL_VAR(v_StgStats, False, Bool)
198 buildStgToDo :: IO [ StgToDo ]
200 stg_stats <- readIORef v_StgStats
201 let flags1 | stg_stats = [ D_stg_stats ]
205 ways_ <- readIORef v_Ways
206 let flags2 | WayProf `elem` ways_ = StgDoMassageForProfiling : flags1
211 -----------------------------------------------------------------------------
214 split_marker = ':' -- not configurable (ToDo)
216 v_Include_paths, v_Library_paths :: IORef [String]
217 GLOBAL_VAR(v_Include_paths, [], [String])
218 GLOBAL_VAR(v_Library_paths, [], [String])
220 #ifdef darwin_TARGET_OS
221 GLOBAL_VAR(v_Framework_paths, [], [String])
222 GLOBAL_VAR(v_Cmdline_frameworks, [], [String])
225 addToDirList :: IORef [String] -> String -> IO ()
226 addToDirList ref path
227 = do paths <- readIORef ref
228 shiny_new_ones <- splitPathList path
229 writeIORef ref (paths ++ shiny_new_ones)
232 splitPathList :: String -> IO [String]
233 splitPathList s = do ps <- splitUp s; return (filter notNull ps)
234 -- empty paths are ignored: there might be a trailing
235 -- ':' in the initial list, for example. Empty paths can
236 -- cause confusion when they are translated into -I options
237 -- for passing to gcc.
239 #ifdef mingw32_TARGET_OS
240 -- 'hybrid' support for DOS-style paths in directory lists.
242 -- That is, if "foo:bar:baz" is used, this interpreted as
243 -- consisting of three entries, 'foo', 'bar', 'baz'.
244 -- However, with "c:/foo:c:\\foo;x:/bar", this is interpreted
245 -- as four elts, "c:/foo", "c:\\foo", "x", and "/bar" --
246 -- *provided* c:/foo exists and x:/bar doesn't.
248 -- Notice that no attempt is made to fully replace the 'standard'
249 -- split marker ':' with the Windows / DOS one, ';'. The reason being
250 -- that this will cause too much breakage for users & ':' will
251 -- work fine even with DOS paths, if you're not insisting on being silly.
253 splitUp [] = return []
254 splitUp (x:':':div:xs)
255 | div `elem` dir_markers = do
256 let (p,rs) = findNextPath xs
259 Consult the file system to check the interpretation
260 of (x:':':div:p) -- this is arguably excessive, we
261 could skip this test & just say that it is a valid
264 flg <- doesDirectoryExist (x:':':div:p)
266 return ((x:':':div:p):ps)
268 return ([x]:(div:p):ps)
270 let (p,rs) = findNextPath xs
277 -- will be called either when we've consumed nought or the "<Drive>:/" part of
278 -- a DOS path, so splitting is just a Q of finding the next split marker.
280 case break (`elem` split_markers) xs of
284 split_markers :: [Char]
285 split_markers = [':', ';']
287 dir_markers :: [Char]
288 dir_markers = ['/', '\\']
291 splitUp xs = return (split split_marker xs)
294 -----------------------------------------------------------------------------
297 -- The central concept of a "way" is that all objects in a given
298 -- program must be compiled in the same "way". Certain options change
299 -- parameters of the virtual machine, eg. profiling adds an extra word
300 -- to the object header, so profiling objects cannot be linked with
301 -- non-profiling objects.
303 -- After parsing the command-line options, we determine which "way" we
304 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
306 -- We then find the "build-tag" associated with this way, and this
307 -- becomes the suffix used to find .hi files and libraries used in
310 GLOBAL_VAR(v_Build_tag, "", String)
312 -- The RTS has its own build tag, because there are some ways that
313 -- affect the RTS only.
314 GLOBAL_VAR(v_RTS_Build_tag, "", String)
345 GLOBAL_VAR(v_Ways, [] ,[WayName])
347 allowed_combination way = and [ x `allowedWith` y
348 | x <- way, y <- way, x < y ]
350 -- Note ordering in these tests: the left argument is
351 -- <= the right argument, according to the Ord instance
354 -- debug is allowed with everything
355 _ `allowedWith` WayDebug = True
356 WayDebug `allowedWith` _ = True
358 WayThreaded `allowedWith` WayProf = True
359 WayProf `allowedWith` WayUnreg = True
360 WayProf `allowedWith` WaySMP = True
361 WayProf `allowedWith` WayNDP = True
362 _ `allowedWith` _ = False
365 findBuildTag :: IO [String] -- new options
367 way_names <- readIORef v_Ways
368 let ws = sort way_names
369 if not (allowed_combination ws)
370 then throwDyn (CmdLineError $
371 "combination not supported: " ++
372 foldr1 (\a b -> a ++ '/':b)
373 (map (wayName . lkupWay) ws))
374 else let ways = map lkupWay ws
375 tag = mkBuildTag (filter (not.wayRTSOnly) ways)
376 rts_tag = mkBuildTag ways
377 flags = map wayOpts ways
379 writeIORef v_Build_tag tag
380 writeIORef v_RTS_Build_tag rts_tag
381 return (concat flags)
383 mkBuildTag :: [Way] -> String
384 mkBuildTag ways = concat (intersperse "_" (map wayTag ways))
387 case lookup w way_details of
388 Nothing -> error "findBuildTag"
389 Just details -> details
398 way_details :: [ (WayName, Way) ]
400 [ (WayThreaded, Way "thr" True "Threaded" [
401 #if defined(freebsd_TARGET_OS)
407 (WayDebug, Way "debug" True "Debug" [] ),
409 (WayProf, Way "p" False "Profiling"
415 (WayTicky, Way "t" False "Ticky-ticky Profiling"
418 , "-optc-DTICKY_TICKY"
421 (WayUnreg, Way "u" False "Unregisterised"
424 -- optl's below to tell linker where to find the PVM library -- HWL
425 (WayPar, Way "mp" False "Parallel"
427 , "-D__PARALLEL_HASKELL__"
429 , "-package concurrent"
431 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
436 -- at the moment we only change the RTS and could share compiler and libs!
437 (WayPar, Way "mt" False "Parallel ticky profiling"
439 , "-D__PARALLEL_HASKELL__"
442 , "-package concurrent"
444 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
449 (WayPar, Way "md" False "Distributed"
451 , "-D__PARALLEL_HASKELL__"
452 , "-D__DISTRIBUTED_HASKELL__"
455 , "-package concurrent"
457 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
462 (WayGran, Way "mg" False "GranSim"
466 , "-package concurrent"
469 (WaySMP, Way "s" False "SMP"
472 #ifndef freebsd_TARGET_OS
478 (WayNDP, Way "ndp" False "Nested data parallelism"
482 (WayUser_a, Way "a" False "User way 'a'" ["$WAY_a_REAL_OPTS"]),
483 (WayUser_b, Way "b" False "User way 'b'" ["$WAY_b_REAL_OPTS"]),
484 (WayUser_c, Way "c" False "User way 'c'" ["$WAY_c_REAL_OPTS"]),
485 (WayUser_d, Way "d" False "User way 'd'" ["$WAY_d_REAL_OPTS"]),
486 (WayUser_e, Way "e" False "User way 'e'" ["$WAY_e_REAL_OPTS"]),
487 (WayUser_f, Way "f" False "User way 'f'" ["$WAY_f_REAL_OPTS"]),
488 (WayUser_g, Way "g" False "User way 'g'" ["$WAY_g_REAL_OPTS"]),
489 (WayUser_h, Way "h" False "User way 'h'" ["$WAY_h_REAL_OPTS"]),
490 (WayUser_i, Way "i" False "User way 'i'" ["$WAY_i_REAL_OPTS"]),
491 (WayUser_j, Way "j" False "User way 'j'" ["$WAY_j_REAL_OPTS"]),
492 (WayUser_k, Way "k" False "User way 'k'" ["$WAY_k_REAL_OPTS"]),
493 (WayUser_l, Way "l" False "User way 'l'" ["$WAY_l_REAL_OPTS"]),
494 (WayUser_m, Way "m" False "User way 'm'" ["$WAY_m_REAL_OPTS"]),
495 (WayUser_n, Way "n" False "User way 'n'" ["$WAY_n_REAL_OPTS"]),
496 (WayUser_o, Way "o" False "User way 'o'" ["$WAY_o_REAL_OPTS"]),
497 (WayUser_A, Way "A" False "User way 'A'" ["$WAY_A_REAL_OPTS"]),
498 (WayUser_B, Way "B" False "User way 'B'" ["$WAY_B_REAL_OPTS"])
503 , "-optc-DUSE_MINIINTERPRETER"
504 , "-fno-asm-mangling"
508 -----------------------------------------------------------------------------
509 -- Options for particular phases
511 GLOBAL_VAR(v_Opt_dep, [], [String])
512 GLOBAL_VAR(v_Anti_opt_C, [], [String])
513 GLOBAL_VAR(v_Opt_C, [], [String])
514 GLOBAL_VAR(v_Opt_l, [], [String])
515 GLOBAL_VAR(v_Opt_dll, [], [String])
517 getStaticOpts :: IORef [String] -> IO [String]
518 getStaticOpts ref = readIORef ref >>= return . reverse