Skip to content

Commit

Permalink
[Build]: Configure JNI build for use in Java on Linux/MacOS (#3411)
Browse files Browse the repository at this point in the history
* [Kotlin] JVM support

* [build]: Configure JNI build for use from Java on Linux/MacOS platforms

* Add Linux CI JNI workflow

* [build]: Fix wallet-core-kotlin paths in CMakeLists.txt

* [build]: Rename `TW_COMPILE_JAVA` and `TW_COMPILE_KOTLIN`

* [build]: Fix Kotlin and Android builds

* [build]: Add JNI MacOS workflow

* [build]: turn off `TrezorCryptoTests`

* [build]: Install rustup one more time

* [build]: Install rustup one more time

* [build]: Remove unnecessary step

* [build]: Move `JNI MacOS` build to Kotlin CI

* Remove `JNI MacOS` and `JNI Linux` workflows

---------

Co-authored-by: Maxim Pestryakov <17238054+MaximPestryakov@users.noreply.github.com>
  • Loading branch information
satoshiotomakan and MaximPestryakov authored Sep 5, 2023
1 parent 0e794d2 commit b809ccc
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/kotlin-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,14 @@ jobs:
source emsdk/emsdk_env.sh
tools/generate-files
- name: CMake (Java, Kotlin)
run: |
cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Debug -DTW_UNITY_BUILD=ON -DTW_COMPILE_JAVA=ON -DTW_COMPILE_KOTLIN=ON -GNinja
# The build artifact will be used in `tools/kotlin-build` in the future.
- name: Build JNI
run: |
ninja -Cbuild
- name: Build Kotlin Multiplatform
run: tools/kotlin-build
20 changes: 19 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ include(cmake/Protobuf.cmake)

# Source files
if (${ANDROID})
message("Configuring for JNI")
message("Configuring for Android JNI")
file(GLOB_RECURSE core_sources src/*.c src/*.cc src/*.cpp src/*.h jni/cpp/*.cpp jni/cpp/*.h)
if (${KOTLIN})
file(GLOB_RECURSE specific_sources
Expand All @@ -75,6 +75,24 @@ if (${ANDROID})
set(WALLET_CORE_BINDGEN ${WALLET_CORE_RS_TARGET_DIR}/x86_64-linux-android/release/${WALLET_CORE_RS_LIB})
endif ()
target_link_libraries(TrustWalletCore PUBLIC ${WALLET_CORE_BINDGEN} ${PROJECT_NAME}_INTERFACE PRIVATE TrezorCrypto protobuf ${log-lib} Boost::boost)
elseif (${TW_COMPILE_JAVA})
message("Configuring for JNI")
file(GLOB_RECURSE core_sources src/*.c src/*.cc src/*.cpp src/*.h jni/cpp/*.cpp jni/cpp/*.h)
if (${TW_COMPILE_KOTLIN})
file(GLOB_RECURSE specific_sources
jni/kotlin/*.h
jni/kotlin/*.c
kotlin/wallet-core-kotlin/src/commonAndroidJvmMain/cpp/generated/*.h
kotlin/wallet-core-kotlin/src/commonAndroidJvmMain/cpp/generated/*.c
)
else ()
file(GLOB_RECURSE specific_sources jni/android/*.h jni/android/*.c)
endif ()
set(sources ${core_sources} ${specific_sources})
add_library(TrustWalletCore SHARED ${sources} ${PROTO_SRCS} ${PROTO_HDRS})
find_package(JNI REQUIRED)
target_include_directories(TrustWalletCore PRIVATE ${JNI_INCLUDE_DIRS})
target_link_libraries(TrustWalletCore PUBLIC ${WALLET_CORE_BINDGEN} ${PROJECT_NAME}_INTERFACE PRIVATE TrezorCrypto protobuf Boost::boost)
else ()
message("Configuring standalone")
file(GLOB_RECURSE sources src/*.c src/*.cc src/*.cpp src/*.h)
Expand Down
2 changes: 1 addition & 1 deletion cmake/StandardSettings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ endif ()
option(TW_UNIT_TESTS "Enable the unit tests of the project" ON)
option(TW_BUILD_EXAMPLES "Enable the examples builds of the project" ON)

if (ANDROID OR IOS_PLATFORM OR TW_COMPILE_WASM)
if (ANDROID OR IOS_PLATFORM OR TW_COMPILE_WASM OR TW_COMPILE_JAVA)
set(TW_UNIT_TESTS OFF)
set(TW_BUILD_EXAMPLES OFF)
endif()
Expand Down
6 changes: 6 additions & 0 deletions jni/cpp/Random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <jni.h>
#include <string.h>
#include <stdint.h>

static JavaVM* cachedJVM;

Expand All @@ -27,7 +28,12 @@ uint32_t random32() {

void random_buffer(uint8_t *buf, size_t len) {
JNIEnv *env;

#if defined(__ANDROID__) || defined(ANDROID)
cachedJVM->AttachCurrentThread(&env, nullptr);
#else
cachedJVM->AttachCurrentThread((void **) &env, nullptr);
#endif

// SecureRandom random = new SecureRandom();
jclass secureRandomClass = env->FindClass("java/security/SecureRandom");
Expand Down
2 changes: 1 addition & 1 deletion trezor-crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ if (EMSCRIPTEN)
message(STATUS "Skip building trezor-crypto/tests")
set(TW_WARNING_FLAGS ${TW_WARNING_FLAGS} -Wno-bitwise-instead-of-logical)
else ()
if(NOT ANDROID AND NOT IOS_PLATFORM)
if(NOT ANDROID AND NOT IOS_PLATFORM AND NOT TW_COMPILE_JAVA)
add_subdirectory(crypto/tests)
endif()
endif()
Expand Down

0 comments on commit b809ccc

Please sign in to comment.