setup_cython.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. #
  2. # Cython patcher
  3. # v1.17
  4. #
  5. # Copyright (C) 2012-2018 Michael Buesch <m@bues.ch>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. from __future__ import print_function
  21. import sys
  22. import os
  23. import platform
  24. import errno
  25. import shutil
  26. import hashlib
  27. import re
  28. WORKER_MEM_BYTES = 800 * 1024*1024
  29. WORKER_CPU_OVERCOMMIT = 2
  30. parallelBuild = False
  31. profileEnabled = False
  32. ext_modules = []
  33. CythonBuildExtension = None
  34. _Cython_Distutils_build_ext = None
  35. _cythonPossible = None
  36. _cythonBuildUnits = []
  37. _isWindows = os.name.lower() in {"nt", "ce"}
  38. _isPosix = os.name.lower() == "posix"
  39. def getSystemMemBytesCount():
  40. try:
  41. with open("/proc/meminfo", "rb") as fd:
  42. for line in fd.read().decode("UTF-8", "ignore").splitlines():
  43. if line.startswith("MemTotal:") and\
  44. line.endswith("kB"):
  45. kB = int(line.split()[1], 10)
  46. return kB * 1024
  47. except (OSError, IndexError, ValueError, UnicodeError) as e:
  48. pass
  49. if hasattr(os, "sysconf"):
  50. try:
  51. return os.sysconf("SC_PAGE_SIZE") * os.sysconf("SC_PHYS_PAGES")
  52. except ValueError as e:
  53. pass
  54. return None
  55. def makedirs(path, mode=0o755):
  56. try:
  57. os.makedirs(path, mode)
  58. except OSError as e:
  59. if e.errno == errno.EEXIST:
  60. return
  61. raise e
  62. def hashFile(path):
  63. if sys.version_info[0] < 3:
  64. ExpectedException = IOError
  65. else:
  66. ExpectedException = FileNotFoundError
  67. try:
  68. with open(path, "rb") as fd:
  69. return hashlib.sha1(fd.read()).hexdigest()
  70. except ExpectedException as e:
  71. return None
  72. def __fileopIfChanged(fromFile, toFile, fileops):
  73. toFileHash = hashFile(toFile)
  74. if toFileHash is not None:
  75. fromFileHash = hashFile(fromFile)
  76. if toFileHash == fromFileHash:
  77. return False
  78. makedirs(os.path.dirname(toFile))
  79. for fileop in fileops:
  80. fileop(fromFile, toFile)
  81. return True
  82. def removeFile(filename):
  83. try:
  84. os.unlink(filename)
  85. except OSError:
  86. pass
  87. def copyIfChanged(fromFile, toFile):
  88. fileops = []
  89. if _isWindows:
  90. fileops.append(lambda _fromFile, _toFile: removeFile(_toFile))
  91. fileops.append(shutil.copy2)
  92. return __fileopIfChanged(fromFile, toFile, fileops)
  93. def moveIfChanged(fromFile, toFile):
  94. fileops = []
  95. if _isWindows:
  96. fileops.append(lambda _fromFile, _toFile: removeFile(_toFile))
  97. fileops.append(os.rename)
  98. return __fileopIfChanged(fromFile, toFile, fileops)
  99. def makeDummyFile(path):
  100. if os.path.isfile(path):
  101. return
  102. print("creating dummy file '%s'" % path)
  103. makedirs(os.path.dirname(path))
  104. with open(path, "wb") as fd:
  105. fd.write("\n".encode("UTF-8"))
  106. def pyCythonPatchLine(line):
  107. return line
  108. def pyCythonPatch(fromFile, toFile):
  109. print("cython-patch: patching file '%s' to '%s'" %\
  110. (fromFile, toFile))
  111. tmpFile = toFile + ".TMP"
  112. makedirs(os.path.dirname(tmpFile))
  113. with open(fromFile, "rb") as infd,\
  114. open(tmpFile, "wb") as outfd:
  115. for line in infd.read().decode("UTF-8").splitlines(True):
  116. stripLine = line.strip()
  117. if stripLine.endswith("#@no-cython-patch"):
  118. outfd.write(line.encode("UTF-8"))
  119. continue
  120. # Replace import by cimport as requested by #+cimport
  121. if "#+cimport" in stripLine:
  122. line = line.replace("#+cimport", "#")
  123. line = re.sub(r'\bimport\b', "cimport", line)
  124. # Convert None to NULL
  125. if "#+NoneToNULL" in stripLine:
  126. line = line.replace("#+NoneToNULL", "#")
  127. line = re.sub(r'\bNone\b', "NULL", line)
  128. # Uncomment all lines containing #@cy
  129. def uncomment(line, removeStr):
  130. line = line.replace(removeStr, "")
  131. if line.startswith("#"):
  132. line = line[1:]
  133. if not line.endswith("\n"):
  134. line += "\n"
  135. return line
  136. if "#@cy-posix" in stripLine:
  137. if _isPosix:
  138. line = uncomment(line, "#@cy-posix")
  139. elif "#@cy-win" in stripLine:
  140. if _isWindows:
  141. line = uncomment(line, "#@cy-win")
  142. elif "#@cy2" in stripLine:
  143. if sys.version_info[0] < 3:
  144. line = uncomment(line, "#@cy2")
  145. elif "#@cy3" in stripLine:
  146. if sys.version_info[0] >= 3:
  147. line = uncomment(line, "#@cy3")
  148. elif "#@cy" in stripLine:
  149. line = uncomment(line, "#@cy")
  150. # Sprinkle magic cdef/cpdef, as requested by #+cdef/#+cpdef
  151. if "#+cdef-" in stripLine:
  152. # +cdef-foo-bar is the extended cdef patching.
  153. # It adds cdef and any additional characters to the
  154. # start of the line. Dashes are replaced with spaces.
  155. # Get the additional text
  156. idx = line.find("#+cdef-")
  157. cdefText = line[idx+2 : ]
  158. cdefText = cdefText.replace("-", " ").rstrip("\r\n")
  159. # Get the initial space length
  160. spaceCnt = 0
  161. while spaceCnt < len(line) and line[spaceCnt].isspace():
  162. spaceCnt += 1
  163. # Construct the new line
  164. line = line[ : spaceCnt] + cdefText + " " + line[spaceCnt : ]
  165. elif "#+cdef" in stripLine:
  166. # Simple cdef patching:
  167. # def -> cdef
  168. # class -> cdef class
  169. if stripLine.startswith("class"):
  170. line = re.sub(r'\bclass\b', "cdef class", line)
  171. else:
  172. line = re.sub(r'\bdef\b', "cdef", line)
  173. if "#+cpdef" in stripLine:
  174. # Simple cpdef patching:
  175. # def -> cpdef
  176. line = re.sub(r'\bdef\b', "cpdef", line)
  177. # Add likely()/unlikely() to if-conditions.
  178. for likely in ("likely", "unlikely"):
  179. if "#+" + likely in stripLine:
  180. line = re.sub(r'\bif\s(.*):', r'if ' + likely + r'(\1):', line)
  181. break
  182. # Add an "u" suffix to decimal and hexadecimal numbers.
  183. if "#+suffix-u" in line or "#+suffix-U" in line:
  184. line = re.sub(r'\b([0-9]+)\b', r'\1u', line)
  185. line = re.sub(r'\b(0x[0-9a-fA-F]+)\b', r'\1u', line)
  186. # Add an "LL" suffix to decimal and hexadecimal numbers.
  187. if "#+suffix-ll" in line or "#+suffix-LL" in line:
  188. line = re.sub(r'\b(\-?[0-9]+)\b', r'\1LL', line)
  189. line = re.sub(r'\b(0x[0-9a-fA-F]+)\b', r'\1LL', line)
  190. # Comment all lines containing #@nocy
  191. if "#@nocy" in stripLine:
  192. line = "#" + line
  193. # Comment all lines containing #@cyX
  194. # for the not matching version.
  195. if sys.version_info[0] < 3:
  196. if "#@cy3" in stripLine:
  197. line = "#" + line
  198. else:
  199. if "#@cy2" in stripLine:
  200. line = "#" + line
  201. # Comment all lines containing #@cy-posix/win
  202. # for the not matching platform.
  203. if _isPosix:
  204. if "#@cy-win" in stripLine:
  205. line = "#" + line
  206. elif _isWindows:
  207. if "#@cy-posix" in stripLine:
  208. line = "#" + line
  209. # Remove compat stuff
  210. line = line.replace("absolute_import,", "")
  211. line = pyCythonPatchLine(line)
  212. outfd.write(line.encode("UTF-8"))
  213. outfd.flush()
  214. if moveIfChanged(tmpFile, toFile):
  215. print("(updated)")
  216. else:
  217. os.unlink(tmpFile)
  218. class CythonBuildUnit(object):
  219. def __init__(self, cyModName, baseName, fromPy, fromPxd, toDir, toPyx, toPxd):
  220. self.cyModName = cyModName
  221. self.baseName = baseName
  222. self.fromPy = fromPy
  223. self.fromPxd = fromPxd
  224. self.toDir = toDir
  225. self.toPyx = toPyx
  226. self.toPxd = toPxd
  227. def patchCythonModules(buildDir):
  228. for unit in _cythonBuildUnits:
  229. makedirs(unit.toDir)
  230. makeDummyFile(os.path.join(unit.toDir, "__init__.py"))
  231. if unit.baseName == "__init__":
  232. # Copy and patch the package __init__.py
  233. toPy = os.path.join(buildDir, *unit.cyModName.split(".")) + ".py"
  234. pyCythonPatch(unit.fromPy, toPy)
  235. else:
  236. # Generate the .pyx
  237. pyCythonPatch(unit.fromPy, unit.toPyx)
  238. # Copy and patch the .pxd, if any
  239. if os.path.isfile(unit.fromPxd):
  240. pyCythonPatch(unit.fromPxd, unit.toPxd)
  241. def registerCythonModule(baseDir, sourceModName):
  242. global ext_modules
  243. global _cythonBuildUnits
  244. modDir = os.path.join(baseDir, sourceModName)
  245. # Make path to the cython patch-build-dir
  246. patchDir = os.path.join(baseDir, "build",
  247. "cython_patched.%s-%s-%d.%d" %\
  248. (platform.system().lower(),
  249. platform.machine().lower(),
  250. sys.version_info[0], sys.version_info[1]),
  251. "%s_cython" % sourceModName
  252. )
  253. if not os.path.exists(os.path.join(baseDir, "setup.py")) or\
  254. not os.path.exists(modDir) or\
  255. not os.path.isdir(modDir):
  256. raise Exception("Wrong directory. "
  257. "Execute setup.py from within the main directory.")
  258. # Walk the module
  259. for dirpath, dirnames, filenames in os.walk(modDir):
  260. subpath = os.path.relpath(dirpath, modDir)
  261. if subpath == baseDir:
  262. subpath = ""
  263. dirpathList = dirpath.split(os.path.sep)
  264. if any(os.path.exists(os.path.sep.join(dirpathList[:i] + ["no_cython"]))
  265. for i in range(len(dirpathList) + 1)):
  266. # no_cython file exists. -> skip
  267. continue
  268. for filename in filenames:
  269. if filename.endswith(".py"):
  270. fromSuffix = ".py"
  271. elif filename.endswith(".pyx.in"):
  272. fromSuffix = ".pyx.in"
  273. else:
  274. continue
  275. baseName = filename[:-len(fromSuffix)] # Strip .py/.pyx.in
  276. fromPy = os.path.join(dirpath, baseName + fromSuffix)
  277. fromPxd = os.path.join(dirpath, baseName + ".pxd.in")
  278. toDir = os.path.join(patchDir, subpath)
  279. toPyx = os.path.join(toDir, baseName + ".pyx")
  280. toPxd = os.path.join(toDir, baseName + ".pxd")
  281. # Construct the new cython module name
  282. cyModName = [ "%s_cython" % sourceModName ]
  283. if subpath:
  284. cyModName.extend(subpath.split(os.sep))
  285. cyModName.append(baseName)
  286. cyModName = ".".join(cyModName)
  287. # Remember the filenames for the build
  288. unit = CythonBuildUnit(cyModName, baseName, fromPy, fromPxd,
  289. toDir, toPyx, toPxd)
  290. _cythonBuildUnits.append(unit)
  291. if baseName != "__init__":
  292. # Create a distutils Extension for the module
  293. extra_compile_args = []
  294. if not _isWindows:
  295. extra_compile_args.append("-Wno-maybe-uninitialized")
  296. ext_modules.append(
  297. _Cython_Distutils_Extension(
  298. cyModName,
  299. [toPyx],
  300. cython_directives={
  301. # Enable profile hooks?
  302. "profile" : profileEnabled,
  303. "linetrace" : profileEnabled,
  304. # Warn about unused variables?
  305. "warn.unused" : False,
  306. # Set language version
  307. "language_level" : 2 if sys.version_info[0] < 3 else 3,
  308. },
  309. define_macros=[
  310. ("CYTHON_TRACE", str(int(profileEnabled))),
  311. ("CYTHON_TRACE_NOGIL", str(int(profileEnabled))),
  312. ],
  313. include_dirs=[
  314. os.path.join("libs", "cython_headers"),
  315. ],
  316. extra_compile_args=extra_compile_args
  317. )
  318. )
  319. def registerCythonModules():
  320. baseDir = os.curdir # Base directory, where setup.py lives.
  321. for filename in os.listdir(baseDir):
  322. if os.path.isdir(os.path.join(baseDir, filename)) and\
  323. os.path.exists(os.path.join(baseDir, filename, "__init__.py")) and\
  324. not os.path.exists(os.path.join(baseDir, filename, "no_cython")):
  325. registerCythonModule(baseDir, filename)
  326. def cythonBuildPossible():
  327. global _cythonPossible
  328. if _cythonPossible is not None:
  329. return _cythonPossible
  330. _cythonPossible = False
  331. try:
  332. import Cython.Compiler.Options
  333. # Omit docstrings in cythoned modules.
  334. Cython.Compiler.Options.docstrings = False
  335. # Generate module exit cleanup code.
  336. Cython.Compiler.Options.generate_cleanup_code = True
  337. # Generate HTML outputs.
  338. Cython.Compiler.Options.annotate = True
  339. from Cython.Distutils import build_ext, Extension
  340. global _Cython_Distutils_build_ext
  341. global _Cython_Distutils_Extension
  342. _Cython_Distutils_build_ext = build_ext
  343. _Cython_Distutils_Extension = Extension
  344. except ImportError as e:
  345. print("WARNING: Could not build the CYTHON modules: "
  346. "%s" % str(e))
  347. print("--> Is Cython installed?")
  348. return False
  349. _cythonPossible = True
  350. return True
  351. if sys.version_info[0] < 3:
  352. # Cython2 build libraries need method pickling
  353. # for parallel build.
  354. def unpickle_method(fname, obj, cls):
  355. # Ignore MRO. We don't seem to inherit methods.
  356. return cls.__dict__[fname].__get__(obj, cls)
  357. def pickle_method(m):
  358. return unpickle_method, (m.im_func.__name__,
  359. m.im_self,
  360. m.im_class)
  361. import copy_reg, types
  362. copy_reg.pickle(types.MethodType, pickle_method, unpickle_method)
  363. def cyBuildWrapper(arg):
  364. # This function does the same thing as the for-loop-body
  365. # inside of Cython's build_ext.build_extensions() method.
  366. # It is called via multiprocessing to build extensions
  367. # in parallel.
  368. # Note that this might break, if Cython's build_extensions()
  369. # is changed and stuff is added to its for loop. Meh.
  370. self, ext = arg
  371. ext.sources = self.cython_sources(ext.sources, ext)
  372. self.build_extension(ext)
  373. if cythonBuildPossible():
  374. # Override Cython's build_ext class.
  375. class CythonBuildExtension(_Cython_Distutils_build_ext):
  376. class Error(Exception): pass
  377. def build_extension(self, ext):
  378. assert(not ext.name.endswith("__init__"))
  379. _Cython_Distutils_build_ext.build_extension(self, ext)
  380. def build_extensions(self):
  381. global parallelBuild
  382. # First patch the files, the run the build
  383. patchCythonModules(self.build_lib)
  384. if parallelBuild:
  385. # Run the parallel build, yay.
  386. try:
  387. self.check_extensions_list(self.extensions)
  388. # Calculate the number of worker processes to use.
  389. memBytes = getSystemMemBytesCount()
  390. if memBytes is None:
  391. raise self.Error("Unknown system memory size")
  392. print("System memory detected: %d MB" % (memBytes // (1024*1024)))
  393. memProcsMax = memBytes // WORKER_MEM_BYTES
  394. if memProcsMax < 2:
  395. raise self.Error("Not enough system memory")
  396. import multiprocessing
  397. numProcs = min(multiprocessing.cpu_count() + WORKER_CPU_OVERCOMMIT,
  398. memProcsMax)
  399. # Start the worker pool.
  400. print("Building in parallel with %d workers." % numProcs)
  401. from multiprocessing.pool import Pool
  402. Pool(numProcs).map(cyBuildWrapper,
  403. ((self, ext) for ext in self.extensions))
  404. except (OSError, self.Error) as e:
  405. # OSError might happen in a restricted
  406. # environment like chroot.
  407. print("WARNING: Parallel build "
  408. "disabled due to: %s" % str(e))
  409. parallelBuild = False
  410. if not parallelBuild:
  411. # Run the normal non-parallel build.
  412. _Cython_Distutils_build_ext.build_extensions(self)