[cmake-developers] escape double quote in generated file

Eugene Karpov karpov.en at gmail.com
Fri Aug 30 12:01:11 EDT 2019


Not working too.
The failed lines in a generated make file looks like this
-----------------------
CMakeFiles/mkflags_test:
        /usr/bin/cmake
-DDEFINITIONS=-DAPI=__attribute__((visibility("default")))
-DFILENAME=/home/ekarpov/tmp/build/flags.txt
-P /home/ekarpov/tmp/escape_quotes.cmake
-----------------------

And I've tried to double quote the DEFINITIONS parameter that is passed to
cmake - didn't help.

пт, 30 авг. 2019 г. в 18:15, Kyle Edwards <kyle.edwards at kitware.com>:

> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake-developers/attachments/20190830/bbac5ba7/attachment.html>


More information about the cmake-developers mailing list