~ubuntu-branches/ubuntu/wily/pyzmq/wily

« back to all changes in this revision

Viewing changes to buildutils/detect.py

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2013-02-24 19:23:15 UTC
  • mfrom: (1.2.1) (9 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: package-import@ubuntu.com-20130224192315-qhmwp3m3ymk8r60d
Tags: 2.2.0.1-1
* New upstream release
* relicense debian packaging to LGPL-3
* update watch file to use github directly
  thanks to Bart Martens for the file
* add autopkgtests
* drop obsolete DM-Upload-Allowed
* bump standard to 3.9.4, no changes required

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Detect zmq version"""
 
2
#-----------------------------------------------------------------------------
 
3
#  Copyright (C) 2011 Brian Granger, Min Ragan-Kelley
 
4
#
 
5
#  This file is part of pyzmq, copied and adapted from h5py.
 
6
#  h5py source used under the New BSD license
 
7
#
 
8
#  h5py: <http://code.google.com/p/h5py/>
 
9
#
 
10
#  Distributed under the terms of the New BSD License.  The full license is in
 
11
#  the file COPYING.BSD, distributed as part of this software.
 
12
#-----------------------------------------------------------------------------
 
13
 
 
14
import shutil
 
15
import sys
 
16
import os
 
17
import logging
 
18
import platform
 
19
from distutils import ccompiler
 
20
from distutils.sysconfig import customize_compiler
 
21
from subprocess import Popen, PIPE
 
22
 
 
23
pjoin = os.path.join
 
24
 
 
25
#-----------------------------------------------------------------------------
 
26
# Utility functions (adapted from h5py: http://h5py.googlecode.com)
 
27
#-----------------------------------------------------------------------------
 
28
 
 
29
def detect_zmq(basedir, **compiler_attrs):
 
30
    """Compile, link & execute a test program, in empty directory `basedir`.
 
31
    
 
32
    The C compiler will be updated with any keywords given via setattr.
 
33
    
 
34
    Parameters
 
35
    ----------
 
36
    
 
37
    basedir : path
 
38
        The location where the test program will be compiled and run
 
39
    **compiler_attrs : dict
 
40
        Any extra compiler attributes, which will be set via ``setattr(cc)``.
 
41
    
 
42
    Returns
 
43
    -------
 
44
    
 
45
    A dict of properties for zmq compilation, with the following two keys:
 
46
    
 
47
    vers : tuple
 
48
        The ZMQ version as a tuple of ints, e.g. (2,2,0)
 
49
    settings : dict
 
50
        The compiler options used to compile the test function, e.g. `include_dirs`,
 
51
        `library_dirs`, `libs`, etc.
 
52
    """
 
53
 
 
54
    cc = ccompiler.new_compiler()
 
55
    customize_compiler(cc)
 
56
    for name, val in compiler_attrs.items():
 
57
        setattr(cc, name, val)
 
58
 
 
59
    cfile = pjoin(basedir, 'vers.c')
 
60
    efile = pjoin(basedir, 'vers')
 
61
    
 
62
    shutil.copy(pjoin(os.path.dirname(__file__), 'vers.c'), cfile)
 
63
 
 
64
    cpreargs = lpreargs = None
 
65
    if sys.platform == 'darwin':
 
66
        # use appropriate arch for compiler
 
67
        if platform.architecture()[0]=='32bit':
 
68
            cpreargs = ['-arch','i386']
 
69
            lpreargs = ['-arch', 'i386', '-undefined', 'dynamic_lookup']
 
70
        else:
 
71
            # allow for missing UB arch, since it will still work:
 
72
            lpreargs = ['-undefined', 'dynamic_lookup']
 
73
 
 
74
    objs = cc.compile([cfile],extra_preargs=cpreargs)
 
75
    cc.link_executable(objs, efile, extra_preargs=lpreargs)
 
76
 
 
77
    result = Popen(efile, stdout=PIPE, stderr=PIPE)
 
78
    so, se = result.communicate()
 
79
    # for py3k:
 
80
    so = so.decode()
 
81
    se = se.decode()
 
82
    if result.returncode:
 
83
        msg = "Error running version detection script:\n%s\n%s" % (so,se)
 
84
        logging.error(msg)
 
85
        raise IOError(msg)
 
86
 
 
87
    handlers = {'vers':  lambda val: tuple(int(v) for v in val.split('.'))}
 
88
 
 
89
    props = {}
 
90
    for line in (x for x in so.split('\n') if x):
 
91
        key, val = line.split(':')
 
92
        props[key] = handlers[key](val)
 
93
 
 
94
    props['settings'] = compiler_attrs
 
95
    return props
 
96