DriverPipeline.hs 45.1 KB
Newer Older
1
2
3
4
-----------------------------------------------------------------------------
--
-- GHC Driver
--
5
-- (c) The University of Glasgow 2002
6
7
8
9
--
-----------------------------------------------------------------------------

module DriverPipeline (
10

11
	-- Interfaces for the batch-mode driver
12
   compileFile, staticLink,
13

14
15
16
17
	-- Interfaces for the compilation manager (interpreted/batch-mode)
   preprocess, 
   compile, CompResult(..), 
   link, 
18

rrt's avatar
rrt committed
19
20
        -- DLL building
   doMkDLL
21
22
23
24
  ) where

#include "HsVersions.h"

25
import Packages
26
import GetImports
27
28
import DriverState
import DriverUtil
29
import DriverPhases
30
import DriverFlags
sof's avatar
sof committed
31
import SysTools		( newTempName, addFilesToClean, getSysMan, copy )
32
import qualified SysTools	
33
import HscMain
34
import Finder
35
36
37
import HscTypes
import Outputable
import Module
38
import ErrUtils
39
import CmdLineOpts
40
import Config
41
import RdrName		( GlobalRdrEnv )
42
import Panic
43
import Util
44
import StringBuffer	( hGetStringBuffer )
45
import BasicTypes	( SuccessFlag(..) )
46
import Maybes		( expectJust )
47

48
49
import ParserCoreUtils ( getCoreModuleName )

50
51
52
import EXCEPTION
import DATA_IOREF	( readIORef, writeIORef )

53
54
import Directory
import System
55
56
57
58
import IO
import Monad
import Maybe

59

60
61
-- ---------------------------------------------------------------------------
-- Pre-process
62
63
64

-- Just preprocess a file, put the result in a temp. file (used by the
-- compilation manager during the summary phase).
65
66
67
--
-- We return the augmented DynFlags, because they contain the result
-- of slurping in the OPTIONS pragmas
68

69
preprocess :: DynFlags -> FilePath -> IO (DynFlags, FilePath)
70
preprocess dflags filename =
71
  ASSERT2(isHaskellSrcFilename filename, text filename) 
72
  runPipeline anyHsc "preprocess"  dflags
73
74
75
	False{-temporary output file-}
	Nothing{-no specific output file-}
	filename
76
	Nothing{-no ModLocation-}
77

78
79
80
81
82
83
84
85
86
87
88


-- ---------------------------------------------------------------------------
--		Compile a file
--  	This is used in batch mode 
compileFile :: GhcMode -> DynFlags -> FilePath -> IO FilePath
compileFile mode dflags src = do
   exists <- doesFileExist src
   when (not exists) $ 
   	throwDyn (CmdLineError ("file `" ++ src ++ "' does not exist"))
   
89
90
91
   split    <- readIORef v_Split_object_files
   o_file   <- readIORef v_Output_file
   ghc_link <- readIORef v_GhcLink	-- Set by -c or -no-link
92
93
	-- When linking, the -o argument refers to the linker's output.	
	-- otherwise, we use it as the name for the pipeline's output.
94
95
96
97
98
   let maybe_o_file
	 | isLinkMode mode && not (isNoLink ghc_link) = Nothing
		-- -o foo applies to linker
	 | otherwise = o_file
		-- -o foo applies to the file we are compiling now
99
100
101
102
103

       stop_phase = case mode of 
			StopBefore As | split -> SplitAs
			StopBefore phase      -> phase
			other		      -> StopLn
104

105
   mode_flag_string <- readIORef v_GhcModeFlag
106
107
   (_, out_file) <- runPipeline stop_phase mode_flag_string dflags
			 True maybe_o_file src Nothing{-no ModLocation-}
108
109
110
   return out_file


111
112
-- ---------------------------------------------------------------------------
-- Compile
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

-- Compile a single module, under the control of the compilation manager.
--
-- This is the interface between the compilation manager and the
-- compiler proper (hsc), where we deal with tedious details like
-- reading the OPTIONS pragma from the source file, and passing the
-- output of hsc through the C compiler.

-- The driver sits between 'compile' and 'hscMain', translating calls
-- to the former into calls to the latter, and results from the latter
-- into results from the former.  It does things like preprocessing
-- the .hs file if necessary, and compiling up the .stub_c files to
-- generate Linkables.

-- NB.  No old interface can also mean that the source has changed.

129
compile :: HscEnv
130
131
132
133
	-> ModSummary
	-> Bool			-- True <=> source unchanged
	-> Bool			-- True <=> have object
        -> Maybe ModIface       -- Old interface, if available
134
135
136
        -> IO CompResult

data CompResult
137
   = CompOK   ModDetails 		-- New details
138
139
140
141
              ModIface			-- New iface
              (Maybe Linkable)	-- New code; Nothing => compilation was not reqd
		                --			(old code is still valid)

142
   | CompErrs 
143
144


145
146
compile hsc_env mod_summary
	source_unchanged have_object old_iface = do 
147

148
149
150
   let dyn_flags   = hsc_dflags hsc_env
       this_mod    = ms_mod mod_summary
       src_flavour = ms_hsc_src mod_summary
