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