Skip to content

Commit 72a844d

Browse files
anpMark-Simulacrum
andcommitted
bootstrap.py fetches rustfmt.
Co-Authored-By: Mark Rousskov <[email protected]>
1 parent 8369a1a commit 72a844d

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/bootstrap/bootstrap.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/bootstrap/bootstrap_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ def setUp(self):
2020
os.mkdir(os.path.join(self.rust_root, "src"))
2121
with open(os.path.join(self.rust_root, "src",
2222
"stage0.txt"), "w") as stage0:
23-
stage0.write("#ignore\n\ndate: 2017-06-15\nrustc: beta\ncargo: beta")
23+
stage0.write("#ignore\n\ndate: 2017-06-15\nrustc: beta\ncargo: beta\nrustfmt: beta")
2424

2525
def tearDown(self):
2626
rmtree(self.rust_root)
2727

2828
def test_stage0_data(self):
2929
"""Extract data from stage0.txt"""
30-
expected = {"date": "2017-06-15", "rustc": "beta", "cargo": "beta"}
30+
expected = {"date": "2017-06-15", "rustc": "beta", "cargo": "beta", "rustfmt": "beta"}
3131
data = bootstrap.stage0_data(self.rust_root)
3232
self.assertDictEqual(data, expected)
3333

src/stage0.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ date: 2019-12-18
1616
rustc: beta
1717
cargo: beta
1818

19+
# We use a nightly rustfmt to format the source because it solves some bootstrapping
20+
# issues with use of new syntax in this repo. If you're looking at the beta/stable branch, this key should be omitted,
21+
# as we don't want to depend on rustfmt from nightly there.
22+
rustfmt: nightly-2019-11-05
23+
1924
# When making a stable release the process currently looks like:
2025
#
2126
# 1. Produce stable build, upload it to dev-static

0 commit comments

Comments
 (0)