1*333d2b36SAndroid Build Coastguard Worker// Copyright 2021 Google Inc. All rights reserved. 2*333d2b36SAndroid Build Coastguard Worker// 3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*333d2b36SAndroid Build Coastguard Worker// 7*333d2b36SAndroid Build Coastguard Worker// http://www.apache.org/licenses/LICENSE-2.0 8*333d2b36SAndroid Build Coastguard Worker// 9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*333d2b36SAndroid Build Coastguard Worker// limitations under the License. 14*333d2b36SAndroid Build Coastguard Worker 15*333d2b36SAndroid Build Coastguard Workerpackage java 16*333d2b36SAndroid Build Coastguard Worker 17*333d2b36SAndroid Build Coastguard Workerimport ( 18*333d2b36SAndroid Build Coastguard Worker "fmt" 19*333d2b36SAndroid Build Coastguard Worker "path/filepath" 20*333d2b36SAndroid Build Coastguard Worker "regexp" 21*333d2b36SAndroid Build Coastguard Worker "strings" 22*333d2b36SAndroid Build Coastguard Worker 23*333d2b36SAndroid Build Coastguard Worker "github.com/google/blueprint/proptools" 24*333d2b36SAndroid Build Coastguard Worker 25*333d2b36SAndroid Build Coastguard Worker "android/soong/android" 26*333d2b36SAndroid Build Coastguard Worker "android/soong/java/config" 27*333d2b36SAndroid Build Coastguard Worker "android/soong/remoteexec" 28*333d2b36SAndroid Build Coastguard Worker) 29*333d2b36SAndroid Build Coastguard Worker 30*333d2b36SAndroid Build Coastguard Worker// The values allowed for Droidstubs' Api_levels_sdk_type 31*333d2b36SAndroid Build Coastguard Workervar allowedApiLevelSdkTypes = []string{"public", "system", "module-lib", "system-server"} 32*333d2b36SAndroid Build Coastguard Worker 33*333d2b36SAndroid Build Coastguard Workertype StubsType int 34*333d2b36SAndroid Build Coastguard Worker 35*333d2b36SAndroid Build Coastguard Workerconst ( 36*333d2b36SAndroid Build Coastguard Worker Everything StubsType = iota 37*333d2b36SAndroid Build Coastguard Worker Runtime 38*333d2b36SAndroid Build Coastguard Worker Exportable 39*333d2b36SAndroid Build Coastguard Worker Unavailable 40*333d2b36SAndroid Build Coastguard Worker) 41*333d2b36SAndroid Build Coastguard Worker 42*333d2b36SAndroid Build Coastguard Workerfunc (s StubsType) String() string { 43*333d2b36SAndroid Build Coastguard Worker switch s { 44*333d2b36SAndroid Build Coastguard Worker case Everything: 45*333d2b36SAndroid Build Coastguard Worker return "everything" 46*333d2b36SAndroid Build Coastguard Worker case Runtime: 47*333d2b36SAndroid Build Coastguard Worker return "runtime" 48*333d2b36SAndroid Build Coastguard Worker case Exportable: 49*333d2b36SAndroid Build Coastguard Worker return "exportable" 50*333d2b36SAndroid Build Coastguard Worker default: 51*333d2b36SAndroid Build Coastguard Worker return "" 52*333d2b36SAndroid Build Coastguard Worker } 53*333d2b36SAndroid Build Coastguard Worker} 54*333d2b36SAndroid Build Coastguard Worker 55*333d2b36SAndroid Build Coastguard Workerfunc StringToStubsType(s string) StubsType { 56*333d2b36SAndroid Build Coastguard Worker switch strings.ToLower(s) { 57*333d2b36SAndroid Build Coastguard Worker case Everything.String(): 58*333d2b36SAndroid Build Coastguard Worker return Everything 59*333d2b36SAndroid Build Coastguard Worker case Runtime.String(): 60*333d2b36SAndroid Build Coastguard Worker return Runtime 61*333d2b36SAndroid Build Coastguard Worker case Exportable.String(): 62*333d2b36SAndroid Build Coastguard Worker return Exportable 63*333d2b36SAndroid Build Coastguard Worker default: 64*333d2b36SAndroid Build Coastguard Worker return Unavailable 65*333d2b36SAndroid Build Coastguard Worker } 66*333d2b36SAndroid Build Coastguard Worker} 67*333d2b36SAndroid Build Coastguard Worker 68*333d2b36SAndroid Build Coastguard Workerfunc init() { 69*333d2b36SAndroid Build Coastguard Worker RegisterStubsBuildComponents(android.InitRegistrationContext) 70*333d2b36SAndroid Build Coastguard Worker} 71*333d2b36SAndroid Build Coastguard Worker 72*333d2b36SAndroid Build Coastguard Workerfunc RegisterStubsBuildComponents(ctx android.RegistrationContext) { 73*333d2b36SAndroid Build Coastguard Worker ctx.RegisterModuleType("stubs_defaults", StubsDefaultsFactory) 74*333d2b36SAndroid Build Coastguard Worker 75*333d2b36SAndroid Build Coastguard Worker ctx.RegisterModuleType("droidstubs", DroidstubsFactory) 76*333d2b36SAndroid Build Coastguard Worker ctx.RegisterModuleType("droidstubs_host", DroidstubsHostFactory) 77*333d2b36SAndroid Build Coastguard Worker 78*333d2b36SAndroid Build Coastguard Worker ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory) 79*333d2b36SAndroid Build Coastguard Worker} 80*333d2b36SAndroid Build Coastguard Worker 81*333d2b36SAndroid Build Coastguard Workertype stubsArtifacts struct { 82*333d2b36SAndroid Build Coastguard Worker nullabilityWarningsFile android.WritablePath 83*333d2b36SAndroid Build Coastguard Worker annotationsZip android.WritablePath 84*333d2b36SAndroid Build Coastguard Worker apiVersionsXml android.WritablePath 85*333d2b36SAndroid Build Coastguard Worker metadataZip android.WritablePath 86*333d2b36SAndroid Build Coastguard Worker metadataDir android.WritablePath 87*333d2b36SAndroid Build Coastguard Worker} 88*333d2b36SAndroid Build Coastguard Worker 89*333d2b36SAndroid Build Coastguard Worker// Droidstubs 90*333d2b36SAndroid Build Coastguard Workertype Droidstubs struct { 91*333d2b36SAndroid Build Coastguard Worker Javadoc 92*333d2b36SAndroid Build Coastguard Worker embeddableInModuleAndImport 93*333d2b36SAndroid Build Coastguard Worker 94*333d2b36SAndroid Build Coastguard Worker properties DroidstubsProperties 95*333d2b36SAndroid Build Coastguard Worker apiFile android.Path 96*333d2b36SAndroid Build Coastguard Worker removedApiFile android.Path 97*333d2b36SAndroid Build Coastguard Worker 98*333d2b36SAndroid Build Coastguard Worker checkCurrentApiTimestamp android.WritablePath 99*333d2b36SAndroid Build Coastguard Worker updateCurrentApiTimestamp android.WritablePath 100*333d2b36SAndroid Build Coastguard Worker checkLastReleasedApiTimestamp android.WritablePath 101*333d2b36SAndroid Build Coastguard Worker apiLintTimestamp android.WritablePath 102*333d2b36SAndroid Build Coastguard Worker apiLintReport android.WritablePath 103*333d2b36SAndroid Build Coastguard Worker 104*333d2b36SAndroid Build Coastguard Worker checkNullabilityWarningsTimestamp android.WritablePath 105*333d2b36SAndroid Build Coastguard Worker 106*333d2b36SAndroid Build Coastguard Worker everythingArtifacts stubsArtifacts 107*333d2b36SAndroid Build Coastguard Worker exportableArtifacts stubsArtifacts 108*333d2b36SAndroid Build Coastguard Worker 109*333d2b36SAndroid Build Coastguard Worker exportableApiFile android.WritablePath 110*333d2b36SAndroid Build Coastguard Worker exportableRemovedApiFile android.WritablePath 111*333d2b36SAndroid Build Coastguard Worker} 112*333d2b36SAndroid Build Coastguard Worker 113*333d2b36SAndroid Build Coastguard Workertype DroidstubsProperties struct { 114*333d2b36SAndroid Build Coastguard Worker // The generated public API filename by Metalava, defaults to <module>_api.txt 115*333d2b36SAndroid Build Coastguard Worker Api_filename *string 116*333d2b36SAndroid Build Coastguard Worker 117*333d2b36SAndroid Build Coastguard Worker // the generated removed API filename by Metalava, defaults to <module>_removed.txt 118*333d2b36SAndroid Build Coastguard Worker Removed_api_filename *string 119*333d2b36SAndroid Build Coastguard Worker 120*333d2b36SAndroid Build Coastguard Worker Check_api struct { 121*333d2b36SAndroid Build Coastguard Worker Last_released ApiToCheck 122*333d2b36SAndroid Build Coastguard Worker 123*333d2b36SAndroid Build Coastguard Worker Current ApiToCheck 124*333d2b36SAndroid Build Coastguard Worker 125*333d2b36SAndroid Build Coastguard Worker Api_lint struct { 126*333d2b36SAndroid Build Coastguard Worker Enabled *bool 127*333d2b36SAndroid Build Coastguard Worker 128*333d2b36SAndroid Build Coastguard Worker // If set, performs api_lint on any new APIs not found in the given signature file 129*333d2b36SAndroid Build Coastguard Worker New_since *string `android:"path"` 130*333d2b36SAndroid Build Coastguard Worker 131*333d2b36SAndroid Build Coastguard Worker // If not blank, path to the baseline txt file for approved API lint violations. 132*333d2b36SAndroid Build Coastguard Worker Baseline_file *string `android:"path"` 133*333d2b36SAndroid Build Coastguard Worker } 134*333d2b36SAndroid Build Coastguard Worker } 135*333d2b36SAndroid Build Coastguard Worker 136*333d2b36SAndroid Build Coastguard Worker // user can specify the version of previous released API file in order to do compatibility check. 137*333d2b36SAndroid Build Coastguard Worker Previous_api *string `android:"path"` 138*333d2b36SAndroid Build Coastguard Worker 139*333d2b36SAndroid Build Coastguard Worker // is set to true, Metalava will allow framework SDK to contain annotations. 140*333d2b36SAndroid Build Coastguard Worker Annotations_enabled *bool 141*333d2b36SAndroid Build Coastguard Worker 142*333d2b36SAndroid Build Coastguard Worker // a list of top-level directories containing files to merge qualifier annotations (i.e. those intended to be included in the stubs written) from. 143*333d2b36SAndroid Build Coastguard Worker Merge_annotations_dirs []string 144*333d2b36SAndroid Build Coastguard Worker 145*333d2b36SAndroid Build Coastguard Worker // a list of top-level directories containing Java stub files to merge show/hide annotations from. 146*333d2b36SAndroid Build Coastguard Worker Merge_inclusion_annotations_dirs []string 147*333d2b36SAndroid Build Coastguard Worker 148*333d2b36SAndroid Build Coastguard Worker // a file containing a list of classes to do nullability validation for. 149*333d2b36SAndroid Build Coastguard Worker Validate_nullability_from_list *string 150*333d2b36SAndroid Build Coastguard Worker 151*333d2b36SAndroid Build Coastguard Worker // a file containing expected warnings produced by validation of nullability annotations. 152*333d2b36SAndroid Build Coastguard Worker Check_nullability_warnings *string 153*333d2b36SAndroid Build Coastguard Worker 154*333d2b36SAndroid Build Coastguard Worker // if set to true, allow Metalava to generate doc_stubs source files. Defaults to false. 155*333d2b36SAndroid Build Coastguard Worker Create_doc_stubs *bool 156*333d2b36SAndroid Build Coastguard Worker 157*333d2b36SAndroid Build Coastguard Worker // if set to true, cause Metalava to output Javadoc comments in the stubs source files. Defaults to false. 158*333d2b36SAndroid Build Coastguard Worker // Has no effect if create_doc_stubs: true. 159*333d2b36SAndroid Build Coastguard Worker Output_javadoc_comments *bool 160*333d2b36SAndroid Build Coastguard Worker 161*333d2b36SAndroid Build Coastguard Worker // if set to false then do not write out stubs. Defaults to true. 162*333d2b36SAndroid Build Coastguard Worker // 163*333d2b36SAndroid Build Coastguard Worker // TODO(b/146727827): Remove capability when we do not need to generate stubs and API separately. 164*333d2b36SAndroid Build Coastguard Worker Generate_stubs *bool 165*333d2b36SAndroid Build Coastguard Worker 166*333d2b36SAndroid Build Coastguard Worker // if set to true, provides a hint to the build system that this rule uses a lot of memory, 167*333d2b36SAndroid Build Coastguard Worker // which can be used for scheduling purposes 168*333d2b36SAndroid Build Coastguard Worker High_mem *bool 169*333d2b36SAndroid Build Coastguard Worker 170*333d2b36SAndroid Build Coastguard Worker // if set to true, Metalava will allow framework SDK to contain API levels annotations. 171*333d2b36SAndroid Build Coastguard Worker Api_levels_annotations_enabled *bool 172*333d2b36SAndroid Build Coastguard Worker 173*333d2b36SAndroid Build Coastguard Worker // Apply the api levels database created by this module rather than generating one in this droidstubs. 174*333d2b36SAndroid Build Coastguard Worker Api_levels_module *string 175*333d2b36SAndroid Build Coastguard Worker 176*333d2b36SAndroid Build Coastguard Worker // the dirs which Metalava extracts API levels annotations from. 177*333d2b36SAndroid Build Coastguard Worker Api_levels_annotations_dirs []string 178*333d2b36SAndroid Build Coastguard Worker 179*333d2b36SAndroid Build Coastguard Worker // the sdk kind which Metalava extracts API levels annotations from. Supports 'public', 'system', 'module-lib' and 'system-server'; defaults to public. 180*333d2b36SAndroid Build Coastguard Worker Api_levels_sdk_type *string 181*333d2b36SAndroid Build Coastguard Worker 182*333d2b36SAndroid Build Coastguard Worker // the filename which Metalava extracts API levels annotations from. Defaults to android.jar. 183*333d2b36SAndroid Build Coastguard Worker Api_levels_jar_filename *string 184*333d2b36SAndroid Build Coastguard Worker 185*333d2b36SAndroid Build Coastguard Worker // if set to true, collect the values used by the Dev tools and 186*333d2b36SAndroid Build Coastguard Worker // write them in files packaged with the SDK. Defaults to false. 187*333d2b36SAndroid Build Coastguard Worker Write_sdk_values *bool 188*333d2b36SAndroid Build Coastguard Worker 189*333d2b36SAndroid Build Coastguard Worker // path or filegroup to file defining extension an SDK name <-> numerical ID mapping and 190*333d2b36SAndroid Build Coastguard Worker // what APIs exist in which SDKs; passed to metalava via --sdk-extensions-info 191*333d2b36SAndroid Build Coastguard Worker Extensions_info_file *string `android:"path"` 192*333d2b36SAndroid Build Coastguard Worker 193*333d2b36SAndroid Build Coastguard Worker // API surface of this module. If set, the module contributes to an API surface. 194*333d2b36SAndroid Build Coastguard Worker // For the full list of available API surfaces, refer to soong/android/sdk_version.go 195*333d2b36SAndroid Build Coastguard Worker Api_surface *string 196*333d2b36SAndroid Build Coastguard Worker 197*333d2b36SAndroid Build Coastguard Worker // a list of aconfig_declarations module names that the stubs generated in this module 198*333d2b36SAndroid Build Coastguard Worker // depend on. 199*333d2b36SAndroid Build Coastguard Worker Aconfig_declarations []string 200*333d2b36SAndroid Build Coastguard Worker 201*333d2b36SAndroid Build Coastguard Worker // List of hard coded filegroups containing Metalava config files that are passed to every 202*333d2b36SAndroid Build Coastguard Worker // Metalava invocation that this module performs. See addMetalavaConfigFilesToCmd. 203*333d2b36SAndroid Build Coastguard Worker ConfigFiles []string `android:"path" blueprint:"mutated"` 204*333d2b36SAndroid Build Coastguard Worker} 205*333d2b36SAndroid Build Coastguard Worker 206*333d2b36SAndroid Build Coastguard Worker// Used by xsd_config 207*333d2b36SAndroid Build Coastguard Workertype ApiFilePath interface { 208*333d2b36SAndroid Build Coastguard Worker ApiFilePath(StubsType) (android.Path, error) 209*333d2b36SAndroid Build Coastguard Worker} 210*333d2b36SAndroid Build Coastguard Worker 211*333d2b36SAndroid Build Coastguard Workertype ApiStubsSrcProvider interface { 212*333d2b36SAndroid Build Coastguard Worker StubsSrcJar(StubsType) (android.Path, error) 213*333d2b36SAndroid Build Coastguard Worker} 214*333d2b36SAndroid Build Coastguard Worker 215*333d2b36SAndroid Build Coastguard Worker// Provider of information about API stubs, used by java_sdk_library. 216*333d2b36SAndroid Build Coastguard Workertype ApiStubsProvider interface { 217*333d2b36SAndroid Build Coastguard Worker AnnotationsZip(StubsType) (android.Path, error) 218*333d2b36SAndroid Build Coastguard Worker ApiFilePath 219*333d2b36SAndroid Build Coastguard Worker RemovedApiFilePath(StubsType) (android.Path, error) 220*333d2b36SAndroid Build Coastguard Worker 221*333d2b36SAndroid Build Coastguard Worker ApiStubsSrcProvider 222*333d2b36SAndroid Build Coastguard Worker} 223*333d2b36SAndroid Build Coastguard Worker 224*333d2b36SAndroid Build Coastguard Workertype currentApiTimestampProvider interface { 225*333d2b36SAndroid Build Coastguard Worker CurrentApiTimestamp() android.Path 226*333d2b36SAndroid Build Coastguard Worker} 227*333d2b36SAndroid Build Coastguard Worker 228*333d2b36SAndroid Build Coastguard Workertype annotationFlagsParams struct { 229*333d2b36SAndroid Build Coastguard Worker migratingNullability bool 230*333d2b36SAndroid Build Coastguard Worker validatingNullability bool 231*333d2b36SAndroid Build Coastguard Worker nullabilityWarningsFile android.WritablePath 232*333d2b36SAndroid Build Coastguard Worker annotationsZip android.WritablePath 233*333d2b36SAndroid Build Coastguard Worker} 234*333d2b36SAndroid Build Coastguard Workertype stubsCommandParams struct { 235*333d2b36SAndroid Build Coastguard Worker srcJarDir android.ModuleOutPath 236*333d2b36SAndroid Build Coastguard Worker stubsDir android.OptionalPath 237*333d2b36SAndroid Build Coastguard Worker stubsSrcJar android.WritablePath 238*333d2b36SAndroid Build Coastguard Worker metadataZip android.WritablePath 239*333d2b36SAndroid Build Coastguard Worker metadataDir android.WritablePath 240*333d2b36SAndroid Build Coastguard Worker apiVersionsXml android.WritablePath 241*333d2b36SAndroid Build Coastguard Worker nullabilityWarningsFile android.WritablePath 242*333d2b36SAndroid Build Coastguard Worker annotationsZip android.WritablePath 243*333d2b36SAndroid Build Coastguard Worker stubConfig stubsCommandConfigParams 244*333d2b36SAndroid Build Coastguard Worker} 245*333d2b36SAndroid Build Coastguard Workertype stubsCommandConfigParams struct { 246*333d2b36SAndroid Build Coastguard Worker stubsType StubsType 247*333d2b36SAndroid Build Coastguard Worker javaVersion javaVersion 248*333d2b36SAndroid Build Coastguard Worker deps deps 249*333d2b36SAndroid Build Coastguard Worker checkApi bool 250*333d2b36SAndroid Build Coastguard Worker generateStubs bool 251*333d2b36SAndroid Build Coastguard Worker doApiLint bool 252*333d2b36SAndroid Build Coastguard Worker doCheckReleased bool 253*333d2b36SAndroid Build Coastguard Worker writeSdkValues bool 254*333d2b36SAndroid Build Coastguard Worker migratingNullability bool 255*333d2b36SAndroid Build Coastguard Worker validatingNullability bool 256*333d2b36SAndroid Build Coastguard Worker} 257*333d2b36SAndroid Build Coastguard Worker 258*333d2b36SAndroid Build Coastguard Worker// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be 259*333d2b36SAndroid Build Coastguard Worker// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to 260*333d2b36SAndroid Build Coastguard Worker// a droiddoc module to generate documentation. 261*333d2b36SAndroid Build Coastguard Workerfunc DroidstubsFactory() android.Module { 262*333d2b36SAndroid Build Coastguard Worker module := &Droidstubs{} 263*333d2b36SAndroid Build Coastguard Worker 264*333d2b36SAndroid Build Coastguard Worker module.AddProperties(&module.properties, 265*333d2b36SAndroid Build Coastguard Worker &module.Javadoc.properties) 266*333d2b36SAndroid Build Coastguard Worker module.properties.ConfigFiles = getMetalavaConfigFilegroupReference() 267*333d2b36SAndroid Build Coastguard Worker module.initModuleAndImport(module) 268*333d2b36SAndroid Build Coastguard Worker 269*333d2b36SAndroid Build Coastguard Worker InitDroiddocModule(module, android.HostAndDeviceSupported) 270*333d2b36SAndroid Build Coastguard Worker 271*333d2b36SAndroid Build Coastguard Worker module.SetDefaultableHook(func(ctx android.DefaultableHookContext) { 272*333d2b36SAndroid Build Coastguard Worker module.createApiContribution(ctx) 273*333d2b36SAndroid Build Coastguard Worker }) 274*333d2b36SAndroid Build Coastguard Worker return module 275*333d2b36SAndroid Build Coastguard Worker} 276*333d2b36SAndroid Build Coastguard Worker 277*333d2b36SAndroid Build Coastguard Worker// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API 278*333d2b36SAndroid Build Coastguard Worker// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be 279*333d2b36SAndroid Build Coastguard Worker// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs 280*333d2b36SAndroid Build Coastguard Worker// module when symbols needed by the source files are provided by java_library_host modules. 281*333d2b36SAndroid Build Coastguard Workerfunc DroidstubsHostFactory() android.Module { 282*333d2b36SAndroid Build Coastguard Worker module := &Droidstubs{} 283*333d2b36SAndroid Build Coastguard Worker 284*333d2b36SAndroid Build Coastguard Worker module.AddProperties(&module.properties, 285*333d2b36SAndroid Build Coastguard Worker &module.Javadoc.properties) 286*333d2b36SAndroid Build Coastguard Worker 287*333d2b36SAndroid Build Coastguard Worker module.properties.ConfigFiles = getMetalavaConfigFilegroupReference() 288*333d2b36SAndroid Build Coastguard Worker InitDroiddocModule(module, android.HostSupported) 289*333d2b36SAndroid Build Coastguard Worker return module 290*333d2b36SAndroid Build Coastguard Worker} 291*333d2b36SAndroid Build Coastguard Worker 292*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) AnnotationsZip(stubsType StubsType) (ret android.Path, err error) { 293*333d2b36SAndroid Build Coastguard Worker switch stubsType { 294*333d2b36SAndroid Build Coastguard Worker case Everything: 295*333d2b36SAndroid Build Coastguard Worker ret, err = d.everythingArtifacts.annotationsZip, nil 296*333d2b36SAndroid Build Coastguard Worker case Exportable: 297*333d2b36SAndroid Build Coastguard Worker ret, err = d.exportableArtifacts.annotationsZip, nil 298*333d2b36SAndroid Build Coastguard Worker default: 299*333d2b36SAndroid Build Coastguard Worker ret, err = nil, fmt.Errorf("annotations zip not supported for the stub type %s", stubsType.String()) 300*333d2b36SAndroid Build Coastguard Worker } 301*333d2b36SAndroid Build Coastguard Worker return ret, err 302*333d2b36SAndroid Build Coastguard Worker} 303*333d2b36SAndroid Build Coastguard Worker 304*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) ApiFilePath(stubsType StubsType) (ret android.Path, err error) { 305*333d2b36SAndroid Build Coastguard Worker switch stubsType { 306*333d2b36SAndroid Build Coastguard Worker case Everything: 307*333d2b36SAndroid Build Coastguard Worker ret, err = d.apiFile, nil 308*333d2b36SAndroid Build Coastguard Worker case Exportable: 309*333d2b36SAndroid Build Coastguard Worker ret, err = d.exportableApiFile, nil 310*333d2b36SAndroid Build Coastguard Worker default: 311*333d2b36SAndroid Build Coastguard Worker ret, err = nil, fmt.Errorf("api file path not supported for the stub type %s", stubsType.String()) 312*333d2b36SAndroid Build Coastguard Worker } 313*333d2b36SAndroid Build Coastguard Worker if ret == nil && err == nil { 314*333d2b36SAndroid Build Coastguard Worker err = fmt.Errorf("api file is null for the stub type %s", stubsType.String()) 315*333d2b36SAndroid Build Coastguard Worker } 316*333d2b36SAndroid Build Coastguard Worker return ret, err 317*333d2b36SAndroid Build Coastguard Worker} 318*333d2b36SAndroid Build Coastguard Worker 319*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) ApiVersionsXmlFilePath(stubsType StubsType) (ret android.Path, err error) { 320*333d2b36SAndroid Build Coastguard Worker switch stubsType { 321*333d2b36SAndroid Build Coastguard Worker case Everything: 322*333d2b36SAndroid Build Coastguard Worker ret, err = d.everythingArtifacts.apiVersionsXml, nil 323*333d2b36SAndroid Build Coastguard Worker case Exportable: 324*333d2b36SAndroid Build Coastguard Worker ret, err = d.exportableArtifacts.apiVersionsXml, nil 325*333d2b36SAndroid Build Coastguard Worker default: 326*333d2b36SAndroid Build Coastguard Worker ret, err = nil, fmt.Errorf("api versions xml file path not supported for the stub type %s", stubsType.String()) 327*333d2b36SAndroid Build Coastguard Worker } 328*333d2b36SAndroid Build Coastguard Worker if ret == nil && err == nil { 329*333d2b36SAndroid Build Coastguard Worker err = fmt.Errorf("api versions xml file is null for the stub type %s", stubsType.String()) 330*333d2b36SAndroid Build Coastguard Worker } 331*333d2b36SAndroid Build Coastguard Worker return ret, err 332*333d2b36SAndroid Build Coastguard Worker} 333*333d2b36SAndroid Build Coastguard Worker 334*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) DocZip(stubsType StubsType) (ret android.Path, err error) { 335*333d2b36SAndroid Build Coastguard Worker switch stubsType { 336*333d2b36SAndroid Build Coastguard Worker case Everything: 337*333d2b36SAndroid Build Coastguard Worker ret, err = d.docZip, nil 338*333d2b36SAndroid Build Coastguard Worker default: 339*333d2b36SAndroid Build Coastguard Worker ret, err = nil, fmt.Errorf("docs zip not supported for the stub type %s", stubsType.String()) 340*333d2b36SAndroid Build Coastguard Worker } 341*333d2b36SAndroid Build Coastguard Worker if ret == nil && err == nil { 342*333d2b36SAndroid Build Coastguard Worker err = fmt.Errorf("docs zip is null for the stub type %s", stubsType.String()) 343*333d2b36SAndroid Build Coastguard Worker } 344*333d2b36SAndroid Build Coastguard Worker return ret, err 345*333d2b36SAndroid Build Coastguard Worker} 346*333d2b36SAndroid Build Coastguard Worker 347*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) RemovedApiFilePath(stubsType StubsType) (ret android.Path, err error) { 348*333d2b36SAndroid Build Coastguard Worker switch stubsType { 349*333d2b36SAndroid Build Coastguard Worker case Everything: 350*333d2b36SAndroid Build Coastguard Worker ret, err = d.removedApiFile, nil 351*333d2b36SAndroid Build Coastguard Worker case Exportable: 352*333d2b36SAndroid Build Coastguard Worker ret, err = d.exportableRemovedApiFile, nil 353*333d2b36SAndroid Build Coastguard Worker default: 354*333d2b36SAndroid Build Coastguard Worker ret, err = nil, fmt.Errorf("removed api file path not supported for the stub type %s", stubsType.String()) 355*333d2b36SAndroid Build Coastguard Worker } 356*333d2b36SAndroid Build Coastguard Worker if ret == nil && err == nil { 357*333d2b36SAndroid Build Coastguard Worker err = fmt.Errorf("removed api file is null for the stub type %s", stubsType.String()) 358*333d2b36SAndroid Build Coastguard Worker } 359*333d2b36SAndroid Build Coastguard Worker return ret, err 360*333d2b36SAndroid Build Coastguard Worker} 361*333d2b36SAndroid Build Coastguard Worker 362*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) StubsSrcJar(stubsType StubsType) (ret android.Path, err error) { 363*333d2b36SAndroid Build Coastguard Worker switch stubsType { 364*333d2b36SAndroid Build Coastguard Worker case Everything: 365*333d2b36SAndroid Build Coastguard Worker ret, err = d.stubsSrcJar, nil 366*333d2b36SAndroid Build Coastguard Worker case Exportable: 367*333d2b36SAndroid Build Coastguard Worker ret, err = d.exportableStubsSrcJar, nil 368*333d2b36SAndroid Build Coastguard Worker default: 369*333d2b36SAndroid Build Coastguard Worker ret, err = nil, fmt.Errorf("stubs srcjar not supported for the stub type %s", stubsType.String()) 370*333d2b36SAndroid Build Coastguard Worker } 371*333d2b36SAndroid Build Coastguard Worker if ret == nil && err == nil { 372*333d2b36SAndroid Build Coastguard Worker err = fmt.Errorf("stubs srcjar is null for the stub type %s", stubsType.String()) 373*333d2b36SAndroid Build Coastguard Worker } 374*333d2b36SAndroid Build Coastguard Worker return ret, err 375*333d2b36SAndroid Build Coastguard Worker} 376*333d2b36SAndroid Build Coastguard Worker 377*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) CurrentApiTimestamp() android.Path { 378*333d2b36SAndroid Build Coastguard Worker return d.checkCurrentApiTimestamp 379*333d2b36SAndroid Build Coastguard Worker} 380*333d2b36SAndroid Build Coastguard Worker 381*333d2b36SAndroid Build Coastguard Workervar metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"} 382*333d2b36SAndroid Build Coastguard Workervar metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"} 383*333d2b36SAndroid Build Coastguard Workervar metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"} 384*333d2b36SAndroid Build Coastguard Workervar metalavaAPILevelsModuleTag = dependencyTag{name: "metalava-api-levels-module-tag"} 385*333d2b36SAndroid Build Coastguard Workervar metalavaCurrentApiTimestampTag = dependencyTag{name: "metalava-current-api-timestamp-tag"} 386*333d2b36SAndroid Build Coastguard Worker 387*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) { 388*333d2b36SAndroid Build Coastguard Worker d.Javadoc.addDeps(ctx) 389*333d2b36SAndroid Build Coastguard Worker 390*333d2b36SAndroid Build Coastguard Worker if len(d.properties.Merge_annotations_dirs) != 0 { 391*333d2b36SAndroid Build Coastguard Worker for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs { 392*333d2b36SAndroid Build Coastguard Worker ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir) 393*333d2b36SAndroid Build Coastguard Worker } 394*333d2b36SAndroid Build Coastguard Worker } 395*333d2b36SAndroid Build Coastguard Worker 396*333d2b36SAndroid Build Coastguard Worker if len(d.properties.Merge_inclusion_annotations_dirs) != 0 { 397*333d2b36SAndroid Build Coastguard Worker for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs { 398*333d2b36SAndroid Build Coastguard Worker ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir) 399*333d2b36SAndroid Build Coastguard Worker } 400*333d2b36SAndroid Build Coastguard Worker } 401*333d2b36SAndroid Build Coastguard Worker 402*333d2b36SAndroid Build Coastguard Worker if len(d.properties.Api_levels_annotations_dirs) != 0 { 403*333d2b36SAndroid Build Coastguard Worker for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs { 404*333d2b36SAndroid Build Coastguard Worker ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir) 405*333d2b36SAndroid Build Coastguard Worker } 406*333d2b36SAndroid Build Coastguard Worker } 407*333d2b36SAndroid Build Coastguard Worker 408*333d2b36SAndroid Build Coastguard Worker if len(d.properties.Aconfig_declarations) != 0 { 409*333d2b36SAndroid Build Coastguard Worker for _, aconfigDeclarationModuleName := range d.properties.Aconfig_declarations { 410*333d2b36SAndroid Build Coastguard Worker ctx.AddDependency(ctx.Module(), aconfigDeclarationTag, aconfigDeclarationModuleName) 411*333d2b36SAndroid Build Coastguard Worker } 412*333d2b36SAndroid Build Coastguard Worker } 413*333d2b36SAndroid Build Coastguard Worker 414*333d2b36SAndroid Build Coastguard Worker if d.properties.Api_levels_module != nil { 415*333d2b36SAndroid Build Coastguard Worker ctx.AddDependency(ctx.Module(), metalavaAPILevelsModuleTag, proptools.String(d.properties.Api_levels_module)) 416*333d2b36SAndroid Build Coastguard Worker } 417*333d2b36SAndroid Build Coastguard Worker} 418*333d2b36SAndroid Build Coastguard Worker 419*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) sdkValuesFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, metadataDir android.WritablePath) { 420*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithArg("--sdk-values ", metadataDir.String()) 421*333d2b36SAndroid Build Coastguard Worker} 422*333d2b36SAndroid Build Coastguard Worker 423*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath, stubsType StubsType, checkApi bool) { 424*333d2b36SAndroid Build Coastguard Worker 425*333d2b36SAndroid Build Coastguard Worker apiFileName := proptools.StringDefault(d.properties.Api_filename, ctx.ModuleName()+"_api.txt") 426*333d2b36SAndroid Build Coastguard Worker uncheckedApiFile := android.PathForModuleOut(ctx, stubsType.String(), apiFileName) 427*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithOutput("--api ", uncheckedApiFile) 428*333d2b36SAndroid Build Coastguard Worker if checkApi || String(d.properties.Api_filename) != "" { 429*333d2b36SAndroid Build Coastguard Worker if stubsType == Everything { 430*333d2b36SAndroid Build Coastguard Worker d.apiFile = uncheckedApiFile 431*333d2b36SAndroid Build Coastguard Worker } else if stubsType == Exportable { 432*333d2b36SAndroid Build Coastguard Worker d.exportableApiFile = uncheckedApiFile 433*333d2b36SAndroid Build Coastguard Worker } 434*333d2b36SAndroid Build Coastguard Worker } else if sourceApiFile := proptools.String(d.properties.Check_api.Current.Api_file); sourceApiFile != "" { 435*333d2b36SAndroid Build Coastguard Worker if stubsType == Everything { 436*333d2b36SAndroid Build Coastguard Worker // If check api is disabled then make the source file available for export. 437*333d2b36SAndroid Build Coastguard Worker d.apiFile = android.PathForModuleSrc(ctx, sourceApiFile) 438*333d2b36SAndroid Build Coastguard Worker } else if stubsType == Exportable { 439*333d2b36SAndroid Build Coastguard Worker d.exportableApiFile = uncheckedApiFile 440*333d2b36SAndroid Build Coastguard Worker } 441*333d2b36SAndroid Build Coastguard Worker } 442*333d2b36SAndroid Build Coastguard Worker 443*333d2b36SAndroid Build Coastguard Worker removedApiFileName := proptools.StringDefault(d.properties.Removed_api_filename, ctx.ModuleName()+"_removed.txt") 444*333d2b36SAndroid Build Coastguard Worker uncheckedRemovedFile := android.PathForModuleOut(ctx, stubsType.String(), removedApiFileName) 445*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithOutput("--removed-api ", uncheckedRemovedFile) 446*333d2b36SAndroid Build Coastguard Worker if checkApi || String(d.properties.Removed_api_filename) != "" { 447*333d2b36SAndroid Build Coastguard Worker if stubsType == Everything { 448*333d2b36SAndroid Build Coastguard Worker d.removedApiFile = uncheckedRemovedFile 449*333d2b36SAndroid Build Coastguard Worker } else if stubsType == Exportable { 450*333d2b36SAndroid Build Coastguard Worker d.exportableRemovedApiFile = uncheckedRemovedFile 451*333d2b36SAndroid Build Coastguard Worker } 452*333d2b36SAndroid Build Coastguard Worker } else if sourceRemovedApiFile := proptools.String(d.properties.Check_api.Current.Removed_api_file); sourceRemovedApiFile != "" { 453*333d2b36SAndroid Build Coastguard Worker if stubsType == Everything { 454*333d2b36SAndroid Build Coastguard Worker // If check api is disabled then make the source removed api file available for export. 455*333d2b36SAndroid Build Coastguard Worker d.removedApiFile = android.PathForModuleSrc(ctx, sourceRemovedApiFile) 456*333d2b36SAndroid Build Coastguard Worker } else if stubsType == Exportable { 457*333d2b36SAndroid Build Coastguard Worker d.exportableRemovedApiFile = uncheckedRemovedFile 458*333d2b36SAndroid Build Coastguard Worker } 459*333d2b36SAndroid Build Coastguard Worker } 460*333d2b36SAndroid Build Coastguard Worker 461*333d2b36SAndroid Build Coastguard Worker if stubsDir.Valid() { 462*333d2b36SAndroid Build Coastguard Worker if Bool(d.properties.Create_doc_stubs) { 463*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithArg("--doc-stubs ", stubsDir.String()) 464*333d2b36SAndroid Build Coastguard Worker } else { 465*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithArg("--stubs ", stubsDir.String()) 466*333d2b36SAndroid Build Coastguard Worker if !Bool(d.properties.Output_javadoc_comments) { 467*333d2b36SAndroid Build Coastguard Worker cmd.Flag("--exclude-documentation-from-stubs") 468*333d2b36SAndroid Build Coastguard Worker } 469*333d2b36SAndroid Build Coastguard Worker } 470*333d2b36SAndroid Build Coastguard Worker } 471*333d2b36SAndroid Build Coastguard Worker} 472*333d2b36SAndroid Build Coastguard Worker 473*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, params annotationFlagsParams) { 474*333d2b36SAndroid Build Coastguard Worker if Bool(d.properties.Annotations_enabled) { 475*333d2b36SAndroid Build Coastguard Worker cmd.Flag(config.MetalavaAnnotationsFlags) 476*333d2b36SAndroid Build Coastguard Worker 477*333d2b36SAndroid Build Coastguard Worker if params.migratingNullability { 478*333d2b36SAndroid Build Coastguard Worker previousApiFiles := android.PathsForModuleSrc(ctx, []string{String(d.properties.Previous_api)}) 479*333d2b36SAndroid Build Coastguard Worker cmd.FlagForEachInput("--migrate-nullness ", previousApiFiles) 480*333d2b36SAndroid Build Coastguard Worker } 481*333d2b36SAndroid Build Coastguard Worker 482*333d2b36SAndroid Build Coastguard Worker if s := String(d.properties.Validate_nullability_from_list); s != "" { 483*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s)) 484*333d2b36SAndroid Build Coastguard Worker } 485*333d2b36SAndroid Build Coastguard Worker 486*333d2b36SAndroid Build Coastguard Worker if params.validatingNullability { 487*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithOutput("--nullability-warnings-txt ", params.nullabilityWarningsFile) 488*333d2b36SAndroid Build Coastguard Worker } 489*333d2b36SAndroid Build Coastguard Worker 490*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithOutput("--extract-annotations ", params.annotationsZip) 491*333d2b36SAndroid Build Coastguard Worker 492*333d2b36SAndroid Build Coastguard Worker if len(d.properties.Merge_annotations_dirs) != 0 { 493*333d2b36SAndroid Build Coastguard Worker d.mergeAnnoDirFlags(ctx, cmd) 494*333d2b36SAndroid Build Coastguard Worker } 495*333d2b36SAndroid Build Coastguard Worker 496*333d2b36SAndroid Build Coastguard Worker cmd.Flag(config.MetalavaAnnotationsWarningsFlags) 497*333d2b36SAndroid Build Coastguard Worker } 498*333d2b36SAndroid Build Coastguard Worker} 499*333d2b36SAndroid Build Coastguard Worker 500*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) { 501*333d2b36SAndroid Build Coastguard Worker ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) { 502*333d2b36SAndroid Build Coastguard Worker if t, ok := m.(*ExportedDroiddocDir); ok { 503*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps) 504*333d2b36SAndroid Build Coastguard Worker } else { 505*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf("merge_annotations_dirs", 506*333d2b36SAndroid Build Coastguard Worker "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m)) 507*333d2b36SAndroid Build Coastguard Worker } 508*333d2b36SAndroid Build Coastguard Worker }) 509*333d2b36SAndroid Build Coastguard Worker} 510*333d2b36SAndroid Build Coastguard Worker 511*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) { 512*333d2b36SAndroid Build Coastguard Worker ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) { 513*333d2b36SAndroid Build Coastguard Worker if t, ok := m.(*ExportedDroiddocDir); ok { 514*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps) 515*333d2b36SAndroid Build Coastguard Worker } else { 516*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf("merge_inclusion_annotations_dirs", 517*333d2b36SAndroid Build Coastguard Worker "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m)) 518*333d2b36SAndroid Build Coastguard Worker } 519*333d2b36SAndroid Build Coastguard Worker }) 520*333d2b36SAndroid Build Coastguard Worker} 521*333d2b36SAndroid Build Coastguard Worker 522*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsType StubsType, apiVersionsXml android.WritablePath) { 523*333d2b36SAndroid Build Coastguard Worker var apiVersions android.Path 524*333d2b36SAndroid Build Coastguard Worker if proptools.Bool(d.properties.Api_levels_annotations_enabled) { 525*333d2b36SAndroid Build Coastguard Worker d.apiLevelsGenerationFlags(ctx, cmd, stubsType, apiVersionsXml) 526*333d2b36SAndroid Build Coastguard Worker apiVersions = apiVersionsXml 527*333d2b36SAndroid Build Coastguard Worker } else { 528*333d2b36SAndroid Build Coastguard Worker ctx.VisitDirectDepsWithTag(metalavaAPILevelsModuleTag, func(m android.Module) { 529*333d2b36SAndroid Build Coastguard Worker if s, ok := m.(*Droidstubs); ok { 530*333d2b36SAndroid Build Coastguard Worker if stubsType == Everything { 531*333d2b36SAndroid Build Coastguard Worker apiVersions = s.everythingArtifacts.apiVersionsXml 532*333d2b36SAndroid Build Coastguard Worker } else if stubsType == Exportable { 533*333d2b36SAndroid Build Coastguard Worker apiVersions = s.exportableArtifacts.apiVersionsXml 534*333d2b36SAndroid Build Coastguard Worker } else { 535*333d2b36SAndroid Build Coastguard Worker ctx.ModuleErrorf("%s stubs type does not generate api-versions.xml file", stubsType.String()) 536*333d2b36SAndroid Build Coastguard Worker } 537*333d2b36SAndroid Build Coastguard Worker } else { 538*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf("api_levels_module", 539*333d2b36SAndroid Build Coastguard Worker "module %q is not a droidstubs module", ctx.OtherModuleName(m)) 540*333d2b36SAndroid Build Coastguard Worker } 541*333d2b36SAndroid Build Coastguard Worker }) 542*333d2b36SAndroid Build Coastguard Worker } 543*333d2b36SAndroid Build Coastguard Worker if apiVersions != nil { 544*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion().String()) 545*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename()) 546*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithInput("--apply-api-levels ", apiVersions) 547*333d2b36SAndroid Build Coastguard Worker } 548*333d2b36SAndroid Build Coastguard Worker} 549*333d2b36SAndroid Build Coastguard Worker 550*333d2b36SAndroid Build Coastguard Worker// AndroidPlusUpdatableJar is the name of some extra jars added into `module-lib` and 551*333d2b36SAndroid Build Coastguard Worker// `system-server` directories that contain all the APIs provided by the platform and updatable 552*333d2b36SAndroid Build Coastguard Worker// modules because the `android.jar` files do not. See b/337836752. 553*333d2b36SAndroid Build Coastguard Workerconst AndroidPlusUpdatableJar = "android-plus-updatable.jar" 554*333d2b36SAndroid Build Coastguard Worker 555*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) apiLevelsGenerationFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsType StubsType, apiVersionsXml android.WritablePath) { 556*333d2b36SAndroid Build Coastguard Worker if len(d.properties.Api_levels_annotations_dirs) == 0 { 557*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf("api_levels_annotations_dirs", 558*333d2b36SAndroid Build Coastguard Worker "has to be non-empty if api levels annotations was enabled!") 559*333d2b36SAndroid Build Coastguard Worker } 560*333d2b36SAndroid Build Coastguard Worker 561*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithOutput("--generate-api-levels ", apiVersionsXml) 562*333d2b36SAndroid Build Coastguard Worker 563*333d2b36SAndroid Build Coastguard Worker filename := proptools.StringDefault(d.properties.Api_levels_jar_filename, "android.jar") 564*333d2b36SAndroid Build Coastguard Worker 565*333d2b36SAndroid Build Coastguard Worker // TODO: Avoid the duplication of API surfaces, reuse apiScope. 566*333d2b36SAndroid Build Coastguard Worker // Add all relevant --android-jar-pattern patterns for Metalava. 567*333d2b36SAndroid Build Coastguard Worker // When parsing a stub jar for a specific version, Metalava picks the first pattern that defines 568*333d2b36SAndroid Build Coastguard Worker // an actual file present on disk (in the order the patterns were passed). For system APIs for 569*333d2b36SAndroid Build Coastguard Worker // privileged apps that are only defined since API level 21 (Lollipop), fallback to public stubs 570*333d2b36SAndroid Build Coastguard Worker // for older releases. Similarly, module-lib falls back to system API. 571*333d2b36SAndroid Build Coastguard Worker var sdkDirs []string 572*333d2b36SAndroid Build Coastguard Worker apiLevelsSdkType := proptools.StringDefault(d.properties.Api_levels_sdk_type, "public") 573*333d2b36SAndroid Build Coastguard Worker switch apiLevelsSdkType { 574*333d2b36SAndroid Build Coastguard Worker case "system-server": 575*333d2b36SAndroid Build Coastguard Worker sdkDirs = []string{"system-server", "module-lib", "system", "public"} 576*333d2b36SAndroid Build Coastguard Worker case "module-lib": 577*333d2b36SAndroid Build Coastguard Worker sdkDirs = []string{"module-lib", "system", "public"} 578*333d2b36SAndroid Build Coastguard Worker case "system": 579*333d2b36SAndroid Build Coastguard Worker sdkDirs = []string{"system", "public"} 580*333d2b36SAndroid Build Coastguard Worker case "public": 581*333d2b36SAndroid Build Coastguard Worker sdkDirs = []string{"public"} 582*333d2b36SAndroid Build Coastguard Worker default: 583*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf("api_levels_sdk_type", "needs to be one of %v", allowedApiLevelSdkTypes) 584*333d2b36SAndroid Build Coastguard Worker return 585*333d2b36SAndroid Build Coastguard Worker } 586*333d2b36SAndroid Build Coastguard Worker 587*333d2b36SAndroid Build Coastguard Worker // Construct a pattern to match the appropriate extensions that should be included in the 588*333d2b36SAndroid Build Coastguard Worker // generated api-versions.xml file. 589*333d2b36SAndroid Build Coastguard Worker // 590*333d2b36SAndroid Build Coastguard Worker // Use the first item in the sdkDirs array as that is the sdk type for the target API levels 591*333d2b36SAndroid Build Coastguard Worker // being generated but has the advantage over `Api_levels_sdk_type` as it has been validated. 592*333d2b36SAndroid Build Coastguard Worker // The exception is for system-server which needs to include module-lib and system-server. That 593*333d2b36SAndroid Build Coastguard Worker // is because while system-server extends module-lib the system-server extension directory only 594*333d2b36SAndroid Build Coastguard Worker // contains service-* modules which provide system-server APIs it does not list the modules which 595*333d2b36SAndroid Build Coastguard Worker // only provide a module-lib, so they have to be included separately. 596*333d2b36SAndroid Build Coastguard Worker extensionSurfacesPattern := sdkDirs[0] 597*333d2b36SAndroid Build Coastguard Worker if apiLevelsSdkType == "system-server" { 598*333d2b36SAndroid Build Coastguard Worker // Take the first two items in sdkDirs, which are system-server and module-lib, and construct 599*333d2b36SAndroid Build Coastguard Worker // a pattern that will match either. 600*333d2b36SAndroid Build Coastguard Worker extensionSurfacesPattern = strings.Join(sdkDirs[0:2], "|") 601*333d2b36SAndroid Build Coastguard Worker } 602*333d2b36SAndroid Build Coastguard Worker extensionsPattern := fmt.Sprintf(`/extensions/[0-9]+/(%s)/.*\.jar`, extensionSurfacesPattern) 603*333d2b36SAndroid Build Coastguard Worker 604*333d2b36SAndroid Build Coastguard Worker var dirs []string 605*333d2b36SAndroid Build Coastguard Worker var extensions_dir string 606*333d2b36SAndroid Build Coastguard Worker ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) { 607*333d2b36SAndroid Build Coastguard Worker if t, ok := m.(*ExportedDroiddocDir); ok { 608*333d2b36SAndroid Build Coastguard Worker extRegex := regexp.MustCompile(t.dir.String() + extensionsPattern) 609*333d2b36SAndroid Build Coastguard Worker 610*333d2b36SAndroid Build Coastguard Worker // Grab the first extensions_dir and we find while scanning ExportedDroiddocDir.deps; 611*333d2b36SAndroid Build Coastguard Worker // ideally this should be read from prebuiltApis.properties.Extensions_* 612*333d2b36SAndroid Build Coastguard Worker for _, dep := range t.deps { 613*333d2b36SAndroid Build Coastguard Worker // Check to see if it matches an extension first. 614*333d2b36SAndroid Build Coastguard Worker depBase := dep.Base() 615*333d2b36SAndroid Build Coastguard Worker if extRegex.MatchString(dep.String()) && d.properties.Extensions_info_file != nil { 616*333d2b36SAndroid Build Coastguard Worker if extensions_dir == "" { 617*333d2b36SAndroid Build Coastguard Worker extensions_dir = t.dir.String() + "/extensions" 618*333d2b36SAndroid Build Coastguard Worker } 619*333d2b36SAndroid Build Coastguard Worker cmd.Implicit(dep) 620*333d2b36SAndroid Build Coastguard Worker } else if depBase == filename { 621*333d2b36SAndroid Build Coastguard Worker // Check to see if it matches a dessert release for an SDK, e.g. Android, Car, Wear, etc.. 622*333d2b36SAndroid Build Coastguard Worker cmd.Implicit(dep) 623*333d2b36SAndroid Build Coastguard Worker } else if depBase == AndroidPlusUpdatableJar && d.properties.Extensions_info_file != nil { 624*333d2b36SAndroid Build Coastguard Worker // The output api-versions.xml has been requested to include information on SDK 625*333d2b36SAndroid Build Coastguard Worker // extensions. That means it also needs to include 626*333d2b36SAndroid Build Coastguard Worker // so 627*333d2b36SAndroid Build Coastguard Worker // The module-lib and system-server directories should use `android-plus-updatable.jar` 628*333d2b36SAndroid Build Coastguard Worker // instead of `android.jar`. See AndroidPlusUpdatableJar for more information. 629*333d2b36SAndroid Build Coastguard Worker cmd.Implicit(dep) 630*333d2b36SAndroid Build Coastguard Worker } else if filename != "android.jar" && depBase == "android.jar" { 631*333d2b36SAndroid Build Coastguard Worker // Metalava implicitly searches these patterns: 632*333d2b36SAndroid Build Coastguard Worker // prebuilts/tools/common/api-versions/android-%/android.jar 633*333d2b36SAndroid Build Coastguard Worker // prebuilts/sdk/%/public/android.jar 634*333d2b36SAndroid Build Coastguard Worker // Add android.jar files from the api_levels_annotations_dirs directories to try 635*333d2b36SAndroid Build Coastguard Worker // to satisfy these patterns. If Metalava can't find a match for an API level 636*333d2b36SAndroid Build Coastguard Worker // between 1 and 28 in at least one pattern it will fail. 637*333d2b36SAndroid Build Coastguard Worker cmd.Implicit(dep) 638*333d2b36SAndroid Build Coastguard Worker } 639*333d2b36SAndroid Build Coastguard Worker } 640*333d2b36SAndroid Build Coastguard Worker 641*333d2b36SAndroid Build Coastguard Worker dirs = append(dirs, t.dir.String()) 642*333d2b36SAndroid Build Coastguard Worker } else { 643*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf("api_levels_annotations_dirs", 644*333d2b36SAndroid Build Coastguard Worker "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m)) 645*333d2b36SAndroid Build Coastguard Worker } 646*333d2b36SAndroid Build Coastguard Worker }) 647*333d2b36SAndroid Build Coastguard Worker 648*333d2b36SAndroid Build Coastguard Worker // Generate the list of --android-jar-pattern options. The order matters so the first one which 649*333d2b36SAndroid Build Coastguard Worker // matches will be the one that is used for a specific api level.. 650*333d2b36SAndroid Build Coastguard Worker for _, sdkDir := range sdkDirs { 651*333d2b36SAndroid Build Coastguard Worker for _, dir := range dirs { 652*333d2b36SAndroid Build Coastguard Worker addPattern := func(jarFilename string) { 653*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithArg("--android-jar-pattern ", fmt.Sprintf("%s/%%/%s/%s", dir, sdkDir, jarFilename)) 654*333d2b36SAndroid Build Coastguard Worker } 655*333d2b36SAndroid Build Coastguard Worker 656*333d2b36SAndroid Build Coastguard Worker if sdkDir == "module-lib" || sdkDir == "system-server" { 657*333d2b36SAndroid Build Coastguard Worker // The module-lib and system-server android.jars do not include the updatable modules (as 658*333d2b36SAndroid Build Coastguard Worker // doing so in the source would introduce dependency cycles and the prebuilts have to 659*333d2b36SAndroid Build Coastguard Worker // match the sources). So, instead an additional `android-plus-updatable.jar` will be used 660*333d2b36SAndroid Build Coastguard Worker // that does include the updatable modules and this pattern will match that. This pattern 661*333d2b36SAndroid Build Coastguard Worker // is added in addition to the following pattern to decouple this change from the change 662*333d2b36SAndroid Build Coastguard Worker // to add the `android-plus-updatable.jar`. 663*333d2b36SAndroid Build Coastguard Worker addPattern(AndroidPlusUpdatableJar) 664*333d2b36SAndroid Build Coastguard Worker } 665*333d2b36SAndroid Build Coastguard Worker 666*333d2b36SAndroid Build Coastguard Worker addPattern(filename) 667*333d2b36SAndroid Build Coastguard Worker } 668*333d2b36SAndroid Build Coastguard Worker } 669*333d2b36SAndroid Build Coastguard Worker 670*333d2b36SAndroid Build Coastguard Worker if d.properties.Extensions_info_file != nil { 671*333d2b36SAndroid Build Coastguard Worker if extensions_dir == "" { 672*333d2b36SAndroid Build Coastguard Worker ctx.ModuleErrorf("extensions_info_file set, but no SDK extension dirs found") 673*333d2b36SAndroid Build Coastguard Worker } 674*333d2b36SAndroid Build Coastguard Worker info_file := android.PathForModuleSrc(ctx, *d.properties.Extensions_info_file) 675*333d2b36SAndroid Build Coastguard Worker cmd.Implicit(info_file) 676*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithArg("--sdk-extensions-root ", extensions_dir) 677*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithArg("--sdk-extensions-info ", info_file.String()) 678*333d2b36SAndroid Build Coastguard Worker } 679*333d2b36SAndroid Build Coastguard Worker} 680*333d2b36SAndroid Build Coastguard Worker 681*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) apiCompatibilityFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsType StubsType) { 682*333d2b36SAndroid Build Coastguard Worker if len(d.Javadoc.properties.Out) > 0 { 683*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf("out", "out property may not be combined with check_api") 684*333d2b36SAndroid Build Coastguard Worker } 685*333d2b36SAndroid Build Coastguard Worker 686*333d2b36SAndroid Build Coastguard Worker apiFiles := android.PathsForModuleSrc(ctx, []string{String(d.properties.Check_api.Last_released.Api_file)}) 687*333d2b36SAndroid Build Coastguard Worker removedApiFiles := android.PathsForModuleSrc(ctx, []string{String(d.properties.Check_api.Last_released.Removed_api_file)}) 688*333d2b36SAndroid Build Coastguard Worker 689*333d2b36SAndroid Build Coastguard Worker cmd.FlagForEachInput("--check-compatibility:api:released ", apiFiles) 690*333d2b36SAndroid Build Coastguard Worker cmd.FlagForEachInput("--check-compatibility:removed:released ", removedApiFiles) 691*333d2b36SAndroid Build Coastguard Worker 692*333d2b36SAndroid Build Coastguard Worker baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file) 693*333d2b36SAndroid Build Coastguard Worker if baselineFile.Valid() { 694*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithInput("--baseline:compatibility:released ", baselineFile.Path()) 695*333d2b36SAndroid Build Coastguard Worker } 696*333d2b36SAndroid Build Coastguard Worker} 697*333d2b36SAndroid Build Coastguard Worker 698*333d2b36SAndroid Build Coastguard Workerfunc metalavaUseRbe(ctx android.ModuleContext) bool { 699*333d2b36SAndroid Build Coastguard Worker return ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_METALAVA") 700*333d2b36SAndroid Build Coastguard Worker} 701*333d2b36SAndroid Build Coastguard Worker 702*333d2b36SAndroid Build Coastguard Workerfunc metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths, 703*333d2b36SAndroid Build Coastguard Worker srcJarList android.Path, homeDir android.WritablePath, params stubsCommandConfigParams, configFiles android.Paths) *android.RuleBuilderCommand { 704*333d2b36SAndroid Build Coastguard Worker rule.Command().Text("rm -rf").Flag(homeDir.String()) 705*333d2b36SAndroid Build Coastguard Worker rule.Command().Text("mkdir -p").Flag(homeDir.String()) 706*333d2b36SAndroid Build Coastguard Worker 707*333d2b36SAndroid Build Coastguard Worker cmd := rule.Command() 708*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithArg("ANDROID_PREFS_ROOT=", homeDir.String()) 709*333d2b36SAndroid Build Coastguard Worker 710*333d2b36SAndroid Build Coastguard Worker if metalavaUseRbe(ctx) { 711*333d2b36SAndroid Build Coastguard Worker rule.Remoteable(android.RemoteRuleSupports{RBE: true}) 712*333d2b36SAndroid Build Coastguard Worker execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy) 713*333d2b36SAndroid Build Coastguard Worker compare := ctx.Config().IsEnvTrue("RBE_METALAVA_COMPARE") 714*333d2b36SAndroid Build Coastguard Worker remoteUpdateCache := !ctx.Config().IsEnvFalse("RBE_METALAVA_REMOTE_UPDATE_CACHE") 715*333d2b36SAndroid Build Coastguard Worker labels := map[string]string{"type": "tool", "name": "metalava"} 716*333d2b36SAndroid Build Coastguard Worker // TODO: metalava pool rejects these jobs 717*333d2b36SAndroid Build Coastguard Worker pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "java16") 718*333d2b36SAndroid Build Coastguard Worker rule.Rewrapper(&remoteexec.REParams{ 719*333d2b36SAndroid Build Coastguard Worker Labels: labels, 720*333d2b36SAndroid Build Coastguard Worker ExecStrategy: execStrategy, 721*333d2b36SAndroid Build Coastguard Worker ToolchainInputs: []string{config.JavaCmd(ctx).String()}, 722*333d2b36SAndroid Build Coastguard Worker Platform: map[string]string{remoteexec.PoolKey: pool}, 723*333d2b36SAndroid Build Coastguard Worker Compare: compare, 724*333d2b36SAndroid Build Coastguard Worker NumLocalRuns: 1, 725*333d2b36SAndroid Build Coastguard Worker NumRemoteRuns: 1, 726*333d2b36SAndroid Build Coastguard Worker NoRemoteUpdateCache: !remoteUpdateCache, 727*333d2b36SAndroid Build Coastguard Worker }) 728*333d2b36SAndroid Build Coastguard Worker } 729*333d2b36SAndroid Build Coastguard Worker 730*333d2b36SAndroid Build Coastguard Worker cmd.BuiltTool("metalava").ImplicitTool(ctx.Config().HostJavaToolPath(ctx, "metalava.jar")). 731*333d2b36SAndroid Build Coastguard Worker Flag(config.JavacVmFlags). 732*333d2b36SAndroid Build Coastguard Worker Flag(config.MetalavaAddOpens). 733*333d2b36SAndroid Build Coastguard Worker FlagWithArg("--java-source ", params.javaVersion.String()). 734*333d2b36SAndroid Build Coastguard Worker FlagWithRspFileInputList("@", android.PathForModuleOut(ctx, fmt.Sprintf("%s.metalava.rsp", params.stubsType.String())), srcs). 735*333d2b36SAndroid Build Coastguard Worker FlagWithInput("@", srcJarList) 736*333d2b36SAndroid Build Coastguard Worker 737*333d2b36SAndroid Build Coastguard Worker // Metalava does not differentiate between bootclasspath and classpath and has not done so for 738*333d2b36SAndroid Build Coastguard Worker // years, so it is unlikely to change any time soon. 739*333d2b36SAndroid Build Coastguard Worker combinedPaths := append(([]android.Path)(nil), params.deps.bootClasspath.Paths()...) 740*333d2b36SAndroid Build Coastguard Worker combinedPaths = append(combinedPaths, params.deps.classpath.Paths()...) 741*333d2b36SAndroid Build Coastguard Worker if len(combinedPaths) > 0 { 742*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithInputList("--classpath ", combinedPaths, ":") 743*333d2b36SAndroid Build Coastguard Worker } 744*333d2b36SAndroid Build Coastguard Worker 745*333d2b36SAndroid Build Coastguard Worker cmd.Flag(config.MetalavaFlags) 746*333d2b36SAndroid Build Coastguard Worker 747*333d2b36SAndroid Build Coastguard Worker addMetalavaConfigFilesToCmd(cmd, configFiles) 748*333d2b36SAndroid Build Coastguard Worker 749*333d2b36SAndroid Build Coastguard Worker return cmd 750*333d2b36SAndroid Build Coastguard Worker} 751*333d2b36SAndroid Build Coastguard Worker 752*333d2b36SAndroid Build Coastguard Worker// MetalavaConfigFilegroup is the name of the filegroup in build/soong/java/metalava that lists 753*333d2b36SAndroid Build Coastguard Worker// the configuration files to pass to Metalava. 754*333d2b36SAndroid Build Coastguard Workerconst MetalavaConfigFilegroup = "metalava-config-files" 755*333d2b36SAndroid Build Coastguard Worker 756*333d2b36SAndroid Build Coastguard Worker// Get a reference to the MetalavaConfigFilegroup suitable for use in a property. 757*333d2b36SAndroid Build Coastguard Workerfunc getMetalavaConfigFilegroupReference() []string { 758*333d2b36SAndroid Build Coastguard Worker return []string{":" + MetalavaConfigFilegroup} 759*333d2b36SAndroid Build Coastguard Worker} 760*333d2b36SAndroid Build Coastguard Worker 761*333d2b36SAndroid Build Coastguard Worker// addMetalavaConfigFilesToCmd adds --config-file options to use the config files list in the 762*333d2b36SAndroid Build Coastguard Worker// MetalavaConfigFilegroup filegroup. 763*333d2b36SAndroid Build Coastguard Workerfunc addMetalavaConfigFilesToCmd(cmd *android.RuleBuilderCommand, configFiles android.Paths) { 764*333d2b36SAndroid Build Coastguard Worker cmd.FlagForEachInput("--config-file ", configFiles) 765*333d2b36SAndroid Build Coastguard Worker} 766*333d2b36SAndroid Build Coastguard Worker 767*333d2b36SAndroid Build Coastguard Worker// Pass flagged apis related flags to metalava. When aconfig_declarations property is not 768*333d2b36SAndroid Build Coastguard Worker// defined for a module, simply revert all flagged apis annotations. If aconfig_declarations 769*333d2b36SAndroid Build Coastguard Worker// property is defined, apply transformations and only revert the flagged apis that are not 770*333d2b36SAndroid Build Coastguard Worker// enabled via release configurations and are not specified in aconfig_declarations 771*333d2b36SAndroid Build Coastguard Workerfunc generateRevertAnnotationArgs(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsType StubsType, aconfigFlagsPaths android.Paths) { 772*333d2b36SAndroid Build Coastguard Worker var filterArgs string 773*333d2b36SAndroid Build Coastguard Worker switch stubsType { 774*333d2b36SAndroid Build Coastguard Worker // No flagged apis specific flags need to be passed to metalava when generating 775*333d2b36SAndroid Build Coastguard Worker // everything stubs 776*333d2b36SAndroid Build Coastguard Worker case Everything: 777*333d2b36SAndroid Build Coastguard Worker return 778*333d2b36SAndroid Build Coastguard Worker 779*333d2b36SAndroid Build Coastguard Worker case Runtime: 780*333d2b36SAndroid Build Coastguard Worker filterArgs = "--filter='state:ENABLED+permission:READ_ONLY' --filter='permission:READ_WRITE'" 781*333d2b36SAndroid Build Coastguard Worker 782*333d2b36SAndroid Build Coastguard Worker case Exportable: 783*333d2b36SAndroid Build Coastguard Worker // When the build flag RELEASE_EXPORT_RUNTIME_APIS is set to true, apis marked with 784*333d2b36SAndroid Build Coastguard Worker // the flagged apis that have read_write permissions are exposed on top of the enabled 785*333d2b36SAndroid Build Coastguard Worker // and read_only apis. This is to support local override of flag values at runtime. 786*333d2b36SAndroid Build Coastguard Worker if ctx.Config().ReleaseExportRuntimeApis() { 787*333d2b36SAndroid Build Coastguard Worker filterArgs = "--filter='state:ENABLED+permission:READ_ONLY' --filter='permission:READ_WRITE'" 788*333d2b36SAndroid Build Coastguard Worker } else { 789*333d2b36SAndroid Build Coastguard Worker filterArgs = "--filter='state:ENABLED+permission:READ_ONLY'" 790*333d2b36SAndroid Build Coastguard Worker } 791*333d2b36SAndroid Build Coastguard Worker } 792*333d2b36SAndroid Build Coastguard Worker 793*333d2b36SAndroid Build Coastguard Worker if len(aconfigFlagsPaths) == 0 { 794*333d2b36SAndroid Build Coastguard Worker // This argument should not be added for "everything" stubs 795*333d2b36SAndroid Build Coastguard Worker cmd.Flag("--revert-annotation android.annotation.FlaggedApi") 796*333d2b36SAndroid Build Coastguard Worker return 797*333d2b36SAndroid Build Coastguard Worker } 798*333d2b36SAndroid Build Coastguard Worker 799*333d2b36SAndroid Build Coastguard Worker releasedFlaggedApisFile := android.PathForModuleOut(ctx, fmt.Sprintf("released-flagged-apis-%s.txt", stubsType.String())) 800*333d2b36SAndroid Build Coastguard Worker revertAnnotationsFile := android.PathForModuleOut(ctx, fmt.Sprintf("revert-annotations-%s.txt", stubsType.String())) 801*333d2b36SAndroid Build Coastguard Worker 802*333d2b36SAndroid Build Coastguard Worker ctx.Build(pctx, android.BuildParams{ 803*333d2b36SAndroid Build Coastguard Worker Rule: gatherReleasedFlaggedApisRule, 804*333d2b36SAndroid Build Coastguard Worker Inputs: aconfigFlagsPaths, 805*333d2b36SAndroid Build Coastguard Worker Output: releasedFlaggedApisFile, 806*333d2b36SAndroid Build Coastguard Worker Description: fmt.Sprintf("%s gather aconfig flags", stubsType), 807*333d2b36SAndroid Build Coastguard Worker Args: map[string]string{ 808*333d2b36SAndroid Build Coastguard Worker "flags_path": android.JoinPathsWithPrefix(aconfigFlagsPaths, "--cache "), 809*333d2b36SAndroid Build Coastguard Worker "filter_args": filterArgs, 810*333d2b36SAndroid Build Coastguard Worker }, 811*333d2b36SAndroid Build Coastguard Worker }) 812*333d2b36SAndroid Build Coastguard Worker 813*333d2b36SAndroid Build Coastguard Worker ctx.Build(pctx, android.BuildParams{ 814*333d2b36SAndroid Build Coastguard Worker Rule: generateMetalavaRevertAnnotationsRule, 815*333d2b36SAndroid Build Coastguard Worker Input: releasedFlaggedApisFile, 816*333d2b36SAndroid Build Coastguard Worker Output: revertAnnotationsFile, 817*333d2b36SAndroid Build Coastguard Worker Description: fmt.Sprintf("%s revert annotations", stubsType), 818*333d2b36SAndroid Build Coastguard Worker }) 819*333d2b36SAndroid Build Coastguard Worker 820*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithInput("@", revertAnnotationsFile) 821*333d2b36SAndroid Build Coastguard Worker} 822*333d2b36SAndroid Build Coastguard Worker 823*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) commonMetalavaStubCmd(ctx android.ModuleContext, rule *android.RuleBuilder, 824*333d2b36SAndroid Build Coastguard Worker params stubsCommandParams) *android.RuleBuilderCommand { 825*333d2b36SAndroid Build Coastguard Worker if BoolDefault(d.properties.High_mem, false) { 826*333d2b36SAndroid Build Coastguard Worker // This metalava run uses lots of memory, restrict the number of metalava jobs that can run in parallel. 827*333d2b36SAndroid Build Coastguard Worker rule.HighMem() 828*333d2b36SAndroid Build Coastguard Worker } 829*333d2b36SAndroid Build Coastguard Worker 830*333d2b36SAndroid Build Coastguard Worker if params.stubConfig.generateStubs { 831*333d2b36SAndroid Build Coastguard Worker rule.Command().Text("rm -rf").Text(params.stubsDir.String()) 832*333d2b36SAndroid Build Coastguard Worker rule.Command().Text("mkdir -p").Text(params.stubsDir.String()) 833*333d2b36SAndroid Build Coastguard Worker } 834*333d2b36SAndroid Build Coastguard Worker 835*333d2b36SAndroid Build Coastguard Worker srcJarList := zipSyncCmd(ctx, rule, params.srcJarDir, d.Javadoc.srcJars) 836*333d2b36SAndroid Build Coastguard Worker 837*333d2b36SAndroid Build Coastguard Worker homeDir := android.PathForModuleOut(ctx, params.stubConfig.stubsType.String(), "home") 838*333d2b36SAndroid Build Coastguard Worker 839*333d2b36SAndroid Build Coastguard Worker configFiles := android.PathsForModuleSrc(ctx, d.properties.ConfigFiles) 840*333d2b36SAndroid Build Coastguard Worker 841*333d2b36SAndroid Build Coastguard Worker cmd := metalavaCmd(ctx, rule, d.Javadoc.srcFiles, srcJarList, homeDir, params.stubConfig, configFiles) 842*333d2b36SAndroid Build Coastguard Worker cmd.Implicits(d.Javadoc.implicits) 843*333d2b36SAndroid Build Coastguard Worker 844*333d2b36SAndroid Build Coastguard Worker d.stubsFlags(ctx, cmd, params.stubsDir, params.stubConfig.stubsType, params.stubConfig.checkApi) 845*333d2b36SAndroid Build Coastguard Worker 846*333d2b36SAndroid Build Coastguard Worker if params.stubConfig.writeSdkValues { 847*333d2b36SAndroid Build Coastguard Worker d.sdkValuesFlags(ctx, cmd, params.metadataDir) 848*333d2b36SAndroid Build Coastguard Worker } 849*333d2b36SAndroid Build Coastguard Worker 850*333d2b36SAndroid Build Coastguard Worker annotationParams := annotationFlagsParams{ 851*333d2b36SAndroid Build Coastguard Worker migratingNullability: params.stubConfig.migratingNullability, 852*333d2b36SAndroid Build Coastguard Worker validatingNullability: params.stubConfig.validatingNullability, 853*333d2b36SAndroid Build Coastguard Worker nullabilityWarningsFile: params.nullabilityWarningsFile, 854*333d2b36SAndroid Build Coastguard Worker annotationsZip: params.annotationsZip, 855*333d2b36SAndroid Build Coastguard Worker } 856*333d2b36SAndroid Build Coastguard Worker 857*333d2b36SAndroid Build Coastguard Worker d.annotationsFlags(ctx, cmd, annotationParams) 858*333d2b36SAndroid Build Coastguard Worker d.inclusionAnnotationsFlags(ctx, cmd) 859*333d2b36SAndroid Build Coastguard Worker d.apiLevelsAnnotationsFlags(ctx, cmd, params.stubConfig.stubsType, params.apiVersionsXml) 860*333d2b36SAndroid Build Coastguard Worker 861*333d2b36SAndroid Build Coastguard Worker if params.stubConfig.doCheckReleased { 862*333d2b36SAndroid Build Coastguard Worker d.apiCompatibilityFlags(ctx, cmd, params.stubConfig.stubsType) 863*333d2b36SAndroid Build Coastguard Worker } 864*333d2b36SAndroid Build Coastguard Worker 865*333d2b36SAndroid Build Coastguard Worker d.expandArgs(ctx, cmd) 866*333d2b36SAndroid Build Coastguard Worker 867*333d2b36SAndroid Build Coastguard Worker for _, o := range d.Javadoc.properties.Out { 868*333d2b36SAndroid Build Coastguard Worker cmd.ImplicitOutput(android.PathForModuleGen(ctx, o)) 869*333d2b36SAndroid Build Coastguard Worker } 870*333d2b36SAndroid Build Coastguard Worker 871*333d2b36SAndroid Build Coastguard Worker return cmd 872*333d2b36SAndroid Build Coastguard Worker} 873*333d2b36SAndroid Build Coastguard Worker 874*333d2b36SAndroid Build Coastguard Worker// Sandbox rule for generating the everything stubs and other artifacts 875*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) everythingStubCmd(ctx android.ModuleContext, params stubsCommandConfigParams) { 876*333d2b36SAndroid Build Coastguard Worker srcJarDir := android.PathForModuleOut(ctx, Everything.String(), "srcjars") 877*333d2b36SAndroid Build Coastguard Worker rule := android.NewRuleBuilder(pctx, ctx) 878*333d2b36SAndroid Build Coastguard Worker rule.Sbox(android.PathForModuleOut(ctx, Everything.String()), 879*333d2b36SAndroid Build Coastguard Worker android.PathForModuleOut(ctx, "metalava.sbox.textproto")). 880*333d2b36SAndroid Build Coastguard Worker SandboxInputs() 881*333d2b36SAndroid Build Coastguard Worker 882*333d2b36SAndroid Build Coastguard Worker var stubsDir android.OptionalPath 883*333d2b36SAndroid Build Coastguard Worker if params.generateStubs { 884*333d2b36SAndroid Build Coastguard Worker stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, Everything.String(), "stubsDir")) 885*333d2b36SAndroid Build Coastguard Worker d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, Everything.String(), ctx.ModuleName()+"-"+"stubs.srcjar") 886*333d2b36SAndroid Build Coastguard Worker } 887*333d2b36SAndroid Build Coastguard Worker 888*333d2b36SAndroid Build Coastguard Worker if params.writeSdkValues { 889*333d2b36SAndroid Build Coastguard Worker d.everythingArtifacts.metadataDir = android.PathForModuleOut(ctx, Everything.String(), "metadata") 890*333d2b36SAndroid Build Coastguard Worker d.everythingArtifacts.metadataZip = android.PathForModuleOut(ctx, Everything.String(), ctx.ModuleName()+"-metadata.zip") 891*333d2b36SAndroid Build Coastguard Worker } 892*333d2b36SAndroid Build Coastguard Worker 893*333d2b36SAndroid Build Coastguard Worker if Bool(d.properties.Annotations_enabled) { 894*333d2b36SAndroid Build Coastguard Worker if params.validatingNullability { 895*333d2b36SAndroid Build Coastguard Worker d.everythingArtifacts.nullabilityWarningsFile = android.PathForModuleOut(ctx, Everything.String(), ctx.ModuleName()+"_nullability_warnings.txt") 896*333d2b36SAndroid Build Coastguard Worker } 897*333d2b36SAndroid Build Coastguard Worker d.everythingArtifacts.annotationsZip = android.PathForModuleOut(ctx, Everything.String(), ctx.ModuleName()+"_annotations.zip") 898*333d2b36SAndroid Build Coastguard Worker } 899*333d2b36SAndroid Build Coastguard Worker if Bool(d.properties.Api_levels_annotations_enabled) { 900*333d2b36SAndroid Build Coastguard Worker d.everythingArtifacts.apiVersionsXml = android.PathForModuleOut(ctx, Everything.String(), "api-versions.xml") 901*333d2b36SAndroid Build Coastguard Worker } 902*333d2b36SAndroid Build Coastguard Worker 903*333d2b36SAndroid Build Coastguard Worker commonCmdParams := stubsCommandParams{ 904*333d2b36SAndroid Build Coastguard Worker srcJarDir: srcJarDir, 905*333d2b36SAndroid Build Coastguard Worker stubsDir: stubsDir, 906*333d2b36SAndroid Build Coastguard Worker stubsSrcJar: d.Javadoc.stubsSrcJar, 907*333d2b36SAndroid Build Coastguard Worker metadataDir: d.everythingArtifacts.metadataDir, 908*333d2b36SAndroid Build Coastguard Worker apiVersionsXml: d.everythingArtifacts.apiVersionsXml, 909*333d2b36SAndroid Build Coastguard Worker nullabilityWarningsFile: d.everythingArtifacts.nullabilityWarningsFile, 910*333d2b36SAndroid Build Coastguard Worker annotationsZip: d.everythingArtifacts.annotationsZip, 911*333d2b36SAndroid Build Coastguard Worker stubConfig: params, 912*333d2b36SAndroid Build Coastguard Worker } 913*333d2b36SAndroid Build Coastguard Worker 914*333d2b36SAndroid Build Coastguard Worker cmd := d.commonMetalavaStubCmd(ctx, rule, commonCmdParams) 915*333d2b36SAndroid Build Coastguard Worker 916*333d2b36SAndroid Build Coastguard Worker d.everythingOptionalCmd(ctx, cmd, params.doApiLint, params.doCheckReleased) 917*333d2b36SAndroid Build Coastguard Worker 918*333d2b36SAndroid Build Coastguard Worker if params.generateStubs { 919*333d2b36SAndroid Build Coastguard Worker rule.Command(). 920*333d2b36SAndroid Build Coastguard Worker BuiltTool("soong_zip"). 921*333d2b36SAndroid Build Coastguard Worker Flag("-write_if_changed"). 922*333d2b36SAndroid Build Coastguard Worker Flag("-jar"). 923*333d2b36SAndroid Build Coastguard Worker FlagWithOutput("-o ", d.Javadoc.stubsSrcJar). 924*333d2b36SAndroid Build Coastguard Worker FlagWithArg("-C ", stubsDir.String()). 925*333d2b36SAndroid Build Coastguard Worker FlagWithArg("-D ", stubsDir.String()) 926*333d2b36SAndroid Build Coastguard Worker } 927*333d2b36SAndroid Build Coastguard Worker 928*333d2b36SAndroid Build Coastguard Worker if params.writeSdkValues { 929*333d2b36SAndroid Build Coastguard Worker rule.Command(). 930*333d2b36SAndroid Build Coastguard Worker BuiltTool("soong_zip"). 931*333d2b36SAndroid Build Coastguard Worker Flag("-write_if_changed"). 932*333d2b36SAndroid Build Coastguard Worker Flag("-d"). 933*333d2b36SAndroid Build Coastguard Worker FlagWithOutput("-o ", d.everythingArtifacts.metadataZip). 934*333d2b36SAndroid Build Coastguard Worker FlagWithArg("-C ", d.everythingArtifacts.metadataDir.String()). 935*333d2b36SAndroid Build Coastguard Worker FlagWithArg("-D ", d.everythingArtifacts.metadataDir.String()) 936*333d2b36SAndroid Build Coastguard Worker } 937*333d2b36SAndroid Build Coastguard Worker 938*333d2b36SAndroid Build Coastguard Worker // TODO: We don't really need two separate API files, but this is a reminiscence of how 939*333d2b36SAndroid Build Coastguard Worker // we used to run metalava separately for API lint and the "last_released" check. Unify them. 940*333d2b36SAndroid Build Coastguard Worker if params.doApiLint { 941*333d2b36SAndroid Build Coastguard Worker rule.Command().Text("touch").Output(d.apiLintTimestamp) 942*333d2b36SAndroid Build Coastguard Worker } 943*333d2b36SAndroid Build Coastguard Worker if params.doCheckReleased { 944*333d2b36SAndroid Build Coastguard Worker rule.Command().Text("touch").Output(d.checkLastReleasedApiTimestamp) 945*333d2b36SAndroid Build Coastguard Worker } 946*333d2b36SAndroid Build Coastguard Worker 947*333d2b36SAndroid Build Coastguard Worker // TODO(b/183630617): rewrapper doesn't support restat rules 948*333d2b36SAndroid Build Coastguard Worker if !metalavaUseRbe(ctx) { 949*333d2b36SAndroid Build Coastguard Worker rule.Restat() 950*333d2b36SAndroid Build Coastguard Worker } 951*333d2b36SAndroid Build Coastguard Worker 952*333d2b36SAndroid Build Coastguard Worker zipSyncCleanupCmd(rule, srcJarDir) 953*333d2b36SAndroid Build Coastguard Worker 954*333d2b36SAndroid Build Coastguard Worker rule.Build("metalava", "metalava merged") 955*333d2b36SAndroid Build Coastguard Worker} 956*333d2b36SAndroid Build Coastguard Worker 957*333d2b36SAndroid Build Coastguard Worker// Sandbox rule for generating the everything artifacts that are not run by 958*333d2b36SAndroid Build Coastguard Worker// default but only run based on the module configurations 959*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) everythingOptionalCmd(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, doApiLint bool, doCheckReleased bool) { 960*333d2b36SAndroid Build Coastguard Worker 961*333d2b36SAndroid Build Coastguard Worker // Add API lint options. 962*333d2b36SAndroid Build Coastguard Worker treatDocumentationIssuesAsErrors := false 963*333d2b36SAndroid Build Coastguard Worker if doApiLint { 964*333d2b36SAndroid Build Coastguard Worker var newSince android.Paths 965*333d2b36SAndroid Build Coastguard Worker if d.properties.Check_api.Api_lint.New_since != nil { 966*333d2b36SAndroid Build Coastguard Worker newSince = android.PathsForModuleSrc(ctx, []string{proptools.String(d.properties.Check_api.Api_lint.New_since)}) 967*333d2b36SAndroid Build Coastguard Worker } 968*333d2b36SAndroid Build Coastguard Worker cmd.Flag("--api-lint") 969*333d2b36SAndroid Build Coastguard Worker cmd.FlagForEachInput("--api-lint-previous-api ", newSince) 970*333d2b36SAndroid Build Coastguard Worker d.apiLintReport = android.PathForModuleOut(ctx, Everything.String(), "api_lint_report.txt") 971*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport) // TODO: Change to ":api-lint" 972*333d2b36SAndroid Build Coastguard Worker 973*333d2b36SAndroid Build Coastguard Worker // If UnflaggedApi issues have not already been configured then make sure that existing 974*333d2b36SAndroid Build Coastguard Worker // UnflaggedApi issues are reported as warnings but issues in new/changed code are treated as 975*333d2b36SAndroid Build Coastguard Worker // errors by the Build Warnings Aye Aye Analyzer in Gerrit. 976*333d2b36SAndroid Build Coastguard Worker // Once existing issues have been fixed this will be changed to error. 977*333d2b36SAndroid Build Coastguard Worker // TODO(b/362771529): Switch to --error 978*333d2b36SAndroid Build Coastguard Worker if !strings.Contains(cmd.String(), " UnflaggedApi ") { 979*333d2b36SAndroid Build Coastguard Worker cmd.Flag("--error-when-new UnflaggedApi") 980*333d2b36SAndroid Build Coastguard Worker } 981*333d2b36SAndroid Build Coastguard Worker 982*333d2b36SAndroid Build Coastguard Worker // TODO(b/154317059): Clean up this allowlist by baselining and/or checking in last-released. 983*333d2b36SAndroid Build Coastguard Worker if d.Name() != "android.car-system-stubs-docs" && 984*333d2b36SAndroid Build Coastguard Worker d.Name() != "android.car-stubs-docs" { 985*333d2b36SAndroid Build Coastguard Worker treatDocumentationIssuesAsErrors = true 986*333d2b36SAndroid Build Coastguard Worker cmd.Flag("--warnings-as-errors") // Most lints are actually warnings. 987*333d2b36SAndroid Build Coastguard Worker } 988*333d2b36SAndroid Build Coastguard Worker 989*333d2b36SAndroid Build Coastguard Worker baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file) 990*333d2b36SAndroid Build Coastguard Worker updatedBaselineOutput := android.PathForModuleOut(ctx, Everything.String(), "api_lint_baseline.txt") 991*333d2b36SAndroid Build Coastguard Worker d.apiLintTimestamp = android.PathForModuleOut(ctx, Everything.String(), "api_lint.timestamp") 992*333d2b36SAndroid Build Coastguard Worker 993*333d2b36SAndroid Build Coastguard Worker // Note this string includes a special shell quote $' ... ', which decodes the "\n"s. 994*333d2b36SAndroid Build Coastguard Worker // 995*333d2b36SAndroid Build Coastguard Worker // TODO: metalava also has a slightly different message hardcoded. Should we unify this 996*333d2b36SAndroid Build Coastguard Worker // message and metalava's one? 997*333d2b36SAndroid Build Coastguard Worker msg := `$'` + // Enclose with $' ... ' 998*333d2b36SAndroid Build Coastguard Worker `************************************************************\n` + 999*333d2b36SAndroid Build Coastguard Worker `Your API changes are triggering API Lint warnings or errors.\n` + 1000*333d2b36SAndroid Build Coastguard Worker `\n` + 1001*333d2b36SAndroid Build Coastguard Worker `To make the failures go away:\n` + 1002*333d2b36SAndroid Build Coastguard Worker `\n` + 1003*333d2b36SAndroid Build Coastguard Worker `1. REQUIRED: Read the messages carefully and address them by` + 1004*333d2b36SAndroid Build Coastguard Worker ` fixing the API if appropriate.\n` + 1005*333d2b36SAndroid Build Coastguard Worker `2. If the failure is a false positive, you can suppress it with:\n` + 1006*333d2b36SAndroid Build Coastguard Worker ` @SuppressLint("<id>")\n` + 1007*333d2b36SAndroid Build Coastguard Worker ` where the <id> is given in brackets in the error message above.\n` 1008*333d2b36SAndroid Build Coastguard Worker 1009*333d2b36SAndroid Build Coastguard Worker if baselineFile.Valid() { 1010*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path()) 1011*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithOutput("--update-baseline:api-lint ", updatedBaselineOutput) 1012*333d2b36SAndroid Build Coastguard Worker 1013*333d2b36SAndroid Build Coastguard Worker msg += fmt.Sprintf(``+ 1014*333d2b36SAndroid Build Coastguard Worker `3. FOR LSC ONLY: You can update the baseline by executing\n`+ 1015*333d2b36SAndroid Build Coastguard Worker ` the following command:\n`+ 1016*333d2b36SAndroid Build Coastguard Worker ` (cd $ANDROID_BUILD_TOP && cp \\\n`+ 1017*333d2b36SAndroid Build Coastguard Worker ` "%s" \\\n`+ 1018*333d2b36SAndroid Build Coastguard Worker ` "%s")\n`+ 1019*333d2b36SAndroid Build Coastguard Worker ` To submit the revised baseline.txt to the main Android\n`+ 1020*333d2b36SAndroid Build Coastguard Worker ` repository, you will need approval.\n`, updatedBaselineOutput, baselineFile.Path()) 1021*333d2b36SAndroid Build Coastguard Worker } else { 1022*333d2b36SAndroid Build Coastguard Worker msg += fmt.Sprintf(``+ 1023*333d2b36SAndroid Build Coastguard Worker `3. FOR LSC ONLY: You can add a baseline file of existing lint failures\n`+ 1024*333d2b36SAndroid Build Coastguard Worker ` to the build rule of %s.\n`, d.Name()) 1025*333d2b36SAndroid Build Coastguard Worker } 1026*333d2b36SAndroid Build Coastguard Worker // Note the message ends with a ' (single quote), to close the $' ... ' . 1027*333d2b36SAndroid Build Coastguard Worker msg += `************************************************************\n'` 1028*333d2b36SAndroid Build Coastguard Worker 1029*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithArg("--error-message:api-lint ", msg) 1030*333d2b36SAndroid Build Coastguard Worker } 1031*333d2b36SAndroid Build Coastguard Worker 1032*333d2b36SAndroid Build Coastguard Worker if !treatDocumentationIssuesAsErrors { 1033*333d2b36SAndroid Build Coastguard Worker treatDocumentationIssuesAsWarningErrorWhenNew(cmd) 1034*333d2b36SAndroid Build Coastguard Worker } 1035*333d2b36SAndroid Build Coastguard Worker 1036*333d2b36SAndroid Build Coastguard Worker // Add "check released" options. (Detect incompatible API changes from the last public release) 1037*333d2b36SAndroid Build Coastguard Worker if doCheckReleased { 1038*333d2b36SAndroid Build Coastguard Worker baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file) 1039*333d2b36SAndroid Build Coastguard Worker d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, Everything.String(), "check_last_released_api.timestamp") 1040*333d2b36SAndroid Build Coastguard Worker if baselineFile.Valid() { 1041*333d2b36SAndroid Build Coastguard Worker updatedBaselineOutput := android.PathForModuleOut(ctx, Everything.String(), "last_released_baseline.txt") 1042*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithOutput("--update-baseline:compatibility:released ", updatedBaselineOutput) 1043*333d2b36SAndroid Build Coastguard Worker } 1044*333d2b36SAndroid Build Coastguard Worker // Note this string includes quote ($' ... '), which decodes the "\n"s. 1045*333d2b36SAndroid Build Coastguard Worker msg := `$'\n******************************\n` + 1046*333d2b36SAndroid Build Coastguard Worker `You have tried to change the API from what has been previously released in\n` + 1047*333d2b36SAndroid Build Coastguard Worker `an SDK. Please fix the errors listed above.\n` + 1048*333d2b36SAndroid Build Coastguard Worker `******************************\n'` 1049*333d2b36SAndroid Build Coastguard Worker 1050*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithArg("--error-message:compatibility:released ", msg) 1051*333d2b36SAndroid Build Coastguard Worker } 1052*333d2b36SAndroid Build Coastguard Worker 1053*333d2b36SAndroid Build Coastguard Worker if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") { 1054*333d2b36SAndroid Build Coastguard Worker // Pass the current API file into metalava so it can use it as the basis for determining how to 1055*333d2b36SAndroid Build Coastguard Worker // generate the output signature files (both api and removed). 1056*333d2b36SAndroid Build Coastguard Worker currentApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file)) 1057*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithInput("--use-same-format-as ", currentApiFile) 1058*333d2b36SAndroid Build Coastguard Worker } 1059*333d2b36SAndroid Build Coastguard Worker} 1060*333d2b36SAndroid Build Coastguard Worker 1061*333d2b36SAndroid Build Coastguard Worker// HIDDEN_DOCUMENTATION_ISSUES is the set of documentation related issues that should always be 1062*333d2b36SAndroid Build Coastguard Worker// hidden as they are very noisy and provide little value. 1063*333d2b36SAndroid Build Coastguard Workervar HIDDEN_DOCUMENTATION_ISSUES = []string{ 1064*333d2b36SAndroid Build Coastguard Worker "Deprecated", 1065*333d2b36SAndroid Build Coastguard Worker "IntDef", 1066*333d2b36SAndroid Build Coastguard Worker "Nullable", 1067*333d2b36SAndroid Build Coastguard Worker} 1068*333d2b36SAndroid Build Coastguard Worker 1069*333d2b36SAndroid Build Coastguard Workerfunc treatDocumentationIssuesAsWarningErrorWhenNew(cmd *android.RuleBuilderCommand) { 1070*333d2b36SAndroid Build Coastguard Worker // Treat documentation issues as warnings, but error when new. 1071*333d2b36SAndroid Build Coastguard Worker cmd.Flag("--error-when-new-category").Flag("Documentation") 1072*333d2b36SAndroid Build Coastguard Worker 1073*333d2b36SAndroid Build Coastguard Worker // Hide some documentation issues that generated a lot of noise for little benefit. 1074*333d2b36SAndroid Build Coastguard Worker cmd.FlagForEachArg("--hide ", HIDDEN_DOCUMENTATION_ISSUES) 1075*333d2b36SAndroid Build Coastguard Worker} 1076*333d2b36SAndroid Build Coastguard Worker 1077*333d2b36SAndroid Build Coastguard Worker// Sandbox rule for generating exportable stubs and other artifacts 1078*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) exportableStubCmd(ctx android.ModuleContext, params stubsCommandConfigParams) { 1079*333d2b36SAndroid Build Coastguard Worker optionalCmdParams := stubsCommandParams{ 1080*333d2b36SAndroid Build Coastguard Worker stubConfig: params, 1081*333d2b36SAndroid Build Coastguard Worker } 1082*333d2b36SAndroid Build Coastguard Worker 1083*333d2b36SAndroid Build Coastguard Worker if params.generateStubs { 1084*333d2b36SAndroid Build Coastguard Worker d.Javadoc.exportableStubsSrcJar = android.PathForModuleOut(ctx, params.stubsType.String(), ctx.ModuleName()+"-"+"stubs.srcjar") 1085*333d2b36SAndroid Build Coastguard Worker optionalCmdParams.stubsSrcJar = d.Javadoc.exportableStubsSrcJar 1086*333d2b36SAndroid Build Coastguard Worker } 1087*333d2b36SAndroid Build Coastguard Worker 1088*333d2b36SAndroid Build Coastguard Worker if params.writeSdkValues { 1089*333d2b36SAndroid Build Coastguard Worker d.exportableArtifacts.metadataZip = android.PathForModuleOut(ctx, params.stubsType.String(), ctx.ModuleName()+"-metadata.zip") 1090*333d2b36SAndroid Build Coastguard Worker d.exportableArtifacts.metadataDir = android.PathForModuleOut(ctx, params.stubsType.String(), "metadata") 1091*333d2b36SAndroid Build Coastguard Worker optionalCmdParams.metadataZip = d.exportableArtifacts.metadataZip 1092*333d2b36SAndroid Build Coastguard Worker optionalCmdParams.metadataDir = d.exportableArtifacts.metadataDir 1093*333d2b36SAndroid Build Coastguard Worker } 1094*333d2b36SAndroid Build Coastguard Worker 1095*333d2b36SAndroid Build Coastguard Worker if Bool(d.properties.Annotations_enabled) { 1096*333d2b36SAndroid Build Coastguard Worker if params.validatingNullability { 1097*333d2b36SAndroid Build Coastguard Worker d.exportableArtifacts.nullabilityWarningsFile = android.PathForModuleOut(ctx, params.stubsType.String(), ctx.ModuleName()+"_nullability_warnings.txt") 1098*333d2b36SAndroid Build Coastguard Worker optionalCmdParams.nullabilityWarningsFile = d.exportableArtifacts.nullabilityWarningsFile 1099*333d2b36SAndroid Build Coastguard Worker } 1100*333d2b36SAndroid Build Coastguard Worker d.exportableArtifacts.annotationsZip = android.PathForModuleOut(ctx, params.stubsType.String(), ctx.ModuleName()+"_annotations.zip") 1101*333d2b36SAndroid Build Coastguard Worker optionalCmdParams.annotationsZip = d.exportableArtifacts.annotationsZip 1102*333d2b36SAndroid Build Coastguard Worker } 1103*333d2b36SAndroid Build Coastguard Worker if Bool(d.properties.Api_levels_annotations_enabled) { 1104*333d2b36SAndroid Build Coastguard Worker d.exportableArtifacts.apiVersionsXml = android.PathForModuleOut(ctx, params.stubsType.String(), "api-versions.xml") 1105*333d2b36SAndroid Build Coastguard Worker optionalCmdParams.apiVersionsXml = d.exportableArtifacts.apiVersionsXml 1106*333d2b36SAndroid Build Coastguard Worker } 1107*333d2b36SAndroid Build Coastguard Worker 1108*333d2b36SAndroid Build Coastguard Worker if params.checkApi || String(d.properties.Api_filename) != "" { 1109*333d2b36SAndroid Build Coastguard Worker filename := proptools.StringDefault(d.properties.Api_filename, ctx.ModuleName()+"_api.txt") 1110*333d2b36SAndroid Build Coastguard Worker d.exportableApiFile = android.PathForModuleOut(ctx, params.stubsType.String(), filename) 1111*333d2b36SAndroid Build Coastguard Worker } 1112*333d2b36SAndroid Build Coastguard Worker 1113*333d2b36SAndroid Build Coastguard Worker if params.checkApi || String(d.properties.Removed_api_filename) != "" { 1114*333d2b36SAndroid Build Coastguard Worker filename := proptools.StringDefault(d.properties.Removed_api_filename, ctx.ModuleName()+"_api.txt") 1115*333d2b36SAndroid Build Coastguard Worker d.exportableRemovedApiFile = android.PathForModuleOut(ctx, params.stubsType.String(), filename) 1116*333d2b36SAndroid Build Coastguard Worker } 1117*333d2b36SAndroid Build Coastguard Worker 1118*333d2b36SAndroid Build Coastguard Worker d.optionalStubCmd(ctx, optionalCmdParams) 1119*333d2b36SAndroid Build Coastguard Worker} 1120*333d2b36SAndroid Build Coastguard Worker 1121*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) optionalStubCmd(ctx android.ModuleContext, params stubsCommandParams) { 1122*333d2b36SAndroid Build Coastguard Worker 1123*333d2b36SAndroid Build Coastguard Worker params.srcJarDir = android.PathForModuleOut(ctx, params.stubConfig.stubsType.String(), "srcjars") 1124*333d2b36SAndroid Build Coastguard Worker rule := android.NewRuleBuilder(pctx, ctx) 1125*333d2b36SAndroid Build Coastguard Worker rule.Sbox(android.PathForModuleOut(ctx, params.stubConfig.stubsType.String()), 1126*333d2b36SAndroid Build Coastguard Worker android.PathForModuleOut(ctx, fmt.Sprintf("metalava_%s.sbox.textproto", params.stubConfig.stubsType.String()))). 1127*333d2b36SAndroid Build Coastguard Worker SandboxInputs() 1128*333d2b36SAndroid Build Coastguard Worker 1129*333d2b36SAndroid Build Coastguard Worker if params.stubConfig.generateStubs { 1130*333d2b36SAndroid Build Coastguard Worker params.stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, params.stubConfig.stubsType.String(), "stubsDir")) 1131*333d2b36SAndroid Build Coastguard Worker } 1132*333d2b36SAndroid Build Coastguard Worker 1133*333d2b36SAndroid Build Coastguard Worker cmd := d.commonMetalavaStubCmd(ctx, rule, params) 1134*333d2b36SAndroid Build Coastguard Worker 1135*333d2b36SAndroid Build Coastguard Worker generateRevertAnnotationArgs(ctx, cmd, params.stubConfig.stubsType, params.stubConfig.deps.aconfigProtoFiles) 1136*333d2b36SAndroid Build Coastguard Worker 1137*333d2b36SAndroid Build Coastguard Worker if params.stubConfig.doApiLint { 1138*333d2b36SAndroid Build Coastguard Worker // Pass the lint baseline file as an input to resolve the lint errors. 1139*333d2b36SAndroid Build Coastguard Worker // The exportable stubs generation does not update the lint baseline file. 1140*333d2b36SAndroid Build Coastguard Worker // Lint baseline file update is handled by the everything stubs 1141*333d2b36SAndroid Build Coastguard Worker baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file) 1142*333d2b36SAndroid Build Coastguard Worker if baselineFile.Valid() { 1143*333d2b36SAndroid Build Coastguard Worker cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path()) 1144*333d2b36SAndroid Build Coastguard Worker } 1145*333d2b36SAndroid Build Coastguard Worker } 1146*333d2b36SAndroid Build Coastguard Worker 1147*333d2b36SAndroid Build Coastguard Worker // Treat documentation issues as warnings, but error when new. 1148*333d2b36SAndroid Build Coastguard Worker treatDocumentationIssuesAsWarningErrorWhenNew(cmd) 1149*333d2b36SAndroid Build Coastguard Worker 1150*333d2b36SAndroid Build Coastguard Worker if params.stubConfig.generateStubs { 1151*333d2b36SAndroid Build Coastguard Worker rule.Command(). 1152*333d2b36SAndroid Build Coastguard Worker BuiltTool("soong_zip"). 1153*333d2b36SAndroid Build Coastguard Worker Flag("-write_if_changed"). 1154*333d2b36SAndroid Build Coastguard Worker Flag("-jar"). 1155*333d2b36SAndroid Build Coastguard Worker FlagWithOutput("-o ", params.stubsSrcJar). 1156*333d2b36SAndroid Build Coastguard Worker FlagWithArg("-C ", params.stubsDir.String()). 1157*333d2b36SAndroid Build Coastguard Worker FlagWithArg("-D ", params.stubsDir.String()) 1158*333d2b36SAndroid Build Coastguard Worker } 1159*333d2b36SAndroid Build Coastguard Worker 1160*333d2b36SAndroid Build Coastguard Worker if params.stubConfig.writeSdkValues { 1161*333d2b36SAndroid Build Coastguard Worker rule.Command(). 1162*333d2b36SAndroid Build Coastguard Worker BuiltTool("soong_zip"). 1163*333d2b36SAndroid Build Coastguard Worker Flag("-write_if_changed"). 1164*333d2b36SAndroid Build Coastguard Worker Flag("-d"). 1165*333d2b36SAndroid Build Coastguard Worker FlagWithOutput("-o ", params.metadataZip). 1166*333d2b36SAndroid Build Coastguard Worker FlagWithArg("-C ", params.metadataDir.String()). 1167*333d2b36SAndroid Build Coastguard Worker FlagWithArg("-D ", params.metadataDir.String()) 1168*333d2b36SAndroid Build Coastguard Worker } 1169*333d2b36SAndroid Build Coastguard Worker 1170*333d2b36SAndroid Build Coastguard Worker // TODO(b/183630617): rewrapper doesn't support restat rules 1171*333d2b36SAndroid Build Coastguard Worker if !metalavaUseRbe(ctx) { 1172*333d2b36SAndroid Build Coastguard Worker rule.Restat() 1173*333d2b36SAndroid Build Coastguard Worker } 1174*333d2b36SAndroid Build Coastguard Worker 1175*333d2b36SAndroid Build Coastguard Worker zipSyncCleanupCmd(rule, params.srcJarDir) 1176*333d2b36SAndroid Build Coastguard Worker 1177*333d2b36SAndroid Build Coastguard Worker rule.Build(fmt.Sprintf("metalava_%s", params.stubConfig.stubsType.String()), "metalava merged") 1178*333d2b36SAndroid Build Coastguard Worker} 1179*333d2b36SAndroid Build Coastguard Worker 1180*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) { 1181*333d2b36SAndroid Build Coastguard Worker deps := d.Javadoc.collectDeps(ctx) 1182*333d2b36SAndroid Build Coastguard Worker 1183*333d2b36SAndroid Build Coastguard Worker javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), android.SdkContext(d)) 1184*333d2b36SAndroid Build Coastguard Worker generateStubs := BoolDefault(d.properties.Generate_stubs, true) 1185*333d2b36SAndroid Build Coastguard Worker 1186*333d2b36SAndroid Build Coastguard Worker // Add options for the other optional tasks: API-lint and check-released. 1187*333d2b36SAndroid Build Coastguard Worker // We generate separate timestamp files for them. 1188*333d2b36SAndroid Build Coastguard Worker doApiLint := BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) 1189*333d2b36SAndroid Build Coastguard Worker doCheckReleased := apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") 1190*333d2b36SAndroid Build Coastguard Worker 1191*333d2b36SAndroid Build Coastguard Worker writeSdkValues := Bool(d.properties.Write_sdk_values) 1192*333d2b36SAndroid Build Coastguard Worker 1193*333d2b36SAndroid Build Coastguard Worker annotationsEnabled := Bool(d.properties.Annotations_enabled) 1194*333d2b36SAndroid Build Coastguard Worker 1195*333d2b36SAndroid Build Coastguard Worker migratingNullability := annotationsEnabled && String(d.properties.Previous_api) != "" 1196*333d2b36SAndroid Build Coastguard Worker validatingNullability := annotationsEnabled && (strings.Contains(String(d.Javadoc.properties.Args), "--validate-nullability-from-merged-stubs") || 1197*333d2b36SAndroid Build Coastguard Worker String(d.properties.Validate_nullability_from_list) != "") 1198*333d2b36SAndroid Build Coastguard Worker 1199*333d2b36SAndroid Build Coastguard Worker checkApi := apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") || 1200*333d2b36SAndroid Build Coastguard Worker apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") 1201*333d2b36SAndroid Build Coastguard Worker 1202*333d2b36SAndroid Build Coastguard Worker stubCmdParams := stubsCommandConfigParams{ 1203*333d2b36SAndroid Build Coastguard Worker javaVersion: javaVersion, 1204*333d2b36SAndroid Build Coastguard Worker deps: deps, 1205*333d2b36SAndroid Build Coastguard Worker checkApi: checkApi, 1206*333d2b36SAndroid Build Coastguard Worker generateStubs: generateStubs, 1207*333d2b36SAndroid Build Coastguard Worker doApiLint: doApiLint, 1208*333d2b36SAndroid Build Coastguard Worker doCheckReleased: doCheckReleased, 1209*333d2b36SAndroid Build Coastguard Worker writeSdkValues: writeSdkValues, 1210*333d2b36SAndroid Build Coastguard Worker migratingNullability: migratingNullability, 1211*333d2b36SAndroid Build Coastguard Worker validatingNullability: validatingNullability, 1212*333d2b36SAndroid Build Coastguard Worker } 1213*333d2b36SAndroid Build Coastguard Worker stubCmdParams.stubsType = Everything 1214*333d2b36SAndroid Build Coastguard Worker // Create default (i.e. "everything" stubs) rule for metalava 1215*333d2b36SAndroid Build Coastguard Worker d.everythingStubCmd(ctx, stubCmdParams) 1216*333d2b36SAndroid Build Coastguard Worker 1217*333d2b36SAndroid Build Coastguard Worker // The module generates "exportable" (and "runtime" eventually) stubs regardless of whether 1218*333d2b36SAndroid Build Coastguard Worker // aconfig_declarations property is defined or not. If the property is not defined, the module simply 1219*333d2b36SAndroid Build Coastguard Worker // strips all flagged apis to generate the "exportable" stubs 1220*333d2b36SAndroid Build Coastguard Worker stubCmdParams.stubsType = Exportable 1221*333d2b36SAndroid Build Coastguard Worker d.exportableStubCmd(ctx, stubCmdParams) 1222*333d2b36SAndroid Build Coastguard Worker 1223*333d2b36SAndroid Build Coastguard Worker if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") { 1224*333d2b36SAndroid Build Coastguard Worker 1225*333d2b36SAndroid Build Coastguard Worker if len(d.Javadoc.properties.Out) > 0 { 1226*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf("out", "out property may not be combined with check_api") 1227*333d2b36SAndroid Build Coastguard Worker } 1228*333d2b36SAndroid Build Coastguard Worker 1229*333d2b36SAndroid Build Coastguard Worker apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file)) 1230*333d2b36SAndroid Build Coastguard Worker removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file)) 1231*333d2b36SAndroid Build Coastguard Worker baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file) 1232*333d2b36SAndroid Build Coastguard Worker 1233*333d2b36SAndroid Build Coastguard Worker if baselineFile.Valid() { 1234*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf("baseline_file", "current API check can't have a baseline file. (module %s)", ctx.ModuleName()) 1235*333d2b36SAndroid Build Coastguard Worker } 1236*333d2b36SAndroid Build Coastguard Worker 1237*333d2b36SAndroid Build Coastguard Worker d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, Everything.String(), "check_current_api.timestamp") 1238*333d2b36SAndroid Build Coastguard Worker 1239*333d2b36SAndroid Build Coastguard Worker rule := android.NewRuleBuilder(pctx, ctx) 1240*333d2b36SAndroid Build Coastguard Worker 1241*333d2b36SAndroid Build Coastguard Worker // Diff command line. 1242*333d2b36SAndroid Build Coastguard Worker // -F matches the closest "opening" line, such as "package android {" 1243*333d2b36SAndroid Build Coastguard Worker // and " public class Intent {". 1244*333d2b36SAndroid Build Coastguard Worker diff := `diff -u -F '{ *$'` 1245*333d2b36SAndroid Build Coastguard Worker 1246*333d2b36SAndroid Build Coastguard Worker rule.Command().Text("( true") 1247*333d2b36SAndroid Build Coastguard Worker rule.Command(). 1248*333d2b36SAndroid Build Coastguard Worker Text(diff). 1249*333d2b36SAndroid Build Coastguard Worker Input(apiFile).Input(d.apiFile) 1250*333d2b36SAndroid Build Coastguard Worker 1251*333d2b36SAndroid Build Coastguard Worker rule.Command(). 1252*333d2b36SAndroid Build Coastguard Worker Text(diff). 1253*333d2b36SAndroid Build Coastguard Worker Input(removedApiFile).Input(d.removedApiFile) 1254*333d2b36SAndroid Build Coastguard Worker 1255*333d2b36SAndroid Build Coastguard Worker msg := fmt.Sprintf(`\n******************************\n`+ 1256*333d2b36SAndroid Build Coastguard Worker `You have tried to change the API from what has been previously approved.\n\n`+ 1257*333d2b36SAndroid Build Coastguard Worker `To make these errors go away, you have two choices:\n`+ 1258*333d2b36SAndroid Build Coastguard Worker ` 1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)\n`+ 1259*333d2b36SAndroid Build Coastguard Worker ` to the new methods, etc. shown in the above diff.\n\n`+ 1260*333d2b36SAndroid Build Coastguard Worker ` 2. You can update current.txt and/or removed.txt by executing the following command:\n`+ 1261*333d2b36SAndroid Build Coastguard Worker ` m %s-update-current-api\n\n`+ 1262*333d2b36SAndroid Build Coastguard Worker ` To submit the revised current.txt to the main Android repository,\n`+ 1263*333d2b36SAndroid Build Coastguard Worker ` you will need approval.\n`+ 1264*333d2b36SAndroid Build Coastguard Worker `If your build failed due to stub validation, you can resolve the errors with\n`+ 1265*333d2b36SAndroid Build Coastguard Worker `either of the two choices above and try re-building the target.\n`+ 1266*333d2b36SAndroid Build Coastguard Worker `If the mismatch between the stubs and the current.txt is intended,\n`+ 1267*333d2b36SAndroid Build Coastguard Worker `you can try re-building the target by executing the following command:\n`+ 1268*333d2b36SAndroid Build Coastguard Worker `m DISABLE_STUB_VALIDATION=true <your build target>.\n`+ 1269*333d2b36SAndroid Build Coastguard Worker `Note that DISABLE_STUB_VALIDATION=true does not bypass checkapi.\n`+ 1270*333d2b36SAndroid Build Coastguard Worker `******************************\n`, ctx.ModuleName()) 1271*333d2b36SAndroid Build Coastguard Worker 1272*333d2b36SAndroid Build Coastguard Worker rule.Command(). 1273*333d2b36SAndroid Build Coastguard Worker Text("touch").Output(d.checkCurrentApiTimestamp). 1274*333d2b36SAndroid Build Coastguard Worker Text(") || ("). 1275*333d2b36SAndroid Build Coastguard Worker Text("echo").Flag("-e").Flag(`"` + msg + `"`). 1276*333d2b36SAndroid Build Coastguard Worker Text("; exit 38"). 1277*333d2b36SAndroid Build Coastguard Worker Text(")") 1278*333d2b36SAndroid Build Coastguard Worker 1279*333d2b36SAndroid Build Coastguard Worker rule.Build("metalavaCurrentApiCheck", "check current API") 1280*333d2b36SAndroid Build Coastguard Worker 1281*333d2b36SAndroid Build Coastguard Worker d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, Everything.String(), "update_current_api.timestamp") 1282*333d2b36SAndroid Build Coastguard Worker 1283*333d2b36SAndroid Build Coastguard Worker // update API rule 1284*333d2b36SAndroid Build Coastguard Worker rule = android.NewRuleBuilder(pctx, ctx) 1285*333d2b36SAndroid Build Coastguard Worker 1286*333d2b36SAndroid Build Coastguard Worker rule.Command().Text("( true") 1287*333d2b36SAndroid Build Coastguard Worker 1288*333d2b36SAndroid Build Coastguard Worker rule.Command(). 1289*333d2b36SAndroid Build Coastguard Worker Text("cp").Flag("-f"). 1290*333d2b36SAndroid Build Coastguard Worker Input(d.apiFile).Flag(apiFile.String()) 1291*333d2b36SAndroid Build Coastguard Worker 1292*333d2b36SAndroid Build Coastguard Worker rule.Command(). 1293*333d2b36SAndroid Build Coastguard Worker Text("cp").Flag("-f"). 1294*333d2b36SAndroid Build Coastguard Worker Input(d.removedApiFile).Flag(removedApiFile.String()) 1295*333d2b36SAndroid Build Coastguard Worker 1296*333d2b36SAndroid Build Coastguard Worker msg = "failed to update public API" 1297*333d2b36SAndroid Build Coastguard Worker 1298*333d2b36SAndroid Build Coastguard Worker rule.Command(). 1299*333d2b36SAndroid Build Coastguard Worker Text("touch").Output(d.updateCurrentApiTimestamp). 1300*333d2b36SAndroid Build Coastguard Worker Text(") || ("). 1301*333d2b36SAndroid Build Coastguard Worker Text("echo").Flag("-e").Flag(`"` + msg + `"`). 1302*333d2b36SAndroid Build Coastguard Worker Text("; exit 38"). 1303*333d2b36SAndroid Build Coastguard Worker Text(")") 1304*333d2b36SAndroid Build Coastguard Worker 1305*333d2b36SAndroid Build Coastguard Worker rule.Build("metalavaCurrentApiUpdate", "update current API") 1306*333d2b36SAndroid Build Coastguard Worker } 1307*333d2b36SAndroid Build Coastguard Worker 1308*333d2b36SAndroid Build Coastguard Worker if String(d.properties.Check_nullability_warnings) != "" { 1309*333d2b36SAndroid Build Coastguard Worker if d.everythingArtifacts.nullabilityWarningsFile == nil { 1310*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf("check_nullability_warnings", 1311*333d2b36SAndroid Build Coastguard Worker "Cannot specify check_nullability_warnings unless validating nullability") 1312*333d2b36SAndroid Build Coastguard Worker } 1313*333d2b36SAndroid Build Coastguard Worker 1314*333d2b36SAndroid Build Coastguard Worker checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings)) 1315*333d2b36SAndroid Build Coastguard Worker 1316*333d2b36SAndroid Build Coastguard Worker d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, Everything.String(), "check_nullability_warnings.timestamp") 1317*333d2b36SAndroid Build Coastguard Worker 1318*333d2b36SAndroid Build Coastguard Worker msg := fmt.Sprintf(`\n******************************\n`+ 1319*333d2b36SAndroid Build Coastguard Worker `The warnings encountered during nullability annotation validation did\n`+ 1320*333d2b36SAndroid Build Coastguard Worker `not match the checked in file of expected warnings. The diffs are shown\n`+ 1321*333d2b36SAndroid Build Coastguard Worker `above. You have two options:\n`+ 1322*333d2b36SAndroid Build Coastguard Worker ` 1. Resolve the differences by editing the nullability annotations.\n`+ 1323*333d2b36SAndroid Build Coastguard Worker ` 2. Update the file of expected warnings by running:\n`+ 1324*333d2b36SAndroid Build Coastguard Worker ` cp %s %s\n`+ 1325*333d2b36SAndroid Build Coastguard Worker ` and submitting the updated file as part of your change.`, 1326*333d2b36SAndroid Build Coastguard Worker d.everythingArtifacts.nullabilityWarningsFile, checkNullabilityWarnings) 1327*333d2b36SAndroid Build Coastguard Worker 1328*333d2b36SAndroid Build Coastguard Worker rule := android.NewRuleBuilder(pctx, ctx) 1329*333d2b36SAndroid Build Coastguard Worker 1330*333d2b36SAndroid Build Coastguard Worker rule.Command(). 1331*333d2b36SAndroid Build Coastguard Worker Text("("). 1332*333d2b36SAndroid Build Coastguard Worker Text("diff").Input(checkNullabilityWarnings).Input(d.everythingArtifacts.nullabilityWarningsFile). 1333*333d2b36SAndroid Build Coastguard Worker Text("&&"). 1334*333d2b36SAndroid Build Coastguard Worker Text("touch").Output(d.checkNullabilityWarningsTimestamp). 1335*333d2b36SAndroid Build Coastguard Worker Text(") || ("). 1336*333d2b36SAndroid Build Coastguard Worker Text("echo").Flag("-e").Flag(`"` + msg + `"`). 1337*333d2b36SAndroid Build Coastguard Worker Text("; exit 38"). 1338*333d2b36SAndroid Build Coastguard Worker Text(")") 1339*333d2b36SAndroid Build Coastguard Worker 1340*333d2b36SAndroid Build Coastguard Worker rule.Build("nullabilityWarningsCheck", "nullability warnings check") 1341*333d2b36SAndroid Build Coastguard Worker } 1342*333d2b36SAndroid Build Coastguard Worker 1343*333d2b36SAndroid Build Coastguard Worker d.setOutputFiles(ctx) 1344*333d2b36SAndroid Build Coastguard Worker} 1345*333d2b36SAndroid Build Coastguard Worker 1346*333d2b36SAndroid Build Coastguard Worker// This method sets the outputFiles property, which is used to set the 1347*333d2b36SAndroid Build Coastguard Worker// OutputFilesProvider later. 1348*333d2b36SAndroid Build Coastguard Worker// Droidstubs' tag supports specifying with the stubs type. 1349*333d2b36SAndroid Build Coastguard Worker// While supporting the pre-existing tags, it also supports tags with 1350*333d2b36SAndroid Build Coastguard Worker// the stubs type prefix. Some examples are shown below: 1351*333d2b36SAndroid Build Coastguard Worker// {.annotations.zip} - pre-existing behavior. Returns the path to the 1352*333d2b36SAndroid Build Coastguard Worker// annotation zip. 1353*333d2b36SAndroid Build Coastguard Worker// {.exportable} - Returns the path to the exportable stubs src jar. 1354*333d2b36SAndroid Build Coastguard Worker// {.exportable.annotations.zip} - Returns the path to the exportable 1355*333d2b36SAndroid Build Coastguard Worker// annotations zip file. 1356*333d2b36SAndroid Build Coastguard Worker// {.runtime.api_versions.xml} - Runtime stubs does not generate api versions 1357*333d2b36SAndroid Build Coastguard Worker// xml file. For unsupported combinations, the default everything output file 1358*333d2b36SAndroid Build Coastguard Worker// is returned. 1359*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) setOutputFiles(ctx android.ModuleContext) { 1360*333d2b36SAndroid Build Coastguard Worker tagToOutputFileFunc := map[string]func(StubsType) (android.Path, error){ 1361*333d2b36SAndroid Build Coastguard Worker "": d.StubsSrcJar, 1362*333d2b36SAndroid Build Coastguard Worker ".docs.zip": d.DocZip, 1363*333d2b36SAndroid Build Coastguard Worker ".api.txt": d.ApiFilePath, 1364*333d2b36SAndroid Build Coastguard Worker android.DefaultDistTag: d.ApiFilePath, 1365*333d2b36SAndroid Build Coastguard Worker ".removed-api.txt": d.RemovedApiFilePath, 1366*333d2b36SAndroid Build Coastguard Worker ".annotations.zip": d.AnnotationsZip, 1367*333d2b36SAndroid Build Coastguard Worker ".api_versions.xml": d.ApiVersionsXmlFilePath, 1368*333d2b36SAndroid Build Coastguard Worker } 1369*333d2b36SAndroid Build Coastguard Worker stubsTypeToPrefix := map[StubsType]string{ 1370*333d2b36SAndroid Build Coastguard Worker Everything: "", 1371*333d2b36SAndroid Build Coastguard Worker Exportable: ".exportable", 1372*333d2b36SAndroid Build Coastguard Worker } 1373*333d2b36SAndroid Build Coastguard Worker for _, tag := range android.SortedKeys(tagToOutputFileFunc) { 1374*333d2b36SAndroid Build Coastguard Worker for _, stubType := range android.SortedKeys(stubsTypeToPrefix) { 1375*333d2b36SAndroid Build Coastguard Worker tagWithPrefix := stubsTypeToPrefix[stubType] + tag 1376*333d2b36SAndroid Build Coastguard Worker outputFile, err := tagToOutputFileFunc[tag](stubType) 1377*333d2b36SAndroid Build Coastguard Worker if err == nil && outputFile != nil { 1378*333d2b36SAndroid Build Coastguard Worker ctx.SetOutputFiles(android.Paths{outputFile}, tagWithPrefix) 1379*333d2b36SAndroid Build Coastguard Worker } 1380*333d2b36SAndroid Build Coastguard Worker } 1381*333d2b36SAndroid Build Coastguard Worker } 1382*333d2b36SAndroid Build Coastguard Worker} 1383*333d2b36SAndroid Build Coastguard Worker 1384*333d2b36SAndroid Build Coastguard Workerfunc (d *Droidstubs) createApiContribution(ctx android.DefaultableHookContext) { 1385*333d2b36SAndroid Build Coastguard Worker api_file := d.properties.Check_api.Current.Api_file 1386*333d2b36SAndroid Build Coastguard Worker api_surface := d.properties.Api_surface 1387*333d2b36SAndroid Build Coastguard Worker 1388*333d2b36SAndroid Build Coastguard Worker props := struct { 1389*333d2b36SAndroid Build Coastguard Worker Name *string 1390*333d2b36SAndroid Build Coastguard Worker Api_surface *string 1391*333d2b36SAndroid Build Coastguard Worker Api_file *string 1392*333d2b36SAndroid Build Coastguard Worker Visibility []string 1393*333d2b36SAndroid Build Coastguard Worker }{} 1394*333d2b36SAndroid Build Coastguard Worker 1395*333d2b36SAndroid Build Coastguard Worker props.Name = proptools.StringPtr(d.Name() + ".api.contribution") 1396*333d2b36SAndroid Build Coastguard Worker props.Api_surface = api_surface 1397*333d2b36SAndroid Build Coastguard Worker props.Api_file = api_file 1398*333d2b36SAndroid Build Coastguard Worker props.Visibility = []string{"//visibility:override", "//visibility:public"} 1399*333d2b36SAndroid Build Coastguard Worker 1400*333d2b36SAndroid Build Coastguard Worker ctx.CreateModule(ApiContributionFactory, &props) 1401*333d2b36SAndroid Build Coastguard Worker} 1402*333d2b36SAndroid Build Coastguard Worker 1403*333d2b36SAndroid Build Coastguard Worker// TODO (b/262014796): Export the API contributions of CorePlatformApi 1404*333d2b36SAndroid Build Coastguard Worker// A map to populate the api surface of a droidstub from a substring appearing in its name 1405*333d2b36SAndroid Build Coastguard Worker// This map assumes that droidstubs (either checked-in or created by java_sdk_library) 1406*333d2b36SAndroid Build Coastguard Worker// use a strict naming convention 1407*333d2b36SAndroid Build Coastguard Workervar ( 1408*333d2b36SAndroid Build Coastguard Worker droidstubsModuleNamingToSdkKind = map[string]android.SdkKind{ 1409*333d2b36SAndroid Build Coastguard Worker // public is commented out since the core libraries use public in their java_sdk_library names 1410*333d2b36SAndroid Build Coastguard Worker "intracore": android.SdkIntraCore, 1411*333d2b36SAndroid Build Coastguard Worker "intra.core": android.SdkIntraCore, 1412*333d2b36SAndroid Build Coastguard Worker "system_server": android.SdkSystemServer, 1413*333d2b36SAndroid Build Coastguard Worker "system-server": android.SdkSystemServer, 1414*333d2b36SAndroid Build Coastguard Worker "system": android.SdkSystem, 1415*333d2b36SAndroid Build Coastguard Worker "module_lib": android.SdkModule, 1416*333d2b36SAndroid Build Coastguard Worker "module-lib": android.SdkModule, 1417*333d2b36SAndroid Build Coastguard Worker "platform.api": android.SdkCorePlatform, 1418*333d2b36SAndroid Build Coastguard Worker "test": android.SdkTest, 1419*333d2b36SAndroid Build Coastguard Worker "toolchain": android.SdkToolchain, 1420*333d2b36SAndroid Build Coastguard Worker } 1421*333d2b36SAndroid Build Coastguard Worker) 1422*333d2b36SAndroid Build Coastguard Worker 1423*333d2b36SAndroid Build Coastguard Workerfunc StubsDefaultsFactory() android.Module { 1424*333d2b36SAndroid Build Coastguard Worker module := &DocDefaults{} 1425*333d2b36SAndroid Build Coastguard Worker 1426*333d2b36SAndroid Build Coastguard Worker module.AddProperties( 1427*333d2b36SAndroid Build Coastguard Worker &JavadocProperties{}, 1428*333d2b36SAndroid Build Coastguard Worker &DroidstubsProperties{}, 1429*333d2b36SAndroid Build Coastguard Worker ) 1430*333d2b36SAndroid Build Coastguard Worker 1431*333d2b36SAndroid Build Coastguard Worker android.InitDefaultsModule(module) 1432*333d2b36SAndroid Build Coastguard Worker 1433*333d2b36SAndroid Build Coastguard Worker return module 1434*333d2b36SAndroid Build Coastguard Worker} 1435*333d2b36SAndroid Build Coastguard Worker 1436*333d2b36SAndroid Build Coastguard Workervar _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil) 1437*333d2b36SAndroid Build Coastguard Worker 1438*333d2b36SAndroid Build Coastguard Workertype PrebuiltStubsSourcesProperties struct { 1439*333d2b36SAndroid Build Coastguard Worker Srcs []string `android:"path"` 1440*333d2b36SAndroid Build Coastguard Worker 1441*333d2b36SAndroid Build Coastguard Worker // Name of the source soong module that gets shadowed by this prebuilt 1442*333d2b36SAndroid Build Coastguard Worker // If unspecified, follows the naming convention that the source module of 1443*333d2b36SAndroid Build Coastguard Worker // the prebuilt is Name() without "prebuilt_" prefix 1444*333d2b36SAndroid Build Coastguard Worker Source_module_name *string 1445*333d2b36SAndroid Build Coastguard Worker 1446*333d2b36SAndroid Build Coastguard Worker // Non-nil if this prebuilt stub srcs module was dynamically created by a java_sdk_library_import 1447*333d2b36SAndroid Build Coastguard Worker // The name is the undecorated name of the java_sdk_library as it appears in the blueprint file 1448*333d2b36SAndroid Build Coastguard Worker // (without any prebuilt_ prefix) 1449*333d2b36SAndroid Build Coastguard Worker Created_by_java_sdk_library_name *string `blueprint:"mutated"` 1450*333d2b36SAndroid Build Coastguard Worker} 1451*333d2b36SAndroid Build Coastguard Worker 1452*333d2b36SAndroid Build Coastguard Workerfunc (j *PrebuiltStubsSources) BaseModuleName() string { 1453*333d2b36SAndroid Build Coastguard Worker return proptools.StringDefault(j.properties.Source_module_name, j.ModuleBase.Name()) 1454*333d2b36SAndroid Build Coastguard Worker} 1455*333d2b36SAndroid Build Coastguard Worker 1456*333d2b36SAndroid Build Coastguard Workerfunc (j *PrebuiltStubsSources) CreatedByJavaSdkLibraryName() *string { 1457*333d2b36SAndroid Build Coastguard Worker return j.properties.Created_by_java_sdk_library_name 1458*333d2b36SAndroid Build Coastguard Worker} 1459*333d2b36SAndroid Build Coastguard Worker 1460*333d2b36SAndroid Build Coastguard Workertype PrebuiltStubsSources struct { 1461*333d2b36SAndroid Build Coastguard Worker android.ModuleBase 1462*333d2b36SAndroid Build Coastguard Worker android.DefaultableModuleBase 1463*333d2b36SAndroid Build Coastguard Worker embeddableInModuleAndImport 1464*333d2b36SAndroid Build Coastguard Worker 1465*333d2b36SAndroid Build Coastguard Worker prebuilt android.Prebuilt 1466*333d2b36SAndroid Build Coastguard Worker 1467*333d2b36SAndroid Build Coastguard Worker properties PrebuiltStubsSourcesProperties 1468*333d2b36SAndroid Build Coastguard Worker 1469*333d2b36SAndroid Build Coastguard Worker stubsSrcJar android.Path 1470*333d2b36SAndroid Build Coastguard Worker} 1471*333d2b36SAndroid Build Coastguard Worker 1472*333d2b36SAndroid Build Coastguard Workerfunc (d *PrebuiltStubsSources) StubsSrcJar(_ StubsType) (android.Path, error) { 1473*333d2b36SAndroid Build Coastguard Worker return d.stubsSrcJar, nil 1474*333d2b36SAndroid Build Coastguard Worker} 1475*333d2b36SAndroid Build Coastguard Worker 1476*333d2b36SAndroid Build Coastguard Workerfunc (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) { 1477*333d2b36SAndroid Build Coastguard Worker if len(p.properties.Srcs) != 1 { 1478*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf("srcs", "must only specify one directory path or srcjar, contains %d paths", len(p.properties.Srcs)) 1479*333d2b36SAndroid Build Coastguard Worker return 1480*333d2b36SAndroid Build Coastguard Worker } 1481*333d2b36SAndroid Build Coastguard Worker 1482*333d2b36SAndroid Build Coastguard Worker src := p.properties.Srcs[0] 1483*333d2b36SAndroid Build Coastguard Worker if filepath.Ext(src) == ".srcjar" { 1484*333d2b36SAndroid Build Coastguard Worker // This is a srcjar. We can use it directly. 1485*333d2b36SAndroid Build Coastguard Worker p.stubsSrcJar = android.PathForModuleSrc(ctx, src) 1486*333d2b36SAndroid Build Coastguard Worker } else { 1487*333d2b36SAndroid Build Coastguard Worker outPath := android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar") 1488*333d2b36SAndroid Build Coastguard Worker 1489*333d2b36SAndroid Build Coastguard Worker // This is a directory. Glob the contents just in case the directory does not exist. 1490*333d2b36SAndroid Build Coastguard Worker srcGlob := src + "/**/*" 1491*333d2b36SAndroid Build Coastguard Worker srcPaths := android.PathsForModuleSrc(ctx, []string{srcGlob}) 1492*333d2b36SAndroid Build Coastguard Worker 1493*333d2b36SAndroid Build Coastguard Worker // Although PathForModuleSrc can return nil if either the path doesn't exist or 1494*333d2b36SAndroid Build Coastguard Worker // the path components are invalid it won't in this case because no components 1495*333d2b36SAndroid Build Coastguard Worker // are specified and the module directory must exist in order to get this far. 1496*333d2b36SAndroid Build Coastguard Worker srcDir := android.PathForModuleSrc(ctx).(android.SourcePath).Join(ctx, src) 1497*333d2b36SAndroid Build Coastguard Worker 1498*333d2b36SAndroid Build Coastguard Worker rule := android.NewRuleBuilder(pctx, ctx) 1499*333d2b36SAndroid Build Coastguard Worker rule.Command(). 1500*333d2b36SAndroid Build Coastguard Worker BuiltTool("soong_zip"). 1501*333d2b36SAndroid Build Coastguard Worker Flag("-write_if_changed"). 1502*333d2b36SAndroid Build Coastguard Worker Flag("-jar"). 1503*333d2b36SAndroid Build Coastguard Worker FlagWithOutput("-o ", outPath). 1504*333d2b36SAndroid Build Coastguard Worker FlagWithArg("-C ", srcDir.String()). 1505*333d2b36SAndroid Build Coastguard Worker FlagWithRspFileInputList("-r ", outPath.ReplaceExtension(ctx, "rsp"), srcPaths) 1506*333d2b36SAndroid Build Coastguard Worker rule.Restat() 1507*333d2b36SAndroid Build Coastguard Worker rule.Build("zip src", "Create srcjar from prebuilt source") 1508*333d2b36SAndroid Build Coastguard Worker p.stubsSrcJar = outPath 1509*333d2b36SAndroid Build Coastguard Worker } 1510*333d2b36SAndroid Build Coastguard Worker 1511*333d2b36SAndroid Build Coastguard Worker ctx.SetOutputFiles(android.Paths{p.stubsSrcJar}, "") 1512*333d2b36SAndroid Build Coastguard Worker // prebuilt droidstubs does not output "exportable" stubs. 1513*333d2b36SAndroid Build Coastguard Worker // Output the "everything" stubs srcjar file if the tag is ".exportable". 1514*333d2b36SAndroid Build Coastguard Worker ctx.SetOutputFiles(android.Paths{p.stubsSrcJar}, ".exportable") 1515*333d2b36SAndroid Build Coastguard Worker} 1516*333d2b36SAndroid Build Coastguard Worker 1517*333d2b36SAndroid Build Coastguard Workerfunc (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt { 1518*333d2b36SAndroid Build Coastguard Worker return &p.prebuilt 1519*333d2b36SAndroid Build Coastguard Worker} 1520*333d2b36SAndroid Build Coastguard Worker 1521*333d2b36SAndroid Build Coastguard Workerfunc (p *PrebuiltStubsSources) Name() string { 1522*333d2b36SAndroid Build Coastguard Worker return p.prebuilt.Name(p.ModuleBase.Name()) 1523*333d2b36SAndroid Build Coastguard Worker} 1524*333d2b36SAndroid Build Coastguard Worker 1525*333d2b36SAndroid Build Coastguard Worker// prebuilt_stubs_sources imports a set of java source files as if they were 1526*333d2b36SAndroid Build Coastguard Worker// generated by droidstubs. 1527*333d2b36SAndroid Build Coastguard Worker// 1528*333d2b36SAndroid Build Coastguard Worker// By default, a prebuilt_stubs_sources has a single variant that expects a 1529*333d2b36SAndroid Build Coastguard Worker// set of `.java` files generated by droidstubs. 1530*333d2b36SAndroid Build Coastguard Worker// 1531*333d2b36SAndroid Build Coastguard Worker// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one 1532*333d2b36SAndroid Build Coastguard Worker// for host modules. 1533*333d2b36SAndroid Build Coastguard Worker// 1534*333d2b36SAndroid Build Coastguard Worker// Intended only for use by sdk snapshots. 1535*333d2b36SAndroid Build Coastguard Workerfunc PrebuiltStubsSourcesFactory() android.Module { 1536*333d2b36SAndroid Build Coastguard Worker module := &PrebuiltStubsSources{} 1537*333d2b36SAndroid Build Coastguard Worker 1538*333d2b36SAndroid Build Coastguard Worker module.AddProperties(&module.properties) 1539*333d2b36SAndroid Build Coastguard Worker module.initModuleAndImport(module) 1540*333d2b36SAndroid Build Coastguard Worker 1541*333d2b36SAndroid Build Coastguard Worker android.InitPrebuiltModule(module, &module.properties.Srcs) 1542*333d2b36SAndroid Build Coastguard Worker InitDroiddocModule(module, android.HostAndDeviceSupported) 1543*333d2b36SAndroid Build Coastguard Worker return module 1544*333d2b36SAndroid Build Coastguard Worker} 1545