#-------------------------------------------------------------------------------
#
# CMakeLists.txt
#
# CMake(1) targets for the OpenPowerLink CN library on FreeRTOS
#
# (c)2023 AIT Austrian Institute of Technology
#-------------------------------------------------------------------------------

# configure subproject
project(oplkcn C)
message(STATUS "Configuring ${PROJECT_NAME}")

# Set type of library
if(CFG_COMPILE_SHARED_LIBRARY)
  set(LIB_TYPE "SHARED")
else()
  set(LIB_TYPE "STATIC")
endif()

# set sources of POWERLINK library
set(LIB_KERNEL_SOURCES
  ${KERNEL_SOURCES}
  ${CTRL_KCAL_DIRECT_SOURCES}
  ${DLL_KCAL_CIRCBUF_SOURCES}
  ${EVENT_KCAL_NOOSKERNEL_SOURCES}
  ${ERRHND_KCAL_LOCAL_SOURCES}
  ${PDO_KCAL_LOCAL_SOURCES}
  ${HARDWARE_DRIVER_OPENMAC_SOURCES}
  ${HARDWARE_DRIVER_OPENMAC_CN_SOURCES}
)

set(LIB_USER_SOURCES
  ${USER_SOURCES}
  ${CTRL_UCAL_DIRECT_SOURCES}
  ${DLL_UCAL_CIRCBUF_SOURCES}
  ${ERRHND_UCAL_LOCAL_SOURCES}
  ${PDO_UCAL_LOCAL_SOURCES}
  ${EVENT_UCAL_NOOSKERNEL_SOURCES}
  ${USER_TIMER_GENERIC_SOURCES}
)

set(LIB_COMMON_SOURCES
  ${COMMON_SOURCES}
  ${COMMON_CAL_DIRECT_SOURCES}
  ${CIRCBUF_NOOS_SOURCES}
  ${MEMMAP_NOOSLOCAL_SOURCES}
  ${COMMON_NOOS_SOURCES}
  ${ARCH_LE_SOURCES}
)

# Create full source list
set(LIB_SOURCES
  ${LIB_KERNEL_SOURCES}
  ${LIB_USER_SOURCES}
  ${LIB_COMMON_SOURCES}
)

# set includes of POWERLINK library
include_directories(
  ${PROJECT_SOURCE_DIR}
)

# show invocations of cc, and c++
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# set C standard and warning level
add_compile_options("-Wall" "-Wextra" "-pedantic")
add_compile_options("-std=c99")

# configure stack variant and size (i.e., number of different Node IDs)
add_compile_definitions(CONFIG_CN)
add_compile_definitions(NMT_MAX_NODE_ID=254)

# Handle target specific functions
if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "Microblaze")
  include(configure-microblaze.cmake)
elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "Nios")
  message(FATAL_ERROR "TODO: Nios implementation")
else()
  message(FATAL_ERROR "Unknown platform selected!")
endif()

# Define library and installation rules
add_library(${PROJECT_NAME} ${LIB_TYPE} ${LIB_SOURCES} ${LIB_ARCH_SOURCES})
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:Debug>:DEBUG;DEF_DEBUG_LVL=${CFG_DEBUG_LVL}>)
install(TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX})
install(FILES ${PROJECT_SOURCE_DIR}/oplkcfg.h DESTINATION ${CMAKE_INSTALL_PREFIX}/config-${PROJECT_NAME})
