1*333d2b36SAndroid Build Coastguard Worker// Copyright 2020 Google Inc. All rights reserved. 2*333d2b36SAndroid Build Coastguard Worker// 3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*333d2b36SAndroid Build Coastguard Worker// 7*333d2b36SAndroid Build Coastguard Worker// http://www.apache.org/licenses/LICENSE-2.0 8*333d2b36SAndroid Build Coastguard Worker// 9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*333d2b36SAndroid Build Coastguard Worker// limitations under the License. 14*333d2b36SAndroid Build Coastguard Worker 15*333d2b36SAndroid Build Coastguard Workerpackage dexpreopt 16*333d2b36SAndroid Build Coastguard Worker 17*333d2b36SAndroid Build Coastguard Workerimport ( 18*333d2b36SAndroid Build Coastguard Worker "encoding/json" 19*333d2b36SAndroid Build Coastguard Worker "fmt" 20*333d2b36SAndroid Build Coastguard Worker "strconv" 21*333d2b36SAndroid Build Coastguard Worker 22*333d2b36SAndroid Build Coastguard Worker "android/soong/android" 23*333d2b36SAndroid Build Coastguard Worker 24*333d2b36SAndroid Build Coastguard Worker "github.com/google/blueprint/proptools" 25*333d2b36SAndroid Build Coastguard Worker) 26*333d2b36SAndroid Build Coastguard Worker 27*333d2b36SAndroid Build Coastguard Worker// This comment describes the following: 28*333d2b36SAndroid Build Coastguard Worker// 1. the concept of class loader context (CLC) and its relation to classpath 29*333d2b36SAndroid Build Coastguard Worker// 2. how PackageManager constructs CLC from shared libraries and their dependencies 30*333d2b36SAndroid Build Coastguard Worker// 3. build-time vs. run-time CLC and why this matters for dexpreopt 31*333d2b36SAndroid Build Coastguard Worker// 4. manifest fixer: a tool that adds missing <uses-library> tags to the manifests 32*333d2b36SAndroid Build Coastguard Worker// 5. build system support for CLC 33*333d2b36SAndroid Build Coastguard Worker// 34*333d2b36SAndroid Build Coastguard Worker// 1. Class loader context 35*333d2b36SAndroid Build Coastguard Worker// ----------------------- 36*333d2b36SAndroid Build Coastguard Worker// 37*333d2b36SAndroid Build Coastguard Worker// Java libraries and apps that have run-time dependency on other libraries should list the used 38*333d2b36SAndroid Build Coastguard Worker// libraries in their manifest (AndroidManifest.xml file). Each used library should be specified in 39*333d2b36SAndroid Build Coastguard Worker// a <uses-library> tag that has the library name and an optional attribute specifying if the 40*333d2b36SAndroid Build Coastguard Worker// library is optional or required. Required libraries are necessary for the library/app to run (it 41*333d2b36SAndroid Build Coastguard Worker// will fail at runtime if the library cannot be loaded), and optional libraries are used only if 42*333d2b36SAndroid Build Coastguard Worker// they are present (if not, the library/app can run without them). 43*333d2b36SAndroid Build Coastguard Worker// 44*333d2b36SAndroid Build Coastguard Worker// The libraries listed in <uses-library> tags are in the classpath of a library/app. 45*333d2b36SAndroid Build Coastguard Worker// 46*333d2b36SAndroid Build Coastguard Worker// Besides libraries, an app may also use another APK (for example in the case of split APKs), or 47*333d2b36SAndroid Build Coastguard Worker// anything that gets added by the app dynamically. In general, it is impossible to know at build 48*333d2b36SAndroid Build Coastguard Worker// time what the app may use at runtime. In the build system we focus on the known part: libraries. 49*333d2b36SAndroid Build Coastguard Worker// 50*333d2b36SAndroid Build Coastguard Worker// Class loader context (CLC) is a tree-like structure that describes class loader hierarchy. The 51*333d2b36SAndroid Build Coastguard Worker// build system uses CLC in a more narrow sense: it is a tree of libraries that represents 52*333d2b36SAndroid Build Coastguard Worker// transitive closure of all <uses-library> dependencies of a library/app. The top-level elements of 53*333d2b36SAndroid Build Coastguard Worker// a CLC are the direct <uses-library> dependencies specified in the manifest (aka. classpath). Each 54*333d2b36SAndroid Build Coastguard Worker// node of a CLC tree is a <uses-library> which may have its own <uses-library> sub-nodes. 55*333d2b36SAndroid Build Coastguard Worker// 56*333d2b36SAndroid Build Coastguard Worker// Because <uses-library> dependencies are, in general, a graph and not necessarily a tree, CLC may 57*333d2b36SAndroid Build Coastguard Worker// contain subtrees for the same library multiple times. In other words, CLC is the dependency graph 58*333d2b36SAndroid Build Coastguard Worker// "unfolded" to a tree. The duplication is only on a logical level, and the actual underlying class 59*333d2b36SAndroid Build Coastguard Worker// loaders are not duplicated (at runtime there is a single class loader instance for each library). 60*333d2b36SAndroid Build Coastguard Worker// 61*333d2b36SAndroid Build Coastguard Worker// Example: A has <uses-library> tags B, C and D; C has <uses-library tags> B and D; 62*333d2b36SAndroid Build Coastguard Worker// 63*333d2b36SAndroid Build Coastguard Worker// D has <uses-library> E; B and E have no <uses-library> dependencies. The CLC is: 64*333d2b36SAndroid Build Coastguard Worker// A 65*333d2b36SAndroid Build Coastguard Worker// ├── B 66*333d2b36SAndroid Build Coastguard Worker// ├── C 67*333d2b36SAndroid Build Coastguard Worker// │ ├── B 68*333d2b36SAndroid Build Coastguard Worker// │ └── D 69*333d2b36SAndroid Build Coastguard Worker// │ └── E 70*333d2b36SAndroid Build Coastguard Worker// └── D 71*333d2b36SAndroid Build Coastguard Worker// └── E 72*333d2b36SAndroid Build Coastguard Worker// 73*333d2b36SAndroid Build Coastguard Worker// CLC defines the lookup order of libraries when resolving Java classes used by the library/app. 74*333d2b36SAndroid Build Coastguard Worker// The lookup order is important because libraries may contain duplicate classes, and the class is 75*333d2b36SAndroid Build Coastguard Worker// resolved to the first match. 76*333d2b36SAndroid Build Coastguard Worker// 77*333d2b36SAndroid Build Coastguard Worker// 2. PackageManager and "shared" libraries 78*333d2b36SAndroid Build Coastguard Worker// ---------------------------------------- 79*333d2b36SAndroid Build Coastguard Worker// 80*333d2b36SAndroid Build Coastguard Worker// In order to load an APK at runtime, PackageManager (in frameworks/base) creates a CLC. It adds 81*333d2b36SAndroid Build Coastguard Worker// the libraries listed in the <uses-library> tags in the app's manifest as top-level CLC elements. 82*333d2b36SAndroid Build Coastguard Worker// For each of the used libraries PackageManager gets all its <uses-library> dependencies (specified 83*333d2b36SAndroid Build Coastguard Worker// as tags in the manifest of that library) and adds a nested CLC for each dependency. This process 84*333d2b36SAndroid Build Coastguard Worker// continues recursively until all leaf nodes of the constructed CLC tree are libraries that have no 85*333d2b36SAndroid Build Coastguard Worker// <uses-library> dependencies. 86*333d2b36SAndroid Build Coastguard Worker// 87*333d2b36SAndroid Build Coastguard Worker// PackageManager is aware only of "shared" libraries. The definition of "shared" here differs from 88*333d2b36SAndroid Build Coastguard Worker// its usual meaning (as in shared vs. static). In Android, Java "shared" libraries are those listed 89*333d2b36SAndroid Build Coastguard Worker// in /system/etc/permissions/platform.xml file. This file is installed on device. Each entry in it 90*333d2b36SAndroid Build Coastguard Worker// contains the name of a "shared" library, a path to its DEX jar file and a list of dependencies 91*333d2b36SAndroid Build Coastguard Worker// (other "shared" libraries that this one uses at runtime and specifies them in <uses-library> tags 92*333d2b36SAndroid Build Coastguard Worker// in its manifest). 93*333d2b36SAndroid Build Coastguard Worker// 94*333d2b36SAndroid Build Coastguard Worker// In other words, there are two sources of information that allow PackageManager to construct CLC 95*333d2b36SAndroid Build Coastguard Worker// at runtime: <uses-library> tags in the manifests and "shared" library dependencies in 96*333d2b36SAndroid Build Coastguard Worker// /system/etc/permissions/platform.xml. 97*333d2b36SAndroid Build Coastguard Worker// 98*333d2b36SAndroid Build Coastguard Worker// 3. Build-time and run-time CLC and dexpreopt 99*333d2b36SAndroid Build Coastguard Worker// -------------------------------------------- 100*333d2b36SAndroid Build Coastguard Worker// 101*333d2b36SAndroid Build Coastguard Worker// CLC is needed not only when loading a library/app, but also when compiling it. Compilation may 102*333d2b36SAndroid Build Coastguard Worker// happen either on device (known as "dexopt") or during the build (known as "dexpreopt"). Since 103*333d2b36SAndroid Build Coastguard Worker// dexopt takes place on device, it has the same information as PackageManager (manifests and 104*333d2b36SAndroid Build Coastguard Worker// shared library dependencies). Dexpreopt, on the other hand, takes place on host and in a totally 105*333d2b36SAndroid Build Coastguard Worker// different environment, and it has to get the same information from the build system (see the 106*333d2b36SAndroid Build Coastguard Worker// section about build system support below). 107*333d2b36SAndroid Build Coastguard Worker// 108*333d2b36SAndroid Build Coastguard Worker// Thus, the build-time CLC used by dexpreopt and the run-time CLC used by PackageManager are 109*333d2b36SAndroid Build Coastguard Worker// the same thing, but computed in two different ways. 110*333d2b36SAndroid Build Coastguard Worker// 111*333d2b36SAndroid Build Coastguard Worker// It is important that build-time and run-time CLCs coincide, otherwise the AOT-compiled code 112*333d2b36SAndroid Build Coastguard Worker// created by dexpreopt will be rejected. In order to check the equality of build-time and 113*333d2b36SAndroid Build Coastguard Worker// run-time CLCs, the dex2oat compiler records build-time CLC in the *.odex files (in the 114*333d2b36SAndroid Build Coastguard Worker// "classpath" field of the OAT file header). To find the stored CLC, use the following command: 115*333d2b36SAndroid Build Coastguard Worker// `oatdump --oat-file=<FILE> | grep '^classpath = '`. 116*333d2b36SAndroid Build Coastguard Worker// 117*333d2b36SAndroid Build Coastguard Worker// Mismatch between build-time and run-time CLC is reported in logcat during boot (search with 118*333d2b36SAndroid Build Coastguard Worker// `logcat | grep -E 'ClassLoaderContext [a-z ]+ mismatch'`. Mismatch is bad for performance, as it 119*333d2b36SAndroid Build Coastguard Worker// forces the library/app to either be dexopted, or to run without any optimizations (e.g. the app's 120*333d2b36SAndroid Build Coastguard Worker// code may need to be extracted in memory from the APK, a very expensive operation). 121*333d2b36SAndroid Build Coastguard Worker// 122*333d2b36SAndroid Build Coastguard Worker// A <uses-library> can be either optional or required. From dexpreopt standpoint, required library 123*333d2b36SAndroid Build Coastguard Worker// must be present at build time (its absence is a build error). An optional library may be either 124*333d2b36SAndroid Build Coastguard Worker// present or absent at build time: if present, it will be added to the CLC, passed to dex2oat and 125*333d2b36SAndroid Build Coastguard Worker// recorded in the *.odex file; otherwise, if the library is absent, it will be skipped and not 126*333d2b36SAndroid Build Coastguard Worker// added to CLC. If there is a mismatch between built-time and run-time status (optional library is 127*333d2b36SAndroid Build Coastguard Worker// present in one case, but not the other), then the build-time and run-time CLCs won't match and 128*333d2b36SAndroid Build Coastguard Worker// the compiled code will be rejected. It is unknown at build time if the library will be present at 129*333d2b36SAndroid Build Coastguard Worker// runtime, therefore either including or excluding it may cause CLC mismatch. 130*333d2b36SAndroid Build Coastguard Worker// 131*333d2b36SAndroid Build Coastguard Worker// 4. Manifest fixer 132*333d2b36SAndroid Build Coastguard Worker// ----------------- 133*333d2b36SAndroid Build Coastguard Worker// 134*333d2b36SAndroid Build Coastguard Worker// Sometimes <uses-library> tags are missing from the source manifest of a library/app. This may 135*333d2b36SAndroid Build Coastguard Worker// happen for example if one of the transitive dependencies of the library/app starts using another 136*333d2b36SAndroid Build Coastguard Worker// <uses-library>, and the library/app's manifest isn't updated to include it. 137*333d2b36SAndroid Build Coastguard Worker// 138*333d2b36SAndroid Build Coastguard Worker// Soong can compute some of the missing <uses-library> tags for a given library/app automatically 139*333d2b36SAndroid Build Coastguard Worker// as SDK libraries in the transitive dependency closure of the library/app. The closure is needed 140*333d2b36SAndroid Build Coastguard Worker// because a library/app may depend on a static library that may in turn depend on an SDK library, 141*333d2b36SAndroid Build Coastguard Worker// (possibly transitively via another library). 142*333d2b36SAndroid Build Coastguard Worker// 143*333d2b36SAndroid Build Coastguard Worker// Not all <uses-library> tags can be computed in this way, because some of the <uses-library> 144*333d2b36SAndroid Build Coastguard Worker// dependencies are not SDK libraries, or they are not reachable via transitive dependency closure. 145*333d2b36SAndroid Build Coastguard Worker// But when possible, allowing Soong to calculate the manifest entries is less prone to errors and 146*333d2b36SAndroid Build Coastguard Worker// simplifies maintenance. For example, consider a situation when many apps use some static library 147*333d2b36SAndroid Build Coastguard Worker// that adds a new <uses-library> dependency -- all the apps will have to be updated. That is 148*333d2b36SAndroid Build Coastguard Worker// difficult to maintain. 149*333d2b36SAndroid Build Coastguard Worker// 150*333d2b36SAndroid Build Coastguard Worker// Soong computes the libraries that need to be in the manifest as the top-level libraries in CLC. 151*333d2b36SAndroid Build Coastguard Worker// These libraries are passed to the manifest_fixer. 152*333d2b36SAndroid Build Coastguard Worker// 153*333d2b36SAndroid Build Coastguard Worker// All libraries added to the manifest should be "shared" libraries, so that PackageManager can look 154*333d2b36SAndroid Build Coastguard Worker// up their dependencies and reconstruct the nested subcontexts at runtime. There is no build check 155*333d2b36SAndroid Build Coastguard Worker// to ensure this, it is an assumption. 156*333d2b36SAndroid Build Coastguard Worker// 157*333d2b36SAndroid Build Coastguard Worker// 5. Build system support 158*333d2b36SAndroid Build Coastguard Worker// ----------------------- 159*333d2b36SAndroid Build Coastguard Worker// 160*333d2b36SAndroid Build Coastguard Worker// In order to construct CLC for dexpreopt and manifest_fixer, the build system needs to know all 161*333d2b36SAndroid Build Coastguard Worker// <uses-library> dependencies of the dexpreopted library/app (including transitive dependencies). 162*333d2b36SAndroid Build Coastguard Worker// For each <uses-librarry> dependency it needs to know the following information: 163*333d2b36SAndroid Build Coastguard Worker// 164*333d2b36SAndroid Build Coastguard Worker// - the real name of the <uses-library> (it may be different from the module name) 165*333d2b36SAndroid Build Coastguard Worker// - build-time (on host) and run-time (on device) paths to the DEX jar file of the library 166*333d2b36SAndroid Build Coastguard Worker// - whether this library is optional or required 167*333d2b36SAndroid Build Coastguard Worker// - all <uses-library> dependencies 168*333d2b36SAndroid Build Coastguard Worker// 169*333d2b36SAndroid Build Coastguard Worker// Since the build system doesn't have access to the manifest contents (it cannot read manifests at 170*333d2b36SAndroid Build Coastguard Worker// the time of build rule generation), it is necessary to copy this information to the Android.bp 171*333d2b36SAndroid Build Coastguard Worker// and Android.mk files. For blueprints, the relevant properties are `uses_libs` and 172*333d2b36SAndroid Build Coastguard Worker// `optional_uses_libs`. For makefiles, relevant variables are `LOCAL_USES_LIBRARIES` and 173*333d2b36SAndroid Build Coastguard Worker// `LOCAL_OPTIONAL_USES_LIBRARIES`. It is preferable to avoid specifying these properties explicilty 174*333d2b36SAndroid Build Coastguard Worker// when they can be computed automatically by Soong (as the transitive closure of SDK library 175*333d2b36SAndroid Build Coastguard Worker// dependencies). 176*333d2b36SAndroid Build Coastguard Worker// 177*333d2b36SAndroid Build Coastguard Worker// Some of the Java libraries that are used as <uses-library> are not SDK libraries (they are 178*333d2b36SAndroid Build Coastguard Worker// defined as `java_library` rather than `java_sdk_library` in the Android.bp files). In order for 179*333d2b36SAndroid Build Coastguard Worker// the build system to handle them automatically like SDK libraries, it is possible to set a 180*333d2b36SAndroid Build Coastguard Worker// property `provides_uses_lib` or variable `LOCAL_PROVIDES_USES_LIBRARY` on the blueprint/makefile 181*333d2b36SAndroid Build Coastguard Worker// module of such library. This property can also be used to specify real library name in cases 182*333d2b36SAndroid Build Coastguard Worker// when it differs from the module name. 183*333d2b36SAndroid Build Coastguard Worker// 184*333d2b36SAndroid Build Coastguard Worker// Because the information from the manifests has to be duplicated in the Android.bp/Android.mk 185*333d2b36SAndroid Build Coastguard Worker// files, there is a danger that it may get out of sync. To guard against that, the build system 186*333d2b36SAndroid Build Coastguard Worker// generates a rule that checks the metadata in the build files against the contents of a manifest 187*333d2b36SAndroid Build Coastguard Worker// (verify_uses_libraries). The manifest can be available as a source file, or as part of a prebuilt 188*333d2b36SAndroid Build Coastguard Worker// APK. Note that reading the manifests at the Ninja stage of the build is fine, unlike the build 189*333d2b36SAndroid Build Coastguard Worker// rule generation phase. 190*333d2b36SAndroid Build Coastguard Worker// 191*333d2b36SAndroid Build Coastguard Worker// ClassLoaderContext is a structure that represents CLC. 192*333d2b36SAndroid Build Coastguard Workertype ClassLoaderContext struct { 193*333d2b36SAndroid Build Coastguard Worker // The name of the library. 194*333d2b36SAndroid Build Coastguard Worker Name string 195*333d2b36SAndroid Build Coastguard Worker 196*333d2b36SAndroid Build Coastguard Worker // If the library is optional or required. 197*333d2b36SAndroid Build Coastguard Worker Optional bool 198*333d2b36SAndroid Build Coastguard Worker 199*333d2b36SAndroid Build Coastguard Worker // On-host build path to the library dex file (used in dex2oat argument --class-loader-context). 200*333d2b36SAndroid Build Coastguard Worker Host android.Path 201*333d2b36SAndroid Build Coastguard Worker 202*333d2b36SAndroid Build Coastguard Worker // On-device install path (used in dex2oat argument --stored-class-loader-context). 203*333d2b36SAndroid Build Coastguard Worker Device string 204*333d2b36SAndroid Build Coastguard Worker 205*333d2b36SAndroid Build Coastguard Worker // Nested sub-CLC for dependencies. 206*333d2b36SAndroid Build Coastguard Worker Subcontexts []*ClassLoaderContext 207*333d2b36SAndroid Build Coastguard Worker} 208*333d2b36SAndroid Build Coastguard Worker 209*333d2b36SAndroid Build Coastguard Worker// excludeLibs excludes the libraries from this ClassLoaderContext. 210*333d2b36SAndroid Build Coastguard Worker// 211*333d2b36SAndroid Build Coastguard Worker// This treats the supplied context as being immutable (as it may come from a dependency). So, it 212*333d2b36SAndroid Build Coastguard Worker// implements copy-on-exclusion logic. That means that if any of the excluded libraries are used 213*333d2b36SAndroid Build Coastguard Worker// within this context then this will return a deep copy of this without those libraries. 214*333d2b36SAndroid Build Coastguard Worker// 215*333d2b36SAndroid Build Coastguard Worker// If this ClassLoaderContext matches one of the libraries to exclude then this returns (nil, true) 216*333d2b36SAndroid Build Coastguard Worker// to indicate that this context should be excluded from the containing list. 217*333d2b36SAndroid Build Coastguard Worker// 218*333d2b36SAndroid Build Coastguard Worker// If any of this ClassLoaderContext's Subcontexts reference the excluded libraries then this 219*333d2b36SAndroid Build Coastguard Worker// returns a pointer to a copy of this without the excluded libraries and true to indicate that this 220*333d2b36SAndroid Build Coastguard Worker// was copied. 221*333d2b36SAndroid Build Coastguard Worker// 222*333d2b36SAndroid Build Coastguard Worker// Otherwise, this returns a pointer to this and false to indicate that this was not copied. 223*333d2b36SAndroid Build Coastguard Workerfunc (c *ClassLoaderContext) excludeLibs(excludedLibs []string) (*ClassLoaderContext, bool) { 224*333d2b36SAndroid Build Coastguard Worker if android.InList(c.Name, excludedLibs) { 225*333d2b36SAndroid Build Coastguard Worker return nil, true 226*333d2b36SAndroid Build Coastguard Worker } 227*333d2b36SAndroid Build Coastguard Worker 228*333d2b36SAndroid Build Coastguard Worker if excludedList, modified := excludeLibsFromCLCList(c.Subcontexts, excludedLibs); modified { 229*333d2b36SAndroid Build Coastguard Worker clcCopy := *c 230*333d2b36SAndroid Build Coastguard Worker clcCopy.Subcontexts = excludedList 231*333d2b36SAndroid Build Coastguard Worker return &clcCopy, true 232*333d2b36SAndroid Build Coastguard Worker } 233*333d2b36SAndroid Build Coastguard Worker 234*333d2b36SAndroid Build Coastguard Worker return c, false 235*333d2b36SAndroid Build Coastguard Worker} 236*333d2b36SAndroid Build Coastguard Worker 237*333d2b36SAndroid Build Coastguard Worker// ClassLoaderContextMap is a map from SDK version to CLC. There is a special entry with key 238*333d2b36SAndroid Build Coastguard Worker// AnySdkVersion that stores unconditional CLC that is added regardless of the target SDK version. 239*333d2b36SAndroid Build Coastguard Worker// 240*333d2b36SAndroid Build Coastguard Worker// Conditional CLC is for compatibility libraries which didn't exist prior to a certain SDK version 241*333d2b36SAndroid Build Coastguard Worker// (say, N), but classes in them were in the bootclasspath jars, etc., and in version N they have 242*333d2b36SAndroid Build Coastguard Worker// been separated into a standalone <uses-library>. Compatibility libraries should only be in the 243*333d2b36SAndroid Build Coastguard Worker// CLC if the library/app that uses them has `targetSdkVersion` less than N in the manifest. 244*333d2b36SAndroid Build Coastguard Worker// 245*333d2b36SAndroid Build Coastguard Worker// Currently only apps (but not libraries) use conditional CLC. 246*333d2b36SAndroid Build Coastguard Worker// 247*333d2b36SAndroid Build Coastguard Worker// Target SDK version information is unavailable to the build system at rule generation time, so 248*333d2b36SAndroid Build Coastguard Worker// the build system doesn't know whether conditional CLC is needed for a given app or not. So it 249*333d2b36SAndroid Build Coastguard Worker// generates a build rule that includes conditional CLC for all versions, extracts the target SDK 250*333d2b36SAndroid Build Coastguard Worker// version from the manifest, and filters the CLCs based on that version. Exact final CLC that is 251*333d2b36SAndroid Build Coastguard Worker// passed to dex2oat is unknown to the build system, and gets known only at Ninja stage. 252*333d2b36SAndroid Build Coastguard Workertype ClassLoaderContextMap map[int][]*ClassLoaderContext 253*333d2b36SAndroid Build Coastguard Worker 254*333d2b36SAndroid Build Coastguard Worker// Compatibility libraries. Some are optional, and some are required: this is the default that 255*333d2b36SAndroid Build Coastguard Worker// affects how they are handled by the Soong logic that automatically adds implicit SDK libraries 256*333d2b36SAndroid Build Coastguard Worker// to the manifest_fixer, but an explicit `uses_libs`/`optional_uses_libs` can override this. 257*333d2b36SAndroid Build Coastguard Workervar OrgApacheHttpLegacy = "org.apache.http.legacy" 258*333d2b36SAndroid Build Coastguard Workervar AndroidTestBase = "android.test.base" 259*333d2b36SAndroid Build Coastguard Workervar AndroidTestMock = "android.test.mock" 260*333d2b36SAndroid Build Coastguard Workervar AndroidHidlBase = "android.hidl.base-V1.0-java" 261*333d2b36SAndroid Build Coastguard Workervar AndroidHidlManager = "android.hidl.manager-V1.0-java" 262*333d2b36SAndroid Build Coastguard Worker 263*333d2b36SAndroid Build Coastguard Worker// Compatibility libraries grouped by version/optionality (for convenience, to avoid repeating the 264*333d2b36SAndroid Build Coastguard Worker// same lists in multiple places). 265*333d2b36SAndroid Build Coastguard Workervar OptionalCompatUsesLibs28 = []string{ 266*333d2b36SAndroid Build Coastguard Worker OrgApacheHttpLegacy, 267*333d2b36SAndroid Build Coastguard Worker} 268*333d2b36SAndroid Build Coastguard Workervar OptionalCompatUsesLibs30 = []string{ 269*333d2b36SAndroid Build Coastguard Worker AndroidTestBase, 270*333d2b36SAndroid Build Coastguard Worker AndroidTestMock, 271*333d2b36SAndroid Build Coastguard Worker} 272*333d2b36SAndroid Build Coastguard Workervar CompatUsesLibs29 = []string{ 273*333d2b36SAndroid Build Coastguard Worker AndroidHidlManager, 274*333d2b36SAndroid Build Coastguard Worker AndroidHidlBase, 275*333d2b36SAndroid Build Coastguard Worker} 276*333d2b36SAndroid Build Coastguard Workervar OptionalCompatUsesLibs = append(android.CopyOf(OptionalCompatUsesLibs28), OptionalCompatUsesLibs30...) 277*333d2b36SAndroid Build Coastguard Workervar CompatUsesLibs = android.CopyOf(CompatUsesLibs29) 278*333d2b36SAndroid Build Coastguard Worker 279*333d2b36SAndroid Build Coastguard Workerconst UnknownInstallLibraryPath = "error" 280*333d2b36SAndroid Build Coastguard Worker 281*333d2b36SAndroid Build Coastguard Worker// AnySdkVersion means that the class loader context is needed regardless of the targetSdkVersion 282*333d2b36SAndroid Build Coastguard Worker// of the app. The numeric value affects the key order in the map and, as a result, the order of 283*333d2b36SAndroid Build Coastguard Worker// arguments passed to construct_context.py (high value means that the unconditional context goes 284*333d2b36SAndroid Build Coastguard Worker// last). We use the converntional "current" SDK level (10000), but any big number would do as well. 285*333d2b36SAndroid Build Coastguard Workerconst AnySdkVersion int = android.FutureApiLevelInt 286*333d2b36SAndroid Build Coastguard Worker 287*333d2b36SAndroid Build Coastguard Worker// Add class loader context for the given library to the map entry for the given SDK version. 288*333d2b36SAndroid Build Coastguard Workerfunc (clcMap ClassLoaderContextMap) addContext(ctx android.ModuleInstallPathContext, sdkVer int, lib string, 289*333d2b36SAndroid Build Coastguard Worker optional bool, hostPath, installPath android.Path, nestedClcMap ClassLoaderContextMap) error { 290*333d2b36SAndroid Build Coastguard Worker 291*333d2b36SAndroid Build Coastguard Worker // For prebuilts, library should have the same name as the source module. 292*333d2b36SAndroid Build Coastguard Worker lib = android.RemoveOptionalPrebuiltPrefix(lib) 293*333d2b36SAndroid Build Coastguard Worker 294*333d2b36SAndroid Build Coastguard Worker devicePath := UnknownInstallLibraryPath 295*333d2b36SAndroid Build Coastguard Worker if installPath == nil { 296*333d2b36SAndroid Build Coastguard Worker if android.InList(lib, CompatUsesLibs) || android.InList(lib, OptionalCompatUsesLibs) { 297*333d2b36SAndroid Build Coastguard Worker // Assume that compatibility libraries are installed in /system/framework. 298*333d2b36SAndroid Build Coastguard Worker installPath = android.PathForModuleInstall(ctx, "framework", lib+".jar") 299*333d2b36SAndroid Build Coastguard Worker } else { 300*333d2b36SAndroid Build Coastguard Worker // For some stub libraries the only known thing is the name of their implementation 301*333d2b36SAndroid Build Coastguard Worker // library, but the library itself is unavailable (missing or part of a prebuilt). In 302*333d2b36SAndroid Build Coastguard Worker // such cases we still need to add the library to <uses-library> tags in the manifest, 303*333d2b36SAndroid Build Coastguard Worker // but we cannot use it for dexpreopt. 304*333d2b36SAndroid Build Coastguard Worker } 305*333d2b36SAndroid Build Coastguard Worker } 306*333d2b36SAndroid Build Coastguard Worker if installPath != nil { 307*333d2b36SAndroid Build Coastguard Worker devicePath = android.InstallPathToOnDevicePath(ctx, installPath.(android.InstallPath)) 308*333d2b36SAndroid Build Coastguard Worker } 309*333d2b36SAndroid Build Coastguard Worker 310*333d2b36SAndroid Build Coastguard Worker // Nested class loader context shouldn't have conditional part (it is allowed only at the top level). 311*333d2b36SAndroid Build Coastguard Worker for ver, _ := range nestedClcMap { 312*333d2b36SAndroid Build Coastguard Worker if ver != AnySdkVersion { 313*333d2b36SAndroid Build Coastguard Worker _, clcPaths := ComputeClassLoaderContextDependencies(nestedClcMap) 314*333d2b36SAndroid Build Coastguard Worker return fmt.Errorf("nested class loader context shouldn't have conditional part: %+v", clcPaths) 315*333d2b36SAndroid Build Coastguard Worker } 316*333d2b36SAndroid Build Coastguard Worker } 317*333d2b36SAndroid Build Coastguard Worker subcontexts := nestedClcMap[AnySdkVersion] 318*333d2b36SAndroid Build Coastguard Worker 319*333d2b36SAndroid Build Coastguard Worker // Check if the library with this name is already present in unconditional top-level CLC. 320*333d2b36SAndroid Build Coastguard Worker for _, clc := range clcMap[sdkVer] { 321*333d2b36SAndroid Build Coastguard Worker if clc.Name != lib { 322*333d2b36SAndroid Build Coastguard Worker // Ok, a different library. 323*333d2b36SAndroid Build Coastguard Worker } else if clc.Host == hostPath && clc.Device == devicePath { 324*333d2b36SAndroid Build Coastguard Worker // Ok, the same library with the same paths. Don't re-add it, but don't raise an error 325*333d2b36SAndroid Build Coastguard Worker // either, as the same library may be reachable via different transitional dependencies. 326*333d2b36SAndroid Build Coastguard Worker clc.Optional = clc.Optional && optional 327*333d2b36SAndroid Build Coastguard Worker return nil 328*333d2b36SAndroid Build Coastguard Worker } else { 329*333d2b36SAndroid Build Coastguard Worker // Fail, as someone is trying to add the same library with different paths. This likely 330*333d2b36SAndroid Build Coastguard Worker // indicates an error somewhere else, like trying to add a stub library. 331*333d2b36SAndroid Build Coastguard Worker return fmt.Errorf("a <uses-library> named %q is already in class loader context,"+ 332*333d2b36SAndroid Build Coastguard Worker "but the library paths are different:\t\n", lib) 333*333d2b36SAndroid Build Coastguard Worker } 334*333d2b36SAndroid Build Coastguard Worker } 335*333d2b36SAndroid Build Coastguard Worker 336*333d2b36SAndroid Build Coastguard Worker clcMap[sdkVer] = append(clcMap[sdkVer], &ClassLoaderContext{ 337*333d2b36SAndroid Build Coastguard Worker Name: lib, 338*333d2b36SAndroid Build Coastguard Worker Optional: optional, 339*333d2b36SAndroid Build Coastguard Worker Host: hostPath, 340*333d2b36SAndroid Build Coastguard Worker Device: devicePath, 341*333d2b36SAndroid Build Coastguard Worker Subcontexts: subcontexts, 342*333d2b36SAndroid Build Coastguard Worker }) 343*333d2b36SAndroid Build Coastguard Worker return nil 344*333d2b36SAndroid Build Coastguard Worker} 345*333d2b36SAndroid Build Coastguard Worker 346*333d2b36SAndroid Build Coastguard Worker// Add class loader context for the given SDK version. Don't fail on unknown build/install paths, as 347*333d2b36SAndroid Build Coastguard Worker// libraries with unknown paths still need to be processed by manifest_fixer (which doesn't care 348*333d2b36SAndroid Build Coastguard Worker// about paths). For the subset of libraries that are used in dexpreopt, their build/install paths 349*333d2b36SAndroid Build Coastguard Worker// are validated later before CLC is used (in validateClassLoaderContext). 350*333d2b36SAndroid Build Coastguard Workerfunc (clcMap ClassLoaderContextMap) AddContext(ctx android.ModuleInstallPathContext, sdkVer int, 351*333d2b36SAndroid Build Coastguard Worker lib string, optional bool, hostPath, installPath android.Path, nestedClcMap ClassLoaderContextMap) { 352*333d2b36SAndroid Build Coastguard Worker 353*333d2b36SAndroid Build Coastguard Worker err := clcMap.addContext(ctx, sdkVer, lib, optional, hostPath, installPath, nestedClcMap) 354*333d2b36SAndroid Build Coastguard Worker if err != nil { 355*333d2b36SAndroid Build Coastguard Worker ctx.ModuleErrorf(err.Error()) 356*333d2b36SAndroid Build Coastguard Worker } 357*333d2b36SAndroid Build Coastguard Worker} 358*333d2b36SAndroid Build Coastguard Worker 359*333d2b36SAndroid Build Coastguard Worker// Merge the other class loader context map into this one, do not override existing entries. 360*333d2b36SAndroid Build Coastguard Worker// The implicitRootLib parameter is the name of the library for which the other class loader 361*333d2b36SAndroid Build Coastguard Worker// context map was constructed. If the implicitRootLib is itself a <uses-library>, it should be 362*333d2b36SAndroid Build Coastguard Worker// already present in the class loader context (with the other context as its subcontext) -- in 363*333d2b36SAndroid Build Coastguard Worker// that case do not re-add the other context. Otherwise add the other context at the top-level. 364*333d2b36SAndroid Build Coastguard Workerfunc (clcMap ClassLoaderContextMap) AddContextMap(otherClcMap ClassLoaderContextMap, implicitRootLib string) { 365*333d2b36SAndroid Build Coastguard Worker if otherClcMap == nil { 366*333d2b36SAndroid Build Coastguard Worker return 367*333d2b36SAndroid Build Coastguard Worker } 368*333d2b36SAndroid Build Coastguard Worker 369*333d2b36SAndroid Build Coastguard Worker // If the implicit root of the merged map is already present as one of top-level subtrees, do 370*333d2b36SAndroid Build Coastguard Worker // not merge it second time. 371*333d2b36SAndroid Build Coastguard Worker for _, clc := range clcMap[AnySdkVersion] { 372*333d2b36SAndroid Build Coastguard Worker if clc.Name == implicitRootLib { 373*333d2b36SAndroid Build Coastguard Worker return 374*333d2b36SAndroid Build Coastguard Worker } 375*333d2b36SAndroid Build Coastguard Worker } 376*333d2b36SAndroid Build Coastguard Worker 377*333d2b36SAndroid Build Coastguard Worker for sdkVer, otherClcs := range otherClcMap { 378*333d2b36SAndroid Build Coastguard Worker for _, otherClc := range otherClcs { 379*333d2b36SAndroid Build Coastguard Worker alreadyHave := false 380*333d2b36SAndroid Build Coastguard Worker for _, clc := range clcMap[sdkVer] { 381*333d2b36SAndroid Build Coastguard Worker if clc.Name == otherClc.Name { 382*333d2b36SAndroid Build Coastguard Worker alreadyHave = true 383*333d2b36SAndroid Build Coastguard Worker break 384*333d2b36SAndroid Build Coastguard Worker } 385*333d2b36SAndroid Build Coastguard Worker } 386*333d2b36SAndroid Build Coastguard Worker if !alreadyHave { 387*333d2b36SAndroid Build Coastguard Worker clcMap[sdkVer] = append(clcMap[sdkVer], otherClc) 388*333d2b36SAndroid Build Coastguard Worker } 389*333d2b36SAndroid Build Coastguard Worker } 390*333d2b36SAndroid Build Coastguard Worker } 391*333d2b36SAndroid Build Coastguard Worker} 392*333d2b36SAndroid Build Coastguard Worker 393*333d2b36SAndroid Build Coastguard Worker// Returns top-level libraries in the CLC (conditional CLC, i.e. compatibility libraries are not 394*333d2b36SAndroid Build Coastguard Worker// included). This is the list of libraries that should be in the <uses-library> tags in the 395*333d2b36SAndroid Build Coastguard Worker// manifest. Some of them may be present in the source manifest, others are added by manifest_fixer. 396*333d2b36SAndroid Build Coastguard Worker// Required and optional libraries are in separate lists. 397*333d2b36SAndroid Build Coastguard Workerfunc (clcMap ClassLoaderContextMap) UsesLibs() (required []string, optional []string) { 398*333d2b36SAndroid Build Coastguard Worker if clcMap != nil { 399*333d2b36SAndroid Build Coastguard Worker clcs := clcMap[AnySdkVersion] 400*333d2b36SAndroid Build Coastguard Worker required = make([]string, 0, len(clcs)) 401*333d2b36SAndroid Build Coastguard Worker optional = make([]string, 0, len(clcs)) 402*333d2b36SAndroid Build Coastguard Worker for _, clc := range clcs { 403*333d2b36SAndroid Build Coastguard Worker if clc.Optional { 404*333d2b36SAndroid Build Coastguard Worker optional = append(optional, clc.Name) 405*333d2b36SAndroid Build Coastguard Worker } else { 406*333d2b36SAndroid Build Coastguard Worker required = append(required, clc.Name) 407*333d2b36SAndroid Build Coastguard Worker } 408*333d2b36SAndroid Build Coastguard Worker } 409*333d2b36SAndroid Build Coastguard Worker } 410*333d2b36SAndroid Build Coastguard Worker return required, optional 411*333d2b36SAndroid Build Coastguard Worker} 412*333d2b36SAndroid Build Coastguard Worker 413*333d2b36SAndroid Build Coastguard Workerfunc (clcMap ClassLoaderContextMap) Dump() string { 414*333d2b36SAndroid Build Coastguard Worker jsonCLC := toJsonClassLoaderContext(clcMap) 415*333d2b36SAndroid Build Coastguard Worker bytes, err := json.MarshalIndent(jsonCLC, "", " ") 416*333d2b36SAndroid Build Coastguard Worker if err != nil { 417*333d2b36SAndroid Build Coastguard Worker panic(err) 418*333d2b36SAndroid Build Coastguard Worker } 419*333d2b36SAndroid Build Coastguard Worker return string(bytes) 420*333d2b36SAndroid Build Coastguard Worker} 421*333d2b36SAndroid Build Coastguard Worker 422*333d2b36SAndroid Build Coastguard Workerfunc (clcMap ClassLoaderContextMap) DumpForFlag() string { 423*333d2b36SAndroid Build Coastguard Worker jsonCLC := toJsonClassLoaderContext(clcMap) 424*333d2b36SAndroid Build Coastguard Worker bytes, err := json.Marshal(jsonCLC) 425*333d2b36SAndroid Build Coastguard Worker if err != nil { 426*333d2b36SAndroid Build Coastguard Worker panic(err) 427*333d2b36SAndroid Build Coastguard Worker } 428*333d2b36SAndroid Build Coastguard Worker return proptools.ShellEscapeIncludingSpaces(string(bytes)) 429*333d2b36SAndroid Build Coastguard Worker} 430*333d2b36SAndroid Build Coastguard Worker 431*333d2b36SAndroid Build Coastguard Worker// excludeLibsFromCLCList excludes the libraries from the ClassLoaderContext in this list. 432*333d2b36SAndroid Build Coastguard Worker// 433*333d2b36SAndroid Build Coastguard Worker// This treats the supplied list as being immutable (as it may come from a dependency). So, it 434*333d2b36SAndroid Build Coastguard Worker// implements copy-on-exclusion logic. That means that if any of the excluded libraries are used 435*333d2b36SAndroid Build Coastguard Worker// within the contexts in the list then this will return a deep copy of the list without those 436*333d2b36SAndroid Build Coastguard Worker// libraries. 437*333d2b36SAndroid Build Coastguard Worker// 438*333d2b36SAndroid Build Coastguard Worker// If any of the ClassLoaderContext in the list reference the excluded libraries then this returns a 439*333d2b36SAndroid Build Coastguard Worker// copy of this list without the excluded libraries and true to indicate that this was copied. 440*333d2b36SAndroid Build Coastguard Worker// 441*333d2b36SAndroid Build Coastguard Worker// Otherwise, this returns the list and false to indicate that this was not copied. 442*333d2b36SAndroid Build Coastguard Workerfunc excludeLibsFromCLCList(clcList []*ClassLoaderContext, excludedLibs []string) ([]*ClassLoaderContext, bool) { 443*333d2b36SAndroid Build Coastguard Worker modifiedList := false 444*333d2b36SAndroid Build Coastguard Worker copiedList := make([]*ClassLoaderContext, 0, len(clcList)) 445*333d2b36SAndroid Build Coastguard Worker for _, clc := range clcList { 446*333d2b36SAndroid Build Coastguard Worker resultClc, modifiedClc := clc.excludeLibs(excludedLibs) 447*333d2b36SAndroid Build Coastguard Worker if resultClc != nil { 448*333d2b36SAndroid Build Coastguard Worker copiedList = append(copiedList, resultClc) 449*333d2b36SAndroid Build Coastguard Worker } 450*333d2b36SAndroid Build Coastguard Worker modifiedList = modifiedList || modifiedClc 451*333d2b36SAndroid Build Coastguard Worker } 452*333d2b36SAndroid Build Coastguard Worker 453*333d2b36SAndroid Build Coastguard Worker if modifiedList { 454*333d2b36SAndroid Build Coastguard Worker return copiedList, true 455*333d2b36SAndroid Build Coastguard Worker } else { 456*333d2b36SAndroid Build Coastguard Worker return clcList, false 457*333d2b36SAndroid Build Coastguard Worker } 458*333d2b36SAndroid Build Coastguard Worker} 459*333d2b36SAndroid Build Coastguard Worker 460*333d2b36SAndroid Build Coastguard Worker// ExcludeLibs excludes the libraries from the ClassLoaderContextMap. 461*333d2b36SAndroid Build Coastguard Worker// 462*333d2b36SAndroid Build Coastguard Worker// If the list o libraries is empty then this returns the ClassLoaderContextMap. 463*333d2b36SAndroid Build Coastguard Worker// 464*333d2b36SAndroid Build Coastguard Worker// This treats the ClassLoaderContextMap as being immutable (as it may come from a dependency). So, 465*333d2b36SAndroid Build Coastguard Worker// it implements copy-on-exclusion logic. That means that if any of the excluded libraries are used 466*333d2b36SAndroid Build Coastguard Worker// within the contexts in the map then this will return a deep copy of the map without those 467*333d2b36SAndroid Build Coastguard Worker// libraries. 468*333d2b36SAndroid Build Coastguard Worker// 469*333d2b36SAndroid Build Coastguard Worker// Otherwise, this returns the map unchanged. 470*333d2b36SAndroid Build Coastguard Workerfunc (clcMap ClassLoaderContextMap) ExcludeLibs(excludedLibs []string) ClassLoaderContextMap { 471*333d2b36SAndroid Build Coastguard Worker if len(excludedLibs) == 0 { 472*333d2b36SAndroid Build Coastguard Worker return clcMap 473*333d2b36SAndroid Build Coastguard Worker } 474*333d2b36SAndroid Build Coastguard Worker 475*333d2b36SAndroid Build Coastguard Worker excludedClcMap := make(ClassLoaderContextMap) 476*333d2b36SAndroid Build Coastguard Worker modifiedMap := false 477*333d2b36SAndroid Build Coastguard Worker for sdkVersion, clcList := range clcMap { 478*333d2b36SAndroid Build Coastguard Worker excludedList, modifiedList := excludeLibsFromCLCList(clcList, excludedLibs) 479*333d2b36SAndroid Build Coastguard Worker if len(excludedList) != 0 { 480*333d2b36SAndroid Build Coastguard Worker excludedClcMap[sdkVersion] = excludedList 481*333d2b36SAndroid Build Coastguard Worker } 482*333d2b36SAndroid Build Coastguard Worker modifiedMap = modifiedMap || modifiedList 483*333d2b36SAndroid Build Coastguard Worker } 484*333d2b36SAndroid Build Coastguard Worker 485*333d2b36SAndroid Build Coastguard Worker if modifiedMap { 486*333d2b36SAndroid Build Coastguard Worker return excludedClcMap 487*333d2b36SAndroid Build Coastguard Worker } else { 488*333d2b36SAndroid Build Coastguard Worker return clcMap 489*333d2b36SAndroid Build Coastguard Worker } 490*333d2b36SAndroid Build Coastguard Worker} 491*333d2b36SAndroid Build Coastguard Worker 492*333d2b36SAndroid Build Coastguard Worker// Now that the full unconditional context is known, reconstruct conditional context. 493*333d2b36SAndroid Build Coastguard Worker// Apply filters for individual libraries, mirroring what the PackageManager does when it 494*333d2b36SAndroid Build Coastguard Worker// constructs class loader context on device. 495*333d2b36SAndroid Build Coastguard Worker// 496*333d2b36SAndroid Build Coastguard Worker// TODO(b/132357300): remove "android.hidl.manager" and "android.hidl.base" for non-system apps. 497*333d2b36SAndroid Build Coastguard Workerfunc fixClassLoaderContext(clcMap ClassLoaderContextMap) { 498*333d2b36SAndroid Build Coastguard Worker required, optional := clcMap.UsesLibs() 499*333d2b36SAndroid Build Coastguard Worker usesLibs := append(required, optional...) 500*333d2b36SAndroid Build Coastguard Worker 501*333d2b36SAndroid Build Coastguard Worker for sdkVer, clcs := range clcMap { 502*333d2b36SAndroid Build Coastguard Worker if sdkVer == AnySdkVersion { 503*333d2b36SAndroid Build Coastguard Worker continue 504*333d2b36SAndroid Build Coastguard Worker } 505*333d2b36SAndroid Build Coastguard Worker fixedClcs := []*ClassLoaderContext{} 506*333d2b36SAndroid Build Coastguard Worker for _, clc := range clcs { 507*333d2b36SAndroid Build Coastguard Worker if android.InList(clc.Name, usesLibs) { 508*333d2b36SAndroid Build Coastguard Worker // skip compatibility libraries that are already included in unconditional context 509*333d2b36SAndroid Build Coastguard Worker } else if clc.Name == AndroidTestMock && !android.InList("android.test.runner", usesLibs) { 510*333d2b36SAndroid Build Coastguard Worker // android.test.mock is only needed as a compatibility library (in conditional class 511*333d2b36SAndroid Build Coastguard Worker // loader context) if android.test.runner is used, otherwise skip it 512*333d2b36SAndroid Build Coastguard Worker } else { 513*333d2b36SAndroid Build Coastguard Worker fixedClcs = append(fixedClcs, clc) 514*333d2b36SAndroid Build Coastguard Worker } 515*333d2b36SAndroid Build Coastguard Worker clcMap[sdkVer] = fixedClcs 516*333d2b36SAndroid Build Coastguard Worker } 517*333d2b36SAndroid Build Coastguard Worker } 518*333d2b36SAndroid Build Coastguard Worker} 519*333d2b36SAndroid Build Coastguard Worker 520*333d2b36SAndroid Build Coastguard Worker// Return true if all build/install library paths are valid (including recursive subcontexts), 521*333d2b36SAndroid Build Coastguard Worker// otherwise return false. A build path is valid if it's not nil. An install path is valid if it's 522*333d2b36SAndroid Build Coastguard Worker// not equal to a special "error" value. 523*333d2b36SAndroid Build Coastguard Workerfunc validateClassLoaderContext(clcMap ClassLoaderContextMap) (bool, error) { 524*333d2b36SAndroid Build Coastguard Worker for sdkVer, clcs := range clcMap { 525*333d2b36SAndroid Build Coastguard Worker if valid, err := validateClassLoaderContextRec(sdkVer, clcs); !valid || err != nil { 526*333d2b36SAndroid Build Coastguard Worker return valid, err 527*333d2b36SAndroid Build Coastguard Worker } 528*333d2b36SAndroid Build Coastguard Worker } 529*333d2b36SAndroid Build Coastguard Worker return true, nil 530*333d2b36SAndroid Build Coastguard Worker} 531*333d2b36SAndroid Build Coastguard Worker 532*333d2b36SAndroid Build Coastguard Worker// Helper function for validateClassLoaderContext() that handles recursion. 533*333d2b36SAndroid Build Coastguard Workerfunc validateClassLoaderContextRec(sdkVer int, clcs []*ClassLoaderContext) (bool, error) { 534*333d2b36SAndroid Build Coastguard Worker for _, clc := range clcs { 535*333d2b36SAndroid Build Coastguard Worker if clc.Host == nil || clc.Device == UnknownInstallLibraryPath { 536*333d2b36SAndroid Build Coastguard Worker if sdkVer == AnySdkVersion { 537*333d2b36SAndroid Build Coastguard Worker // Return error if dexpreopt doesn't know paths to one of the <uses-library> 538*333d2b36SAndroid Build Coastguard Worker // dependencies. In the future we may need to relax this and just disable dexpreopt. 539*333d2b36SAndroid Build Coastguard Worker if clc.Host == nil { 540*333d2b36SAndroid Build Coastguard Worker return false, fmt.Errorf("invalid build path for <uses-library> \"%s\"", clc.Name) 541*333d2b36SAndroid Build Coastguard Worker } else { 542*333d2b36SAndroid Build Coastguard Worker return false, fmt.Errorf("invalid install path for <uses-library> \"%s\"", clc.Name) 543*333d2b36SAndroid Build Coastguard Worker } 544*333d2b36SAndroid Build Coastguard Worker } else { 545*333d2b36SAndroid Build Coastguard Worker // No error for compatibility libraries, as Soong doesn't know if they are needed 546*333d2b36SAndroid Build Coastguard Worker // (this depends on the targetSdkVersion in the manifest), but the CLC is invalid. 547*333d2b36SAndroid Build Coastguard Worker return false, nil 548*333d2b36SAndroid Build Coastguard Worker } 549*333d2b36SAndroid Build Coastguard Worker } 550*333d2b36SAndroid Build Coastguard Worker if valid, err := validateClassLoaderContextRec(sdkVer, clc.Subcontexts); !valid || err != nil { 551*333d2b36SAndroid Build Coastguard Worker return valid, err 552*333d2b36SAndroid Build Coastguard Worker } 553*333d2b36SAndroid Build Coastguard Worker } 554*333d2b36SAndroid Build Coastguard Worker return true, nil 555*333d2b36SAndroid Build Coastguard Worker} 556*333d2b36SAndroid Build Coastguard Worker 557*333d2b36SAndroid Build Coastguard Worker// Returns a slice of library names and a slice of build paths for all possible dependencies that 558*333d2b36SAndroid Build Coastguard Worker// the class loader context may refer to. 559*333d2b36SAndroid Build Coastguard Worker// Perform a depth-first preorder traversal of the class loader context tree for each SDK version. 560*333d2b36SAndroid Build Coastguard Workerfunc ComputeClassLoaderContextDependencies(clcMap ClassLoaderContextMap) (names []string, paths android.Paths) { 561*333d2b36SAndroid Build Coastguard Worker for _, clcs := range clcMap { 562*333d2b36SAndroid Build Coastguard Worker currentNames, currentPaths := ComputeClassLoaderContextDependenciesRec(clcs) 563*333d2b36SAndroid Build Coastguard Worker names = append(names, currentNames...) 564*333d2b36SAndroid Build Coastguard Worker paths = append(paths, currentPaths...) 565*333d2b36SAndroid Build Coastguard Worker } 566*333d2b36SAndroid Build Coastguard Worker return android.FirstUniqueStrings(names), android.FirstUniquePaths(paths) 567*333d2b36SAndroid Build Coastguard Worker} 568*333d2b36SAndroid Build Coastguard Worker 569*333d2b36SAndroid Build Coastguard Worker// Helper function for ComputeClassLoaderContextDependencies() that handles recursion. 570*333d2b36SAndroid Build Coastguard Workerfunc ComputeClassLoaderContextDependenciesRec(clcs []*ClassLoaderContext) (names []string, paths android.Paths) { 571*333d2b36SAndroid Build Coastguard Worker for _, clc := range clcs { 572*333d2b36SAndroid Build Coastguard Worker subNames, subPaths := ComputeClassLoaderContextDependenciesRec(clc.Subcontexts) 573*333d2b36SAndroid Build Coastguard Worker names = append(names, clc.Name) 574*333d2b36SAndroid Build Coastguard Worker paths = append(paths, clc.Host) 575*333d2b36SAndroid Build Coastguard Worker names = append(names, subNames...) 576*333d2b36SAndroid Build Coastguard Worker paths = append(paths, subPaths...) 577*333d2b36SAndroid Build Coastguard Worker } 578*333d2b36SAndroid Build Coastguard Worker return names, paths 579*333d2b36SAndroid Build Coastguard Worker} 580*333d2b36SAndroid Build Coastguard Worker 581*333d2b36SAndroid Build Coastguard Worker// Class loader contexts that come from Make via JSON dexpreopt.config. JSON CLC representation is 582*333d2b36SAndroid Build Coastguard Worker// the same as Soong representation except that SDK versions and paths are represented with strings. 583*333d2b36SAndroid Build Coastguard Workertype jsonClassLoaderContext struct { 584*333d2b36SAndroid Build Coastguard Worker Name string 585*333d2b36SAndroid Build Coastguard Worker Optional bool 586*333d2b36SAndroid Build Coastguard Worker Host string 587*333d2b36SAndroid Build Coastguard Worker Device string 588*333d2b36SAndroid Build Coastguard Worker Subcontexts []*jsonClassLoaderContext 589*333d2b36SAndroid Build Coastguard Worker} 590*333d2b36SAndroid Build Coastguard Worker 591*333d2b36SAndroid Build Coastguard Worker// A map from SDK version (represented with a JSON string) to JSON CLCs. 592*333d2b36SAndroid Build Coastguard Workertype jsonClassLoaderContextMap map[string][]*jsonClassLoaderContext 593*333d2b36SAndroid Build Coastguard Worker 594*333d2b36SAndroid Build Coastguard Worker// Convert JSON CLC map to Soong represenation. 595*333d2b36SAndroid Build Coastguard Workerfunc fromJsonClassLoaderContext(ctx android.PathContext, jClcMap jsonClassLoaderContextMap) ClassLoaderContextMap { 596*333d2b36SAndroid Build Coastguard Worker clcMap := make(ClassLoaderContextMap) 597*333d2b36SAndroid Build Coastguard Worker for sdkVerStr, clcs := range jClcMap { 598*333d2b36SAndroid Build Coastguard Worker sdkVer, ok := strconv.Atoi(sdkVerStr) 599*333d2b36SAndroid Build Coastguard Worker if ok != nil { 600*333d2b36SAndroid Build Coastguard Worker if sdkVerStr == "any" { 601*333d2b36SAndroid Build Coastguard Worker sdkVer = AnySdkVersion 602*333d2b36SAndroid Build Coastguard Worker } else { 603*333d2b36SAndroid Build Coastguard Worker android.ReportPathErrorf(ctx, "failed to parse SDK version in dexpreopt.config: '%s'", sdkVerStr) 604*333d2b36SAndroid Build Coastguard Worker } 605*333d2b36SAndroid Build Coastguard Worker } 606*333d2b36SAndroid Build Coastguard Worker clcMap[sdkVer] = fromJsonClassLoaderContextRec(ctx, clcs) 607*333d2b36SAndroid Build Coastguard Worker } 608*333d2b36SAndroid Build Coastguard Worker return clcMap 609*333d2b36SAndroid Build Coastguard Worker} 610*333d2b36SAndroid Build Coastguard Worker 611*333d2b36SAndroid Build Coastguard Worker// Recursive helper for fromJsonClassLoaderContext. 612*333d2b36SAndroid Build Coastguard Workerfunc fromJsonClassLoaderContextRec(ctx android.PathContext, jClcs []*jsonClassLoaderContext) []*ClassLoaderContext { 613*333d2b36SAndroid Build Coastguard Worker clcs := make([]*ClassLoaderContext, 0, len(jClcs)) 614*333d2b36SAndroid Build Coastguard Worker for _, clc := range jClcs { 615*333d2b36SAndroid Build Coastguard Worker clcs = append(clcs, &ClassLoaderContext{ 616*333d2b36SAndroid Build Coastguard Worker Name: clc.Name, 617*333d2b36SAndroid Build Coastguard Worker Optional: clc.Optional, 618*333d2b36SAndroid Build Coastguard Worker Host: constructPath(ctx, clc.Host), 619*333d2b36SAndroid Build Coastguard Worker Device: clc.Device, 620*333d2b36SAndroid Build Coastguard Worker Subcontexts: fromJsonClassLoaderContextRec(ctx, clc.Subcontexts), 621*333d2b36SAndroid Build Coastguard Worker }) 622*333d2b36SAndroid Build Coastguard Worker } 623*333d2b36SAndroid Build Coastguard Worker return clcs 624*333d2b36SAndroid Build Coastguard Worker} 625*333d2b36SAndroid Build Coastguard Worker 626*333d2b36SAndroid Build Coastguard Worker// Convert Soong CLC map to JSON representation for Make. 627*333d2b36SAndroid Build Coastguard Workerfunc toJsonClassLoaderContext(clcMap ClassLoaderContextMap) jsonClassLoaderContextMap { 628*333d2b36SAndroid Build Coastguard Worker jClcMap := make(jsonClassLoaderContextMap) 629*333d2b36SAndroid Build Coastguard Worker for sdkVer, clcs := range clcMap { 630*333d2b36SAndroid Build Coastguard Worker sdkVerStr := fmt.Sprintf("%d", sdkVer) 631*333d2b36SAndroid Build Coastguard Worker if sdkVer == AnySdkVersion { 632*333d2b36SAndroid Build Coastguard Worker sdkVerStr = "any" 633*333d2b36SAndroid Build Coastguard Worker } 634*333d2b36SAndroid Build Coastguard Worker jClcMap[sdkVerStr] = toJsonClassLoaderContextRec(clcs) 635*333d2b36SAndroid Build Coastguard Worker } 636*333d2b36SAndroid Build Coastguard Worker return jClcMap 637*333d2b36SAndroid Build Coastguard Worker} 638*333d2b36SAndroid Build Coastguard Worker 639*333d2b36SAndroid Build Coastguard Worker// Recursive helper for toJsonClassLoaderContext. 640*333d2b36SAndroid Build Coastguard Workerfunc toJsonClassLoaderContextRec(clcs []*ClassLoaderContext) []*jsonClassLoaderContext { 641*333d2b36SAndroid Build Coastguard Worker jClcs := make([]*jsonClassLoaderContext, len(clcs)) 642*333d2b36SAndroid Build Coastguard Worker for i, clc := range clcs { 643*333d2b36SAndroid Build Coastguard Worker var host string 644*333d2b36SAndroid Build Coastguard Worker if clc.Host == nil { 645*333d2b36SAndroid Build Coastguard Worker // Defer build failure to when this CLC is actually used. 646*333d2b36SAndroid Build Coastguard Worker host = fmt.Sprintf("implementation-jar-for-%s-is-not-available.jar", clc.Name) 647*333d2b36SAndroid Build Coastguard Worker } else { 648*333d2b36SAndroid Build Coastguard Worker host = clc.Host.String() 649*333d2b36SAndroid Build Coastguard Worker } 650*333d2b36SAndroid Build Coastguard Worker jClcs[i] = &jsonClassLoaderContext{ 651*333d2b36SAndroid Build Coastguard Worker Name: clc.Name, 652*333d2b36SAndroid Build Coastguard Worker Optional: clc.Optional, 653*333d2b36SAndroid Build Coastguard Worker Host: host, 654*333d2b36SAndroid Build Coastguard Worker Device: clc.Device, 655*333d2b36SAndroid Build Coastguard Worker Subcontexts: toJsonClassLoaderContextRec(clc.Subcontexts), 656*333d2b36SAndroid Build Coastguard Worker } 657*333d2b36SAndroid Build Coastguard Worker } 658*333d2b36SAndroid Build Coastguard Worker return jClcs 659*333d2b36SAndroid Build Coastguard Worker} 660