Skip to content

Commit a8194ed

Browse files
committed
rustpkg: Address review comments from Jack
1 parent ed4859e commit a8194ed

File tree

8 files changed

+126
-164
lines changed

8 files changed

+126
-164
lines changed

src/librustpkg/api.rs

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@ use workcache_support::*;
1717

1818
use extra::arc::{Arc,RWArc};
1919
use extra::workcache;
20-
use extra::workcache::*;
21-
use std::os;
20+
use extra::workcache::{Database, Logger, FreshnessMap};
2221
use extra::treemap::TreeMap;
2322

2423
/// Convenience functions intended for calling from pkg.rs
2524
/// p is where to put the cache file for dependencies
26-
pub fn default_ctxt(p: Path) -> BuildCtx {
27-
new_default_ctx(new_workcache_cx(&p), p)
25+
pub fn default_context(p: Path) -> BuildContext {
26+
new_default_context(new_workcache_context(&p), p)
2827
}
2928

30-
pub fn new_default_ctx(c: Context, p: Path) -> BuildCtx {
31-
BuildCtx {
32-
cx: Ctx { use_rust_path_hack: false,
33-
sysroot_opt: p },
34-
workcache_cx: c
29+
pub fn new_default_context(c: workcache::Context, p: Path) -> BuildContext {
30+
BuildContext {
31+
context: Context {
32+
use_rust_path_hack: false,
33+
sysroot: p
34+
},
35+
workcache_context: c
3536
}
3637
}
3738

@@ -44,27 +45,27 @@ fn binary_is_fresh(path: &str, in_hash: &str) -> bool {
4445
}
4546

4647

47-
pub fn new_workcache_cx(p: &Path) -> Context {
48+
pub fn new_workcache_context(p: &Path) -> workcache::Context {
4849
let db_file = p.push("rustpkg_db.json"); // ??? probably wrong
4950
debug!("Workcache database file: %s", db_file.to_str());
5051
let db = RWArc::new(Database::new(db_file));
5152
let lg = RWArc::new(Logger::new());
5253
let cfg = Arc::new(TreeMap::new());
53-
let mut rslt: FreshnessMap = TreeMap::new();
54+
let mut freshness: FreshnessMap = TreeMap::new();
5455
// Set up freshness functions for every type of dependency rustpkg
5556
// knows about
56-
rslt.insert(~"file", file_is_fresh);
57-
rslt.insert(~"binary", binary_is_fresh);
58-
workcache::Context::new_with_freshness(db, lg, cfg, Arc::new(rslt))
57+
freshness.insert(~"file", file_is_fresh);
58+
freshness.insert(~"binary", binary_is_fresh);
59+
workcache::Context::new_with_freshness(db, lg, cfg, Arc::new(freshness))
5960
}
6061

6162
pub fn build_lib(sysroot: Path, root: Path, name: ~str, version: Version,
6263
lib: Path) {
63-
let cx = default_ctxt(sysroot);
64+
let cx = default_context(sysroot);
6465
let subroot = root.clone();
6566
let subversion = version.clone();
6667
let sublib = lib.clone();
67-
do cx.workcache_cx.with_prep(name) |prep| {
68+
do cx.workcache_context.with_prep(name) |prep| {
6869
let pkg_src = PkgSrc {
6970
workspace: subroot.clone(),
7071
start_dir: subroot.push("src").push(name),
@@ -78,17 +79,17 @@ pub fn build_lib(sysroot: Path, root: Path, name: ~str, version: Version,
7879
let subcx = cx.clone();
7980
let subsrc = pkg_src.clone();
8081
do prep.exec |exec| {
81-
subsrc.clone().build(exec, &subcx.clone(), ~[]);
82+
subsrc.build(exec, &subcx.clone(), ~[]);
8283
}
8384
};
8485
}
8586

8687
pub fn build_exe(sysroot: Path, root: Path, name: ~str, version: Version,
8788
main: Path) {
88-
let cx = default_ctxt(sysroot);
89+
let cx = default_context(sysroot);
8990
let subroot = root.clone();
9091
let submain = main.clone();
91-
do cx.workcache_cx.with_prep(name) |prep| {
92+
do cx.workcache_context.with_prep(name) |prep| {
9293
let pkg_src = PkgSrc {
9394
workspace: subroot.clone(),
9495
start_dir: subroot.push("src").push(name),
@@ -107,41 +108,8 @@ pub fn build_exe(sysroot: Path, root: Path, name: ~str, version: Version,
107108
}
108109
}
109110

110-
pub fn install_lib(sysroot: Path,
111-
workspace: Path,
112-
name: ~str,
113-
lib_path: Path,
114-
version: Version) {
115-
debug!("self_exe: %?", os::self_exe_path());
116-
debug!("sysroot = %s", sysroot.to_str());
117-
debug!("workspace = %s", workspace.to_str());
118-
// make a PkgSrc
119-
let pkg_id = PkgId{ version: version, ..PkgId::new(name)};
120-
let cx = default_ctxt(sysroot);
121-
let subpath = lib_path.clone();
122-
do cx.workcache_cx.with_prep(pkg_id.to_str()) |prep| {
123-
let pkg_src = PkgSrc {
124-
workspace: workspace.clone(),
125-
start_dir: subpath.push("src").push(name),
126-
id: pkg_id.clone(),
127-
libs: ~[mk_crate(subpath.clone())],
128-
mains: ~[],
129-
tests: ~[],
130-
benchs: ~[]
131-
};
132-
pkg_src.declare_inputs(prep);
133-
let subcx = cx.clone();
134-
let subpkg_src = pkg_src.clone();
135-
do prep.exec |exec| {
136-
subpkg_src.clone().build(exec, &subcx.clone(), ~[]);
137-
}
138-
}
139-
cx.install_no_build(&workspace, &pkg_id);
140-
}
141-
142-
pub fn install_exe(sysroot: Path, workspace: Path, name: ~str, version: Version) {
143-
let cx = default_ctxt(sysroot);
144-
debug!("install_exe calling with_prep");
111+
pub fn install_pkg(sysroot: Path, workspace: Path, name: ~str, version: Version) {
112+
let cx = default_context(sysroot);
145113
let pkgid = PkgId{ version: version, ..PkgId::new(name)};
146114
cx.install(PkgSrc::new(workspace, false, pkgid));
147115
}

src/librustpkg/context.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,62 +14,61 @@ use std::os;
1414
use extra::workcache;
1515

1616
#[deriving(Clone)]
17-
pub struct Ctx {
17+
pub struct Context {
1818
// If use_rust_path_hack is true, rustpkg searches for sources
1919
// in *package* directories that are in the RUST_PATH (for example,
2020
// FOO/src/bar-0.1 instead of FOO). The flag doesn't affect where
2121
// rustpkg stores build artifacts.
2222
use_rust_path_hack: bool,
2323
// The root directory containing the Rust standard libraries
24-
sysroot_opt: Path
24+
sysroot: Path
2525
}
2626

2727
#[deriving(Clone)]
28-
pub struct BuildCtx {
28+
pub struct BuildContext {
2929
// Context for workcache
30-
workcache_cx: workcache::Context,
30+
workcache_context: workcache::Context,
3131
// Everything else
32-
cx: Ctx
32+
context: Context
3333
}
3434

35-
impl BuildCtx {
36-
pub fn sysroot_opt(&self) -> Path {
37-
self.cx.sysroot_opt.clone()
35+
impl BuildContext {
36+
pub fn sysroot(&self) -> Path {
37+
self.context.sysroot.clone()
3838
}
3939

4040
pub fn sysroot_to_use(&self) -> Path {
41-
self.cx.sysroot_to_use()
41+
self.context.sysroot_to_use()
4242
}
4343
}
4444

45-
impl Ctx {
46-
pub fn sysroot_opt(&self) -> Path {
47-
self.sysroot_opt.clone()
45+
impl Context {
46+
pub fn sysroot(&self) -> Path {
47+
self.sysroot.clone()
4848
}
4949
}
5050

51-
impl Ctx {
51+
impl Context {
5252
/// Debugging
53-
pub fn sysroot_opt_str(&self) -> ~str {
54-
self.sysroot_opt.to_str()
53+
pub fn sysroot_str(&self) -> ~str {
54+
self.sysroot.to_str()
5555
}
5656

5757
// Hack so that rustpkg can run either out of a rustc target dir,
5858
// or the host dir
5959
pub fn sysroot_to_use(&self) -> Path {
60-
if !in_target(&self.sysroot_opt) {
61-
self.sysroot_opt.clone()
60+
if !in_target(&self.sysroot) {
61+
self.sysroot.clone()
62+
} else {
63+
self.sysroot.pop().pop().pop()
6264
}
63-
else {
64-
self.sysroot_opt.pop().pop().pop()
65-
}
66-
}
65+
}
6766
}
6867

6968
/// We assume that if ../../rustc exists, then we're running
7069
/// rustpkg from a Rust target directory. This is part of a
7170
/// kludgy hack used to adjust the sysroot.
72-
pub fn in_target(sysroot_opt: &Path) -> bool {
73-
debug!("Checking whether %s is in target", sysroot_opt.to_str());
74-
os::path_is_dir(&sysroot_opt.pop().pop().push("rustc"))
71+
pub fn in_target(sysroot: &Path) -> bool {
72+
debug!("Checking whether %s is in target", sysroot.to_str());
73+
os::path_is_dir(&sysroot.pop().pop().push("rustc"))
7574
}

src/librustpkg/package_source.rs

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ use extra::workcache;
2828
// This contains a list of files found in the source workspace.
2929
#[deriving(Clone)]
3030
pub struct PkgSrc {
31-
workspace: Path, // root of where the package source code lives
32-
start_dir: Path, // dir to start looking in for packages -- normally
33-
// this is workspace/src/id but it may be just workspace
31+
/// Root of where the package source code lives
32+
workspace: Path,
33+
// Directory to start looking in for packages -- normally
34+
// this is workspace/src/id but it may be just workspace
35+
start_dir: Path,
3436
id: PkgId,
3537
libs: ~[Crate],
3638
mains: ~[Crate],
@@ -61,8 +63,7 @@ impl PkgSrc {
6163
let mut to_try = ~[];
6264
if use_rust_path_hack {
6365
to_try.push(workspace.clone());
64-
}
65-
else {
66+
} else {
6667
let result = workspace.push("src").push_rel(&id.path.pop()).push(fmt!("%s-%s",
6768
id.short_name, id.version.to_str()));
6869
to_try.push(result);
@@ -251,7 +252,7 @@ impl PkgSrc {
251252
}
252253

253254
fn build_crates(&self,
254-
ctx: &BuildCtx,
255+
ctx: &BuildContext,
255256
exec: &mut workcache::Exec,
256257
destination_dir: &Path,
257258
crates: &[Crate],
@@ -263,23 +264,17 @@ impl PkgSrc {
263264
let path_str = path.to_str();
264265
let cfgs = crate.cfgs + cfgs;
265266

266-
let result = {
267+
let result =
267268
// compile_crate should return the path of the output artifact
268-
match compile_crate(ctx,
269-
exec,
270-
&self.id,
271-
&path,
272-
destination_dir,
273-
crate.flags,
274-
cfgs,
275-
false,
276-
what).map(|p| p.to_str()) {
277-
Some(p) => p,
278-
None => build_err::cond.raise(fmt!("build failure on %s",
279-
path_str))
280-
281-
}
282-
};
269+
compile_crate(ctx,
270+
exec,
271+
&self.id,
272+
&path,
273+
destination_dir,
274+
crate.flags,
275+
cfgs,
276+
false,
277+
what).to_str();
283278
debug!("Result of compiling %s was %s", path_str, result);
284279
}
285280
}
@@ -301,24 +296,25 @@ impl PkgSrc {
301296

302297
// It would be better if build returned a Path, but then Path would have to derive
303298
// Encodable.
304-
pub fn build(&self, exec: &mut workcache::Exec, ctx: &BuildCtx, cfgs: ~[~str]) -> ~str {
299+
pub fn build(&self,
300+
exec: &mut workcache::Exec,
301+
build_context: &BuildContext,
302+
cfgs: ~[~str]) -> ~str {
305303
use conditions::not_a_workspace::cond;
306304

307305
// Determine the destination workspace (which depends on whether
308306
// we're using the rust_path_hack)
309307
let destination_workspace = if is_workspace(&self.workspace) {
310308
debug!("%s is indeed a workspace", self.workspace.to_str());
311309
self.workspace.clone()
312-
}
313-
else {
310+
} else {
314311
// It would be nice to have only one place in the code that checks
315312
// for the use_rust_path_hack flag...
316-
if ctx.cx.use_rust_path_hack {
313+
if build_context.context.use_rust_path_hack {
317314
let rs = default_workspace();
318315
debug!("Using hack: %s", rs.to_str());
319316
rs
320-
}
321-
else {
317+
} else {
322318
cond.raise(fmt!("Package root %s is not a workspace; pass in --rust_path_hack \
323319
if you want to treat it as a package source",
324320
self.workspace.to_str()))
@@ -331,13 +327,13 @@ impl PkgSrc {
331327
let benchs = self.benchs.clone();
332328
debug!("Building libs in %s, destination = %s",
333329
destination_workspace.to_str(), destination_workspace.to_str());
334-
self.build_crates(ctx, exec, &destination_workspace, libs, cfgs, Lib);
330+
self.build_crates(build_context, exec, &destination_workspace, libs, cfgs, Lib);
335331
debug!("Building mains");
336-
self.build_crates(ctx, exec, &destination_workspace, mains, cfgs, Main);
332+
self.build_crates(build_context, exec, &destination_workspace, mains, cfgs, Main);
337333
debug!("Building tests");
338-
self.build_crates(ctx, exec, &destination_workspace, tests, cfgs, Test);
334+
self.build_crates(build_context, exec, &destination_workspace, tests, cfgs, Test);
339335
debug!("Building benches");
340-
self.build_crates(ctx, exec, &destination_workspace, benchs, cfgs, Bench);
336+
self.build_crates(build_context, exec, &destination_workspace, benchs, cfgs, Bench);
341337
destination_workspace.to_str()
342338
}
343339
}

src/librustpkg/path_util.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ pub fn workspace_contains_package_id_(pkgid: &PkgId, workspace: &Path,
8484

8585
if found.is_some() {
8686
debug!("Found %s in %s", pkgid.to_str(), workspace.to_str());
87-
}
88-
else {
87+
} else {
8988
debug!("Didn't find %s in %s", pkgid.to_str(), workspace.to_str());
9089
}
9190
found

0 commit comments

Comments
 (0)