1 -----------------------------------------------------------------------------
3 -- (c) The University of Glasgow 2001
5 -- Access to system tools: gcc, cp, rm etc
7 -----------------------------------------------------------------------------
13 setPgm, -- String -> IO ()
14 -- Command-line override
17 getTopDir, -- IO String -- The value of $libdir
18 getPackageConfigPath, -- IO String -- Where package.conf is
20 -- Interface to system tools
21 runUnlit, runCpp, runCc, -- [Option] -> IO ()
22 runMangle, runSplit, -- [Option] -> IO ()
23 runAs, runLink, -- [Option] -> IO ()
26 runIlx2il, runIlasm, -- [String] -> IO ()
30 touch, -- String -> String -> IO ()
31 copy, -- String -> String -> String -> IO ()
32 unDosifyPath, -- String -> String
34 -- Temporary-file management
37 cleanTempFiles, cleanTempFilesExcept, removeTmpFiles,
41 getProcessID, -- IO Int
42 system, -- String -> IO ExitCode
45 showGhcUsage, -- IO () Shows usage message and exits
46 getSysMan, -- IO String Parallel system only
55 import Panic ( progName, GhcException(..) )
56 import Util ( global )
57 import CmdLineOpts ( dynFlag, verbosity )
59 import Exception ( throwDyn, catchAllIO )
61 import Directory ( doesFileExist, removeFile )
62 import IOExts ( IORef, readIORef, writeIORef )
63 import Monad ( when, unless )
64 import System ( ExitCode(..), exitWith, getEnv, system )
69 #include "../includes/config.h"
71 #ifndef mingw32_TARGET_OS
72 import qualified Posix
74 import List ( isPrefixOf )
78 -- This is a kludge for bootstrapping with 4.08.X. Given that
79 -- all distributed compilers >= 5.0 will be compiled with themselves.
80 -- I don't think this kludge is a problem. And we have to start
81 -- building with >= 5.0 on Win32 anyway.
82 #if __GLASGOW_HASKELL__ > 408
83 -- use the line below when we can be sure of compiling with GHC >=
84 -- 5.02, and remove the implementation of rawSystem at the end of this
86 import PrelIOBase -- this can be removed when SystemExts is used
87 import CError ( throwErrnoIfMinus1 ) -- as can this
88 -- import SystemExts ( rawSystem )
90 import System ( system )
93 #include "HsVersions.h"
98 The configuration story
99 ~~~~~~~~~~~~~~~~~~~~~~~
101 GHC needs various support files (library packages, RTS etc), plus
102 various auxiliary programs (cp, gcc, etc). It finds these in one
105 * When running as an *installed program*, GHC finds most of this support
106 stuff in the installed library tree. The path to this tree is passed
107 to GHC via the -B flag, and given to initSysTools .
109 * When running *in-place* in a build tree, GHC finds most of this support
110 stuff in the build tree. The path to the build tree is, again passed
113 GHC tells which of the two is the case by seeing whether package.conf
114 is in TopDir [installed] or in TopDir/ghc/driver [inplace] (what a hack).
117 SysTools.initSysProgs figures out exactly where all the auxiliary programs
118 are, and initialises mutable variables to make it easy to call them.
119 To to this, it makes use of definitions in Config.hs, which is a Haskell
120 file containing variables whose value is figured out by the build system.
122 Config.hs contains two sorts of things
124 cGCC, The *names* of the programs
127 etc They do *not* include paths
130 cUNLIT_DIR The *path* to the directory containing unlit, split etc
131 cSPLIT_DIR *relative* to the root of the build tree,
132 for use when running *in-place* in a build tree (only)
136 ---------------------------------------------
137 NOTES for an ALTERNATIVE scheme (i.e *not* what is currently implemented):
139 Another hair-brained scheme for simplifying the current tool location
140 nightmare in GHC: Simon originally suggested using another
141 configuration file along the lines of GCC's specs file - which is fine
142 except that it means adding code to read yet another configuration
143 file. What I didn't notice is that the current package.conf is
144 general enough to do this:
147 {name = "tools", import_dirs = [], source_dirs = [],
148 library_dirs = [], hs_libraries = [], extra_libraries = [],
149 include_dirs = [], c_includes = [], package_deps = [],
150 extra_ghc_opts = ["-pgmc/usr/bin/gcc","-pgml${libdir}/bin/unlit", ... etc.],
151 extra_cc_opts = [], extra_ld_opts = []}
153 Which would have the advantage that we get to collect together in one
154 place the path-specific package stuff with the path-specific tool
157 ---------------------------------------------
160 %************************************************************************
162 \subsection{Global variables to contain system programs}
164 %************************************************************************
166 All these pathnames are maintained IN THE NATIVE FORMAT OF THE HOST MACHINE.
167 (See remarks under pathnames below)
170 GLOBAL_VAR(v_Pgm_L, error "pgm_L", String) -- unlit
171 GLOBAL_VAR(v_Pgm_P, error "pgm_P", String) -- cpp
172 GLOBAL_VAR(v_Pgm_c, error "pgm_c", String) -- gcc
173 GLOBAL_VAR(v_Pgm_m, error "pgm_m", String) -- asm code mangler
174 GLOBAL_VAR(v_Pgm_s, error "pgm_s", String) -- asm code splitter
175 GLOBAL_VAR(v_Pgm_a, error "pgm_a", String) -- as
177 GLOBAL_VAR(v_Pgm_I, error "pgm_I", String) -- ilx2il
178 GLOBAL_VAR(v_Pgm_i, error "pgm_i", String) -- ilasm
180 GLOBAL_VAR(v_Pgm_l, error "pgm_l", String) -- ld
181 GLOBAL_VAR(v_Pgm_MkDLL, error "pgm_dll", String) -- mkdll
183 GLOBAL_VAR(v_Pgm_T, error "pgm_T", String) -- touch
184 GLOBAL_VAR(v_Pgm_CP, error "pgm_CP", String) -- cp
186 GLOBAL_VAR(v_Path_package_config, error "path_package_config", String)
187 GLOBAL_VAR(v_Path_usage, error "ghc_usage.txt", String)
189 GLOBAL_VAR(v_TopDir, error "TopDir", String) -- -B<dir>
191 -- Parallel system only
192 GLOBAL_VAR(v_Pgm_sysman, error "pgm_sysman", String) -- system manager
194 -- ways to get at some of these variables from outside this module
195 getPackageConfigPath = readIORef v_Path_package_config
196 getTopDir = readIORef v_TopDir
200 %************************************************************************
202 \subsection{Initialisation}
204 %************************************************************************
207 initSysTools :: [String] -- Command-line arguments starting "-B"
209 -> IO () -- Set all the mutable variables above, holding
210 -- (a) the system programs
211 -- (b) the package-config file
212 -- (c) the GHC usage message
215 initSysTools minusB_args
216 = do { (am_installed, top_dir) <- findTopDir minusB_args
217 ; writeIORef v_TopDir top_dir
219 -- for "installed" this is the root of GHC's support files
220 -- for "in-place" it is the root of the build tree
221 -- NB: top_dir is assumed to be in standard Unix format '/' separated
223 ; let installed, installed_bin :: FilePath -> FilePath
224 installed_bin pgm = pgmPath top_dir pgm
225 installed file = pgmPath top_dir file
226 inplace dir pgm = pgmPath (top_dir `slash` dir) pgm
229 | am_installed = installed "package.conf"
230 | otherwise = inplace cGHC_DRIVER_DIR "package.conf.inplace"
233 | am_installed = installed "ghc-usage.txt"
234 | otherwise = inplace cGHC_DRIVER_DIR "ghc-usage.txt"
236 -- For all systems, unlit, split, mangle are GHC utilities
237 -- architecture-specific stuff is done when building Config.hs
239 | am_installed = installed_bin cGHC_UNLIT
240 | otherwise = inplace cGHC_UNLIT_DIR cGHC_UNLIT
242 -- split and mangle are Perl scripts
244 | am_installed = installed_bin cGHC_SPLIT
245 | otherwise = inplace cGHC_SPLIT_DIR cGHC_SPLIT
248 | am_installed = installed_bin cGHC_MANGLER
249 | otherwise = inplace cGHC_MANGLER_DIR cGHC_MANGLER
251 #ifndef mingw32_TARGET_OS
252 -- check whether TMPDIR is set in the environment
253 ; IO.try (do dir <- getEnv "TMPDIR" -- fails if not set
258 -- On Win32, consult GetTempPath() for a temp dir.
259 -- => it first tries TMP, TEMP, then finally the
260 -- Windows directory(!). The directory is in short-path
261 -- form and *does* have a trailing backslash.
263 let len = (2048::Int)
264 buf <- mallocArray len
265 ret <- getTempPath len buf
268 -- failed, consult TEMP.
269 destructArray len buf
273 destructArray len buf
276 -- strip the trailing backslash (awful, but
277 -- we only do this once).
287 -- Check that the package config exists
288 ; config_exists <- doesFileExist pkgconfig_path
289 ; when (not config_exists) $
290 throwDyn (InstallationError
291 ("Can't find package.conf as " ++ pkgconfig_path))
293 #if defined(mingw32_TARGET_OS)
294 -- WINDOWS-SPECIFIC STUFF
295 -- On Windows, gcc and friends are distributed with GHC,
296 -- so when "installed" we look in TopDir/bin
297 -- When "in-place" we look wherever the build-time configure
299 -- When "install" we tell gcc where its specs file + exes are (-B)
300 -- and also some places to pick up include files. We need
301 -- to be careful to put all necessary exes in the -B place
302 -- (as, ld, cc1, etc) since if they don't get found there, gcc
303 -- then tries to run unadorned "as", "ld", etc, and will
304 -- pick up whatever happens to be lying around in the path,
305 -- possibly including those from a cygwin install on the target,
306 -- which is exactly what we're trying to avoid.
307 ; let gcc_path | am_installed = installed_bin ("gcc -B\"" ++ installed "gcc-lib/\"")
309 -- The trailing "/" is absolutely essential; gcc seems
310 -- to construct file names simply by concatenating to this
311 -- -B path with no extra slash
312 -- We use "/" rather than "\\" because otherwise "\\\" is mangled
313 -- later on; although gcc_path is in NATIVE format, gcc can cope
314 -- (see comments with declarations of global variables)
316 -- The quotes round the -B argument are in case TopDir has spaces in it
318 perl_path | am_installed = installed_bin cGHC_PERL
319 | otherwise = cGHC_PERL
321 -- 'touch' is a GHC util for Windows, and similarly unlit, mangle
322 ; let touch_path | am_installed = installed_bin cGHC_TOUCHY
323 | otherwise = inplace cGHC_TOUCHY_DIR cGHC_TOUCHY
325 -- On Win32 we don't want to rely on #!/bin/perl, so we prepend
326 -- a call to Perl to get the invocation of split and mangle
327 ; let split_path = perl_path ++ " \"" ++ split_script ++ "\""
328 mangle_path = perl_path ++ " \"" ++ mangle_script ++ "\""
330 ; let mkdll_path = cMKDLL
332 -- UNIX-SPECIFIC STUFF
333 -- On Unix, the "standard" tools are assumed to be
334 -- in the same place whether we are running "in-place" or "installed"
335 -- That place is wherever the build-time configure script found them.
336 ; let gcc_path = cGCC
337 touch_path = cGHC_TOUCHY
338 mkdll_path = panic "Can't build DLLs on a non-Win32 system"
340 -- On Unix, scripts are invoked using the '#!' method. Binary
341 -- installations of GHC on Unix place the correct line on the front
342 -- of the script at installation time, so we don't want to wire-in
343 -- our knowledge of $(PERL) on the host system here.
344 ; let split_path = split_script
345 mangle_path = mangle_script
348 -- cpp is derived from gcc on all platforms
349 ; let cpp_path = gcc_path ++ " -E " ++ cRAWCPP_FLAGS
351 -- For all systems, copy and remove are provided by the host
352 -- system; architecture-specific stuff is done when building Config.hs
353 ; let cp_path = cGHC_CP
355 -- Other things being equal, as and ld are simply gcc
356 ; let as_path = gcc_path
360 -- ilx2il and ilasm are specified in Config.hs
361 ; let ilx2il_path = cILX2IL
365 -- Initialise the global vars
366 ; writeIORef v_Path_package_config pkgconfig_path
367 ; writeIORef v_Path_usage ghc_usage_msg_path
369 ; writeIORef v_Pgm_sysman (top_dir ++ "/ghc/rts/parallel/SysMan")
370 -- Hans: this isn't right in general, but you can
371 -- elaborate it in the same way as the others
373 ; writeIORef v_Pgm_L unlit_path
374 ; writeIORef v_Pgm_P cpp_path
375 ; writeIORef v_Pgm_c gcc_path
376 ; writeIORef v_Pgm_m mangle_path
377 ; writeIORef v_Pgm_s split_path
378 ; writeIORef v_Pgm_a as_path
380 ; writeIORef v_Pgm_I ilx2il_path
381 ; writeIORef v_Pgm_i ilasm_path
383 ; writeIORef v_Pgm_l ld_path
384 ; writeIORef v_Pgm_MkDLL mkdll_path
385 ; writeIORef v_Pgm_T touch_path
386 ; writeIORef v_Pgm_CP cp_path
391 #if defined(mingw32_TARGET_OS)
392 foreign import stdcall "GetTempPathA" getTempPath :: Int -> CString -> IO Int32
396 setPgm is called when a command-line option like
398 is used to override a particular program with a new one
401 setPgm :: String -> IO ()
402 -- The string is the flag, minus the '-pgm' prefix
403 -- So the first character says which program to override
405 setPgm ('P' : pgm) = writeIORef v_Pgm_P pgm
406 setPgm ('c' : pgm) = writeIORef v_Pgm_c pgm
407 setPgm ('m' : pgm) = writeIORef v_Pgm_m pgm
408 setPgm ('s' : pgm) = writeIORef v_Pgm_s pgm
409 setPgm ('a' : pgm) = writeIORef v_Pgm_a pgm
410 setPgm ('l' : pgm) = writeIORef v_Pgm_l pgm
412 setPgm ('I' : pgm) = writeIORef v_Pgm_I pgm
413 setPgm ('i' : pgm) = writeIORef v_Pgm_i pgm
415 setPgm pgm = unknownFlagErr ("-pgm" ++ pgm)
421 -- for "installed" this is the root of GHC's support files
422 -- for "in-place" it is the root of the build tree
425 -- 1. Set proto_top_dir
426 -- a) look for (the last) -B flag, and use it
427 -- b) if there are no -B flags, get the directory
428 -- where GHC is running (only on Windows)
430 -- 2. If package.conf exists in proto_top_dir, we are running
431 -- installed; and TopDir = proto_top_dir
433 -- 3. Otherwise we are running in-place, so
434 -- proto_top_dir will be /...stuff.../ghc/compiler
435 -- Set TopDir to /...stuff..., which is the root of the build tree
437 -- This is very gruesome indeed
439 findTopDir :: [String]
440 -> IO (Bool, -- True <=> am installed, False <=> in-place
441 String) -- TopDir (in Unix format '/' separated)
444 = do { top_dir <- get_proto
445 -- Discover whether we're running in a build tree or in an installation,
446 -- by looking for the package configuration file.
447 ; am_installed <- doesFileExist (top_dir `slash` "package.conf")
449 ; return (am_installed, top_dir)
452 -- get_proto returns a Unix-format path (relying on getExecDir to do so too)
453 get_proto | not (null minusbs)
454 = return (unDosifyPath (drop 2 (last minusbs))) -- 2 for "-B"
456 = do { maybe_exec_dir <- getExecDir -- Get directory of executable
457 ; case maybe_exec_dir of -- (only works on Windows;
458 -- returns Nothing on Unix)
459 Nothing -> throwDyn (InstallationError "missing -B<dir> option")
460 Just dir -> return dir
465 %************************************************************************
467 \subsection{Command-line options}
469 %************************************************************************
471 When invoking external tools as part of the compilation pipeline, we
472 pass these a sequence of options on the command-line. Rather than
473 just using a list of Strings, we use a type that allows us to distinguish
474 between filepaths and 'other stuff'. [The reason being, of course, that
475 this type gives us a handle on transforming filenames, and filenames only,
476 to whatever format they're expected to be on a particular platform.]
480 = FileOption -- an entry that _contains_ filename(s) / filepaths.
481 String -- a non-filepath prefix that shouldn't be transformed (e.g., "/out="
482 String -- the filepath/filename portion
485 showOptions :: [Option] -> String
486 showOptions ls = unwords (map (quote.showOpt) ls)
488 showOpt (FileOption pre f) = pre ++ dosifyPath f
489 showOpt (Option s) = s
491 #if defined(mingw32_TARGET_OS)
493 quote s = "\"" ++ s ++ "\""
501 %************************************************************************
503 \subsection{Running an external program}
505 %************************************************************************
509 runUnlit :: [Option] -> IO ()
510 runUnlit args = do p <- readIORef v_Pgm_L
511 runSomething "Literate pre-processor" p args
513 runCpp :: [Option] -> IO ()
514 runCpp args = do p <- readIORef v_Pgm_P
515 runSomething "C pre-processor" p args
517 runCc :: [Option] -> IO ()
518 runCc args = do p <- readIORef v_Pgm_c
519 runSomething "C Compiler" p args
521 runMangle :: [Option] -> IO ()
522 runMangle args = do p <- readIORef v_Pgm_m
523 runSomething "Mangler" p args
525 runSplit :: [Option] -> IO ()
526 runSplit args = do p <- readIORef v_Pgm_s
527 runSomething "Splitter" p args
529 runAs :: [Option] -> IO ()
530 runAs args = do p <- readIORef v_Pgm_a
531 runSomething "Assembler" p args
533 runLink :: [Option] -> IO ()
534 runLink args = do p <- readIORef v_Pgm_l
535 runSomething "Linker" p args
538 runIlx2il :: [Option] -> IO ()
539 runIlx2il args = do p <- readIORef v_Pgm_I
540 runSomething "Ilx2Il" p args
542 runIlasm :: [Option] -> IO ()
543 runIlasm args = do p <- readIORef v_Pgm_i
544 runSomething "Ilasm" p args
547 runMkDLL :: [Option] -> IO ()
548 runMkDLL args = do p <- readIORef v_Pgm_MkDLL
549 runSomething "Make DLL" p args
551 touch :: String -> String -> IO ()
552 touch purpose arg = do p <- readIORef v_Pgm_T
553 runSomething purpose p [FileOption "" arg]
555 copy :: String -> String -> String -> IO ()
556 copy purpose from to = do
557 verb <- dynFlag verbosity
558 when (verb >= 2) $ hPutStrLn stderr ("*** " ++ purpose)
560 h <- openFile to WriteMode
561 ls <- readFile from -- inefficient, but it'll do for now.
562 -- ToDo: speed up via slurping.
568 getSysMan :: IO String -- How to invoke the system manager
569 -- (parallel system only)
570 getSysMan = readIORef v_Pgm_sysman
573 %************************************************************************
575 \subsection{GHC Usage message}
577 %************************************************************************
579 Show the usage message and exit
582 showGhcUsage = do { usage_path <- readIORef v_Path_usage
583 ; usage <- readFile usage_path
585 ; exitWith ExitSuccess }
588 dump ('$':'$':s) = hPutStr stderr progName >> dump s
589 dump (c:s) = hPutChar stderr c >> dump s
593 %************************************************************************
595 \subsection{Managing temporary files
597 %************************************************************************
600 GLOBAL_VAR(v_FilesToClean, [], [String] )
601 GLOBAL_VAR(v_TmpDir, cDEFAULT_TMPDIR, String )
602 -- v_TmpDir has no closing '/'
606 setTmpDir dir = writeIORef v_TmpDir dir
608 cleanTempFiles :: Int -> IO ()
609 cleanTempFiles verb = do fs <- readIORef v_FilesToClean
610 removeTmpFiles verb fs
612 cleanTempFilesExcept :: Int -> [FilePath] -> IO ()
613 cleanTempFilesExcept verb dont_delete
614 = do fs <- readIORef v_FilesToClean
615 let leftovers = filter (`notElem` dont_delete) fs
616 removeTmpFiles verb leftovers
617 writeIORef v_FilesToClean dont_delete
620 -- find a temporary name that doesn't already exist.
621 newTempName :: Suffix -> IO FilePath
623 = do x <- getProcessID
624 tmp_dir <- readIORef v_TmpDir
625 findTempName tmp_dir x
627 findTempName tmp_dir x
628 = do let filename = tmp_dir ++ "/ghc" ++ show x ++ '.':extn
629 b <- doesFileExist filename
630 if b then findTempName tmp_dir (x+1)
631 else do add v_FilesToClean filename -- clean it up later
634 addFilesToClean :: [FilePath] -> IO ()
635 -- May include wildcards [used by DriverPipeline.run_phase SplitMangle]
636 addFilesToClean files = mapM_ (add v_FilesToClean) files
638 removeTmpFiles :: Int -> [FilePath] -> IO ()
639 removeTmpFiles verb fs
640 = traceCmd "Deleting temp files"
641 ("Deleting: " ++ unwords fs)
644 rm f = removeFile f `catchAllIO`
647 hPutStrLn stderr ("Warning: deleting non-existent " ++ f)
653 %************************************************************************
655 \subsection{Running a program}
657 %************************************************************************
660 GLOBAL_VAR(v_Dry_run, False, Bool)
663 setDryRun = writeIORef v_Dry_run True
665 -----------------------------------------------------------------------------
666 -- Running an external program
668 runSomething :: String -- For -v message
669 -> String -- Command name (possibly a full path)
670 -- assumed already dos-ified
671 -> [Option] -- Arguments
672 -- runSomething will dos-ify them
675 runSomething phase_name pgm args
676 = traceCmd phase_name cmd_line $
678 #ifndef mingw32_TARGET_OS
679 exit_code <- system cmd_line
681 exit_code <- rawSystem cmd_line
683 ; if exit_code /= ExitSuccess
684 then throwDyn (PhaseFailed phase_name exit_code)
688 cmd_line = pgm ++ ' ':showOptions args -- unwords (pgm : dosifyPaths (map quote args))
689 -- The pgm is already in native format (appropriate dir separators)
690 #if defined(mingw32_TARGET_OS)
692 quote s = "\"" ++ s ++ "\""
697 traceCmd :: String -> String -> IO () -> IO ()
698 -- a) trace the command (at two levels of verbosity)
699 -- b) don't do it at all if dry-run is set
700 traceCmd phase_name cmd_line action
701 = do { verb <- dynFlag verbosity
702 ; when (verb >= 2) $ hPutStrLn stderr ("*** " ++ phase_name)
703 ; when (verb >= 3) $ hPutStrLn stderr cmd_line
707 ; n <- readIORef v_Dry_run
711 ; action `catchAllIO` handle_exn verb
714 handle_exn verb exn = do { when (verb >= 2) (hPutStr stderr "\n")
715 ; when (verb >= 3) (hPutStrLn stderr ("Failed: " ++ cmd_line))
716 ; throwDyn (PhaseFailed phase_name (ExitFailure 1)) }
720 %************************************************************************
722 \subsection{Path names}
724 %************************************************************************
726 We maintain path names in Unix form ('/'-separated) right until
727 the last moment. On Windows we dos-ify them just before passing them
728 to the Windows command.
730 The alternative, of using '/' consistently on Unix and '\' on Windows,
731 proved quite awkward. There were a lot more calls to dosifyPath,
732 and even on Windows we might invoke a unix-like utility (eg 'sh'), which
733 interpreted a command line 'foo\baz' as 'foobaz'.
736 -----------------------------------------------------------------------------
737 -- Convert filepath into MSDOS form.
739 dosifyPaths :: [String] -> [String]
740 -- dosifyPaths does two things
741 -- a) change '/' to '\'
742 -- b) remove initial '/cygdrive/'
744 unDosifyPath :: String -> String
745 -- Just change '\' to '/'
747 pgmPath :: String -- Directory string in Unix format
748 -> String -- Program name with no directory separators
750 -> String -- Program invocation string in native format
754 #if defined(mingw32_TARGET_OS)
756 --------------------- Windows version ------------------
757 dosifyPaths xs = map dosifyPath xs
759 unDosifyPath xs = subst '\\' '/' xs
761 pgmPath dir pgm = dosifyPath dir ++ '\\' : pgm
764 = subst '/' '\\' real_stuff
766 -- fully convince myself that /cygdrive/ prefixes cannot
767 -- really appear here.
768 cygdrive_prefix = "/cygdrive/"
771 | cygdrive_prefix `isPrefixOf` stuff = drop (length cygdrive_prefix) stuff
776 --------------------- Unix version ---------------------
779 pgmPath dir pgm = dir ++ '/' : pgm
780 dosifyPath stuff = stuff
781 --------------------------------------------------------
784 subst a b ls = map (\ x -> if x == a then b else x) ls
788 -----------------------------------------------------------------------------
789 Path name construction
792 slash :: String -> String -> String
793 absPath, relPath :: [String] -> String
796 relPath xs = foldr1 slash xs
798 absPath xs = "" `slash` relPath xs
800 slash s1 s2 = s1 ++ ('/' : s2)
804 %************************************************************************
806 \subsection{Support code}
808 %************************************************************************
811 -----------------------------------------------------------------------------
812 -- Define getExecDir :: IO (Maybe String)
814 #if defined(mingw32_TARGET_OS)
815 getExecDir :: IO (Maybe String)
816 getExecDir = do let len = (2048::Int) -- plenty, PATH_MAX is 512 under Win32.
817 buf <- mallocArray len
818 ret <- getModuleFileName nullAddr buf len
819 if ret == 0 then destructArray len buf >> return Nothing
820 else do s <- peekCString buf
821 destructArray len buf
822 return (Just (reverse (drop (length "/bin/ghc.exe") (reverse (unDosifyPath s)))))
825 foreign import stdcall "GetModuleFileNameA" getModuleFileName :: Addr -> CString -> Int -> IO Int32
827 getExecDir :: IO (Maybe String) = do return Nothing
830 #ifdef mingw32_TARGET_OS
831 foreign import "_getpid" getProcessID :: IO Int -- relies on Int == Int32 on Windows
833 getProcessID :: IO Int
834 getProcessID = Posix.getProcessID
837 rawSystem :: String -> IO ExitCode
838 #if __GLASGOW_HASKELL__ > 408
839 rawSystem "" = ioException (IOError Nothing InvalidArgument "rawSystem" "null command" Nothing)
841 withCString cmd $ \s -> do
842 status <- throwErrnoIfMinus1 "rawSystem" (primRawSystem s)
844 0 -> return ExitSuccess
845 n -> return (ExitFailure n)
847 foreign import ccall "rawSystemCmd" unsafe primRawSystem :: CString -> IO Int
849 rawSystem = System.system