From 55cbe69ad5876c7ea380cb261609a2693a8813ba Mon Sep 17 00:00:00 2001 From: GarfieldHan <2652609017@qq.com> Date: Thu, 18 Jul 2024 10:27:54 +0800 Subject: [PATCH] Upgrade libcxx and use c++ 23 1. Meet latest glaze's requirements, and upgrade it 2. Get rid of glaze_write_json to avoid using iostream, which made binary size increased. Use C methods instead. Signed-off-by: GarfieldHan <2652609017@qq.com> --- .gitmodules | 3 - gradle/libs.versions.toml | 1 + module/build.gradle.kts | 12 ++- module/src/main/cpp/CMakeLists.txt | 10 ++- module/src/main/cpp/external/CMakeLists.txt | 97 +-------------------- module/src/main/cpp/external/libcxx | 1 - module/src/main/cpp/zygisk/main.cpp | 33 +++++-- 7 files changed, 47 insertions(+), 110 deletions(-) delete mode 160000 module/src/main/cpp/external/libcxx diff --git a/.gitmodules b/.gitmodules index 6290cd0..ccd2f91 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ -[submodule "module/src/main/cpp/external/libcxx"] - path = module/src/main/cpp/external/libcxx - url = https://github.com/topjohnwu/libcxx [submodule "module/src/main/cpp/external/LSPlt"] path = module/src/main/cpp/external/LSPlt url = https://github.com/LSPosed/LSPlt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index c530905..0049c0d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -7,6 +7,7 @@ annotation = "1.8.0" [libraries] annotation = { module = "androidx.annotation:annotation", version.ref = "annotation" } bcpkix-jdk18on = { module = "org.bouncycastle:bcpkix-jdk18on", version.ref = "bcpkix-jdk18on" } +cxx = { module = "org.lsposed.libcxx:libcxx", version = "27.0.12077973" } [plugins] agp-app = { id = "com.android.application", version.ref = "agp" } diff --git a/module/build.gradle.kts b/module/build.gradle.kts index bc84efa..622b84f 100644 --- a/module/build.gradle.kts +++ b/module/build.gradle.kts @@ -28,7 +28,7 @@ android { } externalNativeBuild { cmake { - cppFlags("-std=c++20") + cppFlags("-std=c++23") arguments( "-DANDROID_STL=none", "-DMODULE_NAME=$moduleId" @@ -36,8 +36,14 @@ android { } } } + + buildFeatures { + prefab = true + } + externalNativeBuild { cmake { + version = "3.28.0+" path("src/main/cpp/CMakeLists.txt") } } @@ -51,6 +57,10 @@ android { } } +dependencies { + compileOnly(libs.cxx) +} + androidComponents.onVariants { variant -> afterEvaluate { val variantLowered = variant.name.lowercase() diff --git a/module/src/main/cpp/CMakeLists.txt b/module/src/main/cpp/CMakeLists.txt index 572d282..f109470 100644 --- a/module/src/main/cpp/CMakeLists.txt +++ b/module/src/main/cpp/CMakeLists.txt @@ -1,6 +1,9 @@ -cmake_minimum_required(VERSION 3.22.1) +cmake_minimum_required(VERSION 3.28) project(sample) +find_package(cxx REQUIRED CONFIG) +link_libraries(cxx::cxx) + set(LINKER_FLAGS "-ffixed-x18 -Wl,--hash-style=both") set(CXX_FLAGS "${CXX_FLAGS} -fno-exceptions -fno-rtti") @@ -14,13 +17,12 @@ add_library(elf_util STATIC elf_util/elf_util.cpp) add_library(my_logging STATIC logging/logging.cpp) add_subdirectory(external) -link_libraries(cxx) target_include_directories(my_logging PUBLIC logging/include) target_include_directories(elf_util PUBLIC elf_util/include) -target_link_libraries(my_logging cxx log) -target_link_libraries(elf_util cxx lsplt my_logging) +target_link_libraries(my_logging log) +target_link_libraries(elf_util lsplt my_logging) # libutils stub add_library(utils SHARED binder/stub_utils.cpp) diff --git a/module/src/main/cpp/external/CMakeLists.txt b/module/src/main/cpp/external/CMakeLists.txt index dca30f2..0cbf52d 100644 --- a/module/src/main/cpp/external/CMakeLists.txt +++ b/module/src/main/cpp/external/CMakeLists.txt @@ -6,102 +6,7 @@ set(SOURCES LSPlt/lsplt/src/main/jni/lsplt.cc LSPlt/lsplt/src/main/jni/elf_util. add_library(lsplt STATIC ${SOURCES}) target_include_directories(lsplt PUBLIC LSPlt/lsplt/src/main/jni/include) target_include_directories(lsplt PRIVATE LSPlt/lsplt/src/main/jni) - -target_link_libraries(lsplt PUBLIC my_logging cxx) +target_link_libraries(lsplt PUBLIC my_logging) # end lsplt add_subdirectory(glaze) - -# cxx -set(LIBCXX_SOURCES - algorithm.cpp - # any.cpp - atomic.cpp - barrier.cpp - # bind.cpp - charconv.cpp - chrono.cpp - condition_variable.cpp - condition_variable_destructor.cpp - # debug.cpp - exception.cpp - filesystem/directory_iterator.cpp - filesystem/int128_builtins.cpp - filesystem/operations.cpp - functional.cpp - future.cpp - hash.cpp - ios.cpp - ios.instantiations.cpp - iostream.cpp - locale.cpp - memory.cpp - mutex.cpp - mutex_destructor.cpp - new.cpp - optional.cpp - random.cpp - # regex.cpp - # ryu/d2fixed.cpp - # ryu/d2s.cpp - # ryu/f2s.cpp - shared_mutex.cpp - stdexcept.cpp - string.cpp - # strstream.cpp - system_error.cpp - thread.cpp - # typeinfo.cpp - utility.cpp - valarray.cpp - variant.cpp - vector.cpp -) - -list(TRANSFORM LIBCXX_SOURCES PREPEND libcxx/src/) - -set(LIBCXX_EXPORT_FLAGS - -DLIBCXX_BUILDING_LIBCXXABI - -D_LIBCPP_NO_EXCEPTIONS - -D_LIBCPP_NO_RTTI - -D_LIBCPP_BUILDING_LIBRARY - -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS - -D_LIBCXXABI_NO_EXCEPTIONS -) -set(LIBCXX_FLAGS - -fvisibility-global-new-delete-hidden - -fvisibility=hidden - -fvisibility-inlines-hidden -) -set(LIBCXX_EXPORT_INCLUDES libcxx/include) -set(LIBCXX_INCLUDES libcxx/src) - -set(LIBCXXABI_SOURCES - abort_message.cpp - cxa_aux_runtime.cpp - cxa_default_handlers.cpp - cxa_exception_storage.cpp - cxa_guard.cpp - cxa_handlers.cpp - cxa_noexception.cpp - cxa_thread_atexit.cpp - cxa_vector.cpp - cxa_virtual.cpp - stdlib_exception.cpp - stdlib_new_delete.cpp - stdlib_stdexcept.cpp - stdlib_typeinfo.cpp -) -list(TRANSFORM LIBCXXABI_SOURCES PREPEND libcxx/src/abi/) -set(LIBCXXABI_FLAGS - -Wno-macro-redefined - -Wno-unknown-attributes - -DHAS_THREAD_LOCAL) -set(LIBCXXABI_INCLUDES libcxx/include/abi) - -add_library(cxx STATIC ${LIBCXX_SOURCES} ${LIBCXXABI_SOURCES}) -target_compile_options(cxx PUBLIC ${LIBCXX_EXPORT_FLAGS}) -target_compile_options(cxx PRIVATE ${LIBCXX_FLAGS} ${LIBCXXABI_FLAGS} -ffunction-sections -fdata-sections) -target_include_directories(cxx PUBLIC ${LIBCXX_EXPORT_INCLUDES}) -target_include_directories(cxx PRIVATE ${LIBCXX_INCLUDES} ${LIBCXXABI_INCLUDES}) -# end cxx diff --git a/module/src/main/cpp/external/libcxx b/module/src/main/cpp/external/libcxx deleted file mode 160000 index 12c8f4e..0000000 --- a/module/src/main/cpp/external/libcxx +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 12c8f4e93f196a700137e983dcceeac43cf807f2 diff --git a/module/src/main/cpp/zygisk/main.cpp b/module/src/main/cpp/zygisk/main.cpp index 0eb812e..552a367 100644 --- a/module/src/main/cpp/zygisk/main.cpp +++ b/module/src/main/cpp/zygisk/main.cpp @@ -111,6 +111,33 @@ private: } }; +static inline void write_spoof_configs(const struct spoof_config& spoofConfig) { + std::string buffer{}; + + if (glz::write(spoofConfig, buffer)) [[unlikely]] { + // This should NEVER happen, but it's not the reason we don't handle the case + LOGE("[write_spoof_configs] Failed to parse json to std::string"); + return; + } + + // Remove old one first + std::filesystem::remove("/data/adb/tricky_store/spoof_build_vars"sv); + FILE* file = fopen("/data/adb/tricky_store/spoof_build_vars", "w"); + if (!file) [[unlikely]] { + LOGE("[write_spoof_configs] Failed to open spoof_build_vars"); + return; + } + + if (fprintf(file, "%s", buffer.c_str()) < 0) [[unlikely]] { + LOGE("[write_spoof_configs] Failed to write spoof_build_vars"); + fclose(file); + return; + } + + fclose(file); + LOGI("[write_spoof_configs] write done!"); +} + static void companion_handler(int fd) { int enabled = access("/data/adb/tricky_store/spoof_build_vars", F_OK) == 0; write(fd, &enabled, sizeof(enabled)); @@ -123,11 +150,7 @@ static void companion_handler(int fd) { auto ec = glz::read_file_json(spoofConfig, "/data/adb/tricky_store/spoof_build_vars"sv, std::string{}); if (ec) [[unlikely]] { LOGW("[companion_handler] Failed to parse spoof_build_vars, writing and using default spoof config..."); - ec = glz::write_file_json(spoofConfig, "/data/adb/tricky_store/spoof_build_vars"sv, std::string{}); - if (ec) [[unlikely]] { - LOGW("[companion_handler] Failed to write spoof_build_vars"); - } - LOGI("[write_spoof_configs@companion_handler] write done!"); + write_spoof_configs(spoofConfig); } std::string buffer = glz::write_json(spoofConfig).value_or("");