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

Commit

Permalink
feat: replace extraDepTarget with extraDepTargets
Browse files Browse the repository at this point in the history
  • Loading branch information
tydeu committed Aug 5, 2022
1 parent 0d4013a commit 35873c6
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 31 deletions.
5 changes: 5 additions & 0 deletions Lake/Build/Data.lean
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ abbrev BuildData : BuildKey → Type
| .targetFacet _ _ f => TargetData f
| .customTarget p t => CustomData (p, t)

instance : FamilyDef BuildData (.moduleFacet m f) (ModuleData f) := ⟨rfl⟩
instance : FamilyDef BuildData (.packageFacet p f) (PackageData f) := ⟨rfl⟩
instance : FamilyDef BuildData (.targetFacet p t f) (TargetData f) := ⟨rfl⟩
instance : FamilyDef BuildData (.customTarget p t) (CustomData (p,t)) := ⟨rfl⟩

--------------------------------------------------------------------------------
/-! ## Macros for Declaring Build Data -/
--------------------------------------------------------------------------------
Expand Down
5 changes: 1 addition & 4 deletions Lake/Build/Index.lean
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ dynamically typed equivalent.
cast (by rw [← h.family_key_eq_type]) build

def ExternLib.recBuildStatic (lib : ExternLib) : IndexBuildM (BuildJob FilePath) := do
if let some target := lib.pkg.findTargetConfig? (.str lib.name "static") then
lib.config.getJob <$> (target.build lib.pkg)
else
error "missing target for external library"
lib.config.getJob <$> fetch (lib.pkg.target lib.staticTargetName)

def ExternLib.recBuildShared (lib : ExternLib) : IndexBuildM (BuildJob FilePath) := do
buildLeanSharedLibOfStatic (← lib.static.fetch) lib.linkArgs
Expand Down
2 changes: 1 addition & 1 deletion Lake/Build/Info.lean
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ abbrev IndexT (m : Type → Type v) := EquipT (IndexBuildFn m) m
/-- The monad for build functions that are part of the index. -/
abbrev IndexBuildM := IndexT RecBuildM

/-- Build the given info using the Lake build index. -/
/-- Fetch the given info using the Lake build index. -/
@[inline] def BuildInfo.fetch (self : BuildInfo) [FamilyDef BuildData self.key α] : IndexBuildM α :=
fun build => cast (by simp) <| build self

Expand Down
6 changes: 6 additions & 0 deletions Lake/Build/Job.lean
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ namespace BuildJob

instance : Pure BuildJob := ⟨BuildJob.pure⟩

@[inline] protected def map (f : α → β) (self : BuildJob α) : BuildJob β :=
mk <| (fun (a,t) => (f a,t)) <$> self.toJob

instance : Functor BuildJob where
map := BuildJob.map

@[inline] protected def bindSync
(self : BuildJob α) (f : α → BuildTrace → JobM β) : SchedulerM (Job β) :=
self.toJob.bindSync fun (a, t) => f a t
Expand Down
21 changes: 13 additions & 8 deletions Lake/Build/Package.lean
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ Build the `extraDepTarget` for the package and its transitive dependencies.
Also fetch pre-built releases for the package's' dependencies.
-/
def Package.recBuildExtraDepTargets (self : Package) : IndexBuildM (BuildJob Unit) := do
let job ← self.deps.foldlM (do ·.mix <| ← ·.extraDep.fetch) BuildJob.nil
-- Fetch dependency's pre-built release if desired
let isDepPkg := self.name ≠ (← getWorkspace).root.name
let job ← do
if isDepPkg ∧ self.preferReleaseBuild then
job.mix <| ← self.release.fetch
let mut job := BuildJob.nil
-- Build dependencies' extra dep targets
for dep in self.deps do
job ← job.mix <| ← dep.extraDep.fetch
-- Fetch pre-built release if desired and this package is a dependency
if self.name ≠ (← getWorkspace).root.name ∧ self.preferReleaseBuild then
job ← job.mix <| ← self.release.fetch
-- Build this package's extra dep targets
for target in self.extraDepTargets do
if let some config := self.findTargetConfig? target then
job ← job.mix <| config.getJob <| ← fetch <| self.target target
else
return job
job.mix <| ← self.extraDepTarget
error s!"unknown target `{target}`"
return job

