Skip to content

Commit 34a9a33

Browse files
committed
Integrate Rust into the build process properly
1 parent bc4b956 commit 34a9a33

File tree

14 files changed

+104
-49
lines changed

14 files changed

+104
-49
lines changed

.github/workflows/ci.yml

+19-11
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,23 @@ jobs:
1515
strategy:
1616
matrix:
1717
PYTHON:
18-
- {VERSION: "2.7", TOXENV: "py27", EXTRA_CFLAGS: ""}
1918
- {VERSION: "3.5", TOXENV: "py35", EXTRA_CFLAGS: ""}
2019
- {VERSION: "3.8", TOXENV: "py38", EXTRA_CFLAGS: "-DUSE_OSRANDOM_RNG_FOR_TESTING"}
20+
RUST:
21+
- stable
2122
name: "Python ${{ matrix.PYTHON.VERSION }} on macOS"
2223
steps:
2324
- uses: actions/checkout@master
2425
- name: Setup python
2526
uses: actions/setup-python@v1
2627
with:
2728
python-version: ${{ matrix.PYTHON.VERSION }}
29+
- uses: actions-rs/toolchain@v1
30+
with:
31+
profile: minimal
32+
toolchain: ${{ matrix.RUST }}
33+
override: true
34+
default: true
2835

2936
- run: python -m pip install tox requests coverage
3037

@@ -55,14 +62,15 @@ jobs:
5562
strategy:
5663
matrix:
5764
WINDOWS:
58-
- {ARCH: 'x86', WINDOWS: 'win32'}
59-
- {ARCH: 'x64', WINDOWS: 'win64'}
65+
- {ARCH: 'x86', WINDOWS: 'win32', RUST_TRIPLE: 'i686-pc-windows-msvc'}
66+
- {ARCH: 'x64', WINDOWS: 'win64', RUST_TRIPLE: 'x86_64-pc-windows-msvc'}
6067
PYTHON:
61-
- {VERSION: "2.7", TOXENV: "py27", MSVC_VERSION: "2010", CL_FLAGS: ""}
6268
- {VERSION: "3.5", TOXENV: "py35", MSVC_VERSION: "2019", CL_FLAGS: ""}
6369
- {VERSION: "3.6", TOXENV: "py36", MSVC_VERSION: "2019", CL_FLAGS: ""}
6470
- {VERSION: "3.7", TOXENV: "py37", MSVC_VERSION: "2019", CL_FLAGS: ""}
6571
- {VERSION: "3.8", TOXENV: "py38", MSVC_VERSION: "2019", CL_FLAGS: "/D USE_OSRANDOM_RNG_FOR_TESTING"}
72+
RUST:
73+
- stable
6674
name: "Python ${{ matrix.PYTHON.VERSION }} on ${{ matrix.WINDOWS.WINDOWS }}"
6775
steps:
6876
- uses: actions/checkout@master
@@ -71,14 +79,14 @@ jobs:
7179
with:
7280
python-version: ${{ matrix.PYTHON.VERSION }}
7381
architecture: ${{ matrix.WINDOWS.ARCH }}
82+
- uses: actions-rs/toolchain@v1
83+
with:
84+
profile: minimal
85+
toolchain: ${{ matrix.RUST }}
86+
override: true
87+
default: true
88+
target: ${{ matrix.WINDOWS.RUST_TRIPLE }}
7489

75-
- name: Install MSVC for Python 2.7
76-
run: |
77-
Invoke-WebRequest -Uri https://download.microsoft.com/download/7/9/6/796EF2E4-801B-4FC4-AB28-B59FBF6D907B/VCForPython27.msi -OutFile VCForPython27.msi
78-
Start-Process msiexec -Wait -ArgumentList @('/i', 'VCForPython27.msi', '/qn', 'ALLUSERS=1')
79-
Remove-Item VCForPython27.msi -Force
80-
shell: powershell
81-
if: matrix.PYTHON.VERSION == '2.7'
8290
- run: python -m pip install tox requests coverage
8391
- name: Download OpenSSL
8492
run: |

.github/workflows/wheel-builder.yml

