Skip to content

Commit

Permalink
static libraries with shared library wrapper (working with haveno on …
Browse files Browse the repository at this point in the history
  • Loading branch information
nsec1 committed Jun 27, 2024
1 parent 15d4918 commit f8c298a
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 21 deletions.
238 changes: 229 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ set(MONERO_CPP_SRC "${MONERO_CPP}/src")
set(MONERO_PROJECT ${MONERO_CPP}/external/monero-project)
set(MONERO_PROJECT_SRC "${MONERO_PROJECT}/src")

list(APPEND CMAKE_MODULE_PATH "${MONERO_PROJECT}/cmake")
list(APPEND CMAKE_MODULE_PATH "${MONERO_CPP}/cmake")

# check JAVA_HOME
if(NOT DEFINED ENV{JAVA_HOME} OR "$ENV{JAVA_HOME}" STREQUAL "")
message(FATAL_ERROR "JAVA_HOME variable not set, for example: export JAVA_HOME=/path/to/jdk")
Expand All @@ -50,12 +53,33 @@ endif()

message(STATUS EXTRA_LIBRARIES: ${EXTRA_LIBRARIES})

find_library(ZMQ_LIB zmq)

############
# Protobuf
############

if (NOT APPLE)
include(FindProtobuf)
set(Protobuf_USE_STATIC_LIBS ON)
find_package(Protobuf)
message(STATUS "Protobuf lib: ${Protobuf_LIBRARY}, inc: ${Protobuf_INCLUDE_DIR}, protoc: ${Protobuf_PROTOC_EXECUTABLE}")
endif()

############
# LibUSB
############

find_library(usb_LIBRARY NAMES usb-1.0 libusb usb)
set(LibUSB_LIBRARIES ${usb_LIBRARY})

############
# Boost
############

set(Boost_NO_BOOST_CMAKE 1)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost 1.58 QUIET REQUIRED COMPONENTS chrono date_time filesystem program_options regex serialization wserialization system thread)
message(STATUS "Using Boost include dir at ${Boost_INCLUDE_DIR}")

Expand Down Expand Up @@ -86,20 +110,174 @@ if (WIN32)
list(APPEND OPENSSL_LIBRARIES ws2_32 crypt32)
endif()

############
# libsodium
############

find_library(SODIUM_LIBRARY sodium REQUIRED)
message(STATUS "Using libsodium library at ${SODIUM_LIBRARY}")

############
# HIDAPI
############

if(APPLE)
include_directories(SYSTEM /usr/include/malloc)
if(POLICY CMP0042)
cmake_policy(SET CMP0042 NEW)
endif()
endif()

find_package(HIDAPI REQUIRED)
message(STATUS "Using HIDAPI include dir at ${HIDAPI_INCLUDE_DIR}")
add_definitions(-DHAVE_HIDAPI)

#############
# Monero
#############

set(MONERO_PROJECT_BUILD "${MONERO_PROJECT}/build/release" CACHE STRING "Monero project build directory")
message(STATUS "Using monero-project build: " ${MONERO_PROJECT_BUILD})

list(APPEND CMAKE_MODULE_PATH "${MONERO_PROJECT}/cmake")

add_library(wallet STATIC IMPORTED)
set_target_properties(wallet PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/lib/libwallet.a)

# libwallet-crypto.a provides x86_64 asm for some wallet functions
if (EXISTS ${MONERO_PROJECT_BUILD}/src/crypto/wallet/libwallet-crypto.a)
add_library(wallet_crypto_lib STATIC IMPORTED)
set_target_properties(wallet_crypto_lib PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/crypto/wallet/libwallet-crypto.a)
set(wallet_crypto wallet_crypto_lib)
endif()

add_library(lmdb STATIC IMPORTED)
set_target_properties(lmdb PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/external/db_drivers/liblmdb/liblmdb.a)

add_library(epee STATIC IMPORTED)
set_target_properties(epee PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/contrib/epee/src/libepee.a)

add_library(rpc_base STATIC IMPORTED)
set_target_properties(rpc_base PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/rpc/librpc_base.a)

add_library(net STATIC IMPORTED)
set_target_properties(net PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/net/libnet.a)

add_library(hardforks STATIC IMPORTED)
set_target_properties(hardforks PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/hardforks/libhardforks.a)

