From 2930a198f610e729d73df6771d70119adac6df2e Mon Sep 17 00:00:00 2001 From: Sebastian Holtermann Date: Wed, 30 May 2018 16:34:09 +0200 Subject: Autogen: Add lock file to AUTORCC commands By using a per .qrc lock file in AUTORCC, the same `cmake -E cmake_autorcc ...` command can be called concurrently. --- Source/cmQtAutoGenInitializer.cxx | 6 +++++ Source/cmQtAutoGenInitializer.h | 1 + Source/cmQtAutoGenerator.cxx | 5 ++-- Source/cmQtAutoGenerator.h | 2 +- Source/cmQtAutoGeneratorRcc.cxx | 52 +++++++++++++++++++++++++++++++++++---- Source/cmQtAutoGeneratorRcc.h | 5 +++- 6 files changed, 62 insertions(+), 9 deletions(-) diff --git a/Source/cmQtAutoGenInitializer.cxx b/Source/cmQtAutoGenInitializer.cxx index 566a2a9..59f4eb8 100644 --- a/Source/cmQtAutoGenInitializer.cxx +++ b/Source/cmQtAutoGenInitializer.cxx @@ -588,8 +588,13 @@ void cmQtAutoGenInitializer::InitCustomTargets() if (!qrc.Unique) { base += qrc.PathChecksum; } + + qrc.LockFile = base; + qrc.LockFile += ".lock"; + qrc.InfoFile = base; qrc.InfoFile += "Info.cmake"; + qrc.SettingsFile = base; qrc.SettingsFile += "Settings.txt"; } @@ -1043,6 +1048,7 @@ void cmQtAutoGenInitializer::SetupCustomTargets() CWrite("ARCC_RCC_LIST_OPTIONS", cmJoin(this->RccListOptions, ";")); ofs << "# Rcc job\n"; + CWrite("ARCC_LOCK_FILE", qrc.LockFile); CWrite("ARCC_SOURCE", qrc.QrcFile); CWrite("ARCC_OUTPUT_CHECKSUM", qrc.PathChecksum); CWrite("ARCC_OUTPUT_NAME", diff --git a/Source/cmQtAutoGenInitializer.h b/Source/cmQtAutoGenInitializer.h index 2a47e46..5fbf0cb 100644 --- a/Source/cmQtAutoGenInitializer.h +++ b/Source/cmQtAutoGenInitializer.h @@ -32,6 +32,7 @@ public: } public: + std::string LockFile; std::string QrcFile; std::string QrcName; std::string PathChecksum; diff --git a/Source/cmQtAutoGenerator.cxx b/Source/cmQtAutoGenerator.cxx index bf184d8..750e1ed 100644 --- a/Source/cmQtAutoGenerator.cxx +++ b/Source/cmQtAutoGenerator.cxx @@ -367,10 +367,11 @@ bool cmQtAutoGenerator::FileSystem::FileRemove(std::string const& filename) return cmSystemTools::RemoveFile(filename); } -bool cmQtAutoGenerator::FileSystem::Touch(std::string const& filename) +bool cmQtAutoGenerator::FileSystem::Touch(std::string const& filename, + bool create) { std::lock_guard lock(Mutex_); - return cmSystemTools::Touch(filename, false); + return cmSystemTools::Touch(filename, create); } bool cmQtAutoGenerator::FileSystem::MakeDirectory(std::string const& dirname) diff --git a/Source/cmQtAutoGenerator.h b/Source/cmQtAutoGenerator.h index 299e4c2..4e38413 100644 --- a/Source/cmQtAutoGenerator.h +++ b/Source/cmQtAutoGenerator.h @@ -124,7 +124,7 @@ public: bool FileDiffers(std::string const& filename, std::string const& content); bool FileRemove(std::string const& filename); - bool Touch(std::string const& filename); + bool Touch(std::string const& filename, bool create = false); // -- Directory access bool MakeDirectory(std::string const& dirname); diff --git a/Source/cmQtAutoGeneratorRcc.cxx b/Source/cmQtAutoGeneratorRcc.cxx index 84ec5e2..fdb556a 100644 --- a/Source/cmQtAutoGeneratorRcc.cxx +++ b/Source/cmQtAutoGeneratorRcc.cxx @@ -5,6 +5,7 @@ #include "cmAlgorithms.h" #include "cmCryptoHash.h" +#include "cmFileLockResult.h" #include "cmMakefile.h" #include "cmSystemTools.h" #include "cmUVHandlePtr.h" @@ -89,6 +90,7 @@ bool cmQtAutoGeneratorRcc::Init(cmMakefile* makefile) RccListOptions_ = InfoGetList("ARCC_RCC_LIST_OPTIONS"); // - Job + LockFile_ = InfoGet("ARCC_LOCK_FILE"); QrcFile_ = InfoGet("ARCC_SOURCE"); QrcFileName_ = cmSystemTools::GetFilenameName(QrcFile_); QrcFileDir_ = cmSystemTools::GetFilenamePath(QrcFile_); @@ -101,6 +103,10 @@ bool cmQtAutoGeneratorRcc::Init(cmMakefile* makefile) SettingsFile_ = InfoGetConfig("ARCC_SETTINGS_FILE"); // - Validity checks + if (LockFile_.empty()) { + Log().ErrorFile(GeneratorT::RCC, InfoFile(), "Lock file name missing"); + return false; + } if (SettingsFile_.empty()) { Log().ErrorFile(GeneratorT::RCC, InfoFile(), "Settings file name missing"); return false; @@ -170,8 +176,11 @@ void cmQtAutoGeneratorRcc::PollStage() switch (Stage_) { // -- Initialize case StageT::SETTINGS_READ: - SettingsFileRead(); - SetStage(StageT::TEST_QRC_RCC_FILES); + if (SettingsFileRead()) { + SetStage(StageT::TEST_QRC_RCC_FILES); + } else { + SetStage(StageT::FINISH); + } break; // -- Change detection @@ -252,7 +261,7 @@ std::string cmQtAutoGeneratorRcc::MultiConfigOutput() const return res; } -void cmQtAutoGeneratorRcc::SettingsFileRead() +bool cmQtAutoGeneratorRcc::SettingsFileRead() { // Compose current settings strings { @@ -278,21 +287,51 @@ void cmQtAutoGeneratorRcc::SettingsFileRead() } } + // Make sure the settings file exists + if (!FileSys().FileExists(SettingsFile_, true)) { + // Touch the settings file to make sure it exists + FileSys().Touch(SettingsFile_, true); + } + + // Lock the lock file + { + // Make sure the lock file exists + if (!FileSys().FileExists(LockFile_, true)) { + if (!FileSys().Touch(LockFile_, true)) { + Log().ErrorFile(GeneratorT::RCC, LockFile_, + "Lock file creation failed"); + Error_ = true; + return false; + } + } + // Lock the lock file + cmFileLockResult lockResult = + LockFileLock_.Lock(LockFile_, static_cast(-1)); + if (!lockResult.IsOk()) { + Log().ErrorFile(GeneratorT::RCC, LockFile_, + "File lock failed: " + lockResult.GetOutputMessage()); + Error_ = true; + return false; + } + } + // Read old settings { std::string content; if (FileSys().FileRead(content, SettingsFile_)) { SettingsChanged_ = (SettingsString_ != SettingsFind(content, "rcc")); - // In case any setting changed remove the old settings file. + // In case any setting changed clear the old settings file. // This triggers a full rebuild on the next run if the current // build is aborted before writing the current settings in the end. if (SettingsChanged_) { - FileSys().FileRemove(SettingsFile_); + FileSys().FileWrite(GeneratorT::RCC, SettingsFile_, ""); } } else { SettingsChanged_ = true; } } + + return true; } void cmQtAutoGeneratorRcc::SettingsFileWrite() @@ -315,6 +354,9 @@ void cmQtAutoGeneratorRcc::SettingsFileWrite() Error_ = true; } } + + // Unlock the lock file + LockFileLock_.Release(); } bool cmQtAutoGeneratorRcc::TestQrcRccFiles() diff --git a/Source/cmQtAutoGeneratorRcc.h b/Source/cmQtAutoGeneratorRcc.h index 55e0998..74cec36 100644 --- a/Source/cmQtAutoGeneratorRcc.h +++ b/Source/cmQtAutoGeneratorRcc.h @@ -5,6 +5,7 @@ #include "cmConfigure.h" // IWYU pragma: keep +#include "cmFileLock.h" #include "cmQtAutoGenerator.h" #include "cm_uv.h" @@ -48,7 +49,7 @@ private: void PollStage(); void SetStage(StageT stage); // -- Settings file - void SettingsFileRead(); + bool SettingsFileRead(); void SettingsFileWrite(); // -- Tests bool TestQrcRccFiles(); @@ -77,6 +78,8 @@ private: std::string RccExecutable_; std::vector RccListOptions_; // -- Job + std::string LockFile_; + cmFileLock LockFileLock_; std::string QrcFile_; std::string QrcFileName_; std::string QrcFileDir_; -- cgit v0.12