cmake_minimum_required(VERSION 3.20)
project(DNCurves VERSION 0.8.3 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
find_package(Threads REQUIRED)
add_executable(dn-core-tests tests/core_tests.cpp)
target_include_directories(dn-core-tests PRIVATE src)
target_link_libraries(dn-core-tests PRIVATE Threads::Threads)
enable_testing()
add_test(NAME curves-core COMMAND dn-core-tests)
add_executable(dn-feature-tests tests/feature_tests.cpp)
target_include_directories(dn-feature-tests PRIVATE src)
target_link_libraries(dn-feature-tests PRIVATE Threads::Threads)
add_test(NAME curves-features COMMAND dn-feature-tests)
add_executable(dn-host-tests tests/host_tests.cpp)
target_include_directories(dn-host-tests PRIVATE src)
target_link_libraries(dn-host-tests PRIVATE Threads::Threads)
add_test(NAME photoshop-host-simulation COMMAND dn-host-tests)
add_executable(dn-installation-tests tests/installation_tests.cpp)
target_include_directories(dn-installation-tests PRIVATE src)
add_test(NAME installation-files COMMAND dn-installation-tests)
if(WIN32)
    enable_language(RC)
    set_target_properties(dn-core-tests PROPERTIES OUTPUT_NAME "DN-Core-Tests")
    set_target_properties(dn-feature-tests PROPERTIES OUTPUT_NAME "DN-Feature-Tests")
    set_target_properties(dn-host-tests PROPERTIES OUTPUT_NAME "DN-Host-Tests")
    set_target_properties(dn-installation-tests PROPERTIES OUTPUT_NAME "DN-Installation-Tests")
    set(PHOTOSHOP_SDK "" CACHE PATH "Adobe SDK root, containing pluginsdk")
    if(EXISTS "${PHOTOSHOP_SDK}/pluginsdk")
        set(DN_SDK "${PHOTOSHOP_SDK}/pluginsdk")
    else()
        set(DN_SDK "${PHOTOSHOP_SDK}")
    endif()
    if(NOT EXISTS "${DN_SDK}/photoshopapi/photoshop/PIFilter.h")
        message(FATAL_ERROR "Set PHOTOSHOP_SDK to the external Adobe Photoshop SDK directory")
    endif()
    target_include_directories(dn-host-tests PRIVATE "${DN_SDK}/photoshopapi/photoshop" "${DN_SDK}/photoshopapi/pica_sp")
    add_library(dn-editor STATIC src/editor_win.cpp src/image_win.cpp)
    target_include_directories(dn-editor PUBLIC src)
    target_compile_definitions(dn-editor PUBLIC UNICODE _UNICODE WIN32=1 _WINDOWS=1)
    if(MSVC)
        target_compile_options(dn-editor PUBLIC /utf-8)
    endif()
    target_link_libraries(dn-editor PUBLIC gdiplus comdlg32 comctl32 ole32 oleaut32 uuid windowscodecs mscms gdi32 user32 shell32 Threads::Threads)
    add_library(dn-curves MODULE src/photoshop_filter.cpp resources/native.rc)
    target_include_directories(dn-curves PRIVATE "${DN_SDK}/photoshopapi/photoshop" "${DN_SDK}/photoshopapi/pica_sp" resources)
    target_link_libraries(dn-curves PRIVATE dn-editor)
    set_target_properties(dn-curves PROPERTIES PREFIX "" SUFFIX ".8bf" OUTPUT_NAME "DN-Curves")
    add_executable(dn-curves-test WIN32 src/standalone.cpp resources/test.rc)
    target_include_directories(dn-curves-test PRIVATE resources)
    target_link_libraries(dn-curves-test PRIVATE dn-editor)
    set_target_properties(dn-curves-test PROPERTIES OUTPUT_NAME "DN-Curves-Test")
    add_executable(dn-install WIN32 src/installer_win.cpp resources/install.rc)
    target_include_directories(dn-install PRIVATE src resources)
    target_compile_definitions(dn-install PRIVATE UNICODE _UNICODE WIN32=1 _WINDOWS=1)
    target_link_libraries(dn-install PRIVATE shell32 ole32 uuid user32)
    set_target_properties(dn-install PROPERTIES OUTPUT_NAME "INSTALLIEREN")
    if(MSVC)
        target_compile_options(dn-install PRIVATE /utf-8)
    endif()
    if(MINGW)
        target_link_options(dn-curves PRIVATE -static -static-libgcc -static-libstdc++)
        target_link_options(dn-curves-test PRIVATE -municode -static -static-libgcc -static-libstdc++)
        target_link_options(dn-install PRIVATE -municode -static -static-libgcc -static-libstdc++)
        foreach(test dn-core-tests dn-feature-tests dn-host-tests dn-installation-tests)
            target_link_options(${test} PRIVATE -static -static-libgcc -static-libstdc++)
        endforeach()
    endif()
    install(TARGETS dn-curves dn-curves-test dn-core-tests dn-feature-tests dn-host-tests dn-installation-tests RUNTIME DESTINATION bin LIBRARY DESTINATION bin)
    install(TARGETS dn-install RUNTIME DESTINATION .)
    install(FILES README.md BITTE-ZUERST-LESEN.txt PHOTOSHOP-START.txt START-TEST.cmd SELBSTTEST.cmd THIRD-PARTY-NOTICES.txt DESTINATION .)
    install(DIRECTORY tests/fixtures DESTINATION tests)
    install(DIRECTORY licenses DESTINATION .)
endif()
