1 -----------------------------------------------------------------------------
3 -- (c) The University of Glasgow 2001-2003
5 -- Access to system tools: gcc, cp, rm etc
7 -----------------------------------------------------------------------------
14 -- Interface to system tools
15 runUnlit, runCpp, runCc, -- [Option] -> IO ()
16 runPp, -- [Option] -> IO ()
17 runMangle, runSplit, -- [Option] -> IO ()
18 runAs, runLink, -- [Option] -> IO ()
22 touch, -- String -> String -> IO ()
27 -- Temporary-file management
30 cleanTempDirs, cleanTempFiles, cleanTempFilesExcept,
37 #include "HsVersions.h"
52 import System.Environment
53 import System.FilePath
55 import System.IO.Error as IO
56 import System.Directory
60 #ifndef mingw32_HOST_OS
61 import qualified System.Posix.Internals
62 #else /* Must be Win32 */
64 import Foreign.C.String
67 import System.Process ( runInteractiveProcess, getProcessExitCode )
68 import Control.Concurrent
70 import SrcLoc ( SrcLoc, mkSrcLoc, noSrcSpan, mkSrcSpan )
73 How GHC finds its files
74 ~~~~~~~~~~~~~~~~~~~~~~~
78 GHC needs various support files (library packages, RTS etc), plus
79 various auxiliary programs (cp, gcc, etc). It starts by finding topdir,
80 the root of GHC's support files
83 - ghc always has a shell wrapper that passes a -B<dir> option
86 - ghc never has a shell wrapper.
87 - we can find the location of the ghc binary, which is
88 $topdir/bin/<something>.exe
89 where <something> may be "ghc", "ghc-stage2", or similar
90 - we strip off the "bin/<something>.exe" to leave $topdir.
92 from topdir we can find package.conf, ghc-asm, etc.
95 SysTools.initSysProgs figures out exactly where all the auxiliary programs
96 are, and initialises mutable variables to make it easy to call them.
97 To to this, it makes use of definitions in Config.hs, which is a Haskell
98 file containing variables whose value is figured out by the build system.
100 Config.hs contains two sorts of things
102 cGCC, The *names* of the programs
105 etc They do *not* include paths
108 cUNLIT_DIR The *path* to the directory containing unlit, split etc
109 cSPLIT_DIR *relative* to the root of the build tree,
110 for use when running *in-place* in a build tree (only)
114 ---------------------------------------------
115 NOTES for an ALTERNATIVE scheme (i.e *not* what is currently implemented):
117 Another hair-brained scheme for simplifying the current tool location
118 nightmare in GHC: Simon originally suggested using another
119 configuration file along the lines of GCC's specs file - which is fine
120 except that it means adding code to read yet another configuration
121 file. What I didn't notice is that the current package.conf is
122 general enough to do this:
125 {name = "tools", import_dirs = [], source_dirs = [],
126 library_dirs = [], hs_libraries = [], extra_libraries = [],
127 include_dirs = [], c_includes = [], package_deps = [],
128 extra_ghc_opts = ["-pgmc/usr/bin/gcc","-pgml${topdir}/bin/unlit", ... etc.],
129 extra_cc_opts = [], extra_ld_opts = []}
131 Which would have the advantage that we get to collect together in one
132 place the path-specific package stuff with the path-specific tool
135 ---------------------------------------------
137 %************************************************************************
139 \subsection{Initialisation}
141 %************************************************************************
144 initSysTools :: Maybe String -- Maybe TopDir path (without the '-B' prefix)
147 -> IO DynFlags -- Set all the mutable variables above, holding
148 -- (a) the system programs
149 -- (b) the package-config file
150 -- (c) the GHC usage message
153 initSysTools mbMinusB dflags0
154 = do { top_dir <- findTopDir mbMinusB
156 -- NB: top_dir is assumed to be in standard Unix
157 -- format, '/' separated
159 ; let installed :: FilePath -> FilePath
160 installed file = top_dir </> file
161 installed_mingw_bin file = top_dir </> ".." </> "mingw" </> "bin" </> file
163 ; let pkgconfig_path = installed "package.conf.d"
164 ghc_usage_msg_path = installed "ghc-usage.txt"
165 ghci_usage_msg_path = installed "ghci-usage.txt"
167 -- For all systems, unlit, split, mangle are GHC utilities
168 -- architecture-specific stuff is done when building Config.hs
169 unlit_path = installed cGHC_UNLIT_PGM
171 -- split and mangle are Perl scripts
172 split_script = installed cGHC_SPLIT_PGM
173 mangle_script = installed cGHC_MANGLER_PGM
175 windres_path = installed_mingw_bin "windres"
177 ; tmpdir <- getTemporaryDirectory
178 ; let dflags1 = setTmpDir tmpdir dflags0
180 -- On Windows, mingw is distributed with GHC,
181 -- so we look in TopDir/../mingw/bin
184 | isWindowsHost = installed_mingw_bin "gcc"
187 | isWindowsHost = installed cGHC_PERL
188 | otherwise = cGHC_PERL
189 -- 'touch' is a GHC util for Windows
191 | isWindowsHost = installed cGHC_TOUCHY_PGM
192 | otherwise = "touch"
193 -- On Win32 we don't want to rely on #!/bin/perl, so we prepend
194 -- a call to Perl to get the invocation of split and mangle.
195 -- On Unix, scripts are invoked using the '#!' method. Binary
196 -- installations of GHC on Unix place the correct line on the
197 -- front of the script at installation time, so we don't want
198 -- to wire-in our knowledge of $(PERL) on the host system here.
199 (split_prog, split_args)
200 | isWindowsHost = (perl_path, [Option split_script])
201 | otherwise = (split_script, [])
202 (mangle_prog, mangle_args)
203 | isWindowsHost = (perl_path, [Option mangle_script])
204 | otherwise = (mangle_script, [])
205 (mkdll_prog, mkdll_args)
207 = panic "Can't build DLLs on a non-Win32 system"
209 (installed_mingw_bin cMKDLL, [])
211 -- cpp is derived from gcc on all platforms
212 -- HACK, see setPgmP below. We keep 'words' here to remember to fix
213 -- Config.hs one day.
214 ; let cpp_path = (gcc_prog,
215 (Option "-E"):(map Option (words cRAWCPP_FLAGS)))
217 -- Other things being equal, as and ld are simply gcc
218 ; let as_prog = gcc_prog
222 ghcUsagePath = ghc_usage_msg_path,
223 ghciUsagePath = ghci_usage_msg_path,
225 systemPackageConfig = pkgconfig_path,
229 pgm_c = (gcc_prog,[]),
230 pgm_m = (mangle_prog,mangle_args),
231 pgm_s = (split_prog,split_args),
232 pgm_a = (as_prog,[]),
233 pgm_l = (ld_prog,[]),
234 pgm_dll = (mkdll_prog,mkdll_args),
236 pgm_sysman = top_dir ++ "/ghc/rts/parallel/SysMan",
237 pgm_windres = windres_path
238 -- Hans: this isn't right in general, but you can
239 -- elaborate it in the same way as the others
245 -- returns a Unix-format path (relying on getBaseDir to do so too)
246 findTopDir :: Maybe String -- Maybe TopDir path (without the '-B' prefix).
247 -> IO String -- TopDir (in Unix format '/' separated)
248 findTopDir (Just minusb) = return (normalise minusb)
250 = do -- Get directory of executable
251 maybe_exec_dir <- getBaseDir
252 case maybe_exec_dir of
253 -- "Just" on Windows, "Nothing" on unix
254 Nothing -> ghcError (InstallationError "missing -B<dir> option")
255 Just dir -> return dir
259 %************************************************************************
261 \subsection{Running an external program}
263 %************************************************************************
267 runUnlit :: DynFlags -> [Option] -> IO ()
268 runUnlit dflags args = do
270 runSomething dflags "Literate pre-processor" p args
272 runCpp :: DynFlags -> [Option] -> IO ()
273 runCpp dflags args = do
274 let (p,args0) = pgm_P dflags
275 args1 = args0 ++ args
276 args2 = if dopt Opt_WarnIsError dflags
277 then Option "-Werror" : args1
279 mb_env <- getGccEnv args2
280 runSomethingFiltered dflags id "C pre-processor" p args2 mb_env
282 runPp :: DynFlags -> [Option] -> IO ()
283 runPp dflags args = do
285 runSomething dflags "Haskell pre-processor" p args
287 runCc :: DynFlags -> [Option] -> IO ()
288 runCc dflags args = do
289 let (p,args0) = pgm_c dflags
290 args1 = args0 ++ args
291 mb_env <- getGccEnv args1
292 runSomethingFiltered dflags cc_filter "C Compiler" p args1 mb_env
294 -- discard some harmless warnings from gcc that we can't turn off
295 cc_filter = unlines . doFilter . lines
298 gcc gives warnings in chunks like so:
299 In file included from /foo/bar/baz.h:11,
300 from /foo/bar/baz2.h:22,
302 /foo/flibble:14: global register variable ...
303 /foo/flibble:15: warning: call-clobbered r...
304 We break it up into its chunks, remove any call-clobbered register
305 warnings from each chunk, and then delete any chunks that we have
308 doFilter = unChunkWarnings . filterWarnings . chunkWarnings []
309 -- We can't assume that the output will start with an "In file inc..."
310 -- line, so we start off expecting a list of warnings rather than a
312 chunkWarnings :: [String] -- The location stack to use for the next
314 -> [String] -- The remaining lines to look at
315 -> [([String], [String])]
316 chunkWarnings loc_stack [] = [(loc_stack, [])]
317 chunkWarnings loc_stack xs
318 = case break loc_stack_start xs of
319 (warnings, lss:xs') ->
320 case span loc_start_continuation xs' of
322 (loc_stack, warnings) : chunkWarnings (lss : lsc) xs''
323 _ -> [(loc_stack, xs)]
325 filterWarnings :: [([String], [String])] -> [([String], [String])]
326 filterWarnings [] = []
327 -- If the warnings are already empty then we are probably doing
328 -- something wrong, so don't delete anything
329 filterWarnings ((xs, []) : zs) = (xs, []) : filterWarnings zs
330 filterWarnings ((xs, ys) : zs) = case filter wantedWarning ys of
331 [] -> filterWarnings zs
332 ys' -> (xs, ys') : filterWarnings zs
334 unChunkWarnings :: [([String], [String])] -> [String]
335 unChunkWarnings [] = []
336 unChunkWarnings ((xs, ys) : zs) = xs ++ ys ++ unChunkWarnings zs
338 loc_stack_start s = "In file included from " `isPrefixOf` s
339 loc_start_continuation s = " from " `isPrefixOf` s
341 | "warning: call-clobbered register used" `isContainedIn` w = False
344 isContainedIn :: String -> String -> Bool
345 xs `isContainedIn` ys = any (xs `isPrefixOf`) (tails ys)
347 -- If the -B<dir> option is set, add <dir> to PATH. This works around
348 -- a bug in gcc on Windows Vista where it can't find its auxiliary
349 -- binaries (see bug #1110).
350 getGccEnv :: [Option] -> IO (Maybe [(String,String)])
354 else do env <- getEnvironment
355 return (Just (map mangle_path env))
357 (b_dirs, _) = partitionWith get_b_opt opts
359 get_b_opt (Option ('-':'B':dir)) = Left dir
360 get_b_opt other = Right other
362 mangle_path (path,paths) | map toUpper path == "PATH"
363 = (path, '\"' : head b_dirs ++ "\";" ++ paths)
364 mangle_path other = other
366 runMangle :: DynFlags -> [Option] -> IO ()
367 runMangle dflags args = do
368 let (p,args0) = pgm_m dflags
369 runSomething dflags "Mangler" p (args0++args)
371 runSplit :: DynFlags -> [Option] -> IO ()
372 runSplit dflags args = do
373 let (p,args0) = pgm_s dflags
374 runSomething dflags "Splitter" p (args0++args)
376 runAs :: DynFlags -> [Option] -> IO ()
377 runAs dflags args = do
378 let (p,args0) = pgm_a dflags
379 args1 = args0 ++ args
380 mb_env <- getGccEnv args1
381 runSomethingFiltered dflags id "Assembler" p args1 mb_env
383 runLink :: DynFlags -> [Option] -> IO ()
384 runLink dflags args = do
385 let (p,args0) = pgm_l dflags
386 args1 = args0 ++ args
387 mb_env <- getGccEnv args1
388 runSomethingFiltered dflags id "Linker" p args1 mb_env
390 runMkDLL :: DynFlags -> [Option] -> IO ()
391 runMkDLL dflags args = do
392 let (p,args0) = pgm_dll dflags
393 args1 = args0 ++ args
394 mb_env <- getGccEnv (args0++args)
395 runSomethingFiltered dflags id "Make DLL" p args1 mb_env
397 runWindres :: DynFlags -> [Option] -> IO ()
398 runWindres dflags args = do
399 let (gcc, gcc_args) = pgm_c dflags
400 windres = pgm_windres dflags
401 quote x = "\"" ++ x ++ "\""
402 args' = -- If windres.exe and gcc.exe are in a directory containing
403 -- spaces then windres fails to run gcc. We therefore need
404 -- to tell it what command to use...
405 Option ("--preprocessor=" ++
406 unwords (map quote (gcc :
407 map showOpt gcc_args ++
408 ["-E", "-xc", "-DRC_INVOKED"])))
409 -- ...but if we do that then if windres calls popen then
410 -- it can't understand the quoting, so we have to use
411 -- --use-temp-file so that it interprets it correctly.
413 : Option "--use-temp-file"
415 mb_env <- getGccEnv gcc_args
416 runSomethingFiltered dflags id "Windres" windres args' mb_env
418 touch :: DynFlags -> String -> String -> IO ()
419 touch dflags purpose arg =
420 runSomething dflags purpose (pgm_T dflags) [FileOption "" arg]
422 copy :: DynFlags -> String -> FilePath -> FilePath -> IO ()
423 copy dflags purpose from to = copyWithHeader dflags purpose Nothing from to
425 copyWithHeader :: DynFlags -> String -> Maybe String -> FilePath -> FilePath
427 copyWithHeader dflags purpose maybe_header from to = do
428 showPass dflags purpose
430 hout <- openBinaryFile to WriteMode
431 hin <- openBinaryFile from ReadMode
432 ls <- hGetContents hin -- inefficient, but it'll do for now. ToDo: speed up
433 maybe (return ()) (hPutStr hout) maybe_header
438 getExtraViaCOpts :: DynFlags -> IO [String]
439 getExtraViaCOpts dflags = do
440 f <- readFile (topDir dflags </> "extra-gcc-opts")
444 %************************************************************************
446 \subsection{Managing temporary files
448 %************************************************************************
451 cleanTempDirs :: DynFlags -> IO ()
453 = unless (dopt Opt_KeepTmpFiles dflags)
454 $ do let ref = dirsToClean dflags
456 removeTmpDirs dflags (eltsFM ds)
457 writeIORef ref emptyFM
459 cleanTempFiles :: DynFlags -> IO ()
460 cleanTempFiles dflags
461 = unless (dopt Opt_KeepTmpFiles dflags)
462 $ do let ref = filesToClean dflags
464 removeTmpFiles dflags fs
467 cleanTempFilesExcept :: DynFlags -> [FilePath] -> IO ()
468 cleanTempFilesExcept dflags dont_delete
469 = unless (dopt Opt_KeepTmpFiles dflags)
470 $ do let ref = filesToClean dflags
471 files <- readIORef ref
472 let (to_keep, to_delete) = partition (`elem` dont_delete) files
473 removeTmpFiles dflags to_delete
474 writeIORef ref to_keep
477 -- find a temporary name that doesn't already exist.
478 newTempName :: DynFlags -> Suffix -> IO FilePath
479 newTempName dflags extn
480 = do d <- getTempDir dflags
482 findTempName (d ++ "/ghc" ++ show x ++ "_") 0
484 findTempName :: FilePath -> Integer -> IO FilePath
485 findTempName prefix x
486 = do let filename = (prefix ++ show x) <.> extn
487 b <- doesFileExist filename
488 if b then findTempName prefix (x+1)
489 else do -- clean it up later
490 consIORef (filesToClean dflags) filename
493 -- return our temporary directory within tmp_dir, creating one if we
494 -- don't have one yet
495 getTempDir :: DynFlags -> IO FilePath
496 getTempDir dflags@(DynFlags{tmpDir=tmp_dir})
497 = do let ref = dirsToClean dflags
498 mapping <- readIORef ref
499 case lookupFM mapping tmp_dir of
502 let prefix = tmp_dir ++ "/ghc" ++ show x ++ "_"
504 mkTempDir :: Integer -> IO FilePath
506 = let dirname = prefix ++ show x
507 in do createDirectory dirname
508 let mapping' = addToFM mapping tmp_dir dirname
509 writeIORef ref mapping'
510 debugTraceMsg dflags 2 (ptext (sLit "Created temporary directory:") <+> text dirname)
513 if isAlreadyExistsError e
519 addFilesToClean :: DynFlags -> [FilePath] -> IO ()
520 -- May include wildcards [used by DriverPipeline.run_phase SplitMangle]
521 addFilesToClean dflags files = mapM_ (consIORef (filesToClean dflags)) files
523 removeTmpDirs :: DynFlags -> [FilePath] -> IO ()
524 removeTmpDirs dflags ds
525 = traceCmd dflags "Deleting temp dirs"
526 ("Deleting: " ++ unwords ds)
527 (mapM_ (removeWith dflags removeDirectory) ds)
529 removeTmpFiles :: DynFlags -> [FilePath] -> IO ()
530 removeTmpFiles dflags fs
532 traceCmd dflags "Deleting temp files"
533 ("Deleting: " ++ unwords deletees)
534 (mapM_ (removeWith dflags removeFile) deletees)
536 -- Flat out refuse to delete files that are likely to be source input
537 -- files (is there a worse bug than having a compiler delete your source
540 -- Deleting source files is a sign of a bug elsewhere, so prominently flag
543 | null non_deletees = act
545 putMsg dflags (text "WARNING - NOT deleting source files:" <+> hsep (map text non_deletees))
548 (non_deletees, deletees) = partition isHaskellUserSrcFilename fs
550 removeWith :: DynFlags -> (FilePath -> IO ()) -> FilePath -> IO ()
551 removeWith dflags remover f = remover f `IO.catch`
553 let msg = if isDoesNotExistError e
554 then ptext (sLit "Warning: deleting non-existent") <+> text f
555 else ptext (sLit "Warning: exception raised when deleting")
558 in debugTraceMsg dflags 2 msg
561 -----------------------------------------------------------------------------
562 -- Running an external program
564 runSomething :: DynFlags
565 -> String -- For -v message
566 -> String -- Command name (possibly a full path)
567 -- assumed already dos-ified
568 -> [Option] -- Arguments
569 -- runSomething will dos-ify them
572 runSomething dflags phase_name pgm args =
573 runSomethingFiltered dflags id phase_name pgm args Nothing
576 :: DynFlags -> (String->String) -> String -> String -> [Option]
577 -> Maybe [(String,String)] -> IO ()
579 runSomethingFiltered dflags filter_fn phase_name pgm args mb_env = do
580 let real_args = filter notNull (map showOpt args)
581 traceCmd dflags phase_name (unwords (pgm:real_args)) $ do
582 (exit_code, doesn'tExist) <-
584 rc <- builderMainLoop dflags filter_fn pgm real_args mb_env
586 ExitSuccess{} -> return (rc, False)
588 -- rawSystem returns (ExitFailure 127) if the exec failed for any
589 -- reason (eg. the program doesn't exist). This is the only clue
590 -- we have, but we need to report something to the user because in
591 -- the case of a missing program there will otherwise be no output
593 | n == 127 -> return (rc, True)
594 | otherwise -> return (rc, False))
595 -- Should 'rawSystem' generate an IO exception indicating that
596 -- 'pgm' couldn't be run rather than a funky return code, catch
597 -- this here (the win32 version does this, but it doesn't hurt
598 -- to test for this in general.)
600 if IO.isDoesNotExistError err
601 then return (ExitFailure 1, True)
603 case (doesn'tExist, exit_code) of
604 (True, _) -> ghcError (InstallationError ("could not execute: " ++ pgm))
605 (_, ExitSuccess) -> return ()
606 _ -> ghcError (PhaseFailed phase_name exit_code)
608 builderMainLoop :: DynFlags -> (String -> String) -> FilePath
609 -> [String] -> Maybe [(String, String)]
611 builderMainLoop dflags filter_fn pgm real_args mb_env = do
613 (hStdIn, hStdOut, hStdErr, hProcess) <- runInteractiveProcess pgm real_args Nothing mb_env
615 -- and run a loop piping the output from the compiler to the log_action in DynFlags
616 hSetBuffering hStdOut LineBuffering
617 hSetBuffering hStdErr LineBuffering
618 _ <- forkIO (readerProc chan hStdOut filter_fn)
619 _ <- forkIO (readerProc chan hStdErr filter_fn)
620 -- we don't want to finish until 2 streams have been completed
621 -- (stdout and stderr)
622 -- nor until 1 exit code has been retrieved.
623 rc <- loop chan hProcess (2::Integer) (1::Integer) ExitSuccess
624 -- after that, we're done here.
630 -- status starts at zero, and increments each time either
631 -- a reader process gets EOF, or the build proc exits. We wait
632 -- for all of these to happen (status==3).
633 -- ToDo: we should really have a contingency plan in case any of
634 -- the threads dies, such as a timeout.
635 loop _ _ 0 0 exitcode = return exitcode
636 loop chan hProcess t p exitcode = do
638 then getProcessExitCode hProcess
641 Just code -> loop chan hProcess t (p-1) code
647 log_action dflags SevInfo noSrcSpan defaultUserStyle msg
648 loop chan hProcess t p exitcode
649 BuildError loc msg -> do
650 log_action dflags SevError (mkSrcSpan loc loc) defaultUserStyle msg
651 loop chan hProcess t p exitcode
653 loop chan hProcess (t-1) p exitcode
654 | otherwise -> loop chan hProcess t p exitcode
656 readerProc :: Chan BuildMessage -> Handle -> (String -> String) -> IO ()
657 readerProc chan hdl filter_fn =
658 (do str <- hGetContents hdl
659 loop (linesPlatform (filter_fn str)) Nothing)
662 -- ToDo: check errors more carefully
663 -- ToDo: in the future, the filter should be implemented as
664 -- a stream transformer.
666 loop [] Nothing = return ()
667 loop [] (Just err) = writeChan chan err
670 Just err@(BuildError srcLoc msg)
671 | leading_whitespace l -> do
672 loop ls (Just (BuildError srcLoc (msg $$ text l)))
678 _ -> panic "readerProc/loop"
681 = case parseError l of
683 writeChan chan (BuildMsg (text l))
685 Just (file, lineNum, colNum, msg) -> do
686 let srcLoc = mkSrcLoc (mkFastString file) lineNum colNum
687 loop ls (Just (BuildError srcLoc (text msg)))
689 leading_whitespace [] = False
690 leading_whitespace (x:_) = isSpace x
692 parseError :: String -> Maybe (String, Int, Int, String)
693 parseError s0 = case breakColon s0 of
694 Just (filename, s1) ->
695 case breakIntColon s1 of
696 Just (lineNum, s2) ->
697 case breakIntColon s2 of
698 Just (columnNum, s3) ->
699 Just (filename, lineNum, columnNum, s3)
701 Just (filename, lineNum, 0, s2)
705 breakColon :: String -> Maybe (String, String)
706 breakColon xs = case break (':' ==) xs of
707 (ys, _:zs) -> Just (ys, zs)
710 breakIntColon :: String -> Maybe (Int, String)
711 breakIntColon xs = case break (':' ==) xs of
713 | not (null ys) && all isAscii ys && all isDigit ys ->
719 | BuildError !SrcLoc !SDoc
722 traceCmd :: DynFlags -> String -> String -> IO () -> IO ()
723 -- a) trace the command (at two levels of verbosity)
724 -- b) don't do it at all if dry-run is set
725 traceCmd dflags phase_name cmd_line action
726 = do { let verb = verbosity dflags
727 ; showPass dflags phase_name
728 ; debugTraceMsg dflags 3 (text cmd_line)
732 ; unless (dopt Opt_DryRun dflags) $ do {
735 ; action `IO.catch` handle_exn verb
738 handle_exn _verb exn = do { debugTraceMsg dflags 2 (char '\n')
739 ; debugTraceMsg dflags 2 (ptext (sLit "Failed:") <+> text cmd_line <+> text (show exn))
740 ; ghcError (PhaseFailed phase_name (ExitFailure 1)) }
743 %************************************************************************
745 \subsection{Support code}
747 %************************************************************************
750 -----------------------------------------------------------------------------
751 -- Define getBaseDir :: IO (Maybe String)
753 getBaseDir :: IO (Maybe String)
754 #if defined(mingw32_HOST_OS)
755 -- Assuming we are running ghc, accessed by path $(stuff)/bin/ghc.exe,
756 -- return the path $(stuff)/lib.
757 getBaseDir = do let len = (2048::Int) -- plenty, PATH_MAX is 512 under Win32.
758 buf <- mallocArray len
759 ret <- getModuleFileName nullPtr buf len
760 if ret == 0 then free buf >> return Nothing
761 else do s <- peekCString buf
763 return (Just (rootDir s))
765 rootDir s = case splitFileName $ normalise s of
767 | lower ghc_exe `elem` ["ghc.exe",
771 case splitFileName $ takeDirectory d of
772 -- ghc is in $topdir/bin/ghc.exe
773 (d', bin) | lower bin == "bin" -> takeDirectory d' </> "lib"
776 where fail = panic ("can't decompose ghc.exe path: " ++ show s)
779 foreign import stdcall unsafe "GetModuleFileNameA"
780 getModuleFileName :: Ptr () -> CString -> Int -> IO Int32
782 getBaseDir = return Nothing
785 #ifdef mingw32_HOST_OS
786 foreign import ccall unsafe "_getpid" getProcessID :: IO Int -- relies on Int == Int32 on Windows
788 getProcessID :: IO Int
789 getProcessID = System.Posix.Internals.c_getpid >>= return . fromIntegral
792 -- Divvy up text stream into lines, taking platform dependent
793 -- line termination into account.
794 linesPlatform :: String -> [String]
795 #if !defined(mingw32_HOST_OS)
796 linesPlatform ls = lines ls
798 linesPlatform "" = []
801 (as,xs1) -> as : linesPlatform xs1
803 lineBreak "" = ("","")
804 lineBreak ('\r':'\n':xs) = ([],xs)
805 lineBreak ('\n':xs) = ([],xs)
806 lineBreak (x:xs) = let (as,bs) = lineBreak xs in (x:as,bs)