Skip to content

Commit 5df68ca

Browse files
keithaadeshps-mcw
authored andcommitted
[bazel] Use zstd from the BCR (llvm#169146)
This way if the downstream consuming project uses zstd we make sure they are dedup'd. This uses a new rule to make sure layering_check still works while allowing us to augment the upstream library rules with LLVM specific `defines`.
1 parent 2a67261 commit 5df68ca

File tree

8 files changed

+95
-40
lines changed

8 files changed

+95
-40
lines changed

utils/bazel/MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ bazel_dep(name = "rules_cc", version = "0.2.11")
1515
bazel_dep(name = "rules_foreign_cc", version = "0.15.1")
1616
bazel_dep(name = "rules_python", version = "1.6.3")
1717
bazel_dep(name = "rules_shell", version = "0.6.1")
18+
bazel_dep(name = "zstd", version = "1.5.7", repo_name = "llvm_zstd")
1819

1920
llvm_repos_extension = use_extension(":extensions.bzl", "llvm_repos_extension")
2021
use_repo(
2122
llvm_repos_extension,
2223
"gmp",
2324
"llvm-raw",
2425
"llvm_zlib",
25-
"llvm_zstd",
2626
"mpc",
2727
"mpfr",
2828
"nanobind",

utils/bazel/MODULE.bazel.lock

Lines changed: 4 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/bazel/extensions.bzl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,6 @@ def _llvm_repos_extension_impl(module_ctx):
7979
build_file = "@llvm-raw//utils/bazel/third_party_build:pfm.BUILD",
8080
)
8181

82-
http_archive(
83-
name = "llvm_zstd",
84-
build_file = "@llvm-raw//utils/bazel/third_party_build:zstd.BUILD",
85-
sha256 = "7c42d56fac126929a6a85dbc73ff1db2411d04f104fae9bdea51305663a83fd0",
86-
strip_prefix = "zstd-1.5.2",
87-
urls = [
88-
"https://github.com/facebook/zstd/releases/download/v1.5.2/zstd-1.5.2.tar.gz",
89-
],
90-
)
91-
9282
http_archive(
9383
name = "pybind11",
9484
url = "https://github.com/pybind/pybind11/archive/v2.10.3.zip",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ cc_library(
105105
"//llvm:TargetParser",
106106
"//llvm:TransformUtils",
107107
"//llvm:config",
108+
"//third-party:zstd",
108109
"@llvm_zlib//:zlib",
109-
"@llvm_zstd//:zstd",
110110
],
111111
)
112112

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ cc_library(
331331
# We unconditionally depend on the custom LLVM zstd wrapper. This will
332332
# be an empty library unless zstd is enabled, in which case it will
333333
# both provide the necessary dependencies and configuration defines.
334-
"@llvm_zstd//:zstd",
334+
"//third-party:zstd",
335335
],
336336
)
337337

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
5+
load(":cc_library_wrapper.bzl", "cc_library_wrapper")
6+
7+
package(default_visibility = ["//visibility:public"])
8+
9+
bool_flag(
10+
name = "llvm_enable_zstd",
11+
build_setting_default = True,
12+
)
13+
14+
config_setting(
15+
name = "llvm_zstd_enabled",
16+
flag_values = {":llvm_enable_zstd": "true"},
17+
)
18+
19+
cc_library_wrapper(
20+
name = "zstd",
21+
defines = select({
22+
":llvm_zstd_enabled": [
23+
"LLVM_ENABLE_ZSTD=1",
24+
"ZSTD_MULTITHREAD",
25+
],
26+
"//conditions:default": [],
27+
}),
28+
deps = select({
29+
":llvm_zstd_enabled": [
30+
"@llvm_zstd//:zstd",
31+
],
32+
"//conditions:default": [],
33+
}),
34+
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
"""Re-export a cc_library with added LLVM specific settings.
6+
7+
This re-exports the dependent libraries in a way that satisfies layering_check
8+
9+
cc_library_wrapper(
10+
name = "library_wrapper",
11+
deps = [
12+
"@example//:library",
13+
],
14+
defines = [
15+
"LLVM_ENABLE_EXAMPLE=1",
16+
],
17+
)
18+
"""
19+
20+
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
21+
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
22+
23+
visibility("private")
24+
25+
def _cc_library_wrapper_impl(ctx):
26+
all_cc_infos = [dep[CcInfo] for dep in ctx.attr.deps]
27+
if ctx.attr.defines:
28+
all_cc_infos.append(CcInfo(
29+
compilation_context = cc_common.create_compilation_context(
30+
defines = depset(ctx.attr.defines),
31+
),
32+
))
33+
34+
return cc_common.merge_cc_infos(direct_cc_infos = all_cc_infos)
35+
36+
cc_library_wrapper = rule(
37+
implementation = _cc_library_wrapper_impl,
38+
attrs = {
39+
"deps": attr.label_list(
40+
doc = "Dependencies to cc_library targets to re-export.",
41+
providers = [CcInfo],
42+
),
43+
"defines": attr.string_list(
44+
doc = "Additional preprocessor definitions to add to all dependent targets.",
45+
default = [],
46+
),
47+
},
48+
doc = "Re-export a cc_library with added LLVM specific settings.",
49+
provides = [CcInfo],
50+
)

utils/bazel/third_party_build/zstd.BUILD

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
22
# See https://llvm.org/LICENSE.txt for license information.
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4-
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
4+
load("@rules_cc//cc:cc_library.bzl", "cc_library")
55

66
package(
77
default_visibility = ["//visibility:public"],
88
# BSD/MIT-like license (for zstd)
99
licenses = ["notice"],
1010
)
1111

12-
bool_flag(
13-
name = "llvm_enable_zstd",
14-
build_setting_default = True,
15-
)
16-
17-
config_setting(
18-
name = "llvm_zstd_enabled",
19-
flag_values = {":llvm_enable_zstd": "true"},
20-
)
21-
2212
cc_library(
2313
name = "zstd",
2414
srcs = select({
25-
":llvm_zstd_enabled": glob([
15+
"@llvm-project//third-party:llvm_zstd_enabled": glob([
2616
"lib/common/*.c",
2717
"lib/common/*.h",
2818
"lib/compress/*.c",
@@ -36,15 +26,15 @@ cc_library(
3626
"//conditions:default": [],
3727
}),
3828
hdrs = select({
39-
":llvm_zstd_enabled": [
29+
"@llvm-project//third-party:llvm_zstd_enabled": [
4030
"lib/zdict.h",
4131
"lib/zstd.h",
4232
"lib/zstd_errors.h",
4333
],
4434
"//conditions:default": [],
4535
}),
4636
defines = select({
47-
":llvm_zstd_enabled": [
37+
"@llvm-project//third-party:llvm_zstd_enabled": [
4838
"LLVM_ENABLE_ZSTD=1",
4939
"ZSTD_MULTITHREAD",
5040
],

0 commit comments

Comments
 (0)