1cmake_minimum_required(VERSION 3.5) 2 3project(idf_as_lib C) 4 5if("${TARGET}" STREQUAL "esp32") 6 # Include for ESP-IDF build system functions 7 include($ENV{IDF_PATH}/tools/cmake/idf.cmake) 8 # Create idf::esp32 and idf::freertos static libraries 9 idf_build_process(esp32 10 # try and trim the build; additional components 11 # will be included as needed based on dependency tree 12 # 13 # although esptool_py does not generate static library, 14 # processing the component is needed for flashing related 15 # targets and file generation 16 COMPONENTS esp32 freertos esptool_py 17 SDKCONFIG ${CMAKE_CURRENT_LIST_DIR}/sdkconfig 18 BUILD_DIR ${CMAKE_BINARY_DIR}) 19else() 20 # Create stubs for esp32 and freertos, stub::esp32 and stub::freertos 21 add_subdirectory(stubs/esp32) 22 add_subdirectory(stubs/freertos) 23 add_subdirectory(stubs/spi_flash) 24endif() 25 26set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 27 28set(elf_file ${CMAKE_PROJECT_NAME}.elf) 29add_executable(${elf_file} main.c) 30 31# Link the static libraries to the executable 32if("${TARGET}" STREQUAL "esp32") 33 target_link_libraries(${elf_file} idf::esp32 idf::freertos idf::spi_flash) 34 # Attach additional targets to the executable file for flashing, 35 # linker script generation, partition_table generation, etc. 36 idf_build_executable(${elf_file}) 37else() 38 target_link_libraries(${elf_file} stub::esp32 stub::freertos stub::spi_flash) 39endif() 40