1 -----------------------------------------------------------------------------
3 -- (c) The University of Glasgow 2001
5 -- Access to system tools: gcc, cp, rm etc
7 -----------------------------------------------------------------------------
14 setPgmL, -- String -> IO ()
26 -- Command-line override
29 getTopDir, -- IO String -- The value of $libdir
30 getPackageConfigPath, -- IO String -- Where package.conf is
32 -- Interface to system tools
33 runUnlit, runCpp, runCc, -- [Option] -> IO ()
34 runPp, -- [Option] -> IO ()
35 runMangle, runSplit, -- [Option] -> IO ()
36 runAs, runLink, -- [Option] -> IO ()
39 runIlx2il, runIlasm, -- [String] -> IO ()
43 touch, -- String -> String -> IO ()
44 copy, -- String -> String -> String -> IO ()
45 unDosifyPath, -- String -> String
47 -- Temporary-file management
50 cleanTempFiles, cleanTempFilesExcept, removeTmpFiles,
54 getProcessID, -- IO Int
55 system, -- String -> IO ExitCode
58 showGhcUsage, -- IO () Shows usage message and exits
59 getSysMan, -- IO String Parallel system only
65 #include "HsVersions.h"
70 import Panic ( progName, GhcException(..) )
71 import Util ( global, notNull )
72 import CmdLineOpts ( dynFlag, verbosity )
74 import EXCEPTION ( throwDyn )
75 import DATA_IOREF ( IORef, readIORef, writeIORef )
78 import Monad ( when, unless )
79 import System ( ExitCode(..), exitWith, getEnv, system )
80 import IO ( try, catch,
81 openFile, hPutChar, hPutStrLn, hPutStr, hClose, hFlush, IOMode(..),
83 import Directory ( doesFileExist, removeFile )
84 import List ( intersperse )
86 #include "../includes/config.h"
88 -- GHC <= 4.08 didn't have rawSystem, and runs into problems with long command
89 -- lines on mingw32, so we disallow it now.
90 #if defined(mingw32_HOST_OS) && (__GLASGOW_HASKELL__ <= 408)
91 #error GHC <= 4.08 is not supported for bootstrapping GHC on i386-unknown-mingw32
94 #ifndef mingw32_HOST_OS
95 #if __GLASGOW_HASKELL__ > 504
96 import qualified System.Posix.Internals
97 import System.Posix.Process ( executeFile, getProcessStatus, forkProcess, ProcessStatus(..))
98 import System.Posix.Signals ( installHandler, sigCHLD, sigCONT, Handler(..) )
100 import qualified Posix
101 import Posix ( executeFile, getProcessStatus, forkProcess, ProcessStatus(..), installHandler,
102 sigCHLD, sigCONT, Handler(..) )
104 #else /* Must be Win32 */
105 import List ( isPrefixOf )
106 import Util ( dropList )
108 import CString ( CString, peekCString )
111 #ifdef mingw32_HOST_OS
112 #if __GLASGOW_HASKELL__ > 504
113 import System.Cmd ( rawSystem )
115 import SystemExts ( rawSystem )
118 #else /* Not Win32 */
120 import System ( system )
125 The configuration story
126 ~~~~~~~~~~~~~~~~~~~~~~~
128 GHC needs various support files (library packages, RTS etc), plus
129 various auxiliary programs (cp, gcc, etc). It finds these in one
132 * When running as an *installed program*, GHC finds most of this support
133 stuff in the installed library tree. The path to this tree is passed
134 to GHC via the -B flag, and given to initSysTools .
136 * When running *in-place* in a build tree, GHC finds most of this support
137 stuff in the build tree. The path to the build tree is, again passed
140 GHC tells which of the two is the case by seeing whether package.conf
141 is in TopDir [installed] or in TopDir/ghc/driver [inplace] (what a hack).
144 SysTools.initSysProgs figures out exactly where all the auxiliary programs
145 are, and initialises mutable variables to make it easy to call them.
146 To to this, it makes use of definitions in Config.hs, which is a Haskell
147 file containing variables whose value is figured out by the build system.
149 Config.hs contains two sorts of things
151 cGCC, The *names* of the programs
154 etc They do *not* include paths
157 cUNLIT_DIR_REL The *path* to the directory containing unlit, split etc
158 cSPLIT_DIR_REL *relative* to the root of the build tree,
159 for use when running *in-place* in a build tree (only)
163 ---------------------------------------------
164 NOTES for an ALTERNATIVE scheme (i.e *not* what is currently implemented):
166 Another hair-brained scheme for simplifying the current tool location
167 nightmare in GHC: Simon originally suggested using another
168 configuration file along the lines of GCC's specs file - which is fine
169 except that it means adding code to read yet another configuration
170 file. What I didn't notice is that the current package.conf is
171 general enough to do this:
174 {name = "tools", import_dirs = [], source_dirs = [],
175 library_dirs = [], hs_libraries = [], extra_libraries = [],
176 include_dirs = [], c_includes = [], package_deps = [],
177 extra_ghc_opts = ["-pgmc/usr/bin/gcc","-pgml${libdir}/bin/unlit", ... etc.],
178 extra_cc_opts = [], extra_ld_opts = []}
180 Which would have the advantage that we get to collect together in one
181 place the path-specific package stuff with the path-specific tool
184 ---------------------------------------------
187 %************************************************************************
189 \subsection{Global variables to contain system programs}
191 %************************************************************************
193 All these pathnames are maintained IN THE NATIVE FORMAT OF THE HOST MACHINE.
194 (See remarks under pathnames below)
197 GLOBAL_VAR(v_Pgm_L, error "pgm_L", String) -- unlit
198 GLOBAL_VAR(v_Pgm_P, error "pgm_P", (String,[Option])) -- cpp
199 GLOBAL_VAR(v_Pgm_F, error "pgm_F", String) -- pp
200 GLOBAL_VAR(v_Pgm_c, error "pgm_c", String) -- gcc
201 GLOBAL_VAR(v_Pgm_m, error "pgm_m", String) -- asm code mangler
202 GLOBAL_VAR(v_Pgm_s, error "pgm_s", String) -- asm code splitter
203 GLOBAL_VAR(v_Pgm_a, error "pgm_a", String) -- as
205 GLOBAL_VAR(v_Pgm_I, error "pgm_I", String) -- ilx2il
206 GLOBAL_VAR(v_Pgm_i, error "pgm_i", String) -- ilasm
208 GLOBAL_VAR(v_Pgm_l, error "pgm_l", String) -- ld
209 GLOBAL_VAR(v_Pgm_MkDLL, error "pgm_dll", String) -- mkdll
211 GLOBAL_VAR(v_Pgm_T, error "pgm_T", String) -- touch
212 GLOBAL_VAR(v_Pgm_CP, error "pgm_CP", String) -- cp
214 GLOBAL_VAR(v_Path_package_config, error "path_package_config", String)
215 GLOBAL_VAR(v_Path_usage, error "ghc_usage.txt", String)
217 GLOBAL_VAR(v_TopDir, error "TopDir", String) -- -B<dir>
219 -- Parallel system only
220 GLOBAL_VAR(v_Pgm_sysman, error "pgm_sysman", String) -- system manager
222 -- ways to get at some of these variables from outside this module
223 getPackageConfigPath = readIORef v_Path_package_config
224 getTopDir = readIORef v_TopDir
228 %************************************************************************
230 \subsection{Initialisation}
232 %************************************************************************
235 initSysTools :: [String] -- Command-line arguments starting "-B"
237 -> IO () -- Set all the mutable variables above, holding
238 -- (a) the system programs
239 -- (b) the package-config file
240 -- (c) the GHC usage message
243 initSysTools minusB_args
244 = do { (am_installed, top_dir) <- findTopDir minusB_args
245 ; writeIORef v_TopDir top_dir
247 -- for "installed" this is the root of GHC's support files
248 -- for "in-place" it is the root of the build tree
249 -- NB: top_dir is assumed to be in standard Unix format '/' separated
251 ; let installed, installed_bin :: FilePath -> FilePath
252 installed_bin pgm = pgmPath top_dir pgm
253 installed file = pgmPath top_dir file
254 inplace dir pgm = pgmPath (top_dir `slash`
255 cPROJECT_DIR `slash` dir) pgm
258 | am_installed = installed "package.conf"
259 | otherwise = inplace cGHC_DRIVER_DIR_REL "package.conf.inplace"
262 | am_installed = installed "ghc-usage.txt"
263 | otherwise = inplace cGHC_DRIVER_DIR_REL "ghc-usage.txt"
265 -- For all systems, unlit, split, mangle are GHC utilities
266 -- architecture-specific stuff is done when building Config.hs
268 | am_installed = installed_bin cGHC_UNLIT_PGM
269 | otherwise = inplace cGHC_UNLIT_DIR_REL cGHC_UNLIT_PGM
271 -- split and mangle are Perl scripts
273 | am_installed = installed_bin cGHC_SPLIT_PGM
274 | otherwise = inplace cGHC_SPLIT_DIR_REL cGHC_SPLIT_PGM
277 | am_installed = installed_bin cGHC_MANGLER_PGM
278 | otherwise = inplace cGHC_MANGLER_DIR_REL cGHC_MANGLER_PGM
280 #ifndef mingw32_HOST_OS
281 -- check whether TMPDIR is set in the environment
282 ; IO.try (do dir <- getEnv "TMPDIR" -- fails if not set
287 -- On Win32, consult GetTempPath() for a temp dir.
288 -- => it first tries TMP, TEMP, then finally the
289 -- Windows directory(!). The directory is in short-path
290 -- form and *does* have a trailing backslash.
292 let len = (2048::Int)
293 buf <- mallocArray len
294 ret <- getTempPath len buf
297 -- failed, consult TEMP.
305 -- strip the trailing backslash (awful, but
306 -- we only do this once).
316 -- Check that the package config exists
317 ; config_exists <- doesFileExist pkgconfig_path
318 ; when (not config_exists) $
319 throwDyn (InstallationError
320 ("Can't find package.conf as " ++ pkgconfig_path))
322 #if defined(mingw32_HOST_OS)
323 -- WINDOWS-SPECIFIC STUFF
324 -- On Windows, gcc and friends are distributed with GHC,
325 -- so when "installed" we look in TopDir/bin
326 -- When "in-place" we look wherever the build-time configure
328 -- When "install" we tell gcc where its specs file + exes are (-B)
329 -- and also some places to pick up include files. We need
330 -- to be careful to put all necessary exes in the -B place
331 -- (as, ld, cc1, etc) since if they don't get found there, gcc
332 -- then tries to run unadorned "as", "ld", etc, and will
333 -- pick up whatever happens to be lying around in the path,
334 -- possibly including those from a cygwin install on the target,
335 -- which is exactly what we're trying to avoid.
336 ; let gcc_path | am_installed = installed_bin ("gcc -B\"" ++ installed "gcc-lib/\"")
338 -- The trailing "/" is absolutely essential; gcc seems
339 -- to construct file names simply by concatenating to this
340 -- -B path with no extra slash
341 -- We use "/" rather than "\\" because otherwise "\\\" is mangled
342 -- later on; although gcc_path is in NATIVE format, gcc can cope
343 -- (see comments with declarations of global variables)
345 -- The quotes round the -B argument are in case TopDir has spaces in it
347 perl_path | am_installed = installed_bin cGHC_PERL
348 | otherwise = cGHC_PERL
350 -- 'touch' is a GHC util for Windows, and similarly unlit, mangle
351 ; let touch_path | am_installed = installed_bin cGHC_TOUCHY_PGM
352 | otherwise = inplace cGHC_TOUCHY_DIR_REL cGHC_TOUCHY_PGM
354 -- On Win32 we don't want to rely on #!/bin/perl, so we prepend
355 -- a call to Perl to get the invocation of split and mangle
356 ; let split_path = perl_path ++ " \"" ++ split_script ++ "\""
357 mangle_path = perl_path ++ " \"" ++ mangle_script ++ "\""
360 | am_installed = pgmPath (installed "gcc-lib/") cMKDLL ++
361 " --dlltool-name " ++ pgmPath (installed "gcc-lib/") "dlltool" ++
362 " --driver-name " ++ gcc_path
365 -- UNIX-SPECIFIC STUFF
366 -- On Unix, the "standard" tools are assumed to be
367 -- in the same place whether we are running "in-place" or "installed"
368 -- That place is wherever the build-time configure script found them.
369 ; let gcc_path = cGCC
371 mkdll_path = panic "Can't build DLLs on a non-Win32 system"
373 -- On Unix, scripts are invoked using the '#!' method. Binary
374 -- installations of GHC on Unix place the correct line on the front
375 -- of the script at installation time, so we don't want to wire-in
376 -- our knowledge of $(PERL) on the host system here.
377 ; let split_path = split_script
378 mangle_path = mangle_script
381 -- cpp is derived from gcc on all platforms
382 -- HACK, see setPgmP below. We keep 'words' here to remember to fix
383 -- Config.hs one day.
384 ; let cpp_path = (gcc_path, (Option "-E"):(map Option (words cRAWCPP_FLAGS)))
386 -- For all systems, copy and remove are provided by the host
387 -- system; architecture-specific stuff is done when building Config.hs
388 ; let cp_path = cGHC_CP
390 -- Other things being equal, as and ld are simply gcc
391 ; let as_path = gcc_path
395 -- ilx2il and ilasm are specified in Config.hs
396 ; let ilx2il_path = cILX2IL
400 -- Initialise the global vars
401 ; writeIORef v_Path_package_config pkgconfig_path
402 ; writeIORef v_Path_usage ghc_usage_msg_path
404 ; writeIORef v_Pgm_sysman (top_dir ++ "/ghc/rts/parallel/SysMan")
405 -- Hans: this isn't right in general, but you can
406 -- elaborate it in the same way as the others
408 ; writeIORef v_Pgm_L unlit_path
409 ; writeIORef v_Pgm_P cpp_path
410 ; writeIORef v_Pgm_F ""
411 ; writeIORef v_Pgm_c gcc_path
412 ; writeIORef v_Pgm_m mangle_path
413 ; writeIORef v_Pgm_s split_path
414 ; writeIORef v_Pgm_a as_path
416 ; writeIORef v_Pgm_I ilx2il_path
417 ; writeIORef v_Pgm_i ilasm_path
419 ; writeIORef v_Pgm_l ld_path
420 ; writeIORef v_Pgm_MkDLL mkdll_path
421 ; writeIORef v_Pgm_T touch_path
422 ; writeIORef v_Pgm_CP cp_path
427 #if defined(mingw32_HOST_OS)
428 foreign import stdcall "GetTempPathA" unsafe getTempPath :: Int -> CString -> IO Int32
432 The various setPgm functions are called when a command-line option
437 is used to override a particular program with a new one
440 setPgmL = writeIORef v_Pgm_L
441 -- XXX HACK: Prelude> words "'does not' work" ===> ["'does","not'","work"]
442 -- Config.hs should really use Option.
443 setPgmP arg = let (pgm:args) = words arg in writeIORef v_Pgm_P (pgm,map Option args)
444 setPgmF = writeIORef v_Pgm_F
445 setPgmc = writeIORef v_Pgm_c
446 setPgmm = writeIORef v_Pgm_m
447 setPgms = writeIORef v_Pgm_s
448 setPgma = writeIORef v_Pgm_a
449 setPgml = writeIORef v_Pgm_l
451 setPgmI = writeIORef v_Pgm_I
452 setPgmi = writeIORef v_Pgm_i
459 -- for "installed" this is the root of GHC's support files
460 -- for "in-place" it is the root of the build tree
463 -- 1. Set proto_top_dir
464 -- a) look for (the last) -B flag, and use it
465 -- b) if there are no -B flags, get the directory
466 -- where GHC is running (only on Windows)
468 -- 2. If package.conf exists in proto_top_dir, we are running
469 -- installed; and TopDir = proto_top_dir
471 -- 3. Otherwise we are running in-place, so
472 -- proto_top_dir will be /...stuff.../ghc/compiler
473 -- Set TopDir to /...stuff..., which is the root of the build tree
475 -- This is very gruesome indeed
477 findTopDir :: [String]
478 -> IO (Bool, -- True <=> am installed, False <=> in-place
479 String) -- TopDir (in Unix format '/' separated)
482 = do { top_dir <- get_proto
483 -- Discover whether we're running in a build tree or in an installation,
484 -- by looking for the package configuration file.
485 ; am_installed <- doesFileExist (top_dir `slash` "package.conf")
487 ; return (am_installed, top_dir)
490 -- get_proto returns a Unix-format path (relying on getExecDir to do so too)
491 get_proto | notNull minusbs
492 = return (unDosifyPath (drop 2 (last minusbs))) -- 2 for "-B"
494 = do { maybe_exec_dir <- getExecDir -- Get directory of executable
495 ; case maybe_exec_dir of -- (only works on Windows;
496 -- returns Nothing on Unix)
497 Nothing -> throwDyn (InstallationError "missing -B<dir> option")
498 Just dir -> return dir
503 %************************************************************************
505 \subsection{Command-line options}
507 %************************************************************************
509 When invoking external tools as part of the compilation pipeline, we
510 pass these a sequence of options on the command-line. Rather than
511 just using a list of Strings, we use a type that allows us to distinguish
512 between filepaths and 'other stuff'. [The reason being, of course, that
513 this type gives us a handle on transforming filenames, and filenames only,
514 to whatever format they're expected to be on a particular platform.]
518 = FileOption -- an entry that _contains_ filename(s) / filepaths.
519 String -- a non-filepath prefix that shouldn't be transformed (e.g., "/out="
520 String -- the filepath/filename portion
523 showOptions :: [Option] -> String
524 showOptions ls = unwords (map (quote.showOpt) ls)
526 showOpt (FileOption pre f) = pre ++ dosifyPath f
527 showOpt (Option "") = ""
528 showOpt (Option s) = s
533 %************************************************************************
535 \subsection{Running an external program}
537 %************************************************************************
541 runUnlit :: [Option] -> IO ()
542 runUnlit args = do p <- readIORef v_Pgm_L
543 runSomething "Literate pre-processor" p args
545 runCpp :: [Option] -> IO ()
546 runCpp args = do (p,baseArgs) <- readIORef v_Pgm_P
547 runSomething "C pre-processor" p (baseArgs ++ args)
549 runPp :: [Option] -> IO ()
550 runPp args = do p <- readIORef v_Pgm_F
551 runSomething "Haskell pre-processor" p args
553 runCc :: [Option] -> IO ()
554 runCc args = do p <- readIORef v_Pgm_c
555 runSomething "C Compiler" p args
557 runMangle :: [Option] -> IO ()
558 runMangle args = do p <- readIORef v_Pgm_m
559 runSomething "Mangler" p args
561 runSplit :: [Option] -> IO ()
562 runSplit args = do p <- readIORef v_Pgm_s
563 runSomething "Splitter" p args
565 runAs :: [Option] -> IO ()
566 runAs args = do p <- readIORef v_Pgm_a
567 runSomething "Assembler" p args
569 runLink :: [Option] -> IO ()
570 runLink args = do p <- readIORef v_Pgm_l
571 runSomething "Linker" p args
574 runIlx2il :: [Option] -> IO ()
575 runIlx2il args = do p <- readIORef v_Pgm_I
576 runSomething "Ilx2Il" p args
578 runIlasm :: [Option] -> IO ()
579 runIlasm args = do p <- readIORef v_Pgm_i
580 runSomething "Ilasm" p args
583 runMkDLL :: [Option] -> IO ()
584 runMkDLL args = do p <- readIORef v_Pgm_MkDLL
585 runSomething "Make DLL" p args
587 touch :: String -> String -> IO ()
588 touch purpose arg = do p <- readIORef v_Pgm_T
589 runSomething purpose p [FileOption "" arg]
591 copy :: String -> String -> String -> IO ()
592 copy purpose from to = do
593 verb <- dynFlag verbosity
594 when (verb >= 2) $ hPutStrLn stderr ("*** " ++ purpose)
596 h <- openFile to WriteMode
597 ls <- readFile from -- inefficient, but it'll do for now.
598 -- ToDo: speed up via slurping.
604 getSysMan :: IO String -- How to invoke the system manager
605 -- (parallel system only)
606 getSysMan = readIORef v_Pgm_sysman
609 %************************************************************************
611 \subsection{GHC Usage message}
613 %************************************************************************
615 Show the usage message and exit
618 showGhcUsage = do { usage_path <- readIORef v_Path_usage
619 ; usage <- readFile usage_path
621 ; exitWith ExitSuccess }
624 dump ('$':'$':s) = hPutStr stderr progName >> dump s
625 dump (c:s) = hPutChar stderr c >> dump s
629 %************************************************************************
631 \subsection{Managing temporary files
633 %************************************************************************
636 GLOBAL_VAR(v_FilesToClean, [], [String] )
637 GLOBAL_VAR(v_TmpDir, cDEFAULT_TMPDIR, String )
638 -- v_TmpDir has no closing '/'
642 setTmpDir dir = writeIORef v_TmpDir dir
644 cleanTempFiles :: Int -> IO ()
645 cleanTempFiles verb = do fs <- readIORef v_FilesToClean
646 removeTmpFiles verb fs
648 cleanTempFilesExcept :: Int -> [FilePath] -> IO ()
649 cleanTempFilesExcept verb dont_delete
650 = do fs <- readIORef v_FilesToClean
651 let leftovers = filter (`notElem` dont_delete) fs
652 removeTmpFiles verb leftovers
653 writeIORef v_FilesToClean dont_delete
656 -- find a temporary name that doesn't already exist.
657 newTempName :: Suffix -> IO FilePath
659 = do x <- getProcessID
660 tmp_dir <- readIORef v_TmpDir
661 findTempName tmp_dir x
663 findTempName tmp_dir x
664 = do let filename = tmp_dir ++ "/ghc" ++ show x ++ '.':extn
665 b <- doesFileExist filename
666 if b then findTempName tmp_dir (x+1)
667 else do add v_FilesToClean filename -- clean it up later
670 addFilesToClean :: [FilePath] -> IO ()
671 -- May include wildcards [used by DriverPipeline.run_phase SplitMangle]
672 addFilesToClean files = mapM_ (add v_FilesToClean) files
674 removeTmpFiles :: Int -> [FilePath] -> IO ()
675 removeTmpFiles verb fs
676 = traceCmd "Deleting temp files"
677 ("Deleting: " ++ unwords fs)
680 rm f = removeFile f `IO.catch`
683 hPutStrLn stderr ("Warning: deleting non-existent " ++ f)
689 %************************************************************************
691 \subsection{Running a program}
693 %************************************************************************
696 GLOBAL_VAR(v_Dry_run, False, Bool)
699 setDryRun = writeIORef v_Dry_run True
701 -----------------------------------------------------------------------------
702 -- Running an external program
704 runSomething :: String -- For -v message
705 -> String -- Command name (possibly a full path)
706 -- assumed already dos-ified
707 -> [Option] -- Arguments
708 -- runSomething will dos-ify them
711 runSomething phase_name pgm args
712 = traceCmd phase_name (concat (intersperse " " (pgm:quoteargs))) $
714 #ifndef mingw32_HOST_OS
716 exit_code <- case mpid of
717 Nothing -> do -- Child
718 executeFile pgm True quoteargs Nothing
719 exitWith (ExitFailure 127)
722 Just child -> do -- Parent
723 #if __GLASGOW_HASKELL__ <= 504
724 -- avoid interaction with broken getProcessStatus-FFI:
725 oldHandler <- installHandler sigCONT Ignore Nothing
727 Just (Exited res) <- getProcessStatus True False child
728 #if __GLASGOW_HASKELL__ <= 504
730 installHandler sigCONT oldHandler Nothing
735 exit_code <- rawSystem cmd_line
737 when (exit_code /= ExitSuccess)
738 $ throwDyn (PhaseFailed phase_name exit_code)
741 -- The pgm is already in native format (appropriate dir separators)
742 cmd_line = pgm ++ ' ':showOptions args
743 -- unwords (pgm : dosifyPaths (map quote args))
744 quoteargs = filter (not.null) (map showOpt args)
746 traceCmd :: String -> String -> IO () -> IO ()
747 -- a) trace the command (at two levels of verbosity)
748 -- b) don't do it at all if dry-run is set
749 traceCmd phase_name cmd_line action
750 = do { verb <- dynFlag verbosity
751 ; when (verb >= 2) $ hPutStrLn stderr ("*** " ++ phase_name)
752 ; when (verb >= 3) $ hPutStrLn stderr cmd_line
756 ; n <- readIORef v_Dry_run
760 ; action `IO.catch` handle_exn verb
763 handle_exn verb exn = do { when (verb >= 2) (hPutStr stderr "\n")
764 ; when (verb >= 3) (hPutStrLn stderr ("Failed: " ++ cmd_line ++ (show exn)))
765 ; throwDyn (PhaseFailed phase_name (ExitFailure 1)) }
769 %************************************************************************
771 \subsection{Path names}
773 %************************************************************************
775 We maintain path names in Unix form ('/'-separated) right until
776 the last moment. On Windows we dos-ify them just before passing them
777 to the Windows command.
779 The alternative, of using '/' consistently on Unix and '\' on Windows,
780 proved quite awkward. There were a lot more calls to dosifyPath,
781 and even on Windows we might invoke a unix-like utility (eg 'sh'), which
782 interpreted a command line 'foo\baz' as 'foobaz'.
785 -----------------------------------------------------------------------------
786 -- Convert filepath into MSDOS form.
788 dosifyPaths :: [String] -> [String]
789 -- dosifyPaths does two things
790 -- a) change '/' to '\'
791 -- b) remove initial '/cygdrive/'
793 unDosifyPath :: String -> String
794 -- Just change '\' to '/'
796 pgmPath :: String -- Directory string in Unix format
797 -> String -- Program name with no directory separators
799 -> String -- Program invocation string in native format
803 #if defined(mingw32_HOST_OS)
805 --------------------- Windows version ------------------
806 dosifyPaths xs = map dosifyPath xs
808 unDosifyPath xs = subst '\\' '/' xs
810 pgmPath dir pgm = dosifyPath dir ++ '\\' : pgm
813 = subst '/' '\\' real_stuff
815 -- fully convince myself that /cygdrive/ prefixes cannot
816 -- really appear here.
817 cygdrive_prefix = "/cygdrive/"
820 | cygdrive_prefix `isPrefixOf` stuff = dropList cygdrive_prefix stuff
825 --------------------- Unix version ---------------------
828 pgmPath dir pgm = dir ++ '/' : pgm
829 dosifyPath stuff = stuff
830 --------------------------------------------------------
833 subst a b ls = map (\ x -> if x == a then b else x) ls
837 -----------------------------------------------------------------------------
838 Path name construction
841 slash :: String -> String -> String
842 absPath, relPath :: [String] -> String
845 relPath xs = foldr1 slash xs
847 absPath xs = "" `slash` relPath xs
849 slash s1 s2 = s1 ++ ('/' : s2)
853 %************************************************************************
855 \subsection{Support code}
857 %************************************************************************
860 -----------------------------------------------------------------------------
861 -- Define getExecDir :: IO (Maybe String)
863 #if defined(mingw32_HOST_OS)
864 getExecDir :: IO (Maybe String)
865 getExecDir = do let len = (2048::Int) -- plenty, PATH_MAX is 512 under Win32.
866 buf <- mallocArray len
867 ret <- getModuleFileName nullPtr buf len
868 if ret == 0 then free buf >> return Nothing
869 else do s <- peekCString buf
871 return (Just (reverse (dropList "/bin/ghc.exe" (reverse (unDosifyPath s)))))
874 foreign import stdcall "GetModuleFileNameA" unsafe
875 getModuleFileName :: Ptr () -> CString -> Int -> IO Int32
877 getExecDir :: IO (Maybe String) = do return Nothing
880 #ifdef mingw32_HOST_OS
881 foreign import ccall "_getpid" unsafe getProcessID :: IO Int -- relies on Int == Int32 on Windows
882 #elif __GLASGOW_HASKELL__ > 504
883 getProcessID :: IO Int
884 getProcessID = System.Posix.Internals.c_getpid >>= return . fromIntegral
886 getProcessID :: IO Int
887 getProcessID = Posix.getProcessID
890 quote :: String -> String
892 quote s = "\"" ++ s ++ "\""