Reputation: 1561
when using JNI, we would do the following on bash/cmd:
In script, something like:
javac foo.java
javah foo
cmake . && make
jar .....
How can I reproduce on CMake?
Other than using CUSTOM_COMMAND I could only find add_jar to compile the java sources. Is it possible to compile the sources in CMake without jaring them afterwards?
Or should I resign and add_jar the java sources, build the library and add_jar again?
Also, how is Java_JAVAH_EXECUTABLE supposed to be used, with CUSTOM_COMMAND?
Upvotes: 2
Views: 2307
Reputation: 328
I have a project where I'm trying to achieve something like this. (And some Android NDK work using android-cmake, with limited success, though that's not the point of your question.)
CMake does support options for building Java. [see note 1] However, that's exactly how it's oriented: your native code is the first-class citizen as far as this build system is concerned, so keep that in mind if you're considering going down the same road that I did. Some people use CMake as a component of their Java workflow, under ant/maven or things of that nature, and there are various plugins that load CMake projects within the scope of a 'larger' build.
But I think this really thwarts the value of CMake's generators. I investigated doing that, but we ended up not going down that route. That's really a choice you'll have to make for yourself. (I find this project has Java, not is in Java, so it makes sense for me to do something more like what you're asking after.)
Here's the thing: add_jar has a little black-box magic to it. If you want to integrate UseJNI... well, I couldn't find much in the way of support for that. (Reading through the source of the module included in CMake 2.8.x exposes things like CMAKE_JNI_TARGET. Though I never really got that working, I included it anyway. Here is my custom solution to this problem:
Good stuff for your top-level CMakeLists.txt in projects that use Java
Contains the definition of a helpful macro: javah
A sample of how to use the macro around native libraries
[1] At the time of this writing, there appear to be two methods:
Here is reference for the later which I found myself referencing quite a bit.
Good luck.
Upvotes: 2