Skip to content

Commit 979a456

Browse files
committed
Update ab.
1 parent 3440792 commit 979a456

File tree

4 files changed

+83
-27
lines changed

4 files changed

+83
-27
lines changed

build/ab.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ def _filetarget(value, cwd):
388388
return t
389389

390390

391+
def getcwd():
392+
return cwdStack[-1]
393+
394+
391395
def targetof(value, cwd=None):
392396
if not cwd:
393397
cwd = cwdStack[-1]
@@ -544,12 +548,14 @@ def shell(*args):
544548

545549
def add_commanddb_entry(commands, file):
546550
global commandsDb
547-
commandsDb += [{
548-
"directory": os.getcwd(),
549-
"command": (" && ".join(commands)),
550-
"file": file
551-
}
552-
]
551+
commandsDb += [
552+
{
553+
"directory": os.getcwd(),
554+
"command": (" && ".join(commands)),
555+
"file": file,
556+
}
557+
]
558+
553559

554560
def emit_rule(self, ins, outs, cmds=[], label=None):
555561
name = self.name

build/protobuf.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
from build.utils import filenamesmatchingof, collectattrs
33
from os.path import join, abspath, dirname, relpath
44
from build.pkg import has_package
5-
import platform
65

76
G.setdefault("PROTOC", "protoc")
87
G.setdefault("HOSTPROTOC", "hostprotoc")
98

109
assert has_package("protobuf"), "required package 'protobuf' not installed"
1110

12-
PROTO_SEPARATOR = ";" if (platform.system() == "Windows") else ":"
1311

