cmake_minimum_required(VERSION 3.03)

project(tcpip VERSION 0.1)

# If the target already exists in a project (e.g. if tcpip is included multiple time as a submodule of submodules),
# this "header guard"-style check prevents defining the same target multiple times (which will lead to CMake errors).
if(TARGET tcpip)
  return()
endif()

add_compile_options(
  -Wall
  -Wformat=2
  -Wno-format-nonliteral
  -Wno-strict-aliasing
  -Wuninitialized
  -Wno-unused-function
)

option(
  BUILD_FRONTEND
  "If ON, build the default frontend executable"
  OFF
)

set(CMAKE_PREFIX_PATH "$ENV{MIDASSYS}")
find_package(Midas REQUIRED)

set(SYS_LIBS
    pthread
    util
    rt
    dl
)

set(MIDAS_LIBS
    midas::mfe
    midas::midas
    midas::mscb
)

set(LIBS ${SYS_LIBS} ${MIDAS_LIBS})

set(
  PROPERTIES
  CXX_STANDARD 11
)

################################################################################
## Build Static Library
################################################################################

add_library(
  tcpip
  bus/tcpip.cxx
)

set_property(
  TARGET
    tcpip
  PROPERTY
    ${PROPERTIES}
)

target_include_directories(
  tcpip
  PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
)

target_link_libraries(
  tcpip
  PRIVATE
  ${LIBS}
)

################################################################################
## Build Test Executable
################################################################################

if(${BUILD_FRONTEND})

  add_executable(
    test_tcpip
    test/test_tcpip.cxx
  )

  target_link_libraries(
    test_tcpip
    PRIVATE
      tcpip
      ${LIBS}
  )

endif()
