From 42e1cd137c0eff0297adaea5b34abca0da254eba Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Fri, 25 Apr 2014 20:11:05 +0200 Subject: [PATCH] file(GENERATE): Only write the file if content is different. No policy is used to control this behavior for now. --- .../cmGeneratorExpressionEvaluationFile.cxx | 15 +++-------- .../RunCMake/File_Generate/RunCMakeTest.cmake | 27 +++++++++++++++++++ .../File_Generate/WriteIfDifferent-result.txt | 1 + .../File_Generate/WriteIfDifferent-stderr.txt | 1 + .../File_Generate/WriteIfDifferent.cmake | 5 ++++ 5 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 Tests/RunCMake/File_Generate/WriteIfDifferent-result.txt create mode 100644 Tests/RunCMake/File_Generate/WriteIfDifferent-stderr.txt create mode 100644 Tests/RunCMake/File_Generate/WriteIfDifferent.cmake diff --git a/Source/cmGeneratorExpressionEvaluationFile.cxx b/Source/cmGeneratorExpressionEvaluationFile.cxx index 95a946a82..01d31d3b8 100644 --- a/Source/cmGeneratorExpressionEvaluationFile.cxx +++ b/Source/cmGeneratorExpressionEvaluationFile.cxx @@ -13,6 +13,7 @@ #include "cmGeneratorExpressionEvaluationFile.h" #include "cmMakefile.h" +#include "cmGeneratedFileStream.h" #include #include @@ -79,19 +80,9 @@ void cmGeneratorExpressionEvaluationFile::Generate(const std::string& config, this->Files.push_back(outputFileName); outputFiles[outputFileName] = outputContent; - cmsys::ofstream fout(outputFileName.c_str()); - - if(!fout) - { - cmOStringStream e; - e << "Evaluation file \"" << outputFileName << "\" cannot be written."; - this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str()); - return; - } - + cmGeneratedFileStream fout(outputFileName.c_str()); + fout.SetCopyIfDifferent(true); fout << outputContent; - - fout.close(); } //---------------------------------------------------------------------------- diff --git a/Tests/RunCMake/File_Generate/RunCMakeTest.cmake b/Tests/RunCMake/File_Generate/RunCMakeTest.cmake index f07431c0e..f23fa1983 100644 --- a/Tests/RunCMake/File_Generate/RunCMakeTest.cmake +++ b/Tests/RunCMake/File_Generate/RunCMakeTest.cmake @@ -8,3 +8,30 @@ run_cmake(EmptyCondition1) run_cmake(EmptyCondition2) run_cmake(BadCondition) run_cmake(DebugEvaluate) + +set(timeformat "%Y%j%H%M%S") + +file(REMOVE "${RunCMake_BINARY_DIR}/WriteIfDifferent-build/output_file.txt") +set(RunCMake_TEST_FILE "WriteIfDifferent") +set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/WriteIfDifferent-build") +run_cmake(WriteIfDifferent-prepare) +unset(RunCMake_TEST_FILE) +unset(RunCMake_TEST_BINARY_DIR) +file(TIMESTAMP "${RunCMake_BINARY_DIR}/WriteIfDifferent-build/output_file.txt" timestamp ${timeformat}) +if(NOT timestamp) + message(SEND_ERROR "Could not get timestamp for \"${RunCMake_BINARY_DIR}/WriteIfDifferent-build/output_file.txt\"") +endif() + +execute_process(COMMAND ${CMAKE_COMMAND} -E sleep 1) + +set(RunCMake_TEST_NO_CLEAN ON) +run_cmake(WriteIfDifferent) +file(TIMESTAMP "${RunCMake_BINARY_DIR}/WriteIfDifferent-build/output_file.txt" timestamp_after ${timeformat}) +if(NOT timestamp_after) + message(SEND_ERROR "Could not get timestamp for \"${RunCMake_BINARY_DIR}/WriteIfDifferent-build/output_file.txt\"") +endif() +unset(RunCMake_TEST_NO_CLEAN) + +if (NOT timestamp_after STREQUAL timestamp) + message(SEND_ERROR "WriteIfDifferent changed output file.") +endif() diff --git a/Tests/RunCMake/File_Generate/WriteIfDifferent-result.txt b/Tests/RunCMake/File_Generate/WriteIfDifferent-result.txt new file mode 100644 index 000000000..573541ac9 --- /dev/null +++ b/Tests/RunCMake/File_Generate/WriteIfDifferent-result.txt @@ -0,0 +1 @@ +0 diff --git a/Tests/RunCMake/File_Generate/WriteIfDifferent-stderr.txt b/Tests/RunCMake/File_Generate/WriteIfDifferent-stderr.txt new file mode 100644 index 000000000..10f32932e --- /dev/null +++ b/Tests/RunCMake/File_Generate/WriteIfDifferent-stderr.txt @@ -0,0 +1 @@ +^$ diff --git a/Tests/RunCMake/File_Generate/WriteIfDifferent.cmake b/Tests/RunCMake/File_Generate/WriteIfDifferent.cmake new file mode 100644 index 000000000..d1d832a11 --- /dev/null +++ b/Tests/RunCMake/File_Generate/WriteIfDifferent.cmake @@ -0,0 +1,5 @@ + +file(GENERATE + OUTPUT output_file.txt + CONTENT "123" +)