dymokomi/luce-base
Source of release 0.44.0
0.44.0 / src / support / native_packages.lucb
#==============================================================================================
#
# native_packages - Native inputs merged across a package closure
#
# DESCRIPTION:
# Native inputs belong to their declaring package. Relative paths are resolved before
# import closures are merged or sources are handed to another compiler's workspace.
#
#==============================================================================================
import c
import io
import strings
import support.list
import support.in_arena
import support.manifest
import support.modules
## One native input: its kind and its resolved value.
pub struct Input:
## The kind of input: sources, link_search, libraries, frameworks, or pkg_config.
pub let kind: str
## The resolved path or name.
pub let value: c.str
## The native inputs gathered across a package's import closure, each kept once.
pub struct Inputs:
## The gathered inputs, in the order first seen.
pub var entries: list.List[Input]
var roots: list.List[str]
## An empty set of inputs.
pub static func create() -> Inputs!:
return Inputs(try list.List[Input].create(8), try list.List[str].create(8))
## Add the native inputs of the package at `path`, once per package root.
pub func collect(path: str) -> !:
let package = try manifest.load(try modules.canonical(path))
if package.root.length == 0:
return
for root in self.roots.span():
if root == package.root:
return
try self.roots.push(package.root)
try self.add("sources", package.sources, package.root)
try self.add("link_search", package.link_search, package.root)
try self.add("libraries", package.libraries, "")
try self.add("frameworks", package.frameworks, "")
try self.add("pkg_config", package.pkg_config, "")
func add(kind: str, values: c.str[], root: str) -> !:
for value in values:
let resolved = try in_arena.join(root, (str)value)
var duplicate = false
for existing in self.entries.span():
if existing.kind == kind and (str)existing.value == resolved:
duplicate = true
if not duplicate:
try self.entries.push(Input(kind, (c.str)resolved))
## The values gathered for one `kind`, in order.
pub func values(kind: str) -> c.str[]!:
var selected = try list.List[c.str].create(4)
for entry in self.entries.span():
if entry.kind == kind:
try selected.push(entry.value)
return selected.span()
## Replace `package`'s native fields with the merged inputs.
pub func apply(package: manifest.Manifest*) -> !:
package.sources = try self.values("sources")
package.link_search = try self.values("link_search")
package.libraries = try self.values("libraries")
package.frameworks = try self.values("frameworks")
package.pkg_config = try self.values("pkg_config")
## Write the inputs as NUL-separated `native` records.
pub func write(out: io.Writer) -> !:
for entry in self.entries.span():
try out.write(f"native\0{entry.kind}\0{(str)entry.value}\0")