# Audio X - live venue audio playback, show control and radio.
# Copyright (C) 2026 Justin L Worley
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License, version 3, as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: AGPL-3.0-only

cmake_minimum_required(VERSION 3.22)
project(AudioXEngine LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Static C++ runtime: the DLL must run on a clean PC without the Visual C++ Redistributable installed.
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

# --- Dependencies -----------------------------------------------------------
# JUCE: use a local checkout (-DAX_JUCE_DIR=C:/dev/JUCE) or fetch it.
set(AX_JUCE_DIR "" CACHE PATH "Path to a local JUCE checkout (optional)")
# Steinberg ASIO SDK: download manually (licence acceptance required) and point to its root.
set(AX_ASIO_SDK_DIR "" CACHE PATH "Path to ASIOSDK root (contains 'common' folder)")

if(AX_JUCE_DIR)
  add_subdirectory(${AX_JUCE_DIR} juce EXCLUDE_FROM_ALL)
else()
  include(FetchContent)
  FetchContent_Declare(JUCE
    GIT_REPOSITORY https://github.com/juce-framework/JUCE.git
    GIT_TAG 8.0.8
    GIT_SHALLOW TRUE)
  FetchContent_MakeAvailable(JUCE)
endif()

# --- Engine DLL -------------------------------------------------------------
add_library(audiox_engine SHARED
  src/Voice.cpp
  src/Engine.cpp
  src/Loopback.cpp
  src/Loudness.cpp
  src/Generator.cpp
  src/Upmixer.cpp
  src/Recorder.cpp
  src/Mixer.cpp
  src/CApi.cpp)

target_include_directories(audiox_engine PUBLIC include PRIVATE src)
target_compile_definitions(audiox_engine PRIVATE
  AX_BUILD_DLL=1
  JUCE_STANDALONE_APPLICATION=0
  JUCE_WEB_BROWSER=0
  JUCE_USE_CURL=0
  JUCE_DISPLAY_SPLASH_SCREEN=0
  JUCE_WASAPI=1
  JUCE_DIRECTSOUND=0
  JUCE_USE_WINRT_MIDI=0)

if(AX_ASIO_SDK_DIR)
  target_compile_definitions(juce_audio_devices INTERFACE JUCE_ASIO=1)
  target_include_directories(juce_audio_devices INTERFACE ${AX_ASIO_SDK_DIR}/common)
else()
  message(WARNING "AX_ASIO_SDK_DIR not set: ASIO backend disabled (WASAPI only).")
endif()

target_link_libraries(audiox_engine PRIVATE
  juce::juce_core
  juce::juce_events
  juce::juce_audio_basics
  juce::juce_audio_devices
  juce::juce_audio_formats
  juce::juce_recommended_config_flags
  juce::juce_recommended_lto_flags)

# --- CLI test harness ---------------------------------------------------------
add_executable(ax_cli tests/ax_cli.cpp)
target_link_libraries(ax_cli PRIVATE audiox_engine)
