Skip to content

cargo: Add local mode and use it by default #1760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 109 additions & 40 deletions src/cargo/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std;

import rustc::syntax::{ast, codemap};
import rustc::syntax::parse::parser;
import rustc::util::filesearch::get_cargo_root;
import rustc::util::filesearch::{get_cargo_root, get_cargo_root_nearest,
get_cargo_sysroot};
import rustc::driver::diagnostic;

import std::fs;
Expand All @@ -19,6 +20,8 @@ import std::run;
import str;
import std::tempfile;
import vec;
import std::getopts;
import getopts::{optflag, optopt, opt_present};

enum _src {
/* Break cycles in package <-> source */
Expand Down Expand Up @@ -53,7 +56,7 @@ type cargo = {
workdir: str,
sourcedir: str,
sources: map::hashmap<str, source>,
mutable test: bool
opts: options
};

type pkg = {
Expand All @@ -65,6 +68,18 @@ type pkg = {
crate_type: option<str>
};

type options = {
test: bool,
mode: mode,
free: [str],
};

enum mode { system_mode, user_mode, local_mode }

fn opts() -> [getopts::opt] {
[optflag("g"), optflag("G"), optopt("mode"), optflag("test")]
}

fn info(msg: str) {
io::stdout().write_line("info: " + msg);
}
Expand Down Expand Up @@ -322,10 +337,49 @@ fn load_source_packages(&c: cargo, &src: source) {
};
}

fn configure() -> cargo {
let p = alt get_cargo_root() {
result::ok(p) { p }
result::err(e) { fail e }
fn build_cargo_options(argv: [str]) -> options {
let match = alt getopts::getopts(argv, opts()) {
result::ok(m) { m }
result::err(f) {
fail #fmt["%s", getopts::fail_str(f)];
}
};

let test = opt_present(match, "test");
let mode = if opt_present(match, "G") {
if opt_present(match, "mode") { fail "--mode and -G both provided"; }
if opt_present(match, "g") { fail "-G and -g both provided"; }
system_mode
} else if opt_present(match, "g") {
if opt_present(match, "mode") { fail "--mode and -g both provided"; }
if opt_present(match, "G") { fail "-G and -g both provided"; }
user_mode
} else if opt_present(match, "mode") {
alt getopts::opt_str(match, "mode") {
"system" { system_mode }
"user" { user_mode }
"local" { local_mode }
_ { fail "argument to `mode` must be one of `system`" +
", `user`, or `normal`";
}
}
} else {
local_mode
};

{test: test, mode: mode, free: match.free}
}

fn configure(opts: options) -> cargo {
let get_cargo_dir = alt opts.mode {
system_mode { get_cargo_sysroot }
user_mode { get_cargo_root }
local_mode { get_cargo_root_nearest }
};

let p = alt get_cargo_dir() {
result::ok(p) { p }
result::err(e) { fail e }
};

let sources = map::new_str_hash::<source>();
Expand All @@ -339,7 +393,7 @@ fn configure() -> cargo {
workdir: fs::connect(p, "work"),
sourcedir: fs::connect(p, "sources"),
sources: sources,
mutable test: false
opts: opts
};

need_dir(c.root);
Expand Down Expand Up @@ -430,7 +484,7 @@ fn install_source(c: cargo, path: str) {
alt p {
none { cont; }
some(_p) {
if c.test {
if c.opts.test {
test_one_crate(c, path, cf, _p);
}
install_one_crate(c, path, cf, _p);
Expand Down Expand Up @@ -573,19 +627,14 @@ fn install_named_specific(c: cargo, wd: str, src: str, name: str) {
error("Can't find package " + src + "/" + name);
}

fn cmd_install(c: cargo, argv: [str]) unsafe {
fn cmd_install(c: cargo) unsafe {
// cargo install <pkg>
if vec::len(argv) < 3u {
if vec::len(c.opts.free) < 3u {
cmd_usage();
ret;
}

let target = argv[2];
// TODO: getopts
if vec::len(argv) > 3u && argv[2] == "--test" {
c.test = true;
target = argv[3];
}
let target = c.opts.free[2];

let wd = alt tempfile::mkdtemp(c.workdir + fs::path_sep(), "") {
some(_wd) { _wd }
Expand Down Expand Up @@ -671,9 +720,9 @@ fn sync_one(c: cargo, name: str, src: source) {
run::run_program("cp", [pkgfile, destpkgfile]);
}

fn cmd_sync(c: cargo, argv: [str]) {
if vec::len(argv) == 3u {
sync_one(c, argv[2], c.sources.get(argv[2]));
fn cmd_sync(c: cargo) {
if vec::len(c.opts.free) == 3u {
sync_one(c, c.opts.free[2], c.sources.get(c.opts.free[2]));
} else {
cargo_suggestion(c, true, { || } );
c.sources.items { |k, v|
Expand Down Expand Up @@ -709,6 +758,8 @@ fn cmd_init(c: cargo) {
}
info(#fmt["signature ok for sources.json"]);
run::run_program("cp", [srcfile, destsrcfile]);

info(#fmt["Initialized .cargo in %s", c.root]);
}

fn print_pkg(s: source, p: package) {
Expand All @@ -721,22 +772,22 @@ fn print_pkg(s: source, p: package) {
print(" >> " + p.description + "\n")
}
}
fn cmd_list(c: cargo, argv: [str]) {
fn cmd_list(c: cargo) {
for_each_package(c, { |s, p|
if vec::len(argv) <= 2u || argv[2] == s.name {
if vec::len(c.opts.free) <= 2u || c.opts.free[2] == s.name {
print_pkg(s, p);
}
});
}

fn cmd_search(c: cargo, argv: [str]) {
if vec::len(argv) < 3u {
fn cmd_search(c: cargo) {
if vec::len(c.opts.free) < 3u {
cmd_usage();
ret;
}
let n = 0;
let name = argv[2];
let tags = vec::slice(argv, 3u, vec::len(argv));
let name = c.opts.free[2];
let tags = vec::slice(c.opts.free, 3u, vec::len(c.opts.free));
for_each_package(c, { |s, p|
if (str::contains(p.name, name) || name == "*") &&
vec::all(tags, { |t| vec::member(t, p.tags) }) {
Expand All @@ -748,28 +799,46 @@ fn cmd_search(c: cargo, argv: [str]) {
}

fn cmd_usage() {
print("Usage: cargo <verb> [args...]");
print(" init Set up ~/.cargo");
print(" install [--test] [source/]package-name Install by name");
print(" install [--test] uuid:[source/]package-uuid Install by uuid");
print(" list [source] List packages");
print(" search <name | '*'> [tags...] Search packages");
print(" sync Sync all sources");
print(" usage This");
print("Usage: cargo <verb> [options] [args...]" +
"

init Set up .cargo
install [--test] [source/]package-name Install by name
install [--test] uuid:[source/]package-uuid Install by uuid
list [source] List packages
search <name | '*'> [tags...] Search packages
sync Sync all sources
usage This

Options:

--mode=[system,user,local] change mode as (system/user/local)
-g equivalent to --mode=user
-G equivalent to --mode=system

NOTE:
This command creates/uses local-level .cargo by default.
To create/use user-level .cargo, use option -g/--mode=user.
To create/use system-level .cargo, use option -G/--mode=system.
");
}

fn main(argv: [str]) {
if vec::len(argv) < 2u {
let o = build_cargo_options(argv);

if vec::len(o.free) < 2u {
cmd_usage();
ret;
}
let c = configure();
alt argv[1] {

let c = configure(o);

alt o.free[1] {
"init" { cmd_init(c); }
"install" { cmd_install(c, argv); }
"list" { cmd_list(c, argv); }
"search" { cmd_search(c, argv); }
"sync" { cmd_sync(c, argv); }
"install" { cmd_install(c); }
"list" { cmd_list(c); }
"search" { cmd_search(c); }
"sync" { cmd_sync(c); }
"usage" { cmd_usage(); }
_ { cmd_usage(); }
}
Expand Down
43 changes: 43 additions & 0 deletions src/comp/util/filesearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export pick;
export pick_file;
export search;
export relative_target_lib_path;
export get_cargo_sysroot;
export get_cargo_root;
export get_cargo_root_nearest;
export libdir;

type pick<T> = fn(path: fs::path) -> option<T>;
Expand Down Expand Up @@ -43,10 +45,16 @@ fn mk_filesearch(maybe_sysroot: option<fs::path>,
fn lib_search_paths() -> [fs::path] {
self.addl_lib_search_paths
+ [make_target_lib_path(self.sysroot, self.target_triple)]
+ alt get_cargo_lib_path_nearest() {
result::ok(p) { [p] }
result::err(p) { [] }
}
+ alt get_cargo_lib_path() {
result::ok(p) { [p] }
result::err(p) { [] }
}
+ [fs::connect(fs::connect(self.sysroot, ".cargo"),
libdir())]
}
fn get_target_lib_path() -> fs::path {
make_target_lib_path(self.sysroot, self.target_triple)
Expand Down Expand Up @@ -109,6 +117,10 @@ fn get_sysroot(maybe_sysroot: option<fs::path>) -> fs::path {
}
}

fn get_cargo_sysroot() -> result::t<fs::path, str> {
result::ok(fs::connect(get_default_sysroot(), ".cargo"))
}

fn get_cargo_root() -> result::t<fs::path, str> {
alt generic_os::getenv("CARGO_ROOT") {
some(_p) { result::ok(_p) }
Expand All @@ -121,12 +133,43 @@ fn get_cargo_root() -> result::t<fs::path, str> {
}
}

fn get_cargo_root_nearest() -> result::t<fs::path, str> {
result::chain(get_cargo_root()) { |p|
let cwd = os::getcwd();
let dirname = fs::dirname(cwd);
let dirpath = fs::split(dirname);
let cwd_cargo = fs::connect(cwd, ".cargo");
let par_cargo = fs::connect(dirname, ".cargo");

if fs::path_is_dir(cwd_cargo) || cwd_cargo == p {
ret result::ok(cwd_cargo);
}

while vec::is_not_empty(dirpath) && par_cargo != p {
if fs::path_is_dir(par_cargo) {
ret result::ok(par_cargo);
}
vec::pop(dirpath);
dirname = fs::dirname(dirname);
par_cargo = fs::connect(dirname, ".cargo");
}

result::ok(cwd_cargo)
}
}

fn get_cargo_lib_path() -> result::t<fs::path, str> {
result::chain(get_cargo_root()) { |p|
result::ok(fs::connect(p, libdir()))
}
}

fn get_cargo_lib_path_nearest() -> result::t<fs::path, str> {
result::chain(get_cargo_root_nearest()) { |p|
result::ok(fs::connect(p, libdir()))
}
}

// The name of the directory rustc expects libraries to be located.
// On Unix should be "lib", on windows "bin"
fn libdir() -> str {
Expand Down