1
- use anstream:: eprintln as println;
2
- use color_print:: cprintln;
3
1
use std:: path:: { Path , PathBuf } ;
4
2
use std:: process:: Command ;
5
3
6
- use crate :: log:: Log ;
4
+ use anstream:: eprintln as println;
5
+ use color_print:: cprintln;
6
+
7
+ use crate :: Run ;
7
8
8
9
#[ derive( Debug ) ]
9
10
pub struct Manifest {
@@ -12,111 +13,95 @@ pub struct Manifest {
12
13
pub out_dir : PathBuf ,
13
14
}
14
15
15
- impl Log for Manifest {
16
- fn log_step ( & self , step_type : & str , name : & str , details : Vec < ( & str , & str ) > ) {
17
- if self . verbose {
18
- cprintln ! ( "<b>[BUILD]</b> {} {}" , step_type, name) ;
19
- for ( label, value) in details {
20
- cprintln ! ( " {}: {}" , label, value) ;
21
- }
16
+ impl Manifest {
17
+ /// Builds the rustc codegen c library
18
+ pub fn prepare ( & self ) {
19
+ let prepare = PrepareAction { verbose : self . verbose } ;
20
+ prepare. run ( & self ) ;
21
+ }
22
+
23
+ /// The path to the rustc codegen c library
24
+ pub fn codegen_backend ( & self ) -> & ' static Path {
25
+ if self . release {
26
+ Path :: new ( "crates/target/release/librustc_codegen_c.so" )
22
27
} else {
23
- cprintln ! ( "<b>[BUILD]</b> {} {}" , step_type , name ) ;
28
+ Path :: new ( "crates/target/debug/librustc_codegen_c.so" )
24
29
}
25
30
}
26
31
27
- fn is_verbose ( & self ) -> bool {
28
- self . verbose
32
+ /// The command to run rustc with the codegen backend
33
+ pub fn rustc ( & self ) -> Command {
34
+ let mut command = Command :: new ( "rustc" ) ;
35
+ command
36
+ . args ( [ "--edition" , "2021" ] )
37
+ . arg ( "-Z" )
38
+ . arg ( format ! ( "codegen-backend={}" , self . codegen_backend( ) . display( ) ) )
39
+ . args ( [ "-C" , "panic=abort" ] )
40
+ . args ( [ "-C" , "lto=false" ] )
41
+ . arg ( format ! ( "-Lall={}" , self . out_dir. display( ) ) )
42
+ . env ( "CFLAGS" , "-Irust_runtime" )
43
+ . arg ( "-lc" )
44
+ . arg ( "-lrust_runtime" ) ;
45
+ if self . verbose {
46
+ command. env ( "RUST_BACKTRACE" , "full" ) ;
47
+ }
48
+ command
29
49
}
30
50
}
31
51
32
- impl Manifest {
33
- /// Builds the rustc codegen c library
34
- pub fn prepare ( & self ) {
35
- // New step
36
- // Build codegen backend
37
- self . log_step (
38
- "preparing" ,
39
- "codegen backend" ,
40
- vec ! [ ( "target" , & self . codegen_backend( ) . display( ) . to_string( ) ) ] ,
41
- ) ;
52
+ struct PrepareAction {
53
+ verbose : bool ,
54
+ }
55
+
56
+ impl Run for PrepareAction {
57
+ const STEP_DISPLAY_NAME : & ' static str = "prepare" ;
58
+
59
+ fn run ( & self , manifest : & Manifest ) {
60
+ // action: Build codegen backend
61
+ self . log_action_start ( "building" , "codegen backend" ) ;
62
+ self . log_action_context ( "target" , manifest. codegen_backend ( ) . display ( ) ) ;
42
63
43
64
let mut command = Command :: new ( "cargo" ) ;
44
65
command. arg ( "build" ) . args ( [ "--manifest-path" , "crates/Cargo.toml" ] ) ;
45
- if self . verbose {
66
+ if manifest . verbose {
46
67
command. args ( [ "-v" ] ) ;
47
68
}
48
- if self . release {
69
+ if manifest . release {
49
70
command. arg ( "--release" ) ;
50
71
}
51
- let status = command. status ( ) . unwrap ( ) ;
52
- let status = if self . verbose { Some ( status) } else { None } ;
53
- self . log_command ( "command" , & command, & status) ;
54
-
55
- // New step
56
- // Build runtime library
57
- self . log_step (
58
- "librust_runtime" ,
59
- "librust_runtime" ,
60
- vec ! [ ( "output" , & self . out_dir. display( ) . to_string( ) ) ] ,
61
- ) ;
72
+ self . command_status ( "build" , & mut command) ;
73
+
74
+ // action: Build runtime library
75
+ self . log_action_start ( "building" , "librust_runtime" ) ;
76
+ self . log_action_context ( "output dir" , & manifest. out_dir . to_path_buf ( ) . display ( ) ) ;
62
77
63
78
// cmd: Create output directory
64
- match std:: fs:: create_dir_all ( & self . out_dir ) {
65
- Ok ( _) => ( ) ,
66
- Err ( e) => {
67
- cprintln ! ( " <r>failed</r> to create output directory: {}" , e) ;
68
- std:: process:: exit ( 1 ) ;
69
- }
79
+ if let Err ( e) = std:: fs:: create_dir_all ( & manifest. out_dir ) {
80
+ cprintln ! ( " <r>failed</r> to create output directory: {}" , e) ;
81
+ std:: process:: exit ( 1 ) ;
70
82
}
83
+
71
84
let cc = std:: env:: var ( "CC" ) . unwrap_or ( "clang" . to_string ( ) ) ;
72
85
73
86
// cmd: Compile runtime.c
74
87
let mut command = Command :: new ( & cc) ;
75
88
command
76
89
. arg ( "rust_runtime/rust_runtime.c" )
77
90
. arg ( "-o" )
78
- . arg ( self . out_dir . join ( "rust_runtime.o" ) )
91
+ . arg ( manifest . out_dir . join ( "rust_runtime.o" ) )
79
92
. arg ( "-c" ) ;
80
- let status = command. status ( ) . unwrap ( ) ;
81
- let status = if self . verbose { Some ( status) } else { None } ;
82
- self . log_command ( "compile" , & command, & status) ;
93
+ self . command_status ( "build" , & mut command) ;
83
94
84
95
// cmd: Create static library
85
96
let mut command = Command :: new ( "ar" ) ;
86
97
command
87
98
. arg ( "rcs" )
88
- . arg ( self . out_dir . join ( "librust_runtime.a" ) )
89
- . arg ( self . out_dir . join ( "rust_runtime.o" ) ) ;
90
- let status = command. status ( ) . unwrap ( ) ;
91
- let status = if self . verbose { Some ( status) } else { None } ;
92
- self . log_command ( "archive" , & command, & status) ;
93
- }
94
-
95
- /// The path to the rustc codegen c library
96
- pub fn codegen_backend ( & self ) -> & ' static Path {
97
- if self . release {
98
- Path :: new ( "crates/target/release/librustc_codegen_c.so" )
99
- } else {
100
- Path :: new ( "crates/target/debug/librustc_codegen_c.so" )
101
- }
99
+ . arg ( manifest. out_dir . join ( "librust_runtime.a" ) )
100
+ . arg ( manifest. out_dir . join ( "rust_runtime.o" ) ) ;
101
+ self . command_status ( "archive" , & mut command) ;
102
102
}
103
103
104
- /// The command to run rustc with the codegen backend
105
- pub fn rustc ( & self ) -> Command {
106
- let mut command = Command :: new ( "rustc" ) ;
107
- command
108
- . args ( [ "--edition" , "2021" ] )
109
- . arg ( "-Z" )
110
- . arg ( format ! ( "codegen-backend={}" , self . codegen_backend( ) . display( ) ) )
111
- . args ( [ "-C" , "panic=abort" ] )
112
- . args ( [ "-C" , "lto=false" ] )
113
- . arg ( format ! ( "-Lall={}" , self . out_dir. display( ) ) )
114
- . env ( "CFLAGS" , "-Irust_runtime" )
115
- . arg ( "-lc" )
116
- . arg ( "-lrust_runtime" ) ;
117
- if self . verbose {
118
- command. env ( "RUST_BACKTRACE" , "full" ) ;
119
- }
120
- command
104
+ fn verbose ( & self ) -> bool {
105
+ self . verbose
121
106
}
122
107
}
0 commit comments