Skip to content

Commit

Permalink
rustc/rusti/rustpkg: Infer packages from extern mod directives
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
catamorphism committed Jun 10, 2013
1 parent cf8979c commit 85720c6
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,10 @@ pub fn compile_rest(sess: Session,
let middle::resolve::CrateMap {
def_map: def_map,
exp_map2: exp_map2,
trait_map: trait_map
} =
time(time_passes, ~"resolution", ||
middle::resolve::resolve_crate(sess, lang_items, crate));
trait_map: trait_map
} =
time(time_passes, ~"resolution", ||
middle::resolve::resolve_crate(sess, lang_items, crate));

time(time_passes, ~"looking for entry point",
|| middle::entry::find_entry_point(sess, crate, ast_map));
Expand Down
5 changes: 4 additions & 1 deletion src/librustpkg/package_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub use package_path::{RemotePath, LocalPath, normalize, hash};
use core::prelude::*;
use version::{try_getting_version, Version, NoVersion, split_version};

/// Placeholder
pub fn default_version() -> Version { ExactRevision(0.1) }

/// Path-fragment identifier of a package such as
/// 'github.com/graydon/test'; path must be a relative
/// path with >=1 component.
Expand Down Expand Up @@ -77,7 +80,7 @@ impl PkgId {
}
}

pub fn hash(&self) -> ~str {
fn hash(&self) -> ~str {
fmt!("%s-%s-%s", self.remote_path.to_str(),
hash(self.remote_path.to_str() + self.version.to_str()),
self.version.to_str())
Expand Down
5 changes: 3 additions & 2 deletions src/librustpkg/package_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ pub fn write<W: Writer>(writer: &mut W, string: &str) {
}

pub fn hash(data: ~str) -> ~str {
let hasher = &mut hash::default_state();
write(hasher, data);
let mut hasher = hash::default_state();
let buffer = str::as_bytes_slice(data);
hasher.write(buffer);
hasher.result_str()
}
22 changes: 8 additions & 14 deletions src/librustpkg/path_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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

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


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


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

/// Returns the test executable that would be installed for <pkgid>
Expand Down
1 change: 1 addition & 0 deletions src/librustpkg/rustpkg.rc
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl<'self> PkgScript<'self> {
&self.build_dir,
sess,
crate,
util::Main,
driver::build_configuration(sess,
binary, &self.input));
debug!("Running program: %s %s %s", exe.to_str(), root.to_str(), what);
Expand Down
10 changes: 8 additions & 2 deletions src/librustpkg/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use core::prelude::*;
use core::result;
use extra::tempfile::mkdtemp;
use package_path::*;
use package_id::PkgId;
use package_id::{PkgId, default_version};
use package_source::*;
use version::{ExactRevision, NoVersion, Version};
use path_util::{target_executable_in_workspace, target_library_in_workspace,
Expand Down Expand Up @@ -82,6 +82,12 @@ fn mk_temp_workspace(short_name: &LocalPath, version: &Version) -> Path {
debug!("Created %s and does it exist? %?", package_dir.to_str(),
os::path_is_dir(&package_dir));
// Create main, lib, test, and bench files
let package_dir = workspace.push("src").push(fmt!("%s-0.1", short_name.to_str()));
assert!(os::mkdir_recursive(&package_dir, u_rwx));
debug!("Created %s and does it exist? %?", package_dir.to_str(),
os::path_is_dir(&package_dir));
// Create main, lib, test, and bench files

writeFile(&package_dir.push("main.rs"),
"fn main() { let _x = (); }");
writeFile(&package_dir.push("lib.rs"),
Expand All @@ -90,7 +96,7 @@ fn mk_temp_workspace(short_name: &LocalPath, version: &Version) -> Path {
"#[test] pub fn f() { (); }");
writeFile(&package_dir.push("bench.rs"),
"#[bench] pub fn f() { (); }");
package_dir.pop().pop()
package_dir
}

fn is_rwx(p: &Path) -> bool {
Expand Down
3 changes: 1 addition & 2 deletions src/librustpkg/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ pub fn warn(msg: &str) {
pub fn error(msg: &str) {
pretty_message(msg, "error: ", term::color_red, io::stdout())
}

// FIXME (#4432): Use workcache to only compile when needed
pub fn compile_input(ctxt: &Ctx,
pkg_id: &PkgId,
Expand Down Expand Up @@ -318,7 +317,7 @@ pub fn compile_crate_from_input(input: &driver::input,
}

#[cfg(windows)]
pub fn exe_suffix() -> ~str { ~".exe" }
pub fn exe_suffix() -> ~str { ".exe" }

#[cfg(target_os = "linux")]
#[cfg(target_os = "android")]
Expand Down

0 comments on commit 85720c6

Please sign in to comment.