cmake_minimum_required(VERSION 3.22)
project(NAMPlayer VERSION 1.5.1)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT DEFINED JUCE_DIR)
    set(JUCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../tools/JUCE")
endif()
add_subdirectory(${JUCE_DIR} JUCE)

file(GLOB NAM_SOURCES
    "${CMAKE_CURRENT_SOURCE_DIR}/nam-core/NAM/*.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/nam-core/NAM/*/*.cpp")

juce_add_plugin(NAMPlayer
    COMPANY_NAME "Kakihara"
    PLUGIN_MANUFACTURER_CODE Kaki
    PLUGIN_CODE NmPl
    FORMATS VST3
    PRODUCT_NAME "NAM Player"
    IS_SYNTH FALSE
    NEEDS_MIDI_INPUT FALSE
    NEEDS_MIDI_OUTPUT FALSE
    IS_MIDI_EFFECT FALSE
    EDITOR_WANTS_KEYBOARD_FOCUS FALSE
    COPY_PLUGIN_AFTER_BUILD FALSE
    VST3_AUTO_MANIFEST FALSE
    VST3_CATEGORIES "Fx" "Distortion")

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

# NAM Core registers its model architectures through file-static objects.
# If the sources sit in the JUCE shared-code static library, the linker
# drops those objects (nothing references them directly) and every model
# load fails with "No config parser registered for architecture".
# Compile them as an OBJECT library and link the objects straight into
# the VST3 module so registration always runs.
add_library(NAMCore OBJECT ${NAM_SOURCES})
target_include_directories(NAMCore PRIVATE
    nam-core
    nam-core/Dependencies/eigen
    nam-core/Dependencies/nlohmann)
target_sources(NAMPlayer_VST3 PRIVATE $<TARGET_OBJECTS:NAMCore>)

target_include_directories(NAMPlayer PRIVATE
    Source
    nam-core
    nam-core/Dependencies/eigen
    nam-core/Dependencies/nlohmann)

target_compile_definitions(NAMPlayer PRIVATE
    JUCE_WEB_BROWSER=0
    JUCE_USE_CURL=0
    JUCE_VST3_CAN_REPLACE_VST2=0)

if(NOT MSVC)
    set_source_files_properties(${NAM_SOURCES} PROPERTIES COMPILE_FLAGS "-w")
endif()

if(WIN32)
    foreach(t NAMPlayer NAMPlayer_VST3)
        if(TARGET ${t})
            target_link_options(${t} PRIVATE -static-libgcc -static-libstdc++ -static)
        endif()
    endforeach()
endif()

target_link_libraries(NAMPlayer PRIVATE
    juce::juce_audio_utils
    juce::juce_dsp
    juce::juce_recommended_config_flags)

# MinGW ships windows.h in lowercase; the VST3 manifest helper includes <Windows.h>
if(TARGET juce_vst3_helper)
    target_include_directories(juce_vst3_helper PRIVATE
        "${CMAKE_CURRENT_SOURCE_DIR}/cmake/winshim")
endif()
