Skip to content

Commit d846169

Browse files
tychoscibrson
authored andcommitted
cargo: 2 modes -> 3 modes, and clarify them
* -g or --mode=user to create/use .cargo under $HOME * -G or --mode=system to create/use .cargo under sysroot * by default, `cargo` uses .cargo under current working directory
1 parent 48e206e commit d846169

File tree

2 files changed

+67
-23
lines changed

2 files changed

+67
-23
lines changed

src/cargo/cargo.rs

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use std;
55

66
import rustc::syntax::{ast, codemap};
77
import rustc::syntax::parse::parser;
8-
import rustc::util::filesearch::{get_cargo_root, get_cargo_root_nearest};
8+
import rustc::util::filesearch::{get_cargo_root, get_cargo_root_nearest,
9+
get_cargo_sysroot};
910
import rustc::driver::diagnostic;
1011

1112
import std::fs;
@@ -20,7 +21,7 @@ import str;
2021
import std::tempfile;
2122
import vec;
2223
import std::getopts;
23-
import getopts::{optflag, opt_present};
24+
import getopts::{optflag, optopt, opt_present};
2425

2526
enum _src {
2627
/* Break cycles in package <-> source */
@@ -69,12 +70,14 @@ type pkg = {
6970

7071
type options = {
7172
test: bool,
72-
cwd: bool,
73+
mode: mode,
7374
free: [str],
7475
};
7576

77+
enum mode { system_mode, user_mode, local_mode }
78+
7679
fn opts() -> [getopts::opt] {
77-
[optflag("g"), optflag("global"), optflag("test")]
80+
[optflag("g"), optflag("G"), optopt("mode"), optflag("test")]
7881
}
7982

8083
fn info(msg: str) {
@@ -343,21 +346,40 @@ fn build_cargo_options(argv: [str]) -> options {
343346
};
344347

345348
let test = opt_present(match, "test");
346-
let cwd = !(opt_present(match, "g") || opt_present(match, "global"));
349+
let mode = if opt_present(match, "G") {
350+
if opt_present(match, "mode") { fail "--mode and -G both provided"; }
351+
if opt_present(match, "g") { fail "-G and -g both provided"; }
352+
system_mode
353+
} else if opt_present(match, "g") {
354+
if opt_present(match, "mode") { fail "--mode and -g both provided"; }
355+
if opt_present(match, "G") { fail "-G and -g both provided"; }
356+
user_mode
357+
} else if opt_present(match, "mode") {
358+
alt getopts::opt_str(match, "mode") {
359+
"system" { system_mode }
360+
"user" { user_mode }
361+
"local" { local_mode }
362+
_ { fail "argument to `mode` must be one of `system`" +
363+
", `user`, or `normal`";
364+
}
365+
}
366+
} else {
367+
local_mode
368+
};
347369

348-
{test: test, cwd: cwd, free: match.free}
370+
{test: test, mode: mode, free: match.free}
349371
}
350372

351373
fn configure(opts: options) -> cargo {
352-
let get_cargo_dir = if opts.cwd {
353-
get_cargo_root_nearest
354-
} else {
355-
get_cargo_root
374+
let get_cargo_dir = alt opts.mode {
375+
system_mode { get_cargo_sysroot }
376+
user_mode { get_cargo_root }
377+
local_mode { get_cargo_root_nearest }
356378
};
357379

358380
let p = alt get_cargo_dir() {
359-
result::ok(p) { p }
360-
result::err(e) { fail e }
381+
result::ok(p) { p }
382+
result::err(e) { fail e }
361383
};
362384

363385
let sources = map::new_str_hash::<source>();
@@ -736,6 +758,8 @@ fn cmd_init(c: cargo) {
736758
}
737759
info(#fmt["signature ok for sources.json"]);
738760
run::run_program("cp", [srcfile, destsrcfile]);
761+
762+
info(#fmt["Initialized .cargo in %s", c.root]);
739763
}
740764

741765
fn print_pkg(s: source, p: package) {
@@ -775,14 +799,28 @@ fn cmd_search(c: cargo) {
775799
}
776800

777801
fn cmd_usage() {
778-
print("Usage: cargo <verb> [args...]");
779-
print(" init Set up .cargo");
780-
print(" install [--test] [source/]package-name Install by name");
781-
print(" install [--test] uuid:[source/]package-uuid Install by uuid");
782-
print(" list [source] List packages");
783-
print(" search <name | '*'> [tags...] Search packages");
784-
print(" sync Sync all sources");
785-
print(" usage This");
802+
print("Usage: cargo <verb> [options] [args...]" +
803+
"
804+
805+
init Set up .cargo
806+
install [--test] [source/]package-name Install by name
807+
install [--test] uuid:[source/]package-uuid Install by uuid
808+
list [source] List packages
809+
search <name | '*'> [tags...] Search packages
810+
sync Sync all sources
811+
usage This
812+
813+
Options:
814+
815+
--mode=[system,user,local] change mode as (system/user/local)
816+
-g equivalent to --mode=user
817+
-G equivalent to --mode=system
818+
819+
NOTE:
820+
This command creates/uses local-level .cargo by default.
821+
To create/use user-level .cargo, use option -g/--mode=user.
822+
To create/use system-level .cargo, use option -G/--mode=system.
823+
");
786824
}
787825

788826
fn main(argv: [str]) {

src/comp/util/filesearch.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export pick;
1010
export pick_file;
1111
export search;
1212
export relative_target_lib_path;
13+
export get_cargo_sysroot;
1314
export get_cargo_root;
1415
export get_cargo_root_nearest;
1516
export libdir;
@@ -47,6 +48,8 @@ fn mk_filesearch(maybe_sysroot: option<fs::path>,
4748
result::ok(p) { [p] }
4849
result::err(p) { [] }
4950
}
51+
+ [fs::connect(fs::connect(self.sysroot, ".cargo"),
52+
libdir())]
5053
}
5154
fn get_target_lib_path() -> fs::path {
5255
make_target_lib_path(self.sysroot, self.target_triple)
@@ -109,6 +112,10 @@ fn get_sysroot(maybe_sysroot: option<fs::path>) -> fs::path {
109112
}
110113
}
111114

115+
fn get_cargo_sysroot() -> result::t<fs::path, str> {
116+
result::ok(fs::connect(get_default_sysroot(), ".cargo"))
117+
}
118+
112119
fn get_cargo_root() -> result::t<fs::path, str> {
113120
alt generic_os::getenv("CARGO_ROOT") {
114121
some(_p) { result::ok(_p) }
@@ -129,9 +136,8 @@ fn get_cargo_root_nearest() -> result::t<fs::path, str> {
129136
let cwd_cargo = fs::connect(cwd, ".cargo");
130137
let par_cargo = fs::connect(dirname, ".cargo");
131138

132-
// FIXME: this duplicates lib path
133-
if cwd_cargo == p {
134-
ret result::ok(p);
139+
if fs::path_is_dir(cwd_cargo) || cwd_cargo == p {
140+
ret result::ok(cwd_cargo);
135141
}
136142

137143
while vec::is_not_empty(dirpath) && par_cargo != p {

0 commit comments

Comments
 (0)