Added support for res directory.

This commit is contained in:
Sleepy Monax 2023-04-16 01:03:06 +02:00
parent 175f4e0c3f
commit e6f245d2ad
3 changed files with 61 additions and 38 deletions

View file

@ -4,7 +4,7 @@ from typing import TextIO
from osdk.model import Props from osdk.model import Props
from osdk.ninja import Writer from osdk.ninja import Writer
from osdk.logger import Logger from osdk.logger import Logger
from osdk.context import Context, contextFor from osdk.context import ComponentInstance, Context, contextFor
from osdk import shell, rules from osdk import shell, rules
logger = Logger("builder") logger = Logger("builder")
@ -28,6 +28,9 @@ def gen(out: TextIO, context: Context):
writer.newline() writer.newline()
writer.rule("cp", "cp $in $out")
writer.newline()
for i in target.tools: for i in target.tools:
tool = target.tools[i] tool = target.tools[i]
rule = rules.rules[i] rule = rules.rules[i]
@ -43,7 +46,8 @@ def gen(out: TextIO, context: Context):
all: list[str] = [] all: list[str] = []
for instance in context.enabledInstances(): for instance in context.enabledInstances():
objects = instance.objsfiles(context) objects = instance.objsfiles()
assets = instance.resfiles()
writer.comment(f"Component: {instance.manifest.id}") writer.comment(f"Component: {instance.manifest.id}")
writer.comment(f"Resolved: {', '.join(instance.resolved)}") writer.comment(f"Resolved: {', '.join(instance.resolved)}")
@ -54,11 +58,14 @@ def gen(out: TextIO, context: Context):
t = target.tools[r.id] t = target.tools[r.id]
writer.build(obj[1], r.id, obj[0], order_only=t.files) writer.build(obj[1], r.id, obj[0], order_only=t.files)
for asset in assets:
writer.build(asset[1], "cp", asset[0])
writer.newline() writer.newline()
if instance.isLib(): if instance.isLib():
writer.build(instance.libfile(context), "ar", writer.build(instance.outfile(), "ar",
list(map(lambda o: o[1], objects))) list(map(lambda o: o[1], objects)), implicit=list(map(lambda o: o[1], assets)))
else: else:
libraries: list[str] = [] libraries: list[str] = []
@ -71,12 +78,12 @@ def gen(out: TextIO, context: Context):
if not reqInstance.isLib(): if not reqInstance.isLib():
raise Exception(f"Component {req} is not a library") raise Exception(f"Component {req} is not a library")
libraries.append(reqInstance.outfile(context)) libraries.append(reqInstance.outfile())
writer.build(instance.binfile(context), "ld", writer.build(instance.outfile(), "ld", list(
list(map(lambda o: o[1], objects)) + libraries) map(lambda o: o[1], objects)) + libraries, implicit=list(map(lambda o: o[1], assets)))
all.append(instance.binfile(context)) all.append(instance.outfile())
writer.newline() writer.newline()
@ -86,7 +93,7 @@ def gen(out: TextIO, context: Context):
writer.default("all") writer.default("all")
def build(componentSpec: str, targetSpec: str, props: Props = {}) -> str: def build(componentSpec: str, targetSpec: str, props: Props = {}) -> ComponentInstance:
context = contextFor(targetSpec, props) context = contextFor(targetSpec, props)
shell.mkdir(context.builddir()) shell.mkdir(context.builddir())
@ -104,9 +111,9 @@ def build(componentSpec: str, targetSpec: str, props: Props = {}) -> str:
raise Exception( raise Exception(
f"Component {componentSpec} is disabled: {instance.disableReason}") f"Component {componentSpec} is disabled: {instance.disableReason}")
shell.exec(f"ninja", "-v", "-f", ninjaPath, instance.outfile(context)) shell.exec(f"ninja", "-v", "-f", ninjaPath, instance.outfile())
return instance.outfile(context) return instance
class Paths: class Paths:
@ -120,7 +127,7 @@ class Paths:
self.obj = obj self.obj = obj
def buildAll(targetSpec: str) -> Paths: def buildAll(targetSpec: str) -> Context:
context = contextFor(targetSpec) context = contextFor(targetSpec)
shell.mkdir(context.builddir()) shell.mkdir(context.builddir())
@ -131,11 +138,7 @@ def buildAll(targetSpec: str) -> Paths:
shell.exec(f"ninja", "-v", "-f", ninjaPath) shell.exec(f"ninja", "-v", "-f", ninjaPath)
return Paths( return context
os.path.join(context.builddir(), "bin"),
os.path.join(context.builddir(), "lib"),
os.path.join(context.builddir(), "obj")
)
def testAll(targetSpec: str): def testAll(targetSpec: str):
@ -155,4 +158,4 @@ def testAll(targetSpec: str):
if instance.id().endswith("-tests"): if instance.id().endswith("-tests"):
print(f"Running {instance.id()}") print(f"Running {instance.id()}")
shell.exec(instance.outfile(context)) shell.exec(instance.outfile())

View file

