cmake_minimum_required(VERSION 3.0)
project(Beamline_elements)


# Check if the required environment variables MIDASSYS and EPICSSYS are available
if (NOT DEFINED ENV{MIDASSYS})
   message(SEND_ERROR "MIDASSYS environment variable not defined.")
endif()
if (NOT DEFINED ENV{EPICSSYS})
   message(SEND_ERROR "EPICSSYS environment variable not defined.")
endif()

set(CMAKE_CXX_STANDARD 11)
set(MIDASSYS $ENV{MIDASSYS})
set(EPICSSYS $ENV{EPICSSYS})

# Select the correct EPICS library depending on the OS (currently Linus or Darwin are available)
if (${CMAKE_SYSTEM_NAME} MATCHES Linux)
   link_directories(${EPICSSYS}/lib/linux-x86_64)
   set(LIBS ${LIBS} -lpthread -lutil -lrt -lbsd       -ldl)
endif()
if (${CMAKE_SYSTEM_NAME} MATCHES Darwin)
   link_directories(${EPICSSYS}/lib/darwin-aarch64)
   set(LIBS ${LIBS} -lutil -lca)
endif()

add_executable(Beamline_elements_scfe 
  frontend/Beamline_elements_scfe.cxx
)

target_compile_options(Beamline_elements_scfe PRIVATE -g)

target_include_directories(Beamline_elements_scfe 
    PRIVATE
        ${MIDASSYS}/include
        ${EPICSSYS}/include
        ${EPICSSYS}/include/os/Linux
        ${EPICSSYS}/include/os/Darwin
        ${EPICSSYS}/include/compiler/gcc
        ${EPICSSYS}/include/compiler/clang
        ${CMAKE_CURRENT_SOURCE_DIR}/mxml
)

target_link_libraries(Beamline_elements_scfe 
    PRIVATE 
        ${EPICSSYS}/lib/$ENV{EPICS_HOST_ARCH}/libca.so
        $ENV{MIDASSYS}/lib/libmidas.a
        $ENV{MIDASSYS}/lib/libmfe.a
        $ENV{MIDASSYS}/lib/libmidas-mfed.a
        ${LIBS} 
)

install(
  TARGETS
    Beamline_elements_scfe
  RUNTIME DESTINATION bin
)
