Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cabal/Distribution/Simple/Configure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,8 +1036,8 @@ checkForeignDeps pkg lbi verbosity = do

builds program args = do
tempDir <- getTemporaryDirectory
withTempFile False tempDir ".c" $ \cName cHnd ->
withTempFile False tempDir "" $ \oNname oHnd -> do
withTempFile tempDir ".c" $ \cName cHnd ->
withTempFile tempDir "" $ \oNname oHnd -> do
hPutStrLn cHnd program
hClose cHnd
hClose oHnd
Expand Down
6 changes: 3 additions & 3 deletions Cabal/Distribution/Simple/GHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,13 @@ configureToolchain ghcProg ghcInfo =
configureLd' :: Verbosity -> ConfiguredProgram -> IO [ProgArg]
configureLd' verbosity ldProg = do
tempDir <- getTemporaryDirectory
ldx <- withTempFile False tempDir ".c" $ \testcfile testchnd ->
withTempFile False tempDir ".o" $ \testofile testohnd -> do
ldx <- withTempFile tempDir ".c" $ \testcfile testchnd ->
withTempFile tempDir ".o" $ \testofile testohnd -> do
hPutStrLn testchnd "int foo() { return 0; }"
hClose testchnd; hClose testohnd
rawSystemProgram verbosity ghcProg ["-c", testcfile,
"-o", testofile]
withTempFile False tempDir ".o" $ \testofile' testohnd' ->
withTempFile tempDir ".o" $ \testofile' testohnd' ->
do
hClose testohnd'
_ <- rawSystemProgramStdout verbosity ldProg
Expand Down
31 changes: 19 additions & 12 deletions Cabal/Distribution/Simple/Haddock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ import Distribution.InstalledPackageInfo
( InstalledPackageInfo )
import Distribution.Simple.Utils
( die, copyFileTo, warn, notice, intercalate, setupMessage
, createDirectoryIfMissingVerbose, withTempFile, copyFileVerbose
, withTempDirectory, matchFileGlob
, createDirectoryIfMissingVerbose
, TempFileOptions(..), defaultTempFileOptions
, withTempFileEx, copyFileVerbose
, withTempDirectoryEx, matchFileGlob
, findFileWithExtension, findFile )
import Distribution.Text
( display, simpleParse )
Expand Down Expand Up @@ -212,24 +214,24 @@ haddock pkg_descr lbi suffixes flags = do
let
doExe com = case (compToExe com) of
Just exe -> do
withTempDirectory verbosity keepTempFiles (buildDir lbi) "tmp" $ \tmp -> do
withTempDirectoryEx verbosity tmpFileOpts (buildDir lbi) "tmp" $ \tmp -> do
let bi = buildInfo exe
exeArgs <- fromExecutable verbosity tmp lbi exe clbi htmlTemplate
exeArgs' <- prepareSources verbosity tmp
lbi isVersion2 bi (commonArgs `mappend` exeArgs)
runHaddock verbosity keepTempFiles confHaddock exeArgs'
runHaddock verbosity tmpFileOpts confHaddock exeArgs'
Nothing -> do
warn (fromFlag $ haddockVerbosity flags)
"Unsupported component, skipping..."
return ()
case comp of
CLib lib -> do
withTempDirectory verbosity keepTempFiles (buildDir lbi) "tmp" $ \tmp -> do
withTempDirectoryEx verbosity tmpFileOpts (buildDir lbi) "tmp" $ \tmp -> do
let bi = libBuildInfo lib
libArgs <- fromLibrary verbosity tmp lbi lib clbi htmlTemplate
libArgs' <- prepareSources verbosity tmp
lbi isVersion2 bi (commonArgs `mappend` libArgs)
runHaddock verbosity keepTempFiles confHaddock libArgs'
runHaddock verbosity tmpFileOpts confHaddock libArgs'
CExe _ -> when (flag haddockExecutables) $ doExe comp
CTest _ -> when (flag haddockTestSuites) $ doExe comp
CBench _ -> when (flag haddockBenchmarks) $ doExe comp
Expand All @@ -240,6 +242,7 @@ haddock pkg_descr lbi suffixes flags = do
where
verbosity = flag haddockVerbosity
keepTempFiles = flag haddockKeepTempFiles
tmpFileOpts = defaultTempFileOptions { optKeepTempFiles = keepTempFiles }
flag f = fromFlag $ f flags
htmlTemplate = fmap toPathTemplate . flagToMaybe . haddockHtmlLocation $ flags

