[project @ 2001-05-23 09:59:18 by simonmar]
[ghc-hetmet.git] / ghc / compiler / main / TmpFiles.hs
index 823b79c..b27217c 100644 (file)
@@ -1,5 +1,5 @@
 -----------------------------------------------------------------------------
--- $Id: TmpFiles.hs,v 1.13 2000/12/11 14:42:21 sewardj Exp $
+-- $Id: TmpFiles.hs,v 1.18 2001/04/21 10:19:53 panne Exp $
 --
 -- Temporary file management
 --
 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 kludgedSystem ("rm -f " ++ f) "Cleaning temp files" >> 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,7 +81,37 @@ 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 
+                 then kludgedSystem ("rm -f " ++ f) "Cleaning temp files" >> return ()
+                 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
+#ifndef mingw32_TARGET_OS
+   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