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 addToOrDeleteDirList :: IORef [String] -> String -> IO ()
209 addToOrDeleteDirList ref "" = writeIORef ref []
210 addToOrDeleteDirList ref path = addToDirList ref path
212 addToDirList :: IORef [String] -> String -> IO ()
213 addToDirList ref path
214 = do paths <- readIORef ref
215 shiny_new_ones <- splitUp path
216 writeIORef ref (paths ++ filter notNull shiny_new_ones)
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 splitUp ::String -> IO [String]
223 #ifdef mingw32_TARGET_OS
224 -- 'hybrid' support for DOS-style paths in directory lists.
226 -- That is, if "foo:bar:baz" is used, this interpreted as
227 -- consisting of three entries, 'foo', 'bar', 'baz'.
228 -- However, with "c:/foo:c:\\foo;x:/bar", this is interpreted
229 -- as four elts, "c:/foo", "c:\\foo", "x", and "/bar" --
230 -- *provided* c:/foo exists and x:/bar doesn't.
232 -- Notice that no attempt is made to fully replace the 'standard'
233 -- split marker ':' with the Windows / DOS one, ';'. The reason being
234 -- that this will cause too much breakage for users & ':' will
235 -- work fine even with DOS paths, if you're not insisting on being silly.
237 splitUp [] = return []
238 splitUp (x:':':div:xs)
239 | div `elem` dir_markers = do
240 let (p,rs) = findNextPath xs
243 Consult the file system to check the interpretation
244 of (x:':':div:p) -- this is arguably excessive, we
245 could skip this test & just say that it is a valid
248 flg <- doesDirectoryExist (x:':':div:p)
250 return ((x:':':div:p):ps)
252 return ([x]:(div:p):ps)
254 let (p,rs) = findNextPath xs
261 -- will be called either when we've consumed nought or the "<Drive>:/" part of
262 -- a DOS path, so splitting is just a Q of finding the next split marker.
264 case break (`elem` split_markers) xs of
268 split_markers :: [Char]
269 split_markers = [':', ';']
271 dir_markers :: [Char]
272 dir_markers = ['/', '\\']
275 splitUp xs = return (split split_marker xs)
278 -----------------------------------------------------------------------------
281 -- The central concept of a "way" is that all objects in a given
282 -- program must be compiled in the same "way". Certain options change
283 -- parameters of the virtual machine, eg. profiling adds an extra word
284 -- to the object header, so profiling objects cannot be linked with
285 -- non-profiling objects.
287 -- After parsing the command-line options, we determine which "way" we
288 -- are building - this might be a combination way, eg. profiling+ticky-ticky.
290 -- We then find the "build-tag" associated with this way, and this
291 -- becomes the suffix used to find .hi files and libraries used in
294 GLOBAL_VAR(v_Build_tag, "", String)
296 -- The RTS has its own build tag, because there are some ways that
297 -- affect the RTS only.
298 GLOBAL_VAR(v_RTS_Build_tag, "", String)
329 GLOBAL_VAR(v_Ways, [] ,[WayName])
331 allowed_combination way = and [ x `allowedWith` y
332 | x <- way, y <- way, x < y ]
334 -- Note ordering in these tests: the left argument is
335 -- <= the right argument, according to the Ord instance
338 -- debug is allowed with everything
339 _ `allowedWith` WayDebug = True
340 WayDebug `allowedWith` _ = True
342 WayThreaded `allowedWith` WayProf = True
343 WayProf `allowedWith` WayUnreg = True
344 WayProf `allowedWith` WaySMP = True
345 WayProf `allowedWith` WayNDP = True
346 _ `allowedWith` _ = False
349 findBuildTag :: IO [String] -- new options
351 way_names <- readIORef v_Ways
352 let ws = sort way_names
353 if not (allowed_combination ws)
354 then throwDyn (CmdLineError $
355 "combination not supported: " ++
356 foldr1 (\a b -> a ++ '/':b)
357 (map (wayName . lkupWay) ws))
358 else let ways = map lkupWay ws
359 tag = mkBuildTag (filter (not.wayRTSOnly) ways)
360 rts_tag = mkBuildTag ways
361 flags = map wayOpts ways
363 writeIORef v_Build_tag tag
364 writeIORef v_RTS_Build_tag rts_tag
365 return (concat flags)
367 mkBuildTag :: [Way] -> String
368 mkBuildTag ways = concat (intersperse "_" (map wayTag ways))
371 case lookup w way_details of
372 Nothing -> error "findBuildTag"
373 Just details -> details
382 way_details :: [ (WayName, Way) ]
384 [ (WayThreaded, Way "thr" True "Threaded" [
385 #if defined(freebsd_TARGET_OS)
391 (WayDebug, Way "debug" True "Debug" [] ),
393 (WayProf, Way "p" False "Profiling"
399 (WayTicky, Way "t" False "Ticky-ticky Profiling"
402 , "-optc-DTICKY_TICKY"
405 (WayUnreg, Way "u" False "Unregisterised"
408 -- optl's below to tell linker where to find the PVM library -- HWL
409 (WayPar, Way "mp" False "Parallel"
411 , "-D__PARALLEL_HASKELL__"
413 , "-package concurrent"
415 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
420 -- at the moment we only change the RTS and could share compiler and libs!
421 (WayPar, Way "mt" False "Parallel ticky profiling"
423 , "-D__PARALLEL_HASKELL__"
426 , "-package concurrent"
428 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
433 (WayPar, Way "md" False "Distributed"
435 , "-D__PARALLEL_HASKELL__"
436 , "-D__DISTRIBUTED_HASKELL__"
439 , "-package concurrent"
441 , "-optl-L${PVM_ROOT}/lib/${PVM_ARCH}"
446 (WayGran, Way "mg" False "GranSim"
450 , "-package concurrent"
453 (WaySMP, Way "s" False "SMP"
456 #ifndef freebsd_TARGET_OS
462 (WayNDP, Way "ndp" False "Nested data parallelism"
466 (WayUser_a, Way "a" False "User way 'a'" ["$WAY_a_REAL_OPTS"]),
467 (WayUser_b, Way "b" False "User way 'b'" ["$WAY_b_REAL_OPTS"]),
468 (WayUser_c, Way "c" False "User way 'c'" ["$WAY_c_REAL_OPTS"]),
469 (WayUser_d, Way "d" False "User way 'd'" ["$WAY_d_REAL_OPTS"]),
470 (WayUser_e, Way "e" False "User way 'e'" ["$WAY_e_REAL_OPTS"]),
471 (WayUser_f, Way "f" False "User way 'f'" ["$WAY_f_REAL_OPTS"]),
472 (WayUser_g, Way "g" False "User way 'g'" ["$WAY_g_REAL_OPTS"]),
473 (WayUser_h, Way "h" False "User way 'h'" ["$WAY_h_REAL_OPTS"]),
474 (WayUser_i, Way "i" False "User way 'i'" ["$WAY_i_REAL_OPTS"]),
475 (WayUser_j, Way "j" False "User way 'j'" ["$WAY_j_REAL_OPTS"]),
476 (WayUser_k, Way "k" False "User way 'k'" ["$WAY_k_REAL_OPTS"]),
477 (WayUser_l, Way "l" False "User way 'l'" ["$WAY_l_REAL_OPTS"]),
478 (WayUser_m, Way "m" False "User way 'm'" ["$WAY_m_REAL_OPTS"]),
479 (WayUser_n, Way "n" False "User way 'n'" ["$WAY_n_REAL_OPTS"]),
480 (WayUser_o, Way "o" False "User way 'o'" ["$WAY_o_REAL_OPTS"]),
481 (WayUser_A, Way "A" False "User way 'A'" ["$WAY_A_REAL_OPTS"]),
482 (WayUser_B, Way "B" False "User way 'B'" ["$WAY_B_REAL_OPTS"])
487 , "-optc-DUSE_MINIINTERPRETER"
488 , "-fno-asm-mangling"
492 -----------------------------------------------------------------------------
493 -- Options for particular phases
495 GLOBAL_VAR(v_Opt_dep, [], [String])
496 GLOBAL_VAR(v_Anti_opt_C, [], [String])
497 GLOBAL_VAR(v_Opt_C, [], [String])
498 GLOBAL_VAR(v_Opt_l, [], [String])
499 GLOBAL_VAR(v_Opt_dll, [], [String])
501 getStaticOpts :: IORef [String] -> IO [String]
502 getStaticOpts ref = readIORef ref >>= return . reverse