| 1 |
#!/usr/bin/env python |
| 2 |
# |
| 3 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
# or more contributor license agreements. See the NOTICE file |
| 5 |
# distributed with this work for additional information |
| 6 |
# regarding copyright ownership. The ASF licenses this file |
| 7 |
# to you under the Apache License, Version 2.0 (the |
| 8 |
# "License"); you may not use this file except in compliance |
| 9 |
# with the License. You may obtain a copy of the License at |
| 10 |
# |
| 11 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
# |
| 13 |
# Unless required by applicable law or agreed to in writing, |
| 14 |
# software distributed under the License is distributed on an |
| 15 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
# KIND, either express or implied. See the License for the |
| 17 |
# specific language governing permissions and limitations |
| 18 |
# under the License. |
| 19 |
|
| 20 |
""" |
| 21 |
python-qpid-proton setup script |
| 22 |
|
| 23 |
DISCLAIMER: This script took lots of inspirations from PyZMQ, which is licensed |
| 24 |
under the 'MODIFIED BSD LICENSE'. |
| 25 |
|
| 26 |
Although inspired by the work in PyZMQ, this script and the modules it depends |
| 27 |
on were largely simplified to meet the requirements of the library. |
| 28 |
|
| 29 |
The behavior of this script is to build the registered `_cproton` extension |
| 30 |
using the installed Qpid Proton C library and header files. If the library and |
| 31 |
headers are not installed, or the installed version does not match the version |
| 32 |
of these python bindings, then the script will attempt to build the extension |
| 33 |
using the Proton C sources included in the python source distribution package. |
| 34 |
|
| 35 |
While the above removes the need of *always* having Qpid Proton C development |
| 36 |
files installed, it does not solve the need of having `swig` and the libraries |
| 37 |
qpid-proton requires installed to make this setup work. |
| 38 |
|
| 39 |
From the Python side, this scripts overrides 1 command - build_ext - and it adds a |
| 40 |
new one. The latter - Configure - is called from the former to setup/discover what's |
| 41 |
in the system. The rest of the commands and steps are done normally without any kind |
| 42 |
of monkey patching. |
| 43 |
|
| 44 |
TODO: On windows we now only support VS2015 and above and python 3, we should check |
| 45 |
for this and produce an appropriate error if the requirements are not met. |
| 46 |
""" |
| 47 |
|
| 48 |
import os |
| 49 |
|
| 50 |
from setuptools import setup, Extension |
| 51 |
from setuptools.command.sdist import sdist |
| 52 |
from setuptools.command.build_ext import build_ext |
| 53 |
from setuptools.command.build_py import build_py |
| 54 |
|
| 55 |
import distutils.sysconfig as ds_sys |
| 56 |
from distutils.ccompiler import new_compiler, get_default_compiler |
| 57 |
|
| 58 |
from setuputils import log |
| 59 |
from setuputils import misc |
| 60 |
|
| 61 |
|
| 62 |
_PROTON_VERSION = (@PN_VERSION_MAJOR@, @PN_VERSION_MINOR@, @PN_VERSION_POINT@) |
| 63 |
_PROTON_VERSION_STR = "%d.%d.%d" % _PROTON_VERSION |
| 64 |
|
| 65 |
|
| 66 |
class Swig(build_ext): |
| 67 |
def run(self): |
| 68 |
"""Run swig against the sources. This will cause swig to compile the |
| 69 |
cproton.i file into a .c file called cproton_wrap.c, and create |
| 70 |
cproton.py. |
| 71 |
""" |
| 72 |
ext = Extension('_cproton', |
| 73 |
sources=['cproton.i'], |
| 74 |
swig_opts=['-threads', '-Iinclude']) |
| 75 |
|
| 76 |
if 'SWIG' in os.environ: |
| 77 |
self.swig = os.environ['SWIG'] |
| 78 |
|
| 79 |
self.swig_sources(ext.sources, ext) |
| 80 |
|
| 81 |
|
| 82 |
class CheckSDist(sdist): |
| 83 |
def run(self): |
| 84 |
self.distribution.run_command('swig') |
| 85 |
sdist.run(self) |
| 86 |
|
| 87 |
|
| 88 |
class Configure(build_ext): |
| 89 |
description = "Discover Qpid Proton version" |
| 90 |
|
| 91 |
@property |
| 92 |
def compiler_type(self): |
| 93 |
compiler = self.compiler |
| 94 |
if compiler is None: |
| 95 |
return get_default_compiler() |
| 96 |
elif isinstance(compiler, str): |
| 97 |
return compiler |
| 98 |
else: |
| 99 |
return compiler.compiler_type |
| 100 |
|
| 101 |
def use_bundled_proton(self): |
| 102 |
"""The proper version of libqpid-proton-core is not installed on the system, |
| 103 |
so use the included proton-c sources to build the extension |
| 104 |
""" |
| 105 |
log.info("Building the bundled proton-c sources into the extension") |
| 106 |
|
| 107 |
setup_path = os.path.dirname(os.path.realpath(__file__)) |
| 108 |
base = self.get_finalized_command('build').build_base |
| 109 |
build_include = os.path.join(base, 'include') |
| 110 |
proton_base = os.path.abspath(os.path.join(setup_path)) |
| 111 |
proton_src = os.path.join(proton_base, 'src') |
| 112 |
proton_core_src = os.path.join(proton_base, 'src', 'core') |
| 113 |
proton_include = os.path.join(proton_base, 'include') |
| 114 |
|
| 115 |
log.debug("Using Proton C sources: %s" % proton_base) |
| 116 |
|
| 117 |
# Collect all the Proton C files packaged in the sdist and strip out |
| 118 |
# anything windows and configuration-dependent |
| 119 |
|
| 120 |
sources = [] |
| 121 |
for root, _, files in os.walk(proton_core_src): |
| 122 |
for file_ in files: |
| 123 |
if file_.endswith(('.c', '.cpp')): |
| 124 |
sources.append(os.path.join(root, file_)) |
| 125 |
|
| 126 |
# Look for any optional libraries that proton needs, and adjust the |
| 127 |
# source list and compile flags as necessary. |
| 128 |
library_dirs = [] |
| 129 |
libraries = [] |
| 130 |
includes = [] |
| 131 |
macros = [] |
| 132 |
|
| 133 |
# -D flags (None means no value, just define) |
| 134 |
macros += [('PROTON_DECLARE_STATIC', None)] |
| 135 |
|
| 136 |
if self.compiler_type == 'msvc': |
| 137 |
sources += [ |
| 138 |
os.path.join(proton_src, 'compiler', 'msvc', 'start.c') |
| 139 |
] |
| 140 |
elif self.compiler_type == 'unix': |
| 141 |
sources += [ |
| 142 |
os.path.join(proton_src, 'compiler', 'gcc', 'start.c') |
| 143 |
] |
| 144 |
|
| 145 |
# Check whether openssl is installed by poking |
| 146 |
# pkg-config for a minimum version 0. If it's installed, it should |
| 147 |
# return True and we'll use it. Otherwise, we'll use the stub. |
| 148 |
if misc.pkg_config_version_installed('openssl', atleast='0'): |
| 149 |
library_dirs += [misc.pkg_config_get_var('openssl', 'libdir')] |
| 150 |
libraries += ['ssl', 'crypto'] |
| 151 |
includes += [misc.pkg_config_get_var('openssl', 'includedir')] |
| 152 |
sources.append(os.path.join(proton_src, 'ssl', 'openssl.c')) |
| 153 |
elif os.name == 'nt': |
| 154 |
libraries += ['crypt32', 'secur32'] |
| 155 |
sources.append(os.path.join(proton_src, 'ssl', 'schannel.cpp')) |
| 156 |
else: |
| 157 |
sources.append(os.path.join(proton_src, 'ssl', 'ssl_stub.c')) |
| 158 |
log.warn("OpenSSL not installed - disabling SSL support!") |
| 159 |
|
| 160 |
# create a temp compiler to check for optional compile-time features |
| 161 |
cc = new_compiler(compiler=self.compiler_type) |
| 162 |
cc.output_dir = self.build_temp |
| 163 |
|
| 164 |
# 0.10 added an implementation for cyrus. Check |
| 165 |
# if it is available before adding the implementation to the sources |
| 166 |
# list. 'sasl.c` and 'default_sasl.c' are added and one of the existing |
| 167 |
# implementations will be used. |
| 168 |
sources.append(os.path.join(proton_src, 'sasl', 'sasl.c')) |
| 169 |
sources.append(os.path.join(proton_src, 'sasl', 'default_sasl.c')) |
| 170 |
|
| 171 |
# Skip the SASL detection on Windows. |
| 172 |
# MSbuild scans output of Exec tasks and fails the build if it notices error-like |
| 173 |
# strings there. This is a known issue with CMake and msbuild, see |
| 174 |
# * https://github.com/Microsoft/msbuild/issues/2424 |
| 175 |
# * https://cmake.org/pipermail/cmake-developers/2015-October/026775.html |
| 176 |
if cc.compiler_type != 'msvc': |
| 177 |
if cc.has_function('sasl_client_done', includes=['sasl/sasl.h'], libraries=['sasl2']): |
| 178 |
libraries.append('sasl2') |
| 179 |
sources.append(os.path.join(proton_src, 'sasl', 'cyrus_sasl.c')) |
| 180 |
else: |
| 181 |
log.warn("Cyrus SASL not installed - only the ANONYMOUS and PLAIN mechanisms will be supported!") |
| 182 |
sources.append(os.path.join(proton_src, 'sasl', 'cyrus_stub.c')) |
| 183 |
else: |
| 184 |
log.warn("Windows - only the ANONYMOUS and PLAIN mechanisms will be supported!") |
| 185 |
sources.append(os.path.join(proton_src, 'sasl', 'cyrus_stub.c')) |
| 186 |
|
| 187 |
# compile all the proton sources. We'll add the resulting list of |
| 188 |
# objects to the _cproton extension as 'extra objects'. We do this |
| 189 |
# instead of just lumping all the sources into the extension to prevent |
| 190 |
# any proton-specific compilation flags from affecting the compilation |
| 191 |
# of the generated swig code |
| 192 |
|
| 193 |
cc = new_compiler(compiler=self.compiler_type) |
| 194 |
ds_sys.customize_compiler(cc) |
| 195 |
|
| 196 |
extra = [] |
| 197 |
if self.compiler_type == 'unix': |
| 198 |
extra.append('-std=gnu99') |
| 199 |
objects = cc.compile(sources, |
| 200 |
macros=macros, |
| 201 |
include_dirs=[build_include, |
| 202 |
proton_include, |
| 203 |
proton_src] + includes, |
| 204 |
# compiler command line options: |
| 205 |
extra_preargs=extra, |
| 206 |
output_dir=self.build_temp) |
| 207 |
|
| 208 |
# |
| 209 |
# Now update the _cproton extension instance passed to setup to include |
| 210 |
# the objects and libraries |
| 211 |
# |
| 212 |
_cproton = self.distribution.ext_modules[-1] |
| 213 |
_cproton.extra_objects = objects |
| 214 |
_cproton.include_dirs.append(build_include) |
| 215 |
_cproton.include_dirs.append(proton_include) |
| 216 |
|
| 217 |
# lastly replace the libqpid-proton-core dependency with libraries required |
| 218 |
# by the Proton objects: |
| 219 |
_cproton.library_dirs = library_dirs |
| 220 |
_cproton.libraries = libraries |
| 221 |
|
| 222 |
def libqpid_proton_installed(self, version): |
| 223 |
"""Check to see if the proper version of the Proton development library |
| 224 |
and headers are already installed |
| 225 |
""" |
| 226 |
return misc.pkg_config_version_installed('libqpid-proton-core', version) |
| 227 |
|
| 228 |
def use_installed_proton(self): |
| 229 |
"""The Proton development headers and library are installed, update the |
| 230 |
_cproton extension to tell it where to find the library and headers. |
| 231 |
""" |
| 232 |
# update the Extension instance passed to setup() to use the installed |
| 233 |
# headers and link library |
| 234 |
_cproton = self.distribution.ext_modules[-1] |
| 235 |
incs = misc.pkg_config_get_var('libqpid-proton-core', 'includedir') |
| 236 |
for i in incs.split(): |
| 237 |
_cproton.swig_opts.append('-I%s' % i) |
| 238 |
_cproton.include_dirs.append(i) |
| 239 |
ldirs = misc.pkg_config_get_var('libqpid-proton-core', 'libdir') |
| 240 |
_cproton.library_dirs.extend(ldirs.split()) |
| 241 |
|
| 242 |
def run(self): |
| 243 |
# check if the Proton library and headers are installed and are |
| 244 |
# compatible with this version of the binding. |
| 245 |
if self.libqpid_proton_installed(_PROTON_VERSION_STR): |
| 246 |
self.use_installed_proton() |
| 247 |
else: |
| 248 |
# Proton not installed or compatible, use bundled proton-c sources |
| 249 |
self.use_bundled_proton() |
| 250 |
|
| 251 |
|
| 252 |
class BuildExtFirst(build_py): |
| 253 |
def run(self): |
| 254 |
# Make sure swig runs first and adds file etc |
| 255 |
self.distribution.run_command('build_ext') |
| 256 |
build_py.run(self) |
| 257 |
|
| 258 |
|
| 259 |
class CheckingBuildExt(build_ext): |
| 260 |
def run(self): |
| 261 |
# Discover qpid-proton and prerequisites in the system |
| 262 |
self.distribution.run_command('configure') |
| 263 |
build_ext.run(self) |
| 264 |
|
| 265 |
|
| 266 |
setup(name='python-qpid-proton', |
| 267 |
version=_PROTON_VERSION_STR + os.environ.get('PROTON_VERSION_SUFFIX', ''), |
| 268 |
description='An AMQP based messaging library.', |
| 269 |
author='Apache Qpid', |
| 270 |
author_email='[email protected]', |
| 271 |
url='http://qpid.apache.org/proton/', |
| 272 |
packages=['proton'], |
| 273 |
py_modules=['cproton'], |
| 274 |
license="Apache Software License", |
| 275 |
classifiers=["License :: OSI Approved :: Apache Software License", |
| 276 |
"Intended Audience :: Developers", |
| 277 |
"Programming Language :: Python", |
| 278 |
"Programming Language :: Python :: 3", |
| 279 |
"Programming Language :: Python :: 3 :: Only", |
| 280 |
"Programming Language :: Python :: 3.6", |
| 281 |
"Programming Language :: Python :: 3.7", |
| 282 |
"Programming Language :: Python :: 3.8", |
| 283 |
"Programming Language :: Python :: 3.9", |
| 284 |
"Programming Language :: Python :: 3.10"], |
| 285 |
cmdclass={ |
| 286 |
'configure': Configure, |
| 287 |
'swig': Swig, |
| 288 |
'build_py': BuildExtFirst, |
| 289 |
'build_ext': CheckingBuildExt, |
| 290 |
'sdist': CheckSDist |
| 291 |
}, |
| 292 |
extras_require={ |
| 293 |
'opentracing': ['opentracing', 'jaeger_client'] |
| 294 |
}, |
| 295 |
# Note well: the following extension instance is modified during the |
| 296 |
# installation! If you make changes below, you may need to update the |
| 297 |
# Configure class above |
| 298 |
ext_modules=[Extension('_cproton', |
| 299 |
sources=['cproton_wrap.c'], |
| 300 |
extra_compile_args=['-DPROTON_DECLARE_STATIC'], |
| 301 |
libraries=['qpid-proton-core'])]) |