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

Commit e134863

Browse files
committed
feat: library facets
1 parent fa3c7e2 commit e134863

15 files changed

+165
-87
lines changed

Diff for: Lake/Build.lean

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ import Lake.Build.Actions
88
import Lake.Build.Index
99
import Lake.Build.Module
1010
import Lake.Build.Package
11+
import Lake.Build.Library
1112
import Lake.Build.Imports

Diff for: Lake/Build/Data.lean

+23-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Lake
1414
--------------------------------------------------------------------------------
1515

1616
/--
17-
The open type family which maps a module facet's name to it build data
17+
The open type family which maps a module facet's name to its build data
1818
in the Lake build store. For example, a transitive × direct import pair
1919
for the `lean.imports` facet or an active build target for `lean.c`.
2020
@@ -24,7 +24,7 @@ as needed (via `module_data`).
2424
opaque ModuleData (facet : Name) : Type
2525

2626
/--
27-
The open type family which maps a package facet's name to it build data
27+
The open type family which maps a package facet's name to its build data
2828
in the Lake build store. For example, a transitive dependencies of the package
2929
for the facet `deps`.
3030
@@ -43,6 +43,19 @@ as needed (via `target_data`).
4343
-/
4444
opaque TargetData (facet : Name) : Type
4545

46+
/-
47+
The open type family which maps a library facet's name to its build data
48+
in the Lake build store. For example, an active build target for the `static`
49+
facet.
50+
51+
It is an open type, meaning additional mappings can be add lazily
52+
as needed (via `library_data`).
53+
-/
54+
abbrev LibraryData (facet : Name) := TargetData (`leanLib ++ facet)
55+
56+
instance [h : FamilyDef TargetData (`leanLib ++ facet) α] : FamilyDef LibraryData facet α :=
57+
⟨h.family_key_eq_type⟩
58+
4659
/--
4760
The open type family which maps a custom target (package × target name) to
4861
its build data in the Lake build store.
@@ -85,6 +98,14 @@ scoped macro (name := moduleDataDecl) doc?:optional(Parser.Command.docComment)
8598
let key := Name.quoteFrom id id.getId
8699
`($[$doc?]? family_def $id : $dty $key := $ty)
87100

101+
/-- Macro for declaring new `TargetData` for libraries. -/
102+
scoped macro (name := libraryDataDecl) doc?:optional(Parser.Command.docComment)
103+
"library_data " id:ident " : " ty:term : command => do
104+
let dty := mkCIdentFrom (← getRef) ``TargetData
105+
let key := Name.quoteFrom id id.getId
106+
let id := mkIdentFrom id <| id.getId.modifyBase (`leanLib ++ ·)
107+
`($[$doc?]? family_def $id : $dty (`leanLib ++ $key) := $ty)
108+
88109
/-- Macro for declaring new `TargetData`. -/
89110
scoped macro (name := targetDataDecl) doc?:optional(Parser.Command.docComment)
90111
"target_data " id:ident " : " ty:term : command => do

