compiler: Write .o files atomically. See #14533
[ghc.git] / compiler / main / DriverPhases.hs
1 {-# LANGUAGE CPP #-}
2
3 -----------------------------------------------------------------------------
4 -- $Id: DriverPhases.hs,v 1.38 2005/05/17 11:01:59 simonmar Exp $
5 --
6 -- GHC Driver
7 --
8 -- (c) The University of Glasgow 2002
9 --
10 -----------------------------------------------------------------------------
11
12 module DriverPhases (
13 HscSource(..), isHsBootOrSig, isHsigFile, hscSourceString,
14 Phase(..),
15 happensBefore, eqPhase, anyHsc, isStopLn,
16 startPhase,
17 phaseInputExt,
18
19 isHaskellishSuffix,
20 isHaskellSrcSuffix,
21 isBackpackishSuffix,
22 isObjectSuffix,
23 isCishSuffix,
24 isDynLibSuffix,
25 isHaskellUserSrcSuffix,
26 isHaskellSigSuffix,
27 isSourceSuffix,
28
29 isHaskellishTarget,
30
31 isHaskellishFilename,
32 isHaskellSrcFilename,
33 isHaskellSigFilename,
34 isObjectFilename,
35 isCishFilename,
36 isDynLibFilename,
37 isHaskellUserSrcFilename,
38 isSourceFilename
39 ) where
40
41 #include "HsVersions.h"
42
43 import GhcPrelude
44
45 import {-# SOURCE #-} DynFlags
46 import Outputable
47 import Platform
48 import System.FilePath
49 import Binary
50 import Util
51
52 -----------------------------------------------------------------------------
53 -- Phases
54
55 {-
56 Phase of the | Suffix saying | Flag saying | (suffix of)
57 compilation system | ``start here''| ``stop after''| output file
58
59 literate pre-processor | .lhs | - | -
60 C pre-processor (opt.) | - | -E | -
61 Haskell compiler | .hs | -C, -S | .hc, .s
62 C compiler (opt.) | .hc or .c | -S | .s
63 assembler | .s or .S | -c | .o
64 linker | other | - | a.out
65 -}
66
67 -- Note [HscSource types]
68 -- ~~~~~~~~~~~~~~~~~~~~~~
69 -- There are three types of source file for Haskell code:
70 --
71 -- * HsSrcFile is an ordinary hs file which contains code,
72 --
73 -- * HsBootFile is an hs-boot file, which is used to break
74 -- recursive module imports (there will always be an
75 -- HsSrcFile associated with it), and
76 --
77 -- * HsigFile is an hsig file, which contains only type
78 -- signatures and is used to specify signatures for
79 -- modules.
80 --
81 -- Syntactically, hs-boot files and hsig files are quite similar: they
82 -- only include type signatures and must be associated with an
83 -- actual HsSrcFile. isHsBootOrSig allows us to abstract over code
84 -- which is indifferent to which. However, there are some important
85 -- differences, mostly owing to the fact that hsigs are proper
86 -- modules (you `import Sig` directly) whereas HsBootFiles are
87 -- temporary placeholders (you `import {-# SOURCE #-} Mod).
88 -- When we finish compiling the true implementation of an hs-boot,
89 -- we replace the HomeModInfo with the real HsSrcFile. An HsigFile, on the
90 -- other hand, is never replaced (in particular, we *cannot* use the
91 -- HomeModInfo of the original HsSrcFile backing the signature, since it
92 -- will export too many symbols.)
93 --
94 -- Additionally, while HsSrcFile is the only Haskell file
95 -- which has *code*, we do generate .o files for HsigFile, because
96 -- this is how the recompilation checker figures out if a file
97 -- needs to be recompiled. These are fake object files which
98 -- should NOT be linked against.
99
100 data HscSource
101 = HsSrcFile | HsBootFile | HsigFile
102 deriving( Eq, Ord, Show )
103 -- Ord needed for the finite maps we build in CompManager
104
105 instance Binary HscSource where
106 put_ bh HsSrcFile = putByte bh 0
107 put_ bh HsBootFile = putByte bh 1
108 put_ bh HsigFile = putByte bh 2
109 get bh = do
110 h <- getByte bh
111 case h of
112 0 -> return HsSrcFile
113 1 -> return HsBootFile
114 _ -> return HsigFile
115
116 hscSourceString :: HscSource -> String
117 hscSourceString HsSrcFile = ""
118 hscSourceString HsBootFile = "[boot]"
119 hscSourceString HsigFile = "[sig]"
120
121 -- See Note [isHsBootOrSig]
122 isHsBootOrSig :: HscSource -> Bool
123 isHsBootOrSig HsBootFile = True
124 isHsBootOrSig HsigFile = True
125 isHsBootOrSig _ = False
126
127 isHsigFile :: HscSource -> Bool
128 isHsigFile HsigFile = True
129 isHsigFile _ = False
130
131 data Phase
132 = Unlit HscSource
133 | Cpp HscSource
134 | HsPp HscSource
135 | Hsc HscSource
136 | Ccxx -- Compile C++
137 | Cc -- Compile C
138 | Cobjc -- Compile Objective-C
139 | Cobjcxx -- Compile Objective-C++
140 | HCc -- Haskellised C (as opposed to vanilla C) compilation
141 | Splitter -- Assembly file splitter (part of '-split-objs')
142 | SplitAs -- Assembler for split assembly files (part of '-split-objs')
143 | As Bool -- Assembler for regular assembly files (Bool: with-cpp)
144 | LlvmOpt -- Run LLVM opt tool over llvm assembly
145 | LlvmLlc -- LLVM bitcode to native assembly
146 | LlvmMangle -- Fix up TNTC by processing assembly produced by LLVM
147 | CmmCpp -- pre-process Cmm source
148 | Cmm -- parse & compile Cmm code
149 | MergeForeign -- merge in the foreign object files
150
151 -- The final phase is a pseudo-phase that tells the pipeline to stop.
152 -- There is no runPhase case for it.
153 | StopLn -- Stop, but linking will follow, so generate .o file
154 deriving (Eq, Show)
155
156 instance Outputable Phase where
157 ppr p = text (show p)
158
159 anyHsc :: Phase
160 anyHsc = Hsc (panic "anyHsc")
161
162 isStopLn :: Phase -> Bool
163 isStopLn StopLn = True
164 isStopLn _ = False
165
166 eqPhase :: Phase -> Phase -> Bool
167 -- Equality of constructors, ignoring the HscSource field
168 -- NB: the HscSource field can be 'bot'; see anyHsc above
169 eqPhase (Unlit _) (Unlit _) = True
170 eqPhase (Cpp _) (Cpp _) = True
171 eqPhase (HsPp _) (HsPp _) = True
172 eqPhase (Hsc _) (Hsc _) = True
173 eqPhase Cc Cc = True
174 eqPhase Cobjc Cobjc = True
175 eqPhase HCc HCc = True
176 eqPhase Splitter Splitter = True
177 eqPhase SplitAs SplitAs = True
178 eqPhase (As x) (As y) = x == y
179 eqPhase LlvmOpt LlvmOpt = True
180 eqPhase LlvmLlc LlvmLlc = True
181 eqPhase LlvmMangle LlvmMangle = True
182 eqPhase CmmCpp CmmCpp = True
183 eqPhase Cmm Cmm = True
184 eqPhase MergeForeign MergeForeign = True
185 eqPhase StopLn StopLn = True
186 eqPhase Ccxx Ccxx = True
187 eqPhase Cobjcxx Cobjcxx = True
188 eqPhase _ _ = False
189
190 {- Note [Partial ordering on phases]
191
192 We want to know which phases will occur before which others. This is used for
193 sanity checking, to ensure that the pipeline will stop at some point (see
194 DriverPipeline.runPipeline).
195
196 A < B iff A occurs before B in a normal compilation pipeline.
197
198 There is explicitly not a total ordering on phases, because in registerised
199 builds, the phase `HsC` doesn't happen before nor after any other phase.
200
201 Although we check that a normal user doesn't set the stop_phase to HsC through
202 use of -C with registerised builds (in Main.checkOptions), it is still
203 possible for a ghc-api user to do so. So be careful when using the function
204 happensBefore, and don't think that `not (a <= b)` implies `b < a`.
205 -}
206 happensBefore :: DynFlags -> Phase -> Phase -> Bool
207 happensBefore dflags p1 p2 = p1 `happensBefore'` p2
208 where StopLn `happensBefore'` _ = False
209 x `happensBefore'` y = after_x `eqPhase` y
210 || after_x `happensBefore'` y
211 where after_x = nextPhase dflags x
212
213 nextPhase :: DynFlags -> Phase -> Phase
214 nextPhase dflags p
215 -- A conservative approximation to the next phase, used in happensBefore
216 = case p of
217 Unlit sf -> Cpp sf
218 Cpp sf -> HsPp sf
219 HsPp sf -> Hsc sf
220 Hsc _ -> maybeHCc
221 Splitter -> SplitAs
222 LlvmOpt -> LlvmLlc
223 LlvmLlc -> LlvmMangle
224 LlvmMangle -> As False
225 SplitAs -> MergeForeign
226 As _ -> MergeForeign
227 Ccxx -> As False
228 Cc -> As False
229 Cobjc -> As False
230 Cobjcxx -> As False
231 CmmCpp -> Cmm
232 Cmm -> maybeHCc
233 HCc -> As False
234 MergeForeign -> StopLn
235 StopLn -> panic "nextPhase: nothing after StopLn"
236 where maybeHCc = if platformUnregisterised (targetPlatform dflags)
237 then HCc
238 else As False
239
240 -- the first compilation phase for a given file is determined
241 -- by its suffix.
242 startPhase :: String -> Phase
243 startPhase "lhs" = Unlit HsSrcFile
244 startPhase "lhs-boot" = Unlit HsBootFile
245 startPhase "lhsig" = Unlit HsigFile
246 startPhase "hs" = Cpp HsSrcFile
247 startPhase "hs-boot" = Cpp HsBootFile
248 startPhase "hsig" = Cpp HsigFile
249 startPhase "hscpp" = HsPp HsSrcFile
250 startPhase "hspp" = Hsc HsSrcFile
251 startPhase "hc" = HCc
252 startPhase "c" = Cc
253 startPhase "cpp" = Ccxx
254 startPhase "C" = Cc
255 startPhase "m" = Cobjc
256 startPhase "M" = Cobjcxx
257 startPhase "mm" = Cobjcxx
258 startPhase "cc" = Ccxx
259 startPhase "cxx" = Ccxx
260 startPhase "split_s" = Splitter
261 startPhase "s" = As False
262 startPhase "S" = As True
263 startPhase "ll" = LlvmOpt
264 startPhase "bc" = LlvmLlc
265 startPhase "lm_s" = LlvmMangle
266 startPhase "o" = StopLn
267 startPhase "cmm" = CmmCpp
268 startPhase "cmmcpp" = Cmm
269 startPhase _ = StopLn -- all unknown file types
270
271 -- This is used to determine the extension for the output from the
272 -- current phase (if it generates a new file). The extension depends
273 -- on the next phase in the pipeline.
274 phaseInputExt :: Phase -> String
275 phaseInputExt (Unlit HsSrcFile) = "lhs"
276 phaseInputExt (Unlit HsBootFile) = "lhs-boot"
277 phaseInputExt (Unlit HsigFile) = "lhsig"
278 phaseInputExt (Cpp _) = "lpp" -- intermediate only
279 phaseInputExt (HsPp _) = "hscpp" -- intermediate only
280 phaseInputExt (Hsc _) = "hspp" -- intermediate only
281 -- NB: as things stand, phaseInputExt (Hsc x) must not evaluate x
282 -- because runPipeline uses the StopBefore phase to pick the
283 -- output filename. That could be fixed, but watch out.
284 phaseInputExt HCc = "hc"
285 phaseInputExt Ccxx = "cpp"
286 phaseInputExt Cobjc = "m"
287 phaseInputExt Cobjcxx = "mm"
288 phaseInputExt Cc = "c"
289 phaseInputExt Splitter = "split_s"
290 phaseInputExt (As True) = "S"
291 phaseInputExt (As False) = "s"
292 phaseInputExt LlvmOpt = "ll"
293 phaseInputExt LlvmLlc = "bc"
294 phaseInputExt LlvmMangle = "lm_s"
295 phaseInputExt SplitAs = "split_s"
296 phaseInputExt CmmCpp = "cmmcpp"
297 phaseInputExt Cmm = "cmm"
298 phaseInputExt MergeForeign = "o"
299 phaseInputExt StopLn = "o"
300
301 haskellish_src_suffixes, backpackish_suffixes, haskellish_suffixes, cish_suffixes,
302 haskellish_user_src_suffixes, haskellish_sig_suffixes
303 :: [String]
304 -- When a file with an extension in the haskellish_src_suffixes group is
305 -- loaded in --make mode, its imports will be loaded too.
306 haskellish_src_suffixes = haskellish_user_src_suffixes ++
307 [ "hspp", "hscpp" ]
308 haskellish_suffixes = haskellish_src_suffixes ++
309 [ "hc", "cmm", "cmmcpp" ]
310 cish_suffixes = [ "c", "cpp", "C", "cc", "cxx", "s", "S", "ll", "bc", "lm_s", "m", "M", "mm" ]
311
312 -- Will not be deleted as temp files:
313 haskellish_user_src_suffixes =
314 haskellish_sig_suffixes ++ [ "hs", "lhs", "hs-boot", "lhs-boot" ]
315 haskellish_sig_suffixes = [ "hsig", "lhsig" ]
316 backpackish_suffixes = [ "bkp" ]
317
318 objish_suffixes :: Platform -> [String]
319 -- Use the appropriate suffix for the system on which
320 -- the GHC-compiled code will run
321 objish_suffixes platform = case platformOS platform of
322 OSMinGW32 -> [ "o", "O", "obj", "OBJ" ]
323 _ -> [ "o" ]
324
325 dynlib_suffixes :: Platform -> [String]
326 dynlib_suffixes platform = case platformOS platform of
327 OSMinGW32 -> ["dll", "DLL"]
328 OSDarwin -> ["dylib", "so"]
329 _ -> ["so"]
330
331 isHaskellishSuffix, isBackpackishSuffix, isHaskellSrcSuffix, isCishSuffix,
332 isHaskellUserSrcSuffix, isHaskellSigSuffix
333 :: String -> Bool
334 isHaskellishSuffix s = s `elem` haskellish_suffixes
335 isBackpackishSuffix s = s `elem` backpackish_suffixes
336 isHaskellSigSuffix s = s `elem` haskellish_sig_suffixes
337 isHaskellSrcSuffix s = s `elem` haskellish_src_suffixes
338 isCishSuffix s = s `elem` cish_suffixes
339 isHaskellUserSrcSuffix s = s `elem` haskellish_user_src_suffixes
340
341 isObjectSuffix, isDynLibSuffix :: Platform -> String -> Bool
342 isObjectSuffix platform s = s `elem` objish_suffixes platform
343 isDynLibSuffix platform s = s `elem` dynlib_suffixes platform
344
345 isSourceSuffix :: String -> Bool
346 isSourceSuffix suff = isHaskellishSuffix suff
347 || isCishSuffix suff
348 || isBackpackishSuffix suff
349
350 -- | When we are given files (modified by -x arguments) we need
351 -- to determine if they are Haskellish or not to figure out
352 -- how we should try to compile it. The rules are:
353 --
354 -- 1. If no -x flag was specified, we check to see if
355 -- the file looks like a module name, has no extension,
356 -- or has a Haskell source extension.
357 --
358 -- 2. If an -x flag was specified, we just make sure the
359 -- specified suffix is a Haskell one.
360 isHaskellishTarget :: (String, Maybe Phase) -> Bool
361 isHaskellishTarget (f,Nothing) =
362 looksLikeModuleName f || isHaskellSrcFilename f || not (hasExtension f)
363 isHaskellishTarget (_,Just phase) =
364 phase `notElem` [ As True, As False, Cc, Cobjc, Cobjcxx, CmmCpp, Cmm
365 , StopLn]
366
367 isHaskellishFilename, isHaskellSrcFilename, isCishFilename,
368 isHaskellUserSrcFilename, isSourceFilename, isHaskellSigFilename
369 :: FilePath -> Bool
370 -- takeExtension return .foo, so we drop 1 to get rid of the .
371 isHaskellishFilename f = isHaskellishSuffix (drop 1 $ takeExtension f)
372 isHaskellSrcFilename f = isHaskellSrcSuffix (drop 1 $ takeExtension f)
373 isCishFilename f = isCishSuffix (drop 1 $ takeExtension f)
374 isHaskellUserSrcFilename f = isHaskellUserSrcSuffix (drop 1 $ takeExtension f)
375 isSourceFilename f = isSourceSuffix (drop 1 $ takeExtension f)
376 isHaskellSigFilename f = isHaskellSigSuffix (drop 1 $ takeExtension f)
377
378 isObjectFilename, isDynLibFilename :: Platform -> FilePath -> Bool
379 isObjectFilename platform f = isObjectSuffix platform (drop 1 $ takeExtension f)
380 isDynLibFilename platform f = isDynLibSuffix platform (drop 1 $ takeExtension f)
381