Skip to content

Commit

Permalink
Update ab.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgiven committed Oct 19, 2024
1 parent c8e9f6c commit afa4e8c
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 176 deletions.
43 changes: 40 additions & 3 deletions build/ab.mk
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ AR ?= ar
CFLAGS ?= -g -Og
LDFLAGS ?= -g
PKG_CONFIG ?= pkg-config
HOST_PKG_CONFIG ?= $(PKG_CONFIG)
ECHO ?= echo
TARGETS ?= +all
CP ?= cp

export PKG_CONFIG
export HOST_PKG_CONFIG

ifdef VERBOSE
hide =
Expand All @@ -23,11 +27,44 @@ else
endif
endif

WINDOWS := no
OSX := no
LINUX := no
ifeq ($(OS),Windows_NT)
WINDOWS := yes
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
LINUX := yes
endif
ifeq ($(UNAME_S),Darwin)
OSX := yes
endif
endif

ifeq ($(OS), Windows_NT)
EXT ?= .exe
endif
EXT ?=

ifeq ($(PROGRESSINFO),)
# The first make invocation here has to have its output discarded or else it
# produces spurious 'Leaving directory' messages... don't know why.
rulecount := $(strip $(shell $(MAKE) --no-print-directory -q $(OBJ)/build.mk PROGRESSINFO=1 > /dev/null \
&& $(MAKE) --no-print-directory -n $(MAKECMDGOALS) PROGRESSINFO=XXXPROGRESSINFOXXX | grep XXXPROGRESSINFOXXX | wc -l))
ruleindex := 1
PROGRESSINFO = "[$(ruleindex)/$(rulecount)]$(eval ruleindex := $(shell expr $(ruleindex) + 1))"
endif

PKG_CONFIG_HASHES = $(OBJ)/.pkg-config-hashes/target-$(word 1, $(shell $(PKG_CONFIG) --list-all | md5sum))
HOST_PKG_CONFIG_HASHES = $(OBJ)/.pkg-config-hashes/host-$(word 1, $(shell $(HOST_PKG_CONFIG) --list-all | md5sum))

$(OBJ)/build.mk : $(PKG_CONFIG_HASHES) $(HOST_PKG_CONFIG_HASHES)
$(PKG_CONFIG_HASHES) $(HOST_PKG_CONFIG_HASHES) &:
$(hide) rm -rf $(OBJ)/.pkg-config-hashes
$(hide) mkdir -p $(OBJ)/.pkg-config-hashes
$(hide) touch $(PKG_CONFIG_HASHES) $(HOST_PKG_CONFIG_HASHES)

include $(OBJ)/build.mk

MAKEFLAGS += -r -j$(shell nproc)
Expand All @@ -47,8 +84,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 $@
58 changes: 40 additions & 18 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 @@ -147,12 +148,16 @@ def __init__(self, cwd, name):
self.dir = join("$(OBJ)", name)
self.ins = []
self.outs = []
self.deps = []
self.materialised = False
self.args = {}

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 +318,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 +397,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 +411,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 @@ -451,7 +473,7 @@ def simplerule(
name=self.name,
ins=ins + deps,
outs=outs,
label=self.templateexpand("{label} {name}"),
label=self.templateexpand("{label} {name}") if label else None,
cmds=cs,
)

Expand All @@ -476,8 +498,8 @@ def export(self, name=None, items: TargetsMap = {}, deps: Targets = []):
cwd=self.cwd,
ins=[srcs[0]],
outs=[destf],
commands=["cp %s %s" % (srcs[0], destf)],
label="CP",
commands=["$(CP) %s %s" % (srcs[0], destf)],
label="",
)
subrule.materialise()

Expand Down
Loading

0 comments on commit afa4e8c

Please sign in to comment.