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