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 (stdenv.cc.isClang && lib.versionAtLeast (lib.getVersion stdenv.cc) "13") [
54 # -Wnull-pointer-subtraction is enabled with -Wextra. Suppress it to allow the following tests
55 # to run and pass when cffi is built with newer versions of clang:
56 # - testing/cffi1/test_verify1.py::test_enum_usage
57 # - testing/cffi1/test_verify1.py::test_named_pointer_as_argument
58 ./clang-pointer-substraction-warning.diff
59 ] ++ lib.optionals (pythonAtLeast "3.11") [
60 # Fix test that failed because python seems to have changed the exception format in the
61 # final release. This patch should be included in the next version and can be removed when
62 # it is released.
63 (fetchpatch {
64 url = "https://foss.heptapod.net/pypy/cffi/-/commit/8a3c2c816d789639b49d3ae867213393ed7abdff.diff";
65 hash = "sha256-3wpZeBqN4D8IP+47QDGK7qh/9Z0Ag4lAe+H0R5xCb1E=";
66 })
67 ];
68
69 postPatch = lib.optionalString stdenv.isDarwin ''
70 # Remove setup.py impurities
71 substituteInPlace setup.py \
72 --replace "'-iwithsysroot/usr/include/ffi'" "" \
73 --replace "'/usr/include/ffi'," "" \
74 --replace '/usr/include/libffi' '${lib.getDev libffi}/include'
75 '';
76
77 buildInputs = [ libffi ];
78
79 nativeBuildInputs = [ pkg-config ];
80
81 propagatedBuildInputs = [ pycparser ];
82
83 # The tests use -Werror but with python3.6 clang detects some unreachable code.
84 env.NIX_CFLAGS_COMPILE = lib.optionalString stdenv.cc.isClang
85 "-Wno-unused-command-line-argument -Wno-unreachable-code -Wno-c++11-narrowing";
86
87 doCheck = !stdenv.hostPlatform.isMusl;
88
89 nativeCheckInputs = [ pytestCheckHook ];
90
91 disabledTests = lib.optionals stdenv.isDarwin [
92 # AssertionError: cannot seem to get an int[10] not completely cleared
93 # https://foss.heptapod.net/pypy/cffi/-/issues/556
94 "test_ffi_new_allocator_1"
95 ];
96
97 meta = with lib; {
98 maintainers = with maintainers; [ domenkozar lnl7 ];
99 homepage = "https://cffi.readthedocs.org/";
100 license = licenses.mit;
101 description = "Foreign Function Interface for Python calling C code";
102 };
103}