Skip to content

Commit 748fbdf

Browse files
dzbarskycopybara-github
authored andcommitted
Support env for validate_static_library
Copybara Import from #590 BEGIN_PUBLIC Support env for validate_static_library (#590) We need a way to pass a flag corresponding to "is_darwin" to this action to do the equivalent of https://github.com/bazelbuild/rules_cc/blob/d81c50d0732241d07ea66e9f12982a145fb6d26d/cc/private/toolchain/unix_cc_configure.bzl#L430-L431 Passing via ENV is the easiest way. (If the implementation is a prebuilt binary instead of a shell script, it can't really be templated in a repo rule) Closes #590 END_PUBLIC COPYBARA_INTEGRATE_REVIEW=#590 from dzbarsky:zbarsky/validate b52c1bb PiperOrigin-RevId: 883290004 Change-Id: I5eb22e3de2bf82c4c0edc80309ce37b24258df46
1 parent 0b2df85 commit 748fbdf

5 files changed

Lines changed: 151 additions & 0 deletions

File tree

cc/private/rules_impl/cc_static_library.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ def _validate_static_library(*, name, actions, cc_toolchain, feature_configurati
122122
args.add(static_library)
123123
args.add(validation_output)
124124

125+
env = cc_common.get_environment_variables(
126+
feature_configuration = feature_configuration,
127+
action_name = ACTION_NAMES.validate_static_library,
128+
variables = cc_common.empty_variables(),
129+
)
130+
125131
execution_requirements_keys = cc_common.get_execution_requirements(
126132
feature_configuration = feature_configuration,
127133
action_name = ACTION_NAMES.validate_static_library,
@@ -130,6 +136,7 @@ def _validate_static_library(*, name, actions, cc_toolchain, feature_configurati
130136
actions.run(
131137
executable = validator_path,
132138
arguments = [args],
139+
env = env,
133140
execution_requirements = {k: "" for k in execution_requirements_keys},
134141
inputs = depset(
135142
direct = [static_library],
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright 2026 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load("//cc/toolchains:args.bzl", "cc_args")
16+
load("//cc/toolchains:tool.bzl", "cc_tool")
17+
load("//cc/toolchains:tool_map.bzl", "cc_tool_map")
18+
load("//cc/toolchains:toolchain.bzl", "cc_toolchain")
19+
load(":validate_static_library_env_test.bzl", "maybe_define_validate_static_library_env_targets")
20+
21+
package(default_visibility = ["//visibility:private"])
22+
23+
constraint_setting(name = "toolchain_selector")
24+
25+
constraint_value(
26+
name = "use_test_toolchain",
27+
constraint_setting = ":toolchain_selector",
28+
)
29+
30+
platform(
31+
name = "test_platform",
32+
constraint_values = [":use_test_toolchain"],
33+
)
34+
35+
cc_tool(
36+
name = "archive_tool",
37+
src = "tools/archive_tool.sh",
38+
)
39+
40+
cc_tool(
41+
name = "validate_tool",
42+
src = "tools/validate_tool.sh",
43+
)
44+
45+
cc_tool_map(
46+
name = "tool_map",
47+
tools = {
48+
"//cc/toolchains/actions:cpp_link_static_library": ":archive_tool",
49+
"//cc/toolchains/actions:validate_static_library": ":validate_tool",
50+
},
51+
)
52+
53+
cc_args(
54+
name = "archive_output_arg",
55+
actions = ["//cc/toolchains/actions:cpp_link_static_library"],
56+
args = ["{output_execpath}"],
57+
format = {
58+
"output_execpath": "//cc/toolchains/variables:output_execpath",
59+
},
60+
requires_not_none = "//cc/toolchains/variables:output_execpath",
61+
)
62+
63+
cc_args(
64+
name = "validate_static_library_env_args",
65+
actions = ["//cc/toolchains/actions:validate_static_library"],
66+
env = {
67+
"VALIDATE_STATIC_LIBRARY_ENV": "expected",
68+
},
69+
)
70+
71+
cc_toolchain(
72+
name = "test_cc_toolchain",
73+
args = [
74+
":archive_output_arg",
75+
":validate_static_library_env_args",
76+
],
77+
tool_map = ":tool_map",
78+
)
79+
80+
toolchain(
81+
name = "test_cc_toolchain_registration",
82+
target_compatible_with = [":use_test_toolchain"],
83+
toolchain = ":test_cc_toolchain",
84+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
85+
)
86+
87+
maybe_define_validate_static_library_env_targets()

tests/validate_static_library_env/tools/archive_tool.sh

Whitespace-only changes.

tests/validate_static_library_env/tools/validate_tool.sh

Whitespace-only changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2026 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Analysis test for validate_static_library environment propagation."""
15+
16+
load("@bazel_features//private:util.bzl", _bazel_version_ge = "ge")
17+
load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
18+
load("@rules_testing//lib:util.bzl", "util")
19+
load("//cc:cc_static_library.bzl", "cc_static_library")
20+
21+
def _validate_static_library_env_test_impl(env, target):
22+
env.expect.that_target(target).action_named("ValidateStaticLibrary").env().contains_at_least({
23+
"VALIDATE_STATIC_LIBRARY_ENV": "expected",
24+
})
25+
26+
def validate_static_library_env_test(name, target):
27+
analysis_test(
28+
name = name,
29+
target = target,
30+
impl = _validate_static_library_env_test_impl,
31+
config_settings = {
32+
"//command_line_option:extra_toolchains": [
33+
Label("//tests/validate_static_library_env:test_cc_toolchain_registration"),
34+
],
35+
"//command_line_option:platforms": [
36+
Label("@rules_cc//tests/validate_static_library_env:test_platform"),
37+
],
38+
},
39+
)
40+
41+
def maybe_define_validate_static_library_env_targets():
42+
# cc_static_library is implemented in rules_cc only for Bazel 9+.
43+
# For older Bazel versions, the native rule is used and does not wire
44+
# env vars from rules_cc toolchains for ValidateStaticLibrary.
45+
if not _bazel_version_ge("9.0.0-pre.20250911"):
46+
return
47+
48+
util.helper_target(
49+
cc_static_library,
50+
name = "env_check_lib",
51+
deps = [],
52+
)
53+
54+
validate_static_library_env_test(
55+
name = "validate_static_library_env_test",
56+
target = ":env_check_lib",
57+
)

0 commit comments

Comments
 (0)