Skip to content
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

Add NASM minimum version check #2223

Merged
merged 1 commit into from
Apr 6, 2020
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ binaries = [
"av-metrics",
]
default = ["binaries", "asm", "signal_support"]
asm = ["nasm-rs", "cc"]
asm = ["nasm-rs", "cc", "regex"]
signal_support = ["signal-hook"]
dump_ivf = ["ivf"]
quick_test = []
Expand Down Expand Up @@ -100,6 +100,7 @@ cc = { version = "1.0", optional = true, features = ["parallel"] }
# which takes a long time to build.
vergen = { version = "3", path = "crates/vergen" }
rustc_version = "0.2"
regex = { version = "1", optional = true }

[build-dependencies.nasm-rs]
version = "0.1.7"
Expand Down
82 changes: 82 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,87 @@ fn rustc_version_check() {
}
}

#[cfg(feature = "asm")]
fn is_nasm_new_enough(
min_version: Option<(u8, u8, u8)>, nasm_path: &Path,
) -> Result<(), String> {
match get_output(std::process::Command::new(nasm_path).arg("-v")) {
Ok(version) => {
if version.contains("NASM version 0.") {
Err(version)
} else if let Some((major, minor, micro)) = min_version {
let ver = parse_nasm_version(&version)?;
eprintln!("{:?} {:?} {:?}", min_version, version, ver);
if major > ver.0
|| (major == ver.0 && minor > ver.1)
|| (major == ver.0 && minor == ver.1 && micro > ver.2)
{
Err(version)
} else {
Ok(())
}
} else {
Ok(())
}
}
Err(err) => Err(err),
}
}

#[cfg(feature = "asm")]
fn parse_nasm_version(version_string: &str) -> Result<(u8, u8, u8), String> {
// Rust regex lib doesn't support empty expressions in alterations,
// so workaround the other way
let regex1 =
regex::Regex::new(r"(?:NASM version )?(\d+)\.(\d+)\.(\d+)").unwrap();
if let Some(captures) = regex1.captures(version_string) {
Ok((
captures[1].parse().expect("Invalid version component"),
captures[2].parse().expect("Invalid version component"),
captures[3].parse().expect("Invalid version component"),
))
} else {
let regex2 = regex::Regex::new(r"(?:NASM version )?(\d+)\.(\d+)").unwrap();
let captures = regex2
.captures(version_string)
.ok_or_else(|| "Unable to parse NASM version string".to_string())?;
Ok((
captures[1].parse().expect("Invalid version component"),
captures[2].parse().expect("Invalid version component"),
0,
))
}
}

#[test]
fn test_parse_nasm_version() {
let ver_str = "NASM version 2.14.02 compiled on Jan 22 2019";
assert_eq!((2, 14, 2), parse_nasm_version(ver_str));
let ver_str = "NASM version 2.14 compiled on Jan 22 2019";
assert_eq!((2, 14, 0), parse_nasm_version(ver_str));
}

#[cfg(feature = "asm")]
fn nasm_version_check() -> std::path::PathBuf {
let nasm_path = std::path::PathBuf::from("nasm");
match is_nasm_new_enough(Some((2, 14, 0)), &nasm_path) {
Ok(_) => nasm_path,
Err(version) => {
panic!("This version of NASM is too old: {}", version);
}
}
}

#[cfg(feature = "asm")]
fn get_output(cmd: &mut std::process::Command) -> Result<String, String> {
let out = cmd.output().map_err(|e| e.to_string())?;
if out.status.success() {
Ok(String::from_utf8_lossy(&out.stdout).to_string())
} else {
Err(String::from_utf8_lossy(&out.stderr).to_string())
}
}

#[allow(unused_variables)]
fn main() {
rustc_version_check();
Expand All @@ -121,6 +202,7 @@ fn main() {
{
if arch == "x86_64" {
println!("cargo:rustc-cfg={}", "nasm_x86_64");
nasm_version_check();
build_nasm_files()
}
if arch == "aarch64" {
Expand Down