Diff for: Lake/Build/Executable.lean

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/-
2+
Copyright (c) 2022 Mac Malone. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Mac Malone
5+
-/
6+
import Lake.Build.Info
7+
import Lake.Build.Store
8+
import Lake.Build.Targets
9+
10+
namespace Lake
11+
12+
/-! # Build Executable -/
13+
14+
protected def LeanExe.recBuildExe
15+
(self : LeanExe) : IndexT RecBuildM ActiveFileTarget := do
16+
let (_, imports) ← self.root.imports.recBuild
17+
let linkTargets := #[Target.active <| ← self.root.o.recBuild]
18+
let mut linkTargets ← imports.foldlM (init := linkTargets) fun arr mod => do
19+
let mut arr := arr
20+
for facet in mod.nativeFacets do
21+
arr := arr.push <| Target.active <| ← recBuild <| mod.facet facet.name
22+
return arr
23+
let deps := (← recBuild <| self.pkg.facet `deps).push self.pkg
24+
for dep in deps do for lib in dep.externLibs do
25+
linkTargets := linkTargets.push <| Target.active <| ← lib.static.recBuild
26+
leanExeTarget self.file linkTargets self.linkArgs |>.activate

Diff for: Lake/Build/Facets.lean

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ package_data extraDep : ActiveOpaqueTarget
7171
/-! ## Target Facets -/
7272

7373
/-- A Lean library's Lean libraries. -/
74-
abbrev LeanLib.leanFacet := `leanLib.lean
75-
target_data leanLib.lean : ActiveOpaqueTarget
74+
abbrev LeanLib.leanFacet := `lean
75+
library_data lean : ActiveOpaqueTarget
7676

7777
/-- A Lean library's static binary. -/
78-
abbrev LeanLib.staticFacet := `leanLib.static
79-
target_data leanLib.static : ActiveFileTarget
78+
abbrev LeanLib.staticFacet := `static
79+
library_data static : ActiveFileTarget
8080

8181
/-- A Lean library's shared binary. -/
82-
abbrev LeanLib.sharedFacet := `leanLib.shared
83-
target_data leanLib.shared : ActiveFileTarget
82+
abbrev LeanLib.sharedFacet := `shared
83+
library_data shared : ActiveFileTarget
8484

8585
/-- A Lean binary executable. -/
8686
abbrev LeanExe.exeFacet := `leanExe

Diff for: Lake/Build/Index.lean

+7-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Copyright (c) 2022 Mac Malone. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Mac Malone
55
-/
6-
import Lake.Build.Roots
6+
import Lake.Build.Executable
77
import Lake.Build.Topological
88

99
/-!
@@ -51,12 +51,11 @@ def recBuildWithIndex : (info : BuildInfo) → IndexBuildM (BuildData info.key)
5151
error "target's name in the configuration does not match the name it was registered with"
5252
else
5353
error s!"could not build `{target}` of `{pkg.name}` -- target not found"
54-
| .leanLib lib =>
55-
mkTargetFacetBuild LeanLib.leanFacet lib.recBuildLean
56-
| .staticLeanLib lib =>
57-
mkTargetFacetBuild LeanLib.staticFacet lib.recBuildStatic
58-
| .sharedLeanLib lib =>
59-
mkTargetFacetBuild LeanLib.sharedFacet lib.recBuildShared
54+
| .libraryFacet lib facet => do
55+
if let some config := (← getWorkspace).findLibraryFacetConfig? facet then
56+
config.build lib
57+
else
58+
error s!"do not know how to build library facet `{facet}`"
6059
| .leanExe exe =>
6160
mkTargetFacetBuild LeanExe.exeFacet exe.recBuildExe
6261
| .staticExternLib lib =>
@@ -108,13 +107,11 @@ export BuildInfo (build)
108107
[FamilyDef ModuleData facet (ActiveBuildTarget α)] : OpaqueTarget :=
109108
self.facet facet |>.target
110109

111-
/-! ### Pure Lean Lib Targets -/
110+
/-! ### Lean Library Targets -/
112111

113112
@[inline] protected def LeanLib.leanTarget (self : LeanLib) : OpaqueTarget :=
114113
self.lean.target
115114

116-
/-! ### Native Lean Lib Targets -/
117-
118115
@[inline] protected def LeanLib.staticLibTarget (self : LeanLib) : FileTarget :=
119116
self.static.target.withInfo self.sharedLibFile
120117

Diff for: Lake/Build/Info.lean

+13-27
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ namespace Lake
2222
inductive BuildInfo
2323
| moduleFacet (module : Module) (facet : Name)
2424
| packageFacet (package : Package) (facet : Name)
25-
| leanLib (lib : LeanLib)
26-
| staticLeanLib (lib : LeanLib)
27-
| sharedLeanLib (lib : LeanLib)
25+
| libraryFacet (lib : LeanLib) (facet : Name)
2826
| leanExe (exe : LeanExe)
2927
| staticExternLib (lib : ExternLib)
3028
| sharedExternLib (lib : ExternLib)
@@ -45,14 +43,8 @@ abbrev Package.facetBuildKey (facet : Name) (self : Package) : BuildKey :=
4543
abbrev Package.targetBuildKey (target : Name) (self : Package) : BuildKey :=
4644
.customTarget self.name target
4745

48-
abbrev LeanLib.leanBuildKey (self : LeanLib) : BuildKey :=
49-
.targetFacet self.pkg.name self.name leanFacet
50-
51-
abbrev LeanLib.staticBuildKey (self : LeanLib) : BuildKey :=
52-
.targetFacet self.pkg.name self.name staticFacet
53-
54-
abbrev LeanLib.sharedBuildKey (self : LeanLib) : BuildKey :=
55-
.targetFacet self.pkg.name self.name sharedFacet
46+
abbrev LeanLib.facetBuildKey (self : LeanLib) (facet : Name) : BuildKey :=
47+
.targetFacet self.pkg.name self.name (`leanLib ++ facet)
5648

5749
abbrev LeanExe.buildKey (self : LeanExe) : BuildKey :=
5850
.targetFacet self.pkg.name self.name exeFacet
@@ -69,9 +61,7 @@ abbrev ExternLib.sharedBuildKey (self : ExternLib) : BuildKey :=
6961
abbrev BuildInfo.key : (self : BuildInfo) → BuildKey
7062
| moduleFacet m f => m.facetBuildKey f
7163
| packageFacet p f => p.facetBuildKey f
72-
| leanLib l => l.leanBuildKey
73-
| staticLeanLib l => l.staticBuildKey
74-
| sharedLeanLib l => l.sharedBuildKey
64+
| libraryFacet l f => l.facetBuildKey f
7565
| leanExe x => x.buildKey
7666
| staticExternLib l => l.staticBuildKey
7767
| sharedExternLib l => l.sharedBuildKey
@@ -91,16 +81,8 @@ instance [FamilyDef CustomData (p.name, t) α]
9181
: FamilyDef BuildData (BuildInfo.key (.customTarget p t)) α where
9282
family_key_eq_type := by unfold BuildData; simp
9383

94-
instance [FamilyDef TargetData LeanLib.leanFacet α]
95-
: FamilyDef BuildData (BuildInfo.key (.leanLib l)) α where
96-
family_key_eq_type := by unfold BuildData; simp
97-
98-
instance [FamilyDef TargetData LeanLib.staticFacet α]
99-
: FamilyDef BuildData (BuildInfo.key (.staticLeanLib l)) α where
100-
family_key_eq_type := by unfold BuildData; simp
101-
102-
instance [FamilyDef TargetData LeanLib.sharedFacet α]
103-
: FamilyDef BuildData (BuildInfo.key (.sharedLeanLib l)) α where
84+
instance [FamilyDef TargetData (`leanLib ++ f) α]
85+
: FamilyDef BuildData (BuildInfo.key (.libraryFacet l f)) α where
10486
family_key_eq_type := by unfold BuildData; simp
10587

