diff options
Diffstat (limited to 'Source/cmQtAutoGenerator.cxx')
-rw-r--r-- | Source/cmQtAutoGenerator.cxx | 305 |
1 files changed, 145 insertions, 160 deletions
diff --git a/Source/cmQtAutoGenerator.cxx b/Source/cmQtAutoGenerator.cxx index e2d7deb..f115016 100644 --- a/Source/cmQtAutoGenerator.cxx +++ b/Source/cmQtAutoGenerator.cxx @@ -15,10 +15,39 @@ #include "cmake.h" #include <algorithm> +#include <sstream> #include <utility> // -- Class methods +cmQtAutoGenerator::Logger::Logger() +{ + // Initialize logger + { + std::string verbose; + if (cmSystemTools::GetEnv("VERBOSE", verbose) && !verbose.empty()) { + unsigned long iVerbose = 0; + if (cmSystemTools::StringToULong(verbose.c_str(), &iVerbose)) { + SetVerbosity(static_cast<unsigned int>(iVerbose)); + } else { + // Non numeric verbosity + SetVerbose(cmSystemTools::IsOn(verbose)); + } + } + } + { + std::string colorEnv; + cmSystemTools::GetEnv("COLOR", colorEnv); + if (!colorEnv.empty()) { + SetColorOutput(cmSystemTools::IsOn(colorEnv)); + } else { + SetColorOutput(true); + } + } +} + +cmQtAutoGenerator::Logger::~Logger() = default; + void cmQtAutoGenerator::Logger::RaiseVerbosity(std::string const& value) { unsigned long verbosity = 0; @@ -43,8 +72,7 @@ std::string cmQtAutoGenerator::Logger::HeadLine(std::string const& title) return head; } -void cmQtAutoGenerator::Logger::Info(GeneratorT genType, - std::string const& message) +void cmQtAutoGenerator::Logger::Info(GenT genType, std::string const& message) { std::string msg = GeneratorName(genType); msg += ": "; @@ -58,7 +86,7 @@ void cmQtAutoGenerator::Logger::Info(GeneratorT genType, } } -void cmQtAutoGenerator::Logger::Warning(GeneratorT genType, +void cmQtAutoGenerator::Logger::Warning(GenT genType, std::string const& message) { std::string msg; @@ -82,7 +110,7 @@ void cmQtAutoGenerator::Logger::Warning(GeneratorT genType, } } -void cmQtAutoGenerator::Logger::WarningFile(GeneratorT genType, +void cmQtAutoGenerator::Logger::WarningFile(GenT genType, std::string const& filename, std::string const& message) { @@ -94,8 +122,7 @@ void cmQtAutoGenerator::Logger::WarningFile(GeneratorT genType, Warning(genType, msg); } -void cmQtAutoGenerator::Logger::Error(GeneratorT genType, - std::string const& message) +void cmQtAutoGenerator::Logger::Error(GenT genType, std::string const& message) { std::string msg; msg += HeadLine(GeneratorName(genType) + " error"); @@ -111,7 +138,7 @@ void cmQtAutoGenerator::Logger::Error(GeneratorT genType, } } -void cmQtAutoGenerator::Logger::ErrorFile(GeneratorT genType, +void cmQtAutoGenerator::Logger::ErrorFile(GenT genType, std::string const& filename, std::string const& message) { @@ -124,7 +151,7 @@ void cmQtAutoGenerator::Logger::ErrorFile(GeneratorT genType, } void cmQtAutoGenerator::Logger::ErrorCommand( - GeneratorT genType, std::string const& message, + GenT genType, std::string const& message, std::vector<std::string> const& command, std::string const& output) { std::string msg; @@ -153,6 +180,91 @@ void cmQtAutoGenerator::Logger::ErrorCommand( } } +bool cmQtAutoGenerator::MakeParentDirectory(std::string const& filename) +{ + bool success = true; + std::string const dirName = cmSystemTools::GetFilenamePath(filename); + if (!dirName.empty()) { + success = cmSystemTools::MakeDirectory(dirName); + } + return success; +} + +bool cmQtAutoGenerator::FileRead(std::string& content, + std::string const& filename, + std::string* error) +{ + content.clear(); + if (!cmSystemTools::FileExists(filename, true)) { + if (error != nullptr) { + error->append("Not a file."); + } + return false; + } + + unsigned long const length = cmSystemTools::FileLength(filename); + cmsys::ifstream ifs(filename.c_str(), (std::ios::in | std::ios::binary)); + + // Use lambda to save destructor calls of ifs + return [&ifs, length, &content, error]() -> bool { + if (!ifs) { + if (error != nullptr) { + error->append("Opening the file for reading failed."); + } + return false; + } + content.reserve(length); + typedef std::istreambuf_iterator<char> IsIt; + content.assign(IsIt{ ifs }, IsIt{}); + if (!ifs) { + content.clear(); + if (error != nullptr) { + error->append("Reading from the file failed."); + } + return false; + } + return true; + }(); +} + +bool cmQtAutoGenerator::FileWrite(std::string const& filename, + std::string const& content, + std::string* error) +{ + // Make sure the parent directory exists + if (!cmQtAutoGenerator::MakeParentDirectory(filename)) { + if (error != nullptr) { + error->assign("Could not create parent directory."); + } + return false; + } + cmsys::ofstream ofs; + ofs.open(filename.c_str(), + (std::ios::out | std::ios::binary | std::ios::trunc)); + + // Use lambda to save destructor calls of ofs + return [&ofs, &content, error]() -> bool { + if (!ofs) { + if (error != nullptr) { + error->assign("Opening file for writing failed."); + } + return false; + } + ofs << content; + if (!ofs.good()) { + if (error != nullptr) { + error->assign("File writing failed."); + } + return false; + } + return true; + }(); +} + +cmQtAutoGenerator::FileSystem::FileSystem() = default; + +cmQtAutoGenerator::FileSystem::~FileSystem() = default; + std::string cmQtAutoGenerator::FileSystem::GetRealPath( std::string const& filename) { @@ -160,11 +272,11 @@ std::string cmQtAutoGenerator::FileSystem::GetRealPath( return cmSystemTools::GetRealPath(filename); } -std::string cmQtAutoGenerator::FileSystem::CollapseCombinedPath( - std::string const& dir, std::string const& file) +std::string cmQtAutoGenerator::FileSystem::CollapseFullPath( + std::string const& file, std::string const& dir) { std::lock_guard<std::mutex> lock(Mutex_); - return cmSystemTools::CollapseCombinedPath(dir, file); + return cmSystemTools::CollapseFullPath(file, dir); } void cmQtAutoGenerator::FileSystem::SplitPath( @@ -268,91 +380,16 @@ bool cmQtAutoGenerator::FileSystem::FileRead(std::string& content, std::string const& filename, std::string* error) { - bool success = false; - if (FileExists(filename, true)) { - unsigned long const length = FileLength(filename); - { - std::lock_guard<std::mutex> lock(Mutex_); - cmsys::ifstream ifs(filename.c_str(), (std::ios::in | std::ios::binary)); - if (ifs) { - content.reserve(length); - content.assign(std::istreambuf_iterator<char>{ ifs }, - std::istreambuf_iterator<char>{}); - if (ifs) { - success = true; - } else { - content.clear(); - if (error != nullptr) { - error->append("Reading from the file failed."); - } - } - } else if (error != nullptr) { - error->append("Opening the file for reading failed."); - } - } - } else if (error != nullptr) { - error->append( - "The file does not exist, is not readable or is a directory."); - } - return success; -} - -bool cmQtAutoGenerator::FileSystem::FileRead(GeneratorT genType, - std::string& content, - std::string const& filename) -{ - std::string error; - if (!FileRead(content, filename, &error)) { - Log()->ErrorFile(genType, filename, error); - return false; - } - return true; + std::lock_guard<std::mutex> lock(Mutex_); + return cmQtAutoGenerator::FileRead(content, filename, error); } bool cmQtAutoGenerator::FileSystem::FileWrite(std::string const& filename, std::string const& content, std::string* error) { - bool success = false; - // Make sure the parent directory exists - if (MakeParentDirectory(filename)) { - std::lock_guard<std::mutex> lock(Mutex_); - cmsys::ofstream outfile; - outfile.open(filename.c_str(), - (std::ios::out | std::ios::binary | std::ios::trunc)); - if (outfile) { - outfile << content; - // Check for write errors - if (outfile.good()) { - success = true; - } else { - if (error != nullptr) { - error->assign("File writing failed"); - } - } - } else { - if (error != nullptr) { - error->assign("Opening file for writing failed"); - } - } - } else { - if (error != nullptr) { - error->assign("Could not create parent directory"); - } - } - return success; -} - -bool cmQtAutoGenerator::FileSystem::FileWrite(GeneratorT genType, - std::string const& filename, - std::string const& content) -{ - std::string error; - if (!FileWrite(filename, content, &error)) { - Log()->ErrorFile(genType, filename, error); - return false; - } - return true; + std::lock_guard<std::mutex> lock(Mutex_); + return cmQtAutoGenerator::FileWrite(filename, content, error); } bool cmQtAutoGenerator::FileSystem::FileDiffers(std::string const& filename, @@ -387,35 +424,11 @@ bool cmQtAutoGenerator::FileSystem::MakeDirectory(std::string const& dirname) return cmSystemTools::MakeDirectory(dirname); } -bool cmQtAutoGenerator::FileSystem::MakeDirectory(GeneratorT genType, - std::string const& dirname) -{ - if (!MakeDirectory(dirname)) { - Log()->ErrorFile(genType, dirname, "Could not create directory"); - return false; - } - return true; -} - bool cmQtAutoGenerator::FileSystem::MakeParentDirectory( std::string const& filename) { - bool success = true; - std::string const dirName = cmSystemTools::GetFilenamePath(filename); - if (!dirName.empty()) { - success = MakeDirectory(dirName); - } - return success; -} - -bool cmQtAutoGenerator::FileSystem::MakeParentDirectory( - GeneratorT genType, std::string const& filename) -{ - if (!MakeParentDirectory(filename)) { - Log()->ErrorFile(genType, filename, "Could not create parent directory"); - return false; - } - return true; + std::lock_guard<std::mutex> lock(Mutex_); + return cmQtAutoGenerator::MakeParentDirectory(filename); } int cmQtAutoGenerator::ReadOnlyProcessT::PipeT::init(uv_loop_t* uv_loop, @@ -447,7 +460,7 @@ void cmQtAutoGenerator::ReadOnlyProcessT::PipeT::UVAlloc(uv_handle_t* handle, { auto& pipe = *reinterpret_cast<PipeT*>(handle->data); pipe.Buffer_.resize(suggestedSize); - buf->base = &pipe.Buffer_.front(); + buf->base = pipe.Buffer_.data(); buf->len = pipe.Buffer_.size(); } @@ -506,6 +519,13 @@ void cmQtAutoGenerator::ReadOnlyProcessT::setup( Setup_.MergedOutput = mergedOutput; } +static std::string getUVError(const char* prefixString, int uvErrorCode) +{ + std::ostringstream ost; + ost << prefixString << ": " << uv_strerror(uvErrorCode); + return ost.str(); +} + bool cmQtAutoGenerator::ReadOnlyProcessT::start( uv_loop_t* uv_loop, std::function<void()>&& finishedCallback) { @@ -555,15 +575,17 @@ bool cmQtAutoGenerator::ReadOnlyProcessT::start( std::fill_n(reinterpret_cast<char*>(&UVOptions_), sizeof(UVOptions_), 0); UVOptions_.exit_cb = &ReadOnlyProcessT::UVExit; UVOptions_.file = CommandPtr_[0]; - UVOptions_.args = const_cast<char**>(&CommandPtr_.front()); + UVOptions_.args = const_cast<char**>(CommandPtr_.data()); UVOptions_.cwd = Setup_.WorkingDirectory.c_str(); UVOptions_.flags = UV_PROCESS_WINDOWS_HIDE; UVOptions_.stdio_count = static_cast<int>(UVOptionsStdIO_.size()); - UVOptions_.stdio = &UVOptionsStdIO_.front(); + UVOptions_.stdio = UVOptionsStdIO_.data(); // -- Spawn process - if (UVProcess_.spawn(*uv_loop, UVOptions_, this) != 0) { - Result()->ErrorMessage = "libuv process spawn failed"; + int uvErrorCode = UVProcess_.spawn(*uv_loop, UVOptions_, this); + if (uvErrorCode != 0) { + Result()->ErrorMessage = + getUVError("libuv process spawn failed ", uvErrorCode); } } // -- Start reading from stdio streams @@ -635,46 +657,9 @@ void cmQtAutoGenerator::ReadOnlyProcessT::UVTryFinish() } } -cmQtAutoGenerator::cmQtAutoGenerator() - : FileSys_(&Logger_) -{ - // Initialize logger - { - std::string verbose; - if (cmSystemTools::GetEnv("VERBOSE", verbose) && !verbose.empty()) { - unsigned long iVerbose = 0; - if (cmSystemTools::StringToULong(verbose.c_str(), &iVerbose)) { - Logger_.SetVerbosity(static_cast<unsigned int>(iVerbose)); - } else { - // Non numeric verbosity - Logger_.SetVerbose(cmSystemTools::IsOn(verbose)); - } - } - } - { - std::string colorEnv; - cmSystemTools::GetEnv("COLOR", colorEnv); - if (!colorEnv.empty()) { - Logger_.SetColorOutput(cmSystemTools::IsOn(colorEnv)); - } else { - Logger_.SetColorOutput(true); - } - } - - // Initialize libuv loop - uv_disable_stdio_inheritance(); -#ifdef CMAKE_UV_SIGNAL_HACK - UVHackRAII_ = cm::make_unique<cmUVSignalHackRAII>(); -#endif - UVLoop_ = cm::make_unique<uv_loop_t>(); - uv_loop_init(UVLoop()); -} +cmQtAutoGenerator::cmQtAutoGenerator() = default; -cmQtAutoGenerator::~cmQtAutoGenerator() -{ - // Close libuv loop - uv_loop_close(UVLoop()); -} +cmQtAutoGenerator::~cmQtAutoGenerator() = default; bool cmQtAutoGenerator::Run(std::string const& infoFile, std::string const& config) |