Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ab. Again. #790

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions build.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from build.ab import export
from build.c import clibrary, cxxlibrary
from build.protobuf import proto, protocc
from build.pkg import package, hostpackage
from build.pkg import package
from build.utils import test
from glob import glob
import config
Expand All @@ -12,11 +12,6 @@
package(name="fmt_lib", package="fmt", fallback="dep/fmt")
package(name="sqlite3_lib", package="sqlite3")

hostpackage(name="protobuf_host_lib", package="protobuf")
hostpackage(name="z_host_lib", package="zlib")
hostpackage(name="fmt_host_lib", package="fmt", fallback="dep/fmt")
hostpackage(name="sqlite3_host_lib", package="sqlite3")

clibrary(name="protocol", hdrs={"protocol.h": "./protocol.h"})

corpustests = []
Expand Down
6 changes: 5 additions & 1 deletion build/ab.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
ifeq ($(findstring 4.,$(MAKE_VERSION)),)
MAKENOT4 := $(if $(findstring 3.9999, $(lastword $(sort 3.9999 $(MAKE_VERSION)))),yes,no)
MAKE4.3 := $(if $(findstring 4.3, $(firstword $(sort 4.3 $(MAKE_VERSION)))),yes,no)
MAKE4.1 := $(if $(findstring no_no,$(MAKENOT4)_$(MAKE4.3)),yes,no)

