config.py 19 KB

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