]> sigrok.org Git - libsigrok.git/blob - bindings/python/setup.py
Build: Pass compiler flags from make to setup.py
[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 spawn (self, cmd):
73         cmd[1:-1] = [arg if arg.startswith('-') else unvpath(arg) for arg in
74                      cmd[1:-1]]
75         _build_ext.spawn(self, cmd)
76     def swig_sources (self, sources, extension):
77         return [unvpath(src) for src in
78                 _build_ext.swig_sources(self, sources, extension)]
79
80 setup(
81     name = 'libsigrok',
82     namespace_packages = ['sigrok'],
83     packages = find_packages(srcdir),
84     version = os.environ.get('VERSION'),
85     description = "libsigrok API wrapper",
86     zip_safe = False,
87     ext_modules = [
88         Extension('sigrok.core._classes',
89             sources = [vpath('sigrok/core/classes.i')],
90             swig_opts = ['-c++', '-threads', '-Isigrok/core', '-I..', '-I' + srcdir_parent] +
91                 ['-I%s' % i for i in includes],
92             extra_compile_args = ['-Wno-uninitialized'],
93             include_dirs = includes,
94             library_dirs = libdirs,
95             libraries = libs)
96     ],
97     cmdclass = {'build_py': build_py, 'build_ext': build_ext},
98 )