[Bazel] Use bazel_skylib paths for paths munging

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
This commit is contained in:
Geoffrey Martin-Noble 2021-07-16 11:49:50 -07:00
parent da3dbfcacf
commit 8b86b3a846
6 changed files with 64 additions and 47 deletions

View 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",
@ -57,19 +69,6 @@ maybe(
name = "vulkan_sdk",
)
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",
@ -81,4 +80,5 @@ http_archive(
)
load("@bazel_toolchains//rules:rbe_repo.bzl", "rbe_autoconfig")
rbe_autoconfig(name = "rbe_default")

View 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,
)

View 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"

View 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"

View 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:

View 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")