@@ -3,11 +3,21 @@ load("//scala/private:common.bzl", "write_manifest_file")
33load ("//scala/private:dependency.bzl" , "legacy_unclear_dependency_info_for_protobuf_scrooge" )
44load ("//scala/private:rule_impls.bzl" , "compile_scala" )
55load ("//scala/private/toolchain_deps:toolchain_deps.bzl" , "find_deps_info_on" )
6- load ("@bazel_tools//tools/jdk:toolchain_utils.bzl" , "find_java_runtime_toolchain" , "find_java_toolchain" )
7-
8- ScalaPBAspectInfo = provider (fields = [
9- "java_info" ,
10- ])
6+ load (
7+ "@bazel_tools//tools/jdk:toolchain_utils.bzl" ,
8+ "find_java_runtime_toolchain" ,
9+ "find_java_toolchain" ,
10+ )
11+ load (
12+ "@io_bazel_rules_scala//scala_proto/private:scala_proto_aspect_provider.bzl" ,
13+ "ScalaProtoAspectInfo" ,
14+ )
15+ load (
16+ "@io_bazel_rules_scala//scala/private:phases/api.bzl" ,
17+ "extras_phases" ,
18+ "run_aspect_phases" ,
19+ )
20+ load ("@bazel_skylib//lib:dicts.bzl" , "dicts" )
1121
1222def _import_paths (proto ):
1323 source_root = proto .proto_source_root
@@ -75,7 +85,7 @@ def _generate_sources(ctx, toolchain, proto):
7585
7686 return outputs .values ()
7787
78- def _compile_sources (ctx , toolchain , proto , src_jars , deps ):
88+ def _compile_sources (ctx , toolchain , proto , src_jars , deps , stamp_label ):
7989 output = ctx .actions .declare_file (ctx .label .name + "_scalapb.jar" )
8090 manifest = ctx .actions .declare_file (ctx .label .name + "_MANIFEST.MF" )
8191 write_manifest_file (ctx .actions , manifest , None )
@@ -111,6 +121,7 @@ def _compile_sources(ctx, toolchain, proto, src_jars, deps):
111121 scalac = toolchain .scalac ,
112122 dependency_info = legacy_unclear_dependency_info_for_protobuf_scrooge (ctx ),
113123 unused_dependency_checker_ignored_targets = [],
124+ stamp_target_label = stamp_label ,
114125 )
115126
116127 return JavaInfo (
@@ -122,27 +133,60 @@ def _compile_sources(ctx, toolchain, proto, src_jars, deps):
122133 runtime_deps = compile_deps ,
123134 )
124135
125- ####
126- # This is applied to the DAG of proto_librarys reachable from a deps
127- # or a scalapb_scala_library. Each proto_library will be one scalapb
128- # invocation assuming it has some sources.
129- def _scalapb_aspect_impl (target , ctx ):
136+ def _phase_proto_provider (ctx , p ):
137+ return p .target [ProtoInfo ]
138+
139+ def _phase_deps (ctx , p ):
140+ return [d [ScalaProtoAspectInfo ].java_info for d in ctx .rule .attr .deps ]
141+
142+ def _phase_generate_and_compile (ctx , p ):
143+ proto = p .proto_info
144+ deps = p .deps
145+ stamp_label = p .stamp_label
130146 toolchain = ctx .toolchains ["@io_bazel_rules_scala//scala_proto:toolchain_type" ]
131- proto = target [ProtoInfo ]
132- deps = [d [ScalaPBAspectInfo ].java_info for d in ctx .rule .attr .deps ]
133147
134148 if proto .direct_sources and _code_should_be_generated (ctx , toolchain ):
135149 src_jars = _generate_sources (ctx , toolchain , proto )
136- java_info = _compile_sources (ctx , toolchain , proto , src_jars , deps )
137- return [ ScalaPBAspectInfo ( java_info = java_info )]
150+ java_info = _compile_sources (ctx , toolchain , proto , src_jars , deps , stamp_label )
151+ return java_info
138152 else :
139153 # this target is only an aggregation target
140- return [ScalaPBAspectInfo (java_info = java_common .merge (deps ))]
154+ return java_common .merge (deps )
155+
156+ def _phase_aspect_provider (ctx , p ):
157+ return struct (
158+ external_providers = {
159+ "ScalaProtoAspectInfo" : ScalaProtoAspectInfo (java_info = p .generate_and_compile ),
160+ },
161+ )
162+
163+ def _phase_stamp_label (ctx , p ):
164+ rule_label = str (p .target .label )
165+ toolchain = ctx .toolchains ["@io_bazel_rules_scala//scala_proto:toolchain_type" ]
141166
142- scalapb_aspect = aspect (
143- implementation = _scalapb_aspect_impl ,
144- attr_aspects = ["deps" ],
145- incompatible_use_toolchain_transition = True ,
167+ if toolchain .stamp_by_convention and rule_label .endswith ("_proto" ):
168+ return rule_label .rstrip ("_proto" ) + "_scala_proto"
169+ else :
170+ return rule_label
171+
172+ ####
173+ # This is applied to the DAG of proto_librarys reachable from a deps
174+ # or a scalapb_scala_library. Each proto_library will be one scalapb
175+ # invocation assuming it has some sources.
176+ def _scala_proto_aspect_impl (target , ctx ):
177+ return run_aspect_phases (
178+ ctx ,
179+ [
180+ ("proto_info" , _phase_proto_provider ),
181+ ("deps" , _phase_deps ),
182+ ("stamp_label" , _phase_stamp_label ),
183+ ("generate_and_compile" , _phase_generate_and_compile ),
184+ ("aspect_provider" , _phase_aspect_provider ),
185+ ],
186+ target = target ,
187+ )
188+
189+ def make_scala_proto_aspect (* extras ):
146190 attrs = {
147191 "_java_toolchain" : attr .label (
148192 default = Label ("@bazel_tools//tools/jdk:current_java_toolchain" ),
@@ -151,10 +195,21 @@ scalapb_aspect = aspect(
151195 default = Label ("@bazel_tools//tools/jdk:current_java_runtime" ),
152196 cfg = "exec" ,
153197 ),
154- },
155- toolchains = [
156- "@io_bazel_rules_scala//scala:toolchain_type" ,
157- "@io_bazel_rules_scala//scala_proto:toolchain_type" ,
158- "@io_bazel_rules_scala//scala_proto:deps_toolchain_type" ,
159- ],
160- )
198+ }
199+ return aspect (
200+ implementation = _scala_proto_aspect_impl ,
201+ attr_aspects = ["deps" ],
202+ incompatible_use_toolchain_transition = True ,
203+ attrs = dicts .add (
204+ attrs ,
205+ extras_phases (extras ),
206+ * [extra ["attrs" ] for extra in extras if "attrs" in extra ]
207+ ),
208+ toolchains = [
209+ "@io_bazel_rules_scala//scala:toolchain_type" ,
210+ "@io_bazel_rules_scala//scala_proto:toolchain_type" ,
211+ "@io_bazel_rules_scala//scala_proto:deps_toolchain_type" ,
212+ ],
213+ )
214+
215+ scala_proto_aspect = make_scala_proto_aspect ()
0 commit comments