@@ -5,9 +5,7 @@ pub struct CompilationCommandBuilder {
5
5
cxx_toolchain_dir : Option < String > ,
6
6
arch_flags : Vec < String > ,
7
7
optimization : String ,
8
- include_paths : Vec < String > ,
9
8
project_root : Option < String > ,
10
- linker : Option < String > ,
11
9
extra_flags : Vec < String > ,
12
10
}
13
11
@@ -19,9 +17,7 @@ impl CompilationCommandBuilder {
19
17
cxx_toolchain_dir : None ,
20
18
arch_flags : Vec :: new ( ) ,
21
19
optimization : "2" . to_string ( ) ,
22
- include_paths : Vec :: new ( ) ,
23
20
project_root : None ,
24
- linker : None ,
25
21
extra_flags : Vec :: new ( ) ,
26
22
}
27
23
}
@@ -53,25 +49,12 @@ impl CompilationCommandBuilder {
53
49
self
54
50
}
55
51
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
-
64
52
/// Sets the root path of all the generated test files.
65
53
pub fn set_project_root ( mut self , path : & str ) -> Self {
66
54
self . project_root = Some ( path. to_string ( ) ) ;
67
55
self
68
56
}
69
57
70
- pub fn set_linker ( mut self , linker : String ) -> Self {
71
- self . linker = Some ( linker) ;
72
- self
73
- }
74
-
75
58
pub fn add_extra_flags ( mut self , flags : Vec < & str > ) -> Self {
76
59
let mut flags: Vec < String > = flags. into_iter ( ) . map ( |f| f. to_string ( ) ) . collect ( ) ;
77
60
self . extra_flags . append ( & mut flags) ;
@@ -104,30 +87,11 @@ impl CompilationCommandBuilder {
104
87
cpp_compiler. arg ( format ! ( "--target={target}" ) ) ;
105
88
}
106
89
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)
121
91
}
122
92
}
123
93
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 ) ;
131
95
132
96
fn clone_command ( command : & std:: process:: Command ) -> std:: process:: Command {
133
97
let mut cmd = std:: process:: Command :: new ( command. get_program ( ) ) ;
@@ -144,62 +108,15 @@ fn clone_command(command: &std::process::Command) -> std::process::Command {
144
108
}
145
109
146
110
impl CppCompilation {
111
+ pub fn command_mut ( & mut self ) -> & mut std:: process:: Command {
112
+ & mut self . 0
113
+ }
114
+
147
115
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 ( )
204
121
}
205
122
}
0 commit comments