cmake_minimum_required( VERSION 3.20 )

set(DSOLOADER_MODULE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "")

include_directories(${DSOLOADER_MODULE_DIR})

set(DSOLOADER_SOURCE
  DSOLoader.cpp
  DSOExceptions.cpp
  Exception.cpp
  ExceptionMessages.cpp
)

set(DSOLOADER_HEADER_PUBLIC
  DSOLoader.hh
  DSOExceptions.hh
  Exception.hh
  # DSOVisibility.hh is #included by the three public headers above (and by
  # plugin/PluginCreator.hh) — it must ship with them.
  DSOVisibility.hh
)

set(DSOLOADER_HEADER_PRIVATE
  ExceptionMessages.hh
)

set(DSOLOADER_HEADER
  ${DSOLOADER_HEADER_PUBLIC}
  ${DSOLOADER_HEADER_PRIVATE})

add_library(dsold SHARED ${DSOLOADER_SOURCE})
target_link_libraries(dsold PUBLIC ${CMAKE_DL_LIBS})
target_include_directories(dsold
    PUBLIC  ${DSOLOADER_MODULE_DIR}
            ${DSOLOADER_MODULE_DIR}/..
)

# ── Sign module integration (optional — requires OpenSSL) ────────────────────
# When plugin-sign is available, dsold gains loadSigned() / loadVerified().
# DSOLD_WITH_SIGN guards the implementation in DSOLoader.cpp so the code
# compiles cleanly even without OpenSSL.
if(TARGET plugin-sign)
    target_link_libraries(dsold PUBLIC plugin-sign)
    target_compile_definitions(dsold PRIVATE DSOLD_WITH_SIGN)
    message(STATUS "dsold: plugin-sign found — loadSigned/loadVerified enabled")
else()
    message(STATUS "dsold: plugin-sign not found — loadSigned/loadVerified disabled")
endif()

set_target_properties(dsold PROPERTIES VERSION ${PROJECT_VERSION})
set_target_properties(dsold PROPERTIES SOVERSION 2)
set_target_properties(dsold PROPERTIES POSITION_INDEPENDENT_CODE ON)

install(FILES ${DSOLOADER_HEADER_PUBLIC}
        COMPONENT plugin_development COMPONENT plugin_source
        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dso)

# dsold must be in ffs-core so it gets deployed to production.
# plugin-development is for development headers only.
install(TARGETS dsold
    LIBRARY
        COMPONENT   ffs-core
        DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
