cmake_minimum_required(VERSION 3.16.3)
project(pytcim)

set(CMAKE_CXX_STANDARD 17)
if(MSVC)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /std:c++17 -D_CRT_SECURE_NO_WARNINGS")
else()
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -Werror -Wunused-variable")
endif()

# ################################ SUPPORT GCC7 ################################
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
  if(GCC_VERSION VERSION_LESS 8)
    message(STATUS "GCC_VERSION=${GCC_VERSION} less than 7, linking with stdc++fs")
    set(GCC7LIB -lstdc++fs)
  endif()
endif()

find_package(Python COMPONENTS Interpreter Development)
find_package(pybind11 REQUIRED)

message(STATUS "pybind11_INCLUDE_DIR=${pybind11_INCLUDE_DIR}")

include_directories(../include)
link_directories(../lib)

pybind11_add_module(pytcim pytcim.cc)
target_link_libraries(pytcim PUBLIC tcim_runtime_lite ${GCC7LIB})

if(MINGW
   OR CYGWIN
   OR ENABLE_ASAN)
  target_link_options(pytcim PUBLIC -static-libstdc++ -static-libgcc)
endif()

# pybind11_add_module produces a MODULE with no install rpath by default, so installed Python extensions need an
# explicit relative path back to the SDK lib directory instead of relying on LD_LIBRARY_PATH or Python-side preloading.
set(TCIM_PYTHON_EXTENSION_RPATH
    "$ORIGIN;$ORIGIN/../../lib"
    CACHE STRING "Runtime library search path for installed Python extensions")

if(APPLE)
  set_target_properties(pytcim PROPERTIES INSTALL_RPATH "@loader_path/../../lib" BUILD_WITH_INSTALL_RPATH TRUE)
elseif(UNIX)
  set_target_properties(pytcim PROPERTIES INSTALL_RPATH "${TCIM_PYTHON_EXTENSION_RPATH}" BUILD_WITH_INSTALL_RPATH TRUE)
endif()

# ############################# pydev_ctrl ##################################
# Standalone Python extension for the tcim_dev_ctrl library. Links only against libtcim_dev_ctrl (which transitively
# pulls in libtcim_runtime_lite).
option(ENABLE_DEV_CTRL "Enable pydev_ctrl Python extension" ON)
option(ENABLE_XH1_HDI "XH1 HDI backend for dev_ctrl" ON)
option(ENABLE_XH2A_HAL "XH2A HAL backend for dev_ctrl" ON)
option(ENABLE_XH3_HAL "XH3 HAL backend for dev_ctrl" ON)
option(ENABLE_YH1_HAL "YH1 HAL backend for dev_ctrl" ON)

if(ENABLE_DEV_CTRL)

  pybind11_add_module(pydev_ctrl pydev_ctrl.cc)
  target_link_libraries(pydev_ctrl PUBLIC tcim_dev_ctrl ${GCC7LIB})

  if(MINGW
     OR CYGWIN
     OR ENABLE_ASAN)
    target_link_options(pydev_ctrl PUBLIC -static-libstdc++ -static-libgcc)
  endif()

  # See pytcim above for why this is needed. pydev_ctrl transitively pulls in libtcim_runtime_lite through
  # libtcim_dev_ctrl.
  if(APPLE)
    set_target_properties(pydev_ctrl PROPERTIES INSTALL_RPATH "@loader_path/../../lib" BUILD_WITH_INSTALL_RPATH TRUE)
  elseif(UNIX)
    set_target_properties(pydev_ctrl PROPERTIES INSTALL_RPATH "${TCIM_PYTHON_EXTENSION_RPATH}" BUILD_WITH_INSTALL_RPATH
                                                                                               TRUE)
  endif()

  install(
    TARGETS pydev_ctrl
    COMPONENT python
    RUNTIME DESTINATION .
    LIBRARY DESTINATION .
    ARCHIVE DESTINATION .)
endif()

if(WIN32)
  file(GLOB DLL_FILES ${CMAKE_CURRENT_SOURCE_DIR}/../bin/*.dll)
  install(
    FILES ${DLL_FILES}
    COMPONENT python
    DESTINATION .)
endif()

install(
  TARGETS pytcim
  COMPONENT python
  RUNTIME DESTINATION .
  LIBRARY DESTINATION .
  ARCHIVE DESTINATION .)
