configure: Also test for new "check" option
[libsigrok.git] / bindings / python / setup.py
blob8542232736c5e8bfa157d5dde24ee9d261a1bc4c
1 ##
2 ## This file is part of the libsigrok project.
3 ##
4 ## Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li>
5 ##
6 ## This program is free software: you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation, either version 3 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from setuptools import setup, find_packages, Extension
21 from distutils.command.build_py import build_py as _build_py
22 from distutils.command.build_ext import build_ext as _build_ext
23 import numpy as np
24 import os
25 import sys
26 import re
27 import shlex
29 srcdir = os.path.dirname(os.path.abspath(__file__))
30 os.chdir('bindings/python')
31 srcdir = os.path.relpath(srcdir)
32 srcdir_parent = os.path.normpath(os.path.join(srcdir, '..'))
34 # Override the default compile flags used by distutils.
35 os.environ['OPT'] = ''
37 # Parse the command line arguments for VAR=value assignments,
38 # and apply them as environment variables.
39 while len(sys.argv) > 1:
40 match = re.match(r'([A-Z]+)=(.*)', sys.argv[1])
41 if match is None:
42 break
43 os.environ[match.group(1)] = match.group(2)
44 del sys.argv[1]
46 includes = ['../../include', '../cxx/include']
47 includes += [os.path.normpath(os.path.join(srcdir, path)) for path in includes]
48 includes += ['../..', np.get_include()]
50 ldadd = shlex.split(os.environ.get('LDADD', ''))
51 libdirs = ['../../.libs', '../cxx/.libs'] + \
52 [l[2:] for l in ldadd if l.startswith('-L')]
53 libs = [l[2:] for l in ldadd if l.startswith('-l')] + ['sigrokcxx']
55 def vpath(file):
56 vfile = os.path.join(srcdir, file)
57 return vfile if os.path.exists(vfile) else file
59 def unvpath(file):
60 return os.path.relpath(file, srcdir) if file.startswith(srcdir) else file
62 class build_py(_build_py):
63 def find_package_modules(self, package, pkg_dir):
64 mods = _build_py.find_package_modules(self, package, pkg_dir)
65 vmods = _build_py.find_package_modules(self, package, vpath(pkg_dir))
66 mods.extend([mod for mod in vmods if mod not in mods])
67 return mods
68 def check_package(self, package, package_dir):
69 return _build_py.check_package(self, package, vpath(package_dir))
71 class build_ext(_build_ext):
72 def finalize_options(self):
73 _build_ext.finalize_options(self)
74 self.swig_opts = ['-c++', '-threads', '-Isigrok/core', '-I..',
75 '-I' + srcdir_parent] + ['-I%s' % i for i in includes] + self.swig_opts
76 def spawn (self, cmd):
77 cmd[1:-1] = [arg if arg.startswith('-') else unvpath(arg) for arg in
78 cmd[1:-1]]
79 _build_ext.spawn(self, cmd)
80 def swig_sources (self, sources, extension):
81 return [unvpath(src) for src in
82 _build_ext.swig_sources(self, sources, extension)]
84 setup(
85 name = 'libsigrok',
86 packages = find_packages(srcdir),
87 version = os.environ.get('VERSION'),
88 description = "libsigrok API wrapper",
89 zip_safe = False,
90 ext_modules = [
91 Extension('sigrok.core._classes',
92 sources = [vpath('sigrok/core/classes.i')],
93 extra_compile_args = ['-Wno-uninitialized'],
94 include_dirs = includes,
95 library_dirs = libdirs,
96 libraries = libs)
98 cmdclass = {'build_py': build_py, 'build_ext': build_ext},