Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 1 | // Copyright 2019 Google Inc. All rights reserved. |
| 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 | 1b033f5 | 2019-06-10 14:15:04 +0100 | [diff] [blame] | 18 | "fmt" |
| 19 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 20 | "android/soong/android" |
Anton Hansson | b3cbd61 | 2020-10-06 12:04:34 +0100 | [diff] [blame] | 21 | "android/soong/genrule" |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | func init() { |
Paul Duffin | 01289a2 | 2021-02-04 17:49:33 +0000 | [diff] [blame] | 25 | RegisterHiddenApiSingletonComponents(android.InitRegistrationContext) |
| 26 | } |
| 27 | |
| 28 | func RegisterHiddenApiSingletonComponents(ctx android.RegistrationContext) { |
| 29 | ctx.RegisterSingletonType("hiddenapi", hiddenAPISingletonFactory) |
| 30 | ctx.RegisterSingletonType("hiddenapi_index", hiddenAPIIndexSingletonFactory) |
| 31 | ctx.RegisterModuleType("hiddenapi_flags", hiddenAPIFlagsFactory) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | type hiddenAPISingletonPathsStruct struct { |
Paul Duffin | ff774a0 | 2021-01-29 12:53:15 +0000 | [diff] [blame] | 35 | // The path to the CSV file that contains the flags that will be encoded into the dex boot jars. |
| 36 | // |
| 37 | // It is created by the generate_hiddenapi_lists.py tool that is passed the stubFlags along with |
| 38 | // a number of additional files that are used to augment the information in the stubFlags with |
| 39 | // manually curated data. |
| 40 | flags android.OutputPath |
| 41 | |
| 42 | // The path to the CSV index file that contains mappings from Java signature to source location |
| 43 | // information for all Java elements annotated with the UnsupportedAppUsage annotation in the |
| 44 | // source of all the boot jars. |
| 45 | // |
| 46 | // It is created by the merge_csv tool which merges all the hiddenAPI.indexCSVPath files that have |
| 47 | // been created by the rest of the build. That includes the index files generated for |
| 48 | // <x>-hiddenapi modules. |
| 49 | index android.OutputPath |
| 50 | |
| 51 | // The path to the CSV metadata file that contains mappings from Java signature to the value of |
| 52 | // properties specified on UnsupportedAppUsage annotations in the source of all the boot jars. |
| 53 | // |
| 54 | // It is created by the merge_csv tool which merges all the hiddenAPI.metadataCSVPath files that |
| 55 | // have been created by the rest of the build. That includes the metadata files generated for |
| 56 | // <x>-hiddenapi modules. |
| 57 | metadata android.OutputPath |
| 58 | |
| 59 | // The path to the CSV metadata file that contains mappings from Java signature to flags obtained |
| 60 | // from the public, system and test API stubs. |
| 61 | // |
| 62 | // This is created by the hiddenapi tool which is given dex files for the public, system and test |
| 63 | // API stubs (including product specific stubs) along with dex boot jars, so does not include |
| 64 | // <x>-hiddenapi modules. For each API surface (i.e. public, system, test) it records which |
| 65 | // members in the dex boot jars match a member in the dex stub jars for that API surface and then |
| 66 | // outputs a file containing the signatures of all members in the dex boot jars along with the |
| 67 | // flags that indicate which API surface it belongs, if any. |
| 68 | // |
| 69 | // e.g. a dex member that matches a member in the public dex stubs would have flags |
| 70 | // "public-api,system-api,test-api" set (as system and test are both supersets of public). A dex |
| 71 | // member that didn't match a member in any of the dex stubs is still output it just has an empty |
| 72 | // set of flags. |
| 73 | // |
| 74 | // The notion of matching is quite complex, it is not restricted to just exact matching but also |
| 75 | // follows the Java inheritance rules. e.g. if a method is public then all overriding/implementing |
| 76 | // methods are also public. If an interface method is public and a class inherits an |
| 77 | // implementation of that method from a super class then that super class method is also public. |
| 78 | // That ensures that any method that can be called directly by an App through a public method is |
| 79 | // visible to that App. |
| 80 | // |
| 81 | // Propagating the visibility of members across the inheritance hierarchy at build time will cause |
| 82 | // problems when modularizing and unbundling as it that propagation can cross module boundaries. |
| 83 | // e.g. Say that a private framework class implements a public interface and inherits an |
| 84 | // implementation of one of its methods from a core platform ART class. In that case the ART |
| 85 | // implementation method needs to be marked as public which requires the build to have access to |
| 86 | // the framework implementation classes at build time. The work to rectify this is being tracked |
| 87 | // at http://b/178693149. |
| 88 | // |
| 89 | // This file (or at least those items marked as being in the public-api) is used by hiddenapi when |
| 90 | // creating the metadata and flags for the individual modules in order to perform consistency |
| 91 | // checks and filter out bridge methods that are part of the public API. The latter relies on the |
| 92 | // propagation of visibility across the inheritance hierarchy. |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 93 | stubFlags android.OutputPath |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | var hiddenAPISingletonPathsKey = android.NewOnceKey("hiddenAPISingletonPathsKey") |
| 97 | |
| 98 | // hiddenAPISingletonPaths creates all the paths for singleton files the first time it is called, which may be |
| 99 | // from a ModuleContext that needs to reference a file that will be created by a singleton rule that hasn't |
| 100 | // yet been created. |
| 101 | func hiddenAPISingletonPaths(ctx android.PathContext) hiddenAPISingletonPathsStruct { |
| 102 | return ctx.Config().Once(hiddenAPISingletonPathsKey, func() interface{} { |
| 103 | return hiddenAPISingletonPathsStruct{ |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 104 | flags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-flags.csv"), |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 105 | index: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-index.csv"), |
Andrei Onea | 4784197 | 2020-08-10 17:23:52 +0100 | [diff] [blame] | 106 | metadata: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-unsupported.csv"), |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 107 | stubFlags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-stub-flags.txt"), |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 108 | } |
| 109 | }).(hiddenAPISingletonPathsStruct) |
| 110 | } |
| 111 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 112 | func hiddenAPISingletonFactory() android.Singleton { |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 113 | return &hiddenAPISingleton{} |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 116 | type hiddenAPISingleton struct { |
| 117 | flags, metadata android.Path |
| 118 | } |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 119 | |
| 120 | // hiddenAPI singleton rules |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 121 | func (h *hiddenAPISingleton) GenerateBuildActions(ctx android.SingletonContext) { |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 122 | // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true |
| 123 | if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") { |
| 124 | return |
| 125 | } |
| 126 | |
| 127 | stubFlagsRule(ctx) |
| 128 | |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 129 | // If there is a prebuilt hiddenapi dir, generate rules to use the |
| 130 | // files within. Generally, we build the hiddenapi files from source |
| 131 | // during the build, ensuring consistency. It's possible, in a split |
| 132 | // build (framework and vendor) scenario, for the vendor build to use |
| 133 | // prebuilt hiddenapi files from the framework build. In this scenario, |
| 134 | // the framework and vendor builds must use the same source to ensure |
| 135 | // consistency. |
| 136 | |
| 137 | if ctx.Config().PrebuiltHiddenApiDir(ctx) != "" { |
| 138 | h.flags = prebuiltFlagsRule(ctx) |
| 139 | return |
| 140 | } |
| 141 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 142 | // These rules depend on files located in frameworks/base, skip them if running in a tree that doesn't have them. |
Jiyong Park | 09cb629 | 2019-07-15 15:29:23 +0900 | [diff] [blame] | 143 | if ctx.Config().FrameworksBaseDirExists(ctx) { |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 144 | h.flags = flagsRule(ctx) |
| 145 | h.metadata = metadataRule(ctx) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 146 | } else { |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 147 | h.flags = emptyFlagsRule(ctx) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // Export paths to Make. INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/. |
| 152 | // Both paths are used to call dist-for-goals. |
| 153 | func (h *hiddenAPISingleton) MakeVars(ctx android.MakeVarsContext) { |
| 154 | if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") { |
| 155 | return |
| 156 | } |
| 157 | |
| 158 | ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_FLAGS", h.flags.String()) |
| 159 | |
| 160 | if h.metadata != nil { |
| 161 | ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_GREYLIST_METADATA", h.metadata.String()) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | // stubFlagsRule creates the rule to build hiddenapi-stub-flags.txt out of dex jars from stub modules and boot image |
| 166 | // modules. |
| 167 | func stubFlagsRule(ctx android.SingletonContext) { |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 168 | var publicStubModules []string |
| 169 | var systemStubModules []string |
| 170 | var testStubModules []string |
| 171 | var corePlatformStubModules []string |
| 172 | |
| 173 | if ctx.Config().AlwaysUsePrebuiltSdks() { |
| 174 | // Build configuration mandates using prebuilt stub modules |
| 175 | publicStubModules = append(publicStubModules, "sdk_public_current_android") |
| 176 | systemStubModules = append(systemStubModules, "sdk_system_current_android") |
| 177 | testStubModules = append(testStubModules, "sdk_test_current_android") |
| 178 | } else { |
| 179 | // Use stub modules built from source |
| 180 | publicStubModules = append(publicStubModules, "android_stubs_current") |
| 181 | systemStubModules = append(systemStubModules, "android_system_stubs_current") |
| 182 | testStubModules = append(testStubModules, "android_test_stubs_current") |
Paul Duffin | 719fed4 | 2019-02-28 16:15:44 +0000 | [diff] [blame] | 183 | } |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 184 | // We do not have prebuilts of the core platform api yet |
| 185 | corePlatformStubModules = append(corePlatformStubModules, "legacy.core.platform.api.stubs") |
Paul Duffin | 719fed4 | 2019-02-28 16:15:44 +0000 | [diff] [blame] | 186 | |
| 187 | // Add the android.test.base to the set of stubs only if the android.test.base module is on |
| 188 | // the boot jars list as the runtime will only enforce hiddenapi access against modules on |
| 189 | // that list. |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 190 | if inList("android.test.base", ctx.Config().BootJars()) { |
| 191 | if ctx.Config().AlwaysUsePrebuiltSdks() { |
| 192 | publicStubModules = append(publicStubModules, "sdk_public_current_android.test.base") |
| 193 | } else { |
| 194 | publicStubModules = append(publicStubModules, "android.test.base.stubs") |
| 195 | } |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // Allow products to define their own stubs for custom product jars that apps can use. |
| 199 | publicStubModules = append(publicStubModules, ctx.Config().ProductHiddenAPIStubs()...) |
| 200 | systemStubModules = append(systemStubModules, ctx.Config().ProductHiddenAPIStubsSystem()...) |
| 201 | testStubModules = append(testStubModules, ctx.Config().ProductHiddenAPIStubsTest()...) |
Allen Hair | de816cf | 2019-02-25 16:37:42 -0800 | [diff] [blame] | 202 | if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") { |
| 203 | publicStubModules = append(publicStubModules, "jacoco-stubs") |
| 204 | } |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 205 | |
| 206 | publicStubPaths := make(android.Paths, len(publicStubModules)) |
| 207 | systemStubPaths := make(android.Paths, len(systemStubModules)) |
| 208 | testStubPaths := make(android.Paths, len(testStubModules)) |
| 209 | corePlatformStubPaths := make(android.Paths, len(corePlatformStubModules)) |
| 210 | |
| 211 | moduleListToPathList := map[*[]string]android.Paths{ |
| 212 | &publicStubModules: publicStubPaths, |
| 213 | &systemStubModules: systemStubPaths, |
| 214 | &testStubModules: testStubPaths, |
| 215 | &corePlatformStubModules: corePlatformStubPaths, |
| 216 | } |
| 217 | |
| 218 | var bootDexJars android.Paths |
| 219 | |
| 220 | ctx.VisitAllModules(func(module android.Module) { |
| 221 | // Collect dex jar paths for the modules listed above. |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 222 | if j, ok := module.(UsesLibraryDependency); ok { |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 223 | name := ctx.ModuleName(module) |
| 224 | for moduleList, pathList := range moduleListToPathList { |
| 225 | if i := android.IndexList(name, *moduleList); i != -1 { |
Ulyana Trafimovich | 5539e7b | 2020-06-04 14:08:17 +0000 | [diff] [blame] | 226 | pathList[i] = j.DexJarBuildPath() |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // Collect dex jar paths for modules that had hiddenapi encode called on them. |
| 232 | if h, ok := module.(hiddenAPIIntf); ok { |
| 233 | if jar := h.bootDexJar(); jar != nil { |
| 234 | bootDexJars = append(bootDexJars, jar) |
| 235 | } |
| 236 | } |
| 237 | }) |
| 238 | |
| 239 | var missingDeps []string |
| 240 | // Ensure all modules were converted to paths |
| 241 | for moduleList, pathList := range moduleListToPathList { |
| 242 | for i := range pathList { |
| 243 | if pathList[i] == nil { |
Paul Duffin | 7f48eef | 2020-12-03 11:15:58 +0000 | [diff] [blame] | 244 | moduleName := (*moduleList)[i] |
| 245 | pathList[i] = android.PathForOutput(ctx, "missing/module", moduleName) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 246 | if ctx.Config().AllowMissingDependencies() { |
Paul Duffin | 7f48eef | 2020-12-03 11:15:58 +0000 | [diff] [blame] | 247 | missingDeps = append(missingDeps, moduleName) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 248 | } else { |
| 249 | ctx.Errorf("failed to find dex jar path for module %q", |
Paul Duffin | 7f48eef | 2020-12-03 11:15:58 +0000 | [diff] [blame] | 250 | moduleName) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // Singleton rule which applies hiddenapi on all boot class path dex files. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 257 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 258 | |
| 259 | outputPath := hiddenAPISingletonPaths(ctx).stubFlags |
| 260 | tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp") |
| 261 | |
| 262 | rule.MissingDeps(missingDeps) |
| 263 | |
| 264 | rule.Command(). |
Martin Stjernholm | 7260d06 | 2019-12-09 21:47:14 +0000 | [diff] [blame] | 265 | Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 266 | Text("list"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 267 | FlagForEachInput("--boot-dex=", bootDexJars). |
| 268 | FlagWithInputList("--public-stub-classpath=", publicStubPaths, ":"). |
Andrei Onea | e04da07 | 2019-03-01 17:44:13 +0000 | [diff] [blame] | 269 | FlagWithInputList("--system-stub-classpath=", systemStubPaths, ":"). |
| 270 | FlagWithInputList("--test-stub-classpath=", testStubPaths, ":"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 271 | FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths, ":"). |
| 272 | FlagWithOutput("--out-api-flags=", tempPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 273 | |
| 274 | commitChangeForRestat(rule, tempPath, outputPath) |
| 275 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 276 | rule.Build("hiddenAPIStubFlagsFile", "hiddenapi stub flags") |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Paul Duffin | dd63d6d | 2021-02-03 18:34:00 +0000 | [diff] [blame] | 279 | // Checks to see whether the supplied module variant is in the list of boot jars. |
| 280 | // |
| 281 | // This is similar to logic in getBootImageJar() so any changes needed here are likely to be needed |
| 282 | // there too. |
| 283 | // |
| 284 | // TODO(b/179354495): Avoid having to perform this type of check or if necessary dedup it. |
Paul Duffin | 82b3fcf | 2021-02-12 15:42:46 +0000 | [diff] [blame^] | 285 | func isModuleInConfiguredList(ctx android.BaseModuleContext, module android.Module, configuredBootJars android.ConfiguredJarList) bool { |
| 286 | name := ctx.OtherModuleName(module) |
Paul Duffin | dd63d6d | 2021-02-03 18:34:00 +0000 | [diff] [blame] | 287 | |
| 288 | // Strip a prebuilt_ prefix so that this can match a prebuilt module that has not been renamed. |
| 289 | name = android.RemoveOptionalPrebuiltPrefix(name) |
| 290 | |
| 291 | // Ignore any module that is not listed in the boot image configuration. |
| 292 | index := configuredBootJars.IndexOfJar(name) |
| 293 | if index == -1 { |
| 294 | return false |
| 295 | } |
| 296 | |
| 297 | // It is an error if the module is not an ApexModule. |
| 298 | if _, ok := module.(android.ApexModule); !ok { |
Paul Duffin | 82b3fcf | 2021-02-12 15:42:46 +0000 | [diff] [blame^] | 299 | ctx.ModuleErrorf("is configured in boot jars but does not support being added to an apex") |
Paul Duffin | dd63d6d | 2021-02-03 18:34:00 +0000 | [diff] [blame] | 300 | return false |
| 301 | } |
| 302 | |
Paul Duffin | 82b3fcf | 2021-02-12 15:42:46 +0000 | [diff] [blame^] | 303 | apexInfo := ctx.OtherModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo) |
Paul Duffin | dd63d6d | 2021-02-03 18:34:00 +0000 | [diff] [blame] | 304 | |
| 305 | // Now match the apex part of the boot image configuration. |
| 306 | requiredApex := configuredBootJars.Apex(index) |
| 307 | if requiredApex == "platform" { |
| 308 | if len(apexInfo.InApexes) != 0 { |
| 309 | // A platform variant is required but this is for an apex so ignore it. |
| 310 | return false |
| 311 | } |
| 312 | } else if !apexInfo.InApexByBaseName(requiredApex) { |
| 313 | // An apex variant for a specific apex is required but this is the wrong apex. |
| 314 | return false |
| 315 | } |
| 316 | |
| 317 | return true |
| 318 | } |
| 319 | |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 320 | func prebuiltFlagsRule(ctx android.SingletonContext) android.Path { |
| 321 | outputPath := hiddenAPISingletonPaths(ctx).flags |
| 322 | inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-flags.csv") |
| 323 | |
| 324 | ctx.Build(pctx, android.BuildParams{ |
| 325 | Rule: android.Cp, |
| 326 | Output: outputPath, |
| 327 | Input: inputPath, |
| 328 | }) |
| 329 | |
| 330 | return outputPath |
| 331 | } |
| 332 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 333 | // flagsRule creates a rule to build hiddenapi-flags.csv out of flags.csv files generated for boot image modules and |
Aleksei Kalinov | f0f5cdc | 2020-07-28 13:44:24 +0000 | [diff] [blame] | 334 | // the unsupported API. |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 335 | func flagsRule(ctx android.SingletonContext) android.Path { |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 336 | var flagsCSV android.Paths |
Anton Hansson | b3cbd61 | 2020-10-06 12:04:34 +0100 | [diff] [blame] | 337 | var combinedRemovedApis android.Path |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 338 | |
| 339 | ctx.VisitAllModules(func(module android.Module) { |
| 340 | if h, ok := module.(hiddenAPIIntf); ok { |
| 341 | if csv := h.flagsCSV(); csv != nil { |
| 342 | flagsCSV = append(flagsCSV, csv) |
| 343 | } |
Anton Hansson | b3cbd61 | 2020-10-06 12:04:34 +0100 | [diff] [blame] | 344 | } else if g, ok := module.(*genrule.Module); ok { |
| 345 | if ctx.ModuleName(module) == "combined-removed-dex" { |
| 346 | if len(g.GeneratedSourceFiles()) != 1 || combinedRemovedApis != nil { |
| 347 | ctx.Errorf("Expected 1 combined-removed-dex module that generates 1 output file.") |
| 348 | } |
| 349 | combinedRemovedApis = g.GeneratedSourceFiles()[0] |
Artur Satayev | c7fb5c9 | 2020-03-25 16:48:49 +0000 | [diff] [blame] | 350 | } |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 351 | } |
| 352 | }) |
| 353 | |
Anton Hansson | b3cbd61 | 2020-10-06 12:04:34 +0100 | [diff] [blame] | 354 | if combinedRemovedApis == nil { |
| 355 | ctx.Errorf("Failed to find combined-removed-dex.") |
| 356 | } |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 357 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 358 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 359 | |
| 360 | outputPath := hiddenAPISingletonPaths(ctx).flags |
| 361 | tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp") |
| 362 | |
| 363 | stubFlags := hiddenAPISingletonPaths(ctx).stubFlags |
| 364 | |
| 365 | rule.Command(). |
Paul Duffin | fdada68 | 2021-02-08 18:08:09 +0000 | [diff] [blame] | 366 | BuiltTool("generate_hiddenapi_lists"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 367 | FlagWithInput("--csv ", stubFlags). |
| 368 | Inputs(flagsCSV). |
Aleksei Kalinov | f0f5cdc | 2020-07-28 13:44:24 +0000 | [diff] [blame] | 369 | FlagWithInput("--unsupported ", |
Andrei Onea | ca79081 | 2020-08-04 15:34:35 +0100 | [diff] [blame] | 370 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-unsupported.txt")). |
Mathew Inwood | a44e8c5 | 2020-10-20 15:23:04 +0100 | [diff] [blame] | 371 | FlagWithInput("--unsupported ", combinedRemovedApis).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed"). |
Mathew Inwood | c1be2f8 | 2021-01-13 15:49:17 +0000 | [diff] [blame] | 372 | FlagWithInput("--max-target-r ", |
| 373 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-max-target-r-loprio.txt")).FlagWithArg("--tag ", "lo-prio"). |
Aleksei Kalinov | f0f5cdc | 2020-07-28 13:44:24 +0000 | [diff] [blame] | 374 | FlagWithInput("--max-target-q ", |
Andrei Onea | ca79081 | 2020-08-04 15:34:35 +0100 | [diff] [blame] | 375 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-max-target-q.txt")). |
Aleksei Kalinov | f0f5cdc | 2020-07-28 13:44:24 +0000 | [diff] [blame] | 376 | FlagWithInput("--max-target-p ", |
Andrei Onea | ca79081 | 2020-08-04 15:34:35 +0100 | [diff] [blame] | 377 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-max-target-p.txt")). |
Mathew Inwood | a44e8c5 | 2020-10-20 15:23:04 +0100 | [diff] [blame] | 378 | FlagWithInput("--max-target-o ", android.PathForSource( |
Mathew Inwood | 1ef4ba9 | 2020-11-10 14:49:43 +0000 | [diff] [blame] | 379 | ctx, "frameworks/base/config/hiddenapi-max-target-o.txt")).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "lo-prio"). |
Aleksei Kalinov | f0f5cdc | 2020-07-28 13:44:24 +0000 | [diff] [blame] | 380 | FlagWithInput("--blocked ", |
Andrei Onea | ca79081 | 2020-08-04 15:34:35 +0100 | [diff] [blame] | 381 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-force-blocked.txt")). |
Mathew Inwood | a44e8c5 | 2020-10-20 15:23:04 +0100 | [diff] [blame] | 382 | FlagWithInput("--unsupported ", android.PathForSource( |
| 383 | ctx, "frameworks/base/config/hiddenapi-unsupported-packages.txt")).Flag("--packages "). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 384 | FlagWithOutput("--output ", tempPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 385 | |
| 386 | commitChangeForRestat(rule, tempPath, outputPath) |
| 387 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 388 | rule.Build("hiddenAPIFlagsFile", "hiddenapi flags") |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 389 | |
| 390 | return outputPath |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | // emptyFlagsRule creates a rule to build an empty hiddenapi-flags.csv, which is needed by master-art-host builds that |
| 394 | // have a partial manifest without frameworks/base but still need to build a boot image. |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 395 | func emptyFlagsRule(ctx android.SingletonContext) android.Path { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 396 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 397 | |
| 398 | outputPath := hiddenAPISingletonPaths(ctx).flags |
| 399 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 400 | rule.Command().Text("rm").Flag("-f").Output(outputPath) |
| 401 | rule.Command().Text("touch").Output(outputPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 402 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 403 | rule.Build("emptyHiddenAPIFlagsFile", "empty hiddenapi flags") |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 404 | |
| 405 | return outputPath |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Andrei Onea | 4784197 | 2020-08-10 17:23:52 +0100 | [diff] [blame] | 408 | // metadataRule creates a rule to build hiddenapi-unsupported.csv out of the metadata.csv files generated for boot image |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 409 | // modules. |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 410 | func metadataRule(ctx android.SingletonContext) android.Path { |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 411 | var metadataCSV android.Paths |
| 412 | |
| 413 | ctx.VisitAllModules(func(module android.Module) { |
| 414 | if h, ok := module.(hiddenAPIIntf); ok { |
| 415 | if csv := h.metadataCSV(); csv != nil { |
| 416 | metadataCSV = append(metadataCSV, csv) |
| 417 | } |
| 418 | } |
| 419 | }) |
| 420 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 421 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 422 | |
| 423 | outputPath := hiddenAPISingletonPaths(ctx).metadata |
| 424 | |
| 425 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 426 | BuiltTool("merge_csv"). |
Artur Satayev | 79fac05 | 2020-01-20 19:11:33 +0000 | [diff] [blame] | 427 | FlagWithOutput("--output=", outputPath). |
| 428 | Inputs(metadataCSV) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 429 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 430 | rule.Build("hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata") |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 431 | |
| 432 | return outputPath |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | // commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different. It |
| 436 | // also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of |
| 437 | // the rule. |
| 438 | func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) { |
| 439 | rule.Restat() |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 440 | rule.Temporary(tempPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 441 | rule.Command(). |
| 442 | Text("("). |
| 443 | Text("if"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 444 | Text("cmp -s").Input(tempPath).Output(outputPath).Text(";"). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 445 | Text("then"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 446 | Text("rm").Input(tempPath).Text(";"). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 447 | Text("else"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 448 | Text("mv").Input(tempPath).Output(outputPath).Text(";"). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 449 | Text("fi"). |
| 450 | Text(")") |
| 451 | } |
Paul Duffin | 1b033f5 | 2019-06-10 14:15:04 +0100 | [diff] [blame] | 452 | |
| 453 | type hiddenAPIFlagsProperties struct { |
| 454 | // name of the file into which the flags will be copied. |
| 455 | Filename *string |
| 456 | } |
| 457 | |
| 458 | type hiddenAPIFlags struct { |
| 459 | android.ModuleBase |
| 460 | |
| 461 | properties hiddenAPIFlagsProperties |
| 462 | |
| 463 | outputFilePath android.OutputPath |
| 464 | } |
| 465 | |
| 466 | func (h *hiddenAPIFlags) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 467 | filename := String(h.properties.Filename) |
| 468 | |
| 469 | inputPath := hiddenAPISingletonPaths(ctx).flags |
| 470 | h.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath |
| 471 | |
| 472 | // This ensures that outputFilePath has the correct name for others to |
| 473 | // use, as the source file may have a different name. |
| 474 | ctx.Build(pctx, android.BuildParams{ |
| 475 | Rule: android.Cp, |
| 476 | Output: h.outputFilePath, |
| 477 | Input: inputPath, |
| 478 | }) |
| 479 | } |
| 480 | |
| 481 | func (h *hiddenAPIFlags) OutputFiles(tag string) (android.Paths, error) { |
| 482 | switch tag { |
| 483 | case "": |
| 484 | return android.Paths{h.outputFilePath}, nil |
| 485 | default: |
| 486 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | // hiddenapi-flags provides access to the hiddenapi-flags.csv file generated during the build. |
| 491 | func hiddenAPIFlagsFactory() android.Module { |
| 492 | module := &hiddenAPIFlags{} |
| 493 | module.AddProperties(&module.properties) |
| 494 | android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon) |
| 495 | return module |
| 496 | } |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 497 | |
| 498 | func hiddenAPIIndexSingletonFactory() android.Singleton { |
| 499 | return &hiddenAPIIndexSingleton{} |
| 500 | } |
| 501 | |
| 502 | type hiddenAPIIndexSingleton struct { |
| 503 | index android.Path |
| 504 | } |
| 505 | |
| 506 | func (h *hiddenAPIIndexSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 507 | // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true |
| 508 | if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") { |
| 509 | return |
| 510 | } |
| 511 | |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 512 | if ctx.Config().PrebuiltHiddenApiDir(ctx) != "" { |
| 513 | outputPath := hiddenAPISingletonPaths(ctx).index |
| 514 | inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-index.csv") |
| 515 | |
| 516 | ctx.Build(pctx, android.BuildParams{ |
| 517 | Rule: android.Cp, |
| 518 | Output: outputPath, |
| 519 | Input: inputPath, |
| 520 | }) |
| 521 | |
| 522 | h.index = outputPath |
| 523 | return |
| 524 | } |
| 525 | |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 526 | indexes := android.Paths{} |
| 527 | ctx.VisitAllModules(func(module android.Module) { |
| 528 | if h, ok := module.(hiddenAPIIntf); ok { |
| 529 | if h.indexCSV() != nil { |
| 530 | indexes = append(indexes, h.indexCSV()) |
| 531 | } |
| 532 | } |
| 533 | }) |
| 534 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 535 | rule := android.NewRuleBuilder(pctx, ctx) |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 536 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 537 | BuiltTool("merge_csv"). |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 538 | FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties"). |
| 539 | FlagWithOutput("--output=", hiddenAPISingletonPaths(ctx).index). |
| 540 | Inputs(indexes) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 541 | rule.Build("singleton-merged-hiddenapi-index", "Singleton merged Hidden API index") |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 542 | |
| 543 | h.index = hiddenAPISingletonPaths(ctx).index |
| 544 | } |
| 545 | |
| 546 | func (h *hiddenAPIIndexSingleton) MakeVars(ctx android.MakeVarsContext) { |
| 547 | if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") { |
| 548 | return |
| 549 | } |
| 550 | |
| 551 | ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_INDEX", h.index.String()) |
| 552 | } |