@@ -272,7 +272,7 @@ def _fold_jars_action(ctx, rule_kind, toolchains, output_jar, input_jars, action
272
272
toolchain = _TOOLCHAIN_TYPE ,
273
273
)
274
274
275
- def _resourcejar_args_action (ctx ):
275
+ def _resourcejar_args_action (ctx , extra_resources = {} ):
276
276
res_cmd = []
277
277
for f in ctx .files .resources :
278
278
target_path = _adjust_resources_path (f .short_path , ctx .attr .resource_strip_prefix )
@@ -283,20 +283,31 @@ def _resourcejar_args_action(ctx):
283
283
f_path = f .path ,
284
284
)
285
285
res_cmd .extend ([line ])
286
+
287
+ for key , value in extra_resources .items ():
288
+ target_path = _adjust_resources_path (value .short_path , ctx .label .package )
289
+ if target_path [0 ] == "/" :
290
+ target_path = target_path [1 :]
291
+ line = "{target_path}={res_path}\n " .format (
292
+ res_path = value .path ,
293
+ target_path = key ,
294
+ )
295
+ res_cmd .extend ([line ])
296
+
286
297
zipper_args_file = ctx .actions .declare_file ("%s_resources_zipper_args" % ctx .label .name )
287
298
ctx .actions .write (zipper_args_file , "" .join (res_cmd ))
288
299
return zipper_args_file
289
300
290
- def _build_resourcejar_action (ctx ):
301
+ def _build_resourcejar_action (ctx , extra_resources = {} ):
291
302
"""sets up an action to build a resource jar for the target being compiled.
292
303
Returns:
293
304
The file resource jar file.
294
305
"""
295
306
resources_jar_output = ctx .actions .declare_file (ctx .label .name + "-resources.jar" )
296
- zipper_args = _resourcejar_args_action (ctx )
307
+ zipper_args = _resourcejar_args_action (ctx , extra_resources )
297
308
ctx .actions .run_shell (
298
309
mnemonic = "KotlinZipResourceJar" ,
299
- inputs = ctx .files .resources + [zipper_args ],
310
+ inputs = ctx .files .resources + extra_resources . values () + [zipper_args ],
300
311
tools = [ctx .executable ._zipper ],
301
312
outputs = [resources_jar_output ],
302
313
command = "{zipper} c {resources_jar_output} @{path}" .format (
@@ -570,27 +581,57 @@ def _run_kt_builder_action(
570
581
571
582
# MAIN ACTIONS #########################################################################################################
572
583
573
- def kt_jvm_produce_jar_actions (ctx , rule_kind ):
584
+ def _kt_jvm_produce_jar_actions (ctx , rule_kind , extra_resources = {}):
585
+ """Setup The actions to compile a jar and if any resources or resource_jars were provided to merge these in with the
586
+ compilation output.
587
+
588
+ Returns:
589
+ see `kt_jvm_compile_action`.
590
+ """
591
+ deps = getattr (ctx .attr , "deps" , [])
592
+ associates = getattr (ctx .attr , "associates" , [])
593
+ _fail_if_invalid_associate_deps (associates , deps )
594
+ compile_deps = _jvm_deps_utils .jvm_deps (
595
+ ctx ,
596
+ toolchains = _compiler_toolchains (ctx ),
597
+ associate_deps = associates ,
598
+ deps = deps ,
599
+ exports = getattr (ctx .attr , "exports" , []),
600
+ runtime_deps = getattr (ctx .attr , "runtime_deps" , []),
601
+ )
602
+
603
+ outputs = struct (
604
+ jar = ctx .outputs .jar ,
605
+ srcjar = ctx .outputs .srcjar ,
606
+ )
607
+
608
+ # Setup the compile action.
609
+ return _kt_jvm_produce_output_jar_actions (
610
+ ctx ,
611
+ rule_kind = rule_kind ,
612
+ compile_deps = compile_deps ,
613
+ outputs = outputs ,
614
+ extra_resources = extra_resources ,
615
+ )
616
+
617
+ def _kt_jvm_produce_output_jar_actions (
618
+ ctx ,
619
+ rule_kind ,
620
+ compile_deps ,
621
+ outputs ,
622
+ extra_resources = {}):
574
623
"""This macro sets up a compile action for a Kotlin jar.
575
624
576
625
Args:
577
626
ctx: Invoking rule ctx, used for attr, actions, and label.
578
627
rule_kind: The rule kind --e.g., `kt_jvm_library`.
628
+ compile_deps: The rule kind --e.g., `kt_jvm_library`.
579
629
Returns:
580
630
A struct containing the providers JavaInfo (`java`) and `kt` (KtJvmInfo). This struct is not intended to be
581
631
used as a legacy provider -- rather the caller should transform the result.
582
632
"""
583
633
toolchains = _compiler_toolchains (ctx )
584
634
srcs = _partitioned_srcs (ctx .files .srcs )
585
- _fail_if_invalid_associate_deps (ctx .attr .associates , ctx .attr .deps )
586
- compile_deps = _jvm_deps_utils .jvm_deps (
587
- ctx ,
588
- toolchains = toolchains ,
589
- associate_deps = ctx .attr .associates ,
590
- deps = ctx .attr .deps ,
591
- exports = getattr (ctx .attr , "exports" , []),
592
- runtime_deps = getattr (ctx .attr , "runtime_deps" , []),
593
- )
594
635
595
636
annotation_processors = _plugin_mappers .targets_to_annotation_processors (ctx .attr .plugins + ctx .attr .deps )
596
637
ksp_annotation_processors = _plugin_mappers .targets_to_ksp_annotation_processors (ctx .attr .plugins + ctx .attr .deps )
@@ -627,12 +668,12 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
627
668
annotation_processing = outputs_struct .annotation_processing
628
669
629
670
# If this rule has any resources declared setup a zipper action to turn them into a jar.
630
- if len (ctx .files .resources ) > 0 :
631
- output_jars .append (_build_resourcejar_action (ctx ))
671
+ if len (ctx .files .resources ) + len ( extra_resources ) > 0 :
672
+ output_jars .append (_build_resourcejar_action (ctx , extra_resources ))
632
673
output_jars .extend (ctx .files .resource_jars )
633
674
634
675
# Merge outputs into final runtime jar.
635
- output_jar = ctx . actions . declare_file ( ctx . label . name + ". jar" )
676
+ output_jar = outputs . jar
636
677
_fold_jars_action (
637
678
ctx ,
638
679
rule_kind = rule_kind ,
@@ -644,7 +685,7 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
644
685
645
686
source_jar = java_common .pack_sources (
646
687
ctx .actions ,
647
- output_source_jar = ctx . outputs .srcjar ,
688
+ output_source_jar = outputs .srcjar ,
648
689
sources = srcs .kt + srcs .java ,
649
690
source_jars = srcs .src_jars + generated_src_jars ,
650
691
java_toolchain = toolchains .java ,
@@ -928,14 +969,15 @@ def _create_annotation_processing(annotation_processors, ap_class_jar, ap_source
928
969
)
929
970
return None
930
971
931
- def export_only_providers (ctx , actions , attr , outputs ):
972
+ def _export_only_providers (ctx , actions , attr , outputs ):
932
973
"""_export_only_providers creates a series of forwarding providers without compilation overhead.
933
974
934
975
Args:
935
976
ctx: kt_compiler_ctx
936
977
actions: invoking rule actions,
937
978
attr: kt_compiler_attributes,
938
979
outputs: kt_compiler_outputs
980
+
939
981
Returns:
940
982
kt_compiler_result
941
983
"""
@@ -987,3 +1029,11 @@ def export_only_providers(ctx, actions, attr, outputs):
987
1029
extensions = ["kt" , "java" ],
988
1030
),
989
1031
)
1032
+
1033
+ compile = struct (
1034
+ compiler_toolchains = _compiler_toolchains ,
1035
+ verify_associates_not_duplicated_in_deps = _fail_if_invalid_associate_deps ,
1036
+ export_only_providers = _export_only_providers ,
1037
+ kt_jvm_produce_output_jar_actions = _kt_jvm_produce_output_jar_actions ,
1038
+ kt_jvm_produce_jar_actions = _kt_jvm_produce_jar_actions ,
1039
+ )
0 commit comments