Use attributes for conditional compilation in std.rc

This commit is contained in:
Brian Anderson 2011-06-30 14:12:11 -07:00
parent df8161d44c
commit 64d6081429
3 changed files with 38 additions and 14 deletions

View File

@ -99,11 +99,15 @@ fn eq(@ast::meta_item a, @ast::meta_item b) -> bool {
}
fn contains(&vec[@ast::meta_item] haystack, @ast::meta_item needle) -> bool {
log #fmt("looking for %s", pretty::pprust::meta_item_to_str(*needle));
for (@ast::meta_item item in haystack) {
log #fmt("looking in %s", pretty::pprust::meta_item_to_str(*item));
if (eq(item, needle)) {
log "found it!";
ret true;
}
}
log "found it not :(";
ret false;
}

View File

@ -71,12 +71,30 @@ fn fold_block(&ast::crate_cfg cfg, &ast::block_ b,
// configuration based on the item's attributes
fn in_cfg(&ast::crate_cfg cfg, &@ast::item item) -> bool {
// The "cfg" attributes on the item
auto item_cfg_attrs = attr::find_attrs_by_name(item.attrs, "cfg");
auto item_has_cfg_attrs = vec::len(item_cfg_attrs) > 0u;
if (!item_has_cfg_attrs) { ret true; }
auto item_cfg_metas = attr::attr_metas(item_cfg_attrs);
// Pull the inner meta_items from the #[cfg(meta_item, ...)] attributes,
// so we can match against them. This is the list of configurations for
// which the item is valid
auto item_cfg_metas = {
fn extract_metas(&vec[@ast::meta_item] inner_items,
&@ast::meta_item cfg_item)
-> vec[@ast::meta_item] {
alt (cfg_item.node) {
case (ast::meta_list(?name, ?items)) {
assert name == "cfg";
inner_items + items
}
case (_) { inner_items }
}
}
auto cfg_metas = attr::attr_metas(item_cfg_attrs);
vec::foldl(extract_metas, [], cfg_metas)
};
for (@ast::meta_item cfg_mi in item_cfg_metas) {
if (attr::contains(cfg, cfg_mi)) {

View File

@ -49,18 +49,20 @@ auth rand::mk_rng = unsafe;
// TODO: Have each os module re-export everything from genericos.
mod generic_os;
alt (target_os) {
case ("win32") {
mod os = "win32_os.rs";
mod os_fs = "win32_fs.rs";
} case ("macos") {
mod os = "macos_os.rs";
mod os_fs = "posix_fs.rs";
} case (_) {
mod os = "linux_os.rs";
mod os_fs = "posix_fs.rs";
}
}
#[cfg(target_os = "win32")]
mod os = "win32_os.rs";
#[cfg(target_os = "win32")]
mod os_fs = "win32_fs.rs";
#[cfg(target_os = "macos")]
mod os = "macos_os.rs";
#[cfg(target_os = "macos")]
mod os_fs = "posix_fs.rs";
#[cfg(target_os = "linux")]
mod os = "linux_os.rs";
#[cfg(target_os = "linux")]
mod os_fs = "posix_fs.rs";
mod run = "run_program.rs";
mod fs;