1 -----------------------------------------------------------------------------
5 -- (c) The University of Glasgow 2002
7 -----------------------------------------------------------------------------
9 #include "../includes/config.h"
11 module DriverPipeline (
13 -- Interfaces for the batch-mode driver
14 genPipeline, runPipeline, pipeLoop, staticLink,
16 -- Interfaces for the compilation manager (interpreted/batch-mode)
18 compile, CompResult(..),
25 #include "HsVersions.h"
34 import SysTools ( newTempName, addFilesToClean, getSysMan, copy )
35 import qualified SysTools
46 import BasicTypes ( SuccessFlag(..) )
47 import Maybes ( expectJust )
49 import ParserCoreUtils ( getCoreModuleName )
52 import DATA_IOREF ( readIORef, writeIORef )
55 import Time ( getClockTime )
64 -----------------------------------------------------------------------------
66 -----------------------------------------------------------------------------
68 -- Just preprocess a file, put the result in a temp. file (used by the
69 -- compilation manager during the summary phase).
71 preprocess :: FilePath -> IO FilePath
73 ASSERT(haskellish_src_file filename)
74 do restoreDynFlags -- Restore to state of last save
75 let fInfo = (filename, getFileSuffix filename)
76 pipeline <- genPipeline (StopBefore Hsc) ("preprocess") False
78 (fn,_) <- runPipeline pipeline fInfo
79 False{-no linking-} False{-no -o flag-}
82 -----------------------------------------------------------------------------
84 -----------------------------------------------------------------------------
86 -- Compile a single module, under the control of the compilation manager.
88 -- This is the interface between the compilation manager and the
89 -- compiler proper (hsc), where we deal with tedious details like
90 -- reading the OPTIONS pragma from the source file, and passing the
91 -- output of hsc through the C compiler.
93 -- The driver sits between 'compile' and 'hscMain', translating calls
94 -- to the former into calls to the latter, and results from the latter
95 -- into results from the former. It does things like preprocessing
96 -- the .hs file if necessary, and compiling up the .stub_c files to
97 -- generate Linkables.
99 -- NB. No old interface can also mean that the source has changed.
101 compile :: GhciMode -- distinguish batch from interactive
104 -> Bool -- True <=> source unchanged
105 -> Bool -- True <=> have object
106 -> Maybe ModIface -- old interface, if available
107 -> HomePackageTable -- For home-module stuff
108 -> PersistentCompilerState -- persistent compiler state
112 = CompOK PersistentCompilerState -- Updated PCS
113 ModDetails -- New details
114 ModIface -- New iface
115 (Maybe Linkable) -- New code; Nothing => compilation was not reqd
116 -- (old code is still valid)
118 | CompErrs PersistentCompilerState -- Updated PCS
121 compile ghci_mode this_mod location
122 source_unchanged have_object
123 old_iface hpt pcs = do
125 dyn_flags <- restoreDynFlags -- Restore to the state of the last save
129 (showSDoc (text "Compiling" <+> ppr this_mod))
131 let verb = verbosity dyn_flags
132 let input_fn = expectJust "compile:hs" (ml_hs_file location)
133 let input_fnpp = expectJust "compile:hspp" (ml_hspp_file location)
134 let mod_name = moduleName this_mod
136 when (verb >= 2) (hPutStrLn stderr ("compile: input file " ++ input_fnpp))
138 opts <- getOptionsFromSource input_fnpp
139 processArgs dynamic_flags opts []
140 dyn_flags <- getDynFlags
142 let hsc_lang = hscLang dyn_flags
143 (basename, _) = splitFilename input_fn
145 keep_hc <- readIORef v_Keep_hc_files
147 keep_il <- readIORef v_Keep_il_files
149 keep_s <- readIORef v_Keep_s_files
153 HscAsm | keep_s -> return (basename ++ '.':phaseInputExt As)
154 | otherwise -> newTempName (phaseInputExt As)
155 HscC | keep_hc -> return (basename ++ '.':phaseInputExt HCc)
156 | otherwise -> newTempName (phaseInputExt HCc)
157 HscJava -> newTempName "java" -- ToDo
159 HscILX | keep_il -> return (basename ++ '.':phaseInputExt Ilasm)
160 | otherwise -> newTempName (phaseInputExt Ilx2Il)
162 HscInterpreted -> return (error "no output file")
163 HscNothing -> return (error "no output file")
165 let dyn_flags' = dyn_flags { hscOutName = output_fn,
166 hscStubCOutName = basename ++ "_stub.c",
167 hscStubHOutName = basename ++ "_stub.h",
168 extCoreName = basename ++ ".hcr" }
170 -- figure out which header files to #include in a generated .hc file
171 c_includes <- getPackageCIncludes
172 cmdline_includes <- dynFlag cmdlineHcIncludes -- -#include options
174 let cc_injects = unlines (map mk_include
175 (c_includes ++ reverse cmdline_includes))
178 '"':_{-"-} -> "#include "++h_file
179 '<':_ -> "#include "++h_file
180 _ -> "#include \""++h_file++"\""
182 writeIORef v_HCHeader cc_injects
184 -- -no-recomp should also work with --make
185 do_recomp <- readIORef v_Recomp
186 let source_unchanged' = source_unchanged && do_recomp
187 hsc_env = HscEnv { hsc_mode = ghci_mode,
188 hsc_dflags = dyn_flags',
192 hsc_result <- hscMain hsc_env pcs this_mod location
193 source_unchanged' have_object old_iface
196 HscFail pcs -> return (CompErrs pcs)
198 HscNoRecomp pcs details iface -> return (CompOK pcs details iface Nothing)
200 HscRecomp pcs details iface
201 stub_h_exists stub_c_exists maybe_interpreted_code -> do
203 maybe_stub_o <- compileStub dyn_flags' stub_c_exists
204 let stub_unlinked = case maybe_stub_o of
206 Just stub_o -> [ DotO stub_o ]
208 (hs_unlinked, unlinked_time) <-
211 -- in interpreted mode, just return the compiled code
212 -- as our "unlinked" object.
214 case maybe_interpreted_code of
216 Just comp_bc -> do tm <- getClockTime
217 return ([BCOs comp_bc], tm)
219 Nothing -> panic "compile: no interpreted code"
221 -- we're in batch mode: finish the compilation pipeline.
222 _other -> do pipe <- genPipeline (StopBefore Ln) "" True
223 hsc_lang (output_fn, getFileSuffix output_fn)
224 -- runPipeline takes input_fn so it can split off
225 -- the base name and use it as the base of
226 -- the output object file.
227 let (basename, suffix) = splitFilename input_fn
229 pipeLoop pipe (output_fn, getFileSuffix output_fn)
232 o_time <- getModificationTime o_file
233 return ([DotO o_file], o_time)
235 let linkable = LM unlinked_time mod_name
236 (hs_unlinked ++ stub_unlinked)
238 return (CompOK pcs details iface (Just linkable))
240 -----------------------------------------------------------------------------
241 -- stub .h and .c files (for foreign export support)
243 compileStub dflags stub_c_exists
244 | not stub_c_exists = return Nothing
246 -- compile the _stub.c file w/ gcc
247 let stub_c = hscStubCOutName dflags
248 pipeline <- genPipeline (StopBefore Ln) "" True defaultHscLang (stub_c,"c")
249 (stub_o,_) <- runPipeline pipeline (stub_c,"c") False{-no linking-}
250 False{-no -o option-}
254 -----------------------------------------------------------------------------
256 -----------------------------------------------------------------------------
258 link :: GhciMode -- interactive or batch
259 -> DynFlags -- dynamic flags
260 -> Bool -- attempt linking in batch mode?
264 -- For the moment, in the batch linker, we don't bother to tell doLink
265 -- which packages to link -- it just tries all that are available.
266 -- batch_attempt_linking should only be *looked at* in batch mode. It
267 -- should only be True if the upsweep was successful and someone
268 -- exports main, i.e., we have good reason to believe that linking
271 -- There will be (ToDo: are) two lists passed to link. These
274 -- 1. The list of all linkables in the current home package. This is
275 -- used by the batch linker to link the program, and by the interactive
276 -- linker to decide which modules from the previous link it can
278 -- 2. The list of modules on which we just called "compile". This list
279 -- is used by the interactive linker to decide which modules need
280 -- to be actually linked this time around (or unlinked and re-linked
281 -- if the module was recompiled).
283 link mode dflags batch_attempt_linking linkables
284 = do let verb = verbosity dflags
285 when (verb >= 3) $ do
286 hPutStrLn stderr "link: linkables are ..."
287 hPutStrLn stderr (showSDoc (vcat (map ppr linkables)))
289 res <- link' mode dflags batch_attempt_linking linkables
291 when (verb >= 3) (hPutStrLn stderr "link: done")
296 link' Interactive dflags batch_attempt_linking linkables
297 = do showPass dflags "Not Linking...(demand linker will do the job)"
298 -- linkModules dflags linkables
302 link' Batch dflags batch_attempt_linking linkables
303 | batch_attempt_linking
305 -- check for the -no-link flag
306 omit_linking <- readIORef v_NoLink
308 then do when (verb >= 3) $
309 hPutStrLn stderr "link(batch): linking omitted (-no-link flag given)."
314 hPutStrLn stderr "Linking ..."
316 -- Don't showPass in Batch mode; doLink will do that for us.
317 staticLink (concatMap getOfiles linkables)
319 -- staticLink only returns if it succeeds
323 = do when (verb >= 3) $ do
324 hPutStrLn stderr "link(batch): upsweep (partially) failed OR"
325 hPutStrLn stderr " Main.main not exported; not linking."
328 verb = verbosity dflags
329 getOfiles (LM _ _ us) = map nameOfObject (filter isObject us)
333 -----------------------------------------------------------------------------
334 -- genPipeline: Pipeline construction
335 -----------------------------------------------------------------------------
337 -- Herein is all the magic about which phases to run in which order, whether
338 -- the intermediate files should be in TMPDIR or in the current directory,
339 -- what the suffix of the intermediate files should be, etc.
341 -- The following compilation pipeline algorithm is fairly hacky. A
342 -- better way to do this would be to express the whole compilation as a
343 -- data flow DAG, where the nodes are the intermediate files and the
344 -- edges are the compilation phases. This framework would also work
345 -- nicely if a haskell dependency generator was included in the
348 -- It would also deal much more cleanly with compilation phases that
349 -- generate multiple intermediates, (eg. hsc generates .hc, .hi, and
350 -- possibly stub files), where some of the output files need to be
351 -- processed further (eg. the stub files need to be compiled by the C
354 -- A cool thing to do would then be to execute the data flow graph
355 -- concurrently, automatically taking advantage of extra processors on
356 -- the host machine. For example, when compiling two Haskell files
357 -- where one depends on the other, the data flow graph would determine
358 -- that the C compiler from the first compilation can be overlapped
359 -- with the hsc compilation for the second file.
361 data IntermediateFileType
367 :: GhcMode -- when to stop
368 -> String -- "stop after" flag (for error messages)
369 -> Bool -- True => output is persistent
370 -> HscLang -- preferred output language for hsc
371 -> (FilePath, String) -- original filename & its suffix
372 -> IO [ -- list of phases to run for this file
374 IntermediateFileType, -- keep the output from this phase?
375 String) -- output file suffix
378 genPipeline todo stop_flag persistent_output lang (filename,suffix)
380 split <- readIORef v_Split_object_files
381 mangle <- readIORef v_Do_asm_mangling
382 keep_hc <- readIORef v_Keep_hc_files
384 keep_il <- readIORef v_Keep_il_files
385 keep_ilx <- readIORef v_Keep_ilx_files
387 keep_raw_s <- readIORef v_Keep_raw_s_files
388 keep_s <- readIORef v_Keep_s_files
389 osuf <- readIORef v_Object_suf
390 hcsuf <- readIORef v_HC_suf
393 ----------- ----- ---- --- -- -- - - -
394 start = startPhase suffix
396 -- special case for mkdependHS: .hspp files go through MkDependHS
397 start_phase | todo == DoMkDependHS && start == Hsc = MkDependHS
400 haskellish = haskellish_suffix suffix
401 cish = cish_suffix suffix
403 -- for a .hc file we need to force lang to HscC
404 real_lang | start_phase == HCc || start_phase == Mangle = HscC
408 ----------- ----- ---- --- -- -- - - -
409 pipeline = preprocess ++ compile
412 | haskellish = [ Unlit, Cpp, HsPp ]
416 | todo == DoMkDependHS = [ MkDependHS ]
422 HscC | split && mangle -> [ Hsc, HCc, Mangle, SplitMangle, SplitAs ]
423 | mangle -> [ Hsc, HCc, Mangle, As ]
425 | otherwise -> [ Hsc, HCc, As ]
427 HscAsm | split -> [ Hsc, SplitMangle, SplitAs ]
428 | otherwise -> [ Hsc, As ]
430 HscJava | split -> not_valid
431 | otherwise -> error "not implemented: compiling via Java"
433 HscILX | split -> not_valid
434 | otherwise -> [ Hsc, Ilx2Il, Ilasm ]
436 HscNothing -> [ Hsc, HCc ] -- HCc is a dummy stop phase
438 | otherwise = [ ] -- just pass this file through to the linker
440 -- ToDo: this is somewhat cryptic
441 not_valid = throwDyn (UsageError ("invalid option combination"))
443 stop_phase = case todo of
444 StopBefore As | split -> SplitAs
446 | real_lang == HscILX -> Ilasm
448 StopBefore phase -> phase
452 ----------- ----- ---- --- -- -- - - -
454 -- this shouldn't happen.
455 when (start_phase /= Ln && start_phase `notElem` pipeline)
456 (throwDyn (CmdLineError ("can't find starting phase for "
458 -- if we can't find the phase we're supposed to stop before,
459 -- something has gone wrong. This test carefully avoids the
460 -- case where we aren't supposed to do any compilation, because the file
461 -- is already in linkable form (for example).
462 -- hPutStrLn stderr (show ((start_phase `elem` pipeline,stop_phase /= Ln,stop_phase `notElem` pipeline), start_phase, stop_phase, pipeline,todo))
464 when (start_phase `elem` pipeline &&
465 (stop_phase /= Ln && stop_phase `notElem` pipeline))
468 ("flag `" ++ stop_flag
469 ++ "' is incompatible with source file `"
470 ++ filename ++ "'" ++ show pipeline ++ show stop_phase)))
472 -- .o and .hc suffixes can be overriden by command-line options:
473 myPhaseInputExt HCc | Just s <- hcsuf = s
474 myPhaseInputExt Ln = osuf
475 myPhaseInputExt other = phaseInputExt other
478 :: [Phase] -- raw pipeline
479 -> Phase -- phase to stop before
480 -> [(Phase, IntermediateFileType, String{-file extension-})]
481 annotatePipeline [] _ = []
482 annotatePipeline (Ln:_) _ = []
483 annotatePipeline (phase:next_phase:ps) stop =
484 (phase, keep_this_output, myPhaseInputExt next_phase)
485 : annotatePipeline (next_phase:ps) stop
489 = if persistent_output then Persistent else Temporary
493 Mangle | keep_raw_s -> Persistent
494 As | keep_s -> Persistent
495 HCc | keep_hc -> Persistent
497 Ilx2Il | keep_ilx -> Persistent
498 Ilasm | keep_il -> Persistent
502 -- add information about output files to the pipeline
503 -- the suffix on an output file is determined by the next phase
504 -- in the pipeline, so we add linking to the end of the pipeline
505 -- to force the output from the final phase to be a .o file.
507 annotated_pipeline = annotatePipeline (pipeline ++ [Ln]) stop_phase
509 phase_ne p (p1,_,_) = (p1 /= p)
510 ----------- ----- ---- --- -- -- - - -
513 takeWhile (phase_ne stop_phase ) $
514 dropWhile (phase_ne start_phase) $
520 :: [ (Phase, IntermediateFileType, String) ] -- phases to run
521 -> (String,String) -- input file
522 -> Bool -- doing linking afterward?
523 -> Bool -- take into account -o when generating output?
524 -> IO (String, String) -- return final filename
526 runPipeline pipeline (input_fn,suffix) do_linking use_ofile
527 = pipeLoop pipeline (input_fn,suffix) do_linking use_ofile basename suffix
528 where (basename, _) = splitFilename input_fn
530 pipeLoop [] input_fn _ _ _ _ = return input_fn
531 pipeLoop (all_phases@((phase, keep, o_suffix):phases))
532 (input_fn,real_suff) do_linking use_ofile orig_basename orig_suffix
535 output_fn <- outputFileName (null phases) keep o_suffix
537 mbCarryOn <- run_phase phase orig_basename orig_suffix
539 -- sometimes we bail out early, eg. when the compiler's recompilation
540 -- checker has determined that recompilation isn't necessary.
543 let (_,keep,final_suffix) = last all_phases
544 ofile <- outputFileName True keep final_suffix
545 return (ofile, final_suffix)
549 Check to see whether we've reached the end of the
550 pipeline, but did so with an ineffective last stage.
551 (i.e., it returned the input_fn as the output filename).
553 If we did and the output is persistent, copy the contents
554 of input_fn into the file where the pipeline's output is
557 atEnd <- finalStage (null phases)
558 when (atEnd && fn == input_fn)
559 (copy "Saving away compilation pipeline's output"
563 Notice that in order to keep the invariant that we can
564 determine a compilation pipeline's 'start phase' just
565 by looking at the input filename, the input filename
566 to the next stage/phase is associated here with the suffix
567 of the output file, *even* if it does not have that
570 Why is this important? Because we may run a compilation
571 pipeline in stages (cf. Main.main.compileFile's two stages),
572 so when generating the next stage we need to be precise
573 about what kind of file (=> suffix) is given as input.
575 [Not having to generate a pipeline in stages seems like
576 the right way to go, but I've punted on this for now --sof]
579 pipeLoop phases (fn, o_suffix) do_linking use_ofile
580 orig_basename orig_suffix
582 finalStage lastPhase = do
583 o_file <- readIORef v_Output_file
584 return (lastPhase && not do_linking && use_ofile && isJust o_file)
586 outputFileName last_phase keep suffix
587 = do o_file <- readIORef v_Output_file
588 atEnd <- finalStage last_phase
592 Nothing -> error "outputFileName"
593 else if keep == Persistent
594 then odir_ify (orig_basename ++ '.':suffix)
595 else newTempName suffix
598 -> String -- basename of original input source
599 -> String -- its extension
600 -> FilePath -- name of file which contains the input to this phase.
601 -> FilePath -- where to stick the result.
602 -> IO (Maybe FilePath)
603 -- Nothing => stop the compilation pipeline
604 -- Just fn => the result of this phase can be found in 'fn'
605 -- (this can either be 'input_fn' or 'output_fn').
606 -------------------------------------------------------------------------------
609 run_phase Unlit _basename _suff input_fn output_fn
610 = do unlit_flags <- getOpts opt_L
611 -- The -h option passes the file name for unlit to put in a #line directive
612 SysTools.runUnlit (map SysTools.Option unlit_flags ++
613 [ SysTools.Option "-h"
614 , SysTools.Option input_fn
615 , SysTools.FileOption "" input_fn
616 , SysTools.FileOption "" output_fn
618 return (Just output_fn)
620 -------------------------------------------------------------------------------
623 run_phase Cpp basename suff input_fn output_fn
624 = do src_opts <- getOptionsFromSource input_fn
625 unhandled_flags <- processArgs dynamic_flags src_opts []
626 checkProcessArgsResult unhandled_flags basename suff
628 do_cpp <- dynFlag cppFlag
630 -- no need to preprocess CPP, just pass input file along
631 -- to the next phase of the pipeline.
632 return (Just input_fn)
634 hscpp_opts <- getOpts opt_P
635 hs_src_cpp_opts <- readIORef v_Hs_source_cpp_opts
637 cmdline_include_paths <- readIORef v_Include_paths
638 pkg_include_dirs <- getPackageIncludePath
639 let include_paths = foldr (\ x xs -> "-I" : x : xs) []
640 (cmdline_include_paths ++ pkg_include_dirs)
643 (md_c_flags, _) <- machdepCCOpts
645 SysTools.runCpp ([SysTools.Option verb]
646 ++ map SysTools.Option include_paths
647 ++ map SysTools.Option hs_src_cpp_opts
648 ++ map SysTools.Option hscpp_opts
649 ++ map SysTools.Option md_c_flags
650 ++ [ SysTools.Option "-x"
651 , SysTools.Option "c"
652 , SysTools.Option input_fn
653 -- We hackily use Option instead of FileOption here, so that the file
654 -- name is not back-slashed on Windows. cpp is capable of
655 -- dealing with / in filenames, so it works fine. Furthermore
656 -- if we put in backslashes, cpp outputs #line directives
657 -- with *double* backslashes. And that in turn means that
658 -- our error messages get double backslashes in them.
659 -- In due course we should arrange that the lexer deals
660 -- with these \\ escapes properly.
661 , SysTools.Option "-o"
662 , SysTools.FileOption "" output_fn
664 return (Just output_fn)
666 -------------------------------------------------------------------------------
669 run_phase HsPp basename suff input_fn output_fn
670 = do let orig_fn = basename ++ '.':suff
671 do_pp <- dynFlag ppFlag
673 -- no need to preprocess, just pass input file along
674 -- to the next phase of the pipeline.
675 return (Just input_fn)
677 hspp_opts <- getOpts opt_F
678 hs_src_pp_opts <- readIORef v_Hs_source_pp_opts
679 SysTools.runPp ( [ SysTools.Option orig_fn
680 , SysTools.Option input_fn
681 , SysTools.FileOption "" output_fn
683 map SysTools.Option hs_src_pp_opts ++
684 map SysTools.Option hspp_opts
686 return (Just output_fn)
688 -----------------------------------------------------------------------------
691 run_phase MkDependHS basename suff input_fn output_fn
692 = do src <- readFile input_fn
693 let (import_sources, import_normals, _) = getImports src
694 let orig_fn = basename ++ '.':suff
695 deps_sources <- mapM (findDependency True orig_fn) import_sources
696 deps_normals <- mapM (findDependency False orig_fn) import_normals
697 let deps = deps_sources ++ deps_normals
699 osuf <- readIORef v_Object_suf
701 extra_suffixes <- readIORef v_Dep_suffixes
702 let suffixes = osuf : map (++ ('_':osuf)) extra_suffixes
703 ofiles = map (\suf -> basename ++ '.':suf) suffixes
705 objs <- mapM odir_ify ofiles
707 -- Handle for file that accumulates dependencies
708 hdl <- readIORef v_Dep_tmp_hdl
710 -- std dependency of the object(s) on the source file
711 hPutStrLn hdl (unwords (map escapeSpaces objs) ++ " : " ++
712 escapeSpaces (basename ++ '.':suff))
714 let genDep (dep, False {- not an hi file -}) =
715 hPutStrLn hdl (unwords (map escapeSpaces objs) ++ " : " ++
717 genDep (dep, True {- is an hi file -}) = do
718 hisuf <- readIORef v_Hi_suf
719 let dep_base = remove_suffix '.' dep
720 deps = (dep_base ++ hisuf)
721 : map (\suf -> dep_base ++ suf ++ '_':hisuf) extra_suffixes
722 -- length objs should be == length deps
723 sequence_ (zipWith (\o d -> hPutStrLn hdl (escapeSpaces o ++ " : " ++ escapeSpaces d)) objs deps)
725 sequence_ (map genDep [ d | Just d <- deps ])
726 return (Just output_fn)
728 -- add the lines to dep_makefile:
732 -- if the dependency is on something other than a .hi file:
733 -- this.o this.p_o ... : dep
735 -- if the import is {-# SOURCE #-}
736 -- this.o this.p_o ... : dep.hi-boot[-$vers]
739 -- this.o ... : dep.hi
740 -- this.p_o ... : dep.p_hi
743 -- (where .o is $osuf, and the other suffixes come from
744 -- the cmdline -s options).
747 -----------------------------------------------------------------------------
750 -- Compilation of a single module, in "legacy" mode (_not_ under
751 -- the direction of the compilation manager).
752 run_phase Hsc basename suff input_fn output_fn
755 -- we add the current directory (i.e. the directory in which
756 -- the .hs files resides) to the import path, since this is
757 -- what gcc does, and it's probably what you want.
758 let current_dir = directoryOf basename
760 paths <- readIORef v_Include_paths
761 writeIORef v_Include_paths (current_dir : paths)
763 -- figure out which header files to #include in a generated .hc file
764 c_includes <- getPackageCIncludes
765 cmdline_includes <- dynFlag cmdlineHcIncludes -- -#include options
767 let cc_injects = unlines (map mk_include
768 (c_includes ++ reverse cmdline_includes))
771 '"':_{-"-} -> "#include "++h_file
772 '<':_ -> "#include "++h_file
773 _ -> "#include \""++h_file++"\""
775 writeIORef v_HCHeader cc_injects
777 -- gather the imports and module name
778 (srcimps,imps,mod_name) <-
779 if extcoreish_suffix suff
781 -- no explicit imports in ExtCore input.
782 m <- getCoreModuleName input_fn
783 return ([], [], mkModuleName m)
785 getImportsFromFile input_fn
787 -- build a ModLocation to pass to hscMain.
788 let (path,file) = splitFilenameDir basename
789 (mod, location') <- mkHomeModLocation mod_name True path file suff
791 -- take -ohi into account if present
792 ohi <- readIORef v_Output_hi
793 let location | Just fn <- ohi = location'{ ml_hi_file = fn }
794 | otherwise = location'
796 -- figure out if the source has changed, for recompilation avoidance.
797 -- only do this if we're eventually going to generate a .o file.
798 -- (ToDo: do when generating .hc files too?)
800 -- Setting source_unchanged to True means that M.o seems
801 -- to be up to date wrt M.hs; so no need to recompile unless imports have
802 -- changed (which the compiler itself figures out).
803 -- Setting source_unchanged to False tells the compiler that M.o is out of
804 -- date wrt M.hs (or M.o doesn't exist) so we must recompile regardless.
805 do_recomp <- readIORef v_Recomp
806 todo <- readIORef v_GhcMode
807 expl_o_file <- readIORef v_Output_file
809 let o_file -- if the -o option is given and IT IS THE OBJECT FILE FOR
810 -- THIS COMPILATION, then use that to determine if the
811 -- source is unchanged.
812 | Just x <- expl_o_file, todo == StopBefore Ln = x
813 | otherwise = expectJust "source_unchanged" (ml_obj_file location)
816 if not (do_recomp && ( todo == DoLink || todo == StopBefore Ln ))
818 else do t1 <- getModificationTime (basename ++ '.':suff)
819 o_file_exists <- doesFileExist o_file
821 then return False -- Need to recompile
822 else do t2 <- getModificationTime o_file
828 dyn_flags <- getDynFlags
830 let dyn_flags' = dyn_flags { hscOutName = output_fn,
831 hscStubCOutName = basename ++ "_stub.c",
832 hscStubHOutName = basename ++ "_stub.h",
833 extCoreName = basename ++ ".hcr" }
834 hsc_env = HscEnv { hsc_mode = OneShot,
835 hsc_dflags = dyn_flags',
836 hsc_HPT = emptyHomePackageTable }
840 pcs <- initPersistentCompilerState
841 result <- hscMain hsc_env pcs mod
842 location{ ml_hspp_file=Just input_fn }
849 HscFail pcs -> throwDyn (PhaseFailed "hsc" (ExitFailure 1));
851 HscNoRecomp pcs details iface -> do { SysTools.touch "Touching object file" o_file
854 HscRecomp pcs details iface stub_h_exists stub_c_exists
855 _maybe_interpreted_code -> do
858 maybe_stub_o <- compileStub dyn_flags' stub_c_exists
861 Just stub_o -> add v_Ld_inputs stub_o
862 case hscLang dyn_flags of
863 HscNothing -> return Nothing
864 _ -> return (Just output_fn)
867 -----------------------------------------------------------------------------
870 -- we don't support preprocessing .c files (with -E) now. Doing so introduces
871 -- way too many hacks, and I can't say I've ever used it anyway.
873 run_phase cc_phase basename suff input_fn output_fn
874 | cc_phase == Cc || cc_phase == HCc
875 = do cc_opts <- getOpts opt_c
876 cmdline_include_paths <- readIORef v_Include_paths
878 let hcc = cc_phase == HCc
880 -- add package include paths even if we're just compiling
881 -- .c files; this is the Value Add(TM) that using
882 -- ghc instead of gcc gives you :)
883 pkg_include_dirs <- getPackageIncludePath
884 let include_paths = foldr (\ x xs -> "-I" : x : xs) []
885 (cmdline_include_paths ++ pkg_include_dirs)
887 mangle <- readIORef v_Do_asm_mangling
888 (md_c_flags, md_regd_c_flags) <- machdepCCOpts
892 o2 <- readIORef v_minus_o2_for_C
893 let opt_flag | o2 = "-O2"
896 pkg_extra_cc_opts <- getPackageExtraCcOpts
898 split_objs <- readIORef v_Split_object_files
899 let split_opt | hcc && split_objs = [ "-DUSE_SPLIT_MARKERS" ]
902 excessPrecision <- readIORef v_Excess_precision
904 -- force the C compiler to interpret this file as C when
905 -- compiling .hc files, by adding the -x c option.
907 | cc_phase == HCc = [ SysTools.Option "-x", SysTools.Option "c"]
910 SysTools.runCc (langopt ++
911 [ SysTools.FileOption "" input_fn
912 , SysTools.Option "-o"
913 , SysTools.FileOption "" output_fn
915 ++ map SysTools.Option (
917 ++ (if cc_phase == HCc && mangle
920 ++ [ verb, "-S", "-Wimplicit", opt_flag ]
921 ++ [ "-D__GLASGOW_HASKELL__="++cProjectVersionInt ]
924 ++ (if excessPrecision then [] else [ "-ffloat-store" ])
928 return (Just output_fn)
930 -- ToDo: postprocess the output from gcc
932 -----------------------------------------------------------------------------
935 run_phase Mangle _basename _suff input_fn output_fn
936 = do mangler_opts <- getOpts opt_m
937 machdep_opts <- if (prefixMatch "i386" cTARGETPLATFORM)
938 then do n_regs <- dynFlag stolen_x86_regs
939 return [ show n_regs ]
942 SysTools.runMangle (map SysTools.Option mangler_opts
943 ++ [ SysTools.FileOption "" input_fn
944 , SysTools.FileOption "" output_fn
946 ++ map SysTools.Option machdep_opts)
947 return (Just output_fn)
949 -----------------------------------------------------------------------------
952 run_phase SplitMangle _basename _suff input_fn output_fn
953 = do -- tmp_pfx is the prefix used for the split .s files
954 -- We also use it as the file to contain the no. of split .s files (sigh)
955 split_s_prefix <- SysTools.newTempName "split"
956 let n_files_fn = split_s_prefix
958 SysTools.runSplit [ SysTools.FileOption "" input_fn
959 , SysTools.FileOption "" split_s_prefix
960 , SysTools.FileOption "" n_files_fn
963 -- Save the number of split files for future references
964 s <- readFile n_files_fn
965 let n_files = read s :: Int
966 writeIORef v_Split_info (split_s_prefix, n_files)
968 -- Remember to delete all these files
969 addFilesToClean [ split_s_prefix ++ "__" ++ show n ++ ".s"
972 return (Just output_fn)
974 -----------------------------------------------------------------------------
977 run_phase As _basename _suff input_fn output_fn
978 = do as_opts <- getOpts opt_a
979 cmdline_include_paths <- readIORef v_Include_paths
981 SysTools.runAs (map SysTools.Option as_opts
982 ++ [ SysTools.Option ("-I" ++ p) | p <- cmdline_include_paths ]
983 ++ [ SysTools.Option "-c"
984 , SysTools.FileOption "" input_fn
985 , SysTools.Option "-o"
986 , SysTools.FileOption "" output_fn
988 return (Just output_fn)
990 run_phase SplitAs basename _suff _input_fn output_fn
991 = do as_opts <- getOpts opt_a
993 (split_s_prefix, n) <- readIORef v_Split_info
995 odir <- readIORef v_Output_dir
996 let real_odir = case odir of
997 Nothing -> basename ++ "_split"
1001 = do let input_s = split_s_prefix ++ "__" ++ show n ++ ".s"
1002 let output_o = replaceFilenameDirectory
1003 (basename ++ "__" ++ show n ++ ".o")
1005 real_o <- osuf_ify output_o
1006 SysTools.runAs (map SysTools.Option as_opts ++
1007 [ SysTools.Option "-c"
1008 , SysTools.Option "-o"
1009 , SysTools.FileOption "" real_o
1010 , SysTools.FileOption "" input_s
1013 mapM_ assemble_file [1..n]
1014 return (Just output_fn)
1017 -----------------------------------------------------------------------------
1019 -- Run ilx2il over the ILX output, getting an IL file
1021 run_phase Ilx2Il _basename _suff input_fn output_fn
1022 = do ilx2il_opts <- getOpts opt_I
1023 SysTools.runIlx2il (map SysTools.Option ilx2il_opts
1024 ++ [ SysTools.Option "--no-add-suffix-to-assembly",
1025 SysTools.Option "mscorlib",
1026 SysTools.Option "-o",
1027 SysTools.FileOption "" output_fn,
1028 SysTools.FileOption "" input_fn ])
1029 return (Just output_fn)
1031 -----------------------------------------------------------------------------
1033 -- Run ilasm over the IL, getting a DLL
1035 run_phase Ilasm _basename _suff input_fn output_fn
1036 = do ilasm_opts <- getOpts opt_i
1037 SysTools.runIlasm (map SysTools.Option ilasm_opts
1038 ++ [ SysTools.Option "/QUIET",
1039 SysTools.Option "/DLL",
1040 SysTools.FileOption "/OUT=" output_fn,
1041 SysTools.FileOption "" input_fn ])
1042 return (Just output_fn)
1046 -----------------------------------------------------------------------------
1047 -- MoveBinary sort-of-phase
1048 -- After having produced a binary, move it somewhere else and generate a
1049 -- wrapper script calling the binary. Currently, we need this only in
1050 -- a parallel way (i.e. in GUM), because PVM expects the binary in a
1051 -- central directory.
1052 -- This is called from staticLink below, after linking. I haven't made it
1053 -- a separate phase to minimise interfering with other modules, and
1054 -- we don't need the generality of a phase (MoveBinary is always
1055 -- done after linking and makes only sense in a parallel setup) -- HWL
1057 run_phase_MoveBinary input_fn
1060 pvm_root <- getEnv "PVM_ROOT"
1061 pvm_arch <- getEnv "PVM_ARCH"
1063 pvm_executable_base = "=" ++ input_fn
1064 pvm_executable = pvm_root ++ "/bin/" ++ pvm_arch ++ "/" ++ pvm_executable_base
1065 -- nuke old binary; maybe use configur'ed names for cp and rm?
1066 system ("rm -f " ++ pvm_executable)
1067 -- move the newly created binary into PVM land
1068 system ("cp -p " ++ input_fn ++ " " ++ pvm_executable)
1069 -- generate a wrapper script for running a parallel prg under PVM
1070 writeFile input_fn (mk_pvm_wrapper_script pvm_executable pvm_executable_base sysMan)
1073 -- generates a Perl skript starting a parallel prg under PVM
1074 mk_pvm_wrapper_script :: String -> String -> String -> String
1075 mk_pvm_wrapper_script pvm_executable pvm_executable_base sysMan = unlines $
1077 "eval 'exec perl -S $0 ${1+\"$@\"}'",
1078 " if $running_under_some_shell;",
1079 "# =!=!=!=!=!=!=!=!=!=!=!",
1080 "# This script is automatically generated: DO NOT EDIT!!!",
1081 "# Generated by Glasgow Haskell Compiler",
1082 "# ngoqvam choHbogh vaj' vIHoHnISbej !!!!",
1084 "$pvm_executable = '" ++ pvm_executable ++ "';",
1085 "$pvm_executable_base = '" ++ pvm_executable_base ++ "';",
1086 "$SysMan = '" ++ sysMan ++ "';",
1088 {- ToDo: add the magical shortcuts again iff we actually use them -- HWL
1089 "# first, some magical shortcuts to run "commands" on the binary",
1090 "# (which is hidden)",
1091 "if ($#ARGV == 1 && $ARGV[0] eq '+RTS' && $ARGV[1] =~ /^--((size|file|strip|rm|nm).*)/ ) {",
1092 " local($cmd) = $1;",
1093 " system("$cmd $pvm_executable");",
1094 " exit(0); # all done",
1097 "# Now, run the real binary; process the args first",
1098 "$ENV{'PE'} = $pvm_executable_base;", -- ++ pvm_executable_base,
1100 "$nprocessors = 0; # the default: as many PEs as machines in PVM config",
1101 "@nonPVM_args = ();",
1102 "$in_RTS_args = 0;",
1104 "args: while ($a = shift(@ARGV)) {",
1105 " if ( $a eq '+RTS' ) {",
1106 " $in_RTS_args = 1;",
1107 " } elsif ( $a eq '-RTS' ) {",
1108 " $in_RTS_args = 0;",
1110 " if ( $a eq '-d' && $in_RTS_args ) {",
1112 " } elsif ( $a =~ /^-qN(\\d+)/ && $in_RTS_args ) {",
1113 " $nprocessors = $1;",
1114 " } elsif ( $a =~ /^-qp(\\d+)/ && $in_RTS_args ) {",
1115 " $nprocessors = $1;",
1117 " push(@nonPVM_args, $a);",
1121 "local($return_val) = 0;",
1122 "# Start the parallel execution by calling SysMan",
1123 "system(\"$SysMan $debug $pvm_executable $nprocessors @nonPVM_args\");",
1124 "$return_val = $?;",
1125 "# ToDo: fix race condition moving files and flushing them!!",
1126 "system(\"cp $ENV{'HOME'}/$pvm_executable_base.???.gr .\") if -f \"$ENV{'HOME'}/$pvm_executable_base.002.gr\";",
1127 "exit($return_val);"
1130 -----------------------------------------------------------------------------
1131 -- Complain about non-dynamic flags in OPTIONS pragmas
1133 checkProcessArgsResult flags basename suff
1134 = do when (notNull flags) (throwDyn (ProgramError (
1135 basename ++ "." ++ suff
1136 ++ ": static flags are not allowed in {-# OPTIONS #-} pragmas:\n\t"
1137 ++ unwords flags)) (ExitFailure 1))
1139 -----------------------------------------------------------------------------
1140 -- Static linking, of .o files
1142 staticLink :: [String] -> IO ()
1143 staticLink o_files = do
1145 static <- readIORef v_Static
1146 no_hs_main <- readIORef v_NoHsMain
1148 o_file <- readIORef v_Output_file
1149 let output_fn = case o_file of { Just s -> s; Nothing -> "a.out"; }
1151 pkg_lib_paths <- getPackageLibraryPath
1152 let pkg_lib_path_opts = map ("-L"++) pkg_lib_paths
1154 lib_paths <- readIORef v_Library_paths
1155 let lib_path_opts = map ("-L"++) lib_paths
1157 pkg_link_opts <- getPackageLinkOpts
1159 #ifdef darwin_TARGET_OS
1160 pkg_framework_paths <- getPackageFrameworkPath
1161 let pkg_framework_path_opts = map ("-F"++) pkg_framework_paths
1163 framework_paths <- readIORef v_Framework_paths
1164 let framework_path_opts = map ("-F"++) framework_paths
1166 pkg_frameworks <- getPackageFrameworks
1167 let pkg_framework_opts = map ("-framework " ++) pkg_frameworks
1169 frameworks <- readIORef v_Cmdline_frameworks
1170 let framework_opts = map ("-framework "++) (reverse frameworks)
1171 -- reverse because they're added in reverse order from the cmd line
1174 -- probably _stub.o files
1175 extra_ld_inputs <- readIORef v_Ld_inputs
1177 -- opts from -optl-<blah> (including -l<blah> options)
1178 extra_ld_opts <- getStaticOpts v_Opt_l
1180 [rts_pkg, std_pkg] <- getPackageDetails [rtsPackage, basePackage]
1182 let extra_os = if static || no_hs_main
1184 else [ head (library_dirs rts_pkg) ++ "/Main.dll_o",
1185 head (library_dirs std_pkg) ++ "/PrelMain.dll_o" ]
1187 (md_c_flags, _) <- machdepCCOpts
1188 SysTools.runLink ( [ SysTools.Option verb
1189 , SysTools.Option "-o"
1190 , SysTools.FileOption "" output_fn
1192 ++ map SysTools.Option (
1199 #ifdef darwin_TARGET_OS
1200 ++ framework_path_opts
1203 ++ pkg_lib_path_opts
1205 #ifdef darwin_TARGET_OS
1206 ++ pkg_framework_path_opts
1207 ++ pkg_framework_opts
1209 ++ if static && not no_hs_main then
1210 [ "-u", prefixUnderscore "Main_zdmain_closure"]
1213 -- parallel only: move binary to another dir -- HWL
1214 ways_ <- readIORef v_Ways
1215 when (WayPar `elem` ways_)
1216 (do success <- run_phase_MoveBinary output_fn
1217 if success then return ()
1218 else throwDyn (InstallationError ("cannot move binary to PVM dir")))
1220 -----------------------------------------------------------------------------
1221 -- Making a DLL (only for Win32)
1223 doMkDLL :: [String] -> IO ()
1224 doMkDLL o_files = do
1226 static <- readIORef v_Static
1227 no_hs_main <- readIORef v_NoHsMain
1229 o_file <- readIORef v_Output_file
1230 let output_fn = case o_file of { Just s -> s; Nothing -> "HSdll.dll"; }
1232 pkg_lib_paths <- getPackageLibraryPath
1233 let pkg_lib_path_opts = map ("-L"++) pkg_lib_paths
1235 lib_paths <- readIORef v_Library_paths
1236 let lib_path_opts = map ("-L"++) lib_paths
1238 pkg_link_opts <- getPackageLinkOpts
1240 -- probably _stub.o files
1241 extra_ld_inputs <- readIORef v_Ld_inputs
1243 -- opts from -optdll-<blah>
1244 extra_ld_opts <- getStaticOpts v_Opt_dll
1246 [rts_pkg, std_pkg] <- getPackageDetails [rtsPackage, basePackage]
1248 let extra_os = if static || no_hs_main
1250 else [ head (library_dirs rts_pkg) ++ "/Main.dll_o",
1251 head (library_dirs std_pkg) ++ "/PrelMain.dll_o" ]
1253 (md_c_flags, _) <- machdepCCOpts
1255 ([ SysTools.Option verb
1256 , SysTools.Option "-o"
1257 , SysTools.FileOption "" output_fn
1259 ++ map SysTools.Option (
1263 ++ [ "--target=i386-mingw32" ]
1267 ++ pkg_lib_path_opts
1269 ++ (if "--def" `elem` (concatMap words extra_ld_opts)
1271 else [ "--export-all" ])