diff options
author | Fabian Meumertzheim <meumertzheim@code-intelligence.com> | 2021-08-06 07:56:51 +0200 |
---|---|---|
committer | Fabian Meumertzheim <fabian@meumertzhe.im> | 2021-08-09 09:22:07 +0200 |
commit | 3282049bdf62d053a7ac9a0157ca3ef0f0c4ec27 (patch) | |
tree | b7710bc10daf7f89e1551b054659e4774f07b169 | |
parent | fe5e2bd92cfd89d74b83dffe620bc08d459a398c (diff) | |
download | jazzer-api-3282049bdf62d053a7ac9a0157ca3ef0f0c4ec27.tar.gz |
Find libjvm with a repository rule
libjvm lives in different subpaths of JAVA_HOME, depending both on the
OS and the Java version. Since it is currently not possible to select a
dependency based on the Java version, supporting Java 8 required a
custom build setting. This also broke bazel query (but not cquery).
By loading libjvm from a simple repository rule, we can cover all
OSes and Java versions with a single dependency, even if libjvm.so is
installed in a non-standard location.
-rw-r--r-- | .github/workflows/run-all-tests.yml | 8 | ||||
-rw-r--r-- | bazel/local_jdk_libjvm.bzl | 59 | ||||
-rw-r--r-- | driver/BUILD.bazel | 2 | ||||
-rw-r--r-- | repositories.bzl | 6 | ||||
-rw-r--r-- | third_party/jni/BUILD.bazel | 45 |
5 files changed, 68 insertions, 52 deletions
diff --git a/.github/workflows/run-all-tests.yml b/.github/workflows/run-all-tests.yml index 007b46f2..64980368 100644 --- a/.github/workflows/run-all-tests.yml +++ b/.github/workflows/run-all-tests.yml @@ -23,10 +23,6 @@ jobs: - os: macos-latest arch: "darwin" cache: "/private/var/tmp" - - jdk: 8 - jdk8_flag: "--//third_party/jni:jdk_8" - - jdk: 15 - jdk8_flag: "--no//third_party/jni:jdk_8" steps: - uses: actions/checkout@v2 @@ -48,10 +44,10 @@ jobs: run: curl -LO https://github.com/bazelbuild/bazelisk/releases/download/v1.7.5/bazelisk-darwin-amd64 && chmod +x bazelisk-darwin-amd64 - name: Build - run: ./bazelisk-${{ matrix.arch }}-amd64 build --config=ci --remote_header=x-buildbuddy-api-key=${{ secrets.BUILDBUDDY_API_KEY }} --disk_cache=${{ matrix.cache }} --java_runtime_version=localjdk_${{ matrix.jdk }} ${{ matrix.jdk8_flag }} //... + run: ./bazelisk-${{ matrix.arch }}-amd64 build --config=ci --remote_header=x-buildbuddy-api-key=${{ secrets.BUILDBUDDY_API_KEY }} --disk_cache=${{ matrix.cache }} --java_runtime_version=localjdk_${{ matrix.jdk }} //... - name: Test - run: ./bazelisk-${{ matrix.arch }}-amd64 test --config=ci --remote_header=x-buildbuddy-api-key=${{ secrets.BUILDBUDDY_API_KEY }} --disk_cache=${{ matrix.cache }} --java_runtime_version=localjdk_${{ matrix.jdk }} ${{ matrix.jdk8_flag }} --test_tag_filters="-broken-on-${{ matrix.arch }}" //... + run: ./bazelisk-${{ matrix.arch }}-amd64 test --config=ci --remote_header=x-buildbuddy-api-key=${{ secrets.BUILDBUDDY_API_KEY }} --disk_cache=${{ matrix.cache }} --java_runtime_version=localjdk_${{ matrix.jdk }} --test_tag_filters="-broken-on-${{ matrix.arch }}" //... - name: Upload test logs if: always() diff --git a/bazel/local_jdk_libjvm.bzl b/bazel/local_jdk_libjvm.bzl new file mode 100644 index 00000000..f1481601 --- /dev/null +++ b/bazel/local_jdk_libjvm.bzl @@ -0,0 +1,59 @@ +# Copyright 2021 Code Intelligence GmbH +# +# 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. + +def _find_recursively_under_path(repository_ctx, path, basename): + result = repository_ctx.execute([ + repository_ctx.which("sh"), + "-c", + """find -L "{path}" -name "{basename}" | head -1""".format( + path = path, + basename = basename, + ), + ]) + if result.return_code != 0: + return None + file_path = result.stdout.strip() + if not file_path: + return None + return repository_ctx.path(file_path) + +LIBJVM_NAMES = [ + "libjvm.dylib", + "libjvm.so", +] + +def _local_jdk_libjvm(repository_ctx): + java_binary = str(repository_ctx.path(Label("@local_jdk//:bin/java"))) + java_home = str(repository_ctx.path(java_binary + "/../../")) + + libjvm_path = None + for libjvm_name in LIBJVM_NAMES: + libjvm_path = _find_recursively_under_path(repository_ctx, java_home, libjvm_name) + if libjvm_path != None: + break + + if libjvm_path != None: + repository_ctx.symlink(libjvm_path, libjvm_path.basename) + build_content = """ +cc_import( + name = "libjvm", + shared_library = "{libjvm}", + visibility = ["//visibility:public"], +) +""".format(libjvm = libjvm_path.basename) + repository_ctx.file("BUILD.bazel", build_content, executable = False) + +local_jdk_libjvm = repository_rule( + implementation = _local_jdk_libjvm, +) diff --git a/driver/BUILD.bazel b/driver/BUILD.bazel index f7b09efe..d4d166a2 100644 --- a/driver/BUILD.bazel +++ b/driver/BUILD.bazel @@ -50,13 +50,13 @@ cc_library( visibility = ["//visibility:public"], deps = [ ":sanitizer_hooks_with_pc", - "//third_party/jni:jni_libs", "@bazel_tools//tools/cpp/runfiles", "@bazel_tools//tools/jdk:jni", "@com_google_absl//absl/strings", "@com_google_absl//absl/strings:str_format", "@com_google_glog//:glog", "@jazzer_com_github_gflags_gflags//:gflags", + "@jazzer_libjvm//:libjvm", ], ) diff --git a/repositories.bzl b/repositories.bzl index bddb540a..c1f91962 100644 --- a/repositories.bzl +++ b/repositories.bzl @@ -16,6 +16,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") +load("//bazel:local_jdk_libjvm.bzl", "local_jdk_libjvm") def jazzer_dependencies(): maybe( @@ -128,3 +129,8 @@ def jazzer_dependencies(): strip_prefix = "llvm-project-jazzer-b9c07e9c1e75857901808c13101c909104c413a8", url = "https://github.com/CodeIntelligenceTesting/llvm-project-jazzer/archive/b9c07e9c1e75857901808c13101c909104c413a8.tar.gz", ) + + maybe( + local_jdk_libjvm, + name = "jazzer_libjvm", + ) diff --git a/third_party/jni/BUILD.bazel b/third_party/jni/BUILD.bazel deleted file mode 100644 index cd181e8e..00000000 --- a/third_party/jni/BUILD.bazel +++ /dev/null @@ -1,45 +0,0 @@ -load("@rules_cc//cc:defs.bzl", "cc_import", "cc_library") -load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") - -cc_library( - name = "jni_libs", - visibility = ["//visibility:public"], - deps = select({ - "@platforms//os:osx": [":libjvm_osx"], - "//conditions:default": [":libjvm_linux"], - }), -) - -bool_flag( - name = "jdk_8", - build_setting_default = False, -) - -config_setting( - name = "is_jdk_8", - flag_values = { - ":jdk_8": "true", - }, -) - -cc_import( - name = "libjvm_osx", - interface_library = select({ - ":is_jdk_8": "@local_jdk//:jre/lib/server/libjvm.dylib", - "//conditions:default": "@local_jdk//:lib/server/libjvm.dylib", - }), - system_provided = True, - # Workaround for https://github.com/bazelbuild/bazel/issues/12745 - tags = ["manual"], -) - -cc_import( - name = "libjvm_linux", - interface_library = select({ - ":is_jdk_8": "@local_jdk//:jre/lib/amd64/server/libjvm.so", - "//conditions:default": "@local_jdk//:lib/server/libjvm.so", - }), - system_provided = True, - # Workaround for https://github.com/bazelbuild/bazel/issues/12745 - tags = ["manual"], -) |