1 -----------------------------------------------------------------------------
3 -- (c) The University of Glasgow 2001-2003
5 -- Access to system tools: gcc, cp, rm etc
7 -----------------------------------------------------------------------------
10 {-# OPTIONS -fno-warn-unused-do-bind #-}
15 -- Interface to system tools
16 runUnlit, runCpp, runCc, -- [Option] -> IO ()
17 runPp, -- [Option] -> IO ()
18 runSplit, -- [Option] -> IO ()
19 runAs, runLink, -- [Option] -> IO ()
27 touch, -- String -> String -> IO ()
31 -- Temporary-file management
34 cleanTempDirs, cleanTempFiles, cleanTempFilesExcept,
41 #include "HsVersions.h"
56 import System.Environment
57 import System.FilePath
59 import System.IO.Error as IO
60 import System.Directory
63 import qualified Data.Map as Map
64 import Text.ParserCombinators.ReadP hiding (char)
65 import qualified Text.ParserCombinators.ReadP as R
67 #ifndef mingw32_HOST_OS
68 import qualified System.Posix.Internals
69 #else /* Must be Win32 */
71 import Foreign.C.String
75 import Control.Concurrent
77 import SrcLoc ( SrcLoc, mkSrcLoc, noSrcSpan, mkSrcSpan )
80 How GHC finds its files
81 ~~~~~~~~~~~~~~~~~~~~~~~
85 GHC needs various support files (library packages, RTS etc), plus
86 various auxiliary programs (cp, gcc, etc). It starts by finding topdir,
87 the root of GHC's support files
90 - ghc always has a shell wrapper that passes a -B<dir> option
93 - ghc never has a shell wrapper.
94 - we can find the location of the ghc binary, which is
95 $topdir/bin/<something>.exe
96 where <something> may be "ghc", "ghc-stage2", or similar
97 - we strip off the "bin/<something>.exe" to leave $topdir.
99 from topdir we can find package.conf, ghc-asm, etc.
102 SysTools.initSysProgs figures out exactly where all the auxiliary programs
103 are, and initialises mutable variables to make it easy to call them.
104 To to this, it makes use of definitions in Config.hs, which is a Haskell
105 file containing variables whose value is figured out by the build system.
107 Config.hs contains two sorts of things
109 cGCC, The *names* of the programs
112 etc They do *not* include paths
115 cUNLIT_DIR The *path* to the directory containing unlit, split etc
116 cSPLIT_DIR *relative* to the root of the build tree,
117 for use when running *in-place* in a build tree (only)
121 ---------------------------------------------
122 NOTES for an ALTERNATIVE scheme (i.e *not* what is currently implemented):
124 Another hair-brained scheme for simplifying the current tool location
125 nightmare in GHC: Simon originally suggested using another
126 configuration file along the lines of GCC's specs file - which is fine
127 except that it means adding code to read yet another configuration
128 file. What I didn't notice is that the current package.conf is
129 general enough to do this:
132 {name = "tools", import_dirs = [], source_dirs = [],
133 library_dirs = [], hs_libraries = [], extra_libraries = [],
134 include_dirs = [], c_includes = [], package_deps = [],
135 extra_ghc_opts = ["-pgmc/usr/bin/gcc","-pgml${topdir}/bin/unlit", ... etc.],
136 extra_cc_opts = [], extra_ld_opts = []}
138 Which would have the advantage that we get to collect together in one
139 place the path-specific package stuff with the path-specific tool
142 ---------------------------------------------
144 %************************************************************************
146 \subsection{Initialisation}
148 %************************************************************************
151 initSysTools :: Maybe String -- Maybe TopDir path (without the '-B' prefix)
152 -> IO Settings -- Set all the mutable variables above, holding
153 -- (a) the system programs
154 -- (b) the package-config file
155 -- (c) the GHC usage message
156 initSysTools mbMinusB
157 = do { top_dir <- findTopDir mbMinusB
159 -- NB: top_dir is assumed to be in standard Unix
160 -- format, '/' separated
162 ; let settingsFile = top_dir </> "settings"
163 installed :: FilePath -> FilePath
164 installed file = top_dir </> file
165 installed_mingw_bin file = top_dir </> ".." </> "mingw" </> "bin" </> file
166 installed_perl_bin file = top_dir </> ".." </> "perl" </> file
168 ; settingsStr <- readFile settingsFile
169 ; mySettings <- case maybeReadFuzzy settingsStr of
173 pgmError ("Can't parse " ++ show settingsFile)
174 ; let getSetting key = case lookup key mySettings of
177 Nothing -> pgmError ("No entry for " ++ show key ++ " in " ++ show settingsFile)
178 ; myExtraGccViaCFlags <- getSetting "GCC extra via C opts"
179 -- On Windows, mingw is distributed with GHC,
180 -- so we look in TopDir/../mingw/bin
181 -- It would perhaps be nice to be able to override this
182 -- with the settings file, but it would be a little fiddly
183 -- to make that possible, so for now you can't.
184 ; gcc_prog <- if isWindowsHost then return $ installed_mingw_bin "gcc"
185 else getSetting "C compiler command"
186 ; gcc_args_str <- if isWindowsHost then return []
187 else getSetting "C compiler flags"
188 ; let gcc_args = map Option (words gcc_args_str)
189 ; perl_path <- if isWindowsHost
190 then return $ installed_perl_bin "perl"
191 else getSetting "perl command"
193 ; let pkgconfig_path = installed "package.conf.d"
194 ghc_usage_msg_path = installed "ghc-usage.txt"
195 ghci_usage_msg_path = installed "ghci-usage.txt"
197 -- For all systems, unlit, split, mangle are GHC utilities
198 -- architecture-specific stuff is done when building Config.hs
199 unlit_path = installed cGHC_UNLIT_PGM
201 -- split is a Perl script
202 split_script = installed cGHC_SPLIT_PGM
204 windres_path = installed_mingw_bin "windres"
206 ; tmpdir <- getTemporaryDirectory
209 -- 'touch' is a GHC util for Windows
211 | isWindowsHost = installed cGHC_TOUCHY_PGM
212 | otherwise = "touch"
213 -- On Win32 we don't want to rely on #!/bin/perl, so we prepend
214 -- a call to Perl to get the invocation of split.
215 -- On Unix, scripts are invoked using the '#!' method. Binary
216 -- installations of GHC on Unix place the correct line on the
217 -- front of the script at installation time, so we don't want
218 -- to wire-in our knowledge of $(PERL) on the host system here.
219 (split_prog, split_args)
220 | isWindowsHost = (perl_path, [Option split_script])
221 | otherwise = (split_script, [])
222 (mkdll_prog, mkdll_args)
224 = panic "Can't build DLLs on a non-Win32 system"
226 (installed_mingw_bin cMKDLL, [])
228 -- cpp is derived from gcc on all platforms
229 -- HACK, see setPgmP below. We keep 'words' here to remember to fix
230 -- Config.hs one day.
231 ; let cpp_prog = gcc_prog
232 cpp_args = Option "-E"
233 : map Option (words cRAWCPP_FLAGS)
236 -- Other things being equal, as and ld are simply gcc
237 ; let as_prog = gcc_prog
242 -- We just assume on command line
243 ; let lc_prog = "llc"
246 ; return $ Settings {
247 sTmpDir = normalise tmpdir,
248 sGhcUsagePath = ghc_usage_msg_path,
249 sGhciUsagePath = ghci_usage_msg_path,
251 sRawSettings = mySettings,
252 sExtraGccViaCFlags = words myExtraGccViaCFlags,
253 sSystemPackageConfig = pkgconfig_path,
255 sPgm_P = (cpp_prog, cpp_args),
257 sPgm_c = (gcc_prog, gcc_args),
258 sPgm_s = (split_prog,split_args),
259 sPgm_a = (as_prog, as_args),
260 sPgm_l = (ld_prog, ld_args),
261 sPgm_dll = (mkdll_prog,mkdll_args),
263 sPgm_sysman = top_dir ++ "/ghc/rts/parallel/SysMan",
264 sPgm_windres = windres_path,
265 sPgm_lo = (lo_prog,[]),
266 sPgm_lc = (lc_prog,[]),
267 -- Hans: this isn't right in general, but you can
268 -- elaborate it in the same way as the others
271 then -- this list gets reversed
272 ["-D__PIC__", "-U __PIC__"]
287 -- returns a Unix-format path (relying on getBaseDir to do so too)
288 findTopDir :: Maybe String -- Maybe TopDir path (without the '-B' prefix).
289 -> IO String -- TopDir (in Unix format '/' separated)
290 findTopDir (Just minusb) = return (normalise minusb)
292 = do -- Get directory of executable
293 maybe_exec_dir <- getBaseDir
294 case maybe_exec_dir of
295 -- "Just" on Windows, "Nothing" on unix
296 Nothing -> ghcError (InstallationError "missing -B<dir> option")
297 Just dir -> return dir
301 %************************************************************************
303 \subsection{Running an external program}
305 %************************************************************************
309 runUnlit :: DynFlags -> [Option] -> IO ()
310 runUnlit dflags args = do
312 runSomething dflags "Literate pre-processor" p args
314 runCpp :: DynFlags -> [Option] -> IO ()
315 runCpp dflags args = do
316 let (p,args0) = pgm_P dflags
317 args1 = args0 ++ args
318 args2 = if dopt Opt_WarnIsError dflags
319 then Option "-Werror" : args1
321 mb_env <- getGccEnv args2
322 runSomethingFiltered dflags id "C pre-processor" p args2 mb_env
324 runPp :: DynFlags -> [Option] -> IO ()
325 runPp dflags args = do
327 runSomething dflags "Haskell pre-processor" p args
329 runCc :: DynFlags -> [Option] -> IO ()
330 runCc dflags args = do
331 let (p,args0) = pgm_c dflags
332 args1 = args0 ++ args
333 mb_env <- getGccEnv args1
334 runSomethingFiltered dflags cc_filter "C Compiler" p args1 mb_env
336 -- discard some harmless warnings from gcc that we can't turn off
337 cc_filter = unlines . doFilter . lines
340 gcc gives warnings in chunks like so:
341 In file included from /foo/bar/baz.h:11,
342 from /foo/bar/baz2.h:22,
344 /foo/flibble:14: global register variable ...
345 /foo/flibble:15: warning: call-clobbered r...
346 We break it up into its chunks, remove any call-clobbered register
347 warnings from each chunk, and then delete any chunks that we have
350 doFilter = unChunkWarnings . filterWarnings . chunkWarnings []
351 -- We can't assume that the output will start with an "In file inc..."
352 -- line, so we start off expecting a list of warnings rather than a
354 chunkWarnings :: [String] -- The location stack to use for the next
356 -> [String] -- The remaining lines to look at
357 -> [([String], [String])]
358 chunkWarnings loc_stack [] = [(loc_stack, [])]
359 chunkWarnings loc_stack xs
360 = case break loc_stack_start xs of
361 (warnings, lss:xs') ->
362 case span loc_start_continuation xs' of
364 (loc_stack, warnings) : chunkWarnings (lss : lsc) xs''
365 _ -> [(loc_stack, xs)]
367 filterWarnings :: [([String], [String])] -> [([String], [String])]
368 filterWarnings [] = []
369 -- If the warnings are already empty then we are probably doing
370 -- something wrong, so don't delete anything
371 filterWarnings ((xs, []) : zs) = (xs, []) : filterWarnings zs
372 filterWarnings ((xs, ys) : zs) = case filter wantedWarning ys of
373 [] -> filterWarnings zs
374 ys' -> (xs, ys') : filterWarnings zs
376 unChunkWarnings :: [([String], [String])] -> [String]
377 unChunkWarnings [] = []
378 unChunkWarnings ((xs, ys) : zs) = xs ++ ys ++ unChunkWarnings zs
380 loc_stack_start s = "In file included from " `isPrefixOf` s
381 loc_start_continuation s = " from " `isPrefixOf` s
383 | "warning: call-clobbered register used" `isContainedIn` w = False
386 isContainedIn :: String -> String -> Bool
387 xs `isContainedIn` ys = any (xs `isPrefixOf`) (tails ys)
389 -- If the -B<dir> option is set, add <dir> to PATH. This works around
390 -- a bug in gcc on Windows Vista where it can't find its auxiliary
391 -- binaries (see bug #1110).
392 getGccEnv :: [Option] -> IO (Maybe [(String,String)])
396 else do env <- getEnvironment
397 return (Just (map mangle_path env))
399 (b_dirs, _) = partitionWith get_b_opt opts
401 get_b_opt (Option ('-':'B':dir)) = Left dir
402 get_b_opt other = Right other
404 mangle_path (path,paths) | map toUpper path == "PATH"
405 = (path, '\"' : head b_dirs ++ "\";" ++ paths)
406 mangle_path other = other
408 runSplit :: DynFlags -> [Option] -> IO ()
409 runSplit dflags args = do
410 let (p,args0) = pgm_s dflags
411 runSomething dflags "Splitter" p (args0++args)
413 runAs :: DynFlags -> [Option] -> IO ()
414 runAs dflags args = do
415 let (p,args0) = pgm_a dflags
416 args1 = args0 ++ args
417 mb_env <- getGccEnv args1
418 runSomethingFiltered dflags id "Assembler" p args1 mb_env
420 -- | Run the LLVM Optimiser
421 runLlvmOpt :: DynFlags -> [Option] -> IO ()
422 runLlvmOpt dflags args = do
423 let (p,args0) = pgm_lo dflags
424 runSomething dflags "LLVM Optimiser" p (args0++args)
426 -- | Run the LLVM Compiler
427 runLlvmLlc :: DynFlags -> [Option] -> IO ()
428 runLlvmLlc dflags args = do
429 let (p,args0) = pgm_lc dflags
430 runSomething dflags "LLVM Compiler" p (args0++args)
432 -- | Figure out which version of LLVM we are running this session
433 figureLlvmVersion :: DynFlags -> IO (Maybe Int)
434 figureLlvmVersion dflags = do
435 let (pgm,opts) = pgm_lc dflags
436 args = filter notNull (map showOpt opts)
437 -- we grab the args even though they should be useless just in
438 -- case the user is using a customised 'llc' that requires some
439 -- of the options they've specified. llc doesn't care what other
440 -- options are specified when '-version' is used.
441 args' = args ++ ["-version"]
443 (pin, pout, perr, _) <- runInteractiveProcess pgm args'
446 Low Level Virtual Machine (http://llvm.org/):
447 llvm version 2.8 (Ubuntu 2.8-0Ubuntu1)
450 hSetBinaryMode pout False
452 vline <- hGetLine pout
453 v <- case filter isDigit vline of
454 [] -> fail "no digits!"
455 [x] -> fail $ "only 1 digit! (" ++ show x ++ ")"
456 (x:y:_) -> return ((read [x,y]) :: Int)
463 putMsg dflags $ text $ "Warning: " ++ show err
468 runLink :: DynFlags -> [Option] -> IO ()
469 runLink dflags args = do
470 let (p,args0) = pgm_l dflags
471 args1 = args0 ++ args
472 mb_env <- getGccEnv args1
473 runSomethingFiltered dflags id "Linker" p args1 mb_env
475 runMkDLL :: DynFlags -> [Option] -> IO ()
476 runMkDLL dflags args = do
477 let (p,args0) = pgm_dll dflags
478 args1 = args0 ++ args
479 mb_env <- getGccEnv (args0++args)
480 runSomethingFiltered dflags id "Make DLL" p args1 mb_env
482 runWindres :: DynFlags -> [Option] -> IO ()
483 runWindres dflags args = do
484 let (gcc, gcc_args) = pgm_c dflags
485 windres = pgm_windres dflags
486 quote x = "\"" ++ x ++ "\""
487 args' = -- If windres.exe and gcc.exe are in a directory containing
488 -- spaces then windres fails to run gcc. We therefore need
489 -- to tell it what command to use...
490 Option ("--preprocessor=" ++
491 unwords (map quote (gcc :
492 map showOpt gcc_args ++
493 ["-E", "-xc", "-DRC_INVOKED"])))
494 -- ...but if we do that then if windres calls popen then
495 -- it can't understand the quoting, so we have to use
496 -- --use-temp-file so that it interprets it correctly.
498 : Option "--use-temp-file"
500 mb_env <- getGccEnv gcc_args
501 runSomethingFiltered dflags id "Windres" windres args' mb_env
503 touch :: DynFlags -> String -> String -> IO ()
504 touch dflags purpose arg =
505 runSomething dflags purpose (pgm_T dflags) [FileOption "" arg]
507 copy :: DynFlags -> String -> FilePath -> FilePath -> IO ()
508 copy dflags purpose from to = copyWithHeader dflags purpose Nothing from to
510 copyWithHeader :: DynFlags -> String -> Maybe String -> FilePath -> FilePath
512 copyWithHeader dflags purpose maybe_header from to = do
513 showPass dflags purpose
515 hout <- openBinaryFile to WriteMode
516 hin <- openBinaryFile from ReadMode
517 ls <- hGetContents hin -- inefficient, but it'll do for now. ToDo: speed up
518 maybe (return ()) (hPutStr hout) maybe_header
523 -- | read the contents of the named section in an ELF object as a
525 readElfSection :: DynFlags -> String -> FilePath -> IO (Maybe String)
526 readElfSection _dflags section exe = do
529 args = [Option "-p", Option section, FileOption "" exe]
531 r <- readProcessWithExitCode prog (filter notNull (map showOpt args)) ""
533 (ExitSuccess, out, _err) -> return (doFilter (lines out))
536 doFilter [] = Nothing
537 doFilter (s:r) = case readP_to_S parse s of
541 skipSpaces; R.char '['; skipSpaces; string "0]"; skipSpaces;
545 %************************************************************************
547 \subsection{Managing temporary files
549 %************************************************************************
552 cleanTempDirs :: DynFlags -> IO ()
554 = unless (dopt Opt_KeepTmpFiles dflags)
555 $ do let ref = dirsToClean dflags
557 removeTmpDirs dflags (Map.elems ds)
558 writeIORef ref Map.empty
560 cleanTempFiles :: DynFlags -> IO ()
561 cleanTempFiles dflags
562 = unless (dopt Opt_KeepTmpFiles dflags)
563 $ do let ref = filesToClean dflags
565 removeTmpFiles dflags fs
568 cleanTempFilesExcept :: DynFlags -> [FilePath] -> IO ()
569 cleanTempFilesExcept dflags dont_delete
570 = unless (dopt Opt_KeepTmpFiles dflags)
571 $ do let ref = filesToClean dflags
572 files <- readIORef ref
573 let (to_keep, to_delete) = partition (`elem` dont_delete) files
574 writeIORef ref to_keep
575 removeTmpFiles dflags to_delete
578 -- find a temporary name that doesn't already exist.
579 newTempName :: DynFlags -> Suffix -> IO FilePath
580 newTempName dflags extn
581 = do d <- getTempDir dflags
583 findTempName (d </> "ghc" ++ show x ++ "_") 0
585 findTempName :: FilePath -> Integer -> IO FilePath
586 findTempName prefix x
587 = do let filename = (prefix ++ show x) <.> extn
588 b <- doesFileExist filename
589 if b then findTempName prefix (x+1)
590 else do -- clean it up later
591 consIORef (filesToClean dflags) filename
594 -- return our temporary directory within tmp_dir, creating one if we
595 -- don't have one yet
596 getTempDir :: DynFlags -> IO FilePath
598 = do let ref = dirsToClean dflags
599 tmp_dir = tmpDir dflags
600 mapping <- readIORef ref
601 case Map.lookup tmp_dir mapping of
604 let prefix = tmp_dir </> "ghc" ++ show x ++ "_"
606 mkTempDir :: Integer -> IO FilePath
608 = let dirname = prefix ++ show x
609 in do createDirectory dirname
610 let mapping' = Map.insert tmp_dir dirname mapping
611 writeIORef ref mapping'
612 debugTraceMsg dflags 2 (ptext (sLit "Created temporary directory:") <+> text dirname)
615 if isAlreadyExistsError e
621 addFilesToClean :: DynFlags -> [FilePath] -> IO ()
622 -- May include wildcards [used by DriverPipeline.run_phase SplitMangle]
623 addFilesToClean dflags files = mapM_ (consIORef (filesToClean dflags)) files
625 removeTmpDirs :: DynFlags -> [FilePath] -> IO ()
626 removeTmpDirs dflags ds
627 = traceCmd dflags "Deleting temp dirs"
628 ("Deleting: " ++ unwords ds)
629 (mapM_ (removeWith dflags removeDirectory) ds)
631 removeTmpFiles :: DynFlags -> [FilePath] -> IO ()
632 removeTmpFiles dflags fs
634 traceCmd dflags "Deleting temp files"
635 ("Deleting: " ++ unwords deletees)
636 (mapM_ (removeWith dflags removeFile) deletees)
638 -- Flat out refuse to delete files that are likely to be source input
639 -- files (is there a worse bug than having a compiler delete your source
642 -- Deleting source files is a sign of a bug elsewhere, so prominently flag
645 | null non_deletees = act
647 putMsg dflags (text "WARNING - NOT deleting source files:" <+> hsep (map text non_deletees))
650 (non_deletees, deletees) = partition isHaskellUserSrcFilename fs
652 removeWith :: DynFlags -> (FilePath -> IO ()) -> FilePath -> IO ()
653 removeWith dflags remover f = remover f `catchIO`
655 let msg = if isDoesNotExistError e
656 then ptext (sLit "Warning: deleting non-existent") <+> text f
657 else ptext (sLit "Warning: exception raised when deleting")
660 in debugTraceMsg dflags 2 msg
663 -----------------------------------------------------------------------------
664 -- Running an external program
666 runSomething :: DynFlags
667 -> String -- For -v message
668 -> String -- Command name (possibly a full path)
669 -- assumed already dos-ified
670 -> [Option] -- Arguments
671 -- runSomething will dos-ify them
674 runSomething dflags phase_name pgm args =
675 runSomethingFiltered dflags id phase_name pgm args Nothing
678 :: DynFlags -> (String->String) -> String -> String -> [Option]
679 -> Maybe [(String,String)] -> IO ()
681 runSomethingFiltered dflags filter_fn phase_name pgm args mb_env = do
682 let real_args = filter notNull (map showOpt args)
683 #if __GLASGOW_HASKELL__ >= 701
684 cmdLine = showCommandForUser pgm real_args
686 cmdLine = unwords (pgm:real_args)
688 traceCmd dflags phase_name cmdLine $ do
689 (exit_code, doesn'tExist) <-
691 rc <- builderMainLoop dflags filter_fn pgm real_args mb_env
693 ExitSuccess{} -> return (rc, False)
695 -- rawSystem returns (ExitFailure 127) if the exec failed for any
696 -- reason (eg. the program doesn't exist). This is the only clue
697 -- we have, but we need to report something to the user because in
698 -- the case of a missing program there will otherwise be no output
700 | n == 127 -> return (rc, True)
701 | otherwise -> return (rc, False))
702 -- Should 'rawSystem' generate an IO exception indicating that
703 -- 'pgm' couldn't be run rather than a funky return code, catch
704 -- this here (the win32 version does this, but it doesn't hurt
705 -- to test for this in general.)
707 if IO.isDoesNotExistError err
708 then return (ExitFailure 1, True)
710 case (doesn'tExist, exit_code) of
711 (True, _) -> ghcError (InstallationError ("could not execute: " ++ pgm))
712 (_, ExitSuccess) -> return ()
713 _ -> ghcError (PhaseFailed phase_name exit_code)
715 builderMainLoop :: DynFlags -> (String -> String) -> FilePath
716 -> [String] -> Maybe [(String, String)]
718 builderMainLoop dflags filter_fn pgm real_args mb_env = do
720 (hStdIn, hStdOut, hStdErr, hProcess) <- runInteractiveProcess pgm real_args Nothing mb_env
722 -- and run a loop piping the output from the compiler to the log_action in DynFlags
723 hSetBuffering hStdOut LineBuffering
724 hSetBuffering hStdErr LineBuffering
725 _ <- forkIO (readerProc chan hStdOut filter_fn)
726 _ <- forkIO (readerProc chan hStdErr filter_fn)
727 -- we don't want to finish until 2 streams have been completed
728 -- (stdout and stderr)
729 -- nor until 1 exit code has been retrieved.
730 rc <- loop chan hProcess (2::Integer) (1::Integer) ExitSuccess
731 -- after that, we're done here.
737 -- status starts at zero, and increments each time either
738 -- a reader process gets EOF, or the build proc exits. We wait
739 -- for all of these to happen (status==3).
740 -- ToDo: we should really have a contingency plan in case any of
741 -- the threads dies, such as a timeout.
742 loop _ _ 0 0 exitcode = return exitcode
743 loop chan hProcess t p exitcode = do
745 then getProcessExitCode hProcess
748 Just code -> loop chan hProcess t (p-1) code
754 log_action dflags SevInfo noSrcSpan defaultUserStyle msg
755 loop chan hProcess t p exitcode
756 BuildError loc msg -> do
757 log_action dflags SevError (mkSrcSpan loc loc) defaultUserStyle msg
758 loop chan hProcess t p exitcode
760 loop chan hProcess (t-1) p exitcode
761 | otherwise -> loop chan hProcess t p exitcode
763 readerProc :: Chan BuildMessage -> Handle -> (String -> String) -> IO ()
764 readerProc chan hdl filter_fn =
765 (do str <- hGetContents hdl
766 loop (linesPlatform (filter_fn str)) Nothing)
769 -- ToDo: check errors more carefully
770 -- ToDo: in the future, the filter should be implemented as
771 -- a stream transformer.
773 loop [] Nothing = return ()
774 loop [] (Just err) = writeChan chan err
777 Just err@(BuildError srcLoc msg)
778 | leading_whitespace l -> do
779 loop ls (Just (BuildError srcLoc (msg $$ text l)))
785 _ -> panic "readerProc/loop"
788 = case parseError l of
790 writeChan chan (BuildMsg (text l))
792 Just (file, lineNum, colNum, msg) -> do
793 let srcLoc = mkSrcLoc (mkFastString file) lineNum colNum
794 loop ls (Just (BuildError srcLoc (text msg)))
796 leading_whitespace [] = False
797 leading_whitespace (x:_) = isSpace x
799 parseError :: String -> Maybe (String, Int, Int, String)
800 parseError s0 = case breakColon s0 of
801 Just (filename, s1) ->
802 case breakIntColon s1 of
803 Just (lineNum, s2) ->
804 case breakIntColon s2 of
805 Just (columnNum, s3) ->
806 Just (filename, lineNum, columnNum, s3)
808 Just (filename, lineNum, 0, s2)
812 breakColon :: String -> Maybe (String, String)
813 breakColon xs = case break (':' ==) xs of
814 (ys, _:zs) -> Just (ys, zs)
817 breakIntColon :: String -> Maybe (Int, String)
818 breakIntColon xs = case break (':' ==) xs of
820 | not (null ys) && all isAscii ys && all isDigit ys ->
826 | BuildError !SrcLoc !SDoc
829 traceCmd :: DynFlags -> String -> String -> IO () -> IO ()
830 -- trace the command (at two levels of verbosity)
831 traceCmd dflags phase_name cmd_line action
832 = do { let verb = verbosity dflags
833 ; showPass dflags phase_name
834 ; debugTraceMsg dflags 3 (text cmd_line)
838 ; action `catchIO` handle_exn verb
841 handle_exn _verb exn = do { debugTraceMsg dflags 2 (char '\n')
842 ; debugTraceMsg dflags 2 (ptext (sLit "Failed:") <+> text cmd_line <+> text (show exn))
843 ; ghcError (PhaseFailed phase_name (ExitFailure 1)) }
846 %************************************************************************
848 \subsection{Support code}
850 %************************************************************************
853 -----------------------------------------------------------------------------
854 -- Define getBaseDir :: IO (Maybe String)
856 getBaseDir :: IO (Maybe String)
857 #if defined(mingw32_HOST_OS)
858 -- Assuming we are running ghc, accessed by path $(stuff)/bin/ghc.exe,
859 -- return the path $(stuff)/lib.
860 getBaseDir = try_size 2048 -- plenty, PATH_MAX is 512 under Win32.
862 try_size size = allocaArray (fromIntegral size) $ \buf -> do
863 ret <- c_GetModuleFileName nullPtr buf size
866 _ | ret < size -> fmap (Just . rootDir) $ peekCWString buf
867 | otherwise -> try_size (size * 2)
869 rootDir s = case splitFileName $ normalise s of
871 | lower ghc_exe `elem` ["ghc.exe",
875 case splitFileName $ takeDirectory d of
876 -- ghc is in $topdir/bin/ghc.exe
877 (d', bin) | lower bin == "bin" -> takeDirectory d' </> "lib"
880 where fail = panic ("can't decompose ghc.exe path: " ++ show s)
883 foreign import stdcall unsafe "windows.h GetModuleFileNameW"
884 c_GetModuleFileName :: Ptr () -> CWString -> Word32 -> IO Word32
886 getBaseDir = return Nothing
889 #ifdef mingw32_HOST_OS
890 foreign import ccall unsafe "_getpid" getProcessID :: IO Int -- relies on Int == Int32 on Windows
892 getProcessID :: IO Int
893 getProcessID = System.Posix.Internals.c_getpid >>= return . fromIntegral
896 -- Divvy up text stream into lines, taking platform dependent
897 -- line termination into account.
898 linesPlatform :: String -> [String]
899 #if !defined(mingw32_HOST_OS)
900 linesPlatform ls = lines ls
902 linesPlatform "" = []
905 (as,xs1) -> as : linesPlatform xs1
907 lineBreak "" = ("","")
908 lineBreak ('\r':'\n':xs) = ([],xs)
909 lineBreak ('\n':xs) = ([],xs)
910 lineBreak (x:xs) = let (as,bs) = lineBreak xs in (x:as,bs)