Skip to content

Commit 37fd8f0

Browse files
committed
rustpkg: Simplify the PkgId struct
Get rid of special cases for names beginning with "rust-" or containing hyphens, and just store a Path in a package ID. The Rust-identifier for the crate is none of rustpkg's business.
1 parent 96fd606 commit 37fd8f0

File tree

10 files changed

+99
-189
lines changed

10 files changed

+99
-189
lines changed

mk/tests.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,8 @@ $(3)/stage$(1)/test/rustpkgtest-$(2)$$(X_$(2)): \
349349
$$(SREQ$(1)_T_$(2)_H_$(3)) \
350350
$$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_LIBSYNTAX_$(2)) \
351351
$$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_LIBRUSTC_$(2)) \
352-
$$(TBIN$(1)_T_$(2)_H_$(3))/rustpkg$$(X_$(2))
352+
$$(TBIN$(1)_T_$(2)_H_$(3))/rustpkg$$(X_$(2)) \
353+
$$(TBIN$(1)_T_$(2)_H_$(3))/rustc$$(X_$(2))
353354
@$$(call E, compile_and_link: $$@)
354355
$$(STAGE$(1)_T_$(2)_H_$(3)) -o $$@ $$< --test
355356

src/librustpkg/package_id.rs

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,44 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
pub use package_path::{RemotePath, LocalPath, normalize, hash};
1211
use version::{try_getting_version, try_getting_local_version,
1312
Version, NoVersion, split_version};
13+
use std::rt::io::Writer;
14+
use std::hash::Streaming;
15+
use std::hash;
1416

1517
/// Path-fragment identifier of a package such as
1618
/// 'github.com/graydon/test'; path must be a relative
1719
/// path with >=1 component.
1820
#[deriving(Clone)]
1921
pub struct PkgId {
20-
/// Remote path: for example, github.com/mozilla/quux-whatever
21-
remote_path: RemotePath,
22-
/// Local path: for example, /home/quux/github.com/mozilla/quux_whatever
23-
/// Note that '-' normalizes to '_' when mapping a remote path
24-
/// onto a local path
25-
/// Also, this will change when we implement #6407, though we'll still
26-
/// need to keep track of separate local and remote paths
27-
local_path: LocalPath,
28-
/// Short name. This is the local path's filestem, but we store it
22+
/// This is a path, on the local filesystem, referring to where the
23+
/// files for this package live. For example:
24+
/// github.com/mozilla/quux-whatever (it's assumed that if we're
25+
/// working with a package ID of this form, rustpkg has already cloned
26+
/// the sources into a local directory in the RUST_PATH).
27+
path: Path,
28+
/// Short name. This is the path's filestem, but we store it
2929
/// redundantly so as to not call get() everywhere (filestem() returns an
3030
/// option)
31+
/// The short name does not need to be a valid Rust identifier.
32+
/// Users can write: `extern mod foo = "...";` to get around the issue
33+
/// of package IDs whose short names aren't valid Rust identifiers.
3134
short_name: ~str,
35+
/// The requested package version.
3236
version: Version
3337
}
3438

3539
impl Eq for PkgId {
3640
fn eq(&self, p: &PkgId) -> bool {
37-
*p.local_path == *self.local_path && p.version == self.version
41+
p.path == self.path && p.version == self.version
3842
}
3943
fn ne(&self, p: &PkgId) -> bool {
4044
!(self.eq(p))
4145
}
4246
}
4347

