@@ -322,6 +322,7 @@ def __init__(self):
322322 self .date = ''
323323 self ._download_url = ''
324324 self .rustc_channel = ''
325+ self .rustfmt_channel = ''
325326 self .build = ''
326327 self .build_dir = os .path .join (os .getcwd (), "build" )
327328 self .clean = False
@@ -344,6 +345,7 @@ def download_stage0(self):
344345 """
345346 rustc_channel = self .rustc_channel
346347 cargo_channel = self .cargo_channel
348+ rustfmt_channel = self .rustfmt_channel
347349
348350 def support_xz ():
349351 try :
@@ -393,13 +395,29 @@ def support_xz():
393395 with output (self .cargo_stamp ()) as cargo_stamp :
394396 cargo_stamp .write (self .date )
395397
396- def _download_stage0_helper (self , filename , pattern , tarball_suffix ):
398+ if self .rustfmt () and self .rustfmt ().startswith (self .bin_root ()) and (
399+ not os .path .exists (self .rustfmt ())
400+ or self .program_out_of_date (self .rustfmt_stamp ())
401+ ):
402+ if rustfmt_channel :
403+ tarball_suffix = '.tar.xz' if support_xz () else '.tar.gz'
404+ [channel , date ] = rustfmt_channel .split ('-' , 1 )
405+ filename = "rustfmt-{}-{}{}" .format (channel , self .build , tarball_suffix )
406+ self ._download_stage0_helper (filename , "rustfmt-preview" , tarball_suffix , date )
407+ self .fix_executable ("{}/bin/rustfmt" .format (self .bin_root ()))
408+ self .fix_executable ("{}/bin/cargo-fmt" .format (self .bin_root ()))
409+ with output (self .rustfmt_stamp ()) as rustfmt_stamp :
410+ rustfmt_stamp .write (self .date )
411+
412+ def _download_stage0_helper (self , filename , pattern , tarball_suffix , date = None ):
413+ if date is None :
414+ date = self .date
397415 cache_dst = os .path .join (self .build_dir , "cache" )
398- rustc_cache = os .path .join (cache_dst , self . date )
416+ rustc_cache = os .path .join (cache_dst , date )
399417 if not os .path .exists (rustc_cache ):
400418 os .makedirs (rustc_cache )
401419
402- url = "{}/dist/{}" .format (self ._download_url , self . date )
420+ url = "{}/dist/{}" .format (self ._download_url , date )
403421 tarball = os .path .join (rustc_cache , filename )
404422 if not os .path .exists (tarball ):
405423 get ("{}/{}" .format (url , filename ), tarball , verbose = self .verbose )
@@ -493,6 +511,16 @@ def cargo_stamp(self):
493511 """
494512 return os .path .join (self .bin_root (), '.cargo-stamp' )
495513
514+ def rustfmt_stamp (self ):
515+ """Return the path for .rustfmt-stamp
516+
517+ >>> rb = RustBuild()
518+ >>> rb.build_dir = "build"
519+ >>> rb.rustfmt_stamp() == os.path.join("build", "stage0", ".rustfmt-stamp")
520+ True
521+ """
522+ return os .path .join (self .bin_root (), '.rustfmt-stamp' )
523+
496524 def program_out_of_date (self , stamp_path ):
497525 """Check if the given program stamp is out of date"""
498526 if not os .path .exists (stamp_path ) or self .clean :
@@ -565,6 +593,12 @@ def rustc(self):
565593 """Return config path for rustc"""
566594 return self .program_config ('rustc' )
567595
596+ def rustfmt (self ):
597+ """Return config path for rustfmt"""
598+ if not self .rustfmt_channel :
599+ return None
600+ return self .program_config ('rustfmt' )
601+
568602 def program_config (self , program ):
569603 """Return config path for the given program
570604
@@ -868,6 +902,9 @@ def bootstrap(help_triggered):
868902 build .rustc_channel = data ['rustc' ]
869903 build .cargo_channel = data ['cargo' ]
870904
905+ if "rustfmt" in data :
906+ build .rustfmt_channel = data ['rustfmt' ]
907+
871908 if 'dev' in data :
872909 build .set_dev_environment ()
873910 else :
@@ -895,6 +932,8 @@ def bootstrap(help_triggered):
895932 env ["RUSTC_BOOTSTRAP" ] = '1'
896933 env ["CARGO" ] = build .cargo ()
897934 env ["RUSTC" ] = build .rustc ()
935+ if build .rustfmt ():
936+ env ["RUSTFMT" ] = build .rustfmt ()
898937 run (args , env = env , verbose = build .verbose )
899938
900939
0 commit comments