config.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. # Added Fortran compiler support to config. Currently useful only for
  2. # try_compile call. try_run works but is untested for most of Fortran
  3. # compilers (they must define linker_exe first).
  4. # Pearu Peterson
  5. import os
  6. import signal
  7. import subprocess
  8. import sys
  9. import textwrap
  10. import warnings
  11. from distutils.command.config import config as old_config
  12. from distutils.command.config import LANG_EXT
  13. from distutils import log
  14. from distutils.file_util import copy_file
  15. from distutils.ccompiler import CompileError, LinkError
  16. import distutils
  17. from numpy.distutils.exec_command import filepath_from_subprocess_output
  18. from numpy.distutils.mingw32ccompiler import generate_manifest
  19. from numpy.distutils.command.autodist import (check_gcc_function_attribute,
  20. check_gcc_function_attribute_with_intrinsics,
  21. check_gcc_variable_attribute,
  22. check_gcc_version_at_least,
  23. check_inline,
  24. check_restrict,
  25. check_compiler_gcc)
  26. LANG_EXT['f77'] = '.f'
  27. LANG_EXT['f90'] = '.f90'
  28. class config(old_config):
  29. old_config.user_options += [
  30. ('fcompiler=', None, "specify the Fortran compiler type"),
  31. ]
  32. def initialize_options(self):
  33. self.fcompiler = None
  34. old_config.initialize_options(self)
  35. def _check_compiler (self):
  36. old_config._check_compiler(self)
  37. from numpy.distutils.fcompiler import FCompiler, new_fcompiler
  38. if sys.platform == 'win32' and (self.compiler.compiler_type in
  39. ('msvc', 'intelw', 'intelemw')):
  40. # XXX: hack to circumvent a python 2.6 bug with msvc9compiler:
  41. # initialize call query_vcvarsall, which throws an IOError, and
  42. # causes an error along the way without much information. We try to
  43. # catch it here, hoping it is early enough, and print an helpful
  44. # message instead of Error: None.
  45. if not self.compiler.initialized:
  46. try:
  47. self.compiler.initialize()
  48. except IOError as e:
  49. msg = textwrap.dedent("""\
  50. Could not initialize compiler instance: do you have Visual Studio
  51. installed? If you are trying to build with MinGW, please use "python setup.py
  52. build -c mingw32" instead. If you have Visual Studio installed, check it is
  53. correctly installed, and the right version (VS 2008 for python 2.6, 2.7 and 3.2,
  54. VS 2010 for >= 3.3).
  55. Original exception was: %s, and the Compiler class was %s
  56. ============================================================================""") \
  57. % (e, self.compiler.__class__.__name__)
  58. print(textwrap.dedent("""\
  59. ============================================================================"""))
  60. raise distutils.errors.DistutilsPlatformError(msg)
  61. # After MSVC is initialized, add an explicit /MANIFEST to linker
  62. # flags. See issues gh-4245 and gh-4101 for details. Also
  63. # relevant are issues 4431 and 16296 on the Python bug tracker.
  64. from distutils import msvc9compiler
  65. if msvc9compiler.get_build_version() >= 10:
  66. for ldflags in [self.compiler.ldflags_shared,
  67. self.compiler.ldflags_shared_debug]:
  68. if '/MANIFEST' not in ldflags:
  69. ldflags.append('/MANIFEST')
  70. if not isinstance(self.fcompiler, FCompiler):
  71. self.fcompiler = new_fcompiler(compiler=self.fcompiler,
  72. dry_run=self.dry_run, force=1,
  73. c_compiler=self.compiler)
  74. if self.fcompiler is not None:
  75. self.fcompiler.customize(self.distribution)
  76. if self.fcompiler.get_version():
  77. self.fcompiler.customize_cmd(self)
  78. self.fcompiler.show_customization()
  79. def _wrap_method(self, mth, lang, args):
  80. from distutils.ccompiler import CompileError
  81. from distutils.errors import DistutilsExecError
  82. save_compiler = self.compiler
  83. if lang in ['f77', 'f90']:
  84. self.compiler = self.fcompiler
  85. try:
  86. ret = mth(*((self,)+args))
  87. except (DistutilsExecError, CompileError) as e:
  88. str(e)
  89. self.compiler = save_compiler
  90. raise CompileError
  91. self.compiler = save_compiler
  92. return ret
  93. def _compile (self, body, headers, include_dirs, lang):
  94. src, obj = self._wrap_method(old_config._compile, lang,
  95. (body, headers, include_dirs, lang))
  96. # _compile in unixcompiler.py sometimes creates .d dependency files.
  97. # Clean them up.
  98. self.temp_files.append(obj + '.d')
  99. return src, obj
  100. def _link (self, body,
  101. headers, include_dirs,
  102. libraries, library_dirs, lang):
  103. if self.compiler.compiler_type=='msvc':
  104. libraries = (libraries or [])[:]
  105. library_dirs = (library_dirs or [])[:]
  106. if lang in ['f77', 'f90']:
  107. lang = 'c' # always use system linker when using MSVC compiler
  108. if self.fcompiler:
  109. for d in self.fcompiler.library_dirs or []:
  110. # correct path when compiling in Cygwin but with
  111. # normal Win Python
  112. if d.startswith('/usr/lib'):
  113. try:
  114. d = subprocess.check_output(['cygpath',
  115. '-w', d])
  116. except (OSError, subprocess.CalledProcessError):
  117. pass
  118. else:
  119. d = filepath_from_subprocess_output(d)
  120. library_dirs.append(d)
  121. for libname in self.fcompiler.libraries or []:
  122. if libname not in libraries:
  123. libraries.append(libname)
  124. for libname in libraries:
  125. if libname.startswith('msvc'): continue
  126. fileexists = False
  127. for libdir in library_dirs or []:
  128. libfile = os.path.join(libdir, '%s.lib' % (libname))
  129. if os.path.isfile(libfile):
  130. fileexists = True
  131. break
  132. if fileexists: continue
  133. # make g77-compiled static libs available to MSVC
  134. fileexists = False
  135. for libdir in library_dirs:
  136. libfile = os.path.join(libdir, 'lib%s.a' % (libname))
  137. if os.path.isfile(libfile):
  138. # copy libname.a file to name.lib so that MSVC linker
  139. # can find it
  140. libfile2 = os.path.join(libdir, '%s.lib' % (libname))
  141. copy_file(libfile, libfile2)
  142. self.temp_files.append(libfile2)
  143. fileexists = True
  144. break
  145. if fileexists: continue
  146. log.warn('could not find library %r in directories %s' \
  147. % (libname, library_dirs))
  148. elif self.compiler.compiler_type == 'mingw32':
  149. generate_manifest(self)
  150. return self._wrap_method(old_config._link, lang,
  151. (body, headers, include_dirs,
  152. libraries, library_dirs, lang))
  153. def check_header(self, header, include_dirs=None, library_dirs=None, lang='c'):
  154. self._check_compiler()
  155. return self.try_compile(
  156. "/* we need a dummy line to make distutils happy */",
  157. [header], include_dirs)
  158. def check_decl(self, symbol,
  159. headers=None, include_dirs=None):
  160. self._check_compiler()
  161. body = textwrap.dedent("""
  162. int main(void)
  163. {
  164. #ifndef %s
  165. (void) %s;
  166. #endif
  167. ;
  168. return 0;
  169. }""") % (symbol, symbol)
  170. return self.try_compile(body, headers, include_dirs)
  171. def check_macro_true(self, symbol,
  172. headers=None, include_dirs=None):
  173. self._check_compiler()
  174. body = textwrap.dedent("""
  175. int main(void)
  176. {
  177. #if %s
  178. #else
  179. #error false or undefined macro
  180. #endif
  181. ;
  182. return 0;
  183. }""") % (symbol,)
  184. return self.try_compile(body, headers, include_dirs)
  185. def check_type(self, type_name, headers=None, include_dirs=None,
  186. library_dirs=None):
  187. """Check type availability. Return True if the type can be compiled,
  188. False otherwise"""
  189. self._check_compiler()
  190. # First check the type can be compiled
  191. body = textwrap.dedent(r"""
  192. int main(void) {
  193. if ((%(name)s *) 0)
  194. return 0;
  195. if (sizeof (%(name)s))
  196. return 0;
  197. }
  198. """) % {'name': type_name}
  199. st = False
  200. try:
  201. try:
  202. self._compile(body % {'type': type_name},
  203. headers, include_dirs, 'c')
  204. st = True
  205. except distutils.errors.CompileError:
  206. st = False
  207. finally:
  208. self._clean()
  209. return st
  210. def check_type_size(self, type_name, headers=None, include_dirs=None, library_dirs=None, expected=None):
  211. """Check size of a given type."""
  212. self._check_compiler()
  213. # First check the type can be compiled
  214. body = textwrap.dedent(r"""
  215. typedef %(type)s npy_check_sizeof_type;
  216. int main (void)
  217. {
  218. static int test_array [1 - 2 * !(((long) (sizeof (npy_check_sizeof_type))) >= 0)];
  219. test_array [0] = 0
  220. ;
  221. return 0;
  222. }
  223. """)
  224. self._compile(body % {'type': type_name},
  225. headers, include_dirs, 'c')
  226. self._clean()
  227. if expected:
  228. body = textwrap.dedent(r"""
  229. typedef %(type)s npy_check_sizeof_type;
  230. int main (void)
  231. {
  232. static int test_array [1 - 2 * !(((long) (sizeof (npy_check_sizeof_type))) == %(size)s)];
  233. test_array [0] = 0
  234. ;
  235. return 0;
  236. }
  237. """)
  238. for size in expected:
  239. try:
  240. self._compile(body % {'type': type_name, 'size': size},
  241. headers, include_dirs, 'c')
  242. self._clean()
  243. return size
  244. except CompileError:
  245. pass
  246. # this fails to *compile* if size > sizeof(type)
  247. body = textwrap.dedent(r"""
  248. typedef %(type)s npy_check_sizeof_type;
  249. int main (void)
  250. {
  251. static int test_array [1 - 2 * !(((long) (sizeof (npy_check_sizeof_type))) <= %(size)s)];
  252. test_array [0] = 0
  253. ;
  254. return 0;
  255. }
  256. """)
  257. # The principle is simple: we first find low and high bounds of size
  258. # for the type, where low/high are looked up on a log scale. Then, we
  259. # do a binary search to find the exact size between low and high
  260. low = 0
  261. mid = 0
  262. while True:
  263. try:
  264. self._compile(body % {'type': type_name, 'size': mid},
  265. headers, include_dirs, 'c')
  266. self._clean()
  267. break
  268. except CompileError:
  269. #log.info("failure to test for bound %d" % mid)
  270. low = mid + 1
  271. mid = 2 * mid + 1
  272. high = mid
  273. # Binary search:
  274. while low != high:
  275. mid = (high - low) // 2 + low
  276. try:
  277. self._compile(body % {'type': type_name, 'size': mid},
  278. headers, include_dirs, 'c')
  279. self._clean()
  280. high = mid
  281. except CompileError:
  282. low = mid + 1
  283. return low
  284. def check_func(self, func,
  285. headers=None, include_dirs=None,
  286. libraries=None, library_dirs=None,
  287. decl=False, call=False, call_args=None):
  288. # clean up distutils's config a bit: add void to main(), and
  289. # return a value.
  290. self._check_compiler()
  291. body = []
  292. if decl:
  293. if type(decl) == str:
  294. body.append(decl)
  295. else:
  296. body.append("int %s (void);" % func)
  297. # Handle MSVC intrinsics: force MS compiler to make a function call.
  298. # Useful to test for some functions when built with optimization on, to
  299. # avoid build error because the intrinsic and our 'fake' test
  300. # declaration do not match.
  301. body.append("#ifdef _MSC_VER")
  302. body.append("#pragma function(%s)" % func)
  303. body.append("#endif")
  304. body.append("int main (void) {")
  305. if call:
  306. if call_args is None:
  307. call_args = ''
  308. body.append(" %s(%s);" % (func, call_args))
  309. else:
  310. body.append(" %s;" % func)
  311. body.append(" return 0;")
  312. body.append("}")
  313. body = '\n'.join(body) + "\n"
  314. return self.try_link(body, headers, include_dirs,
  315. libraries, library_dirs)
  316. def check_funcs_once(self, funcs,
  317. headers=None, include_dirs=None,
  318. libraries=None, library_dirs=None,
  319. decl=False, call=False, call_args=None):
  320. """Check a list of functions at once.
  321. This is useful to speed up things, since all the functions in the funcs
  322. list will be put in one compilation unit.
  323. Arguments
  324. ---------
  325. funcs : seq
  326. list of functions to test
  327. include_dirs : seq
  328. list of header paths
  329. libraries : seq
  330. list of libraries to link the code snippet to
  331. library_dirs : seq
  332. list of library paths
  333. decl : dict
  334. for every (key, value), the declaration in the value will be
  335. used for function in key. If a function is not in the
  336. dictionary, no declaration will be used.
  337. call : dict
  338. for every item (f, value), if the value is True, a call will be
  339. done to the function f.
  340. """
  341. self._check_compiler()
  342. body = []
  343. if decl:
  344. for f, v in decl.items():
  345. if v:
  346. body.append("int %s (void);" % f)
  347. # Handle MS intrinsics. See check_func for more info.
  348. body.append("#ifdef _MSC_VER")
  349. for func in funcs:
  350. body.append("#pragma function(%s)" % func)
  351. body.append("#endif")
  352. body.append("int main (void) {")
  353. if call:
  354. for f in funcs:
  355. if f in call and call[f]:
  356. if not (call_args and f in call_args and call_args[f]):
  357. args = ''
  358. else:
  359. args = call_args[f]
  360. body.append(" %s(%s);" % (f, args))
  361. else:
  362. body.append(" %s;" % f)
  363. else:
  364. for f in funcs:
  365. body.append(" %s;" % f)
  366. body.append(" return 0;")
  367. body.append("}")
  368. body = '\n'.join(body) + "\n"
  369. return self.try_link(body, headers, include_dirs,
  370. libraries, library_dirs)
  371. def check_inline(self):
  372. """Return the inline keyword recognized by the compiler, empty string
  373. otherwise."""
  374. return check_inline(self)
  375. def check_restrict(self):
  376. """Return the restrict keyword recognized by the compiler, empty string
  377. otherwise."""
  378. return check_restrict(self)
  379. def check_compiler_gcc(self):
  380. """Return True if the C compiler is gcc"""
  381. return check_compiler_gcc(self)
  382. def check_gcc_function_attribute(self, attribute, name):
  383. return check_gcc_function_attribute(self, attribute, name)
  384. def check_gcc_function_attribute_with_intrinsics(self, attribute, name,
  385. code, include):
  386. return check_gcc_function_attribute_with_intrinsics(self, attribute,
  387. name, code, include)
  388. def check_gcc_variable_attribute(self, attribute):
  389. return check_gcc_variable_attribute(self, attribute)
  390. def check_gcc_version_at_least(self, major, minor=0, patchlevel=0):
  391. """Return True if the GCC version is greater than or equal to the
  392. specified version."""
  393. return check_gcc_version_at_least(self, major, minor, patchlevel)
  394. def get_output(self, body, headers=None, include_dirs=None,
  395. libraries=None, library_dirs=None,
  396. lang="c", use_tee=None):
  397. """Try to compile, link to an executable, and run a program
  398. built from 'body' and 'headers'. Returns the exit status code
  399. of the program and its output.
  400. """
  401. # 2008-11-16, RemoveMe
  402. warnings.warn("\n+++++++++++++++++++++++++++++++++++++++++++++++++\n"
  403. "Usage of get_output is deprecated: please do not \n"
  404. "use it anymore, and avoid configuration checks \n"
  405. "involving running executable on the target machine.\n"
  406. "+++++++++++++++++++++++++++++++++++++++++++++++++\n",
  407. DeprecationWarning, stacklevel=2)
  408. self._check_compiler()
  409. exitcode, output = 255, ''
  410. try:
  411. grabber = GrabStdout()
  412. try:
  413. src, obj, exe = self._link(body, headers, include_dirs,
  414. libraries, library_dirs, lang)
  415. grabber.restore()
  416. except Exception:
  417. output = grabber.data
  418. grabber.restore()
  419. raise
  420. exe = os.path.join('.', exe)
  421. try:
  422. # specify cwd arg for consistency with
  423. # historic usage pattern of exec_command()
  424. # also, note that exe appears to be a string,
  425. # which exec_command() handled, but we now
  426. # use a list for check_output() -- this assumes
  427. # that exe is always a single command
  428. output = subprocess.check_output([exe], cwd='.')
  429. except subprocess.CalledProcessError as exc:
  430. exitstatus = exc.returncode
  431. output = ''
  432. except OSError:
  433. # preserve the EnvironmentError exit status
  434. # used historically in exec_command()
  435. exitstatus = 127
  436. output = ''
  437. else:
  438. output = filepath_from_subprocess_output(output)
  439. if hasattr(os, 'WEXITSTATUS'):
  440. exitcode = os.WEXITSTATUS(exitstatus)
  441. if os.WIFSIGNALED(exitstatus):
  442. sig = os.WTERMSIG(exitstatus)
  443. log.error('subprocess exited with signal %d' % (sig,))
  444. if sig == signal.SIGINT:
  445. # control-C
  446. raise KeyboardInterrupt
  447. else:
  448. exitcode = exitstatus
  449. log.info("success!")
  450. except (CompileError, LinkError):
  451. log.info("failure.")
  452. self._clean()
  453. return exitcode, output
  454. class GrabStdout:
  455. def __init__(self):
  456. self.sys_stdout = sys.stdout
  457. self.data = ''
  458. sys.stdout = self
  459. def write (self, data):
  460. self.sys_stdout.write(data)
  461. self.data += data
  462. def flush (self):
  463. self.sys_stdout.flush()
  464. def restore(self):
  465. sys.stdout = self.sys_stdout