summaryrefslogtreecommitdiff
path: root/numpy/core/SConscript
blob: 569c33bbaf68126ce52d7e19dc2d0da5106f709a (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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Last Change: Fri Oct 03 04:00 PM 2008 J
# vim:syntax=python
import os
import sys
from os.path import join as pjoin, basename as pbasename, dirname as pdirname
from copy import deepcopy

from numscons import get_pythonlib_dir
from numscons import GetNumpyEnvironment
from numscons import CheckCBLAS
from numscons import write_info

from scons_support import CheckBrokenMathlib, define_no_smp, \
    check_mlib, check_mlibs, is_npy_no_signal
from scons_support import array_api_gen_bld, ufunc_api_gen_bld, template_bld, \
                          umath_bld


env = GetNumpyEnvironment(ARGUMENTS)
env.Append(CPPPATH = env["PYEXTCPPPATH"])
if os.name == 'nt':
    # NT needs the pythonlib to run any code importing Python.h, including
    # simple code using only typedef and so on, so we need it for configuration
    # checks
    env.AppendUnique(LIBPATH = [get_pythonlib_dir()])

#=======================
# Starting Configuration
#=======================
config = env.NumpyConfigure(custom_tests = {'CheckBrokenMathlib' : CheckBrokenMathlib,
    'CheckCBLAS' : CheckCBLAS}, config_h = pjoin('config.h'))

# numpyconfig_sym will keep the values of some configuration variables, the one
# needed for the public numpy API.

# Convention: list of tuples (definition, value). value:
# - 0: #undef definition
# - 1: #define definition
# - string: #define definition value
numpyconfig_sym = []

#---------------
# Checking Types
#---------------
if not config.CheckHeader("Python.h"):
    errmsg = []
    for line in config.GetLastError():
        errmsg.append("%s " % line)
    print """
Error: Python.h header cannot be compiled (or cannot be found).
On linux, check that you have python-dev/python-devel packages. On windows,
check that you have he platform SDK. You may also use unsupported cflags.
Configuration error log says: \n\n%s""" % ''.join(errmsg)
    Exit(-1)

def check_type(type, include = None):
    st = config.CheckTypeSize(type, includes = include)
    type = type.replace(' ', '_')
    if st:
        numpyconfig_sym.append(('SIZEOF_%s' % type.upper(), '%d' % st))
    else:
        numpyconfig_sym.append(('SIZEOF_%s' % type.upper(), 0))

for type in ('short', 'int', 'long', 'float', 'double', 'long double'):
    check_type(type)

for type in ('Py_intptr_t',):
    check_type(type, include = "#include <Python.h>\n")

# We check declaration AND type because that's how distutils does it.
if config.CheckDeclaration('PY_LONG_LONG', includes = '#include <Python.h>\n'):
    st = config.CheckTypeSize('PY_LONG_LONG',
                              includes = '#include <Python.h>\n')
    assert not st == 0
    numpyconfig_sym.append(('DEFINE_NPY_SIZEOF_LONGLONG',
                            '#define NPY_SIZEOF_LONGLONG %d' % st))
    numpyconfig_sym.append(('DEFINE_NPY_SIZEOF_PY_LONG_LONG',
                            '#define NPY_SIZEOF_PY_LONG_LONG %d' % st))
else:
    numpyconfig_sym.append(('DEFINE_NPY_SIZEOF_LONGLONG', ''))
    numpyconfig_sym.append(('DEFINE_NPY_SIZEOF_PY_LONG_LONG', ''))

if not config.CheckDeclaration('CHAR_BIT', includes= '#include <Python.h>\n'):
    raise RuntimeError(\
"""Config wo CHAR_BIT is not supported with scons: please contact the
maintainer (cdavid)""")

#----------------------
# Checking signal stuff
#----------------------
if is_npy_no_signal():
    numpyconfig_sym.append(('DEFINE_NPY_NO_SIGNAL', '#define NPY_NO_SIGNAL\n'))
    config.Define('__NPY_PRIVATE_NO_SIGNAL',
                  comment = "define to 1 to disable SMP support ")
else:
    numpyconfig_sym.append(('DEFINE_NPY_NO_SIGNAL', ''))

#---------------------
# Checking SMP option
#---------------------
if define_no_smp():
    nosmp = 1
else:
    nosmp = 0
numpyconfig_sym.append(('NPY_NO_SMP', nosmp))

#----------------------------------------------
# Check whether we can use C99 printing formats
#----------------------------------------------
if config.CheckDeclaration(('PRIdPTR'), includes  = '#include <inttypes.h>'):
    usec99 = 1
else:
    usec99 = 0
numpyconfig_sym.append(('USE_C99_FORMATS', usec99))
    
#----------------------
# Checking the mathlib
#----------------------
mlibs = [[], ['m'], ['cpml']]
mathlib = os.environ.get('MATHLIB')
if mathlib:
    mlibs.insert(0, mathlib)

mlib = check_mlibs(config, mlibs)

# XXX: this is ugly: mathlib has nothing to do in a public header file
numpyconfig_sym.append(('MATHLIB', ','.join(mlib)))

#----------------------------------
# Checking the math funcs available
#----------------------------------
# Function to check:
mfuncs = ('expl', 'expf', 'log1p', 'expm1', 'asinh', 'atanhf', 'atanhl',
          'isnan', 'isinf', 'rint', 'trunc')

# Set value to 1 for each defined function (in math lib)
mfuncs_defined = dict([(f, 0) for f in mfuncs])

# Check for mandatory funcs: we barf if a single one of those is not there
mandatory_funcs = ["sin", "cos", "tan", "sinh", "cosh", "tanh", "fabs",
"floor", "ceil", "sqrt", "log10", "log", "exp", "asin", "acos", "atan", "fmod",
'modf', 'frexp', 'ldexp']

if not config.CheckFuncsAtOnce(mandatory_funcs):
    raise SystemError("One of the required function to build numpy is not"
            " available (the list is %s)." % str(mandatory_funcs))

# Standard functions which may not be available and for which we have a
# replacement implementation
#
def check_funcs(funcs):
    # Use check_funcs_once first, and if it does not work, test func per
    # func. Return success only if all the functions are available
    st = config.CheckFuncsAtOnce(funcs)
    if not st:
        # Global check failed, check func per func
        for f in funcs:
            st = config.CheckFunc(f, language = 'C')

# XXX: we do not test for hypot because python checks for it (HAVE_HYPOT in
# python.h... I wish they would clean their public headers someday)
optional_stdfuncs = ["expm1", "log1p", "acosh", "asinh", "atanh",
                     "rint", "trunc"]

check_funcs(optional_stdfuncs)

# C99 functions: float and long double versions
c99_funcs = ["sin", "cos", "tan", "sinh", "cosh", "tanh", "fabs", "floor",
             "ceil", "rint", "trunc", "sqrt", "log10", "log", "exp",
             "expm1", "asin", "acos", "atan", "asinh", "acosh", "atanh",
             "hypot", "atan2", "pow", "fmod", "modf", 'frexp', 'ldexp']

for prec in ['l', 'f']:
    fns = [f + prec for f in c99_funcs]
    check_funcs(fns)

# Normally, isnan and isinf are macro (C99), but some platforms only have
# func, or both func and macro version. Check for macro only, and define
# replacement ones if not found.
# Note: including Python.h is necessary because it modifies some math.h
# definitions
for f in ["isnan", "isinf", "signbit", "isfinite"]:
    includes = """\
#include <Python.h>
#include <math.h>
"""
    config.CheckDeclaration(f, includes=includes)

#-------------------------------------------------------
# Define the function PyOS_ascii_strod if not available
#-------------------------------------------------------
if not config.CheckDeclaration('PyOS_ascii_strtod',
                               includes = "#include <Python.h>"):
    if config.CheckFunc('strtod'):
        config.Define('PyOS_ascii_strtod', 'strtod',
                      "Define to a function to use as a replacement for "\
                      "PyOS_ascii_strtod if not available in python header")

#------------------------------------
# DISTUTILS Hack on AMD64 on windows
#------------------------------------
# XXX: this is ugly
if sys.platform=='win32' or os.name=='nt':
    from distutils.msvccompiler import get_build_architecture
    a = get_build_architecture()
    print 'BUILD_ARCHITECTURE: %r, os.name=%r, sys.platform=%r' % \
          (a, os.name, sys.platform)
    if a == 'AMD64':
        distutils_use_sdk = 1
        config.Define('DISTUTILS_USE_SDK', distutils_use_sdk,
                      "define to 1 to disable SMP support ")

#--------------
# Checking Blas
#--------------
if config.CheckCBLAS():
    build_blasdot = 1
else:
    build_blasdot = 0

config.Finish()
write_info(env)

#==========
#  Build
#==========

#---------------------------------------
# Generate the public configuration file
#---------------------------------------
config_dict = {}
# XXX: this is ugly, make the API for config.h and numpyconfig.h similar
for key, value in numpyconfig_sym:
    config_dict['@%s@' % key] = str(value)
env['SUBST_DICT'] = config_dict

include_dir = 'include/numpy'
env.SubstInFile(pjoin(include_dir, 'numpyconfig.h'), pjoin(include_dir, 'numpyconfig.h.in'))

env['CONFIG_H_GEN'] = numpyconfig_sym

#---------------------------
# Builder for generated code
#---------------------------
env.Append(BUILDERS = {'GenerateMultiarrayApi' : array_api_gen_bld,
                       'GenerateUfuncApi' : ufunc_api_gen_bld,
                       'GenerateFromTemplate' : template_bld,
                       'GenerateUmath' : umath_bld})

#------------------------
# Generate generated code
#------------------------
scalartypes_src = env.GenerateFromTemplate(pjoin('src', 'scalartypes.inc.src'))
math_c99_src = env.GenerateFromTemplate(pjoin('src', 'math_c99.inc.src'))
arraytypes_src = env.GenerateFromTemplate(pjoin('src', 'arraytypes.inc.src'))
sortmodule_src = env.GenerateFromTemplate(pjoin('src', '_sortmodule.c.src'))
umathmodule_src = env.GenerateFromTemplate(pjoin('src', 'umathmodule.c.src'))
scalarmathmodule_src = env.GenerateFromTemplate(
                            pjoin('src', 'scalarmathmodule.c.src'))

umath = env.GenerateUmath('__umath_generated',
                          pjoin('code_generators', 'generate_umath.py'))

multiarray_api = env.GenerateMultiarrayApi('multiarray_api',
                        [ pjoin('code_generators', 'numpy_api_order.txt')])

ufunc_api = env.GenerateUfuncApi('ufunc_api',
                    pjoin('code_generators', 'ufunc_api_order.txt'))

env.Prepend(CPPPATH = ['include', '.'])

#-----------------
# Build multiarray
#-----------------
multiarray_src = [pjoin('src', 'multiarraymodule.c')]
multiarray = env.DistutilsPythonExtension('multiarray', source = multiarray_src)

#------------------
# Build sort module
#------------------
sort = env.DistutilsPythonExtension('_sort', source = sortmodule_src)

#-------------------
# Build umath module
#-------------------
umathmodule = env.DistutilsPythonExtension('umath', source = umathmodule_src)

#------------------------
# Build scalarmath module
#------------------------
scalarmathmodule = env.DistutilsPythonExtension('scalarmath',
                                            source = scalarmathmodule_src)

#----------------------
# Build _dotblas module
#----------------------
if build_blasdot:
    dotblas_src = [pjoin('blasdot', i) for i in ['_dotblas.c']]
    # because _dotblas does #include CBLAS_HEADER instead of #include
    # "cblas.h", scons does not detect the dependency
    # XXX: PythonExtension builder does not take the Depends on extension into
    # account for some reason, so we first build the object, with forced
    # dependency, and then builds the extension. This is more likely a bug in
    # our PythonExtension builder, but I cannot see how to solve it.
    dotblas_o = env.PythonObject('_dotblas', source = dotblas_src)
    env.Depends(dotblas_o, pjoin("blasdot", "cblas.h"))
    dotblas = env.DistutilsPythonExtension('_dotblas', dotblas_o)