1+ use crate :: builder:: { Builder , RunConfig , ShouldRun , Step } ;
12use crate :: { t, VERSION } ;
23use crate :: { Config , TargetSelection } ;
34use std:: env:: consts:: EXE_SUFFIX ;
@@ -11,7 +12,7 @@ use std::{
1112 io:: { self , Write } ,
1213} ;
1314
14- #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
15+ #[ derive( Clone , Copy , Debug , Eq , PartialEq , Hash ) ]
1516pub enum Profile {
1617 Compiler ,
1718 Codegen ,
@@ -50,6 +51,16 @@ impl Profile {
5051 }
5152 out
5253 }
54+
55+ pub fn as_str ( & self ) -> & ' static str {
56+ match self {
57+ Profile :: Compiler => "compiler" ,
58+ Profile :: Codegen => "codegen" ,
59+ Profile :: Library => "library" ,
60+ Profile :: Tools => "tools" ,
61+ Profile :: User => "user" ,
62+ }
63+ }
5364}
5465
5566impl FromStr for Profile {
@@ -71,13 +82,43 @@ impl FromStr for Profile {
7182
7283impl fmt:: Display for Profile {
7384 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
74- match self {
75- Profile :: Compiler => write ! ( f, "compiler" ) ,
76- Profile :: Codegen => write ! ( f, "codegen" ) ,
77- Profile :: Library => write ! ( f, "library" ) ,
78- Profile :: User => write ! ( f, "user" ) ,
79- Profile :: Tools => write ! ( f, "tools" ) ,
85+ f. write_str ( self . as_str ( ) )
86+ }
87+ }
88+
89+ impl Step for Profile {
90+ type Output = ( ) ;
91+ const DEFAULT : bool = true ;
92+
93+ fn should_run ( mut run : ShouldRun < ' _ > ) -> ShouldRun < ' _ > {
94+ for choice in Profile :: all ( ) {
95+ run = run. alias ( choice. as_str ( ) ) ;
8096 }
97+ run
98+ }
99+
100+ fn make_run ( run : RunConfig < ' _ > ) {
101+ // for Profile, `run.paths` will have 1 and only 1 element
102+ // this is because we only accept at most 1 path from user input.
103+ // If user calls `x.py setup` without arguments, the interacctive TUI
104+ // will guide user to provide one.
105+ let profile: Profile = run
106+ . paths
107+ . first ( )
108+ . unwrap ( )
109+ . assert_single_path ( )
110+ . path
111+ . to_owned ( )
112+ . into_os_string ( )
113+ . into_string ( )
114+ . unwrap ( )
115+ . parse ( )
116+ . unwrap ( ) ;
117+ run. builder . ensure ( profile) ;
118+ }
119+
120+ fn run ( self , builder : & Builder < ' _ > ) {
121+ setup ( & builder. build . config , self )
81122 }
82123}
83124
@@ -103,6 +144,7 @@ pub fn setup(config: &Config, profile: Profile) {
103144 changelog-seen = {}\n ",
104145 profile, VERSION
105146 ) ;
147+
106148 t ! ( fs:: write( path, settings) ) ;
107149
108150 let include_path = profile. include_path ( & config. src ) ;
@@ -116,7 +158,7 @@ pub fn setup(config: &Config, profile: Profile) {
116158
117159 if !rustup_installed ( ) && profile != Profile :: User {
118160 eprintln ! ( "`rustup` is not installed; cannot link `stage1` toolchain" ) ;
119- } else if stage_dir_exists ( & stage_path[ ..] ) {
161+ } else if stage_dir_exists ( & stage_path[ ..] ) && !config . dry_run {
120162 attempt_toolchain_link ( & stage_path[ ..] ) ;
121163 }
122164
@@ -136,7 +178,9 @@ pub fn setup(config: &Config, profile: Profile) {
136178
137179 println ! ( ) ;
138180
139- t ! ( install_git_hook_maybe( & config) ) ;
181+ if !config. dry_run {
182+ t ! ( install_git_hook_maybe( & config) ) ;
183+ }
140184
141185 println ! ( ) ;
142186
0 commit comments