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