@@ -5,11 +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- output : String ,
11- input : String ,
12- linker : Option < String > ,
139 extra_flags : Vec < String > ,
1410}
1511
@@ -21,11 +17,7 @@ impl CompilationCommandBuilder {
2117 cxx_toolchain_dir : None ,
2218 arch_flags : Vec :: new ( ) ,
2319 optimization : "2" . to_string ( ) ,
24- include_paths : Vec :: new ( ) ,
2520 project_root : None ,
26- output : String :: new ( ) ,
27- input : String :: new ( ) ,
28- linker : None ,
2921 extra_flags : Vec :: new ( ) ,
3022 }
3123 }
@@ -57,37 +49,12 @@ impl CompilationCommandBuilder {
5749 self
5850 }
5951
60- /// Sets a list of include paths for compilation.
61- /// The paths that are passed must be relative to the
62- /// "cxx_toolchain_dir" directory path.
63- pub fn set_include_paths ( mut self , paths : Vec < & str > ) -> Self {
64- self . include_paths = paths. into_iter ( ) . map ( |path| path. to_string ( ) ) . collect ( ) ;
65- self
66- }
67-
6852 /// Sets the root path of all the generated test files.
6953 pub fn set_project_root ( mut self , path : & str ) -> Self {
7054 self . project_root = Some ( path. to_string ( ) ) ;
7155 self
7256 }
7357
74- /// The name of the output executable, without any suffixes
75- pub fn set_output_name ( mut self , path : & str ) -> Self {
76- self . output = path. to_string ( ) ;
77- self
78- }
79-
80- /// The name of the input C file, without any suffixes
81- pub fn set_input_name ( mut self , path : & str ) -> Self {
82- self . input = path. to_string ( ) ;
83- self
84- }
85-
86- pub fn set_linker ( mut self , linker : String ) -> Self {
87- self . linker = Some ( linker) ;
88- self
89- }
90-
9158 pub fn add_extra_flags ( mut self , flags : Vec < & str > ) -> Self {
9259 let mut flags: Vec < String > = flags. into_iter ( ) . map ( |f| f. to_string ( ) ) . collect ( ) ;
9360 self . extra_flags . append ( & mut flags) ;
@@ -100,55 +67,56 @@ impl CompilationCommandBuilder {
10067}
10168
10269impl CompilationCommandBuilder {
103- pub fn make_string ( self ) -> String {
104- let arch_flags = self . arch_flags . join ( "+" ) ;
70+ pub fn into_cpp_compilation ( self ) -> CppCompilation {
71+ let mut cpp_compiler = std:: process:: Command :: new ( self . compiler ) ;
72+
73+ if let Some ( project_root) = self . project_root {
74+ cpp_compiler. current_dir ( project_root) ;
75+ }
76+
10577 let flags = std:: env:: var ( "CPPFLAGS" ) . unwrap_or ( "" . into ( ) ) ;
106- let project_root = self . project_root . unwrap_or_default ( ) ;
107- let project_root_str = project_root. as_str ( ) ;
108- let mut output = self . output . clone ( ) ;
109- if self . linker . is_some ( ) {
110- output += ".o"
111- } ;
112- let mut command = format ! (
113- "{} {flags} -march={arch_flags} \
114- -O{} \
115- -o {project_root}/{} \
116- {project_root}/{}.cpp",
117- self . compiler, self . optimization, output, self . input,
118- ) ;
119-
120- command = command + " " + self . extra_flags . join ( " " ) . as_str ( ) ;
78+ cpp_compiler. args ( flags. split_whitespace ( ) ) ;
79+
80+ cpp_compiler. arg ( format ! ( "-march={}" , self . arch_flags. join( "+" ) ) ) ;
81+
82+ cpp_compiler. arg ( format ! ( "-O{}" , self . optimization) ) ;
83+
84+ cpp_compiler. args ( self . extra_flags ) ;
12185
12286 if let Some ( target) = & self . target {
123- command = command + " --target=" + target;
87+ cpp_compiler . arg ( format ! ( " --target={ target}" ) ) ;
12488 }
12589
126- if let ( Some ( linker) , Some ( cxx_toolchain_dir) ) = ( & self . linker , & self . cxx_toolchain_dir ) {
127- let include_args = self
128- . include_paths
129- . iter ( )
130- . map ( |path| "--include-directory=" . to_string ( ) + cxx_toolchain_dir + path)
131- . collect :: < Vec < _ > > ( )
132- . join ( " " ) ;
133-
134- command = command
135- + " -c "
136- + include_args. as_str ( )
137- + " && "
138- + linker
139- + " "
140- + project_root_str
141- + "/"
142- + & output
143- + " -o "
144- + project_root_str
145- + "/"
146- + & self . output
147- + " && rm "
148- + project_root_str
149- + "/"
150- + & output;
151- }
152- command
90+ CppCompilation ( cpp_compiler)
91+ }
92+ }
93+
94+ pub struct CppCompilation ( std:: process:: Command ) ;
95+
96+ fn clone_command ( command : & std:: process:: Command ) -> std:: process:: Command {
97+ let mut cmd = std:: process:: Command :: new ( command. get_program ( ) ) ;
98+ if let Some ( current_dir) = command. get_current_dir ( ) {
99+ cmd. current_dir ( current_dir) ;
100+ }
101+ cmd. args ( command. get_args ( ) ) ;
102+
103+ for ( key, val) in command. get_envs ( ) {
104+ cmd. env ( key, val. unwrap_or_default ( ) ) ;
105+ }
106+
107+ cmd
108+ }
109+
110+ impl CppCompilation {
111+ pub fn command_mut ( & mut self ) -> & mut std:: process:: Command {
112+ & mut self . 0
113+ }
114+
115+ pub fn run ( & self , inputs : & [ String ] , output : & str ) -> std:: io:: Result < std:: process:: Output > {
116+ let mut cmd = clone_command ( & self . 0 ) ;
117+ cmd. args ( inputs) ;
118+ cmd. args ( [ "-o" , output] ) ;
119+
120+ cmd. output ( )
153121 }
154122}
0 commit comments