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

[Build]: Configure JNI build for use in Java on Linux/MacOS #3411

Merged
merged 12 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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