Skip to content

Commit 459420c

Browse files
committed
[Bazel] Use dynamic workspace root determination
The `clang:ast` and `clang:builtin_headers_gen` targets currently use hardcoded `external/llvm-project` paths to access generated headers. With bzlmod this path becomes dependent on the module name, module version and module extension, so we need a more dynamic approach. Does not affect the WORKSPACE build. Reviewed By: GMNGeoffrey, #bazel_build Differential Revision: https://reviews.llvm.org/D137007
1 parent 3a6c66a commit 459420c

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

utils/bazel/llvm-project-overlay/clang/BUILD.bazel

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

55
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
6+
load("//:workspace_root.bzl", "workspace_root")
67
load("//llvm:tblgen.bzl", "gentbl")
78
load("//llvm:binary_alias.bzl", "binary_alias")
89
load("//llvm:cc_plugin_library.bzl", "cc_plugin_library")
@@ -718,6 +719,8 @@ gentbl(
718719
],
719720
)
720721

722+
workspace_root(name = "workspace_root")
723+
721724
cc_library(
722725
name = "ast",
723726
srcs = glob([
@@ -742,8 +745,8 @@ cc_library(
742745
# least bad approach. Using `includes` is *specifically* problematic for
743746
# this library because it contains files that collide easily with system
744747
# headers such as `CXXABI.h`.
745-
"-I$(GENDIR)/external/llvm-project/clang/lib/AST",
746-
"-I$(GENDIR)/external/llvm-project/clang/lib/AST/Interp",
748+
"-I$(GENDIR)/$(WORKSPACE_ROOT)/clang/lib/AST",
749+
"-I$(GENDIR)/$(WORKSPACE_ROOT)/clang/lib/AST/Interp",
747750
],
748751
textual_hdrs = [
749752
"include/clang/AST/AttrImpl.inc",
@@ -763,6 +766,9 @@ cc_library(
763766
] + glob([
764767
"include/clang/AST/*.def",
765768
]),
769+
toolchains = [
770+
":workspace_root",
771+
],
766772
deps = [
767773
":ast_attr_gen",
768774
":ast_comment_command_info_gen",
@@ -1536,12 +1542,15 @@ genrule(
15361542
outs = [hdr.replace("lib/Headers/", "staging/include/") for hdr in builtin_headers],
15371543
cmd = """
15381544
for src in $(SRCS); do
1539-
relsrc=$${src/*external\\/llvm-project\\/clang\\/lib\\/Headers\\/}
1545+
relsrc=$${src/*"$(WORKSPACE_ROOT)"\\/clang\\/lib\\/Headers}
15401546
target=$(@D)/staging/include/$$relsrc
15411547
mkdir -p $$(dirname $$target)
15421548
cp $$src $$target
15431549
done""",
15441550
output_to_bindir = 1,
1551+
toolchains = [
1552+
":workspace_root",
1553+
],
15451554
)
15461555

15471556
cc_library(
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def _workspace_root_impl(ctx):
2+
"""Dynamically determine the workspace root from the current context.
3+
4+
The path is made available as a `WORKSPACE_ROOT` environmment variable and
5+
may for instance be consumed in the `toolchains` attributes for `cc_library`
6+
and `genrule` targets.
7+
"""
8+
return [
9+
platform_common.TemplateVariableInfo({
10+
"WORKSPACE_ROOT": ctx.label.workspace_root,
11+
}),
12+
]
13+
14+
workspace_root = rule(
15+
implementation = _workspace_root_impl,
16+
attrs = {},
17+
)

0 commit comments

Comments
 (0)