[Cmake-commits] [cmake-commits] king committed cmComputeLinkDepends.cxx 1.23 1.24 cmComputeLinkDepends.h 1.12 1.13

cmake-commits at cmake.org cmake-commits at cmake.org
Wed Aug 27 10:22:00 EDT 2008


Update of /cvsroot/CMake/CMake/Source
In directory public:/mounts/ram/cvs-serv32717/Source

Modified Files:
	cmComputeLinkDepends.cxx cmComputeLinkDepends.h 
Log Message:
ENH: New link line item ordering algorithm

This change introduces a new algorithm for link line construction.  The
order it computes always begins with the exact link line specified by
the user.  Dependencies of items specified by the user are tracked, and
those that are not already satisified by the line are appended to it at
the end with minimal repeats.  This restores the behavior of CMake 2.4
and below while still fixing some of its bugs.  See issue #7546.


Index: cmComputeLinkDepends.h
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmComputeLinkDepends.h,v
retrieving revision 1.12
retrieving revision 1.13
diff -C 2 -d -r1.12 -r1.13
*** cmComputeLinkDepends.h	27 Aug 2008 14:21:50 -0000	1.12
--- cmComputeLinkDepends.h	27 Aug 2008 14:21:57 -0000	1.13
***************
*** 132,144 ****
    void OrderLinkEntires();
    std::vector<char> ComponentVisited;
    std::vector<int> FinalLinkOrder;
!   void DisplayComponents(cmComputeComponentGraph const& ccg);
!   void VisitComponent(cmComputeComponentGraph const& ccg, unsigned int i);
!   void EmitComponent(NodeList const& nl);
    void DisplayFinalEntries();
  
!   // Preservation of original link line.
    std::vector<int> OriginalEntries;
-   void PreserveOriginalEntries();
  
    // Compatibility help.
--- 132,162 ----
    void OrderLinkEntires();
    std::vector<char> ComponentVisited;
+   std::vector<int> ComponentOrder;
+   int ComponentOrderId;
+   struct PendingComponent
+   {
+     // The real component id.  Needed because the map is indexed by
+     // component topological index.
+     int Id;
+ 
+     // The number of times the component needs to be seen.  This is
+     // always 1 for trivial components and is initially 2 for
+     // non-trivial components.
+     int Count;
+ 
+     // The entries yet to be seen to complete the component.
+     DependSet Entries;
+   };
+   std::map<int, PendingComponent> PendingComponents;
+   cmComputeComponentGraph* CCG;
    std::vector<int> FinalLinkOrder;
!   void DisplayComponents();
!   void VisitComponent(unsigned int c);
!   void VisitEntry(int index);
!   PendingComponent& MakePendingComponent(unsigned int component);
    void DisplayFinalEntries();
  
!   // Record of the original link line.
    std::vector<int> OriginalEntries;
  
    // Compatibility help.

Index: cmComputeLinkDepends.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmComputeLinkDepends.cxx,v
retrieving revision 1.23
retrieving revision 1.24
diff -C 2 -d -r1.23 -r1.24
*** cmComputeLinkDepends.cxx	27 Aug 2008 14:21:50 -0000	1.23
--- cmComputeLinkDepends.cxx	27 Aug 2008 14:21:57 -0000	1.24
***************
*** 34,41 ****
  the string naming it.  A graph of dependencies is created in which
  each node corresponds to one item and directed eges lead from nodes to
! those which must *precede* them on the link line.  For example, the
  graph
  
!   C -> B -> A
  
  will lead to the link line order
--- 34,41 ----
  the string naming it.  A graph of dependencies is created in which
  each node corresponds to one item and directed eges lead from nodes to
! those which must *follow* them on the link line.  For example, the
  graph
  
!   A -> B -> C
  
  will lead to the link line order
***************
*** 66,74 ****
  The explicitly known dependencies form graph edges
  
!   X <- Y  ,  X <- A  ,  X <- B  ,  Y <- A  ,  Y <- B
  
  We can also infer the edge
  
