-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathclosure_proto_library.bzl
167 lines (145 loc) · 5.06 KB
/
closure_proto_library.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
# Copyright 2018 The Closure Rules Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities for building JavaScript Protocol Buffers.
"""
load("//closure/compiler:closure_js_library.bzl", "closure_js_library_impl")
load("//closure/private:defs.bzl",
"CLOSURE_WORKER_ATTR",
"CLOSURE_LIBRARY_BASE_ATTR",
"CLOSURE_LIBRARY_DEPS_ATTR",
"unfurl")
# This was taken from https://github.com/bazelbuild/rules_go/blob/67f44035d84a352cffb9465159e199066ecb814c/proto/compiler.bzl#L72
def _proto_path(proto):
path = proto.path
root = proto.root.path
ws = proto.owner.workspace_root
if path.startswith(root):
path = path[len(root):]
if path.startswith("/"):
path = path[1:]
if path.startswith(ws):
path = path[len(ws):]
if path.startswith("/"):
path = path[1:]
return path
def _proto_include_path(proto):
path = proto.path[:-len(_proto_path(proto))]
if not path:
return '.'
if path.endswith("/"):
path = path[:-1]
return path
def _proto_include_paths(protos):
return depset([_proto_include_path(proto) for proto in protos])
def _generate_closure_js_progress_message(name):
# TODO(yannic): Add a better message?
return 'Generating JavaScript Protocol Buffer %s' % name
def _generate_closure_js(target, ctx):
# Support only `import_style=closure`, and always add
# |goog.require()| for enums.
js_out_options = [
'import_style=closure',
'library=%s' % ctx.label.name,
'add_require_for_enums',
]
if getattr(ctx.rule.attr, 'testonly', False):
js_out_options.append('testonly')
js = ctx.actions.declare_file('%s.js' % ctx.label.name)
# Add include paths for all proto files,
# to avoid copying/linking the files for every target.
protos = target.proto.transitive_imports
args = ['-I%s' % p for p in _proto_include_paths(protos)]
out_options = ','.join(js_out_options)
out_path = '/'.join(js.path.split('/')[:-1])
args += ['--js_out=%s:%s' % (out_options, out_path)]
# Add paths of protos we generate files for.
args += [file.path for file in target.proto.direct_sources]
ctx.actions.run(
inputs = protos,
outputs = [js],
executable = ctx.executable._protoc,
arguments = args,
progress_message =
_generate_closure_js_progress_message(ctx.rule.attr.name),
)
return js
def _closure_proto_aspect_impl(target, ctx):
js = _generate_closure_js(target, ctx)
srcs = depset([js])
deps = unfurl(ctx.rule.attr.deps, provider="closure_js_library")
deps += [ctx.attr._closure_library, ctx.attr._closure_protobuf_jspb]
suppress = [
"analyzerChecks",
"missingOverride",
"missingProperties",
"reportUnknownTypes",
"unusedLocalVariables",
]
library = closure_js_library_impl(
ctx.actions, ctx.label, ctx.workspace_name,
srcs, deps, ctx.rule.attr.testonly, suppress,
ctx.file._closure_library_base,
ctx.file._closure_library_deps,
ctx.executable._ClosureWorker)
return struct(
exports = library.exports,
closure_js_library=library.closure_js_library,
# The usual suspects are exported as runfiles, in addition to raw source.
runfiles=ctx.runfiles(files=[js]))
_closure_proto_aspect = aspect(
attr_aspects = ['deps'],
attrs = {
# internal only
"_protoc": attr.label(
default = Label("@com_google_protobuf//:protoc"),
executable = True,
cfg = "host",
),
"_ClosureWorker": CLOSURE_WORKER_ATTR,
"_closure_library_base": CLOSURE_LIBRARY_BASE_ATTR,
"_closure_library_deps": CLOSURE_LIBRARY_DEPS_ATTR,
"_closure_library": attr.label(
default = Label("//closure/library"),
),
"_closure_protobuf_jspb": attr.label(
default = Label("//closure/protobuf:jspb"),
),
},
implementation = _closure_proto_aspect_impl,
)
_error_multiple_deps = ''.join([
"'deps' attribute must contain exactly one label ",
"(we didn't name it 'dep' for consistency). ",
"We may revisit this restriction later.",
])
def _closure_proto_library_impl(ctx):
if len(ctx.attr.deps) > 1:
# TODO(yannic): Revisit this restriction.
fail(_error_multiple_deps, 'deps');
dep = ctx.attr.deps[0]
return struct(
files=depset(),
exports=dep.exports,
closure_js_library=dep.closure_js_library,
)
closure_proto_library = rule(
attrs = {
'deps': attr.label_list(
mandatory = True,
providers = ['proto'],
aspects = [_closure_proto_aspect],
),
},
implementation = _closure_proto_library_impl,
)