library-ify nasm helper

main
pantonshire 9 months ago
parent ed0f9119f4
commit ae2720c826

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "buildtools"
version = "0.1.0"

@ -0,0 +1,6 @@
[package]
name = "buildtools"
version = "0.1.0"
edition = "2021"
[dependencies]

@ -0,0 +1,109 @@
use std::{ffi::OsString, path::{Path, PathBuf}, process::Command};
#[derive(Clone)]
pub struct Nasm {
bin_path: PathBuf,
includes: Vec<PathBuf>,
warnings: Vec<String>,
werror: bool,
out_format: Option<String>,
}
impl Nasm {
pub fn new(bin_path: PathBuf) -> Self {
Self {
bin_path,
includes: Vec::new(),
warnings: Vec::new(),
werror: true,
out_format: None,
}
}
pub fn include<T, I>(self, paths: I) -> Self
where
PathBuf: From<T>,
I: IntoIterator<Item = T>,
{
Self {
includes: {
let mut updated_includes = self.includes;
updated_includes.extend(paths.into_iter().map(From::from));
updated_includes
},
..self
}
}
pub fn warnings<T, I>(self, warnings: I) -> Self
where
String: From<T>,
I: IntoIterator<Item = T>,
{
Self {
warnings: {
let mut updated_warnings = self.warnings;
updated_warnings.extend(warnings.into_iter().map(From::from));
updated_warnings
},
..self
}
}
pub fn werror(self, enabled: bool) -> Self {
Self {
werror: enabled,
..self
}
}
pub fn out_format<T>(self, format: T) -> Self
where
String: From<T>,
{
Self {
out_format: Some(From::from(format)),
..self
}
}
// TODO: make this generic over sources & path types
pub fn to_cmd(&self, output: &Path, sources: &[&Path]) -> Command {
let mut cmd = self.to_common_cmd();
cmd
.arg("-o")
.arg(output)
.args(sources);
cmd
}
fn to_common_cmd(&self) -> Command {
let mut cmd = Command::new(&self.bin_path);
if let Some(format) = self.out_format.as_ref() {
cmd.arg("-f").arg(format);
}
for include in self.includes.iter() {
let mut buf = OsString::new();
buf.push("-I");
buf.push(include);
cmd.arg(buf);
}
for warning in self.warnings.iter() {
let mut buf = OsString::new();
buf.push("-w+");
buf.push(warning);
cmd.arg(buf);
}
if self.werror {
cmd.arg("-werror");
}
cmd
}
}
Loading…
Cancel
Save