Skip to content

Commit 8b0561d

Browse files
committed
Auto merge of #6989 - da-x:custom-profile-pr-rfc, r=ehuss
Support for named profiles (RFC 2678) Tracking issue: #6988 Implementation according to the [RFC](https://github.com/rust-lang/rfcs/blob/master/text/2678-named-custom-cargo-profiles.md).
2 parents 8ae8b5e + fad192d commit 8b0561d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1513
-457
lines changed

src/bin/cargo/commands/bench.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,18 @@ Compilation can be customized with the `bench` profile in the manifest.
7373

7474
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
7575
let ws = args.workspace(config)?;
76-
let mut compile_opts = args.compile_options(config, CompileMode::Bench, Some(&ws))?;
76+
let mut compile_opts = args.compile_options(
77+
config,
78+
CompileMode::Bench,
79+
Some(&ws),
80+
ProfileChecking::Checked,
81+
)?;
7782

78-
compile_opts.build_config.release = true;
83+
compile_opts.build_config.profile_kind = args.get_profile_kind(
84+
config,
85+
ProfileKind::Custom("bench".to_owned()),
86+
ProfileChecking::Checked,
87+
)?;
7988

8089
let ops = TestOptions {
8190
no_run: args.is_present("no-run"),

src/bin/cargo/commands/build.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub fn cli() -> App {
2727
"Build all targets",
2828
)
2929
.arg_release("Build artifacts in release mode, with optimizations")
30+
.arg_profile("Build artifacts with the specified profile")
3031
.arg_features()
3132
.arg_target_triple("Build for the target triple")
3233
.arg_target_dir()
@@ -55,7 +56,12 @@ the --release flag will use the `release` profile instead.
5556

5657
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
5758
let ws = args.workspace(config)?;
58-
let mut compile_opts = args.compile_options(config, CompileMode::Build, Some(&ws))?;
59+
let mut compile_opts = args.compile_options(
60+
config,
61+
CompileMode::Build,
62+
Some(&ws),
63+
ProfileChecking::Checked,
64+
)?;
5965

6066
compile_opts.export_dir = args.value_of_path("out-dir", config);
6167
if compile_opts.export_dir.is_some() {

src/bin/cargo/commands/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn cli() -> App {
2727
"Check all targets",
2828
)
2929
.arg_release("Check artifacts in release mode, with optimizations")
30-
.arg(opt("profile", "Profile to build the selected target for").value_name("PROFILE"))
30+
.arg_profile("Check artifacts with the specified profile")
3131
.arg_features()
3232
.arg_target_triple("Check for the target triple")
3333
.arg_target_dir()
@@ -69,7 +69,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
6969
}
7070
};
7171
let mode = CompileMode::Check { test };
72-
let compile_opts = args.compile_options(config, mode, Some(&ws))?;
72+
let compile_opts = args.compile_options(config, mode, Some(&ws), ProfileChecking::Unchecked)?;
7373

7474
ops::compile(&ws, &compile_opts)?;
7575
Ok(())