151

152
   showPass dyn_flags ("Compiling " ++ showModMsg have_object mod_summary)
153
154

   let verb	  = verbosity dyn_flags
155
   let location	  = ms_location mod_summary
156
   let input_fn   = expectJust "compile:hs" (ml_hs_file location) 
157
   let input_fnpp = expectJust "compile:hspp" (ms_hspp_file mod_summary)
158
159
160

   when (verb >= 2) (hPutStrLn stderr ("compile: input file " ++ input_fnpp))

161
162
163
   -- Add in the OPTIONS from the source file
   -- This is nasty: we've done this once already, in the compilation manager
   -- It might be better to cache the flags in the ml_hspp_file field,say
164
   opts <- getOptionsFromSource input_fnpp
165
166
   (dyn_flags,unhandled_flags) <- processDynamicFlags opts dyn_flags
   checkProcessArgsResult unhandled_flags input_fn
167

168
   let (basename, _) = splitFilename input_fn
169

170
171
172
173
174
175
176
177
178
  -- We add the directory in which the .hs files resides) to the import path.
  -- This is needed when we try to compile the .hc file later, if it
  -- imports a _stub.h file that we created here.
   let current_dir = directoryOf basename
   old_paths <- readIORef v_Include_paths
   writeIORef v_Include_paths (current_dir : old_paths)
   -- put back the old include paths afterward.
   later (writeIORef v_Include_paths old_paths) $ do

179
   -- Figure out what lang we're generating
180
   hsc_lang <- hscMaybeAdjustTarget StopLn src_flavour (hscTarget dyn_flags)
181
182
183
184
185
   -- ... and what the next phase should be
   next_phase <- hscNextPhase src_flavour hsc_lang
   -- ... and what file to generate the output into
   get_output_fn <- genOutputFilenameFunc next_phase False Nothing basename
   output_fn     <- get_output_fn next_phase (Just location)
186

187
   let dyn_flags' = dyn_flags { hscTarget = hsc_lang,
188
				hscOutName = output_fn,
189
190
191
192
193
				hscStubCOutName = basename ++ "_stub.c",
				hscStubHOutName = basename ++ "_stub.h",
				extCoreName = basename ++ ".hcr" }

   -- -no-recomp should also work with --make
194
195
   let do_recomp = recompFlag dyn_flags
       source_unchanged' = source_unchanged && do_recomp
196
       hsc_env' = hsc_env { hsc_dflags = dyn_flags' }
197
198

   -- run the compiler
199
   hsc_result <- hscMain hsc_env' printErrorsAndWarnings mod_summary
200
201
202
			 source_unchanged' have_object old_iface

   case hsc_result of
203
      HscFail -> return CompErrs
204

205
      HscNoRecomp details iface -> return (CompOK details iface Nothing)
206

207
      HscRecomp details iface
208
209
210
		stub_h_exists stub_c_exists maybe_interpreted_code 

	| isHsBoot src_flavour	-- No further compilation to do
211
	-> return (CompOK details iface Nothing)
212
213
214

	| otherwise		-- Normal Haskell source files
	-> do
215
216
217
218
219
220
221
222
223
224
225
226
227
228
	   let 
	   maybe_stub_o <- compileStub dyn_flags' stub_c_exists
	   let stub_unlinked = case maybe_stub_o of
				  Nothing -> []
				  Just stub_o -> [ DotO stub_o ]

	   (hs_unlinked, unlinked_time) <-
	     case hsc_lang of

		-- in interpreted mode, just return the compiled code
		-- as our "unlinked" object.
		HscInterpreted -> 
		    case maybe_interpreted_code of
#ifdef GHCI
229
		       Just comp_bc -> return ([BCOs comp_bc], ms_hs_date mod_summary)
230
231
232
233
234
235
			-- Why do we use the timestamp of the source file here,
			-- rather than the current time?  This works better in
			-- the case where the local clock is out of sync
			-- with the filesystem's clock.  It's just as accurate:
			-- if the source is modified, then the linkable will
			-- be out of date.
236
237
238
239
#endif
		       Nothing -> panic "compile: no interpreted code"

		-- we're in batch mode: finish the compilation pipeline.
240
241
242
		_other -> do
		   let object_filename = ml_obj_file location

243
		   runPipeline StopLn "" dyn_flags
244
			       True Nothing output_fn (Just location)
245
			-- the object filename comes from the ModLocation
246
247
248

		   o_time <- getModificationTime object_filename
		   return ([DotO object_filename], o_time)
249

250
	   let linkable = LM unlinked_time this_mod
251
252
			     (hs_unlinked ++ stub_unlinked)

253
	   return (CompOK details iface (Just linkable))
254

255
-----------------------------------------------------------------------------
256
257
258
259
260
261
262
-- stub .h and .c files (for foreign export support)

compileStub dflags stub_c_exists
  | not stub_c_exists = return Nothing
  | stub_c_exists = do
	-- compile the _stub.c file w/ gcc
	let stub_c = hscStubCOutName dflags
