Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,7 @@ filegroup(
"//rust/tests/tonic:module_srcs",
"//scala:module_srcs",
"//scala/private:module_srcs",
"//scala/private/quality:module_srcs",
"//scala/private/toolchain:module_srcs",
"//scala/private/toolchain/hub:module_srcs",
"//sql/private:module_srcs",
Expand Down
1 change: 1 addition & 0 deletions android/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ filegroup(
"//android/private/library:module_srcs",
"//android/private/packaging:module_srcs",
"//android/private/platforms:module_srcs",
"//android/private/quality:module_srcs",
"//android/private/sdk:module_srcs",
"//android/private/toolchain:module_srcs",
],
Expand Down
15 changes: 15 additions & 0 deletions android/lang.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@
"unsupported": {
"linux_arm64": "Google ships no Linux arm64 Android build-tools, platform-tools or NDK"
},
"quality": {
"bzl": "android/private/quality:android_sdk.bzl",
"file_extensions": [],
"licenses": [
"android-sdk-license"
],
"aspects": {
"lint": [
"android_sdk_aspect"
],
"typecheck": [
"android_sdk_aspect"
]
}
},
"pins": [
"pins.json"
],
Expand Down
25 changes: 25 additions & 0 deletions android/private/quality/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("//core/quality:fixtures.bzl", "real_source_target")

package(default_visibility = [
"//:__pkg__",
"//android:__subpackages__",
"//core/quality:__subpackages__",
"//tools/ci:__pkg__",
])

exports_files(["android_sdk.bzl"])

filegroup(
name = "module_srcs",
srcs = glob(["**"]),
visibility = ["//:__subpackages__"],
)

real_source_target(
name = "corpus_starlark",
starlark_srcs = [
"BUILD.bazel",
"android_sdk.bzl",
],
tags = ["corpus"],
)
24 changes: 24 additions & 0 deletions android/private/quality/android_sdk.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Exposes the android.jar of the resolved Android SDK to the JVM quality aspects."""

load("@rules_android//providers:providers.bzl", "AndroidIdeInfo")
load("//java/private/quality:jvm_info.bzl", "JvmAndroidSdkInfo")

_ANDROID_SDK_TOOLCHAIN_TYPE = "@rules_android//toolchains/android_sdk:toolchain_type"
_ANDROID_KINDS = ["android_library", "kt_android_library"]

def _android_sdk_impl(_target, ctx):
if ctx.rule.kind not in _ANDROID_KINDS:
return [JvmAndroidSdkInfo(jars = depset())]
toolchain = ctx.toolchains[_ANDROID_SDK_TOOLCHAIN_TYPE]
if toolchain == None or getattr(toolchain, "android_sdk_info", None) == None:
fail("%s: %s needs a resolved Android SDK toolchain for quality checks" % (ctx.label, ctx.rule.kind))
return [JvmAndroidSdkInfo(jars = depset([toolchain.android_sdk_info.android_jar]))]

android_sdk_aspect = aspect(
implementation = _android_sdk_impl,
doc = "Exposes the android.jar of the resolved Android SDK for android_library and kt_android_library targets. Applies only to targets that advertise AndroidIdeInfo.",
attr_aspects = ["upstream"],
provides = [JvmAndroidSdkInfo],
required_providers = [AndroidIdeInfo],
toolchains = [config_common.toolchain_type(_ANDROID_SDK_TOOLCHAIN_TYPE, mandatory = False)],
)
10 changes: 7 additions & 3 deletions android/tests/conformance/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
load("//android:defs.bzl", "android_java_library")
load("//core/quality:fixtures.bzl", "real_source_target")
load("//tools/testing/conformance:defs.bzl", "dx_conformance_suite")
load("//tools/testing/conformance:quality.bzl", "java_format_subject", "java_lint_subject")
load("//tools/testing/conformance:quality.bzl", "java_format_subject")
load(":subjects.bzl", "android_lint_subject")

package(
default_testonly = True,
Expand Down Expand Up @@ -56,13 +57,16 @@ dx_conformance_suite(
"lint_line": 4,
"lint_rule": "UnusedPrivateMethod",
"lint_source": "LintDirty.java",
"lint_subject": java_lint_subject,
"lint_subject": android_lint_subject,
},
)

real_source_target(
name = "corpus_starlark",
testonly = True,
starlark_srcs = ["BUILD.bazel"],
starlark_srcs = [
"BUILD.bazel",
"subjects.bzl",
],
tags = ["corpus"],
)
7 changes: 7 additions & 0 deletions android/tests/conformance/subjects.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Quality subjects of the Android conformance suite."""

load("//android/private/quality:android_sdk.bzl", "android_sdk_aspect")
load("//core/quality/testdata:config_subject.bzl", "config_subject_rule")
load("//java/private/quality:jvm.bzl", "real_jvm_lint_aspect", "real_pkl_lint_aspect")

android_lint_subject = config_subject_rule([android_sdk_aspect, real_jvm_lint_aspect, real_pkl_lint_aspect], [OutputGroupInfo])
16 changes: 15 additions & 1 deletion android/tests/fixtures/android_library/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# gazelle:ignore
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("//android:defs.bzl", "android_java_library", "android_kotlin_library")
load(":android_library_tests.bzl", "android_library_providers_test")
load(":android_library_tests.bzl", "android_library_providers_test", "android_sdk_classpath_tests")

package(default_visibility = ["//android/tests:__pkg__"])

Expand Down Expand Up @@ -44,3 +44,17 @@ android_library_providers_test(
kotlin = True,
library = "kotlin_screen",
)

android_sdk_classpath_tests(
name = "java_screen_javac",
size = "small",
library = "java_screen",
mnemonic = "JavacCheck",
)

android_sdk_classpath_tests(
name = "kotlin_screen_kotlinc",
size = "small",
library = "kotlin_screen",
mnemonic = "KotlincCheck",
)
61 changes: 61 additions & 0 deletions android/tests/fixtures/android_library/android_library_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("@rules_android//providers:providers.bzl", "AndroidIdeInfo", "AndroidLibraryAarInfo")
load("@rules_java//java:defs.bzl", "JavaInfo")
load("@rules_kotlin//kotlin/internal:defs.bzl", "KtJvmInfo")
load("//android/private/quality:android_sdk.bzl", "android_sdk_aspect")
load("//java/private/quality:jvm_check.bzl", "jvm_check_aspect")

def _providers_impl(ctx):
env = analysistest.begin(ctx)
Expand Down Expand Up @@ -34,3 +36,62 @@ def android_library_providers_test(name, library, kotlin, size = "small"):
target_under_test = ":" + library,
kotlin = kotlin,
)

