Skip to content

Support for custom Cargo profiles #5326

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 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: 5 additions & 0 deletions src/bin/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ pub trait AppExt: Sized {
self._arg(opt("target", target).value_name("TRIPLE"))
}

fn arg_profile(self, profile: &'static str) -> Self {
self._arg(opt("profile", profile).value_name("PROFILE-NAME"))
}

fn arg_manifest_path(self) -> Self {
self._arg(opt("manifest-path", "Path to Cargo.toml").value_name("PATH"))
}
Expand Down Expand Up @@ -272,6 +276,7 @@ pub trait ArgMatchesExt {
no_default_features: self._is_present("no-default-features"),
spec,
mode,
profile: self._value_of("profile").map(|s| s.to_string()),
release: self._is_present("release"),
filter: CompileFilter::new(
self._is_present("lib"),
Expand Down
1 change: 1 addition & 0 deletions src/bin/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub fn cli() -> App {
.arg_release("Build artifacts in release mode, with optimizations")
.arg_features()
.arg_target_triple("Build for the target triple")
.arg_profile("Build artifacts with the specified custom profile")
.arg(opt("out-dir", "Copy final artifacts to this directory").value_name("PATH"))
.arg_manifest_path()
.arg_message_format()
Expand Down
1 change: 1 addition & 0 deletions src/bin/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn cli() -> App {
.arg_release("Build artifacts in release mode, with optimizations")
.arg_features()
.arg_target_triple("Build for the target triple")
.arg_profile("Build artifacts with the specified custom profile")
.arg_manifest_path()
.arg_message_format()
.after_help(
Expand Down
1 change: 1 addition & 0 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ pub struct Profiles {
pub check: Profile,
pub check_test: Profile,
pub doctest: Profile,
pub custom: HashMap<String, Profile>,
}

/// Information about a binary, a library, an example, etc. that is part of the
Expand Down
1 change: 1 addition & 0 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ impl<'cfg> Workspace<'cfg> {
check: Profile::default_check(),
check_test: Profile::default_check_test(),
doctest: Profile::default_doctest(),
custom: HashMap::new(),
};

for pkg in self.members()
Expand Down
10 changes: 9 additions & 1 deletion src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub fn clean(ws: &Workspace, opts: &CleanOptions) -> CargoResult<()> {
ref check,
ref check_test,
ref doctest,
ref custom,
} = *profiles;
let profiles = [
release,
Expand All @@ -73,13 +74,19 @@ pub fn clean(ws: &Workspace, opts: &CleanOptions) -> CargoResult<()> {
check_test,
doctest,
];
for profile in profiles.iter() {
let mut add = |profile| {
units.push(Unit {
pkg,
target,
profile,
kind: *kind,
});
};
for profile in profiles.iter() {
add(profile);
}
for profile in custom.values() {
add(profile);
}
}
}
Expand All @@ -98,6 +105,7 @@ pub fn clean(ws: &Workspace, opts: &CleanOptions) -> CargoResult<()> {
..BuildConfig::default()
},
profiles,
&None,
None,
&units,
)?;
Expand Down
58 changes: 46 additions & 12 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub struct CompileOptions<'a> {
pub filter: CompileFilter,
/// Whether this is a release build or not
pub release: bool,
/// Custom profile
pub profile: Option<String>,
/// Mode for this compile.
pub mode: CompileMode,
/// `--error_format` flag for the compiler.
Expand Down Expand Up @@ -87,6 +89,7 @@ impl<'a> CompileOptions<'a> {
filter: CompileFilter::Default {
required_features_filterable: false,
},
profile: None,
message_format: MessageFormat::Human,
target_rustdoc_args: None,
target_rustc_args: None,
Expand Down Expand Up @@ -241,8 +244,10 @@ pub fn compile_ws<'a>(
ref target_rustdoc_args,
ref target_rustc_args,
ref export_dir,
ref profile,
} = *options;

let profile_name = profile;
let target = match target {
&Some(ref target) if target.ends_with(".json") => {
let path = Path::new(target)
Expand Down Expand Up @@ -292,8 +297,15 @@ pub fn compile_ws<'a>(
(&Some(ref args), _) => {
let all_features =
resolve_all_features(&resolve_with_overrides, to_builds[0].package_id());
let targets =
generate_targets(to_builds[0], profiles, mode, filter, &all_features, release)?;
let targets = generate_targets(
to_builds[0],
profiles,
profile_name,
mode,
filter,
&all_features,
release,
)?;
if targets.len() == 1 {
let (target, profile) = targets[0];
let mut profile = profile.clone();
Expand All @@ -310,25 +322,37 @@ pub fn compile_ws<'a>(
(&None, &Some(ref args)) => {
let all_features =
resolve_all_features(&resolve_with_overrides, to_builds[0].package_id());
let targets =
generate_targets(to_builds[0], profiles, mode, filter, &all_features, release)?;
let targets = generate_targets(
to_builds[0],
profiles,
profile_name,
mode,
filter,
&all_features,
release,
)?;
if targets.len() == 1 {
let (target, profile) = targets[0];
let mut profile = profile.clone();
profile.rustdoc_args = Some(args.to_vec());
general_targets.push((target, profile));
} else {
bail!(
"extra arguments to `rustdoc` can only be passed to one \
target, consider filtering\nthe package by passing e.g. \
`--lib` or `--bin NAME` to specify a single target"
)
bail!("extra arguments to `rustdoc` can only be passed to one \
target, consider filtering\nthe package by passing e.g. \
`--lib` or `--bin NAME` to specify a single target")
}
}
(&None, &None) => for &to_build in to_builds.iter() {
let all_features = resolve_all_features(&resolve_with_overrides, to_build.package_id());
let targets =
generate_targets(to_build, profiles, mode, filter, &all_features, release)?;
let targets = generate_targets(
to_build,
profiles,
profile_name,
mode,
filter,
&all_features,
release,
)?;
package_targets.push((to_build, targets));
},
};
Expand Down Expand Up @@ -357,6 +381,7 @@ pub fn compile_ws<'a>(
build_config,
profiles,
export_dir.clone(),
&profile_name,
&exec,
)?
};
Expand Down Expand Up @@ -689,6 +714,7 @@ fn filter_compatible_targets<'a>(
fn generate_targets<'a>(
pkg: &'a Package,
profiles: &'a Profiles,
profile_name: &Option<String>,
mode: CompileMode,
filter: &CompileFilter,
features: &HashSet<String>,
Expand All @@ -704,7 +730,7 @@ fn generate_targets<'a>(
} else {
&profiles.test
};
let profile = match mode {
let predef_profile = match mode {
CompileMode::Test => test,
CompileMode::Bench => &profiles.bench,
CompileMode::Build => build,
Expand All @@ -714,6 +740,14 @@ fn generate_targets<'a>(
CompileMode::Doctest => &profiles.doctest,
};

let profile = match profile_name {
&None => predef_profile,
&Some(ref name) => profiles
.custom
.get(name)
.expect(format!("Missing profile {}", name).as_ref()),
};

let test_profile = if profile.check {
&profiles.check_test
} else if mode == CompileMode::Build {
Expand Down
1 change: 1 addition & 0 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ fn run_verify(ws: &Workspace, tar: &FileLock, opts: &PackageOpts) -> CargoResult
required_features_filterable: true,
},
release: false,
profile: None,
message_format: ops::MessageFormat::Human,
mode: ops::CompileMode::Build,
target_rustdoc_args: None,
Expand Down
22 changes: 17 additions & 5 deletions src/cargo/ops/cargo_rustc/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub struct Context<'a, 'cfg: 'a> {
target_info: TargetInfo,
host_info: TargetInfo,
profiles: &'a Profiles,
profile_name: Option<String>,
incremental_env: Option<bool>,

unit_dependencies: HashMap<Unit<'a>, Vec<Unit<'a>>>,
Expand All @@ -105,13 +106,17 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
config: &'cfg Config,
build_config: BuildConfig,
profiles: &'a Profiles,
profile_name: &Option<String>,
export_dir: Option<PathBuf>,
units: &[Unit<'a>],
) -> CargoResult<Context<'a, 'cfg>> {
let dest = if build_config.release {
"release"
} else {
"debug"
let dest = match profile_name {
&None => if build_config.release {
"release"
} else {
"debug"
},
&Some(ref s) => s.as_str(),
};
let host_layout = Layout::new(ws, None, dest)?;
let target_layout = match build_config.requested_target.as_ref() {
Expand Down Expand Up @@ -148,6 +153,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
build_state: Arc::new(BuildState::new(&build_config)),
build_config,
fingerprints: HashMap::new(),
profile_name: profile_name.clone(),
profiles,
compiled: HashSet::new(),
build_scripts: HashMap::new(),
Expand Down Expand Up @@ -363,7 +369,13 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
if self.build_config.test {
test
} else {
normal
match &self.profile_name {
&None => normal,
&Some(ref name) => self.profiles
.custom
.get(name)
.expect(format!("Missing profile {}", name).as_ref()),
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/cargo/ops/cargo_rustc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ pub fn compile_targets<'a, 'cfg: 'a>(
build_config: BuildConfig,
profiles: &'a Profiles,
export_dir: Option<PathBuf>,
profile_name: &Option<String>,
exec: &Arc<Executor>,
) -> CargoResult<Compilation<'cfg>> {
let units = pkg_targets
Expand Down Expand Up @@ -172,6 +173,7 @@ pub fn compile_targets<'a, 'cfg: 'a>(
config,
build_config,
profiles,
profile_name,
export_dir,
&units,
)?;
Expand Down
Loading