10688
instance [FamilyDef TargetData LeanExe.exeFacet α]
@@ -194,17 +176,21 @@ abbrev Package.extraDep (self : Package) : BuildInfo :=
194176
abbrev Package.customTarget (target : Name) (self : Package) : BuildInfo :=
195177
.customTarget self target
196178

179+
/-- Build info of the Lean library's Lean binaries. -/
180+
abbrev LeanLib.facet (self : LeanLib) (facet : Name) : BuildInfo :=
181+
.libraryFacet self facet
182+
197183
/-- Build info of the Lean library's Lean binaries. -/
198184
abbrev LeanLib.lean (self : LeanLib) : BuildInfo :=
199-
.leanLib self
185+
self.facet leanFacet
200186

201187
/-- Build info of the Lean library's static binary. -/
202188
abbrev LeanLib.static (self : LeanLib) : BuildInfo :=
203-
.staticLeanLib self
189+
self.facet staticFacet
204190

205191
/-- Build info of the Lean library's shared binary. -/
206192
abbrev LeanLib.shared (self : LeanLib) : BuildInfo :=
207-
.sharedLeanLib self
193+
self.facet sharedFacet
208194

209195
/-- Build info of the Lean executable. -/
210196
abbrev LeanExe.exe (self : LeanExe) : BuildInfo :=

Diff for: Lake/Build/Roots.lean renamed to Lake/Build/Library.lean

+23-18
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ Copyright (c) 2022 Mac Malone. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Mac Malone
55
-/
6-
import Lake.Build.Info
7-
import Lake.Build.Store
8-
import Lake.Build.Targets
6+
import Lake.Build.Index
97

108
namespace Lake
119

12-
/-! # Build Static Lib -/
10+
/-! # Build Lean & Static Lib -/
1311

1412
/-- Build and collect the specified facet of the library's local modules. -/
1513
def LeanLib.recBuildLocalModules
@@ -33,11 +31,19 @@ protected def LeanLib.recBuildLean
3331
ActiveTarget.collectOpaqueArray (i := PUnit) <|
3432
← self.recBuildLocalModules #[Module.leanBinFacet]
3533

34+
/-- The `LibraryFacetConfig` for the builtin `leanFacet`. -/
35+
def LeanLib.leanFacetConfig : LibraryFacetConfig leanFacet :=
36+
mkFacetTargetConfig (·.recBuildLean)
37+
3638
protected def LeanLib.recBuildStatic
3739
(self : LeanLib) : IndexT RecBuildM ActiveFileTarget := do
3840
let oTargets := (← self.recBuildLocalModules self.nativeFacets).map Target.active
3941
staticLibTarget self.staticLibFile oTargets |>.activate
4042

43+
/-- The `LibraryFacetConfig` for the builtin `staticFacet`. -/
44+
def LeanLib.staticFacetConfig : LibraryFacetConfig staticFacet :=
45+
mkFacetTargetConfig (·.recBuildStatic)
46+
4147
/-! # Build Shared Lib -/
4248

4349
/--
@@ -76,18 +82,17 @@ protected def LeanLib.recBuildShared
7682
let linkTargets := (← self.recBuildLinks).map Target.active
7783
leanSharedLibTarget self.sharedLibFile linkTargets self.linkArgs |>.activate
7884

