# 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()

add_library(cvt_core OBJECT cvt_core.c int_to_bin.c hex_to_bin.c bytes_to_bin.c bin_to_int.c bin_to_hex.c bin_to_nrz.c nrz_to_bin.c)
target_include_directories(cvt_core PUBLIC ${CMAKE_SOURCE_DIR}/native/inc)

if(BUILD_PYTHON)
# cvt Python module — aggregates: F32ToI8, F32ToI16, F32ToI32, I8ToF32, U8ToF32, I16ToF32, I32ToF32, F32ToI16U32, F32ToI16U64, I16U32ToF32, I16U64ToF32, F32ToUQ15, UQ15ToF32, ADC
Python3_add_library(cvt MODULE WITH_SOABI cvt_ext.c)
target_link_libraries(cvt PRIVATE
    cvt_core
    f32_to_i8_core
    f32_to_i16_core
    f32_to_i32_core
    i8_to_f32_core
    u8_to_f32_core
    i16_to_f32_core
    i32_to_f32_core
    f32_to_i16u32_core
    f32_to_i16u64_core
    i16u32_to_f32_core
    i16u64_to_f32_core
    f32_to_uq15_core
    uq15_to_f32_core
    adc_core
    Python3::NumPy)
target_include_directories(cvt PRIVATE ${CMAKE_SOURCE_DIR}/native/inc)
set_target_properties(cvt PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY "${PYTHON_PACKAGE_DIR}/cvt"
    RUNTIME_OUTPUT_DIRECTORY "${PYTHON_PACKAGE_DIR}/cvt")
add_custom_command(TARGET cvt POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        "$<TARGET_FILE:cvt>"
        "${PYTHON_PACKAGE_DIR}/cvt/$<TARGET_FILE_NAME:cvt>"
    VERBATIM
    COMMENT "Copy cvt extension module")
endif()

# The C unit test; `ctest` runs it.
add_executable(test_cvt_core
    ${CMAKE_SOURCE_DIR}/native/tests/cvt/test_cvt_core.c)
target_link_libraries(test_cvt_core PRIVATE
    cvt_core
    f32_to_i8_core
    f32_to_i16_core
    f32_to_i32_core
    i8_to_f32_core
    u8_to_f32_core
    i16_to_f32_core
    i32_to_f32_core
    f32_to_i16u32_core
    f32_to_i16u64_core
    i16u32_to_f32_core
    i16u64_to_f32_core
    f32_to_uq15_core
    uq15_to_f32_core
    adc_core
    ${JM_MATH_LIBRARY})
target_include_directories(test_cvt_core
    PRIVATE ${CMAKE_SOURCE_DIR}/native/inc
            ${CMAKE_SOURCE_DIR}/native/tests)
add_test(NAME test_cvt_core COMMAND test_cvt_core)

# The C benchmark; built, but not run by ctest.
add_executable(bench_cvt_core
    ${CMAKE_SOURCE_DIR}/native/benchmarks/cvt/bench_cvt_core.c)
target_link_libraries(bench_cvt_core PRIVATE
    cvt_core
    f32_to_i8_core
    f32_to_i16_core
    f32_to_i32_core
    i8_to_f32_core
    u8_to_f32_core
    i16_to_f32_core
    i32_to_f32_core
    f32_to_i16u32_core
    f32_to_i16u64_core
    i16u32_to_f32_core
    i16u64_to_f32_core
    f32_to_uq15_core
    uq15_to_f32_core
    adc_core
    ${JM_MATH_LIBRARY})
target_include_directories(bench_cvt_core
    PRIVATE ${CMAKE_SOURCE_DIR}/native/inc
            ${CMAKE_SOURCE_DIR}/native/benchmarks)

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