/compiler/main/DriverPipeline.hs

https://bitbucket.org/carter/ghc · Haskell · 2173 lines · 1351 code · 327 blank · 495 comment · 207 complexity · a6224165062afeff42111013ddfc698a MD5 · raw file

Large files are truncated click here to view the full file

  1. {-# OPTIONS -fno-cse #-}
  2. {-# LANGUAGE NamedFieldPuns #-}
  3. -- -fno-cse is needed for GLOBAL_VAR's to behave properly
  4. -----------------------------------------------------------------------------
  5. --
  6. -- GHC Driver
  7. --
  8. -- (c) The University of Glasgow 2005
  9. --
  10. -----------------------------------------------------------------------------
  11. module DriverPipeline (
  12. -- Run a series of compilation steps in a pipeline, for a
  13. -- collection of source files.
  14. oneShot, compileFile,
  15. -- Interfaces for the batch-mode driver
  16. linkBinary,
  17. -- Interfaces for the compilation manager (interpreted/batch-mode)
  18. preprocess,
  19. compile, compile',
  20. link,
  21. ) where
  22. #include "HsVersions.h"
  23. import Packages
  24. import HeaderInfo
  25. import DriverPhases
  26. import SysTools
  27. import HscMain
  28. import Finder
  29. import HscTypes
  30. import Outputable
  31. import Module
  32. import UniqFM ( eltsUFM )
  33. import ErrUtils
  34. import DynFlags
  35. import Config
  36. import Panic
  37. import Util
  38. import StringBuffer ( hGetStringBuffer )
  39. import BasicTypes ( SuccessFlag(..) )
  40. import Maybes ( expectJust )
  41. import ParserCoreUtils ( getCoreModuleName )
  42. import SrcLoc
  43. import FastString
  44. import LlvmCodeGen ( llvmFixupAsm )
  45. import MonadUtils
  46. import Platform
  47. import Exception
  48. import Data.IORef ( readIORef )
  49. import System.Directory
  50. import System.FilePath
  51. import System.IO
  52. import Control.Monad
  53. import Data.List ( isSuffixOf )
  54. import Data.Maybe
  55. import System.Environment
  56. import Data.Char
  57. -- ---------------------------------------------------------------------------
  58. -- Pre-process
  59. -- | Just preprocess a file, put the result in a temp. file (used by the
  60. -- compilation manager during the summary phase).
  61. --
  62. -- We return the augmented DynFlags, because they contain the result
  63. -- of slurping in the OPTIONS pragmas
  64. preprocess :: HscEnv
  65. -> (FilePath, Maybe Phase) -- ^ filename and starting phase
  66. -> IO (DynFlags, FilePath)
  67. preprocess hsc_env (filename, mb_phase) =
  68. ASSERT2(isJust mb_phase || isHaskellSrcFilename filename, text filename)
  69. runPipeline anyHsc hsc_env (filename, mb_phase)
  70. Nothing Temporary Nothing{-no ModLocation-} Nothing{-no stub-}
  71. -- ---------------------------------------------------------------------------
  72. -- | Compile
  73. --
  74. -- Compile a single module, under the control of the compilation manager.
  75. --
  76. -- This is the interface between the compilation manager and the
  77. -- compiler proper (hsc), where we deal with tedious details like
  78. -- reading the OPTIONS pragma from the source file, converting the
  79. -- C or assembly that GHC produces into an object file, and compiling
  80. -- FFI stub files.
  81. --
  82. -- NB. No old interface can also mean that the source has changed.
  83. compile :: HscEnv
  84. -> ModSummary -- ^ summary for module being compiled
  85. -> Int -- ^ module N ...
  86. -> Int -- ^ ... of M
  87. -> Maybe ModIface -- ^ old interface, if we have one
  88. -> Maybe Linkable -- ^ old linkable, if we have one
  89. -> SourceModified
  90. -> IO HomeModInfo -- ^ the complete HomeModInfo, if successful
  91. compile = compile' (hscCompileNothing, hscCompileInteractive, hscCompileBatch)
  92. compile' ::
  93. (Compiler (HscStatus, ModIface, ModDetails),
  94. Compiler (InteractiveStatus, ModIface, ModDetails),
  95. Compiler (HscStatus, ModIface, ModDetails))
  96. -> HscEnv
  97. -> ModSummary -- ^ summary for module being compiled
  98. -> Int -- ^ module N ...
  99. -> Int -- ^ ... of M
  100. -> Maybe ModIface -- ^ old interface, if we have one
  101. -> Maybe Linkable -- ^ old linkable, if we have one
  102. -> SourceModified
  103. -> IO HomeModInfo -- ^ the complete HomeModInfo, if successful
  104. compile' (nothingCompiler, interactiveCompiler, batchCompiler)
  105. hsc_env0 summary mod_index nmods mb_old_iface maybe_old_linkable
  106. source_modified0
  107. = do
  108. let dflags0 = ms_hspp_opts summary
  109. this_mod = ms_mod summary
  110. src_flavour = ms_hsc_src summary
  111. location = ms_location summary
  112. input_fn = expectJust "compile:hs" (ml_hs_file location)
  113. input_fnpp = ms_hspp_file summary
  114. debugTraceMsg dflags0 2 (text "compile: input file" <+> text input_fnpp)
  115. let basename = dropExtension input_fn
  116. -- We add the directory in which the .hs files resides) to the import path.
  117. -- This is needed when we try to compile the .hc file later, if it
  118. -- imports a _stub.h file that we created here.
  119. let current_dir = takeDirectory basename
  120. old_paths = includePaths dflags0
  121. dflags = dflags0 { includePaths = current_dir : old_paths }
  122. hsc_env = hsc_env0 {hsc_dflags = dflags}
  123. -- Figure out what lang we're generating
  124. let hsc_lang = hscTarget dflags
  125. -- ... and what the next phase should be
  126. let next_phase = hscPostBackendPhase dflags src_flavour hsc_lang
  127. -- ... and what file to generate the output into
  128. output_fn <- getOutputFilename next_phase
  129. Temporary basename dflags next_phase (Just location)
  130. let dflags' = dflags { hscTarget = hsc_lang,
  131. hscOutName = output_fn,
  132. extCoreName = basename ++ ".hcr" }
  133. let hsc_env' = hsc_env { hsc_dflags = dflags' }
  134. -- -fforce-recomp should also work with --make
  135. let force_recomp = dopt Opt_ForceRecomp dflags
  136. source_modified
  137. | force_recomp || isNothing maybe_old_linkable = SourceModified
  138. | otherwise = source_modified0
  139. object_filename = ml_obj_file location
  140. let handleBatch HscNoRecomp
  141. = ASSERT (isJust maybe_old_linkable)
  142. return maybe_old_linkable
  143. handleBatch (HscRecomp hasStub _)
  144. | isHsBoot src_flavour
  145. = do when (isObjectTarget hsc_lang) $ -- interpreted reaches here too
  146. liftIO $ touchObjectFile dflags' object_filename
  147. return maybe_old_linkable
  148. | otherwise
  149. = do (hs_unlinked, unlinked_time) <-
  150. case hsc_lang of
  151. HscNothing ->
  152. return ([], ms_hs_date summary)
  153. -- We're in --make mode: finish the compilation pipeline.
  154. _other -> do
  155. maybe_stub_o <- case hasStub of
  156. Nothing -> return Nothing
  157. Just stub_c -> do
  158. stub_o <- compileStub hsc_env' stub_c
  159. return (Just stub_o)
  160. _ <- runPipeline StopLn hsc_env' (output_fn,Nothing)
  161. (Just basename)
  162. Persistent
  163. (Just location)
  164. maybe_stub_o
  165. -- The object filename comes from the ModLocation
  166. o_time <- getModificationUTCTime object_filename
  167. return ([DotO object_filename], o_time)
  168. let linkable = LM unlinked_time this_mod hs_unlinked
  169. return (Just linkable)
  170. handleInterpreted HscNoRecomp
  171. = ASSERT (isJust maybe_old_linkable)
  172. return maybe_old_linkable
  173. handleInterpreted (HscRecomp _hasStub Nothing)
  174. = ASSERT (isHsBoot src_flavour)
  175. return maybe_old_linkable
  176. handleInterpreted (HscRecomp hasStub (Just (comp_bc, modBreaks)))
  177. = do stub_o <- case hasStub of
  178. Nothing -> return []
  179. Just stub_c -> do
  180. stub_o <- compileStub hsc_env' stub_c
  181. return [DotO stub_o]
  182. let hs_unlinked = [BCOs comp_bc modBreaks]
  183. unlinked_time = ms_hs_date summary
  184. -- Why do we use the timestamp of the source file here,
  185. -- rather than the current time? This works better in
  186. -- the case where the local clock is out of sync
  187. -- with the filesystem's clock. It's just as accurate:
  188. -- if the source is modified, then the linkable will
  189. -- be out of date.
  190. let linkable = LM unlinked_time this_mod
  191. (hs_unlinked ++ stub_o)
  192. return (Just linkable)
  193. let -- runCompiler :: Compiler result -> (result -> Maybe Linkable)
  194. -- -> m HomeModInfo
  195. runCompiler compiler handle
  196. = do (result, iface, details)
  197. <- compiler hsc_env' summary source_modified mb_old_iface
  198. (Just (mod_index, nmods))
  199. linkable <- handle result
  200. return (HomeModInfo{ hm_details = details,
  201. hm_iface = iface,
  202. hm_linkable = linkable })
  203. -- run the compiler
  204. case hsc_lang of
  205. HscInterpreted -> runCompiler interactiveCompiler handleInterpreted
  206. HscNothing -> runCompiler nothingCompiler handleBatch
  207. _other -> runCompiler batchCompiler handleBatch
  208. -----------------------------------------------------------------------------
  209. -- stub .h and .c files (for foreign export support)
  210. -- The _stub.c file is derived from the haskell source file, possibly taking
  211. -- into account the -stubdir option.
  212. --
  213. -- The object file created by compiling the _stub.c file is put into a
  214. -- temporary file, which will be later combined with the main .o file
  215. -- (see the MergeStubs phase).
  216. compileStub :: HscEnv -> FilePath -> IO FilePath
  217. compileStub hsc_env stub_c = do
  218. (_, stub_o) <- runPipeline StopLn hsc_env (stub_c,Nothing) Nothing
  219. Temporary Nothing{-no ModLocation-} Nothing
  220. return stub_o
  221. -- ---------------------------------------------------------------------------
  222. -- Link
  223. link :: GhcLink -- interactive or batch
  224. -> DynFlags -- dynamic flags
  225. -> Bool -- attempt linking in batch mode?
  226. -> HomePackageTable -- what to link
  227. -> IO SuccessFlag
  228. -- For the moment, in the batch linker, we don't bother to tell doLink
  229. -- which packages to link -- it just tries all that are available.
  230. -- batch_attempt_linking should only be *looked at* in batch mode. It
  231. -- should only be True if the upsweep was successful and someone
  232. -- exports main, i.e., we have good reason to believe that linking
  233. -- will succeed.
  234. link LinkInMemory _ _ _
  235. = if cGhcWithInterpreter == "YES"
  236. then -- Not Linking...(demand linker will do the job)
  237. return Succeeded
  238. else panicBadLink LinkInMemory
  239. link NoLink _ _ _
  240. = return Succeeded
  241. link LinkBinary dflags batch_attempt_linking hpt
  242. = link' dflags batch_attempt_linking hpt
  243. link LinkDynLib dflags batch_attempt_linking hpt
  244. = link' dflags batch_attempt_linking hpt
  245. panicBadLink :: GhcLink -> a
  246. panicBadLink other = panic ("link: GHC not built to link this way: " ++
  247. show other)
  248. link' :: DynFlags -- dynamic flags
  249. -> Bool -- attempt linking in batch mode?
  250. -> HomePackageTable -- what to link
  251. -> IO SuccessFlag
  252. link' dflags batch_attempt_linking hpt
  253. | batch_attempt_linking
  254. = do
  255. let
  256. home_mod_infos = eltsUFM hpt
  257. -- the packages we depend on
  258. pkg_deps = concatMap (map fst . dep_pkgs . mi_deps . hm_iface) home_mod_infos
  259. -- the linkables to link
  260. linkables = map (expectJust "link".hm_linkable) home_mod_infos
  261. debugTraceMsg dflags 3 (text "link: linkables are ..." $$ vcat (map ppr linkables))
  262. -- check for the -no-link flag
  263. if isNoLink (ghcLink dflags)
  264. then do debugTraceMsg dflags 3 (text "link(batch): linking omitted (-c flag given).")
  265. return Succeeded
  266. else do
  267. let getOfiles (LM _ _ us) = map nameOfObject (filter isObject us)
  268. obj_files = concatMap getOfiles linkables
  269. exe_file = exeFileName dflags
  270. linking_needed <- linkingNeeded dflags linkables pkg_deps
  271. if not (dopt Opt_ForceRecomp dflags) && not linking_needed
  272. then do debugTraceMsg dflags 2 (text exe_file <+> ptext (sLit "is up to date, linking not required."))
  273. return Succeeded
  274. else do
  275. compilationProgressMsg dflags ("Linking " ++ exe_file ++ " ...")
  276. -- Don't showPass in Batch mode; doLink will do that for us.
  277. let link = case ghcLink dflags of
  278. LinkBinary -> linkBinary
  279. LinkDynLib -> linkDynLib
  280. other -> panicBadLink other
  281. link dflags obj_files pkg_deps
  282. debugTraceMsg dflags 3 (text "link: done")
  283. -- linkBinary only returns if it succeeds
  284. return Succeeded
  285. | otherwise
  286. = do debugTraceMsg dflags 3 (text "link(batch): upsweep (partially) failed OR" $$
  287. text " Main.main not exported; not linking.")
  288. return Succeeded
  289. linkingNeeded :: DynFlags -> [Linkable] -> [PackageId] -> IO Bool
  290. linkingNeeded dflags linkables pkg_deps = do
  291. -- if the modification time on the executable is later than the
  292. -- modification times on all of the objects and libraries, then omit
  293. -- linking (unless the -fforce-recomp flag was given).
  294. let exe_file = exeFileName dflags
  295. e_exe_time <- tryIO $ getModificationUTCTime exe_file
  296. case e_exe_time of
  297. Left _ -> return True
  298. Right t -> do
  299. -- first check object files and extra_ld_inputs
  300. let extra_ld_inputs = ldInputs dflags
  301. e_extra_times <- mapM (tryIO . getModificationUTCTime) extra_ld_inputs
  302. let (errs,extra_times) = splitEithers e_extra_times
  303. let obj_times = map linkableTime linkables ++ extra_times
  304. if not (null errs) || any (t <) obj_times
  305. then return True
  306. else do
  307. -- next, check libraries. XXX this only checks Haskell libraries,
  308. -- not extra_libraries or -l things from the command line.
  309. let pkg_map = pkgIdMap (pkgState dflags)
  310. pkg_hslibs = [ (libraryDirs c, lib)
  311. | Just c <- map (lookupPackage pkg_map) pkg_deps,
  312. lib <- packageHsLibs dflags c ]
  313. pkg_libfiles <- mapM (uncurry (findHSLib dflags)) pkg_hslibs
  314. if any isNothing pkg_libfiles then return True else do
  315. e_lib_times <- mapM (tryIO . getModificationUTCTime)
  316. (catMaybes pkg_libfiles)
  317. let (lib_errs,lib_times) = splitEithers e_lib_times
  318. if not (null lib_errs) || any (t <) lib_times
  319. then return True
  320. else checkLinkInfo dflags pkg_deps exe_file
  321. -- Returns 'False' if it was, and we can avoid linking, because the
  322. -- previous binary was linked with "the same options".
  323. checkLinkInfo :: DynFlags -> [PackageId] -> FilePath -> IO Bool
  324. checkLinkInfo dflags pkg_deps exe_file
  325. | not (platformSupportsSavingLinkOpts (platformOS (targetPlatform dflags)))
  326. -- ToDo: Windows and OS X do not use the ELF binary format, so
  327. -- readelf does not work there. We need to find another way to do
  328. -- this.
  329. = return False -- conservatively we should return True, but not
  330. -- linking in this case was the behaviour for a long
  331. -- time so we leave it as-is.
  332. | otherwise
  333. = do
  334. link_info <- getLinkInfo dflags pkg_deps
  335. debugTraceMsg dflags 3 $ text ("Link info: " ++ link_info)
  336. m_exe_link_info <- readElfSection dflags ghcLinkInfoSectionName exe_file
  337. debugTraceMsg dflags 3 $ text ("Exe link info: " ++ show m_exe_link_info)
  338. return (Just link_info /= m_exe_link_info)
  339. platformSupportsSavingLinkOpts :: OS -> Bool
  340. platformSupportsSavingLinkOpts os
  341. | os == OSSolaris2 = False -- see #5382
  342. | otherwise = osElfTarget os
  343. ghcLinkInfoSectionName :: String
  344. ghcLinkInfoSectionName = ".debug-ghc-link-info"
  345. -- if we use the ".debug" prefix, then strip will strip it by default
  346. findHSLib :: DynFlags -> [String] -> String -> IO (Maybe FilePath)
  347. findHSLib dflags dirs lib = do
  348. let batch_lib_file = if dopt Opt_Static dflags
  349. then "lib" ++ lib <.> "a"
  350. else mkSOName (targetPlatform dflags) lib
  351. found <- filterM doesFileExist (map (</> batch_lib_file) dirs)
  352. case found of
  353. [] -> return Nothing
  354. (x:_) -> return (Just x)
  355. -- -----------------------------------------------------------------------------
  356. -- Compile files in one-shot mode.
  357. oneShot :: HscEnv -> Phase -> [(String, Maybe Phase)] -> IO ()
  358. oneShot hsc_env stop_phase srcs = do
  359. o_files <- mapM (compileFile hsc_env stop_phase) srcs
  360. doLink (hsc_dflags hsc_env) stop_phase o_files
  361. compileFile :: HscEnv -> Phase -> (FilePath, Maybe Phase) -> IO FilePath
  362. compileFile hsc_env stop_phase (src, mb_phase) = do
  363. exists <- doesFileExist src
  364. when (not exists) $
  365. ghcError (CmdLineError ("does not exist: " ++ src))
  366. let
  367. dflags = hsc_dflags hsc_env
  368. split = dopt Opt_SplitObjs dflags
  369. mb_o_file = outputFile dflags
  370. ghc_link = ghcLink dflags -- Set by -c or -no-link
  371. -- When linking, the -o argument refers to the linker's output.
  372. -- otherwise, we use it as the name for the pipeline's output.
  373. output
  374. | StopLn <- stop_phase, not (isNoLink ghc_link) = Persistent
  375. -- -o foo applies to linker
  376. | Just o_file <- mb_o_file = SpecificFile o_file
  377. -- -o foo applies to the file we are compiling now
  378. | otherwise = Persistent
  379. stop_phase' = case stop_phase of
  380. As | split -> SplitAs
  381. _ -> stop_phase
  382. ( _, out_file) <- runPipeline stop_phase' hsc_env
  383. (src, mb_phase) Nothing output
  384. Nothing{-no ModLocation-} Nothing
  385. return out_file
  386. doLink :: DynFlags -> Phase -> [FilePath] -> IO ()
  387. doLink dflags stop_phase o_files
  388. | not (isStopLn stop_phase)
  389. = return () -- We stopped before the linking phase
  390. | otherwise
  391. = case ghcLink dflags of
  392. NoLink -> return ()
  393. LinkBinary -> linkBinary dflags o_files []
  394. LinkDynLib -> linkDynLib dflags o_files []
  395. other -> panicBadLink other
  396. -- ---------------------------------------------------------------------------
  397. data PipelineOutput
  398. = Temporary
  399. -- ^ Output should be to a temporary file: we're going to
  400. -- run more compilation steps on this output later.
  401. | Persistent
  402. -- ^ We want a persistent file, i.e. a file in the current directory
  403. -- derived from the input filename, but with the appropriate extension.
  404. -- eg. in "ghc -c Foo.hs" the output goes into ./Foo.o.
  405. | SpecificFile FilePath
  406. -- ^ The output must go into the specified file.
  407. -- | Run a compilation pipeline, consisting of multiple phases.
  408. --
  409. -- This is the interface to the compilation pipeline, which runs
  410. -- a series of compilation steps on a single source file, specifying
  411. -- at which stage to stop.
  412. --
  413. -- The DynFlags can be modified by phases in the pipeline (eg. by
  414. -- OPTIONS_GHC pragmas), and the changes affect later phases in the
  415. -- pipeline.
  416. runPipeline
  417. :: Phase -- ^ When to stop
  418. -> HscEnv -- ^ Compilation environment
  419. -> (FilePath,Maybe Phase) -- ^ Input filename (and maybe -x suffix)
  420. -> Maybe FilePath -- ^ original basename (if different from ^^^)
  421. -> PipelineOutput -- ^ Output filename
  422. -> Maybe ModLocation -- ^ A ModLocation, if this is a Haskell module
  423. -> Maybe FilePath -- ^ stub object, if we have one
  424. -> IO (DynFlags, FilePath) -- ^ (final flags, output filename)
  425. runPipeline stop_phase hsc_env0 (input_fn, mb_phase)
  426. mb_basename output maybe_loc maybe_stub_o
  427. = do
  428. let dflags0 = hsc_dflags hsc_env0
  429. (input_basename, suffix) = splitExtension input_fn
  430. suffix' = drop 1 suffix -- strip off the .
  431. basename | Just b <- mb_basename = b
  432. | otherwise = input_basename
  433. -- Decide where dump files should go based on the pipeline output
  434. dflags = dflags0 { dumpPrefix = Just (basename ++ ".") }
  435. hsc_env = hsc_env0 {hsc_dflags = dflags}
  436. -- If we were given a -x flag, then use that phase to start from
  437. start_phase = fromMaybe (startPhase suffix') mb_phase
  438. -- We want to catch cases of "you can't get there from here" before
  439. -- we start the pipeline, because otherwise it will just run off the
  440. -- end.
  441. --
  442. -- There is a partial ordering on phases, where A < B iff A occurs
  443. -- before B in a normal compilation pipeline.
  444. when (not (start_phase `happensBefore` stop_phase)) $
  445. ghcError (UsageError
  446. ("cannot compile this file to desired target: "
  447. ++ input_fn))
  448. -- this is a function which will be used to calculate output file names
  449. -- as we go along (we partially apply it to some of its inputs here)
  450. let get_output_fn = getOutputFilename stop_phase output basename
  451. -- Execute the pipeline...
  452. let env = PipeEnv{ stop_phase,
  453. src_basename = basename,
  454. src_suffix = suffix',
  455. output_spec = output }
  456. state = PipeState{ hsc_env, maybe_loc, maybe_stub_o = maybe_stub_o }
  457. (state', output_fn) <- unP (pipeLoop start_phase input_fn) env state
  458. let PipeState{ hsc_env=hsc_env', maybe_loc } = state'
  459. dflags' = hsc_dflags hsc_env'
  460. -- Sometimes, a compilation phase doesn't actually generate any output
  461. -- (eg. the CPP phase when -fcpp is not turned on). If we end on this
  462. -- stage, but we wanted to keep the output, then we have to explicitly
  463. -- copy the file, remembering to prepend a {-# LINE #-} pragma so that
  464. -- further compilation stages can tell what the original filename was.
  465. case output of
  466. Temporary ->
  467. return (dflags', output_fn)
  468. _other ->
  469. do final_fn <- get_output_fn dflags' stop_phase maybe_loc
  470. when (final_fn /= output_fn) $ do
  471. let msg = ("Copying `" ++ output_fn ++"' to `" ++ final_fn ++ "'")
  472. line_prag = Just ("{-# LINE 1 \"" ++ input_fn ++ "\" #-}\n")
  473. copyWithHeader dflags msg line_prag output_fn final_fn
  474. return (dflags', final_fn)
  475. -- -----------------------------------------------------------------------------
  476. -- The pipeline uses a monad to carry around various bits of information
  477. -- PipeEnv: invariant information passed down
  478. data PipeEnv = PipeEnv {
  479. stop_phase :: Phase, -- ^ Stop just before this phase
  480. src_basename :: String, -- ^ basename of original input source
  481. src_suffix :: String, -- ^ its extension
  482. output_spec :: PipelineOutput -- ^ says where to put the pipeline output
  483. }
  484. -- PipeState: information that might change during a pipeline run
  485. data PipeState = PipeState {
  486. hsc_env :: HscEnv,
  487. -- ^ only the DynFlags change in the HscEnv. The DynFlags change
  488. -- at various points, for example when we read the OPTIONS_GHC
  489. -- pragmas in the Cpp phase.
  490. maybe_loc :: Maybe ModLocation,
  491. -- ^ the ModLocation. This is discovered during compilation,
  492. -- in the Hsc phase where we read the module header.
  493. maybe_stub_o :: Maybe FilePath
  494. -- ^ the stub object. This is set by the Hsc phase if a stub
  495. -- object was created. The stub object will be joined with
  496. -- the main compilation object using "ld -r" at the end.
  497. }
  498. getPipeEnv :: CompPipeline PipeEnv
  499. getPipeEnv = P $ \env state -> return (state, env)
  500. getPipeState :: CompPipeline PipeState
  501. getPipeState = P $ \_env state -> return (state, state)
  502. instance HasDynFlags CompPipeline where
  503. getDynFlags = P $ \_env state -> return (state, hsc_dflags (hsc_env state))
  504. setDynFlags :: DynFlags -> CompPipeline ()
  505. setDynFlags dflags = P $ \_env state ->
  506. return (state{hsc_env= (hsc_env state){ hsc_dflags = dflags }}, ())
  507. setModLocation :: ModLocation -> CompPipeline ()
  508. setModLocation loc = P $ \_env state ->
  509. return (state{ maybe_loc = Just loc }, ())
  510. setStubO :: FilePath -> CompPipeline ()
  511. setStubO stub_o = P $ \_env state ->
  512. return (state{ maybe_stub_o = Just stub_o }, ())
  513. newtype CompPipeline a = P { unP :: PipeEnv -> PipeState -> IO (PipeState, a) }
  514. instance Monad CompPipeline where
  515. return a = P $ \_env state -> return (state, a)
  516. P m >>= k = P $ \env state -> do (state',a) <- m env state
  517. unP (k a) env state'
  518. io :: IO a -> CompPipeline a
  519. io m = P $ \_env state -> do a <- m; return (state, a)
  520. phaseOutputFilename :: Phase{-next phase-} -> CompPipeline FilePath
  521. phaseOutputFilename next_phase = do
  522. PipeEnv{stop_phase, src_basename, output_spec} <- getPipeEnv
  523. PipeState{maybe_loc, hsc_env} <- getPipeState
  524. let dflags = hsc_dflags hsc_env
  525. io $ getOutputFilename stop_phase output_spec
  526. src_basename dflags next_phase maybe_loc
  527. -- ---------------------------------------------------------------------------
  528. -- outer pipeline loop
  529. -- | pipeLoop runs phases until we reach the stop phase
  530. pipeLoop :: Phase -> FilePath -> CompPipeline FilePath
  531. pipeLoop phase input_fn = do
  532. PipeEnv{stop_phase} <- getPipeEnv
  533. PipeState{hsc_env} <- getPipeState
  534. case () of
  535. _ | phase `eqPhase` stop_phase -- All done
  536. -> return input_fn
  537. | not (phase `happensBefore` stop_phase)
  538. -- Something has gone wrong. We'll try to cover all the cases when
  539. -- this could happen, so if we reach here it is a panic.
  540. -- eg. it might happen if the -C flag is used on a source file that
  541. -- has {-# OPTIONS -fasm #-}.
  542. -> panic ("pipeLoop: at phase " ++ show phase ++
  543. " but I wanted to stop at phase " ++ show stop_phase)
  544. | otherwise
  545. -> do io $ debugTraceMsg (hsc_dflags hsc_env) 4
  546. (ptext (sLit "Running phase") <+> ppr phase)
  547. dflags <- getDynFlags
  548. (next_phase, output_fn) <- runPhase phase input_fn dflags
  549. pipeLoop next_phase output_fn
  550. -- -----------------------------------------------------------------------------
  551. -- In each phase, we need to know into what filename to generate the
  552. -- output. All the logic about which filenames we generate output
  553. -- into is embodied in the following function.
  554. getOutputFilename
  555. :: Phase -> PipelineOutput -> String
  556. -> DynFlags -> Phase{-next phase-} -> Maybe ModLocation -> IO FilePath
  557. getOutputFilename stop_phase output basename
  558. = func
  559. where
  560. func dflags next_phase maybe_location
  561. | is_last_phase, Persistent <- output = persistent_fn
  562. | is_last_phase, SpecificFile f <- output = return f
  563. | keep_this_output = persistent_fn
  564. | otherwise = newTempName dflags suffix
  565. where
  566. hcsuf = hcSuf dflags
  567. odir = objectDir dflags
  568. osuf = objectSuf dflags
  569. keep_hc = dopt Opt_KeepHcFiles dflags
  570. keep_s = dopt Opt_KeepSFiles dflags
  571. keep_bc = dopt Opt_KeepLlvmFiles dflags
  572. myPhaseInputExt HCc = hcsuf
  573. myPhaseInputExt MergeStub = osuf
  574. myPhaseInputExt StopLn = osuf
  575. myPhaseInputExt other = phaseInputExt other
  576. is_last_phase = next_phase `eqPhase` stop_phase
  577. -- sometimes, we keep output from intermediate stages
  578. keep_this_output =
  579. case next_phase of
  580. As | keep_s -> True
  581. LlvmOpt | keep_bc -> True
  582. HCc | keep_hc -> True
  583. _other -> False
  584. suffix = myPhaseInputExt next_phase
  585. -- persistent object files get put in odir
  586. persistent_fn
  587. | StopLn <- next_phase = return odir_persistent
  588. | otherwise = return persistent
  589. persistent = basename <.> suffix
  590. odir_persistent
  591. | Just loc <- maybe_location = ml_obj_file loc
  592. | Just d <- odir = d </> persistent
  593. | otherwise = persistent
  594. -- -----------------------------------------------------------------------------
  595. -- | Each phase in the pipeline returns the next phase to execute, and the
  596. -- name of the file in which the output was placed.
  597. --
  598. -- We must do things dynamically this way, because we often don't know
  599. -- what the rest of the phases will be until part-way through the
  600. -- compilation: for example, an {-# OPTIONS -fasm #-} at the beginning
  601. -- of a source file can change the latter stages of the pipeline from
  602. -- taking the via-C route to using the native code generator.
  603. --
  604. runPhase :: Phase -- ^ Run this phase
  605. -> FilePath -- ^ name of the input file
  606. -> DynFlags -- ^ for convenience, we pass the current dflags in
  607. -> CompPipeline (Phase, -- next phase to run
  608. FilePath) -- output filename
  609. -- Invariant: the output filename always contains the output
  610. -- Interesting case: Hsc when there is no recompilation to do
  611. -- Then the output filename is still a .o file
  612. -------------------------------------------------------------------------------
  613. -- Unlit phase
  614. runPhase (Unlit sf) input_fn dflags
  615. = do
  616. output_fn <- phaseOutputFilename (Cpp sf)
  617. let unlit_flags = getOpts dflags opt_L
  618. flags = map SysTools.Option unlit_flags ++
  619. [ -- The -h option passes the file name for unlit to
  620. -- put in a #line directive
  621. SysTools.Option "-h"
  622. , SysTools.Option $ escape $ normalise input_fn
  623. , SysTools.FileOption "" input_fn
  624. , SysTools.FileOption "" output_fn
  625. ]
  626. io $ SysTools.runUnlit dflags flags
  627. return (Cpp sf, output_fn)
  628. where
  629. -- escape the characters \, ", and ', but don't try to escape
  630. -- Unicode or anything else (so we don't use Util.charToC
  631. -- here). If we get this wrong, then in
  632. -- Coverage.addTicksToBinds where we check that the filename in
  633. -- a SrcLoc is the same as the source filenaame, the two will
  634. -- look bogusly different. See test:
  635. -- libraries/hpc/tests/function/subdir/tough2.lhs
  636. escape ('\\':cs) = '\\':'\\': escape cs
  637. escape ('\"':cs) = '\\':'\"': escape cs
  638. escape ('\'':cs) = '\\':'\'': escape cs
  639. escape (c:cs) = c : escape cs
  640. escape [] = []
  641. -------------------------------------------------------------------------------
  642. -- Cpp phase : (a) gets OPTIONS out of file
  643. -- (b) runs cpp if necessary
  644. runPhase (Cpp sf) input_fn dflags0
  645. = do
  646. src_opts <- io $ getOptionsFromFile dflags0 input_fn
  647. (dflags1, unhandled_flags, warns)
  648. <- io $ parseDynamicFilePragma dflags0 src_opts
  649. setDynFlags dflags1
  650. io $ checkProcessArgsResult dflags1 unhandled_flags
  651. if not (xopt Opt_Cpp dflags1) then do
  652. -- we have to be careful to emit warnings only once.
  653. unless (dopt Opt_Pp dflags1) $ io $ handleFlagWarnings dflags1 warns
  654. -- no need to preprocess CPP, just pass input file along
  655. -- to the next phase of the pipeline.
  656. return (HsPp sf, input_fn)
  657. else do
  658. output_fn <- phaseOutputFilename (HsPp sf)
  659. io $ doCpp dflags1 True{-raw-} False{-no CC opts-} input_fn output_fn
  660. -- re-read the pragmas now that we've preprocessed the file
  661. -- See #2464,#3457
  662. src_opts <- io $ getOptionsFromFile dflags0 output_fn
  663. (dflags2, unhandled_flags, warns)
  664. <- io $ parseDynamicFilePragma dflags0 src_opts
  665. io $ checkProcessArgsResult dflags2 unhandled_flags
  666. unless (dopt Opt_Pp dflags2) $ io $ handleFlagWarnings dflags2 warns
  667. -- the HsPp pass below will emit warnings
  668. setDynFlags dflags2
  669. return (HsPp sf, output_fn)
  670. -------------------------------------------------------------------------------
  671. -- HsPp phase
  672. runPhase (HsPp sf) input_fn dflags
  673. = do
  674. if not (dopt Opt_Pp dflags) then
  675. -- no need to preprocess, just pass input file along
  676. -- to the next phase of the pipeline.
  677. return (Hsc sf, input_fn)
  678. else do
  679. let hspp_opts = getOpts dflags opt_F
  680. PipeEnv{src_basename, src_suffix} <- getPipeEnv
  681. let orig_fn = src_basename <.> src_suffix
  682. output_fn <- phaseOutputFilename (Hsc sf)
  683. io $ SysTools.runPp dflags
  684. ( [ SysTools.Option orig_fn
  685. , SysTools.Option input_fn
  686. , SysTools.FileOption "" output_fn
  687. ] ++
  688. map SysTools.Option hspp_opts
  689. )
  690. -- re-read pragmas now that we've parsed the file (see #3674)
  691. src_opts <- io $ getOptionsFromFile dflags output_fn
  692. (dflags1, unhandled_flags, warns)
  693. <- io $ parseDynamicFilePragma dflags src_opts
  694. setDynFlags dflags1
  695. io $ checkProcessArgsResult dflags1 unhandled_flags
  696. io $ handleFlagWarnings dflags1 warns
  697. return (Hsc sf, output_fn)
  698. -----------------------------------------------------------------------------
  699. -- Hsc phase
  700. -- Compilation of a single module, in "legacy" mode (_not_ under
  701. -- the direction of the compilation manager).
  702. runPhase (Hsc src_flavour) input_fn dflags0
  703. = do -- normal Hsc mode, not mkdependHS
  704. PipeEnv{ stop_phase=stop,
  705. src_basename=basename,
  706. src_suffix=suff } <- getPipeEnv
  707. -- we add the current directory (i.e. the directory in which
  708. -- the .hs files resides) to the include path, since this is
  709. -- what gcc does, and it's probably what you want.
  710. let current_dir = takeDirectory basename
  711. paths = includePaths dflags0
  712. dflags = dflags0 { includePaths = current_dir : paths }
  713. setDynFlags dflags
  714. -- gather the imports and module name
  715. (hspp_buf,mod_name,imps,src_imps) <- io $
  716. case src_flavour of
  717. ExtCoreFile -> do -- no explicit imports in ExtCore input.
  718. m <- getCoreModuleName input_fn
  719. return (Nothing, mkModuleName m, [], [])
  720. _ -> do
  721. buf <- hGetStringBuffer input_fn
  722. (src_imps,imps,L _ mod_name) <- getImports dflags buf input_fn (basename <.> suff)
  723. return (Just buf, mod_name, imps, src_imps)
  724. -- Build a ModLocation to pass to hscMain.
  725. -- The source filename is rather irrelevant by now, but it's used
  726. -- by hscMain for messages. hscMain also needs
  727. -- the .hi and .o filenames, and this is as good a way
  728. -- as any to generate them, and better than most. (e.g. takes
  729. -- into accout the -osuf flags)
  730. location1 <- io $ mkHomeModLocation2 dflags mod_name basename suff
  731. -- Boot-ify it if necessary
  732. let location2 | isHsBoot src_flavour = addBootSuffixLocn location1
  733. | otherwise = location1
  734. -- Take -ohi into account if present
  735. -- This can't be done in mkHomeModuleLocation because
  736. -- it only applies to the module being compiles
  737. let ohi = outputHi dflags
  738. location3 | Just fn <- ohi = location2{ ml_hi_file = fn }
  739. | otherwise = location2
  740. -- Take -o into account if present
  741. -- Very like -ohi, but we must *only* do this if we aren't linking
  742. -- (If we're linking then the -o applies to the linked thing, not to
  743. -- the object file for one module.)
  744. -- Note the nasty duplication with the same computation in compileFile above
  745. let expl_o_file = outputFile dflags
  746. location4 | Just ofile <- expl_o_file
  747. , isNoLink (ghcLink dflags)
  748. = location3 { ml_obj_file = ofile }
  749. | otherwise = location3
  750. o_file = ml_obj_file location4 -- The real object file
  751. setModLocation location4
  752. -- Figure out if the source has changed, for recompilation avoidance.
  753. --
  754. -- Setting source_unchanged to True means that M.o seems
  755. -- to be up to date wrt M.hs; so no need to recompile unless imports have
  756. -- changed (which the compiler itself figures out).
  757. -- Setting source_unchanged to False tells the compiler that M.o is out of
  758. -- date wrt M.hs (or M.o doesn't exist) so we must recompile regardless.
  759. src_timestamp <- io $ getModificationUTCTime (basename <.> suff)
  760. let hsc_lang = hscTarget dflags
  761. source_unchanged <- io $
  762. if not (isStopLn stop)
  763. -- SourceModified unconditionally if
  764. -- (a) recompilation checker is off, or
  765. -- (b) we aren't going all the way to .o file (e.g. ghc -S)
  766. then return SourceModified
  767. -- Otherwise look at file modification dates
  768. else do o_file_exists <- doesFileExist o_file
  769. if not o_file_exists
  770. then return SourceModified -- Need to recompile
  771. else do t2 <- getModificationUTCTime o_file
  772. if t2 > src_timestamp
  773. then return SourceUnmodified
  774. else return SourceModified
  775. -- get the DynFlags
  776. let next_phase = hscPostBackendPhase dflags src_flavour hsc_lang
  777. output_fn <- phaseOutputFilename next_phase
  778. let dflags' = dflags { hscTarget = hsc_lang,
  779. hscOutName = output_fn,
  780. extCoreName = basename ++ ".hcr" }
  781. setDynFlags dflags'
  782. PipeState{hsc_env=hsc_env'} <- getPipeState
  783. -- Tell the finder cache about this module
  784. mod <- io $ addHomeModuleToFinder hsc_env' mod_name location4
  785. -- Make the ModSummary to hand to hscMain
  786. let
  787. mod_summary = ModSummary { ms_mod = mod,
  788. ms_hsc_src = src_flavour,
  789. ms_hspp_file = input_fn,
  790. ms_hspp_opts = dflags,
  791. ms_hspp_buf = hspp_buf,
  792. ms_location = location4,
  793. ms_hs_date = src_timestamp,
  794. ms_obj_date = Nothing,
  795. ms_textual_imps = imps,
  796. ms_srcimps = src_imps }
  797. -- run the compiler!
  798. result <- io $ hscCompileOneShot hsc_env'
  799. mod_summary source_unchanged
  800. Nothing -- No iface
  801. Nothing -- No "module i of n" progress info
  802. case result of
  803. HscNoRecomp
  804. -> do io $ touchObjectFile dflags' o_file
  805. -- The .o file must have a later modification date
  806. -- than the source file (else we wouldn't be in HscNoRecomp)
  807. -- but we touch it anyway, to keep 'make' happy (we think).
  808. return (StopLn, o_file)
  809. (HscRecomp hasStub _)
  810. -> do case hasStub of
  811. Nothing -> return ()
  812. Just stub_c ->
  813. do stub_o <- io $ compileStub hsc_env' stub_c
  814. setStubO stub_o
  815. -- In the case of hs-boot files, generate a dummy .o-boot
  816. -- stamp file for the benefit of Make
  817. when (isHsBoot src_flavour) $
  818. io $ touchObjectFile dflags' o_file
  819. return (next_phase, output_fn)
  820. -----------------------------------------------------------------------------
  821. -- Cmm phase
  822. runPhase CmmCpp input_fn dflags
  823. = do
  824. output_fn <- phaseOutputFilename Cmm
  825. io $ doCpp dflags False{-not raw-} True{-include CC opts-}
  826. input_fn output_fn
  827. return (Cmm, output_fn)
  828. runPhase Cmm input_fn dflags
  829. = do
  830. PipeEnv{src_basename} <- getPipeEnv
  831. let hsc_lang = hscTarget dflags
  832. let next_phase = hscPostBackendPhase dflags HsSrcFile hsc_lang
  833. output_fn <- phaseOutputFilename next_phase
  834. let dflags' = dflags { hscTarget = hsc_lang,
  835. hscOutName = output_fn,
  836. extCoreName = src_basename ++ ".hcr" }
  837. setDynFlags dflags'
  838. PipeState{hsc_env} <- getPipeState
  839. io $ hscCompileCmmFile hsc_env input_fn
  840. return (next_phase, output_fn)
  841. -----------------------------------------------------------------------------
  842. -- Cc phase
  843. -- we don't support preprocessing .c files (with -E) now. Doing so introduces
  844. -- way too many hacks, and I can't say I've ever used it anyway.
  845. runPhase cc_phase input_fn dflags
  846. | any (cc_phase `eqPhase`) [Cc, Ccpp, HCc, Cobjc, Cobjcpp]
  847. = do
  848. let platform = targetPlatform dflags
  849. cc_opts = getOpts dflags opt_c
  850. hcc = cc_phase `eqPhase` HCc
  851. let cmdline_include_paths = includePaths dflags
  852. -- HC files have the dependent packages stamped into them
  853. pkgs <- if hcc then io $ getHCFilePackages input_fn else return []
  854. -- add package include paths even if we're just compiling .c
  855. -- files; this is the Value Add(TM) that using ghc instead of
  856. -- gcc gives you :)
  857. pkg_include_dirs <- io $ getPackageIncludePath dflags pkgs
  858. let include_paths = foldr (\ x xs -> "-I" : x : xs) []
  859. (cmdline_include_paths ++ pkg_include_dirs)
  860. let gcc_extra_viac_flags = extraGccViaCFlags dflags
  861. let pic_c_flags = picCCOpts dflags
  862. let verbFlags = getVerbFlags dflags
  863. -- cc-options are not passed when compiling .hc files. Our
  864. -- hc code doesn't not #include any header files anyway, so these
  865. -- options aren't necessary.
  866. pkg_extra_cc_opts <- io $
  867. if cc_phase `eqPhase` HCc
  868. then return []
  869. else getPackageExtraCcOpts dflags pkgs
  870. framework_paths <-
  871. case platformOS platform of
  872. OSDarwin ->
  873. do pkgFrameworkPaths <- io $ getPackageFrameworkPath dflags pkgs
  874. let cmdlineFrameworkPaths = frameworkPaths dflags
  875. return $ map ("-F"++)
  876. (cmdlineFrameworkPaths ++ pkgFrameworkPaths)
  877. _ ->
  878. return []
  879. let split_objs = dopt Opt_SplitObjs dflags
  880. split_opt | hcc && split_objs = [ "-DUSE_SPLIT_MARKERS" ]
  881. | otherwise = [ ]
  882. let cc_opt | optLevel dflags >= 2 = "-O2"
  883. | otherwise = "-O"
  884. -- Decide next phase
  885. let next_phase = As
  886. output_fn <- phaseOutputFilename next_phase
  887. let
  888. more_hcc_opts =
  889. -- on x86 the floating point regs have greater precision
  890. -- than a double, which leads to unpredictable results.
  891. -- By default, we turn this off with -ffloat-store unless
  892. -- the user specified -fexcess-precision.
  893. (if platformArch platform == ArchX86 &&
  894. not (dopt Opt_ExcessPrecision dflags)
  895. then [ "-ffloat-store" ]
  896. else []) ++
  897. -- gcc's -fstrict-aliasing allows two accesses to memory
  898. -- to be considered non-aliasing if they have different types.
  899. -- This interacts badly with the C code we generate, which is
  900. -- very weakly typed, being derived from C--.
  901. ["-fno-strict-aliasing"]
  902. let gcc_lang_opt | cc_phase `eqPhase` Ccpp = "c++"
  903. | cc_phase `eqPhase` Cobjc = "objective-c"
  904. | cc_phase `eqPhase` Cobjcpp = "objective-c++"
  905. | otherwise = "c"
  906. io $ SysTools.runCc dflags (
  907. -- force the C compiler to interpret this file as C when
  908. -- compiling .hc files, by adding the -x c option.
  909. -- Also useful for plain .c files, just in case GHC saw a
  910. -- -x c option.
  911. [ SysTools.Option "-x", SysTools.Option gcc_lang_opt
  912. , SysTools.FileOption "" input_fn
  913. , SysTools.Option "-o"
  914. , SysTools.FileOption "" output_fn
  915. ]
  916. ++ map SysTools.Option (
  917. pic_c_flags
  918. -- Stub files generated for foreign exports references the runIO_closure
  919. -- and runNonIO_closure symbols, which are defined in the base package.
  920. -- These symbols are imported into the stub.c file via RtsAPI.h, and the
  921. -- way we do the import depends on whether we're currently compiling
  922. -- the base package or not.
  923. ++ (if platformOS platform == OSMinGW32 &&
  924. thisPackage dflags == basePackageId
  925. then [ "-DCOMPILING_BASE_PACKAGE" ]
  926. else [])
  927. -- We only support SparcV9 and better because V8 lacks an atomic CAS
  928. -- instruction. Note that the user can still override this
  929. -- (e.g., -mcpu=ultrasparc) as GCC picks the "best" -mcpu flag
  930. -- regardless of the ordering.
  931. --
  932. -- This is a temporary hack.
  933. ++ (if platformArch platform == ArchSPARC
  934. then ["-mcpu=v9"]
  935. else [])
  936. -- GCC 4.6+ doesn't like -Wimplicit when compiling C++.
  937. ++ (if (cc_phase /= Ccpp && cc_phase /= Cobjcpp)
  938. then ["-Wimplicit"]
  939. else [])
  940. ++ (if hcc
  941. then gcc_extra_viac_flags ++ more_hcc_opts
  942. else [])
  943. ++ verbFlags
  944. ++ [ "-S", cc_opt ]
  945. ++ [ "-D__GLASGOW_HASKELL__="++cProjectVersionInt ]
  946. ++ framework_paths
  947. ++ cc_opts
  948. ++ split_opt
  949. ++ include_paths
  950. ++ pkg_extra_cc_opts
  951. ))
  952. return (next_phase, output_fn)
  953. -----------------------------------------------------------------------------
  954. -- Splitting phase
  955. runPhase Splitter input_fn dflags
  956. = do -- tmp_pfx is the prefix used for the split .s files
  957. split_s_prefix <- io $ SysTools.newTempName dflags "split"
  958. let n_files_fn = split_s_prefix
  959. io $ SysTools.runSplit dflags
  960. [ SysTools.FileOption "" input_fn
  961. , SysTools.FileOption "" split_s_prefix
  962. , SysTools.FileOption "" n_files_fn
  963. ]
  964. -- Save the number of split files for future references
  965. s <- io $ readFile n_files_fn
  966. let n_files = read s :: Int
  967. dflags' = dflags { splitInfo = Just (split_s_prefix, n_files) }
  968. setDynFlags dflags'
  969. -- Remember to delete all these files
  970. io $ addFilesToClean dflags' [ split_s_prefix ++ "__" ++ show n ++ ".s"
  971. | n <- [1..n_files]]
  972. return (SplitAs,
  973. "**splitter**") -- we don't use the filename in SplitAs
  974. -----------------------------------------------------------------------------
  975. -- As, SpitAs phase : Assembler
  976. -- This is for calling the assembler on a regular assembly file (not split).
  977. runPhase As input_fn dflags
  978. = do
  979. -- LLVM from version 3.0 onwards doesn't support the OS X system
  980. -- assembler, so we use clang as the assembler instead. (#5636)
  981. let whichAsProg | hscTarget dflags == HscLlvm &&
  982. platformOS (targetPlatform dflags) == OSDarwin
  983. = do
  984. llvmVer <- io $ figureLlvmVersion dflags
  985. return $ case llvmVer of
  986. Just n | n >= 30 ->
  987. (SysTools.runClang, getOpts dflags opt_c)
  988. _ -> (SysTools.runAs, getOpts dflags opt_a)
  989. | otherwise
  990. = return (SysTools.runAs, getOpts dflags opt_a)
  991. (as_prog, as_opts) <- whichAsProg
  992. let cmdline_include_paths = includePaths dflags
  993. next_phase <- maybeMergeStub
  994. output_fn <- phaseOutputFilename next_phase
  995. -- we create directories for the object file, because it
  996. -- might be a hierarchical module.
  997. io $ createDirectoryIfMissing True (takeDirectory output_fn)