Skip to content

Commit 13c1fce

Browse files
authored
Merge pull request #1261 from emanuele-f/cryptography-42-and-rust
Cryptography 42 and rust
2 parents e57c25f + d900438 commit 13c1fce

File tree

8 files changed

+66
-57
lines changed

8 files changed

+66
-57
lines changed

server/pypi/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ these can be installed using your distribution. Some of them have special entrie
5959
[here](https://github.com/mzakharo/android-gfortran/releases/tag/r21e). Create a
6060
`fortran` subdirectory in the same directory as this README, and unpack the .bz2 files
6161
into it.
62+
* `rust`: `rustup` must be on the PATH.
6263

6364

6465
## Building a package

server/pypi/build-wheel.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def main(self):
8585
normalize_version(self.version))
8686

8787
self.non_python_build_reqs = set()
88-
for name in ["cmake", "fortran"]:
88+
for name in ["cmake", "fortran", "rust"]:
8989
try:
9090
self.meta["requirements"]["build"].remove(name)
9191
except ValueError:
@@ -611,6 +611,10 @@ def get_common_env_vars(self, env):
611611
env["CPPFLAGS"] = ""
612612
env["LDSHARED"] = f"{env['CC']} -shared"
613613

614+
if exists(f"{self.host_env}/chaquopy/lib/libssl.so"):
615+
# `cryptography` requires this variable.
616+
env["OPENSSL_DIR"] = f"{self.host_env}/chaquopy"
617+
614618
def get_python_env_vars(self, env, pypi_env):
615619
# Adding host_env to PYTHONPATH allows setup.py to import requirements, for
616620
# example to call numpy.get_include().
@@ -633,7 +637,26 @@ def get_python_env_vars(self, env, pypi_env):
633637

634638
# Overrides sysconfig.get_platform and distutils.util.get_platform.
635639
# TODO: consider replacing this with crossenv.
636-
env["_PYTHON_HOST_PLATFORM"] = f"linux_{ABIS[self.abi].uname_machine}"
640+
env["_PYTHON_HOST_PLATFORM"] = f"linux-{ABIS[self.abi].uname_machine}"
641+
642+
def get_rust_env_vars(self, env):
643+
tool_prefix = ABIS[self.abi].tool_prefix
644+
run(f"rustup target add {tool_prefix}")
645+
env.update({
646+
"RUSTFLAGS": f"-C linker={env['CC']} -L native={self.host_env}/chaquopy/lib",
647+
"CARGO_BUILD_TARGET": tool_prefix,
648+
649+
# Normally PyO3 requires sysconfig modules, which are not currently
650+
# available in the `target` packages for Python 3.12 and older. However,
651+
# since PyO3 0.16.4, it's possible to compile abi3 modules without sysconfig
652+
# modules. This only requires packages to specify the minimum python
653+
# compatibility version via one of the "abi3-py*" features (e.g.
654+
# abi3-py310). Doing this requires the "-L native" flag in RUSTFLAGS above.
655+
# https://pyo3.rs/main/building-and-distribution#building-abi3-extensions-without-a-python-interpreter
656+
"PYO3_NO_PYTHON": "1",
657+
"PYO3_CROSS": "1",
658+
"PYO3_CROSS_PYTHON_VERSION": self.python,
659+
})
637660

638661
@contextmanager
639662
def env_vars(self):
@@ -649,6 +672,8 @@ def env_vars(self):
649672

650673
if self.needs_python:
651674
self.get_python_env_vars(env, pypi_env)
675+
if "rust" in self.non_python_build_reqs:
676+
self.get_rust_env_vars(env)
652677

