Skip to content

Commit 77f8cc3

Browse files
jkurland-rokufmeum
authored andcommitted
Add a test for extra_target_compatible_with
1 parent 2707b82 commit 77f8cc3

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

tests/BUILD.bazel

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
load("@bazel_skylib//rules:build_test.bzl", "build_test")
1616
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
17-
load(":transitions.bzl", "dwp_file")
17+
load(":transitions.bzl", "dwp_file", "transition_library_to_test_define")
1818

1919
cc_library(
2020
name = "stdlib",
@@ -139,3 +139,53 @@ toolchain(
139139
toolchain = "@@rules_foreign_cc~~tools~ninja_1.11.1_mac//:ninja_tool",
140140
toolchain_type = "@rules_foreign_cc//toolchains:ninja_toolchain",
141141
)
142+
143+
# Testing extra_target_compatible_with
144+
constraint_setting(
145+
name = "with_test_define",
146+
default_constraint_value = ":no_test_define",
147+
visibility = ["//visibility:public"],
148+
)
149+
150+
constraint_value(
151+
name = "no_test_define",
152+
constraint_setting = ":with_test_define",
153+
visibility = ["//visibility:public"],
154+
)
155+
156+
constraint_value(
157+
name = "yes_test_define",
158+
constraint_setting = ":with_test_define",
159+
visibility = ["//visibility:public"],
160+
)
161+
162+
platform(
163+
name = "platform_with_test_define",
164+
constraint_values = [
165+
":yes_test_define",
166+
],
167+
parents = ["@bazel_tools//tools:host_platform"],
168+
visibility = ["//visibility:public"],
169+
)
170+
171+
cc_library(
172+
name = "test_define_lib",
173+
srcs = ["test_define.cc"],
174+
)
175+
176+
cc_test(
177+
name = "test_define_no_define",
178+
args = ["no"],
179+
deps = [":test_define_lib"],
180+
)
181+
182+
transition_library_to_test_define(
183+
name = "test_define_lib_with_define",
184+
lib = ":test_define_lib",
185+
)
186+
187+
cc_test(
188+
name = "test_define_yes_define",
189+
args = ["yes"],
190+
deps = [":test_define_lib_with_define"],
191+
)

tests/MODULE.bazel

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,26 @@ llvm.toolchain(
6666
name = "llvm_toolchain",
6767
llvm_versions = LLVM_VERSIONS,
6868
)
69+
llvm.extra_target_compatible_with(
70+
name = "llvm_toolchain",
71+
constraints = ["@//:no_test_define"],
72+
)
6973
use_repo(llvm, "llvm_toolchain", "llvm_toolchain_llvm")
70-
7174
register_toolchains("@llvm_toolchain//:all")
7275

76+
llvm.toolchain(
77+
name = "llvm_toolchain_with_define",
78+
llvm_versions = LLVM_VERSIONS,
79+
cxx_flags = {"": ["-DTEST_DEFINE", "-stdlib=libc++"]},
80+
)
81+
llvm.extra_target_compatible_with(
82+
name = "llvm_toolchain_with_define",
83+
constraints = ["//:yes_test_define"],
84+
)
85+
use_repo(llvm, "llvm_toolchain_with_define")
86+
register_toolchains("@llvm_toolchain_with_define//:all")
87+
88+
7389
# Example toolchain with user provided URLs.
7490
# TODO(siddharthab): Add test.
7591
llvm.toolchain(

tests/test_define.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <string>
2+
#include <iostream>
3+
4+
int main(int argc, char** argv) {
5+
if (argc != 2) {
6+
std::cout << "Not enough arguments" << std::endl;
7+
return 1;
8+
}
9+
10+
std::string arg = argv[1];
11+
12+
#ifdef TEST_DEFINE
13+
if (arg != "yes") {
14+
std::cout << "TEST_DEFINE is defined but it was expected to be not defined" << std::endl;
15+
return 1;
16+
}
17+
#else
18+
if (arg != "no") {
19+
std::cout << "TEST_DEFINE is not defined but it was expected to be defined" << std::endl;
20+
return 1;
21+
}
22+
#endif
23+
return 0;
24+
}

tests/transitions.bzl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,24 @@ dwp_file = rule(
9696
),
9797
},
9898
)
99+
100+
def _transition_to_test_define_impl(_, _attr):
101+
return {"//command_line_option:platforms": "//:platform_with_test_define"}
102+
103+
_transition_to_test_define = transition(
104+
implementation = _transition_to_test_define_impl,
105+
inputs = [],
106+
outputs = ["//command_line_option:platforms"],
107+
)
108+
109+
def _transition_library_to_test_define_impl(ctx):
110+
return [
111+
ctx.attr.lib[0][CcInfo],
112+
]
113+
114+
transition_library_to_test_define = rule(
115+
implementation = _transition_library_to_test_define_impl,
116+
attrs = {
117+
"lib": attr.label(cfg = _transition_to_test_define),
118+
},
119+
)

0 commit comments

Comments
 (0)