# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# =- Compile lisabeta cython extension modules -=
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

function(define_lisabeta_extension import_path import_name supplementary_sources supplementary_links)
    # - Do not modify
    add_custom_command(
        OUTPUT ${import_name}.c
        COMMENT "Cythonize ${import_name}.pyx into ${import_name}.c"
        COMMAND
          Python::Interpreter -m cython "${CMAKE_CURRENT_SOURCE_DIR}/${import_name}.pyx"
          --output-file "${CMAKE_CURRENT_BINARY_DIR}/${import_name}.c" --module-name "${import_name}"
          -I "${CMAKE_CURRENT_SOURCE_DIR}"
        DEPENDS "${import_name}.pyx"
        WORKING_DIRECTORY ${LISABETA_BASE_BUILD_DIR}
        VERBATIM
    )

    # - Compute target name as ${import_path}_${import_name} (with '/' replaced by '_' in import_path)
    string(REPLACE "/" "_" sanitized_import_path ${import_path})
    set(target_name "${sanitized_import_path}_${import_name}")

    # If supplementary_sources is not empty, create a ${target_name}_base static library from them
    # treat supplementary_sources as a CMake list
    if(supplementary_sources)
       add_library(${target_name}_base STATIC ${supplementary_sources})
       target_link_libraries(${target_name}_base PRIVATE lisabeta_lalsuite ${supplementary_links} Python::Module Python::NumPy)
    endif()

    python_add_library(${target_name} MODULE WITH_SOABI
        ${import_name}.c
        ${supplementary_sources}
    )
    target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

    if(supplementary_sources)
        target_link_libraries(${target_name} PRIVATE ${target_name}_base)
    endif()

    target_link_libraries(${target_name} PRIVATE lisabeta_lalsuite ${supplementary_links} Python::NumPy)

    set_property(TARGET ${target_name} PROPERTY OUTPUT_NAME ${import_name})
    set_property(TARGET ${target_name} PROPERTY INSTALL_RPATH_USE_LINK_PATH ON)
    
    install(TARGETS ${target_name} DESTINATION ${import_path})
endfunction()

# -- lisabeta.pyconstants --

set(import_path "lisabeta")
set(import_name "pyconstants")
set(supplementary_sources "") # Separate with ; (e.g. "file1.c;file2.c")
set(supplementary_links "") # Separate with ; (e.g. "GSL::gsl;fftw3")

define_lisabeta_extension(
    ${import_path} ${import_name}
    "${supplementary_sources}" "${supplementary_links}")

# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# =- Add subdirectories with compiled extensions -=
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

add_subdirectory(struct)
add_subdirectory(tools)
add_subdirectory(lisa)
add_subdirectory(lvk)
add_subdirectory(likelihood)
add_subdirectory(waveforms)

# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=