653678
env.update({
654679
# TODO: make everything use HOST instead, and remove this.

server/pypi/compiler-wrapper.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
valid_dirs = [abspath(f"{dirname(sys.argv[0])}/../..")]
1313

1414
def is_valid(dir, prefix):
15-
if any(commonpath([vd, abspath(dir)]) == vd for vd in valid_dirs):
15+
absdir = abspath(dir)
16+
if (
17+
any(commonpath([vd, absdir]) == vd for vd in valid_dirs)
18+
or (".cargo" in absdir) or (".rustup" in absdir)
19+
):
1620
return True
1721
else:
1822
print(f"Chaquopy: ignored invalid {prefix} directory: {dir!r}", file=sys.stderr)

server/pypi/packages/cffi/meta.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package:
22
name: cffi
3-
version: "1.16.0"
3+
version: "1.17.1"
44

55
build:
66
number: 0

server/pypi/packages/cryptography/build-with-static-openssl.sh

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,9 @@
11
package:
22
name: cryptography
3-
version: "3.4.8"
4-
5-
build:
6-
number: 2
7-
script_env:
8-
- CRYPTOGRAPHY_DONT_BUILD_RUST=1
3+
version: "42.0.8"
94

105
requirements:
116
build:
12-
- cffi 1.15.1
13-
- setuptools-rust 0.11.6
7+
- rust
148
host:
15-
# This version of cryptography isn't compatible with OpenSSL 3. So to build it for
16-
# Python 3.9 and 3.10, we link it against OpenSSL 1.1.
17-
#
18-
# We don't do this by supplying an OpenSSL 1.1 wheel with a shared library, because
19-
# the Chaquopy runtime (perhaps unnecessarily) loads non-Python libraries using
20-
# RTLD_GLOBAL, which could cause conflicts with the OpenSSL 3 library which Chaquopy
21-
# loads on startup.
22-
#
23-
# Instead, we link against OpenSSL 1.1 statically, as follows:
24-
# * Download an OpenSSL 1.1 build from
25-
# https://github.com/beeware/cpython-android-source-deps/releases.
26-
# * For each combination of Python version and ABI, run build-with-static-openssl.sh
27-
# in this directory.
28-
#
29-
# Although this may cause some of OpenSSL's symbols to be exported by crytography's
30-
# Python modules, that's safe because Python modules are loaded using RTLD_LOCAL. And
31-
# although the GLOBAL/LOCAL distinction is only respected from API level 23, older
32-
# versions wouldn't include earlier libraries in the symbol search order for later
33-
# libraries anyway, unless they were listed in DT_NEEDED.
34-
#
35-
# More information:
36-
# * https://github.com/aosp-mirror/platform_bionic/blob/master/android-changes-for-ndk-developers.md
37-
# * https://github.com/android/ndk/issues/1244
389
- openssl
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--- src-original/src/rust/src/lib.rs
2+
+++ src/src/rust/src/lib.rs
3+
@@ -45,9 +45,10 @@
4+
// serialization), RC4, Blowfish, IDEA, SEED, etc. These things
5+
// are ugly legacy, but we aren't going to get rid of them
6+
// any time soon.
7+
- let load_legacy = env::var("CRYPTOGRAPHY_OPENSSL_NO_LEGACY")
8+
- .map(|v| v.is_empty() || v == "0")
9+
- .unwrap_or(true);
10+
+
11+
+ // Chaquopy: the legacy provider is not available.
12+
+ let load_legacy = false;
13+
+
14+
let legacy = if load_legacy {
15+
let legacy_result = provider::Provider::load(None, "legacy");
16+
_legacy_provider_error(legacy_result.is_ok())?;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--- src-original/src/rust/Cargo.toml
2+
+++ src/src/rust/Cargo.toml
3+
@@ -10,7 +10,10 @@
4+
[dependencies]
5+
once_cell = "1"
6+
cfg-if = "1"
7+
-pyo3 = { version = "0.20", features = ["abi3"] }
8+
+
9+
+# Chaquopy: added abi3-py10 - see needs_rust in build-wheel.py.
10+
+pyo3 = { version = "0.20", features = ["abi3", "abi3-py310"] }
11+
+
12+
asn1 = { version = "0.15.5", default-features = false }
13+
cryptography-cffi = { path = "cryptography-cffi" }
14+
cryptography-key-parsing = { path = "cryptography-key-parsing" }

0 commit comments

Comments
 (0)