@@ -446,13 +446,49 @@ pub fn compile_input(sess: Session, cfg: ast::CrateConfig, input: &input,
446446 let ( outputs, trans) = {
447447 let expanded_crate = {
448448 let crate = phase_1_parse_input ( sess, cfg. clone ( ) , input) ;
449+ let ( crate_id, crate_name, crate_file_name) = sess. opts . print_metas ;
450+ // these nasty nested conditions are to avoid doing extra work
451+ if crate_id || crate_name || crate_file_name {
452+ let t_outputs = build_output_filenames ( input, outdir, output, crate . attrs, sess) ;
453+ if crate_id || crate_name {
454+ let pkgid = match attr:: find_pkgid ( crate . attrs) {
455+ Some ( pkgid) => pkgid,
456+ None => fail ! ( "No crate_id and --crate-id or --crate-name requested" )
457+ } ;
458+ if crate_id {
459+ println ( pkgid. to_str ( ) ) ;
460+ }
461+ if crate_name {
462+ println ( pkgid. name ) ;
463+ }
464+ }
465+
466+ if crate_file_name {
467+ let lm = link:: build_link_meta ( sess, & crate , & t_outputs. obj_filename ,
468+ & mut :: util:: sha2:: Sha256 :: new ( ) ) ;
469+ // if the vector is empty we default to OutputExecutable.
470+ let style = sess. opts . outputs . get_opt ( 0 ) . unwrap_or ( & OutputExecutable ) ;
471+ let fname = link:: filename_for_input ( & sess, * style, & lm,
472+ & t_outputs. out_filename ) ;
473+ println ! ( "{}" , fname. display( ) ) ;
474+
475+ // we already maybe printed the first one, so skip it
476+ for style in sess. opts . outputs . iter ( ) . skip ( 1 ) {
477+ let fname = link:: filename_for_input ( & sess, * style, & lm,
478+ & t_outputs. out_filename ) ;
479+ println ! ( "{}" , fname. display( ) ) ;
480+ }
481+ }
482+
483+ return ;
484+ }
449485 if stop_after_phase_1 ( sess) { return ; }
450486 phase_2_configure_and_expand ( sess, cfg, crate )
451487 } ;
452- let analysis = phase_3_run_analysis_passes ( sess, & expanded_crate) ;
453- if stop_after_phase_3 ( sess) { return ; }
454488 let outputs = build_output_filenames ( input, outdir, output,
455489 expanded_crate. attrs , sess) ;
490+ let analysis = phase_3_run_analysis_passes ( sess, & expanded_crate) ;
491+ if stop_after_phase_3 ( sess) { return ; }
456492 let trans = phase_4_translate_to_llvm ( sess, expanded_crate,
457493 & analysis, outputs) ;
458494 ( outputs, trans)
@@ -789,6 +825,9 @@ pub fn build_session_options(binary: @str,
789825 } ) . collect ( )
790826 }
791827 } ;
828+ let print_metas = ( matches. opt_present ( "crate-id" ) ,
829+ matches. opt_present ( "crate-name" ) ,
830+ matches. opt_present ( "crate-file-name" ) ) ;
792831
793832 let sopts = @session:: options {
794833 outputs : outputs,
@@ -817,6 +856,7 @@ pub fn build_session_options(binary: @str,
817856 debugging_opts : debugging_opts,
818857 android_cross_path : android_cross_path,
819858 write_dependency_info : write_dependency_info,
859+ print_metas : print_metas,
820860 } ;
821861 return sopts;
822862}
@@ -897,6 +937,10 @@ pub fn optgroups() -> ~[getopts::groups::OptGroup] {
897937 optflag ( "" , "dylib" , "Compile a dynamic library crate" ) ,
898938 optopt ( "" , "linker" , "Program to use for linking instead of the default." , "LINKER" ) ,
899939 optopt ( "" , "ar" , "Program to use for managing archives instead of the default." , "AR" ) ,
940+ optflag ( "" , "crate-id" , "Output the crate id and exit" ) ,
941+ optflag ( "" , "crate-name" , "Output the crate name and exit" ) ,
942+ optflag ( "" , "crate-file-name" , "Output the file(s) that would be written if compilation \
943+ continued and exit") ,
900944 optmulti ( "" , "link-args" , "FLAGS is a space-separated list of flags
901945 passed to the linker" , "FLAGS" ) ,
902946 optflag ( "" , "ls" , "List the symbols defined by a library crate" ) ,
0 commit comments