setup.py 22.7 KB
Newer Older
1 2
#!/usr/bin/env python

3 4 5

#-----------------------------------------------------------------------------
#  Copyright (c) 2012 Brian Granger, Min Ragan-Kelley
6
#
7
#  This file is part of pyzmq
8
#
9 10
#  Distributed under the terms of the New BSD License.  The full license is in
#  the file COPYING.BSD, distributed as part of this software.
11
#
12 13
#  The `configure` subcommand is copied and adaped from h5py
#  h5py source used under the New BSD license
14
#
15 16
#  h5py: <http://code.google.com/p/h5py/>
#-----------------------------------------------------------------------------
17 18 19 20

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
21
from __future__ import with_statement
22

23 24 25 26 27
import copy
import os
import re
import shutil
import sys
28
from traceback import print_exc
29 30

from distutils.core import setup, Command
31
from distutils.ccompiler import get_default_compiler
32
from distutils.extension import Extension
33
from distutils.errors import CompileError, LinkError
34
from distutils.command.build import build
35
from distutils.command.build_ext import build_ext
36
from distutils.command.sdist import sdist
37 38 39

from unittest import TextTestRunner, TestLoader
from glob import glob
40
from os.path import splitext, basename, join as pjoin
41

42 43 44 45 46 47 48 49
from subprocess import Popen, PIPE
import logging

try:
    from configparser import ConfigParser
except:
    from ConfigParser import ConfigParser

50 51 52 53 54
try:
    import nose
except ImportError:
    nose = None

55 56 57
# local script imports:
from buildutils import (discover_settings, v_str, localpath, savepickle, loadpickle, detect_zmq,
                        warn, fatal, copy_and_patch_libzmq)
58 59 60 61 62 63 64 65 66 67

#-----------------------------------------------------------------------------
# Flags
#-----------------------------------------------------------------------------
# ignore unused-function and strict-aliasing warnings, of which there
# will be many from the Cython generated code:
# note that this is only for gcc-style compilers
if get_default_compiler() in ('unix', 'mingw32'):
    ignore_common_warnings=True
else:
68
    ignore_common_warnings=False
69

70
# the minimum zeromq version this will work against:
71
min_zmq = (2,1,4)
72 73 74 75 76

# set dylib ext:
if sys.platform.startswith('win'):
    lib_ext = '.dll'
elif sys.platform == 'darwin':
77
    lib_ext = '.dylib'
78
else:
79
    lib_ext = '.so'
80 81 82 83 84 85 86 87 88 89 90 91 92 93

# whether any kind of bdist is happening
doing_bdist = any(arg.startswith('bdist') for arg in sys.argv[1:])

#-----------------------------------------------------------------------------
# Configuration (adapted from h5py: http://h5py.googlecode.com)
#-----------------------------------------------------------------------------


ZMQ = discover_settings()

if ZMQ is not None and not os.path.exists(ZMQ):
    warn("ZMQ directory \"%s\" does not appear to exist" % ZMQ)

94
# bundle_libzmq flag for whether libzmq will be included in pyzmq:
95
if sys.platform.startswith('win'):
96 97 98
    bundle_libzmq = True
elif ZMQ is not None:
    bundle_libzmq = doing_bdist
99
else:
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    bundle_libzmq = False

# --- compiler settings -------------------------------------------------

def settings_from_prefix(zmq=None):
    """load appropriate library/include settings from ZMQ prefix"""
    if sys.platform.startswith('win'):
        settings = {
            'libraries'     : ['libzmq'],
            'include_dirs'  : [],
            'library_dirs'  : [],
        }
        if zmq is not None:
            settings['include_dirs'] += [pjoin(zmq, 'include')]
            settings['library_dirs'] += [pjoin(zmq, 'lib')]
    else:
        settings = {
           'libraries'      : ['zmq'],
           'include_dirs'   : [],
           'library_dirs'   : [],
        }
121
    
122 123 124
        # add pthread on freebsd
        if sys.platform.startswith('freebsd'):
            settings['libraries'].append('pthread')
