Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion cc/private/rules_impl/cc_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -443,6 +473,11 @@ most build rules</a>."""),
"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 <code>data</code>
at <a href="${link common-definitions#typical-attributes}">Typical attributes defined by
most build rules</a>.""",
),
"defines": attr.string_list(doc = """
List of defines to add to the compile line of this and all dependent targets.
Expand Down
3 changes: 3 additions & 0 deletions tests/cc/common/BUILD
Original file line number Diff line number Diff line change
@@ -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")
36 changes: 36 additions & 0 deletions tests/cc/common/cc_import_configured_target_tests.bzl
Original file line number Diff line number Diff line change
@@ -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 [],
)
1 change: 1 addition & 0 deletions tests/cc/common/data_file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test data
1 change: 1 addition & 0 deletions tests/cc/common/header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty header for testing.