diff --git a/cc/private/rules_impl/cc_static_library.bzl b/cc/private/rules_impl/cc_static_library.bzl index 0dd42ce55..a171488bd 100644 --- a/cc/private/rules_impl/cc_static_library.bzl +++ b/cc/private/rules_impl/cc_static_library.bzl @@ -122,6 +122,12 @@ def _validate_static_library(*, name, actions, cc_toolchain, feature_configurati args.add(static_library) args.add(validation_output) + env = cc_common.get_environment_variables( + feature_configuration = feature_configuration, + action_name = ACTION_NAMES.validate_static_library, + variables = cc_common.empty_variables(), + ) + execution_requirements_keys = cc_common.get_execution_requirements( feature_configuration = feature_configuration, action_name = ACTION_NAMES.validate_static_library, @@ -130,6 +136,7 @@ def _validate_static_library(*, name, actions, cc_toolchain, feature_configurati actions.run( executable = validator_path, arguments = [args], + env = env, execution_requirements = {k: "" for k in execution_requirements_keys}, inputs = depset( direct = [static_library], diff --git a/tests/validate_static_library_env/BUILD b/tests/validate_static_library_env/BUILD new file mode 100644 index 000000000..489a820d1 --- /dev/null +++ b/tests/validate_static_library_env/BUILD @@ -0,0 +1,87 @@ +# Copyright 2026 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("//cc/toolchains:args.bzl", "cc_args") +load("//cc/toolchains:tool.bzl", "cc_tool") +load("//cc/toolchains:tool_map.bzl", "cc_tool_map") +load("//cc/toolchains:toolchain.bzl", "cc_toolchain") +load(":validate_static_library_env_test.bzl", "maybe_define_validate_static_library_env_targets") + +package(default_visibility = ["//visibility:private"]) + +constraint_setting(name = "toolchain_selector") + +constraint_value( + name = "use_test_toolchain", + constraint_setting = ":toolchain_selector", +) + +platform( + name = "test_platform", + constraint_values = [":use_test_toolchain"], +) + +cc_tool( + name = "archive_tool", + src = "tools/archive_tool.sh", +) + +cc_tool( + name = "validate_tool", + src = "tools/validate_tool.sh", +) + +cc_tool_map( + name = "tool_map", + tools = { + "//cc/toolchains/actions:cpp_link_static_library": ":archive_tool", + "//cc/toolchains/actions:validate_static_library": ":validate_tool", + }, +) + +cc_args( + name = "archive_output_arg", + actions = ["//cc/toolchains/actions:cpp_link_static_library"], + args = ["{output_execpath}"], + format = { + "output_execpath": "//cc/toolchains/variables:output_execpath", + }, + requires_not_none = "//cc/toolchains/variables:output_execpath", +) + +cc_args( + name = "validate_static_library_env_args", + actions = ["//cc/toolchains/actions:validate_static_library"], + env = { + "VALIDATE_STATIC_LIBRARY_ENV": "expected", + }, +) + +cc_toolchain( + name = "test_cc_toolchain", + args = [ + ":archive_output_arg", + ":validate_static_library_env_args", + ], + tool_map = ":tool_map", +) + +toolchain( + name = "test_cc_toolchain_registration", + target_compatible_with = [":use_test_toolchain"], + toolchain = ":test_cc_toolchain", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) + +maybe_define_validate_static_library_env_targets() diff --git a/tests/validate_static_library_env/tools/archive_tool.sh b/tests/validate_static_library_env/tools/archive_tool.sh new file mode 100755 index 000000000..e69de29bb diff --git a/tests/validate_static_library_env/tools/validate_tool.sh b/tests/validate_static_library_env/tools/validate_tool.sh new file mode 100755 index 000000000..e69de29bb diff --git a/tests/validate_static_library_env/validate_static_library_env_test.bzl b/tests/validate_static_library_env/validate_static_library_env_test.bzl new file mode 100644 index 000000000..428373b8c --- /dev/null +++ b/tests/validate_static_library_env/validate_static_library_env_test.bzl @@ -0,0 +1,57 @@ +# Copyright 2026 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Analysis test for validate_static_library environment propagation.""" + +load("@bazel_features//private:util.bzl", _bazel_version_ge = "ge") +load("@rules_testing//lib:analysis_test.bzl", "analysis_test") +load("@rules_testing//lib:util.bzl", "util") +load("//cc:cc_static_library.bzl", "cc_static_library") + +def _validate_static_library_env_test_impl(env, target): + env.expect.that_target(target).action_named("ValidateStaticLibrary").env().contains_at_least({ + "VALIDATE_STATIC_LIBRARY_ENV": "expected", + }) + +def validate_static_library_env_test(name, target): + analysis_test( + name = name, + target = target, + impl = _validate_static_library_env_test_impl, + config_settings = { + "//command_line_option:extra_toolchains": [ + Label("//tests/validate_static_library_env:test_cc_toolchain_registration"), + ], + "//command_line_option:platforms": [ + Label("@rules_cc//tests/validate_static_library_env:test_platform"), + ], + }, + ) + +def maybe_define_validate_static_library_env_targets(): + # cc_static_library is implemented in rules_cc only for Bazel 9+. + # For older Bazel versions, the native rule is used and does not wire + # env vars from rules_cc toolchains for ValidateStaticLibrary. + if not _bazel_version_ge("9.0.0-pre.20250911"): + return + + util.helper_target( + cc_static_library, + name = "env_check_lib", + deps = [], + ) + + validate_static_library_env_test( + name = "validate_static_library_env_test", + target = ":env_check_lib", + )