cmake_minimum_required(VERSION 3.21)
project(regolith VERSION 0.0.1 LANGUAGES C)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

option(REGOLITH_BUILD_TESTS "Build tests" ON)
option(REGOLITH_BUILD_SDL3 "Build the SDL3 backend" ON)
option(REGOLITH_SANITIZE "Build with ASan and UBSan" OFF)
if(REGOLITH_SANITIZE AND NOT MSVC AND NOT EMSCRIPTEN)
  add_compile_options(-fsanitize=address,undefined -fno-omit-frame-pointer)
  add_link_options(-fsanitize=address,undefined)
endif()

add_subdirectory(third_party)

add_library(regolith
  src/context.c
  src/nodes.c
  src/props.c
  src/types.c
  src/layout_clay.c
  src/text_stb.c
  src/lua_module.c
)
add_library(regolith::regolith ALIAS regolith)

target_include_directories(regolith
  PUBLIC  ${PROJECT_SOURCE_DIR}/include
  PRIVATE ${PROJECT_SOURCE_DIR}/src
)
target_link_libraries(regolith PUBLIC lua PRIVATE clay stb)
target_compile_definitions(regolith PRIVATE RG_BUILD)
target_compile_options(regolith PRIVATE
  $<$<NOT:$<C_COMPILER_ID:MSVC>>:-Wall -Wextra -Wpedantic>)
set_target_properties(regolith PROPERTIES
  C_STANDARD 99
  C_EXTENSIONS OFF
  C_VISIBILITY_PRESET hidden
)

if(REGOLITH_BUILD_SDL3 AND NOT EMSCRIPTEN)
  find_package(SDL3 REQUIRED CONFIG)
endif()

if(REGOLITH_BUILD_TESTS)
  enable_testing()
  add_subdirectory(tests)
endif()
