Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve docs for rules_jni_init #61

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions docs/targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ current target platform.
This library can be added to the `deps` of a `cc_*` target that requires access to
the [Java Invocation API](https://docs.oracle.com/en/java/javase/17/docs/specs/jni/invocation.html).

**Note:** Every `cc_binary` that transitively depends on this library should also directly depend on it and call
`rules_jni_init` from its `main` function, providing `argv[0]`. This ensures that

This target serves as a drop-in replacement for the `jvm` shared library contained in JDKs and JREs, which is
called `jvm.dll`, `libjvm.dylib`
or `libjvm.so` depending on the OS. These libraries are usually not available in the standard locations searched by the
Expand All @@ -37,12 +34,13 @@ runtime when its first symbol is accessed. Concretely, it does the following in
finds a runfiles directory or manifest, locate the current Bazel Java runtime in the runfiles and look for the `jvm`
shared library relative to it at well-known locations. If it cannot be found, print a warning and continue.

**Note:** If you want this behavior also for binaries executed via `bazel run` or directly from `bazel-bin`, you have
to:
**Note:** To ensure that the current Bazel Java runtime is also found if the binary is executed via `bazel run` or
directly from `bazel-bin` and to ensure that Java code can find its runfiles, do the following:

1. Depend on `@fmeum_rules_jni//jni:libjvm` directly from your top-level `cc_binary`.
1. Depend on `@fmeum_rules_jni//jni:libjvm` directly from the top-level `cc_binary`.
2. Add `#include <rules_jni.h>`.
3. Call `rules_jni_init(const char* argv0)` from your `main` function, providing `argv[0]` as the argument.
3. Call `rules_jni_init(const char* argv0)` from the `main` function, providing `argv[0]` as the argument. This
call is **not thread-safe** as it modifies the environment.

If you want this lookup to succeed also for binaries executed by other binaries that are themselves run from Bazel,
[set the environment variables required for runfiles discovery](https://github.com/bazelbuild/bazel/blob/e8a066e9e625a136363338d10f03ed14c26dedfa/tools/cpp/runfiles/runfiles_src.h#L58).
Expand Down