Dependencies.py 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. from __future__ import absolute_import, print_function
  2. import cython
  3. from .. import __version__
  4. import os
  5. import shutil
  6. import hashlib
  7. import subprocess
  8. import collections
  9. import re, sys, time
  10. from glob import iglob
  11. from io import open as io_open
  12. from os.path import relpath as _relpath
  13. from distutils.extension import Extension
  14. from distutils.util import strtobool
  15. try:
  16. import gzip
  17. gzip_open = gzip.open
  18. gzip_ext = '.gz'
  19. except ImportError:
  20. gzip_open = open
  21. gzip_ext = ''
  22. try:
  23. import pythran
  24. import pythran.config
  25. PythranAvailable = True
  26. except:
  27. PythranAvailable = False
  28. from .. import Utils
  29. from ..Utils import (cached_function, cached_method, path_exists,
  30. safe_makedirs, copy_file_to_dir_if_newer, is_package_dir)
  31. from ..Compiler.Main import Context, CompilationOptions, default_options
  32. join_path = cached_function(os.path.join)
  33. copy_once_if_newer = cached_function(copy_file_to_dir_if_newer)
  34. safe_makedirs_once = cached_function(safe_makedirs)
  35. if sys.version_info[0] < 3:
  36. # stupid Py2 distutils enforces str type in list of sources
  37. _fs_encoding = sys.getfilesystemencoding()
  38. if _fs_encoding is None:
  39. _fs_encoding = sys.getdefaultencoding()
  40. def encode_filename_in_py2(filename):
  41. if not isinstance(filename, bytes):
  42. return filename.encode(_fs_encoding)
  43. return filename
  44. else:
  45. def encode_filename_in_py2(filename):
  46. return filename
  47. basestring = str
  48. def _make_relative(file_paths, base=None):
  49. if not base:
  50. base = os.getcwd()
  51. if base[-1] != os.path.sep:
  52. base += os.path.sep
  53. return [_relpath(path, base) if path.startswith(base) else path
  54. for path in file_paths]
  55. def extended_iglob(pattern):
  56. if '{' in pattern:
  57. m = re.match('(.*){([^}]+)}(.*)', pattern)
  58. if m:
  59. before, switch, after = m.groups()
  60. for case in switch.split(','):
  61. for path in extended_iglob(before + case + after):
  62. yield path
  63. return
  64. if '**/' in pattern:
  65. seen = set()
  66. first, rest = pattern.split('**/', 1)
  67. if first:
  68. first = iglob(first+'/')
  69. else:
  70. first = ['']
  71. for root in first:
  72. for path in extended_iglob(join_path(root, rest)):
  73. if path not in seen:
  74. seen.add(path)
  75. yield path
  76. for path in extended_iglob(join_path(root, '*', '**/' + rest)):
  77. if path not in seen:
  78. seen.add(path)
  79. yield path
  80. else:
  81. for path in iglob(pattern):
  82. yield path
  83. def nonempty(it, error_msg="expected non-empty iterator"):
  84. empty = True
  85. for value in it:
  86. empty = False
  87. yield value
  88. if empty:
  89. raise ValueError(error_msg)
  90. @cached_function
  91. def file_hash(filename):
  92. path = os.path.normpath(filename.encode("UTF-8"))
  93. prefix = (str(len(path)) + ":").encode("UTF-8")
  94. m = hashlib.md5(prefix)
  95. m.update(path)
  96. f = open(filename, 'rb')
  97. try:
  98. data = f.read(65000)
  99. while data:
  100. m.update(data)
  101. data = f.read(65000)
  102. finally:
  103. f.close()
  104. return m.hexdigest()
  105. def parse_list(s):
  106. """
  107. >>> parse_list("")
  108. []
  109. >>> parse_list("a")
  110. ['a']
  111. >>> parse_list("a b c")
  112. ['a', 'b', 'c']
  113. >>> parse_list("[a, b, c]")
  114. ['a', 'b', 'c']
  115. >>> parse_list('a " " b')
  116. ['a', ' ', 'b']
  117. >>> parse_list('[a, ",a", "a,", ",", ]')
  118. ['a', ',a', 'a,', ',']
  119. """
  120. if len(s) >= 2 and s[0] == '[' and s[-1] == ']':
  121. s = s[1:-1]
  122. delimiter = ','
  123. else:
  124. delimiter = ' '
  125. s, literals = strip_string_literals(s)
  126. def unquote(literal):
  127. literal = literal.strip()
  128. if literal[0] in "'\"":
  129. return literals[literal[1:-1]]
  130. else:
  131. return literal
  132. return [unquote(item) for item in s.split(delimiter) if item.strip()]
  133. transitive_str = object()
  134. transitive_list = object()
  135. bool_or = object()
  136. distutils_settings = {
  137. 'name': str,
  138. 'sources': list,
  139. 'define_macros': list,
  140. 'undef_macros': list,
  141. 'libraries': transitive_list,
  142. 'library_dirs': transitive_list,
  143. 'runtime_library_dirs': transitive_list,
  144. 'include_dirs': transitive_list,
  145. 'extra_objects': list,
  146. 'extra_compile_args': transitive_list,
  147. 'extra_link_args': transitive_list,
  148. 'export_symbols': list,
  149. 'depends': transitive_list,
  150. 'language': transitive_str,
  151. 'np_pythran': bool_or
  152. }
  153. def update_pythran_extension(ext):
  154. if not PythranAvailable:
  155. raise RuntimeError("You first need to install Pythran to use the np_pythran directive.")
  156. pythran_ext = pythran.config.make_extension()
  157. ext.include_dirs.extend(pythran_ext['include_dirs'])
  158. ext.extra_compile_args.extend(pythran_ext['extra_compile_args'])
  159. ext.extra_link_args.extend(pythran_ext['extra_link_args'])
  160. ext.define_macros.extend(pythran_ext['define_macros'])
  161. ext.undef_macros.extend(pythran_ext['undef_macros'])
  162. ext.library_dirs.extend(pythran_ext['library_dirs'])
  163. ext.libraries.extend(pythran_ext['libraries'])
  164. ext.language = 'c++'
  165. # These options are not compatible with the way normal Cython extensions work
  166. for bad_option in ["-fwhole-program", "-fvisibility=hidden"]:
  167. try:
  168. ext.extra_compile_args.remove(bad_option)
  169. except ValueError:
  170. pass
  171. @cython.locals(start=cython.Py_ssize_t, end=cython.Py_ssize_t)
  172. def line_iter(source):
  173. if isinstance(source, basestring):
  174. start = 0
  175. while True:
  176. end = source.find('\n', start)
  177. if end == -1:
  178. yield source[start:]
  179. return
  180. yield source[start:end]
  181. start = end+1
  182. else:
  183. for line in source:
  184. yield line
  185. class DistutilsInfo(object):
  186. def __init__(self, source=None, exn=None):
  187. self.values = {}
  188. if source is not None:
  189. for line in line_iter(source):
  190. line = line.lstrip()
  191. if not line:
  192. continue
  193. if line[0] != '#':
  194. break
  195. line = line[1:].lstrip()
  196. kind = next((k for k in ("distutils:","cython:") if line.startswith(k)), None)
  197. if not kind is None:
  198. key, _, value = [s.strip() for s in line[len(kind):].partition('=')]
  199. type = distutils_settings.get(key, None)
  200. if line.startswith("cython:") and type is None: continue
  201. if type in (list, transitive_list):
  202. value = parse_list(value)
  203. if key == 'define_macros':
  204. value = [tuple(macro.split('=', 1))
  205. if '=' in macro else (macro, None)
  206. for macro in value]
  207. if type is bool_or:
  208. value = strtobool(value)
  209. self.values[key] = value
  210. elif exn is not None:
  211. for key in distutils_settings:
  212. if key in ('name', 'sources','np_pythran'):
  213. continue
  214. value = getattr(exn, key, None)
  215. if value:
  216. self.values[key] = value
  217. def merge(self, other):
  218. if other is None:
  219. return self
  220. for key, value in other.values.items():
  221. type = distutils_settings[key]
  222. if type is transitive_str and key not in self.values:
  223. self.values[key] = value
  224. elif type is transitive_list:
  225. if key in self.values:
  226. # Change a *copy* of the list (Trac #845)
  227. all = self.values[key][:]
  228. for v in value:
  229. if v not in all:
  230. all.append(v)
  231. value = all
  232. self.values[key] = value
  233. elif type is bool_or:
  234. self.values[key] = self.values.get(key, False) | value
  235. return self
  236. def subs(self, aliases):
  237. if aliases is None:
  238. return self
  239. resolved = DistutilsInfo()
  240. for key, value in self.values.items():
  241. type = distutils_settings[key]
  242. if type in [list, transitive_list]:
  243. new_value_list = []
  244. for v in value:
  245. if v in aliases:
  246. v = aliases[v]
  247. if isinstance(v, list):
  248. new_value_list += v
  249. else:
  250. new_value_list.append(v)
  251. value = new_value_list
  252. else:
  253. if value in aliases:
  254. value = aliases[value]
  255. resolved.values[key] = value
  256. return resolved
  257. def apply(self, extension):
  258. for key, value in self.values.items():
  259. type = distutils_settings[key]
  260. if type in [list, transitive_list]:
  261. value = getattr(extension, key) + list(value)
  262. setattr(extension, key, value)
  263. @cython.locals(start=cython.Py_ssize_t, q=cython.Py_ssize_t,
  264. single_q=cython.Py_ssize_t, double_q=cython.Py_ssize_t,
  265. hash_mark=cython.Py_ssize_t, end=cython.Py_ssize_t,
  266. k=cython.Py_ssize_t, counter=cython.Py_ssize_t, quote_len=cython.Py_ssize_t)
  267. def strip_string_literals(code, prefix='__Pyx_L'):
  268. """
  269. Normalizes every string literal to be of the form '__Pyx_Lxxx',
  270. returning the normalized code and a mapping of labels to
  271. string literals.
  272. """
  273. new_code = []
  274. literals = {}
  275. counter = 0
  276. start = q = 0
  277. in_quote = False
  278. hash_mark = single_q = double_q = -1
  279. code_len = len(code)
  280. quote_type = quote_len = None
  281. while True:
  282. if hash_mark < q:
  283. hash_mark = code.find('#', q)
  284. if single_q < q:
  285. single_q = code.find("'", q)
  286. if double_q < q:
  287. double_q = code.find('"', q)
  288. q = min(single_q, double_q)
  289. if q == -1:
  290. q = max(single_q, double_q)
  291. # We're done.
  292. if q == -1 and hash_mark == -1:
  293. new_code.append(code[start:])
  294. break
  295. # Try to close the quote.
  296. elif in_quote:
  297. if code[q-1] == u'\\':
  298. k = 2
  299. while q >= k and code[q-k] == u'\\':
  300. k += 1
  301. if k % 2 == 0:
  302. q += 1
  303. continue
  304. if code[q] == quote_type and (
  305. quote_len == 1 or (code_len > q + 2 and quote_type == code[q+1] == code[q+2])):
  306. counter += 1
  307. label = "%s%s_" % (prefix, counter)
  308. literals[label] = code[start+quote_len:q]
  309. full_quote = code[q:q+quote_len]
  310. new_code.append(full_quote)
  311. new_code.append(label)
  312. new_code.append(full_quote)
  313. q += quote_len
  314. in_quote = False
  315. start = q
  316. else:
  317. q += 1
  318. # Process comment.
  319. elif -1 != hash_mark and (hash_mark < q or q == -1):
  320. new_code.append(code[start:hash_mark+1])
  321. end = code.find('\n', hash_mark)
  322. counter += 1
  323. label = "%s%s_" % (prefix, counter)
  324. if end == -1:
  325. end_or_none = None
  326. else:
  327. end_or_none = end
  328. literals[label] = code[hash_mark+1:end_or_none]
  329. new_code.append(label)
  330. if end == -1:
  331. break
  332. start = q = end
  333. # Open the quote.
  334. else:
  335. if code_len >= q+3 and (code[q] == code[q+1] == code[q+2]):
  336. quote_len = 3
  337. else:
  338. quote_len = 1
  339. in_quote = True
  340. quote_type = code[q]
  341. new_code.append(code[start:q])
  342. start = q
  343. q += quote_len
  344. return "".join(new_code), literals
  345. # We need to allow spaces to allow for conditional compilation like
  346. # IF ...:
  347. # cimport ...
  348. dependency_regex = re.compile(r"(?:^\s*from +([0-9a-zA-Z_.]+) +cimport)|"
  349. r"(?:^\s*cimport +([0-9a-zA-Z_.]+(?: *, *[0-9a-zA-Z_.]+)*))|"
  350. r"(?:^\s*cdef +extern +from +['\"]([^'\"]+)['\"])|"
  351. r"(?:^\s*include +['\"]([^'\"]+)['\"])", re.M)
  352. def normalize_existing(base_path, rel_paths):
  353. return normalize_existing0(os.path.dirname(base_path), tuple(set(rel_paths)))
  354. @cached_function
  355. def normalize_existing0(base_dir, rel_paths):
  356. """
  357. Given some base directory ``base_dir`` and a list of path names
  358. ``rel_paths``, normalize each relative path name ``rel`` by
  359. replacing it by ``os.path.join(base, rel)`` if that file exists.
  360. Return a couple ``(normalized, needed_base)`` where ``normalized``
  361. if the list of normalized file names and ``needed_base`` is
  362. ``base_dir`` if we actually needed ``base_dir``. If no paths were
  363. changed (for example, if all paths were already absolute), then
  364. ``needed_base`` is ``None``.
  365. """
  366. normalized = []
  367. needed_base = None
  368. for rel in rel_paths:
  369. if os.path.isabs(rel):
  370. normalized.append(rel)
  371. continue
  372. path = join_path(base_dir, rel)
  373. if path_exists(path):
  374. normalized.append(os.path.normpath(path))
  375. needed_base = base_dir
  376. else:
  377. normalized.append(rel)
  378. return (normalized, needed_base)
  379. def resolve_depends(depends, include_dirs):
  380. include_dirs = tuple(include_dirs)
  381. resolved = []
  382. for depend in depends:
  383. path = resolve_depend(depend, include_dirs)
  384. if path is not None:
  385. resolved.append(path)
  386. return resolved
  387. @cached_function
  388. def resolve_depend(depend, include_dirs):
  389. if depend[0] == '<' and depend[-1] == '>':
  390. return None
  391. for dir in include_dirs:
  392. path = join_path(dir, depend)
  393. if path_exists(path):
  394. return os.path.normpath(path)
  395. return None
  396. @cached_function
  397. def package(filename):
  398. dir = os.path.dirname(os.path.abspath(str(filename)))
  399. if dir != filename and is_package_dir(dir):
  400. return package(dir) + (os.path.basename(dir),)
  401. else:
  402. return ()
  403. @cached_function
  404. def fully_qualified_name(filename):
  405. module = os.path.splitext(os.path.basename(filename))[0]
  406. return '.'.join(package(filename) + (module,))
  407. @cached_function
  408. def parse_dependencies(source_filename):
  409. # Actual parsing is way too slow, so we use regular expressions.
  410. # The only catch is that we must strip comments and string
  411. # literals ahead of time.
  412. fh = Utils.open_source_file(source_filename, error_handling='ignore')
  413. try:
  414. source = fh.read()
  415. finally:
  416. fh.close()
  417. distutils_info = DistutilsInfo(source)
  418. source, literals = strip_string_literals(source)
  419. source = source.replace('\\\n', ' ').replace('\t', ' ')
  420. # TODO: pure mode
  421. cimports = []
  422. includes = []
  423. externs = []
  424. for m in dependency_regex.finditer(source):
  425. cimport_from, cimport_list, extern, include = m.groups()
  426. if cimport_from:
  427. cimports.append(cimport_from)
  428. elif cimport_list:
  429. cimports.extend(x.strip() for x in cimport_list.split(","))
  430. elif extern:
  431. externs.append(literals[extern])
  432. else:
  433. includes.append(literals[include])
  434. return cimports, includes, externs, distutils_info
  435. class DependencyTree(object):
  436. def __init__(self, context, quiet=False):
  437. self.context = context
  438. self.quiet = quiet
  439. self._transitive_cache = {}
  440. def parse_dependencies(self, source_filename):
  441. if path_exists(source_filename):
  442. source_filename = os.path.normpath(source_filename)
  443. return parse_dependencies(source_filename)
  444. @cached_method
  445. def included_files(self, filename):
  446. # This is messy because included files are textually included, resolving
  447. # cimports (but not includes) relative to the including file.
  448. all = set()
  449. for include in self.parse_dependencies(filename)[1]:
  450. include_path = join_path(os.path.dirname(filename), include)
  451. if not path_exists(include_path):
  452. include_path = self.context.find_include_file(include, None)
  453. if include_path:
  454. if '.' + os.path.sep in include_path:
  455. include_path = os.path.normpath(include_path)
  456. all.add(include_path)
  457. all.update(self.included_files(include_path))
  458. elif not self.quiet:
  459. print("Unable to locate '%s' referenced from '%s'" % (filename, include))
  460. return all
  461. @cached_method
  462. def cimports_externs_incdirs(self, filename):
  463. # This is really ugly. Nested cimports are resolved with respect to the
  464. # includer, but includes are resolved with respect to the includee.
  465. cimports, includes, externs = self.parse_dependencies(filename)[:3]
  466. cimports = set(cimports)
  467. externs = set(externs)
  468. incdirs = set()
  469. for include in self.included_files(filename):
  470. included_cimports, included_externs, included_incdirs = self.cimports_externs_incdirs(include)
  471. cimports.update(included_cimports)
  472. externs.update(included_externs)
  473. incdirs.update(included_incdirs)
  474. externs, incdir = normalize_existing(filename, externs)
  475. if incdir:
  476. incdirs.add(incdir)
  477. return tuple(cimports), externs, incdirs
  478. def cimports(self, filename):
  479. return self.cimports_externs_incdirs(filename)[0]
  480. def package(self, filename):
  481. return package(filename)
  482. def fully_qualified_name(self, filename):
  483. return fully_qualified_name(filename)
  484. @cached_method
  485. def find_pxd(self, module, filename=None):
  486. is_relative = module[0] == '.'
  487. if is_relative and not filename:
  488. raise NotImplementedError("New relative imports.")
  489. if filename is not None:
  490. module_path = module.split('.')
  491. if is_relative:
  492. module_path.pop(0) # just explicitly relative
  493. package_path = list(self.package(filename))
  494. while module_path and not module_path[0]:
  495. try:
  496. package_path.pop()
  497. except IndexError:
  498. return None # FIXME: error?
  499. module_path.pop(0)
  500. relative = '.'.join(package_path + module_path)
  501. pxd = self.context.find_pxd_file(relative, None)
  502. if pxd:
  503. return pxd
  504. if is_relative:
  505. return None # FIXME: error?
  506. return self.context.find_pxd_file(module, None)
  507. @cached_method
  508. def cimported_files(self, filename):
  509. if filename[-4:] == '.pyx' and path_exists(filename[:-4] + '.pxd'):
  510. pxd_list = [filename[:-4] + '.pxd']
  511. else:
  512. pxd_list = []
  513. for module in self.cimports(filename):
  514. if module[:7] == 'cython.' or module == 'cython':
  515. continue
  516. pxd_file = self.find_pxd(module, filename)
  517. if pxd_file is not None:
  518. pxd_list.append(pxd_file)
  519. elif not self.quiet:
  520. print("%s: cannot find cimported module '%s'" % (filename, module))
  521. return tuple(pxd_list)
  522. @cached_method
  523. def immediate_dependencies(self, filename):
  524. all = set([filename])
  525. all.update(self.cimported_files(filename))
  526. all.update(self.included_files(filename))
  527. return all
  528. def all_dependencies(self, filename):
  529. return self.transitive_merge(filename, self.immediate_dependencies, set.union)
  530. @cached_method
  531. def timestamp(self, filename):
  532. return os.path.getmtime(filename)
  533. def extract_timestamp(self, filename):
  534. return self.timestamp(filename), filename
  535. def newest_dependency(self, filename):
  536. return max([self.extract_timestamp(f) for f in self.all_dependencies(filename)])
  537. def transitive_fingerprint(self, filename, extra=None):
  538. try:
  539. m = hashlib.md5(__version__.encode('UTF-8'))
  540. m.update(file_hash(filename).encode('UTF-8'))
  541. for x in sorted(self.all_dependencies(filename)):
  542. if os.path.splitext(x)[1] not in ('.c', '.cpp', '.h'):
  543. m.update(file_hash(x).encode('UTF-8'))
  544. if extra is not None:
  545. m.update(str(extra).encode('UTF-8'))
  546. return m.hexdigest()
  547. except IOError:
  548. return None
  549. def distutils_info0(self, filename):
  550. info = self.parse_dependencies(filename)[3]
  551. kwds = info.values
  552. cimports, externs, incdirs = self.cimports_externs_incdirs(filename)
  553. basedir = os.getcwd()
  554. # Add dependencies on "cdef extern from ..." files
  555. if externs:
  556. externs = _make_relative(externs, basedir)
  557. if 'depends' in kwds:
  558. kwds['depends'] = list(set(kwds['depends']).union(externs))
  559. else:
  560. kwds['depends'] = list(externs)
  561. # Add include_dirs to ensure that the C compiler will find the
  562. # "cdef extern from ..." files
  563. if incdirs:
  564. include_dirs = list(kwds.get('include_dirs', []))
  565. for inc in _make_relative(incdirs, basedir):
  566. if inc not in include_dirs:
  567. include_dirs.append(inc)
  568. kwds['include_dirs'] = include_dirs
  569. return info
  570. def distutils_info(self, filename, aliases=None, base=None):
  571. return (self.transitive_merge(filename, self.distutils_info0, DistutilsInfo.merge)
  572. .subs(aliases)
  573. .merge(base))
  574. def transitive_merge(self, node, extract, merge):
  575. try:
  576. seen = self._transitive_cache[extract, merge]
  577. except KeyError:
  578. seen = self._transitive_cache[extract, merge] = {}
  579. return self.transitive_merge_helper(
  580. node, extract, merge, seen, {}, self.cimported_files)[0]
  581. def transitive_merge_helper(self, node, extract, merge, seen, stack, outgoing):
  582. if node in seen:
  583. return seen[node], None
  584. deps = extract(node)
  585. if node in stack:
  586. return deps, node
  587. try:
  588. stack[node] = len(stack)
  589. loop = None
  590. for next in outgoing(node):
  591. sub_deps, sub_loop = self.transitive_merge_helper(next, extract, merge, seen, stack, outgoing)
  592. if sub_loop is not None:
  593. if loop is not None and stack[loop] < stack[sub_loop]:
  594. pass
  595. else:
  596. loop = sub_loop
  597. deps = merge(deps, sub_deps)
  598. if loop == node:
  599. loop = None
  600. if loop is None:
  601. seen[node] = deps
  602. return deps, loop
  603. finally:
  604. del stack[node]
  605. _dep_tree = None
  606. def create_dependency_tree(ctx=None, quiet=False):
  607. global _dep_tree
  608. if _dep_tree is None:
  609. if ctx is None:
  610. ctx = Context(["."], CompilationOptions(default_options))
  611. _dep_tree = DependencyTree(ctx, quiet=quiet)
  612. return _dep_tree
  613. # If this changes, change also docs/src/reference/compilation.rst
  614. # which mentions this function
  615. def default_create_extension(template, kwds):
  616. if 'depends' in kwds:
  617. include_dirs = kwds.get('include_dirs', []) + ["."]
  618. depends = resolve_depends(kwds['depends'], include_dirs)
  619. kwds['depends'] = sorted(set(depends + template.depends))
  620. t = template.__class__
  621. ext = t(**kwds)
  622. metadata = dict(distutils=kwds, module_name=kwds['name'])
  623. return (ext, metadata)
  624. # This may be useful for advanced users?
  625. def create_extension_list(patterns, exclude=None, ctx=None, aliases=None, quiet=False, language=None,
  626. exclude_failures=False):
  627. if language is not None:
  628. print('Please put "# distutils: language=%s" in your .pyx or .pxd file(s)' % language)
  629. if exclude is None:
  630. exclude = []
  631. if patterns is None:
  632. return [], {}
  633. elif isinstance(patterns, basestring) or not isinstance(patterns, collections.Iterable):
  634. patterns = [patterns]
  635. explicit_modules = set([m.name for m in patterns if isinstance(m, Extension)])
  636. seen = set()
  637. deps = create_dependency_tree(ctx, quiet=quiet)
  638. to_exclude = set()
  639. if not isinstance(exclude, list):
  640. exclude = [exclude]
  641. for pattern in exclude:
  642. to_exclude.update(map(os.path.abspath, extended_iglob(pattern)))
  643. module_list = []
  644. module_metadata = {}
  645. # workaround for setuptools
  646. if 'setuptools' in sys.modules:
  647. Extension_distutils = sys.modules['setuptools.extension']._Extension
  648. Extension_setuptools = sys.modules['setuptools'].Extension
  649. else:
  650. # dummy class, in case we do not have setuptools
  651. Extension_distutils = Extension
  652. class Extension_setuptools(Extension): pass
  653. # if no create_extension() function is defined, use a simple
  654. # default function.
  655. create_extension = ctx.options.create_extension or default_create_extension
  656. for pattern in patterns:
  657. if isinstance(pattern, str):
  658. filepattern = pattern
  659. template = Extension(pattern, []) # Fake Extension without sources
  660. name = '*'
  661. base = None
  662. ext_language = language
  663. elif isinstance(pattern, (Extension_distutils, Extension_setuptools)):
  664. cython_sources = [s for s in pattern.sources
  665. if os.path.splitext(s)[1] in ('.py', '.pyx')]
  666. if cython_sources:
  667. filepattern = cython_sources[0]
  668. if len(cython_sources) > 1:
  669. print("Warning: Multiple cython sources found for extension '%s': %s\n"
  670. "See http://cython.readthedocs.io/en/latest/src/userguide/sharing_declarations.html "
  671. "for sharing declarations among Cython files." % (pattern.name, cython_sources))
  672. else:
  673. # ignore non-cython modules
  674. module_list.append(pattern)
  675. continue
  676. template = pattern
  677. name = template.name
  678. base = DistutilsInfo(exn=template)
  679. ext_language = None # do not override whatever the Extension says
  680. else:
  681. msg = str("pattern is not of type str nor subclass of Extension (%s)"
  682. " but of type %s and class %s" % (repr(Extension),
  683. type(pattern),
  684. pattern.__class__))
  685. raise TypeError(msg)
  686. for file in nonempty(sorted(extended_iglob(filepattern)), "'%s' doesn't match any files" % filepattern):
  687. if os.path.abspath(file) in to_exclude:
  688. continue
  689. module_name = deps.fully_qualified_name(file)
  690. if '*' in name:
  691. if module_name in explicit_modules:
  692. continue
  693. elif name:
  694. module_name = name
  695. if module_name == 'cython':
  696. raise ValueError('cython is a special module, cannot be used as a module name')
  697. if module_name not in seen:
  698. try:
  699. kwds = deps.distutils_info(file, aliases, base).values
  700. except Exception:
  701. if exclude_failures:
  702. continue
  703. raise
  704. if base is not None:
  705. for key, value in base.values.items():
  706. if key not in kwds:
  707. kwds[key] = value
  708. kwds['name'] = module_name
  709. sources = [file] + [m for m in template.sources if m != filepattern]
  710. if 'sources' in kwds:
  711. # allow users to add .c files etc.
  712. for source in kwds['sources']:
  713. source = encode_filename_in_py2(source)
  714. if source not in sources:
  715. sources.append(source)
  716. kwds['sources'] = sources
  717. if ext_language and 'language' not in kwds:
  718. kwds['language'] = ext_language
  719. np_pythran = kwds.pop('np_pythran', False)
  720. # Create the new extension
  721. m, metadata = create_extension(template, kwds)
  722. m.np_pythran = np_pythran or getattr(m, 'np_pythran', False)
  723. if m.np_pythran:
  724. update_pythran_extension(m)
  725. module_list.append(m)
  726. # Store metadata (this will be written as JSON in the
  727. # generated C file but otherwise has no purpose)
  728. module_metadata[module_name] = metadata
  729. if file not in m.sources:
  730. # Old setuptools unconditionally replaces .pyx with .c/.cpp
  731. target_file = os.path.splitext(file)[0] + ('.cpp' if m.language == 'c++' else '.c')
  732. try:
  733. m.sources.remove(target_file)
  734. except ValueError:
  735. # never seen this in the wild, but probably better to warn about this unexpected case
  736. print("Warning: Cython source file not found in sources list, adding %s" % file)
  737. m.sources.insert(0, file)
  738. seen.add(name)
  739. return module_list, module_metadata
  740. # This is the user-exposed entry point.
  741. def cythonize(module_list, exclude=None, nthreads=0, aliases=None, quiet=False, force=False, language=None,
  742. exclude_failures=False, **options):
  743. """
  744. Compile a set of source modules into C/C++ files and return a list of distutils
  745. Extension objects for them.
  746. As module list, pass either a glob pattern, a list of glob patterns or a list of
  747. Extension objects. The latter allows you to configure the extensions separately
  748. through the normal distutils options.
  749. When using glob patterns, you can exclude certain module names explicitly
  750. by passing them into the 'exclude' option.
  751. To globally enable C++ mode, you can pass language='c++'. Otherwise, this
  752. will be determined at a per-file level based on compiler directives. This
  753. affects only modules found based on file names. Extension instances passed
  754. into cythonize() will not be changed.
  755. For parallel compilation, set the 'nthreads' option to the number of
  756. concurrent builds.
  757. For a broad 'try to compile' mode that ignores compilation failures and
  758. simply excludes the failed extensions, pass 'exclude_failures=True'. Note
  759. that this only really makes sense for compiling .py files which can also
  760. be used without compilation.
  761. Additional compilation options can be passed as keyword arguments.
  762. """
  763. if exclude is None:
  764. exclude = []
  765. if 'include_path' not in options:
  766. options['include_path'] = ['.']
  767. if 'common_utility_include_dir' in options:
  768. if options.get('cache'):
  769. raise NotImplementedError("common_utility_include_dir does not yet work with caching")
  770. safe_makedirs(options['common_utility_include_dir'])
  771. pythran_options = None
  772. if PythranAvailable:
  773. pythran_options = CompilationOptions(**options)
  774. pythran_options.cplus = True
  775. pythran_options.np_pythran = True
  776. c_options = CompilationOptions(**options)
  777. cpp_options = CompilationOptions(**options); cpp_options.cplus = True
  778. ctx = c_options.create_context()
  779. options = c_options
  780. module_list, module_metadata = create_extension_list(
  781. module_list,
  782. exclude=exclude,
  783. ctx=ctx,
  784. quiet=quiet,
  785. exclude_failures=exclude_failures,
  786. language=language,
  787. aliases=aliases)
  788. deps = create_dependency_tree(ctx, quiet=quiet)
  789. build_dir = getattr(options, 'build_dir', None)
  790. def copy_to_build_dir(filepath, root=os.getcwd()):
  791. filepath_abs = os.path.abspath(filepath)
  792. if os.path.isabs(filepath):
  793. filepath = filepath_abs
  794. if filepath_abs.startswith(root):
  795. # distutil extension depends are relative to cwd
  796. mod_dir = join_path(build_dir,
  797. os.path.dirname(_relpath(filepath, root)))
  798. copy_once_if_newer(filepath_abs, mod_dir)
  799. modules_by_cfile = collections.defaultdict(list)
  800. to_compile = []
  801. for m in module_list:
  802. if build_dir:
  803. for dep in m.depends:
  804. copy_to_build_dir(dep)
  805. cy_sources = [
  806. source for source in m.sources
  807. if os.path.splitext(source)[1] in ('.pyx', '.py')]
  808. if len(cy_sources) == 1:
  809. # normal "special" case: believe the Extension module name to allow user overrides
  810. full_module_name = m.name
  811. else:
  812. # infer FQMN from source files
  813. full_module_name = None
  814. new_sources = []
  815. for source in m.sources:
  816. base, ext = os.path.splitext(source)
  817. if ext in ('.pyx', '.py'):
  818. if m.np_pythran:
  819. c_file = base + '.cpp'
  820. options = pythran_options
  821. elif m.language == 'c++':
  822. c_file = base + '.cpp'
  823. options = cpp_options
  824. else:
  825. c_file = base + '.c'
  826. options = c_options
  827. # setup for out of place build directory if enabled
  828. if build_dir:
  829. c_file = os.path.join(build_dir, c_file)
  830. dir = os.path.dirname(c_file)
  831. safe_makedirs_once(dir)
  832. if os.path.exists(c_file):
  833. c_timestamp = os.path.getmtime(c_file)
  834. else:
  835. c_timestamp = -1
  836. # Priority goes first to modified files, second to direct
  837. # dependents, and finally to indirect dependents.
  838. if c_timestamp < deps.timestamp(source):
  839. dep_timestamp, dep = deps.timestamp(source), source
  840. priority = 0
  841. else:
  842. dep_timestamp, dep = deps.newest_dependency(source)
  843. priority = 2 - (dep in deps.immediate_dependencies(source))
  844. if force or c_timestamp < dep_timestamp:
  845. if not quiet and not force:
  846. if source == dep:
  847. print("Compiling %s because it changed." % source)
  848. else:
  849. print("Compiling %s because it depends on %s." % (source, dep))
  850. if not force and options.cache:
  851. extra = m.language
  852. fingerprint = deps.transitive_fingerprint(source, extra)
  853. else:
  854. fingerprint = None
  855. to_compile.append((
  856. priority, source, c_file, fingerprint, quiet,
  857. options, not exclude_failures, module_metadata.get(m.name),
  858. full_module_name))
  859. new_sources.append(c_file)
  860. modules_by_cfile[c_file].append(m)
  861. else:
  862. new_sources.append(source)
  863. if build_dir:
  864. copy_to_build_dir(source)
  865. m.sources = new_sources
  866. if options.cache:
  867. if not os.path.exists(options.cache):
  868. os.makedirs(options.cache)
  869. to_compile.sort()
  870. # Drop "priority" component of "to_compile" entries and add a
  871. # simple progress indicator.
  872. N = len(to_compile)
  873. progress_fmt = "[{0:%d}/{1}] " % len(str(N))
  874. for i in range(N):
  875. progress = progress_fmt.format(i+1, N)
  876. to_compile[i] = to_compile[i][1:] + (progress,)
  877. if N <= 1:
  878. nthreads = 0
  879. if nthreads:
  880. # Requires multiprocessing (or Python >= 2.6)
  881. try:
  882. import multiprocessing
  883. pool = multiprocessing.Pool(
  884. nthreads, initializer=_init_multiprocessing_helper)
  885. except (ImportError, OSError):
  886. print("multiprocessing required for parallel cythonization")
  887. nthreads = 0
  888. else:
  889. # This is a bit more involved than it should be, because KeyboardInterrupts
  890. # break the multiprocessing workers when using a normal pool.map().
  891. # See, for example:
  892. # http://noswap.com/blog/python-multiprocessing-keyboardinterrupt
  893. try:
  894. result = pool.map_async(cythonize_one_helper, to_compile, chunksize=1)
  895. pool.close()
  896. while not result.ready():
  897. try:
  898. result.get(99999) # seconds
  899. except multiprocessing.TimeoutError:
  900. pass
  901. except KeyboardInterrupt:
  902. pool.terminate()
  903. raise
  904. pool.join()
  905. if not nthreads:
  906. for args in to_compile:
  907. cythonize_one(*args)
  908. if exclude_failures:
  909. failed_modules = set()
  910. for c_file, modules in modules_by_cfile.items():
  911. if not os.path.exists(c_file):
  912. failed_modules.update(modules)
  913. elif os.path.getsize(c_file) < 200:
  914. f = io_open(c_file, 'r', encoding='iso8859-1')
  915. try:
  916. if f.read(len('#error ')) == '#error ':
  917. # dead compilation result
  918. failed_modules.update(modules)
  919. finally:
  920. f.close()
  921. if failed_modules:
  922. for module in failed_modules:
  923. module_list.remove(module)
  924. print("Failed compilations: %s" % ', '.join(sorted([
  925. module.name for module in failed_modules])))
  926. if options.cache:
  927. cleanup_cache(options.cache, getattr(options, 'cache_size', 1024 * 1024 * 100))
  928. # cythonize() is often followed by the (non-Python-buffered)
  929. # compiler output, flush now to avoid interleaving output.
  930. sys.stdout.flush()
  931. return module_list
  932. if os.environ.get('XML_RESULTS'):
  933. compile_result_dir = os.environ['XML_RESULTS']
  934. def record_results(func):
  935. def with_record(*args):
  936. t = time.time()
  937. success = True
  938. try:
  939. try:
  940. func(*args)
  941. except:
  942. success = False
  943. finally:
  944. t = time.time() - t
  945. module = fully_qualified_name(args[0])
  946. name = "cythonize." + module
  947. failures = 1 - success
  948. if success:
  949. failure_item = ""
  950. else:
  951. failure_item = "failure"
  952. output = open(os.path.join(compile_result_dir, name + ".xml"), "w")
  953. output.write("""
  954. <?xml version="1.0" ?>
  955. <testsuite name="%(name)s" errors="0" failures="%(failures)s" tests="1" time="%(t)s">
  956. <testcase classname="%(name)s" name="cythonize">
  957. %(failure_item)s
  958. </testcase>
  959. </testsuite>
  960. """.strip() % locals())
  961. output.close()
  962. return with_record
  963. else:
  964. def record_results(func):
  965. return func
  966. # TODO: Share context? Issue: pyx processing leaks into pxd module
  967. @record_results
  968. def cythonize_one(pyx_file, c_file, fingerprint, quiet, options=None,
  969. raise_on_failure=True, embedded_metadata=None, full_module_name=None,
  970. progress=""):
  971. from ..Compiler.Main import compile_single, default_options
  972. from ..Compiler.Errors import CompileError, PyrexError
  973. if fingerprint:
  974. if not os.path.exists(options.cache):
  975. safe_makedirs(options.cache)
  976. # Cython-generated c files are highly compressible.
  977. # (E.g. a compression ratio of about 10 for Sage).
  978. fingerprint_file = join_path(
  979. options.cache, "%s-%s%s" % (os.path.basename(c_file), fingerprint, gzip_ext))
  980. if os.path.exists(fingerprint_file):
  981. if not quiet:
  982. print("%sFound compiled %s in cache" % (progress, pyx_file))
  983. os.utime(fingerprint_file, None)
  984. g = gzip_open(fingerprint_file, 'rb')
  985. try:
  986. f = open(c_file, 'wb')
  987. try:
  988. shutil.copyfileobj(g, f)
  989. finally:
  990. f.close()
  991. finally:
  992. g.close()
  993. return
  994. if not quiet:
  995. print("%sCythonizing %s" % (progress, pyx_file))
  996. if options is None:
  997. options = CompilationOptions(default_options)
  998. options.output_file = c_file
  999. options.embedded_metadata = embedded_metadata
  1000. any_failures = 0
  1001. try:
  1002. result = compile_single(pyx_file, options, full_module_name=full_module_name)
  1003. if result.num_errors > 0:
  1004. any_failures = 1
  1005. except (EnvironmentError, PyrexError) as e:
  1006. sys.stderr.write('%s\n' % e)
  1007. any_failures = 1
  1008. # XXX
  1009. import traceback
  1010. traceback.print_exc()
  1011. except Exception:
  1012. if raise_on_failure:
  1013. raise
  1014. import traceback
  1015. traceback.print_exc()
  1016. any_failures = 1
  1017. if any_failures:
  1018. if raise_on_failure:
  1019. raise CompileError(None, pyx_file)
  1020. elif os.path.exists(c_file):
  1021. os.remove(c_file)
  1022. elif fingerprint:
  1023. f = open(c_file, 'rb')
  1024. try:
  1025. g = gzip_open(fingerprint_file, 'wb')
  1026. try:
  1027. shutil.copyfileobj(f, g)
  1028. finally:
  1029. g.close()
  1030. finally:
  1031. f.close()
  1032. def cythonize_one_helper(m):
  1033. import traceback
  1034. try:
  1035. return cythonize_one(*m)
  1036. except Exception:
  1037. traceback.print_exc()
  1038. raise
  1039. def _init_multiprocessing_helper():
  1040. # KeyboardInterrupt kills workers, so don't let them get it
  1041. import signal
  1042. signal.signal(signal.SIGINT, signal.SIG_IGN)
  1043. def cleanup_cache(cache, target_size, ratio=.85):
  1044. try:
  1045. p = subprocess.Popen(['du', '-s', '-k', os.path.abspath(cache)], stdout=subprocess.PIPE)
  1046. res = p.wait()
  1047. if res == 0:
  1048. total_size = 1024 * int(p.stdout.read().strip().split()[0])
  1049. if total_size < target_size:
  1050. return
  1051. except (OSError, ValueError):
  1052. pass
  1053. total_size = 0
  1054. all = []
  1055. for file in os.listdir(cache):
  1056. path = join_path(cache, file)
  1057. s = os.stat(path)
  1058. total_size += s.st_size
  1059. all.append((s.st_atime, s.st_size, path))
  1060. if total_size > target_size:
  1061. for time, size, file in reversed(sorted(all)):
  1062. os.unlink(file)
  1063. total_size -= size
  1064. if total_size < target_size * ratio:
  1065. break