263
	(_, stub_o) <- runPipeline StopLn "stub-compile" dflags
264
265
266
267
			    True{-persistent output-} 
			    Nothing{-no specific output file-}
			    stub_c
			    Nothing{-no ModLocation-}
268
269
270
	return (Just stub_o)


271
272
-- ---------------------------------------------------------------------------
-- Link
273
274
275
276

link :: GhciMode		-- interactive or batch
     -> DynFlags		-- dynamic flags
     -> Bool			-- attempt linking in batch mode?
277
     -> HomePackageTable	-- what to link
278
279
280
281
282
283
284
285
286
287
     -> IO SuccessFlag

-- For the moment, in the batch linker, we don't bother to tell doLink
-- which packages to link -- it just tries all that are available.
-- batch_attempt_linking should only be *looked at* in batch mode.  It
-- should only be True if the upsweep was successful and someone
-- exports main, i.e., we have good reason to believe that linking
-- will succeed.

#ifdef GHCI
288
289
link Interactive dflags batch_attempt_linking hpt
    = do -- Not Linking...(demand linker will do the job)
290
291
292
	 return Succeeded
#endif

293
link Batch dflags batch_attempt_linking hpt
294
   | batch_attempt_linking
295
   = do 
296
297
298
299
300
301
302
303
304
305
306
307
308
	let 
	    home_mod_infos = moduleEnvElts hpt

	    -- the packages we depend on
	    pkg_deps  = concatMap (dep_pkgs . mi_deps . hm_iface) home_mod_infos

	    -- the linkables to link
	    linkables = map hm_linkable home_mod_infos

        when (verb >= 3) $ do
	     hPutStrLn stderr "link: linkables are ..."
             hPutStrLn stderr (showSDoc (vcat (map ppr linkables)))

309
	-- check for the -no-link flag
310
311
	ghc_link <- readIORef v_GhcLink
	if isNoLink ghc_link
312
	  then do when (verb >= 3) $
313
		    hPutStrLn stderr "link(batch): linking omitted (-c flag given)."
314
315
316
317
318
	          return Succeeded
	  else do

	when (verb >= 1) $
             hPutStrLn stderr "Linking ..."
319

320
321
322
	let getOfiles (LM _ _ us) = map nameOfObject (filter isObject us)
	    obj_files = concatMap getOfiles linkables

323
	-- Don't showPass in Batch mode; doLink will do that for us.
324
        staticLink dflags obj_files pkg_deps
325
326

        when (verb >= 3) (hPutStrLn stderr "link: done")
327
328
329
330
331
332
333
334
335
336
337

	-- staticLink only returns if it succeeds
        return Succeeded

   | otherwise
   = do when (verb >= 3) $ do
	    hPutStrLn stderr "link(batch): upsweep (partially) failed OR"
            hPutStrLn stderr "   Main.main not exported; not linking."
        return Succeeded
   where
      verb = verbosity dflags
338
      
339
340
-- ---------------------------------------------------------------------------
-- Run a compilation pipeline, consisting of multiple phases.
341

342
343
344
345
-- The DynFlags can be modified by phases in the pipeline (eg. by
-- OPTIONS pragmas), and the changes affect later phases in the
-- pipeline, but we throw away the resulting DynFlags at the end.

346
runPipeline
347
348
349
350
351
352
353
  :: Phase		-- When to stop
  -> String		-- "GhcMode" flag as a string
  -> DynFlags		-- Dynamic flags
  -> Bool		-- Final output is persistent?
  -> Maybe FilePath	-- Where to put the output, optionally
  -> FilePath 		-- Input filename
  -> Maybe ModLocation  -- A ModLocation for this module, if we have one
354
  -> IO (DynFlags, FilePath)	-- (final flags, output filename)
355

356
runPipeline stop_phase mode_flag_string dflags keep_output 
357
  maybe_output_filename input_fn maybe_loc
358
359
360
  = do
  let (basename, suffix) = splitFilename input_fn
      start_phase = startPhase suffix
361

362
363
364
365
366
367
  -- We want to catch cases of "you can't get there from here" before
  -- we start the pipeline, because otherwise it will just run off the
  -- end.
  --
  -- There is a partial ordering on phases, where A < B iff A occurs
  -- before B in a normal compilation pipeline.
368

369
370
  when (not (start_phase `happensBefore` stop_phase)) $
	throwDyn (UsageError 
371
		    ("flag `" ++ mode_flag_string
372
373
374
375
376
		     ++ "' is incompatible with source file `"
		     ++ input_fn ++ "'"))

  -- generate a function which will be used to calculate output file names
  -- as we go along.
377
378
  get_output_fn <- genOutputFilenameFunc stop_phase keep_output 
					 maybe_output_filename basename
379

380
  -- Execute the pipeline...