add_library(easylogging STATIC IMPORTED)
set_target_properties(easylogging PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/external/easylogging++/libeasylogging.a)

add_library(cryptonote_core STATIC IMPORTED)
set_target_properties(cryptonote_core PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/cryptonote_core/libcryptonote_core.a)

add_library(cryptonote_protocol STATIC IMPORTED)
set_target_properties(cryptonote_protocol PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/cryptonote_protocol/libcryptonote_protocol.a)

add_library(cryptonote_basic STATIC IMPORTED)
set_target_properties(cryptonote_basic PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/cryptonote_basic/libcryptonote_basic.a)

add_library(cryptonote_format_utils_basic STATIC IMPORTED)
set_target_properties(cryptonote_format_utils_basic PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/cryptonote_basic/libcryptonote_format_utils_basic.a)

add_library(mnemonics STATIC IMPORTED)
set_target_properties(mnemonics PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/mnemonics/libmnemonics.a)

add_library(common STATIC IMPORTED)
set_target_properties(common PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/common/libcommon.a)

add_library(cncrypto STATIC IMPORTED)
set_target_properties(cncrypto PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/crypto/libcncrypto.a)

add_library(ringct STATIC IMPORTED)
set_target_properties(ringct PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/ringct/libringct.a)

add_library(ringct_basic STATIC IMPORTED)
set_target_properties(ringct_basic PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/ringct/libringct_basic.a)

add_library(blockchain_db STATIC IMPORTED)
set_target_properties(blockchain_db PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/blockchain_db/libblockchain_db.a)

add_library(blocks STATIC IMPORTED)
set_target_properties(blocks PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/blocks/libblocks.a)

add_library(checkpoints STATIC IMPORTED)
set_target_properties(checkpoints PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/checkpoints/libcheckpoints.a)

add_library(device STATIC IMPORTED)
set_target_properties(device PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/device/libdevice.a)

add_library(device_trezor STATIC IMPORTED)
set_target_properties(device_trezor PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/device_trezor/libdevice_trezor.a)

add_library(multisig STATIC IMPORTED)
set_target_properties(multisig PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/multisig/libmultisig.a)

add_library(version STATIC IMPORTED)
set_target_properties(version PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/src/libversion.a)

add_library(randomx STATIC IMPORTED)
set_target_properties(randomx PROPERTIES IMPORTED_LOCATION
${MONERO_PROJECT_BUILD}/external/randomx/librandomx.a)

############
# Unbound
############

find_package(Unbound)
if(NOT UNBOUND_INCLUDE_DIR)
message(FATAL_ERROR "Could not find libunbound")
else()
message(STATUS "Found libunbound include (unbound.h) in ${UNBOUND_INCLUDE_DIR}")
if(UNBOUND_LIBRARIES)
message(STATUS "Found libunbound library")
if (WIN32)
add_library(unbound STATIC IMPORTED)
else()
add_library(unbound SHARED IMPORTED)
endif()
set_target_properties(unbound PROPERTIES IMPORTED_LOCATION ${UNBOUND_LIBRARIES})
else()
message(FATAL_ERROR "Found libunbound includes, but could not find libunbound library. Please make sure you have installed libunbound or libunbound-dev or the equivalent")
endif()
endif()

######################
# monero-cpp
######################

add_library(monero-cpp SHARED IMPORTED)
add_library(monero-cpp STATIC IMPORTED)

# import shared c++ library
if (APPLE)
set_target_properties(monero-cpp PROPERTIES IMPORTED_LOCATION ./libmonero-cpp.dylib)
elseif (WIN32)
set_target_properties(monero-cpp PROPERTIES IMPORTED_LOCATION ./libmonero-cpp.dll)
set_target_properties(monero-cpp PROPERTIES IMPORTED_IMPLIB ./libmonero-cpp.dll.a)
# import static c++ library
if (WIN32)
set_target_properties(monero-cpp PROPERTIES IMPORTED_LOCATION ./libmonero-cpp.lib)
else()
set_target_properties(monero-cpp PROPERTIES IMPORTED_LOCATION ./libmonero-cpp.so)
set_target_properties(monero-cpp PROPERTIES IMPORTED_LOCATION ./libmonero-cpp.a)
endif()

