|
| 1 | +use cargo_metadata::Message; |
| 2 | +use color_eyre::eyre::Result; |
| 3 | +use xshell::{cmd, Shell}; |
| 4 | + |
| 5 | +const DEFINITION_PATH: &str = "target/definition.yaml"; |
| 6 | + |
| 7 | +pub fn export() -> Result<&'static str> { |
| 8 | + let sh = Shell::new()?; |
| 9 | + |
| 10 | + // We build the actual flash algorithm. |
| 11 | + // We relay all the output of the build process to the open shell. |
| 12 | + let mut cmd = cmd!( |
| 13 | + sh, |
| 14 | + "cargo build --release --message-format=json-diagnostic-rendered-ansi" |
| 15 | + ); |
| 16 | + cmd.set_ignore_status(true); |
| 17 | + let output = cmd.output()?; |
| 18 | + print!("{}", String::from_utf8_lossy(&output.stderr)); |
| 19 | + |
| 20 | + // Parse build information to extract the artifcat. |
| 21 | + let messages = Message::parse_stream(output.stdout.as_ref()); |
| 22 | + |
| 23 | + // Find artifacts. |
| 24 | + let mut target_artifact = None; |
| 25 | + for message in messages { |
| 26 | + match message? { |
| 27 | + Message::CompilerArtifact(artifact) => { |
| 28 | + if let Some(executable) = artifact.executable { |
| 29 | + if target_artifact.is_some() { |
| 30 | + // We found multiple binary artifacts, |
| 31 | + // so we don't know which one to use. |
| 32 | + // This should never happen! |
| 33 | + unreachable!() |
| 34 | + } else { |
| 35 | + target_artifact = Some(executable); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + Message::CompilerMessage(message) => { |
| 40 | + if let Some(rendered) = message.message.rendered { |
| 41 | + print!("{}", rendered); |
| 42 | + } |
| 43 | + } |
| 44 | + // Ignore other messages. |
| 45 | + _ => (), |
| 46 | + } |
| 47 | + } |
| 48 | + let target_artifact = target_artifact.expect("a flash algorithm artifact"); |
| 49 | + let target_artifact = target_artifact.as_str(); |
| 50 | + |
| 51 | + cmd!(sh, "cp template.yaml {DEFINITION_PATH}").run()?; |
| 52 | + cmd!( |
| 53 | + sh, |
| 54 | + "target-gen elf -n algorithm-test -u {target_artifact} {DEFINITION_PATH}" |
| 55 | + ) |
| 56 | + .run()?; |
| 57 | + |
| 58 | + generate_debug_info(&sh, target_artifact)?; |
| 59 | + |
| 60 | + Ok(DEFINITION_PATH) |
| 61 | +} |
| 62 | + |
| 63 | +/// Generates information about the ELF binary. |
| 64 | +fn generate_debug_info(sh: &Shell, target_artifact: &str) -> Result<()> { |
| 65 | + std::fs::write( |
| 66 | + "target/disassembly.s", |
| 67 | + cmd!(sh, "rust-objdump --disassemble {target_artifact}") |
| 68 | + .output()? |
| 69 | + .stdout, |
| 70 | + )?; |
| 71 | + std::fs::write( |
| 72 | + "target/dump.txt", |
| 73 | + cmd!(sh, "rust-objdump -x {target_artifact}") |
| 74 | + .output()? |
| 75 | + .stdout, |
| 76 | + )?; |
| 77 | + std::fs::write( |
| 78 | + "target/nm.txt", |
| 79 | + cmd!(sh, "rust-nm {target_artifact} -n").output()?.stdout, |
| 80 | + )?; |
| 81 | + |
| 82 | + Ok(()) |
| 83 | +} |
0 commit comments