ifeq ($(MAKENOT3),yes)
$(error You need GNU Make 4.x for this (if you're on OSX, use gmake).)
endif

Expand Down
13 changes: 11 additions & 2 deletions build/ab.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@

class PathFinderImpl(PathFinder):
def find_spec(self, fullname, path, target=None):
if not path:
# The second test here is needed for Python 3.9.
if not path or not path[0]:
path = ["."]
if len(path) != 1:
return None
Expand Down Expand Up @@ -420,7 +421,15 @@ def emit_rule(name, ins, outs, cmds=[], label=None):
emit(".PHONY:", name, into=lines)
if outs:
emit(name, ":", *fouts, into=lines)
emit(*fouts, "&:" if len(fouts) > 1 else ":", *fins, "\x01", into=lines)
if len(fouts) == 1:
emit(*fouts, ":", *fins, "\x01", into=lines)
else:
emit("ifeq ($(MAKE4.3),yes)", into=lines)
emit(*fouts, "&:", *fins, "\x01", into=lines)
emit("else", into=lines)
emit(*(fouts[1:]), ":", fouts[0], into=lines)
emit(fouts[0], ":", *fins, "\x01", into=lines)
emit("endif", into=lines)

if label:
emit("\t$(hide)", "$(ECHO) $(PROGRESSINFO)", label, into=lines)
Expand Down
143 changes: 42 additions & 101 deletions build/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,27 @@
emit(
"""
ifeq ($(OSX),no)
HOSTSTARTGROUP ?= -Wl,--start-group
HOSTENDGROUP ?= -Wl,--end-group
STARTGROUP ?= -Wl,--start-group
ENDGROUP ?= -Wl,--end-group
endif
STARTGROUP ?= $(HOSTSTARTGROUP)
ENDGROUP ?= $(HOSTENDGROUP)
"""
)


class Toolchain:
label = ""
cfile = ["$(CC) -c -o {outs[0]} {ins[0]} $(CFLAGS) {cflags}"]
cxxfile = ["$(CXX) -c -o {outs[0]} {ins[0]} $(CFLAGS) {cflags}"]
clibrary = ["rm -f {outs[0]} && $(AR) cqs {outs[0]} {ins}"]
cxxlibrary = ["rm -f {outs[0]} && $(AR) cqs {outs[0]} {ins}"]
cprogram = [
"$(CC) -o {outs[0]} $(STARTGROUP) {ins} {ldflags} $(LDFLAGS) $(ENDGROUP)"
]
cxxprogram = [
"$(CXX) -o {outs[0]} $(STARTGROUP) {ins} {ldflags} $(LDFLAGS) $(ENDGROUP)"
]


class HostToolchain:
label = "HOST "
cfile = ["$(HOSTCC) -c -o {outs[0]} {ins[0]} $(HOSTCFLAGS) {cflags}"]
cxxfile = ["$(HOSTCXX) -c -o {outs[0]} {ins[0]} $(HOSTCFLAGS) {cflags}"]
clibrary = ["rm -f {outs[0]} && $(HOSTAR) cqs {outs[0]} {ins}"]
cxxlibrary = ["rm -f {outs[0]} && $(HOSTAR) cqs {outs[0]} {ins}"]
cprogram = [
"$(HOSTCC) -o {outs[0]} $(HOSTSTARTGROUP) {ins} {ldflags} $(HOSTLDFLAGS) $(HOSTENDGROUP)"
]
cxxprogram = [
"$(HOSTCXX) -o {outs[0]} $(HOSTSTARTGROUP) {ins} {ldflags} $(HOSTLDFLAGS) $(HOSTENDGROUP)"
]

def _combine(list1, list2):
r = list(list1)
for i in list2:
if i not in r:
r.append(i)
return r

def _indirect(deps, name):
r = set()
r = []
for d in deps:
r.update(d.args.get(name, {d}))
r = _combine(r, d.args.get(name, [d]))
return r


def cfileimpl(self, name, srcs, deps, suffix, commands, label, kind, cflags):
def cfileimpl(self, name, srcs, deps, suffix, commands, label, cflags):
outleaf = "=" + stripext(basename(filenameof(srcs[0]))) + suffix

hdr_deps = _indirect(deps, "cheader_deps")
Expand All @@ -85,15 +61,10 @@ def cfile(
deps: Targets = None,
cflags=[],
suffix=".o",
toolchain=Toolchain,
commands=None,
label=None,
commands=["$(CC) -c -o {outs[0]} {ins[0]} $(CFLAGS) {cflags}"],
label="CC",
):
if not label:
label = toolchain.label + "CC"
if not commands:
commands = toolchain.cfile
cfileimpl(self, name, srcs, deps, suffix, commands, label, "cfile", cflags)
cfileimpl(self, name, srcs, deps, suffix, commands, label, cflags)


@Rule
Expand All @@ -104,20 +75,13 @@ def cxxfile(
deps: Targets = None,
cflags=[],
suffix=".o",
toolchain=Toolchain,
commands=None,
label=None,
commands=["$(CXX) -c -o {outs[0]} {ins[0]} $(CFLAGS) {cflags}"],
label="CXX",
):
if not label:
label = toolchain.label + "CXX"
if not commands:
commands = toolchain.cxxfile
cfileimpl(
self, name, srcs, deps, suffix, commands, label, "cxxfile", cflags
)
cfileimpl(self, name, srcs, deps, suffix, commands, label, cflags)


def findsources(name, srcs, deps, cflags, toolchain, filerule, cwd):
def findsources(name, srcs, deps, cflags, filerule, cwd):
for f in filenamesof(srcs):
if f.endswith(".h") or f.endswith(".hh"):
cflags = cflags + [f"-I{dirname(f)}"]
Expand All @@ -130,7 +94,6 @@ def findsources(name, srcs, deps, cflags, toolchain, filerule, cwd):
srcs=[f],
deps=deps,
cflags=sorted(set(cflags)),
toolchain=toolchain,
cwd=cwd,
)
for f in filenamesof([s])
Expand All @@ -156,13 +119,12 @@ def libraryimpl(
caller_ldflags,
cflags,
ldflags,
toolchain,
commands,
label,
kind,
filerule,
):
hdr_deps = _indirect(deps, "cheader_deps") | {self}
lib_deps = _indirect(deps, "clibrary_deps") | {self}
hdr_deps = _combine(_indirect(deps, "cheader_deps"), [self])
lib_deps = _combine(_indirect(deps, "clibrary_deps"), [self])

hr = None
hf = []
Expand Down Expand Up @@ -198,8 +160,7 @@ def libraryimpl(
srcs,
deps + ([hr] if hr else []),
cflags + hf,
toolchain,
kind,
filerule,
self.cwd,
)

Expand Down Expand Up @@ -233,15 +194,10 @@ def clibrary(
caller_ldflags=[],
cflags=[],
ldflags=[],
toolchain=Toolchain,
commands=None,
label=None,
commands=["rm -f {outs[0]} && $(AR) cqs {outs[0]} {ins}"],
label="LIB",
cfilerule=cfile,
):
if not label:
label = toolchain.label + "LIB"
if not commands:
commands = toolchain.clibrary
libraryimpl(
self,
name,
Expand All @@ -252,7 +208,6 @@ def clibrary(
caller_ldflags,
cflags,
ldflags,
toolchain,
commands,
label,
cfilerule,
Expand All @@ -270,15 +225,10 @@ def cxxlibrary(
caller_ldflags=[],
cflags=[],
ldflags=[],
toolchain=Toolchain,
commands=None,
label=None,
commands=["rm -f {outs[0]} && $(AR) cqs {outs[0]} {ins}"],
label="CXXLIB",
cxxfilerule=cxxfile,
):
if not label:
label = toolchain.label + "LIB"
if not commands:
commands = toolchain.cxxlibrary
libraryimpl(
self,
name,
Expand All @@ -289,7 +239,6 @@ def cxxlibrary(
caller_ldflags,
cflags,
ldflags,
toolchain,
commands,
label,
cxxfilerule,
Expand All @@ -303,20 +252,16 @@ def programimpl(
deps,
cflags,
ldflags,
toolchain,
commands,
label,
filerule,
kind,
):
cfiles = findsources(
self.localname, srcs, deps, cflags, toolchain, filerule, self.cwd
)
cfiles = findsources(self.localname, srcs, deps, cflags, filerule, self.cwd)

lib_deps = set()
lib_deps = []
for d in deps:
lib_deps.update(d.args.get("clibrary_deps", {d}))
libs = sorted(filenamesmatchingof(lib_deps, "*.a"))
lib_deps = _combine(lib_deps, d.args.get("clibrary_deps", {d}))
libs = filenamesmatchingof(lib_deps, "*.a")
ldflags = collectattrs(
targets=lib_deps, name="caller_ldflags", initial=ldflags
)
Expand All @@ -325,8 +270,8 @@ def programimpl(
replaces=self,
ins=cfiles + libs,
outs=[f"={self.localname}$(EXT)"],
deps=sorted(_indirect(lib_deps, "clibrary_files")),
label=toolchain.label + label,
deps=_indirect(lib_deps, "clibrary_files"),
label=label,
commands=commands,
args={
"ldflags": collectattrs(
Expand All @@ -344,24 +289,22 @@ def cprogram(
deps: Targets = None,
cflags=[],
ldflags=[],
toolchain=Toolchain,
commands=None,
commands=[
"$(CC) -o {outs[0]} $(STARTGROUP) {ins} {ldflags} $(LDFLAGS) $(ENDGROUP)"
],
label="CLINK",
cfilerule=cfile,
):
if not commands:
commands = toolchain.cprogram
programimpl(
self,
name,
srcs,
deps,
cflags,
ldflags,
toolchain,
commands,
label,
cfile,
"cprogram",
cfilerule,
)


Expand All @@ -373,22 +316,20 @@ def cxxprogram(
deps: Targets = None,
cflags=[],
ldflags=[],
toolchain=Toolchain,
commands=None,
commands=[
"$(CXX) -o {outs[0]} $(STARTGROUP) {ins} {ldflags} $(LDFLAGS) $(ENDGROUP)"
],
label="CXXLINK",
cxxfilerule=cxxfile,
):
if not commands:
commands = toolchain.cxxprogram
programimpl(
self,
name,
srcs,
deps,
cflags,
ldflags,
toolchain,
commands,
label,
cxxfile,
"cxxprogram",
cxxfilerule,
)
6 changes: 0 additions & 6 deletions build/pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def get_property(self, name, flag):


TargetPkgConfig = _PkgConfig(os.getenv("PKG_CONFIG"))
HostPkgConfig = _PkgConfig(os.getenv("HOST_PKG_CONFIG"))


def _package(self, name, package, fallback, pkgconfig):
Expand Down Expand Up @@ -70,8 +69,3 @@ def _package(self, name, package, fallback, pkgconfig):
@Rule
def package(self, name, package=None, fallback: Target = None):
_package(self, name, package, fallback, TargetPkgConfig)


@Rule
def hostpackage(self, name, package=None, fallback: Target = None):
_package(self, name, package, fallback, HostPkgConfig)
2 changes: 1 addition & 1 deletion dep/fmt/build.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from build.c import cxxlibrary, HostToolchain
from build.c import cxxlibrary

cxxlibrary(
name="fmt",
Expand Down
2 changes: 1 addition & 1 deletion scripts/build.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from build.ab import Rule, simplerule, Targets, TargetsMap
from build.c import cxxprogram, HostToolchain
from build.c import cxxprogram

encoders = {}

Expand Down
Loading