!   A <- B
  
  because *every* time A appears B is seen on its right.  We do not know
--- 66,74 ----
  The explicitly known dependencies form graph edges
  
!   X -> Y  ,  X -> A  ,  X -> B  ,  Y -> A  ,  Y -> B
  
  We can also infer the edge
  
!   A -> B
  
  because *every* time A appears B is seen on its right.  We do not know
***************
*** 94,103 ****
  The explicit edges are
  
!   X <- Y  ,  X <- A  ,  X <- B  ,  X <- C  ,  Y <- A  ,  Y <- B  ,  Y <- C
  
  For the unknown items, we infer dependencies by looking at the
  "follow" sets:
  
!   A: intersect( {B,Y,C} , {C,B} ) = {B,C} ; infer edges  A <- B  ,  A <- C
    B: intersect( {Y,C}   , {}    ) = {}    ; infer no edges
    C: intersect( {}      , {B}   ) = {}    ; infer no edges
--- 94,103 ----
  The explicit edges are
  
!   X -> Y  ,  X -> A  ,  X -> B  ,  X -> C  ,  Y -> A  ,  Y -> B  ,  Y -> C
  
  For the unknown items, we infer dependencies by looking at the
  "follow" sets:
  
!   A: intersect( {B,Y,C} , {C,B} ) = {B,C} ; infer edges  A -> B  ,  A -> C
    B: intersect( {Y,C}   , {}    ) = {}    ; infer no edges
    C: intersect( {}      , {B}   ) = {}    ; infer no edges
***************
*** 108,162 ****
  ------------------------------------------------------------------------------
  
! Once the complete graph is formed from all known and inferred
! dependencies we must use it to produce a valid link line.  If the
! dependency graph were known to be acyclic a simple depth-first-search
! would produce a correct link line.  Unfortunately we cannot make this
! assumption so the following technique is used.
  
  The original graph is converted to a directed acyclic graph in which
  each node corresponds to a strongly connected component of the
  original graph.  For example, the dependency graph
  
!   X <- A <- B <- C <- A <- Y
  
  contains strongly connected components {X}, {A,B,C}, and {Y}.  The
  implied directed acyclic graph (DAG) is
  
!   {X} <- {A,B,C} <- {Y}
! 
! The final list of link items is constructed by a series of
! depth-first-searches through this DAG of components.  When visiting a
! component all outgoing edges are followed first because the neighbors
! must precede it.  Once neighbors across all edges have been emitted it
! is safe to emit the current component.
! 
! Trivial components (those with one item) are handled simply by
! emitting the item.  Non-trivial components (those with more than one
! item) are assumed to consist only of static libraries that may be
! safely repeated on the link line.  We emit members of the component
! multiple times (see code below for details).  The final link line for
! the example graph might be
  
!   X A B C A B C Y
  
  ------------------------------------------------------------------------------
  
! The initial exploration of dependencies using a BFS associates an
! integer index with each link item.  When the graph is built outgoing
! edges are sorted by this index.
  
! This preserves the original link order as much as possible subject to
! the dependencies.  We then further preserve the original link line by
! appending items to make sure all those that might be static libraries
! appear in the order and multiplicity that they do in the original
! line.
  
! After the initial exploration of the link interface tree, any
! transitive (dependent) shared libraries that were encountered and not
! included in the interface are processed in their own BFS.  This BFS
! follows only the dependent library lists and not the link interfaces.
! They are added to the link items with a mark indicating that the are
! transitive dependencies.  Then cmComputeLinkInformation deals with
! them on a per-platform basis.
  
  */
--- 108,176 ----
  ------------------------------------------------------------------------------
  
! The initial exploration of dependencies using a BFS associates an
! integer index with each link item.  When the graph is built outgoing
! edges are sorted by this index.
! 
! After the initial exploration of the link interface tree, any
! transitive (dependent) shared libraries that were encountered and not
! included in the interface are processed in their own BFS.  This BFS
! follows only the dependent library lists and not the link interfaces.
! They are added to the link items with a mark indicating that the are
! transitive dependencies.  Then cmComputeLinkInformation deals with
! them on a per-platform basis.
  
