1
1
use crate :: config:: set_config;
2
- use crate :: utils:: { get_gcc_path, run_command_with_env, run_command_with_output, walk_dir} ;
2
+ use crate :: utils:: {
3
+ get_gcc_path, run_command, run_command_with_env, run_command_with_output_and_env, walk_dir,
4
+ } ;
3
5
use std:: collections:: HashMap ;
4
6
use std:: ffi:: OsStr ;
5
7
use std:: fs;
@@ -9,7 +11,6 @@ use std::path::Path;
9
11
struct BuildArg {
10
12
codegen_release_channel : bool ,
11
13
sysroot_release_channel : bool ,
12
- no_default_features : bool ,
13
14
features : Vec < String > ,
14
15
gcc_path : String ,
15
16
}
@@ -21,13 +22,16 @@ impl BuildArg {
21
22
gcc_path,
22
23
..Default :: default ( )
23
24
} ;
25
+ // We skip binary name and the `build` command.
24
26
let mut args = std:: env:: args ( ) . skip ( 2 ) ;
25
27
26
28
while let Some ( arg) = args. next ( ) {
27
29
match arg. as_str ( ) {
28
30
"--release" => build_arg. codegen_release_channel = true ,
29
31
"--release-sysroot" => build_arg. sysroot_release_channel = true ,
30
- "--no-default-features" => build_arg. no_default_features = true ,
32
+ "--no-default-features" => {
33
+ build_arg. features . push ( "--no-default-features" . to_string ( ) ) ;
34
+ }
31
35
"--features" => {
32
36
if let Some ( arg) = args. next ( ) {
33
37
build_arg. features . push ( arg. as_str ( ) . into ( ) ) ;
@@ -41,7 +45,7 @@ impl BuildArg {
41
45
Self :: usage ( ) ;
42
46
return Ok ( None ) ;
43
47
}
44
- a => return Err ( format ! ( "Unknown argument `{a }`" ) ) ,
48
+ arg => return Err ( format ! ( "Unknown argument `{}`" , arg ) ) ,
45
49
}
46
50
}
47
51
Ok ( Some ( build_arg) )
@@ -70,8 +74,8 @@ fn build_sysroot(
70
74
std:: env:: set_current_dir ( "build_sysroot" )
71
75
. map_err ( |e| format ! ( "Failed to go to `build_sysroot` directory: {e:?}" ) ) ?;
72
76
// Cleanup for previous run
73
- // v Clean target dir except for build scripts and incremental cache
74
- let _e = walk_dir (
77
+ // Clean target dir except for build scripts and incremental cache
78
+ let _e = walk_dir (
75
79
"target" ,
76
80
|dir : & Path | {
77
81
for top in & [ "debug" , "release" ] {
@@ -116,14 +120,14 @@ fn build_sysroot(
116
120
// Builds libs
117
121
let channel = if release_mode {
118
122
let rustflags = env
119
- . get ( & "RUSTFLAGS" . to_owned ( ) )
123
+ . get ( & "RUSTFLAGS" . to_string ( ) )
120
124
. cloned ( )
121
125
. unwrap_or_default ( ) ;
122
126
env. insert (
123
- "RUSTFLAGS" . to_owned ( ) ,
124
- format ! ( "{rustflags } -Zmir-opt-level=3" ) ,
127
+ "RUSTFLAGS" . to_string ( ) ,
128
+ format ! ( "{} -Zmir-opt-level=3" , rustflags ) ,
125
129
) ;
126
- run_command_with_output (
130
+ run_command_with_output_and_env (
127
131
& [
128
132
& "cargo" ,
129
133
& "build" ,
@@ -136,7 +140,7 @@ fn build_sysroot(
136
140
) ?;
137
141
"release"
138
142
} else {
139
- run_command_with_output (
143
+ run_command_with_output_and_env (
140
144
& [
141
145
& "cargo" ,
142
146
& "build" ,
@@ -152,12 +156,12 @@ fn build_sysroot(
152
156
} ;
153
157
154
158
// Copy files to sysroot
155
- let sysroot_path = format ! ( "sysroot/lib/rustlib/{target_triple }/lib/" ) ;
159
+ let sysroot_path = format ! ( "sysroot/lib/rustlib/{}/lib/" , target_triple ) ;
156
160
fs:: create_dir_all ( & sysroot_path)
157
- . map_err ( |e| format ! ( "Failed to create directory `{sysroot_path }`: {e :?}" ) ) ?;
158
- let copier = |d : & Path | run_command_with_output ( & [ & "cp" , & "-r" , & d, & sysroot_path] , None , None ) ;
161
+ . map_err ( |e| format ! ( "Failed to create directory `{}`: {:?}" , sysroot_path , e ) ) ?;
162
+ let copier = |d : & Path | run_command ( & [ & "cp" , & "-r" , & d, & sysroot_path] , None ) . map ( |_| ( ) ) ;
159
163
walk_dir (
160
- & format ! ( "target/{target_triple }/{channel }/deps" ) ,
164
+ & format ! ( "target/{}/{}/deps" , target_triple , channel ) ,
161
165
copier,
162
166
copier,
163
167
) ?;
@@ -169,21 +173,25 @@ fn build_codegen(args: &BuildArg) -> Result<(), String> {
169
173
let mut env = HashMap :: new ( ) ;
170
174
171
175
let current_dir =
172
- std:: env:: current_dir ( ) . map_err ( |e| format ! ( "`current_dir` failed: {e:?}" ) ) ?;
173
- env. insert (
174
- "RUST_COMPILER_RT_ROOT" . to_owned ( ) ,
175
- format ! ( "{}" , current_dir. join( "llvm/compiler-rt" ) . display( ) ) ,
176
- ) ;
177
- env. insert ( "LD_LIBRARY_PATH" . to_owned ( ) , args. gcc_path . clone ( ) ) ;
178
- env. insert ( "LIBRARY_PATH" . to_owned ( ) , args. gcc_path . clone ( ) ) ;
176
+ std:: env:: current_dir ( ) . map_err ( |e| format ! ( "`current_dir` failed: {:?}" , e) ) ?;
177
+ if let Ok ( rt_root) = std:: env:: var ( "RUST_COMPILER_RT_ROOT" ) {
178
+ env. insert ( "RUST_COMPILER_RT_ROOT" . to_string ( ) , rt_root) ;
179
+ } else {
180
+ env. insert (
181
+ "RUST_COMPILER_RT_ROOT" . to_string ( ) ,
182
+ format ! ( "{}" , current_dir. join( "llvm/compiler-rt" ) . display( ) ) ,
183
+ ) ;
184
+ }
185
+ env. insert ( "LD_LIBRARY_PATH" . to_string ( ) , args. gcc_path . clone ( ) ) ;
186
+ env. insert ( "LIBRARY_PATH" . to_string ( ) , args. gcc_path . clone ( ) ) ;
179
187
180
188
let mut command: Vec < & dyn AsRef < OsStr > > = vec ! [ & "cargo" , & "rustc" ] ;
181
189
if args. codegen_release_channel {
182
190
command. push ( & "--release" ) ;
183
- env. insert ( "CHANNEL" . to_owned ( ) , "release" . to_owned ( ) ) ;
184
- env. insert ( "CARGO_INCREMENTAL" . to_owned ( ) , "1" . to_owned ( ) ) ;
191
+ env. insert ( "CHANNEL" . to_string ( ) , "release" . to_string ( ) ) ;
192
+ env. insert ( "CARGO_INCREMENTAL" . to_string ( ) , "1" . to_string ( ) ) ;
185
193
} else {
186
- env. insert ( "CHANNEL" . to_owned ( ) , "debug" . to_owned ( ) ) ;
194
+ env. insert ( "CHANNEL" . to_string ( ) , "debug" . to_string ( ) ) ;
187
195
}
188
196
let ref_features = args. features . iter ( ) . map ( |s| s. as_str ( ) ) . collect :: < Vec < _ > > ( ) ;
189
197
for feature in & ref_features {
@@ -197,7 +205,7 @@ fn build_codegen(args: &BuildArg) -> Result<(), String> {
197
205
let _e = fs:: remove_dir_all ( "target/out" ) ;
198
206
let gccjit_target = "target/out/gccjit" ;
199
207
fs:: create_dir_all ( gccjit_target)
200
- . map_err ( |e| format ! ( "Failed to create directory `{gccjit_target }`: {e :?}" ) ) ?;
208
+ . map_err ( |e| format ! ( "Failed to create directory `{}`: {:?}" , gccjit_target , e ) ) ?;
201
209
202
210
println ! ( "[BUILD] sysroot" ) ;
203
211
build_sysroot (
@@ -210,7 +218,7 @@ fn build_codegen(args: &BuildArg) -> Result<(), String> {
210
218
211
219
pub fn run ( ) -> Result < ( ) , String > {
212
220
let args = match BuildArg :: new ( ) ? {
213
- Some ( a ) => a ,
221
+ Some ( args ) => args ,
214
222
None => return Ok ( ( ) ) ,
215
223
} ;
216
224
build_codegen ( & args) ?;
0 commit comments