cmake:bugfix generate config.h contain ; characters will be handled incorrectly

`;` is treated as a list separator in CMake.
the file(STRING) function will read the wrong .config value
string(REGEX REPLACE) will also incorrectly handle lines containing `;`
so here we can only parse character by character.

Signed-off-by: xuxin19 <xuxin19@xiaomi.com>
This commit is contained in:
xuxin19 2024-04-29 19:18:02 +08:00 committed by Alan Carvalho de Assis
parent 8f23a2db01
commit 94d8fba629
2 changed files with 30 additions and 1 deletions

View File

@ -18,14 +18,36 @@
#
# ##############################################################################
macro(encode_brackets contents)
string(REGEX REPLACE "\\[" "__OPEN_BRACKET__" ${contents} "${${contents}}")
string(REGEX REPLACE "\\]" "__CLOSE_BRACKET__" ${contents} "${${contents}}")
endmacro()
macro(decode_brackets contents)
string(REGEX REPLACE "__OPEN_BRACKET__" "[" ${contents} "${${contents}}")
string(REGEX REPLACE "__CLOSE_BRACKET__" "]" ${contents} "${${contents}}")
endmacro()
macro(encode_semicolon contents)
string(REGEX REPLACE ";" "__SEMICOLON__" ${contents} "${${contents}}")
endmacro()
macro(decode_semicolon contents)
string(REGEX REPLACE "__SEMICOLON__" ";" ${contents} "${${contents}}")
endmacro()
function(nuttx_export_kconfig_by_value kconfigfile config)
file(STRINGS ${kconfigfile} ConfigContents)
encode_brackets(ConfigContents)
foreach(NameAndValue ${ConfigContents})
decode_brackets(NameAndValue)
encode_semicolon(NameAndValue)
string(REGEX REPLACE "^[ ]+" "" NameAndValue ${NameAndValue})
string(REGEX MATCH "^CONFIG[^=]+" Name ${NameAndValue})
if(Name STREQUAL ${config})
string(REPLACE "${Name}=" "" Value ${NameAndValue})
string(REPLACE "\"" "" Value ${Value})
decode_semicolon(Value)
set(${Name}
${Value}
PARENT_SCOPE)
@ -41,7 +63,10 @@ function(nuttx_export_kconfig kconfigfile)
set(${key} PARENT_SCOPE)
endforeach()
file(STRINGS ${kconfigfile} ConfigContents)
encode_brackets(ConfigContents)
foreach(NameAndValue ${ConfigContents})
decode_brackets(NameAndValue)
encode_semicolon(NameAndValue)
# Strip leading spaces
string(REGEX REPLACE "^[ ]+" "" NameAndValue ${NameAndValue})
@ -54,7 +79,7 @@ function(nuttx_export_kconfig kconfigfile)
# remove extra quotes
string(REPLACE "\"" "" Value ${Value})
decode_semicolon(Value)
# Set the variable
set(${Name}
${Value}

View File

@ -92,7 +92,10 @@ file(APPEND ${CONFIG_H}
file(APPEND ${CONFIG_H} "#define CONFIG_BASE_DEFCONFIG \"${BASE_DEFCONFIG}\"\n")
file(STRINGS ${CMAKE_BINARY_DIR}/.config ConfigContents)
encode_brackets(ConfigContents)
foreach(NameAndValue ${ConfigContents})
decode_brackets(NameAndValue)
encode_semicolon(NameAndValue)
string(REGEX REPLACE "^[ ]+" "" NameAndValue ${NameAndValue})
string(REGEX MATCH "^CONFIG[^=]+" NAME ${NameAndValue})
# skip BASE_DEFCONFIG here as it is handled above
@ -121,6 +124,7 @@ foreach(NameAndValue ${ConfigContents})
endif()
endforeach()
if(NOT "${VALUE}" STREQUAL "")
decode_semicolon(VALUE)
file(APPEND ${CONFIG_H} "#define ${NAME} ${VALUE}\n")
endif()
endif()