cmake_minimum_required(VERSION 3.16)
project(NukAnim C)

add_library(nukanim INTERFACE)
target_include_directories(nukanim INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
    return()
endif()

option(NUKANIM_SANITIZE "Build the tests with AddressSanitizer and UBSan" OFF)
enable_testing()
set(NUKLEAR ${CMAKE_CURRENT_SOURCE_DIR}/external/nuklear)
if(NOT EXISTS ${NUKLEAR}/nuklear.h)
    message(FATAL_ERROR "Nuklear is missing: git submodule update --init")
endif()

if(MSVC)
    set(STRICT /W4 /WX /wd5287)
    set(QUIET /W1)
else()
    set(STRICT -std=c99 -Wall -Wextra -pedantic -Werror)
    set(QUIET -w)
    find_library(MATH m)
endif()

add_library(nuklear_tests STATIC tests/nuklear.c)
target_include_directories(nuklear_tests PUBLIC ${NUKLEAR})
target_compile_options(nuklear_tests PRIVATE ${QUIET})
if(MATH)
    target_link_libraries(nuklear_tests PUBLIC ${MATH})
endif()

foreach(test core clip path fx nk)
    add_executable(test_${test} tests/${test}.c)
    target_link_libraries(test_${test} PRIVATE nukanim nuklear_tests)
    target_compile_options(test_${test} PRIVATE ${STRICT})
    if(NUKANIM_SANITIZE)
        target_compile_options(test_${test} PRIVATE -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer)
        target_link_options(test_${test} PRIVATE -fsanitize=address,undefined)
    endif()
    add_test(NAME ${test} COMMAND test_${test})
endforeach()

add_library(nuklear_examples STATIC examples/nuklear.c)
target_include_directories(nuklear_examples PUBLIC ${NUKLEAR} examples)
target_compile_options(nuklear_examples PRIVATE ${QUIET})
if(MATH)
    target_link_libraries(nuklear_examples PUBLIC ${MATH})
endif()

add_executable(clips examples/clips.c)
target_link_libraries(clips PRIVATE nukanim nuklear_examples)
target_compile_options(clips PRIVATE ${STRICT})
