Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2018 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | """A tool for checking that a manifest agrees with the build system.""" |
| 18 | |
| 19 | from __future__ import print_function |
| 20 | |
| 21 | import argparse |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 22 | import json |
Ulya Trafimovich | 24abbe8 | 2022-04-28 12:50:47 +0100 | [diff] [blame^] | 23 | import os |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 24 | import re |
| 25 | import subprocess |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 26 | import sys |
Ulya Trafimovich | 24abbe8 | 2022-04-28 12:50:47 +0100 | [diff] [blame^] | 27 | from collections import OrderedDict |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 28 | from xml.dom import minidom |
| 29 | |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 30 | from manifest import android_ns |
| 31 | from manifest import get_children_with_tag |
| 32 | from manifest import parse_manifest |
| 33 | from manifest import write_xml |
| 34 | |
| 35 | |
| 36 | class ManifestMismatchError(Exception): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 37 | pass |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def parse_args(): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 41 | """Parse commandline arguments.""" |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 42 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 43 | parser = argparse.ArgumentParser() |
| 44 | parser.add_argument( |
| 45 | '--uses-library', |
| 46 | dest='uses_libraries', |
| 47 | action='append', |
Ulya Trafimovich | 24abbe8 | 2022-04-28 12:50:47 +0100 | [diff] [blame^] | 48 | default=[], |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 49 | help='specify uses-library entries known to the build system') |
| 50 | parser.add_argument( |
| 51 | '--optional-uses-library', |
| 52 | dest='optional_uses_libraries', |
| 53 | action='append', |
Ulya Trafimovich | 24abbe8 | 2022-04-28 12:50:47 +0100 | [diff] [blame^] | 54 | default=[], |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 55 | help='specify uses-library entries known to the build system with ' |
| 56 | 'required:false' |
| 57 | ) |
| 58 | parser.add_argument( |
| 59 | '--enforce-uses-libraries', |
| 60 | dest='enforce_uses_libraries', |
| 61 | action='store_true', |
| 62 | help='check the uses-library entries known to the build system against ' |
| 63 | 'the manifest' |
| 64 | ) |
| 65 | parser.add_argument( |
| 66 | '--enforce-uses-libraries-relax', |
| 67 | dest='enforce_uses_libraries_relax', |
| 68 | action='store_true', |
| 69 | help='do not fail immediately, just save the error message to file') |
| 70 | parser.add_argument( |
| 71 | '--enforce-uses-libraries-status', |
| 72 | dest='enforce_uses_libraries_status', |
| 73 | help='output file to store check status (error message)') |
| 74 | parser.add_argument( |
| 75 | '--extract-target-sdk-version', |
| 76 | dest='extract_target_sdk_version', |
| 77 | action='store_true', |
| 78 | help='print the targetSdkVersion from the manifest') |
| 79 | parser.add_argument( |
| 80 | '--dexpreopt-config', |
Ulya Trafimovich | 24abbe8 | 2022-04-28 12:50:47 +0100 | [diff] [blame^] | 81 | dest='dexpreopt_config', |
| 82 | help='a path to dexpreopt.config file for this library/app') |
| 83 | parser.add_argument( |
| 84 | '--dexpreopt-dep-config', |
| 85 | dest='dexpreopt_dep_configs', |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 86 | action='append', |
Ulya Trafimovich | 24abbe8 | 2022-04-28 12:50:47 +0100 | [diff] [blame^] | 87 | default=[], |
| 88 | help='a path to dexpreopt.config file for a dependency library') |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 89 | parser.add_argument('--aapt', dest='aapt', help='path to aapt executable') |
| 90 | parser.add_argument( |
| 91 | '--output', '-o', dest='output', help='output AndroidManifest.xml file') |
| 92 | parser.add_argument('input', help='input AndroidManifest.xml file') |
| 93 | return parser.parse_args() |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 94 | |
| 95 | |
Ulya Trafimovich | b4c19f8 | 2021-11-01 12:57:59 +0000 | [diff] [blame] | 96 | C_RED = "\033[1;31m" |
| 97 | C_GREEN = "\033[1;32m" |
| 98 | C_BLUE = "\033[1;34m" |
| 99 | C_OFF = "\033[0m" |
| 100 | C_BOLD = "\033[1m" |
| 101 | |
| 102 | |
Ulya Trafimovich | bb7513d | 2021-03-30 17:15:16 +0100 | [diff] [blame] | 103 | def enforce_uses_libraries(manifest, required, optional, relax, is_apk, path): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 104 | """Verify that the <uses-library> tags in the manifest match those provided |
| 105 | |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 106 | by the build system. |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 107 | |
| 108 | Args: |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 109 | manifest: manifest (either parsed XML or aapt dump of APK) |
| 110 | required: required libs known to the build system |
| 111 | optional: optional libs known to the build system |
| 112 | relax: if true, suppress error on mismatch and just write it to file |
| 113 | is_apk: if the manifest comes from an APK or an XML file |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 114 | """ |
| 115 | if is_apk: |
| 116 | manifest_required, manifest_optional, tags = extract_uses_libs_apk( |
| 117 | manifest) |
| 118 | else: |
| 119 | manifest_required, manifest_optional, tags = extract_uses_libs_xml( |
| 120 | manifest) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 121 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 122 | # Trim namespace component. Normally Soong does that automatically when it |
| 123 | # handles module names specified in Android.bp properties. However not all |
| 124 | # <uses-library> entries in the manifest correspond to real modules: some of |
| 125 | # the optional libraries may be missing at build time. Therefor this script |
| 126 | # accepts raw module names as spelled in Android.bp/Amdroid.mk and trims the |
| 127 | # optional namespace part manually. |
| 128 | required = trim_namespace_parts(required) |
| 129 | optional = trim_namespace_parts(optional) |
Ulya Trafimovich | 1b51345 | 2021-07-20 14:27:32 +0100 | [diff] [blame] | 130 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 131 | if manifest_required == required and manifest_optional == optional: |
| 132 | return None |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 133 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 134 | #pylint: disable=line-too-long |
| 135 | errmsg = ''.join([ |
| 136 | 'mismatch in the <uses-library> tags between the build system and the ' |
| 137 | 'manifest:\n', |
Ulya Trafimovich | b4c19f8 | 2021-11-01 12:57:59 +0000 | [diff] [blame] | 138 | '\t- required libraries in build system: %s[%s]%s\n' % (C_RED, ', '.join(required), C_OFF), |
| 139 | '\t vs. in the manifest: %s[%s]%s\n' % (C_RED, ', '.join(manifest_required), C_OFF), |
| 140 | '\t- optional libraries in build system: %s[%s]%s\n' % (C_RED, ', '.join(optional), C_OFF), |
| 141 | '\t vs. in the manifest: %s[%s]%s\n' % (C_RED, ', '.join(manifest_optional), C_OFF), |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 142 | '\t- tags in the manifest (%s):\n' % path, |
| 143 | '\t\t%s\n' % '\t\t'.join(tags), |
Ulya Trafimovich | b4c19f8 | 2021-11-01 12:57:59 +0000 | [diff] [blame] | 144 | '%snote:%s the following options are available:\n' % (C_BLUE, C_OFF), |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 145 | '\t- to temporarily disable the check on command line, rebuild with ', |
Ulya Trafimovich | b4c19f8 | 2021-11-01 12:57:59 +0000 | [diff] [blame] | 146 | '%sRELAX_USES_LIBRARY_CHECK=true%s' % (C_BOLD, C_OFF), |
| 147 | ' (this will set compiler filter "verify" and disable AOT-compilation in dexpreopt)\n', |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 148 | '\t- to temporarily disable the check for the whole product, set ', |
Ulya Trafimovich | b4c19f8 | 2021-11-01 12:57:59 +0000 | [diff] [blame] | 149 | '%sPRODUCT_BROKEN_VERIFY_USES_LIBRARIES := true%s in the product makefiles\n' % (C_BOLD, C_OFF), |
| 150 | '\t- to fix the check, make build system properties coherent with the manifest\n', |
| 151 | '\t- for details, see %sbuild/make/Changes.md%s' % (C_GREEN, C_OFF), |
| 152 | ' and %shttps://source.android.com/devices/tech/dalvik/art-class-loader-context%s\n' % (C_GREEN, C_OFF) |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 153 | ]) |
| 154 | #pylint: enable=line-too-long |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 155 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 156 | if not relax: |
| 157 | raise ManifestMismatchError(errmsg) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 158 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 159 | return errmsg |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 160 | |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 161 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 162 | MODULE_NAMESPACE = re.compile('^//[^:]+:') |
| 163 | |
Ulya Trafimovich | 1b51345 | 2021-07-20 14:27:32 +0100 | [diff] [blame] | 164 | |
| 165 | def trim_namespace_parts(modules): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 166 | """Trim the namespace part of each module, if present. |
Ulya Trafimovich | 1b51345 | 2021-07-20 14:27:32 +0100 | [diff] [blame] | 167 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 168 | Leave only the name. |
| 169 | """ |
| 170 | |
| 171 | trimmed = [] |
| 172 | for module in modules: |
| 173 | trimmed.append(MODULE_NAMESPACE.sub('', module)) |
| 174 | return trimmed |
Ulya Trafimovich | 1b51345 | 2021-07-20 14:27:32 +0100 | [diff] [blame] | 175 | |
| 176 | |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 177 | def extract_uses_libs_apk(badging): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 178 | """Extract <uses-library> tags from the manifest of an APK.""" |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 179 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 180 | pattern = re.compile("^uses-library(-not-required)?:'(.*)'$", re.MULTILINE) |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 181 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 182 | required = [] |
| 183 | optional = [] |
| 184 | lines = [] |
| 185 | for match in re.finditer(pattern, badging): |
| 186 | lines.append(match.group(0)) |
| 187 | libname = match.group(2) |
| 188 | if match.group(1) is None: |
| 189 | required.append(libname) |
| 190 | else: |
| 191 | optional.append(libname) |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 192 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 193 | required = first_unique_elements(required) |
| 194 | optional = first_unique_elements(optional) |
| 195 | tags = first_unique_elements(lines) |
| 196 | return required, optional, tags |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 197 | |
| 198 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 199 | def extract_uses_libs_xml(xml): #pylint: disable=inconsistent-return-statements |
| 200 | """Extract <uses-library> tags from the manifest.""" |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 201 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 202 | manifest = parse_manifest(xml) |
| 203 | elems = get_children_with_tag(manifest, 'application') |
| 204 | application = elems[0] if len(elems) == 1 else None |
| 205 | if len(elems) > 1: #pylint: disable=no-else-raise |
| 206 | raise RuntimeError('found multiple <application> tags') |
| 207 | elif not elems: |
| 208 | if uses_libraries or optional_uses_libraries: #pylint: disable=undefined-variable |
| 209 | raise ManifestMismatchError('no <application> tag found') |
| 210 | return |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 211 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 212 | libs = get_children_with_tag(application, 'uses-library') |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 213 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 214 | required = [uses_library_name(x) for x in libs if uses_library_required(x)] |
| 215 | optional = [ |
| 216 | uses_library_name(x) for x in libs if not uses_library_required(x) |
| 217 | ] |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 218 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 219 | # render <uses-library> tags as XML for a pretty error message |
| 220 | tags = [] |
| 221 | for lib in libs: |
| 222 | tags.append(lib.toprettyxml()) |
Ulya Trafimovich | bb7513d | 2021-03-30 17:15:16 +0100 | [diff] [blame] | 223 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 224 | required = first_unique_elements(required) |
| 225 | optional = first_unique_elements(optional) |
| 226 | tags = first_unique_elements(tags) |
| 227 | return required, optional, tags |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 228 | |
| 229 | |
| 230 | def first_unique_elements(l): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 231 | result = [] |
| 232 | for x in l: |
| 233 | if x not in result: |
| 234 | result.append(x) |
| 235 | return result |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 236 | |
| 237 | |
| 238 | def uses_library_name(lib): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 239 | """Extract the name attribute of a uses-library tag. |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 240 | |
| 241 | Args: |
| 242 | lib: a <uses-library> tag. |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 243 | """ |
| 244 | name = lib.getAttributeNodeNS(android_ns, 'name') |
| 245 | return name.value if name is not None else '' |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 246 | |
| 247 | |
| 248 | def uses_library_required(lib): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 249 | """Extract the required attribute of a uses-library tag. |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 250 | |
| 251 | Args: |
| 252 | lib: a <uses-library> tag. |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 253 | """ |
| 254 | required = lib.getAttributeNodeNS(android_ns, 'required') |
| 255 | return (required.value == 'true') if required is not None else True |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 256 | |
| 257 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 258 | def extract_target_sdk_version(manifest, is_apk=False): |
| 259 | """Returns the targetSdkVersion from the manifest. |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 260 | |
| 261 | Args: |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 262 | manifest: manifest (either parsed XML or aapt dump of APK) |
| 263 | is_apk: if the manifest comes from an APK or an XML file |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 264 | """ |
| 265 | if is_apk: #pylint: disable=no-else-return |
| 266 | return extract_target_sdk_version_apk(manifest) |
| 267 | else: |
| 268 | return extract_target_sdk_version_xml(manifest) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 269 | |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 270 | |
| 271 | def extract_target_sdk_version_apk(badging): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 272 | """Extract targetSdkVersion tags from the manifest of an APK.""" |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 273 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 274 | pattern = re.compile("^targetSdkVersion?:'(.*)'$", re.MULTILINE) |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 275 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 276 | for match in re.finditer(pattern, badging): |
| 277 | return match.group(1) |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 278 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 279 | raise RuntimeError('cannot find targetSdkVersion in the manifest') |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 280 | |
| 281 | |
| 282 | def extract_target_sdk_version_xml(xml): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 283 | """Extract targetSdkVersion tags from the manifest.""" |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 284 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 285 | manifest = parse_manifest(xml) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 286 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 287 | # Get or insert the uses-sdk element |
| 288 | uses_sdk = get_children_with_tag(manifest, 'uses-sdk') |
| 289 | if len(uses_sdk) > 1: #pylint: disable=no-else-raise |
| 290 | raise RuntimeError('found multiple uses-sdk elements') |
| 291 | elif len(uses_sdk) == 0: |
| 292 | raise RuntimeError('missing uses-sdk element') |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 293 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 294 | uses_sdk = uses_sdk[0] |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 295 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 296 | min_attr = uses_sdk.getAttributeNodeNS(android_ns, 'minSdkVersion') |
| 297 | if min_attr is None: |
| 298 | raise RuntimeError('minSdkVersion is not specified') |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 299 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 300 | target_attr = uses_sdk.getAttributeNodeNS(android_ns, 'targetSdkVersion') |
| 301 | if target_attr is None: |
| 302 | target_attr = min_attr |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 303 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 304 | return target_attr.value |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 305 | |
| 306 | |
Ulya Trafimovich | 24abbe8 | 2022-04-28 12:50:47 +0100 | [diff] [blame^] | 307 | def remove_duplicates(l): |
| 308 | return list(OrderedDict.fromkeys(l)) |
| 309 | |
| 310 | |
| 311 | def load_dexpreopt_configs(args): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 312 | """Load dexpreopt.config files and map module names to library names.""" |
| 313 | module_to_libname = {} |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 314 | |
Ulya Trafimovich | 24abbe8 | 2022-04-28 12:50:47 +0100 | [diff] [blame^] | 315 | # Go over dexpreopt.config files for uses-library dependencies and create |
| 316 | # a mapping from module name to real library name (they may differ). |
| 317 | for config in args.dexpreopt_dep_configs: |
| 318 | # Empty dexpreopt.config files are expected for some dependencies. |
| 319 | if os.stat(config).st_size != 0: |
| 320 | with open(config, 'r') as f: |
| 321 | contents = json.load(f) |
| 322 | module_to_libname[contents['Name']] = contents['ProvidesUsesLibrary'] |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 323 | |
Ulya Trafimovich | 24abbe8 | 2022-04-28 12:50:47 +0100 | [diff] [blame^] | 324 | required = translate_libnames(args.uses_libraries, module_to_libname) |
| 325 | optional = translate_libnames(args.optional_uses_libraries, module_to_libname) |
| 326 | |
| 327 | # Add extra uses-libraries from the library/app's own dexpreopt.config. |
| 328 | # Extra libraries may be propagated via dependencies' dexpreopt.config files |
| 329 | # (not only uses-library ones, but also transitively via static libraries). |
| 330 | if args.dexpreopt_config: |
| 331 | with open(args.dexpreopt_config, 'r') as f: |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 332 | contents = json.load(f) |
Ulya Trafimovich | 24abbe8 | 2022-04-28 12:50:47 +0100 | [diff] [blame^] | 333 | for clc in contents['ClassLoaderContexts']['any']: |
| 334 | ulib = clc['Name'] |
| 335 | if clc['Optional']: |
| 336 | optional.append(ulib) |
| 337 | else: |
| 338 | required.append(ulib) |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 339 | |
Ulya Trafimovich | 24abbe8 | 2022-04-28 12:50:47 +0100 | [diff] [blame^] | 340 | required = remove_duplicates(required) |
| 341 | optional = remove_duplicates(optional) |
| 342 | |
| 343 | # If the same library is both in optional and required, prefer required. |
| 344 | # This may happen for compatibility libraries, e.g. org.apache.http.legacy. |
| 345 | for lib in required: |
| 346 | if lib in optional: |
| 347 | optional.remove(lib) |
| 348 | |
| 349 | return required, optional |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 350 | |
| 351 | |
| 352 | def translate_libnames(modules, module_to_libname): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 353 | """Translate module names into library names using the mapping.""" |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 354 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 355 | libnames = [] |
| 356 | for name in modules: |
| 357 | if name in module_to_libname: |
| 358 | name = module_to_libname[name] |
| 359 | libnames.append(name) |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 360 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 361 | return libnames |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 362 | |
| 363 | |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 364 | def main(): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 365 | """Program entry point.""" |
| 366 | try: |
| 367 | args = parse_args() |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 368 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 369 | # The input can be either an XML manifest or an APK, they are parsed and |
| 370 | # processed in different ways. |
| 371 | is_apk = args.input.endswith('.apk') |
| 372 | if is_apk: |
| 373 | aapt = args.aapt if args.aapt is not None else 'aapt' |
| 374 | manifest = subprocess.check_output( |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 375 | [aapt, 'dump', 'badging', args.input]).decode('utf-8') |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 376 | else: |
| 377 | manifest = minidom.parse(args.input) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 378 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 379 | if args.enforce_uses_libraries: |
| 380 | # Load dexpreopt.config files and build a mapping from module |
| 381 | # names to library names. This is necessary because build system |
| 382 | # addresses libraries by their module name (`uses_libs`, |
| 383 | # `optional_uses_libs`, `LOCAL_USES_LIBRARIES`, |
| 384 | # `LOCAL_OPTIONAL_LIBRARY_NAMES` all contain module names), while |
| 385 | # the manifest addresses libraries by their name. |
Ulya Trafimovich | 24abbe8 | 2022-04-28 12:50:47 +0100 | [diff] [blame^] | 386 | required, optional = load_dexpreopt_configs(args) |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 387 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 388 | # Check if the <uses-library> lists in the build system agree with |
| 389 | # those in the manifest. Raise an exception on mismatch, unless the |
| 390 | # script was passed a special parameter to suppress exceptions. |
| 391 | errmsg = enforce_uses_libraries(manifest, required, optional, |
| 392 | args.enforce_uses_libraries_relax, |
| 393 | is_apk, args.input) |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 394 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 395 | # Create a status file that is empty on success, or contains an |
| 396 | # error message on failure. When exceptions are suppressed, |
| 397 | # dexpreopt command command will check file size to determine if |
| 398 | # the check has failed. |
| 399 | if args.enforce_uses_libraries_status: |
| 400 | with open(args.enforce_uses_libraries_status, 'w') as f: |
Spandan Das | 3d5cd4d | 2021-09-20 18:24:56 +0000 | [diff] [blame] | 401 | if errmsg is not None: |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 402 | f.write('%s\n' % errmsg) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 403 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 404 | if args.extract_target_sdk_version: |
| 405 | try: |
| 406 | print(extract_target_sdk_version(manifest, is_apk)) |
| 407 | except: #pylint: disable=bare-except |
| 408 | # Failed; don't crash, return "any" SDK version. This will |
| 409 | # result in dexpreopt not adding any compatibility libraries. |
| 410 | print(10000) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 411 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 412 | if args.output: |
| 413 | # XML output is supposed to be written only when this script is |
| 414 | # invoked with XML input manifest, not with an APK. |
| 415 | if is_apk: |
| 416 | raise RuntimeError('cannot save APK manifest as XML') |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 417 | |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 418 | with open(args.output, 'w') as f: |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 419 | write_xml(f, manifest) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 420 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 421 | # pylint: disable=broad-except |
| 422 | except Exception as err: |
Ulya Trafimovich | b4c19f8 | 2021-11-01 12:57:59 +0000 | [diff] [blame] | 423 | print('%serror:%s ' % (C_RED, C_OFF) + str(err), file=sys.stderr) |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 424 | sys.exit(-1) |
| 425 | |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 426 | |
| 427 | if __name__ == '__main__': |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 428 | main() |