From 43cb62af16fcd3b57b7b06632fe728cbc0ea09a6 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Wed, 24 Jul 2019 11:25:40 -0400 Subject: [PATCH] fix android compile, have makefile pull in libuv for cmake on android --- Makefile | 22 ++++++++++++++---- android/build.gradle | 3 +-- cmake/unix.cmake | 21 +++++++++++++++--- include/llarp.h | 3 --- include/llarp.hpp | 4 ++++ jni/lokinet_android.cpp | 42 +++++++++++++++++++++++++++-------- llarp/CMakeLists.txt | 7 +++++- llarp/context.cpp | 6 +++++ llarp/util/android_logger.cpp | 7 +++++- llarp/util/android_logger.hpp | 6 ++--- 10 files changed, 95 insertions(+), 26 deletions(-) mode change 100755 => 100644 jni/lokinet_android.cpp diff --git a/Makefile b/Makefile index 9941b1a34..f77cd4603 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,8 @@ SIGN = gpg --sign --detach REPO := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) +BUILD_ROOT = $(REPO)/build + DESTDIR ?= CC ?= cc @@ -26,6 +28,9 @@ SHADOW_PARSE ?= $(PYTHON) $(SHADOW_SRC)/src/tools/parse-shadow.py - -m 0 --packe SHADOW_PLOT ?= $(PYTHON) $(SHADOW_SRC)/src/tools/plot-shadow.py -d $(REPO) LokiNET -c $(SHADOW_CONFIG) -r 10000 -e '.*' SHADOW_OPTS ?= +LIBUV_VERSION ?= v1.30.1 +LIBUV_PREFIX = $(BUILD_ROOT)/libuv + TESTNET_ROOT=/tmp/lokinet_testnet_tmp TESTNET_CONF=$(TESTNET_ROOT)/supervisor.conf TESTNET_LOG=$(TESTNET_ROOT)/testnet.log @@ -83,7 +88,6 @@ ifdef NINJA endif -BUILD_ROOT = $(REPO)/build SCAN_BUILD ?= scan-build @@ -113,13 +117,16 @@ LINT_FILES = $(wildcard llarp/*.cpp) LINT_CHECK = $(LINT_FILES:.cpp=.cpp-check) -clean: +clean: android-clean rm -f $(TARGETS) rm -rf $(BUILD_ROOT) rm -f $(SHADOW_PLUGIN) $(SHADOW_CONFIG) rm -f *.sig rm -f *.a *.so +android-clean: + rm -rf $(ANDROID_DIR)/.externalNativeBuild + debug-configure: mkdir -p '$(BUILD_ROOT)' (test x$(TOOLCHAIN) = x && $(CONFIG_CMD) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DCMAKE_C_FLAGS='$(CFLAGS)' -DCMAKE_CXX_FLAGS='$(CXXFLAGS)') || (test x$(TOOLCHAIN) != x && $(CONFIG_CMD) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DCMAKE_C_FLAGS='$(CFLAGS)' -DCMAKE_CXX_FLAGS='$(CXXFLAGS)' -DCMAKE_TOOLCHAIN_FILE=$(TOOLCHAIN) ) @@ -184,10 +191,17 @@ $(TEST_EXE): debug test: $(TEST_EXE) test x$(CROSS) = xOFF && $(TEST_EXE) || test x$(CROSS) = xON -android-gradle-prepare: + + +$(LIBUV_PREFIX): + mkdir -p $(BUILD_ROOT) + git clone -b "$(LIBUV_VERSION)" https://github.com/libuv/libuv "$(LIBUV_PREFIX)" + +android-gradle-prepare: $(LIBUV_PREFIX) rm -f $(ANDROID_PROPS) rm -f $(ANDROID_LOCAL_PROPS) echo "#auto generated don't modify kthnx" >> $(ANDROID_PROPS) + echo "libuvsrc=$(LIBUV_PREFIX)" >> $(ANDROID_PROPS) echo "lokinetCMake=$(REPO)/CMakeLists.txt" >> $(ANDROID_PROPS) echo "org.gradle.parallel=true" >> $(ANDROID_PROPS) echo "org.gradle.jvmargs=-Xmx1536M" >> $(ANDROID_PROPS) @@ -223,7 +237,7 @@ abyss: debug $(ABYSS_EXE) format: - clang-format -i $$(find daemon llarp include libabyss | grep -E '\.[h,c](pp)?$$') + clang-format -i $$(find jni daemon llarp include libabyss | grep -E '\.[h,c](pp)?$$') analyze-config: clean mkdir -p '$(BUILD_ROOT)' diff --git a/android/build.gradle b/android/build.gradle index d2d496e4f..dbe7bfe58 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -33,7 +33,7 @@ android { externalNativeBuild { cmake { targets "lokinetandroid" - arguments "-DANDROID=ON", "-DANDROID_STL=c++_static", "-DANDROID_ARM_NEON=TRUE" + arguments "-DANDROID=ON", "-DANDROID_STL=c++_static", "-DANDROID_ARM_NEON=TRUE", "-DLIBUV_ROOT=" + libuvsrc, "-DANDROID_TOOLCHAIN=clang", "-DANDROID_ARM_MODE=arm" cppFlags "-fexceptions -std=c++14 -frtti" } } @@ -68,7 +68,6 @@ android { externalNativeBuild { cmake { path lokinetCMake - } } } diff --git a/cmake/unix.cmake b/cmake/unix.cmake index 5dceb78e8..6d80525a4 100644 --- a/cmake/unix.cmake +++ b/cmake/unix.cmake @@ -9,14 +9,29 @@ if (STATIC_LINK_RUNTIME) set(LIBUV_USE_STATIC ON) endif() -find_package(LibUV 1.28.0 REQUIRED) -include_directories(SYSTEM ${LIBUV_INCLUDE_DIRS}) +if(LIBUV_ROOT) + add_subdirectory(${LIBUV_ROOT}) + set(LIBUV_INCLUDE_DIRS ${LIBUV_ROOT}/include) + set(LIBUV_LIBRARY uv_a) + add_definitions(-D_LARGEFILE_SOURCE) + add_definitions(-D_FILE_OFFSET_BITS=64) +else() + find_package(LibUV 1.28.0 REQUIRED) +endif() + +include_directories(${LIBUV_INCLUDE_DIRS}) if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") set(FS_LIB stdc++fs) get_filename_component(LIBTUNTAP_IMPL ${TT_ROOT}/tuntap-unix-linux.c ABSOLUTE) elseif(${CMAKE_SYSTEM_NAME} MATCHES "Android") - set(FS_LIB stdc++fs) + find_library(FS_LIB NAMES c++fs c++experimental stdc++fs) + if(FS_LIB STREQUAL FS_LIB-NOTFOUND) + add_subdirectory(vendor) + include_directories("${CMAKE_CURRENT_LIST_DIR}/../vendor/cppbackport-master/lib") + add_definitions(-DLOKINET_USE_CPPBACKPORT) + set(FS_LIB cppbackport) + endif() get_filename_component(LIBTUNTAP_IMPL ${TT_ROOT}/tuntap-unix-linux.c ABSOLUTE) elseif (${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD") set(LIBTUNTAP_IMPL ${TT_ROOT}/tuntap-unix-openbsd.c ${TT_ROOT}/tuntap-unix-bsd.c) diff --git a/include/llarp.h b/include/llarp.h index 7f14ea4f1..8c00f9dc9 100644 --- a/include/llarp.h +++ b/include/llarp.h @@ -115,9 +115,6 @@ extern "C" llarp::RouterContact * llarp_main_getDatabase(struct llarp_main *ptr, byte_t *pk); - llarp::handlers::TunEndpoint * - main_router_getFirstTunEndpoint(struct llarp_main *ptr); - llarp_tun_io * main_router_getRange(struct llarp_main *ptr); diff --git a/include/llarp.hpp b/include/llarp.hpp index c723bf0ea..d380350eb 100644 --- a/include/llarp.hpp +++ b/include/llarp.hpp @@ -14,6 +14,7 @@ struct llarp_ev_loop; struct llarp_nodedb; struct llarp_nodedb_iter; +struct llarp_main; namespace llarp { @@ -118,4 +119,7 @@ namespace llarp }; } // namespace llarp +llarp::Context * +llarp_main_get_context(llarp_main *m); + #endif diff --git a/jni/lokinet_android.cpp b/jni/lokinet_android.cpp old mode 100755 new mode 100644 index ea21814d1..2ecc41243 --- a/jni/lokinet_android.cpp +++ b/jni/lokinet_android.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include #include @@ -75,13 +77,33 @@ struct AndroidMain const char* GetIfAddr() { + std::string addr; if(m_impl) { - auto tun = main_router_getFirstTunEndpoint(m_impl); - if(tun) - return tun->tunif.ifaddr; + auto* ctx = llarp_main_get_context(m_impl); + if(!ctx) + return ""; + + ctx->router->hiddenServiceContext().ForEachService( + [&addr](const std::string&, + const llarp::service::Endpoint_ptr& ep) -> bool { + if(addr.empty()) + { + if(ep->HasIfAddr()) + { + // TODO: v4 + const auto ip = ep->GetIfAddr(); + if(ip.h) + { + addr = ip.ToString(); + return false; + } + } + } + return true; + }); } - return ""; + return addr.c_str(); } int @@ -89,9 +111,9 @@ struct AndroidMain { if(m_impl) { - auto tun = main_router_getFirstTunEndpoint(m_impl); - if(tun) - return tun->tunif.netmask; + auto* ctx = llarp_main_get_context(m_impl); + if(!ctx) + return -1; } return -1; } @@ -99,8 +121,10 @@ struct AndroidMain void SetVPN_FD(int rfd, int wfd) { - if(m_impl) - llarp_main_inject_vpn_fd(m_impl, rfd, wfd); + (void)rfd; + (void)wfd; + // if(m_impl) + // llarp_main_inject_vpn_fd(m_impl, rfd, wfd); } /// stop daemon thread diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index 0a19450d6..1ba84dc46 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -53,7 +53,12 @@ set(LIB_UTIL_SRC add_library(${UTIL_LIB} STATIC ${LIB_UTIL_SRC}) target_include_directories(${UTIL_LIB} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include) -target_link_libraries(${UTIL_LIB} PUBLIC ${CRYPTOGRAPHY_LIB} ${FS_LIB}) +if(ANDROID) + set(LOG_LIB log) +endif() + +target_link_libraries(${UTIL_LIB} PUBLIC ${CRYPTOGRAPHY_LIB} ${FS_LIB} ${LOG_LIB}) + target_link_libraries_system(${UTIL_LIB} absl::synchronization absl::hash absl::container nlohmann_json::nlohmann_json) # cut back on fluff diff --git a/llarp/context.cpp b/llarp/context.cpp index a3deb4c9f..c01a7454a 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -399,6 +399,12 @@ struct llarp_main std::unique_ptr< llarp::Context > ctx; }; +llarp::Context * +llarp_main_get_context(llarp_main *m) +{ + return m->ctx.get(); +} + extern "C" { struct llarp_main * diff --git a/llarp/util/android_logger.cpp b/llarp/util/android_logger.cpp index 6544b5ef1..a342b98e7 100644 --- a/llarp/util/android_logger.cpp +++ b/llarp/util/android_logger.cpp @@ -6,7 +6,8 @@ namespace llarp { void AndroidLogStream::PreLog(std::stringstream& ss, LogLevel lvl, - const char* fname, int lineno, const std::string&) const + const char* fname, int lineno, + const std::string&) const { switch(lvl) { @@ -35,6 +36,10 @@ namespace llarp { } + void AndroidLogStream::Tick(llarp_time_t) + { + } + void AndroidLogStream::Print(LogLevel lvl, const char* tag, const std::string& msg) { diff --git a/llarp/util/android_logger.hpp b/llarp/util/android_logger.hpp index c281c87ba..2e52bb853 100644 --- a/llarp/util/android_logger.hpp +++ b/llarp/util/android_logger.hpp @@ -9,11 +9,11 @@ namespace llarp struct AndroidLogStream : public ILogStream { void - PreLog(std::stringstream& s, LogLevel lvl, const char* fname, - int lineno, const std::string& nodename) const override; + PreLog(std::stringstream& s, LogLevel lvl, const char* fname, int lineno, + const std::string& nodename) const override; void - Print(LogLevel lvl,const char* filename, const std::string& msg) override; + Print(LogLevel lvl, const char* filename, const std::string& msg) override; void PostLog(std::stringstream&) const override;