381
  (dflags', output_fn, maybe_loc) <- pipeLoop dflags start_phase stop_phase input_fn 
382
				  	      basename suffix get_output_fn maybe_loc
383

384
  -- Sometimes, a compilation phase doesn't actually generate any output
385
386
387
  -- (eg. the CPP phase when -fcpp is not turned on).  If we end on this
  -- stage, but we wanted to keep the output, then we have to explicitly
  -- copy the file.
388
  if keep_output 
389
	then do final_fn <- get_output_fn stop_phase maybe_loc
390
	        when (final_fn /= output_fn) $
391
	 	  copy dflags ("Copying `" ++ output_fn ++ "' to `" ++ final_fn
392
			++ "'") output_fn final_fn
393
	        return (dflags', final_fn)
394
	else
395
	        return (dflags', output_fn)
396
397


398
pipeLoop :: DynFlags -> Phase -> Phase 
399
400
401
402
	 -> FilePath  -> String -> Suffix
	 -> (Phase -> Maybe ModLocation -> IO FilePath)
	 -> Maybe ModLocation
	 -> IO (DynFlags, FilePath, Maybe ModLocation)
403

404
pipeLoop dflags phase stop_phase 
405
406
	 input_fn orig_basename orig_suff 
	 orig_get_output_fn maybe_loc
407

408
409
  | phase `eqPhase` stop_phase		  -- All done
  = return (dflags, input_fn, maybe_loc)
410

411
  | not (phase `happensBefore` stop_phase)
412
413
414
415
	-- Something has gone wrong.  We'll try to cover all the cases when
	-- this could happen, so if we reach here it is a panic.
	-- eg. it might happen if the -C flag is used on a source file that
	-- has {-# OPTIONS -fasm #-}.
416
417
418
419
420
  = panic ("pipeLoop: at phase " ++ show phase ++ 
	   " but I wanted to stop at phase " ++ show stop_phase)

  | otherwise 
  = do	{ (next_phase, dflags', maybe_loc, output_fn)
421
		<- runPhase phase stop_phase dflags orig_basename 
422
			    orig_suff input_fn orig_get_output_fn maybe_loc
423
	; pipeLoop dflags' next_phase stop_phase output_fn
424
425
426
		   orig_basename orig_suff orig_get_output_fn maybe_loc }

genOutputFilenameFunc :: Phase -> Bool -> Maybe FilePath -> String
427
  -> IO (Phase{-next phase-} -> Maybe ModLocation -> IO FilePath)
428
genOutputFilenameFunc stop_phase keep_final_output maybe_output_filename basename
429
 = do
430
   hcsuf      <- readIORef v_HC_suf
431
   odir       <- readIORef v_Output_dir
432
   osuf       <- readIORef v_Object_suf
433
   keep_hc    <- readIORef v_Keep_hc_files
sof's avatar
sof committed
434
#ifdef ILX
rrt's avatar
rrt committed
435
   keep_il    <- readIORef v_Keep_il_files
sof's avatar
sof committed
436
437
   keep_ilx   <- readIORef v_Keep_ilx_files
#endif
438
439
   keep_raw_s <- readIORef v_Keep_raw_s_files
   keep_s     <- readIORef v_Keep_s_files
440
   let
441
442
443
        myPhaseInputExt HCc    = hcsuf
        myPhaseInputExt StopLn = osuf
        myPhaseInputExt other  = phaseInputExt other
444

445
	func next_phase maybe_location
446
447
448
449
450
		| is_last_phase, Just f <- maybe_output_filename = return f
		| is_last_phase && keep_final_output = persistent_fn
		| keep_this_output 		     = persistent_fn
     		| otherwise        		     = newTempName suffix

451
	   where
452
		is_last_phase = next_phase `eqPhase` stop_phase
453
454
455
456

		-- sometimes, we keep output from intermediate stages
		keep_this_output = 
     		     case next_phase of
457
     			     StopLn              -> True
458
459
460
461
462
     			     Mangle | keep_raw_s -> True
     			     As     | keep_s     -> True
     			     HCc    | keep_hc    -> True
     			     _other              -> False

463
		suffix = myPhaseInputExt next_phase
464
465
466

		-- persistent object files get put in odir
	        persistent_fn 
467
468
		   | StopLn <- next_phase = return odir_persistent
		   | otherwise            = return persistent
469

470
		persistent = basename ++ '.':suffix
471

472
		odir_persistent
473
		   | Just loc <- maybe_location = ml_obj_file loc
474
475
476
		   | Just d <- odir = replaceFilenameDirectory persistent d
		   | otherwise      = persistent

477
   return func
478
479


480
481
482
483
484
485
486
487
488
489
-- -----------------------------------------------------------------------------
-- Each phase in the pipeline returns the next phase to execute, and the
-- name of the file in which the output was placed.
--
-- We must do things dynamically this way, because we often don't know
-- what the rest of the phases will be until part-way through the
-- compilation: for example, an {-# OPTIONS -fasm #-} at the beginning
-- of a source file can change the latter stages of the pipeline from
-- taking the via-C route to using the native code generator.

490
491
runPhase :: Phase	-- Do this phase first
	 -> Phase	-- Stop just before this phase
492
493
494
495
496
	 -> DynFlags
	 -> String	-- basename of original input source
	 -> String	-- its extension
	 -> FilePath	-- name of file which contains the input to this phase.
	 -> (Phase -> Maybe ModLocation -> IO FilePath)
497
			-- how to calculate the output filename
498
	 -> Maybe ModLocation		-- the ModLocation, if we have one
499
	 -> IO (Phase,	  		-- next phase
500
501
502
		DynFlags,		-- new dynamic flags
		Maybe ModLocation,	-- the ModLocation, if we have one
		FilePath)		-- output filename
503

504
505
506
507
	-- Invariant: the output filename always contains the output
	-- Interesting case: Hsc when there is no recompilation to do
	--		     Then the output filename is still a .o file 

508
509
510
-------------------------------------------------------------------------------
-- Unlit phase 

511
runPhase (Unlit sf) _stop dflags _basename _suff input_fn get_output_fn maybe_loc
512
  = do let unlit_flags = getOpts dflags opt_L
rrt's avatar
rrt committed
513
       -- The -h option passes the file name for unlit to put in a #line directive
514
       output_fn <- get_output_fn (Cpp sf) maybe_loc
515

516
517
       SysTools.runUnlit dflags 
		(map SysTools.Option unlit_flags ++
sof's avatar
sof committed
518
519
       			  [ SysTools.Option     "-h"
 			  , SysTools.Option     input_fn
sof's avatar
sof committed
520
521
			  , SysTools.FileOption "" input_fn
			  , SysTools.FileOption "" output_fn
sof's avatar
sof committed
522
			  ])
523

524
       return (Cpp sf, dflags, maybe_loc, output_fn)
525
526

-------------------------------------------------------------------------------
527
528
-- Cpp phase : (a) gets OPTIONS out of file
--	       (b) runs cpp if necessary
529

530
runPhase (Cpp sf) _stop dflags basename suff input_fn get_output_fn maybe_loc
531
  = do src_opts <- getOptionsFromSource input_fn
532
533
       (dflags,unhandled_flags) <- processDynamicFlags src_opts dflags
       checkProcessArgsResult unhandled_flags (basename++'.':suff)
534

535
       if not (cppFlag dflags) then
sof's avatar
sof committed
536
537
           -- no need to preprocess CPP, just pass input file along
	   -- to the next phase of the pipeline.
538
          return (HsPp sf, dflags, maybe_loc, input_fn)
sof's avatar
sof committed
539
	else do
540
	    output_fn <- get_output_fn (HsPp sf) maybe_loc
541
	    doCpp dflags True{-raw-} False{-no CC opts-} input_fn output_fn
542
	    return (HsPp sf, dflags, maybe_loc, output_fn)
543

sof's avatar
sof committed
544
545
546
-------------------------------------------------------------------------------
-- HsPp phase 

547
runPhase (HsPp sf) _stop dflags basename suff input_fn get_output_fn maybe_loc
548
  = do if not (ppFlag dflags) then
sof's avatar
sof committed
549
550
           -- no need to preprocess, just pass input file along
	   -- to the next phase of the pipeline.
551
          return (Hsc sf, dflags, maybe_loc, input_fn)
sof's avatar
sof committed
552
	else do
553
	    let hspp_opts = getOpts dflags opt_F
sof's avatar
sof committed
554
       	    hs_src_pp_opts <- readIORef v_Hs_source_pp_opts
555
	    let orig_fn = basename ++ '.':suff
556
	    output_fn <- get_output_fn (Hsc sf) maybe_loc
557
558
	    SysTools.runPp dflags
			   ( [ SysTools.Option     orig_fn
sof's avatar
sof committed
559
560
561
562
563
564
			     , SysTools.Option     input_fn
			     , SysTools.FileOption "" output_fn
			     ] ++
			     map SysTools.Option hs_src_pp_opts ++
			     map SysTools.Option hspp_opts
			   )
565
	    return (Hsc sf, dflags, maybe_loc, output_fn)
566

567
568
569
-----------------------------------------------------------------------------
-- Hsc phase

570
571
-- Compilation of a single module, in "legacy" mode (_not_ under
-- the direction of the compilation manager).
572
runPhase (Hsc src_flavour) stop dflags basename suff input_fn get_output_fn _maybe_loc 
573
 = do	-- normal Hsc mode, not mkdependHS
574

575
576
577
  -- we add the current directory (i.e. the directory in which
  -- the .hs files resides) to the import path, since this is
  -- what gcc does, and it's probably what you want.
578
	let current_dir = directoryOf basename
579
	
580
581
	paths <- readIORef v_Include_paths
	writeIORef v_Include_paths (current_dir : paths)
582
	
583
  -- gather the imports and module name
584
        (hspp_buf,mod_name) <- 
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
            case src_flavour of
		ExtCoreFile -> do {  -- no explicit imports in ExtCore input.
			          ; m <- getCoreModuleName input_fn
			          ; return (Nothing, mkModule m) }

		other -> do { buf <- hGetStringBuffer input_fn
			    ; (_,_,mod_name) <- getImports dflags buf input_fn
			    ; return (Just buf, mod_name) }

  -- Build a ModLocation to pass to hscMain.
  -- The source filename is rather irrelevant by now, but it's used
  -- by hscMain for messages.  hscMain also needs 
  -- the .hi and .o filenames, and this is as good a way
  -- as any to generate them, and better than most. (e.g. takes 
  -- into accout the -osuf flags)
	location1 <- mkHomeModLocation2 mod_name basename suff

  -- Boot-ify it if necessary
	let location2 | isHsBoot src_flavour = addBootSuffixLocn location1
		      | otherwise	     = location1 
					

  -- Take -ohi into account if present
  -- This can't be done in mkHomeModuleLocation because
  -- it only applies to the module being compiles
610
	ohi <- readIORef v_Output_hi
611
612
613
614
615
616
617
618
619
	let location3 | Just fn <- ohi = location2{ ml_hi_file = fn }
		      | otherwise      = location2

  -- Take -o into account if present
  -- Very like -ohi, but we must *only* do this if we aren't linking
  -- (If we're linking then the -o applies to the linked thing, not to
  -- the object file for one module.)
  -- Note the nasty duplication with the same computation in compileFile above
	expl_o_file <- readIORef v_Output_file
620
621
622
	ghc_link     <- readIORef v_GhcLink
	let location4 | Just ofile <- expl_o_file
		      , isNoLink ghc_link 
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
		      = location3 { ml_obj_file = ofile }
		      | otherwise = location3

  -- Tell the finder cache about this module
	addHomeModuleToFinder mod_name location4

  -- Make the ModSummary to hand to hscMain
	src_timestamp <- getModificationTime (basename ++ '.':suff)
	let
	    unused_field = panic "runPhase:ModSummary field"
		-- Some fields are not looked at by hscMain
	    mod_summary = ModSummary {	ms_mod 	     = mod_name, 
					ms_hsc_src   = src_flavour,
				 	ms_hspp_file = Just input_fn,
					ms_hspp_buf  = hspp_buf,
					ms_location  = location4,
					ms_hs_date   = src_timestamp,
					ms_imps	     = unused_field,
					ms_srcimps   = unused_field }

	    o_file = ml_obj_file location4 	-- The real object file


  -- Figure out if the source has changed, for recompilation avoidance.
647
  --
648
  -- Setting source_unchanged to True means that M.o seems
649
650
  -- to be up to date wrt M.hs; so no need to recompile unless imports have
  -- changed (which the compiler itself figures out).
651
  -- Setting source_unchanged to False tells the compiler that M.o is out of
652
  -- date wrt M.hs (or M.o doesn't exist) so we must recompile regardless.
653
	let do_recomp = recompFlag dflags
654
	source_unchanged <- 
655
          if not do_recomp || not (isStopLn stop)
656
657
		-- Set source_unchanged to False unconditionally if
		--	(a) recompilation checker is off, or
658
		-- 	(b) we aren't going all the way to .o file (e.g. ghc -S)
659
660
	     then return False	
		-- Otherwise look at file modification dates
661
	     else do o_file_exists <- doesFileExist o_file
662
		     if not o_file_exists
663
		        then return False	-- Need to recompile
664
			else do t2 <- getModificationTime o_file
665
			        if t2 > src_timestamp
666
667
				  then return True
				  else return False
668

669
  -- get the DynFlags
670
	hsc_lang   <- hscMaybeAdjustTarget stop src_flavour (hscTarget dflags)
671
672
	next_phase <- hscNextPhase src_flavour hsc_lang
	output_fn  <- get_output_fn next_phase (Just location4)
673

674
        let dflags' = dflags { hscTarget = hsc_lang,
675
676
677
678
			       hscOutName = output_fn,
		   	       hscStubCOutName = basename ++ "_stub.c",
			       hscStubHOutName = basename ++ "_stub.h",
			       extCoreName = basename ++ ".hcr" }
679

680
	hsc_env <- newHscEnv OneShot dflags'
681

682
  -- run the compiler!
683
684
685
686
	result <- hscMain hsc_env printErrorsAndWarnings
			  mod_summary source_unchanged 
			  False		-- No object file
			  Nothing	-- No iface
687

688
	case result of
689

690
	    HscFail -> throwDyn (PhaseFailed "hsc" (ExitFailure 1))
691

692
            HscNoRecomp details iface -> do
693
		SysTools.touch dflags' "Touching object file" o_file
694
		return (StopLn, dflags', Just location4, o_file)
695

696
	    HscRecomp _details _iface 
697
		      stub_h_exists stub_c_exists
698
		      _maybe_interpreted_code -> do
699

700
		-- Deal with stubs 
701
		maybe_stub_o <- compileStub dflags' stub_c_exists
702
		case maybe_stub_o of
703
		      Nothing     -> return ()
704
		      Just stub_o -> add v_Ld_inputs stub_o
705
706
707
708
709
710
711
712

		-- In the case of hs-boot files, generate a dummy .o-boot 
		-- stamp file for the benefit of Make
		case src_flavour of
		  HsBootFile -> SysTools.touch dflags' "Touching object file" o_file
		  other	     -> return ()

		return (next_phase, dflags', Just location4, output_fn)
713

714
715
716
-----------------------------------------------------------------------------
-- Cmm phase

717
runPhase CmmCpp stop dflags basename suff input_fn get_output_fn maybe_loc
718
719
  = do
       output_fn <- get_output_fn Cmm maybe_loc
720
       doCpp dflags False{-not raw-} True{-include CC opts-} input_fn output_fn	
721
       return (Cmm, dflags, maybe_loc, output_fn)
722

723
runPhase Cmm stop dflags basename suff input_fn get_output_fn maybe_loc
724
  = do
725
	hsc_lang <- hscMaybeAdjustTarget stop HsSrcFile (hscTarget dflags)
726
	next_phase <- hscNextPhase HsSrcFile hsc_lang
727
728
	output_fn <- get_output_fn next_phase maybe_loc

729
        let dflags' = dflags { hscTarget = hsc_lang,
730
731
732
733
			       hscOutName = output_fn,
		   	       hscStubCOutName = basename ++ "_stub.c",
			       hscStubHOutName = basename ++ "_stub.h",
			       extCoreName = basename ++ ".hcr" }
734

735
	ok <- hscCmmFile dflags' input_fn
736
737
738

	when (not ok) $ throwDyn (PhaseFailed "cmm" (ExitFailure 1))

739
	return (next_phase, dflags, maybe_loc, output_fn)
740

741
742
743
744
745
746
-----------------------------------------------------------------------------
-- Cc phase

-- we don't support preprocessing .c files (with -E) now.  Doing so introduces
-- way too many hacks, and I can't say I've ever used it anyway.

747
runPhase cc_phase stop dflags basename suff input_fn get_output_fn maybe_loc
748
   | cc_phase `eqPhase` Cc || cc_phase `eqPhase` HCc
749
   = do	let cc_opts = getOpts dflags opt_c
750
	    hcc = cc_phase `eqPhase` HCc
751

752
       	cmdline_include_paths <- readIORef v_Include_paths
753

754
755
756
757
758
759
	-- HC files have the dependent packages stamped into them
	pkgs <- if hcc then getHCFilePackages input_fn else return []

	-- add package include paths even if we're just compiling .c
	-- files; this is the Value Add(TM) that using ghc instead of
	-- gcc gives you :)
760
        pkg_include_dirs <- getPackageIncludePath dflags pkgs
rrt's avatar
rrt committed
761
762
        let include_paths = foldr (\ x xs -> "-I" : x : xs) []
			      (cmdline_include_paths ++ pkg_include_dirs)
763

764
	(md_c_flags, md_regd_c_flags) <- machdepCCOpts dflags
765
        pic_c_flags <- picCCOpts dflags
766

767
        let verb = getVerbFlag dflags
768

769
	pkg_extra_cc_opts <- getPackageExtraCcOpts dflags pkgs
770

771
	split_objs <- readIORef v_Split_object_files
sof's avatar
sof committed
772
773
	let split_opt | hcc && split_objs = [ "-DUSE_SPLIT_MARKERS" ]
		      | otherwise         = [ ]
774

775
	excessPrecision <- readIORef v_Excess_precision
776

777
778
779
780
781
782
783
	-- Decide next phase
	mangle <- readIORef v_Do_asm_mangling
        let next_phase
		| hcc && mangle     = Mangle
		| otherwise         = As
	output_fn <- get_output_fn next_phase maybe_loc

784
785
	-- force the C compiler to interpret this file as C when
	-- compiling .hc files, by adding the -x c option.
786
787
	let langopt | hcc = [ SysTools.Option "-x", SysTools.Option "c"]
		    | otherwise = [ ]
788

789
	SysTools.runCc dflags (langopt ++
790
			[ SysTools.FileOption "" input_fn
sof's avatar
sof committed
791
			, SysTools.Option "-o"
sof's avatar
sof committed
792
			, SysTools.FileOption "" output_fn
sof's avatar
sof committed
793
794
795
			]
		       ++ map SysTools.Option (
		          md_c_flags
796
                       ++ pic_c_flags
797
		       ++ (if hcc && mangle
798
799
		  	     then md_regd_c_flags
		  	     else [])
800
		       ++ [ verb, "-S", "-Wimplicit", "-O" ]
801
802
		       ++ [ "-D__GLASGOW_HASKELL__="++cProjectVersionInt ]
		       ++ cc_opts
sof's avatar
sof committed
803
		       ++ split_opt
804
805
806
		       ++ (if excessPrecision then [] else [ "-ffloat-store" ])
		       ++ include_paths
		       ++ pkg_extra_cc_opts
sof's avatar
sof committed
807
		       ))
808

809
	return (next_phase, dflags, maybe_loc, output_fn)
810
811
812
813
814
815

	-- ToDo: postprocess the output from gcc

-----------------------------------------------------------------------------
-- Mangle phase

816
runPhase Mangle stop dflags _basename _suff input_fn get_output_fn maybe_loc
817
   = do let mangler_opts = getOpts dflags opt_m
818
819
820
821
822
823

#if i386_TARGET_ARCH
        machdep_opts <- return [ show (stolen_x86_regs dflags) ]
#else
	machdep_opts <- return []
#endif
824
825
826
827
828

	split <- readIORef v_Split_object_files
	let next_phase
		| split = SplitMangle
		| otherwise = As
829
	output_fn <- get_output_fn next_phase maybe_loc
830

831
	SysTools.runMangle dflags (map SysTools.Option mangler_opts
sof's avatar
sof committed
832
833
		          ++ [ SysTools.FileOption "" input_fn
			     , SysTools.FileOption "" output_fn
sof's avatar
sof committed
834
835
			     ]
			  ++ map SysTools.Option machdep_opts)
836

837
	return (next_phase, dflags, maybe_loc, output_fn)
838
839
840
841

-----------------------------------------------------------------------------
-- Splitting phase

842
runPhase SplitMangle stop dflags _basename _suff input_fn get_output_fn maybe_loc
843
844
845
846
  = do  -- tmp_pfx is the prefix used for the split .s files
	-- We also use it as the file to contain the no. of split .s files (sigh)
	split_s_prefix <- SysTools.newTempName "split"
	let n_files_fn = split_s_prefix
847

848
849
	SysTools.runSplit dflags
			  [ SysTools.FileOption "" input_fn
sof's avatar
sof committed
850
851
			  , SysTools.FileOption "" split_s_prefix
			  , SysTools.FileOption "" n_files_fn
sof's avatar
sof committed
852
			  ]
853
854
855
856
857
858
859
860
861

	-- Save the number of split files for future references
	s <- readFile n_files_fn
	let n_files = read s :: Int
	writeIORef v_Split_info (split_s_prefix, n_files)

	-- Remember to delete all these files
	addFilesToClean [ split_s_prefix ++ "__" ++ show n ++ ".s"
			| n <- [1..n_files]]
862

863
	return (SplitAs, dflags, maybe_loc, "**splitmangle**")
864
	  -- we don't use the filename
865
866
867
868

-----------------------------------------------------------------------------
-- As phase

869
runPhase As stop dflags _basename _suff input_fn get_output_fn maybe_loc
870
  = do	let as_opts =  getOpts dflags opt_a
871
        cmdline_include_paths <- readIORef v_Include_paths
872

873
	output_fn <- get_output_fn StopLn maybe_loc
874

875
876
877
878
	-- we create directories for the object file, because it
	-- might be a hierarchical module.
	createDirectoryHierarchy (directoryOf output_fn)

879
880
	SysTools.runAs dflags	
		       (map SysTools.Option as_opts
sof's avatar
sof committed
881
882
		       ++ [ SysTools.Option ("-I" ++ p) | p <- cmdline_include_paths ]
		       ++ [ SysTools.Option "-c"
sof's avatar
sof committed
883
		          , SysTools.FileOption "" input_fn
sof's avatar
sof committed
884
			  , SysTools.Option "-o"
sof's avatar
sof committed
885
			  , SysTools.FileOption "" output_fn
sof's avatar
sof committed
886
			  ])
887

888
	return (StopLn, dflags, maybe_loc, output_fn)
889
890


891
runPhase SplitAs stop dflags basename _suff _input_fn get_output_fn maybe_loc
892
  = do  let as_opts = getOpts dflags opt_a
893

894
	(split_s_prefix, n) <- readIORef v_Split_info
895

896
	odir <- readIORef v_Output_dir
897
	let real_odir = case odir of
898
				Nothing -> basename ++ "_split"
899
900
				Just d  -> d

901
902
	let assemble_file n
	      = do  let input_s  = split_s_prefix ++ "__" ++ show n ++ ".s"
903
		    let output_o = replaceFilenameDirectory
904
					(basename ++ "__" ++ show n ++ ".o")
905
					 real_odir
906
		    real_o <- osuf_ify output_o
907
908
		    SysTools.runAs dflags
				 (map SysTools.Option as_opts ++
sof's avatar
sof committed
909
910
		    		    [ SysTools.Option "-c"
				    , SysTools.Option "-o"
sof's avatar
sof committed
911
912
				    , SysTools.FileOption "" real_o
				    , SysTools.FileOption "" input_s
sof's avatar
sof committed
913
				    ])
914
915
	
	mapM_ assemble_file [1..n]
916

917
918
	output_fn <- get_output_fn StopLn maybe_loc
	return (StopLn, dflags, maybe_loc, output_fn)
919

rrt's avatar
rrt committed
920
921
922
923
924
#ifdef ILX
-----------------------------------------------------------------------------
-- Ilx2Il phase
-- Run ilx2il over the ILX output, getting an IL file

925
runPhase Ilx2Il stop dflags _basename _suff input_fn get_output_fn maybe_loc
926
  = do	let ilx2il_opts = getOpts dflags opt_I
rrt's avatar
rrt committed
927
928
929
930
        SysTools.runIlx2il (map SysTools.Option ilx2il_opts
                           ++ [ SysTools.Option "--no-add-suffix-to-assembly",
				SysTools.Option "mscorlib",
				SysTools.Option "-o",
sof's avatar
sof committed
931
932
				SysTools.FileOption "" output_fn,
				SysTools.FileOption "" input_fn ])
933
	return True