@ -41,7 +41,7 @@ def runCmd(args: Args):
if componentSpec is None: if componentSpec is None:
raise Exception("Component not specified") raise Exception("Component not specified")
exe = builder.build(componentSpec, targetSpec) exe = builder.build(componentSpec, targetSpec).outfile()
shell.exec(exe, *args.args) shell.exec(exe, *args.args)
@ -67,7 +67,7 @@ def debugCmd(args: Args):
if componentSpec is None: if componentSpec is None:
raise Exception("Component not specified") raise Exception("Component not specified")
exe = builder.build(componentSpec, targetSpec) exe = builder.build(componentSpec, targetSpec).outfile()
shell.exec("lldb", "-o", "run", exe) shell.exec("lldb", "-o", "run", exe)
@ -270,7 +270,7 @@ def initCmd(args: Args):
f.write("from osdk.cmds import Cmd, append\n\n") f.write("from osdk.cmds import Cmd, append\n\n")
f.write("def runCmd(args: Args) -> None:\n") f.write("def runCmd(args: Args) -> None:\n")
f.write( f.write(
f" {project_name.lower()} = builder.build(\"{project_name.lower()}\", \"host-{shell.uname().machine}\")\n" f" {project_name.lower()} = builder.build(\"{project_name.lower()}\", \"host-{shell.uname().machine}\").outfile()\n"
) )
f.write(f" shell.exec(*[{project_name.lower()}])") f.write(f" shell.exec(*[{project_name.lower()}])")
f.write("\n\nappend(Cmd(\"s\", \"start\", \"Run the project\", runCmd))") f.write("\n\nappend(Cmd(\"s\", \"start\", \"Run the project\", runCmd))")

View file

@ -21,7 +21,9 @@ class ComponentInstance:
disableReason = "" disableReason = ""
manifest: ComponentManifest manifest: ComponentManifest
sources: list[str] = [] sources: list[str] = []
res: list[str] = []
resolved: list[str] = [] resolved: list[str] = []
context: IContext
def __init__( def __init__(
self, self,
@ -29,11 +31,13 @@ class ComponentInstance:
disableReason: str, disableReason: str,
manifest: ComponentManifest, manifest: ComponentManifest,
sources: list[str], sources: list[str],
res: list[str],
resolved: list[str]): resolved: list[str]):
self.enabled = enabled self.enabled = enabled
self.disableReason = disableReason self.disableReason = disableReason
self.manifest = manifest self.manifest = manifest
self.sources = sources self.sources = sources
self.res = res
self.resolved = resolved self.resolved = resolved
def id(self) -> str: def id(self) -> str:
@ -42,27 +46,31 @@ class ComponentInstance:
def isLib(self): def isLib(self):
return self.manifest.type == Type.LIB return self.manifest.type == Type.LIB
def binfile(self, context: IContext) -> str: def objdir(self) -> str:
return os.path.join(context.builddir(), "bin", f"{self.manifest.id}.out") return os.path.join(self.context.builddir(), f"{self.manifest.id}/obj")
def objdir(self, context: IContext) -> str: def resdir(self) -> str:
return os.path.join(context.builddir(), "obj", self.manifest.id) return os.path.join(self.context.builddir(), f"{self.manifest.id}/res")
def objsfiles(self, context: IContext) -> list[tuple[str, str]]: def objsfiles(self) -> list[tuple[str, str]]:
return list( def toOFile(s: str) -> str:
map( return os.path.join(self.objdir(), s.replace(os.path.join(self.manifest.dirname(), ''), '') + ".o")
lambda s: ( return list(map(lambda s: (s, toOFile(s)), self.sources))
s, os.path.join(self.objdir(context), s.replace(os.path.join(self.manifest.dirname(), ''), '') + ".o")),
self.sources))
def libfile(self, context: IContext) -> str: def resfiles(self) -> list[tuple[str, str, str]]:
return os.path.join(context.builddir(), "lib", f"{self.manifest.id}.a") def toAssetFile(s: str) -> str:
return os.path.join(self.resdir(), s.replace(os.path.join(self.manifest.dirname(), 'res/'), ''))
def outfile(self, context: IContext) -> str: def toAssetId(s: str) -> str:
return s.replace(os.path.join(self.manifest.dirname(), 'res/'), '')
return list(map(lambda s: (s, toAssetFile(s), toAssetId(s)), self.res))
def outfile(self) -> str:
if self.isLib(): if self.isLib():
return self.libfile(context) return os.path.join(self.context.builddir(), self.manifest.id, f"lib/{self.manifest.id}.a")
else: else:
return self.binfile(context) return os.path.join(self.context.builddir(), self.manifest.id, f"bin/{self.manifest.id}.out")
def cinclude(self) -> str: def cinclude(self) -> str:
if "cpp-root-include" in self.manifest.props: if "cpp-root-include" in self.manifest.props:
@ -202,14 +210,23 @@ def instanciate(componentSpec: str, components: list[ComponentManifest], target:
chain(*map(lambda rule: rule.fileIn, rules.rules.values()))) chain(*map(lambda rule: rule.fileIn, rules.rules.values())))
sources = shell.find( sources = shell.find(
manifest.subdirs, list(wildcards), recusive=False) manifest.subdirs, list(wildcards), recusive=False)
res = shell.find(os.path.join(manifest.dirname(), "res"))
enabled, unresolvedReason, resolved = resolveDeps( enabled, unresolvedReason, resolved = resolveDeps(
componentSpec, components, target) componentSpec, components, target)
return ComponentInstance(enabled, unresolvedReason, manifest, sources, resolved[1:]) return ComponentInstance(enabled, unresolvedReason, manifest, sources, res, resolved[1:])
def instanciateDisabled(component: ComponentManifest, target: TargetManifest) -> ComponentInstance: def instanciateDisabled(component: ComponentManifest, target: TargetManifest) -> ComponentInstance:
return ComponentInstance(False, component.isEnabled(target)[1], component, [], []) return ComponentInstance(
enabled=False,
disableReason=component.isEnabled(target)[1],
manifest=component,
sources=[],
res=[],
resolved=[])
context: dict[str, Context] = {} context: dict[str, Context] = {}
@ -266,4 +283,7 @@ def contextFor(targetSpec: str, props: Props = {}) -> Context:
tools, tools,
) )
for instance in instances:
instance.context = context[targetSpec]
return context[targetSpec] return context[targetSpec]