diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index d468b1b0..7d813fda 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -16,6 +16,7 @@ build_targets_bazel6: &build_targets_bazel6 build_targets_integration: &build_targets_integration - "//..." - "//:bin_deploy.jar" + - "-//:native_test" test_targets: &test_targets - "//test/..." @@ -29,6 +30,10 @@ test_targets_bazel6: &test_targets_bazel6 test_target_integration: &test_target_integration - "//:MyTest" + - "//:native_test" + +test_target_integration_pre_bazel_8: &test_target_integration_pre_bazel_8 + - "//:MyTest" flags_workspace_integration: &flags_workspace_integration - "--noenable_bzlmod" @@ -52,7 +57,7 @@ tasks: shell_commands: - sh setup.sh build_targets: *build_targets_integration - test_targets: *test_target_integration + test_targets: *test_target_integration_pre_bazel_8 ubuntu2004_integration_workspace: name: "Bazel 7.x Integration (WORKSPACE)" bazel: "7.4.0" @@ -62,7 +67,7 @@ tasks: - sh setup.sh build_targets: *build_targets_integration build_flags: *flags_workspace_integration - test_targets: *test_target_integration + test_targets: *test_target_integration_pre_bazel_8 test_flags: *flags_workspace_integration macos: name: "Bazel 7.x" @@ -125,7 +130,7 @@ tasks: shell_commands: - sh setup.sh build_targets: *build_targets_integration - test_targets: *test_target_integration + test_targets: *test_target_integration_pre_bazel_8 macos_bazel6: name: "Bazel 6.x" bazel: 6.4.0 diff --git a/java/common/rules/impl/basic_java_library_impl.bzl b/java/common/rules/impl/basic_java_library_impl.bzl index 35b1edd1..a2f51aad 100644 --- a/java/common/rules/impl/basic_java_library_impl.bzl +++ b/java/common/rules/impl/basic_java_library_impl.bzl @@ -16,6 +16,7 @@ Common code for reuse across java_* rules """ +load("@rules_cc//cc/common:cc_common.bzl", "cc_common") load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") load("//java/common/rules:android_lint.bzl", "android_lint_subrule") load("//java/private:boot_class_path_info.bzl", "BootClassPathInfo") @@ -26,6 +27,8 @@ load(":proguard_validation.bzl", "validate_proguard_specs") # copybara: default multiline visibility +_EMPTY_CC_INFO = CcInfo() + def _filter_srcs(srcs, ext): return [f for f in srcs if f.extension == ext] @@ -124,6 +127,7 @@ def basic_java_library( resources = list(resources) resources.extend(properties) + native_libraries = _collect_native_libraries(deps, runtime_deps, exports) java_info, compilation_info = compile_action( ctx, ctx.outputs.classjar, @@ -138,7 +142,7 @@ def basic_java_library( resources, resource_jars, classpath_resources, - _collect_native_libraries(deps, runtime_deps, exports), + native_libraries, javacopts, neverlink, ctx.fragments.java.strict_java_deps, @@ -150,6 +154,19 @@ def basic_java_library( ) target = {"JavaInfo": java_info} + if native_libraries: + dependencies_cc_info = cc_common.merge_cc_infos(cc_infos = native_libraries) + + # Native dependencies have the same semantics as + # `cc_library#implementation_deps`. We only want to propagate + # `Cc{Debug,Linking}Context` to libraries depending on this. + target["CcInfo"] = CcInfo( + linking_context = dependencies_cc_info.linking_context, + debug_context = dependencies_cc_info.debug_context(), + ) + else: + target["CcInfo"] = _EMPTY_CC_INFO + output_groups = dict( compilation_outputs = compilation_info.files_to_build, _source_jars = java_info.transitive_source_jars, diff --git a/test/repo/BUILD.bazel b/test/repo/BUILD.bazel index 7f6887a9..80e26d89 100644 --- a/test/repo/BUILD.bazel +++ b/test/repo/BUILD.bazel @@ -1,9 +1,30 @@ +load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test") load("@rules_java//java:defs.bzl", "java_binary", "java_library", "java_test") # copybara-use-repo-external-label load("@rules_java//toolchains:default_java_toolchain.bzl", "default_java_toolchain") # copybara-use-repo-external-label +cc_library( + name = "native_dep", + srcs = [ + "src/native_dep.cc", + ], +) + java_library( name = "lib", srcs = ["src/Main.java"], + deps = [ + ":native_dep", + ], +) + +cc_test( + name = "native_test", + srcs = [ + "src/native_test.cc", + ], + deps = [ + ":lib", + ], ) java_binary( diff --git a/test/repo/MODULE.bazel b/test/repo/MODULE.bazel index c582bf40..7b6fb7bd 100644 --- a/test/repo/MODULE.bazel +++ b/test/repo/MODULE.bazel @@ -1,5 +1,6 @@ module(name = "integration_test_repo") +bazel_dep(name = "rules_cc", version = "0.0.15") bazel_dep(name = "rules_java", version = "7.5.0") archive_override( module_name = "rules_java", diff --git a/test/repo/src/native_dep.cc b/test/repo/src/native_dep.cc new file mode 100644 index 00000000..2b0a5c90 --- /dev/null +++ b/test/repo/src/native_dep.cc @@ -0,0 +1,3 @@ +int my_number() { + return 42; +} diff --git a/test/repo/src/native_test.cc b/test/repo/src/native_test.cc new file mode 100644 index 00000000..ae2a9f09 --- /dev/null +++ b/test/repo/src/native_test.cc @@ -0,0 +1,12 @@ +#include + +int my_number(); + +int main() { + int actual = my_number(); + if (actual != 42) { + std::cerr << "Expected my_number() to return 42, got " << actual << std::endl; + return 1; + } + return 0; +}