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