This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
forked from robotpy/robotpy-rev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
377 lines (301 loc) · 11.8 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#
# Much of this copied from https://github.com/pybind/python_example.git
#
import os
from os.path import dirname, exists, join
from setuptools import find_packages, setup, Extension
from setuptools.command.build_ext import build_ext
from setuptools.command.sdist import sdist
import shutil
import subprocess
import sys
import setuptools
rev_lib_version = "1.5.1"
setup_dir = dirname(__file__)
git_dir = join(setup_dir, ".git")
base_package = "rev"
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))
fp.write("\n__rev_version__ = '%s'" % rev_lib_version)
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++14 compiler flag.
This ensures that the compiler actually supports C++14.
Attempts to give a friendly error message otherwise.
"""
if has_flag(compiler, "-std=c++14"):
return "-std=c++14"
raise RuntimeError("Unsupported compiler -- at least C++14 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"' % rev_lib_version)
opts.append("-s") # strip
opts.append("-g0") # remove debug symbols
opts.append(cpp_flag(self.compiler))
if has_flag(self.compiler, "-fvisibility=hidden"):
opts.append("-fvisibility=hidden")
elif ct == "msvc":
opts.append('/DVERSION_INFO=\\"%s\\"' % rev_lib_version)
for ext in self.extensions:
ext.extra_compile_args = opts
build_ext.build_extensions(self)
install_requires = ["wpilib>=2020.0.0,<2021.0.0"]
class Downloader:
"""
Utility object to allow lazily retrieving needed artifacts on demand,
instead of distributing several extra MB with the pypi build.
"""
def __init__(self):
self._hallib = None
self._halsrc = None
self._revsrc = None
self._wpiutillib = None
self._wpiutilsrc = None
rev_devdir = os.environ.get("RPY_REV_DEVDIR")
if rev_devdir:
# development use only -- preextracted files so it doesn't have
# to download it over and over again
# -> if the directory doesn't exist, it will download the current
# files to that directory
self._hallib = join(rev_devdir, "hallib")
self._halsrc = join(rev_devdir, "halsrc")
self._revsrc = join(rev_devdir, "rev")
self._wpiutillib = join(rev_devdir, "wpiutillib")
self._wpiutilsrc = join(rev_devdir, "wpiutilsrc")
# copy/paste from hal_impl.distutils
def _download(self, url):
import atexit
import posixpath
from urllib.request import urlretrieve, urlcleanup
import sys
print("Downloading", posixpath.basename(url))
def _reporthook(count, blocksize, totalsize):
percent = int(count * blocksize * 100 / totalsize)
sys.stdout.write("\r%02d%%" % percent)
sys.stdout.flush()
filename, _ = urlretrieve(url, reporthook=_reporthook)
atexit.register(urlcleanup)
return filename
def _download_and_extract_zip(self, url, to=None):
import atexit
import tempfile
if to is None:
# generate temporary directory
tod = tempfile.TemporaryDirectory()
to = tod.name
atexit.register(tod.cleanup)
zip_fname = self._download(url)
return self._extract_zip(zip_fname, to)
def _extract_zip(self, zip_fname, to):
import shutil
import zipfile
with zipfile.ZipFile(zip_fname) as z:
if isinstance(to, str):
z.extractall(to)
return to
else:
for src, dst in to.items():
with z.open(src, "r") as zfp:
with open(dst, "wb") as fp:
shutil.copyfileobj(zfp, fp)
@property
def hallib(self):
if not self._hallib or not exists(self._hallib):
import hal_impl.distutils
self._hallib = hal_impl.distutils.extract_hal_libs(to=self._hallib)
return self._hallib
@property
def halsrc(self):
if not self._halsrc or not exists(self._halsrc):
import hal_impl.distutils
self._halsrc = hal_impl.distutils.extract_hal_headers(to=self._halsrc)
return self._halsrc
@property
def wpiutillib(self):
if not self._wpiutillib or not exists(self._wpiutillib):
import hal_impl.distutils
self._wpiutillib = hal_impl.distutils.extract_wpiutil_libs(
to=self._wpiutillib
)
return self._wpiutillib
@property
def wpiutilsrc(self):
if not self._wpiutilsrc or not exists(self._wpiutilsrc):
import hal_impl.distutils
url = "%s/%s/%s" % (
hal_impl.distutils.wpiutil_site,
hal_impl.distutils.wpiutil_version,
"wpiutil-cpp-%s-headers.zip" % hal_impl.distutils.wpiutil_version,
)
self._wpiutilsrc = self._download_and_extract_zip(url, to=self._wpiutilsrc)
return self._wpiutilsrc
@property
def revsrc(self):
if not self._revsrc or not exists(self._revsrc):
# Download and extract three libs
base = "https://www.revrobotics.com/content/sw/max/sdk/maven/com/revrobotics/frc/"
dirs = [
"SparkMax-cpp/%(version)s/SparkMax-cpp-%(version)s-headers.zip",
"SparkMax-cpp/%(version)s/SparkMax-cpp-%(version)s-linuxathenastatic.zip",
"SparkMax-driver/%(version)s/SparkMax-driver-%(version)s-linuxathenastatic.zip",
]
for l in dirs:
url = base + (l % dict(version=rev_lib_version))
self._revsrc = self._download_and_extract_zip(url, to=self._revsrc)
return self._revsrc
get = Downloader()
_travis_build = os.environ.get("TRAVIS_BUILD")
# Detect roboRIO.. not foolproof, but good enough
if exists("/etc/natinst/share/scs_imagemetadata.ini") or _travis_build:
# Don't try to link when testing on travis, as it will fail
# -> We can still catch compile errors, which is good enough I suspect
if _travis_build:
libraries = None
else:
libraries = ["wpiHal", "SparkMax", "SparkMaxDriver"]
wpilibc = join(setup_dir, base_package, "_impl", "wpilibc")
ext_modules = [
Extension(
"rev._impl.rev_roborio",
["rev/_impl/rev_roborio.cpp"]
+ [
join(wpilibc, "frc", f)
for f in [
"CAN.cpp",
"DriverStation.cpp",
"Error.cpp",
"ErrorBase.cpp",
"RobotController.cpp",
"Timer.cpp",
"Utility.cpp",
]
],
include_dirs=[
# Path to pybind11 headers
get_pybind_include(),
get_pybind_include(user=True),
get.revsrc,
get.halsrc,
wpilibc,
join(get.halsrc),
join(get.wpiutilsrc),
],
libraries=libraries,
library_dirs=[
join(get.hallib, "linux", "athena", "shared"),
join(get.wpiutillib, "linux", "athena", "shared"),
join(get.revsrc, "linux", "athena", "static"),
],
language="c++",
)
]
# This doesn't actually work, as it needs to be installed before setup.py is ran
# ... but we specify it
# install_requires = ['pybind11>=1.7']
install_requires.append("robotpy-hal>=2020.0.0,<2021.0.0")
cmdclass = {"build_ext": BuildExt}
else:
install_requires.append("robotpy-halsim-gui>=2020.0.0,<2021.0.0")
ext_modules = None
cmdclass = {}
#
# Autogenerating the required REV files is something that
# is done at sdist time. This means if you are testing builds,
# you have to run 'setup.py sdist build'.
#
# The advantage of doing it this way is that the autogen files
# are distributed with the pypi package, so simulation users
# don't have to install anything special to build this
#
class SDist(sdist):
def run(self):
from header2whatever import batch_convert
# Do this before deleting the autogen directory, as it may fail
revsrc = get.revsrc
config_path = join(setup_dir, "gen", "gen.yml")
outdir = join(setup_dir, "rev", "_impl", "autogen")
shutil.rmtree(outdir, ignore_errors=True)
batch_convert(config_path, outdir, revsrc)
with open(join(outdir, "__init__.py"), "w"):
pass
super().run()
cmdclass["sdist"] = SDist
if os.environ.get("READTHEDOCS", None) == "True":
sys.argv.insert(1, "sdist")
setup(
name="robotpy-rev",
version=__version__,
author="Dustin Spicuzza",
author_email="dustin@virtualroadside.com",
url="https://github.com/robotpy/robotpy-rev",
description="RobotPy bindings for REV third party libraries",
long_description=long_description,
packages=find_packages(),
ext_modules=ext_modules,
install_requires=install_requires,
cmdclass=cmdclass,
zip_safe=False,
entry_points={
"robotpylib": ["info = rev._impl.info:Info"],
"robotpysim": ["rev = rev._impl.sim_ui:RevUI"],
},
)