1 # Designed to be included from an IDF app's CMakeLists.txt file
3 cmake_minimum_required(VERSION 3.5)
5 # Set IDF_PATH, as nothing else will work without this
6 set(IDF_PATH "$ENV{IDF_PATH}")
8 # Documentation says you should set IDF_PATH in your environment, but we
9 # can infer it relative to tools/cmake directory if it's not set.
10 get_filename_component(IDF_PATH "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE)
12 file(TO_CMAKE_PATH "${IDF_PATH}" IDF_PATH)
13 set(ENV{IDF_PATH} ${IDF_PATH})
20 "${IDF_PATH}/tools/cmake"
21 "${IDF_PATH}/tools/cmake/third_party"
23 include(GetGitRevisionDescription)
27 include(git_submodules)
28 include(idf_functions)
30 set_default(PYTHON "python")
34 # This macro wraps the cmake 'project' command to add
35 # all of the IDF-specific functionality required
37 # Implementation Note: This macro wraps 'project' on purpose, because cmake has
38 # some backwards-compatible magic where if you don't call "project" in the
39 # top-level CMakeLists file, it will call it implicitly. However, the implicit
40 # project will not have CMAKE_TOOLCHAIN_FILE set and therefore tries to
41 # create a native build project.
43 # Therefore, to keep all the IDF "build magic", the cleanest way is to keep the
44 # top-level "project" call but customize it to do what we want in the IDF build.
47 # Set global variables used by rest of the build
48 idf_set_global_variables()
50 # Establish dependencies for components in the build
51 # (this happens before we even generate config...)
53 # Make sure if an explicit list of COMPONENTS is given, it contains the "common" component requirements
54 # (otherwise, if COMPONENTS is empty then all components will be included in the build.)
55 set(COMPONENTS "${COMPONENTS} ${COMPONENT_REQUIRES_COMMON}")
57 execute_process(COMMAND "${CMAKE_COMMAND}"
58 -D "COMPONENTS=${COMPONENTS}"
59 -D "DEPENDENCIES_FILE=${CMAKE_BINARY_DIR}/component_depends.cmake"
60 -D "COMPONENT_DIRS=${COMPONENT_DIRS}"
61 -D "BOOTLOADER_BUILD=${BOOTLOADER_BUILD}"
62 -P "${IDF_PATH}/tools/cmake/scripts/expand_requirements.cmake"
63 WORKING_DIRECTORY "${IDF_PATH}/tools/cmake")
64 include("${CMAKE_BINARY_DIR}/component_depends.cmake")
66 # We now have the following component-related variables:
67 # COMPONENTS is the list of initial components set by the user (or empty to include all components in the build).
68 # BUILD_COMPONENTS is the list of components to include in the build.
69 # BUILD_COMPONENT_PATHS is the paths to all of these components.
71 # Print list of components
72 string(REPLACE ";" " " BUILD_COMPONENTS_SPACES "${BUILD_COMPONENTS}")
73 message(STATUS "Component names: ${BUILD_COMPONENTS_SPACES}")
74 unset(BUILD_COMPONENTS_SPACES)
75 message(STATUS "Component paths: ${BUILD_COMPONENT_PATHS}")
77 kconfig_set_variables()
79 kconfig_process_config()
81 # Include sdkconfig.cmake so rest of the build knows the configuration
82 include(${SDKCONFIG_CMAKE})
84 # Now the configuration is loaded, set the toolchain appropriately
86 # TODO: support more toolchains than just ESP32
87 set(CMAKE_TOOLCHAIN_FILE $ENV{IDF_PATH}/tools/cmake/toolchain-esp32.cmake)
89 # Declare the actual cmake-level project
90 _project(${name} ASM C CXX)
92 # generate compile_commands.json (needs to come after project)
93 set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
95 # Verify the environment is configured correctly
96 idf_verify_environment()
98 # Add some idf-wide definitions
99 idf_set_global_compiler_options()
101 # Check git revision (may trigger reruns of cmake)
102 ## sets IDF_VER to IDF git revision
103 idf_get_git_revision()
104 ## if project uses git, retrieve revision
105 git_describe(PROJECT_VER "${CMAKE_CURRENT_SOURCE_DIR}")
107 # Include any top-level project_include.cmake files from components
108 foreach(component ${BUILD_COMPONENT_PATHS})
109 include_if_exists("${component}/project_include.cmake")
113 # Add each component to the build as a library
115 foreach(COMPONENT_PATH ${BUILD_COMPONENT_PATHS})
116 get_filename_component(COMPONENT_NAME ${COMPONENT_PATH} NAME)
117 add_subdirectory(${COMPONENT_PATH} ${COMPONENT_NAME})
119 unset(COMPONENT_NAME)
120 unset(COMPONENT_PATH)
123 # Add the app executable to the build (has name of PROJECT.elf)
127 # Write project description JSON file
128 make_json_list("${BUILD_COMPONENTS}" build_components_json)
129 make_json_list("${BUILD_COMPONENT_PATHS}" build_component_paths_json)
130 configure_file("${IDF_PATH}/tools/cmake/project_description.json.in"
131 "${CMAKE_BINARY_DIR}/project_description.json")
132 unset(build_components_json)
133 unset(build_component_paths_json)
136 # Finish component registration (add cross-dependencies, make
137 # executable dependent on all components)
139 components_finish_registration()