1412
def _getprotodeps(deps):
1513
r = set()
@@ -21,7 +19,7 @@ def _getprotodeps(deps):
2119
@Rule
2220
def proto(self, name, srcs: Targets = [], deps: Targets = []):
2321
protodeps = _getprotodeps(deps)
24-
descriptorlist = PROTO_SEPARATOR.join(
22+
descriptorlist = ":".join(
2523
[
2624
relpath(f, start=self.dir)
2725
for f in filenamesmatchingof(protodeps, "*.descriptor")
@@ -48,7 +46,7 @@ def proto(self, name, srcs: Targets = [], deps: Targets = []):
4846
f"--descriptor_set_out={self.localname}.descriptor",
4947
]
5048
+ (
51-
[f"--descriptor_set_in='{descriptorlist}'"]
49+
[f"--descriptor_set_in={descriptorlist}"]
5250
if descriptorlist
5351
else []
5452
)
@@ -91,7 +89,7 @@ def protocc(self, name, srcs: Targets = [], deps: Targets = []):
9189
outs += ["=" + cc, "=" + h]
9290

9391
protodeps = _getprotodeps(deps + srcs)
94-
descriptorlist = PROTO_SEPARATOR.join(
92+
descriptorlist = ":".join(
9593
[
9694
relpath(f, start=self.dir)
9795
for f in filenamesmatchingof(protodeps, "*.descriptor")
@@ -112,7 +110,7 @@ def protocc(self, name, srcs: Targets = [], deps: Targets = []):
112110
"$(PROTOC)",
113111
"--proto_path=.",
114112
"--cpp_out=.",
115-
f"--descriptor_set_in='{descriptorlist}'",
113+
f"--descriptor_set_in={descriptorlist}",
116114
]
117115
+ protos
118116
)
@@ -144,7 +142,7 @@ def protojava(self, name, srcs: Targets = [], deps: Targets = []):
144142
protos += [f]
145143
srcs += [f]
146144

147-
descriptorlist = PROTO_SEPARATOR.join(
145+
descriptorlist = ":".join(
148146
[abspath(f) for f in filenamesmatchingof(srcs + deps, "*.descriptor")]
149147
)
150148

@@ -163,7 +161,7 @@ def protojava(self, name, srcs: Targets = [], deps: Targets = []):
163161
"$(PROTOC)",
164162
"--proto_path=.",
165163
"--java_out=.",
166-
f"--descriptor_set_in='{descriptorlist}'",
164+
f"--descriptor_set_in={descriptorlist}",
167165
]
168166
+ protos
169167
)

build/utils.py

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44
Targets,
55
filenameof,
66
filenamesof,
7-
cwdStack,
7+
getcwd,
88
error,
99
simplerule,
10-
G
10+
G,
1111
)
12-
from os.path import relpath, splitext, join, basename, isfile
12+
from os.path import relpath, splitext, join, basename, isfile, normpath
13+
from os import walk
1314
from glob import iglob
1415
import fnmatch
1516
import subprocess
1617
import shutil
18+
import re
19+
import functools
1720

1821

1922
def filenamesmatchingof(xs, pattern):
@@ -35,9 +38,61 @@ def collectattrs(*, targets, name, initial=[]):
3538
return sorted(s)
3639

3740

41+
@functools.cache
42+
def _glob_to_re(glob_str):
43+
opts = re.compile('([.]|[*][*]/|[*]|[?])|(.)')
44+
out = ''
45+
for (pattern_match, literal_text) in opts.findall(glob_str):
46+
if pattern_match == '.':
47+
out += '[.]'
48+
elif pattern_match == '**/':
49+
out += '(?:.*/)?'
50+
elif pattern_match == '*':
51+
out += '[^/]*'
52+
elif pattern_match == '?':
53+
out += '.'
54+
elif literal_text:
55+
out += literal_text
56+
return re.compile(out)
57+
58+
def _glob_filter(paths, pattern):
59+
r = _glob_to_re(pattern)
60+
for f in paths:
61+
if r.match(f):
62+
yield f
63+
64+
def _glob_matches(path, pattern):
65+
r = _glob_to_re(pattern)
66+
return r.match(path)
67+
68+
def glob(include=["*"], exclude=[], dir=None, relative_to="."):
69+
if not dir:
70+
dir = getcwd()
71+
if dir.startswith("./"):
72+
dir = normpath(join(getcwd(), dir))
73+
if relative_to.startswith("./"):
74+
relative_to = normpath(join(getcwd(), relative_to))
75+
76+
def iterate():
77+
for dirpath, dirnames, filenames in walk(
78+
dir, topdown=True, followlinks=True
79+
):
80+
dirpath = relpath(dirpath, dir)
81+
filenames = [normpath(join(dirpath, f)) for f in filenames]
82+
matching = set()
83+
for p in include:
84+
matching.update(_glob_filter(filenames, p))
85+
for p in exclude:
86+
matching = [n for n in matching if not _glob_matches(n, p)]
87+
for f in matching:
88+
yield normpath(relpath(join(dir, f), relative_to))
89+
90+
return list(iterate())
91+
92+
3893
def itemsof(pattern, root=None, cwd=None):
3994
if not cwd:
40-
cwd = cwdStack[-1]
95+
cwd = getcwd()
4196
if not root:
4297
root = "."
4398

src/gui2/build.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from build.c import cxxprogram, cxxlibrary, simplerule, clibrary
22
from build.ab import simplerule
33
from build.pkg import package
4-
from glob import glob
4+
from build.utils import glob
55
from functools import reduce
66
import operator
77
from os.path import *
@@ -26,17 +26,14 @@
2626

2727
def headers_from(path):
2828
hdrs = {
29-
k: f"{path}/{k}" for k in glob("**/*.h*", root_dir=path, recursive=True)
29+
k: f"{path}/{k}" for k in glob(["**/*.h*"], dir=path, relative_to=path)
3030
}
3131
assert hdrs, f"path {path} contained no headers"
3232
return hdrs
3333

3434

3535
def sources_from(path, except_for=[]):
36-
srcs = [
37-
join(path, f) for f in glob("**/*.[ch]*", root_dir=path, recursive=True)
38-
]
39-
srcs = [f for f in srcs if f not in except_for]
36+
srcs = glob(["**/*.[ch]*"], exclude=except_for, dir=path)
4037
assert srcs, f"path {path} contained no sources"
4138
return srcs
4239

@@ -352,7 +349,7 @@ def romfs(name, id, dir):
352349

353350
simplerule(
354351
name=name,
355-
ins=[f for f in glob(dir + "/**", recursive=True) if isfile(f)],
352+
ins=glob(dir=dir),
356353
outs=["=romfs.cc"],
357354
deps=[f".+{id}_mkromfs"],
358355
commands=[
@@ -429,9 +426,9 @@ def plugin(name, id, srcs, hdrs, romfsdir, deps):
429426
"dep/imhex/plugins/builtin/source/content/welcome_screen.cpp",
430427
]
431428
+ glob(
432-
"dep/imhex/plugins/builtin/source/content/data_processor_nodes/*"
429+
dir="dep/imhex/plugins/builtin/source/content/data_processor_nodes"
433430
)
434-
+ glob("dep/imhex/plugins/builtin/source/content/tutorials/*"),
431+
+ glob(dir="dep/imhex/plugins/builtin/source/content/tutorials"),
435432
)
436433
+ [
437434
"./imhex_overrides/main_menu_items.cpp",

0 commit comments

Comments
 (0)