[PATCH] D137007: [Bazel] Use dynamic workspace root determination

Aaron Siddhartha Mondal via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 28 22:41:08 PDT 2022


aaronmondal created this revision.
aaronmondal added reviewers: GMNGeoffrey, csigg.
aaronmondal added a project: bazel build.
Herald added a project: All.
aaronmondal requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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 affect the WORKSPACE build.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137007

Files:
  utils/bazel/llvm-project-overlay/clang/BUILD.bazel
  utils/bazel/llvm-project-overlay/workspace_root.bzl


Index: utils/bazel/llvm-project-overlay/workspace_root.bzl
===================================================================
--- /dev/null
+++ utils/bazel/llvm-project-overlay/workspace_root.bzl
@@ -0,0 +1,17 @@
+def _workspace_root_impl(ctx):
+    """Dynamically determine the workspace root from the current context.
+
+    The path is made available as a `WORKSPACE_ROOT` environmment variable and
+    may for instance be consumed in the `toolchains` attributes for `cc_library`
+    and `genrule` targets.
+    """
+    return [
+        platform_common.TemplateVariableInfo({
+            "WORKSPACE_ROOT": ctx.label.workspace_root,
+        }),
+    ]
+
+workspace_root = rule(
+    implementation = _workspace_root_impl,
+    attrs = {},
+)
Index: utils/bazel/llvm-project-overlay/clang/BUILD.bazel
===================================================================
--- utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -3,6 +3,7 @@
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
 load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
+load("//:workspace_root.bzl", "workspace_root")
 load("//llvm:tblgen.bzl", "gentbl")
 load("//llvm:binary_alias.bzl", "binary_alias")
 load("//llvm:cc_plugin_library.bzl", "cc_plugin_library")
@@ -701,6 +702,8 @@
     ],
 )
 
+workspace_root(name = "workspace_root")
+
 cc_library(
     name = "ast",
     srcs = glob([
@@ -725,8 +728,8 @@
         # least bad approach. Using `includes` is *specifically* problematic for
         # this library because it contains files that collide easily with system
         # headers such as `CXXABI.h`.
-        "-I$(GENDIR)/external/llvm-project/clang/lib/AST",
-        "-I$(GENDIR)/external/llvm-project/clang/lib/AST/Interp",
+        "-I$(GENDIR)/$(WORKSPACE_ROOT)/clang/lib/AST",
+        "-I$(GENDIR)/$(WORKSPACE_ROOT)/clang/lib/AST/Interp",
     ],
     textual_hdrs = [
         "include/clang/AST/AttrImpl.inc",
@@ -746,6 +749,9 @@
     ] + glob([
         "include/clang/AST/*.def",
     ]),
+    toolchains = [
+        ":workspace_root",
+    ],
     deps = [
         ":ast_attr_gen",
         ":ast_comment_command_info_gen",
@@ -1502,12 +1508,15 @@
     outs = [hdr.replace("lib/Headers/", "staging/include/") for hdr in builtin_headers],
     cmd = """
        for src in $(SRCS); do
-         relsrc=$${src/*external\\/llvm-project\\/clang\\/lib\\/Headers\\/}
+         relsrc=$${src/*"$(WORKSPACE_ROOT)"\\/clang\\/lib\\/Headers}
          target=$(@D)/staging/include/$$relsrc
          mkdir -p $$(dirname $$target)
          cp $$src $$target
        done""",
     output_to_bindir = 1,
+    toolchains = [
+        ":workspace_root",
+    ],
 )
 
 cc_library(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137007.471714.patch
Type: text/x-patch
Size: 2756 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221029/5b0b9c02/attachment.bin>


More information about the llvm-commits mailing list