cmake_minimum_required(VERSION 3.22)

project(OhShit VERSION 1.0.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Static MSVC runtime: the plugin doesn't need the VC++ redistributable on the target machine.
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

if(APPLE AND NOT CMAKE_OSX_ARCHITECTURES)
    set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64" CACHE STRING "" FORCE)
endif()
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.13" CACHE STRING "Minimum macOS version")

option(OHSHIT_BUILD_TOOLS "Build the offline render/test tool" ON)

# ---------------------------------------------------------------- JUCE
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/libs/JUCE/CMakeLists.txt")
    add_subdirectory(libs/JUCE)
else()
    include(FetchContent)
    FetchContent_Declare(JUCE
        GIT_REPOSITORY https://github.com/juce-framework/JUCE.git
        GIT_TAG        8.0.15
        GIT_SHALLOW    ON)
    FetchContent_MakeAvailable(JUCE)
endif()

# ---------------------------------------------------------------- Neural Amp Modeler core
include(cmake/NAM.cmake)

# ---------------------------------------------------------------- DSP core (shared by plugin and tools)
add_library(OhShitDSP INTERFACE)
target_sources(OhShitDSP INTERFACE
    Source/dsp/AmpEngine.cpp
    Source/dsp/AmpChannels.cpp
    Source/dsp/InputNormalizer.cpp
    Source/dsp/NamModel.cpp)
target_include_directories(OhShitDSP INTERFACE Source)
target_link_libraries(OhShitDSP INTERFACE nam_core OhShitAssets)
# Object files of an OBJECT library only reach direct consumers; pass them on explicitly.
target_sources(OhShitDSP INTERFACE $<TARGET_OBJECTS:nam_core>)

# ---------------------------------------------------------------- Plugin
# macOS: copy into ~/Library/Audio/Plug-Ins after building. Windows' system VST3
# folder needs admin rights, so there the bundle is copied by hand.
if(APPLE)
    set(OHSHIT_COPY_AFTER_BUILD TRUE)
else()
    set(OHSHIT_COPY_AFTER_BUILD FALSE)
endif()

juce_add_plugin(OhShit
    COMPANY_NAME                "Bytecore Sound"
    COMPANY_WEBSITE             "https://bytecoresound.quietflow.ru"
    PRODUCT_NAME                "OhShit"
    BUNDLE_ID                   com.bytecoresound.ohshit
    ICON_BIG                    "${CMAKE_CURRENT_SOURCE_DIR}/Assets/icon/icon_1024.png"
    ICON_SMALL                  "${CMAKE_CURRENT_SOURCE_DIR}/Assets/icon/icon_256.png"
    IS_SYNTH                    FALSE
    NEEDS_MIDI_INPUT            FALSE
    NEEDS_MIDI_OUTPUT           FALSE
    IS_MIDI_EFFECT              FALSE
    EDITOR_WANTS_KEYBOARD_FOCUS FALSE
    COPY_PLUGIN_AFTER_BUILD     ${OHSHIT_COPY_AFTER_BUILD}
    PLUGIN_MANUFACTURER_CODE    OhSh
    PLUGIN_CODE                 OhS1
    FORMATS                     AU VST3 Standalone
    VST3_CATEGORIES             Fx Distortion
    AU_MAIN_TYPE                kAudioUnitType_Effect
    MICROPHONE_PERMISSION_ENABLED TRUE
    MICROPHONE_PERMISSION_TEXT  "OhShit needs your audio input to process the guitar signal.")

juce_add_binary_data(OhShitAssets
    HEADER_NAME OhShitAssets.h
    NAMESPACE   OhShitAssets
    SOURCES
        Assets/ir/v30_sm57.wav
        Assets/ir/greenback_sm57.wav
        Assets/nam/6505_red_mxr.nam
        Assets/nam/jcm2000_clean.nam)

# The comic UI's art layers: background and the CLEAN / HEAVY plate sprites.
juce_add_binary_data(OhShitUiAssets
    HEADER_NAME OhShitUiAssets.h
    NAMESPACE   OhShitUiAssets
    SOURCES
        Assets/ui/base.jpg
        Assets/ui/clean_on.png
        Assets/ui/clean_off.png
        Assets/ui/heavy_on.png
        Assets/ui/heavy_off.png)

target_sources(OhShit PRIVATE
    Source/PluginProcessor.cpp
    Source/PluginEditor.cpp
    Source/ui/ComicArt.cpp
    Source/ui/ComicControls.cpp
    Source/ui/ComicScene.cpp)

target_compile_definitions(OhShit PUBLIC
    JUCE_WEB_BROWSER=0
    JUCE_USE_CURL=0
    JUCE_VST3_CAN_REPLACE_VST2=0
    JUCE_DISPLAY_SPLASH_SCREEN=0
    JUCE_REPORT_APP_USAGE=0)

target_link_libraries(OhShit
    PRIVATE
        OhShitDSP
        OhShitAssets
        OhShitUiAssets
        juce::juce_audio_utils
        juce::juce_dsp
    PUBLIC
        juce::juce_recommended_config_flags
        juce::juce_recommended_lto_flags
        juce::juce_recommended_warning_flags)

# ---------------------------------------------------------------- Offline render / sanity tool
# Developer tools; they are not part of the published source, so only when present.
if(OHSHIT_BUILD_TOOLS AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/Tools/RenderMain.cpp")
    juce_add_console_app(OhShitRender PRODUCT_NAME "OhShitRender")
    target_sources(OhShitRender PRIVATE Tools/RenderMain.cpp)
    target_compile_definitions(OhShitRender PRIVATE
        JUCE_WEB_BROWSER=0
        JUCE_USE_CURL=0)
    target_link_libraries(OhShitRender
        PRIVATE
            OhShitDSP
            juce::juce_audio_formats
            juce::juce_dsp
        PUBLIC
            juce::juce_recommended_config_flags
            juce::juce_recommended_warning_flags)

    # Hosts the built VST3 like a DAW would (audio, presets, state, editor).
    juce_add_console_app(PluginSmokeTest PRODUCT_NAME "PluginSmokeTest")
    target_sources(PluginSmokeTest PRIVATE Tools/PluginSmokeTest.cpp)
    target_compile_definitions(PluginSmokeTest PRIVATE
        JUCE_PLUGINHOST_VST3=1
        JUCE_MODAL_LOOPS_PERMITTED=1
        JUCE_WEB_BROWSER=0
        JUCE_USE_CURL=0)
    target_link_libraries(PluginSmokeTest
        PRIVATE
            juce::juce_audio_formats
            juce::juce_audio_processors
            juce::juce_gui_basics
        PUBLIC
            juce::juce_recommended_config_flags)
endif()
