Skip to content

Commit 9116a6d

Browse files
committed
rustc: Support 'crate_type' attribute
[crate_type = "lib"] builds it as a library. [crate_type = "bin"] builds it as an executable. Executable is the default. --lib and --bin switches override.
1 parent 6156295 commit 9116a6d

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

src/comp/driver/session.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,80 @@ obj session(targ_cfg: @config,
120120
fn filesearch() -> filesearch::filesearch { filesearch }
121121
fn building_library() -> bool { opts.crate_type == lib_crate }
122122
}
123+
124+
fn building_library(req_crate_type: crate_type, crate: @ast::crate) -> bool {
125+
alt req_crate_type {
126+
bin_crate. { false }
127+
lib_crate. { true }
128+
unknown_crate. {
129+
alt front::attr::get_meta_item_value_str_by_name(
130+
crate.node.attrs,
131+
"crate_type") {
132+
option::some("lib") { true }
133+
_ { false }
134+
}
135+
}
136+
}
137+
}
138+
139+
#[cfg(test)]
140+
mod test {
141+
import syntax::ast_util;
142+
143+
fn make_crate_type_attr(t: str) -> ast::attribute {
144+
ast_util::respan(ast_util::dummy_sp(), {
145+
style: ast::attr_outer,
146+
value: ast_util::respan(ast_util::dummy_sp(),
147+
ast::meta_name_value(
148+
"crate_type",
149+
ast_util::respan(ast_util::dummy_sp(),
150+
ast::lit_str(t))))
151+
})
152+
}
153+
154+
fn make_crate(with_bin: bool, with_lib: bool) -> @ast::crate {
155+
let attrs = [];
156+
if with_bin { attrs += [make_crate_type_attr("bin")]; }
157+
if with_lib { attrs += [make_crate_type_attr("lib")]; }
158+
@ast_util::respan(ast_util::dummy_sp(), {
159+
directives: [],
160+
module: {view_items: [], items: []},
161+
attrs: attrs,
162+
config: []
163+
})
164+
}
165+
166+
#[test]
167+
fn bin_crate_type_attr_results_in_bin_output() {
168+
let crate = make_crate(true, false);
169+
assert !building_library(unknown_crate, crate);
170+
}
171+
172+
#[test]
173+
fn lib_crate_type_attr_results_in_lib_output() {
174+
let crate = make_crate(false, true);
175+
assert building_library(unknown_crate, crate);
176+
}
177+
178+
#[test]
179+
fn bin_option_overrides_lib_crate_type() {
180+
let crate = make_crate(false, true);
181+
assert !building_library(bin_crate, crate);
182+
}
183+
184+
#[test]
185+
fn lib_option_overrides_bin_crate_type() {
186+
let crate = make_crate(true, false);
187+
assert building_library(lib_crate, crate);
188+
}
189+
190+
#[test]
191+
fn bin_crate_type_is_default() {
192+
let crate = make_crate(false, false);
193+
assert !building_library(unknown_crate, crate);
194+
}
195+
}
196+
123197
// Local Variables:
124198
// fill-column: 78;
125199
// indent-tabs-mode: nil

0 commit comments

Comments
 (0)