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