~myers-1/pyopenssl/npn

« back to all changes in this revision

Viewing changes to setup.py

  • Committer: Jean-Paul Calderone
  • Date: 2010-01-25 22:55:30 UTC
  • mfrom: (126 trunk)
  • mto: This revision was merged to the branch mainline in revision 129.
  • Revision ID: exarkun@divmod.com-20100125225530-5e9nsb6bzoesoz42
merge trunk and resolve simple conflict

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
Installation script for the OpenSSL module
12
12
"""
13
13
 
 
14
import distutils.log
 
15
distutils.log.set_verbosity(3)
 
16
 
14
17
import sys, os
15
18
from distutils.core import Extension, setup
16
 
 
17
 
from glob import glob
 
19
from distutils.errors import DistutilsFileError
 
20
from distutils.command.build_ext import build_ext
18
21
 
19
22
from version import __version__
20
23
 
46
49
if os.name == 'nt' or sys.platform == 'win32':
47
50
 
48
51
    Libraries = ['Ws2_32']
49
 
    def makeTellMeIf(original, what):
50
 
        class tellMeIf(original):
51
 
            def __init__(*a, **kw):
52
 
                Libraries.extend(what)
53
 
                return original.__init__(*a, **kw)
54
 
        return tellMeIf
55
 
 
56
 
    from distutils import cygwinccompiler
57
 
    cygwinccompiler.Mingw32CCompiler = makeTellMeIf(cygwinccompiler.Mingw32CCompiler, ['eay32', 'ssl32'])
58
 
    from distutils import msvccompiler
59
 
    msvccompiler.MSVCCompiler = makeTellMeIf(msvccompiler.MSVCCompiler, ['libeay32', 'ssleay32'])
60
 
 
61
 
    import shutil
62
 
    shutil.copy("C:\\OpenSSL\\ssleay32.dll", os.path.split(os.path.abspath(__file__))[0])
63
 
    shutil.copy("C:\\OpenSSL\\libeay32.dll", os.path.split(os.path.abspath(__file__))[0])
64
 
    package_data = {'': ['ssleay32.dll', 'libeay32.dll']}
 
52
 
 
53
 
 
54
 
 
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
            """
 
84
            potentials = []
 
85
            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)
 
91
                    potentials.append(ssldir)
 
92
                    childdirs = os.listdir(ssldir)
 
93
                    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'")
 
102
 
 
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
 
65
185
else:
66
186
    Libraries = ['ssl', 'crypto']
67
 
    package_data = {}
 
187
    BuildExtension = build_ext
 
188
 
68
189
 
69
190
 
70
191
def mkExtension(name):
87
208
                     'OpenSSL.test.test_rand',
88
209
                     'OpenSSL.test.test_ssl'],
89
210
      zip_safe = False,
90
 
      package_data = package_data,
 
211
      cmdclass = {"build_ext": BuildExtension},
91
212
      description = 'Python wrapper module around the OpenSSL library',
92
213
      author = 'Martin Sjögren, AB Strakt',
93
214
      author_email = 'msjogren@gmail.com',