cmake_minimum_required(VERSION 3.28.1)

# Android 设置
set(CMAKE_SYSTEM_NAME Android)
set(CMAKE_SYSTEM_VERSION 33)
set(ANDROID_PLATFORM 33)
set(ANDROID_ABI arm64-v8a)
set(ANDROID_NDK "E:/working_directory/Environment/android-ndk-r29-beta2")
set(CMAKE_TOOLCHAIN_FILE "${ANDROID_NDK}/build/cmake/android.toolchain.cmake")

set(PYTHON_LIB_DIR "third_party/android-libs/lib")
set(PYTHON_INCLUDE_DIR "third_party/android-libs/include/python3.11")
if(CMAKE_BUILD_TYPE MATCHES Debug)
    add_definitions(-DDEBUG_LOG)
endif()

find_library(PYTHON_LIBRARY NAMES python3.11 PATHS ${PYTHON_LIB_DIR})

project(Android_verify LANGUAGES C CXX ASM)

include(GNUInstallDirs)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -ffunction-sections -fdata-sections")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -ffunction-sections -fdata-sections -std=c++20")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -g0")

set(CMAKE_CXX_SCAN_FOR_MODULES ON)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/build/${ANDROID_ABI})

include_directories(
        include/
        third_party/glm/
        third_party/dobby/
        ${ANDROID_NDK}/sources/android/native_app_glue
        ${PYTHON_INCLUDE_DIR}
        include/AppManager
        lib/curl/include
        lib/openssl/include
)

add_subdirectory(third_party/glm)

add_library(dobby STATIC IMPORTED)
set_target_properties(dobby PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/third_party/dobby/libdobby.a)

add_library(zlib STATIC IMPORTED)
set_target_properties(zlib PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/zlib/lib/libz.a)

add_library(ssl STATIC IMPORTED)
set_target_properties(ssl PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/openssl/lib/libssl.a)

add_library(crypto STATIC IMPORTED)
set_target_properties(crypto PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/openssl/lib/libcrypto.a)

add_library(curl STATIC IMPORTED)
set_target_properties(curl PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/curl/lib/libcurl.a)

set(asm_syscall
        src/asm/syscall.s
)

set(AppManager
        src/AppManager/Api.cpp
        src/AppManager/Encryption.cpp
        src/AppManager/Network.cpp
        src/AppManager/Utils.cpp
)

add_library(Android_verify SHARED
        ${asm_syscall}
        ${AppManager}
        src/native-lib.cpp
        src/decrypt.cpp
)

target_link_libraries(Android_verify
        PRIVATE
        dobby
        glm
        ${PYTHON_LIBRARY}
        m
        dl
        android
        EGL
        GLESv2
        GLESv3
        log
        ssl
        crypto
        zlib
        curl
)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -ffunction-sections -fdata-sections -fno-rtti -fno-exceptions")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -ffunction-sections -fdata-sections -std=c++20 -fno-rtti -fno-exceptions")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -g0")

file(WRITE ${CMAKE_BINARY_DIR}/export.lds
        "{\n  global:\n    initLib;\n raw_syscall;\n  local:\n    *;\n};\n")

target_link_options(Android_verify PRIVATE -Wl,-Map=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/Android_verify.map -Wl,--version-script=${CMAKE_BINARY_DIR}/export.lds)

add_custom_command(TARGET Android_verify POST_BUILD
        COMMAND ${ANDROID_NDK}/toolchains/llvm/prebuilt/windows-x86_64/bin/llvm-nm -g --defined-only $<TARGET_FILE:Android_verify> > ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/symbols.txt
        COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/symbols.txt ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/symbols_backup.txt
        COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/symbols.txt
        COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/Android_verify.map ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/Angel_backup.map
        COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/Android_verify.map
        COMMENT "Exporting and securing symbols"
)

add_custom_command(TARGET Android_verify POST_BUILD
        COMMAND ${ANDROID_NDK}/toolchains/llvm/prebuilt/windows-x86_64/bin/llvm-strip --strip-unneeded $<TARGET_FILE:Android_verify>
        COMMENT "Stripping unneeded symbols from Android_verify"
)