Skip to content

Commit

Permalink
chore: Aspect templating - 1/n
Browse files Browse the repository at this point in the history
  • Loading branch information
tpasternak committed Sep 26, 2024
1 parent d5f0def commit 11de6fb
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 21 deletions.
1 change: 1 addition & 0 deletions aspect/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ filegroup(
"intellij_info_bundled.bzl",
"intellij_info_impl_bundled.bzl",
"java_classpath.bzl",
"java_info.bzl",
"make_variables.bzl",
":BUILD.bazel",
"//aspect/tools:CreateAar",
Expand Down
13 changes: 8 additions & 5 deletions aspect/build_dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ load(
"CPP_COMPILE_ACTION_NAME",
"C_COMPILE_ACTION_NAME",
)
load(":java_info.bzl", "get_java_info")

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"

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

if declares_android_resources(target, ctx):
ide_aar = _get_ide_aar_file(target, ctx)
Expand Down Expand Up @@ -313,8 +315,9 @@ def _collect_own_java_artifacts(

# Add generated java_outputs (e.g. from annotation processing)
generated_class_jars = []
if JavaInfo in target:
for java_output in target[JavaInfo].java_outputs:
java_info = get_java_info(target)
if java_info:
for java_output in java_info.java_outputs:
# Prefer source jars if they exist:
if use_generated_srcjars and java_output.generated_source_jar:
own_gensrc_files.append(java_output.generated_source_jar)
Expand Down
4 changes: 3 additions & 1 deletion aspect/fast_build_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ load(
":intellij_info_impl.bzl",
"stringify_label",
)
load(":java_info.bzl", "get_java_info")

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

Expand Down Expand Up @@ -66,7 +67,8 @@ def _fast_build_info_impl(target, ctx):
source_version = toolchain.source_version,
target_version = toolchain.target_version,
)
if JavaInfo in target:
java_info = get_java_info(target)
if java_info:
write_output = True
launcher = None
if hasattr(ctx.rule.attr, "use_launcher") and not ctx.rule.attr.use_launcher:
Expand Down
25 changes: 14 additions & 11 deletions aspect/intellij_info_impl.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""Implementation of IntelliJ-specific information collecting aspect."""

load(
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
"ACTION_NAMES",
)
load(
":artifacts.bzl",
"artifact_location",
Expand All @@ -9,14 +13,11 @@ load(
"struct_omit_none",
"to_artifact_location",
)
load(":java_info.bzl", "get_java_info", "java_info_in_target", "java_info_reference")
load(
":make_variables.bzl",
"expand_make_variables",
)
load(
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
"ACTION_NAMES",
)

IntelliJInfo = provider(
doc = "Collected infromation about the targets visited by the aspect.",
Expand Down Expand Up @@ -254,7 +255,7 @@ def _is_language_specific_proto_library(ctx, target, semantics):
"""Returns True if the target is a proto library with attached language-specific aspect."""
if ctx.rule.kind != "proto_library":
return False
if JavaInfo in target:
if java_info_in_target(target):
return True
if CcInfo in target:
return True
Expand Down Expand Up @@ -594,8 +595,9 @@ def get_java_provider(target):
# See https://github.com/bazelbuild/intellij/pull/1202
if hasattr(target, "kt") and hasattr(target.kt, "outputs"):
return target.kt
if JavaInfo in target:
return target[JavaInfo]
java_info = get_java_info(target)
if java_info:
return java_info
if hasattr(java_common, "JavaPluginInfo") and java_common.JavaPluginInfo in target:
return target[java_common.JavaPluginInfo]
return None
Expand Down Expand Up @@ -725,8 +727,9 @@ def collect_java_info(target, ctx, semantics, ide_info, ide_info_file, output_gr
return True

def _android_lint_plugin_jars(target):
if JavaInfo in target:
return target[JavaInfo].transitive_runtime_jars.to_list()
java_info = get_java_info(target)
if java_info:
return java_info.transitive_runtime_jars.to_list()
else:
return []

Expand Down Expand Up @@ -1125,7 +1128,7 @@ def intellij_info_aspect_impl(target, ctx, semantics):
# Propagate my own exports
export_deps = []
direct_exports = []
if JavaInfo in target:
if java_info_in_target(target):
direct_exports = collect_targets_from_attrs(rule_attrs, ["exports"])
export_deps.extend(make_deps(direct_exports, COMPILE_TIME))

Expand Down Expand Up @@ -1292,6 +1295,6 @@ def make_intellij_info_aspect(aspect_impl, semantics):
attr_aspects = attr_aspects,
attrs = attrs,
fragments = ["cpp"],
required_aspect_providers = [[JavaInfo], [CcInfo]] + semantics.extra_required_aspect_providers,
required_aspect_providers = [java_info_reference(), [CcInfo]] + semantics.extra_required_aspect_providers,
implementation = aspect_impl,
)
11 changes: 7 additions & 4 deletions aspect/java_classpath.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""An aspect which extracts the runtime classpath from a java target."""

load(":java_info.bzl", "get_java_info", "java_info_in_target")

def _runtime_classpath_impl(target, ctx):
"""The top level aspect implementation function.
Expand All @@ -18,15 +20,16 @@ def _runtime_classpath_impl(target, ctx):
})

def _get_runtime_jars(target):
if JavaInfo not in target:
java_info = get_java_info(target)
if not java_info:
return depset()
if target[JavaInfo].compilation_info:
return target[JavaInfo].compilation_info.runtime_classpath
if java_info.compilation_info:
return java_info.compilation_info.runtime_classpath

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

def _aspect_def(impl):
return aspect(implementation = impl)
Expand Down
9 changes: 9 additions & 0 deletions aspect/java_info.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def java_info_in_target(target):
return JavaInfo in target

def get_java_info(target):
if JavaInfo in target:
return target[JavaInfo]

def java_info_reference():
return [JavaInfo]

0 comments on commit 11de6fb

Please sign in to comment.