# Copyright (C) 2025 ASTRON (Netherlands Institute for Radio Astronomy)
# SPDX-License-Identifier: GPL-3.0-or-later

#------------------------------------------------------------------------------
# directory for config.h
include_directories(${CMAKE_BINARY_DIR})

#------------------------------------------------------------------------------
# build libeverybeam-skalowelementbeam.so
add_library(skalowelementbeam SHARED skalowelementresponse.cc
                                     skalowfekocoefficients.cc)

string(TOLOWER ${CMAKE_PROJECT_NAME} projectname)
set_target_properties(
  skalowelementbeam PROPERTIES LIBRARY_OUTPUT_NAME
                               "${projectname}-skalowelementbeam")

# EveryBeam only uses OpenMP for disabling OpenMP multi-threading (in
# ska-sdp-func) and for single-threaded SIMD loops.
# It doesn't contain any multi-threaded OpenMP code, since it may interfere
# with other multithreading constructs, e.g., in DP3 and WSClean.
find_package(OpenMP REQUIRED)
# Add OpenMP compile flags from CMake detection or toolchain
target_compile_options(skalowelementbeam PRIVATE ${OpenMP_CXX_FLAGS})

if(SKBUILD)
  set_target_properties(skalowelementbeam PROPERTIES INSTALL_RPATH "$ORIGIN")
endif()
# Make sure that when other targets within this project link against the skalowelementbeam target,
# they can find the include files.
target_include_directories(
  skalowelementbeam PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)

#------------------------------------------------------------------------------
# Link against HDF5 and OpenMP
target_link_libraries(
  skalowelementbeam PRIVATE ${HDF5_LIBRARIES} ${HDF5_CXX_LIBRARIES}
                            OpenMP::OpenMP_CXX everybeam-core)

# Link against ska-sdp-func. Disable compiler warnings using 'SYSTEM'.
get_target_property(ska_sdp_func_include_dirs ska_sdp_func
                    INTERFACE_INCLUDE_DIRECTORIES)
target_include_directories(skalowelementbeam SYSTEM
                           PRIVATE ${ska_sdp_func_include_dirs})
target_link_libraries(skalowelementbeam PRIVATE ska_sdp_func xtensor)

#------------------------------------------------------------------------------
# install libeverybeam-skalowelementbeam.so
install(
  TARGETS skalowelementbeam
  COMPONENT libraries
  EXPORT EveryBeamTargets
  DESTINATION ${INSTALL_LIBDIR})

# install coefficients
message(
  "Install SKA Low FEKO test coefficients in: ${EVERYBEAM_INSTALL_DATADIR}")
install(
  DIRECTORY ${CMAKE_SOURCE_DIR}/coeffs/skalow/
  COMPONENT data-files
  DESTINATION ${EVERYBEAM_INSTALL_DATADIR}/skalow
  FILES_MATCHING
  PATTERN "SKA-Low_*.h5")
#------------------------------------------------------------------------------
