Skip to content

Commit

Permalink
Add --toolchain-version argument
Browse files Browse the repository at this point in the history
This allows the user to specify which toolchain to use to build their
script. This allows for the use of nightly features, features only
stabilized in beta, and even the use of custom-built toolchains if
the need ever arises.
  • Loading branch information
PatchMixolydic committed Apr 26, 2021
1 parent 88c57c3 commit ef1d3eb
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ struct Args {
build_kind: BuildKind,
template: Option<String>,
list_templates: bool,
// This is a String instead of an
// enum since one can have custom
// toolchains (ex. a rustc developer
// will probably have `stage1`).
toolchain_version: Option<String>,

#[cfg(windows)]
install_file_association: bool,
Expand Down Expand Up @@ -235,6 +240,15 @@ fn parse_args() -> Args {
.takes_value(true)
.requires("expr")
)
.arg(Arg::new("toolchain-version")
.about("Build the script using the given toolchain version.")
.long("toolchain-version")
// "channel"
.short('c')
.takes_value(true)
// FIXME: remove if benchmarking is stabilized
.conflicts_with("bench")
)
.arg(Arg::new("list-templates")
.about("List the available templates.")
.long("list-templates")
Expand Down Expand Up @@ -289,6 +303,7 @@ fn parse_args() -> Args {
build_kind: BuildKind::from_flags(m.is_present("test"), m.is_present("bench")),
template: m.value_of("template").map(Into::into),
list_templates: m.is_present("list-templates"),
toolchain_version: m.value_of("toolchain-version").map(Into::into),
#[cfg(windows)]
install_file_association: m.is_present("install-file-association"),
#[cfg(windows)]
Expand Down Expand Up @@ -658,7 +673,12 @@ struct InputAction {
*/
using_cache: bool,

use_nightly: bool,
/**
Which toolchain the script should be built with.
`None` indicates that the script should be built with a stable toolchain.
*/
toolchain_version: Option<String>,

/// The package metadata structure for the current invocation.
metadata: PackageMetadata,
Expand All @@ -685,7 +705,7 @@ impl InputAction {
cargo(
cmd,
&*self.manifest_path().to_string_lossy(),
self.use_nightly,
self.toolchain_version.as_ref().map(|s| s.as_str()),
&self.metadata,
script_args,
run_quietly,
Expand Down Expand Up @@ -788,14 +808,22 @@ fn decide_action_for(
};
info!("input_meta: {:?}", input_meta);

let toolchain_version = args.toolchain_version.clone().or_else(|| {
if matches!(args.build_kind, BuildKind::Bench) {
Some("nightly".into())
} else {
None
}
});

let mut action = InputAction {
cargo_output: args.cargo_output,
force_compile: force,
emit_metadata: true,
execute: true,
pkg_path,
using_cache,
use_nightly: matches!(args.build_kind, BuildKind::Bench),
toolchain_version,
metadata: input_meta,
old_metadata: None,
manifest: mani_str,
Expand Down Expand Up @@ -1123,14 +1151,14 @@ Constructs a Cargo command that runs on the script package.
fn cargo(
cmd_name: &str,
manifest: &str,
nightly: bool,
maybe_toolchain_version: Option<&str>,
meta: &PackageMetadata,
script_args: &[String],
run_quietly: bool,
) -> MainResult<Command> {
let mut cmd = Command::new("cargo");
if nightly {
cmd.arg("+nightly");
if let Some(toolchain_version) = maybe_toolchain_version {
cmd.arg(format!("+{}", toolchain_version));
}
cmd.arg(cmd_name);

Expand Down

0 comments on commit ef1d3eb

Please sign in to comment.