cmake_minimum_required(VERSION 3.20)
project(recvx_pc VERSION 0.8.2 LANGUAGES C CXX)   # the release version (pc/server reads it from here too)
include(cmake/version.cmake)

# The decomp's data files and code assume the EE's 32-bit pointers: the release game is 32-bit.
# RECVX_64BIT (experimental, docs/BUILDING.md "64-bit configuration") builds the 64-bit game, which
# converts the disc's 32-bit formats on load, with the platform layer, the server and the tests.
option(RECVX_64BIT "64-bit configuration (experimental)" OFF)
if(NOT CMAKE_SIZEOF_VOID_P EQUAL 4 AND NOT RECVX_64BIT)
    message(FATAL_ERROR "recvx_pc is built as 32-bit unless asked for the experimental 64-bit build. "
                        "Linux: -DCMAKE_TOOLCHAIN_FILE=cmake/linux-i686.cmake; Windows: -A Win32 "
                        "(-DRECVX_64BIT=ON: the experimental 64-bit build)")
endif()
if(RECVX_64BIT AND NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
    message(FATAL_ERROR "RECVX_64BIT needs a 64-bit compiler (Linux: no linux-i686 toolchain file; Windows: cmake/mingw-x86_64.cmake)")
endif()

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Debug, Release, RelWithDebInfo, MinSizeRel" FORCE)   # not -O0
endif()

# Linux: run on older glibc than the build machine's (cmake/glibc_compat.h/.c, docs/BUILDING.md "Running on older distributions (glibc)")
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    option(RECVX_GLIBC_COMPAT "Bind glibc symbols to versions up to 2.34" ON)
    if(RECVX_GLIBC_COMPAT)
        add_compile_options("$<$<COMPILE_LANGUAGE:C,CXX>:SHELL:-include ${CMAKE_CURRENT_SOURCE_DIR}/cmake/glibc_compat.h>")
        add_library(glibc_compat OBJECT cmake/glibc_compat.c)
        target_compile_options(glibc_compat PRIVATE -std=gnu11 -Wall)   # C11 + POSIX, not C23/_GNU_SOURCE (see the file)
    endif()
endif()

set(RECVX_DECOMP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/decomp" CACHE PATH "recvx-decomp checkout")

enable_testing()
add_subdirectory(pc)