+ The complete graph formed from all known and inferred dependencies may
+ not be acyclic, so an acyclic version must be created.
  The original graph is converted to a directed acyclic graph in which
  each node corresponds to a strongly connected component of the
  original graph.  For example, the dependency graph
  
!   X -> A -> B -> C -> A -> Y
  
  contains strongly connected components {X}, {A,B,C}, and {Y}.  The
  implied directed acyclic graph (DAG) is
  
!   {X} -> {A,B,C} -> {Y}
  
! We then compute a topological order for the DAG nodes to serve as a
! reference for satisfying dependencies efficiently.  We perform the DFS
! in reverse order and assign topological order indices counting down so
! that the result is as close to the original BFS order as possible
! without violating dependencies.
  
  ------------------------------------------------------------------------------
  
! The final link entry order is constructed as follows.  We first walk
! through and emit the *original* link line as specified by the user.
! As each item is emitted, a set of pending nodes in the component DAG
! is maintained.  When a pending component has been completely seen, it
! is removed from the pending set and its dependencies (following edges
! of the DAG) are added.  A trivial component (those with one item) is
! complete as soon as its item is seen.  A non-trivial component (one
! with more than one item; assumed to be static libraries) is complete
! when *all* its entries have been seen *twice* (all entries seen once,
! then all entries seen again, not just each entry twice).  A pending
! component tracks which items have been seen and a count of how many
! times the component needs to be seen (once for trivial components,
! twice for non-trivial).  If at any time another component finishes and
! re-adds an already pending component, the pending component is reset
! so that it needs to be seen in its entirety again.  This ensures that
! all dependencies of a component are satisified no matter where it
! appears.
  
! After the original link line has been completed, we append to it the
! remaining pending components and their dependencies.  This is done by
! repeatedly emitting the first item from the first pending component
! and following the same update rules as when traversing the original
! link line.  Since the pending components are kept in topological order
! they are emitted with minimal repeats (we do not want to emit a
! component just to have it added again when another component is
! completed later).  This process continues until no pending components
! remain.  We know it will terminate because the component graph is
! guaranteed to be acyclic.
  
! The final list of items produced by this procedure consists of the
! original user link line followed by minimal additional items needed to
! satisfy dependencies.
  
  */
***************
*** 181,184 ****
--- 195,201 ----
    // Assume no compatibility until set.
    this->OldLinkDirMode = false;
+ 
+   // No computation has been done.
+   this->CCG = 0;
  }
  
***************
*** 192,195 ****
--- 209,213 ----
      delete *i;
      }
+   delete this->CCG;
  }
  
***************
*** 245,249 ****
    // Compute the final ordering.
    this->OrderLinkEntires();
-   this->PreserveOriginalEntries();
  
    // Compute the final set of link entries.
--- 263,266 ----
***************
*** 403,409 ****
    LinkEntry& entry = this->EntryList[index];
  
!   // This shared library dependency must be preceded by the item that
!   // listed it.
!   this->EntryConstraintGraph[index].push_back(dep.DependerIndex);
  
    // Target items may have their own dependencies.
--- 420,426 ----
    LinkEntry& entry = this->EntryList[index];
  
!   // This shared library dependency must follow the item that listed
!   // it.
!   this->EntryConstraintGraph[dep.DependerIndex].push_back(index);
  
    // Target items may have their own dependencies.
***************
*** 556,565 ****
      int dependee_index = this->AddLinkEntry(item);
  
!     // The depender must come before the dependee.
      if(depender_index >= 0)
        {
        if(!this->EntryList[dependee_index].IsFlag)
          {
!         this->EntryConstraintGraph[dependee_index].push_back(depender_index);
          }
        }
--- 573,582 ----
      int dependee_index = this->AddLinkEntry(item);
  
!     // The dependee must come after the depender.
      if(depender_index >= 0)
        {
        if(!this->EntryList[dependee_index].IsFlag)
          {
!         this->EntryConstraintGraph[depender_index].push_back(dependee_index);
          }
        }
