]> granicus.if.org Git - esp-idf/blob - tools/cmake/project.cmake
cmake: Fix issues when IDF_PATH is not set in environment
[esp-idf] / tools / cmake / project.cmake
1 # Designed to be included from an IDF app's CMakeLists.txt file
2 #
3 cmake_minimum_required(VERSION 3.5)
4
5 # Set IDF_PATH, as nothing else will work without this
6 set(IDF_PATH "$ENV{IDF_PATH}")
7 if(NOT 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)
11 endif()
12 file(TO_CMAKE_PATH "${IDF_PATH}" IDF_PATH)
13 set(ENV{IDF_PATH} ${IDF_PATH})
14
15
16 #
17 # Load cmake modules
18 #
19 set(CMAKE_MODULE_PATH
20     "${IDF_PATH}/tools/cmake"
21     "${IDF_PATH}/tools/cmake/third_party"
22     ${CMAKE_MODULE_PATH})
23 include(GetGitRevisionDescription)
24 include(utilities)
25 include(components)
26 include(kconfig)
27 include(git_submodules)
28 include(idf_functions)
29
30 set_default(PYTHON "python")
31
32 # project
33 #
34 # This macro wraps the cmake 'project' command to add
35 # all of the IDF-specific functionality required
36 #
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.
42 #
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.
45 #
46 macro(project name)
47     # Set global variables used by rest of the build
48     idf_set_global_variables()
49
50     # Establish dependencies for components in the build
51     # (this happens before we even generate config...)
52     if(COMPONENTS)
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}")
56     endif()
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")
65
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.
70
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}")
76
77     kconfig_set_variables()
78
79     kconfig_process_config()
80
81     # Include sdkconfig.cmake so rest of the build knows the configuration
82     include(${SDKCONFIG_CMAKE})
83
84     # Now the configuration is loaded, set the toolchain appropriately
85     #
86     # TODO: support more toolchains than just ESP32
87     set(CMAKE_TOOLCHAIN_FILE $ENV{IDF_PATH}/tools/cmake/toolchain-esp32.cmake)
88
89     # Declare the actual cmake-level project
90     _project(${name} ASM C CXX)
91
92     # generate compile_commands.json (needs to come after project)
93     set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
94
95     # Verify the environment is configured correctly
96     idf_verify_environment()
97
98     # Add some idf-wide definitions
99     idf_set_global_compiler_options()
100
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}")
106
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")
110     endforeach()
111
112     #
113     # Add each component to the build as a library
114     #
115     foreach(COMPONENT_PATH ${BUILD_COMPONENT_PATHS})
116         get_filename_component(COMPONENT_NAME ${COMPONENT_PATH} NAME)
117         add_subdirectory(${COMPONENT_PATH} ${COMPONENT_NAME})
118     endforeach()
119     unset(COMPONENT_NAME)
120     unset(COMPONENT_PATH)
121
122     #
123     # Add the app executable to the build (has name of PROJECT.elf)
124     #
125     idf_add_executable()
126
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)
134
135     #
136     # Finish component registration (add cross-dependencies, make
137     # executable dependent on all components)
138     #
139     components_finish_registration()
140
141 endmacro()