Lake
Lake (Lean Make) is a new build system and package manager for Lean 4.
With Lake, the package's configuration is written in Lean inside a dedicated lakefile.lean stored in the root of the package's directory.
Each lakefile.lean includes a package declaration (akin to main) which defines the package's basic configuration. It also typically includes build configurations for different targets (e.g., Lean libraries and binary executables) and Lean scripts to run on the command line (via lake script run).
This README provides information about Lake relative to the current commit. If you are looking for documentation for the Lake version shipped with a given Lean nightly, you should look at the README of that version.
Table of Contents
- Getting Lake
- Creating and Building a Package
- Package Configuration Options
- Defining Build Targets
- Adding Dependencies
- Writing and Running Scripts
- Building and Running Lake from the Source
Getting Lake
Lake is part of the lean4 repository and is distributed along with its official releases (e.g., as part of the elan toolchain). So if you have installed a semi-recent Lean 4 nightly, you should already have it!
Note that the Lake included with Lean is not updated as frequently as this repository, so some bleeding edge features may be missing. If you want to build the latest version from the source yourself, check out the build instructions at the bottom of this README.
Creating and Building a Package
To create a new package, either run lake init to setup the package in the current directory or lake new to create it in a new directory. For example, we could create the package hello like so:
$ mkdir hello
$ cd hello
$ lake init hello
or like so:
$ lake new hello
$ cd hello
Either way, Lake will initialize a git repository in the package directory with a basic .gitignore that ignores the build directory (i.e., build) where Lake outputs build files.
It will also create the root Lean file for the package's library, which uses the capitalized version of the package's name (e.g., Hello.lean in this example), and the root file for the package's binary Main.lean. They contain the following dummy "Hello World" program split across the two files:
Hello.lean
def hello := "world"Main.lean
import Hello
def main : IO Unit :=
IO.println s!"Hello, {hello}!"Lake also creates a basic lakefile.lean for the package along with a lean-toolchain file that contains the version string of the currently active Lean, which tells elan to use that Lean toolchain for the package.
lakefile.lean
import Lake
open Lake DSL
package hello {
-- add package configuration options here
}
lean_lib Hello {
-- add library configuration options here
}
@[defaultTarget]
lean_exe hello {
root := `Main
}The command lake build can then be used to build the package (and its dependencies, if it has them) into a native executable. The result will be placed in build/bin.
$ lake build
...
$ ./build/bin/hello
Hello, world!
Examples of different package configurations can be found in the examples folder of this repository.
Package Configuration Options
Lake provides a large assortment of configuration options for packages.
Workspace Options
Workspace options are shared across a package and its dependencies.
packagesDir: The directory to which Lake should download remote dependencies. Defaults tolean_packages.
Per Package Options
srcDir: The directory containing the package's Lean source files. Defaults to the package's directory. (This will be passed toleanas the-Roption.)buildDir: The directory to which Lake should output the package's build results. Defaults tobuild.oleanDir: The build subdirectory to which Lake should output the package's.oleanfiles. Defaults tolib.irDir: The build subdirectory to which Lake should output the package's intermediary results (e.g.,.cand.ofiles). Defaults toir.libDir: The build subdirectory to which Lake should output the package's libraries. Defaults tolib.binDir: The build subdirectory to which Lake should output the package's binary executables. Defaults tobin.extraDepTarget: An extraOpaqueTargetthat should be built before the package.Target.collectOpaqueList/collectOpaqueArraycan be used combine multiple extra targets into a singleextraDepTarget.precompileModules: Whether to compile each module into a native shared library that is loaded whenever the module is imported. This speeds up the evaluation of metaprograms and enables the interpreter to run functions marked@[extern]. Defaults tofalse.isLeanOnly: Whether the package is "Lean-only". A Lean-only package does not produce native files for modules (e.g..c,.o). Defaults tofalse. SettingprecompileModulestotruewill override this setting and produce native files anyway (as they are needed to build module dynlibs).moreServerArgs: Additional arguments to pass to the Lean language server (i.e.,lean --server) launched bylake serve.moreLeanArgs: AnArrayof additional arguments to pass toleanwhile compiling Lean source files.moreLeancArgs: AnArrayof additional arguments to pass toleancwhile compiling the C source files generated bylean. Lake already passes-O3and-DNDEBUGautomatically, but you can change this by, for example, adding-O0and-UNDEBUG.moreLibTargets: AnArrayof additional libraryFileTargets (beyond the package's and its dependencies' Lean libraries) to build and link to the package's binaries (and to dependent packages' binaries). DEPRECATED: Use separateextern_libtargets instead.defaultFacet: ThePackageFacetto build on a barelake buildof the package. Can be one ofexe,leanLib,staticLib,sharedLib, ornone. Defaults toexe. Seelake help buildfor more info on build facets. DEPRECATED: Package facets will be removed in a future version of Lake. Use a separatelean_liborlean_exedefault target instead.
Defining Build Targets
A Lake package can have many build targets, such as different Lean libraries and multiple binary executables. Any number of these declarations can be marked with the @[defaultTarget] attribute to tell Lake to build them on a bare lake build of the package.
Lean Libraries
A Lean library target defines a set of Lean modules available to import and how to build them.
Syntax
lean_lib «target-name» {
-- configuration options go here
}Configuration Options
roots: The root module(s) of the library. Submodules of these roots (e.g.,Lib.FooofLib) are considered part of the library. Defaults to a single root of the library's upper camel case name.globs: AnArrayof moduleGlobs to build for the library. Defaults to aGlob.oneof each of the library'sroots. Submodule globs build every source file within their directory. Local imports of glob'ed files (i.e., fellow modules of the workspace) are also recursively built.libName: The name of the library. Used as a base for the file names of its static and dynamic binaries. Defaults to the upper camel case name of the target.moreLinkArgs: AnArrayof additional arguments to pass toleancwhile linking the shared library. These will come after the paths of libraries built with the package'smoreLibTargets.
Binary Executables
A Lean executable target builds a binary executable from a Lean module with a main function.
Syntax
lean_exe «target-name» {
-- configuration options go here
}Configuration Options
root: The root module of the binary executable. Should include amaindefinition that will serve as the entry point of the program. The root is built by recursively building its local imports (i.e., fellow modules of the workspace). Defaults to the name of the target.exeName: The name of the binary executable. Defaults to the target name with any.replaced with a-.supportInterpreter: Whether to expose symbols within the executable to the Lean interpreter. This allows the executable to interpret Lean files (e.g., viaLean.Elab.runFrontend). Implementation-wise, this passes-rdynamicto the linker when building on a non-Windows systems. Defaults tofalse.moreLinkArgs: AnArrayof additional arguments to pass toleancwhen linking the binary executable. These will come after the paths of libraries built with the package'smoreLibTargets.
External Libraries
A external library target is a non-Lean static library that will be linked to the binaries of the package and its dependents (e.g., their shared libraries and executables).
Syntax
extern_lib «target-name» :=
-- term of type `FileTarget` that builds the external libraryAdding Dependencies
Lake packages can have dependencies. Dependencies are other Lake packages the current package needs in order to function. They can be sourced directly from a local folder (e.g., a subdirectory of the package) or come from remote Git repositories. For example, one can depend on the Lean 4 port of mathlib like so:
package hello
require mathlib from git
"https://github.com/leanprover-community/mathlib4.git"@"master"The next run of lake build (or refreshing dependencies in an editor like VSCode) will clone the mathlib repository and build it. Information on the specific revision cloned will then be saved to manifest.json in the workspace's packageDir (by default, lean_packages) to enable reproducibility. To update mathlib after this, you will need to run lake update -- other commands do not update resolved dependencies.
Syntax of require
The require command has two forms:
require foo from "path"/"to"/"local"/"package" with ["optional","args"]
require bar from git "url.git"@"rev"/"optional"/"path-to"/"dir-with-pkg"The first form adds a local dependency and the second form adds a Git dependency. For a Git dependency, the revision can be a commit hash, branch, or tag. Also, the @"rev" and /"path-to"/"term" parts of the require are optional.
Both forms also support an optional with clause to specify arguments to pass to the dependency's package configuration (i.e., same as args in a lake build -- invocation). The elements of both the from and with clauses are proper terms so normal computation is supported within them (though parentheses made be required to disambiguate the syntax).
Writing and Running Scripts
A configuration file can also contain a number of scripts declaration. A script is an arbitrary (args : List String) → ScriptM UInt32 definition that can be run by lake script run. For example, given the following lakefile.lean:
import Lake
open Lake DSL
package scripts
/--
Display a greeting
USAGE:
lake run greet [name]
Greet the entity with the given name. Otherwise, greet the whole world.
-/
script greet (args) do
if h : 0 < args.length then
IO.println s!"Hello, {args.get 0 h}!"
else
IO.println "Hello, world!"
return 0The script greet can be run like so:
$ lake script run greet
Hello, world!
$ lake script run greet me
Hello, me!
You can print the docstring of a script with lake script doc:
$ lake script doc greet
Display a greeting
USAGE:
lake run greet [name]
Greet the entity with the given name. Otherwise, greet the whole world.
Building and Running Lake from the Source
If you already have a Lean installation with lake packaged with it, you can build a new lake by just running lake build.
Otherwise, there is a pre-packaged build.sh shell script that can be used to build Lake. It passes it arguments down to a make command. So, if you have more than one core, you will probably want to use a -jX option to specify how many build tasks you want it to run in parallel. For example:
$ ./build.sh -j4After building, the lake binary will be located at build/bin/lake and the library's .olean files will be located in build/lib.
Building with Nix Flakes
It is also possible to build Lake with the Nix setup buildLeanPackage from the lean4 repository. To do so, you need to have Nix installed with flakes enabled. It is recommended to also set up the Lean 4 binary cache as described in the Lean 4 repository.
It is then possible to build Lake with nix build . or run it from anywhere with nix run github:leanprover/lake.
A development environment with Lean 4 installed can be loaded automatically by running nix develop or automatically on cd with direnv by running direnv allow.
The versions of nixpkgs and lean4 are fixed to specific hashes. They can be updated by running nix flake update.
Thank Anders Christiansen Sørby (@Anderssorby) for this support!
Augmenting Lake's Search Path
The lake executable needs to know where to find the .olean files for the modules used in the package configuration file. Lake will intelligently setup an initial search path based on the location of its own executable and lean.
Specifically, if Lake is co-located with lean (i.e., there is lean executable in the same directory as itself), it will assume it was installed with Lean and that both Lean and Lake are located in with Lean's .olean files at .olean files at lean --print-prefix to find Lean's home and assume that its .olean files are at and that lake is at with its .olean files at .
This search path can be augmented by including other directories of .olean files in the LEAN_PATH environment variable. Such directories will take precedence over the initial search path, so LEAN_PATH can also be used to correct Lake's search if the .olean files for Lean (or Lake itself) are in non-standard locations.