Skip to content

Commit 8b86b3a

Browse files
committed
[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
1 parent da3dbfc commit 8b86b3a

File tree

6 files changed

+64
-47
lines changed

6 files changed

+64
-47
lines changed

utils/bazel/WORKSPACE

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@
22
# See https://llvm.org/LICENSE.txt for license information.
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

5-
load(":configure.bzl", "llvm_configure")
65
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
76
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
87

8+
SKYLIB_VERSION = "1.0.3"
9+
10+
http_archive(
11+
name = "bazel_skylib",
12+
sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
13+
urls = [
14+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz".format(version=SKYLIB_VERSION),
15+
"https://github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz".format(version=SKYLIB_VERSION),
16+
],
17+
)
18+
19+
load(":configure.bzl", "llvm_configure")
20+
921
llvm_configure(
1022
name = "llvm-project",
1123
overlay_path = "llvm-project-overlay",
@@ -57,19 +69,6 @@ maybe(
5769
name = "vulkan_sdk",
5870
)
5971

60-
http_archive(
61-
name = "bazel_skylib",
62-
sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
63-
urls = [
64-
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
65-
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
66-
],
67-
)
68-
69-
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
70-
71-
bazel_skylib_workspace()
72-
7372
http_archive(
7473
name = "bazel_toolchains",
7574
sha256 = "1adf5db506a7e3c465a26988514cfc3971af6d5b3c2218925cd6e71ee443fc3f",
@@ -81,4 +80,5 @@ http_archive(
8180
)
8281

8382
load("@bazel_toolchains//rules:rbe_repo.bzl", "rbe_autoconfig")
83+
8484
rbe_autoconfig(name = "rbe_default")

utils/bazel/configure.bzl

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""Helper macros to configure the LLVM overlay project."""
66

77
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
8+
load("@bazel_skylib//lib:paths.bzl", "paths")
89
load(":zlib.bzl", "llvm_zlib_disable", "llvm_zlib_system")
910
load(":terminfo.bzl", "llvm_terminfo_disable", "llvm_terminfo_system")
1011

@@ -26,32 +27,17 @@ DEFAULT_TARGETS = [
2627
"X86",
2728
]
2829

29-
def _is_absolute(path):
30-
"""Returns `True` if `path` is an absolute path.
31-
32-
Args:
33-
path: A path (which is a string).
34-
Returns:
35-
`True` if `path` is an absolute path.
36-
"""
37-
return path.startswith("/") or (len(path) > 2 and path[1] == ":")
38-
39-
def _join_path(a, b):
40-
if _is_absolute(b):
41-
return b
42-
return str(a) + "/" + str(b)
43-
4430
def _overlay_directories(repository_ctx):
45-
src_workspace_path = repository_ctx.path(
31+
src_workspace_path = str(repository_ctx.path(
4632
repository_ctx.attr.src_workspace,
47-
).dirname
33+
).dirname)
4834

49-
src_path = _join_path(src_workspace_path, repository_ctx.attr.src_path)
35+
src_path = paths.join(src_workspace_path, repository_ctx.attr.src_path)
5036

51-
overlay_workspace_path = repository_ctx.path(
37+
overlay_workspace_path = str(repository_ctx.path(
5238
repository_ctx.attr.overlay_workspace,
53-
).dirname
54-
overlay_path = _join_path(
39+
).dirname)
40+
overlay_path = paths.join(
5541
overlay_workspace_path,
5642
repository_ctx.attr.overlay_path,
5743
)

utils/bazel/examples/http_archive/WORKSPACE

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ workspace(name = "http_archive_example")
88

99
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
1010

11+
SKYLIB_VERSION = "1.0.3"
12+
13+
http_archive(
14+
name = "bazel_skylib",
15+
sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
16+
urls = [
17+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz".format(version=SKYLIB_VERSION),
18+
"https://github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz".format(version=SKYLIB_VERSION),
19+
],
20+
)
21+
1122
# Replace with the LLVM commit you want to use.
1223
LLVM_COMMIT = "09ac97ce350316b95b8cddb796d52f71b6f68296"
1324

utils/bazel/examples/submodule/WORKSPACE

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66

77
workspace(name = "submodule_example")
88

9+
SKYLIB_VERSION = "1.0.3"
10+
11+
http_archive(
12+
name = "bazel_skylib",
13+
sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
14+
urls = [
15+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz".format(version=SKYLIB_VERSION),
16+
"https://github.com/bazelbuild/bazel-skylib/releases/download/{version}/bazel-skylib-{version}.tar.gz".format(version=SKYLIB_VERSION),
17+
],
18+
)
19+
920
# Or wherever your submodule is located.
1021
SUBMODULE_PATH = "third_party/llvm-project"
1122

utils/bazel/llvm-project-overlay/mlir/tblgen.bzl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44
"""BUILD extensions for MLIR table generation."""
55

6+
load("@bazel_skylib//lib:paths.bzl", "paths")
7+
8+
69
TdInfo = provider(
710
"Holds TableGen files and the dependencies and include paths necessary to" +
811
" build them.",
@@ -69,8 +72,8 @@ def _prefix_roots(ctx, includes):
6972
prefixed_includes = []
7073
for include in includes:
7174
prefixed_includes.append(include)
72-
prefixed_includes.append(ctx.genfiles_dir.path + "/" + include)
73-
prefixed_includes.append(ctx.bin_dir.path + "/" + include)
75+
prefixed_includes.append(paths.join(ctx.genfiles_dir.path, include))
76+
prefixed_includes.append(paths.join(ctx.bin_dir.path, include))
7477
return prefixed_includes
7578

7679
def _resolve_includes(ctx, includes):
@@ -84,9 +87,11 @@ def _resolve_includes(ctx, includes):
8487
workspace_root = workspace_root if workspace_root else "."
8588
resolved_includes = []
8689
for include in includes:
87-
if not include.startswith("/"):
88-
include = "/" + package + "/" + include
89-
include = workspace_root + include
90+
if paths.is_absolute(include):
91+
include = include.lstrip("/")
92+
else:
93+
include = paths.join(package, include)
94+
include = paths.join(workspace_root, include)
9095
resolved_includes.extend(_prefix_roots(ctx, [include]))
9196
return resolved_includes
9297

@@ -324,12 +329,15 @@ def gentbl_filegroup(
324329
**kwargs: Extra keyword arguments to pass to all generated rules.
325330
"""
326331

327-
llvm_project_execroot_path = Label("//mlir:tblgen.bzl", relative_to_caller_repository = False).workspace_root
332+
llvm_project_execroot_path = Label(
333+
"//mlir:tblgen.bzl",
334+
relative_to_caller_repository = False,
335+
).workspace_root
328336

329337
# TODO(gcmn): Update callers to td_library and explicit includes and drop
330338
# this hardcoded include.
331339
hardcoded_includes = [
332-
"%s/mlir/include" % llvm_project_execroot_path,
340+
paths.join(llvm_project_execroot_path, "mlir/include"),
333341
]
334342

335343
for (opts, out) in tbl_outs:

utils/bazel/llvm_configs/BUILD.bazel

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
# See https://llvm.org/LICENSE.txt for license information.
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

5-
# We perform diff testing here to avoid any in-tree BUILD files depending on
6-
# bazel_sklyib. These diff tests ensure that the current Bazel configuration
7-
# does not drift from the configuration in the .cmake files, since we don't
8-
# alway use them directly (and even if we did we wouldn't necessarily pick up
9-
# changes there). These are literal change-detector tests.
5+
# These diff tests ensure that the current Bazel configuration does not drift
6+
# from the configuration in the .cmake files, since we don't alway use them
7+
# directly (and even if we did we wouldn't necessarily pick up changes there).
8+
# These are literal change-detector tests. We perform diff testing here since
9+
# it isn't really part of building LLVM and we don't want to include the config
10+
# copies in the workspace used by dependent projects.
1011

1112
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
1213

0 commit comments

Comments
 (0)