/-- The `PackageFacetConfig` for the builtin `dynlibFacet`. -/
def Package.extraDepFacetConfig : PackageFacetConfig extraDepFacet :=
Expand Down
4 changes: 1 addition & 3 deletions Lake/CLI/Build.lean
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ def resolveCustomTarget (pkg : Package)
throw <| CliError.invalidFacet name facet
else do
let info := pkg.target name
let some getJob := config.getJob?
| throw <| CliError.nonCliTarget name
have h : BuildData info.key = CustomData (pkg.name, name) := rfl
return {info, getJob := h ▸ getJob}
return {info, getJob := fun data => discard (h ▸ config.getJob data).toJob}

def resolveTargetInPackage (ws : Workspace)
(pkg : Package) (target facet : Name) : Except CliError BuildSpec :=
Expand Down
4 changes: 4 additions & 0 deletions Lake/Config/ExternLib.lean
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ That is, the package's `moreLinkArgs`.
-/
@[inline] def linkArgs (self : ExternLib) : Array String :=
self.pkg.moreLinkArgs

/-- The name of the package target used to build the external library's static binary. -/
@[inline] def staticTargetName (self : ExternLib) : Name :=
.str self.name "static"
17 changes: 5 additions & 12 deletions Lake/Config/Package.lean
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,8 @@ structure PackageConfig extends WorkspaceConfig, LeanConfig where
/-- The `Name` of the package. -/
name : Name

/--
An extra `OpaqueTarget` that should be built before the package.
`OpaqueTarget.collectList/collectArray` can be used combine multiple
extra targets into a single `extraDepTarget`.
**DEPRECATED:** Use separate custom `target` declarations instead?
-/
extraDepTarget : Option (SchedulerM (BuildJob Unit)) := none
/-- An `Array` of target names to build whenever the package is used. -/
extraDepTargets : Array Name := #[]

/--
Whether to compile each of the package's module into a native shared library
Expand Down Expand Up @@ -234,9 +227,9 @@ def manifestFile (self : Package) : FilePath :=
@[inline] def buildDir (self : Package) : FilePath :=
self.dir / self.config.buildDir

/-- The package's `extraDepTarget` configuration. -/
@[inline] def extraDepTarget (self : Package) : SchedulerM (BuildJob Unit) :=
self.config.extraDepTarget.getD (pure BuildJob.nil)
/-- The package's `extraDepTargets` configuration. -/
@[inline] def extraDepTargets (self : Package) : Array Name :=
self.config.extraDepTargets

/-- The package's `releaseRepo?` configuration. -/
@[inline] def releaseRepo? (self : Package) : Option String :=
Expand Down
6 changes: 3 additions & 3 deletions Lake/Config/TargetConfig.lean
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ structure TargetConfig (pkgName name : Name) : Type where
/-- The target's build function. -/
build : (pkg : Package) → [Fact (pkg.name = pkgName)] →
IndexBuildM (CustomData (pkgName, name))
/-- Does this target produce an associated asynchronous job? -/
getJob? : Option (CustomData (pkgName, name) → Job Unit)
/-- The target's resulting build job. -/
getJob : CustomData (pkgName, name) → BuildJob Unit
deriving Inhabited

/-- A smart constructor for target configurations that generate CLI targets. -/
@[inline] def mkTargetJobConfig
(build : (pkg : Package) → [Fact (pkg.name = pkgName)] → IndexBuildM (BuildJob α))
[h : FamilyDef CustomData (pkgName, name) (BuildJob α)] : TargetConfig pkgName name where
build := cast (by rw [← h.family_key_eq_type]) build
getJob? := some fun data => discard <| ofFamily data
getJob := fun data => discard <| ofFamily data

/-- A dependently typed configuration based on its registered package and name. -/
structure TargetDecl where
Expand Down

0 comments on commit 35873c6

Please sign in to comment.