1 -----------------------------------------------------------------------------
2 -- $Id: DriverPipeline.hs,v 1.4 2000/10/17 11:50:20 simonmar 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)
19 -- batch-mode linking interface
23 #include "HsVersions.h"
54 -----------------------------------------------------------------------------
55 -- GHC modes of operation
58 = DoMkDependHS -- ghc -M
59 | DoMkDLL -- ghc -mk-dll
60 | StopBefore Phase -- ghc -E | -C | -S | -c
61 | DoMake -- ghc --make
62 | DoInteractive -- ghc --interactive
63 | DoLink -- [ the default ]
66 GLOBAL_VAR(v_GhcMode, error "todo", GhcMode)
68 modeFlag :: String -> Maybe GhcMode
69 modeFlag "-M" = Just $ DoMkDependHS
70 modeFlag "-E" = Just $ StopBefore Hsc
71 modeFlag "-C" = Just $ StopBefore HCc
72 modeFlag "-S" = Just $ StopBefore As
73 modeFlag "-c" = Just $ StopBefore Ln
74 modeFlag "--make" = Just $ DoMake
75 modeFlag "--interactive" = Just $ DoInteractive
78 getGhcMode :: [String]
79 -> IO ( [String] -- rest of command line
81 , String -- "GhcMode" flag
84 = case my_partition modeFlag flags of
85 ([] , rest) -> return (rest, DoLink, "") -- default is to do linking
86 ([(flag,one)], rest) -> return (rest, one, flag)
89 "only one of the flags -M, -E, -C, -S, -c, --make, --interactive is allowed")
91 -----------------------------------------------------------------------------
94 -- Herein is all the magic about which phases to run in which order, whether
95 -- the intermediate files should be in /tmp or in the current directory,
96 -- what the suffix of the intermediate files should be, etc.
98 -- The following compilation pipeline algorithm is fairly hacky. A
99 -- better way to do this would be to express the whole comilation as a
100 -- data flow DAG, where the nodes are the intermediate files and the
101 -- edges are the compilation phases. This framework would also work
102 -- nicely if a haskell dependency generator was included in the
105 -- It would also deal much more cleanly with compilation phases that
106 -- generate multiple intermediates, (eg. hsc generates .hc, .hi, and
107 -- possibly stub files), where some of the output files need to be
108 -- processed further (eg. the stub files need to be compiled by the C
111 -- A cool thing to do would then be to execute the data flow graph
112 -- concurrently, automatically taking advantage of extra processors on
113 -- the host machine. For example, when compiling two Haskell files
114 -- where one depends on the other, the data flow graph would determine
115 -- that the C compiler from the first comilation can be overlapped
116 -- with the hsc comilation for the second file.
118 data IntermediateFileType
124 :: GhcMode -- when to stop
125 -> String -- "stop after" flag (for error messages)
126 -> String -- original filename
127 -> IO [ -- list of phases to run for this file
129 IntermediateFileType, -- keep the output from this phase?
130 String) -- output file suffix
133 genPipeline todo stop_flag filename
135 split <- readIORef split_object_files
136 mangle <- readIORef do_asm_mangling
137 lang <- readIORef hsc_lang
138 keep_hc <- readIORef keep_hc_files
139 keep_raw_s <- readIORef keep_raw_s_files
140 keep_s <- readIORef keep_s_files
143 ----------- ----- ---- --- -- -- - - -
144 (_basename, suffix) = splitFilename filename
146 start_phase = startPhase suffix
148 haskellish = haskellish_suffix suffix
149 cish = cish_suffix suffix
151 -- for a .hc file, or if the -C flag is given, we need to force lang to HscC
153 | suffix == "hc" = HscC
154 | todo == StopBefore HCc && haskellish = HscC
158 ----------- ----- ---- --- -- -- - - -
160 | todo == DoMkDependHS = [ Unlit, Cpp, MkDependHS ]
164 HscC | split && mangle -> [ Unlit, Cpp, Hsc, HCc, Mangle,
165 SplitMangle, SplitAs ]
166 | mangle -> [ Unlit, Cpp, Hsc, HCc, Mangle, As ]
168 | otherwise -> [ Unlit, Cpp, Hsc, HCc, As ]
170 HscAsm | split -> [ Unlit, Cpp, Hsc, SplitMangle, SplitAs ]
171 | otherwise -> [ Unlit, Cpp, Hsc, As ]
173 HscJava | split -> not_valid
174 | otherwise -> error "not implemented: compiling via Java"
178 | otherwise = [ ] -- just pass this file through to the linker
180 -- ToDo: this is somewhat cryptic
181 not_valid = throwDyn (OtherError ("invalid option combination"))
182 ----------- ----- ---- --- -- -- - - -
184 -- this shouldn't happen.
185 if start_phase /= Ln && start_phase `notElem` pipeline
186 then throwDyn (OtherError ("can't find starting phase for "
190 -- if we can't find the phase we're supposed to stop before,
191 -- something has gone wrong.
195 && phase `notElem` pipeline
196 && not (phase == As && SplitAs `elem` pipeline)) $
198 ("flag " ++ stop_flag
199 ++ " is incompatible with source file `" ++ filename ++ "'"))
203 ----------- ----- ---- --- -- -- - - -
205 :: [Phase] -- raw pipeline
206 -> Phase -- phase to stop before
207 -> [(Phase, IntermediateFileType, String{-file extension-})]
208 annotatePipeline [] _ = []
209 annotatePipeline (Ln:_) _ = []
210 annotatePipeline (phase:next_phase:ps) stop =
211 (phase, keep_this_output, phaseInputExt next_phase)
212 : annotatePipeline (next_phase:ps) stop
215 | next_phase == stop = Persistent
219 Mangle | keep_raw_s -> Persistent
220 As | keep_s -> Persistent
221 HCc | keep_hc -> Persistent
224 -- add information about output files to the pipeline
225 -- the suffix on an output file is determined by the next phase
226 -- in the pipeline, so we add linking to the end of the pipeline
227 -- to force the output from the final phase to be a .o file.
228 stop_phase = case todo of StopBefore phase -> phase
231 annotated_pipeline = annotatePipeline (pipeline ++ [ Ln ]) stop_phase
233 phase_ne p (p1,_,_) = (p1 /= p)
234 ----------- ----- ---- --- -- -- - - -
237 dropWhile (phase_ne start_phase) .
238 foldr (\p ps -> if phase_ne stop_phase p then p:ps else []) []
243 :: [ (Phase, IntermediateFileType, String) ] -- phases to run
244 -> String -- input file
245 -> Bool -- doing linking afterward?
246 -> Bool -- take into account -o when generating output?
247 -> IO String -- return final filename
249 runPipeline pipeline input_fn do_linking use_ofile
250 = pipeLoop pipeline input_fn do_linking use_ofile basename suffix
251 where (basename, suffix) = splitFilename input_fn
253 pipeLoop [] input_fn _ _ _ _ = return input_fn
254 pipeLoop ((phase, keep, o_suffix):phases)
255 input_fn do_linking use_ofile orig_basename orig_suffix
258 output_fn <- outputFileName (null phases) keep o_suffix
260 carry_on <- run_phase phase orig_basename orig_suffix input_fn output_fn
261 -- sometimes we bail out early, eg. when the compiler's recompilation
262 -- checker has determined that recompilation isn't necessary.
264 then do let (_,keep,final_suffix) = last phases
265 ofile <- outputFileName True keep final_suffix
267 else do -- carry on ...
269 -- sadly, ghc -E is supposed to write the file to stdout. We
270 -- generate <file>.cpp, so we also have to cat the file here.
271 when (null phases && phase == Cpp) $
272 run_something "Dump pre-processed file to stdout"
273 ("cat " ++ output_fn)
275 pipeLoop phases output_fn do_linking use_ofile orig_basename orig_suffix
278 outputFileName last_phase keep suffix
279 = do o_file <- readIORef output_file
280 if last_phase && not do_linking && use_ofile && isJust o_file
283 Nothing -> error "outputFileName"
284 else if keep == Persistent
285 then do f <- odir_ify (orig_basename ++ '.':suffix)
287 else newTempName suffix
289 -------------------------------------------------------------------------------
292 run_phase Unlit _basename _suff input_fn output_fn
293 = do unlit <- readIORef pgm_L
294 unlit_flags <- getOpts opt_L
295 run_something "Literate pre-processor"
296 ("echo '# 1 \"" ++input_fn++"\"' > "++output_fn++" && "
297 ++ unlit ++ ' ':input_fn ++ " - >> " ++ output_fn)
300 -------------------------------------------------------------------------------
303 run_phase Cpp _basename _suff input_fn output_fn
304 = do src_opts <- getOptionsFromSource input_fn
305 -- ToDo: this is *wrong* if we're processing more than one file:
306 -- the OPTIONS will persist through the subsequent compilations.
307 _ <- processArgs dynamic_flags src_opts []
309 do_cpp <- readState cpp_flag
312 cpp <- readIORef pgm_P
313 hscpp_opts <- getOpts opt_P
314 hs_src_cpp_opts <- readIORef hs_source_cpp_opts
316 cmdline_include_paths <- readIORef include_paths
317 pkg_include_dirs <- getPackageIncludePath
318 let include_paths = map (\p -> "-I"++p) (cmdline_include_paths
322 run_something "C pre-processor"
324 (["echo '{-# LINE 1 \"" ++ input_fn ++ "\" -}'", ">", output_fn, "&&",
329 ++ [ "-x", "c", input_fn, ">>", output_fn ]
332 run_something "Ineffective C pre-processor"
333 ("echo '{-# LINE 1 \"" ++ input_fn ++ "\" -}' > "
334 ++ output_fn ++ " && cat " ++ input_fn
335 ++ " >> " ++ output_fn)
338 -----------------------------------------------------------------------------
341 run_phase MkDependHS basename suff input_fn _output_fn = do
342 src <- readFile input_fn
343 let imports = getImports src
345 deps <- mapM (findDependency basename) imports
347 osuf_opt <- readIORef output_suf
348 let osuf = case osuf_opt of
352 extra_suffixes <- readIORef dep_suffixes
353 let suffixes = osuf : map (++ ('_':osuf)) extra_suffixes
354 ofiles = map (\suf -> basename ++ '.':suf) suffixes
356 objs <- mapM odir_ify ofiles
358 hdl <- readIORef dep_tmp_hdl
360 -- std dependeny of the object(s) on the source file
361 hPutStrLn hdl (unwords objs ++ " : " ++ basename ++ '.':suff)
363 let genDep (dep, False {- not an hi file -}) =
364 hPutStrLn hdl (unwords objs ++ " : " ++ dep)
365 genDep (dep, True {- is an hi file -}) = do
366 hisuf <- readIORef hi_suf
367 let dep_base = remove_suffix '.' dep
368 deps = (dep_base ++ hisuf)
369 : map (\suf -> dep_base ++ suf ++ '_':hisuf) extra_suffixes
370 -- length objs should be == length deps
371 sequence_ (zipWith (\o d -> hPutStrLn hdl (o ++ " : " ++ d)) objs deps)
373 mapM genDep [ d | Just d <- deps ]
377 -- add the lines to dep_makefile:
381 -- if the dependency is on something other than a .hi file:
382 -- this.o this.p_o ... : dep
384 -- if the import is {-# SOURCE #-}
385 -- this.o this.p_o ... : dep.hi-boot[-$vers]
388 -- this.o ... : dep.hi
389 -- this.p_o ... : dep.p_hi
392 -- (where .o is $osuf, and the other suffixes come from
393 -- the cmdline -s options).
395 -----------------------------------------------------------------------------
398 run_phase Hsc basename suff input_fn output_fn
401 -- we add the current directory (i.e. the directory in which
402 -- the .hs files resides) to the import path, since this is
403 -- what gcc does, and it's probably what you want.
404 let current_dir = getdir basename
406 paths <- readIORef include_paths
407 writeIORef include_paths (current_dir : paths)
409 -- figure out where to put the .hi file
410 ohi <- readIORef output_hi
411 hisuf <- readIORef hi_suf
412 let hifile = case ohi of
413 Nothing -> current_dir ++ {-ToDo: modname!!-}basename
417 -- figure out if the source has changed, for recompilation avoidance.
418 -- only do this if we're eventually going to generate a .o file.
419 -- (ToDo: do when generating .hc files too?)
421 -- Setting source_unchanged to "-fsource_unchanged" means that M.o seems
422 -- to be up to date wrt M.hs; so no need to recompile unless imports have
423 -- changed (which the compiler itself figures out).
424 -- Setting source_unchanged to "" tells the compiler that M.o is out of
425 -- date wrt M.hs (or M.o doesn't exist) so we must recompile regardless.
426 do_recomp <- readIORef recomp
427 todo <- readIORef v_GhcMode
428 o_file <- odir_ify (basename ++ '.':phaseInputExt Ln)
430 if not (do_recomp && ( todo == DoLink || todo == StopBefore Ln ))
432 else do t1 <- getModificationTime (basename ++ '.':suff)
433 o_file_exists <- doesFileExist o_file
435 then return "" -- Need to recompile
436 else do t2 <- getModificationTime o_file
438 then return "-fsource-unchanged"
441 -- build a bogus ModSummary to pass to hscMain.
442 let summary = ModSummary {
443 ms_location = error "no loc",
444 ms_ppsource = Just (loc, error "no fingerprint"),
445 ms_imports = error "no imports"
449 result <- hscMain dyn_flags mod_summary
451 output_fn emptyUFM emptyPCS
455 HscErrs pcs errs warns -> do {
456 printErrorsAndWarnings errs warns
457 throwDyn (PhaseFailed "hsc" (ExitFailure 1)) };
459 HscOK details maybe_iface maybe_stub_h maybe_stub_c pcs warns -> do
461 pprBagOfWarnings warns
463 -- get the module name
465 -- generate the interface file
467 Nothing -> -- compilation not required
468 do run_something "Touching object file" ("touch " ++ o_file)
472 -- discover the filename for the .hi file in a roundabout way
473 let mod = md_id details
474 locn <- mkHomeModule mod basename input_fn
475 let hifile = hi_file locn
476 -- write out the interface file here...
480 maybe_stub_o <- dealWithStubs basename maybe_stub_h maybe_stub_c
483 Just stub_o -> add ld_inputs stub_o
488 -----------------------------------------------------------------------------
491 -- we don't support preprocessing .c files (with -E) now. Doing so introduces
492 -- way too many hacks, and I can't say I've ever used it anyway.
494 run_phase cc_phase _basename _suff input_fn output_fn
495 | cc_phase == Cc || cc_phase == HCc
496 = do cc <- readIORef pgm_c
497 cc_opts <- (getOpts opt_c)
498 cmdline_include_dirs <- readIORef include_paths
500 let hcc = cc_phase == HCc
502 -- add package include paths even if we're just compiling
503 -- .c files; this is the Value Add(TM) that using
504 -- ghc instead of gcc gives you :)
505 pkg_include_dirs <- getPackageIncludePath
506 let include_paths = map (\p -> "-I"++p) (cmdline_include_dirs
509 c_includes <- getPackageCIncludes
510 cmdline_includes <- readState cmdline_hc_includes -- -#include options
512 let cc_injects | hcc = unlines (map mk_include
513 (c_includes ++ reverse cmdline_includes))
517 '"':_{-"-} -> "#include "++h_file
518 '<':_ -> "#include "++h_file
519 _ -> "#include \""++h_file++"\""
521 cc_help <- newTempName "c"
522 h <- openFile cc_help WriteMode
524 hPutStrLn h ("#include \"" ++ input_fn ++ "\"\n")
527 ccout <- newTempName "ccout"
529 mangle <- readIORef do_asm_mangling
530 (md_c_flags, md_regd_c_flags) <- machdepCCOpts
534 o2 <- readIORef opt_minus_o2_for_C
535 let opt_flag | o2 = "-O2"
538 pkg_extra_cc_opts <- getPackageExtraCcOpts
540 excessPrecision <- readIORef excess_precision
542 run_something "C Compiler"
543 (unwords ([ cc, "-x", "c", cc_help, "-o", output_fn ]
545 ++ (if cc_phase == HCc && mangle
548 ++ [ verb, "-S", "-Wimplicit", opt_flag ]
549 ++ [ "-D__GLASGOW_HASKELL__="++cProjectVersionInt ]
551 #ifdef mingw32_TARGET_OS
554 ++ (if excessPrecision then [] else [ "-ffloat-store" ])
561 -- ToDo: postprocess the output from gcc
563 -----------------------------------------------------------------------------
566 run_phase Mangle _basename _suff input_fn output_fn
567 = do mangler <- readIORef pgm_m
568 mangler_opts <- getOpts opt_m
570 if (prefixMatch "i386" cTARGETPLATFORM)
571 then do n_regs <- readState stolen_x86_regs
572 return [ show n_regs ]
574 run_something "Assembly Mangler"
577 ++ [ input_fn, output_fn ]
582 -----------------------------------------------------------------------------
585 run_phase SplitMangle _basename _suff input_fn _output_fn
586 = do splitter <- readIORef pgm_s
588 -- this is the prefix used for the split .s files
589 tmp_pfx <- readIORef v_TmpDir
591 let split_s_prefix = tmp_pfx ++ "/ghc" ++ show x
592 writeIORef split_prefix split_s_prefix
593 addFilesToClean [split_s_prefix ++ "__*"] -- d:-)
595 -- allocate a tmp file to put the no. of split .s files in (sigh)
596 n_files <- newTempName "n_files"
598 run_something "Split Assembly File"
605 -- save the number of split files for future references
606 s <- readFile n_files
607 let n = read s :: Int
608 writeIORef n_split_files n
611 -----------------------------------------------------------------------------
614 run_phase As _basename _suff input_fn output_fn
615 = do as <- readIORef pgm_a
616 as_opts <- getOpts opt_a
618 cmdline_include_paths <- readIORef include_paths
619 let cmdline_include_flags = map (\p -> "-I"++p) cmdline_include_paths
620 run_something "Assembler"
621 (unwords (as : as_opts
622 ++ cmdline_include_flags
623 ++ [ "-c", input_fn, "-o", output_fn ]
627 run_phase SplitAs basename _suff _input_fn _output_fn
628 = do as <- readIORef pgm_a
629 as_opts <- getOpts opt_a
631 split_s_prefix <- readIORef split_prefix
632 n <- readIORef n_split_files
634 odir <- readIORef output_dir
635 let real_odir = case odir of
639 let assemble_file n = do
640 let input_s = split_s_prefix ++ "__" ++ show n ++ ".s"
641 let output_o = newdir real_odir
642 (basename ++ "__" ++ show n ++ ".o")
643 real_o <- osuf_ify output_o
644 run_something "Assembler"
645 (unwords (as : as_opts
646 ++ [ "-c", "-o", real_o, input_s ]
649 mapM_ assemble_file [1..n]
652 -----------------------------------------------------------------------------
655 doLink :: [String] -> IO ()
657 ln <- readIORef pgm_l
659 o_file <- readIORef output_file
660 let output_fn = case o_file of { Just s -> s; Nothing -> "a.out"; }
662 pkg_lib_paths <- getPackageLibraryPath
663 let pkg_lib_path_opts = map ("-L"++) pkg_lib_paths
665 lib_paths <- readIORef library_paths
666 let lib_path_opts = map ("-L"++) lib_paths
668 pkg_libs <- getPackageLibraries
669 let pkg_lib_opts = map (\lib -> "-l"++lib) pkg_libs
671 libs <- readIORef cmdline_libraries
672 let lib_opts = map ("-l"++) (reverse libs)
673 -- reverse because they're added in reverse order from the cmd line
675 pkg_extra_ld_opts <- getPackageExtraLdOpts
677 -- probably _stub.o files
678 extra_ld_inputs <- readIORef ld_inputs
680 -- opts from -optl-<blah>
681 extra_ld_opts <- getStaticOpts opt_l
683 run_something "Linker"
685 ([ ln, verb, "-o", output_fn ]
697 -----------------------------------------------------------------------------
698 -- Just preprocess a file, put the result in a temp. file (used by the
699 -- compilation manager during the summary phase).
701 preprocess :: FilePath -> IO FilePath
702 preprocess filename =
703 ASSERT(haskellish_file filename)
704 do pipeline <- genPipeline (StopBefore Hsc) ("preprocess") filename
705 runPipeline pipeline filename False{-no linking-} False{-no -o flag-}
707 -----------------------------------------------------------------------------
708 -- Compile a single module.
710 -- This is the interface between the compilation manager and the
711 -- compiler proper (hsc), where we deal with tedious details like
712 -- reading the OPTIONS pragma from the source file, and passing the
713 -- output of hsc through the C compiler.
715 compile :: Finder -- to find modules
716 -> ModSummary -- summary, including source
717 -> Maybe ModIFace -- old interface, if available
718 -> HomeSymbolTable -- for home module ModDetails
719 -> PersistentCompilerState -- persistent compiler state
722 compile finder summary old_iface hst pcs = do
723 verb <- readIORef verbose
724 when verb (hPutStrLn stderr ("compile: compiling " ++
725 name_of_summary summary))
727 init_dyn_flags <- readIORef v_InitDynFlags
728 writeIORef v_DynFlags init_dyn_flags
730 let input_fn = case ms_ppsource summary of
731 Just (ppsource, fingerprint) -> ppsource
732 Nothing -> hs_file (ms_location summary)
734 when verb (hPutStrLn stderr ("compile: input file " ++ input_fn))
736 opts <- getOptionsFromSource input_fn
737 processArgs dynamic_flags opts []
738 dyn_flags <- readIORef v_DynFlags
740 output_fn <- case hsc_lang of
741 HscAsm -> newTempName (phaseInputExt As)
742 HscC -> newTempName (phaseInputExt HCc)
743 HscJava -> newTempName "java" -- ToDo
744 HscInterpreter -> return (error "no output file")
747 hsc_result <- hscMain dyn_flags summary old_iface output_fn hst pcs
750 HscErrs pcs errs warns -> return (CompErrs pcs errs warns);
752 HscOK details maybe_iface
753 maybe_stub_h maybe_stub_c maybe_interpreted_code pcs warns -> do
755 -- if no compilation happened, bail out early
756 case maybe_iface of {
757 Nothing -> return (CompOK details Nothing pcs warns);
760 let (basename, _) = splitFilename (hs_file (ms_location summary))
761 maybe_stub_o <- dealWithStubs basename maybe_stub_h maybe_stub_c
762 stub_unlinked <- case maybe_stub_o of
764 Just stub_o -> [ DotO stub_o ]
769 -- in interpreted mode, just return the compiled code
770 -- as our "unlinked" object.
772 case maybe_interpreted_code of
773 Just code -> return (Trees code)
774 Nothing -> panic "compile: no interpreted code"
776 -- we're in batch mode: finish the compilation pipeline.
777 _other -> do pipe <- genPipeline (StopBefore Ln) "" output_fn
778 o_file <- runPipeline pipe output_fn False False
779 return [ DotO o_file ]
781 let linkable = LM (moduleName (ms_mod summary))
782 (hs_unlinked ++ stub_unlinked)
784 return (CompOK details (Just (iface, linkable)) pcs warns)
788 -----------------------------------------------------------------------------
789 -- stub .h and .c files (for foreign export support)
791 dealWithStubs basename maybe_stub_h maybe_stub_c
793 = do let stub_h = basename ++ "_stub.h"
794 let stub_c = basename ++ "_stub.c"
796 -- copy the .stub_h file into the current dir if necessary
799 Just tmp_stub_h -> do
800 run_something "Copy stub .h file"
801 ("cp " ++ tmp_stub_h ++ ' ':stub_h)
803 -- #include <..._stub.h> in .hc file
804 addCmdlineHCInclude tmp_stub_h -- hack
806 -- copy the .stub_c file into the current dir, and compile it, if necessary
808 Nothing -> return Nothing
809 Just tmp_stub_c -> do -- copy the _stub.c file into the current dir
810 run_something "Copy stub .c file"
812 "rm -f", stub_c, "&&",
813 "echo \'#include \""++stub_h++"\"\' >"++stub_c, " &&",
814 "cat", tmp_stub_c, ">> ", stub_c
817 -- compile the _stub.c file w/ gcc
818 pipeline <- genPipeline (StopBefore Ln) "" stub_c
819 stub_o <- runPipeline pipeline stub_c False{-no linking-}
820 False{-no -o option-}