forked from tahoe-lafs/pycryptopp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
348 lines (298 loc) · 12.6 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pycryptopp -- Python wrappers for a few algorithms from Crypto++
#
# Copyright © 2009-2012 Zooko Wilcox-O'Hearn
# Author: Zooko Wilcox-O'Hearn
#
# See README.rst for licensing information.
import os, platform, re, subprocess, sys
from setuptools import Extension, setup
from setuptools import Command
from distutils.util import get_platform
from setuptools.command.test import ScanningLoader
import unittest
import versioneer
versioneer.versionfile_source = "src/pycryptopp/_version.py"
versioneer.versionfile_build = "pycryptopp/_version.py"
versioneer.tag_prefix = "pycryptopp-"
versioneer.parentdir_prefix = "pycryptopp-"
# ECDSA=False
ECDSA=True
DEBUG=False
if "--debug" in sys.argv:
DEBUG=True
sys.argv.remove("--debug")
DISABLE_EMBEDDED_CRYPTOPP=False
if "--disable-embedded-cryptopp" in sys.argv:
DISABLE_EMBEDDED_CRYPTOPP=True
sys.argv.remove("--disable-embedded-cryptopp")
# Unfortunately stdeb v0.3 doesn't seem to offer a way to pass command-line
# arguments to setup.py when building for Debian, but it does offer a way to
# pass environment variables, so we here check for that in addition to the
# command-line argument check above.
if os.environ.get('PYCRYPTOPP_DISABLE_EMBEDDED_CRYPTOPP') == "1":
DISABLE_EMBEDDED_CRYPTOPP=True
EMBEDDED_CRYPTOPP_DIR='src-cryptopp'
BUILD_DOUBLE_LOAD_TESTER=False
BDLTARG="--build-double-load-tester"
if BDLTARG in sys.argv:
BUILD_DOUBLE_LOAD_TESTER=True
sys.argv.remove(BDLTARG)
# There are two ways that this setup.py script can build pycryptopp, either by using the
# Crypto++ source code bundled in the pycryptopp source tree, or by linking to a copy of the
# Crypto++ library that is already installed on the system.
extra_compile_args=[]
extra_link_args=[]
define_macros=[]
undef_macros=[]
libraries=[]
ext_modules=[]
include_dirs=[]
library_dirs=[]
extra_srcs=[] # This is for Crypto++ .cpp files if they are needed.
#
# Fix the build on OpenBSD
# http://tahoe-lafs/trac/pycryptopp/ticket/32
#
if 'openbsd' in platform.system().lower():
extra_link_args.append("-fpic")
if DEBUG:
extra_compile_args.append("-O0")
extra_compile_args.append("-g")
extra_compile_args.append("-Wall")
extra_link_args.append("-g")
undef_macros.append('NDEBUG')
else:
extra_compile_args.append("-w")
if DISABLE_EMBEDDED_CRYPTOPP:
define_macros.append(('DISABLE_EMBEDDED_CRYPTOPP', 1))
# Link with a Crypto++ library that is already installed on the system.
for inclpath in ["/usr/local/include/cryptopp", "/usr/include/cryptopp"]:
if os.path.exists(inclpath):
libraries.append("cryptopp")
incldir = os.path.dirname(inclpath)
include_dirs.append(incldir)
libdir = os.path.join(os.path.dirname(incldir), "lib")
library_dirs.append(libdir)
break
if not libraries:
print "Did not locate libcryptopp in the usual places."
print "Adding /usr/local/{include,lib} and -lcryptopp in the hopes"
print "that they will work."
# Note that when using cygwin build tools (including gcc) to build
# Windows-native binaries, the os.path.exists() will not see the
# /usr/local/include/cryptopp directory but the subsequent call to g++
# will.
libraries.append("cryptopp")
include_dirs.append("/usr/local/include")
library_dirs.append("/usr/local/lib")
else:
# Build the bundled Crypto++ library which is included by source
# code in the pycryptopp tree and link against it.
include_dirs.append(".")
if 'sunos' in platform.system().lower():
extra_compile_args.append('-Wa,--divide') # allow use of "/" operator
if 'win32' in sys.platform.lower():
try:
res = subprocess.Popen(['cl'], stdin=open(os.devnull), stdout=subprocess.PIPE).communicate()
except EnvironmentError, le:
# Okay I guess we're not using the "cl.exe" compiler.
using_msvc = False
else:
using_msvc = True
else:
using_msvc = False
if using_msvc:
# We can handle out-of-line assembly.
cryptopp_src = [ os.path.join(EMBEDDED_CRYPTOPP_DIR, x) for x in os.listdir(EMBEDDED_CRYPTOPP_DIR) if x.endswith(('.cpp', '.asm')) ]
else:
# We can't handle out-of-line assembly.
cryptopp_src = [ os.path.join(EMBEDDED_CRYPTOPP_DIR, x) for x in os.listdir(EMBEDDED_CRYPTOPP_DIR) if x.endswith('.cpp') ]
# Mac OS X extended attribute files when written to a non-Mac-OS-X
# filesystem come out as "._$FNAME", for example "._rdtables.cpp",
# and those files contain uncompilable data that is not C++, thus
# on occasion causing the build to fail. This works-around that:
cryptopp_src = [ c for c in cryptopp_src if not os.path.basename(c).startswith('._') ]
extra_srcs.extend(cryptopp_src)
# In either case, we must provide a value for CRYPTOPP_DISABLE_ASM that
# matches the one used when Crypto++ was originally compiled. The Crypto++
# GNUmakefile tests the assembler version and only enables assembly for
# recent versions of the GNU assembler (2.10 or later). The /usr/bin/as on
# Mac OS-X 10.6 is too old.
try:
sp = subprocess.Popen(['as', '-v'], stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True)
sp.stdin.close()
sp.wait()
if re.search("GNU assembler version (0|1|2.0)", sp.stderr.read()):
define_macros.append(('CRYPTOPP_DISABLE_ASM', 1))
except EnvironmentError:
# Okay, nevermind. Maybe there isn't even an 'as' executable on this
# platform.
pass
else:
try:
# that "as -v" step creates an empty a.out, so clean it up. Modern GNU
# "as" has --version, which emits the version number without actually
# assembling anything, but older versions only have -v, which emits a
# version number and *then* assembles from stdin.
os.unlink("a.out")
except EnvironmentError:
pass
trove_classifiers=[
"Environment :: Console",
"License :: OSI Approved :: GNU General Public License (GPL)", # See README.rst for alternative licensing.
"License :: DFSG approved",
"License :: Other/Proprietary License",
"Intended Audience :: Developers",
"Operating System :: Microsoft :: Windows",
"Operating System :: Unix",
"Operating System :: MacOS :: MacOS X",
"Natural Language :: English",
"Programming Language :: C",
"Programming Language :: C++",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.4",
"Programming Language :: Python :: 2.5",
"Programming Language :: Python :: 2.6",
"Topic :: Software Development :: Libraries",
]
PKG='pycryptopp'
srcs = ['src/pycryptopp/_pycryptoppmodule.cpp',
'src/pycryptopp/publickey/rsamodule.cpp',
'src/pycryptopp/hash/sha256module.cpp',
'src/pycryptopp/cipher/aesmodule.cpp',
'src/pycryptopp/cipher/xsalsa20module.cpp',
]
if ECDSA:
srcs.append('src/pycryptopp/publickey/ecdsamodule.cpp')
if BUILD_DOUBLE_LOAD_TESTER:
srcs.append('_doubleloadtester.cpp', )
ext_modules.append(
Extension('pycryptopp._pycryptopp', extra_srcs + srcs, include_dirs=include_dirs, library_dirs=library_dirs, libraries=libraries, extra_link_args=extra_link_args, extra_compile_args=extra_compile_args, define_macros=define_macros, undef_macros=undef_macros)
)
# python-ed25519
sources = [os.path.join("src-ed25519","glue","ed25519module.c")]
sources.extend([os.path.join("src-ed25519","supercop-ref",s)
for s in os.listdir(os.path.join("src-ed25519","supercop-ref"))
if s.endswith(".c") and s!="test.c"])
m = Extension("pycryptopp.publickey.ed25519._ed25519",
include_dirs=[os.path.join("src-ed25519","supercop-ref")],
sources=sources)
ext_modules.append(m)
if BUILD_DOUBLE_LOAD_TESTER:
ext_modules.append(
Extension('_doubleloadtester', extra_srcs + srcs, include_dirs=include_dirs, library_dirs=library_dirs, libraries=libraries, extra_link_args=extra_link_args, extra_compile_args=extra_compile_args, define_macros=define_macros, undef_macros=undef_macros)
)
miscdeps=os.path.join(os.getcwd(), 'misc', 'dependencies')
dependency_links=[os.path.join(miscdeps, t) for t in os.listdir(miscdeps) if t.endswith(".tar")]
setup_requires = []
install_requires = ['setuptools >= 0.6a9'] # for pkg_resources for loading test vectors for unit tests
# setuptools_pyflakes is needed only if you want "./setup.py flakes" to run
# pyflakes on all the pycryptopp modules.
if 'flakes' in sys.argv[1:]:
setup_requires.append('setuptools_pyflakes >= 1.0.0')
# stdeb is required to produce Debian files with "sdist_dsc".
# http://github.com/astraw/stdeb/tree/master
if "sdist_dsc" in sys.argv:
setup_requires.append('stdeb')
data_fnames=['COPYING.GPL', 'COPYING.TGPPL.html', 'README.rst']
readmetext = open('README.rst').read()
# In case we are building for a .deb with stdeb's sdist_dsc command, we put the
# docs in "share/doc/pycryptopp".
doc_loc = "share/doc/" + PKG
data_files = [(doc_loc, data_fnames)]
commands = versioneer.get_cmdclass().copy()
###### Version updating code
CPP_GIT_VERSION_BODY = '''
/* This _version.py is generated from git metadata by the pycryptopp
* setup.py. The main version number is taken from the most recent release
* tag. If some patches have been added since the last release, this will
* have a -NN "build number" suffix, or else a -rNN "revision number" suffix.
*/
#define CRYPTOPP_EXTRA_VERSION "%(pkgname)s-%(normalized)s"
'''
def get_normalized_version():
pieces = versioneer.get_versions()["version"].split("-")
if len(pieces) == 1:
normalized_version = pieces[0]
else:
normalized_version = "%s.post%s" % (pieces[0], pieces[1])
if pieces[-1] == "dirty":
normalized_version += ".dev0"
return normalized_version
class UpdateVersion(Command):
description = "update extraversion.h from revision-control metadata"
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
versions = versioneer.get_versions()
fn = os.path.join(EMBEDDED_CRYPTOPP_DIR, 'extraversion.h')
f = open(fn, "wb")
BODY = CPP_GIT_VERSION_BODY
f.write(BODY %
{ "pkgname": self.distribution.get_name(),
"version": versions["version"],
"normalized": get_normalized_version(),
"full": versions["full"] })
f.close()
print "git-version: wrote '%s' into '%s'" % (versions["version"], fn)
commands["update_version"] = UpdateVersion
class Test(Command):
description = "run tests"
user_options = []
def initialize_options(self):
self.test_suite = None
def finalize_options(self):
if self.test_suite is None:
self.test_suite = self.distribution.test_suite
def setup_path(self):
# copied from distutils/command/build.py
self.plat_name = get_platform()
plat_specifier = ".%s-%s" % (self.plat_name, sys.version[0:3])
self.build_lib = os.path.join("build", "lib"+plat_specifier)
sys.path.insert(0, self.build_lib)
def run(self):
self.setup_path()
loader = ScanningLoader()
test = loader.loadTestsFromName(self.test_suite)
runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(test)
sys.exit(not result.wasSuccessful())
commands["test"] = Test
setup(name=PKG,
version=get_normalized_version(),
description='Python wrappers for a few algorithms from the Crypto++ library',
long_description=readmetext,
author='Zooko Wilcox-O\'Hearn',
author_email='zooko@zooko.com',
url='https://tahoe-lafs.org/trac/' + PKG,
license='GNU GPL', # see README.rst for details -- there is also an alternative licence
packages=["pycryptopp",
"pycryptopp.cipher",
"pycryptopp.hash",
"pycryptopp.publickey",
"pycryptopp.publickey.ed25519",
"pycryptopp.test",
],
include_package_data=True,
exclude_package_data={
'': [ '*.cpp', '*.hpp', ]
},
data_files=data_files,
package_dir={"pycryptopp": "src/pycryptopp"},
setup_requires=setup_requires,
install_requires=install_requires,
dependency_links=dependency_links,
classifiers=trove_classifiers,
ext_modules=ext_modules,
test_suite=PKG+".test",
zip_safe=False, # I prefer unzipped for easier access.
cmdclass=commands,
)