[cmake-developers] escape double quote in generated file
Hello all, I'm working on a cross platform project. On Ubuntu I would like to save all the compiler options, definitions and other complier related stuff to a generated file to use it later for precompiled header generation. My issue is that I have to specify a macro that contain double quotes for g++ compiler visibility attribute. When I generate a file with double quotes they are not escaped. I also tried to use `string(replace "\"" "\\\""...)` without effect. I've made simple two files project of a shared library to show the issue. It has only target compile definitions for simplicity. - CMakeLists.txt -- cmake_minimum_required(VERSION 3.14) set(target test) project(${target}) if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") set(API_IMPORT_MACRO "__attribute__((visibility(\"default\")))") set(API_EXPORT_MACRO "__attribute__((visibility(\"default\")))") else() set(API_IMPORT_MACRO "__declspec(dllimport)") set(API_EXPORT_MACRO "__declspec(dllexport)") endif() function(export_all_flags _target _filename) set(_compile_definitions "$") set(_compile_definitions "$<$:-D$\n>") file(GENERATE OUTPUT "${_filename}" CONTENT "${_compile_definitions}") endfunction() add_library(${target} SHARED test.cpp) target_compile_definitions(${target} PRIVATE API=${API_EXPORT_MACRO} INTERFACE API=${API_IMPORT_MACRO}) export_all_flags(${target} ${CMAKE_BINARY_DIR}/flags.txt) - CMakeLists.txt -- - test.cpp -- void API test() {} - test.cpp -- The result file "flags.txt" is following: -DAPI=__attribute__((visibility("default"))) I would like any solution to make the result as: -DAPI=__attribute__((visibility(\"default\"))) Could you help me please? Eugene. -- Powered by www.kitware.com Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Kitware offers various services to support the CMake community. For more information on each offering, please visit: CMake Support: http://cmake.org/cmake/help/support.html CMake Consulting: http://cmake.org/cmake/help/consulting.html CMake Training Courses: http://cmake.org/cmake/help/training.html Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Follow this link to subscribe/unsubscribe: https://cmake.org/mailman/listinfo/cmake-developers
Re: [cmake-developers] escape double quote in generated file
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 : > 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 > "$") > set(_compile_definitions "$<$:- > 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 > -- Powered by www.kitware.com Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Kitware offers various services to support the CMake community. For more information on each offering, please visit: CMake Support: http://cmake.org/cmake/help/support.html CMake Consulting: http://cmake.org/cmake/help/consulting.html CMake Training Courses: http://cmake.org/cmake/help/training.html Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Follow this link to subscribe/unsubscribe: https://cmake.org/mailman/listinfo/cmake-developers
Re: [cmake-developers] escape double quote in generated file
I've tried this. But then it fails to compile due to `INTERFACE API=${API_EXPORT_MACRO}` target compile definition. пт, 30 авг. 2019 г. в 17:49, Kyle Edwards : > On Fri, 2019-08-30 at 17:36 +0300, Eugene Karpov wrote: > > Hello all, > > > > I'm working on a cross platform project. On Ubuntu I would like to > > save all the compiler options, definitions and other complier related > > stuff to a generated file to use it later for precompiled header > > generation. > > My issue is that I have to specify a macro that contain double quotes > > for g++ compiler visibility attribute. When I generate a file with > > double quotes they are not escaped. I also tried to use > > `string(replace "\"" "\\\""...)` without effect. > > I've made simple two files project of a shared library to show the > > issue. It has only target compile definitions for simplicity. > > > > - CMakeLists.txt -- > > cmake_minimum_required(VERSION 3.14) > > set(target test) > > project(${target}) > > if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") > > set(API_IMPORT_MACRO "__attribute__((visibility(\"default\")))") > > set(API_EXPORT_MACRO "__attribute__((visibility(\"default\")))") > > The quotes here need to be double-escaped, like so: > > set(API_IMPORT_MACRO "__attribute__((visibility(\\\"default\\\")))") > set(API_EXPORT_MACRO "__attribute__((visibility(\\\"default\\\")))") > > Kyle > -- Powered by www.kitware.com Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Kitware offers various services to support the CMake community. For more information on each offering, please visit: CMake Support: http://cmake.org/cmake/help/support.html CMake Consulting: http://cmake.org/cmake/help/consulting.html CMake Training Courses: http://cmake.org/cmake/help/training.html Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Follow this link to subscribe/unsubscribe: https://cmake.org/mailman/listinfo/cmake-developers
Re: [cmake-developers] escape double quote in generated file
Yes, verbatim works to escape double quotes, but it doesn't work for join by '\n' in the generated make file. So I switched to join by space. Thank you. пт, 30 авг. 2019 г. в 20:24, Kyle Edwards : > On Fri, 2019-08-30 at 19:01 +0300, Eugene Karpov wrote: > > 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. > > Please try adding the VERBATIM option to add_custom_target(): > > https://cmake.org/cmake/help/latest/command/add_custom_target.html > > Kyle > -- Powered by www.kitware.com Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Kitware offers various services to support the CMake community. For more information on each offering, please visit: CMake Support: http://cmake.org/cmake/help/support.html CMake Consulting: http://cmake.org/cmake/help/consulting.html CMake Training Courses: http://cmake.org/cmake/help/training.html Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Follow this link to subscribe/unsubscribe: https://cmake.org/mailman/listinfo/cmake-developers