Add option `no-keep-hi-files` and `no-keep-o-files` (fixes #4114)
authorKai Harries <kai.harries@gmail.com>
Thu, 24 Mar 2016 21:33:27 +0000 (22:33 +0100)
committerBen Gamari <ben@smart-cactus.org>
Thu, 24 Mar 2016 22:15:16 +0000 (23:15 +0100)
Summary: Remove `.hi` and `.o` files if the flags `no-keep-hi-files` and
`no-keep-o-files` are given.

Test Plan: ./validate

Reviewers: austin, thomie, bgamari

Reviewed By: thomie, bgamari

Subscribers: thomie

Differential Revision: https://phabricator.haskell.org/D2021

GHC Trac Issues: #4114

13 files changed:
compiler/main/DriverPipeline.hs
compiler/main/DynFlags.hs
docs/users_guide/separate_compilation.rst
testsuite/tests/driver/T4114a.hs [new file with mode: 0644]
testsuite/tests/driver/T4114aSub.hs [new file with mode: 0644]
testsuite/tests/driver/T4114b.hs [new file with mode: 0644]
testsuite/tests/driver/T4114bSub.hs [new file with mode: 0644]
testsuite/tests/driver/T4114c.hs [new file with mode: 0644]
testsuite/tests/driver/T4114cSub.hs [new file with mode: 0644]
testsuite/tests/driver/T4114d.hs [new file with mode: 0644]
testsuite/tests/driver/T4114dSub.hs [new file with mode: 0644]
testsuite/tests/driver/all.T
utils/mkUserGuidePart/Options/KeepingIntermediates.hs

index 41a7c5b..89fbb42 100644 (file)
@@ -137,6 +137,12 @@ compileOne' m_tc_result mHscMessage
                         m_tc_result mHscMessage
                         hsc_env summary source_modified mb_old_iface (mod_index, nmods)
 
+   let flags = hsc_dflags hsc_env0
+     in do unless (gopt Opt_KeepHiFiles flags) $
+               addFilesToClean flags [ml_hi_file $ ms_location summary]
+           unless (gopt Opt_KeepOFiles flags) $
+               addFilesToClean flags [ml_obj_file $ ms_location summary]
+
    case (status, hsc_lang) of
         (HscUpToDate, _) ->
             ASSERT( isJust maybe_old_linkable || isNoLink (ghcLink dflags) )
index f14a36f..99d80d6 100644 (file)
@@ -527,6 +527,8 @@ data GeneralFlag
    | Opt_KeepTmpFiles
    | Opt_KeepRawTokenStream
    | Opt_KeepLlvmFiles
+   | Opt_KeepHiFiles
+   | Opt_KeepOFiles
 
    | Opt_BuildDynamicToo
 
@@ -2505,6 +2507,22 @@ dynamic_flags_deps = [
      -- This only makes sense as plural
   , make_ord_flag defGhcFlag "keep-tmp-files"
         (NoArg (setGeneralFlag Opt_KeepTmpFiles))
+  , make_ord_flag defGhcFlag "keep-hi-file"
+        (NoArg (setGeneralFlag Opt_KeepHiFiles))
+  , make_ord_flag defGhcFlag "no-keep-hi-file"
+        (NoArg (unSetGeneralFlag Opt_KeepHiFiles))
+  , make_ord_flag defGhcFlag "keep-hi-files"
+        (NoArg (setGeneralFlag Opt_KeepHiFiles))
+  , make_ord_flag defGhcFlag "no-keep-hi-files"
+        (NoArg (unSetGeneralFlag Opt_KeepHiFiles))
+  , make_ord_flag defGhcFlag "keep-o-file"
+        (NoArg (setGeneralFlag Opt_KeepOFiles))
+  , make_ord_flag defGhcFlag "no-keep-o-file"
+        (NoArg (unSetGeneralFlag Opt_KeepOFiles))
+  , make_ord_flag defGhcFlag "keep-o-files"
+        (NoArg (setGeneralFlag Opt_KeepOFiles))
+  , make_ord_flag defGhcFlag "no-keep-o-files"
+        (NoArg (unSetGeneralFlag Opt_KeepOFiles))
 
         ------- Miscellaneous ----------------------------------------------
   , make_ord_flag defGhcFlag "no-auto-link-packages"
@@ -3615,6 +3633,8 @@ defaultFlags settings
       Opt_GhciHistory,
       Opt_GhciSandbox,
       Opt_HelpfulErrors,
+      Opt_KeepHiFiles,
+      Opt_KeepOFiles,
       Opt_OmitYields,
       Opt_PrintBindContents,
       Opt_ProfCountEntries,
index a2ce5eb..c0d42e9 100644 (file)
@@ -330,8 +330,9 @@ Keeping Intermediate Files
    single: .ll files, saving
    single: .s files, saving
 
-The following options are useful for keeping certain intermediate files
-around, when normally GHC would throw these away after compilation:
+The following options are useful for keeping (or not keeping) certain
+intermediate files around, when normally GHC would throw these away after
+compilation:
 
 .. ghc-flag:: -keep-hc-file
               -keep-hc-files
@@ -340,6 +341,14 @@ around, when normally GHC would throw these away after compilation:
     compilations via :ref:`C <c-code-gen>` (Note: ``.hc`` files are only
     generated by :ref:`unregisterised <unreg>` compilers).
 
+.. ghc-flag:: -keep-hi-files
+
+    .. index::
+       single: temporary files; keeping
+
+   Keep intermediate ``.hi`` files. This is the default. You may use
+   ``-no-keep-hi-files`` if you are not interested in the ``.hi`` files.
+
 .. ghc-flag:: -keep-llvm-file
               -keep-llvm-files
 
@@ -350,6 +359,14 @@ around, when normally GHC would throw these away after compilation:
     aren't generated when using the native code generator, you may need
     to use :ghc-flag:`-fllvm` to force them to be produced).
 
+.. ghc-flag:: -keep-o-files
+
+    .. index::
+       single: temporary files; keeping
+
+   Keep intermediate ``.o`` files. This is the default. You may use
+   ``-no-keep-o-files`` if you are not interested in the ``.o`` files.
+
 .. ghc-flag:: -keep-s-file
               -keep-s-files
 
diff --git a/testsuite/tests/driver/T4114a.hs b/testsuite/tests/driver/T4114a.hs
new file mode 100644 (file)
index 0000000..ff835d6
--- /dev/null
@@ -0,0 +1,14 @@
+-- Test the flage `force-no-intermediates` (issue #4114)
+module Main (main) where
+
+import T4114aSub
+
+keep, nokeep :: [FilePath]
+keep   = ["T4114a", "T4114aSub.hi", "T4114aSub.o", "T4114a.hi", "T4114a.o"]
+nokeep = [ ]
+
+
+main :: IO ()
+main = do
+  mapM_ assertNoKeep nokeep
+  mapM_ assertKeep keep
diff --git a/testsuite/tests/driver/T4114aSub.hs b/testsuite/tests/driver/T4114aSub.hs
new file mode 100644 (file)
index 0000000..782f32c
--- /dev/null
@@ -0,0 +1,20 @@
+module T4114aSub (assertKeep, assertNoKeep) where
+
+import Control.Monad    (unless, when)
+import System.Directory (doesFileExist)
+
+assertNoKeep :: FilePath -> IO ()
+assertNoKeep a =
+   whenM (doesFileExist a) $
+       error ("error: intermediate '" ++ a ++ "' exists")
+
+assertKeep :: FilePath -> IO ()
+assertKeep a =
+   unlessM (doesFileExist a) $
+       error ("error: intermediate '" ++ a ++ "' is missing")
+
+whenM :: Monad m => m Bool -> m () -> m ()
+whenM mp f = mp >>= \p -> when p f
+
+unlessM :: Monad m => m Bool -> m () -> m ()
+unlessM mp f = mp >>= \p -> unless p f
diff --git a/testsuite/tests/driver/T4114b.hs b/testsuite/tests/driver/T4114b.hs
new file mode 100644 (file)
index 0000000..07486c5
--- /dev/null
@@ -0,0 +1,13 @@
+-- Test the flage `force-no-intermediates` (issue #4114)
+module Main (main) where
+
+import T4114bSub
+
+keep, nokeep :: [FilePath]
+keep   = ["T4114b", "T4114bSub.o", "T4114b.o"]
+nokeep = ["T4114bSub.hi", "T4114b.hi"]
+
+main :: IO ()
+main = do
+  mapM_ assertNoKeep nokeep
+  mapM_ assertKeep keep
diff --git a/testsuite/tests/driver/T4114bSub.hs b/testsuite/tests/driver/T4114bSub.hs
new file mode 100644 (file)
index 0000000..41041d9
--- /dev/null
@@ -0,0 +1,20 @@
+module T4114bSub (assertKeep, assertNoKeep) where
+
+import Control.Monad    (unless, when)
+import System.Directory (doesFileExist)
+
+assertNoKeep :: FilePath -> IO ()
+assertNoKeep a =
+   whenM (doesFileExist a) $
+       error ("error: intermediate '" ++ a ++ "' exists")
+
+assertKeep :: FilePath -> IO ()
+assertKeep a =
+   unlessM (doesFileExist a) $
+       error ("error: intermediate '" ++ a ++ "' is missing")
+
+whenM :: Monad m => m Bool -> m () -> m ()
+whenM mp f = mp >>= \p -> when p f
+
+unlessM :: Monad m => m Bool -> m () -> m ()
+unlessM mp f = mp >>= \p -> unless p f
diff --git a/testsuite/tests/driver/T4114c.hs b/testsuite/tests/driver/T4114c.hs
new file mode 100644 (file)
index 0000000..f7802e1
--- /dev/null
@@ -0,0 +1,14 @@
+-- Test the flage `force-no-intermediates` (issue #4114)
+module Main (main) where
+
+import T4114cSub
+
+keep, nokeep :: [FilePath]
+keep   = ["T4114c", "T4114cSub.hi", "T4114c.hi"]
+nokeep = ["T4114cSub.o", "T4114c.o"]
+
+
+main :: IO ()
+main = do
+  mapM_ assertNoKeep nokeep
+  mapM_ assertKeep keep
diff --git a/testsuite/tests/driver/T4114cSub.hs b/testsuite/tests/driver/T4114cSub.hs
new file mode 100644 (file)
index 0000000..3ecd0c9
--- /dev/null
@@ -0,0 +1,20 @@
+module T4114cSub (assertKeep, assertNoKeep) where
+
+import Control.Monad    (unless, when)
+import System.Directory (doesFileExist)
+
+assertNoKeep :: FilePath -> IO ()
+assertNoKeep a =
+   whenM (doesFileExist a) $
+       error ("error: intermediate '" ++ a ++ "' exists")
+
+assertKeep :: FilePath -> IO ()
+assertKeep a =
+   unlessM (doesFileExist a) $
+       error ("error: intermediate '" ++ a ++ "' is missing")
+
+whenM :: Monad m => m Bool -> m () -> m ()
+whenM mp f = mp >>= \p -> when p f
+
+unlessM :: Monad m => m Bool -> m () -> m ()
+unlessM mp f = mp >>= \p -> unless p f
diff --git a/testsuite/tests/driver/T4114d.hs b/testsuite/tests/driver/T4114d.hs
new file mode 100644 (file)
index 0000000..2ff717b
--- /dev/null
@@ -0,0 +1,14 @@
+-- Test the flage `force-no-intermediates` (issue #4114)
+module Main (main) where
+
+import T4114dSub
+
+keep, nokeep :: [FilePath]
+keep   = ["T4114d", "T4114dSub.myhi", "T4114d.myhi"]
+nokeep = ["T4114dSub.myo", "T4114d.myo"]
+
+
+main :: IO ()
+main = do
+  mapM_ assertNoKeep nokeep
+  mapM_ assertKeep keep
diff --git a/testsuite/tests/driver/T4114dSub.hs b/testsuite/tests/driver/T4114dSub.hs
new file mode 100644 (file)
index 0000000..0616a21
--- /dev/null
@@ -0,0 +1,20 @@
+module T4114dSub (assertKeep, assertNoKeep) where
+
+import Control.Monad    (unless, when)
+import System.Directory (doesFileExist)
+
+assertNoKeep :: FilePath -> IO ()
+assertNoKeep a =
+   whenM (doesFileExist a) $
+       error ("error: intermediate '" ++ a ++ "' exists")
+
+assertKeep :: FilePath -> IO ()
+assertKeep a =
+   unlessM (doesFileExist a) $
+       error ("error: intermediate '" ++ a ++ "' is missing")
+
+whenM :: Monad m => m Bool -> m () -> m ()
+whenM mp f = mp >>= \p -> when p f
+
+unlessM :: Monad m => m Bool -> m () -> m ()
+unlessM mp f = mp >>= \p -> unless p f
index ae75823..2798dd5 100644 (file)
@@ -372,6 +372,11 @@ test('spacesInArgs',
      compile_and_run,
      [''])
 
+test( 'T4114a', normal, compile_and_run, ['-cpp'])
+test( 'T4114b', normal, compile_and_run, ['-no-keep-hi-files'])
+test( 'T4114c', normal, compile_and_run, ['-no-keep-o-files'])
+test( 'T4114d', normal, compile_and_run, ['-hisuf .myhi -osuf .myo -no-keep-o-files'])
+
 test('T5584',
      extra_clean(['T5584_in/A.hi-boot', 'T5584_out/T5584/A.o-boot']),
      run_command, ['$MAKE -s --no-print-directory T5584'])
index dd68de4..eb26409 100644 (file)
@@ -8,11 +8,23 @@ keepingIntermediatesOptions =
          , flagDescription = "Retain intermediate ``.hc`` files."
          , flagType = DynamicFlag
          }
+  , flag { flagName = "-keep-hi-files"
+         , flagDescription =
+           "Retain intermediate ``.hi`` files (the default)."
+         , flagType = DynamicFlag
+         , flagReverse = "-no-keep-hi-files"
+         }
   , flag { flagName = "-keep-llvm-file, -keep-llvm-files"
          , flagDescription = "Retain intermediate LLVM ``.ll`` files. "++
            "Implies :ghc-flag:`-fllvm`."
          , flagType = DynamicFlag
          }
+  , flag { flagName = "-keep-o-files"
+         , flagDescription =
+           "Retain intermediate ``.o`` files (the default)."
+         , flagType = DynamicFlag
+         , flagReverse = "-no-keep-o-files"
+         }
   , flag { flagName = "-keep-s-file, -keep-s-files"
          , flagDescription = "Retain intermediate ``.s`` files."
          , flagType = DynamicFlag