From 0d3a034725bdf221f7ea47eee286854facac4979 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 7 Jul 2020 13:26:14 -0400 Subject: cmGeneratedFileStream: Optionally use custom temporary file extension --- Source/cmGeneratedFileStream.cxx | 14 ++++++++++++-- Source/cmGeneratedFileStream.h | 9 +++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Source/cmGeneratedFileStream.cxx b/Source/cmGeneratedFileStream.cxx index 9cee0e6..119067e 100644 --- a/Source/cmGeneratedFileStream.cxx +++ b/Source/cmGeneratedFileStream.cxx @@ -122,10 +122,15 @@ void cmGeneratedFileStreamBase::Open(std::string const& name) // Create the name of the temporary file. this->TempName = name; #if defined(__VMS) - this->TempName += "_tmp"; + this->TempName += "_"; #else - this->TempName += ".tmp"; + this->TempName += "."; #endif + if (!this->TempExt.empty()) { + this->TempName += this->TempExt; + } else { + this->TempName += "tmp"; + } // Make sure the temporary file that will be used is not present. cmSystemTools::RemoveFile(this->TempName); @@ -216,3 +221,8 @@ void cmGeneratedFileStream::SetName(const std::string& fname) { this->Name = fname; } + +void cmGeneratedFileStream::SetTempExt(std::string const& ext) +{ + this->TempExt = ext; +} diff --git a/Source/cmGeneratedFileStream.h b/Source/cmGeneratedFileStream.h index a9088ac..3dee142 100644 --- a/Source/cmGeneratedFileStream.h +++ b/Source/cmGeneratedFileStream.h @@ -43,6 +43,9 @@ protected: // The name of the final destination file for the output. std::string Name; + // The extension of the temporary file. + std::string TempExt; + // The name of the temporary file. std::string TempName; @@ -138,6 +141,12 @@ public: * the output file to be changed during the use of cmGeneratedFileStream. */ void SetName(const std::string& fname); + + /** + * Set set a custom temporary file extension used with 'Open'. + * This does not work if the file was opened by the constructor. + */ + void SetTempExt(std::string const& ext); }; #endif -- cgit v0.12 From 09ff1cb650a8084fce51f0005c446ea9215890d3 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 7 Jul 2020 13:36:49 -0400 Subject: cmCTest: Explicitly specify '.tmp' extension on in-progress log files Do not depend on `cmGeneratedFileStream` to have this default. --- Source/cmCTest.cxx | 1 + 1 file changed, 1 insertion(+) diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx index bca7540..4254e2e 100644 --- a/Source/cmCTest.cxx +++ b/Source/cmCTest.cxx @@ -841,6 +841,7 @@ bool cmCTest::OpenOutputFile(const std::string& path, const std::string& name, } } std::string filename = testingDir + "/" + name; + stream.SetTempExt("tmp"); stream.Open(filename); if (!stream) { cmCTestLog(this, ERROR_MESSAGE, -- cgit v0.12 From eaa420e99c86cb700f37fd21420ab86adeed798f Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 7 Jul 2020 13:39:35 -0400 Subject: cmGeneratedFileStream: Use random temporary file extension by default Avoid collisions with project-specified files named with `.tmp`. Issue: #20873 --- Source/cmGeneratedFileStream.cxx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Source/cmGeneratedFileStream.cxx b/Source/cmGeneratedFileStream.cxx index 119067e..345f0ba 100644 --- a/Source/cmGeneratedFileStream.cxx +++ b/Source/cmGeneratedFileStream.cxx @@ -129,7 +129,9 @@ void cmGeneratedFileStreamBase::Open(std::string const& name) if (!this->TempExt.empty()) { this->TempName += this->TempExt; } else { - this->TempName += "tmp"; + char buf[64]; + sprintf(buf, "tmp%05x", cmSystemTools::RandomSeed() & 0xFFFFF); + this->TempName += buf; } // Make sure the temporary file that will be used is not present. -- cgit v0.12 From 4d40253f1cbb261aa469411a3c2a7ffc2ef761a4 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 7 Jul 2020 13:53:20 -0400 Subject: Tests: Cover file(GENERATE) using 'foo.tmp' as input for output 'foo' Fixes: #20873 --- Tests/RunCMake/File_Generate/AdjacentInOut.cmake | 6 ++++++ Tests/RunCMake/File_Generate/AdjacentInOut.in | 1 + Tests/RunCMake/File_Generate/RunCMakeTest.cmake | 8 ++++++++ 3 files changed, 15 insertions(+) create mode 100644 Tests/RunCMake/File_Generate/AdjacentInOut.cmake create mode 100644 Tests/RunCMake/File_Generate/AdjacentInOut.in diff --git a/Tests/RunCMake/File_Generate/AdjacentInOut.cmake b/Tests/RunCMake/File_Generate/AdjacentInOut.cmake new file mode 100644 index 0000000..828c2ee --- /dev/null +++ b/Tests/RunCMake/File_Generate/AdjacentInOut.cmake @@ -0,0 +1,6 @@ +cmake_policy(SET CMP0070 NEW) +if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/AdjacentInOut.txt") + message(FATAL_ERROR "CMake should not re-run during the build!") +endif() +configure_file(AdjacentInOut.in ${CMAKE_CURRENT_BINARY_DIR}/AdjacentInOut.txt.tmp) +file(GENERATE OUTPUT AdjacentInOut.txt INPUT ${CMAKE_CURRENT_BINARY_DIR}/AdjacentInOut.txt.tmp) diff --git a/Tests/RunCMake/File_Generate/AdjacentInOut.in b/Tests/RunCMake/File_Generate/AdjacentInOut.in new file mode 100644 index 0000000..bfbbda7 --- /dev/null +++ b/Tests/RunCMake/File_Generate/AdjacentInOut.in @@ -0,0 +1 @@ +Sample Text File diff --git a/Tests/RunCMake/File_Generate/RunCMakeTest.cmake b/Tests/RunCMake/File_Generate/RunCMakeTest.cmake index 94aaca8..5987417 100644 --- a/Tests/RunCMake/File_Generate/RunCMakeTest.cmake +++ b/Tests/RunCMake/File_Generate/RunCMakeTest.cmake @@ -72,6 +72,7 @@ if (UNIX AND EXISTS /bin/sh) if (NOT script_output STREQUAL SUCCESS) message(SEND_ERROR "Generated script did not execute correctly:\n${script_output}\n====\n${script_error}") endif() + unset(RunCMake_TEST_NO_CLEAN) endif() if (RunCMake_GENERATOR MATCHES Makefiles) @@ -104,3 +105,10 @@ if (RunCMake_GENERATOR MATCHES Makefiles) message(SEND_ERROR "File did not re-generate: \"${RunCMake_BINARY_DIR}/ReRunCMake-build/output_file.txt\"") endif() endif() + +set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/AdjacentInOut-build) +run_cmake(AdjacentInOut) +set(RunCMake_TEST_NO_CLEAN 1) +run_cmake_command(AdjacentInOut-nowork ${CMAKE_COMMAND} --build .) +unset(RunCMake_TEST_BINARY_DIR) +unset(RunCMake_TEST_NO_CLEAN) -- cgit v0.12