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