blob: 02b5784e535e512550151b37a608a3cc952bc2db [file]
"""Analysis test for tool paths that refer to files generated by the build."""
load("@bazel_features//:features.bzl", "bazel_features")
load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
load("//cc:cc_toolchain_config_lib.bzl", "tool_path") # buildifier: disable=deprecated-function
load("//cc/common:cc_common.bzl", "cc_common")
def _generated_tool_cc_toolchain_config_impl(ctx):
return cc_common.create_cc_toolchain_config_info(
ctx = ctx,
toolchain_identifier = "generated-tool-path-test",
host_system_name = "local",
target_system_name = "local",
target_cpu = "k8",
target_libc = "local",
compiler = "compiler",
abi_version = "local",
abi_libc_version = "local",
tool_paths = [
# The path of a generated file is relative to the execution root rather than to the
# package of the toolchain.
tool_path(name = "gcc", path = ctx.file.gcc.path),
tool_path(name = "ar", path = "/usr/bin/ar"),
tool_path(name = "cpp", path = "/usr/bin/cpp"),
tool_path(name = "ld", path = "/usr/bin/ld"),
tool_path(name = "nm", path = "/usr/bin/nm"),
tool_path(name = "objdump", path = "/usr/bin/objdump"),
tool_path(name = "strip", path = "/usr/bin/strip"),
],
)
generated_tool_cc_toolchain_config = rule(
implementation = _generated_tool_cc_toolchain_config_impl,
attrs = {
"gcc": attr.label(
allow_single_file = True,
mandatory = True,
),
},
)
def _generated_tool_path_test_impl(env, target):
all_files = target[cc_common.CcToolchainInfo].all_files.to_list()
gcc = [file for file in all_files if file.basename == "gcc.sh"]
env.expect.that_collection(gcc).has_size(1)
cc = target[platform_common.TemplateVariableInfo].variables["CC"]
env.expect.that_str(cc).equals(gcc[0].path)
def generated_tool_path_test(name, target):
analysis_test(
name = name,
target = target,
impl = _generated_tool_path_test_impl,
config_settings = {
"//command_line_option:extra_toolchains": [
Label("//tests/generated_tool_path:cc_toolchain_registration"),
],
"//command_line_option:platforms": [
Label("//tests/generated_tool_path:test_platform"),
],
},
)
def maybe_define_generated_tool_path_targets():
# cc_toolchain is implemented in rules_cc only for Bazel 9+. For older Bazel versions, the
# native rule is used.
if not bazel_features.cc.cc_common_is_in_rules_cc:
return
generated_tool_path_test(
name = "generated_tool_path_test",
target = ":current_cc_toolchain",
)