blob: 9cb43647f5d58d1eac5f0623c6b2be124ecff0e1 [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 Calderonee53ccf72008-04-11 11:40:39 -04005# Copyright (C) Jean-Paul Calderone 2008, All rights reserved
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05006#
7# @(#) $Id: setup.py,v 1.28 2004/08/10 10:59:01 martin Exp $
8#
Jean-Paul Calderonee53ccf72008-04-11 11:40:39 -04009
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050010"""
11Installation script for the OpenSSL module
12"""
13
Jean-Paul Calderone3b48cb72009-11-11 10:51:56 -050014import distutils.log
15distutils.log.set_verbosity(3)
16
Jean-Paul Calderoneddbc28f2008-12-31 16:33:17 -050017import sys, os
Jean-Paul Calderone5a1bf382009-02-08 16:39:15 -050018from distutils.core import Extension, setup
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -050019from distutils.errors import DistutilsFileError
20from distutils.command.build_ext import build_ext
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050021
22from version import __version__
23
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050024crypto_src = ['src/crypto/crypto.c', 'src/crypto/x509.c',
25 'src/crypto/x509name.c', 'src/crypto/pkey.c',
26 'src/crypto/x509store.c', 'src/crypto/x509req.c',
27 'src/crypto/x509ext.c', 'src/crypto/pkcs7.c',
28 'src/crypto/pkcs12.c', 'src/crypto/netscape_spki.c',
Rick Dean536ba022009-07-24 23:57:27 -050029 'src/crypto/revoked.c', 'src/crypto/crl.c',
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050030 'src/util.c']
31crypto_dep = ['src/crypto/crypto.h', 'src/crypto/x509.h',
32 'src/crypto/x509name.h', 'src/crypto/pkey.h',
33 'src/crypto/x509store.h', 'src/crypto/x509req.h',
34 'src/crypto/x509ext.h', 'src/crypto/pkcs7.h',
35 'src/crypto/pkcs12.h', 'src/crypto/netscape_spki.h',
Rick Dean536ba022009-07-24 23:57:27 -050036 'src/crypto/revoked.h', 'src/crypto/crl.h',
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050037 'src/util.h']
38rand_src = ['src/rand/rand.c', 'src/util.c']
39rand_dep = ['src/util.h']
40ssl_src = ['src/ssl/connection.c', 'src/ssl/context.c', 'src/ssl/ssl.c',
41 'src/util.c']
42ssl_dep = ['src/ssl/connection.h', 'src/ssl/context.h', 'src/ssl/ssl.h',
43 'src/util.h']
44
45IncludeDirs = None
46LibraryDirs = None
47
48# Add more platforms here when needed
49if os.name == 'nt' or sys.platform == 'win32':
Jean-Paul Calderone61b0c432009-07-20 16:55:35 -040050
51 Libraries = ['Ws2_32']
Jean-Paul Calderone61b0c432009-07-20 16:55:35 -040052
Jean-Paul Calderone61b0c432009-07-20 16:55:35 -040053
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -050054
55 class BuildExtension(build_ext):
56 """
57 A custom command that semiautomatically finds dependencies required by
58 PyOpenSSL.
59 """
60
61 user_options = (build_ext.user_options +
62 [("with-openssl=", None,
63 "directory where OpenSSL is installed")])
64 with_openssl = None
65 openssl_dlls = ()
66 openssl_mingw = False
67
68
69 def finalize_options(self):
70 """
71 Update build options with details about OpenSSL.
72 """
73 build_ext.finalize_options(self)
74 if self.with_openssl is None:
75 self.find_openssl()
76 self.find_openssl_dlls()
77 self.add_openssl_compile_info()
78
79
80 def find_openssl(self):
81 """
82 Find OpenSSL's install directory.
83 """
Jean-Paul Calderone6ef65882009-11-11 10:36:04 -050084 potentials = []
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -050085 dirs = os.environ.get("PATH").split(os.pathsep)
86 for d in dirs:
87 if os.path.exists(os.path.join(d, "openssl.exe")):
88 ssldir, bin = os.path.split(d)
89 if not bin:
90 ssldir, bin = os.path.split(ssldir)
Jean-Paul Calderone6ef65882009-11-11 10:36:04 -050091 potentials.append(ssldir)
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -050092 childdirs = os.listdir(ssldir)
Jean-Paul Calderone6ef65882009-11-11 10:36:04 -050093 if "lib" in childdirs and "include" in childdirs:
94 self.with_openssl = ssldir
95 return
96 if potentials:
97 raise DistutilsFileError(
98 "Only found improper OpenSSL directories: %r" % (
99 potentials,))
100 else:
101 raise DistutilsFileError("Could not find 'openssl.exe'")
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -0500102
103
104 def find_openssl_dlls(self):
105 """
106 Find OpenSSL's shared libraries.
107 """
108 self.openssl_dlls = []
109 self.find_openssl_dll("libssl32.dll", False)
110 if self.openssl_dlls:
111 self.openssl_mingw = True
112 else:
113 self.find_openssl_dll("ssleay32.dll", True)
114 self.find_openssl_dll("libeay32.dll", True)
115 # add zlib to the mix if it looks like OpenSSL
116 # was linked with a private copy of it
117 self.find_openssl_dll("zlib1.dll", False)
118
119
120 def find_openssl_dll(self, name, required):
121 """
122 Find OpenSSL's shared library and its path after installation.
123 """
124 dllpath = os.path.join(self.with_openssl, "bin", name)
125 if not os.path.exists(dllpath):
126 if required:
127 raise DistutilsFileError("could not find '%s'" % name)
128 else:
129 return
130 newpath = os.path.join(self.build_lib, "OpenSSL", name)
131 self.openssl_dlls.append((dllpath, newpath))
132
133
134 def add_openssl_compile_info(self):
135 """
136 Set up various compile and link parameters.
137 """
138 if self.compiler == "mingw32":
139 if self.openssl_mingw:
140 # Library path and library names are sane when OpenSSL is
141 # built with MinGW .
142 libdir = "lib"
143 libs = ["eay32", "ssl32"]
144 else:
145 libdir = ""
146 libs = []
147 # Unlike when using the binary installer, which creates
148 # an atypical shared library name 'ssleay32', so we have
149 # to use this workaround.
150 if self.link_objects is None:
151 self.link_objects = []
152 for dllpath, _ in self.openssl_dlls:
153 dllname = os.path.basename(dllpath)
154 libname = os.path.splitext(dllname)[0] + ".a"
155 libpath = os.path.join(self.with_openssl,
156 "lib", "MinGW", libname)
157 self.link_objects.append(libpath)
158 else:
159 libdir = "lib"
160 libs = ["libeay32", "ssleay32"]
161 self.include_dirs.append(os.path.join(self.with_openssl, "include"))
162 self.library_dirs.append(os.path.join(self.with_openssl, libdir))
163 self.libraries.extend(libs)
164
165
166 def run(self):
167 """
168 Build extension modules and copy shared libraries.
169 """
170 build_ext.run(self)
171 for dllpath, newpath in self.openssl_dlls:
172 self.copy_file(dllpath, newpath)
173
174
175 def get_outputs(self):
176 """
177 Return a list of file paths built by this comand.
178 """
179 output = [pathpair[1] for pathpair in self.openssl_dlls]
180 output.extend(build_ext.get_outputs(self))
181 return output
182
183
184
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500185else:
186 Libraries = ['ssl', 'crypto']
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -0500187 BuildExtension = build_ext
188
Jean-Paul Calderone5a1bf382009-02-08 16:39:15 -0500189
U-YOUR-FA38FA253F\Zooko Brillnonywonxd78922f2008-11-10 06:19:16 -0700190
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500191def mkExtension(name):
Jean-Paul Calderonee9404372008-03-04 22:19:18 -0500192 modname = 'OpenSSL.' + name
193 src = globals()[name.lower() + '_src']
194 dep = globals()[name.lower() + '_dep']
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500195 return Extension(modname, src, libraries=Libraries, depends=dep,
Jean-Paul Calderone2d79b302009-07-20 14:49:53 -0400196 include_dirs=IncludeDirs, library_dirs=LibraryDirs)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500197
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500198
199setup(name='pyOpenSSL', version=__version__,
Jean-Paul Calderonee0d94c82009-07-21 11:12:52 -0400200 packages = ['OpenSSL'],
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400201 package_dir = {'OpenSSL': '.'},
202 ext_modules = [mkExtension('crypto'), mkExtension('rand'),
203 mkExtension('SSL')],
204 py_modules = ['OpenSSL.__init__', 'OpenSSL.tsafe',
205 'OpenSSL.version', 'OpenSSL.test.__init__',
Jean-Paul Calderone0b88b6a2009-07-05 12:44:41 -0400206 'OpenSSL.test.util',
Jean-Paul Calderone30c09ea2008-03-21 17:04:05 -0400207 'OpenSSL.test.test_crypto',
Rick Dean433dc642009-07-07 13:11:55 -0500208 'OpenSSL.test.test_rand',
Jean-Paul Calderone30c09ea2008-03-21 17:04:05 -0400209 'OpenSSL.test.test_ssl'],
Jean-Paul Calderone71d62c02009-07-21 11:30:22 -0400210 zip_safe = False,
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -0500211 cmdclass = {"build_ext": BuildExtension},
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500212 description = 'Python wrapper module around the OpenSSL library',
Jean-Paul Calderonee53ccf72008-04-11 11:40:39 -0400213 author = 'Martin Sjögren, AB Strakt',
214 author_email = 'msjogren@gmail.com',
215 maintainer = 'Jean-Paul Calderone',
216 maintainer_email = 'exarkun@twistedmatrix.com',
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500217 url = 'http://pyopenssl.sourceforge.net/',
218 license = 'LGPL',
219 long_description = """\
220High-level wrapper around a subset of the OpenSSL library, includes
221 * SSL.Connection objects, wrapping the methods of Python's portable
222 sockets
223 * Callbacks written in Python
224 * Extensive error-handling mechanism, mirroring OpenSSL's error codes
225... and much more ;)"""
226 )