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

project(everybeam_cpp)

#------------------------------------------------------------------------------
add_library(everybeam-core SHARED elementresponse.cc options.cc)
target_include_directories(everybeam-core PRIVATE "${CMAKE_BINARY_DIR}")

set(EVERYBEAM_FILENAMES
    antenna.cc
    beamformer/dipolebeamformer.cc
    beamformer.cc
    beamformer_fastmath.cc
    beamformeridenticalantennas.cc
    beamformerlofar.cc
    beamformerlofarhba.cc
    beamformerlofarlba.cc
    circularsymmetric/atcacoefficients.cc
    circularsymmetric/gmrtcoefficients.cc
    circularsymmetric/meerkatcoefficients.cc
    circularsymmetric/vlacoefficients.cc
    circularsymmetric/voltagepattern.cc
    common/directionlist.cc
    common/sphericalharmonics.cc
    common/fftresampler.cc
    coords/itrfconverter.cc
    coords/itrfdirection.cc
    elementmodel/dishresponse.cc
    elementmodel/hamakerelementmodel.cc
    elementmodel/meerkatelementmodel.cc
    element.cc
    elementhamaker.cc
    elementresponse.cc
    elementresponsefactory.cc
    everybeam.cc
    griddedresponse/aartfaacgrid.cc
    griddedresponse/airygrid.cc
    griddedresponse/dishgrid.cc
    griddedresponse/griddedresponse.cc
    griddedresponse/mwagrid.cc
    griddedresponse/phasedarraygrid.cc
    griddedresponse/skamidgrid.cc
    load/load_new.cc
    load/loadlofar.cc
    load/loadmeerkat.cc
    load.cc
    lobes/lobeselementresponse.cc
    lwa/lwaelementresponse.cc
    msreadutils.cc
    mwabeam/tilebeam2016.cc
    mwabeam/beam2016implementation.cc
    options.cc
    phasedarrayresponse.cc
    pointresponse/airypoint.cc
    pointresponse/dishpoint.cc
    pointresponse/mwapoint.cc
    pointresponse/phasedarraypoint.cc
    pointresponse/skamidpoint.cc
    response/allstationresponse.cc
    response/singlestationresponse.cc
    sphericalharmonicsresponse.cc
    sphericalharmonicsresponsefixeddirection.cc
    station.cc
    telescope/alma.cc
    telescope/dish.cc
    telescope/dsa110.cc
    telescope/lofar.cc
    telescope/lwa.cc
    telescope/mwa.cc
    telescope/phasedarray.cc
    telescope/oskar.cc
    telescope/skamid.cc
    telescope.cc)

set_source_files_properties(beamformer_fastmath.cc
                            PROPERTIES COMPILE_FLAGS "-fopenmp -ffast-math")

set(EVERYBEAM_LIBRARIES everybeam)
if(BUILD_TESTING)
  list(APPEND EVERYBEAM_LIBRARIES everybeam-test)
endif()

foreach(EVERYBEAM_LIBRARY ${EVERYBEAM_LIBRARIES})
  add_library(${EVERYBEAM_LIBRARY} SHARED ${EVERYBEAM_FILENAMES})

  # Make sure that when other targets within this project link against the everybeam target,
  # they can find the include files.
  target_include_directories(
    ${EVERYBEAM_LIBRARY} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
  # Required to get config.h header
  target_include_directories(${EVERYBEAM_LIBRARY} PRIVATE "${CMAKE_BINARY_DIR}")

  target_include_directories(${EVERYBEAM_LIBRARY}
                             PUBLIC ${CASACORE_INCLUDE_DIR})

  target_link_libraries(${EVERYBEAM_LIBRARY} PRIVATE aterms)
  target_link_libraries(${EVERYBEAM_LIBRARY}
                        PUBLIC hamaker oskar skalowelementbeam skamidbeam)
  target_link_libraries(
    ${EVERYBEAM_LIBRARY}
    PRIVATE ${CASACORE_LIBRARIES}
            ${HDF5_CXX_LIBRARIES}
            ${FFTW3F_LIBRARY}
            ${CFITSIO_LIBRARY}
            schaapcommon
            Threads::Threads
            xtensor)

  if(SKBUILD)
    set_target_properties(${EVERYBEAM_LIBRARY} PROPERTIES INSTALL_RPATH
                                                          "$ORIGIN")
  endif()
endforeach()
if(BUILD_TESTING)
  set_target_properties(everybeam-test PROPERTIES CXX_VISIBILITY_PRESET default)
endif()

install(
  TARGETS everybeam everybeam-core
  COMPONENT libraries
  EXPORT EveryBeamTargets
  DESTINATION ${INSTALL_LIBDIR})

install(
  FILES antenna.h
        beamformer.h
        beamformeridenticalantennas.h
        beammode.h
        beamnormalisationmode.h
        element.h
        elementhamaker.h
        elementresponse.h
        everybeam.h
        msreadutils.h
        station.h
        stationnode.h
        load.h
        options.h
        phasedarrayresponse.h
        telescope.h
  DESTINATION "include/${CMAKE_PROJECT_NAME}")

# Install EveryBeamTargets.cmake, which contains the EveryBeam cmake targets.
# Other install() calls generate the contents for EveryBeamTargets.cmake.
# EveryBeamConfig.cmake includes the generated EveryBeamTargets.cmake.
install(
  EXPORT EveryBeamTargets
  NAMESPACE EveryBeam::
  DESTINATION "lib/${projectname}")

# Set-up cmake configuration files
configure_file("${CMAKE_SOURCE_DIR}/CMake/EveryBeamConfig.cmake.in"
               "${CMAKE_BINARY_DIR}/CMakeFiles/EveryBeamConfig.cmake" @ONLY)
configure_file(
  "${CMAKE_SOURCE_DIR}/CMake/EveryBeamConfigVersion.cmake.in"
  "${CMAKE_BINARY_DIR}/CMakeFiles/EveryBeamConfigVersion.cmake" @ONLY)

install(FILES "${CMAKE_BINARY_DIR}/CMakeFiles/EveryBeamConfig.cmake"
              "${CMAKE_BINARY_DIR}/CMakeFiles/EveryBeamConfigVersion.cmake"
        DESTINATION "lib/${projectname}")

add_subdirectory(aterms)
add_subdirectory(circularsymmetric)
add_subdirectory(common)
add_subdirectory(coords)
add_subdirectory(elementmodel)
add_subdirectory(griddedresponse)
add_subdirectory(hamaker)
add_subdirectory(lobes)
add_subdirectory(lwa)
add_subdirectory(oskar)
add_subdirectory(pointresponse)
add_subdirectory(skalowelementbeam)
add_subdirectory(skamidbeam)
add_subdirectory(telescope)
