cmake_minimum_required(VERSION 3.21)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
include(DynamicVersion)

extract_version_string(
    HEADER_FILE ${CMAKE_CURRENT_LIST_DIR}/include/stb.h
    VERSION_PREFIX LIBSTB_
    OUTPUT_VAR PROJECT_VERSION
)

project(libstb LANGUAGES CXX VERSION ${PROJECT_VERSION})
message(STATUS "Project: ${PROJECT_NAME}@v${PROJECT_VERSION}")
include(Startup)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

include(Optimize)

option(STB_BUILD_TESTS "Build tests" ${PROJECT_IS_TOP_LEVEL})
option(STB_BUILD_PYTHON "Build Python bindings" ${PROJECT_IS_TOP_LEVEL})

# --- libstb C++ core ---
# There is no stb header in the tree any more: the decoders, encoders, font reader and resizer are
# libstb's own code derived from stb (see NOTICE.md), and no public include/ header includes any of it.
# One folder per module (util, image, codec, zlib, font, draw); the bindings live elsewhere.
#
# The core is always a shared library (libstb.so, libstb.dylib, stb.dll): the Python extension and
# every C++ program link to the same copy, which is what lets a wheel carry one engine instead
# of two. Only what the public headers mark STB_API is exported (include/stb/export.hpp); the rest
# is hidden.
file(GLOB_RECURSE LIBSTB_CORE_SOURCES CONFIGURE_DEPENDS
    ${PROJECT_SOURCE_DIR}/src/util/*.cpp
    ${PROJECT_SOURCE_DIR}/src/image/*.cpp
    ${PROJECT_SOURCE_DIR}/src/codec/*.cpp
    ${PROJECT_SOURCE_DIR}/src/zlib/*.cpp
    ${PROJECT_SOURCE_DIR}/src/font/*.cpp
    ${PROJECT_SOURCE_DIR}/src/draw/*.cpp)

add_library(stb_core SHARED ${LIBSTB_CORE_SOURCES})
target_compile_definitions(stb_core PRIVATE STB_BUILDING_LIBRARY)
add_library(libstb::libstb ALIAS stb_core)

# image::resize shares big work between std::thread threads (src/util/parallel.hpp); on Linux the
# library needs the threads library for that.
find_package(Threads REQUIRED)
target_link_libraries(stb_core PRIVATE Threads::Threads)

set_target_properties(stb_core PROPERTIES
    CXX_VISIBILITY_PRESET hidden
    VISIBILITY_INLINES_HIDDEN ON
    EXPORT_NAME libstb                  # consumers link libstb::libstb
    OUTPUT_NAME stb                     # libstb.so / libstb.dylib / stb.dll
)
if(NOT DEFINED SKBUILD)
    # libstb.so -> libstb.so.0.8 -> libstb.so.0.8.0 in a plain install. A wheel carries a single
    # file (zip has no symlinks), so it gets no version suffix.
    set_target_properties(stb_core PROPERTIES
        VERSION ${PROJECT_VERSION}
        SOVERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    )
endif()
# MSVC: the exported classes hold std::string / std::vector members (C4251) and derive from std
# exceptions (C4275). Both are fine as long as the library and its users use the same compiler and
# runtime, which a DLL of C++ classes already requires.
target_compile_options(stb_core PUBLIC $<$<CXX_COMPILER_ID:MSVC>:/wd4251 /wd4275>)

# Install layout: the wheel carries the C++ SDK as a regular install prefix, libstb/sdk/ (include/,
# lib/ and lib/cmake/libstb/ inside it: the same tree `cmake --install` makes, so it also works as a
# CMAKE_PREFIX_PATH; see get_sdk_dir() in the Python package). A plain `cmake --install` uses the
# usual prefix layout.
if(DEFINED SKBUILD)
    set(_libstb_sdk_dest   libstb/sdk)
    set(_libstb_lib_dest   ${_libstb_sdk_dest}/lib)
    set(_libstb_bin_dest   ${_libstb_sdk_dest}/lib)  # stb.dll beside stb.lib; libstb/__init__.py adds it with os.add_dll_directory
    set(_libstb_inc_dest   ${_libstb_sdk_dest}/include)
    set(_libstb_cmake_dest ${_libstb_sdk_dest}/lib/cmake/libstb)
else()
    set(_libstb_lib_dest   lib)
    set(_libstb_bin_dest   bin)
    set(_libstb_inc_dest   include)
    set(_libstb_cmake_dest lib/cmake/libstb)
endif()

target_include_directories(stb_core
    PUBLIC
        $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:${_libstb_inc_dest}>
)
# Everything that decides how the core sources are compiled lives in this function, so a target
# that compiles them itself (tests/cpp does, to attach coverage flags) gets exactly the same
# settings as stb_core.
function(libstb_apply_core_build_settings target)
    # Internal headers are included relative to src/ ("image/simd.hpp", "font/shaper/gsub.hpp").
    target_include_directories(${target} PRIVATE ${PROJECT_SOURCE_DIR}/src)

    # A target that compiles the core sources itself (the C++ tests) has no library to import
    # from: STB_API must be empty there. stb_core builds with STB_BUILDING_LIBRARY instead.
    if(NOT target STREQUAL "stb_core")
        target_compile_definitions(${target} PRIVATE STB_STATIC_DEFINE)
    endif()

    target_compile_options(${target} PRIVATE
        $<$<CXX_COMPILER_ID:MSVC>:/W4 /EHsc>
        $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Wextra -Wpedantic>
    )
    # -fno-trapping-math: lets GCC/Clang vectorize float->int conversions in the JPEG encoder's
    # quantization (about 2x faster JPEG). It only means FP operations are not required to
    # raise hardware traps (nothing here enables or reads them); results are unchanged.
    target_compile_options(${target} PRIVATE
        $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fno-trapping-math>
    )
    apply_optimization_flags(${target})

    # The operating system log handlers (src/util/logging_os.cpp) call into system libraries: the
    # Event Log of Windows lives in advapi32 and Android's logcat in liblog. Elsewhere the C library
    # has everything (syslog, os_log).
    if(WIN32)
        target_link_libraries(${target} PRIVATE advapi32)
    elseif(ANDROID)
        target_link_libraries(${target} PRIVATE log)
    endif()

    # Everything under src/draw (shapes, paths, the rasterizer) does floating point and must give
    # the same pixels on every platform (the C++ and Python canvas tests compare against fixed
    # hashes), so no fused multiply-add contraction: GCC and Clang would otherwise use FMA on ARM
    # and give last-bit differences.
    if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        file(GLOB_RECURSE LIBSTB_DRAW_SOURCES CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/src/draw/*.cpp)
        set_source_files_properties(${LIBSTB_DRAW_SOURCES}
            TARGET_DIRECTORY ${target}
            PROPERTIES COMPILE_OPTIONS "-ffp-contract=off")
    endif()

    # The numeric kernels and what is built on them (filters, tone, colour spaces, metrics, thresholds, morphology, components, superpixels) promise the same bits on every
    # SIMD backend and every number of threads, so no fused multiply-add contraction there either, and
    # no errno from sqrt (it would keep the compiler from vectorising it).
    if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        set_source_files_properties(
            ${PROJECT_SOURCE_DIR}/src/image/math_ops.cpp
            ${PROJECT_SOURCE_DIR}/src/image/filter.cpp
            ${PROJECT_SOURCE_DIR}/src/image/tone.cpp
            ${PROJECT_SOURCE_DIR}/src/image/colorspace.cpp
            ${PROJECT_SOURCE_DIR}/src/image/metrics.cpp
            ${PROJECT_SOURCE_DIR}/src/image/threshold.cpp
            ${PROJECT_SOURCE_DIR}/src/image/morphology.cpp
            ${PROJECT_SOURCE_DIR}/src/image/components.cpp
            ${PROJECT_SOURCE_DIR}/src/image/canny.cpp
            ${PROJECT_SOURCE_DIR}/src/image/warp.cpp
            ${PROJECT_SOURCE_DIR}/src/image/superpixel.cpp
            TARGET_DIRECTORY ${target}
            PROPERTIES COMPILE_OPTIONS "-ffp-contract=off;-fno-math-errno")
    endif()

    # kernels_scalar.cpp is the "scalar" SIMD backend: the same pixel loops with the
    # compiler's auto-vectoriser off. (MSVC has no such switch: there it may still vectorise,
    # with identical results.) TARGET_DIRECTORY: source file properties are per directory scope.
    if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
        set_source_files_properties(${PROJECT_SOURCE_DIR}/src/image/kernels_scalar.cpp
            TARGET_DIRECTORY ${target}
            PROPERTIES COMPILE_OPTIONS "-fno-tree-vectorize;-fno-tree-slp-vectorize")
        set_source_files_properties(${PROJECT_SOURCE_DIR}/src/image/math_scalar.cpp
            TARGET_DIRECTORY ${target}
            PROPERTIES COMPILE_OPTIONS "-fno-tree-vectorize;-fno-tree-slp-vectorize;-ffp-contract=off;-fno-math-errno")
    elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        set_source_files_properties(${PROJECT_SOURCE_DIR}/src/image/kernels_scalar.cpp
            TARGET_DIRECTORY ${target}
            PROPERTIES COMPILE_OPTIONS "-fno-vectorize;-fno-slp-vectorize")
        set_source_files_properties(${PROJECT_SOURCE_DIR}/src/image/math_scalar.cpp
            TARGET_DIRECTORY ${target}
            PROPERTIES COMPILE_OPTIONS "-fno-vectorize;-fno-slp-vectorize;-ffp-contract=off;-fno-math-errno")
    endif()
endfunction()

libstb_apply_core_build_settings(stb_core)

# --- install (find_package(libstb) -> libstb::libstb) ---
include(CMakePackageConfigHelpers)
install(TARGETS stb_core EXPORT libstbTargets
    LIBRARY DESTINATION ${_libstb_lib_dest}     # libstb.so, libstb.dylib
    RUNTIME DESTINATION ${_libstb_bin_dest}     # stb.dll
    ARCHIVE DESTINATION ${_libstb_lib_dest}     # stb.lib, the import library on Windows
)
install(DIRECTORY include/ DESTINATION ${_libstb_inc_dest})
install(EXPORT libstbTargets FILE libstbTargets.cmake NAMESPACE libstb:: DESTINATION ${_libstb_cmake_dest})
configure_package_config_file(
    ${PROJECT_SOURCE_DIR}/cmake/libstbConfig.cmake.in
    ${PROJECT_BINARY_DIR}/libstbConfig.cmake
    INSTALL_DESTINATION ${_libstb_cmake_dest}
)
write_basic_package_version_file(
    ${PROJECT_BINARY_DIR}/libstbConfigVersion.cmake
    VERSION ${PROJECT_VERSION}
    COMPATIBILITY SameMajorVersion
)
install(FILES
    ${PROJECT_BINARY_DIR}/libstbConfig.cmake
    ${PROJECT_BINARY_DIR}/libstbConfigVersion.cmake
    DESTINATION ${_libstb_cmake_dest}
)

if(STB_BUILD_PYTHON OR DEFINED SKBUILD)
    set(PYTHON_INSTALL_DIR ${CMAKE_BINARY_DIR}/python/install)
    add_subdirectory(${PROJECT_SOURCE_DIR}/src/bindings/python)
    if(DEFINED SKBUILD)
        return()
    endif()
endif()

if(STB_BUILD_TESTS)
    enable_testing()
    set(CMAKE_CTEST_ARGUMENTS "--output-on-failure" "-V")
    add_subdirectory(${PROJECT_SOURCE_DIR}/tests)
endif()
