cmake_minimum_required(VERSION 3.24)
project(sc64_android LANGUAGES C CXX)
if(NOT ANDROID OR NOT CMAKE_ANDROID_ARCH_ABI STREQUAL "arm64-v8a")
    message(FATAL_ERROR "This profile requires the Android arm64-v8a NDK toolchain")
endif()
set(CMAKE_C_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
get_filename_component(SC64_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../.." ABSOLUTE)
set(SC64_RUNTIME_DIR "${SC64_ROOT}/local/deps/N64ModernRuntime-shell" CACHE PATH "Guarded shell runtime")
# ON (the personal APK) links the generated game into libmain.so (ADR-086); OFF builds the launcher, which
# loads a module built on a computer by `sc64_setup --target android-arm64` (ADR-089) and holds no game code.
option(SC64_STATIC_GAME "Link the generated game statically" ON)
set(SC64_ANDROID_PACKAGE "com.nomadic.sc64.personal" CACHE STRING "Application ID of the APK")
set(SC64_GENERATED_DIR "${SC64_ROOT}/local/generated/game" CACHE PATH "Private game C")
if(NOT EXISTS "${SC64_RUNTIME_DIR}/sc64-shell-patch.json")
    message(FATAL_ERROR "Prepare the guarded runtime first")
endif()
if(SC64_STATIC_GAME)
    if(NOT EXISTS "${SC64_GENERATED_DIR}/recomp_overlays.inl")
        message(FATAL_ERROR "Prepare the private game first")
    endif()
    find_package(Python3 3.10 REQUIRED COMPONENTS Interpreter)
    execute_process(COMMAND "${Python3_EXECUTABLE}" "${SC64_ROOT}/tools/desktop/game_validation.py"
        "${SC64_GENERATED_DIR}" RESULT_VARIABLE SC64_GAME_VALIDATION_RESULT
        ERROR_VARIABLE SC64_GAME_VALIDATION_ERROR)
    if(NOT SC64_GAME_VALIDATION_RESULT EQUAL 0)
        message(FATAL_ERROR "${SC64_GAME_VALIDATION_ERROR}")
    endif()
endif()
include("${SC64_ROOT}/cmake/runtime.cmake")
find_package(Threads REQUIRED)
add_compile_options(-UNDEBUG -ffunction-sections -fdata-sections
    "$<$<COMPILE_LANGUAGE:CXX>:-include>" "$<$<COMPILE_LANGUAGE:CXX>:cstdlib>")
sc64_add_runtime("${SC64_RUNTIME_DIR}" runtime EXCLUDE_FROM_ALL)
target_include_directories(librecomp PRIVATE "${SC64_ROOT}/native/runtime")
target_include_directories(ultramodern PRIVATE "${SC64_ROOT}/native/runtime")
if(SC64_STATIC_GAME)
    file(GLOB SC64_GAME_SOURCES CONFIGURE_DEPENDS "${SC64_GENERATED_DIR}/*.c")
    add_library(sc64_game STATIC ${SC64_GAME_SOURCES})
    target_include_directories(sc64_game PUBLIC "${SC64_GENERATED_DIR}" "${SC64_RUNTIME_DIR}/N64Recomp/include")
    target_compile_options(sc64_game PRIVATE -O2 -fno-strict-aliasing -ffp-contract=off -fexceptions)
endif()
add_library(sc64_gfx_core STATIC
    ../gfx_core/interpreter.cpp ../gfx_core/tmem.cpp ../gfx_core/software.cpp)
target_include_directories(sc64_gfx_core PUBLIC ../gfx_core)
target_link_libraries(sc64_gfx_core PUBLIC Threads::Threads)
include("${SC64_ROOT}/cmake/gfx_optimize.cmake")
sc64_optimize_gfx_targets(sc64_gfx_core)
include("${SC64_ROOT}/cmake/shell.cmake")
add_executable(sc64_guest_fpu_test "${SC64_ROOT}/tests/native/test_guest_fpu.cpp")
target_include_directories(sc64_guest_fpu_test PRIVATE "${SC64_ROOT}/native/runtime"
    "${SC64_RUNTIME_DIR}/N64Recomp/include")
target_compile_options(sc64_guest_fpu_test PRIVATE -ffp-contract=off -frounding-math)
