1{
2 stdenv,
3 lib,
4 buildPythonPackage,
5 pythonOlder,
6 fetchFromGitHub,
7 cmake,
8 ninja,
9 setuptools,
10 boost,
11 eigen,
12 python,
13 catch,
14 numpy,
15 pytestCheckHook,
16 libxcrypt,
17 makeSetupHook,
18}:
19let
20 setupHook = makeSetupHook {
21 name = "pybind11-setup-hook";
22 substitutions = {
23 out = placeholder "out";
24 pythonInterpreter = python.pythonOnBuildForHost.interpreter;
25 pythonIncludeDir = "${python}/include/python${python.pythonVersion}";
26 pythonSitePackages = "${python}/${python.sitePackages}";
27 };
28 } ./setup-hook.sh;
29
30 # clang 16 defaults to C++17, which results in the use of aligned allocations by pybind11.
31 # libc++ supports aligned allocations via `posix_memalign`, which is available since 10.6,
32 # but clang has a check hard-coded requiring 10.13 because that’s when Apple first shipped a
33 # support for C++17 aligned allocations on macOS.
34 # Tell clang we’re targeting 10.13 on x86_64-darwin while continuing to use the default SDK.
35 stdenv' =
36 if stdenv.isDarwin && stdenv.isx86_64 then
37 python.stdenv.override (oldStdenv: {
38 buildPlatform = oldStdenv.buildPlatform // {
39 darwinMinVersion = "10.13";
40 };
41 targetPlatform = oldStdenv.targetPlatform // {
42 darwinMinVersion = "10.13";
43 };
44 hostPlatform = oldStdenv.hostPlatform // {
45 darwinMinVersion = "10.13";
46 };
47 })
48 else
49 python.stdenv;
50in
51buildPythonPackage rec {
52 pname = "pybind11";
53 version = "2.12.0";
54 pyproject = true;
55
56 src = fetchFromGitHub {
57 owner = "pybind";
58 repo = "pybind11";
59 rev = "v${version}";
60 hash = "sha256-DVkI5NxM5uME9m3PFYVpJOOa2j+yjL6AJn76fCTv2nE=";
61 };
62
63 postPatch = ''
64 substituteInPlace pyproject.toml \
65 --replace-fail "timeout=300" ""
66 '';
67
68 build-system = [
69 cmake
70 ninja
71 setuptools
72 ];
73
74 buildInputs = lib.optionals (pythonOlder "3.9") [ libxcrypt ];
75 propagatedNativeBuildInputs = [ setupHook ];
76
77 stdenv = stdenv';
78
79 dontUseCmakeBuildDir = true;
80
81 # Don't build tests if not needed, read the doInstallCheck value at runtime
82 preConfigure = ''
83 if [ -n "$doInstallCheck" ]; then
84 cmakeFlagsArray+=("-DBUILD_TESTING=ON")
85 fi
86 '';
87
88 cmakeFlags = [
89 "-DBoost_INCLUDE_DIR=${lib.getDev boost}/include"
90 "-DEIGEN3_INCLUDE_DIR=${lib.getDev eigen}/include/eigen3"
91 ] ++ lib.optionals (python.isPy3k && !stdenv.cc.isClang) [ "-DPYBIND11_CXX_STANDARD=-std=c++17" ];
92
93 postBuild = ''
94 # build tests
95 make -j $NIX_BUILD_CORES
96 '';
97
98 postInstall = ''
99 make install
100 # Symlink the CMake-installed headers to the location expected by setuptools
101 mkdir -p $out/include/${python.libPrefix}
102 ln -sf $out/include/pybind11 $out/include/${python.libPrefix}/pybind11
103 '';
104
105 nativeCheckInputs = [
106 catch
107 numpy
108 pytestCheckHook
109 ];
110
111 disabledTestPaths = [
112 # require dependencies not available in nixpkgs
113 "tests/test_embed/test_trampoline.py"
114 "tests/test_embed/test_interpreter.py"
115 # numpy changed __repr__ output of numpy dtypes
116 "tests/test_numpy_dtypes.py"
117 # no need to test internal packaging
118 "tests/extra_python_package/test_files.py"
119 # tests that try to parse setuptools stdout
120 "tests/extra_setuptools/test_setuphelper.py"
121 ];
122
123 disabledTests = lib.optionals stdenv.isDarwin [
124 # expects KeyError, gets RuntimeError
125 # https://github.com/pybind/pybind11/issues/4243
126 "test_cross_module_exception_translator"
127 ];
128
129 hardeningDisable = lib.optional stdenv.hostPlatform.isMusl "fortify";
130
131 meta = with lib; {
132 homepage = "https://github.com/pybind/pybind11";
133 changelog = "https://github.com/pybind/pybind11/blob/${src.rev}/docs/changelog.rst";
134 description = "Seamless operability between C++11 and Python";
135 mainProgram = "pybind11-config";
136 longDescription = ''
137 Pybind11 is a lightweight header-only library that exposes
138 C++ types in Python and vice versa, mainly to create Python
139 bindings of existing C++ code.
140 '';
141 license = licenses.bsd3;
142 maintainers = with maintainers; [
143 yuriaisaka
144 dotlambda
145 ];
146 };
147}