Skip to content

Commit

Permalink
Merge pull request #180 from davidgiven/ab
Browse files Browse the repository at this point in the history
Update ab.
  • Loading branch information
davidgiven authored Sep 30, 2024
2 parents b039f29 + dc02908 commit f7f1e6d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 209 deletions.
5 changes: 5 additions & 0 deletions build/_progress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sys

(_, current, max) = sys.argv
percent = int(100 * float(current) / float(max))
print(f"[{percent:>3}%]")
11 changes: 9 additions & 2 deletions build/ab.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CFLAGS ?= -g -Og
LDFLAGS ?= -g
PKG_CONFIG ?= pkg-config
ECHO ?= echo
CP ?= cp
TARGETS ?= +all

ifdef VERBOSE
Expand All @@ -28,6 +29,12 @@ ifeq ($(OS), Windows_NT)
endif
EXT ?=

ifeq ($(PROGRESSINFO),)
rulecount := $(shell $(MAKE) --no-print-directory -q $(OBJ)/build.mk PROGRESSINFO=1 && $(MAKE) -n $(MAKECMDGOALS) PROGRESSINFO=XXXPROGRESSINFOXXX | grep XXXPROGRESSINFOXXX | wc -l)
ruleindex := 1
PROGRESSINFO = "$(shell $(PYTHON) build/_progress.py $(ruleindex) $(rulecount))$(eval ruleindex := $(shell expr $(ruleindex) + 1))"
endif

include $(OBJ)/build.mk

MAKEFLAGS += -r -j$(shell nproc)
Expand All @@ -47,8 +54,8 @@ clean::

export PYTHONHASHSEED = 1
build-files = $(shell find . -name 'build.py') $(wildcard build/*.py) $(wildcard config.py)
$(OBJ)/build.mk: Makefile $(build-files)
$(OBJ)/build.mk: Makefile $(build-files) build/ab.mk
@echo "AB"
@mkdir -p $(OBJ)
$(hide) $(PYTHON) -X pycache_prefix=$(OBJ) build/ab.py -o $@ build.py \
$(hide) $(PYTHON) -X pycache_prefix=$(OBJ)/__pycache__ build/ab.py -o $@ build.py \
|| rm -f $@
53 changes: 37 additions & 16 deletions build/ab.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import inspect
import string
import sys
import hashlib

verbose = False
quiet = False
Expand Down Expand Up @@ -153,6 +154,9 @@ def __init__(self, cwd, name):
def __eq__(self, other):
return self.name is other.name

def __lt__(self, other):
return self.name < other.name

def __hash__(self):
return id(self)

Expand Down Expand Up @@ -313,7 +317,12 @@ def targetof(value, cwd=None):
# Load the new build file.

path = join(path, "build.py")
loadbuildfile(path)
try:
loadbuildfile(path)
except ModuleNotFoundError:
error(
f"no such build file '{path}' while trying to resolve '{value}'"
)
assert (
value in targets
), f"build file at '{path}' doesn't contain '+{target}' when trying to resolve '{value}'"
Expand Down Expand Up @@ -387,9 +396,12 @@ def filenameof(x):
return xs[0]


def emit(*args):
outputFp.write(" ".join(args))
outputFp.write("\n")
def emit(*args, into=None):
s = " ".join(args) + "\n"
if into is not None:
into += [s]
else:
outputFp.write(s)


def emit_rule(name, ins, outs, cmds=[], label=None):
Expand All @@ -398,26 +410,35 @@ def emit_rule(name, ins, outs, cmds=[], label=None):
nonobjs = [f for f in fouts if not f.startswith("$(OBJ)")]

emit("")

lines = []
if nonobjs:
emit("clean::")
emit("\t$(hide) rm -f", *nonobjs)
emit("clean::", into=lines)
emit("\t$(hide) rm -f", *nonobjs, into=lines)

emit(".PHONY:", name)
emit(".PHONY:", name, into=lines)
if outs:
emit(name, ":", *fouts)
if cmds:
emit(*fouts, "&:", *fins)
else:
emit(*fouts, ":", *fins)
emit(name, ":", *fouts, into=lines)
emit(*fouts, "&:" if len(fouts) > 1 else ":", *fins, "\x01", into=lines)

if label:
emit("\t$(hide)", "$(ECHO)", label)
emit("\t$(hide)", "$(ECHO) $(PROGRESSINFO) ", label, into=lines)
for c in cmds:
emit("\t$(hide)", c)
emit("\t$(hide)", c, into=lines)
else:
assert len(cmds) == 0, "rules with no outputs cannot have commands"
emit(name, ":", *fins)
emit(name, ":", *fins, into=lines)

cmd = "".join(lines)
hash = hashlib.sha1(bytes(cmd, "utf-8")).hexdigest()

outputFp.write(cmd.replace("\x01", f"$(OBJ)/.hashes/{hash}"))

if outs:
emit(f"$(OBJ)/.hashes/{hash}:")
emit(
f"\t$(hide) mkdir -p $(OBJ)/.hashes && touch $(OBJ)/.hashes/{hash}"
)
emit("")


Expand Down Expand Up @@ -476,7 +497,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) %s %s" % (srcs[0], destf)],
label="CP",
)
subrule.materialise()
Expand Down
2 changes: 1 addition & 1 deletion build/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def cheaders(
len(s) == 1
), "the target of a header must return exactly one file"

cs += ["cp {ins[" + str(i) + "]} {outs[" + str(i) + "]}"]
cs += ["$(CP) {ins[" + str(i) + "]} {outs[" + str(i) + "]}"]
outs += ["=" + dest]
i = i + 1

Expand Down
185 changes: 0 additions & 185 deletions build/java.py

This file was deleted.

11 changes: 6 additions & 5 deletions build/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
error,
simplerule,
)
from os.path import relpath, splitext, join, basename
from glob import glob
from os.path import relpath, splitext, join, basename, isfile
from glob import iglob
import fnmatch
import itertools

Expand All @@ -30,7 +30,7 @@ def collectattrs(*, targets, name, initial=[]):
s = set(initial)
for a in [t.args.get(name, []) for t in targets]:
s.update(a)
return list(s)
return sorted(list(s))


def itemsof(pattern, root=None, cwd=None):
Expand All @@ -43,9 +43,10 @@ def itemsof(pattern, root=None, cwd=None):
root = join(cwd, root)

result = {}
for f in glob(pattern, recursive=True):
for f in iglob(pattern, recursive=True):
try:
result[relpath(f, root)] = f
if isfile(f):
result[relpath(f, root)] = f
except ValueError:
error(f"file '{f}' is not in root '{root}'")
return result
Expand Down

0 comments on commit f7f1e6d

Please sign in to comment.