79-
/-! # Build Executable -/
85+
/-- The `LibraryFacetConfig` for the builtin `sharedFacet`. -/
86+
def LeanLib.sharedFacetConfig : LibraryFacetConfig sharedFacet :=
87+
mkFacetTargetConfig (·.recBuildShared)
8088

81-
protected def LeanExe.recBuildExe
82-
(self : LeanExe) : IndexT RecBuildM ActiveFileTarget := do
83-
let (_, imports) ← self.root.imports.recBuild
84-
let linkTargets := #[Target.active <| ← self.root.o.recBuild]
85-
let mut linkTargets ← imports.foldlM (init := linkTargets) fun arr mod => do
86-
let mut arr := arr
87-
for facet in mod.nativeFacets do
88-
arr := arr.push <| Target.active <| ← recBuild <| mod.facet facet.name
89-
return arr
90-
let deps := (← recBuild <| self.pkg.facet `deps).push self.pkg
91-
for dep in deps do for lib in dep.externLibs do
92-
linkTargets := linkTargets.push <| Target.active <| ← lib.static.recBuild
93-
leanExeTarget self.file linkTargets self.linkArgs |>.activate
89+
open LeanLib in
90+
/--
91+
A library facet name to build function map that contains builders for
92+
the initial set of Lake package facets (e.g., `lean`, `static`, and `shared`).
93+
-/
94+
def initLibraryFacetConfigs : DNameMap LibraryFacetConfig :=
95+
DNameMap.empty
96+
|>.insert leanFacet leanFacetConfig
97+
|>.insert staticFacet staticFacetConfig
98+
|>.insert sharedFacet sharedFacetConfig

Diff for: Lake/CLI/Build.lean

+7-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Copyright (c) 2021 Mac Malone. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Mac Malone
55
-/
6-
import Lake.Build
6+
import Lake.Build.Index
77
import Lake.CLI.Error
88

99
namespace Lake
@@ -56,13 +56,11 @@ def resolveModuleTarget (ws : Workspace) (mod : Module) (facet : Name) : Except
5656
else
5757
throw <| CliError.unknownFacet "module" facet
5858

59-
def resolveLibTarget (lib : LeanLib) (facet : Name) : Except CliError BuildSpec :=
60-
if facet.isAnonymous || facet = `lean then
59+
def resolveLibTarget (ws : Workspace) (lib : LeanLib) (facet : Name) : Except CliError BuildSpec :=
60+
if facet.isAnonymous then
6161
return mkBuildSpec lib.lean
62-
else if facet = `static then
63-
return mkBuildSpec lib.static
64-
else if facet = `shared then
65-
return mkBuildSpec lib.shared
62+
else if let some config := ws.findLibraryFacetConfig? facet then do
63+
mkConfigBuildSpec "library" (lib.facet facet) config rfl
6664
else
6765
throw <| CliError.unknownFacet "library" facet
6866

@@ -100,7 +98,7 @@ def resolveTargetInPackage (ws : Workspace)
10098
else if let some lib := pkg.findExternLib? target then
10199
resolveExternLibTarget lib facet
102100
else if let some lib := pkg.findLeanLib? target then
103-
resolveLibTarget lib facet
101+
resolveLibTarget ws lib facet
104102
else if let some mod := pkg.findModule? target then
105103
resolveModuleTarget ws mod facet
106104
else
@@ -126,7 +124,7 @@ def resolveTargetInWorkspace (ws : Workspace)
126124
else if let some lib := ws.findExternLib? target then
127125
Array.singleton <$> resolveExternLibTarget lib facet
128126
else if let some lib := ws.findLeanLib? target then
129-
Array.singleton <$> resolveLibTarget lib facet
127+
Array.singleton <$> resolveLibTarget ws lib facet
130128
else if let some pkg := ws.findPackage? target then
131129
resolvePackageTarget ws pkg facet
132130
else if let some mod := ws.findModule? target then

Diff for: Lake/CLI/Main.lean

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Mac Malone
55
-/
66
import Lake.Load
7+
import Lake.Build.Imports
78
import Lake.Util.Error
89
import Lake.Util.MainM
910
import Lake.Util.Cli

Diff for: Lake/Config/FacetConfig.lean

+6
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ abbrev PackageFacetConfig := FacetConfig PackageData Package
4040

4141
/-- A package facet declaration from a configuration file. -/
4242
abbrev PackageFacetDecl := NamedConfigDecl PackageFacetConfig
43+
44+
/-- A library facet's declarative configuration. -/
45+
abbrev LibraryFacetConfig := FacetConfig LibraryData LeanLib
46+
47+
/-- A library facet declaration from a configuration file. -/
48+
abbrev LibraryFacetDecl := NamedConfigDecl LibraryFacetConfig

0 commit comments

Comments
 (0)