Skip to content

Commit f43960f

Browse files
authored
Rollup merge of rust-lang#42081 - ishitatsuyuki:submodule-better, r=aidanhs
Use the improved submodule handling r? @alexcrichton That was a crap... ``` Updating submodules Traceback (most recent call last): File "./x.py", line 20, in <module> bootstrap.main() File "/home/ishitatsuyuki/Documents/rust/src/bootstrap/bootstrap.py", line 684, in main bootstrap() File "/home/ishitatsuyuki/Documents/rust/src/bootstrap/bootstrap.py", line 662, in bootstrap rb.update_submodules() File "/home/ishitatsuyuki/Documents/rust/src/bootstrap/bootstrap.py", line 566, in update_submodules path = line[1:].split(' ')[1] TypeError: a bytes-like object is required, not 'str' ``` Maybe we need to confirm the compatibility of git options, such as `git config` or `git -C` (I believe they existed long before, though). This is tested locally.
2 parents b2310e1 + d34aaa1 commit f43960f

File tree

1 file changed

+83
-90
lines changed

1 file changed

+83
-90
lines changed

src/bootstrap/bootstrap.py

+83-90
Original file line numberDiff line numberDiff line change
@@ -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,22 @@ 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):
131+
132+
def run(args, verbose=False, exception=False):
131133
if verbose:
132134
print("running: " + ' '.join(args))
133135
sys.stdout.flush()
134136
# Use Popen here instead of call() as it apparently allows powershell on
135137
# Windows to not lock up waiting for input presumably.
136-
ret = subprocess.Popen(args, cwd=cwd)
138+
ret = subprocess.Popen(args)
137139
code = ret.wait()
138140
if code != 0:
139141
err = "failed to run: " + ' '.join(args)
140142
if verbose or exception:
141143
raise RuntimeError(err)
142144
sys.exit(err)
143145

146+
144147
def stage0_data(rust_root):
145148
nightlies = os.path.join(rust_root, "src/stage0.txt")
146149
data = {}
@@ -153,11 +156,13 @@ def stage0_data(rust_root):
153156
data[a] = b
154157
return data
155158

159+
156160
def format_build_time(duration):
157161
return str(datetime.timedelta(seconds=int(duration)))
158162

159163

160164
class RustBuild(object):
165+
161166
def download_stage0(self):
162167
cache_dst = os.path.join(self.build_dir, "cache")
163168
rustc_cache = os.path.join(cache_dst, self.stage0_date())
@@ -172,11 +177,13 @@ def download_stage0(self):
172177
self.print_what_it_means_to_bootstrap()
173178
if os.path.exists(self.bin_root()):
174179
shutil.rmtree(self.bin_root())
175-
filename = "rust-std-{}-{}.tar.gz".format(rustc_channel, self.build)
180+
filename = "rust-std-{}-{}.tar.gz".format(
181+
rustc_channel, self.build)
176182
url = self._download_url + "/dist/" + self.stage0_date()
177183
tarball = os.path.join(rustc_cache, filename)
178184
if not os.path.exists(tarball):
179-
get("{}/{}".format(url, filename), tarball, verbose=self.verbose)
185+
get("{}/{}".format(url, filename),
186+
tarball, verbose=self.verbose)
180187
unpack(tarball, self.bin_root(),
181188
match="rust-std-" + self.build,
182189
verbose=self.verbose)
@@ -185,20 +192,25 @@ def download_stage0(self):
185192
url = self._download_url + "/dist/" + self.stage0_date()
186193
tarball = os.path.join(rustc_cache, filename)
187194
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)
195+
get("{}/{}".format(url, filename),
196+
tarball, verbose=self.verbose)
197+
unpack(tarball, self.bin_root(),
198+
match="rustc", verbose=self.verbose)
190199
self.fix_executable(self.bin_root() + "/bin/rustc")
191200
self.fix_executable(self.bin_root() + "/bin/rustdoc")
192201
with open(self.rustc_stamp(), 'w') as f:
193202
f.write(self.stage0_date())
194203

