1 cmake_minimum_required(VERSION 3.5)
2 project(esp-idf C CXX ASM)
5 set(IDF_PATH ${CMAKE_CURRENT_LIST_DIR})
8 include(tools/cmake/idf_functions.cmake)
11 # Set variables that control the build configuration and the build itself
15 kconfig_set_variables()
18 # Generate a component dependencies file, enumerating components to be included in the build
19 # as well as their dependencies.
21 execute_process(COMMAND "${CMAKE_COMMAND}"
22 -D "COMPONENTS=${IDF_COMPONENTS}"
23 -D "COMPONENT_REQUIRES_COMMON=${IDF_COMPONENT_REQUIRES_COMMON}"
24 -D "EXCLUDE_COMPONENTS=${IDF_EXCLUDE_COMPONENTS}"
25 -D "TEST_COMPONENTS=${IDF_TEST_COMPONENTS}"
26 -D "TEST_EXCLUDE_COMPONENTS=${IDF_TEST_EXCLUDE_COMPONENTS}"
27 -D "BUILD_TESTS=${IDF_BUILD_TESTS}"
28 -D "DEPENDENCIES_FILE=${CMAKE_BINARY_DIR}/component_depends.cmake"
29 -D "COMPONENT_DIRS=${IDF_COMPONENT_DIRS}"
30 -D "BOOTLOADER_BUILD=${BOOTLOADER_BUILD}"
31 -D "IDF_TARGET=${IDF_TARGET}"
32 -D "IDF_PATH=${IDF_PATH}"
34 -P "${IDF_PATH}/tools/cmake/scripts/expand_requirements.cmake"
35 WORKING_DIRECTORY "${PROJECT_PATH}"
36 RESULT_VARIABLE expand_requirements_result)
38 if(expand_requirements_result)
39 message(FATAL_ERROR "Failed to expand component requirements")
42 include("${CMAKE_BINARY_DIR}/component_depends.cmake")
45 # We now have the following component-related variables:
47 # IDF_COMPONENTS is the list of initial components set by the user
48 # (or empty to include all components in the build).
49 # BUILD_COMPONENTS is the list of components to include in the build.
50 # BUILD_COMPONENT_PATHS is the paths to all of these components, obtained from the component dependencies file.
52 # Print the list of found components and test components
54 string(REPLACE ";" " " BUILD_COMPONENTS_SPACES "${BUILD_COMPONENTS}")
55 message(STATUS "Component names: ${BUILD_COMPONENTS_SPACES}")
56 unset(BUILD_COMPONENTS_SPACES)
57 message(STATUS "Component paths: ${BUILD_COMPONENT_PATHS}")
59 # Print list of test components
60 if(TESTS_ALL EQUAL 1 OR TEST_COMPONENTS)
61 string(REPLACE ";" " " BUILD_TEST_COMPONENTS_SPACES "${BUILD_TEST_COMPONENTS}")
62 message(STATUS "Test component names: ${BUILD_TEST_COMPONENTS_SPACES}")
63 unset(BUILD_TEST_COMPONENTS_SPACES)
64 message(STATUS "Test component paths: ${BUILD_TEST_COMPONENT_PATHS}")
67 # Generate project configuration
68 kconfig_process_config()
70 # Include sdkconfig.cmake so rest of the build knows the configuration
71 include(${SDKCONFIG_CMAKE})
73 # Verify the environment is configured correctly
74 idf_verify_environment()
76 # Check git revision (may trigger reruns of cmake)
77 ## sets IDF_VER to IDF git revision
78 idf_get_git_revision()
80 # Check that the targets set in cache, sdkconfig, and in environment all match
81 idf_check_config_target()
84 if(NOT BOOTLOADER_BUILD)
85 app_get_revision("${CMAKE_SOURCE_DIR}")
88 # Add some idf-wide definitions
89 idf_set_global_compile_options()
91 # generate compile_commands.json (needs to come after project)
92 set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
95 # Setup variables for linker script generation
99 # Include any top-level project_include.cmake files from components
100 foreach(component ${BUILD_COMPONENT_PATHS})
101 set(COMPONENT_PATH "${component}")
102 include_if_exists("${component}/project_include.cmake")
103 unset(COMPONENT_PATH)
107 # Add each component to the build as a library
109 foreach(COMPONENT_PATH ${BUILD_COMPONENT_PATHS})
110 get_filename_component(COMPONENT_NAME ${COMPONENT_PATH} NAME)
112 list(FIND BUILD_TEST_COMPONENT_PATHS ${COMPONENT_PATH} idx)
115 list(GET BUILD_TEST_COMPONENTS ${idx} test_component)
116 set(COMPONENT_NAME ${test_component})
119 component_get_target(COMPONENT_TARGET ${COMPONENT_NAME})
121 add_subdirectory(${COMPONENT_PATH} ${COMPONENT_NAME})
123 unset(COMPONENT_NAME)
124 unset(COMPONENT_PATH)
126 # each component should see the include directories of its requirements
128 # (we can't do this until all components are registered and targets exist in cmake, as we have
129 # a circular requirements graph...)
130 foreach(component ${BUILD_COMPONENTS})
131 component_get_target(component_target ${component})
132 if(TARGET ${component_target})
133 get_component_requirements(${component} deps priv_deps)
135 list(APPEND priv_deps ${IDF_COMPONENT_REQUIRES_COMMON})
138 component_get_target(dep_target ${dep})
139 add_component_dependencies(${component_target} ${dep_target} PUBLIC)
142 foreach(dep ${priv_deps})
143 component_get_target(dep_target ${dep})
144 add_component_dependencies(${component_target} ${dep_target} PRIVATE)
149 if(IDF_BUILD_ARTIFACTS)
150 # Write project description JSON file
151 make_json_list("${BUILD_COMPONENTS}" build_components_json)
152 make_json_list("${BUILD_COMPONENT_PATHS}" build_component_paths_json)
153 configure_file("${IDF_PATH}/tools/cmake/project_description.json.in"
154 "${IDF_BUILD_ARTIFACTS_DIR}/project_description.json")
155 unset(build_components_json)
156 unset(build_component_paths_json)
159 set(BUILD_COMPONENTS ${BUILD_COMPONENTS} PARENT_SCOPE)
161 ldgen_add_dependencies()