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