user1244932
user1244932

Reputation: 8142

cmake add_custom_target escaping

Here is minimal CMakeLists.txt to reproduce problem:

cmake_minimum_required(VERSION 3.9 FATAL_ERROR)

set(BUILD_INFO_CPP "${CMAKE_BINARY_DIR}/build_info.cpp")
add_custom_target(GenerateBuildInfo ALL
   COMMAND ${CMAKE_COMMAND} -E echo "#include Something" > "${BUILD_INFO_CPP}"
)

On every build I want regenerate ${CMAKE_BINARY_DIR}/build_info.cpp, but this code not create build_info.cpp, because of cmake execute it like this:

/usr/bin/cmake -E echo #include > /tmp/2/build/build_info.cpp

and as understand shell interpret # as start of comments, so this is equivalent to

/usr/bin/cmake -E echo

I wonder is special symbols escaping not implemented in cmake?

I use linux/bash and Makefile generator.

Upvotes: 0

Views: 803

Answers (1)

Arnav Borborah
Arnav Borborah

Reputation: 11787

According to the official documentation for add_custom_target (CMake 3.0):

If VERBATIM is given then all arguments to the commands will be escaped properly for the build tool so that the invoked command receives each argument unchanged. Note that one level of escapes is still used by the CMake language processor before add_custom_target even sees the arguments. Use of VERBATIM is recommended as it enables correct behavior. When VERBATIM is not given the behavior is platform specific because there is no protection of tool-specific special characters.

Thus, add VERBATIM so that the command is properly escaped in add_custom_target.

The same applies for all versions onwards and CMake 2.8 as well.

Upvotes: 1

Related Questions