Skip to content

Commit c42f121

Browse files
committed
rustpkg: Handle sysroot more correctly
In rustpkg, pass around sysroot; in rustpkg tests, set the sysroot manually so that tests can find libcore and such. With bonus metadata::filesearch refactoring to avoid copies.
1 parent 376a552 commit c42f121

File tree

9 files changed

+106
-52
lines changed

9 files changed

+106
-52
lines changed

src/librustc/back/rpath.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn get_rpath_flags(sess: session::Session, out_filename: &Path)
4040
// where rustrt is and we know every rust program needs it
4141
let libs = vec::append_one(libs, get_sysroot_absolute_rt_lib(sess));
4242

43-
let rpaths = get_rpaths(os, &sysroot, output, libs,
43+
let rpaths = get_rpaths(os, sysroot, output, libs,
4444
sess.opts.target_triple);
4545
rpaths_to_flags(rpaths)
4646
}

src/librustc/driver/driver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ pub fn build_session_options(binary: @~str,
603603
link::output_type_bitcode
604604
} else { link::output_type_exe };
605605
let sysroot_opt = getopts::opt_maybe_str(matches, ~"sysroot");
606-
let sysroot_opt = sysroot_opt.map(|m| Path(*m));
606+
let sysroot_opt = sysroot_opt.map(|m| @Path(*m));
607607
let target_opt = getopts::opt_maybe_str(matches, ~"target");
608608
let target_feature_opt = getopts::opt_maybe_str(matches, ~"target-feature");
609609
let save_temps = getopts::opt_present(matches, ~"save-temps");

