cmake/Source/cmDependsC.cxx

468 lines
16 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmDependsC.h"
2017-04-14 19:02:05 +02:00
#include <utility>
2020-02-01 23:06:01 +01:00
#include "cmsys/FStream.hxx"
2019-11-11 23:01:05 +01:00
#include "cmFileTime.h"
2021-09-14 00:13:48 +02:00
#include "cmGlobalUnixMakefileGenerator3.h"
2023-07-02 19:51:09 +02:00
#include "cmList.h"
2020-08-30 11:54:41 +02:00
#include "cmLocalUnixMakefileGenerator3.h"
#include "cmMakefile.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2016-07-09 11:21:54 +02:00
#define INCLUDE_REGEX_LINE \
2017-04-14 19:02:05 +02:00
"^[ \t]*[#%][ \t]*(include|import)[ \t]*[<\"]([^\">]+)([\">])"
#define INCLUDE_REGEX_LINE_MARKER "#IncludeRegexLine: "
#define INCLUDE_REGEX_SCAN_MARKER "#IncludeRegexScan: "
#define INCLUDE_REGEX_COMPLAIN_MARKER "#IncludeRegexComplain: "
#define INCLUDE_REGEX_TRANSFORM_MARKER "#IncludeRegexTransform: "
2019-11-11 23:01:05 +01:00
cmDependsC::cmDependsC() = default;
2020-08-30 11:54:41 +02:00
cmDependsC::cmDependsC(cmLocalUnixMakefileGenerator3* lg,
const std::string& targetDir, const std::string& lang,
const DependencyMap* validDeps)
2016-07-09 11:21:54 +02:00
: cmDepends(lg, targetDir)
, ValidDeps(validDeps)
{
cmMakefile* mf = lg->GetMakefile();
// Configure the include file search path.
this->SetIncludePathFromLanguage(lang);
// Configure regular expressions.
std::string scanRegex = "^.*$";
std::string complainRegex = "^$";
{
2020-02-01 23:06:01 +01:00
std::string scanRegexVar = cmStrCat("CMAKE_", lang, "_INCLUDE_REGEX_SCAN");
2021-11-20 13:41:27 +01:00
if (cmValue sr = mf->GetDefinition(scanRegexVar)) {
2021-09-14 00:13:48 +02:00
scanRegex = *sr;
}
2020-02-01 23:06:01 +01:00
std::string complainRegexVar =
cmStrCat("CMAKE_", lang, "_INCLUDE_REGEX_COMPLAIN");
2021-11-20 13:41:27 +01:00
if (cmValue cr = mf->GetDefinition(complainRegexVar)) {
2021-09-14 00:13:48 +02:00
complainRegex = *cr;
}
}
this->IncludeRegexLine.compile(INCLUDE_REGEX_LINE);
2019-11-11 23:01:05 +01:00
this->IncludeRegexScan.compile(scanRegex);
this->IncludeRegexComplain.compile(complainRegex);
this->IncludeRegexLineString = INCLUDE_REGEX_LINE_MARKER INCLUDE_REGEX_LINE;
2020-02-01 23:06:01 +01:00
this->IncludeRegexScanString =
cmStrCat(INCLUDE_REGEX_SCAN_MARKER, scanRegex);
this->IncludeRegexComplainString =
cmStrCat(INCLUDE_REGEX_COMPLAIN_MARKER, complainRegex);
this->SetupTransforms();
2020-02-01 23:06:01 +01:00
this->CacheFileName =
cmStrCat(this->TargetDirectory, '/', lang, ".includecache");
this->ReadCacheFile();
}
cmDependsC::~cmDependsC()
{
this->WriteCacheFile();
}
2013-03-16 19:13:01 +02:00
bool cmDependsC::WriteDependencies(const std::set<std::string>& sources,
const std::string& obj,
std::ostream& makeDepends,
std::ostream& internalDepends)
{
// Make sure this is a scanning instance.
2016-07-09 11:21:54 +02:00
if (sources.empty() || sources.begin()->empty()) {
cmSystemTools::Error("Cannot scan dependencies without a source file.");
return false;
2016-07-09 11:21:54 +02:00
}
if (obj.empty()) {
cmSystemTools::Error("Cannot scan dependencies without an object file.");
return false;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
std::set<std::string> dependencies;
2013-03-16 19:13:01 +02:00
bool haveDeps = false;
2018-08-09 18:06:22 +02:00
// Compute a path to the object file to write to the internal depend file.
// Any existing content of the internal depend file has already been
// loaded in ValidDeps with this path as a key.
2021-09-14 00:13:48 +02:00
std::string obj_i = this->LocalGenerator->MaybeRelativeToTopBinDir(obj);
2018-08-09 18:06:22 +02:00
2024-11-11 15:18:55 +01:00
if (this->ValidDeps) {
2019-11-11 23:01:05 +01:00
auto const tmpIt = this->ValidDeps->find(obj_i);
2016-07-09 11:21:54 +02:00
if (tmpIt != this->ValidDeps->end()) {
2015-04-27 22:25:09 +02:00
dependencies.insert(tmpIt->second.begin(), tmpIt->second.end());
2013-03-16 19:13:01 +02:00
haveDeps = true;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
if (!haveDeps) {
2013-03-16 19:13:01 +02:00
// Walk the dependency graph starting with the source file.
2018-01-26 17:06:56 +01:00
int srcFiles = static_cast<int>(sources.size());
2013-03-16 19:13:01 +02:00
this->Encountered.clear();
2018-01-26 17:06:56 +01:00
for (std::string const& src : sources) {
2013-03-16 19:13:01 +02:00
UnscannedEntry root;
2018-01-26 17:06:56 +01:00
root.FileName = src;
2013-03-16 19:13:01 +02:00
this->Unscanned.push(root);
2018-01-26 17:06:56 +01:00
this->Encountered.insert(src);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
std::set<std::string> scanned;
2016-07-09 11:21:54 +02:00
while (!this->Unscanned.empty()) {
2013-03-16 19:13:01 +02:00
// Get the next file to scan.
UnscannedEntry current = this->Unscanned.front();
this->Unscanned.pop();
// If not a full path, find the file in the include path.
std::string fullName;
2018-04-23 21:13:27 +02:00
if ((srcFiles > 0) || cmSystemTools::FileIsFullPath(current.FileName)) {
if (cmSystemTools::FileExists(current.FileName, true)) {
2013-03-16 19:13:01 +02:00
fullName = current.FileName;
}
2016-07-09 11:21:54 +02:00
} else if (!current.QuotedLocation.empty() &&
2018-04-23 21:13:27 +02:00
cmSystemTools::FileExists(current.QuotedLocation, true)) {
2013-03-16 19:13:01 +02:00
// The include statement producing this entry was a double-quote
// include and the included file is present in the directory of
// the source containing the include statement.
fullName = current.QuotedLocation;
2016-07-09 11:21:54 +02:00
} else {
2019-11-11 23:01:05 +01:00
auto headerLocationIt =
2016-07-09 11:21:54 +02:00
this->HeaderLocationCache.find(current.FileName);
if (headerLocationIt != this->HeaderLocationCache.end()) {
fullName = headerLocationIt->second;
2016-10-30 18:24:19 +01:00
} else {
2019-11-11 23:01:05 +01:00
for (std::string const& iPath : this->IncludePath) {
2016-07-09 11:21:54 +02:00
// Construct the name of the file as if it were in the current
// include directory. Avoid using a leading "./".
2019-11-11 23:01:05 +01:00
std::string tmpPath =
cmSystemTools::CollapseFullPath(current.FileName, iPath);
2016-07-09 11:21:54 +02:00
// Look for the file in this location.
2019-11-11 23:01:05 +01:00
if (cmSystemTools::FileExists(tmpPath, true)) {
fullName = tmpPath;
this->HeaderLocationCache[current.FileName] = std::move(tmpPath);
2016-07-09 11:21:54 +02:00
break;
2013-03-16 19:13:01 +02:00
}
}
2016-10-30 18:24:19 +01:00
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
// Complain if the file cannot be found and matches the complain
// regex.
2016-07-09 11:21:54 +02:00
if (fullName.empty() &&
2018-10-28 12:09:07 +01:00
this->IncludeRegexComplain.find(current.FileName)) {
2019-11-11 23:01:05 +01:00
cmSystemTools::Error("Cannot find file \"" + current.FileName + "\".");
2013-03-16 19:13:01 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
// Scan the file if it was found and has not been scanned already.
2016-07-09 11:21:54 +02:00
if (!fullName.empty() && (scanned.find(fullName) == scanned.end())) {
2013-03-16 19:13:01 +02:00
// Record scanned files.
scanned.insert(fullName);
// Check whether this file is already in the cache
2019-11-11 23:01:05 +01:00
auto fileIt = this->FileCache.find(fullName);
2016-07-09 11:21:54 +02:00
if (fileIt != this->FileCache.end()) {
2020-02-01 23:06:01 +01:00
fileIt->second.Used = true;
2013-03-16 19:13:01 +02:00
dependencies.insert(fullName);
2020-02-01 23:06:01 +01:00
for (UnscannedEntry const& inc : fileIt->second.UnscannedEntries) {
2018-01-26 17:06:56 +01:00
if (this->Encountered.find(inc.FileName) ==
2016-07-09 11:21:54 +02:00
this->Encountered.end()) {
2018-01-26 17:06:56 +01:00
this->Encountered.insert(inc.FileName);
this->Unscanned.push(inc);
}
}
2016-07-09 11:21:54 +02:00
} else {
2013-03-16 19:13:01 +02:00
// Try to scan the file. Just leave it out if we cannot find
// it.
2014-08-03 19:52:23 +02:00
cmsys::ifstream fin(fullName.c_str());
2016-07-09 11:21:54 +02:00
if (fin) {
2015-08-17 11:37:30 +02:00
cmsys::FStream::BOM bom = cmsys::FStream::ReadBOM(fin);
2016-07-09 11:21:54 +02:00
if (bom == cmsys::FStream::BOM_None ||
bom == cmsys::FStream::BOM_UTF8) {
2015-08-17 11:37:30 +02:00
// Add this file as a dependency.
dependencies.insert(fullName);
2013-03-16 19:13:01 +02:00
2015-08-17 11:37:30 +02:00
// Scan this file for new dependencies. Pass the directory
// containing the file to handle double-quote includes.
std::string dir = cmSystemTools::GetFilenamePath(fullName);
2019-11-11 23:01:05 +01:00
this->Scan(fin, dir, fullName);
2016-07-09 11:21:54 +02:00
} else {
2015-08-17 11:37:30 +02:00
// Skip file with encoding we do not implement.
2013-03-16 19:13:01 +02:00
}
}
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
srcFiles--;
}
2016-07-09 11:21:54 +02:00
}
// Write the dependencies to the output stream. Makefile rules
// written by the original local generator for this directory
// convert the dependencies to paths relative to the home output
// directory. We must do the same here.
2020-08-30 11:54:41 +02:00
std::string obj_m = this->LocalGenerator->ConvertToMakefilePath(obj_i);
internalDepends << obj_i << '\n';
2021-09-14 00:13:48 +02:00
if (!dependencies.empty()) {
const auto& lineContinue = static_cast<cmGlobalUnixMakefileGenerator3*>(
this->LocalGenerator->GetGlobalGenerator())
->LineContinueDirective;
bool supportLongLineDepend = static_cast<cmGlobalUnixMakefileGenerator3*>(
this->LocalGenerator->GetGlobalGenerator())
->SupportsLongLineDependencies();
if (supportLongLineDepend) {
makeDepends << obj_m << ':';
}
for (std::string const& dep : dependencies) {
std::string dependee = this->LocalGenerator->ConvertToMakefilePath(
this->LocalGenerator->MaybeRelativeToTopBinDir(dep));
if (supportLongLineDepend) {
makeDepends << ' ' << lineContinue << ' ' << dependee;
} else {
makeDepends << obj_m << ": " << dependee << '\n';
}
internalDepends << ' ' << dep << '\n';
}
makeDepends << '\n';
2016-07-09 11:21:54 +02:00
}
return true;
}
void cmDependsC::ReadCacheFile()
{
2016-07-09 11:21:54 +02:00
if (this->CacheFileName.empty()) {
return;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
cmsys::ifstream fin(this->CacheFileName.c_str());
2016-07-09 11:21:54 +02:00
if (!fin) {
return;
2016-07-09 11:21:54 +02:00
}
std::string line;
2018-01-26 17:06:56 +01:00
cmIncludeLines* cacheEntry = nullptr;
2016-07-09 11:21:54 +02:00
bool haveFileName = false;
2019-11-11 23:01:05 +01:00
cmFileTime cacheFileTime;
bool const cacheFileTimeGood = cacheFileTime.Load(this->CacheFileName);
2016-07-09 11:21:54 +02:00
while (cmSystemTools::GetLineFromStream(fin, line)) {
if (line.empty()) {
2018-01-26 17:06:56 +01:00
cacheEntry = nullptr;
2016-07-09 11:21:54 +02:00
haveFileName = false;
continue;
2016-07-09 11:21:54 +02:00
}
// the first line after an empty line is the name of the parsed file
2016-10-30 18:24:19 +01:00
if (!haveFileName) {
2016-07-09 11:21:54 +02:00
haveFileName = true;
2012-02-18 12:40:36 +02:00
2019-11-11 23:01:05 +01:00
cmFileTime fileTime;
bool const res = cacheFileTimeGood && fileTime.Load(line);
bool const newer = res && cacheFileTime.Newer(fileTime);
if (res && newer) // cache is newer than the parsed file
2016-07-09 11:21:54 +02:00
{
2020-02-01 23:06:01 +01:00
cacheEntry = &this->FileCache[line];
2016-07-09 11:21:54 +02:00
}
// file doesn't exist, check that the regular expressions
// haven't changed
2016-10-30 18:24:19 +01:00
else if (!res) {
2020-08-30 11:54:41 +02:00
if (cmHasLiteralPrefix(line, INCLUDE_REGEX_LINE_MARKER)) {
2016-07-09 11:21:54 +02:00
if (line != this->IncludeRegexLineString) {
return;
}
2020-08-30 11:54:41 +02:00
} else if (cmHasLiteralPrefix(line, INCLUDE_REGEX_SCAN_MARKER)) {
2016-07-09 11:21:54 +02:00
if (line != this->IncludeRegexScanString) {
return;
}
2020-08-30 11:54:41 +02:00
} else if (cmHasLiteralPrefix(line, INCLUDE_REGEX_COMPLAIN_MARKER)) {
2016-07-09 11:21:54 +02:00
if (line != this->IncludeRegexComplainString) {
return;
}
2020-08-30 11:54:41 +02:00
} else if (cmHasLiteralPrefix(line, INCLUDE_REGEX_TRANSFORM_MARKER)) {
2016-07-09 11:21:54 +02:00
if (line != this->IncludeRegexTransformString) {
return;
}
}
}
2024-11-11 15:18:55 +01:00
} else if (cacheEntry) {
UnscannedEntry entry;
entry.FileName = line;
2016-07-09 11:21:54 +02:00
if (cmSystemTools::GetLineFromStream(fin, line)) {
if (line != "-") {
entry.QuotedLocation = line;
}
2018-04-23 21:13:27 +02:00
cacheEntry->UnscannedEntries.push_back(std::move(entry));
}
}
2016-07-09 11:21:54 +02:00
}
}
void cmDependsC::WriteCacheFile() const
{
2016-07-09 11:21:54 +02:00
if (this->CacheFileName.empty()) {
return;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
cmsys::ofstream cacheOut(this->CacheFileName.c_str());
2016-07-09 11:21:54 +02:00
if (!cacheOut) {
return;
2016-07-09 11:21:54 +02:00
}
2012-02-18 12:40:36 +02:00
cacheOut << this->IncludeRegexLineString << "\n\n";
cacheOut << this->IncludeRegexScanString << "\n\n";
cacheOut << this->IncludeRegexComplainString << "\n\n";
cacheOut << this->IncludeRegexTransformString << "\n\n";
2018-01-26 17:06:56 +01:00
for (auto const& fileIt : this->FileCache) {
2020-02-01 23:06:01 +01:00
if (fileIt.second.Used) {
2020-08-30 11:54:41 +02:00
cacheOut << fileIt.first << '\n';
2018-01-26 17:06:56 +01:00
2020-02-01 23:06:01 +01:00
for (UnscannedEntry const& inc : fileIt.second.UnscannedEntries) {
2020-08-30 11:54:41 +02:00
cacheOut << inc.FileName << '\n';
2018-01-26 17:06:56 +01:00
if (inc.QuotedLocation.empty()) {
2020-08-30 11:54:41 +02:00
cacheOut << '-' << '\n';
2016-07-09 11:21:54 +02:00
} else {
2020-08-30 11:54:41 +02:00
cacheOut << inc.QuotedLocation << '\n';
}
}
2020-08-30 11:54:41 +02:00
cacheOut << '\n';
2016-07-09 11:21:54 +02:00
}
}
}
2019-11-11 23:01:05 +01:00
void cmDependsC::Scan(std::istream& is, const std::string& directory,
2016-07-09 11:21:54 +02:00
const std::string& fullName)
{
2020-02-01 23:06:01 +01:00
cmIncludeLines& newCacheEntry = this->FileCache[fullName];
newCacheEntry.Used = true;
2012-02-18 12:40:36 +02:00
// Read one line at a time.
std::string line;
2016-07-09 11:21:54 +02:00
while (cmSystemTools::GetLineFromStream(is, line)) {
// Transform the line content first.
2016-07-09 11:21:54 +02:00
if (!this->TransformRules.empty()) {
this->TransformLine(line);
2016-07-09 11:21:54 +02:00
}
// Match include directives.
2018-10-28 12:09:07 +01:00
if (this->IncludeRegexLine.find(line)) {
// Get the file being included.
UnscannedEntry entry;
entry.FileName = this->IncludeRegexLine.match(2);
2011-02-07 16:37:25 +01:00
cmSystemTools::ConvertToUnixSlashes(entry.FileName);
2016-07-09 11:21:54 +02:00
if (this->IncludeRegexLine.match(3) == "\"" &&
2018-04-23 21:13:27 +02:00
!cmSystemTools::FileIsFullPath(entry.FileName)) {
// This was a double-quoted include with a relative path. We
// must check for the file in the directory containing the
// file we are scanning.
2013-11-03 12:27:13 +02:00
entry.QuotedLocation =
2019-11-11 23:01:05 +01:00
cmSystemTools::CollapseFullPath(entry.FileName, directory);
2016-07-09 11:21:54 +02:00
}
// Queue the file if it has not yet been encountered and it
// matches the regular expression for recursive scanning. Note
// that this check does not account for the possibility of two
// headers with the same name in different directories when one
// is included by double-quotes and the other by angle brackets.
2012-02-18 12:40:36 +02:00
// It also does not work properly if two header files with the same
// name exist in different directories, and both are included from a
// file their own directory by simply using "filename.h" (#12619)
// This kind of problem will be fixed when a more
// preprocessor-like implementation of this scanner is created.
2018-10-28 12:09:07 +01:00
if (this->IncludeRegexScan.find(entry.FileName)) {
2020-02-01 23:06:01 +01:00
newCacheEntry.UnscannedEntries.push_back(entry);
2016-07-09 11:21:54 +02:00
if (this->Encountered.find(entry.FileName) ==
this->Encountered.end()) {
this->Encountered.insert(entry.FileName);
this->Unscanned.push(entry);
}
}
}
2016-07-09 11:21:54 +02:00
}
}
void cmDependsC::SetupTransforms()
{
// Get the transformation rules.
cmMakefile* mf = this->LocalGenerator->GetMakefile();
2023-07-02 19:51:09 +02:00
cmList transformRules{ mf->GetDefinition("CMAKE_INCLUDE_TRANSFORMS"),
cmList::EmptyElements::Yes };
for (auto const& tr : transformRules) {
2018-01-26 17:06:56 +01:00
this->ParseTransform(tr);
2016-07-09 11:21:54 +02:00
}
this->IncludeRegexTransformString = INCLUDE_REGEX_TRANSFORM_MARKER;
2016-07-09 11:21:54 +02:00
if (!this->TransformRules.empty()) {
// Construct the regular expression to match lines to be
// transformed.
2017-04-14 19:02:05 +02:00
std::string xform = "^([ \t]*[#%][ \t]*(include|import)[ \t]*)(";
const char* sep = "";
2018-01-26 17:06:56 +01:00
for (auto const& tr : this->TransformRules) {
xform += sep;
2018-01-26 17:06:56 +01:00
xform += tr.first;
sep = "|";
2016-07-09 11:21:54 +02:00
}
xform += ")[ \t]*\\(([^),]*)\\)";
2019-11-11 23:01:05 +01:00
this->IncludeRegexTransform.compile(xform);
// Build a string that encodes all transformation rules and will
// change when rules are changed.
this->IncludeRegexTransformString += xform;
2018-01-26 17:06:56 +01:00
for (auto const& tr : this->TransformRules) {
this->IncludeRegexTransformString += " ";
2018-01-26 17:06:56 +01:00
this->IncludeRegexTransformString += tr.first;
this->IncludeRegexTransformString += "(%)=";
2018-01-26 17:06:56 +01:00
this->IncludeRegexTransformString += tr.second;
}
2016-07-09 11:21:54 +02:00
}
}
void cmDependsC::ParseTransform(std::string const& xform)
{
// A transform rule is of the form SOME_MACRO(%)=value-with-%
// We can simply separate with "(%)=".
std::string::size_type pos = xform.find("(%)=");
2017-07-20 19:35:53 +02:00
if (pos == std::string::npos || pos == 0) {
return;
2016-07-09 11:21:54 +02:00
}
std::string name = xform.substr(0, pos);
2017-07-20 19:35:53 +02:00
std::string value = xform.substr(pos + 4);
this->TransformRules[name] = value;
}
void cmDependsC::TransformLine(std::string& line)
{
// Check for a transform rule match. Return if none.
2018-10-28 12:09:07 +01:00
if (!this->IncludeRegexTransform.find(line)) {
return;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
auto tri = this->TransformRules.find(this->IncludeRegexTransform.match(3));
2016-07-09 11:21:54 +02:00
if (tri == this->TransformRules.end()) {
return;
2016-07-09 11:21:54 +02:00
}
// Construct the transformed line.
std::string newline = this->IncludeRegexTransform.match(1);
std::string arg = this->IncludeRegexTransform.match(4);
2019-11-11 23:01:05 +01:00
for (char c : tri->second) {
if (c == '%') {
newline += arg;
2016-07-09 11:21:54 +02:00
} else {
2019-11-11 23:01:05 +01:00
newline += c;
}
2016-07-09 11:21:54 +02:00
}
// Return the transformed line.
line = newline;
}