_CheckActionsInfo = provider(
doc = "Actions of the subject's library with the JVM check aspect applied.",
fields = {"actions": "The library's actions, the check aspect's included."},
)

def _check_subject_impl(ctx):
library = ctx.attr.library[0] if type(ctx.attr.library) == "list" else ctx.attr.library
return [_CheckActionsInfo(actions = library.actions)]

# Both subjects check the same library, so one builds it in another output directory to keep their
# check outputs apart.
def _opt_impl(_settings, _attr):
return {"//command_line_option:compilation_mode": "opt"}

_opt_transition = transition(
implementation = _opt_impl,
inputs = [],
outputs = ["//command_line_option:compilation_mode"],
)

_with_sdk_subject = rule(
implementation = _check_subject_impl,
doc = "Applies android_sdk_aspect and then jvm_check_aspect to the library.",
attrs = {"library": attr.label(mandatory = True, aspects = [android_sdk_aspect, jvm_check_aspect])},
)

_without_sdk_subject = rule(
implementation = _check_subject_impl,
doc = "Applies jvm_check_aspect alone to the library.",
attrs = {"library": attr.label(mandatory = True, cfg = _opt_transition, aspects = [jvm_check_aspect])},
)

def _android_jar_inputs(env, mnemonic):
target = analysistest.target_under_test(env)
runs = [a for a in target[_CheckActionsInfo].actions if a.mnemonic == mnemonic]
asserts.equals(env, 1, len(runs), "want one %s action" % mnemonic)
return [f for run in runs for f in run.inputs.to_list() if f.basename == "android.jar"]

def _with_sdk_impl(ctx):
env = analysistest.begin(ctx)
asserts.true(env, _android_jar_inputs(env, ctx.attr.mnemonic), "%s has no android.jar input" % ctx.attr.mnemonic)
return analysistest.end(env)

def _without_sdk_impl(ctx):
env = analysistest.begin(ctx)
asserts.equals(env, [], _android_jar_inputs(env, ctx.attr.mnemonic))
return analysistest.end(env)

_with_sdk_test = analysistest.make(_with_sdk_impl, attrs = {"mnemonic": attr.string(mandatory = True)})

_without_sdk_test = analysistest.make(_without_sdk_impl, attrs = {"mnemonic": attr.string(mandatory = True)})

