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