Anki/cargo/update.py
Damien Elmes e9fdb5600c use ring on Linux; native-tls on other platforms
Python wheels on Linux require statically linked SSL libraries.
We were previously relying on the native-tls-vendored feature in
reqwest, but that does not work with Bazel, as openssl-src makes
assumptions that break when sandboxed. The static libs distributed
by distros like Ubuntu fail to link, and while we could potentially
build OpenSSL ourselves, we'd then need to keep it up to
date.

On Windows and Mac however, native-tls is preferable to ring, as it
allows us to get free updates from the OS, and results in
a smaller library.

Rust currently only supports platform-specific features in nightly,
and cargo-raze does not have support for them, so we currently need
to override the generated build file with a hand-crafted one that
specifies the relative features/deps for each platform.

update.py has been updated to automatically keep the version numbers
in this file up to date, so it should hopefully not prove too hard to
maintain going forward.
2020-11-15 15:25:28 +10:00

156 lines
4.4 KiB
Python

#!/usr/bin/env python3
#
# See README.md
EXTRA_DEPS = [
'pyo3 = { git = "https://github.com/PyO3/pyo3.git", rev = "3b3ba4e3abd57bc3b8f86444b3f61e6e2f4c5fc1", features = ["extension-module", "abi3"] }'
]
# If you get a message like the following during a build:
#
# DEBUG: Rule 'raze__reqwest__0_10_8' indicated that a canonical reproducible
# form can be obtained by modifying arguments shallow_since = "1604362745 +1000"
# ...then the commit and shallow_since argument should be added below, as this
# will remove the debug warning, and speed up the git clone.
COMMITS_SHALLOW_SINCE = {
# reqwest
"eab12efe22f370f386d99c7d90e7a964e85dd071": "1604362745 +1000",
# hyper-timeout
"f9ef687120d88744c1da50a222e19208b4553503": "1604362633 +1000",
# tokio-io-timeout
"96e1358555c49905de89170f2b1102a7d8b6c4c2": "1598411535 +1000",
# prost
"4ded4a98ef339da0b7babd4efee3fbe8adaf746b": "1598739849 -0700",
# pyo3
"3b3ba4e3abd57bc3b8f86444b3f61e6e2f4c5fc1": "1603809036 +0900",
}
import os
import sys
import subprocess
import shutil
import re
import glob
if os.getcwd() != os.path.abspath(os.path.dirname(__file__)):
print("Run this from the cargo/ folder")
sys.exit(1)
def write_cargo_toml():
with open("raze.toml") as file:
header = file.read()
deps = []
with open("../rslib/Cargo.toml") as file:
started = False
for line in file.readlines():
if line.startswith("# BEGIN DEPENDENCIES"):
started = True
continue
if not started:
continue
deps.append(line.strip())
deps.extend(EXTRA_DEPS)
# write out Cargo.toml
with open("Cargo.toml", "w") as file:
file.write(header)
file.write("\n".join(deps))
def remove_cargo_toml():
os.remove("Cargo.toml")
def update_cargo_lock():
# update Cargo.lock
subprocess.run(["cargo", "update"], check=True)
def run_cargo_raze():
# generate cargo-raze files
if os.path.exists("remote"):
shutil.rmtree("remote")
subprocess.run(["cargo-raze"], check=True)
def write_licenses():
# dump licenses
result = subprocess.check_output(["cargo", "license", "-j"])
with open("licenses.json", "wb") as file:
file.write(result)
# export license file
with open("BUILD.bazel", "a", encoding="utf8") as file:
file.write(
"""
exports_files(["licenses.json"])
"""
)
def update_crates_bzl():
output_lines = []
commit_re = re.compile('\s+commit = "([0-9a-f]+)",')
reqwest_build_prefix = re.compile(r"/remote:BUILD.reqwest-\d+\.\d+\.\d+")
with open("crates.bzl") as file:
for line in file.readlines():
# update shallow-since references for git crates
if match := commit_re.match(line):
commit = match.group(1)
if commit in line:
if since := COMMITS_SHALLOW_SINCE.get(commit):
output_lines.append(f' shallow_since = "{since}",\n')
else:
print(f"{commit} not in COMMITS_SHALLOW_SINCE")
# use our custom reqwest build file
if match := reqwest_build_prefix.search(line):
line = line.replace(match.group(0), ":BUILD.reqwest")
output_lines.append(line)
with open("crates.bzl", "w") as file:
for line in output_lines:
file.write(line)
def generated_reqwest_build_file():
return glob.glob("remote/*reqwest-*")[0]
def update_reqwest_deps():
"Update version numbers in our custom build file."
dep_with_version = re.compile(r"@raze__(.+?)__([\d_]+)//")
version_map = {}
with open(generated_reqwest_build_file(), encoding="utf8") as file:
for line in file.readlines():
if match := dep_with_version.search(line):
version_map[match.group(1)] = match.group(2)
with open("BUILD.reqwest.bazel", "r+", encoding="utf8") as file:
def repl(m):
name = m.group(1)
current_version = m.group(2)
new_version = version_map.get(name)
return m.group(0).replace(current_version, new_version)
data = dep_with_version.sub(repl, file.read())
file.seek(0)
file.write(data)
write_cargo_toml()
update_cargo_lock()
run_cargo_raze()
write_licenses()
update_crates_bzl()
update_reqwest_deps()
remove_cargo_toml()