1# SPDX-License-Identifier: Apache-2.0
2#
3# Copyright (c) 2021, Nordic Semiconductor ASA
4
5# This CMake module will load all Zephyr CMake modules in correct order for
6# default Zephyr build system.
7#
8# Outcome:
9# See individual CMake module descriptions
10
11include_guard(GLOBAL)
12
13# The code line below defines the real minimum supported CMake version.
14#
15# Unfortunately CMake requires the toplevel CMakeLists.txt file to define the
16# required version, not even invoking it from a CMake module is sufficient.
17# It is however permitted to have multiple invocations of cmake_minimum_required.
18cmake_minimum_required(VERSION 3.20.0)
19
20message(STATUS "Application: ${APPLICATION_SOURCE_DIR}")
21
22# Different CMake versions can have very subtle differences, for
23# instance CMake 3.21 links object files in a different order compared
24# to CMake 3.20; this produces different binaries.
25message(STATUS "CMake version: ${CMAKE_VERSION}")
26
27# Find and execute workspace build configuration
28find_package(ZephyrBuildConfiguration
29  QUIET NO_POLICY_SCOPE
30  NAMES ZephyrBuild
31  PATHS ${ZEPHYR_BASE}/../*
32  NO_CMAKE_PATH
33  NO_CMAKE_ENVIRONMENT_PATH
34  NO_SYSTEM_ENVIRONMENT_PATH
35  NO_CMAKE_PACKAGE_REGISTRY
36  NO_CMAKE_SYSTEM_PATH
37  NO_CMAKE_SYSTEM_PACKAGE_REGISTRY
38)
39
40# Find and execute application-specific build configuration
41find_package(ZephyrAppConfiguration
42  QUIET NO_POLICY_SCOPE
43  NAMES ZephyrApp
44  PATHS ${APPLICATION_SOURCE_DIR}
45  NO_CMAKE_PATH
46  NO_CMAKE_ENVIRONMENT_PATH
47  NO_SYSTEM_ENVIRONMENT_PATH
48  NO_CMAKE_PACKAGE_REGISTRY
49  NO_CMAKE_SYSTEM_PATH
50  NO_CMAKE_SYSTEM_PACKAGE_REGISTRY
51)
52
53# Test and error-out if we are affected by the PyPI CMake 3.22.1 / 3.22.2 bug
54if(${CMAKE_VERSION} VERSION_EQUAL 3.22.1 OR ${CMAKE_VERSION} VERSION_EQUAL 3.22.2)
55  # It seems only pip-installed builds are affected so we test to see if we are affected
56  cmake_path(GET ZEPHYR_BASE PARENT_PATH test_cmake_path)
57  if(ZEPHYR_BASE STREQUAL test_cmake_path)
58    message(FATAL_ERROR "The CMake version ${CMAKE_VERSION} installed suffers"
59            " the \n 'cmake_path(... PARENT_PATH)' bug, see: \n"
60	    "https://gitlab.kitware.com/cmake/cmake/-/issues/23187\n"
61	    "https://github.com/scikit-build/cmake-python-distributions/issues/221\n"
62	    "Please install another CMake version or use a build of CMake that"
63	    " does not come from PyPI."
64    )
65  endif()
66endif()
67
68# Prepare user cache
69list(APPEND zephyr_cmake_modules python)
70list(APPEND zephyr_cmake_modules user_cache)
71
72# Load Zephyr extensions
73list(APPEND zephyr_cmake_modules extensions)
74list(APPEND zephyr_cmake_modules version)
75
76# Load basic settings
77list(APPEND zephyr_cmake_modules basic_settings)
78
79#
80# Find tools
81#
82
83list(APPEND zephyr_cmake_modules west)
84list(APPEND zephyr_cmake_modules ccache)
85list(APPEND zephyr_cmake_modules yaml)
86
87# Load default root settings
88list(APPEND zephyr_cmake_modules root)
89
90#
91# Find Zephyr modules.
92# Those may contain additional DTS, BOARD, SOC, ARCH ROOTs.
93# Also create the Kconfig binary dir for generated Kconf files.
94#
95list(APPEND zephyr_cmake_modules zephyr_module)
96
97list(APPEND zephyr_cmake_modules boards)
98list(APPEND zephyr_cmake_modules shields)
99list(APPEND zephyr_cmake_modules snippets)
100list(APPEND zephyr_cmake_modules arch_v1)
101list(APPEND zephyr_cmake_modules hwm_v2)
102list(APPEND zephyr_cmake_modules configuration_files)
103list(APPEND zephyr_cmake_modules generated_file_directories)
104
105# Include board specific device-tree flags before parsing.
106set(pre_dt_board "\${BOARD_DIR}/pre_dt_board.cmake" OPTIONAL)
107list(APPEND zephyr_cmake_modules "\${pre_dt_board}")
108
109# DTS should be close to kconfig because CONFIG_ variables from
110# kconfig and dts should be available at the same time.
111list(APPEND zephyr_cmake_modules dts)
112list(APPEND zephyr_cmake_modules kconfig)
113list(APPEND zephyr_cmake_modules arch_v2)
114list(APPEND zephyr_cmake_modules soc_v1)
115list(APPEND zephyr_cmake_modules soc_v2)
116
117foreach(component ${SUB_COMPONENTS})
118  if(NOT ${component} IN_LIST zephyr_cmake_modules)
119    message(FATAL_ERROR
120      "Subcomponent '${component}' not default module for Zephyr CMake build system.\n"
121      "Please choose one or more valid components: ${zephyr_cmake_modules}"
122    )
123  endif()
124endforeach()
125
126foreach(module IN LISTS zephyr_cmake_modules)
127  # Ensures any module of type `${module}` are properly expanded to list before
128  # passed on the `include(${module})`.
129  # This is done twice to support cases where the content of `${module}` itself
130  # contains a variable, like `${BOARD_DIR}`.
131  string(CONFIGURE "${module}" module)
132  string(CONFIGURE "${module}" module)
133  include(${module})
134
135  list(REMOVE_ITEM SUB_COMPONENTS ${module})
136  if(DEFINED SUB_COMPONENTS AND NOT SUB_COMPONENTS)
137    # All requested Zephyr CMake modules have been loaded, so let's return.
138    return()
139  endif()
140endforeach()
141
142include(kernel)
143