Hi,
I wanted to automate the version-string generation from a git tag via CMake and came up with an approach very similar to the following.
main.c:
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(TestingCmake LANGUAGES C)
add_executable(TestingCmake main.c)
set(FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/test_file.c)
add_custom_command(
OUTPUT ${FILE_NAME}
COMMAND ${CMAKE_COMMAND}
-DSTRING_CONTENT="text with whitespace"
-DFILE_PATH="${FILE_NAME}"
-P ${CMAKE_CURRENT_SOURCE_DIR}/CreateVersion.cmake
)
add_custom_target(CreateClControllerVersionFile ALL
COMMAND ${CMAKE_COMMAND}
-DSTRING_CONTENT="text with whitespace"
-DFILE_PATH="${FILE_NAME}"
-P ${CMAKE_CURRENT_SOURCE_DIR}/CreateVersion.cmake
)
add_dependencies(TestingCmake CreateClControllerVersionFile)
CreateVersion.cmake:
file(WRITE ${FILE_PATH} "${STRING_CONTENT}")
Doing so I encountered [to me] seemingly strange behavior:
- under Windows [CMake 3.23.2] I got the expected result in the output file
text with whitespace
- under Linux [CMake 3.21.3] I encountered
text\ with\ whitespace
Is this expected behavior?
Is there a way for me to tell CMake to unescape the whitespaces during writing to file?
Should I approach my goal in a totally different way?
Thankful for any help
Johannes