Skip to content

Commit

Permalink
collect custom lint jars from external libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadkahelghi-grabtaxi committed Mar 28, 2024
1 parent 04571be commit 13000eb
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions rules/android/android_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def android_binary(
manifest = attrs.get("manifest"),
baseline = lint_baseline,
lint_config = lint_options.get("config", None),
deps = kotlin_library_deps,
)

# Build deps
Expand Down
1 change: 1 addition & 0 deletions rules/android/android_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def android_library(
manifest = attrs.get("manifest"),
baseline = lint_baseline,
lint_config = lint_options.get("config", None),
deps = android_library_deps,
)
android_library_deps = android_library_deps + [lint_sources_target]
lint(
Expand Down
43 changes: 43 additions & 0 deletions rules/android/lint/collect_aar_aspect.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
load("@grab_bazel_common//rules/android/lint:providers.bzl", "AarInfo", "AarNodeInfo")

def _collect_aar_aspect(_, ctx):
deps = getattr(ctx.rule.attr, "deps", [])
exports = getattr(ctx.rule.attr, "exports", [])
associates = getattr(ctx.rule.attr, "associates", [])
transitive_aar_depsets = []
for dep in deps + exports + associates:
if AarNodeInfo in dep:
transitive_aar_depsets.append(dep[AarNodeInfo].aars)

current_info = AarNodeInfo(
aar = None,
aar_dir = None,
)

if hasattr(ctx.rule.attr, "aar"):
aar = ctx.rule.attr.aar.files.to_list()[0]
aar_extract = ctx.actions.declare_directory("lint/" + ctx.label.name + "_extracted_aar")

ctx.actions.run_shell(
inputs = [aar],
outputs = [aar_extract],
mnemonic = "ExtractAar",
progress_message = "Extracting %s's " % (ctx.label.name),
command = ("unzip -q -o %s -d %s/ " % (aar.path, aar_extract.path)),
)
current_info = AarNodeInfo(
aar = aar,
aar_dir = aar_extract,
)

return [
AarInfo(
self = current_info,
transitive = depset(transitive_aar_depsets),
),
]

collect_aar_aspect = aspect(
implementation = _collect_aar_aspect,
attr_aspects = ["aar", "deps", "exports", "associates"],
)
25 changes: 25 additions & 0 deletions rules/android/lint/lint_aspect.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
load("@grab_bazel_common//rules/android:utils.bzl", "utils")
load(
"@grab_bazel_common//rules/android/lint:providers.bzl",
"AarInfo",
"AarNodeInfo",
"AndroidLintInfo",
"AndroidLintNodeInfo",
"AndroidLintSourcesInfo",
Expand Down Expand Up @@ -50,6 +52,7 @@ def _collect_sources(target, ctx, library):
struct(
srcs = dep[AndroidLintSourcesInfo].srcs,
resources = dep[AndroidLintSourcesInfo].resources,
aar_deps = dep[AndroidLintSourcesInfo].aar_deps,
manifest = dep[AndroidLintSourcesInfo].manifest,
merged_manifest = merged_manifest,
baseline = dep[AndroidLintSourcesInfo].baseline,
Expand Down Expand Up @@ -111,6 +114,7 @@ def _lint_common_args(
compile_sdk_version,
srcs,
resources,
aar_dirs,
classpath,
manifest,
merged_manifest,
Expand Down Expand Up @@ -138,6 +142,11 @@ def _lint_common_args(
join_with = ",",
map_each = utils.to_path,
)
args.add_joined(
"--aar_dirs",
aar_dirs,
join_with = ",",
)
args.add_joined(
"--resource-files",
resources,
Expand Down Expand Up @@ -184,6 +193,7 @@ def _lint_analyze_action(
compile_sdk_version,
srcs,
resources,
aar_dirs,
classpath,
manifest,
merged_manifest,
Expand All @@ -206,6 +216,7 @@ def _lint_analyze_action(
compile_sdk_version = compile_sdk_version,
srcs = srcs,
resources = resources,
aar_dirs = aar_dirs,
classpath = classpath,
manifest = manifest,
merged_manifest = merged_manifest,
Expand All @@ -218,6 +229,7 @@ def _lint_analyze_action(
verbose = verbose,
)

# print("lint aspect analyze args=", args)
mnemonic = "AndroidLintAnalyze"
ctx.actions.run(
mnemonic = mnemonic,
Expand All @@ -244,6 +256,7 @@ def _lint_report_action(
compile_sdk_version,
srcs,
resources,
aar_dirs,
classpath,
manifest,
merged_manifest,
Expand All @@ -269,6 +282,7 @@ def _lint_report_action(
compile_sdk_version = compile_sdk_version,
srcs = srcs,
resources = resources,
aar_dirs = aar_dirs,
classpath = classpath,
manifest = manifest,
merged_manifest = merged_manifest,
Expand Down Expand Up @@ -305,6 +319,14 @@ def _lint_report_action(
)
return

def _extract_aar_dirs(aar_deps):
aars = [
[dep[AarInfo].self] + dep[AarInfo].transitive.to_list()
for dep in aar_deps
if AarInfo in dep and (dep[AarInfo].self.aar != None or len(dep[AarInfo].transitive.to_list()) > 0)
]
return [(aarInfoNode.aar.path + ":" + aarInfoNode.aar_dir.path) for aarInfoNodes in aars for aarInfoNode in aarInfoNodes]

def _lint_aspect_impl(target, ctx):
if target.label.workspace_root.startswith("external"):
# Run lint only on internal targets
Expand Down Expand Up @@ -334,6 +356,7 @@ def _lint_aspect_impl(target, ctx):
project_xml_file = ctx.actions.declare_file("lint/" + target.label.name + "_project.xml")

sources = _collect_sources(target, ctx, library)
aar_dirs = _extract_aar_dirs(sources.aar_deps)
compile_sdk_version = _compile_sdk_version(ctx.attr._android_sdk)
dep_lint_node_infos = _dep_lint_node_infos(target, transitive_lint_node_infos)
partial_results = [info.partial_results_dir for info in dep_lint_node_infos]
Expand Down Expand Up @@ -364,6 +387,7 @@ def _lint_aspect_impl(target, ctx):
compile_sdk_version = compile_sdk_version,
srcs = sources.srcs,
resources = sources.resources,
aar_dirs = aar_dirs,
classpath = sources.classpath,
manifest = sources.manifest,
merged_manifest = sources.merged_manifest,
Expand Down Expand Up @@ -397,6 +421,7 @@ def _lint_aspect_impl(target, ctx):
compile_sdk_version = compile_sdk_version,
srcs = sources.srcs,
resources = sources.resources,
aar_dirs = aar_dirs,
classpath = sources.classpath,
manifest = sources.manifest,
merged_manifest = sources.merged_manifest,
Expand Down
8 changes: 8 additions & 0 deletions rules/android/lint/lint_sources.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@grab_bazel_common//rules/android/lint:providers.bzl", "AndroidLintSourcesInfo")
load("@grab_bazel_common//rules/android/lint:collect_aar_aspect.bzl", "collect_aar_aspect")

def _target_outputs(targets):
results = []
Expand All @@ -20,6 +21,7 @@ def _lint_sources_impl(ctx):
name = ctx.attr.name,
srcs = _target_outputs(ctx.attr.srcs),
resources = _target_outputs(ctx.attr.resources),
aar_deps = ctx.attr.deps,
manifest = _target_outputs([ctx.attr.manifest]),
baseline = _target_outputs([ctx.attr.baseline]) if ctx.attr.baseline != None else None,
lint_config = _target_outputs([ctx.attr.lint_config]) if ctx.attr.lint_config != None else _target_outputs([ctx.attr._default_lint_config]),
Expand Down Expand Up @@ -52,6 +54,12 @@ lint_sources = rule(
allow_single_file = True,
default = Label("//third_party:empty.jar"),
),
"deps": attr.label_list(
doc = """deps.""",
allow_empty = True,
default = [],
aspects = [collect_aar_aspect],
),
# TODO(arun) add assets
},
provides = [
Expand Down
17 changes: 17 additions & 0 deletions rules/android/lint/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,27 @@ AndroidLintSourcesInfo = provider(
name = "Name of target",
srcs = "Java/Kotlin sources",
resources = "Android resources",
aar_deps = "direct and transiive aars AarInfo",
manifest = "Android manifest file",
baseline = "Lint baseline XML",
lint_config = "Lint config XML",
),
)

AarNodeInfo = provider(
"A provider to collect aar info of the current target",
fields = {
"aar": "aar path",
"aar_dir": "aar extracrted path",
},
)

AarInfo = provider(
"A provider to collect all aars from transitive dependencies",
fields = {
"self": "AarNodeInfo",
"transitive": "depset(AarNodeInfo)",
},
)

LINT_ENABLED = "lint_enabled"
6 changes: 6 additions & 0 deletions tools/lint/src/main/java/com/grab/lint/LintBaseCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ abstract class LintBaseCommand : CliktCommand() {
"--path-variables"
).default("${PWD}=${System.getenv(PWD)}")


protected val aarDeps by option(
"--aar_dirs"
).split(",").default(emptyList())

override fun run() {
preRun()
prepareJdk()
Expand All @@ -121,6 +126,7 @@ abstract class LintBaseCommand : CliktCommand() {
partialResults = partialResults,
srcs = srcs,
resources = resources,
aarDeps = aarDeps,
classpath = classpath,
manifest = manifest,
mergedManifest = mergedManifest,
Expand Down
5 changes: 5 additions & 0 deletions tools/lint/src/main/java/com/grab/lint/ProjectXmlCreator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ProjectXmlCreator(
partialResults: File,
srcs: List<String>,
resources: List<String>,
aarDeps: List<String>,
classpath: List<String>,
manifest: File?,
mergedManifest: File?,
Expand All @@ -44,6 +45,10 @@ class ProjectXmlCreator(
resources.forEach { resource ->
appendLine(" <resource file=\"$resource\" />")
}
aarDeps.forEach { aar ->
val aarInfo = aar.split(":")
appendLine(" <aar file=\"${aarInfo.first()}\" extracted=\"${aarInfo[1]}\" />")
}
manifest?.let { manifest ->
appendLine(" <manifest file=\"$manifest\" />")
}
Expand Down

0 comments on commit 13000eb

Please sign in to comment.