blob: 3a9ebf47ba500c5d9ee7cf99959ad510b7739b94 [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 """
79 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)
85 childdirs = os.listdir(ssldir)
86 if (not os.path.isdir(os.path.join(ssldir, "lib")) or
87 not os.path.isdir(os.path.join(ssldir, "include"))):
88 msg = "'%s' is not a proper OpenSSL install dir"
89 raise DistutilsFileError(msg % ssldir)
90 self.with_openssl = ssldir
91 return
92 raise DistutilsFileError("could not find 'openssl.exe'")
93
94
95 def find_openssl_dlls(self):
96 """
97 Find OpenSSL's shared libraries.
98 """
99 self.openssl_dlls = []
100 self.find_openssl_dll("libssl32.dll", False)
101 if self.openssl_dlls:
102 self.openssl_mingw = True
103 else:
104 self.find_openssl_dll("ssleay32.dll", True)
105 self.find_openssl_dll("libeay32.dll", True)
106 # add zlib to the mix if it looks like OpenSSL
107 # was linked with a private copy of it
108 self.find_openssl_dll("zlib1.dll", False)
109
110
111 def find_openssl_dll(self, name, required):
112 """
113 Find OpenSSL's shared library and its path after installation.
114 """
115 dllpath = os.path.join(self.with_openssl, "bin", name)
116 if not os.path.exists(dllpath):
117 if required:
118 raise DistutilsFileError("could not find '%s'" % name)
119 else:
120 return
121 newpath = os.path.join(self.build_lib, "OpenSSL", name)
122 self.openssl_dlls.append((dllpath, newpath))
123
124
125 def add_openssl_compile_info(self):
126 """
127 Set up various compile and link parameters.
128 """
129 if self.compiler == "mingw32":
130 if self.openssl_mingw:
131 # Library path and library names are sane when OpenSSL is
132 # built with MinGW .
133 libdir = "lib"
134 libs = ["eay32", "ssl32"]
135 else:
136 libdir = ""
137 libs = []
138 # Unlike when using the binary installer, which creates
139 # an atypical shared library name 'ssleay32', so we have
140 # to use this workaround.
141 if self.link_objects is None:
142 self.link_objects = []
143 for dllpath, _ in self.openssl_dlls:
144 dllname = os.path.basename(dllpath)
145 libname = os.path.splitext(dllname)[0] + ".a"
146 libpath = os.path.join(self.with_openssl,
147 "lib", "MinGW", libname)
148 self.link_objects.append(libpath)
149 else:
150 libdir = "lib"
151 libs = ["libeay32", "ssleay32"]
152 self.include_dirs.append(os.path.join(self.with_openssl, "include"))
153 self.library_dirs.append(os.path.join(self.with_openssl, libdir))
154 self.libraries.extend(libs)
155
156
157 def run(self):
158 """
159 Build extension modules and copy shared libraries.
160 """
161 build_ext.run(self)
162 for dllpath, newpath in self.openssl_dlls:
163 self.copy_file(dllpath, newpath)
164
165
166 def get_outputs(self):
167 """
168 Return a list of file paths built by this comand.
169 """
170 output = [pathpair[1] for pathpair in self.openssl_dlls]
171 output.extend(build_ext.get_outputs(self))
172 return output
173
174
175
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500176else:
177 Libraries = ['ssl', 'crypto']
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -0500178 BuildExtension = build_ext
179
Jean-Paul Calderone5a1bf382009-02-08 16:39:15 -0500180
U-YOUR-FA38FA253F\Zooko Brillnonywonxd78922f2008-11-10 06:19:16 -0700181
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500182def mkExtension(name):
Jean-Paul Calderonee9404372008-03-04 22:19:18 -0500183 modname = 'OpenSSL.' + name
184 src = globals()[name.lower() + '_src']
185 dep = globals()[name.lower() + '_dep']
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500186 return Extension(modname, src, libraries=Libraries, depends=dep,
Jean-Paul Calderone2d79b302009-07-20 14:49:53 -0400187 include_dirs=IncludeDirs, library_dirs=LibraryDirs)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500188
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500189
190setup(name='pyOpenSSL', version=__version__,
Jean-Paul Calderonee0d94c82009-07-21 11:12:52 -0400191 packages = ['OpenSSL'],
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400192 package_dir = {'OpenSSL': '.'},
193 ext_modules = [mkExtension('crypto'), mkExtension('rand'),
194 mkExtension('SSL')],
195 py_modules = ['OpenSSL.__init__', 'OpenSSL.tsafe',
196 'OpenSSL.version', 'OpenSSL.test.__init__',
Jean-Paul Calderone0b88b6a2009-07-05 12:44:41 -0400197 'OpenSSL.test.util',
Jean-Paul Calderone30c09ea2008-03-21 17:04:05 -0400198 'OpenSSL.test.test_crypto',
Rick Dean433dc642009-07-07 13:11:55 -0500199 'OpenSSL.test.test_rand',
Jean-Paul Calderone30c09ea2008-03-21 17:04:05 -0400200 'OpenSSL.test.test_ssl'],
Jean-Paul Calderone71d62c02009-07-21 11:30:22 -0400201 zip_safe = False,
Jean-Paul Calderonebcd45452009-11-11 10:10:57 -0500202 cmdclass = {"build_ext": BuildExtension},
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500203 description = 'Python wrapper module around the OpenSSL library',
Jean-Paul Calderonee53ccf72008-04-11 11:40:39 -0400204 author = 'Martin Sjögren, AB Strakt',
205 author_email = 'msjogren@gmail.com',
206 maintainer = 'Jean-Paul Calderone',
207 maintainer_email = 'exarkun@twistedmatrix.com',
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500208 url = 'http://pyopenssl.sourceforge.net/',
209 license = 'LGPL',
210 long_description = """\
211High-level wrapper around a subset of the OpenSSL library, includes
212 * SSL.Connection objects, wrapping the methods of Python's portable
213 sockets
214 * Callbacks written in Python
215 * Extensive error-handling mechanism, mirroring OpenSSL's error codes
216... and much more ;)"""
217 )