+16-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
container: ${{ matrix.MANYLINUX.CONTAINER }}
1212
strategy:
1313
matrix:
14-
PYTHON: ["cp27-cp27m", "cp27-cp27mu", "cp35-cp35m"]
14+
PYTHON: ["cp35-cp35m"]
1515
MANYLINUX:
1616
- NAME: manylinux1_x86_64
1717
CONTAINER: "pyca/cryptography-manylinux1:x86_64"
@@ -57,10 +57,6 @@ jobs:
5757
strategy:
5858
matrix:
5959
PYTHON:
60-
- VERSION: '2.7'
61-
ABI_VERSION: '2.7'
62-
DOWNLOAD_URL: 'https://www.python.org/ftp/python/2.7.17/python-2.7.17-macosx10.9.pkg'
63-
BIN_PATH: '/Library/Frameworks/Python.framework/Versions/2.7/bin/python'
6460
- VERSION: '3.8'
6561
ABI_VERSION: '3.5'
6662
DOWNLOAD_URL: 'https://www.python.org/ftp/python/3.8.2/python-3.8.2-macosx10.9.pkg'
@@ -79,6 +75,12 @@ jobs:
7975
${{ matrix.PYTHON.BIN_PATH }} .github/workflows/download_openssl.py macos openssl-macos
8076
env:
8177
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
78+
- uses: actions-rs/toolchain@v1
79+
with:
80+
profile: minimal
81+
toolchain: stable
82+
override: true
83+
default: true
8284

8385
- run: ${{ matrix.PYTHON.BIN_PATH }} -m virtualenv venv
8486
- run: venv/bin/pip install -U pip wheel cffi six ipaddress "enum34; python_version < '3'"
@@ -111,10 +113,9 @@ jobs:
111113
strategy:
112114
matrix:
113115
WINDOWS:
114-
- {ARCH: 'x86', WINDOWS: 'win32'}
115-
- {ARCH: 'x64', WINDOWS: 'win64'}
116+
- {ARCH: 'x86', WINDOWS: 'win32', RUST_TRIPLE: 'i686-pc-windows-msvc'}
117+
- {ARCH: 'x64', WINDOWS: 'win64', RUST_TRIPLE: 'x86_64-pc-windows-msvc'}
116118
PYTHON:
117-
- {VERSION: "2.7", MSVC_VERSION: "2010"}
118119
- {VERSION: "3.5", MSVC_VERSION: "2019"}
119120
- {VERSION: "3.6", MSVC_VERSION: "2019"}
120121
- {VERSION: "3.7", MSVC_VERSION: "2019"}
@@ -128,13 +129,13 @@ jobs:
128129
with:
129130
python-version: ${{ matrix.PYTHON.VERSION }}
130131
architecture: ${{ matrix.WINDOWS.ARCH }}
131-
- name: Install MSVC for Python 2.7
132-
run: |
133-
Invoke-WebRequest -Uri https://download.microsoft.com/download/7/9/6/796EF2E4-801B-4FC4-AB28-B59FBF6D907B/VCForPython27.msi -OutFile VCForPython27.msi
134-
Start-Process msiexec -Wait -ArgumentList @('/i', 'VCForPython27.msi', '/qn', 'ALLUSERS=1')
135-
Remove-Item VCForPython27.msi -Force
136-
shell: powershell
137-
if: matrix.PYTHON.VERSION == '2.7'
132+
- uses: actions-rs/toolchain@v1
133+
with:
134+
profile: minimal
135+
toolchain: stable
136+
override: true
137+
default: true
138+
target: ${{ matrix.WINDOWS.RUST_TRIPLE }}
138139
- run: pip install requests
139140
- name: Download OpenSSL
140141
run: |

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ htmlcov/
1212
.eggs/
1313
*.py[cdo]
1414
.hypothesis/
15+
target/
16+
Cargo.lock

.travis.yml

