[cmake-developers] escape double quote in generated file

Eugene Karpov karpov.en at gmail.com
Fri Aug 30 10:36:12 EDT 2019


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
"$<TARGET_PROPERTY:${_target},COMPILE_DEFINITIONS>")
  set(_compile_definitions
"$<$<BOOL:${_compile_definitions}>:-D$<JOIN:${_compile_definitions},\n-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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake-developers/attachments/20190830/11381b6f/attachment.html>


More information about the cmake-developers mailing list