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 support for min version #28

Merged
merged 1 commit into from
Apr 21, 2020
Merged
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
67 changes: 42 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub struct Build {
archiver: Option<PathBuf>,
nasm: Option<PathBuf>,
debug: bool,
min_version: (usize, usize, usize),
}

impl Build {
Expand All @@ -85,6 +86,7 @@ impl Build {
out_dir: None,
nasm: None,
target: None,
min_version: (1, 0, 0),
debug: env::var("DEBUG").ok().map_or(false, |d| d != "false"),
}
}
Expand Down Expand Up @@ -180,6 +182,12 @@ impl Build {
self
}

/// Set the minimum version required
pub fn min_version(&mut self, major: usize, minor: usize, micro: usize) -> &mut Self {
self.min_version = (major, minor, micro);
self
}

/// Run the compiler, generating the file output
///
/// The name output should be the base name of the library,
Expand Down Expand Up @@ -290,17 +298,40 @@ impl Build {
.unwrap_or_else(|| env::var("TARGET").expect("TARGET must be set"))
}

/// Returns version string if nasm is too old,
/// or error message string if it's unusable.
fn is_nasm_new_enough(&self, nasm_path: &Path) -> Result<(), String> {
let version = get_output(Command::new(nasm_path).arg("-v"))?;
let (major, minor, micro) = self.min_version;
let ver: Vec<usize> = version
.split(" ")
.skip(2)
.next()
.map(|ver| {
ver.split(".")
.map(|v| v.parse().expect("Invalid version component"))
.collect()
})
.expect("Invalid version");
if ver.len() < 3 {
panic!("Not enough version components");
} else {
if major > ver[0] ||
(major == ver[0] && minor > ver[1]) ||
(major == ver[0] && minor == ver[1] && micro > ver[2]) {
Err(version)?
} else {
Ok(())
}
}
Comment on lines +318 to +326
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the previous if statement panic!s, this code doesn't need an explicit else branch,

}

fn find_nasm(&mut self) -> PathBuf {
match self.nasm.clone() {
Some(path) => path,
None => {
let nasm_path = PathBuf::from("nasm");
match is_nasm_new_enough(&nasm_path) {
Ok(_) => nasm_path,
Err(version) => {
panic!("This version of NASM is too old: {}", version);
},
}
let nasm_path = self.nasm.clone().unwrap_or_else(|| PathBuf::from("nasm"));
match self.is_nasm_new_enough(&nasm_path) {
Ok(_) => nasm_path,
Err(version) => {
panic!("This version of NASM is too old: {}", version);
},
}
}
Expand All @@ -315,21 +346,6 @@ fn get_output(cmd: &mut Command) -> Result<String, String> {
}
}

/// Returns version string if nasm is too old,
/// or error message string if it's unusable.
fn is_nasm_new_enough(nasm_path: &Path) -> Result<(), String> {
match get_output(Command::new(nasm_path).arg("-v")) {
Ok(version) => {
if version.contains("NASM version 0.") {
Err(version)?
} else {
Ok(())
}
},
Err(err) => Err(err)?,
}
}

fn run(cmd: &mut Command) {
println!("running: {:?}", cmd);

Expand All @@ -356,6 +372,7 @@ fn test_build() {
build.flag("-test");
build.target("i686-unknown-linux-musl");
build.out_dir("/tmp");
build.min_version(0, 0, 0);

assert_eq!(build.get_args("i686-unknown-linux-musl"), &["-felf32", "-I./", "-Idir/", "-Dfoo=1", "-Dbar", "-test"]);
}