about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2011-12-08 21:05:44 -0800
committerBrian Anderson <banderson@mozilla.com>2011-12-08 21:09:15 -0800
commit9116a6d9b10aee0cd29390d9370ff25e7b995004 (patch)
tree21804f82ace82c6ae0d51a94c883d111ec7db229
parent6156295a4c0ed42f1f383aff3aa6cc65fe1e1f30 (diff)
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.
-rw-r--r--src/comp/driver/session.rs74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/comp/driver/session.rs b/src/comp/driver/session.rs
index 2292c5c2882..1317a9a328c 100644
--- a/src/comp/driver/session.rs
+++ b/src/comp/driver/session.rs
@@ -120,6 +120,80 @@ obj session(targ_cfg: @config,
     fn filesearch() -> filesearch::filesearch { filesearch }
     fn building_library() -> bool { opts.crate_type == lib_crate }
 }
+
+fn building_library(req_crate_type: crate_type, crate: @ast::crate) -> bool {
+    alt req_crate_type {
+      bin_crate. { false }
+      lib_crate. { true }
+      unknown_crate. {
+        alt front::attr::get_meta_item_value_str_by_name(
+            crate.node.attrs,
+            "crate_type") {
+          option::some("lib") { true }
+          _ { false }
+        }
+      }
+    }
+}
+
+#[cfg(test)]
+mod test {
+    import syntax::ast_util;
+
+    fn make_crate_type_attr(t: str) -> ast::attribute {
+        ast_util::respan(ast_util::dummy_sp(), {
+            style: ast::attr_outer,
+            value: ast_util::respan(ast_util::dummy_sp(),
+                ast::meta_name_value(
+                    "crate_type",
+                    ast_util::respan(ast_util::dummy_sp(),
+                                     ast::lit_str(t))))
+        })
+    }
+
+    fn make_crate(with_bin: bool, with_lib: bool) -> @ast::crate {
+        let attrs = [];
+        if with_bin { attrs += [make_crate_type_attr("bin")]; }
+        if with_lib { attrs += [make_crate_type_attr("lib")]; }
+        @ast_util::respan(ast_util::dummy_sp(), {
+            directives: [],
+            module: {view_items: [], items: []},
+            attrs: attrs,
+            config: []
+        })
+    }
+
+    #[test]
+    fn bin_crate_type_attr_results_in_bin_output() {
+        let crate = make_crate(true, false);
+        assert !building_library(unknown_crate, crate);
+    }
+
+    #[test]
+    fn lib_crate_type_attr_results_in_lib_output() {
+        let crate = make_crate(false, true);
+        assert building_library(unknown_crate, crate);
+    }
+
+    #[test]
+    fn bin_option_overrides_lib_crate_type() {
+        let crate = make_crate(false, true);
+        assert !building_library(bin_crate, crate);
+    }
+
+    #[test]
+    fn lib_option_overrides_bin_crate_type() {
+        let crate = make_crate(true, false);
+        assert building_library(lib_crate, crate);
+    }
+
+    #[test]
+    fn bin_crate_type_is_default() {
+        let crate = make_crate(false, false);
+        assert !building_library(unknown_crate, crate);
+    }
+}
+
 // Local Variables:
 // fill-column: 78;
 // indent-tabs-mode: nil