@@ -19,6 +19,7 @@ use std::ops::Deref;
1919use std:: path:: { Path , PathBuf } ;
2020use std:: process:: Command ;
2121use std:: time:: { Instant , Duration } ;
22+ use std:: collections:: HashMap ;
2223
2324use compile;
2425use install;
@@ -35,6 +36,9 @@ use native;
3536
3637pub use Compiler ;
3738
39+ use petgraph:: Graph ;
40+ use petgraph:: graph:: NodeIndex ;
41+
3842pub struct Builder < ' a > {
3943 pub build : & ' a Build ,
4044 pub top_stage : u32 ,
@@ -43,6 +47,9 @@ pub struct Builder<'a> {
4347 stack : RefCell < Vec < Box < Any > > > ,
4448 time_spent_on_dependencies : Cell < Duration > ,
4549 pub paths : Vec < PathBuf > ,
50+ graph_nodes : RefCell < HashMap < String , NodeIndex > > ,
51+ graph : RefCell < Graph < String , bool > > ,
52+ parent : Cell < Option < NodeIndex > > ,
4653}
4754
4855impl < ' a > Deref for Builder < ' a > {
@@ -353,6 +360,9 @@ impl<'a> Builder<'a> {
353360 stack : RefCell :: new ( Vec :: new ( ) ) ,
354361 time_spent_on_dependencies : Cell :: new ( Duration :: new ( 0 , 0 ) ) ,
355362 paths : vec ! [ ] ,
363+ graph_nodes : RefCell :: new ( HashMap :: new ( ) ) ,
364+ graph : RefCell :: new ( Graph :: new ( ) ) ,
365+ parent : Cell :: new ( None ) ,
356366 } ;
357367
358368 let builder = & builder;
@@ -389,6 +399,9 @@ impl<'a> Builder<'a> {
389399 stack : RefCell :: new ( Vec :: new ( ) ) ,
390400 time_spent_on_dependencies : Cell :: new ( Duration :: new ( 0 , 0 ) ) ,
391401 paths : paths. to_owned ( ) ,
402+ graph_nodes : RefCell :: new ( HashMap :: new ( ) ) ,
403+ graph : RefCell :: new ( Graph :: new ( ) ) ,
404+ parent : Cell :: new ( None ) ,
392405 } ;
393406
394407 if kind == Kind :: Dist {
@@ -833,12 +846,37 @@ impl<'a> Builder<'a> {
833846 if let Some ( out) = self . cache . get ( & step) {
834847 self . build . verbose ( & format ! ( "{}c {:?}" , " " . repeat( stack. len( ) ) , step) ) ;
835848
849+ {
850+ let mut graph = self . graph . borrow_mut ( ) ;
851+ let parent = self . parent . get ( ) ;
852+ let us = * self . graph_nodes . borrow_mut ( )
853+ . entry ( format ! ( "{:?}" , step) )
854+ . or_insert_with ( || graph. add_node ( format ! ( "{:?}" , step) ) ) ;
855+ if let Some ( parent) = parent {
856+ graph. add_edge ( parent, us, false ) ;
857+ }
858+ }
859+
836860 return out;
837861 }
838862 self . build . verbose ( & format ! ( "{}> {:?}" , " " . repeat( stack. len( ) ) , step) ) ;
839863 stack. push ( Box :: new ( step. clone ( ) ) ) ;
840864 }
841865
866+ let prev_parent = self . parent . get ( ) ;
867+
868+ {
869+ let mut graph = self . graph . borrow_mut ( ) ;
870+ let parent = self . parent . get ( ) ;
871+ let us = * self . graph_nodes . borrow_mut ( )
872+ . entry ( format ! ( "{:?}" , step) )
873+ . or_insert_with ( || graph. add_node ( format ! ( "{:?}" , step) ) ) ;
874+ self . parent . set ( Some ( us) ) ;
875+ if let Some ( parent) = parent {
876+ graph. add_edge ( parent, us, true ) ;
877+ }
878+ }
879+
842880 let ( out, dur) = {
843881 let start = Instant :: now ( ) ;
844882 let zero = Duration :: new ( 0 , 0 ) ;
@@ -849,6 +887,8 @@ impl<'a> Builder<'a> {
849887 ( out, dur - deps)
850888 } ;
851889
890+ self . parent . set ( prev_parent) ;
891+
852892 if self . build . config . print_step_timings && dur > Duration :: from_millis ( 100 ) {
853893 println ! ( "[TIMING] {:?} -- {}.{:03}" ,
854894 step,
0 commit comments