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 hwm_v2)
101list(APPEND zephyr_cmake_modules configuration_files)
102list(APPEND zephyr_cmake_modules generated_file_directories)
103
104# Include board specific device-tree flags before parsing.
105set(pre_dt_board "\${BOARD_DIR}/pre_dt_board.cmake" OPTIONAL)
106list(APPEND zephyr_cmake_modules "\${pre_dt_board}")
107
108# DTS should be close to kconfig because CONFIG_ variables from
109# kconfig and dts should be available at the same time.
110list(APPEND zephyr_cmake_modules dts)
111list(APPEND zephyr_cmake_modules kconfig)
112list(APPEND zephyr_cmake_modules arch)
113list(APPEND zephyr_cmake_modules soc)
114
115foreach(component ${SUB_COMPONENTS})
116  if(NOT ${component} IN_LIST zephyr_cmake_modules)
117    message(FATAL_ERROR
118      "Subcomponent '${component}' not default module for Zephyr CMake build system.\n"
119      "Please choose one or more valid components: ${zephyr_cmake_modules}"
120    )
121  endif()
122endforeach()
123
124foreach(module IN LISTS zephyr_cmake_modules)
125  # Ensures any module of type `${module}` are properly expanded to list before
126  # passed on the `include(${module})`.
127  # This is done twice to support cases where the content of `${module}` itself
128  # contains a variable, like `${BOARD_DIR}`.
129  string(CONFIGURE "${module}" module)
130  string(CONFIGURE "${module}" module)
131  include(${module})
132
133  list(REMOVE_ITEM SUB_COMPONENTS ${module})
134  if(DEFINED SUB_COMPONENTS AND NOT SUB_COMPONENTS)
135    # All requested Zephyr CMake modules have been loaded, so let's return.
136    return()
137  endif()
138endforeach()
139
140include(kernel)
141