Skip to content

Commit e8bf04e

Browse files
committed
Python: Allow source dists to install correctly
This uses PEP 517 which causes pybind11 to be installed in a temporary location at build time. https://martin-thoma.com/pyproject-toml/ Unfortunately when pybind11 is installed there, it produces the wrong include path, so this inclludes a workaround. Hopefully when that issue is fixed the workaround can be removed: pybind/pybind11#1067 I’ve also posted the workaround here: pybind/python_example#45
1 parent ef5bb4c commit e8bf04e

File tree

5 files changed

+88
-24
lines changed

5 files changed

+88
-24
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
#Common folder for building objects
22
build/
3+
/python/dist/
4+
/python/*.egg-info
5+
/python/.eggs
6+
/python/tiny_obj_loader.h

python/MANIFEST.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copy the header file into the python/ folder.
2+
include ../tiny_obj_loader.h
3+
# Include it in the source distribution.
4+
include tiny_obj_loader.h
5+
6+
include pyproject.toml

python/bindings.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include "pybind11/pybind11.h"
2-
#include "pybind11/stl.h"
3-
#include "pybind11/numpy.h"
1+
#include <pybind11/pybind11.h>
2+
#include <pybind11/stl.h>
3+
#include <pybind11/numpy.h>
44
#include <cstring>
55

66
// Use double precision for better python integration.

python/pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build-system]
2+
requires = ["setuptools", "wheel", "pybind11>=2.3"]

python/setup.py

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,80 @@
11
import setuptools
22

33
with open("README.md", "r") as fh:
4-
long_description= fh.read()
4+
long_description = fh.read()
5+
6+
# Adapted from https://github.com/pybind/python_example/blob/master/setup.py
7+
class get_pybind_include(object):
8+
"""Helper class to determine the pybind11 include path
9+
The purpose of this class is to postpone importing pybind11
10+
until it is actually installed, so that the ``get_include()``
11+
method can be invoked. """
12+
13+
def __init__(self, user=False, pep517=False):
14+
self.user = user
15+
self.pep517 = pep517
16+
17+
def __str__(self):
18+
import os
19+
import pybind11
20+
21+
interpreter_include_path = pybind11.get_include(self.user)
22+
23+
if self.pep517:
24+
# When pybind11 is installed permanently in site packages, the headers
25+
# will be in the interpreter include path above. PEP 517 provides an
26+
# experimental feature for build system dependencies. When installing
27+
# a package from a source distribvution, first its build dependencies
28+
# are installed in a temporary location. pybind11 does not return the
29+
# correct path for this condition, so we glom together a second path,
30+
# and ultimately specify them _both_ in the include search path.
31+
# https://github.com/pybind/pybind11/issues/1067
32+
return os.path.abspath(
33+
os.path.join(
34+
os.path.dirname(pybind11.__file__),
35+
"..",
36+
"..",
37+
"..",
38+
"..",
39+
"include",
40+
os.path.basename(interpreter_include_path),
41+
)
42+
)
43+
else:
44+
return interpreter_include_path
45+
546

647
# `tiny_obj_loader.cc` contains implementation of tiny_obj_loader.
7-
m = setuptools.Extension('tinyobjloader',
8-
sources = ['bindings.cc', 'tiny_obj_loader.cc'],
9-
extra_compile_args=['-std=c++11'],
10-
include_dirs = ['../', '../pybind11/include']
11-
)
12-
13-
14-
setuptools.setup (name = 'tinyobjloader',
15-
version = '0.1',
16-
description = 'Python module for tinyobjloader',
17-
long_description = long_description,
18-
long_description_content_type = "text/markdown",
19-
author="Syoyo Fujita",
20-
author_email="[email protected]",
21-
url="https://github.com/syoyo/tinyobjloader",
22-
classifiers=[
23-
"License :: OSI Approved :: MIT License",
24-
],
25-
packages=setuptools.find_packages(),
26-
ext_modules = [m])
48+
m = setuptools.Extension(
49+
"tinyobjloader",
50+
extra_compile_args=["-std=c++11"],
51+
sources=["bindings.cc", "tiny_obj_loader.cc"],
52+
include_dirs=[
53+
# Support `build_ext` finding tinyobjloader (without first running
54+
# `sdist`).
55+
"..",
56+
# Support `build_ext` finding pybind 11 (provided it's permanently
57+
# installed).
58+
get_pybind_include(),
59+
get_pybind_include(user=True),
60+
# Support building from a source distribution finding pybind11 from
61+
# a PEP 517 temporary install.
62+
get_pybind_include(pep517=True),
63+
],
64+
language="c++",
65+
)
2766

2867

68+
setuptools.setup(
69+
name="tinymetabobjloader",
70+
version="0.1.0",
71+
description="Experimental fork of tinyobjloader Python module",
72+
long_description=long_description,
73+
long_description_content_type="text/markdown",
74+
author="Syoyo Fujita, Paul Melnikow",
75+
76+
url="https://github.com/metabolize/tinyobjloader",
77+
classifiers=["License :: OSI Approved :: MIT License"],
78+
packages=setuptools.find_packages(),
79+
ext_modules=[m],
80+
)

0 commit comments

Comments
 (0)