Skip to content

Commit 53bf24c

Browse files
committed
Move download-rustc from bootstrap.py to rustbuild
- Remove download-rustc handling from bootstrap.py - Allow a custom `pattern` in `builder.unpack()` - Only download rustc once another part of bootstrap depends on it. This is somewhat necessary since the download functions rely on having a full `Builder`, which isn't available until after config parsing finishes.
1 parent 61dd278 commit 53bf24c

File tree

7 files changed

+200
-86
lines changed

7 files changed

+200
-86
lines changed

src/bootstrap/bootstrap.py

+38-65
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def __init__(self):
446446
self.nix_deps_dir = None
447447
self.rustc_commit = None
448448

449-
def download_toolchain(self, stage0=True, rustc_channel=None):
449+
def download_toolchain(self, rustc_channel=None):
450450
"""Fetch the build system for Rust, written in Rust
451451
452452
This method will build a cache directory, then it will fetch the
@@ -458,43 +458,34 @@ def download_toolchain(self, stage0=True, rustc_channel=None):
458458
"""
459459
if rustc_channel is None:
460460
rustc_channel = self.stage0_compiler.version
461-
bin_root = self.bin_root(stage0)
461+
bin_root = self.bin_root()
462462

463463
key = self.stage0_compiler.date
464-
if not stage0:
465-
key += str(self.rustc_commit)
466-
if self.rustc(stage0).startswith(bin_root) and \
467-
(not os.path.exists(self.rustc(stage0)) or
468-
self.program_out_of_date(self.rustc_stamp(stage0), key)):
464+
if self.rustc().startswith(bin_root) and \
465+
(not os.path.exists(self.rustc()) or
466+
self.program_out_of_date(self.rustc_stamp(), key)):
469467
if os.path.exists(bin_root):
470468
shutil.rmtree(bin_root)
471469
tarball_suffix = '.tar.xz' if support_xz() else '.tar.gz'
472470
filename = "rust-std-{}-{}{}".format(
473471
rustc_channel, self.build, tarball_suffix)
474472
pattern = "rust-std-{}".format(self.build)
475-
self._download_component_helper(filename, pattern, tarball_suffix, stage0)
473+
self._download_component_helper(filename, pattern, tarball_suffix)
476474
filename = "rustc-{}-{}{}".format(rustc_channel, self.build,
477475
tarball_suffix)
478-
self._download_component_helper(filename, "rustc", tarball_suffix, stage0)
479-
# download-rustc doesn't need its own cargo, it can just use beta's.
480-
if stage0:
481-
filename = "cargo-{}-{}{}".format(rustc_channel, self.build,
482-
tarball_suffix)
483-
self._download_component_helper(filename, "cargo", tarball_suffix)
484-
self.fix_bin_or_dylib("{}/bin/cargo".format(bin_root))
485-
else:
486-
filename = "rustc-dev-{}-{}{}".format(rustc_channel, self.build, tarball_suffix)
487-
self._download_component_helper(
488-
filename, "rustc-dev", tarball_suffix, stage0
489-
)
476+
self._download_component_helper(filename, "rustc", tarball_suffix)
477+
filename = "cargo-{}-{}{}".format(rustc_channel, self.build,
478+
tarball_suffix)
479+
self._download_component_helper(filename, "cargo", tarball_suffix)
480+
self.fix_bin_or_dylib("{}/bin/cargo".format(bin_root))
490481

491482
self.fix_bin_or_dylib("{}/bin/rustc".format(bin_root))
492483
self.fix_bin_or_dylib("{}/bin/rustdoc".format(bin_root))
493484
lib_dir = "{}/lib".format(bin_root)
494485
for lib in os.listdir(lib_dir):
495486
if lib.endswith(".so"):
496487
self.fix_bin_or_dylib(os.path.join(lib_dir, lib))
497-
with output(self.rustc_stamp(stage0)) as rust_stamp:
488+
with output(self.rustc_stamp()) as rust_stamp:
498489
rust_stamp.write(key)
499490

