[cmake-developers] escape double quote in generated file
Kyle Edwards
kyle.edwards at kitware.com
Fri Aug 30 11:15:09 EDT 2019
On Fri, 2019-08-30 at 17:54 +0300, Eugene Karpov wrote:
> I've tried this. But then it fails to compile due to `INTERFACE
> API=${API_EXPORT_MACRO}` target compile definition.
Ah right, you want the file contents to be escaped while the compile
flags are not. My next suggestion was going to be to use a generator
expression that replaces `"` with `\"`, but there does not appear to be
a "string replace" genex. In that case, I would suggest using
add_custom_command()/add_custom_target() to call a cmake -P script
which escapes the quotes and writes the file. For example:
set(_compile_definitions
"$<TARGET_PROPERTY:${_target},COMPILE_DEFINITIONS>")
set(_compile_definitions "$<$<BOOL:${_compile_definitions}>:-
D$<JOIN:${_compile_definitions},\n-D>\n>")
add_custom_target(mkflags_${_target} COMMAND ${CMAKE_COMMAND} "-
DDEFINITIONS=${_compile_definitions}" "-DFILENAME=${_filename}" -P
path/to/script.cmake BYPRODUCTS "${_filename}")
And then the script would look like:
string(REPLACE "\"" "\\\"" DEFINITIONS "${DEFINITIONS}")
file(WRITE "${FILENAME}" "${DEFINITIONS}")
Kyle
More information about the cmake-developers
mailing list