| 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 ( | 
|  | 18 | "android/soong/android" | 
|  | 19 | ) | 
|  | 20 |  | 
|  | 21 | func init() { | 
|  | 22 | android.RegisterSingletonType("hiddenapi", hiddenAPISingletonFactory) | 
|  | 23 | } | 
|  | 24 |  | 
|  | 25 | type hiddenAPISingletonPathsStruct struct { | 
|  | 26 | stubFlags android.OutputPath | 
|  | 27 | flags     android.OutputPath | 
|  | 28 | metadata  android.OutputPath | 
|  | 29 | } | 
|  | 30 |  | 
|  | 31 | var hiddenAPISingletonPathsKey = android.NewOnceKey("hiddenAPISingletonPathsKey") | 
|  | 32 |  | 
|  | 33 | // hiddenAPISingletonPaths creates all the paths for singleton files the first time it is called, which may be | 
|  | 34 | // from a ModuleContext that needs to reference a file that will be created by a singleton rule that hasn't | 
|  | 35 | // yet been created. | 
|  | 36 | func hiddenAPISingletonPaths(ctx android.PathContext) hiddenAPISingletonPathsStruct { | 
|  | 37 | return ctx.Config().Once(hiddenAPISingletonPathsKey, func() interface{} { | 
|  | 38 | return hiddenAPISingletonPathsStruct{ | 
|  | 39 | stubFlags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-stub-flags.txt"), | 
|  | 40 | flags:     android.PathForOutput(ctx, "hiddenapi", "hiddenapi-flags.csv"), | 
|  | 41 | metadata:  android.PathForOutput(ctx, "hiddenapi", "hiddenapi-greylist.csv"), | 
|  | 42 | } | 
|  | 43 | }).(hiddenAPISingletonPathsStruct) | 
|  | 44 | } | 
|  | 45 |  | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 46 | func hiddenAPISingletonFactory() android.Singleton { | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 47 | return &hiddenAPISingleton{} | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 48 | } | 
|  | 49 |  | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 50 | type hiddenAPISingleton struct { | 
|  | 51 | flags, metadata android.Path | 
|  | 52 | } | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 53 |  | 
|  | 54 | // hiddenAPI singleton rules | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 55 | func (h *hiddenAPISingleton) GenerateBuildActions(ctx android.SingletonContext) { | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 56 | // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true | 
|  | 57 | if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") { | 
|  | 58 | return | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | stubFlagsRule(ctx) | 
|  | 62 |  | 
|  | 63 | // These rules depend on files located in frameworks/base, skip them if running in a tree that doesn't have them. | 
|  | 64 | if ctx.Config().FrameworksBaseDirExists(ctx) { | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 65 | h.flags = flagsRule(ctx) | 
|  | 66 | h.metadata = metadataRule(ctx) | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 67 | } else { | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 68 | h.flags = emptyFlagsRule(ctx) | 
|  | 69 | } | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | // Export paths to Make.  INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/. | 
|  | 73 | // Both paths are used to call dist-for-goals. | 
|  | 74 | func (h *hiddenAPISingleton) MakeVars(ctx android.MakeVarsContext) { | 
|  | 75 | if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") { | 
|  | 76 | return | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_FLAGS", h.flags.String()) | 
|  | 80 |  | 
|  | 81 | if h.metadata != nil { | 
|  | 82 | ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_GREYLIST_METADATA", h.metadata.String()) | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 83 | } | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | // stubFlagsRule creates the rule to build hiddenapi-stub-flags.txt out of dex jars from stub modules and boot image | 
|  | 87 | // modules. | 
|  | 88 | func stubFlagsRule(ctx android.SingletonContext) { | 
|  | 89 | // Public API stubs | 
|  | 90 | publicStubModules := []string{ | 
|  | 91 | "android_stubs_current", | 
| Paul Duffin | 719fed4 | 2019-02-28 16:15:44 +0000 | [diff] [blame] | 92 | } | 
|  | 93 |  | 
|  | 94 | // Add the android.test.base to the set of stubs only if the android.test.base module is on | 
|  | 95 | // the boot jars list as the runtime will only enforce hiddenapi access against modules on | 
|  | 96 | // that list. | 
|  | 97 | if inList("android.test.base", ctx.Config().BootJars()) { | 
|  | 98 | publicStubModules = append(publicStubModules, "android.test.base.stubs") | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
|  | 101 | // System API stubs | 
|  | 102 | systemStubModules := []string{ | 
|  | 103 | "android_system_stubs_current", | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | // Test API stubs | 
|  | 107 | testStubModules := []string{ | 
|  | 108 | "android_test_stubs_current", | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | // Core Platform API stubs | 
|  | 112 | corePlatformStubModules := []string{ | 
|  | 113 | "core.platform.api.stubs", | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | // Allow products to define their own stubs for custom product jars that apps can use. | 
|  | 117 | publicStubModules = append(publicStubModules, ctx.Config().ProductHiddenAPIStubs()...) | 
|  | 118 | systemStubModules = append(systemStubModules, ctx.Config().ProductHiddenAPIStubsSystem()...) | 
|  | 119 | testStubModules = append(testStubModules, ctx.Config().ProductHiddenAPIStubsTest()...) | 
| Allen Hair | de816cf | 2019-02-25 16:37:42 -0800 | [diff] [blame] | 120 | if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") { | 
|  | 121 | publicStubModules = append(publicStubModules, "jacoco-stubs") | 
|  | 122 | } | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 123 |  | 
|  | 124 | publicStubPaths := make(android.Paths, len(publicStubModules)) | 
|  | 125 | systemStubPaths := make(android.Paths, len(systemStubModules)) | 
|  | 126 | testStubPaths := make(android.Paths, len(testStubModules)) | 
|  | 127 | corePlatformStubPaths := make(android.Paths, len(corePlatformStubModules)) | 
|  | 128 |  | 
|  | 129 | moduleListToPathList := map[*[]string]android.Paths{ | 
|  | 130 | &publicStubModules:       publicStubPaths, | 
|  | 131 | &systemStubModules:       systemStubPaths, | 
|  | 132 | &testStubModules:         testStubPaths, | 
|  | 133 | &corePlatformStubModules: corePlatformStubPaths, | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | var bootDexJars android.Paths | 
|  | 137 |  | 
|  | 138 | ctx.VisitAllModules(func(module android.Module) { | 
|  | 139 | // Collect dex jar paths for the modules listed above. | 
|  | 140 | if j, ok := module.(Dependency); ok { | 
|  | 141 | name := ctx.ModuleName(module) | 
|  | 142 | for moduleList, pathList := range moduleListToPathList { | 
|  | 143 | if i := android.IndexList(name, *moduleList); i != -1 { | 
|  | 144 | pathList[i] = j.DexJar() | 
|  | 145 | } | 
|  | 146 | } | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | // Collect dex jar paths for modules that had hiddenapi encode called on them. | 
|  | 150 | if h, ok := module.(hiddenAPIIntf); ok { | 
|  | 151 | if jar := h.bootDexJar(); jar != nil { | 
|  | 152 | bootDexJars = append(bootDexJars, jar) | 
|  | 153 | } | 
|  | 154 | } | 
|  | 155 | }) | 
|  | 156 |  | 
|  | 157 | var missingDeps []string | 
|  | 158 | // Ensure all modules were converted to paths | 
|  | 159 | for moduleList, pathList := range moduleListToPathList { | 
|  | 160 | for i := range pathList { | 
|  | 161 | if pathList[i] == nil { | 
|  | 162 | if ctx.Config().AllowMissingDependencies() { | 
|  | 163 | missingDeps = append(missingDeps, (*moduleList)[i]) | 
|  | 164 | pathList[i] = android.PathForOutput(ctx, "missing") | 
|  | 165 | } else { | 
|  | 166 | ctx.Errorf("failed to find dex jar path for module %q", | 
|  | 167 | (*moduleList)[i]) | 
|  | 168 | } | 
|  | 169 | } | 
|  | 170 | } | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | // Singleton rule which applies hiddenapi on all boot class path dex files. | 
|  | 174 | rule := android.NewRuleBuilder() | 
|  | 175 |  | 
|  | 176 | outputPath := hiddenAPISingletonPaths(ctx).stubFlags | 
|  | 177 | tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp") | 
|  | 178 |  | 
|  | 179 | rule.MissingDeps(missingDeps) | 
|  | 180 |  | 
|  | 181 | rule.Command(). | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 182 | Tool(pctx.HostBinToolPath(ctx, "hiddenapi")). | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 183 | Text("list"). | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 184 | FlagForEachInput("--boot-dex=", bootDexJars). | 
|  | 185 | FlagWithInputList("--public-stub-classpath=", publicStubPaths, ":"). | 
|  | 186 | FlagWithInputList("--public-stub-classpath=", systemStubPaths, ":"). | 
|  | 187 | FlagWithInputList("--public-stub-classpath=", testStubPaths, ":"). | 
|  | 188 | FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths, ":"). | 
|  | 189 | FlagWithOutput("--out-api-flags=", tempPath) | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 190 |  | 
|  | 191 | commitChangeForRestat(rule, tempPath, outputPath) | 
|  | 192 |  | 
|  | 193 | rule.Build(pctx, ctx, "hiddenAPIStubFlagsFile", "hiddenapi stub flags") | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | // flagsRule creates a rule to build hiddenapi-flags.csv out of flags.csv files generated for boot image modules and | 
|  | 197 | // the greylists. | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 198 | func flagsRule(ctx android.SingletonContext) android.Path { | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 199 | var flagsCSV android.Paths | 
|  | 200 |  | 
|  | 201 | var greylistIgnoreConflicts android.Path | 
|  | 202 |  | 
|  | 203 | ctx.VisitAllModules(func(module android.Module) { | 
|  | 204 | if h, ok := module.(hiddenAPIIntf); ok { | 
|  | 205 | if csv := h.flagsCSV(); csv != nil { | 
|  | 206 | flagsCSV = append(flagsCSV, csv) | 
|  | 207 | } | 
|  | 208 | } else if ds, ok := module.(*Droidstubs); ok && ctx.ModuleName(module) == "hiddenapi-lists-docs" { | 
|  | 209 | greylistIgnoreConflicts = ds.removedDexApiFile | 
|  | 210 | } | 
|  | 211 | }) | 
|  | 212 |  | 
|  | 213 | if greylistIgnoreConflicts == nil { | 
|  | 214 | ctx.Errorf("failed to find removed_dex_api_filename from hiddenapi-lists-docs module") | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 215 | return nil | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 216 | } | 
|  | 217 |  | 
|  | 218 | rule := android.NewRuleBuilder() | 
|  | 219 |  | 
|  | 220 | outputPath := hiddenAPISingletonPaths(ctx).flags | 
|  | 221 | tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp") | 
|  | 222 |  | 
|  | 223 | stubFlags := hiddenAPISingletonPaths(ctx).stubFlags | 
|  | 224 |  | 
|  | 225 | rule.Command(). | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 226 | Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/generate_hiddenapi_lists.py")). | 
|  | 227 | FlagWithInput("--csv ", stubFlags). | 
|  | 228 | Inputs(flagsCSV). | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 229 | FlagWithInput("--greylist ", | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 230 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist.txt")). | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 231 | FlagWithInput("--greylist-ignore-conflicts ", | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 232 | greylistIgnoreConflicts). | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 233 | FlagWithInput("--greylist-max-p ", | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 234 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-p.txt")). | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 235 | FlagWithInput("--greylist-max-o-ignore-conflicts ", | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 236 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-o.txt")). | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 237 | FlagWithInput("--blacklist ", | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 238 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-force-blacklist.txt")). | 
|  | 239 | FlagWithOutput("--output ", tempPath) | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 240 |  | 
|  | 241 | commitChangeForRestat(rule, tempPath, outputPath) | 
|  | 242 |  | 
|  | 243 | rule.Build(pctx, ctx, "hiddenAPIFlagsFile", "hiddenapi flags") | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 244 |  | 
|  | 245 | return outputPath | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 246 | } | 
|  | 247 |  | 
|  | 248 | // emptyFlagsRule creates a rule to build an empty hiddenapi-flags.csv, which is needed by master-art-host builds that | 
|  | 249 | // 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] | 250 | func emptyFlagsRule(ctx android.SingletonContext) android.Path { | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 251 | rule := android.NewRuleBuilder() | 
|  | 252 |  | 
|  | 253 | outputPath := hiddenAPISingletonPaths(ctx).flags | 
|  | 254 |  | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 255 | rule.Command().Text("rm").Flag("-f").Output(outputPath) | 
|  | 256 | rule.Command().Text("touch").Output(outputPath) | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 257 |  | 
|  | 258 | rule.Build(pctx, ctx, "emptyHiddenAPIFlagsFile", "empty hiddenapi flags") | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 259 |  | 
|  | 260 | return outputPath | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 261 | } | 
|  | 262 |  | 
|  | 263 | // metadataRule creates a rule to build hiddenapi-greylist.csv out of the metadata.csv files generated for boot image | 
|  | 264 | // modules. | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 265 | func metadataRule(ctx android.SingletonContext) android.Path { | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 266 | var metadataCSV android.Paths | 
|  | 267 |  | 
|  | 268 | ctx.VisitAllModules(func(module android.Module) { | 
|  | 269 | if h, ok := module.(hiddenAPIIntf); ok { | 
|  | 270 | if csv := h.metadataCSV(); csv != nil { | 
|  | 271 | metadataCSV = append(metadataCSV, csv) | 
|  | 272 | } | 
|  | 273 | } | 
|  | 274 | }) | 
|  | 275 |  | 
|  | 276 | rule := android.NewRuleBuilder() | 
|  | 277 |  | 
|  | 278 | outputPath := hiddenAPISingletonPaths(ctx).metadata | 
|  | 279 |  | 
|  | 280 | rule.Command(). | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 281 | Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/merge_csv.py")). | 
|  | 282 | Inputs(metadataCSV). | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 283 | Text(">"). | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 284 | Output(outputPath) | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 285 |  | 
|  | 286 | rule.Build(pctx, ctx, "hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata") | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 287 |  | 
|  | 288 | return outputPath | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 289 | } | 
|  | 290 |  | 
|  | 291 | // commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different.  It | 
|  | 292 | // also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of | 
|  | 293 | // the rule. | 
|  | 294 | func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) { | 
|  | 295 | rule.Restat() | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 296 | rule.Temporary(tempPath) | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 297 | rule.Command(). | 
|  | 298 | Text("("). | 
|  | 299 | Text("if"). | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 300 | Text("cmp -s").Input(tempPath).Output(outputPath).Text(";"). | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 301 | Text("then"). | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 302 | Text("rm").Input(tempPath).Text(";"). | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 303 | Text("else"). | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 304 | Text("mv").Input(tempPath).Output(outputPath).Text(";"). | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 305 | Text("fi"). | 
|  | 306 | Text(")") | 
|  | 307 | } |