Skip to content

Commit

Permalink
Add --build-profile=PROFILE instead of just --debug ("dev") and --rel…
Browse files Browse the repository at this point in the history
…ease ("release")

Closes: #282
  • Loading branch information
nabijaczleweli committed Nov 3, 2024
1 parent 523bf79 commit 55550ea
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 22 deletions.
9 changes: 8 additions & 1 deletion man/cargo-install-update-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ which may yield, depending on the Cargo version, the following subset of the dat

* whether to use default features,
* additional feature list,
* build profile (equivalent to --debug if "debug", ignored otherwise).
* build profile.

See cargo-install-update(1) for general information.

Expand Down Expand Up @@ -58,10 +58,17 @@ See cargo-install-update(1) for general information.
--debug

Compile in debug mode.
Same as --build-profile dev.

--release

Compile in release mode (default).
Same as --build-profile release.

--build-profile [PROFILE]

Compile with PROFILE
(dev/release/test/bench or defined in $CARGO_DIR/.cargo/config.toml under [profile.PROFILE]).

--install-prereleases

Expand Down
4 changes: 2 additions & 2 deletions src/main-config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ fn actual_main() -> Result<(), i32> {
if let Some(ref t) = cfg.toolchain {
writeln!(out, "Toolchain\t{}", t).unwrap();
}
if let Some(d) = cfg.debug {
writeln!(out, "Debug mode\t{}", d).unwrap();
if let Some(p) = cfg.build_profile.as_deref().or_else(|| cfg.debug.and_then(|d| if d { Some("dev") } else { None })) {
writeln!(out, "Build profile\t{}", p).unwrap();
}
if let Some(ip) = cfg.install_prereleases {
writeln!(out, "Install prereleases\t{}", ip).unwrap();
Expand Down
67 changes: 53 additions & 14 deletions src/ops/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub enum ConfigOperation {
AddFeature(String),
/// Remove the feature from the list of features to compile with.
RemoveFeature(String),
/// Set debug mode being enabled to the specified value.
SetDebugMode(bool),
/// Set build profile (`dev`/`release`/*~/.cargo/config.toml* `[profile.gaming]`/&c.)
SetBuildProfile(Cow<'static, str>),
/// Set allowing to install prereleases to the specified value.
SetInstallPrereleases(bool),
/// Set enforcing Cargo.lock to the specified value.
Expand Down Expand Up @@ -76,8 +76,11 @@ pub struct PackageConfig {
pub default_features: bool,
/// Features to compile the package with.
pub features: BTreeSet<String>,
/// Whether to compile in debug mode.
/// Equivalent to `build_profile = Some("dev")` but binds stronger
pub debug: Option<bool>,
/// The build profile (`test` or `bench` or one from *~/.cargo/config.toml* `[profile.gaming]`); CANNOT be `dev` (`debug =
/// Some(true)`) or `release` (`debug = build_profile = None`)
pub build_profile: Option<Cow<'static, str>>,
/// Whether to install pre-release versions.
pub install_prereleases: Option<bool>,
/// Whether to enforce Cargo.lock versions.
Expand All @@ -98,6 +101,7 @@ impl PartialEq for PackageConfig {
self.default_features /*****/ == other.default_features && // !
self.features /*************/ == other.features && // !
self.debug /****************/ == other.debug && // !
self.build_profile /********/ == other.build_profile && // !
self.install_prereleases /**/ == other.install_prereleases && // !
self.enforce_lock /*********/ == other.enforce_lock && // !
self.respect_binaries /*****/ == other.respect_binaries && // !
Expand Down Expand Up @@ -125,7 +129,7 @@ impl PackageConfig {
/// assert_eq!(PackageConfig::from(&[ConfigOperation::SetToolchain("nightly".to_string()),
/// ConfigOperation::DefaultFeatures(false),
/// ConfigOperation::AddFeature("rustc-serialize".to_string()),
/// ConfigOperation::SetDebugMode(true),
/// ConfigOperation::SetBuildProfile("dev".into()),
/// ConfigOperation::SetInstallPrereleases(false),
/// ConfigOperation::SetEnforceLock(true),
/// ConfigOperation::SetRespectBinaries(true),
Expand All @@ -141,6 +145,7 @@ impl PackageConfig {
/// feats
/// },
/// debug: Some(true),
/// build_profile: None,
/// install_prereleases: Some(false),
/// enforce_lock: Some(true),
/// respect_binaries: Some(true),
Expand Down Expand Up @@ -218,6 +223,9 @@ impl PackageConfig {
}
if let Some(true) = self.debug {
res.push("--debug".into());
} else if let Some(prof) = self.build_profile.as_ref() {
res.push("--profile".into());
res.push(prof.clone());
}
res
}
Expand Down Expand Up @@ -259,6 +267,7 @@ impl PackageConfig {
/// feats
/// },
/// debug: None,
/// build_profile: None,
/// install_prereleases: None,
/// enforce_lock: None,
/// respect_binaries: None,
Expand All @@ -269,7 +278,7 @@ impl PackageConfig {
/// cfg.execute_operations(&[ConfigOperation::RemoveToolchain,
/// ConfigOperation::AddFeature("serde".to_string()),
/// ConfigOperation::RemoveFeature("rustc-serialize".to_string()),
/// ConfigOperation::SetDebugMode(true),
/// ConfigOperation::SetBuildProfile("dev".into()),
/// ConfigOperation::RemoveTargetVersion]);
/// assert_eq!(cfg,
/// PackageConfig {
Expand All @@ -281,6 +290,7 @@ impl PackageConfig {
/// feats
/// },
/// debug: Some(true),
/// build_profile: None,
/// install_prereleases: None,
/// enforce_lock: None,
/// respect_binaries: None,
Expand All @@ -298,20 +308,24 @@ impl PackageConfig {
}

fn execute_operation(&mut self, op: &ConfigOperation) {
match *op {
match op {
ConfigOperation::SetToolchain(ref tchn) => self.toolchain = Some(tchn.clone()),
ConfigOperation::RemoveToolchain => self.toolchain = None,
ConfigOperation::DefaultFeatures(f) => self.default_features = f,
ConfigOperation::DefaultFeatures(f) => self.default_features = *f,
ConfigOperation::AddFeature(ref feat) => {
self.features.insert(feat.clone());
}
ConfigOperation::RemoveFeature(ref feat) => {
self.features.remove(feat);
}
ConfigOperation::SetDebugMode(d) => self.debug = Some(d),
ConfigOperation::SetInstallPrereleases(pr) => self.install_prereleases = Some(pr),
ConfigOperation::SetEnforceLock(el) => self.enforce_lock = Some(el),
ConfigOperation::SetRespectBinaries(rb) => self.respect_binaries = Some(rb),
ConfigOperation::SetBuildProfile(d) => {
self.debug = None;
self.build_profile = Some(d.clone());
self.normalise();
}
ConfigOperation::SetInstallPrereleases(pr) => self.install_prereleases = Some(*pr),
ConfigOperation::SetEnforceLock(el) => self.enforce_lock = Some(*el),
ConfigOperation::SetRespectBinaries(rb) => self.respect_binaries = Some(*rb),
ConfigOperation::SetTargetVersion(ref vr) => self.target_version = Some(vr.clone()),
ConfigOperation::RemoveTargetVersion => self.target_version = None,
ConfigOperation::SetEnvironment(ref var, ref val) => {
Expand Down Expand Up @@ -361,6 +375,7 @@ impl PackageConfig {
/// feats
/// },
/// debug: None,
/// build_profile: None,
/// install_prereleases: None,
/// enforce_lock: None,
/// respect_binaries: None,
Expand Down Expand Up @@ -408,9 +423,33 @@ impl PackageConfig {
}
}
}
for (_, v) in &mut base {
v.normalise();
}
Ok(base)
}

fn normalise(&mut self) {
if self.debug.unwrap_or(false) && self.build_profile.is_none() {
self.build_profile = Some("dev".into());
}

match self.build_profile.as_deref().unwrap_or("release") {
"dev" => {
self.debug = Some(true);
self.build_profile = None;
}
"release" => {
self.debug = None;
self.build_profile = None;
}
_ => {
self.debug = None;
// self.build_profile unchanged
}
}
}

fn cargo2_package_config(mut blob: json::Object) -> PackageConfig {
let mut ret = PackageConfig::default();
ret.from_transient = true;
Expand All @@ -429,9 +468,7 @@ impl PackageConfig {
}
// Nothing to parse "all_features" into
if let Some(json::Value::String(prof)) = blob.get("profile") {
if prof == "debug" {
ret.debug = Some(true);
}
ret.build_profile = Some(prof.clone().into_owned().into());
}
// Nothing to parse PackageConfig::install_prereleases from
// Nothing to parse PackageConfig::enforce_lock from
Expand Down Expand Up @@ -465,6 +502,7 @@ impl PackageConfig {
/// feats
/// },
/// debug: None,
/// build_profile: None,
/// install_prereleases: None,
/// enforce_lock: None,
/// respect_binaries: None,
Expand Down Expand Up @@ -492,6 +530,7 @@ impl Default for PackageConfig {
default_features: true,
features: BTreeSet::new(),
debug: None,
build_profile: None,
install_prereleases: None,
enforce_lock: None,
respect_binaries: None,
Expand Down
12 changes: 7 additions & 5 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ impl ConfigOptions {
Arg::from_usage("-d --default-features=[DEFAULT_FEATURES] 'Whether to allow default features'")
.possible_values(&["1", "yes", "true", "0", "no", "false"])
.hide_possible_values(true),
Arg::from_usage("--debug 'Compile the package in debug mode'").conflicts_with("release"),
Arg::from_usage("--release 'Compile the package in release mode'").conflicts_with("debug"),
Arg::from_usage("--debug 'Compile the package in debug (\"dev\") mode'").conflicts_with("release").conflicts_with("build-profile"),
Arg::from_usage("--release 'Compile the package in release mode'").conflicts_with("debug").conflicts_with("build-profile"),
Arg::from_usage("--build-profile=[PROFILE] 'Compile the package in the given profile'").conflicts_with("debug").conflicts_with("release"),
Arg::from_usage("--install-prereleases 'Install prerelease versions'").conflicts_with("no-install-prereleases"),
Arg::from_usage("--no-install-prereleases 'Filter out prerelease versions'").conflicts_with("install-prereleases"),
Arg::from_usage("--enforce-lock 'Require Cargo.lock to be up to date'").conflicts_with("no-enforce-lock"),
Expand Down Expand Up @@ -223,9 +224,10 @@ impl ConfigOptions {
.chain(matches.values_of("feature").into_iter().flatten().map(str::to_string).map(ConfigOperation::AddFeature))
.chain(matches.values_of("no-feature").into_iter().flatten().map(str::to_string).map(ConfigOperation::RemoveFeature))
.chain(matches.value_of("default-features").map(|d| ["1", "yes", "true"].contains(&d)).map(ConfigOperation::DefaultFeatures).into_iter())
.chain(match (matches.is_present("debug"), matches.is_present("release")) {
(true, _) => Some(ConfigOperation::SetDebugMode(true)),
(_, true) => Some(ConfigOperation::SetDebugMode(false)),
.chain(match (matches.is_present("debug"), matches.is_present("release"), matches.value_of("build-profile")) {
(true, _, _) => Some(ConfigOperation::SetBuildProfile("dev".into())),
(_, true, _) => Some(ConfigOperation::SetBuildProfile("release".into())),
(_, _, Some(prof)) => Some(ConfigOperation::SetBuildProfile(prof.to_string().into())),
_ => None,
})
.chain(match (matches.is_present("install-prereleases"), matches.is_present("no-install-prereleases")) {
Expand Down

0 comments on commit 55550ea

Please sign in to comment.