+3-21
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,18 @@ branches:
1919
matrix:
2020
include:
2121
- python: 3.8
22-
env: TOXENV=pep8,packaging
22+
env: TOXENV=pep8,rust,packaging
2323
# Setting 'python' is just to make travis's UI a bit prettier
2424
- python: 3.6
2525
env: TOXENV=py36
2626
# Travis lists available Pythons (including PyPy) by arch and distro here:
2727
# https://docs.travis-ci.com/user/languages/python/#python-versions
28-
- python: pypy2.7-7.3.1
29-
env: TOXENV=pypy-nocoverage
3028
- python: pypy3.6-7.3.1
3129
env: TOXENV=pypy3-nocoverage
3230
- python: 3.8
3331
env: TOXENV=py38 OPENSSL=1.0.2u
34-
- python: 2.7
35-
env: TOXENV=py27 OPENSSL=1.1.0l
36-
- python: 2.7
37-
env: TOXENV=py27-ssh OPENSSL=1.1.0l
3832
- python: 3.8
3933
env: TOXENV=py38 OPENSSL=1.1.0l
40-
- python: 2.7
41-
env: TOXENV=py27 OPENSSL=1.1.1g
4234
- python: 3.8
4335
env: TOXENV=py38 OPENSSL=1.1.1g
4436
- python: 3.8
@@ -54,21 +46,12 @@ matrix:
5446
- python: 3.8
5547
env: TOXENV=py38 LIBRESSL=3.2.0
5648

57-
- python: 2.7
58-
services: docker
59-
env: TOXENV=py27 DOCKER=pyca/cryptography-runner-centos7
60-
- python: 2.7
61-
services: docker
62-
env: TOXENV=py27 DOCKER=pyca/cryptography-runner-centos8
6349
- python: 3.6
6450
services: docker
6551
env: TOXENV=py36 DOCKER=pyca/cryptography-runner-centos8
6652
- python: 3.6
6753
services: docker
6854
env: TOXENV=py36 OPENSSL_FORCE_FIPS_MODE=1 DOCKER=pyca/cryptography-runner-centos8-fips
69-
- python: 2.7
70-
services: docker
71-
env: TOXENV=py27 DOCKER=pyca/cryptography-runner-stretch
7255
- python: 3.5
7356
services: docker
7457
env: TOXENV=py35 DOCKER=pyca/cryptography-runner-stretch
@@ -87,9 +70,6 @@ matrix:
8770
- python: 3.8
8871
services: docker
8972
env: TOXENV=py38 DOCKER=pyca/cryptography-runner-ubuntu-focal
90-
- python: 2.7
91-
services: docker
92-
env: TOXENV=py27 DOCKER=pyca/cryptography-runner-ubuntu-rolling
9373
- python: 3.8
9474
services: docker
9575
env: TOXENV=py38 DOCKER=pyca/cryptography-runner-ubuntu-rolling
@@ -138,9 +118,11 @@ matrix:
138118
dist: xenial
139119

140120
install:
121+
- curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain stable --component rustmft
141122
- ./.travis/install.sh
142123

143124
script:
125+
- source $HOME/.cargo/env
144126
- ./.travis/run.sh
145127

146128
after_success:

.zuul.playbooks/playbooks/main.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
become: yes
2828
when: ansible_distribution == 'CentOS'
2929

30+
- name: Install rust
31+
include_role:
32+
name: ensure-rust
33+
3034
- name: Clone wycheproof
3135
git:
3236
repo: https://github.com/google/wycheproof

CHANGELOG.rst

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Changelog
33

44
.. _v3-1:
55

6+
TODO: Add Rust to the current release when this is ready to land.
7+
68
3.1 - `master`_
79
~~~~~~~~~~~~~~~
810

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ include pyproject.toml
1111

1212
recursive-include docs *
1313
recursive-include src/_cffi_src *.py *.c *.h
14+
recursive-include src/rust Cargo.toml *.rs
1415
prune docs/_build
1516
recursive-include tests *.py
1617
exclude vectors

docs/faq.rst

+13
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ series. Since they are no longer receiving security patches from upstream,
118118
should upgrade to a newer version of OpenSSL (1.0.2 or later). This may require
119119
you to upgrade to a newer operating system.
120120

121+
Installing ``cryptography`` fails with ``error: Can not find Rust compiler``
122+
----------------------------------------------------------------------------
123+
124+
Building ``cryptography`` from source requires you have Rust installed and on
125+
your ``PATH``. You may be able to fix this by upgrading to a newer version of
126+
``pip`` which will install a pre-compiled ``cryptography`` wheel. If not,
127+
you'll need to install Rust.
128+
129+
For the current release *only* you can temporarily bypass the requirement to
130+
have Rust installed by setting the ``CRYPTOGRAPHY_DONT_BUILD_RUST`` environment
131+
variable. Note that this option will be removed in the next release and not
132+
having Rust available will be a hard error.
133+
121134
Why are there no wheels for Python 3.6+ on Linux or macOS?
122135
----------------------------------------------------------
123136

