Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 1 | // Copyright 2021 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 | "fmt" |
Anton Hansson | 86758ac | 2021-11-03 14:44:12 +0000 | [diff] [blame^] | 19 | "path/filepath" |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 20 | "strings" |
| 21 | |
| 22 | "github.com/google/blueprint/proptools" |
| 23 | |
| 24 | "android/soong/android" |
| 25 | "android/soong/java/config" |
| 26 | "android/soong/remoteexec" |
| 27 | ) |
| 28 | |
Pedro Loureiro | cc20350 | 2021-10-04 17:24:00 +0000 | [diff] [blame] | 29 | // The values allowed for Droidstubs' Api_levels_sdk_type |
| 30 | var allowedApiLevelSdkTypes = []string{"public", "system", "module-lib"} |
| 31 | |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 32 | func init() { |
| 33 | RegisterStubsBuildComponents(android.InitRegistrationContext) |
| 34 | } |
| 35 | |
| 36 | func RegisterStubsBuildComponents(ctx android.RegistrationContext) { |
| 37 | ctx.RegisterModuleType("stubs_defaults", StubsDefaultsFactory) |
| 38 | |
| 39 | ctx.RegisterModuleType("droidstubs", DroidstubsFactory) |
| 40 | ctx.RegisterModuleType("droidstubs_host", DroidstubsHostFactory) |
| 41 | |
| 42 | ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory) |
| 43 | } |
| 44 | |
| 45 | // |
| 46 | // Droidstubs |
| 47 | // |
| 48 | type Droidstubs struct { |
| 49 | Javadoc |
| 50 | android.SdkBase |
| 51 | |
| 52 | properties DroidstubsProperties |
| 53 | apiFile android.WritablePath |
| 54 | apiXmlFile android.WritablePath |
| 55 | lastReleasedApiXmlFile android.WritablePath |
| 56 | privateApiFile android.WritablePath |
| 57 | removedApiFile android.WritablePath |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 58 | nullabilityWarningsFile android.WritablePath |
| 59 | |
| 60 | checkCurrentApiTimestamp android.WritablePath |
| 61 | updateCurrentApiTimestamp android.WritablePath |
| 62 | checkLastReleasedApiTimestamp android.WritablePath |
| 63 | apiLintTimestamp android.WritablePath |
| 64 | apiLintReport android.WritablePath |
| 65 | |
| 66 | checkNullabilityWarningsTimestamp android.WritablePath |
| 67 | |
| 68 | annotationsZip android.WritablePath |
| 69 | apiVersionsXml android.WritablePath |
| 70 | |
| 71 | apiFilePath android.Path |
| 72 | removedApiFilePath android.Path |
| 73 | |
| 74 | metadataZip android.WritablePath |
| 75 | metadataDir android.WritablePath |
| 76 | } |
| 77 | |
| 78 | type DroidstubsProperties struct { |
| 79 | // The generated public API filename by Metalava, defaults to <module>_api.txt |
| 80 | Api_filename *string |
| 81 | |
| 82 | // the generated removed API filename by Metalava, defaults to <module>_removed.txt |
| 83 | Removed_api_filename *string |
| 84 | |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 85 | Check_api struct { |
| 86 | Last_released ApiToCheck |
| 87 | |
| 88 | Current ApiToCheck |
| 89 | |
| 90 | Api_lint struct { |
| 91 | Enabled *bool |
| 92 | |
| 93 | // If set, performs api_lint on any new APIs not found in the given signature file |
| 94 | New_since *string `android:"path"` |
| 95 | |
| 96 | // If not blank, path to the baseline txt file for approved API lint violations. |
| 97 | Baseline_file *string `android:"path"` |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // user can specify the version of previous released API file in order to do compatibility check. |
| 102 | Previous_api *string `android:"path"` |
| 103 | |
| 104 | // is set to true, Metalava will allow framework SDK to contain annotations. |
| 105 | Annotations_enabled *bool |
| 106 | |
| 107 | // a list of top-level directories containing files to merge qualifier annotations (i.e. those intended to be included in the stubs written) from. |
| 108 | Merge_annotations_dirs []string |
| 109 | |
| 110 | // a list of top-level directories containing Java stub files to merge show/hide annotations from. |
| 111 | Merge_inclusion_annotations_dirs []string |
| 112 | |
| 113 | // a file containing a list of classes to do nullability validation for. |
| 114 | Validate_nullability_from_list *string |
| 115 | |
| 116 | // a file containing expected warnings produced by validation of nullability annotations. |
| 117 | Check_nullability_warnings *string |
| 118 | |
| 119 | // if set to true, allow Metalava to generate doc_stubs source files. Defaults to false. |
| 120 | Create_doc_stubs *bool |
| 121 | |
| 122 | // if set to true, cause Metalava to output Javadoc comments in the stubs source files. Defaults to false. |
| 123 | // Has no effect if create_doc_stubs: true. |
| 124 | Output_javadoc_comments *bool |
| 125 | |
| 126 | // if set to false then do not write out stubs. Defaults to true. |
| 127 | // |
| 128 | // TODO(b/146727827): Remove capability when we do not need to generate stubs and API separately. |
| 129 | Generate_stubs *bool |
| 130 | |
| 131 | // if set to true, provides a hint to the build system that this rule uses a lot of memory, |
| 132 | // whicih can be used for scheduling purposes |
| 133 | High_mem *bool |
| 134 | |
satayev | 783195c | 2021-06-23 21:49:57 +0100 | [diff] [blame] | 135 | // if set to true, Metalava will allow framework SDK to contain API levels annotations. |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 136 | Api_levels_annotations_enabled *bool |
| 137 | |
| 138 | // the dirs which Metalava extracts API levels annotations from. |
| 139 | Api_levels_annotations_dirs []string |
| 140 | |
Pedro Loureiro | cc20350 | 2021-10-04 17:24:00 +0000 | [diff] [blame] | 141 | // the sdk kind which Metalava extracts API levels annotations from. Supports 'public', 'system' and 'module-lib' for now; defaults to public. |
satayev | 783195c | 2021-06-23 21:49:57 +0100 | [diff] [blame] | 142 | Api_levels_sdk_type *string |
| 143 | |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 144 | // the filename which Metalava extracts API levels annotations from. Defaults to android.jar. |
| 145 | Api_levels_jar_filename *string |
| 146 | |
| 147 | // if set to true, collect the values used by the Dev tools and |
| 148 | // write them in files packaged with the SDK. Defaults to false. |
| 149 | Write_sdk_values *bool |
| 150 | } |
| 151 | |
Anton Hansson | 5260932 | 2021-05-05 10:36:05 +0100 | [diff] [blame] | 152 | // Used by xsd_config |
| 153 | type ApiFilePath interface { |
| 154 | ApiFilePath() android.Path |
| 155 | } |
| 156 | |
| 157 | type ApiStubsSrcProvider interface { |
| 158 | StubsSrcJar() android.Path |
| 159 | } |
| 160 | |
| 161 | // Provider of information about API stubs, used by java_sdk_library. |
| 162 | type ApiStubsProvider interface { |
Anton Hansson | d78eb76 | 2021-09-21 15:25:12 +0100 | [diff] [blame] | 163 | AnnotationsZip() android.Path |
Anton Hansson | 5260932 | 2021-05-05 10:36:05 +0100 | [diff] [blame] | 164 | ApiFilePath |
| 165 | RemovedApiFilePath() android.Path |
| 166 | |
| 167 | ApiStubsSrcProvider |
| 168 | } |
| 169 | |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 170 | // droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be |
| 171 | // documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to |
| 172 | // a droiddoc module to generate documentation. |
| 173 | func DroidstubsFactory() android.Module { |
| 174 | module := &Droidstubs{} |
| 175 | |
| 176 | module.AddProperties(&module.properties, |
| 177 | &module.Javadoc.properties) |
| 178 | |
| 179 | InitDroiddocModule(module, android.HostAndDeviceSupported) |
| 180 | android.InitSdkAwareModule(module) |
| 181 | return module |
| 182 | } |
| 183 | |
| 184 | // droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API |
| 185 | // to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be |
| 186 | // passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs |
| 187 | // module when symbols needed by the source files are provided by java_library_host modules. |
| 188 | func DroidstubsHostFactory() android.Module { |
| 189 | module := &Droidstubs{} |
| 190 | |
| 191 | module.AddProperties(&module.properties, |
| 192 | &module.Javadoc.properties) |
| 193 | |
| 194 | InitDroiddocModule(module, android.HostSupported) |
| 195 | return module |
| 196 | } |
| 197 | |
| 198 | func (d *Droidstubs) OutputFiles(tag string) (android.Paths, error) { |
| 199 | switch tag { |
| 200 | case "": |
| 201 | return android.Paths{d.stubsSrcJar}, nil |
| 202 | case ".docs.zip": |
| 203 | return android.Paths{d.docZip}, nil |
| 204 | case ".api.txt", android.DefaultDistTag: |
| 205 | // This is the default dist path for dist properties that have no tag property. |
| 206 | return android.Paths{d.apiFilePath}, nil |
| 207 | case ".removed-api.txt": |
| 208 | return android.Paths{d.removedApiFilePath}, nil |
| 209 | case ".annotations.zip": |
| 210 | return android.Paths{d.annotationsZip}, nil |
| 211 | case ".api_versions.xml": |
| 212 | return android.Paths{d.apiVersionsXml}, nil |
| 213 | default: |
| 214 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 215 | } |
| 216 | } |
| 217 | |
Anton Hansson | d78eb76 | 2021-09-21 15:25:12 +0100 | [diff] [blame] | 218 | func (d *Droidstubs) AnnotationsZip() android.Path { |
| 219 | return d.annotationsZip |
| 220 | } |
| 221 | |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 222 | func (d *Droidstubs) ApiFilePath() android.Path { |
| 223 | return d.apiFilePath |
| 224 | } |
| 225 | |
| 226 | func (d *Droidstubs) RemovedApiFilePath() android.Path { |
| 227 | return d.removedApiFilePath |
| 228 | } |
| 229 | |
| 230 | func (d *Droidstubs) StubsSrcJar() android.Path { |
| 231 | return d.stubsSrcJar |
| 232 | } |
| 233 | |
| 234 | var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"} |
| 235 | var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"} |
| 236 | var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"} |
| 237 | |
| 238 | func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 239 | d.Javadoc.addDeps(ctx) |
| 240 | |
| 241 | if len(d.properties.Merge_annotations_dirs) != 0 { |
| 242 | for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs { |
| 243 | ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir) |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if len(d.properties.Merge_inclusion_annotations_dirs) != 0 { |
| 248 | for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs { |
| 249 | ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir) |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if len(d.properties.Api_levels_annotations_dirs) != 0 { |
| 254 | for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs { |
| 255 | ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir) |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath) { |
| 261 | if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") || |
| 262 | apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") || |
| 263 | String(d.properties.Api_filename) != "" { |
| 264 | filename := proptools.StringDefault(d.properties.Api_filename, ctx.ModuleName()+"_api.txt") |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 265 | d.apiFile = android.PathForModuleOut(ctx, "metalava", filename) |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 266 | cmd.FlagWithOutput("--api ", d.apiFile) |
| 267 | d.apiFilePath = d.apiFile |
| 268 | } else if sourceApiFile := proptools.String(d.properties.Check_api.Current.Api_file); sourceApiFile != "" { |
| 269 | // If check api is disabled then make the source file available for export. |
| 270 | d.apiFilePath = android.PathForModuleSrc(ctx, sourceApiFile) |
| 271 | } |
| 272 | |
| 273 | if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") || |
| 274 | apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") || |
| 275 | String(d.properties.Removed_api_filename) != "" { |
| 276 | filename := proptools.StringDefault(d.properties.Removed_api_filename, ctx.ModuleName()+"_removed.txt") |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 277 | d.removedApiFile = android.PathForModuleOut(ctx, "metalava", filename) |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 278 | cmd.FlagWithOutput("--removed-api ", d.removedApiFile) |
| 279 | d.removedApiFilePath = d.removedApiFile |
| 280 | } else if sourceRemovedApiFile := proptools.String(d.properties.Check_api.Current.Removed_api_file); sourceRemovedApiFile != "" { |
| 281 | // If check api is disabled then make the source removed api file available for export. |
| 282 | d.removedApiFilePath = android.PathForModuleSrc(ctx, sourceRemovedApiFile) |
| 283 | } |
| 284 | |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 285 | if Bool(d.properties.Write_sdk_values) { |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 286 | d.metadataDir = android.PathForModuleOut(ctx, "metalava", "metadata") |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 287 | cmd.FlagWithArg("--sdk-values ", d.metadataDir.String()) |
| 288 | } |
| 289 | |
| 290 | if stubsDir.Valid() { |
| 291 | if Bool(d.properties.Create_doc_stubs) { |
| 292 | cmd.FlagWithArg("--doc-stubs ", stubsDir.String()) |
| 293 | } else { |
| 294 | cmd.FlagWithArg("--stubs ", stubsDir.String()) |
| 295 | if !Bool(d.properties.Output_javadoc_comments) { |
| 296 | cmd.Flag("--exclude-documentation-from-stubs") |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) { |
| 303 | if Bool(d.properties.Annotations_enabled) { |
| 304 | cmd.Flag("--include-annotations") |
| 305 | |
Andrei Onea | 4985e51 | 2021-04-29 16:29:34 +0100 | [diff] [blame] | 306 | cmd.FlagWithArg("--exclude-annotation ", "androidx.annotation.RequiresApi") |
| 307 | |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 308 | validatingNullability := |
Colin Cross | bc13992 | 2021-03-25 18:33:16 -0700 | [diff] [blame] | 309 | strings.Contains(String(d.Javadoc.properties.Args), "--validate-nullability-from-merged-stubs") || |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 310 | String(d.properties.Validate_nullability_from_list) != "" |
| 311 | |
| 312 | migratingNullability := String(d.properties.Previous_api) != "" |
| 313 | if migratingNullability { |
| 314 | previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api)) |
| 315 | cmd.FlagWithInput("--migrate-nullness ", previousApi) |
| 316 | } |
| 317 | |
| 318 | if s := String(d.properties.Validate_nullability_from_list); s != "" { |
| 319 | cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s)) |
| 320 | } |
| 321 | |
| 322 | if validatingNullability { |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 323 | d.nullabilityWarningsFile = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"_nullability_warnings.txt") |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 324 | cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile) |
| 325 | } |
| 326 | |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 327 | d.annotationsZip = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"_annotations.zip") |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 328 | cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip) |
| 329 | |
| 330 | if len(d.properties.Merge_annotations_dirs) != 0 { |
| 331 | d.mergeAnnoDirFlags(ctx, cmd) |
| 332 | } |
| 333 | |
| 334 | // TODO(tnorbye): find owners to fix these warnings when annotation was enabled. |
| 335 | cmd.FlagWithArg("--hide ", "HiddenTypedefConstant"). |
| 336 | FlagWithArg("--hide ", "SuperfluousPrefix"). |
| 337 | FlagWithArg("--hide ", "AnnotationExtraction") |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) { |
| 342 | ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) { |
| 343 | if t, ok := m.(*ExportedDroiddocDir); ok { |
| 344 | cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps) |
| 345 | } else { |
| 346 | ctx.PropertyErrorf("merge_annotations_dirs", |
| 347 | "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m)) |
| 348 | } |
| 349 | }) |
| 350 | } |
| 351 | |
| 352 | func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) { |
| 353 | ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) { |
| 354 | if t, ok := m.(*ExportedDroiddocDir); ok { |
| 355 | cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps) |
| 356 | } else { |
| 357 | ctx.PropertyErrorf("merge_inclusion_annotations_dirs", |
| 358 | "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m)) |
| 359 | } |
| 360 | }) |
| 361 | } |
| 362 | |
| 363 | func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) { |
| 364 | if !Bool(d.properties.Api_levels_annotations_enabled) { |
| 365 | return |
| 366 | } |
| 367 | |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 368 | d.apiVersionsXml = android.PathForModuleOut(ctx, "metalava", "api-versions.xml") |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 369 | |
| 370 | if len(d.properties.Api_levels_annotations_dirs) == 0 { |
| 371 | ctx.PropertyErrorf("api_levels_annotations_dirs", |
| 372 | "has to be non-empty if api levels annotations was enabled!") |
| 373 | } |
| 374 | |
| 375 | cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml) |
| 376 | cmd.FlagWithInput("--apply-api-levels ", d.apiVersionsXml) |
| 377 | cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion().String()) |
| 378 | cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename()) |
| 379 | |
| 380 | filename := proptools.StringDefault(d.properties.Api_levels_jar_filename, "android.jar") |
| 381 | |
satayev | 783195c | 2021-06-23 21:49:57 +0100 | [diff] [blame] | 382 | var dirs []string |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 383 | ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) { |
| 384 | if t, ok := m.(*ExportedDroiddocDir); ok { |
| 385 | for _, dep := range t.deps { |
Colin Cross | 5f6ffc7 | 2021-03-29 21:54:45 -0700 | [diff] [blame] | 386 | if dep.Base() == filename { |
| 387 | cmd.Implicit(dep) |
| 388 | } |
| 389 | if filename != "android.jar" && dep.Base() == "android.jar" { |
| 390 | // Metalava implicitly searches these patterns: |
| 391 | // prebuilts/tools/common/api-versions/android-%/android.jar |
| 392 | // prebuilts/sdk/%/public/android.jar |
| 393 | // Add android.jar files from the api_levels_annotations_dirs directories to try |
| 394 | // to satisfy these patterns. If Metalava can't find a match for an API level |
| 395 | // between 1 and 28 in at least one pattern it will fail. |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 396 | cmd.Implicit(dep) |
| 397 | } |
| 398 | } |
satayev | 783195c | 2021-06-23 21:49:57 +0100 | [diff] [blame] | 399 | |
| 400 | dirs = append(dirs, t.dir.String()) |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 401 | } else { |
| 402 | ctx.PropertyErrorf("api_levels_annotations_dirs", |
| 403 | "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m)) |
| 404 | } |
| 405 | }) |
satayev | 783195c | 2021-06-23 21:49:57 +0100 | [diff] [blame] | 406 | |
| 407 | // Add all relevant --android-jar-pattern patterns for Metalava. |
| 408 | // When parsing a stub jar for a specific version, Metalava picks the first pattern that defines |
| 409 | // an actual file present on disk (in the order the patterns were passed). For system APIs for |
| 410 | // privileged apps that are only defined since API level 21 (Lollipop), fallback to public stubs |
Pedro Loureiro | cc20350 | 2021-10-04 17:24:00 +0000 | [diff] [blame] | 411 | // for older releases. Similarly, module-lib falls back to system API. |
| 412 | var sdkDirs []string |
| 413 | switch proptools.StringDefault(d.properties.Api_levels_sdk_type, "public") { |
| 414 | case "module-lib": |
| 415 | sdkDirs = []string{"module-lib", "system", "public"} |
| 416 | case "system": |
| 417 | sdkDirs = []string{"system", "public"} |
| 418 | case "public": |
| 419 | sdkDirs = []string{"public"} |
| 420 | default: |
| 421 | ctx.PropertyErrorf("api_levels_sdk_type", "needs to be one of %v", allowedApiLevelSdkTypes) |
| 422 | return |
satayev | 783195c | 2021-06-23 21:49:57 +0100 | [diff] [blame] | 423 | } |
Pedro Loureiro | cc20350 | 2021-10-04 17:24:00 +0000 | [diff] [blame] | 424 | |
| 425 | for _, sdkDir := range sdkDirs { |
| 426 | for _, dir := range dirs { |
| 427 | cmd.FlagWithArg("--android-jar-pattern ", fmt.Sprintf("%s/%%/%s/%s", dir, sdkDir, filename)) |
| 428 | } |
satayev | 783195c | 2021-06-23 21:49:57 +0100 | [diff] [blame] | 429 | } |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths, |
Anton Hansson | 556e814 | 2021-06-04 16:20:25 +0100 | [diff] [blame] | 433 | srcJarList android.Path, bootclasspath, classpath classpath, homeDir android.WritablePath) *android.RuleBuilderCommand { |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 434 | rule.Command().Text("rm -rf").Flag(homeDir.String()) |
| 435 | rule.Command().Text("mkdir -p").Flag(homeDir.String()) |
| 436 | |
Anton Hansson | 556e814 | 2021-06-04 16:20:25 +0100 | [diff] [blame] | 437 | cmd := rule.Command() |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 438 | cmd.FlagWithArg("ANDROID_PREFS_ROOT=", homeDir.String()) |
| 439 | |
| 440 | if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_METALAVA") { |
| 441 | rule.Remoteable(android.RemoteRuleSupports{RBE: true}) |
Colin Cross | 8095c29 | 2021-03-30 16:40:48 -0700 | [diff] [blame] | 442 | execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy) |
| 443 | labels := map[string]string{"type": "tool", "name": "metalava"} |
| 444 | // TODO: metalava pool rejects these jobs |
| 445 | pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "java16") |
| 446 | rule.Rewrapper(&remoteexec.REParams{ |
| 447 | Labels: labels, |
| 448 | ExecStrategy: execStrategy, |
| 449 | ToolchainInputs: []string{config.JavaCmd(ctx).String()}, |
| 450 | Platform: map[string]string{remoteexec.PoolKey: pool}, |
| 451 | }) |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Colin Cross | 6aa5c40 | 2021-03-24 12:28:50 -0700 | [diff] [blame] | 454 | cmd.BuiltTool("metalava").ImplicitTool(ctx.Config().HostJavaToolPath(ctx, "metalava.jar")). |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 455 | Flag(config.JavacVmFlags). |
| 456 | Flag("-J--add-opens=java.base/java.util=ALL-UNNAMED"). |
| 457 | FlagWithArg("-encoding ", "UTF-8"). |
| 458 | FlagWithArg("-source ", javaVersion.String()). |
| 459 | FlagWithRspFileInputList("@", android.PathForModuleOut(ctx, "metalava.rsp"), srcs). |
| 460 | FlagWithInput("@", srcJarList) |
| 461 | |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 462 | if len(bootclasspath) > 0 { |
| 463 | cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":") |
| 464 | } |
| 465 | |
| 466 | if len(classpath) > 0 { |
| 467 | cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":") |
| 468 | } |
| 469 | |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 470 | cmd.Flag("--no-banner"). |
| 471 | Flag("--color"). |
| 472 | Flag("--quiet"). |
| 473 | Flag("--format=v2"). |
| 474 | FlagWithArg("--repeat-errors-max ", "10"). |
| 475 | FlagWithArg("--hide ", "UnresolvedImport") |
| 476 | |
| 477 | return cmd |
| 478 | } |
| 479 | |
| 480 | func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 481 | deps := d.Javadoc.collectDeps(ctx) |
| 482 | |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 483 | javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), android.SdkContext(d)) |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 484 | |
| 485 | // Create rule for metalava |
| 486 | |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 487 | srcJarDir := android.PathForModuleOut(ctx, "metalava", "srcjars") |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 488 | |
| 489 | rule := android.NewRuleBuilder(pctx, ctx) |
| 490 | |
Colin Cross | 8095c29 | 2021-03-30 16:40:48 -0700 | [diff] [blame] | 491 | rule.Sbox(android.PathForModuleOut(ctx, "metalava"), |
| 492 | android.PathForModuleOut(ctx, "metalava.sbox.textproto")). |
| 493 | SandboxInputs() |
Colin Cross | 6aa5c40 | 2021-03-24 12:28:50 -0700 | [diff] [blame] | 494 | |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 495 | if BoolDefault(d.properties.High_mem, false) { |
| 496 | // This metalava run uses lots of memory, restrict the number of metalava jobs that can run in parallel. |
| 497 | rule.HighMem() |
| 498 | } |
| 499 | |
| 500 | generateStubs := BoolDefault(d.properties.Generate_stubs, true) |
| 501 | var stubsDir android.OptionalPath |
| 502 | if generateStubs { |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 503 | d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"-"+"stubs.srcjar") |
| 504 | stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "metalava", "stubsDir")) |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 505 | rule.Command().Text("rm -rf").Text(stubsDir.String()) |
| 506 | rule.Command().Text("mkdir -p").Text(stubsDir.String()) |
| 507 | } |
| 508 | |
| 509 | srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars) |
| 510 | |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 511 | homeDir := android.PathForModuleOut(ctx, "metalava", "home") |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 512 | cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList, |
Anton Hansson | 556e814 | 2021-06-04 16:20:25 +0100 | [diff] [blame] | 513 | deps.bootClasspath, deps.classpath, homeDir) |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 514 | cmd.Implicits(d.Javadoc.implicits) |
| 515 | |
| 516 | d.stubsFlags(ctx, cmd, stubsDir) |
| 517 | |
| 518 | d.annotationsFlags(ctx, cmd) |
| 519 | d.inclusionAnnotationsFlags(ctx, cmd) |
| 520 | d.apiLevelsAnnotationsFlags(ctx, cmd) |
| 521 | |
Colin Cross | bc13992 | 2021-03-25 18:33:16 -0700 | [diff] [blame] | 522 | d.expandArgs(ctx, cmd) |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 523 | |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 524 | for _, o := range d.Javadoc.properties.Out { |
| 525 | cmd.ImplicitOutput(android.PathForModuleGen(ctx, o)) |
| 526 | } |
| 527 | |
| 528 | // Add options for the other optional tasks: API-lint and check-released. |
| 529 | // We generate separate timestamp files for them. |
| 530 | |
| 531 | doApiLint := false |
| 532 | doCheckReleased := false |
| 533 | |
| 534 | // Add API lint options. |
| 535 | |
| 536 | if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) { |
| 537 | doApiLint = true |
| 538 | |
| 539 | newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since) |
| 540 | if newSince.Valid() { |
| 541 | cmd.FlagWithInput("--api-lint ", newSince.Path()) |
| 542 | } else { |
| 543 | cmd.Flag("--api-lint") |
| 544 | } |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 545 | d.apiLintReport = android.PathForModuleOut(ctx, "metalava", "api_lint_report.txt") |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 546 | cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport) // TODO: Change to ":api-lint" |
| 547 | |
Colin Cross | 0d53241 | 2021-03-25 09:38:45 -0700 | [diff] [blame] | 548 | // TODO(b/154317059): Clean up this allowlist by baselining and/or checking in last-released. |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 549 | if d.Name() != "android.car-system-stubs-docs" && |
| 550 | d.Name() != "android.car-stubs-docs" { |
| 551 | cmd.Flag("--lints-as-errors") |
| 552 | cmd.Flag("--warnings-as-errors") // Most lints are actually warnings. |
| 553 | } |
| 554 | |
| 555 | baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file) |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 556 | updatedBaselineOutput := android.PathForModuleOut(ctx, "metalava", "api_lint_baseline.txt") |
| 557 | d.apiLintTimestamp = android.PathForModuleOut(ctx, "metalava", "api_lint.timestamp") |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 558 | |
| 559 | // Note this string includes a special shell quote $' ... ', which decodes the "\n"s. |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 560 | // |
| 561 | // TODO: metalava also has a slightly different message hardcoded. Should we unify this |
| 562 | // message and metalava's one? |
| 563 | msg := `$'` + // Enclose with $' ... ' |
| 564 | `************************************************************\n` + |
| 565 | `Your API changes are triggering API Lint warnings or errors.\n` + |
| 566 | `To make these errors go away, fix the code according to the\n` + |
| 567 | `error and/or warning messages above.\n` + |
| 568 | `\n` + |
| 569 | `If it is not possible to do so, there are workarounds:\n` + |
| 570 | `\n` + |
Aurimas Liutikas | b23b745 | 2021-05-24 18:00:37 +0000 | [diff] [blame] | 571 | `1. You can suppress the errors with @SuppressLint("<id>")\n` + |
| 572 | ` where the <id> is given in brackets in the error message above.\n` |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 573 | |
| 574 | if baselineFile.Valid() { |
| 575 | cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path()) |
| 576 | cmd.FlagWithOutput("--update-baseline:api-lint ", updatedBaselineOutput) |
| 577 | |
| 578 | msg += fmt.Sprintf(``+ |
| 579 | `2. You can update the baseline by executing the following\n`+ |
| 580 | ` command:\n`+ |
Colin Cross | 63eeda0 | 2021-04-15 19:01:57 -0700 | [diff] [blame] | 581 | ` (cd $ANDROID_BUILD_TOP && cp \\\n`+ |
| 582 | ` "%s" \\\n`+ |
| 583 | ` "%s")\n`+ |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 584 | ` To submit the revised baseline.txt to the main Android\n`+ |
| 585 | ` repository, you will need approval.\n`, updatedBaselineOutput, baselineFile.Path()) |
| 586 | } else { |
| 587 | msg += fmt.Sprintf(``+ |
| 588 | `2. You can add a baseline file of existing lint failures\n`+ |
| 589 | ` to the build rule of %s.\n`, d.Name()) |
| 590 | } |
| 591 | // Note the message ends with a ' (single quote), to close the $' ... ' . |
| 592 | msg += `************************************************************\n'` |
| 593 | |
| 594 | cmd.FlagWithArg("--error-message:api-lint ", msg) |
| 595 | } |
| 596 | |
| 597 | // Add "check released" options. (Detect incompatible API changes from the last public release) |
| 598 | |
| 599 | if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") { |
| 600 | doCheckReleased = true |
| 601 | |
| 602 | if len(d.Javadoc.properties.Out) > 0 { |
| 603 | ctx.PropertyErrorf("out", "out property may not be combined with check_api") |
| 604 | } |
| 605 | |
| 606 | apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file)) |
| 607 | removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file)) |
| 608 | baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file) |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 609 | updatedBaselineOutput := android.PathForModuleOut(ctx, "metalava", "last_released_baseline.txt") |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 610 | |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 611 | d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "metalava", "check_last_released_api.timestamp") |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 612 | |
| 613 | cmd.FlagWithInput("--check-compatibility:api:released ", apiFile) |
| 614 | cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile) |
| 615 | |
| 616 | if baselineFile.Valid() { |
| 617 | cmd.FlagWithInput("--baseline:compatibility:released ", baselineFile.Path()) |
| 618 | cmd.FlagWithOutput("--update-baseline:compatibility:released ", updatedBaselineOutput) |
| 619 | } |
| 620 | |
| 621 | // Note this string includes quote ($' ... '), which decodes the "\n"s. |
| 622 | msg := `$'\n******************************\n` + |
| 623 | `You have tried to change the API from what has been previously released in\n` + |
| 624 | `an SDK. Please fix the errors listed above.\n` + |
| 625 | `******************************\n'` |
| 626 | |
| 627 | cmd.FlagWithArg("--error-message:compatibility:released ", msg) |
| 628 | } |
| 629 | |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 630 | if generateStubs { |
| 631 | rule.Command(). |
| 632 | BuiltTool("soong_zip"). |
| 633 | Flag("-write_if_changed"). |
| 634 | Flag("-jar"). |
| 635 | FlagWithOutput("-o ", d.Javadoc.stubsSrcJar). |
| 636 | FlagWithArg("-C ", stubsDir.String()). |
| 637 | FlagWithArg("-D ", stubsDir.String()) |
| 638 | } |
| 639 | |
| 640 | if Bool(d.properties.Write_sdk_values) { |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 641 | d.metadataZip = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"-metadata.zip") |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 642 | rule.Command(). |
| 643 | BuiltTool("soong_zip"). |
| 644 | Flag("-write_if_changed"). |
| 645 | Flag("-d"). |
| 646 | FlagWithOutput("-o ", d.metadataZip). |
| 647 | FlagWithArg("-C ", d.metadataDir.String()). |
| 648 | FlagWithArg("-D ", d.metadataDir.String()) |
| 649 | } |
| 650 | |
| 651 | // TODO: We don't really need two separate API files, but this is a reminiscence of how |
| 652 | // we used to run metalava separately for API lint and the "last_released" check. Unify them. |
| 653 | if doApiLint { |
| 654 | rule.Command().Text("touch").Output(d.apiLintTimestamp) |
| 655 | } |
| 656 | if doCheckReleased { |
| 657 | rule.Command().Text("touch").Output(d.checkLastReleasedApiTimestamp) |
| 658 | } |
| 659 | |
Colin Cross | 6aa5c40 | 2021-03-24 12:28:50 -0700 | [diff] [blame] | 660 | // TODO(b/183630617): rewrapper doesn't support restat rules |
Colin Cross | 8095c29 | 2021-03-30 16:40:48 -0700 | [diff] [blame] | 661 | // rule.Restat() |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 662 | |
| 663 | zipSyncCleanupCmd(rule, srcJarDir) |
| 664 | |
| 665 | rule.Build("metalava", "metalava merged") |
| 666 | |
| 667 | if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") { |
| 668 | |
| 669 | if len(d.Javadoc.properties.Out) > 0 { |
| 670 | ctx.PropertyErrorf("out", "out property may not be combined with check_api") |
| 671 | } |
| 672 | |
| 673 | apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file)) |
| 674 | removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file)) |
| 675 | baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file) |
| 676 | |
| 677 | if baselineFile.Valid() { |
| 678 | ctx.PropertyErrorf("baseline_file", "current API check can't have a baseline file. (module %s)", ctx.ModuleName()) |
| 679 | } |
| 680 | |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 681 | d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "metalava", "check_current_api.timestamp") |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 682 | |
| 683 | rule := android.NewRuleBuilder(pctx, ctx) |
| 684 | |
| 685 | // Diff command line. |
| 686 | // -F matches the closest "opening" line, such as "package android {" |
| 687 | // and " public class Intent {". |
| 688 | diff := `diff -u -F '{ *$'` |
| 689 | |
| 690 | rule.Command().Text("( true") |
| 691 | rule.Command(). |
| 692 | Text(diff). |
| 693 | Input(apiFile).Input(d.apiFile) |
| 694 | |
| 695 | rule.Command(). |
| 696 | Text(diff). |
| 697 | Input(removedApiFile).Input(d.removedApiFile) |
| 698 | |
| 699 | msg := fmt.Sprintf(`\n******************************\n`+ |
| 700 | `You have tried to change the API from what has been previously approved.\n\n`+ |
| 701 | `To make these errors go away, you have two choices:\n`+ |
| 702 | ` 1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)\n`+ |
| 703 | ` to the new methods, etc. shown in the above diff.\n\n`+ |
| 704 | ` 2. You can update current.txt and/or removed.txt by executing the following command:\n`+ |
| 705 | ` m %s-update-current-api\n\n`+ |
| 706 | ` To submit the revised current.txt to the main Android repository,\n`+ |
| 707 | ` you will need approval.\n`+ |
| 708 | `******************************\n`, ctx.ModuleName()) |
| 709 | |
| 710 | rule.Command(). |
| 711 | Text("touch").Output(d.checkCurrentApiTimestamp). |
| 712 | Text(") || ("). |
| 713 | Text("echo").Flag("-e").Flag(`"` + msg + `"`). |
| 714 | Text("; exit 38"). |
| 715 | Text(")") |
| 716 | |
| 717 | rule.Build("metalavaCurrentApiCheck", "check current API") |
| 718 | |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 719 | d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "metalava", "update_current_api.timestamp") |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 720 | |
| 721 | // update API rule |
| 722 | rule = android.NewRuleBuilder(pctx, ctx) |
| 723 | |
| 724 | rule.Command().Text("( true") |
| 725 | |
| 726 | rule.Command(). |
| 727 | Text("cp").Flag("-f"). |
| 728 | Input(d.apiFile).Flag(apiFile.String()) |
| 729 | |
| 730 | rule.Command(). |
| 731 | Text("cp").Flag("-f"). |
| 732 | Input(d.removedApiFile).Flag(removedApiFile.String()) |
| 733 | |
| 734 | msg = "failed to update public API" |
| 735 | |
| 736 | rule.Command(). |
| 737 | Text("touch").Output(d.updateCurrentApiTimestamp). |
| 738 | Text(") || ("). |
| 739 | Text("echo").Flag("-e").Flag(`"` + msg + `"`). |
| 740 | Text("; exit 38"). |
| 741 | Text(")") |
| 742 | |
| 743 | rule.Build("metalavaCurrentApiUpdate", "update current API") |
| 744 | } |
| 745 | |
| 746 | if String(d.properties.Check_nullability_warnings) != "" { |
| 747 | if d.nullabilityWarningsFile == nil { |
| 748 | ctx.PropertyErrorf("check_nullability_warnings", |
| 749 | "Cannot specify check_nullability_warnings unless validating nullability") |
| 750 | } |
| 751 | |
| 752 | checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings)) |
| 753 | |
Colin Cross | cb77f75 | 2021-03-24 12:04:44 -0700 | [diff] [blame] | 754 | d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "metalava", "check_nullability_warnings.timestamp") |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 755 | |
| 756 | msg := fmt.Sprintf(`\n******************************\n`+ |
| 757 | `The warnings encountered during nullability annotation validation did\n`+ |
| 758 | `not match the checked in file of expected warnings. The diffs are shown\n`+ |
| 759 | `above. You have two options:\n`+ |
| 760 | ` 1. Resolve the differences by editing the nullability annotations.\n`+ |
| 761 | ` 2. Update the file of expected warnings by running:\n`+ |
| 762 | ` cp %s %s\n`+ |
| 763 | ` and submitting the updated file as part of your change.`, |
| 764 | d.nullabilityWarningsFile, checkNullabilityWarnings) |
| 765 | |
| 766 | rule := android.NewRuleBuilder(pctx, ctx) |
| 767 | |
| 768 | rule.Command(). |
| 769 | Text("("). |
| 770 | Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile). |
| 771 | Text("&&"). |
| 772 | Text("touch").Output(d.checkNullabilityWarningsTimestamp). |
| 773 | Text(") || ("). |
| 774 | Text("echo").Flag("-e").Flag(`"` + msg + `"`). |
| 775 | Text("; exit 38"). |
| 776 | Text(")") |
| 777 | |
| 778 | rule.Build("nullabilityWarningsCheck", "nullability warnings check") |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | func StubsDefaultsFactory() android.Module { |
| 783 | module := &DocDefaults{} |
| 784 | |
| 785 | module.AddProperties( |
| 786 | &JavadocProperties{}, |
| 787 | &DroidstubsProperties{}, |
| 788 | ) |
| 789 | |
| 790 | android.InitDefaultsModule(module) |
| 791 | |
| 792 | return module |
| 793 | } |
| 794 | |
| 795 | var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil) |
| 796 | |
| 797 | type PrebuiltStubsSourcesProperties struct { |
| 798 | Srcs []string `android:"path"` |
| 799 | } |
| 800 | |
| 801 | type PrebuiltStubsSources struct { |
| 802 | android.ModuleBase |
| 803 | android.DefaultableModuleBase |
| 804 | prebuilt android.Prebuilt |
| 805 | android.SdkBase |
| 806 | |
| 807 | properties PrebuiltStubsSourcesProperties |
| 808 | |
Anton Hansson | 86758ac | 2021-11-03 14:44:12 +0000 | [diff] [blame^] | 809 | stubsSrcJar android.Path |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) { |
| 813 | switch tag { |
| 814 | case "": |
| 815 | return android.Paths{p.stubsSrcJar}, nil |
| 816 | default: |
| 817 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | func (d *PrebuiltStubsSources) StubsSrcJar() android.Path { |
| 822 | return d.stubsSrcJar |
| 823 | } |
| 824 | |
| 825 | func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 826 | if len(p.properties.Srcs) != 1 { |
Anton Hansson | 86758ac | 2021-11-03 14:44:12 +0000 | [diff] [blame^] | 827 | ctx.PropertyErrorf("srcs", "must only specify one directory path or srcjar, contains %d paths", len(p.properties.Srcs)) |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 828 | return |
| 829 | } |
| 830 | |
Anton Hansson | 86758ac | 2021-11-03 14:44:12 +0000 | [diff] [blame^] | 831 | src := p.properties.Srcs[0] |
| 832 | if filepath.Ext(src) == ".srcjar" { |
| 833 | // This is a srcjar. We can use it directly. |
| 834 | p.stubsSrcJar = android.PathForModuleSrc(ctx, src) |
| 835 | } else { |
| 836 | outPath := android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar") |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 837 | |
Anton Hansson | 86758ac | 2021-11-03 14:44:12 +0000 | [diff] [blame^] | 838 | // This is a directory. Glob the contents just in case the directory does not exist. |
| 839 | srcGlob := src + "/**/*" |
| 840 | srcPaths := android.PathsForModuleSrc(ctx, []string{srcGlob}) |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 841 | |
Anton Hansson | 86758ac | 2021-11-03 14:44:12 +0000 | [diff] [blame^] | 842 | // Although PathForModuleSrc can return nil if either the path doesn't exist or |
| 843 | // the path components are invalid it won't in this case because no components |
| 844 | // are specified and the module directory must exist in order to get this far. |
| 845 | srcDir := android.PathForModuleSrc(ctx).(android.SourcePath).Join(ctx, src) |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 846 | |
Anton Hansson | 86758ac | 2021-11-03 14:44:12 +0000 | [diff] [blame^] | 847 | rule := android.NewRuleBuilder(pctx, ctx) |
| 848 | rule.Command(). |
| 849 | BuiltTool("soong_zip"). |
| 850 | Flag("-write_if_changed"). |
| 851 | Flag("-jar"). |
| 852 | FlagWithOutput("-o ", outPath). |
| 853 | FlagWithArg("-C ", srcDir.String()). |
| 854 | FlagWithRspFileInputList("-r ", outPath.ReplaceExtension(ctx, "rsp"), srcPaths) |
| 855 | rule.Restat() |
| 856 | rule.Build("zip src", "Create srcjar from prebuilt source") |
| 857 | p.stubsSrcJar = outPath |
| 858 | } |
Colin Cross | 2207f87 | 2021-03-24 12:39:08 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt { |
| 862 | return &p.prebuilt |
| 863 | } |
| 864 | |
| 865 | func (p *PrebuiltStubsSources) Name() string { |
| 866 | return p.prebuilt.Name(p.ModuleBase.Name()) |
| 867 | } |
| 868 | |
| 869 | // prebuilt_stubs_sources imports a set of java source files as if they were |
| 870 | // generated by droidstubs. |
| 871 | // |
| 872 | // By default, a prebuilt_stubs_sources has a single variant that expects a |
| 873 | // set of `.java` files generated by droidstubs. |
| 874 | // |
| 875 | // Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one |
| 876 | // for host modules. |
| 877 | // |
| 878 | // Intended only for use by sdk snapshots. |
| 879 | func PrebuiltStubsSourcesFactory() android.Module { |
| 880 | module := &PrebuiltStubsSources{} |
| 881 | |
| 882 | module.AddProperties(&module.properties) |
| 883 | |
| 884 | android.InitPrebuiltModule(module, &module.properties.Srcs) |
| 885 | android.InitSdkAwareModule(module) |
| 886 | InitDroiddocModule(module, android.HostAndDeviceSupported) |
| 887 | return module |
| 888 | } |