-
Notifications
You must be signed in to change notification settings - Fork 727
Implement recompilation checking for pre-build rules #11731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sheaf
wants to merge
2
commits into
haskell:master
Choose a base branch
from
sheaf:hooks-recomp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| {-# LANGUAGE DeriveGeneric #-} | ||
| {-# LANGUAGE DerivingStrategies #-} | ||
| {-# LANGUAGE DuplicateRecordFields #-} | ||
| {-# LANGUAGE FlexibleContexts #-} | ||
| {-# LANGUAGE GADTs #-} | ||
| {-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
| {-# LANGUAGE LambdaCase #-} | ||
|
|
@@ -12,7 +13,8 @@ | |
| -- | | ||
| -- Module: Distribution.Simple.SetupHooks.Internal | ||
| -- | ||
| -- Internal implementation module. | ||
| -- Internal implementation module for 'SetupHooks'. | ||
| -- | ||
| -- Users of @build-type: Hooks@ should import "Distribution.Simple.SetupHooks" | ||
| -- instead. | ||
| module Distribution.Simple.SetupHooks.Internal | ||
|
|
@@ -115,14 +117,20 @@ import Distribution.Types.LocalBuildConfig as LBC | |
| import Distribution.Types.TargetInfo | ||
| import Distribution.Verbosity | ||
|
|
||
| import qualified Data.ByteString as BS | ||
| import qualified Data.ByteString.Lazy as LBS | ||
| import Data.Coerce (coerce) | ||
| import Data.Either (fromRight) | ||
| import qualified Data.Graph as Graph | ||
| import Data.IORef (IORef, modifyIORef', newIORef, readIORef) | ||
| import qualified Data.List.NonEmpty as NE | ||
| import qualified Data.Map as Map | ||
| import qualified Data.Set as Set | ||
| import Data.Time (UTCTime) | ||
|
|
||
| import Distribution.Utils.Structured (structuredDecodeOrFailIO, structuredEncodeFile) | ||
|
|
||
| import System.Directory (doesFileExist) | ||
| import System.Directory (doesFileExist, getModificationTime) | ||
|
|
||
| -------------------------------------------------------------------------------- | ||
| -- SetupHooks | ||
|
|
@@ -849,7 +857,11 @@ executeRules = | |
| -- an external hooks executable. | ||
| executeRulesUserOrSystem | ||
| :: forall userOrSystem | ||
| . SScope userOrSystem | ||
| . ( Binary (RuleData userOrSystem) | ||
| , Structured (RuleData userOrSystem) | ||
| , Eq (RuleData userOrSystem) | ||
| ) | ||
| => SScope userOrSystem | ||
| -> (RuleId -> RuleDynDepsCmd userOrSystem -> IO (Maybe ([Rule.Dependency], LBS.ByteString))) | ||
| -> (RuleId -> RuleExecCmd userOrSystem -> IO ()) | ||
| -> Verbosity | ||
|
|
@@ -858,6 +870,16 @@ executeRulesUserOrSystem | |
| -> Map RuleId (RuleData userOrSystem) | ||
| -> IO () | ||
| executeRulesUserOrSystem scope runDepsCmdData runCmdData verbosity lbi tgtInfo allRules = do | ||
| -- Load the rule cache from the previous build. | ||
| -- Used to detect when rule definitions have changed. | ||
| oldRules <- do | ||
| cacheExists <- doesFileExist rulesCacheFile | ||
| if cacheExists | ||
| then do | ||
| -- NB: do a strict read to avoid retaining the file handle. | ||
| bs <- BS.readFile rulesCacheFile | ||
| fromRight Map.empty <$> structuredDecodeOrFailIO (LBS.fromStrict bs) | ||
| else return Map.empty | ||
| -- Compute all extra dynamic dependency edges. | ||
| dynDepsEdges <- | ||
| flip Map.traverseMaybeWithKey allRules $ | ||
|
|
@@ -939,7 +961,8 @@ executeRulesUserOrSystem scope runDepsCmdData runCmdData verbosity lbi tgtInfo a | |
| , " it is not in the appropriate 'autogenComponentModules' directory)" | ||
| ] | ||
|
|
||
| -- Run all the demanded rules, in dependency order. | ||
| -- Run all the demanded rules, in dependency order, propagating staleness. | ||
| staleRulesRef <- newIORef Set.empty | ||
| for_ sccs $ \(Graph.Node ruleVertex _) -> | ||
| -- Don't run a rule unless it is demanded. | ||
| unless (ruleVertex `Set.member` nonDemandedRuleVerts) $ do | ||
|
|
@@ -960,16 +983,27 @@ executeRulesUserOrSystem scope runDepsCmdData runCmdData verbosity lbi tgtInfo a | |
| case NE.nonEmpty missingRuleDeps of | ||
| Just missingDeps -> | ||
| errorOut $ CantFindSourceForRuleDependencies (toRuleBinary r) missingDeps | ||
| -- Dependencies OK: run the associated action. | ||
| -- Dependencies OK: check whether the rule is up to date before | ||
| -- deciding to run it. | ||
| Nothing -> do | ||
| let execCmd = ruleExecCmd scope cmds (snd <$> mbDyn) | ||
| runCmdData rId execCmd | ||
| -- Throw an error if running the action did not result in | ||
| -- the generation of outputs that we expected it to. | ||
| missingRuleResults <- filterM (missingDep mbWorkDir) $ NE.toList reslts | ||
| for_ (NE.nonEmpty missingRuleResults) $ \missingResults -> | ||
| errorOut $ MissingRuleOutputs (toRuleBinary r) missingResults | ||
| return () | ||
| let dynDeps = maybe [] fst (Map.lookup rId dynDepsEdges) | ||
| upToDate <- ruleUpToDate mbWorkDir oldRules staleRulesRef rId r dynDeps | ||
| if upToDate | ||
| then | ||
| info verbosity $ | ||
| "Rule " ++ show rId ++ " is up to date; skipping." | ||
| else do | ||
| modifyIORef' staleRulesRef (Set.insert rId) | ||
| let execCmd = ruleExecCmd scope cmds (snd <$> mbDyn) | ||
| runCmdData rId execCmd | ||
| -- Throw an error if running the action did not result in | ||
| -- the generation of outputs that we expected it to. | ||
| missingRuleResults <- filterM (missingDep mbWorkDir) $ NE.toList reslts | ||
| for_ (NE.nonEmpty missingRuleResults) $ \missingResults -> | ||
| errorOut $ MissingRuleOutputs (toRuleBinary r) missingResults | ||
| return () | ||
| -- Save the current rules to the cache for use in the next build. | ||
| structuredEncodeFile rulesCacheFile allRules | ||
| where | ||
| toRuleBinary :: RuleData userOrSystem -> RuleBinary | ||
| toRuleBinary = case scope of | ||
|
|
@@ -978,6 +1012,7 @@ executeRulesUserOrSystem scope runDepsCmdData runCmdData verbosity lbi tgtInfo a | |
| clbi = targetCLBI tgtInfo | ||
| mbWorkDir = mbWorkDirLBI lbi | ||
| compAutogenDir = autogenComponentModulesDir lbi clbi | ||
| rulesCacheFile = interpretSymbolicPath mbWorkDir (preBuildRulesCacheFile lbi clbi) | ||
| errorOut e = | ||
| dieWithException verbosity $ | ||
| SetupHooksException $ | ||
|
|
@@ -987,6 +1022,65 @@ directRuleDependencyMaybe :: Rule.Dependency -> Maybe RuleId | |
| directRuleDependencyMaybe (RuleDependency dep) = Just $ outputOfRule dep | ||
| directRuleDependencyMaybe (FileDependency{}) = Nothing | ||
|
|
||
| mtimeIfExists :: FilePath -> IO (Maybe UTCTime) | ||
| mtimeIfExists fp = do | ||
| ex <- doesFileExist fp | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Granting that it's brief, but this looks like a TOCTOU as well. |
||
| if ex | ||
| then Just <$> getModificationTime fp | ||
| else return Nothing | ||
|
|
||
| -- | Is the rule up to date (so that we can skip re-running it)? | ||
| -- | ||
| -- As per the SetupHooks documentation, a rule must be re-run if: | ||
| -- | ||
| -- - [N] the rule is new, or | ||
| -- - [S] the rule matches with an old rule, and either: | ||
| -- - [S1] an input to the rule has changed (either a file or rule dependency) | ||
| -- - [S2] the rule itself has changed | ||
| ruleUpToDate | ||
| :: Eq (RuleData userOrSystem) | ||
| => Maybe (SymbolicPath CWD (Dir Pkg)) | ||
| -- ^ working directory | ||
| -> Map RuleId (RuleData userOrSystem) | ||
| -- ^ old rules from the previous build | ||
| -> IORef (Set RuleId) | ||
| -- ^ rules that have been re-run | ||
| -> RuleId | ||
| -> RuleData userOrSystem | ||
| -> [Rule.Dependency] | ||
| -- ^ dynamic dependencies of this rule | ||
| -> IO Bool | ||
| ruleUpToDate mbWorkDir oldRules staleRulesRef rId rule dynDeps = do | ||
| staleRules <- readIORef staleRulesRef | ||
| if ruleChanged || any (`Set.member` staleRules) ruleDeps | ||
| then return False | ||
| else do | ||
| outMtimes <- traverse mtimeIfExists outputPaths | ||
| case sequenceA outMtimes of | ||
| -- At least one output is missing: must run the rule. | ||
| Nothing -> return False | ||
| Just outs -> | ||
| -- Re-run if an input is more recent than the oldest output. | ||
| case inputPaths of | ||
| [] -> return True | ||
| _ -> do | ||
| inMtimes <- traverse getModificationTime inputPaths | ||
| return (minimum outs >= maximum inMtimes) | ||
| where | ||
| i (Location dir file) = interpretSymbolicPath mbWorkDir (dir </> file) | ||
| allDeps = staticDependencies rule ++ dynDeps | ||
| ruleDeps = [outputOfRule ro | RuleDependency ro <- allDeps] | ||
| fileDeps = [loc | FileDependency loc <- allDeps] | ||
| inputPaths = map i fileDeps | ||
| outputPaths = fmap i (results rule) | ||
| ruleChanged = | ||
| case Map.lookup rId oldRules of | ||
| Just oldRule -> | ||
| -- Use the Eq instance to determine if the rule has changed | ||
| -- (as documented in the API). | ||
| oldRule /= rule | ||
| Nothing -> True | ||
|
|
||
| resolveDependency :: Verbosity -> RuleId -> Map RuleId (RuleData scope) -> Rule.Dependency -> IO Location | ||
| resolveDependency verbosity rId allRules = \case | ||
| FileDependency l -> return l | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
cabal-testsuite/PackageTests/SetupHooks/SetupHooksRecompRules/A.myPP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| a = True |
1 change: 1 addition & 0 deletions
1
cabal-testsuite/PackageTests/SetupHooks/SetupHooksRecompRules/B.myPP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| b = False |
1 change: 1 addition & 0 deletions
1
cabal-testsuite/PackageTests/SetupHooks/SetupHooksRecompRules/C.myPP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| c = 'x' |
7 changes: 7 additions & 0 deletions
7
cabal-testsuite/PackageTests/SetupHooks/SetupHooksRecompRules/Setup.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| module Main where | ||
|
|
||
| import Distribution.Simple ( defaultMainWithSetupHooks ) | ||
| import SetupHooks ( setupHooks ) | ||
|
|
||
| main :: IO () | ||
| main = defaultMainWithSetupHooks setupHooks |
112 changes: 112 additions & 0 deletions
112
cabal-testsuite/PackageTests/SetupHooks/SetupHooksRecompRules/SetupHooks.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| {-# LANGUAGE DataKinds #-} | ||
| {-# LANGUAGE DeriveAnyClass #-} | ||
| {-# LANGUAGE DeriveGeneric #-} | ||
| {-# LANGUAGE DerivingStrategies #-} | ||
| {-# LANGUAGE DuplicateRecordFields #-} | ||
| {-# LANGUAGE OverloadedStrings #-} | ||
| {-# LANGUAGE RecordWildCards #-} | ||
| {-# LANGUAGE StaticPointers #-} | ||
|
|
||
| module SetupHooks where | ||
|
|
||
| -- Cabal | ||
| import Distribution.Compat.Binary | ||
| import Distribution.Parsec | ||
| ( simpleParsec ) | ||
| import Distribution.Simple.LocalBuildInfo | ||
| ( interpretSymbolicPathLBI ) | ||
| import Distribution.Simple.Utils | ||
| ( warn, rewriteFileEx ) | ||
| import Distribution.Utils.Path | ||
| import Distribution.Verbosity | ||
|
|
||
| -- Cabal-hooks | ||
| import Distribution.Simple.SetupHooks | ||
|
|
||
| -- base | ||
| import Control.Monad.IO.Class | ||
| ( liftIO ) | ||
| import Data.Foldable | ||
| ( for_ ) | ||
| import Data.List | ||
| ( isSuffixOf ) | ||
| import qualified Data.List.NonEmpty as NE | ||
| ( NonEmpty(..) ) | ||
| import Data.String | ||
| ( fromString ) | ||
| import GHC.Generics | ||
|
|
||
| -- directory | ||
| import System.Directory | ||
| ( listDirectory ) | ||
|
|
||
| -- filepath | ||
| import System.FilePath | ||
| ( dropExtension ) | ||
|
|
||
| -------------------------------------------------------------------------------- | ||
|
|
||
| setupHooks :: SetupHooks | ||
| setupHooks = | ||
| noSetupHooks | ||
| { buildHooks = | ||
| noBuildHooks | ||
| { preBuildComponentRules = Just $ rules (static ()) preBuildRules | ||
| } | ||
| } | ||
|
|
||
| preBuildRules :: PreBuildComponentInputs -> RulesM () | ||
| preBuildRules (PreBuildComponentInputs { buildingWhat = what, localBuildInfo = lbi, targetInfo = tgt }) = do | ||
| let verbosityFlags = buildingWhatVerbosity what | ||
| clbi = targetCLBI tgt | ||
| autogenDir = autogenComponentModulesDir lbi clbi | ||
| srcDir = sameDirectory | ||
|
|
||
| -- Monitor .myPP files in the package directory. | ||
| let myPPGlob = | ||
| case simpleParsec "*.myPP" of | ||
| Just g -> g | ||
| Nothing -> error "SetupHooksRecompRules: failed to parse *.myPP glob" | ||
| addRuleMonitors [ monitorFileGlobExistence myPPGlob ] | ||
|
|
||
| -- Scan the package directory for .myPP files and register one | ||
| -- preprocessing rule per file. | ||
| allFiles <- liftIO $ listDirectory (interpretSymbolicPathLBI lbi srcDir) | ||
| for_ (filter (".myPP" `isSuffixOf`) allFiles) $ \fileName -> do | ||
| let baseName = dropExtension fileName | ||
| -- For A and B, bake in a constant verbosity so that their rules are | ||
| -- unaffected by the --verbose flag. C uses the actual verbosity, so | ||
| -- its rule changes when the verbosity changes. | ||
| ruleVerbosityFlags | ||
| | baseName `elem` ["A", "B"] = normal | ||
| | otherwise = verbosityFlags | ||
| registerRule_ (fromString $ "myPP " ++ baseName) $ | ||
| staticRule | ||
| (mkCommand (static Dict) (static runMyPP) $ | ||
| MyPPInput | ||
| { ppVerbosityFlags = ruleVerbosityFlags | ||
| , ppSrcDir = srcDir | ||
| , ppAutogenDir = autogenDir | ||
| , ppBaseName = baseName | ||
| }) | ||
| [ FileDependency $ Location srcDir (makeRelativePathEx fileName) ] | ||
| ( Location autogenDir (makeRelativePathEx baseName <.> "hs") NE.:| [] ) | ||
|
|
||
| -- | Preprocess a single .myPP file into a .hs module. | ||
| runMyPP :: MyPPInput -> IO () | ||
| runMyPP (MyPPInput {..}) = do | ||
| let verbosity = mkVerbosity defaultVerbosityHandles ppVerbosityFlags | ||
| warn verbosity $ "Running myPP preprocessor for " ++ ppBaseName | ||
| content <- readFile (getSymbolicPath ppSrcDir </> ppBaseName <.> "myPP") | ||
| rewriteFileEx verbosity (getSymbolicPath ppAutogenDir </> ppBaseName <.> "hs") $ | ||
| "module " ++ ppBaseName ++ " where\n" ++ content | ||
|
|
||
| data MyPPInput | ||
| = MyPPInput | ||
| { ppVerbosityFlags :: VerbosityFlags | ||
| , ppSrcDir :: SymbolicPath Pkg (Dir Source) | ||
| , ppAutogenDir :: SymbolicPath Pkg (Dir Source) | ||
| , ppBaseName :: String | ||
| } | ||
| deriving stock ( Show, Generic ) | ||
| deriving anyclass Binary |
1 change: 1 addition & 0 deletions
1
cabal-testsuite/PackageTests/SetupHooks/SetupHooksRecompRules/cabal.project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| packages: . |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this have a TOCTOU issue?