1 -----------------------------------------------------------------------------
2 -- $Id: DriverPipeline.hs,v 1.36 2000/12/04 16:42:14 rrt Exp $
6 -- (c) Simon Marlow 2000
8 -----------------------------------------------------------------------------
10 module DriverPipeline (
12 -- interfaces for the batch-mode driver
13 GhcMode(..), getGhcMode, v_GhcMode,
14 genPipeline, runPipeline,
16 -- interfaces for the compilation manager (interpreted/batch-mode)
17 preprocess, compile, CompResult(..),
19 -- batch-mode linking interface
23 #include "HsVersions.h"
25 import CmStaticInfo ( GhciMode(..) )
43 import Time ( getClockTime )
53 -----------------------------------------------------------------------------
54 -- GHC modes of operation
57 = DoMkDependHS -- ghc -M
58 | DoMkDLL -- ghc -mk-dll
59 | StopBefore Phase -- ghc -E | -C | -S | -c
60 | DoMake -- ghc --make
61 | DoInteractive -- ghc --interactive
62 | DoLink -- [ the default ]
65 GLOBAL_VAR(v_GhcMode, error "todo", GhcMode)
67 modeFlag :: String -> Maybe GhcMode
68 modeFlag "-M" = Just $ DoMkDependHS
69 modeFlag "-E" = Just $ StopBefore Hsc
70 modeFlag "-C" = Just $ StopBefore HCc
71 modeFlag "-S" = Just $ StopBefore As
72 modeFlag "-c" = Just $ StopBefore Ln
73 modeFlag "--make" = Just $ DoMake
74 modeFlag "--interactive" = Just $ DoInteractive
77 getGhcMode :: [String]
78 -> IO ( [String] -- rest of command line
80 , String -- "GhcMode" flag
83 = case my_partition modeFlag flags of
84 ([] , rest) -> return (rest, DoLink, "") -- default is to do linking
85 ([(flag,one)], rest) -> return (rest, one, flag)
88 "only one of the flags -M, -E, -C, -S, -c, --make, --interactive is allowed")
90 -----------------------------------------------------------------------------
93 -- Herein is all the magic about which phases to run in which order, whether
94 -- the intermediate files should be in /tmp or in the current directory,
95 -- what the suffix of the intermediate files should be, etc.
97 -- The following compilation pipeline algorithm is fairly hacky. A
98 -- better way to do this would be to express the whole compilation as a
99 -- data flow DAG, where the nodes are the intermediate files and the
100 -- edges are the compilation phases. This framework would also work
101 -- nicely if a haskell dependency generator was included in the
104 -- It would also deal much more cleanly with compilation phases that
105 -- generate multiple intermediates, (eg. hsc generates .hc, .hi, and
106 -- possibly stub files), where some of the output files need to be
107 -- processed further (eg. the stub files need to be compiled by the C
110 -- A cool thing to do would then be to execute the data flow graph
111 -- concurrently, automatically taking advantage of extra processors on
112 -- the host machine. For example, when compiling two Haskell files
113 -- where one depends on the other, the data flow graph would determine
114 -- that the C compiler from the first compilation can be overlapped
115 -- with the hsc compilation for the second file.
117 data IntermediateFileType
123 :: GhcMode -- when to stop
124 -> String -- "stop after" flag (for error messages)
125 -> Bool -- True => output is persistent
126 -> HscLang -- preferred output language for hsc
127 -> String -- original filename
128 -> IO [ -- list of phases to run for this file
130 IntermediateFileType, -- keep the output from this phase?
131 String) -- output file suffix
134 genPipeline todo stop_flag persistent_output lang filename
136 split <- readIORef v_Split_object_files
137 mangle <- readIORef v_Do_asm_mangling
138 keep_hc <- readIORef v_Keep_hc_files
139 keep_raw_s <- readIORef v_Keep_raw_s_files
140 keep_s <- readIORef v_Keep_s_files
141 osuf <- readIORef v_Object_suf
144 ----------- ----- ---- --- -- -- - - -
145 (_basename, suffix) = splitFilename filename
147 start_phase = startPhase suffix
149 haskellish = haskellish_suffix suffix
150 cish = cish_suffix suffix
152 -- for a .hc file we need to force lang to HscC
153 real_lang | start_phase == HCc = HscC
157 ----------- ----- ---- --- -- -- - - -
159 | todo == DoMkDependHS = [ Unlit, Cpp, MkDependHS ]
163 HscC | split && mangle -> [ Unlit, Cpp, Hsc, HCc, Mangle,
164 SplitMangle, SplitAs ]
165 | mangle -> [ Unlit, Cpp, Hsc, HCc, Mangle, As ]
167 | otherwise -> [ Unlit, Cpp, Hsc, HCc, As ]
169 HscAsm | split -> [ Unlit, Cpp, Hsc, SplitMangle, SplitAs ]
170 | otherwise -> [ Unlit, Cpp, Hsc, As ]
172 HscJava | split -> not_valid
173 | otherwise -> error "not implemented: compiling via Java"
177 | otherwise = [ ] -- just pass this file through to the linker
179 -- ToDo: this is somewhat cryptic
180 not_valid = throwDyn (OtherError ("invalid option combination"))
181 ----------- ----- ---- --- -- -- - - -
183 -- this shouldn't happen.
184 if start_phase /= Ln && start_phase `notElem` pipeline
185 then throwDyn (OtherError ("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.
194 && phase `notElem` pipeline
195 && not (phase == As && SplitAs `elem` pipeline)) $
197 ("flag " ++ stop_flag
198 ++ " is incompatible with source file `" ++ filename ++ "'"))
202 ----------- ----- ---- --- -- -- - - -
203 myPhaseInputExt Ln = case osuf of Nothing -> phaseInputExt Ln
205 myPhaseInputExt other = phaseInputExt other
208 :: [Phase] -- raw pipeline
209 -> Phase -- phase to stop before
210 -> [(Phase, IntermediateFileType, String{-file extension-})]
211 annotatePipeline [] _ = []
212 annotatePipeline (Ln:_) _ = []
213 annotatePipeline (phase:next_phase:ps) stop =
214 (phase, keep_this_output, myPhaseInputExt next_phase)
215 : annotatePipeline (next_phase:ps) stop
219 = if persistent_output then Persistent else Temporary
223 Mangle | keep_raw_s -> Persistent
224 As | keep_s -> Persistent
225 HCc | keep_hc -> Persistent
228 -- add information about output files to the pipeline
229 -- the suffix on an output file is determined by the next phase
230 -- in the pipeline, so we add linking to the end of the pipeline
231 -- to force the output from the final phase to be a .o file.
232 stop_phase = case todo of StopBefore phase -> phase
235 annotated_pipeline = annotatePipeline (pipeline ++ [ Ln ]) stop_phase
237 phase_ne p (p1,_,_) = (p1 /= p)
238 ----------- ----- ---- --- -- -- - - -
241 dropWhile (phase_ne start_phase) .
242 foldr (\p ps -> if phase_ne stop_phase p then p:ps else []) []
247 :: [ (Phase, IntermediateFileType, String) ] -- phases to run
248 -> String -- input file
249 -> Bool -- doing linking afterward?
250 -> Bool -- take into account -o when generating output?
251 -> IO String -- return final filename
253 runPipeline pipeline input_fn do_linking use_ofile
254 = pipeLoop pipeline input_fn do_linking use_ofile basename suffix
255 where (basename, suffix) = splitFilename input_fn
257 pipeLoop [] input_fn _ _ _ _ = return input_fn
258 pipeLoop ((phase, keep, o_suffix):phases)
259 input_fn do_linking use_ofile orig_basename orig_suffix
262 output_fn <- outputFileName (null phases) keep o_suffix
264 carry_on <- run_phase phase orig_basename orig_suffix input_fn output_fn
265 -- sometimes we bail out early, eg. when the compiler's recompilation
266 -- checker has determined that recompilation isn't necessary.
268 then do let (_,keep,final_suffix) = last phases
269 ofile <- outputFileName True keep final_suffix
271 else do -- carry on ...
273 pipeLoop phases output_fn do_linking use_ofile orig_basename orig_suffix
276 outputFileName last_phase keep suffix
277 = do o_file <- readIORef v_Output_file
278 if last_phase && not do_linking && use_ofile && isJust o_file
281 Nothing -> error "outputFileName"
282 else if keep == Persistent
283 then odir_ify (orig_basename ++ '.':suffix)
284 else newTempName suffix
286 -------------------------------------------------------------------------------
289 run_phase Unlit _basename _suff input_fn output_fn
290 = do unlit <- readIORef v_Pgm_L
291 unlit_flags <- getOpts opt_L
292 runSomething "Literate pre-processor"
293 ("echo '# 1 \"" ++input_fn++"\"' > "++output_fn++" && "
294 ++ unlit ++ ' ':input_fn ++ " - >> " ++ output_fn)
297 -------------------------------------------------------------------------------
300 run_phase Cpp basename suff input_fn output_fn
301 = do src_opts <- getOptionsFromSource input_fn
302 unhandled_flags <- processArgs dynamic_flags src_opts []
304 when (not (null unhandled_flags))
305 (throwDyn (OtherError (
306 basename ++ "." ++ suff
307 ++ ": static flags are not allowed in {-# OPTIONS #-} pragmas:\n\t"
308 ++ unwords unhandled_flags)) (ExitFailure 1))
310 do_cpp <- readState cpp_flag
313 cpp <- readIORef v_Pgm_P
314 hscpp_opts <- getOpts opt_P
315 hs_src_cpp_opts <- readIORef v_Hs_source_cpp_opts
317 cmdline_include_paths <- readIORef v_Include_paths
318 pkg_include_dirs <- getPackageIncludePath
319 let include_paths = map (\p -> "-I"++p) (cmdline_include_paths
324 runSomething "C pre-processor"
326 (["echo '{-# LINE 1 \"" ++ input_fn ++ "\" -}'", ">", output_fn, "&&",
331 ++ [ "-x", "c", input_fn, ">>", output_fn ]
334 runSomething "Ineffective C pre-processor"
335 ("echo '{-# LINE 1 \"" ++ input_fn ++ "\" -}' > "
336 ++ output_fn ++ " && cat " ++ input_fn
337 ++ " >> " ++ output_fn)
340 -----------------------------------------------------------------------------
343 run_phase MkDependHS basename suff input_fn _output_fn = do
344 src <- readFile input_fn
345 let (import_sources, import_normals, module_name) = getImports src
347 deps_sources <- mapM (findDependency True basename) import_sources
348 deps_normals <- mapM (findDependency False basename) import_normals
349 let deps = deps_sources ++ deps_normals
351 osuf_opt <- readIORef v_Object_suf
352 let osuf = case osuf_opt of
353 Nothing -> phaseInputExt Ln
356 extra_suffixes <- readIORef v_Dep_suffixes
357 let suffixes = osuf : map (++ ('_':osuf)) extra_suffixes
358 ofiles = map (\suf -> basename ++ '.':suf) suffixes
360 objs <- mapM odir_ify ofiles
362 hdl <- readIORef v_Dep_tmp_hdl
364 -- std dependency of the object(s) on the source file
365 hPutStrLn hdl (unwords objs ++ " : " ++ basename ++ '.':suff)
367 let genDep (dep, False {- not an hi file -}) =
368 hPutStrLn hdl (unwords objs ++ " : " ++ dep)
369 genDep (dep, True {- is an hi file -}) = do
370 hisuf <- readIORef v_Hi_suf
371 let dep_base = remove_suffix '.' dep
372 deps = (dep_base ++ hisuf)
373 : map (\suf -> dep_base ++ suf ++ '_':hisuf) extra_suffixes
374 -- length objs should be == length deps
375 sequence_ (zipWith (\o d -> hPutStrLn hdl (o ++ " : " ++ d)) objs deps)
377 mapM genDep [ d | Just d <- deps ]
381 -- add the lines to dep_makefile:
385 -- if the dependency is on something other than a .hi file:
386 -- this.o this.p_o ... : dep
388 -- if the import is {-# SOURCE #-}
389 -- this.o this.p_o ... : dep.hi-boot[-$vers]
392 -- this.o ... : dep.hi
393 -- this.p_o ... : dep.p_hi
396 -- (where .o is $osuf, and the other suffixes come from
397 -- the cmdline -s options).
399 -----------------------------------------------------------------------------
402 -- Compilation of a single module, in "legacy" mode (_not_ under
403 -- the direction of the compilation manager).
404 run_phase Hsc basename suff input_fn output_fn
407 -- we add the current directory (i.e. the directory in which
408 -- the .hs files resides) to the import path, since this is
409 -- what gcc does, and it's probably what you want.
410 let current_dir = getdir basename
412 paths <- readIORef v_Include_paths
413 writeIORef v_Include_paths (current_dir : paths)
415 -- figure out where to put the .hi file
416 ohi <- readIORef v_Output_hi
417 hisuf <- readIORef v_Hi_suf
418 let hifile = case ohi of
419 Nothing -> basename ++ '.':hisuf
422 -- figure out if the source has changed, for recompilation avoidance.
423 -- only do this if we're eventually going to generate a .o file.
424 -- (ToDo: do when generating .hc files too?)
426 -- Setting source_unchanged to True means that M.o seems
427 -- to be up to date wrt M.hs; so no need to recompile unless imports have
428 -- changed (which the compiler itself figures out).
429 -- Setting source_unchanged to False tells the compiler that M.o is out of
430 -- date wrt M.hs (or M.o doesn't exist) so we must recompile regardless.
431 do_recomp <- readIORef v_Recomp
432 todo <- readIORef v_GhcMode
433 o_file' <- odir_ify (basename ++ '.':phase_input_ext Ln)
434 o_file <- osuf_ify o_file'
436 if not (do_recomp && ( todo == DoLink || todo == StopBefore Ln ))
438 else do t1 <- getModificationTime (basename ++ '.':suff)
439 o_file_exists <- doesFileExist o_file
441 then return False -- Need to recompile
442 else do t2 <- getModificationTime o_file
447 -- build a ModuleLocation to pass to hscMain.
448 let location = ModuleLocation {
449 ml_hs_file = Nothing,
450 ml_hspp_file = Just input_fn,
451 ml_hi_file = Just hifile,
452 ml_obj_file = Just o_file
456 dyn_flags <- readIORef v_DynFlags
459 pcs <- initPersistentCompilerState
460 result <- hscMain OneShot
461 dyn_flags{ hscOutName = output_fn }
465 emptyModuleEnv -- HomeSymbolTable
466 emptyModuleEnv -- HomeIfaceTable
471 HscFail pcs -> throwDyn (PhaseFailed "hsc" (ExitFailure 1));
473 HscNoRecomp pcs details iface -> return False;
475 HscRecomp pcs details iface maybe_stub_h maybe_stub_c
476 _maybe_interpreted_code -> do
479 maybe_stub_o <- dealWithStubs basename maybe_stub_h maybe_stub_c
482 Just stub_o -> add v_Ld_inputs stub_o
487 -----------------------------------------------------------------------------
490 -- we don't support preprocessing .c files (with -E) now. Doing so introduces
491 -- way too many hacks, and I can't say I've ever used it anyway.
493 run_phase cc_phase _basename _suff input_fn output_fn
494 | cc_phase == Cc || cc_phase == HCc
495 = do cc <- readIORef v_Pgm_c
496 cc_opts <- (getOpts opt_c)
497 cmdline_include_dirs <- readIORef v_Include_paths
499 let hcc = cc_phase == HCc
501 -- add package include paths even if we're just compiling
502 -- .c files; this is the Value Add(TM) that using
503 -- ghc instead of gcc gives you :)
504 pkg_include_dirs <- getPackageIncludePath
505 let include_paths = map (\p -> "-I"++p) (cmdline_include_dirs
508 c_includes <- getPackageCIncludes
509 cmdline_includes <- readState cmdline_hc_includes -- -#include options
511 let cc_injects | hcc = unlines (map mk_include
512 (c_includes ++ reverse cmdline_includes))
516 '"':_{-"-} -> "#include "++h_file
517 '<':_ -> "#include "++h_file
518 _ -> "#include \""++h_file++"\""
520 cc_help <- newTempName "c"
521 h <- openFile cc_help WriteMode
523 hPutStrLn h ("#include \"" ++ input_fn ++ "\"\n")
526 ccout <- newTempName "ccout"
528 mangle <- readIORef v_Do_asm_mangling
529 (md_c_flags, md_regd_c_flags) <- machdepCCOpts
533 o2 <- readIORef v_minus_o2_for_C
534 let opt_flag | o2 = "-O2"
537 pkg_extra_cc_opts <- getPackageExtraCcOpts
539 split_objs <- readIORef v_Split_object_files
540 let split_opt | hcc && split_objs = [ "-DUSE_SPLIT_MARKERS" ]
543 excessPrecision <- readIORef v_Excess_precision
545 runSomething "C Compiler"
546 (unwords ([ cc, "-x", "c", cc_help, "-o", output_fn ]
548 ++ (if cc_phase == HCc && mangle
551 ++ [ verb, "-S", "-Wimplicit", opt_flag ]
552 ++ [ "-D__GLASGOW_HASKELL__="++cProjectVersionInt ]
555 ++ (if excessPrecision then [] else [ "-ffloat-store" ])
562 -- ToDo: postprocess the output from gcc
564 -----------------------------------------------------------------------------
567 run_phase Mangle _basename _suff input_fn output_fn
568 = do mangler <- readIORef v_Pgm_m
569 mangler_opts <- getOpts opt_m
571 if (prefixMatch "i386" cTARGETPLATFORM)
572 then do n_regs <- readState stolen_x86_regs
573 return [ show n_regs ]
575 runSomething "Assembly Mangler"
578 ++ [ input_fn, output_fn ]
583 -----------------------------------------------------------------------------
586 run_phase SplitMangle _basename _suff input_fn _output_fn
587 = do splitter <- readIORef v_Pgm_s
589 -- this is the prefix used for the split .s files
590 tmp_pfx <- readIORef v_TmpDir
592 let split_s_prefix = tmp_pfx ++ "/ghc" ++ show x
593 writeIORef v_Split_prefix split_s_prefix
594 addFilesToClean [split_s_prefix ++ "__*"] -- d:-)
596 -- allocate a tmp file to put the no. of split .s files in (sigh)
597 n_files <- newTempName "n_files"
599 runSomething "Split Assembly File"
606 -- save the number of split files for future references
607 s <- readFile n_files
608 let n = read s :: Int
609 writeIORef v_N_split_files n
612 -----------------------------------------------------------------------------
615 run_phase As _basename _suff input_fn output_fn
616 = do as <- readIORef v_Pgm_a
617 as_opts <- getOpts opt_a
619 cmdline_include_paths <- readIORef v_Include_paths
620 let cmdline_include_flags = map (\p -> "-I"++p) cmdline_include_paths
621 runSomething "Assembler"
622 (unwords (as : as_opts
623 ++ cmdline_include_flags
624 ++ [ "-c", input_fn, "-o", output_fn ]
628 run_phase SplitAs basename _suff _input_fn _output_fn
629 = do as <- readIORef v_Pgm_a
630 as_opts <- getOpts opt_a
632 split_s_prefix <- readIORef v_Split_prefix
633 n <- readIORef v_N_split_files
635 odir <- readIORef v_Output_dir
636 let real_odir = case odir of
640 let assemble_file n = do
641 let input_s = split_s_prefix ++ "__" ++ show n ++ ".s"
642 let output_o = newdir real_odir
643 (basename ++ "__" ++ show n ++ ".o")
644 real_o <- osuf_ify output_o
645 runSomething "Assembler"
646 (unwords (as : as_opts
647 ++ [ "-c", "-o", real_o, input_s ]
650 mapM_ assemble_file [1..n]
653 -----------------------------------------------------------------------------
656 doLink :: [String] -> IO ()
658 ln <- readIORef v_Pgm_l
660 static <- readIORef v_Static
661 let imp = if static then "" else "_imp"
662 no_hs_main <- readIORef v_NoHsMain
664 o_file <- readIORef v_Output_file
665 let output_fn = case o_file of { Just s -> s; Nothing -> "a.out"; }
667 pkg_lib_paths <- getPackageLibraryPath
668 let pkg_lib_path_opts = map ("-L"++) pkg_lib_paths
670 lib_paths <- readIORef v_Library_paths
671 let lib_path_opts = map ("-L"++) lib_paths
673 pkg_libs <- getPackageLibraries
674 let pkg_lib_opts = map (\lib -> "-l" ++ lib ++ imp) pkg_libs
676 libs <- readIORef v_Cmdline_libraries
677 let lib_opts = map ("-l"++) (reverse libs)
678 -- reverse because they're added in reverse order from the cmd line
680 pkg_extra_ld_opts <- getPackageExtraLdOpts
682 -- probably _stub.o files
683 extra_ld_inputs <- readIORef v_Ld_inputs
685 -- opts from -optl-<blah>
686 extra_ld_opts <- getStaticOpts v_Opt_l
688 rts_pkg <- getPackageDetails ["rts"]
689 std_pkg <- getPackageDetails ["std"]
690 #ifdef mingw32_TARGET_OS
691 let extra_os = if static || no_hs_main
693 -- else [ head (lib_paths (head rts_pkg)) ++ "/Main.dll_o",
694 -- head (lib_paths (head std_pkg)) ++ "/PrelMain.dll_o" ]
697 (md_c_flags, _) <- machdepCCOpts
698 runSomething "Linker"
700 ([ ln, verb, "-o", output_fn ]
703 #ifdef mingw32_TARGET_OS
713 #ifdef mingw32_TARGET_OS
714 ++ if static then [ "-u _PrelMain_mainIO_closure" , "-u ___init_PrelMain"] else []
716 ++ [ "-u PrelMain_mainIO_closure" , "-u __init_PrelMain"]
721 -----------------------------------------------------------------------------
722 -- Just preprocess a file, put the result in a temp. file (used by the
723 -- compilation manager during the summary phase).
725 preprocess :: FilePath -> IO FilePath
726 preprocess filename =
727 ASSERT(haskellish_file filename)
728 do init_driver_state <- readIORef v_InitDriverState
729 writeIORef v_Driver_state init_driver_state
731 pipeline <- genPipeline (StopBefore Hsc) ("preprocess") False
732 defaultHscLang filename
733 runPipeline pipeline filename False{-no linking-} False{-no -o flag-}
736 -----------------------------------------------------------------------------
737 -- Compile a single module, under the control of the compilation manager.
739 -- This is the interface between the compilation manager and the
740 -- compiler proper (hsc), where we deal with tedious details like
741 -- reading the OPTIONS pragma from the source file, and passing the
742 -- output of hsc through the C compiler.
744 -- The driver sits between 'compile' and 'hscMain', translating calls
745 -- to the former into calls to the latter, and results from the latter
746 -- into results from the former. It does things like preprocessing
747 -- the .hs file if necessary, and compiling up the .stub_c files to
748 -- generate Linkables.
750 -- NB. No old interface can also mean that the source has changed.
752 compile :: GhciMode -- distinguish batch from interactive
753 -> ModSummary -- summary, including source
754 -> Bool -- source unchanged?
755 -> Maybe ModIface -- old interface, if available
756 -> HomeSymbolTable -- for home module ModDetails
757 -> HomeIfaceTable -- for home module Ifaces
758 -> PersistentCompilerState -- persistent compiler state
762 = CompOK PersistentCompilerState -- updated PCS
763 ModDetails -- new details (HST additions)
764 ModIface -- new iface (HIT additions)
766 -- new code; Nothing => compilation was not reqd
767 -- (old code is still valid)
769 | CompErrs PersistentCompilerState -- updated PCS
772 compile ghci_mode summary source_unchanged old_iface hst hit pcs = do
773 init_dyn_flags <- readIORef v_InitDynFlags
774 writeIORef v_DynFlags init_dyn_flags
775 init_driver_state <- readIORef v_InitDriverState
776 writeIORef v_Driver_state init_driver_state
778 showPass init_dyn_flags
779 (showSDoc (text "Compiling" <+> ppr (name_of_summary summary)))
781 let verb = verbosity init_dyn_flags
782 let location = ms_location summary
783 let input_fn = unJust "compile:hs" (ml_hs_file location)
784 let input_fnpp = unJust "compile:hspp" (ml_hspp_file location)
786 when (verb >= 2) (hPutStrLn stderr ("compile: input file " ++ input_fnpp))
788 opts <- getOptionsFromSource input_fnpp
789 processArgs dynamic_flags opts []
790 dyn_flags <- readIORef v_DynFlags
792 let hsc_lang = hscLang dyn_flags
793 output_fn <- case hsc_lang of
794 HscAsm -> newTempName (phaseInputExt As)
795 HscC -> newTempName (phaseInputExt HCc)
796 HscJava -> newTempName "java" -- ToDo
797 HscInterpreted -> return (error "no output file")
800 hsc_result <- hscMain ghci_mode dyn_flags{ hscOutName = output_fn }
802 location old_iface hst hit pcs
805 HscFail pcs -> return (CompErrs pcs)
807 HscNoRecomp pcs details iface -> return (CompOK pcs details iface Nothing)
809 HscRecomp pcs details iface
810 maybe_stub_h maybe_stub_c maybe_interpreted_code -> do
812 let (basename, _) = splitFilename input_fn
813 maybe_stub_o <- dealWithStubs basename maybe_stub_h maybe_stub_c
814 let stub_unlinked = case maybe_stub_o of
816 Just stub_o -> [ DotO stub_o ]
818 (hs_unlinked, unlinked_time) <-
821 -- in interpreted mode, just return the compiled code
822 -- as our "unlinked" object.
824 case maybe_interpreted_code of
825 Just (code,itbl_env) -> do tm <- getClockTime
826 return ([Trees code itbl_env], tm)
827 Nothing -> panic "compile: no interpreted code"
829 -- we're in batch mode: finish the compilation pipeline.
830 _other -> do pipe <- genPipeline (StopBefore Ln) "" True
832 -- runPipeline takes input_fn so it can split off
833 -- the base name and use it as the base of
834 -- the output object file.
835 let (basename, suffix) = splitFilename input_fn
836 o_file <- pipeLoop pipe output_fn False False
838 o_time <- getModificationTime o_file
839 return ([DotO o_file], o_time)
841 let linkable = LM unlinked_time (moduleName (ms_mod summary))
842 (hs_unlinked ++ stub_unlinked)
844 return (CompOK pcs details iface (Just linkable))
847 -----------------------------------------------------------------------------
848 -- stub .h and .c files (for foreign export support)
850 dealWithStubs basename maybe_stub_h maybe_stub_c
852 = do let stub_h = basename ++ "_stub.h"
853 let stub_c = basename ++ "_stub.c"
855 -- copy the .stub_h file into the current dir if necessary
858 Just tmp_stub_h -> do
859 runSomething "Copy stub .h file"
860 ("cp " ++ tmp_stub_h ++ ' ':stub_h)
862 -- #include <..._stub.h> in .hc file
863 addCmdlineHCInclude tmp_stub_h -- hack
865 -- copy the .stub_c file into the current dir, and compile it, if necessary
867 Nothing -> return Nothing
868 Just tmp_stub_c -> do -- copy the _stub.c file into the current dir
869 runSomething "Copy stub .c file"
871 "rm -f", stub_c, "&&",
872 "echo \'#include \"Stg.h\"\n#include \""++stub_h++"\"\' >"++stub_c, " &&",
873 "cat", tmp_stub_c, ">> ", stub_c
876 -- compile the _stub.c file w/ gcc
877 pipeline <- genPipeline (StopBefore Ln) "" True
878 defaultHscLang stub_c
879 stub_o <- runPipeline pipeline stub_c False{-no linking-}
880 False{-no -o option-}