Added support for cdefs.

This commit is contained in:
Sleepy Monax 2023-02-06 11:14:32 +01:00
parent 3706ecb183
commit 8ee6e9625d
7 changed files with 58 additions and 27 deletions

View file

@ -24,8 +24,8 @@ def mergeToolsArgs(tool, layers):
def genNinja(out: TextIO, manifests: dict, target: dict) -> None: def genNinja(out: TextIO, manifests: dict, target: dict) -> None:
target = copy.deepcopy(target) target = copy.deepcopy(target)
target = targets.patchToolArgs(target, "cc", [m.cincludes(manifests)]) target = targets.patchToolArgs(target, "cc", [m.cinc(manifests)])
target = targets.patchToolArgs(target, "cxx", [m.cincludes(manifests)]) target = targets.patchToolArgs(target, "cxx", [m.cinc(manifests)])
writer = ninja.Writer(out) writer = ninja.Writer(out)

View file

@ -159,7 +159,7 @@ def prepareInOut(manifests: dict, target: dict) -> dict:
return manifests return manifests
def cincludes(manifests: dict) -> str: def cinc(manifests: dict) -> str:
include_paths = [] include_paths = []
for key in manifests: for key in manifests:

View file

@ -20,7 +20,10 @@ def gen(out: TextIO, context: Context):
writer.separator("Tools") writer.separator("Tools")
writer.variable("cincs", " ".join( writer.variable("cincs", " ".join(
map(lambda i: f"-I{i}", context.cincludes()))) map(lambda i: f"-I{i}", context.cincls())))
writer.variable("cdefs", " ".join(context.cdefs()))
writer.newline() writer.newline()
for i in target.tools: for i in target.tools:

View file

