about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTim Chevalier <chevalier@alum.wellesley.edu>2013-04-17 13:53:34 -0700
committerTim Chevalier <chevalier@alum.wellesley.edu>2013-04-17 14:32:34 -0700
commitc01c3d9fc6e28b8260894c9522eb29e29874dcbd (patch)
tree513ac6794d09de418527e420db683a6113ff6bb7
parentc2f5a87cdd79e5531b751749827d2e8a3d269c9e (diff)
downloadrust-c01c3d9fc6e28b8260894c9522eb29e29874dcbd.tar.gz
rust-c01c3d9fc6e28b8260894c9522eb29e29874dcbd.zip
rustpkg: Fail when crate inference fails; inject link attributes
1. Fail when there's no package script and no crates named
main.rs, lib.rs, bench.rs, or test.rs.

2. Inject the crate link_meta "name" and "vers" attributes, so
that the output file gets named correctly in the library case.

3. Normalize '-' to '_' in package names.
-rw-r--r--src/librustpkg/path_util.rs21
-rw-r--r--src/librustpkg/rustpkg.rc92
-rw-r--r--src/librustpkg/util.rs114
3 files changed, 142 insertions, 85 deletions
diff --git a/src/librustpkg/path_util.rs b/src/librustpkg/path_util.rs
index c821281184d..140d9ced580 100644
--- a/src/librustpkg/path_util.rs
+++ b/src/librustpkg/path_util.rs
@@ -11,7 +11,8 @@
 // rustpkg utilities having to do with paths and directories
 
 use core::path::*;
-use core::os;
+use core::{os, str};
+use core::option::*;
 use util::PkgId;
 
 /// Returns the output directory to use.
@@ -50,6 +51,24 @@ pub fn default_dest_dir(pkg_dir: &Path) -> Path {
     }
 }
 
