CMakeLists.txt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. cmake_minimum_required(VERSION 3.5)
  2. project(esp-idf C CXX ASM)
  3. if(NOT IDF_PATH)
  4. set(IDF_PATH ${CMAKE_CURRENT_LIST_DIR})
  5. endif()
  6. include(tools/cmake/idf_functions.cmake)
  7. #
  8. # Set variables that control the build configuration and the build itself
  9. #
  10. idf_set_variables()
  11. kconfig_set_variables()
  12. #
  13. # Generate a component dependencies file, enumerating components to be included in the build
  14. # as well as their dependencies.
  15. #
  16. execute_process(COMMAND "${CMAKE_COMMAND}"
  17. -D "COMPONENTS=${IDF_COMPONENTS}"
  18. -D "COMPONENT_REQUIRES_COMMON=${IDF_COMPONENT_REQUIRES_COMMON}"
  19. -D "EXCLUDE_COMPONENTS=${IDF_EXCLUDE_COMPONENTS}"
  20. -D "TEST_COMPONENTS=${IDF_TEST_COMPONENTS}"
  21. -D "TEST_EXCLUDE_COMPONENTS=${IDF_TEST_EXCLUDE_COMPONENTS}"
  22. -D "BUILD_TESTS=${IDF_BUILD_TESTS}"
  23. -D "DEPENDENCIES_FILE=${CMAKE_BINARY_DIR}/component_depends.cmake"
  24. -D "COMPONENT_DIRS=${IDF_COMPONENT_DIRS}"
  25. -D "BOOTLOADER_BUILD=${BOOTLOADER_BUILD}"
  26. -D "IDF_TARGET=${IDF_TARGET}"
  27. -D "IDF_PATH=${IDF_PATH}"
  28. -D "DEBUG=${DEBUG}"
  29. -P "${IDF_PATH}/tools/cmake/scripts/expand_requirements.cmake"
  30. WORKING_DIRECTORY "${PROJECT_PATH}"
  31. RESULT_VARIABLE expand_requirements_result)
  32. if(expand_requirements_result)
  33. message(FATAL_ERROR "Failed to expand component requirements")
  34. endif()
  35. include("${CMAKE_BINARY_DIR}/component_depends.cmake")
  36. #
  37. # We now have the following component-related variables:
  38. #
  39. # IDF_COMPONENTS is the list of initial components set by the user
  40. # (or empty to include all components in the build).
  41. # BUILD_COMPONENTS is the list of components to include in the build.
  42. # BUILD_COMPONENT_PATHS is the paths to all of these components, obtained from the component dependencies file.
  43. #
  44. # Print the list of found components and test components
  45. #
  46. string(REPLACE ";" " " BUILD_COMPONENTS_SPACES "${BUILD_COMPONENTS}")
  47. message(STATUS "Component names: ${BUILD_COMPONENTS_SPACES}")
  48. unset(BUILD_COMPONENTS_SPACES)
  49. message(STATUS "Component paths: ${BUILD_COMPONENT_PATHS}")
  50. # Print list of test components
  51. if(TESTS_ALL EQUAL 1 OR TEST_COMPONENTS)
  52. string(REPLACE ";" " " BUILD_TEST_COMPONENTS_SPACES "${BUILD_TEST_COMPONENTS}")
  53. message(STATUS "Test component names: ${BUILD_TEST_COMPONENTS_SPACES}")
  54. unset(BUILD_TEST_COMPONENTS_SPACES)
  55. message(STATUS "Test component paths: ${BUILD_TEST_COMPONENT_PATHS}")
  56. endif()
  57. # Generate project configuration
  58. kconfig_process_config()
  59. # Include sdkconfig.cmake so rest of the build knows the configuration
  60. include(${SDKCONFIG_CMAKE})
  61. # Verify the environment is configured correctly
  62. idf_verify_environment()
  63. # Check git revision (may trigger reruns of cmake)
  64. ## sets IDF_VER to IDF git revision
  65. idf_get_git_revision()
  66. # Check that the targets set in cache, sdkconfig, and in environment all match
  67. idf_check_config_target()
  68. ## get PROJECT_VER
  69. if(NOT BOOTLOADER_BUILD)
  70. app_get_revision("${CMAKE_SOURCE_DIR}")
  71. endif()
  72. # Add some idf-wide definitions
  73. idf_set_global_compile_options()
  74. # generate compile_commands.json (needs to come after project)
  75. set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
  76. #
  77. # Setup variables for linker script generation
  78. #
  79. ldgen_set_variables()
  80. # Include any top-level project_include.cmake files from components
  81. foreach(component ${BUILD_COMPONENT_PATHS})
  82. set(COMPONENT_PATH "${component}")
  83. include_if_exists("${component}/project_include.cmake")
  84. unset(COMPONENT_PATH)
  85. endforeach()
  86. #
  87. # Add each component to the build as a library
  88. #
  89. foreach(COMPONENT_PATH ${BUILD_COMPONENT_PATHS})
  90. get_filename_component(COMPONENT_NAME ${COMPONENT_PATH} NAME)
  91. list(FIND BUILD_TEST_COMPONENT_PATHS ${COMPONENT_PATH} idx)
  92. if(NOT idx EQUAL -1)
  93. list(GET BUILD_TEST_COMPONENTS ${idx} test_component)
  94. set(COMPONENT_NAME ${test_component})
  95. endif()
  96. component_get_target(COMPONENT_TARGET ${COMPONENT_NAME})
  97. add_subdirectory(${COMPONENT_PATH} ${COMPONENT_NAME})
  98. endforeach()
  99. unset(COMPONENT_NAME)
  100. unset(COMPONENT_PATH)
  101. # each component should see the include directories of its requirements
  102. #
  103. # (we can't do this until all components are registered and targets exist in cmake, as we have
  104. # a circular requirements graph...)
  105. foreach(component ${BUILD_COMPONENTS})
  106. component_get_target(component_target ${component})
  107. if(TARGET ${component_target})
  108. get_component_requirements(${component} deps priv_deps)
  109. list(APPEND priv_deps ${IDF_COMPONENT_REQUIRES_COMMON})
  110. foreach(dep ${deps})
  111. component_get_target(dep_target ${dep})
  112. add_component_dependencies(${component_target} ${dep_target} PUBLIC)
  113. endforeach()
  114. foreach(dep ${priv_deps})
  115. component_get_target(dep_target ${dep})
  116. add_component_dependencies(${component_target} ${dep_target} PRIVATE)
  117. endforeach()
  118. endif()
  119. endforeach()
  120. if(IDF_BUILD_ARTIFACTS)
  121. # Write project description JSON file
  122. make_json_list("${BUILD_COMPONENTS}" build_components_json)
  123. make_json_list("${BUILD_COMPONENT_PATHS}" build_component_paths_json)
  124. configure_file("${IDF_PATH}/tools/cmake/project_description.json.in"
  125. "${IDF_BUILD_ARTIFACTS_DIR}/project_description.json")
  126. unset(build_components_json)
  127. unset(build_component_paths_json)
  128. endif()
  129. set(BUILD_COMPONENTS ${BUILD_COMPONENTS} PARENT_SCOPE)
  130. ldgen_add_dependencies()