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