Skip to content

Commit 85720c6

Browse files
committed
rustc/rusti/rustpkg: Infer packages from extern mod directives
This commit won't be quite as useful until I implement RUST_PATH and until we change `extern mod` to take a general string instead of an identifier (rust-lang#5682 and rust-lang#6407). With that said, now if you're using rustpkg and a program contains: extern mod foo; rustpkg will attempt to search for `foo`, so that you don't have to provide a -L directory explicitly. In addition, rustpkg will actually try to build and install `foo`, unless it's already installed (specifically, I tested that `extern mod extra;` would not cause it to try to find source for `extra` and compile it again). This is as per rust-lang#5681. Incidentally, I changed some driver code to infer the link name from the crate link_meta attributes. If that change isn't ok, say something.
1 parent cf8979c commit 85720c6

File tree

7 files changed

+29
-25
lines changed

7 files changed

+29
-25
lines changed

src/librustc/driver/driver.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,10 @@ pub fn compile_rest(sess: Session,
239239
let middle::resolve::CrateMap {
240240
def_map: def_map,
241241
exp_map2: exp_map2,
242-
trait_map: trait_map
243-
} =
244-
time(time_passes, ~"resolution", ||
245-
middle::resolve::resolve_crate(sess, lang_items, crate));
242+
trait_map: trait_map
243+
} =
244+
time(time_passes, ~"resolution", ||
245+
middle::resolve::resolve_crate(sess, lang_items, crate));
246246

247247
time(time_passes, ~"looking for entry point",
248248
|| middle::entry::find_entry_point(sess, crate, ast_map));

src/librustpkg/package_id.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ pub use package_path::{RemotePath, LocalPath, normalize, hash};
1212
use core::prelude::*;
1313
use version::{try_getting_version, Version, NoVersion, split_version};
1414

15+
/// Placeholder
16+
pub fn default_version() -> Version { ExactRevision(0.1) }
17+
1518
/// Path-fragment identifier of a package such as
1619
/// 'github.com/graydon/test'; path must be a relative
1720
/// path with >=1 component.
@@ -77,7 +80,7 @@ impl PkgId {
7780
}
7881
}
7982

