diff --git a/native/android/app/CMakeLists.txt b/native/android/app/CMakeLists.txt index 4f761007c..e9097e2c8 100644 --- a/native/android/app/CMakeLists.txt +++ b/native/android/app/CMakeLists.txt @@ -1,264 +1,264 @@ # For more information about using CMake with Android Studio, read the # documentation: https://d.android.com/studio/projects/add-native-code.html project(comm CXX C) set(CMAKE_CXX_STANDARD 14) # C0103 is a naming convention, but the variable names which need to be set # are determined by the upstream project # cmake-lint: disable=C0103 # Disable line length as some paths are hard to reduce without becoming cryptic # cmake-lint: disable=C0301 # Sets the minimum version of CMake required to build the native library. cmake_minimum_required(VERSION 3.18) # Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK. set(PACKAGE_NAME "comm_jni_module") find_package(fbjni REQUIRED CONFIG) set(BUILD_TESTING OFF) set(HAVE_SYMBOLIZE OFF) set(WITH_GTEST OFF CACHE BOOL "Use googletest" FORCE) set(WITH_GFLAGS OFF CACHE BOOL "Use gflags" FORCE) # General set(_third_party_dir ${CMAKE_CURRENT_SOURCE_DIR}/build/third-party-ndk) set(_android_build_dir build/${CMAKE_ANDROID_ARCH_ABI}) # Protobuf set(protobuf_BUILD_TESTS OFF) # gRPC building parameters set(gRPC_BUILD_CSHARP_EXT OFF) set(gRPC_SSL_PROVIDER "package" CACHE STRING "SSL library provider") # gRPC client include(FetchContent) if(CMAKE_ANDROID_ARCH_ABI STREQUAL arm64-v8a) set(Rust_CARGO_TARGET aarch64-linux-android) elseif(CMAKE_ANDROID_ARCH_ABI STREQUAL x86) set(Rust_CARGO_TARGET i686-linux-android) elseif(CMAKE_ANDROID_ARCH_ABI STREQUAL x86_64) set(Rust_CARGO_TARGET x86_64-linux-android) elseif(CMAKE_ANDROID_ARCH_ABI STREQUAL armeabi-v7a) set(Rust_CARGO_TARGET arm-linux-androideabi) endif() string(TOLOWER ${CMAKE_HOST_SYSTEM_NAME} CMAKE_HOST_SYSTEM_NAME_LOWER) set(AR $ENV{ANDROID_HOME}/ndk/${NDK_VERSION}/toolchains/llvm/prebuilt/${CMAKE_HOST_SYSTEM_NAME_LOWER}-x86_64/bin/${Rust_CARGO_TARGET}-ar) FetchContent_Declare( Corrosion GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git GIT_TAG v0.2.1 ) FetchContent_MakeAvailable(Corrosion) include(../../../shared/cmake/corrosion-cxx.cmake) add_library_rust(PATH ../../native_rust_library NAMESPACE comm) # Disable unused plugins set(gRPC_BUILD_GRPC_PHP_PLUGIN OFF) set(gRPC_BUILD_GRPC_RUBY_PLUGIN OFF) set(gRPC_BUILD_GRPC_PYTHON_PLUGIN OFF) set(gRPC_BUILD_GRPC_CSHARP_PLUGIN OFF) # We're updating parameters below for Cmake's find_OpenSSL() function set(OPENSSL_ROOT_DIR "${_third_party_dir}/openssl/openssl-${OPENSSL_VERSION}/${_android_build_dir}" ) list(APPEND CMAKE_FIND_ROOT_PATH "${OPENSSL_ROOT_DIR}") add_subdirectory(${_third_party_dir}/grpc/grpc-${GRPC_VERSION}/ ${CMAKE_CURRENT_BINARY_DIR}/grpc/ EXCLUDE_FROM_ALL ) add_subdirectory(${_third_party_dir}/glog/glog-${GLOG_VERSION}/) add_subdirectory(../../node_modules/olm ./build) set(_node_modules_dir ${CMAKE_CURRENT_SOURCE_DIR}/../../node_modules) set(_react_native_dir ${_node_modules_dir}/react-native) string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER) include_directories( ${_react_native_dir}/React ${_react_native_dir}/React/Base ${_react_native_dir}/ReactCommon ${_react_native_dir}/ReactCommon/jsi ${_react_native_dir}/ReactCommon/callinvoker ${_react_native_dir}/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni/ReactCommon # OpenSSL ${_third_party_dir}/openssl/openssl-${OPENSSL_VERSION}/build/${CMAKE_ANDROID_ARCH_ABI}/include # SQLCipher amalgamation ${_node_modules_dir}/@commapp/sqlcipher-amalgamation/src # SQLite ORM ../../cpp/third-party/sqlite_orm # symlinked React Native headers ../headers # external libs ${_third_party_dir}/folly ${_third_party_dir}/boost/boost ${_third_party_dir}/double-conversion # comm android specific code ./src/cpp # comm native mutual code ../../cpp/CommonCpp/ ../../cpp/CommonCpp/NativeModules ../../cpp/CommonCpp/NativeModules/InternalModules ../../cpp/CommonCpp/NativeModules/PersistentStorageUtilities ../../cpp/CommonCpp/NativeModules/PersistentStorageUtilities/ThreadOperationsUtilities ../../cpp/CommonCpp/NativeModules/PersistentStorageUtilities/MessageOperationsUtilities ../../cpp/CommonCpp/NativeModules/PersistentStorageUtilities/MessageOperationsUtilities/MessageSpecs ../../cpp/CommonCpp/DatabaseManagers ../../cpp/CommonCpp/Tools ../../../shared/protos/_generated ../../cpp/CommonCpp/grpc # native rust library - ./.cxx/cmake/${CMAKE_BUILD_TYPE_LOWER}/${CMAKE_ANDROID_ARCH_ABI}/cargo/build/${Rust_CARGO_TARGET}/cxxbridge/native_rust_library/src + ${native_rust_library_include_dir} ) # search for all cpp files in this directory file(GLOB SQLCIPHER "${_node_modules_dir}/@commapp/sqlcipher-amalgamation/src/*.c" ) file(GLOB_RECURSE COMMON_NATIVE_CODE "../../cpp/CommonCpp/**/*.cpp") file(GLOB ANDROID_NATIVE_CODE "./src/cpp/*.cpp") file(GLOB DOUBLE_CONVERSION_SOURCES "${_third_party_dir}/double-conversion/double-conversion/*.cc" ) file(GLOB GRPC_CODE "../../../shared/protos/_generated/*.cc") add_library( # Sets the name of the library. ${PACKAGE_NAME} # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). ${_react_native_dir}/ReactCommon/jsi/jsi/jsi.cpp ${_react_native_dir}/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni/ReactCommon/CallInvokerHolder.cpp ${_react_native_dir}/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.cpp ${_react_native_dir}/ReactCommon/react/nativemodule/core/ReactCommon/LongLivedObject.cpp ${_react_native_dir}/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleUtils.cpp # SQLCipher ${SQLCIPHER} # folly ${_third_party_dir}/folly/folly/detail/Futex.cpp ${_third_party_dir}/folly/folly/synchronization/ParkingLot.cpp ${_third_party_dir}/folly/folly/lang/SafeAssert.cpp ${_third_party_dir}/folly/folly/FileUtil.cpp ${_third_party_dir}/folly/folly/Subprocess.cpp ${_third_party_dir}/folly/folly/File.cpp ${_third_party_dir}/folly/folly/Format.cpp ${_third_party_dir}/folly/folly/Conv.cpp ${_third_party_dir}/folly/folly/io/IOBuf.cpp ${_third_party_dir}/folly/folly/memory/detail/MallocImpl.cpp ${_third_party_dir}/folly/folly/ScopeGuard.cpp ${_third_party_dir}/folly/folly/hash/SpookyHashV2.cpp ${_third_party_dir}/folly/folly/io/IOBufQueue.cpp ${_third_party_dir}/folly/folly/lang/Assume.cpp ${_third_party_dir}/folly/folly/String.cpp ${_third_party_dir}/folly/folly/portability/SysUio.cpp ${_third_party_dir}/folly/folly/net/NetOps.cpp ${_third_party_dir}/folly/folly/dynamic.cpp ${_third_party_dir}/folly/folly/json.cpp ${_third_party_dir}/folly/folly/json_pointer.cpp ${_third_party_dir}/folly/folly/Unicode.cpp # double-conversion ${DOUBLE_CONVERSION_SOURCES} ${GRPC_CODE} # comm code ${ANDROID_NATIVE_CODE} ${COMMON_NATIVE_CODE} ) add_definitions( # Folly -DFOLLY_NO_CONFIG=1 -DFOLLY_HAVE_CLOCK_GETTIME=1 -DFOLLY_HAVE_MEMRCHR=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_MOBILE=1 # SQLCipher -DSQLITE_THREADSAFE=0 -DSQLITE_HAS_CODEC -DSQLITE_TEMP_STORE=2 -DSQLCIPHER_CRYPTO_OPENSSL ) find_library(log-lib log) add_library( # OpenSSL Crypto lib openssl-crypto STATIC IMPORTED ) set_target_properties( # OpenSSL Crypto lib openssl-crypto PROPERTIES IMPORTED_LOCATION "${_third_party_dir}/openssl/openssl-${OPENSSL_VERSION}/build/${CMAKE_ANDROID_ARCH_ABI}/lib/libcrypto.a" ) add_library( # OpenSSL SSL lib openssl-ssl STATIC IMPORTED ) set_target_properties( # OpenSSL SSL lib openssl-ssl PROPERTIES IMPORTED_LOCATION "${_third_party_dir}/openssl/openssl-${OPENSSL_VERSION}/build/${CMAKE_ANDROID_ARCH_ABI}/lib/libssl.a" ) target_link_libraries( ${PACKAGE_NAME} fbjni::fbjni android ${log-lib} glog::glog olm openssl-crypto openssl-ssl grpc++ libprotobuf comm::native_rust_library ) # add a dummy library which is required by CallInvokerHolderImpl.java add_library( turbomodulejsijni # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). ./src/cpp/dummy.cpp ) diff --git a/shared/cmake/corrosion-cxx.cmake b/shared/cmake/corrosion-cxx.cmake index bdc12eae2..76a9573b0 100644 --- a/shared/cmake/corrosion-cxx.cmake +++ b/shared/cmake/corrosion-cxx.cmake @@ -1,103 +1,108 @@ # Creates a target including rust lib and cxxbridge which is # named as ${NAMESPACE}::${_LIB_PATH_STEM} # <_LIB_PATH_STEM> must match the crate name: # "path/to/myrustcrate" -> "libmyrustcrate.a" function(add_library_rust) set(value_keywords PATH NAMESPACE CXX_BRIDGE_SOURCE_FILE) cmake_parse_arguments( rust_lib "${OPTIONS}" "${value_keywords}" "${MULTI_value_KEYWORDS}" ${ARGN} ) if("${Rust_CARGO_TARGET}" STREQUAL "") message( FATAL_ERROR "Rust_CARGO_TARGET is not detected and empty") endif() if("${rust_lib_PATH}" STREQUAL "") message( FATAL_ERROR "add_library_rust called without a given path to root of a rust crate") endif() if("${rust_lib_NAMESPACE}" STREQUAL "") message( FATAL_ERROR "Must supply a namespace given by keyvalue NAMESPACE ") endif() - if("${rust_lib_CXX_BRIDGE_SOURCE_FILE}" STREQUAL "") - set(rust_lib_CXX_BRIDGE_SOURCE_FILE "src/lib.rs") - endif() + set(rust_lib_SOURCE_FOLDER "src") if(NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/${rust_lib_PATH}/Cargo.toml") message( FATAL_ERROR "${CMAKE_CURRENT_LIST_DIR}/${rust_lib_PATH} doesn't contain a Cargo.toml") endif() set(lib_path ${rust_lib_PATH}) set(namespace ${rust_lib_NAMESPACE}) - set(cxx_bridge_source_file ${rust_lib_CXX_BRIDGE_SOURCE_FILE}) + set(cxx_bridge_source_file "${rust_lib_SOURCE_FOLDER}/lib.rs") corrosion_import_crate(MANIFEST_PATH "${lib_path}/Cargo.toml") get_filename_component(_LIB_PATH_STEM ${lib_path} NAME) message(STATUS "Library stem path: ${_LIB_PATH_STEM}") # Set AR env var if it's present (Android-specific) if(AR) corrosion_set_env_vars(${_LIB_PATH_STEM} "AR=${AR}") endif() # Set cxxbridge values set( cxx_bridge_binary_folder ${CMAKE_BINARY_DIR}/cargo/build/${Rust_CARGO_TARGET}/cxxbridge) set( common_header ${cxx_bridge_binary_folder}/rust/cxx.h) + # We name the variable dynamically in case a single + # build has multiple entrypoints into this script + # cmake-lint: disable=C0103 + set( + "${_LIB_PATH_STEM}_include_dir" + ${cxx_bridge_binary_folder}/${_LIB_PATH_STEM}/${rust_lib_SOURCE_FOLDER} + PARENT_SCOPE) set( binding_header - ${cxx_bridge_binary_folder}/${_LIB_PATH_STEM}/${cxx_bridge_source_file}.h) + ${${_LIB_PATH_STEM}_include_dir}/${cxx_bridge_source_file}.h) set( binding_source ${cxx_bridge_binary_folder}/${_LIB_PATH_STEM}/${cxx_bridge_source_file}.cc) set( cxx_binding_include_dir ${cxx_bridge_binary_folder}) # Create cxxbridge target add_custom_command( OUTPUT ${common_header} ${binding_header} ${binding_source} COMMAND DEPENDS ${_LIB_PATH_STEM}-static COMMENT "Fixing cmake to find source files" ) add_library(${_LIB_PATH_STEM}_cxxbridge ${common_header} ${binding_header} ${binding_source} ) target_include_directories(${_LIB_PATH_STEM}_cxxbridge PUBLIC ${cxx_binding_include_dir} ) # Create total target with alias with given namespace add_library(${_LIB_PATH_STEM}-total INTERFACE) target_link_libraries(${_LIB_PATH_STEM}-total INTERFACE ${_LIB_PATH_STEM}_cxxbridge ${_LIB_PATH_STEM} ) # For end-user to link into project add_library(${namespace}::${_LIB_PATH_STEM} ALIAS ${_LIB_PATH_STEM}-total) endfunction(add_library_rust)