cmake_minimum_required(VERSION 3.14)

set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_CXX_STANDARD 20)
set(C_STANDARD 20)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output)

project(scpi-server)

if(NOT DEFINED INSTALL_DIR)
    message(FATAL_ERROR,"Installation path not set.")
endif()

if(NOT DEFINED VERSION)
    set(VERSION dev)
endif()

if(NOT DEFINED REVISION)
    set(REVISION devbuild)
endif()

if(NOT DEFINED BUILD_NUMBER)
  set(BUILD_NUMBER devbuild)
endif()

message(STATUS "Install path ${INSTALL_DIR}")
message(STATUS "VERSION=${VERSION}")
message(STATUS "REVISION=${REVISION}")
message(STATUS "BUILD_NUMBER=${BUILD_NUMBER}")
message(STATUS "LINUX_VER=${LINUX_VER}")

message(STATUS "Compiler C path: ${CMAKE_C_COMPILER}")
message(STATUS "Compiler C ID: ${CMAKE_C_COMPILER_ID}")
message(STATUS "Compiler C version: ${CMAKE_C_COMPILER_VERSION}")
message(STATUS "Compiler C is part: ${CMAKE_COMPILER_IS_GNUC}")

message(STATUS "Compiler C++ path: ${CMAKE_CXX_COMPILER}")
message(STATUS "Compiler C++ ID: ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "Compiler C++version: ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS "Compiler C++ is part: ${CMAKE_COMPILER_IS_GNUCXX}")

# list(APPEND r_paths
#     /opt/redpitaya/lib
#     /opt/redpitaya/lib/web
# )

# set(CMAKE_INSTALL_RPATH ${r_paths})

include_directories(${INSTALL_DIR}/include)
include_directories(scpi-parser/libscpi/inc)

file(GLOB SOURCES "src/*.cpp")
file(GLOB RP_HEADERS "src/*.h")
file(GLOB SOURCES_PARSER "scpi-parser/libscpi/src/*.c")

add_compile_options(-mcpu=cortex-a9 -mfpu=neon-fp16 -fPIC)
add_compile_definitions(ARCH_ARM)
add_compile_options($<$<CONFIG:Debug>:-DTRACE_ENABLE>)
add_compile_options(-Wall -Wno-pointer-arith -Wextra -DVERSION=${VERSION} -DREVISION=${REVISION} $<$<CONFIG:Debug>:-g3> $<$<CONFIG:Release>:-Os> -ffunction-sections -fdata-sections)

add_executable(${PROJECT_NAME} ${SOURCES} ${SOURCES_PARSER})

target_link_directories(${PROJECT_NAME} PRIVATE ${INSTALL_DIR}/lib)

target_link_libraries(${PROJECT_NAME} PRIVATE
    -Wl,-Bstatic
    rpapp_lcr
    rp-sweep
    rp-dsp
    rp
    rp-hw-calib
    rp-hw-can
    rp-hw-profiles
    rp-i2c
    rp-spi
    rp-gpio
    rp-hw
    -Wl,-Bdynamic
    i2c
    socketcan
    pthread
    dl
    stdc++
)

install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${INSTALL_DIR}/bin)

unset(INSTALL_DIR CACHE)