4448
impl PkgId {
45-
// The PkgId constructor takes a Path argument so as
46-
// to be able to infer the version if the path refers
47-
// to a local git repository
4849
pub fn new(s: &str) -> PkgId {
4950
use conditions::bad_pkg_id::cond;
5051

@@ -63,40 +64,37 @@ impl PkgId {
6364
}
6465
};
6566

66-
let p = Path(s);
67-
if p.is_absolute {
68-
return cond.raise((p, ~"absolute pkgid"));
67+
let path = Path(s);
68+
if path.is_absolute {
69+
return cond.raise((path, ~"absolute pkgid"));
6970
}
70-
if p.components.len() < 1 {
71-
return cond.raise((p, ~"0-length pkgid"));
71+
if path.components.len() < 1 {
72+
return cond.raise((path, ~"0-length pkgid"));
7273
}
73-
let remote_path = RemotePath(p);
74-
let local_path = normalize(remote_path.clone());
75-
let short_name = local_path.clone().filestem().expect(fmt!("Strange path! %s", s));
74+
let short_name = path.clone().filestem().expect(fmt!("Strange path! %s", s));
7675

7776
let version = match given_version {
7877
Some(v) => v,
79-
None => match try_getting_local_version(&*local_path) {
78+
None => match try_getting_local_version(&path) {
8079
Some(v) => v,
81-
None => match try_getting_version(&remote_path) {
80+
None => match try_getting_version(&path) {
8281
Some(v) => v,
8382
None => NoVersion
8483
}
8584
}
8685
};
8786

88-
debug!("local_path = %s, remote_path = %s", local_path.to_str(), remote_path.to_str());
87+
debug!("path = %s", path.to_str());
8988
PkgId {
90-
local_path: local_path,
91-
remote_path: remote_path,
89+
path: path,
9290
short_name: short_name,
9391
version: version
9492
}
9593
}
9694

9795
pub fn hash(&self) -> ~str {
98-
fmt!("%s-%s-%s", self.remote_path.to_str(),
99-
hash(self.remote_path.to_str() + self.version.to_str()),
96+
fmt!("%s-%s-%s", self.path.to_str(),
97+
hash(self.path.to_str() + self.version.to_str()),
10098
self.version.to_str())
10199
}
102100

@@ -106,13 +104,24 @@ impl PkgId {
106104

107105
/// True if the ID has multiple components
108106
pub fn is_complex(&self) -> bool {
109-
self.short_name != self.local_path.to_str()
107+
self.short_name != self.path.to_str()
110108
}
111109
}
112110

113111
impl ToStr for PkgId {
114112
fn to_str(&self) -> ~str {
115113
// should probably use the filestem and not the whole path
116-
fmt!("%s-%s", self.local_path.to_str(), self.version.to_str())
114+
fmt!("%s-%s", self.path.to_str(), self.version.to_str())
117115
}
118116
}
117+
118+
119+
pub fn write<W: Writer>(writer: &mut W, string: &str) {
120+
writer.write(string.as_bytes());
121+
}
122+
123+
pub fn hash(data: ~str) -> ~str {
124+
let hasher = &mut hash::default_state();
125+
write(hasher, data);
126+
hasher.result_str()
127+
}

src/librustpkg/package_path.rs

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/librustpkg/package_source.rs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use target::*;
1212
use package_id::PkgId;
1313
use std::path::Path;
14-
use std::{os, str};
14+
use std::os;
1515
use context::*;
1616
use crate::Crate;
1717
use messages::*;
@@ -51,8 +51,7 @@ impl PkgSrc {
5151
fn check_dir(&self) -> Path {
5252
use conditions::nonexistent_package::cond;
5353

54-
debug!("Pushing onto root: %s | %s", self.id.remote_path.to_str(),
55-
self.root.to_str());
54+
debug!("Pushing onto root: %s | %s", self.id.path.to_str(), self.root.to_str());
5655
let dir;
5756
let dirs = pkgid_src_in_workspace(&self.id, &self.root);
5857
debug!("Checking dirs: %?", dirs);
@@ -86,18 +85,18 @@ impl PkgSrc {
8685
os::remove_dir_recursive(&local);
8786

8887
debug!("Checking whether %s exists locally. Cwd = %s, does it? %?",
89-
self.id.local_path.to_str(),
88+
self.id.path.to_str(),
9089
os::getcwd().to_str(),
91-
os::path_exists(&*self.id.local_path));
90+
os::path_exists(&self.id.path));
9291

93-
if os::path_exists(&*self.id.local_path) {
92+
if os::path_exists(&self.id.path) {
9493
debug!("%s exists locally! Cloning it into %s",
95-
self.id.local_path.to_str(), local.to_str());
96-
git_clone(&*self.id.local_path, &local, &self.id.version);
94+
self.id.path.to_str(), local.to_str());
95+
git_clone(&self.id.path, &local, &self.id.version);
9796
return Some(local);
9897
}
9998

100-
let url = fmt!("https://%s", self.id.remote_path.to_str());
99+
let url = fmt!("https://%s", self.id.path.to_str());
101100
note(fmt!("Fetching package: git clone %s %s [version=%s]",
102101
url, local.to_str(), self.id.version.to_str()));
103102
if git_clone_general(url, &local, &self.id.version) {
@@ -122,25 +121,8 @@ impl PkgSrc {
122121
}
123122

124123
/// True if the given path's stem is self's pkg ID's stem
125-
/// or if the pkg ID's stem is <rust-foo> and the given path's
126-
/// stem is foo
127-
/// Requires that dashes in p have already been normalized to
128-
/// underscores
129124
fn stem_matches(&self, p: &Path) -> bool {
130-
let self_id = self.id.local_path.filestem();
131-
if self_id == p.filestem() {
132-
return true;
133-
}
134-
else {
135-
for pth in self_id.iter() {
136-
if pth.starts_with("rust_") // because p is already normalized
137-
&& match p.filestem() {
138-
Some(s) => str::eq_slice(s, pth.slice(5, pth.len())),
139-
None => false
140-
} { return true; }
141-
}
142-
}
143-
false
125+
p.filestem().map_default(false, |p| { p == &self.id.short_name })
144126
}
145127

146128
fn push_crate(cs: &mut ~[Crate], prefix: uint, p: &Path) {
@@ -161,7 +143,7 @@ impl PkgSrc {
161143
let dir = self.check_dir();
162144
debug!("Called check_dir, I'm in %s", dir.to_str());
163145
let prefix = dir.components.len();
164-
debug!("Matching against %?", self.id.local_path.filestem());
146+
debug!("Matching against %?", self.id.short_name);
165147
do os::walk_dir(&dir) |pth| {
166148
match pth.filename() {
167149
Some(~"lib.rs") => PkgSrc::push_crate(&mut self.libs,

src/librustpkg/path_util.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// rustpkg utilities having to do with paths and directories
1212

13-
pub use package_path::{RemotePath, LocalPath, normalize};
1413
pub use package_id::PkgId;
1514
pub use target::{OutputType, Main, Lib, Test, Bench, Target, Build, Install};
1615
pub use version::{Version, NoVersion, split_version_general};
@@ -94,9 +93,9 @@ pub fn workspace_contains_package_id(pkgid: &PkgId, workspace: &Path) -> bool {
9493
pub fn pkgid_src_in_workspace(pkgid: &PkgId, workspace: &Path) -> ~[Path] {
9594
let mut results = ~[];
9695
let result = workspace.push("src").push(fmt!("%s-%s",
97-
pkgid.local_path.to_str(), pkgid.version.to_str()));
96+
pkgid.path.to_str(), pkgid.version.to_str()));
9897
results.push(result);
99-
results.push(workspace.push("src").push_rel(&*pkgid.remote_path));
98+
results.push(workspace.push("src").push_rel(&pkgid.path));
10099
results
101100
}
102101

@@ -159,22 +158,20 @@ fn output_in_workspace(pkgid: &PkgId, workspace: &Path, what: OutputType) -> Opt
159158
/// Figure out what the library name for <pkgid> in <workspace>'s build
160159
/// directory is, and if the file exists, return it.
161160
pub fn built_library_in_workspace(pkgid: &PkgId, workspace: &Path) -> Option<Path> {
162-
library_in_workspace(&pkgid.local_path, pkgid.short_name,
163-
Build, workspace, "build")
161+
library_in_workspace(&pkgid.path, pkgid.short_name, Build, workspace, "build")
164162
}
165163

166164
/// Does the actual searching stuff
167165
pub fn installed_library_in_workspace(short_name: &str, workspace: &Path) -> Option<Path> {
168-
library_in_workspace(&normalize(RemotePath(Path(short_name))),
169-
short_name, Install, workspace, "lib")
166+
library_in_workspace(&Path(short_name), short_name, Install, workspace, "lib")
170167
}
171168

172169

173170
/// This doesn't take a PkgId, so we can use it for `extern mod` inference, where we
174171
/// don't know the entire package ID.
175172
/// `workspace` is used to figure out the directory to search.
176173
/// `short_name` is taken as the link name of the library.
177-
pub fn library_in_workspace(path: &LocalPath, short_name: &str, where: Target,
174+
pub fn library_in_workspace(path: &Path, short_name: &str, where: Target,
178175
workspace: &Path, prefix: &str) -> Option<Path> {
179176
debug!("library_in_workspace: checking whether a library named %s exists",
180177
short_name);
@@ -186,7 +183,7 @@ pub fn library_in_workspace(path: &LocalPath, short_name: &str, where: Target,
186183
prefix = %s", short_name, where, workspace.to_str(), prefix);
187184

188185
let dir_to_search = match where {
189-
Build => workspace.push(prefix).push_rel(&**path),
186+
Build => workspace.push(prefix).push_rel(path),
190187
Install => workspace.push(prefix)
191188
};
192189
debug!("Listing directory %s", dir_to_search.to_str());
@@ -302,7 +299,7 @@ fn target_file_in_workspace(pkgid: &PkgId, workspace: &Path,
302299
// Artifacts in the build directory live in a package-ID-specific subdirectory,
303300
// but installed ones don't.
304301
let result = match where {
305-
Build => workspace.push(subdir).push_rel(&*pkgid.local_path),
302+
Build => workspace.push(subdir).push_rel(&pkgid.path),
306303
_ => workspace.push(subdir)
307304
};
308305
if !os::path_exists(&result) && !mkdir_recursive(&result, U_RWX) {
@@ -321,7 +318,7 @@ pub fn build_pkg_id_in_workspace(pkgid: &PkgId, workspace: &Path) -> Path {
321318
let mut result = workspace.push("build");
322319
// n.b. Should actually use a target-specific
323320
// subdirectory of build/
324-
result = result.push_rel(&*pkgid.local_path);
321+
result = result.push_rel(&pkgid.path);
325322
if os::path_exists(&result) || os::mkdir_recursive(&result, U_RWX) {
326323
result
327324
}
@@ -342,7 +339,7 @@ pub fn mk_output_path(what: OutputType, where: Target,
342339
// If we're installing, it just goes under <workspace>...
343340
Install => workspace,
344341
// and if we're just building, it goes in a package-specific subdir
345-
Build => workspace.push_rel(&*pkg_id.local_path)
342+
Build => workspace.push_rel(&pkg_id.path)
346343
};
347344
debug!("[%?:%?] mk_output_path: short_name = %s, path = %s", what, where,
348345
if what == Lib { short_name_with_version.clone() } else { pkg_id.short_name.clone() },

0 commit comments

Comments
 (0)