Skip to content

Commit 5656883

Browse files
danakjAravind Vasudevan
authored andcommitted
update_rust.py will try the latest and fallback revisions
Have --print-package-version print the stamp version, as the Clang update.py script does too. This tells the actual version of the toolchain in the repo. When updating Rust, check if the latest Rust/Clang revisions have a Rust toolchain package. Use if it so. If not, try the FALLBACK_REVISION. After we have chosen a version, then proceed to check if the stamp matches, and download a replacement toolchain if not. This should mean that clang rolls no longer need to bump the FALLBACK_REVISION to succeed. We can do it after the fact if we find that we can't roll Rust, to pin it to something newish. Bug: 1401042 Change-Id: Ic0436e87d18566f3498d30f3560fcc4e196ec240 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4406712 Reviewed-by: Collin Baker <collinbaker@chromium.org> Commit-Queue: danakj <danakj@chromium.org> Reviewed-by: Zequan Wu <zequanwu@google.com> Cr-Commit-Position: refs/heads/main@{#1128845} NOKEYCHECK=True GitOrigin-RevId: 8f23682d65831ac89b32764d98238513d3950cc3
1 parent f34946a commit 5656883

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

build_rust.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
from update_rust import (RUST_REVISION, RUST_TOOLCHAIN_OUT_DIR,
6262
STAGE0_JSON_SHA256, THIRD_PARTY_DIR, THIS_DIR,
63-
VERSION_STAMP_PATH, GetPackageVersionForBuild)
63+
VERSION_STAMP_PATH, GetLatestRevision)
6464

6565
EXCLUDED_TESTS = [
6666
# https://github.com/rust-lang/rust/issues/45222 which appears to have
@@ -442,7 +442,7 @@ def quote_string(s: str):
442442
subs = {}
443443
subs['INSTALL_DIR'] = quote_string(str(RUST_TOOLCHAIN_OUT_DIR))
444444
subs['LLVM_BIN'] = quote_string(str(self._llvm_bins_path))
445-
subs['PACKAGE_VERSION'] = GetPackageVersionForBuild()
445+
subs['PACKAGE_VERSION'] = GetLatestRevision()
446446

447447
subs["LLVM_CONFIG_WINDOWS_x86_64"] = quote_string(
448448
str(x86_64_llvm_config))
@@ -490,7 +490,7 @@ def MakeVersionStamp(git_hash):
490490
with open(RUST_SRC_VERSION_FILE_PATH) as version_file:
491491
rust_version = version_file.readline().rstrip()
492492
return (f'rustc {rust_version} {git_hash}'
493-
f' ({GetPackageVersionForBuild()} chromium)\n')
493+
f' ({GetLatestRevision()} chromium)\n')
494494

495495

496496
def GetLatestRustCommit():

package_rust.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
os.path.join(os.path.dirname(THIS_DIR), '..', 'clang', 'scripts'))
1616

1717
from build_rust import (RUST_TOOLCHAIN_OUT_DIR, THIRD_PARTY_DIR)
18-
from update_rust import (GetPackageVersionForBuild)
18+
from update_rust import (GetLatestRevision)
1919
from package import (MaybeUpload, TeeCmd)
2020
from update import (CHROMIUM_DIR)
2121

22-
PACKAGE_VERSION = GetPackageVersionForBuild()
22+
PACKAGE_VERSION = GetLatestRevision()
2323
BUILDLOG_NAME = f'rust-buildlog-{PACKAGE_VERSION}.txt'
2424
RUST_TOOLCHAIN_PACKAGE_NAME = f'rust-toolchain-{PACKAGE_VERSION}.tgz'
2525

update_rust.py

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import re
1717
import shutil
1818
import sys
19-
import tempfile
19+
import time
2020
import urllib
2121

2222
from pathlib import Path
@@ -82,20 +82,20 @@
8282
# Package version built in build_rust.py, which is always built against the
8383
# current Clang. Typically Clang and Rust revisions are both updated together
8484
# and this picks the Clang that has just been built.
85-
def GetPackageVersionForBuild():
85+
def GetLatestRevision():
8686
from update import (CLANG_REVISION, CLANG_SUB_REVISION)
8787
return (f'{RUST_REVISION}-{RUST_SUB_REVISION}'
8888
f'-{CLANG_REVISION}-{CLANG_SUB_REVISION}')
8989

9090

