project.cmake 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Designed to be included from an IDF app's CMakeLists.txt file
  2. #
  3. cmake_minimum_required(VERSION 3.5)
  4. include(${CMAKE_CURRENT_LIST_DIR}/idf_functions.cmake)
  5. # Set the path of idf.py.
  6. set(IDFTOOL ${PYTHON} "${IDF_PATH}/tools/idf.py")
  7. # Trick to temporarily redefine project(). When functions are overriden in CMake, the originals can still be accessed
  8. # using an underscore prefixed function of the same name. The following lines make sure that __project calls
  9. # the original project(). See https://cmake.org/pipermail/cmake/2015-October/061751.html.
  10. function(project)
  11. endfunction()
  12. function(_project)
  13. endfunction()
  14. macro(project name)
  15. # Bridge existing documented variable names with library namespaced
  16. # variables in order for old projects to work.
  17. if(COMPONENT_DIRS)
  18. spaces2list(COMPONENT_DIRS)
  19. foreach(component_dir ${COMPONENT_DIRS})
  20. get_filename_component(component_dir ${component_dir} ABSOLUTE BASE_DIR ${CMAKE_SOURCE_DIR})
  21. list(APPEND IDF_COMPONENT_DIRS "${component_dir}")
  22. endforeach()
  23. endif()
  24. if(EXTRA_COMPONENT_DIRS)
  25. spaces2list(EXTRA_COMPONENT_DIRS)
  26. foreach(component_dir ${EXTRA_COMPONENT_DIRS})
  27. get_filename_component(component_dir ${component_dir} ABSOLUTE BASE_DIR ${CMAKE_SOURCE_DIR})
  28. list(APPEND IDF_EXTRA_COMPONENT_DIRS "${component_dir}")
  29. endforeach()
  30. endif()
  31. list(APPEND IDF_EXTRA_COMPONENT_DIRS "${CMAKE_SOURCE_DIR}/components")
  32. if(NOT MAIN_SRCS)
  33. list(APPEND IDF_EXTRA_COMPONENT_DIRS "${CMAKE_SOURCE_DIR}/main")
  34. endif()
  35. if(COMPONENTS)
  36. set(IDF_COMPONENTS "${COMPONENTS}")
  37. endif()
  38. if(COMPONENT_REQUIRES_COMMON)
  39. set(IDF_COMPONENT_REQUIRES_COMMON "${COMPONENT_REQUIRES_COMMON}")
  40. endif()
  41. if(EXCLUDE_COMPONENTS)
  42. set(IDF_EXCLUDE_COMPONENTS "${COMPONENT_EXCLUDES}")
  43. endif()
  44. if(TESTS_ALL EQUAL 1 OR TEST_COMPONENTS)
  45. set(IDF_BUILD_TESTS 1)
  46. endif()
  47. if(TEST_COMPONENTS)
  48. set(IDF_TEST_COMPONENTS "${TEST_COMPONENTS}")
  49. endif()
  50. if(TEST_EXCLUDE_COMPONENTS)
  51. set(IDF_TEST_EXCLUDE_COMPONENTS "${TEST_EXCLUDE_COMPONENTS}")
  52. endif()
  53. if(NOT SDKCONFIG_DEFAULTS)
  54. if(EXISTS ${CMAKE_SOURCE_DIR}/sdkconfig.defaults)
  55. set(IDF_SDKCONFIG_DEFAULTS ${CMAKE_SOURCE_DIR}/sdkconfig.defaults)
  56. endif()
  57. else()
  58. set(IDF_SDKCONFIG_DEFAULTS ${SDKCONFIG_DEFAULTS})
  59. endif()
  60. # Set build variables
  61. idf_set_variables()
  62. # Now the configuration is loaded, set the toolchain appropriately
  63. idf_set_toolchain()
  64. __project(${name} C CXX ASM)
  65. set(IDF_BUILD_ARTIFACTS ON)
  66. set(IDF_PROJECT_EXECUTABLE ${CMAKE_PROJECT_NAME}.elf)
  67. set(IDF_BUILD_ARTIFACTS_DIR ${CMAKE_BINARY_DIR})
  68. if(MAIN_SRCS)
  69. spaces2list(MAIN_SRCS)
  70. add_executable(${IDF_PROJECT_EXECUTABLE} ${MAIN_SRCS})
  71. else()
  72. # Create a dummy file to work around CMake requirement of having a source
  73. # file while adding an executable
  74. add_executable(${IDF_PROJECT_EXECUTABLE} "${CMAKE_CURRENT_BINARY_DIR}/dummy_main_src.c")
  75. add_custom_command(OUTPUT dummy_main_src.c
  76. COMMAND ${CMAKE_COMMAND} -E touch dummy_main_src.c
  77. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  78. VERBATIM)
  79. add_custom_target(dummy_main_src DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/dummy_main_src.c)
  80. add_dependencies(${IDF_PROJECT_EXECUTABLE} dummy_main_src)
  81. endif()
  82. set(mapfile "${CMAKE_PROJECT_NAME}.map")
  83. target_link_libraries(${IDF_PROJECT_EXECUTABLE} "-Wl,--cref -Wl,--Map=${mapfile}")
  84. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY
  85. ADDITIONAL_MAKE_CLEAN_FILES
  86. "${CMAKE_CURRENT_BINARY_DIR}/${mapfile}")
  87. # Add size targets, depend on map file, run idf_size.py
  88. add_custom_target(size
  89. DEPENDS ${exe_target}
  90. COMMAND ${PYTHON} ${IDF_PATH}/tools/idf_size.py ${mapfile}
  91. )
  92. add_custom_target(size-files
  93. DEPENDS ${exe_target}
  94. COMMAND ${PYTHON} ${IDF_PATH}/tools/idf_size.py --files ${mapfile}
  95. )
  96. add_custom_target(size-components
  97. DEPENDS ${exe_target}
  98. COMMAND ${PYTHON} ${IDF_PATH}/tools/idf_size.py --archives ${mapfile}
  99. )
  100. # Since components can import third-party libraries, the original definition of project() should be restored
  101. # before the call to add components to the build.
  102. function(project)
  103. set(project_ARGV ARGV)
  104. __project(${${project_ARGV}})
  105. endfunction()
  106. # Finally, add the rest of the components to the build.
  107. idf_import_components(components $ENV{IDF_PATH} esp-idf)
  108. idf_link_components(${IDF_PROJECT_EXECUTABLE} "${components}")
  109. endmacro()