src/librustc/driver/session.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub struct options {
125125
output_type: back::link::output_type,
126126
addl_lib_search_paths: ~[Path],
127127
linker_args: ~[~str],
128-
maybe_sysroot: Option<Path>,
128+
maybe_sysroot: Option<@Path>,
129129
target_triple: ~str,
130130
target_feature: ~str,
131131
// User-specified cfg meta items. The compiler itself will add additional

src/librustc/metadata/filesearch.rs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,49 @@ pub fn pick_file(file: Path, path: &Path) -> Option<Path> {
2020
}
2121

2222
pub trait FileSearch {
23-
fn sysroot(&self) -> Path;
24-
fn lib_search_paths(&self) -> ~[Path];
23+
fn sysroot(&self) -> @Path;
24+
fn for_each_lib_search_path(&self, f: &fn(&Path) -> bool);
2525
fn get_target_lib_path(&self) -> Path;
2626
fn get_target_lib_file_path(&self, file: &Path) -> Path;
2727
}
2828

29-
pub fn mk_filesearch(maybe_sysroot: &Option<Path>,
29+
pub fn mk_filesearch(maybe_sysroot: &Option<@Path>,
3030
target_triple: &str,
3131
addl_lib_search_paths: ~[Path])
3232
-> @FileSearch {
3333
struct FileSearchImpl {
34-
sysroot: Path,
34+
sysroot: @Path,
3535
addl_lib_search_paths: ~[Path],
3636
target_triple: ~str
3737
}
3838
impl FileSearch for FileSearchImpl {
39-
fn sysroot(&self) -> Path { /*bad*/copy self.sysroot }
40-
fn lib_search_paths(&self) -> ~[Path] {
41-
let mut paths = /*bad*/copy self.addl_lib_search_paths;
42-
43-
paths.push(
44-
make_target_lib_path(&self.sysroot,
45-
self.target_triple));
46-
match get_rustpkg_lib_path_nearest() {
47-
result::Ok(ref p) => paths.push((/*bad*/copy *p)),
48-
result::Err(_) => ()
39+
fn sysroot(&self) -> @Path { self.sysroot }
40+
fn for_each_lib_search_path(&self, f: &fn(&Path) -> bool) {
41+
debug!("filesearch: searching additional lib search paths");
42+
if !self.addl_lib_search_paths.each(f) {
43+
return;
4944
}
50-
match get_rustpkg_lib_path() {
51-
result::Ok(ref p) => paths.push((/*bad*/copy *p)),
52-
result::Err(_) => ()
45+
46+
debug!("filesearch: searching target lib path");
47+
if !f(&make_target_lib_path(self.sysroot,
48+
self.target_triple)) {
49+
return;
5350
}
54-
paths
51+
debug!("filesearch: searching rustpkg lib path nearest");
52+
if match get_rustpkg_lib_path_nearest() {
53+
result::Ok(ref p) => f(p),
54+
result::Err(_) => true
55+
} {
56+
return;
57+
}
58+
debug!("filesearch: searching rustpkg lib path");
59+
match get_rustpkg_lib_path() {
60+
result::Ok(ref p) => f(p),
61+
result::Err(_) => true
62+
}
5563
}
5664
fn get_target_lib_path(&self) -> Path {
57-
make_target_lib_path(&self.sysroot, self.target_triple)
65+
make_target_lib_path(self.sysroot, self.target_triple)
5866
}
5967
fn get_target_lib_file_path(&self, file: &Path) -> Path {
6068
self.get_target_lib_path().push_rel(file)
@@ -72,7 +80,7 @@ pub fn mk_filesearch(maybe_sysroot: &Option<Path>,
7280

7381
pub fn search<T:Copy>(filesearch: @FileSearch, pick: pick<T>) -> Option<T> {
7482
let mut rslt = None;
75-
for filesearch.lib_search_paths().each |lib_search_path| {
83+
for filesearch.for_each_lib_search_path() |lib_search_path| {
7684
debug!("searching %s", lib_search_path.to_str());
7785
for os::list_dir_path(lib_search_path).each |path| {
7886
debug!("testing %s", path.to_str());
@@ -108,10 +116,10 @@ fn get_or_default_sysroot() -> Path {
108116
}
109117
}
110118
111-
fn get_sysroot(maybe_sysroot: &Option<Path>) -> Path {
119+
fn get_sysroot(maybe_sysroot: &Option<@Path>) -> @Path {
112120
match *maybe_sysroot {
113-
option::Some(ref sr) => (/*bad*/copy *sr),
114-
option::None => get_or_default_sysroot()
121+
option::Some(sr) => sr,
122+
option::None => @get_or_default_sysroot()
115123
}
116124
}
117125

src/librustpkg/context.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use core::hashmap::HashMap;
1414

1515
pub struct Ctx {
16+
// Sysroot -- if this is None, uses rustc filesearch's
17+
// idea of the default
18+
sysroot_opt: Option<@Path>,
1619
// I'm not sure what this is for
1720
json: bool,
1821
// Cache of hashes of things already installed

src/librustpkg/path_util.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ pub static u_rwx: i32 = (S_IRUSR | S_IWUSR | S_IXUSR) as i32;
2929
/// Creates a directory that is readable, writeable,
3030
/// and executable by the user. Returns true iff creation
3131
/// succeeded.
32-
pub fn make_dir_rwx(p: &Path) -> bool {
33-
use core::libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
34-
35-
os::make_dir(p, u_rwx)
36-
}
32+
pub fn make_dir_rwx(p: &Path) -> bool { os::make_dir(p, u_rwx) }
3733

3834
/// Replace all occurrences of '-' in the stem part of path with '_'
3935
/// This is because we treat rust-foo-bar-quux and rust_foo_bar_quux

src/librustpkg/rustpkg.rc

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl Ctx {
306306
// Find crates inside the workspace
307307
src.find_crates();
308308
// Build it!
309-
src.build(&build_dir, cfgs);
309+
src.build(&build_dir, cfgs, self.sysroot_opt);
310310
}
311311

312312
}
@@ -506,6 +506,7 @@ pub fn main() {
506506
}
507507

508508
Ctx {
509+
sysroot_opt: None, // Currently, only tests override this
509510
json: json,
510511
dep_cache: @mut HashMap::new()
511512
}.run(cmd, args);
@@ -648,6 +649,8 @@ impl PkgSrc {
648649

649650
debug!("Checking dir: %s", dir.to_str());
650651

652+
// tjc: Rather than erroring out, need to try downloading the
653+
// contents of the path to a local directory (#5679)
651654
if !os::path_exists(&dir) {
652655
cond.raise((self.id, ~"missing package dir"));
653656
}
@@ -744,18 +747,20 @@ impl PkgSrc {
744747
self.benchs.len())
745748
}
746749

747-
fn build_crates(&self, dst_dir: &Path,
748-
src_dir: &Path,
749-
crates: &[Crate],
750-
cfgs: ~[~str],
751-
test: bool, crate_type: crate_type) {
750+
fn build_crates(&self,
751+
maybe_sysroot: Option<@Path>,
752+
dst_dir: &Path,
753+
src_dir: &Path,
754+
crates: &[Crate],
755+
cfgs: ~[~str],
756+
test: bool, crate_type: crate_type) {
752757

753758
for crates.each |&crate| {
754759
let path = &src_dir.push_rel(&crate.file).normalize();
755760
util::note(fmt!("build_crates: compiling %s", path.to_str()));
756761
util::note(fmt!("build_crates: destination dir is %s", dst_dir.to_str()));
757762

758-
let result = util::compile_crate(None, self.id, path,
763+
let result = util::compile_crate(maybe_sysroot, self.id, path,
759764
dst_dir,
760765
crate.flags,
761766
crate.cfgs + cfgs,
@@ -769,15 +774,15 @@ impl PkgSrc {
769774
}
770775
}
771776

772-
fn build(&self, dst_dir: &Path, cfgs: ~[~str]) {
777+
fn build(&self, dst_dir: &Path, cfgs: ~[~str], maybe_sysroot: Option<@Path>) {
773778
let dir = self.check_dir();
774779
debug!("Building libs");
775-
self.build_crates(dst_dir, &dir, self.libs, cfgs, false, lib_crate);
780+
self.build_crates(maybe_sysroot, dst_dir, &dir, self.libs, cfgs, false, lib_crate);
776781
debug!("Building mains");
777-
self.build_crates(dst_dir, &dir, self.mains, cfgs, false, bin_crate);
782+
self.build_crates(maybe_sysroot, dst_dir, &dir, self.mains, cfgs, false, bin_crate);
778783
debug!("Building tests");
779-
self.build_crates(dst_dir, &dir, self.tests, cfgs, true, bin_crate);
784+
self.build_crates(maybe_sysroot, dst_dir, &dir, self.tests, cfgs, true, bin_crate);
780785
debug!("Building benches");
781-
self.build_crates(dst_dir, &dir, self.benchs, cfgs, true, bin_crate);
786+
self.build_crates(maybe_sysroot, dst_dir, &dir, self.benchs, cfgs, true, bin_crate);
782787
}
783788
}

src/librustpkg/tests.rs

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ use path_util::{target_executable_in_workspace, target_library_in_workspace,
2020
make_dir_rwx, u_rwx};
2121
use core::os::mkdir_recursive;
2222

23-
fn fake_ctxt() -> Ctx {
23+
fn fake_ctxt(sysroot_opt: Option<@Path>) -> Ctx {
2424
Ctx {
25+
sysroot_opt: sysroot_opt,
2526
json: false,
2627
dep_cache: @mut HashMap::new()
2728
}
@@ -34,6 +35,13 @@ fn fake_pkg() -> PkgId {
3435
}
3536
}
3637

38+
fn remote_pkg() -> PkgId {
39+
PkgId {
40+
path: Path(~"github.com/catamorphism/test-pkg"),
41+
version: default_version()
42+
}
43+
}
44+
3745
fn writeFile(file_path: &Path, contents: ~str) {
3846
let out: @io::Writer =
3947
result::get(&io::file_writer(file_path,
@@ -69,6 +77,15 @@ fn is_rwx(p: &Path) -> bool {
6977
}
7078
}
7179

80+
#[cfg(test)]
81+
fn test_sysroot() -> Path {
82+
// Totally gross hack but it's just for test cases.
83+
// Infer the sysroot from the exe name and tack "stage2"
84+
// onto it. (Did I mention it was a gross hack?)
85+
let self_path = os::self_exe_path().expect("Couldn't get self_exe path");
86+
self_path.pop().push("stage2")
87+
}
88+
7289
#[test]
7390
fn test_make_dir_rwx() {
7491
let temp = &os::tmpdir();
@@ -84,11 +101,9 @@ fn test_make_dir_rwx() {
84101

85102
#[test]
86103
fn test_install_valid() {
87-
use rustc::metadata::filesearch;
88-
89-
let sysroot = filesearch::get_rustpkg_sysroot();
90-
debug!("sysroot = %s", sysroot.get().to_str());
91-
let ctxt = fake_ctxt();
104+
let sysroot = test_sysroot();
105+
debug!("sysroot = %s", sysroot.to_str());
106+
let ctxt = fake_ctxt(Some(@sysroot));
92107
let temp_pkg_id = fake_pkg();
93108
let temp_workspace = mk_temp_workspace(&temp_pkg_id.path);
94109
// should have test, bench, lib, and main
@@ -114,7 +129,7 @@ fn test_install_invalid() {
114129
use conditions::nonexistent_package::cond;
115130
use cond1 = conditions::missing_pkg_files::cond;
116131

117-
let ctxt = fake_ctxt();
132+
let ctxt = fake_ctxt(None);
118133
let pkgid = fake_pkg();
119134
let temp_workspace = mkdtemp(&os::tmpdir(), "test").expect("couldn't create temp dir");
120135
let mut error_occurred = false;
@@ -130,3 +145,29 @@ fn test_install_invalid() {
130145
}
131146
assert!(error_occurred && error1_occurred);
132147
}
148+
149+
#[test]
150+
#[ignore(reason = "install from URL-fragment not yet implemented")]
151+
fn test_install_url() {
152+
let sysroot = test_sysroot();
153+
debug!("sysroot = %s", sysroot.to_str());
154+
let ctxt = fake_ctxt(Some(@sysroot));
155+
let temp_pkg_id = remote_pkg();
156+
let temp_workspace = mk_temp_workspace(&temp_pkg_id.path);
157+
// should have test, bench, lib, and main
158+
ctxt.install(&temp_workspace, temp_pkg_id);
159+
// Check that all files exist
160+
let exec = target_executable_in_workspace(temp_pkg_id, &temp_workspace);
161+
debug!("exec = %s", exec.to_str());
162+
assert!(os::path_exists(&exec));
163+
assert!(is_rwx(&exec));
164+
let lib = target_library_in_workspace(temp_pkg_id, &temp_workspace);
165+
debug!("lib = %s", lib.to_str());
166+
assert!(os::path_exists(&lib));
167+
assert!(is_rwx(&lib));
168+
// And that the test and bench executables aren't installed
169+
assert!(!os::path_exists(&target_test_in_workspace(temp_pkg_id, &temp_workspace)));
170+
let bench = target_bench_in_workspace(temp_pkg_id, &temp_workspace);
171+
debug!("bench = %s", bench.to_str());
172+
assert!(!os::path_exists(&bench));
173+
}

src/librustpkg/util.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ pub fn add_pkg(pkg: &Pkg) -> bool {
435435
}
436436
437437
// FIXME (#4432): Use workcache to only compile when needed
438-
pub fn compile_input(sysroot: Option<Path>,
438+
pub fn compile_input(sysroot: Option<@Path>,
439439
pkg_id: PkgId,
440440
in_file: &Path,
441441
out_dir: &Path,
@@ -474,6 +474,7 @@ pub fn compile_input(sysroot: Option<Path>,
474474
out_file.to_str());
475475
debug!("flags: %s", str::connect(flags, ~" "));
476476
debug!("cfgs: %s", str::connect(cfgs, ~" "));
477+
debug!("compile_input's sysroot = %?", sysroot);
477478
478479
let matches = getopts(~[~"-Z", ~"time-passes"]
479480
+ if building_library { ~[~"--lib"] }
@@ -587,7 +588,7 @@ fn add_attrs(c: ast::crate, new_attrs: ~[attribute]) -> @ast::crate {
587588
588589
// Called by build_crates
589590
// FIXME (#4432): Use workcache to only compile when needed
590-
pub fn compile_crate(sysroot: Option<Path>, pkg_id: PkgId,
591+
pub fn compile_crate(sysroot: Option<@Path>, pkg_id: PkgId,
591592
crate: &Path, dir: &Path,
592593
flags: ~[~str], cfgs: ~[~str], opt: bool,
593594
test: bool, crate_type: crate_type) -> bool {

0 commit comments

Comments
 (0)