cmake/Source/cmSourceFileLocation.cxx

230 lines
7.6 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 "cmSourceFileLocation.h"
2016-07-09 11:21:54 +02:00
#include "cmAlgorithms.h"
#include "cmGlobalGenerator.h"
2016-07-09 11:21:54 +02:00
#include "cmMakefile.h"
#include "cmSystemTools.h"
2016-10-30 18:24:19 +01:00
#include "cmake.h"
2016-10-30 18:24:19 +01:00
#include <assert.h>
2015-04-27 22:25:09 +02:00
cmSourceFileLocation::cmSourceFileLocation()
2018-01-26 17:06:56 +01:00
: Makefile(nullptr)
2016-07-09 11:21:54 +02:00
, AmbiguousDirectory(true)
, AmbiguousExtension(true)
{
}
2015-04-27 22:25:09 +02:00
cmSourceFileLocation::cmSourceFileLocation(const cmSourceFileLocation& loc)
: Makefile(loc.Makefile)
{
2015-04-27 22:25:09 +02:00
this->AmbiguousDirectory = loc.AmbiguousDirectory;
this->AmbiguousExtension = loc.AmbiguousExtension;
this->Directory = loc.Directory;
this->Name = loc.Name;
}
2016-07-09 11:21:54 +02:00
cmSourceFileLocation::cmSourceFileLocation(cmMakefile const* mf,
2018-04-23 21:13:27 +02:00
const std::string& name,
cmSourceFileLocationKind kind)
2015-04-27 22:25:09 +02:00
: Makefile(mf)
{
2018-04-23 21:13:27 +02:00
this->AmbiguousDirectory = !cmSystemTools::FileIsFullPath(name);
2015-04-27 22:25:09 +02:00
this->AmbiguousExtension = true;
this->Directory = cmSystemTools::GetFilenamePath(name);
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileIsFullPath(this->Directory)) {
2016-07-09 11:21:54 +02:00
this->Directory = cmSystemTools::CollapseFullPath(this->Directory);
}
2015-04-27 22:25:09 +02:00
this->Name = cmSystemTools::GetFilenameName(name);
2018-04-23 21:13:27 +02:00
if (kind == cmSourceFileLocationKind::Known) {
this->DirectoryUseSource();
this->AmbiguousExtension = false;
} else {
this->UpdateExtension(name);
}
}
void cmSourceFileLocation::Update(cmSourceFileLocation const& loc)
{
2016-07-09 11:21:54 +02:00
if (this->AmbiguousDirectory && !loc.AmbiguousDirectory) {
this->Directory = loc.Directory;
this->AmbiguousDirectory = false;
2016-07-09 11:21:54 +02:00
}
if (this->AmbiguousExtension && !loc.AmbiguousExtension) {
this->Name = loc.Name;
this->AmbiguousExtension = false;
2016-07-09 11:21:54 +02:00
}
}
void cmSourceFileLocation::DirectoryUseSource()
{
2015-04-27 22:25:09 +02:00
assert(this->Makefile);
2016-07-09 11:21:54 +02:00
if (this->AmbiguousDirectory) {
this->Directory = cmSystemTools::CollapseFullPath(
this->Directory, this->Makefile->GetCurrentSourceDirectory());
this->AmbiguousDirectory = false;
2016-07-09 11:21:54 +02:00
}
}
void cmSourceFileLocation::DirectoryUseBinary()
{
2015-04-27 22:25:09 +02:00
assert(this->Makefile);
2016-07-09 11:21:54 +02:00
if (this->AmbiguousDirectory) {
this->Directory = cmSystemTools::CollapseFullPath(
this->Directory, this->Makefile->GetCurrentBinaryDirectory());
this->AmbiguousDirectory = false;
2016-07-09 11:21:54 +02:00
}
}
2015-04-27 22:25:09 +02:00
void cmSourceFileLocation::UpdateExtension(const std::string& name)
{
2015-04-27 22:25:09 +02:00
assert(this->Makefile);
// Check the extension.
std::string ext = cmSystemTools::GetFilenameLastExtension(name);
2016-07-09 11:21:54 +02:00
if (!ext.empty()) {
ext = ext.substr(1);
}
// The global generator checks extensions of enabled languages.
2015-08-17 11:37:30 +02:00
cmGlobalGenerator* gg = this->Makefile->GetGlobalGenerator();
2014-08-03 19:52:23 +02:00
cmMakefile const* mf = this->Makefile;
2018-04-23 21:13:27 +02:00
auto cm = mf->GetCMakeInstance();
2016-07-09 11:21:54 +02:00
if (!gg->GetLanguageFromExtension(ext.c_str()).empty() ||
2018-04-23 21:13:27 +02:00
cm->IsSourceExtension(ext) || cm->IsHeaderExtension(ext)) {
// This is a known extension. Use the given filename with extension.
this->Name = cmSystemTools::GetFilenameName(name);
this->AmbiguousExtension = false;
2016-07-09 11:21:54 +02:00
} else {
// This is not a known extension. See if the file exists on disk as
// named.
std::string tryPath;
2016-07-09 11:21:54 +02:00
if (this->AmbiguousDirectory) {
// Check the source tree only because a file in the build tree should
// be specified by full path at least once. We do not want this
// detection to depend on whether the project has already been built.
2015-08-17 11:37:30 +02:00
tryPath = this->Makefile->GetCurrentSourceDirectory();
tryPath += "/";
2016-07-09 11:21:54 +02:00
}
if (!this->Directory.empty()) {
tryPath += this->Directory;
tryPath += "/";
2016-07-09 11:21:54 +02:00
}
tryPath += this->Name;
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(tryPath, true)) {
// We found a source file named by the user on disk. Trust it's
// extension.
this->Name = cmSystemTools::GetFilenameName(name);
this->AmbiguousExtension = false;
// If the directory was ambiguous, it isn't anymore.
2016-07-09 11:21:54 +02:00
if (this->AmbiguousDirectory) {
this->DirectoryUseSource();
}
}
2016-07-09 11:21:54 +02:00
}
}
2016-07-09 11:21:54 +02:00
bool cmSourceFileLocation::MatchesAmbiguousExtension(
cmSourceFileLocation const& loc) const
{
2015-04-27 22:25:09 +02:00
assert(this->Makefile);
// This location's extension is not ambiguous but loc's extension
// is. See if the names match as-is.
2016-07-09 11:21:54 +02:00
if (this->Name == loc.Name) {
return true;
2016-07-09 11:21:54 +02:00
}
// Check if loc's name could possibly be extended to our name by
// adding an extension.
2016-07-09 11:21:54 +02:00
if (!(this->Name.size() > loc.Name.size() &&
this->Name[loc.Name.size()] == '.' &&
cmHasLiteralPrefixImpl(this->Name.c_str(), loc.Name.c_str(),
loc.Name.size()))) {
return false;
2016-07-09 11:21:54 +02:00
}
// Only a fixed set of extensions will be tried to match a file on
// disk. One of these must match if loc refers to this source file.
2016-07-09 11:21:54 +02:00
std::string const& ext = this->Name.substr(loc.Name.size() + 1);
2014-08-03 19:52:23 +02:00
cmMakefile const* mf = this->Makefile;
2018-04-23 21:13:27 +02:00
auto cm = mf->GetCMakeInstance();
return cm->IsSourceExtension(ext) || cm->IsHeaderExtension(ext);
}
bool cmSourceFileLocation::Matches(cmSourceFileLocation const& loc)
{
2015-04-27 22:25:09 +02:00
assert(this->Makefile);
2016-07-09 11:21:54 +02:00
if (this->AmbiguousExtension == loc.AmbiguousExtension) {
2015-04-27 22:25:09 +02:00
// Both extensions are similarly ambiguous. Since only the old fixed set
// of extensions will be tried, the names must match at this point to be
// the same file.
2016-07-09 11:21:54 +02:00
if (this->Name.size() != loc.Name.size() ||
!cmSystemTools::ComparePath(this->Name, loc.Name)) {
return false;
}
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
const cmSourceFileLocation* loc1;
const cmSourceFileLocation* loc2;
2016-07-09 11:21:54 +02:00
if (this->AmbiguousExtension) {
2015-04-27 22:25:09 +02:00
// Only "this" extension is ambiguous.
loc1 = &loc;
loc2 = this;
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
// Only "loc" extension is ambiguous.
loc1 = this;
loc2 = &loc;
2016-07-09 11:21:54 +02:00
}
if (!loc1->MatchesAmbiguousExtension(*loc2)) {
return false;
}
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (!this->AmbiguousDirectory && !loc.AmbiguousDirectory) {
// Both sides have absolute directories.
2016-07-09 11:21:54 +02:00
if (this->Directory != loc.Directory) {
return false;
}
2016-07-09 11:21:54 +02:00
} else if (this->AmbiguousDirectory && loc.AmbiguousDirectory) {
if (this->Makefile == loc.Makefile) {
2015-04-27 22:25:09 +02:00
// Both sides have directories relative to the same location.
2016-07-09 11:21:54 +02:00
if (this->Directory != loc.Directory) {
2015-04-27 22:25:09 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
// Each side has a directory relative to a different location.
// This can occur when referencing a source file from a different
// directory. This is not yet allowed.
this->Makefile->IssueMessage(
cmake::INTERNAL_ERROR,
"Matches error: Each side has a directory relative to a different "
"location. This can occur when referencing a source file from a "
2016-07-09 11:21:54 +02:00
"different directory. This is not yet allowed.");
return false;
}
2016-07-09 11:21:54 +02:00
} else if (this->AmbiguousDirectory) {
// Compare possible directory combinations.
2016-07-09 11:21:54 +02:00
std::string const& srcDir = cmSystemTools::CollapseFullPath(
this->Directory, this->Makefile->GetCurrentSourceDirectory());
std::string const& binDir = cmSystemTools::CollapseFullPath(
this->Directory, this->Makefile->GetCurrentBinaryDirectory());
if (srcDir != loc.Directory && binDir != loc.Directory) {
return false;
}
2016-07-09 11:21:54 +02:00
} else if (loc.AmbiguousDirectory) {
// Compare possible directory combinations.
2016-07-09 11:21:54 +02:00
std::string const& srcDir = cmSystemTools::CollapseFullPath(
loc.Directory, loc.Makefile->GetCurrentSourceDirectory());
std::string const& binDir = cmSystemTools::CollapseFullPath(
loc.Directory, loc.Makefile->GetCurrentBinaryDirectory());
if (srcDir != this->Directory && binDir != this->Directory) {
return false;
}
2016-07-09 11:21:54 +02:00
}
// File locations match.
this->Update(loc);
return true;
}