Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
feat: make .lean hashes cross-platform compatible (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kha committed May 23, 2023
1 parent 257fc59 commit 5a8f71c
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lake/Build/Module.lean
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def Module.buildUnlessUpToDate (mod : Module)
(depTrace : BuildTrace) : BuildM PUnit := do
let isOldMode ← getIsOldMode
let argTrace : BuildTrace := pureHash mod.leanArgs
let srcTrace : BuildTrace ← computeTrace mod.leanFile
let srcTrace : BuildTrace ← computeTrace { path := mod.leanFile : TextFilePath }
let modTrace := (← getLeanTrace).mix <| argTrace.mix <| srcTrace.mix depTrace
let modUpToDate ← do
if isOldMode then
Expand Down
13 changes: 13 additions & 0 deletions Lake/Build/Trace.lean
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ def computeFileHash (file : FilePath) : IO Hash :=

instance : ComputeHash FilePath IO := ⟨computeFileHash⟩

/--
A wrapper around `FilePath` that adjusts its `ComputeHash` implementation
to normalize `\r\n` sequences to `\n` for cross-platform compatibility. -/
structure TextFilePath where
path : FilePath

instance : ComputeHash TextFilePath IO where
computeHash file := do
let text ← IO.FS.readFile file.path
let text := text.replace "\r\n" "\n"
return Hash.ofString text

instance [ComputeHash α m] [Monad m] : ComputeHash (Array α) m where
computeHash ar := ar.foldlM (fun b a => Hash.mix b <$> computeHash a) Hash.nil

Expand Down Expand Up @@ -171,6 +183,7 @@ def getFileMTime (file : FilePath) : IO MTime :=
return (← file.metadata).modified

instance : GetMTime FilePath := ⟨getFileMTime⟩
instance : GetMTime TextFilePath := ⟨(getFileMTime ·.path)⟩

/-- Check if the info's `MTIme` is at least `depMTime`. -/
def checkIfNewer [GetMTime i] (info : i) (depMTime : MTime) : BaseIO Bool :=
Expand Down
198 changes: 198 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5a8f71c

Please sign in to comment.