Skip to content

Commit

Permalink
Expand --keep-temp-files
Browse files Browse the repository at this point in the history
Currently, `cabal repl` has a `--keep-temp-files` option, and
`cabal.project` has a `keep-temp-files` option but it only effects
Haddock builds.

This patch adds `--keep-temp-files` to `CommonSetupFlags`, making it
available to all commands. The expanded `--keep-temp-files` flag is used
for the `cabal repl` command and Haddock builds (retaining compatibility
with the previous behavior) but is also used to determine when to keep
response files.
  • Loading branch information
9999years committed Aug 29, 2024
1 parent 302b99d commit e36ac84
Show file tree
Hide file tree
Showing 19 changed files with 240 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ md5CheckGenericPackageDescription proxy = md5Check proxy

md5CheckLocalBuildInfo :: Proxy LocalBuildInfo -> Assertion
md5CheckLocalBuildInfo proxy = md5Check proxy
0x2c8550e1552f68bf169fafbfcd8f845a
0xd8d6695c12b407e453f192a4d833cb79
3 changes: 3 additions & 0 deletions Cabal/src/Distribution/Simple/GHC/Build/ExtraSources.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Distribution.Simple.GHC.Build.Modules
import Distribution.Simple.GHC.Build.Utils
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Program.Types
import Distribution.Simple.Setup.Common (commonSetupTempFileOptions)
import Distribution.System (Arch (JavaScript), Platform (..))
import Distribution.Types.ComponentLocalBuildInfo
import Distribution.Utils.Path
Expand Down Expand Up @@ -177,11 +178,13 @@ buildExtraSources
comp = compiler lbi
platform = hostPlatform lbi
responseFileDir = coerceSymbolicPath buildTargetDir
tempFileOptions = commonSetupTempFileOptions $ buildingWhatCommonFlags buildingWhat
runGhcProg =
runGHCWithResponseFile
"ghc.rsp"
Nothing
responseFileDir
tempFileOptions
verbosity
ghcProg
comp
Expand Down
4 changes: 4 additions & 0 deletions Cabal/src/Distribution/Simple/GHC/Build/Link.hs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ linkOrLoadComponent
isIndef = componentIsIndefinite clbi
mbWorkDir = mbWorkDirLBI lbi
responseFileDir = coerceSymbolicPath buildTargetDir
tempFileOptions = commonSetupTempFileOptions $ buildingWhatCommonFlags what

-- See Note [Symbolic paths] in Distribution.Utils.Path
i = interpretSymbolicPathLBI lbi
Expand Down Expand Up @@ -204,6 +205,7 @@ linkOrLoadComponent
"ghc.rsp"
Nothing
responseFileDir
tempFileOptions
verbosity
ghcProg
comp
Expand Down Expand Up @@ -748,12 +750,14 @@ runReplOrWriteFlags ghcProg lbi rflags ghcOpts pkg_name target responseFileDir =
common = configCommonFlags $ configFlags lbi
mbWorkDir = mbWorkDirLBI lbi
verbosity = fromFlag $ setupVerbosity common
tempFileOptions = commonSetupTempFileOptions common
in case replOptionsFlagOutput (replReplOptions rflags) of
NoFlag ->
runGHCWithResponseFile
"ghc.rsp"
Nothing
responseFileDir
tempFileOptions
verbosity
ghcProg
comp
Expand Down
2 changes: 2 additions & 0 deletions Cabal/src/Distribution/Simple/GHC/Build/Modules.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,13 @@ buildHaskellModules numJobs ghcProg mbMainFile inputModules buildTargetDir neede

mbWorkDir = mbWorkDirLBI lbi
responseFileDir = coerceSymbolicPath buildTargetDir
tempFileOptions = commonSetupTempFileOptions $ buildingWhatCommonFlags what
runGhcProg =
runGHCWithResponseFile
"ghc.rsp"
Nothing
responseFileDir
tempFileOptions
verbosity
ghcProg
comp
Expand Down
10 changes: 3 additions & 7 deletions Cabal/src/Distribution/Simple/Haddock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,9 @@ import Distribution.Simple.Program.GHC
import qualified Distribution.Simple.Program.HcPkg as HcPkg
import Distribution.Simple.Program.ResponseFile
import Distribution.Simple.Register
import Distribution.Simple.Setup.Common
import Distribution.Simple.Setup.Haddock
import Distribution.Simple.Setup.Hscolour
import Distribution.Simple.Setup
import Distribution.Simple.SetupHooks.Internal
( BuildHooks (..)
, BuildingWhat (..)
, noBuildHooks
)
import qualified Distribution.Simple.SetupHooks.Internal as SetupHooks
Expand Down Expand Up @@ -265,6 +262,7 @@ haddock_setupHooks
mbWorkDir = flagToMaybe $ haddockWorkingDir flags
comp = compiler lbi
platform = hostPlatform lbi
config = configFlags lbi

