Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 1 | // Copyright (C) 2021 The Android Open Source Project |
| 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 | package java |
| 16 | |
| 17 | import ( |
Paul Duffin | 7487a7a | 2021-05-19 09:36:09 +0100 | [diff] [blame] | 18 | "fmt" |
Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 19 | "strings" |
| 20 | |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 21 | "android/soong/android" |
Paul Duffin | 9b381ef | 2021-04-08 23:01:37 +0100 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Paul Duffin | 6237092 | 2021-05-23 16:55:37 +0100 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | // Contains support for processing hiddenAPI in a modular fashion. |
| 27 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 28 | // HiddenAPIScope encapsulates all the information that the hidden API processing needs about API |
| 29 | // scopes, i.e. what is called android.SdkKind and apiScope. It does not just use those as they do |
| 30 | // not provide the information needed by hidden API processing. |
| 31 | type HiddenAPIScope struct { |
| 32 | // The name of the scope, used for debug purposes. |
| 33 | name string |
| 34 | |
| 35 | // The corresponding android.SdkKind, used for retrieving paths from java_sdk_library* modules. |
| 36 | sdkKind android.SdkKind |
| 37 | |
| 38 | // The option needed to passed to "hiddenapi list". |
| 39 | hiddenAPIListOption string |
Paul Duffin | 5cca7c4 | 2021-05-26 10:16:01 +0100 | [diff] [blame] | 40 | |
| 41 | // The name sof the source stub library modules that contain the API provided by the platform, |
| 42 | // i.e. by modules that are not in an APEX. |
| 43 | nonUpdatableSourceModule string |
| 44 | |
| 45 | // The names of the prebuilt stub library modules that contain the API provided by the platform, |
| 46 | // i.e. by modules that are not in an APEX. |
| 47 | nonUpdatablePrebuiltModule string |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // initHiddenAPIScope initializes the scope. |
| 51 | func initHiddenAPIScope(apiScope *HiddenAPIScope) *HiddenAPIScope { |
Paul Duffin | 5cca7c4 | 2021-05-26 10:16:01 +0100 | [diff] [blame] | 52 | sdkKind := apiScope.sdkKind |
| 53 | // The platform does not provide a core platform API. |
| 54 | if sdkKind != android.SdkCorePlatform { |
| 55 | kindAsString := sdkKind.String() |
| 56 | var insert string |
| 57 | if sdkKind == android.SdkPublic { |
| 58 | insert = "" |
| 59 | } else { |
| 60 | insert = "." + strings.ReplaceAll(kindAsString, "-", "_") |
| 61 | } |
| 62 | |
| 63 | nonUpdatableModule := "android-non-updatable" |
| 64 | |
| 65 | // Construct the name of the android-non-updatable source module for this scope. |
| 66 | apiScope.nonUpdatableSourceModule = fmt.Sprintf("%s.stubs%s", nonUpdatableModule, insert) |
| 67 | |
| 68 | prebuiltModuleName := func(name string, kind string) string { |
| 69 | return fmt.Sprintf("sdk_%s_current_%s", kind, name) |
| 70 | } |
| 71 | |
| 72 | // Construct the name of the android-non-updatable prebuilt module for this scope. |
| 73 | apiScope.nonUpdatablePrebuiltModule = prebuiltModuleName(nonUpdatableModule, kindAsString) |
| 74 | } |
| 75 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 76 | return apiScope |
| 77 | } |
| 78 | |
Paul Duffin | 5cca7c4 | 2021-05-26 10:16:01 +0100 | [diff] [blame] | 79 | // android-non-updatable takes the name of a module and returns a possibly scope specific name of |
| 80 | // the module. |
| 81 | func (l *HiddenAPIScope) scopeSpecificStubModule(ctx android.BaseModuleContext, name string) string { |
| 82 | // The android-non-updatable is not a java_sdk_library but there are separate stub libraries for |
| 83 | // each scope. |
| 84 | // TODO(b/192067200): Remove special handling of android-non-updatable. |
| 85 | if name == "android-non-updatable" { |
| 86 | if ctx.Config().AlwaysUsePrebuiltSdks() { |
| 87 | return l.nonUpdatablePrebuiltModule |
| 88 | } else { |
| 89 | return l.nonUpdatableSourceModule |
| 90 | } |
| 91 | } else { |
| 92 | // Assume that the module is either a java_sdk_library (or equivalent) and so will provide |
| 93 | // separate stub jars for each scope or is a java_library (or equivalent) in which case it will |
| 94 | // have the same stub jar for each scope. |
| 95 | return name |
| 96 | } |
| 97 | } |
| 98 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 99 | func (l *HiddenAPIScope) String() string { |
| 100 | return fmt.Sprintf("HiddenAPIScope{%s}", l.name) |
| 101 | } |
| 102 | |
| 103 | var ( |
| 104 | PublicHiddenAPIScope = initHiddenAPIScope(&HiddenAPIScope{ |
| 105 | name: "public", |
| 106 | sdkKind: android.SdkPublic, |
| 107 | hiddenAPIListOption: "--public-stub-classpath", |
| 108 | }) |
| 109 | SystemHiddenAPIScope = initHiddenAPIScope(&HiddenAPIScope{ |
| 110 | name: "system", |
| 111 | sdkKind: android.SdkSystem, |
| 112 | hiddenAPIListOption: "--system-stub-classpath", |
| 113 | }) |
| 114 | TestHiddenAPIScope = initHiddenAPIScope(&HiddenAPIScope{ |
| 115 | name: "test", |
| 116 | sdkKind: android.SdkTest, |
| 117 | hiddenAPIListOption: "--test-stub-classpath", |
| 118 | }) |
Paul Duffin | b51db2e | 2021-06-21 14:08:08 +0100 | [diff] [blame] | 119 | ModuleLibHiddenAPIScope = initHiddenAPIScope(&HiddenAPIScope{ |
| 120 | name: "module-lib", |
| 121 | sdkKind: android.SdkModule, |
| 122 | }) |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 123 | CorePlatformHiddenAPIScope = initHiddenAPIScope(&HiddenAPIScope{ |
| 124 | name: "core-platform", |
| 125 | sdkKind: android.SdkCorePlatform, |
| 126 | hiddenAPIListOption: "--core-platform-stub-classpath", |
| 127 | }) |
| 128 | |
| 129 | // hiddenAPIRelevantSdkKinds lists all the android.SdkKind instances that are needed by the hidden |
| 130 | // API processing. |
| 131 | // |
| 132 | // These are roughly in order from narrowest API surface to widest. Widest means the API stubs |
| 133 | // with the biggest API surface, e.g. test is wider than system is wider than public. |
| 134 | // |
Paul Duffin | b51db2e | 2021-06-21 14:08:08 +0100 | [diff] [blame] | 135 | // Core platform is considered wider than system/module-lib because those modules that provide |
| 136 | // core platform APIs either do not have any system/module-lib APIs at all, or if they do it is |
| 137 | // because the core platform API is being converted to system/module-lib APIs. In either case the |
| 138 | // system/module-lib APIs are subsets of the core platform API. |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 139 | // |
| 140 | // This is not strictly in order from narrowest to widest as the Test API is wider than system but |
Paul Duffin | b51db2e | 2021-06-21 14:08:08 +0100 | [diff] [blame] | 141 | // is neither wider or narrower than the module-lib or core platform APIs. However, this works |
| 142 | // well enough at the moment. |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 143 | // TODO(b/191644675): Correctly reflect the sub/superset relationships between APIs. |
| 144 | hiddenAPIScopes = []*HiddenAPIScope{ |
| 145 | PublicHiddenAPIScope, |
| 146 | SystemHiddenAPIScope, |
| 147 | TestHiddenAPIScope, |
Paul Duffin | b51db2e | 2021-06-21 14:08:08 +0100 | [diff] [blame] | 148 | ModuleLibHiddenAPIScope, |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 149 | CorePlatformHiddenAPIScope, |
| 150 | } |
| 151 | |
| 152 | // The HiddenAPIScope instances that are supported by a java_sdk_library. |
| 153 | // |
| 154 | // CorePlatformHiddenAPIScope is not used as the java_sdk_library does not have special support |
| 155 | // for core_platform API, instead it is implemented as a customized form of PublicHiddenAPIScope. |
| 156 | hiddenAPISdkLibrarySupportedScopes = []*HiddenAPIScope{ |
| 157 | PublicHiddenAPIScope, |
| 158 | SystemHiddenAPIScope, |
| 159 | TestHiddenAPIScope, |
Paul Duffin | b51db2e | 2021-06-21 14:08:08 +0100 | [diff] [blame] | 160 | ModuleLibHiddenAPIScope, |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // The HiddenAPIScope instances that are supported by the `hiddenapi list`. |
| 164 | hiddenAPIFlagScopes = []*HiddenAPIScope{ |
| 165 | PublicHiddenAPIScope, |
| 166 | SystemHiddenAPIScope, |
| 167 | TestHiddenAPIScope, |
| 168 | CorePlatformHiddenAPIScope, |
| 169 | } |
| 170 | ) |
| 171 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 172 | type hiddenAPIStubsDependencyTag struct { |
| 173 | blueprint.BaseDependencyTag |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 174 | |
| 175 | // The api scope for which this dependency was added. |
| 176 | apiScope *HiddenAPIScope |
Paul Duffin | 5cca7c4 | 2021-05-26 10:16:01 +0100 | [diff] [blame] | 177 | |
| 178 | // Indicates that the dependency is not for an API provided by the current bootclasspath fragment |
| 179 | // but is an additional API provided by a module that is not part of the current bootclasspath |
| 180 | // fragment. |
| 181 | fromAdditionalDependency bool |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | func (b hiddenAPIStubsDependencyTag) ExcludeFromApexContents() { |
| 185 | } |
| 186 | |
| 187 | func (b hiddenAPIStubsDependencyTag) ReplaceSourceWithPrebuilt() bool { |
| 188 | return false |
| 189 | } |
| 190 | |
Paul Duffin | 976b0e5 | 2021-04-27 23:20:26 +0100 | [diff] [blame] | 191 | func (b hiddenAPIStubsDependencyTag) SdkMemberType(child android.Module) android.SdkMemberType { |
Paul Duffin | 5cca7c4 | 2021-05-26 10:16:01 +0100 | [diff] [blame] | 192 | // Do not add additional dependencies to the sdk. |
| 193 | if b.fromAdditionalDependency { |
| 194 | return nil |
| 195 | } |
| 196 | |
Paul Duffin | 976b0e5 | 2021-04-27 23:20:26 +0100 | [diff] [blame] | 197 | // If the module is a java_sdk_library then treat it as if it was specific in the java_sdk_libs |
| 198 | // property, otherwise treat if it was specified in the java_header_libs property. |
| 199 | if javaSdkLibrarySdkMemberType.IsInstance(child) { |
| 200 | return javaSdkLibrarySdkMemberType |
| 201 | } |
| 202 | |
| 203 | return javaHeaderLibsSdkMemberType |
| 204 | } |
| 205 | |
| 206 | func (b hiddenAPIStubsDependencyTag) ExportMember() bool { |
| 207 | // Export the module added via this dependency tag from the sdk. |
| 208 | return true |
| 209 | } |
| 210 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 211 | // Avoid having to make stubs content explicitly visible to dependent modules. |
| 212 | // |
| 213 | // This is a temporary workaround to make it easier to migrate to bootclasspath_fragment modules |
| 214 | // with proper dependencies. |
| 215 | // TODO(b/177892522): Remove this and add needed visibility. |
| 216 | func (b hiddenAPIStubsDependencyTag) ExcludeFromVisibilityEnforcement() { |
| 217 | } |
| 218 | |
| 219 | var _ android.ExcludeFromVisibilityEnforcementTag = hiddenAPIStubsDependencyTag{} |
| 220 | var _ android.ReplaceSourceWithPrebuilt = hiddenAPIStubsDependencyTag{} |
| 221 | var _ android.ExcludeFromApexContentsTag = hiddenAPIStubsDependencyTag{} |
Paul Duffin | 976b0e5 | 2021-04-27 23:20:26 +0100 | [diff] [blame] | 222 | var _ android.SdkMemberTypeDependencyTag = hiddenAPIStubsDependencyTag{} |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 223 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 224 | // hiddenAPIComputeMonolithicStubLibModules computes the set of module names that provide stubs |
| 225 | // needed to produce the hidden API monolithic stub flags file. |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 226 | func hiddenAPIComputeMonolithicStubLibModules(config android.Config) map[*HiddenAPIScope][]string { |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 227 | var publicStubModules []string |
| 228 | var systemStubModules []string |
| 229 | var testStubModules []string |
| 230 | var corePlatformStubModules []string |
| 231 | |
| 232 | if config.AlwaysUsePrebuiltSdks() { |
| 233 | // Build configuration mandates using prebuilt stub modules |
| 234 | publicStubModules = append(publicStubModules, "sdk_public_current_android") |
| 235 | systemStubModules = append(systemStubModules, "sdk_system_current_android") |
| 236 | testStubModules = append(testStubModules, "sdk_test_current_android") |
| 237 | } else { |
| 238 | // Use stub modules built from source |
| 239 | publicStubModules = append(publicStubModules, "android_stubs_current") |
| 240 | systemStubModules = append(systemStubModules, "android_system_stubs_current") |
| 241 | testStubModules = append(testStubModules, "android_test_stubs_current") |
| 242 | } |
| 243 | // We do not have prebuilts of the core platform api yet |
| 244 | corePlatformStubModules = append(corePlatformStubModules, "legacy.core.platform.api.stubs") |
| 245 | |
| 246 | // Allow products to define their own stubs for custom product jars that apps can use. |
| 247 | publicStubModules = append(publicStubModules, config.ProductHiddenAPIStubs()...) |
| 248 | systemStubModules = append(systemStubModules, config.ProductHiddenAPIStubsSystem()...) |
| 249 | testStubModules = append(testStubModules, config.ProductHiddenAPIStubsTest()...) |
| 250 | if config.IsEnvTrue("EMMA_INSTRUMENT") { |
Paul Duffin | 098c878 | 2021-05-14 10:45:25 +0100 | [diff] [blame] | 251 | // Add jacoco-stubs to public, system and test. It doesn't make any real difference as public |
| 252 | // allows everyone access but it is needed to ensure consistent flags between the |
| 253 | // bootclasspath fragment generated flags and the platform_bootclasspath generated flags. |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 254 | publicStubModules = append(publicStubModules, "jacoco-stubs") |
Paul Duffin | 098c878 | 2021-05-14 10:45:25 +0100 | [diff] [blame] | 255 | systemStubModules = append(systemStubModules, "jacoco-stubs") |
| 256 | testStubModules = append(testStubModules, "jacoco-stubs") |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 259 | m := map[*HiddenAPIScope][]string{} |
| 260 | m[PublicHiddenAPIScope] = publicStubModules |
| 261 | m[SystemHiddenAPIScope] = systemStubModules |
| 262 | m[TestHiddenAPIScope] = testStubModules |
| 263 | m[CorePlatformHiddenAPIScope] = corePlatformStubModules |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 264 | return m |
| 265 | } |
| 266 | |
| 267 | // hiddenAPIAddStubLibDependencies adds dependencies onto the modules specified in |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 268 | // apiScopeToStubLibModules. It adds them in a well known order and uses a HiddenAPIScope specific |
| 269 | // tag to identify the source of the dependency. |
| 270 | func hiddenAPIAddStubLibDependencies(ctx android.BottomUpMutatorContext, apiScopeToStubLibModules map[*HiddenAPIScope][]string) { |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 271 | module := ctx.Module() |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 272 | for _, apiScope := range hiddenAPIScopes { |
| 273 | modules := apiScopeToStubLibModules[apiScope] |
| 274 | ctx.AddDependency(module, hiddenAPIStubsDependencyTag{apiScope: apiScope}, modules...) |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 278 | // hiddenAPIRetrieveDexJarBuildPath retrieves the DexJarBuildPath from the specified module, if |
| 279 | // available, or reports an error. |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 280 | func hiddenAPIRetrieveDexJarBuildPath(ctx android.ModuleContext, module android.Module, kind android.SdkKind) android.Path { |
| 281 | var dexJar android.Path |
| 282 | if sdkLibrary, ok := module.(SdkLibraryDependency); ok { |
| 283 | dexJar = sdkLibrary.SdkApiStubDexJar(ctx, kind) |
| 284 | } else if j, ok := module.(UsesLibraryDependency); ok { |
| 285 | dexJar = j.DexJarBuildPath() |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 286 | } else { |
| 287 | ctx.ModuleErrorf("dependency %s of module type %s does not support providing a dex jar", module, ctx.OtherModuleType(module)) |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 288 | return nil |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 289 | } |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 290 | |
| 291 | if dexJar == nil { |
| 292 | ctx.ModuleErrorf("dependency %s does not provide a dex jar, consider setting compile_dex: true", module) |
| 293 | } |
| 294 | return dexJar |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Paul Duffin | 4539a37 | 2021-06-23 23:20:43 +0100 | [diff] [blame] | 297 | // buildRuleToGenerateHiddenAPIStubFlagsFile creates a rule to create a hidden API stub flags file. |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 298 | // |
| 299 | // The rule is initialized but not built so that the caller can modify it and select an appropriate |
| 300 | // name. |
Paul Duffin | 2e88097 | 2021-06-23 23:29:09 +0100 | [diff] [blame] | 301 | func buildRuleToGenerateHiddenAPIStubFlagsFile(ctx android.BuilderContext, name, desc string, outputPath android.WritablePath, bootDexJars android.Paths, input HiddenAPIFlagInput, moduleStubFlagsPaths android.Paths) { |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 302 | // Singleton rule which applies hiddenapi on all boot class path dex files. |
| 303 | rule := android.NewRuleBuilder(pctx, ctx) |
| 304 | |
| 305 | tempPath := tempPathForRestat(ctx, outputPath) |
| 306 | |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 307 | // Find the widest API stubs provided by the fragments on which this depends, if any. |
Paul Duffin | d2b1e0c | 2021-06-27 20:53:39 +0100 | [diff] [blame] | 308 | dependencyStubDexJars := input.DependencyStubDexJarsByScope.StubDexJarsForWidestAPIScope() |
Paul Duffin | 5cca7c4 | 2021-05-26 10:16:01 +0100 | [diff] [blame] | 309 | |
| 310 | // Add widest API stubs from the additional dependencies of this, if any. |
Paul Duffin | d2b1e0c | 2021-06-27 20:53:39 +0100 | [diff] [blame] | 311 | dependencyStubDexJars = append(dependencyStubDexJars, input.AdditionalStubDexJarsByScope.StubDexJarsForWidestAPIScope()...) |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 312 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 313 | command := rule.Command(). |
| 314 | Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")). |
| 315 | Text("list"). |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 316 | FlagForEachInput("--dependency-stub-dex=", dependencyStubDexJars). |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 317 | FlagForEachInput("--boot-dex=", bootDexJars) |
| 318 | |
Paul Duffin | 156b5d3 | 2021-06-24 23:06:52 +0100 | [diff] [blame] | 319 | // If no module stub flags paths are provided then this must be being called for a |
| 320 | // bootclasspath_fragment and not the whole platform_bootclasspath. |
| 321 | if moduleStubFlagsPaths == nil { |
| 322 | // This is being run on a fragment of the bootclasspath. |
| 323 | command.Flag("--fragment") |
| 324 | } |
| 325 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 326 | // Iterate over the api scopes in a fixed order. |
| 327 | for _, apiScope := range hiddenAPIFlagScopes { |
| 328 | // Merge in the stub dex jar paths for this api scope from the fragments on which it depends. |
| 329 | // They will be needed to resolve dependencies from this fragment's stubs to classes in the |
| 330 | // other fragment's APIs. |
| 331 | var paths android.Paths |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 332 | paths = append(paths, input.DependencyStubDexJarsByScope.StubDexJarsForScope(apiScope)...) |
| 333 | paths = append(paths, input.AdditionalStubDexJarsByScope.StubDexJarsForScope(apiScope)...) |
| 334 | paths = append(paths, input.StubDexJarsByScope.StubDexJarsForScope(apiScope)...) |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 335 | if len(paths) > 0 { |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 336 | option := apiScope.hiddenAPIListOption |
| 337 | command.FlagWithInputList(option+"=", paths, ":") |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | // Add the output path. |
| 342 | command.FlagWithOutput("--out-api-flags=", tempPath) |
| 343 | |
Paul Duffin | 2e88097 | 2021-06-23 23:29:09 +0100 | [diff] [blame] | 344 | // If there are stub flag files that have been generated by fragments on which this depends then |
| 345 | // use them to validate the stub flag file generated by the rules created by this method. |
| 346 | if len(moduleStubFlagsPaths) > 0 { |
| 347 | validFile := buildRuleValidateOverlappingCsvFiles(ctx, name, desc, outputPath, moduleStubFlagsPaths) |
| 348 | |
| 349 | // Add the file that indicates that the file generated by this is valid. |
| 350 | // |
| 351 | // This will cause the validation rule above to be run any time that the output of this rule |
| 352 | // changes but the validation will run in parallel with other rules that depend on this file. |
| 353 | command.Validation(validFile) |
| 354 | } |
| 355 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 356 | commitChangeForRestat(rule, tempPath, outputPath) |
Paul Duffin | 4539a37 | 2021-06-23 23:20:43 +0100 | [diff] [blame] | 357 | |
| 358 | rule.Build(name, desc) |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 359 | } |
| 360 | |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 361 | // HiddenAPIFlagFileProperties contains paths to the flag files that can be used to augment the |
| 362 | // information obtained from annotations within the source code in order to create the complete set |
| 363 | // of flags that should be applied to the dex implementation jars on the bootclasspath. |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 364 | // |
| 365 | // Each property contains a list of paths. With the exception of the Unsupported_packages the paths |
| 366 | // of each property reference a plain text file that contains a java signature per line. The flags |
| 367 | // for each of those signatures will be updated in a property specific way. |
| 368 | // |
| 369 | // The Unsupported_packages property contains a list of paths, each of which is a plain text file |
| 370 | // with one Java package per line. All members of all classes within that package (but not nested |
| 371 | // packages) will be updated in a property specific way. |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 372 | type HiddenAPIFlagFileProperties struct { |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 373 | // Marks each signature in the referenced files as being unsupported. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 374 | Unsupported []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 375 | |
| 376 | // Marks each signature in the referenced files as being unsupported because it has been removed. |
| 377 | // Any conflicts with other flags are ignored. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 378 | Removed []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 379 | |
| 380 | // Marks each signature in the referenced files as being supported only for targetSdkVersion <= R |
| 381 | // and low priority. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 382 | Max_target_r_low_priority []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 383 | |
| 384 | // Marks each signature in the referenced files as being supported only for targetSdkVersion <= Q. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 385 | Max_target_q []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 386 | |
| 387 | // Marks each signature in the referenced files as being supported only for targetSdkVersion <= P. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 388 | Max_target_p []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 389 | |
| 390 | // Marks each signature in the referenced files as being supported only for targetSdkVersion <= O |
| 391 | // and low priority. Any conflicts with other flags are ignored. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 392 | Max_target_o_low_priority []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 393 | |
| 394 | // Marks each signature in the referenced files as being blocked. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 395 | Blocked []string `android:"path"` |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 396 | |
| 397 | // Marks each signature in every package in the referenced files as being unsupported. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 398 | Unsupported_packages []string `android:"path"` |
| 399 | } |
| 400 | |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 401 | type hiddenAPIFlagFileCategory struct { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 402 | // PropertyName is the name of the property for this category. |
| 403 | PropertyName string |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 404 | |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 405 | // propertyValueReader retrieves the value of the property for this category from the set of |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 406 | // properties. |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 407 | propertyValueReader func(properties *HiddenAPIFlagFileProperties) []string |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 408 | |
| 409 | // commandMutator adds the appropriate command line options for this category to the supplied |
| 410 | // command |
| 411 | commandMutator func(command *android.RuleBuilderCommand, path android.Path) |
| 412 | } |
| 413 | |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 414 | // The flag file category for removed members of the API. |
| 415 | // |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 416 | // This is extracted from HiddenAPIFlagFileCategories as it is needed to add the dex signatures |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 417 | // list of removed API members that are generated automatically from the removed.txt files provided |
| 418 | // by API stubs. |
| 419 | var hiddenAPIRemovedFlagFileCategory = &hiddenAPIFlagFileCategory{ |
| 420 | // See HiddenAPIFlagFileProperties.Removed |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 421 | PropertyName: "removed", |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 422 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
| 423 | return properties.Removed |
| 424 | }, |
| 425 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 426 | command.FlagWithInput("--unsupported ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed") |
| 427 | }, |
| 428 | } |
| 429 | |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 430 | var HiddenAPIFlagFileCategories = []*hiddenAPIFlagFileCategory{ |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 431 | // See HiddenAPIFlagFileProperties.Unsupported |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 432 | { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 433 | PropertyName: "unsupported", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 434 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 435 | return properties.Unsupported |
| 436 | }, |
| 437 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 438 | command.FlagWithInput("--unsupported ", path) |
| 439 | }, |
| 440 | }, |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 441 | hiddenAPIRemovedFlagFileCategory, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 442 | // See HiddenAPIFlagFileProperties.Max_target_r_low_priority |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 443 | { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 444 | PropertyName: "max_target_r_low_priority", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 445 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 446 | return properties.Max_target_r_low_priority |
| 447 | }, |
| 448 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 449 | command.FlagWithInput("--max-target-r ", path).FlagWithArg("--tag ", "lo-prio") |
| 450 | }, |
| 451 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 452 | // See HiddenAPIFlagFileProperties.Max_target_q |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 453 | { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 454 | PropertyName: "max_target_q", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 455 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 456 | return properties.Max_target_q |
| 457 | }, |
| 458 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 459 | command.FlagWithInput("--max-target-q ", path) |
| 460 | }, |
| 461 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 462 | // See HiddenAPIFlagFileProperties.Max_target_p |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 463 | { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 464 | PropertyName: "max_target_p", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 465 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 466 | return properties.Max_target_p |
| 467 | }, |
| 468 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 469 | command.FlagWithInput("--max-target-p ", path) |
| 470 | }, |
| 471 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 472 | // See HiddenAPIFlagFileProperties.Max_target_o_low_priority |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 473 | { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 474 | PropertyName: "max_target_o_low_priority", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 475 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 476 | return properties.Max_target_o_low_priority |
| 477 | }, |
| 478 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 479 | command.FlagWithInput("--max-target-o ", path).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "lo-prio") |
| 480 | }, |
| 481 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 482 | // See HiddenAPIFlagFileProperties.Blocked |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 483 | { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 484 | PropertyName: "blocked", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 485 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 486 | return properties.Blocked |
| 487 | }, |
| 488 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 489 | command.FlagWithInput("--blocked ", path) |
| 490 | }, |
| 491 | }, |
Paul Duffin | 4616977 | 2021-04-14 15:01:56 +0100 | [diff] [blame] | 492 | // See HiddenAPIFlagFileProperties.Unsupported_packages |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 493 | { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 494 | PropertyName: "unsupported_packages", |
Paul Duffin | cc17bfe | 2021-04-19 13:21:20 +0100 | [diff] [blame] | 495 | propertyValueReader: func(properties *HiddenAPIFlagFileProperties) []string { |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 496 | return properties.Unsupported_packages |
| 497 | }, |
| 498 | commandMutator: func(command *android.RuleBuilderCommand, path android.Path) { |
| 499 | command.FlagWithInput("--unsupported ", path).Flag("--packages ") |
| 500 | }, |
| 501 | }, |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Paul Duffin | 438eb57 | 2021-05-21 16:58:23 +0100 | [diff] [blame] | 504 | // FlagFilesByCategory maps a hiddenAPIFlagFileCategory to the paths to the files in that category. |
| 505 | type FlagFilesByCategory map[*hiddenAPIFlagFileCategory]android.Paths |
| 506 | |
| 507 | // append appends the supplied flags files to the corresponding category in this map. |
| 508 | func (s FlagFilesByCategory) append(other FlagFilesByCategory) { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 509 | for _, category := range HiddenAPIFlagFileCategories { |
Paul Duffin | 438eb57 | 2021-05-21 16:58:23 +0100 | [diff] [blame] | 510 | s[category] = append(s[category], other[category]...) |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | // dedup removes duplicates in the flag files, while maintaining the order in which they were |
| 515 | // appended. |
| 516 | func (s FlagFilesByCategory) dedup() { |
| 517 | for category, paths := range s { |
| 518 | s[category] = android.FirstUniquePaths(paths) |
| 519 | } |
| 520 | } |
| 521 | |
Paul Duffin | af99afa | 2021-05-21 22:18:56 +0100 | [diff] [blame] | 522 | // HiddenAPIInfo contains information provided by the hidden API processing. |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 523 | // |
Paul Duffin | af99afa | 2021-05-21 22:18:56 +0100 | [diff] [blame] | 524 | // That includes paths resolved from HiddenAPIFlagFileProperties and also generated by hidden API |
| 525 | // processing. |
| 526 | type HiddenAPIInfo struct { |
Paul Duffin | 438eb57 | 2021-05-21 16:58:23 +0100 | [diff] [blame] | 527 | // FlagFilesByCategory maps from the flag file category to the paths containing information for |
| 528 | // that category. |
| 529 | FlagFilesByCategory FlagFilesByCategory |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 530 | |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 531 | // The paths to the stub dex jars for each of the *HiddenAPIScope in hiddenAPIScopes provided by |
| 532 | // this fragment and the fragments on which this depends. |
| 533 | TransitiveStubDexJarsByScope StubDexJarsByModule |
Paul Duffin | 18cf197 | 2021-05-21 22:46:59 +0100 | [diff] [blame] | 534 | |
Paul Duffin | 1e6f5c4 | 2021-05-21 16:15:31 +0100 | [diff] [blame] | 535 | // The output from the hidden API processing needs to be made available to other modules. |
| 536 | HiddenAPIFlagOutput |
Paul Duffin | c6bb7cf | 2021-04-08 17:49:27 +0100 | [diff] [blame] | 537 | } |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 538 | |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 539 | func newHiddenAPIInfo() *HiddenAPIInfo { |
| 540 | info := HiddenAPIInfo{ |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 541 | FlagFilesByCategory: FlagFilesByCategory{}, |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 542 | TransitiveStubDexJarsByScope: StubDexJarsByModule{}, |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 543 | } |
| 544 | return &info |
| 545 | } |
| 546 | |
| 547 | func (i *HiddenAPIInfo) mergeFromFragmentDeps(ctx android.ModuleContext, fragments []android.Module) { |
| 548 | // Merge all the information from the fragments. The fragments form a DAG so it is possible that |
| 549 | // this will introduce duplicates so they will be resolved after processing all the fragments. |
| 550 | for _, fragment := range fragments { |
| 551 | if ctx.OtherModuleHasProvider(fragment, HiddenAPIInfoProvider) { |
| 552 | info := ctx.OtherModuleProvider(fragment, HiddenAPIInfoProvider).(HiddenAPIInfo) |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 553 | i.TransitiveStubDexJarsByScope.addStubDexJarsByModule(info.TransitiveStubDexJarsByScope) |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 554 | } |
| 555 | } |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 556 | } |
| 557 | |
Paul Duffin | af99afa | 2021-05-21 22:18:56 +0100 | [diff] [blame] | 558 | var HiddenAPIInfoProvider = blueprint.NewProvider(HiddenAPIInfo{}) |
Paul Duffin | 9b381ef | 2021-04-08 23:01:37 +0100 | [diff] [blame] | 559 | |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 560 | // ModuleStubDexJars contains the stub dex jars provided by a single module. |
| 561 | // |
| 562 | // It maps a *HiddenAPIScope to the path to stub dex jars appropriate for that scope. See |
| 563 | // hiddenAPIScopes for a list of the acceptable *HiddenAPIScope values. |
| 564 | type ModuleStubDexJars map[*HiddenAPIScope]android.Path |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 565 | |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 566 | // stubDexJarForWidestAPIScope returns the stub dex jars for the widest API scope provided by this |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 567 | // map. |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 568 | // |
| 569 | // The relative width of APIs is determined by their order in hiddenAPIScopes. |
| 570 | func (s ModuleStubDexJars) stubDexJarForWidestAPIScope() android.Path { |
Paul Duffin | 5cca7c4 | 2021-05-26 10:16:01 +0100 | [diff] [blame] | 571 | for i := len(hiddenAPIScopes) - 1; i >= 0; i-- { |
| 572 | apiScope := hiddenAPIScopes[i] |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 573 | if stubsForAPIScope, ok := s[apiScope]; ok { |
Paul Duffin | 5cca7c4 | 2021-05-26 10:16:01 +0100 | [diff] [blame] | 574 | return stubsForAPIScope |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | return nil |
| 579 | } |
| 580 | |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 581 | // StubDexJarsByModule contains the stub dex jars provided by a set of modules. |
| 582 | // |
| 583 | // It maps a module name to the path to the stub dex jars provided by that module. |
| 584 | type StubDexJarsByModule map[string]ModuleStubDexJars |
| 585 | |
| 586 | // addStubDexJar adds a stub dex jar path provided by the specified module for the specified scope. |
| 587 | func (s StubDexJarsByModule) addStubDexJar(ctx android.ModuleContext, module android.Module, scope *HiddenAPIScope, stubDexJar android.Path) { |
| 588 | name := android.RemoveOptionalPrebuiltPrefix(module.Name()) |
| 589 | if name == scope.nonUpdatablePrebuiltModule || name == scope.nonUpdatableSourceModule { |
| 590 | // Treat all *android-non-updatable* modules as if they were part of an android-non-updatable |
| 591 | // java_sdk_library. |
| 592 | // TODO(b/192067200): Remove once android-non-updatable is a java_sdk_library or equivalent. |
| 593 | name = "android-non-updatable" |
| 594 | } else if name == "legacy.art.module.platform.api" { |
| 595 | // Treat legacy.art.module.platform.api as if it was an API scope provided by the |
| 596 | // art.module.public.api java_sdk_library which will be the case once the former has been |
| 597 | // migrated to a module_lib API. |
| 598 | name = "art.module.public.api" |
| 599 | } else if name == "legacy.i18n.module.platform.api" { |
| 600 | // Treat legacy.i18n.module.platform.api as if it was an API scope provided by the |
| 601 | // i18n.module.public.api java_sdk_library which will be the case once the former has been |
| 602 | // migrated to a module_lib API. |
| 603 | name = "i18n.module.public.api" |
| 604 | } else if name == "conscrypt.module.platform.api" { |
| 605 | // Treat conscrypt.module.platform.api as if it was an API scope provided by the |
| 606 | // conscrypt.module.public.api java_sdk_library which will be the case once the former has been |
| 607 | // migrated to a module_lib API. |
| 608 | name = "conscrypt.module.public.api" |
| 609 | } |
| 610 | stubDexJarsByScope := s[name] |
| 611 | if stubDexJarsByScope == nil { |
| 612 | stubDexJarsByScope = ModuleStubDexJars{} |
| 613 | s[name] = stubDexJarsByScope |
| 614 | } |
| 615 | stubDexJarsByScope[scope] = stubDexJar |
| 616 | } |
| 617 | |
| 618 | // addStubDexJarsByModule adds the stub dex jars in the supplied StubDexJarsByModule to this map. |
| 619 | func (s StubDexJarsByModule) addStubDexJarsByModule(other StubDexJarsByModule) { |
| 620 | for module, stubDexJarsByScope := range other { |
| 621 | s[module] = stubDexJarsByScope |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | // StubDexJarsForWidestAPIScope returns a list of stub dex jars containing the widest API scope |
| 626 | // provided by each module. |
| 627 | // |
| 628 | // The relative width of APIs is determined by their order in hiddenAPIScopes. |
| 629 | func (s StubDexJarsByModule) StubDexJarsForWidestAPIScope() android.Paths { |
| 630 | stubDexJars := android.Paths{} |
| 631 | modules := android.SortedStringKeys(s) |
| 632 | for _, module := range modules { |
| 633 | stubDexJarsByScope := s[module] |
| 634 | |
| 635 | stubDexJars = append(stubDexJars, stubDexJarsByScope.stubDexJarForWidestAPIScope()) |
| 636 | } |
| 637 | |
| 638 | return stubDexJars |
| 639 | } |
| 640 | |
| 641 | // StubDexJarsForScope returns a list of stub dex jars containing the stub dex jars provided by each |
| 642 | // module for the specified scope. |
| 643 | // |
| 644 | // If a module does not provide a stub dex jar for the supplied scope then it does not contribute to |
| 645 | // the returned list. |
| 646 | func (s StubDexJarsByModule) StubDexJarsForScope(scope *HiddenAPIScope) android.Paths { |
| 647 | stubDexJars := android.Paths{} |
| 648 | modules := android.SortedStringKeys(s) |
| 649 | for _, module := range modules { |
| 650 | stubDexJarsByScope := s[module] |
| 651 | // Not every module will have the same set of |
| 652 | if jars, ok := stubDexJarsByScope[scope]; ok { |
| 653 | stubDexJars = append(stubDexJars, jars) |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | return stubDexJars |
| 658 | } |
| 659 | |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 660 | // HiddenAPIFlagInput encapsulates information obtained from a module and its dependencies that are |
| 661 | // needed for hidden API flag generation. |
| 662 | type HiddenAPIFlagInput struct { |
| 663 | // FlagFilesByCategory contains the flag files that override the initial flags that are derived |
| 664 | // from the stub dex files. |
| 665 | FlagFilesByCategory FlagFilesByCategory |
| 666 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 667 | // StubDexJarsByScope contains the stub dex jars for different *HiddenAPIScope and which determine |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 668 | // the initial flags for each dex member. |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 669 | StubDexJarsByScope StubDexJarsByModule |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 670 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 671 | // DependencyStubDexJarsByScope contains the stub dex jars provided by the fragments on which this |
| 672 | // depends. It is the result of merging HiddenAPIInfo.TransitiveStubDexJarsByScope from each |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 673 | // fragment on which this depends. |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 674 | DependencyStubDexJarsByScope StubDexJarsByModule |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 675 | |
Paul Duffin | 5cca7c4 | 2021-05-26 10:16:01 +0100 | [diff] [blame] | 676 | // AdditionalStubDexJarsByScope contains stub dex jars provided by other modules in addition to |
| 677 | // the ones that are obtained from fragments on which this depends. |
| 678 | // |
| 679 | // These are kept separate from stub dex jars in HiddenAPIFlagInput.DependencyStubDexJarsByScope |
| 680 | // as there are not propagated transitively to other fragments that depend on this. |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 681 | AdditionalStubDexJarsByScope StubDexJarsByModule |
Paul Duffin | 5cca7c4 | 2021-05-26 10:16:01 +0100 | [diff] [blame] | 682 | |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 683 | // RemovedTxtFiles is the list of removed.txt files provided by java_sdk_library modules that are |
| 684 | // specified in the bootclasspath_fragment's stub_libs and contents properties. |
| 685 | RemovedTxtFiles android.Paths |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | // newHiddenAPIFlagInput creates a new initialize HiddenAPIFlagInput struct. |
| 689 | func newHiddenAPIFlagInput() HiddenAPIFlagInput { |
| 690 | input := HiddenAPIFlagInput{ |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 691 | FlagFilesByCategory: FlagFilesByCategory{}, |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 692 | StubDexJarsByScope: StubDexJarsByModule{}, |
| 693 | DependencyStubDexJarsByScope: StubDexJarsByModule{}, |
| 694 | AdditionalStubDexJarsByScope: StubDexJarsByModule{}, |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | return input |
| 698 | } |
| 699 | |
Paul Duffin | 6237092 | 2021-05-23 16:55:37 +0100 | [diff] [blame] | 700 | // canPerformHiddenAPIProcessing determines whether hidden API processing should be performed. |
| 701 | // |
| 702 | // A temporary workaround to avoid existing bootclasspath_fragments that do not provide the |
| 703 | // appropriate information needed for hidden API processing breaking the build. |
| 704 | // TODO(b/179354495): Remove this workaround. |
| 705 | func (i *HiddenAPIFlagInput) canPerformHiddenAPIProcessing(ctx android.ModuleContext, properties bootclasspathFragmentProperties) bool { |
| 706 | // Performing hidden API processing without stubs is not supported and it is unlikely to ever be |
| 707 | // required as the whole point of adding something to the bootclasspath fragment is to add it to |
| 708 | // the bootclasspath in order to be used by something else in the system. Without any stubs it |
| 709 | // cannot do that. |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 710 | if len(i.StubDexJarsByScope) == 0 { |
Paul Duffin | 6237092 | 2021-05-23 16:55:37 +0100 | [diff] [blame] | 711 | return false |
| 712 | } |
| 713 | |
| 714 | // Hidden API processing is always enabled in tests. |
| 715 | if ctx.Config().TestProductVariables != nil { |
| 716 | return true |
| 717 | } |
| 718 | |
| 719 | // A module that has fragments should have access to the information it needs in order to perform |
| 720 | // hidden API processing. |
| 721 | if len(properties.Fragments) != 0 { |
| 722 | return true |
| 723 | } |
| 724 | |
| 725 | // The art bootclasspath fragment does not depend on any other fragments but already supports |
| 726 | // hidden API processing. |
| 727 | imageName := proptools.String(properties.Image_name) |
| 728 | if imageName == "art" { |
| 729 | return true |
| 730 | } |
| 731 | |
| 732 | // Disable it for everything else. |
| 733 | return false |
| 734 | } |
| 735 | |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 736 | // gatherStubLibInfo gathers information from the stub libs needed by hidden API processing from the |
| 737 | // dependencies added in hiddenAPIAddStubLibDependencies. |
| 738 | // |
| 739 | // That includes paths to the stub dex jars as well as paths to the *removed.txt files. |
| 740 | func (i *HiddenAPIFlagInput) gatherStubLibInfo(ctx android.ModuleContext, contents []android.Module) { |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 741 | addFromModule := func(ctx android.ModuleContext, module android.Module, apiScope *HiddenAPIScope) { |
| 742 | sdkKind := apiScope.sdkKind |
| 743 | dexJar := hiddenAPIRetrieveDexJarBuildPath(ctx, module, sdkKind) |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 744 | if dexJar != nil { |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 745 | i.StubDexJarsByScope.addStubDexJar(ctx, module, apiScope, dexJar) |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 746 | } |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 747 | |
| 748 | if sdkLibrary, ok := module.(SdkLibraryDependency); ok { |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 749 | removedTxtFile := sdkLibrary.SdkRemovedTxtFile(ctx, sdkKind) |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 750 | i.RemovedTxtFiles = append(i.RemovedTxtFiles, removedTxtFile.AsPaths()...) |
| 751 | } |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | // If the contents includes any java_sdk_library modules then add them to the stubs. |
| 755 | for _, module := range contents { |
| 756 | if _, ok := module.(SdkLibraryDependency); ok { |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 757 | // Add information for every possible API scope needed by hidden API. |
| 758 | for _, apiScope := range hiddenAPISdkLibrarySupportedScopes { |
| 759 | addFromModule(ctx, module, apiScope) |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
Paul Duffin | d061d40 | 2021-06-07 21:36:01 +0100 | [diff] [blame] | 764 | ctx.VisitDirectDeps(func(module android.Module) { |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 765 | tag := ctx.OtherModuleDependencyTag(module) |
| 766 | if hiddenAPIStubsTag, ok := tag.(hiddenAPIStubsDependencyTag); ok { |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 767 | apiScope := hiddenAPIStubsTag.apiScope |
Paul Duffin | 5cca7c4 | 2021-05-26 10:16:01 +0100 | [diff] [blame] | 768 | if hiddenAPIStubsTag.fromAdditionalDependency { |
| 769 | dexJar := hiddenAPIRetrieveDexJarBuildPath(ctx, module, apiScope.sdkKind) |
| 770 | if dexJar != nil { |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 771 | i.AdditionalStubDexJarsByScope.addStubDexJar(ctx, module, apiScope, dexJar) |
Paul Duffin | 5cca7c4 | 2021-05-26 10:16:01 +0100 | [diff] [blame] | 772 | } |
| 773 | } else { |
| 774 | addFromModule(ctx, module, apiScope) |
| 775 | } |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 776 | } |
| 777 | }) |
| 778 | |
| 779 | // Normalize the paths, i.e. remove duplicates and sort. |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 780 | i.RemovedTxtFiles = android.SortedUniquePaths(i.RemovedTxtFiles) |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | // extractFlagFilesFromProperties extracts the paths to flag files that are specified in the |
| 784 | // supplied properties and stores them in this struct. |
| 785 | func (i *HiddenAPIFlagInput) extractFlagFilesFromProperties(ctx android.ModuleContext, p *HiddenAPIFlagFileProperties) { |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 786 | for _, category := range HiddenAPIFlagFileCategories { |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 787 | paths := android.PathsForModuleSrc(ctx, category.propertyValueReader(p)) |
| 788 | i.FlagFilesByCategory[category] = paths |
| 789 | } |
| 790 | } |
| 791 | |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 792 | func (i *HiddenAPIFlagInput) transitiveStubDexJarsByScope() StubDexJarsByModule { |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 793 | transitive := i.DependencyStubDexJarsByScope |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 794 | transitive.addStubDexJarsByModule(i.StubDexJarsByScope) |
Paul Duffin | f1b358c | 2021-05-17 07:38:47 +0100 | [diff] [blame] | 795 | return transitive |
| 796 | } |
| 797 | |
Paul Duffin | 1e6f5c4 | 2021-05-21 16:15:31 +0100 | [diff] [blame] | 798 | // HiddenAPIFlagOutput contains paths to output files from the hidden API flag generation for a |
| 799 | // bootclasspath_fragment module. |
| 800 | type HiddenAPIFlagOutput struct { |
| 801 | // The path to the generated stub-flags.csv file. |
| 802 | StubFlagsPath android.Path |
| 803 | |
| 804 | // The path to the generated annotation-flags.csv file. |
| 805 | AnnotationFlagsPath android.Path |
| 806 | |
| 807 | // The path to the generated metadata.csv file. |
| 808 | MetadataPath android.Path |
| 809 | |
| 810 | // The path to the generated index.csv file. |
| 811 | IndexPath android.Path |
| 812 | |
| 813 | // The path to the generated all-flags.csv file. |
| 814 | AllFlagsPath android.Path |
| 815 | } |
| 816 | |
Paul Duffin | 5f148ca | 2021-06-02 17:24:22 +0100 | [diff] [blame] | 817 | // bootDexJarByModule is a map from base module name (without prebuilt_ prefix) to the boot dex |
| 818 | // path. |
| 819 | type bootDexJarByModule map[string]android.Path |
| 820 | |
| 821 | // addPath adds the path for a module to the map. |
| 822 | func (b bootDexJarByModule) addPath(module android.Module, path android.Path) { |
| 823 | b[android.RemoveOptionalPrebuiltPrefix(module.Name())] = path |
| 824 | } |
| 825 | |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 826 | // bootDexJars returns the boot dex jar paths sorted by their keys. |
| 827 | func (b bootDexJarByModule) bootDexJars() android.Paths { |
| 828 | paths := android.Paths{} |
| 829 | for _, k := range android.SortedStringKeys(b) { |
| 830 | paths = append(paths, b[k]) |
| 831 | } |
| 832 | return paths |
| 833 | } |
| 834 | |
Paul Duffin | 7f87216 | 2021-06-17 19:33:24 +0100 | [diff] [blame] | 835 | // bootDexJarsWithoutCoverage returns the boot dex jar paths sorted by their keys without coverage |
| 836 | // libraries if present. |
| 837 | func (b bootDexJarByModule) bootDexJarsWithoutCoverage() android.Paths { |
| 838 | paths := android.Paths{} |
| 839 | for _, k := range android.SortedStringKeys(b) { |
| 840 | if k == "jacocoagent" { |
| 841 | continue |
| 842 | } |
| 843 | paths = append(paths, b[k]) |
| 844 | } |
| 845 | return paths |
| 846 | } |
| 847 | |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 848 | // HiddenAPIOutput encapsulates the output from the hidden API processing. |
| 849 | type HiddenAPIOutput struct { |
| 850 | HiddenAPIFlagOutput |
| 851 | |
| 852 | // The map from base module name to the path to the encoded boot dex file. |
| 853 | EncodedBootDexFilesByModule bootDexJarByModule |
| 854 | } |
| 855 | |
Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 856 | // pathForValidation creates a path of the same type as the supplied type but with a name of |
| 857 | // <path>.valid. |
| 858 | // |
| 859 | // e.g. If path is an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv then this will return |
| 860 | // an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv.valid |
| 861 | func pathForValidation(ctx android.PathContext, path android.WritablePath) android.WritablePath { |
| 862 | extWithoutLeadingDot := strings.TrimPrefix(path.Ext(), ".") |
| 863 | return path.ReplaceExtension(ctx, extWithoutLeadingDot+".valid") |
| 864 | } |
| 865 | |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 866 | // buildRuleToGenerateHiddenApiFlags creates a rule to create the monolithic hidden API flags from |
| 867 | // the flags from all the modules, the stub flags, augmented with some additional configuration |
| 868 | // files. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 869 | // |
| 870 | // baseFlagsPath is the path to the flags file containing all the information from the stubs plus |
| 871 | // an entry for every single member in the dex implementation jars of the individual modules. Every |
| 872 | // signature in any of the other files MUST be included in this file. |
| 873 | // |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 874 | // annotationFlags is the path to the annotation flags file generated from annotation information |
| 875 | // in each module. |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 876 | // |
Paul Duffin | af99afa | 2021-05-21 22:18:56 +0100 | [diff] [blame] | 877 | // hiddenAPIInfo is a struct containing paths to files that augment the information provided by |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 878 | // the annotationFlags. |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 879 | func buildRuleToGenerateHiddenApiFlags(ctx android.BuilderContext, name, desc string, |
Paul Duffin | d061d40 | 2021-06-07 21:36:01 +0100 | [diff] [blame] | 880 | outputPath android.WritablePath, baseFlagsPath android.Path, annotationFlagPaths android.Paths, |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 881 | flagFilesByCategory FlagFilesByCategory, allFlagsPaths android.Paths, generatedRemovedDexSignatures android.OptionalPath) { |
Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 882 | |
Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 883 | // Create the rule that will generate the flag files. |
Paul Duffin | d3c1513 | 2021-04-21 22:12:35 +0100 | [diff] [blame] | 884 | tempPath := tempPathForRestat(ctx, outputPath) |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 885 | rule := android.NewRuleBuilder(pctx, ctx) |
| 886 | command := rule.Command(). |
| 887 | BuiltTool("generate_hiddenapi_lists"). |
| 888 | FlagWithInput("--csv ", baseFlagsPath). |
Paul Duffin | d061d40 | 2021-06-07 21:36:01 +0100 | [diff] [blame] | 889 | Inputs(annotationFlagPaths). |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 890 | FlagWithOutput("--output ", tempPath) |
| 891 | |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 892 | // Add the options for the different categories of flag files. |
Paul Duffin | 524c82c | 2021-06-09 14:39:28 +0100 | [diff] [blame] | 893 | for _, category := range HiddenAPIFlagFileCategories { |
Paul Duffin | 438eb57 | 2021-05-21 16:58:23 +0100 | [diff] [blame] | 894 | paths := flagFilesByCategory[category] |
Paul Duffin | e3dc660 | 2021-04-14 09:50:43 +0100 | [diff] [blame] | 895 | for _, path := range paths { |
| 896 | category.commandMutator(command, path) |
| 897 | } |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 898 | } |
| 899 | |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 900 | // If available then pass the automatically generated file containing dex signatures of removed |
| 901 | // API members to the rule so they can be marked as removed. |
| 902 | if generatedRemovedDexSignatures.Valid() { |
| 903 | hiddenAPIRemovedFlagFileCategory.commandMutator(command, generatedRemovedDexSignatures.Path()) |
| 904 | } |
| 905 | |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 906 | commitChangeForRestat(rule, tempPath, outputPath) |
| 907 | |
Paul Duffin | 2e88097 | 2021-06-23 23:29:09 +0100 | [diff] [blame] | 908 | // If there are flag files that have been generated by fragments on which this depends then use |
| 909 | // them to validate the flag file generated by the rules created by this method. |
| 910 | if len(allFlagsPaths) > 0 { |
| 911 | validFile := buildRuleValidateOverlappingCsvFiles(ctx, name, desc, outputPath, allFlagsPaths) |
| 912 | |
Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 913 | // Add the file that indicates that the file generated by this is valid. |
| 914 | // |
| 915 | // This will cause the validation rule above to be run any time that the output of this rule |
| 916 | // changes but the validation will run in parallel with other rules that depend on this file. |
| 917 | command.Validation(validFile) |
| 918 | } |
| 919 | |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 920 | rule.Build(name, desc) |
| 921 | } |
| 922 | |
Paul Duffin | 2e88097 | 2021-06-23 23:29:09 +0100 | [diff] [blame] | 923 | // buildRuleValidateOverlappingCsvFiles checks that the modular CSV files, i.e. the files generated |
| 924 | // by the individual bootclasspath_fragment modules are subsets of the monolithic CSV file. |
| 925 | func buildRuleValidateOverlappingCsvFiles(ctx android.BuilderContext, name string, desc string, monolithicFilePath android.WritablePath, modularFilePaths android.Paths) android.WritablePath { |
| 926 | // The file which is used to record that the flags file is valid. |
| 927 | validFile := pathForValidation(ctx, monolithicFilePath) |
| 928 | |
| 929 | // Create a rule to validate the output from the following rule. |
| 930 | rule := android.NewRuleBuilder(pctx, ctx) |
| 931 | rule.Command(). |
| 932 | BuiltTool("verify_overlaps"). |
| 933 | Input(monolithicFilePath). |
| 934 | Inputs(modularFilePaths). |
| 935 | // If validation passes then update the file that records that. |
| 936 | Text("&& touch").Output(validFile) |
| 937 | rule.Build(name+"Validation", desc+" validation") |
| 938 | |
| 939 | return validFile |
| 940 | } |
| 941 | |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 942 | // hiddenAPIRulesForBootclasspathFragment will generate all the flags for a fragment of the |
| 943 | // bootclasspath and then encode the flags into the boot dex files. |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 944 | // |
| 945 | // It takes: |
| 946 | // * Map from android.SdkKind to stub dex jar paths defining the API for that sdk kind. |
| 947 | // * The list of modules that are the contents of the fragment. |
| 948 | // * The additional manually curated flag files to use. |
| 949 | // |
| 950 | // It generates: |
| 951 | // * stub-flags.csv |
| 952 | // * annotation-flags.csv |
| 953 | // * metadata.csv |
| 954 | // * index.csv |
| 955 | // * all-flags.csv |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 956 | // * encoded boot dex files |
| 957 | func hiddenAPIRulesForBootclasspathFragment(ctx android.ModuleContext, contents []android.Module, input HiddenAPIFlagInput) *HiddenAPIOutput { |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 958 | hiddenApiSubDir := "modular-hiddenapi" |
| 959 | |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 960 | // Gather information about the boot dex files for the boot libraries provided by this fragment. |
| 961 | bootDexInfoByModule := extractBootDexInfoFromModules(ctx, contents) |
Paul Duffin | 1352f7c | 2021-05-21 22:18:49 +0100 | [diff] [blame] | 962 | |
| 963 | // Generate the stub-flags.csv. |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 964 | stubFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "stub-flags.csv") |
Paul Duffin | 2e88097 | 2021-06-23 23:29:09 +0100 | [diff] [blame] | 965 | buildRuleToGenerateHiddenAPIStubFlagsFile(ctx, "modularHiddenAPIStubFlagsFile", "modular hiddenapi stub flags", stubFlagsCSV, bootDexInfoByModule.bootDexJars(), input, nil) |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 966 | |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 967 | // Extract the classes jars from the contents. |
Paul Duffin | dd5993f | 2021-06-10 10:18:22 +0100 | [diff] [blame] | 968 | classesJars := extractClassesJarsFromModules(contents) |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 969 | |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 970 | // Generate the set of flags from the annotations in the source code. |
| 971 | annotationFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "annotation-flags.csv") |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 972 | buildRuleToGenerateAnnotationFlags(ctx, "modular hiddenapi annotation flags", classesJars, stubFlagsCSV, annotationFlagsCSV) |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 973 | |
| 974 | // Generate the metadata from the annotations in the source code. |
| 975 | metadataCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "metadata.csv") |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 976 | buildRuleToGenerateMetadata(ctx, "modular hiddenapi metadata", classesJars, stubFlagsCSV, metadataCSV) |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 977 | |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 978 | // Generate the index file from the CSV files in the classes jars. |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 979 | indexCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "index.csv") |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 980 | buildRuleToGenerateIndex(ctx, "modular hiddenapi index", classesJars, indexCSV) |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 981 | |
Paul Duffin | af99afa | 2021-05-21 22:18:56 +0100 | [diff] [blame] | 982 | // Removed APIs need to be marked and in order to do that the hiddenAPIInfo needs to specify files |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 983 | // containing dex signatures of all the removed APIs. In the monolithic files that is done by |
| 984 | // manually combining all the removed.txt files for each API and then converting them to dex |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 985 | // signatures, see the combined-removed-dex module. This does that automatically by using the |
| 986 | // *removed.txt files retrieved from the java_sdk_library modules that are specified in the |
| 987 | // stub_libs and contents properties of a bootclasspath_fragment. |
| 988 | removedDexSignatures := buildRuleToGenerateRemovedDexSignatures(ctx, input.RemovedTxtFiles) |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 989 | |
| 990 | // Generate the all-flags.csv which are the flags that will, in future, be encoded into the dex |
| 991 | // files. |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 992 | allFlagsCSV := android.PathForModuleOut(ctx, hiddenApiSubDir, "all-flags.csv") |
Paul Duffin | d061d40 | 2021-06-07 21:36:01 +0100 | [diff] [blame] | 993 | buildRuleToGenerateHiddenApiFlags(ctx, "modularHiddenApiAllFlags", "modular hiddenapi all flags", allFlagsCSV, stubFlagsCSV, android.Paths{annotationFlagsCSV}, input.FlagFilesByCategory, nil, removedDexSignatures) |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 994 | |
| 995 | // Encode the flags into the boot dex files. |
| 996 | encodedBootDexJarsByModule := map[string]android.Path{} |
| 997 | outputDir := android.PathForModuleOut(ctx, "hiddenapi-modular/encoded").OutputPath |
| 998 | for _, name := range android.SortedStringKeys(bootDexInfoByModule) { |
| 999 | bootDexInfo := bootDexInfoByModule[name] |
| 1000 | unencodedDex := bootDexInfo.path |
| 1001 | encodedDex := hiddenAPIEncodeDex(ctx, unencodedDex, allFlagsCSV, bootDexInfo.uncompressDex, outputDir) |
| 1002 | encodedBootDexJarsByModule[name] = encodedDex |
| 1003 | } |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 1004 | |
| 1005 | // Store the paths in the info for use by other modules and sdk snapshot generation. |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 1006 | output := HiddenAPIOutput{ |
| 1007 | HiddenAPIFlagOutput: HiddenAPIFlagOutput{ |
| 1008 | StubFlagsPath: stubFlagsCSV, |
| 1009 | AnnotationFlagsPath: annotationFlagsCSV, |
| 1010 | MetadataPath: metadataCSV, |
| 1011 | IndexPath: indexCSV, |
| 1012 | AllFlagsPath: allFlagsCSV, |
| 1013 | }, |
| 1014 | EncodedBootDexFilesByModule: encodedBootDexJarsByModule, |
Paul Duffin | 1e6f5c4 | 2021-05-21 16:15:31 +0100 | [diff] [blame] | 1015 | } |
| 1016 | return &output |
Paul Duffin | 702210b | 2021-04-08 20:12:41 +0100 | [diff] [blame] | 1017 | } |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 1018 | |
Paul Duffin | 32cf58a | 2021-05-18 16:32:50 +0100 | [diff] [blame] | 1019 | func buildRuleToGenerateRemovedDexSignatures(ctx android.ModuleContext, removedTxtFiles android.Paths) android.OptionalPath { |
| 1020 | if len(removedTxtFiles) == 0 { |
| 1021 | return android.OptionalPath{} |
| 1022 | } |
| 1023 | |
| 1024 | output := android.PathForModuleOut(ctx, "modular-hiddenapi/removed-dex-signatures.txt") |
| 1025 | |
| 1026 | rule := android.NewRuleBuilder(pctx, ctx) |
| 1027 | rule.Command(). |
| 1028 | BuiltTool("metalava"). |
| 1029 | Flag("--no-banner"). |
| 1030 | Inputs(removedTxtFiles). |
| 1031 | FlagWithOutput("--dex-api ", output) |
| 1032 | rule.Build("modular-hiddenapi-removed-dex-signatures", "modular hiddenapi removed dex signatures") |
| 1033 | return android.OptionalPathForPath(output) |
| 1034 | } |
| 1035 | |
Paul Duffin | dd5993f | 2021-06-10 10:18:22 +0100 | [diff] [blame] | 1036 | // extractBootDexJarsFromModules extracts the boot dex jars from the supplied modules. |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 1037 | func extractBootDexJarsFromModules(ctx android.ModuleContext, contents []android.Module) bootDexJarByModule { |
| 1038 | bootDexJars := bootDexJarByModule{} |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 1039 | for _, module := range contents { |
Paul Duffin | dd5993f | 2021-06-10 10:18:22 +0100 | [diff] [blame] | 1040 | hiddenAPIModule := hiddenAPIModuleFromModule(ctx, module) |
| 1041 | if hiddenAPIModule == nil { |
| 1042 | continue |
| 1043 | } |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 1044 | bootDexJar := retrieveBootDexJarFromHiddenAPIModule(ctx, hiddenAPIModule) |
| 1045 | bootDexJars.addPath(module, bootDexJar) |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 1046 | } |
| 1047 | return bootDexJars |
| 1048 | } |
| 1049 | |
Paul Duffin | dd5993f | 2021-06-10 10:18:22 +0100 | [diff] [blame] | 1050 | func hiddenAPIModuleFromModule(ctx android.BaseModuleContext, module android.Module) hiddenAPIModule { |
| 1051 | if hiddenAPIModule, ok := module.(hiddenAPIModule); ok { |
| 1052 | return hiddenAPIModule |
| 1053 | } else if _, ok := module.(*DexImport); ok { |
| 1054 | // Ignore this for the purposes of hidden API processing |
| 1055 | } else { |
| 1056 | ctx.ModuleErrorf("module %s does not implement hiddenAPIModule", module) |
| 1057 | } |
| 1058 | |
| 1059 | return nil |
| 1060 | } |
| 1061 | |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 1062 | // bootDexInfo encapsulates both the path and uncompressDex status retrieved from a hiddenAPIModule. |
| 1063 | type bootDexInfo struct { |
| 1064 | // The path to the dex jar that has not had hidden API flags encoded into it. |
| 1065 | path android.Path |
| 1066 | |
| 1067 | // Indicates whether the dex jar needs uncompressing before encoding. |
| 1068 | uncompressDex bool |
| 1069 | } |
| 1070 | |
| 1071 | // bootDexInfoByModule is a map from module name (as returned by module.Name()) to the boot dex |
| 1072 | // path (as returned by hiddenAPIModule.bootDexJar()) and the uncompressDex flag. |
| 1073 | type bootDexInfoByModule map[string]bootDexInfo |
| 1074 | |
| 1075 | // bootDexJars returns the boot dex jar paths sorted by their keys. |
| 1076 | func (b bootDexInfoByModule) bootDexJars() android.Paths { |
| 1077 | paths := android.Paths{} |
| 1078 | for _, m := range android.SortedStringKeys(b) { |
| 1079 | paths = append(paths, b[m].path) |
| 1080 | } |
| 1081 | return paths |
| 1082 | } |
| 1083 | |
| 1084 | // extractBootDexInfoFromModules extracts the boot dex jar and uncompress dex state from |
| 1085 | // each of the supplied modules which must implement hiddenAPIModule. |
| 1086 | func extractBootDexInfoFromModules(ctx android.ModuleContext, contents []android.Module) bootDexInfoByModule { |
| 1087 | bootDexJarsByModule := bootDexInfoByModule{} |
| 1088 | for _, module := range contents { |
| 1089 | hiddenAPIModule := module.(hiddenAPIModule) |
| 1090 | bootDexJar := retrieveBootDexJarFromHiddenAPIModule(ctx, hiddenAPIModule) |
| 1091 | bootDexJarsByModule[module.Name()] = bootDexInfo{ |
| 1092 | path: bootDexJar, |
| 1093 | uncompressDex: *hiddenAPIModule.uncompressDex(), |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | return bootDexJarsByModule |
| 1098 | } |
| 1099 | |
| 1100 | // retrieveBootDexJarFromHiddenAPIModule retrieves the boot dex jar from the hiddenAPIModule. |
| 1101 | // |
| 1102 | // If the module does not provide a boot dex jar, i.e. the returned boot dex jar is nil, then that |
| 1103 | // create a fake path and either report an error immediately or defer reporting of the error until |
| 1104 | // the path is actually used. |
| 1105 | func retrieveBootDexJarFromHiddenAPIModule(ctx android.ModuleContext, module hiddenAPIModule) android.Path { |
| 1106 | bootDexJar := module.bootDexJar() |
| 1107 | if bootDexJar == nil { |
| 1108 | fake := android.PathForModuleOut(ctx, fmt.Sprintf("fake/boot-dex/%s.jar", module.Name())) |
| 1109 | bootDexJar = fake |
| 1110 | |
| 1111 | handleMissingDexBootFile(ctx, module, fake) |
| 1112 | } |
| 1113 | return bootDexJar |
| 1114 | } |
| 1115 | |
Paul Duffin | dd5993f | 2021-06-10 10:18:22 +0100 | [diff] [blame] | 1116 | // extractClassesJarsFromModules extracts the class jars from the supplied modules. |
| 1117 | func extractClassesJarsFromModules(contents []android.Module) android.Paths { |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 1118 | classesJars := android.Paths{} |
| 1119 | for _, module := range contents { |
Paul Duffin | dd5993f | 2021-06-10 10:18:22 +0100 | [diff] [blame] | 1120 | classesJars = append(classesJars, retrieveClassesJarsFromModule(module)...) |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 1121 | } |
| 1122 | return classesJars |
| 1123 | } |
Paul Duffin | 5f148ca | 2021-06-02 17:24:22 +0100 | [diff] [blame] | 1124 | |
Paul Duffin | dd5993f | 2021-06-10 10:18:22 +0100 | [diff] [blame] | 1125 | // retrieveClassesJarsFromModule retrieves the classes jars from the supplied module. |
| 1126 | func retrieveClassesJarsFromModule(module android.Module) android.Paths { |
| 1127 | if hiddenAPIModule, ok := module.(hiddenAPIModule); ok { |
| 1128 | return hiddenAPIModule.classesJars() |
| 1129 | } |
| 1130 | |
| 1131 | return nil |
| 1132 | } |
| 1133 | |
Paul Duffin | 5f148ca | 2021-06-02 17:24:22 +0100 | [diff] [blame] | 1134 | // deferReportingMissingBootDexJar returns true if a missing boot dex jar should not be reported by |
| 1135 | // Soong but should instead only be reported in ninja if the file is actually built. |
| 1136 | func deferReportingMissingBootDexJar(ctx android.ModuleContext, module android.Module) bool { |
| 1137 | // TODO(b/179354495): Remove this workaround when it is unnecessary. |
| 1138 | // Prebuilt modules like framework-wifi do not yet provide dex implementation jars. So, |
| 1139 | // create a fake one that will cause a build error only if it is used. |
| 1140 | if ctx.Config().AlwaysUsePrebuiltSdks() { |
| 1141 | return true |
| 1142 | } |
| 1143 | |
Paul Duffin | e521881 | 2021-06-07 13:28:19 +0100 | [diff] [blame] | 1144 | // Any missing dependency should be allowed. |
| 1145 | if ctx.Config().AllowMissingDependencies() { |
| 1146 | return true |
| 1147 | } |
| 1148 | |
Paul Duffin | ef083c9 | 2021-06-29 13:36:34 +0100 | [diff] [blame^] | 1149 | // A bootclasspath module that is part of a versioned sdk never provides a boot dex jar as there |
| 1150 | // is no equivalently versioned prebuilt APEX file from which it can be obtained. However, |
| 1151 | // versioned bootclasspath modules are processed by Soong so in order to avoid them causing build |
| 1152 | // failures missing boot dex jars need to be deferred. |
| 1153 | if android.IsModuleInVersionedSdk(ctx.Module()) { |
| 1154 | return true |
| 1155 | } |
| 1156 | |
Paul Duffin | 5f148ca | 2021-06-02 17:24:22 +0100 | [diff] [blame] | 1157 | // This is called for both platform_bootclasspath and bootclasspath_fragment modules. |
| 1158 | // |
| 1159 | // A bootclasspath_fragment module should only use the APEX variant of source or prebuilt modules. |
| 1160 | // Ideally, a bootclasspath_fragment module should never have a platform variant created for it |
| 1161 | // but unfortunately, due to b/187910671 it does. |
| 1162 | // |
| 1163 | // That causes issues when obtaining a boot dex jar for a prebuilt module as a prebuilt module |
| 1164 | // used by a bootclasspath_fragment can only provide a boot dex jar when it is part of APEX, i.e. |
| 1165 | // has an APEX variant not a platform variant. |
| 1166 | // |
| 1167 | // There are some other situations when a prebuilt module used by a bootclasspath_fragment cannot |
| 1168 | // provide a boot dex jar: |
| 1169 | // 1. If the bootclasspath_fragment is not exported by the prebuilt_apex/apex_set module then it |
| 1170 | // does not have an APEX variant and only has a platform variant and neither do its content |
| 1171 | // modules. |
| 1172 | // 2. Some build configurations, e.g. setting TARGET_BUILD_USE_PREBUILT_SDKS causes all |
| 1173 | // java_sdk_library_import modules to be treated as preferred and as many of them are not part |
| 1174 | // of an apex they cannot provide a boot dex jar. |
| 1175 | // |
| 1176 | // The first case causes problems when the affected prebuilt modules are preferred but that is an |
| 1177 | // invalid configuration and it is ok for it to fail as the work to enable that is not yet |
| 1178 | // complete. The second case is used for building targets that do not use boot dex jars and so |
| 1179 | // deferring error reporting to ninja is fine as the affected ninja targets should never be built. |
| 1180 | // That is handled above. |
| 1181 | // |
| 1182 | // A platform_bootclasspath module can use libraries from both platform and APEX variants. Unlike |
| 1183 | // the bootclasspath_fragment it supports dex_import modules which provides the dex file. So, it |
| 1184 | // can obtain a boot dex jar from a prebuilt that is not part of an APEX. However, it is assumed |
| 1185 | // that if the library can be part of an APEX then it is the APEX variant that is used. |
| 1186 | // |
| 1187 | // This check handles the slightly different requirements of the bootclasspath_fragment and |
| 1188 | // platform_bootclasspath modules by only deferring error reporting for the platform variant of |
| 1189 | // a prebuilt modules that has other variants which are part of an APEX. |
| 1190 | // |
| 1191 | // TODO(b/187910671): Remove this once platform variants are no longer created unnecessarily. |
| 1192 | if android.IsModulePrebuilt(module) { |
Paul Duffin | ef083c9 | 2021-06-29 13:36:34 +0100 | [diff] [blame^] | 1193 | // An inactive source module can still contribute to the APEX but an inactive prebuilt module |
| 1194 | // should not contribute to anything. So, rather than have a missing dex jar cause a Soong |
| 1195 | // failure defer the error reporting to Ninja. Unless the prebuilt build target is explicitly |
| 1196 | // built Ninja should never use the dex jar file. |
| 1197 | if !isActiveModule(module) { |
| 1198 | return true |
| 1199 | } |
| 1200 | |
Paul Duffin | 5f148ca | 2021-06-02 17:24:22 +0100 | [diff] [blame] | 1201 | if am, ok := module.(android.ApexModule); ok && am.InAnyApex() { |
| 1202 | apexInfo := ctx.OtherModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo) |
| 1203 | if apexInfo.IsForPlatform() { |
| 1204 | return true |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | |
Paul Duffin | 5f148ca | 2021-06-02 17:24:22 +0100 | [diff] [blame] | 1209 | return false |
| 1210 | } |
| 1211 | |
| 1212 | // handleMissingDexBootFile will either log a warning or create an error rule to create the fake |
| 1213 | // file depending on the value returned from deferReportingMissingBootDexJar. |
| 1214 | func handleMissingDexBootFile(ctx android.ModuleContext, module android.Module, fake android.WritablePath) { |
| 1215 | if deferReportingMissingBootDexJar(ctx, module) { |
| 1216 | // Create an error rule that pretends to create the output file but will actually fail if it |
| 1217 | // is run. |
| 1218 | ctx.Build(pctx, android.BuildParams{ |
| 1219 | Rule: android.ErrorRule, |
| 1220 | Output: fake, |
| 1221 | Args: map[string]string{ |
| 1222 | "error": fmt.Sprintf("missing dependencies: boot dex jar for %s", module), |
| 1223 | }, |
| 1224 | }) |
| 1225 | } else { |
| 1226 | ctx.ModuleErrorf("module %s does not provide a dex jar", module) |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | // retrieveEncodedBootDexJarFromModule returns a path to the boot dex jar from the supplied module's |
| 1231 | // DexJarBuildPath() method. |
| 1232 | // |
| 1233 | // The returned path will usually be to a dex jar file that has been encoded with hidden API flags. |
| 1234 | // However, under certain conditions, e.g. errors, or special build configurations it will return |
| 1235 | // a path to a fake file. |
| 1236 | func retrieveEncodedBootDexJarFromModule(ctx android.ModuleContext, module android.Module) android.Path { |
| 1237 | bootDexJar := module.(interface{ DexJarBuildPath() android.Path }).DexJarBuildPath() |
| 1238 | if bootDexJar == nil { |
| 1239 | fake := android.PathForModuleOut(ctx, fmt.Sprintf("fake/encoded-dex/%s.jar", module.Name())) |
| 1240 | bootDexJar = fake |
| 1241 | |
| 1242 | handleMissingDexBootFile(ctx, module, fake) |
| 1243 | } |
| 1244 | return bootDexJar |
| 1245 | } |
| 1246 | |
| 1247 | // extractEncodedDexJarsFromModules extracts the encoded dex jars from the supplied modules. |
| 1248 | func extractEncodedDexJarsFromModules(ctx android.ModuleContext, contents []android.Module) bootDexJarByModule { |
| 1249 | encodedDexJarsByModuleName := bootDexJarByModule{} |
| 1250 | for _, module := range contents { |
| 1251 | path := retrieveEncodedBootDexJarFromModule(ctx, module) |
| 1252 | encodedDexJarsByModuleName.addPath(module, path) |
| 1253 | } |
| 1254 | return encodedDexJarsByModuleName |
| 1255 | } |