Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[monodroid] Use right calling convention on Windows (#2525)
Context: dotnet/java-interop#403 On Windows, JNI defines `JNICALL` as `__stdcall`: most Java APIs need to use the `__stdcall` calling convention. In particular this includes the function pointers within the `JNIEnv` struct: `JNIEnv::GetVersion()` is a `__stdcall` function pointer, as well as most other function pointers: // <jni.h> /* partial */ struct JNINativeInterface_ { jint (JNICALL *GetVersion)(JNIEnv *env); }; `JNICALL` is `__stdcall` on Windows, and undefined (cdecl) elsewhere. Unfortunately, we build `src/monodroid` for Windows *from macOS* with MXE, and we didn't have a full or complete JDK Windows installation. The result is that we have a *calling convention mismatch* between e.g. `libmono-android.debug.dll` and `jvm.dll`, which can result in runtime call stack corruption. Fix this by adding a new `jni/win32/jni_md.h` file and inserting `jni/win32` into CMake's `include_directories` *before* the OS-native JDK include directory, e.g. `/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/include/darwin`, ensuring that `JNICALL` is set to `__stdcall` when compiling *for* Windows *from* macOS. This allows us to cross-compile for Windows without installing the Java SDK for Windows.
- Loading branch information