2013-01-14 20:55:47 +10:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// rustpkg - a purely function package manager and build system
|
|
|
|
|
|
|
|
#[link(name = "rustpkg",
|
|
|
|
vers = "0.6",
|
|
|
|
uuid = "25de5e6e-279e-4a20-845c-4cabae92daaf",
|
|
|
|
url = "https://github.com/mozilla/rust/tree/master/src/librustpkg")];
|
|
|
|
|
|
|
|
#[crate_type = "lib"];
|
|
|
|
#[no_core];
|
2013-01-16 21:59:37 +10:00
|
|
|
#[allow(vecs_implicitly_copyable,
|
|
|
|
non_implicitly_copyable_typarams)];
|
2013-01-14 20:55:47 +10:00
|
|
|
|
|
|
|
extern mod core(vers = "0.6");
|
|
|
|
extern mod std(vers = "0.6");
|
|
|
|
extern mod rustc(vers = "0.6");
|
|
|
|
extern mod syntax(vers = "0.6");
|
|
|
|
|
2013-01-15 23:57:03 +10:00
|
|
|
use core::*;
|
2013-01-16 21:59:37 +10:00
|
|
|
use io::{ReaderUtil, WriterUtil};
|
2013-01-15 23:57:03 +10:00
|
|
|
use std::getopts;
|
2013-01-18 18:31:43 +10:00
|
|
|
use std::net::url;
|
2013-01-19 19:59:19 +10:00
|
|
|
use send_map::linear::LinearMap;
|
2013-01-18 18:31:43 +10:00
|
|
|
use rustc::driver::{driver, session};
|
2013-01-14 20:55:47 +10:00
|
|
|
use rustc::metadata::{filesearch};
|
2013-01-18 18:31:43 +10:00
|
|
|
use syntax::{ast, attr, codemap, diagnostic, parse, visit};
|
2013-01-16 21:59:37 +10:00
|
|
|
use semver::Version;
|
2013-01-14 20:55:47 +10:00
|
|
|
|
2013-01-15 23:57:03 +10:00
|
|
|
mod api;
|
|
|
|
mod usage;
|
|
|
|
mod util;
|
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
use util::Package;
|
|
|
|
|
2013-01-16 21:59:37 +10:00
|
|
|
struct PackageScript {
|
|
|
|
id: ~str,
|
|
|
|
name: ~str,
|
2013-01-17 19:05:19 +10:00
|
|
|
vers: Version,
|
|
|
|
crates: ~[~str],
|
|
|
|
deps: ~[(~str, Option<~str>)]
|
2013-01-16 21:59:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PackageScript {
|
2013-01-18 18:31:43 +10:00
|
|
|
static fn parse(parent: Path) -> Result<PackageScript, ~str> {
|
2013-01-16 21:59:37 +10:00
|
|
|
let script = parent.push(~"package.rs");
|
|
|
|
|
|
|
|
if !os::path_exists(&script) {
|
2013-01-18 18:31:43 +10:00
|
|
|
return result::Err(~"no package.rs file");
|
2013-01-16 21:59:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
let sess = parse::new_parse_sess(None);
|
|
|
|
let crate = parse::parse_crate_from_file(&script, ~[], sess);
|
|
|
|
let mut id = None;
|
|
|
|
let mut vers = None;
|
2013-01-17 19:05:19 +10:00
|
|
|
let mut crates = ~[];
|
|
|
|
let mut deps = ~[];
|
2013-01-16 21:59:37 +10:00
|
|
|
|
|
|
|
fn load_pkg_attr(mis: ~[@ast::meta_item]) -> (Option<~str>,
|
|
|
|
Option<~str>) {
|
|
|
|
let mut id = None;
|
|
|
|
let mut vers = None;
|
|
|
|
|
|
|
|
for mis.each |a| {
|
|
|
|
match a.node {
|
|
|
|
ast::meta_name_value(v, ast::spanned {
|
|
|
|
node: ast::lit_str(s),
|
|
|
|
span: _}) => {
|
|
|
|
match v {
|
|
|
|
~"id" => id = Some(*s),
|
|
|
|
~"vers" => vers = Some(*s),
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(id, vers)
|
|
|
|
}
|
|
|
|
|
2013-01-17 19:05:19 +10:00
|
|
|
fn load_pkg_dep_attr(mis: ~[@ast::meta_item]) -> (Option<~str>,
|
|
|
|
Option<~str>) {
|
|
|
|
let mut url = None;
|
|
|
|
let mut target = None;
|
|
|
|
|
|
|
|
for mis.each |a| {
|
|
|
|
match a.node {
|
|
|
|
ast::meta_name_value(v, ast::spanned {
|
|
|
|
node: ast::lit_str(s),
|
|
|
|
span: _}) => {
|
|
|
|
match v {
|
|
|
|
~"url" => url = Some(*s),
|
|
|
|
~"target" => target = Some(*s),
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(url, target)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_pkg_crate_attr(mis: ~[@ast::meta_item]) -> Option<~str> {
|
|
|
|
let mut file = None;
|
|
|
|
|
|
|
|
for mis.each |a| {
|
|
|
|
match a.node {
|
|
|
|
ast::meta_name_value(v, ast::spanned {
|
|
|
|
node: ast::lit_str(s),
|
|
|
|
span: _}) => {
|
|
|
|
match v {
|
|
|
|
~"file" => file = Some(*s),
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file
|
|
|
|
}
|
|
|
|
|
2013-01-16 21:59:37 +10:00
|
|
|
for crate.node.attrs.each |a| {
|
|
|
|
match a.node.value.node {
|
|
|
|
ast::meta_list(v, mis) => {
|
|
|
|
match v {
|
|
|
|
~"pkg" => {
|
|
|
|
let (i, v) = load_pkg_attr(mis);
|
|
|
|
|
|
|
|
id = i;
|
|
|
|
vers = v;
|
|
|
|
}
|
2013-01-17 19:05:19 +10:00
|
|
|
~"pkg_dep" => {
|
|
|
|
let (u, t) = load_pkg_dep_attr(mis);
|
|
|
|
|
|
|
|
if u.is_none() {
|
|
|
|
fail ~"pkg_dep attr without a url value";
|
|
|
|
}
|
|
|
|
|
|
|
|
deps.push((u.get(), t));
|
|
|
|
}
|
|
|
|
~"pkg_crate" => {
|
|
|
|
let f = load_pkg_crate_attr(mis);
|
|
|
|
|
|
|
|
if f.is_none() {
|
|
|
|
fail ~"pkg_file attr without a file value";
|
|
|
|
}
|
|
|
|
|
|
|
|
crates.push(f.get());
|
|
|
|
}
|
2013-01-16 21:59:37 +10:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if id.is_none() || vers.is_none() {
|
2013-01-18 18:31:43 +10:00
|
|
|
return result::Err(~"package's pkg attr is missing required data (id, vers)");
|
2013-01-16 21:59:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
let id = id.get();
|
2013-01-19 19:59:19 +10:00
|
|
|
let name = match util::parse_name(id) {
|
|
|
|
result::Ok(name) => name,
|
|
|
|
result::Err(err) => return result::Err(err)
|
|
|
|
};
|
|
|
|
let vers = match util::parse_vers(vers.get()) {
|
|
|
|
result::Ok(vers) => vers,
|
|
|
|
result::Err(err) => return result::Err(err)
|
|
|
|
};
|
2013-01-16 21:59:37 +10:00
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
result::Ok(PackageScript {
|
2013-01-16 21:59:37 +10:00
|
|
|
id: id,
|
2013-01-19 19:59:19 +10:00
|
|
|
name: name,
|
|
|
|
vers: vers,
|
2013-01-17 19:05:19 +10:00
|
|
|
crates: crates,
|
|
|
|
deps: deps
|
2013-01-18 18:31:43 +10:00
|
|
|
})
|
2013-01-16 21:59:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
fn hash() -> ~str {
|
2013-01-19 19:59:19 +10:00
|
|
|
fmt!("%s-%s-%s", self.name, util::hash(self.id + self.vers.to_str()), self.vers.to_str())
|
2013-01-16 21:59:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
fn work_dir() -> Path {
|
2013-01-17 19:05:19 +10:00
|
|
|
util::root().push(~"work").push(self.hash())
|
2013-01-16 21:59:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Ctx {
|
2013-01-19 19:59:19 +10:00
|
|
|
cfg: ast::crate_cfg,
|
|
|
|
mut dep_cache: LinearMap<~str, bool>
|
2013-01-16 21:59:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Ctx {
|
2013-01-17 19:05:19 +10:00
|
|
|
fn run(cmd: ~str, args: ~[~str]) {
|
|
|
|
let root = util::root();
|
|
|
|
|
|
|
|
util::need_dir(&root);
|
|
|
|
util::need_dir(&root.push(~"work"));
|
|
|
|
util::need_dir(&root.push(~"lib"));
|
|
|
|
util::need_dir(&root.push(~"bin"));
|
|
|
|
util::need_dir(&root.push(~"tmp"));
|
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
fn sep_name_vers(in: ~str) -> (Option<~str>, Option<~str>) {
|
|
|
|
let mut name = None;
|
|
|
|
let mut vers = None;
|
|
|
|
let parts = str::split_char(in, '@');
|
|
|
|
|
|
|
|
if parts.len() >= 1 {
|
|
|
|
name = Some(parts[0]);
|
2013-01-19 19:59:19 +10:00
|
|
|
|
|
|
|
if parts.len() >= 2 {
|
|
|
|
vers = Some(parts[1]);
|
|
|
|
}
|
2013-01-18 18:31:43 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
(name, vers)
|
|
|
|
}
|
|
|
|
|
2013-01-17 19:05:19 +10:00
|
|
|
match cmd {
|
2013-01-18 18:31:43 +10:00
|
|
|
~"build" => self.build(),
|
|
|
|
~"clean" => self.clean(),
|
|
|
|
~"install" => {
|
|
|
|
self.install(if args.len() >= 1 { Some(args[0]) }
|
|
|
|
else { None },
|
|
|
|
if args.len() >= 2 { Some(args[1]) }
|
|
|
|
else { None }, false)
|
|
|
|
}
|
|
|
|
~"prefer" => {
|
|
|
|
if args.len() < 1 {
|
|
|
|
return usage::uninstall();
|
|
|
|
}
|
|
|
|
|
|
|
|
let (name, vers) = sep_name_vers(args[0]);
|
|
|
|
|
|
|
|
self.prefer(name.get(), vers)
|
|
|
|
}
|
|
|
|
~"test" => self.test(),
|
|
|
|
~"uninstall" => {
|
|
|
|
if args.len() < 1 {
|
|
|
|
return usage::uninstall();
|
|
|
|
}
|
|
|
|
|
|
|
|
let (name, vers) = sep_name_vers(args[0]);
|
|
|
|
|
|
|
|
self.uninstall(name.get(), vers)
|
|
|
|
}
|
|
|
|
~"unprefer" => {
|
|
|
|
if args.len() < 1 {
|
|
|
|
return usage::uninstall();
|
|
|
|
}
|
|
|
|
|
|
|
|
let (name, vers) = sep_name_vers(args[0]);
|
|
|
|
|
|
|
|
self.unprefer(name.get(), vers)
|
|
|
|
}
|
2013-01-16 21:59:37 +10:00
|
|
|
_ => fail ~"reached an unhandled command"
|
2013-01-17 19:05:19 +10:00
|
|
|
};
|
2013-01-16 21:59:37 +10:00
|
|
|
}
|
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
fn build() -> bool {
|
|
|
|
let dir = os::getcwd();
|
|
|
|
let script = match PackageScript::parse(dir) {
|
|
|
|
result::Ok(script) => script,
|
|
|
|
result::Err(err) => {
|
|
|
|
util::error(err);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let work_dir = script.work_dir();
|
2013-01-17 19:05:19 +10:00
|
|
|
let mut success = true;
|
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
util::need_dir(&work_dir);
|
|
|
|
util::note(fmt!("building %s v%s (%s)", script.name, script.vers.to_str(),
|
2013-01-17 19:05:19 +10:00
|
|
|
script.id));
|
|
|
|
|
|
|
|
if script.deps.len() >= 1 {
|
2013-01-18 18:31:43 +10:00
|
|
|
util::note(~"installing dependencies");
|
2013-01-17 19:05:19 +10:00
|
|
|
|
|
|
|
for script.deps.each |&dep| {
|
|
|
|
let (url, target) = dep;
|
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
success = self.install(Some(url), target, true);
|
2013-01-17 19:05:19 +10:00
|
|
|
|
|
|
|
if !success { break; }
|
|
|
|
}
|
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
|
2013-01-17 19:05:19 +10:00
|
|
|
if !success {
|
|
|
|
util::error(fmt!("building %s v%s failed: a dep wasn't installed",
|
|
|
|
script.name, script.vers.to_str()));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
util::note(~"installed dependencies");
|
2013-01-17 19:05:19 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
for script.crates.each |&crate| {
|
2013-01-19 19:59:19 +10:00
|
|
|
success = self.compile(&work_dir, &dir.push_rel(&Path(crate)), ~[],
|
|
|
|
false, false);
|
2013-01-17 19:05:19 +10:00
|
|
|
|
|
|
|
if !success { break; }
|
|
|
|
}
|
|
|
|
|
|
|
|
if !success {
|
|
|
|
util::error(fmt!("building %s v%s failed: a crate failed to compile",
|
|
|
|
script.name, script.vers.to_str()));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-16 21:59:37 +10:00
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
util::note(fmt!("built %s v%s", script.name, script.vers.to_str()));
|
2013-01-17 19:05:19 +10:00
|
|
|
|
|
|
|
true
|
2013-01-16 21:59:37 +10:00
|
|
|
}
|
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
fn compile(dir: &Path, crate: &Path, flags: ~[~str],
|
|
|
|
opt: bool, test: bool) -> bool {
|
2013-01-18 18:31:43 +10:00
|
|
|
util::note(~"compiling " + crate.to_str());
|
|
|
|
|
|
|
|
let lib_dir = dir.push(~"lib");
|
|
|
|
let bin_dir = dir.push(~"bin");
|
2013-01-19 19:59:19 +10:00
|
|
|
let test_dir = dir.push(~"test");
|
2013-01-18 18:31:43 +10:00
|
|
|
let binary = os::args()[0];
|
|
|
|
let options: @session::options = @{
|
|
|
|
binary: binary,
|
2013-01-19 19:59:19 +10:00
|
|
|
addl_lib_search_paths: ~[util::root().push(~"lib")],
|
2013-01-18 18:31:43 +10:00
|
|
|
crate_type: session::unknown_crate,
|
2013-01-19 19:59:19 +10:00
|
|
|
optimize: if opt { session::Aggressive } else { session::No },
|
|
|
|
test: test,
|
2013-01-18 18:31:43 +10:00
|
|
|
.. *session::basic_options()
|
|
|
|
};
|
|
|
|
let input = driver::file_input(*crate);
|
|
|
|
let sess = driver::build_session(options, diagnostic::emit);
|
|
|
|
let cfg = driver::build_configuration(sess, binary, input);
|
|
|
|
let mut outputs = driver::build_output_filenames(input, &None, &None,
|
|
|
|
sess);
|
|
|
|
let {crate, _} = driver::compile_upto(sess, cfg, input, driver::cu_parse,
|
|
|
|
Some(outputs));
|
|
|
|
|
|
|
|
let mut name = None;
|
|
|
|
let mut vers = None;
|
|
|
|
let mut uuid = None;
|
|
|
|
let mut crate_type = None;
|
|
|
|
|
|
|
|
fn load_link_attr(mis: ~[@ast::meta_item]) -> (Option<~str>,
|
|
|
|
Option<~str>,
|
|
|
|
Option<~str>) {
|
|
|
|
let mut name = None;
|
|
|
|
let mut vers = None;
|
|
|
|
let mut uuid = None;
|
|
|
|
|
|
|
|
for mis.each |a| {
|
|
|
|
match a.node {
|
|
|
|
ast::meta_name_value(v, ast::spanned {node: ast::lit_str(s),
|
|
|
|
span: _}) => {
|
|
|
|
match v {
|
|
|
|
~"name" => name = Some(*s),
|
|
|
|
~"vers" => vers = Some(*s),
|
|
|
|
~"uuid" => uuid = Some(*s),
|
|
|
|
_ => { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(name, vers, uuid)
|
|
|
|
}
|
|
|
|
|
|
|
|
for crate.node.attrs.each |a| {
|
|
|
|
match a.node.value.node {
|
|
|
|
ast::meta_name_value(v, ast::spanned {node: ast::lit_str(s),
|
|
|
|
span: _}) => {
|
|
|
|
match v {
|
|
|
|
~"crate_type" => crate_type = Some(*s),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::meta_list(v, mis) => {
|
|
|
|
match v {
|
|
|
|
~"link" => {
|
|
|
|
let (n, v, u) = load_link_attr(mis);
|
|
|
|
|
|
|
|
name = n;
|
|
|
|
vers = v;
|
|
|
|
uuid = u;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if name.is_none() || vers.is_none() || uuid.is_none() {
|
|
|
|
util::error(~"crate's link attr is missing required data (name, vers, uuid)");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = name.get();
|
|
|
|
let vers = vers.get();
|
|
|
|
let uuid = uuid.get();
|
|
|
|
|
|
|
|
let is_bin = match crate_type {
|
|
|
|
Some(crate_type) => {
|
|
|
|
match crate_type {
|
|
|
|
~"bin" => true,
|
|
|
|
~"lib" => false,
|
|
|
|
_ => {
|
|
|
|
util::warn(~"unknown crate_type, falling back to lib");
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
util::warn(~"missing crate_type attr, assuming lib");
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
if test {
|
|
|
|
util::need_dir(&test_dir);
|
2013-01-18 18:31:43 +10:00
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
outputs = driver::build_output_filenames(input, &Some(test_dir),
|
|
|
|
&None, sess)
|
|
|
|
}
|
|
|
|
else if is_bin {
|
2013-01-18 18:31:43 +10:00
|
|
|
util::need_dir(&bin_dir);
|
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
#[cfg(windows)]
|
|
|
|
fn suffix() -> ~str { ~".exe" }
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
fn suffix() -> ~str { ~"" }
|
|
|
|
|
|
|
|
let path = bin_dir.push(fmt!("%s-%s-%s%s", name,
|
|
|
|
util::hash(name + uuid + vers),
|
|
|
|
vers, suffix()));
|
2013-01-18 18:31:43 +10:00
|
|
|
outputs = driver::build_output_filenames(input, &None, &Some(path), sess);
|
|
|
|
} else {
|
|
|
|
util::need_dir(&lib_dir);
|
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
outputs = driver::build_output_filenames(input, &Some(lib_dir),
|
|
|
|
&None, sess)
|
2013-01-18 18:31:43 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
driver::compile_upto(sess, cfg, input, driver::cu_everything,
|
|
|
|
Some(outputs));
|
2013-01-16 21:59:37 +10:00
|
|
|
|
2013-01-17 19:05:19 +10:00
|
|
|
true
|
2013-01-16 21:59:37 +10:00
|
|
|
}
|
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
fn clean() -> bool {
|
|
|
|
let script = match PackageScript::parse(os::getcwd()) {
|
|
|
|
result::Ok(script) => script,
|
|
|
|
result::Err(err) => {
|
|
|
|
util::error(err);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2013-01-17 19:05:19 +10:00
|
|
|
let dir = script.work_dir();
|
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
util::note(fmt!("cleaning %s v%s (%s)", script.name, script.vers.to_str(),
|
2013-01-17 19:05:19 +10:00
|
|
|
script.id));
|
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
if os::path_exists(&dir) {
|
|
|
|
util::remove_dir_r(&dir);
|
|
|
|
util::note(fmt!("removed %s", dir.to_str()));
|
2013-01-17 19:05:19 +10:00
|
|
|
}
|
2013-01-16 21:59:37 +10:00
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
util::note(fmt!("cleaned %s v%s", script.name,
|
|
|
|
script.vers.to_str()));
|
|
|
|
|
2013-01-17 19:05:19 +10:00
|
|
|
true
|
2013-01-16 21:59:37 +10:00
|
|
|
}
|
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
fn install(url: Option<~str>, target: Option<~str>, cache: bool) -> bool {
|
|
|
|
let mut success = true;
|
2013-01-17 19:05:19 +10:00
|
|
|
let mut dir;
|
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
if url.is_none() {
|
|
|
|
util::note(~"installing from the cwd");
|
2013-01-17 19:05:19 +10:00
|
|
|
|
|
|
|
dir = os::getcwd();
|
|
|
|
} else {
|
2013-01-18 18:31:43 +10:00
|
|
|
let url = url.get();
|
2013-01-19 19:59:19 +10:00
|
|
|
let hash = util::hash(if !target.is_none() { url + target.get() } else { url });
|
2013-01-17 19:05:19 +10:00
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
if self.dep_cache.contains_key(&hash) {
|
|
|
|
util::warn(~"already installed dep this run");
|
2013-01-17 19:05:19 +10:00
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
return true;
|
2013-01-17 19:05:19 +10:00
|
|
|
}
|
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
self.dep_cache.insert(hash, true);
|
|
|
|
|
|
|
|
dir = util::root().push(~"tmp").push(hash);
|
2013-01-18 18:31:43 +10:00
|
|
|
|
|
|
|
if cache && os::path_exists(&dir) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-17 19:05:19 +10:00
|
|
|
success = self.fetch(&dir, url, target);
|
2013-01-16 21:59:37 +10:00
|
|
|
|
2013-01-17 19:05:19 +10:00
|
|
|
if !success {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
let script = match PackageScript::parse(dir) {
|
|
|
|
result::Ok(script) => script,
|
|
|
|
result::Err(err) => {
|
|
|
|
util::error(err);
|
2013-01-17 19:05:19 +10:00
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let work_dir = script.work_dir();
|
|
|
|
|
|
|
|
util::need_dir(&work_dir);
|
|
|
|
util::note(fmt!("installing %s v%s (%s)", script.name, script.vers.to_str(),
|
2013-01-17 19:05:19 +10:00
|
|
|
script.id));
|
|
|
|
|
|
|
|
if script.deps.len() >= 1 {
|
2013-01-18 18:31:43 +10:00
|
|
|
util::note(~"installing dependencies");
|
2013-01-17 19:05:19 +10:00
|
|
|
|
|
|
|
for script.deps.each |&dep| {
|
|
|
|
let (url, target) = dep;
|
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
success = self.install(Some(url), target, false);
|
2013-01-17 19:05:19 +10:00
|
|
|
|
|
|
|
if !success { break; }
|
|
|
|
}
|
|
|
|
|
|
|
|
if !success {
|
|
|
|
util::error(fmt!("installing %s v%s failed: a dep wasn't installed",
|
|
|
|
script.name, script.vers.to_str()));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
util::note(~"installed dependencies");
|
2013-01-17 19:05:19 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
for script.crates.each |&crate| {
|
2013-01-19 19:59:19 +10:00
|
|
|
success = self.compile(&work_dir, &dir.push_rel(&Path(crate)), ~[],
|
|
|
|
true, false);
|
2013-01-17 19:05:19 +10:00
|
|
|
|
|
|
|
if !success { break; }
|
|
|
|
}
|
|
|
|
|
|
|
|
if !success {
|
|
|
|
util::error(fmt!("installing %s v%s failed: a crate failed to compile",
|
|
|
|
script.name, script.vers.to_str()));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
let from_bin_dir = work_dir.push(~"bin");
|
|
|
|
let from_lib_dir = work_dir.push(~"lib");
|
2013-01-18 18:31:43 +10:00
|
|
|
let to_bin_dir = util::root().push(~"bin");
|
|
|
|
let to_lib_dir = util::root().push(~"lib");
|
2013-01-19 19:59:19 +10:00
|
|
|
let mut bins = ~[];
|
|
|
|
let mut libs = ~[];
|
2013-01-18 18:31:43 +10:00
|
|
|
|
|
|
|
for os::walk_dir(&from_bin_dir) |bin| {
|
|
|
|
let to = to_bin_dir.push_rel(&bin.file_path());
|
|
|
|
|
|
|
|
os::copy_file(bin, &to);
|
2013-01-19 19:59:19 +10:00
|
|
|
bins.push(to.to_str());
|
2013-01-18 18:31:43 +10:00
|
|
|
}
|
2013-01-19 19:59:19 +10:00
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
for os::walk_dir(&from_lib_dir) |lib| {
|
|
|
|
let to = to_lib_dir.push_rel(&lib.file_path());
|
|
|
|
|
|
|
|
os::copy_file(lib, &to);
|
2013-01-19 19:59:19 +10:00
|
|
|
libs.push(to.to_str());
|
2013-01-18 18:31:43 +10:00
|
|
|
}
|
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
let package = Package {
|
|
|
|
id: script.id,
|
|
|
|
vers: script.vers,
|
|
|
|
bins: bins,
|
|
|
|
libs: libs
|
|
|
|
};
|
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
util::note(fmt!("installed %s v%s", script.name,
|
2013-01-17 19:05:19 +10:00
|
|
|
script.vers.to_str()));
|
2013-01-19 19:59:19 +10:00
|
|
|
util::add_pkg(&package);
|
2013-01-17 19:05:19 +10:00
|
|
|
|
|
|
|
true
|
2013-01-16 21:59:37 +10:00
|
|
|
}
|
|
|
|
|
2013-01-17 19:05:19 +10:00
|
|
|
fn fetch(dir: &Path, url: ~str, target: Option<~str>) -> bool {
|
2013-01-18 18:31:43 +10:00
|
|
|
let url = match url::from_str(if str::find_str(url, "://").is_none() { ~"http://" + url }
|
|
|
|
else { url }) {
|
|
|
|
result::Ok(url) => url,
|
|
|
|
result::Err(err) => {
|
|
|
|
util::error(fmt!("failed parsing %s", err.to_lower()));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let str = url.to_str();
|
|
|
|
|
|
|
|
match Path(url.path).filetype() {
|
|
|
|
Some(ext) => {
|
|
|
|
if ext == ~".git" {
|
|
|
|
return self.fetch_git(dir, str, target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
match url.scheme {
|
|
|
|
~"git" => self.fetch_git(dir, str, target),
|
|
|
|
~"http" | ~"ftp" | ~"file" => self.fetch_curl(dir, str),
|
|
|
|
_ => {
|
|
|
|
util::warn(~"unknown url scheme to fetch, using curl");
|
|
|
|
self.fetch_curl(dir, str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fetch_curl(dir: &Path, url: ~str) -> bool {
|
|
|
|
util::note(fmt!("fetching from %s using curl", url));
|
|
|
|
|
|
|
|
let tar = dir.dir_path().push(&dir.file_path().to_str() + ~".tar");
|
|
|
|
|
|
|
|
if run::program_output(~"curl", ~[~"-f", ~"-s", ~"-o", tar.to_str(), url]).status != 0 {
|
|
|
|
util::error(~"fetching failed: downloading using curl failed");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if run::program_output(~"tar", ~[~"-x", ~"--strip-components=1", ~"-C", dir.to_str(), ~"-f", tar.to_str()]).status != 0 {
|
|
|
|
util::error(~"fetching failed: extracting using tar failed (is it a valid tar archive?)");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fetch_git(dir: &Path, url: ~str, target: Option<~str>) -> bool {
|
|
|
|
util::note(fmt!("fetching from %s using git", url));
|
|
|
|
|
|
|
|
// Git can't clone into a non-empty directory
|
2013-01-19 19:59:19 +10:00
|
|
|
util::remove_dir_r(dir);
|
2013-01-18 18:31:43 +10:00
|
|
|
|
|
|
|
if run::program_output(~"git", ~[~"clone", url, dir.to_str()]).status != 0 {
|
|
|
|
util::error(~"fetching failed: can't clone repository");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !target.is_none() {
|
|
|
|
let mut success = true;
|
|
|
|
|
|
|
|
do util::temp_change_dir(dir) {
|
|
|
|
success = run::program_output(~"git", ~[~"checkout", target.get()]).status != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if !success {
|
|
|
|
util::error(~"fetching failed: can't checkout target");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-01-16 21:59:37 +10:00
|
|
|
|
2013-01-17 19:05:19 +10:00
|
|
|
true
|
2013-01-16 21:59:37 +10:00
|
|
|
}
|
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
fn prefer(id: ~str, vers: Option<~str>) -> bool {
|
|
|
|
let package = match util::get_pkg(id, vers) {
|
|
|
|
result::Ok(package) => package,
|
|
|
|
result::Err(err) => {
|
|
|
|
util::error(err);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let name = match util::parse_name(package.id) {
|
|
|
|
result::Ok(name) => name,
|
|
|
|
result::Err(err) => {
|
|
|
|
util::error(err);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
util::note(fmt!("preferring %s v%s (%s)", name, package.vers.to_str(),
|
|
|
|
package.id));
|
|
|
|
|
|
|
|
let bin_dir = util::root().push(~"bin");
|
|
|
|
|
|
|
|
for package.bins.each |&bin| {
|
|
|
|
let path = Path(bin);
|
|
|
|
let name = str::split_char(path.file_path().to_str(), '-')[0];
|
|
|
|
let out = bin_dir.push(name);
|
|
|
|
|
|
|
|
util::link_exe(&path, &out);
|
|
|
|
util::note(fmt!("linked %s", out.to_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
util::note(fmt!("preferred %s v%s", name, package.vers.to_str()));
|
|
|
|
|
2013-01-17 19:05:19 +10:00
|
|
|
true
|
|
|
|
}
|
2013-01-16 21:59:37 +10:00
|
|
|
|
2013-01-18 18:31:43 +10:00
|
|
|
fn test() -> bool {
|
2013-01-19 19:59:19 +10:00
|
|
|
let dir = os::getcwd();
|
|
|
|
let script = match PackageScript::parse(dir) {
|
|
|
|
result::Ok(script) => script,
|
|
|
|
result::Err(err) => {
|
|
|
|
util::error(err);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let work_dir = script.work_dir();
|
|
|
|
let test_dir = work_dir.push(~"test");
|
|
|
|
let mut success = true;
|
|
|
|
|
|
|
|
util::need_dir(&work_dir);
|
|
|
|
util::note(fmt!("testing %s v%s (%s)", script.name, script.vers.to_str(),
|
|
|
|
script.id));
|
|
|
|
|
|
|
|
if script.deps.len() >= 1 {
|
|
|
|
util::note(~"installing dependencies");
|
|
|
|
|
|
|
|
for script.deps.each |&dep| {
|
|
|
|
let (url, target) = dep;
|
|
|
|
|
|
|
|
success = self.install(Some(url), target, true);
|
|
|
|
|
|
|
|
if !success { break; }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !success {
|
|
|
|
util::error(fmt!("testing %s v%s failed: a dep wasn't installed",
|
|
|
|
script.name, script.vers.to_str()));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
util::note(~"installed dependencies");
|
|
|
|
}
|
|
|
|
|
|
|
|
for script.crates.each |&crate| {
|
|
|
|
success = self.compile(&work_dir, &dir.push_rel(&Path(crate)), ~[],
|
|
|
|
false, true);
|
|
|
|
|
|
|
|
if !success { break; }
|
|
|
|
}
|
|
|
|
|
|
|
|
if !success {
|
|
|
|
util::error(fmt!("testing %s v%s failed: a crate failed to compile",
|
|
|
|
script.name, script.vers.to_str()));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for os::walk_dir(&test_dir) |test| {
|
|
|
|
util::note(fmt!("running %s", test.to_str()));
|
|
|
|
|
|
|
|
let status = run::run_program(test.to_str(), ~[]);
|
|
|
|
|
|
|
|
if status != 0 {
|
|
|
|
os::set_exit_status(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
util::note(fmt!("tested %s v%s", script.name, script.vers.to_str()));
|
|
|
|
|
2013-01-17 19:05:19 +10:00
|
|
|
true
|
2013-01-16 21:59:37 +10:00
|
|
|
}
|
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
fn uninstall(id: ~str, vers: Option<~str>) -> bool {
|
|
|
|
let package = match util::get_pkg(id, vers) {
|
|
|
|
result::Ok(package) => package,
|
|
|
|
result::Err(err) => {
|
|
|
|
util::error(err);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let name = match util::parse_name(package.id) {
|
|
|
|
result::Ok(name) => name,
|
|
|
|
result::Err(err) => {
|
|
|
|
util::error(err);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
util::note(fmt!("uninstalling %s v%s (%s)", name, package.vers.to_str(),
|
|
|
|
package.id));
|
|
|
|
|
|
|
|
for vec::append(package.bins, package.libs).each |&file| {
|
|
|
|
let path = Path(file);
|
|
|
|
|
|
|
|
if os::path_exists(&path) {
|
|
|
|
if os::remove_file(&path) {
|
|
|
|
util::note(fmt!("removed %s", path.to_str()));
|
|
|
|
} else {
|
|
|
|
util::error(fmt!("could not remove %s", path.to_str()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
util::note(fmt!("uninstalled %s v%s", name, package.vers.to_str()));
|
|
|
|
util::remove_pkg(&package);
|
|
|
|
|
2013-01-17 19:05:19 +10:00
|
|
|
true
|
|
|
|
}
|
2013-01-16 21:59:37 +10:00
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
fn unprefer(id: ~str, vers: Option<~str>) -> bool {
|
|
|
|
let package = match util::get_pkg(id, vers) {
|
|
|
|
result::Ok(package) => package,
|
|
|
|
result::Err(err) => {
|
|
|
|
util::error(err);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let name = match util::parse_name(package.id) {
|
|
|
|
result::Ok(name) => name,
|
|
|
|
result::Err(err) => {
|
|
|
|
util::error(err);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
util::note(fmt!("unpreferring %s v%s (%s)", name, package.vers.to_str(),
|
|
|
|
package.id));
|
|
|
|
|
|
|
|
let bin_dir = util::root().push(~"bin");
|
|
|
|
|
|
|
|
for package.bins.each |&bin| {
|
|
|
|
let path = Path(bin);
|
|
|
|
let name = str::split_char(path.file_path().to_str(), '-')[0];
|
|
|
|
let out = bin_dir.push(name);
|
|
|
|
|
|
|
|
if os::path_exists(&out) {
|
|
|
|
if os::remove_file(&out) {
|
|
|
|
util::note(fmt!("unlinked %s", out.to_str()));
|
|
|
|
} else {
|
|
|
|
util::error(fmt!("could not unlink %s", out.to_str()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
util::note(fmt!("unpreferred %s v%s", name, package.vers.to_str()));
|
|
|
|
|
2013-01-17 19:05:19 +10:00
|
|
|
true
|
2013-01-16 21:59:37 +10:00
|
|
|
}
|
|
|
|
}
|
2013-01-15 23:57:03 +10:00
|
|
|
|
2013-01-14 20:55:47 +10:00
|
|
|
pub fn main() {
|
2013-01-15 23:57:03 +10:00
|
|
|
let args = os::args();
|
2013-01-16 21:59:37 +10:00
|
|
|
let opts = ~[getopts::optflag(~"h"), getopts::optflag(~"help"),
|
2013-01-19 19:59:19 +10:00
|
|
|
getopts::optmulti(~"c"), getopts::optmulti(~"cfg")];
|
2013-01-15 23:57:03 +10:00
|
|
|
let matches = &match getopts::getopts(args, opts) {
|
|
|
|
result::Ok(m) => m,
|
|
|
|
result::Err(f) => {
|
2013-01-19 19:59:19 +10:00
|
|
|
util::error(fmt!("%s", getopts::fail_str(f)));
|
|
|
|
|
|
|
|
return;
|
2013-01-15 23:57:03 +10:00
|
|
|
}
|
|
|
|
};
|
2013-01-16 21:59:37 +10:00
|
|
|
let help = getopts::opt_present(matches, ~"h") ||
|
|
|
|
getopts::opt_present(matches, ~"help");
|
2013-01-19 19:59:19 +10:00
|
|
|
let cfg = vec::append(getopts::opt_strs(matches, ~"cfg"),
|
2013-01-16 21:59:37 +10:00
|
|
|
getopts::opt_strs(matches, ~"c"));
|
2013-01-15 23:57:03 +10:00
|
|
|
let mut args = copy matches.free;
|
|
|
|
|
|
|
|
args.shift();
|
2013-01-14 20:55:47 +10:00
|
|
|
|
2013-01-15 23:57:03 +10:00
|
|
|
if (args.len() < 1) {
|
|
|
|
return usage::general();
|
|
|
|
}
|
|
|
|
|
2013-01-16 21:59:37 +10:00
|
|
|
let cmd = args.shift();
|
2013-01-15 23:57:03 +10:00
|
|
|
|
2013-01-16 21:59:37 +10:00
|
|
|
if !util::is_cmd(cmd) {
|
2013-01-15 23:57:03 +10:00
|
|
|
return usage::general();
|
|
|
|
} else if help {
|
2013-01-16 21:59:37 +10:00
|
|
|
return match cmd {
|
2013-01-15 23:57:03 +10:00
|
|
|
~"build" => usage::build(),
|
|
|
|
~"clean" => usage::clean(),
|
|
|
|
~"install" => usage::install(),
|
|
|
|
~"prefer" => usage::prefer(),
|
|
|
|
~"test" => usage::test(),
|
|
|
|
~"uninstall" => usage::uninstall(),
|
|
|
|
~"unprefer" => usage::unprefer(),
|
|
|
|
_ => usage::general()
|
2013-01-16 21:59:37 +10:00
|
|
|
};
|
2013-01-15 23:57:03 +10:00
|
|
|
}
|
|
|
|
|
2013-01-19 19:59:19 +10:00
|
|
|
let mut cfg_specs = ~[];
|
|
|
|
|
|
|
|
for cfg.each |s| {
|
|
|
|
cfg_specs.push(attr::mk_word_item(/*bad*/copy *s));
|
|
|
|
}
|
|
|
|
|
2013-01-16 21:59:37 +10:00
|
|
|
Ctx {
|
2013-01-19 19:59:19 +10:00
|
|
|
cfg: cfg_specs,
|
|
|
|
mut dep_cache: LinearMap()
|
2013-01-17 19:05:19 +10:00
|
|
|
}.run(cmd, args);
|
2013-01-14 20:55:47 +10:00
|
|
|
}
|
2013-01-15 23:57:03 +10:00
|
|
|
|
|
|
|
pub use Crate = api::Crate;
|
|
|
|
pub use build = api::build;
|
|
|
|
pub use util = api::util;
|