***************
*** 714,718 ****
        {
        int dependee_index = *j;
!       this->EntryConstraintGraph[dependee_index].push_back(depender_index);
        }
      }
--- 731,735 ----
        {
        int dependee_index = *j;
!       this->EntryConstraintGraph[depender_index].push_back(dependee_index);
        }
      }
***************
*** 746,750 ****
      for(NodeList::const_iterator j = nl.begin(); j != nl.end(); ++j)
        {
!       e << "  item " << *j << " must precede it\n";
        }
      }
--- 763,767 ----
      for(NodeList::const_iterator j = nl.begin(); j != nl.end(); ++j)
        {
!       e << "  item " << *j << " must follow it\n";
        }
      }
***************
*** 760,778 ****
    // the BFS.  This should preserve the original order when no
    // constraints disallow it.
!   cmComputeComponentGraph ccg(this->EntryConstraintGraph);
!   Graph const& cgraph = ccg.GetComponentGraph();
    if(this->DebugMode)
      {
!     this->DisplayComponents(ccg);
      }
  
!   // Setup visit tracking.
!   this->ComponentVisited.resize(cgraph.size(), 0);
  
!   // The component graph is guaranteed to be acyclic.  Start a DFS
!   // from every entry.
!   for(unsigned int c=0; c < cgraph.size(); ++c)
      {
!     this->VisitComponent(ccg, c);
      }
  }
--- 777,820 ----
    // the BFS.  This should preserve the original order when no
    // constraints disallow it.
!   this->CCG = new cmComputeComponentGraph(this->EntryConstraintGraph);
! 
!   // The component graph is guaranteed to be acyclic.  Start a DFS
!   // from every entry to compute a topological order for the
!   // components.
!   Graph const& cgraph = this->CCG->GetComponentGraph();
!   int n = static_cast<int>(cgraph.size());
!   this->ComponentVisited.resize(cgraph.size(), 0);
!   this->ComponentOrder.resize(cgraph.size(), n);
!   this->ComponentOrderId = n;
!   // Run in reverse order so the topological order will preserve the
!   // original order where there are no constraints.
!   for(int c = n-1; c >= 0; --c)
!     {
!     this->VisitComponent(c);
!     }
! 
!   // Display the component graph.
    if(this->DebugMode)
      {
!     this->DisplayComponents();
      }
  
!   // Start with the original link line.
!   for(std::vector<int>::const_iterator i = this->OriginalEntries.begin();
!       i != this->OriginalEntries.end(); ++i)
!     {
!     this->VisitEntry(*i);
!     }
  
!   // Now explore anything left pending.  Since the component graph is
!   // guaranteed to be acyclic we know this will terminate.
!   while(!this->PendingComponents.empty())
      {
!     // Visit one entry from the first pending component.  The visit
!     // logic will update the pending components accordingly.  Since
!     // the pending components are kept in topological order this will
!     // not repeat one.
!     int e = *this->PendingComponents.begin()->second.Entries.begin();
!     this->VisitEntry(e);
      }
  }
***************
*** 780,787 ****
  //----------------------------------------------------------------------------
  void
