@@ -104,30 +104,19 @@ impl CompilationCommandBuilder {
104
104
cpp_compiler. arg ( format ! ( "--target={target}" ) ) ;
105
105
}
106
106
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 {
108
108
cpp_compiler. args (
109
109
self . include_paths
110
110
. iter ( )
111
- . map ( |path| "--include-directory=" . to_string ( ) + cxx_toolchain_dir + path) ,
111
+ . map ( |path| format ! ( "--include-directory={ cxx_toolchain_dir}{ path}" ) ) ,
112
112
) ;
113
-
114
- CppCompilation :: CustomLinker {
115
- cpp_compiler,
116
- linker : linker. to_owned ( ) ,
117
- }
118
- } else {
119
- CppCompilation :: Simple ( cpp_compiler)
120
113
}
114
+
115
+ CppCompilation ( cpp_compiler)
121
116
}
122
117
}
123
118
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 ) ;
131
120
132
121
fn clone_command ( command : & std:: process:: Command ) -> std:: process:: Command {
133
122
let mut cmd = std:: process:: Command :: new ( command. get_program ( ) ) ;
@@ -144,85 +133,24 @@ fn clone_command(command: &std::process::Command) -> std::process::Command {
144
133
}
145
134
146
135
impl 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 ,
151
139
output : & str ,
152
140
) -> 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] ) ;
162
143
cmd. output ( )
163
144
}
164
145
165
- pub fn run (
146
+ pub fn link_executable (
166
147
& self ,
167
- includes : & [ String ] ,
168
- inputs : & [ String ] ,
148
+ inputs : impl Iterator < Item = String > ,
169
149
output : & str ,
170
150
) -> 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 ( )
227
155
}
228
156
}
0 commit comments