build asm in build.rs
parent
246f5cfccf
commit
465da6e7cd
@ -0,0 +1,6 @@
|
||||
%include "defines.s"
|
||||
|
||||
[org BOOT1_LOADPOINT]
|
||||
[bits 16]
|
||||
|
||||
hlt
|
||||
@ -1,4 +1,157 @@
|
||||
use std::{env, ffi::{OsStr, OsString}, fmt, fs, io, path::{Path, PathBuf}, process::{self, Command}};
|
||||
|
||||
fn main() {
|
||||
let out_dir = env::var_os("OUT_DIR")
|
||||
.expect("OUT_DIR not set");
|
||||
let out_dir = PathBuf::from(out_dir);
|
||||
|
||||
let path = SearchPath::load();
|
||||
|
||||
let nasm_path = path.search("nasm")
|
||||
.next()
|
||||
.expect("failed to find nasm in PATH");
|
||||
let nasm = Nasm::new(nasm_path);
|
||||
|
||||
build_asm(&nasm, &out_dir);
|
||||
|
||||
emit_link_args();
|
||||
|
||||
rerun_if_changed("build.rs".as_ref());
|
||||
}
|
||||
|
||||
fn emit_link_args() {
|
||||
println!("cargo::rustc-link-arg=-Tlink.ld");
|
||||
}
|
||||
|
||||
fn rerun_if_changed(path: &Path) {
|
||||
if let Some(path_str) = path.to_str() {
|
||||
println!("cargo::rerun-if-changed={}", path_str);
|
||||
}
|
||||
else {
|
||||
eprintln!("invalid unicode file path");
|
||||
}
|
||||
}
|
||||
|
||||
fn build_asm(nasm: &Nasm, out_dir: &Path) {
|
||||
nasm.assemble(
|
||||
&out_dir.join("prelude.bin"),
|
||||
&["asm/prelude.s".as_ref()],
|
||||
&["..".as_ref()],
|
||||
).unwrap();
|
||||
}
|
||||
|
||||
struct Nasm {
|
||||
bin_path: PathBuf,
|
||||
}
|
||||
|
||||
impl Nasm {
|
||||
fn new(bin_path: PathBuf) -> Self {
|
||||
Self { bin_path }
|
||||
}
|
||||
|
||||
fn assemble(&self, output: &Path, sources: &[&Path], includes: &[&Path]) -> Result<(), CmdError>
|
||||
{
|
||||
for source in sources {
|
||||
rerun_if_changed(source);
|
||||
}
|
||||
|
||||
let mut cmd = Command::new(&self.bin_path);
|
||||
cmd.arg("-f").arg("bin");
|
||||
|
||||
for include in includes {
|
||||
let mut buf = OsString::new();
|
||||
buf.push("-I");
|
||||
buf.push(include);
|
||||
cmd.arg(buf);
|
||||
}
|
||||
|
||||
cmd
|
||||
.arg("-o")
|
||||
.arg(output)
|
||||
.args(sources);
|
||||
|
||||
run_cmd(&mut cmd)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct SearchPath {
|
||||
paths: Vec<PathBuf>
|
||||
}
|
||||
|
||||
impl SearchPath {
|
||||
fn load() -> Self {
|
||||
let path_var = env::var_os("PATH").unwrap_or_default();
|
||||
let paths = env::split_paths(&path_var).collect();
|
||||
Self { paths }
|
||||
}
|
||||
|
||||
fn search<'a, 'b, 'c, T>(&'a self, bin: &'b T) -> impl Iterator<Item = PathBuf> + 'c
|
||||
where
|
||||
'a: 'c,
|
||||
'b: 'c,
|
||||
T: AsRef<OsStr> + ?Sized,
|
||||
{
|
||||
let bin = bin.as_ref();
|
||||
self.paths
|
||||
.iter()
|
||||
.filter_map(move |path| {
|
||||
let path = path.join(bin);
|
||||
fs::metadata(&path)
|
||||
.ok()
|
||||
.and_then(|meta| if meta.is_file() || meta.is_symlink() {
|
||||
Some(path)
|
||||
} else {
|
||||
None
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn run_cmd(cmd: &mut Command) -> Result<process::Output, CmdError> {
|
||||
use fmt::Write;
|
||||
|
||||
cmd
|
||||
.output()
|
||||
.map_err(CmdErrorKind::Io)
|
||||
.and_then(|out| if out.status.success() {
|
||||
Ok(out)
|
||||
} else {
|
||||
Err(CmdErrorKind::Status(out))
|
||||
})
|
||||
.map_err(|err| {
|
||||
let mut cmd_buf = String::new();
|
||||
write!(&mut cmd_buf, "{:?}", cmd).ok();
|
||||
CmdError::new(cmd_buf, err)
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct CmdError {
|
||||
cmd: String,
|
||||
kind: CmdErrorKind,
|
||||
}
|
||||
|
||||
impl CmdError {
|
||||
fn new(cmd: String, kind: CmdErrorKind) -> Self {
|
||||
Self { cmd, kind }
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for CmdError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "`{}` failed: ", self.cmd)?;
|
||||
match &self.kind {
|
||||
CmdErrorKind::Io(err) => err.fmt(f),
|
||||
CmdErrorKind::Status(out) => write!(f, "exited with status {}", out.status),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum CmdErrorKind {
|
||||
Io(io::Error),
|
||||
Status(process::Output),
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue