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