ahumesky | 655b5e4 | 2020-10-19 18:04:56 -0400 | [diff] [blame] | 1 | # Copyright 2018 The Bazel Authors. All rights reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """android_library rule.""" |
| 16 | |
| 17 | load(":attrs.bzl", _ATTRS = "ATTRS") |
| 18 | load(":impl.bzl", _impl = "impl") |
| 19 | load( |
| 20 | "@rules_android//rules:attrs.bzl", |
| 21 | _attrs = "attrs", |
| 22 | ) |
| 23 | |
| 24 | def _outputs(_defined_local_resources): |
| 25 | outputs = dict( |
| 26 | lib_jar = "lib%{name}.jar", |
| 27 | lib_src_jar = "lib%{name}-src.jar", |
| 28 | aar = "%{name}.aar", |
| 29 | ) |
| 30 | |
| 31 | if _defined_local_resources: |
ahumesky | 58f3fff | 2021-01-25 21:20:42 -0500 | [diff] [blame] | 32 | # TODO(b/177261846): resource-related predeclared outputs need to be re-pointed at the |
| 33 | # corresponding artifacts in the Starlark pipeline. |
ahumesky | 655b5e4 | 2020-10-19 18:04:56 -0400 | [diff] [blame] | 34 | outputs.update( |
| 35 | dict( |
ahumesky | 58f3fff | 2021-01-25 21:20:42 -0500 | [diff] [blame] | 36 | resources_src_jar = "_migrated/%{name}.srcjar", |
| 37 | resources_txt = "_migrated/%{name}_symbols/R.txt", |
| 38 | resources_jar = "_migrated/%{name}_resources.jar", |
ahumesky | 655b5e4 | 2020-10-19 18:04:56 -0400 | [diff] [blame] | 39 | ), |
| 40 | ) |
| 41 | |
| 42 | return outputs |
| 43 | |
| 44 | def make_rule( |
| 45 | attrs = _ATTRS, |
| 46 | implementation = _impl, |
ahumesky | 58f3fff | 2021-01-25 21:20:42 -0500 | [diff] [blame] | 47 | outputs = _outputs, |
ahumesky | 655b5e4 | 2020-10-19 18:04:56 -0400 | [diff] [blame] | 48 | additional_toolchains = []): |
| 49 | """Makes the rule. |
| 50 | |
| 51 | Args: |
| 52 | attrs: A dict. The attributes for the rule. |
| 53 | implementation: A function. The rule's implementation method. |
| 54 | |
| 55 | Returns: |
| 56 | A rule. |
| 57 | """ |
| 58 | return rule( |
| 59 | attrs = attrs, |
| 60 | fragments = [ |
| 61 | "android", |
| 62 | "java", |
| 63 | ], |
| 64 | implementation = implementation, |
| 65 | provides = [ |
| 66 | AndroidCcLinkParamsInfo, |
| 67 | AndroidIdeInfo, |
| 68 | AndroidIdlInfo, |
| 69 | AndroidLibraryResourceClassJarProvider, |
| 70 | AndroidNativeLibsInfo, |
| 71 | JavaInfo, |
| 72 | ], |
ahumesky | 58f3fff | 2021-01-25 21:20:42 -0500 | [diff] [blame] | 73 | outputs = outputs, |
| 74 | toolchains = [ |
| 75 | "@rules_android//toolchains/android:toolchain_type", |
| 76 | "@rules_android//toolchains/android_sdk:toolchain_type", |
| 77 | ] + additional_toolchains, |
ahumesky | 655b5e4 | 2020-10-19 18:04:56 -0400 | [diff] [blame] | 78 | _skylark_testable = True, |
| 79 | ) |
| 80 | |
| 81 | android_library = make_rule() |
| 82 | |
| 83 | def _is_defined(name, attrs): |
| 84 | return name in attrs and attrs[name] != None |
| 85 | |
| 86 | def attrs_metadata(attrs): |
| 87 | """Adds additional metadata for specific android_library attrs. |
| 88 | |
| 89 | Bazel native rules have additional capabilities when inspecting attrs that |
| 90 | are not available in Starlark. For example, native rules are able to |
| 91 | determine if an attribute was set by a user and make decisions based on this |
| 92 | knowledge - sometimes the behavior may differ if the user specifies the |
| 93 | default value of the attribute. As such the Starlark android_library uses |
| 94 | this shim to provide similar capabilities. |
| 95 | |
| 96 | Args: |
| 97 | attrs: The attributes passed to the android_library rule. |
| 98 | |
| 99 | Returns: |
| 100 | A dictionary containing attr values with additional metadata. |
| 101 | """ |
| 102 | |
| 103 | # Required for the outputs. |
| 104 | attrs["$defined_local_resources"] = bool( |
| 105 | attrs.get("assets") or |
| 106 | attrs.get("assets_dir") or |
| 107 | attrs.get("assets_dir") == "" or |
| 108 | attrs.get("export_manifest") or |
| 109 | attrs.get("manifest") or |
| 110 | attrs.get("resource_files"), |
| 111 | ) |
| 112 | |
| 113 | # TODO(b/116691720): Remove normalization when bug is fixed. |
| 114 | if _is_defined("exports_manifest", attrs): |
| 115 | attrs["exports_manifest"] = _attrs.tristate.normalize( |
| 116 | attrs.get("exports_manifest"), |
| 117 | ) |
| 118 | |
| 119 | # TODO(b/127517031): Remove these entries once fixed. |
| 120 | attrs["$defined_assets"] = _is_defined("assets", attrs) |
| 121 | attrs["$defined_assets_dir"] = _is_defined("assets_dir", attrs) |
| 122 | attrs["$defined_idl_import_root"] = _is_defined("idl_import_root", attrs) |
| 123 | attrs["$defined_idl_parcelables"] = _is_defined("idl_parcelables", attrs) |
| 124 | attrs["$defined_idl_srcs"] = _is_defined("idl_srcs", attrs) |
| 125 | return attrs |
| 126 | |
| 127 | def android_library_macro(**attrs): |
| 128 | """Bazel android_library rule. |
| 129 | |
| 130 | https://docs.bazel.build/versions/master/be/android.html#android_library |
| 131 | |
| 132 | Args: |
| 133 | **attrs: Rule attributes |
| 134 | """ |
| 135 | android_library(**attrs_metadata(attrs)) |