import Lake
open Lake DSL
/-!
## Mathlib dependencies on upstream projects
-/
require "leanprover-community" / "batteries" @ git "main"
require "leanprover-community" / "Qq" @ git "master"
require "leanprover-community" / "aesop" @ git "master"
require "leanprover-community" / "proofwidgets" @ git "main"
with NameMap.empty.insert `errorOnBuild
"ProofWidgets failed to reuse pre-built JS code. \
Please report this issue on the Lean Zulip."
require "leanprover-community" / "importGraph" @ git "main"
require "leanprover-community" / "LeanSearchClient" @ git "main"
require "leanprover-community" / "plausible" @ git "main"
/-!
## Options for building mathlib
-/
/-- These options are used as `leanOptions`, prefixed by `` `weak``, so that
`lake build` uses them, as well as `Archive` and `Counterexamples`. -/
abbrev mathlibOnlyLinters : Array LeanOption := #[
⟨`linter.mathlibStandardSet, true⟩,
-- Explicitly enable the header linter, since the standard set is defined in `Mathlib.Init`
-- but we want to run this linter in files imported by `Mathlib.Init`.
⟨`linter.style.header, true⟩,
⟨`linter.checkInitImports, true⟩,
⟨`linter.allScriptsDocumented, true⟩,
⟨`linter.style.longFile, .ofNat 1500⟩,
-- ⟨`linter.nightlyRegressionSet, true⟩,
-- `latest_import.yml` uses this comment: if you edit it, make sure that the workflow still works
]
/-- These options are passed as `leanOptions` to building mathlib, as well as the
`Archive` and `Counterexamples`. -/
abbrev mathlibLeanOptions := #[
⟨`pp.unicode.fun, true⟩, -- pretty-prints `fun a ↦ b`
⟨`autoImplicit, false⟩,
⟨`maxSynthPendingDepth, .ofNat 3⟩,
⟨`weak.linter.unreachableTactic, false⟩, -- superseded by the unused tactic linter
⟨`weak.linter.unnecessarySeqFocus, false⟩, -- superseded by the unused tactic linter
] ++ -- options that are used in `lake build`
mathlibOnlyLinters.map fun s ↦ { s with name := `weak ++ s.name }
/-- These options are passed as `leanOptions` when building `MathlibTest`. We don't use the typical
mathlib options in order to simulate the default downstream environment. -/
abbrev mathlibTestOptions : Array LeanOption := #[
⟨`pp.mvars.anonymous, false⟩ -- test stability: pretty-print `?m.37` as `?_`
]
package mathlib where
testDriver := "MathlibTest"
lintDriver := "batteries/runLinter"
lintDriverArgs := #["Mathlib"]
-- Run the builtin linting steps in addition to the `lintDriver` set above.
builtinLint := true
-- A version of Mathlib only supports the toolchain it is built with.
fixedToolchain := true
-- Mathlib oleans are built on Linux CI and used across platforms.
platformIndependent := true
-- Mathlib currently expects artifacts to be in the build directory.
restoreAllArtifacts := true
requiresModuleSystem := true
-- These are additional settings which do not affect the lake hash,
-- so they can be enabled in CI and disabled locally or vice versa.
-- Warning: Do not put any options here that actually change the olean files,
-- or inconsistent behavior may result
-- weakLeanArgs := #[]
/-!
## Mathlib libraries
-/
@[default_target]
lean_lib Mathlib where
-- Enforce Mathlib's default linters and style options.
leanOptions := mathlibLeanOptions
-- NB. When adding further libraries, check if they should be excluded from `getLeanLibs` in
-- `scripts/mk_all.lean`.
lean_lib Cache where
globs := #[`Cache.+]
-- The `Cache` modules do not use the module system, but they import each other and thus
-- the `requiresModuleSystem` package option set above.
allowNonModules := true
lean_lib MathlibTest where
globs := #[`MathlibTest.+]
leanOptions := mathlibTestOptions
allowNonModules := true
lean_lib Archive where
leanOptions := mathlibLeanOptions
lean_lib Counterexamples where
leanOptions := mathlibLeanOptions
/-- Wanted statements: `Wanted/X/Y/Z.lean` contains the `proof_wanted` statements
corresponding to `Mathlib/X/Y/Z.lean`. Each file carries a copyright header naming the
author of the original statements, but beyond that contains only imports, context setup
(`open`/`namespace`/`variable`) and `proof_wanted` statements; in particular there are no
module docstrings, so the header style linter is disabled.
`proof_wanted` elaborates to a `private` placeholder declaration, so every module here
consists solely of private declarations; the `privateModule` linter is disabled accordingly
(neither `@[expose] public section` nor a `public` modifier suppresses it, since the
placeholder is unconditionally `private`). -/
lean_lib Wanted where
leanOptions := mathlibLeanOptions.push ⟨`weak.linter.style.header, false⟩
|>.push ⟨`weak.linter.privateModule, false⟩
/-- Additional documentation in the form of modules that only contain module docstrings. -/
lean_lib docs where
roots := #[`docs]
/-!
## Executables provided by Mathlib
-/
/--
`lake exe autolabel 150100` adds a topic label to PR `150100` if there is a unique choice.
This requires GitHub CLI `gh` to be installed!
Calling `lake exe autolabel` without a PR number will print the result without applying
any labels online.
-/
lean_exe autolabel where
srcDir := "scripts"
/-- `lake exe cache get` retrieves precompiled `.olean` files from a central server. -/
lean_exe cache where
root := `Cache.Main
-- As for `lean_lib Cache`: the root does not use the module system either, and the
-- executable's configuration does not inherit the library's.
allowNonModules := true
/-- `lake exe cache-test` runs the cache tool's unit tests (container URL
construction, per-repo trust-ordered allowlist, `--cache-from` parsing).
Runnable standalone — does not require building Mathlib or `MathlibTest`. -/
lean_exe «cache-test» where
root := `Cache.Test
/-- `lake exe check-yaml` verifies that all declarations referred to in `docs/*.yaml` files exist. -/
lean_exe «check-yaml» where
srcDir := "scripts"
supportInterpreter := true
/-- `lake exe mk_all` constructs the files containing all imports for a project. -/
lean_exe mk_all where
-- TODO: modulise this script, so remove the need for this option
allowNonModules := true
srcDir := "scripts"
supportInterpreter := true
/-- `lake exe lint-style` runs text-based style linters. -/
lean_exe «lint-style» where
srcDir := "scripts"
supportInterpreter := true
-- TODO: modulise this script, so remove the need for this option
allowNonModules := true
/-- `lake exe check-title-labels` checks if a PR title obeys some basic formatting requirements.
Currently, these checks are quite lenient, but could be made stricter in the future. -/
lean_exe «check-title-labels» where
srcDir := "scripts"
-- TODO: provided for backwards compatibility; delete after #44306
lean_exe «check_title_labels» where
srcDir := "scripts"
-- TODO: modulise this script, so remove the need for this option
allowNonModules := true
/-- `lake exe nightly-testing-checklist` reports nightly-testing branch status. -/
lean_exe «nightly-testing-checklist» where
srcDir := "scripts"
lean_exe mathlib_test_executable where
root := `MathlibTest.MathlibTestExecutable
/-!
## Other configuration
-/
/--
When a package depending on Mathlib updates its dependencies,
update its toolchain to match Mathlib's and fetch the new cache.
-/
post_update pkg do
let rootPkg ← getRootPackage
if rootPkg.baseName = pkg.baseName then
return -- do not run in Mathlib itself
if (← IO.getEnv "MATHLIB_NO_CACHE_ON_UPDATE") != some "1" then
-- Check if Lake version matches toolchain version
let toolchainFile := rootPkg.dir / "lean-toolchain"
let toolchainContent ← IO.FS.readFile toolchainFile
let toolchainVersion := match toolchainContent.trimAscii.copy.splitOn ":" with
| [_, version] => version
| _ => toolchainContent.trimAscii.copy -- fallback to full content if format is unexpected
-- Lean.versionString does not start with a `v`, while the `lean-toolchain` file is flexible.
let toolchainVersion := (toolchainVersion.dropPrefix "v").copy
if Lean.versionString ≠ toolchainVersion then
IO.println s!"Not running `lake exe cache get` yet, as \
the `lake` version ({Lean.versionString}) does not match \
the toolchain version ({toolchainVersion}) in the project.\n\
You should run `lake exe cache get` manually."
return
let exeFile ← runBuild cache.fetch
-- Run the command in the root package directory,
-- which is the one that holds the .lake folder and lean-toolchain file.
let cwd ← IO.Process.getCurrentDir
let exitCode ← try
IO.Process.setCurrentDir rootPkg.dir
env exeFile.toString #["get"]
finally
IO.Process.setCurrentDir cwd
if exitCode ≠ 0 then
error s!"{pkg.baseName}: failed to fetch cache"
¤ Dauer der Verarbeitung: 0.1 Sekunden
(vorverarbeitet am 2026-11-24)
¤
*© Formatika GbR, Deutschland