1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmDependsCompiler.h"
#include <algorithm>
#include <iterator>
#include <map>
#include <string>
#include <unordered_set>
#include <utility>
#include <cm/optional>
#include <cm/string_view>
#include <cm/vector>
#include <cmext/string_view>
#include "cmsys/FStream.hxx"
#include "cmFileTime.h"
#include "cmGccDepfileReader.h"
#include "cmGccDepfileReaderTypes.h"
#include "cmGlobalUnixMakefileGenerator3.h"
#include "cmLocalUnixMakefileGenerator3.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
bool cmDependsCompiler::CheckDependencies(
const std::string& internalDepFile, const std::vector<std::string>& depFiles,
cmDepends::DependencyMap& dependencies,
const std::function<bool(const std::string&)>& isValidPath)
{
bool status = true;
bool forceReadDeps = true;
cmFileTime internalDepFileTime;
// read cached dependencies stored in internal file
if (cmSystemTools::FileExists(internalDepFile)) {
internalDepFileTime.Load(internalDepFile);
forceReadDeps = false;
// read current dependencies
cmsys::ifstream fin(internalDepFile.c_str());
if (fin) {
std::string line;
std::string depender;
std::vector<std::string>* currentDependencies = nullptr;
while (std::getline(fin, line)) {
if (line.empty() || line.front() == '#') {
continue;
}
// Drop carriage return character at the end
if (line.back() == '\r') {
line.pop_back();
if (line.empty()) {
continue;
}
}
// Check if this a depender line
if (line.front() != ' ') {
depender = std::move(line);
currentDependencies = &dependencies[depender];
continue;
}
// This is a dependee line
if (currentDependencies) {
currentDependencies->emplace_back(line.substr(1));
}
}
fin.close();
}
}
// Now, update dependencies map with all new compiler generated
// dependencies files
cmFileTime depFileTime;
for (auto dep = depFiles.begin(); dep != depFiles.end(); dep++) {
const auto& source = *dep++;
const auto& target = *dep++;
const auto& format = *dep++;
const auto& depFile = *dep;
if (!cmSystemTools::FileExists(depFile)) {
continue;
}
if (!forceReadDeps) {
depFileTime.Load(depFile);
}
if (forceReadDeps || depFileTime.Compare(internalDepFileTime) >= 0) {
status = false;
if (this->Verbose) {
cmSystemTools::Stdout(cmStrCat("Dependencies file \"", depFile,
"\" is newer than depends file \"",
internalDepFile, "\".\n"));
}
std::vector<std::string> depends;
if (format == "custom"_s) {
auto deps = cmReadGccDepfile(
depFile.c_str(), this->LocalGenerator->GetCurrentBinaryDirectory());
if (!deps) {
continue;
}
for (auto& entry : *deps) {
depends = std::move(entry.paths);
if (isValidPath) {
cm::erase_if(depends, isValidPath);
}
// copy depends for each target, except first one, which can be
// moved
for (auto index = entry.rules.size() - 1; index > 0; --index) {
auto& rule_deps = dependencies[entry.rules[index]];
rule_deps.insert(rule_deps.end(), depends.cbegin(),
depends.cend());
}
auto& rule_deps = dependencies[entry.rules.front()];
std::move(depends.cbegin(), depends.cend(),
std::back_inserter(rule_deps));
}
} else {
if (format == "msvc"_s) {
cmsys::ifstream fin(depFile.c_str());
if (!fin) {
continue;
}
std::string line;
if (!isValidPath && !source.empty()) {
// insert source as first dependency
depends.push_back(source);
}
while (cmSystemTools::GetLineFromStream(fin, line)) {
depends.emplace_back(std::move(line));
}
} else if (format == "gcc"_s) {
auto deps = cmReadGccDepfile(
depFile.c_str(), this->LocalGenerator->GetCurrentBinaryDirectory(),
GccDepfilePrependPaths::Deps);
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;
}
// depending of the effective format of the dependencies file
// generated by the compiler, the target can be wrongly identified
// as a dependency so remove it from the list
if (depends.front() == target) {
depends.erase(depends.begin());
}
// ensure source file is the first dependency
if (!source.empty()) {
if (depends.front() != source) {
cm::erase(depends, source);
if (!isValidPath) {
depends.insert(depends.begin(), source);
}
} else if (isValidPath) {
// remove first dependency because it must not be filtered out
depends.erase(depends.begin());
}
}
} else {
// unknown format, ignore it
continue;
}
if (isValidPath) {
cm::erase_if(depends, isValidPath);
if (!source.empty()) {
// insert source as first dependency
depends.insert(depends.begin(), source);
}
}
dependencies[target] = std::move(depends);
}
}
}
return status;
}
void cmDependsCompiler::WriteDependencies(
const cmDepends::DependencyMap& dependencies, std::ostream& makeDepends,
std::ostream& internalDepends)
{
// dependencies file consumed by make tool
const auto& lineContinue = static_cast<cmGlobalUnixMakefileGenerator3*>(
this->LocalGenerator->GetGlobalGenerator())
->LineContinueDirective;
bool supportLongLineDepend = static_cast<cmGlobalUnixMakefileGenerator3*>(
this->LocalGenerator->GetGlobalGenerator())
->SupportsLongLineDependencies();
cmDepends::DependencyMap makeDependencies(dependencies);
std::unordered_set<cm::string_view> phonyTargets;
// external dependencies file
for (auto& node : makeDependencies) {
auto target = this->LocalGenerator->ConvertToMakefilePath(
this->LocalGenerator->MaybeRelativeToTopBinDir(node.first));
auto& deps = node.second;
std::transform(deps.cbegin(), deps.cend(), deps.begin(),
[this](const std::string& dep) {
return this->LocalGenerator->ConvertToMakefilePath(
this->LocalGenerator->MaybeRelativeToTopBinDir(dep));
});
bool first_dep = true;
if (supportLongLineDepend) {
makeDepends << target << ": ";
}
for (const auto& dep : deps) {
if (supportLongLineDepend) {
if (first_dep) {
first_dep = false;
makeDepends << dep;
} else {
makeDepends << ' ' << lineContinue << " " << dep;
}
} else {
makeDepends << target << ": " << dep << std::endl;
}
phonyTargets.emplace(dep.data(), dep.length());
}
makeDepends << std::endl << std::endl;
}
// add phony targets
for (const auto& target : phonyTargets) {
makeDepends << std::endl << target << ':' << std::endl;
}
// internal dependencies file
for (const auto& node : dependencies) {
internalDepends << node.first << std::endl;
for (const auto& dep : node.second) {
internalDepends << ' ' << dep << std::endl;
}
internalDepends << std::endl;
}
}
void cmDependsCompiler::ClearDependencies(
const std::vector<std::string>& depFiles)
{
for (auto dep = depFiles.begin(); dep != depFiles.end(); dep++) {
dep += 3;
cmSystemTools::RemoveFile(*dep);
}
}
|