@@ -5,9 +5,7 @@ pub struct CompilationCommandBuilder {
55 cxx_toolchain_dir : Option < String > ,
66 arch_flags : Vec < String > ,
77 optimization : String ,
8- include_paths : Vec < String > ,
98 project_root : Option < String > ,
10- linker : Option < String > ,
119 extra_flags : Vec < String > ,
1210}
1311
@@ -19,9 +17,7 @@ impl CompilationCommandBuilder {
1917 cxx_toolchain_dir : None ,
2018 arch_flags : Vec :: new ( ) ,
2119 optimization : "2" . to_string ( ) ,
22- include_paths : Vec :: new ( ) ,
2320 project_root : None ,
24- linker : None ,
2521 extra_flags : Vec :: new ( ) ,
2622 }
2723 }
@@ -53,25 +49,12 @@ impl CompilationCommandBuilder {
5349 self
5450 }
5551
56- /// Sets a list of include paths for compilation.
57- /// The paths that are passed must be relative to the
58- /// "cxx_toolchain_dir" directory path.
59- pub fn set_include_paths ( mut self , paths : Vec < & str > ) -> Self {
60- self . include_paths = paths. into_iter ( ) . map ( |path| path. to_string ( ) ) . collect ( ) ;
61- self
62- }
63-
6452 /// Sets the root path of all the generated test files.
6553 pub fn set_project_root ( mut self , path : & str ) -> Self {
6654 self . project_root = Some ( path. to_string ( ) ) ;
6755 self
6856 }
6957
70- pub fn set_linker ( mut self , linker : String ) -> Self {
71- self . linker = Some ( linker) ;
72- self
73- }
74-
7558 pub fn add_extra_flags ( mut self , flags : Vec < & str > ) -> Self {
7659 let mut flags: Vec < String > = flags. into_iter ( ) . map ( |f| f. to_string ( ) ) . collect ( ) ;
7760 self . extra_flags . append ( & mut flags) ;
@@ -104,30 +87,11 @@ impl CompilationCommandBuilder {
10487 cpp_compiler. arg ( format ! ( "--target={target}" ) ) ;
10588 }
10689
107- if let ( Some ( linker) , Some ( cxx_toolchain_dir) ) = ( & self . linker , & self . cxx_toolchain_dir ) {
108- cpp_compiler. args (
109- self . include_paths
110- . iter ( )
111- . map ( |path| "--include-directory=" . to_string ( ) + cxx_toolchain_dir + path) ,
112- ) ;
113-
114- CppCompilation :: CustomLinker {
115- cpp_compiler,
116- linker : linker. to_owned ( ) ,
117- }
118- } else {
119- CppCompilation :: Simple ( cpp_compiler)
120- }
90+ CppCompilation ( cpp_compiler)
12191 }
12292}
12393
124- pub enum CppCompilation {
125- Simple ( std:: process:: Command ) ,
126- CustomLinker {
127- cpp_compiler : std:: process:: Command ,
128- linker : String ,
129- } ,
130- }
94+ pub struct CppCompilation ( std:: process:: Command ) ;
13195
13296fn clone_command ( command : & std:: process:: Command ) -> std:: process:: Command {
13397 let mut cmd = std:: process:: Command :: new ( command. get_program ( ) ) ;
@@ -144,62 +108,15 @@ fn clone_command(command: &std::process::Command) -> std::process::Command {
144108}
145109
146110impl CppCompilation {
111+ pub fn command_mut ( & mut self ) -> & mut std:: process:: Command {
112+ & mut self . 0
113+ }
114+
147115 pub fn run ( & self , inputs : & [ String ] , output : & str ) -> std:: io:: Result < std:: process:: Output > {
148- match self {
149- CppCompilation :: Simple ( command) => {
150- let mut cmd = clone_command ( command) ;
151- cmd. args ( inputs) ;
152- cmd. args ( [ "-o" , output] ) ;
153-
154- cmd. output ( )
155- }
156- CppCompilation :: CustomLinker {
157- cpp_compiler,
158- linker,
159- } => {
160- let object_file = & format ! ( "{output}.o" ) ;
161-
162- // Build an object file using the cpp compiler.
163- let mut cmd = clone_command ( cpp_compiler) ;
164- cmd. args ( inputs) ;
165- cmd. args ( [ "-c" , "-o" , object_file] ) ;
166-
167- let cpp_output = cmd. output ( ) ?;
168- if !cpp_output. status . success ( ) {
169- error ! ( "c++ compilaton failed" ) ;
170- return Ok ( cpp_output) ;
171- }
172-
173- trace ! ( "using custom linker" ) ;
174-
175- // Use the custom linker to turn the object file into an executable.
176- let mut cmd = std:: process:: Command :: new ( linker) ;
177- cmd. args ( [ object_file, "-o" , output] ) ;
178-
179- if let Some ( current_dir) = cpp_compiler. get_current_dir ( ) {
180- cmd. current_dir ( current_dir) ;
181- }
182-
183- for ( key, val) in cpp_compiler. get_envs ( ) {
184- cmd. env ( key, val. unwrap_or_default ( ) ) ;
185- }
186-
187- let linker_output = cmd. output ( ) ?;
188- if !linker_output. status . success ( ) {
189- error ! ( "custom linker failed" ) ;
190- return Ok ( linker_output) ;
191- }
192-
193- trace ! ( "removing {object_file}" ) ;
194- let object_file_path = match cpp_compiler. get_current_dir ( ) {
195- Some ( current_dir) => & format ! ( "{}/{object_file}" , current_dir. display( ) ) ,
196- None => object_file,
197- } ;
198-
199- std:: fs:: remove_file ( object_file_path) ?;
200-
201- Ok ( cpp_output)
202- }
203- }
116+ let mut cmd = clone_command ( & self . 0 ) ;
117+ cmd. args ( inputs) ;
118+ cmd. args ( [ "-o" , output] ) ;
119+
120+ cmd. output ( )
204121 }
205122}
0 commit comments