Skip to content

Commit 16f38c1

Browse files
committed
Auto merge of #3296 - nrc:check, r=alexcrichton
cargo check ~~This is not finished - the big omission is no tests, and it needs some more testing too. It also requires rust-lang/rust#37681 However, this is my first non-trivial Cargo patch, so I'd like to get some early feedback. r? @alexcrichton and cc @rust-lang/tools since this adds a new Cargo sub-command (although we have previously discussed and approved the idea in a tools meeting).
2 parents f464d00 + 5d18e37 commit 16f38c1

File tree

13 files changed

+574
-46
lines changed

13 files changed

+574
-46
lines changed

src/bin/cargo.rs

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Options:
5151
5252
Some common cargo commands are (see all commands with --list):
5353
build Compile the current project
54+
check Analyze the current project and report errors, but don't build object files
5455
clean Remove the target directory
5556
doc Build this project's and its dependencies' documentation
5657
new Create a new cargo project
@@ -75,6 +76,7 @@ macro_rules! each_subcommand{
7576
($mac:ident) => {
7677
$mac!(bench);
7778
$mac!(build);
79+
$mac!(check);
7880
$mac!(clean);
7981
$mac!(doc);
8082
$mac!(fetch);

src/bin/check.rs

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
use std::env;
2+
3+
use cargo::core::Workspace;
4+
use cargo::ops::{self, CompileOptions, MessageFormat};
5+
use cargo::util::important_paths::{find_root_manifest_for_wd};
6+
use cargo::util::{CliResult, Config};
7+
8+
#[derive(RustcDecodable)]
9+
pub struct Options {
10+
flag_package: Vec<String>,
11+
flag_jobs: Option<u32>,
12+
flag_features: Vec<String>,
13+
flag_all_features: bool,
14+
flag_no_default_features: bool,
15+
flag_target: Option<String>,
16+
flag_manifest_path: Option<String>,
17+
flag_verbose: u32,
18+
flag_quiet: Option<bool>,
19+
flag_color: Option<String>,
20+
flag_message_format: MessageFormat,
21+
flag_release: bool,
22+
flag_lib: bool,
23+
flag_bin: Vec<String>,
24+
flag_example: Vec<String>,
25+
flag_test: Vec<String>,
26+
flag_bench: Vec<String>,
27+
flag_locked: bool,
28+
flag_frozen: bool,
29+
}
30+
31+
pub const USAGE: &'static str = "
32+
Check a local package and all of its dependencies for errors
33+
34+
Usage:
35+
cargo check [options]
36+
37+
Options:
38+
-h, --help Print this message
39+
-p SPEC, --package SPEC ... Package to check
40+
-j N, --jobs N Number of parallel jobs, defaults to # of CPUs
41+
--lib Check only this package's library
42+
--bin NAME Check only the specified binary
43+
--example NAME Check only the specified example
44+
--test NAME Check only the specified test target
45+
--bench NAME Check only the specified benchmark target
46+
--release Check artifacts in release mode, with optimizations
47+
--features FEATURES Space-separated list of features to also check
48+
--all-features Check all available features
49+
--no-default-features Do not check the `default` feature
50+
--target TRIPLE Check for the target triple
51+
--manifest-path PATH Path to the manifest to compile
52+
-v, --verbose ... Use verbose output
53+
-q, --quiet No output printed to stdout
54+
--color WHEN Coloring: auto, always, never
55+
--message-format FMT Error format: human, json [default: human]
56+
--frozen Require Cargo.lock and cache are up to date
57+
--locked Require Cargo.lock is up to date
58+
59+
If the --package argument is given, then SPEC is a package id specification
60+
which indicates which package should be built. If it is not given, then the
61+
current package is built. For more information on SPEC and its format, see the
62+
`cargo help pkgid` command.
63+
64+
Compilation can be configured via the use of profiles which are configured in
65+
the manifest. The default profile for this command is `dev`, but passing
66+
the --release flag will use the `release` profile instead.
67+
";
68+
69+
pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
70+
debug!("executing; cmd=cargo-check; args={:?}",
71+
env::args().collect::<Vec<_>>());
72+
config.configure(options.flag_verbose,
73+
options.flag_quiet,
74+
&options.flag_color,
75+
options.flag_frozen,
76+
options.flag_locked)?;
77+
78+
let root = find_root_manifest_for_wd(options.flag_manifest_path, config.cwd())?;
79+
80+
let opts = CompileOptions {
81+
config: config,
82+
jobs: options.flag_jobs,
83+
target: options.flag_target.as_ref().map(|t| &t[..]),
84+
features: &options.flag_features,
85+
all_features: options.flag_all_features,
86+
no_default_features: options.flag_no_default_features,
87+
spec: ops::Packages::Packages(&options.flag_package),
88+
mode: ops::CompileMode::Check,
89+
release: options.flag_release,
90+
filter: ops::CompileFilter::new(options.flag_lib,
91+
&options.flag_bin,
92+
&options.flag_test,
93+
&options.flag_example,
94+
&options.flag_bench),
95+
message_format: options.flag_message_format,
96+
target_rustdoc_args: None,
97+
target_rustc_args: None,
98+
};
99+
100+
let ws = Workspace::new(&root, config)?;
101+
ops::compile(&ws, &opts)?;
102+
Ok(None)
103+
}

src/cargo/core/manifest.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl LibKind {
7171
"lib" => LibKind::Lib,
7272
"rlib" => LibKind::Rlib,
7373
"dylib" => LibKind::Dylib,
74-
"procc-macro" => LibKind::ProcMacro,
74+
"proc-macro" => LibKind::ProcMacro,
7575
s => LibKind::Other(s.to_string()),
7676
}
7777
}
@@ -136,6 +136,7 @@ pub struct Profile {
136136
pub test: bool,
137137
pub doc: bool,
138138
pub run_custom_build: bool,
139+
pub check: bool,
139140
pub panic: Option<String>,
140141
}
141142

