# SPDX-FileCopyrightText: 2014-2026 Pavel Kalvoda <me@pavelkalvoda.com>
# SPDX-License-Identifier: MIT

add_executable(readfile readfile.c)
target_link_libraries(readfile cbor cbor_project_options)

add_executable(create_items create_items.c)
target_link_libraries(create_items cbor cbor_project_options)

add_executable(streaming_parser streaming_parser.c)
target_link_libraries(streaming_parser cbor cbor_project_options)

add_executable(streaming_array streaming_array.c)
target_link_libraries(streaming_array cbor cbor_project_options)

add_executable(sort sort.c)
target_link_libraries(sort cbor cbor_project_options)

add_executable(hello hello.c)
target_link_libraries(hello cbor cbor_project_options)

add_executable(cbor_sequence cbor_sequence.c)
target_link_libraries(cbor_sequence cbor cbor_project_options)

add_executable(crash_course crash_course.c)
target_link_libraries(crash_course cbor cbor_project_options)

add_executable(capped_alloc capped_alloc.c)
target_link_libraries(capped_alloc cbor cbor_project_options)

find_package(CJSON)

if(CJSON_FOUND)
  # The cJSON include directory is typically a shared prefix such as
  # /usr/include or /opt/homebrew/include. If a system-wide libcbor is also
  # installed there, a plain -I would make its (possibly stale) cbor.h shadow
  # the in-tree headers. SYSTEM emits -isystem, which is searched after all
  # -I paths, so the headers from the cbor target always take precedence.
  add_executable(cjson2cbor cjson2cbor.c)
  target_include_directories(cjson2cbor SYSTEM PRIVATE ${CJSON_INCLUDE_DIRS})
  target_link_libraries(cjson2cbor cbor cbor_project_options ${CJSON_LIBRARY})
  # For fabs
  find_library(MATH_LIBRARY m)
  if(MATH_LIBRARY)
    target_link_libraries(cjson2cbor ${MATH_LIBRARY})
  endif()

  add_executable(cbor2cjson cbor2cjson.c)
  target_include_directories(cbor2cjson SYSTEM PRIVATE ${CJSON_INCLUDE_DIRS})
  target_link_libraries(cbor2cjson cbor cbor_project_options ${CJSON_LIBRARY})
endif()

file(COPY data DESTINATION .)