###############################################
Expand Down Expand Up @@ -141,12 +319,54 @@ endif()

target_link_libraries(monero-java-static
monero-cpp
wallet
rpc_base
net
lmdb
unbound
easylogging
cryptonote_core
cryptonote_protocol
cryptonote_basic
cryptonote_format_utils_basic
mnemonics
ringct
ringct_basic
common
cncrypto
blockchain_db
blocks
checkpoints
device
device_trezor
multisig
version
randomx
epee
hardforks
${wallet_crypto}
${ZMQ_LIB}
${UNBOUND_LIBRARIES}
${Boost_LIBRARIES}
${Protobuf_LIBRARY}
${LibUSB_LIBRARIES}
${OPENSSL_LIBRARIES}
${SODIUM_LIBRARY}
${HIDAPI_LIBRARIES}
${EXTRA_LIBRARIES}
)

add_library(monero-java SHARED $<TARGET_OBJECTS:monero-java-static>)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/null.cpp" "")
add_library(monero-java SHARED "${CMAKE_CURRENT_BINARY_DIR}/null.cpp")

target_link_libraries(monero-java PUBLIC
-Wl,--whole-archive
cncrypto
-Wl,--no-whole-archive
-Wl,--whole-archive
monero-java-static
-Wl,--no-whole-archive
)

if (WIN32)
target_link_options(monero-java PUBLIC "-Wl,--enable-auto-import,--export-all-symbols")
Expand Down
8 changes: 4 additions & 4 deletions bin/build_libmonero_java.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

HOST_NCORES=$(nproc 2>/dev/null || shell nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 1)

# build libmonero-cpp shared library
# build libmonero-cpp static library
cd ./external/monero-cpp/ &&
./bin/build_libmonero_cpp.sh &&
./bin/build_libmonero_cpp.sh -DBUILD_SHARED_LIBS=OFF &&

# copy libmonero-cpp shared library to ./build
# copy libmonero-cpp static library to ./build
cd ../../ &&
mkdir -p ./build &&
cp ./external/monero-cpp/build/libmonero-cpp.* ./build &&
Expand All @@ -17,4 +17,4 @@ cp ./external/monero-cpp/build/libmonero-cpp.* ./build &&
cd build &&
cmake .. &&
cmake --build . -j$HOST_NCORES &&
make .
make .
11 changes: 3 additions & 8 deletions src/main/java/monero/common/MoneroUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,18 @@ public static void loadNativeLibrary() {

// get library file names and paths
String libraryPath = "/";
String libraryCppFile = null;
String libraryJavaFile = null;
if (osName.contains("windows")) {
libraryPath += "windows/";
libraryFiles = new String[] { "libmonero-cpp.dll", "libmonero-cpp.dll.a", "libmonero-java.dll", "libmonero-java.dll.a" };
libraryCppFile = "libmonero-cpp.dll";
libraryFiles = new String[] { "libmonero-java.dll", "libmonero-java.dll.a" };
libraryJavaFile = "libmonero-java.dll";
} else if (osName.contains("linux")) {
libraryPath += osArch.contains("aarch64") ? "linux-arm64/" : "linux-x86_64/";
libraryFiles = new String[] { "libmonero-cpp.so", "libmonero-java.so" };
libraryCppFile = "libmonero-cpp.so";
libraryFiles = new String[] { "libmonero-java.so" };
libraryJavaFile = "libmonero-java.so";
} else if (osName.contains("mac")) {
libraryPath += osArch.contains("aarch64") ? "mac-arm64/" : "mac-x86_64/";
libraryFiles = new String[] { "libmonero-cpp.dylib", "libmonero-java.dylib" };
libraryCppFile = "libmonero-cpp.dylib";
libraryFiles = new String[] { "libmonero-java.dylib" };
libraryJavaFile = "libmonero-java.dylib";
} else {
throw new UnsupportedOperationException("Unsupported operating system: " + osName);
Expand All @@ -109,7 +105,6 @@ public static void loadNativeLibrary() {
}

// load native libraries
System.load(tempDir.resolve(libraryCppFile).toString());
System.load(tempDir.resolve(libraryJavaFile).toString());
} catch (Exception | UnsatisfiedLinkError e) {
throw new MoneroError(e);
Expand Down

0 comments on commit f8c298a

Please sign in to comment.