X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2Fmain%2FSysTools.lhs;h=4bd328498e9504dfbff5e2f116417b877429e8c7;hb=ec2ccca277f5779f4699aa27f9628b5578c42924;hp=001241f7a4def2cfa7ebf5ea9508cd4281b8fbd5;hpb=c6701e0ce62ee27ad9f29891c729c2f0f213020b;p=ghc-hetmet.git diff --git a/ghc/compiler/main/SysTools.lhs b/ghc/compiler/main/SysTools.lhs index 001241f..4bd3284 100644 --- a/ghc/compiler/main/SysTools.lhs +++ b/ghc/compiler/main/SysTools.lhs @@ -1,7 +1,8 @@ ----------------------------------------------------------------------------- --- Access to system tools: gcc, cp, rm etc -- --- (c) The University of Glasgow 2000 +-- (c) The University of Glasgow 2001 +-- +-- Access to system tools: gcc, cp, rm etc -- ----------------------------------------------------------------------------- @@ -13,17 +14,22 @@ module SysTools ( -- Command-line override setDryRun, - packageConfigPath, -- IO String - -- Where package.conf is + getTopDir, -- IO String -- The value of $libdir + getPackageConfigPath, -- IO String -- Where package.conf is -- Interface to system tools - runUnlit, runCpp, runCc, -- [String] -> IO () - runMangle, runSplit, -- [String] -> IO () - runAs, runLink, -- [String] -> IO () + runUnlit, runCpp, runCc, -- [Option] -> IO () + runMangle, runSplit, -- [Option] -> IO () + runAs, runLink, -- [Option] -> IO () runMkDLL, +#ifdef ILX + runIlx2il, runIlasm, -- [String] -> IO () +#endif + touch, -- String -> String -> IO () copy, -- String -> String -> String -> IO () + unDosifyPath, -- String -> String -- Temporary-file management setTmpDir, @@ -33,13 +39,14 @@ module SysTools ( -- System interface getProcessID, -- IO Int - system, -- String -> IO Int + system, -- String -> IO ExitCode -- Misc showGhcUsage, -- IO () Shows usage message and exits getSysMan, -- IO String Parallel system only + + Option(..) - runSomething -- ToDo: make private ) where import DriverUtil @@ -54,30 +61,37 @@ import IO import Directory ( doesFileExist, removeFile ) import IOExts ( IORef, readIORef, writeIORef ) import Monad ( when, unless ) -import System ( system, ExitCode(..), exitWith ) +import System ( ExitCode(..), exitWith, getEnv, system ) import CString -import CTypes import Int -import Ptr -import MarshalAlloc -import MarshalArray -import Storable +import Addr #include "../includes/config.h" -#if !defined(mingw32_TARGET_OS) +#ifndef mingw32_TARGET_OS import qualified Posix #else -import Addr import List ( isPrefixOf ) +import MarshalArray #endif -import List ( isSuffixOf ) +-- This is a kludge for bootstrapping with 4.08.X. Given that +-- all distributed compilers >= 5.0 will be compiled with themselves. +-- I don't think this kludge is a problem. And we have to start +-- building with >= 5.0 on Win32 anyway. +#if __GLASGOW_HASKELL__ > 408 +-- use the line below when we can be sure of compiling with GHC >= +-- 5.02, and remove the implementation of rawSystem at the end of this +-- file +import PrelIOBase -- this can be removed when SystemExts is used +import CError ( throwErrnoIfMinus1 ) -- as can this +-- import SystemExts ( rawSystem ) +#else +import System ( system ) +#endif #include "HsVersions.h" -{-# DEPRECATED runSomething "runSomething should be private to SysTools" #-} - \end{code} @@ -159,6 +173,10 @@ GLOBAL_VAR(v_Pgm_c, error "pgm_c", String) -- gcc GLOBAL_VAR(v_Pgm_m, error "pgm_m", String) -- asm code mangler GLOBAL_VAR(v_Pgm_s, error "pgm_s", String) -- asm code splitter GLOBAL_VAR(v_Pgm_a, error "pgm_a", String) -- as +#ifdef ILX +GLOBAL_VAR(v_Pgm_I, error "pgm_I", String) -- ilx2il +GLOBAL_VAR(v_Pgm_i, error "pgm_i", String) -- ilasm +#endif GLOBAL_VAR(v_Pgm_l, error "pgm_l", String) -- ld GLOBAL_VAR(v_Pgm_MkDLL, error "pgm_dll", String) -- mkdll @@ -168,8 +186,14 @@ GLOBAL_VAR(v_Pgm_CP, error "pgm_CP", String) -- cp GLOBAL_VAR(v_Path_package_config, error "path_package_config", String) GLOBAL_VAR(v_Path_usage, error "ghc_usage.txt", String) +GLOBAL_VAR(v_TopDir, error "TopDir", String) -- -B + -- Parallel system only GLOBAL_VAR(v_Pgm_sysman, error "pgm_sysman", String) -- system manager + +-- ways to get at some of these variables from outside this module +getPackageConfigPath = readIORef v_Path_package_config +getTopDir = readIORef v_TopDir \end{code} @@ -182,22 +206,22 @@ GLOBAL_VAR(v_Pgm_sysman, error "pgm_sysman", String) -- system manager \begin{code} initSysTools :: [String] -- Command-line arguments starting "-B" - -> IO String -- Set all the mutable variables above, holding + -> IO () -- Set all the mutable variables above, holding -- (a) the system programs -- (b) the package-config file -- (c) the GHC usage message - -- Return TopDir initSysTools minusB_args - = do { (am_installed, top_dir) <- getTopDir minusB_args + = do { (am_installed, top_dir) <- findTopDir minusB_args + ; writeIORef v_TopDir top_dir -- top_dir -- for "installed" this is the root of GHC's support files -- for "in-place" it is the root of the build tree -- NB: top_dir is assumed to be in standard Unix format '/' separated ; let installed, installed_bin :: FilePath -> FilePath - installed_bin pgm = pgmPath (top_dir `slash` "extra-bin") pgm + installed_bin pgm = pgmPath top_dir pgm installed file = pgmPath top_dir file inplace dir pgm = pgmPath (top_dir `slash` dir) pgm @@ -224,6 +248,42 @@ initSysTools minusB_args | am_installed = installed_bin cGHC_MANGLER | otherwise = inplace cGHC_MANGLER_DIR cGHC_MANGLER +#ifndef mingw32_TARGET_OS + -- check whether TMPDIR is set in the environment + ; IO.try (do dir <- getEnv "TMPDIR" -- fails if not set + setTmpDir dir + return () + ) +#else + -- On Win32, consult GetTempPath() for a temp dir. + -- => it first tries TMP, TEMP, then finally the + -- Windows directory(!). The directory is in short-path + -- form and *does* have a trailing backslash. + ; IO.try (do + let len = (2048::Int) + buf <- mallocArray len + ret <- getTempPath len buf + tdir <- + if ret == 0 then do + -- failed, consult TEMP. + destructArray len buf + getEnv "TMP" + else do + s <- peekCString buf + destructArray len buf + return s + let + -- strip the trailing backslash (awful, but + -- we only do this once). + tmpdir = + case last tdir of + '/' -> init tdir + '\\' -> init tdir + _ -> tdir + setTmpDir tmpdir + return ()) +#endif + -- Check that the package config exists ; config_exists <- doesFileExist pkgconfig_path ; when (not config_exists) $ @@ -244,9 +304,17 @@ initSysTools minusB_args -- pick up whatever happens to be lying around in the path, -- possibly including those from a cygwin install on the target, -- which is exactly what we're trying to avoid. - ; let gcc_path | am_installed = installed_bin ("gcc -B" ++ installed "gcc-lib/" - ++ " -I" ++ installed "include/mingw") + ; let gcc_path | am_installed = installed_bin ("gcc -B\"" ++ installed "gcc-lib/\"") | otherwise = cGCC + -- The trailing "/" is absolutely essential; gcc seems + -- to construct file names simply by concatenating to this + -- -B path with no extra slash + -- We use "/" rather than "\\" because otherwise "\\\" is mangled + -- later on; although gcc_path is in NATIVE format, gcc can cope + -- (see comments with declarations of global variables) + -- + -- The quotes round the -B argument are in case TopDir has spaces in it + perl_path | am_installed = installed_bin cGHC_PERL | otherwise = cGHC_PERL @@ -256,8 +324,8 @@ initSysTools minusB_args -- On Win32 we don't want to rely on #!/bin/perl, so we prepend -- a call to Perl to get the invocation of split and mangle - ; let split_path = perl_path ++ " " ++ split_script - mangle_path = perl_path ++ " " ++ mangle_script + ; let split_path = perl_path ++ " \"" ++ split_script ++ "\"" + mangle_path = perl_path ++ " \"" ++ mangle_script ++ "\"" ; let mkdll_path = cMKDLL #else @@ -288,6 +356,11 @@ initSysTools minusB_args ; let as_path = gcc_path ld_path = gcc_path +#ifdef ILX + -- ilx2il and ilasm are specified in Config.hs + ; let ilx2il_path = cILX2IL + ilasm_path = cILASM +#endif -- Initialise the global vars ; writeIORef v_Path_package_config pkgconfig_path @@ -303,18 +376,26 @@ initSysTools minusB_args ; writeIORef v_Pgm_m mangle_path ; writeIORef v_Pgm_s split_path ; writeIORef v_Pgm_a as_path +#ifdef ILX + ; writeIORef v_Pgm_I ilx2il_path + ; writeIORef v_Pgm_i ilasm_path +#endif ; writeIORef v_Pgm_l ld_path ; writeIORef v_Pgm_MkDLL mkdll_path ; writeIORef v_Pgm_T touch_path ; writeIORef v_Pgm_CP cp_path - ; return top_dir + ; return () } + +#if defined(mingw32_TARGET_OS) +foreign import stdcall "GetTempPathA" getTempPath :: Int -> CString -> IO Int32 +#endif \end{code} setPgm is called when a command-line option like -pgmLld -is used to override a particular program with a new onw +is used to override a particular program with a new one \begin{code} setPgm :: String -> IO () @@ -327,6 +408,10 @@ setPgm ('m' : pgm) = writeIORef v_Pgm_m pgm setPgm ('s' : pgm) = writeIORef v_Pgm_s pgm setPgm ('a' : pgm) = writeIORef v_Pgm_a pgm setPgm ('l' : pgm) = writeIORef v_Pgm_l pgm +#ifdef ILX +setPgm ('I' : pgm) = writeIORef v_Pgm_I pgm +setPgm ('i' : pgm) = writeIORef v_Pgm_i pgm +#endif setPgm pgm = unknownFlagErr ("-pgm" ++ pgm) \end{code} @@ -351,11 +436,11 @@ setPgm pgm = unknownFlagErr ("-pgm" ++ pgm) -- -- This is very gruesome indeed -getTopDir :: [String] +findTopDir :: [String] -> IO (Bool, -- True <=> am installed, False <=> in-place String) -- TopDir (in Unix format '/' separated) -getTopDir minusbs +findTopDir minusbs = do { top_dir <- get_proto -- Discover whether we're running in a build tree or in an installation, -- by looking for the package configuration file. @@ -364,7 +449,7 @@ getTopDir minusbs ; return (am_installed, top_dir) } where - -- get_proto returns a Unix-format path + -- get_proto returns a Unix-format path (relying on getExecDir to do so too) get_proto | not (null minusbs) = return (unDosifyPath (drop 2 (last minusbs))) -- 2 for "-B" | otherwise @@ -372,23 +457,44 @@ getTopDir minusbs ; case maybe_exec_dir of -- (only works on Windows; -- returns Nothing on Unix) Nothing -> throwDyn (InstallationError "missing -B option") - Just dir -> return (remove_suffix (unDosifyPath dir)) + Just dir -> return dir } +\end{code} + + +%************************************************************************ +%* * +\subsection{Command-line options} +n%* * +%************************************************************************ + +When invoking external tools as part of the compilation pipeline, we +pass these a sequence of options on the command-line. Rather than +just using a list of Strings, we use a type that allows us to distinguish +between filepaths and 'other stuff'. [The reason being, of course, that +this type gives us a handle on transforming filenames, and filenames only, +to whatever format they're expected to be on a particular platform.] + +\begin{code} +data Option + = FileOption -- an entry that _contains_ filename(s) / filepaths. + String -- a non-filepath prefix that shouldn't be transformed (e.g., "/out=" + String -- the filepath/filename portion + | Option String + +showOptions :: [Option] -> String +showOptions ls = unwords (map (quote.showOpt) ls) + where + showOpt (FileOption pre f) = pre ++ dosifyPath f + showOpt (Option s) = s + +#if defined(mingw32_TARGET_OS) + quote "" = "" + quote s = "\"" ++ s ++ "\"" +#else + quote = id +#endif - -- In an installed tree, the ghc binary lives in $libexecdir, which - -- is normally $libdir/bin. So we strip off a /bin suffix here. - -- In a build tree, the ghc binary lives in $fptools/ghc/compiler, - -- so we strip off the /ghc/compiler suffix here too, leaving a - -- standard TOPDIR. - remove_suffix ghc_bin_dir -- ghc_bin_dir is in standard Unix format - | "/ghc/compiler" `isSuffixOf` ghc_bin_dir = back_two - | "/bin" `isSuffixOf` ghc_bin_dir = back_one - | otherwise = ghc_bin_dir - where - p1 = dropWhile (not . isSlash) (reverse ghc_bin_dir) - p2 = dropWhile (not . isSlash) (tail p1) -- head is '/' - back_two = reverse (tail p2) -- head is '/' - back_one = reverse (tail p1) \end{code} @@ -400,41 +506,51 @@ n%* * \begin{code} -runUnlit :: [String] -> IO () +runUnlit :: [Option] -> IO () runUnlit args = do p <- readIORef v_Pgm_L runSomething "Literate pre-processor" p args -runCpp :: [String] -> IO () +runCpp :: [Option] -> IO () runCpp args = do p <- readIORef v_Pgm_P runSomething "C pre-processor" p args -runCc :: [String] -> IO () +runCc :: [Option] -> IO () runCc args = do p <- readIORef v_Pgm_c runSomething "C Compiler" p args -runMangle :: [String] -> IO () +runMangle :: [Option] -> IO () runMangle args = do p <- readIORef v_Pgm_m runSomething "Mangler" p args -runSplit :: [String] -> IO () +runSplit :: [Option] -> IO () runSplit args = do p <- readIORef v_Pgm_s runSomething "Splitter" p args -runAs :: [String] -> IO () +runAs :: [Option] -> IO () runAs args = do p <- readIORef v_Pgm_a runSomething "Assembler" p args -runLink :: [String] -> IO () +runLink :: [Option] -> IO () runLink args = do p <- readIORef v_Pgm_l runSomething "Linker" p args -runMkDLL :: [String] -> IO () +#ifdef ILX +runIlx2il :: [Option] -> IO () +runIlx2il args = do p <- readIORef v_Pgm_I + runSomething "Ilx2Il" p args + +runIlasm :: [Option] -> IO () +runIlasm args = do p <- readIORef v_Pgm_i + runSomething "Ilasm" p args +#endif + +runMkDLL :: [Option] -> IO () runMkDLL args = do p <- readIORef v_Pgm_MkDLL runSomething "Make DLL" p args touch :: String -> String -> IO () touch purpose arg = do p <- readIORef v_Pgm_T - runSomething purpose p [arg] + runSomething purpose p [FileOption "" arg] copy :: String -> String -> String -> IO () copy purpose from to = do @@ -471,8 +587,6 @@ showGhcUsage = do { usage_path <- readIORef v_Path_usage dump "" = return () dump ('$':'$':s) = hPutStr stderr progName >> dump s dump (c:s) = hPutChar stderr c >> dump s - -packageConfigPath = readIORef v_Path_package_config \end{code} @@ -554,20 +668,31 @@ setDryRun = writeIORef v_Dry_run True runSomething :: String -- For -v message -> String -- Command name (possibly a full path) -- assumed already dos-ified - -> [String] -- Arguments + -> [Option] -- Arguments -- runSomething will dos-ify them -> IO () runSomething phase_name pgm args = traceCmd phase_name cmd_line $ - do { exit_code <- system cmd_line + do { +#ifndef mingw32_TARGET_OS + exit_code <- system cmd_line +#else + exit_code <- rawSystem cmd_line +#endif ; if exit_code /= ExitSuccess then throwDyn (PhaseFailed phase_name exit_code) else return () } where - cmd_line = unwords (pgm : dosifyPaths args) + cmd_line = pgm ++ ' ':showOptions args -- unwords (pgm : dosifyPaths (map quote args)) -- The pgm is already in native format (appropriate dir separators) +#if defined(mingw32_TARGET_OS) + quote "" = "" + quote s = "\"" ++ s ++ "\"" +#else + quote = id +#endif traceCmd :: String -> String -> IO () -> IO () -- a) trace the command (at two levels of verbosity) @@ -629,10 +754,10 @@ pgmPath :: String -- Directory string in Unix format #if defined(mingw32_TARGET_OS) --------------------- Windows version ------------------ -unDosifyPath xs = xs - dosifyPaths xs = map dosifyPath xs +unDosifyPath xs = subst '\\' '/' xs + pgmPath dir pgm = dosifyPath dir ++ '\\' : pgm dosifyPath stuff @@ -649,9 +774,10 @@ dosifyPath stuff #else --------------------- Unix version --------------------- -dosifyPaths ps = ps -unDosifyPath xs = subst '\\' '/' xs -pgmPath dir pgm = dir ++ '/' : pgm +dosifyPaths ps = ps +unDosifyPath xs = xs +pgmPath dir pgm = dir ++ '/' : pgm +dosifyPath stuff = stuff -------------------------------------------------------- #endif @@ -666,9 +792,6 @@ subst a b ls = map (\ x -> if x == a then b else x) ls slash :: String -> String -> String absPath, relPath :: [String] -> String -isSlash '/' = True -isSlash other = False - relPath [] = "" relPath xs = foldr1 slash xs @@ -688,25 +811,18 @@ slash s1 s2 = s1 ++ ('/' : s2) ----------------------------------------------------------------------------- -- Define getExecDir :: IO (Maybe String) -#if defined(mingw32_TARGET_OS) && __GLASGOW_HASKELL__ >= 500 -foreign import stdcall "RegQueryValueExA" regQueryValue :: Addr -> CString -> Addr -> Addr -> Ptr CChar -> Ptr Int32 -> IO Int32 -foreign import stdcall "RegQueryValueExA" regQueryValueLen :: Addr -> CString -> Addr -> Addr -> Addr -> Ptr Int32 -> IO Int32 -foreign import stdcall "RegOpenKeyExA" regOpenKey :: Int32 -> CString -> Int32 -> Int32 -> Ptr Addr -> IO Int32 +#if defined(mingw32_TARGET_OS) getExecDir :: IO (Maybe String) -getExecDir = do alloca $ \ p_len -> do - alloca $ \ p_hKey -> do - withCString "SOFTWARE\\University of Glasgow\\Glasgow Haskell Compiler\\ghc-5.01" $ \ name -> - regOpenKey 0x80000002 {-HKEY_LOCAL_MACHINE-} name 0 1 {-KEY_QUERY_VALUE-} p_hKey - hKey <- peek p_hKey - withCString "InstallDir" $ \ key -> do - regQueryValueLen hKey key nullAddr nullAddr nullAddr p_len - len <- peek p_len - buf <- mallocArray (fromIntegral len) - ret <- regQueryValue hKey key nullAddr nullAddr buf p_len - if ret /= 0 then return Nothing - else do s <- peekCString buf - destructArray (fromIntegral len) buf - return (Just s) +getExecDir = do let len = (2048::Int) -- plenty, PATH_MAX is 512 under Win32. + buf <- mallocArray len + ret <- getModuleFileName nullAddr buf len + if ret == 0 then destructArray len buf >> return Nothing + else do s <- peekCString buf + destructArray len buf + return (Just (reverse (drop (length "/bin/ghc.exe") (reverse (unDosifyPath s))))) + + +foreign import stdcall "GetModuleFileNameA" getModuleFileName :: Addr -> CString -> Int -> IO Int32 #else getExecDir :: IO (Maybe String) = do return Nothing #endif @@ -717,4 +833,21 @@ foreign import "_getpid" getProcessID :: IO Int -- relies on Int == Int32 on Win getProcessID :: IO Int getProcessID = Posix.getProcessID #endif + +rawSystem :: String -> IO ExitCode +#if __GLASGOW_HASKELL__ > 408 +rawSystem "" = ioException (IOError Nothing InvalidArgument "rawSystem" "null command" Nothing) +rawSystem cmd = + withCString cmd $ \s -> do + status <- throwErrnoIfMinus1 "rawSystem" (primRawSystem s) + case status of + 0 -> return ExitSuccess + n -> return (ExitFailure n) + +foreign import ccall "rawSystemCmd" unsafe primRawSystem :: CString -> IO Int +#else +rawSystem = System.system +#endif + + \end{code}