blob: 1a74e6e1ee46f87bfad399ea33cc5bead836ed8f [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 Calderonea819f062010-10-31 10:35:24 -040018__version__ = '0.11'
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050019
Jean-Paul Calderone024375a2010-07-27 20:37:50 -040020crypto_src = ['OpenSSL/crypto/crypto.c', 'OpenSSL/crypto/x509.c',
21 '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']
27crypto_dep = ['OpenSSL/crypto/crypto.h', 'OpenSSL/crypto/x509.h',
28 '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']
34rand_src = ['OpenSSL/rand/rand.c', 'OpenSSL/util.c']
35rand_dep = ['OpenSSL/util.h']
36ssl_src = ['OpenSSL/ssl/connection.c', 'OpenSSL/ssl/context.c', 'OpenSSL/ssl/ssl.c',
37 'OpenSSL/util.c']
38ssl_dep = ['OpenSSL/ssl/connection.h', 'OpenSSL/ssl/context.h', 'OpenSSL/ssl/ssl.h',
39 'OpenSSL/util.h']
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050040
41IncludeDirs = None
42LibraryDirs = None
43
44# Add more platforms here when needed
45if os.name == 'nt' or sys.platform == 'win32':
Jean-Paul Calderone61b0c432009-07-20 16:55:35 -040046
47 Libraries = ['Ws2_32']
Jean-Paul Calderone61b0c432009-07-20 16:55:35 -040048
Jean-Paul Calderone61b0c432009-07-20 16:55:35 -040049
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -050050
51 class BuildExtension(build_ext):
52 """
53 A custom command that semiautomatically finds dependencies required by
54 PyOpenSSL.
55 """
56
57 user_options = (build_ext.user_options +
58 [("with-openssl=", None,
59 "directory where OpenSSL is installed")])
60 with_openssl = None
61 openssl_dlls = ()
62 openssl_mingw = False
63
64
65 def finalize_options(self):
66 """
67 Update build options with details about OpenSSL.
68 """
69 build_ext.finalize_options(self)
70 if self.with_openssl is None:
71 self.find_openssl()
72 self.find_openssl_dlls()
73 self.add_openssl_compile_info()
74
75
76 def find_openssl(self):
77 """
78 Find OpenSSL's install directory.
79 """
Jean-Paul Calderone6ef65882009-11-11 10:36:04 -050080 potentials = []
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -050081 dirs = os.environ.get("PATH").split(os.pathsep)
82 for d in dirs:
83 if os.path.exists(os.path.join(d, "openssl.exe")):
84 ssldir, bin = os.path.split(d)
85 if not bin:
86 ssldir, bin = os.path.split(ssldir)
Jean-Paul Calderone6ef65882009-11-11 10:36:04 -050087 potentials.append(ssldir)
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -050088 childdirs = os.listdir(ssldir)
Jean-Paul Calderone6ef65882009-11-11 10:36:04 -050089 if "lib" in childdirs and "include" in childdirs:
90 self.with_openssl = ssldir
91 return
92 if potentials:
93 raise DistutilsFileError(
94 "Only found improper OpenSSL directories: %r" % (
95 potentials,))
96 else:
97 raise DistutilsFileError("Could not find 'openssl.exe'")
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -050098
99
100 def find_openssl_dlls(self):
101 """
102 Find OpenSSL's shared libraries.
103 """
104 self.openssl_dlls = []
105 self.find_openssl_dll("libssl32.dll", False)
106 if self.openssl_dlls:
107 self.openssl_mingw = True
108 else:
109 self.find_openssl_dll("ssleay32.dll", True)
110 self.find_openssl_dll("libeay32.dll", True)
111 # add zlib to the mix if it looks like OpenSSL
112 # was linked with a private copy of it
113 self.find_openssl_dll("zlib1.dll", False)
114
115
116 def find_openssl_dll(self, name, required):
117 """
118 Find OpenSSL's shared library and its path after installation.
119 """
120 dllpath = os.path.join(self.with_openssl, "bin", name)
121 if not os.path.exists(dllpath):
122 if required:
123 raise DistutilsFileError("could not find '%s'" % name)
124 else:
125 return
126 newpath = os.path.join(self.build_lib, "OpenSSL", name)
127 self.openssl_dlls.append((dllpath, newpath))
128
129
130 def add_openssl_compile_info(self):
131 """
132 Set up various compile and link parameters.
133 """
134 if self.compiler == "mingw32":
135 if self.openssl_mingw:
136 # Library path and library names are sane when OpenSSL is
137 # built with MinGW .
138 libdir = "lib"
139 libs = ["eay32", "ssl32"]
140 else:
141 libdir = ""
142 libs = []
143 # Unlike when using the binary installer, which creates
144 # an atypical shared library name 'ssleay32', so we have
145 # to use this workaround.
146 if self.link_objects is None:
147 self.link_objects = []
148 for dllpath, _ in self.openssl_dlls:
149 dllname = os.path.basename(dllpath)
150 libname = os.path.splitext(dllname)[0] + ".a"
151 libpath = os.path.join(self.with_openssl,
152 "lib", "MinGW", libname)
153 self.link_objects.append(libpath)
154 else:
155 libdir = "lib"
156 libs = ["libeay32", "ssleay32"]
157 self.include_dirs.append(os.path.join(self.with_openssl, "include"))
158 self.library_dirs.append(os.path.join(self.with_openssl, libdir))
159 self.libraries.extend(libs)
160
161
162 def run(self):
163 """
164 Build extension modules and copy shared libraries.
165 """
166 build_ext.run(self)
167 for dllpath, newpath in self.openssl_dlls:
168 self.copy_file(dllpath, newpath)
169
170
171 def get_outputs(self):
172 """
173 Return a list of file paths built by this comand.
174 """
175 output = [pathpair[1] for pathpair in self.openssl_dlls]
176 output.extend(build_ext.get_outputs(self))
177 return output
178
179
180
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500181else:
182 Libraries = ['ssl', 'crypto']
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -0500183 BuildExtension = build_ext
184
Jean-Paul Calderone5a1bf382009-02-08 16:39:15 -0500185
U-YOUR-FA38FA253F\Zooko Brillnonywonxd78922f2008-11-10 06:19:16 -0700186
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500187def mkExtension(name):
Jean-Paul Calderonee9404372008-03-04 22:19:18 -0500188 modname = 'OpenSSL.' + name
189 src = globals()[name.lower() + '_src']
190 dep = globals()[name.lower() + '_dep']
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500191 return Extension(modname, src, libraries=Libraries, depends=dep,
Jean-Paul Calderone2d79b302009-07-20 14:49:53 -0400192 include_dirs=IncludeDirs, library_dirs=LibraryDirs)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500193
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500194
195setup(name='pyOpenSSL', version=__version__,
Jean-Paul Calderonee0d94c82009-07-21 11:12:52 -0400196 packages = ['OpenSSL'],
Jean-Paul Calderone024375a2010-07-27 20:37:50 -0400197 package_dir = {'OpenSSL': 'OpenSSL'},
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400198 ext_modules = [mkExtension('crypto'), mkExtension('rand'),
199 mkExtension('SSL')],
200 py_modules = ['OpenSSL.__init__', 'OpenSSL.tsafe',
201 'OpenSSL.version', 'OpenSSL.test.__init__',
Jean-Paul Calderone0b88b6a2009-07-05 12:44:41 -0400202 'OpenSSL.test.util',
Jean-Paul Calderone30c09ea2008-03-21 17:04:05 -0400203 'OpenSSL.test.test_crypto',
Rick Dean433dc642009-07-07 13:11:55 -0500204 'OpenSSL.test.test_rand',
Jean-Paul Calderone30c09ea2008-03-21 17:04:05 -0400205 'OpenSSL.test.test_ssl'],
Jean-Paul Calderone71d62c02009-07-21 11:30:22 -0400206 zip_safe = False,
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -0500207 cmdclass = {"build_ext": BuildExtension},
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500208 description = 'Python wrapper module around the OpenSSL library',
Jean-Paul Calderonee53ccf72008-04-11 11:40:39 -0400209 author = 'Martin Sjögren, AB Strakt',
210 author_email = 'msjogren@gmail.com',
211 maintainer = 'Jean-Paul Calderone',
212 maintainer_email = 'exarkun@twistedmatrix.com',
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500213 url = 'http://pyopenssl.sourceforge.net/',
214 license = 'LGPL',
215 long_description = """\
216High-level wrapper around a subset of the OpenSSL library, includes
217 * SSL.Connection objects, wrapping the methods of Python's portable
218 sockets
219 * Callbacks written in Python
220 * Extensive error-handling mechanism, mirroring OpenSSL's error codes
221... and much more ;)"""
222 )