195204
if "pc-windows-gnu" in self.build:
196-
filename = "rust-mingw-{}-{}.tar.gz".format(rustc_channel, self.build)
205+
filename = "rust-mingw-{}-{}.tar.gz".format(
206+
rustc_channel, self.build)
197207
url = self._download_url + "/dist/" + self.stage0_date()
198208
tarball = os.path.join(rustc_cache, filename)
199209
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)
210+
get("{}/{}".format(url, filename),
211+
tarball, verbose=self.verbose)
212+
unpack(tarball, self.bin_root(),
213+
match="rust-mingw", verbose=self.verbose)
202214

203215
if self.cargo().startswith(self.bin_root()) and \
204216
(not os.path.exists(self.cargo()) or self.cargo_out_of_date()):
@@ -207,8 +219,10 @@ def download_stage0(self):
207219
url = self._download_url + "/dist/" + self.stage0_date()
208220
tarball = os.path.join(rustc_cache, filename)
209221
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)
222+
get("{}/{}".format(url, filename),
223+
tarball, verbose=self.verbose)
224+
unpack(tarball, self.bin_root(),
225+
match="cargo", verbose=self.verbose)
212226
self.fix_executable(self.bin_root() + "/bin/cargo")
213227
with open(self.cargo_stamp(), 'w') as f:
214228
f.write(self.stage0_date())
@@ -218,7 +232,8 @@ def fix_executable(self, fname):
218232

219233
default_encoding = sys.getdefaultencoding()
220234
try:
221-
ostype = subprocess.check_output(['uname', '-s']).strip().decode(default_encoding)
235+
ostype = subprocess.check_output(
236+
['uname', '-s']).strip().decode(default_encoding)
222237
except (subprocess.CalledProcessError, WindowsError):
223238
return
224239

@@ -234,7 +249,8 @@ def fix_executable(self, fname):
234249
print("info: you seem to be running NixOS. Attempting to patch " + fname)
235250

236251
try:
237-
interpreter = subprocess.check_output(["patchelf", "--print-interpreter", fname])
252+
interpreter = subprocess.check_output(
253+
["patchelf", "--print-interpreter", fname])
238254
interpreter = interpreter.strip().decode(default_encoding)
239255
except subprocess.CalledProcessError as e:
240256
print("warning: failed to call patchelf: %s" % e)
@@ -243,7 +259,8 @@ def fix_executable(self, fname):
243259
loader = interpreter.split("/")[-1]
244260

245261
try:
246-
ldd_output = subprocess.check_output(['ldd', '/run/current-system/sw/bin/sh'])
262+
ldd_output = subprocess.check_output(
263+
['ldd', '/run/current-system/sw/bin/sh'])
247264
ldd_output = ldd_output.strip().decode(default_encoding)
248265
except subprocess.CalledProcessError as e:
249266
print("warning: unable to call ldd: %s" % e)
@@ -261,7 +278,8 @@ def fix_executable(self, fname):
261278
correct_interpreter = loader_path + loader
262279

