From a3774418f437fa09f91e2703461ad81994da63a9 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Sat, 27 Apr 2024 13:16:53 +0200 Subject: [PATCH 01/10] Update CI setup --- .bazelrc | 4 ++-- .github/workflows/check-formatting.yml | 2 +- .github/workflows/run-tests-externally.yml | 24 ++++++++++++++-------- .github/workflows/run-tests-internally.yml | 4 ++-- .gitignore | 2 ++ WORKSPACE | 2 +- jni/defs.bzl | 2 +- jni/tools/libjvm_stub/BUILD.bazel | 2 +- tests/analysis/cc_jni_library/tests.bzl | 6 ++++-- tests/analysis/jni_headers/tests.bzl | 2 +- tests/verify_coverage.sh | 2 ++ 11 files changed, 33 insertions(+), 19 deletions(-) diff --git a/.bazelrc b/.bazelrc index 95be903..1d053c7 100644 --- a/.bazelrc +++ b/.bazelrc @@ -9,5 +9,5 @@ test --noincompatible_strict_action_env build:ci --repo_env=CC=clang build:ci --features=layering_check -# Requires Bazel 5. -build:bzlmod --experimental_enable_bzlmod +build:bzlmod --enable_bzlmod +build:workspace --noexperimental_enable_bzlmod diff --git a/.github/workflows/check-formatting.yml b/.github/workflows/check-formatting.yml index eefecf9..10eaf52 100644 --- a/.github/workflows/check-formatting.yml +++ b/.github/workflows/check-formatting.yml @@ -19,7 +19,7 @@ jobs: - name: Setup Go environment uses: actions/setup-go@v2 with: - go-version: '^1.15.5' + go-version: '^1.22' - name: Install formatters run: | diff --git a/.github/workflows/run-tests-externally.yml b/.github/workflows/run-tests-externally.yml index 083d6f8..2820d63 100644 --- a/.github/workflows/run-tests-externally.yml +++ b/.github/workflows/run-tests-externally.yml @@ -19,33 +19,41 @@ jobs: matrix: bazel: [5.0.0, latest, last_green] bazel_mode: [workspace, module] - os: [ubuntu-latest, macos-latest, windows-2019] - jdk: [8, 11, 17] + os: [ubuntu-latest, macos-13, macos-latest, windows-2019] + jdk: [8, 11, 17, 21] exclude: - bazel: 5.0.0 jdk: 11 - bazel: 5.0.0 jdk: 17 + - bazel: 5.0.0 + jdk: 21 - bazel: 5.0.0 bazel_mode: module - bazel: last_green jdk: 8 - bazel: last_green jdk: 11 - - bazel_mode: module - jdk: 8 - - bazel_mode: module - jdk: 11 + - bazel: last_green + jdk: 17 + - bazel_mode: workspace + jdk: 17 + - bazel_mode: workspace + jdk: 21 include: - os: ubuntu-latest cache: "/home/runner/.cache/bazel-disk" - - os: macos-latest + - os: macos-13 cache: "/private/var/tmp/bazel-disk" bazel_macos_args: "--xcode_version_config=//.github:host_xcodes" + - os: macos-latest + cache: "/private/var/tmp/bazel-disk" - os: windows-2019 cache: "C:\\tmp\\bazel-disk" - bazel_mode: module bazel_extra_args: "--config=bzlmod" + - bazel_mode: workspace + bazel_extra_args: "--config=workspace" name: Test externally (${{ matrix.os }}, Bazel ${{ matrix.bazel }} ${{ matrix.bazel_mode }}, JDK ${{ matrix.jdk }}) env: BAZELISK_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -76,7 +84,7 @@ jobs: run: bazelisk test --config=ci --disk_cache=${{ matrix.cache }} --profile=${{ github.workspace }}/profile.gz ${{ matrix.bazel_extra_args }} ${{ matrix.bazel_macos_args }} //... - name: Check coverage - if: ${{ (matrix.os == 'ubuntu-latest') && (matrix.jdk == '11') }} + if: ${{ (matrix.os == 'ubuntu-latest') && (matrix.jdk == '17') }} env: USE_BAZEL_VERSION: ${{ matrix.bazel }} working-directory: ./tests diff --git a/.github/workflows/run-tests-internally.yml b/.github/workflows/run-tests-internally.yml index 2cb03fa..6b2b8da 100644 --- a/.github/workflows/run-tests-internally.yml +++ b/.github/workflows/run-tests-internally.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest env: BAZELISK_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Build and test internally (ubuntu-latest, Bazel last_green workspace, JDK 17) + name: Build and test internally (ubuntu-latest, Bazel last_green workspace, JDK 21) steps: - uses: actions/checkout@v2 @@ -24,7 +24,7 @@ jobs: - name: Set up JDK uses: actions/setup-java@v1 with: - java-version: 17 + java-version: 21 - name: Mount Bazel disk cache uses: actions/cache@v2 diff --git a/.gitignore b/.gitignore index d3ca90c..a6339ce 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .ijwb .clwb +.vscode bazel-* +MODULE.bazel.lock diff --git a/WORKSPACE b/WORKSPACE index a62c1f7..5f0c6bd 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,7 +1,7 @@ workspace(name = "fmeum_rules_jni") -load("//jni:repositories.bzl", "rules_jni_dependencies") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_jar") +load("//jni:repositories.bzl", "rules_jni_dependencies") rules_jni_dependencies() diff --git a/jni/defs.bzl b/jni/defs.bzl index 299597d..3925749 100644 --- a/jni/defs.bzl +++ b/jni/defs.bzl @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. +load("//jni/internal:cc_jni_library.bzl", _cc_jni_library = "cc_jni_library") load("//jni/internal:java_jni_library.bzl", _java_jni_library = "java_jni_library") load("//jni/internal:jni_headers.bzl", _jni_headers = "jni_headers") -load("//jni/internal:cc_jni_library.bzl", _cc_jni_library = "cc_jni_library") java_jni_library = _java_jni_library jni_headers = _jni_headers diff --git a/jni/tools/libjvm_stub/BUILD.bazel b/jni/tools/libjvm_stub/BUILD.bazel index f176f01..6f88bf9 100644 --- a/jni/tools/libjvm_stub/BUILD.bazel +++ b/jni/tools/libjvm_stub/BUILD.bazel @@ -1,6 +1,6 @@ +load("//jni/internal:common.bzl", "make_root_relative", "merge_cc_infos") load(":current_java_runtime.bzl", "current_java_runtime") load(":current_repository.bzl", "RULES_JNI_REPOSITORY_DEFINE") -load("//jni/internal:common.bzl", "make_root_relative", "merge_cc_infos") merge_cc_infos( name = "libjvm_stub_with_jni", diff --git a/tests/analysis/cc_jni_library/tests.bzl b/tests/analysis/cc_jni_library/tests.bzl index 291e1f4..47ec5ff 100644 --- a/tests/analysis/cc_jni_library/tests.bzl +++ b/tests/analysis/cc_jni_library/tests.bzl @@ -87,9 +87,11 @@ def _get_host_constraint_value(constraint_setting): def _get_host_legacy_cpu(): cpu = _get_host_constraint_value("cpu") - if cpu != "x86_64": - fail("This test requires the host CPU to be x86_64, got: %s" % cpu) os = _get_host_constraint_value("os") + if os == "osx" and cpu == "aarch64": + return "darwin_arm64" + if cpu != "x86_64": + fail("This test requires the host CPU to be x86_64 or darwin_arm64, got: %s" % cpu) if os == "linux": return "k8" elif os == "osx": diff --git a/tests/analysis/jni_headers/tests.bzl b/tests/analysis/jni_headers/tests.bzl index 99931fc..93c260f 100644 --- a/tests/analysis/jni_headers/tests.bzl +++ b/tests/analysis/jni_headers/tests.bzl @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") load("@bazel_skylib//lib:new_sets.bzl", "sets") +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") load("@fmeum_rules_jni//jni:defs.bzl", "jni_headers") def _provider_test_impl(ctx): diff --git a/tests/verify_coverage.sh b/tests/verify_coverage.sh index d81fef4..03515c6 100755 --- a/tests/verify_coverage.sh +++ b/tests/verify_coverage.sh @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +set -euo pipefail + report=bazel-out/_coverage/_coverage_report.dat find native_loader/src/main libjvm_stub -type f \ From b9787bf30242337c9cfaa0849502e5011b6214cd Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Sat, 27 Apr 2024 13:41:36 +0200 Subject: [PATCH 02/10] apple_support --- .bazelrc | 2 ++ MODULE.bazel | 2 ++ WORKSPACE | 13 +++++++++++++ tests/MODULE.bazel | 1 + tests/WORKSPACE | 15 ++++++++++++++- 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/.bazelrc b/.bazelrc index 1d053c7..d55e179 100644 --- a/.bazelrc +++ b/.bazelrc @@ -11,3 +11,5 @@ build:ci --features=layering_check build:bzlmod --enable_bzlmod build:workspace --noexperimental_enable_bzlmod + +test --test_output=errors diff --git a/MODULE.bazel b/MODULE.bazel index 9bf414d..6dfbc67 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -8,6 +8,8 @@ bazel_dep(name = "bazel_skylib", version = "1.3.0") bazel_dep(name = "platforms", version = "0.0.5") bazel_dep(name = "rules_license", version = "0.0.7") +bazel_dep(name = "apple_support", version = "1.15.1", dev_dependency = True, repo_name = "build_bazel_apple_support") + download_jdk_deps = use_extension( "@rules_jni//bzlmod:extensions.bzl", "download_jdk_deps", diff --git a/WORKSPACE b/WORKSPACE index 5f0c6bd..f9a66fc 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -11,6 +11,19 @@ local_repository( path = "tests", ) +http_archive( + name = "build_bazel_apple_support", + sha256 = "c4bb2b7367c484382300aee75be598b92f847896fb31bbd22f3a2346adf66a80", + url = "https://github.com/bazelbuild/apple_support/releases/download/1.15.1/apple_support.1.15.1.tar.gz", +) + +load( + "@build_bazel_apple_support//lib:repositories.bzl", + "apple_support_dependencies", +) + +apple_support_dependencies() + http_archive( name = "rules_jvm_external", sha256 = "b17d7388feb9bfa7f2fa09031b32707df529f26c91ab9e5d909eb1676badd9a6", diff --git a/tests/MODULE.bazel b/tests/MODULE.bazel index ba77683..5a3f6f4 100644 --- a/tests/MODULE.bazel +++ b/tests/MODULE.bazel @@ -9,6 +9,7 @@ bazel_dep(name = "platforms", version = "0.0.4") bazel_dep(name = "rules_cc", version = "0.0.2") bazel_dep(name = "rules_java", version = "5.1.0") bazel_dep(name = "stardoc", version = "0.5.0", repo_name = "io_bazel_stardoc") +bazel_dep(name = "apple_support", version = "1.15.1", repo_name = "build_bazel_apple_support") cc_configure = use_extension("@bazel_tools//tools/cpp:cc_configure.bzl", "cc_configure_extension") use_repo(cc_configure, "local_config_cc") diff --git a/tests/WORKSPACE b/tests/WORKSPACE index 239ffaf..72e9adf 100644 --- a/tests/WORKSPACE +++ b/tests/WORKSPACE @@ -1,6 +1,6 @@ workspace(name = "fmeum_rules_jni_tests") -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_jar") +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_jar") local_repository( name = "fmeum_rules_jni", @@ -11,6 +11,19 @@ load("@fmeum_rules_jni//jni:repositories.bzl", "rules_jni_dependencies") rules_jni_dependencies() +http_archive( + name = "build_bazel_apple_support", + sha256 = "c4bb2b7367c484382300aee75be598b92f847896fb31bbd22f3a2346adf66a80", + url = "https://github.com/bazelbuild/apple_support/releases/download/1.15.1/apple_support.1.15.1.tar.gz", +) + +load( + "@build_bazel_apple_support//lib:repositories.bzl", + "apple_support_dependencies", +) + +apple_support_dependencies() + http_jar( name = "junit", sha256 = "8e495b634469d64fb8acfa3495a065cbacc8a0fff55ce1e31007be4c16dc57d3", From d08508d78dc460eca6473f9b552c1c1f846ec6a9 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Sat, 27 Apr 2024 13:55:08 +0200 Subject: [PATCH 03/10] Use arm64 JDK --- .github/workflows/run-tests-externally.yml | 3 ++- .github/workflows/run-tests-internally.yml | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/run-tests-externally.yml b/.github/workflows/run-tests-externally.yml index 2820d63..ae3c14b 100644 --- a/.github/workflows/run-tests-externally.yml +++ b/.github/workflows/run-tests-externally.yml @@ -62,8 +62,9 @@ jobs: - uses: actions/checkout@v2 - name: Set up JDK - uses: actions/setup-java@v1 + uses: actions/setup-java@v4 with: + distribution: 'zulu' java-version: ${{ matrix.jdk }} - name: Mount Bazel disk cache diff --git a/.github/workflows/run-tests-internally.yml b/.github/workflows/run-tests-internally.yml index 6b2b8da..9ffe9f7 100644 --- a/.github/workflows/run-tests-internally.yml +++ b/.github/workflows/run-tests-internally.yml @@ -22,8 +22,9 @@ jobs: - uses: actions/checkout@v2 - name: Set up JDK - uses: actions/setup-java@v1 + uses: actions/setup-java@v4 with: + distribution: 'zulu' java-version: 21 - name: Mount Bazel disk cache From 48c1c28acdce27809d8b2df5e58e3eb8d53bbd23 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Sat, 27 Apr 2024 13:58:08 +0200 Subject: [PATCH 04/10] Add arm64 platform --- tests/native_loader/BUILD.bazel | 9 +++++ .../main/native/com/example/os/BUILD.bazel | 39 +++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/tests/native_loader/BUILD.bazel b/tests/native_loader/BUILD.bazel index 564043c..bf3ce94 100644 --- a/tests/native_loader/BUILD.bazel +++ b/tests/native_loader/BUILD.bazel @@ -16,6 +16,15 @@ platform( visibility = ["//visibility:public"], ) +platform( + name = "macos_arm64", + constraint_values = [ + "@platforms//os:macos", + "@platforms//cpu:arm64", + ], + visibility = ["//visibility:public"], +) + platform( name = "windows_x86_64", constraint_values = [ diff --git a/tests/native_loader/src/main/native/com/example/os/BUILD.bazel b/tests/native_loader/src/main/native/com/example/os/BUILD.bazel index b9537d3..294c72c 100644 --- a/tests/native_loader/src/main/native/com/example/os/BUILD.bazel +++ b/tests/native_loader/src/main/native/com/example/os/BUILD.bazel @@ -1,5 +1,37 @@ load("@fmeum_rules_jni//jni:defs.bzl", "cc_jni_library") +config_setting( + name = "linux_x86_64", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], +) + +config_setting( + name = "macos_x86_64", + constraint_values = [ + "@platforms//os:macos", + "@platforms//cpu:x86_64", + ], +) + +config_setting( + name = "macos_arm64", + constraint_values = [ + "@platforms//os:macos", + "@platforms//cpu:arm64", + ], +) + +config_setting( + name = "windows_x86_64", + constraint_values = [ + "@platforms//os:windows", + "@platforms//cpu:x86_64", + ], +) + cc_jni_library( name = "impl/os", testonly = True, @@ -7,9 +39,10 @@ cc_jni_library( "onload.cpp", ], platforms = select({ - "@platforms//os:linux": ["//native_loader:linux_x86_64"], - "@platforms//os:macos": ["//native_loader:macos_x86_64"], - "@platforms//os:windows": ["//native_loader:windows_x86_64"], + ":linux_x86_64": ["//native_loader:linux_x86_64"], + ":macos_x86_64": ["//native_loader:macos_x86_64"], + ":macos_arm64": ["//native_loader:macos_arm64"], + ":windows_x86_64": ["//native_loader:windows_x86_64"], }), visibility = [ "//native_loader/src/main/java/com/example/os:__pkg__", From 9ca5b03a544da5aca699c536c7af823a060a2d71 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Sat, 27 Apr 2024 16:02:28 +0200 Subject: [PATCH 05/10] Fix and improve coverage test --- tests/.bazelrc | 1 + tests/verify_coverage.sh | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/tests/.bazelrc b/tests/.bazelrc index fd43ec3..4ee4175 100644 --- a/tests/.bazelrc +++ b/tests/.bazelrc @@ -8,6 +8,7 @@ coverage --combined_report=lcov coverage --experimental_use_llvm_covmap coverage --experimental_generate_llvm_lcov coverage --test_tag_filters=-no-coverage +coverage --instrumentation_filter=^//analysis/cc_jni_library[/:],^//analysis/jni_headers[/:],^//libjvm_stub[/:],^//native_loader/src/main[/:] coverage:ci --repo_env=CC=clang-14 coverage:ci --repo_env=GCOV=llvm-profdata-14 diff --git a/tests/verify_coverage.sh b/tests/verify_coverage.sh index 03515c6..5a5edd6 100755 --- a/tests/verify_coverage.sh +++ b/tests/verify_coverage.sh @@ -22,7 +22,7 @@ find native_loader/src/main libjvm_stub -type f \ -print0 | while read -r -d $'\0' file do pattern=$(basename "$file") - if grep -q "$pattern" "$report"; + if grep -F -q "$pattern" "$report"; then echo "Coverage report contains $pattern" else @@ -32,7 +32,7 @@ do fi done -if grep -q "__llvm_profile_write_file" "$report"; +if grep -F -q "__llvm_profile_write_file" "$report"; then echo "Coverage report mentions internal function __llvm_profile_write_file:" cat "$report" @@ -40,3 +40,24 @@ then else echo "Coverage report expectedly does not contain internal functions" fi + +declare -a expected_lines=( + "FNDA:2,Java_com_example_math_NativeMath_add" + "FNDA:1,Java_com_example_math_NativeMath_increment" + "FNDA:1,Java_com_example_os_OsUtils_setenv" + "FNDA:1,JNI_OnLoad" + # hermetic_test, java_home_test, path_test + "FNDA:3,com/example/HelloFromJava::helloFromJava (Ljava/lang/String;)Ljava/lang/String;" +) + +for line in "${expected_lines[@]}" +do + if grep -F -q "$line" "$report"; + then + echo "Coverage report contains expected line $line" + else + echo "Coverage report is missing expected line $line:" + cat "$report" + exit 1 + fi +done From ae6b7ebc904558fda5687b41447dbeb2f9d63e1c Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Sat, 27 Apr 2024 16:02:28 +0200 Subject: [PATCH 06/10] Remove Bazel 5.0.0 coverage workarounds --- .github/workflows/run-tests-externally.yml | 10 +- README.md | 38 ++-- jni/internal/cc_jni_library.bzl | 24 +-- jni/internal/coverage.bzl | 124 ----------- jni/internal/templates/BUILD.bazel | 4 - .../templates/native_library_coverage.tmpl.c | 33 --- .../native_library_coverage.tmpl.java | 20 -- .../com/github/fmeum/rules_jni/BUILD.bazel | 35 ---- .../fmeum/rules_jni/CoverageHelper.java | 197 ------------------ .../fmeum/rules_jni/NativeLibraryInfo.java | 27 --- .../fmeum/rules_jni/NoopCoverageHelper.java | 26 --- .../com/github/fmeum/rules_jni/RulesJni.java | 10 +- 12 files changed, 24 insertions(+), 524 deletions(-) delete mode 100644 jni/internal/coverage.bzl delete mode 100644 jni/internal/templates/BUILD.bazel delete mode 100644 jni/internal/templates/native_library_coverage.tmpl.c delete mode 100644 jni/internal/templates/native_library_coverage.tmpl.java delete mode 100644 jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/CoverageHelper.java delete mode 100644 jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/NativeLibraryInfo.java delete mode 100644 jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/NoopCoverageHelper.java diff --git a/.github/workflows/run-tests-externally.yml b/.github/workflows/run-tests-externally.yml index ae3c14b..aef81d1 100644 --- a/.github/workflows/run-tests-externally.yml +++ b/.github/workflows/run-tests-externally.yml @@ -17,19 +17,17 @@ jobs: strategy: fail-fast: false matrix: - bazel: [5.0.0, latest, last_green] + bazel: [6.0.0, 7.x, last_green] bazel_mode: [workspace, module] os: [ubuntu-latest, macos-13, macos-latest, windows-2019] jdk: [8, 11, 17, 21] exclude: - - bazel: 5.0.0 + - bazel: 6.0.0 jdk: 11 - - bazel: 5.0.0 + - bazel: 6.0.0 jdk: 17 - - bazel: 5.0.0 + - bazel: 6.0.0 jdk: 21 - - bazel: 5.0.0 - bazel_mode: module - bazel: last_green jdk: 8 - bazel: last_green diff --git a/README.md b/README.md index 38ec500..4231a53 100644 --- a/README.md +++ b/README.md @@ -16,16 +16,25 @@ not covered by the native Bazel rules: ## Setup -Add the following snippet to your `WORKSPACE`: +If you are [MODULE.bazel](https://bazel.build/external/overview#bzlmod), add the following to your +`MODULE.bazel`: + +```starlark +bazel_dep(name = "rules_jni", version = "") +# Alternatively, to keep using the repository as @fmeum_rules_jni, use: +bazel_dep(name = "rules_jni", version = "", repo_name = "fmeum_rules_jni") +``` + +Otherwise, add the following snippet to your `WORKSPACE`: ```starlark load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "fmeum_rules_jni", - sha256 = "9a387a066f683a8aac4d165917dc7fe15ec2a20931894a97e153a9caab6123ca", - strip_prefix = "rules_jni-0.4.0", - url = "https://github.com/fmeum/rules_jni/archive/refs/tags/v0.4.0.tar.gz", + sha256 = "", + strip_prefix = "rules_jni-", + url = "https://github.com/fmeum/rules_jni/archive/refs/tags/v.tar.gz", ) load("@fmeum_rules_jni//jni:repositories.bzl", "rules_jni_dependencies") @@ -33,15 +42,6 @@ load("@fmeum_rules_jni//jni:repositories.bzl", "rules_jni_dependencies") rules_jni_dependencies() ``` -If you are using Bazel 5 with [bzlmod](https://docs.bazel.build/versions/main/bzlmod.html), add the following to your -`MODULE.bazel`: - -```starlark -bazel_dep(name = "rules_jni", version = "0.4.0") -# Alternatively, to keep using the repository as @fmeum_rules_jni, use: -bazel_dep(name = "rules_jni", version = "0.4.0", repo_name = "fmeum_rules_jni") -``` - ## Documentation See the documentation for [targets](docs/targets.md), [rules](docs/rules.md) @@ -57,9 +57,8 @@ Bazel runfiles using the ## Compatibility -rules_jni heavily uses [platforms](https://docs.bazel.build/versions/main/platforms.html) and thus requires at least -Bazel 4.0.0. For advanced use cases such as multi-platform native libraries, -enabling [`--incompatible_enable_cc_toolchain_resolution`](https://github.com/bazelbuild/bazel/issues/7260) is required. +rules_jni requires at least Bazel 6.0.0. For advanced use cases such as multi-platform native libraries, +enabling [`--incompatible_enable_cc_toolchain_resolution`](https://github.com/bazelbuild/bazel/issues/7260) is required (default in Bazel 7+). ## Multi-language coverage @@ -71,13 +70,6 @@ and `@fmeum_rules_jni//jni:libjvm`. This feature currently has the following lim rather than `@fmeum_rules_jni//jni:libjvm_lite` and `rules_jni_init` has to be called. * All jars on the classpath of a JVM started with `JNI_CreateJavaVM` have to be deploy jars. -There are also the following known issues with Bazel Java coverage to keep in mind: - -* `java_test` does not collect coverage for `cc_binary` targets it executes at - runtime (https://github.com/bazelbuild/bazel/issues/15098) -* Java coverage is not collected correctly with JDK 16+ (https://github.com/bazelbuild/bazel/pull/15081) -* Coverage is not collected for native code that transitively depends on a `java_jni_library` target (https://github.com/bazelbuild/bazel/pull/15118) - To enable this feature, add the following lines to your project's `.bazelrc`: ``` diff --git a/jni/internal/cc_jni_library.bzl b/jni/internal/cc_jni_library.bzl index eb89914..18a3d74 100644 --- a/jni/internal/cc_jni_library.bzl +++ b/jni/internal/cc_jni_library.bzl @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -load(":coverage.bzl", "cc_jni_coverage_helper_library", "java_jni_coverage_helper_library") load(":jni_headers.bzl", "jni_headers") load(":os_cpu_utils.bzl", "SELECT_TARGET_CPU", "SELECT_TARGET_OS") load(":transitions.bzl", "multi_platform_transition") @@ -188,26 +187,9 @@ def cc_jni_library( # Arguments to be set on all targets. testonly = cc_binary_args.pop("testonly", default = None) - java_coverage_helper_name = "%s_java_coverage_helper" % name - java_jni_coverage_helper_library( - name = java_coverage_helper_name, - library_name = basename, - ) - - cc_coverage_helper_name = "%s_cc_coverage_helper" % name - cc_jni_coverage_helper_library( - name = cc_coverage_helper_name, - library_name = basename, - ) - # Simple concatenation is compatible with select, append is not. cc_binary_args.setdefault("deps", []) - cc_binary_args["deps"] += [ - Label("//jni"), - ] + select({ - str(Label("//jni/internal:collect_coverage")): [":" + cc_coverage_helper_name], - "//conditions:default": [], - }) + cc_binary_args["deps"] += [Label("//jni")] native.cc_binary( name = macos_library_name, @@ -267,10 +249,6 @@ def cc_jni_library( name = name, resources = [":" + multi_platform_artifact_name], resource_strip_prefix = _maven_resource_prefix_if_present(), - runtime_deps = select({ - str(Label("//jni/internal:collect_coverage")): [":" + java_coverage_helper_name], - "//conditions:default": [], - }), tags = tags, testonly = testonly, visibility = visibility, diff --git a/jni/internal/coverage.bzl b/jni/internal/coverage.bzl deleted file mode 100644 index fbe3a1a..0000000 --- a/jni/internal/coverage.bzl +++ /dev/null @@ -1,124 +0,0 @@ -# Copyright 2022 Fabian Meumertzheim -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") -load("@bazel_tools//tools/jdk:toolchain_utils.bzl", "find_java_toolchain") -load(":common.bzl", "java_identifier", "jni_escaped_identifier") - -def _cc_jni_coverage_helper_library_impl(ctx): - jni_name = jni_escaped_identifier(ctx.attr.library_name) - - c_file = ctx.actions.declare_file(jni_name + ".c") - ctx.actions.expand_template( - template = ctx.file._template, - output = c_file, - substitutions = { - "$$NAME$$": jni_name, - }, - ) - - cc_toolchain = find_cpp_toolchain(ctx) - feature_configuration = cc_common.configure_features( - ctx = ctx, - cc_toolchain = cc_toolchain, - requested_features = ctx.features, - unsupported_features = ctx.disabled_features + ["coverage"], - ) - compilation_context, compilation_outputs = cc_common.compile( - name = jni_name, - actions = ctx.actions, - feature_configuration = feature_configuration, - cc_toolchain = cc_toolchain, - srcs = [c_file], - compilation_contexts = [ctx.attr._jni[CcInfo].compilation_context], - ) - linking_context, _ = cc_common.create_linking_context_from_compilation_outputs( - name = jni_name, - actions = ctx.actions, - feature_configuration = feature_configuration, - cc_toolchain = cc_toolchain, - compilation_outputs = compilation_outputs, - alwayslink = True, - disallow_dynamic_library = True, - ) - cc_info = CcInfo( - compilation_context = compilation_context, - linking_context = linking_context, - ) - - return [cc_info] - -cc_jni_coverage_helper_library = rule( - implementation = _cc_jni_coverage_helper_library_impl, - attrs = { - "library_name": attr.string(mandatory = True), - "_cc_toolchain": attr.label( - default = "@bazel_tools//tools/cpp:current_cc_toolchain", - ), - "_jni": attr.label( - default = "//jni", - ), - "_template": attr.label( - default = "//jni/internal/templates:native_library_coverage.tmpl.c", - allow_single_file = True, - ), - }, - fragments = ["cpp"], - provides = [CcInfo], - toolchains = [ - "@bazel_tools//tools/cpp:toolchain_type", - ], -) - -def _java_jni_coverage_helper_library_impl(ctx): - java_name = java_identifier(ctx.attr.library_name) - - java_file = ctx.actions.declare_file(java_name + ".java") - ctx.actions.expand_template( - template = ctx.file._template, - output = java_file, - substitutions = { - "$$NAME$$": java_name, - }, - ) - - jar_file = ctx.actions.declare_file(java_name + ".jar") - java_toolchain = find_java_toolchain(ctx, ctx.attr._java_toolchain) - java_info = java_common.compile( - ctx, - java_toolchain = java_toolchain, - output = jar_file, - source_files = [java_file], - ) - - return [java_info] - -java_jni_coverage_helper_library = rule( - implementation = _java_jni_coverage_helper_library_impl, - attrs = { - "library_name": attr.string(mandatory = True), - "_java_toolchain": attr.label( - default = "@bazel_tools//tools/jdk:current_java_toolchain", - ), - "_template": attr.label( - default = "//jni/internal/templates:native_library_coverage.tmpl.java", - allow_single_file = True, - ), - }, - fragments = ["java"], - provides = [JavaInfo], - toolchains = [ - "@bazel_tools//tools/jdk:toolchain_type", - ], -) diff --git a/jni/internal/templates/BUILD.bazel b/jni/internal/templates/BUILD.bazel deleted file mode 100644 index cb21333..0000000 --- a/jni/internal/templates/BUILD.bazel +++ /dev/null @@ -1,4 +0,0 @@ -exports_files([ - "native_library_coverage.tmpl.c", - "native_library_coverage.tmpl.java", -]) diff --git a/jni/internal/templates/native_library_coverage.tmpl.c b/jni/internal/templates/native_library_coverage.tmpl.c deleted file mode 100644 index ac820cc..0000000 --- a/jni/internal/templates/native_library_coverage.tmpl.c +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2022 Fabian Meumertzheim -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include - -// Disable the LLVM profile runtime's static initializer. -int __llvm_profile_runtime = 0; - -void __llvm_profile_initialize_file(); -// Using __llvm_profile_dump instead of __llvm_profile_write_file ensures that -// the profile isn't written twice without a warning. -int __llvm_profile_dump(); - -JNIEXPORT void JNICALL -Java_javax_com_github_fmeum_rules_1jni_gen_$$NAME$$_initCoverageFile() { - __llvm_profile_initialize_file(); -} - -JNIEXPORT void JNICALL -Java_javax_com_github_fmeum_rules_1jni_gen_$$NAME$$_writeCoverageFile() { - __llvm_profile_dump(); -} diff --git a/jni/internal/templates/native_library_coverage.tmpl.java b/jni/internal/templates/native_library_coverage.tmpl.java deleted file mode 100644 index 2051762..0000000 --- a/jni/internal/templates/native_library_coverage.tmpl.java +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2022 Fabian Meumertzheim -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package javax.com.github.fmeum.rules_jni.gen; - -public final class $$NAME$$ { - public static native void initCoverageFile(); - public static native void writeCoverageFile(); -} diff --git a/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/BUILD.bazel b/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/BUILD.bazel index a35bfc6..1a0b5c6 100644 --- a/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/BUILD.bazel +++ b/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/BUILD.bazel @@ -5,39 +5,4 @@ java_library( "RulesJni.java", ], visibility = ["//jni/tools/native_loader:__pkg__"], - deps = select({ - "//jni/internal:collect_coverage": [":coverage_helper"], - "//conditions:default": [":noop_coverage_helper"], - }) + [ - "//jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni:native_library_info", - ], -) - -java_library( - name = "coverage_helper", - srcs = [ - "CoverageHelper.java", - ], - resource_strip_prefix = "external", - resources = [ - "@bazel_tools//tools/test:collect_cc_coverage.sh", - ], - deps = [ - ":native_library_info", - ], -) - -java_library( - name = "noop_coverage_helper", - srcs = [ - "NoopCoverageHelper.java", - ], - deps = [ - ":native_library_info", - ], -) - -java_library( - name = "native_library_info", - srcs = ["NativeLibraryInfo.java"], ) diff --git a/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/CoverageHelper.java b/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/CoverageHelper.java deleted file mode 100644 index 81dc2eb..0000000 --- a/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/CoverageHelper.java +++ /dev/null @@ -1,197 +0,0 @@ -// Copyright 2022 Fabian Meumertzheim -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.github.fmeum.rules_jni; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; -import java.util.Map; -import java.util.stream.Collectors; - -final class CoverageHelper { - private static final String RUNTIME_OBJECTS_LIST_SUFFIX = "runtime_objects_list.txt"; - // We inject our coverage helpers under javax.* to make them less likely to be shaded to a - // different name, which would break the native method lookup. - private static final String COVERAGE_HELPERS_PACKAGE = "javax.com.github.fmeum.rules_jni.gen"; - private static final String INIT_COVERAGE_FILE = "initCoverageFile"; - private static final String WRITE_COVERAGE_FILE = "writeCoverageFile"; - - static void initCoverage(String libraryName) { - callHelperMethod(libraryName, INIT_COVERAGE_FILE); - } - - static void collectNativeLibrariesCoverage(Map loadedLibs) { - if (loadedLibs.isEmpty()) { - return; - } - - for (String libraryName : loadedLibs.keySet()) { - callHelperMethod(libraryName, WRITE_COVERAGE_FILE); - } - - // collect_cc_coverage.sh emits its output at a fixed location relative to COVERAGE_DIR. In - // order to ensure that multiple instances of rules_jni (in multiple java_binary targets or - // shaded) do not interfere with each other when collecting coverage, copy all raw coverage - // files into a temporary directory and generate the report there. - Path tempCoverageDir; - try { - tempCoverageDir = - Files.createTempDirectory(Paths.get(System.getenv("TEST_TMPDIR")), "rules_jni_coverage."); - } catch (IOException e) { - error(e); - // Not reached. - return; - } - - Path coverageDir = Paths.get(System.getenv("COVERAGE_DIR")); - File[] profrawFiles = coverageDir.toFile().listFiles((file, s) -> s.endsWith(".profraw")); - if (profrawFiles != null) { - try { - for (File profrawFile : profrawFiles) { - Files.copy(profrawFile.toPath(), tempCoverageDir.resolve(profrawFile.getName())); - } - } catch (IOException e) { - error(e); - // Not reached. - return; - } - } - - Path collectCcCoverageScript = null; - try { - collectCcCoverageScript = Files.createTempFile(tempCoverageDir, "collect_cc_coverage", ".sh"); - try (InputStream stream = CoverageHelper.class.getResourceAsStream( - "/bazel_tools/tools/test/collect_cc_coverage.sh")) { - if (stream == null) { - error("failed to find collect_cc_coverage.sh in resources"); - // Not reached. - return; - } - Files.copy(stream, collectCcCoverageScript, StandardCopyOption.REPLACE_EXISTING); - } - if (!collectCcCoverageScript.toFile().setExecutable(true)) { - error("failed to make collect_cc_coverage.sh executable"); - // Not reached. - return; - } - } catch (IOException e) { - error(e); - } - - Path objectsBasePath = - Paths.get(System.getenv("RUNFILES_DIR"), System.getenv("TEST_WORKSPACE")); - String runtimeObjectsList = - loadedLibs.values() - .stream() - .map(lib -> objectsBasePath.relativize(lib.tempFile.toPath()).toString()) - .collect(Collectors.joining("\n", "", "\n")); - try { - Path runtimeObjectsListFile = - Files.createTempFile(tempCoverageDir, null, RUNTIME_OBJECTS_LIST_SUFFIX); - Files.write(runtimeObjectsListFile, runtimeObjectsList.getBytes(StandardCharsets.UTF_8)); - Path coverageManifestFile = Files.createTempFile(tempCoverageDir, null, null); - Files.write(coverageManifestFile, - (runtimeObjectsListFile.toAbsolutePath() + "\n").getBytes(StandardCharsets.UTF_8)); - ProcessBuilder processBuilder = - new ProcessBuilder() - .command(collectCcCoverageScript.toAbsolutePath().toString()) - .directory(new File(System.getenv("ROOT"))) - .inheritIO(); - processBuilder.environment().put( - "COVERAGE_MANIFEST", coverageManifestFile.toAbsolutePath().toString()); - processBuilder.environment().put("COVERAGE_DIR", tempCoverageDir.toAbsolutePath().toString()); - processBuilder.start().waitFor(); - } catch (IOException | InterruptedException e) { - error(e); - } - - Path tempCoverageOutput = tempCoverageDir.resolve("_cc_coverage.dat"); - if (!tempCoverageOutput.toFile().exists()) { - error("coverage report at " + tempCoverageOutput + " failed to generate"); - // Not reached. - return; - } - try { - Path uniqueCoverageOutput = Files.createTempFile(coverageDir, "_cc_coverage.", ".dat"); - Files.move(tempCoverageOutput, uniqueCoverageOutput, StandardCopyOption.REPLACE_EXISTING); - } catch (IOException e) { - e.printStackTrace(); - } - - // We intentionally don't clean up the temporary files: They are only created when we run within - // Bazel and thus land in the test's temporary directory, which is cleaned up by Bazel. By not - // cleaning up ourselves, we make it easier to inspect the coverage files with --sandbox_debug. - } - - private static void writeCoverageFile(String libraryName) { - String helperClassName = COVERAGE_HELPERS_PACKAGE + "." + toJavaIdentifier(libraryName); - try { - Class helperClass = Class.forName(helperClassName); - Method writeCoverageFileMethod = helperClass.getMethod("writeCoverageFile"); - writeCoverageFileMethod.invoke(null); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException | UnsatisfiedLinkError e) { - error(e); - } - } - - private static void callHelperMethod(String libraryName, String methodName) { - String helperClassName = COVERAGE_HELPERS_PACKAGE + "." + toJavaIdentifier(libraryName); - try { - Class helperClass = Class.forName(helperClassName); - Method helperMethod = helperClass.getMethod(methodName); - helperMethod.invoke(null); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException | UnsatisfiedLinkError e) { - error(e); - } - } - - private static void error(String message) { - System.err.println("[rules_jni] Failed to collect coverage for native libraries: " + message); - Runtime.getRuntime().halt(1); - } - - private static void error(Throwable t) { - System.err.println("[rules_jni] Failed to collect coverage for native libraries:"); - t.printStackTrace(); - Runtime.getRuntime().halt(1); - } - - // Computes the same identifier as java_identifier in //jni/internal:common.bzl. - private static String toJavaIdentifier(String name) { - char[] safeChars = new char[name.length()]; - for (int i = 0; i < name.length(); i++) { - char c = name.charAt(i); - if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) { - safeChars[i] = c; - } else { - safeChars[i] = '_'; - } - } - String safeName = new String(safeChars); - if (safeChars.length == 0 || (safeChars[0] >= '0' && safeChars[0] <= '9')) { - safeName = "_" + safeName; - } - return safeName + "_" + (((long) name.hashCode()) - Integer.MIN_VALUE); - } -} diff --git a/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/NativeLibraryInfo.java b/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/NativeLibraryInfo.java deleted file mode 100644 index 213f5f3..0000000 --- a/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/NativeLibraryInfo.java +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2022 Fabian Meumertzheim -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.github.fmeum.rules_jni; - -import java.io.File; - -class NativeLibraryInfo { - public final String canonicalPath; - public final File tempFile; - - NativeLibraryInfo(String canonicalPath, File tempFile) { - this.canonicalPath = canonicalPath; - this.tempFile = tempFile; - } -} diff --git a/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/NoopCoverageHelper.java b/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/NoopCoverageHelper.java deleted file mode 100644 index 204a91c..0000000 --- a/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/NoopCoverageHelper.java +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2022 Fabian Meumertzheim -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.github.fmeum.rules_jni; - -import java.nio.file.Path; -import java.util.Map; - -/** - * A no-op implementation of CoverageHelper used then coverage is not enabled. - */ -final class CoverageHelper { - static void initCoverage(String libraryName) {} - static void collectNativeLibrariesCoverage(Map loadedLibs) {} -} diff --git a/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/RulesJni.java b/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/RulesJni.java index 694ef8a..7dbd90f 100644 --- a/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/RulesJni.java +++ b/jni/tools/native_loader/src/main/java/com/github/fmeum/rules_jni/RulesJni.java @@ -27,7 +27,7 @@ * {@code rules_jni}. */ public final class RulesJni { - private static final Map LOADED_LIBS = new HashMap<>(); + private static final Map LOADED_LIBS = new HashMap<>(); private static Path tempDir; @@ -158,10 +158,10 @@ public static Path extractLibrary(String name, String absolutePathToPackage) thr synchronized private static void loadLibrary(String name, URL libraryResource) { String basename = libraryBasename(name); if (LOADED_LIBS.containsKey(basename)) { - if (!libraryResource.toString().equals(LOADED_LIBS.get(basename).canonicalPath)) { + if (!libraryResource.toString().equals(LOADED_LIBS.get(basename))) { throw new UnsatisfiedLinkError(String.format( "Cannot load two native libraries with same basename ('%s') from different paths\nFirst library: %s\nSecond library: %s\n", - basename, LOADED_LIBS.get(basename).canonicalPath, libraryResource)); + basename, LOADED_LIBS.get(basename), libraryResource)); } return; } @@ -172,8 +172,7 @@ synchronized private static void loadLibrary(String name, URL libraryResource) { throw new UnsatisfiedLinkError(e.getMessage()); } System.load(tempFile.toAbsolutePath().toString()); - LOADED_LIBS.put(basename, new NativeLibraryInfo(libraryResource.toString(), tempFile.toFile())); - CoverageHelper.initCoverage(basename); + LOADED_LIBS.put(basename, libraryResource.toString()); } private static Path extractLibrary(String basename, URL libraryResource) throws IOException { @@ -236,7 +235,6 @@ private static void atExit() { System.err.println( "[rules_jni] Not cleaning up temporary directory as requested: " + tempDir); } - CoverageHelper.collectNativeLibrariesCoverage(LOADED_LIBS); if (!skipCleanup) { try (Stream tempFiles = Files.walk(tempDir)) { tempFiles.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); From 71aa574e56708855632aa4b4247e9ac9f93fdcfd Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Sat, 27 Apr 2024 16:48:48 +0200 Subject: [PATCH 07/10] Switch host CPU --- tests/analysis/cc_jni_library/tests.bzl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/analysis/cc_jni_library/tests.bzl b/tests/analysis/cc_jni_library/tests.bzl index 47ec5ff..0bb3ef5 100644 --- a/tests/analysis/cc_jni_library/tests.bzl +++ b/tests/analysis/cc_jni_library/tests.bzl @@ -88,6 +88,9 @@ def _get_host_constraint_value(constraint_setting): def _get_host_legacy_cpu(): cpu = _get_host_constraint_value("cpu") os = _get_host_constraint_value("os") + # Indirectly test for Bazel 7 via: + # https://github.com/bazelbuild/bazel/commit/31fd464af77f084049386af02dbcc5189c745892 + is_bazel_7_or_higher = attr.string() == attr.string() if os == "osx" and cpu == "aarch64": return "darwin_arm64" if cpu != "x86_64": @@ -95,7 +98,10 @@ def _get_host_legacy_cpu(): if os == "linux": return "k8" elif os == "osx": - return "darwin_x86_64" + if is_bazel_7_or_higher: + return "darwin_x86_64" + else: + return "darwin" elif os == "windows": return "x64_windows" else: From 21db33d368a6aec3ac92538333d2cb8af55c1f6c Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Sat, 27 Apr 2024 17:11:59 +0200 Subject: [PATCH 08/10] Fix buildifier --- tests/analysis/cc_jni_library/tests.bzl | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/analysis/cc_jni_library/tests.bzl b/tests/analysis/cc_jni_library/tests.bzl index 0bb3ef5..63f14ed 100644 --- a/tests/analysis/cc_jni_library/tests.bzl +++ b/tests/analysis/cc_jni_library/tests.bzl @@ -88,6 +88,7 @@ def _get_host_constraint_value(constraint_setting): def _get_host_legacy_cpu(): cpu = _get_host_constraint_value("cpu") os = _get_host_constraint_value("os") + # Indirectly test for Bazel 7 via: # https://github.com/bazelbuild/bazel/commit/31fd464af77f084049386af02dbcc5189c745892 is_bazel_7_or_higher = attr.string() == attr.string() From 27bfb85b4417e61f096f673dafdbef6a01e6b3aa Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Sat, 27 Apr 2024 17:21:54 +0200 Subject: [PATCH 09/10] Remove xcode config --- .github/workflows/run-tests-externally.yml | 5 ++--- tests/.github/BUILD.bazel | 26 ---------------------- 2 files changed, 2 insertions(+), 29 deletions(-) delete mode 100644 tests/.github/BUILD.bazel diff --git a/.github/workflows/run-tests-externally.yml b/.github/workflows/run-tests-externally.yml index aef81d1..997b5d5 100644 --- a/.github/workflows/run-tests-externally.yml +++ b/.github/workflows/run-tests-externally.yml @@ -43,7 +43,6 @@ jobs: cache: "/home/runner/.cache/bazel-disk" - os: macos-13 cache: "/private/var/tmp/bazel-disk" - bazel_macos_args: "--xcode_version_config=//.github:host_xcodes" - os: macos-latest cache: "/private/var/tmp/bazel-disk" - os: windows-2019 @@ -80,7 +79,7 @@ jobs: env: USE_BAZEL_VERSION: ${{ matrix.bazel }} working-directory: ./tests - run: bazelisk test --config=ci --disk_cache=${{ matrix.cache }} --profile=${{ github.workspace }}/profile.gz ${{ matrix.bazel_extra_args }} ${{ matrix.bazel_macos_args }} //... + run: bazelisk test --config=ci --disk_cache=${{ matrix.cache }} --profile=${{ github.workspace }}/profile.gz ${{ matrix.bazel_extra_args }} //... - name: Check coverage if: ${{ (matrix.os == 'ubuntu-latest') && (matrix.jdk == '17') }} @@ -89,7 +88,7 @@ jobs: working-directory: ./tests run: | sudo apt install clang-14 llvm-14 - bazelisk coverage --config=ci --disk_cache=${{ matrix.cache }} --profile=${{ github.workspace }}/profile.gz ${{ matrix.bazel_extra_args }} ${{ matrix.bazel_macos_args }} //... + bazelisk coverage --config=ci --disk_cache=${{ matrix.cache }} --profile=${{ github.workspace }}/profile.gz ${{ matrix.bazel_extra_args }} //... ./verify_coverage.sh - name: Upload test logs diff --git a/tests/.github/BUILD.bazel b/tests/.github/BUILD.bazel deleted file mode 100644 index ee17ccc..0000000 --- a/tests/.github/BUILD.bazel +++ /dev/null @@ -1,26 +0,0 @@ -# Extracted on 2022-01-05 as described in -# https://www.smileykeith.com/2021/03/08/locking-xcode-in-bazel/ - -package(default_visibility = ["//visibility:public"]) - -xcode_version( - name = "version13_1_0_13A1030d", - aliases = [ - "13.1.0", - "13.1", - "13.1.0.13A1030d", - ], - default_ios_sdk_version = "15.0", - default_macos_sdk_version = "12.0", - default_tvos_sdk_version = "15.0", - default_watchos_sdk_version = "8.0", - version = "13.1.0.13A1030d", -) - -xcode_config( - name = "host_xcodes", - default = ":version13_1_0_13A1030d", - versions = [ - ":version13_1_0_13A1030d", - ], -) From cc3c3c2a76a1aa6f789ee4c511afb71f840ec75d Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Sat, 27 Apr 2024 17:36:47 +0200 Subject: [PATCH 10/10] Switch to 6.x --- .github/workflows/run-tests-externally.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/run-tests-externally.yml b/.github/workflows/run-tests-externally.yml index 997b5d5..a02844d 100644 --- a/.github/workflows/run-tests-externally.yml +++ b/.github/workflows/run-tests-externally.yml @@ -17,16 +17,16 @@ jobs: strategy: fail-fast: false matrix: - bazel: [6.0.0, 7.x, last_green] + bazel: [6.x, 7.x, last_green] bazel_mode: [workspace, module] os: [ubuntu-latest, macos-13, macos-latest, windows-2019] jdk: [8, 11, 17, 21] exclude: - - bazel: 6.0.0 + - bazel: 6.x jdk: 11 - - bazel: 6.0.0 + - bazel: 6.x jdk: 17 - - bazel: 6.0.0 + - bazel: 6.x jdk: 21 - bazel: last_green jdk: 8