blob: da8489ac12dca8831277969cd229cc7d1ac00348 [file] [log] [blame]
Nan Zhang581fd212018-01-10 16:06:12 -08001// Copyright 2018 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
15package java
16
17import (
Nan Zhang581fd212018-01-10 16:06:12 -080018 "fmt"
Nan Zhangb2b33de2018-02-23 11:18:47 -080019 "path/filepath"
Nan Zhang581fd212018-01-10 16:06:12 -080020 "strings"
21
Jeongik Cha6bd33c12019-06-25 16:26:18 +090022 "github.com/google/blueprint/proptools"
Nan Zhang581fd212018-01-10 16:06:12 -080023
Colin Crossab054432019-07-15 16:13:59 -070024 "android/soong/android"
25 "android/soong/java/config"
Ramy Medhat427683c2020-04-30 03:08:37 -040026 "android/soong/remoteexec"
Nan Zhang581fd212018-01-10 16:06:12 -080027)
28
29func init() {
Paul Duffin884363e2019-12-19 10:21:09 +000030 RegisterDocsBuildComponents(android.InitRegistrationContext)
31 RegisterStubsBuildComponents(android.InitRegistrationContext)
Nan Zhang581fd212018-01-10 16:06:12 -080032}
33
Paul Duffin884363e2019-12-19 10:21:09 +000034func RegisterDocsBuildComponents(ctx android.RegistrationContext) {
35 ctx.RegisterModuleType("doc_defaults", DocDefaultsFactory)
36
37 ctx.RegisterModuleType("droiddoc", DroiddocFactory)
38 ctx.RegisterModuleType("droiddoc_host", DroiddocHostFactory)
39 ctx.RegisterModuleType("droiddoc_exported_dir", ExportedDroiddocDirFactory)
40 ctx.RegisterModuleType("javadoc", JavadocFactory)
41 ctx.RegisterModuleType("javadoc_host", JavadocHostFactory)
42}
43
44func RegisterStubsBuildComponents(ctx android.RegistrationContext) {
45 ctx.RegisterModuleType("stubs_defaults", StubsDefaultsFactory)
46
47 ctx.RegisterModuleType("droidstubs", DroidstubsFactory)
48 ctx.RegisterModuleType("droidstubs_host", DroidstubsHostFactory)
49
50 ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory)
51}
52
Colin Crossa1ce2a02018-06-20 15:19:39 -070053var (
54 srcsLibTag = dependencyTag{name: "sources from javalib"}
55)
56
Nan Zhang581fd212018-01-10 16:06:12 -080057type JavadocProperties struct {
58 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
59 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080060 Srcs []string `android:"path,arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080061
Nan Zhang581fd212018-01-10 16:06:12 -080062 // list of source files that should not be used to build the Java module.
63 // This is most useful in the arch/multilib variants to remove non-common files
64 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -080065 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080066
Jiyong Parkc6ddccf2019-09-13 20:56:14 +090067 // list of package names that should actually be used. If this property is left unspecified,
68 // all the sources from the srcs property is used.
69 Filter_packages []string
70
Nan Zhangb2b33de2018-02-23 11:18:47 -080071 // list of java libraries that will be in the classpath.
Nan Zhang581fd212018-01-10 16:06:12 -080072 Libs []string `android:"arch_variant"`
73
74 // If set to false, don't allow this module(-docs.zip) to be exported. Defaults to true.
Nan Zhangb2b33de2018-02-23 11:18:47 -080075 Installable *bool
Nan Zhang581fd212018-01-10 16:06:12 -080076
Paul Duffine25c6442019-10-11 13:50:28 +010077 // if not blank, set to the version of the sdk to compile against.
78 // Defaults to compiling against the current platform.
Nan Zhang581fd212018-01-10 16:06:12 -080079 Sdk_version *string `android:"arch_variant"`
Jiyong Park1e440682018-05-23 18:42:04 +090080
Paul Duffine25c6442019-10-11 13:50:28 +010081 // When targeting 1.9 and above, override the modules to use with --system,
82 // otherwise provides defaults libraries to add to the bootclasspath.
83 // Defaults to "none"
84 System_modules *string
85
Jiyong Park1e440682018-05-23 18:42:04 +090086 Aidl struct {
87 // Top level directories to pass to aidl tool
88 Include_dirs []string
89
90 // Directories rooted at the Android.bp file to pass to aidl tool
91 Local_include_dirs []string
92 }
Nan Zhang357466b2018-04-17 17:38:36 -070093
94 // If not blank, set the java version passed to javadoc as -source
95 Java_version *string
Nan Zhang1598a9e2018-09-04 17:14:32 -070096
97 // local files that are used within user customized droiddoc options.
Colin Cross27b922f2019-03-04 22:35:41 -080098 Arg_files []string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -070099
Liz Kammer585cac22020-07-06 09:12:57 -0700100 // user customized droiddoc args. Deprecated, use flags instead.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700101 // Available variables for substitution:
102 //
103 // $(location <label>): the path to the arg_files with name <label>
Colin Crosse4a05842019-05-28 10:17:14 -0700104 // $$: a literal $
Nan Zhang1598a9e2018-09-04 17:14:32 -0700105 Args *string
106
Liz Kammer585cac22020-07-06 09:12:57 -0700107 // user customized droiddoc args. Not compatible with property args.
108 // Available variables for substitution:
109 //
110 // $(location <label>): the path to the arg_files with name <label>
111 // $$: a literal $
112 Flags []string
113
Nan Zhang1598a9e2018-09-04 17:14:32 -0700114 // names of the output files used in args that will be generated
115 Out []string
Ramy Medhat2f99eec2020-06-13 17:38:27 -0400116
117 // If set, metalava is sandboxed to only read files explicitly specified on the command
118 // line. Defaults to false.
119 Sandbox *bool
Nan Zhang581fd212018-01-10 16:06:12 -0800120}
121
Nan Zhang61819ce2018-05-04 18:49:16 -0700122type ApiToCheck struct {
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900123 // path to the API txt file that the new API extracted from source code is checked
124 // against. The path can be local to the module or from other module (via :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800125 Api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700126
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900127 // path to the API txt file that the new @removed API extractd from source code is
128 // checked against. The path can be local to the module or from other module (via
129 // :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800130 Removed_api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700131
Adrian Roos14f75a92019-08-12 17:54:09 +0200132 // If not blank, path to the baseline txt file for approved API check violations.
133 Baseline_file *string `android:"path"`
134
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900135 // Arguments to the apicheck tool.
Nan Zhang61819ce2018-05-04 18:49:16 -0700136 Args *string
137}
138
Nan Zhang581fd212018-01-10 16:06:12 -0800139type DroiddocProperties struct {
140 // directory relative to top of the source tree that contains doc templates files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800141 Custom_template *string
Nan Zhang581fd212018-01-10 16:06:12 -0800142
Nan Zhanga40da042018-08-01 12:48:00 -0700143 // directories under current module source which contains html/jd files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800144 Html_dirs []string
Nan Zhang581fd212018-01-10 16:06:12 -0800145
146 // set a value in the Clearsilver hdf namespace.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800147 Hdf []string
Nan Zhang581fd212018-01-10 16:06:12 -0800148
149 // proofread file contains all of the text content of the javadocs concatenated into one file,
150 // suitable for spell-checking and other goodness.
Colin Crossab054432019-07-15 16:13:59 -0700151 Proofread_file *string
Nan Zhang581fd212018-01-10 16:06:12 -0800152
153 // a todo file lists the program elements that are missing documentation.
154 // At some point, this might be improved to show more warnings.
Colin Cross27b922f2019-03-04 22:35:41 -0800155 Todo_file *string `android:"path"`
Nan Zhangb2b33de2018-02-23 11:18:47 -0800156
157 // directory under current module source that provide additional resources (images).
158 Resourcesdir *string
159
160 // resources output directory under out/soong/.intermediates.
161 Resourcesoutdir *string
Nan Zhang581fd212018-01-10 16:06:12 -0800162
Nan Zhange2ba5d42018-07-11 15:16:55 -0700163 // index.html under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800164 Static_doc_index_redirect *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700165
166 // source.properties under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800167 Static_doc_properties *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700168
Nan Zhang581fd212018-01-10 16:06:12 -0800169 // a list of files under current module source dir which contains known tags in Java sources.
170 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -0800171 Knowntags []string `android:"path"`
Nan Zhang28c68b92018-03-13 16:17:01 -0700172
Nan Zhang1598a9e2018-09-04 17:14:32 -0700173 // if set to true, generate docs through Dokka instead of Doclava.
174 Dokka_enabled *bool
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000175
176 // Compat config XML. Generates compat change documentation if set.
177 Compat_config *string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -0700178}
179
180type DroidstubsProperties struct {
Liz Kammer9ed79152020-08-24 15:56:32 -0700181 // The generated public API filename by Metalava, defaults to <module>_api.txt
Nan Zhang1598a9e2018-09-04 17:14:32 -0700182 Api_filename *string
183
Liz Kammer9ed79152020-08-24 15:56:32 -0700184 // the generated removed API filename by Metalava, defaults to <module>_removed.txt
Nan Zhang1598a9e2018-09-04 17:14:32 -0700185 Removed_api_filename *string
186
Nan Zhang199645c2018-09-19 12:40:06 -0700187 // the generated removed Dex API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700188 Removed_dex_api_filename *string
189
Nan Zhang1598a9e2018-09-04 17:14:32 -0700190 Check_api struct {
191 Last_released ApiToCheck
192
193 Current ApiToCheck
Inseob Kim38449af2019-02-28 14:24:05 +0900194
Paul Duffin160fe412020-05-10 19:32:20 +0100195 // The java_sdk_library module generates references to modules (i.e. filegroups)
196 // from which information about the latest API version can be obtained. As those
197 // modules may not exist (e.g. because a previous version has not been released) it
198 // sets ignore_missing_latest_api=true on the droidstubs modules it creates so
199 // that droidstubs can ignore those references if the modules do not yet exist.
200 //
201 // If true then this will ignore module references for modules that do not exist
202 // in properties that supply the previous version of the API.
203 //
204 // There are two sets of those:
205 // * Api_file, Removed_api_file in check_api.last_released
206 // * New_since in check_api.api_lint.new_since
207 //
208 // The first two must be set as a pair, so either they should both exist or neither
209 // should exist - in which case when this property is true they are ignored. If one
210 // exists and the other does not then it is an error.
Inseob Kim38449af2019-02-28 14:24:05 +0900211 Ignore_missing_latest_api *bool `blueprint:"mutated"`
Adrian Roos075eedc2019-10-10 12:07:03 +0200212
213 Api_lint struct {
214 Enabled *bool
215
216 // If set, performs api_lint on any new APIs not found in the given signature file
217 New_since *string `android:"path"`
218
219 // If not blank, path to the baseline txt file for approved API lint violations.
220 Baseline_file *string `android:"path"`
221 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700222 }
Nan Zhang79614d12018-04-19 18:03:39 -0700223
224 // user can specify the version of previous released API file in order to do compatibility check.
Colin Cross27b922f2019-03-04 22:35:41 -0800225 Previous_api *string `android:"path"`
Nan Zhang79614d12018-04-19 18:03:39 -0700226
227 // is set to true, Metalava will allow framework SDK to contain annotations.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700228 Annotations_enabled *bool
Nan Zhang79614d12018-04-19 18:03:39 -0700229
Pete Gillin77167902018-09-19 18:16:26 +0100230 // a list of top-level directories containing files to merge qualifier annotations (i.e. those intended to be included in the stubs written) from.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700231 Merge_annotations_dirs []string
Nan Zhang86d2d552018-08-09 15:33:27 -0700232
Pete Gillin77167902018-09-19 18:16:26 +0100233 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
234 Merge_inclusion_annotations_dirs []string
235
Pete Gillinc382a562018-11-14 18:45:46 +0000236 // a file containing a list of classes to do nullability validation for.
237 Validate_nullability_from_list *string
238
Pete Gillin581d6082018-10-22 15:55:04 +0100239 // a file containing expected warnings produced by validation of nullability annotations.
240 Check_nullability_warnings *string
241
Nan Zhang1598a9e2018-09-04 17:14:32 -0700242 // if set to true, allow Metalava to generate doc_stubs source files. Defaults to false.
243 Create_doc_stubs *bool
Nan Zhang9c69a122018-08-22 10:22:08 -0700244
Paul Duffin6877e6d2020-09-25 19:59:14 +0100245 // if set to true, cause Metalava to output Javadoc comments in the stubs source files. Defaults to false.
246 // Has no effect if create_doc_stubs: true.
247 Output_javadoc_comments *bool
248
Paul Duffin3ae29512020-04-08 18:18:03 +0100249 // if set to false then do not write out stubs. Defaults to true.
250 //
251 // TODO(b/146727827): Remove capability when we do not need to generate stubs and API separately.
252 Generate_stubs *bool
253
Anton Hansson52ac73d2020-10-26 09:57:40 +0000254 // if set to true, provides a hint to the build system that this rule uses a lot of memory,
255 // whicih can be used for scheduling purposes
256 High_mem *bool
257
Nan Zhang9c69a122018-08-22 10:22:08 -0700258 // is set to true, Metalava will allow framework SDK to contain API levels annotations.
259 Api_levels_annotations_enabled *bool
260
261 // the dirs which Metalava extracts API levels annotations from.
262 Api_levels_annotations_dirs []string
263
Liz Kammer3d894b72020-08-04 09:55:13 -0700264 // the filename which Metalava extracts API levels annotations from. Defaults to android.jar.
265 Api_levels_jar_filename *string
266
Nan Zhang9c69a122018-08-22 10:22:08 -0700267 // if set to true, collect the values used by the Dev tools and
268 // write them in files packaged with the SDK. Defaults to false.
269 Write_sdk_values *bool
Nan Zhang581fd212018-01-10 16:06:12 -0800270}
271
Nan Zhanga40da042018-08-01 12:48:00 -0700272//
273// Common flags passed down to build rule
274//
275type droiddocBuilderFlags struct {
Nan Zhang86d2d552018-08-09 15:33:27 -0700276 bootClasspathArgs string
277 classpathArgs string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700278 sourcepathArgs string
Nan Zhang86d2d552018-08-09 15:33:27 -0700279 dokkaClasspathArgs string
280 aidlFlags string
Colin Cross3047fa22019-04-18 10:56:44 -0700281 aidlDeps android.Paths
Nan Zhanga40da042018-08-01 12:48:00 -0700282
Nan Zhanga40da042018-08-01 12:48:00 -0700283 doclavaStubsFlags string
Nan Zhang86d2d552018-08-09 15:33:27 -0700284 doclavaDocsFlags string
Nan Zhanga40da042018-08-01 12:48:00 -0700285 postDoclavaCmds string
Nan Zhanga40da042018-08-01 12:48:00 -0700286}
287
288func InitDroiddocModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
289 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
290 android.InitDefaultableModule(module)
291}
292
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200293func apiCheckEnabled(ctx android.ModuleContext, apiToCheck ApiToCheck, apiVersionTag string) bool {
294 if ctx.Config().IsEnvTrue("WITHOUT_CHECK_API") {
295 return false
296 } else if String(apiToCheck.Api_file) != "" && String(apiToCheck.Removed_api_file) != "" {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700297 return true
298 } else if String(apiToCheck.Api_file) != "" {
299 panic("for " + apiVersionTag + " removed_api_file has to be non-empty!")
300 } else if String(apiToCheck.Removed_api_file) != "" {
301 panic("for " + apiVersionTag + " api_file has to be non-empty!")
302 }
303
304 return false
305}
306
Inseob Kim38449af2019-02-28 14:24:05 +0900307func ignoreMissingModules(ctx android.BottomUpMutatorContext, apiToCheck *ApiToCheck) {
308 api_file := String(apiToCheck.Api_file)
309 removed_api_file := String(apiToCheck.Removed_api_file)
310
311 api_module := android.SrcIsModule(api_file)
312 removed_api_module := android.SrcIsModule(removed_api_file)
313
314 if api_module == "" || removed_api_module == "" {
315 return
316 }
317
318 if ctx.OtherModuleExists(api_module) || ctx.OtherModuleExists(removed_api_module) {
319 return
320 }
321
322 apiToCheck.Api_file = nil
323 apiToCheck.Removed_api_file = nil
324}
325
Paul Duffin3d1248c2020-04-09 00:10:17 +0100326// Used by xsd_config
Nan Zhang1598a9e2018-09-04 17:14:32 -0700327type ApiFilePath interface {
328 ApiFilePath() android.Path
329}
330
Paul Duffin0f8faff2020-05-20 16:18:00 +0100331type ApiStubsSrcProvider interface {
332 StubsSrcJar() android.Path
333}
334
Paul Duffin3d1248c2020-04-09 00:10:17 +0100335// Provider of information about API stubs, used by java_sdk_library.
336type ApiStubsProvider interface {
337 ApiFilePath
Paul Duffin1fd005d2020-04-09 01:08:11 +0100338 RemovedApiFilePath() android.Path
Paul Duffin0f8faff2020-05-20 16:18:00 +0100339
340 ApiStubsSrcProvider
Paul Duffin3d1248c2020-04-09 00:10:17 +0100341}
342
Nan Zhanga40da042018-08-01 12:48:00 -0700343//
344// Javadoc
345//
Nan Zhang581fd212018-01-10 16:06:12 -0800346type Javadoc struct {
347 android.ModuleBase
348 android.DefaultableModuleBase
349
350 properties JavadocProperties
351
352 srcJars android.Paths
353 srcFiles android.Paths
354 sourcepaths android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700355 argFiles android.Paths
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400356 implicits android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700357
Liz Kammer585cac22020-07-06 09:12:57 -0700358 args []string
Nan Zhang581fd212018-01-10 16:06:12 -0800359
Nan Zhangccff0f72018-03-08 17:26:16 -0800360 docZip android.WritablePath
361 stubsSrcJar android.WritablePath
Nan Zhang581fd212018-01-10 16:06:12 -0800362}
363
Colin Cross41955e82019-05-29 14:40:35 -0700364func (j *Javadoc) OutputFiles(tag string) (android.Paths, error) {
365 switch tag {
366 case "":
367 return android.Paths{j.stubsSrcJar}, nil
Colin Crosse68e5542019-08-12 13:11:40 -0700368 case ".docs.zip":
369 return android.Paths{j.docZip}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700370 default:
371 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
372 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800373}
374
Colin Crossa3002fc2019-07-08 16:48:04 -0700375// javadoc converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800376func JavadocFactory() android.Module {
377 module := &Javadoc{}
378
379 module.AddProperties(&module.properties)
380
381 InitDroiddocModule(module, android.HostAndDeviceSupported)
382 return module
383}
384
Colin Crossa3002fc2019-07-08 16:48:04 -0700385// javadoc_host converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800386func JavadocHostFactory() android.Module {
387 module := &Javadoc{}
388
389 module.AddProperties(&module.properties)
390
391 InitDroiddocModule(module, android.HostSupported)
392 return module
393}
394
Colin Cross41955e82019-05-29 14:40:35 -0700395var _ android.OutputFileProducer = (*Javadoc)(nil)
Nan Zhang581fd212018-01-10 16:06:12 -0800396
Jiyong Park6a927c42020-01-21 02:03:43 +0900397func (j *Javadoc) sdkVersion() sdkSpec {
398 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700399}
400
Paul Duffine25c6442019-10-11 13:50:28 +0100401func (j *Javadoc) systemModules() string {
402 return proptools.String(j.properties.System_modules)
403}
404
Jiyong Park6a927c42020-01-21 02:03:43 +0900405func (j *Javadoc) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700406 return j.sdkVersion()
407}
408
Jiyong Park6a927c42020-01-21 02:03:43 +0900409func (j *Javadoc) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700410 return j.sdkVersion()
411}
412
Nan Zhang581fd212018-01-10 16:06:12 -0800413func (j *Javadoc) addDeps(ctx android.BottomUpMutatorContext) {
414 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100415 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Pete Gilline3d44b22020-06-29 11:28:51 +0100416 if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700417 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100418 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700419 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Pete Gilline3d44b22020-06-29 11:28:51 +0100420 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
Nan Zhang581fd212018-01-10 16:06:12 -0800421 }
422 }
423
Colin Cross42d48b72018-08-29 14:10:52 -0700424 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800425}
426
Nan Zhanga40da042018-08-01 12:48:00 -0700427func (j *Javadoc) collectAidlFlags(ctx android.ModuleContext, deps deps) droiddocBuilderFlags {
428 var flags droiddocBuilderFlags
Jiyong Park1e440682018-05-23 18:42:04 +0900429
Colin Cross3047fa22019-04-18 10:56:44 -0700430 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Jiyong Park1e440682018-05-23 18:42:04 +0900431
432 return flags
433}
434
435func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700436 aidlIncludeDirs android.Paths) (string, android.Paths) {
Jiyong Park1e440682018-05-23 18:42:04 +0900437
438 aidlIncludes := android.PathsForModuleSrc(ctx, j.properties.Aidl.Local_include_dirs)
439 aidlIncludes = append(aidlIncludes, android.PathsForSource(ctx, j.properties.Aidl.Include_dirs)...)
440
441 var flags []string
Colin Cross3047fa22019-04-18 10:56:44 -0700442 var deps android.Paths
443
Jiyong Park1e440682018-05-23 18:42:04 +0900444 if aidlPreprocess.Valid() {
445 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700446 deps = append(deps, aidlPreprocess.Path())
Jiyong Park1e440682018-05-23 18:42:04 +0900447 } else {
448 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
449 }
450
451 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
452 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
453 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
454 flags = append(flags, "-I"+src.String())
455 }
456
Colin Cross3047fa22019-04-18 10:56:44 -0700457 return strings.Join(flags, " "), deps
Jiyong Park1e440682018-05-23 18:42:04 +0900458}
459
Jiyong Parkd90d7412019-08-20 22:49:19 +0900460// TODO: remove the duplication between this and the one in gen.go
Jiyong Park1e440682018-05-23 18:42:04 +0900461func (j *Javadoc) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Nan Zhanga40da042018-08-01 12:48:00 -0700462 flags droiddocBuilderFlags) android.Paths {
Jiyong Park1e440682018-05-23 18:42:04 +0900463
464 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Crossc0806172019-06-14 18:51:47 -0700465 var aidlSrcs android.Paths
Jiyong Park1e440682018-05-23 18:42:04 +0900466
Jiyong Park1112c4c2019-08-16 21:12:10 +0900467 aidlIncludeFlags := genAidlIncludeFlags(srcFiles)
468
Jiyong Park1e440682018-05-23 18:42:04 +0900469 for _, srcFile := range srcFiles {
470 switch srcFile.Ext() {
471 case ".aidl":
Colin Crossc0806172019-06-14 18:51:47 -0700472 aidlSrcs = append(aidlSrcs, srcFile)
Jiyong Parkd90d7412019-08-20 22:49:19 +0900473 case ".logtags":
474 javaFile := genLogtags(ctx, srcFile)
475 outSrcFiles = append(outSrcFiles, javaFile)
Jiyong Park1e440682018-05-23 18:42:04 +0900476 default:
477 outSrcFiles = append(outSrcFiles, srcFile)
478 }
479 }
480
Colin Crossc0806172019-06-14 18:51:47 -0700481 // Process all aidl files together to support sharding them into one or more rules that produce srcjars.
482 if len(aidlSrcs) > 0 {
483 srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags+aidlIncludeFlags, flags.aidlDeps)
484 outSrcFiles = append(outSrcFiles, srcJarFiles...)
485 }
486
Jiyong Park1e440682018-05-23 18:42:04 +0900487 return outSrcFiles
488}
489
Nan Zhang581fd212018-01-10 16:06:12 -0800490func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
491 var deps deps
492
Colin Cross83bb3162018-06-25 15:48:06 -0700493 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800494 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700495 ctx.AddMissingDependencies(sdkDep.bootclasspath)
496 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Nan Zhang581fd212018-01-10 16:06:12 -0800497 } else if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700498 deps.bootClasspath = append(deps.bootClasspath, sdkDep.jars...)
Anton Hansson26bf49b2020-02-08 20:26:29 +0000499 deps.aidlPreprocess = sdkDep.aidl
500 } else {
501 deps.aidlPreprocess = sdkDep.aidl
Nan Zhang581fd212018-01-10 16:06:12 -0800502 }
503
504 ctx.VisitDirectDeps(func(module android.Module) {
505 otherName := ctx.OtherModuleName(module)
506 tag := ctx.OtherModuleDependencyTag(module)
507
Colin Cross2d24c1b2018-05-23 10:59:18 -0700508 switch tag {
509 case bootClasspathTag:
510 if dep, ok := module.(Dependency); ok {
Nan Zhang581fd212018-01-10 16:06:12 -0800511 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars()...)
Paul Duffin83a2d962019-11-19 19:44:10 +0000512 } else if sm, ok := module.(SystemModulesProvider); ok {
Paul Duffine25c6442019-10-11 13:50:28 +0100513 // A system modules dependency has been added to the bootclasspath
514 // so add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +0000515 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700516 } else {
517 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
518 }
519 case libTag:
520 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800521 case SdkLibraryDependency:
Paul Duffin649dadf2020-05-26 11:42:13 +0100522 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700523 case Dependency:
Sundong Ahnba493602018-11-20 17:36:35 +0900524 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park19a7f252019-07-10 16:59:31 +0900525 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700526 case android.SourceFileProducer:
Nan Zhang581fd212018-01-10 16:06:12 -0800527 checkProducesJars(ctx, dep)
528 deps.classpath = append(deps.classpath, dep.Srcs()...)
Nan Zhang581fd212018-01-10 16:06:12 -0800529 default:
530 ctx.ModuleErrorf("depends on non-java module %q", otherName)
531 }
Colin Cross6cef4812019-10-17 14:23:50 -0700532 case java9LibTag:
533 switch dep := module.(type) {
534 case Dependency:
535 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
536 default:
537 ctx.ModuleErrorf("depends on non-java module %q", otherName)
538 }
Nan Zhang357466b2018-04-17 17:38:36 -0700539 case systemModulesTag:
540 if deps.systemModules != nil {
541 panic("Found two system module dependencies")
542 }
Paul Duffin83a2d962019-11-19 19:44:10 +0000543 sm := module.(SystemModulesProvider)
544 outputDir, outputDeps := sm.OutputDirAndDeps()
545 deps.systemModules = &systemModules{outputDir, outputDeps}
Nan Zhang581fd212018-01-10 16:06:12 -0800546 }
547 })
548 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
549 // may contain filegroup or genrule.
Colin Cross8a497952019-03-05 22:25:09 -0800550 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400551 j.implicits = append(j.implicits, srcFiles...)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900552
553 filterByPackage := func(srcs []android.Path, filterPackages []string) []android.Path {
554 if filterPackages == nil {
555 return srcs
556 }
557 filtered := []android.Path{}
558 for _, src := range srcs {
559 if src.Ext() != ".java" {
560 // Don't filter-out non-Java (=generated sources) by package names. This is not ideal,
561 // but otherwise metalava emits stub sources having references to the generated AIDL classes
562 // in filtered-out pacages (e.g. com.android.internal.*).
563 // TODO(b/141149570) We need to fix this by introducing default private constructors or
564 // fixing metalava to not emit constructors having references to unknown classes.
565 filtered = append(filtered, src)
566 continue
567 }
568 packageName := strings.ReplaceAll(filepath.Dir(src.Rel()), "/", ".")
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800569 if android.HasAnyPrefix(packageName, filterPackages) {
570 filtered = append(filtered, src)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900571 }
572 }
573 return filtered
574 }
575 srcFiles = filterByPackage(srcFiles, j.properties.Filter_packages)
576
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400577 // While metalava needs package html files, it does not need them to be explicit on the command
Anton Hansson746be9c2020-10-08 19:05:40 +0100578 // line. javadoc complains if it receives html files on the command line. The filter
579 // below excludes html files from the rsp file metalava. Note that the html
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400580 // files are still included as implicit inputs for successful remote execution and correct
581 // incremental builds.
582 filterHtml := func(srcs []android.Path) []android.Path {
583 filtered := []android.Path{}
584 for _, src := range srcs {
585 if src.Ext() == ".html" {
586 continue
587 }
588 filtered = append(filtered, src)
589 }
590 return filtered
591 }
592 srcFiles = filterHtml(srcFiles)
593
Liz Kammer585cac22020-07-06 09:12:57 -0700594 aidlFlags := j.collectAidlFlags(ctx, deps)
595 srcFiles = j.genSources(ctx, srcFiles, aidlFlags)
Nan Zhang581fd212018-01-10 16:06:12 -0800596
597 // srcs may depend on some genrule output.
598 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800599 j.srcJars = append(j.srcJars, deps.srcJars...)
600
Nan Zhang581fd212018-01-10 16:06:12 -0800601 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800602 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800603
Liz Kammere1ab2502020-09-10 15:29:25 +0000604 if len(j.srcFiles) > 0 {
605 j.sourcepaths = android.PathsForModuleSrc(ctx, []string{"."})
Nan Zhang581fd212018-01-10 16:06:12 -0800606 }
Nan Zhang581fd212018-01-10 16:06:12 -0800607
Colin Cross8a497952019-03-05 22:25:09 -0800608 j.argFiles = android.PathsForModuleSrc(ctx, j.properties.Arg_files)
Paul Duffin99e4a502019-02-11 15:38:42 +0000609 argFilesMap := map[string]string{}
610 argFileLabels := []string{}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700611
Paul Duffin99e4a502019-02-11 15:38:42 +0000612 for _, label := range j.properties.Arg_files {
Colin Cross8a497952019-03-05 22:25:09 -0800613 var paths = android.PathsForModuleSrc(ctx, []string{label})
Paul Duffin99e4a502019-02-11 15:38:42 +0000614 if _, exists := argFilesMap[label]; !exists {
615 argFilesMap[label] = strings.Join(paths.Strings(), " ")
616 argFileLabels = append(argFileLabels, label)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700617 } else {
618 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000619 label, argFilesMap[label], paths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700620 }
621 }
622
Liz Kammer585cac22020-07-06 09:12:57 -0700623 var argsPropertyName string
624 flags := make([]string, 0)
625 if j.properties.Args != nil && j.properties.Flags != nil {
626 ctx.PropertyErrorf("args", "flags is set. Cannot set args")
627 } else if args := proptools.String(j.properties.Args); args != "" {
628 flags = append(flags, args)
629 argsPropertyName = "args"
630 } else {
631 flags = append(flags, j.properties.Flags...)
632 argsPropertyName = "flags"
633 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700634
Liz Kammer585cac22020-07-06 09:12:57 -0700635 for _, flag := range flags {
636 args, err := android.Expand(flag, func(name string) (string, error) {
637 if strings.HasPrefix(name, "location ") {
638 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
639 if paths, ok := argFilesMap[label]; ok {
640 return paths, nil
641 } else {
642 return "", fmt.Errorf("unknown location label %q, expecting one of %q",
643 label, strings.Join(argFileLabels, ", "))
644 }
645 } else if name == "genDir" {
646 return android.PathForModuleGen(ctx).String(), nil
647 }
648 return "", fmt.Errorf("unknown variable '$(%s)'", name)
649 })
650
651 if err != nil {
652 ctx.PropertyErrorf(argsPropertyName, "%s", err.Error())
653 }
654 j.args = append(j.args, args)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700655 }
656
Nan Zhang581fd212018-01-10 16:06:12 -0800657 return deps
658}
659
660func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
661 j.addDeps(ctx)
662}
663
664func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
665 deps := j.collectDeps(ctx)
666
Colin Crossdaa4c672019-07-15 22:53:46 -0700667 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800668
Colin Crossdaa4c672019-07-15 22:53:46 -0700669 outDir := android.PathForModuleOut(ctx, "out")
670 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
671
672 j.stubsSrcJar = nil
673
674 rule := android.NewRuleBuilder()
675
676 rule.Command().Text("rm -rf").Text(outDir.String())
677 rule.Command().Text("mkdir -p").Text(outDir.String())
678
679 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, j.srcJars)
Nan Zhang357466b2018-04-17 17:38:36 -0700680
Colin Cross83bb3162018-06-25 15:48:06 -0700681 javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800682
Colin Crossdaa4c672019-07-15 22:53:46 -0700683 cmd := javadocSystemModulesCmd(ctx, rule, j.srcFiles, outDir, srcJarDir, srcJarList,
684 deps.systemModules, deps.classpath, j.sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800685
Colin Cross1e743852019-10-28 11:37:20 -0700686 cmd.FlagWithArg("-source ", javaVersion.String()).
Colin Crossdaa4c672019-07-15 22:53:46 -0700687 Flag("-J-Xmx1024m").
688 Flag("-XDignore.symbol.file").
689 Flag("-Xdoclint:none")
Nan Zhang581fd212018-01-10 16:06:12 -0800690
Colin Crossdaa4c672019-07-15 22:53:46 -0700691 rule.Command().
692 BuiltTool(ctx, "soong_zip").
693 Flag("-write_if_changed").
694 Flag("-d").
695 FlagWithOutput("-o ", j.docZip).
696 FlagWithArg("-C ", outDir.String()).
697 FlagWithArg("-D ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700698
Colin Crossdaa4c672019-07-15 22:53:46 -0700699 rule.Restat()
700
701 zipSyncCleanupCmd(rule, srcJarDir)
702
703 rule.Build(pctx, ctx, "javadoc", "javadoc")
Nan Zhang581fd212018-01-10 16:06:12 -0800704}
705
Nan Zhanga40da042018-08-01 12:48:00 -0700706//
707// Droiddoc
708//
709type Droiddoc struct {
710 Javadoc
711
Liz Kammere1ab2502020-09-10 15:29:25 +0000712 properties DroiddocProperties
Nan Zhanga40da042018-08-01 12:48:00 -0700713}
714
Colin Crossa3002fc2019-07-08 16:48:04 -0700715// droiddoc converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700716func DroiddocFactory() android.Module {
717 module := &Droiddoc{}
718
719 module.AddProperties(&module.properties,
720 &module.Javadoc.properties)
721
722 InitDroiddocModule(module, android.HostAndDeviceSupported)
723 return module
724}
725
Colin Crossa3002fc2019-07-08 16:48:04 -0700726// droiddoc_host converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700727func DroiddocHostFactory() android.Module {
728 module := &Droiddoc{}
729
730 module.AddProperties(&module.properties,
731 &module.Javadoc.properties)
732
733 InitDroiddocModule(module, android.HostSupported)
734 return module
735}
736
Liz Kammere1ab2502020-09-10 15:29:25 +0000737func (d *Droiddoc) OutputFiles(tag string) (android.Paths, error) {
738 switch tag {
739 case "", ".docs.zip":
740 return android.Paths{d.Javadoc.docZip}, nil
741 default:
742 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
743 }
Nan Zhanga40da042018-08-01 12:48:00 -0700744}
745
Nan Zhang581fd212018-01-10 16:06:12 -0800746func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
747 d.Javadoc.addDeps(ctx)
748
Nan Zhang79614d12018-04-19 18:03:39 -0700749 if String(d.properties.Custom_template) != "" {
Dan Willemsencc090972018-02-26 14:33:31 -0800750 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
751 }
Nan Zhang581fd212018-01-10 16:06:12 -0800752}
753
Colin Crossab054432019-07-15 16:13:59 -0700754func (d *Droiddoc) doclavaDocsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, docletPath classpath) {
Colin Cross2a2e0db2020-02-21 16:55:46 -0800755 buildNumberFile := ctx.Config().BuildNumberFile(ctx)
Nan Zhang443fa522018-08-20 20:58:28 -0700756 // Droiddoc always gets "-source 1.8" because it doesn't support 1.9 sources. For modules with 1.9
757 // sources, droiddoc will get sources produced by metalava which will have already stripped out the
758 // 1.9 language features.
Colin Crossab054432019-07-15 16:13:59 -0700759 cmd.FlagWithArg("-source ", "1.8").
760 Flag("-J-Xmx1600m").
761 Flag("-J-XX:-OmitStackTraceInFastThrow").
762 Flag("-XDignore.symbol.file").
763 FlagWithArg("-doclet ", "com.google.doclava.Doclava").
764 FlagWithInputList("-docletpath ", docletPath.Paths(), ":").
Colin Cross2a2e0db2020-02-21 16:55:46 -0800765 FlagWithArg("-hdf page.build ", ctx.Config().BuildId()+"-$(cat "+buildNumberFile.String()+")").OrderOnly(buildNumberFile).
Elliott Hughes26bce342019-09-12 15:05:13 -0700766 FlagWithArg("-hdf page.now ", `"$(date -d @$(cat `+ctx.Config().Getenv("BUILD_DATETIME_FILE")+`) "+%d %b %Y %k:%M")" `)
Nan Zhang46130972018-06-04 11:28:01 -0700767
Nan Zhanga40da042018-08-01 12:48:00 -0700768 if String(d.properties.Custom_template) == "" {
769 // TODO: This is almost always droiddoc-templates-sdk
770 ctx.PropertyErrorf("custom_template", "must specify a template")
771 }
772
773 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
Nan Zhangf4936b02018-08-01 15:00:28 -0700774 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Crossab054432019-07-15 16:13:59 -0700775 cmd.FlagWithArg("-templatedir ", t.dir.String()).Implicits(t.deps)
Nan Zhanga40da042018-08-01 12:48:00 -0700776 } else {
Paul Duffin884363e2019-12-19 10:21:09 +0000777 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_exported_dir", ctx.OtherModuleName(m))
Nan Zhanga40da042018-08-01 12:48:00 -0700778 }
779 })
780
781 if len(d.properties.Html_dirs) > 0 {
Colin Crossab054432019-07-15 16:13:59 -0700782 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
783 cmd.FlagWithArg("-htmldir ", htmlDir.String()).
784 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[0], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700785 }
786
787 if len(d.properties.Html_dirs) > 1 {
Colin Crossab054432019-07-15 16:13:59 -0700788 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
789 cmd.FlagWithArg("-htmldir2 ", htmlDir2.String()).
790 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[1], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700791 }
792
793 if len(d.properties.Html_dirs) > 2 {
794 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
795 }
796
Colin Cross8a497952019-03-05 22:25:09 -0800797 knownTags := android.PathsForModuleSrc(ctx, d.properties.Knowntags)
Colin Crossab054432019-07-15 16:13:59 -0700798 cmd.FlagForEachInput("-knowntags ", knownTags)
Nan Zhanga40da042018-08-01 12:48:00 -0700799
Colin Crossab054432019-07-15 16:13:59 -0700800 cmd.FlagForEachArg("-hdf ", d.properties.Hdf)
Nan Zhanga40da042018-08-01 12:48:00 -0700801
802 if String(d.properties.Proofread_file) != "" {
803 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
Colin Crossab054432019-07-15 16:13:59 -0700804 cmd.FlagWithOutput("-proofread ", proofreadFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700805 }
806
807 if String(d.properties.Todo_file) != "" {
808 // tricky part:
809 // we should not compute full path for todo_file through PathForModuleOut().
810 // the non-standard doclet will get the full path relative to "-o".
Colin Crossab054432019-07-15 16:13:59 -0700811 cmd.FlagWithArg("-todo ", String(d.properties.Todo_file)).
812 ImplicitOutput(android.PathForModuleOut(ctx, String(d.properties.Todo_file)))
Nan Zhanga40da042018-08-01 12:48:00 -0700813 }
814
815 if String(d.properties.Resourcesdir) != "" {
816 // TODO: should we add files under resourcesDir to the implicits? It seems that
817 // resourcesDir is one sub dir of htmlDir
818 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
Colin Crossab054432019-07-15 16:13:59 -0700819 cmd.FlagWithArg("-resourcesdir ", resourcesDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700820 }
821
822 if String(d.properties.Resourcesoutdir) != "" {
823 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
Colin Crossab054432019-07-15 16:13:59 -0700824 cmd.FlagWithArg("-resourcesoutdir ", String(d.properties.Resourcesoutdir))
Nan Zhanga40da042018-08-01 12:48:00 -0700825 }
Nan Zhanga40da042018-08-01 12:48:00 -0700826}
827
Colin Crossab054432019-07-15 16:13:59 -0700828func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700829 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700830 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
831 rule.Command().Text("cp").
832 Input(staticDocIndexRedirect).
833 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700834 }
835
836 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700837 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
838 rule.Command().Text("cp").
839 Input(staticDocProperties).
840 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700841 }
Nan Zhanga40da042018-08-01 12:48:00 -0700842}
843
Colin Crossab054432019-07-15 16:13:59 -0700844func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700845 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700846
847 cmd := rule.Command().
848 BuiltTool(ctx, "soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
849 Flag(config.JavacVmFlags).
850 FlagWithArg("-encoding ", "UTF-8").
Colin Crossab054432019-07-15 16:13:59 -0700851 FlagWithRspFileInputList("@", srcs).
852 FlagWithInput("@", srcJarList)
853
Colin Crossab054432019-07-15 16:13:59 -0700854 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
855 // based stubs generation.
856 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
857 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
858 // the correct package name base path.
859 if len(sourcepaths) > 0 {
860 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
861 } else {
862 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
863 }
864
865 cmd.FlagWithArg("-d ", outDir.String()).
866 Flag("-quiet")
867
868 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700869}
870
Colin Crossdaa4c672019-07-15 22:53:46 -0700871func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
872 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
873 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
874
875 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
876
877 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
878 cmd.Flag(flag).Implicits(deps)
879
880 cmd.FlagWithArg("--patch-module ", "java.base=.")
881
882 if len(classpath) > 0 {
883 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
884 }
885
886 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700887}
888
Colin Crossdaa4c672019-07-15 22:53:46 -0700889func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
890 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
891 sourcepaths android.Paths) *android.RuleBuilderCommand {
892
893 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
894
895 if len(bootclasspath) == 0 && ctx.Device() {
896 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
897 // ensure java does not fall back to the default bootclasspath.
898 cmd.FlagWithArg("-bootclasspath ", `""`)
899 } else if len(bootclasspath) > 0 {
900 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
901 }
902
903 if len(classpath) > 0 {
904 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
905 }
906
907 return cmd
908}
909
Colin Crossab054432019-07-15 16:13:59 -0700910func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
911 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700912
Colin Crossab054432019-07-15 16:13:59 -0700913 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
914 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
915
916 return rule.Command().
917 BuiltTool(ctx, "dokka").
918 Flag(config.JavacVmFlags).
919 Flag(srcJarDir.String()).
920 FlagWithInputList("-classpath ", dokkaClasspath, ":").
921 FlagWithArg("-format ", "dac").
922 FlagWithArg("-dacRoot ", "/reference/kotlin").
923 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700924}
925
926func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
927 deps := d.Javadoc.collectDeps(ctx)
928
Colin Crossdaa4c672019-07-15 22:53:46 -0700929 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Colin Crossdaa4c672019-07-15 22:53:46 -0700930
Nan Zhang1598a9e2018-09-04 17:14:32 -0700931 jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
932 doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700933
Colin Crossab054432019-07-15 16:13:59 -0700934 outDir := android.PathForModuleOut(ctx, "out")
935 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700936
Colin Crossab054432019-07-15 16:13:59 -0700937 rule := android.NewRuleBuilder()
Nan Zhang1598a9e2018-09-04 17:14:32 -0700938
Colin Crossab054432019-07-15 16:13:59 -0700939 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
940
941 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -0700942 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -0700943 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700944 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -0700945 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -0700946 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700947 }
948
Liz Kammer585cac22020-07-06 09:12:57 -0700949 cmd.Flag(strings.Join(d.Javadoc.args, " ")).Implicits(d.Javadoc.argFiles)
Colin Crossab054432019-07-15 16:13:59 -0700950
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000951 if d.properties.Compat_config != nil {
952 compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
953 cmd.FlagWithInput("-compatconfig ", compatConfig)
954 }
955
Colin Crossab054432019-07-15 16:13:59 -0700956 var desc string
957 if Bool(d.properties.Dokka_enabled) {
958 desc = "dokka"
959 } else {
960 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
961
962 for _, o := range d.Javadoc.properties.Out {
963 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
964 }
965
966 d.postDoclavaCmds(ctx, rule)
967 desc = "doclava"
968 }
969
970 rule.Command().
971 BuiltTool(ctx, "soong_zip").
972 Flag("-write_if_changed").
973 Flag("-d").
974 FlagWithOutput("-o ", d.docZip).
975 FlagWithArg("-C ", outDir.String()).
976 FlagWithArg("-D ", outDir.String())
977
Colin Crossab054432019-07-15 16:13:59 -0700978 rule.Restat()
979
980 zipSyncCleanupCmd(rule, srcJarDir)
981
982 rule.Build(pctx, ctx, "javadoc", desc)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700983}
984
985//
986// Droidstubs
987//
988type Droidstubs struct {
989 Javadoc
Paul Duffin91547182019-11-12 19:39:36 +0000990 android.SdkBase
Nan Zhang1598a9e2018-09-04 17:14:32 -0700991
Pete Gillin581d6082018-10-22 15:55:04 +0100992 properties DroidstubsProperties
993 apiFile android.WritablePath
994 apiXmlFile android.WritablePath
995 lastReleasedApiXmlFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +0100996 privateApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +0100997 removedApiFile android.WritablePath
998 removedDexApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +0100999 nullabilityWarningsFile android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001000
1001 checkCurrentApiTimestamp android.WritablePath
1002 updateCurrentApiTimestamp android.WritablePath
1003 checkLastReleasedApiTimestamp android.WritablePath
Adrian Roos075eedc2019-10-10 12:07:03 +02001004 apiLintTimestamp android.WritablePath
Adrian Roos3b8f1cd2019-11-01 13:42:39 +01001005 apiLintReport android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001006
Pete Gillin581d6082018-10-22 15:55:04 +01001007 checkNullabilityWarningsTimestamp android.WritablePath
1008
Nan Zhang1598a9e2018-09-04 17:14:32 -07001009 annotationsZip android.WritablePath
Nan Zhang9c69a122018-08-22 10:22:08 -07001010 apiVersionsXml android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001011
Paul Duffinbc0fe962020-10-13 15:04:02 +01001012 apiFilePath android.Path
1013 removedApiFilePath android.Path
Nan Zhang71bbe632018-09-17 14:32:21 -07001014
Jerome Gaillard0f599032019-10-10 19:29:11 +01001015 metadataZip android.WritablePath
1016 metadataDir android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001017}
1018
Colin Crossa3002fc2019-07-08 16:48:04 -07001019// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
1020// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to
1021// a droiddoc module to generate documentation.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001022func DroidstubsFactory() android.Module {
1023 module := &Droidstubs{}
1024
1025 module.AddProperties(&module.properties,
1026 &module.Javadoc.properties)
1027
1028 InitDroiddocModule(module, android.HostAndDeviceSupported)
Paul Duffin91547182019-11-12 19:39:36 +00001029 android.InitSdkAwareModule(module)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001030 return module
1031}
1032
Colin Crossa3002fc2019-07-08 16:48:04 -07001033// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API
1034// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be
1035// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs
1036// module when symbols needed by the source files are provided by java_library_host modules.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001037func DroidstubsHostFactory() android.Module {
1038 module := &Droidstubs{}
1039
1040 module.AddProperties(&module.properties,
1041 &module.Javadoc.properties)
1042
1043 InitDroiddocModule(module, android.HostSupported)
1044 return module
1045}
1046
Colin Cross014489c2020-06-02 20:09:13 -07001047func (d *Droidstubs) OutputFiles(tag string) (android.Paths, error) {
1048 switch tag {
1049 case "":
1050 return android.Paths{d.stubsSrcJar}, nil
1051 case ".docs.zip":
1052 return android.Paths{d.docZip}, nil
Paul Duffind8aed4b2020-11-26 00:26:42 +00001053 case ".api.txt", android.DefaultDistTag:
1054 // This is the default dist path for dist properties that have no tag property.
Anton Hanssonecf54352020-10-02 17:44:34 +01001055 return android.Paths{d.apiFilePath}, nil
1056 case ".removed-api.txt":
Paul Duffinbc0fe962020-10-13 15:04:02 +01001057 return android.Paths{d.removedApiFilePath}, nil
Colin Cross014489c2020-06-02 20:09:13 -07001058 case ".annotations.zip":
1059 return android.Paths{d.annotationsZip}, nil
1060 case ".api_versions.xml":
1061 return android.Paths{d.apiVersionsXml}, nil
1062 default:
1063 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1064 }
1065}
1066
Nan Zhang1598a9e2018-09-04 17:14:32 -07001067func (d *Droidstubs) ApiFilePath() android.Path {
1068 return d.apiFilePath
1069}
1070
Paul Duffin1fd005d2020-04-09 01:08:11 +01001071func (d *Droidstubs) RemovedApiFilePath() android.Path {
Paul Duffinbc0fe962020-10-13 15:04:02 +01001072 return d.removedApiFilePath
Paul Duffin1fd005d2020-04-09 01:08:11 +01001073}
1074
Paul Duffin3d1248c2020-04-09 00:10:17 +01001075func (d *Droidstubs) StubsSrcJar() android.Path {
1076 return d.stubsSrcJar
1077}
1078
Nan Zhang1598a9e2018-09-04 17:14:32 -07001079func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
1080 d.Javadoc.addDeps(ctx)
1081
Paul Duffin160fe412020-05-10 19:32:20 +01001082 // If requested clear any properties that provide information about the latest version
1083 // of an API and which reference non-existent modules.
Inseob Kim38449af2019-02-28 14:24:05 +09001084 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
1085 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
Paul Duffin160fe412020-05-10 19:32:20 +01001086
1087 // If the new_since references a module, e.g. :module-latest-api and the module
1088 // does not exist then clear it.
1089 newSinceSrc := d.properties.Check_api.Api_lint.New_since
1090 newSinceSrcModule := android.SrcIsModule(proptools.String(newSinceSrc))
1091 if newSinceSrcModule != "" && !ctx.OtherModuleExists(newSinceSrcModule) {
1092 d.properties.Check_api.Api_lint.New_since = nil
1093 }
Inseob Kim38449af2019-02-28 14:24:05 +09001094 }
1095
Nan Zhang1598a9e2018-09-04 17:14:32 -07001096 if len(d.properties.Merge_annotations_dirs) != 0 {
1097 for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
1098 ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
1099 }
1100 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001101
Pete Gillin77167902018-09-19 18:16:26 +01001102 if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
1103 for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
1104 ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
1105 }
1106 }
1107
Nan Zhang9c69a122018-08-22 10:22:08 -07001108 if len(d.properties.Api_levels_annotations_dirs) != 0 {
1109 for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
1110 ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
1111 }
1112 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001113}
1114
Paul Duffin3ae29512020-04-08 18:18:03 +01001115func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001116 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1117 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001118 String(d.properties.Api_filename) != "" {
Liz Kammer9ed79152020-08-24 15:56:32 -07001119 filename := proptools.StringDefault(d.properties.Api_filename, ctx.ModuleName()+"_api.txt")
1120 d.apiFile = android.PathForModuleOut(ctx, filename)
Colin Cross33961b52019-07-11 11:01:22 -07001121 cmd.FlagWithOutput("--api ", d.apiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001122 d.apiFilePath = d.apiFile
Paul Duffinbc0fe962020-10-13 15:04:02 +01001123 } else if sourceApiFile := proptools.String(d.properties.Check_api.Current.Api_file); sourceApiFile != "" {
1124 // If check api is disabled then make the source file available for export.
1125 d.apiFilePath = android.PathForModuleSrc(ctx, sourceApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001126 }
1127
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001128 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1129 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001130 String(d.properties.Removed_api_filename) != "" {
Liz Kammer9ed79152020-08-24 15:56:32 -07001131 filename := proptools.StringDefault(d.properties.Removed_api_filename, ctx.ModuleName()+"_removed.txt")
1132 d.removedApiFile = android.PathForModuleOut(ctx, filename)
Colin Cross33961b52019-07-11 11:01:22 -07001133 cmd.FlagWithOutput("--removed-api ", d.removedApiFile)
Paul Duffinbc0fe962020-10-13 15:04:02 +01001134 d.removedApiFilePath = d.removedApiFile
1135 } else if sourceRemovedApiFile := proptools.String(d.properties.Check_api.Current.Removed_api_file); sourceRemovedApiFile != "" {
1136 // If check api is disabled then make the source removed api file available for export.
1137 d.removedApiFilePath = android.PathForModuleSrc(ctx, sourceRemovedApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001138 }
1139
Nan Zhang1598a9e2018-09-04 17:14:32 -07001140 if String(d.properties.Removed_dex_api_filename) != "" {
1141 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001142 cmd.FlagWithOutput("--removed-dex-api ", d.removedDexApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001143 }
1144
Nan Zhang9c69a122018-08-22 10:22:08 -07001145 if Bool(d.properties.Write_sdk_values) {
Jerome Gaillard0f599032019-10-10 19:29:11 +01001146 d.metadataDir = android.PathForModuleOut(ctx, "metadata")
1147 cmd.FlagWithArg("--sdk-values ", d.metadataDir.String())
Nan Zhang9c69a122018-08-22 10:22:08 -07001148 }
1149
Paul Duffin3ae29512020-04-08 18:18:03 +01001150 if stubsDir.Valid() {
1151 if Bool(d.properties.Create_doc_stubs) {
1152 cmd.FlagWithArg("--doc-stubs ", stubsDir.String())
1153 } else {
1154 cmd.FlagWithArg("--stubs ", stubsDir.String())
Paul Duffin6877e6d2020-09-25 19:59:14 +01001155 if !Bool(d.properties.Output_javadoc_comments) {
1156 cmd.Flag("--exclude-documentation-from-stubs")
1157 }
Paul Duffin3ae29512020-04-08 18:18:03 +01001158 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001159 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001160}
1161
Colin Cross33961b52019-07-11 11:01:22 -07001162func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang1598a9e2018-09-04 17:14:32 -07001163 if Bool(d.properties.Annotations_enabled) {
Colin Cross33961b52019-07-11 11:01:22 -07001164 cmd.Flag("--include-annotations")
1165
Pete Gillinc382a562018-11-14 18:45:46 +00001166 validatingNullability :=
Liz Kammer585cac22020-07-06 09:12:57 -07001167 android.InList("--validate-nullability-from-merged-stubs", d.Javadoc.args) ||
Pete Gillinc382a562018-11-14 18:45:46 +00001168 String(d.properties.Validate_nullability_from_list) != ""
Paul Duffin13a9dd62019-11-04 10:26:47 +00001169
Pete Gillina262c052018-09-14 14:25:48 +01001170 migratingNullability := String(d.properties.Previous_api) != ""
Pete Gillina262c052018-09-14 14:25:48 +01001171 if migratingNullability {
Colin Cross8a497952019-03-05 22:25:09 -08001172 previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
Colin Cross33961b52019-07-11 11:01:22 -07001173 cmd.FlagWithInput("--migrate-nullness ", previousApi)
Pete Gillina262c052018-09-14 14:25:48 +01001174 }
Colin Cross33961b52019-07-11 11:01:22 -07001175
Pete Gillinc382a562018-11-14 18:45:46 +00001176 if s := String(d.properties.Validate_nullability_from_list); s != "" {
Colin Cross33961b52019-07-11 11:01:22 -07001177 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
Pete Gillinc382a562018-11-14 18:45:46 +00001178 }
Colin Cross33961b52019-07-11 11:01:22 -07001179
Pete Gillina262c052018-09-14 14:25:48 +01001180 if validatingNullability {
Pete Gillin581d6082018-10-22 15:55:04 +01001181 d.nullabilityWarningsFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_nullability_warnings.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001182 cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile)
Pete Gillina262c052018-09-14 14:25:48 +01001183 }
Nan Zhanga40da042018-08-01 12:48:00 -07001184
1185 d.annotationsZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"_annotations.zip")
Colin Cross33961b52019-07-11 11:01:22 -07001186 cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip)
Nan Zhangf4936b02018-08-01 15:00:28 -07001187
Anton Hansson9d7c3fb2020-05-21 10:11:31 +01001188 if len(d.properties.Merge_annotations_dirs) != 0 {
1189 d.mergeAnnoDirFlags(ctx, cmd)
Nan Zhanga40da042018-08-01 12:48:00 -07001190 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001191
Colin Cross33961b52019-07-11 11:01:22 -07001192 // TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
1193 cmd.FlagWithArg("--hide ", "HiddenTypedefConstant").
1194 FlagWithArg("--hide ", "SuperfluousPrefix").
1195 FlagWithArg("--hide ", "AnnotationExtraction")
1196 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001197}
1198
Colin Cross33961b52019-07-11 11:01:22 -07001199func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
1200 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
1201 if t, ok := m.(*ExportedDroiddocDir); ok {
1202 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
1203 } else {
1204 ctx.PropertyErrorf("merge_annotations_dirs",
1205 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1206 }
1207 })
1208}
1209
1210func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Pete Gillin77167902018-09-19 18:16:26 +01001211 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
1212 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Cross33961b52019-07-11 11:01:22 -07001213 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
Pete Gillin77167902018-09-19 18:16:26 +01001214 } else {
1215 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
1216 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1217 }
1218 })
Nan Zhanga40da042018-08-01 12:48:00 -07001219}
1220
Colin Cross33961b52019-07-11 11:01:22 -07001221func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001222 if !Bool(d.properties.Api_levels_annotations_enabled) {
1223 return
Nan Zhang9c69a122018-08-22 10:22:08 -07001224 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001225
1226 d.apiVersionsXml = android.PathForModuleOut(ctx, "api-versions.xml")
1227
1228 if len(d.properties.Api_levels_annotations_dirs) == 0 {
1229 ctx.PropertyErrorf("api_levels_annotations_dirs",
1230 "has to be non-empty if api levels annotations was enabled!")
1231 }
1232
1233 cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml)
1234 cmd.FlagWithInput("--apply-api-levels ", d.apiVersionsXml)
Dan Albert4f378d72020-07-23 17:32:15 -07001235 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion().String())
Liz Kammer3d894b72020-08-04 09:55:13 -07001236 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
1237
1238 filename := proptools.StringDefault(d.properties.Api_levels_jar_filename, "android.jar")
1239
1240 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
1241 if t, ok := m.(*ExportedDroiddocDir); ok {
1242 for _, dep := range t.deps {
1243 if strings.HasSuffix(dep.String(), filename) {
1244 cmd.Implicit(dep)
1245 }
1246 }
1247 cmd.FlagWithArg("--android-jar-pattern ", t.dir.String()+"/%/public/"+filename)
1248 } else {
1249 ctx.PropertyErrorf("api_levels_annotations_dirs",
1250 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
1251 }
1252 })
Nan Zhang9c69a122018-08-22 10:22:08 -07001253}
1254
Colin Cross1e743852019-10-28 11:37:20 -07001255func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths,
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001256 srcJarList android.Path, bootclasspath, classpath classpath, sourcepaths android.Paths, implicitsRsp android.WritablePath, sandbox bool) *android.RuleBuilderCommand {
Ramy Medhat427683c2020-04-30 03:08:37 -04001257 cmd := rule.Command()
Ramy Medhat16f23a42020-09-03 01:29:49 -04001258 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_METALAVA") {
Ramy Medhat427683c2020-04-30 03:08:37 -04001259 rule.Remoteable(android.RemoteRuleSupports{RBE: true})
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001260 pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "metalava")
1261 execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
1262 labels := map[string]string{"type": "compile", "lang": "java", "compiler": "metalava"}
1263 if !sandbox {
1264 execStrategy = remoteexec.LocalExecStrategy
1265 labels["shallow"] = "true"
Ramy Medhat427683c2020-04-30 03:08:37 -04001266 }
1267 inputs := []string{android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "metalava.jar").String()}
1268 if v := ctx.Config().Getenv("RBE_METALAVA_INPUTS"); v != "" {
1269 inputs = append(inputs, strings.Split(v, ",")...)
1270 }
1271 cmd.Text((&remoteexec.REParams{
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001272 Labels: labels,
Ramy Medhat427683c2020-04-30 03:08:37 -04001273 ExecStrategy: execStrategy,
1274 Inputs: inputs,
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001275 RSPFile: implicitsRsp.String(),
Ramy Medhat427683c2020-04-30 03:08:37 -04001276 ToolchainInputs: []string{config.JavaCmd(ctx).String()},
1277 Platform: map[string]string{remoteexec.PoolKey: pool},
1278 }).NoVarTemplate(ctx.Config()))
1279 }
1280
1281 cmd.BuiltTool(ctx, "metalava").
Colin Cross33961b52019-07-11 11:01:22 -07001282 Flag(config.JavacVmFlags).
Aurimas Liutikas4c5efde2020-09-21 11:18:06 -07001283 Flag("-J--add-opens=java.base/java.util=ALL-UNNAMED").
Colin Cross33961b52019-07-11 11:01:22 -07001284 FlagWithArg("-encoding ", "UTF-8").
Colin Cross1e743852019-10-28 11:37:20 -07001285 FlagWithArg("-source ", javaVersion.String()).
Colin Cross33961b52019-07-11 11:01:22 -07001286 FlagWithRspFileInputList("@", srcs).
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001287 FlagWithInput("@", srcJarList)
1288
1289 if javaHome := ctx.Config().Getenv("ANDROID_JAVA_HOME"); javaHome != "" {
1290 cmd.Implicit(android.PathForSource(ctx, javaHome))
1291 }
1292
1293 if sandbox {
1294 cmd.FlagWithOutput("--strict-input-files ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
1295 } else {
1296 cmd.FlagWithOutput("--strict-input-files:warn ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
1297 }
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001298
Colin Cross238c1f32020-06-07 16:58:18 -07001299 if implicitsRsp != nil {
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001300 cmd.FlagWithArg("--strict-input-files-exempt ", "@"+implicitsRsp.String())
1301 }
Colin Cross33961b52019-07-11 11:01:22 -07001302
1303 if len(bootclasspath) > 0 {
1304 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
Nan Zhang71bbe632018-09-17 14:32:21 -07001305 }
1306
Colin Cross33961b52019-07-11 11:01:22 -07001307 if len(classpath) > 0 {
1308 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
1309 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001310
Colin Cross33961b52019-07-11 11:01:22 -07001311 if len(sourcepaths) > 0 {
1312 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
1313 } else {
1314 cmd.FlagWithArg("-sourcepath ", `""`)
1315 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001316
Colin Cross33961b52019-07-11 11:01:22 -07001317 cmd.Flag("--no-banner").
1318 Flag("--color").
1319 Flag("--quiet").
Makoto Onuki0df103a2020-07-17 15:11:24 -07001320 Flag("--format=v2").
1321 FlagWithArg("--repeat-errors-max ", "10").
1322 FlagWithArg("--hide ", "UnresolvedImport")
Nan Zhang86d2d552018-08-09 15:33:27 -07001323
Colin Cross33961b52019-07-11 11:01:22 -07001324 return cmd
Nan Zhang71bbe632018-09-17 14:32:21 -07001325}
1326
Nan Zhang1598a9e2018-09-04 17:14:32 -07001327func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga40da042018-08-01 12:48:00 -07001328 deps := d.Javadoc.collectDeps(ctx)
1329
1330 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), sdkContext(d))
Nan Zhang581fd212018-01-10 16:06:12 -08001331
Colin Cross33961b52019-07-11 11:01:22 -07001332 // Create rule for metalava
Nan Zhanga40da042018-08-01 12:48:00 -07001333
Colin Cross33961b52019-07-11 11:01:22 -07001334 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang71bbe632018-09-17 14:32:21 -07001335
Colin Cross33961b52019-07-11 11:01:22 -07001336 rule := android.NewRuleBuilder()
Nan Zhanga40da042018-08-01 12:48:00 -07001337
Anton Hansson52ac73d2020-10-26 09:57:40 +00001338 if BoolDefault(d.properties.High_mem, false) {
1339 // This metalava run uses lots of memory, restrict the number of metalava jobs that can run in parallel.
1340 rule.HighMem()
1341 }
1342
Paul Duffin3ae29512020-04-08 18:18:03 +01001343 generateStubs := BoolDefault(d.properties.Generate_stubs, true)
1344 var stubsDir android.OptionalPath
1345 if generateStubs {
1346 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1347 stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "stubsDir"))
1348 rule.Command().Text("rm -rf").Text(stubsDir.String())
1349 rule.Command().Text("mkdir -p").Text(stubsDir.String())
1350 }
Nan Zhanga40da042018-08-01 12:48:00 -07001351
Colin Cross33961b52019-07-11 11:01:22 -07001352 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1353
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001354 implicitsRsp := android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"implicits.rsp")
1355
Colin Cross33961b52019-07-11 11:01:22 -07001356 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001357 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths, implicitsRsp,
1358 Bool(d.Javadoc.properties.Sandbox))
1359 cmd.Implicits(d.Javadoc.implicits)
Colin Cross33961b52019-07-11 11:01:22 -07001360
1361 d.stubsFlags(ctx, cmd, stubsDir)
1362
1363 d.annotationsFlags(ctx, cmd)
1364 d.inclusionAnnotationsFlags(ctx, cmd)
1365 d.apiLevelsAnnotationsFlags(ctx, cmd)
Nan Zhang71bbe632018-09-17 14:32:21 -07001366
Liz Kammer585cac22020-07-06 09:12:57 -07001367 if android.InList("--generate-documentation", d.Javadoc.args) {
Nan Zhang1598a9e2018-09-04 17:14:32 -07001368 // Currently Metalava have the ability to invoke Javadoc in a seperate process.
1369 // Pass "-nodocs" to suppress the Javadoc invocation when Metalava receives
1370 // "--generate-documentation" arg. This is not needed when Metalava removes this feature.
Liz Kammer585cac22020-07-06 09:12:57 -07001371 d.Javadoc.args = append(d.Javadoc.args, "-nodocs")
Nan Zhang79614d12018-04-19 18:03:39 -07001372 }
Colin Cross33961b52019-07-11 11:01:22 -07001373
Liz Kammer585cac22020-07-06 09:12:57 -07001374 cmd.Flag(strings.Join(d.Javadoc.args, " ")).Implicits(d.Javadoc.argFiles)
Colin Cross33961b52019-07-11 11:01:22 -07001375 for _, o := range d.Javadoc.properties.Out {
1376 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1377 }
1378
Makoto Onuki88b99052020-04-27 17:22:16 -07001379 // Add options for the other optional tasks: API-lint and check-released.
1380 // We generate separate timestamp files for them.
1381
1382 doApiLint := false
1383 doCheckReleased := false
1384
1385 // Add API lint options.
1386
Dan Willemsen9f435972020-05-28 15:28:00 -07001387 if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) {
Makoto Onuki88b99052020-04-27 17:22:16 -07001388 doApiLint = true
1389
1390 newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since)
1391 if newSince.Valid() {
1392 cmd.FlagWithInput("--api-lint ", newSince.Path())
1393 } else {
1394 cmd.Flag("--api-lint")
1395 }
1396 d.apiLintReport = android.PathForModuleOut(ctx, "api_lint_report.txt")
1397 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport) // TODO: Change to ":api-lint"
1398
Makoto Onuki194c43f2020-04-27 17:22:16 -07001399 // TODO(b/154317059): Clean up this whitelist by baselining and/or checking in last-released.
1400 if d.Name() != "android.car-system-stubs-docs" &&
Anton Hanssonb30f5932020-09-16 13:14:18 +01001401 d.Name() != "android.car-stubs-docs" {
Makoto Onuki194c43f2020-04-27 17:22:16 -07001402 cmd.Flag("--lints-as-errors")
1403 cmd.Flag("--warnings-as-errors") // Most lints are actually warnings.
1404 }
1405
Makoto Onuki88b99052020-04-27 17:22:16 -07001406 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
1407 updatedBaselineOutput := android.PathForModuleOut(ctx, "api_lint_baseline.txt")
1408 d.apiLintTimestamp = android.PathForModuleOut(ctx, "api_lint.timestamp")
1409
1410 // Note this string includes a special shell quote $' ... ', which decodes the "\n"s.
1411 // However, because $' ... ' doesn't expand environmental variables, we can't just embed
1412 // $PWD, so we have to terminate $'...', use "$PWD", then start $' ... ' again,
1413 // which is why we have '"$PWD"$' in it.
1414 //
1415 // TODO: metalava also has a slightly different message hardcoded. Should we unify this
1416 // message and metalava's one?
1417 msg := `$'` + // Enclose with $' ... '
1418 `************************************************************\n` +
1419 `Your API changes are triggering API Lint warnings or errors.\n` +
1420 `To make these errors go away, fix the code according to the\n` +
1421 `error and/or warning messages above.\n` +
1422 `\n` +
1423 `If it is not possible to do so, there are workarounds:\n` +
1424 `\n` +
1425 `1. You can suppress the errors with @SuppressLint("<id>")\n`
1426
1427 if baselineFile.Valid() {
1428 cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path())
1429 cmd.FlagWithOutput("--update-baseline:api-lint ", updatedBaselineOutput)
1430
1431 msg += fmt.Sprintf(``+
1432 `2. You can update the baseline by executing the following\n`+
1433 ` command:\n`+
Anton Hansson3361a292020-05-11 15:38:31 +01001434 ` cp \\\n`+
1435 ` "'"$PWD"$'/%s" \\\n`+
1436 ` "'"$PWD"$'/%s"\n`+
Makoto Onuki88b99052020-04-27 17:22:16 -07001437 ` To submit the revised baseline.txt to the main Android\n`+
1438 ` repository, you will need approval.\n`, updatedBaselineOutput, baselineFile.Path())
1439 } else {
1440 msg += fmt.Sprintf(``+
1441 `2. You can add a baseline file of existing lint failures\n`+
1442 ` to the build rule of %s.\n`, d.Name())
1443 }
1444 // Note the message ends with a ' (single quote), to close the $' ... ' .
1445 msg += `************************************************************\n'`
1446
1447 cmd.FlagWithArg("--error-message:api-lint ", msg)
1448 }
1449
1450 // Add "check released" options. (Detect incompatible API changes from the last public release)
1451
Dan Willemsen9f435972020-05-28 15:28:00 -07001452 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") {
Makoto Onuki88b99052020-04-27 17:22:16 -07001453 doCheckReleased = true
1454
1455 if len(d.Javadoc.properties.Out) > 0 {
1456 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1457 }
1458
1459 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1460 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
1461 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
1462 updatedBaselineOutput := android.PathForModuleOut(ctx, "last_released_baseline.txt")
1463
1464 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
1465
1466 cmd.FlagWithInput("--check-compatibility:api:released ", apiFile)
1467 cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile)
1468
1469 if baselineFile.Valid() {
1470 cmd.FlagWithInput("--baseline:compatibility:released ", baselineFile.Path())
1471 cmd.FlagWithOutput("--update-baseline:compatibility:released ", updatedBaselineOutput)
1472 }
1473
1474 // Note this string includes quote ($' ... '), which decodes the "\n"s.
1475 msg := `$'\n******************************\n` +
1476 `You have tried to change the API from what has been previously released in\n` +
1477 `an SDK. Please fix the errors listed above.\n` +
1478 `******************************\n'`
1479
1480 cmd.FlagWithArg("--error-message:compatibility:released ", msg)
1481 }
1482
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001483 impRule := android.NewRuleBuilder()
1484 impCmd := impRule.Command()
Liz Kammer20ebfb42020-07-28 11:32:07 -07001485 // An action that copies the ninja generated rsp file to a new location. This allows us to
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001486 // add a large number of inputs to a file without exceeding bash command length limits (which
1487 // would happen if we use the WriteFile rule). The cp is needed because RuleBuilder sets the
1488 // rsp file to be ${output}.rsp.
1489 impCmd.Text("cp").FlagWithRspFileInputList("", cmd.GetImplicits()).Output(implicitsRsp)
1490 impRule.Build(pctx, ctx, "implicitsGen", "implicits generation")
1491 cmd.Implicit(implicitsRsp)
1492
Paul Duffin3ae29512020-04-08 18:18:03 +01001493 if generateStubs {
1494 rule.Command().
1495 BuiltTool(ctx, "soong_zip").
1496 Flag("-write_if_changed").
1497 Flag("-jar").
1498 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
1499 FlagWithArg("-C ", stubsDir.String()).
1500 FlagWithArg("-D ", stubsDir.String())
1501 }
Jerome Gaillard0f599032019-10-10 19:29:11 +01001502
1503 if Bool(d.properties.Write_sdk_values) {
1504 d.metadataZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-metadata.zip")
1505 rule.Command().
1506 BuiltTool(ctx, "soong_zip").
1507 Flag("-write_if_changed").
1508 Flag("-d").
1509 FlagWithOutput("-o ", d.metadataZip).
1510 FlagWithArg("-C ", d.metadataDir.String()).
1511 FlagWithArg("-D ", d.metadataDir.String())
1512 }
1513
Makoto Onuki88b99052020-04-27 17:22:16 -07001514 // TODO: We don't really need two separate API files, but this is a reminiscence of how
1515 // we used to run metalava separately for API lint and the "last_released" check. Unify them.
1516 if doApiLint {
1517 rule.Command().Text("touch").Output(d.apiLintTimestamp)
1518 }
1519 if doCheckReleased {
1520 rule.Command().Text("touch").Output(d.checkLastReleasedApiTimestamp)
1521 }
1522
Colin Cross33961b52019-07-11 11:01:22 -07001523 rule.Restat()
1524
1525 zipSyncCleanupCmd(rule, srcJarDir)
1526
Makoto Onuki88b99052020-04-27 17:22:16 -07001527 rule.Build(pctx, ctx, "metalava", "metalava merged")
Adrian Roos075eedc2019-10-10 12:07:03 +02001528
Dan Willemsen9f435972020-05-28 15:28:00 -07001529 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") {
Colin Cross33961b52019-07-11 11:01:22 -07001530
1531 if len(d.Javadoc.properties.Out) > 0 {
1532 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1533 }
1534
1535 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1536 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Adrian Roos14f75a92019-08-12 17:54:09 +02001537 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
Makoto Onuki5405a732020-04-16 17:02:40 -07001538
1539 if baselineFile.Valid() {
Makoto Onuki88b99052020-04-27 17:22:16 -07001540 ctx.PropertyErrorf("baseline_file", "current API check can't have a baseline file. (module %s)", ctx.ModuleName())
Makoto Onuki5405a732020-04-16 17:02:40 -07001541 }
Nan Zhang61819ce2018-05-04 18:49:16 -07001542
Nan Zhang2760dfc2018-08-24 17:32:54 +00001543 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Nan Zhang2760dfc2018-08-24 17:32:54 +00001544
Colin Cross33961b52019-07-11 11:01:22 -07001545 rule := android.NewRuleBuilder()
1546
Makoto Onuki5405a732020-04-16 17:02:40 -07001547 // Diff command line.
Makoto Onuki88b99052020-04-27 17:22:16 -07001548 // -F matches the closest "opening" line, such as "package android {"
1549 // and " public class Intent {".
Makoto Onuki5405a732020-04-16 17:02:40 -07001550 diff := `diff -u -F '{ *$'`
1551
Colin Cross33961b52019-07-11 11:01:22 -07001552 rule.Command().Text("( true")
Makoto Onuki5405a732020-04-16 17:02:40 -07001553 rule.Command().
1554 Text(diff).
1555 Input(apiFile).Input(d.apiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001556
Makoto Onuki5405a732020-04-16 17:02:40 -07001557 rule.Command().
1558 Text(diff).
1559 Input(removedApiFile).Input(d.removedApiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001560
1561 msg := fmt.Sprintf(`\n******************************\n`+
1562 `You have tried to change the API from what has been previously approved.\n\n`+
1563 `To make these errors go away, you have two choices:\n`+
Makoto Onuki5405a732020-04-16 17:02:40 -07001564 ` 1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)\n`+
1565 ` to the new methods, etc. shown in the above diff.\n\n`+
1566 ` 2. You can update current.txt and/or removed.txt by executing the following command:\n`+
Colin Cross33961b52019-07-11 11:01:22 -07001567 ` make %s-update-current-api\n\n`+
1568 ` To submit the revised current.txt to the main Android repository,\n`+
1569 ` you will need approval.\n`+
1570 `******************************\n`, ctx.ModuleName())
1571
1572 rule.Command().
1573 Text("touch").Output(d.checkCurrentApiTimestamp).
1574 Text(") || (").
1575 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1576 Text("; exit 38").
1577 Text(")")
1578
Makoto Onuki5405a732020-04-16 17:02:40 -07001579 rule.Build(pctx, ctx, "metalavaCurrentApiCheck", "check current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001580
1581 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001582
1583 // update API rule
1584 rule = android.NewRuleBuilder()
1585
1586 rule.Command().Text("( true")
1587
1588 rule.Command().
1589 Text("cp").Flag("-f").
1590 Input(d.apiFile).Flag(apiFile.String())
1591
1592 rule.Command().
1593 Text("cp").Flag("-f").
1594 Input(d.removedApiFile).Flag(removedApiFile.String())
1595
1596 msg = "failed to update public API"
1597
1598 rule.Command().
1599 Text("touch").Output(d.updateCurrentApiTimestamp).
1600 Text(") || (").
1601 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1602 Text("; exit 38").
1603 Text(")")
1604
1605 rule.Build(pctx, ctx, "metalavaCurrentApiUpdate", "update current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001606 }
Nan Zhanga40da042018-08-01 12:48:00 -07001607
Pete Gillin581d6082018-10-22 15:55:04 +01001608 if String(d.properties.Check_nullability_warnings) != "" {
1609 if d.nullabilityWarningsFile == nil {
1610 ctx.PropertyErrorf("check_nullability_warnings",
1611 "Cannot specify check_nullability_warnings unless validating nullability")
1612 }
Colin Cross33961b52019-07-11 11:01:22 -07001613
1614 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
1615
Pete Gillin581d6082018-10-22 15:55:04 +01001616 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "check_nullability_warnings.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001617
Pete Gillin581d6082018-10-22 15:55:04 +01001618 msg := fmt.Sprintf(`\n******************************\n`+
1619 `The warnings encountered during nullability annotation validation did\n`+
1620 `not match the checked in file of expected warnings. The diffs are shown\n`+
1621 `above. You have two options:\n`+
1622 ` 1. Resolve the differences by editing the nullability annotations.\n`+
1623 ` 2. Update the file of expected warnings by running:\n`+
1624 ` cp %s %s\n`+
1625 ` and submitting the updated file as part of your change.`,
1626 d.nullabilityWarningsFile, checkNullabilityWarnings)
Colin Cross33961b52019-07-11 11:01:22 -07001627
1628 rule := android.NewRuleBuilder()
1629
1630 rule.Command().
1631 Text("(").
1632 Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile).
1633 Text("&&").
1634 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
1635 Text(") || (").
1636 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1637 Text("; exit 38").
1638 Text(")")
1639
1640 rule.Build(pctx, ctx, "nullabilityWarningsCheck", "nullability warnings check")
Pete Gillin581d6082018-10-22 15:55:04 +01001641 }
Nan Zhang581fd212018-01-10 16:06:12 -08001642}
Dan Willemsencc090972018-02-26 14:33:31 -08001643
Nan Zhanga40da042018-08-01 12:48:00 -07001644//
Nan Zhangf4936b02018-08-01 15:00:28 -07001645// Exported Droiddoc Directory
Nan Zhanga40da042018-08-01 12:48:00 -07001646//
Dan Willemsencc090972018-02-26 14:33:31 -08001647var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
Nan Zhangf4936b02018-08-01 15:00:28 -07001648var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
Pete Gillin77167902018-09-19 18:16:26 +01001649var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
Nan Zhang9c69a122018-08-22 10:22:08 -07001650var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
Dan Willemsencc090972018-02-26 14:33:31 -08001651
Nan Zhangf4936b02018-08-01 15:00:28 -07001652type ExportedDroiddocDirProperties struct {
1653 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -08001654 Path *string
1655}
1656
Nan Zhangf4936b02018-08-01 15:00:28 -07001657type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -08001658 android.ModuleBase
1659
Nan Zhangf4936b02018-08-01 15:00:28 -07001660 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -08001661
1662 deps android.Paths
1663 dir android.Path
1664}
1665
Colin Crossa3002fc2019-07-08 16:48:04 -07001666// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -07001667func ExportedDroiddocDirFactory() android.Module {
1668 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -08001669 module.AddProperties(&module.properties)
1670 android.InitAndroidModule(module)
1671 return module
1672}
1673
Nan Zhangf4936b02018-08-01 15:00:28 -07001674func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -08001675
Nan Zhangf4936b02018-08-01 15:00:28 -07001676func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -08001677 path := String(d.properties.Path)
1678 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -08001679 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Dan Willemsencc090972018-02-26 14:33:31 -08001680}
Nan Zhangb2b33de2018-02-23 11:18:47 -08001681
1682//
1683// Defaults
1684//
1685type DocDefaults struct {
1686 android.ModuleBase
1687 android.DefaultsModuleBase
1688}
1689
Nan Zhangb2b33de2018-02-23 11:18:47 -08001690func DocDefaultsFactory() android.Module {
1691 module := &DocDefaults{}
1692
1693 module.AddProperties(
1694 &JavadocProperties{},
1695 &DroiddocProperties{},
1696 )
1697
1698 android.InitDefaultsModule(module)
1699
1700 return module
1701}
Nan Zhang1598a9e2018-09-04 17:14:32 -07001702
1703func StubsDefaultsFactory() android.Module {
1704 module := &DocDefaults{}
1705
1706 module.AddProperties(
1707 &JavadocProperties{},
1708 &DroidstubsProperties{},
1709 )
1710
1711 android.InitDefaultsModule(module)
1712
1713 return module
1714}
Colin Cross33961b52019-07-11 11:01:22 -07001715
1716func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
1717 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
1718
1719 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1720 rule.Command().Text("mkdir -p").Text(srcJarDir.String())
1721 srcJarList := srcJarDir.Join(ctx, "list")
1722
1723 rule.Temporary(srcJarList)
1724
1725 rule.Command().BuiltTool(ctx, "zipsync").
1726 FlagWithArg("-d ", srcJarDir.String()).
1727 FlagWithOutput("-l ", srcJarList).
1728 FlagWithArg("-f ", `"*.java"`).
1729 Inputs(srcJars)
1730
1731 return srcJarList
1732}
1733
1734func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
1735 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1736}
Paul Duffin91547182019-11-12 19:39:36 +00001737
1738var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
1739
1740type PrebuiltStubsSourcesProperties struct {
1741 Srcs []string `android:"path"`
1742}
1743
1744type PrebuiltStubsSources struct {
1745 android.ModuleBase
1746 android.DefaultableModuleBase
1747 prebuilt android.Prebuilt
1748 android.SdkBase
1749
1750 properties PrebuiltStubsSourcesProperties
1751
Paul Duffin91547182019-11-12 19:39:36 +00001752 stubsSrcJar android.ModuleOutPath
1753}
1754
Paul Duffin9b478b02019-12-10 13:41:51 +00001755func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) {
1756 switch tag {
1757 case "":
1758 return android.Paths{p.stubsSrcJar}, nil
1759 default:
1760 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1761 }
1762}
1763
Paul Duffin0f8faff2020-05-20 16:18:00 +01001764func (d *PrebuiltStubsSources) StubsSrcJar() android.Path {
1765 return d.stubsSrcJar
1766}
1767
Paul Duffin91547182019-11-12 19:39:36 +00001768func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin9b478b02019-12-10 13:41:51 +00001769 p.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1770
Paul Duffin1a393322020-11-18 16:36:47 +00001771 if len(p.properties.Srcs) != 1 {
1772 ctx.PropertyErrorf("srcs", "must only specify one directory path, contains %d paths", len(p.properties.Srcs))
1773 return
1774 }
1775
1776 localSrcDir := p.properties.Srcs[0]
1777 // Although PathForModuleSrc can return nil if either the path doesn't exist or
1778 // the path components are invalid it won't in this case because no components
1779 // are specified and the module directory must exist in order to get this far.
1780 srcDir := android.PathForModuleSrc(ctx).(android.SourcePath).Join(ctx, localSrcDir)
1781
1782 // Glob the contents of the directory just in case the directory does not exist.
1783 srcGlob := localSrcDir + "/**/*"
1784 srcPaths := android.PathsForModuleSrc(ctx, []string{srcGlob})
Paul Duffin9b478b02019-12-10 13:41:51 +00001785
1786 rule := android.NewRuleBuilder()
Paul Duffin1a393322020-11-18 16:36:47 +00001787 rule.Command().
Paul Duffin9b478b02019-12-10 13:41:51 +00001788 BuiltTool(ctx, "soong_zip").
1789 Flag("-write_if_changed").
1790 Flag("-jar").
Paul Duffin1a393322020-11-18 16:36:47 +00001791 FlagWithOutput("-o ", p.stubsSrcJar).
1792 FlagWithArg("-C ", srcDir.String()).
1793 FlagWithRspFileInputList("-r ", srcPaths)
Paul Duffin9b478b02019-12-10 13:41:51 +00001794
1795 rule.Restat()
1796
1797 rule.Build(pctx, ctx, "zip src", "Create srcjar from prebuilt source")
Paul Duffin91547182019-11-12 19:39:36 +00001798}
1799
1800func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
1801 return &p.prebuilt
1802}
1803
1804func (p *PrebuiltStubsSources) Name() string {
1805 return p.prebuilt.Name(p.ModuleBase.Name())
1806}
1807
Paul Duffin91547182019-11-12 19:39:36 +00001808// prebuilt_stubs_sources imports a set of java source files as if they were
1809// generated by droidstubs.
1810//
1811// By default, a prebuilt_stubs_sources has a single variant that expects a
1812// set of `.java` files generated by droidstubs.
1813//
1814// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1815// for host modules.
1816//
1817// Intended only for use by sdk snapshots.
1818func PrebuiltStubsSourcesFactory() android.Module {
1819 module := &PrebuiltStubsSources{}
1820
1821 module.AddProperties(&module.properties)
1822
1823 android.InitPrebuiltModule(module, &module.properties.Srcs)
1824 android.InitSdkAwareModule(module)
1825 InitDroiddocModule(module, android.HostAndDeviceSupported)
1826 return module
1827}