[Bazel] Use bazel_skylib paths for paths munging
authorGeoffrey Martin-Noble <gcmn@google.com>
Fri, 16 Jul 2021 18:49:50 +0000 (11:49 -0700)
committerGeoffrey Martin-Noble <gcmn@google.com>
Fri, 16 Jul 2021 20:52:07 +0000 (13:52 -0700)
We do a lot of path munging and bazel_sklyib is a pretty reasonable dep.

Reviewed By: jpienaar

Differential Revision: https://reviews.llvm.org/D106175

utils/bazel/WORKSPACE
utils/bazel/configure.bzl
utils/bazel/examples/http_archive/WORKSPACE
utils/bazel/examples/submodule/WORKSPACE
utils/bazel/llvm-project-overlay/mlir/tblgen.bzl
utils/bazel/llvm_configs/BUILD.bazel

index bc0f5d2..af189f7 100644 (file)
@@ -2,10 +2,22 @@
 # See https://llvm.org/LICENSE.txt for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-load(":configure.bzl", "llvm_configure")
 load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
 
+SKYLIB_VERSION = "1.0.3"
+
+http_archive(
+    name = "bazel_skylib",
+    sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
+    urls = [
+        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz".format(version=SKYLIB_VERSION),
+        "https://github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz".format(version=SKYLIB_VERSION),
+    ],
+)
+
+load(":configure.bzl", "llvm_configure")
+
 llvm_configure(
     name = "llvm-project",
     overlay_path = "llvm-project-overlay",
@@ -58,19 +70,6 @@ maybe(
 )
 
 http_archive(
-    name = "bazel_skylib",
-    sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
-    urls = [
-        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
-        "https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
-    ],
-)
-
-load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
-
-bazel_skylib_workspace()
-
-http_archive(
     name = "bazel_toolchains",
     sha256 = "1adf5db506a7e3c465a26988514cfc3971af6d5b3c2218925cd6e71ee443fc3f",
     strip_prefix = "bazel-toolchains-4.0.0",
@@ -81,4 +80,5 @@ http_archive(
 )
 
 load("@bazel_toolchains//rules:rbe_repo.bzl", "rbe_autoconfig")
+
 rbe_autoconfig(name = "rbe_default")
index da67e34..be78de3 100644 (file)
@@ -5,6 +5,7 @@
 """Helper macros to configure the LLVM overlay project."""
 
 load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
+load("@bazel_skylib//lib:paths.bzl", "paths")
 load(":zlib.bzl", "llvm_zlib_disable", "llvm_zlib_system")
 load(":terminfo.bzl", "llvm_terminfo_disable", "llvm_terminfo_system")
 
@@ -26,32 +27,17 @@ DEFAULT_TARGETS = [
     "X86",
 ]
 
-def _is_absolute(path):
-    """Returns `True` if `path` is an absolute path.
-
-    Args:
-      path: A path (which is a string).
-    Returns:
-      `True` if `path` is an absolute path.
-    """
-    return path.startswith("/") or (len(path) > 2 and path[1] == ":")
-
-def _join_path(a, b):
-    if _is_absolute(b):
-        return b
-    return str(a) + "/" + str(b)
-
 def _overlay_directories(repository_ctx):
-    src_workspace_path = repository_ctx.path(
+    src_workspace_path = str(repository_ctx.path(
         repository_ctx.attr.src_workspace,
-    ).dirname
+    ).dirname)
 
-    src_path = _join_path(src_workspace_path, repository_ctx.attr.src_path)
+    src_path = paths.join(src_workspace_path, repository_ctx.attr.src_path)
 
-    overlay_workspace_path = repository_ctx.path(
+    overlay_workspace_path = str(repository_ctx.path(
         repository_ctx.attr.overlay_workspace,
-    ).dirname
-    overlay_path = _join_path(
+    ).dirname)
+    overlay_path = paths.join(
         overlay_workspace_path,
         repository_ctx.attr.overlay_path,
     )
index 01f674e..cadeefb 100644 (file)
@@ -8,6 +8,17 @@ workspace(name = "http_archive_example")
 
 load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 
+SKYLIB_VERSION = "1.0.3"
+
+http_archive(
+    name = "bazel_skylib",
+    sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
+    urls = [
+        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz".format(version=SKYLIB_VERSION),
+        "https://github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz".format(version=SKYLIB_VERSION),
+    ],
+)
+
 # Replace with the LLVM commit you want to use.
 LLVM_COMMIT = "09ac97ce350316b95b8cddb796d52f71b6f68296"
 
index 1337c1c..c83ab97 100644 (file)
@@ -6,6 +6,17 @@
 
 workspace(name = "submodule_example")
 
+SKYLIB_VERSION = "1.0.3"
+
+http_archive(
+    name = "bazel_skylib",
+    sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
+    urls = [
+        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz".format(version=SKYLIB_VERSION),
+        "https://github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz".format(version=SKYLIB_VERSION),
+    ],
+)
+
 # Or wherever your submodule is located.
 SUBMODULE_PATH = "third_party/llvm-project"
 
index 88562ce..ea1e5d0 100644 (file)
@@ -3,6 +3,9 @@
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 """BUILD extensions for MLIR table generation."""
 
+load("@bazel_skylib//lib:paths.bzl", "paths")
+
+
 TdInfo = provider(
     "Holds TableGen files and the dependencies and include paths necessary to" +
     " build them.",
@@ -69,8 +72,8 @@ def _prefix_roots(ctx, includes):
     prefixed_includes = []
     for include in includes:
         prefixed_includes.append(include)
-        prefixed_includes.append(ctx.genfiles_dir.path + "/" + include)
-        prefixed_includes.append(ctx.bin_dir.path + "/" + include)
+        prefixed_includes.append(paths.join(ctx.genfiles_dir.path, include))
+        prefixed_includes.append(paths.join(ctx.bin_dir.path, include))
     return prefixed_includes
 
 def _resolve_includes(ctx, includes):
@@ -84,9 +87,11 @@ def _resolve_includes(ctx, includes):
     workspace_root = workspace_root if workspace_root else "."
     resolved_includes = []
     for include in includes:
-        if not include.startswith("/"):
-            include = "/" + package + "/" + include
-        include = workspace_root + include
+        if paths.is_absolute(include):
+            include = include.lstrip("/")
+        else:
+            include = paths.join(package, include)
+        include = paths.join(workspace_root, include)
         resolved_includes.extend(_prefix_roots(ctx, [include]))
     return resolved_includes
 
@@ -324,12 +329,15 @@ def gentbl_filegroup(
       **kwargs: Extra keyword arguments to pass to all generated rules.
     """
 
-    llvm_project_execroot_path = Label("//mlir:tblgen.bzl", relative_to_caller_repository = False).workspace_root
+    llvm_project_execroot_path = Label(
+        "//mlir:tblgen.bzl",
+        relative_to_caller_repository = False,
+    ).workspace_root
 
     # TODO(gcmn): Update callers to td_library and explicit includes and drop
     # this hardcoded include.
     hardcoded_includes = [
-        "%s/mlir/include" % llvm_project_execroot_path,
+        paths.join(llvm_project_execroot_path, "mlir/include"),
     ]
 
     for (opts, out) in tbl_outs:
index cfbb1e1..5a4f997 100644 (file)
@@ -2,11 +2,12 @@
 # See https://llvm.org/LICENSE.txt for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-# We perform diff testing here to avoid any in-tree BUILD files depending on
-# bazel_sklyib. These diff tests ensure that the current Bazel configuration
-# does not drift from the configuration in the .cmake files, since we don't
-# alway use them directly (and even if we did we wouldn't necessarily pick up
-# changes there). These are literal change-detector tests.
+# These diff tests ensure that the current Bazel configuration does not drift
+# from the configuration in the .cmake files, since we don't alway use them
+# directly (and even if we did we wouldn't necessarily pick up changes there).
+# These are literal change-detector tests. We perform diff testing here since
+# it isn't really part of building LLVM and we don't want to include the config
+# copies in the workspace used by dependent projects.
 
 load("@bazel_skylib//rules:diff_test.bzl", "diff_test")