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 isCompManagerMode DoMake = True
62 isCompManagerMode DoInteractive = True
63 isCompManagerMode (DoEval _) = True
64 isCompManagerMode _ = False
66 -----------------------------------------------------------------------------
67 -- Global compilation flags
69 -- Default CPP defines in Haskell source
71 [ "-D__HASKELL1__="++cHaskell1Version
72 , "-D__GLASGOW_HASKELL__="++cProjectVersionInt
74 , "-D__CONCURRENT_HASKELL__"
78 -- Keep output from intermediate phases
79 GLOBAL_VAR(v_Keep_hi_diffs, False, Bool)
80 GLOBAL_VAR(v_Keep_hc_files, False, Bool)
81 GLOBAL_VAR(v_Keep_s_files, False, Bool)
82 GLOBAL_VAR(v_Keep_raw_s_files, False, Bool)
83 GLOBAL_VAR(v_Keep_tmp_files, False, Bool)
85 GLOBAL_VAR(v_Keep_il_files, False, Bool)
86 GLOBAL_VAR(v_Keep_ilx_files, False, Bool)
90 GLOBAL_VAR(v_Scale_sizes_by, 1.0, Double)
91 GLOBAL_VAR(v_Static, True, Bool)
92 GLOBAL_VAR(v_NoLink, False, Bool)
93 GLOBAL_VAR(v_NoHsMain, False, Bool)
94 GLOBAL_VAR(v_MainModIs, Nothing, Maybe String)
95 GLOBAL_VAR(v_MainFunIs, Nothing, Maybe String)
96 GLOBAL_VAR(v_Recomp, True, Bool)
97 GLOBAL_VAR(v_Collect_ghc_timing, False, Bool)
98 GLOBAL_VAR(v_Do_asm_mangling, True, Bool)
99 GLOBAL_VAR(v_Excess_precision, False, Bool)
100 GLOBAL_VAR(v_Read_DotGHCi, True, Bool)
102 -- Preprocessor flags
103 GLOBAL_VAR(v_Hs_source_pp_opts, [], [String])
105 -----------------------------------------------------------------------------
106 -- Splitting object files (for libraries)
108 GLOBAL_VAR(v_Split_object_files, False, Bool)
109 GLOBAL_VAR(v_Split_info, ("",0), (String,Int))
110 -- The split prefix and number of files
114 can_split = prefixMatch "i386" cTARGETPLATFORM
115 || prefixMatch "alpha" cTARGETPLATFORM
116 || prefixMatch "hppa" cTARGETPLATFORM
117 || prefixMatch "m68k" cTARGETPLATFORM
118 || prefixMatch "mips" cTARGETPLATFORM
119 || prefixMatch "powerpc" cTARGETPLATFORM
120 || prefixMatch "rs6000" cTARGETPLATFORM
121 || prefixMatch "sparc" cTARGETPLATFORM
123 -----------------------------------------------------------------------------
124 -- Compiler output options
126 GLOBAL_VAR(v_Output_dir, Nothing, Maybe String)
127 GLOBAL_VAR(v_Output_file, Nothing, Maybe String)
128 GLOBAL_VAR(v_Output_hi, Nothing, Maybe String)
130 -- called to verify that the output files & directories
131 -- point somewhere valid.
133 -- The assumption is that the directory portion of these output
134 -- options will have to exist by the time 'verifyOutputFiles'
137 verifyOutputFiles :: IO ()
138 verifyOutputFiles = do
139 odir <- readIORef v_Output_dir
140 when (isJust odir) $ do
141 let dir = fromJust odir
142 flg <- doesDirectoryExist dir
143 when (not flg) (nonExistentDir "-odir" dir)
144 ofile <- readIORef v_Output_file
145 when (isJust ofile) $ do
146 let fn = fromJust ofile
147 flg <- doesDirNameExist fn
148 when (not flg) (nonExistentDir "-o" fn)
149 ohi <- readIORef v_Output_hi
150 when (isJust ohi) $ do
151 let hi = fromJust ohi
152 flg <- doesDirNameExist hi
153 when (not flg) (nonExistentDir "-ohi" hi)
155 nonExistentDir flg dir =
156 throwDyn (CmdLineError ("error: directory portion of " ++
157 show dir ++ " does not exist (used with " ++
158 show flg ++ " option.)"))
160 GLOBAL_VAR(v_Object_suf, phaseInputExt Ln, String)
161 GLOBAL_VAR(v_HC_suf, Nothing, Maybe String)
162 GLOBAL_VAR(v_Hi_dir, Nothing, Maybe String)
163 GLOBAL_VAR(v_Hi_suf, "hi", String)
165 GLOBAL_VAR(v_Ld_inputs, [], [String])
167 odir_ify :: String -> IO String
169 odir_opt <- readIORef v_Output_dir
172 Just d -> return (replaceFilenameDirectory f d)
174 osuf_ify :: String -> IO String
176 osuf <- readIORef v_Object_suf
177 return (replaceFilenameSuffix f osuf)
179 GLOBAL_VAR(v_StgStats, False, Bool)
181 buildStgToDo :: IO [ StgToDo ]
183 stg_stats <- readIORef v_StgStats
184 let flags1 | stg_stats = [ D_stg_stats ]
188 ways_ <- readIORef v_Ways
189 let flags2 | WayProf `elem` ways_ = StgDoMassageForProfiling : flags1
194 -----------------------------------------------------------------------------
197 split_marker = ':' -- not configurable (ToDo)
199 v_Include_paths, v_Library_paths :: IORef [String]
200 GLOBAL_VAR(v_Include_paths, [], [String])
201 GLOBAL_VAR(v_Library_paths, [], [String])
203 #ifdef darwin_TARGET_OS
204 GLOBAL_VAR(v_Framework_paths, [], [String])
205 GLOBAL_VAR(v_Cmdline_frameworks, [], [String])
208 addToDirList :: IORef [String] -> String -> IO ()
209 addToDirList ref path
210 = do paths <- readIORef ref
211 shiny_new_ones <- splitPathList path
212 writeIORef ref (paths ++ shiny_new_ones)
215 splitPathList :: String -> IO [String]
216 splitPathList s = do ps <- splitUp s; return (filter notNull ps)
217 -- empty paths are ignored: there might be a trailing
218 -- ':' in the initial list, for example. Empty paths can
219 -- cause confusion when they are translated into -I options
220 -- for passing to gcc.
222 #ifdef mingw32_TARGET_OS
223 -- 'hybrid' support for DOS-style paths in directory lists.
225 -- That is, if "foo:bar:baz" is used, this interpreted as
226 -- consisting of three entries, 'foo', 'bar', 'baz'.
227 -- However, with "c:/foo:c:\\foo;x:/bar", this is interpreted
228 -- as four elts, "c:/foo", "c:\\foo", "x", and "/bar" --
229 -- *provided* c:/foo exists and x:/bar doesn't.
231 -- Notice that no attempt is made to fully replace the 'standard'
232 -- split marker ':' with the Windows / DOS one, ';'. The reason being
233 -- that this will cause too much breakage for users & ':' will
234 -- work fine even with DOS paths, if you're not insisting on being silly.
236 splitUp [] = return []
237 splitUp (x:':':div:xs)
238 | div `elem` dir_markers = do
239 let (p,rs) = findNextPath xs
242 Consult the file system to check the interpretation
243 of (x:':':div:p) -- this is arguably excessive, we
244 could skip this test & just say that it is a valid
247 flg <- doesDirectoryExist (x:':':div:p)
249 return ((x:':':div:p):ps)
251 return ([x]:(div:p):ps)
253 let (p,rs) = findNextPath xs
260 -- will be called either when we've consumed nought or the "<Drive>:/" part of
261 -- a DOS path, so splitting is just a Q of finding the next split marker.
263 case break (`elem` split_markers) xs of
267 split_markers :: [Char]
268 split_markers = [':', ';']
270 dir_markers :: [Char]
271 dir_markers = ['/', '\\']
274 splitUp xs = return (split split_marker xs)
277 -----------------------------------------------------------------------------
280 -- The central concept of a "way" is that all objects in a given
281 -- program must be compiled in the same "way". Certain options change
282 -- parameters of the virtual machine, eg. profiling adds an extra word
283 -- to the object header, so profiling objects cannot be linked with
284 -- non-profiling objects.
286 -- After parsing the command-line options, we determine which "way" we
287 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
289 -- We then find the "build-tag" associated with this way, and this
290 -- becomes the suffix used to find .hi files and libraries used in
293 GLOBAL_VAR(v_Build_tag, "", String)
295 -- The RTS has its own build tag, because there are some ways that
296 -- affect the RTS only.
297 GLOBAL_VAR(v_RTS_Build_tag, "", String)
328 GLOBAL_VAR(v_Ways, [] ,[WayName])
330 allowed_combination way = and [ x `allowedWith` y
331 | x <- way, y <- way, x < y ]
333 -- Note ordering in these tests: the left argument is
334 -- <= the right argument, according to the Ord instance
337 -- debug is allowed with everything
338 _ `allowedWith` WayDebug = True
339 WayDebug `allowedWith` _ = True
341 WayThreaded `allowedWith` WayProf = True
342 WayProf `allowedWith` WayUnreg = True
343 WayProf `allowedWith` WaySMP = True
344 WayProf `allowedWith` WayNDP = True
345 _ `allowedWith` _ = False
348 findBuildTag :: IO [String] -- new options
350 way_names <- readIORef v_Ways
351 let ws = sort way_names
352 if not (allowed_combination ws)
353 then throwDyn (CmdLineError $
354 "combination not supported: " ++
355 foldr1 (\a b -> a ++ '/':b)
356 (map (wayName . lkupWay) ws))
357 else let ways = map lkupWay ws
358 tag = mkBuildTag (filter (not.wayRTSOnly) ways)
359 rts_tag = mkBuildTag ways
360 flags = map wayOpts ways
362 writeIORef v_Build_tag tag
363 writeIORef v_RTS_Build_tag rts_tag
364 return (concat flags)
366 mkBuildTag :: [Way] -> String
367 mkBuildTag ways = concat (intersperse "_" (map wayTag ways))
370 case lookup w way_details of
371 Nothing -> error "findBuildTag"
372 Just details -> details
381 way_details :: [ (WayName, Way) ]
383 [ (WayThreaded, Way "thr" True "Threaded" [
384 #if defined(freebsd_TARGET_OS)
390 (WayDebug, Way "debug" True "Debug" [] ),
392 (WayProf, Way "p" False "Profiling"
398 (WayTicky, Way "t" False "Ticky-ticky Profiling"
401 , "-optc-DTICKY_TICKY"
404 (WayUnreg, Way "u" False "Unregisterised"
407 -- optl's below to tell linker where to find the PVM library -- HWL
408 (WayPar, Way "mp" False "Parallel"
410 , "-D__PARALLEL_HASKELL__"
412 , "-package concurrent"
414 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
419 -- at the moment we only change the RTS and could share compiler and libs!
420 (WayPar, Way "mt" False "Parallel ticky profiling"
422 , "-D__PARALLEL_HASKELL__"
425 , "-package concurrent"
427 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
432 (WayPar, Way "md" False "Distributed"
434 , "-D__PARALLEL_HASKELL__"
435 , "-D__DISTRIBUTED_HASKELL__"
438 , "-package concurrent"
440 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
445 (WayGran, Way "mg" False "GranSim"
449 , "-package concurrent"
452 (WaySMP, Way "s" False "SMP"
455 #ifndef freebsd_TARGET_OS
461 (WayNDP, Way "ndp" False "Nested data parallelism"
465 (WayUser_a, Way "a" False "User way 'a'" ["$WAY_a_REAL_OPTS"]),
466 (WayUser_b, Way "b" False "User way 'b'" ["$WAY_b_REAL_OPTS"]),
467 (WayUser_c, Way "c" False "User way 'c'" ["$WAY_c_REAL_OPTS"]),
468 (WayUser_d, Way "d" False "User way 'd'" ["$WAY_d_REAL_OPTS"]),
469 (WayUser_e, Way "e" False "User way 'e'" ["$WAY_e_REAL_OPTS"]),
470 (WayUser_f, Way "f" False "User way 'f'" ["$WAY_f_REAL_OPTS"]),
471 (WayUser_g, Way "g" False "User way 'g'" ["$WAY_g_REAL_OPTS"]),
472 (WayUser_h, Way "h" False "User way 'h'" ["$WAY_h_REAL_OPTS"]),
473 (WayUser_i, Way "i" False "User way 'i'" ["$WAY_i_REAL_OPTS"]),
474 (WayUser_j, Way "j" False "User way 'j'" ["$WAY_j_REAL_OPTS"]),
475 (WayUser_k, Way "k" False "User way 'k'" ["$WAY_k_REAL_OPTS"]),
476 (WayUser_l, Way "l" False "User way 'l'" ["$WAY_l_REAL_OPTS"]),
477 (WayUser_m, Way "m" False "User way 'm'" ["$WAY_m_REAL_OPTS"]),
478 (WayUser_n, Way "n" False "User way 'n'" ["$WAY_n_REAL_OPTS"]),
479 (WayUser_o, Way "o" False "User way 'o'" ["$WAY_o_REAL_OPTS"]),
480 (WayUser_A, Way "A" False "User way 'A'" ["$WAY_A_REAL_OPTS"]),
481 (WayUser_B, Way "B" False "User way 'B'" ["$WAY_B_REAL_OPTS"])
486 , "-optc-DUSE_MINIINTERPRETER"
487 , "-fno-asm-mangling"
491 -----------------------------------------------------------------------------
492 -- Options for particular phases
494 GLOBAL_VAR(v_Opt_dep, [], [String])
495 GLOBAL_VAR(v_Anti_opt_C, [], [String])
496 GLOBAL_VAR(v_Opt_C, [], [String])
497 GLOBAL_VAR(v_Opt_l, [], [String])
498 GLOBAL_VAR(v_Opt_dll, [], [String])
500 getStaticOpts :: IORef [String] -> IO [String]
501 getStaticOpts ref = readIORef ref >>= return . reverse