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 detection of JDK location for JNI #551

Merged
merged 2 commits into from
Nov 8, 2019

Conversation

ilammy
Copy link
Collaborator

@ilammy ilammy commented Oct 29, 2019

Currently on anything other than Debian it is necessary to manually point the build to the JDK location. There is a rudimentary mechanism for automatic detection, but we can do better. Let’s make sure that running make themis_jni ‘just works’ for all operating systems that we support.

We need to have correct include path to be able to access JNI headers. These are installed by JDK somewhere into the system. Normally there are two paths: generic path where "jni.h" is located and a platform-specific directory (usually a subdirectory under the generic path).

First of all, there is user override. If JDK_INCLUDE_PATH contains anything, that’s considered our include path. Prepend -I to every directory in that list and use that.

Otherwise we try to detect the paths automatically. Debian and Ubuntu packages distribute a java_defaults.mk makefile which conveniently defines C compiler flags in the jvm_includes variable. Check if that file exists via Make magic, include it, and use that variable. Note
that this file requires the dpkg-dev package to be installed in addition to JDK package (e.g., openjdk-11-jdk for Debian 10 Buster).

Other systems are not so nice. JNI headers are usually installed into JAVA_HOME. If JAVA_HOME environment variable is not set then ask Java. The includes are usually located under the include directory in the home directory plus there is an platform-specific subdirectory. On macOS it's called darwin. Many Linux distributions use linux (CentOS certainly does).

If automatic detection does not work, use JDK_INCLUDE_PATH. Typically you’ll need something like this:

make themis_jni JDK_INCLUDE_PATH="$JAVA_HOME/include $JAVA_HOME/include/plan9"

P.S. Android uses its own magic for building the library correctly, see jni/Android.mk.

Checklist

  • Change is covered by automated tests
  • Benchmark results are attached (not applicable)
  • The coding guidelines are followed
  • Public API has proper documentation: external docs
  • Example projects and code samples are updated if needed (no API changes)
  • Changelog is updated if needed (in case of notable or breaking changes)

Currently on anything other than Debian it is necessary to manually
point the build to the JDK location. There is a rudimentary mechanism
for automatic detection, but we can do better. Let's make sure that
running "make themis_jni" 'just works' for all operating systems that
we support.

We need to have correct include path to be able to access JNI headers.
These are installed by JDK somewhere into the system. Normally there
are two paths: generic path where "jni.h" is located and a platform-
specific directory (usually a subdirectory under the generic path).

First of all, there is user override. If JDK_INCLUDE_PATH contains
anything, that's considered our include path. Prepend '-I' to every
directory in that list and use that.

Otherwise we try to detect the paths automatically. Debian and Ubuntu
packages distribute a "java_defaults.mk" makefile which conveniently
defines C compiler flags in the "jvm_includes" variable. Check if that
file exists via Make magic, include it, and use that variable. Note
that this file requires the "dpkg-dev" package to be installed in
addition to JDK package ("openjdk-11-jdk" for Debian 10 Buster).

Other systems are not so nice. JNI headers are usually installed into
JAVA_HOME. If JAVA_HOME environment variable is not set then ask Java.
The includes are usually located under the "include" directory in the
home directory plus there is an platform-specific subdirectory.
On macOS it's called "darwin". Many Linux distributions use "linux"
(CentOS certainly does).

If automatic detection does not work, use JDK_INCLUDE_PATH. Typically
you'll need something like this:

    make themis_jni JDK_INCLUDE_PATH="$JAVA_HOME/include $JAVA_HOME/include/plan9"

P.S. Android uses it's own magic for building the library correctly,
see jni/Android.mk.
@ilammy ilammy added W-JavaThemis ☕ Wrapper: Java, Java and Kotlin API infrastructure Automated building and packaging labels Oct 29, 2019
@vixentael
Copy link
Contributor

Makes a lot of sense to me as macOS user, because I usually need to use following:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home

export CFLAGS="-I$JAVA_HOME/include -I$JAVA_HOME/include/darwin"

Copy link
Contributor

@vixentael vixentael left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shadinua please take a look

@ilammy ilammy merged commit 4d0ef9f into cossacklabs:master Nov 8, 2019
@ilammy ilammy deleted the jni-autoconfig branch November 8, 2019 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
infrastructure Automated building and packaging W-JavaThemis ☕ Wrapper: Java, Java and Kotlin API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants