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,
16 -- interfaces for the compilation manager (interpreted/batch-mode)
17 preprocess, compile, CompResult(..),
19 -- batch-mode linking interface
25 #include "HsVersions.h"
35 import SysTools ( newTempName, addFilesToClean, getSysMan, copy )
36 import qualified SysTools
47 import Maybes ( expectJust )
49 import ParserCoreUtils ( getCoreModuleName )
52 import Time ( getClockTime )
65 -----------------------------------------------------------------------------
68 -- Herein is all the magic about which phases to run in which order, whether
69 -- the intermediate files should be in TMPDIR or in the current directory,
70 -- what the suffix of the intermediate files should be, etc.
72 -- The following compilation pipeline algorithm is fairly hacky. A
73 -- better way to do this would be to express the whole compilation as a
74 -- data flow DAG, where the nodes are the intermediate files and the
75 -- edges are the compilation phases. This framework would also work
76 -- nicely if a haskell dependency generator was included in the
79 -- It would also deal much more cleanly with compilation phases that
80 -- generate multiple intermediates, (eg. hsc generates .hc, .hi, and
81 -- possibly stub files), where some of the output files need to be
82 -- processed further (eg. the stub files need to be compiled by the C
85 -- A cool thing to do would then be to execute the data flow graph
86 -- concurrently, automatically taking advantage of extra processors on
87 -- the host machine. For example, when compiling two Haskell files
88 -- where one depends on the other, the data flow graph would determine
89 -- that the C compiler from the first compilation can be overlapped
90 -- with the hsc compilation for the second file.
92 data IntermediateFileType
98 :: GhcMode -- when to stop
99 -> String -- "stop after" flag (for error messages)
100 -> Bool -- True => output is persistent
101 -> HscLang -- preferred output language for hsc
102 -> (FilePath, String) -- original filename & its suffix
103 -> IO [ -- list of phases to run for this file
105 IntermediateFileType, -- keep the output from this phase?
106 String) -- output file suffix
109 genPipeline todo stop_flag persistent_output lang (filename,suffix)
111 split <- readIORef v_Split_object_files
112 mangle <- readIORef v_Do_asm_mangling
113 keep_hc <- readIORef v_Keep_hc_files
115 keep_il <- readIORef v_Keep_il_files
116 keep_ilx <- readIORef v_Keep_ilx_files
118 keep_raw_s <- readIORef v_Keep_raw_s_files
119 keep_s <- readIORef v_Keep_s_files
120 osuf <- readIORef v_Object_suf
121 hcsuf <- readIORef v_HC_suf
124 ----------- ----- ---- --- -- -- - - -
125 start = startPhase suffix
127 -- special case for mkdependHS: .hspp files go through MkDependHS
128 start_phase | todo == DoMkDependHS && start == Hsc = MkDependHS
131 haskellish = haskellish_suffix suffix
132 cish = cish_suffix suffix
134 -- for a .hc file we need to force lang to HscC
135 real_lang | start_phase == HCc || start_phase == Mangle = HscC
139 ----------- ----- ---- --- -- -- - - -
140 pipeline = preprocess ++ compile
143 | haskellish = [ Unlit, Cpp, HsPp ]
147 | todo == DoMkDependHS = [ MkDependHS ]
153 HscC | split && mangle -> [ Hsc, HCc, Mangle, SplitMangle, SplitAs ]
154 | mangle -> [ Hsc, HCc, Mangle, As ]
156 | otherwise -> [ Hsc, HCc, As ]
158 HscAsm | split -> [ Hsc, SplitMangle, SplitAs ]
159 | otherwise -> [ Hsc, As ]
161 HscJava | split -> not_valid
162 | otherwise -> error "not implemented: compiling via Java"
164 HscILX | split -> not_valid
165 | otherwise -> [ Hsc, Ilx2Il, Ilasm ]
167 HscNothing -> [ Hsc, HCc ] -- HCc is a dummy stop phase
169 | otherwise = [ ] -- just pass this file through to the linker
171 -- ToDo: this is somewhat cryptic
172 not_valid = throwDyn (UsageError ("invalid option combination"))
174 stop_phase = case todo of
175 StopBefore As | split -> SplitAs
177 | real_lang == HscILX -> Ilasm
179 StopBefore phase -> phase
183 ----------- ----- ---- --- -- -- - - -
185 -- this shouldn't happen.
186 when (start_phase /= Ln && start_phase `notElem` pipeline)
187 (throwDyn (CmdLineError ("can't find starting phase for "
189 -- if we can't find the phase we're supposed to stop before,
190 -- something has gone wrong. This test carefully avoids the
191 -- case where we aren't supposed to do any compilation, because the file
192 -- is already in linkable form (for example).
193 -- hPutStrLn stderr (show ((start_phase `elem` pipeline,stop_phase /= Ln,stop_phase `notElem` pipeline), start_phase, stop_phase, pipeline,todo))
195 when (start_phase `elem` pipeline &&
196 (stop_phase /= Ln && stop_phase `notElem` pipeline))
199 ("flag `" ++ stop_flag
200 ++ "' is incompatible with source file `"
201 ++ filename ++ "'" ++ show pipeline ++ show stop_phase)))
203 -- .o and .hc suffixes can be overriden by command-line options:
204 myPhaseInputExt Ln | Just s <- osuf = s
205 myPhaseInputExt HCc | Just s <- hcsuf = s
206 myPhaseInputExt other = phaseInputExt other
209 :: [Phase] -- raw pipeline
210 -> Phase -- phase to stop before
211 -> [(Phase, IntermediateFileType, String{-file extension-})]
212 annotatePipeline [] _ = []
213 annotatePipeline (Ln:_) _ = []
214 annotatePipeline (phase:next_phase:ps) stop =
215 (phase, keep_this_output, myPhaseInputExt next_phase)
216 : annotatePipeline (next_phase:ps) stop
220 = if persistent_output then Persistent else Temporary
224 Mangle | keep_raw_s -> Persistent
225 As | keep_s -> Persistent
226 HCc | keep_hc -> Persistent
228 Ilx2Il | keep_ilx -> Persistent
229 Ilasm | keep_il -> Persistent
233 -- add information about output files to the pipeline
234 -- the suffix on an output file is determined by the next phase
235 -- in the pipeline, so we add linking to the end of the pipeline
236 -- to force the output from the final phase to be a .o file.
238 annotated_pipeline = annotatePipeline (pipeline ++ [Ln]) stop_phase
240 phase_ne p (p1,_,_) = (p1 /= p)
241 ----------- ----- ---- --- -- -- - - -
244 takeWhile (phase_ne stop_phase ) $
245 dropWhile (phase_ne start_phase) $
251 :: [ (Phase, IntermediateFileType, String) ] -- phases to run
252 -> (String,String) -- input file
253 -> Bool -- doing linking afterward?
254 -> Bool -- take into account -o when generating output?
255 -> IO (String, String) -- return final filename
257 runPipeline pipeline (input_fn,suffix) do_linking use_ofile
258 = pipeLoop pipeline (input_fn,suffix) do_linking use_ofile basename suffix
259 where (basename, _) = splitFilename input_fn
261 pipeLoop [] input_fn _ _ _ _ = return input_fn
262 pipeLoop (all_phases@((phase, keep, o_suffix):phases))
263 (input_fn,real_suff) do_linking use_ofile orig_basename orig_suffix
266 output_fn <- outputFileName (null phases) keep o_suffix
268 mbCarryOn <- run_phase phase orig_basename orig_suffix
270 -- sometimes we bail out early, eg. when the compiler's recompilation
271 -- checker has determined that recompilation isn't necessary.
274 let (_,keep,final_suffix) = last all_phases
275 ofile <- outputFileName True keep final_suffix
276 return (ofile, final_suffix)
280 Check to see whether we've reached the end of the
281 pipeline, but did so with an ineffective last stage.
282 (i.e., it returned the input_fn as the output filename).
284 If we did and the output is persistent, copy the contents
285 of input_fn into the file where the pipeline's output is
288 atEnd <- finalStage (null phases)
289 when (atEnd && fn == input_fn)
290 (copy "Saving away compilation pipeline's output"
294 Notice that in order to keep the invariant that we can
295 determine a compilation pipeline's 'start phase' just
296 by looking at the input filename, the input filename
297 to the next stage/phase is associated here with the suffix
298 of the output file, *even* if it does not have that
301 Why is this important? Because we may run a compilation
302 pipeline in stages (cf. Main.main.compileFile's two stages),
303 so when generating the next stage we need to be precise
304 about what kind of file (=> suffix) is given as input.
306 [Not having to generate a pipeline in stages seems like
307 the right way to go, but I've punted on this for now --sof]
310 pipeLoop phases (fn, o_suffix) do_linking use_ofile
311 orig_basename orig_suffix
313 finalStage lastPhase = do
314 o_file <- readIORef v_Output_file
315 return (lastPhase && not do_linking && use_ofile && isJust o_file)
317 outputFileName last_phase keep suffix
318 = do o_file <- readIORef v_Output_file
319 atEnd <- finalStage last_phase
323 Nothing -> error "outputFileName"
324 else if keep == Persistent
325 then odir_ify (orig_basename ++ '.':suffix)
326 else newTempName suffix
329 -> String -- basename of original input source
330 -> String -- its extension
331 -> FilePath -- name of file which contains the input to this phase.
332 -> FilePath -- where to stick the result.
333 -> IO (Maybe FilePath)
334 -- Nothing => stop the compilation pipeline
335 -- Just fn => the result of this phase can be found in 'fn'
336 -- (this can either be 'input_fn' or 'output_fn').
337 -------------------------------------------------------------------------------
340 run_phase Unlit _basename _suff input_fn output_fn
341 = do unlit_flags <- getOpts opt_L
342 -- The -h option passes the file name for unlit to put in a #line directive
343 SysTools.runUnlit (map SysTools.Option unlit_flags ++
344 [ SysTools.Option "-h"
345 , SysTools.Option input_fn
346 , SysTools.FileOption "" input_fn
347 , SysTools.FileOption "" output_fn
349 return (Just output_fn)
351 -------------------------------------------------------------------------------
354 run_phase Cpp basename suff input_fn output_fn
355 = do src_opts <- getOptionsFromSource input_fn
356 unhandled_flags <- processArgs dynamic_flags src_opts []
357 checkProcessArgsResult unhandled_flags basename suff
359 do_cpp <- dynFlag cppFlag
361 -- no need to preprocess CPP, just pass input file along
362 -- to the next phase of the pipeline.
363 return (Just input_fn)
365 hscpp_opts <- getOpts opt_P
366 hs_src_cpp_opts <- readIORef v_Hs_source_cpp_opts
368 cmdline_include_paths <- readIORef v_Include_paths
369 pkg_include_dirs <- getPackageIncludePath
370 let include_paths = foldr (\ x xs -> "-I" : x : xs) []
371 (cmdline_include_paths ++ pkg_include_dirs)
374 (md_c_flags, _) <- machdepCCOpts
376 SysTools.runCpp ([SysTools.Option verb]
377 ++ map SysTools.Option include_paths
378 ++ map SysTools.Option hs_src_cpp_opts
379 ++ map SysTools.Option hscpp_opts
380 ++ map SysTools.Option md_c_flags
381 ++ [ SysTools.Option "-x"
382 , SysTools.Option "c"
383 , SysTools.Option input_fn
384 -- We hackily use Option instead of FileOption here, so that the file
385 -- name is not back-slashed on Windows. cpp is capable of
386 -- dealing with / in filenames, so it works fine. Furthermore
387 -- if we put in backslashes, cpp outputs #line directives
388 -- with *double* backslashes. And that in turn means that
389 -- our error messages get double backslashes in them.
390 -- In due course we should arrange that the lexer deals
391 -- with these \\ escapes properly.
392 , SysTools.Option "-o"
393 , SysTools.FileOption "" output_fn
395 return (Just output_fn)
397 -------------------------------------------------------------------------------
400 run_phase HsPp basename suff input_fn output_fn
401 = do let orig_fn = basename ++ '.':suff
402 do_pp <- dynFlag ppFlag
404 -- no need to preprocess, just pass input file along
405 -- to the next phase of the pipeline.
406 return (Just input_fn)
408 hspp_opts <- getOpts opt_F
409 hs_src_pp_opts <- readIORef v_Hs_source_pp_opts
410 SysTools.runPp ( [ SysTools.Option orig_fn
411 , SysTools.Option input_fn
412 , SysTools.FileOption "" output_fn
414 map SysTools.Option hs_src_pp_opts ++
415 map SysTools.Option hspp_opts
417 return (Just output_fn)
419 -----------------------------------------------------------------------------
422 run_phase MkDependHS basename suff input_fn output_fn
423 = do src <- readFile input_fn
424 let (import_sources, import_normals, _) = getImports src
425 let orig_fn = basename ++ '.':suff
426 deps_sources <- mapM (findDependency True orig_fn) import_sources
427 deps_normals <- mapM (findDependency False orig_fn) import_normals
428 let deps = deps_sources ++ deps_normals
430 osuf_opt <- readIORef v_Object_suf
431 let osuf = case osuf_opt of
432 Nothing -> phaseInputExt Ln
435 extra_suffixes <- readIORef v_Dep_suffixes
436 let suffixes = osuf : map (++ ('_':osuf)) extra_suffixes
437 ofiles = map (\suf -> basename ++ '.':suf) suffixes
439 objs <- mapM odir_ify ofiles
441 -- Handle for file that accumulates dependencies
442 hdl <- readIORef v_Dep_tmp_hdl
444 -- std dependency of the object(s) on the source file
445 hPutStrLn hdl (unwords (map escapeSpaces objs) ++ " : " ++
446 escapeSpaces (basename ++ '.':suff))
448 let genDep (dep, False {- not an hi file -}) =
449 hPutStrLn hdl (unwords (map escapeSpaces objs) ++ " : " ++
451 genDep (dep, True {- is an hi file -}) = do
452 hisuf <- readIORef v_Hi_suf
453 let dep_base = remove_suffix '.' dep
454 deps = (dep_base ++ hisuf)
455 : map (\suf -> dep_base ++ suf ++ '_':hisuf) extra_suffixes
456 -- length objs should be == length deps
457 sequence_ (zipWith (\o d -> hPutStrLn hdl (escapeSpaces o ++ " : " ++ escapeSpaces d)) objs deps)
459 sequence_ (map genDep [ d | Just d <- deps ])
460 return (Just output_fn)
462 -- add the lines to dep_makefile:
466 -- if the dependency is on something other than a .hi file:
467 -- this.o this.p_o ... : dep
469 -- if the import is {-# SOURCE #-}
470 -- this.o this.p_o ... : dep.hi-boot[-$vers]
473 -- this.o ... : dep.hi
474 -- this.p_o ... : dep.p_hi
477 -- (where .o is $osuf, and the other suffixes come from
478 -- the cmdline -s options).
481 -----------------------------------------------------------------------------
484 -- Compilation of a single module, in "legacy" mode (_not_ under
485 -- the direction of the compilation manager).
486 run_phase Hsc basename suff input_fn output_fn
489 -- we add the current directory (i.e. the directory in which
490 -- the .hs files resides) to the import path, since this is
491 -- what gcc does, and it's probably what you want.
492 let current_dir = getdir basename
494 paths <- readIORef v_Include_paths
495 writeIORef v_Include_paths (current_dir : paths)
497 -- figure out which header files to #include in a generated .hc file
498 c_includes <- getPackageCIncludes
499 cmdline_includes <- dynFlag cmdlineHcIncludes -- -#include options
501 let cc_injects = unlines (map mk_include
502 (c_includes ++ reverse cmdline_includes))
505 '"':_{-"-} -> "#include "++h_file
506 '<':_ -> "#include "++h_file
507 _ -> "#include \""++h_file++"\""
509 writeIORef v_HCHeader cc_injects
511 -- gather the imports and module name
512 (srcimps,imps,mod_name) <-
513 if extcoreish_suffix suff
515 -- no explicit imports in ExtCore input.
516 m <- getCoreModuleName input_fn
517 return ([], [], mkModuleName m)
519 getImportsFromFile input_fn
521 -- build a ModuleLocation to pass to hscMain.
523 <- mkHomeModuleLocn mod_name basename (basename ++ '.':suff)
525 -- take -ohi into account if present
526 ohi <- readIORef v_Output_hi
527 let location | Just fn <- ohi = location'{ ml_hi_file = fn }
528 | otherwise = location'
530 -- figure out if the source has changed, for recompilation avoidance.
531 -- only do this if we're eventually going to generate a .o file.
532 -- (ToDo: do when generating .hc files too?)
534 -- Setting source_unchanged to True means that M.o seems
535 -- to be up to date wrt M.hs; so no need to recompile unless imports have
536 -- changed (which the compiler itself figures out).
537 -- Setting source_unchanged to False tells the compiler that M.o is out of
538 -- date wrt M.hs (or M.o doesn't exist) so we must recompile regardless.
539 do_recomp <- readIORef v_Recomp
540 todo <- readIORef v_GhcMode
541 expl_o_file <- readIORef v_Output_file
543 let o_file -- if the -o option is given and IT IS THE OBJECT FILE FOR
544 -- THIS COMPILATION, then use that to determine if the
545 -- source is unchanged.
546 | Just x <- expl_o_file, todo == StopBefore Ln = x
547 | otherwise = expectJust "source_unchanged" (ml_obj_file location)
550 if not (do_recomp && ( todo == DoLink || todo == StopBefore Ln ))
552 else do t1 <- getModificationTime (basename ++ '.':suff)
553 o_file_exists <- doesFileExist o_file
555 then return False -- Need to recompile
556 else do t2 <- getModificationTime o_file
562 dyn_flags <- getDynFlags
564 let dyn_flags' = dyn_flags { hscOutName = output_fn,
565 hscStubCOutName = basename ++ "_stub.c",
566 hscStubHOutName = basename ++ "_stub.h",
567 extCoreName = basename ++ ".hcr" }
570 pcs <- initPersistentCompilerState
571 result <- hscMain OneShot
573 location{ ml_hspp_file=Just input_fn }
577 emptyModuleEnv -- HomeSymbolTable
578 emptyModuleEnv -- HomeIfaceTable
583 HscFail pcs -> throwDyn (PhaseFailed "hsc" (ExitFailure 1));
585 HscNoRecomp pcs details iface -> do { SysTools.touch "Touching object file" o_file
588 HscRecomp pcs details iface stub_h_exists stub_c_exists
589 _maybe_interpreted_code -> do
592 maybe_stub_o <- compileStub dyn_flags' stub_c_exists
595 Just stub_o -> add v_Ld_inputs stub_o
596 case hscLang dyn_flags of
597 HscNothing -> return Nothing
598 _ -> return (Just output_fn)
601 -----------------------------------------------------------------------------
604 -- we don't support preprocessing .c files (with -E) now. Doing so introduces
605 -- way too many hacks, and I can't say I've ever used it anyway.
607 run_phase cc_phase basename suff input_fn output_fn
608 | cc_phase == Cc || cc_phase == HCc
609 = do cc_opts <- getOpts opt_c
610 cmdline_include_paths <- readIORef v_Include_paths
612 let hcc = cc_phase == HCc
614 -- add package include paths even if we're just compiling
615 -- .c files; this is the Value Add(TM) that using
616 -- ghc instead of gcc gives you :)
617 pkg_include_dirs <- getPackageIncludePath
618 let include_paths = foldr (\ x xs -> "-I" : x : xs) []
619 (cmdline_include_paths ++ pkg_include_dirs)
621 mangle <- readIORef v_Do_asm_mangling
622 (md_c_flags, md_regd_c_flags) <- machdepCCOpts
626 o2 <- readIORef v_minus_o2_for_C
627 let opt_flag | o2 = "-O2"
630 pkg_extra_cc_opts <- getPackageExtraCcOpts
632 split_objs <- readIORef v_Split_object_files
633 let split_opt | hcc && split_objs = [ "-DUSE_SPLIT_MARKERS" ]
636 excessPrecision <- readIORef v_Excess_precision
638 -- force the C compiler to interpret this file as C when
639 -- compiling .hc files, by adding the -x c option.
641 | cc_phase == HCc = [ SysTools.Option "-x", SysTools.Option "c"]
644 SysTools.runCc (langopt ++
645 [ SysTools.FileOption "" input_fn
646 , SysTools.Option "-o"
647 , SysTools.FileOption "" output_fn
649 ++ map SysTools.Option (
651 ++ (if cc_phase == HCc && mangle
654 ++ [ verb, "-S", "-Wimplicit", opt_flag ]
655 ++ [ "-D__GLASGOW_HASKELL__="++cProjectVersionInt ]
658 ++ (if excessPrecision then [] else [ "-ffloat-store" ])
662 return (Just output_fn)
664 -- ToDo: postprocess the output from gcc
666 -----------------------------------------------------------------------------
669 run_phase Mangle _basename _suff input_fn output_fn
670 = do mangler_opts <- getOpts opt_m
671 machdep_opts <- if (prefixMatch "i386" cTARGETPLATFORM)
672 then do n_regs <- dynFlag stolen_x86_regs
673 return [ show n_regs ]
676 SysTools.runMangle (map SysTools.Option mangler_opts
677 ++ [ SysTools.FileOption "" input_fn
678 , SysTools.FileOption "" output_fn
680 ++ map SysTools.Option machdep_opts)
681 return (Just output_fn)
683 -----------------------------------------------------------------------------
686 run_phase SplitMangle _basename _suff input_fn output_fn
687 = do -- tmp_pfx is the prefix used for the split .s files
688 -- We also use it as the file to contain the no. of split .s files (sigh)
689 split_s_prefix <- SysTools.newTempName "split"
690 let n_files_fn = split_s_prefix
692 SysTools.runSplit [ SysTools.FileOption "" input_fn
693 , SysTools.FileOption "" split_s_prefix
694 , SysTools.FileOption "" n_files_fn
697 -- Save the number of split files for future references
698 s <- readFile n_files_fn
699 let n_files = read s :: Int
700 writeIORef v_Split_info (split_s_prefix, n_files)
702 -- Remember to delete all these files
703 addFilesToClean [ split_s_prefix ++ "__" ++ show n ++ ".s"
706 return (Just output_fn)
708 -----------------------------------------------------------------------------
711 run_phase As _basename _suff input_fn output_fn
712 = do as_opts <- getOpts opt_a
713 cmdline_include_paths <- readIORef v_Include_paths
715 SysTools.runAs (map SysTools.Option as_opts
716 ++ [ SysTools.Option ("-I" ++ p) | p <- cmdline_include_paths ]
717 ++ [ SysTools.Option "-c"
718 , SysTools.FileOption "" input_fn
719 , SysTools.Option "-o"
720 , SysTools.FileOption "" output_fn
722 return (Just output_fn)
724 run_phase SplitAs basename _suff _input_fn output_fn
725 = do as_opts <- getOpts opt_a
727 (split_s_prefix, n) <- readIORef v_Split_info
729 odir <- readIORef v_Output_dir
730 let real_odir = case odir of
731 Nothing -> basename ++ "_split"
735 = do let input_s = split_s_prefix ++ "__" ++ show n ++ ".s"
736 let output_o = newdir real_odir
737 (basename ++ "__" ++ show n ++ ".o")
738 real_o <- osuf_ify output_o
739 SysTools.runAs (map SysTools.Option as_opts ++
740 [ SysTools.Option "-c"
741 , SysTools.Option "-o"
742 , SysTools.FileOption "" real_o
743 , SysTools.FileOption "" input_s
746 mapM_ assemble_file [1..n]
747 return (Just output_fn)
750 -----------------------------------------------------------------------------
752 -- Run ilx2il over the ILX output, getting an IL file
754 run_phase Ilx2Il _basename _suff input_fn output_fn
755 = do ilx2il_opts <- getOpts opt_I
756 SysTools.runIlx2il (map SysTools.Option ilx2il_opts
757 ++ [ SysTools.Option "--no-add-suffix-to-assembly",
758 SysTools.Option "mscorlib",
759 SysTools.Option "-o",
760 SysTools.FileOption "" output_fn,
761 SysTools.FileOption "" input_fn ])
762 return (Just output_fn)
764 -----------------------------------------------------------------------------
766 -- Run ilasm over the IL, getting a DLL
768 run_phase Ilasm _basename _suff input_fn output_fn
769 = do ilasm_opts <- getOpts opt_i
770 SysTools.runIlasm (map SysTools.Option ilasm_opts
771 ++ [ SysTools.Option "/QUIET",
772 SysTools.Option "/DLL",
773 SysTools.FileOption "/OUT=" output_fn,
774 SysTools.FileOption "" input_fn ])
775 return (Just output_fn)
779 -----------------------------------------------------------------------------
780 -- MoveBinary sort-of-phase
781 -- After having produced a binary, move it somewhere else and generate a
782 -- wrapper script calling the binary. Currently, we need this only in
783 -- a parallel way (i.e. in GUM), because PVM expects the binary in a
784 -- central directory.
785 -- This is called from doLink below, after linking. I haven't made it
786 -- a separate phase to minimise interfering with other modules, and
787 -- we don't need the generality of a phase (MoveBinary is always
788 -- done after linking and makes only sense in a parallel setup) -- HWL
790 run_phase_MoveBinary input_fn
793 pvm_root <- getEnv "PVM_ROOT"
794 pvm_arch <- getEnv "PVM_ARCH"
796 pvm_executable_base = "=" ++ input_fn
797 pvm_executable = pvm_root ++ "/bin/" ++ pvm_arch ++ "/" ++ pvm_executable_base
798 -- nuke old binary; maybe use configur'ed names for cp and rm?
799 system ("rm -f " ++ pvm_executable)
800 -- move the newly created binary into PVM land
801 system ("cp -p " ++ input_fn ++ " " ++ pvm_executable)
802 -- generate a wrapper script for running a parallel prg under PVM
803 writeFile input_fn (mk_pvm_wrapper_script pvm_executable pvm_executable_base sysMan)
806 -- generates a Perl skript starting a parallel prg under PVM
807 mk_pvm_wrapper_script :: String -> String -> String -> String
808 mk_pvm_wrapper_script pvm_executable pvm_executable_base sysMan = unlines $
810 "eval 'exec perl -S $0 ${1+\"$@\"}'",
811 " if $running_under_some_shell;",
812 "# =!=!=!=!=!=!=!=!=!=!=!",
813 "# This script is automatically generated: DO NOT EDIT!!!",
814 "# Generated by Glasgow Haskell Compiler",
815 "# ngoqvam choHbogh vaj' vIHoHnISbej !!!!",
817 "$pvm_executable = '" ++ pvm_executable ++ "';",
818 "$pvm_executable_base = '" ++ pvm_executable_base ++ "';",
819 "$SysMan = '" ++ sysMan ++ "';",
821 {- ToDo: add the magical shortcuts again iff we actually use them -- HWL
822 "# first, some magical shortcuts to run "commands" on the binary",
823 "# (which is hidden)",
824 "if ($#ARGV == 1 && $ARGV[0] eq '+RTS' && $ARGV[1] =~ /^--((size|file|strip|rm|nm).*)/ ) {",
825 " local($cmd) = $1;",
826 " system("$cmd $pvm_executable");",
827 " exit(0); # all done",
830 "# Now, run the real binary; process the args first",
831 "$ENV{'PE'} = $pvm_executable_base;", -- ++ pvm_executable_base,
833 "$nprocessors = 0; # the default: as many PEs as machines in PVM config",
834 "@nonPVM_args = ();",
837 "args: while ($a = shift(@ARGV)) {",
838 " if ( $a eq '+RTS' ) {",
839 " $in_RTS_args = 1;",
840 " } elsif ( $a eq '-RTS' ) {",
841 " $in_RTS_args = 0;",
843 " if ( $a eq '-d' && $in_RTS_args ) {",
845 " } elsif ( $a =~ /^-qN(\\d+)/ && $in_RTS_args ) {",
846 " $nprocessors = $1;",
847 " } elsif ( $a =~ /^-qp(\\d+)/ && $in_RTS_args ) {",
848 " $nprocessors = $1;",
850 " push(@nonPVM_args, $a);",
854 "local($return_val) = 0;",
855 "# Start the parallel execution by calling SysMan",
856 "system(\"$SysMan $debug $pvm_executable $nprocessors @nonPVM_args\");",
858 "# ToDo: fix race condition moving files and flushing them!!",
859 "system(\"cp $ENV{'HOME'}/$pvm_executable_base.???.gr .\") if -f \"$ENV{'HOME'}/$pvm_executable_base.002.gr\";",
863 -----------------------------------------------------------------------------
864 -- Complain about non-dynamic flags in OPTIONS pragmas
866 checkProcessArgsResult flags basename suff
867 = do when (notNull flags) (throwDyn (ProgramError (
868 basename ++ "." ++ suff
869 ++ ": static flags are not allowed in {-# OPTIONS #-} pragmas:\n\t"
870 ++ unwords flags)) (ExitFailure 1))
872 -----------------------------------------------------------------------------
875 doLink :: [String] -> IO ()
878 static <- readIORef v_Static
879 no_hs_main <- readIORef v_NoHsMain
881 o_file <- readIORef v_Output_file
882 let output_fn = case o_file of { Just s -> s; Nothing -> "a.out"; }
884 pkg_lib_paths <- getPackageLibraryPath
885 let pkg_lib_path_opts = map ("-L"++) pkg_lib_paths
887 lib_paths <- readIORef v_Library_paths
888 let lib_path_opts = map ("-L"++) lib_paths
890 pkg_libs <- getPackageLibraries
891 let imp = if static then "" else "_imp"
892 pkg_lib_opts = map (\lib -> "-l" ++ lib ++ imp) pkg_libs
894 libs <- readIORef v_Cmdline_libraries
895 let lib_opts = map ("-l"++) (reverse libs)
896 -- reverse because they're added in reverse order from the cmd line
898 #ifdef darwin_TARGET_OS
899 pkg_framework_paths <- getPackageFrameworkPath
900 let pkg_framework_path_opts = map ("-F"++) pkg_framework_paths
902 framework_paths <- readIORef v_Framework_paths
903 let framework_path_opts = map ("-F"++) framework_paths
905 pkg_frameworks <- getPackageFrameworks
906 let pkg_framework_opts = map ("-framework " ++) pkg_frameworks
908 frameworks <- readIORef v_Cmdline_frameworks
909 let framework_opts = map ("-framework "++) (reverse frameworks)
910 -- reverse because they're added in reverse order from the cmd line
913 pkg_extra_ld_opts <- getPackageExtraLdOpts
915 -- probably _stub.o files
916 extra_ld_inputs <- readIORef v_Ld_inputs
918 -- opts from -optl-<blah>
919 extra_ld_opts <- getStaticOpts v_Opt_l
921 rts_pkg <- getPackageDetails ["rts"]
922 std_pkg <- getPackageDetails ["std"]
923 let extra_os = if static || no_hs_main
925 else [ head (library_dirs (head rts_pkg)) ++ "/Main.dll_o",
926 head (library_dirs (head std_pkg)) ++ "/PrelMain.dll_o" ]
928 (md_c_flags, _) <- machdepCCOpts
929 SysTools.runLink ( [ SysTools.Option verb
930 , SysTools.Option "-o"
931 , SysTools.FileOption "" output_fn
933 ++ map SysTools.Option (
940 #ifdef darwin_TARGET_OS
941 ++ framework_path_opts
946 #ifdef darwin_TARGET_OS
947 ++ pkg_framework_path_opts
948 ++ pkg_framework_opts
952 ++ (if static then [ "-static" ] else [])
953 ++ if static && not no_hs_main then
954 [ "-u", prefixUnderscore "Main_zdmain_closure"]
957 -- parallel only: move binary to another dir -- HWL
958 ways_ <- readIORef v_Ways
959 when (WayPar `elem` ways_)
960 (do success <- run_phase_MoveBinary output_fn
961 if success then return ()
962 else throwDyn (InstallationError ("cannot move binary to PVM dir")))
964 -----------------------------------------------------------------------------
965 -- Making a DLL (only for Win32)
967 doMkDLL :: [String] -> IO ()
970 static <- readIORef v_Static
971 no_hs_main <- readIORef v_NoHsMain
973 o_file <- readIORef v_Output_file
974 let output_fn = case o_file of { Just s -> s; Nothing -> "HSdll.dll"; }
976 pkg_lib_paths <- getPackageLibraryPath
977 let pkg_lib_path_opts = map ("-L"++) pkg_lib_paths
979 lib_paths <- readIORef v_Library_paths
980 let lib_path_opts = map ("-L"++) lib_paths
982 pkg_libs <- getPackageLibraries
983 let imp = if static then "" else "_imp"
984 pkg_lib_opts = map (\lib -> "-l" ++ lib ++ imp) pkg_libs
986 libs <- readIORef v_Cmdline_libraries
987 let lib_opts = map ("-l"++) (reverse libs)
988 -- reverse because they're added in reverse order from the cmd line
990 pkg_extra_ld_opts <- getPackageExtraLdOpts
992 -- probably _stub.o files
993 extra_ld_inputs <- readIORef v_Ld_inputs
995 -- opts from -optdll-<blah>
996 extra_ld_opts <- getStaticOpts v_Opt_dll
998 rts_pkg <- getPackageDetails ["rts"]
999 std_pkg <- getPackageDetails ["std"]
1001 let extra_os = if static || no_hs_main
1003 else [ head (library_dirs (head rts_pkg)) ++ "/Main.dll_o",
1004 head (library_dirs (head std_pkg)) ++ "/PrelMain.dll_o" ]
1006 (md_c_flags, _) <- machdepCCOpts
1008 ([ SysTools.Option verb
1009 , SysTools.Option "-o"
1010 , SysTools.FileOption "" output_fn
1012 ++ map SysTools.Option (
1016 ++ [ "--target=i386-mingw32" ]
1020 ++ pkg_lib_path_opts
1022 ++ pkg_extra_ld_opts
1023 ++ (if "--def" `elem` (concatMap words extra_ld_opts)
1025 else [ "--export-all" ])
1029 -----------------------------------------------------------------------------
1030 -- Just preprocess a file, put the result in a temp. file (used by the
1031 -- compilation manager during the summary phase).
1033 preprocess :: FilePath -> IO FilePath
1034 preprocess filename =
1035 ASSERT(haskellish_src_file filename)
1036 do restoreDynFlags -- Restore to state of last save
1037 let fInfo = (filename, getFileSuffix filename)
1038 pipeline <- genPipeline (StopBefore Hsc) ("preprocess") False
1039 defaultHscLang fInfo
1040 (fn,_) <- runPipeline pipeline fInfo
1041 False{-no linking-} False{-no -o flag-}
1044 -----------------------------------------------------------------------------
1045 -- Compile a single module, under the control of the compilation manager.
1047 -- This is the interface between the compilation manager and the
1048 -- compiler proper (hsc), where we deal with tedious details like
1049 -- reading the OPTIONS pragma from the source file, and passing the
1050 -- output of hsc through the C compiler.
1052 -- The driver sits between 'compile' and 'hscMain', translating calls
1053 -- to the former into calls to the latter, and results from the latter
1054 -- into results from the former. It does things like preprocessing
1055 -- the .hs file if necessary, and compiling up the .stub_c files to
1056 -- generate Linkables.
1058 -- NB. No old interface can also mean that the source has changed.
1060 compile :: GhciMode -- distinguish batch from interactive
1061 -> ModSummary -- summary, including source
1062 -> Bool -- True <=> source unchanged
1063 -> Bool -- True <=> have object
1064 -> Maybe ModIface -- old interface, if available
1065 -> HomeSymbolTable -- for home module ModDetails
1066 -> HomeIfaceTable -- for home module Ifaces
1067 -> PersistentCompilerState -- persistent compiler state
1071 = CompOK PersistentCompilerState -- updated PCS
1072 ModDetails -- new details (HST additions)
1073 ModIface -- new iface (HIT additions)
1075 -- new code; Nothing => compilation was not reqd
1076 -- (old code is still valid)
1078 | CompErrs PersistentCompilerState -- updated PCS
1081 compile ghci_mode summary source_unchanged have_object
1082 old_iface hst hit pcs = do
1083 dyn_flags <- restoreDynFlags -- Restore to the state of the last save
1087 (showSDoc (text "Compiling" <+> ppr (modSummaryName summary)))
1089 let verb = verbosity dyn_flags
1090 let location = ms_location summary
1091 let input_fn = expectJust "compile:hs" (ml_hs_file location)
1092 let input_fnpp = expectJust "compile:hspp" (ml_hspp_file location)
1094 when (verb >= 2) (hPutStrLn stderr ("compile: input file " ++ input_fnpp))
1096 opts <- getOptionsFromSource input_fnpp
1097 processArgs dynamic_flags opts []
1098 dyn_flags <- getDynFlags
1100 let hsc_lang = hscLang dyn_flags
1101 (basename, _) = splitFilename input_fn
1103 keep_hc <- readIORef v_Keep_hc_files
1105 keep_il <- readIORef v_Keep_il_files
1107 keep_s <- readIORef v_Keep_s_files
1111 HscAsm | keep_s -> return (basename ++ '.':phaseInputExt As)
1112 | otherwise -> newTempName (phaseInputExt As)
1113 HscC | keep_hc -> return (basename ++ '.':phaseInputExt HCc)
1114 | otherwise -> newTempName (phaseInputExt HCc)
1115 HscJava -> newTempName "java" -- ToDo
1117 HscILX | keep_il -> return (basename ++ '.':phaseInputExt Ilasm)
1118 | otherwise -> newTempName (phaseInputExt Ilx2Il)
1120 HscInterpreted -> return (error "no output file")
1121 HscNothing -> return (error "no output file")
1123 let dyn_flags' = dyn_flags { hscOutName = output_fn,
1124 hscStubCOutName = basename ++ "_stub.c",
1125 hscStubHOutName = basename ++ "_stub.h",
1126 extCoreName = basename ++ ".hcr" }
1128 -- figure out which header files to #include in a generated .hc file
1129 c_includes <- getPackageCIncludes
1130 cmdline_includes <- dynFlag cmdlineHcIncludes -- -#include options
1132 let cc_injects = unlines (map mk_include
1133 (c_includes ++ reverse cmdline_includes))
1136 '"':_{-"-} -> "#include "++h_file
1137 '<':_ -> "#include "++h_file
1138 _ -> "#include \""++h_file++"\""
1140 writeIORef v_HCHeader cc_injects
1142 -- -no-recomp should also work with --make
1143 do_recomp <- readIORef v_Recomp
1144 let source_unchanged' = source_unchanged && do_recomp
1147 hsc_result <- hscMain ghci_mode dyn_flags'
1148 (ms_mod summary) location
1149 source_unchanged' have_object old_iface hst hit pcs
1152 HscFail pcs -> return (CompErrs pcs)
1154 HscNoRecomp pcs details iface -> return (CompOK pcs details iface Nothing)
1156 HscRecomp pcs details iface
1157 stub_h_exists stub_c_exists maybe_interpreted_code -> do
1159 maybe_stub_o <- compileStub dyn_flags' stub_c_exists
1160 let stub_unlinked = case maybe_stub_o of
1162 Just stub_o -> [ DotO stub_o ]
1164 (hs_unlinked, unlinked_time) <-
1167 -- in interpreted mode, just return the compiled code
1168 -- as our "unlinked" object.
1170 case maybe_interpreted_code of
1172 Just (bcos,itbl_env) -> do tm <- getClockTime
1173 return ([BCOs bcos itbl_env], tm)
1175 Nothing -> panic "compile: no interpreted code"
1177 -- we're in batch mode: finish the compilation pipeline.
1178 _other -> do pipe <- genPipeline (StopBefore Ln) "" True
1179 hsc_lang (output_fn, getFileSuffix output_fn)
1180 -- runPipeline takes input_fn so it can split off
1181 -- the base name and use it as the base of
1182 -- the output object file.
1183 let (basename, suffix) = splitFilename input_fn
1185 pipeLoop pipe (output_fn, getFileSuffix output_fn)
1188 o_time <- getModificationTime o_file
1189 return ([DotO o_file], o_time)
1191 let linkable = LM unlinked_time (modSummaryName summary)
1192 (hs_unlinked ++ stub_unlinked)
1194 return (CompOK pcs details iface (Just linkable))
1197 -----------------------------------------------------------------------------
1198 -- stub .h and .c files (for foreign export support)
1200 compileStub dflags stub_c_exists
1201 | not stub_c_exists = return Nothing
1202 | stub_c_exists = do
1203 -- compile the _stub.c file w/ gcc
1204 let stub_c = hscStubCOutName dflags
1205 pipeline <- genPipeline (StopBefore Ln) "" True defaultHscLang (stub_c,"c")
1206 (stub_o,_) <- runPipeline pipeline (stub_c,"c") False{-no linking-}
1207 False{-no -o option-}
1208 return (Just stub_o)