From b2ff3c52df52765b22e0d3b5a23129f6c4a261e1 Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Fri, 11 Dec 2020 13:33:55 +0100 Subject: cmDependsCompiler: rely now on cmGccDepfileReader for depfile parser To avoid duplicate effort of depfile parsing and enhance robustness of parsing against mal-formed depfiles in preparation of DEPFILE option support of add_custom_command command for makefiles generators. --- Source/cmDependsCompiler.cxx | 93 +++++++------------------------------------- 1 file changed, 14 insertions(+), 79 deletions(-) diff --git a/Source/cmDependsCompiler.cxx b/Source/cmDependsCompiler.cxx index 0f695c8..f6e5af8 100644 --- a/Source/cmDependsCompiler.cxx +++ b/Source/cmDependsCompiler.cxx @@ -5,10 +5,12 @@ #include #include +#include #include #include #include +#include #include #include #include @@ -16,77 +18,12 @@ #include "cmsys/FStream.hxx" #include "cmFileTime.h" +#include "cmGccDepfileReader.h" #include "cmGlobalUnixMakefileGenerator3.h" #include "cmLocalUnixMakefileGenerator3.h" #include "cmStringAlgorithms.h" #include "cmSystemTools.h" -namespace { -std::string& ReplaceAll(std::string& data, const std::string& toSearch, - const std::string& replaceStr) -{ - // Get the first occurrence - auto pos = data.find(toSearch); - // Repeat until the end is reached - while (pos != std::string::npos) { - // Replace this occurrence of Sub String - data.replace(pos, toSearch.size(), replaceStr); - // Get the next occurrence from the current position - pos = data.find(toSearch, pos + replaceStr.size()); - } - - return data; -} - -std::string& NormalizePath(std::string& item) -{ - ReplaceAll(item, "$$", "$"); - ReplaceAll(item, "\\ ", " "); - ReplaceAll(item, "\\#", "#"); - ReplaceAll(item, "\\", "/"); - - return item; -} - -void ParseLine(const std::string& line, std::vector& depends) -{ - auto start = line.find_first_not_of(' '); - if (start == std::string::npos || line[start] == '#') { - return; - } - - auto index = start; - while ((index = line.find(' ', index)) != std::string::npos) { - if (line[index - 1] == '\\') { - index += 1; - continue; - } - - auto item = line.substr(start, index - start); - if (item.back() != ':') { - // check that ':' is not present after some spaces - auto index2 = line.find_first_not_of(' ', index + 1); - if (index2 == std::string::npos || line[index2] != ':') { - // this is a dependency, add it - depends.emplace_back(std::move(NormalizePath(item))); - } else { - index = index2; - } - } - - start = line.find_first_not_of(' ', index + 1); - index = start; - } - if (start != std::string::npos) { - auto item = line.substr(start); - if (line.back() != ':') { - // this is a dependency, add it - depends.emplace_back(std::move(NormalizePath(item))); - } - } -} -} - bool cmDependsCompiler::CheckDependencies( const std::string& internalDepFile, const std::vector& depFiles, cmDepends::DependencyMap& dependencies, @@ -156,14 +93,15 @@ bool cmDependsCompiler::CheckDependencies( "\" is newer than depends file \"", internalDepFile, "\".\n")); } - cmsys::ifstream fin(depFile.c_str()); - if (!fin) { - continue; - } std::vector depends; - std::string line; if (format == "msvc"_s) { + cmsys::ifstream fin(depFile.c_str()); + if (!fin) { + continue; + } + + std::string line; if (!isValidPath) { // insert source as first dependency depends.push_back(source); @@ -172,16 +110,13 @@ bool cmDependsCompiler::CheckDependencies( depends.emplace_back(std::move(line)); } } else { - while (cmSystemTools::GetLineFromStream(fin, line)) { - if (line.empty()) { - continue; - } - if (line.back() == '\\') { - line.pop_back(); - } - ParseLine(line, depends); + auto deps = cmReadGccDepfile(depFile.c_str()); + if (!deps) { + continue; } + // dependencies generated by the compiler contains only one target + depends = std::move(deps->front().paths); if (depends.empty()) { // unexpectedly empty, ignore it and continue continue; -- cgit v0.12