From e6f245d2ad7b7b143ffaea5ce8dfd4fd3a4f5fc7 Mon Sep 17 00:00:00 2001 From: VAN BOSSUYT Nicolas Date: Sun, 16 Apr 2023 01:03:06 +0200 Subject: [PATCH] Added support for res directory. --- osdk/builder.py | 39 ++++++++++++++++++----------------- osdk/cmds.py | 6 +++--- osdk/context.py | 54 +++++++++++++++++++++++++++++++++---------------- 3 files changed, 61 insertions(+), 38 deletions(-) diff --git a/osdk/builder.py b/osdk/builder.py index d88fff3..48c1256 100644 --- a/osdk/builder.py +++ b/osdk/builder.py @@ -4,7 +4,7 @@ from typing import TextIO from osdk.model import Props from osdk.ninja import Writer from osdk.logger import Logger -from osdk.context import Context, contextFor +from osdk.context import ComponentInstance, Context, contextFor from osdk import shell, rules logger = Logger("builder") @@ -28,6 +28,9 @@ def gen(out: TextIO, context: Context): writer.newline() + writer.rule("cp", "cp $in $out") + writer.newline() + for i in target.tools: tool = target.tools[i] rule = rules.rules[i] @@ -43,7 +46,8 @@ def gen(out: TextIO, context: Context): all: list[str] = [] 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"Resolved: {', '.join(instance.resolved)}") @@ -54,11 +58,14 @@ def gen(out: TextIO, context: Context): t = target.tools[r.id] 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() if instance.isLib(): - writer.build(instance.libfile(context), "ar", - list(map(lambda o: o[1], objects))) + writer.build(instance.outfile(), "ar", + list(map(lambda o: o[1], objects)), implicit=list(map(lambda o: o[1], assets))) else: libraries: list[str] = [] @@ -71,12 +78,12 @@ def gen(out: TextIO, context: Context): if not reqInstance.isLib(): raise Exception(f"Component {req} is not a library") - libraries.append(reqInstance.outfile(context)) + libraries.append(reqInstance.outfile()) - writer.build(instance.binfile(context), "ld", - list(map(lambda o: o[1], objects)) + libraries) + writer.build(instance.outfile(), "ld", list( + 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() @@ -86,7 +93,7 @@ def gen(out: TextIO, context: Context): 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) shell.mkdir(context.builddir()) @@ -104,9 +111,9 @@ def build(componentSpec: str, targetSpec: str, props: Props = {}) -> str: raise Exception( 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: @@ -120,7 +127,7 @@ class Paths: self.obj = obj -def buildAll(targetSpec: str) -> Paths: +def buildAll(targetSpec: str) -> Context: context = contextFor(targetSpec) shell.mkdir(context.builddir()) @@ -131,11 +138,7 @@ def buildAll(targetSpec: str) -> Paths: shell.exec(f"ninja", "-v", "-f", ninjaPath) - return Paths( - os.path.join(context.builddir(), "bin"), - os.path.join(context.builddir(), "lib"), - os.path.join(context.builddir(), "obj") - ) + return context def testAll(targetSpec: str): @@ -155,4 +158,4 @@ def testAll(targetSpec: str): if instance.id().endswith("-tests"): print(f"Running {instance.id()}") - shell.exec(instance.outfile(context)) + shell.exec(instance.outfile()) diff --git a/osdk/cmds.py b/osdk/cmds.py index bd4a2be..d7cadda 100644 --- a/osdk/cmds.py +++ b/osdk/cmds.py @@ -41,7 +41,7 @@ def runCmd(args: Args): if componentSpec is None: raise Exception("Component not specified") - exe = builder.build(componentSpec, targetSpec) + exe = builder.build(componentSpec, targetSpec).outfile() shell.exec(exe, *args.args) @@ -67,7 +67,7 @@ def debugCmd(args: Args): if componentSpec is None: raise Exception("Component not specified") - exe = builder.build(componentSpec, targetSpec) + exe = builder.build(componentSpec, targetSpec).outfile() 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("def runCmd(args: Args) -> None:\n") 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("\n\nappend(Cmd(\"s\", \"start\", \"Run the project\", runCmd))") diff --git a/osdk/context.py b/osdk/context.py index bb03d4d..f6cc9d3 100644 --- a/osdk/context.py +++ b/osdk/context.py @@ -21,7 +21,9 @@ class ComponentInstance: disableReason = "" manifest: ComponentManifest sources: list[str] = [] + res: list[str] = [] resolved: list[str] = [] + context: IContext def __init__( self, @@ -29,11 +31,13 @@ class ComponentInstance: disableReason: str, manifest: ComponentManifest, sources: list[str], + res: list[str], resolved: list[str]): self.enabled = enabled self.disableReason = disableReason self.manifest = manifest self.sources = sources + self.res = res self.resolved = resolved def id(self) -> str: @@ -42,27 +46,31 @@ class ComponentInstance: def isLib(self): return self.manifest.type == Type.LIB - def binfile(self, context: IContext) -> str: - return os.path.join(context.builddir(), "bin", f"{self.manifest.id}.out") + def objdir(self) -> str: + return os.path.join(self.context.builddir(), f"{self.manifest.id}/obj") - def objdir(self, context: IContext) -> str: - return os.path.join(context.builddir(), "obj", self.manifest.id) + def resdir(self) -> str: + return os.path.join(self.context.builddir(), f"{self.manifest.id}/res") - def objsfiles(self, context: IContext) -> list[tuple[str, str]]: - return list( - map( - lambda s: ( - s, os.path.join(self.objdir(context), s.replace(os.path.join(self.manifest.dirname(), ''), '') + ".o")), - self.sources)) + def objsfiles(self) -> list[tuple[str, str]]: + def toOFile(s: str) -> str: + return os.path.join(self.objdir(), s.replace(os.path.join(self.manifest.dirname(), ''), '') + ".o") + return list(map(lambda s: (s, toOFile(s)), self.sources)) - def libfile(self, context: IContext) -> str: - return os.path.join(context.builddir(), "lib", f"{self.manifest.id}.a") + def resfiles(self) -> list[tuple[str, str, str]]: + 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(): - return self.libfile(context) + return os.path.join(self.context.builddir(), self.manifest.id, f"lib/{self.manifest.id}.a") 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: 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()))) sources = shell.find( manifest.subdirs, list(wildcards), recusive=False) + + res = shell.find(os.path.join(manifest.dirname(), "res")) + enabled, unresolvedReason, resolved = resolveDeps( 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: - 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] = {} @@ -266,4 +283,7 @@ def contextFor(targetSpec: str, props: Props = {}) -> Context: tools, ) + for instance in instances: + instance.context = context[targetSpec] + return context[targetSpec]