263280
try:
264-
subprocess.check_output(["patchelf", "--set-interpreter", correct_interpreter, fname])
281+
subprocess.check_output(
282+
["patchelf", "--set-interpreter", correct_interpreter, fname])
265283
except subprocess.CalledProcessError as e:
266284
print("warning: failed to call patchelf: %s" % e)
267285
return
@@ -371,16 +389,16 @@ def build_bootstrap(self):
371389
env["CARGO_TARGET_DIR"] = build_dir
372390
env["RUSTC"] = self.rustc()
373391
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 ""
392+
(os.pathsep + env["LD_LIBRARY_PATH"]) \
393+
if "LD_LIBRARY_PATH" in env else ""
376394
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 ""
395+
(os.pathsep + env["DYLD_LIBRARY_PATH"]) \
396+
if "DYLD_LIBRARY_PATH" in env else ""
379397
env["LIBRARY_PATH"] = os.path.join(self.bin_root(), "lib") + \
380-
(os.pathsep + env["LIBRARY_PATH"]) \
381-
if "LIBRARY_PATH" in env else ""
398+
(os.pathsep + env["LIBRARY_PATH"]) \
399+
if "LIBRARY_PATH" in env else ""
382400
env["PATH"] = os.path.join(self.bin_root(), "bin") + \
383-
os.pathsep + env["PATH"]
401+
os.pathsep + env["PATH"]
384402
if not os.path.isfile(self.cargo()):
385403
raise Exception("no cargo executable found at `%s`" % self.cargo())
386404
args = [self.cargo(), "build", "--manifest-path",
@@ -389,23 +407,13 @@ def build_bootstrap(self):
389407
args.append("--locked")
390408
if self.use_vendored_sources:
391409
args.append("--frozen")
392-
self.run(args, env)
393-
394-
def run(self, args, env=None, cwd=None):
395-
proc = subprocess.Popen(args, env=env, cwd=cwd)
396-
ret = proc.wait()
397-
if ret != 0:
398-
sys.exit(ret)
410+
self.run(args, env=env)
399411

400-
def output(self, args, env=None, cwd=None):
401-
default_encoding = sys.getdefaultencoding()
402-
proc = subprocess.Popen(args, stdout=subprocess.PIPE, env=env, cwd=cwd)
403-
(out, err) = proc.communicate()
412+
def run(self, args, **kwargs):
413+
proc = subprocess.Popen(args, **kwargs)
404414
ret = proc.wait()
405415
if ret != 0:
406-
print(out)
407416
sys.exit(ret)
408-
return out.decode(default_encoding)
409417

410418
def build_triple(self):
411419
default_encoding = sys.getdefaultencoding()
@@ -416,8 +424,10 @@ def build_triple(self):
416424
if config:
417425
return config
418426
try:
419-
ostype = subprocess.check_output(['uname', '-s']).strip().decode(default_encoding)
420-
cputype = subprocess.check_output(['uname', '-m']).strip().decode(default_encoding)
427+
ostype = subprocess.check_output(
428+
['uname', '-s']).strip().decode(default_encoding)
429+
cputype = subprocess.check_output(
430+
['uname', '-m']).strip().decode(default_encoding)
421431
except (subprocess.CalledProcessError, OSError):
422432
if sys.platform == 'win32':
423433
return 'x86_64-pc-windows-msvc'
@@ -429,7 +439,8 @@ def build_triple(self):
429439
# The goal here is to come up with the same triple as LLVM would,
430440
# at least for the subset of platforms we're willing to target.
431441
if ostype == 'Linux':
432-
os_from_sp = subprocess.check_output(['uname', '-o']).strip().decode(default_encoding)
442+
os_from_sp = subprocess.check_output(
443+
['uname', '-o']).strip().decode(default_encoding)
433444
if os_from_sp == 'Android':
434445
ostype = 'linux-android'
435446
else:
@@ -453,7 +464,7 @@ def build_triple(self):
453464
# must be used instead.
454465
try:
455466
cputype = subprocess.check_output(['isainfo',
456-
'-k']).strip().decode(default_encoding)
467+
'-k']).strip().decode(default_encoding)
457468
except (subprocess.CalledProcessError, OSError):
458469
err = "isainfo not found"
459470
if self.verbose:
@@ -546,51 +557,29 @@ def build_triple(self):
546557

