cmake_minimum_required(VERSION 3.22.1)

project("mpcore")

# Force an optimized build regardless of the Android Gradle Plugin's own
# build variant (2026-09-05, "why is this ~100x slower than native"
# investigation - see ARM64_TRANSLATION_LAYER.md). Confirmed live via the
# actual generated build.ninja: neither this project's own C++ (guest_engine.cpp
# et al.) nor vendored Unicorn/QEMU-TCG's own C sources ever received an -O
# flag - AGP's external CMake integration never sets CMAKE_BUILD_TYPE here,
# and CMake's own default (empty CMAKE_BUILD_TYPE) means no per-build-type
# flags get added at all, i.e. plain -O0. Two separate, targeted dispatch-
# overhead fixes made zero measurable difference to a tight guest hot loop's
# wall-clock rate - this is why: a CPU emulator's performance is dominated
# by how well the COMPILER optimizes its OWN interpreter/JIT dispatch, and
# -O0 there dwarfs any micro-optimization in the C++ source. RelWithDebInfo
# (not plain Release) keeps -g/debug info for native crash symbolication -
# this project's own crash/fault diagnostics throughout this session depend
# on it. FORCE + setting it before add_subdirectory so Unicorn's nested
# CMake build (which does NOT set its own CMAKE_BUILD_TYPE) inherits it too.
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "" FORCE)

# ---- Vendored Unicorn Engine (ARM32-on-ARM64 CPU-emulation core) ----
# Only the ARM (AArch32) backend is built - this project never needs any of
# Unicorn's other target architectures. See ARM64_TRANSLATION_LAYER.md for
# why Unicorn specifically (embeddable-by-design, MIT-licensed, QEMU-TCG-
# derived JIT) was picked over a full QEMU-user-mode process.
set(UNICORN_ARCH "arm" CACHE STRING "" FORCE)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
set(UNICORN_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(UNICORN_INSTALL OFF CACHE BOOL "" FORCE)
set(UNICORN_FUZZ OFF CACHE BOOL "" FORCE)
add_subdirectory(third_party/unicorn)

add_library(${CMAKE_PROJECT_NAME} SHARED
        main.cpp
        crash_handler.cpp
        game_lifecycle_stubs.cpp
        game_lifecycle_stubs_extra.cpp
        game_lifecycle_stubs_extra2.cpp
        util/util.cpp
        util/armhook.cpp
        emu/guest_heap.cpp
        emu/guest_engine.cpp
        emu/import_shims.cpp
        emu/pthread_shim.cpp
        emu/jni_shim.cpp
        emu/gles_shim.cpp
        emu/dyncast_fastpath.cpp
        emu/libc_shims.cpp
        emu/rtti_shims.cpp
        emu/fmod_shims.cpp
        emu/zlib_accel.cpp
        emu/name_lookup_accel.cpp
        emu/profiler.cpp
        emu/guest_trace.cpp
        emu/tcg_bench.cpp
        emu/ostream_repro_test.cpp)

target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
        third_party/unicorn/include)

target_compile_features(${CMAKE_PROJECT_NAME} PRIVATE cxx_std_17)

target_link_libraries(${CMAKE_PROJECT_NAME}
        unicorn
        android
        log
        GLESv2
        z
        EGL
        jnigraphics)
