This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsetup.py
192 lines (155 loc) · 5.87 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#
# Much of this copied from https://github.com/pybind/python_example.git
#
import sys
if sys.version_info[0] < 3:
sys.stderr.write("ERROR: robotpy-pathfinder requires python 3!")
exit(1)
import fnmatch
import os
from os.path import dirname, exists, join
from setuptools import find_packages, setup, Extension
from setuptools.command.build_ext import build_ext
import subprocess
import sys
import setuptools
setup_dir = dirname(__file__)
git_dir = join(setup_dir, ".git")
base_package = "pathfinder"
version_file = join(setup_dir, base_package, "version.py")
# Automatically generate a version.py based on the git version
if exists(git_dir):
p = subprocess.Popen(
["git", "describe", "--tags", "--long", "--dirty=-dirty"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
out, err = p.communicate()
# Make sure the git version has at least one tag
if err:
print("Error: You need to create a tag for this repo to use the builder")
sys.exit(1)
# Convert git version to PEP440 compliant version
# - Older versions of pip choke on local identifiers, so we can't include the git commit
v, commits, local = out.decode("utf-8").rstrip().split("-", 2)
if commits != "0" or "-dirty" in local:
v = "%s.post0.dev%s" % (v, commits)
# Create the version.py file
with open(version_file, "w") as fp:
fp.write("# Autogenerated by setup.py\n__version__ = '{0}'".format(v))
if exists(version_file):
with open(join(setup_dir, base_package, "version.py"), "r") as fp:
exec(fp.read(), globals())
else:
__version__ = "master"
with open(join(setup_dir, "README.rst"), "r") as readme_file:
long_description = readme_file.read()
#
# pybind-specific compilation stuff
#
class get_pybind_include(object):
"""Helper class to determine the pybind11 include path
The purpose of this class is to postpone importing pybind11
until it is actually installed, so that the ``get_include()``
method can be invoked. """
def __init__(self, user=False):
self.user = user
def __str__(self):
import pybind11
return pybind11.get_include(self.user)
# As of Python 3.6, CCompiler has a `has_flag` method.
# cf http://bugs.python.org/issue26689
def has_flag(compiler, flagname):
"""Return a boolean indicating whether a flag name is supported on
the specified compiler.
"""
import tempfile
with tempfile.NamedTemporaryFile("w", suffix=".cpp") as f:
f.write("int main (int argc, char **argv) { return 0; }")
try:
compiler.compile([f.name], extra_postargs=[flagname])
except setuptools.distutils.errors.CompileError:
return False
return True
def cpp_flag(compiler):
"""Return the -std=c++[11/14] compiler flag.
The c++14 is preferred over c++11 (when it is available).
"""
if has_flag(compiler, "-std=c++14"):
return "-std=c++14"
elif has_flag(compiler, "-std=c++11"):
return "-std=c++11"
else:
raise RuntimeError(
"Unsupported compiler -- at least C++11 support " "is needed!"
)
class BuildExt(build_ext):
"""A custom build extension for adding compiler-specific options."""
c_opts = {"msvc": ["/EHsc"], "unix": []}
if sys.platform == "darwin":
c_opts["unix"] += ["-stdlib=libc++", "-mmacosx-version-min=10.7"]
def build_extensions(self):
ct = self.compiler.compiler_type
opts = self.c_opts.get(ct, [])
if ct == "unix":
opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version())
# TODO: this feels like a hack
if not os.environ.get("RPY_DEBUG"):
opts.append("-s") # strip
opts.append("-g0") # remove debug symbols
else:
opts.append("-O0")
c11arg = cpp_flag(self.compiler)
opts.append(c11arg)
if has_flag(self.compiler, "-fvisibility=hidden"):
opts.append("-fvisibility=hidden")
# HACK: clang doesn't allow compiling .c files with the -std=c++11
# flag... so remove it
if sys.platform == "darwin":
_orig_compile = self.compiler._compile
def _compile(obj, src, ext, cc_args, extra_postargs, pp_opts):
if src.endswith(".c"):
extra_postargs = extra_postargs[:]
extra_postargs.remove(c11arg)
return _orig_compile(
obj, src, ext, cc_args, extra_postargs, pp_opts
)
self.compiler._compile = _compile
elif ct == "msvc":
opts.append('/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version())
for ext in self.extensions:
ext.extra_compile_args = opts
build_ext.build_extensions(self)
def recursive_glob(d):
for root, _, filenames in os.walk(d):
for fname in fnmatch.filter(filenames, "*.c"):
yield join(root, fname)
ext_modules = [
Extension(
"pathfinder._pathfinder",
["pathfinder/_pathfinder.cpp"]
+ list(recursive_glob("pathfinder_src/Pathfinder-Core/src")),
include_dirs=[
# Path to pybind11 headers
get_pybind_include(),
get_pybind_include(user=True),
"pathfinder_src/Pathfinder-Core/include",
],
language="c++",
)
]
setup(
name="robotpy-pathfinder",
version=__version__,
author="Dustin Spicuzza",
author_email="dustin@virtualroadside.com",
url="https://github.com/robotpy/robotpy-pathfinder",
description="RobotPy bindings for Jaci R's PathFinder library",
long_description=long_description,
packages=find_packages(),
ext_modules=ext_modules,
setup_requires=["pybind11>=2.2"],
license="MIT",
zip_safe=False,
cmdclass={"build_ext": BuildExt},
)