file(GENERATE): Re-run cmake when appropriate.

Re-run if the input file changes or if the output file is removed.

This only works with the Makefile generators currently.  The limitation
of the Ninja generator is tracked as issue #15256.  The IDE
generators will need larger refactoring as they currently rely on
being able to determine the depends and output files at the start of
generate-time, which is too early for the file(GENERATE) case.
This commit is contained in:
Stephen Kelly 2014-07-22 15:07:39 +02:00
parent d526ebc603
commit 26e98c34dc
5 changed files with 40 additions and 0 deletions

View File

@ -80,6 +80,7 @@ void cmGeneratorExpressionEvaluationFile::Generate(const std::string& config,
return;
}
this->Makefile->AddCMakeOutputFile(outputFileName.c_str());
this->Files.push_back(outputFileName);
outputFiles[outputFileName] = outputContent;
@ -117,6 +118,7 @@ void cmGeneratorExpressionEvaluationFile::Generate()
}
else
{
this->Makefile->AddCMakeDependFile(this->Input.c_str());
cmSystemTools::GetPermissions(this->Input.c_str(), perm);
cmsys::ifstream fin(this->Input.c_str());
if(!fin)

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1 @@
^$

View File

@ -0,0 +1,5 @@
file(GENERATE
OUTPUT output_file.txt
INPUT "${CMAKE_CURRENT_BINARY_DIR}/input_file.txt"
)

View File

@ -61,3 +61,34 @@ if (UNIX AND EXISTS /bin/sh)
message(SEND_ERROR "Generated script did not execute correctly:\n${script_output}\n====\n${script_error}")
endif()
endif()
if (RunCMake_GENERATOR MATCHES Makefiles)
file(MAKE_DIRECTORY "${RunCMake_BINARY_DIR}/ReRunCMake-build/")
file(WRITE "${RunCMake_BINARY_DIR}/ReRunCMake-build/input_file.txt" "InitialContent\n")
set(RunCMake_TEST_NO_CLEAN ON)
run_cmake(ReRunCMake)
unset(RunCMake_TEST_NO_CLEAN)
file(TIMESTAMP "${RunCMake_BINARY_DIR}/ReRunCMake-build/output_file.txt" timestamp ${timeformat})
if(NOT timestamp)
message(SEND_ERROR "Could not get timestamp for \"${RunCMake_BINARY_DIR}/ReRunCMake-build/output_file.txt\"")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} -E sleep 1)
file(WRITE "${RunCMake_BINARY_DIR}/ReRunCMake-build/input_file.txt" "ChangedContent\n")
execute_process(COMMAND ${CMAKE_COMMAND} --build "${RunCMake_BINARY_DIR}/ReRunCMake-build/")
file(READ "${RunCMake_BINARY_DIR}/ReRunCMake-build/output_file.txt" out_content)
if(NOT out_content STREQUAL "ChangedContent\n")
message(SEND_ERROR "File did not change: \"${RunCMake_BINARY_DIR}/ReRunCMake-build/output_file.txt\"")
endif()
file(REMOVE "${RunCMake_BINARY_DIR}/ReRunCMake-build/output_file.txt")
execute_process(COMMAND ${CMAKE_COMMAND} --build "${RunCMake_BINARY_DIR}/ReRunCMake-build/")
if (NOT EXISTS "${RunCMake_BINARY_DIR}/ReRunCMake-build/output_file.txt")
message(SEND_ERROR "File did not re-generate: \"${RunCMake_BINARY_DIR}/ReRunCMake-build/output_file.txt\"")
endif()
endif()