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.pythonForBuild.interpreter;
21 pythonIncludeDir = "${python}/include/python${python.pythonVersion}";
22 pythonSitePackages = "${python}/${python.sitePackages}";
23 };
24 } ./setup-hook.sh;
25in buildPythonPackage rec {
26 pname = "pybind11";
27 version = "2.11.1";
28
29 src = fetchFromGitHub {
30 owner = "pybind";
31 repo = pname;
32 rev = "v${version}";
33 hash = "sha256-sO/Fa+QrAKyq2EYyYMcjPrYI+bdJIrDoj6L3JHoDo3E=";
34 };
35
36 postPatch = ''
37 sed -i "/^timeout/d" pyproject.toml
38 '';
39
40 nativeBuildInputs = [ cmake ];
41 buildInputs = lib.optionals (pythonOlder "3.9") [ libxcrypt ];
42 propagatedBuildInputs = [ setupHook ];
43
44 dontUseCmakeBuildDir = true;
45
46 # Don't build tests if not needed, read the doInstallCheck value at runtime
47 preConfigure = ''
48 if [ -n "$doInstallCheck" ]; then
49 cmakeFlagsArray+=("-DBUILD_TESTING=ON")
50 fi
51 '';
52
53 cmakeFlags = [
54 "-DBoost_INCLUDE_DIR=${lib.getDev boost}/include"
55 "-DEIGEN3_INCLUDE_DIR=${lib.getDev eigen}/include/eigen3"
56 ] ++ lib.optionals (python.isPy3k && !stdenv.cc.isClang) [
57 "-DPYBIND11_CXX_STANDARD=-std=c++17"
58 ];
59
60 postBuild = ''
61 # build tests
62 make -j $NIX_BUILD_CORES
63 '';
64
65 postInstall = ''
66 make install
67 # Symlink the CMake-installed headers to the location expected by setuptools
68 mkdir -p $out/include/${python.libPrefix}
69 ln -sf $out/include/pybind11 $out/include/${python.libPrefix}/pybind11
70 '';
71
72 nativeCheckInputs = [
73 catch
74 numpy
75 pytestCheckHook
76 ];
77
78 disabledTestPaths = [
79 # require dependencies not available in nixpkgs
80 "tests/test_embed/test_trampoline.py"
81 "tests/test_embed/test_interpreter.py"
82 # numpy changed __repr__ output of numpy dtypes
83 "tests/test_numpy_dtypes.py"
84 # no need to test internal packaging
85 "tests/extra_python_package/test_files.py"
86 # tests that try to parse setuptools stdout
87 "tests/extra_setuptools/test_setuphelper.py"
88 ];
89
90 disabledTests = lib.optionals stdenv.isDarwin [
91 # expects KeyError, gets RuntimeError
92 # https://github.com/pybind/pybind11/issues/4243
93 "test_cross_module_exception_translator"
94 ];
95
96 hardeningDisable = lib.optional stdenv.hostPlatform.isMusl "fortify";
97
98 meta = with lib; {
99 homepage = "https://github.com/pybind/pybind11";
100 changelog = "https://github.com/pybind/pybind11/blob/${src.rev}/docs/changelog.rst";
101 description = "Seamless operability between C++11 and Python";
102 longDescription = ''
103 Pybind11 is a lightweight header-only library that exposes
104 C++ types in Python and vice versa, mainly to create Python
105 bindings of existing C++ code.
106 '';
107 license = licenses.bsd3;
108 maintainers = with maintainers; [ yuriaisaka dotlambda ];
109 };
110}