src/bin/cargo/commands/clean.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub fn cli() -> App {
1111
.arg_target_triple("Target triple to clean output for")
1212
.arg_target_dir()
1313
.arg_release("Whether or not to clean release artifacts")
14+
.arg_profile("Clean artifacts of the specified profile")
1415
.arg_doc("Whether or not to clean just the documentation directory")
1516
.after_help(
1617
"\
@@ -28,7 +29,8 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
2829
config,
2930
spec: values(args, "package"),
3031
target: args.target(),
31-
release: args.is_present("release"),
32+
profile_kind: args.get_profile_kind(config, ProfileKind::Dev, ProfileChecking::Checked)?,
33+
profile_specified: args.is_present("profile") || args.is_present("release"),
3234
doc: args.is_present("doc"),
3335
};
3436
ops::clean(&ws, &opts)?;

src/bin/cargo/commands/clippy.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub fn cli() -> App {
2626
"Check all targets",
2727
)
2828
.arg_release("Check artifacts in release mode, with optimizations")
29+
.arg_profile("Check artifacts with the specified profile")
2930
.arg_features()
3031
.arg_target_triple("Check for the target triple")
3132
.arg_target_dir()
@@ -61,7 +62,8 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
6162
let ws = args.workspace(config)?;
6263

6364
let mode = CompileMode::Check { test: false };
64-
let mut compile_opts = args.compile_options(config, mode, Some(&ws))?;
65+
let mut compile_opts =
66+
args.compile_options(config, mode, Some(&ws), ProfileChecking::Checked)?;
6567

6668
if !config.cli_unstable().unstable_options {
6769
return Err(failure::format_err!(

src/bin/cargo/commands/doc.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub fn cli() -> App {
2424
"Document all binaries",
2525
)
2626
.arg_release("Build artifacts in release mode, with optimizations")
27+
.arg_profile("Build artifacts with the specified profile")
2728
.arg_features()
2829
.arg_target_triple("Build for the target triple")
2930
.arg_target_dir()
@@ -52,7 +53,8 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
5253
let mode = CompileMode::Doc {
5354
deps: !args.is_present("no-deps"),
5455
};
55-
let mut compile_opts = args.compile_options(config, mode, Some(&ws))?;
56+
let mut compile_opts =
57+
args.compile_options(config, mode, Some(&ws), ProfileChecking::Checked)?;
5658
compile_opts.local_rustdoc_args = if args.is_present("document-private-items") {
5759
Some(vec!["--document-private-items".to_string()])
5860
} else {

src/bin/cargo/commands/fix.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn cli() -> App {
2525
"Fix all targets (default)",
2626
)
2727
.arg_release("Fix artifacts in release mode, with optimizations")
28-
.arg(opt("profile", "Profile to build the selected target for").value_name("PROFILE"))
28+
.arg_profile("Build artifacts with the specified profile")
2929
.arg_features()
3030
.arg_target_triple("Fix for the target triple")
3131
.arg_target_dir()
@@ -132,7 +132,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
132132

133133
// Unlike other commands default `cargo fix` to all targets to fix as much
134134
// code as we can.
135-
let mut opts = args.compile_options(config, mode, Some(&ws))?;
135+
let mut opts = args.compile_options(config, mode, Some(&ws), ProfileChecking::Unchecked)?;
136136

137137
let use_clippy = args.is_present("clippy");
138138

src/bin/cargo/commands/install.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub fn cli() -> App {
5151
"Do not save tracking information (unstable)",
5252
))
5353
.arg_features()
54+
.arg_profile("Install artifacts with from the specified profile")
5455
.arg(opt("debug", "Build in debug mode instead of release mode"))
5556
.arg_targets_bins_examples(
5657
"Install only the specified binary",
@@ -115,9 +116,15 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
115116
}
116117

117118
let workspace = args.workspace(config).ok();
118-
let mut compile_opts = args.compile_options(config, CompileMode::Build, workspace.as_ref())?;
119-
120-
compile_opts.build_config.release = !args.is_present("debug");
119+
let mut compile_opts = args.compile_options(
120+
config,
121+
CompileMode::Build,
122+
workspace.as_ref(),
123+
ProfileChecking::Checked,
124+
)?;
125+
126+
compile_opts.build_config.profile_kind =
127+
args.get_profile_kind(config, ProfileKind::Release, ProfileChecking::Checked)?;
121128

122129
let krates = args
123130
.values_of("crate")

src/bin/cargo/commands/run.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub fn cli() -> App {
1818
.arg_package("Package with the target to run")
1919
.arg_jobs()
2020
.arg_release("Build artifacts in release mode, with optimizations")
21+
.arg_profile("Build artifacts with the specified profile")
2122
.arg_features()
2223
.arg_target_triple("Build for the target triple")
2324
.arg_target_dir()
@@ -40,7 +41,12 @@ run. If you're passing arguments to both Cargo and the binary, the ones after
4041
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
4142
let ws = args.workspace(config)?;
4243

43-
let mut compile_opts = args.compile_options(config, CompileMode::Build, Some(&ws))?;
44+
let mut compile_opts = args.compile_options(
45+
config,
46+
CompileMode::Build,
47+
Some(&ws),
48+
ProfileChecking::Checked,
49+
)?;
4450

4551
if !args.is_present("example") && !args.is_present("bin") {
4652
let default_runs: Vec<_> = compile_opts

src/bin/cargo/commands/rustc.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn cli() -> App {
2323
"Build all targets",
2424
)
2525
.arg_release("Build artifacts in release mode, with optimizations")
26-
.arg(opt("profile", "Profile to build the selected target for").value_name("PROFILE"))
26+
.arg_profile("Build artifacts with the specified profile")
2727
.arg_features()
2828
.arg_target_triple("Target triple which compiles will be for")
2929
.arg_target_dir()
@@ -63,7 +63,12 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
6363
return Err(CliError::new(err, 101));
6464
}
6565
};
66-
let mut compile_opts = args.compile_options_for_single_package(config, mode, Some(&ws))?;
66+
let mut compile_opts = args.compile_options_for_single_package(
67+
config,
68+
mode,
69+
Some(&ws),
70+
ProfileChecking::Unchecked,
71+
)?;
6772
let target_args = values(args, "args");
6873
compile_opts.target_rustc_args = if target_args.is_empty() {
6974
None

src/bin/cargo/commands/rustdoc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub fn cli() -> App {
2727
"Build all targets",
2828
)
2929
.arg_release("Build artifacts in release mode, with optimizations")
30+
.arg_profile("Build artifacts with the specified profile")
3031
.arg_features()
3132
.arg_target_triple("Build for the target triple")
3233
.arg_target_dir()
@@ -55,6 +56,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
5556
config,
5657
CompileMode::Doc { deps: false },
5758
Some(&ws),
59+
ProfileChecking::Checked,
5860
)?;
5961
let target_args = values(args, "args");
6062
compile_opts.target_rustdoc_args = if target_args.is_empty() {

src/bin/cargo/commands/test.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub fn cli() -> App {
4747
)
4848
.arg_jobs()
4949
.arg_release("Build artifacts in release mode, with optimizations")
50+
.arg_profile("Build artifacts with the specified profile")
5051
.arg_features()
5152
.arg_target_triple("Build for the target triple")
5253
.arg_target_dir()
@@ -99,7 +100,18 @@ To get the list of all options available for the test binaries use this:
99100
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
100101
let ws = args.workspace(config)?;
101102

102-
let mut compile_opts = args.compile_options(config, CompileMode::Test, Some(&ws))?;
103+
let mut compile_opts = args.compile_options(
104+
config,
105+
CompileMode::Test,
106+
Some(&ws),
107+
ProfileChecking::Checked,
108+
)?;
109+
110+
compile_opts.build_config.profile_kind = args.get_profile_kind(
111+
config,
112+
ProfileKind::Custom("test".to_owned()),
113+
ProfileChecking::Checked,
114+
)?;
103115

104116
// `TESTNAME` is actually an argument of the test binary, but it's
105117
// important, so we explicitly mention it and reconfigure.

src/cargo/core/compiler/build_config.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,32 @@ use crate::core::compiler::{CompileKind, CompileTarget};
66
use crate::util::ProcessBuilder;
77
use crate::util::{CargoResult, Config, RustfixDiagnosticServer};
88

9+
#[derive(Debug, Clone)]
10+
pub enum ProfileKind {
11+
Dev,
12+
Release,
13+
Custom(String),
14+
}
15+
16+
impl ProfileKind {
17+
pub fn name(&self) -> &str {
18+
match self {
19+
ProfileKind::Dev => "dev",
20+
ProfileKind::Release => "release",
21+
ProfileKind::Custom(name) => &name,
22+
}
23+
}
24+
}
25+
926
/// Configuration information for a rustc build.
1027
#[derive(Debug)]
1128
pub struct BuildConfig {
1229
/// The requested kind of compilation for this session
1330
pub requested_kind: CompileKind,
1431
/// Number of rustc jobs to run in parallel.
1532
pub jobs: u32,
16-
/// `true` if we are building for release.
17-
pub release: bool,
33+
/// Build profile
34+
pub profile_kind: ProfileKind,
1835
/// The mode we are compiling in.
1936
pub mode: CompileMode,
2037
/// `true` to print stdout in JSON format (for machine reading).
@@ -77,7 +94,7 @@ impl BuildConfig {
7794
Ok(BuildConfig {
7895
requested_kind,
7996
jobs,
80-
release: false,
97+
profile_kind: ProfileKind::Dev,
8198
mode,
8299
message_format: MessageFormat::Human,
83100
force_rebuild: false,
@@ -102,6 +119,10 @@ impl BuildConfig {
102119
}
103120
}
104121

122+
pub fn profile_name(&self) -> &str {
123+
self.profile_kind.name()
124+
}
125+
105126
pub fn test(&self) -> bool {
106127
self.mode == CompileMode::Test || self.mode == CompileMode::Bench
107128
}

src/cargo/core/compiler/context/mod.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,12 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
298298
export_dir: Option<PathBuf>,
299299
units: &[Unit<'a>],
300300
) -> CargoResult<()> {
301-
let dest = if self.bcx.build_config.release {
302-
"release"
303-
} else {
304-
"debug"
305-
};
306-
let host_layout = Layout::new(self.bcx.ws, None, dest)?;
301+
let profile_kind = &self.bcx.build_config.profile_kind;
302+
let dest = self.bcx.profiles.get_dir_name(profile_kind);
303+
let host_layout = Layout::new(self.bcx.ws, None, &dest)?;
307304
let mut targets = HashMap::new();
308305
if let CompileKind::Target(target) = self.bcx.build_config.requested_kind {
309-
let layout = Layout::new(self.bcx.ws, Some(target), dest)?;
306+
let layout = Layout::new(self.bcx.ws, Some(target), &dest)?;
310307
standard_lib::prepare_sysroot(&layout)?;
311308
targets.insert(target, layout);
312309
}

src/cargo/core/compiler/custom_build.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::str;
66
use std::sync::Arc;
77

88
use crate::core::compiler::job_queue::JobState;
9-
use crate::core::PackageId;
9+
use crate::core::{profiles::ProfileRoot, PackageId};
1010
use crate::util::errors::{CargoResult, CargoResultExt};
1111
use crate::util::machine_message::{self, Message};
1212
use crate::util::{self, internal, paths, profile};
@@ -163,10 +163,9 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoRes
163163
.env("OPT_LEVEL", &unit.profile.opt_level.to_string())
164164
.env(
165165
"PROFILE",
166-
if bcx.build_config.release {
167-
"release"
168-
} else {
169-
"debug"
166+
match unit.profile.root {
167+
ProfileRoot::Release => "release",
168+
ProfileRoot::Debug => "debug",
170169
},
171170
)
172171
.env("HOST", &bcx.host_triple())

src/cargo/core/compiler/job_queue.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use super::job::{
2020
use super::standard_lib;
2121
use super::timings::Timings;
2222
use super::{BuildContext, BuildPlan, CompileMode, Context, Unit};
23+
use crate::core::compiler::ProfileKind;
2324
use crate::core::{PackageId, TargetKind};
2425
use crate::handle_error;
2526
use crate::util;
@@ -41,10 +42,10 @@ pub struct JobQueue<'a, 'cfg> {
4142
compiled: HashSet<PackageId>,
4243
documented: HashSet<PackageId>,
4344
counts: HashMap<PackageId, usize>,
44-
is_release: bool,
4545
progress: Progress<'cfg>,
4646
next_id: u32,
4747
timings: Timings<'a, 'cfg>,
48+
profile_kind: ProfileKind,
4849
}
4950

5051
pub struct JobState<'a> {
@@ -145,10 +146,10 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> {
145146
compiled: HashSet::new(),
146147
documented: HashSet::new(),
147148
counts: HashMap::new(),
148-
is_release: bcx.build_config.release,
149149
progress,
150150
next_id: 0,
151151
timings,
152+
profile_kind: bcx.build_config.profile_kind.clone(),
152153
}
153154
}
154155

@@ -416,15 +417,15 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> {
416417
}
417418
self.progress.clear();
418419

419-
let build_type = if self.is_release { "release" } else { "dev" };
420+
let build_type = self.profile_kind.name();
420421
// NOTE: this may be a bit inaccurate, since this may not display the
421422
// profile for what was actually built. Profile overrides can change
422423
// these settings, and in some cases different targets are built with
423424
// different profiles. To be accurate, it would need to collect a
424425
// list of Units built, and maybe display a list of the different
425426
// profiles used. However, to keep it simple and compatible with old
426427
// behavior, we just display what the base profile is.
427-
let profile = cx.bcx.profiles.base_profile(self.is_release);
428+
let profile = cx.bcx.profiles.base_profile(&self.profile_kind)?;
428429
let mut opt_type = String::from(if profile.opt_level.as_str() == "0" {
429430
"unoptimized"
430431
} else {

0 commit comments

Comments
 (0)