aboutsummaryrefslogtreecommitdiff
path: root/bazel
diff options
context:
space:
mode:
authorFabian Meumertzheim <meumertzheim@code-intelligence.com>2021-08-06 07:56:51 +0200
committerFabian Meumertzheim <fabian@meumertzhe.im>2021-08-09 09:22:07 +0200
commit3282049bdf62d053a7ac9a0157ca3ef0f0c4ec27 (patch)
treeb7710bc10daf7f89e1551b054659e4774f07b169 /bazel
parentfe5e2bd92cfd89d74b83dffe620bc08d459a398c (diff)
downloadjazzer-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.
Diffstat (limited to 'bazel')
-rw-r--r--bazel/local_jdk_libjvm.bzl59
1 files changed, 59 insertions, 0 deletions
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,
+)