Escape characters not handled correctly in COMPILE_DEFINITIONS
Hi,
I've known about this issue for a long time, and have put workarounds in place for it a while ago, but am now rewriting some of the code in which I had those workarounds. And had to realise that it would be just so much easier if this shortcoming would not be there in the first place...
In our builds we pass a string value to our compiled code from the command line. With the following formalism:
-DPACKAGE_VERSION=\"Foo-01-02-03\"
Note that the quotes have to be escaped like this, otherwise the compiler will receive the value of PACKAGE_VERSION
as just Foo-01-02-03
, and it would not be treated as a string in C(++) code anymore.
But so far, so good. The following simple CMakeLists.txt
file does pass the argument in the expected way to the compiler:
# Set up the demonstrator project.
cmake_minimum_required( VERSION 3.0 )
project( QuoteErrorDemonstration )
# Set up a library with a custom definition.
add_library( TestLibrary test.cxx )
target_compile_definitions( TestLibrary PRIVATE -DPACKAGE_VERSION=\"Foo-01-02-03\" )
I mean, like this:
[bash][Legolas]:build > make VERBOSE=1
...
[ 50%] Building CXX object CMakeFiles/TestLibrary.dir/test.cxx.o
/usr/bin/c++ -DPACKAGE_VERSION=\"Foo-01-02-03\" -o CMakeFiles/TestLibrary.dir/test.cxx.o -c /home/krasznaa/development/cmake/quotes/test.cxx
...
My problem is that we also need to run some custom commands in which we need to use the same arguments that our compiler uses. (It's a long story why we need that...) But when I ask for all the definitions set for TestLibrary
(in this example), the escape character is swallowed by CMake.
# Create a text file with the compiler definitions of the
# library.
file( GENERATE OUTPUT "${CMAKE_BINARY_DIR}/definitions.txt"
CONTENT "-D$<JOIN:$<TARGET_PROPERTY:TestLibrary,COMPILE_DEFINITIONS>, -D>" )
That file is generated like this:
[bash][Legolas]:build > cmake ../quotes/
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/krasznaa/development/cmake/build
[bash][Legolas]:build > more definitions.txt
-DPACKAGE_VERSION="Foo-01-02-03"
[bash][Legolas]:build >
The escape characters are no longer there.
As I started, we can of course work around this limitation in our project. But it would make our code a whole lot cleaner if we didn't have to...
Cheers, Attila