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", |
| 92 | "android.test.base.stubs", |
| 93 | } |
| 94 | |
| 95 | // System API stubs |
| 96 | systemStubModules := []string{ |
| 97 | "android_system_stubs_current", |
| 98 | } |
| 99 | |
| 100 | // Test API stubs |
| 101 | testStubModules := []string{ |
| 102 | "android_test_stubs_current", |
| 103 | } |
| 104 | |
| 105 | // Core Platform API stubs |
| 106 | corePlatformStubModules := []string{ |
| 107 | "core.platform.api.stubs", |
| 108 | } |
| 109 | |
| 110 | // Allow products to define their own stubs for custom product jars that apps can use. |
| 111 | publicStubModules = append(publicStubModules, ctx.Config().ProductHiddenAPIStubs()...) |
| 112 | systemStubModules = append(systemStubModules, ctx.Config().ProductHiddenAPIStubsSystem()...) |
| 113 | testStubModules = append(testStubModules, ctx.Config().ProductHiddenAPIStubsTest()...) |
| 114 | |
| 115 | publicStubPaths := make(android.Paths, len(publicStubModules)) |
| 116 | systemStubPaths := make(android.Paths, len(systemStubModules)) |
| 117 | testStubPaths := make(android.Paths, len(testStubModules)) |
| 118 | corePlatformStubPaths := make(android.Paths, len(corePlatformStubModules)) |
| 119 | |
| 120 | moduleListToPathList := map[*[]string]android.Paths{ |
| 121 | &publicStubModules: publicStubPaths, |
| 122 | &systemStubModules: systemStubPaths, |
| 123 | &testStubModules: testStubPaths, |
| 124 | &corePlatformStubModules: corePlatformStubPaths, |
| 125 | } |
| 126 | |
| 127 | var bootDexJars android.Paths |
| 128 | |
| 129 | ctx.VisitAllModules(func(module android.Module) { |
| 130 | // Collect dex jar paths for the modules listed above. |
| 131 | if j, ok := module.(Dependency); ok { |
| 132 | name := ctx.ModuleName(module) |
| 133 | for moduleList, pathList := range moduleListToPathList { |
| 134 | if i := android.IndexList(name, *moduleList); i != -1 { |
| 135 | pathList[i] = j.DexJar() |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Collect dex jar paths for modules that had hiddenapi encode called on them. |
| 141 | if h, ok := module.(hiddenAPIIntf); ok { |
| 142 | if jar := h.bootDexJar(); jar != nil { |
| 143 | bootDexJars = append(bootDexJars, jar) |
| 144 | } |
| 145 | } |
| 146 | }) |
| 147 | |
| 148 | var missingDeps []string |
| 149 | // Ensure all modules were converted to paths |
| 150 | for moduleList, pathList := range moduleListToPathList { |
| 151 | for i := range pathList { |
| 152 | if pathList[i] == nil { |
| 153 | if ctx.Config().AllowMissingDependencies() { |
| 154 | missingDeps = append(missingDeps, (*moduleList)[i]) |
| 155 | pathList[i] = android.PathForOutput(ctx, "missing") |
| 156 | } else { |
| 157 | ctx.Errorf("failed to find dex jar path for module %q", |
| 158 | (*moduleList)[i]) |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Singleton rule which applies hiddenapi on all boot class path dex files. |
| 165 | rule := android.NewRuleBuilder() |
| 166 | |
| 167 | outputPath := hiddenAPISingletonPaths(ctx).stubFlags |
| 168 | tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp") |
| 169 | |
| 170 | rule.MissingDeps(missingDeps) |
| 171 | |
| 172 | rule.Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 173 | Tool(pctx.HostBinToolPath(ctx, "hiddenapi")). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 174 | Text("list"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 175 | FlagForEachInput("--boot-dex=", bootDexJars). |
| 176 | FlagWithInputList("--public-stub-classpath=", publicStubPaths, ":"). |
| 177 | FlagWithInputList("--public-stub-classpath=", systemStubPaths, ":"). |
| 178 | FlagWithInputList("--public-stub-classpath=", testStubPaths, ":"). |
| 179 | FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths, ":"). |
| 180 | FlagWithOutput("--out-api-flags=", tempPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 181 | |
| 182 | commitChangeForRestat(rule, tempPath, outputPath) |
| 183 | |
| 184 | rule.Build(pctx, ctx, "hiddenAPIStubFlagsFile", "hiddenapi stub flags") |
| 185 | } |
| 186 | |
| 187 | // flagsRule creates a rule to build hiddenapi-flags.csv out of flags.csv files generated for boot image modules and |
| 188 | // the greylists. |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 189 | func flagsRule(ctx android.SingletonContext) android.Path { |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 190 | var flagsCSV android.Paths |
| 191 | |
| 192 | var greylistIgnoreConflicts android.Path |
| 193 | |
| 194 | ctx.VisitAllModules(func(module android.Module) { |
| 195 | if h, ok := module.(hiddenAPIIntf); ok { |
| 196 | if csv := h.flagsCSV(); csv != nil { |
| 197 | flagsCSV = append(flagsCSV, csv) |
| 198 | } |
| 199 | } else if ds, ok := module.(*Droidstubs); ok && ctx.ModuleName(module) == "hiddenapi-lists-docs" { |
| 200 | greylistIgnoreConflicts = ds.removedDexApiFile |
| 201 | } |
| 202 | }) |
| 203 | |
| 204 | if greylistIgnoreConflicts == nil { |
| 205 | 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] | 206 | return nil |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | rule := android.NewRuleBuilder() |
| 210 | |
| 211 | outputPath := hiddenAPISingletonPaths(ctx).flags |
| 212 | tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp") |
| 213 | |
| 214 | stubFlags := hiddenAPISingletonPaths(ctx).stubFlags |
| 215 | |
| 216 | rule.Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 217 | Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/generate_hiddenapi_lists.py")). |
| 218 | FlagWithInput("--csv ", stubFlags). |
| 219 | Inputs(flagsCSV). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 220 | FlagWithInput("--greylist ", |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 221 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist.txt")). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 222 | FlagWithInput("--greylist-ignore-conflicts ", |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 223 | greylistIgnoreConflicts). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 224 | FlagWithInput("--greylist-max-p ", |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 225 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-p.txt")). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 226 | FlagWithInput("--greylist-max-o-ignore-conflicts ", |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 227 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-o.txt")). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 228 | FlagWithInput("--blacklist ", |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 229 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-force-blacklist.txt")). |
| 230 | FlagWithOutput("--output ", tempPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 231 | |
| 232 | commitChangeForRestat(rule, tempPath, outputPath) |
| 233 | |
| 234 | rule.Build(pctx, ctx, "hiddenAPIFlagsFile", "hiddenapi flags") |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 235 | |
| 236 | return outputPath |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | // emptyFlagsRule creates a rule to build an empty hiddenapi-flags.csv, which is needed by master-art-host builds that |
| 240 | // 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] | 241 | func emptyFlagsRule(ctx android.SingletonContext) android.Path { |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 242 | rule := android.NewRuleBuilder() |
| 243 | |
| 244 | outputPath := hiddenAPISingletonPaths(ctx).flags |
| 245 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 246 | rule.Command().Text("rm").Flag("-f").Output(outputPath) |
| 247 | rule.Command().Text("touch").Output(outputPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 248 | |
| 249 | rule.Build(pctx, ctx, "emptyHiddenAPIFlagsFile", "empty hiddenapi flags") |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 250 | |
| 251 | return outputPath |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // metadataRule creates a rule to build hiddenapi-greylist.csv out of the metadata.csv files generated for boot image |
| 255 | // modules. |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 256 | func metadataRule(ctx android.SingletonContext) android.Path { |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 257 | var metadataCSV android.Paths |
| 258 | |
| 259 | ctx.VisitAllModules(func(module android.Module) { |
| 260 | if h, ok := module.(hiddenAPIIntf); ok { |
| 261 | if csv := h.metadataCSV(); csv != nil { |
| 262 | metadataCSV = append(metadataCSV, csv) |
| 263 | } |
| 264 | } |
| 265 | }) |
| 266 | |
| 267 | rule := android.NewRuleBuilder() |
| 268 | |
| 269 | outputPath := hiddenAPISingletonPaths(ctx).metadata |
| 270 | |
| 271 | rule.Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 272 | Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/merge_csv.py")). |
| 273 | Inputs(metadataCSV). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 274 | Text(">"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 275 | Output(outputPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 276 | |
| 277 | rule.Build(pctx, ctx, "hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata") |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 278 | |
| 279 | return outputPath |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | // commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different. It |
| 283 | // also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of |
| 284 | // the rule. |
| 285 | func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) { |
| 286 | rule.Restat() |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 287 | rule.Temporary(tempPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 288 | rule.Command(). |
| 289 | Text("("). |
| 290 | Text("if"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 291 | Text("cmp -s").Input(tempPath).Output(outputPath).Text(";"). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 292 | Text("then"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 293 | Text("rm").Input(tempPath).Text(";"). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 294 | Text("else"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 295 | Text("mv").Input(tempPath).Output(outputPath).Text(";"). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 296 | Text("fi"). |
| 297 | Text(")") |
| 298 | } |