Skip to content

Commit 1bc54ad

Browse files
committed
Add cc_library_wrapper
1 parent a49686c commit 1bc54ad

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
55
load("@rules_cc//cc:defs.bzl", "cc_library")
6+
load(":cc_library_wrapper.bzl", "cc_library_wrapper")
67

78
cc_library(name = "empty_lib")
89

@@ -16,25 +17,20 @@ config_setting(
1617
flag_values = {":llvm_enable_zstd": "true"},
1718
)
1819

19-
cc_library(
20-
name = "zstd_wrapper",
20+
cc_library_wrapper(
21+
name = "zstd",
2122
defines = select({
2223
":llvm_zstd_enabled": [
2324
"LLVM_ENABLE_ZSTD=1",
2425
"ZSTD_MULTITHREAD",
2526
],
2627
"//conditions:default": [],
2728
}),
28-
deps = [
29-
"@llvm_zstd//:zstd",
30-
],
31-
)
32-
33-
alias(
34-
name = "zstd",
35-
actual = select({
36-
":llvm_zstd_enabled": ":zstd_wrapper",
37-
"//conditions:default": ":empty_lib",
38-
}),
3929
visibility = ["//visibility:public"],
30+
deps = select({
31+
":llvm_zstd_enabled": [
32+
"@llvm_zstd//:zstd",
33+
],
34+
"//conditions:default": [],
35+
}),
4036
)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
def _cc_library_wrapper_impl(ctx):
24+
all_cc_infos = [dep[CcInfo] for dep in ctx.attr.deps]
25+
if ctx.attr.defines:
26+
all_cc_infos.append(CcInfo(
27+
compilation_context = cc_common.create_compilation_context(
28+
defines = depset(ctx.attr.defines),
29+
),
30+
))
31+
32+
return cc_common.merge_cc_infos(direct_cc_infos = all_cc_infos)
33+
34+
cc_library_wrapper = rule(
35+
implementation = _cc_library_wrapper_impl,
36+
attrs = {
37+
"deps": attr.label_list(
38+
doc = "Dependencies to cc_library targets to re-export.",
39+
providers = [CcInfo],
40+
),
41+
"defines": attr.string_list(
42+
doc = "Additional preprocessor definitions to add to all dependent targets.",
43+
default = [],
44+
),
45+
},
46+
doc = "Re-export a cc_library with added LLVM specific settings.",
47+
)

0 commit comments

Comments
 (0)