80-
pub fn hash(&self) -> ~str {
83+
fn hash(&self) -> ~str {
8184
fmt!("%s-%s-%s", self.remote_path.to_str(),
8285
hash(self.remote_path.to_str() + self.version.to_str()),
8386
self.version.to_str())

src/librustpkg/package_path.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ pub fn write<W: Writer>(writer: &mut W, string: &str) {
4949
}
5050

5151
pub fn hash(data: ~str) -> ~str {
52-
let hasher = &mut hash::default_state();
53-
write(hasher, data);
52+
let mut hasher = hash::default_state();
53+
let buffer = str::as_bytes_slice(data);
54+
hasher.write(buffer);
5455
hasher.result_str()
5556
}

src/librustpkg/path_util.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
use core::prelude::*;
1414
pub use package_path::{RemotePath, LocalPath};
15-
pub use package_id::PkgId;
15+
pub use package_id::{PkgId, Version{;
1616
pub use target::{OutputType, Main, Lib, Test, Bench, Target, Build, Install};
1717
use core::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
1818
use core::os::mkdir_recursive;
@@ -116,20 +116,20 @@ fn output_in_workspace(pkgid: &PkgId, workspace: &Path, what: OutputType) -> Opt
116116
pub fn built_library_in_workspace(pkgid: &PkgId, workspace: &Path) -> Option<Path> {
117117
// passing in local_path here sounds fishy
118118
library_in_workspace(pkgid.local_path.to_str(), pkgid.short_name, Build,
119-
workspace, "build")
119+
Some(@copy pkgid.version), workspace, "build")
120120
}
121121

122122
/// Does the actual searching stuff
123123
pub fn installed_library_in_workspace(short_name: &str, workspace: &Path) -> Option<Path> {
124-
library_in_workspace(short_name, short_name, Install, workspace, "lib")
124+
library_in_workspace(short_name, short_name, Install, None, workspace, "lib")
125125
}
126126

127127

128128
/// This doesn't take a PkgId, so we can use it for `extern mod` inference, where we
129129
/// don't know the entire package ID.
130130
/// `full_name` is used to figure out the directory to search.
131131
/// `short_name` is taken as the link name of the library.
132-
fn library_in_workspace(full_name: &str, short_name: &str, where: Target,
132+
fn library_in_workspace(full_name: &str, short_name: &str, where: Target, version: Option<@Version>,
133133
workspace: &Path, prefix: &str) -> Option<Path> {
134134
debug!("library_in_workspace: checking whether a library named %s exists",
135135
short_name);
@@ -210,17 +210,11 @@ pub fn target_executable_in_workspace(pkgid: &PkgId, workspace: &Path) -> Path {
210210
}
211211

212212

213-
/// Returns the installed path for <built_library> in <workspace>
213+
/// Returns the executable that would be installed for <pkgid>
214+
/// in <workspace>
214215
/// As a side effect, creates the lib-dir if it doesn't exist
215-
pub fn target_library_in_workspace(workspace: &Path,
216-
built_library: &Path) -> Path {
217-
use conditions::bad_path::cond;
218-
let result = workspace.push("lib");
219-
if !os::path_exists(&result) && !mkdir_recursive(&result, u_rwx) {
220-
cond.raise((copy result, ~"I couldn't create the library directory"));
221-
}
222-
result.push(built_library.filename().expect(fmt!("I don't know how to treat %s as a library",
223-
built_library.to_str())))
216+
pub fn target_library_in_workspace(pkgid: &PkgId, workspace: &Path) -> Path {
217+
target_file_in_workspace(pkgid, workspace, Lib, Install)
224218
}
225219

226220
/// Returns the test executable that would be installed for <pkgid>

src/librustpkg/rustpkg.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ impl<'self> PkgScript<'self> {
149149
&self.build_dir,
150150
sess,
151151
crate,
152+
util::Main,
152153
driver::build_configuration(sess,
153154
binary, &self.input));
154155
debug!("Running program: %s %s %s", exe.to_str(), root.to_str(), what);

src/librustpkg/tests.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use core::prelude::*;
1818
use core::result;
1919
use extra::tempfile::mkdtemp;
2020
use package_path::*;
21-
use package_id::PkgId;
21+
use package_id::{PkgId, default_version};
2222
use package_source::*;
2323
use version::{ExactRevision, NoVersion, Version};
2424
use path_util::{target_executable_in_workspace, target_library_in_workspace,
@@ -82,6 +82,12 @@ fn mk_temp_workspace(short_name: &LocalPath, version: &Version) -> Path {
8282
debug!("Created %s and does it exist? %?", package_dir.to_str(),
8383
os::path_is_dir(&package_dir));
8484
// Create main, lib, test, and bench files
85+
let package_dir = workspace.push("src").push(fmt!("%s-0.1", short_name.to_str()));
86+
assert!(os::mkdir_recursive(&package_dir, u_rwx));
87+
debug!("Created %s and does it exist? %?", package_dir.to_str(),
88+
os::path_is_dir(&package_dir));
89+
// Create main, lib, test, and bench files
90+
8591
writeFile(&package_dir.push("main.rs"),
8692
"fn main() { let _x = (); }");
8793
writeFile(&package_dir.push("lib.rs"),
@@ -90,7 +96,7 @@ fn mk_temp_workspace(short_name: &LocalPath, version: &Version) -> Path {
9096
"#[test] pub fn f() { (); }");
9197
writeFile(&package_dir.push("bench.rs"),
9298
"#[bench] pub fn f() { (); }");
93-
package_dir.pop().pop()
99+
package_dir
94100
}
95101

96102
fn is_rwx(p: &Path) -> bool {

src/librustpkg/util.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ pub fn warn(msg: &str) {
186186
pub fn error(msg: &str) {
187187
pretty_message(msg, "error: ", term::color_red, io::stdout())
188188
}
189-
190189
// FIXME (#4432): Use workcache to only compile when needed
191190
pub fn compile_input(ctxt: &Ctx,
192191
pkg_id: &PkgId,
@@ -318,7 +317,7 @@ pub fn compile_crate_from_input(input: &driver::input,
318317
}
319318

320319
#[cfg(windows)]
321-
pub fn exe_suffix() -> ~str { ~".exe" }
320+
pub fn exe_suffix() -> ~str { ".exe" }
322321

323322
#[cfg(target_os = "linux")]
324323
#[cfg(target_os = "android")]

0 commit comments

Comments
 (0)