125
    
126 127 128 129 130 131 132
        if zmq is not None:
            settings['include_dirs'] += [pjoin(zmq, 'include')]
            settings['library_dirs'] += [pjoin(zmq, 'lib')]
        elif sys.platform == 'darwin' and os.path.isdir('/opt/local/lib'):
            # allow macports default
            settings['include_dirs'] += ['/opt/local/include']
            settings['library_dirs'] += ['/opt/local/lib']
133
    
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        if bundle_libzmq:
            # bdist should link against bundled libzmq
            settings['library_dirs'] = ['zmq']
            if sys.platform == 'darwin':
                pass
                # unused rpath args for OSX:
                # settings['extra_link_args'] = ['-Wl,-rpath','-Wl,$ORIGIN/..']
            else:
                settings['runtime_library_dirs'] = ['$ORIGIN/..']
        elif sys.platform != 'darwin':
            settings['runtime_library_dirs'] = [os.path.abspath(x) for x in settings['library_dirs']]

    # suppress common warnings

    extra_flags = []
    if ignore_common_warnings:
        for warning in ('unused-function', 'strict-aliasing'):
            extra_flags.append('-Wno-'+warning)

    settings['extra_compile_args'] = extra_flags
    
    # include internal directories
    settings['include_dirs'] += [pjoin('zmq', sub) for sub in ('utils','core','devices')]

    return settings

COMPILER_SETTINGS = settings_from_prefix(ZMQ)
161

162 163 164 165 166

#-----------------------------------------------------------------------------
# Extra commands
#-----------------------------------------------------------------------------

167 168 169 170 171 172 173 174 175
class Configure(Command):
    """Configure command adapted from h5py"""

    description = "Discover ZMQ version and features"

    # DON'T REMOVE: distutils demands these be here even if they do nothing.
    user_options = []
    boolean_options = []
    def initialize_options(self):
176 177 178
        self.zmq = ZMQ
        self.settings = copy.copy(COMPILER_SETTINGS)
    
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    def finalize_options(self):
        pass

    tempdir = 'detect'

    def create_tempdir(self):
        self.erase_tempdir()
        os.mkdir(self.tempdir)
        if sys.platform.startswith('win'):
            # fetch libzmq.dll into local dir
            local_dll = pjoin(self.tempdir, 'libzmq.dll')
            if ZMQ is None and not os.path.exists(local_dll):
                fatal("ZMQ directory must be specified on Windows via setup.cfg or 'python setup.py configure --zmq=/path/to/zeromq2'")
            
            try:
                shutil.copy(pjoin(ZMQ, 'lib', 'libzmq.dll'), local_dll)
            except Exception:
                if not os.path.exists(local_dll):
                    warn("Could not copy libzmq into zmq/, which is usually necessary on Windows."
                    "Please specify zmq prefix via configure --zmq=/path/to/zmq or copy "
                    "libzmq into zmq/ manually.")
            

    def erase_tempdir(self):
        try:
            shutil.rmtree(self.tempdir)
        except Exception:
            pass

    def getcached(self):
        return loadpickle('configure.pickle')

    def check_zmq_version(self):
212
        zmq = self.zmq
213 214 215 216
        if zmq is not None and not os.path.isdir(zmq):
            fatal("Custom zmq directory \"%s\" does not exist" % zmq)

        config = self.getcached()
217
        if config is None or config['options'] != self.settings:
218 219 220 221 222 223 224 225 226 227 228 229 230 231
            self.run()
            config = self.config
        else:
            self.config = config

        vers = config['vers']
        vs = v_str(vers)
        if vers < min_zmq:
            fatal("Detected ZMQ version: %s, but depend on zmq >= %s"%(
                    vs, v_str(min_zmq))
                    +'\n       Using ZMQ=%s'%(zmq or 'unspecified'))
        pyzmq_version = extract_version().strip('abcdefghijklmnopqrstuvwxyz')

        if vs < pyzmq_version:
