python: support .pydistutils.cfg mode
[gpgme.git] / lang / python / setup.py.in
1 #!/usr/bin/env python
2
3 # Copyright (C) 2016-2017 g10 Code GmbH
4 # Copyright (C) 2004,2008 Igor Belyi <belyi@users.sourceforge.net>
5 # Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>
6 #
7 #    This library is free software; you can redistribute it and/or
8 #    modify it under the terms of the GNU Lesser General Public
9 #    License as published by the Free Software Foundation; either
10 #    version 2.1 of the License, or (at your option) any later version.
11 #
12 #    This library is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #    Lesser General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Lesser General Public
18 #    License along with this library; if not, write to the Free Software
19 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
20
21 from distutils.core import setup, Extension
22 import os, os.path, sys
23 import glob
24 import re
25 import shutil
26 import subprocess
27
28 # Out-of-tree build of the gpg bindings.
29 gpg_error_config = ["gpg-error-config"]
30 gpgme_config_flags = ["--thread=pthread"]
31 gpgme_config = ["gpgme-config"] + gpgme_config_flags
32 gpgme_h = ""
33 include_dirs = [os.getcwd()]
34 library_dirs = []
35 in_tree = False
36 extra_swig_opts = []
37 extra_macros = dict()
38
39 top_builddir = os.environ.get("top_builddir")
40 if top_builddir:
41     # In-tree build.
42     in_tree = True
43     gpgme_config = [os.path.join(top_builddir, "src/gpgme-config")] + gpgme_config_flags
44     gpgme_h = os.path.join(top_builddir, "src/gpgme.h")
45     library_dirs = [os.path.join(top_builddir, "src/.libs")] # XXX uses libtool internals
46     extra_macros.update(
47         HAVE_CONFIG_H=1,
48         HAVE_DATA_H=1,
49         IN_TREE_BUILD=1,
50     )
51
52 if hasattr(subprocess, "DEVNULL"):
53     devnull = subprocess.DEVNULL
54 else:
55     devnull = open(os.devnull, "w")
56
57 try:
58     subprocess.check_call(gpg_error_config + ['--version'],
59                           stdout=devnull)
60 except:
61     sys.exit("Could not find gpg-error-config.  " +
62              "Please install the libgpg-error development package.")
63
64 try:
65     subprocess.check_call(gpgme_config + ['--version'],
66                           stdout=devnull)
67 except:
68     sys.exit("Could not find gpgme-config.  " +
69              "Please install the libgpgme development package.")
70
71 def getconfig(what, config=gpgme_config):
72     confdata = subprocess.Popen(config + ["--%s" % what],
73                                 stdout=subprocess.PIPE).communicate()[0]
74     return [x for x in confdata.decode('utf-8').split() if x != '']
75
76 version = version_raw = getconfig("version")[0]
77 if '-' in version:
78     version = version.split('-')[0]
79 major, minor, patch = map(int, version.split('.'))
80
81 if not (major > 1 or (major == 1 and minor >= 7)):
82     sys.exit('Need at least GPGME version 1.7, found {}.'.format(version_raw))
83
84 if not gpgme_h:
85     gpgme_h = os.path.join(getconfig("prefix")[0], "include", "gpgme.h")
86
87 gpg_error_prefix = getconfig("prefix", config=gpg_error_config)[0]
88 gpg_error_h = os.path.join(gpg_error_prefix, "include", "gpg-error.h")
89 if not os.path.exists(gpg_error_h):
90     gpg_error_h = \
91         glob.glob(os.path.join(gpg_error_prefix, "include",
92                                "*", "gpg-error.h"))[0]
93
94 define_macros = []
95 libs = getconfig('libs')
96
97 # Define extra_macros for both the SWIG and C code
98 for k, v in extra_macros.items():
99     extra_swig_opts.append("-D{0}={1}".format(k, v))
100     define_macros.append((k, str(v)))
101
102 for item in getconfig('cflags'):
103     if item.startswith("-I"):
104         include_dirs.append(item[2:])
105     elif item.startswith("-D"):
106         defitem = item[2:].split("=", 1)
107         if len(defitem)==2:
108             define_macros.append((defitem[0], defitem[1]))
109         else:
110             define_macros.append((defitem[0], None))
111
112 # Adjust include and library locations in case of win32
113 uname_s = os.popen("uname -s").read()
114 if uname_s.startswith("MINGW32"):
115    mnts = [x.split()[0:3:2] for x in os.popen("mount").read().split("\n") if x]
116    tmplist = sorted([(len(x[1]), x[1], x[0]) for x in mnts])
117    tmplist.reverse()
118    extra_dirs = []
119    for item in include_dirs:
120        for ln, mnt, tgt in tmplist:
121            if item.startswith(mnt):
122                item = os.path.normpath(item[ln:])
123                while item[0] == os.path.sep:
124                    item = item[1:]
125                extra_dirs.append(os.path.join(tgt, item))
126                break
127    include_dirs += extra_dirs
128    for item in [x[2:] for x in libs if x.startswith("-L")]:
129        for ln, mnt, tgt in tmplist:
130            if item.startswith(mnt):
131                item = os.path.normpath(item[ln:])
132                while item[0] == os.path.sep:
133                    item = item[1:]
134                library_dirs.append(os.path.join(tgt, item))
135                break
136
137 def in_srcdir(name):
138     return os.path.join(os.environ.get("srcdir", ""), name)
139 def up_to_date(source, target):
140     return (os.path.exists(target)
141             and os.path.getmtime(source) <= os.path.getmtime(target))
142
143 # We build an Extension using SWIG, which generates a Python module.
144 # By default, the 'build_py' step is run before 'build_ext', and
145 # therefore the generated Python module is not copied into the build
146 # directory.
147 # Bug: http://bugs.python.org/issue1016626
148 # Workaround:
149 # http://stackoverflow.com/questions/12491328/python-distutils-not-include-the-swig-generated-module
150 from distutils.command.build import build
151 class BuildExtFirstHack(build):
152
153     def _generate_gpgme_h(self, source_name, sink_name):
154         if up_to_date(source_name, sink_name):
155             return
156
157         deprec_func = re.compile(r'^(.*typedef.*|.*\(.*\)|[^#]+\s+.+)'
158                                  + r'\s*_GPGME_DEPRECATED(_OUTSIDE_GPGME)?\(.*\);\s*',
159                                  re.S)
160         line_break = re.compile(';|\\$|\\x0c|^\s*#|{')
161
162         with open(sink_name, "w") as sink, open(source_name) as source:
163             text = ''
164             for line in source:
165                 text += re.sub(' class ', ' _py_obsolete_class ', line)
166                 if line_break.search(line):
167                     if not deprec_func.search(text):
168                         sink.write(text)
169                     text = ''
170             sink.write(text)
171
172     def _generate_errors_i(self, source_name, sink_name):
173         if up_to_date(source_name, sink_name):
174             return
175
176         filter_re = re.compile(r'GPG_ERR_[^ ]* =')
177         rewrite_re = re.compile(r' *(.*) = .*')
178
179         with open(sink_name, "w") as sink, open(source_name) as source:
180             for line in source:
181                 if not filter_re.search(line):
182                     continue
183                 sink.write(rewrite_re.sub(r'%constant long \1 = \1;'+'\n', line.strip()))
184
185     def _in_build_base(self, name):
186         return os.path.join(self.build_base, name)
187
188     def _generate(self):
189         print("Building python gpg module using {} and {}.".format(gpgme_h, gpg_error_h))
190
191         # Cleanup gpgme.h from deprecated functions and typedefs.
192         if not os.path.exists(self.build_base):
193             os.makedirs(self.build_base)
194
195         self._generate_gpgme_h(gpgme_h, self._in_build_base("gpgme.h"))
196         self._generate_errors_i(gpg_error_h, self._in_build_base("errors.i"))
197
198         # Keep timestamp to avoid rebuild
199         for source, target in ((gpgme_h, self._in_build_base("gpgme.h")),
200                                (gpg_error_h, self._in_build_base("errors.i"))):
201             if not up_to_date(source, target):
202                 shutil.copystat(source, target)
203
204         # Copy due to http://bugs.python.org/issue2624
205         # Avoid creating in srcdir
206         for source, target in ((in_srcdir(n), self._in_build_base(n))
207                                for n in ('gpgme.i', 'helpers.c', 'private.h', 'helpers.h')):
208             if not up_to_date(source, target):
209                 shutil.copy2(source, target)
210
211         # Append generated files via build_base
212         if not os.path.exists(os.path.join(self.build_lib, "gpg")):
213             os.makedirs(os.path.join(self.build_lib, "gpg"))
214         shutil.copy2("version.py", os.path.join(self.build_lib, "gpg"))
215
216     def run(self):
217         self._generate()
218
219         swig_sources.extend((self._in_build_base('gpgme.i'), self._in_build_base('helpers.c')))
220         swig_opts.extend(['-I' + self.build_base,
221                           '-outdir', os.path.join(self.build_lib, 'gpg')])
222         include_dirs.append(self.build_base)
223
224         self.run_command('build_ext')
225         build.run(self)
226
227 py3 = [] if sys.version_info.major < 3 else ['-py3']
228 swig_sources = []
229 swig_opts = ['-threads'] + py3 + extra_swig_opts
230 swige = Extension("gpg._gpgme",
231                   sources = swig_sources,
232                   swig_opts = swig_opts,
233                   include_dirs = include_dirs,
234                   define_macros = define_macros,
235                   library_dirs = library_dirs,
236                   extra_link_args = libs)
237
238 setup(name="gpg",
239       cmdclass={'build': BuildExtFirstHack},
240       version="@VERSION@",
241       description='Python bindings for GPGME GnuPG cryptography library',
242       # XXX add a long description
243       #long_description=long_description,
244       author='The GnuPG hackers',
245       author_email='gnupg-devel@gnupg.org',
246       url='https://www.gnupg.org',
247       ext_modules=[swige],
248       packages = ['gpg', 'gpg.constants', 'gpg.constants.data',
249                   'gpg.constants.keylist', 'gpg.constants.sig',
250                   'gpg.constants.tofu'],
251       license="LGPL2.1+ (the library), GPL2+ (tests and examples)",
252       classifiers=[
253           'Development Status :: 4 - Beta',
254           'Intended Audience :: Developers',
255           'License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)',
256           'Programming Language :: Python :: 2',
257           'Programming Language :: Python :: 2.7',
258           'Programming Language :: Python :: 3',
259           'Programming Language :: Python :: 3.4',
260           'Programming Language :: Python :: 3.5',
261           'Programming Language :: Python :: 3.6',
262           'Operating System :: POSIX',
263           'Operating System :: Microsoft :: Windows',
264           'Topic :: Communications :: Email',
265           'Topic :: Security :: Cryptography',
266       ],
267 )