Skip to content

Commit 79bf77c

Browse files
committed
Propagate CcInfo for native dependencies
This will, in a follow-up, allow us to create and link `java_{binary,test}#laucher` as the docs suggest. Unfortunately, `JavaInfo#transitive_native_libraries` fails to propagate sufficient information about transitive dependencies of the native libraries, so linking fails because of missing symbols from transitive libraries that should be there.
1 parent 767e441 commit 79bf77c

File tree

6 files changed

+61
-4
lines changed

6 files changed

+61
-4
lines changed

.bazelci/presubmit.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ build_targets_bazel6: &build_targets_bazel6
1616
build_targets_integration: &build_targets_integration
1717
- "//..."
1818
- "//:bin_deploy.jar"
19+
- "-//:native_test"
1920

2021
test_targets: &test_targets
2122
- "//test/..."
@@ -29,6 +30,10 @@ test_targets_bazel6: &test_targets_bazel6
2930

3031
test_target_integration: &test_target_integration
3132
- "//:MyTest"
33+
- "//:native_test"
34+
35+
test_target_integration_pre_bazel_8: &test_target_integration_pre_bazel_8
36+
- "//:MyTest"
3237

3338
flags_workspace_integration: &flags_workspace_integration
3439
- "--noenable_bzlmod"
@@ -52,7 +57,7 @@ tasks:
5257
shell_commands:
5358
- sh setup.sh
5459
build_targets: *build_targets_integration
55-
test_targets: *test_target_integration
60+
test_targets: *test_target_integration_pre_bazel_8
5661
ubuntu2004_integration_workspace:
5762
name: "Bazel 7.x Integration (WORKSPACE)"
5863
bazel: "7.4.0"
@@ -62,7 +67,7 @@ tasks:
6267
- sh setup.sh
6368
build_targets: *build_targets_integration
6469
build_flags: *flags_workspace_integration
65-
test_targets: *test_target_integration
70+
test_targets: *test_target_integration_pre_bazel_8
6671
test_flags: *flags_workspace_integration
6772
macos:
6873
name: "Bazel 7.x"
@@ -125,7 +130,7 @@ tasks:
125130
shell_commands:
126131
- sh setup.sh
127132
build_targets: *build_targets_integration
128-
test_targets: *test_target_integration
133+
test_targets: *test_target_integration_pre_bazel_8
129134
macos_bazel6:
130135
name: "Bazel 6.x"
131136
bazel: 6.4.0

java/common/rules/impl/basic_java_library_impl.bzl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Common code for reuse across java_* rules
1717
"""
1818

19+
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
1920
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
2021
load("//java/common/rules:android_lint.bzl", "android_lint_subrule")
2122
load("//java/private:boot_class_path_info.bzl", "BootClassPathInfo")
@@ -124,6 +125,7 @@ def basic_java_library(
124125
resources = list(resources)
125126
resources.extend(properties)
126127

128+
native_libraries = _collect_native_libraries(deps, runtime_deps, exports)
127129
java_info, compilation_info = compile_action(
128130
ctx,
129131
ctx.outputs.classjar,
@@ -138,7 +140,7 @@ def basic_java_library(
138140
resources,
139141
resource_jars,
140142
classpath_resources,
141-
_collect_native_libraries(deps, runtime_deps, exports),
143+
native_libraries,
142144
javacopts,
143145
neverlink,
144146
ctx.fragments.java.strict_java_deps,
@@ -150,6 +152,19 @@ def basic_java_library(
150152
)
151153
target = {"JavaInfo": java_info}
152154

155+
if native_libraries:
156+
dependencies_cc_info = cc_common.merge_cc_infos(cc_infos = native_libraries)
157+
158+
# Native dependencies have the same semantics as
159+
# `cc_library#implementation_deps`. We only want to propagate
160+
# `Cc{Debug,Linking}Context` to libraries depending on this.
161+
target["CcInfo"] = CcInfo(
162+
linking_context = dependencies_cc_info.linking_context,
163+
debug_context = dependencies_cc_info.debug_context(),
164+
)
165+
else:
166+
target["CcInfo"] = CcInfo()
167+
153168
output_groups = dict(
154169
compilation_outputs = compilation_info.files_to_build,
155170
_source_jars = java_info.transitive_source_jars,

test/repo/BUILD.bazel

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
12
load("@rules_java//java:defs.bzl", "java_binary", "java_library", "java_test") # copybara-use-repo-external-label
23
load("@rules_java//toolchains:default_java_toolchain.bzl", "default_java_toolchain") # copybara-use-repo-external-label
34

5+
cc_library(
6+
name = "native_dep",
7+
srcs = [
8+
"src/native_dep.cc",
9+
],
10+
)
11+
412
java_library(
513
name = "lib",
614
srcs = ["src/Main.java"],
15+
deps = [
16+
":native_dep",
17+
],
18+
)
19+
20+
cc_test(
21+
name = "native_test",
22+
srcs = [
23+
"src/native_test.cc",
24+
],
25+
deps = [
26+
":lib",
27+
],
728
)
829

930
java_binary(

test/repo/MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module(name = "integration_test_repo")
22

3+
bazel_dep(name = "rules_cc", version = "0.0.15")
34
bazel_dep(name = "rules_java", version = "7.5.0")
45
archive_override(
56
module_name = "rules_java",

test/repo/src/native_dep.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int my_number() {
2+
return 42;
3+
}

test/repo/src/native_test.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <iostream>
2+
3+
int my_number();
4+
5+
int main() {
6+
int actual = my_number();
7+
if (actual != 42) {
8+
std::cerr << "Expected my_number() to return 42, got " << actual << std::endl;
9+
return 1;
10+
}
11+
return 0;
12+
}

0 commit comments

Comments
 (0)