#
# Copyright (c) 2022 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0
#

cmake_minimum_required(VERSION 3.13.3)
project (sample_pet)

# The zcbor-generated files can be regenerated by calling this file with -DREGENERATE_ZCBOR=Y
# This will call zcbor to regenerate the files, and then build like usual.
if (REGENERATE_ZCBOR)
  set (zcbor_command
    zcbor code # Invoke code generation
    --decode --encode # Generate both encoding and decoding code
    --short-names # Attempt to make generated symbol names shorter (at the risk of collision)
    -c ${CMAKE_CURRENT_LIST_DIR}/../../tests/cases/pet.cddl # Generate code for the data structures in pet.cddl
    -t Pet # Create a public API for decoding/encoding the "Pet" type from pet.cddl
    --output-cmake ${CMAKE_CURRENT_LIST_DIR}/pet.cmake # The generated cmake file will be placed here
                                                       # This also causes the c code to be placed in
                                                       # ${CMAKE_CURRENT_LIST_DIR}/src and ${CMAKE_CURRENT_LIST_DIR}/include)
    --file-header "Copyright (c) 2022 Nordic Semiconductor ASA\n\nSPDX-License-Identifier: Apache-2.0"
    )
  execute_process(COMMAND ${zcbor_command})
endif()

# Convert the data in the pet1.yml file to CBOR and put the data in a header
# file as a C array.
set(py_command_convert_pet1
  zcbor convert -c ${CMAKE_CURRENT_LIST_DIR}/../../tests/cases/pet.cddl -t Pet
  -i ${CMAKE_CURRENT_LIST_DIR}/pet1.yml
  -o ${PROJECT_BINARY_DIR}/include/pet1.h --c-code-var-name pet1
  --yaml-compatibility
  )
add_custom_target(pet1_yaml
  COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/include
  COMMAND ${py_command_convert_pet1}
  )

# Include the cmake file generated by the regenerate_zcbor.sh file. It adds the
# generated code and the necessary zcbor C code files.
include(${CMAKE_CURRENT_LIST_DIR}/pet.cmake)

add_executable(app src/main.c)

# Link the library from pet.cmake
target_link_libraries(app PRIVATE pet)
add_dependencies(app pet1_yaml)

# Add the directory containing pet1.h
target_include_directories(app PRIVATE ${PROJECT_BINARY_DIR}/include)
