1{ lib
2, stdenv
3, buildPythonPackage
4, isPyPy
5, fetchPypi
6, fetchpatch
7, pytestCheckHook
8, libffi
9, pkg-config
10, pycparser
11, pythonAtLeast
12}:
13
14if isPyPy then null else buildPythonPackage rec {
15 pname = "cffi";
16 version = "1.15.1";
17
18 src = fetchPypi {
19 inherit pname version;
20 hash = "sha256-1AC/uaN7E1ElPLQCZxzqfom97MKU6AFqcH9tHYrJNPk=";
21 };
22
23 patches = [
24 #
25 # Trusts the libffi library inside of nixpkgs on Apple devices.
26 #
27 # Based on some analysis I did:
28 #
29 # https://groups.google.com/g/python-cffi/c/xU0Usa8dvhk
30 #
31 # I believe that libffi already contains the code from Apple's fork that is
32 # deemed safe to trust in cffi.
33 #
34 ./darwin-use-libffi-closures.diff
35 (fetchpatch {
36 # Drop py.code usage from tests, no longer depend on the deprecated py package
37 url = "https://foss.heptapod.net/pypy/cffi/-/commit/9c7d865e17ec16a847090a3e0d1498b698b99756.patch";
38 excludes = [
39 "README.md"
40 "requirements.txt"
41 ];
42 hash = "sha256-HSuLLIYXXGGCPccMNLV7o1G3ppn2P0FGCrPjqDv2e7k=";
43 })
44 (fetchpatch {
45 # Replace py.test usage with pytest
46 url = "https://foss.heptapod.net/pypy/cffi/-/commit/bd02e1b122612baa74a126e428bacebc7889e897.patch";
47 excludes = [
48 "README.md"
49 "requirements.txt"
50 ];
51 hash = "sha256-+2daRTvxtyrCPimOEAmVbiVm1Bso9hxGbaAbd03E+ws=";
52 })
53 ] ++ lib.optionals (pythonAtLeast "3.11") [
54 # Fix test that failed because python seems to have changed the exception format in the
55 # final release. This patch should be included in the next version and can be removed when
56 # it is released.
57 (fetchpatch {
58 url = "https://foss.heptapod.net/pypy/cffi/-/commit/8a3c2c816d789639b49d3ae867213393ed7abdff.diff";
59 hash = "sha256-3wpZeBqN4D8IP+47QDGK7qh/9Z0Ag4lAe+H0R5xCb1E=";
60 })
61 ];
62
63 postPatch = lib.optionalString stdenv.isDarwin ''
64 # Remove setup.py impurities
65 substituteInPlace setup.py \
66 --replace "'-iwithsysroot/usr/include/ffi'" "" \
67 --replace "'/usr/include/ffi'," "" \
68 --replace '/usr/include/libffi' '${lib.getDev libffi}/include'
69 '';
70
71 buildInputs = [ libffi ];
72
73 nativeBuildInputs = [ pkg-config ];
74
75 propagatedBuildInputs = [ pycparser ];
76
77 # The tests use -Werror but with python3.6 clang detects some unreachable code.
78 env.NIX_CFLAGS_COMPILE = lib.optionalString stdenv.cc.isClang
79 "-Wno-unused-command-line-argument -Wno-unreachable-code -Wno-c++11-narrowing";
80
81 doCheck = !stdenv.hostPlatform.isMusl;
82
83 nativeCheckInputs = [ pytestCheckHook ];
84
85 disabledTests = lib.optionals stdenv.isDarwin [
86 # AssertionError: cannot seem to get an int[10] not completely cleared
87 # https://foss.heptapod.net/pypy/cffi/-/issues/556
88 "test_ffi_new_allocator_1"
89 ];
90
91 meta = with lib; {
92 maintainers = with maintainers; [ domenkozar lnl7 ];
93 homepage = "https://cffi.readthedocs.org/";
94 license = licenses.mit;
95 description = "Foreign Function Interface for Python calling C code";
96 };
97}