diff options
author | Romain Jobredeaux <jobredeaux@google.com> | 2023-03-20 17:26:13 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-03-20 17:26:13 +0000 |
commit | ec4be2ed60f3181e0a3ea51f9906a1c428c716c5 (patch) | |
tree | 13c3377871b030d69e60aacdd0562d17a78efaaa | |
parent | 15bcd3924431ee57d1386b240f97ddd453fe3afb (diff) | |
parent | 3a0e2ce00cbe93a43ea84367cfaacb8a0ae12e50 (diff) | |
download | bazelbuild-rules_android-ec4be2ed60f3181e0a3ea51f9906a1c428c716c5.tar.gz |
Merge remote-tracking branch 'aosp/upstream-pre-alpha' into HEAD am: 9621390973 am: 2d03939b6d am: 5d06937033 am: 3a0e2ce00c
Original change: https://android-review.googlesource.com/c/platform/external/bazelbuild-rules_android/+/2494414
Change-Id: Ic2afc724c82c6ab19f9625da6a44e4639e65c43c
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
25 files changed, 517 insertions, 47 deletions
@@ -1,6 +1,20 @@ load("@bazel_gazelle//:def.bzl", "gazelle") +load("@rules_license//rules:license.bzl", "license") -package(default_visibility = ["//visibility:public"]) +package( + default_visibility = ["//visibility:public"], + default_applicable_licenses = [":license"], +) + +license( + name = "license", + package_name = "bazelbuild/rules_android", + copyright_notice = "Copyright © 2023 The Bazel Authors. All rights reserved.", + license_kinds = [ + "@rules_license//licenses/spdx:Apache-2.0", + ], + license_text = "LICENSE", +) # gazelle:prefix github.com/bazelbuild/rules_android gazelle(name = "gazelle") @@ -13,6 +13,6 @@ third_party { value: "https://github.com/bazelbuild/rules_android" } version: "ab13c86fafc79b965b7ad6e4d91c821760d869d3" - last_upgrade_date { year: 2023 month: 2 day: 08 } + last_upgrade_date { year: 2023 month: 3 day: 17 } license_type: NOTICE } @@ -4,7 +4,8 @@ NOTE: This branch contains a development preview of the Starlark implementation of Android rules for Bazel. This code is incomplete and may not function as-is. -A version of Bazel built at or near head and the following flags are necessary to use these rules: +A version of Bazel built at or near head or a recent pre-release and the following flags are necessary to use these rules: + ``` --experimental_enable_android_migration_apis --experimental_google_legacy_api @@ -24,13 +25,18 @@ tree](https://source.bazel.build/bazel/+/master:src/main/java/com/google/devtool For the list of Android rules, see the Bazel [documentation](https://docs.bazel.build/versions/master/be/android.html). ## Getting Started -To use the new Bazel Android rules, add the following to your WORKSPACE file: +To use the Starlark Bazel Android rules, add the following to your WORKSPACE file: load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + + # Or a later commit + RULES_ANDROID_COMMIT= "0bf3093bd011acd35de3c479c8990dd630d552aa" + RULES_ANDROID_SHA = "b75a673a66c157138ab53f4d8612a6e655d38b69bb14207c1a6675f0e10afa61" http_archive( name = "build_bazel_rules_android", - urls = ["https://github.com/bazelbuild/rules_android/archive/refs/heads/pre-alpha.zip"], - strip_prefix = "rules_android-pre-alpha", + url = "https://github.com/bazelbuild/rules_android/archive/%s.zip" % RULES_ANDROID_COMMIT, + sha256 = RULES_ANDROID_SHA, + strip_prefix = "rules_android-%s" % RULES_ANDROID_COMMIT, ) load("@build_bazel_rules_android//:prereqs.bzl", "rules_android_prereqs") rules_android_prereqs() @@ -38,14 +44,17 @@ To use the new Bazel Android rules, add the following to your WORKSPACE file: rules_android_workspace() register_toolchains( - "@build_bazel_rules_android//toolchains/android:android_default_toolchain", - "@build_bazel_rules_android//toolchains/android_sdk:android_sdk_tools", + "@build_bazel_rules_android//toolchains/android:android_default_toolchain", + "@build_bazel_rules_android//toolchains/android_sdk:android_sdk_tools", ) - Then, in your BUILD files, import and use the rules: - load("@build_bazel_rules_android//rules:rules.bzl", "android_library") + load("@build_bazel_rules_android//rules:rules.bzl", "android_binary", "android_library") + android_binary( + ... + ) + android_library( ... ) diff --git a/android/BUILD b/android/BUILD new file mode 100644 index 0000000..4db0c10 --- /dev/null +++ b/android/BUILD @@ -0,0 +1,18 @@ +# Copyright 2023 The Bazel Authors. 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. + +""" +Package used for redirecting Starlark rules from //android/rules.bzl to //rules/rules.bzl. +Used for easier migration to a new branch due to directory differences. +""" diff --git a/android/rules.bzl b/android/rules.bzl new file mode 100644 index 0000000..556968d --- /dev/null +++ b/android/rules.bzl @@ -0,0 +1,64 @@ +# Copyright 2023 The Bazel Authors. 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. + +"""Redirecting starlark rules to //rules/rules.bzl for easier migration to a new branch.""" + +load( + "//rules/rules.bzl", + _aar_import = "aar_import", + _android_archive = "android_archive", + _android_binary = "android_binary", + _android_bundle_to_apks = "android_bundle_to_apks", + _android_device = "android_device", + _android_device_script_fixture = "android_device_script_fixture", + _android_host_service_fixture = "android_host_service_fixture", + _android_instrumentation_test = "android_instrumentation_test_macro", + _android_library = "android_library_macro", + _android_local_test = "android_local_test", + _android_ndk_repository = "android_ndk_repository", + _android_sdk = "android_sdk", + _android_sdk_repository = "android_sdk_repository", + _android_tools_defaults_jar = "android_tools_defaults_jar", + _apk_import = "apk_import", +) + +aar_import = _aar_import + +android_archive = _android_archive + +android_binary = _android_binary + +android_bundle_to_apks = _android_bundle_to_apks + +android_device = _android_device + +android_device_script_fixture = _android_device_script_fixture + +android_host_service_fixture = _android_host_service_fixture + +android_instrumentation_test = _android_instrumentation_test + +android_library = _android_library + +android_local_test = _android_local_test + +android_ndk_repository = _android_ndk_repository + +android_sdk = _android_sdk + +android_sdk_repository = _android_sdk_repository + +android_tools_defaults_jar = _android_tools_defaults_jar + +apk_import = _apk_import diff --git a/examples/basicapp/java/com/basicapp/BasicActivity.java b/examples/basicapp/java/com/basicapp/BasicActivity.java index 14042be..03c9aef 100644 --- a/examples/basicapp/java/com/basicapp/BasicActivity.java +++ b/examples/basicapp/java/com/basicapp/BasicActivity.java @@ -17,9 +17,10 @@ package com.basicapp; import android.app.Activity; import android.os.Bundle; import android.view.Menu; +import android.view.View; +import android.widget.Button; import android.widget.TextView; - /** * The main activity of the Basic Sample App. */ @@ -29,6 +30,24 @@ public class BasicActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.basic_activity); + + final Button buttons[] = { + findViewById(R.id.button_id_fizz), findViewById(R.id.button_id_buzz), + }; + + for (Button b : buttons) { + b.setOnClickListener( + new View.OnClickListener() { + public void onClick(View v) { + TextView tv = findViewById(R.id.text_hello); + if (v.getId() == R.id.button_id_fizz) { + tv.setText("fizz"); + } else if (v.getId() == R.id.button_id_buzz) { + tv.setText("buzz"); + } + } + }); + } } @Override diff --git a/examples/basicapp/java/com/basicapp/res/layout/basic_activity.xml b/examples/basicapp/java/com/basicapp/res/layout/basic_activity.xml index 61fb73e..f84199c 100644 --- a/examples/basicapp/java/com/basicapp/res/layout/basic_activity.xml +++ b/examples/basicapp/java/com/basicapp/res/layout/basic_activity.xml @@ -9,4 +9,15 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" /> + <Button + android:id="@+id/button_id_fizz" + android:layout_height="wrap_content" + android:layout_width="wrap_content" + android:text="fizz" /> + <Button + android:id="@+id/button_id_buzz" + android:layout_height="wrap_content" + android:layout_width="wrap_content" + android:text="buzz" /> + </LinearLayout> diff --git a/prereqs.bzl b/prereqs.bzl index d106a88..bf832d8 100644 --- a/prereqs.bzl +++ b/prereqs.bzl @@ -59,10 +59,10 @@ def rules_android_prereqs(): maybe( http_archive, name = "io_bazel_rules_go", - sha256 = "16e9fca53ed6bd4ff4ad76facc9b7b651a89db1689a2877d6fd7b82aa824e366", + sha256 = "dd926a88a564a9246713a9c00b35315f54cbd46b31a26d5d8fb264c07045f05d", urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.34.0/rules_go-v0.34.0.zip", - "https://github.com/bazelbuild/rules_go/releases/download/v0.34.0/rules_go-v0.34.0.zip", + "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.38.1/rules_go-v0.38.1.zip", + "https://github.com/bazelbuild/rules_go/releases/download/v0.38.1/rules_go-v0.38.1.zip", ], ) @@ -84,4 +84,14 @@ def rules_android_prereqs(): sha256 = "7e007fcfdca7b7228cb4de72707e8b317026ea95000f963e91d5ae365be52d0d", ) + maybe( + http_archive, + name = "rules_license", + urls = [ + "https://github.com/bazelbuild/rules_license/releases/download/0.0.4/rules_license-0.0.4.tar.gz", + "https://mirror.bazel.build/github.com/bazelbuild/rules_license/releases/download/0.0.4/rules_license-0.0.4.tar.gz", + ], + sha256 = "6157e1e68378532d0241ecd15d3c45f6e5cfd98fc10846045509fb2a7cc9e381", + ) + diff --git a/rules/aar_import/attrs.bzl b/rules/aar_import/attrs.bzl index 6fefdb0..022231b 100644 --- a/rules/aar_import/attrs.bzl +++ b/rules/aar_import/attrs.bzl @@ -69,4 +69,5 @@ ATTRS = _attrs.add( ), ), _attrs.DATA_CONTEXT, + _attrs.ANDROID_TOOLCHAIN_ATTRS, ) diff --git a/rules/acls.bzl b/rules/acls.bzl index e17e136..c84ed18 100644 --- a/rules/acls.bzl +++ b/rules/acls.bzl @@ -70,11 +70,12 @@ load( ) load("//rules/acls:kt_android_library_rollout.bzl", "KT_ANDROID_LIBRARY_FALLBACK", "KT_ANDROID_LIBRARY_ROLLOUT") load("//rules/acls:android_instrumentation_test_manifest_check_rollout.bzl", "ANDROID_INSTRUMENTATION_TEST_MANIFEST_CHECK_FALLBACK", "ANDROID_INSTRUMENTATION_TEST_MANIFEST_CHECK_ROLLOUT") -load("//rules/acls:android_instrumentation_test_prebuilt_test_apk.bzl", "ANDROID_INSTRUMENTATION_TEST_PREBUILT_TEST_APK") +load("//rules/acls:android_instrumentation_test_prebuilt_test_apk.bzl", "ANDROID_INSTRUMENTATION_TEST_PREBUILT_TEST_APK_FALLBACK", "ANDROID_INSTRUMENTATION_TEST_PREBUILT_TEST_APK_ROLLOUT") load("//rules/acls:android_rules_with_kt_rollout.bzl", "ANDROID_RULES_WITH_KT_FALLBACK", "ANDROID_RULES_WITH_KT_ROLLOUT") load("//rules/acls:baseline_profiles_rollout.bzl", "BASELINE_PROFILES_ROLLOUT") load("//rules/acls:enforce_min_sdk_floor_rollout.bzl", "ENFORCE_MIN_SDK_FLOOR_FALLBACK", "ENFORCE_MIN_SDK_FLOOR_ROLLOUT") load("//rules/acls:android_apk_to_bundle_features_lockdown.bzl", "ANDROID_APK_TO_BUNDLE_FEATURES") +load("//rules/acls:android_local_test_jdk_sts_rollout.bzl", "ANDROID_LOCAL_TEST_JDK_STS_FALLBACK", "ANDROID_LOCAL_TEST_JDK_STS_ROLLOUT") def _in_aar_import_deps_checker(fqn): return not matches(fqn, AAR_IMPORT_DEPS_CHECKER_FALLBACK_DICT) and matches(fqn, AAR_IMPORT_DEPS_CHECKER_ROLLOUT_DICT) @@ -193,7 +194,7 @@ def _in_android_instrumentation_test_manifest_check_rollout(fqn): return not matches(fqn, ANDROID_INSTRUMENTATION_TEST_MANIFEST_CHECK_FALLBACK_DICT) and matches(fqn, ANDROID_INSTRUMENTATION_TEST_MANIFEST_CHECK_ROLLOUT_DICT) def _in_android_instrumentation_test_prebuilt_test_apk(fqn): - return matches(fqn, ANDROID_INSTRUMENTATION_TEST_PREBUILT_TEST_APK_DICT) + return matches(fqn, ANDROID_INSTRUMENTATION_TEST_PREBUILT_TEST_APK_ROLLOUT_DICT) and not matches(fqn, ANDROID_INSTRUMENTATION_TEST_PREBUILT_TEST_APK_FALLBACK_DICT) def _in_android_rules_with_kt_rollout(fqn): return not matches(fqn, ANDROID_RULES_WITH_KT_FALLBACK_DICT) and matches(fqn, ANDROID_RULES_WITH_KT_ROLLOUT_DICT) @@ -213,6 +214,9 @@ def _in_android_apk_to_bundle_features(fqn): def _get_android_archive_duplicate_class_allowlist(fqn): return ANDROID_ARCHIVE_DUPLICATE_CLASS_ALLOWLIST.get(fqn, []) +def _in_android_local_test_jdk_sts_rollout(fqn): + return not matches(fqn, ANDROID_LOCAL_TEST_JDK_STS_FALLBACK_DICT) and matches(fqn, ANDROID_LOCAL_TEST_JDK_STS_ROLLOUT_DICT) + def make_dict(lst): """Do not use this method outside of acls directory.""" return {t: True for t in lst} @@ -281,12 +285,15 @@ KT_ANDROID_LIBRARY_ROLLOUT_DICT = make_dict(KT_ANDROID_LIBRARY_ROLLOUT) KT_ANDROID_LIBRARY_FALLBACK_DICT = make_dict(KT_ANDROID_LIBRARY_FALLBACK) ANDROID_INSTRUMENTATION_TEST_MANIFEST_CHECK_ROLLOUT_DICT = make_dict(ANDROID_INSTRUMENTATION_TEST_MANIFEST_CHECK_ROLLOUT) ANDROID_INSTRUMENTATION_TEST_MANIFEST_CHECK_FALLBACK_DICT = make_dict(ANDROID_INSTRUMENTATION_TEST_MANIFEST_CHECK_FALLBACK) -ANDROID_INSTRUMENTATION_TEST_PREBUILT_TEST_APK_DICT = make_dict(ANDROID_INSTRUMENTATION_TEST_PREBUILT_TEST_APK) +ANDROID_INSTRUMENTATION_TEST_PREBUILT_TEST_APK_ROLLOUT_DICT = make_dict(ANDROID_INSTRUMENTATION_TEST_PREBUILT_TEST_APK_ROLLOUT) +ANDROID_INSTRUMENTATION_TEST_PREBUILT_TEST_APK_FALLBACK_DICT = make_dict(ANDROID_INSTRUMENTATION_TEST_PREBUILT_TEST_APK_FALLBACK) BASELINE_PROFILES_ROLLOUT_DICT = make_dict(BASELINE_PROFILES_ROLLOUT) ENFORCE_MIN_SDK_FLOOR_ROLLOUT_DICT = make_dict(ENFORCE_MIN_SDK_FLOOR_ROLLOUT) ENFORCE_MIN_SDK_FLOOR_FALLBACK_DICT = make_dict(ENFORCE_MIN_SDK_FLOOR_FALLBACK) ANDROID_APK_TO_BUNDLE_FEATURES_DICT = make_dict(ANDROID_APK_TO_BUNDLE_FEATURES) ANDROID_LIBRARY_USE_AOSP_AIDL_COMPILER_ALLOWLIST_DICT = make_dict(ANDROID_LIBRARY_USE_AOSP_AIDL_COMPILER_ALLOWLIST) +ANDROID_LOCAL_TEST_JDK_STS_FALLBACK_DICT = make_dict(ANDROID_LOCAL_TEST_JDK_STS_FALLBACK) +ANDROID_LOCAL_TEST_JDK_STS_ROLLOUT_DICT = make_dict(ANDROID_LOCAL_TEST_JDK_STS_ROLLOUT) def matches(fqn, dct): # Labels with workspace names ("@workspace//pkg:target") are not supported. @@ -369,6 +376,7 @@ acls = struct( in_baseline_profiles_rollout = _in_baseline_profiles_rollout, in_enforce_min_sdk_floor_rollout = _in_enforce_min_sdk_floor_rollout, in_android_apk_to_bundle_features = _in_android_apk_to_bundle_features, + in_android_local_test_jdk_sts_rollout = _in_android_local_test_jdk_sts_rollout, ) # Visible for testing diff --git a/rules/acls/android_instrumentation_test_prebuilt_test_apk.bzl b/rules/acls/android_instrumentation_test_prebuilt_test_apk.bzl index 64aaa17..fb3b431 100644 --- a/rules/acls/android_instrumentation_test_prebuilt_test_apk.bzl +++ b/rules/acls/android_instrumentation_test_prebuilt_test_apk.bzl @@ -15,5 +15,8 @@ """Allow list of a_i_t targets allowed to use a prebuilt test apk.""" # keep sorted -ANDROID_INSTRUMENTATION_TEST_PREBUILT_TEST_APK = [ +ANDROID_INSTRUMENTATION_TEST_PREBUILT_TEST_APK_ROLLOUT = [ +] + +ANDROID_INSTRUMENTATION_TEST_PREBUILT_TEST_APK_FALLBACK = [ ] diff --git a/rules/acls/android_local_test_jdk_sts_rollout.bzl b/rules/acls/android_local_test_jdk_sts_rollout.bzl new file mode 100644 index 0000000..22055fa --- /dev/null +++ b/rules/acls/android_local_test_jdk_sts_rollout.bzl @@ -0,0 +1,24 @@ +# Copyright 2023 The Bazel Authors. 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. + +"""Allow and fallback lists for using the latest JDK runtime in android_local_test.""" + +# keep sorted +ANDROID_LOCAL_TEST_JDK_STS_ROLLOUT = [ +] + +# keep sorted +ANDROID_LOCAL_TEST_JDK_STS_FALLBACK = [ + "//:__subpackages__", +] diff --git a/rules/android_binary_internal/attrs.bzl b/rules/android_binary_internal/attrs.bzl index 2cc6a3d..5d5fe49 100644 --- a/rules/android_binary_internal/attrs.bzl +++ b/rules/android_binary_internal/attrs.bzl @@ -26,6 +26,10 @@ load( ATTRS = _attrs.replace( _attrs.add( dict( + srcs = attr.label_list( + # TODO(timpeut): Set PropertyFlag direct_compile_time_input + allow_files = [".java", ".srcjar"], + ), deps = attr.label_list( allow_files = True, allow_rules = [ @@ -61,6 +65,9 @@ ATTRS = _attrs.replace( shrink_resources = _attrs.tristate.create( default = _attrs.tristate.auto, ), + _java_toolchain = attr.label( + default = Label("//tools/jdk:toolchain_android_only"), + ), _defined_resource_files = attr.bool(default = False), _enable_manifest_merging = attr.bool(default = True), _cc_toolchain_split = attr.label( @@ -77,6 +84,7 @@ ATTRS = _attrs.replace( ), _attrs.COMPILATION, _attrs.DATA_CONTEXT, + _attrs.ANDROID_TOOLCHAIN_ATTRS, ), # TODO(b/167599192): don't override manifest attr to remove .xml file restriction. manifest = attr.label( diff --git a/rules/android_binary_internal/impl.bzl b/rules/android_binary_internal/impl.bzl index 3d89a7b..585bbd8 100644 --- a/rules/android_binary_internal/impl.bzl +++ b/rules/android_binary_internal/impl.bzl @@ -15,6 +15,8 @@ """Implementation.""" load("//rules:acls.bzl", "acls") +load("//rules:common.bzl", "common") +load("//rules:data_binding.bzl", "data_binding") load("//rules:java.bzl", "java") load( "//rules:processing_pipeline.bzl", @@ -101,6 +103,76 @@ def _process_native_libs(ctx, **_unusued_ctxs): value = struct(providers = providers), ) +def _process_build_stamp(_unused_ctx, **_unused_ctxs): + return ProviderInfo( + name = "stamp_ctx", + value = struct( + resource_files = [], + deps = [], + java_info = None, + providers = [], + ), + ) + +def _process_data_binding(ctx, java_package, packaged_resources_ctx, **_unused_ctxs): + return ProviderInfo( + name = "db_ctx", + value = data_binding.process( + ctx, + defines_resources = True, + enable_data_binding = ctx.attr.enable_data_binding, + java_package = java_package, + layout_info = packaged_resources_ctx.data_binding_layout_info, + artifact_type = "APPLICATION", + deps = utils.collect_providers(DataBindingV2Info, utils.dedupe_split_attr(ctx.split_attr.deps)), + data_binding_exec = get_android_toolchain(ctx).data_binding_exec.files_to_run, + data_binding_annotation_processor = + get_android_toolchain(ctx).data_binding_annotation_processor[JavaPluginInfo], + data_binding_annotation_template = + utils.only(get_android_toolchain(ctx).data_binding_annotation_template.files.to_list()), + ), + ) + +def _process_jvm(ctx, db_ctx, packaged_resources_ctx, stamp_ctx, **_unused_ctxs): + native_name = ctx.label.name.removesuffix(common.PACKAGED_RESOURCES_SUFFIX) + java_info = java.compile_android( + ctx, + # Use the same format as the class jar from native android_binary. + # Some macros expect the class jar to be named like this. + ctx.actions.declare_file("%s/lib%s.jar" % (ctx.label.name, native_name)), + ctx.actions.declare_file(ctx.label.name + "-src.jar"), + srcs = ctx.files.srcs + db_ctx.java_srcs, + javac_opts = ctx.attr.javacopts + db_ctx.javac_opts, + r_java = packaged_resources_ctx.r_java, + enable_deps_without_srcs = True, + deps = utils.collect_providers(JavaInfo, utils.dedupe_split_attr(ctx.split_attr.deps) + stamp_ctx.deps), + plugins = + utils.collect_providers(JavaPluginInfo, ctx.attr.plugins) + + db_ctx.java_plugins, + annotation_processor_additional_outputs = + db_ctx.java_annotation_processor_additional_outputs, + annotation_processor_additional_inputs = + db_ctx.java_annotation_processor_additional_inputs, + strict_deps = "DEFAULT", + java_toolchain = common.get_java_toolchain(ctx), + ) + java_info = java_common.add_constraints( + java_info, + constraints = ["android"], + ) + + providers = [] + if acls.in_android_binary_starlark_javac(str(ctx.label)): + providers.append(java_info) + + return ProviderInfo( + name = "jvm_ctx", + value = struct( + java_info = java_info, + providers = providers, + ), + ) + def use_legacy_manifest_merger(ctx): """Whether legacy manifest merging is enabled. @@ -144,9 +216,12 @@ def _is_test_binary(ctx): # insertion. PROCESSORS = dict( ManifestProcessor = _process_manifest, + StampProcessor = _process_build_stamp, ResourceProcessor = _process_resources, ValidateManifestProcessor = _validate_manifest, NativeLibsProcessor = _process_native_libs, + DataBindingProcessor = _process_data_binding, + JvmProcessor = _process_jvm, ) _PROCESSING_PIPELINE = processing_pipeline.make_processing_pipeline( diff --git a/rules/android_library/attrs.bzl b/rules/android_library/attrs.bzl index 2bc3a0d..6b7380b 100644 --- a/rules/android_library/attrs.bzl +++ b/rules/android_library/attrs.bzl @@ -212,4 +212,5 @@ ATTRS = _attrs.add( ), _attrs.COMPILATION, _attrs.DATA_CONTEXT, + _attrs.ANDROID_TOOLCHAIN_ATTRS, ) diff --git a/rules/attrs.bzl b/rules/attrs.bzl index fcab354..c084016 100644 --- a/rules/attrs.bzl +++ b/rules/attrs.bzl @@ -325,6 +325,14 @@ ANDROID_SDK_ATTRS = dict( ), ) +# Attributes for resolving platform-based toolchains. Only needed by the native DexArchiveAspect. +_ANDROID_TOOLCHAIN_ATTRS = dict( + _android_sdk_toolchain_type = attr.label( + allow_rules = ["toolchain_type"], + default = Label("//tools/android:sdk_toolchain_type"), + ), +) + ANDROID_TOOLS_DEFAULTS_JAR_ATTRS = _add(_ANDROID_SDK) attrs = struct( @@ -332,6 +340,7 @@ attrs = struct( COMPILATION = _COMPILATION, DATA_CONTEXT = _DATA_CONTEXT, JAVA_RUNTIME = _JAVA_RUNTIME, + ANDROID_TOOLCHAIN_ATTRS = _ANDROID_TOOLCHAIN_ATTRS, tristate = _tristate, add = _add, replace = _replace, diff --git a/rules/idl.bzl b/rules/idl.bzl index 42ea015..c77b137 100644 --- a/rules/idl.bzl +++ b/rules/idl.bzl @@ -67,13 +67,14 @@ def _gen_java_from_idl( args.add(idl_src) args.add(out_idl_java_src) + aidl_lib_files = [aidl_lib.files] if aidl_lib else [] + ctx.actions.run( executable = aidl, arguments = [args], inputs = depset( [aidl_framework], - transitive = [ - aidl_lib.files, + transitive = aidl_lib_files + [ transitive_idl_imports, transitive_idl_preprocessed, ], @@ -189,7 +190,7 @@ def _process( are supplied. aidl_lib: Target. A target pointing to the aidl_lib library required during Java compilation when Java code is generated from idl sources. - Optional, unless idl_srcs are supplied. + Optional. aidl_framework: Target. A target pointing to the aidl framework. Optional, unless idl_srcs are supplied. uses_aosp_compiler: boolean. If True, the upstream AOSP AIDL compiler is @@ -204,7 +205,7 @@ def _process( Returns: A IDLContextInfo provider. """ - if idl_srcs and not (aidl and aidl_lib and aidl_framework): + if idl_srcs and not (aidl and aidl_framework): _log.error(_AIDL_TOOLCHAIN_MISSING_ERROR) transitive_idl_import_roots = [] @@ -253,7 +254,7 @@ def _process( idl_srcs = idl_srcs, idl_import_root = idl_import_root, idl_java_srcs = idl_java_srcs, - idl_deps = [aidl_lib] if idl_java_srcs else [], + idl_deps = [aidl_lib] if (idl_java_srcs and aidl_lib) else [], providers = [ # TODO(b/146216105): Make this a Starlark provider. AndroidIdlInfo( diff --git a/rules/native_deps.bzl b/rules/native_deps.bzl index 7702e38..2711c03 100644 --- a/rules/native_deps.bzl +++ b/rules/native_deps.bzl @@ -145,15 +145,26 @@ def _contains_code_to_link(input): object_files = input.objects elif input.pic_objects: object_files = input.pic_objects - else: + elif _is_any_source_file(input.static_library, input.pic_static_library): # this is an opaque library so we're going to have to link it return True + else: + # if we reach here, this is a cc_library without sources generating an + # empty archive which does not need to be linked + # TODO(hvd): replace all such cc_library with exporting_cc_library + return False for obj in object_files: if not _is_shared_library(obj): # this library was built with a non-shared-library object so we should link it return True return False +def _is_any_source_file(*files): + for file in files: + if file and file.is_source: + return True + return False + def _is_shared_library(lib_artifact): if (lib_artifact.extension in ["so", "dll", "dylib"]): return True diff --git a/src/common/golang/BUILD b/src/common/golang/BUILD index 23d2761..4c1bf0f 100644 --- a/src/common/golang/BUILD +++ b/src/common/golang/BUILD @@ -94,3 +94,23 @@ go_library( srcs = ["flagfile.go"], importpath = "src/common/golang/flagfile", ) + +genrule( + name = "a_txt", + outs = ["a.txt"], + cmd = "echo hello world > $@", +) + +go_library( + name = "runfilelocation", + srcs = ["runfilelocation.go"], + importpath = "src/common/golang/runfilelocation", + deps = ["@io_bazel_rules_go//go/runfiles"], +) + +go_test( + name = "runfilelocation_test", + srcs = ["runfilelocation_test.go"], + data = [":a_txt"], + embed = [":runfilelocation"], +) diff --git a/src/common/golang/runfilelocation.go b/src/common/golang/runfilelocation.go new file mode 100644 index 0000000..b2eb5d6 --- /dev/null +++ b/src/common/golang/runfilelocation.go @@ -0,0 +1,40 @@ +// Copyright 2023 The Bazel Authors. 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. + +// Package runfilelocation provides utility functions to deal with runfiles + +package runfilelocation + +import ( + "os" + "path" + + "github.com/bazelbuild/rules_go/go/runfiles" +) + +// Find determines the absolute path to a given runfile +func Find(runfilePath string) (string, error) { + runfileLocation, err := runfiles.Rlocation(path.Join(os.Getenv("TEST_WORKSPACE"), runfilePath)) + + if err != nil { + return "", err + } + + // Check if file exists + if _, err := os.Stat(runfileLocation); err != nil { + return "", err + } + + return runfileLocation, err +} diff --git a/src/common/golang/runfilelocation_test.go b/src/common/golang/runfilelocation_test.go new file mode 100644 index 0000000..b2f3fb4 --- /dev/null +++ b/src/common/golang/runfilelocation_test.go @@ -0,0 +1,51 @@ +// Copyright 2023 The Bazel Authors. 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. + +package runfilelocation + +import ( + "io/ioutil" + "testing" +) + +func TestValidRunfileLocation(t *testing.T) { + // Check that Find() returns a valid path to a runfile + runfilePath := "src/common/golang/a.txt" + + absRunFilePath, err := Find(runfilePath) + if err != nil { + t.Errorf("Runfile path through Runfilelocation() failed: %v", err) + } + + // Check that the path actually exists + contents, err := ioutil.ReadFile(absRunFilePath) + text := string(contents) + if err != nil { + t.Errorf("Could not read file: %v", err) + } + + if text != "hello world\n" { + t.Errorf("Expected 'hello world' in file, got %v instead.", text) + } +} + +func TestInvalidRunfileLocation(t *testing.T) { + invalidRunfilePath := "src/common/golang/b.txt" + + runfileLocationShouldNotExist, err := Find(invalidRunfilePath) + if err == nil { + // If no error, that means the library is not properly detecting invalid runfile paths + t.Errorf("Expected error: %v should not be a valid path. Returned %v", invalidRunfilePath, runfileLocationShouldNotExist) + } +} diff --git a/src/tools/ak/BUILD b/src/tools/ak/BUILD index 985575f..51d524f 100644 --- a/src/tools/ak/BUILD +++ b/src/tools/ak/BUILD @@ -9,26 +9,13 @@ licenses(["notice"]) go_binary( name = "ak", - srcs = ["ak.go"], + srcs = [ + "ak.go", + ], deps = [ + ":akcommands", ":types", "//src/common/golang:flagfile", - "//src/tools/ak/bucketize", - "//src/tools/ak/compile", - "//src/tools/ak/dex", - "//src/tools/ak/extractaar", - "//src/tools/ak/finalrjar", - "//src/tools/ak/generatemanifest", - "//src/tools/ak/link", - "//src/tools/ak/liteparse", - "//src/tools/ak/manifest", - "//src/tools/ak/mindex", - "//src/tools/ak/nativelib", - "//src/tools/ak/patch", - "//src/tools/ak/repack", - "//src/tools/ak/rjar", - "//src/tools/ak/rstub", - "//src/tools/ak/shellapk", ], ) @@ -52,3 +39,27 @@ go_library( "//src/common/golang:xml2", ], ) + +go_library( + name = "akcommands", + srcs = ["akcommands.go"], + importpath = "src/tools/ak/akcommands", + deps = [ + ":types", + "//src/tools/ak/bucketize", + "//src/tools/ak/compile", + "//src/tools/ak/dex", + "//src/tools/ak/extractaar", + "//src/tools/ak/finalrjar", + "//src/tools/ak/generatemanifest", + "//src/tools/ak/link", + "//src/tools/ak/liteparse", + "//src/tools/ak/manifest", + "//src/tools/ak/mindex", + "//src/tools/ak/nativelib", + "//src/tools/ak/patch", + "//src/tools/ak/repack", + "//src/tools/ak/rjar", + "//src/tools/ak/shellapk", + ], +) diff --git a/src/tools/ak/akcommands.go b/src/tools/ak/akcommands.go new file mode 100644 index 0000000..7a100ca --- /dev/null +++ b/src/tools/ak/akcommands.go @@ -0,0 +1,56 @@ +// Copyright 2023 The Bazel Authors. 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. + +// Package akcommands provides a map of all AK commands to their respective binaries. +package akcommands + +import ( + "src/tools/ak/bucketize/bucketize" + "src/tools/ak/compile/compile" + "src/tools/ak/dex/dex" + "src/tools/ak/extractaar/extractaar" + "src/tools/ak/finalrjar/finalrjar" + "src/tools/ak/generatemanifest/generatemanifest" + "src/tools/ak/link/link" + "src/tools/ak/liteparse/liteparse" + "src/tools/ak/manifest/manifest" + "src/tools/ak/mindex/mindex" + "src/tools/ak/nativelib/nativelib" + "src/tools/ak/patch/patch" + "src/tools/ak/repack/repack" + "src/tools/ak/rjar/rjar" + "src/tools/ak/shellapk/shellapk" + "src/tools/ak/types" +) + +var ( + // Cmds map AK commands to their respective binaries + Cmds = map[string]types.Command{ + "bucketize": bucketize.Cmd, + "compile": compile.Cmd, + "dex": dex.Cmd, + "extractaar": extractaar.Cmd, + "link": link.Cmd, + "liteparse": liteparse.Cmd, + "generatemanifest": generatemanifest.Cmd, + "manifest": manifest.Cmd, + "mindex": mindex.Cmd, + "nativelib": nativelib.Cmd, + "patch": patch.Cmd, + "repack": repack.Cmd, + "rjar": rjar.Cmd, + "finalrjar": finalrjar.Cmd, + "shellapk": shellapk.Cmd, + } +) diff --git a/toolchains/android/toolchain.bzl b/toolchains/android/toolchain.bzl index 1a45c5d..905238d 100644 --- a/toolchains/android/toolchain.bzl +++ b/toolchains/android/toolchain.bzl @@ -66,12 +66,6 @@ _ATTRS = dict( cfg = "exec", executable = True, ), - android_archive_manifest_package_validator = attr.label( - allow_files = True, - default = "@androidsdk//:fail", - cfg = "exec", - executable = True, - ), android_archive_packages_validator = attr.label( allow_files = True, default = "@androidsdk//:fail", @@ -102,6 +96,12 @@ _ATTRS = dict( default = "//tools/android:bundletool_deploy.jar", executable = True, ), + centralize_r_class_tool = attr.label( + allow_files = True, + cfg = "exec", + default = "@androidsdk//:fail", + executable = True, + ), data_binding_annotation_processor = attr.label( cfg = "exec", default = "//tools/android:compiler_annotation_processor", diff --git a/tools/android/BUILD b/tools/android/BUILD index 7a85b59..2995c06 100644 --- a/tools/android/BUILD +++ b/tools/android/BUILD @@ -5,6 +5,12 @@ android_jar( visibility = ["//visibility:public"], ) +alias( + name = "sdk_toolchain_type", + actual = "@bazel_tools//tools/android:sdk_toolchain_type", + visibility = ["//visibility:public"], +) + # TODO(b/175833857): This is a stub, should remove. alias( name = "merge_manifests", |