diff options
author | Liz Kammer <eakammer@google.com> | 2023-10-05 17:10:23 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-10-05 17:10:23 +0000 |
commit | 97c23bb2824f0746ad902a818aa1324215098a03 (patch) | |
tree | a373592da914f2a8fb6419540b1b6f43d0420624 | |
parent | 5a241f6fa25a9c09cbe0a18f5fa883f9d647e25a (diff) | |
parent | ae99674846bd8ee005a3a19fae1cac97028a28d5 (diff) | |
download | bazelbuild-kotlin-rules-97c23bb2824f0746ad902a818aa1324215098a03.tar.gz |
Merge remote-tracking branch 'aosp/upstream-main' am: 4a0a0cc613 am: 4739f3adbe am: 0a8252429c am: ce3f0f96ce am: ae99674846
Original change: https://android-review.googlesource.com/c/platform/external/bazelbuild-kotlin-rules/+/2771304
Change-Id: I1d69a59b2f6b12d9050ac2f28db87167b3b56eb3
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
29 files changed, 289 insertions, 285 deletions
@@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +load("//bazel:stubs.bzl", "integration_test_filegroup") load("@bazel_skylib//:bzl_library.bzl", "bzl_library") licenses(["notice"]) @@ -11,8 +11,8 @@ third_party { type: GIT value: "https://team.googlesource.com/kotlin-rules/rules" } - version: "4effec817231742ccf4ee377790bf42426ef612e" - last_upgrade_date { year: 2023 month: 7 day: 25} + version: "7a8fbedcfd9b4c1121d144329d9bf3e9a3184026" + last_upgrade_date { year: 2023 month: 10 day: 3} license_type: NOTICE } @@ -70,7 +70,7 @@ load("@//toolchains/kotlin_jvm:kt_jvm_toolchains.bzl", "KT_VERSION") http_archive( name = "kotlinc", build_file = "@//bazel:kotlinc.BUILD", - sha256 = "1fc50d805f9809e92de43e91f089cc8618567c1a350faebdabf8a40c5048bee8", + sha256 = "7d74863deecf8e0f28ea54c3735feab003d0eac67e8d3a791254b16889c20342", strip_prefix = "kotlinc", urls = [ "https://github.com/JetBrains/kotlin/releases/download/v{0}/kotlin-compiler-{0}.zip".format(KT_VERSION[1:].replace("_", ".")), diff --git a/bazel/BUILD b/bazel/BUILD index 303a377..6cc6ba2 100644 --- a/bazel/BUILD +++ b/bazel/BUILD @@ -14,7 +14,7 @@ licenses(["notice"]) # Apache 2.0 -package(default_visibility = ["//:internal"]) +package(default_visibility = ["//visibility:public"]) java_library( name = "auto_service", diff --git a/bazel/stubs.bzl b/bazel/stubs.bzl index e49ad4e..4a6915d 100644 --- a/bazel/stubs.bzl +++ b/bazel/stubs.bzl @@ -14,8 +14,8 @@ """Stubs""" -load("@bazel_skylib//lib:sets.bzl", "sets") load("//:visibility.bzl", "RULES_KOTLIN") +load("@bazel_skylib//lib:sets.bzl", "sets") def _empty_fn(*_args, **_kwargs): pass @@ -57,3 +57,10 @@ lint_actions = struct( def check_compiler_opt_allowlist(_label): pass + +def jspecify_flags(ctx): + # Trust JSpecify nullness annotations + # (see https://kotlinlang.org/docs/whatsnew1520.html#support-for-jspecify-nullness-annotations) + return ["-Xjspecify-annotations=strict"] + +integration_test_filegroup = _empty_fn diff --git a/copy.bara.sky b/copy.bara.sky index aff4a12..13546c4 100644 --- a/copy.bara.sky +++ b/copy.bara.sky @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. --1392002248
\ No newline at end of file +1486640829
\ No newline at end of file diff --git a/kokoro/presubmit.cfg b/kokoro/presubmit.cfg index ca64f8c..5e14719 100644 --- a/kokoro/presubmit.cfg +++ b/kokoro/presubmit.cfg @@ -15,18 +15,4 @@ # -*- protobuffer -*- # proto-message: BuildConfig -# The version of bazel to use to test the rules_kotlin. -# Update this as newer versions of bazel are released. -build_params { - key: "bazel_version" - value: "6.1.1" -} - -env_vars { - key: "bazel_version" - value: "$[bazel_version]" -} - -gfile_resources: "/x20/teams/bazel/releases/bazel-$[bazel_version]-linux-x86_64" - build_file: "presubmit/kokoro/presubmit.sh" diff --git a/kokoro/presubmit.sh b/kokoro/presubmit.sh index c6302a3..0ac125f 100755 --- a/kokoro/presubmit.sh +++ b/kokoro/presubmit.sh @@ -18,8 +18,40 @@ kokoro_scm_name="presubmit" workspace_root="${KOKORO_ARTIFACTS_DIR}/git/${kokoro_scm_name}" -bazel="${KOKORO_GFILE_DIR}/bazel-${bazel_version}-linux-x86_64" -chmod +x "$bazel" + +function DownloadBazelisk() { + # Downloads bazelisk to a temp directory. + + local version="${1:-1.18.0}" + local platform="${2:-linux}" + local arch="${3:-amd64}" + + local dest=$(mktemp -d) + ( + set -euxo pipefail + + echo "== Downloading bazelisk =====================================" + + download_url="https://github.com/bazelbuild/bazelisk/releases/download/v${version}/bazelisk-${platform}-${arch}" + mkdir -p "${dest}" + wget -nv ${download_url} -O "${dest}/bazelisk" + chmod +x "${dest}/bazelisk" + + echo "=============================================================" + ) &> /dev/stderr + + echo "${dest}" +} + +bazelisk_dir=$(DownloadBazelisk "1.18.0" linux amd64) +export PATH="${bazelisk_dir}:${PATH}" + +function Cleanup() { + # Clean up all temporary directories: bazelisk install, sandbox, and + # android_tools. + rm -rf "$bazelisk_dir" +} +trap Cleanup EXIT # Default JDK on GCP_UBUNTU is JDK8 sudo update-java-alternatives --set java-1.11.0-openjdk-amd64 @@ -40,7 +72,7 @@ cd "${workspace_root}" # Run test coverage for all the test targets except excluded by # --instrumentation_filter - code coverage doesn't work for them and they # would only be tested -"$bazel" coverage \ +bazelisk coverage \ --sandbox_tmpfs_path="$hsperfdata_dir" \ --verbose_failures \ --experimental_google_legacy_api \ @@ -48,7 +80,7 @@ cd "${workspace_root}" //tests/... # For a specific test //tools:source_jar_zipper_freshness_test run test only -"$bazel" test \ +bazelisk test \ --sandbox_tmpfs_path="$hsperfdata_dir" \ --verbose_failures \ --experimental_google_legacy_api \ diff --git a/kotlin/common.bzl b/kotlin/common.bzl index 0e3b77d..2279596 100644 --- a/kotlin/common.bzl +++ b/kotlin/common.bzl @@ -14,14 +14,16 @@ """Common Kotlin definitions.""" +load("//:visibility.bzl", "RULES_DEFS_THAT_COMPILE_KOTLIN") + # go/keep-sorted start load("//kotlin/jvm/internal_do_not_use/util:file_factory.bzl", "FileFactory") load("//kotlin/jvm/internal_do_not_use/util:srcjars.bzl", "kt_srcjars") load("//toolchains/kotlin_jvm:androidlint_toolchains.bzl", "androidlint_toolchains") load("//toolchains/kotlin_jvm:kt_jvm_toolchains.bzl", "kt_jvm_toolchains") -load("//:visibility.bzl", "RULES_DEFS_THAT_COMPILE_KOTLIN") load("@bazel_skylib//lib:sets.bzl", "sets") load("//bazel:stubs.bzl", "lint_actions") +load("//bazel:stubs.bzl", "jspecify_flags") load("//bazel:stubs.bzl", "BASE_JVMOPTS") # go/keep-sorted end @@ -84,7 +86,7 @@ def _get_common_and_user_kotlinc_args(ctx, toolchain, extra_kotlinc_args): # Set module name so module-level metadata is preserved when merging Jars (b/139403883) "-module-name", _derive_module_name(ctx), - ] + extra_kotlinc_args + ] + jspecify_flags(ctx) + extra_kotlinc_args def _kt_plugins_map( android_lint_rulesets = [], @@ -196,7 +198,9 @@ def _run_kotlinc( mnemonic = mnemonic, progress_message = message_prefix + str(_get_original_kt_target_label(ctx)), execution_requirements = { - "local": "1", # Ensure comparable results across runs (cold builds, same machine) + # Ensure comparable results across runs (cold builds, same machine) + "no-cache": "1", + "no-remote": "1", } if toolchain.is_profiling_enabled(ctx.label) else { "worker-key-mnemonic": "Kt2JavaCompile", }, @@ -499,6 +503,8 @@ def _kt_jvm_library( srcs = [], common_srcs = [], coverage_srcs = [], + java_android_lint_config = None, + force_android_lint = False, # TODO Remove this param manifest = None, # set for Android libs, otherwise None. merged_manifest = None, # set for Android libs, otherwise None. resource_files = [], # set for Android libs, otherwise empty. @@ -532,30 +538,33 @@ def _kt_jvm_library( file_factory = FileFactory(ctx, output) static_deps = list(deps) # Defensive copy - kt_codegen_processing_env = dict( - processors_for_kt_codegen_processing = depset(), - processors_for_legacy_kapt = [], - codegen_output_java_infos = [], - java_plugin_datas_legacy = [], - legacy_java_plugin_classpaths = depset(), - ) + kt_codegen_processing_env = dict() + codegen_plugin_output = None - kt_codegen_processors = kt_codegen_processing_env["processors_for_kt_codegen_processing"] - generative_deps = kt_codegen_processing_env["codegen_output_java_infos"] + kt_codegen_processors = kt_codegen_processing_env.get("processors_for_kt_codegen_processing", depset()).to_list() + codegen_tags = kt_codegen_processing_env.get("codegen_tags", []) + generative_deps = kt_codegen_processing_env.get("codegen_output_java_infos", depset()).to_list() java_syncer = kt_srcjars.DirSrcjarSyncer(ctx, kt_toolchain, file_factory) kt_srcs, java_srcs = _split_srcs_by_language(srcs, common_srcs, java_syncer) + is_android_library_without_kt_srcs = rule_family == _RULE_FAMILY.ANDROID_LIBRARY and not kt_srcs and not common_srcs + is_android_library_without_kt_srcs_without_generative_deps = is_android_library_without_kt_srcs and not generative_deps + # TODO: Remove this special case if kt_srcs and ("flogger" in [p.plugin_id for p in plugins.kt_compiler_plugin_infos]): static_deps.append(kt_toolchain.flogger_runtime) - if kt_srcs or common_srcs or rule_family != _RULE_FAMILY.ANDROID_LIBRARY: + if not is_android_library_without_kt_srcs_without_generative_deps: static_deps.extend(kt_toolchain.kotlin_libs) # Skip srcs package check for android_library targets with no kotlin sources: b/239725424 - if rule_family != _RULE_FAMILY.ANDROID_LIBRARY or kt_srcs: - _check_srcs_package(ctx.label.package, srcs, "srcs") + if not is_android_library_without_kt_srcs: + if "check_srcs_package_against_kt_srcs_only" in codegen_tags: + _check_srcs_package(ctx.label.package, kt_srcs, "srcs") + else: + _check_srcs_package(ctx.label.package, srcs, "srcs") + _check_srcs_package(ctx.label.package, common_srcs, "common_srcs") _check_srcs_package(ctx.label.package, coverage_srcs, "coverage_srcs") @@ -567,13 +576,24 @@ def _kt_jvm_library( # Collect all plugin data, including processors to run and all plugin classpaths, # whether they have processors or not (b/120995492). # This may include go/errorprone plugin classpaths that kapt will ignore. - java_plugin_datas_legacy = kt_codegen_processing_env["java_plugin_datas_legacy"] - legacy_kapt_processors = kt_codegen_processing_env["processors_for_legacy_kapt"] - legacy_java_plugin_classpaths = kt_codegen_processing_env["legacy_java_plugin_classpaths"] + java_plugin_datas = kt_codegen_processing_env.get("java_plugin_data_set", depset()).to_list() + processors_for_java_srcs = kt_codegen_processing_env.get("processors_for_java_srcs", depset()).to_list() + java_plugin_classpaths_for_java_srcs = depset(transitive = [p.processor_jars for p in java_plugin_datas]) + + out_jars = [ + jar + for java_info in generative_deps + for jar in java_info.runtime_output_jars + ] + + out_srcjars = [ + ] if codegen_plugin_output else [] - out_jars = [] - out_srcjars = [] - out_compilejars = [] + out_compilejars = [ + jar + for java_info in generative_deps + for jar in java_info.compile_jars.to_list() + ] kt_hdrs = _derive_headers( ctx, @@ -629,10 +649,9 @@ def _kt_jvm_library( javac_java_info = None java_native_headers_jar = None - is_android_library_without_kt_srcs = rule_family == _RULE_FAMILY.ANDROID_LIBRARY and not kt_srcs if java_srcs or java_syncer.srcjars or classpath_resources: - javac_deps = extended_deps # Defensive copy + javac_deps = list(extended_deps) # Defensive copy if kotlinc_result: javac_deps.append(kotlinc_result.java_info) if ctx.coverage_instrumented(): @@ -641,12 +660,12 @@ def _kt_jvm_library( # (which doesn't contain instrumentation). See b/117897097. javac_deps.append(kt_toolchain.coverage_runtime) - javac_out = output if is_android_library_without_kt_srcs else file_factory.declare_file("-java.jar") + javac_out = output if is_android_library_without_kt_srcs_without_generative_deps else file_factory.declare_file("-libjvm-java.jar") annotation_plugins = list(plugins.java_plugin_infos) # Enable annotation processing for java-only sources to enable data binding - enable_annotation_processing = not kt_srcs + enable_annotation_processing = True if processors_for_java_srcs else False javac_java_info = java_common.compile( ctx, @@ -655,7 +674,7 @@ def _kt_jvm_library( resources = classpath_resources_non_dirs, # For targets that are not android_library with java-only srcs, exports will be passed # to the final constructed JavaInfo. - exports = exports if is_android_library_without_kt_srcs else [], + exports = exports if is_android_library_without_kt_srcs_without_generative_deps else [], output = javac_out, exported_plugins = exported_plugins, deps = javac_deps, @@ -674,7 +693,7 @@ def _kt_jvm_library( # Directly return the JavaInfo from java.compile() for java-only android_library targets # to avoid creating a new JavaInfo. See b/239847857 for additional context. - if is_android_library_without_kt_srcs: + if is_android_library_without_kt_srcs_without_generative_deps: return struct( java_info = javac_java_info, validations = [], @@ -687,17 +706,9 @@ def _kt_jvm_library( java_gensrcjar = None java_genjar = None - if kt_codegen_processors: - java_gen_srcjars = kt_codegen_processing_env["java_gen_srcjar"] - kt_gen_srcjars = kt_codegen_processing_env["kt_gen_srcjar"] - java_gensrcjar = file_factory.declare_file("-java_info_generated_source_jar.srcjar") - _singlejar( - ctx, - inputs = java_gen_srcjars + kt_gen_srcjars, - output = java_gensrcjar, - singlejar = java_toolchain.single_jar, - mnemonic = "JavaInfoGeneratedSourceJar", - ) + + if codegen_plugin_output: + pass elif javac_java_info: java_gensrcjar = javac_java_info.annotation_processing.source_jar @@ -715,7 +726,8 @@ def _kt_jvm_library( # TODO: Remove the is_android_library_without_kt_srcs condition once KtAndroidLint # uses the same lint checks with AndroidLint - if not is_android_library_without_kt_srcs: + disable_lint_checks = disable_lint_checks + kt_codegen_processing_env.get("disabled_lint_checks", []) + if force_android_lint or not is_android_library_without_kt_srcs: lint_flags = [ "--java-language-level", # b/159950410 kt_toolchain.java_language_version, @@ -743,12 +755,12 @@ def _kt_jvm_library( merged_manifest = merged_manifest, resource_files = resource_files, baseline_file = androidlint_toolchains.get_baseline(ctx), - config = kt_toolchain.android_lint_config, + config = (None if (kt_srcs or common_srcs) else java_android_lint_config) or kt_toolchain.android_lint_config, android_lint_rules = plugins.android_lint_rulesets + [ - lint_actions.AndroidLintRulesetInfo(singlejars = legacy_java_plugin_classpaths), + lint_actions.AndroidLintRulesetInfo(singlejars = java_plugin_classpaths_for_java_srcs), ], lint_flags = lint_flags, - extra_input_depsets = [p.processor_data for p in java_plugin_datas_legacy] + [depset([java_genjar] if java_genjar else [])], + extra_input_depsets = [p.processor_data for p in java_plugin_datas], testonly = testonly, android_java8_libs = kt_toolchain.android_java8_apis_desugared, mnemonic = "KtAndroidLint", # so LSA extractor can distinguish Kotlin (b/189442586) @@ -881,6 +893,7 @@ def _validate_proguard_specs( ctx.actions.run( executable = proguard_allowlister, + toolchain = kt_jvm_toolchains.type, arguments = [args], inputs = [proguard_spec], outputs = [validated_proguard_spec], diff --git a/kotlin/common/BUILD b/kotlin/common/BUILD index d75adbc..0e51294 100644 --- a/kotlin/common/BUILD +++ b/kotlin/common/BUILD @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +load("//bazel:stubs.bzl", "integration_test_filegroup") load("@bazel_skylib//:bzl_library.bzl", "bzl_library") package( @@ -31,3 +32,7 @@ bzl_library( "//:visibility_bzl", ], ) + +integration_test_filegroup( + name = "bazel_osx_p4deps", +) diff --git a/kotlin/jvm/internal_do_not_use/traverse_exports/BUILD b/kotlin/jvm/internal_do_not_use/traverse_exports/BUILD index 0f39dbe..9b5382c 100644 --- a/kotlin/jvm/internal_do_not_use/traverse_exports/BUILD +++ b/kotlin/jvm/internal_do_not_use/traverse_exports/BUILD @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +load("//bazel:stubs.bzl", "integration_test_filegroup") load("@bazel_skylib//:bzl_library.bzl", "bzl_library") package( @@ -30,3 +31,11 @@ bzl_library( "@bazel_skylib//lib:sets", ], ) + +integration_test_filegroup( + name = "bazel_osx_p4deps", + deps = [ + ":traverse_exports_bzl", + "//kotlin/common:bazel_osx_p4deps", + ], +) diff --git a/kotlin/jvm/internal_do_not_use/traverse_exports/codegen_plugin.bzl b/kotlin/jvm/internal_do_not_use/traverse_exports/codegen_plugin.bzl deleted file mode 100644 index 84a0960..0000000 --- a/kotlin/jvm/internal_do_not_use/traverse_exports/codegen_plugin.bzl +++ /dev/null @@ -1,33 +0,0 @@ -# Copyright 2022 Google LLC. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the License); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""kt_codegen_plugin_visitor""" - -load("//kotlin:codegen_plugin.internal.bzl", "KtCodegenPluginInfo") -load("//:visibility.bzl", "RULES_KOTLIN") - -def _get_kt_codegen_plugins(_target, ctx_rule): - return [ - t[KtCodegenPluginInfo] - for t in getattr(ctx_rule.attr, "exported_plugins", []) - if KtCodegenPluginInfo in t - ] - -kt_codegen_plugin_visitor = struct( - name = "codegen_plugins", - visit_target = _get_kt_codegen_plugins, - filter_edge = None, - finish_expansion = None, - process_unvisited_target = None, -) diff --git a/kotlin/jvm/internal_do_not_use/util/BUILD b/kotlin/jvm/internal_do_not_use/util/BUILD index 7e82771..157c675 100644 --- a/kotlin/jvm/internal_do_not_use/util/BUILD +++ b/kotlin/jvm/internal_do_not_use/util/BUILD @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +load("//bazel:stubs.bzl", "integration_test_filegroup") load("@bazel_skylib//:bzl_library.bzl", "bzl_library") package( @@ -29,3 +30,8 @@ bzl_library( "@bazel_tools//tools/jdk:jvmopts", ], ) + +integration_test_filegroup( + name = "bazel_osx_p4deps", + deps = [":util_bzl"], +) diff --git a/kotlin/jvm/internal_do_not_use/util/class_file_selectors.bzl b/kotlin/jvm/internal_do_not_use/util/class_file_selectors.bzl deleted file mode 100644 index d3647d3..0000000 --- a/kotlin/jvm/internal_do_not_use/util/class_file_selectors.bzl +++ /dev/null @@ -1,59 +0,0 @@ -# Copyright 2022 Google LLC. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the License); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Selects bytecode class files with a matched source file in srcjars.""" - -load("//:visibility.bzl", "RULES_KOTLIN") -load("//toolchains/kotlin_jvm:kt_jvm_toolchains.bzl", "kt_jvm_toolchains") - -def gen_java_info_generated_class_jar(ctx, file_factory, kt_toolchain, input_jars, srcjars): - """Generates a class jar with class file jar entries matching files in the give srcjars. - - Args: - ctx: A rule context. - file_factory: A file factory in responsible for file creation under the current context. - kt_toolchain: The toolchain for kotlin builds. - input_jars: A sequence of jar files from which class files are selected. - srcjars: A sequence of source jar files that the selection references to. - Returns: - The output jar file, i.e. output_jar. - """ - output_jar = file_factory.declare_file("-java_info_generated_class_jar.jar") - input_jars = depset(input_jars) - transformer_env_files = depset(srcjars) - - transformer_entry_point = "com.google.devtools.jar.transformation.ClassFileSelectorBySourceFile" - transformer_jars = kt_toolchain.class_file_selector_by_source_file[JavaInfo].transitive_runtime_jars - jar_transformer = kt_toolchain.jar_transformer[DefaultInfo].files_to_run - - args = ctx.actions.args() - args.add_joined("--input_jars", input_jars, join_with = ",") - args.add_joined("--transformer_jars", transformer_jars, join_with = ",") - args.add("--transformer_entry_point", transformer_entry_point) - args.add_joined("--transformer_env_files", transformer_env_files, join_with = ",") - args.add("--result", output_jar) - ctx.actions.run( - inputs = depset(transitive = [ - input_jars, - transformer_jars, - transformer_env_files, - ]), - outputs = [output_jar], - arguments = [args], - progress_message = "Generating JavaInfo.generated_class_jar into %{output}", - mnemonic = "ClassFileSelectorBySourceFile", - executable = jar_transformer, - toolchain = kt_jvm_toolchains.type, - ) - return output_jar diff --git a/kotlin/jvm_compile.bzl b/kotlin/jvm_compile.bzl index ba3da82..58a2818 100644 --- a/kotlin/jvm_compile.bzl +++ b/kotlin/jvm_compile.bzl @@ -14,11 +14,11 @@ """Compile method that can compile kotlin or java sources""" +load("//:visibility.bzl", "RULES_DEFS_THAT_COMPILE_KOTLIN") +load("//bazel:stubs.bzl", "lint_actions") load(":common.bzl", "common") load(":compiler_plugin.bzl", "KtCompilerPluginInfo") load(":traverse_exports.bzl", "kt_traverse_exports") -load("//:visibility.bzl", "RULES_DEFS_THAT_COMPILE_KOTLIN") -load("//bazel:stubs.bzl", "lint_actions") _RULE_FAMILY = common.RULE_FAMILY @@ -38,6 +38,8 @@ def kt_jvm_compile( android_lint_plugins, resource_files, exported_plugins, + java_android_lint_config = None, + force_android_lint = False, # TODO Remove this param manifest = None, merged_manifest = None, classpath_resources = [], @@ -72,6 +74,8 @@ def kt_jvm_compile( execute as a part of linting. resource_files: List of Files. The list of Android Resource files. exported_plugins: List of exported javac/kotlinc plugins + java_android_lint_config: Android Lint XML config file to use if there are no Kotlin srcs + force_android_lint: Force AndroidLint action manifest: A File. The raw Android manifest. Optional. merged_manifest: A File. The merged Android manifest. Optional. classpath_resources: List of Files. The list of classpath resources (kt_jvm_library only). @@ -182,6 +186,8 @@ def kt_jvm_compile( common.collect_providers(KtCompilerPluginInfo, plugins) ), ), + java_android_lint_config = java_android_lint_config, + force_android_lint = force_android_lint, resource_files = resource_files, runtime_deps = [d[JavaInfo] for d in runtime_deps if JavaInfo in d], srcs = srcs, diff --git a/kotlin/jvm_library.bzl b/kotlin/jvm_library.bzl index 9485bcf..ad77dbd 100644 --- a/kotlin/jvm_library.bzl +++ b/kotlin/jvm_library.bzl @@ -14,13 +14,13 @@ """Kotlin kt_jvm_library rule.""" -load(":jvm_library.internal.bzl", "kt_jvm_library_helper") load("//bazel:stubs.bzl", "register_extension_info") +load("//:visibility.bzl", "RULES_KOTLIN") load("@bazel_skylib//lib:dicts.bzl", "dicts") load("//bazel:stubs.bzl", "lint_actions") load("//bazel:stubs.bzl", "LINT_REGISTRY") load("//bazel:stubs.bzl", "registry_checks_for_package") -load("//:visibility.bzl", "RULES_KOTLIN") +load(":jvm_library.internal.bzl", "kt_jvm_library_helper") def kt_jvm_library( name, @@ -87,7 +87,6 @@ def kt_jvm_library( # Helps go/build_cleaner to identify the targets generated by the macro. tags = (tags or []) + ["kt_jvm_library"] - # Ask go/build_cleaner to avoid all generated targets. javacopts = javacopts or [] disable_lint_checks = disable_lint_checks or [] diff --git a/kotlin/rules.bzl b/kotlin/rules.bzl index f08a1f2..e14f02e 100644 --- a/kotlin/rules.bzl +++ b/kotlin/rules.bzl @@ -14,18 +14,18 @@ """Kotlin rules.""" +# go/keep-sorted start load("//kotlin:compiler_opt.bzl", _kt_compiler_opt = "kt_compiler_opt") load("//kotlin:compiler_plugin_export.bzl", _kt_compiler_plugin_export = "kt_compiler_plugin_export") load("//kotlin:jvm_import.bzl", _kt_jvm_import = "kt_jvm_import") load("//kotlin:jvm_library.bzl", _kt_jvm_library = "kt_jvm_library") load("//kotlin:jvm_test.bzl", _kt_jvm_test = "kt_jvm_test") +# go/keep-sorted end +# go/keep-sorted start kt_compiler_opt = _kt_compiler_opt - kt_compiler_plugin_export = _kt_compiler_plugin_export - kt_jvm_import = _kt_jvm_import - kt_jvm_library = _kt_jvm_library - kt_jvm_test = _kt_jvm_test +# go/keep-sorted end diff --git a/tests/analysis/jvm_library_test.bzl b/tests/analysis/jvm_library_test.bzl index b43ee3e..1627c5f 100644 --- a/tests/analysis/jvm_library_test.bzl +++ b/tests/analysis/jvm_library_test.bzl @@ -14,20 +14,22 @@ """Kotlin kt_jvm_library rule tests.""" +load("//:visibility.bzl", "RULES_KOTLIN") load("//kotlin:jvm_library.bzl", "kt_jvm_library") load("//tests/analysis:util.bzl", "ONLY_FOR_ANALYSIS_TEST_TAGS", "create_file", "get_action", "get_arg") load("@bazel_skylib//lib:sets.bzl", "sets") load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") load(":assert_failure_test.bzl", "assert_failure_test") -load("//:visibility.bzl", "RULES_KOTLIN") _DEFAULT_LIST = ["__default__"] def _test_impl(ctx): env = analysistest.begin(ctx) - actions = analysistest.target_actions(env) actual = ctx.attr.target_under_test + actions = analysistest.target_actions(env) + kt_al_action = get_action(actions, "KtAndroidLint") + asserts.true( env, JavaInfo in actual, @@ -39,17 +41,6 @@ def _test_impl(ctx): "Expected a ProguardSpecProvider provider.", ) - if ctx.attr.expected_al_ruleset_names != _DEFAULT_LIST: - al_action = get_action(actions, "KtAndroidLint") - asserts.set_equals( - env, - sets.make(ctx.attr.expected_al_ruleset_names), - sets.make([ - a.rsplit("/", 1)[1] - for a in get_arg(al_action, "--lint_rules", style = "list") - ]), - ) - if ctx.attr.expected_runfile_names != _DEFAULT_LIST: asserts.set_equals( env, diff --git a/tests/analysis/util.bzl b/tests/analysis/util.bzl index 50bcc0e..653843b 100644 --- a/tests/analysis/util.bzl +++ b/tests/analysis/util.bzl @@ -111,6 +111,8 @@ def get_arg(action, arg_name, style = "trim"): if style == "trim": return arg[len(arg_name):] + elif style == "next": + return args[index + 1] elif style == "list": result = [] for i in range(index + 1, len(args)): @@ -118,5 +120,6 @@ def get_arg(action, arg_name, style = "trim"): break result.append(args[i]) return result + else: fail("Unrecognized arg style '%s" % style) diff --git a/toolchains/kotlin_jvm/BUILD b/toolchains/kotlin_jvm/BUILD index 1f20159..66056a7 100644 --- a/toolchains/kotlin_jvm/BUILD +++ b/toolchains/kotlin_jvm/BUILD @@ -14,14 +14,14 @@ """Kotlin/JVM toolchain.""" -load(":kt_jvm_toolchains.bzl", "kt_jvm_toolchains") -load("@bazel_skylib//:bzl_library.bzl", "bzl_library") +load("//bazel:stubs.bzl", "integration_test_filegroup") load("@bazel_skylib//rules:common_settings.bzl", "string_flag") +load(":kt_jvm_toolchains.bzl", "kt_jvm_toolchains") licenses(["notice"]) package( - default_visibility = ["//:internal"], + default_visibility = ["//visibility:public"], ) toolchain_type(name = kt_jvm_toolchains.name) @@ -36,13 +36,6 @@ toolchain( visibility = ["//visibility:public"], ) -# TODO: Remove this once cl/520112558 has made it into an Android rules release -alias( - name = "kt_jvm_toolchain_impl", - actual = ":kt_jvm_toolchain_linux_sts_jdk", - visibility = ["//visibility:public"], -) - kt_jvm_toolchains.declare( name = "kt_jvm_toolchain_linux_sts_jdk", # TODO: Required due to magic attribute for DexArchiveAspect, remove once that @@ -71,5 +64,12 @@ kt_jvm_toolchains.declare( string_flag( name = "profiling_filter", build_setting_default = "", - visibility = ["//visibility:private"], +) + +integration_test_filegroup( + name = "bazel_osx_p4deps", + deps = [ + "//:visibility_bzl", + "//tools:bazel_osx_p4deps", + ], ) diff --git a/toolchains/kotlin_jvm/kotlinc_flags.bzl b/toolchains/kotlin_jvm/kotlinc_flags.bzl new file mode 100644 index 0000000..65f3a5a --- /dev/null +++ b/toolchains/kotlin_jvm/kotlinc_flags.bzl @@ -0,0 +1,96 @@ +# Copyright 2022 Google LLC. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the License); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Static flags for kotlinc.""" + +load("//:visibility.bzl", "RULES_KOTLIN") + +_KT_LANG_VERSION = "1.9" + +_SHARED_FLAGS = [ + # We're supplying JDK in bootclasspath explicitly instead + "-no-jdk", + + # stdlib included in merged_deps + "-no-stdlib", + + # The bytecode format to emit + "-jvm-target", + "11", + + # Emit bytecode with parameter names + "-java-parameters", + + # Allow default method declarations, akin to what javac emits (b/110378321). + "-Xjvm-default=all", + + # Trust JSR305 nullness type qualifier nicknames the same as @Nonnull/@Nullable + # (see https://kotlinlang.org/docs/reference/java-interop.html#jsr-305-support) + "-Xjsr305=strict", + + # Trust annotations on type arguments, etc. + # (see https://kotlinlang.org/docs/java-interop.html#annotating-type-arguments-and-type-parameters) + "-Xtype-enhancement-improvements-strict-mode", + + # TODO: Remove this as the default setting (probably Kotlin 1.7) + "-Xenhance-type-parameter-types-to-def-not-null=true", + + # Explicitly set language version so we can update compiler separate from language version + "-language-version", + _KT_LANG_VERSION, + + # Enable type annotations in the JVM bytecode (b/170647926) + "-Xemit-jvm-type-annotations", + + # TODO: Temporarily disable 1.5's sam wrapper conversion + "-Xsam-conversions=class", + + # We don't want people to use experimental APIs, but if they do, we want them to use @OptIn + "-opt-in=kotlin.RequiresOptIn", + + # Don't complain when using old builds or release candidate builds + "-Xskip-prerelease-check", + + # Allows a no source files to create an empty jar. + "-Xallow-no-source-files", + + # TODO: Remove this flag + "-Xuse-old-innerclasses-logic", + + # TODO: Remove this flag + "-Xno-source-debug-extension", +] + +_CLI_ADDITIONAL_FLAGS = [ + # Silence all warning-level diagnostics + "-nowarn", +] + +def _read_one_define_flags(ctx, name): + define = ctx.var.get(name, default = None) + return [f for f in define.split(" ") if f] if define else [] + +def _read_define_flags(ctx): + return ( + _read_one_define_flags(ctx, "extra_kt_jvm_opts") + ) + +kotlinc_flags = struct( + # go/keep-sorted start + CLI_FLAGS = _SHARED_FLAGS + _CLI_ADDITIONAL_FLAGS, + IDE_FLAGS = _SHARED_FLAGS, + KT_LANG_VERSION = _KT_LANG_VERSION, + read_define_flags = _read_define_flags, + # go/keep-sorted end +) diff --git a/toolchains/kotlin_jvm/kt_jvm_toolchains.bzl b/toolchains/kotlin_jvm/kt_jvm_toolchains.bzl index 68bd2cd..16f8ac2 100644 --- a/toolchains/kotlin_jvm/kt_jvm_toolchains.bzl +++ b/toolchains/kotlin_jvm/kt_jvm_toolchains.bzl @@ -14,102 +14,29 @@ """Kotlin toolchain.""" -load("//bazel:stubs.bzl", "select_java_language_level") load("//:visibility.bzl", "RULES_DEFS_THAT_COMPILE_KOTLIN") load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") +load("//bazel:stubs.bzl", "select_java_language_level") +load(":kotlinc_flags.bzl", "kotlinc_flags") # Work around to toolchains in Google3. # buildifier: disable=provider-params KtJvmToolchainInfo = provider() -KT_VERSION = "v1_9_0" +KT_VERSION = "v1_9_10" -KT_LANG_VERSION = "1.9" +# TODO: Remove this alias. Why are we letting people read this? +KT_LANG_VERSION = kotlinc_flags.KT_LANG_VERSION # Kotlin JVM toolchain type label _TYPE = Label("//toolchains/kotlin_jvm:kt_jvm_toolchain_type") -def _kotlinc_common_flags(ctx, other_flags): - """Returns kotlinc flags to use in all compilations.""" - args = [ - # We're supplying JDK in bootclasspath explicitly instead - "-no-jdk", - - # stdlib included in merged_deps - "-no-stdlib", - - # The bytecode format to emit - "-jvm-target", - ctx.attr.jvm_target, - - # Emit bytecode with parameter names - "-java-parameters", - - # Allow default method declarations, akin to what javac emits (b/110378321). - "-Xjvm-default=all", - - # Trust JSR305 nullness type qualifier nicknames the same as @Nonnull/@Nullable - # (see https://kotlinlang.org/docs/reference/java-interop.html#jsr-305-support) - "-Xjsr305=strict", - - # Trust go/JSpecify nullness annotations - # (see https://kotlinlang.org/docs/whatsnew1520.html#support-for-jspecify-nullness-annotations) - "-Xjspecify-annotations=strict", - - # Trust annotations on type arguments, etc. - # (see https://kotlinlang.org/docs/java-interop.html#annotating-type-arguments-and-type-parameters) - "-Xtype-enhancement-improvements-strict-mode", - - # TODO: Remove this as the default setting (probably Kotlin 1.7) - "-Xenhance-type-parameter-types-to-def-not-null=true", - - # Explicitly set language version so we can update compiler separate from language version - "-language-version", - ctx.attr.kotlin_language_version, - - # Enable type annotations in the JVM bytecode (b/170647926) - "-Xemit-jvm-type-annotations", - - # TODO: Temporarily disable 1.5's sam wrapper conversion - "-Xsam-conversions=class", - - # We don't want people to use experimental APIs, but if they do, we want them to use @OptIn - "-opt-in=kotlin.RequiresOptIn", - - # Don't complain when using old builds or release candidate builds - "-Xskip-prerelease-check", - - # Allows a no source files to create an empty jar. - "-Xallow-no-source-files", - - # TODO: Remove this flag - "-Xuse-old-innerclasses-logic", - - # TODO: Remove this flag - "-Xno-source-debug-extension", - ] + other_flags - - # --define=extra_kt_jvm_opts is for overriding from command line. - # (Last wins in repeated --define=foo= use, so use --define=bar= instead.) - extra_kt_jvm_opts = ctx.var.get("extra_kt_jvm_opts", default = None) - if extra_kt_jvm_opts: - args.extend([o for o in extra_kt_jvm_opts.split(" ") if o]) - return args - -def _kotlinc_ide_flags(ctx): - return _kotlinc_common_flags(ctx, other_flags = []) - -def _kotlinc_cli_flags(ctx): - return _kotlinc_common_flags(ctx, other_flags = [ - # Silence all warning-level diagnostics - "-nowarn", - ]) - def _opt_for_test(val, getter): return getter(val) if val else None def _kt_jvm_toolchain_impl(ctx): profiling_filter = ctx.attr.profiling_filter[BuildSettingInfo].value + kotlinc_define_flags = kotlinc_flags.read_define_flags(ctx) kt_jvm_toolchain = dict( # go/keep-sorted start @@ -125,16 +52,16 @@ def _kt_jvm_toolchain_impl(ctx): java_language_version = ctx.attr.java_language_version, java_runtime = ctx.attr.java_runtime, jvm_abi_gen_plugin = ctx.file.jvm_abi_gen_plugin, - jvm_target = ctx.attr.jvm_target, kotlin_compiler = ctx.attr.kotlin_compiler[DefaultInfo].files_to_run, kotlin_language_version = ctx.attr.kotlin_language_version, kotlin_libs = [x[JavaInfo] for x in ctx.attr.kotlin_libs], kotlin_sdk_libraries = ctx.attr.kotlin_sdk_libraries, - kotlinc_cli_flags = _kotlinc_cli_flags(ctx), - kotlinc_ide_flags = _kotlinc_ide_flags(ctx), + kotlinc_cli_flags = ctx.attr.kotlinc_cli_flags + kotlinc_define_flags, + kotlinc_ide_flags = ctx.attr.kotlinc_ide_flags + kotlinc_define_flags, kt_codegen_java_runtime = ctx.attr.kt_codegen_java_runtime, proguard_whitelister = ctx.attr.proguard_whitelister[DefaultInfo].files_to_run, source_jar_zipper = ctx.file.source_jar_zipper, + toolchain_type = None if ctx.attr.toolchain_type == None else str(ctx.attr.toolchain_type.label), turbine = ctx.file.turbine, turbine_direct = _opt_for_test(ctx.attr.turbine_direct, lambda x: x[DefaultInfo].files_to_run), turbine_java_runtime = ctx.attr.turbine_java_runtime, @@ -201,9 +128,6 @@ kt_jvm_toolchain = rule( cfg = "exec", allow_single_file = [".jar"], ), - jvm_target = attr.string( - doc = "The value to pass as -jvm-target, indicating the bytecode format to emit.", - ), kotlin_compiler = attr.label( default = "@kotlinc//:kotlin_compiler", cfg = "exec", @@ -229,6 +153,14 @@ kt_jvm_toolchain = rule( ], cfg = "target", ), + kotlinc_cli_flags = attr.string_list( + doc = "The static flags to pass to CLI kotlinc invocations", + default = kotlinc_flags.CLI_FLAGS, + ), + kotlinc_ide_flags = attr.string_list( + doc = "The static flags to pass to IDE kotlinc invocations", + default = kotlinc_flags.IDE_FLAGS, + ), kt_codegen_java_runtime = attr.label( cfg = "exec", ), @@ -269,19 +201,22 @@ kt_jvm_toolchain = rule( turbine_java_runtime = attr.label( cfg = "exec", ), + toolchain_type = attr.label(), ), provides = [platform_common.ToolchainInfo], implementation = _kt_jvm_toolchain_impl, ) -def _declare(**kwargs): +def _declare( + toolchain_type = _TYPE, + **kwargs): kt_jvm_toolchain( android_java8_apis_desugared = select({ "//conditions:default": False, }), # The JVM bytecode version to output # https://kotlinlang.org/docs/compiler-reference.html#jvm-target-version - jvm_target = "11", + toolchain_type = toolchain_type, **kwargs ) diff --git a/tools/BUILD b/tools/BUILD index 812302f..820ac59 100644 --- a/tools/BUILD +++ b/tools/BUILD @@ -14,11 +14,12 @@ # Description: Kotlin building rules tools load("//bazel:deploy_jar_freshness_golden_test.bzl", "deploy_jar_freshness_golden_test") +load("//bazel:stubs.bzl", "integration_test_filegroup") licenses(["notice"]) package( - default_visibility = ["//:internal"], + default_visibility = ["//visibility:public"], ) # JAR that contains a marker entry, for identifying apps built by rules_kotlin. @@ -29,8 +30,12 @@ genrule( tools = ["@bazel_tools//tools/zip:zipper"], ) +integration_test_filegroup( + name = "bazel_osx_p4deps", +) + deploy_jar_freshness_golden_test( name = "source_jar_zipper_freshness_test", current_jar = "//tools/bin:source_jar_zipper_binary", - newly_built_jar = "//tools/java/com/google/devtools/kotlin:source_jar_zipper_deploy.jar", + newly_built_jar = "//tools/java/com/google/devtools/kotlin/srczip:source_jar_zipper_deploy.jar", ) diff --git a/tools/bin/BUILD b/tools/bin/BUILD index b5bc641..850ef15 100644 --- a/tools/bin/BUILD +++ b/tools/bin/BUILD @@ -14,7 +14,7 @@ licenses(["notice"]) # Apache 2.0 -package(default_visibility = ["//:internal"]) +package(default_visibility = ["//visibility:public"]) alias( name = "source_jar_zipper_binary", diff --git a/tools/bin/source_jar_zipper_deploy.jar b/tools/bin/source_jar_zipper_deploy.jar Binary files differindex b181e8a..e195334 100755 --- a/tools/bin/source_jar_zipper_deploy.jar +++ b/tools/bin/source_jar_zipper_deploy.jar diff --git a/tools/coverage/BUILD b/tools/coverage/BUILD index 78a73ea..bedb896 100644 --- a/tools/coverage/BUILD +++ b/tools/coverage/BUILD @@ -21,6 +21,6 @@ sh_binary( "//bazel:jacoco_cli", ], visibility = [ - "//:internal", + "//visibility:public", ], ) diff --git a/tools/java/com/google/devtools/kotlin/BUILD b/tools/java/com/google/devtools/kotlin/srczip/BUILD index 0b7a223..3c8969b 100644 --- a/tools/java/com/google/devtools/kotlin/BUILD +++ b/tools/java/com/google/devtools/kotlin/srczip/BUILD @@ -30,6 +30,6 @@ kt_jvm_library( java_binary( name = "source_jar_zipper", - main_class = "com.google.devtools.kotlin.SourceJarZipperKt", + main_class = "com.google.devtools.kotlin.srczip.SourceJarZipperKt", runtime_deps = [":source_jar_zipper_lib"], ) diff --git a/tools/java/com/google/devtools/kotlin/SourceJarZipper.kt b/tools/java/com/google/devtools/kotlin/srczip/SourceJarZipper.kt index 2ad7568..c82f356 100644 --- a/tools/java/com/google/devtools/kotlin/SourceJarZipper.kt +++ b/tools/java/com/google/devtools/kotlin/srczip/SourceJarZipper.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.devtools.kotlin +package com.google.devtools.kotlin.srczip import java.io.BufferedInputStream import java.io.BufferedOutputStream diff --git a/visibility.bzl b/visibility.bzl index 328e2e5..bb0710e 100644 --- a/visibility.bzl +++ b/visibility.bzl @@ -14,7 +14,9 @@ """Bzl visibility lists for rules_kotlin""" -RULES_KOTLIN = ["//..."] +RULES_KOTLIN = [ + "//...", +] TOOLS_KOTLIN = [ ] |