cmake_minimum_required(VERSION 3.25)
project(OpenGP VERSION 1.0.4 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

option(OPENGP_BUILD_TESTS "Build the test suite" ON)
option(OPENGP_BUILD_GAMES "Build the games whose executables are present in orig/" ON)
option(OPENGP_GENERATED_DEBUG_INFO "Emit debug info for recompiled game code (slow, large)" OFF)
option(OPENGP_TRACE_CALLS "Track guest call stacks in recompiled code (for debugging)" OFF)
option(OPENGP_BUILD_VIDEO "Build the renderer (fetches SDL3 and glslang if not installed)" ON)
option(OPENGP_BUILD_INPUT "Build the controller and webcam libraries without the renderer (for the launcher, which builds OpenGP's libraries as part of itself)" OFF)
option(OPENGP_PACKAGE "Build the installer package: the runtime as libraries plus the installer, which builds the games on the player's machine (see docs/packaging.md)" OFF)

if(MSVC AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  message(FATAL_ERROR "OpenGP needs GCC or Clang (use clang-cl on Windows): "
                      "recompiled code relies on weak symbols, which MSVC does not support.")
endif()

# Settings shared by everything that executes guest code (the runtime, the
# interpreter and the generated code). Floating-point contraction must be off:
# the host compiler must not fuse or reorder guest FP operations, or recompiled
# code and the interpreter could produce different bits.
add_library(opengp_guest_flags INTERFACE)
if(CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
  target_compile_options(opengp_guest_flags INTERFACE
    /clang:-ffp-contract=off /clang:-fwrapv /clang:-fno-strict-aliasing)
else()
  target_compile_options(opengp_guest_flags INTERFACE
    -ffp-contract=off -fwrapv -fno-strict-aliasing)
endif()

if(CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
  # CMake's RelWithDebInfo for MSVC-style compilers uses /Ob1, which only
  # inlines functions declared inline; release builds should inline freely.
  string(REPLACE "/Ob1" "/Ob2" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
  string(REPLACE "/Ob1" "/Ob2" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
endif()

if(WIN32)
  # Standard C functions (fopen etc.) are fine; don't nag about the _s variants.
  add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
endif()

add_library(opengp_warnings INTERFACE)
if(CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
  target_compile_options(opengp_warnings INTERFACE /W4)
else()
  target_compile_options(opengp_warnings INTERFACE -Wall -Wextra)
endif()

add_library(tomlplusplus INTERFACE)
target_include_directories(tomlplusplus SYSTEM INTERFACE thirdparty/tomlplusplus)

add_library(doctest INTERFACE)
target_include_directories(doctest SYSTEM INTERFACE thirdparty/doctest)

add_subdirectory(common/gekko)
add_subdirectory(runtime)
add_subdirectory(tools/gekkorecomp)
add_subdirectory(tools/installer)

if(OPENGP_PACKAGE)
  if(NOT OPENGP_BUILD_VIDEO)
    message(FATAL_ERROR "OPENGP_PACKAGE needs OPENGP_BUILD_VIDEO")
  endif()
  include(cmake/package.cmake)
endif()

if(OPENGP_BUILD_GAMES)
  add_subdirectory(games)
endif()

if(OPENGP_BUILD_TESTS)
  enable_testing()
  add_subdirectory(tests)
endif()
