blob: 9fb607e5a4dde57501c89b3565291f3d092fe348 [file] [log] [blame]
#!/usr/bin/env python
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
python-qpid-proton setup script
DISCLAIMER: This script took lots of inspirations from PyZMQ, which is licensed
Although inspired by the work in PyZMQ, this script and the modules it depends
on were largely simplified to meet the requirements of the library.
The behavior of this script is to build the registered `_cproton` extension
using the installed Qpid Proton C library and header files. If the library and
headers are not installed, or the installed version does not match the version
of these python bindings, then the script will attempt to build the extension
using the Proton C sources included in the python source distribution package.
While the above removes the need of *always* having Qpid Proton C development
files installed, it does not solve the need of having `swig` and the libraries
qpid-proton requires installed to make this setup work.
From the Python side, this scripts overrides 1 command - build_ext - and it adds a
new one. The latter - Configure - is called from the former to setup/discover what's
in the system. The rest of the commands and steps are done normally without any kind
of monkey patching.
TODO: On windows we now only support VS2015 and above and python 3, we should check
for this and produce an appropriate error if the requirements are not met.
import os
import shutil
from setuptools import setup, Extension
from setuptools.command.sdist import sdist
from setuptools.command.build_ext import build_ext
from setuptools.command.build_py import build_py
import distutils.sysconfig as ds_sys
from distutils.ccompiler import new_compiler, get_default_compiler
from setuputils import log
from setuputils import misc
class Swig(build_ext):
def run(self):
"""Run swig against the sources. This will cause swig to compile the
cproton.i file into a .c file called cproton_wrap.c, and create
ext = Extension('_cproton',
swig_opts=['-threads', '-Iinclude'])
if 'SWIG' in os.environ:
self.swig = os.environ['SWIG']
self.swig_sources(ext.sources, ext)
class CheckSDist(sdist):
def run(self):
class Configure(build_ext):
description = "Discover Qpid Proton version"
def compiler_type(self):
compiler = self.compiler
if compiler is None:
return get_default_compiler()
elif isinstance(compiler, str):
return compiler
return compiler.compiler_type
def use_bundled_proton(self):
"""The proper version of libqpid-proton-core is not installed on the system,
so use the included proton-c sources to build the extension
""""Building the bundled proton-c sources into the extension")
setup_path = os.path.dirname(os.path.realpath(__file__))
base = self.get_finalized_command('build').build_base
build_include = os.path.join(base, 'include')
proton_base = os.path.abspath(os.path.join(setup_path))
proton_src = os.path.join(proton_base, 'src')
proton_core_src = os.path.join(proton_base, 'src', 'core')
proton_include = os.path.join(proton_base, 'include')
log.debug("Using Proton C sources: %s" % proton_base)
# Collect all the Proton C files packaged in the sdist and strip out
# anything windows and configuration-dependent
sources = []
for root, _, files in os.walk(proton_core_src):
for file_ in files:
if file_.endswith(('.c', '.cpp')):
sources.append(os.path.join(root, file_))
# Look for any optional libraries that proton needs, and adjust the
# source list and compile flags as necessary.
libraries = []
includes = []
macros = []
# -D flags (None means no value, just define)
macros += [('PROTON_DECLARE_STATIC', None)]
if self.compiler_type=='msvc':
sources += [
os.path.join(proton_src, 'compiler', 'msvc', 'start.c')
elif self.compiler_type=='unix':
sources += [
os.path.join(proton_src, 'compiler' , 'gcc', 'start.c')
# Check whether openssl is installed by poking
# pkg-config for a minimum version 0. If it's installed, it should
# return True and we'll use it. Otherwise, we'll use the stub.
if misc.pkg_config_version_installed('openssl', atleast='0'):
libraries += ['ssl', 'crypto']
includes += [misc.pkg_config_get_var('openssl', 'includedir')]
sources.append(os.path.join(proton_src, 'ssl', 'openssl.c'))
libraries += ['crypt32', 'secur32']
sources.append(os.path.join(proton_src, 'ssl', 'schannel.cpp'))
sources.append(os.path.join(proton_src, 'ssl', 'ssl_stub.c'))
log.warn("OpenSSL not installed - disabling SSL support!")
# create a temp compiler to check for optional compile-time features
cc = new_compiler(compiler=self.compiler_type)
cc.output_dir = self.build_temp
# 0.10 added an implementation for cyrus. Check
# if it is available before adding the implementation to the sources
# list. 'sasl.c` and 'default_sasl.c' are added and one of the existing
# implementations will be used.
sources.append(os.path.join(proton_src, 'sasl', 'sasl.c'))
sources.append(os.path.join(proton_src, 'sasl', 'default_sasl.c'))
# Skip the SASL detection on Windows.
# MSbuild scans output of Exec tasks and fails the build if it notices error-like
# strings there. This is a known issue with CMake and msbuild, see
# *
# *
if cc.compiler_type!='msvc':
if cc.has_function('sasl_client_done', includes=['sasl/sasl.h'], libraries=['sasl2']):
sources.append(os.path.join(proton_src, 'sasl', 'cyrus_sasl.c'))
log.warn("Cyrus SASL not installed - only the ANONYMOUS and PLAIN mechanisms will be supported!")
sources.append(os.path.join(proton_src, 'sasl', 'cyrus_stub.c'))
log.warn("Windows - only the ANONYMOUS and PLAIN mechanisms will be supported!")
sources.append(os.path.join(proton_src, 'sasl', 'cyrus_stub.c'))
# compile all the proton sources. We'll add the resulting list of
# objects to the _cproton extension as 'extra objects'. We do this
# instead of just lumping all the sources into the extension to prevent
# any proton-specific compilation flags from affecting the compilation
# of the generated swig code
cc = new_compiler(compiler=self.compiler_type)
extra = []
if self.compiler_type=='unix':
objects = cc.compile(sources,
# compiler command line options:
# Now update the _cproton extension instance passed to setup to include
# the objects and libraries
_cproton = self.distribution.ext_modules[-1]
_cproton.extra_objects = objects
# lastly replace the libqpid-proton-core dependency with libraries required
# by the Proton objects:
def libqpid_proton_installed(self, version):
"""Check to see if the proper version of the Proton development library
and headers are already installed
return misc.pkg_config_version_installed('libqpid-proton-core', version)
def use_installed_proton(self):
"""The Proton development headers and library are installed, update the
_cproton extension to tell it where to find the library and headers.
# update the Extension instance passed to setup() to use the installed
# headers and link library
_cproton = self.distribution.ext_modules[-1]
incs = misc.pkg_config_get_var('libqpid-proton-core', 'includedir')
for i in incs.split():
_cproton.swig_opts.append('-I%s' % i)
ldirs = misc.pkg_config_get_var('libqpid-proton-core', 'libdir')
def run(self):
# check if the Proton library and headers are installed and are
# compatible with this version of the binding.
if self.libqpid_proton_installed(_PROTON_VERSION_STR):
# Proton not installed or compatible, use bundled proton-c sources
class BuildExtFirst(build_py):
def run(self):
# Make sure swig runs first and adds file etc
class CheckingBuildExt(build_ext):
def run(self):
# Discover qpid-proton and prerequisites in the system
version=_PROTON_VERSION_STR + os.environ.get('PROTON_VERSION_SUFFIX', ''),
description='An AMQP based messaging library.',
author='Apache Qpid',
license="Apache Software License",
classifiers=["License :: OSI Approved :: Apache Software License",
"Intended Audience :: Developers",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8"],
cmdclass = {
'configure': Configure,
'swig': Swig,
'build_py': BuildExtFirst,
'build_ext': CheckingBuildExt,
'sdist': CheckSDist
'opentracing': ['opentracing', 'jaeger_client']
# Note well: the following extension instance is modified during the
# installation! If you make changes below, you may need to update the
# Configure class above