# Copyright (C) 2023 SpyroSoft Solutions S.A. All rights reserved. #

#
# DO NOT EDIT THIS FILE! This file is generated by `scargo update`.
#
cmake_minimum_required(VERSION 3.16)

list(APPEND CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}/generators/")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# To support pure idf usage
if(NOT DEFINED ENV{SCARGO_BUILD_TARGET})
    set(ENV{SCARGO_BUILD_TARGET} "ESP32")
endif()

{# This could be put in profile or toolchain if we were able to resolve issues with esp32 toolchain not loading #}
# Add definiton [ATSAM, ESP32, STM32, X86] depending on current build target
string(TOUPPER $ENV{SCARGO_BUILD_TARGET} SCARGO_BUILD_TARGET)
add_definitions(-D${SCARGO_BUILD_TARGET})

{% if config.project.is_esp32()  %}
string(FIND "${SCARGO_BUILD_TARGET}" "ESP32" position)
if(NOT position EQUAL -1)
  include($ENV{IDF_PATH}/tools/cmake/project.cmake)
  set(CMAKE_CXX_STANDARD {{ config.project.cxxstandard }})
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{WORKAROUND_FOR_ESP32_C_FLAGS}")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{WORKAROUND_FOR_ESP32_CXX_FLAGS}")

  {% if config.esp32.extra_component_dirs %}
  set(EXTRA_COMPONENT_DIRS {{ config.esp32.extra_component_dirs |join(" ") }})
  {% endif %}
  set(IDF_TARGET {{ config.esp32.chip|lower }})
endif()
{% endif %}

project(
  {{ config.project.name }}
  {% if config.project.version %}
  VERSION {{ config.project.version }}
  {% endif %}
  {% if config.project.description %}
  DESCRIPTION "{{ config.project.description }}"
  {% endif %}
  {% if config.project.homepage_url %}
  HOMEPAGE_URL "{{ config.project.homepage_url }}"
  {% endif %}
  LANGUAGES C CXX ASM)

{% for key, value in config.project.cmake_variables.items() %}
set({{ key }} "{{ value }}")
{% endfor %}

{% for profile_name, profile in config.profiles.items() %}
    {% if profile.extras %}
# {{ profile_name }}
if("${CMAKE_BUILD_TYPE}" STREQUAL "{{ profile_name }}")
        {% for key, value in profile.extras.items() %}
  set({{ key }} {{ value }})
        {% endfor %}
endif()
    {% endif %}
{% endfor -%}

if(NOT ${SCARGO_BUILD_TARGET} STREQUAL ESP32)
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  add_subdirectory({{ config.source_dir_path.relative_to(config.project_root) }})
endif()

