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 | |
Paul Duffin | dd63d6d | 2021-02-03 18:34:00 +0000 | [diff] [blame] | 220 | // Get the configured non-updatable and updatable boot jars. |
| 221 | nonUpdatableBootJars := ctx.Config().NonUpdatableBootJars() |
| 222 | updatableBootJars := ctx.Config().UpdatableBootJars() |
| 223 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 224 | ctx.VisitAllModules(func(module android.Module) { |
| 225 | // Collect dex jar paths for the modules listed above. |
| 226 | if j, ok := module.(Dependency); ok { |
| 227 | name := ctx.ModuleName(module) |
| 228 | for moduleList, pathList := range moduleListToPathList { |
| 229 | if i := android.IndexList(name, *moduleList); i != -1 { |
Ulyana Trafimovich | 5539e7b | 2020-06-04 14:08:17 +0000 | [diff] [blame] | 230 | pathList[i] = j.DexJarBuildPath() |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // Collect dex jar paths for modules that had hiddenapi encode called on them. |
| 236 | if h, ok := module.(hiddenAPIIntf); ok { |
| 237 | if jar := h.bootDexJar(); jar != nil { |
Paul Duffin | dd63d6d | 2021-02-03 18:34:00 +0000 | [diff] [blame] | 238 | if !isModuleInConfiguredList(ctx, module, nonUpdatableBootJars) && |
| 239 | !isModuleInConfiguredList(ctx, module, updatableBootJars) { |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 240 | return |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 241 | } |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 242 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 243 | bootDexJars = append(bootDexJars, jar) |
| 244 | } |
| 245 | } |
| 246 | }) |
| 247 | |
| 248 | var missingDeps []string |
| 249 | // Ensure all modules were converted to paths |
| 250 | for moduleList, pathList := range moduleListToPathList { |
| 251 | for i := range pathList { |
| 252 | if pathList[i] == nil { |
Paul Duffin | 7f48eef | 2020-12-03 11:15:58 +0000 | [diff] [blame] | 253 | moduleName := (*moduleList)[i] |
| 254 | pathList[i] = android.PathForOutput(ctx, "missing/module", moduleName) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 255 | if ctx.Config().AllowMissingDependencies() { |
Paul Duffin | 7f48eef | 2020-12-03 11:15:58 +0000 | [diff] [blame] | 256 | missingDeps = append(missingDeps, moduleName) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 257 | } else { |
| 258 | ctx.Errorf("failed to find dex jar path for module %q", |
Paul Duffin | 7f48eef | 2020-12-03 11:15:58 +0000 | [diff] [blame] | 259 | moduleName) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // Singleton rule which applies hiddenapi on all boot class path dex files. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 266 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 267 | |
| 268 | outputPath := hiddenAPISingletonPaths(ctx).stubFlags |
| 269 | tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp") |
| 270 | |
| 271 | rule.MissingDeps(missingDeps) |
| 272 | |
| 273 | rule.Command(). |
Martin Stjernholm | 7260d06 | 2019-12-09 21:47:14 +0000 | [diff] [blame] | 274 | Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 275 | Text("list"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 276 | FlagForEachInput("--boot-dex=", bootDexJars). |
| 277 | FlagWithInputList("--public-stub-classpath=", publicStubPaths, ":"). |
Andrei Onea | e04da07 | 2019-03-01 17:44:13 +0000 | [diff] [blame] | 278 | FlagWithInputList("--system-stub-classpath=", systemStubPaths, ":"). |
| 279 | FlagWithInputList("--test-stub-classpath=", testStubPaths, ":"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 280 | FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths, ":"). |
| 281 | FlagWithOutput("--out-api-flags=", tempPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 282 | |
| 283 | commitChangeForRestat(rule, tempPath, outputPath) |
| 284 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 285 | rule.Build("hiddenAPIStubFlagsFile", "hiddenapi stub flags") |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Paul Duffin | dd63d6d | 2021-02-03 18:34:00 +0000 | [diff] [blame] | 288 | // Checks to see whether the supplied module variant is in the list of boot jars. |
| 289 | // |
| 290 | // This is similar to logic in getBootImageJar() so any changes needed here are likely to be needed |
| 291 | // there too. |
| 292 | // |
| 293 | // TODO(b/179354495): Avoid having to perform this type of check or if necessary dedup it. |
| 294 | func isModuleInConfiguredList(ctx android.SingletonContext, module android.Module, configuredBootJars android.ConfiguredJarList) bool { |
| 295 | name := ctx.ModuleName(module) |
| 296 | |
| 297 | // Strip a prebuilt_ prefix so that this can match a prebuilt module that has not been renamed. |
| 298 | name = android.RemoveOptionalPrebuiltPrefix(name) |
| 299 | |
| 300 | // Ignore any module that is not listed in the boot image configuration. |
| 301 | index := configuredBootJars.IndexOfJar(name) |
| 302 | if index == -1 { |
| 303 | return false |
| 304 | } |
| 305 | |
| 306 | // It is an error if the module is not an ApexModule. |
| 307 | if _, ok := module.(android.ApexModule); !ok { |
| 308 | ctx.Errorf("module %q configured in boot jars does not support being added to an apex", module) |
| 309 | return false |
| 310 | } |
| 311 | |
| 312 | apexInfo := ctx.ModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo) |
| 313 | |
| 314 | // Now match the apex part of the boot image configuration. |
| 315 | requiredApex := configuredBootJars.Apex(index) |
| 316 | if requiredApex == "platform" { |
| 317 | if len(apexInfo.InApexes) != 0 { |
| 318 | // A platform variant is required but this is for an apex so ignore it. |
| 319 | return false |
| 320 | } |
| 321 | } else if !apexInfo.InApexByBaseName(requiredApex) { |
| 322 | // An apex variant for a specific apex is required but this is the wrong apex. |
| 323 | return false |
| 324 | } |
| 325 | |
| 326 | return true |
| 327 | } |
| 328 | |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 329 | func prebuiltFlagsRule(ctx android.SingletonContext) android.Path { |
| 330 | outputPath := hiddenAPISingletonPaths(ctx).flags |
| 331 | inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-flags.csv") |
| 332 | |
| 333 | ctx.Build(pctx, android.BuildParams{ |
| 334 | Rule: android.Cp, |
| 335 | Output: outputPath, |
| 336 | Input: inputPath, |
| 337 | }) |
| 338 | |
| 339 | return outputPath |
| 340 | } |
| 341 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 342 | // 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] | 343 | // the unsupported API. |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 344 | func flagsRule(ctx android.SingletonContext) android.Path { |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 345 | var flagsCSV android.Paths |
Anton Hansson | b3cbd61 | 2020-10-06 12:04:34 +0100 | [diff] [blame] | 346 | var combinedRemovedApis android.Path |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 347 | |
| 348 | ctx.VisitAllModules(func(module android.Module) { |
| 349 | if h, ok := module.(hiddenAPIIntf); ok { |
| 350 | if csv := h.flagsCSV(); csv != nil { |
| 351 | flagsCSV = append(flagsCSV, csv) |
| 352 | } |
Anton Hansson | b3cbd61 | 2020-10-06 12:04:34 +0100 | [diff] [blame] | 353 | } else if g, ok := module.(*genrule.Module); ok { |
| 354 | if ctx.ModuleName(module) == "combined-removed-dex" { |
| 355 | if len(g.GeneratedSourceFiles()) != 1 || combinedRemovedApis != nil { |
| 356 | ctx.Errorf("Expected 1 combined-removed-dex module that generates 1 output file.") |
| 357 | } |
| 358 | combinedRemovedApis = g.GeneratedSourceFiles()[0] |
Artur Satayev | c7fb5c9 | 2020-03-25 16:48:49 +0000 | [diff] [blame] | 359 | } |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 360 | } |
| 361 | }) |
| 362 | |
Anton Hansson | b3cbd61 | 2020-10-06 12:04:34 +0100 | [diff] [blame] | 363 | if combinedRemovedApis == nil { |
| 364 | ctx.Errorf("Failed to find combined-removed-dex.") |
| 365 | } |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 366 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 367 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 368 | |
| 369 | outputPath := hiddenAPISingletonPaths(ctx).flags |
| 370 | tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp") |
| 371 | |
| 372 | stubFlags := hiddenAPISingletonPaths(ctx).stubFlags |
| 373 | |
| 374 | rule.Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 375 | Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/generate_hiddenapi_lists.py")). |
| 376 | FlagWithInput("--csv ", stubFlags). |
| 377 | Inputs(flagsCSV). |
Aleksei Kalinov | f0f5cdc | 2020-07-28 13:44:24 +0000 | [diff] [blame] | 378 | FlagWithInput("--unsupported ", |
Andrei Onea | ca79081 | 2020-08-04 15:34:35 +0100 | [diff] [blame] | 379 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-unsupported.txt")). |
Mathew Inwood | a44e8c5 | 2020-10-20 15:23:04 +0100 | [diff] [blame] | 380 | FlagWithInput("--unsupported ", combinedRemovedApis).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed"). |
Mathew Inwood | c1be2f8 | 2021-01-13 15:49:17 +0000 | [diff] [blame] | 381 | FlagWithInput("--max-target-r ", |
| 382 | 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] | 383 | FlagWithInput("--max-target-q ", |
Andrei Onea | ca79081 | 2020-08-04 15:34:35 +0100 | [diff] [blame] | 384 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-max-target-q.txt")). |
Aleksei Kalinov | f0f5cdc | 2020-07-28 13:44:24 +0000 | [diff] [blame] | 385 | FlagWithInput("--max-target-p ", |
Andrei Onea | ca79081 | 2020-08-04 15:34:35 +0100 | [diff] [blame] | 386 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-max-target-p.txt")). |
Mathew Inwood | a44e8c5 | 2020-10-20 15:23:04 +0100 | [diff] [blame] | 387 | FlagWithInput("--max-target-o ", android.PathForSource( |
Mathew Inwood | 1ef4ba9 | 2020-11-10 14:49:43 +0000 | [diff] [blame] | 388 | 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] | 389 | FlagWithInput("--blocked ", |
Andrei Onea | ca79081 | 2020-08-04 15:34:35 +0100 | [diff] [blame] | 390 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-force-blocked.txt")). |
Mathew Inwood | a44e8c5 | 2020-10-20 15:23:04 +0100 | [diff] [blame] | 391 | FlagWithInput("--unsupported ", android.PathForSource( |
| 392 | ctx, "frameworks/base/config/hiddenapi-unsupported-packages.txt")).Flag("--packages "). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 393 | FlagWithOutput("--output ", tempPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 394 | |
| 395 | commitChangeForRestat(rule, tempPath, outputPath) |
| 396 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 397 | rule.Build("hiddenAPIFlagsFile", "hiddenapi flags") |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 398 | |
| 399 | return outputPath |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | // emptyFlagsRule creates a rule to build an empty hiddenapi-flags.csv, which is needed by master-art-host builds that |
| 403 | // 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] | 404 | func emptyFlagsRule(ctx android.SingletonContext) android.Path { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 405 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 406 | |
| 407 | outputPath := hiddenAPISingletonPaths(ctx).flags |
| 408 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 409 | rule.Command().Text("rm").Flag("-f").Output(outputPath) |
| 410 | rule.Command().Text("touch").Output(outputPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 411 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 412 | rule.Build("emptyHiddenAPIFlagsFile", "empty hiddenapi flags") |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 413 | |
| 414 | return outputPath |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Andrei Onea | 4784197 | 2020-08-10 17:23:52 +0100 | [diff] [blame] | 417 | // 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] | 418 | // modules. |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 419 | func metadataRule(ctx android.SingletonContext) android.Path { |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 420 | var metadataCSV android.Paths |
| 421 | |
| 422 | ctx.VisitAllModules(func(module android.Module) { |
| 423 | if h, ok := module.(hiddenAPIIntf); ok { |
| 424 | if csv := h.metadataCSV(); csv != nil { |
| 425 | metadataCSV = append(metadataCSV, csv) |
| 426 | } |
| 427 | } |
| 428 | }) |
| 429 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 430 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 431 | |
| 432 | outputPath := hiddenAPISingletonPaths(ctx).metadata |
| 433 | |
| 434 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 435 | BuiltTool("merge_csv"). |
Artur Satayev | 79fac05 | 2020-01-20 19:11:33 +0000 | [diff] [blame] | 436 | FlagWithOutput("--output=", outputPath). |
| 437 | Inputs(metadataCSV) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 438 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 439 | rule.Build("hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata") |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 440 | |
| 441 | return outputPath |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different. It |
| 445 | // also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of |
| 446 | // the rule. |
| 447 | func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) { |
| 448 | rule.Restat() |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 449 | rule.Temporary(tempPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 450 | rule.Command(). |
| 451 | Text("("). |
| 452 | Text("if"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 453 | Text("cmp -s").Input(tempPath).Output(outputPath).Text(";"). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 454 | Text("then"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 455 | Text("rm").Input(tempPath).Text(";"). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 456 | Text("else"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 457 | Text("mv").Input(tempPath).Output(outputPath).Text(";"). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 458 | Text("fi"). |
| 459 | Text(")") |
| 460 | } |
Paul Duffin | 1b033f5 | 2019-06-10 14:15:04 +0100 | [diff] [blame] | 461 | |
| 462 | type hiddenAPIFlagsProperties struct { |
| 463 | // name of the file into which the flags will be copied. |
| 464 | Filename *string |
| 465 | } |
| 466 | |
| 467 | type hiddenAPIFlags struct { |
| 468 | android.ModuleBase |
| 469 | |
| 470 | properties hiddenAPIFlagsProperties |
| 471 | |
| 472 | outputFilePath android.OutputPath |
| 473 | } |
| 474 | |
| 475 | func (h *hiddenAPIFlags) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 476 | filename := String(h.properties.Filename) |
| 477 | |
| 478 | inputPath := hiddenAPISingletonPaths(ctx).flags |
| 479 | h.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath |
| 480 | |
| 481 | // This ensures that outputFilePath has the correct name for others to |
| 482 | // use, as the source file may have a different name. |
| 483 | ctx.Build(pctx, android.BuildParams{ |
| 484 | Rule: android.Cp, |
| 485 | Output: h.outputFilePath, |
| 486 | Input: inputPath, |
| 487 | }) |
| 488 | } |
| 489 | |
| 490 | func (h *hiddenAPIFlags) OutputFiles(tag string) (android.Paths, error) { |
| 491 | switch tag { |
| 492 | case "": |
| 493 | return android.Paths{h.outputFilePath}, nil |
| 494 | default: |
| 495 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | // hiddenapi-flags provides access to the hiddenapi-flags.csv file generated during the build. |
| 500 | func hiddenAPIFlagsFactory() android.Module { |
| 501 | module := &hiddenAPIFlags{} |
| 502 | module.AddProperties(&module.properties) |
| 503 | android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon) |
| 504 | return module |
| 505 | } |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 506 | |
| 507 | func hiddenAPIIndexSingletonFactory() android.Singleton { |
| 508 | return &hiddenAPIIndexSingleton{} |
| 509 | } |
| 510 | |
| 511 | type hiddenAPIIndexSingleton struct { |
| 512 | index android.Path |
| 513 | } |
| 514 | |
| 515 | func (h *hiddenAPIIndexSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 516 | // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true |
| 517 | if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") { |
| 518 | return |
| 519 | } |
| 520 | |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 521 | if ctx.Config().PrebuiltHiddenApiDir(ctx) != "" { |
| 522 | outputPath := hiddenAPISingletonPaths(ctx).index |
| 523 | inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-index.csv") |
| 524 | |
| 525 | ctx.Build(pctx, android.BuildParams{ |
| 526 | Rule: android.Cp, |
| 527 | Output: outputPath, |
| 528 | Input: inputPath, |
| 529 | }) |
| 530 | |
| 531 | h.index = outputPath |
| 532 | return |
| 533 | } |
| 534 | |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 535 | indexes := android.Paths{} |
| 536 | ctx.VisitAllModules(func(module android.Module) { |
| 537 | if h, ok := module.(hiddenAPIIntf); ok { |
| 538 | if h.indexCSV() != nil { |
| 539 | indexes = append(indexes, h.indexCSV()) |
| 540 | } |
| 541 | } |
| 542 | }) |
| 543 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 544 | rule := android.NewRuleBuilder(pctx, ctx) |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 545 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 546 | BuiltTool("merge_csv"). |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 547 | FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties"). |
| 548 | FlagWithOutput("--output=", hiddenAPISingletonPaths(ctx).index). |
| 549 | Inputs(indexes) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 550 | rule.Build("singleton-merged-hiddenapi-index", "Singleton merged Hidden API index") |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 551 | |
| 552 | h.index = hiddenAPISingletonPaths(ctx).index |
| 553 | } |
| 554 | |
| 555 | func (h *hiddenAPIIndexSingleton) MakeVars(ctx android.MakeVarsContext) { |
| 556 | if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") { |
| 557 | return |
| 558 | } |
| 559 | |
| 560 | ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_INDEX", h.index.String()) |
| 561 | } |