--- 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
- -> String -- original filename
- -> 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
- = do
- split <- readIORef v_Split_object_files
- mangle <- readIORef v_Do_asm_mangling
- keep_hc <- readIORef v_Keep_hc_files
- keep_raw_s <- readIORef v_Keep_raw_s_files
- keep_s <- readIORef v_Keep_s_files
-#ifdef ILX
- writeIORef v_Object_suf (Just "ilx")
+-- We return the augmented DynFlags, because they contain the result
+-- of slurping in the OPTIONS pragmas
+
+preprocess :: DynFlags -> FilePath -> IO (DynFlags, FilePath)
+preprocess dflags filename =
+ ASSERT2(isHaskellSrcFilename filename, text filename)
+ runPipeline (StopBefore anyHsc) dflags ("preprocess")
+ False{-temporary output file-}
+ Nothing{-no specific output file-}
+ filename
+ Nothing{-no ModLocation-}
+
+
+
+-- ---------------------------------------------------------------------------
+-- Compile a file
+-- This is used in batch mode
+compileFile :: GhcMode -> DynFlags -> FilePath -> IO FilePath
+compileFile mode dflags src = do
+ exists <- doesFileExist src
+ when (not exists) $
+ throwDyn (CmdLineError ("file `" ++ src ++ "' does not exist"))
+
+ o_file <- readIORef v_Output_file
+ no_link <- readIORef v_NoLink -- Set by -c or -no-link
+ -- When linking, the -o argument refers to the linker's output.
+ -- otherwise, we use it as the name for the pipeline's output.
+ let maybe_o_file | no_link = o_file
+ | otherwise = Nothing
+
+ stop_flag <- readIORef v_GhcModeFlag
+ (_, out_file) <- runPipeline mode dflags stop_flag True maybe_o_file
+ src Nothing{-no ModLocation-}
+ return out_file
+
+
+-- ---------------------------------------------------------------------------
+-- Compile
+
+-- Compile a single module, under the control of the compilation manager.
+--
+-- 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
+ -> ModSummary
+ -> 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 mod_summary
+ source_unchanged have_object old_iface = do
+
+ let dyn_flags = hsc_dflags hsc_env
+ this_mod = ms_mod mod_summary
+ src_flavour = ms_hsc_src mod_summary
+
+ showPass dyn_flags ("Compiling " ++ showModMsg have_object mod_summary)
+
+ let verb = verbosity dyn_flags
+ let location = ms_location mod_summary
+ let input_fn = expectJust "compile:hs" (ml_hs_file location)
+ let input_fnpp = expectJust "compile:hspp" (ms_hspp_file mod_summary)
+
+ when (verb >= 2) (hPutStrLn stderr ("compile: input file " ++ input_fnpp))
+
+ -- Add in the OPTIONS from the source file
+ -- This is nasty: we've done this once already, in the compilation manager
+ -- It might be better to cache the flags in the ml_hspp_file field,say
+ opts <- getOptionsFromSource input_fnpp
+ (dyn_flags,unhandled_flags) <- processDynamicFlags opts dyn_flags
+ checkProcessArgsResult unhandled_flags input_fn
+
+ let (basename, _) = splitFilename input_fn
+
+ -- We add the directory in which the .hs files resides) to the import path.
+ -- This is needed when we try to compile the .hc file later, if it
+ -- imports a _stub.h file that we created here.
+ let current_dir = directoryOf basename
+ old_paths <- readIORef v_Include_paths
+ writeIORef v_Include_paths (current_dir : old_paths)
+ -- put back the old include paths afterward.
+ later (writeIORef v_Include_paths old_paths) $ do
+
+ -- Figure out what lang we're generating
+ todo <- readIORef v_GhcMode
+ hsc_lang <- hscMaybeAdjustTarget todo src_flavour (hscTarget dyn_flags)
+ -- ... and what the next phase should be
+ next_phase <- hscNextPhase src_flavour hsc_lang
+ -- ... and what file to generate the output into
+ get_output_fn <- genOutputFilenameFunc next_phase False Nothing basename
+ output_fn <- get_output_fn next_phase (Just location)
+
+ let dyn_flags' = dyn_flags { hscTarget = 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 mod_summary
+ 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
+
+ | isHsBoot src_flavour -- No further compilation to do
+ -> return (CompOK details rdr_env iface Nothing)
+
+ | otherwise -- Normal Haskell source files
+ -> 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], ms_hs_date mod_summary)
+ -- 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.