Skip to content

Commit f7ad0cf

Browse files
authored
Support Starlark rules_android (#1333)
* Support Starlark rules_android * Revert * Formatting * Add missing _lcov_merger attr * More clean up * More clean up and fixes * More cleanup * More cleanup * More cleanup * More cleanup and fixes * More * Fixes * More fixes * More fixes * Fixes
1 parent cb8b7be commit f7ad0cf

File tree

16 files changed

+592
-175
lines changed

16 files changed

+592
-175
lines changed

examples/android/MODULE.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ bazel_dep(name = "rules_java", version = "8.9.0")
77
bazel_dep(name = "rules_kotlin", version = "1.9.5")
88
bazel_dep(name = "rules_jvm_external", version = "6.6")
99

10+
android_sdk_repository_extension = use_extension("@rules_android//rules/android_sdk_repository:rule.bzl", "android_sdk_repository_extension")
11+
use_repo(android_sdk_repository_extension, "androidsdk")
12+
13+
register_toolchains("@androidsdk//:sdk-toolchain", "@androidsdk//:all")
14+
1015
maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
1116
maven.install(
1217
name = "maven_rules_kotlin_example",

examples/android/libKtAndroid/src/test/java/examples/android/lib/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ load("@rules_kotlin//kotlin:android.bzl", "kt_android_local_test")
33
kt_android_local_test(
44
name = "SomeTest",
55
srcs = ["SomeTest.kt"],
6-
associates = ["//libKtAndroid:my_kt_kt"],
6+
associates = ["//libKtAndroid:my_kt"],
77
custom_package = "examples.android.lib",
88
jvm_flags = [
99
"-Djava.security.manager=allow",

kotlin/android.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
load(
2-
"//kotlin/internal/jvm:android.bzl",
2+
"//kotlin/internal/jvm:kt_android_library.bzl",
33
_kt_android_library = "kt_android_library",
4+
)
5+
load(
6+
"//kotlin/internal/jvm:kt_android_local_test.bzl",
47
_kt_android_local_test = "kt_android_local_test",
58
)
69

kotlin/internal/jvm/android.bzl

Lines changed: 4 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -11,123 +11,8 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
load(
15-
"@rules_android//android:rules.bzl",
16-
_android_library = "android_library",
17-
_android_local_test = "android_local_test",
18-
)
19-
load(
20-
"//kotlin/internal/jvm:jvm.bzl",
21-
_kt_jvm_library = "kt_jvm_library",
22-
)
14+
load(":kt_android_library.bzl", _kt_android_library = "kt_android_library")
15+
load(":kt_android_local_test_impl.bzl", _kt_android_local_test = "kt_android_local_test")
2316

24-
_ANDROID_SDK_JAR = "%s" % Label("//third_party:android_sdk")
25-
26-
def _kt_android_artifact(
27-
name,
28-
srcs = [],
29-
deps = [],
30-
resources = [],
31-
plugins = [],
32-
associates = [],
33-
module_name = "",
34-
kotlinc_opts = None,
35-
javac_opts = None,
36-
enable_data_binding = False,
37-
tags = [],
38-
exec_properties = None,
39-
**kwargs):
40-
"""Delegates Android related build attributes to the native rules but uses the Kotlin builder to compile Java and
41-
Kotlin srcs. Returns a sequence of labels that a wrapping macro should export.
42-
"""
43-
base_name = name + "_base"
44-
kt_name = name + "_kt"
45-
46-
# TODO(bazelbuild/rules_kotlin/issues/273): This should be retrieved from a provider.
47-
base_deps = [_ANDROID_SDK_JAR] + deps
48-
49-
_android_library(
50-
name = base_name,
51-
visibility = ["//visibility:private"],
52-
exports = base_deps,
53-
deps = deps if enable_data_binding else [],
54-
enable_data_binding = enable_data_binding,
55-
tags = tags,
56-
exec_properties = exec_properties,
57-
**kwargs
58-
)
59-
_kt_jvm_library(
60-
name = kt_name,
61-
srcs = srcs,
62-
deps = [base_name] + base_deps,
63-
resources = resources,
64-
plugins = plugins,
65-
associates = associates,
66-
module_name = module_name,
67-
testonly = kwargs.get("testonly", default = False),
68-
visibility = ["//visibility:public"],
69-
kotlinc_opts = kotlinc_opts,
70-
javac_opts = javac_opts,
71-
tags = tags,
72-
exec_properties = exec_properties,
73-
)
74-
return [base_name, kt_name]
75-
76-
def kt_android_library(name, exports = [], visibility = None, exec_properties = None, **kwargs):
77-
"""Creates an Android sandwich library.
78-
79-
`srcs`, `deps`, `plugins` are routed to `kt_jvm_library` the other android
80-
related attributes are handled by the native `android_library` rule.
81-
"""
82-
83-
_android_library(
84-
name = name,
85-
exports = exports + _kt_android_artifact(name, exec_properties = exec_properties, **kwargs),
86-
visibility = visibility,
87-
tags = kwargs.get("tags", default = None),
88-
testonly = kwargs.get("testonly", default = 0),
89-
exec_properties = exec_properties,
90-
)
91-
92-
def kt_android_local_test(
93-
name,
94-
jvm_flags = None,
95-
manifest = None,
96-
manifest_values = None,
97-
test_class = None,
98-
size = None,
99-
data = None,
100-
timeout = None,
101-
flaky = False,
102-
shard_count = None,
103-
visibility = None,
104-
testonly = True,
105-
exec_properties = None,
106-
nocompress_extensions = None,
107-
**kwargs):
108-
"""Creates a testable Android sandwich library.
109-
110-
`srcs`, `deps`, `plugins`, `associates` are routed to `kt_jvm_library` the other android
111-
related attributes are handled by the native `android_library` rule while the test attributes
112-
are picked out and handled by the `android_local_test` rule.
113-
"""
114-
115-
_android_local_test(
116-
name = name,
117-
deps = kwargs.get("deps", []) + _kt_android_artifact(name = name, testonly = testonly, exec_properties = exec_properties, **kwargs),
118-
jvm_flags = jvm_flags,
119-
test_class = test_class,
120-
visibility = visibility,
121-
size = size,
122-
data = data,
123-
timeout = timeout,
124-
flaky = flaky,
125-
shard_count = shard_count,
126-
custom_package = kwargs.get("custom_package", default = None),
127-
manifest = manifest,
128-
manifest_values = manifest_values,
129-
tags = kwargs.get("tags", default = None),
130-
testonly = testonly,
131-
exec_properties = exec_properties,
132-
nocompress_extensions = nocompress_extensions,
133-
)
17+
kt_android_library = _kt_android_library
18+
kt_android_local_test = _kt_android_local_test

kotlin/internal/jvm/compile.bzl

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def _fold_jars_action(ctx, rule_kind, toolchains, output_jar, input_jars, action
272272
toolchain = _TOOLCHAIN_TYPE,
273273
)
274274

275-
def _resourcejar_args_action(ctx):
275+
def _resourcejar_args_action(ctx, extra_resources = {}):
276276
res_cmd = []
277277
for f in ctx.files.resources:
278278
target_path = _adjust_resources_path(f.short_path, ctx.attr.resource_strip_prefix)
@@ -283,20 +283,31 @@ def _resourcejar_args_action(ctx):
283283
f_path = f.path,
284284
)
285285
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+
286297
zipper_args_file = ctx.actions.declare_file("%s_resources_zipper_args" % ctx.label.name)
287298
ctx.actions.write(zipper_args_file, "".join(res_cmd))
288299
return zipper_args_file
289300

290-
def _build_resourcejar_action(ctx):
301+
def _build_resourcejar_action(ctx, extra_resources = {}):
291302
"""sets up an action to build a resource jar for the target being compiled.
292303
Returns:
293304
The file resource jar file.
294305
"""
295306
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)
297308
ctx.actions.run_shell(
298309
mnemonic = "KotlinZipResourceJar",
299-
inputs = ctx.files.resources + [zipper_args],
310+
inputs = ctx.files.resources + extra_resources.values() + [zipper_args],
300311
tools = [ctx.executable._zipper],
301312
outputs = [resources_jar_output],
302313
command = "{zipper} c {resources_jar_output} @{path}".format(
@@ -570,27 +581,57 @@ def _run_kt_builder_action(
570581

571582
# MAIN ACTIONS #########################################################################################################
572583

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 = {}):
574623
"""This macro sets up a compile action for a Kotlin jar.
575624
576625
Args:
577626
ctx: Invoking rule ctx, used for attr, actions, and label.
578627
rule_kind: The rule kind --e.g., `kt_jvm_library`.
628+
compile_deps: The rule kind --e.g., `kt_jvm_library`.
579629
Returns:
580630
A struct containing the providers JavaInfo (`java`) and `kt` (KtJvmInfo). This struct is not intended to be
581631
used as a legacy provider -- rather the caller should transform the result.
582632
"""
583633
toolchains = _compiler_toolchains(ctx)
584634
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-
)
594635

595636
annotation_processors = _plugin_mappers.targets_to_annotation_processors(ctx.attr.plugins + ctx.attr.deps)
596637
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):
627668
annotation_processing = outputs_struct.annotation_processing
628669

629670
# 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))
632673
output_jars.extend(ctx.files.resource_jars)
633674

634675
# Merge outputs into final runtime jar.
635-
output_jar = ctx.actions.declare_file(ctx.label.name + ".jar")
676+
output_jar = outputs.jar
636677
_fold_jars_action(
637678
ctx,
638679
rule_kind = rule_kind,
@@ -644,7 +685,7 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
644685

645686
source_jar = java_common.pack_sources(
646687
ctx.actions,
647-
output_source_jar = ctx.outputs.srcjar,
688+
output_source_jar = outputs.srcjar,
648689
sources = srcs.kt + srcs.java,
649690
source_jars = srcs.src_jars + generated_src_jars,
650691
java_toolchain = toolchains.java,
@@ -928,14 +969,15 @@ def _create_annotation_processing(annotation_processors, ap_class_jar, ap_source
928969
)
929970
return None
930971

931-
def export_only_providers(ctx, actions, attr, outputs):
972+
def _export_only_providers(ctx, actions, attr, outputs):
932973
"""_export_only_providers creates a series of forwarding providers without compilation overhead.
933974
934975
Args:
935976
ctx: kt_compiler_ctx
936977
actions: invoking rule actions,
937978
attr: kt_compiler_attributes,
938979
outputs: kt_compiler_outputs
980+
939981
Returns:
940982
kt_compiler_result
941983
"""
@@ -987,3 +1029,11 @@ def export_only_providers(ctx, actions, attr, outputs):
9871029
extensions = ["kt", "java"],
9881030
),
9891031
)
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+
)

kotlin/internal/jvm/impl.bzl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ load(
2424
)
2525
load(
2626
"//kotlin/internal/jvm:compile.bzl",
27-
"export_only_providers",
28-
_kt_jvm_produce_jar_actions = "kt_jvm_produce_jar_actions",
27+
_compile = "compile",
2928
)
3029
load(
3130
"//kotlin/internal/utils:utils.bzl",
@@ -41,6 +40,7 @@ def _make_providers(ctx, providers, runfiles_targets, transitive_files = depset(
4140
files = [ctx.outputs.jar]
4241
if providers.java.outputs.jdeps:
4342
files.append(providers.java.outputs.jdeps)
43+
4444
return [
4545
providers.java,
4646
providers.kt,
@@ -224,7 +224,7 @@ def kt_jvm_library_impl(ctx):
224224
)
225225
return _make_providers(
226226
ctx,
227-
_kt_jvm_produce_jar_actions(ctx, "kt_jvm_library") if ctx.attr.srcs or ctx.attr.resources else export_only_providers(
227+
_compile.kt_jvm_produce_jar_actions(ctx, "kt_jvm_library") if ctx.attr.srcs or ctx.attr.resources else _compile.export_only_providers(
228228
ctx = ctx,
229229
actions = ctx.actions,
230230
outputs = ctx.outputs,
@@ -234,7 +234,7 @@ def kt_jvm_library_impl(ctx):
234234
)
235235

236236
def kt_jvm_binary_impl(ctx):
237-
providers = _kt_jvm_produce_jar_actions(ctx, "kt_jvm_binary")
237+
providers = _compile.kt_jvm_produce_jar_actions(ctx, "kt_jvm_binary")
238238
jvm_flags = []
239239
if hasattr(ctx.fragments.java, "default_jvm_opts"):
240240
jvm_flags = ctx.fragments.java.default_jvm_opts
@@ -269,7 +269,7 @@ _SPLIT_STRINGS = [
269269
]
270270

271271
def kt_jvm_junit_test_impl(ctx):
272-
providers = _kt_jvm_produce_jar_actions(ctx, "kt_jvm_test")
272+
providers = _compile.kt_jvm_produce_jar_actions(ctx, "kt_jvm_test")
273273
runtime_jars = depset(ctx.files._bazel_test_runner, transitive = [providers.java.transitive_runtime_jars])
274274

275275
coverage_runfiles = []

kotlin/internal/jvm/jvm.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ _common_attr = utils.add_dicts(
249249
providers = [_JavacOptions],
250250
mandatory = False,
251251
),
252+
"_use_auto_exec_groups": attr.bool(default = False),
252253
},
253254
)
254255

@@ -659,3 +660,8 @@ kt_plugin_cfg = rule(
659660
),
660661
},
661662
)
663+
664+
attrs = struct(
665+
lib_common_attr = _lib_common_attr,
666+
runnable_common_attr = _runnable_common_attr,
667+
)

kotlin/internal/jvm/jvm_deps.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ load("//kotlin/internal/utils:sets.bzl", _sets = "sets")
2121
def _java_info(target):
2222
return target[JavaInfo] if JavaInfo in target else None
2323

24-
def _jvm_deps(ctx, toolchains, associate_deps, deps, exports = [], runtime_deps = []):
24+
def _jvm_deps(ctx, toolchains, associate_deps, deps = [], deps_java_infos = [], exports = [], runtime_deps = []):
2525
"""Encapsulates jvm dependency metadata."""
26-
dep_infos = [_java_info(d) for d in deps] + [toolchains.kt.jvm_stdlibs]
26+
dep_infos = deps_java_infos + [_java_info(d) for d in deps] + [toolchains.kt.jvm_stdlibs]
2727

2828
associates = _associate_utils.get_associates(ctx, toolchains = toolchains, associates = associate_deps)
2929

0 commit comments

Comments
 (0)