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