Skip to content
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

Implement cargo check #2834

Closed
wants to merge 1 commit 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
5 changes: 3 additions & 2 deletions src/bin/bench.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cargo::core::Workspace;
use cargo::ops;
use cargo::util::{CliResult, CliError, Human, Config, human};
use cargo::util::important_paths::{find_root_manifest_for_wd};
use cargo::util::important_paths::find_root_manifest_for_wd;

#[derive(RustcDecodable)]
pub struct Options {
Expand Down Expand Up @@ -89,6 +89,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
&options.flag_bench),
target_rustdoc_args: None,
target_rustc_args: None,
compile_check: false,
},
};

Expand All @@ -99,7 +100,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
Some(err) => {
Err(match err.exit.as_ref().and_then(|e| e.code()) {
Some(i) => CliError::new(human("bench failed"), i),
None => CliError::new(Box::new(Human(err)), 101)
None => CliError::new(Box::new(Human(err)), 101),
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/bin/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::env;
use cargo::core::Workspace;
use cargo::ops::CompileOptions;
use cargo::ops;
use cargo::util::important_paths::{find_root_manifest_for_wd};
use cargo::util::important_paths::find_root_manifest_for_wd;
use cargo::util::{CliResult, Config};

#[derive(RustcDecodable)]
Expand Down Expand Up @@ -85,6 +85,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
&options.flag_bench),
target_rustdoc_args: None,
target_rustc_args: None,
compile_check: false,
};

let ws = try!(Workspace::new(&root, config));
Expand Down
80 changes: 42 additions & 38 deletions src/bin/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ extern crate env_logger;
extern crate git2_curl;
extern crate rustc_serialize;
extern crate toml;
#[macro_use] extern crate log;
#[macro_use]
extern crate log;

use std::collections::BTreeSet;
use std::env;
use std::fs;
use std::path::{Path,PathBuf};
use std::path::{Path, PathBuf};

use cargo::core::shell::Verbosity;
use cargo::execute_main_without_stdin;
Expand Down Expand Up @@ -47,6 +48,7 @@ Options:

Some common cargo commands are (see all commands with --list):
build Compile the current project
check Fast compilation to report errors without creating artifacts
clean Remove the target directory
doc Build this project's and its dependencies' documentation
new Create a new cargo project
Expand All @@ -71,6 +73,7 @@ macro_rules! each_subcommand{
($mac:ident) => {
$mac!(bench);
$mac!(build);
$mac!(check);
$mac!(clean);
$mac!(doc);
$mac!(fetch);
Expand Down Expand Up @@ -112,30 +115,31 @@ each_subcommand!(declare_mod);
on this top-level information.
*/
fn execute(flags: Flags, config: &Config) -> CliResult<Option<()>> {
try!(config.configure_shell(flags.flag_verbose,
flags.flag_quiet,
&flags.flag_color));
try!(config.configure_shell(flags.flag_verbose, flags.flag_quiet, &flags.flag_color));

init_git_transports(config);
cargo::util::job::setup();

if flags.flag_version {
println!("{}", cargo::version());
return Ok(None)
return Ok(None);
}

if flags.flag_list {
println!("Installed Commands:");
for command in list_commands(config) {
println!(" {}", command);
};
return Ok(None)
}
return Ok(None);
}

if let Some(ref code) = flags.flag_explain {
try!(process(config.rustc()).arg("--explain").arg(code).exec()
.map_err(human));
return Ok(None)
try!(process(config.rustc())
.arg("--explain")
.arg(code)
.exec()
.map_err(human));
return Ok(None);
}

let args = match &flags.arg_command[..] {
Expand All @@ -145,23 +149,20 @@ fn execute(flags: Flags, config: &Config) -> CliResult<Option<()>> {
"" | "help" if flags.arg_args.is_empty() => {
config.shell().set_verbosity(Verbosity::Verbose);
let args = &["cargo".to_string(), "-h".to_string()];
let r = cargo::call_main_without_stdin(execute, config, USAGE, args,
false);
let r = cargo::call_main_without_stdin(execute, config, USAGE, args, false);
cargo::process_executed(r, &mut config.shell());
return Ok(None)
return Ok(None);
}

// For `cargo help -h` and `cargo help --help`, print out the help
// message for `cargo help`
"help" if flags.arg_args[0] == "-h" ||
flags.arg_args[0] == "--help" => {
"help" if flags.arg_args[0] == "-h" || flags.arg_args[0] == "--help" => {
vec!["cargo".to_string(), "help".to_string(), "-h".to_string()]
}

// For `cargo help foo`, print out the usage message for the specified
// subcommand by executing the command with the `-h` flag.
"help" => vec!["cargo".to_string(), flags.arg_args[0].clone(),
"-h".to_string()],
"help" => vec!["cargo".to_string(), flags.arg_args[0].clone(), "-h".to_string()],

// For all other invocations, we're of the form `cargo foo args...`. We
// use the exact environment arguments to preserve tokens like `--` for
Expand Down Expand Up @@ -190,29 +191,32 @@ fn find_closest(config: &Config, cmd: &str) -> Option<String> {
let cmds = list_commands(config);
// Only consider candidates with a lev_distance of 3 or less so we don't
// suggest out-of-the-blue options.
let mut filtered = cmds.iter().map(|c| (lev_distance(&c, cmd), c))
.filter(|&(d, _)| d < 4)
.collect::<Vec<_>>();
let mut filtered = cmds.iter()
.map(|c| (lev_distance(&c, cmd), c))
.filter(|&(d, _)| d < 4)
.collect::<Vec<_>>();
filtered.sort_by(|a, b| a.0.cmp(&b.0));
filtered.get(0).map(|slot| slot.1.clone())
}

fn execute_subcommand(config: &Config,
cmd: &str,
args: &[String]) -> CliResult<()> {
fn execute_subcommand(config: &Config, cmd: &str, args: &[String]) -> CliResult<()> {
let command_exe = format!("cargo-{}{}", cmd, env::consts::EXE_SUFFIX);
let path = search_directories(config)
.iter()
.map(|dir| dir.join(&command_exe))
.find(|file| is_executable(file));
.iter()
.map(|dir| dir.join(&command_exe))
.find(|file| is_executable(file));
let command = match path {
Some(command) => command,
None => {
return Err(human(match find_closest(config, cmd) {
Some(closest) => format!("no such subcommand: `{}`\n\n\t\
Did you mean `{}`?\n", cmd, closest),
None => format!("no such subcommand: `{}`", cmd)
}).into())
Some(closest) => {
format!("no such subcommand: `{}`\n\n\tDid you mean `{}`?\n",
cmd,
closest)
}
None => format!("no such subcommand: `{}`", cmd),
})
.into())
}
};
let err = match util::process(&command).args(&args[1..]).exec() {
Expand All @@ -236,16 +240,16 @@ fn list_commands(config: &Config) -> BTreeSet<String> {
for dir in search_directories(config) {
let entries = match fs::read_dir(dir) {
Ok(entries) => entries,
_ => continue
_ => continue,
};
for entry in entries.filter_map(|e| e.ok()) {
let path = entry.path();
let filename = match path.file_name().and_then(|s| s.to_str()) {
Some(filename) => filename,
_ => continue
_ => continue,
};
if !filename.starts_with(prefix) || !filename.ends_with(suffix) {
continue
continue;
}
if is_executable(entry.path()) {
let end = filename.len() - suffix.len();
Expand All @@ -264,9 +268,9 @@ fn list_commands(config: &Config) -> BTreeSet<String> {
#[cfg(unix)]
fn is_executable<P: AsRef<Path>>(path: P) -> bool {
use std::os::unix::prelude::*;
fs::metadata(path).map(|metadata| {
metadata.is_file() && metadata.permissions().mode() & 0o111 != 0
}).unwrap_or(false)
fs::metadata(path)
.map(|metadata| metadata.is_file() && metadata.permissions().mode() & 0o111 != 0)
.unwrap_or(false)
}
#[cfg(windows)]
fn is_executable<P: AsRef<Path>>(path: P) -> bool {
Expand All @@ -287,7 +291,7 @@ fn init_git_transports(config: &Config) {
// case. The custom transport, however, is not as well battle-tested.
match cargo::ops::http_proxy_exists(config) {
Ok(true) => {}
_ => return
_ => return,
}

let handle = match cargo::ops::http_handle(config) {
Expand Down
92 changes: 92 additions & 0 deletions src/bin/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use std::env;

use cargo::core::Workspace;
use cargo::ops::CompileOptions;
use cargo::ops;
use cargo::util::important_paths::find_root_manifest_for_wd;
use cargo::util::{CliResult, Config};

#[derive(RustcDecodable)]
pub struct Options {
flag_package: Vec<String>,
flag_jobs: Option<u32>,
flag_features: Vec<String>,
flag_no_default_features: bool,
flag_target: Option<String>,
flag_manifest_path: Option<String>,
flag_verbose: u32,
flag_quiet: Option<bool>,
flag_color: Option<String>,
flag_lib: bool,
flag_bin: Vec<String>,
flag_example: Vec<String>,
flag_test: Vec<String>,
flag_bench: Vec<String>,
}

pub const USAGE: &'static str = "
Compile a local package to check for compilation errors without creating an artifact

Usage:
cargo check [options]

Options:
-h, --help Print this message
-p SPEC, --package SPEC ... Package to build
-j N, --jobs N Number of parallel jobs, defaults to # of CPUs
--lib Build only this package's library
--bin NAME Build only the specified binary
--example NAME Build only the specified example
--test NAME Build only the specified test target
--bench NAME Build only the specified benchmark target
--features FEATURES Space-separated list of features to also build
--no-default-features Do not build the `default` feature
--target TRIPLE Build for the target triple
--manifest-path PATH Path to the manifest to compile
-v, --verbose ... Use verbose output
-q, --quiet No output printed to stdout
--color WHEN Coloring: auto, always, never

If the --package argument is given, then SPEC is a package id specification
which indicates which package should be built. If it is not given, then the
current package is built. For more information on SPEC and its format, see the
`cargo help pkgid` command.

Compilation can be configured via the use of profiles which are configured in
the manifest. The default profile for this command is `dev`, but passing
the --release flag will use the `release` profile instead.
";

pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
debug!("executing; cmd=cargo-check; args={:?}",
env::args().collect::<Vec<_>>());
try!(config.configure_shell(options.flag_verbose,
options.flag_quiet,
&options.flag_color));

let root = try!(find_root_manifest_for_wd(options.flag_manifest_path, config.cwd()));

let opts = CompileOptions {
config: config,
jobs: options.flag_jobs,
target: options.flag_target.as_ref().map(|t| &t[..]),
features: &options.flag_features,
no_default_features: options.flag_no_default_features,
spec: &options.flag_package,
exec_engine: None,
mode: ops::CompileMode::Build,
release: false,
filter: ops::CompileFilter::new(options.flag_lib,
&options.flag_bin,
&options.flag_test,
&options.flag_example,
&options.flag_bench),
target_rustdoc_args: None,
target_rustc_args: None,
compile_check: true,
};

let ws = try!(Workspace::new(&root, config));
try!(ops::compile(&ws, &opts));
Ok(None)
}
7 changes: 3 additions & 4 deletions src/bin/doc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cargo::core::Workspace;
use cargo::ops;
use cargo::util::{CliResult, Config};
use cargo::util::important_paths::{find_root_manifest_for_wd};
use cargo::util::important_paths::find_root_manifest_for_wd;

#[derive(RustcDecodable)]
pub struct Options {
Expand Down Expand Up @@ -77,11 +77,10 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
&empty,
&empty),
release: options.flag_release,
mode: ops::CompileMode::Doc {
deps: !options.flag_no_deps,
},
mode: ops::CompileMode::Doc { deps: !options.flag_no_deps },
target_rustc_args: None,
target_rustdoc_args: None,
compile_check: false,
},
};

Expand Down
11 changes: 8 additions & 3 deletions src/bin/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
exec_engine: None,
mode: ops::CompileMode::Build,
release: !options.flag_debug,
filter: ops::CompileFilter::new(false, &options.flag_bin, &[],
&options.flag_example, &[]),
filter: ops::CompileFilter::new(false, &options.flag_bin, &[], &options.flag_example, &[]),
target_rustc_args: None,
target_rustdoc_args: None,
compile_check: false,
};

let source = if let Some(url) = options.flag_git {
Expand Down Expand Up @@ -136,7 +136,12 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
if options.flag_list {
try!(ops::install_list(root, config));
} else {
try!(ops::install(root, krate, &source, vers, &compile_opts, options.flag_force));
try!(ops::install(root,
krate,
&source,
vers,
&compile_opts,
options.flag_force));
}
Ok(None)
}
Loading