Skip to content

Commit 3735249

Browse files
isurufwjakob
authored andcommitted
Install headers using both headers and package_data (#1995)
1 parent a606482 commit 3735249

File tree

3 files changed

+48
-59
lines changed

3 files changed

+48
-59
lines changed

pybind11/__init__.py

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,11 @@
22

33

44
def get_include(user=False):
5-
from distutils.dist import Distribution
65
import os
7-
import sys
8-
9-
# Are we running in a virtual environment?
10-
virtualenv = hasattr(sys, 'real_prefix') or \
11-
sys.prefix != getattr(sys, "base_prefix", sys.prefix)
12-
13-
# Are we running in a conda environment?
14-
conda = os.path.exists(os.path.join(sys.prefix, 'conda-meta'))
15-
16-
if virtualenv:
17-
return os.path.join(sys.prefix, 'include', 'site',
18-
'python' + sys.version[:3])
19-
elif conda:
20-
if os.name == 'nt':
21-
return os.path.join(sys.prefix, 'Library', 'include')
22-
else:
23-
return os.path.join(sys.prefix, 'include')
6+
d = os.path.dirname(__file__)
7+
if os.path.exists(os.path.join(d, "include")):
8+
# Package is installed
9+
return os.path.join(d, "include")
2410
else:
25-
dist = Distribution({'name': 'pybind11'})
26-
dist.parse_config_files()
27-
28-
dist_cobj = dist.get_command_obj('install', create=True)
29-
30-
# Search for packages in user's home directory?
31-
if user:
32-
dist_cobj.user = user
33-
dist_cobj.prefix = ""
34-
dist_cobj.finalize_options()
35-
36-
return os.path.dirname(dist_cobj.install_headers)
11+
# Package is from a source directory
12+
return os.path.join(os.path.dirname(d), "include")

pybind11/__main__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
def print_includes():
1111
dirs = [sysconfig.get_path('include'),
1212
sysconfig.get_path('platinclude'),
13-
get_include(),
14-
get_include(True)]
13+
get_include()]
1514

1615
# Make unique but preserve order
1716
unique_dirs = []

setup.py

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,43 @@
44

55
from setuptools import setup
66
from distutils.command.install_headers import install_headers
7+
from distutils.command.build_py import build_py
78
from pybind11 import __version__
89
import os
910

11+
package_data = [
12+
'include/pybind11/detail/class.h',
13+
'include/pybind11/detail/common.h',
14+
'include/pybind11/detail/descr.h',
15+
'include/pybind11/detail/init.h',
16+
'include/pybind11/detail/internals.h',
17+
'include/pybind11/detail/typeid.h',
18+
'include/pybind11/attr.h',
19+
'include/pybind11/buffer_info.h',
20+
'include/pybind11/cast.h',
21+
'include/pybind11/chrono.h',
22+
'include/pybind11/common.h',
23+
'include/pybind11/complex.h',
24+
'include/pybind11/eigen.h',
25+
'include/pybind11/embed.h',
26+
'include/pybind11/eval.h',
27+
'include/pybind11/functional.h',
28+
'include/pybind11/iostream.h',
29+
'include/pybind11/numpy.h',
30+
'include/pybind11/operators.h',
31+
'include/pybind11/options.h',
32+
'include/pybind11/pybind11.h',
33+
'include/pybind11/pytypes.h',
34+
'include/pybind11/stl.h',
35+
'include/pybind11/stl_bind.h',
36+
]
37+
1038
# Prevent installation of pybind11 headers by setting
1139
# PYBIND11_USE_CMAKE.
1240
if os.environ.get('PYBIND11_USE_CMAKE'):
1341
headers = []
1442
else:
15-
headers = [
16-
'include/pybind11/detail/class.h',
17-
'include/pybind11/detail/common.h',
18-
'include/pybind11/detail/descr.h',
19-
'include/pybind11/detail/init.h',
20-
'include/pybind11/detail/internals.h',
21-
'include/pybind11/detail/typeid.h',
22-
'include/pybind11/attr.h',
23-
'include/pybind11/buffer_info.h',
24-
'include/pybind11/cast.h',
25-
'include/pybind11/chrono.h',
26-
'include/pybind11/common.h',
27-
'include/pybind11/complex.h',
28-
'include/pybind11/eigen.h',
29-
'include/pybind11/embed.h',
30-
'include/pybind11/eval.h',
31-
'include/pybind11/functional.h',
32-
'include/pybind11/iostream.h',
33-
'include/pybind11/numpy.h',
34-
'include/pybind11/operators.h',
35-
'include/pybind11/options.h',
36-
'include/pybind11/pybind11.h',
37-
'include/pybind11/pytypes.h',
38-
'include/pybind11/stl.h',
39-
'include/pybind11/stl_bind.h',
40-
]
43+
headers = package_data
4144

4245

4346
class InstallHeaders(install_headers):
@@ -55,6 +58,16 @@ def run(self):
5558
self.outfiles.append(out)
5659

5760

61+
# Install the headers inside the package as well
62+
class BuildPy(build_py):
63+
def build_package_data(self):
64+
build_py.build_package_data(self)
65+
for header in package_data:
66+
target = os.path.join(self.build_lib, 'pybind11', header)
67+
self.mkpath(os.path.dirname(target))
68+
self.copy_file(header, target, preserve_mode=False)
69+
70+
5871
setup(
5972
name='pybind11',
6073
version=__version__,
@@ -66,7 +79,8 @@ def run(self):
6679
packages=['pybind11'],
6780
license='BSD',
6881
headers=headers,
69-
cmdclass=dict(install_headers=InstallHeaders),
82+
zip_safe=False,
83+
cmdclass=dict(install_headers=InstallHeaders, build_py=BuildPy),
7084
classifiers=[
7185
'Development Status :: 5 - Production/Stable',
7286
'Intended Audience :: Developers',

0 commit comments

Comments
 (0)