cmComputeTargetDepends.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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 "cmSourceFile.h"
  17. #include "cmTarget.h"
  18. #include "cmake.h"
  19. #include <algorithm>
  20. #include <assert.h>
  21. /*
  22. This class is meant to analyze inter-target dependencies globally
  23. during the generation step. The goal is to produce a set of direct
  24. dependencies for each target such that no cycles are left and the
  25. build order is safe.
  26. For most target types cyclic dependencies are not allowed. However
  27. STATIC libraries may depend on each other in a cyclic fashion. In
  28. general the directed dependency graph forms a directed-acyclic-graph
  29. of strongly connected components. All strongly connected components
  30. should consist of only STATIC_LIBRARY targets.
  31. In order to safely break dependency cycles we must preserve all other
  32. dependencies passing through the corresponding strongly connected component.
  33. The approach taken by this class is as follows:
  34. - Collect all targets and form the original dependency graph
  35. - Run Tarjan's algorithm to extract the strongly connected components
  36. (error if any member of a non-trivial component is not STATIC)
  37. - The original dependencies imply a DAG on the components.
  38. Use the implied DAG to construct a final safe set of dependencies.
  39. The final dependency set is constructed as follows:
  40. - For each connected component targets are placed in an arbitrary
  41. order. Each target depends on the target following it in the order.
  42. The first target is designated the head and the last target the tail.
  43. (most components will be just 1 target anyway)
  44. - Original dependencies between targets in different components are
  45. converted to connect the depender's component tail to the
  46. dependee's component head.
  47. In most cases this will reproduce the original dependencies. However
  48. when there are cycles of static libraries they will be broken in a
  49. safe manner.
  50. For example, consider targets A0, A1, A2, B0, B1, B2, and C with these
  51. dependencies:
  52. A0 -> A1 -> A2 -> A0 , B0 -> B1 -> B2 -> B0 -> A0 , C -> B0
  53. Components may be identified as
  54. Component 0: A0, A1, A2
  55. Component 1: B0, B1, B2
  56. Component 2: C
  57. Intra-component dependencies are:
  58. 0: A0 -> A1 -> A2 , head=A0, tail=A2
  59. 1: B0 -> B1 -> B2 , head=B0, tail=B2
  60. 2: head=C, tail=C
  61. The inter-component dependencies are converted as:
  62. B0 -> A0 is component 1->0 and becomes B2 -> A0
  63. C -> B0 is component 2->1 and becomes C -> B0
  64. This leads to the final target dependencies:
  65. C -> B0 -> B1 -> B2 -> A0 -> A1 -> A2
  66. These produce a safe build order since C depends directly or
  67. transitively on all the static libraries it links.
  68. */
  69. //----------------------------------------------------------------------------
  70. cmComputeTargetDepends::cmComputeTargetDepends(cmGlobalGenerator* gg)
  71. {
  72. this->GlobalGenerator = gg;
  73. cmake* cm = this->GlobalGenerator->GetCMakeInstance();
  74. this->DebugMode = cm->GetPropertyAsBool("GLOBAL_DEPENDS_DEBUG_MODE");
  75. this->NoCycles = cm->GetPropertyAsBool("GLOBAL_DEPENDS_NO_CYCLES");
  76. }
  77. //----------------------------------------------------------------------------
  78. cmComputeTargetDepends::~cmComputeTargetDepends()
  79. {
  80. }
  81. //----------------------------------------------------------------------------
  82. bool cmComputeTargetDepends::Compute()
  83. {
  84. // Build the original graph.
  85. this->CollectTargets();
  86. this->CollectDepends();
  87. if(this->DebugMode)
  88. {
  89. this->DisplayGraph(this->InitialGraph, "initial");
  90. }
  91. // Identify components.
  92. cmComputeComponentGraph ccg(this->InitialGraph);
  93. if(this->DebugMode)
  94. {
  95. this->DisplayComponents(ccg);
  96. }
  97. if(!this->CheckComponents(ccg))
  98. {
  99. return false;
  100. }
  101. // Compute the final dependency graph.
  102. if(!this->ComputeFinalDepends(ccg))
  103. {
  104. return false;
  105. }
  106. if(this->DebugMode)
  107. {
  108. this->DisplayGraph(this->FinalGraph, "final");
  109. }
  110. return true;
  111. }
  112. //----------------------------------------------------------------------------
  113. void
  114. cmComputeTargetDepends::GetTargetDirectDepends(cmTarget const* t,
  115. cmTargetDependSet& deps)
  116. {
  117. // Lookup the index for this target. All targets should be known by
  118. // this point.
  119. std::map<cmTarget const*, int>::const_iterator tii
  120. = this->TargetIndex.find(t);
  121. assert(tii != this->TargetIndex.end());
  122. int i = tii->second;
  123. // Get its final dependencies.
  124. EdgeList const& nl = this->FinalGraph[i];
  125. for(EdgeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  126. {
  127. cmTarget const* dep = this->Targets[*ni];
  128. cmTargetDependSet::iterator di = deps.insert(dep).first;
  129. di->SetType(ni->IsStrong());
  130. }
  131. }
  132. //----------------------------------------------------------------------------
  133. void cmComputeTargetDepends::CollectTargets()
  134. {
  135. // Collect all targets from all generators.
  136. std::vector<cmLocalGenerator*> const& lgens =
  137. this->GlobalGenerator->GetLocalGenerators();
  138. for(unsigned int i = 0; i < lgens.size(); ++i)
  139. {
  140. const cmTargets& targets = lgens[i]->GetMakefile()->GetTargets();
  141. for(cmTargets::const_iterator ti = targets.begin();
  142. ti != targets.end(); ++ti)
  143. {
  144. cmTarget const* target = &ti->second;
  145. int index = static_cast<int>(this->Targets.size());
  146. this->TargetIndex[target] = index;
  147. this->Targets.push_back(target);
  148. }
  149. }
  150. }
  151. //----------------------------------------------------------------------------
  152. void cmComputeTargetDepends::CollectDepends()
  153. {
  154. // Allocate the dependency graph adjacency lists.
  155. this->InitialGraph.resize(this->Targets.size());
  156. // Compute each dependency list.
  157. for(unsigned int i=0; i < this->Targets.size(); ++i)
  158. {
  159. this->CollectTargetDepends(i);
  160. }
  161. }
  162. //----------------------------------------------------------------------------
  163. void cmComputeTargetDepends::CollectTargetDepends(int depender_index)
  164. {
  165. // Get the depender.
  166. cmTarget const* depender = this->Targets[depender_index];
  167. if (depender->GetType() == cmTarget::INTERFACE_LIBRARY)
  168. {
  169. return;
  170. }
  171. // Loop over all targets linked directly in all configs.
  172. // We need to make targets depend on the union of all config-specific
  173. // dependencies in all targets, because the generated build-systems can't
  174. // deal with config-specific dependencies.
  175. {
  176. std::set<std::string> emitted;
  177. cmGeneratorTarget* gt = depender->GetMakefile()->GetLocalGenerator()
  178. ->GetGlobalGenerator()
  179. ->GetGeneratorTarget(depender);
  180. std::vector<std::string> configs;
  181. depender->GetMakefile()->GetConfigurations(configs);
  182. if (configs.empty())
  183. {
  184. configs.push_back("");
  185. }
  186. for (std::vector<std::string>::const_iterator it = configs.begin();
  187. it != configs.end(); ++it)
  188. {
  189. std::vector<cmSourceFile const*> objectFiles;
  190. gt->GetExternalObjects(objectFiles, *it);
  191. for(std::vector<cmSourceFile const*>::const_iterator
  192. oi = objectFiles.begin(); oi != objectFiles.end(); ++oi)
  193. {
  194. std::string objLib = (*oi)->GetObjectLibrary();
  195. if (!objLib.empty() && emitted.insert(objLib).second)
  196. {
  197. if(depender->GetType() != cmTarget::EXECUTABLE &&
  198. depender->GetType() != cmTarget::STATIC_LIBRARY &&
  199. depender->GetType() != cmTarget::SHARED_LIBRARY &&
  200. depender->GetType() != cmTarget::MODULE_LIBRARY)
  201. {
  202. this->GlobalGenerator->GetCMakeInstance()
  203. ->IssueMessage(cmake::FATAL_ERROR,
  204. "Only executables and non-OBJECT libraries may "
  205. "reference target objects.",
  206. depender->GetBacktrace());
  207. return;
  208. }
  209. const_cast<cmTarget*>(depender)->AddUtility(objLib);
  210. }
  211. }
  212. cmTarget::LinkImplementation const* impl =
  213. depender->GetLinkImplementation(*it);
  214. // A target should not depend on itself.
  215. emitted.insert(depender->GetName());
  216. for(std::vector<cmLinkItem>::const_iterator
  217. lib = impl->Libraries.begin();
  218. lib != impl->Libraries.end(); ++lib)
  219. {
  220. // Don't emit the same library twice for this target.
  221. if(emitted.insert(*lib).second)
  222. {
  223. this->AddTargetDepend(depender_index, *lib, true);
  224. this->AddInterfaceDepends(depender_index, *lib, emitted);
  225. }
  226. }
  227. }
  228. }
  229. // Loop over all utility dependencies.
  230. {
  231. std::set<cmLinkItem> const& tutils = depender->GetUtilityItems();
  232. std::set<std::string> emitted;
  233. // A target should not depend on itself.
  234. emitted.insert(depender->GetName());
  235. for(std::set<cmLinkItem>::const_iterator util = tutils.begin();
  236. util != tutils.end(); ++util)
  237. {
  238. // Don't emit the same utility twice for this target.
  239. if(emitted.insert(*util).second)
  240. {
  241. this->AddTargetDepend(depender_index, *util, false);
  242. }
  243. }
  244. }
  245. }
  246. //----------------------------------------------------------------------------
  247. void cmComputeTargetDepends::AddInterfaceDepends(int depender_index,
  248. cmTarget const* dependee,
  249. const std::string& config,
  250. std::set<std::string> &emitted)
  251. {
  252. cmTarget const* depender = this->Targets[depender_index];
  253. if(cmTarget::LinkInterface const* iface =
  254. dependee->GetLinkInterface(config, depender))
  255. {
  256. for(std::vector<cmLinkItem>::const_iterator
  257. lib = iface->Libraries.begin();
  258. lib != iface->Libraries.end(); ++lib)
  259. {
  260. // Don't emit the same library twice for this target.
  261. if(emitted.insert(*lib).second)
  262. {
  263. this->AddTargetDepend(depender_index, *lib, true);
  264. this->AddInterfaceDepends(depender_index, *lib, emitted);
  265. }
  266. }
  267. }
  268. }
  269. //----------------------------------------------------------------------------
  270. void cmComputeTargetDepends::AddInterfaceDepends(int depender_index,
  271. cmLinkItem const& dependee_name,
  272. std::set<std::string> &emitted)
  273. {
  274. cmTarget const* depender = this->Targets[depender_index];
  275. cmTarget const* dependee = dependee_name.Target;
  276. // Skip targets that will not really be linked. This is probably a
  277. // name conflict between an external library and an executable
  278. // within the project.
  279. if(dependee &&
  280. dependee->GetType() == cmTarget::EXECUTABLE &&
  281. !dependee->IsExecutableWithExports())
  282. {
  283. dependee = 0;
  284. }
  285. if(dependee)
  286. {
  287. this->AddInterfaceDepends(depender_index, dependee, "", emitted);
  288. std::vector<std::string> configs;
  289. depender->GetMakefile()->GetConfigurations(configs);
  290. for (std::vector<std::string>::const_iterator it = configs.begin();
  291. it != configs.end(); ++it)
  292. {
  293. // A target should not depend on itself.
  294. emitted.insert(depender->GetName());
  295. this->AddInterfaceDepends(depender_index, dependee,
  296. *it, emitted);
  297. }
  298. }
  299. }
  300. //----------------------------------------------------------------------------
  301. void cmComputeTargetDepends::AddTargetDepend(
  302. int depender_index, cmLinkItem const& dependee_name,
  303. bool linking)
  304. {
  305. // Get the depender.
  306. cmTarget const* depender = this->Targets[depender_index];
  307. // Check the target's makefile first.
  308. cmTarget const* dependee = dependee_name.Target;
  309. if(!dependee && !linking &&
  310. (depender->GetType() != cmTarget::GLOBAL_TARGET))
  311. {
  312. cmMakefile *makefile = depender->GetMakefile();
  313. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  314. bool issueMessage = false;
  315. cmOStringStream e;
  316. switch(depender->GetPolicyStatusCMP0046())
  317. {
  318. case cmPolicies::WARN:
  319. e << (makefile->GetPolicies()
  320. ->GetPolicyWarning(cmPolicies::CMP0046)) << "\n";
  321. issueMessage = true;
  322. case cmPolicies::OLD:
  323. break;
  324. case cmPolicies::NEW:
  325. case cmPolicies::REQUIRED_IF_USED:
  326. case cmPolicies::REQUIRED_ALWAYS:
  327. issueMessage = true;
  328. messageType = cmake::FATAL_ERROR;
  329. }
  330. if(issueMessage)
  331. {
  332. cmake* cm = this->GlobalGenerator->GetCMakeInstance();
  333. e << "The dependency target \"" << dependee_name
  334. << "\" of target \"" << depender->GetName() << "\" does not exist.";
  335. cmListFileBacktrace const* backtrace =
  336. depender->GetUtilityBacktrace(dependee_name);
  337. if(backtrace)
  338. {
  339. cm->IssueMessage(messageType, e.str(), *backtrace);
  340. }
  341. else
  342. {
  343. cm->IssueMessage(messageType, e.str());
  344. }
  345. }
  346. }
  347. // Skip targets that will not really be linked. This is probably a
  348. // name conflict between an external library and an executable
  349. // within the project.
  350. if(linking && dependee &&
  351. dependee->GetType() == cmTarget::EXECUTABLE &&
  352. !dependee->IsExecutableWithExports())
  353. {
  354. dependee = 0;
  355. }
  356. if(dependee)
  357. {
  358. this->AddTargetDepend(depender_index, dependee, linking);
  359. }
  360. }
  361. //----------------------------------------------------------------------------
  362. void cmComputeTargetDepends::AddTargetDepend(int depender_index,
  363. cmTarget const* dependee,
  364. bool linking)
  365. {
  366. if(dependee->IsImported())
  367. {
  368. // Skip imported targets but follow their utility dependencies.
  369. std::set<cmLinkItem> const& utils = dependee->GetUtilityItems();
  370. for(std::set<cmLinkItem>::const_iterator i = utils.begin();
  371. i != utils.end(); ++i)
  372. {
  373. if(cmTarget const* transitive_dependee = i->Target)
  374. {
  375. this->AddTargetDepend(depender_index, transitive_dependee, false);
  376. }
  377. }
  378. }
  379. else
  380. {
  381. // Lookup the index for this target. All targets should be known by
  382. // this point.
  383. std::map<cmTarget const*, int>::const_iterator tii =
  384. this->TargetIndex.find(dependee);
  385. assert(tii != this->TargetIndex.end());
  386. int dependee_index = tii->second;
  387. // Add this entry to the dependency graph.
  388. this->InitialGraph[depender_index].push_back(
  389. cmGraphEdge(dependee_index, !linking));
  390. }
  391. }
  392. //----------------------------------------------------------------------------
  393. void
  394. cmComputeTargetDepends::DisplayGraph(Graph const& graph,
  395. const std::string& name)
  396. {
  397. fprintf(stderr, "The %s target dependency graph is:\n", name.c_str());
  398. int n = static_cast<int>(graph.size());
  399. for(int depender_index = 0; depender_index < n; ++depender_index)
  400. {
  401. EdgeList const& nl = graph[depender_index];
  402. cmTarget const* depender = this->Targets[depender_index];
  403. fprintf(stderr, "target %d is [%s]\n",
  404. depender_index, depender->GetName().c_str());
  405. for(EdgeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  406. {
  407. int dependee_index = *ni;
  408. cmTarget const* dependee = this->Targets[dependee_index];
  409. fprintf(stderr, " depends on target %d [%s] (%s)\n", dependee_index,
  410. dependee->GetName().c_str(), ni->IsStrong()? "strong" : "weak");
  411. }
  412. }
  413. fprintf(stderr, "\n");
  414. }
  415. //----------------------------------------------------------------------------
  416. void
  417. cmComputeTargetDepends
  418. ::DisplayComponents(cmComputeComponentGraph const& ccg)
  419. {
  420. fprintf(stderr, "The strongly connected components are:\n");
  421. std::vector<NodeList> const& components = ccg.GetComponents();
  422. int n = static_cast<int>(components.size());
  423. for(int c = 0; c < n; ++c)
  424. {
  425. NodeList const& nl = components[c];
  426. fprintf(stderr, "Component (%d):\n", c);
  427. for(NodeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  428. {
  429. int i = *ni;
  430. fprintf(stderr, " contains target %d [%s]\n",
  431. i, this->Targets[i]->GetName().c_str());
  432. }
  433. }
  434. fprintf(stderr, "\n");
  435. }
  436. //----------------------------------------------------------------------------
  437. bool
  438. cmComputeTargetDepends
  439. ::CheckComponents(cmComputeComponentGraph const& ccg)
  440. {
  441. // All non-trivial components should consist only of static
  442. // libraries.
  443. std::vector<NodeList> const& components = ccg.GetComponents();
  444. int nc = static_cast<int>(components.size());
  445. for(int c=0; c < nc; ++c)
  446. {
  447. // Get the current component.
  448. NodeList const& nl = components[c];
  449. // Skip trivial components.
  450. if(nl.size() < 2)
  451. {
  452. continue;
  453. }
  454. // Immediately complain if no cycles are allowed at all.
  455. if(this->NoCycles)
  456. {
  457. this->ComplainAboutBadComponent(ccg, c);
  458. return false;
  459. }
  460. // Make sure the component is all STATIC_LIBRARY targets.
  461. for(NodeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  462. {
  463. if(this->Targets[*ni]->GetType() != cmTarget::STATIC_LIBRARY)
  464. {
  465. this->ComplainAboutBadComponent(ccg, c);
  466. return false;
  467. }
  468. }
  469. }
  470. return true;
  471. }
  472. //----------------------------------------------------------------------------
  473. void
  474. cmComputeTargetDepends
  475. ::ComplainAboutBadComponent(cmComputeComponentGraph const& ccg, int c,
  476. bool strong)
  477. {
  478. // Construct the error message.
  479. cmOStringStream e;
  480. e << "The inter-target dependency graph contains the following "
  481. << "strongly connected component (cycle):\n";
  482. std::vector<NodeList> const& components = ccg.GetComponents();
  483. std::vector<int> const& cmap = ccg.GetComponentMap();
  484. NodeList const& cl = components[c];
  485. for(NodeList::const_iterator ci = cl.begin(); ci != cl.end(); ++ci)
  486. {
  487. // Get the depender.
  488. int i = *ci;
  489. cmTarget const* depender = this->Targets[i];
  490. // Describe the depender.
  491. e << " \"" << depender->GetName() << "\" of type "
  492. << cmTarget::GetTargetTypeName(depender->GetType()) << "\n";
  493. // List its dependencies that are inside the component.
  494. EdgeList const& nl = this->InitialGraph[i];
  495. for(EdgeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  496. {
  497. int j = *ni;
  498. if(cmap[j] == c)
  499. {
  500. cmTarget const* dependee = this->Targets[j];
  501. e << " depends on \"" << dependee->GetName() << "\""
  502. << " (" << (ni->IsStrong()? "strong" : "weak") << ")\n";
  503. }
  504. }
  505. }
  506. if(strong)
  507. {
  508. // Custom command executable dependencies cannot occur within a
  509. // component of static libraries. The cycle must appear in calls
  510. // to add_dependencies.
  511. e << "The component contains at least one cycle consisting of strong "
  512. << "dependencies (created by add_dependencies) that cannot be broken.";
  513. }
  514. else if(this->NoCycles)
  515. {
  516. e << "The GLOBAL_DEPENDS_NO_CYCLES global property is enabled, so "
  517. << "cyclic dependencies are not allowed even among static libraries.";
  518. }
  519. else
  520. {
  521. e << "At least one of these targets is not a STATIC_LIBRARY. "
  522. << "Cyclic dependencies are allowed only among static libraries.";
  523. }
  524. cmSystemTools::Error(e.str().c_str());
  525. }
  526. //----------------------------------------------------------------------------
  527. bool
  528. cmComputeTargetDepends
  529. ::IntraComponent(std::vector<int> const& cmap, int c, int i, int* head,
  530. std::set<int>& emitted, std::set<int>& visited)
  531. {
  532. if(!visited.insert(i).second)
  533. {
  534. // Cycle in utility depends!
  535. return false;
  536. }
  537. if(emitted.insert(i).second)
  538. {
  539. // Honor strong intra-component edges in the final order.
  540. EdgeList const& el = this->InitialGraph[i];
  541. for(EdgeList::const_iterator ei = el.begin(); ei != el.end(); ++ei)
  542. {
  543. int j = *ei;
  544. if(cmap[j] == c && ei->IsStrong())
  545. {
  546. this->FinalGraph[i].push_back(cmGraphEdge(j, true));
  547. if(!this->IntraComponent(cmap, c, j, head, emitted, visited))
  548. {
  549. return false;
  550. }
  551. }
  552. }
  553. // Prepend to a linear linked-list of intra-component edges.
  554. if(*head >= 0)
  555. {
  556. this->FinalGraph[i].push_back(cmGraphEdge(*head, false));
  557. }
  558. else
  559. {
  560. this->ComponentTail[c] = i;
  561. }
  562. *head = i;
  563. }
  564. return true;
  565. }
  566. //----------------------------------------------------------------------------
  567. bool
  568. cmComputeTargetDepends
  569. ::ComputeFinalDepends(cmComputeComponentGraph const& ccg)
  570. {
  571. // Get the component graph information.
  572. std::vector<NodeList> const& components = ccg.GetComponents();
  573. Graph const& cgraph = ccg.GetComponentGraph();
  574. // Allocate the final graph.
  575. this->FinalGraph.resize(0);
  576. this->FinalGraph.resize(this->InitialGraph.size());
  577. // Choose intra-component edges to linearize dependencies.
  578. std::vector<int> const& cmap = ccg.GetComponentMap();
  579. this->ComponentHead.resize(components.size());
  580. this->ComponentTail.resize(components.size());
  581. int nc = static_cast<int>(components.size());
  582. for(int c=0; c < nc; ++c)
  583. {
  584. int head = -1;
  585. std::set<int> emitted;
  586. NodeList const& nl = components[c];
  587. for(NodeList::const_reverse_iterator ni = nl.rbegin();
  588. ni != nl.rend(); ++ni)
  589. {
  590. std::set<int> visited;
  591. if(!this->IntraComponent(cmap, c, *ni, &head, emitted, visited))
  592. {
  593. // Cycle in add_dependencies within component!
  594. this->ComplainAboutBadComponent(ccg, c, true);
  595. return false;
  596. }
  597. }
  598. this->ComponentHead[c] = head;
  599. }
  600. // Convert inter-component edges to connect component tails to heads.
  601. int n = static_cast<int>(cgraph.size());
  602. for(int depender_component=0; depender_component < n; ++depender_component)
  603. {
  604. int depender_component_tail = this->ComponentTail[depender_component];
  605. EdgeList const& nl = cgraph[depender_component];
  606. for(EdgeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  607. {
  608. int dependee_component = *ni;
  609. int dependee_component_head = this->ComponentHead[dependee_component];
  610. this->FinalGraph[depender_component_tail]
  611. .push_back(cmGraphEdge(dependee_component_head, ni->IsStrong()));
  612. }
  613. }
  614. return true;
  615. }