@@ -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
0 commit comments