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