Skip to content

Commit c01c3d9

Browse files
committed
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.
1 parent c2f5a87 commit c01c3d9

File tree

3 files changed

+142
-85
lines changed

3 files changed

+142
-85
lines changed

src/librustpkg/path_util.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
// rustpkg utilities having to do with paths and directories
1212

1313
use core::path::*;
14-
use core::os;
14+
use core::{os, str};
15+
use core::option::*;
1516
use util::PkgId;
1617

1718
/// Returns the output directory to use.
@@ -50,6 +51,24 @@ pub fn default_dest_dir(pkg_dir: &Path) -> Path {
5051
}
5152
}
5253

54+
/// Replace all occurrences of '-' in the stem part of path with '_'
55+
/// This is because we treat rust-foo-bar-quux and rust_foo_bar_quux
56+
/// as the same name
57+
pub fn normalize(p: ~Path) -> ~Path {
58+
match p.filestem() {
59+
None => p,
60+
Some(st) => {
61+
let replaced = str::replace(st, "-", "_");
62+
if replaced != st {
63+
~p.with_filestem(replaced)
64+
}
65+
else {
66+
p
67+
}
68+
}
69+
}
70+
}
71+
5372
#[cfg(test)]
5473
mod test {
5574
use core::{os, rand};

src/librustpkg/rustpkg.rc

Lines changed: 38 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ use rustc::metadata::filesearch;
3636
use std::net::url;
3737
use std::{getopts};
3838
use syntax::{ast, diagnostic};
39-
use util::{ExitCode, Pkg, PkgId};
40-
use path_util::dest_dir;
39+
use util::*;
40+
use path_util::{dest_dir, normalize};
41+
use rustc::driver::session::{lib_crate, bin_crate, unknown_crate, crate_type};
4142

4243
mod conditions;
4344
mod usage;
@@ -117,9 +118,12 @@ impl PkgScript {
117118
Ok(r) => {
118119
let root = r.pop().pop().pop().pop(); // :-\
119120
debug!("Root is %s, calling compile_rest", root.to_str());
120-
util::compile_crate_from_input(self.input, Some(self.build_dir),
121-
sess, Some(crate), os::args()[0]);
122121
let exe = self.build_dir.push(~"pkg" + util::exe_suffix());
122+
util::compile_crate_from_input(self.input, self.id,
123+
Some(self.build_dir),
124+
sess, Some(crate),
125+
exe, os::args()[0],
126+
driver::cu_everything);
123127
debug!("Running program: %s %s %s", exe.to_str(), root.to_str(), what);
124128
let status = run::run_program(exe.to_str(), ~[root.to_str(), what]);
125129
if status != 0 {
@@ -199,15 +203,15 @@ impl Ctx {
199203
// relative to the CWD. In the future, we should search
200204
// paths
201205
let cwd = os::getcwd().normalize();
202-
debug!("Current working directory = %?", cwd);
206+
debug!("Current working directory = %s", cwd.to_str());
203207

204-
// Find crates inside the workspace
208+
// Create the package source
205209
let mut src = PkgSrc::new(&cwd, &dst_dir, &pkgid);
206210
debug!("Package src = %?", src);
207-
src.find_crates();
208211

209212
// Is there custom build logic? If so, use it
210213
let pkg_src_dir = cwd.push_rel(&pkgid.path);
214+
let mut custom = false;;
211215
debug!("Package source directory = %s", pkg_src_dir.to_str());
212216
let cfgs = match src.package_script_option(&pkg_src_dir) {
213217
Some(package_script_path) => {
@@ -221,6 +225,7 @@ impl Ctx {
221225
if hook_result != 0 {
222226
fail!(fmt!("Error running custom build command"))
223227
}
228+
custom = true;
224229
// otherwise, the package script succeeded
225230
cfgs
226231
}
@@ -229,6 +234,13 @@ impl Ctx {
229234
~[]
230235
}
231236
};
237+
238+
// Find crates inside the workspace
239+
if !custom {
240+
src.find_crates();
241+
}
242+
243+
// Build it!
232244
src.build(&dst_dir, cfgs);
233245
}
234246
~"clean" => {
@@ -728,7 +740,6 @@ condition! {
728740

729741
impl PkgSrc {
730742

731-
732743
fn new(src_dir: &Path, dst_dir: &Path,
733744
id: &PkgId) -> PkgSrc {
734745
PkgSrc {
@@ -765,12 +776,6 @@ impl PkgSrc {
765776
dir
766777
}
767778

768-
769-
fn has_pkg_file(&self) -> bool {
770-
let dir = self.check_dir();
771-
dir.push("pkg.rs").exists()
772-
}
773-
774779
// If a file named "pkg.rs" in the current directory exists,
775780
// return the path for it. Otherwise, None
776781
fn package_script_option(&self, cwd: &Path) -> Option<Path> {
@@ -786,14 +791,16 @@ impl PkgSrc {
786791
/// True if the given path's stem is self's pkg ID's stem
787792
/// or if the pkg ID's stem is <rust-foo> and the given path's
788793
/// stem is foo
794+
/// Requires that dashes in p have already been normalized to
795+
/// underscores
789796
fn stem_matches(&self, p: &Path) -> bool {
790-
let self_id = self.id.path.filestem();
797+
let self_id = normalize(~self.id.path).filestem();
791798
if self_id == p.filestem() {
792799
return true;
793800
}
794801
else {
795802
for self_id.each |pth| {
796-
if pth.starts_with("rust-")
803+
if pth.starts_with("rust_") // because p is already normalized
797804
&& match p.filestem() {
798805
Some(s) => str::eq_slice(s, pth.slice(5, pth.len())),
799806
None => false
@@ -814,17 +821,14 @@ impl PkgSrc {
814821
cs.push(Crate::new(&sub));
815822
}
816823

824+
/// Infers crates to build. Called only in the case where there
825+
/// is no custom build logic
817826
fn find_crates(&mut self) {
818827
use PkgSrc::push_crate;
819828

820829
let dir = self.check_dir();
821830
let prefix = dir.components.len();
822-
// This is ugly, but can go away once we get rid
823-
// of .rc files
824-
let mut saw_rs = false;
825-
let mut saw_rc = false;
826-
debug!("Matching against %?",
827-
self.id.path.filestem());
831+
debug!("Matching against %?", self.id.path.filestem());
828832
for os::walk_dir(&dir) |pth| {
829833
match pth.filename() {
830834
Some(~"lib.rs") => push_crate(&mut self.libs,
@@ -836,30 +840,10 @@ impl PkgSrc {
836840
Some(~"bench.rs") => push_crate(&mut self.benchs,
837841
prefix, pth),
838842
_ => {
839-
// If the file stem is the same as the
840-
// package ID, with an .rs or .rc extension,
841-
// consider it to be a crate
842-
let ext = pth.filetype();
843-
let matches = |p: &Path| {
844-
self.stem_matches(p) && (ext == Some(~".rc")
845-
|| ext == Some(~".rs"))
846-
};
847-
debug!("Checking %? which %s and ext = %? %? %?", pth.filestem(),
848-
if matches(pth) { "matches" } else { "does not match" },
849-
ext, saw_rs, saw_rc);
850-
if matches(pth) &&
851-
// Avoid pushing foo.rc *and* foo.rs
852-
!((ext == Some(~".rc") && saw_rs) ||
853-
(ext == Some(~".rs") && saw_rc)) {
854-
push_crate(&mut self.libs, // ????
855-
prefix, pth);
856-
if ext == Some(~".rc") {
857-
saw_rc = true;
858-
}
859-
else if ext == Some(~".rs") {
860-
saw_rs = true;
861-
}
862-
}
843+
util::note(~"Couldn't infer any crates to build.\n\
844+
Try naming a crate `main.rs`, `lib.rs`, \
845+
`test.rs`, or `bench.rs`.");
846+
fail!(~"Failed to infer crates to build");
863847
}
864848
}
865849
}
@@ -870,22 +854,22 @@ impl PkgSrc {
870854
self.benchs.len())
871855
}
872856

873-
fn build_crates(dst_dir: &Path,
857+
fn build_crates(&self, dst_dir: &Path,
874858
src_dir: &Path,
875859
crates: &[Crate],
876860
cfgs: ~[~str],
877-
test: bool) {
861+
test: bool, crate_type: crate_type) {
878862

879863
for crates.each |&crate| {
880864
let path = &src_dir.push_rel(&crate.file).normalize();
881865
util::note(fmt!("build_crates: compiling %s", path.to_str()));
882866
util::note(fmt!("build_crates: destination dir is %s", dst_dir.to_str()));
883867

884-
let result = util::compile_crate(None, path,
868+
let result = util::compile_crate(None, self.id, path,
885869
dst_dir,
886870
crate.flags,
887871
crate.cfgs + cfgs,
888-
false, test);
872+
false, test, crate_type);
889873
if !result {
890874
build_err::cond.raise(fmt!("build failure on %s",
891875
path.to_str()));
@@ -898,12 +882,12 @@ impl PkgSrc {
898882
fn build(&self, dst_dir: &Path, cfgs: ~[~str]) {
899883
let dir = self.check_dir();
900884
debug!("Building libs");
901-
PkgSrc::build_crates(dst_dir, &dir, self.libs, cfgs, false);
885+
self.build_crates(dst_dir, &dir, self.libs, cfgs, false, lib_crate);
902886
debug!("Building mains");
903-
PkgSrc::build_crates(dst_dir, &dir, self.mains, cfgs, false);
887+
self.build_crates(dst_dir, &dir, self.mains, cfgs, false, bin_crate);
904888
debug!("Building tests");
905-
PkgSrc::build_crates(dst_dir, &dir, self.tests, cfgs, true);
889+
self.build_crates(dst_dir, &dir, self.tests, cfgs, true, bin_crate);
906890
debug!("Building benches");
907-
PkgSrc::build_crates(dst_dir, &dir, self.benchs, cfgs, true);
891+
self.build_crates(dst_dir, &dir, self.benchs, cfgs, true, bin_crate);
908892
}
909893
}

0 commit comments

Comments
 (0)