# gguf.cpp / diffusion — the image/video generation runtime and its CLI.
#
# Same sources as the standalone diffusion tree, with kernels/ removed: ggml is
# the shared build from ../kernels, configured and added by the top level. The
# global toolchain setup (standards, output dirs, MSVC flags, backend options,
# GGML_MAX_NAME=128) also lives there.
#
# project() is kept because cli/CMakeLists.txt addresses the runtime sources
# through ${PROJECT_SOURCE_DIR}/src.

project("diffusion" C CXX)

option(SD_BUILD_CLI         "diffusion: build the diffusion executable" ON)
option(SD_BUILD_SHARED_LIBS "diffusion: build shared library"           ${BUILD_SHARED_LIBS})

if(APPLE)
    function(sd_set_macos_rpaths target)
        get_target_property(target_type ${target} TYPE)
        if(target_type STREQUAL "EXECUTABLE")
            set(runtime_paths "@executable_path" "@executable_path/../lib")
        elseif(target_type STREQUAL "SHARED_LIBRARY" OR target_type STREQUAL "MODULE_LIBRARY")
            set(runtime_paths "@loader_path" "@loader_path/../lib")
            set_target_properties(${target} PROPERTIES
                MACOSX_RPATH ON
                INSTALL_NAME_DIR "@rpath"
                BUILD_WITH_INSTALL_NAME_DIR ON
            )
        else()
            return()
        endif()
        set_target_properties(${target} PROPERTIES
            BUILD_RPATH "${runtime_paths}"
            INSTALL_RPATH "${runtime_paths}"
            BUILD_WITH_INSTALL_RPATH ON
        )
    endfunction()
endif()

set(SD_LIB diffusion)

file(GLOB SD_LIB_SOURCES CONFIGURE_DEPENDS
    "src/*.h"
    "src/*.cpp"
    "src/*.hpp"
    "src/conditioning/*.h"
    "src/conditioning/*.cpp"
    "src/conditioning/*.hpp"
    "src/core/*.h"
    "src/core/*.cpp"
    "src/core/*.hpp"
    "src/extensions/*.h"
    "src/extensions/*.cpp"
    "src/extensions/*.hpp"
    "src/model/*/*.h"
    "src/model/*/*.cpp"
    "src/model/*/*.hpp"
    "src/runtime/*.h"
    "src/runtime/*.cpp"
    "src/runtime/*.hpp"
    "src/model_io/*.h"
    "src/model_io/*.cpp"
    "src/tokenizers/*.h"
    "src/tokenizers/*.cpp"
    "src/tokenizers/vocab/*.h"
    "src/tokenizers/vocab/*.cpp"
)

find_program(GIT_EXE NAMES git git.exe NO_CMAKE_FIND_ROOT_PATH)
if(GIT_EXE)
    execute_process(COMMAND ${GIT_EXE} describe --tags --abbrev=7 --dirty=+
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        OUTPUT_VARIABLE DIFFUSION_BUILD_VERSION
        OUTPUT_STRIP_TRAILING_WHITESPACE
        ERROR_QUIET
    )
    execute_process(COMMAND ${GIT_EXE} rev-parse --short HEAD
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        OUTPUT_VARIABLE DIFFUSION_BUILD_COMMIT
        OUTPUT_STRIP_TRAILING_WHITESPACE
        ERROR_QUIET
    )
endif()
if(NOT DIFFUSION_BUILD_VERSION)
    set(DIFFUSION_BUILD_VERSION 0.0.8)
endif()
if(NOT DIFFUSION_BUILD_COMMIT)
    set(DIFFUSION_BUILD_COMMIT 2027.7.26)
endif()
message(STATUS "gguf.cpp: diffusion version ${DIFFUSION_BUILD_VERSION} (${DIFFUSION_BUILD_COMMIT})")

set_property(
  SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/src/version.cpp
  APPEND PROPERTY COMPILE_DEFINITIONS
  SDCPP_BUILD_COMMIT=${DIFFUSION_BUILD_COMMIT} SDCPP_BUILD_VERSION=${DIFFUSION_BUILD_VERSION}
)

if(SD_BUILD_SHARED_LIBS)
    message(STATUS "gguf.cpp: diffusion as a shared library")
    add_library(${SD_LIB} SHARED ${SD_LIB_SOURCES})
    target_compile_definitions(${SD_LIB} PUBLIC  SD_BUILD_SHARED_LIB)
    target_compile_definitions(${SD_LIB} PRIVATE SD_BUILD_DLL)
else()
    message(STATUS "gguf.cpp: diffusion as a static library")
    add_library(${SD_LIB} STATIC ${SD_LIB_SOURCES})
endif()

if(APPLE)
    sd_set_macos_rpaths(${SD_LIB})
endif()

target_link_libraries(${SD_LIB} PUBLIC ggml)
target_include_directories(${SD_LIB} PUBLIC . src include)
target_include_directories(${SD_LIB} PRIVATE src/core)
target_include_directories(${SD_LIB} PUBLIC thirdparty)
# ggml_graph_cut.cpp / ggml_extend_backend.cpp reach into ggml's internal
# header as "kernels/src/ggml-impl.h" — with the kernels shared one level up,
# that path is resolved from the engine root.
target_include_directories(${SD_LIB} PRIVATE ..)
target_compile_features(${SD_LIB} PUBLIC c_std_11 cxx_std_17)

if(SD_BUILD_CLI)
    add_subdirectory(cli)
endif()
