1{ lib
2, stdenv
3, buildPythonPackage
4, isPyPy
5, fetchPypi
6, setuptools
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.16.0";
17 pyproject = true;
18
19 src = fetchPypi {
20 inherit pname version;
21 hash = "sha256-vLPvQ+WGZbvaL7GYaY/K5ndkg+DEpjGqVkeAbCXgLMA=";
22 };
23
24 patches = [
25 #
26 # Trusts the libffi library inside of nixpkgs on Apple devices.
27 #
28 # Based on some analysis I did:
29 #
30 # https://groups.google.com/g/python-cffi/c/xU0Usa8dvhk
31 #
32 # I believe that libffi already contains the code from Apple's fork that is
33 # deemed safe to trust in cffi.
34 #
35 ./darwin-use-libffi-closures.diff
36 ] ++ lib.optionals (stdenv.cc.isClang && lib.versionAtLeast (lib.getVersion stdenv.cc) "13") [
37 # -Wnull-pointer-subtraction is enabled with -Wextra. Suppress it to allow the following tests
38 # to run and pass when cffi is built with newer versions of clang:
39 # - testing/cffi1/test_verify1.py::test_enum_usage
40 # - testing/cffi1/test_verify1.py::test_named_pointer_as_argument
41 ./clang-pointer-substraction-warning.diff
42 ];
43
44 postPatch = lib.optionalString stdenv.isDarwin ''
45 # Remove setup.py impurities
46 substituteInPlace setup.py \
47 --replace "'-iwithsysroot/usr/include/ffi'" "" \
48 --replace "'/usr/include/ffi'," "" \
49 --replace '/usr/include/libffi' '${lib.getDev libffi}/include'
50 '';
51
52 nativeBuildInputs = [
53 pkg-config
54 setuptools
55 ];
56
57 buildInputs = [
58 libffi
59 ];
60
61 propagatedBuildInputs = [
62 pycparser
63 ];
64
65 # The tests use -Werror but with python3.6 clang detects some unreachable code.
66 env.NIX_CFLAGS_COMPILE = lib.optionalString stdenv.cc.isClang
67 "-Wno-unused-command-line-argument -Wno-unreachable-code -Wno-c++11-narrowing";
68
69 doCheck = !stdenv.hostPlatform.isMusl;
70
71 nativeCheckInputs = [
72 pytestCheckHook
73 ];
74
75 meta = with lib; {
76 changelog = "https://github.com/python-cffi/cffi/releases/tag/v${version}";
77 description = "Foreign Function Interface for Python calling C code";
78 downloadPage = "https://github.com/python-cffi/cffi";
79 homepage = "https://cffi.readthedocs.org/";
80 license = licenses.mit;
81 maintainers = teams.python.members;
82 };
83}