1
+ load (
2
+ "@bazel_tools//tools/jdk:toolchain_utils.bzl" ,
3
+ "find_java_runtime_toolchain" ,
4
+ "find_java_toolchain" ,
5
+ )
6
+
1
7
# Copyright 2018 The Bazel Authors. All rights reserved.
2
8
#
3
9
# Licensed under the Apache License, Version 2.0 (the "License");
11
17
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
18
# See the License for the specific language governing permissions and
13
19
# limitations under the License.
14
- load (
15
- "@bazel_tools//tools/jdk:toolchain_utils.bzl" ,
16
- "find_java_runtime_toolchain" ,
17
- "find_java_toolchain" ,
18
- )
19
- load (
20
- "@rules_java//java:defs.bzl" ,
21
- "JavaInfo" ,
22
- "java_common" ,
23
- )
20
+ load ("@rules_java//java:defs.bzl" , "JavaInfo" , "java_common" )
24
21
load (
25
22
"//kotlin/internal:defs.bzl" ,
26
23
_KtCompilerPluginInfo = "KtCompilerPluginInfo" ,
27
24
_KtJvmInfo = "KtJvmInfo" ,
28
- _KtPluginConfiguration = "KtPluginConfiguration" ,
29
25
_TOOLCHAIN_TYPE = "TOOLCHAIN_TYPE" ,
30
26
)
31
27
load (
@@ -182,83 +178,9 @@ def _adjust_resources_path(path, resource_strip_prefix):
182
178
else :
183
179
return _adjust_resources_path_by_default_prefixes (path )
184
180
185
- def _format_compile_plugin_options (o ):
186
- """Format compiler option into id:value for cmd line."""
187
- return [
188
- "%s:%s" % (o .id , o .value ),
189
- ]
190
-
191
- def _new_plugins_from (targets ):
192
- """Returns a struct containing the plugin metadata for the given targets.
193
-
194
- Args:
195
- targets: A list of targets.
196
- Returns:
197
- A struct containing the plugins for the given targets in the format:
198
- {
199
- stubs_phase = {
200
- classpath = depset,
201
- options= List[KtCompilerPluginOption],
202
- ),
203
- compile = {
204
- classpath = depset,
205
- options = List[KtCompilerPluginOption],
206
- },
207
- }
208
- """
209
-
210
- all_plugins = {}
211
- plugins_without_phase = []
212
- for t in targets :
213
- if _KtCompilerPluginInfo not in t :
214
- continue
215
- plugin = t [_KtCompilerPluginInfo ]
216
- if not (plugin .stubs or plugin .compile ):
217
- plugins_without_phase .append ("%s: %s" % (t .label , plugin .id ))
218
- if plugin .id in all_plugins :
219
- # This need a more robust error messaging.
220
- fail ("has multiple plugins with the same id: %s." % plugin .id )
221
- all_plugins [plugin .id ] = plugin
222
-
223
- if plugins_without_phase :
224
- fail ("has plugin without a phase defined: %s" % cfgs_without_plugin )
225
-
226
- all_plugin_cfgs = {}
227
- cfgs_without_plugin = []
228
- for t in targets :
229
- if _KtPluginConfiguration not in t :
230
- continue
231
- cfg = t [_KtPluginConfiguration ]
232
- if cfg .id not in all_plugins :
233
- cfgs_without_plugin .append ("%s: %s" % (t .label , cfg .id ))
234
- all_plugin_cfgs [cfg .id ] = cfg
235
-
236
- if cfgs_without_plugin :
237
- fail ("has plugin configurations without corresponding plugins: %s" % cfgs_without_plugin )
238
-
239
- return struct (
240
- stubs_phase = _new_plugin_from (all_plugin_cfgs , [p for p in all_plugins .values () if p .stubs ]),
241
- compile_phase = _new_plugin_from (all_plugin_cfgs , [p for p in all_plugins .values () if p .compile ]),
242
- )
243
-
244
- def _new_plugin_from (all_cfgs , plugins_for_phase ):
245
- classpath = []
246
- data = []
247
- options = []
248
- for p in plugins_for_phase :
249
- classpath .append (p .classpath )
250
- options .extend (p .options )
251
- if p .id in all_cfgs :
252
- cfg = all_cfgs [p .id ]
253
- classpath .append (cfg .classpath )
254
- data .append (cfg .data )
255
- options .extend (cfg .options )
256
-
257
- return struct (
258
- classpath = depset (transitive = classpath ),
259
- data = depset (transitive = data ),
260
- options = options ,
261
- )
181
+ def _format_compile_plugin_options (options ):
182
+ """Format options into id:value for cmd line."""
183
+ return ["%s:%s" % (o .id , o .value ) for o in options ]
262
184
263
185
# INTERNAL ACTIONS #####################################################################################################
264
186
def _fold_jars_action (ctx , rule_kind , toolchains , output_jar , input_jars , action_type = "" ):
@@ -496,28 +418,49 @@ def _run_kt_builder_action(
496
418
uniquify = True ,
497
419
)
498
420
421
+ compiler_plugins = [
422
+ p [_KtCompilerPluginInfo ]
423
+ for p in plugins
424
+ if _KtCompilerPluginInfo in p and p [_KtCompilerPluginInfo ]
425
+ ]
426
+
427
+ stubs_compiler_plugins = [
428
+ kcp
429
+ for kcp in compiler_plugins
430
+ if kcp .stubs
431
+ ]
432
+
433
+ compiler_compiler_plugins = [
434
+ ccp
435
+ for ccp in compiler_plugins
436
+ if ccp .compile
437
+ ]
438
+
439
+ if compiler_plugins and not (stubs_compiler_plugins or compiler_compiler_plugins ):
440
+ fail ("plugins but no phase plugins: %s" % compiler_plugins )
441
+
499
442
args .add_all (
500
443
"--stubs_plugin_classpath" ,
501
- plugins . stubs_phase . classpath ,
444
+ depset ( transitive = [ p . classpath for p in stubs_compiler_plugins ]) ,
502
445
omit_if_empty = True ,
503
446
)
504
447
505
448
args .add_all (
506
449
"--stubs_plugin_options" ,
507
- plugins . stubs_phase . options ,
450
+ [ p . options for p in stubs_compiler_plugins ] ,
508
451
map_each = _format_compile_plugin_options ,
509
452
omit_if_empty = True ,
510
453
)
511
454
512
455
args .add_all (
513
456
"--compiler_plugin_classpath" ,
514
- plugins . compile_phase . classpath ,
457
+ depset ( transitive = [ p . classpath for p in compiler_compiler_plugins ]) ,
515
458
omit_if_empty = True ,
516
459
)
517
460
518
461
args .add_all (
519
462
"--compiler_plugin_options" ,
520
- plugins . compile_phase . options ,
463
+ [ p . options for p in compiler_compiler_plugins ] ,
521
464
map_each = _format_compile_plugin_options ,
522
465
omit_if_empty = True ,
523
466
)
@@ -536,13 +479,7 @@ def _run_kt_builder_action(
536
479
mnemonic = mnemonic ,
537
480
inputs = depset (
538
481
srcs .all_srcs + srcs .src_jars + generated_src_jars ,
539
- transitive = [
540
- compile_deps .compile_jars ,
541
- transitive_runtime_jars ,
542
- deps_artifacts ,
543
- plugins .stubs_phase .classpath ,
544
- plugins .compile_phase .classpath ,
545
- ],
482
+ transitive = [compile_deps .compile_jars , transitive_runtime_jars , deps_artifacts ] + [p .classpath for p in compiler_plugins ],
546
483
),
547
484
tools = [
548
485
toolchains .kt .kotlinbuilder .files_to_run ,
@@ -584,12 +521,10 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
584
521
deps = ctx .attr .deps ,
585
522
runtime_deps = ctx .attr .runtime_deps ,
586
523
)
587
-
588
524
annotation_processors = _plugin_mappers .targets_to_annotation_processors (ctx .attr .plugins + ctx .attr .deps )
589
525
ksp_annotation_processors = _plugin_mappers .targets_to_ksp_annotation_processors (ctx .attr .plugins + ctx .attr .deps )
590
526
transitive_runtime_jars = _plugin_mappers .targets_to_transitive_runtime_jars (ctx .attr .plugins + ctx .attr .deps )
591
- plugins = _new_plugins_from (ctx .attr .plugins + _exported_plugins (deps = ctx .attr .deps ))
592
-
527
+ plugins = ctx .attr .plugins + _exported_plugins (deps = ctx .attr .deps )
593
528
deps_artifacts = _deps_artifacts (toolchains , ctx .attr .deps + associates .targets )
594
529
595
530
generated_src_jars = []
0 commit comments