Make build with the new ab --- but the tests fail.

This commit is contained in:
David Given
2025-03-17 22:33:54 +01:00
parent 0f763fe06b
commit 15a69f6dcb
57 changed files with 220 additions and 148 deletions

View File

@@ -17,6 +17,9 @@ import inspect
import string
import sys
import hashlib
import re
import ast
from collections import namedtuple
verbose = False
quiet = False
@@ -26,6 +29,22 @@ unmaterialisedTargets = {} # dict, not set, to get consistent ordering
materialisingStack = []
defaultGlobals = {}
RE_FORMAT_SPEC = re.compile(
r"(?:(?P<fill>[\s\S])?(?P<align>[<>=^]))?"
r"(?P<sign>[- +])?"
r"(?P<pos_zero>z)?"
r"(?P<alt>#)?"
r"(?P<zero_padding>0)?"
r"(?P<width_str>\d+)?"
r"(?P<grouping>[_,])?"
r"(?:(?P<decimal>\.)(?P<precision_str>\d+))?"
r"(?P<type>[bcdeEfFgGnosxX%])?"
)
CommandFormatSpec = namedtuple(
"CommandFormatSpec", RE_FORMAT_SPEC.groupindex.keys()
)
sys.path += ["."]
old_import = builtins.__import__
@@ -80,6 +99,29 @@ def error(message):
raise ABException(message)
class BracketedFormatter(string.Formatter):
def parse(self, format_string):
while format_string:
left, *right = format_string.split("$[", 1)
if not right:
yield (left, None, None, None)
break
right = right[0]
offset = len(right) + 1
try:
ast.parse(right)
except SyntaxError as e:
if not str(e).startswith("unmatched ']'"):
raise e
offset = e.offset
expr = right[0 : offset - 1]
format_string = right[offset:]
yield (left if left else None, expr, None, None)
def Rule(func):
sig = inspect.signature(func)
@@ -115,7 +157,7 @@ def Rule(func):
t.callback = func
t.traits.add(func.__name__)
if "args" in kwargs:
t.args |= kwargs["args"]
t.args.update(kwargs["args"])
del kwargs["args"]
if "traits" in kwargs:
t.traits |= kwargs["traits"]
@@ -166,7 +208,7 @@ class Target:
return f"Target('{self.name}')"
def templateexpand(selfi, s):
class Formatter(string.Formatter):
class Formatter(BracketedFormatter):
def get_field(self, name, a1, a2):
return (
eval(name, selfi.callback.__globals__, selfi.args),
@@ -355,8 +397,16 @@ class TargetsMap:
return output
def _removesuffix(self, suffix):
# suffix='' should not call self[:-0].
if suffix and self.endswith(suffix):
return self[: -len(suffix)]
else:
return self[:]
def loadbuildfile(filename):
filename = filename.replace("/", ".").removesuffix(".py")
filename = _removesuffix(filename.replace("/", "."), ".py")
builtins.__import__(filename)
@@ -406,8 +456,9 @@ def emit(*args, into=None):
outputFp.write(s)
def emit_rule(name, ins, outs, cmds=[], label=None):
fins = filenamesof(ins)
def emit_rule(self, ins, outs, cmds=[], label=None):
name = self.name
fins = set(filenamesof(ins))
fouts = filenamesof(outs)
nonobjs = [f for f in fouts if not f.startswith("$(OBJ)")]
@@ -433,8 +484,23 @@ def emit_rule(name, ins, outs, cmds=[], label=None):
if label:
emit("\t$(hide)", "$(ECHO) $(PROGRESSINFO)", label, into=lines)
sandbox = join(self.dir, "sandbox")
emit("\t$(hide)", f"rm -rf {sandbox}", into=lines)
emit(
"\t$(hide)",
f"$(PYTHON) build/_sandbox.py --link -s {sandbox}",
*fins,
into=lines,
)
for c in cmds:
emit("\t$(hide)", c, into=lines)
emit(f"\t$(hide) cd {sandbox} && (", c, ")", into=lines)
emit(
"\t$(hide)",
f"$(PYTHON) build/_sandbox.py --export -s {sandbox}",
*fouts,
into=lines,
)
else:
assert len(cmds) == 0, "rules with no outputs cannot have commands"
emit(name, ":", *fins, into=lines)
@@ -479,10 +545,10 @@ def simplerule(
cs += [self.templateexpand(c)]
emit_rule(
name=self.name,
self=self,
ins=ins + deps,
outs=outs,
label=self.templateexpand("{label} {name}") if label else None,
label=self.templateexpand("$[label] $[name]") if label else None,
cmds=cs,
)
@@ -507,7 +573,7 @@ def export(self, name=None, items: TargetsMap = {}, deps: Targets = []):
cwd=self.cwd,
ins=[srcs[0]],
outs=[destf],
commands=["$(CP) %s %s" % (srcs[0], destf)],
commands=["$(CP) -H %s %s" % (srcs[0], destf)],
label="",
)
subrule.materialise()
@@ -516,7 +582,7 @@ def export(self, name=None, items: TargetsMap = {}, deps: Targets = []):
replaces=self,
ins=outs + deps,
outs=["=sentinel"],
commands=["touch {outs[0]}"],
commands=["touch $[outs[0]]"],
label="EXPORT",
)