@@ -168,6 +169,7 @@ pub struct Profiles {
168169
pub bench_deps: Profile,
169170
pub doc: Profile,
170171
pub custom_build: Profile,
172+
pub check: Profile,
171173
}
172174

173175
/// Information about a binary, a library, an example, etc. that is part of the
@@ -531,6 +533,13 @@ impl Profile {
531533
..Profile::default_dev()
532534
}
533535
}
536+
537+
pub fn default_check() -> Profile {
538+
Profile {
539+
check: true,
540+
..Profile::default_dev()
541+
}
542+
}
534543
}
535544

536545
impl Default for Profile {
@@ -547,6 +556,7 @@ impl Default for Profile {
547556
test: false,
548557
doc: false,
549558
run_custom_build: false,
559+
check: false,
550560
panic: None,
551561
}
552562
}
@@ -560,6 +570,8 @@ impl fmt::Display for Profile {
560570
write!(f, "Profile(doc)")
561571
} else if self.run_custom_build {
562572
write!(f, "Profile(run)")
573+
} else if self.check {
574+
write!(f, "Profile(check)")
563575
} else {
564576
write!(f, "Profile(build)")
565577
}

src/cargo/core/workspace.rs

+1
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ impl<'cfg> Workspace<'cfg> {
450450
bench_deps: Profile::default_release(),
451451
doc: Profile::default_doc(),
452452
custom_build: Profile::default_custom_build(),
453+
check: Profile::default_check(),
453454
};
454455

455456
for pkg in self.members().filter(|p| p.manifest_path() != root_manifest) {

src/cargo/ops/cargo_clean.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ pub fn clean(ws: &Workspace, opts: &CleanOptions) -> CargoResult<()> {
5454
for kind in [Kind::Host, Kind::Target].iter() {
5555
let Profiles {
5656
ref release, ref dev, ref test, ref bench, ref doc,
57-
ref custom_build, ref test_deps, ref bench_deps,
57+
ref custom_build, ref test_deps, ref bench_deps, ref check
5858
} = *profiles;
5959
let profiles = [release, dev, test, bench, doc, custom_build,
60-
test_deps, bench_deps];
60+
test_deps, bench_deps, check];
6161
for profile in profiles.iter() {
6262
units.push(Unit {
6363
pkg: &pkg,

src/cargo/ops/cargo_compile.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@ pub struct CompileOptions<'a> {
6565
pub target_rustc_args: Option<&'a [String]>,
6666
}
6767

68-
#[derive(Clone, Copy, PartialEq)]
68+
#[derive(Clone, Copy, PartialEq, Debug)]
6969
pub enum CompileMode {
7070
Test,
7171
Build,
72+
Check,
7273
Bench,
7374
Doc { deps: bool },
7475
}
@@ -343,6 +344,7 @@ fn generate_targets<'a>(pkg: &'a Package,
343344
CompileMode::Test => test,
344345
CompileMode::Bench => &profiles.bench,
345346
CompileMode::Build => build,
347+
CompileMode::Check => &profiles.check,
346348
CompileMode::Doc { .. } => &profiles.doc,
347349
};
348350
match *filter {
@@ -374,7 +376,7 @@ fn generate_targets<'a>(pkg: &'a Package,
374376
}
375377
Ok(base)
376378
}
377-
CompileMode::Build => {
379+
CompileMode::Build | CompileMode::Check => {
378380
Ok(pkg.targets().iter().filter(|t| {
379381
t.is_bin() || t.is_lib()
380382
}).map(|t| (t, profile)).collect())

0 commit comments

Comments
 (0)