configure_file with two steps with '.tmp' extension creates blank final file
We have a version.hpp.in
file which needs to be processed two times by configure_file
because we have variables like @@PROJECT_NAME@_VERSION_MAJOR@
in them. This works if the first generated file (the temporary one) has a different name, than the final file. If the generated temporary file has just a .tmp
file extension, then the final generated file is a empty (blank file)
- failing example:
version.hpp.in
->version.hpp.tmp
->version.hpp
- working_example:
version.hpp.in
->temporary_version.hpp.tmp
->version.hpp
I've created a CMakeLists.txt
file showing the behavior:
contents of this file:
cmake_minimum_required(VERSION 3.10...${CMAKE_VERSION})
project(cmake_configure_blank_file)
# create a template file for the configure_file calls to use
# this is just for ease of use to have a single CMakeLists.txt file to test this with
file(WRITE "version.hpp.in" "int version = 1;")
# when creating the tmp-file with the same basename as the outputfile (but with '.tmp' extension)
# then the final generated file is empty
configure_file(
"version.hpp.in"
"${CMAKE_BINARY_DIR}/version_same_name_as_tmp.hpp.tmp")
configure_file(
"${CMAKE_BINARY_DIR}/version_same_name_as_tmp.hpp.tmp"
"${CMAKE_BINARY_DIR}/version_same_name_as_tmp.hpp")
file(STRINGS "${CMAKE_BINARY_DIR}/version_same_name_as_tmp.hpp" ver)
message(STATUS "Contents of 'version_same_name_as_tmp.hpp': '${ver}'")
# same blank file with @ONLY
file(WRITE "version.hpp.in" "int version = 1;")
configure_file(
"version.hpp.in"
"${CMAKE_BINARY_DIR}/version_same_name_as_tmp_at_only.hpp.tmp"
@ONLY)
configure_file(
"${CMAKE_BINARY_DIR}/version_same_name_as_tmp_at_only.hpp.tmp"
"${CMAKE_BINARY_DIR}/version_same_name_as_tmp_at_only.hpp"
@ONLY)
file(STRINGS "${CMAKE_BINARY_DIR}/version_same_name_as_tmp_at_only.hpp" ver)
message(STATUS "Contents of 'version_same_name_as_tmp_at_only.hpp': '${ver}'")
# just copying the file twice with the intermediate having a '.tmp' extension works
file(WRITE "version.hpp.in" "int version = 1;")
configure_file(
"version.hpp.in"
"${CMAKE_BINARY_DIR}/version_same_name_as_tmp_copy_only.hpp.tmp"
COPYONLY)
configure_file(
"${CMAKE_BINARY_DIR}/version_same_name_as_tmp_copy_only.hpp.tmp"
"${CMAKE_BINARY_DIR}/version_same_name_as_tmp_copy_only.hpp"
COPYONLY)
file(STRINGS "${CMAKE_BINARY_DIR}/version_same_name_as_tmp_copy_only.hpp" ver)
message(STATUS "Contents of 'version_same_name_as_tmp_copy_only.hpp': '${ver}'")
# when creating the tmp-file with a different basename the double-configure succeeds
configure_file(
"version.hpp.in"
"${CMAKE_BINARY_DIR}/version_different_tmp_filename.hpp.tmp"
@ONLY)
configure_file(
"${CMAKE_BINARY_DIR}/version_different_tmp_filename.hpp.tmp"
"${CMAKE_BINARY_DIR}/version_with_different_tmp_filename.hpp"
@ONLY)
file(STRINGS "${CMAKE_BINARY_DIR}/version_with_different_tmp_filename.hpp" ver)
message(STATUS "Contents of 'version_with_different_tmp_filename.hpp': '${ver}'")
CMake configure output:
[cmake] -- Contents of 'version_same_name_as_tmp.hpp': ''
[cmake] -- Contents of 'version_same_name_as_tmp_at_only.hpp': ''
[cmake] -- Contents of 'version_same_name_as_tmp_copy_only.hpp': 'int version = 1\;'
[cmake] -- Contents of 'version_with_different_tmp_filename.hpp': 'int version = 1\;'
[cmake] -- Configuring done
[cmake] -- Generating done