Skip to content

Commit 9228c37

Browse files
57300carlescufi
authored andcommitted
cmake: extensions: Improve import_kconfig() bool/tristate parsing
This function has been working fine for parsing Kconfig fragments as processed by Kconfiglib, but it can be made more robust for parsing fragments from other sources, including handwritten ones. Previously, Kconfig fragment lines of the form `# CONFIG_FOO is not set` were ignored entirely, while lines of the form `CONFIG_FOO=n` would set the corresponding CMake variable or target property to a literal `n`. However, Kconfiglib treats both equivalently - as assignments to `n` - so `import_kconfig()` should too. Due to the fact that `.config` files output by Kconfiglib always show disabled options as `# CONFIG_FOO is not set` (which was being ignored), existing CMake code expects `CONFIG_FOO` to be unset. To avoid breakage, the variable/property will now be unset explicitly by `import_kconfig()` when it encounters either representation of `CONFIG_FOO=n`. Moreover, for bool and tristate symbols, Kconfiglib accepts assignments like `CONFIG_FOO=yeah`, `CONFIG_FOO=maybe`, or `CONFIG_FOO=nope`, and it transforms the value of `CONFIG_FOO` into `y`, `m`, or `n` respectively. This effect is now replicated in CMake. Signed-off-by: Grzegorz Swiderski <[email protected]>
1 parent 0d12e1b commit 9228c37

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

cmake/modules/extensions.cmake

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,40 +1552,62 @@ endfunction()
15521552
# CMake namespace.
15531553
function(import_kconfig prefix kconfig_fragment)
15541554
cmake_parse_arguments(IMPORT_KCONFIG "" "TARGET" "" ${ARGN})
1555-
# Parse the lines prefixed with 'prefix' in ${kconfig_fragment}
15561555
file(
15571556
STRINGS
15581557
${kconfig_fragment}
15591558
DOT_CONFIG_LIST
1560-
REGEX "^${prefix}"
15611559
ENCODING "UTF-8"
15621560
)
15631561

1564-
foreach (CONFIG ${DOT_CONFIG_LIST})
1565-
# CONFIG could look like: CONFIG_NET_BUF=y
1566-
1567-
# Match the first part, the variable name
1568-
string(REGEX MATCH "[^=]+" CONF_VARIABLE_NAME ${CONFIG})
1562+
foreach (LINE ${DOT_CONFIG_LIST})
1563+
if("${LINE}" MATCHES "^(${prefix}[^=]+)=([ymn]|.+$)")
1564+
# Matched a normal value assignment, like: CONFIG_NET_BUF=y
1565+
# Note: if the value starts with 'y', 'm', or 'n', then we assume it's a
1566+
# bool or tristate (we don't know the type from <kconfig_fragment> alone)
1567+
# and we only match the first character. This is to align with Kconfiglib.
1568+
set(CONF_VARIABLE_NAME "${CMAKE_MATCH_1}")
1569+
set(CONF_VARIABLE_VALUE "${CMAKE_MATCH_2}")
1570+
elseif("${LINE}" MATCHES "^# (${prefix}[^ ]+) is not set")
1571+
# Matched something like: # CONFIG_FOO is not set
1572+
# This is interpreted as: CONFIG_FOO=n
1573+
set(CONF_VARIABLE_NAME "${CMAKE_MATCH_1}")
1574+
set(CONF_VARIABLE_VALUE "n")
1575+
else()
1576+
# Ignore this line.
1577+
# Note: we also ignore assignments which don't have the desired <prefix>.
1578+
continue()
1579+
endif()
15691580

1570-
# Match the second part, variable value
1571-
string(REGEX MATCH "=(.+$)" CONF_VARIABLE_VALUE ${CONFIG})
1572-
# The variable name match we just did included the '=' symbol. To just get the
1573-
# part on the RHS we use match group 1
1574-
set(CONF_VARIABLE_VALUE ${CMAKE_MATCH_1})
1581+
# If the provided value is n, then the corresponding CMake variable or
1582+
# target property will be unset.
1583+
if("${CONF_VARIABLE_VALUE}" STREQUAL "n")
1584+
if(DEFINED IMPORT_KCONFIG_TARGET)
1585+
set_property(TARGET ${IMPORT_KCONFIG_TARGET} PROPERTY "${CONF_VARIABLE_NAME}")
1586+
else()
1587+
unset("${CONF_VARIABLE_NAME}" PARENT_SCOPE)
1588+
endif()
1589+
list(REMOVE_ITEM keys "${CONF_VARIABLE_NAME}")
1590+
continue()
1591+
endif()
15751592

1576-
if("${CONF_VARIABLE_VALUE}" MATCHES "^\"(.*)\"$") # Is surrounded by quotes
1593+
# Otherwise, the variable/property will be set to the provided value.
1594+
# For string values, we also remove the surrounding quotation marks.
1595+
if("${CONF_VARIABLE_VALUE}" MATCHES "^\"(.*)\"$")
15771596
set(CONF_VARIABLE_VALUE ${CMAKE_MATCH_1})
15781597
endif()
15791598

15801599
if(DEFINED IMPORT_KCONFIG_TARGET)
1581-
set_property(TARGET ${IMPORT_KCONFIG_TARGET} APPEND PROPERTY "kconfigs" "${CONF_VARIABLE_NAME}")
15821600
set_property(TARGET ${IMPORT_KCONFIG_TARGET} PROPERTY "${CONF_VARIABLE_NAME}" "${CONF_VARIABLE_VALUE}")
15831601
else()
15841602
set("${CONF_VARIABLE_NAME}" "${CONF_VARIABLE_VALUE}" PARENT_SCOPE)
15851603
endif()
15861604
list(APPEND keys "${CONF_VARIABLE_NAME}")
15871605
endforeach()
15881606

1607+
if(DEFINED IMPORT_KCONFIG_TARGET)
1608+
set_property(TARGET ${IMPORT_KCONFIG_TARGET} PROPERTY "kconfigs" "${keys}")
1609+
endif()
1610+
15891611
list(LENGTH IMPORT_KCONFIG_UNPARSED_ARGUMENTS unparsed_length)
15901612
if(unparsed_length GREATER 0)
15911613
if(unparsed_length GREATER 1)

0 commit comments

Comments
 (0)