547558
def update_submodules(self):
548559
if (not os.path.exists(os.path.join(self.rust_root, ".git"))) or \
549-
self.get_toml('submodules') == "false" or \
550-
self.get_mk('CFG_DISABLE_MANAGE_SUBMODULES') == "1":
560+
self.get_toml('submodules') == "false" or \
561+
self.get_mk('CFG_DISABLE_MANAGE_SUBMODULES') == "1":
551562
return
552-
553563
print('Updating submodules')
554-
output = self.output(["git", "submodule", "status"], cwd=self.rust_root)
555-
submodules = []
556-
for line in output.splitlines():
557-
# NOTE `git submodule status` output looks like this:
558-
#
559-
# -5066b7dcab7e700844b0e2ba71b8af9dc627a59b src/liblibc
560-
# +b37ef24aa82d2be3a3cc0fe89bf82292f4ca181c src/compiler-rt (remotes/origin/..)
561-
# e058ca661692a8d01f8cf9d35939dfe3105ce968 src/jemalloc (3.6.0-533-ge058ca6)
562-
#
563-
# The first character can be '-', '+' or ' ' and denotes the
564-
# `State` of the submodule Right next to this character is the
565-
# SHA-1 of the submodule HEAD And after that comes the path to the
566-
# submodule
567-
path = line[1:].split(' ')[1]
568-
submodules.append([path, line[0]])
569-
570-
self.run(["git", "submodule", "sync"], cwd=self.rust_root)
571-
572-
for submod in submodules:
573-
path, status = submod
574-
if path.endswith('llvm') and \
575-
(self.get_toml('llvm-config') or self.get_mk('CFG_LLVM_ROOT')):
576-
continue
577-
if path.endswith('jemalloc') and \
578-
(self.get_toml('jemalloc') or self.get_mk('CFG_JEMALLOC_ROOT')):
579-
continue
580-
submod_path = os.path.join(self.rust_root, path)
581-
582-
if status == ' ':
583-
self.run(["git", "reset", "--hard"], cwd=submod_path)
584-
self.run(["git", "clean", "-fdx"], cwd=submod_path)
585-
elif status == '+':
586-
self.run(["git", "submodule", "update", path], cwd=self.rust_root)
587-
self.run(["git", "reset", "--hard"], cwd=submod_path)
588-
self.run(["git", "clean", "-fdx"], cwd=submod_path)
589-
elif status == '-':
590-
self.run(["git", "submodule", "init", path], cwd=self.rust_root)
591-
self.run(["git", "submodule", "update", path], cwd=self.rust_root)
592-
else:
593-
raise ValueError('unknown submodule status: ' + status)
564+
default_encoding = sys.getdefaultencoding()
565+
self.run(["git", "submodule", "-q", "sync"], cwd=self.rust_root)
566+
submodules = [s.split(' ', 1)[1] for s in subprocess.check_output(
567+
["git", "config", "--file", os.path.join(self.rust_root, ".gitmodules"),
568+
"--get-regexp", "path"]
569+
).decode(default_encoding).splitlines()]
570+
submodules = [module for module in submodules
571+
if not ((module.endswith("llvm") and
572+
(self.get_toml('llvm-config') or self.get_mk('CFG_LLVM_ROOT'))) or
573+
(module.endswith("jemalloc") and
574+
(self.get_toml('jemalloc') or self.get_mk('CFG_JEMALLOC_ROOT'))))
575+
]
576+
self.run(["git", "submodule", "update",
577+
"--init"] + submodules, cwd=self.rust_root)
578+
self.run(["git", "submodule", "-q", "foreach", "git",
579+
"reset", "-q", "--hard"], cwd=self.rust_root)
580+
self.run(["git", "submodule", "-q", "foreach", "git",
581+
"clean", "-qdfx"], cwd=self.rust_root)
582+
594583

595584
def bootstrap():
596585
parser = argparse.ArgumentParser(description='Build rust')
@@ -638,7 +627,7 @@ def bootstrap():
638627
if rb.use_vendored_sources:
639628
if not os.path.exists('.cargo'):
640629
os.makedirs('.cargo')
641-
with open('.cargo/config','w') as f:
630+
with open('.cargo/config', 'w') as f:
642631
f.write("""
643632
[source.crates-io]
644633
replace-with = 'vendored-sources'
@@ -676,23 +665,27 @@ def bootstrap():
676665
env["BUILD"] = rb.build
677666
env["SRC"] = rb.rust_root
678667
env["BOOTSTRAP_PARENT_ID"] = str(os.getpid())
679-
rb.run(args, env)
668+
rb.run(args, env=env)
669+
680670

681671
def main():
682672
start_time = time()
683-
help_triggered = ('-h' in sys.argv) or ('--help' in sys.argv) or (len(sys.argv) == 1)
673+
help_triggered = (
674+
'-h' in sys.argv) or ('--help' in sys.argv) or (len(sys.argv) == 1)
684675
try:
685676
bootstrap()
686677
if not help_triggered:
687-
print("Build completed successfully in %s" % format_build_time(time() - start_time))
678+
print("Build completed successfully in %s" %
679+
format_build_time(time() - start_time))
688680
except (SystemExit, KeyboardInterrupt) as e:
689681
if hasattr(e, 'code') and isinstance(e.code, int):
690682
exit_code = e.code
691683
else:
692684
exit_code = 1
693685
print(e)
694686
if not help_triggered:
695-
print("Build completed unsuccessfully in %s" % format_build_time(time() - start_time))
687+
print("Build completed unsuccessfully in %s" %
688+
format_build_time(time() - start_time))
696689
sys.exit(exit_code)
697690

698691
if __name__ == '__main__':

0 commit comments

Comments
 (0)