232
            warn("Detected ZMQ version: %s, but pyzmq targets zmq %s."%(
233
                    vs, pyzmq_version))
234 235 236 237
            warn("libzmq features and fixes introduced after %s will be unavailable."%vs)
            print('*'*42)
        elif vs >= '3.0':
            warn("Detected ZMQ version: %s. pyzmq's support for libzmq-dev is experimental."%vs)
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
            print('*'*42)

        if sys.platform.startswith('win'):
            # fetch libzmq.dll into local dir
            local_dll = localpath('zmq','libzmq.dll')
            if zmq is None and not os.path.exists(local_dll):
                fatal("ZMQ directory must be specified on Windows via setup.cfg or 'python setup.py configure --zmq=/path/to/zeromq2'")
            try:
                shutil.copy(pjoin(zmq, 'lib', 'libzmq.dll'), local_dll)
            except Exception:
                if not os.path.exists(local_dll):
                    warn("Could not copy libzmq into zmq/, which is usually necessary on Windows."
                    "Please specify zmq prefix via configure --zmq=/path/to/zmq or copy "
                    "libzmq into zmq/ manually.")

    def run(self):
        self.create_tempdir()
255 256
        settings = self.settings
        if bundle_libzmq and not sys.platform.startswith('win'):
257 258 259 260 261 262 263 264 265 266 267
            # rpath slightly differently here, because libzmq not in .. but ../zmq:
            settings['library_dirs'] = ['zmq']
            if sys.platform == 'darwin':
                pass
                # unused rpath args for OSX:
                # settings['extra_link_args'] = ['-Wl,-rpath','-Wl,$ORIGIN/../zmq']
            else:
                settings['runtime_library_dirs'] = ['$ORIGIN/../zmq']
        try:
            print ("*"*42)
            print ("Configure: Autodetecting ZMQ settings...")
268
            print ("    Custom ZMQ dir:       %s" % (self.zmq,))
269 270
            config = detect_zmq(self.tempdir, **settings)
        except Exception:
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
            # if zmq unspecified on *ix, try again with explicit /usr/local
            if self.zmq is None and not sys.platform.startswith('win'):
                self.erase_tempdir()
                print ("Failed with default libzmq, trying again with /usr/local")
                self.zmq = '/usr/local'
                self.settings = settings_from_prefix(self.zmq)
                
                self.run()
                # if we get here the second run succeeded, so we need to update compiler
                # settings for the extensions with /usr/local prefix
                for ext in self.distribution.ext_modules:
                    for key,value in self.settings.iteritems():
                        setattr(ext, key, value)
                return
            
286 287 288
            etype, evalue, tb = sys.exc_info()
            # print the error as distutils would if we let it raise:
            print ("error: %s" % evalue)
289 290 291 292 293
            if etype is CompileError:
                action = 'compile'
            elif etype is LinkError:
                action = 'link'
            else:
294
                action = 'build or run'
295
            
296
            fatal("""
297
    Failed to %s ZMQ test program.  Please check to make sure:
298 299 300

    * You have a C compiler installed
    * A development version of Python is installed (including header files)
301 302 303 304 305 306
    * A development version of ZMQ >= %s is installed (including header files)
    * If ZMQ is not in a default location, supply the argument --zmq=<path>
    * If you did recently install ZMQ to a default location, 
      try rebuilding the ld cache with `sudo ldconfig`
      or specify zmq's location with `--zmq=/usr/local`
    """%(action, v_str(min_zmq)))
307 308 309 310 311 312 313 314 315
            
        else:
            savepickle('configure.pickle', config)
            print ("    ZMQ version detected: %s" % v_str(config['vers']))
        finally:
            print ("*"*42)
            self.erase_tempdir()
        self.config = config

316 317 318 319 320 321 322 323 324 325
class TestCommand(Command):
    """Custom distutils command to run the test suite."""

    user_options = [ ]

    def initialize_options(self):
        self._dir = os.getcwd()

    def finalize_options(self):
        pass
326 327 328
    
    def run_nose(self):
        """Run the test suite with nose."""
