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 runPipeline, 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 -- ---------------------------------------------------------------------------
67 -- Just preprocess a file, put the result in a temp. file (used by the
68 -- compilation manager during the summary phase).
70 preprocess :: FilePath -> IO FilePath
72 ASSERT(haskellish_src_file filename)
73 do restoreDynFlags -- Restore to state of last save
74 runPipeline (StopBefore Hsc) ("preprocess")
75 False{-temporary output file-}
76 Nothing{-no specific output file-}
79 -- ---------------------------------------------------------------------------
82 -- Compile a single module, under the control of the compilation manager.
84 -- This is the interface between the compilation manager and the
85 -- compiler proper (hsc), where we deal with tedious details like
86 -- reading the OPTIONS pragma from the source file, and passing the
87 -- output of hsc through the C compiler.
89 -- The driver sits between 'compile' and 'hscMain', translating calls
90 -- to the former into calls to the latter, and results from the latter
91 -- into results from the former. It does things like preprocessing
92 -- the .hs file if necessary, and compiling up the .stub_c files to
93 -- generate Linkables.
95 -- NB. No old interface can also mean that the source has changed.
97 compile :: GhciMode -- distinguish batch from interactive
100 -> Bool -- True <=> source unchanged
101 -> Bool -- True <=> have object
102 -> Maybe ModIface -- old interface, if available
103 -> HomePackageTable -- For home-module stuff
104 -> PersistentCompilerState -- persistent compiler state
108 = CompOK PersistentCompilerState -- Updated PCS
109 ModDetails -- New details
110 ModIface -- New iface
111 (Maybe Linkable) -- New code; Nothing => compilation was not reqd
112 -- (old code is still valid)
114 | CompErrs PersistentCompilerState -- Updated PCS
117 compile ghci_mode this_mod location
118 source_unchanged have_object
119 old_iface hpt pcs = do
121 dyn_flags <- restoreDynFlags -- Restore to the state of the last save
124 (showSDoc (text "Compiling" <+> ppr this_mod))
126 let verb = verbosity dyn_flags
127 let input_fn = expectJust "compile:hs" (ml_hs_file location)
128 let input_fnpp = expectJust "compile:hspp" (ml_hspp_file location)
129 let mod_name = moduleName this_mod
131 when (verb >= 2) (hPutStrLn stderr ("compile: input file " ++ input_fnpp))
133 opts <- getOptionsFromSource input_fnpp
134 processArgs dynamic_flags opts []
135 dyn_flags <- getDynFlags
137 let hsc_lang = hscLang dyn_flags
138 (basename, _) = splitFilename input_fn
140 keep_hc <- readIORef v_Keep_hc_files
142 keep_il <- readIORef v_Keep_il_files
144 keep_s <- readIORef v_Keep_s_files
148 HscAsm | keep_s -> return (basename ++ '.':phaseInputExt As)
149 | otherwise -> newTempName (phaseInputExt As)
150 HscC | keep_hc -> return (basename ++ '.':phaseInputExt HCc)
151 | otherwise -> newTempName (phaseInputExt HCc)
152 HscJava -> newTempName "java" -- ToDo
154 HscILX | keep_il -> return (basename ++ '.':phaseInputExt Ilasm)
155 | otherwise -> newTempName (phaseInputExt Ilx2Il)
157 HscInterpreted -> return (error "no output file")
158 HscNothing -> return (error "no output file")
160 let dyn_flags' = dyn_flags { hscOutName = output_fn,
161 hscStubCOutName = basename ++ "_stub.c",
162 hscStubHOutName = basename ++ "_stub.h",
163 extCoreName = basename ++ ".hcr" }
165 -- -no-recomp should also work with --make
166 do_recomp <- readIORef v_Recomp
167 let source_unchanged' = source_unchanged && do_recomp
168 hsc_env = HscEnv { hsc_mode = ghci_mode,
169 hsc_dflags = dyn_flags',
173 hsc_result <- hscMain hsc_env pcs this_mod location
174 source_unchanged' have_object old_iface
177 HscFail pcs -> return (CompErrs pcs)
179 HscNoRecomp pcs details iface -> return (CompOK pcs details iface Nothing)
181 HscRecomp pcs details iface
182 stub_h_exists stub_c_exists maybe_interpreted_code -> do
184 maybe_stub_o <- compileStub dyn_flags' stub_c_exists
185 let stub_unlinked = case maybe_stub_o of
187 Just stub_o -> [ DotO stub_o ]
189 (hs_unlinked, unlinked_time) <-
192 -- in interpreted mode, just return the compiled code
193 -- as our "unlinked" object.
195 case maybe_interpreted_code of
197 Just comp_bc -> do tm <- getClockTime
198 return ([BCOs comp_bc], tm)
200 Nothing -> panic "compile: no interpreted code"
202 -- we're in batch mode: finish the compilation pipeline.
204 let object_filename = ml_obj_file location
205 object_dir = directoryOf object_filename
207 -- create the object dir if it doesn't exist
208 createDirectoryHierarchy object_dir
210 runPipeline (StopBefore Ln) ""
211 True (Just object_filename) output_fn
213 o_time <- getModificationTime object_filename
214 return ([DotO object_filename], o_time)
216 let linkable = LM unlinked_time mod_name
217 (hs_unlinked ++ stub_unlinked)
219 return (CompOK pcs details iface (Just linkable))
221 -----------------------------------------------------------------------------
222 -- stub .h and .c files (for foreign export support)
224 compileStub dflags stub_c_exists
225 | not stub_c_exists = return Nothing
227 -- compile the _stub.c file w/ gcc
228 let stub_c = hscStubCOutName dflags
229 stub_o <- runPipeline (StopBefore Ln) "stub-compile"
230 True{-persistent output-}
231 Nothing{-no specific output file-}
236 -- ---------------------------------------------------------------------------
239 link :: GhciMode -- interactive or batch
240 -> DynFlags -- dynamic flags
241 -> Bool -- attempt linking in batch mode?
242 -> HomePackageTable -- what to link
245 -- For the moment, in the batch linker, we don't bother to tell doLink
246 -- which packages to link -- it just tries all that are available.
247 -- batch_attempt_linking should only be *looked at* in batch mode. It
248 -- should only be True if the upsweep was successful and someone
249 -- exports main, i.e., we have good reason to believe that linking
253 link Interactive dflags batch_attempt_linking hpt
254 = do -- Not Linking...(demand linker will do the job)
258 link Batch dflags batch_attempt_linking hpt
259 | batch_attempt_linking
262 home_mod_infos = moduleEnvElts hpt
264 -- the packages we depend on
265 pkg_deps = concatMap (dep_pkgs . mi_deps . hm_iface) home_mod_infos
267 -- the linkables to link
268 linkables = map hm_linkable home_mod_infos
270 when (verb >= 3) $ do
271 hPutStrLn stderr "link: linkables are ..."
272 hPutStrLn stderr (showSDoc (vcat (map ppr linkables)))
274 -- check for the -no-link flag
275 omit_linking <- readIORef v_NoLink
277 then do when (verb >= 3) $
278 hPutStrLn stderr "link(batch): linking omitted (-no-link flag given)."
283 hPutStrLn stderr "Linking ..."
285 let getOfiles (LM _ _ us) = map nameOfObject (filter isObject us)
286 obj_files = concatMap getOfiles linkables
288 -- Don't showPass in Batch mode; doLink will do that for us.
289 staticLink obj_files pkg_deps
291 when (verb >= 3) (hPutStrLn stderr "link: done")
293 -- staticLink only returns if it succeeds
297 = do when (verb >= 3) $ do
298 hPutStrLn stderr "link(batch): upsweep (partially) failed OR"
299 hPutStrLn stderr " Main.main not exported; not linking."
302 verb = verbosity dflags
304 -- ---------------------------------------------------------------------------
305 -- Run a compilation pipeline, consisting of multiple phases.
308 :: GhcMode -- when to stop
309 -> String -- "stop after" flag
310 -> Bool -- final output is persistent?
311 -> Maybe FilePath -- where to put the output, optionally
312 -> FilePath -- input filename
313 -> IO FilePath -- output filename
315 runPipeline todo stop_flag keep_output maybe_output_filename input_fn
317 split <- readIORef v_Split_object_files
318 let (basename, suffix) = splitFilename input_fn
319 start_phase = startPhase suffix
321 stop_phase = case todo of
322 StopBefore As | split -> SplitAs
323 StopBefore phase -> phase
328 -- We want to catch cases of "you can't get there from here" before
329 -- we start the pipeline, because otherwise it will just run off the
332 -- There is a partial ordering on phases, where A < B iff A occurs
333 -- before B in a normal compilation pipeline.
335 when (not (start_phase `happensBefore` stop_phase)) $
337 ("flag `" ++ stop_flag
338 ++ "' is incompatible with source file `"
341 -- generate a function which will be used to calculate output file names
343 get_output_fn <- genOutputFilenameFunc keep_output maybe_output_filename
346 -- and execute the pipeline...
347 output_fn <- pipeLoop start_phase stop_phase input_fn basename suffix
350 -- sometimes, a compilation phase doesn't actually generate any output
351 -- (eg. the CPP phase when -fcpp is not turned on). If we end on this
352 -- stage, but we wanted to keep the output, then we have to explicitly
355 then do final_fn <- get_output_fn stop_phase
356 when (final_fn /= output_fn) $
357 copy ("Copying `" ++ output_fn ++ "' to `" ++ final_fn
358 ++ "'") output_fn final_fn
364 pipeLoop :: Phase -> Phase -> FilePath -> String -> Suffix
365 -> (Phase -> IO FilePath) -> IO FilePath
366 pipeLoop phase stop_phase input_fn orig_basename orig_suff get_output_fn
367 | phase == stop_phase = return input_fn -- all done
369 maybe_next_phase <- runPhase phase orig_basename orig_suff input_fn
371 case maybe_next_phase of
372 (Nothing, output_fn) -> return output_fn
373 (Just next_phase, output_fn) ->
374 pipeLoop next_phase stop_phase output_fn
375 orig_basename orig_suff get_output_fn
378 genOutputFilenameFunc :: Bool -> Maybe FilePath -> Phase -> String
379 -> IO (Phase{-next phase-} -> IO FilePath)
380 genOutputFilenameFunc keep_output maybe_output_filename stop_phase basename
382 hcsuf <- readIORef v_HC_suf
383 osuf <- readIORef v_Object_suf
384 keep_hc <- readIORef v_Keep_hc_files
386 keep_il <- readIORef v_Keep_il_files
387 keep_ilx <- readIORef v_Keep_ilx_files
389 keep_raw_s <- readIORef v_Keep_raw_s_files
390 keep_s <- readIORef v_Keep_s_files
392 myPhaseInputExt HCc | Just s <- hcsuf = s
393 myPhaseInputExt Ln = osuf
394 myPhaseInputExt other = phaseInputExt other
397 | next_phase == stop_phase
398 = case maybe_output_filename of
399 Just file -> return file
400 Nothing | keep_output -> return persistent
401 | otherwise -> newTempName suffix
402 -- sometimes, we keep output from intermediate stages
405 Ln -> return persistent
406 Mangle | keep_raw_s -> return persistent
407 As | keep_s -> return persistent
408 HCc | keep_hc -> return persistent
409 _other -> newTempName suffix
411 suffix = myPhaseInputExt next_phase
412 persistent = basename ++ '.':suffix
417 -- -----------------------------------------------------------------------------
418 -- Each phase in the pipeline returns the next phase to execute, and the
419 -- name of the file in which the output was placed.
421 -- We must do things dynamically this way, because we often don't know
422 -- what the rest of the phases will be until part-way through the
423 -- compilation: for example, an {-# OPTIONS -fasm #-} at the beginning
424 -- of a source file can change the latter stages of the pipeline from
425 -- taking the via-C route to using the native code generator.
428 -> String -- basename of original input source
429 -> String -- its extension
430 -> FilePath -- name of file which contains the input to this phase.
431 -> (Phase -> IO FilePath) -- how to calculate the output filename
432 -> IO (Maybe Phase, -- next phase
433 FilePath) -- output filename
435 -------------------------------------------------------------------------------
438 runPhase Unlit _basename _suff input_fn get_output_fn
439 = do unlit_flags <- getOpts opt_L
440 -- The -h option passes the file name for unlit to put in a #line directive
441 output_fn <- get_output_fn Cpp
443 SysTools.runUnlit (map SysTools.Option unlit_flags ++
444 [ SysTools.Option "-h"
445 , SysTools.Option input_fn
446 , SysTools.FileOption "" input_fn
447 , SysTools.FileOption "" output_fn
450 return (Just Cpp, output_fn)
452 -------------------------------------------------------------------------------
455 runPhase Cpp basename suff input_fn get_output_fn
456 = do src_opts <- getOptionsFromSource input_fn
457 unhandled_flags <- processArgs dynamic_flags src_opts []
458 checkProcessArgsResult unhandled_flags basename suff
460 do_cpp <- dynFlag cppFlag
462 -- no need to preprocess CPP, just pass input file along
463 -- to the next phase of the pipeline.
464 return (Just HsPp, input_fn)
466 hscpp_opts <- getOpts opt_P
467 hs_src_cpp_opts <- readIORef v_Hs_source_cpp_opts
469 cmdline_include_paths <- readIORef v_Include_paths
471 pkg_include_dirs <- getPackageIncludePath []
472 let include_paths = foldr (\ x xs -> "-I" : x : xs) []
473 (cmdline_include_paths ++ pkg_include_dirs)
476 (md_c_flags, _) <- machdepCCOpts
478 output_fn <- get_output_fn HsPp
480 SysTools.runCpp ([SysTools.Option verb]
481 ++ map SysTools.Option include_paths
482 ++ map SysTools.Option hs_src_cpp_opts
483 ++ map SysTools.Option hscpp_opts
484 ++ map SysTools.Option md_c_flags
485 ++ [ SysTools.Option "-x"
486 , SysTools.Option "c"
487 , SysTools.Option input_fn
488 -- We hackily use Option instead of FileOption here, so that the file
489 -- name is not back-slashed on Windows. cpp is capable of
490 -- dealing with / in filenames, so it works fine. Furthermore
491 -- if we put in backslashes, cpp outputs #line directives
492 -- with *double* backslashes. And that in turn means that
493 -- our error messages get double backslashes in them.
494 -- In due course we should arrange that the lexer deals
495 -- with these \\ escapes properly.
496 , SysTools.Option "-o"
497 , SysTools.FileOption "" output_fn
500 return (Just HsPp, output_fn)
502 -------------------------------------------------------------------------------
505 runPhase HsPp basename suff input_fn get_output_fn
506 = do do_pp <- dynFlag ppFlag
508 -- no need to preprocess, just pass input file along
509 -- to the next phase of the pipeline.
510 return (Just Hsc, input_fn)
512 hspp_opts <- getOpts opt_F
513 hs_src_pp_opts <- readIORef v_Hs_source_pp_opts
514 let orig_fn = basename ++ '.':suff
515 output_fn <- get_output_fn Hsc
516 SysTools.runPp ( [ SysTools.Option orig_fn
517 , SysTools.Option input_fn
518 , SysTools.FileOption "" output_fn
520 map SysTools.Option hs_src_pp_opts ++
521 map SysTools.Option hspp_opts
523 return (Just Hsc, output_fn)
525 -----------------------------------------------------------------------------
528 -- Compilation of a single module, in "legacy" mode (_not_ under
529 -- the direction of the compilation manager).
530 runPhase Hsc basename suff input_fn get_output_fn = do
531 todo <- readIORef v_GhcMode
532 if todo == DoMkDependHS then do
533 doMkDependHSPhase basename suff input_fn
534 return (Nothing, input_fn) -- Ln is a dummy stop phase
537 -- normal Hsc mode, not mkdependHS
539 -- we add the current directory (i.e. the directory in which
540 -- the .hs files resides) to the import path, since this is
541 -- what gcc does, and it's probably what you want.
542 let current_dir = directoryOf basename
544 paths <- readIORef v_Include_paths
545 writeIORef v_Include_paths (current_dir : paths)
547 -- gather the imports and module name
549 if extcoreish_suffix suff
551 -- no explicit imports in ExtCore input.
552 m <- getCoreModuleName input_fn
553 return ([], [], mkModuleName m)
555 getImportsFromFile input_fn
557 -- build a ModLocation to pass to hscMain.
558 let (path,file) = splitFilenameDir basename
559 (mod, location') <- mkHomeModLocation mod_name True path file suff
561 -- take -ohi into account if present
562 ohi <- readIORef v_Output_hi
563 let location | Just fn <- ohi = location'{ ml_hi_file = fn }
564 | otherwise = location'
566 -- figure out if the source has changed, for recompilation avoidance.
567 -- only do this if we're eventually going to generate a .o file.
568 -- (ToDo: do when generating .hc files too?)
570 -- Setting source_unchanged to True means that M.o seems
571 -- to be up to date wrt M.hs; so no need to recompile unless imports have
572 -- changed (which the compiler itself figures out).
573 -- Setting source_unchanged to False tells the compiler that M.o is out of
574 -- date wrt M.hs (or M.o doesn't exist) so we must recompile regardless.
575 do_recomp <- readIORef v_Recomp
576 expl_o_file <- readIORef v_Output_file
578 let o_file -- if the -o option is given and IT IS THE OBJECT FILE FOR
579 -- THIS COMPILATION, then use that to determine if the
580 -- source is unchanged.
581 | Just x <- expl_o_file, todo == StopBefore Ln = x
582 | otherwise = ml_obj_file location
585 if not (do_recomp && ( todo == DoLink || todo == StopBefore Ln ))
587 else do t1 <- getModificationTime (basename ++ '.':suff)
588 o_file_exists <- doesFileExist o_file
590 then return False -- Need to recompile
591 else do t2 <- getModificationTime o_file
597 dyn_flags <- getDynFlags
598 let hsc_lang = hscLang dyn_flags
599 split <- readIORef v_Split_object_files
601 let next_phase = case hsc_lang of
603 HscAsm | split -> SplitMangle
607 output_fn <- get_output_fn next_phase
609 let dyn_flags' = dyn_flags { hscOutName = output_fn,
610 hscStubCOutName = basename ++ "_stub.c",
611 hscStubHOutName = basename ++ "_stub.h",
612 extCoreName = basename ++ ".hcr" }
613 hsc_env = HscEnv { hsc_mode = OneShot,
614 hsc_dflags = dyn_flags',
615 hsc_HPT = emptyHomePackageTable }
619 pcs <- initPersistentCompilerState
620 result <- hscMain hsc_env pcs mod
621 location{ ml_hspp_file=Just input_fn }
628 HscFail pcs -> throwDyn (PhaseFailed "hsc" (ExitFailure 1))
630 HscNoRecomp pcs details iface -> do
631 SysTools.touch "Touching object file" o_file
632 return (Nothing, output_fn)
634 HscRecomp _pcs _details _iface stub_h_exists stub_c_exists
635 _maybe_interpreted_code -> do
638 maybe_stub_o <- compileStub dyn_flags' stub_c_exists
641 Just stub_o -> add v_Ld_inputs stub_o
642 case hscLang dyn_flags of
643 HscNothing -> return (Nothing, output_fn)
644 _ -> return (Just next_phase, output_fn)
646 -----------------------------------------------------------------------------
649 -- we don't support preprocessing .c files (with -E) now. Doing so introduces
650 -- way too many hacks, and I can't say I've ever used it anyway.
652 runPhase cc_phase basename suff input_fn get_output_fn
653 | cc_phase == Cc || cc_phase == HCc
654 = do cc_opts <- getOpts opt_c
655 cmdline_include_paths <- readIORef v_Include_paths
657 split <- readIORef v_Split_object_files
658 mangle <- readIORef v_Do_asm_mangling
660 let hcc = cc_phase == HCc
663 | hcc && mangle = Mangle
666 output_fn <- get_output_fn next_phase
668 -- HC files have the dependent packages stamped into them
669 pkgs <- if hcc then getHCFilePackages input_fn else return []
671 -- add package include paths even if we're just compiling .c
672 -- files; this is the Value Add(TM) that using ghc instead of
674 pkg_include_dirs <- getPackageIncludePath pkgs
675 let include_paths = foldr (\ x xs -> "-I" : x : xs) []
676 (cmdline_include_paths ++ pkg_include_dirs)
678 mangle <- readIORef v_Do_asm_mangling
679 (md_c_flags, md_regd_c_flags) <- machdepCCOpts
683 o2 <- readIORef v_minus_o2_for_C
684 let opt_flag | o2 = "-O2"
687 pkg_extra_cc_opts <- getPackageExtraCcOpts pkgs
689 split_objs <- readIORef v_Split_object_files
690 let split_opt | hcc && split_objs = [ "-DUSE_SPLIT_MARKERS" ]
693 excessPrecision <- readIORef v_Excess_precision
695 -- force the C compiler to interpret this file as C when
696 -- compiling .hc files, by adding the -x c option.
698 | cc_phase == HCc = [ SysTools.Option "-x", SysTools.Option "c"]
701 SysTools.runCc (langopt ++
702 [ SysTools.FileOption "" input_fn
703 , SysTools.Option "-o"
704 , SysTools.FileOption "" output_fn
706 ++ map SysTools.Option (
708 ++ (if cc_phase == HCc && mangle
711 ++ [ verb, "-S", "-Wimplicit", opt_flag ]
712 ++ [ "-D__GLASGOW_HASKELL__="++cProjectVersionInt ]
715 ++ (if excessPrecision then [] else [ "-ffloat-store" ])
720 return (Just next_phase, output_fn)
722 -- ToDo: postprocess the output from gcc
724 -----------------------------------------------------------------------------
727 runPhase Mangle _basename _suff input_fn get_output_fn
728 = do mangler_opts <- getOpts opt_m
729 machdep_opts <- if (prefixMatch "i386" cTARGETPLATFORM)
730 then do n_regs <- dynFlag stolen_x86_regs
731 return [ show n_regs ]
734 split <- readIORef v_Split_object_files
736 | split = SplitMangle
738 output_fn <- get_output_fn next_phase
740 SysTools.runMangle (map SysTools.Option mangler_opts
741 ++ [ SysTools.FileOption "" input_fn
742 , SysTools.FileOption "" output_fn
744 ++ map SysTools.Option machdep_opts)
746 return (Just next_phase, output_fn)
748 -----------------------------------------------------------------------------
751 runPhase SplitMangle _basename _suff input_fn get_output_fn
752 = do -- tmp_pfx is the prefix used for the split .s files
753 -- We also use it as the file to contain the no. of split .s files (sigh)
754 split_s_prefix <- SysTools.newTempName "split"
755 let n_files_fn = split_s_prefix
757 SysTools.runSplit [ SysTools.FileOption "" input_fn
758 , SysTools.FileOption "" split_s_prefix
759 , SysTools.FileOption "" n_files_fn
762 -- Save the number of split files for future references
763 s <- readFile n_files_fn
764 let n_files = read s :: Int
765 writeIORef v_Split_info (split_s_prefix, n_files)
767 -- Remember to delete all these files
768 addFilesToClean [ split_s_prefix ++ "__" ++ show n ++ ".s"
771 return (Just SplitAs, "**splitmangle**") -- we don't use the filename
773 -----------------------------------------------------------------------------
776 runPhase As _basename _suff input_fn get_output_fn
777 = do as_opts <- getOpts opt_a
778 cmdline_include_paths <- readIORef v_Include_paths
780 output_fn <- get_output_fn Ln
782 SysTools.runAs (map SysTools.Option as_opts
783 ++ [ SysTools.Option ("-I" ++ p) | p <- cmdline_include_paths ]
784 ++ [ SysTools.Option "-c"
785 , SysTools.FileOption "" input_fn
786 , SysTools.Option "-o"
787 , SysTools.FileOption "" output_fn
790 return (Just Ln, output_fn)
793 runPhase SplitAs basename _suff _input_fn get_output_fn
794 = do as_opts <- getOpts opt_a
796 (split_s_prefix, n) <- readIORef v_Split_info
798 odir <- readIORef v_Output_dir
799 let real_odir = case odir of
800 Nothing -> basename ++ "_split"
804 = do let input_s = split_s_prefix ++ "__" ++ show n ++ ".s"
805 let output_o = replaceFilenameDirectory
806 (basename ++ "__" ++ show n ++ ".o")
808 real_o <- osuf_ify output_o
809 SysTools.runAs (map SysTools.Option as_opts ++
810 [ SysTools.Option "-c"
811 , SysTools.Option "-o"
812 , SysTools.FileOption "" real_o
813 , SysTools.FileOption "" input_s
816 mapM_ assemble_file [1..n]
818 return (Just Ln, "**split_as**") -- we don't use the output file
821 -----------------------------------------------------------------------------
823 -- Run ilx2il over the ILX output, getting an IL file
825 runPhase Ilx2Il _basename _suff input_fn get_output_fn
826 = do ilx2il_opts <- getOpts opt_I
827 SysTools.runIlx2il (map SysTools.Option ilx2il_opts
828 ++ [ SysTools.Option "--no-add-suffix-to-assembly",
829 SysTools.Option "mscorlib",
830 SysTools.Option "-o",
831 SysTools.FileOption "" output_fn,
832 SysTools.FileOption "" input_fn ])
835 -----------------------------------------------------------------------------
837 -- Run ilasm over the IL, getting a DLL
839 runPhase Ilasm _basename _suff input_fn get_output_fn
840 = do ilasm_opts <- getOpts opt_i
841 SysTools.runIlasm (map SysTools.Option ilasm_opts
842 ++ [ SysTools.Option "/QUIET",
843 SysTools.Option "/DLL",
844 SysTools.FileOption "/OUT=" output_fn,
845 SysTools.FileOption "" input_fn ])
850 -----------------------------------------------------------------------------
851 -- MoveBinary sort-of-phase
852 -- After having produced a binary, move it somewhere else and generate a
853 -- wrapper script calling the binary. Currently, we need this only in
854 -- a parallel way (i.e. in GUM), because PVM expects the binary in a
855 -- central directory.
856 -- This is called from staticLink below, after linking. I haven't made it
857 -- a separate phase to minimise interfering with other modules, and
858 -- we don't need the generality of a phase (MoveBinary is always
859 -- done after linking and makes only sense in a parallel setup) -- HWL
861 runPhase_MoveBinary input_fn
864 pvm_root <- getEnv "PVM_ROOT"
865 pvm_arch <- getEnv "PVM_ARCH"
867 pvm_executable_base = "=" ++ input_fn
868 pvm_executable = pvm_root ++ "/bin/" ++ pvm_arch ++ "/" ++ pvm_executable_base
869 -- nuke old binary; maybe use configur'ed names for cp and rm?
870 system ("rm -f " ++ pvm_executable)
871 -- move the newly created binary into PVM land
872 system ("cp -p " ++ input_fn ++ " " ++ pvm_executable)
873 -- generate a wrapper script for running a parallel prg under PVM
874 writeFile input_fn (mk_pvm_wrapper_script pvm_executable pvm_executable_base sysMan)
877 -- generates a Perl skript starting a parallel prg under PVM
878 mk_pvm_wrapper_script :: String -> String -> String -> String
879 mk_pvm_wrapper_script pvm_executable pvm_executable_base sysMan = unlines $
881 "eval 'exec perl -S $0 ${1+\"$@\"}'",
882 " if $running_under_some_shell;",
883 "# =!=!=!=!=!=!=!=!=!=!=!",
884 "# This script is automatically generated: DO NOT EDIT!!!",
885 "# Generated by Glasgow Haskell Compiler",
886 "# ngoqvam choHbogh vaj' vIHoHnISbej !!!!",
888 "$pvm_executable = '" ++ pvm_executable ++ "';",
889 "$pvm_executable_base = '" ++ pvm_executable_base ++ "';",
890 "$SysMan = '" ++ sysMan ++ "';",
892 {- ToDo: add the magical shortcuts again iff we actually use them -- HWL
893 "# first, some magical shortcuts to run "commands" on the binary",
894 "# (which is hidden)",
895 "if ($#ARGV == 1 && $ARGV[0] eq '+RTS' && $ARGV[1] =~ /^--((size|file|strip|rm|nm).*)/ ) {",
896 " local($cmd) = $1;",
897 " system("$cmd $pvm_executable");",
898 " exit(0); # all done",
901 "# Now, run the real binary; process the args first",
902 "$ENV{'PE'} = $pvm_executable_base;", -- ++ pvm_executable_base,
904 "$nprocessors = 0; # the default: as many PEs as machines in PVM config",
905 "@nonPVM_args = ();",
908 "args: while ($a = shift(@ARGV)) {",
909 " if ( $a eq '+RTS' ) {",
910 " $in_RTS_args = 1;",
911 " } elsif ( $a eq '-RTS' ) {",
912 " $in_RTS_args = 0;",
914 " if ( $a eq '-d' && $in_RTS_args ) {",
916 " } elsif ( $a =~ /^-qN(\\d+)/ && $in_RTS_args ) {",
917 " $nprocessors = $1;",
918 " } elsif ( $a =~ /^-qp(\\d+)/ && $in_RTS_args ) {",
919 " $nprocessors = $1;",
921 " push(@nonPVM_args, $a);",
925 "local($return_val) = 0;",
926 "# Start the parallel execution by calling SysMan",
927 "system(\"$SysMan $debug $pvm_executable $nprocessors @nonPVM_args\");",
929 "# ToDo: fix race condition moving files and flushing them!!",
930 "system(\"cp $ENV{'HOME'}/$pvm_executable_base.???.gr .\") if -f \"$ENV{'HOME'}/$pvm_executable_base.002.gr\";",
934 -----------------------------------------------------------------------------
935 -- Complain about non-dynamic flags in OPTIONS pragmas
937 checkProcessArgsResult flags basename suff
938 = do when (notNull flags) (throwDyn (ProgramError (
939 showSDoc (hang (text basename <> text ('.':suff) <> char ':')
940 4 (text "unknown flags in {-# OPTIONS #-} pragma:" <+>
941 hsep (map text flags)))
944 -----------------------------------------------------------------------------
945 -- Look for the /* GHC_PACKAGES ... */ comment at the top of a .hc file
947 getHCFilePackages :: FilePath -> IO [PackageName]
948 getHCFilePackages filename =
949 EXCEPTION.bracket (openFile filename ReadMode) hClose $ \h -> do
952 '/':'*':' ':'G':'H':'C':'_':'P':'A':'C':'K':'A':'G':'E':'S':rest ->
953 return (map mkPackageName (words rest))
957 -----------------------------------------------------------------------------
958 -- Static linking, of .o files
960 -- The list of packages passed to link is the list of packages on
961 -- which this program depends, as discovered by the compilation
962 -- manager. It is combined with the list of packages that the user
963 -- specifies on the command line with -package flags.
965 -- In one-shot linking mode, we can't discover the package
966 -- dependencies (because we haven't actually done any compilation or
967 -- read any interface files), so the user must explicitly specify all
970 staticLink :: [FilePath] -> [PackageName] -> IO ()
971 staticLink o_files dep_packages = do
973 static <- readIORef v_Static
974 no_hs_main <- readIORef v_NoHsMain
976 -- get the full list of packages to link with, by combining the
977 -- explicit packages with the auto packages and all of their
978 -- dependencies, and eliminating duplicates.
980 o_file <- readIORef v_Output_file
981 let output_fn = case o_file of { Just s -> s; Nothing -> "a.out"; }
983 pkg_lib_paths <- getPackageLibraryPath dep_packages
984 let pkg_lib_path_opts = map ("-L"++) pkg_lib_paths
986 lib_paths <- readIORef v_Library_paths
987 let lib_path_opts = map ("-L"++) lib_paths
989 pkg_link_opts <- getPackageLinkOpts dep_packages
991 #ifdef darwin_TARGET_OS
992 pkg_framework_paths <- getPackageFrameworkPath dep_packages
993 let pkg_framework_path_opts = map ("-F"++) pkg_framework_paths
995 framework_paths <- readIORef v_Framework_paths
996 let framework_path_opts = map ("-F"++) framework_paths
998 pkg_frameworks <- getPackageFrameworks dep_packages
999 let pkg_framework_opts = concat [ ["-framework", fw] | fw <- pkg_frameworks ]
1001 frameworks <- readIORef v_Cmdline_frameworks
1002 let framework_opts = concat [ ["-framework", fw] | fw <- reverse frameworks ]
1003 -- reverse because they're added in reverse order from the cmd line
1006 -- probably _stub.o files
1007 extra_ld_inputs <- readIORef v_Ld_inputs
1009 -- opts from -optl-<blah> (including -l<blah> options)
1010 extra_ld_opts <- getStaticOpts v_Opt_l
1012 [rts_pkg, std_pkg] <- getPackageDetails [rtsPackage, basePackage]
1014 let extra_os = if static || no_hs_main
1016 else [ head (library_dirs rts_pkg) ++ "/Main.dll_o",
1017 head (library_dirs std_pkg) ++ "/PrelMain.dll_o" ]
1019 (md_c_flags, _) <- machdepCCOpts
1020 SysTools.runLink ( [ SysTools.Option verb
1021 , SysTools.Option "-o"
1022 , SysTools.FileOption "" output_fn
1024 ++ map SysTools.Option (
1031 #ifdef darwin_TARGET_OS
1032 ++ framework_path_opts
1035 ++ pkg_lib_path_opts
1037 #ifdef darwin_TARGET_OS
1038 ++ pkg_framework_path_opts
1039 ++ pkg_framework_opts
1041 ++ if static && not no_hs_main then
1042 [ "-u", prefixUnderscore "Main_zdmain_closure"]
1045 -- parallel only: move binary to another dir -- HWL
1046 ways_ <- readIORef v_Ways
1047 when (WayPar `elem` ways_)
1048 (do success <- runPhase_MoveBinary output_fn
1049 if success then return ()
1050 else throwDyn (InstallationError ("cannot move binary to PVM dir")))
1052 -----------------------------------------------------------------------------
1053 -- Making a DLL (only for Win32)
1055 doMkDLL :: [String] -> IO ()
1056 doMkDLL o_files = do
1058 static <- readIORef v_Static
1059 no_hs_main <- readIORef v_NoHsMain
1061 o_file <- readIORef v_Output_file
1062 let output_fn = case o_file of { Just s -> s; Nothing -> "HSdll.dll"; }
1064 pkg_lib_paths <- getPackageLibraryPath []
1065 let pkg_lib_path_opts = map ("-L"++) pkg_lib_paths
1067 lib_paths <- readIORef v_Library_paths
1068 let lib_path_opts = map ("-L"++) lib_paths
1070 pkg_link_opts <- getPackageLinkOpts []
1072 -- probably _stub.o files
1073 extra_ld_inputs <- readIORef v_Ld_inputs
1075 -- opts from -optdll-<blah>
1076 extra_ld_opts <- getStaticOpts v_Opt_dll
1078 [rts_pkg, std_pkg] <- getPackageDetails [rtsPackage, basePackage]
1080 let extra_os = if static || no_hs_main
1082 else [ head (library_dirs rts_pkg) ++ "/Main.dll_o",
1083 head (library_dirs std_pkg) ++ "/PrelMain.dll_o" ]
1085 (md_c_flags, _) <- machdepCCOpts
1087 ([ SysTools.Option verb
1088 , SysTools.Option "-o"
1089 , SysTools.FileOption "" output_fn
1091 ++ map SysTools.Option (
1095 ++ [ "--target=i386-mingw32" ]
1099 ++ pkg_lib_path_opts
1101 ++ (if "--def" `elem` (concatMap words extra_ld_opts)
1103 else [ "--export-all" ])