--- Herein is all the magic about which phases to run in which order, whether
--- the intermediate files should be in TMPDIR or in the current directory,
--- what the suffix of the intermediate files should be, etc.
-
--- The following compilation pipeline algorithm is fairly hacky. A
--- better way to do this would be to express the whole compilation as a
--- data flow DAG, where the nodes are the intermediate files and the
--- edges are the compilation phases. This framework would also work
--- nicely if a haskell dependency generator was included in the
--- driver.
-
--- It would also deal much more cleanly with compilation phases that
--- generate multiple intermediates, (eg. hsc generates .hc, .hi, and
--- possibly stub files), where some of the output files need to be
--- processed further (eg. the stub files need to be compiled by the C
--- compiler).
-
--- A cool thing to do would then be to execute the data flow graph
--- concurrently, automatically taking advantage of extra processors on
--- the host machine. For example, when compiling two Haskell files
--- where one depends on the other, the data flow graph would determine
--- that the C compiler from the first compilation can be overlapped
--- with the hsc compilation for the second file.
-
-data IntermediateFileType
- = Temporary
- | Persistent
- deriving (Eq, Show)
-
-genPipeline
- :: GhcMode -- when to stop
- -> String -- "stop after" flag (for error messages)
- -> Bool -- True => output is persistent
- -> HscLang -- preferred output language for hsc
- -> (FilePath, String) -- original filename & its suffix
- -> IO [ -- list of phases to run for this file
- (Phase,
- IntermediateFileType, -- keep the output from this phase?
- String) -- output file suffix
- ]
-
-genPipeline todo stop_flag persistent_output lang (filename,suffix)
- = do
- split <- readIORef v_Split_object_files
- mangle <- readIORef v_Do_asm_mangling
- keep_hc <- readIORef v_Keep_hc_files
-#ifdef ILX
- keep_il <- readIORef v_Keep_il_files
- keep_ilx <- readIORef v_Keep_ilx_files
+-- This is the interface between the compilation manager and the
+-- compiler proper (hsc), where we deal with tedious details like
+-- reading the OPTIONS pragma from the source file, and passing the
+-- output of hsc through the C compiler.
+
+-- The driver sits between 'compile' and 'hscMain', translating calls
+-- to the former into calls to the latter, and results from the latter
+-- into results from the former. It does things like preprocessing
+-- the .hs file if necessary, and compiling up the .stub_c files to
+-- generate Linkables.
+
+-- NB. No old interface can also mean that the source has changed.
+
+compile :: HscEnv
+ -> Module
+ -> ModLocation
+ -> ClockTime -- timestamp of original source file
+ -> Bool -- True <=> source unchanged
+ -> Bool -- True <=> have object
+ -> Maybe ModIface -- old interface, if available
+ -> IO CompResult
+
+data CompResult
+ = CompOK ModDetails -- New details
+ (Maybe GlobalRdrEnv) -- Lexical environment for the module
+ -- (Maybe because we may have loaded it from
+ -- its precompiled interface)
+ ModIface -- New iface
+ (Maybe Linkable) -- New code; Nothing => compilation was not reqd
+ -- (old code is still valid)
+
+ | CompErrs
+
+
+compile hsc_env this_mod location src_timestamp
+ source_unchanged have_object
+ old_iface = do
+
+ dyn_flags <- restoreDynFlags -- Restore to the state of the last save
+
+ showPass dyn_flags
+ (showSDoc (text "Compiling" <+> ppr this_mod))
+
+ let verb = verbosity dyn_flags
+ let input_fn = expectJust "compile:hs" (ml_hs_file location)
+ let input_fnpp = expectJust "compile:hspp" (ml_hspp_file location)
+ let mod_name = moduleName this_mod
+
+ when (verb >= 2) (hPutStrLn stderr ("compile: input file " ++ input_fnpp))
+
+ opts <- getOptionsFromSource input_fnpp
+ processArgs dynamic_flags opts []
+ dyn_flags <- getDynFlags
+
+ let (basename, _) = splitFilename input_fn
+
+ -- figure out what lang we're generating
+ hsc_lang <- hscMaybeAdjustLang (hscLang dyn_flags)
+ -- figure out what the next phase should be
+ next_phase <- hscNextPhase hsc_lang
+ -- figure out what file to generate the output into
+ get_output_fn <- genOutputFilenameFunc False Nothing next_phase basename
+ output_fn <- get_output_fn next_phase (Just location)
+
+ let dyn_flags' = dyn_flags { hscLang = hsc_lang,
+ hscOutName = output_fn,
+ hscStubCOutName = basename ++ "_stub.c",
+ hscStubHOutName = basename ++ "_stub.h",
+ extCoreName = basename ++ ".hcr" }
+
+ -- -no-recomp should also work with --make
+ do_recomp <- readIORef v_Recomp
+ let source_unchanged' = source_unchanged && do_recomp
+ hsc_env' = hsc_env { hsc_dflags = dyn_flags' }
+
+ -- run the compiler
+ hsc_result <- hscMain hsc_env' printErrorsAndWarnings this_mod location
+ source_unchanged' have_object old_iface
+
+ case hsc_result of
+ HscFail -> return CompErrs
+
+ HscNoRecomp details iface -> return (CompOK details Nothing iface Nothing)
+
+ HscRecomp details rdr_env iface
+ stub_h_exists stub_c_exists maybe_interpreted_code -> do
+ let
+ maybe_stub_o <- compileStub dyn_flags' stub_c_exists
+ let stub_unlinked = case maybe_stub_o of
+ Nothing -> []
+ Just stub_o -> [ DotO stub_o ]
+
+ (hs_unlinked, unlinked_time) <-
+ case hsc_lang of
+
+ -- in interpreted mode, just return the compiled code
+ -- as our "unlinked" object.
+ HscInterpreted ->
+ case maybe_interpreted_code of
+#ifdef GHCI
+ Just comp_bc -> return ([BCOs comp_bc], src_timestamp)
+ -- Why do we use the timestamp of the source file here,
+ -- rather than the current time? This works better in
+ -- the case where the local clock is out of sync
+ -- with the filesystem's clock. It's just as accurate:
+ -- if the source is modified, then the linkable will
+ -- be out of date.