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,27 +22,31 @@ 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 ( ) {
37
+ build_arg. features . push ( "--features" . to_string ( ) ) ;
33
38
build_arg. features . push ( arg. as_str ( ) . into ( ) ) ;
34
39
} else {
35
- return Err ( format ! (
36
- "Expected a value after `--features`, found nothing"
37
- ) ) ;
40
+ return Err (
41
+ "Expected a value after `--features`, found nothing" . to_string ( )
42
+ ) ;
38
43
}
39
44
}
40
45
"--help" => {
41
46
Self :: usage ( ) ;
42
47
return Ok ( None ) ;
43
48
}
44
- a => return Err ( format ! ( "Unknown argument `{a }`" ) ) ,
49
+ arg => return Err ( format ! ( "Unknown argument `{}`" , arg ) ) ,
45
50
}
46
51
}
47
52
Ok ( Some ( build_arg) )
@@ -68,37 +73,37 @@ fn build_sysroot(
68
73
target_triple : & str ,
69
74
) -> Result < ( ) , String > {
70
75
std:: env:: set_current_dir ( "build_sysroot" )
71
- . map_err ( |e | format ! ( "Failed to go to `build_sysroot` directory: {e :?}" ) ) ?;
76
+ . map_err ( |error | format ! ( "Failed to go to `build_sysroot` directory: {:?}" , error ) ) ?;
72
77
// Cleanup for previous run
73
- // v Clean target dir except for build scripts and incremental cache
74
- let _e = walk_dir (
78
+ // Clean target dir except for build scripts and incremental cache
79
+ let _ = walk_dir (
75
80
"target" ,
76
81
|dir : & Path | {
77
82
for top in & [ "debug" , "release" ] {
78
- let _e = fs:: remove_dir_all ( dir. join ( top) . join ( "build" ) ) ;
79
- let _e = fs:: remove_dir_all ( dir. join ( top) . join ( "deps" ) ) ;
80
- let _e = fs:: remove_dir_all ( dir. join ( top) . join ( "examples" ) ) ;
81
- let _e = fs:: remove_dir_all ( dir. join ( top) . join ( "native" ) ) ;
83
+ let _ = fs:: remove_dir_all ( dir. join ( top) . join ( "build" ) ) ;
84
+ let _ = fs:: remove_dir_all ( dir. join ( top) . join ( "deps" ) ) ;
85
+ let _ = fs:: remove_dir_all ( dir. join ( top) . join ( "examples" ) ) ;
86
+ let _ = fs:: remove_dir_all ( dir. join ( top) . join ( "native" ) ) ;
82
87
83
- let _e = walk_dir (
88
+ let _ = walk_dir (
84
89
dir. join ( top) ,
85
90
|sub_dir : & Path | {
86
91
if sub_dir
87
92
. file_name ( )
88
- . map ( |s| s . to_str ( ) . unwrap ( ) . starts_with ( "libsysroot" ) )
93
+ . map ( |filename| filename . to_str ( ) . unwrap ( ) . starts_with ( "libsysroot" ) )
89
94
. unwrap_or ( false )
90
95
{
91
- let _e = fs:: remove_dir_all ( sub_dir) ;
96
+ let _ = fs:: remove_dir_all ( sub_dir) ;
92
97
}
93
98
Ok ( ( ) )
94
99
} ,
95
100
|file : & Path | {
96
101
if file
97
102
. file_name ( )
98
- . map ( |s| s . to_str ( ) . unwrap ( ) . starts_with ( "libsysroot" ) )
103
+ . map ( |filename| filename . to_str ( ) . unwrap ( ) . starts_with ( "libsysroot" ) )
99
104
. unwrap_or ( false )
100
105
{
101
- let _e = fs:: remove_file ( file) ;
106
+ let _ = fs:: remove_file ( file) ;
102
107
}
103
108
Ok ( ( ) )
104
109
} ,
@@ -109,21 +114,21 @@ fn build_sysroot(
109
114
|_| Ok ( ( ) ) ,
110
115
) ;
111
116
112
- let _e = fs:: remove_file ( "Cargo.lock" ) ;
113
- let _e = fs:: remove_file ( "test_target/Cargo.lock" ) ;
114
- let _e = fs:: remove_dir_all ( "sysroot" ) ;
117
+ let _ = fs:: remove_file ( "Cargo.lock" ) ;
118
+ let _ = fs:: remove_file ( "test_target/Cargo.lock" ) ;
119
+ let _ = fs:: remove_dir_all ( "sysroot" ) ;
115
120
116
121
// Builds libs
117
122
let channel = if release_mode {
118
123
let rustflags = env
119
- . get ( & "RUSTFLAGS" . to_owned ( ) )
124
+ . get ( "RUSTFLAGS" )
120
125
. cloned ( )
121
126
. unwrap_or_default ( ) ;
122
127
env. insert (
123
- "RUSTFLAGS" . to_owned ( ) ,
124
- format ! ( "{rustflags } -Zmir-opt-level=3" ) ,
128
+ "RUSTFLAGS" . to_string ( ) ,
129
+ format ! ( "{} -Zmir-opt-level=3" , rustflags ) ,
125
130
) ;
126
- run_command_with_output (
131
+ run_command_with_output_and_env (
127
132
& [
128
133
& "cargo" ,
129
134
& "build" ,
@@ -136,7 +141,7 @@ fn build_sysroot(
136
141
) ?;
137
142
"release"
138
143
} else {
139
- run_command_with_output (
144
+ run_command_with_output_and_env (
140
145
& [
141
146
& "cargo" ,
142
147
& "build" ,
@@ -152,12 +157,14 @@ fn build_sysroot(
152
157
} ;
153
158
154
159
// Copy files to sysroot
155
- let sysroot_path = format ! ( "sysroot/lib/rustlib/{target_triple }/lib/" ) ;
160
+ let sysroot_path = format ! ( "sysroot/lib/rustlib/{}/lib/" , target_triple ) ;
156
161
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 ) ;
162
+ . map_err ( |error| format ! ( "Failed to create directory `{}`: {:?}" , sysroot_path, error) ) ?;
163
+ let copier = |dir_to_copy : & Path | {
164
+ run_command ( & [ & "cp" , & "-r" , & dir_to_copy, & sysroot_path] , None ) . map ( |_| ( ) )
165
+ } ;
159
166
walk_dir (
160
- & format ! ( "target/{target_triple }/{channel }/deps" ) ,
167
+ & format ! ( "target/{}/{}/deps" , target_triple , channel ) ,
161
168
copier,
162
169
copier,
163
170
) ?;
@@ -169,21 +176,25 @@ fn build_codegen(args: &BuildArg) -> Result<(), String> {
169
176
let mut env = HashMap :: new ( ) ;
170
177
171
178
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 ( ) ) ;
179
+ std:: env:: current_dir ( ) . map_err ( |error| format ! ( "`current_dir` failed: {:?}" , error) ) ?;
180
+ if let Ok ( rt_root) = std:: env:: var ( "RUST_COMPILER_RT_ROOT" ) {
181
+ env. insert ( "RUST_COMPILER_RT_ROOT" . to_string ( ) , rt_root) ;
182
+ } else {
183
+ env. insert (
184
+ "RUST_COMPILER_RT_ROOT" . to_string ( ) ,
185
+ format ! ( "{}" , current_dir. join( "llvm/compiler-rt" ) . display( ) ) ,
186
+ ) ;
187
+ }
188
+ env. insert ( "LD_LIBRARY_PATH" . to_string ( ) , args. gcc_path . clone ( ) ) ;
189
+ env. insert ( "LIBRARY_PATH" . to_string ( ) , args. gcc_path . clone ( ) ) ;
179
190
180
191
let mut command: Vec < & dyn AsRef < OsStr > > = vec ! [ & "cargo" , & "rustc" ] ;
181
192
if args. codegen_release_channel {
182
193
command. push ( & "--release" ) ;
183
- env. insert ( "CHANNEL" . to_owned ( ) , "release" . to_owned ( ) ) ;
184
- env. insert ( "CARGO_INCREMENTAL" . to_owned ( ) , "1" . to_owned ( ) ) ;
194
+ env. insert ( "CHANNEL" . to_string ( ) , "release" . to_string ( ) ) ;
195
+ env. insert ( "CARGO_INCREMENTAL" . to_string ( ) , "1" . to_string ( ) ) ;
185
196
} else {
186
- env. insert ( "CHANNEL" . to_owned ( ) , "debug" . to_owned ( ) ) ;
197
+ env. insert ( "CHANNEL" . to_string ( ) , "debug" . to_string ( ) ) ;
187
198
}
188
199
let ref_features = args. features . iter ( ) . map ( |s| s. as_str ( ) ) . collect :: < Vec < _ > > ( ) ;
189
200
for feature in & ref_features {
@@ -194,10 +205,14 @@ fn build_codegen(args: &BuildArg) -> Result<(), String> {
194
205
let config = set_config ( & mut env, & [ ] , Some ( & args. gcc_path ) ) ?;
195
206
196
207
// We voluntarily ignore the error.
197
- let _e = fs:: remove_dir_all ( "target/out" ) ;
208
+ let _ = fs:: remove_dir_all ( "target/out" ) ;
198
209
let gccjit_target = "target/out/gccjit" ;
199
- fs:: create_dir_all ( gccjit_target)
200
- . map_err ( |e| format ! ( "Failed to create directory `{gccjit_target}`: {e:?}" ) ) ?;
210
+ fs:: create_dir_all ( gccjit_target) . map_err ( |error| {
211
+ format ! (
212
+ "Failed to create directory `{}`: {:?}" ,
213
+ gccjit_target, error
214
+ )
215
+ } ) ?;
201
216
202
217
println ! ( "[BUILD] sysroot" ) ;
203
218
build_sysroot (
@@ -210,7 +225,7 @@ fn build_codegen(args: &BuildArg) -> Result<(), String> {
210
225
211
226
pub fn run ( ) -> Result < ( ) , String > {
212
227
let args = match BuildArg :: new ( ) ? {
213
- Some ( a ) => a ,
228
+ Some ( args ) => args ,
214
229
None => return Ok ( ( ) ) ,
215
230
} ;
216
231
build_codegen ( & args) ?;
0 commit comments