set(PACKAGE rda-rcp8-open)

message(STATUS ${PACKAGE})

# The build_date.c is generated during the build and belongs to the
# "build" dirctory, along with outher artifacts.
add_custom_command(
        OUTPUT build_date.c
        COMMAND echo \"const char *rcp8OpenDate_c = \\"`date`\\" \;\" >
        ${CMAKE_CURRENT_BINARY_DIR}/build_date.c
)

set(LIB_NAME rcp8_open)
set(APP_NAME rcp8)
set(CONF_FILES nexrad_rcp8.conf rcp8.conf rcp8_eec_ddc_token_config.conf scandinova_modulator.conf)
set(INCLUDE_FILES rcp8.h rcp8main.h)
set(SERVICE_FILE rcp8.service)
set(SOURCE_FILES
        andrew_acu_main.c
        appsys_main.c
        ara_acu3_main.c
        canbus.h
        canbus_main.c
        canopen_shared.c
        canopen_shared.h
        canopen_position.c
        canopen_velocity.c
        can_interface.c
        can_interface.h
        can_kvaser.c
        can_kvaser.h
        can_peak_eth.c
        can_peak_eth.h
        can_peak_eth_channel.c
        can_peak_eth_channel.h
        dau_main.c
        dcu_main.c
        dehydrator.c
        dehydrator.h
        dehydrator_snmp.c
        dmu801_shared.h
        dmu801_main.c
        dualsys.c
        eec_ddc_main.c
        eec_udp_main.c
        gigacom_main.c
        hpib_main.c
        ipa15hc_main.c
        kavouras_tcu_main.c
        klystron.h
        klystron_main.c
        melco_tky01.c
        modbus_tcp.c
        orbit_main.c
        power_monitor.c
        power_monitor.h
        ps_tr1163.c
        radtec_xcm_main.c
        rcp8.h
        rcp8_lib_nls_init.c
        rcp8main.h
        rcp8_modbus_tcp.h
        rcp_limits.c
        rpm_ped_main.c
        scandinova_modulator.c
        scandinova_modulator.h
        seapath_inu_main.c
        ShaftAutoCal.c
        shaft_sim.c
        siggen_eth.c
        siggen_lxi.c
        subs_monitor.c
        subs_open.c
        subs_setup.c
        tdrs_lib.h
        tdrs_ped_rcv_main.c
        tdrs_ped_xmt_main.c
        tsc_twt_main.c
        ups_snmp.c
        vaisala_trx.h
        vaisala_trx.c
        ${CMAKE_CURRENT_BINARY_DIR}/build_date.c)

# When ESDK is used, add  ab842_main.c to SOURCE_FILES

if (DEFINED BASE_SOURCE_DIR)
    include_directories(
            ${BASE_SOURCE_DIR}/user_lib
            ${BASE_SOURCE_DIR}/himath_lib
            ${BASE_SOURCE_DIR}/config_lib
            ${BASE_SOURCE_DIR}/antenna_lib
            ${BASE_SOURCE_DIR}/dsp_lib)
endif (DEFINED BASE_SOURCE_DIR)

enable_nls()

include_directories(
        /usr/include/modbus
        ${CMAKE_CURRENT_SOURCE_DIR}/../../include
        ${CMAKE_CURRENT_SOURCE_DIR}/../../rdasubs_lib
        ${CMAKE_CURRENT_SOURCE_DIR}/../../netcards_lib
        ${CMAKE_CURRENT_SOURCE_DIR}/../../pcicards
        ${CMAKE_CURRENT_SOURCE_DIR}/../../softplane)

add_library(${LIB_NAME} SHARED ${SOURCE_FILES})
set(LIB_MAJOR_VER 10) # Set Library major version, this is also used for SO file versioning
set(LIB_MINOR_VER 0) # Set Library minor version
set(LIB_PATCH_VER 0) # Set Library patch version
set(LIB_VERSION "${LIB_MAJOR_VER}.${LIB_MINOR_VER}.${LIB_PATCH_VER}")
set_target_properties(${LIB_NAME} PROPERTIES VERSION ${LIB_VERSION} SOVERSION ${LIB_MAJOR_VER})

option(VAISALA_TRX_BUILD_FOR_DEV_SETUP "build rcp8/open/vaisala_trx specially for TRX dev setup, which doesn't have all components" FALSE)
if (VAISALA_TRX_BUILD_FOR_DEV_SETUP)
    target_compile_definitions(${LIB_NAME} PRIVATE VAISALA_TRX_BUILD_FOR_DEV_SETUP)
endif ()

option(CANOPEN_DEBUG "print debug messages in CANopen threads" FALSE)
if (CANOPEN_DEBUG)
    target_compile_definitions(${LIB_NAME} PRIVATE CANOPEN_DEBUG)
endif()

# Local variables in code with long jumps have to be declared volatile
# Until all of those variables are identified and changed
# optimization has to be disabled
set_source_files_properties(subs_setup.c PROPERTIES COMPILE_OPTIONS "-O0")

link_libraries(
        rcp8_core rcp8_open rcp8_site
        softplane pcicards netcards rdasubs
        antenna-iris exthdr-iris config-iris dspi-iris himath-iris user-iris
        lxi
        IPP::ipps canlib nrp rsnrpz tirpc dl modbus pthread netsnmp rt m)

add_executable(rcp8 rcp8.c)
add_executable(test_inu test_inu.c)

install(
  TARGETS ${APP_NAME}
  DESTINATION ${IRIS_LIBEXEC_SERVICES}
  COMPONENT rcp8)

install(
  TARGETS ${LIB_NAME}
  DESTINATION ${IRIS_LIB}
  LIBRARY COMPONENT rcp8 NAMELINK_COMPONENT rcp8-devel)

install(
  FILES ${CONF_FILES}
  DESTINATION ${IRIS_CONFIG_TMPL}
  COMPONENT rcp8)
install(
  FILES ${SERVICE_FILE}
  DESTINATION ${IRIS_SYSCONFDIR}
  COMPONENT rcp8)

install(
  FILES ${INCLUDE_FILES}
  DESTINATION ${IRIS_INCLUDE}
  COMPONENT rcp8-devel)
