Skip to content

Commit

Permalink
CMake: Fix escaping of quotes in response file
Browse files Browse the repository at this point in the history
We put macros in a response file compile_time_defs.txt and pass it
to the compiler. Adding a pair of single quotes around each -D flag
ensures macro values are quoted correctly.

For example,
* String
  * target_compile_definitions(): either FOO="BAR" or FOO=\"BAR\"
  * response file: '-DFOO="BAR"'
  * actual definition: #define FOO "BAR"
* Array of integers
  * target_compile_definitions(): FOO={1, 2, 3}
  * response file: 'FOO={1, 2, 3}'
  * actual definition: #define FOO {1, 2, 3}
  • Loading branch information
LDong-Arm committed Aug 31, 2021
1 parent 539d1c7 commit 9241e86
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions tools/cmake/mbed_toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,9 @@ function(mbed_generate_options_for_linker target output_response_file_path)
"$<TARGET_PROPERTY:${target},INTERFACE_COMPILE_DEFINITIONS>"
)

# Remove macro definitions that contain spaces as the lack of escape sequences and quotation marks
# in the macro when retrieved using generator expressions causes linker errors.
# This includes string macros, array macros, and macros with operations.
# TODO CMake: Add escape sequences and quotation marks where necessary instead of removing these macros.
# Append -D to all macros and quote them as we pass these as response file to cxx compiler
set(_compile_definitions
"$<FILTER:${_compile_definitions},EXCLUDE, +>"
)

# Append -D to all macros as we pass these as response file to cxx compiler
set(_compile_definitions
"$<$<BOOL:${_compile_definitions}>:-D$<JOIN:${_compile_definitions}, -D>>"
"$<$<BOOL:${_compile_definitions}>:'-D$<JOIN:${_compile_definitions},' '-D>'>"
)
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/compile_time_defs.txt" CONTENT "${_compile_definitions}\n")
set(${output_response_file_path} @${CMAKE_CURRENT_BINARY_DIR}/compile_time_defs.txt PARENT_SCOPE)
Expand Down

0 comments on commit 9241e86

Please sign in to comment.