Compare commits
33 commits
rfc_pathli
...
stable
Author | SHA1 | Date | |
---|---|---|---|
Sleepy Monax | 433111b555 | ||
Sleepy Monax | a4c5e2376b | ||
Sleepy Monax | 11d0667b83 | ||
Sleepy Monax | 8838340e7d | ||
Sleepy Monax | 69aa190d9e | ||
Sleepy Monax | 27fcacbdd3 | ||
Sleepy Monax | 5b703d779a | ||
Sleepy Monax | 83f56dbf8d | ||
Sleepy Monax | fc6c503a47 | ||
Sleepy Monax | 7ccd17d64b | ||
Sleepy Monax | 6cf0502beb | ||
Sleepy Monax | bbe2d1d387 | ||
Sleepy Monax | 16e321d82f | ||
Sleepy Monax | b3cc65ac36 | ||
Jordan ⌨️ | c2510da0ca | ||
Sleepy Monax | df7bfb7c3f | ||
Sleepy Monax | e1ff2307f5 | ||
Sleepy Monax | 244e8664e8 | ||
Sleepy Monax | d0564a968f | ||
Sleepy Monax | 8f4d19c98e | ||
Sleepy Monax | b1415cce16 | ||
Sleepy Monax | 367df1034d | ||
Sleepy Monax | c637675a6f | ||
Sleepy Monax | 5f7a12e2e2 | ||
Sleepy Monax | 8d1ca3095a | ||
Sleepy Monax | 3a78537dff | ||
Sleepy Monax | 9a962e825e | ||
Jordan ⌨️ | 2c9a7c5fc6 | ||
Jordan ⌨️ | b9fa1422a4 | ||
Sleepy Monax | d912e7d4ce | ||
Sleepy Monax | 6822bb2352 | ||
Sleepy Monax | 548a35ffa1 | ||
Sleepy Monax | 8a2024bc1c |
40
.github/workflows/checks.yml
vendored
Normal file
40
.github/workflows/checks.yml
vendored
Normal file
|
@ -0,0 +1,40 @@
|
|||
name: Checks
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
branches:
|
||||
- stable
|
||||
- dev
|
||||
|
||||
jobs:
|
||||
checks:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v3
|
||||
with:
|
||||
python-version: '3.11'
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install -r requirements.txt
|
||||
python -m pip install mypy pytest ruff
|
||||
|
||||
- name: Type Checking
|
||||
run: |
|
||||
python -m mypy --install-types --non-interactive .
|
||||
|
||||
- name: Linting
|
||||
run: |
|
||||
ruff check cutekit
|
||||
|
||||
- name: Unit Testing
|
||||
run: |
|
||||
python -m pytest
|
||||
|
7
.github/workflows/publish.yml
vendored
7
.github/workflows/publish.yml
vendored
|
@ -20,11 +20,14 @@ jobs:
|
|||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v3
|
||||
with:
|
||||
python-version: '3.x'
|
||||
python-version: '3.11'
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
|
|
16
README.md
16
README.md
|
@ -6,7 +6,7 @@
|
|||
</p>
|
||||
<h1 align="center">CuteKit</h1>
|
||||
<p align="center">
|
||||
The Cute build system and package manager
|
||||
The *magical* build system and package manager
|
||||
</p>
|
||||
<br/>
|
||||
<br/>
|
||||
|
@ -20,17 +20,17 @@
|
|||
- [Example](#example)
|
||||
|
||||
|
||||
## Introduction
|
||||
## Introduction
|
||||
|
||||
**CuteKit** is a simple - yet - powerful build system and package manager for C and C++. It:
|
||||
**CuteKit** is a simple - yet - powerful build system and package manager for C and C++. It:
|
||||
|
||||
- ✨ It uses **JSON**: Cutekit uses JSON instead of introducing a whole new programming language for describing the project. And also has macros to help the user experience (see [Jexpr](doc/spec/jexpr.md)).
|
||||
- ✨ It's a **package manager**: Cutekit package manager is based on **Git**, nothing is centralized.
|
||||
- ✨ It's **extendible**: Cutekit can be [extended](./doc/extends.md) by writing custom Python plugins.
|
||||
- ✨ It's **extendible**: Cutekit can be [extended](./doc/extends.md) by writing custom Python plugins.
|
||||
- ✨ It's **easy**: the [**templates**](./doc/templates.md) help the user quick-start a project.
|
||||
- ✨ It's **portable**: Cutekit can run on MacOS Gnu/Linux and Windows.
|
||||
|
||||
## Installation
|
||||
## Installation
|
||||
|
||||
To install Cutekit, you may use your favourite package manager if it is available. Or you can install it manually by following the instructions below.
|
||||
|
||||
|
@ -45,13 +45,13 @@ $ cd cutekit
|
|||
$ pip install --user -e .
|
||||
```
|
||||
|
||||
## Quick-start
|
||||
## Quick-start
|
||||
|
||||
-> If you directly want to start using Cutekit for a new project, you can just run `$ ck I host` and it will create a new project in the host directory (you can rename it later).
|
||||
-> If you directly want to start using Cutekit for a new project, you can just run `$ ck I host` and it will create a new project in the host directory (you can rename it later).
|
||||
|
||||
-> If you want to use Cutekit for writing operating systems, you can create a new [limine](https://github.com/limine-bootloader/limine/)-based project by running `$ ck I limine-barebone`.
|
||||
|
||||
## Example
|
||||
## Example
|
||||
|
||||
If you want to see how it works you can read the [doc/cutekit.md](doc/cutekit.md) file.
|
||||
|
||||
|
|
|
@ -2,21 +2,42 @@ import sys
|
|||
import os
|
||||
import logging
|
||||
|
||||
from cutekit import const, project, vt100, plugins, cmds
|
||||
from cutekit.args import parse
|
||||
from . import (
|
||||
builder, # noqa: F401 this is imported for side effects
|
||||
cli,
|
||||
const,
|
||||
graph, # noqa: F401 this is imported for side effects
|
||||
model,
|
||||
plugins,
|
||||
vt100,
|
||||
)
|
||||
|
||||
|
||||
def ensure(version: tuple[int, int, int]):
|
||||
if (
|
||||
const.VERSION[0] == version[0]
|
||||
and const.VERSION[1] == version[1]
|
||||
and const.VERSION[2] >= version[2]
|
||||
):
|
||||
return
|
||||
|
||||
raise RuntimeError(
|
||||
f"Expected cutekit version {version[0]}.{version[1]}.{version[2]} but found {const.VERSION_STR}"
|
||||
)
|
||||
|
||||
|
||||
def setupLogger(verbose: bool):
|
||||
if verbose:
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
level=logging.DEBUG,
|
||||
format=f"{vt100.CYAN}%(asctime)s{vt100.RESET} {vt100.YELLOW}%(levelname)s{vt100.RESET} %(name)s: %(message)s",
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
else:
|
||||
projectRoot = project.root()
|
||||
projectRoot = model.Project.topmost()
|
||||
logFile = const.GLOBAL_LOG_FILE
|
||||
if projectRoot is not None:
|
||||
logFile = os.path.join(projectRoot, const.PROJECT_LOG_FILE)
|
||||
logFile = os.path.join(projectRoot.dirname(), const.PROJECT_LOG_FILE)
|
||||
|
||||
# create the directory if it doesn't exist
|
||||
logDir = os.path.dirname(logFile)
|
||||
|
@ -31,18 +52,19 @@ def setupLogger(verbose: bool):
|
|||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
try:
|
||||
a = parse(sys.argv[1:])
|
||||
a = cli.parse(sys.argv[1:])
|
||||
setupLogger(a.consumeOpt("verbose", False) is True)
|
||||
plugins.loadAll()
|
||||
cmds.exec(a)
|
||||
cli.exec(a)
|
||||
print()
|
||||
return 0
|
||||
except RuntimeError as e:
|
||||
logging.exception(e)
|
||||
cmds.error(str(e))
|
||||
cmds.usage()
|
||||
cli.error(str(e))
|
||||
cli.usage()
|
||||
print()
|
||||
return 1
|
||||
except KeyboardInterrupt:
|
||||
|
|
|
@ -1,60 +0,0 @@
|
|||
from typing import Optional, Union
|
||||
|
||||
|
||||
Value = Union[str, bool, int]
|
||||
|
||||
|
||||
class Args:
|
||||
opts: dict[str, Value]
|
||||
args: list[str]
|
||||
|
||||
def __init__(self):
|
||||
self.opts = {}
|
||||
self.args = []
|
||||
|
||||
def consumePrefix(self, prefix: str) -> dict[str, Value]:
|
||||
result: dict[str, Value] = {}
|
||||
copy = self.opts.copy()
|
||||
for key, value in copy.items():
|
||||
if key.startswith(prefix):
|
||||
result[key[len(prefix) :]] = value
|
||||
del self.opts[key]
|
||||
return result
|
||||
|
||||
def consumeOpt(self, key: str, default: Value = False) -> Value:
|
||||
if key in self.opts:
|
||||
result = self.opts[key]
|
||||
del self.opts[key]
|
||||
return result
|
||||
return default
|
||||
|
||||
def tryConsumeOpt(self, key: str) -> Optional[Value]:
|
||||
if key in self.opts:
|
||||
result = self.opts[key]
|
||||
del self.opts[key]
|
||||
return result
|
||||
return None
|
||||
|
||||
def consumeArg(self, default: Optional[str] = None) -> Optional[str]:
|
||||
if len(self.args) == 0:
|
||||
return default
|
||||
|
||||
first = self.args[0]
|
||||
del self.args[0]
|
||||
return first
|
||||
|
||||
|
||||
def parse(args: list[str]) -> Args:
|
||||
result = Args()
|
||||
|
||||
for arg in args:
|
||||
if arg.startswith("--"):
|
||||
if "=" in arg:
|
||||
key, value = arg[2:].split("=", 1)
|
||||
result.opts[key] = value
|
||||
else:
|
||||
result.opts[arg[2:]] = True
|
||||
else:
|
||||
result.args.append(arg)
|
||||
|
||||
return result
|
|
@ -1,171 +1,298 @@
|
|||
import os
|
||||
import logging
|
||||
from typing import TextIO
|
||||
import dataclasses as dt
|
||||
|
||||
from cutekit.model import Props
|
||||
from cutekit.ninja import Writer
|
||||
from cutekit.context import ComponentInstance, Context, contextFor
|
||||
from cutekit import shell, rules
|
||||
from pathlib import Path
|
||||
from typing import TextIO, Union
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
from . import shell, rules, model, ninja, const, cli
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def gen(out: TextIO, context: Context):
|
||||
writer = Writer(out)
|
||||
def aggregateCincs(target: model.Target, registry: model.Registry) -> set[str]:
|
||||
res = set()
|
||||
|
||||
target = context.target
|
||||
for c in registry.iterEnabled(target):
|
||||
if "cpp-root-include" in c.props:
|
||||
res.add(c.dirname())
|
||||
elif c.type == model.Kind.LIB:
|
||||
res.add(str(Path(c.dirname()).parent))
|
||||
|
||||
writer.comment("File generated by the build system, do not edit")
|
||||
writer.newline()
|
||||
writer.variable("builddir", context.builddir())
|
||||
return set(map(lambda i: f"-I{i}", res))
|
||||
|
||||
writer.separator("Tools")
|
||||
|
||||
writer.variable("cincs", " ".join(map(lambda i: f"-I{i}", context.cincls())))
|
||||
def aggregateCdefs(target: model.Target) -> set[str]:
|
||||
res = set()
|
||||
|
||||
writer.variable("cdefs", " ".join(context.cdefs()))
|
||||
def sanatize(s: str) -> str:
|
||||
return s.lower().replace(" ", "_").replace("-", "_").replace(".", "_")
|
||||
|
||||
writer.newline()
|
||||
for k, v in target.props.items():
|
||||
if isinstance(v, bool):
|
||||
if v:
|
||||
res.add(f"-D__ck_{sanatize(k)}__")
|
||||
else:
|
||||
res.add(f"-D__ck_{sanatize(k)}_{sanatize(str(v))}__")
|
||||
res.add(f"-D__ck_{sanatize(k)}_value={str(v)}")
|
||||
|
||||
writer.rule("cp", "cp $in $out")
|
||||
writer.newline()
|
||||
return res
|
||||
|
||||
|
||||
def buildpath(target: model.Target, component: model.Component, path) -> Path:
|
||||
return Path(target.builddir) / component.id / path
|
||||
|
||||
|
||||
# --- Compilation ------------------------------------------------------------ #
|
||||
|
||||
|
||||
def wilcard(component: model.Component, wildcards: list[str]) -> list[str]:
|
||||
dirs = [component.dirname()] + list(
|
||||
map(lambda d: os.path.join(component.dirname(), d), component.subdirs)
|
||||
)
|
||||
return shell.find(dirs, list(wildcards), recusive=False)
|
||||
|
||||
|
||||
def compile(
|
||||
w: ninja.Writer,
|
||||
target: model.Target,
|
||||
component: model.Component,
|
||||
rule: str,
|
||||
srcs: list[str],
|
||||
) -> list[str]:
|
||||
res: list[str] = []
|
||||
for src in srcs:
|
||||
rel = Path(src).relative_to(component.dirname())
|
||||
dest = buildpath(target, component, "obj") / rel.with_suffix(".o")
|
||||
t = target.tools[rule]
|
||||
w.build(str(dest), rule, inputs=src, order_only=t.files)
|
||||
res.append(str(dest))
|
||||
return res
|
||||
|
||||
|
||||
# --- Ressources ------------------------------------------------------------- #
|
||||
|
||||
|
||||
def listRes(component: model.Component) -> list[str]:
|
||||
return shell.find(str(component.subpath("res")))
|
||||
|
||||
|
||||
def compileRes(
|
||||
w: ninja.Writer,
|
||||
target: model.Target,
|
||||
component: model.Component,
|
||||
) -> list[str]:
|
||||
res: list[str] = []
|
||||
for r in listRes(component):
|
||||
rel = Path(r).relative_to(component.subpath("res"))
|
||||
dest = buildpath(target, component, "res") / rel
|
||||
w.build(str(dest), "cp", r)
|
||||
res.append(str(dest))
|
||||
return res
|
||||
|
||||
|
||||
# --- Linking ---------------------------------------------------------------- #
|
||||
|
||||
|
||||
def outfile(target: model.Target, component: model.Component) -> str:
|
||||
if component.type == model.Kind.LIB:
|
||||
return str(buildpath(target, component, f"lib/{component.id}.a"))
|
||||
else:
|
||||
return str(buildpath(target, component, f"bin/{component.id}.out"))
|
||||
|
||||
|
||||
def collectLibs(
|
||||
registry: model.Registry, target: model.Target, component: model.Component
|
||||
) -> list[str]:
|
||||
res: list[str] = []
|
||||
for r in component.resolved[target.id].resolved:
|
||||
req = registry.lookup(r, model.Component)
|
||||
assert req is not None # model.Resolver has already checked this
|
||||
|
||||
if r == component.id:
|
||||
continue
|
||||
if not req.type == model.Kind.LIB:
|
||||
raise RuntimeError(f"Component {r} is not a library")
|
||||
res.append(outfile(target, req))
|
||||
return res
|
||||
|
||||
|
||||
def link(
|
||||
w: ninja.Writer,
|
||||
registry: model.Registry,
|
||||
target: model.Target,
|
||||
component: model.Component,
|
||||
) -> str:
|
||||
w.newline()
|
||||
out = outfile(target, component)
|
||||
|
||||
objs = []
|
||||
objs += compile(w, target, component, "cc", wilcard(component, ["*.c"]))
|
||||
objs += compile(
|
||||
w, target, component, "cxx", wilcard(component, ["*.cpp", "*.cc", "*.cxx"])
|
||||
)
|
||||
objs += compile(
|
||||
w, target, component, "as", wilcard(component, ["*.s", "*.asm", "*.S"])
|
||||
)
|
||||
|
||||
res = compileRes(w, target, component)
|
||||
libs = collectLibs(registry, target, component)
|
||||
if component.type == model.Kind.LIB:
|
||||
w.build(out, "ar", objs, implicit=res)
|
||||
else:
|
||||
w.build(out, "ld", objs + libs, implicit=res)
|
||||
return out
|
||||
|
||||
|
||||
# --- Phony ------------------------------------------------------------------ #
|
||||
|
||||
|
||||
def all(w: ninja.Writer, registry: model.Registry, target: model.Target) -> list[str]:
|
||||
all: list[str] = []
|
||||
for c in registry.iterEnabled(target):
|
||||
all.append(link(w, registry, target, c))
|
||||
w.build("all", "phony", all)
|
||||
w.default("all")
|
||||
return all
|
||||
|
||||
|
||||
def gen(out: TextIO, target: model.Target, registry: model.Registry):
|
||||
w = ninja.Writer(out)
|
||||
|
||||
w.comment("File generated by the build system, do not edit")
|
||||
w.newline()
|
||||
|
||||
w.variable("builddir", target.builddir)
|
||||
w.variable("hashid", target.hashid)
|
||||
|
||||
w.separator("Tools")
|
||||
|
||||
w.variable("cincs", " ".join(aggregateCincs(target, registry)))
|
||||
w.variable("cdefs", " ".join(aggregateCdefs(target)))
|
||||
w.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(
|
||||
w.variable(i, tool.cmd)
|
||||
w.variable(i + "flags", " ".join(rule.args + tool.args))
|
||||
w.rule(
|
||||
i,
|
||||
f"{tool.cmd} {rule.rule.replace('$flags',f'${i}flags')}",
|
||||
depfile=rule.deps,
|
||||
)
|
||||
writer.newline()
|
||||
w.newline()
|
||||
|
||||
writer.separator("Components")
|
||||
w.separator("Build")
|
||||
|
||||
all: list[str] = []
|
||||
all(w, registry, target)
|
||||
|
||||
for instance in context.enabledInstances():
|
||||
objects = instance.objsfiles()
|
||||
assets = instance.resfiles()
|
||||
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 RuntimeError(f"Unknown rule for file {obj[0]}")
|
||||
t = target.tools[r.id]
|
||||
writer.build(obj[1], r.id, obj[0], order_only=t.files)
|
||||
@dt.dataclass
|
||||
class Product:
|
||||
path: Path
|
||||
target: model.Target
|
||||
component: model.Component
|
||||
|
||||
for asset in assets:
|
||||
writer.build(asset[1], "cp", asset[0])
|
||||
|
||||
writer.newline()
|
||||
def build(
|
||||
target: model.Target,
|
||||
registry: model.Registry,
|
||||
components: Union[list[model.Component], model.Component, None] = None,
|
||||
) -> list[Product]:
|
||||
all = False
|
||||
shell.mkdir(target.builddir)
|
||||
ninjaPath = os.path.join(target.builddir, "build.ninja")
|
||||
|
||||
if instance.isLib():
|
||||
writer.build(
|
||||
instance.outfile(),
|
||||
"ar",
|
||||
list(map(lambda o: o[1], objects)),
|
||||
implicit=list(map(lambda o: o[1], assets)),
|
||||
if not os.path.exists(ninjaPath):
|
||||
with open(ninjaPath, "w") as f:
|
||||
gen(f, target, registry)
|
||||
|
||||
if components is None:
|
||||
all = True
|
||||
components = list(registry.iterEnabled(target))
|
||||
|
||||
if isinstance(components, model.Component):
|
||||
components = [components]
|
||||
|
||||
products: list[Product] = []
|
||||
for c in components:
|
||||
r = c.resolved[target.id]
|
||||
if not r.enabled:
|
||||
raise RuntimeError(f"Component {c.id} is disabled: {r.reason}")
|
||||
|
||||
products.append(
|
||||
Product(
|
||||
path=Path(outfile(target, c)),
|
||||
target=target,
|
||||
component=c,
|
||||
)
|
||||
else:
|
||||
libraries: list[str] = []
|
||||
|
||||
for req in instance.resolved:
|
||||
reqInstance = context.componentByName(req)
|
||||
|
||||
if reqInstance is None:
|
||||
raise RuntimeError(f"Component {req} not found")
|
||||
|
||||
if not reqInstance.isLib():
|
||||
raise RuntimeError(f"Component {req} is not a library")
|
||||
|
||||
libraries.append(reqInstance.outfile())
|
||||
|
||||
writer.build(
|
||||
instance.outfile(),
|
||||
"ld",
|
||||
list(map(lambda o: o[1], objects)) + libraries,
|
||||
implicit=list(map(lambda o: o[1], assets)),
|
||||
)
|
||||
|
||||
all.append(instance.outfile())
|
||||
|
||||
writer.newline()
|
||||
|
||||
writer.separator("Phony targets")
|
||||
|
||||
writer.build("all", "phony", all)
|
||||
writer.default("all")
|
||||
|
||||
|
||||
def build(componentSpec: str, targetSpec: str, props: Props = {}) -> ComponentInstance:
|
||||
context = contextFor(targetSpec, props)
|
||||
|
||||
shell.mkdir(context.builddir())
|
||||
ninjaPath = os.path.join(context.builddir(), "build.ninja")
|
||||
|
||||
with open(ninjaPath, "w") as f:
|
||||
gen(f, context)
|
||||
|
||||
instance = context.componentByName(componentSpec)
|
||||
|
||||
if instance is None:
|
||||
raise RuntimeError(f"Component {componentSpec} not found")
|
||||
|
||||
if not instance.enabled:
|
||||
raise RuntimeError(
|
||||
f"Component {componentSpec} is disabled: {instance.disableReason}"
|
||||
)
|
||||
|
||||
shell.exec("ninja", "-f", ninjaPath, instance.outfile())
|
||||
outs = list(map(lambda p: str(p.path), products))
|
||||
|
||||
return instance
|
||||
shell.exec("ninja", "-f", ninjaPath, *(outs if not all else []))
|
||||
|
||||
return products
|
||||
|
||||
|
||||
class Paths:
|
||||
bin: str
|
||||
lib: str
|
||||
obj: str
|
||||
|
||||
def __init__(self, bin: str, lib: str, obj: str):
|
||||
self.bin = bin
|
||||
self.lib = lib
|
||||
self.obj = obj
|
||||
# --- Commands --------------------------------------------------------------- #
|
||||
|
||||
|
||||
def buildAll(targetSpec: str, props: Props = {}) -> Context:
|
||||
context = contextFor(targetSpec, props)
|
||||
|
||||
shell.mkdir(context.builddir())
|
||||
ninjaPath = os.path.join(context.builddir(), "build.ninja")
|
||||
|
||||
with open(ninjaPath, "w") as f:
|
||||
gen(f, context)
|
||||
|
||||
shell.exec("ninja", "-v", "-f", ninjaPath)
|
||||
|
||||
return context
|
||||
@cli.command("b", "build", "Build a component or all components")
|
||||
def buildCmd(args: cli.Args):
|
||||
registry = model.Registry.use(args)
|
||||
target = model.Target.use(args)
|
||||
componentSpec = args.consumeArg()
|
||||
component = None
|
||||
if componentSpec is not None:
|
||||
component = registry.lookup(componentSpec, model.Component)
|
||||
build(target, registry, component)[0]
|
||||
|
||||
|
||||
def testAll(targetSpec: str):
|
||||
context = contextFor(targetSpec)
|
||||
@cli.command("r", "run", "Run a component")
|
||||
def runCmd(args: cli.Args):
|
||||
registry = model.Registry.use(args)
|
||||
target = model.Target.use(args)
|
||||
debug = args.consumeOpt("debug", False) is True
|
||||
|
||||
shell.mkdir(context.builddir())
|
||||
ninjaPath = os.path.join(context.builddir(), "build.ninja")
|
||||
componentSpec = args.consumeArg() or "__main__"
|
||||
component = registry.lookup(componentSpec, model.Component, includeProvides=True)
|
||||
if component is None:
|
||||
raise RuntimeError(f"Component {componentSpec} not found")
|
||||
|
||||
with open(ninjaPath, "w") as f:
|
||||
gen(f, context)
|
||||
product = build(target, registry, component)[0]
|
||||
|
||||
shell.exec("ninja", "-v", "-f", ninjaPath, "all")
|
||||
os.environ["CK_TARGET"] = target.id
|
||||
os.environ["CK_COMPONENT"] = product.component.id
|
||||
os.environ["CK_BUILDDIR"] = target.builddir
|
||||
|
||||
for instance in context.enabledInstances():
|
||||
if instance.isLib():
|
||||
continue
|
||||
shell.exec(*(["lldb", "-o", "run"] if debug else []), str(product.path), *args.args)
|
||||
|
||||
if instance.id().endswith("-tests"):
|
||||
print(f"Running {instance.id()}")
|
||||
shell.exec(instance.outfile())
|
||||
|
||||
@cli.command("t", "test", "Run all test targets")
|
||||
def testCmd(args: cli.Args):
|
||||
# This is just a wrapper around the `run` command that try
|
||||
# to run a special hook component named __tests__.
|
||||
args.args.insert(0, "__tests__")
|
||||
runCmd(args)
|
||||
|
||||
|
||||
@cli.command("d", "debug", "Debug a component")
|
||||
def debugCmd(args: cli.Args):
|
||||
# This is just a wrapper around the `run` command that
|
||||
# always enable debug mode.
|
||||
args.opts["debug"] = True
|
||||
runCmd(args)
|
||||
|
||||
|
||||
@cli.command("c", "clean", "Clean build files")
|
||||
def cleanCmd(args: cli.Args):
|
||||
model.Project.use(args)
|
||||
shell.rmrf(const.BUILD_DIR)
|
||||
|
||||
|
||||
@cli.command("n", "nuke", "Clean all build files and caches")
|
||||
def nukeCmd(args: cli.Args):
|
||||
model.Project.use(args)
|
||||
shell.rmrf(const.PROJECT_CK_DIR)
|
||||
|
|
165
cutekit/cli.py
Normal file
165
cutekit/cli.py
Normal file
|
@ -0,0 +1,165 @@
|
|||
import inspect
|
||||
import logging
|
||||
import sys
|
||||
import dataclasses as dt
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union, Callable
|
||||
|
||||
from . import const, vt100
|
||||
|
||||
Value = Union[str, bool, int]
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Args:
|
||||
opts: dict[str, Value]
|
||||
args: list[str]
|
||||
|
||||
def __init__(self):
|
||||
self.opts = {}
|
||||
self.args = []
|
||||
|
||||
def consumePrefix(self, prefix: str) -> dict[str, Value]:
|
||||
result: dict[str, Value] = {}
|
||||
copy = self.opts.copy()
|
||||
for key, value in copy.items():
|
||||
if key.startswith(prefix):
|
||||
result[key[len(prefix) :]] = value
|
||||
del self.opts[key]
|
||||
return result
|
||||
|
||||
def consumeOpt(self, key: str, default: Value = False) -> Value:
|
||||
if key in self.opts:
|
||||
result = self.opts[key]
|
||||
del self.opts[key]
|
||||
return result
|
||||
return default
|
||||
|
||||
def tryConsumeOpt(self, key: str) -> Optional[Value]:
|
||||
if key in self.opts:
|
||||
result = self.opts[key]
|
||||
del self.opts[key]
|
||||
return result
|
||||
return None
|
||||
|
||||
def consumeArg(self, default: Optional[str] = None) -> Optional[str]:
|
||||
if len(self.args) == 0:
|
||||
return default
|
||||
|
||||
first = self.args[0]
|
||||
del self.args[0]
|
||||
return first
|
||||
|
||||
|
||||
def parse(args: list[str]) -> Args:
|
||||
result = Args()
|
||||
|
||||
for arg in args:
|
||||
if arg.startswith("--"):
|
||||
if "=" in arg:
|
||||
key, value = arg[2:].split("=", 1)
|
||||
result.opts[key] = value
|
||||
else:
|
||||
result.opts[arg[2:]] = True
|
||||
else:
|
||||
result.args.append(arg)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
Callback = Callable[[Args], None]
|
||||
|
||||
|
||||
@dt.dataclass
|
||||
class Command:
|
||||
shortName: Optional[str]
|
||||
longName: str
|
||||
helpText: str
|
||||
isPlugin: bool
|
||||
callback: Callback
|
||||
|
||||
|
||||
commands: list[Command] = []
|
||||
|
||||
|
||||
def command(shortName: Optional[str], longName: str, helpText: str):
|
||||
curframe = inspect.currentframe()
|
||||
calframe = inspect.getouterframes(curframe, 2)
|
||||
|
||||
def wrap(fn: Callable[[Args], None]):
|
||||
_logger.debug(f"Registering command {longName}")
|
||||
commands.append(
|
||||
Command(
|
||||
shortName,
|
||||
longName,
|
||||
helpText,
|
||||
Path(calframe[1].filename).parent != Path(__file__).parent,
|
||||
fn,
|
||||
)
|
||||
)
|
||||
return fn
|
||||
|
||||
return wrap
|
||||
|
||||
|
||||
# --- Builtins Commands ------------------------------------------------------ #
|
||||
|
||||
|
||||
@command("u", "usage", "Show usage information")
|
||||
def usage(args: Optional[Args] = None):
|
||||
print(f"Usage: {const.ARGV0} <command> [args...]")
|
||||
|
||||
|
||||
def error(msg: str) -> None:
|
||||
print(f"{vt100.RED}Error:{vt100.RESET} {msg}\n", file=sys.stderr)
|
||||
|
||||
|
||||
@command("h", "help", "Show this help message")
|
||||
def helpCmd(args: Args):
|
||||
usage()
|
||||
|
||||
print()
|
||||
|
||||
vt100.title("Description")
|
||||
print(f" {const.DESCRIPTION}")
|
||||
|
||||
print()
|
||||
vt100.title("Commands")
|
||||
for cmd in sorted(commands, key=lambda c: c.longName):
|
||||
if cmd.longName.startswith("_"):
|
||||
continue
|
||||
|
||||
pluginText = ""
|
||||
if cmd.isPlugin:
|
||||
pluginText = f"{vt100.CYAN}(plugin){vt100.RESET}"
|
||||
|
||||
print(
|
||||
f" {vt100.GREEN}{cmd.shortName or ' '}{vt100.RESET} {cmd.longName} - {cmd.helpText} {pluginText}"
|
||||
)
|
||||
|
||||
print()
|
||||
vt100.title("Logging")
|
||||
print(" Logs are stored in:")
|
||||
print(f" - {const.PROJECT_LOG_FILE}")
|
||||
print(f" - {const.GLOBAL_LOG_FILE}")
|
||||
|
||||
|
||||
@command("v", "version", "Show current version")
|
||||
def versionCmd(args: Args):
|
||||
print(f"CuteKit v{const.VERSION_STR}")
|
||||
|
||||
|
||||
def exec(args: Args):
|
||||
cmd = args.consumeArg()
|
||||
|
||||
if cmd is None:
|
||||
raise RuntimeError("No command specified")
|
||||
|
||||
for c in commands:
|
||||
if c.shortName == cmd or c.longName == cmd:
|
||||
c.callback(args)
|
||||
return
|
||||
|
||||
raise RuntimeError(f"Unknown command {cmd}")
|
317
cutekit/cmds.py
317
cutekit/cmds.py
|
@ -1,317 +0,0 @@
|
|||
import os
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from typing import Callable, cast, Optional, NoReturn
|
||||
|
||||
from cutekit import context, shell, const, vt100, builder, graph, project
|
||||
from cutekit.args import Args
|
||||
from cutekit.jexpr import Json
|
||||
from cutekit.model import Extern
|
||||
from cutekit.context import contextFor
|
||||
|
||||
Callback = Callable[[Args], None]
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Cmd:
|
||||
shortName: Optional[str]
|
||||
longName: str
|
||||
helpText: str
|
||||
callback: Callable[[Args], NoReturn]
|
||||
isPlugin: bool = False
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
shortName: Optional[str],
|
||||
longName: str,
|
||||
helpText: str,
|
||||
callback: Callable[[Args], NoReturn],
|
||||
):
|
||||
self.shortName = shortName
|
||||
self.longName = longName
|
||||
self.helpText = helpText
|
||||
self.callback = callback
|
||||
|
||||
|
||||
cmds: list[Cmd] = []
|
||||
|
||||
|
||||
def append(cmd: Cmd):
|
||||
cmd.isPlugin = True
|
||||
cmds.append(cmd)
|
||||
cmds.sort(key=lambda c: c.shortName or c.longName)
|
||||
|
||||
|
||||
def runCmd(args: Args):
|
||||
project.chdir()
|
||||
|
||||
targetSpec = cast(str, args.consumeOpt("target", "host-" + shell.uname().machine))
|
||||
props = args.consumePrefix("prop:")
|
||||
|
||||
componentSpec = args.consumeArg()
|
||||
|
||||
if componentSpec is None:
|
||||
raise RuntimeError("Component not specified")
|
||||
|
||||
component = builder.build(componentSpec, targetSpec, props)
|
||||
|
||||
os.environ["CK_TARGET"] = component.context.target.id
|
||||
os.environ["CK_COMPONENT"] = component.id()
|
||||
os.environ["CK_BUILDDIR"] = component.context.builddir()
|
||||
|
||||
shell.exec(component.outfile(), *args.args)
|
||||
|
||||
|
||||
cmds += [Cmd("r", "run", "Run the target", runCmd)]
|
||||
|
||||
|
||||
def testCmd(args: Args):
|
||||
project.chdir()
|
||||
|
||||
targetSpec = cast(str, args.consumeOpt("target", "host-" + shell.uname().machine))
|
||||
builder.testAll(targetSpec)
|
||||
|
||||
|
||||
cmds += [Cmd("t", "test", "Run all test targets", testCmd)]
|
||||
|
||||
|
||||
def debugCmd(args: Args):
|
||||
project.chdir()
|
||||
|
||||
targetSpec = cast(str, args.consumeOpt("target", "host-" + shell.uname().machine))
|
||||
props = args.consumePrefix("prop:")
|
||||
|
||||
componentSpec = args.consumeArg()
|
||||
|
||||
if componentSpec is None:
|
||||
raise RuntimeError("Component not specified")
|
||||
|
||||
component = builder.build(componentSpec, targetSpec, props)
|
||||
|
||||
os.environ["CK_TARGET"] = component.context.target.id
|
||||
os.environ["CK_COMPONENT"] = component.id()
|
||||
os.environ["CK_BUILDDIR"] = component.context.builddir()
|
||||
|
||||
shell.exec("lldb", "-o", "run", component.outfile(), *args.args)
|
||||
|
||||
|
||||
cmds += [Cmd("d", "debug", "Debug the target", debugCmd)]
|
||||
|
||||
|
||||
def buildCmd(args: Args):
|
||||
project.chdir()
|
||||
|
||||
targetSpec = cast(str, args.consumeOpt("target", "host-" + shell.uname().machine))
|
||||
props = args.consumePrefix("prop:")
|
||||
componentSpec = args.consumeArg()
|
||||
|
||||
if componentSpec is None:
|
||||
builder.buildAll(targetSpec, props)
|
||||
else:
|
||||
builder.build(componentSpec, targetSpec, props)
|
||||
|
||||
|
||||
cmds += [Cmd("b", "build", "Build the target", buildCmd)]
|
||||
|
||||
|
||||
def listCmd(args: Args):
|
||||
project.chdir()
|
||||
|
||||
components = context.loadAllComponents()
|
||||
targets = context.loadAllTargets()
|
||||
|
||||
vt100.title("Components")
|
||||
if len(components) == 0:
|
||||
print(" (No components available)")
|
||||
else:
|
||||
print(vt100.indent(vt100.wordwrap(", ".join(map(lambda m: m.id, components)))))
|
||||
print()
|
||||
|
||||
vt100.title("Targets")
|
||||
|
||||
if len(targets) == 0:
|
||||
print(" (No targets available)")
|
||||
else:
|
||||
print(vt100.indent(vt100.wordwrap(", ".join(map(lambda m: m.id, targets)))))
|
||||
|
||||
print()
|
||||
|
||||
|
||||
cmds += [Cmd("l", "list", "List the targets", listCmd)]
|
||||
|
||||
|
||||
def cleanCmd(args: Args):
|
||||
project.chdir()
|
||||
shell.rmrf(const.BUILD_DIR)
|
||||
|
||||
|
||||
cmds += [Cmd("c", "clean", "Clean the build directory", cleanCmd)]
|
||||
|
||||
|
||||
def nukeCmd(args: Args):
|
||||
project.chdir()
|
||||
shell.rmrf(const.PROJECT_CK_DIR)
|
||||
|
||||
|
||||
cmds += [Cmd("n", "nuke", "Clean the build directory and cache", nukeCmd)]
|
||||
|
||||
|
||||
def helpCmd(args: Args):
|
||||
usage()
|
||||
|
||||
print()
|
||||
|
||||
vt100.title("Description")
|
||||
print(f" {const.DESCRIPTION}")
|
||||
|
||||
print()
|
||||
vt100.title("Commands")
|
||||
for cmd in cmds:
|
||||
pluginText = ""
|
||||
if cmd.isPlugin:
|
||||
pluginText = f"{vt100.CYAN}(plugin){vt100.RESET}"
|
||||
|
||||
print(
|
||||
f" {vt100.GREEN}{cmd.shortName or ' '}{vt100.RESET} {cmd.longName} - {cmd.helpText} {pluginText}"
|
||||
)
|
||||
|
||||
print()
|
||||
vt100.title("Logging")
|
||||
print(" Logs are stored in:")
|
||||
print(f" - {const.PROJECT_LOG_FILE}")
|
||||
print(f" - {const.GLOBAL_LOG_FILE}")
|
||||
|
||||
|
||||
cmds += [Cmd("h", "help", "Show this help message", helpCmd)]
|
||||
|
||||
|
||||
def versionCmd(args: Args):
|
||||
print(f"CuteKit v{const.VERSION_STR}\n")
|
||||
|
||||
|
||||
cmds += [Cmd("v", "version", "Show current version", versionCmd)]
|
||||
|
||||
|
||||
def graphCmd(args: Args):
|
||||
project.chdir()
|
||||
|
||||
targetSpec = cast(str, args.consumeOpt("target", "host-" + shell.uname().machine))
|
||||
|
||||
scope: Optional[str] = cast(Optional[str], args.tryConsumeOpt("scope"))
|
||||
onlyLibs: bool = args.consumeOpt("only-libs", False) is True
|
||||
showDisabled: bool = args.consumeOpt("show-disabled", False) is True
|
||||
|
||||
context = contextFor(targetSpec)
|
||||
|
||||
graph.view(context, scope=scope, showExe=not onlyLibs, showDisabled=showDisabled)
|
||||
|
||||
|
||||
cmds += [Cmd("g", "graph", "Show dependency graph", graphCmd)]
|
||||
|
||||
|
||||
def grabExtern(extern: dict[str, Extern]):
|
||||
for extSpec, ext in extern.items():
|
||||
extPath = os.path.join(const.EXTERN_DIR, extSpec)
|
||||
|
||||
if os.path.exists(extPath):
|
||||
print(f"Skipping {extSpec}, already installed")
|
||||
continue
|
||||
|
||||
print(f"Installing {extSpec}-{ext.tag} from {ext.git}...")
|
||||
shell.popen(
|
||||
"git", "clone", "--depth", "1", "--branch", ext.tag, ext.git, extPath
|
||||
)
|
||||
|
||||
if os.path.exists(os.path.join(extPath, "project.json")):
|
||||
grabExtern(context.loadProject(extPath).extern)
|
||||
|
||||
|
||||
def installCmd(args: Args):
|
||||
project.chdir()
|
||||
|
||||
pj = context.loadProject(".")
|
||||
grabExtern(pj.extern)
|
||||
|
||||
|
||||
cmds += [Cmd("i", "install", "Install all the external packages", installCmd)]
|
||||
|
||||
|
||||
def initCmd(args: Args):
|
||||
import requests
|
||||
|
||||
repo = args.consumeOpt("repo", const.DEFAULT_REPO_TEMPLATES)
|
||||
list = args.consumeOpt("list")
|
||||
|
||||
template = args.consumeArg()
|
||||
name = args.consumeArg()
|
||||
|
||||
logger.info("Fetching registry...")
|
||||
r = requests.get(f"https://raw.githubusercontent.com/{repo}/main/registry.json")
|
||||
|
||||
if r.status_code != 200:
|
||||
logger.error("Failed to fetch registry")
|
||||
exit(1)
|
||||
|
||||
registry = r.json()
|
||||
|
||||
if list:
|
||||
print(
|
||||
"\n".join(f"* {entry['id']} - {entry['description']}" for entry in registry)
|
||||
)
|
||||
return
|
||||
|
||||
if not template:
|
||||
raise RuntimeError("Template not specified")
|
||||
|
||||
def template_match(t: Json) -> str:
|
||||
return t["id"] == template
|
||||
|
||||
if not any(filter(template_match, registry)):
|
||||
raise LookupError(f"Couldn't find a template named {template}")
|
||||
|
||||
if not name:
|
||||
logger.info(f"No name was provided, defaulting to {template}")
|
||||
name = template
|
||||
|
||||
if os.path.exists(name):
|
||||
raise RuntimeError(f"Directory {name} already exists")
|
||||
|
||||
print(f"Creating project {name} from template {template}...")
|
||||
shell.cloneDir(f"https://github.com/{repo}", template, name)
|
||||
print(f"Project {name} created\n")
|
||||
|
||||
print("We suggest that you begin by typing:")
|
||||
print(f" {vt100.GREEN}cd {name}{vt100.RESET}")
|
||||
print(
|
||||
f" {vt100.GREEN}cutekit install{vt100.BRIGHT_BLACK} # Install external packages{vt100.RESET}"
|
||||
)
|
||||
print(
|
||||
f" {vt100.GREEN}cutekit build{vt100.BRIGHT_BLACK} # Build the project{vt100.RESET}"
|
||||
)
|
||||
|
||||
|
||||
cmds += [Cmd("I", "init", "Initialize a new project", initCmd)]
|
||||
|
||||
|
||||
def usage():
|
||||
print(f"Usage: {const.ARGV0} <command> [args...]")
|
||||
|
||||
|
||||
def error(msg: str) -> None:
|
||||
print(f"{vt100.RED}Error:{vt100.RESET} {msg}\n", file=sys.stderr)
|
||||
|
||||
|
||||
def exec(args: Args):
|
||||
cmd = args.consumeArg()
|
||||
|
||||
if cmd is None:
|
||||
raise RuntimeError("No command specified")
|
||||
|
||||
for c in cmds:
|
||||
if c.shortName == cmd or c.longName == cmd:
|
||||
c.callback(args)
|
||||
return
|
||||
|
||||
raise RuntimeError(f"Unknown command {cmd}")
|
|
@ -1,3 +1,4 @@
|
|||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
|
||||
|
@ -5,10 +6,11 @@ SUPPORTED_MANIFEST = [
|
|||
"https://schemas.cute.engineering/stable/cutekit.manifest.component.v1",
|
||||
"https://schemas.cute.engineering/stable/cutekit.manifest.project.v1",
|
||||
"https://schemas.cute.engineering/stable/cutekit.manifest.target.v1",
|
||||
|
||||
]
|
||||
|
||||
OSDK_MANIFEST_NOT_SUPPORTED = "OSDK manifests are not supported by CuteKit. Please use CuteKit manifest instead"
|
||||
OSDK_MANIFEST_NOT_SUPPORTED = (
|
||||
"OSDK manifests are not supported by CuteKit. Please use CuteKit manifest instead"
|
||||
)
|
||||
|
||||
UNSUPORTED_MANIFEST = {
|
||||
"https://schemas.cute.engineering/stable/osdk.manifest.component.v1": OSDK_MANIFEST_NOT_SUPPORTED,
|
||||
|
@ -20,14 +22,16 @@ UNSUPORTED_MANIFEST = {
|
|||
}
|
||||
|
||||
|
||||
def ensureSupportedManifest(manifest: Any, path: str):
|
||||
def ensureSupportedManifest(manifest: Any, path: Path):
|
||||
if "$schema" not in manifest:
|
||||
raise RuntimeError(f"Missing $schema in {path}")
|
||||
|
||||
if manifest["$schema"] in UNSUPORTED_MANIFEST:
|
||||
raise RuntimeError(
|
||||
f"Unsupported manifest schema {manifest['$schema']} in {path}: {UNSUPORTED_MANIFEST[manifest['$schema']]}")
|
||||
f"Unsupported manifest schema {manifest['$schema']} in {path}: {UNSUPORTED_MANIFEST[manifest['$schema']]}"
|
||||
)
|
||||
|
||||
if manifest["$schema"] not in SUPPORTED_MANIFEST:
|
||||
raise RuntimeError(
|
||||
f"Unsupported manifest schema {manifest['$schema']} in {path}")
|
||||
f"Unsupported manifest schema {manifest['$schema']} in {path}"
|
||||
)
|
||||
|
|
|
@ -2,7 +2,7 @@ import os
|
|||
import sys
|
||||
|
||||
VERSION = (0, 6, 0, "dev")
|
||||
VERSION_STR = f"{VERSION[0]}.{VERSION[1]}.{VERSION[2]}{'-' + VERSION[3] if VERSION[3] else ''}"
|
||||
VERSION_STR = f"{VERSION[0]}.{VERSION[1]}.{VERSION[2]}{'-' + VERSION[3] if len(VERSION) >= 4 else ''}"
|
||||
MODULE_DIR = os.path.dirname(os.path.realpath(__file__))
|
||||
ARGV0 = os.path.basename(sys.argv[0])
|
||||
PROJECT_CK_DIR = ".cutekit"
|
||||
|
@ -16,5 +16,4 @@ TARGETS_DIR = os.path.join(META_DIR, "targets")
|
|||
DEFAULT_REPO_TEMPLATES = "cute-engineering/cutekit-templates"
|
||||
DESCRIPTION = "A build system and package manager for low-level software development"
|
||||
PROJECT_LOG_FILE = os.path.join(PROJECT_CK_DIR, "cutekit.log")
|
||||
GLOBAL_LOG_FILE = os.path.join(
|
||||
os.path.expanduser("~"), ".cutekit", "cutekit.log")
|
||||
GLOBAL_LOG_FILE = os.path.join(os.path.expanduser("~"), ".cutekit", "cutekit.log")
|
||||
|
|
|
@ -1,303 +0,0 @@
|
|||
from typing import cast, Optional, Protocol, Iterable
|
||||
from itertools import chain
|
||||
from pathlib import Path
|
||||
import os
|
||||
import logging
|
||||
|
||||
from cutekit.model import ProjectManifest, TargetManifest, ComponentManifest, Props, Type, Tool, Tools
|
||||
from cutekit import const, shell, jexpr, utils, rules, mixins, project
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class IContext(Protocol):
|
||||
target: TargetManifest
|
||||
|
||||
def builddir(self) -> str:
|
||||
...
|
||||
|
||||
|
||||
class ComponentInstance:
|
||||
enabled: bool = True
|
||||
disableReason = ""
|
||||
manifest: ComponentManifest
|
||||
sources: list[str] = []
|
||||
res: list[str] = []
|
||||
resolved: list[str] = []
|
||||
context: IContext
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
enabled: bool,
|
||||
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:
|
||||
return self.manifest.id
|
||||
|
||||
def isLib(self):
|
||||
return self.manifest.type == Type.LIB
|
||||
|
||||
def objdir(self) -> str:
|
||||
return os.path.join(self.context.builddir(), f"{self.manifest.id}/obj")
|
||||
|
||||
def resdir(self) -> str:
|
||||
return os.path.join(self.context.builddir(), f"{self.manifest.id}/res")
|
||||
|
||||
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 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 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 os.path.join(self.context.builddir(), self.manifest.id, f"lib/{self.manifest.id}.a")
|
||||
else:
|
||||
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:
|
||||
return self.manifest.dirname()
|
||||
elif self.manifest.type == Type.LIB:
|
||||
return str(Path(self.manifest.dirname()).parent)
|
||||
else:
|
||||
return ""
|
||||
|
||||
|
||||
class Context(IContext):
|
||||
target: TargetManifest
|
||||
instances: list[ComponentInstance]
|
||||
tools: Tools
|
||||
|
||||
def enabledInstances(self) -> Iterable[ComponentInstance]:
|
||||
return filter(lambda x: x.enabled, self.instances)
|
||||
|
||||
def __init__(self, target: TargetManifest, instances: list[ComponentInstance], tools: Tools):
|
||||
self.target = target
|
||||
self.instances = instances
|
||||
self.tools = tools
|
||||
|
||||
def componentByName(self, name: str) -> Optional[ComponentInstance]:
|
||||
result = list(filter(lambda x: x.manifest.id == name, self.instances))
|
||||
if len(result) == 0:
|
||||
return None
|
||||
return result[0]
|
||||
|
||||
def cincls(self) -> list[str]:
|
||||
includes = list(filter(lambda x: x != "", map(
|
||||
lambda x: x.cinclude(), self.enabledInstances())))
|
||||
return utils.uniq(includes)
|
||||
|
||||
def cdefs(self) -> list[str]:
|
||||
return self.target.cdefs()
|
||||
|
||||
def hashid(self) -> str:
|
||||
return utils.hash((self.target.props, [self.tools[t].toJson() for t in self.tools]))[0:8]
|
||||
|
||||
def builddir(self) -> str:
|
||||
return os.path.join(const.BUILD_DIR, f"{self.target.id}-{self.hashid()[:8]}")
|
||||
|
||||
|
||||
def loadAllTargets() -> list[TargetManifest]:
|
||||
projectRoot = project.root()
|
||||
if projectRoot is None:
|
||||
return []
|
||||
|
||||
pj = loadProject(projectRoot)
|
||||
paths = list(
|
||||
map(lambda e: os.path.join(const.EXTERN_DIR,
|
||||
e, const.TARGETS_DIR), pj.extern.keys())
|
||||
) + [const.TARGETS_DIR]
|
||||
|
||||
ret = []
|
||||
for entry in paths:
|
||||
files = shell.find(entry, ["*.json"])
|
||||
ret += list(map(lambda path: TargetManifest(jexpr.evalRead(path), path), files))
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def loadProject(path: str) -> ProjectManifest:
|
||||
path = os.path.join(path, "project.json")
|
||||
return ProjectManifest(jexpr.evalRead(path), path)
|
||||
|
||||
|
||||
def loadTarget(id: str) -> TargetManifest:
|
||||
try:
|
||||
return next(filter(lambda t: t.id == id, loadAllTargets()))
|
||||
except StopIteration:
|
||||
raise RuntimeError(f"Target '{id}' not found")
|
||||
|
||||
|
||||
def loadAllComponents() -> list[ComponentManifest]:
|
||||
files = shell.find(const.SRC_DIR, ["manifest.json"])
|
||||
files += shell.find(const.EXTERN_DIR, ["manifest.json"])
|
||||
|
||||
return list(
|
||||
map(
|
||||
lambda path: ComponentManifest(jexpr.evalRead(path), path),
|
||||
files))
|
||||
|
||||
|
||||
def filterDisabled(components: list[ComponentManifest], target: TargetManifest) -> tuple[list[ComponentManifest], list[ComponentManifest]]:
|
||||
return list(filter(lambda c: c.isEnabled(target)[0], components)), \
|
||||
list(filter(lambda c: not c.isEnabled(target)[0], components))
|
||||
|
||||
|
||||
def providerFor(what: str, components: list[ComponentManifest]) -> tuple[Optional[str], str]:
|
||||
result: list[ComponentManifest] = list(
|
||||
filter(lambda c: c.id == what, components))
|
||||
|
||||
if len(result) == 0:
|
||||
# Try to find a provider
|
||||
result = list(filter(lambda x: (what in x.provides), components))
|
||||
|
||||
if len(result) == 0:
|
||||
logger.error(f"No provider for '{what}'")
|
||||
return (None, f"No provider for '{what}'")
|
||||
|
||||
if len(result) > 1:
|
||||
ids = list(map(lambda x: x.id, result))
|
||||
logger.error(f"Multiple providers for '{what}': {result}")
|
||||
return (None, f"Multiple providers for '{what}': {','.join(ids)}")
|
||||
|
||||
return (result[0].id, "")
|
||||
|
||||
|
||||
def resolveDeps(componentSpec: str, components: list[ComponentManifest], target: TargetManifest) -> tuple[bool, str, list[str]]:
|
||||
mapping = dict(map(lambda c: (c.id, c), components))
|
||||
|
||||
def resolveInner(what: str, stack: list[str] = []) -> tuple[bool, str, list[str]]:
|
||||
result: list[str] = []
|
||||
what = target.route(what)
|
||||
resolved, unresolvedReason = providerFor(what, components)
|
||||
|
||||
if resolved is None:
|
||||
return False, unresolvedReason, []
|
||||
|
||||
if resolved in stack:
|
||||
raise RuntimeError(f"Dependency loop: {stack} -> {resolved}")
|
||||
|
||||
stack.append(resolved)
|
||||
|
||||
for req in mapping[resolved].requires:
|
||||
keep, unresolvedReason, reqs = resolveInner(req, stack)
|
||||
|
||||
if not keep:
|
||||
stack.pop()
|
||||
logger.error(f"Dependency '{req}' not met for '{resolved}'")
|
||||
return False, unresolvedReason, []
|
||||
|
||||
result.extend(reqs)
|
||||
|
||||
stack.pop()
|
||||
result.insert(0, resolved)
|
||||
|
||||
return True, "", result
|
||||
|
||||
enabled, unresolvedReason, resolved = resolveInner(componentSpec)
|
||||
|
||||
return enabled, unresolvedReason, resolved
|
||||
|
||||
|
||||
def instanciate(componentSpec: str, components: list[ComponentManifest], target: TargetManifest) -> Optional[ComponentInstance]:
|
||||
manifest = next(filter(lambda c: c.id == componentSpec, components))
|
||||
wildcards = set(
|
||||
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, res, resolved[1:])
|
||||
|
||||
|
||||
def instanciateDisabled(component: ComponentManifest, target: TargetManifest) -> ComponentInstance:
|
||||
return ComponentInstance(
|
||||
enabled=False,
|
||||
disableReason=component.isEnabled(target)[1],
|
||||
manifest=component,
|
||||
sources=[],
|
||||
res=[],
|
||||
resolved=[])
|
||||
|
||||
|
||||
context: dict[str, Context] = {}
|
||||
|
||||
|
||||
def contextFor(targetSpec: str, props: Props = {}) -> Context:
|
||||
if targetSpec in context:
|
||||
return context[targetSpec]
|
||||
|
||||
logger.info(f"Loading context for '{targetSpec}'")
|
||||
|
||||
targetEls = targetSpec.split(":")
|
||||
|
||||
if targetEls[0] == "":
|
||||
targetEls[0] = "host-" + shell.uname().machine
|
||||
|
||||
target = loadTarget(targetEls[0])
|
||||
target.props |= props
|
||||
|
||||
components = loadAllComponents()
|
||||
components, disabled = filterDisabled(components, target)
|
||||
|
||||
tools: Tools = {}
|
||||
|
||||
for toolSpec in target.tools:
|
||||
tool = target.tools[toolSpec]
|
||||
|
||||
tools[toolSpec] = Tool(
|
||||
strict=False,
|
||||
cmd=tool.cmd,
|
||||
args=tool.args,
|
||||
files=tool.files)
|
||||
|
||||
tools[toolSpec].args += rules.rules[toolSpec].args
|
||||
|
||||
for m in targetEls[1:]:
|
||||
mixin = mixins.byId(m)
|
||||
tools = mixin(target, tools)
|
||||
|
||||
for component in components:
|
||||
for toolSpec in component.tools:
|
||||
tool = component.tools[toolSpec]
|
||||
tools[toolSpec].args += tool.args
|
||||
|
||||
instances: list[ComponentInstance] = list(
|
||||
map(lambda c: instanciateDisabled(c, target), disabled))
|
||||
|
||||
instances += cast(list[ComponentInstance], list(filter(lambda e: e is not None, map(lambda c: instanciate(
|
||||
c.id, components, target), components))))
|
||||
|
||||
context[targetSpec] = Context(
|
||||
target,
|
||||
instances,
|
||||
tools,
|
||||
)
|
||||
|
||||
for instance in instances:
|
||||
instance.context = context[targetSpec]
|
||||
|
||||
return context[targetSpec]
|
106
cutekit/graph.py
106
cutekit/graph.py
|
@ -1,59 +1,95 @@
|
|||
import os
|
||||
|
||||
from typing import Optional
|
||||
from cutekit.context import Context
|
||||
from cutekit import vt100
|
||||
from typing import Optional, cast
|
||||
|
||||
from . import vt100, cli, model
|
||||
|
||||
|
||||
def view(context: Context, scope: Optional[str] = None, showExe: bool = True, showDisabled: bool = False):
|
||||
from graphviz import Digraph
|
||||
def view(
|
||||
registry: model.Registry,
|
||||
target: model.Target,
|
||||
scope: Optional[str] = None,
|
||||
showExe: bool = True,
|
||||
showDisabled: bool = False,
|
||||
):
|
||||
from graphviz import Digraph # type: ignore
|
||||
|
||||
g = Digraph(context.target.id, filename='graph.gv')
|
||||
g = Digraph(target.id, filename="graph.gv")
|
||||
|
||||
g.attr('graph', splines='ortho', rankdir='BT', ranksep='1.5')
|
||||
g.attr('node', shape='ellipse')
|
||||
g.attr("graph", splines="ortho", rankdir="BT", ranksep="1.5")
|
||||
g.attr("node", shape="ellipse")
|
||||
g.attr(
|
||||
'graph', label=f"<<B>{scope or 'Full Dependency Graph'}</B><BR/>{context.target.id}>", labelloc='t')
|
||||
"graph",
|
||||
label=f"<<B>{scope or 'Full Dependency Graph'}</B><BR/>{target.id}>",
|
||||
labelloc="t",
|
||||
)
|
||||
|
||||
scopeInstance = None
|
||||
|
||||
if scope is not None:
|
||||
scopeInstance = context.componentByName(scope)
|
||||
scopeInstance = registry.lookup(scope, model.Component)
|
||||
|
||||
for instance in context.instances:
|
||||
if not instance.isLib() and not showExe:
|
||||
for component in registry.iterEnabled(target):
|
||||
if not component.type == model.Kind.LIB and not showExe:
|
||||
continue
|
||||
|
||||
if scopeInstance is not None and \
|
||||
instance.manifest.id != scope and \
|
||||
instance.manifest.id not in scopeInstance.resolved:
|
||||
if (
|
||||
scopeInstance is not None
|
||||
and component.id != scope
|
||||
and component.id not in scopeInstance.resolved[target.id].resolved
|
||||
):
|
||||
continue
|
||||
|
||||
if instance.enabled:
|
||||
fillcolor = "lightgrey" if instance.isLib() else "lightblue"
|
||||
shape = "plaintext" if not scope == instance.manifest.id else 'box'
|
||||
if component.resolved[target.id].enabled:
|
||||
fillcolor = "lightgrey" if component.type == model.Kind.LIB else "lightblue"
|
||||
shape = "plaintext" if not scope == component.id else "box"
|
||||
|
||||
g.node(instance.manifest.id, f"<<B>{instance.manifest.id}</B><BR/>{vt100.wordwrap(instance.manifest.decription, 40,newline='<BR/>')}>",
|
||||
shape=shape, style="filled", fillcolor=fillcolor)
|
||||
g.node(
|
||||
component.id,
|
||||
f"<<B>{component.id}</B><BR/>{vt100.wordwrap(component.decription, 40,newline='<BR/>')}>",
|
||||
shape=shape,
|
||||
style="filled",
|
||||
fillcolor=fillcolor,
|
||||
)
|
||||
|
||||
for req in instance.manifest.requires:
|
||||
g.edge(instance.manifest.id, req)
|
||||
for req in component.requires:
|
||||
g.edge(component.id, req)
|
||||
|
||||
for req in instance.manifest.provides:
|
||||
isChosen = context.target.routing.get(
|
||||
req, None) == instance.manifest.id
|
||||
for req in component.provides:
|
||||
isChosen = target.routing.get(req, None) == component.id
|
||||
|
||||
g.edge(req, instance.manifest.id, arrowhead="none", color=(
|
||||
"blue" if isChosen else "black"))
|
||||
g.edge(
|
||||
req,
|
||||
component.id,
|
||||
arrowhead="none",
|
||||
color=("blue" if isChosen else "black"),
|
||||
)
|
||||
elif showDisabled:
|
||||
g.node(instance.manifest.id, f"<<B>{instance.manifest.id}</B><BR/>{vt100.wordwrap(instance.manifest.decription, 40,newline='<BR/>')}<BR/><BR/><I>{vt100.wordwrap(instance.disableReason, 40,newline='<BR/>')}</I>>",
|
||||
shape="plaintext", style="filled", fontcolor="#999999", fillcolor="#eeeeee")
|
||||
g.node(
|
||||
component.id,
|
||||
f"<<B>{component.id}</B><BR/>{vt100.wordwrap(component.decription, 40,newline='<BR/>')}<BR/><BR/><I>{vt100.wordwrap(str(component.resolved[target.id].reason), 40,newline='<BR/>')}</I>>",
|
||||
shape="plaintext",
|
||||
style="filled",
|
||||
fontcolor="#999999",
|
||||
fillcolor="#eeeeee",
|
||||
)
|
||||
|
||||
for req in instance.manifest.requires:
|
||||
g.edge(instance.manifest.id, req, color="#aaaaaa")
|
||||
for req in component.requires:
|
||||
g.edge(component.id, req, color="#aaaaaa")
|
||||
|
||||
for req in instance.manifest.provides:
|
||||
g.edge(req, instance.manifest.id,
|
||||
arrowhead="none", color="#aaaaaa")
|
||||
for req in component.provides:
|
||||
g.edge(req, component.id, arrowhead="none", color="#aaaaaa")
|
||||
|
||||
g.view(filename=os.path.join(context.builddir(), "graph.gv"))
|
||||
g.view(filename=os.path.join(target.builddir, "graph.gv"))
|
||||
|
||||
|
||||
@cli.command("g", "graph", "Show the dependency graph")
|
||||
def graphCmd(args: cli.Args):
|
||||
registry = model.Registry.use(args)
|
||||
target = model.Target.use(args)
|
||||
|
||||
scope = cast(Optional[str], args.tryConsumeOpt("scope"))
|
||||
onlyLibs = args.consumeOpt("only-libs", False) is True
|
||||
showDisabled = args.consumeOpt("show-disabled", False) is True
|
||||
|
||||
view(registry, target, scope=scope, showExe=not onlyLibs, showDisabled=showDisabled)
|
||||
|
|
|
@ -1,17 +1,20 @@
|
|||
import os
|
||||
from typing import Any, cast, Callable, Final
|
||||
import json
|
||||
import re
|
||||
import tomllib
|
||||
|
||||
import cutekit.shell as shell
|
||||
from cutekit.compat import ensureSupportedManifest
|
||||
from pathlib import Path
|
||||
|
||||
from typing import Any, Optional, cast, Callable, Final
|
||||
from . import shell
|
||||
|
||||
Json = Any
|
||||
Builtin = Callable[..., Json]
|
||||
|
||||
BUILTINS: Final[dict[str, Builtin]] = {
|
||||
"uname": lambda arg, ctx: getattr(shell.uname(), arg).lower(),
|
||||
"include": lambda arg, ctx: evalRead(arg, compatibilityCheck=False),
|
||||
"evalRead": lambda arg, ctx: evalRead(arg, compatibilityCheck=False),
|
||||
"include": lambda arg, ctx: evalRead(Path(arg)),
|
||||
"evalRead": lambda arg, ctx: evalRead(Path(arg)),
|
||||
"join": lambda lhs, rhs, ctx: cast(
|
||||
Json, {**lhs, **rhs} if isinstance(lhs, dict) else lhs + rhs
|
||||
),
|
||||
|
@ -28,7 +31,7 @@ BUILTINS: Final[dict[str, Builtin]] = {
|
|||
}
|
||||
|
||||
|
||||
def eval(jexpr: Json, filePath: str) -> Json:
|
||||
def eval(jexpr: Json, filePath: Path) -> Json:
|
||||
if isinstance(jexpr, dict):
|
||||
result = {}
|
||||
for k in cast(dict[str, Json], jexpr):
|
||||
|
@ -50,16 +53,28 @@ def eval(jexpr: Json, filePath: str) -> Json:
|
|||
return jexpr
|
||||
|
||||
|
||||
def read(path: str) -> Json:
|
||||
def extraSchema(toml: str) -> Optional[str]:
|
||||
schemaRegex = re.compile(r"#:schema\s+(.*)")
|
||||
schema = schemaRegex.search(toml)
|
||||
return schema.group(1) if schema else None
|
||||
|
||||
|
||||
def read(path: Path) -> Json:
|
||||
try:
|
||||
with open(path, "r") as f:
|
||||
return json.load(f)
|
||||
except:
|
||||
raise RuntimeError(f"Failed to read {path}")
|
||||
if path.suffix == ".toml":
|
||||
tomlStr = f.read()
|
||||
toml = tomllib.loads(tomlStr)
|
||||
schema = extraSchema(tomlStr)
|
||||
if schema:
|
||||
toml["$schema"] = schema
|
||||
return toml
|
||||
else:
|
||||
return json.load(f)
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Failed to read {path}: {e}")
|
||||
|
||||
|
||||
def evalRead(path: str, compatibilityCheck: bool = True) -> Json:
|
||||
def evalRead(path: Path) -> Json:
|
||||
data = read(path)
|
||||
if compatibilityCheck:
|
||||
ensureSupportedManifest(data, path)
|
||||
return eval(data, path)
|
||||
|
|
|
@ -1,31 +1,29 @@
|
|||
from typing import Callable
|
||||
from cutekit.model import TargetManifest, Tools
|
||||
|
||||
Mixin = Callable[[TargetManifest, Tools], Tools]
|
||||
from . import model
|
||||
|
||||
Mixin = Callable[[model.Target, model.Tools], model.Tools]
|
||||
|
||||
|
||||
def patchToolArgs(tools: Tools, toolSpec: str, args: list[str]):
|
||||
def patchToolArgs(tools: model.Tools, toolSpec: str, args: list[str]):
|
||||
tools[toolSpec].args += args
|
||||
|
||||
|
||||
def prefixToolCmd(tools: Tools, toolSpec: str, prefix: str):
|
||||
def prefixToolCmd(tools: model.Tools, toolSpec: str, prefix: str):
|
||||
tools[toolSpec].cmd = prefix + " " + tools[toolSpec].cmd
|
||||
|
||||
|
||||
def mixinCache(target: TargetManifest, tools: Tools) -> Tools:
|
||||
def mixinCache(target: model.Target, tools: model.Tools) -> model.Tools:
|
||||
prefixToolCmd(tools, "cc", "ccache")
|
||||
prefixToolCmd(tools, "cxx", "ccache")
|
||||
return tools
|
||||
|
||||
|
||||
def makeMixinSan(san: str) -> Mixin:
|
||||
def mixinSan(target: TargetManifest, tools: Tools) -> Tools:
|
||||
patchToolArgs(
|
||||
tools, "cc", [f"-fsanitize={san}"])
|
||||
patchToolArgs(
|
||||
tools, "cxx", [f"-fsanitize={san}"])
|
||||
patchToolArgs(
|
||||
tools, "ld", [f"-fsanitize={san}"])
|
||||
def mixinSan(target: model.Target, tools: model.Tools) -> model.Tools:
|
||||
patchToolArgs(tools, "cc", [f"-fsanitize={san}"])
|
||||
patchToolArgs(tools, "cxx", [f"-fsanitize={san}"])
|
||||
patchToolArgs(tools, "ld", [f"-fsanitize={san}"])
|
||||
|
||||
return tools
|
||||
|
||||
|
@ -33,7 +31,7 @@ def makeMixinSan(san: str) -> Mixin:
|
|||
|
||||
|
||||
def makeMixinOptimize(level: str) -> Mixin:
|
||||
def mixinOptimize(target: TargetManifest, tools: Tools) -> Tools:
|
||||
def mixinOptimize(target: model.Target, tools: model.Tools) -> model.Tools:
|
||||
patchToolArgs(tools, "cc", [f"-O{level}"])
|
||||
patchToolArgs(tools, "cxx", [f"-O{level}"])
|
||||
|
||||
|
@ -42,7 +40,7 @@ def makeMixinOptimize(level: str) -> Mixin:
|
|||
return mixinOptimize
|
||||
|
||||
|
||||
def mixinDebug(target: TargetManifest, tools: Tools) -> Tools:
|
||||
def mixinDebug(target: model.Target, tools: model.Tools) -> model.Tools:
|
||||
patchToolArgs(tools, "cc", ["-g", "-gdwarf-4"])
|
||||
patchToolArgs(tools, "cxx", ["-g", "-gdwarf-4"])
|
||||
|
||||
|
@ -50,7 +48,7 @@ def mixinDebug(target: TargetManifest, tools: Tools) -> Tools:
|
|||
|
||||
|
||||
def makeMixinTune(tune: str) -> Mixin:
|
||||
def mixinTune(target: TargetManifest, tools: Tools) -> Tools:
|
||||
def mixinTune(target: model.Target, tools: model.Tools) -> model.Tools:
|
||||
patchToolArgs(tools, "cc", [f"-mtune={tune}"])
|
||||
patchToolArgs(tools, "cxx", [f"-mtune={tune}"])
|
||||
|
||||
|
@ -67,6 +65,7 @@ mixins: dict[str, Mixin] = {
|
|||
"tsan": makeMixinSan("thread"),
|
||||
"ubsan": makeMixinSan("undefined"),
|
||||
"tune": makeMixinTune("native"),
|
||||
"fast": makeMixinOptimize("fast"),
|
||||
"o3": makeMixinOptimize("3"),
|
||||
"o2": makeMixinOptimize("2"),
|
||||
"o1": makeMixinOptimize("1"),
|
||||
|
@ -79,4 +78,6 @@ def append(mixinSpec: str, mixin: Mixin):
|
|||
|
||||
|
||||
def byId(id: str) -> Mixin:
|
||||
if id not in mixins:
|
||||
raise RuntimeError(f"Unknown mixin {id}")
|
||||
return mixins[id]
|
||||
|
|
763
cutekit/model.py
763
cutekit/model.py
|
@ -1,17 +1,23 @@
|
|||
import os
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
import logging
|
||||
|
||||
from cutekit.jexpr import Json
|
||||
import dataclasses as dt
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
from enum import Enum
|
||||
from typing import Any, Generator, Optional, Type, cast
|
||||
from pathlib import Path
|
||||
from dataclasses_json import DataClassJsonMixin
|
||||
|
||||
from cutekit import const, shell
|
||||
|
||||
from . import jexpr, compat, utils, cli, vt100
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
Props = dict[str, Any]
|
||||
|
||||
|
||||
class Type(Enum):
|
||||
class Kind(Enum):
|
||||
UNKNOWN = "unknown"
|
||||
PROJECT = "project"
|
||||
TARGET = "target"
|
||||
|
@ -19,260 +25,298 @@ class Type(Enum):
|
|||
EXE = "exe"
|
||||
|
||||
|
||||
class Manifest:
|
||||
id: str = ""
|
||||
type: Type = Type.UNKNOWN
|
||||
path: str = ""
|
||||
# --- Manifest --------------------------------------------------------------- #
|
||||
|
||||
def __init__(
|
||||
self, json: Json = None, path: str = "", strict: bool = True, **kwargs: Any
|
||||
):
|
||||
if json is not None:
|
||||
if "id" not in json:
|
||||
raise RuntimeError("Missing id")
|
||||
|
||||
self.id = json["id"]
|
||||
@dt.dataclass
|
||||
class Manifest(DataClassJsonMixin):
|
||||
id: str
|
||||
type: Kind = dt.field(default=Kind.UNKNOWN)
|
||||
path: str = dt.field(default="")
|
||||
SUFFIXES = [".json", ".toml"]
|
||||
SUFFIXES_GLOBS = ["*.json", "*.toml"]
|
||||
|
||||
if "type" not in json and strict:
|
||||
raise RuntimeError("Missing type")
|
||||
@staticmethod
|
||||
def parse(path: Path, data: dict[str, Any]) -> "Manifest":
|
||||
"""
|
||||
Parse a manifest from a given path and data
|
||||
"""
|
||||
compat.ensureSupportedManifest(data, path)
|
||||
kind = Kind(data["type"])
|
||||
del data["$schema"]
|
||||
obj = KINDS[kind].from_dict(data)
|
||||
obj.path = str(path)
|
||||
return obj
|
||||
|
||||
self.type = Type(json["type"])
|
||||
@staticmethod
|
||||
def tryLoad(path: Path) -> Optional["Manifest"]:
|
||||
for suffix in Manifest.SUFFIXES:
|
||||
pathWithSuffix = path.with_suffix(suffix)
|
||||
if pathWithSuffix.exists():
|
||||
_logger.debug(f"Loading manifest from '{pathWithSuffix}'")
|
||||
return Manifest.parse(pathWithSuffix, jexpr.evalRead(pathWithSuffix))
|
||||
return None
|
||||
|
||||
self.path = path
|
||||
elif strict:
|
||||
raise RuntimeError("Missing json")
|
||||
|
||||
for key in kwargs:
|
||||
setattr(self, key, kwargs[key])
|
||||
|
||||
def toJson(self) -> Json:
|
||||
return {"id": self.id, "type": self.type.value, "path": self.path}
|
||||
|
||||
def __str__(self):
|
||||
return f"Manifest(id={self.id}, type={self.type}, path={self.path})"
|
||||
|
||||
def __repr__(self):
|
||||
return f"Manifest({id})"
|
||||
@staticmethod
|
||||
def load(path: Path) -> "Manifest":
|
||||
"""
|
||||
Load a manifest from a given path
|
||||
"""
|
||||
manifest = Manifest.tryLoad(path)
|
||||
if manifest is None:
|
||||
raise RuntimeError(f"Could not find manifest at '{path}'")
|
||||
return manifest
|
||||
|
||||
def dirname(self) -> str:
|
||||
return os.path.dirname(self.path)
|
||||
"""
|
||||
Return the directory of the manifest
|
||||
"""
|
||||
return os.path.relpath(os.path.dirname(self.path), Path.cwd())
|
||||
|
||||
def subpath(self, path) -> Path:
|
||||
return Path(self.dirname()) / path
|
||||
|
||||
def ensureType(self, t: Type[utils.T]) -> utils.T:
|
||||
"""
|
||||
Ensure that the manifest is of a given type
|
||||
"""
|
||||
if not isinstance(self, t):
|
||||
raise RuntimeError(
|
||||
f"{self.path} should be a {type.__name__} manifest but is a {self.__class__.__name__} manifest"
|
||||
)
|
||||
return cast(utils.T, self)
|
||||
|
||||
|
||||
class Extern:
|
||||
git: str = ""
|
||||
tag: str = ""
|
||||
# --- Project ---------------------------------------------------------------- #
|
||||
|
||||
def __init__(self, json: Json = None, strict: bool = True, **kwargs: Any):
|
||||
if json is not None:
|
||||
if "git" not in json and strict:
|
||||
raise RuntimeError("Missing git")
|
||||
|
||||
self.git = json["git"]
|
||||
|
||||
if "tag" not in json and strict:
|
||||
raise RuntimeError("Missing tag")
|
||||
|
||||
self.tag = json["tag"]
|
||||
elif strict:
|
||||
raise RuntimeError("Missing json")
|
||||
|
||||
for key in kwargs:
|
||||
setattr(self, key, kwargs[key])
|
||||
|
||||
def toJson(self) -> Json:
|
||||
return {"git": self.git, "tag": self.tag}
|
||||
|
||||
def __str__(self):
|
||||
return f"Extern(git={self.git}, tag={self.tag})"
|
||||
|
||||
def __repr__(self):
|
||||
return f"Extern({self.git})"
|
||||
_project: Optional["Project"] = None
|
||||
|
||||
|
||||
class ProjectManifest(Manifest):
|
||||
description: str = ""
|
||||
extern: dict[str, Extern] = {}
|
||||
|
||||
def __init__(
|
||||
self, json: Json = None, path: str = "", strict: bool = True, **kwargs: Any
|
||||
):
|
||||
if json is not None:
|
||||
if "description" not in json and strict:
|
||||
raise RuntimeError("Missing description")
|
||||
|
||||
self.description = json["description"]
|
||||
|
||||
self.extern = {k: Extern(v) for k, v in json.get("extern", {}).items()}
|
||||
elif strict:
|
||||
raise RuntimeError("Missing json")
|
||||
|
||||
super().__init__(json, path, strict, **kwargs)
|
||||
|
||||
def toJson(self) -> Json:
|
||||
return {
|
||||
**super().toJson(),
|
||||
"description": self.description,
|
||||
"extern": {k: v.toJson() for k, v in self.extern.items()},
|
||||
}
|
||||
|
||||
def __str__(self):
|
||||
return f"ProjectManifest(id={self.id}, type={self.type}, path={self.path}, description={self.description}, extern={self.extern})"
|
||||
|
||||
def __repr__(self):
|
||||
return f"ProjectManifest({self.id})"
|
||||
@dt.dataclass
|
||||
class Extern(DataClassJsonMixin):
|
||||
git: str
|
||||
tag: str
|
||||
|
||||
|
||||
class Tool:
|
||||
cmd: str = ""
|
||||
args: list[str] = []
|
||||
files: list[str] = []
|
||||
@dt.dataclass
|
||||
class Project(Manifest):
|
||||
description: str = dt.field(default="(No description)")
|
||||
extern: dict[str, Extern] = dt.field(default_factory=dict)
|
||||
|
||||
def __init__(self, json: Json = None, strict: bool = True, **kwargs: Any):
|
||||
if json is not None:
|
||||
if "cmd" not in json and strict:
|
||||
raise RuntimeError("Missing cmd")
|
||||
@property
|
||||
def externDirs(self) -> list[str]:
|
||||
res = map(lambda e: os.path.join(const.EXTERN_DIR, e), self.extern.keys())
|
||||
return list(res)
|
||||
|
||||
self.cmd = json.get("cmd", self.cmd)
|
||||
@staticmethod
|
||||
def topmost() -> Optional["Project"]:
|
||||
cwd = Path.cwd()
|
||||
topmost: Optional["Project"] = None
|
||||
while str(cwd) != cwd.root:
|
||||
projectManifest = Manifest.tryLoad(cwd / "project")
|
||||
if projectManifest is not None:
|
||||
topmost = projectManifest.ensureType(Project)
|
||||
cwd = cwd.parent
|
||||
return topmost
|
||||
|
||||
if "args" not in json and strict:
|
||||
raise RuntimeError("Missing args")
|
||||
@staticmethod
|
||||
def ensure() -> "Project":
|
||||
"""
|
||||
Ensure that a project exists in the current directory or any parent directory
|
||||
and chdir to the root of the project.
|
||||
"""
|
||||
project = Project.topmost()
|
||||
if project is None:
|
||||
raise RuntimeError(
|
||||
"No project found in this directory or any parent directory"
|
||||
)
|
||||
os.chdir(project.dirname())
|
||||
return project
|
||||
|
||||
self.args = json.get("args", [])
|
||||
@staticmethod
|
||||
def at(path: Path) -> Optional["Project"]:
|
||||
projectManifest = Manifest.tryLoad(path / "project")
|
||||
if projectManifest is None:
|
||||
return None
|
||||
return projectManifest.ensureType(Project)
|
||||
|
||||
self.files = json.get("files", [])
|
||||
elif strict:
|
||||
raise RuntimeError("Missing json")
|
||||
@staticmethod
|
||||
def fetchs(extern: dict[str, Extern]):
|
||||
for extSpec, ext in extern.items():
|
||||
extPath = os.path.join(const.EXTERN_DIR, extSpec)
|
||||
|
||||
for key in kwargs:
|
||||
setattr(self, key, kwargs[key])
|
||||
if os.path.exists(extPath):
|
||||
print(f"Skipping {extSpec}, already installed")
|
||||
continue
|
||||
|
||||
def toJson(self) -> Json:
|
||||
return {"cmd": self.cmd, "args": self.args, "files": self.files}
|
||||
print(f"Installing {extSpec}-{ext.tag} from {ext.git}...")
|
||||
shell.popen(
|
||||
"git",
|
||||
"clone",
|
||||
"--quiet",
|
||||
"--depth",
|
||||
"1",
|
||||
"--branch",
|
||||
ext.tag,
|
||||
ext.git,
|
||||
extPath,
|
||||
)
|
||||
project = Project.at(Path(extPath))
|
||||
if project is not None:
|
||||
Project.fetchs(project.extern)
|
||||
|
||||
def __str__(self):
|
||||
return f"Tool(cmd={self.cmd}, args={self.args}, files={self.files})"
|
||||
@staticmethod
|
||||
def use(args: cli.Args) -> "Project":
|
||||
global _project
|
||||
if _project is None:
|
||||
_project = Project.ensure()
|
||||
return _project
|
||||
|
||||
def __repr__(self):
|
||||
return f"Tool({self.cmd})"
|
||||
|
||||
@cli.command("i", "install", "Install required external packages")
|
||||
def installCmd(args: cli.Args):
|
||||
project = Project.use(args)
|
||||
Project.fetchs(project.extern)
|
||||
|
||||
|
||||
@cli.command("I", "init", "Initialize a new project")
|
||||
def initCmd(args: cli.Args):
|
||||
import requests
|
||||
|
||||
repo = args.consumeOpt("repo", const.DEFAULT_REPO_TEMPLATES)
|
||||
list = args.consumeOpt("list")
|
||||
|
||||
template = args.consumeArg()
|
||||
name = args.consumeArg()
|
||||
|
||||
_logger.info("Fetching registry...")
|
||||
|
||||
r = requests.get(f"https://raw.githubusercontent.com/{repo}/main/registry.json")
|
||||
|
||||
if r.status_code != 200:
|
||||
_logger.error("Failed to fetch registry")
|
||||
exit(1)
|
||||
|
||||
registry = r.json()
|
||||
|
||||
if list:
|
||||
print(
|
||||
"\n".join(f"* {entry['id']} - {entry['description']}" for entry in registry)
|
||||
)
|
||||
return
|
||||
|
||||
if not template:
|
||||
raise RuntimeError("Template not specified")
|
||||
|
||||
def template_match(t: jexpr.Json) -> str:
|
||||
return t["id"] == template
|
||||
|
||||
if not any(filter(template_match, registry)):
|
||||
raise LookupError(f"Couldn't find a template named {template}")
|
||||
|
||||
if not name:
|
||||
_logger.info(f"No name was provided, defaulting to {template}")
|
||||
name = template
|
||||
|
||||
if os.path.exists(name):
|
||||
raise RuntimeError(f"Directory {name} already exists")
|
||||
|
||||
print(f"Creating project {name} from template {template}...")
|
||||
shell.cloneDir(f"https://github.com/{repo}", template, name)
|
||||
print(f"Project {name} created\n")
|
||||
|
||||
print("We suggest that you begin by typing:")
|
||||
print(f" {vt100.GREEN}cd {name}{vt100.RESET}")
|
||||
print(
|
||||
f" {vt100.GREEN}cutekit install{vt100.BRIGHT_BLACK} # Install external packages{vt100.RESET}"
|
||||
)
|
||||
print(
|
||||
f" {vt100.GREEN}cutekit build{vt100.BRIGHT_BLACK} # Build the project{vt100.RESET}"
|
||||
)
|
||||
|
||||
|
||||
# --- Target ----------------------------------------------------------------- #
|
||||
|
||||
|
||||
@dt.dataclass
|
||||
class Tool(DataClassJsonMixin):
|
||||
cmd: str = dt.field(default="")
|
||||
args: list[str] = dt.field(default_factory=list)
|
||||
files: list[str] = dt.field(default_factory=list)
|
||||
|
||||
|
||||
Tools = dict[str, Tool]
|
||||
|
||||
DEFAULT_TOOLS: Tools = {
|
||||
"cp": Tool("cp"),
|
||||
}
|
||||
|
||||
class TargetManifest(Manifest):
|
||||
props: Props
|
||||
tools: Tools
|
||||
routing: dict[str, str]
|
||||
|
||||
def __init__(
|
||||
self, json: Json = None, path: str = "", strict: bool = True, **kwargs: Any
|
||||
):
|
||||
if json is not None:
|
||||
if "props" not in json and strict:
|
||||
raise RuntimeError("Missing props")
|
||||
@dt.dataclass
|
||||
class Target(Manifest):
|
||||
props: Props = dt.field(default_factory=dict)
|
||||
tools: Tools = dt.field(default_factory=dict)
|
||||
routing: dict[str, str] = dt.field(default_factory=dict)
|
||||
|
||||
self.props = json["props"]
|
||||
@property
|
||||
def hashid(self) -> str:
|
||||
return utils.hash((self.props, [v.to_dict() for k, v in self.tools.items()]))
|
||||
|
||||
if "tools" not in json and strict:
|
||||
raise RuntimeError("Missing tools")
|
||||
@property
|
||||
def builddir(self) -> str:
|
||||
return os.path.join(const.BUILD_DIR, f"{self.id}-{self.hashid[:8]}")
|
||||
|
||||
self.tools = {k: Tool(v) for k, v in json["tools"].items()}
|
||||
|
||||
self.routing = json.get("routing", {})
|
||||
|
||||
super().__init__(json, path, strict, **kwargs)
|
||||
|
||||
def toJson(self) -> Json:
|
||||
return {
|
||||
**super().toJson(),
|
||||
"props": self.props,
|
||||
"tools": {k: v.toJson() for k, v in self.tools.items()},
|
||||
"routing": self.routing,
|
||||
}
|
||||
|
||||
def __repr__(self):
|
||||
return f"TargetManifest({self.id})"
|
||||
@staticmethod
|
||||
def use(args: cli.Args) -> "Target":
|
||||
registry = Registry.use(args)
|
||||
targetSpec = str(args.consumeOpt("target", "host-" + shell.uname().machine))
|
||||
return registry.ensure(targetSpec, Target)
|
||||
|
||||
def route(self, componentSpec: str):
|
||||
"""
|
||||
Route a component spec to a target specific component spec
|
||||
"""
|
||||
return (
|
||||
self.routing[componentSpec]
|
||||
if componentSpec in self.routing
|
||||
else componentSpec
|
||||
)
|
||||
|
||||
def cdefs(self) -> list[str]:
|
||||
defines: list[str] = []
|
||||
|
||||
def sanatize(s: str) -> str:
|
||||
return s.lower().replace(" ", "_").replace("-", "_").replace(".", "_")
|
||||
|
||||
for key in self.props:
|
||||
prop = self.props[key]
|
||||
propStr = str(prop)
|
||||
if isinstance(prop, bool):
|
||||
if prop:
|
||||
defines += [f"-D__ck_{sanatize(key)}__"]
|
||||
else:
|
||||
defines += [f"-D__ck_{sanatize(key)}_{sanatize(propStr)}__"]
|
||||
defines += [f"-D__ck_{sanatize(key)}_value={propStr}"]
|
||||
|
||||
return defines
|
||||
# --- Component -------------------------------------------------------------- #
|
||||
|
||||
|
||||
class ComponentManifest(Manifest):
|
||||
decription: str = "(No description)"
|
||||
props: Props = {}
|
||||
tools: Tools = {}
|
||||
enableIf: dict[str, list[Any]] = {}
|
||||
requires: list[str] = []
|
||||
provides: list[str] = []
|
||||
subdirs: list[str] = []
|
||||
@dt.dataclass
|
||||
class Resolved:
|
||||
reason: Optional[str] = None
|
||||
resolved: list[str] = dt.field(default_factory=list)
|
||||
|
||||
def __init__(
|
||||
self, json: Json = None, path: str = "", strict: bool = True, **kwargs: Any
|
||||
):
|
||||
if json is not None:
|
||||
self.decription = json.get("description", self.decription)
|
||||
self.props = json.get("props", self.props)
|
||||
self.tools = {
|
||||
k: Tool(v, strict=False) for k, v in json.get("tools", {}).items()
|
||||
}
|
||||
self.enableIf = json.get("enableIf", self.enableIf)
|
||||
self.requires = json.get("requires", self.requires)
|
||||
self.provides = json.get("provides", self.provides)
|
||||
self.subdirs = list(
|
||||
map(
|
||||
lambda x: os.path.join(os.path.dirname(path), x),
|
||||
json.get("subdirs", [""]),
|
||||
)
|
||||
)
|
||||
@property
|
||||
def enabled(self) -> bool:
|
||||
return self.reason is None
|
||||
|
||||
super().__init__(json, path, strict, **kwargs)
|
||||
|
||||
def toJson(self) -> Json:
|
||||
return {
|
||||
**super().toJson(),
|
||||
"description": self.decription,
|
||||
"props": self.props,
|
||||
"tools": {k: v.toJson() for k, v in self.tools.items()},
|
||||
"enableIf": self.enableIf,
|
||||
"requires": self.requires,
|
||||
"provides": self.provides,
|
||||
"subdirs": self.subdirs,
|
||||
}
|
||||
@dt.dataclass
|
||||
class Component(Manifest):
|
||||
decription: str = dt.field(default="(No description)")
|
||||
props: Props = dt.field(default_factory=dict)
|
||||
tools: Tools = dt.field(default_factory=dict)
|
||||
enableIf: dict[str, list[Any]] = dt.field(default_factory=dict)
|
||||
requires: list[str] = dt.field(default_factory=list)
|
||||
provides: list[str] = dt.field(default_factory=list)
|
||||
subdirs: list[str] = dt.field(default_factory=list)
|
||||
injects: list[str] = dt.field(default_factory=list)
|
||||
resolved: dict[str, Resolved] = dt.field(default_factory=dict)
|
||||
|
||||
def __repr__(self):
|
||||
return f"ComponentManifest({self.id})"
|
||||
|
||||
def isEnabled(self, target: TargetManifest) -> tuple[bool, str]:
|
||||
def isEnabled(self, target: Target) -> tuple[bool, str]:
|
||||
for k, v in self.enableIf.items():
|
||||
if k not in target.props:
|
||||
logger.info(f"Component {self.id} disabled by missing {k} in target")
|
||||
_logger.info(f"Component {self.id} disabled by missing {k} in target")
|
||||
return False, f"Missing props '{k}' in target"
|
||||
|
||||
if target.props[k] not in v:
|
||||
vStrs = [f"'{str(x)}'" for x in v]
|
||||
logger.info(
|
||||
_logger.info(
|
||||
f"Component {self.id} disabled by {k}={target.props[k]} not in {v}"
|
||||
)
|
||||
return (
|
||||
|
@ -281,3 +325,308 @@ class ComponentManifest(Manifest):
|
|||
)
|
||||
|
||||
return True, ""
|
||||
|
||||
|
||||
KINDS: dict[Kind, Type[Manifest]] = {
|
||||
Kind.PROJECT: Project,
|
||||
Kind.TARGET: Target,
|
||||
Kind.LIB: Component,
|
||||
Kind.EXE: Component,
|
||||
}
|
||||
|
||||
# --- Dependency resolution -------------------------------------------------- #
|
||||
|
||||
|
||||
@dt.dataclass
|
||||
class Resolver:
|
||||
_registry: "Registry"
|
||||
_target: Target
|
||||
_mappings: dict[str, list[Component]] = dt.field(default_factory=dict)
|
||||
_cache: dict[str, Resolved] = dt.field(default_factory=dict)
|
||||
_baked = False
|
||||
|
||||
def _bake(self):
|
||||
"""
|
||||
Bake the resolver by building a mapping of all
|
||||
components that provide a given spec.
|
||||
"""
|
||||
|
||||
if self._baked:
|
||||
return
|
||||
|
||||
for c in self._registry.iter(Component):
|
||||
for p in c.provides + [c.id]:
|
||||
if p not in self._mappings and [0]:
|
||||
self._mappings[p] = []
|
||||
self._mappings[p].append(c)
|
||||
|
||||
# Overide with target routing since it has priority
|
||||
# over component provides and id
|
||||
for k, v in self._target.routing.items():
|
||||
component = self._registry.lookup(v, Component)
|
||||
self._mappings[k] = [component] if component else []
|
||||
|
||||
self._baked = True
|
||||
|
||||
def _provider(self, spec: str) -> tuple[Optional[str], str]:
|
||||
"""
|
||||
Returns the provider for a given spec.
|
||||
"""
|
||||
result = self._mappings.get(spec, [])
|
||||
|
||||
if len(result) == 1:
|
||||
enabled, reason = result[0].isEnabled(self._target)
|
||||
if not enabled:
|
||||
return (None, reason)
|
||||
|
||||
def checkIsEnabled(c: Component) -> bool:
|
||||
enabled, reason = c.isEnabled(self._target)
|
||||
if not enabled:
|
||||
_logger.info(f"Component {c.id} cannot provide '{spec}': {reason}")
|
||||
return enabled
|
||||
|
||||
result = list(filter(checkIsEnabled, result))
|
||||
|
||||
if result == []:
|
||||
return (None, f"No provider for '{spec}'")
|
||||
|
||||
if len(result) > 1:
|
||||
ids = list(map(lambda x: x.id, result))
|
||||
return (None, f"Multiple providers for '{spec}': {','.join(ids)}")
|
||||
|
||||
return (result[0].id, "")
|
||||
|
||||
def resolve(self, what: str, stack: list[str] = []) -> Resolved:
|
||||
"""
|
||||
Resolve a given spec to a list of components.
|
||||
"""
|
||||
self._bake()
|
||||
|
||||
if what in self._cache:
|
||||
return self._cache[what]
|
||||
|
||||
keep, unresolvedReason = self._provider(what)
|
||||
|
||||
if not keep:
|
||||
_logger.error(f"Dependency '{what}' not found: {unresolvedReason}")
|
||||
self._cache[what] = Resolved(reason=unresolvedReason)
|
||||
return self._cache[what]
|
||||
|
||||
if keep in self._cache:
|
||||
return self._cache[keep]
|
||||
|
||||
if keep in stack:
|
||||
raise RuntimeError(
|
||||
f"Dependency loop while resolving '{what}': {stack} -> {keep}"
|
||||
)
|
||||
|
||||
stack.append(keep)
|
||||
|
||||
component = self._registry.lookup(keep, Component)
|
||||
if not component:
|
||||
return Resolved(reason="No provider for 'myembed'")
|
||||
|
||||
result: list[str] = []
|
||||
|
||||
for req in component.requires:
|
||||
reqResolved = self.resolve(req, stack)
|
||||
if reqResolved.reason:
|
||||
stack.pop()
|
||||
|
||||
self._cache[keep] = Resolved(reason=reqResolved.reason)
|
||||
return self._cache[keep]
|
||||
|
||||
result.extend(reqResolved.resolved)
|
||||
|
||||
stack.pop()
|
||||
result.insert(0, keep)
|
||||
self._cache[keep] = Resolved(resolved=utils.uniq(result))
|
||||
return self._cache[keep]
|
||||
|
||||
|
||||
# --- Registry --------------------------------------------------------------- #
|
||||
|
||||
_registry: Optional["Registry"] = None
|
||||
|
||||
|
||||
@dt.dataclass
|
||||
class Registry(DataClassJsonMixin):
|
||||
project: Project
|
||||
manifests: dict[str, Manifest] = dt.field(default_factory=dict)
|
||||
|
||||
def _append(self, m: Optional[Manifest]) -> Optional[Manifest]:
|
||||
"""
|
||||
Append a manifest to the model
|
||||
"""
|
||||
if m is None:
|
||||
return m
|
||||
|
||||
if m.id in self.manifests:
|
||||
raise RuntimeError(
|
||||
f"Duplicated manifest '{m.id}' at '{m.path}' already loaded from '{self.manifests[m.id].path}'"
|
||||
)
|
||||
|
||||
self.manifests[m.id] = m
|
||||
return m
|
||||
|
||||
def iter(self, type: Type[utils.T]) -> Generator[utils.T, None, None]:
|
||||
"""
|
||||
Iterate over all manifests of a given type
|
||||
"""
|
||||
|
||||
for m in self.manifests.values():
|
||||
if isinstance(m, type):
|
||||
yield m
|
||||
|
||||
def iterEnabled(self, target: Target) -> Generator[Component, None, None]:
|
||||
for c in self.iter(Component):
|
||||
resolve = c.resolved[target.id]
|
||||
if resolve.enabled:
|
||||
yield c
|
||||
|
||||
def lookup(
|
||||
self, name: str, type: Type[utils.T], includeProvides: bool = False
|
||||
) -> Optional[utils.T]:
|
||||
"""
|
||||
Lookup a manifest of a given type by name
|
||||
"""
|
||||
|
||||
if name in self.manifests:
|
||||
m = self.manifests[name]
|
||||
if isinstance(m, type):
|
||||
return m
|
||||
|
||||
if includeProvides and type is Component:
|
||||
for m in self.iter(Component):
|
||||
if name in m.provides:
|
||||
return m # type: ignore
|
||||
|
||||
return None
|
||||
|
||||
def ensure(self, name: str, type: Type[utils.T]) -> utils.T:
|
||||
"""
|
||||
Ensure that a manifest of a given type exists
|
||||
and return it.
|
||||
"""
|
||||
|
||||
m = self.lookup(name, type)
|
||||
if not m:
|
||||
raise RuntimeError(f"Could not find {type.__name__} '{name}'")
|
||||
return m
|
||||
|
||||
@staticmethod
|
||||
def use(args: cli.Args) -> "Registry":
|
||||
global _registry
|
||||
|
||||
if _registry is not None:
|
||||
return _registry
|
||||
|
||||
project = Project.use(args)
|
||||
mixins = str(args.consumeOpt("mixins", "")).split(",")
|
||||
if mixins == [""]:
|
||||
mixins = []
|
||||
props = cast(dict[str, str], args.consumePrefix("prop:"))
|
||||
|
||||
_registry = Registry.load(project, mixins, props)
|
||||
return _registry
|
||||
|
||||
@staticmethod
|
||||
def load(project: Project, mixins: list[str], props: Props) -> "Registry":
|
||||
r = Registry(project)
|
||||
r._append(project)
|
||||
|
||||
# Lookup and load all extern projects
|
||||
for externDir in project.externDirs:
|
||||
extern = r._append(
|
||||
Manifest.tryLoad(Path(externDir) / "project")
|
||||
or Manifest.tryLoad(Path(externDir) / "manifest")
|
||||
)
|
||||
|
||||
if extern is not None:
|
||||
_logger.warn("Extern project does not have a project or manifest")
|
||||
|
||||
# Load all manifests from projects
|
||||
for project in list(r.iter(Project)):
|
||||
targetDir = os.path.join(project.dirname(), const.TARGETS_DIR)
|
||||
targetFiles = shell.find(targetDir, Manifest.SUFFIXES_GLOBS)
|
||||
|
||||
for targetFile in targetFiles:
|
||||
r._append(Manifest.load(Path(targetFile)).ensureType(Target))
|
||||
|
||||
componentFiles = shell.find(
|
||||
os.path.join(project.dirname(), const.SRC_DIR),
|
||||
["manifest" + s for s in Manifest.SUFFIXES],
|
||||
)
|
||||
|
||||
rootComponent = Manifest.tryLoad(Path(project.dirname()) / "manifest")
|
||||
if rootComponent is not None:
|
||||
r._append(rootComponent)
|
||||
|
||||
for componentFile in componentFiles:
|
||||
r._append(Manifest.load(Path(componentFile)).ensureType(Component))
|
||||
|
||||
# Resolve all dependencies for all targets
|
||||
for target in r.iter(Target):
|
||||
target.props |= props
|
||||
resolver = Resolver(r, target)
|
||||
|
||||
# Apply injects
|
||||
for c in r.iter(Component):
|
||||
if c.isEnabled(target)[0]:
|
||||
for inject in c.injects:
|
||||
victim = r.lookup(inject, Component)
|
||||
if not victim:
|
||||
raise RuntimeError(f"Cannot find component '{inject}'")
|
||||
victim.requires += [c.id]
|
||||
|
||||
# Resolve all components
|
||||
for c in r.iter(Component):
|
||||
resolved = resolver.resolve(c.id)
|
||||
if resolved.reason:
|
||||
_logger.info(f"Component '{c.id}' disabled: {resolved.reason}")
|
||||
c.resolved[target.id] = resolved
|
||||
|
||||
# Resolve tooling
|
||||
tools: Tools = target.tools
|
||||
|
||||
# Merge in default tools
|
||||
for k, v in DEFAULT_TOOLS.items():
|
||||
if k not in tools:
|
||||
tools[k] = dt.replace(v)
|
||||
|
||||
from . import mixins as mxs
|
||||
|
||||
for mix in mixins:
|
||||
mixin = mxs.byId(mix)
|
||||
tools = mixin(target, tools)
|
||||
|
||||
# Apply tooling from components
|
||||
for c in r.iter(Component):
|
||||
if c.resolved[target.id].enabled:
|
||||
for k, v in c.tools.items():
|
||||
tools[k].args += v.args
|
||||
|
||||
return r
|
||||
|
||||
|
||||
@cli.command("l", "list", "List all components and targets")
|
||||
def listCmd(args: cli.Args):
|
||||
registry = Registry.use(args)
|
||||
|
||||
components = list(registry.iter(Component))
|
||||
targets = list(registry.iter(Target))
|
||||
|
||||
vt100.title("Components")
|
||||
if len(components) == 0:
|
||||
print(vt100.p("(No components available)"))
|
||||
else:
|
||||
print(vt100.p(", ".join(map(lambda m: m.id, components))))
|
||||
print()
|
||||
|
||||
vt100.title("Targets")
|
||||
|
||||
if len(targets) == 0:
|
||||
print(vt100.p("(No targets available)"))
|
||||
else:
|
||||
print(vt100.p(", ".join(map(lambda m: m.id, targets))))
|
||||
print()
|
||||
|
|
159
cutekit/ninja.py
159
cutekit/ninja.py
|
@ -23,13 +23,13 @@ use Python.
|
|||
"""
|
||||
|
||||
import textwrap
|
||||
from typing import TextIO, Union
|
||||
|
||||
from cutekit.utils import asList
|
||||
from typing import TextIO, Union
|
||||
from . import utils
|
||||
|
||||
|
||||
def escapePath(word: str) -> str:
|
||||
return word.replace('$ ', '$$ ').replace(' ', '$ ').replace(':', '$:')
|
||||
return word.replace("$ ", "$$ ").replace(" ", "$ ").replace(":", "$:")
|
||||
|
||||
|
||||
VarValue = Union[int, str, list[str], None]
|
||||
|
@ -42,92 +42,98 @@ class Writer(object):
|
|||
self.width = width
|
||||
|
||||
def newline(self) -> None:
|
||||
self.output.write('\n')
|
||||
self.output.write("\n")
|
||||
|
||||
def comment(self, text: str) -> None:
|
||||
for line in textwrap.wrap(text, self.width - 2, break_long_words=False,
|
||||
break_on_hyphens=False):
|
||||
self.output.write('# ' + line + '\n')
|
||||
for line in textwrap.wrap(
|
||||
text, self.width - 2, break_long_words=False, break_on_hyphens=False
|
||||
):
|
||||
self.output.write("# " + line + "\n")
|
||||
|
||||
def separator(self, text : str) -> None:
|
||||
self.output.write(f"# --- {text} ---" + '-' *
|
||||
(self.width - 10 - len(text)) + " #\n\n")
|
||||
def separator(self, text: str) -> None:
|
||||
self.output.write(
|
||||
f"# --- {text} ---" + "-" * (self.width - 10 - len(text)) + " #\n\n"
|
||||
)
|
||||
|
||||
def variable(self, key: str, value: VarValue, indent: int = 0) -> None:
|
||||
if value is None:
|
||||
return
|
||||
if isinstance(value, list):
|
||||
value = ' '.join(filter(None, value)) # Filter out empty strings.
|
||||
self._line('%s = %s' % (key, value), indent)
|
||||
value = " ".join(filter(None, value)) # Filter out empty strings.
|
||||
self._line("%s = %s" % (key, value), indent)
|
||||
|
||||
def pool(self, name: str, depth: int) -> None:
|
||||
self._line('pool %s' % name)
|
||||
self.variable('depth', depth, indent=1)
|
||||
self._line("pool %s" % name)
|
||||
self.variable("depth", depth, indent=1)
|
||||
|
||||
def rule(self,
|
||||
name: str,
|
||||
command: VarValue,
|
||||
description: Union[str, None] = None,
|
||||
depfile: VarValue = None,
|
||||
generator: VarValue = False,
|
||||
pool: VarValue = None,
|
||||
restat: bool = False,
|
||||
rspfile: VarValue = None,
|
||||
rspfile_content: VarValue = None,
|
||||
deps: VarValue = None) -> None:
|
||||
self._line('rule %s' % name)
|
||||
self.variable('command', command, indent=1)
|
||||
def rule(
|
||||
self,
|
||||
name: str,
|
||||
command: VarValue,
|
||||
description: Union[str, None] = None,
|
||||
depfile: VarValue = None,
|
||||
generator: VarValue = False,
|
||||
pool: VarValue = None,
|
||||
restat: bool = False,
|
||||
rspfile: VarValue = None,
|
||||
rspfile_content: VarValue = None,
|
||||
deps: VarValue = None,
|
||||
) -> None:
|
||||
self._line("rule %s" % name)
|
||||
self.variable("command", command, indent=1)
|
||||
if description:
|
||||
self.variable('description', description, indent=1)
|
||||
self.variable("description", description, indent=1)
|
||||
if depfile:
|
||||
self.variable('depfile', depfile, indent=1)
|
||||
self.variable("depfile", depfile, indent=1)
|
||||
if generator:
|
||||
self.variable('generator', '1', indent=1)
|
||||
self.variable("generator", "1", indent=1)
|
||||
if pool:
|
||||
self.variable('pool', pool, indent=1)
|
||||
self.variable("pool", pool, indent=1)
|
||||
if restat:
|
||||
self.variable('restat', '1', indent=1)
|
||||
self.variable("restat", "1", indent=1)
|
||||
if rspfile:
|
||||
self.variable('rspfile', rspfile, indent=1)
|
||||
self.variable("rspfile", rspfile, indent=1)
|
||||
if rspfile_content:
|
||||
self.variable('rspfile_content', rspfile_content, indent=1)
|
||||
self.variable("rspfile_content", rspfile_content, indent=1)
|
||||
if deps:
|
||||
self.variable('deps', deps, indent=1)
|
||||
self.variable("deps", deps, indent=1)
|
||||
|
||||
def build(self,
|
||||
outputs: Union[str, list[str]],
|
||||
rule: str,
|
||||
inputs: Union[VarPath, None],
|
||||
implicit: VarPath = None,
|
||||
order_only: VarPath = None,
|
||||
variables: Union[dict[str, str], None] = None,
|
||||
implicit_outputs: VarPath = None,
|
||||
pool: Union[str, None] = None,
|
||||
dyndep: Union[str, None] = None) -> list[str]:
|
||||
outputs = asList(outputs)
|
||||
def build(
|
||||
self,
|
||||
outputs: Union[str, list[str]],
|
||||
rule: str,
|
||||
inputs: Union[VarPath, None],
|
||||
implicit: VarPath = None,
|
||||
order_only: VarPath = None,
|
||||
variables: Union[dict[str, str], None] = None,
|
||||
implicit_outputs: VarPath = None,
|
||||
pool: Union[str, None] = None,
|
||||
dyndep: Union[str, None] = None,
|
||||
) -> list[str]:
|
||||
outputs = utils.asList(outputs)
|
||||
out_outputs = [escapePath(x) for x in outputs]
|
||||
all_inputs = [escapePath(x) for x in asList(inputs)]
|
||||
all_inputs = [escapePath(x) for x in utils.asList(inputs)]
|
||||
|
||||
if implicit:
|
||||
implicit = [escapePath(x) for x in asList(implicit)]
|
||||
all_inputs.append('|')
|
||||
implicit = [escapePath(x) for x in utils.asList(implicit)]
|
||||
all_inputs.append("|")
|
||||
all_inputs.extend(implicit)
|
||||
if order_only:
|
||||
order_only = [escapePath(x) for x in asList(order_only)]
|
||||
all_inputs.append('||')
|
||||
order_only = [escapePath(x) for x in utils.asList(order_only)]
|
||||
all_inputs.append("||")
|
||||
all_inputs.extend(order_only)
|
||||
if implicit_outputs:
|
||||
implicit_outputs = [escapePath(x)
|
||||
for x in asList(implicit_outputs)]
|
||||
out_outputs.append('|')
|
||||
implicit_outputs = [escapePath(x) for x in utils.asList(implicit_outputs)]
|
||||
out_outputs.append("|")
|
||||
out_outputs.extend(implicit_outputs)
|
||||
|
||||
self._line('build %s: %s' % (' '.join(out_outputs),
|
||||
' '.join([rule] + all_inputs)))
|
||||
self._line(
|
||||
"build %s: %s" % (" ".join(out_outputs), " ".join([rule] + all_inputs))
|
||||
)
|
||||
if pool is not None:
|
||||
self._line(' pool = %s' % pool)
|
||||
self._line(" pool = %s" % pool)
|
||||
if dyndep is not None:
|
||||
self._line(' dyndep = %s' % dyndep)
|
||||
self._line(" dyndep = %s" % dyndep)
|
||||
|
||||
if variables:
|
||||
iterator = iter(variables.items())
|
||||
|
@ -138,58 +144,59 @@ class Writer(object):
|
|||
return outputs
|
||||
|
||||
def include(self, path: str) -> None:
|
||||
self._line('include %s' % path)
|
||||
self._line("include %s" % path)
|
||||
|
||||
def subninja(self, path: str) -> None:
|
||||
self._line('subninja %s' % path)
|
||||
self._line("subninja %s" % path)
|
||||
|
||||
def default(self, paths: VarPath) -> None:
|
||||
self._line('default %s' % ' '.join(asList(paths)))
|
||||
self._line("default %s" % " ".join(utils.asList(paths)))
|
||||
|
||||
def _count_dollars_before_index(self, s: str, i: int) -> int:
|
||||
"""Returns the number of '$' characters right in front of s[i]."""
|
||||
dollar_count = 0
|
||||
dollar_index = i - 1
|
||||
while dollar_index > 0 and s[dollar_index] == '$':
|
||||
while dollar_index > 0 and s[dollar_index] == "$":
|
||||
dollar_count += 1
|
||||
dollar_index -= 1
|
||||
return dollar_count
|
||||
|
||||
def _line(self, text: str, indent: int = 0) -> None:
|
||||
"""Write 'text' word-wrapped at self.width characters."""
|
||||
leading_space = ' ' * indent
|
||||
leading_space = " " * indent
|
||||
while len(leading_space) + len(text) > self.width:
|
||||
# The text is too wide; wrap if possible.
|
||||
|
||||
# Find the rightmost space that would obey our width constraint and
|
||||
# that's not an escaped space.
|
||||
available_space = self.width - len(leading_space) - len(' $')
|
||||
available_space = self.width - len(leading_space) - len(" $")
|
||||
space = available_space
|
||||
while True:
|
||||
space = text.rfind(' ', 0, space)
|
||||
if (space < 0 or
|
||||
self._count_dollars_before_index(text, space) % 2 == 0):
|
||||
space = text.rfind(" ", 0, space)
|
||||
if space < 0 or self._count_dollars_before_index(text, space) % 2 == 0:
|
||||
break
|
||||
|
||||
if space < 0:
|
||||
# No such space; just use the first unescaped space we can find.
|
||||
space = available_space - 1
|
||||
while True:
|
||||
space = text.find(' ', space + 1)
|
||||
if (space < 0 or
|
||||
self._count_dollars_before_index(text, space) % 2 == 0):
|
||||
space = text.find(" ", space + 1)
|
||||
if (
|
||||
space < 0
|
||||
or self._count_dollars_before_index(text, space) % 2 == 0
|
||||
):
|
||||
break
|
||||
if space < 0:
|
||||
# Give up on breaking.
|
||||
break
|
||||
|
||||
self.output.write(leading_space + text[0:space] + ' $\n')
|
||||
text = text[space+1:]
|
||||
self.output.write(leading_space + text[0:space] + " $\n")
|
||||
text = text[space + 1 :]
|
||||
|
||||
# Subsequent lines are continuations, so indent them.
|
||||
leading_space = ' ' * (indent+2)
|
||||
leading_space = " " * (indent + 2)
|
||||
|
||||
self.output.write(leading_space + text + '\n')
|
||||
self.output.write(leading_space + text + "\n")
|
||||
|
||||
def close(self) -> None:
|
||||
self.output.close()
|
||||
|
@ -198,6 +205,6 @@ class Writer(object):
|
|||
def escape(string: str) -> str:
|
||||
"""Escape a string such that it can be embedded into a Ninja file without
|
||||
further interpretation."""
|
||||
assert '\n' not in string, 'Ninja syntax does not allow newlines'
|
||||
assert "\n" not in string, "Ninja syntax does not allow newlines"
|
||||
# We only have one special metacharacter: '$'.
|
||||
return string.replace('$', '$$')
|
||||
return string.replace("$", "$$")
|
||||
|
|
|
@ -1,18 +1,19 @@
|
|||
import os
|
||||
import logging
|
||||
|
||||
from cutekit import shell, project, const, context
|
||||
from . import shell, model, const
|
||||
|
||||
import importlib.util as importlib
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def load(path: str):
|
||||
logger.info(f"Loading plugin {path}")
|
||||
_logger.info(f"Loading plugin {path}")
|
||||
spec = importlib.spec_from_file_location("plugin", path)
|
||||
|
||||
if not spec or not spec.loader:
|
||||
logger.error(f"Failed to load plugin {path}")
|
||||
_logger.error(f"Failed to load plugin {path}")
|
||||
return None
|
||||
|
||||
module = importlib.module_from_spec(spec)
|
||||
|
@ -20,25 +21,23 @@ def load(path: str):
|
|||
|
||||
|
||||
def loadAll():
|
||||
logger.info("Loading plugins...")
|
||||
_logger.info("Loading plugins...")
|
||||
|
||||
projectRoot = project.root()
|
||||
|
||||
if projectRoot is None:
|
||||
logger.info("Not in project, skipping plugin loading")
|
||||
project = model.Project.topmost()
|
||||
if project is None:
|
||||
_logger.info("Not in project, skipping plugin loading")
|
||||
return
|
||||
|
||||
pj = context.loadProject(projectRoot)
|
||||
paths = list(map(lambda e: os.path.join(const.EXTERN_DIR, e), pj.extern.keys())) + ["."]
|
||||
paths = list(
|
||||
map(lambda e: os.path.join(const.EXTERN_DIR, e), project.extern.keys())
|
||||
) + ["."]
|
||||
|
||||
for dirname in paths:
|
||||
pluginDir = os.path.join(projectRoot, dirname, const.META_DIR, "plugins")
|
||||
pluginDir = os.path.join(project.dirname(), dirname, const.META_DIR, "plugins")
|
||||
|
||||
for files in shell.readdir(pluginDir):
|
||||
if files.endswith(".py"):
|
||||
plugin = load(os.path.join(pluginDir, files))
|
||||
|
||||
if plugin:
|
||||
logger.info(f"Loaded plugin {plugin.name}")
|
||||
_logger.info(f"Loaded plugin {plugin.name}")
|
||||
plugin.init()
|
||||
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
import os
|
||||
from typing import Optional
|
||||
|
||||
|
||||
def root() -> Optional[str]:
|
||||
cwd = os.getcwd()
|
||||
while cwd != "/":
|
||||
if os.path.isfile(os.path.join(cwd, "project.json")):
|
||||
return cwd
|
||||
cwd = os.path.dirname(cwd)
|
||||
return None
|
||||
|
||||
|
||||
def chdir() -> None:
|
||||
projectRoot = root()
|
||||
if projectRoot is None:
|
||||
raise RuntimeError(
|
||||
"No project.json found in this directory or any parent directory")
|
||||
|
||||
os.chdir(projectRoot)
|
|
@ -9,7 +9,15 @@ class Rule:
|
|||
args: list[str]
|
||||
deps: Optional[str] = None
|
||||
|
||||
def __init__(self, id: str, fileIn: list[str], fileOut: list[str], rule: str, args: list[str] = [], deps: Optional[str] = None):
|
||||
def __init__(
|
||||
self,
|
||||
id: str,
|
||||
fileIn: list[str],
|
||||
fileOut: list[str],
|
||||
rule: str,
|
||||
args: list[str] = [],
|
||||
deps: Optional[str] = None,
|
||||
):
|
||||
self.id = id
|
||||
self.fileIn = fileIn
|
||||
self.fileOut = fileOut
|
||||
|
@ -19,16 +27,23 @@ class Rule:
|
|||
|
||||
|
||||
rules: dict[str, Rule] = {
|
||||
"cc": Rule("cc", ["*.c"], ["*.o"], "-c -o $out $in -MD -MF $out.d $flags $cincs $cdefs", ["-std=gnu2x",
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-Werror"], "$out.d"),
|
||||
"cxx": Rule("cxx", ["*.cpp", "*.cc", "*.cxx"], ["*.o"], "-c -o $out $in -MD -MF $out.d $flags $cincs $cdefs", ["-std=gnu++2b",
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-Werror",
|
||||
"-fno-exceptions",
|
||||
"-fno-rtti"], "$out.d"),
|
||||
"cp": Rule("cp", ["*"], ["*"], "$in $out"),
|
||||
"cc": Rule(
|
||||
"cc",
|
||||
["*.c"],
|
||||
["*.o"],
|
||||
"-c -o $out $in -MD -MF $out.d $flags $cincs $cdefs",
|
||||
["-std=gnu2x", "-Wall", "-Wextra", "-Werror"],
|
||||
"$out.d",
|
||||
),
|
||||
"cxx": Rule(
|
||||
"cxx",
|
||||
["*.cpp", "*.cc", "*.cxx"],
|
||||
["*.o"],
|
||||
"-c -o $out $in -MD -MF $out.d $flags $cincs $cdefs",
|
||||
["-std=gnu++2b", "-Wall", "-Wextra", "-Werror", "-fno-exceptions", "-fno-rtti"],
|
||||
"$out.d",
|
||||
),
|
||||
"as": Rule("as", ["*.s", "*.asm", "*.S"], ["*.o"], "-o $out $in $flags"),
|
||||
"ar": Rule("ar", ["*.o"], ["*.a"], "$flags $out $in"),
|
||||
"ld": Rule("ld", ["*.o", "*.a"], ["*.out"], "-o $out $in $flags"),
|
||||
|
|
|
@ -13,13 +13,15 @@ import tempfile
|
|||
|
||||
|
||||
from typing import Optional
|
||||
from cutekit import const
|
||||
from . import const
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Uname:
|
||||
def __init__(self, sysname: str, nodename: str, release: str, version: str, machine: str):
|
||||
def __init__(
|
||||
self, sysname: str, nodename: str, release: str, version: str, machine: str
|
||||
):
|
||||
self.sysname = sysname
|
||||
self.nodename = nodename
|
||||
self.release = release
|
||||
|
@ -47,8 +49,10 @@ def sha256sum(path: str) -> str:
|
|||
return hashlib.sha256(f.read()).hexdigest()
|
||||
|
||||
|
||||
def find(path: str | list[str], wildcards: list[str] = [], recusive: bool = True) -> list[str]:
|
||||
logger.info(f"Looking for files in {path} matching {wildcards}")
|
||||
def find(
|
||||
path: str | list[str], wildcards: list[str] = [], recusive: bool = True
|
||||
) -> list[str]:
|
||||
_logger.info(f"Looking for files in {path} matching {wildcards}")
|
||||
|
||||
result: list[str] = []
|
||||
|
||||
|
@ -84,7 +88,7 @@ def find(path: str | list[str], wildcards: list[str] = [], recusive: bool = True
|
|||
|
||||
|
||||
def mkdir(path: str) -> str:
|
||||
logger.info(f"Creating directory {path}")
|
||||
_logger.info(f"Creating directory {path}")
|
||||
|
||||
try:
|
||||
os.makedirs(path)
|
||||
|
@ -95,7 +99,7 @@ def mkdir(path: str) -> str:
|
|||
|
||||
|
||||
def rmrf(path: str) -> bool:
|
||||
logger.info(f"Removing directory {path}")
|
||||
_logger.info(f"Removing directory {path}")
|
||||
|
||||
if not os.path.exists(path):
|
||||
return False
|
||||
|
@ -108,18 +112,18 @@ def wget(url: str, path: Optional[str] = None) -> str:
|
|||
|
||||
if path is None:
|
||||
path = os.path.join(
|
||||
const.CACHE_DIR,
|
||||
hashlib.sha256(url.encode('utf-8')).hexdigest())
|
||||
const.CACHE_DIR, hashlib.sha256(url.encode("utf-8")).hexdigest()
|
||||
)
|
||||
|
||||
if os.path.exists(path):
|
||||
return path
|
||||
|
||||
logger.info(f"Downloading {url} to {path}")
|
||||
_logger.info(f"Downloading {url} to {path}")
|
||||
|
||||
r = requests.get(url, stream=True)
|
||||
r.raise_for_status()
|
||||
mkdir(os.path.dirname(path))
|
||||
with open(path, 'wb') as f:
|
||||
with open(path, "wb") as f:
|
||||
for chunk in r.iter_content(chunk_size=8192):
|
||||
if chunk:
|
||||
f.write(chunk)
|
||||
|
@ -128,17 +132,20 @@ def wget(url: str, path: Optional[str] = None) -> str:
|
|||
|
||||
|
||||
def exec(*args: str, quiet: bool = False) -> bool:
|
||||
logger.info(f"Executing {args}")
|
||||
_logger.info(f"Executing {args}")
|
||||
|
||||
try:
|
||||
proc = subprocess.run(
|
||||
args, stdout=sys.stdout if not quiet else subprocess.PIPE, stderr=sys.stderr if not quiet else subprocess.PIPE)
|
||||
args,
|
||||
stdout=sys.stdout if not quiet else subprocess.PIPE,
|
||||
stderr=sys.stderr if not quiet else subprocess.PIPE,
|
||||
)
|
||||
|
||||
if proc.stdout:
|
||||
logger.info(proc.stdout.decode('utf-8'))
|
||||
_logger.info(proc.stdout.decode("utf-8"))
|
||||
|
||||
if proc.stderr:
|
||||
logger.error(proc.stderr.decode('utf-8'))
|
||||
_logger.error(proc.stderr.decode("utf-8"))
|
||||
|
||||
except FileNotFoundError:
|
||||
raise RuntimeError(f"{args[0]}: Command not found")
|
||||
|
@ -150,14 +157,13 @@ def exec(*args: str, quiet: bool = False) -> bool:
|
|||
raise RuntimeError(f"{args[0]}: Segmentation fault")
|
||||
|
||||
if proc.returncode != 0:
|
||||
raise RuntimeError(
|
||||
f"{args[0]}: Process exited with code {proc.returncode}")
|
||||
raise RuntimeError(f"{args[0]}: Process exited with code {proc.returncode}")
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def popen(*args: str) -> str:
|
||||
logger.info(f"Executing {args}")
|
||||
_logger.info(f"Executing {args}")
|
||||
|
||||
try:
|
||||
proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=sys.stderr)
|
||||
|
@ -168,14 +174,13 @@ def popen(*args: str) -> str:
|
|||
raise RuntimeError(f"{args[0]}: Segmentation fault")
|
||||
|
||||
if proc.returncode != 0:
|
||||
raise RuntimeError(
|
||||
f"{args[0]}: Process exited with code {proc.returncode}")
|
||||
raise RuntimeError(f"{args[0]}: Process exited with code {proc.returncode}")
|
||||
|
||||
return proc.stdout.decode('utf-8')
|
||||
return proc.stdout.decode("utf-8")
|
||||
|
||||
|
||||
def readdir(path: str) -> list[str]:
|
||||
logger.info(f"Reading directory {path}")
|
||||
_logger.info(f"Reading directory {path}")
|
||||
|
||||
try:
|
||||
return os.listdir(path)
|
||||
|
@ -184,19 +189,19 @@ def readdir(path: str) -> list[str]:
|
|||
|
||||
|
||||
def cp(src: str, dst: str):
|
||||
logger.info(f"Copying {src} to {dst}")
|
||||
_logger.info(f"Copying {src} to {dst}")
|
||||
|
||||
shutil.copy(src, dst)
|
||||
|
||||
|
||||
def mv(src: str, dst: str):
|
||||
logger.info(f"Moving {src} to {dst}")
|
||||
_logger.info(f"Moving {src} to {dst}")
|
||||
|
||||
shutil.move(src, dst)
|
||||
|
||||
|
||||
def cpTree(src: str, dst: str):
|
||||
logger.info(f"Copying {src} to {dst}")
|
||||
_logger.info(f"Copying {src} to {dst}")
|
||||
|
||||
shutil.copytree(src, dst, dirs_exist_ok=True)
|
||||
|
||||
|
@ -204,10 +209,14 @@ def cpTree(src: str, dst: str):
|
|||
def cloneDir(url: str, path: str, dest: str) -> str:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
mkdir(tmp)
|
||||
exec(*["git", "clone", "-n", "--depth=1",
|
||||
"--filter=tree:0", url, tmp, "-q"], quiet=True)
|
||||
exec(*["git", "-C", tmp, "sparse-checkout",
|
||||
"set", "--no-cone", path, "-q"], quiet=True)
|
||||
exec(
|
||||
*["git", "clone", "-n", "--depth=1", "--filter=tree:0", url, tmp, "-q"],
|
||||
quiet=True,
|
||||
)
|
||||
exec(
|
||||
*["git", "-C", tmp, "sparse-checkout", "set", "--no-cone", path, "-q"],
|
||||
quiet=True,
|
||||
)
|
||||
exec(*["git", "-C", tmp, "checkout", "-q", "--no-progress"], quiet=True)
|
||||
mv(os.path.join(tmp, path), dest)
|
||||
|
||||
|
@ -232,7 +241,7 @@ def latest(cmd: str) -> str:
|
|||
if cmd in LATEST_CACHE:
|
||||
return LATEST_CACHE[cmd]
|
||||
|
||||
logger.info(f"Finding latest version of {cmd}")
|
||||
_logger.info(f"Finding latest version of {cmd}")
|
||||
|
||||
regex: re.Pattern[str]
|
||||
|
||||
|
@ -254,14 +263,14 @@ def latest(cmd: str) -> str:
|
|||
versions.sort()
|
||||
chosen = versions[-1]
|
||||
|
||||
logger.info(f"Chosen {chosen} as latest version of {cmd}")
|
||||
_logger.info(f"Chosen {chosen} as latest version of {cmd}")
|
||||
|
||||
LATEST_CACHE[cmd] = chosen
|
||||
|
||||
return chosen
|
||||
|
||||
|
||||
def which(cmd: str) -> str | None:
|
||||
def which(cmd: str) -> Optional[str]:
|
||||
"""
|
||||
Find the path of a command
|
||||
"""
|
||||
|
|
|
@ -3,19 +3,21 @@ from typing import Any, TypeVar, cast, Optional, Union
|
|||
import json
|
||||
import hashlib
|
||||
|
||||
T = TypeVar('T')
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
def uniq(l: list[str]) -> list[str]:
|
||||
result: list[str] = []
|
||||
for i in l:
|
||||
def uniq(lst: list[T]) -> list[T]:
|
||||
result: list[T] = []
|
||||
for i in lst:
|
||||
if i in result:
|
||||
result.remove(i)
|
||||
result.append(i)
|
||||
return result
|
||||
|
||||
|
||||
def hash(obj: Any, keys: list[str] = [], cls: Optional[type[json.JSONEncoder]] = None) -> str:
|
||||
def hash(
|
||||
obj: Any, keys: list[str] = [], cls: Optional[type[json.JSONEncoder]] = None
|
||||
) -> str:
|
||||
toHash = {}
|
||||
if len(keys) == 0:
|
||||
toHash = obj
|
||||
|
@ -28,7 +30,7 @@ def hash(obj: Any, keys: list[str] = [], cls: Optional[type[json.JSONEncoder]] =
|
|||
|
||||
|
||||
def camelCase(s: str) -> str:
|
||||
s = ''.join(x for x in s.title() if x != '_' and x != '-')
|
||||
s = "".join(x for x in s.title() if x != "_" and x != "-")
|
||||
s = s[0].lower() + s[1:]
|
||||
return s
|
||||
|
||||
|
@ -60,4 +62,6 @@ def asList(i: Optional[Union[T, list[T]]]) -> list[T]:
|
|||
|
||||
|
||||
def isNewer(path1: str, path2: str) -> bool:
|
||||
return not os.path.exists(path2) or os.path.getmtime(path1) > os.path.getmtime(path2)
|
||||
return not os.path.exists(path2) or os.path.getmtime(path1) > os.path.getmtime(
|
||||
path2
|
||||
)
|
||||
|
|
|
@ -28,10 +28,6 @@ CROSSED = "\033[9m"
|
|||
RESET = "\033[0m"
|
||||
|
||||
|
||||
def title(text: str):
|
||||
print(f"{BOLD}{text}{RESET}:")
|
||||
|
||||
|
||||
def wordwrap(text: str, width: int = 60, newline: str = "\n") -> str:
|
||||
result = ""
|
||||
curr = 0
|
||||
|
@ -49,3 +45,11 @@ def wordwrap(text: str, width: int = 60, newline: str = "\n") -> str:
|
|||
|
||||
def indent(text: str, indent: int = 4) -> str:
|
||||
return " " * indent + text.replace("\n", "\n" + " " * indent)
|
||||
|
||||
|
||||
def title(text: str):
|
||||
print(f"{BOLD}{text}{RESET}:")
|
||||
|
||||
|
||||
def p(text: str):
|
||||
return indent(wordwrap(text))
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
|
||||
# Extending cutekit
|
||||
# Extending cutekit
|
||||
|
||||
By writing custom Python plugins, you can extend Cutekit to do whatever you want.
|
||||
|
||||
|
@ -9,24 +8,11 @@ Then you can import cutekit and change/add whatever you want.
|
|||
For example you can add a new command to the CLI:
|
||||
|
||||
```python
|
||||
import os
|
||||
import json
|
||||
import magic
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from cutekit import cli
|
||||
|
||||
|
||||
from cutekit import shell, builder, const, project
|
||||
from cutekit.cmds import Cmd, append
|
||||
from cutekit.args import Args
|
||||
from typing import Callable
|
||||
|
||||
|
||||
def bootCmd(args: Args) -> None:
|
||||
project.chdir()
|
||||
@cli.command("h", "hello", "Print hello world")
|
||||
def bootCmd(args: cli.Args) -> None:
|
||||
print("Hello world!")
|
||||
|
||||
append(Cmd("h", "hello", "Print hello world", bootCmd))
|
||||
```
|
||||
|
||||
This feature is used - for example - by [SkiftOS](https://github.com/skift-org/skift/blob/main/meta/plugins/start-cmd.py) to add the `start` command, that build packages and run a virtual machine.
|
||||
|
|
40
doc/mit.svg
Normal file
40
doc/mit.svg
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg width="93.217mm" height="69.497mm" version="1.1" viewBox="0 0 93.217 69.497" xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/"
|
||||
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
|
||||
<metadata>
|
||||
<rdf:RDF>
|
||||
<cc:Work rdf:about="">
|
||||
<dc:format>image/svg+xml</dc:format>
|
||||
<dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
|
||||
<dc:title />
|
||||
</cc:Work>
|
||||
</rdf:RDF>
|
||||
</metadata>
|
||||
<g transform="translate(0 19.497)">
|
||||
<rect y="-19.497" width="93.217" height="69.497" fill="#a00" />
|
||||
<rect x="5.6831" y="-14.441" width="8.8" height="40" ry="0" fill="#fff" />
|
||||
<rect x="5.6332" y="21.559" width="4.45" height="4.05" fill="#a00" />
|
||||
<rect x="10.083" y="17.559" width="4.45" height="4" fill="#a00" />
|
||||
<rect x="5.6332" y="13.559" width="4.45" height="4" fill="#a00" />
|
||||
<rect x="10.083" y="9.5594" width="4.45" height="4" fill="#a00" />
|
||||
<rect x="5.6834" y="-14.441" width="35.2" height="8.8" fill="#fff" />
|
||||
<rect x="32.083" y="-14.441" width="8.8" height="40" ry="0" fill="#fff" />
|
||||
<rect x="36.483" y="21.559" width="4.45" height="4.05" fill="#a00" />
|
||||
<rect x="32.033" y="17.559" width="4.45" height="4" fill="#a00" />
|
||||
<rect x="36.483" y="13.559" width="4.45" height="4" fill="#a00" />
|
||||
<rect x="32.033" y="9.5594" width="4.45" height="4" fill="#a00" />
|
||||
<rect x="20.083" y="-14.441" width="6.6202" height="40" ry="0" fill="#fff" />
|
||||
<rect x="46.083" y="-14.441" width="8.8" height="40" ry="0" fill="#fff" />
|
||||
<rect x="70.083" y="-14.441" width="8.8" height="40" ry="0" fill="#fff" />
|
||||
<rect x="60.283" y="-14.441" width="28.4" height="8.8" fill="#fff" />
|
||||
<rect x="70.033" y="21.559" width="4.45" height="4.05" fill="#a00" />
|
||||
<rect x="74.483" y="17.559" width="4.45" height="4" fill="#a00" />
|
||||
<rect x="70.033" y="13.559" width="4.45" height="4" fill="#a00" />
|
||||
<rect x="74.483" y="9.5594" width="4.45" height="4" fill="#a00" />
|
||||
<text x="12.171427" y="44.462078" fill="#ffffff" font-family="'Liberation Sans'" font-size="11.289px"
|
||||
letter-spacing="0px" stroke-width="1px" word-spacing="0px" style="line-height:125%" xml:space="preserve">
|
||||
<tspan x="12.171427" y="44.462078" font-family="'Liberation Sans'" font-size="19.756px">License</tspan>
|
||||
</text>
|
||||
</g>
|
||||
</svg>
|
After Width: | Height: | Size: 2.5 KiB |
|
@ -12,10 +12,9 @@ authors = [
|
|||
{ name = "Cute Engineering", email = "contact@cute.engineering" },
|
||||
]
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
requires-python = ">=3.11"
|
||||
license = { text = "MIT" }
|
||||
dependencies = ["requests ~= 2.28.0", "graphviz ~= 0.20.1"]
|
||||
dynamic = ["version"]
|
||||
dynamic = ["version", "dependencies"]
|
||||
|
||||
[project.scripts]
|
||||
ck = "cutekit:main"
|
||||
|
@ -27,6 +26,7 @@ packages = ["cutekit"]
|
|||
|
||||
[tool.setuptools.dynamic]
|
||||
version = { attr = "cutekit.const.VERSION" }
|
||||
dependencies = { file = ["requirements.txt"] }
|
||||
|
||||
[tool.setuptools.package-data]
|
||||
"cutekit" = ["py.typed"]
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
requests ~= 2.28.0
|
||||
graphviz ~= 0.20.1
|
||||
requests ~= 2.31.0
|
||||
graphviz ~= 0.20.1
|
||||
dataclasses-json ~= 0.6.2
|
||||
|
|
90
tests/test_resolver.py
Normal file
90
tests/test_resolver.py
Normal file
|
@ -0,0 +1,90 @@
|
|||
from cutekit import model
|
||||
|
||||
|
||||
def test_direct_deps():
|
||||
r = model.Registry("")
|
||||
r._append(model.Component("myapp", requires=["mylib"]))
|
||||
r._append(model.Component("mylib"))
|
||||
t = model.Target("host")
|
||||
res = model.Resolver(r, t)
|
||||
|
||||
resolved = res.resolve("myapp")
|
||||
assert resolved.reason is None
|
||||
assert resolved.resolved == ["myapp", "mylib"]
|
||||
|
||||
|
||||
def test_indirect_deps():
|
||||
r = model.Registry("")
|
||||
r._append(model.Component("myapp", requires=["mylib"]))
|
||||
r._append(model.Component("mylib", requires=["myembed"]))
|
||||
r._append(model.Component("myimpl", provides=["myembed"]))
|
||||
t = model.Target("host")
|
||||
res = model.Resolver(r, t)
|
||||
assert res.resolve("myapp").resolved == ["myapp", "mylib", "myimpl"]
|
||||
|
||||
|
||||
def test_deps_routing():
|
||||
r = model.Registry("")
|
||||
r._append(model.Component("myapp", requires=["mylib"]))
|
||||
r._append(model.Component("mylib", requires=["myembed"]))
|
||||
r._append(model.Component("myimplA", provides=["myembed"]))
|
||||
r._append(model.Component("myimplB", provides=["myembed"]))
|
||||
t = model.Target("host", routing={"myembed": "myimplB"})
|
||||
res = model.Resolver(r, t)
|
||||
assert res.resolve("myapp").resolved == ["myapp", "mylib", "myimplB"]
|
||||
|
||||
t = model.Target("host", routing={"myembed": "myimplA"})
|
||||
res = model.Resolver(r, t)
|
||||
assert res.resolve("myapp").resolved == ["myapp", "mylib", "myimplA"]
|
||||
|
||||
t = model.Target("host", routing={"myembed": "myimplC"})
|
||||
res = model.Resolver(r, t)
|
||||
assert res.resolve("myapp").reason == "No provider for 'myembed'"
|
||||
|
||||
|
||||
def test_deps_routing_with_props():
|
||||
r = model.Registry("")
|
||||
r._append(model.Component("myapp", requires=["mylib"]))
|
||||
r._append(model.Component("mylib", requires=["myembed"]))
|
||||
r._append(
|
||||
model.Component("myimplA", provides=["myembed"], enableIf={"myprop": ["a"]})
|
||||
)
|
||||
r._append(
|
||||
model.Component("myimplB", provides=["myembed"], enableIf={"myprop": ["b"]})
|
||||
)
|
||||
t = model.Target("host", routing={"myembed": "myimplB"}, props={"myprop": "b"})
|
||||
res = model.Resolver(r, t)
|
||||
assert res.resolve("myapp").resolved == ["myapp", "mylib", "myimplB"]
|
||||
|
||||
t = model.Target("host", routing={"myembed": "myimplA"}, props={"myprop": "a"})
|
||||
res = model.Resolver(r, t)
|
||||
assert res.resolve("myapp").resolved == ["myapp", "mylib", "myimplA"]
|
||||
|
||||
t = model.Target("host", routing={"myembed": "myimplC"}, props={"myprop": "c"})
|
||||
res = model.Resolver(r, t)
|
||||
|
||||
resolved = res.resolve("myapp")
|
||||
assert resolved.reason == "No provider for 'myembed'"
|
||||
|
||||
|
||||
def test_deps_routing_with_props_and_requires():
|
||||
r = model.Registry("")
|
||||
r._append(model.Component("myapp", requires=["mylib"]))
|
||||
r._append(model.Component("mylib", requires=["myembed"]))
|
||||
r._append(
|
||||
model.Component("myimplA", provides=["myembed"], enableIf={"myprop": ["a"]})
|
||||
)
|
||||
r._append(
|
||||
model.Component("myimplB", provides=["myembed"], enableIf={"myprop": ["b"]})
|
||||
)
|
||||
t = model.Target("host", routing={"myembed": "myimplB"}, props={"myprop": "b"})
|
||||
res = model.Resolver(r, t)
|
||||
assert res.resolve("myapp").resolved == ["myapp", "mylib", "myimplB"]
|
||||
|
||||
t = model.Target("host", routing={"myembed": "myimplA"}, props={"myprop": "a"})
|
||||
res = model.Resolver(r, t)
|
||||
assert res.resolve("myapp").resolved == ["myapp", "mylib", "myimplA"]
|
||||
|
||||
t = model.Target("host", routing={"myembed": "myimplC"}, props={"myprop": "c"})
|
||||
res = model.Resolver(r, t)
|
||||
assert res.resolve("myapp").reason == "No provider for 'myembed'"
|
Loading…
Reference in a new issue