329
        return nose.core.TestProgram(argv=["", '-vv', pjoin(self._dir, 'zmq', 'tests')])
330 331 332
    
    def run_unittest(self):
        """Finds all the tests modules in zmq/tests/ and runs them."""
333 334
        testfiles = [ ]
        for t in glob(pjoin(self._dir, 'zmq', 'tests', '*.py')):
335 336
            name = splitext(basename(t))[0]
            if name.startswith('test_'):
337
                testfiles.append('.'.join(
338
                    ['zmq.tests', name])
339 340
                )
        tests = TestLoader().loadTestsFromNames(testfiles)
341
        t = TextTestRunner(verbosity = 2)
342
        t.run(tests)
343 344 345 346 347 348 349 350
    
    def run(self):
        """Run the test suite, with nose, or unittest if nose is unavailable"""
        # crude check for inplace build:
        try:
            import zmq
        except ImportError:
            print_exc()
351 352 353
            fatal('\n       '.join(["Could not import zmq!",
            "You must build pyzmq with 'python setup.py build_ext --inplace' for 'python setup.py test' to work.",
            "If you did build pyzmq in-place, then this is a real error."]))
354 355 356 357 358 359 360
            sys.exit(1)
        
        if nose is None:
            print ("nose unavailable, falling back on unittest. Skipped tests will appear as ERRORs.")
            return self.run_unittest()
        else:
            return self.run_nose()
361

362 363 364 365 366 367
class GitRevisionCommand(Command):
    """find the current git revision and add it to zmq.core.verion.__revision__"""
    
    user_options = [ ]
    
    def initialize_options(self):
368
        self.version_py = pjoin('zmq','core','version.py')
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
    
    def run(self):
        try:
            p = Popen('git log -1'.split(), stdin=PIPE, stdout=PIPE, stderr=PIPE)
        except IOError:
            print ("No git found, skipping git revision")
            return
        
        if p.wait():
            print ("checking git branch failed")
            print (p.stderr.read())
            return
        
        line = p.stdout.readline().decode().strip()
        if not line.startswith('commit'):
            print ("bad commit line: %r"%line)
            return
        
        rev = line.split()[-1]
        
389 390
        # now that we have the git revision, we can apply it to version.py
        with open(self.version_py) as f:
391 392 393 394 395 396
            lines = f.readlines()
        
        for i,line in enumerate(lines):
            if line.startswith('__revision__'):
                lines[i] = "__revision__ = '%s'\n"%rev
                break
397
        with open(self.version_py, 'w') as f:
398 399 400 401
            f.writelines(lines)
    
    def finalize_options(self):
        pass
402 403 404 405 406 407 408

class CleanCommand(Command):
    """Custom distutils command to clean the .so and .pyc files."""

    user_options = [ ]

    def initialize_options(self):
409
        self._clean_me = []
410 411
        self._clean_trees = []
        for root, dirs, files in list(os.walk('zmq')):
412
            for f in files:
413
                if os.path.splitext(f)[-1] in ('.pyc', '.so', '.o', '.pyd'):
414
                    self._clean_me.append(pjoin(root, f))
415 416 417 418
            for d in dirs:
                if d == '__pycache__':
                    self._clean_trees.append(pjoin(root, d))
        
419
        for d in ('build',):
420 421
            if os.path.exists(d):
                self._clean_trees.append(d)
422

423 424 425 426
        bundled = glob(pjoin('zmq', 'libzmq*'))
        self._clean_me.extend(bundled)
        

427 428 429 430 431 432 433 434

    def finalize_options(self):
        pass

    def run(self):
        for clean_me in self._clean_me:
            try:
                os.unlink(clean_me)
435 436 437 438 439 440
            except Exception:
                pass
        for clean_tree in self._clean_trees:
            try:
                shutil.rmtree(clean_tree)
            except Exception:
441 442
                pass

443 444 445 446 447 448 449

class CheckSDist(sdist):
    """Custom sdist that ensures Cython has compiled all pyx files to c."""

    def initialize_options(self):
        sdist.initialize_options(self)
        self._pyxfiles = []
