2016-09-27 19:01:08 +00:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2008-02-21 16:41:11 +00:00
|
|
|
#include "cmOrderDirectories.h"
|
|
|
|
|
2016-04-29 14:53:13 +00:00
|
|
|
#include "cmAlgorithms.h"
|
2016-09-01 18:59:28 +00:00
|
|
|
#include "cmGeneratorTarget.h"
|
2008-02-21 16:41:11 +00:00
|
|
|
#include "cmGlobalGenerator.h"
|
|
|
|
#include "cmSystemTools.h"
|
2008-07-29 18:01:04 +00:00
|
|
|
#include "cmake.h"
|
2008-02-21 16:41:11 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2016-09-01 18:59:28 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <functional>
|
|
|
|
#include <sstream>
|
2008-02-21 16:41:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Directory ordering computation.
|
|
|
|
- Useful to compute a safe runtime library path order
|
|
|
|
- Need runtime path for supporting INSTALL_RPATH_USE_LINK_PATH
|
|
|
|
- Need runtime path at link time to pickup transitive link dependencies
|
|
|
|
for shared libraries.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class cmOrderDirectoriesConstraint
|
|
|
|
{
|
|
|
|
public:
|
2016-05-16 14:34:04 +00:00
|
|
|
cmOrderDirectoriesConstraint(cmOrderDirectories* od, std::string const& file)
|
|
|
|
: OD(od)
|
|
|
|
, GlobalGenerator(od->GlobalGenerator)
|
|
|
|
{
|
2008-02-21 16:41:11 +00:00
|
|
|
this->FullPath = file;
|
2013-04-27 04:04:44 +00:00
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
if (file.rfind(".framework") != std::string::npos) {
|
|
|
|
static cmsys::RegularExpression splitFramework(
|
|
|
|
"^(.*)/(.*).framework/(.*)$");
|
|
|
|
if (splitFramework.find(file) &&
|
|
|
|
(std::string::npos !=
|
|
|
|
splitFramework.match(3).find(splitFramework.match(2)))) {
|
2013-04-27 04:04:44 +00:00
|
|
|
this->Directory = splitFramework.match(1);
|
|
|
|
this->FileName =
|
|
|
|
std::string(file.begin() + this->Directory.size() + 1, file.end());
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-04-27 04:04:44 +00:00
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->FileName.empty()) {
|
2013-04-27 04:04:44 +00:00
|
|
|
this->Directory = cmSystemTools::GetFilenamePath(file);
|
|
|
|
this->FileName = cmSystemTools::GetFilenameName(file);
|
2008-02-21 16:41:11 +00:00
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
virtual ~cmOrderDirectoriesConstraint() {}
|
|
|
|
|
|
|
|
void AddDirectory()
|
2016-05-16 14:34:04 +00:00
|
|
|
{
|
2008-02-21 16:41:11 +00:00
|
|
|
this->DirectoryIndex = this->OD->AddOriginalDirectory(this->Directory);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
|
|
|
|
virtual void Report(std::ostream& e) = 0;
|
|
|
|
|
|
|
|
void FindConflicts(unsigned int index)
|
2016-05-16 14:34:04 +00:00
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < this->OD->OriginalDirectories.size(); ++i) {
|
2008-07-29 18:00:59 +00:00
|
|
|
// Check if this directory conflicts with the entry.
|
2008-02-21 16:41:11 +00:00
|
|
|
std::string const& dir = this->OD->OriginalDirectories[i];
|
2015-12-01 15:44:48 +00:00
|
|
|
if (!this->OD->IsSameDirectory(dir, this->Directory) &&
|
2016-05-16 14:34:04 +00:00
|
|
|
this->FindConflict(dir)) {
|
2008-02-21 16:41:11 +00:00
|
|
|
// The library will be found in this directory but this is not
|
|
|
|
// the directory named for it. Add an entry to make sure the
|
|
|
|
// desired directory comes before this one.
|
|
|
|
cmOrderDirectories::ConflictPair p(this->DirectoryIndex, index);
|
|
|
|
this->OD->ConflictGraph[i].push_back(p);
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-07-29 18:57:00 +00:00
|
|
|
|
2015-01-05 19:31:31 +00:00
|
|
|
void FindImplicitConflicts(std::ostringstream& w)
|
2016-05-16 14:34:04 +00:00
|
|
|
{
|
2008-07-29 18:57:00 +00:00
|
|
|
bool first = true;
|
2016-05-16 14:34:04 +00:00
|
|
|
for (unsigned int i = 0; i < this->OD->OriginalDirectories.size(); ++i) {
|
2008-07-29 18:57:00 +00:00
|
|
|
// Check if this directory conflicts with the entry.
|
|
|
|
std::string const& dir = this->OD->OriginalDirectories[i];
|
2016-05-16 14:34:04 +00:00
|
|
|
if (dir != this->Directory &&
|
|
|
|
cmSystemTools::GetRealPath(dir) !=
|
|
|
|
cmSystemTools::GetRealPath(this->Directory) &&
|
|
|
|
this->FindConflict(dir)) {
|
2008-07-29 18:57:00 +00:00
|
|
|
// The library will be found in this directory but it is
|
|
|
|
// supposed to be found in an implicit search directory.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (first) {
|
2008-07-29 18:57:00 +00:00
|
|
|
first = false;
|
|
|
|
w << " ";
|
|
|
|
this->Report(w);
|
|
|
|
w << " in " << this->Directory << " may be hidden by files in:\n";
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
w << " " << dir << "\n";
|
2008-07-29 18:57:00 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
|
|
|
|
2008-02-21 16:41:11 +00:00
|
|
|
protected:
|
|
|
|
virtual bool FindConflict(std::string const& dir) = 0;
|
|
|
|
|
|
|
|
bool FileMayConflict(std::string const& dir, std::string const& name);
|
|
|
|
|
|
|
|
cmOrderDirectories* OD;
|
|
|
|
cmGlobalGenerator* GlobalGenerator;
|
|
|
|
|
|
|
|
// The location in which the item is supposed to be found.
|
|
|
|
std::string FullPath;
|
|
|
|
std::string Directory;
|
|
|
|
std::string FileName;
|
|
|
|
|
|
|
|
// The index assigned to the directory.
|
|
|
|
int DirectoryIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool cmOrderDirectoriesConstraint::FileMayConflict(std::string const& dir,
|
|
|
|
std::string const& name)
|
|
|
|
{
|
2008-12-26 18:28:20 +00:00
|
|
|
// Check if the file exists on disk.
|
2008-02-21 16:41:11 +00:00
|
|
|
std::string file = dir;
|
|
|
|
file += "/";
|
|
|
|
file += name;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (cmSystemTools::FileExists(file.c_str(), true)) {
|
2008-12-26 18:28:20 +00:00
|
|
|
// The file conflicts only if it is not the same as the original
|
|
|
|
// file due to a symlink or hardlink.
|
2014-10-15 12:54:05 +00:00
|
|
|
return !cmSystemTools::SameFile(this->FullPath, file);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-12-26 18:28:20 +00:00
|
|
|
|
|
|
|
// Check if the file will be built by cmake.
|
2014-02-10 05:21:34 +00:00
|
|
|
std::set<std::string> const& files =
|
2008-12-26 18:28:20 +00:00
|
|
|
(this->GlobalGenerator->GetDirectoryContent(dir, false));
|
2014-02-10 05:21:34 +00:00
|
|
|
std::set<std::string>::const_iterator fi = files.find(name);
|
2008-12-26 18:28:20 +00:00
|
|
|
return fi != files.end();
|
2008-02-21 16:41:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
class cmOrderDirectoriesConstraintSOName : public cmOrderDirectoriesConstraint
|
2008-02-21 16:41:11 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
cmOrderDirectoriesConstraintSOName(cmOrderDirectories* od,
|
|
|
|
std::string const& file,
|
2016-05-16 14:34:04 +00:00
|
|
|
const char* soname)
|
|
|
|
: cmOrderDirectoriesConstraint(od, file)
|
|
|
|
, SOName(soname ? soname : "")
|
|
|
|
{
|
|
|
|
if (this->SOName.empty()) {
|
2008-02-21 18:58:41 +00:00
|
|
|
// Try to guess the soname.
|
|
|
|
std::string soguess;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (cmSystemTools::GuessLibrarySOName(file, soguess)) {
|
2008-02-21 18:58:41 +00:00
|
|
|
this->SOName = soguess;
|
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
|
2016-06-22 23:02:39 +00:00
|
|
|
void Report(std::ostream& e) CM_OVERRIDE
|
2016-05-16 14:34:04 +00:00
|
|
|
{
|
2008-02-21 16:41:11 +00:00
|
|
|
e << "runtime library [";
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->SOName.empty()) {
|
2008-02-21 16:41:11 +00:00
|
|
|
e << this->FileName;
|
2016-05-16 14:34:04 +00:00
|
|
|
} else {
|
2008-02-21 16:41:11 +00:00
|
|
|
e << this->SOName;
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
e << "]";
|
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
|
2016-06-22 23:02:39 +00:00
|
|
|
bool FindConflict(std::string const& dir) CM_OVERRIDE;
|
2016-05-16 14:34:04 +00:00
|
|
|
|
2008-02-21 16:41:11 +00:00
|
|
|
private:
|
|
|
|
// The soname of the shared library if it is known.
|
|
|
|
std::string SOName;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool cmOrderDirectoriesConstraintSOName::FindConflict(std::string const& dir)
|
|
|
|
{
|
|
|
|
// Determine which type of check to do.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!this->SOName.empty()) {
|
2008-02-21 16:41:11 +00:00
|
|
|
// We have the library soname. Check if it will be found.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->FileMayConflict(dir, this->SOName)) {
|
2008-02-21 16:41:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
} else {
|
2008-02-21 16:41:11 +00:00
|
|
|
// We do not have the soname. Look for files in the directory
|
|
|
|
// that may conflict.
|
2014-02-10 05:21:34 +00:00
|
|
|
std::set<std::string> const& files =
|
2016-05-16 14:34:04 +00:00
|
|
|
(this->GlobalGenerator->GetDirectoryContent(dir, true));
|
2008-02-21 16:41:11 +00:00
|
|
|
|
|
|
|
// Get the set of files that might conflict. Since we do not
|
|
|
|
// know the soname just look at all files that start with the
|
|
|
|
// file name. Usually the soname starts with the library name.
|
|
|
|
std::string base = this->FileName;
|
2014-02-10 05:21:34 +00:00
|
|
|
std::set<std::string>::const_iterator first = files.lower_bound(base);
|
2016-05-16 14:34:04 +00:00
|
|
|
++base[base.size() - 1];
|
2014-02-10 05:21:34 +00:00
|
|
|
std::set<std::string>::const_iterator last = files.upper_bound(base);
|
2016-05-16 14:34:04 +00:00
|
|
|
if (first != last) {
|
2008-02-21 16:41:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
class cmOrderDirectoriesConstraintLibrary : public cmOrderDirectoriesConstraint
|
2008-02-21 16:41:11 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
cmOrderDirectoriesConstraintLibrary(cmOrderDirectories* od,
|
2016-05-16 14:34:04 +00:00
|
|
|
std::string const& file)
|
|
|
|
: cmOrderDirectoriesConstraint(od, file)
|
|
|
|
{
|
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
|
2016-06-22 23:02:39 +00:00
|
|
|
void Report(std::ostream& e) CM_OVERRIDE
|
2016-05-16 14:34:04 +00:00
|
|
|
{
|
2008-02-21 16:41:11 +00:00
|
|
|
e << "link library [" << this->FileName << "]";
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
|
2016-06-22 23:02:39 +00:00
|
|
|
bool FindConflict(std::string const& dir) CM_OVERRIDE;
|
2008-02-21 16:41:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool cmOrderDirectoriesConstraintLibrary::FindConflict(std::string const& dir)
|
|
|
|
{
|
|
|
|
// We have the library file name. Check if it will be found.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->FileMayConflict(dir, this->FileName)) {
|
2008-02-21 16:41:11 +00:00
|
|
|
return true;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
|
|
|
|
// Now check if the file exists with other extensions the linker
|
|
|
|
// might consider.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!this->OD->LinkExtensions.empty() &&
|
|
|
|
this->OD->RemoveLibraryExtension.find(this->FileName)) {
|
2014-02-10 05:21:34 +00:00
|
|
|
std::string lib = this->OD->RemoveLibraryExtension.match(1);
|
|
|
|
std::string ext = this->OD->RemoveLibraryExtension.match(2);
|
2016-05-16 14:34:04 +00:00
|
|
|
for (std::vector<std::string>::iterator i =
|
|
|
|
this->OD->LinkExtensions.begin();
|
|
|
|
i != this->OD->LinkExtensions.end(); ++i) {
|
|
|
|
if (*i != ext) {
|
2008-02-21 16:41:11 +00:00
|
|
|
std::string fname = lib;
|
|
|
|
fname += *i;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->FileMayConflict(dir, fname)) {
|
2008-02-21 16:41:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmOrderDirectories::cmOrderDirectories(cmGlobalGenerator* gg,
|
2015-08-04 17:19:51 +00:00
|
|
|
const cmGeneratorTarget* target,
|
2008-02-21 16:41:11 +00:00
|
|
|
const char* purpose)
|
|
|
|
{
|
|
|
|
this->GlobalGenerator = gg;
|
2008-07-29 18:01:04 +00:00
|
|
|
this->Target = target;
|
2008-02-21 16:41:11 +00:00
|
|
|
this->Purpose = purpose;
|
|
|
|
this->Computed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmOrderDirectories::~cmOrderDirectories()
|
|
|
|
{
|
2015-01-04 12:33:16 +00:00
|
|
|
cmDeleteAll(this->ConstraintEntries);
|
|
|
|
cmDeleteAll(this->ImplicitDirEntries);
|
2008-02-21 16:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> const& cmOrderDirectories::GetOrderedDirectories()
|
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!this->Computed) {
|
2008-02-21 16:41:11 +00:00
|
|
|
this->Computed = true;
|
|
|
|
this->CollectOriginalDirectories();
|
|
|
|
this->FindConflicts();
|
|
|
|
this->OrderDirectories();
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
return this->OrderedDirectories;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmOrderDirectories::AddRuntimeLibrary(std::string const& fullPath,
|
|
|
|
const char* soname)
|
|
|
|
{
|
|
|
|
// Add the runtime library at most once.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->EmmittedConstraintSOName.insert(fullPath).second) {
|
2008-07-29 18:57:00 +00:00
|
|
|
// Implicit link directories need special handling.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!this->ImplicitDirectories.empty()) {
|
2008-02-21 16:41:11 +00:00
|
|
|
std::string dir = cmSystemTools::GetFilenamePath(fullPath);
|
2013-04-27 04:04:44 +00:00
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
if (fullPath.rfind(".framework") != std::string::npos) {
|
|
|
|
static cmsys::RegularExpression splitFramework(
|
|
|
|
"^(.*)/(.*).framework/(.*)$");
|
|
|
|
if (splitFramework.find(fullPath) &&
|
|
|
|
(std::string::npos !=
|
|
|
|
splitFramework.match(3).find(splitFramework.match(2)))) {
|
2013-04-27 04:04:44 +00:00
|
|
|
dir = splitFramework.match(1);
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-04-27 04:04:44 +00:00
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->ImplicitDirectories.find(dir) !=
|
|
|
|
this->ImplicitDirectories.end()) {
|
2008-07-29 18:57:00 +00:00
|
|
|
this->ImplicitDirEntries.push_back(
|
2013-07-16 04:23:12 +00:00
|
|
|
new cmOrderDirectoriesConstraintSOName(this, fullPath, soname));
|
2008-02-21 16:41:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
|
|
|
|
// Construct the runtime information entry for this library.
|
|
|
|
this->ConstraintEntries.push_back(
|
2013-07-16 04:23:12 +00:00
|
|
|
new cmOrderDirectoriesConstraintSOName(this, fullPath, soname));
|
2016-05-16 14:34:04 +00:00
|
|
|
} else {
|
2008-02-21 16:41:11 +00:00
|
|
|
// This can happen if the same library is linked multiple times.
|
|
|
|
// In that case the runtime information check need be done only
|
|
|
|
// once anyway. For shared libs we could add a check in AddItem
|
|
|
|
// to not repeat them.
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmOrderDirectories::AddLinkLibrary(std::string const& fullPath)
|
|
|
|
{
|
|
|
|
// Link extension info is required for library constraints.
|
|
|
|
assert(!this->LinkExtensions.empty());
|
|
|
|
|
|
|
|
// Add the link library at most once.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->EmmittedConstraintLibrary.insert(fullPath).second) {
|
2008-07-29 18:57:00 +00:00
|
|
|
// Implicit link directories need special handling.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!this->ImplicitDirectories.empty()) {
|
2008-02-21 16:41:11 +00:00
|
|
|
std::string dir = cmSystemTools::GetFilenamePath(fullPath);
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->ImplicitDirectories.find(dir) !=
|
|
|
|
this->ImplicitDirectories.end()) {
|
2008-07-29 18:57:00 +00:00
|
|
|
this->ImplicitDirEntries.push_back(
|
|
|
|
new cmOrderDirectoriesConstraintLibrary(this, fullPath));
|
2008-02-21 16:41:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
|
|
|
|
// Construct the link library entry.
|
|
|
|
this->ConstraintEntries.push_back(
|
|
|
|
new cmOrderDirectoriesConstraintLibrary(this, fullPath));
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
void cmOrderDirectories::AddUserDirectories(
|
|
|
|
std::vector<std::string> const& extra)
|
2008-02-21 16:41:11 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
this->UserDirectories.insert(this->UserDirectories.end(), extra.begin(),
|
|
|
|
extra.end());
|
2008-02-21 16:41:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
void cmOrderDirectories::AddLanguageDirectories(
|
|
|
|
std::vector<std::string> const& dirs)
|
2009-07-27 16:43:17 +00:00
|
|
|
{
|
|
|
|
this->LanguageDirectories.insert(this->LanguageDirectories.end(),
|
|
|
|
dirs.begin(), dirs.end());
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
void cmOrderDirectories::SetImplicitDirectories(
|
|
|
|
std::set<std::string> const& implicitDirs)
|
2008-02-21 16:41:11 +00:00
|
|
|
{
|
|
|
|
this->ImplicitDirectories = implicitDirs;
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
void cmOrderDirectories::SetLinkExtensionInfo(
|
|
|
|
std::vector<std::string> const& linkExtensions,
|
|
|
|
std::string const& removeExtRegex)
|
2008-02-21 16:41:11 +00:00
|
|
|
{
|
|
|
|
this->LinkExtensions = linkExtensions;
|
|
|
|
this->RemoveLibraryExtension.compile(removeExtRegex.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmOrderDirectories::CollectOriginalDirectories()
|
|
|
|
{
|
|
|
|
// Add user directories specified for inclusion. These should be
|
|
|
|
// indexed first so their original order is preserved as much as
|
|
|
|
// possible subject to the constraints.
|
2009-07-14 14:14:41 +00:00
|
|
|
this->AddOriginalDirectories(this->UserDirectories);
|
2008-02-21 16:41:11 +00:00
|
|
|
|
|
|
|
// Add directories containing constraints.
|
2016-05-16 14:34:04 +00:00
|
|
|
for (unsigned int i = 0; i < this->ConstraintEntries.size(); ++i) {
|
2008-02-21 16:41:11 +00:00
|
|
|
this->ConstraintEntries[i]->AddDirectory();
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2009-07-27 16:43:17 +00:00
|
|
|
|
|
|
|
// Add language runtime directories last.
|
|
|
|
this->AddOriginalDirectories(this->LanguageDirectories);
|
2008-02-21 16:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int cmOrderDirectories::AddOriginalDirectory(std::string const& dir)
|
|
|
|
{
|
|
|
|
// Add the runtime directory with a unique index.
|
2016-05-16 14:34:04 +00:00
|
|
|
std::map<std::string, int>::iterator i = this->DirectoryIndex.find(dir);
|
|
|
|
if (i == this->DirectoryIndex.end()) {
|
|
|
|
std::map<std::string, int>::value_type entry(
|
|
|
|
dir, static_cast<int>(this->OriginalDirectories.size()));
|
2008-02-21 16:41:11 +00:00
|
|
|
i = this->DirectoryIndex.insert(entry).first;
|
|
|
|
this->OriginalDirectories.push_back(dir);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
void cmOrderDirectories::AddOriginalDirectories(
|
|
|
|
std::vector<std::string> const& dirs)
|
2009-07-14 14:14:41 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
for (std::vector<std::string>::const_iterator di = dirs.begin();
|
|
|
|
di != dirs.end(); ++di) {
|
2009-07-14 14:14:41 +00:00
|
|
|
// We never explicitly specify implicit link directories.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->ImplicitDirectories.find(*di) !=
|
|
|
|
this->ImplicitDirectories.end()) {
|
2009-07-14 14:14:41 +00:00
|
|
|
continue;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2009-07-14 14:14:41 +00:00
|
|
|
|
|
|
|
// Skip the empty string.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (di->empty()) {
|
2009-07-14 14:14:41 +00:00
|
|
|
continue;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2009-07-14 14:14:41 +00:00
|
|
|
|
|
|
|
// Add this directory.
|
|
|
|
this->AddOriginalDirectory(*di);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2009-07-14 14:14:41 +00:00
|
|
|
}
|
|
|
|
|
2008-02-21 16:41:11 +00:00
|
|
|
struct cmOrderDirectoriesCompare
|
|
|
|
{
|
|
|
|
typedef std::pair<int, int> ConflictPair;
|
|
|
|
|
|
|
|
// The conflict pair is unique based on just the directory
|
|
|
|
// (first). The second element is only used for displaying
|
|
|
|
// information about why the entry is present.
|
2016-05-16 14:34:04 +00:00
|
|
|
bool operator()(ConflictPair const& l, ConflictPair const& r)
|
|
|
|
{
|
2008-02-21 16:41:11 +00:00
|
|
|
return l.first == r.first;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void cmOrderDirectories::FindConflicts()
|
|
|
|
{
|
|
|
|
// Allocate the conflict graph.
|
|
|
|
this->ConflictGraph.resize(this->OriginalDirectories.size());
|
|
|
|
this->DirectoryVisited.resize(this->OriginalDirectories.size(), 0);
|
|
|
|
|
|
|
|
// Find directories conflicting with each entry.
|
2016-05-16 14:34:04 +00:00
|
|
|
for (unsigned int i = 0; i < this->ConstraintEntries.size(); ++i) {
|
2008-02-21 16:41:11 +00:00
|
|
|
this->ConstraintEntries[i]->FindConflicts(i);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
|
|
|
|
// Clean up the conflict graph representation.
|
2016-05-16 14:34:04 +00:00
|
|
|
for (std::vector<ConflictList>::iterator i = this->ConflictGraph.begin();
|
|
|
|
i != this->ConflictGraph.end(); ++i) {
|
2008-02-21 16:41:11 +00:00
|
|
|
// Sort the outgoing edges for each graph node so that the
|
|
|
|
// original order will be preserved as much as possible.
|
|
|
|
std::sort(i->begin(), i->end());
|
|
|
|
|
|
|
|
// Make the edge list unique so cycle detection will be reliable.
|
|
|
|
ConflictList::iterator last =
|
|
|
|
std::unique(i->begin(), i->end(), cmOrderDirectoriesCompare());
|
|
|
|
i->erase(last, i->end());
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-07-29 18:57:00 +00:00
|
|
|
|
|
|
|
// Check items in implicit link directories.
|
|
|
|
this->FindImplicitConflicts();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmOrderDirectories::FindImplicitConflicts()
|
|
|
|
{
|
|
|
|
// Check for items in implicit link directories that have conflicts
|
|
|
|
// in the explicit directories.
|
2015-01-05 19:31:31 +00:00
|
|
|
std::ostringstream conflicts;
|
2016-05-16 14:34:04 +00:00
|
|
|
for (unsigned int i = 0; i < this->ImplicitDirEntries.size(); ++i) {
|
2008-07-29 18:57:00 +00:00
|
|
|
this->ImplicitDirEntries[i]->FindImplicitConflicts(conflicts);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-07-29 18:57:00 +00:00
|
|
|
|
|
|
|
// Skip warning if there were no conflicts.
|
|
|
|
std::string text = conflicts.str();
|
2016-05-16 14:34:04 +00:00
|
|
|
if (text.empty()) {
|
2008-07-29 18:57:00 +00:00
|
|
|
return;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-07-29 18:57:00 +00:00
|
|
|
|
|
|
|
// Warn about the conflicts.
|
2015-01-05 19:31:31 +00:00
|
|
|
std::ostringstream w;
|
2016-05-16 14:34:04 +00:00
|
|
|
w << "Cannot generate a safe " << this->Purpose << " for target "
|
|
|
|
<< this->Target->GetName()
|
2008-07-29 18:57:00 +00:00
|
|
|
<< " because files in some directories may conflict with "
|
|
|
|
<< " libraries in implicit directories:\n"
|
2016-05-16 14:34:04 +00:00
|
|
|
<< text << "Some of these libraries may not be found correctly.";
|
|
|
|
this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
|
|
|
|
cmake::WARNING, w.str(), this->Target->GetBacktrace());
|
2008-02-21 16:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmOrderDirectories::OrderDirectories()
|
|
|
|
{
|
|
|
|
// Allow a cycle to be diagnosed once.
|
|
|
|
this->CycleDiagnosed = false;
|
|
|
|
this->WalkId = 0;
|
|
|
|
|
|
|
|
// Iterate through the directories in the original order.
|
2016-05-16 14:34:04 +00:00
|
|
|
for (unsigned int i = 0; i < this->OriginalDirectories.size(); ++i) {
|
2008-02-21 16:41:11 +00:00
|
|
|
// Start a new DFS from this node.
|
|
|
|
++this->WalkId;
|
|
|
|
this->VisitDirectory(i);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmOrderDirectories::VisitDirectory(unsigned int i)
|
|
|
|
{
|
|
|
|
// Skip nodes already visited.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->DirectoryVisited[i]) {
|
|
|
|
if (this->DirectoryVisited[i] == this->WalkId) {
|
2008-02-21 16:41:11 +00:00
|
|
|
// We have reached a node previously visited on this DFS.
|
|
|
|
// There is a cycle.
|
|
|
|
this->DiagnoseCycle();
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
|
|
|
|
// We are now visiting this node so mark it.
|
|
|
|
this->DirectoryVisited[i] = this->WalkId;
|
|
|
|
|
|
|
|
// Visit the neighbors of the node first.
|
|
|
|
ConflictList const& clist = this->ConflictGraph[i];
|
2016-05-16 14:34:04 +00:00
|
|
|
for (ConflictList::const_iterator j = clist.begin(); j != clist.end(); ++j) {
|
2008-02-21 16:41:11 +00:00
|
|
|
this->VisitDirectory(j->first);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
|
|
|
|
// Now that all directories required to come before this one have
|
|
|
|
// been emmitted, emit this directory.
|
|
|
|
this->OrderedDirectories.push_back(this->OriginalDirectories[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmOrderDirectories::DiagnoseCycle()
|
|
|
|
{
|
|
|
|
// Report the cycle at most once.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->CycleDiagnosed) {
|
2008-02-21 16:41:11 +00:00
|
|
|
return;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-02-21 16:41:11 +00:00
|
|
|
this->CycleDiagnosed = true;
|
|
|
|
|
|
|
|
// Construct the message.
|
2015-01-05 19:31:31 +00:00
|
|
|
std::ostringstream e;
|
2016-05-16 14:34:04 +00:00
|
|
|
e << "Cannot generate a safe " << this->Purpose << " for target "
|
|
|
|
<< this->Target->GetName()
|
2008-02-21 16:41:11 +00:00
|
|
|
<< " because there is a cycle in the constraint graph:\n";
|
|
|
|
|
|
|
|
// Display the conflict graph.
|
2016-05-16 14:34:04 +00:00
|
|
|
for (unsigned int i = 0; i < this->ConflictGraph.size(); ++i) {
|
2008-02-21 16:41:11 +00:00
|
|
|
ConflictList const& clist = this->ConflictGraph[i];
|
2008-07-29 18:01:04 +00:00
|
|
|
e << " dir " << i << " is [" << this->OriginalDirectories[i] << "]\n";
|
2016-05-16 14:34:04 +00:00
|
|
|
for (ConflictList::const_iterator j = clist.begin(); j != clist.end();
|
|
|
|
++j) {
|
2008-07-29 18:01:04 +00:00
|
|
|
e << " dir " << j->first << " must precede it due to ";
|
2008-02-21 16:41:11 +00:00
|
|
|
this->ConstraintEntries[j->second]->Report(e);
|
|
|
|
e << "\n";
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-07-29 18:01:04 +00:00
|
|
|
e << "Some of these libraries may not be found correctly.";
|
2016-05-16 14:34:04 +00:00
|
|
|
this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
|
|
|
|
cmake::WARNING, e.str(), this->Target->GetBacktrace());
|
2008-02-21 16:41:11 +00:00
|
|
|
}
|
2015-12-01 15:44:48 +00:00
|
|
|
|
|
|
|
bool cmOrderDirectories::IsSameDirectory(std::string const& l,
|
|
|
|
std::string const& r)
|
|
|
|
{
|
2015-12-01 15:57:33 +00:00
|
|
|
return this->GetRealPath(l) == this->GetRealPath(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const& cmOrderDirectories::GetRealPath(std::string const& dir)
|
|
|
|
{
|
|
|
|
std::map<std::string, std::string>::iterator i =
|
|
|
|
this->RealPaths.lower_bound(dir);
|
|
|
|
if (i == this->RealPaths.end() ||
|
2016-05-16 14:34:04 +00:00
|
|
|
this->RealPaths.key_comp()(dir, i->first)) {
|
2015-12-01 15:57:33 +00:00
|
|
|
typedef std::map<std::string, std::string>::value_type value_type;
|
|
|
|
i = this->RealPaths.insert(
|
|
|
|
i, value_type(dir, cmSystemTools::GetRealPath(dir)));
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2015-12-01 15:57:33 +00:00
|
|
|
return i->second;
|
2015-12-01 15:44:48 +00:00
|
|
|
}
|