@@ -6,6 +6,7 @@ import front::parser;
6
6
import front:: token;
7
7
import front:: eval;
8
8
import front:: ast;
9
+ import front:: attr;
9
10
import middle:: trans;
10
11
import middle:: resolve;
11
12
import middle:: ty;
@@ -35,22 +36,42 @@ import back::link::output_type;
35
36
36
37
tag pp_mode { ppm_normal; ppm_typed; ppm_identified; }
37
38
38
- fn default_environment ( session:: session sess, str argv0 , str input ) ->
39
- eval :: env {
39
+ fn default_configuration ( session:: session sess, str argv0 , str input ) ->
40
+ ast :: crate_cfg {
40
41
auto libc =
41
42
alt ( sess. get_targ_cfg ( ) . os ) {
42
43
case ( session:: os_win32) { "msvcrt.dll" }
43
44
case ( session:: os_macos) { "libc.dylib" }
44
45
case ( session:: os_linux) { "libc.so.6" }
45
46
case ( _) { "libc.so" }
46
47
} ;
48
+
49
+ auto mk = attr:: mk_name_value_item;
50
+
47
51
ret [ // Target bindings.
48
- tup ( "target_os" , eval :: val_str ( std:: os:: target_os ( ) ) ) ,
49
- tup ( "target_arch" , eval :: val_str ( "x86" ) ) ,
50
- tup ( "target_libc" , eval :: val_str ( libc) ) ,
52
+ mk ( "target_os" , std:: os:: target_os ( ) ) ,
53
+ mk ( "target_arch" , "x86" ) ,
54
+ mk ( "target_libc" , libc) ,
51
55
// Build bindings.
52
- tup ( "build_compiler" , eval:: val_str ( argv0) ) ,
53
- tup ( "build_input" , eval:: val_str ( input) ) ] ;
56
+ mk ( "build_compiler" , argv0) ,
57
+ mk ( "build_input" , input) ] ;
58
+ }
59
+
60
+ fn build_configuration ( session:: session sess, str argv0 ,
61
+ str input ) -> ast:: crate_cfg {
62
+ // Combine the configuration requested by the session (command line) with
63
+ // some default configuration items
64
+ ret sess. get_opts ( ) . cfg + default_configuration ( sess, argv0, input) ;
65
+ }
66
+
67
+ // Convert strings provided as --cfg [cfgspec] into a crate_cfg
68
+ fn parse_cfgspecs ( & vec[ str] cfgspecs ) -> ast:: crate_cfg {
69
+ // FIXME: It would be nice to use the parser to parse all varieties of
70
+ // meta_item here. At the moment we just support the meta_word variant.
71
+ fn to_meta_word ( & str cfgspec ) -> @ast:: meta_item {
72
+ attr:: mk_word_item ( cfgspec)
73
+ }
74
+ ret vec:: map ( to_meta_word, cfgspecs) ;
54
75
}
55
76
56
77
fn parse_input ( session:: session sess, parser:: parser p, str input ) ->
@@ -73,10 +94,10 @@ fn time[T](bool do_it, str what, fn() -> T thunk) -> T {
73
94
ret rv;
74
95
}
75
96
76
- fn compile_input ( session:: session sess, eval :: env env , str input ,
97
+ fn compile_input ( session:: session sess, ast :: crate_cfg cfg , str input ,
77
98
str output ) {
78
99
auto time_passes = sess. get_opts ( ) . time_passes ;
79
- auto p = parser:: new_parser ( sess, env , input, 0 u, 0 ) ;
100
+ auto p = parser:: new_parser ( sess, cfg , input, 0 u, 0 ) ;
80
101
auto crate =
81
102
time ( time_passes, "parsing" , bind parse_input ( sess, p, input) ) ;
82
103
if ( sess. get_opts ( ) . output_type == link:: output_type_none) { ret; }
@@ -104,9 +125,9 @@ fn compile_input(session::session sess, eval::env env, str input,
104
125
bind link:: write:: run_passes ( sess, llmod, output) ) ;
105
126
}
106
127
107
- fn pretty_print_input ( session:: session sess, eval :: env env , str input ,
108
- pp_mode ppm) {
109
- auto p = front:: parser:: new_parser ( sess, env , input, 0 u, 0 ) ;
128
+ fn pretty_print_input ( session:: session sess, ast :: crate_cfg cfg ,
129
+ str input , pp_mode ppm) {
130
+ auto p = front:: parser:: new_parser ( sess, cfg , input, 0 u, 0 ) ;
110
131
auto crate = parse_input ( sess, p, input) ;
111
132
auto mode;
112
133
alt ( ppm) {
@@ -156,6 +177,7 @@ options:
156
177
--emit-llvm produce an LLVM bitcode file
157
178
--save-temps write intermediate files in addition to normal output
158
179
--stats gather and report various compilation statistics
180
+ --cfg [cfgspec] configure the compilation environment
159
181
--time-passes time the individual phases of the compiler
160
182
--time-llvm-passes time the individual phases of the LLVM backend
161
183
--sysroot <path> override the system root (default: rustc's directory)
@@ -253,6 +275,7 @@ fn build_session_options(str binary, getopts::match match, str binary_dir) ->
253
275
case ( none) { get_default_sysroot ( binary) }
254
276
case ( some ( ?s) ) { s }
255
277
} ;
278
+ auto cfg = parse_cfgspecs( getopts:: opt_strs( match , "cfg" ) ) ;
256
279
let @session:: options sopts =
257
280
@rec ( shared=shared,
258
281
optimize=opt_level,
@@ -265,7 +288,8 @@ fn build_session_options(str binary, getopts::match match, str binary_dir) ->
265
288
time_llvm_passes=time_llvm_passes,
266
289
output_type=output_type,
267
290
library_search_paths=library_search_paths,
268
- sysroot=sysroot) ;
291
+ sysroot=sysroot,
292
+ cfg=cfg) ;
269
293
ret sopts;
270
294
}
271
295
@@ -275,7 +299,7 @@ fn build_session(@session::options sopts) -> session::session {
275
299
auto target_crate_num = 0 ;
276
300
auto sess =
277
301
session:: session( target_crate_num, target_cfg, sopts, crate_cache, [ ] ,
278
- [ ] , front:: codemap:: new_codemap ( ) , 0 u) ;
302
+ [ ] , [ ] , front:: codemap:: new_codemap( ) , 0 u) ;
279
303
ret sess;
280
304
}
281
305
@@ -298,7 +322,7 @@ fn main(vec[str] args) {
298
322
optflag ( "c" ) , optopt ( "o" ) , optflag ( "g" ) , optflag ( "save-temps" ) ,
299
323
optopt ( "sysroot" ) , optflag ( "stats" ) , optflag ( "time-passes" ) ,
300
324
optflag ( "time-llvm-passes" ) , optflag ( "no-typestate" ) ,
301
- optflag ( "noverify" ) ] ;
325
+ optflag ( "noverify" ) , optmulti ( "cfg" ) ] ;
302
326
auto binary = vec:: shift[ str] ( args) ;
303
327
auto binary_dir = fs:: dirname ( binary) ;
304
328
auto match =
@@ -337,15 +361,15 @@ fn main(vec[str] args) {
337
361
}
338
362
auto ifile = match . free . ( 0 ) ;
339
363
let str saved_out_filename = "" ;
340
- auto env = default_environment ( sess, binary, ifile) ;
364
+ auto cfg = build_configuration ( sess, binary, ifile) ;
341
365
auto pretty =
342
366
option:: map[ str,
343
367
pp_mode] ( bind parse_pretty ( sess, _) ,
344
368
getopts:: opt_default ( match , "pretty" , "normal" ) ) ;
345
369
auto ls = opt_present ( match , "ls" ) ;
346
370
alt ( pretty) {
347
371
case ( some[ pp_mode] ( ?ppm) ) {
348
- pretty_print_input ( sess, env , ifile, ppm) ;
372
+ pretty_print_input ( sess, cfg , ifile, ppm) ;
349
373
ret;
350
374
}
351
375
case ( none[ pp_mode] ) { /* continue */ }
@@ -371,7 +395,7 @@ fn main(vec[str] args) {
371
395
case ( link:: output_type_exe) { parts += [ "o" ] ; }
372
396
}
373
397
auto ofile = str:: connect ( parts, "." ) ;
374
- compile_input ( sess, env , ifile, ofile) ;
398
+ compile_input ( sess, cfg , ifile, ofile) ;
375
399
}
376
400
case ( some ( ?ofile) ) {
377
401
// FIXME: what about windows? This will create a foo.exe.o.
@@ -386,7 +410,7 @@ fn main(vec[str] args) {
386
410
}
387
411
case ( _) { temp_filename = ofile; }
388
412
}
389
- compile_input ( sess, env , ifile, temp_filename) ;
413
+ compile_input ( sess, cfg , ifile, temp_filename) ;
390
414
}
391
415
}
392
416
@@ -406,16 +430,11 @@ fn main(vec[str] args) {
406
430
saved_out_filename, saved_out_filename + ".o" ] ;
407
431
auto shared_cmd;
408
432
409
- alt ( sess. get_targ_cfg ( ) . os ) {
410
- case ( session:: os_win32) {
411
- shared_cmd = "-shared" ;
412
- }
413
- case ( session:: os_macos) {
433
+ auto os = sess. get_targ_cfg ( ) . os ;
434
+ if ( os == session:: os_macos) {
414
435
shared_cmd = "-dynamiclib" ;
415
- }
416
- case ( session:: os_linux) {
436
+ } else {
417
437
shared_cmd = "-shared" ;
418
- }
419
438
}
420
439
421
440
// Converts a library file name into a gcc -l argument
@@ -441,7 +460,7 @@ fn main(vec[str] args) {
441
460
case ( _) { rmext ( filename) }
442
461
} ;
443
462
}
444
-
463
+
445
464
for ( str cratepath in sess. get_used_crate_files( ) ) {
446
465
auto dir = fs:: dirname( cratepath) ;
447
466
if ( dir != "" ) {
@@ -451,6 +470,7 @@ fn main(vec[str] args) {
451
470
gcc_args += [ "-l" + libarg] ;
452
471
}
453
472
473
+ gcc_args += sess. get_used_link_args( ) ;
454
474
auto used_libs = sess. get_used_libraries( ) ;
455
475
for ( str l in used_libs) {
456
476
gcc_args += [ "-l" + l] ;
@@ -459,9 +479,8 @@ fn main(vec[str] args) {
459
479
if ( sopts. shared) {
460
480
gcc_args += [ shared_cmd] ;
461
481
} else {
462
- // FIXME: having -Lrustllvm hardcoded in here is hack
463
- // FIXME: same for -lm
464
- gcc_args += [ "-Lrustllvm" , "-lm" , main] ;
482
+ // FIXME: why do we hardcode -lm?
483
+ gcc_args += [ "-lm" , main] ;
465
484
}
466
485
// We run 'gcc' here
467
486
0 commit comments