setup.py 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. import os
  2. import sys
  3. import pickle
  4. import copy
  5. import warnings
  6. import platform
  7. import textwrap
  8. from os.path import join
  9. from numpy.distutils import log
  10. from distutils.dep_util import newer
  11. from distutils.sysconfig import get_config_var
  12. from numpy._build_utils.apple_accelerate import (
  13. uses_accelerate_framework, get_sgemv_fix
  14. )
  15. from numpy.compat import npy_load_module
  16. from setup_common import * # noqa: F403
  17. # Set to True to enable relaxed strides checking. This (mostly) means
  18. # that `strides[dim]` is ignored if `shape[dim] == 1` when setting flags.
  19. NPY_RELAXED_STRIDES_CHECKING = (os.environ.get('NPY_RELAXED_STRIDES_CHECKING', "1") != "0")
  20. # Put NPY_RELAXED_STRIDES_DEBUG=1 in the environment if you want numpy to use a
  21. # bogus value for affected strides in order to help smoke out bad stride usage
  22. # when relaxed stride checking is enabled.
  23. NPY_RELAXED_STRIDES_DEBUG = (os.environ.get('NPY_RELAXED_STRIDES_DEBUG', "0") != "0")
  24. NPY_RELAXED_STRIDES_DEBUG = NPY_RELAXED_STRIDES_DEBUG and NPY_RELAXED_STRIDES_CHECKING
  25. # XXX: ugly, we use a class to avoid calling twice some expensive functions in
  26. # config.h/numpyconfig.h. I don't see a better way because distutils force
  27. # config.h generation inside an Extension class, and as such sharing
  28. # configuration information between extensions is not easy.
  29. # Using a pickled-based memoize does not work because config_cmd is an instance
  30. # method, which cPickle does not like.
  31. #
  32. # Use pickle in all cases, as cPickle is gone in python3 and the difference
  33. # in time is only in build. -- Charles Harris, 2013-03-30
  34. class CallOnceOnly:
  35. def __init__(self):
  36. self._check_types = None
  37. self._check_ieee_macros = None
  38. self._check_complex = None
  39. def check_types(self, *a, **kw):
  40. if self._check_types is None:
  41. out = check_types(*a, **kw)
  42. self._check_types = pickle.dumps(out)
  43. else:
  44. out = copy.deepcopy(pickle.loads(self._check_types))
  45. return out
  46. def check_ieee_macros(self, *a, **kw):
  47. if self._check_ieee_macros is None:
  48. out = check_ieee_macros(*a, **kw)
  49. self._check_ieee_macros = pickle.dumps(out)
  50. else:
  51. out = copy.deepcopy(pickle.loads(self._check_ieee_macros))
  52. return out
  53. def check_complex(self, *a, **kw):
  54. if self._check_complex is None:
  55. out = check_complex(*a, **kw)
  56. self._check_complex = pickle.dumps(out)
  57. else:
  58. out = copy.deepcopy(pickle.loads(self._check_complex))
  59. return out
  60. def pythonlib_dir():
  61. """return path where libpython* is."""
  62. if sys.platform == 'win32':
  63. return os.path.join(sys.prefix, "libs")
  64. else:
  65. return get_config_var('LIBDIR')
  66. def is_npy_no_signal():
  67. """Return True if the NPY_NO_SIGNAL symbol must be defined in configuration
  68. header."""
  69. return sys.platform == 'win32'
  70. def is_npy_no_smp():
  71. """Return True if the NPY_NO_SMP symbol must be defined in public
  72. header (when SMP support cannot be reliably enabled)."""
  73. # Perhaps a fancier check is in order here.
  74. # so that threads are only enabled if there
  75. # are actually multiple CPUS? -- but
  76. # threaded code can be nice even on a single
  77. # CPU so that long-calculating code doesn't
  78. # block.
  79. return 'NPY_NOSMP' in os.environ
  80. def win32_checks(deflist):
  81. from numpy.distutils.misc_util import get_build_architecture
  82. a = get_build_architecture()
  83. # Distutils hack on AMD64 on windows
  84. print('BUILD_ARCHITECTURE: %r, os.name=%r, sys.platform=%r' %
  85. (a, os.name, sys.platform))
  86. if a == 'AMD64':
  87. deflist.append('DISTUTILS_USE_SDK')
  88. # On win32, force long double format string to be 'g', not
  89. # 'Lg', since the MS runtime does not support long double whose
  90. # size is > sizeof(double)
  91. if a == "Intel" or a == "AMD64":
  92. deflist.append('FORCE_NO_LONG_DOUBLE_FORMATTING')
  93. def check_math_capabilities(config, ext, moredefs, mathlibs):
  94. def check_func(func_name):
  95. return config.check_func(func_name, libraries=mathlibs,
  96. decl=True, call=True)
  97. def check_funcs_once(funcs_name):
  98. decl = dict([(f, True) for f in funcs_name])
  99. st = config.check_funcs_once(funcs_name, libraries=mathlibs,
  100. decl=decl, call=decl)
  101. if st:
  102. moredefs.extend([(fname2def(f), 1) for f in funcs_name])
  103. return st
  104. def check_funcs(funcs_name):
  105. # Use check_funcs_once first, and if it does not work, test func per
  106. # func. Return success only if all the functions are available
  107. if not check_funcs_once(funcs_name):
  108. # Global check failed, check func per func
  109. for f in funcs_name:
  110. if check_func(f):
  111. moredefs.append((fname2def(f), 1))
  112. return 0
  113. else:
  114. return 1
  115. #use_msvc = config.check_decl("_MSC_VER")
  116. if not check_funcs_once(MANDATORY_FUNCS):
  117. raise SystemError("One of the required function to build numpy is not"
  118. " available (the list is %s)." % str(MANDATORY_FUNCS))
  119. # Standard functions which may not be available and for which we have a
  120. # replacement implementation. Note that some of these are C99 functions.
  121. # XXX: hack to circumvent cpp pollution from python: python put its
  122. # config.h in the public namespace, so we have a clash for the common
  123. # functions we test. We remove every function tested by python's
  124. # autoconf, hoping their own test are correct
  125. for f in OPTIONAL_STDFUNCS_MAYBE:
  126. if config.check_decl(fname2def(f),
  127. headers=["Python.h", "math.h"]):
  128. OPTIONAL_STDFUNCS.remove(f)
  129. check_funcs(OPTIONAL_STDFUNCS)
  130. for h in OPTIONAL_HEADERS:
  131. if config.check_func("", decl=False, call=False, headers=[h]):
  132. h = h.replace(".", "_").replace(os.path.sep, "_")
  133. moredefs.append((fname2def(h), 1))
  134. for tup in OPTIONAL_INTRINSICS:
  135. headers = None
  136. if len(tup) == 2:
  137. f, args, m = tup[0], tup[1], fname2def(tup[0])
  138. elif len(tup) == 3:
  139. f, args, headers, m = tup[0], tup[1], [tup[2]], fname2def(tup[0])
  140. else:
  141. f, args, headers, m = tup[0], tup[1], [tup[2]], fname2def(tup[3])
  142. if config.check_func(f, decl=False, call=True, call_args=args,
  143. headers=headers):
  144. moredefs.append((m, 1))
  145. for dec, fn in OPTIONAL_FUNCTION_ATTRIBUTES:
  146. if config.check_gcc_function_attribute(dec, fn):
  147. moredefs.append((fname2def(fn), 1))
  148. if fn == 'attribute_target_avx512f':
  149. # GH-14787: Work around GCC<8.4 bug when compiling with AVX512
  150. # support on Windows-based platforms
  151. if (sys.platform in ('win32', 'cygwin') and
  152. config.check_compiler_gcc() and
  153. not config.check_gcc_version_at_least(8, 4)):
  154. ext.extra_compile_args.extend(
  155. ['-ffixed-xmm%s' % n for n in range(16, 32)])
  156. for dec, fn, code, header in OPTIONAL_FUNCTION_ATTRIBUTES_WITH_INTRINSICS:
  157. if config.check_gcc_function_attribute_with_intrinsics(dec, fn, code,
  158. header):
  159. moredefs.append((fname2def(fn), 1))
  160. for fn in OPTIONAL_VARIABLE_ATTRIBUTES:
  161. if config.check_gcc_variable_attribute(fn):
  162. m = fn.replace("(", "_").replace(")", "_")
  163. moredefs.append((fname2def(m), 1))
  164. # C99 functions: float and long double versions
  165. check_funcs(C99_FUNCS_SINGLE)
  166. check_funcs(C99_FUNCS_EXTENDED)
  167. def check_complex(config, mathlibs):
  168. priv = []
  169. pub = []
  170. try:
  171. if os.uname()[0] == "Interix":
  172. warnings.warn("Disabling broken complex support. See #1365", stacklevel=2)
  173. return priv, pub
  174. except Exception:
  175. # os.uname not available on all platforms. blanket except ugly but safe
  176. pass
  177. # Check for complex support
  178. st = config.check_header('complex.h')
  179. if st:
  180. priv.append(('HAVE_COMPLEX_H', 1))
  181. pub.append(('NPY_USE_C99_COMPLEX', 1))
  182. for t in C99_COMPLEX_TYPES:
  183. st = config.check_type(t, headers=["complex.h"])
  184. if st:
  185. pub.append(('NPY_HAVE_%s' % type2def(t), 1))
  186. def check_prec(prec):
  187. flist = [f + prec for f in C99_COMPLEX_FUNCS]
  188. decl = dict([(f, True) for f in flist])
  189. if not config.check_funcs_once(flist, call=decl, decl=decl,
  190. libraries=mathlibs):
  191. for f in flist:
  192. if config.check_func(f, call=True, decl=True,
  193. libraries=mathlibs):
  194. priv.append((fname2def(f), 1))
  195. else:
  196. priv.extend([(fname2def(f), 1) for f in flist])
  197. check_prec('')
  198. check_prec('f')
  199. check_prec('l')
  200. return priv, pub
  201. def check_ieee_macros(config):
  202. priv = []
  203. pub = []
  204. macros = []
  205. def _add_decl(f):
  206. priv.append(fname2def("decl_%s" % f))
  207. pub.append('NPY_%s' % fname2def("decl_%s" % f))
  208. # XXX: hack to circumvent cpp pollution from python: python put its
  209. # config.h in the public namespace, so we have a clash for the common
  210. # functions we test. We remove every function tested by python's
  211. # autoconf, hoping their own test are correct
  212. _macros = ["isnan", "isinf", "signbit", "isfinite"]
  213. for f in _macros:
  214. py_symbol = fname2def("decl_%s" % f)
  215. already_declared = config.check_decl(py_symbol,
  216. headers=["Python.h", "math.h"])
  217. if already_declared:
  218. if config.check_macro_true(py_symbol,
  219. headers=["Python.h", "math.h"]):
  220. pub.append('NPY_%s' % fname2def("decl_%s" % f))
  221. else:
  222. macros.append(f)
  223. # Normally, isnan and isinf are macro (C99), but some platforms only have
  224. # func, or both func and macro version. Check for macro only, and define
  225. # replacement ones if not found.
  226. # Note: including Python.h is necessary because it modifies some math.h
  227. # definitions
  228. for f in macros:
  229. st = config.check_decl(f, headers=["Python.h", "math.h"])
  230. if st:
  231. _add_decl(f)
  232. return priv, pub
  233. def check_types(config_cmd, ext, build_dir):
  234. private_defines = []
  235. public_defines = []
  236. # Expected size (in number of bytes) for each type. This is an
  237. # optimization: those are only hints, and an exhaustive search for the size
  238. # is done if the hints are wrong.
  239. expected = {'short': [2], 'int': [4], 'long': [8, 4],
  240. 'float': [4], 'double': [8], 'long double': [16, 12, 8],
  241. 'Py_intptr_t': [8, 4], 'PY_LONG_LONG': [8], 'long long': [8],
  242. 'off_t': [8, 4]}
  243. # Check we have the python header (-dev* packages on Linux)
  244. result = config_cmd.check_header('Python.h')
  245. if not result:
  246. python = 'python'
  247. if '__pypy__' in sys.builtin_module_names:
  248. python = 'pypy'
  249. raise SystemError(
  250. "Cannot compile 'Python.h'. Perhaps you need to "
  251. "install {0}-dev|{0}-devel.".format(python))
  252. res = config_cmd.check_header("endian.h")
  253. if res:
  254. private_defines.append(('HAVE_ENDIAN_H', 1))
  255. public_defines.append(('NPY_HAVE_ENDIAN_H', 1))
  256. res = config_cmd.check_header("sys/endian.h")
  257. if res:
  258. private_defines.append(('HAVE_SYS_ENDIAN_H', 1))
  259. public_defines.append(('NPY_HAVE_SYS_ENDIAN_H', 1))
  260. # Check basic types sizes
  261. for type in ('short', 'int', 'long'):
  262. res = config_cmd.check_decl("SIZEOF_%s" % sym2def(type), headers=["Python.h"])
  263. if res:
  264. public_defines.append(('NPY_SIZEOF_%s' % sym2def(type), "SIZEOF_%s" % sym2def(type)))
  265. else:
  266. res = config_cmd.check_type_size(type, expected=expected[type])
  267. if res >= 0:
  268. public_defines.append(('NPY_SIZEOF_%s' % sym2def(type), '%d' % res))
  269. else:
  270. raise SystemError("Checking sizeof (%s) failed !" % type)
  271. for type in ('float', 'double', 'long double'):
  272. already_declared = config_cmd.check_decl("SIZEOF_%s" % sym2def(type),
  273. headers=["Python.h"])
  274. res = config_cmd.check_type_size(type, expected=expected[type])
  275. if res >= 0:
  276. public_defines.append(('NPY_SIZEOF_%s' % sym2def(type), '%d' % res))
  277. if not already_declared and not type == 'long double':
  278. private_defines.append(('SIZEOF_%s' % sym2def(type), '%d' % res))
  279. else:
  280. raise SystemError("Checking sizeof (%s) failed !" % type)
  281. # Compute size of corresponding complex type: used to check that our
  282. # definition is binary compatible with C99 complex type (check done at
  283. # build time in npy_common.h)
  284. complex_def = "struct {%s __x; %s __y;}" % (type, type)
  285. res = config_cmd.check_type_size(complex_def,
  286. expected=[2 * x for x in expected[type]])
  287. if res >= 0:
  288. public_defines.append(('NPY_SIZEOF_COMPLEX_%s' % sym2def(type), '%d' % res))
  289. else:
  290. raise SystemError("Checking sizeof (%s) failed !" % complex_def)
  291. for type in ('Py_intptr_t', 'off_t'):
  292. res = config_cmd.check_type_size(type, headers=["Python.h"],
  293. library_dirs=[pythonlib_dir()],
  294. expected=expected[type])
  295. if res >= 0:
  296. private_defines.append(('SIZEOF_%s' % sym2def(type), '%d' % res))
  297. public_defines.append(('NPY_SIZEOF_%s' % sym2def(type), '%d' % res))
  298. else:
  299. raise SystemError("Checking sizeof (%s) failed !" % type)
  300. # We check declaration AND type because that's how distutils does it.
  301. if config_cmd.check_decl('PY_LONG_LONG', headers=['Python.h']):
  302. res = config_cmd.check_type_size('PY_LONG_LONG', headers=['Python.h'],
  303. library_dirs=[pythonlib_dir()],
  304. expected=expected['PY_LONG_LONG'])
  305. if res >= 0:
  306. private_defines.append(('SIZEOF_%s' % sym2def('PY_LONG_LONG'), '%d' % res))
  307. public_defines.append(('NPY_SIZEOF_%s' % sym2def('PY_LONG_LONG'), '%d' % res))
  308. else:
  309. raise SystemError("Checking sizeof (%s) failed !" % 'PY_LONG_LONG')
  310. res = config_cmd.check_type_size('long long',
  311. expected=expected['long long'])
  312. if res >= 0:
  313. #private_defines.append(('SIZEOF_%s' % sym2def('long long'), '%d' % res))
  314. public_defines.append(('NPY_SIZEOF_%s' % sym2def('long long'), '%d' % res))
  315. else:
  316. raise SystemError("Checking sizeof (%s) failed !" % 'long long')
  317. if not config_cmd.check_decl('CHAR_BIT', headers=['Python.h']):
  318. raise RuntimeError(
  319. "Config wo CHAR_BIT is not supported"
  320. ", please contact the maintainers")
  321. return private_defines, public_defines
  322. def check_mathlib(config_cmd):
  323. # Testing the C math library
  324. mathlibs = []
  325. mathlibs_choices = [[], ['m'], ['cpml']]
  326. mathlib = os.environ.get('MATHLIB')
  327. if mathlib:
  328. mathlibs_choices.insert(0, mathlib.split(','))
  329. for libs in mathlibs_choices:
  330. if config_cmd.check_func("exp", libraries=libs, decl=True, call=True):
  331. mathlibs = libs
  332. break
  333. else:
  334. raise EnvironmentError("math library missing; rerun "
  335. "setup.py after setting the "
  336. "MATHLIB env variable")
  337. return mathlibs
  338. def visibility_define(config):
  339. """Return the define value to use for NPY_VISIBILITY_HIDDEN (may be empty
  340. string)."""
  341. hide = '__attribute__((visibility("hidden")))'
  342. if config.check_gcc_function_attribute(hide, 'hideme'):
  343. return hide
  344. else:
  345. return ''
  346. def configuration(parent_package='',top_path=None):
  347. from numpy.distutils.misc_util import Configuration, dot_join
  348. from numpy.distutils.system_info import get_info
  349. config = Configuration('core', parent_package, top_path)
  350. local_dir = config.local_path
  351. codegen_dir = join(local_dir, 'code_generators')
  352. if is_released(config):
  353. warnings.simplefilter('error', MismatchCAPIWarning)
  354. # Check whether we have a mismatch between the set C API VERSION and the
  355. # actual C API VERSION
  356. check_api_version(C_API_VERSION, codegen_dir)
  357. generate_umath_py = join(codegen_dir, 'generate_umath.py')
  358. n = dot_join(config.name, 'generate_umath')
  359. generate_umath = npy_load_module('_'.join(n.split('.')),
  360. generate_umath_py, ('.py', 'U', 1))
  361. header_dir = 'include/numpy' # this is relative to config.path_in_package
  362. cocache = CallOnceOnly()
  363. def generate_config_h(ext, build_dir):
  364. target = join(build_dir, header_dir, 'config.h')
  365. d = os.path.dirname(target)
  366. if not os.path.exists(d):
  367. os.makedirs(d)
  368. if newer(__file__, target):
  369. config_cmd = config.get_config_cmd()
  370. log.info('Generating %s', target)
  371. # Check sizeof
  372. moredefs, ignored = cocache.check_types(config_cmd, ext, build_dir)
  373. # Check math library and C99 math funcs availability
  374. mathlibs = check_mathlib(config_cmd)
  375. moredefs.append(('MATHLIB', ','.join(mathlibs)))
  376. check_math_capabilities(config_cmd, ext, moredefs, mathlibs)
  377. moredefs.extend(cocache.check_ieee_macros(config_cmd)[0])
  378. moredefs.extend(cocache.check_complex(config_cmd, mathlibs)[0])
  379. # Signal check
  380. if is_npy_no_signal():
  381. moredefs.append('__NPY_PRIVATE_NO_SIGNAL')
  382. # Windows checks
  383. if sys.platform == 'win32' or os.name == 'nt':
  384. win32_checks(moredefs)
  385. # C99 restrict keyword
  386. moredefs.append(('NPY_RESTRICT', config_cmd.check_restrict()))
  387. # Inline check
  388. inline = config_cmd.check_inline()
  389. # Use relaxed stride checking
  390. if NPY_RELAXED_STRIDES_CHECKING:
  391. moredefs.append(('NPY_RELAXED_STRIDES_CHECKING', 1))
  392. # Use bogus stride debug aid when relaxed strides are enabled
  393. if NPY_RELAXED_STRIDES_DEBUG:
  394. moredefs.append(('NPY_RELAXED_STRIDES_DEBUG', 1))
  395. # Get long double representation
  396. rep = check_long_double_representation(config_cmd)
  397. moredefs.append(('HAVE_LDOUBLE_%s' % rep, 1))
  398. if check_for_right_shift_internal_compiler_error(config_cmd):
  399. moredefs.append('NPY_DO_NOT_OPTIMIZE_LONG_right_shift')
  400. moredefs.append('NPY_DO_NOT_OPTIMIZE_ULONG_right_shift')
  401. moredefs.append('NPY_DO_NOT_OPTIMIZE_LONGLONG_right_shift')
  402. moredefs.append('NPY_DO_NOT_OPTIMIZE_ULONGLONG_right_shift')
  403. # Generate the config.h file from moredefs
  404. with open(target, 'w') as target_f:
  405. for d in moredefs:
  406. if isinstance(d, str):
  407. target_f.write('#define %s\n' % (d))
  408. else:
  409. target_f.write('#define %s %s\n' % (d[0], d[1]))
  410. # define inline to our keyword, or nothing
  411. target_f.write('#ifndef __cplusplus\n')
  412. if inline == 'inline':
  413. target_f.write('/* #undef inline */\n')
  414. else:
  415. target_f.write('#define inline %s\n' % inline)
  416. target_f.write('#endif\n')
  417. # add the guard to make sure config.h is never included directly,
  418. # but always through npy_config.h
  419. target_f.write(textwrap.dedent("""
  420. #ifndef _NPY_NPY_CONFIG_H_
  421. #error config.h should never be included directly, include npy_config.h instead
  422. #endif
  423. """))
  424. log.info('File: %s' % target)
  425. with open(target) as target_f:
  426. log.info(target_f.read())
  427. log.info('EOF')
  428. else:
  429. mathlibs = []
  430. with open(target) as target_f:
  431. for line in target_f:
  432. s = '#define MATHLIB'
  433. if line.startswith(s):
  434. value = line[len(s):].strip()
  435. if value:
  436. mathlibs.extend(value.split(','))
  437. # Ugly: this can be called within a library and not an extension,
  438. # in which case there is no libraries attributes (and none is
  439. # needed).
  440. if hasattr(ext, 'libraries'):
  441. ext.libraries.extend(mathlibs)
  442. incl_dir = os.path.dirname(target)
  443. if incl_dir not in config.numpy_include_dirs:
  444. config.numpy_include_dirs.append(incl_dir)
  445. return target
  446. def generate_numpyconfig_h(ext, build_dir):
  447. """Depends on config.h: generate_config_h has to be called before !"""
  448. # put common include directory in build_dir on search path
  449. # allows using code generation in headers
  450. config.add_include_dirs(join(build_dir, "src", "common"))
  451. config.add_include_dirs(join(build_dir, "src", "npymath"))
  452. target = join(build_dir, header_dir, '_numpyconfig.h')
  453. d = os.path.dirname(target)
  454. if not os.path.exists(d):
  455. os.makedirs(d)
  456. if newer(__file__, target):
  457. config_cmd = config.get_config_cmd()
  458. log.info('Generating %s', target)
  459. # Check sizeof
  460. ignored, moredefs = cocache.check_types(config_cmd, ext, build_dir)
  461. if is_npy_no_signal():
  462. moredefs.append(('NPY_NO_SIGNAL', 1))
  463. if is_npy_no_smp():
  464. moredefs.append(('NPY_NO_SMP', 1))
  465. else:
  466. moredefs.append(('NPY_NO_SMP', 0))
  467. mathlibs = check_mathlib(config_cmd)
  468. moredefs.extend(cocache.check_ieee_macros(config_cmd)[1])
  469. moredefs.extend(cocache.check_complex(config_cmd, mathlibs)[1])
  470. if NPY_RELAXED_STRIDES_CHECKING:
  471. moredefs.append(('NPY_RELAXED_STRIDES_CHECKING', 1))
  472. if NPY_RELAXED_STRIDES_DEBUG:
  473. moredefs.append(('NPY_RELAXED_STRIDES_DEBUG', 1))
  474. # Check whether we can use inttypes (C99) formats
  475. if config_cmd.check_decl('PRIdPTR', headers=['inttypes.h']):
  476. moredefs.append(('NPY_USE_C99_FORMATS', 1))
  477. # visibility check
  478. hidden_visibility = visibility_define(config_cmd)
  479. moredefs.append(('NPY_VISIBILITY_HIDDEN', hidden_visibility))
  480. # Add the C API/ABI versions
  481. moredefs.append(('NPY_ABI_VERSION', '0x%.8X' % C_ABI_VERSION))
  482. moredefs.append(('NPY_API_VERSION', '0x%.8X' % C_API_VERSION))
  483. # Add moredefs to header
  484. with open(target, 'w') as target_f:
  485. for d in moredefs:
  486. if isinstance(d, str):
  487. target_f.write('#define %s\n' % (d))
  488. else:
  489. target_f.write('#define %s %s\n' % (d[0], d[1]))
  490. # Define __STDC_FORMAT_MACROS
  491. target_f.write(textwrap.dedent("""
  492. #ifndef __STDC_FORMAT_MACROS
  493. #define __STDC_FORMAT_MACROS 1
  494. #endif
  495. """))
  496. # Dump the numpyconfig.h header to stdout
  497. log.info('File: %s' % target)
  498. with open(target) as target_f:
  499. log.info(target_f.read())
  500. log.info('EOF')
  501. config.add_data_files((header_dir, target))
  502. return target
  503. def generate_api_func(module_name):
  504. def generate_api(ext, build_dir):
  505. script = join(codegen_dir, module_name + '.py')
  506. sys.path.insert(0, codegen_dir)
  507. try:
  508. m = __import__(module_name)
  509. log.info('executing %s', script)
  510. h_file, c_file, doc_file = m.generate_api(os.path.join(build_dir, header_dir))
  511. finally:
  512. del sys.path[0]
  513. config.add_data_files((header_dir, h_file),
  514. (header_dir, doc_file))
  515. return (h_file,)
  516. return generate_api
  517. generate_numpy_api = generate_api_func('generate_numpy_api')
  518. generate_ufunc_api = generate_api_func('generate_ufunc_api')
  519. config.add_include_dirs(join(local_dir, "src", "common"))
  520. config.add_include_dirs(join(local_dir, "src"))
  521. config.add_include_dirs(join(local_dir))
  522. config.add_data_dir('include/numpy')
  523. config.add_include_dirs(join('src', 'npymath'))
  524. config.add_include_dirs(join('src', 'multiarray'))
  525. config.add_include_dirs(join('src', 'umath'))
  526. config.add_include_dirs(join('src', 'npysort'))
  527. config.add_define_macros([("NPY_INTERNAL_BUILD", "1")]) # this macro indicates that Numpy build is in process
  528. config.add_define_macros([("HAVE_NPY_CONFIG_H", "1")])
  529. if sys.platform[:3] == "aix":
  530. config.add_define_macros([("_LARGE_FILES", None)])
  531. else:
  532. config.add_define_macros([("_FILE_OFFSET_BITS", "64")])
  533. config.add_define_macros([('_LARGEFILE_SOURCE', '1')])
  534. config.add_define_macros([('_LARGEFILE64_SOURCE', '1')])
  535. config.numpy_include_dirs.extend(config.paths('include'))
  536. deps = [join('src', 'npymath', '_signbit.c'),
  537. join('include', 'numpy', '*object.h'),
  538. join(codegen_dir, 'genapi.py'),
  539. ]
  540. #######################################################################
  541. # npymath library #
  542. #######################################################################
  543. subst_dict = dict([("sep", os.path.sep), ("pkgname", "numpy.core")])
  544. def get_mathlib_info(*args):
  545. # Another ugly hack: the mathlib info is known once build_src is run,
  546. # but we cannot use add_installed_pkg_config here either, so we only
  547. # update the substitution dictionary during npymath build
  548. config_cmd = config.get_config_cmd()
  549. # Check that the toolchain works, to fail early if it doesn't
  550. # (avoid late errors with MATHLIB which are confusing if the
  551. # compiler does not work).
  552. st = config_cmd.try_link('int main(void) { return 0;}')
  553. if not st:
  554. # rerun the failing command in verbose mode
  555. config_cmd.compiler.verbose = True
  556. config_cmd.try_link('int main(void) { return 0;}')
  557. raise RuntimeError("Broken toolchain: cannot link a simple C program")
  558. mlibs = check_mathlib(config_cmd)
  559. posix_mlib = ' '.join(['-l%s' % l for l in mlibs])
  560. msvc_mlib = ' '.join(['%s.lib' % l for l in mlibs])
  561. subst_dict["posix_mathlib"] = posix_mlib
  562. subst_dict["msvc_mathlib"] = msvc_mlib
  563. npymath_sources = [join('src', 'npymath', 'npy_math_internal.h.src'),
  564. join('src', 'npymath', 'npy_math.c'),
  565. join('src', 'npymath', 'ieee754.c.src'),
  566. join('src', 'npymath', 'npy_math_complex.c.src'),
  567. join('src', 'npymath', 'halffloat.c')
  568. ]
  569. # Must be true for CRT compilers but not MinGW/cygwin. See gh-9977.
  570. # Intel and Clang also don't seem happy with /GL
  571. is_msvc = (platform.platform().startswith('Windows') and
  572. platform.python_compiler().startswith('MS'))
  573. config.add_installed_library('npymath',
  574. sources=npymath_sources + [get_mathlib_info],
  575. install_dir='lib',
  576. build_info={
  577. 'include_dirs' : [], # empty list required for creating npy_math_internal.h
  578. 'extra_compiler_args' : (['/GL-'] if is_msvc else []),
  579. })
  580. config.add_npy_pkg_config("npymath.ini.in", "lib/npy-pkg-config",
  581. subst_dict)
  582. config.add_npy_pkg_config("mlib.ini.in", "lib/npy-pkg-config",
  583. subst_dict)
  584. #######################################################################
  585. # npysort library #
  586. #######################################################################
  587. # This library is created for the build but it is not installed
  588. npysort_sources = [join('src', 'common', 'npy_sort.h.src'),
  589. join('src', 'npysort', 'quicksort.c.src'),
  590. join('src', 'npysort', 'mergesort.c.src'),
  591. join('src', 'npysort', 'timsort.c.src'),
  592. join('src', 'npysort', 'heapsort.c.src'),
  593. join('src', 'npysort', 'radixsort.c.src'),
  594. join('src', 'common', 'npy_partition.h.src'),
  595. join('src', 'npysort', 'selection.c.src'),
  596. join('src', 'common', 'npy_binsearch.h.src'),
  597. join('src', 'npysort', 'binsearch.c.src'),
  598. ]
  599. config.add_library('npysort',
  600. sources=npysort_sources,
  601. include_dirs=[])
  602. #######################################################################
  603. # multiarray_tests module #
  604. #######################################################################
  605. config.add_extension('_multiarray_tests',
  606. sources=[join('src', 'multiarray', '_multiarray_tests.c.src'),
  607. join('src', 'common', 'mem_overlap.c')],
  608. depends=[join('src', 'common', 'mem_overlap.h'),
  609. join('src', 'common', 'npy_extint128.h')],
  610. libraries=['npymath'])
  611. #######################################################################
  612. # _multiarray_umath module - common part #
  613. #######################################################################
  614. common_deps = [
  615. join('src', 'common', 'array_assign.h'),
  616. join('src', 'common', 'binop_override.h'),
  617. join('src', 'common', 'cblasfuncs.h'),
  618. join('src', 'common', 'lowlevel_strided_loops.h'),
  619. join('src', 'common', 'mem_overlap.h'),
  620. join('src', 'common', 'npy_cblas.h'),
  621. join('src', 'common', 'npy_config.h'),
  622. join('src', 'common', 'npy_ctypes.h'),
  623. join('src', 'common', 'npy_extint128.h'),
  624. join('src', 'common', 'npy_import.h'),
  625. join('src', 'common', 'npy_longdouble.h'),
  626. join('src', 'common', 'templ_common.h.src'),
  627. join('src', 'common', 'ucsnarrow.h'),
  628. join('src', 'common', 'ufunc_override.h'),
  629. join('src', 'common', 'umathmodule.h'),
  630. join('src', 'common', 'numpyos.h'),
  631. ]
  632. common_src = [
  633. join('src', 'common', 'array_assign.c'),
  634. join('src', 'common', 'mem_overlap.c'),
  635. join('src', 'common', 'npy_longdouble.c'),
  636. join('src', 'common', 'templ_common.h.src'),
  637. join('src', 'common', 'ucsnarrow.c'),
  638. join('src', 'common', 'ufunc_override.c'),
  639. join('src', 'common', 'numpyos.c'),
  640. join('src', 'common', 'npy_cpu_features.c.src'),
  641. ]
  642. if os.environ.get('NPY_USE_BLAS_ILP64', "0") != "0":
  643. blas_info = get_info('blas_ilp64_opt', 2)
  644. else:
  645. blas_info = get_info('blas_opt', 0)
  646. have_blas = blas_info and ('HAVE_CBLAS', None) in blas_info.get('define_macros', [])
  647. if have_blas:
  648. extra_info = blas_info
  649. # These files are also in MANIFEST.in so that they are always in
  650. # the source distribution independently of HAVE_CBLAS.
  651. common_src.extend([join('src', 'common', 'cblasfuncs.c'),
  652. join('src', 'common', 'python_xerbla.c'),
  653. ])
  654. if uses_accelerate_framework(blas_info):
  655. common_src.extend(get_sgemv_fix())
  656. else:
  657. extra_info = {}
  658. #######################################################################
  659. # _multiarray_umath module - multiarray part #
  660. #######################################################################
  661. multiarray_deps = [
  662. join('src', 'multiarray', 'arrayobject.h'),
  663. join('src', 'multiarray', 'arraytypes.h'),
  664. join('src', 'multiarray', 'arrayfunction_override.h'),
  665. join('src', 'multiarray', 'npy_buffer.h'),
  666. join('src', 'multiarray', 'calculation.h'),
  667. join('src', 'multiarray', 'common.h'),
  668. join('src', 'multiarray', 'convert_datatype.h'),
  669. join('src', 'multiarray', 'convert.h'),
  670. join('src', 'multiarray', 'conversion_utils.h'),
  671. join('src', 'multiarray', 'ctors.h'),
  672. join('src', 'multiarray', 'descriptor.h'),
  673. join('src', 'multiarray', 'dragon4.h'),
  674. join('src', 'multiarray', 'getset.h'),
  675. join('src', 'multiarray', 'hashdescr.h'),
  676. join('src', 'multiarray', 'iterators.h'),
  677. join('src', 'multiarray', 'mapping.h'),
  678. join('src', 'multiarray', 'methods.h'),
  679. join('src', 'multiarray', 'multiarraymodule.h'),
  680. join('src', 'multiarray', 'nditer_impl.h'),
  681. join('src', 'multiarray', 'number.h'),
  682. join('src', 'multiarray', 'refcount.h'),
  683. join('src', 'multiarray', 'scalartypes.h'),
  684. join('src', 'multiarray', 'sequence.h'),
  685. join('src', 'multiarray', 'shape.h'),
  686. join('src', 'multiarray', 'strfuncs.h'),
  687. join('src', 'multiarray', 'typeinfo.h'),
  688. join('src', 'multiarray', 'usertypes.h'),
  689. join('src', 'multiarray', 'vdot.h'),
  690. join('include', 'numpy', 'arrayobject.h'),
  691. join('include', 'numpy', '_neighborhood_iterator_imp.h'),
  692. join('include', 'numpy', 'npy_endian.h'),
  693. join('include', 'numpy', 'arrayscalars.h'),
  694. join('include', 'numpy', 'noprefix.h'),
  695. join('include', 'numpy', 'npy_interrupt.h'),
  696. join('include', 'numpy', 'npy_3kcompat.h'),
  697. join('include', 'numpy', 'npy_math.h'),
  698. join('include', 'numpy', 'halffloat.h'),
  699. join('include', 'numpy', 'npy_common.h'),
  700. join('include', 'numpy', 'npy_os.h'),
  701. join('include', 'numpy', 'utils.h'),
  702. join('include', 'numpy', 'ndarrayobject.h'),
  703. join('include', 'numpy', 'npy_cpu.h'),
  704. join('include', 'numpy', 'numpyconfig.h'),
  705. join('include', 'numpy', 'ndarraytypes.h'),
  706. join('include', 'numpy', 'npy_1_7_deprecated_api.h'),
  707. # add library sources as distuils does not consider libraries
  708. # dependencies
  709. ] + npysort_sources + npymath_sources
  710. multiarray_src = [
  711. join('src', 'multiarray', 'alloc.c'),
  712. join('src', 'multiarray', 'arrayobject.c'),
  713. join('src', 'multiarray', 'arraytypes.c.src'),
  714. join('src', 'multiarray', 'array_assign_scalar.c'),
  715. join('src', 'multiarray', 'array_assign_array.c'),
  716. join('src', 'multiarray', 'arrayfunction_override.c'),
  717. join('src', 'multiarray', 'buffer.c'),
  718. join('src', 'multiarray', 'calculation.c'),
  719. join('src', 'multiarray', 'compiled_base.c'),
  720. join('src', 'multiarray', 'common.c'),
  721. join('src', 'multiarray', 'convert.c'),
  722. join('src', 'multiarray', 'convert_datatype.c'),
  723. join('src', 'multiarray', 'conversion_utils.c'),
  724. join('src', 'multiarray', 'ctors.c'),
  725. join('src', 'multiarray', 'datetime.c'),
  726. join('src', 'multiarray', 'datetime_strings.c'),
  727. join('src', 'multiarray', 'datetime_busday.c'),
  728. join('src', 'multiarray', 'datetime_busdaycal.c'),
  729. join('src', 'multiarray', 'descriptor.c'),
  730. join('src', 'multiarray', 'dragon4.c'),
  731. join('src', 'multiarray', 'dtype_transfer.c'),
  732. join('src', 'multiarray', 'einsum.c.src'),
  733. join('src', 'multiarray', 'flagsobject.c'),
  734. join('src', 'multiarray', 'getset.c'),
  735. join('src', 'multiarray', 'hashdescr.c'),
  736. join('src', 'multiarray', 'item_selection.c'),
  737. join('src', 'multiarray', 'iterators.c'),
  738. join('src', 'multiarray', 'lowlevel_strided_loops.c.src'),
  739. join('src', 'multiarray', 'mapping.c'),
  740. join('src', 'multiarray', 'methods.c'),
  741. join('src', 'multiarray', 'multiarraymodule.c'),
  742. join('src', 'multiarray', 'nditer_templ.c.src'),
  743. join('src', 'multiarray', 'nditer_api.c'),
  744. join('src', 'multiarray', 'nditer_constr.c'),
  745. join('src', 'multiarray', 'nditer_pywrap.c'),
  746. join('src', 'multiarray', 'number.c'),
  747. join('src', 'multiarray', 'refcount.c'),
  748. join('src', 'multiarray', 'sequence.c'),
  749. join('src', 'multiarray', 'shape.c'),
  750. join('src', 'multiarray', 'scalarapi.c'),
  751. join('src', 'multiarray', 'scalartypes.c.src'),
  752. join('src', 'multiarray', 'strfuncs.c'),
  753. join('src', 'multiarray', 'temp_elide.c'),
  754. join('src', 'multiarray', 'typeinfo.c'),
  755. join('src', 'multiarray', 'usertypes.c'),
  756. join('src', 'multiarray', 'vdot.c'),
  757. ]
  758. #######################################################################
  759. # _multiarray_umath module - umath part #
  760. #######################################################################
  761. def generate_umath_c(ext, build_dir):
  762. target = join(build_dir, header_dir, '__umath_generated.c')
  763. dir = os.path.dirname(target)
  764. if not os.path.exists(dir):
  765. os.makedirs(dir)
  766. script = generate_umath_py
  767. if newer(script, target):
  768. with open(target, 'w') as f:
  769. f.write(generate_umath.make_code(generate_umath.defdict,
  770. generate_umath.__file__))
  771. return []
  772. umath_src = [
  773. join('src', 'umath', 'umathmodule.c'),
  774. join('src', 'umath', 'reduction.c'),
  775. join('src', 'umath', 'funcs.inc.src'),
  776. join('src', 'umath', 'simd.inc.src'),
  777. join('src', 'umath', 'loops.h.src'),
  778. join('src', 'umath', 'loops.c.src'),
  779. join('src', 'umath', 'matmul.h.src'),
  780. join('src', 'umath', 'matmul.c.src'),
  781. join('src', 'umath', 'clip.h.src'),
  782. join('src', 'umath', 'clip.c.src'),
  783. join('src', 'umath', 'ufunc_object.c'),
  784. join('src', 'umath', 'extobj.c'),
  785. join('src', 'umath', 'scalarmath.c.src'),
  786. join('src', 'umath', 'ufunc_type_resolution.c'),
  787. join('src', 'umath', 'override.c'),
  788. ]
  789. umath_deps = [
  790. generate_umath_py,
  791. join('include', 'numpy', 'npy_math.h'),
  792. join('include', 'numpy', 'halffloat.h'),
  793. join('src', 'multiarray', 'common.h'),
  794. join('src', 'multiarray', 'number.h'),
  795. join('src', 'common', 'templ_common.h.src'),
  796. join('src', 'umath', 'simd.inc.src'),
  797. join('src', 'umath', 'override.h'),
  798. join(codegen_dir, 'generate_ufunc_api.py'),
  799. ]
  800. config.add_extension('_multiarray_umath',
  801. sources=multiarray_src + umath_src +
  802. npymath_sources + common_src +
  803. [generate_config_h,
  804. generate_numpyconfig_h,
  805. generate_numpy_api,
  806. join(codegen_dir, 'generate_numpy_api.py'),
  807. join('*.py'),
  808. generate_umath_c,
  809. generate_ufunc_api,
  810. ],
  811. depends=deps + multiarray_deps + umath_deps +
  812. common_deps,
  813. libraries=['npymath', 'npysort'],
  814. extra_info=extra_info)
  815. #######################################################################
  816. # umath_tests module #
  817. #######################################################################
  818. config.add_extension('_umath_tests',
  819. sources=[join('src', 'umath', '_umath_tests.c.src')])
  820. #######################################################################
  821. # custom rational dtype module #
  822. #######################################################################
  823. config.add_extension('_rational_tests',
  824. sources=[join('src', 'umath', '_rational_tests.c.src')])
  825. #######################################################################
  826. # struct_ufunc_test module #
  827. #######################################################################
  828. config.add_extension('_struct_ufunc_tests',
  829. sources=[join('src', 'umath', '_struct_ufunc_tests.c.src')])
  830. #######################################################################
  831. # operand_flag_tests module #
  832. #######################################################################
  833. config.add_extension('_operand_flag_tests',
  834. sources=[join('src', 'umath', '_operand_flag_tests.c.src')])
  835. config.add_subpackage('tests')
  836. config.add_data_dir('tests/data')
  837. config.make_svn_version_py()
  838. return config
  839. if __name__ == '__main__':
  840. from numpy.distutils.core import setup
  841. setup(configuration=configuration)