Update ab.

This commit is contained in:
David Given
2025-10-31 01:19:53 +01:00
parent aa49e094f1
commit 121cdefd09
4 changed files with 42 additions and 11 deletions

View File

@@ -117,6 +117,7 @@ $(OBJ)/build.ninja $(OBJ)/build.targets &:
-o $(OBJ) build.py \
-v $(OBJ)/vars.txt \
|| (rm -f $@ && false)
$(hide) cp $(OBJ)/compile_commands.json compile_commands.json
include $(OBJ)/build.targets
.PHONY: $(ninja-targets)

View File

@@ -12,6 +12,7 @@ import hashlib
import importlib
import importlib.util
import inspect
import json
import os
import re
import string
@@ -27,6 +28,7 @@ unmaterialisedTargets = {} # dict, not set, to get consistent ordering
materialisingStack = []
defaultGlobals = {}
outputTargets = set()
commandsDb = []
RE_FORMAT_SPEC = re.compile(
r"(?:(?P<fill>[\s\S])?(?P<align>[<>=^]))?"
@@ -540,6 +542,15 @@ def shell(*args):
shellFp.write(s)
def add_commanddb_entry(commands, file):
global commandsDb
commandsDb += [{
"directory": os.getcwd(),
"command": (" && ".join(commands)),
"file": file
}
]
def emit_rule(self, ins, outs, cmds=[], label=None):
name = self.name
fins = [self.templateexpand(f) for f in set(filenamesof(ins))]
@@ -562,7 +573,10 @@ def emit_rule(self, ins, outs, cmds=[], label=None):
sandbox = join(self.dir, "sandbox")
emit(f"rm -rf {sandbox}", into=rule)
emit(
f"{G.PYTHON} build/_sandbox.py --link -s", sandbox, *fins, into=rule
f"{G.PYTHON} build/_sandbox.py --link -s",
sandbox,
*fins,
into=rule,
)
for c in cmds:
emit(f"(cd {sandbox} &&", c, ")", into=rule)
@@ -596,7 +610,6 @@ def emit_rule(self, ins, outs, cmds=[], label=None):
if label:
emit(" description=", label)
emit("build", name, ":phony", *fouts)
else:
assert len(cmds) == 0, "rules with no outputs cannot have commands"
emit("build", name, ":phony", *fins)
@@ -612,6 +625,7 @@ def simplerule(
outs: Targets = [],
deps: Targets = [],
commands=[],
add_to_commanddb=False,
label="RULE",
):
self.ins = ins
@@ -627,8 +641,22 @@ def simplerule(
cs = [("mkdir -p %s" % dir) for dir in dirs]
coreCommands = []
for c in commands:
cs += [self.templateexpand(c)]
coreCommands += [self.templateexpand(c)]
cs += coreCommands
if add_to_commanddb:
infiles = filenamesof(ins)
if len(infiles) > 0:
global commandsDb
commandsDb += [
{
"directory": os.getcwd(),
"command": (" && ".join(coreCommands)),
"file": infiles[0],
}
]
emit_rule(
self=self,
@@ -702,7 +730,7 @@ def main():
G.setdefault(name.strip(), value.strip())
G.setdefault("AB_SANDBOX", "yes")
global ninjaFp, shellFp, outputdir
global ninjaFp, shellFp, jsonFp, outputdir
outputdir = args.outputdir
G.setdefault("OBJ", outputdir)
ninjaFp = open(outputdir + "/build.ninja", "wt")
@@ -726,5 +754,8 @@ def main():
fp.write("ninja-targets =")
fp.write(substituteGlobalVariables(" ".join(outputTargets)))
with open(outputdir + "/compile_commands.json", "wt") as fp:
json.dump(commandsDb, fp)
main()

View File

@@ -6,7 +6,7 @@ from build.ab import (
filenamesof,
flatten,
simplerule,
emit,
add_commanddb_entry,
G,
)
from build.utils import stripext, collectattrs
@@ -116,6 +116,7 @@ def cfileimpl(self, name, srcs, deps, suffix, commands, label, toolchain, cflags
outs=[outleaf],
label=label,
commands=commands,
add_to_commanddb=True,
args={"cflags": cflags},
)

View File

@@ -4,13 +4,11 @@ from os.path import join, abspath, dirname, relpath
from build.pkg import has_package
G.setdefault("PROTOC", "protoc")
G.setdefault("PROTOC_SEPARATOR", ":")
G.setdefault("HOSTPROTOC", "hostprotoc")
assert has_package("protobuf"), "required package 'protobuf' not installed"
def _getprotodeps(deps):
r = set()
for d in deps:
@@ -21,7 +19,7 @@ def _getprotodeps(deps):
@Rule
def proto(self, name, srcs: Targets = [], deps: Targets = []):
protodeps = _getprotodeps(deps)
descriptorlist = (G.PROTOC_SEPARATOR).join(
descriptorlist = ":".join(
[
relpath(f, start=self.dir)
for f in filenamesmatchingof(protodeps, "*.descriptor")
@@ -48,7 +46,7 @@ def proto(self, name, srcs: Targets = [], deps: Targets = []):
f"--descriptor_set_out={self.localname}.descriptor",
]
+ (
[f"--descriptor_set_in='{descriptorlist}'"]
[f"--descriptor_set_in={descriptorlist}"]
if descriptorlist
else []
)
@@ -91,7 +89,7 @@ def protocc(self, name, srcs: Targets = [], deps: Targets = []):
outs += ["=" + cc, "=" + h]
protodeps = _getprotodeps(deps + srcs)
descriptorlist = G.PROTOC_SEPARATOR.join(
descriptorlist = ":".join(
[
relpath(f, start=self.dir)
for f in filenamesmatchingof(protodeps, "*.descriptor")
@@ -112,7 +110,7 @@ def protocc(self, name, srcs: Targets = [], deps: Targets = []):
"$(PROTOC)",
"--proto_path=.",
"--cpp_out=.",
f"--descriptor_set_in='{descriptorlist}'",
f"--descriptor_set_in={descriptorlist}",
]
+ protos
)