Skip to content

Commit 744480a

Browse files
authored
[bazel] Rewrite overlay handling to starlark (#170000)
Starlark is perfectly capable of doing what we need and this avoids the dependency on a host Python
1 parent 1f794e6 commit 744480a

File tree

2 files changed

+47
-138
lines changed

2 files changed

+47
-138
lines changed

utils/bazel/configure.bzl

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
"""Helper macros to configure the LLVM overlay project."""
66

7-
# Directory of overlay files relative to WORKSPACE
8-
DEFAULT_OVERLAY_PATH = "llvm-project-overlay"
9-
107
DEFAULT_TARGETS = [
118
"AArch64",
129
"AMDGPU",
@@ -30,43 +27,54 @@ DEFAULT_TARGETS = [
3027
"XCore",
3128
]
3229

30+
31+
MAX_TRAVERSAL_STEPS = 1000000 # "big number" upper bound on total visited dirs
32+
3333
def _overlay_directories(repository_ctx):
34-
src_path = repository_ctx.path(Label("@llvm-raw//:WORKSPACE")).dirname
35-
bazel_path = src_path.get_child("utils").get_child("bazel")
36-
overlay_path = bazel_path.get_child("llvm-project-overlay")
37-
script_path = bazel_path.get_child("overlay_directories.py")
38-
39-
python_bin = repository_ctx.which("python3")
40-
if not python_bin:
41-
# Windows typically just defines "python" as python3. The script itself
42-
# contains a check to ensure python3.
43-
python_bin = repository_ctx.which("python")
44-
45-
if not python_bin:
46-
fail("Failed to find python3 binary")
47-
48-
cmd = [
49-
python_bin,
50-
script_path,
51-
"--src",
52-
src_path,
53-
"--overlay",
54-
overlay_path,
55-
"--target",
56-
".",
57-
]
58-
exec_result = repository_ctx.execute(cmd, timeout = 20)
59-
60-
if exec_result.return_code != 0:
61-
fail(("Failed to execute overlay script: '{cmd}'\n" +
62-
"Exited with code {return_code}\n" +
63-
"stdout:\n{stdout}\n" +
64-
"stderr:\n{stderr}\n").format(
65-
cmd = " ".join([str(arg) for arg in cmd]),
66-
return_code = exec_result.return_code,
67-
stdout = exec_result.stdout,
68-
stderr = exec_result.stderr,
69-
))
34+
src_root = repository_ctx.path(Label("@llvm-raw//:WORKSPACE")).dirname
35+
overlay_root = src_root.get_child("utils/bazel/llvm-project-overlay")
36+
target_root = repository_ctx.path(".")
37+
38+
# Tries to minimize the number of symlinks created (that is, does not symlink
39+
# every single file). Symlinks every file in the overlay directory. Only symlinks
40+
# individual files in the source directory if their parent directory is also
41+
# contained in the overlay directory tree.
42+
43+
stack = ["."]
44+
for _ in range(MAX_TRAVERSAL_STEPS):
45+
rel_dir = stack.pop()
46+
47+
overlay_dirs = set()
48+
49+
# Symlink overlay files, overlay dirs will be handled in future iterations.
50+
for entry in overlay_root.get_child(rel_dir).readdir():
51+
name = entry.basename
52+
full_rel_path = rel_dir + "/" + name
53+
54+
if entry.is_dir:
55+
stack.append(full_rel_path)
56+
overlay_dirs.add(name)
57+
else:
58+
src_path = overlay_root.get_child(full_rel_path)
59+
dst_path = target_root.get_child(full_rel_path)
60+
repository_ctx.symlink(src_path, dst_path)
61+
62+
# Symlink source dirs (if not themselves overlaid) and files.
63+
for src_entry in src_root.get_child(rel_dir).readdir():
64+
name = src_entry.basename
65+
if name in overlay_dirs:
66+
# Skip: overlay has a directory with this name
67+
continue
68+
69+
repository_ctx.symlink(src_entry, target_root.get_child(rel_dir + "/" + name))
70+
71+
if not stack:
72+
return
73+
74+
fail("overlay_directories: exceeded MAX_TRAVERSAL_STEPS ({}). " +
75+
"Tree too large or a cycle in the filesystem?".format(
76+
MAX_TRAVERSAL_STEPS,
77+
))
7078

7179
def _extract_cmake_settings(repository_ctx, llvm_cmake):
7280
# The list to be written to vars.bzl

utils/bazel/overlay_directories.py

Lines changed: 0 additions & 99 deletions
This file was deleted.

0 commit comments

Comments
 (0)