setup.py 5.47 KB
Newer Older
1
2
3
4
5
6
7
#!/usr/bin/env python

import sys, subprocess, os
from distutils.core import setup
from distutils.extension import Extension
import numpy as np

8
9
10
11
12
13
14
run_cython = '--run-cython' in sys.argv
if run_cython:
    sys.argv.remove('--run-cython')
    from Cython.Distutils import build_ext
    cmdclass = {'build_ext': build_ext}
else:
    cmdclass = {}
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

# This is an exact copy of the function from kwant/version.py.  We can't import
# it here (because kwant is not yet built when this scipt is run), so we just
# include a copy.
def get_version_from_git():
    kwant_dir = os.path.dirname(os.path.abspath(__file__))
    try:
        p = subprocess.Popen(['git', 'describe'], cwd=kwant_dir,
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    except OSError:
        return

    if p.wait() != 0:
        return
    version = p.communicate()[0].strip()

    if version[0] == 'v':
        version = version[1:]

    try:
        p = subprocess.Popen(['git', 'diff', '--quiet'], cwd=kwant_dir)
    except OSError:
        version += '-confused'  # This should never happen.
    else:
        if p.wait() == 1:
            version += '-dirty'
    return version

def get_static_version():
    try:
        with open('kwant/_static_version.py') as f:
            contents = f.read()
            assert contents[:11] == "version = '"
            assert contents[-2:] == "'\n"
            return contents[11:-2]
    except:
        return None

git_version = get_version_from_git()
static_version = get_static_version()
if git_version is not None:
    version = git_version
    if static_version != git_version:
        with open('kwant/_static_version.py', 'w') as f:
            f.write("version = '%s'\n" % version)
elif static_version is not None:
    version = static_version
else:
    version = 'unknown'

# List of tuples (args, keywords) to be passed to Extension, possibly after
# replacing ".pyx" with ".c" if Cython is not to be used.
extensions = [ # (["kwant.graph.scotch", ["kwant/graph/scotch.pyx"]],
               #  {"libraries" : ["scotch", "scotcherr"]}),
69
70
               (["kwant._system", ["kwant/_system.pyx"]],
                {"include_dirs" : ["kwant/graph"]}),
71
72
73
74
75
76
               (["kwant.graph.core", ["kwant/graph/core.pyx"]],
                {"depends" : ["kwant/graph/core.pxd", "kwant/graph/defs.h",
                              "kwant/graph/defs.pxd"]}),
               (["kwant.graph.utils", ["kwant/graph/utils.pyx"]],
                {"depends" : ["kwant/graph/defs.h", "kwant/graph/defs.pxd",
                              "kwant/graph/core.pxd"]}),
77
78
79
               (["kwant.graph.slicer", ["kwant/graph/slicer.pyx",
                                        "kwant/graph/c_slicer/partitioner.cc",
                                        "kwant/graph/c_slicer/slicer.cc"]],
80
81
82
83
84
85
86
                {"depends" : ["kwant/graph/defs.h", "kwant/graph/defs.pxd",
                              "kwant/graph/core.pxd",
                              "kwant/graph/c_slicer.pxd",
                              "kwant/graph/c_slicer/bucket_list.h",
                              "kwant/graph/c_slicer/graphwrap.h",
                              "kwant/graph/c_slicer/partitioner.h",
                              "kwant/graph/c_slicer/slicer.h"]}),
87
               (["kwant.linalg.lapack", ["kwant/linalg/lapack.pyx"]],
88
                {"libraries" : ["lapack", "blas"],
89
90
91
92
93
94
                 "depends" : ["kwant/linalg/f_lapack.pxd"]}),
               (["kwant.linalg._mumps", ["kwant/linalg/_mumps.pyx"]],
                {"libraries" : ["zmumps", "mumps_common", "pord",
                                "metis", "mpiseq", "lapack", "blas",
                                "gfortran"],
                 "depends" : ["kwant/linalg/cmumps.pxd"]}) ]
95
96
97

ext_modules = []
for args, keywords in extensions:
98
    if not run_cython:
99
100
101
102
103
104
105
106
107
108
        if 'language' in keywords:
            if keywords['language'] == 'c':
                ext = '.c'
            elif keywords['language'] == 'c++':
                ext = '.cpp'
            else:
                print >>sys.stderr, 'Unknown language'
                exit(1)
        else:
            ext = '.c'
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
        pyx_files = []
        cythonized_files = []
        sources = []
        for f in args[1]:
            if f[-4:] == '.pyx':
                pyx_files.append(f)
                f = f[:-4] + ext
                cythonized_files.append(f)
            sources.append(f)
        args[1] = sources

        try:
            cythonized_oldest = min(os.stat(f).st_mtime
                                    for f in cythonized_files)
        except OSError:
            msg = "{0} is missing. Run `./setup.py --run-cython build'."
            print >>sys.stderr, msg.format(f)
            exit(1)
        for f in pyx_files + keywords.get('depends', []):
            if os.stat(f).st_mtime > cythonized_oldest:
                msg = "{0} has been modified. " \
                "Run `./setup.py --run-cython build'."
                print >>sys.stderr, msg.format(f)
                exit(1)

134
    ext_modules.append(Extension(*args, **keywords))
135
136

include_dirs = [np.get_include()]
137
138
139
140
141
142
143
144
145
146
147
148

setup(name='kwant',
      version=version,
      author='A. R. Akhmerov, C. W. Groth, X. Waintal, M. Wimmer',
      author_email='cwg@falma.de',
      description="A package for numerical quantum transport calculations.",
      license="not to be distributed",
      packages=["kwant", "kwant.graph", "kwant.linalg", "kwant.physics",
                "kwant.solvers"],
      cmdclass=cmdclass,
      ext_modules=ext_modules,
      include_dirs = include_dirs)