1# SPDX-License-Identifier: BSD-3-Clause
2
3include(${CMAKE_CURRENT_LIST_DIR}/defconfigs.cmake)
4
5
6           ### configure-time .config ###
7
8
9if(NOT INIT_CONFIG_found)
10# Brand new build directory, search for initial configuration
11
12# Default value when no -DINIT_CONFIG on the command line
13set(INIT_CONFIG "initial.config" CACHE STRING "Initial .config file")
14
15# - ".' is the top source directory.
16# - "src/arch/${arch}/configs" is for convenience and compatibility with
17#   defconfigs.cmake.
18# - First found wins.
19# - If two archs ever use the same platform_defconfig name then a full
20#   path must be used, e.g.: -DINIT_CONFIG=src/arch/myarch/collision_defconfig
21
22set(init_config_search_list ".")
23foreach(arch "xtensa" "host")
24list(APPEND init_config_search_list "src/arch/${arch}/configs")
25endforeach()
26
27find_file(INIT_CONFIG_found
28	NAMES ${INIT_CONFIG}
29	NO_CMAKE_FIND_ROOT_PATH
30	NO_DEFAULT_PATH
31	PATHS ${init_config_search_list}
32)
33
34else()  # old build directory
35
36if (INIT_CONFIG)
37message(WARNING
38	"IGNORING '-DINIT_CONFIG=${INIT_CONFIG}!!' "
39	"Using up-to-date ${INIT_CONFIG_found} instead."
40)
41endif()
42
43endif() # new/old build directory
44
45
46if(NOT INIT_CONFIG_found)
47message(FATAL_ERROR
48	"Initial configuration missing, no ${INIT_CONFIG} found. "
49	"Provide a ${PROJECT_SOURCE_DIR}/initial.config file or specify some "
50	"other -DINIT_CONFIG=location relative to '${PROJECT_SOURCE_DIR}/' or "
51	"'${PROJECT_SOURCE_DIR}/src/arch/*/configs/'"
52)
53endif()
54
55# Did someone or something remove our generated/.config?
56if(NOT EXISTS ${INIT_CONFIG_found})
57message(FATAL_ERROR "The file ${INIT_CONFIG_found} vanished!")
58endif()
59
60# Don't confuse this configure-time, .config generation with
61# the build-time, autoconfig.h genconfig target below
62message(STATUS
63	"(Re-)generating ${DOT_CONFIG_PATH}\n"
64	"   and ${CONFIG_H_PATH}\n"
65	"   from ${INIT_CONFIG_found}"
66)
67execute_process(
68	COMMAND ${CMAKE_COMMAND} -E env
69		KCONFIG_CONFIG=${INIT_CONFIG_found}
70		srctree=${PROJECT_SOURCE_DIR}
71		CC_VERSION_TEXT=${CC_VERSION_TEXT}
72		ARCH=${ARCH}
73		${PYTHON3} ${PROJECT_SOURCE_DIR}/scripts/kconfig/genconfig.py
74		--config-out=${DOT_CONFIG_PATH}
75		--header-path ${CONFIG_H_PATH}
76		${PROJECT_SOURCE_DIR}/Kconfig
77	WORKING_DIRECTORY ${GENERATED_DIRECTORY}
78	# Available only from CMake 3.18. Amazingly not the default.
79	# COMMAND_ERROR_IS_FATAL ANY
80	RESULT_VARIABLE _genret
81)
82if(${_genret})
83message(FATAL_ERROR
84  "genconfig.py from ${INIT_CONFIG_found} to ${DOT_CONFIG_PATH} failed")
85endif()
86
87if(NOT ${INIT_CONFIG_found} STREQUAL ${DOT_CONFIG_PATH})
88# Brand new build directory and config.
89message(STATUS
90	"Done, future changes to ${INIT_CONFIG_found}\n"
91	"   will be IGNORED by this build directory! The primary .config\n"
92	"   file is now 'generated/.config' in the build directory."
93)
94endif()
95# Now force CMake to forget about the initial config and to re-use our
96# own private ${DOT_CONFIG_PATH} when it decides it must re-run itself.
97unset(INIT_CONFIG CACHE)
98set(INIT_CONFIG_found ${DOT_CONFIG_PATH} CACHE FILEPATH "active .config" FORCE)
99
100
101           ###  build-time Kconfig targets ###
102
103add_custom_target(
104	menuconfig
105	COMMAND ${CMAKE_COMMAND} -E env
106		srctree=${PROJECT_SOURCE_DIR}
107		CC_VERSION_TEXT=${CC_VERSION_TEXT}
108		ARCH=${ARCH}
109		${PYTHON3} ${PROJECT_SOURCE_DIR}/scripts/kconfig/menuconfig.py
110		${PROJECT_SOURCE_DIR}/Kconfig
111	WORKING_DIRECTORY ${GENERATED_DIRECTORY}
112	VERBATIM
113	USES_TERMINAL
114)
115
116add_custom_target(
117	overrideconfig
118	COMMAND ${CMAKE_COMMAND} -E env
119		srctree=${PROJECT_SOURCE_DIR}
120		CC_VERSION_TEXT=${CC_VERSION_TEXT}
121		ARCH=${ARCH}
122		${PYTHON3} ${PROJECT_SOURCE_DIR}/scripts/kconfig/overrideconfig.py
123		${PROJECT_SOURCE_DIR}/Kconfig
124		${PROJECT_BINARY_DIR}/override.config
125	WORKING_DIRECTORY ${GENERATED_DIRECTORY}
126	VERBATIM
127	USES_TERMINAL
128)
129
130file(GLOB_RECURSE KCONFIG_FILES "${SOF_ROOT_SOURCE_DIRECTORY}/Kconfig")
131
132
133# Don't confuse this build-time, .h target with the
134#  configure-time, .config genconfig above.
135add_custom_command(
136	OUTPUT ${CONFIG_H_PATH}
137	COMMAND ${CMAKE_COMMAND} -E env
138		srctree=${PROJECT_SOURCE_DIR}
139		CC_VERSION_TEXT=${CC_VERSION_TEXT}
140		ARCH=${ARCH}
141		${PYTHON3} ${PROJECT_SOURCE_DIR}/scripts/kconfig/genconfig.py
142		--header-path ${CONFIG_H_PATH}
143		${PROJECT_SOURCE_DIR}/Kconfig
144	DEPENDS ${DOT_CONFIG_PATH}
145	WORKING_DIRECTORY ${GENERATED_DIRECTORY}
146	COMMENT "Generating ${CONFIG_H_PATH}"
147	VERBATIM
148	USES_TERMINAL
149)
150
151add_custom_target(genconfig DEPENDS ${CONFIG_H_PATH})
152
153add_custom_target(
154	olddefconfig
155	COMMAND ${CMAKE_COMMAND} -E env
156		srctree=${PROJECT_SOURCE_DIR}
157		CC_VERSION_TEXT=${CC_VERSION_TEXT}
158		ARCH=${ARCH}
159		${PYTHON3} ${PROJECT_SOURCE_DIR}/scripts/kconfig/olddefconfig.py
160		${PROJECT_SOURCE_DIR}/Kconfig
161	WORKING_DIRECTORY ${GENERATED_DIRECTORY}
162	VERBATIM
163	USES_TERMINAL
164)
165
166add_custom_target(
167	alldefconfig
168	COMMAND ${CMAKE_COMMAND} -E env
169		srctree=${PROJECT_SOURCE_DIR}
170		CC_VERSION_TEXT=${CC_VERSION_TEXT}
171		ARCH=${ARCH}
172		${PYTHON3} ${PROJECT_SOURCE_DIR}/scripts/kconfig/alldefconfig.py
173		${PROJECT_SOURCE_DIR}/Kconfig
174	WORKING_DIRECTORY ${GENERATED_DIRECTORY}
175	VERBATIM
176	USES_TERMINAL
177)
178
179add_custom_target(
180	savedefconfig
181	COMMAND ${CMAKE_COMMAND} -E env
182		srctree=${PROJECT_SOURCE_DIR}
183		CC_VERSION_TEXT=${CC_VERSION_TEXT}
184		ARCH=${ARCH}
185		${PYTHON3} ${PROJECT_SOURCE_DIR}/scripts/kconfig/savedefconfig.py
186		${PROJECT_SOURCE_DIR}/Kconfig
187		${PROJECT_BINARY_DIR}/defconfig
188	WORKING_DIRECTORY ${GENERATED_DIRECTORY}
189	COMMENT "Saving minimal configuration to: ${PROJECT_BINARY_DIR}/defconfig"
190	VERBATIM
191	USES_TERMINAL
192)
193