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