110 lines
3.1 KiB
Python
110 lines
3.1 KiB
Python
from typing import TextIO
|
|
|
|
from osdk.model import ComponentManifest, TargetManifest, Props
|
|
from osdk.ninja import Writer
|
|
from osdk.logger import Logger
|
|
from osdk.context import Context, contextFor
|
|
from osdk import shell, rules
|
|
|
|
logger = Logger("builder")
|
|
|
|
|
|
def gen(out: TextIO, context: Context):
|
|
writer = Writer(out)
|
|
|
|
target = context.target
|
|
|
|
writer.comment("File generated by the build system, do not edit")
|
|
writer.newline()
|
|
|
|
writer.separator("Tools")
|
|
|
|
writer.variable("cincs", " ".join(
|
|
map(lambda i: f"-I{i}", context.cincls())))
|
|
|
|
writer.variable("cdefs", " ".join(context.cdefs()))
|
|
|
|
writer.newline()
|
|
|
|
for i in target.tools:
|
|
tool = target.tools[i]
|
|
rule = rules.rules[i]
|
|
writer.variable(i, tool.cmd)
|
|
writer.variable(
|
|
i + "flags", " ".join(rule.args + tool.args))
|
|
writer.rule(
|
|
i, f"{tool.cmd} {rule.rule.replace('$flags',f'${i}flags')}", depfile=rule.deps)
|
|
writer.newline()
|
|
|
|
writer.separator("Components")
|
|
|
|
for instance in context.instances:
|
|
objects = instance.objsfiles()
|
|
writer.comment(f"Component: {instance.manifest.id}")
|
|
writer.comment(f"Resolved: {', '.join(instance.resolved)}")
|
|
|
|
for obj in objects:
|
|
r = rules.byFileIn(obj[0])
|
|
if r is None:
|
|
raise Exception(f"Unknown rule for file {obj[0]}")
|
|
t = target.tools[r.id]
|
|
writer.build(obj[1], r.id, obj[0], order_only=t.files)
|
|
|
|
writer.newline()
|
|
|
|
if instance.isLib():
|
|
writer.build(instance.libfile(), "ar",
|
|
list(map(lambda o: o[1], objects)))
|
|
else:
|
|
libraries: list[str] = []
|
|
|
|
for req in instance.resolved:
|
|
reqInstance = context.componentByName(req)
|
|
|
|
if reqInstance is None:
|
|
raise Exception(f"Component {req} not found")
|
|
|
|
if not reqInstance.isLib():
|
|
raise Exception(f"Component {req} is not a library")
|
|
|
|
libraries.append(reqInstance.outfile())
|
|
|
|
writer.build(instance.binfile(), "ld",
|
|
list(map(lambda o: o[1], objects)) + libraries)
|
|
|
|
writer.newline()
|
|
|
|
|
|
def build(componentSpec: str, targetSpec: str, props: Props = {}) -> str:
|
|
context = contextFor(targetSpec, props)
|
|
target = context.target
|
|
|
|
shell.mkdir(target.builddir())
|
|
ninjaPath = f"{target.builddir()}/build.ninja"
|
|
|
|
with open(ninjaPath, "w") as f:
|
|
gen(f, context)
|
|
|
|
component = context.componentByName(componentSpec)
|
|
|
|
if component is None:
|
|
raise Exception(f"Component {componentSpec} not found")
|
|
|
|
shell.exec(f"ninja", "-v", "-f", ninjaPath, component.outfile())
|
|
|
|
return component.outfile()
|
|
|
|
|
|
def buildAll(targetSpec: str, props: Props = {}) -> str:
|
|
context = contextFor(targetSpec, props)
|
|
target = context.target
|
|
|
|
shell.mkdir(target.builddir())
|
|
ninjaPath = f"{target.builddir()}/build.ninja"
|
|
|
|
with open(ninjaPath, "w") as f:
|
|
gen(f, context)
|
|
|
|
shell.exec(f"ninja", "-v", "-f", ninjaPath)
|
|
|
|
return target.builddir()
|