# SumOffice C SDK example.
#
#   cmake -S . -B build && cmake --build build
#   cmake -S . -B build -DSUMOFFICE_ROOT=/opt/sumoffice-c-2026.4.5-linux-x64
#
# Defines imported targets SumOffice::sheet, SumOffice::doc, SumOffice::slide that you can
# copy into your own project.
cmake_minimum_required(VERSION 3.16)
project(sumoffice_open_save C)

set(SUMOFFICE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../.." CACHE PATH "Folder of the SumOffice C SDK")
get_filename_component(SUMOFFICE_ROOT "${SUMOFFICE_ROOT}" ABSOLUTE)

foreach(core sheet doc slide)
  add_library(SumOffice::${core} SHARED IMPORTED)
  set_target_properties(SumOffice::${core} PROPERTIES
    INTERFACE_INCLUDE_DIRECTORIES "${SUMOFFICE_ROOT}/include")
  if(WIN32)
    set_target_properties(SumOffice::${core} PROPERTIES
      IMPORTED_LOCATION "${SUMOFFICE_ROOT}/lib/sum${core}.dll"
      IMPORTED_IMPLIB "${SUMOFFICE_ROOT}/lib/sum${core}.lib")
  else()
    set_target_properties(SumOffice::${core} PROPERTIES
      IMPORTED_LOCATION "${SUMOFFICE_ROOT}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}sum${core}${CMAKE_SHARED_LIBRARY_SUFFIX}")
  endif()
endforeach()

add_executable(open_save open_save.c)
target_link_libraries(open_save PRIVATE SumOffice::sheet SumOffice::doc SumOffice::slide)
if(NOT MSVC)
  target_compile_options(open_save PRIVATE -Wall -Wextra)
endif()
set_property(TARGET open_save PROPERTY C_STANDARD 11)

# Run from the build tree without installing: the SDK lib/ folder goes into the rpath.
set_target_properties(open_save PROPERTIES BUILD_RPATH "${SUMOFFICE_ROOT}/lib")

if(WIN32)
  # Windows has no rpath: put the DLLs next to the executable.
  add_custom_command(TARGET open_save POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
      "${SUMOFFICE_ROOT}/lib/sumsheet.dll" "${SUMOFFICE_ROOT}/lib/sumdoc.dll"
      "${SUMOFFICE_ROOT}/lib/sumslide.dll" $<TARGET_FILE_DIR:open_save>)
endif()
