cmake_minimum_required(VERSION 3.25)
project(PXLTape VERSION 0.8.0 LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_OSX_DEPLOYMENT_TARGET "12.0" CACHE STRING "Minimum macOS")
set(JUCE_PATH "/Users/thecore/Documents/00 Pixel Records Studio/Titty Tweeter/TittyTweeter/third_party/JUCE" CACHE PATH "JUCE 8 source")
add_subdirectory("${JUCE_PATH}" JUCE)
enable_testing()
add_executable(EngineTests Tests/EngineTests.cpp)
target_include_directories(EngineTests PRIVATE Source)
add_test(NAME EngineTests COMMAND EngineTests)
add_executable(LongTapeTests Tests/LongTapeTests.cpp)
target_include_directories(LongTapeTests PRIVATE Source)
add_test(NAME LongTapeTests COMMAND LongTapeTests)
add_executable(DrumKitTests Tests/DrumKitTests.cpp)
target_include_directories(DrumKitTests PRIVATE Source)
add_test(NAME DrumKitTests COMMAND DrumKitTests)
add_executable(DevicesTests Tests/DevicesTests.cpp)
target_include_directories(DevicesTests PRIVATE Source)
add_test(NAME DevicesTests COMMAND DevicesTests)
juce_add_plugin(PXLTape COMPANY_NAME "Pixel Records" BUNDLE_ID com.pixelrecords.pxltape
 PLUGIN_MANUFACTURER_CODE Pxrc PLUGIN_CODE PxTp PRODUCT_NAME "PXL Tape"
 FORMATS AU VST3 Standalone IS_SYNTH TRUE NEEDS_MIDI_INPUT TRUE NEEDS_MIDI_OUTPUT FALSE
 MICROPHONE_PERMISSION_ENABLED TRUE MICROPHONE_PERMISSION_TEXT "PXL Tape records audio from your selected input onto tape."
 AU_MAIN_TYPE kAudioUnitType_MusicDevice VST3_CATEGORIES Instrument Sampler COPY_PLUGIN_AFTER_BUILD FALSE)
target_sources(PXLTape PRIVATE Source/PluginProcessor.cpp Source/PluginEditor.cpp)
target_include_directories(PXLTape PUBLIC Source)
target_compile_definitions(PXLTape PUBLIC JUCE_WEB_BROWSER=0 JUCE_USE_CURL=0 JUCE_VST3_CAN_REPLACE_VST2=0)
target_link_libraries(PXLTape PUBLIC juce::juce_audio_utils juce::juce_cryptography PRIVATE juce::juce_recommended_config_flags)
foreach(app ProcessorTests TapePreview ZoggClientTests)
 juce_add_console_app(${app} PRODUCT_NAME "PXL Tape ${app}")
 target_sources(${app} PRIVATE Tests/${app}.cpp)
 target_link_libraries(${app} PRIVATE PXLTape)
 get_target_property(app_defs ${app} COMPILE_DEFINITIONS)
 list(REMOVE_ITEM app_defs JUCE_STANDALONE_APPLICATION=1)
 set_property(TARGET ${app} PROPERTY COMPILE_DEFINITIONS "${app_defs}")
endforeach()
target_sources(ProcessorTests PRIVATE Tests/RealtimeProbe.cpp)
add_test(NAME ProcessorTests COMMAND ProcessorTests)
add_test(NAME ZoggClientTests COMMAND ZoggClientTests)
if(APPLE)
 foreach(product PXLTape_Standalone PXLTape_AU PXLTape_VST3)
  add_custom_command(TARGET ${product} POST_BUILD COMMAND /usr/bin/codesign --force --deep --sign - "$<TARGET_BUNDLE_DIR:${product}>" VERBATIM)
 endforeach()
endif()
juce_add_console_app(VST3WrapperTests PRODUCT_NAME "PXL Tape VST3 Wrapper Tests")
target_sources(VST3WrapperTests PRIVATE Tests/VST3WrapperTests.cpp)
target_compile_definitions(VST3WrapperTests PRIVATE JUCE_PLUGINHOST_VST3=1 JUCE_WEB_BROWSER=0 JUCE_USE_CURL=0)
target_link_libraries(VST3WrapperTests PRIVATE juce::juce_audio_processors_headless)
add_test(NAME VST3WrapperTests COMMAND VST3WrapperTests "${CMAKE_BINARY_DIR}/PXLTape_artefacts/Release/VST3/PXL Tape.vst3")
add_executable(AUWrapperTests Tests/AUWrapperTests.cpp)
target_link_libraries(AUWrapperTests PRIVATE "-framework AudioToolbox" "-framework CoreFoundation")
add_test(NAME AUWrapperTests COMMAND AUWrapperTests "${CMAKE_BINARY_DIR}/PXLTape_artefacts/Release/AU/PXL Tape.component")
set_tests_properties(AUWrapperTests PROPERTIES SKIP_RETURN_CODE 77)
# Info.plist may change without a relink (for example, microphone permission).
# Always seal complete bundles after all generated metadata is in place.
if(APPLE)
 add_custom_target(SealBundles ALL
  COMMAND /usr/bin/codesign --force --deep --sign - "$<TARGET_BUNDLE_DIR:PXLTape_Standalone>"
  COMMAND /usr/bin/codesign --force --deep --sign - "$<TARGET_BUNDLE_DIR:PXLTape_AU>"
  COMMAND /usr/bin/codesign --force --deep --sign - "$<TARGET_BUNDLE_DIR:PXLTape_VST3>"
  DEPENDS PXLTape_Standalone PXLTape_AU PXLTape_VST3 VERBATIM)
endif()

add_executable(DemoTests Tests/DemoTests.cpp)
target_include_directories(DemoTests PRIVATE Source)
add_test(NAME DemoTests COMMAND DemoTests)
