__init__.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import os
  2. import os.path
  3. from devtools.yamaker import fileutil
  4. from devtools.yamaker import pathutil
  5. from devtools.yamaker.arcpath import ArcPath
  6. from devtools.yamaker.modules import Linkable, Switch
  7. from devtools.yamaker.project import NixProject
  8. def make_full_path(src):
  9. return os.path.join("src/google/protobuf", src)
  10. RUNTIME_EXCESS_SOURCES = [
  11. "src/google/protobuf/compiler/importer.cc",
  12. "src/google/protobuf/compiler/parser.cc",
  13. ]
  14. RUNTIME_YANDEX_SPECIFIC_SOURCES = [
  15. "src/google/protobuf/json_util.cc",
  16. "src/google/protobuf/messagext.cc",
  17. ]
  18. PROTOC_YANDEX_SPECIFIC_SOURCES = [
  19. "src/google/protobuf/compiler/perlxs/perlxs_helpers.cc",
  20. "src/google/protobuf/compiler/perlxs/perlxs_generator.cc",
  21. ]
  22. # Set of proto files coming with original google protobuf (excluding descriptor.proto, see below)
  23. # WARN: upon changing this file, make sure to check protobuf_std counterpart.
  24. RUNTIME_PROTO_FILES = [
  25. "src/google/protobuf/any.proto",
  26. "src/google/protobuf/api.proto",
  27. "src/google/protobuf/descriptor.proto",
  28. "src/google/protobuf/duration.proto",
  29. "src/google/protobuf/empty.proto",
  30. "src/google/protobuf/field_mask.proto",
  31. "src/google/protobuf/source_context.proto",
  32. "src/google/protobuf/struct.proto",
  33. "src/google/protobuf/timestamp.proto",
  34. "src/google/protobuf/type.proto",
  35. "src/google/protobuf/wrappers.proto",
  36. ]
  37. PROTOC_PROTO_FILES = [
  38. "src/google/protobuf/compiler/plugin.proto",
  39. ]
  40. LIBPROTOC_DIR = "contrib/libs/protoc"
  41. PYTHON_DIR = "contrib/python/protobuf/py3"
  42. POSSIBLE_STD_STRING_USAGE_PATTERNS = [
  43. # It can be referenced to as `::std::string` or `std::string`
  44. r"::std::string",
  45. r"\bstd::string\b",
  46. ]
  47. def post_build(self):
  48. # Replace std::string with TProtoStringType.
  49. for pattern in POSSIBLE_STD_STRING_USAGE_PATTERNS:
  50. fileutil.re_sub_dir(
  51. self.dstdir,
  52. pattern,
  53. "TProtoStringType",
  54. # Only apply replacements to C++ code
  55. test=pathutil.is_preprocessable,
  56. )
  57. def post_install(self):
  58. with self.yamakes["."] as libprotobuf:
  59. libprotobuf.PROVIDES = ["protobuf"]
  60. libprotobuf.SRCS.update(RUNTIME_YANDEX_SPECIFIC_SOURCES)
  61. libprotobuf.NO_UTIL = False
  62. # Work around fixed_address_empty_string initialization on macOS.
  63. gmu = "src/google/protobuf/generated_message_util.cc"
  64. libprotobuf.SRCS.remove(gmu)
  65. libprotobuf.SRCS.add(ArcPath(gmu, GLOBAL=True))
  66. # These sources are parts of protoc, they should not be linked into runtime
  67. for src in RUNTIME_EXCESS_SOURCES:
  68. libprotobuf.SRCS.remove(src)
  69. libprotobuf.after(
  70. "CFLAGS",
  71. Switch(
  72. OS_ANDROID=Linkable(
  73. # Link with system android log library
  74. # Android logging is used in stubs/common.cc
  75. EXTRALIBS=["log"]
  76. )
  77. ),
  78. )
  79. libprotobuf.ADDINCL = [
  80. ArcPath(self.arcdir + "/src", GLOBAL=True),
  81. ArcPath(self.arcdir + "/src", GLOBAL=True, FOR="proto"),
  82. ]
  83. libprotobuf.after("SRCS", Linkable(FILES=RUNTIME_PROTO_FILES))
  84. libprotobuf.RECURSE = ["builtin_proto"]
  85. libprotobuf.PEERDIR.add("library/cpp/sanitizer/include")
  86. del self.yamakes["src/google/protobuf/compiler"]
  87. # merging src/google/protobuf/compiler/protoc library and
  88. # src/google/protobuf/compiler binary into top-level binary
  89. with self.yamakes.pop("src/google/protobuf/compiler/protoc") as libprotoc:
  90. libprotoc.VERSION = self.version
  91. libprotoc.ORIGINAL_SOURCE = self.source_url
  92. libprotoc.PROVIDES = ["protoc"]
  93. libprotoc.after("LICENSE", "LICENSE_TEXTS(.yandex_meta/licenses.list.txt)\n")
  94. libprotoc.SRCS = {os.path.join("src/google/protobuf/compiler", src) for src in libprotoc.SRCS}
  95. # Moving a couple of sources from runtime library to compiler (where they actually belong)
  96. libprotoc.SRCS.update(RUNTIME_EXCESS_SOURCES)
  97. libprotoc.SRCS.update(PROTOC_YANDEX_SPECIFIC_SOURCES)
  98. libprotoc.ADDINCL = [
  99. ArcPath(LIBPROTOC_DIR + "/src", GLOBAL=True),
  100. ]
  101. libprotoc.SRCDIR = None
  102. # Unbundle libprotobuf.la which confuses yamaker by being linked statically
  103. libprotoc.PEERDIR = {self.arcdir}
  104. libprotoc_abs_dir = os.path.join(self.ctx.arc, LIBPROTOC_DIR)
  105. fileutil.copy(
  106. os.path.join(self.dstdir, "src/google/protobuf/compiler"),
  107. os.path.join(libprotoc_abs_dir, "src/google/protobuf"),
  108. replace=True,
  109. move=True,
  110. )
  111. fileutil.copy(
  112. [os.path.join(self.dstdir, "LICENSE")],
  113. libprotoc_abs_dir,
  114. replace=True,
  115. move=False,
  116. )
  117. with open(f"{libprotoc_abs_dir}/ya.make", "wt") as ymake:
  118. ymake.write(str(libprotoc))
  119. protobuf = NixProject(
  120. owners=["g:cpp-committee", "g:cpp-contrib"],
  121. arcdir="contrib/libs/protobuf",
  122. nixattr="protobuf",
  123. license_analysis_extra_dirs=[
  124. LIBPROTOC_DIR,
  125. ],
  126. install_targets=[
  127. # Do not install protobuf lite, as nobody needs it
  128. "protobuf",
  129. # Specifying protoc will install both libprotoc and protoc executable
  130. "protoc",
  131. ],
  132. put={"protobuf": "."},
  133. disable_includes=[
  134. "sys/isa_defs.h",
  135. ],
  136. keep_paths=[
  137. # ya.make generation for legacy PACKAGE at protobuf/python/ya.make is not configure by yamaker.
  138. "python/ya.make",
  139. # built-in protobufs need to be exposed via PROTO_LIBRARY.
  140. # Needed at least for Python and for complete decsriptors generation
  141. "builtin_proto",
  142. # yandex-specific files. Should be moved out of the project, if possible
  143. "src/google/protobuf/json_util.*",
  144. "src/google/protobuf/messagext.*",
  145. "src/google/protobuf/compiler/perlxs/perlxs_generator.*",
  146. "src/google/protobuf/compiler/perlxs/perlxs_helpers.*",
  147. ],
  148. copy_sources=(
  149. RUNTIME_PROTO_FILES
  150. + PROTOC_PROTO_FILES
  151. + [
  152. # java_names.h is required by contrib/libs/grpc-java
  153. "src/google/protobuf/compiler/java/java_names.h",
  154. ]
  155. ),
  156. post_build=post_build,
  157. post_install=post_install,
  158. )