summaryrefslogtreecommitdiff
path: root/bscript
blob: 2f608f81e77129ce0ea6f6e02c9a57e32c440001 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
See BENTO_BUILD.txt.

Caveats:

    - no automatic detection for BLAS/LAPACK/etc... You need to set it up
      manually for now (except on Mac OS X and Debian/Ubuntu). The upside is
      that it is extremely easy to do so
    - bento is still in flux, and some things may changes between releases.
"""
    
import os
import sys
import subprocess

# Ugly but necessary hack: import numpy here so that wscript in sub directories
# will see this numpy and not an already installed one
import __builtin__
__builtin__.__NUMPY_SETUP__ = True

from bento.commands import hooks
from bento.utils.utils \
    import \
        cmd_is_runnable
from bento.backends.waf_backend \
    import \
        WAF_TOOLDIR

import waflib
from waflib import Options

sys.path.insert(0, os.getcwd())
try:
    _SETUP_PY = __import__("setup")
finally:
    sys.path.pop(0)

import collections
_PLATFORM_TO_DEFAULT = collections.defaultdict(lambda: "atlas")
_PLATFORM_TO_DEFAULT.update({
        "win32": "mkl",
        "darwin": "accelerate",
})

_OPTIMIZED_CBLAS_TO_KWARGS = {
        "mkl": {"lib": "mkl_intel_c,mkl_intel_thread,mkl_core,libiomp5md".split(",")},
        "atlas": {"lib": ["cblas", "atlas"]},
        "accelerate": {"framework": ["Accelerate"]},
        "openblas": {"lib": ["openblas"]},
}

_OPTIMIZED_LAPACK_TO_KWARGS = {
        "mkl": {"lib": "mkl_lapack95,mkl_blas95,mkl_intel_c,mkl_intel_thread,mkl_core,libiomp5md".split(",")},
        "atlas": {"lib": ["lapack", "f77blas", "cblas", "atlas"]},
        "accelerate": {"framework": ["Accelerate"]},
        "openblas": {"lib": ["openblas"]},
}

def get_optimized_name(context):
    o, a = context.options_context.parser.parse_args(context.command_argv)
    if o.blas_lapack_type == "default" or o.blas_lapack_type is None:
        optimized = _PLATFORM_TO_DEFAULT[sys.platform]
    else:
        optimized = o.blas_lapack_type

    return optimized

def check_cblas(context, optimized):
    conf = context.waf_context

    msg = "Checking for %s (CBLAS)" % optimized.upper()

    kwargs = _OPTIMIZED_CBLAS_TO_KWARGS[optimized]
    kwargs.update({"msg": msg, "uselib_store": "CBLAS"})

    try:
        conf.check_cc(**kwargs)
        conf.env.HAS_CBLAS = True
    except waflib.Errors.ConfigurationError:
        conf.env.HAS_CBLAS = False

def check_lapack(context, optimized):
    conf = context.waf_context

    msg = "Checking for %s (LAPACK)" % optimized.upper()
    if optimized in ["openblas", "atlas"]:
        check_fortran(context)

    kwargs = _OPTIMIZED_LAPACK_TO_KWARGS[optimized]
    kwargs.update({"msg": msg, "uselib_store": "LAPACK"})

    try:
        conf.check_cc(**kwargs)
        conf.env.HAS_LAPACK = True
    except waflib.Errors.ConfigurationError:
        conf.env.HAS_LAPACK = False

def check_blas_lapack(context):
    optimized = get_optimized_name(context)

    o, a = context.options_context.parser.parse_args(context.command_argv)
    if o.blas_lapack_libdir:
        context.waf_context.env.append_value("LIBPATH", o.blas_lapack_libdir)

    check_cblas(context, optimized)
    check_lapack(context, optimized)

    # You can manually set up blas/lapack as follows:
    #conf.env.HAS_CBLAS = True
    #conf.env.LIB_CBLAS = ["cblas", "atlas"]
    #conf.env.HAS_LAPACK = True
    #conf.env.LIB_LAPACK = ["lapack", "f77blas", "cblas", "atlas"]

def compute_git_revision(top_node):
    git_repo_node = top_node.find_node(".git")
    if git_repo_node and cmd_is_runnable(["git", "--version"]):
        s = subprocess.Popen(["git", "rev-parse", "HEAD"],
                stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=top_node.abspath())
        out = s.communicate()[0]
        return out.decode().strip()
    else:
        return ""

def _register_metadata(context):
    git_revision = compute_git_revision(context.top_node)
    full_version = context.pkg.version
    if not _SETUP_PY.ISRELEASED:
        full_version += '.dev-' + git_revision[:7]

    context.register_metadata("git_revision", git_revision)
    context.register_metadata("is_released", _SETUP_PY.ISRELEASED)
    context.register_metadata("full_version", full_version)

def check_fortran(context):
    opts = context.waf_options_context
    conf = context.waf_context

    opts.load("compiler_fc")
    Options.options.check_fc = "gfortran"

    conf.load("compiler_fc")
    conf.load("ordered_c", tooldir=[WAF_TOOLDIR])

    conf.check_fortran_verbose_flag()
    conf.check_fortran_clib()

@hooks.post_configure
def post_configure(context):
    conf = context.waf_context
    if conf.env["CC_NAME"] == "gcc":
        conf.env.CFLAGS_PYEXT.append("-Wfatal-errors")

    conf.load("arch", tooldir=[WAF_TOOLDIR])
    if sys.platform == "darwin":
        conf.env["MACOSX_DEPLOYMENT_TARGET"] = "10.4"
        conf.check_cc_default_arch()
        archs = [conf.env.DEFAULT_CC_ARCH]
        conf.env.ARCH = archs

    check_blas_lapack(context)

@hooks.pre_build
def pre_build(context):
    _register_metadata(context)

@hooks.pre_sdist
def pre_sdist(context):
    _register_metadata(context)

@hooks.options
def options(global_context):
    from bento.commands.options import Option

    global_context.add_option_group("configure", "blas_lapack", "blas/lapack")

    available_optimized = ",".join(_OPTIMIZED_LAPACK_TO_KWARGS.keys())
    global_context.add_option("configure",
            Option("--blas-lapack-type", help="Which blas lapack to use (%s)" % available_optimized),
            "blas_lapack")

    global_context.add_option("configure",
            Option("--with-blas-lapack-libdir", dest="blas_lapack_libdir",
                   help="Where to look for BLAS/LAPACK dir"),
            "blas_lapack")