11use std:: env;
2+ use std:: path:: PathBuf ;
23use std:: process;
34
45use self :: utils:: is_ci;
@@ -13,11 +14,31 @@ mod rustc_info;
1314mod tests;
1415mod utils;
1516
17+ const USAGE : & str = r#"The build system of cg_clif.
18+
19+ USAGE:
20+ ./y.rs prepare [--out-dir DIR]
21+ ./y.rs build [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features]
22+ ./y.rs test [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features]
23+
24+ OPTIONS:
25+ --sysroot none|clif|llvm
26+ Which sysroot libraries to use:
27+ `none` will not include any standard library in the sysroot.
28+ `clif` will build the standard library using Cranelift.
29+ `llvm` will use the pre-compiled standard library of rustc which is compiled with LLVM.
30+
31+ --out-dir DIR
32+ Specify the directory in which the download, build and dist directories are stored.
33+ By default this is the working directory.
34+
35+ --no-unstable-features
36+ fSome features are not yet ready for production usage. This option will disable these
37+ features. This includes the JIT mode and inline assembly support.
38+ "# ;
39+
1640fn usage ( ) {
17- eprintln ! ( "Usage:" ) ;
18- eprintln ! ( " ./y.rs prepare" ) ;
19- eprintln ! ( " ./y.rs build [--debug] [--sysroot none|clif|llvm] [--no-unstable-features]" ) ;
20- eprintln ! ( " ./y.rs test [--debug] [--sysroot none|clif|llvm] [--no-unstable-features]" ) ;
41+ eprintln ! ( "{USAGE}" ) ;
2142}
2243
2344macro_rules! arg_error {
@@ -30,6 +51,7 @@ macro_rules! arg_error {
3051
3152#[ derive( PartialEq , Debug ) ]
3253enum Command {
54+ Prepare ,
3355 Build ,
3456 Test ,
3557}
@@ -45,39 +67,14 @@ pub fn main() {
4567 env:: set_var ( "CG_CLIF_DISPLAY_CG_TIME" , "1" ) ;
4668 env:: set_var ( "CG_CLIF_DISABLE_INCR_CACHE" , "1" ) ;
4769
48- let current_dir = std:: env:: current_dir ( ) . unwrap ( ) ;
49- let dirs = path:: Dirs {
50- source_dir : current_dir. clone ( ) ,
51- download_dir : current_dir. join ( "download" ) ,
52- build_dir : current_dir. join ( "build" ) ,
53- dist_dir : current_dir. join ( "dist" ) ,
54- } ;
55-
56- path:: RelPath :: BUILD . ensure_exists ( & dirs) ;
57-
58- {
59- // Make sure we always explicitly specify the target dir
60- let target =
61- path:: RelPath :: BUILD . join ( "target_dir_should_be_set_explicitly" ) . to_path ( & dirs) ;
62- env:: set_var ( "CARGO_TARGET_DIR" , & target) ;
63- let _ = std:: fs:: remove_file ( & target) ;
64- std:: fs:: File :: create ( target) . unwrap ( ) ;
65- }
66-
6770 if is_ci ( ) {
6871 // Disabling incr comp reduces cache size and incr comp doesn't save as much on CI anyway
6972 env:: set_var ( "CARGO_BUILD_INCREMENTAL" , "false" ) ;
7073 }
7174
7275 let mut args = env:: args ( ) . skip ( 1 ) ;
7376 let command = match args. next ( ) . as_deref ( ) {
74- Some ( "prepare" ) => {
75- if args. next ( ) . is_some ( ) {
76- arg_error ! ( "./y.rs prepare doesn't expect arguments" ) ;
77- }
78- prepare:: prepare ( & dirs) ;
79- process:: exit ( 0 ) ;
80- }
77+ Some ( "prepare" ) => Command :: Prepare ,
8178 Some ( "build" ) => Command :: Build ,
8279 Some ( "test" ) => Command :: Test ,
8380 Some ( flag) if flag. starts_with ( '-' ) => arg_error ! ( "Expected command found flag {}" , flag) ,
@@ -88,11 +85,17 @@ pub fn main() {
8885 }
8986 } ;
9087
88+ let mut out_dir = PathBuf :: from ( "." ) ;
9189 let mut channel = "release" ;
9290 let mut sysroot_kind = SysrootKind :: Clif ;
9391 let mut use_unstable_features = true ;
9492 while let Some ( arg) = args. next ( ) . as_deref ( ) {
9593 match arg {
94+ "--out-dir" => {
95+ out_dir = PathBuf :: from ( args. next ( ) . unwrap_or_else ( || {
96+ arg_error ! ( "--out-dir requires argument" ) ;
97+ } ) )
98+ }
9699 "--debug" => channel = "debug" ,
97100 "--sysroot" => {
98101 sysroot_kind = match args. next ( ) . as_deref ( ) {
@@ -128,9 +131,38 @@ pub fn main() {
128131 host_triple. clone ( )
129132 } ;
130133
134+ // FIXME allow changing the location of these dirs using cli arguments
135+ let current_dir = std:: env:: current_dir ( ) . unwrap ( ) ;
136+ out_dir = current_dir. join ( out_dir) ;
137+ let dirs = path:: Dirs {
138+ source_dir : current_dir. clone ( ) ,
139+ download_dir : out_dir. join ( "download" ) ,
140+ build_dir : out_dir. join ( "build" ) ,
141+ dist_dir : out_dir. join ( "dist" ) ,
142+ } ;
143+
144+ path:: RelPath :: BUILD . ensure_exists ( & dirs) ;
145+
146+ {
147+ // Make sure we always explicitly specify the target dir
148+ let target =
149+ path:: RelPath :: BUILD . join ( "target_dir_should_be_set_explicitly" ) . to_path ( & dirs) ;
150+ env:: set_var ( "CARGO_TARGET_DIR" , & target) ;
151+ let _ = std:: fs:: remove_file ( & target) ;
152+ std:: fs:: File :: create ( target) . unwrap ( ) ;
153+ }
154+
155+ if command == Command :: Prepare {
156+ prepare:: prepare ( & dirs) ;
157+ process:: exit ( 0 ) ;
158+ }
159+
131160 let cg_clif_dylib =
132161 build_backend:: build_backend ( & dirs, channel, & host_triple, use_unstable_features) ;
133162 match command {
163+ Command :: Prepare => {
164+ // Handled above
165+ }
134166 Command :: Test => {
135167 tests:: run_tests (
136168 & dirs,
0 commit comments