cmComputeTargetDepends.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmComputeTargetDepends.h"
  11. #include "cmComputeComponentGraph.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmSystemTools.h"
  16. #include "cmTarget.h"
  17. #include "cmake.h"
  18. #include <algorithm>
  19. #include <assert.h>
  20. /*
  21. This class is meant to analyze inter-target dependencies globally
  22. during the generation step. The goal is to produce a set of direct
  23. dependencies for each target such that no cycles are left and the
  24. build order is safe.
  25. For most target types cyclic dependencies are not allowed. However
  26. STATIC libraries may depend on each other in a cyclic fashion. In
  27. general the directed dependency graph forms a directed-acyclic-graph
  28. of strongly connected components. All strongly connected components
  29. should consist of only STATIC_LIBRARY targets.
  30. In order to safely break dependency cycles we must preserve all other
  31. dependencies passing through the corresponding strongly connected component.
  32. The approach taken by this class is as follows:
  33. - Collect all targets and form the original dependency graph
  34. - Run Tarjan's algorithm to extract the strongly connected components
  35. (error if any member of a non-trivial component is not STATIC)
  36. - The original dependencies imply a DAG on the components.
  37. Use the implied DAG to construct a final safe set of dependencies.
  38. The final dependency set is constructed as follows:
  39. - For each connected component targets are placed in an arbitrary
  40. order. Each target depends on the target following it in the order.
  41. The first target is designated the head and the last target the tail.
  42. (most components will be just 1 target anyway)
  43. - Original dependencies between targets in different components are
  44. converted to connect the depender's component tail to the
  45. dependee's component head.
  46. In most cases this will reproduce the original dependencies. However
  47. when there are cycles of static libraries they will be broken in a
  48. safe manner.
  49. For example, consider targets A0, A1, A2, B0, B1, B2, and C with these
  50. dependencies:
  51. A0 -> A1 -> A2 -> A0 , B0 -> B1 -> B2 -> B0 -> A0 , C -> B0
  52. Components may be identified as
  53. Component 0: A0, A1, A2
  54. Component 1: B0, B1, B2
  55. Component 2: C
  56. Intra-component dependencies are:
  57. 0: A0 -> A1 -> A2 , head=A0, tail=A2
  58. 1: B0 -> B1 -> B2 , head=B0, tail=B2
  59. 2: head=C, tail=C
  60. The inter-component dependencies are converted as:
  61. B0 -> A0 is component 1->0 and becomes B2 -> A0
  62. C -> B0 is component 2->1 and becomes C -> B0
  63. This leads to the final target dependencies:
  64. C -> B0 -> B1 -> B2 -> A0 -> A1 -> A2
  65. These produce a safe build order since C depends directly or
  66. transitively on all the static libraries it links.
  67. */
  68. //----------------------------------------------------------------------------
  69. cmComputeTargetDepends::cmComputeTargetDepends(cmGlobalGenerator* gg)
  70. {
  71. this->GlobalGenerator = gg;
  72. cmake* cm = this->GlobalGenerator->GetCMakeInstance();
  73. this->DebugMode = cm->GetPropertyAsBool("GLOBAL_DEPENDS_DEBUG_MODE");
  74. this->NoCycles = cm->GetPropertyAsBool("GLOBAL_DEPENDS_NO_CYCLES");
  75. }
  76. //----------------------------------------------------------------------------
  77. cmComputeTargetDepends::~cmComputeTargetDepends()
  78. {
  79. }
  80. //----------------------------------------------------------------------------
  81. bool cmComputeTargetDepends::Compute()
  82. {
  83. // Build the original graph.
  84. this->CollectTargets();
  85. this->CollectDepends();
  86. if(this->DebugMode)
  87. {
  88. this->DisplayGraph(this->InitialGraph, "initial");
  89. }
  90. // Identify components.
  91. cmComputeComponentGraph ccg(this->InitialGraph);
  92. if(this->DebugMode)
  93. {
  94. this->DisplayComponents(ccg);
  95. }
  96. if(!this->CheckComponents(ccg))
  97. {
  98. return false;
  99. }
  100. // Compute the final dependency graph.
  101. if(!this->ComputeFinalDepends(ccg))
  102. {
  103. return false;
  104. }
  105. if(this->DebugMode)
  106. {
  107. this->DisplayGraph(this->FinalGraph, "final");
  108. }
  109. return true;
  110. }
  111. //----------------------------------------------------------------------------
  112. void
  113. cmComputeTargetDepends::GetTargetDirectDepends(cmTarget* t,
  114. cmTargetDependSet& deps)
  115. {
  116. // Lookup the index for this target. All targets should be known by
  117. // this point.
  118. std::map<cmTarget*, int>::const_iterator tii = this->TargetIndex.find(t);
  119. assert(tii != this->TargetIndex.end());
  120. int i = tii->second;
  121. // Get its final dependencies.
  122. EdgeList const& nl = this->FinalGraph[i];
  123. for(EdgeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  124. {
  125. cmTarget* dep = this->Targets[*ni];
  126. cmTargetDependSet::iterator di = deps.insert(dep).first;
  127. di->SetType(ni->IsStrong());
  128. }
  129. }
  130. //----------------------------------------------------------------------------
  131. void cmComputeTargetDepends::CollectTargets()
  132. {
  133. // Collect all targets from all generators.
  134. std::vector<cmLocalGenerator*> const& lgens =
  135. this->GlobalGenerator->GetLocalGenerators();
  136. for(unsigned int i = 0; i < lgens.size(); ++i)
  137. {
  138. cmTargets& targets = lgens[i]->GetMakefile()->GetTargets();
  139. for(cmTargets::iterator ti = targets.begin(); ti != targets.end(); ++ti)
  140. {
  141. cmTarget* target = &ti->second;
  142. int index = static_cast<int>(this->Targets.size());
  143. this->TargetIndex[target] = index;
  144. this->Targets.push_back(target);
  145. }
  146. }
  147. }
  148. //----------------------------------------------------------------------------
  149. void cmComputeTargetDepends::CollectDepends()
  150. {
  151. // Allocate the dependency graph adjacency lists.
  152. this->InitialGraph.resize(this->Targets.size());
  153. // Compute each dependency list.
  154. for(unsigned int i=0; i < this->Targets.size(); ++i)
  155. {
  156. this->CollectTargetDepends(i);
  157. }
  158. }
  159. //----------------------------------------------------------------------------
  160. void cmComputeTargetDepends::CollectTargetDepends(int depender_index)
  161. {
  162. // Get the depender.
  163. cmTarget* depender = this->Targets[depender_index];
  164. // Loop over all targets linked directly.
  165. {
  166. cmTarget::LinkLibraryVectorType const& tlibs =
  167. depender->GetOriginalLinkLibraries();
  168. std::set<cmStdString> emitted;
  169. // A target should not depend on itself.
  170. emitted.insert(depender->GetName());
  171. for(cmTarget::LinkLibraryVectorType::const_iterator lib = tlibs.begin();
  172. lib != tlibs.end(); ++lib)
  173. {
  174. // Don't emit the same library twice for this target.
  175. if(emitted.insert(lib->first).second)
  176. {
  177. this->AddTargetDepend(depender_index, lib->first.c_str(), true);
  178. }
  179. }
  180. }
  181. // Loop over all utility dependencies.
  182. {
  183. std::set<cmStdString> const& tutils = depender->GetUtilities();
  184. std::set<cmStdString> emitted;
  185. // A target should not depend on itself.
  186. emitted.insert(depender->GetName());
  187. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  188. util != tutils.end(); ++util)
  189. {
  190. // Don't emit the same utility twice for this target.
  191. if(emitted.insert(*util).second)
  192. {
  193. this->AddTargetDepend(depender_index, util->c_str(), false);
  194. }
  195. }
  196. }
  197. }
  198. //----------------------------------------------------------------------------
  199. void cmComputeTargetDepends::AddTargetDepend(int depender_index,
  200. const char* dependee_name,
  201. bool linking)
  202. {
  203. // Get the depender.
  204. cmTarget* depender = this->Targets[depender_index];
  205. // Check the target's makefile first.
  206. cmTarget* dependee =
  207. depender->GetMakefile()->FindTargetToUse(dependee_name);
  208. // Skip targets that will not really be linked. This is probably a
  209. // name conflict between an external library and an executable
  210. // within the project.
  211. if(linking && dependee &&
  212. dependee->GetType() == cmTarget::EXECUTABLE &&
  213. !dependee->IsExecutableWithExports())
  214. {
  215. dependee = 0;
  216. }
  217. if(dependee)
  218. {
  219. this->AddTargetDepend(depender_index, dependee, linking);
  220. }
  221. }
  222. //----------------------------------------------------------------------------
  223. void cmComputeTargetDepends::AddTargetDepend(int depender_index,
  224. cmTarget* dependee,
  225. bool linking)
  226. {
  227. if(dependee->IsImported())
  228. {
  229. // Skip imported targets but follow their utility dependencies.
  230. std::set<cmStdString> const& utils = dependee->GetUtilities();
  231. for(std::set<cmStdString>::const_iterator i = utils.begin();
  232. i != utils.end(); ++i)
  233. {
  234. if(cmTarget* transitive_dependee =
  235. dependee->GetMakefile()->FindTargetToUse(i->c_str()))
  236. {
  237. this->AddTargetDepend(depender_index, transitive_dependee, false);
  238. }
  239. }
  240. }
  241. else
  242. {
  243. // Lookup the index for this target. All targets should be known by
  244. // this point.
  245. std::map<cmTarget*, int>::const_iterator tii =
  246. this->TargetIndex.find(dependee);
  247. assert(tii != this->TargetIndex.end());
  248. int dependee_index = tii->second;
  249. // Add this entry to the dependency graph.
  250. this->InitialGraph[depender_index].push_back(
  251. cmGraphEdge(dependee_index, !linking));
  252. }
  253. }
  254. //----------------------------------------------------------------------------
  255. void
  256. cmComputeTargetDepends::DisplayGraph(Graph const& graph, const char* name)
  257. {
  258. fprintf(stderr, "The %s target dependency graph is:\n", name);
  259. int n = static_cast<int>(graph.size());
  260. for(int depender_index = 0; depender_index < n; ++depender_index)
  261. {
  262. EdgeList const& nl = graph[depender_index];
  263. cmTarget* depender = this->Targets[depender_index];
  264. fprintf(stderr, "target %d is [%s]\n",
  265. depender_index, depender->GetName());
  266. for(EdgeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  267. {
  268. int dependee_index = *ni;
  269. cmTarget* dependee = this->Targets[dependee_index];
  270. fprintf(stderr, " depends on target %d [%s] (%s)\n", dependee_index,
  271. dependee->GetName(), ni->IsStrong()? "strong" : "weak");
  272. }
  273. }
  274. fprintf(stderr, "\n");
  275. }
  276. //----------------------------------------------------------------------------
  277. void
  278. cmComputeTargetDepends
  279. ::DisplayComponents(cmComputeComponentGraph const& ccg)
  280. {
  281. fprintf(stderr, "The strongly connected components are:\n");
  282. std::vector<NodeList> const& components = ccg.GetComponents();
  283. int n = static_cast<int>(components.size());
  284. for(int c = 0; c < n; ++c)
  285. {
  286. NodeList const& nl = components[c];
  287. fprintf(stderr, "Component (%d):\n", c);
  288. for(NodeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  289. {
  290. int i = *ni;
  291. fprintf(stderr, " contains target %d [%s]\n",
  292. i, this->Targets[i]->GetName());
  293. }
  294. }
  295. fprintf(stderr, "\n");
  296. }
  297. //----------------------------------------------------------------------------
  298. bool
  299. cmComputeTargetDepends
  300. ::CheckComponents(cmComputeComponentGraph const& ccg)
  301. {
  302. // All non-trivial components should consist only of static
  303. // libraries.
  304. std::vector<NodeList> const& components = ccg.GetComponents();
  305. int nc = static_cast<int>(components.size());
  306. for(int c=0; c < nc; ++c)
  307. {
  308. // Get the current component.
  309. NodeList const& nl = components[c];
  310. // Skip trivial components.
  311. if(nl.size() < 2)
  312. {
  313. continue;
  314. }
  315. // Immediately complain if no cycles are allowed at all.
  316. if(this->NoCycles)
  317. {
  318. this->ComplainAboutBadComponent(ccg, c);
  319. return false;
  320. }
  321. // Make sure the component is all STATIC_LIBRARY targets.
  322. for(NodeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  323. {
  324. if(this->Targets[*ni]->GetType() != cmTarget::STATIC_LIBRARY)
  325. {
  326. this->ComplainAboutBadComponent(ccg, c);
  327. return false;
  328. }
  329. }
  330. }
  331. return true;
  332. }
  333. //----------------------------------------------------------------------------
  334. void
  335. cmComputeTargetDepends
  336. ::ComplainAboutBadComponent(cmComputeComponentGraph const& ccg, int c,
  337. bool strong)
  338. {
  339. // Construct the error message.
  340. cmOStringStream e;
  341. e << "The inter-target dependency graph contains the following "
  342. << "strongly connected component (cycle):\n";
  343. std::vector<NodeList> const& components = ccg.GetComponents();
  344. std::vector<int> const& cmap = ccg.GetComponentMap();
  345. NodeList const& cl = components[c];
  346. for(NodeList::const_iterator ci = cl.begin(); ci != cl.end(); ++ci)
  347. {
  348. // Get the depender.
  349. int i = *ci;
  350. cmTarget* depender = this->Targets[i];
  351. // Describe the depender.
  352. e << " \"" << depender->GetName() << "\" of type "
  353. << cmTarget::GetTargetTypeName(depender->GetType()) << "\n";
  354. // List its dependencies that are inside the component.
  355. EdgeList const& nl = this->InitialGraph[i];
  356. for(EdgeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  357. {
  358. int j = *ni;
  359. if(cmap[j] == c)
  360. {
  361. cmTarget* dependee = this->Targets[j];
  362. e << " depends on \"" << dependee->GetName() << "\""
  363. << " (" << (ni->IsStrong()? "strong" : "weak") << ")\n";
  364. }
  365. }
  366. }
  367. if(strong)
  368. {
  369. // Custom command executable dependencies cannot occur within a
  370. // component of static libraries. The cycle must appear in calls
  371. // to add_dependencies.
  372. e << "The component contains at least one cycle consisting of strong "
  373. << "dependencies (created by add_dependencies) that cannot be broken.";
  374. }
  375. else if(this->NoCycles)
  376. {
  377. e << "The GLOBAL_DEPENDS_NO_CYCLES global property is enabled, so "
  378. << "cyclic dependencies are not allowed even among static libraries.";
  379. }
  380. else
  381. {
  382. e << "At least one of these targets is not a STATIC_LIBRARY. "
  383. << "Cyclic dependencies are allowed only among static libraries.";
  384. }
  385. cmSystemTools::Error(e.str().c_str());
  386. }
  387. //----------------------------------------------------------------------------
  388. bool
  389. cmComputeTargetDepends
  390. ::IntraComponent(std::vector<int> const& cmap, int c, int i, int* head,
  391. std::set<int>& emitted, std::set<int>& visited)
  392. {
  393. if(!visited.insert(i).second)
  394. {
  395. // Cycle in utility depends!
  396. return false;
  397. }
  398. if(emitted.insert(i).second)
  399. {
  400. // Honor strong intra-component edges in the final order.
  401. EdgeList const& el = this->InitialGraph[i];
  402. for(EdgeList::const_iterator ei = el.begin(); ei != el.end(); ++ei)
  403. {
  404. int j = *ei;
  405. if(cmap[j] == c && ei->IsStrong())
  406. {
  407. this->FinalGraph[i].push_back(cmGraphEdge(j, true));
  408. if(!this->IntraComponent(cmap, c, j, head, emitted, visited))
  409. {
  410. return false;
  411. }
  412. }
  413. }
  414. // Prepend to a linear linked-list of intra-component edges.
  415. if(*head >= 0)
  416. {
  417. this->FinalGraph[i].push_back(cmGraphEdge(*head, false));
  418. }
  419. else
  420. {
  421. this->ComponentTail[c] = i;
  422. }
  423. *head = i;
  424. }
  425. return true;
  426. }
  427. //----------------------------------------------------------------------------
  428. bool
  429. cmComputeTargetDepends
  430. ::ComputeFinalDepends(cmComputeComponentGraph const& ccg)
  431. {
  432. // Get the component graph information.
  433. std::vector<NodeList> const& components = ccg.GetComponents();
  434. Graph const& cgraph = ccg.GetComponentGraph();
  435. // Allocate the final graph.
  436. this->FinalGraph.resize(0);
  437. this->FinalGraph.resize(this->InitialGraph.size());
  438. // Choose intra-component edges to linearize dependencies.
  439. std::vector<int> const& cmap = ccg.GetComponentMap();
  440. this->ComponentHead.resize(components.size());
  441. this->ComponentTail.resize(components.size());
  442. int nc = static_cast<int>(components.size());
  443. for(int c=0; c < nc; ++c)
  444. {
  445. int head = -1;
  446. std::set<int> emitted;
  447. NodeList const& nl = components[c];
  448. for(NodeList::const_reverse_iterator ni = nl.rbegin();
  449. ni != nl.rend(); ++ni)
  450. {
  451. std::set<int> visited;
  452. if(!this->IntraComponent(cmap, c, *ni, &head, emitted, visited))
  453. {
  454. // Cycle in add_dependencies within component!
  455. this->ComplainAboutBadComponent(ccg, c, true);
  456. return false;
  457. }
  458. }
  459. this->ComponentHead[c] = head;
  460. }
  461. // Convert inter-component edges to connect component tails to heads.
  462. int n = static_cast<int>(cgraph.size());
  463. for(int depender_component=0; depender_component < n; ++depender_component)
  464. {
  465. int depender_component_tail = this->ComponentTail[depender_component];
  466. EdgeList const& nl = cgraph[depender_component];
  467. for(EdgeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  468. {
  469. int dependee_component = *ni;
  470. int dependee_component_head = this->ComponentHead[dependee_component];
  471. this->FinalGraph[depender_component_tail]
  472. .push_back(cmGraphEdge(dependee_component_head, ni->IsStrong()));
  473. }
  474. }
  475. return true;
  476. }