def android_sdk_classpath_tests(name, library, mnemonic, size = "small"):
"""Asserts the `mnemonic` check of `library` gets android.jar only after android_sdk_aspect."""
_with_sdk_subject(name = name + "_with_sdk_subject", library = ":" + library, testonly = True)
_without_sdk_subject(name = name + "_without_sdk_subject", library = ":" + library, testonly = True)
_with_sdk_test(name = name + "_with_sdk", size = size, target_under_test = name + "_with_sdk_subject", mnemonic = mnemonic)
_without_sdk_test(name = name + "_without_sdk", size = size, target_under_test = name + "_without_sdk_subject", mnemonic = mnemonic)
29 changes: 29 additions & 0 deletions cli/adopt/src/families.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ pub fn file_extensions(root: &Path) -> BTreeSet<String> {
.collect()
}

/// The marks that select quality aspect files for the workspace at `root`: its
/// [`file_extensions`] and a `license:<id>` mark for each license its `MODULE.bazel` accepts.
#[must_use]
pub fn quality_marks(root: &Path) -> BTreeSet<String> {
let mut marks = file_extensions(root);
let module = std::fs::read_to_string(root.join("MODULE.bazel")).unwrap_or_default();
marks.extend(
crate::licenses::accepted_license_ids(&module)
.iter()
.map(|id| dx_families::license_mark(id)),
);
marks
}

/// Whether a BUILD file under `root` calls `deployment` or a `*_deploy` macro
/// loaded from rules_dx `deploy:defs.bzl`.
pub(crate) fn has_deployment(root: &Path) -> bool {
Expand Down Expand Up @@ -379,6 +393,21 @@ mod tests {
}
}

#[test]
fn quality_marks_add_the_accepted_licenses_to_the_extensions() {
let dir = workspace(&["app/Main.java"], false);
assert!(!quality_marks(dir.path()).contains("license:android-sdk-license"));
write(
dir.path(),
MODULE_FILE,
"lic = use_extension(\"@rules_dx//core:extensions.bzl\", \"dx_licenses\")\nlic.accept(ids = [\"android-sdk-license\"])\n",
)
.unwrap();
let marks = quality_marks(dir.path());
assert!(marks.contains(".java"), "{marks:?}");
assert!(marks.contains("license:android-sdk-license"), "{marks:?}");
}

fn workspace(files: &[&str], with_git: bool) -> dx_test_scratch::TempDir {
let dir = scratch("families").unwrap();
write(dir.path(), MODULE_FILE, MODULE).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion cli/adopt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod local_paths_tests;

pub use avro_cpp::{AvroCpp, plan_avro_cpp};
pub use error::AdoptError;
pub use families::{detected_families, file_extensions};
pub use families::{detected_families, file_extensions, quality_marks};
pub use git_changes::{
ChangeKind, DEFAULT_BRANCH_CANDIDATES, GitChange, dedupe_changes, dx_config_git_ignored,
is_dx_state, nearest_package_pattern, parse_name_status_nul,
Expand Down
2 changes: 1 addition & 1 deletion cli/exec_quality/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ fn run_quality_request(
// An unreadable MODULE.bazel gives no deps, and the rules_dx root carries every family.
let deps: Vec<String> = dx_adopt::module_deps(env.workspace).into_iter().collect();
let extensions = (!deps.is_empty() && !dx_adopt::is_rules_dx_root(env.workspace))
.then(|| dx_adopt::file_extensions(env.workspace));
.then(|| dx_adopt::quality_marks(env.workspace));
let consumer = extensions
.as_ref()
.map(|extensions| Consumer { extensions });
Expand Down
33 changes: 31 additions & 2 deletions cli/families/languages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
"languages": [
{
"name": "android",
"quality": {
"aspects": {
"lint": [
"android_sdk_aspect"
],
"typecheck": [
"android_sdk_aspect"
]
},
"bzl": "android/private/quality:android_sdk.bzl",
"file_extensions": [],
"licenses": [
"android-sdk-license"
]
},
"setup": {
"licenses": [
{
Expand Down Expand Up @@ -2061,9 +2076,12 @@
],
"lint": [
"real_scala_lint_aspect"
],
"typecheck": [
"real_scala_typecheck_aspect"
]
},
"bzl": "java/private/quality:jvm.bzl",
"bzl": "scala/private/quality:scala.bzl",
"classes": [
"scala"
],
Expand All @@ -2082,7 +2100,18 @@
"file_extensions": [
".sc",
".scala"
]
],
"probes": {
"format": [
"real_scala_format_probe_aspect"
],
"lint": [
"real_scala_lint_probe_aspect"
],
"typecheck": [
"real_scala_typecheck_probe_aspect"
]
}
}
},
{
Expand Down
Loading
Loading