@@ -104,30 +104,19 @@ impl CompilationCommandBuilder {
104104 cpp_compiler. arg ( format ! ( "--target={target}" ) ) ;
105105 }
106106
107- if let ( Some ( linker ) , Some ( cxx_toolchain_dir) ) = ( & self . linker , & self . cxx_toolchain_dir ) {
107+ if let Some ( cxx_toolchain_dir) = & self . cxx_toolchain_dir {
108108 cpp_compiler. args (
109109 self . include_paths
110110 . iter ( )
111- . map ( |path| "--include-directory=" . to_string ( ) + cxx_toolchain_dir + path) ,
111+ . map ( |path| format ! ( "--include-directory={ cxx_toolchain_dir}{ path}" ) ) ,
112112 ) ;
113-
114- CppCompilation :: CustomLinker {
115- cpp_compiler,
116- linker : linker. to_owned ( ) ,
117- }
118- } else {
119- CppCompilation :: Simple ( cpp_compiler)
120113 }
114+
115+ CppCompilation ( cpp_compiler)
121116 }
122117}
123118
124- pub enum CppCompilation {
125- Simple ( std:: process:: Command ) ,
126- CustomLinker {
127- cpp_compiler : std:: process:: Command ,
128- linker : String ,
129- } ,
130- }
119+ pub struct CppCompilation ( std:: process:: Command ) ;
131120
132121fn clone_command ( command : & std:: process:: Command ) -> std:: process:: Command {
133122 let mut cmd = std:: process:: Command :: new ( command. get_program ( ) ) ;
@@ -144,85 +133,24 @@ fn clone_command(command: &std::process::Command) -> std::process::Command {
144133}
145134
146135impl CppCompilation {
147- fn compile_cpp (
148- command : & std:: process:: Command ,
149- includes : & [ String ] ,
150- inputs : & [ String ] ,
136+ pub fn compile_object_file (
137+ & self ,
138+ input : & str ,
151139 output : & str ,
152140 ) -> std:: io:: Result < std:: process:: Output > {
153- let mut cmd = clone_command ( command) ;
154- cmd. args ( includes) ;
155- cmd. args ( inputs) ;
156- cmd. args ( [ "-o" , output] ) ;
157-
158- if output. ends_with ( ".o" ) {
159- cmd. arg ( "-c" ) ;
160- }
161-
141+ let mut cmd = clone_command ( & self . 0 ) ;
142+ cmd. args ( [ input, "-c" , "-o" , output] ) ;
162143 cmd. output ( )
163144 }
164145
165- pub fn run (
146+ pub fn link_executable (
166147 & self ,
167- includes : & [ String ] ,
168- inputs : & [ String ] ,
148+ inputs : impl Iterator < Item = String > ,
169149 output : & str ,
170150 ) -> std:: io:: Result < std:: process:: Output > {
171- match self {
172- CppCompilation :: Simple ( command) => Self :: compile_cpp ( command, includes, inputs, output) ,
173- CppCompilation :: CustomLinker { cpp_compiler, .. } if output. ends_with ( ".o" ) => {
174- // No need to invoke that custom linker if we're creating an object file.
175- Self :: compile_cpp ( cpp_compiler, includes, inputs, output)
176- }
177- CppCompilation :: CustomLinker {
178- cpp_compiler,
179- linker,
180- } => {
181- let object_file = & format ! ( "{output}.o" ) ;
182-
183- // Build an object file using the cpp compiler.
184- let mut cmd = clone_command ( cpp_compiler) ;
185- cmd. args ( inputs) ;
186- cmd. args ( [ "-c" , "-o" , object_file] ) ;
187-
188- let cpp_output = cmd. output ( ) ?;
189- if !cpp_output. status . success ( ) {
190- error ! ( "c++ compilaton failed" ) ;
191- return Ok ( cpp_output) ;
192- }
193-
194- trace ! ( "using custom linker" ) ;
195-
196- // Use the custom linker to turn the object file into an executable.
197- let mut cmd = std:: process:: Command :: new ( linker) ;
198- cmd. args ( includes) ;
199- cmd. args ( [ object_file, "-o" , output] ) ;
200-
201- if let Some ( current_dir) = cpp_compiler. get_current_dir ( ) {
202- cmd. current_dir ( current_dir) ;
203- }
204-
205- for ( key, val) in cpp_compiler. get_envs ( ) {
206- cmd. env ( key, val. unwrap_or_default ( ) ) ;
207- }
208-
209- let linker_output = cmd. output ( ) ?;
210- if !linker_output. status . success ( ) {
211- error ! ( "custom linker failed" ) ;
212- error ! ( "{}" , String :: from_utf8_lossy( & linker_output. stderr) ) ;
213- return Ok ( linker_output) ;
214- }
215-
216- trace ! ( "removing {object_file}" ) ;
217- let object_file_path = match cpp_compiler. get_current_dir ( ) {
218- Some ( current_dir) => & format ! ( "{}/{object_file}" , current_dir. display( ) ) ,
219- None => object_file,
220- } ;
221-
222- std:: fs:: remove_file ( object_file_path) ?;
223-
224- Ok ( cpp_output)
225- }
226- }
151+ let mut cmd = clone_command ( & self . 0 ) ;
152+ cmd. args ( inputs) ;
153+ cmd. args ( [ "-o" , output] ) ;
154+ cmd. output ( )
227155 }
228156}
0 commit comments