cmake_minimum_required(VERSION 3.0)

PROJECT(libuspagentdm.so)

ADD_DEFINITIONS(-Wall -Werror -Wformat -g)
ADD_DEFINITIONS(-D_GNU_SOURCE)

FILE(GLOB BBF_PLUGIN_SRC *.c)

# Compile and install libuspagentdm.so
ADD_LIBRARY(uspagentdm SHARED ${BBF_PLUGIN_SRC})
INSTALL(DIRECTORY DESTINATION usr/share/bbfdm/plugins)
INSTALL(TARGETS uspagentdm LIBRARY DESTINATION usr/share/bbfdm/plugins)

target_include_directories(uspagentdm
  PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
  PRIVATE
    ${CMAKE_SOURCE_DIR}/src/vendor)

target_compile_definitions(uspagentdm
  PRIVATE
    $<IF:$<BOOL:${ENABLE_STOMP}>,ENABLE_STOMP,DISABLE_STOMP>  # DISABLE_STOMP is the define used in core
    $<$<BOOL:${ENABLE_MQTT}>:ENABLE_MQTT>
    $<$<BOOL:${ENABLE_COAP}>:ENABLE_COAP>
    $<$<BOOL:${ENABLE_WEBSOCKETS}>:ENABLE_WEBSOCKETS>
    $<$<BOOL:${ENABLE_UDS}>:ENABLE_UDS>
    _GNU_SOURCE=1)

# Copy libuspagentdm.so to the main repo
add_custom_command(
    TARGET uspagentdm
    POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy
            libuspagentdm.so
            ${CMAKE_SOURCE_DIR}
    COMMENT "Copying libuspagentdm.so to main repo"
)

# Add a custom target to trigger the custom command
add_custom_target(copy_lib_target
    DEPENDS uspagentdm
)

# Make sure the copy_lib_target is built after the library is built
add_dependencies(copy_lib_target uspagentdm)

