__init__.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import copy
  2. import json
  3. import os
  4. import os.path as P
  5. import shutil
  6. from devtools.yamaker import boost
  7. from devtools.yamaker.fileutil import re_sub_dir, re_sub_file
  8. from devtools.yamaker.project import CMakeNinjaNixProject
  9. RUNTIMES = {
  10. "core2": [
  11. # intentionally empty
  12. ],
  13. "corei7": [
  14. "${SSE41_CFLAGS}",
  15. "-DHAVE_SSE41",
  16. "${SSE42_CFLAGS}",
  17. "-DHAVE_SSE42",
  18. "${POPCNT_CFLAGS}",
  19. "-DHAVE_POPCOUNT_INSTR",
  20. ],
  21. "avx2": [
  22. "${SSE41_CFLAGS}",
  23. "-DHAVE_SSE41",
  24. "${SSE42_CFLAGS}",
  25. "-DHAVE_SSE42",
  26. "${POPCNT_CFLAGS}",
  27. "-DHAVE_POPCOUNT_INSTR",
  28. "${AVX_CFLAGS}",
  29. "-DHAVE_AVX",
  30. "${AVX2_CFLAGS}",
  31. "-DHAVE_AVX2",
  32. ],
  33. "avx512": [
  34. "${SSE41_CFLAGS}",
  35. "-DHAVE_SSE41",
  36. "${SSE42_CFLAGS}",
  37. "-DHAVE_SSE42",
  38. "-DHAVE_POPCOUNT_INSTR",
  39. "${POPCNT_CFLAGS}",
  40. "${AVX_CFLAGS}",
  41. "-DHAVE_AVX",
  42. "${AVX2_CFLAGS}",
  43. "-DHAVE_AVX2",
  44. "${AVX512_CFLAGS}",
  45. "-DHAVE_AVX512",
  46. ],
  47. }
  48. def instantiate_runtime(self, *, runtime_name, runtime_yamake):
  49. runtime_subdir = f"runtime_{runtime_name}"
  50. self.yamakes[runtime_subdir] = copy.deepcopy(runtime_yamake)
  51. with self.yamakes[runtime_subdir] as runtime:
  52. runtime_dir = f"{self.dstdir}/{runtime_subdir}"
  53. os.makedirs(runtime_dir)
  54. runtime.CFLAGS = RUNTIMES[runtime_name]
  55. # list of symbols that will be duplicated if compiled without proper wrapping.
  56. # It can be obtained with compiling runtime_* libraries and applying
  57. # nm --defined-only --extern-only --format=posix -o *.a | awk '{print $2}'
  58. with open(P.join(self.meta_dir, 'symbols.json')) as f:
  59. symbols_to_rename = json.load(f)
  60. # rename symbols that would be duplicated between runtimes otherwise
  61. runtime.CFLAGS += [f"-D{symbol}={runtime_name}_{symbol}" for symbol in sorted(symbols_to_rename)]
  62. # TODO: understand if this dispatcher is intended to work at all
  63. runtime.SRCS.remove("src/dispatcher.c")
  64. # copy headers and rename hs_ entrypoints to make them match the ones
  65. for header in ("hs_common.h", "hs_runtime.h"):
  66. runtime_specific_header = f"{runtime_dir}/{header}"
  67. shutil.copy(f"{self.dstdir}/src/{header}", runtime_specific_header)
  68. re_sub_file(
  69. runtime_specific_header,
  70. "HS_CDECL hs_",
  71. f"{runtime_name}_hs_",
  72. )
  73. # Fix include guards to allow inclusions into a single
  74. # library/cpp/regex/hyperscan/hyperscan.cpp
  75. re_sub_file(
  76. f"{runtime_dir}/hs_common.h",
  77. "HS_COMMON_H_",
  78. f"HS_{runtime_name.upper()}_COMMON_H",
  79. )
  80. re_sub_file(
  81. f"{runtime_dir}/hs_runtime.h",
  82. "HS_RUNTIME_H_",
  83. f"HS_{runtime_name.upper()}_RUNTIME_H",
  84. )
  85. def post_install(self):
  86. # make all SRCS to start with src in order
  87. # to make hierarchy match with hyperscan compiler
  88. with self.yamakes["runtime"] as runtime:
  89. runtime.SRCDIR = [self.arcdir]
  90. runtime.SRCS = [f"src/{path}" for path in runtime.SRCS]
  91. runtime_yamake = self.yamakes.pop("runtime")
  92. for runtime_name in RUNTIMES.keys():
  93. instantiate_runtime(self, runtime_name=runtime_name, runtime_yamake=runtime_yamake)
  94. with self.yamakes["runtime_avx512"] as m:
  95. # Do not sanitize to workaround the ICE in clang-16
  96. # See DEVTOOLSSUPPORT-49258 for details.
  97. m.NO_SANITIZE = True
  98. with self.yamakes["."] as hyperscan:
  99. hyperscan.RECURSE = [f"runtime_{name}" for name in sorted(RUNTIMES.keys())]
  100. # rename make_unique into std::make_unique to resolve ambigousness with boost::make_unique
  101. re_sub_dir(
  102. self.dstdir,
  103. r"([ \(])make_unique<",
  104. r"\1std::make_unique<",
  105. )
  106. hyperscan.PEERDIR += [
  107. boost.make_arcdir("dynamic_bitset"),
  108. boost.make_arcdir("graph"),
  109. boost.make_arcdir("icl"),
  110. boost.make_arcdir("multi_array"),
  111. boost.make_arcdir("property_map"),
  112. ]
  113. # TODO: understand if this dispatcher is intended to work at all
  114. hyperscan.SRCS.remove("src/dispatcher.c")
  115. os.remove(f"{self.dstdir}/src/dispatcher.c")
  116. # rename .rl sources to rl6 so they could be recognized by ymake
  117. ragel_srcs = [src for src in hyperscan.SRCS if src.endswith(".rl")]
  118. for ragel_src in ragel_srcs:
  119. os.rename(f"{self.dstdir}/{ragel_src}", f"{self.dstdir}/{ragel_src}6")
  120. hyperscan.SRCS.remove(ragel_src)
  121. hyperscan.SRCS.add(ragel_src + "6")
  122. hyperscan = CMakeNinjaNixProject(
  123. owners=[
  124. "galtsev",
  125. "g:antiinfra",
  126. "g:cpp-contrib",
  127. "g:yql",
  128. ],
  129. arcdir="contrib/libs/hyperscan",
  130. nixattr="hyperscan",
  131. install_targets=[
  132. "hs",
  133. "hs_runtime",
  134. ],
  135. put={
  136. "hs": ".",
  137. "hs_runtime": "runtime",
  138. },
  139. platform_dispatchers=["config.h"],
  140. post_install=post_install,
  141. )