Skip to content

Commit 05abc87

Browse files
committed
nbuild strips utilities.
1 parent d8b5c72 commit 05abc87

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

nbuild/src/lib.rs

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,7 @@ where
128128
{
129129
let path = path.as_ref();
130130
println!("Making binary of: {}", path.display());
131-
let output = std::process::Command::new("rustc")
132-
.arg("--print")
133-
.arg("target-libdir")
134-
.output()
135-
.expect("Failed to run rustc --print target-libdir");
136-
let sysroot = String::from_utf8(output.stdout).expect("sysroot path isn't UTF-8");
137-
let sysroot: std::path::PathBuf = sysroot.trim().into();
138-
let mut objcopy = sysroot.clone();
139-
objcopy.pop();
140-
objcopy.push("bin");
141-
objcopy.push("llvm-objcopy");
131+
let objcopy = tool_path("llvm-objcopy");
142132
let mut command_line = std::process::Command::new(objcopy);
143133
command_line.args(["-O", "binary"]);
144134
command_line.arg(path);
@@ -153,4 +143,47 @@ where
153143
}
154144
}
155145

146+
/// Make a binary version of an ELF file
147+
pub fn strip_elf<P1, P2>(input_path: P1, output_path: P2) -> Result<(), ProcessError>
148+
where
149+
P1: AsRef<std::path::Path>,
150+
P2: AsRef<std::path::Path>,
151+
{
152+
let input_path = input_path.as_ref();
153+
let output_path = output_path.as_ref();
154+
155+
println!(
156+
"Stripping {} as {}",
157+
input_path.display(),
158+
output_path.display()
159+
);
160+
let strip = tool_path("llvm-strip");
161+
let mut command_line = std::process::Command::new(strip);
162+
command_line.arg(input_path);
163+
command_line.arg("-o");
164+
command_line.arg(output_path);
165+
println!("Running: {:?}", command_line);
166+
let output = command_line.output().map_err(ProcessError::SpawnError)?;
167+
if output.status.success() {
168+
Ok(())
169+
} else {
170+
Err(ProcessError::RunError(output.status))
171+
}
172+
}
173+
174+
/// Get the path where `llvm-objcopy` and friends live.
175+
pub fn tool_path(tool: &str) -> std::path::PathBuf {
176+
let output = std::process::Command::new("rustc")
177+
.arg("--print")
178+
.arg("target-libdir")
179+
.output()
180+
.expect("Failed to run rustc --print target-libdir");
181+
let sysroot = String::from_utf8(output.stdout).expect("sysroot path isn't UTF-8");
182+
let mut result: std::path::PathBuf = sysroot.trim().into();
183+
result.pop();
184+
result.push("bin");
185+
result.push(tool);
186+
result
187+
}
188+
156189
// End of file

nbuild/src/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,15 @@ fn binary(packages: &[nbuild::Package], start_address: &str, target: &str) {
123123
let package_output = package
124124
.output(target, "release")
125125
.expect("utilties should have an output");
126-
let contents = match std::fs::read(&package_output) {
126+
let stripped = package_output.clone() + ".stripped";
127+
if let Err(e) = nbuild::strip_elf(&package_output, &stripped) {
128+
eprintln!("Reading of {} failed: {}", stripped, e);
129+
continue;
130+
};
131+
let contents = match std::fs::read(&stripped) {
127132
Ok(contents) => contents,
128133
Err(e) => {
129-
eprintln!("Reading of {} failed: {}", package_output, e);
134+
eprintln!("Reading of {} failed: {}", stripped, e);
130135
continue;
131136
}
132137
};

0 commit comments

Comments
 (0)