1- use anstream:: eprintln as println;
2- use color_print:: cprintln;
31use std:: path:: { Path , PathBuf } ;
42use std:: process:: Command ;
53
6- use crate :: log:: Log ;
4+ use anstream:: eprintln as println;
5+ use color_print:: cprintln;
6+
7+ use crate :: Run ;
78
89#[ derive( Debug ) ]
910pub struct Manifest {
@@ -12,111 +13,95 @@ pub struct Manifest {
1213 pub out_dir : PathBuf ,
1314}
1415
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" )
2227 } else {
23- cprintln ! ( "<b>[BUILD]</b> {} {}" , step_type , name ) ;
28+ Path :: new ( "crates/target/debug/librustc_codegen_c.so" )
2429 }
2530 }
2631
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
2949 }
3050}
3151
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 ( ) ) ;
4263
4364 let mut command = Command :: new ( "cargo" ) ;
4465 command. arg ( "build" ) . args ( [ "--manifest-path" , "crates/Cargo.toml" ] ) ;
45- if self . verbose {
66+ if manifest . verbose {
4667 command. args ( [ "-v" ] ) ;
4768 }
48- if self . release {
69+ if manifest . release {
4970 command. arg ( "--release" ) ;
5071 }
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 ( ) ) ;
6277
6378 // 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 ) ;
7082 }
83+
7184 let cc = std:: env:: var ( "CC" ) . unwrap_or ( "clang" . to_string ( ) ) ;
7285
7386 // cmd: Compile runtime.c
7487 let mut command = Command :: new ( & cc) ;
7588 command
7689 . arg ( "rust_runtime/rust_runtime.c" )
7790 . arg ( "-o" )
78- . arg ( self . out_dir . join ( "rust_runtime.o" ) )
91+ . arg ( manifest . out_dir . join ( "rust_runtime.o" ) )
7992 . 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) ;
8394
8495 // cmd: Create static library
8596 let mut command = Command :: new ( "ar" ) ;
8697 command
8798 . 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) ;
102102 }
103103
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
121106 }
122107}
0 commit comments