X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2Fmain%2FTmpFiles.hs;h=b693d5930466810b6985e400b86417a912d4003a;hb=4cef3202f8067fc3e9860e7a2b72c10b313b7366;hp=b0c19d845c1c841cd022605e81ae627ff4d96aa5;hpb=275085675cabfdf5d3298d436aa1cf3aaf3291ca;p=ghc-hetmet.git diff --git a/ghc/compiler/main/TmpFiles.hs b/ghc/compiler/main/TmpFiles.hs index b0c19d8..b693d59 100644 --- a/ghc/compiler/main/TmpFiles.hs +++ b/ghc/compiler/main/TmpFiles.hs @@ -1,5 +1,5 @@ ----------------------------------------------------------------------------- --- $Id: TmpFiles.hs,v 1.11 2000/12/07 08:20:46 simonpj Exp $ +-- $Id: TmpFiles.hs,v 1.19 2001/05/28 03:31:19 sof Exp $ -- -- Temporary file management -- @@ -10,14 +10,19 @@ module TmpFiles ( Suffix, initTempFileStorage, -- :: IO () - cleanTempFiles, -- :: IO () + cleanTempFiles, -- :: Int -> IO () + cleanTempFilesExcept, -- :: Int -> [FilePath] -> IO () newTempName, -- :: Suffix -> IO FilePath addFilesToClean, -- :: [FilePath] -> IO () - v_TmpDir + removeTmpFiles, -- :: Int -> [FilePath] -> IO () + v_TmpDir, + kludgedSystem ) where -- main +import DriverUtil import Config +import Panic import Util -- hslibs @@ -30,6 +35,7 @@ import Directory import IO import Monad +#include "../includes/config.h" #include "HsVersions.h" GLOBAL_VAR(v_FilesToClean, [], [String] ) @@ -45,18 +51,17 @@ initTempFileStorage = do return () ) -cleanTempFiles :: Bool -> IO () -cleanTempFiles verbose = do +cleanTempFiles :: Int -> IO () +cleanTempFiles verb = do fs <- readIORef v_FilesToClean + removeTmpFiles verb fs - let blowAway f = - (do when verbose (hPutStrLn stderr ("Removing: " ++ f)) - if '*' `elem` f then system ("rm -f " ++ f) >> return () - else removeFile f) - `catchAllIO` - (\_ -> when verbose (hPutStrLn stderr - ("Warning: can't remove tmp file " ++ f))) - mapM_ blowAway fs +cleanTempFilesExcept :: Int -> [FilePath] -> IO () +cleanTempFilesExcept verb dont_delete = do + fs <- readIORef v_FilesToClean + let leftovers = filter (`notElem` dont_delete) fs + removeTmpFiles verb leftovers + writeIORef v_FilesToClean dont_delete type Suffix = String @@ -76,8 +81,44 @@ newTempName extn = do addFilesToClean :: [FilePath] -> IO () addFilesToClean files = mapM_ (add v_FilesToClean) files -add :: IORef [a] -> a -> IO () -add var x = do - xs <- readIORef var - writeIORef var (x:xs) +removeTmpFiles :: Int -> [FilePath] -> IO () +removeTmpFiles verb fs = do + let verbose = verb >= 2 + blowAway f = + (do when verbose (hPutStrLn stderr ("Removing: " ++ f)) + if '*' `elem` f +#if defined(mingw32_TARGET_OS) && defined(MINIMAL_UNIX_DEPS) + then kludgedSystem (cRM ++ ' ':dosifyPath f) "Cleaning temp files" >> return () +#else + then kludgedSystem (cRM ++ f) "Cleaning temp files" >> return () +#endif + else removeFile f) + `catchAllIO` + (\_ -> when verbose (hPutStrLn stderr + ("Warning: can't remove tmp file " ++ f))) + mapM_ blowAway fs + +-- system that works feasibly under Windows (i.e. passes the command line to sh, +-- because system() under Windows doesn't look at SHELL, and always uses CMD.EXE) +kludgedSystem cmd phase_name + = do +#if !defined(mingw32_TARGET_OS) || defined(MINIMAL_UNIX_DEPS) + -- in the case where we do want to use an MSDOS command shell, we assume + -- that files and paths have been converted to a form that's + -- understandable to the command we're invoking. + exit_code <- system cmd `catchAllIO` + (\_ -> throwDyn (PhaseFailed phase_name (ExitFailure 1))) +#else + pid <- myGetProcessID + tmp_dir <- readIORef v_TmpDir + let tmp = tmp_dir++"/sh"++show pid + h <- openFile tmp WriteMode + hPutStrLn h cmd + hClose h + exit_code <- system ("sh - " ++ tmp) `catchAllIO` + (\_ -> removeFile tmp >> + throwDyn (PhaseFailed phase_name (ExitFailure 1))) + removeFile tmp +#endif + return exit_code