I am working on a project, and in the compilation system I need to read each line of the CONFIG file to generate a header file config.h.
When using file(STRINGS) in CMake to obtain the string content of each line, when the string contains],[,;, the obtained content will not be as expected.
a part of CONFIG file:
CONFIG_FEAT_A=y
CONFIG_FEAT_B=y
CONFIG_SOME_CHARS=" ,.;:-_)]}"
CONFIG_SIZE=0
a part of CMakeList.txt:
file(STRINGS ${CMAKE_BINARY_DIR}/.config ConfigContents)
foreach(NameAndValue ${ConfigContents})
string(REGEX REPLACE "^[ ]+" "" NameAndValue ${NameAndValue})
string(REGEX MATCH "^CONFIG[^=]+" NAME ${NameAndValue})
string(REPLACE "${NAME}=" "" VALUE ${NameAndValue})
if(NAME AND NOT "${VALUE}" STREQUAL "")
if(${VALUE} STREQUAL "y")
file(APPEND ${CONFIG_H} "#define ${NAME} 1\n")
elseif(${VALUE} STREQUAL "m")
file(APPEND ${CONFIG_H} "#define ${NAME} 2\n")
elseif(${VALUE} STREQUAL "n")
file(APPEND ${CONFIG_H} "#undef ${NAME}\n")
else()
if(NOT "${VALUE}" STREQUAL "")
file(APPEND ${CONFIG_H} "#define ${NAME} ${VALUE}\n")
endif()
endif()
endif()
endforeach()
The first two lines can be parsed correctly
#define CONFIG_FEAT_A 1
#define CONFIG_FEAT_B 1
But the parsing of the third line is very strange. First, it discards the ; character and merges it with the fourth line.
#define CONFIG_SOME_CHARS " ,.:-_)]}";CONFIG_SIZE=0
What is the problem and how can I get the correct result? Many thanks
I tried removing the ] in the configuration ,.;:-_)]} and that seemed to get the line breaks correct. But it will still be lost ;