@@ -384,13 +384,34 @@ pub fn phase_5_run_llvm_passes(sess: Session,
384384/// This should produce either a finished executable or library.
385385pub fn phase_6_link_output ( sess : Session ,
386386 trans : & CrateTranslation ,
387+ input : & input ,
387388 outputs : & OutputFilenames ) {
388- time ( sess. time_passes ( ) , "linking" , ( ) , |_|
389+ let outputs = time ( sess. time_passes ( ) , "linking" , ( ) , |_|
389390 link:: link_binary ( sess,
390391 trans,
391392 & outputs. obj_filename ,
392393 & outputs. out_filename ,
393394 & trans. link ) ) ;
395+
396+ // Write out dependency rules to the .d file if requested
397+ if sess. opts . write_dependency_info {
398+ match * input {
399+ file_input( ref input_path) => {
400+ let files: ~[ @str ] = sess. codemap . files . iter ( )
401+ . filter_map ( |fmap| if fmap. is_real_file ( ) { Some ( fmap. name ) } else { None } )
402+ . collect ( ) ;
403+ let mut output_path = outputs[ 0 ] . dir_path ( ) ;
404+ let filestem = input_path. filestem ( ) . expect ( "input file must have stem" ) ;
405+ output_path. push ( Path :: new ( filestem) . with_extension ( "d" ) ) ;
406+ let mut file = io:: File :: create ( & output_path) ;
407+ for output in outputs. iter ( ) {
408+ write ! ( & mut file as & mut Writer ,
409+ "{}: {}\n \n " , output. display( ) , files. connect( " " ) ) ;
410+ }
411+ }
412+ str_input( _) => { }
413+ }
414+ }
394415}
395416
396417pub fn stop_after_phase_3 ( sess : Session ) -> bool {
@@ -438,7 +459,7 @@ pub fn compile_input(sess: Session, cfg: ast::CrateConfig, input: &input,
438459 } ;
439460 phase_5_run_llvm_passes ( sess, & trans, outputs) ;
440461 if stop_after_phase_5 ( sess) { return ; }
441- phase_6_link_output ( sess, & trans, outputs) ;
462+ phase_6_link_output ( sess, & trans, input , outputs) ;
442463}
443464
444465struct IdentifiedAnnotation {
@@ -750,6 +771,7 @@ pub fn build_session_options(binary: @str,
750771 let cfg = parse_cfgspecs ( matches. opt_strs ( "cfg" ) , demitter) ;
751772 let test = matches. opt_present ( "test" ) ;
752773 let android_cross_path = matches. opt_str ( "android-cross-path" ) ;
774+ let write_dependency_info = matches. opt_present ( "dep-info" ) ;
753775
754776 let custom_passes = match matches. opt_str ( "passes" ) {
755777 None => ~[ ] ,
@@ -793,7 +815,8 @@ pub fn build_session_options(binary: @str,
793815 parse_only : parse_only,
794816 no_trans : no_trans,
795817 debugging_opts : debugging_opts,
796- android_cross_path : android_cross_path
818+ android_cross_path : android_cross_path,
819+ write_dependency_info : write_dependency_info,
797820 } ;
798821 return sopts;
799822}
@@ -902,6 +925,8 @@ pub fn optgroups() -> ~[getopts::groups::OptGroup] {
902925 or identified (fully parenthesized,
903926 AST nodes and blocks with IDs)" , "TYPE" ) ,
904927 optflag ( "S" , "" , "Compile only; do not assemble or link" ) ,
928+ optflag ( "" , "dep-info" ,
929+ "Output dependency info to .d file after compiling" ) ,
905930 optflag ( "" , "save-temps" ,
906931 "Write intermediate files (.bc, .opt.bc, .o)
907932 in addition to normal output" ) ,
0 commit comments