chaquo / chaquopy

Chaquopy: the Python SDK for Android
https://chaquo.com/chaquopy/
MIT License
850 stars 132 forks source link

Cryptography 42 and rust #1261

Closed emanuele-f closed 1 month ago

emanuele-f commented 1 month ago

This adds support for building PyO3 modules like cryptography which depend on pyo3 >= 0.16.4. Building rust modules requires installing the rust compiler android targets as follows:

# install the stable rust version
# Note cryptography 42 requires Rust >= 1.63.0
curl https://sh.rustup.rs -sSf | sh -s -- -y

# add the android targets for cross compilation
rustup target add aarch64-linux-android
rustup target add arm-linux-androideabi
rustup target add i686-linux-android
rustup target add x86_64-linux-android

Compared to https://github.com/chaquo/chaquopy/issues/657, it's now possible to compile the cryptography module without issues because they migrated to PyO3 0.20, which removes the need to rebuild python, as sysconfig modules are not needed.

Moreover, since chaquopy updated the ndk to 26, this fixes ld: error: unable to find library -lunwind in recent Rust versions (see https://github.com/rust-lang/rust/issues/103673), removing the constrain to use old Rust versions like 1.60; it's now possible to just use the latest stable Rust version. For example, it's now possible to compile the latest mitmproxy_rs module which requires a recent Rust version (see https://github.com/emanuele-f/chaquopy/tree/mitmproxy-11/server/pypi/packages/mitmproxy-rs); a separate PR will be provided to include this module in the supported packages.

Ps. double check if https://github.com/chaquo/chaquopy/commit/4461ddaad28c422d7e7ece57b4ebe44f2665bd3a breaks anything, as I can see that #1141 is still open even if the issue was identified.

mhsmith commented 1 month ago

Thanks very much, I'll look at this as soon as I can.

Michu44 commented 1 month ago

@emanuele-f thank you so much for your contributions! I have been trying to build cryptography 43 for the past 2 days, and still not much luck.

I've seen your email around here somewhere... any chance I can shoot you a direct message or Email somewhere? I could really use your help and we are happy to pay you if you could help us solve our current issues.

Edit: Actually, you can find my contact info in my profile, can you pls. shoot me an email?

emanuele-f commented 1 month ago

Hi @Michu44 , which python version are you targetting? I only tested this with Python 3.10.

After installing the rust toolchain and android targets as explained above, you should be able to build cryprography as any normal wheel, with the standard build-wheel.py invocation. Which errors do you get?

My email is black.silver@hotmail.it but probably better to discuss here, as it would be beneficial for other people too. Also my intention is to allow anyone to build this without any issues

emanuele-f commented 1 month ago

Ps. building cryptography 43.0.1 for Python 3.10 from this PR branch requires minimal changes, just apply the following patch:

diff --git a/server/pypi/packages/cryptography/meta.yaml b/server/pypi/packages/cryptography/meta.yaml
index 0d042e84..59e63fd0 100644
--- a/server/pypi/packages/cryptography/meta.yaml
+++ b/server/pypi/packages/cryptography/meta.yaml
@@ -1,6 +1,6 @@
 package:
   name: cryptography
-  version: "42.0.8"
+  version: "43.0.1"

 requirements:
   build:
diff --git a/server/pypi/packages/cryptography/patches/pyo3_no_interpreter.patch b/server/pypi/packages/cryptography/patches/pyo3_no_interpreter.patch
index 3f03ced5..2d0b9d65 100644
--- a/server/pypi/packages/cryptography/patches/pyo3_no_interpreter.patch
+++ b/server/pypi/packages/cryptography/patches/pyo3_no_interpreter.patch
@@ -1,11 +1,11 @@
 --- src-original/src/rust/Cargo.toml
 +++ src/src/rust/Cargo.toml
-@@ -10,7 +10,7 @@
+@@ -17,7 +17,7 @@
  [dependencies]
  once_cell = "1"
  cfg-if = "1"
--pyo3 = { version = "0.20", features = ["abi3"] }
-+pyo3 = { version = "0.20", features = ["abi3", "abi3-py310"] }
- asn1 = { version = "0.15.5", default-features = false }
+-pyo3 = { version = "0.22.2", features = ["abi3"] }
++pyo3 = { version = "0.22.2", features = ["abi3", "abi3-py310"] }
+ asn1 = { version = "0.16.2", default-features = false }
  cryptography-cffi = { path = "cryptography-cffi" }
- cryptography-key-parsing = { path = "cryptography-key-parsing" }
+ cryptography-keepalive = { path = "cryptography-keepalive" }
Michu44 commented 1 month ago

@emanuele-f Thank you so much for the patch. I'll try and build this using your current code base then. The end-goal for me is to get the current pyatv library to work on chaquopy, as it seams only cryptography seams to be missing at this point. I'll try and figure it out again with your PR.

Michu44 commented 1 month ago

@emanuele-f I'm targeting either 3.10 or 3.11 as pyatv is working on both.

mhsmith commented 1 month ago

I've now released cryptography 42.0.8 wheels for Python 3.10 - 3.13. Thanks again!

emanuele-f commented 1 month ago

Awesome, thanks @mhsmith !

maciekglowka commented 6 days ago

Hi, thanks for your work on the Rust support :) Would this approach work for crates that don't support ABI3 ? I am currently trying to build pydantic core, but due to a jiter dependency I can't use this feature on PyO3. [otherwise I keep getting linking errors with libgcc as previously]

emanuele-f commented 6 days ago

Would this approach work for crates that don't support ABI3 ?

Hello, no, for non-ABI3 modules currently you will still need to cross compile the Python interpreter as explained in https://github.com/chaquo/chaquopy/issues/657

maciekglowka commented 6 days ago

Shoot....thanks for the quick answer though. I was worried that'd be the case (after spending half a day again trying different configurations). I managed to even work around the jiter thing (it has cfg flags). But then pydantic itself import loads of non-abi3 types, so...

mhsmith commented 6 days ago

These comments would be more useful on the pydantic issue (#1017), so I've moved them there.