repository_rules.bzl 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. """Repository rules and macros which are expected to be called from WORKSPACE file of either
  2. googleapis itself or any third_party repository which consumes googleapis as its dependency.
  3. """
  4. def _switched_rules_impl(ctx):
  5. disabled_rule_script = """
  6. def {rule_name}(**kwargs):
  7. pass
  8. """
  9. enabled_native_rule_script = """
  10. {rule_name} = {native_rule_name}
  11. """
  12. enabled_rule_script = """
  13. load("{file_label}", _{rule_name} = "{loaded_rule_name}")
  14. """
  15. elabled_rule_scrip_alias = """
  16. {rule_name} = _{rule_name}
  17. """
  18. load_rules = [] # load() must go before everythin else in .bzl files since Bazel 0.25.0
  19. rules = []
  20. for rule_name, value_and_name in ctx.attr.rules.items():
  21. value = value_and_name[0]
  22. loaded_rule_name = value_and_name[1] if value_and_name[1] else rule_name
  23. if not value:
  24. rules.append(disabled_rule_script.format(rule_name = rule_name))
  25. elif value.startswith("@"):
  26. load_rules.append(enabled_rule_script.format(
  27. file_label = value,
  28. rule_name = rule_name,
  29. loaded_rule_name = loaded_rule_name,
  30. ))
  31. rules.append(elabled_rule_scrip_alias.format(rule_name = rule_name))
  32. elif value.startswith("native."):
  33. rules.append(
  34. enabled_native_rule_script.format(rule_name = rule_name, native_rule_name = value),
  35. )
  36. else:
  37. rules.append(value)
  38. ctx.file("BUILD.bazel", "")
  39. ctx.file("imports.bzl", "".join(load_rules + rules))
  40. switched_rules = repository_rule(
  41. implementation = _switched_rules_impl,
  42. attrs = {
  43. "rules": attr.string_list_dict(
  44. allow_empty = True,
  45. mandatory = False,
  46. default = {},
  47. ),
  48. },
  49. )
  50. def switched_rules_by_language(
  51. name,
  52. gapic = False,
  53. grpc = False,
  54. java = False,
  55. go = False,
  56. cc = False,
  57. php = False,
  58. nodejs = False,
  59. python = False,
  60. ruby = False,
  61. csharp = False,
  62. rules_override = {}):
  63. """Switches rules in the generated imports.bzl between no-op and the actual implementation.
  64. This defines which language-specific rules (or client type specific, like grpc or gapic) should
  65. be enabled during the build. All non-enabled language-specific rules will default to no-op
  66. implementations. Examples of the language-specific rules are: java_gapic_library
  67. (Java-specific), go_proto_library (Go-specific), proto_library_with_info (gapic-specific) etc.
  68. Note, proto_library rule is always enabled.
  69. For example, to use this rule and enable Java and Go rules, add the following in the external
  70. repository which imports com_google_googleapis repository and its corresponding dependencies:
  71. load("@com_google_googleapis//:repository_rules.bzl", "enabled_rules")
  72. enabled_rules(
  73. name = "com_google_googleapis_imports",
  74. grpc = True,
  75. gapic = True,
  76. go = True,
  77. java = True,
  78. )
  79. Note, for build to work you should also import the language-specific transitive dependencies.
  80. Args:
  81. name (str): name of a target, is expected to be "com_google_googleapis_imports".
  82. gapic (bool): Enable GAPIC specific rules. The GAPIC rules are also language-specific, so
  83. the actual enabled rules will be determined by the other language-specific arguments of
  84. this rule. False by default.
  85. grpc (bool): Enable gRPC specific rules. The gRPC rules are also language-specific, so
  86. the actual enabled rules will be determined by the other language-specific arguments of
  87. this rule. False by default.
  88. java (bool): Enable Java specific rules. False by default.
  89. go (bool): Enable Go specific rules. False by default.
  90. cc (bool): Enable C++ specific rules. False by default. Partially implemented (no GAPIC
  91. support).
  92. php (bool): Enable PHP specific rules. False by default.
  93. nodejs (bool): Enable Node.js specific rules. False by default.
  94. ruby (bool): Enable Ruby specific rules. False by default.
  95. python (bool): Enable Python-specific rules. False by default.
  96. csharp (bool): Enable C# specific rules. False by default.
  97. rules_override (dict): Custom rule overrides (for advanced usage).
  98. """
  99. rules = {}
  100. #
  101. # Common
  102. #
  103. rules["proto_library_with_info"] = _switch(
  104. gapic,
  105. "@rules_gapic//:gapic.bzl",
  106. )
  107. rules["moved_proto_library"] = _switch(
  108. gapic,
  109. "@rules_gapic//:gapic.bzl",
  110. )
  111. #
  112. # Java
  113. #
  114. rules["java_proto_library"] = _switch(
  115. java,
  116. "native.java_proto_library",
  117. )
  118. rules["java_grpc_library"] = _switch(
  119. java and grpc,
  120. "@io_grpc_grpc_java//:java_grpc_library.bzl",
  121. )
  122. rules["java_gapic_library"] = _switch(
  123. java and grpc and gapic,
  124. "@gapic_generator_java//rules_java_gapic:java_gapic.bzl",
  125. )
  126. rules["java_gapic_test"] = _switch(
  127. java and grpc and gapic,
  128. "@gapic_generator_java//rules_java_gapic:java_gapic.bzl",
  129. )
  130. rules["java_gapic_assembly_gradle_pkg"] = _switch(
  131. java and grpc and gapic,
  132. "@gapic_generator_java//rules_java_gapic:java_gapic_pkg.bzl",
  133. )
  134. #
  135. # Python
  136. #
  137. rules["py_proto_library"] = _switch(
  138. python,
  139. "@com_github_grpc_grpc//bazel:python_rules.bzl",
  140. )
  141. rules["py_grpc_library"] = _switch(
  142. python and grpc,
  143. "@com_github_grpc_grpc//bazel:python_rules.bzl",
  144. )
  145. rules["py_gapic_library"] = _switch(
  146. python and grpc and gapic,
  147. "@gapic_generator_python//rules_python_gapic:py_gapic.bzl",
  148. )
  149. rules["py_gapic_assembly_pkg"] = _switch(
  150. python and grpc and gapic,
  151. "@gapic_generator_python//rules_python_gapic:py_gapic_pkg.bzl",
  152. )
  153. #
  154. # Go
  155. #
  156. rules["go_proto_library"] = _switch(
  157. go,
  158. "@io_bazel_rules_go//proto:def.bzl",
  159. )
  160. rules["go_library"] = _switch(
  161. go,
  162. "@io_bazel_rules_go//go:def.bzl",
  163. )
  164. rules["go_test"] = _switch(
  165. go and grpc and gapic,
  166. "@io_bazel_rules_go//go:def.bzl",
  167. )
  168. rules["go_gapic_library"] = _switch(
  169. go and grpc and gapic,
  170. "@com_googleapis_gapic_generator_go//rules_go_gapic:go_gapic.bzl",
  171. )
  172. rules["go_gapic_assembly_pkg"] = _switch(
  173. go and grpc and gapic,
  174. "@com_googleapis_gapic_generator_go//rules_go_gapic:go_gapic_pkg.bzl",
  175. )
  176. #
  177. # C++
  178. #
  179. rules["cc_proto_library"] = _switch(
  180. cc,
  181. "native.cc_proto_library",
  182. )
  183. rules["cc_grpc_library"] = _switch(
  184. cc and grpc,
  185. "@com_github_grpc_grpc//bazel:cc_grpc_library.bzl",
  186. )
  187. rules["cc_gapic_library"] = _switch(False)
  188. #
  189. # PHP
  190. #
  191. rules["php_proto_library"] = _switch(
  192. php,
  193. "@gapic_generator_php//rules_php_gapic:php_gapic.bzl",
  194. "php_proto_library",
  195. )
  196. rules["php_grpc_library"] = _switch(
  197. php and grpc,
  198. "@gapic_generator_php//rules_php_gapic:php_gapic.bzl",
  199. "php_grpc_library",
  200. )
  201. rules["php_gapic_library"] = _switch(
  202. php and grpc and gapic,
  203. "@gapic_generator_php//rules_php_gapic:php_gapic.bzl",
  204. "php_gapic_library",
  205. )
  206. rules["php_gapic_assembly_pkg"] = _switch(
  207. php and grpc and gapic,
  208. "@gapic_generator_php//rules_php_gapic:php_gapic_pkg.bzl",
  209. "php_gapic_assembly_pkg",
  210. )
  211. #
  212. # Node.js
  213. #
  214. rules["nodejs_gapic_library"] = _switch(
  215. nodejs and grpc and gapic,
  216. "@gapic_generator_typescript//rules_typescript_gapic:typescript_gapic.bzl",
  217. "typescript_gapic_library",
  218. )
  219. rules["nodejs_gapic_assembly_pkg"] = _switch(
  220. nodejs and grpc and gapic,
  221. "@gapic_generator_typescript//rules_typescript_gapic:typescript_gapic_pkg.bzl",
  222. "typescript_gapic_assembly_pkg",
  223. )
  224. #
  225. # Ruby
  226. #
  227. rules["ruby_proto_library"] = _switch(
  228. ruby,
  229. "@gapic_generator_ruby//rules_ruby_gapic:ruby_gapic.bzl",
  230. )
  231. rules["ruby_grpc_library"] = _switch(
  232. ruby and grpc,
  233. "@gapic_generator_ruby//rules_ruby_gapic:ruby_gapic.bzl",
  234. )
  235. rules["ruby_ads_gapic_library"] = _switch(
  236. ruby and grpc and gapic,
  237. "@gapic_generator_ruby//rules_ruby_gapic:ruby_gapic.bzl",
  238. )
  239. rules["ruby_cloud_gapic_library"] = _switch(
  240. ruby and grpc and gapic,
  241. "@gapic_generator_ruby//rules_ruby_gapic:ruby_gapic.bzl",
  242. )
  243. rules["ruby_gapic_assembly_pkg"] = _switch(
  244. ruby and grpc and gapic,
  245. "@gapic_generator_ruby//rules_ruby_gapic:ruby_gapic_pkg.bzl",
  246. )
  247. #
  248. # C#
  249. #
  250. rules["csharp_proto_library"] = _switch(
  251. csharp,
  252. "@gapic_generator_csharp//rules_csharp_gapic:csharp_gapic.bzl",
  253. )
  254. rules["csharp_grpc_library"] = _switch(
  255. csharp and grpc,
  256. "@gapic_generator_csharp//rules_csharp_gapic:csharp_gapic.bzl",
  257. )
  258. rules["csharp_gapic_library"] = _switch(
  259. csharp and grpc and gapic,
  260. "@gapic_generator_csharp//rules_csharp_gapic:csharp_gapic.bzl",
  261. )
  262. rules["csharp_gapic_assembly_pkg"] = _switch(
  263. csharp and grpc and gapic,
  264. "@gapic_generator_csharp//rules_csharp_gapic:csharp_gapic_pkg.bzl",
  265. )
  266. rules.update(rules_override)
  267. switched_rules(
  268. name = name,
  269. rules = rules,
  270. )
  271. def _switch(enabled, enabled_value = "", actual_name = ""):
  272. if enabled:
  273. return [enabled_value, actual_name]
  274. else:
  275. return ["", actual_name]