cmake_minimum_required(VERSION 3.8)

project (EKA2L1)

# Clang 14 is required to compile nicely with C++ 20
set (CMAKE_CXX_STANDARD 20)

option(EKA2L1_BUILD_TOOLS "Build EKA2L1's tool" ON)
option(EKA2L1_BUILD_TESTS "Build EKA2L1's tests" ON)
option(EKA2L1_ENABLE_SCRIPTING_ABILITY "Enable to script with Python or Lua" ON)
option(EKA2L1_ENABLE_UNEXPECTED_EXCEPTION_HANDLER "Enable EKA2L1 to dump unexpected exception" OFF)
option(EKA2L1_BUILD_VULKAN_BACKEND "Build Vulkan backend" OFF)
option(EKA2L1_DEPLOY_DMG "Deploy EKA2L1 as .dmg" OFF)
option(EKA2L1_BUILD_PATCH "Enable building Symbian's DLL patches using Symbian SDK" OFF)
option(EKA2L1_ENABLE_DISCORD_RICH_PRESENCE "Enable support for Discord Rich Presence" OFF)

set (CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set (ROOT ${CMAKE_CURRENT_SOURCE_DIR})

find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
    set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
    set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")

    message("Using CCache for building")
endif()

if (MSVC)
    add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
    add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
    add_definitions(-D_DISABLE_VECTOR_ANNOTATION)
endif(MSVC)

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    add_compile_options(-Wno-error)
endif()

if (EKA2L1_BUILD_VULKAN_BACKEND)
    set (BUILD_WITH_VULKAN 1)
else()
    set (BUILD_WITH_VULKAN 0)
endif()

if (EKA2L1_ENABLE_SCRIPTING_ABILITY)
    set (ENABLE_SCRIPTING 1)
endif(EKA2L1_ENABLE_SCRIPTING_ABILITY)

if (CI)
    set (BUILD_FOR_USER 1)
endif (CI)

set (ENABLE_SEH_HANDLER 0)

if (EKA2L1_ENABLE_UNEXPECTED_EXCEPTION_HANDLER)
    set(ENABLE_SEH_HANDLER 1)
endif (EKA2L1_ENABLE_UNEXPECTED_EXCEPTION_HANDLER)

if (EKA2L1_ENABLE_DISCORD_RICH_PRESENCE AND NOT ANDROID AND NOT (CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
    set(ENABLE_DISCORD_RICH_PRESENCE 1)
else()
    set(ENABLE_DISCORD_RICH_PRESENCE 0)
endif()

if (EMSCRIPTEN)
    # All TUs need -pthread so wasm-ld accepts --shared-memory (atomics/bulk-memory features)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
    # emscripten's clang does not fully support consteval; disable it for fmt
    add_compile_definitions(FMT_USE_CONSTEVAL=0)
    # yaml-cpp and several emulator subsystems throw/catch C++ exceptions during
    # startup. This must be enabled before adding subdirectories so static libs
    # like config/epoc/yaml-cpp are compiled with exception tables too.
    add_compile_options(-fexceptions)
    add_link_options("SHELL:-s DISABLE_EXCEPTION_CATCHING=0")
    # Link-time optimization across every TU/static lib for the perf build.
    # Both compile and link must carry -flto so the final wasm-ld step can do
    # cross-module inlining (notably into the CPU interpreter hot loop).
    add_compile_options(-flto)
    add_link_options(-flto)
endif()

# HarmonyOS / OpenHarmony (DevEco BiSheng toolchain sets CMAKE_SYSTEM_NAME=OHOS).
# Surfaced as EKA2L1_OHOS so the rest of the tree can branch the same way it does
# for ANDROID/EMSCRIPTEN. The frontend lives in src/emu/hos and is built by the
# DevEco hvigor pipeline, so we don't add it as a subdirectory here.
if (CMAKE_SYSTEM_NAME STREQUAL "OHOS" OR DEFINED OHOS OR DEFINED OHOS_STL)
    set (EKA2L1_OHOS TRUE)
endif()

if (EKA2L1_OHOS)
    message(STATUS "Configuring EKA2L1 core for HarmonyOS / OpenHarmony (OHOS)")
    # Make sure common/platform.h takes the OHOS branch even if the toolchain
    # doesn't predefine __OHOS__ for our preprocessor checks.
    add_compile_definitions(__OHOS__=1)

    # OHOS is aarch64; force the ARM target so dynarmic (x86-host only) is skipped
    # and the dyncom interpreter is used, mirroring the Android/WASM CPU choice.
    set (ARCHITECTURE_AARCH64 TRUE)

    # No prebuilt LuaJIT for OHOS in src/external, so scripting must be off.
    if (EKA2L1_ENABLE_SCRIPTING_ABILITY)
        message(STATUS "Disabling scripting on OHOS build (no OHOS LuaJIT prebuilt)")
        set (EKA2L1_ENABLE_SCRIPTING_ABILITY OFF CACHE BOOL "" FORCE)
        unset (ENABLE_SCRIPTING)
    endif()
endif()

add_subdirectory(src/patch)
add_subdirectory(src/external)
add_subdirectory(src/emu)

if (EKA2L1_BUILD_TOOLS)
    add_subdirectory(src/tools)
endif()

if (EKA2L1_BUILD_TESTS)
    enable_testing()
    add_subdirectory(src/tests)
endif()