Expand Down Expand Up @@ -439,26 +442,30 @@ getGhcLibDir verbosity lbi isVersion2
----------------------------------------------------------------------------------------------

-- | Call haddock with the specified arguments.
runHaddock :: Verbosity -> Bool -> ConfiguredProgram -> HaddockArgs -> IO ()
runHaddock verbosity keepTempFiles confHaddock args = do
runHaddock :: Verbosity
-> TempFileOptions
-> ConfiguredProgram
-> HaddockArgs
-> IO ()
runHaddock verbosity tmpFileOpts confHaddock args = do
let haddockVersion = fromMaybe (error "unable to determine haddock version")
(programVersion confHaddock)
renderArgs verbosity keepTempFiles haddockVersion args $ \(flags,result)-> do
renderArgs verbosity tmpFileOpts haddockVersion args $ \(flags,result)-> do

rawSystemProgram verbosity confHaddock flags

notice verbosity $ "Documentation created: " ++ result


renderArgs :: Verbosity
-> Bool
-> TempFileOptions
-> Version
-> HaddockArgs
-> (([String], FilePath) -> IO a)
-> IO a
renderArgs verbosity keepTempFiles version args k = do
renderArgs verbosity tmpFileOpts version args k = do
createDirectoryIfMissingVerbose verbosity True outputDir
withTempFile keepTempFiles outputDir "haddock-prolog.txt" $ \prologFileName h -> do
withTempFileEx tmpFileOpts outputDir "haddock-prolog.txt" $ \prologFileName h -> do
do
hPutStrLn h $ fromFlag $ argPrologue args
hClose h
Expand Down
6 changes: 3 additions & 3 deletions Cabal/Distribution/Simple/LHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,13 @@ configureToolchain lhcProg =
configureLd :: Verbosity -> ConfiguredProgram -> IO [ProgArg]
configureLd verbosity ldProg = do
tempDir <- getTemporaryDirectory
ldx <- withTempFile False tempDir ".c" $ \testcfile testchnd ->
withTempFile False tempDir ".o" $ \testofile testohnd -> do
ldx <- withTempFile tempDir ".c" $ \testcfile testchnd ->
withTempFile tempDir ".o" $ \testofile testohnd -> do
hPutStrLn testchnd "int foo() { return 0; }"
hClose testchnd; hClose testohnd
rawSystemProgram verbosity lhcProg ["-c", testcfile,
"-o", testofile]
withTempFile False tempDir ".o" $ \testofile' testohnd' ->
withTempFile tempDir ".o" $ \testofile' testohnd' ->
do
hClose testohnd'
_ <- rawSystemProgramStdout verbosity ldProg
Expand Down
2 changes: 1 addition & 1 deletion Cabal/Distribution/Simple/SrcDist.hs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ sdist pkg mb_lbi flags mkTmpDir pps =

