Skip to content

Commit 2438082

Browse files
authored
add new --jni-generate-main parameter (#49)
* add new commandline parameter `--jni-generate-main` to resolve the currently discussed issue on how to provide a default JNI_OnLoad & JNI_OnUnload implementation without the user having to know about the internals. The paramater, if true, generates a file `djinni_jni_main.cpp` that includes the `djinni/jni/djinni_jni_main.hpp` header provided by djinni-support-lib. That way by default the user automatically gets a default JNI_OnLoad implementation if he builds a shared library from all sources. This is a breaking change because it generates a new type of file. This requires the upcoming djinni-support-lib v1.0.0, that comes with the required header file, to work properly.
1 parent 26e1812 commit 2438082

File tree

21 files changed

+67
-13
lines changed

21 files changed

+67
-13
lines changed

docs/cli-usage.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ djinni \
8888
| `--jni-include-cpp-prefix <prefix>` | The prefix for `#includes` of the main header files from JNI C++ files. |
8989
| `--jni-namespace ...` | The namespace name to use for generated JNI C++ classes. |
9090
| `--jni-base-lib-include-prefix ...` | The JNI base support library's include path (default: `djinni/jni/`). |
91+
| `--jni-generate-main <true/false>` | Generate a source file (`djinni_jni_main.cpp`) that includes the default `JNI_OnLoad` & `JNI_OnUnload` implementation from the support library. (default: `true`) |
9192

9293
### Objective-C
9394

docs/generated-code-usage.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ The following headers / code will be generated for each defined type:
1313

1414
(+) Generated only for types that contain constants.
1515

16+
Additionally `djinni_jni_main.cpp` is generated to provide a default implementation for `JNI_OnLoad` and `JNI_OnUnload`, if `--jni-generate-main=true`.
17+
1618
Add all generated source files to your build target, and link the C++ code against the [djinni-support-lib](https://github.com/cross-language-cpp/djinni-support-lib).
1719

1820
### Our JNI approach
1921
JNI stands for [Java Native Interface](http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html), an extension of the Java language to allow interop with
2022
native (C/C++) code or libraries.
2123

22-
2324
For each type, built-in (`list`, `string`, etc.) or user-defined, Djinni produces a translator
2425
class with a `toJava` and `fromJava` function to translate back and forth.
2526

@@ -39,12 +40,15 @@ class Main {
3940
If you package your native library in a jar, you can also use the [`NativeLibLoader`](https://github.com/cross-language-cpp/djinni-support-lib/blob/main/java/com/dropbox/djinni/NativeLibLoader.java)
4041
to help unpack and load your lib(s).
4142

42-
When a native library is called, JNI calls a special function called [`JNI_OnLoad`](https://github.com/cross-language-cpp/djinni-support-lib/blob/main/djinni/jni/djinni_main.cpp#L23).
43+
Any library loaded from Java should provide the functions `JNI_OnLoad` and `JNI_OnUnload`.
44+
They are called by JNI when the library is loaded/unloaded.
4345

44-
If your app doesn't use JNI except through Djinni, include [`djinni/jni/djinni_main.cpp`](https://github.com/cross-language-cpp/djinni-support-lib/blob/main/djinni/jni/djinni_main.cpp).
45-
It defines default `JNI_Onload` and `JNI_OnUnload` functions for Djinni.
46+
Djinni uses these functions to initialize & cleanup internal structures.
47+
The generated file `djinni_jni_main.cpp` includes a default implementation of `JNI_Onload` and `JNI_OnUnload` functions
48+
provided by the support library.
4649

47-
If your app also includes a non-Djinni JNI interface, you'll need to define your own `JNI_OnLoad` and `JNI_OnUnload` functions.
50+
If you are building a library that does not use JNI except through Djinni, this default should work well for you.
51+
If want to provide your own implementation of `JNI_Onload` and `JNI_OnUnload`, the generation of `djinni_jni_main.cpp` can be disabled by setting `--jni-generate-main=false`.
4852

4953
## Objective-C / C++ Project
5054

docs/setup.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ Add the generator as a build requirement in `conanfile.txt`:
3131

3232
```text
3333
[build_requires]
34-
djinni-generator/0.3.1
34+
djinni-generator/1.0.0
3535
```

src/it/resources/expected/all_datatypes/generated-files.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
src/it/resources/result/all_datatypes/cpp-headers/all_datatypes.hpp
22
src/it/resources/result/all_datatypes/java/AllDatatypes.java
3+
src/it/resources/result/all_datatypes/jni/djinni_jni_main.cpp
34
src/it/resources/result/all_datatypes/jni-headers/all_datatypes.hpp
45
src/it/resources/result/all_datatypes/jni/all_datatypes.cpp
56
src/it/resources/result/all_datatypes/objc-headers/ITAllDatatypes.h
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// AUTOGENERATED FILE - DO NOT MODIFY!
2+
// The file included here provides default JNI_OnLoad and JNI_OnUnload implementations.
3+
// Disable the generation of this file with `--jni-generate-main=false`.
4+
#include "djinni/jni/djinni_jni_main.hpp"

src/it/resources/expected/my_client_interface/generated-files.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
src/it/resources/result/my_client_interface/cpp-headers/my_client_interface.hpp
22
src/it/resources/result/my_client_interface/java/MyClientInterface.java
3+
src/it/resources/result/my_client_interface/jni/djinni_jni_main.cpp
34
src/it/resources/result/my_client_interface/jni-headers/my_client_interface.hpp
45
src/it/resources/result/my_client_interface/jni/my_client_interface.cpp
56
src/it/resources/result/my_client_interface/objc-headers/ITMyClientInterface.h
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// AUTOGENERATED FILE - DO NOT MODIFY!
2+
// The file included here provides default JNI_OnLoad and JNI_OnUnload implementations.
3+
// Disable the generation of this file with `--jni-generate-main=false`.
4+
#include "djinni/jni/djinni_jni_main.hpp"

src/it/resources/expected/my_cpp_interface/generated-files.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
src/it/resources/result/my_cpp_interface/cpp-headers/my_cpp_interface.hpp
22
src/it/resources/result/my_cpp_interface/cpp/my_cpp_interface.cpp
33
src/it/resources/result/my_cpp_interface/java/MyCppInterface.java
4+
src/it/resources/result/my_cpp_interface/jni/djinni_jni_main.cpp
45
src/it/resources/result/my_cpp_interface/jni-headers/my_cpp_interface.hpp
56
src/it/resources/result/my_cpp_interface/jni/my_cpp_interface.cpp
67
src/it/resources/result/my_cpp_interface/objc-headers/ITMyCppInterface.h
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// AUTOGENERATED FILE - DO NOT MODIFY!
2+
// The file included here provides default JNI_OnLoad and JNI_OnUnload implementations.
3+
// Disable the generation of this file with `--jni-generate-main=false`.
4+
#include "djinni/jni/djinni_jni_main.hpp"

src/it/resources/expected/my_enum/generated-files.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
src/it/resources/result/my_enum/cpp-headers/my_enum.hpp
22
src/it/resources/result/my_enum/java/MyEnum.java
3+
src/it/resources/result/my_enum/jni/djinni_jni_main.cpp
34
src/it/resources/result/my_enum/jni-headers/my_enum.hpp
45
src/it/resources/result/my_enum/objc-headers/ITMyEnum.h
56
src/it/resources/result/my_enum/objcpp/ITMyEnum+Private.h

0 commit comments

Comments
 (0)