mirror of
https://github.com/encounter/objdiff.git
synced 2025-12-09 13:37:55 +00:00
Initial commit
This commit is contained in:
107
src/jobs/build.rs
Normal file
107
src/jobs/build.rs
Normal file
@@ -0,0 +1,107 @@
|
||||
use std::{
|
||||
path::Path,
|
||||
process::Command,
|
||||
str::from_utf8,
|
||||
sync::{mpsc::Receiver, Arc, RwLock},
|
||||
};
|
||||
|
||||
use anyhow::{Context, Error, Result};
|
||||
|
||||
use crate::{
|
||||
app::AppConfig,
|
||||
diff::diff_objs,
|
||||
elf,
|
||||
jobs::{queue_job, update_status, Job, JobResult, JobState, Status},
|
||||
obj::ObjInfo,
|
||||
};
|
||||
|
||||
pub struct BuildStatus {
|
||||
pub success: bool,
|
||||
pub log: String,
|
||||
}
|
||||
pub struct BuildResult {
|
||||
pub first_status: BuildStatus,
|
||||
pub second_status: BuildStatus,
|
||||
pub first_obj: Option<ObjInfo>,
|
||||
pub second_obj: Option<ObjInfo>,
|
||||
}
|
||||
|
||||
fn run_make(cwd: &Path, arg: &Path) -> BuildStatus {
|
||||
match (|| -> Result<BuildStatus> {
|
||||
let output = Command::new("make")
|
||||
.current_dir(cwd)
|
||||
.arg(arg)
|
||||
.output()
|
||||
.context("Failed to execute build")?;
|
||||
let stdout = from_utf8(&output.stdout).context("Failed to process stdout")?;
|
||||
let stderr = from_utf8(&output.stderr).context("Failed to process stderr")?;
|
||||
Ok(BuildStatus {
|
||||
success: output.status.code().unwrap_or(-1) == 0,
|
||||
log: format!("{}\n{}", stdout, stderr),
|
||||
})
|
||||
})() {
|
||||
Ok(status) => status,
|
||||
Err(e) => BuildStatus { success: false, log: e.to_string() },
|
||||
}
|
||||
}
|
||||
|
||||
fn run_build(
|
||||
status: &Status,
|
||||
cancel: Receiver<()>,
|
||||
obj_path: String,
|
||||
config: Arc<RwLock<AppConfig>>,
|
||||
) -> Result<Box<BuildResult>> {
|
||||
let config = config.read().map_err(|_| Error::msg("Failed to lock app config"))?.clone();
|
||||
let project_dir =
|
||||
config.project_dir.as_ref().ok_or_else(|| Error::msg("Missing project dir"))?;
|
||||
let mut asm_path = config
|
||||
.build_asm_dir
|
||||
.as_ref()
|
||||
.ok_or_else(|| Error::msg("Missing build asm dir"))?
|
||||
.to_owned();
|
||||
asm_path.push(&obj_path);
|
||||
let mut src_path = config
|
||||
.build_src_dir
|
||||
.as_ref()
|
||||
.ok_or_else(|| Error::msg("Missing build src dir"))?
|
||||
.to_owned();
|
||||
src_path.push(&obj_path);
|
||||
let asm_path_rel =
|
||||
asm_path.strip_prefix(project_dir).context("Failed to create relative asm obj path")?;
|
||||
let src_path_rel =
|
||||
src_path.strip_prefix(project_dir).context("Failed to create relative src obj path")?;
|
||||
|
||||
update_status(status, format!("Building asm {}", obj_path), 0, 5, &cancel)?;
|
||||
let first_status = run_make(project_dir, asm_path_rel);
|
||||
|
||||
update_status(status, format!("Building src {}", obj_path), 1, 5, &cancel)?;
|
||||
let second_status = run_make(project_dir, src_path_rel);
|
||||
|
||||
let mut first_obj = if first_status.success {
|
||||
update_status(status, format!("Loading asm {}", obj_path), 2, 5, &cancel)?;
|
||||
Some(elf::read(&asm_path)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut second_obj = if second_status.success {
|
||||
update_status(status, format!("Loading src {}", obj_path), 3, 5, &cancel)?;
|
||||
Some(elf::read(&src_path)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if let (Some(first_obj), Some(second_obj)) = (&mut first_obj, &mut second_obj) {
|
||||
update_status(status, "Performing diff".to_string(), 4, 5, &cancel)?;
|
||||
diff_objs(first_obj, second_obj)?;
|
||||
}
|
||||
|
||||
update_status(status, "Complete".to_string(), 5, 5, &cancel)?;
|
||||
Ok(Box::new(BuildResult { first_status, second_status, first_obj, second_obj }))
|
||||
}
|
||||
|
||||
pub fn queue_build(obj_path: String, config: Arc<RwLock<AppConfig>>) -> JobState {
|
||||
queue_job(Job::Build, move |status, cancel| {
|
||||
run_build(status, cancel, obj_path, config).map(JobResult::Build)
|
||||
})
|
||||
}
|
||||
104
src/jobs/mod.rs
Normal file
104
src/jobs/mod.rs
Normal file
@@ -0,0 +1,104 @@
|
||||
use std::{
|
||||
sync::{
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
mpsc::{Receiver, Sender, TryRecvError},
|
||||
Arc, RwLock,
|
||||
},
|
||||
thread::JoinHandle,
|
||||
};
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::jobs::build::BuildResult;
|
||||
|
||||
pub mod build;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
|
||||
pub enum Job {
|
||||
Build,
|
||||
}
|
||||
pub static JOB_ID: AtomicUsize = AtomicUsize::new(0);
|
||||
pub struct JobState {
|
||||
pub id: usize,
|
||||
pub job_type: Job,
|
||||
pub handle: Option<JoinHandle<JobResult>>,
|
||||
pub status: Arc<RwLock<JobStatus>>,
|
||||
pub cancel: Sender<()>,
|
||||
pub should_remove: bool,
|
||||
}
|
||||
#[derive(Default)]
|
||||
pub struct JobStatus {
|
||||
pub title: String,
|
||||
pub progress_percent: f32,
|
||||
pub progress_items: Option<[u32; 2]>,
|
||||
pub status: String,
|
||||
pub error: Option<anyhow::Error>,
|
||||
}
|
||||
pub enum JobResult {
|
||||
None,
|
||||
Build(Box<BuildResult>),
|
||||
}
|
||||
|
||||
fn should_cancel(rx: &Receiver<()>) -> bool {
|
||||
match rx.try_recv() {
|
||||
Ok(_) | Err(TryRecvError::Disconnected) => true,
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
type Status = Arc<RwLock<JobStatus>>;
|
||||
|
||||
fn queue_job(
|
||||
job_type: Job,
|
||||
run: impl FnOnce(&Status, Receiver<()>) -> Result<JobResult> + Send + 'static,
|
||||
) -> JobState {
|
||||
let status = Arc::new(RwLock::new(JobStatus {
|
||||
title: String::new(),
|
||||
progress_percent: 0.0,
|
||||
progress_items: None,
|
||||
status: "".to_string(),
|
||||
error: None,
|
||||
}));
|
||||
let status_clone = status.clone();
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let handle = std::thread::spawn(move || {
|
||||
return match run(&status, rx) {
|
||||
Ok(state) => state,
|
||||
Err(e) => {
|
||||
if let Ok(mut w) = status.write() {
|
||||
w.error = Some(e);
|
||||
}
|
||||
JobResult::None
|
||||
}
|
||||
};
|
||||
});
|
||||
let id = JOB_ID.fetch_add(1, Ordering::Relaxed);
|
||||
log::info!("Started job {}", id);
|
||||
JobState {
|
||||
id,
|
||||
job_type,
|
||||
handle: Some(handle),
|
||||
status: status_clone,
|
||||
cancel: tx,
|
||||
should_remove: true,
|
||||
}
|
||||
}
|
||||
|
||||
fn update_status(
|
||||
status: &Status,
|
||||
str: String,
|
||||
count: u32,
|
||||
total: u32,
|
||||
cancel: &Receiver<()>,
|
||||
) -> Result<()> {
|
||||
let mut w = status.write().map_err(|_| anyhow::Error::msg("Failed to lock job status"))?;
|
||||
w.progress_items = Some([count, total]);
|
||||
w.progress_percent = count as f32 / total as f32;
|
||||
if should_cancel(cancel) {
|
||||
w.status = "Cancelled".to_string();
|
||||
return Err(anyhow::Error::msg("Cancelled"));
|
||||
} else {
|
||||
w.status = str;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user