Skip to content

Commit 03f93aa

Browse files
committedOct 28, 2011
rustc: Lift output file name handling out of main
1 parent 6b9d4b6 commit 03f93aa

File tree

1 file changed

+53
-40
lines changed

1 file changed

+53
-40
lines changed
 

‎src/comp/driver/rustc.rs

+53-40
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,53 @@ fn opts() -> [getopts::opt] {
438438
optflag("stack-growth"), optflag("check-unsafe")];
439439
}
440440

441+
fn build_output_filenames(ifile: str, ofile: option::t<str>,
442+
sopts: @session::options)
443+
-> @{out_filename: str, obj_filename:str} {
444+
let obj_filename = "";
445+
let saved_out_filename: str = "";
446+
let stop_after_codegen =
447+
sopts.output_type != link::output_type_exe ||
448+
sopts.static && sopts.library;
449+
alt ofile {
450+
none. {
451+
// "-" as input file will cause the parser to read from stdin so we
452+
// have to make up a name
453+
// We want to toss everything after the final '.'
454+
let parts =
455+
if !input_is_stdin(ifile) {
456+
str::split(ifile, '.' as u8)
457+
} else { ["default", "rs"] };
458+
vec::pop(parts);
459+
let base_filename = str::connect(parts, ".");
460+
let suffix =
461+
alt sopts.output_type {
462+
link::output_type_none. { "none" }
463+
link::output_type_bitcode. { "bc" }
464+
link::output_type_assembly. { "s" }
465+
// Object and exe output both use the '.o' extension here
466+
link::output_type_object. | link::output_type_exe. {
467+
"o"
468+
}
469+
};
470+
obj_filename = base_filename + "." + suffix;
471+
472+
if sopts.library {
473+
saved_out_filename = std::os::dylib_filename(base_filename);
474+
} else {
475+
saved_out_filename = base_filename;
476+
}
477+
}
478+
some(out_file) {
479+
// FIXME: what about windows? This will create a foo.exe.o.
480+
saved_out_filename = out_file;
481+
obj_filename =
482+
if stop_after_codegen { out_file } else { out_file + ".o" };
483+
}
484+
}
485+
ret @{out_filename: saved_out_filename, obj_filename: obj_filename};
486+
}
487+
441488
fn main(args: [str]) {
442489
let binary = vec::shift(args);
443490
let match =
@@ -459,14 +506,14 @@ fn main(args: [str]) {
459506
let sopts = build_session_options(match);
460507
let sess = build_session(sopts);
461508
let n_inputs = vec::len::<str>(match.free);
462-
let output_file = getopts::opt_maybe_str(match, "o");
509+
let ofile = getopts::opt_maybe_str(match, "o");
510+
let ifile = match.free[0];
511+
let outputs = build_output_filenames(ifile, ofile, sopts);
463512
if n_inputs == 0u {
464513
sess.fatal("No input filename given.");
465514
} else if n_inputs > 1u {
466515
sess.fatal("Multiple input filenames provided.");
467516
}
468-
let ifile = match.free[0];
469-
let saved_out_filename: str = "";
470517
let cfg = build_configuration(sess, binary, ifile);
471518
let pretty =
472519
option::map::<str,
@@ -484,47 +531,13 @@ fn main(args: [str]) {
484531
sopts.output_type != link::output_type_exe ||
485532
sopts.static && sopts.library;
486533

487-
let ofile = "";
488-
alt output_file {
489-
none. {
490-
// "-" as input file will cause the parser to read from stdin so we
491-
// have to make up a name
492-
// We want to toss everything after the final '.'
493-
let parts =
494-
if !input_is_stdin(ifile) {
495-
str::split(ifile, '.' as u8)
496-
} else { ["default", "rs"] };
497-
vec::pop(parts);
498-
let base_filename = str::connect(parts, ".");
499-
let suffix =
500-
alt sopts.output_type {
501-
link::output_type_none. { "none" }
502-
link::output_type_bitcode. { "bc" }
503-
link::output_type_assembly. { "s" }
504-
// Object and exe output both use the '.o' extension here
505-
link::output_type_object. | link::output_type_exe. {
506-
"o"
507-
}
508-
};
509-
ofile = base_filename + "." + suffix;
534+
let temp_filename = outputs.obj_filename;
510535

511-
if sopts.library {
512-
saved_out_filename = std::os::dylib_filename(base_filename);
513-
} else {
514-
saved_out_filename = base_filename;
515-
}
516-
}
517-
some(out_file) {
518-
saved_out_filename = out_file;
519-
ofile =
520-
if !stop_after_codegen { out_file + ".o" } else { out_file };
521-
}
522-
}
536+
compile_input(sess, cfg, ifile, temp_filename);
523537

524-
compile_input(sess, cfg, ifile, ofile);
525538
if stop_after_codegen { ret; }
526539

527-
link::link_binary(sess, ofile, saved_out_filename);
540+
link::link_binary(sess, temp_filename, outputs.out_filename);
528541
}
529542

530543
#[cfg(test)]

0 commit comments

Comments
 (0)