@ -71,11 +71,14 @@ class Context:
return None return None
return result[0] return result[0]
def cincludes(self) -> list[str]: def cincls(self) -> list[str]:
includes = list( includes = list(
map(lambda x: x.cinclude(), self.instances)) map(lambda x: x.cinclude(), self.instances))
return utils.uniq(includes) return utils.uniq(includes)
def cdefs(self) -> list[str]:
return self.target.cdefs()
def loadAllTargets() -> list[TargetManifest]: def loadAllTargets() -> list[TargetManifest]:
files = shell.find(const.TARGETS_DIR, ["*.json"]) files = shell.find(const.TARGETS_DIR, ["*.json"])
@ -157,7 +160,7 @@ def resolveDeps(componentSpec: str, components: list[ComponentManifest], target:
def instanciate(componentSpec: str, components: list[ComponentManifest], target: TargetManifest) -> ComponentInstance | None: def instanciate(componentSpec: str, components: list[ComponentManifest], target: TargetManifest) -> ComponentInstance | None:
manifest = next(filter(lambda c: c.id == componentSpec, components)) manifest = next(filter(lambda c: c.id == componentSpec, components))
sources = shell.find( sources = shell.find(
manifest.dirname(), ["*.c", "*.cpp", "*.s", "*.asm"]) manifest.dirname(), ["*.c", "*.cpp", "*.s", "*.asm"], recusive=False)
enabled, resolved = resolveDeps(componentSpec, components, target) enabled, resolved = resolveDeps(componentSpec, components, target)
if not enabled: if not enabled:

View file

@ -3,9 +3,9 @@ from enum import Enum
from typing import Any from typing import Any
from json import JSONEncoder from json import JSONEncoder
from osdk.jexpr import Json, evalRead from osdk.jexpr import Json
from osdk.logger import Logger from osdk.logger import Logger
from osdk import shell, const, utils from osdk import const, utils
logger = Logger("model") logger = Logger("model")
@ -111,6 +111,21 @@ class TargetManifest(Manifest):
def patch(self, toolSpec: str, args: list[str]): def patch(self, toolSpec: str, args: list[str]):
self.tools[toolSpec].args += args self.tools[toolSpec].args += args
def cdefs(self) -> list[str]:
defines: list[str] = []
for key in self.props:
macroname = key.lower().replace("-", "_")
prop = self.props[key]
macrovalue = str(prop).lower().replace(" ", "_").replace("-", "_")
if isinstance(prop, bool):
if prop:
defines += [f"-D__osdk_{macroname}__"]
else:
defines += [f"-D__osdk_{macroname}_{macrovalue}__"]
return defines
class ComponentManifest(Manifest): class ComponentManifest(Manifest):
decription: str decription: str
@ -158,7 +173,7 @@ class ComponentManifest(Manifest):
class ModelEncoder(JSONEncoder): class ModelEncoder(JSONEncoder):
def default(self, o): def default(self, o: Any):
if isinstance(o, Manifest): if isinstance(o, Manifest):
return { return {
"id": o.id, "id": o.id,

View file

@ -16,11 +16,11 @@ class Rule:
rules: dict[str, Rule] = { rules: dict[str, Rule] = {
"cc": Rule("cc", ["c"], ["o"], "-c -o $out $in -MD -MF $out.d $flags $cincs", ["-std=gnu2x", "cc": Rule("cc", ["c"], ["o"], "-c -o $out $in -MD -MF $out.d $flags $cincs $cdefs", ["-std=gnu2x",
"-Wall", "-Wall",
"-Wextra", "-Wextra",
"-Werror"], "$out.d"), "-Werror"], "$out.d"),
"cxx": Rule("cxx", ["cpp", "cc", "cxx"], ["o"], "-c -o $out $in -MD -MF $out.d $flags $cincs", ["-std=gnu++2b", "cxx": Rule("cxx", ["cpp", "cc", "cxx"], ["o"], "-c -o $out $in -MD -MF $out.d $flags $cincs $cdefs", ["-std=gnu++2b",
"-Wall", "-Wall",
"-Wextra", "-Wextra",
"-Werror", "-Werror",
@ -28,7 +28,7 @@ rules: dict[str, Rule] = {
"-fno-rtti"], "$out.d"), "-fno-rtti"], "$out.d"),
"as": Rule("as", ["s", "asm", "S"], ["o"], "-o $out $in $flags"), "as": Rule("as", ["s", "asm", "S"], ["o"], "-o $out $in $flags"),
"ar": Rule("ar", ["o"], ["a"], "$flags $out $in"), "ar": Rule("ar", ["o"], ["a"], "$flags $out $in"),
"ld": Rule("ld", ["o", "a"], ["out"], "$flags $out $in"), "ld": Rule("ld", ["o", "a"], ["out"], "-o $out $in $flags"),
} }

View file

@ -35,7 +35,7 @@ def sha256sum(path: str) -> str:
return hashlib.sha256(f.read()).hexdigest() return hashlib.sha256(f.read()).hexdigest()
def find(path: str, wildcards: list[str] = []) -> list[str]: def find(path: str, wildcards: list[str] = [], recusive: bool = True) -> list[str]:
logger.log(f"Looking for files in {path} matching {wildcards}") logger.log(f"Looking for files in {path} matching {wildcards}")
if not os.path.isdir(path): if not os.path.isdir(path):
@ -43,6 +43,7 @@ def find(path: str, wildcards: list[str] = []) -> list[str]:
result: list[str] = [] result: list[str] = []
if recusive:
for root, _, files in os.walk(path): for root, _, files in os.walk(path):
for f in files: for f in files:
if len(wildcards) == 0: if len(wildcards) == 0:
@ -52,6 +53,15 @@ def find(path: str, wildcards: list[str] = []) -> list[str]:
if fnmatch.fnmatch(f, wildcard): if fnmatch.fnmatch(f, wildcard):
result.append(os.path.join(root, f)) result.append(os.path.join(root, f))
break break
else:
for f in os.listdir(path):
if len(wildcards) == 0:
result.append(os.path.join(path, f))
else:
for wildcard in wildcards:
if fnmatch.fnmatch(f, wildcard):
result.append(os.path.join(path, f))
break
return result return result