+/// Replace all occurrences of '-' in the stem part of path with '_'
+/// This is because we treat rust-foo-bar-quux and rust_foo_bar_quux
+/// as the same name
+pub fn normalize(p: ~Path) -> ~Path {
+    match p.filestem() {
+        None => p,
+        Some(st) => {
+            let replaced = str::replace(st, "-", "_");
+            if replaced != st {
+                ~p.with_filestem(replaced)
+            }
+            else {
+                p
+            }
+        }
+    }
+}
+
 #[cfg(test)]
 mod test {
     use core::{os, rand};
diff --git a/src/librustpkg/rustpkg.rc b/src/librustpkg/rustpkg.rc
index f8805142769..b663c8742ff 100644
--- a/src/librustpkg/rustpkg.rc
+++ b/src/librustpkg/rustpkg.rc
@@ -36,8 +36,9 @@ use rustc::metadata::filesearch;
 use std::net::url;
 use std::{getopts};
 use syntax::{ast, diagnostic};
-use util::{ExitCode, Pkg, PkgId};
-use path_util::dest_dir;
+use util::*;
+use path_util::{dest_dir, normalize};
+use rustc::driver::session::{lib_crate, bin_crate, unknown_crate, crate_type};
 
 mod conditions;
 mod usage;
@@ -117,9 +118,12 @@ impl PkgScript {
             Ok(r) => {
                 let root = r.pop().pop().pop().pop(); // :-\
                 debug!("Root is %s, calling compile_rest", root.to_str());
-                util::compile_crate_from_input(self.input, Some(self.build_dir),
-                                               sess, Some(crate), os::args()[0]);
                 let exe = self.build_dir.push(~"pkg" + util::exe_suffix());
+                util::compile_crate_from_input(self.input, self.id,
+                                               Some(self.build_dir),
+                                               sess, Some(crate),
+                                               exe, os::args()[0],
+                                               driver::cu_everything);
                 debug!("Running program: %s %s %s", exe.to_str(), root.to_str(), what);
                 let status = run::run_program(exe.to_str(), ~[root.to_str(), what]);
                 if status != 0 {
@@ -199,15 +203,15 @@ impl Ctx {
                 // relative to the CWD. In the future, we should search
                 // paths
                 let cwd = os::getcwd().normalize();
-                debug!("Current working directory = %?", cwd);
+                debug!("Current working directory = %s", cwd.to_str());
 
-                // Find crates inside the workspace
+                // Create the package source
                 let mut src = PkgSrc::new(&cwd, &dst_dir, &pkgid);
                 debug!("Package src = %?", src);
-                src.find_crates();
 
                 // Is there custom build logic? If so, use it
                 let pkg_src_dir = cwd.push_rel(&pkgid.path);
+                let mut custom = false;;
                 debug!("Package source directory = %s", pkg_src_dir.to_str());
                 let cfgs = match src.package_script_option(&pkg_src_dir) {
                     Some(package_script_path) => {
@@ -221,6 +225,7 @@ impl Ctx {
                         if hook_result != 0 {
                             fail!(fmt!("Error running custom build command"))
                         }
+                        custom = true;
                         // otherwise, the package script succeeded
                         cfgs
                     }
@@ -229,6 +234,13 @@ impl Ctx {
                         ~[]
                     }
                 };
+
+                // Find crates inside the workspace
+                if !custom {
+                    src.find_crates();
+                }
+
+                // Build it!
                 src.build(&dst_dir, cfgs);
             }
             ~"clean" => {
@@ -728,7 +740,6 @@ condition! {
 
 impl PkgSrc {
 
-
     fn new(src_dir: &Path, dst_dir: &Path,
                   id: &PkgId) -> PkgSrc {
         PkgSrc {
@@ -765,12 +776,6 @@ impl PkgSrc {
         dir
     }
 
-
-    fn has_pkg_file(&self) -> bool {
-        let dir = self.check_dir();
-        dir.push("pkg.rs").exists()
-    }
-
     // If a file named "pkg.rs" in the current directory exists,
     // return the path for it. Otherwise, None
     fn package_script_option(&self, cwd: &Path) -> Option<Path> {
@@ -786,14 +791,16 @@ impl PkgSrc {
     /// True if the given path's stem is self's pkg ID's stem
     /// or if the pkg ID's stem is <rust-foo> and the given path's
     /// stem is foo
+    /// Requires that dashes in p have already been normalized to
+    /// underscores
     fn stem_matches(&self, p: &Path) -> bool {
-        let self_id = self.id.path.filestem();
+        let self_id = normalize(~self.id.path).filestem();
         if self_id == p.filestem() {
             return true;
         }
         else {
             for self_id.each |pth| {
-                if pth.starts_with("rust-")
+                if pth.starts_with("rust_") // because p is already normalized
                     && match p.filestem() {
                            Some(s) => str::eq_slice(s, pth.slice(5, pth.len())),
                            None => false
@@ -814,17 +821,14 @@ impl PkgSrc {
         cs.push(Crate::new(&sub));
     }
 
+    /// Infers crates to build. Called only in the case where there
+    /// is no custom build logic
     fn find_crates(&mut self) {
         use PkgSrc::push_crate;
 
         let dir = self.check_dir();
         let prefix = dir.components.len();
-        // This is ugly, but can go away once we get rid
-        // of .rc files
-        let mut saw_rs = false;
-        let mut saw_rc = false;
-        debug!("Matching against %?",
-               self.id.path.filestem());
+        debug!("Matching against %?", self.id.path.filestem());
         for os::walk_dir(&dir) |pth| {
             match pth.filename() {
                 Some(~"lib.rs") => push_crate(&mut self.libs,
@@ -836,30 +840,10 @@ impl PkgSrc {
                 Some(~"bench.rs") => push_crate(&mut self.benchs,
                                                 prefix, pth),
                 _ => {
-                    // If the file stem is the same as the
-                    // package ID, with an .rs or .rc extension,
-                    // consider it to be a crate
-                    let ext = pth.filetype();
-                    let matches = |p: &Path| {
-                        self.stem_matches(p) && (ext == Some(~".rc")
-                                                  || ext == Some(~".rs"))
-                    };
-                    debug!("Checking %? which %s and ext = %? %? %?", pth.filestem(),
-                           if matches(pth) { "matches" } else { "does not match" },
-                           ext, saw_rs, saw_rc);
-                    if matches(pth) &&
-                        // Avoid pushing foo.rc *and* foo.rs
-                         !((ext == Some(~".rc") && saw_rs) ||
-                           (ext == Some(~".rs") && saw_rc)) {
-                        push_crate(&mut self.libs, // ????
-                                   prefix, pth);
-                        if ext == Some(~".rc") {
-                            saw_rc = true;
-                        }
-                        else if ext == Some(~".rs") {
-                            saw_rs = true;
-                        }
-                    }
+                    util::note(~"Couldn't infer any crates to build.\n\
+                                Try naming a crate `main.rs`, `lib.rs`, \
+                                `test.rs`, or `bench.rs`.");
+                    fail!(~"Failed to infer crates to build");
                 }
             }
         }
@@ -870,22 +854,22 @@ impl PkgSrc {
                self.benchs.len())
     }
 
-    fn build_crates(dst_dir: &Path,
+    fn build_crates(&self, dst_dir: &Path,
                            src_dir: &Path,
                            crates: &[Crate],
                            cfgs: ~[~str],
-                           test: bool) {
+                           test: bool, crate_type: crate_type) {
 
         for crates.each |&crate| {
             let path = &src_dir.push_rel(&crate.file).normalize();
             util::note(fmt!("build_crates: compiling %s", path.to_str()));
             util::note(fmt!("build_crates: destination dir is %s", dst_dir.to_str()));
 
-            let result = util::compile_crate(None, path,
+            let result = util::compile_crate(None, self.id, path,
                                      dst_dir,
                                      crate.flags,
                                      crate.cfgs + cfgs,
-                                     false, test);
+                                     false, test, crate_type);
             if !result {
                 build_err::cond.raise(fmt!("build failure on %s",
                                            path.to_str()));
@@ -898,12 +882,12 @@ impl PkgSrc {
     fn build(&self, dst_dir: &Path, cfgs: ~[~str]) {
         let dir = self.check_dir();
         debug!("Building libs");
-        PkgSrc::build_crates(dst_dir, &dir, self.libs, cfgs, false);
+        self.build_crates(dst_dir, &dir, self.libs, cfgs, false, lib_crate);
         debug!("Building mains");
-        PkgSrc::build_crates(dst_dir, &dir, self.mains, cfgs, false);
+        self.build_crates(dst_dir, &dir, self.mains, cfgs, false, bin_crate);
         debug!("Building tests");
-        PkgSrc::build_crates(dst_dir, &dir, self.tests, cfgs, true);
+        self.build_crates(dst_dir, &dir, self.tests, cfgs, true, bin_crate);
         debug!("Building benches");
-        PkgSrc::build_crates(dst_dir, &dir, self.benchs, cfgs, true);
+        self.build_crates(dst_dir, &dir, self.benchs, cfgs, true, bin_crate);
     }
 }
diff --git a/src/librustpkg/util.rs b/src/librustpkg/util.rs
index 19938e8c5f1..1709a8d06ad 100644
--- a/src/librustpkg/util.rs
+++ b/src/librustpkg/util.rs
@@ -12,16 +12,20 @@ use core::*;
 use core::cmp::Ord;
 use core::hash::Streaming;
 use rustc::driver::{driver, session};
+use rustc::driver::session::{lib_crate, bin_crate, unknown_crate};
 use rustc::metadata::filesearch;
 use std::getopts::groups::getopts;
 use std::semver;
 use std::{json, term, getopts};
 use syntax::ast_util::*;
-use syntax::codemap::{dummy_sp};
+use syntax::codemap::{dummy_sp, spanned, dummy_spanned};
 use syntax::ext::base::{mk_ctxt, ext_ctxt};
 use syntax::ext::build;
 use syntax::{ast, attr, codemap, diagnostic, fold};
+use syntax::ast::{meta_name_value, meta_list, attribute, crate_};
+use syntax::attr::{mk_attr};
 use rustc::back::link::output_type_exe;
+use rustc::driver::session::{lib_crate, bin_crate, unknown_crate, crate_type};
 
 pub type ExitCode = int; // For now
 
@@ -112,7 +116,7 @@ pub impl PkgId {
 impl ToStr for PkgId {
     fn to_str(&self) -> ~str {
         // should probably use the filestem and not the whole path
-        fmt!("%s-v%s", self.path.to_str(), self.version.to_str())
+        fmt!("%s-%s", self.path.to_str(), self.version.to_str())
     }
 }
 
@@ -425,44 +429,51 @@ pub fn add_pkg(pkg: &Pkg) -> bool {
 
 // FIXME (#4432): Use workcache to only compile when needed
 pub fn compile_input(sysroot: Option<Path>,
+                     pkg_id: PkgId,
                      in_file: &Path,
                      out_dir: &Path,
                      flags: ~[~str],
                      cfgs: ~[~str],
                      opt: bool,
-                     test: bool) -> bool {
+                     test: bool,
+                     crate_type: session::crate_type) -> bool {
+
+    let short_name = pkg_id.to_str();
 
     assert!(in_file.components.len() > 1);
     let input = driver::file_input(copy *in_file);
-    debug!("compile_input: %s", in_file.to_str());
+    debug!("compile_input: %s / %?", in_file.to_str(), crate_type);
     // tjc: by default, use the package ID name as the link name
     // not sure if we should support anything else
-    let short_name = in_file.filestem().expect("Can't compile a directory!");
-    debug!("short_name = %s", short_name.to_str());
 
-// Right now we're always assuming that we're building a library.
-// What we should do is parse the crate and infer whether it's a library
-// from the absence or presence of a main fn
-    let out_file = out_dir.push(os::dll_filename(short_name));
-    let building_library = true;
+    let binary = os::args()[0];
+    let building_library = match crate_type {
+        lib_crate | unknown_crate => true,
+        _ => false
+    };
+
+    let out_file = if building_library {
+        out_dir.push(os::dll_filename(short_name))
+    }
+    else {
+        out_dir.push(short_name + if test { ~"test" } else { ~"" }
+                     + os::EXE_SUFFIX)
+    };
 
     debug!("compiling %s into %s",
            in_file.to_str(),
            out_file.to_str());
-
-    let binary = os::args()[0];
-
     debug!("flags: %s", str::connect(flags, ~" "));
     debug!("cfgs: %s", str::connect(cfgs, ~" "));
-// Again, we assume we're building a library
+
     let matches = getopts(~[~"-Z", ~"time-passes"]
-                          + if building_library { ~[~"--lib"] } else { ~[] }
+                          + if building_library { ~[~"--lib"] }
+                            else { ~[] }
                           + flags
                           + cfgs.flat_map(|&c| { ~[~"--cfg", c] }),
                           driver::optgroups()).get();
     let options = @session::options {
-        crate_type: if building_library { session::lib_crate }
-                    else { session::bin_crate },
+        crate_type: crate_type,
         optimize: if opt { session::Aggressive } else { session::No },
         test: test,
         maybe_sysroot: sysroot,
@@ -485,7 +496,9 @@ pub fn compile_input(sysroot: Option<Path>,
 
     debug!("calling compile_crate_from_input, out_dir = %s,
            building_library = %?", out_dir.to_str(), sess.building_library);
-    compile_crate_from_input(input, Some(*out_dir), sess, None, binary);
+    let _ = compile_crate_from_input(input, pkg_id, Some(*out_dir), sess, None,
+                                     out_file, binary,
+                                     driver::cu_everything);
     true
 }
 
@@ -493,24 +506,45 @@ pub fn compile_input(sysroot: Option<Path>,
 // Should also rename this to something better
 // If crate_opt is present, then finish compilation. If it's None, then
 // call compile_upto and return the crate
-pub fn compile_crate_from_input(input: driver::input, build_dir_opt: Option<Path>,
-    sess: session::Session, crate_opt: Option<@ast::crate>,
-                                binary: ~str) -> @ast::crate {
+// also, too many arguments
+pub fn compile_crate_from_input(input: driver::input,
+                                pkg_id: PkgId,
+                                build_dir_opt: Option<Path>,
+                                sess: session::Session,
+                                crate_opt: Option<@ast::crate>,
+                                out_file: Path,
+                                binary: ~str,
+                                what: driver::compile_upto) -> @ast::crate {
     debug!("Calling build_output_filenames with %?", build_dir_opt);
-    let outputs = driver::build_output_filenames(input, &build_dir_opt, &None, sess);
+    let outputs = driver::build_output_filenames(input, &build_dir_opt, &Some(out_file), sess);
     debug!("Outputs are %? and output type = %?", outputs, sess.opts.output_type);
     let cfg = driver::build_configuration(sess, binary, input);
     match crate_opt {
         Some(c) => {
             debug!("Calling compile_rest, outputs = %?", outputs);
+            assert!(what == driver::cu_everything);
             driver::compile_rest(sess, cfg, driver::cu_everything, Some(outputs), Some(c));
             c
         }
         None => {
             debug!("Calling compile_upto, outputs = %?", outputs);
-            let (crate, _) = driver::compile_upto(sess, cfg, input, driver::cu_parse,
-                                                  Some(outputs));
-            crate
+            let (crate, _) = driver::compile_upto(sess, cfg, input, driver::cu_parse, Some(outputs));
+
+            // Inject the inferred link_meta info if it's not already there
+            // (assumes that name and vers are the only linkage metas)
+            let mut crate_to_use = crate;
+            if attr::find_linkage_metas(crate.node.attrs).is_empty() {
+                crate_to_use = add_attrs(*crate, ~[mk_attr(@dummy_spanned(meta_list(@~"link",
+                                                  // change PkgId to have a <shortname> field?
+                    ~[@dummy_spanned(meta_name_value(@~"name",
+                                                    mk_string_lit(@pkg_id.path.filestem().get()))),
+                      @dummy_spanned(meta_name_value(@~"vers",
+                                                    mk_string_lit(@pkg_id.version.to_str())))])))]);
+            }
+
+
+            driver::compile_rest(sess, cfg, what, Some(outputs), Some(crate_to_use));
+            crate_to_use
         }
     }
 }
@@ -525,17 +559,30 @@ pub fn exe_suffix() -> ~str { ~".exe" }
 pub fn exe_suffix() -> ~str { ~"" }
 
 
+/// Returns a copy of crate `c` with attributes `attrs` added to its
+/// attributes
+fn add_attrs(c: ast::crate, new_attrs: ~[attribute]) -> @ast::crate {
+    @spanned {
+        node: crate_ {
+            attrs: c.node.attrs + new_attrs, ..c.node
+        },
+        span: c.span
+    }
+}
+
 // Called by build_crates
 // FIXME (#4432): Use workcache to only compile when needed
-pub fn compile_crate(sysroot: Option<Path>, crate: &Path, dir: &Path,
+pub fn compile_crate(sysroot: Option<Path>, pkg_id: PkgId,
+                     crate: &Path, dir: &Path,
                      flags: ~[~str], cfgs: ~[~str], opt: bool,
-                     test: bool) -> bool {
+                     test: bool, crate_type: crate_type) -> bool {
     debug!("compile_crate: crate=%s, dir=%s", crate.to_str(), dir.to_str());
-    debug!("compile_crate: flags =...");
+    debug!("compile_crate: short_name = %s, flags =...", pkg_id.to_str());
     for flags.each |&fl| {
         debug!("+++ %s", fl);
     }
-    compile_input(sysroot, crate, dir, flags, cfgs, opt, test)
+    compile_input(sysroot, pkg_id,
+                  crate, dir, flags, cfgs, opt, test, crate_type)
 }
 
 
@@ -563,6 +610,13 @@ pub fn link_exe(src: &Path, dest: &Path) -> bool {
     }
 }
 
+pub fn mk_string_lit(s: @~str) -> ast::lit {
+    spanned {
+        node: ast::lit_str(s),
+        span: dummy_sp()
+    }
+}
+
 #[cfg(test)]
 mod test {
     use super::{is_cmd, parse_name};