Skip to content

Add --toolchain-version argument #21

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

Merged
merged 3 commits into from
Apr 26, 2021
Merged
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
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(|| match args.build_kind {
BuildKind::Bench => Some("nightly".into()),
_ => 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
6 changes: 6 additions & 0 deletions tests/data/script-unstable-feature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![feature(lang_items)]

fn main() {
println!("--output--");
println!("`#![feature]` *may* be used!");
}
17 changes: 17 additions & 0 deletions tests/tests/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,20 @@ fn test_whitespace_before_main() {
)
.unwrap()
}

#[test]
fn test_stable_toolchain() {
let out = rust_script!("--toolchain-version", "stable", "tests/data/script-unstable-feature.rs").unwrap();
assert!(out.stderr.contains("`#![feature]` may not be used"));
assert!(!out.success());
}

#[test]
fn test_nightly_toolchain() {
let out = rust_script!("--toolchain-version", "nightly", "tests/data/script-unstable-feature.rs").unwrap();
scan!(out.stdout_output();
("`#![feature]` *may* be used!") => ()
)
.unwrap();
assert!(out.success());
}