Skip to content

Commit cc14213

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 cc14213

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

java/common/rules/impl/basic_java_library_impl.bzl

+16-1
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

+21
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

+1
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

+3
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

+12
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)