cmake_minimum_required(VERSION 3.22)
project(ChromaFlock VERSION 1.0.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_compile_options(
    $<$<CXX_COMPILER_ID:Clang,AppleClang>:-Wno-nontrivial-memcall>
)

include(FetchContent)

if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/JUCE/CMakeLists.txt")
    message(STATUS "JUCE not found locally — fetching from GitHub...")
    FetchContent_Declare(
        JUCE
        GIT_REPOSITORY https://github.com/juce-framework/JUCE.git
        GIT_TAG        8.0.6
        GIT_SHALLOW    TRUE
    )
    FetchContent_MakeAvailable(JUCE)
else()
    add_subdirectory(JUCE)
endif()

juce_add_binary_data(ChromaFlockBinaryData
    NAMESPACE BinaryData
    SOURCES
        bg.png
        logo.png
        metalknob.png
    )

if(APPLE)
    set(CHROMAFLOCK_FORMATS VST3 AU Standalone)
    set(CHROMAFLOCK_COPY TRUE)
    set(CHROMAFLOCK_AU_MAIN_TYPE AU_MAIN_TYPE kAudioUnitType_MusicDevice)
elseif(WIN32)
    set(CHROMAFLOCK_FORMATS VST3 Standalone)
    set(CHROMAFLOCK_COPY FALSE)
    set(CHROMAFLOCK_AU_MAIN_TYPE "")
else()
    set(CHROMAFLOCK_FORMATS VST3 Standalone)
    set(CHROMAFLOCK_COPY FALSE)
    set(CHROMAFLOCK_AU_MAIN_TYPE "")
endif()

juce_add_plugin(ChromaFlock
    COMPANY_NAME "ChromaFlock"
    PLUGIN_MANUFACTURER_CODE CFlk
    PLUGIN_CODE CFlk
    FORMATS ${CHROMAFLOCK_FORMATS}
    PRODUCT_NAME "ChromaFlock"
    IS_SYNTH TRUE
    NEEDS_MIDI_INPUT TRUE
    IS_MIDI_EFFECT FALSE
    EDITOR_WANTS_KEYBOARD_FOCUS FALSE
    COPY_PLUGIN_AFTER_BUILD ${CHROMAFLOCK_COPY}
    VST3_CATEGORIES Synth
    ${CHROMAFLOCK_AU_MAIN_TYPE}
    DESCRIPTION "Subtractive Synthesizer"
)

target_sources(ChromaFlock PRIVATE
    Source/PluginProcessor.cpp
    Source/PluginEditor.cpp
)

target_compile_definitions(ChromaFlock PUBLIC
    JUCE_VST3_CAN_REPLACE_VST2=0
    JUCE_WEB_BROWSER=0
    JUCE_USE_CURL=0
    JUCE_PLUGIN_WANTS_MIDI=1
    JUCE_MODAL_LOOPS_PERMITTED=0
)

# Inject the version from the VERSION file into a generated header.
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" CHROMAFLOCK_VERSION_RAW)
string(STRIP "${CHROMAFLOCK_VERSION_RAW}" CHROMAFLOCK_VERSION)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/Source/Version.h.in"
               "${CMAKE_CURRENT_BINARY_DIR}/Generated/Version.h" @ONLY)

target_include_directories(ChromaFlock PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/Generated")

target_link_libraries(ChromaFlock PRIVATE
    ChromaFlockBinaryData
    juce::juce_audio_basics
    juce::juce_audio_devices
    juce::juce_audio_formats
    juce::juce_audio_plugin_client
    juce::juce_audio_processors
    juce::juce_audio_utils
    juce::juce_core
    juce::juce_data_structures
    juce::juce_dsp
    juce::juce_events
    juce::juce_graphics
    juce::juce_gui_basics
    juce::juce_gui_extra
)

# Offline preset-loudness calibration tool (not built by default).
# Usage: ./level_normalizer          (dry run)
#        ./level_normalizer --apply  (write new masterLevels into PresetManager.h)
add_executable(level_normalizer EXCLUDE_FROM_ALL Tools/LevelNormalizer.cpp)
target_compile_definitions(level_normalizer PUBLIC
    JUCE_APPLICATION_NAME=LevelNormalizer
    JUCE_APPLICATION_VERSION=1.0.0
)
target_link_libraries(level_normalizer PRIVATE
    juce::juce_audio_processors
)