Nothing -> do
createDirectoryIfMissingVerbose verbosity True tmpTargetDir
withTempDirectory verbosity False tmpTargetDir "sdist." $ \tmpDir -> do
withTempDirectory verbosity tmpTargetDir "sdist." $ \tmpDir -> do
let targetDir = tmpDir </> tarBallName pkg'
generateSourceDir targetDir pkg'
targzFile <- createArchive verbosity pkg' mb_lbi tmpDir targetPref
Expand Down
48 changes: 36 additions & 12 deletions Cabal/Distribution/Simple/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ module Distribution.Simple.Utils (
FileGlob(..),

-- * temp files and dirs
withTempFile,
withTempDirectory,
TempFileOptions(..), defaultTempFileOptions,
withTempFile, withTempFileEx,
withTempDirectory, withTempDirectoryEx,

-- * .cabal and .buildinfo files
defaultPackageDesc,
Expand Down Expand Up @@ -906,17 +907,33 @@ copyDirectoryRecursiveVerbose verbosity srcDir destDir = do
---------------------------
-- Temporary files and dirs

-- | Advanced options for 'withTempFile' and 'withTempDirectory'.
data TempFileOptions = TempFileOptions {
optKeepTempFiles :: Bool -- ^ Keep temporary files?
}

defaultTempFileOptions :: TempFileOptions
defaultTempFileOptions = TempFileOptions { optKeepTempFiles = False }

-- | Use a temporary filename that doesn't already exist.
--
withTempFile :: Bool -- ^ Keep temporary files?
-> FilePath -- ^ Temp dir to create the file in
-> String -- ^ File name template. See 'openTempFile'.
-> (FilePath -> Handle -> IO a) -> IO a
withTempFile keepTempFiles tmpDir template action =
withTempFile :: FilePath -- ^ Temp dir to create the file in
-> String -- ^ File name template. See 'openTempFile'.
-> (FilePath -> Handle -> IO a) -> IO a
withTempFile tmpDir template action =
withTempFileEx defaultTempFileOptions tmpDir template action

-- | A version of 'withTempFile' that additionally takes a 'TempFileOptions'
-- argument.
withTempFileEx :: TempFileOptions
-> FilePath -- ^ Temp dir to create the file in
-> String -- ^ File name template. See 'openTempFile'.
-> (FilePath -> Handle -> IO a) -> IO a
withTempFileEx opts tmpDir template action =
Exception.bracket
(openTempFile tmpDir template)
(\(name, handle) -> do hClose handle
unless keepTempFiles $ removeFile name)
unless (optKeepTempFiles opts) $ removeFile name)
(uncurry action)

-- | Create and use a temporary directory.
Expand All @@ -930,12 +947,19 @@ withTempFile keepTempFiles tmpDir template action =
-- @src/sdist.342@.
--
withTempDirectory :: Verbosity
-> Bool -- ^ Keep temporary files?
-> FilePath -> String -> (FilePath -> IO a) -> IO a
withTempDirectory _verbosity keepTempFiles targetDir template =
-> FilePath -> String -> (FilePath -> IO a) -> IO a
withTempDirectory verbosity targetDir template =
withTempDirectoryEx verbosity defaultTempFileOptions targetDir template

-- | A version of 'withTempDirectory' that additionally takes a
-- 'TempFileOptions' argument.
withTempDirectoryEx :: Verbosity
-> TempFileOptions
-> FilePath -> String -> (FilePath -> IO a) -> IO a
withTempDirectoryEx _verbosity opts targetDir template =
Exception.bracket
(createTempDirectory targetDir template)
(unless keepTempFiles . removeDirectoryRecursive)
(unless (optKeepTempFiles opts) . removeDirectoryRecursive)

-----------------------------------
-- Safely reading and writing files
Expand Down
2 changes: 1 addition & 1 deletion cabal-install/Distribution/Client/Haddock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ regenerateHaddockIndex verbosity pkgs conf index = do

createDirectoryIfMissing True destDir

withTempDirectory verbosity False destDir "tmphaddock" $ \tempDir -> do
withTempDirectory verbosity destDir "tmphaddock" $ \tempDir -> do

let flags = [ "--gen-contents"
, "--gen-index"
Expand Down
2 changes: 1 addition & 1 deletion cabal-install/Distribution/Client/Install.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ installLocalTarballPackage
-> IO BuildResult
installLocalTarballPackage verbosity jobLimit pkgid tarballPath installPkg = do
tmp <- getTemporaryDirectory
withTempDirectory verbosity False tmp (display pkgid) $ \tmpDirPath ->
withTempDirectory verbosity tmp (display pkgid) $ \tmpDirPath ->
onFailure UnpackFailed $ do
let relUnpackedPath = display pkgid
absUnpackedPath = tmpDirPath </> relUnpackedPath
Expand Down
2 changes: 1 addition & 1 deletion cabal-install/Distribution/Client/SrcDist.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ sdist flags exflags = do
=<< readPackageDescription verbosity
=<< defaultPackageDesc verbosity
let withDir = if not needMakeArchive then (\f -> f tmpTargetDir)
else withTempDirectory verbosity False tmpTargetDir "sdist."
else withTempDirectory verbosity tmpTargetDir "sdist."
-- 'withTempDir' fails if we don't create 'tmpTargetDir'...
when needMakeArchive $
createDirectoryIfMissingVerbose verbosity True tmpTargetDir
Expand Down