cmake_minimum_required(VERSION 3.17)
project(FubonCpp)
set(CMAKE_CXX_STANDARD 20)

# Include directories
include_directories(${CMAKE_SOURCE_DIR}/bindings)

# Link directories
link_directories(${CMAKE_SOURCE_DIR}/bindings)

# 原生庫的搜尋路徑。macOS 上 libfubon.dylib 的 install_name 是 @rpath/libfubon.dylib
# （見 uniffi_wraper/.cargo/config.toml），需要由執行檔提供實際目錄才解析得到；
# Linux 則靠這個省掉自行 export LD_LIBRARY_PATH。
set(CMAKE_BUILD_RPATH ${CMAKE_SOURCE_DIR}/bindings)
set(CMAKE_INSTALL_RPATH ${CMAKE_SOURCE_DIR}/bindings)

# Build example executable
add_executable(fubon_example 
    ${CMAKE_SOURCE_DIR}/Example/main.cpp
    ${CMAKE_SOURCE_DIR}/bindings/fubon.cpp
    ${CMAKE_SOURCE_DIR}/bindings/pretty_print.cpp
)

# Platform-specific settings
if(WIN32)
  set(DLL_PATH "${CMAKE_CURRENT_SOURCE_DIR}/bindings/fubon.dll")
  add_custom_command(TARGET fubon_example POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different "${DLL_PATH}" $<TARGET_FILE_DIR:fubon_example>)
  target_link_libraries(fubon_example fubon ws2_32)
elseif(APPLE)
  target_link_libraries(fubon_example fubon)
else()
  target_link_libraries(fubon_example fubon pthread)
endif()

# Smoke test executable (only built if tests/ directory exists, for CI)
if(EXISTS "${CMAKE_SOURCE_DIR}/tests/smoke_test.cpp")
  add_executable(smoke_test
      ${CMAKE_SOURCE_DIR}/tests/smoke_test.cpp
      ${CMAKE_SOURCE_DIR}/bindings/fubon.cpp
  )

  # Platform-specific settings for smoke_test
  if(WIN32)
    add_custom_command(TARGET smoke_test POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy_if_different "${DLL_PATH}" $<TARGET_FILE_DIR:smoke_test>)
    target_link_libraries(smoke_test fubon ws2_32)
  elseif(APPLE)
    target_link_libraries(smoke_test fubon)
  else()
    target_link_libraries(smoke_test fubon pthread)
  endif()
endif()