9191
# Package version for download. Ideally this is the latest Clang+Rust roll,
92-
# which was built successfully and is returned from GetPackageVersionForBuild().
92+
# which was built successfully and is returned from GetLatestRevision().
9393
# However at this time Clang rolls even if Rust fails to build, so we have Rust
9494
# pinned to the last known successful build with FALLBACK_REVISION. This should
9595
# go away once we block Clang rolls on Rust also being built.
9696
def GetDownloadPackageVersion():
9797
return FALLBACK_REVISION \
98-
if FALLBACK_REVISION else GetPackageVersionForBuild()
98+
if FALLBACK_REVISION else GetLatestRevision()
9999

100100

101101
# Get the version of the toolchain package we already have.
@@ -112,6 +112,28 @@ def GetStampVersion():
112112
return None
113113

114114

115+
def CheckUrl(url) -> bool:
116+
"""Check if a url exists."""
117+
num_retries = 3
118+
retry_wait_s = 5 # Doubled at each retry.
119+
120+
while True:
121+
try:
122+
urllib.request.urlopen(urllib.request.Request(url))
123+
return True
124+
except urllib.error.URLError as e:
125+
if e.code != 404:
126+
print(e)
127+
if num_retries == 0 or isinstance(
128+
e, urllib.error.HTTPError) and e.code == 404:
129+
return False
130+
num_retries -= 1
131+
print(f'Retrying in {retry_wait_s} s ...')
132+
sys.stdout.flush()
133+
time.sleep(retry_wait_s)
134+
retry_wait_s *= 2
135+
136+
115137
def main():
116138
parser = argparse.ArgumentParser(description='Update Rust package')
117139
parser.add_argument(
@@ -130,39 +152,54 @@ def main():
130152
return 0
131153

132154
if args.print_package_version:
133-
print(GetDownloadPackageVersion())
155+
stamp_version = GetStampVersion()
156+
if (stamp_version != GetLatestRevision()
157+
and stamp_version != FALLBACK_REVISION):
158+
print(
159+
f'The expected Rust version is {GetLatestRevision()} '
160+
f'(or fallback {FALLBACK_REVISION} but the actual version is '
161+
f'{stamp_version}')
162+
print('Did you run "gclient sync"?')
163+
return 1
164+
print(stamp_version)
134165
return 0
135166

136167
from update import (DownloadAndUnpack, GetDefaultHostOs,
137168
GetPlatformUrlPrefix)
138169

170+
platform_prefix = GetPlatformUrlPrefix(GetDefaultHostOs())
171+
172+
version = GetLatestRevision()
173+
url = f'{platform_prefix}rust-toolchain-{version}.tgz'
174+
if not CheckUrl(url):
175+
print("Latest Rust toolchain not found. Using fallback revision.")
176+
version = FALLBACK_REVISION
177+
url = f'{platform_prefix}rust-toolchain-{version}.tgz'
178+
if not CheckUrl(url):
179+
print('error: Could not find Rust toolchain package')
180+
return 1
181+
139182
# Exit early if the existing package is up-to-date. Note that we cannot
140183
# simply call DownloadAndUnpack() every time: aside from unnecessarily
141184
# downloading the toolchain if it hasn't changed, it also leads to multiple
142185
# versions of the same rustlibs. build/rust/std/find_std_rlibs.py chokes in
143186
# this case.
144187
if os.path.exists(RUST_TOOLCHAIN_OUT_DIR):
145-
if GetDownloadPackageVersion() == GetStampVersion():
188+
if version == GetStampVersion():
146189
return 0
147190

148191
if os.path.exists(RUST_TOOLCHAIN_OUT_DIR):
149192
shutil.rmtree(RUST_TOOLCHAIN_OUT_DIR)
150193

151194
try:
152195
platform_prefix = GetPlatformUrlPrefix(GetDefaultHostOs())
153-
version = GetDownloadPackageVersion()
154-
url = f'{platform_prefix}rust-toolchain-{version}.tgz'
155196
DownloadAndUnpack(url, THIRD_PARTY_DIR)
156197
except urllib.error.HTTPError as e:
157-
# Fail softly for now. This can happen if a Rust package was not
158-
# produced, e.g. if the Rust build failed upon a Clang update, or if a
159-
# Rust roll and a Clang roll raced against each other.
160-
#
161-
# TODO(https://crbug.com/1245714): Reconsider how to handle this.
162-
print(f'warning: could not download Rust package')
198+
print(f'error: Failed to download Rust package')
199+
return 1
163200

164201
# Ensure the newly extracted package has the correct version.
165-
assert GetDownloadPackageVersion() == GetStampVersion()
202+
assert version == GetStampVersion()
166203

167204

168205
if __name__ == '__main__':

0 commit comments

Comments
 (0)