Skip to content

Commit

Permalink
Better encapsulate compiler version discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
Shnatsel committed Jul 16, 2024
1 parent 92ebfee commit 50f66f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
9 changes: 3 additions & 6 deletions src/project.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::options::{self, BuildMode, BuildOptions, Sanitizer};
use crate::rustc_version::{is_nightly, rust_version_string, RustVersion};
use crate::rustc_version::RustVersion;
use crate::utils::default_target;
use anyhow::{anyhow, bail, Context, Result};
use cargo_metadata::MetadataCommand;
use std::collections::HashSet;
use std::io::Read;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::{
env, ffi, fs,
process::{Command, Stdio},
Expand Down Expand Up @@ -192,9 +191,7 @@ impl FuzzProject {
if !matches!(build.sanitizer, Sanitizer::None) {
// Select the appropriate sanitizer flag for the given rustc version:
// either -Zsanitizer or -Csanitizer.
let rust_version_string = rust_version_string()?;
let rust_version =
RustVersion::from_str(&rust_version_string).map_err(|e| anyhow::anyhow!(e))?;
let rust_version = RustVersion::discover()?;
let sanitizer_flag = match rust_version.has_sanitizers_on_stable() {
true => "-Csanitizer",
false => "-Zsanitizer",
Expand All @@ -219,7 +216,7 @@ impl FuzzProject {
// whenever we're on nightly on a recent enough compiler,
// and let the compiler show an error message
// if the user tries to enable a sanitizer not supported on their stable compiler.
if is_nightly(&rust_version_string) && rust_version.has_sanitizers_on_stable() {
if rust_version.nightly && rust_version.has_sanitizers_on_stable() {
rustflags.push_str(" -Zunstable-options")
}
}
Expand Down
33 changes: 24 additions & 9 deletions src/rustc_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use std::{cmp::Ordering, process::Command, str::FromStr};
use anyhow::Context;

/// Checks if the compiler currently in use is nightly, or `RUSTC_BOOTSTRAP` is set to get nightly features on stable
pub fn is_nightly(version_string: &str) -> bool {
fn is_nightly(version_string: &str) -> bool {
version_string.contains("-nightly ") || std::env::var_os("RUSTC_BOOTSTRAP").is_some()
}

/// Returns the output of `rustc --version`
pub fn rust_version_string() -> anyhow::Result<String> {
fn rust_version_string() -> anyhow::Result<String> {
// The path to rustc can be specified via an environment variable:
// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads
let rustc_path = std::env::var_os("RUSTC").unwrap_or("rustc".into());
Expand All @@ -27,6 +27,7 @@ pub struct RustVersion {
pub major: u32,
pub minor: u32,
// we don't care about the patch version and it's a bit of a pain to parse
pub nightly: bool,
}

impl Ord for RustVersion {
Expand All @@ -38,6 +39,14 @@ impl Ord for RustVersion {
}
}

impl RustVersion {
pub fn discover() -> anyhow::Result<Self> {
let version_string = rust_version_string()?;
let me = Self::from_str(&version_string).map_err(|e| anyhow::anyhow!(e))?;
Ok(me)
}
}

impl FromStr for RustVersion {
type Err = &'static str;

Expand All @@ -60,7 +69,12 @@ impl FromStr for RustVersion {
.map_err(|_| {
"Failed to parse minor version in `rustc --version` output as a number!"
})?;
Ok(RustVersion { major, minor })
let nightly = is_nightly(s);
Ok(RustVersion {
major,
minor,
nightly,
})
}
}

Expand All @@ -76,6 +90,7 @@ impl RustVersion {
let release_that_stabilized_sanitizers = RustVersion {
major: 1,
minor: 85,
nightly: false,
};
self >= &release_that_stabilized_sanitizers
}
Expand All @@ -93,10 +108,10 @@ mod tests {
result,
RustVersion {
major: 1,
minor: 78
minor: 78,
nightly: false,
}
);
assert!(!is_nightly(version_string))
}

#[test]
Expand All @@ -107,10 +122,10 @@ mod tests {
result,
RustVersion {
major: 1,
minor: 81
minor: 81,
nightly: true,
}
);
assert!(is_nightly(version_string))
}

#[test]
Expand All @@ -121,9 +136,9 @@ mod tests {
result,
RustVersion {
major: 2,
minor: 356
minor: 356,
nightly: false,
}
);
assert!(!is_nightly(version_string))
}
}

0 comments on commit 50f66f7

Please sign in to comment.