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 { |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 31 | flags android.OutputPath |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 32 | index android.OutputPath |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 33 | metadata android.OutputPath |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 34 | stubFlags android.OutputPath |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | var hiddenAPISingletonPathsKey = android.NewOnceKey("hiddenAPISingletonPathsKey") |
| 38 | |
| 39 | // hiddenAPISingletonPaths creates all the paths for singleton files the first time it is called, which may be |
| 40 | // from a ModuleContext that needs to reference a file that will be created by a singleton rule that hasn't |
| 41 | // yet been created. |
| 42 | func hiddenAPISingletonPaths(ctx android.PathContext) hiddenAPISingletonPathsStruct { |
| 43 | return ctx.Config().Once(hiddenAPISingletonPathsKey, func() interface{} { |
| 44 | return hiddenAPISingletonPathsStruct{ |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 45 | flags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-flags.csv"), |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 46 | index: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-index.csv"), |
Andrei Onea | 4784197 | 2020-08-10 17:23:52 +0100 | [diff] [blame] | 47 | metadata: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-unsupported.csv"), |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 48 | stubFlags: android.PathForOutput(ctx, "hiddenapi", "hiddenapi-stub-flags.txt"), |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 49 | } |
| 50 | }).(hiddenAPISingletonPathsStruct) |
| 51 | } |
| 52 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 53 | func hiddenAPISingletonFactory() android.Singleton { |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 54 | return &hiddenAPISingleton{} |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 57 | type hiddenAPISingleton struct { |
| 58 | flags, metadata android.Path |
| 59 | } |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 60 | |
| 61 | // hiddenAPI singleton rules |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 62 | func (h *hiddenAPISingleton) GenerateBuildActions(ctx android.SingletonContext) { |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 63 | // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true |
| 64 | if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") { |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | stubFlagsRule(ctx) |
| 69 | |
| 70 | // 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] | 71 | if ctx.Config().FrameworksBaseDirExists(ctx) { |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 72 | h.flags = flagsRule(ctx) |
| 73 | h.metadata = metadataRule(ctx) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 74 | } else { |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 75 | h.flags = emptyFlagsRule(ctx) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Export paths to Make. INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/. |
| 80 | // Both paths are used to call dist-for-goals. |
| 81 | func (h *hiddenAPISingleton) MakeVars(ctx android.MakeVarsContext) { |
| 82 | if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") { |
| 83 | return |
| 84 | } |
| 85 | |
| 86 | ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_FLAGS", h.flags.String()) |
| 87 | |
| 88 | if h.metadata != nil { |
| 89 | ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_GREYLIST_METADATA", h.metadata.String()) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | // stubFlagsRule creates the rule to build hiddenapi-stub-flags.txt out of dex jars from stub modules and boot image |
| 94 | // modules. |
| 95 | func stubFlagsRule(ctx android.SingletonContext) { |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 96 | var publicStubModules []string |
| 97 | var systemStubModules []string |
| 98 | var testStubModules []string |
| 99 | var corePlatformStubModules []string |
| 100 | |
| 101 | if ctx.Config().AlwaysUsePrebuiltSdks() { |
| 102 | // Build configuration mandates using prebuilt stub modules |
| 103 | publicStubModules = append(publicStubModules, "sdk_public_current_android") |
| 104 | systemStubModules = append(systemStubModules, "sdk_system_current_android") |
| 105 | testStubModules = append(testStubModules, "sdk_test_current_android") |
| 106 | } else { |
| 107 | // Use stub modules built from source |
| 108 | publicStubModules = append(publicStubModules, "android_stubs_current") |
| 109 | systemStubModules = append(systemStubModules, "android_system_stubs_current") |
| 110 | testStubModules = append(testStubModules, "android_test_stubs_current") |
Paul Duffin | 719fed4 | 2019-02-28 16:15:44 +0000 | [diff] [blame] | 111 | } |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 112 | // We do not have prebuilts of the core platform api yet |
| 113 | corePlatformStubModules = append(corePlatformStubModules, "legacy.core.platform.api.stubs") |
Paul Duffin | 719fed4 | 2019-02-28 16:15:44 +0000 | [diff] [blame] | 114 | |
| 115 | // Add the android.test.base to the set of stubs only if the android.test.base module is on |
| 116 | // the boot jars list as the runtime will only enforce hiddenapi access against modules on |
| 117 | // that list. |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 118 | if inList("android.test.base", ctx.Config().BootJars()) { |
| 119 | if ctx.Config().AlwaysUsePrebuiltSdks() { |
| 120 | publicStubModules = append(publicStubModules, "sdk_public_current_android.test.base") |
| 121 | } else { |
| 122 | publicStubModules = append(publicStubModules, "android.test.base.stubs") |
| 123 | } |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Allow products to define their own stubs for custom product jars that apps can use. |
| 127 | publicStubModules = append(publicStubModules, ctx.Config().ProductHiddenAPIStubs()...) |
| 128 | systemStubModules = append(systemStubModules, ctx.Config().ProductHiddenAPIStubsSystem()...) |
| 129 | testStubModules = append(testStubModules, ctx.Config().ProductHiddenAPIStubsTest()...) |
Allen Hair | de816cf | 2019-02-25 16:37:42 -0800 | [diff] [blame] | 130 | if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") { |
| 131 | publicStubModules = append(publicStubModules, "jacoco-stubs") |
| 132 | } |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 133 | |
| 134 | publicStubPaths := make(android.Paths, len(publicStubModules)) |
| 135 | systemStubPaths := make(android.Paths, len(systemStubModules)) |
| 136 | testStubPaths := make(android.Paths, len(testStubModules)) |
| 137 | corePlatformStubPaths := make(android.Paths, len(corePlatformStubModules)) |
| 138 | |
| 139 | moduleListToPathList := map[*[]string]android.Paths{ |
| 140 | &publicStubModules: publicStubPaths, |
| 141 | &systemStubModules: systemStubPaths, |
| 142 | &testStubModules: testStubPaths, |
| 143 | &corePlatformStubModules: corePlatformStubPaths, |
| 144 | } |
| 145 | |
| 146 | var bootDexJars android.Paths |
| 147 | |
| 148 | ctx.VisitAllModules(func(module android.Module) { |
| 149 | // Collect dex jar paths for the modules listed above. |
| 150 | if j, ok := module.(Dependency); ok { |
| 151 | name := ctx.ModuleName(module) |
| 152 | for moduleList, pathList := range moduleListToPathList { |
| 153 | if i := android.IndexList(name, *moduleList); i != -1 { |
Ulyana Trafimovich | 5539e7b | 2020-06-04 14:08:17 +0000 | [diff] [blame] | 154 | pathList[i] = j.DexJarBuildPath() |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Collect dex jar paths for modules that had hiddenapi encode called on them. |
| 160 | if h, ok := module.(hiddenAPIIntf); ok { |
| 161 | if jar := h.bootDexJar(); jar != nil { |
Jiyong Park | 4ed468c | 2019-12-19 02:11:10 +0000 | [diff] [blame] | 162 | // For a java lib included in an APEX, only take the one built for |
| 163 | // the platform variant, and skip the variants for APEXes. |
| 164 | // Otherwise, the hiddenapi tool will complain about duplicated classes |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 165 | apexInfo := ctx.ModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo) |
| 166 | if !apexInfo.IsForPlatform() { |
| 167 | return |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 168 | } |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 169 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 170 | bootDexJars = append(bootDexJars, jar) |
| 171 | } |
| 172 | } |
| 173 | }) |
| 174 | |
| 175 | var missingDeps []string |
| 176 | // Ensure all modules were converted to paths |
| 177 | for moduleList, pathList := range moduleListToPathList { |
| 178 | for i := range pathList { |
| 179 | if pathList[i] == nil { |
Paul Duffin | 7f48eef | 2020-12-03 11:15:58 +0000 | [diff] [blame] | 180 | moduleName := (*moduleList)[i] |
| 181 | pathList[i] = android.PathForOutput(ctx, "missing/module", moduleName) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 182 | if ctx.Config().AllowMissingDependencies() { |
Paul Duffin | 7f48eef | 2020-12-03 11:15:58 +0000 | [diff] [blame] | 183 | missingDeps = append(missingDeps, moduleName) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 184 | } else { |
| 185 | ctx.Errorf("failed to find dex jar path for module %q", |
Paul Duffin | 7f48eef | 2020-12-03 11:15:58 +0000 | [diff] [blame] | 186 | moduleName) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // Singleton rule which applies hiddenapi on all boot class path dex files. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 193 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 194 | |
| 195 | outputPath := hiddenAPISingletonPaths(ctx).stubFlags |
| 196 | tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp") |
| 197 | |
| 198 | rule.MissingDeps(missingDeps) |
| 199 | |
| 200 | rule.Command(). |
Martin Stjernholm | 7260d06 | 2019-12-09 21:47:14 +0000 | [diff] [blame] | 201 | Tool(ctx.Config().HostToolPath(ctx, "hiddenapi")). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 202 | Text("list"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 203 | FlagForEachInput("--boot-dex=", bootDexJars). |
| 204 | FlagWithInputList("--public-stub-classpath=", publicStubPaths, ":"). |
Andrei Onea | e04da07 | 2019-03-01 17:44:13 +0000 | [diff] [blame] | 205 | FlagWithInputList("--system-stub-classpath=", systemStubPaths, ":"). |
| 206 | FlagWithInputList("--test-stub-classpath=", testStubPaths, ":"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 207 | FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths, ":"). |
| 208 | FlagWithOutput("--out-api-flags=", tempPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 209 | |
| 210 | commitChangeForRestat(rule, tempPath, outputPath) |
| 211 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 212 | rule.Build("hiddenAPIStubFlagsFile", "hiddenapi stub flags") |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // 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] | 216 | // the unsupported API. |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 217 | func flagsRule(ctx android.SingletonContext) android.Path { |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 218 | var flagsCSV android.Paths |
Anton Hansson | b3cbd61 | 2020-10-06 12:04:34 +0100 | [diff] [blame] | 219 | var combinedRemovedApis android.Path |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 220 | |
| 221 | ctx.VisitAllModules(func(module android.Module) { |
| 222 | if h, ok := module.(hiddenAPIIntf); ok { |
| 223 | if csv := h.flagsCSV(); csv != nil { |
| 224 | flagsCSV = append(flagsCSV, csv) |
| 225 | } |
Anton Hansson | b3cbd61 | 2020-10-06 12:04:34 +0100 | [diff] [blame] | 226 | } else if g, ok := module.(*genrule.Module); ok { |
| 227 | if ctx.ModuleName(module) == "combined-removed-dex" { |
| 228 | if len(g.GeneratedSourceFiles()) != 1 || combinedRemovedApis != nil { |
| 229 | ctx.Errorf("Expected 1 combined-removed-dex module that generates 1 output file.") |
| 230 | } |
| 231 | combinedRemovedApis = g.GeneratedSourceFiles()[0] |
Artur Satayev | c7fb5c9 | 2020-03-25 16:48:49 +0000 | [diff] [blame] | 232 | } |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 233 | } |
| 234 | }) |
| 235 | |
Anton Hansson | b3cbd61 | 2020-10-06 12:04:34 +0100 | [diff] [blame] | 236 | if combinedRemovedApis == nil { |
| 237 | ctx.Errorf("Failed to find combined-removed-dex.") |
| 238 | } |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 239 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 240 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 241 | |
| 242 | outputPath := hiddenAPISingletonPaths(ctx).flags |
| 243 | tempPath := android.PathForOutput(ctx, outputPath.Rel()+".tmp") |
| 244 | |
| 245 | stubFlags := hiddenAPISingletonPaths(ctx).stubFlags |
| 246 | |
| 247 | rule.Command(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 248 | Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/generate_hiddenapi_lists.py")). |
| 249 | FlagWithInput("--csv ", stubFlags). |
| 250 | Inputs(flagsCSV). |
Aleksei Kalinov | f0f5cdc | 2020-07-28 13:44:24 +0000 | [diff] [blame] | 251 | FlagWithInput("--unsupported ", |
Andrei Onea | ca79081 | 2020-08-04 15:34:35 +0100 | [diff] [blame] | 252 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-unsupported.txt")). |
Mathew Inwood | a44e8c5 | 2020-10-20 15:23:04 +0100 | [diff] [blame] | 253 | FlagWithInput("--unsupported ", combinedRemovedApis).Flag("--ignore-conflicts ").FlagWithArg("--tag ", "removed"). |
Mathew Inwood | c1be2f8 | 2021-01-13 15:49:17 +0000 | [diff] [blame^] | 254 | FlagWithInput("--max-target-r ", |
| 255 | 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] | 256 | FlagWithInput("--max-target-q ", |
Andrei Onea | ca79081 | 2020-08-04 15:34:35 +0100 | [diff] [blame] | 257 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-max-target-q.txt")). |
Aleksei Kalinov | f0f5cdc | 2020-07-28 13:44:24 +0000 | [diff] [blame] | 258 | FlagWithInput("--max-target-p ", |
Andrei Onea | ca79081 | 2020-08-04 15:34:35 +0100 | [diff] [blame] | 259 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-max-target-p.txt")). |
Mathew Inwood | a44e8c5 | 2020-10-20 15:23:04 +0100 | [diff] [blame] | 260 | FlagWithInput("--max-target-o ", android.PathForSource( |
Mathew Inwood | 1ef4ba9 | 2020-11-10 14:49:43 +0000 | [diff] [blame] | 261 | 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] | 262 | FlagWithInput("--blocked ", |
Andrei Onea | ca79081 | 2020-08-04 15:34:35 +0100 | [diff] [blame] | 263 | android.PathForSource(ctx, "frameworks/base/config/hiddenapi-force-blocked.txt")). |
Mathew Inwood | a44e8c5 | 2020-10-20 15:23:04 +0100 | [diff] [blame] | 264 | FlagWithInput("--unsupported ", android.PathForSource( |
| 265 | ctx, "frameworks/base/config/hiddenapi-unsupported-packages.txt")).Flag("--packages "). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 266 | FlagWithOutput("--output ", tempPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 267 | |
| 268 | commitChangeForRestat(rule, tempPath, outputPath) |
| 269 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 270 | rule.Build("hiddenAPIFlagsFile", "hiddenapi flags") |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 271 | |
| 272 | return outputPath |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // emptyFlagsRule creates a rule to build an empty hiddenapi-flags.csv, which is needed by master-art-host builds that |
| 276 | // 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] | 277 | func emptyFlagsRule(ctx android.SingletonContext) android.Path { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 278 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 279 | |
| 280 | outputPath := hiddenAPISingletonPaths(ctx).flags |
| 281 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 282 | rule.Command().Text("rm").Flag("-f").Output(outputPath) |
| 283 | rule.Command().Text("touch").Output(outputPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 284 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 285 | rule.Build("emptyHiddenAPIFlagsFile", "empty hiddenapi flags") |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 286 | |
| 287 | return outputPath |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Andrei Onea | 4784197 | 2020-08-10 17:23:52 +0100 | [diff] [blame] | 290 | // 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] | 291 | // modules. |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 292 | func metadataRule(ctx android.SingletonContext) android.Path { |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 293 | var metadataCSV android.Paths |
| 294 | |
| 295 | ctx.VisitAllModules(func(module android.Module) { |
| 296 | if h, ok := module.(hiddenAPIIntf); ok { |
| 297 | if csv := h.metadataCSV(); csv != nil { |
| 298 | metadataCSV = append(metadataCSV, csv) |
| 299 | } |
| 300 | } |
| 301 | }) |
| 302 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 303 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 304 | |
| 305 | outputPath := hiddenAPISingletonPaths(ctx).metadata |
| 306 | |
| 307 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 308 | BuiltTool("merge_csv"). |
Artur Satayev | 79fac05 | 2020-01-20 19:11:33 +0000 | [diff] [blame] | 309 | FlagWithOutput("--output=", outputPath). |
| 310 | Inputs(metadataCSV) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 311 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 312 | rule.Build("hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata") |
Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 313 | |
| 314 | return outputPath |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different. It |
| 318 | // also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of |
| 319 | // the rule. |
| 320 | func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) { |
| 321 | rule.Restat() |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 322 | rule.Temporary(tempPath) |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 323 | rule.Command(). |
| 324 | Text("("). |
| 325 | Text("if"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 326 | Text("cmp -s").Input(tempPath).Output(outputPath).Text(";"). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 327 | Text("then"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 328 | Text("rm").Input(tempPath).Text(";"). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 329 | Text("else"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 330 | Text("mv").Input(tempPath).Output(outputPath).Text(";"). |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 331 | Text("fi"). |
| 332 | Text(")") |
| 333 | } |
Paul Duffin | 1b033f5 | 2019-06-10 14:15:04 +0100 | [diff] [blame] | 334 | |
| 335 | type hiddenAPIFlagsProperties struct { |
| 336 | // name of the file into which the flags will be copied. |
| 337 | Filename *string |
| 338 | } |
| 339 | |
| 340 | type hiddenAPIFlags struct { |
| 341 | android.ModuleBase |
| 342 | |
| 343 | properties hiddenAPIFlagsProperties |
| 344 | |
| 345 | outputFilePath android.OutputPath |
| 346 | } |
| 347 | |
| 348 | func (h *hiddenAPIFlags) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 349 | filename := String(h.properties.Filename) |
| 350 | |
| 351 | inputPath := hiddenAPISingletonPaths(ctx).flags |
| 352 | h.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath |
| 353 | |
| 354 | // This ensures that outputFilePath has the correct name for others to |
| 355 | // use, as the source file may have a different name. |
| 356 | ctx.Build(pctx, android.BuildParams{ |
| 357 | Rule: android.Cp, |
| 358 | Output: h.outputFilePath, |
| 359 | Input: inputPath, |
| 360 | }) |
| 361 | } |
| 362 | |
| 363 | func (h *hiddenAPIFlags) OutputFiles(tag string) (android.Paths, error) { |
| 364 | switch tag { |
| 365 | case "": |
| 366 | return android.Paths{h.outputFilePath}, nil |
| 367 | default: |
| 368 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // hiddenapi-flags provides access to the hiddenapi-flags.csv file generated during the build. |
| 373 | func hiddenAPIFlagsFactory() android.Module { |
| 374 | module := &hiddenAPIFlags{} |
| 375 | module.AddProperties(&module.properties) |
| 376 | android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon) |
| 377 | return module |
| 378 | } |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 379 | |
| 380 | func hiddenAPIIndexSingletonFactory() android.Singleton { |
| 381 | return &hiddenAPIIndexSingleton{} |
| 382 | } |
| 383 | |
| 384 | type hiddenAPIIndexSingleton struct { |
| 385 | index android.Path |
| 386 | } |
| 387 | |
| 388 | func (h *hiddenAPIIndexSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 389 | // Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true |
| 390 | if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") { |
| 391 | return |
| 392 | } |
| 393 | |
| 394 | indexes := android.Paths{} |
| 395 | ctx.VisitAllModules(func(module android.Module) { |
| 396 | if h, ok := module.(hiddenAPIIntf); ok { |
| 397 | if h.indexCSV() != nil { |
| 398 | indexes = append(indexes, h.indexCSV()) |
| 399 | } |
| 400 | } |
| 401 | }) |
| 402 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 403 | rule := android.NewRuleBuilder(pctx, ctx) |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 404 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 405 | BuiltTool("merge_csv"). |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 406 | FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties"). |
| 407 | FlagWithOutput("--output=", hiddenAPISingletonPaths(ctx).index). |
| 408 | Inputs(indexes) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 409 | rule.Build("singleton-merged-hiddenapi-index", "Singleton merged Hidden API index") |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 410 | |
| 411 | h.index = hiddenAPISingletonPaths(ctx).index |
| 412 | } |
| 413 | |
| 414 | func (h *hiddenAPIIndexSingleton) MakeVars(ctx android.MakeVarsContext) { |
| 415 | if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") { |
| 416 | return |
| 417 | } |
| 418 | |
| 419 | ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_INDEX", h.index.String()) |
| 420 | } |