blob: 6366b485a36c9ba4b99d52ae93f12c0b67ca2bb4 [file] [log] [blame]
Jean-Paul Calderonecf20dd62008-06-12 16:38:38 -04001#!/usr/bin/env python
Jean-Paul Calderone0db6cdb2008-04-11 11:52:15 -04002# -*- coding: utf-8 -*-
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05003#
4# Copyright (C) AB Strakt 2001, All rights reserved
Jean-Paul Calderonebe1e9d82010-10-07 22:32:00 -04005# Copyright (C) Jean-Paul Calderone 2008-2010, All rights reserved
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05006#
Jean-Paul Calderonee53ccf72008-04-11 11:40:39 -04007
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05008"""
9Installation script for the OpenSSL module
10"""
11
Jean-Paul Calderoneddbc28f2008-12-31 16:33:17 -050012import sys, os
Jean-Paul Calderone5a1bf382009-02-08 16:39:15 -050013from distutils.core import Extension, setup
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -050014from distutils.errors import DistutilsFileError
15from distutils.command.build_ext import build_ext
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050016
Jean-Paul Calderoneba4308e2010-07-27 20:56:32 -040017# XXX Deduplicate this
Jean-Paul Calderone5e99f442011-09-02 11:08:10 -040018__version__ = '0.13'
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050019
Jean-Paul Calderone084b7522013-02-09 09:53:45 -080020xcrypto_src = ['OpenSSL/crypto/crypto.c', 'OpenSSL/crypto/x509.c',
Jean-Paul Calderone024375a2010-07-27 20:37:50 -040021 'OpenSSL/crypto/x509name.c', 'OpenSSL/crypto/pkey.c',
22 'OpenSSL/crypto/x509store.c', 'OpenSSL/crypto/x509req.c',
23 'OpenSSL/crypto/x509ext.c', 'OpenSSL/crypto/pkcs7.c',
24 'OpenSSL/crypto/pkcs12.c', 'OpenSSL/crypto/netscape_spki.c',
25 'OpenSSL/crypto/revoked.c', 'OpenSSL/crypto/crl.c',
26 'OpenSSL/util.c']
Jean-Paul Calderone084b7522013-02-09 09:53:45 -080027xcrypto_dep = ['OpenSSL/crypto/crypto.h', 'OpenSSL/crypto/x509.h',
Jean-Paul Calderone024375a2010-07-27 20:37:50 -040028 'OpenSSL/crypto/x509name.h', 'OpenSSL/crypto/pkey.h',
29 'OpenSSL/crypto/x509store.h', 'OpenSSL/crypto/x509req.h',
30 'OpenSSL/crypto/x509ext.h', 'OpenSSL/crypto/pkcs7.h',
31 'OpenSSL/crypto/pkcs12.h', 'OpenSSL/crypto/netscape_spki.h',
32 'OpenSSL/crypto/revoked.h', 'OpenSSL/crypto/crl.h',
33 'OpenSSL/util.h']
Jean-Paul Calderone024375a2010-07-27 20:37:50 -040034ssl_src = ['OpenSSL/ssl/connection.c', 'OpenSSL/ssl/context.c', 'OpenSSL/ssl/ssl.c',
Jean-Paul Calderonee0fcf512012-02-13 09:10:15 -050035 'OpenSSL/ssl/session.c', 'OpenSSL/util.c']
Jean-Paul Calderone024375a2010-07-27 20:37:50 -040036ssl_dep = ['OpenSSL/ssl/connection.h', 'OpenSSL/ssl/context.h', 'OpenSSL/ssl/ssl.h',
Jean-Paul Calderonee0fcf512012-02-13 09:10:15 -050037 'OpenSSL/ssl/session.h', 'OpenSSL/util.h']
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050038
39IncludeDirs = None
40LibraryDirs = None
41
42# Add more platforms here when needed
43if os.name == 'nt' or sys.platform == 'win32':
Jean-Paul Calderone61b0c432009-07-20 16:55:35 -040044
45 Libraries = ['Ws2_32']
Jean-Paul Calderone61b0c432009-07-20 16:55:35 -040046
Jean-Paul Calderone61b0c432009-07-20 16:55:35 -040047
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -050048
49 class BuildExtension(build_ext):
50 """
51 A custom command that semiautomatically finds dependencies required by
52 PyOpenSSL.
53 """
54
55 user_options = (build_ext.user_options +
56 [("with-openssl=", None,
57 "directory where OpenSSL is installed")])
58 with_openssl = None
59 openssl_dlls = ()
60 openssl_mingw = False
61
62
63 def finalize_options(self):
64 """
65 Update build options with details about OpenSSL.
66 """
67 build_ext.finalize_options(self)
68 if self.with_openssl is None:
69 self.find_openssl()
70 self.find_openssl_dlls()
71 self.add_openssl_compile_info()
72
73
74 def find_openssl(self):
75 """
76 Find OpenSSL's install directory.
77 """
Jean-Paul Calderone6ef65882009-11-11 10:36:04 -050078 potentials = []
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -050079 dirs = os.environ.get("PATH").split(os.pathsep)
80 for d in dirs:
81 if os.path.exists(os.path.join(d, "openssl.exe")):
82 ssldir, bin = os.path.split(d)
83 if not bin:
84 ssldir, bin = os.path.split(ssldir)
Jean-Paul Calderone6ef65882009-11-11 10:36:04 -050085 potentials.append(ssldir)
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -050086 childdirs = os.listdir(ssldir)
Jean-Paul Calderone6ef65882009-11-11 10:36:04 -050087 if "lib" in childdirs and "include" in childdirs:
88 self.with_openssl = ssldir
89 return
90 if potentials:
91 raise DistutilsFileError(
92 "Only found improper OpenSSL directories: %r" % (
93 potentials,))
94 else:
95 raise DistutilsFileError("Could not find 'openssl.exe'")
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -050096
97
98 def find_openssl_dlls(self):
99 """
100 Find OpenSSL's shared libraries.
101 """
102 self.openssl_dlls = []
103 self.find_openssl_dll("libssl32.dll", False)
104 if self.openssl_dlls:
105 self.openssl_mingw = True
106 else:
107 self.find_openssl_dll("ssleay32.dll", True)
108 self.find_openssl_dll("libeay32.dll", True)
109 # add zlib to the mix if it looks like OpenSSL
110 # was linked with a private copy of it
111 self.find_openssl_dll("zlib1.dll", False)
112
113
114 def find_openssl_dll(self, name, required):
115 """
116 Find OpenSSL's shared library and its path after installation.
117 """
118 dllpath = os.path.join(self.with_openssl, "bin", name)
119 if not os.path.exists(dllpath):
120 if required:
121 raise DistutilsFileError("could not find '%s'" % name)
122 else:
123 return
124 newpath = os.path.join(self.build_lib, "OpenSSL", name)
125 self.openssl_dlls.append((dllpath, newpath))
126
127
128 def add_openssl_compile_info(self):
129 """
130 Set up various compile and link parameters.
131 """
132 if self.compiler == "mingw32":
133 if self.openssl_mingw:
134 # Library path and library names are sane when OpenSSL is
135 # built with MinGW .
136 libdir = "lib"
137 libs = ["eay32", "ssl32"]
138 else:
139 libdir = ""
140 libs = []
141 # Unlike when using the binary installer, which creates
142 # an atypical shared library name 'ssleay32', so we have
143 # to use this workaround.
144 if self.link_objects is None:
145 self.link_objects = []
146 for dllpath, _ in self.openssl_dlls:
147 dllname = os.path.basename(dllpath)
148 libname = os.path.splitext(dllname)[0] + ".a"
149 libpath = os.path.join(self.with_openssl,
150 "lib", "MinGW", libname)
151 self.link_objects.append(libpath)
152 else:
153 libdir = "lib"
154 libs = ["libeay32", "ssleay32"]
155 self.include_dirs.append(os.path.join(self.with_openssl, "include"))
156 self.library_dirs.append(os.path.join(self.with_openssl, libdir))
157 self.libraries.extend(libs)
158
159
160 def run(self):
161 """
162 Build extension modules and copy shared libraries.
163 """
164 build_ext.run(self)
165 for dllpath, newpath in self.openssl_dlls:
166 self.copy_file(dllpath, newpath)
167
168
169 def get_outputs(self):
170 """
171 Return a list of file paths built by this comand.
172 """
173 output = [pathpair[1] for pathpair in self.openssl_dlls]
174 output.extend(build_ext.get_outputs(self))
175 return output
176
177
178
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500179else:
180 Libraries = ['ssl', 'crypto']
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -0500181 BuildExtension = build_ext
182
Jean-Paul Calderone5a1bf382009-02-08 16:39:15 -0500183
U-YOUR-FA38FA253F\Zooko Brillnonywonxd78922f2008-11-10 06:19:16 -0700184
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500185def mkExtension(name):
Jean-Paul Calderonee9404372008-03-04 22:19:18 -0500186 modname = 'OpenSSL.' + name
187 src = globals()[name.lower() + '_src']
188 dep = globals()[name.lower() + '_dep']
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500189 return Extension(modname, src, libraries=Libraries, depends=dep,
Jean-Paul Calderone2d79b302009-07-20 14:49:53 -0400190 include_dirs=IncludeDirs, library_dirs=LibraryDirs)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500191
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500192
193setup(name='pyOpenSSL', version=__version__,
Jean-Paul Calderonee0d94c82009-07-21 11:12:52 -0400194 packages = ['OpenSSL'],
Jean-Paul Calderone024375a2010-07-27 20:37:50 -0400195 package_dir = {'OpenSSL': 'OpenSSL'},
Jean-Paul Calderone084b7522013-02-09 09:53:45 -0800196 ext_modules = [mkExtension('xcrypto'), mkExtension('SSL')],
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400197 py_modules = ['OpenSSL.__init__', 'OpenSSL.tsafe',
Jean-Paul Calderone8210b922013-02-09 09:03:18 -0800198 'OpenSSL.rand',
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400199 'OpenSSL.version', 'OpenSSL.test.__init__',
Jean-Paul Calderone0b88b6a2009-07-05 12:44:41 -0400200 'OpenSSL.test.util',
Jean-Paul Calderone30c09ea2008-03-21 17:04:05 -0400201 'OpenSSL.test.test_crypto',
Rick Dean433dc642009-07-07 13:11:55 -0500202 'OpenSSL.test.test_rand',
Jean-Paul Calderone30c09ea2008-03-21 17:04:05 -0400203 'OpenSSL.test.test_ssl'],
Jean-Paul Calderone71d62c02009-07-21 11:30:22 -0400204 zip_safe = False,
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -0500205 cmdclass = {"build_ext": BuildExtension},
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500206 description = 'Python wrapper module around the OpenSSL library',
Jean-Paul Calderonee53ccf72008-04-11 11:40:39 -0400207 author = 'Martin Sjögren, AB Strakt',
208 author_email = 'msjogren@gmail.com',
209 maintainer = 'Jean-Paul Calderone',
210 maintainer_email = 'exarkun@twistedmatrix.com',
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500211 url = 'http://pyopenssl.sourceforge.net/',
Jean-Paul Calderone9820bba2011-03-02 19:02:33 -0500212 license = 'APL2',
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500213 long_description = """\
214High-level wrapper around a subset of the OpenSSL library, includes
215 * SSL.Connection objects, wrapping the methods of Python's portable
216 sockets
217 * Callbacks written in Python
218 * Extensive error-handling mechanism, mirroring OpenSSL's error codes
Jean-Paul Calderoned59d3bc2011-12-19 13:32:29 -0500219... and much more ;)""",
220 classifiers = [
221 'Development Status :: 6 - Mature',
222 'Intended Audience :: Developers',
223 'License :: OSI Approved :: Apache Software License',
224 'Operating System :: MacOS :: MacOS X',
225 'Operating System :: Microsoft :: Windows',
226 'Operating System :: POSIX',
227 'Programming Language :: C',
228 'Programming Language :: Python :: 2.4',
229 'Programming Language :: Python :: 2.5',
230 'Programming Language :: Python :: 2.6',
231 'Programming Language :: Python :: 2.7',
232 'Programming Language :: Python :: 3.2',
233 'Programming Language :: Python :: Implementation :: CPython',
234 'Programming Language :: Python :: Implementation :: PyPy',
235 'Topic :: Security :: Cryptography',
236 'Topic :: Software Development :: Libraries :: Python Modules',
237 'Topic :: System :: Networking',
238 ])