! cmComputeLinkDepends::DisplayComponents(cmComputeComponentGraph const& ccg)
  {
    fprintf(stderr, "The strongly connected components are:\n");
!   std::vector<NodeList> const& components = ccg.GetComponents();
    for(unsigned int c=0; c < components.size(); ++c)
      {
--- 822,829 ----
  //----------------------------------------------------------------------------
  void
! cmComputeLinkDepends::DisplayComponents()
  {
    fprintf(stderr, "The strongly connected components are:\n");
!   std::vector<NodeList> const& components = this->CCG->GetComponents();
    for(unsigned int c=0; c < components.size(); ++c)
      {
***************
*** 794,797 ****
--- 836,846 ----
                this->EntryList[i].Item.c_str());
        }
+     NodeList const& ol = this->CCG->GetComponentGraphEdges(c);
+     for(NodeList::const_iterator oi = ol.begin(); oi != ol.end(); ++oi)
+       {
+       fprintf(stderr, "  followed by Component (%d)\n", *oi);
+       }
+     fprintf(stderr, "  topo order index %d\n",
+             this->ComponentOrder[c]);
      }
    fprintf(stderr, "\n");
***************
*** 799,805 ****
  
  //----------------------------------------------------------------------------
! void
! cmComputeLinkDepends::VisitComponent(cmComputeComponentGraph const& ccg,
!                                      unsigned int c)
  {
    // Check if the node has already been visited.
--- 848,852 ----
  
  //----------------------------------------------------------------------------
! void cmComputeLinkDepends::VisitComponent(unsigned int c)
  {
    // Check if the node has already been visited.
***************
*** 813,859 ****
  
    // Visit the neighbors of the component first.
!   NodeList const& nl = ccg.GetComponentGraphEdges(c);
!   for(NodeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
      {
!     this->VisitComponent(ccg, *ni);
      }
  
!   // Now that all items required to come before this one have been
!   // emmitted, emit this component's items.
!   this->EmitComponent(ccg.GetComponent(c));
  }
  
  //----------------------------------------------------------------------------
! void cmComputeLinkDepends::EmitComponent(NodeList const& nl)
  {
!   assert(!nl.empty());
  
!   // Handle trivial components.
!   if(nl.size() == 1)
      {
!     this->FinalLinkOrder.push_back(nl[0]);
!     return;
      }
  
!   // This is a non-trivial strongly connected component of the
!   // original graph.  It consists of two or more libraries (archives)
!   // that mutually require objects from one another.  In the worst
!   // case we may have to repeat the list of libraries as many times as
!   // there are object files in the biggest archive.  For now we just
!   // list them twice.
!   //
!   // The list of items in the component has been sorted by the order
!   // of discovery in the original BFS of dependencies.  This has the
!   // advantage that the item directly linked by a target requiring
!   // this component will come first which minimizes the number of
!   // repeats needed.
!   for(NodeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
      {
!     this->FinalLinkOrder.push_back(*ni);
      }
!   for(NodeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
      {
!     this->FinalLinkOrder.push_back(*ni);
      }
  }
  
--- 860,983 ----
  
    // Visit the neighbors of the component first.
!   // Run in reverse order so the topological order will preserve the
!   // original order where there are no constraints.
!   NodeList const& nl = this->CCG->GetComponentGraphEdges(c);
!   for(NodeList::const_reverse_iterator ni = nl.rbegin();
!       ni != nl.rend(); ++ni)
      {
!     this->VisitComponent(*ni);
      }
  
!   // Assign an ordering id to this component.
!   this->ComponentOrder[c] = --this->ComponentOrderId;
  }
  
  //----------------------------------------------------------------------------
! void cmComputeLinkDepends::VisitEntry(int index)
  {
!   // Include this entry on the link line.
!   this->FinalLinkOrder.push_back(index);
  
!   // This entry has now been seen.  Update its component.
!   bool completed = false;
!   int component = this->CCG->GetComponentMap()[index];
!   std::map<int, PendingComponent>::iterator mi =
!     this->PendingComponents.find(this->ComponentOrder[component]);
!   if(mi != this->PendingComponents.end())
      {
!     // The entry is in an already pending component.
!     PendingComponent& pc = mi->second;
! 
!     // Remove the entry from those pending in its component.
!     pc.Entries.erase(index);
!     if(pc.Entries.empty())
!       {
!       // The complete component has been seen since it was last needed.
!       --pc.Count;
! 
!       if(pc.Count == 0)
!         {
!         // The component has been completed.
!         this->PendingComponents.erase(mi);
!         completed = true;
!         }
!       else
!         {
!         // The whole component needs to be seen again.
!         NodeList const& nl = this->CCG->GetComponent(component);
!         assert(nl.size() > 1);
!         pc.Entries.insert(nl.begin(), nl.end());
!         }
!       }
      }
+   else
+     {
+     // The entry is not in an already pending component.
+     NodeList const& nl = this->CCG->GetComponent(component);
+     if(nl.size() > 1)
+       {
+       // This is a non-trivial component.  It is now pending.
+       PendingComponent& pc = this->MakePendingComponent(component);
  
!       // The starting entry has already been seen.
!       pc.Entries.erase(index);
!       }
!     else
!       {
!       // This is a trivial component, so it is already complete.
!       completed = true;
!       }
!     }
! 
!   // If the entry completed a component, the component's dependencies
!   // are now pending.
!   if(completed)
      {
!     NodeList const& ol = this->CCG->GetComponentGraphEdges(component);
!     for(NodeList::const_iterator oi = ol.begin(); oi != ol.end(); ++oi)
!       {
!       // This entire component is now pending no matter whether it has
!       // been partially seen already.
!       this->MakePendingComponent(*oi);
!       }
      }
! }
! 
! //----------------------------------------------------------------------------
! cmComputeLinkDepends::PendingComponent&
! cmComputeLinkDepends::MakePendingComponent(unsigned int component)
! {
!   // Create an entry (in topological order) for the component.
!   PendingComponent& pc =
!     this->PendingComponents[this->ComponentOrder[component]];
!   pc.Id = component;
!   NodeList const& nl = this->CCG->GetComponent(component);
! 
!   if(nl.size() == 1)
      {
!     // Trivial components need be seen only once.
!     pc.Count = 1;
      }
+   else
+     {
+     // This is a non-trivial strongly connected component of the
+     // original graph.  It consists of two or more libraries
+     // (archives) that mutually require objects from one another.  In
+     // the worst case we may have to repeat the list of libraries as
+     // many times as there are object files in the biggest archive.
+     // For now we just list them twice.
+     //
+     // The list of items in the component has been sorted by the order
+     // of discovery in the original BFS of dependencies.  This has the
+     // advantage that the item directly linked by a target requiring
+     // this component will come first which minimizes the number of
+     // repeats needed.
+     pc.Count = 2;
+     }
+ 
+   // Store the entries to be seen.
+   pc.Entries.insert(nl.begin(), nl.end());
+ 
+   return pc;
  }
  
***************
*** 897,952 ****
      }
  }
- 
- //----------------------------------------------------------------------------
- static bool cmComputeLinkDependsNotStatic(cmTarget* tgt)
- {
-   return (tgt &&
-           tgt->GetType() != cmTarget::STATIC_LIBRARY &&
-           tgt->GetType() != cmTarget::UNKNOWN_LIBRARY);
- }
- 
- //----------------------------------------------------------------------------
- void cmComputeLinkDepends::PreserveOriginalEntries()
- {
-   // Skip the part of the input sequence that already appears in the
-   // output.
-   std::vector<int>::const_iterator in = this->OriginalEntries.begin();
-   std::vector<int>::const_iterator out = this->FinalLinkOrder.begin();
-   while(in != this->OriginalEntries.end() &&
-         out != this->FinalLinkOrder.end())
-     {
-     cmTarget* tgt = this->EntryList[*in].Target;
-     if(cmComputeLinkDependsNotStatic(tgt))
-       {
-       // Skip input items known to not be static libraries.
-       ++in;
-       }
-     else if(*in == *out)
-       {
-       // The input and output items match.  Move on to the next items.
-       ++in;
-       ++out;
-       }
-     else
-       {
-       // The output item does not match the next input item.  Skip it.
-       ++out;
-       }
-     }
- 
-   // Append the part of the input sequence that does not already
-   // appear in the output.
-   while(in != this->OriginalEntries.end())
-     {
-     cmTarget* tgt = this->EntryList[*in].Target;
-     if(cmComputeLinkDependsNotStatic(tgt))
-       {
-       // Skip input items known to not be static libraries.
-       ++in;
-       }
-     else
-       {
-       this->FinalLinkOrder.push_back(*in++);
-       }
-     }
- }
--- 1021,1022 ----



More information about the Cmake-commits mailing list