1
- use crate :: config:: { set_config, ConfigInfo } ;
2
- use crate :: utils:: {
3
- get_gcc_path, run_command, run_command_with_output_and_env, walk_dir,
4
- } ;
1
+ use crate :: config:: { Channel , ConfigInfo } ;
2
+ use crate :: utils:: { get_gcc_path, run_command, run_command_with_output_and_env, walk_dir} ;
5
3
use std:: collections:: HashMap ;
6
4
use std:: ffi:: OsStr ;
7
5
use std:: fs;
8
6
use std:: path:: Path ;
9
7
10
8
#[ derive( Default ) ]
11
9
struct BuildArg {
12
- codegen_release_channel : bool ,
13
- sysroot_release_channel : bool ,
14
- sysroot_panic_abort : bool ,
15
10
flags : Vec < String > ,
16
11
gcc_path : String ,
12
+ config_info : ConfigInfo ,
17
13
}
18
14
19
15
impl BuildArg {
@@ -28,14 +24,9 @@ impl BuildArg {
28
24
29
25
while let Some ( arg) = args. next ( ) {
30
26
match arg. as_str ( ) {
31
- "--release" => build_arg. codegen_release_channel = true ,
32
- "--release-sysroot" => build_arg. sysroot_release_channel = true ,
33
27
"--no-default-features" => {
34
28
build_arg. flags . push ( "--no-default-features" . to_string ( ) ) ;
35
29
}
36
- "--sysroot-panic-abort" => {
37
- build_arg. sysroot_panic_abort = true ;
38
- } ,
39
30
"--features" => {
40
31
if let Some ( arg) = args. next ( ) {
41
32
build_arg. flags . push ( "--features" . to_string ( ) ) ;
@@ -50,25 +41,11 @@ impl BuildArg {
50
41
Self :: usage ( ) ;
51
42
return Ok ( None ) ;
52
43
}
53
- "--target-triple" => {
54
- if args. next ( ) . is_some ( ) {
55
- // Handled in config.rs.
56
- } else {
57
- return Err (
58
- "Expected a value after `--target-triple`, found nothing" . to_string ( )
59
- ) ;
60
- }
61
- }
62
- "--target" => {
63
- if args. next ( ) . is_some ( ) {
64
- // Handled in config.rs.
65
- } else {
66
- return Err (
67
- "Expected a value after `--target`, found nothing" . to_string ( )
68
- ) ;
44
+ arg => {
45
+ if !build_arg. config_info . parse_argument ( arg, & mut args) ? {
46
+ return Err ( format ! ( "Unknown argument `{}`" , arg) ) ;
69
47
}
70
48
}
71
- arg => return Err ( format ! ( "Unknown argument `{}`" , arg) ) ,
72
49
}
73
50
}
74
51
Ok ( Some ( build_arg) )
@@ -79,29 +56,20 @@ impl BuildArg {
79
56
r#"
80
57
`build` command help:
81
58
82
- --release : Build codegen in release mode
83
- --release-sysroot : Build sysroot in release mode
84
- --sysroot-panic-abort : Build the sysroot without unwinding support.
85
59
--no-default-features : Add `--no-default-features` flag
86
- --features [arg] : Add a new feature [arg]
87
- --target-triple [arg] : Set the target triple to [arg]
88
- --help : Show this help
89
- "#
90
- )
60
+ --features [arg] : Add a new feature [arg]"#
61
+ ) ;
62
+ ConfigInfo :: show_usage ( ) ;
63
+ println ! ( " --help : Show this help" ) ;
91
64
}
92
65
}
93
66
94
- fn build_sysroot (
95
- env : & mut HashMap < String , String > ,
96
- args : & BuildArg ,
97
- config : & ConfigInfo ,
98
- ) -> Result < ( ) , String > {
99
- std:: env:: set_current_dir ( "build_sysroot" )
100
- . map_err ( |error| format ! ( "Failed to go to `build_sysroot` directory: {:?}" , error) ) ?;
67
+ pub fn build_sysroot ( env : & HashMap < String , String > , config : & ConfigInfo ) -> Result < ( ) , String > {
68
+ let start_dir = Path :: new ( "build_sysroot" ) ;
101
69
// Cleanup for previous run
102
70
// Clean target dir except for build scripts and incremental cache
103
71
let _ = walk_dir (
104
- "target" ,
72
+ start_dir . join ( "target" ) ,
105
73
|dir : & Path | {
106
74
for top in & [ "debug" , "release" ] {
107
75
let _ = fs:: remove_dir_all ( dir. join ( top) . join ( "build" ) ) ;
@@ -138,79 +106,93 @@ fn build_sysroot(
138
106
|_| Ok ( ( ) ) ,
139
107
) ;
140
108
141
- let _ = fs:: remove_file ( "Cargo.lock" ) ;
142
- let _ = fs:: remove_file ( "test_target/Cargo.lock" ) ;
143
- let _ = fs:: remove_dir_all ( "sysroot" ) ;
109
+ let _ = fs:: remove_file ( start_dir . join ( "Cargo.lock" ) ) ;
110
+ let _ = fs:: remove_file ( start_dir . join ( "test_target/Cargo.lock" ) ) ;
111
+ let _ = fs:: remove_dir_all ( start_dir . join ( "sysroot" ) ) ;
144
112
145
113
// Builds libs
146
- let mut rustflags = env
147
- . get ( "RUSTFLAGS" )
148
- . cloned ( )
149
- . unwrap_or_default ( ) ;
150
- if args. sysroot_panic_abort {
114
+ let mut rustflags = env. get ( "RUSTFLAGS" ) . cloned ( ) . unwrap_or_default ( ) ;
115
+ if config. sysroot_panic_abort {
151
116
rustflags. push_str ( " -Cpanic=abort -Zpanic-abort-tests" ) ;
152
117
}
153
- env. insert (
154
- "RUSTFLAGS" . to_string ( ) ,
155
- format ! ( "{} -Zmir-opt-level=3" , rustflags) ,
156
- ) ;
157
- let channel = if args. sysroot_release_channel {
118
+ rustflags. push_str ( " -Z force-unstable-if-unmarked" ) ;
119
+ let mut env = env. clone ( ) ;
120
+ let channel = if config. sysroot_release_channel {
121
+ env. insert (
122
+ "RUSTFLAGS" . to_string ( ) ,
123
+ format ! ( "{} -Zmir-opt-level=3" , rustflags) ,
124
+ ) ;
158
125
run_command_with_output_and_env (
159
126
& [
160
127
& "cargo" ,
161
128
& "build" ,
129
+ & "--release" ,
162
130
& "--target" ,
163
131
& config. target ,
164
- & "--release" ,
165
132
] ,
166
- None ,
133
+ Some ( start_dir ) ,
167
134
Some ( & env) ,
168
135
) ?;
169
136
"release"
170
137
} else {
138
+ env. insert ( "RUSTFLAGS" . to_string ( ) , rustflags) ;
139
+
171
140
run_command_with_output_and_env (
172
- & [
173
- & "cargo" ,
174
- & "build" ,
175
- & "--target" ,
176
- & config. target ,
177
- ] ,
178
- None ,
179
- Some ( env) ,
141
+ & [ & "cargo" , & "build" , & "--target" , & config. target ] ,
142
+ Some ( start_dir) ,
143
+ Some ( & env) ,
180
144
) ?;
181
145
"debug"
182
146
} ;
183
147
184
148
// Copy files to sysroot
185
- let sysroot_path = format ! ( "sysroot/lib/rustlib/{}/lib/" , config. target_triple) ;
186
- fs:: create_dir_all ( & sysroot_path)
187
- . map_err ( |error| format ! ( "Failed to create directory `{}`: {:?}" , sysroot_path, error) ) ?;
149
+ let sysroot_path = start_dir. join ( format ! ( "sysroot/lib/rustlib/{}/lib/" , config. target_triple) ) ;
150
+ fs:: create_dir_all ( & sysroot_path) . map_err ( |error| {
151
+ format ! (
152
+ "Failed to create directory `{}`: {:?}" ,
153
+ sysroot_path. display( ) ,
154
+ error
155
+ )
156
+ } ) ?;
188
157
let copier = |dir_to_copy : & Path | {
158
+ // FIXME: should not use shell command!
189
159
run_command ( & [ & "cp" , & "-r" , & dir_to_copy, & sysroot_path] , None ) . map ( |_| ( ) )
190
160
} ;
191
161
walk_dir (
192
- & format ! ( "target/{}/{}/deps" , config. target_triple, channel) ,
162
+ start_dir . join ( & format ! ( "target/{}/{}/deps" , config. target_triple, channel) ) ,
193
163
copier,
194
164
copier,
195
165
) ?;
196
166
197
167
// Copy the source files to the sysroot (Rust for Linux needs this).
198
168
let sysroot_src_path = "sysroot/lib/rustlib/src/rust" ;
199
- fs:: create_dir_all ( & sysroot_src_path)
200
- . map_err ( |error| format ! ( "Failed to create directory `{}`: {:?}" , sysroot_src_path, error) ) ?;
201
- run_command ( & [ & "cp" , & "-r" , & "sysroot_src/library/" , & sysroot_src_path] , None ) ?;
169
+ fs:: create_dir_all ( & sysroot_src_path) . map_err ( |error| {
170
+ format ! (
171
+ "Failed to create directory `{}`: {:?}" ,
172
+ sysroot_src_path, error
173
+ )
174
+ } ) ?;
175
+ run_command (
176
+ & [
177
+ & "cp" ,
178
+ & "-r" ,
179
+ & start_dir. join ( "sysroot_src/library/" ) ,
180
+ & sysroot_src_path,
181
+ ] ,
182
+ None ,
183
+ ) ?;
202
184
203
185
Ok ( ( ) )
204
186
}
205
187
206
- fn build_codegen ( args : & BuildArg ) -> Result < ( ) , String > {
188
+ fn build_codegen ( args : & mut BuildArg ) -> Result < ( ) , String > {
207
189
let mut env = HashMap :: new ( ) ;
208
190
209
191
env. insert ( "LD_LIBRARY_PATH" . to_string ( ) , args. gcc_path . clone ( ) ) ;
210
192
env. insert ( "LIBRARY_PATH" . to_string ( ) , args. gcc_path . clone ( ) ) ;
211
193
212
194
let mut command: Vec < & dyn AsRef < OsStr > > = vec ! [ & "cargo" , & "rustc" ] ;
213
- if args. codegen_release_channel {
195
+ if args. config_info . channel == Channel :: Release {
214
196
command. push ( & "--release" ) ;
215
197
env. insert ( "CHANNEL" . to_string ( ) , "release" . to_string ( ) ) ;
216
198
env. insert ( "CARGO_INCREMENTAL" . to_string ( ) , "1" . to_string ( ) ) ;
@@ -223,7 +205,7 @@ fn build_codegen(args: &BuildArg) -> Result<(), String> {
223
205
}
224
206
run_command_with_output_and_env ( & command, None , Some ( & env) ) ?;
225
207
226
- let config = set_config ( & mut env, & [ ] , Some ( & args. gcc_path ) ) ?;
208
+ args . config_info . setup ( & mut env, Some ( & args. gcc_path ) ) ?;
227
209
228
210
// We voluntarily ignore the error.
229
211
let _ = fs:: remove_dir_all ( "target/out" ) ;
@@ -236,19 +218,15 @@ fn build_codegen(args: &BuildArg) -> Result<(), String> {
236
218
} ) ?;
237
219
238
220
println ! ( "[BUILD] sysroot" ) ;
239
- build_sysroot (
240
- & mut env,
241
- args,
242
- & config,
243
- ) ?;
221
+ build_sysroot ( & env, & args. config_info ) ?;
244
222
Ok ( ( ) )
245
223
}
246
224
247
225
pub fn run ( ) -> Result < ( ) , String > {
248
- let args = match BuildArg :: new ( ) ? {
226
+ let mut args = match BuildArg :: new ( ) ? {
249
227
Some ( args) => args,
250
228
None => return Ok ( ( ) ) ,
251
229
} ;
252
- build_codegen ( & args) ?;
230
+ build_codegen ( & mut args) ?;
253
231
Ok ( ( ) )
254
232
}
0 commit comments