forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrustc.rs
707 lines (639 loc) · 25.2 KB
/
rustc.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
// -*- rust -*-
import metadata::{creader, cstore};
import syntax::parse::{parser};
import syntax::{ast, codemap};
import front::attr;
import middle::{trans, resolve, freevars, kind, ty, typeck, fn_usage,
last_use};
import syntax::print::{pp, pprust};
import util::{ppaux, filesearch};
import back::link;
import core::{option, str, vec, int, result};
import result::{ok, err};
import std::{fs, io, getopts};
import option::{some, none};
import getopts::{optopt, optmulti, optflag, optflagopt, opt_present};
import back::{x86, x86_64};
tag pp_mode { ppm_normal; ppm_expanded; ppm_typed; ppm_identified; }
fn default_configuration(sess: session::session, argv0: str, input: str) ->
ast::crate_cfg {
let libc =
alt sess.get_targ_cfg().os {
session::os_win32. { "msvcrt.dll" }
session::os_macos. { "libc.dylib" }
session::os_linux. { "libc.so.6" }
_ { "libc.so" }
};
let mk = attr::mk_name_value_item_str;
let arch = alt sess.get_targ_cfg().arch {
session::arch_x86. { "x86" }
session::arch_x86_64. { "x86_64" }
session::arch_arm. { "arm" }
};
ret [ // Target bindings.
mk("target_os", std::os::target_os()),
mk("target_arch", arch),
mk("target_libc", libc),
// Build bindings.
mk("build_compiler", argv0),
mk("build_input", input)];
}
fn build_configuration(sess: session::session, argv0: str, input: str) ->
ast::crate_cfg {
// Combine the configuration requested by the session (command line) with
// some default and generated configuration items
let default_cfg = default_configuration(sess, argv0, input);
let user_cfg = sess.get_opts().cfg;
// If the user wants a test runner, then add the test cfg
let gen_cfg =
{
if sess.get_opts().test && !attr::contains_name(user_cfg, "test")
{
[attr::mk_word_item("test")]
} else { [] }
};
ret user_cfg + gen_cfg + default_cfg;
}
// Convert strings provided as --cfg [cfgspec] into a crate_cfg
fn parse_cfgspecs(cfgspecs: [str]) -> ast::crate_cfg {
// FIXME: It would be nice to use the parser to parse all varieties of
// meta_item here. At the moment we just support the meta_word variant.
let words = [];
for s: str in cfgspecs { words += [attr::mk_word_item(s)]; }
ret words;
}
fn input_is_stdin(filename: str) -> bool { filename == "-" }
fn parse_input(sess: session::session, cfg: ast::crate_cfg, input: str) ->
@ast::crate {
if !input_is_stdin(input) {
parser::parse_crate_from_file(input, cfg, sess.get_parse_sess())
} else { parse_input_src(sess, cfg, input).crate }
}
fn parse_input_src(sess: session::session, cfg: ast::crate_cfg, infile: str)
-> {crate: @ast::crate, src: str} {
let srcbytes = if infile != "-" {
alt io::file_reader(infile) {
result::ok(reader) { reader }
result::err(e) {
sess.fatal(e)
}
}
} else {
io::stdin()
}.read_whole_stream();
let src = str::unsafe_from_bytes(srcbytes);
let crate =
parser::parse_crate_from_source_str(infile, src, cfg,
sess.get_parse_sess());
ret {crate: crate, src: src};
}
fn time<T>(do_it: bool, what: str, thunk: fn@() -> T) -> T {
if !do_it { ret thunk(); }
let start = std::time::precise_time_s();
let rv = thunk();
let end = std::time::precise_time_s();
log_err #fmt["time: %s took %s s", what,
float::to_str(end - start, 3u)];
ret rv;
}
fn inject_libcore_reference(sess: session::session,
crate: @ast::crate) -> @ast::crate {
fn spanned<copy T>(x: T) -> @ast::spanned<T> {
ret @{node: x,
span: {lo: 0u, hi: 0u,
expanded_from: codemap::os_none}};
}
let n1 = sess.next_node_id();
let n2 = sess.next_node_id();
let vi1 = spanned(ast::view_item_use("core", [], n1));
let vi2 = spanned(ast::view_item_import_glob(@["core"], n2));
let vis = [vi1, vi2] + crate.node.module.view_items;
ret @{node: {module: { view_items: vis with crate.node.module }
with crate.node} with *crate }
}
fn compile_input(sess: session::session, cfg: ast::crate_cfg, input: str,
outdir: option::t<str>, output: option::t<str>) {
let time_passes = sess.get_opts().time_passes;
let crate =
time(time_passes, "parsing", bind parse_input(sess, cfg, input));
if sess.get_opts().parse_only { ret; }
sess.set_building_library(crate);
crate =
time(time_passes, "configuration",
bind front::config::strip_unconfigured_items(crate));
if sess.get_opts().test {
crate =
time(time_passes, "building test harness",
bind front::test::modify_for_testing(sess, crate));
}
crate =
time(time_passes, "expansion",
bind syntax::ext::expand::expand_crate(sess, crate));
if sess.get_opts().libcore {
crate = inject_libcore_reference(sess, crate);
}
let ast_map =
time(time_passes, "ast indexing",
bind middle::ast_map::map_crate(*crate));
time(time_passes, "external crate/lib resolution",
bind creader::read_crates(sess, *crate));
let {def_map: def_map, ext_map: ext_map} =
time(time_passes, "resolution",
bind resolve::resolve_crate(sess, ast_map, crate));
let freevars =
time(time_passes, "freevar finding",
bind freevars::annotate_freevars(def_map, crate));
time(time_passes, "const checking",
bind middle::check_const::check_crate(sess, crate));
let ty_cx = ty::mk_ctxt(sess, def_map, ext_map, ast_map, freevars);
time(time_passes, "typechecking", bind typeck::check_crate(ty_cx, crate));
time(time_passes, "block-use checking",
bind middle::block_use::check_crate(ty_cx, crate));
time(time_passes, "function usage",
bind fn_usage::check_crate_fn_usage(ty_cx, crate));
time(time_passes, "alt checking",
bind middle::check_alt::check_crate(ty_cx, crate));
time(time_passes, "typestate checking",
bind middle::tstate::ck::check_crate(ty_cx, crate));
let mut_map =
time(time_passes, "mutability checking",
bind middle::mut::check_crate(ty_cx, crate));
let (copy_map, ref_map) =
time(time_passes, "alias checking",
bind middle::alias::check_crate(ty_cx, crate));
let last_uses = time(time_passes, "last use finding",
bind last_use::find_last_uses(crate, def_map, ref_map, ty_cx));
time(time_passes, "kind checking",
bind kind::check_crate(ty_cx, last_uses, crate));
if sess.get_opts().no_trans { ret; }
let outputs = build_output_filenames(input, outdir, output, sess);
let (llmod, link_meta) =
time(time_passes, "translation",
bind trans::trans_crate(sess, crate, ty_cx,
outputs.obj_filename, ast_map,
mut_map, copy_map, last_uses));
time(time_passes, "LLVM passes",
bind link::write::run_passes(sess, llmod, outputs.obj_filename));
let stop_after_codegen =
sess.get_opts().output_type != link::output_type_exe ||
sess.get_opts().static && sess.building_library();
if stop_after_codegen { ret; }
time(time_passes, "Linking",
bind link::link_binary(sess, outputs.obj_filename,
outputs.out_filename, link_meta));
}
fn pretty_print_input(sess: session::session, cfg: ast::crate_cfg, input: str,
ppm: pp_mode) {
fn ann_paren_for_expr(node: pprust::ann_node) {
alt node { pprust::node_expr(s, expr) { pprust::popen(s); } _ { } }
}
fn ann_typed_post(tcx: ty::ctxt, node: pprust::ann_node) {
alt node {
pprust::node_expr(s, expr) {
pp::space(s.s);
pp::word(s.s, "as");
pp::space(s.s);
pp::word(s.s, ppaux::ty_to_str(tcx, ty::expr_ty(tcx, expr)));
pprust::pclose(s);
}
_ { }
}
}
fn ann_identified_post(node: pprust::ann_node) {
alt node {
pprust::node_item(s, item) {
pp::space(s.s);
pprust::synth_comment(s, int::to_str(item.id, 10u));
}
pprust::node_block(s, blk) {
pp::space(s.s);
pprust::synth_comment(s,
"block " + int::to_str(blk.node.id, 10u));
}
pprust::node_expr(s, expr) {
pp::space(s.s);
pprust::synth_comment(s, int::to_str(expr.id, 10u));
pprust::pclose(s);
}
_ { }
}
}
// Because the pretty printer needs to make a pass over the source
// to collect comments and literals, and we need to support reading
// from stdin, we're going to just suck the source into a string
// so both the parser and pretty-printer can use it.
let crate_src = parse_input_src(sess, cfg, input);
let crate = crate_src.crate;
let src = crate_src.src;
let ann;
alt ppm {
ppm_expanded. {
crate = syntax::ext::expand::expand_crate(sess, crate);
ann = pprust::no_ann();
}
ppm_typed. {
crate = syntax::ext::expand::expand_crate(sess, crate);
let amap = middle::ast_map::map_crate(*crate);
let {def_map: def_map, ext_map: ext_map} =
resolve::resolve_crate(sess, amap, crate);
let freevars = freevars::annotate_freevars(def_map, crate);
let ty_cx = ty::mk_ctxt(sess, def_map, ext_map, amap, freevars);
typeck::check_crate(ty_cx, crate);
ann = {pre: ann_paren_for_expr, post: bind ann_typed_post(ty_cx, _)};
}
ppm_identified. {
ann = {pre: ann_paren_for_expr, post: ann_identified_post};
}
ppm_normal. { ann = pprust::no_ann(); }
}
pprust::print_crate(sess.get_codemap(), crate, input,
io::string_reader(src), io::stdout(), ann);
}
fn version(argv0: str) {
let vers = "unknown version";
let env_vers = #env["CFG_VERSION"];
if str::byte_len(env_vers) != 0u { vers = env_vers; }
io::stdout().write_str(#fmt["%s %s\n", argv0, vers]);
io::stdout().write_str(#fmt["host: %s\n", host_triple()]);
}
fn usage(argv0: str) {
io::stdout().write_str(#fmt["usage: %s [options] <input>\n", argv0] +
"
options:
-h --help display this message
-v --version print version info and exit
-o <filename> write output to <filename>
--out-dir <dir> write output to compiler-chosen filename in <dir>
--lib compile a library crate
--bin compile an executable crate (default)
--static use or produce static libraries
--no-core omit the 'core' library (used and imported by default)
--pretty [type] pretty-print the input instead of compiling
--ls list the symbols defined by a crate file
-L <path> add a directory to the library search path
--no-verify suppress LLVM verification step (slight speedup)
--parse-only parse only; do not compile, assemble, or link
--no-trans run all passes except translation; no output
-g produce debug info
--opt-level <lvl> optimize with possible levels 0-3
-O equivalent to --opt-level=2
-S compile only; do not assemble or link
--no-asm-comments do not add comments into the assembly source
-c compile and assemble, but do not link
--emit-llvm produce an LLVM bitcode file
--save-temps write intermediate files in addition to normal output
--stats gather and report various compilation statistics
--cfg <cfgspec> configure the compilation environment
--time-passes time the individual phases of the compiler
--time-llvm-passes time the individual phases of the LLVM backend
--sysroot <path> override the system root
--target <triple> target to compile for (default: host triple)
--test build test harness
--gc garbage collect shared data (experimental/temporary)
--stack-growth perform stack checks (experimental)
--warn-unused-imports
warn about unnecessary imports
");
}
fn get_os(triple: str) -> session::os {
ret if str::find(triple, "win32") >= 0 ||
str::find(triple, "mingw32") >= 0 {
session::os_win32
} else if str::find(triple, "darwin") >= 0 {
session::os_macos
} else if str::find(triple, "linux") >= 0 {
session::os_linux
} else { early_error("Unknown operating system!") };
}
fn get_arch(triple: str) -> session::arch {
ret if str::find(triple, "i386") >= 0 || str::find(triple, "i486") >= 0 ||
str::find(triple, "i586") >= 0 ||
str::find(triple, "i686") >= 0 ||
str::find(triple, "i786") >= 0 {
session::arch_x86
} else if str::find(triple, "x86_64") >= 0 {
session::arch_x86_64
} else if str::find(triple, "arm") >= 0 ||
str::find(triple, "xscale") >= 0 {
session::arch_arm
} else { early_error("Unknown architecture! " + triple) };
}
fn build_target_config(sopts: @session::options) -> @session::config {
let os = get_os(sopts.target_triple);
let arch = get_arch(sopts.target_triple);
let (int_type, uint_type, float_type) = alt arch {
session::arch_x86. {(ast::ty_i32, ast::ty_u32, ast::ty_f64)}
session::arch_x86_64. {(ast::ty_i64, ast::ty_u64, ast::ty_f64)}
session::arch_arm. {(ast::ty_i32, ast::ty_u32, ast::ty_f64)}
};
let target_strs = alt arch {
session::arch_x86. {x86::get_target_strs(os)}
session::arch_x86_64. {x86_64::get_target_strs(os)}
session::arch_arm. {x86::get_target_strs(os)}
};
let target_cfg: @session::config =
@{os: os, arch: arch, target_strs: target_strs, int_type: int_type,
uint_type: uint_type, float_type: float_type};
ret target_cfg;
}
fn host_triple() -> str {
// Get the host triple out of the build environment. This ensures that our
// idea of the host triple is the same as for the set of libraries we've
// actually built. We can't just take LLVM's host triple because they
// normalize all ix86 architectures to i386.
// FIXME: Instead of grabbing the host triple we really should be
// grabbing (at compile time) the target triple that this rustc is
// built with and calling that (at runtime) the host triple.
let ht = #env("CFG_HOST_TRIPLE");
ret ht != "" ? ht : fail "rustc built without CFG_HOST_TRIPLE";
}
fn build_session_options(match: getopts::match)
-> @session::options {
let crate_type = if opt_present(match, "lib") {
session::lib_crate
} else if opt_present(match, "bin") {
session::bin_crate
} else {
session::unknown_crate
};
let static = opt_present(match, "static");
let parse_only = opt_present(match, "parse-only");
let no_trans = opt_present(match, "no-trans");
let output_type =
if parse_only || no_trans {
link::output_type_none
} else if opt_present(match, "S") && opt_present(match, "emit-llvm") {
link::output_type_llvm_assembly
} else if opt_present(match, "S") {
link::output_type_assembly
} else if opt_present(match, "c") {
link::output_type_object
} else if opt_present(match, "emit-llvm") {
link::output_type_bitcode
} else { link::output_type_exe };
let libcore = !opt_present(match, "no-core");
let verify = !opt_present(match, "no-verify");
let save_temps = opt_present(match, "save-temps");
let debuginfo = opt_present(match, "g");
let stats = opt_present(match, "stats");
let time_passes = opt_present(match, "time-passes");
let time_llvm_passes = opt_present(match, "time-llvm-passes");
let sysroot_opt = getopts::opt_maybe_str(match, "sysroot");
let target_opt = getopts::opt_maybe_str(match, "target");
let no_asm_comments = getopts::opt_present(match, "no-asm-comments");
alt output_type {
// unless we're emitting huamn-readable assembly, omit comments.
link::output_type_llvm_assembly. | link::output_type_assembly. {}
_ { no_asm_comments = true; }
}
let opt_level: uint =
if opt_present(match, "O") {
if opt_present(match, "opt-level") {
early_error("-O and --opt-level both provided");
}
2u
} else if opt_present(match, "opt-level") {
alt getopts::opt_str(match, "opt-level") {
"0" { 0u }
"1" { 1u }
"2" { 2u }
"3" { 3u }
_ {
early_error("optimization level needs " +
"to be between 0-3")
}
}
} else { 0u };
let target =
alt target_opt {
none. { host_triple() }
some(s) { s }
};
let addl_lib_search_paths = getopts::opt_strs(match, "L");
let cfg = parse_cfgspecs(getopts::opt_strs(match, "cfg"));
let test = opt_present(match, "test");
let do_gc = opt_present(match, "gc");
let stack_growth = opt_present(match, "stack-growth");
let warn_unused_imports = opt_present(match, "warn-unused-imports");
let sopts: @session::options =
@{crate_type: crate_type,
static: static,
libcore: libcore,
optimize: opt_level,
debuginfo: debuginfo,
verify: verify,
save_temps: save_temps,
stats: stats,
time_passes: time_passes,
time_llvm_passes: time_llvm_passes,
output_type: output_type,
addl_lib_search_paths: addl_lib_search_paths,
maybe_sysroot: sysroot_opt,
target_triple: target,
cfg: cfg,
test: test,
parse_only: parse_only,
no_trans: no_trans,
do_gc: do_gc,
stack_growth: stack_growth,
no_asm_comments: no_asm_comments,
warn_unused_imports: warn_unused_imports};
ret sopts;
}
fn build_session(sopts: @session::options) -> session::session {
let target_cfg = build_target_config(sopts);
let cstore = cstore::mk_cstore();
let filesearch = filesearch::mk_filesearch(
sopts.maybe_sysroot,
sopts.target_triple,
sopts.addl_lib_search_paths);
ret session::session(target_cfg, sopts, cstore,
@{cm: codemap::new_codemap(), mutable next_id: 0},
none, 0u, filesearch, false);
}
fn parse_pretty(sess: session::session, &&name: str) -> pp_mode {
if str::eq(name, "normal") {
ret ppm_normal;
} else if str::eq(name, "expanded") {
ret ppm_expanded;
} else if str::eq(name, "typed") {
ret ppm_typed;
} else if str::eq(name, "identified") { ret ppm_identified; }
sess.fatal("argument to `pretty` must be one of `normal`, `typed`, or " +
"`identified`");
}
fn opts() -> [getopts::opt] {
ret [optflag("h"), optflag("help"), optflag("v"), optflag("version"),
optflag("emit-llvm"), optflagopt("pretty"),
optflag("ls"), optflag("parse-only"), optflag("no-trans"),
optflag("O"), optopt("opt-level"), optmulti("L"), optflag("S"),
optopt("o"), optopt("out-dir"),
optflag("c"), optflag("g"), optflag("save-temps"),
optopt("sysroot"), optopt("target"), optflag("stats"),
optflag("time-passes"), optflag("time-llvm-passes"),
optflag("no-verify"),
optmulti("cfg"), optflag("test"),
optflag("no-core"),
optflag("lib"), optflag("bin"), optflag("static"), optflag("gc"),
optflag("stack-growth"),
optflag("no-asm-comments"),
optflag("warn-unused-imports")];
}
fn build_output_filenames(ifile: str,
odir: option::t<str>,
ofile: option::t<str>,
sess: session::session)
-> @{out_filename: str, obj_filename:str} {
let obj_path = "";
let out_path: str = "";
let sopts = sess.get_opts();
let stop_after_codegen =
sopts.output_type != link::output_type_exe ||
sopts.static && sess.building_library();
let obj_suffix =
alt sopts.output_type {
link::output_type_none. { "none" }
link::output_type_bitcode. { "bc" }
link::output_type_assembly. { "s" }
link::output_type_llvm_assembly. { "ll" }
// Object and exe output both use the '.o' extension here
link::output_type_object. | link::output_type_exe. {
"o"
}
};
alt ofile {
none. {
// "-" as input file will cause the parser to read from stdin so we
// have to make up a name
// We want to toss everything after the final '.'
let dirname = alt odir {
some(d) { d }
none. {
if input_is_stdin(ifile) {
std::os::getcwd()
} else {
fs::dirname(ifile)
}
}
};
let (base_path, _) = if !input_is_stdin(ifile) {
fs::splitext(ifile)
} else {
(fs::connect(dirname, "rust_out"), "")
};
if sess.building_library() {
let basename = fs::basename(base_path);
let dylibname = std::os::dylib_filename(basename);
out_path = fs::connect(dirname, dylibname);
obj_path = fs::connect(dirname, basename + "." + obj_path);
} else {
out_path = base_path;
obj_path = base_path + "." + obj_suffix;
}
}
some(out_file) {
out_path = out_file;
obj_path = if stop_after_codegen {
out_file
} else {
let (base, _) = fs::splitext(out_file);
let modified = base + "." + obj_suffix;
modified
};
if sess.building_library() {
// FIXME: We might want to warn here; we're actually not going to
// respect the user's choice of library name when it comes time to
// link, we'll be linking to lib<basename>-<hash>-<version>.so no
// matter what.
}
if odir != none {
sess.warn("Ignoring --out-dir flag due to -o flag.");
}
}
}
ret @{out_filename: out_path,
obj_filename: obj_path};
}
fn early_error(msg: str) -> ! {
codemap::print_diagnostic("", codemap::error, msg);
fail;
}
fn main(args: [str]) {
let args = args, binary = vec::shift(args);
let match =
alt getopts::getopts(args, opts()) {
ok(m) { m }
err(f) {
early_error(getopts::fail_str(f))
}
};
if opt_present(match, "h") || opt_present(match, "help") {
usage(binary);
ret;
}
if opt_present(match, "v") || opt_present(match, "version") {
version(binary);
ret;
}
let ifile = alt vec::len(match.free) {
0u { early_error("No input filename given.") }
1u { match.free[0] }
_ { early_error("Multiple input filenames provided.") }
};
let sopts = build_session_options(match);
let sess = build_session(sopts);
let odir = getopts::opt_maybe_str(match, "out-dir");
let ofile = getopts::opt_maybe_str(match, "o");
let cfg = build_configuration(sess, binary, ifile);
let pretty =
option::map::<str,
pp_mode>(bind parse_pretty(sess, _),
getopts::opt_default(match, "pretty",
"normal"));
alt pretty {
some::<pp_mode>(ppm) { pretty_print_input(sess, cfg, ifile, ppm); ret; }
none::<pp_mode>. {/* continue */ }
}
let ls = opt_present(match, "ls");
if ls {
metadata::creader::list_file_metadata(sess, ifile, io::stdout());
ret;
}
compile_input(sess, cfg, ifile, odir, ofile);
}
#[cfg(test)]
mod test {
// When the user supplies --test we should implicitly supply --cfg test
#[test]
fn test_switch_implies_cfg_test() {
let match =
alt getopts::getopts(["--test"], opts()) {
ok(m) { m }
};
let sessopts = build_session_options(match);
let sess = build_session(sessopts);
let cfg = build_configuration(sess, "whatever", "whatever");
assert (attr::contains_name(cfg, "test"));
}
// When the user supplies --test and --cfg test, don't implicitly add
// another --cfg test
#[test]
fn test_switch_implies_cfg_test_unless_cfg_test() {
let match =
alt getopts::getopts(["--test", "--cfg=test"], opts()) {
ok(m) { m }
};
let sessopts = build_session_options(match);
let sess = build_session(sessopts);
let cfg = build_configuration(sess, "whatever", "whatever");
let test_items = attr::find_meta_items_by_name(cfg, "test");
assert (vec::len(test_items) == 1u);
}
}
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End: