@@ -40,7 +40,8 @@ def get(url, path, verbose=False):
4040 return
4141 else :
4242 if verbose :
43- print ("ignoring already-download file " + path + " due to failed verification" )
43+ print ("ignoring already-download file " +
44+ path + " due to failed verification" )
4445 os .unlink (path )
4546 download (temp_path , url , True , verbose )
4647 if not verify (temp_path , sha_path , verbose ):
@@ -100,8 +101,8 @@ def verify(path, sha_path, verbose):
100101 verified = found == expected
101102 if not verified :
102103 print ("invalid checksum:\n "
103- " found: {}\n "
104- " expected: {}" .format (found , expected ))
104+ " found: {}\n "
105+ " expected: {}" .format (found , expected ))
105106 return verified
106107
107108
@@ -127,20 +128,21 @@ def unpack(tarball, dst, verbose=False, match=None):
127128 shutil .move (tp , fp )
128129 shutil .rmtree (os .path .join (dst , fname ))
129130
130- def run (args , verbose = False , exception = False , cwd = None , env = None ):
131+ def run (args , verbose = False , exception = False , ** kwargs ):
131132 if verbose :
132133 print ("running: " + ' ' .join (args ))
133134 sys .stdout .flush ()
134135 # Use Popen here instead of call() as it apparently allows powershell on
135136 # Windows to not lock up waiting for input presumably.
136- ret = subprocess .Popen (args , cwd = cwd , env = env )
137+ ret = subprocess .Popen (args , ** kwargs )
137138 code = ret .wait ()
138139 if code != 0 :
139140 err = "failed to run: " + ' ' .join (args )
140141 if verbose or exception :
141142 raise RuntimeError (err )
142143 sys .exit (err )
143144
145+
144146def stage0_data (rust_root ):
145147 nightlies = os .path .join (rust_root , "src/stage0.txt" )
146148 data = {}
@@ -153,11 +155,13 @@ def stage0_data(rust_root):
153155 data [a ] = b
154156 return data
155157
158+
156159def format_build_time (duration ):
157160 return str (datetime .timedelta (seconds = int (duration )))
158161
159162
160163class RustBuild (object ):
164+
161165 def download_stage0 (self ):
162166 cache_dst = os .path .join (self .build_dir , "cache" )
163167 rustc_cache = os .path .join (cache_dst , self .stage0_date ())
@@ -172,11 +176,13 @@ def download_stage0(self):
172176 self .print_what_it_means_to_bootstrap ()
173177 if os .path .exists (self .bin_root ()):
174178 shutil .rmtree (self .bin_root ())
175- filename = "rust-std-{}-{}.tar.gz" .format (rustc_channel , self .build )
179+ filename = "rust-std-{}-{}.tar.gz" .format (
180+ rustc_channel , self .build )
176181 url = self ._download_url + "/dist/" + self .stage0_date ()
177182 tarball = os .path .join (rustc_cache , filename )
178183 if not os .path .exists (tarball ):
179- get ("{}/{}" .format (url , filename ), tarball , verbose = self .verbose )
184+ get ("{}/{}" .format (url , filename ),
185+ tarball , verbose = self .verbose )
180186 unpack (tarball , self .bin_root (),
181187 match = "rust-std-" + self .build ,
182188 verbose = self .verbose )
@@ -185,20 +191,25 @@ def download_stage0(self):
185191 url = self ._download_url + "/dist/" + self .stage0_date ()
186192 tarball = os .path .join (rustc_cache , filename )
187193 if not os .path .exists (tarball ):
188- get ("{}/{}" .format (url , filename ), tarball , verbose = self .verbose )
189- unpack (tarball , self .bin_root (), match = "rustc" , verbose = self .verbose )
194+ get ("{}/{}" .format (url , filename ),
195+ tarball , verbose = self .verbose )
196+ unpack (tarball , self .bin_root (),
197+ match = "rustc" , verbose = self .verbose )
190198 self .fix_executable (self .bin_root () + "/bin/rustc" )
191199 self .fix_executable (self .bin_root () + "/bin/rustdoc" )
192200 with open (self .rustc_stamp (), 'w' ) as f :
193201 f .write (self .stage0_date ())
194202
195203 if "pc-windows-gnu" in self .build :
196- filename = "rust-mingw-{}-{}.tar.gz" .format (rustc_channel , self .build )
204+ filename = "rust-mingw-{}-{}.tar.gz" .format (
205+ rustc_channel , self .build )
197206 url = self ._download_url + "/dist/" + self .stage0_date ()
198207 tarball = os .path .join (rustc_cache , filename )
199208 if not os .path .exists (tarball ):
200- get ("{}/{}" .format (url , filename ), tarball , verbose = self .verbose )
201- unpack (tarball , self .bin_root (), match = "rust-mingw" , verbose = self .verbose )
209+ get ("{}/{}" .format (url , filename ),
210+ tarball , verbose = self .verbose )
211+ unpack (tarball , self .bin_root (),
212+ match = "rust-mingw" , verbose = self .verbose )
202213
203214 if self .cargo ().startswith (self .bin_root ()) and \
204215 (not os .path .exists (self .cargo ()) or self .cargo_out_of_date ()):
@@ -207,8 +218,10 @@ def download_stage0(self):
207218 url = self ._download_url + "/dist/" + self .stage0_date ()
208219 tarball = os .path .join (rustc_cache , filename )
209220 if not os .path .exists (tarball ):
210- get ("{}/{}" .format (url , filename ), tarball , verbose = self .verbose )
211- unpack (tarball , self .bin_root (), match = "cargo" , verbose = self .verbose )
221+ get ("{}/{}" .format (url , filename ),
222+ tarball , verbose = self .verbose )
223+ unpack (tarball , self .bin_root (),
224+ match = "cargo" , verbose = self .verbose )
212225 self .fix_executable (self .bin_root () + "/bin/cargo" )
213226 with open (self .cargo_stamp (), 'w' ) as f :
214227 f .write (self .stage0_date ())
@@ -218,7 +231,8 @@ def fix_executable(self, fname):
218231
219232 default_encoding = sys .getdefaultencoding ()
220233 try :
221- ostype = subprocess .check_output (['uname' , '-s' ]).strip ().decode (default_encoding )
234+ ostype = subprocess .check_output (
235+ ['uname' , '-s' ]).strip ().decode (default_encoding )
222236 except (subprocess .CalledProcessError , WindowsError ):
223237 return
224238
@@ -234,7 +248,8 @@ def fix_executable(self, fname):
234248 print ("info: you seem to be running NixOS. Attempting to patch " + fname )
235249
236250 try :
237- interpreter = subprocess .check_output (["patchelf" , "--print-interpreter" , fname ])
251+ interpreter = subprocess .check_output (
252+ ["patchelf" , "--print-interpreter" , fname ])
238253 interpreter = interpreter .strip ().decode (default_encoding )
239254 except subprocess .CalledProcessError as e :
240255 print ("warning: failed to call patchelf: %s" % e )
@@ -243,7 +258,8 @@ def fix_executable(self, fname):
243258 loader = interpreter .split ("/" )[- 1 ]
244259
245260 try :
246- ldd_output = subprocess .check_output (['ldd' , '/run/current-system/sw/bin/sh' ])
261+ ldd_output = subprocess .check_output (
262+ ['ldd' , '/run/current-system/sw/bin/sh' ])
247263 ldd_output = ldd_output .strip ().decode (default_encoding )
248264 except subprocess .CalledProcessError as e :
249265 print ("warning: unable to call ldd: %s" % e )
@@ -261,7 +277,8 @@ def fix_executable(self, fname):
261277 correct_interpreter = loader_path + loader
262278
263279 try :
264- subprocess .check_output (["patchelf" , "--set-interpreter" , correct_interpreter , fname ])
280+ subprocess .check_output (
281+ ["patchelf" , "--set-interpreter" , correct_interpreter , fname ])
265282 except subprocess .CalledProcessError as e :
266283 print ("warning: failed to call patchelf: %s" % e )
267284 return
@@ -371,16 +388,16 @@ def build_bootstrap(self):
371388 env ["CARGO_TARGET_DIR" ] = build_dir
372389 env ["RUSTC" ] = self .rustc ()
373390 env ["LD_LIBRARY_PATH" ] = os .path .join (self .bin_root (), "lib" ) + \
374- (os .pathsep + env ["LD_LIBRARY_PATH" ]) \
375- if "LD_LIBRARY_PATH" in env else ""
391+ (os .pathsep + env ["LD_LIBRARY_PATH" ]) \
392+ if "LD_LIBRARY_PATH" in env else ""
376393 env ["DYLD_LIBRARY_PATH" ] = os .path .join (self .bin_root (), "lib" ) + \
377- (os .pathsep + env ["DYLD_LIBRARY_PATH" ]) \
378- if "DYLD_LIBRARY_PATH" in env else ""
394+ (os .pathsep + env ["DYLD_LIBRARY_PATH" ]) \
395+ if "DYLD_LIBRARY_PATH" in env else ""
379396 env ["LIBRARY_PATH" ] = os .path .join (self .bin_root (), "lib" ) + \
380- (os .pathsep + env ["LIBRARY_PATH" ]) \
381- if "LIBRARY_PATH" in env else ""
397+ (os .pathsep + env ["LIBRARY_PATH" ]) \
398+ if "LIBRARY_PATH" in env else ""
382399 env ["PATH" ] = os .path .join (self .bin_root (), "bin" ) + \
383- os .pathsep + env ["PATH" ]
400+ os .pathsep + env ["PATH" ]
384401 if not os .path .isfile (self .cargo ()):
385402 raise Exception ("no cargo executable found at `%s`" % self .cargo ())
386403 args = [self .cargo (), "build" , "--manifest-path" ,
@@ -395,16 +412,6 @@ def build_bootstrap(self):
395412 args .append ("--frozen" )
396413 run (args , env = env , verbose = self .verbose )
397414
398- def output (self , args , env = None , cwd = None ):
399- default_encoding = sys .getdefaultencoding ()
400- proc = subprocess .Popen (args , stdout = subprocess .PIPE , env = env , cwd = cwd )
401- (out , err ) = proc .communicate ()
402- ret = proc .wait ()
403- if ret != 0 :
404- print (out )
405- sys .exit (ret )
406- return out .decode (default_encoding )
407-
408415 def build_triple (self ):
409416 default_encoding = sys .getdefaultencoding ()
410417 config = self .get_toml ('build' )
@@ -414,8 +421,10 @@ def build_triple(self):
414421 if config :
415422 return config
416423 try :
417- ostype = subprocess .check_output (['uname' , '-s' ]).strip ().decode (default_encoding )
418- cputype = subprocess .check_output (['uname' , '-m' ]).strip ().decode (default_encoding )
424+ ostype = subprocess .check_output (
425+ ['uname' , '-s' ]).strip ().decode (default_encoding )
426+ cputype = subprocess .check_output (
427+ ['uname' , '-m' ]).strip ().decode (default_encoding )
419428 except (subprocess .CalledProcessError , OSError ):
420429 if sys .platform == 'win32' :
421430 return 'x86_64-pc-windows-msvc'
@@ -427,7 +436,8 @@ def build_triple(self):
427436 # The goal here is to come up with the same triple as LLVM would,
428437 # at least for the subset of platforms we're willing to target.
429438 if ostype == 'Linux' :
430- os_from_sp = subprocess .check_output (['uname' , '-o' ]).strip ().decode (default_encoding )
439+ os_from_sp = subprocess .check_output (
440+ ['uname' , '-o' ]).strip ().decode (default_encoding )
431441 if os_from_sp == 'Android' :
432442 ostype = 'linux-android'
433443 else :
@@ -451,7 +461,7 @@ def build_triple(self):
451461 # must be used instead.
452462 try :
453463 cputype = subprocess .check_output (['isainfo' ,
454- '-k' ]).strip ().decode (default_encoding )
464+ '-k' ]).strip ().decode (default_encoding )
455465 except (subprocess .CalledProcessError , OSError ):
456466 err = "isainfo not found"
457467 if self .verbose :
@@ -544,51 +554,29 @@ def build_triple(self):
544554
545555 def update_submodules (self ):
546556 if (not os .path .exists (os .path .join (self .rust_root , ".git" ))) or \
547- self .get_toml ('submodules' ) == "false" or \
548- self .get_mk ('CFG_DISABLE_MANAGE_SUBMODULES' ) == "1" :
557+ self .get_toml ('submodules' ) == "false" or \
558+ self .get_mk ('CFG_DISABLE_MANAGE_SUBMODULES' ) == "1" :
549559 return
550-
551560 print ('Updating submodules' )
552- output = self .output (["git" , "submodule" , "status" ], cwd = self .rust_root )
553- submodules = []
554- for line in output .splitlines ():
555- # NOTE `git submodule status` output looks like this:
556- #
557- # -5066b7dcab7e700844b0e2ba71b8af9dc627a59b src/liblibc
558- # +b37ef24aa82d2be3a3cc0fe89bf82292f4ca181c src/compiler-rt (remotes/origin/..)
559- # e058ca661692a8d01f8cf9d35939dfe3105ce968 src/jemalloc (3.6.0-533-ge058ca6)
560- #
561- # The first character can be '-', '+' or ' ' and denotes the
562- # `State` of the submodule Right next to this character is the
563- # SHA-1 of the submodule HEAD And after that comes the path to the
564- # submodule
565- path = line [1 :].split (' ' )[1 ]
566- submodules .append ([path , line [0 ]])
567-
568- run (["git" , "submodule" , "sync" ], cwd = self .rust_root )
569-
570- for submod in submodules :
571- path , status = submod
572- if path .endswith ('llvm' ) and \
573- (self .get_toml ('llvm-config' ) or self .get_mk ('CFG_LLVM_ROOT' )):
574- continue
575- if path .endswith ('jemalloc' ) and \
576- (self .get_toml ('jemalloc' ) or self .get_mk ('CFG_JEMALLOC_ROOT' )):
577- continue
578- submod_path = os .path .join (self .rust_root , path )
579-
580- if status == ' ' :
581- run (["git" , "reset" , "--hard" ], cwd = submod_path )
582- run (["git" , "clean" , "-fdx" ], cwd = submod_path )
583- elif status == '+' :
584- run (["git" , "submodule" , "update" , path ], cwd = self .rust_root )
585- run (["git" , "reset" , "--hard" ], cwd = submod_path )
586- run (["git" , "clean" , "-fdx" ], cwd = submod_path )
587- elif status == '-' :
588- run (["git" , "submodule" , "init" , path ], cwd = self .rust_root )
589- run (["git" , "submodule" , "update" , path ], cwd = self .rust_root )
590- else :
591- raise ValueError ('unknown submodule status: ' + status )
561+ default_encoding = sys .getdefaultencoding ()
562+ run (["git" , "submodule" , "-q" , "sync" ], cwd = self .rust_root )
563+ submodules = [s .split (' ' , 1 )[1 ] for s in subprocess .check_output (
564+ ["git" , "config" , "--file" , os .path .join (self .rust_root , ".gitmodules" ),
565+ "--get-regexp" , "path" ]
566+ ).decode (default_encoding ).splitlines ()]
567+ submodules = [module for module in submodules
568+ if not ((module .endswith ("llvm" ) and
569+ (self .get_toml ('llvm-config' ) or self .get_mk ('CFG_LLVM_ROOT' ))) or
570+ (module .endswith ("jemalloc" ) and
571+ (self .get_toml ('jemalloc' ) or self .get_mk ('CFG_JEMALLOC_ROOT' ))))
572+ ]
573+ run (["git" , "submodule" , "update" ,
574+ "--init" ] + submodules , cwd = self .rust_root , verbose = self .verbose )
575+ run (["git" , "submodule" , "-q" , "foreach" , "git" ,
576+ "reset" , "-q" , "--hard" ], cwd = self .rust_root , verbose = self .verbose )
577+ run (["git" , "submodule" , "-q" , "foreach" , "git" ,
578+ "clean" , "-qdfx" ], cwd = self .rust_root , verbose = self .verbose )
579+
592580
593581def bootstrap ():
594582 parser = argparse .ArgumentParser (description = 'Build rust' )
@@ -641,7 +629,7 @@ def bootstrap():
641629 if rb .use_vendored_sources :
642630 if not os .path .exists ('.cargo' ):
643631 os .makedirs ('.cargo' )
644- with open ('.cargo/config' ,'w' ) as f :
632+ with open ('.cargo/config' , 'w' ) as f :
645633 f .write ("""
646634 [source.crates-io]
647635 replace-with = 'vendored-sources'
@@ -681,21 +669,25 @@ def bootstrap():
681669 env ["BOOTSTRAP_PARENT_ID" ] = str (os .getpid ())
682670 run (args , env = env , verbose = rb .verbose )
683671
672+
684673def main ():
685674 start_time = time ()
686- help_triggered = ('-h' in sys .argv ) or ('--help' in sys .argv ) or (len (sys .argv ) == 1 )
675+ help_triggered = (
676+ '-h' in sys .argv ) or ('--help' in sys .argv ) or (len (sys .argv ) == 1 )
687677 try :
688678 bootstrap ()
689679 if not help_triggered :
690- print ("Build completed successfully in %s" % format_build_time (time () - start_time ))
680+ print ("Build completed successfully in %s" %
681+ format_build_time (time () - start_time ))
691682 except (SystemExit , KeyboardInterrupt ) as e :
692683 if hasattr (e , 'code' ) and isinstance (e .code , int ):
693684 exit_code = e .code
694685 else :
695686 exit_code = 1
696687 print (e )
697688 if not help_triggered :
698- print ("Build completed unsuccessfully in %s" % format_build_time (time () - start_time ))
689+ print ("Build completed unsuccessfully in %s" %
690+ format_build_time (time () - start_time ))
699691 sys .exit (exit_code )
700692
701693if __name__ == '__main__' :
0 commit comments