# The math library (libm), found as a full path. A bare `m` could be mistaken
# for a target of the same name (a module called `m`). Empty where the math
# functions are part of the C library itself (Windows, macOS).
find_library(JM_MATH_LIBRARY m)
if(NOT JM_MATH_LIBRARY)
  set(JM_MATH_LIBRARY "")
endif()

# The component's C code, compiled once and linked into both its
# Python extension and the project's C library.
add_library(capture_core OBJECT capture_core.c)
target_include_directories(
  capture_core PUBLIC ${CMAKE_SOURCE_DIR}/native/inc
                      ${CMAKE_SOURCE_DIR}/native/inc/iqtools/capture)
target_link_libraries(capture_core PUBLIC
    doppler::doppler-static
    m)
if(TARGET ${PROJECT_NAME}_lib)
  target_link_libraries(${PROJECT_NAME}_lib PRIVATE
      doppler::doppler-static
      m)
  target_include_directories(${PROJECT_NAME}_lib INTERFACE
      $<$<TARGET_EXISTS:doppler::doppler-static>:$<TARGET_PROPERTY:doppler::doppler-static,INTERFACE_INCLUDE_DIRECTORIES>>)
  target_compile_definitions(${PROJECT_NAME}_lib INTERFACE
      $<$<TARGET_EXISTS:doppler::doppler-static>:$<TARGET_PROPERTY:doppler::doppler-static,INTERFACE_COMPILE_DEFINITIONS>>)
  target_compile_options(${PROJECT_NAME}_lib INTERFACE
      $<$<TARGET_EXISTS:doppler::doppler-static>:$<TARGET_PROPERTY:doppler::doppler-static,INTERFACE_COMPILE_OPTIONS>>)
endif()
if(TARGET ${PROJECT_NAME}_lib_static)
  target_link_libraries(${PROJECT_NAME}_lib_static PUBLIC
      doppler::doppler-static
      m)
endif()

# The C unit test; `ctest` runs it.
add_executable(test_capture_core
               ${CMAKE_SOURCE_DIR}/native/tests/test_capture_core.c
               ${CMAKE_SOURCE_DIR}/native/tests/test_capture_symbols.c)
target_link_libraries(test_capture_core
                      PRIVATE capture_core doppler::doppler-static
    m
    ${JM_MATH_LIBRARY})
target_include_directories(test_capture_core
                           PRIVATE ${CMAKE_SOURCE_DIR}/native/inc)
add_test(NAME test_capture_core COMMAND test_capture_core)

# The C benchmark; built, but not run by ctest.
add_executable(
  bench_capture_core
  ${CMAKE_SOURCE_DIR}/native/benchmarks/bench_capture_core.c)
target_link_libraries(bench_capture_core
                      PRIVATE capture_core doppler::doppler-static
    m
    ${JM_MATH_LIBRARY})
target_include_directories(
  bench_capture_core PRIVATE ${CMAKE_SOURCE_DIR}/native/inc
                                   ${CMAKE_SOURCE_DIR}/native/benchmarks)
# The math library (libm), found as a full path. A bare `m` could be mistaken
# for a target of the same name (a module called `m`). Empty where the math
# functions are part of the C library itself (Windows, macOS).
find_library(JM_MATH_LIBRARY m)
if(NOT JM_MATH_LIBRARY)
  set(JM_MATH_LIBRARY "")
endif()

if(BUILD_PYTHON)
# capture Python module — aggregates: Capture, RawCapture
Python3_add_library(capture MODULE WITH_SOABI capture_ext.c)
target_link_libraries(capture PRIVATE
    capture_core
    doppler::doppler-static
    m
    Python3::NumPy)
target_include_directories(capture PRIVATE ${CMAKE_SOURCE_DIR}/native/inc)
set_target_properties(capture PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY "${PYTHON_PACKAGE_DIR}/capture"
    RUNTIME_OUTPUT_DIRECTORY "${PYTHON_PACKAGE_DIR}/capture")
add_custom_command(TARGET capture POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        "$<TARGET_FILE:capture>"
        "${PYTHON_PACKAGE_DIR}/capture/$<TARGET_FILE_NAME:capture>"
    VERBATIM
    COMMENT "Copy capture extension module")
endif()

# Your own CMake for this directory goes in capture_extra.cmake beside this
# file: it is included here and never overwritten. This file is
# regenerated by just-makeit.
include(${CMAKE_CURRENT_LIST_DIR}/capture_extra.cmake OPTIONAL)