500491
if self.rustfmt() and self.rustfmt().startswith(bin_root) and (
@@ -518,24 +509,17 @@ def download_toolchain(self, stage0=True, rustc_channel=None):
518509
rustfmt_stamp.write(self.stage0_rustfmt.channel())
519510

520511
def _download_component_helper(
521-
self, filename, pattern, tarball_suffix, stage0=True, key=None
512+
self, filename, pattern, tarball_suffix, key=None
522513
):
523514
if key is None:
524-
if stage0:
525-
key = self.stage0_compiler.date
526-
else:
527-
key = self.rustc_commit
515+
key = self.stage0_compiler.date
528516
cache_dst = os.path.join(self.build_dir, "cache")
529517
rustc_cache = os.path.join(cache_dst, key)
530518
if not os.path.exists(rustc_cache):
531519
os.makedirs(rustc_cache)
532520

533-
if stage0:
534-
base = self._download_url
535-
url = "dist/{}".format(key)
536-
else:
537-
base = "https://ci-artifacts.rust-lang.org"
538-
url = "rustc-builds/{}".format(self.rustc_commit)
521+
base = self._download_url
522+
url = "dist/{}".format(key)
539523
tarball = os.path.join(rustc_cache, filename)
540524
if not os.path.exists(tarball):
541525
get(
@@ -544,9 +528,9 @@ def _download_component_helper(
544528
tarball,
545529
self.checksums_sha256,
546530
verbose=self.verbose,
547-
do_verify=stage0,
531+
do_verify=True,
548532
)
549-
unpack(tarball, tarball_suffix, self.bin_root(stage0), match=pattern, verbose=self.verbose)
533+
unpack(tarball, tarball_suffix, self.bin_root(), match=pattern, verbose=self.verbose)
550534

551535
def fix_bin_or_dylib(self, fname):
552536
"""Modifies the interpreter section of 'fname' to fix the dynamic linker,
@@ -689,17 +673,15 @@ def maybe_download_ci_toolchain(self):
689673
# FIXME: support downloading artifacts from the beta channel
690674
self.download_toolchain(False, "nightly")
691675

692-
def rustc_stamp(self, stage0):
676+
def rustc_stamp(self):
693677
"""Return the path for .rustc-stamp at the given stage
694678
695679
>>> rb = RustBuild()
696680
>>> rb.build_dir = "build"
697-
>>> rb.rustc_stamp(True) == os.path.join("build", "stage0", ".rustc-stamp")
698-
True
699-
>>> rb.rustc_stamp(False) == os.path.join("build", "ci-rustc", ".rustc-stamp")
681+
>>> rb.rustc_stamp() == os.path.join("build", "stage0", ".rustc-stamp")
700682
True
701683
"""
702-
return os.path.join(self.bin_root(stage0), '.rustc-stamp')
684+
return os.path.join(self.bin_root(), '.rustc-stamp')
703685

704686
def rustfmt_stamp(self):
705687
"""Return the path for .rustfmt-stamp
@@ -709,7 +691,7 @@ def rustfmt_stamp(self):
709691
>>> rb.rustfmt_stamp() == os.path.join("build", "stage0", ".rustfmt-stamp")
710692
True
711693
"""
712-
return os.path.join(self.bin_root(True), '.rustfmt-stamp')
694+
return os.path.join(self.bin_root(), '.rustfmt-stamp')
713695

714696
def program_out_of_date(self, stamp_path, key):
715697
"""Check if the given program stamp is out of date"""
@@ -718,26 +700,21 @@ def program_out_of_date(self, stamp_path, key):
718700
with open(stamp_path, 'r') as stamp:
719701
return key != stamp.read()
720702

721-
def bin_root(self, stage0):
703+
def bin_root(self):
722704
"""Return the binary root directory for the given stage
723705
724706
>>> rb = RustBuild()
725707
>>> rb.build_dir = "build"
726-
>>> rb.bin_root(True) == os.path.join("build", "stage0")
727-
True
728-
>>> rb.bin_root(False) == os.path.join("build", "ci-rustc")
708+
>>> rb.bin_root() == os.path.join("build", "stage0")
729709
True
730710
731711
When the 'build' property is given should be a nested directory:
732712
733713
>>> rb.build = "devel"
734-
>>> rb.bin_root(True) == os.path.join("build", "devel", "stage0")
714+
>>> rb.bin_root() == os.path.join("build", "devel", "stage0")
735715
True
736716
"""
737-
if stage0:
738-
subdir = "stage0"
739-
else:
740-
subdir = "ci-rustc"
717+
subdir = "stage0"
741718
return os.path.join(self.build_dir, self.build, subdir)
742719

743720
def get_toml(self, key, section=None):
@@ -785,37 +762,33 @@ def cargo(self):
785762
"""Return config path for cargo"""
786763
return self.program_config('cargo')
787764

788-
def rustc(self, stage0):
765+
def rustc(self):
789766
"""Return config path for rustc"""
790-
return self.program_config('rustc', stage0)
767+
return self.program_config('rustc')
791768

792769
def rustfmt(self):
793770
"""Return config path for rustfmt"""
794771
if self.stage0_rustfmt is None:
795772
return None
796773
return self.program_config('rustfmt')
797774

798-
def program_config(self, program, stage0=True):
775+
def program_config(self, program):
799776
"""Return config path for the given program at the given stage
800777
801778
>>> rb = RustBuild()
802779
>>> rb.config_toml = 'rustc = "rustc"\\n'
803780
>>> rb.program_config('rustc')
804781
'rustc'
805782
>>> rb.config_toml = ''
806-
>>> cargo_path = rb.program_config('cargo', True)
807-
>>> cargo_path.rstrip(".exe") == os.path.join(rb.bin_root(True),
808-
... "bin", "cargo")
809-
True
810-
>>> cargo_path = rb.program_config('cargo', False)
811-
>>> cargo_path.rstrip(".exe") == os.path.join(rb.bin_root(False),
783+
>>> cargo_path = rb.program_config('cargo')
784+
>>> cargo_path.rstrip(".exe") == os.path.join(rb.bin_root(),
812785
... "bin", "cargo")
813786
True
814787
"""
815788
config = self.get_toml(program)
816789
if config:
817790
return os.path.expanduser(config)
818-
return os.path.join(self.bin_root(stage0), "bin", "{}{}".format(
791+
return os.path.join(self.bin_root(), "bin", "{}{}".format(
819792
program, self.exe_suffix()))
820793

821794
@staticmethod
@@ -871,14 +844,14 @@ def build_bootstrap(self):
871844
if "CARGO_BUILD_TARGET" in env:
872845
del env["CARGO_BUILD_TARGET"]
873846
env["CARGO_TARGET_DIR"] = build_dir
874-
env["RUSTC"] = self.rustc(True)
875-
env["LD_LIBRARY_PATH"] = os.path.join(self.bin_root(True), "lib") + \
847+
env["RUSTC"] = self.rustc()
848+
env["LD_LIBRARY_PATH"] = os.path.join(self.bin_root(), "lib") + \
876849
(os.pathsep + env["LD_LIBRARY_PATH"]) \
877850
if "LD_LIBRARY_PATH" in env else ""
878-
env["DYLD_LIBRARY_PATH"] = os.path.join(self.bin_root(True), "lib") + \
851+
env["DYLD_LIBRARY_PATH"] = os.path.join(self.bin_root(), "lib") + \
879852
(os.pathsep + env["DYLD_LIBRARY_PATH"]) \
880853
if "DYLD_LIBRARY_PATH" in env else ""
881-
env["LIBRARY_PATH"] = os.path.join(self.bin_root(True), "lib") + \
854+
env["LIBRARY_PATH"] = os.path.join(self.bin_root(), "lib") + \
882855
(os.pathsep + env["LIBRARY_PATH"]) \
883856
if "LIBRARY_PATH" in env else ""
884857

@@ -900,7 +873,7 @@ def build_bootstrap(self):
900873
if self.get_toml("deny-warnings", "rust") != "false":
901874
env["RUSTFLAGS"] += " -Dwarnings"
902875

903-
env["PATH"] = os.path.join(self.bin_root(True), "bin") + \
876+
env["PATH"] = os.path.join(self.bin_root(), "bin") + \
904877
os.pathsep + env["PATH"]
905878
if not os.path.isfile(self.cargo()):
906879
raise Exception("no cargo executable found at `{}`".format(
@@ -1172,7 +1145,7 @@ def bootstrap(help_triggered):
11721145
# Fetch/build the bootstrap
11731146
build.download_toolchain()
11741147
# Download the master compiler if `download-rustc` is set
1175-
build.maybe_download_ci_toolchain()
1148+
# build.maybe_download_ci_toolchain()
11761149
sys.stdout.flush()
11771150
build.ensure_vendored()
11781151
build.build_bootstrap()

src/bootstrap/builder.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -915,15 +915,12 @@ impl<'a> Builder<'a> {
915915
}
916916
}
917917

918-
pub(crate) fn unpack(&self, tarball: &Path, dst: &Path) {
918+
pub(crate) fn unpack(&self, tarball: &Path, dst: &Path, pattern: &str) {
919919
println!("extracting {} to {}", tarball.display(), dst.display());
920920
if !dst.exists() {
921921
t!(fs::create_dir_all(dst));
922922
}
923923

924-
// FIXME: will need to be a parameter once `download-rustc` is moved to rustbuild
925-
const MATCH: &str = "rust-dev";
926-
927924
// `tarball` ends with `.tar.xz`; strip that suffix
928925
// example: `rust-dev-nightly-x86_64-unknown-linux-gnu`
929926
let uncompressed_filename =
@@ -943,10 +940,10 @@ impl<'a> Builder<'a> {
943940
continue;
944941
}
945942
let mut short_path = t!(original_path.strip_prefix(directory_prefix));
946-
if !short_path.starts_with(MATCH) {
943+
if !short_path.starts_with(pattern) {
947944
continue;
948945
}
949-
short_path = t!(short_path.strip_prefix(MATCH));
946+
short_path = t!(short_path.strip_prefix(pattern));
950947
let dst_path = dst.join(short_path);
951948
self.verbose(&format!("extracting {} to {}", original_path.display(), dst.display()));
952949
if !t!(member.unpack_in(dst)) {
@@ -1022,7 +1019,7 @@ impl<'a> Builder<'a> {
10221019
.join("lib");
10231020
// Avoid deleting the rustlib/ directory we just copied
10241021
// (in `impl Step for Sysroot`).
1025-
if !builder.config.download_rustc {
1022+
if !builder.download_rustc() {
10261023
let _ = fs::remove_dir_all(&sysroot);
10271024
t!(fs::create_dir_all(&sysroot));
10281025
}
@@ -1179,6 +1176,10 @@ impl<'a> Builder<'a> {
11791176
Config::llvm_link_shared(self)
11801177
}
11811178

1179+
pub(crate) fn download_rustc(&self) -> bool {
1180+
Config::download_rustc(self)
1181+
}
1182+
11821183
/// Prepares an invocation of `cargo` to be run.
11831184
///
11841185
/// This will create a `Command` that represents a pending execution of

src/bootstrap/compile.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ impl Step for Std {
4242
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
4343
// When downloading stage1, the standard library has already been copied to the sysroot, so
4444
// there's no need to rebuild it.
45-
let download_rustc = run.builder.config.download_rustc;
46-
run.all_krates("test").path("library").default_condition(!download_rustc)
45+
let builder = run.builder;
46+
run.all_krates("test")
47+
.path("library")
48+
.lazy_default_condition(Box::new(|| !builder.download_rustc()))
4749
}
4850

4951
fn make_run(run: RunConfig<'_>) {
@@ -66,7 +68,7 @@ impl Step for Std {
6668
// Don't recompile them.
6769
// NOTE: the ABI of the beta compiler is different from the ABI of the downloaded compiler,
6870
// so its artifacts can't be reused.
69-
if builder.config.download_rustc && compiler.stage != 0 {
71+
if builder.download_rustc() && compiler.stage != 0 {
7072
return;
7173
}
7274

@@ -551,7 +553,7 @@ impl Step for Rustc {
551553

552554
// NOTE: the ABI of the beta compiler is different from the ABI of the downloaded compiler,
553555
// so its artifacts can't be reused.
554-
if builder.config.download_rustc && compiler.stage != 0 {
556+
if builder.download_rustc() && compiler.stage != 0 {
555557
// Copy the existing artifacts instead of rebuilding them.
556558
// NOTE: this path is only taken for tools linking to rustc-dev.
557559
builder.ensure(Sysroot { compiler });
@@ -995,7 +997,7 @@ impl Step for Sysroot {
995997
t!(fs::create_dir_all(&sysroot));
996998

997999
// If we're downloading a compiler from CI, we can use the same compiler for all stages other than 0.
998-
if builder.config.download_rustc && compiler.stage != 0 {
1000+
if builder.download_rustc() && compiler.stage != 0 {
9991001
assert_eq!(
10001002
builder.config.build, compiler.host,
10011003
"Cross-compiling is not yet supported with `download-rustc`",
@@ -1090,7 +1092,7 @@ impl Step for Assemble {
10901092
let build_compiler = builder.compiler(target_compiler.stage - 1, builder.config.build);
10911093

10921094
// If we're downloading a compiler from CI, we can use the same compiler for all stages other than 0.
1093-
if builder.config.download_rustc {
1095+
if builder.download_rustc() {
10941096
builder.ensure(Sysroot { compiler: target_compiler });
10951097
return target_compiler;
10961098
}

0 commit comments

Comments
 (0)