]> sigrok.org Git - libsigrok.git/blob - bindings/python/setup.py
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / bindings / python / setup.py
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.
10 ##
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.
15 ##
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/>.
18 ##
19
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
28
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, '..'))
33
34 # Override the default compile flags used by distutils.
35 os.environ['OPT'] = ''
36
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]
45
46 includes = ['../../include', '../cxx/include']
47 includes += [os.path.normpath(os.path.join(srcdir, path)) for path in includes]
48 includes += ['../..', np.get_include()]
49
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']
54
55 def vpath(file):
56     vfile = os.path.join(srcdir, file)
57     return vfile if os.path.exists(vfile) else file
58
59 def unvpath(file):
60     return os.path.relpath(file, srcdir) if file.startswith(srcdir) else file
61
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))
70
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)]
83
84 setup(
85     name = 'libsigrok',
86     namespace_packages = ['sigrok'],
87     packages = find_packages(srcdir),
88     version = os.environ.get('VERSION'),
89     description = "libsigrok API wrapper",
90     zip_safe = False,
91     ext_modules = [
92         Extension('sigrok.core._classes',
93             sources = [vpath('sigrok/core/classes.i')],
94             extra_compile_args = ['-Wno-uninitialized'],
95             include_dirs = includes,
96             library_dirs = libdirs,
97             libraries = libs)
98     ],
99     cmdclass = {'build_py': build_py, 'build_ext': build_ext},
100 )