docs/installation.rst

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ platforms). ``cryptography`` links against the new 1.1.0 names by default. If
6666
you need to compile ``cryptography`` against an older version then you **must**
6767
set ``CRYPTOGRAPHY_WINDOWS_LINK_LEGACY_OPENSSL`` or else installation will fail.
6868

69+
You will also need to have Rust installed and available.
70+
6971
If you need to rebuild ``cryptography`` for any reason be sure to clear the
7072
local `wheel cache`_.
7173

@@ -88,6 +90,8 @@ If you are on Alpine or just want to compile it yourself then
8890
using ``pypy``), and headers for the OpenSSL and ``libffi`` libraries
8991
available on your system.
9092

93+
On all Linux distributions you will need to have Rust installed and available.
94+
9195
Alpine
9296
~~~~~~
9397

@@ -238,6 +242,8 @@ open a terminal window and run:
238242
This will install a compiler (clang) along with (most of) the required
239243
development headers.
240244

245+
You will also need to have Rust installed and available.
246+
241247
You'll also need OpenSSL, which you can obtain from `Homebrew`_ or `MacPorts`_.
242248
Cryptography does **not** support Apple's deprecated OpenSSL distribution.
243249

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ requires = [
66
"wheel",
77
# Must be kept in sync with the `setup_requirements` in `setup.py`
88
"cffi>=1.8,!=1.11.3; platform_python_implementation != 'PyPy'",
9+
"setuptools-rust>=0.11.1",
910
]
1011
build-backend = "setuptools.build_meta"
1112

setup.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from setuptools import find_packages, setup
1818
from setuptools.command.install import install
1919

20+
from setuptools_rust import RustExtension
21+
2022

2123
if pkg_resources.parse_version(
2224
setuptools.__version__
@@ -39,7 +41,7 @@
3941

4042

4143
# `setup_requirements` must be kept in sync with `pyproject.toml`
42-
setup_requirements = ["cffi>=1.8,!=1.11.3"]
44+
setup_requirements = ["cffi>=1.8,!=1.11.3", "setuptools-rust>=0.11.1"]
4345

4446
if platform.python_implementation() == "PyPy":
4547
if sys.pypy_version_info < (5, 4):
@@ -152,10 +154,17 @@ def argument_without_setup_requirements(argv, i):
152154
"src/_cffi_src/build_openssl.py:ffi",
153155
"src/_cffi_src/build_padding.py:ffi",
154156
]
157+
if os.environ.get("CRYPTOGRAPHY_DONT_BUILD_RUST"):
158+
rust_extensions = []
159+
else:
160+
rust_extensions = [
161+
RustExtension("_rust", "src/rust/Cargo.toml"),
162+
]
155163

156164
return {
157165
"setup_requires": setup_requirements,
158166
"cffi_modules": cffi_modules,
167+
"rust_extensions": rust_extensions,
159168
}
160169

161170

src/rust/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "cryptography-rust"
3+
version = "0.1.0"
4+
authors = ["The cryptography developers <[email protected]>"]
5+
edition = "2018"
6+
publish = false
7+
8+
[dependencies]
9+
pyo3 = { version = "0.11", features = ["extension-module"] }
10+
11+
[lib]
12+
name = "cryptography_rust"
13+
crate-type = ["cdylib"]

src/rust/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[pyo3::prelude::pymodule]
2+
fn _rust(_py: pyo3::Python<'_>, _m: &pyo3::types::PyModule) -> pyo3::PyResult<()> {
3+
Ok(())
4+
}

tox.ini

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tox]
22
minversion = 2.4
3-
envlist = py27,pypy,py35,py36,py37,py38,docs,pep8,packaging
3+
envlist = py27,pypy,py35,py36,py37,py38,docs,pep8,rust,packaging
44
isolated_build = True
55

66
[testenv]
@@ -67,6 +67,15 @@ commands =
6767
flake8 .
6868
black --check .
6969

70+
[testenv:rust]
71+
basepython = python3
72+
changedir = src/rust/
73+
allowlist_externals =
74+
cargo
75+
commands =
76+
cargo fmt --all -- --check
77+
cargo clippy
78+
7079
[testenv:packaging]
7180
deps =
7281
check-manifest

0 commit comments

Comments
 (0)