# SPDX-License-Identifier: AGPL-3.0-or-later
cmake_minimum_required(VERSION 3.24)
project(soundcat_engine VERSION 1.0.6 LANGUAGES C CXX)

option(SOUNDCAT_BUILD_TESTS "Build Soundcat engine tests" ON)
option(SOUNDCAT_ENABLE_SANITIZERS "Enable ASan/UBSan in debug builds" OFF)
option(SOUNDCAT_WARNINGS_AS_ERRORS "Treat Soundcat engine warnings as errors" OFF)
option(SOUNDCAT_BUILD_AU "Build the macOS Audio Unit instrument" OFF)
option(SOUNDCAT_BUILD_VST3 "Build the VST3 instrument" OFF)
option(SOUNDCAT_BUILD_DESKTOP "Build the interactive Soundcat desktop instrument" OFF)
set(SOUNDCAT_JUCE_PATH "" CACHE PATH "Path to a JUCE source tree (8.0.9 recommended)")

set(CMAKE_CXX_EXTENSIONS OFF)
if(MSVC)
    # UI captions and preset metadata contain UTF-8, regardless of Windows locale.
    add_compile_options("$<$<COMPILE_LANGUAGE:C,CXX>:/utf-8>")
endif()

# Keep the portable engine independent of desktop and plugin dependencies.
include(cmake/SoundcatCore.cmake)
include(cmake/SoundcatAppCore.cmake)
include(cmake/SoundcatCoreTests.cmake)
include(cmake/SoundcatNative.cmake)

# UI-only expression builder regression tests.
include(cmake/SoundcatExpressionBuilderTests.cmake OPTIONAL)
if(TARGET soundcat_expression_builder_gui_tests)
    target_sources(soundcat_expression_builder_gui_tests PRIVATE
        apps/shared/visual/soundcat_look_and_feel.cc
        apps/shared/visual/material_painter.cc
        apps/shared/visual/morph_rail_painter.cc)
endif()

# Soundcat creative-immediacy UI regressions.
include(cmake/SoundcatCreativeWorkflowTests.cmake)

include(cmake/SoundcatDesignTests.cmake)

include(cmake/SoundcatSourceDemandTests.cmake)
include(cmake/SoundcatLayoutIdentityTests.cmake)
include(cmake/SoundcatDesignCanvasTests.cmake)
include(cmake/SoundcatRepairTests.cmake)
include(cmake/SoundcatArchitectureTests.cmake)

# Regression checks use assert() in both portable and native tests. Keep them
# active in Release builds as well; product targets retain normal NDEBUG.
get_property(soundcat_targets DIRECTORY PROPERTY BUILDSYSTEM_TARGETS)
foreach(target IN LISTS soundcat_targets)
    if(target MATCHES "^(soundcat(_.*)?|SoundcatDesktop|SoundcatPlugin)$")
        target_include_directories(${target} PRIVATE "${PROJECT_SOURCE_DIR}")
    endif()
    if(target MATCHES "^soundcat_.*tests$")
        if(MSVC)
            target_compile_options(${target} PRIVATE /UNDEBUG)
        else()
            target_compile_options(${target} PRIVATE -UNDEBUG)
        endif()
    endif()
endforeach()

# These release tests compare multiple complete engines or patch/session values
# in one synchronous fixture. Set their Windows main-thread reserve to 8 MiB.
if(MSVC)
    foreach(target IN ITEMS
            soundcat_desktop_workflow_tests
            soundcat_edit_session_tests
            soundcat_elastic_geometry_tests
            soundcat_graphite_regression_tests
            soundcat_host_tests
            soundcat_macro_mapping_command_tests
            soundcat_panel_randomization_tests
            soundcat_perform_ux_tests
            soundcat_realtime_audio_tests
            soundcat_tests
            soundcat_v1_tests
            soundcat_voice_budget_tests
    )
        if(TARGET ${target})
            target_link_options(${target} PRIVATE /STACK:8388608)
        endif()
    endforeach()
endif()
