Skip to content
Merged
1 change: 1 addition & 0 deletions cmake/modules/boards.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,4 @@ build_info(board name VALUE ${BOARD})
string(REGEX REPLACE "^/" "" qualifiers "${BOARD_QUALIFIERS}")
build_info(board qualifiers VALUE ${qualifiers})
build_info(board revision VALUE ${BOARD_REVISION})
build_info(board path PATH ${BOARD_DIRECTORIES})
21 changes: 20 additions & 1 deletion cmake/modules/extensions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3060,6 +3060,16 @@ endfunction()
# This function extends the CMake string function by providing additional
# manipulation arguments to CMake string.
#
# ESCAPE: Ensure that any single '\', except '\"', in the input string is
# escaped with the escape char '\'. For example the string 'foo\bar'
# will be escaped so that it becomes 'foo\\bar'.
# Backslashes which are already escaped will not be escaped further,
# for example 'foo\\bar' will not be modified.
# This is useful for handling of windows path separator in strings or
# when strings contains newline escapes such as '\n' and this can
# cause issues when writing to a file where a '\n' is desired in the
# string instead of a newline.
#
# SANITIZE: Ensure that the output string does not contain any special
# characters. Special characters, such as -, +, =, $, etc. are
# converted to underscores '_'.
Expand All @@ -3071,9 +3081,11 @@ endfunction()
#
# returns the updated string
function(zephyr_string)
set(options SANITIZE TOUPPER)
set(options SANITIZE TOUPPER ESCAPE)
cmake_parse_arguments(ZEPHYR_STRING "${options}" "" "" ${ARGN})

zephyr_check_flags_exclusive(${CMAKE_CURRENT_FUNCTION} ZEPHYR_STRING SANITIZE ESCAPE)

if (NOT ZEPHYR_STRING_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Function zephyr_string() called without a return variable")
endif()
Expand All @@ -3091,6 +3103,13 @@ function(zephyr_string)
string(TOUPPER ${work_string} work_string)
endif()

if(ZEPHYR_STRING_ESCAPE)
# If a single '\' is discovered, such as 'foo\bar', then it must be escaped like: 'foo\\bar'
# \\1 and \\2 are keeping the match patterns, the \\\\ --> \\ meaning an escaped '\',
# which then becomes a single '\' in the final string.
string(REGEX REPLACE "([^\\][\\])([^\\\"])" "\\1\\\\\\2" work_string "${ZEPHYR_STRING_UNPARSED_ARGUMENTS}")
endif()

set(${return_arg} ${work_string} PARENT_SCOPE)
endfunction()

Expand Down
Loading