cmOrderDirectories.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmOrderDirectories.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmGeneratorTarget.h"
  6. #include "cmGlobalGenerator.h"
  7. #include "cmSystemTools.h"
  8. #include "cmake.h"
  9. #include <algorithm>
  10. #include <assert.h>
  11. #include <functional>
  12. #include <sstream>
  13. /*
  14. Directory ordering computation.
  15. - Useful to compute a safe runtime library path order
  16. - Need runtime path for supporting INSTALL_RPATH_USE_LINK_PATH
  17. - Need runtime path at link time to pickup transitive link dependencies
  18. for shared libraries.
  19. */
  20. class cmOrderDirectoriesConstraint
  21. {
  22. public:
  23. cmOrderDirectoriesConstraint(cmOrderDirectories* od, std::string const& file)
  24. : OD(od)
  25. , GlobalGenerator(od->GlobalGenerator)
  26. {
  27. this->FullPath = file;
  28. if (file.rfind(".framework") != std::string::npos) {
  29. static cmsys::RegularExpression splitFramework(
  30. "^(.*)/(.*).framework/(.*)$");
  31. if (splitFramework.find(file) &&
  32. (std::string::npos !=
  33. splitFramework.match(3).find(splitFramework.match(2)))) {
  34. this->Directory = splitFramework.match(1);
  35. this->FileName =
  36. std::string(file.begin() + this->Directory.size() + 1, file.end());
  37. }
  38. }
  39. if (this->FileName.empty()) {
  40. this->Directory = cmSystemTools::GetFilenamePath(file);
  41. this->FileName = cmSystemTools::GetFilenameName(file);
  42. }
  43. }
  44. virtual ~cmOrderDirectoriesConstraint() {}
  45. void AddDirectory()
  46. {
  47. this->DirectoryIndex = this->OD->AddOriginalDirectory(this->Directory);
  48. }
  49. virtual void Report(std::ostream& e) = 0;
  50. void FindConflicts(unsigned int index)
  51. {
  52. for (unsigned int i = 0; i < this->OD->OriginalDirectories.size(); ++i) {
  53. // Check if this directory conflicts with the entry.
  54. std::string const& dir = this->OD->OriginalDirectories[i];
  55. if (!this->OD->IsSameDirectory(dir, this->Directory) &&
  56. this->FindConflict(dir)) {
  57. // The library will be found in this directory but this is not
  58. // the directory named for it. Add an entry to make sure the
  59. // desired directory comes before this one.
  60. cmOrderDirectories::ConflictPair p(this->DirectoryIndex, index);
  61. this->OD->ConflictGraph[i].push_back(p);
  62. }
  63. }
  64. }
  65. void FindImplicitConflicts(std::ostringstream& w)
  66. {
  67. bool first = true;
  68. for (std::string const& dir : this->OD->OriginalDirectories) {
  69. // Check if this directory conflicts with the entry.
  70. if (dir != this->Directory &&
  71. cmSystemTools::GetRealPath(dir) !=
  72. cmSystemTools::GetRealPath(this->Directory) &&
  73. this->FindConflict(dir)) {
  74. // The library will be found in this directory but it is
  75. // supposed to be found in an implicit search directory.
  76. if (first) {
  77. first = false;
  78. w << " ";
  79. this->Report(w);
  80. w << " in " << this->Directory << " may be hidden by files in:\n";
  81. }
  82. w << " " << dir << "\n";
  83. }
  84. }
  85. }
  86. protected:
  87. virtual bool FindConflict(std::string const& dir) = 0;
  88. bool FileMayConflict(std::string const& dir, std::string const& name);
  89. cmOrderDirectories* OD;
  90. cmGlobalGenerator* GlobalGenerator;
  91. // The location in which the item is supposed to be found.
  92. std::string FullPath;
  93. std::string Directory;
  94. std::string FileName;
  95. // The index assigned to the directory.
  96. int DirectoryIndex;
  97. };
  98. bool cmOrderDirectoriesConstraint::FileMayConflict(std::string const& dir,
  99. std::string const& name)
  100. {
  101. // Check if the file exists on disk.
  102. std::string file = dir;
  103. file += "/";
  104. file += name;
  105. if (cmSystemTools::FileExists(file, true)) {
  106. // The file conflicts only if it is not the same as the original
  107. // file due to a symlink or hardlink.
  108. return !cmSystemTools::SameFile(this->FullPath, file);
  109. }
  110. // Check if the file will be built by cmake.
  111. std::set<std::string> const& files =
  112. (this->GlobalGenerator->GetDirectoryContent(dir, false));
  113. std::set<std::string>::const_iterator fi = files.find(name);
  114. return fi != files.end();
  115. }
  116. class cmOrderDirectoriesConstraintSOName : public cmOrderDirectoriesConstraint
  117. {
  118. public:
  119. cmOrderDirectoriesConstraintSOName(cmOrderDirectories* od,
  120. std::string const& file,
  121. const char* soname)
  122. : cmOrderDirectoriesConstraint(od, file)
  123. , SOName(soname ? soname : "")
  124. {
  125. if (this->SOName.empty()) {
  126. // Try to guess the soname.
  127. std::string soguess;
  128. if (cmSystemTools::GuessLibrarySOName(file, soguess)) {
  129. this->SOName = soguess;
  130. }
  131. }
  132. }
  133. void Report(std::ostream& e) override
  134. {
  135. e << "runtime library [";
  136. if (this->SOName.empty()) {
  137. e << this->FileName;
  138. } else {
  139. e << this->SOName;
  140. }
  141. e << "]";
  142. }
  143. bool FindConflict(std::string const& dir) override;
  144. private:
  145. // The soname of the shared library if it is known.
  146. std::string SOName;
  147. };
  148. bool cmOrderDirectoriesConstraintSOName::FindConflict(std::string const& dir)
  149. {
  150. // Determine which type of check to do.
  151. if (!this->SOName.empty()) {
  152. // We have the library soname. Check if it will be found.
  153. if (this->FileMayConflict(dir, this->SOName)) {
  154. return true;
  155. }
  156. } else {
  157. // We do not have the soname. Look for files in the directory
  158. // that may conflict.
  159. std::set<std::string> const& files =
  160. (this->GlobalGenerator->GetDirectoryContent(dir, true));
  161. // Get the set of files that might conflict. Since we do not
  162. // know the soname just look at all files that start with the
  163. // file name. Usually the soname starts with the library name.
  164. std::string base = this->FileName;
  165. std::set<std::string>::const_iterator first = files.lower_bound(base);
  166. ++base[base.size() - 1];
  167. std::set<std::string>::const_iterator last = files.upper_bound(base);
  168. if (first != last) {
  169. return true;
  170. }
  171. }
  172. return false;
  173. }
  174. class cmOrderDirectoriesConstraintLibrary : public cmOrderDirectoriesConstraint
  175. {
  176. public:
  177. cmOrderDirectoriesConstraintLibrary(cmOrderDirectories* od,
  178. std::string const& file)
  179. : cmOrderDirectoriesConstraint(od, file)
  180. {
  181. }
  182. void Report(std::ostream& e) override
  183. {
  184. e << "link library [" << this->FileName << "]";
  185. }
  186. bool FindConflict(std::string const& dir) override;
  187. };
  188. bool cmOrderDirectoriesConstraintLibrary::FindConflict(std::string const& dir)
  189. {
  190. // We have the library file name. Check if it will be found.
  191. if (this->FileMayConflict(dir, this->FileName)) {
  192. return true;
  193. }
  194. // Now check if the file exists with other extensions the linker
  195. // might consider.
  196. if (!this->OD->LinkExtensions.empty() &&
  197. this->OD->RemoveLibraryExtension.find(this->FileName)) {
  198. std::string lib = this->OD->RemoveLibraryExtension.match(1);
  199. std::string ext = this->OD->RemoveLibraryExtension.match(2);
  200. for (std::string const& LinkExtension : this->OD->LinkExtensions) {
  201. if (LinkExtension != ext) {
  202. std::string fname = lib;
  203. fname += LinkExtension;
  204. if (this->FileMayConflict(dir, fname)) {
  205. return true;
  206. }
  207. }
  208. }
  209. }
  210. return false;
  211. }
  212. cmOrderDirectories::cmOrderDirectories(cmGlobalGenerator* gg,
  213. const cmGeneratorTarget* target,
  214. const char* purpose)
  215. {
  216. this->GlobalGenerator = gg;
  217. this->Target = target;
  218. this->Purpose = purpose;
  219. this->Computed = false;
  220. }
  221. cmOrderDirectories::~cmOrderDirectories()
  222. {
  223. cmDeleteAll(this->ConstraintEntries);
  224. cmDeleteAll(this->ImplicitDirEntries);
  225. }
  226. std::vector<std::string> const& cmOrderDirectories::GetOrderedDirectories()
  227. {
  228. if (!this->Computed) {
  229. this->Computed = true;
  230. this->CollectOriginalDirectories();
  231. this->FindConflicts();
  232. this->OrderDirectories();
  233. }
  234. return this->OrderedDirectories;
  235. }
  236. void cmOrderDirectories::AddRuntimeLibrary(std::string const& fullPath,
  237. const char* soname)
  238. {
  239. // Add the runtime library at most once.
  240. if (this->EmmittedConstraintSOName.insert(fullPath).second) {
  241. // Implicit link directories need special handling.
  242. if (!this->ImplicitDirectories.empty()) {
  243. std::string dir = cmSystemTools::GetFilenamePath(fullPath);
  244. if (fullPath.rfind(".framework") != std::string::npos) {
  245. static cmsys::RegularExpression splitFramework(
  246. "^(.*)/(.*).framework/(.*)$");
  247. if (splitFramework.find(fullPath) &&
  248. (std::string::npos !=
  249. splitFramework.match(3).find(splitFramework.match(2)))) {
  250. dir = splitFramework.match(1);
  251. }
  252. }
  253. if (this->IsImplicitDirectory(dir)) {
  254. this->ImplicitDirEntries.push_back(
  255. new cmOrderDirectoriesConstraintSOName(this, fullPath, soname));
  256. return;
  257. }
  258. }
  259. // Construct the runtime information entry for this library.
  260. this->ConstraintEntries.push_back(
  261. new cmOrderDirectoriesConstraintSOName(this, fullPath, soname));
  262. } else {
  263. // This can happen if the same library is linked multiple times.
  264. // In that case the runtime information check need be done only
  265. // once anyway. For shared libs we could add a check in AddItem
  266. // to not repeat them.
  267. }
  268. }
  269. void cmOrderDirectories::AddLinkLibrary(std::string const& fullPath)
  270. {
  271. // Link extension info is required for library constraints.
  272. assert(!this->LinkExtensions.empty());
  273. // Add the link library at most once.
  274. if (this->EmmittedConstraintLibrary.insert(fullPath).second) {
  275. // Implicit link directories need special handling.
  276. if (!this->ImplicitDirectories.empty()) {
  277. std::string dir = cmSystemTools::GetFilenamePath(fullPath);
  278. if (this->IsImplicitDirectory(dir)) {
  279. this->ImplicitDirEntries.push_back(
  280. new cmOrderDirectoriesConstraintLibrary(this, fullPath));
  281. return;
  282. }
  283. }
  284. // Construct the link library entry.
  285. this->ConstraintEntries.push_back(
  286. new cmOrderDirectoriesConstraintLibrary(this, fullPath));
  287. }
  288. }
  289. void cmOrderDirectories::AddUserDirectories(
  290. std::vector<std::string> const& extra)
  291. {
  292. this->UserDirectories.insert(this->UserDirectories.end(), extra.begin(),
  293. extra.end());
  294. }
  295. void cmOrderDirectories::AddLanguageDirectories(
  296. std::vector<std::string> const& dirs)
  297. {
  298. this->LanguageDirectories.insert(this->LanguageDirectories.end(),
  299. dirs.begin(), dirs.end());
  300. }
  301. void cmOrderDirectories::SetImplicitDirectories(
  302. std::set<std::string> const& implicitDirs)
  303. {
  304. this->ImplicitDirectories.clear();
  305. for (std::string const& implicitDir : implicitDirs) {
  306. this->ImplicitDirectories.insert(this->GetRealPath(implicitDir));
  307. }
  308. }
  309. bool cmOrderDirectories::IsImplicitDirectory(std::string const& dir)
  310. {
  311. std::string const& real = this->GetRealPath(dir);
  312. return this->ImplicitDirectories.find(real) !=
  313. this->ImplicitDirectories.end();
  314. }
  315. void cmOrderDirectories::SetLinkExtensionInfo(
  316. std::vector<std::string> const& linkExtensions,
  317. std::string const& removeExtRegex)
  318. {
  319. this->LinkExtensions = linkExtensions;
  320. this->RemoveLibraryExtension.compile(removeExtRegex.c_str());
  321. }
  322. void cmOrderDirectories::CollectOriginalDirectories()
  323. {
  324. // Add user directories specified for inclusion. These should be
  325. // indexed first so their original order is preserved as much as
  326. // possible subject to the constraints.
  327. this->AddOriginalDirectories(this->UserDirectories);
  328. // Add directories containing constraints.
  329. for (cmOrderDirectoriesConstraint* entry : this->ConstraintEntries) {
  330. entry->AddDirectory();
  331. }
  332. // Add language runtime directories last.
  333. this->AddOriginalDirectories(this->LanguageDirectories);
  334. }
  335. int cmOrderDirectories::AddOriginalDirectory(std::string const& dir)
  336. {
  337. // Add the runtime directory with a unique index.
  338. std::map<std::string, int>::iterator i = this->DirectoryIndex.find(dir);
  339. if (i == this->DirectoryIndex.end()) {
  340. std::map<std::string, int>::value_type entry(
  341. dir, static_cast<int>(this->OriginalDirectories.size()));
  342. i = this->DirectoryIndex.insert(entry).first;
  343. this->OriginalDirectories.push_back(dir);
  344. }
  345. return i->second;
  346. }
  347. void cmOrderDirectories::AddOriginalDirectories(
  348. std::vector<std::string> const& dirs)
  349. {
  350. for (std::string const& dir : dirs) {
  351. // We never explicitly specify implicit link directories.
  352. if (this->IsImplicitDirectory(dir)) {
  353. continue;
  354. }
  355. // Skip the empty string.
  356. if (dir.empty()) {
  357. continue;
  358. }
  359. // Add this directory.
  360. this->AddOriginalDirectory(dir);
  361. }
  362. }
  363. struct cmOrderDirectoriesCompare
  364. {
  365. typedef std::pair<int, int> ConflictPair;
  366. // The conflict pair is unique based on just the directory
  367. // (first). The second element is only used for displaying
  368. // information about why the entry is present.
  369. bool operator()(ConflictPair l, ConflictPair r)
  370. {
  371. return l.first == r.first;
  372. }
  373. };
  374. void cmOrderDirectories::FindConflicts()
  375. {
  376. // Allocate the conflict graph.
  377. this->ConflictGraph.resize(this->OriginalDirectories.size());
  378. this->DirectoryVisited.resize(this->OriginalDirectories.size(), 0);
  379. // Find directories conflicting with each entry.
  380. for (unsigned int i = 0; i < this->ConstraintEntries.size(); ++i) {
  381. this->ConstraintEntries[i]->FindConflicts(i);
  382. }
  383. // Clean up the conflict graph representation.
  384. for (ConflictList& cl : this->ConflictGraph) {
  385. // Sort the outgoing edges for each graph node so that the
  386. // original order will be preserved as much as possible.
  387. std::sort(cl.begin(), cl.end());
  388. // Make the edge list unique so cycle detection will be reliable.
  389. ConflictList::iterator last =
  390. std::unique(cl.begin(), cl.end(), cmOrderDirectoriesCompare());
  391. cl.erase(last, cl.end());
  392. }
  393. // Check items in implicit link directories.
  394. this->FindImplicitConflicts();
  395. }
  396. void cmOrderDirectories::FindImplicitConflicts()
  397. {
  398. // Check for items in implicit link directories that have conflicts
  399. // in the explicit directories.
  400. std::ostringstream conflicts;
  401. for (cmOrderDirectoriesConstraint* entry : this->ImplicitDirEntries) {
  402. entry->FindImplicitConflicts(conflicts);
  403. }
  404. // Skip warning if there were no conflicts.
  405. std::string const text = conflicts.str();
  406. if (text.empty()) {
  407. return;
  408. }
  409. // Warn about the conflicts.
  410. std::ostringstream w;
  411. w << "Cannot generate a safe " << this->Purpose << " for target "
  412. << this->Target->GetName()
  413. << " because files in some directories may conflict with "
  414. << " libraries in implicit directories:\n"
  415. << text << "Some of these libraries may not be found correctly.";
  416. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
  417. cmake::WARNING, w.str(), this->Target->GetBacktrace());
  418. }
  419. void cmOrderDirectories::OrderDirectories()
  420. {
  421. // Allow a cycle to be diagnosed once.
  422. this->CycleDiagnosed = false;
  423. this->WalkId = 0;
  424. // Iterate through the directories in the original order.
  425. for (unsigned int i = 0; i < this->OriginalDirectories.size(); ++i) {
  426. // Start a new DFS from this node.
  427. ++this->WalkId;
  428. this->VisitDirectory(i);
  429. }
  430. }
  431. void cmOrderDirectories::VisitDirectory(unsigned int i)
  432. {
  433. // Skip nodes already visited.
  434. if (this->DirectoryVisited[i]) {
  435. if (this->DirectoryVisited[i] == this->WalkId) {
  436. // We have reached a node previously visited on this DFS.
  437. // There is a cycle.
  438. this->DiagnoseCycle();
  439. }
  440. return;
  441. }
  442. // We are now visiting this node so mark it.
  443. this->DirectoryVisited[i] = this->WalkId;
  444. // Visit the neighbors of the node first.
  445. ConflictList const& clist = this->ConflictGraph[i];
  446. for (ConflictPair const& j : clist) {
  447. this->VisitDirectory(j.first);
  448. }
  449. // Now that all directories required to come before this one have
  450. // been emmitted, emit this directory.
  451. this->OrderedDirectories.push_back(this->OriginalDirectories[i]);
  452. }
  453. void cmOrderDirectories::DiagnoseCycle()
  454. {
  455. // Report the cycle at most once.
  456. if (this->CycleDiagnosed) {
  457. return;
  458. }
  459. this->CycleDiagnosed = true;
  460. // Construct the message.
  461. std::ostringstream e;
  462. e << "Cannot generate a safe " << this->Purpose << " for target "
  463. << this->Target->GetName()
  464. << " because there is a cycle in the constraint graph:\n";
  465. // Display the conflict graph.
  466. for (unsigned int i = 0; i < this->ConflictGraph.size(); ++i) {
  467. ConflictList const& clist = this->ConflictGraph[i];
  468. e << " dir " << i << " is [" << this->OriginalDirectories[i] << "]\n";
  469. for (ConflictPair const& j : clist) {
  470. e << " dir " << j.first << " must precede it due to ";
  471. this->ConstraintEntries[j.second]->Report(e);
  472. e << "\n";
  473. }
  474. }
  475. e << "Some of these libraries may not be found correctly.";
  476. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
  477. cmake::WARNING, e.str(), this->Target->GetBacktrace());
  478. }
  479. bool cmOrderDirectories::IsSameDirectory(std::string const& l,
  480. std::string const& r)
  481. {
  482. return this->GetRealPath(l) == this->GetRealPath(r);
  483. }
  484. std::string const& cmOrderDirectories::GetRealPath(std::string const& dir)
  485. {
  486. std::map<std::string, std::string>::iterator i =
  487. this->RealPaths.lower_bound(dir);
  488. if (i == this->RealPaths.end() ||
  489. this->RealPaths.key_comp()(dir, i->first)) {
  490. typedef std::map<std::string, std::string>::value_type value_type;
  491. i = this->RealPaths.insert(
  492. i, value_type(dir, cmSystemTools::GetRealPath(dir)));
  493. }
  494. return i->second;
  495. }