-
Notifications
You must be signed in to change notification settings - Fork 0
/
bazel.py
436 lines (351 loc) · 13.3 KB
/
bazel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import abc
import argparse
import collections
import dataclasses
import hashlib
import json
import os
import pathlib
import queue
import shutil
import subprocess
import threading
from collections.abc import Iterable
from dataclasses import dataclass
class Label:
label: str
def __init__(self, label: str) -> None:
assert label.startswith("//"), "label must be resolved"
self.label = label
def __hash__(self) -> int:
return hash(self.label)
def __repr__(self) -> str:
return f"Label({self.label})"
def __eq__(self, value: object) -> bool:
return isinstance(value, Label) and self.label == value.label
def path(self) -> str:
assert self.label.startswith("//:")
return self.label.replace("//:", "")
@dataclass
class Action:
target: Label
args: list[str]
outs: list[Label]
srcs: list[Label] = dataclasses.field(default_factory=list)
@dataclass
class ActionResult:
exit_code: int
stdout: bytes
stderr: bytes
outputs: dict[str, str]
def as_json(self) -> str:
return json.dumps(
{
"exit_code": self.exit_code,
"outputs": self.outputs,
"stderr": self.stderr.decode("utf-8"),
"stdout": self.stdout.decode("utf-8"),
}
)
@staticmethod
def from_json(json_str: str) -> "ActionResult":
obj = json.loads(json_str)
assert isinstance(obj, dict)
return ActionResult(
exit_code=obj["exit_code"],
outputs=obj["outputs"],
stderr=obj["stderr"].encode("utf-8"),
stdout=obj["stdout"].encode("utf-8"),
)
class Target(abc.ABC):
pass
@dataclass
class RuleTarget(Target):
outs: list[Label]
@dataclass
class OutputFileTarget(Target):
produced_by_target: Label
@dataclass
class WorkspaceFileTarget(Target):
pass
def clean(workspace: pathlib.Path):
shutil.rmtree(str(workspace / "bazel-out"), ignore_errors=True)
def build(workspace: pathlib.Path, requested_labels: list[str], jobs: int):
with open('./examples/txt/BUILD.bazel', 'r') as file:
contents = file.read()
actions: list[Action] = []
exec(contents, {}, {
"genrule": lambda *_args, **kwargs: actions.append(genrule(**kwargs))
})
label_to_action: dict[Label, Action] = {action.target: action for action in actions}
targets: dict[Label, Target] = dict()
for root, _, files in os.walk(workspace):
rel_root = os.path.relpath(root, workspace)
for file in files:
file_target_label = f"//:{os.path.join(rel_root, file).replace('./', '')}"
targets[Label(file_target_label)] = WorkspaceFileTarget()
for action in actions:
targets[action.target] = RuleTarget(outs=action.outs)
for output in action.outs:
targets[output] = OutputFileTarget(produced_by_target=action.target)
targets_to_build: set[Label] = set()
for rule_target_label in requested_labels:
qualified_label = rule_target_label
# very simple qualification (:one -> //:one)
if not qualified_label.startswith("//"):
qualified_label = f"//{qualified_label}"
qualified_label = Label(qualified_label)
target = targets[qualified_label]
match target:
case RuleTarget():
targets_to_build.add(qualified_label)
case OutputFileTarget():
targets_to_build.add(target.produced_by_target)
case WorkspaceFileTarget():
# ignore these requests
pass
execroot = workspace / "bazel-out" / "execroot"
execroot.mkdir(parents=True, exist_ok=True)
graph = Digraph()
for action in actions:
for src in action.srcs:
target = targets[src]
match target:
case RuleTarget():
graph.insert(action.target, src)
case OutputFileTarget():
graph.insert(action.target, target.produced_by_target)
case WorkspaceFileTarget():
# a dependency on a workspace file has no bearing on the graph
pass
transitive_targets = graph.walk(targets_to_build)
subgraph = graph.subgraph(transitive_targets)
dependees = subgraph.reverse()
indegrees = dependees.indegrees()
sources = [label for label, count in indegrees.items() if count == 0]
runner = ActionRunner(
execroot=execroot,
targets=targets,
cache=Cache(cachedir=execroot / ".cache"),
)
ready: queue.Queue[Label] = queue.Queue()
for label in sources:
ready.put_nowait(label)
def worker():
while True:
label = ready.get()
print(f"executing {label.label}")
runner.run(label_to_action[label])
# schedule all ready dependees
for dep in dependees.edges[label]:
indegrees[dep] -= 1
if indegrees[dep] == 0:
ready.put_nowait(dep)
ready.task_done()
for _ in range(jobs):
threading.Thread(target=worker, daemon=True).start()
ready.join()
stats = runner.cache_hit_stats
if stats.misses == 0:
print(f"{stats.hits} cache {pluralize('hit', stats.hits)}")
elif stats.hits == 0:
print(f"executed {stats.misses} {pluralize('action', stats.misses)}")
else:
print(
f"executed {stats.misses} {pluralize('action', stats.misses)}, {stats.hits} cache {pluralize('hit', stats.hits)}"
)
def pluralize(word: str, count: int) -> str:
return word if count == 1 else f"{word}s"
def genrule(
*,
name: str,
cmd: str,
srcs: list[str] | None = None,
outs: list[str],
) -> Action:
assert name is not None, "missing name"
assert cmd is not None, "missing cmd"
assert len(outs) > 0, "genrule must have at least one output files"
srcs = srcs if srcs is not None else []
src_labels = [Label(f"//{src}") for src in srcs]
out_labels = [Label(f"//:{out}") for out in outs]
cmd_lines = ["set -euo pipefail"]
if len(outs) == 1:
cmd_lines.append(f"out() {{ echo {outs[0]}; }}")
cmd_lines.append(cmd)
args = ["bash", "-c", "\n".join(cmd_lines)]
return Action(
target=Label(f"//:{name}"),
args=args,
outs=out_labels,
srcs=src_labels,
)
class Digraph:
edges: collections.defaultdict[Label, set[Label]]
def __init__(self) -> None:
self.edges = collections.defaultdict(set)
def insert(self, frm: Label, to: Label):
self.edges[frm].add(to)
def walk(self, entrypoints: Iterable[Label]) -> set[Label]:
seen: set[Label] = set()
queue = collections.deque(entrypoints)
while len(queue) > 0:
label = queue.popleft()
if label in seen:
continue
seen.add(label)
for dep in self.edges[label]:
queue.append(dep)
return seen
def subgraph(self, keep_labels: set[Label]) -> "Digraph":
subgraph = Digraph()
for label, deps in self.edges.items():
if label not in keep_labels:
continue
for dep_label in deps:
if dep_label in keep_labels:
subgraph.insert(label, dep_label)
for label in keep_labels:
if label not in subgraph.edges:
subgraph.edges[label] = set()
return subgraph
def indegrees(self) -> dict[Label, int]:
indegrees = collections.defaultdict(int)
for label, deps in self.edges.items():
for dep in deps:
indegrees[dep] += 1
if label not in indegrees:
indegrees[label] = 0
return indegrees
def reverse(self) -> "Digraph":
reverse = Digraph()
for label, deps in self.edges.items():
reverse.edges[label] # ensure that nodes with no dependees are retained
for dep in deps:
reverse.insert(dep, label)
return reverse
class Cache:
def __init__(self, cachedir: pathlib.Path) -> None:
self.cachedir = cachedir
cachedir.mkdir(parents=True, exist_ok=True)
def get_action_result(self, key: str) -> ActionResult | None:
p = self.cachedir / key
if not p.exists():
return None
return ActionResult.from_json(p.read_text("utf-8"))
def update_action_result(self, key: str, result: ActionResult):
(self.cachedir / key).write_bytes(result.as_json().encode("utf-8"))
@dataclass
class CacheHitStats:
hits: int = 0
misses: int = 0
class ActionRunner:
_execroot: pathlib.Path
_targets: dict[Label, Target]
_cache: Cache
_already_linked_paths: set[str]
_next_sandbox_id: int
cache_hit_stats: CacheHitStats
def __init__(
self,
execroot: pathlib.Path,
targets: dict[Label, Target],
cache: Cache,
) -> None:
self._execroot = execroot
self._targets = targets
self._cache = cache
self._already_linked_paths = set()
self._next_sandbox_id = 0
self.cache_hit_stats = CacheHitStats()
def run(self, action: Action) -> ActionResult:
# link all referenced workspace files into the execroot
for src in action.srcs:
target = self._targets[src]
if not isinstance(target, WorkspaceFileTarget):
continue
src_path = src.path()
if src_path in self._already_linked_paths:
continue
dest = self._execroot / src_path
dest.unlink(missing_ok=True)
dest.hardlink_to(workspace / src_path)
self._already_linked_paths.add(src_path)
action_key = self._hash_action(action, self._execroot)
result = self._cache.get_action_result(action_key)
if result is None:
self.cache_hit_stats.misses += 1
result = self._spawn_action(action=action)
if result.exit_code == 0:
self._cache.update_action_result(action_key, result)
else:
self.cache_hit_stats.hits += 1
for file, contents in result.outputs.items():
(self._execroot / file).write_text(contents, encoding="utf-8")
return result
def _hash_action(self, action: Action, execroot: pathlib.Path) -> str:
hasher = hashlib.sha256()
hasher.update(" ".join(action.args).encode())
hasher.update(" ".join(out.path() for out in action.outs).encode())
for src in action.srcs:
content = execroot.joinpath(src.path()).read_bytes()
hasher.update(content)
return hasher.hexdigest()
def _spawn_action(self, action: Action) -> ActionResult:
sandbox_dir = self._get_next_sandbox_dir()
for src in action.srcs:
target = self._targets[src]
match target:
case RuleTarget():
# a dependency on a rule target means you implicitly depend on all of its outputs
for out in target.outs:
(sandbox_dir / out.path()).hardlink_to(
self._execroot / out.path()
)
case WorkspaceFileTarget():
(sandbox_dir / src.path()).hardlink_to(self._execroot / src.path())
case OutputFileTarget():
(sandbox_dir / src.path()).hardlink_to(self._execroot / src.path())
result = subprocess.run(
action.args,
cwd=sandbox_dir,
check=False,
capture_output=True,
)
outputs: dict[str, str] = dict()
if result.returncode == 0:
for out in action.outs:
sandbox_out = sandbox_dir / out.path()
assert sandbox_out.exists(), "action did not produce expected output"
outputs[out.path()] = sandbox_out.read_text("utf-8")
sandbox_out.rename(self._execroot / out.path())
shutil.rmtree(sandbox_dir)
return ActionResult(
exit_code=result.returncode,
stdout=result.stdout,
stderr=result.stderr,
outputs=outputs,
)
def _get_next_sandbox_dir(self):
sandbox_dir = self._execroot / "sandbox" / f"{self._next_sandbox_id}"
self._next_sandbox_id += 1
assert not sandbox_dir.exists(), f"sandbox dir {sandbox_dir} already exists"
sandbox_dir.mkdir(parents=True, exist_ok=False)
return sandbox_dir
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="bazel", description="a simple build tool")
parser.add_argument("mode", help="build or clean", choices=["build", "clean"])
parser.add_argument("labels", nargs="*", help="labels for the targets to build")
parser.add_argument("--workspace", default=".", help="workspace directory")
parser.add_argument("--jobs", default=8, help="workspace directory", type=int)
args = parser.parse_args()
if args.workspace.startswith("/"):
workspace = args.workspace
else:
workspace = os.path.abspath(args.workspace)
workspace = pathlib.Path(workspace)
match args.mode:
case "build":
build(workspace, args.labels, args.jobs)
case "clean":
clean(workspace)