1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmOrderDirectories.cxx,v $
6 Date: $Date: 2008-07-29 18:57:00 $
7 Version: $Revision: 1.7 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmOrderDirectories.h"
19 #include "cmGlobalGenerator.h"
20 #include "cmSystemTools.h"
28 Directory ordering computation.
29 - Useful to compute a safe runtime library path order
30 - Need runtime path for supporting INSTALL_RPATH_USE_LINK_PATH
31 - Need runtime path at link time to pickup transitive link dependencies
35 //----------------------------------------------------------------------------
36 class cmOrderDirectoriesConstraint
39 cmOrderDirectoriesConstraint(cmOrderDirectories
* od
,
40 std::string
const& file
):
41 OD(od
), GlobalGenerator(od
->GlobalGenerator
)
43 this->FullPath
= file
;
44 this->Directory
= cmSystemTools::GetFilenamePath(file
);
45 this->FileName
= cmSystemTools::GetFilenameName(file
);
47 virtual ~cmOrderDirectoriesConstraint() {}
51 this->DirectoryIndex
= this->OD
->AddOriginalDirectory(this->Directory
);
54 virtual void Report(std::ostream
& e
) = 0;
56 void FindConflicts(unsigned int index
)
58 for(unsigned int i
=0; i
< this->OD
->OriginalDirectories
.size(); ++i
)
60 // Check if this directory conflicts with the entry.
61 std::string
const& dir
= this->OD
->OriginalDirectories
[i
];
62 if(dir
!= this->Directory
&& this->FindConflict(dir
))
64 // The library will be found in this directory but this is not
65 // the directory named for it. Add an entry to make sure the
66 // desired directory comes before this one.
67 cmOrderDirectories::ConflictPair
p(this->DirectoryIndex
, index
);
68 this->OD
->ConflictGraph
[i
].push_back(p
);
73 void FindImplicitConflicts(cmOStringStream
& w
)
76 for(unsigned int i
=0; i
< this->OD
->OriginalDirectories
.size(); ++i
)
78 // Check if this directory conflicts with the entry.
79 std::string
const& dir
= this->OD
->OriginalDirectories
[i
];
80 if(dir
!= this->Directory
&& this->FindConflict(dir
))
82 // The library will be found in this directory but it is
83 // supposed to be found in an implicit search directory.
89 w
<< " in " << this->Directory
<< " may be hidden by files in:\n";
91 w
<< " " << dir
<< "\n";
96 virtual bool FindConflict(std::string
const& dir
) = 0;
98 bool FileMayConflict(std::string
const& dir
, std::string
const& name
);
100 cmOrderDirectories
* OD
;
101 cmGlobalGenerator
* GlobalGenerator
;
103 // The location in which the item is supposed to be found.
104 std::string FullPath
;
105 std::string Directory
;
106 std::string FileName
;
108 // The index assigned to the directory.
112 //----------------------------------------------------------------------------
113 bool cmOrderDirectoriesConstraint::FileMayConflict(std::string
const& dir
,
114 std::string
const& name
)
116 // Check if the file will be built by cmake.
117 std::set
<cmStdString
> const& files
=
118 (this->GlobalGenerator
->GetDirectoryContent(dir
, false));
119 if(std::set
<cmStdString
>::const_iterator(files
.find(name
)) != files
.end())
124 // Check if the file exists on disk and is not a symlink back to the
126 std::string file
= dir
;
129 if(cmSystemTools::FileExists(file
.c_str(), true) &&
130 !cmSystemTools::SameFile(this->FullPath
.c_str(), file
.c_str()))
137 //----------------------------------------------------------------------------
138 class cmOrderDirectoriesConstraintSOName
: public cmOrderDirectoriesConstraint
141 cmOrderDirectoriesConstraintSOName(cmOrderDirectories
* od
,
142 std::string
const& file
,
144 cmOrderDirectoriesConstraint(od
, file
), SOName(soname
? soname
: "")
146 if(this->SOName
.empty())
148 // Try to guess the soname.
150 if(cmSystemTools::GuessLibrarySOName(file
, soguess
))
152 this->SOName
= soguess
;
157 virtual void Report(std::ostream
& e
)
159 e
<< "runtime library [";
160 if(this->SOName
.empty())
171 virtual bool FindConflict(std::string
const& dir
);
173 // The soname of the shared library if it is known.
177 //----------------------------------------------------------------------------
178 bool cmOrderDirectoriesConstraintSOName::FindConflict(std::string
const& dir
)
180 // Determine which type of check to do.
181 if(!this->SOName
.empty())
183 // We have the library soname. Check if it will be found.
184 if(this->FileMayConflict(dir
, this->SOName
))
191 // We do not have the soname. Look for files in the directory
192 // that may conflict.
193 std::set
<cmStdString
> const& files
=
194 (this->GlobalGenerator
195 ->GetDirectoryContent(dir
, true));
197 // Get the set of files that might conflict. Since we do not
198 // know the soname just look at all files that start with the
199 // file name. Usually the soname starts with the library name.
200 std::string base
= this->FileName
;
201 std::set
<cmStdString
>::const_iterator first
= files
.lower_bound(base
);
202 ++base
[base
.size()-1];
203 std::set
<cmStdString
>::const_iterator last
= files
.upper_bound(base
);
212 //----------------------------------------------------------------------------
213 class cmOrderDirectoriesConstraintLibrary
: public cmOrderDirectoriesConstraint
216 cmOrderDirectoriesConstraintLibrary(cmOrderDirectories
* od
,
217 std::string
const& file
):
218 cmOrderDirectoriesConstraint(od
, file
)
222 virtual void Report(std::ostream
& e
)
224 e
<< "link library [" << this->FileName
<< "]";
227 virtual bool FindConflict(std::string
const& dir
);
230 //----------------------------------------------------------------------------
231 bool cmOrderDirectoriesConstraintLibrary::FindConflict(std::string
const& dir
)
233 // We have the library file name. Check if it will be found.
234 if(this->FileMayConflict(dir
, this->FileName
))
239 // Now check if the file exists with other extensions the linker
241 if(!this->OD
->LinkExtensions
.empty() &&
242 this->OD
->RemoveLibraryExtension
.find(this->FileName
))
244 cmStdString lib
= this->OD
->RemoveLibraryExtension
.match(1);
245 cmStdString ext
= this->OD
->RemoveLibraryExtension
.match(2);
246 for(std::vector
<std::string
>::iterator
247 i
= this->OD
->LinkExtensions
.begin();
248 i
!= this->OD
->LinkExtensions
.end(); ++i
)
252 std::string fname
= lib
;
254 if(this->FileMayConflict(dir
, fname
.c_str()))
264 //----------------------------------------------------------------------------
265 cmOrderDirectories::cmOrderDirectories(cmGlobalGenerator
* gg
,
269 this->GlobalGenerator
= gg
;
270 this->Target
= target
;
271 this->Purpose
= purpose
;
272 this->Computed
= false;
275 //----------------------------------------------------------------------------
276 cmOrderDirectories::~cmOrderDirectories()
278 for(std::vector
<cmOrderDirectoriesConstraint
*>::iterator
279 i
= this->ConstraintEntries
.begin();
280 i
!= this->ConstraintEntries
.end(); ++i
)
284 for(std::vector
<cmOrderDirectoriesConstraint
*>::iterator
285 i
= this->ImplicitDirEntries
.begin();
286 i
!= this->ImplicitDirEntries
.end(); ++i
)
292 //----------------------------------------------------------------------------
293 std::vector
<std::string
> const& cmOrderDirectories::GetOrderedDirectories()
297 this->Computed
= true;
298 this->CollectOriginalDirectories();
299 this->FindConflicts();
300 this->OrderDirectories();
302 return this->OrderedDirectories
;
305 //----------------------------------------------------------------------------
306 void cmOrderDirectories::AddRuntimeLibrary(std::string
const& fullPath
,
309 // Add the runtime library at most once.
310 if(this->EmmittedConstraintSOName
.insert(fullPath
).second
)
312 // Implicit link directories need special handling.
313 if(!this->ImplicitDirectories
.empty())
315 std::string dir
= cmSystemTools::GetFilenamePath(fullPath
);
316 if(this->ImplicitDirectories
.find(dir
) !=
317 this->ImplicitDirectories
.end())
319 this->ImplicitDirEntries
.push_back(
320 new cmOrderDirectoriesConstraintSOName(this, fullPath
, soname
));
325 // Construct the runtime information entry for this library.
326 this->ConstraintEntries
.push_back(
327 new cmOrderDirectoriesConstraintSOName(this, fullPath
, soname
));
331 // This can happen if the same library is linked multiple times.
332 // In that case the runtime information check need be done only
333 // once anyway. For shared libs we could add a check in AddItem
334 // to not repeat them.
338 //----------------------------------------------------------------------------
339 void cmOrderDirectories::AddLinkLibrary(std::string
const& fullPath
)
341 // Link extension info is required for library constraints.
342 assert(!this->LinkExtensions
.empty());
344 // Add the link library at most once.
345 if(this->EmmittedConstraintLibrary
.insert(fullPath
).second
)
347 // Implicit link directories need special handling.
348 if(!this->ImplicitDirectories
.empty())
350 std::string dir
= cmSystemTools::GetFilenamePath(fullPath
);
351 if(this->ImplicitDirectories
.find(dir
) !=
352 this->ImplicitDirectories
.end())
354 this->ImplicitDirEntries
.push_back(
355 new cmOrderDirectoriesConstraintLibrary(this, fullPath
));
360 // Construct the link library entry.
361 this->ConstraintEntries
.push_back(
362 new cmOrderDirectoriesConstraintLibrary(this, fullPath
));
366 //----------------------------------------------------------------------------
369 ::AddUserDirectories(std::vector
<std::string
> const& extra
)
371 this->UserDirectories
.insert(this->UserDirectories
.end(),
372 extra
.begin(), extra
.end());
375 //----------------------------------------------------------------------------
378 ::SetImplicitDirectories(std::set
<cmStdString
> const& implicitDirs
)
380 this->ImplicitDirectories
= implicitDirs
;
383 //----------------------------------------------------------------------------
386 ::SetLinkExtensionInfo(std::vector
<std::string
> const& linkExtensions
,
387 std::string
const& removeExtRegex
)
389 this->LinkExtensions
= linkExtensions
;
390 this->RemoveLibraryExtension
.compile(removeExtRegex
.c_str());
393 //----------------------------------------------------------------------------
394 void cmOrderDirectories::CollectOriginalDirectories()
396 // Add user directories specified for inclusion. These should be
397 // indexed first so their original order is preserved as much as
398 // possible subject to the constraints.
399 for(std::vector
<std::string
>::const_iterator
400 di
= this->UserDirectories
.begin();
401 di
!= this->UserDirectories
.end(); ++di
)
403 // We never explicitly specify implicit link directories.
404 if(this->ImplicitDirectories
.find(*di
) !=
405 this->ImplicitDirectories
.end())
410 // Skip the empty string.
416 // Add this directory.
417 this->AddOriginalDirectory(*di
);
420 // Add directories containing constraints.
421 for(unsigned int i
=0; i
< this->ConstraintEntries
.size(); ++i
)
423 this->ConstraintEntries
[i
]->AddDirectory();
427 //----------------------------------------------------------------------------
428 int cmOrderDirectories::AddOriginalDirectory(std::string
const& dir
)
430 // Add the runtime directory with a unique index.
431 std::map
<cmStdString
, int>::iterator i
=
432 this->DirectoryIndex
.find(dir
);
433 if(i
== this->DirectoryIndex
.end())
435 std::map
<cmStdString
, int>::value_type
436 entry(dir
, static_cast<int>(this->OriginalDirectories
.size()));
437 i
= this->DirectoryIndex
.insert(entry
).first
;
438 this->OriginalDirectories
.push_back(dir
);
444 //----------------------------------------------------------------------------
445 struct cmOrderDirectoriesCompare
447 typedef std::pair
<int, int> ConflictPair
;
449 // The conflict pair is unique based on just the directory
450 // (first). The second element is only used for displaying
451 // information about why the entry is present.
452 bool operator()(ConflictPair
const& l
,
453 ConflictPair
const& r
)
455 return l
.first
== r
.first
;
459 //----------------------------------------------------------------------------
460 void cmOrderDirectories::FindConflicts()
462 // Allocate the conflict graph.
463 this->ConflictGraph
.resize(this->OriginalDirectories
.size());
464 this->DirectoryVisited
.resize(this->OriginalDirectories
.size(), 0);
466 // Find directories conflicting with each entry.
467 for(unsigned int i
=0; i
< this->ConstraintEntries
.size(); ++i
)
469 this->ConstraintEntries
[i
]->FindConflicts(i
);
472 // Clean up the conflict graph representation.
473 for(std::vector
<ConflictList
>::iterator
474 i
= this->ConflictGraph
.begin();
475 i
!= this->ConflictGraph
.end(); ++i
)
477 // Sort the outgoing edges for each graph node so that the
478 // original order will be preserved as much as possible.
479 std::sort(i
->begin(), i
->end());
481 // Make the edge list unique so cycle detection will be reliable.
482 ConflictList::iterator last
=
483 std::unique(i
->begin(), i
->end(), cmOrderDirectoriesCompare());
484 i
->erase(last
, i
->end());
487 // Check items in implicit link directories.
488 this->FindImplicitConflicts();
491 //----------------------------------------------------------------------------
492 void cmOrderDirectories::FindImplicitConflicts()
494 // Check for items in implicit link directories that have conflicts
495 // in the explicit directories.
496 cmOStringStream conflicts
;
497 for(unsigned int i
=0; i
< this->ImplicitDirEntries
.size(); ++i
)
499 this->ImplicitDirEntries
[i
]->FindImplicitConflicts(conflicts
);
502 // Skip warning if there were no conflicts.
503 std::string text
= conflicts
.str();
509 // Warn about the conflicts.
511 w
<< "Cannot generate a safe " << this->Purpose
512 << " for target " << this->Target
->GetName()
513 << " because files in some directories may conflict with "
514 << " libraries in implicit directories:\n"
516 << "Some of these libraries may not be found correctly.";
517 this->GlobalGenerator
->GetCMakeInstance()
518 ->IssueMessage(cmake::WARNING
, w
.str(), this->Target
->GetBacktrace());
521 //----------------------------------------------------------------------------
522 void cmOrderDirectories::OrderDirectories()
524 // Allow a cycle to be diagnosed once.
525 this->CycleDiagnosed
= false;
528 // Iterate through the directories in the original order.
529 for(unsigned int i
=0; i
< this->OriginalDirectories
.size(); ++i
)
531 // Start a new DFS from this node.
533 this->VisitDirectory(i
);
537 //----------------------------------------------------------------------------
538 void cmOrderDirectories::VisitDirectory(unsigned int i
)
540 // Skip nodes already visited.
541 if(this->DirectoryVisited
[i
])
543 if(this->DirectoryVisited
[i
] == this->WalkId
)
545 // We have reached a node previously visited on this DFS.
547 this->DiagnoseCycle();
552 // We are now visiting this node so mark it.
553 this->DirectoryVisited
[i
] = this->WalkId
;
555 // Visit the neighbors of the node first.
556 ConflictList
const& clist
= this->ConflictGraph
[i
];
557 for(ConflictList::const_iterator j
= clist
.begin();
558 j
!= clist
.end(); ++j
)
560 this->VisitDirectory(j
->first
);
563 // Now that all directories required to come before this one have
564 // been emmitted, emit this directory.
565 this->OrderedDirectories
.push_back(this->OriginalDirectories
[i
]);
568 //----------------------------------------------------------------------------
569 void cmOrderDirectories::DiagnoseCycle()
571 // Report the cycle at most once.
572 if(this->CycleDiagnosed
)
576 this->CycleDiagnosed
= true;
578 // Construct the message.
580 e
<< "Cannot generate a safe " << this->Purpose
581 << " for target " << this->Target
->GetName()
582 << " because there is a cycle in the constraint graph:\n";
584 // Display the conflict graph.
585 for(unsigned int i
=0; i
< this->ConflictGraph
.size(); ++i
)
587 ConflictList
const& clist
= this->ConflictGraph
[i
];
588 e
<< " dir " << i
<< " is [" << this->OriginalDirectories
[i
] << "]\n";
589 for(ConflictList::const_iterator j
= clist
.begin();
590 j
!= clist
.end(); ++j
)
592 e
<< " dir " << j
->first
<< " must precede it due to ";
593 this->ConstraintEntries
[j
->second
]->Report(e
);
597 e
<< "Some of these libraries may not be found correctly.";
598 this->GlobalGenerator
->GetCMakeInstance()
599 ->IssueMessage(cmake::WARNING
, e
.str(), this->Target
->GetBacktrace());