1616// by rust_library/rust_binary.
1717extern crate cargo_build_script_output_parser;
1818
19- use cargo_build_script_output_parser:: BuildScriptOutput ;
19+ use cargo_build_script_output_parser:: { BuildScriptOutput , CompileAndLinkFlags } ;
2020use std:: env;
21- use std:: fs:: { File , create_dir_all} ;
22- use std:: io:: Write ;
21+ use std:: fs:: { create_dir_all, write} ;
2322use std:: path:: Path ;
2423use std:: process:: { exit, Command } ;
2524
@@ -33,13 +32,8 @@ fn main() {
3332
3433 let mut args = env:: args ( ) . skip ( 1 ) ;
3534 let manifest_dir_env = env:: var ( "CARGO_MANIFEST_DIR" ) . expect ( "CARGO_MANIFEST_DIR was not set" ) ;
36- let out_dir_env = env:: var ( "OUT_DIR" ) . expect ( "OUT_DIR was not set" ) ;
37- // For some reason RBE does not creat the output directory, force create it
38- create_dir_all ( out_dir_env. clone ( ) ) . expect ( & format ! ( "Failed to create OUT_DIR: {}" , out_dir_env) ) ;
3935 let rustc_env = env:: var ( "RUSTC" ) . expect ( "RUSTC was not set" ) ;
40- // Because of the Bazel's sandbox, bazel cannot provide full path, convert all relative path to correct path.
4136 let manifest_dir = exec_root. join ( & manifest_dir_env) ;
42- let out_dir = exec_root. join ( & out_dir_env) ;
4337 let rustc = exec_root. join ( & rustc_env) ;
4438
4539 let cc = env:: var_os ( "CC" ) . map ( |env_var| {
@@ -51,13 +45,17 @@ fn main() {
5145 }
5246 } ) ;
5347
54- match ( args. next ( ) , args. next ( ) , args. next ( ) , args. next ( ) , args. next ( ) ) {
55- ( Some ( progname) , Some ( crate_name) , Some ( envfile) , Some ( flagfile) , Some ( depenvfile) ) => {
48+ match ( args. next ( ) , args. next ( ) , args. next ( ) , args. next ( ) , args. next ( ) , args. next ( ) , args. next ( ) ) {
49+ ( Some ( progname) , Some ( crate_name) , Some ( out_dir) , Some ( envfile) , Some ( flagfile) , Some ( linkflags) , Some ( depenvfile) ) => {
50+ let out_dir_abs = exec_root. join ( & out_dir) ;
51+ // For some reason Google's RBE does not create the output directory, force create it.
52+ create_dir_all ( & out_dir_abs) . expect ( & format ! ( "Failed to make output directory: {:?}" , out_dir_abs) ) ;
53+
5654 let mut command = Command :: new ( exec_root. join ( & progname) ) ;
5755 command
5856 . args ( args)
5957 . current_dir ( manifest_dir. clone ( ) )
60- . env ( "OUT_DIR" , out_dir )
58+ . env ( "OUT_DIR" , out_dir_abs )
6159 . env ( "CARGO_MANIFEST_DIR" , manifest_dir)
6260 . env ( "RUSTC" , rustc) ;
6361
@@ -66,21 +64,20 @@ fn main() {
6664 }
6765
6866 let output = BuildScriptOutput :: from_command ( & mut command) ;
69- let mut f =
70- File :: create ( & envfile) . expect ( & format ! ( "Unable to create file {}" , envfile) ) ;
71- f. write_all ( BuildScriptOutput :: to_env ( & output) . as_bytes ( ) )
72- . expect ( & format ! ( "Unable to write file {}" , envfile) ) ;
73- let mut f =
74- File :: create ( & depenvfile) . expect ( & format ! ( "Unable to create file {}" , depenvfile) ) ;
75- f. write_all ( BuildScriptOutput :: to_dep_env ( & output, & crate_name) . as_bytes ( ) )
76- . expect ( & format ! ( "Unable to write file {}" , depenvfile) ) ;
77- let mut f =
78- File :: create ( & flagfile) . expect ( & format ! ( "Unable to create file {}" , flagfile) ) ;
79- f. write_all ( BuildScriptOutput :: to_flags ( & output) . as_bytes ( ) )
80- . expect ( & format ! ( "Unable to write file {}" , flagfile) ) ;
67+ write ( & envfile, BuildScriptOutput :: to_env ( & output) . as_bytes ( ) )
68+ . expect ( & format ! ( "Unable to write file {:?}" , envfile) ) ;
69+ write ( & depenvfile, BuildScriptOutput :: to_dep_env ( & output, & crate_name) . as_bytes ( ) )
70+ . expect ( & format ! ( "Unable to write file {:?}" , depenvfile) ) ;
71+
72+ let CompileAndLinkFlags { compile_flags, link_flags } = BuildScriptOutput :: to_flags ( & output, & exec_root. to_string_lossy ( ) ) ;
73+
74+ write ( & flagfile, compile_flags. as_bytes ( ) )
75+ . expect ( & format ! ( "Unable to write file {:?}" , flagfile) ) ;
76+ write ( & linkflags, link_flags. as_bytes ( ) )
77+ . expect ( & format ! ( "Unable to write file {:?}" , linkflags) ) ;
8178 }
8279 _ => {
83- eprintln ! ( "Usage: $0 progname crate_name envfile flagfile depenvfile [arg1...argn]" ) ;
80+ eprintln ! ( "Usage: $0 progname crate_name out_dir envfile flagfile linkflagfile depenvfile [arg1...argn]" ) ;
8481 exit ( 1 ) ;
8582 }
8683 }
0 commit comments