Skip to content

Commit a70b45a

Browse files
z0w0graydon
authored andcommitted
---
yaml --- r: 47020 b: refs/heads/try c: e34e072 h: refs/heads/master v: v3
1 parent bde2fb4 commit a70b45a

File tree

5 files changed

+120
-26
lines changed

5 files changed

+120
-26
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 3bbcac322669cff3abde5be937cc4ec3860f3985
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a6d9689399d091c3265f00434a69c551a61c28dc
5-
refs/heads/try: 707944184304acbd81c026156a04b98570c6ef9c
5+
refs/heads/try: e34e072d1777ad2064ed9b70ccdbe29b4f100f9b
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/librustpkg/api.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,29 @@ pub struct Crate {
1919
}
2020

2121
pub struct Listener {
22-
cmd: ~str,
22+
cmds: ~[~str],
2323
cb: fn~()
2424
}
2525

2626
pub fn run(listeners: ~[Listener]) {
27-
io::println(src_dir().to_str());
28-
io::println(work_dir().to_str());
29-
30-
let cmd = os::args()[1];
27+
let rcmd = os::args()[2];
28+
let mut found = false;
3129

3230
for listeners.each |listener| {
33-
if listener.cmd == cmd {
34-
(listener.cb)();
31+
for listener.cmds.each |&cmd| {
32+
if cmd == rcmd {
33+
(listener.cb)();
34+
35+
found = true;
36+
37+
break;
38+
}
3539
}
3640
}
41+
42+
if !found {
43+
os::set_exit_status(1);
44+
}
3745
}
3846

3947
pub impl Crate {
@@ -108,19 +116,22 @@ pub fn build(crates: ~[Crate]) -> bool {
108116
let dir = src_dir();
109117
let work_dir = work_dir();
110118
let mut success = true;
119+
let sysroot = Path(os::args()[1]);
111120

112121
for crates.each |&crate| {
113122
let path = &dir.push_rel(&Path(crate.file)).normalize();
114123

115124
note(fmt!("compiling %s", path.to_str()));
116125

117-
success = compile_crate(path, &work_dir, crate.flags, crate.cfgs,
126+
success = compile_crate(Some(sysroot), path, &work_dir, crate.flags, crate.cfgs,
118127
false, false);
119128

120129
if !success { break; }
121130
}
122131

123-
os::set_exit_status(101);
132+
if !success {
133+
os::set_exit_status(2);
134+
}
124135

125136
success
126137
}

branches/try/src/librustpkg/rustpkg.rc

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use io::{ReaderUtil, WriterUtil};
3030
use std::getopts;
3131
use std::net::url;
3232
use send_map::linear::LinearMap;
33+
use rustc::metadata::filesearch;
3334
use rustc::driver::{driver, session};
3435
use syntax::{ast, attr, codemap, diagnostic, parse, visit};
3536
use semver::Version;
@@ -184,7 +185,11 @@ impl PackageScript {
184185
// the build API.
185186
for crate.node.module.items.each |i| {
186187
match i.node {
187-
ast::item_fn(_, _, _, _) => custom = true,
188+
ast::item_fn(_, _, _, _) => {
189+
custom = true;
190+
191+
break;
192+
}
188193
_ => {}
189194
}
190195
}
@@ -228,10 +233,11 @@ impl PackageScript {
228233
let outputs = driver::build_output_filenames(input, &Some(work_dir),
229234
&None, sess);
230235
let exe = work_dir.push(~"package" + util::exe_suffix());
236+
let root = filesearch::get_rustpkg_sysroot().get().pop().pop();
231237

232238
driver::compile_rest(sess, cfg, driver::cu_parse,
233239
Some(outputs), Some(crate));
234-
run::run_program(exe.to_str(), ~[cmd])
240+
run::run_program(exe.to_str(), ~[root.to_str(), cmd])
235241
}
236242

237243
fn hash() -> ~str {
@@ -282,6 +288,13 @@ impl Ctx {
282288
~"clean" => {
283289
self.clean();
284290
}
291+
~"do" => {
292+
if args.len() < 1 {
293+
return usage::do_cmd();
294+
}
295+
296+
self.do_cmd(args[0]);
297+
}
285298
~"install" => {
286299
self.install(if args.len() >= 1 { Some(args[0]) }
287300
else { None },
@@ -322,6 +335,33 @@ impl Ctx {
322335
}
323336
}
324337

338+
fn do_cmd(cmd: ~str) -> bool {
339+
if cmd == ~"build" {
340+
util::error(~"the build cmd is reserved");
341+
342+
return false;
343+
}
344+
345+
let cwd = &os::getcwd();
346+
let script = match PackageScript::parse(cwd) {
347+
result::Ok(script) => script,
348+
result::Err(err) => {
349+
util::error(err);
350+
351+
return false;
352+
}
353+
};
354+
let status = script.run(cmd);
355+
356+
if status == 1 {
357+
util::error(~"no fns are listening for that cmd");
358+
359+
return false;
360+
}
361+
362+
status == 0
363+
}
364+
325365
fn build(dir: &Path, verbose: bool, opt: bool,
326366
test: bool) -> Option<PackageScript> {
327367
let cwd = &os::getcwd();
@@ -399,7 +439,7 @@ impl Ctx {
399439

400440
fn compile(crate: &Path, dir: &Path, flags: ~[~str],
401441
cfgs: ~[~str], opt: bool, test: bool) -> bool {
402-
util::compile_crate(crate, dir, flags, cfgs, opt, test)
442+
util::compile_crate(None, crate, dir, flags, cfgs, opt, test)
403443
}
404444

405445
fn clean() -> bool {
@@ -759,6 +799,7 @@ pub fn main() {
759799
return match cmd {
760800
~"build" => usage::build(),
761801
~"clean" => usage::clean(),
802+
~"do" => usage::do_cmd(),
762803
~"install" => usage::install(),
763804
~"prefer" => usage::prefer(),
764805
~"test" => usage::test(),

branches/try/src/librustpkg/usage.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ Remove all build files in the work cache for the package in the current
3939
directory.");
4040
}
4141

42+
pub fn do_cmd() {
43+
io::println(~"rustpkg do <cmd>
44+
45+
Runs a command in the package script. You can listen to a command
46+
by tagging a function with the attribute `#[pkg_do(cmd)]`.");
47+
}
48+
4249
pub fn install() {
4350
io::println(~"rustpkg [options..] install [url] [target]
4451

branches/try/src/librustpkg/util.rs

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ pub fn root() -> Path {
3434
}
3535

3636
pub fn is_cmd(cmd: ~str) -> bool {
37-
let cmds = &[~"build", ~"clean", ~"install", ~"prefer", ~"test",
38-
~"uninstall", ~"unprefer"];
37+
let cmds = &[~"build", ~"clean", ~"do", ~"install", ~"prefer",
38+
~"test", ~"uninstall", ~"unprefer"];
3939

4040
vec::contains(cmds, &cmd)
4141
}
@@ -74,6 +74,7 @@ fn mk_rustpkg_use(ctx: @ReadyCtx) -> @ast::view_item {
7474
}
7575

7676
struct ListenerFn {
77+
cmds: ~[~str],
7778
span: codemap::span,
7879
path: ~[ast::ident]
7980
}
@@ -119,8 +120,27 @@ fn fold_item(ctx: @ReadyCtx, &&item: @ast::item,
119120

120121
ctx.path.push(item.ident);
121122

122-
if attr::find_attrs_by_name(item.attrs, ~"pkg_do").is_not_empty() {
123+
let attrs = attr::find_attrs_by_name(item.attrs, ~"pkg_do");
124+
125+
if attrs.len() > 0 {
126+
let mut cmds = ~[];
127+
128+
for attrs.each |attr| {
129+
match attr.node.value.node {
130+
ast::meta_list(_, mis) => {
131+
for mis.each |mi| {
132+
match mi.node {
133+
ast::meta_word(cmd) => cmds.push(cmd),
134+
_ => {}
135+
};
136+
}
137+
}
138+
_ => cmds.push(~"build")
139+
};
140+
}
141+
123142
ctx.fns.push(ListenerFn {
143+
cmds: cmds,
124144
span: item.span,
125145
path: /*bad*/copy ctx.path
126146
});
@@ -284,13 +304,26 @@ fn mk_listener_vec(ctx: @ReadyCtx) -> @ast::expr {
284304
fn mk_listener_rec(ctx: @ReadyCtx, listener: ListenerFn) -> @ast::expr {
285305
let span = listener.span;
286306
let path = /*bad*/copy listener.path;
287-
let cmd_lit = no_span(ast::lit_str(@path_name_i(path,
288-
ctx.sess.parse_sess.interner)));
307+
let descs = do listener.cmds.map |&cmd| {
308+
let inner = @{
309+
id: ctx.sess.next_node_id(),
310+
callee_id: ctx.sess.next_node_id(),
311+
node: ast::expr_lit(@no_span(ast::lit_str(@cmd))),
312+
span: span
313+
};
314+
315+
@{
316+
id: ctx.sess.next_node_id(),
317+
callee_id: ctx.sess.next_node_id(),
318+
node: ast::expr_vstore(inner, ast::expr_vstore_uniq),
319+
span: dummy_sp()
320+
}
321+
};
289322
let cmd_expr_inner = @{
290323
id: ctx.sess.next_node_id(),
291324
callee_id: ctx.sess.next_node_id(),
292-
node: ast::expr_lit(@cmd_lit),
293-
span: span
325+
node: ast::expr_vec(descs, ast::m_imm),
326+
span: dummy_sp()
294327
};
295328
let cmd_expr = {
296329
id: ctx.sess.next_node_id(),
@@ -300,7 +333,7 @@ fn mk_listener_rec(ctx: @ReadyCtx, listener: ListenerFn) -> @ast::expr {
300333
};
301334
let cmd_field = no_span(ast::field_ {
302335
mutbl: ast::m_imm,
303-
ident: ctx.sess.ident_of(~"cmd"),
336+
ident: ctx.sess.ident_of(~"cmds"),
304337
expr: @cmd_expr,
305338
});
306339

@@ -827,7 +860,7 @@ pub fn remove_pkg(pkg: &Package) -> bool {
827860
true
828861
}
829862

830-
pub fn compile_input(input: driver::input, dir: &Path,
863+
pub fn compile_input(sysroot: Option<Path>, input: driver::input, dir: &Path,
831864
flags: ~[~str], cfgs: ~[~str], opt: bool, test: bool) -> bool {
832865
let lib_dir = dir.push(~"lib");
833866
let bin_dir = dir.push(~"bin");
@@ -838,6 +871,7 @@ pub fn compile_input(input: driver::input, dir: &Path,
838871
crate_type: session::unknown_crate,
839872
optimize: if opt { session::Aggressive } else { session::No },
840873
test: test,
874+
maybe_sysroot: sysroot,
841875
.. *session::basic_options()
842876
};
843877
let sess = driver::build_session(options, diagnostic::emit);
@@ -966,14 +1000,14 @@ pub fn exe_suffix() -> ~str { ~".exe" }
9661000
#[cfg(target_os = "macos")]
9671001
pub fn exe_suffix() -> ~str { ~"" }
9681002

969-
pub fn compile_crate(crate: &Path, dir: &Path, flags: ~[~str],
1003+
pub fn compile_crate(sysroot: Option<Path>, crate: &Path, dir: &Path, flags: ~[~str],
9701004
cfgs: ~[~str], opt: bool, test: bool) -> bool {
971-
compile_input(driver::file_input(*crate), dir, flags, cfgs, opt, test)
1005+
compile_input(sysroot, driver::file_input(*crate), dir, flags, cfgs, opt, test)
9721006
}
9731007

974-
pub fn compile_str(code: ~str, dir: &Path, flags: ~[~str],
1008+
pub fn compile_str(sysroot: Option<Path>, code: ~str, dir: &Path, flags: ~[~str],
9751009
cfgs: ~[~str], opt: bool, test: bool) -> bool {
976-
compile_input(driver::str_input(code), dir, flags, cfgs, opt, test)
1010+
compile_input(sysroot, driver::str_input(code), dir, flags, cfgs, opt, test)
9771011
}
9781012

9791013
#[cfg(windows)]
@@ -1002,6 +1036,7 @@ pub fn link_exe(src: &Path, dest: &Path) -> bool unsafe {
10021036
fn test_is_cmd() {
10031037
assert is_cmd(~"build");
10041038
assert is_cmd(~"clean");
1039+
assert is_cmd(~"do");
10051040
assert is_cmd(~"install");
10061041
assert is_cmd(~"prefer");
10071042
assert is_cmd(~"test");

0 commit comments

Comments
 (0)