450
        for root, dirs, files in os.walk('zmq'):
451 452 453 454
            for f in files:
                if f.endswith('.pyx'):
                    self._pyxfiles.append(pjoin(root, f))
    def run(self):
455 456 457 458 459 460 461 462
        if 'cython' in cmdclass:
            self.run_command('cython')
        else:
            for pyxfile in self._pyxfiles:
                cfile = pyxfile[:-3]+'c'
                msg = "C-source file '%s' not found."%(cfile)+\
                " Run 'setup.py cython' before sdist."
                assert os.path.isfile(cfile), msg
463 464
        sdist.run(self)

465 466 467 468
class CopyingBuild(build):
    """subclass of build that copies libzmq if doing bdist."""
    
    def run(self):
469
        if bundle_libzmq and not sys.platform.startswith('win'):
470 471 472 473 474
            # always rebuild before bdist, because linking may be wrong:
            self.run_command('clean')
            copy_and_patch_libzmq(ZMQ, 'libzmq'+lib_ext)
        build.run(self)

475 476 477 478 479 480 481
class CheckingBuildExt(build_ext):
    """Subclass build_ext to get clearer report if Cython is neccessary."""
    
    def check_cython_extensions(self, extensions):
        for ext in extensions:
          for src in ext.sources:
            if not os.path.exists(src):
482
                fatal("""Cython-generated file '%s' not found.
483 484 485
                Cython is required to compile pyzmq from a development branch.
                Please install Cython or download a release package of pyzmq.
                """%src)
486
    
487 488 489
    def build_extensions(self):
        self.check_cython_extensions(self.extensions)
        self.check_extensions_list(self.extensions)
490
        
491 492
        for ext in self.extensions:
            self.build_extension(ext)
493 494 495 496 497 498 499
    
    def run(self):
        # check version, to prevent confusing undefined constant errors
        configure = self.distribution.get_command_obj('configure')
        configure.check_zmq_version()
        build_ext.run(self)
    
500

501 502 503 504
#-----------------------------------------------------------------------------
# Extensions
#-----------------------------------------------------------------------------

505 506
cmdclass = {'test':TestCommand, 'clean':CleanCommand, 'revision':GitRevisionCommand,
            'configure': Configure, 'build': CopyingBuild}
507

508 509 510 511 512 513 514 515 516
def pxd(subdir, name):
    return os.path.abspath(pjoin('zmq', subdir, name+'.pxd'))

def pyx(subdir, name):
    return os.path.abspath(pjoin('zmq', subdir, name+'.pyx'))

def dotc(subdir, name):
    return os.path.abspath(pjoin('zmq', subdir, name+'.c'))

517
libzmq = pxd('core', 'libzmq')
518
buffers = pxd('utils', 'buffers')
519 520 521 522
message = pxd('core', 'message')
context = pxd('core', 'context')
socket = pxd('core', 'socket')
monqueue = pxd('devices', 'monitoredqueue')
523 524

submodules = dict(
525 526
    core = {'constants': [libzmq],
            'error':[libzmq],
527
            '_poll':[libzmq, socket, context],
528 529 530 531 532
            'stopwatch':[libzmq, pxd('core','stopwatch')],
            'context':[context, libzmq],
            'message':[libzmq, buffers, message],
            'socket':[context, message, socket, libzmq, buffers],
            'device':[libzmq, socket, context],
533
            '_version':[libzmq],
534 535
    },
    devices = {
536
            'monitoredqueue':[buffers, libzmq, monqueue, socket, context],
537 538
    },
    utils = {
539 540
            'initthreads':[libzmq],
            'rebuffer':[buffers],
541
    },
542 543
)

544 545
try:
    from Cython.Distutils import build_ext
546
    cython=True
547
except ImportError:
548
    cython=False
549 550
    suffix = '.c'
    cmdclass['build_ext'] = CheckingBuildExt
