setup-cython-tests.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #
  2. # Build Awlsim Cython test cases
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. from __future__ import division, absolute_import, print_function
  15. # Avoid __future__.unicode_literals. It breaks on pypy2.
  16. import sys, os, re
  17. basedir = os.path.abspath(os.path.dirname(__file__))
  18. # Add the basedir and basedir/misc to PYTHONPATH before
  19. # we try to import setup_cython.
  20. for base in (os.getcwd(), basedir):
  21. sys.path.insert(0, os.path.join(base, "..", "misc"))
  22. sys.path.insert(0, base)
  23. from distutils.core import setup
  24. import setup_cython
  25. # Find the test case directories.
  26. packages = []
  27. for tc_dir in os.listdir(basedir):
  28. if not re.match(r"^tc\d\d\d_[\w\d_\-]+$", tc_dir):
  29. continue
  30. tc_dir = os.path.join(basedir, tc_dir)
  31. if not os.path.isdir(tc_dir):
  32. continue
  33. for dirpath, dirnames, filenames in os.walk(tc_dir):
  34. if "no_cython" in filenames:
  35. continue
  36. if not any(f.endswith(".py") and f != "__init__.py"
  37. for f in filenames):
  38. continue
  39. packages.append(os.path.relpath(dirpath, basedir))
  40. # Generate an __init__.py, so that the directory
  41. # is a valid Python package.
  42. initpy = os.path.join(tc_dir, "__init__.py")
  43. if not os.path.exists(initpy):
  44. with open(initpy, "w") as fd:
  45. fd.write("# GENERATED dummy __init__.py file\n")
  46. print("Discovered test case packages:", ", ".join(packages))
  47. # Setup Cython build.
  48. def pyCythonPatchLine(line):
  49. # Patch the import statements
  50. line = re.sub(r'^(\s*from awlsim[0-9a-zA-Z_]*)\.([0-9a-zA-Z_\.]+) import', r'\1_cython.\2 import', line)
  51. line = re.sub(r'^(\s*from awlsim[0-9a-zA-Z_]*)\.([0-9a-zA-Z_\.]+) cimport', r'\1_cython.\2 cimport', line)
  52. line = re.sub(r'^(\s*import awlsim[0-9a-zA-Z_]*)\.', r'\1_cython.', line)
  53. line = re.sub(r'^(\s*cimport awlsim[0-9a-zA-Z_]*)\.', r'\1_cython.', line)
  54. return line
  55. os.environ["CFLAGS"] = os.environ["CXXFLAGS"] = "-O0"
  56. os.environ["CPPFLAGS"] = ""
  57. os.environ["LDFLAGS"] = ""
  58. if not setup_cython.cythonBuildPossible():
  59. print("ERROR: Cannot build Cython modules.", file=sys.stderr)
  60. sys.exit(1)
  61. cmdclass = {}
  62. cmdclass["build_ext"] = setup_cython.CythonBuildExtension
  63. setup_cython.setupFileName = os.path.basename(__file__)
  64. setup_cython.parallelBuild = True
  65. setup_cython.pyCythonPatchLine = pyCythonPatchLine
  66. setup_cython.registerCythonModules()
  67. ext_modules = setup_cython.ext_modules
  68. # Create links to the awlsim packages.
  69. awlsimBuildPatchDir = os.path.join("..", "build", setup_cython.patchDirName)
  70. if not os.path.isdir(awlsimBuildPatchDir):
  71. print(("Awlsim build directory '%s' does not exist.\n"
  72. "Has awlsim been built?") % awlsimBuildPatchDir,
  73. file=sys.stderr)
  74. sys.exit(1)
  75. for awlsimPack in os.listdir(awlsimBuildPatchDir):
  76. linkFrom = os.path.join("..", "..", awlsimBuildPatchDir, awlsimPack)
  77. linkToDir = os.path.join("build", setup_cython.patchDirName)
  78. linkTo = os.path.join(linkToDir, awlsimPack)
  79. if not os.path.lexists(linkTo):
  80. print("Linking awlsim package '%s' to '%s'" % (linkFrom, linkTo))
  81. os.makedirs(linkToDir, exist_ok=True)
  82. os.symlink(linkFrom, linkTo)
  83. setup( name = "awlsim-cython-unittests",
  84. packages = packages,
  85. cmdclass = cmdclass,
  86. ext_modules = ext_modules,
  87. )