diff --git a/cc/private/rules_impl/cc_import.bzl b/cc/private/rules_impl/cc_import.bzl index bf0431f6..b27403c6 100644 --- a/cc/private/rules_impl/cc_import.bzl +++ b/cc/private/rules_impl/cc_import.bzl @@ -187,7 +187,37 @@ def _cc_import_impl(ctx): cc_infos.append(dep[CcInfo]) merged_cc_info = cc_common.merge_cc_infos(direct_cc_infos = [this_cc_info], cc_infos = cc_infos) - return [merged_cc_info] + runfiles_list = [] + for data_dep in ctx.attr.data: + if data_dep[DefaultInfo].data_runfiles.files: + runfiles_list.append(data_dep[DefaultInfo].data_runfiles) + else: + # This branch ensures interop with custom Starlark rules following + # https://bazel.build/extending/rules#runfiles_features_to_avoid + runfiles_list.append(ctx.runfiles(transitive_files = data_dep[DefaultInfo].files)) + runfiles_list.append(data_dep[DefaultInfo].default_runfiles) + + for dep in ctx.attr.deps: + runfiles_list.append(dep[DefaultInfo].default_runfiles) + + runfiles = ctx.runfiles().merge_all(runfiles_list) + + if linking_context: + default_runfiles = ctx.runfiles(files = cc_helper.get_dynamic_libraries_for_runtime(linking_context, True)) + default_runfiles = runfiles.merge(default_runfiles) + data_runfiles = ctx.runfiles(files = cc_helper.get_dynamic_libraries_for_runtime(linking_context, False)) + data_runfiles = runfiles.merge(data_runfiles) + else: + default_runfiles = runfiles + data_runfiles = runfiles + + return [ + merged_cc_info, + DefaultInfo( + default_runfiles = default_runfiles, + data_runfiles = data_runfiles, + ), + ] cc_import = rule( implementation = _cc_import_impl, @@ -443,6 +473,11 @@ most build rules."""), "data": attr.label_list( allow_files = True, flags = ["SKIP_CONSTRAINTS_OVERRIDE"], + doc = """ +The list of files needed by this library at runtime. +See general comments about data +at Typical attributes defined by +most build rules.""", ), "defines": attr.string_list(doc = """ List of defines to add to the compile line of this and all dependent targets. diff --git a/tests/cc/common/BUILD b/tests/cc/common/BUILD index 91acc88d..c7274094 100644 --- a/tests/cc/common/BUILD +++ b/tests/cc/common/BUILD @@ -1,3 +1,6 @@ load(":cc_binary_configured_target_tests.bzl", "cc_binary_configured_target_tests") +load(":cc_import_configured_target_tests.bzl", "cc_import_configured_target_tests") cc_binary_configured_target_tests(name = "cc_binary_configured_target_tests") + +cc_import_configured_target_tests(name = "cc_import_configured_target_tests") diff --git a/tests/cc/common/cc_import_configured_target_tests.bzl b/tests/cc/common/cc_import_configured_target_tests.bzl new file mode 100644 index 00000000..34747f75 --- /dev/null +++ b/tests/cc/common/cc_import_configured_target_tests.bzl @@ -0,0 +1,36 @@ +"""Tests for cc_import.""" + +load("@bazel_features//private:util.bzl", _bazel_version_ge = "ge") +load("@rules_testing//lib:truth.bzl", "matching") +load("@rules_testing//lib:util.bzl", "util") +load("//cc:cc_import.bzl", "cc_import") +load("//tests/cc/testutil:cc_analysis_test.bzl", "cc_analysis_test", "cc_test_suite") + +_use_rules_cc_impls = _bazel_version_ge("9.0.0-pre.20250911") + +def _test_data_in_runfiles(name, **kwargs): + util.helper_target( + cc_import, + name = name + "/import_with_data", + hdrs = ["header.h"], + data = ["data_file.txt"], + ) + cc_analysis_test( + name = name, + impl = _test_data_in_runfiles_impl, + target = name + "/import_with_data", + **kwargs + ) + +def _test_data_in_runfiles_impl(env, target): + target = env.expect.that_target(target) + target.runfiles().contains_predicate(matching.str_endswith("/data_file.txt")) + target.data_runfiles().contains_predicate(matching.str_endswith("/data_file.txt")) + +def cc_import_configured_target_tests(name): + cc_test_suite( + name = name, + tests = [ + _test_data_in_runfiles, + ] if _use_rules_cc_impls else [], + ) diff --git a/tests/cc/common/data_file.txt b/tests/cc/common/data_file.txt new file mode 100644 index 00000000..082b3465 --- /dev/null +++ b/tests/cc/common/data_file.txt @@ -0,0 +1 @@ +test data diff --git a/tests/cc/common/header.h b/tests/cc/common/header.h new file mode 100644 index 00000000..d55bc773 --- /dev/null +++ b/tests/cc/common/header.h @@ -0,0 +1 @@ +// Empty header for testing.