551
else:
552 553 554 555 556 557 558 559 560 561
    
    suffix = '.pyx'
    
    class CythonCommand(build_ext):
        """Custom distutils command subclassed from Cython.Distutils.build_ext
        to compile pyx->c, and stop there. All this does is override the 
        C-compile method build_extension() with a no-op."""
        def build_extension(self, ext):
            pass
    
562 563 564 565 566 567
    class zbuild_ext(build_ext):
        def run(self):
            configure = self.distribution.get_command_obj('configure')
            configure.check_zmq_version()
            return build_ext.run(self)
    
568
    cmdclass['cython'] = CythonCommand
569
    cmdclass['build_ext'] =  zbuild_ext
570
    cmdclass['sdist'] =  CheckSDist
571

572 573 574 575 576 577 578 579 580
extensions = []
for submod, packages in submodules.items():
    for pkg in sorted(packages):
        sources = [pjoin('zmq', submod, pkg+suffix)]
        if suffix == '.pyx':
            sources.extend(packages[pkg])
        ext = Extension(
            'zmq.%s.%s'%(submod, pkg),
            sources = sources,
581
            **COMPILER_SETTINGS
582 583
        )
        extensions.append(ext)
584

585 586 587 588 589 590 591
#
package_data = {'zmq':['*.pxd'],
                'zmq.core':['*.pxd'],
                'zmq.devices':['*.pxd'],
                'zmq.utils':['*.pxd', '*.h'],
}

592
if bundle_libzmq:
593 594 595
    package_data['zmq'].append('libzmq'+lib_ext)

def extract_version():
596 597
    """extract pyzmq version from core/version.py, so it's not multiply defined"""
    with open(pjoin('zmq', 'core', 'version.py')) as f:
598 599 600 601 602 603 604 605 606 607 608
        line = f.readline()
        while not line.startswith("__version__"):
            line = f.readline()
    exec(line, globals())
    if 'bdist_msi' in sys.argv:
        # msi has strict version requirements, which requires that
        # we strip any dev suffix
        return re.match(r'\d+(\.\d+)+', __version__).group()
    else:
        return __version__

609 610 611 612 613 614 615 616 617 618 619
def find_packages():
    """adapted from IPython's setupbase.find_packages()"""
    packages = []
    for dir,subdirs,files in os.walk('zmq'):
        package = dir.replace(os.path.sep, '.')
        if '__init__.py' not in files:
            # not a package
            continue
        packages.append(package)
    return packages

620 621 622 623
#-----------------------------------------------------------------------------
# Main setup
#-----------------------------------------------------------------------------

624 625 626 627 628 629
long_desc = \
"""
PyZMQ is a lightweight and super-fast messaging library built on top of
the ZeroMQ library (http://www.zeromq.org). 
"""

630 631
setup(
    name = "pyzmq",
632
    version = extract_version(),
633
    packages = find_packages(),
634 635
    ext_modules = extensions,
    package_data = package_data,
636 637
    author = "Brian E. Granger, Min Ragan-Kelley",
    author_email = "zeromq-dev@lists.zeromq.org",
638 639 640 641
    url = 'http://github.com/zeromq/pyzmq',
    download_url = 'http://github.com/zeromq/pyzmq/downloads',
    description = "Python bindings for 0MQ.",
    long_description = long_desc, 
642
    license = "LGPL+BSD",
643 644 645 646 647 648 649 650
    cmdclass = cmdclass,
    classifiers = [
        'Development Status :: 5 - Production/Stable',
        'Intended Audience :: Developers',
        'Intended Audience :: Financial and Insurance Industry',
        'Intended Audience :: Science/Research',
        'Intended Audience :: System Administrators',
        'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)',
651
        'License :: OSI Approved :: BSD License',
652 653 654
        'Operating System :: MacOS :: MacOS X',
        'Operating System :: Microsoft :: Windows',
        'Operating System :: POSIX',
655 656 657 658 659 660 661 662
        'Topic :: System :: Networking',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.0',
        'Programming Language :: Python :: 3.1',
        'Programming Language :: Python :: 3.2',
663
    ]
664 665
)