quickJmpFlag = haddockQuickJump flags'
flags = case haddockTarget of
Expand All @@ -282,9 +280,7 @@ haddock_setupHooks
flag f = fromFlag $ f flags

tmpFileOpts =
defaultTempFileOptions
{ optKeepTempFiles = flag haddockKeepTempFiles
}
commonSetupTempFileOptions $ configCommonFlags config
htmlTemplate =
fmap toPathTemplate . flagToMaybe . haddockHtmlLocation $
flags
Expand Down
7 changes: 4 additions & 3 deletions Cabal/src/Distribution/Simple/Program/GHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import Distribution.Simple.Program.Find (getExtraPathEnv)
import Distribution.Simple.Program.ResponseFile
import Distribution.Simple.Program.Run
import Distribution.Simple.Program.Types
import Distribution.Simple.Utils (defaultTempFileOptions)
import Distribution.Simple.Utils (TempFileOptions)
import Distribution.System
import Distribution.Types.ComponentId
import Distribution.Types.ParStrat
Expand Down Expand Up @@ -645,14 +645,15 @@ runGHCWithResponseFile
:: FilePath
-> Maybe TextEncoding
-> SymbolicPath Pkg (Dir Response)
-> TempFileOptions
-> Verbosity
-> ConfiguredProgram
-> Compiler
-> Platform
-> Maybe (SymbolicPath CWD (Dir Pkg))
-> GhcOptions
-> IO ()
runGHCWithResponseFile fileNameTemplate encoding responseFileDir verbosity ghcProg comp platform maybeWorkDir opts = do
runGHCWithResponseFile fileNameTemplate encoding responseFileDir tempFileOptions verbosity ghcProg comp platform maybeWorkDir opts = do
invocation <- ghcInvocation verbosity ghcProg comp platform maybeWorkDir opts

-- Don't use response files if the first argument is `--interactive`, for
Expand Down Expand Up @@ -686,7 +687,7 @@ runGHCWithResponseFile fileNameTemplate encoding responseFileDir verbosity ghcPr

withResponseFile
verbosity
defaultTempFileOptions
tempFileOptions
maybeWorkDir
responseFileDir
fileNameTemplate
Expand Down
1 change: 1 addition & 0 deletions Cabal/src/Distribution/Simple/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module Distribution.Simple.Setup
, globalCommand
, CommonSetupFlags (..)
, defaultCommonSetupFlags
, commonSetupTempFileOptions
, ConfigFlags (..)
, emptyConfigFlags
, defaultConfigFlags
Expand Down
29 changes: 27 additions & 2 deletions Cabal/src/Distribution/Simple/Setup/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Distribution.Simple.Setup.Common
( CommonSetupFlags (..)
, defaultCommonSetupFlags
, withCommonSetupOptions
, commonSetupTempFileOptions
, CopyDest (..)
, configureCCompiler
, configureLinker
Expand Down Expand Up @@ -85,6 +86,13 @@ data CommonSetupFlags = CommonSetupFlags
--
-- TODO: this one should not be here, it's just that the silly
-- UserHooks stop us from passing extra info in other ways
, setupKeepTempFiles :: Flag Bool
-- ^ When this flag is set, temporary files will be kept after building.
--
-- Note: Keeping temporary files is important functionality for HLS, which
-- runs @cabal repl@ with a fake GHC to get CLI arguments. It will need the
-- temporary files (including multi unit repl response files) to stay, even
-- after the @cabal repl@ command exits.
}
deriving (Eq, Show, Read, Generic)

Expand All @@ -106,6 +114,15 @@ defaultCommonSetupFlags =
, setupDistPref = NoFlag
, setupCabalFilePath = NoFlag
, setupTargets = []
, setupKeepTempFiles = NoFlag
}

-- | Get `TempFileOptions` that respect the `setupKeepTempFiles` flag.
commonSetupTempFileOptions :: CommonSetupFlags -> TempFileOptions
commonSetupTempFileOptions options =
TempFileOptions
{ optKeepTempFiles =
fromFlagOrDefault False (setupKeepTempFiles options)
}

commonSetupOptions :: ShowOrParseArgs -> [OptionField CommonSetupFlags]
Expand All @@ -124,8 +141,16 @@ commonSetupOptions showOrParseArgs =
setupCabalFilePath
(\v flags -> flags{setupCabalFilePath = v})
(reqSymbolicPathArgFlag "PATH")
-- NB: no --working-dir flag, as that value is populated using the
-- global flag (see Distribution.Simple.Setup.Global.globalCommand).
, -- NB: no --working-dir flag, as that value is populated using the
-- global flag (see Distribution.Simple.Setup.Global.globalCommand).
option
""
["keep-temp-files"]
( "Keep temporary files."
)
setupKeepTempFiles
(\v flags -> flags{setupKeepTempFiles = v})
trueArg
]

withCommonSetupOptions
Expand Down
Loading

0 comments on commit e36ac84

Please sign in to comment.