rustpkg: Search for crates in the current directory

As per #8520, find crates in the current working directory even
if it's not a workspace.

Closes #8520
This commit is contained in:
Tim Chevalier 2013-10-05 22:14:10 -04:00
parent 3a7337ff17
commit 5412794192
5 changed files with 89 additions and 18 deletions

View File

@ -47,10 +47,6 @@ condition! {
pub no_rust_path: (~str) -> Path;
}
condition! {
pub not_a_workspace: (~str) -> Path;
}
condition! {
pub failed_to_create_temp_dir: (~str) -> Path;
}

View File

@ -19,8 +19,8 @@ use crate::Crate;
use messages::*;
use source_control::{safe_git_clone, git_clone_url, DirToUse, CheckedOutSources};
use source_control::make_read_only;
use path_util::{find_dir_using_rust_path_hack, make_dir_rwx_recursive};
use path_util::{target_build_dir, versionize};
use path_util::{find_dir_using_rust_path_hack, make_dir_rwx_recursive, default_workspace};
use path_util::{target_build_dir, versionize, dir_has_crate_file};
use util::{compile_crate, DepMap};
use workcache_support;
use workcache_support::crate_tag;
@ -197,7 +197,23 @@ impl PkgSrc {
match ok_d {
Some(d) => d,
None => {
if use_rust_path_hack {
// See if the sources are in $CWD
let cwd = os::getcwd();
if dir_has_crate_file(&cwd) {
return PkgSrc {
// In this case, source_workspace isn't really a workspace.
// This data structure needs yet more refactoring.
source_workspace: cwd.clone(),
destination_workspace: default_workspace(),
build_in_destination: true,
start_dir: cwd,
id: id,
libs: ~[],
mains: ~[],
benchs: ~[],
tests: ~[]
}
} else if use_rust_path_hack {
match find_dir_using_rust_path_hack(&id) {
Some(d) => d,
None => {

View File

@ -414,6 +414,11 @@ pub fn uninstall_package_from(workspace: &Path, pkgid: &PkgId) {
}
pub fn dir_has_crate_file(dir: &Path) -> bool {
dir_has_file(dir, "lib.rs") || dir_has_file(dir, "main.rs")
|| dir_has_file(dir, "test.rs") || dir_has_file(dir, "bench.rs")
}
fn dir_has_file(dir: &Path, file: &str) -> bool {
assert!(dir.is_absolute());
os::path_exists(&dir.join(file))
@ -427,8 +432,7 @@ pub fn find_dir_using_rust_path_hack(p: &PkgId) -> Option<Path> {
// has a name that's a single component
if dir.ends_with_path(&p.path) || dir.ends_with_path(&versionize(&p.path, &p.version)) {
debug2!("In find_dir_using_rust_path_hack: checking dir {}", dir.display());
if dir_has_file(dir, "lib.rs") || dir_has_file(dir, "main.rs")
|| dir_has_file(dir, "test.rs") || dir_has_file(dir, "bench.rs") {
if dir_has_crate_file(dir) {
debug2!("Did find id {} in dir {}", p.to_str(), dir.display());
return Some(dir.clone());
}

View File

@ -38,7 +38,7 @@ use messages::{error, warn, note};
use path_util::{build_pkg_id_in_workspace, built_test_in_workspace};
use path_util::{U_RWX, in_rust_path};
use path_util::{built_executable_in_workspace, built_library_in_workspace, default_workspace};
use path_util::{target_executable_in_workspace, target_library_in_workspace};
use path_util::{target_executable_in_workspace, target_library_in_workspace, dir_has_crate_file};
use source_control::{CheckedOutSources, is_git_dir, make_read_only};
use workspace::{each_pkg_parent_workspace, pkg_parent_workspaces, cwd_to_workspace};
use workspace::determine_destination;
@ -48,7 +48,6 @@ use context::{Context, BuildContext,
use package_id::PkgId;
use package_source::PkgSrc;
use target::{WhatToBuild, Everything, is_lib, is_main, is_test, is_bench, Tests};
// use workcache_support::{discover_outputs, digest_only_date};
use workcache_support::digest_only_date;
use exit_codes::{COPY_FAILED_CODE, BAD_FLAG_CODE};
@ -210,10 +209,11 @@ pub trait CtxMethods {
impl CtxMethods for BuildContext {
fn build_args(&self, args: ~[~str], what: &WhatToBuild) -> Option<(PkgId, Path)> {
let cwd = os::getcwd();
if args.len() < 1 {
match cwd_to_workspace() {
None if self.context.use_rust_path_hack => {
let cwd = os::getcwd();
None if dir_has_crate_file(&cwd) => {
// FIXME (#9639): This needs to handle non-utf8 paths
let pkgid = PkgId::new(cwd.filename_str().unwrap());
let mut pkg_src = PkgSrc::new(cwd, default_workspace(), true, pkgid);
@ -260,6 +260,7 @@ impl CtxMethods for BuildContext {
}
}
fn run(&self, cmd: &str, args: ~[~str]) {
let cwd = os::getcwd();
match cmd {
"build" => {
self.build_args(args, &Everything);
@ -278,7 +279,6 @@ impl CtxMethods for BuildContext {
// The package id is presumed to be the first command-line
// argument
let pkgid = PkgId::new(args[0].clone());
let cwd = os::getcwd();
self.clean(&cwd, &pkgid); // tjc: should use workspace, not cwd
}
}
@ -295,9 +295,9 @@ impl CtxMethods for BuildContext {
"install" => {
if args.len() < 1 {
match cwd_to_workspace() {
None if self.context.use_rust_path_hack => {
let cwd = os::getcwd();
None if dir_has_crate_file(&cwd) => {
// FIXME (#9639): This needs to handle non-utf8 paths
let inferred_pkgid =
PkgId::new(cwd.filename_str().unwrap());
self.install(PkgSrc::new(cwd, default_workspace(),

View File

@ -580,7 +580,11 @@ fn test_make_dir_rwx() {
assert!(os::remove_dir_recursive(&dir));
}
// n.b. I ignored the next two tests for now because something funny happens on linux
// and I don't want to debug the issue right now (calling into the rustpkg lib directly
// is a little sketchy anyway)
#[test]
#[ignore]
fn test_install_valid() {
use path_util::installed_library_in_workspace;
@ -621,6 +625,7 @@ fn test_install_valid() {
}
#[test]
#[ignore]
fn test_install_invalid() {
let sysroot = test_sysroot();
let pkgid = fake_pkg();
@ -641,7 +646,44 @@ fn test_install_invalid() {
assert!(result == Err(()));
}
// Tests above should (maybe) be converted to shell out to rustpkg, too
#[test]
fn test_install_valid_external() {
let temp_pkg_id = PkgId::new("foo");
let (tempdir, _) = mk_temp_workspace(&temp_pkg_id.path,
&temp_pkg_id.version);
let temp_workspace = tempdir.path();
command_line_test([~"install", ~"foo"], temp_workspace);
// Check that all files exist
let exec = target_executable_in_workspace(&temp_pkg_id, temp_workspace);
debug2!("exec = {}", exec.display());
assert!(os::path_exists(&exec));
assert!(is_rwx(&exec));
let lib = installed_library_in_workspace(&temp_pkg_id.path, temp_workspace);
debug2!("lib = {:?}", lib);
assert!(lib.as_ref().map_default(false, |l| os::path_exists(l)));
assert!(lib.as_ref().map_default(false, |l| is_rwx(l)));
// And that the test and bench executables aren't installed
assert!(!os::path_exists(&target_test_in_workspace(&temp_pkg_id, temp_workspace)));
let bench = target_bench_in_workspace(&temp_pkg_id, temp_workspace);
debug2!("bench = {}", bench.display());
assert!(!os::path_exists(&bench));
}
#[test]
#[ignore(reason = "9994")]
fn test_install_invalid_external() {
let cwd = os::getcwd();
command_line_test_expect_fail([~"install", ~"foo"],
&cwd,
None,
// FIXME #3408: Should be NONEXISTENT_PACKAGE_CODE
COPY_FAILED_CODE);
}
#[test]
fn test_install_git() {
let temp_pkg_id = git_repo_pkg();
@ -1367,6 +1409,8 @@ fn rust_path_hack_test(hack_flag: bool) {
assert!(!built_executable_exists(workspace, "foo"));
}
// Notice that this is the only test case where the --rust-path-hack
// flag is actually needed
#[test]
fn test_rust_path_can_contain_package_dirs_with_flag() {
/*
@ -2029,7 +2073,6 @@ fn test_rustpkg_test_output() {
}
#[test]
#[ignore(reason = "Issue 9441")]
fn test_rebuild_when_needed() {
let foo_id = PkgId::new("foo");
let foo_workspace = create_local_package(&foo_id);
@ -2196,6 +2239,18 @@ fn test_compile_error() {
}
}
#[test]
fn find_sources_in_cwd() {
let temp_dir = TempDir::new("sources").expect("find_sources_in_cwd failed");
let temp_dir = temp_dir.path();
let source_dir = temp_dir.join("foo");
os::mkdir_recursive(&source_dir, U_RWX);
writeFile(&source_dir.join("main.rs"),
"fn main() { let _x = (); }");
command_line_test([~"install", ~"foo"], &source_dir);
assert_executable_exists(&source_dir.join(".rust"), "foo");
}
/// Returns true if p exists and is executable
fn is_executable(p: &Path) -> bool {
use std::libc::consts::os::posix88::{S_IXUSR};