Skip to content

Commit 0d12e1b

Browse files
57300carlescufi
authored andcommitted
cmake: kconfig: Keep symbol names sorted in EXTRA_KCONFIG_OPTIONS
This makes checksum calculation over Kconfig fragments more consistent, which prevents writing a new `.config` when nothing really changes. To explain this, consider the following sequence: 1. west build . -DCONFIG_XXXX=y -DCONFIG_YYYY=y 2. west build . -DCONFIG_YYYY=y 3. west build . -DCONFIG_XXXX=y At (1), we set new values for XXXX and YYYY, so the `.config` changes. At (2), we set a value for YYYY, but it's the same value as before, so the `.config` doesn't get overwritten. At (3), we set a value for XXXX, but it's the same value as before, so the `.config` shouldn't get overwritten... but it does. What happened? The reason is that the generated `extra_kconfig_options.conf` fragment, which is included in the checksum calculation, was being populated using two sets of CMake cache variables: - past assignments, prefixed with `CLI_${KCONFIG_NAMESPACE}_`. - new assignments, prefixed with just `${KCONFIG_NAMESPACE}_`. Usually, past assignments would appear before new assignments, because the default `${KCONFIG_NAMESPACE}` is CONFIG, which goes after CLI in alphabetical order. As a result, the contents of EXTRA_KCONFIG_OPTIONS at (1) and (2): CONFIG_XXXX=y CONFIG_YYYY=y were not identical to its contents at (3): CONFIG_YYYY=y CONFIG_XXXX=y resulting in a different checksum. This is resolved by stripping out the CLI prefix first, then effectively "mergesorting" the past and new assignments, before starting to populate EXTRA_KCONFIG_OPTIONS. Signed-off-by: Grzegorz Swiderski <[email protected]>
1 parent 5fc2586 commit 0d12e1b

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

cmake/modules/kconfig.cmake

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,26 +210,29 @@ if(SYSBUILD)
210210
endforeach()
211211
else()
212212
get_cmake_property(cache_variable_names CACHE_VARIABLES)
213+
list(FILTER cache_variable_names INCLUDE REGEX "^(CLI_)?${KCONFIG_NAMESPACE}_")
214+
list(TRANSFORM cache_variable_names REPLACE "^CLI_" "")
215+
list(REMOVE_DUPLICATES cache_variable_names)
213216
endif()
214217

218+
# Sorting the variable names will make checksum calculation more stable.
219+
list(SORT cache_variable_names)
215220
foreach (name ${cache_variable_names})
216-
if("${name}" MATCHES "^CLI_${KCONFIG_NAMESPACE}_")
217-
# Variable was set by user in earlier invocation, let's append to extra
218-
# config unless a new value has been given.
219-
string(REGEX REPLACE "^CLI_" "" org_name ${name})
220-
if(NOT DEFINED ${org_name})
221-
set(EXTRA_KCONFIG_OPTIONS
222-
"${EXTRA_KCONFIG_OPTIONS}\n${org_name}=${${name}}"
223-
)
224-
endif()
225-
elseif("${name}" MATCHES "^${KCONFIG_NAMESPACE}_")
221+
if(DEFINED ${name})
226222
# When a cache variable starts with the 'KCONFIG_NAMESPACE' value, it is
227223
# assumed to be a Kconfig symbol assignment from the CMake command line.
228224
set(EXTRA_KCONFIG_OPTIONS
229225
"${EXTRA_KCONFIG_OPTIONS}\n${name}=${${name}}"
230226
)
231227
set(CLI_${name} "${${name}}")
232228
list(APPEND cli_config_list ${name})
229+
elseif(DEFINED CLI_${name})
230+
# An additional 'CLI_' prefix means that the value was set by the user in
231+
# an earlier invocation. Append it to extra config only if no new value was
232+
# assigned above.
233+
set(EXTRA_KCONFIG_OPTIONS
234+
"${EXTRA_KCONFIG_OPTIONS}\n${name}=${CLI_${name}}"
235+
)
233236
endif()
234237
endforeach()
235238

0 commit comments

Comments
 (0)