Skip to content

Commit 396c4de

Browse files
committed
Introduce --cfg argument for providing additional configuration. Issue #489
1 parent a34b61f commit 396c4de

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/comp/driver/rustc.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ fn default_configuration(session::session sess, str argv0, str input) ->
5757
mk("build_input", input)];
5858
}
5959

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);
75+
}
76+
6077
fn parse_input(session::session sess, parser::parser p, str input) ->
6178
@ast::crate {
6279
ret if (str::ends_with(input, ".rc")) {
@@ -160,6 +177,7 @@ options:
160177
--emit-llvm produce an LLVM bitcode file
161178
--save-temps write intermediate files in addition to normal output
162179
--stats gather and report various compilation statistics
180+
--cfg [cfgspec] configure the compilation environment
163181
--time-passes time the individual phases of the compiler
164182
--time-llvm-passes time the individual phases of the LLVM backend
165183
--sysroot <path> override the system root (default: rustc's directory)
@@ -257,6 +275,7 @@ fn build_session_options(str binary, getopts::match match, str binary_dir) ->
257275
case (none) { get_default_sysroot(binary) }
258276
case (some(?s)) { s }
259277
};
278+
auto cfg = parse_cfgspecs(getopts::opt_strs(match, "cfg"));
260279
let @session::options sopts =
261280
@rec(shared=shared,
262281
optimize=opt_level,
@@ -269,7 +288,8 @@ fn build_session_options(str binary, getopts::match match, str binary_dir) ->
269288
time_llvm_passes=time_llvm_passes,
270289
output_type=output_type,
271290
library_search_paths=library_search_paths,
272-
sysroot=sysroot);
291+
sysroot=sysroot,
292+
cfg=cfg);
273293
ret sopts;
274294
}
275295

@@ -302,7 +322,7 @@ fn main(vec[str] args) {
302322
optflag("c"), optopt("o"), optflag("g"), optflag("save-temps"),
303323
optopt("sysroot"), optflag("stats"), optflag("time-passes"),
304324
optflag("time-llvm-passes"), optflag("no-typestate"),
305-
optflag("noverify")];
325+
optflag("noverify"), optmulti("cfg")];
306326
auto binary = vec::shift[str](args);
307327
auto binary_dir = fs::dirname(binary);
308328
auto match =
@@ -341,7 +361,7 @@ fn main(vec[str] args) {
341361
}
342362
auto ifile = match.free.(0);
343363
let str saved_out_filename = "";
344-
auto cfg = default_configuration(sess, binary, ifile);
364+
auto cfg = build_configuration(sess, binary, ifile);
345365
auto pretty =
346366
option::map[str,
347367
pp_mode](bind parse_pretty(sess, _),

src/comp/driver/session.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ type options =
3434
bool time_llvm_passes,
3535
back::link::output_type output_type,
3636
vec[str] library_search_paths,
37-
str sysroot);
37+
str sysroot,
38+
// The crate config requested for the session, which may be combined
39+
// with additional crate configurations during the compile process
40+
ast::crate_cfg cfg);
3841

3942
type crate_metadata = rec(str name, vec[u8] data);
4043

0 commit comments

Comments
 (0)