Skip to content

Commit d1bd4a8

Browse files
author
Tomasz Pasternak
authored
chore: Aspect templating - 1/n (bazelbuild#6800)
1 parent d5f0def commit d1bd4a8

File tree

6 files changed

+42
-21
lines changed

6 files changed

+42
-21
lines changed

aspect/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ filegroup(
3737
"intellij_info_bundled.bzl",
3838
"intellij_info_impl_bundled.bzl",
3939
"java_classpath.bzl",
40+
"java_info.bzl",
4041
"make_variables.bzl",
4142
":BUILD.bazel",
4243
"//aspect/tools:CreateAar",

aspect/build_dependencies.bzl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ load(
55
"CPP_COMPILE_ACTION_NAME",
66
"C_COMPILE_ACTION_NAME",
77
)
8+
load(":java_info.bzl", "get_java_info")
89

910
ALWAYS_BUILD_RULES = "java_proto_library,java_lite_proto_library,java_mutable_proto_library,kt_proto_library_helper,_java_grpc_library,_java_lite_grpc_library,kt_grpc_library_helper,java_stubby_library,kt_stubby_library_helper,aar_import,java_import"
1011

@@ -273,11 +274,12 @@ def _collect_own_java_artifacts(
273274
# This is done primarily for rules like proto, whose toolchain classes
274275
# are collected via attribute traversal, but still requires jars for any
275276
# proto deps of the underlying proto_library.
276-
if JavaInfo in target:
277+
java_info = get_java_info(target)
278+
if java_info:
277279
if can_follow_dependencies:
278-
own_jar_depsets.append(target[JavaInfo].compile_jars)
280+
own_jar_depsets.append(java_info.compile_jars)
279281
else:
280-
own_jar_depsets.append(target[JavaInfo].transitive_compile_time_jars)
282+
own_jar_depsets.append(java_info.transitive_compile_time_jars)
281283

282284
if declares_android_resources(target, ctx):
283285
ide_aar = _get_ide_aar_file(target, ctx)
@@ -313,8 +315,9 @@ def _collect_own_java_artifacts(
313315

314316
# Add generated java_outputs (e.g. from annotation processing)
315317
generated_class_jars = []
316-
if JavaInfo in target:
317-
for java_output in target[JavaInfo].java_outputs:
318+
java_info = get_java_info(target)
319+
if java_info:
320+
for java_output in java_info.java_outputs:
318321
# Prefer source jars if they exist:
319322
if use_generated_srcjars and java_output.generated_source_jar:
320323
own_gensrc_files.append(java_output.generated_source_jar)

aspect/fast_build_info.bzl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ load(
1010
":intellij_info_impl.bzl",
1111
"stringify_label",
1212
)
13+
load(":java_info.bzl", "get_java_info")
1314

1415
_DEP_ATTRS = ["deps", "exports", "runtime_deps", "_java_toolchain"]
1516

@@ -66,7 +67,8 @@ def _fast_build_info_impl(target, ctx):
6667
source_version = toolchain.source_version,
6768
target_version = toolchain.target_version,
6869
)
69-
if JavaInfo in target:
70+
java_info = get_java_info(target)
71+
if java_info:
7072
write_output = True
7173
launcher = None
7274
if hasattr(ctx.rule.attr, "use_launcher") and not ctx.rule.attr.use_launcher:

aspect/intellij_info_impl.bzl

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"""Implementation of IntelliJ-specific information collecting aspect."""
22

3+
load(
4+
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
5+
"ACTION_NAMES",
6+
)
37
load(
48
":artifacts.bzl",
59
"artifact_location",
@@ -9,14 +13,11 @@ load(
913
"struct_omit_none",
1014
"to_artifact_location",
1115
)
16+
load(":java_info.bzl", "get_java_info", "java_info_in_target", "java_info_reference")
1217
load(
1318
":make_variables.bzl",
1419
"expand_make_variables",
1520
)
16-
load(
17-
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
18-
"ACTION_NAMES",
19-
)
2021

2122
IntelliJInfo = provider(
2223
doc = "Collected infromation about the targets visited by the aspect.",
@@ -254,7 +255,7 @@ def _is_language_specific_proto_library(ctx, target, semantics):
254255
"""Returns True if the target is a proto library with attached language-specific aspect."""
255256
if ctx.rule.kind != "proto_library":
256257
return False
257-
if JavaInfo in target:
258+
if java_info_in_target(target):
258259
return True
259260
if CcInfo in target:
260261
return True
@@ -594,8 +595,9 @@ def get_java_provider(target):
594595
# See https://github.com/bazelbuild/intellij/pull/1202
595596
if hasattr(target, "kt") and hasattr(target.kt, "outputs"):
596597
return target.kt
597-
if JavaInfo in target:
598-
return target[JavaInfo]
598+
java_info = get_java_info(target)
599+
if java_info:
600+
return java_info
599601
if hasattr(java_common, "JavaPluginInfo") and java_common.JavaPluginInfo in target:
600602
return target[java_common.JavaPluginInfo]
601603
return None
@@ -725,8 +727,9 @@ def collect_java_info(target, ctx, semantics, ide_info, ide_info_file, output_gr
725727
return True
726728

727729
def _android_lint_plugin_jars(target):
728-
if JavaInfo in target:
729-
return target[JavaInfo].transitive_runtime_jars.to_list()
730+
java_info = get_java_info(target)
731+
if java_info:
732+
return java_info.transitive_runtime_jars.to_list()
730733
else:
731734
return []
732735

@@ -1125,7 +1128,7 @@ def intellij_info_aspect_impl(target, ctx, semantics):
11251128
# Propagate my own exports
11261129
export_deps = []
11271130
direct_exports = []
1128-
if JavaInfo in target:
1131+
if java_info_in_target(target):
11291132
direct_exports = collect_targets_from_attrs(rule_attrs, ["exports"])
11301133
export_deps.extend(make_deps(direct_exports, COMPILE_TIME))
11311134

@@ -1292,6 +1295,6 @@ def make_intellij_info_aspect(aspect_impl, semantics):
12921295
attr_aspects = attr_aspects,
12931296
attrs = attrs,
12941297
fragments = ["cpp"],
1295-
required_aspect_providers = [[JavaInfo], [CcInfo]] + semantics.extra_required_aspect_providers,
1298+
required_aspect_providers = [java_info_reference(), [CcInfo]] + semantics.extra_required_aspect_providers,
12961299
implementation = aspect_impl,
12971300
)

aspect/java_classpath.bzl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""An aspect which extracts the runtime classpath from a java target."""
22

3+
load(":java_info.bzl", "get_java_info", "java_info_in_target")
4+
35
def _runtime_classpath_impl(target, ctx):
46
"""The top level aspect implementation function.
57
@@ -18,15 +20,16 @@ def _runtime_classpath_impl(target, ctx):
1820
})
1921

2022
def _get_runtime_jars(target):
21-
if JavaInfo not in target:
23+
java_info = get_java_info(target)
24+
if not java_info:
2225
return depset()
23-
if target[JavaInfo].compilation_info:
24-
return target[JavaInfo].compilation_info.runtime_classpath
26+
if java_info.compilation_info:
27+
return java_info.compilation_info.runtime_classpath
2528

2629
# JavaInfo constructor doesn't fill in compilation info, so just return the
2730
# full transitive set of runtime jars
2831
# https://github.com/bazelbuild/bazel/issues/10170
29-
return target[JavaInfo].transitive_runtime_jars
32+
return java_info.transitive_runtime_jars
3033

3134
def _aspect_def(impl):
3235
return aspect(implementation = impl)

aspect/java_info.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def java_info_in_target(target):
2+
return JavaInfo in target
3+
4+
def get_java_info(target):
5+
if JavaInfo in target:
6+
return target[JavaInfo]
7+
8+
def java_info_reference():
9+
return [JavaInfo]

0 commit comments

Comments
 (0)