blob: da13c621b95229e88de99b4ed2056a29981ff6ec [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
Adrian Roos075eedc2019-10-10 12:07:03 +0200195 Api_lint struct {
196 Enabled *bool
197
198 // If set, performs api_lint on any new APIs not found in the given signature file
199 New_since *string `android:"path"`
200
201 // If not blank, path to the baseline txt file for approved API lint violations.
202 Baseline_file *string `android:"path"`
203 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700204 }
Nan Zhang79614d12018-04-19 18:03:39 -0700205
206 // user can specify the version of previous released API file in order to do compatibility check.
Colin Cross27b922f2019-03-04 22:35:41 -0800207 Previous_api *string `android:"path"`
Nan Zhang79614d12018-04-19 18:03:39 -0700208
209 // is set to true, Metalava will allow framework SDK to contain annotations.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700210 Annotations_enabled *bool
Nan Zhang79614d12018-04-19 18:03:39 -0700211
Pete Gillin77167902018-09-19 18:16:26 +0100212 // 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 -0700213 Merge_annotations_dirs []string
Nan Zhang86d2d552018-08-09 15:33:27 -0700214
Pete Gillin77167902018-09-19 18:16:26 +0100215 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
216 Merge_inclusion_annotations_dirs []string
217
Pete Gillinc382a562018-11-14 18:45:46 +0000218 // a file containing a list of classes to do nullability validation for.
219 Validate_nullability_from_list *string
220
Pete Gillin581d6082018-10-22 15:55:04 +0100221 // a file containing expected warnings produced by validation of nullability annotations.
222 Check_nullability_warnings *string
223
Nan Zhang1598a9e2018-09-04 17:14:32 -0700224 // if set to true, allow Metalava to generate doc_stubs source files. Defaults to false.
225 Create_doc_stubs *bool
Nan Zhang9c69a122018-08-22 10:22:08 -0700226
Paul Duffin6877e6d2020-09-25 19:59:14 +0100227 // if set to true, cause Metalava to output Javadoc comments in the stubs source files. Defaults to false.
228 // Has no effect if create_doc_stubs: true.
229 Output_javadoc_comments *bool
230
Paul Duffin3ae29512020-04-08 18:18:03 +0100231 // if set to false then do not write out stubs. Defaults to true.
232 //
233 // TODO(b/146727827): Remove capability when we do not need to generate stubs and API separately.
234 Generate_stubs *bool
235
Anton Hansson52ac73d2020-10-26 09:57:40 +0000236 // if set to true, provides a hint to the build system that this rule uses a lot of memory,
237 // whicih can be used for scheduling purposes
238 High_mem *bool
239
Nan Zhang9c69a122018-08-22 10:22:08 -0700240 // is set to true, Metalava will allow framework SDK to contain API levels annotations.
241 Api_levels_annotations_enabled *bool
242
243 // the dirs which Metalava extracts API levels annotations from.
244 Api_levels_annotations_dirs []string
245
Liz Kammer3d894b72020-08-04 09:55:13 -0700246 // the filename which Metalava extracts API levels annotations from. Defaults to android.jar.
247 Api_levels_jar_filename *string
248
Nan Zhang9c69a122018-08-22 10:22:08 -0700249 // if set to true, collect the values used by the Dev tools and
250 // write them in files packaged with the SDK. Defaults to false.
251 Write_sdk_values *bool
Nan Zhang581fd212018-01-10 16:06:12 -0800252}
253
Nan Zhanga40da042018-08-01 12:48:00 -0700254//
255// Common flags passed down to build rule
256//
257type droiddocBuilderFlags struct {
Nan Zhang86d2d552018-08-09 15:33:27 -0700258 bootClasspathArgs string
259 classpathArgs string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700260 sourcepathArgs string
Nan Zhang86d2d552018-08-09 15:33:27 -0700261 dokkaClasspathArgs string
262 aidlFlags string
Colin Cross3047fa22019-04-18 10:56:44 -0700263 aidlDeps android.Paths
Nan Zhanga40da042018-08-01 12:48:00 -0700264
Nan Zhanga40da042018-08-01 12:48:00 -0700265 doclavaStubsFlags string
Nan Zhang86d2d552018-08-09 15:33:27 -0700266 doclavaDocsFlags string
Nan Zhanga40da042018-08-01 12:48:00 -0700267 postDoclavaCmds string
Nan Zhanga40da042018-08-01 12:48:00 -0700268}
269
270func InitDroiddocModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
271 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
272 android.InitDefaultableModule(module)
273}
274
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200275func apiCheckEnabled(ctx android.ModuleContext, apiToCheck ApiToCheck, apiVersionTag string) bool {
276 if ctx.Config().IsEnvTrue("WITHOUT_CHECK_API") {
277 return false
278 } else if String(apiToCheck.Api_file) != "" && String(apiToCheck.Removed_api_file) != "" {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700279 return true
280 } else if String(apiToCheck.Api_file) != "" {
281 panic("for " + apiVersionTag + " removed_api_file has to be non-empty!")
282 } else if String(apiToCheck.Removed_api_file) != "" {
283 panic("for " + apiVersionTag + " api_file has to be non-empty!")
284 }
285
286 return false
287}
288
Paul Duffin3d1248c2020-04-09 00:10:17 +0100289// Used by xsd_config
Nan Zhang1598a9e2018-09-04 17:14:32 -0700290type ApiFilePath interface {
291 ApiFilePath() android.Path
292}
293
Paul Duffin0f8faff2020-05-20 16:18:00 +0100294type ApiStubsSrcProvider interface {
295 StubsSrcJar() android.Path
296}
297
Paul Duffin3d1248c2020-04-09 00:10:17 +0100298// Provider of information about API stubs, used by java_sdk_library.
299type ApiStubsProvider interface {
300 ApiFilePath
Paul Duffin1fd005d2020-04-09 01:08:11 +0100301 RemovedApiFilePath() android.Path
Paul Duffin0f8faff2020-05-20 16:18:00 +0100302
303 ApiStubsSrcProvider
Paul Duffin3d1248c2020-04-09 00:10:17 +0100304}
305
Nan Zhanga40da042018-08-01 12:48:00 -0700306//
307// Javadoc
308//
Nan Zhang581fd212018-01-10 16:06:12 -0800309type Javadoc struct {
310 android.ModuleBase
311 android.DefaultableModuleBase
312
313 properties JavadocProperties
314
315 srcJars android.Paths
316 srcFiles android.Paths
317 sourcepaths android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700318 argFiles android.Paths
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400319 implicits android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700320
Liz Kammer585cac22020-07-06 09:12:57 -0700321 args []string
Nan Zhang581fd212018-01-10 16:06:12 -0800322
Nan Zhangccff0f72018-03-08 17:26:16 -0800323 docZip android.WritablePath
324 stubsSrcJar android.WritablePath
Nan Zhang581fd212018-01-10 16:06:12 -0800325}
326
Colin Cross41955e82019-05-29 14:40:35 -0700327func (j *Javadoc) OutputFiles(tag string) (android.Paths, error) {
328 switch tag {
329 case "":
330 return android.Paths{j.stubsSrcJar}, nil
Colin Crosse68e5542019-08-12 13:11:40 -0700331 case ".docs.zip":
332 return android.Paths{j.docZip}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700333 default:
334 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
335 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800336}
337
Colin Crossa3002fc2019-07-08 16:48:04 -0700338// javadoc converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800339func JavadocFactory() android.Module {
340 module := &Javadoc{}
341
342 module.AddProperties(&module.properties)
343
344 InitDroiddocModule(module, android.HostAndDeviceSupported)
345 return module
346}
347
Colin Crossa3002fc2019-07-08 16:48:04 -0700348// javadoc_host converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800349func JavadocHostFactory() android.Module {
350 module := &Javadoc{}
351
352 module.AddProperties(&module.properties)
353
354 InitDroiddocModule(module, android.HostSupported)
355 return module
356}
357
Colin Cross41955e82019-05-29 14:40:35 -0700358var _ android.OutputFileProducer = (*Javadoc)(nil)
Nan Zhang581fd212018-01-10 16:06:12 -0800359
Jiyong Park6a927c42020-01-21 02:03:43 +0900360func (j *Javadoc) sdkVersion() sdkSpec {
361 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700362}
363
Paul Duffine25c6442019-10-11 13:50:28 +0100364func (j *Javadoc) systemModules() string {
365 return proptools.String(j.properties.System_modules)
366}
367
Jiyong Park6a927c42020-01-21 02:03:43 +0900368func (j *Javadoc) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700369 return j.sdkVersion()
370}
371
Jiyong Park6a927c42020-01-21 02:03:43 +0900372func (j *Javadoc) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700373 return j.sdkVersion()
374}
375
Nan Zhang581fd212018-01-10 16:06:12 -0800376func (j *Javadoc) addDeps(ctx android.BottomUpMutatorContext) {
377 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100378 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Pete Gilline3d44b22020-06-29 11:28:51 +0100379 if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700380 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100381 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700382 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Pete Gilline3d44b22020-06-29 11:28:51 +0100383 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
Nan Zhang581fd212018-01-10 16:06:12 -0800384 }
385 }
386
Colin Cross42d48b72018-08-29 14:10:52 -0700387 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800388}
389
Nan Zhanga40da042018-08-01 12:48:00 -0700390func (j *Javadoc) collectAidlFlags(ctx android.ModuleContext, deps deps) droiddocBuilderFlags {
391 var flags droiddocBuilderFlags
Jiyong Park1e440682018-05-23 18:42:04 +0900392
Colin Cross3047fa22019-04-18 10:56:44 -0700393 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Jiyong Park1e440682018-05-23 18:42:04 +0900394
395 return flags
396}
397
398func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700399 aidlIncludeDirs android.Paths) (string, android.Paths) {
Jiyong Park1e440682018-05-23 18:42:04 +0900400
401 aidlIncludes := android.PathsForModuleSrc(ctx, j.properties.Aidl.Local_include_dirs)
402 aidlIncludes = append(aidlIncludes, android.PathsForSource(ctx, j.properties.Aidl.Include_dirs)...)
403
404 var flags []string
Colin Cross3047fa22019-04-18 10:56:44 -0700405 var deps android.Paths
406
Jiyong Park1e440682018-05-23 18:42:04 +0900407 if aidlPreprocess.Valid() {
408 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700409 deps = append(deps, aidlPreprocess.Path())
Jiyong Park1e440682018-05-23 18:42:04 +0900410 } else {
411 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
412 }
413
414 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
415 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
416 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
417 flags = append(flags, "-I"+src.String())
418 }
419
Colin Cross3047fa22019-04-18 10:56:44 -0700420 return strings.Join(flags, " "), deps
Jiyong Park1e440682018-05-23 18:42:04 +0900421}
422
Jiyong Parkd90d7412019-08-20 22:49:19 +0900423// TODO: remove the duplication between this and the one in gen.go
Jiyong Park1e440682018-05-23 18:42:04 +0900424func (j *Javadoc) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Nan Zhanga40da042018-08-01 12:48:00 -0700425 flags droiddocBuilderFlags) android.Paths {
Jiyong Park1e440682018-05-23 18:42:04 +0900426
427 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Crossc0806172019-06-14 18:51:47 -0700428 var aidlSrcs android.Paths
Jiyong Park1e440682018-05-23 18:42:04 +0900429
Jiyong Park1112c4c2019-08-16 21:12:10 +0900430 aidlIncludeFlags := genAidlIncludeFlags(srcFiles)
431
Jiyong Park1e440682018-05-23 18:42:04 +0900432 for _, srcFile := range srcFiles {
433 switch srcFile.Ext() {
434 case ".aidl":
Colin Crossc0806172019-06-14 18:51:47 -0700435 aidlSrcs = append(aidlSrcs, srcFile)
Jiyong Parkd90d7412019-08-20 22:49:19 +0900436 case ".logtags":
437 javaFile := genLogtags(ctx, srcFile)
438 outSrcFiles = append(outSrcFiles, javaFile)
Jiyong Park1e440682018-05-23 18:42:04 +0900439 default:
440 outSrcFiles = append(outSrcFiles, srcFile)
441 }
442 }
443
Colin Crossc0806172019-06-14 18:51:47 -0700444 // Process all aidl files together to support sharding them into one or more rules that produce srcjars.
445 if len(aidlSrcs) > 0 {
446 srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags+aidlIncludeFlags, flags.aidlDeps)
447 outSrcFiles = append(outSrcFiles, srcJarFiles...)
448 }
449
Jiyong Park1e440682018-05-23 18:42:04 +0900450 return outSrcFiles
451}
452
Nan Zhang581fd212018-01-10 16:06:12 -0800453func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
454 var deps deps
455
Colin Cross83bb3162018-06-25 15:48:06 -0700456 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800457 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700458 ctx.AddMissingDependencies(sdkDep.bootclasspath)
459 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Nan Zhang581fd212018-01-10 16:06:12 -0800460 } else if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700461 deps.bootClasspath = append(deps.bootClasspath, sdkDep.jars...)
Anton Hansson26bf49b2020-02-08 20:26:29 +0000462 deps.aidlPreprocess = sdkDep.aidl
463 } else {
464 deps.aidlPreprocess = sdkDep.aidl
Nan Zhang581fd212018-01-10 16:06:12 -0800465 }
466
467 ctx.VisitDirectDeps(func(module android.Module) {
468 otherName := ctx.OtherModuleName(module)
469 tag := ctx.OtherModuleDependencyTag(module)
470
Colin Cross2d24c1b2018-05-23 10:59:18 -0700471 switch tag {
472 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -0800473 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
474 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
475 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars...)
Paul Duffin83a2d962019-11-19 19:44:10 +0000476 } else if sm, ok := module.(SystemModulesProvider); ok {
Paul Duffine25c6442019-10-11 13:50:28 +0100477 // A system modules dependency has been added to the bootclasspath
478 // so add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +0000479 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700480 } else {
481 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
482 }
483 case libTag:
Colin Crossdcf71b22021-02-01 13:59:03 -0800484 if dep, ok := module.(SdkLibraryDependency); ok {
Paul Duffin649dadf2020-05-26 11:42:13 +0100485 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
Colin Crossdcf71b22021-02-01 13:59:03 -0800486 } else if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
487 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
488 deps.classpath = append(deps.classpath, dep.HeaderJars...)
489 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs...)
490 } else if dep, ok := module.(android.SourceFileProducer); ok {
Nan Zhang581fd212018-01-10 16:06:12 -0800491 checkProducesJars(ctx, dep)
492 deps.classpath = append(deps.classpath, dep.Srcs()...)
Colin Crossdcf71b22021-02-01 13:59:03 -0800493 } else {
Nan Zhang581fd212018-01-10 16:06:12 -0800494 ctx.ModuleErrorf("depends on non-java module %q", otherName)
495 }
Colin Cross6cef4812019-10-17 14:23:50 -0700496 case java9LibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -0800497 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
498 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
499 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars...)
500 } else {
Colin Cross6cef4812019-10-17 14:23:50 -0700501 ctx.ModuleErrorf("depends on non-java module %q", otherName)
502 }
Nan Zhang357466b2018-04-17 17:38:36 -0700503 case systemModulesTag:
504 if deps.systemModules != nil {
505 panic("Found two system module dependencies")
506 }
Paul Duffin83a2d962019-11-19 19:44:10 +0000507 sm := module.(SystemModulesProvider)
508 outputDir, outputDeps := sm.OutputDirAndDeps()
509 deps.systemModules = &systemModules{outputDir, outputDeps}
Nan Zhang581fd212018-01-10 16:06:12 -0800510 }
511 })
512 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
513 // may contain filegroup or genrule.
Colin Cross8a497952019-03-05 22:25:09 -0800514 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400515 j.implicits = append(j.implicits, srcFiles...)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900516
517 filterByPackage := func(srcs []android.Path, filterPackages []string) []android.Path {
518 if filterPackages == nil {
519 return srcs
520 }
521 filtered := []android.Path{}
522 for _, src := range srcs {
523 if src.Ext() != ".java" {
524 // Don't filter-out non-Java (=generated sources) by package names. This is not ideal,
525 // but otherwise metalava emits stub sources having references to the generated AIDL classes
526 // in filtered-out pacages (e.g. com.android.internal.*).
527 // TODO(b/141149570) We need to fix this by introducing default private constructors or
528 // fixing metalava to not emit constructors having references to unknown classes.
529 filtered = append(filtered, src)
530 continue
531 }
532 packageName := strings.ReplaceAll(filepath.Dir(src.Rel()), "/", ".")
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800533 if android.HasAnyPrefix(packageName, filterPackages) {
534 filtered = append(filtered, src)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900535 }
536 }
537 return filtered
538 }
539 srcFiles = filterByPackage(srcFiles, j.properties.Filter_packages)
540
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400541 // While metalava needs package html files, it does not need them to be explicit on the command
Anton Hansson746be9c2020-10-08 19:05:40 +0100542 // line. javadoc complains if it receives html files on the command line. The filter
543 // below excludes html files from the rsp file metalava. Note that the html
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400544 // files are still included as implicit inputs for successful remote execution and correct
545 // incremental builds.
546 filterHtml := func(srcs []android.Path) []android.Path {
547 filtered := []android.Path{}
548 for _, src := range srcs {
549 if src.Ext() == ".html" {
550 continue
551 }
552 filtered = append(filtered, src)
553 }
554 return filtered
555 }
556 srcFiles = filterHtml(srcFiles)
557
Liz Kammer585cac22020-07-06 09:12:57 -0700558 aidlFlags := j.collectAidlFlags(ctx, deps)
559 srcFiles = j.genSources(ctx, srcFiles, aidlFlags)
Nan Zhang581fd212018-01-10 16:06:12 -0800560
561 // srcs may depend on some genrule output.
562 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800563 j.srcJars = append(j.srcJars, deps.srcJars...)
564
Nan Zhang581fd212018-01-10 16:06:12 -0800565 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800566 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800567
Liz Kammere1ab2502020-09-10 15:29:25 +0000568 if len(j.srcFiles) > 0 {
569 j.sourcepaths = android.PathsForModuleSrc(ctx, []string{"."})
Nan Zhang581fd212018-01-10 16:06:12 -0800570 }
Nan Zhang581fd212018-01-10 16:06:12 -0800571
Colin Cross8a497952019-03-05 22:25:09 -0800572 j.argFiles = android.PathsForModuleSrc(ctx, j.properties.Arg_files)
Paul Duffin99e4a502019-02-11 15:38:42 +0000573 argFilesMap := map[string]string{}
574 argFileLabels := []string{}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700575
Paul Duffin99e4a502019-02-11 15:38:42 +0000576 for _, label := range j.properties.Arg_files {
Colin Cross8a497952019-03-05 22:25:09 -0800577 var paths = android.PathsForModuleSrc(ctx, []string{label})
Paul Duffin99e4a502019-02-11 15:38:42 +0000578 if _, exists := argFilesMap[label]; !exists {
579 argFilesMap[label] = strings.Join(paths.Strings(), " ")
580 argFileLabels = append(argFileLabels, label)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700581 } else {
582 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000583 label, argFilesMap[label], paths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700584 }
585 }
586
Liz Kammer585cac22020-07-06 09:12:57 -0700587 var argsPropertyName string
588 flags := make([]string, 0)
589 if j.properties.Args != nil && j.properties.Flags != nil {
590 ctx.PropertyErrorf("args", "flags is set. Cannot set args")
591 } else if args := proptools.String(j.properties.Args); args != "" {
592 flags = append(flags, args)
593 argsPropertyName = "args"
594 } else {
595 flags = append(flags, j.properties.Flags...)
596 argsPropertyName = "flags"
597 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700598
Liz Kammer585cac22020-07-06 09:12:57 -0700599 for _, flag := range flags {
600 args, err := android.Expand(flag, func(name string) (string, error) {
601 if strings.HasPrefix(name, "location ") {
602 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
603 if paths, ok := argFilesMap[label]; ok {
604 return paths, nil
605 } else {
606 return "", fmt.Errorf("unknown location label %q, expecting one of %q",
607 label, strings.Join(argFileLabels, ", "))
608 }
609 } else if name == "genDir" {
610 return android.PathForModuleGen(ctx).String(), nil
611 }
612 return "", fmt.Errorf("unknown variable '$(%s)'", name)
613 })
614
615 if err != nil {
616 ctx.PropertyErrorf(argsPropertyName, "%s", err.Error())
617 }
618 j.args = append(j.args, args)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700619 }
620
Nan Zhang581fd212018-01-10 16:06:12 -0800621 return deps
622}
623
624func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
625 j.addDeps(ctx)
626}
627
628func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
629 deps := j.collectDeps(ctx)
630
Colin Crossdaa4c672019-07-15 22:53:46 -0700631 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800632
Colin Crossdaa4c672019-07-15 22:53:46 -0700633 outDir := android.PathForModuleOut(ctx, "out")
634 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
635
636 j.stubsSrcJar = nil
637
Colin Crossf1a035e2020-11-16 17:32:30 -0800638 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossdaa4c672019-07-15 22:53:46 -0700639
640 rule.Command().Text("rm -rf").Text(outDir.String())
641 rule.Command().Text("mkdir -p").Text(outDir.String())
642
643 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, j.srcJars)
Nan Zhang357466b2018-04-17 17:38:36 -0700644
Colin Cross83bb3162018-06-25 15:48:06 -0700645 javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800646
Colin Crossdaa4c672019-07-15 22:53:46 -0700647 cmd := javadocSystemModulesCmd(ctx, rule, j.srcFiles, outDir, srcJarDir, srcJarList,
648 deps.systemModules, deps.classpath, j.sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800649
Colin Cross1e743852019-10-28 11:37:20 -0700650 cmd.FlagWithArg("-source ", javaVersion.String()).
Colin Crossdaa4c672019-07-15 22:53:46 -0700651 Flag("-J-Xmx1024m").
652 Flag("-XDignore.symbol.file").
653 Flag("-Xdoclint:none")
Nan Zhang581fd212018-01-10 16:06:12 -0800654
Colin Crossdaa4c672019-07-15 22:53:46 -0700655 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800656 BuiltTool("soong_zip").
Colin Crossdaa4c672019-07-15 22:53:46 -0700657 Flag("-write_if_changed").
658 Flag("-d").
659 FlagWithOutput("-o ", j.docZip).
660 FlagWithArg("-C ", outDir.String()).
661 FlagWithArg("-D ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700662
Colin Crossdaa4c672019-07-15 22:53:46 -0700663 rule.Restat()
664
665 zipSyncCleanupCmd(rule, srcJarDir)
666
Colin Crossf1a035e2020-11-16 17:32:30 -0800667 rule.Build("javadoc", "javadoc")
Nan Zhang581fd212018-01-10 16:06:12 -0800668}
669
Nan Zhanga40da042018-08-01 12:48:00 -0700670//
671// Droiddoc
672//
673type Droiddoc struct {
674 Javadoc
675
Liz Kammere1ab2502020-09-10 15:29:25 +0000676 properties DroiddocProperties
Nan Zhanga40da042018-08-01 12:48:00 -0700677}
678
Colin Crossa3002fc2019-07-08 16:48:04 -0700679// droiddoc converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700680func DroiddocFactory() android.Module {
681 module := &Droiddoc{}
682
683 module.AddProperties(&module.properties,
684 &module.Javadoc.properties)
685
686 InitDroiddocModule(module, android.HostAndDeviceSupported)
687 return module
688}
689
Colin Crossa3002fc2019-07-08 16:48:04 -0700690// droiddoc_host converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700691func DroiddocHostFactory() android.Module {
692 module := &Droiddoc{}
693
694 module.AddProperties(&module.properties,
695 &module.Javadoc.properties)
696
697 InitDroiddocModule(module, android.HostSupported)
698 return module
699}
700
Liz Kammere1ab2502020-09-10 15:29:25 +0000701func (d *Droiddoc) OutputFiles(tag string) (android.Paths, error) {
702 switch tag {
703 case "", ".docs.zip":
704 return android.Paths{d.Javadoc.docZip}, nil
705 default:
706 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
707 }
Nan Zhanga40da042018-08-01 12:48:00 -0700708}
709
Nan Zhang581fd212018-01-10 16:06:12 -0800710func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
711 d.Javadoc.addDeps(ctx)
712
Nan Zhang79614d12018-04-19 18:03:39 -0700713 if String(d.properties.Custom_template) != "" {
Dan Willemsencc090972018-02-26 14:33:31 -0800714 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
715 }
Nan Zhang581fd212018-01-10 16:06:12 -0800716}
717
Colin Crossab054432019-07-15 16:13:59 -0700718func (d *Droiddoc) doclavaDocsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, docletPath classpath) {
Colin Cross2a2e0db2020-02-21 16:55:46 -0800719 buildNumberFile := ctx.Config().BuildNumberFile(ctx)
Nan Zhang443fa522018-08-20 20:58:28 -0700720 // Droiddoc always gets "-source 1.8" because it doesn't support 1.9 sources. For modules with 1.9
721 // sources, droiddoc will get sources produced by metalava which will have already stripped out the
722 // 1.9 language features.
Colin Crossab054432019-07-15 16:13:59 -0700723 cmd.FlagWithArg("-source ", "1.8").
724 Flag("-J-Xmx1600m").
725 Flag("-J-XX:-OmitStackTraceInFastThrow").
726 Flag("-XDignore.symbol.file").
727 FlagWithArg("-doclet ", "com.google.doclava.Doclava").
728 FlagWithInputList("-docletpath ", docletPath.Paths(), ":").
Colin Cross2a2e0db2020-02-21 16:55:46 -0800729 FlagWithArg("-hdf page.build ", ctx.Config().BuildId()+"-$(cat "+buildNumberFile.String()+")").OrderOnly(buildNumberFile).
Elliott Hughes26bce342019-09-12 15:05:13 -0700730 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 -0700731
Nan Zhanga40da042018-08-01 12:48:00 -0700732 if String(d.properties.Custom_template) == "" {
733 // TODO: This is almost always droiddoc-templates-sdk
734 ctx.PropertyErrorf("custom_template", "must specify a template")
735 }
736
737 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
Nan Zhangf4936b02018-08-01 15:00:28 -0700738 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Crossab054432019-07-15 16:13:59 -0700739 cmd.FlagWithArg("-templatedir ", t.dir.String()).Implicits(t.deps)
Nan Zhanga40da042018-08-01 12:48:00 -0700740 } else {
Paul Duffin884363e2019-12-19 10:21:09 +0000741 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_exported_dir", ctx.OtherModuleName(m))
Nan Zhanga40da042018-08-01 12:48:00 -0700742 }
743 })
744
745 if len(d.properties.Html_dirs) > 0 {
Colin Crossab054432019-07-15 16:13:59 -0700746 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
747 cmd.FlagWithArg("-htmldir ", htmlDir.String()).
748 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[0], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700749 }
750
751 if len(d.properties.Html_dirs) > 1 {
Colin Crossab054432019-07-15 16:13:59 -0700752 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
753 cmd.FlagWithArg("-htmldir2 ", htmlDir2.String()).
754 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[1], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700755 }
756
757 if len(d.properties.Html_dirs) > 2 {
758 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
759 }
760
Colin Cross8a497952019-03-05 22:25:09 -0800761 knownTags := android.PathsForModuleSrc(ctx, d.properties.Knowntags)
Colin Crossab054432019-07-15 16:13:59 -0700762 cmd.FlagForEachInput("-knowntags ", knownTags)
Nan Zhanga40da042018-08-01 12:48:00 -0700763
Colin Crossab054432019-07-15 16:13:59 -0700764 cmd.FlagForEachArg("-hdf ", d.properties.Hdf)
Nan Zhanga40da042018-08-01 12:48:00 -0700765
766 if String(d.properties.Proofread_file) != "" {
767 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
Colin Crossab054432019-07-15 16:13:59 -0700768 cmd.FlagWithOutput("-proofread ", proofreadFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700769 }
770
771 if String(d.properties.Todo_file) != "" {
772 // tricky part:
773 // we should not compute full path for todo_file through PathForModuleOut().
774 // the non-standard doclet will get the full path relative to "-o".
Colin Crossab054432019-07-15 16:13:59 -0700775 cmd.FlagWithArg("-todo ", String(d.properties.Todo_file)).
776 ImplicitOutput(android.PathForModuleOut(ctx, String(d.properties.Todo_file)))
Nan Zhanga40da042018-08-01 12:48:00 -0700777 }
778
779 if String(d.properties.Resourcesdir) != "" {
780 // TODO: should we add files under resourcesDir to the implicits? It seems that
781 // resourcesDir is one sub dir of htmlDir
782 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
Colin Crossab054432019-07-15 16:13:59 -0700783 cmd.FlagWithArg("-resourcesdir ", resourcesDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700784 }
785
786 if String(d.properties.Resourcesoutdir) != "" {
787 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
Colin Crossab054432019-07-15 16:13:59 -0700788 cmd.FlagWithArg("-resourcesoutdir ", String(d.properties.Resourcesoutdir))
Nan Zhanga40da042018-08-01 12:48:00 -0700789 }
Nan Zhanga40da042018-08-01 12:48:00 -0700790}
791
Colin Crossab054432019-07-15 16:13:59 -0700792func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700793 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700794 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
795 rule.Command().Text("cp").
796 Input(staticDocIndexRedirect).
797 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700798 }
799
800 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700801 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
802 rule.Command().Text("cp").
803 Input(staticDocProperties).
804 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700805 }
Nan Zhanga40da042018-08-01 12:48:00 -0700806}
807
Colin Crossab054432019-07-15 16:13:59 -0700808func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700809 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700810
811 cmd := rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800812 BuiltTool("soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
Colin Crossab054432019-07-15 16:13:59 -0700813 Flag(config.JavacVmFlags).
814 FlagWithArg("-encoding ", "UTF-8").
Colin Cross70c47412021-03-12 17:48:14 -0800815 FlagWithRspFileInputList("@", android.PathForModuleOut(ctx, "javadoc.rsp"), srcs).
Colin Crossab054432019-07-15 16:13:59 -0700816 FlagWithInput("@", srcJarList)
817
Colin Crossab054432019-07-15 16:13:59 -0700818 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
819 // based stubs generation.
820 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
821 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
822 // the correct package name base path.
823 if len(sourcepaths) > 0 {
824 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
825 } else {
826 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
827 }
828
829 cmd.FlagWithArg("-d ", outDir.String()).
830 Flag("-quiet")
831
832 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700833}
834
Colin Crossdaa4c672019-07-15 22:53:46 -0700835func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
836 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
837 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
838
839 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
840
841 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
842 cmd.Flag(flag).Implicits(deps)
843
844 cmd.FlagWithArg("--patch-module ", "java.base=.")
845
846 if len(classpath) > 0 {
847 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
848 }
849
850 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700851}
852
Colin Crossdaa4c672019-07-15 22:53:46 -0700853func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
854 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
855 sourcepaths android.Paths) *android.RuleBuilderCommand {
856
857 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
858
859 if len(bootclasspath) == 0 && ctx.Device() {
860 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
861 // ensure java does not fall back to the default bootclasspath.
862 cmd.FlagWithArg("-bootclasspath ", `""`)
863 } else if len(bootclasspath) > 0 {
864 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
865 }
866
867 if len(classpath) > 0 {
868 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
869 }
870
871 return cmd
872}
873
Colin Crossab054432019-07-15 16:13:59 -0700874func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
875 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700876
Colin Crossab054432019-07-15 16:13:59 -0700877 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
878 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
879
880 return rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800881 BuiltTool("dokka").
Colin Crossab054432019-07-15 16:13:59 -0700882 Flag(config.JavacVmFlags).
883 Flag(srcJarDir.String()).
884 FlagWithInputList("-classpath ", dokkaClasspath, ":").
885 FlagWithArg("-format ", "dac").
886 FlagWithArg("-dacRoot ", "/reference/kotlin").
887 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700888}
889
890func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
891 deps := d.Javadoc.collectDeps(ctx)
892
Colin Crossdaa4c672019-07-15 22:53:46 -0700893 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Colin Crossdaa4c672019-07-15 22:53:46 -0700894
Nan Zhang1598a9e2018-09-04 17:14:32 -0700895 jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
896 doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700897
Colin Crossab054432019-07-15 16:13:59 -0700898 outDir := android.PathForModuleOut(ctx, "out")
899 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700900
Colin Crossf1a035e2020-11-16 17:32:30 -0800901 rule := android.NewRuleBuilder(pctx, ctx)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700902
Colin Crossab054432019-07-15 16:13:59 -0700903 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
904
905 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -0700906 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -0700907 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700908 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -0700909 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -0700910 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700911 }
912
Liz Kammer585cac22020-07-06 09:12:57 -0700913 cmd.Flag(strings.Join(d.Javadoc.args, " ")).Implicits(d.Javadoc.argFiles)
Colin Crossab054432019-07-15 16:13:59 -0700914
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000915 if d.properties.Compat_config != nil {
916 compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
917 cmd.FlagWithInput("-compatconfig ", compatConfig)
918 }
919
Colin Crossab054432019-07-15 16:13:59 -0700920 var desc string
921 if Bool(d.properties.Dokka_enabled) {
922 desc = "dokka"
923 } else {
924 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
925
926 for _, o := range d.Javadoc.properties.Out {
927 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
928 }
929
930 d.postDoclavaCmds(ctx, rule)
931 desc = "doclava"
932 }
933
934 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800935 BuiltTool("soong_zip").
Colin Crossab054432019-07-15 16:13:59 -0700936 Flag("-write_if_changed").
937 Flag("-d").
938 FlagWithOutput("-o ", d.docZip).
939 FlagWithArg("-C ", outDir.String()).
940 FlagWithArg("-D ", outDir.String())
941
Colin Crossab054432019-07-15 16:13:59 -0700942 rule.Restat()
943
944 zipSyncCleanupCmd(rule, srcJarDir)
945
Colin Crossf1a035e2020-11-16 17:32:30 -0800946 rule.Build("javadoc", desc)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700947}
948
949//
950// Droidstubs
951//
952type Droidstubs struct {
953 Javadoc
Paul Duffin91547182019-11-12 19:39:36 +0000954 android.SdkBase
Nan Zhang1598a9e2018-09-04 17:14:32 -0700955
Pete Gillin581d6082018-10-22 15:55:04 +0100956 properties DroidstubsProperties
957 apiFile android.WritablePath
958 apiXmlFile android.WritablePath
959 lastReleasedApiXmlFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +0100960 privateApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +0100961 removedApiFile android.WritablePath
962 removedDexApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +0100963 nullabilityWarningsFile android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -0700964
965 checkCurrentApiTimestamp android.WritablePath
966 updateCurrentApiTimestamp android.WritablePath
967 checkLastReleasedApiTimestamp android.WritablePath
Adrian Roos075eedc2019-10-10 12:07:03 +0200968 apiLintTimestamp android.WritablePath
Adrian Roos3b8f1cd2019-11-01 13:42:39 +0100969 apiLintReport android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -0700970
Pete Gillin581d6082018-10-22 15:55:04 +0100971 checkNullabilityWarningsTimestamp android.WritablePath
972
Nan Zhang1598a9e2018-09-04 17:14:32 -0700973 annotationsZip android.WritablePath
Nan Zhang9c69a122018-08-22 10:22:08 -0700974 apiVersionsXml android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -0700975
Paul Duffinbc0fe962020-10-13 15:04:02 +0100976 apiFilePath android.Path
977 removedApiFilePath android.Path
Nan Zhang71bbe632018-09-17 14:32:21 -0700978
Jerome Gaillard0f599032019-10-10 19:29:11 +0100979 metadataZip android.WritablePath
980 metadataDir android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -0700981}
982
Colin Crossa3002fc2019-07-08 16:48:04 -0700983// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
984// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to
985// a droiddoc module to generate documentation.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700986func DroidstubsFactory() android.Module {
987 module := &Droidstubs{}
988
989 module.AddProperties(&module.properties,
990 &module.Javadoc.properties)
991
992 InitDroiddocModule(module, android.HostAndDeviceSupported)
Paul Duffin91547182019-11-12 19:39:36 +0000993 android.InitSdkAwareModule(module)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700994 return module
995}
996
Colin Crossa3002fc2019-07-08 16:48:04 -0700997// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API
998// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be
999// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs
1000// module when symbols needed by the source files are provided by java_library_host modules.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001001func DroidstubsHostFactory() android.Module {
1002 module := &Droidstubs{}
1003
1004 module.AddProperties(&module.properties,
1005 &module.Javadoc.properties)
1006
1007 InitDroiddocModule(module, android.HostSupported)
1008 return module
1009}
1010
Colin Cross014489c2020-06-02 20:09:13 -07001011func (d *Droidstubs) OutputFiles(tag string) (android.Paths, error) {
1012 switch tag {
1013 case "":
1014 return android.Paths{d.stubsSrcJar}, nil
1015 case ".docs.zip":
1016 return android.Paths{d.docZip}, nil
Paul Duffind8aed4b2020-11-26 00:26:42 +00001017 case ".api.txt", android.DefaultDistTag:
1018 // This is the default dist path for dist properties that have no tag property.
Anton Hanssonecf54352020-10-02 17:44:34 +01001019 return android.Paths{d.apiFilePath}, nil
1020 case ".removed-api.txt":
Paul Duffinbc0fe962020-10-13 15:04:02 +01001021 return android.Paths{d.removedApiFilePath}, nil
Colin Cross014489c2020-06-02 20:09:13 -07001022 case ".annotations.zip":
1023 return android.Paths{d.annotationsZip}, nil
1024 case ".api_versions.xml":
1025 return android.Paths{d.apiVersionsXml}, nil
1026 default:
1027 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1028 }
1029}
1030
Nan Zhang1598a9e2018-09-04 17:14:32 -07001031func (d *Droidstubs) ApiFilePath() android.Path {
1032 return d.apiFilePath
1033}
1034
Paul Duffin1fd005d2020-04-09 01:08:11 +01001035func (d *Droidstubs) RemovedApiFilePath() android.Path {
Paul Duffinbc0fe962020-10-13 15:04:02 +01001036 return d.removedApiFilePath
Paul Duffin1fd005d2020-04-09 01:08:11 +01001037}
1038
Paul Duffin3d1248c2020-04-09 00:10:17 +01001039func (d *Droidstubs) StubsSrcJar() android.Path {
1040 return d.stubsSrcJar
1041}
1042
Nan Zhang1598a9e2018-09-04 17:14:32 -07001043func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
1044 d.Javadoc.addDeps(ctx)
1045
Nan Zhang1598a9e2018-09-04 17:14:32 -07001046 if len(d.properties.Merge_annotations_dirs) != 0 {
1047 for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
1048 ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
1049 }
1050 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001051
Pete Gillin77167902018-09-19 18:16:26 +01001052 if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
1053 for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
1054 ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
1055 }
1056 }
1057
Nan Zhang9c69a122018-08-22 10:22:08 -07001058 if len(d.properties.Api_levels_annotations_dirs) != 0 {
1059 for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
1060 ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
1061 }
1062 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001063}
1064
Paul Duffin3ae29512020-04-08 18:18:03 +01001065func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001066 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1067 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001068 String(d.properties.Api_filename) != "" {
Liz Kammer9ed79152020-08-24 15:56:32 -07001069 filename := proptools.StringDefault(d.properties.Api_filename, ctx.ModuleName()+"_api.txt")
1070 d.apiFile = android.PathForModuleOut(ctx, filename)
Colin Cross33961b52019-07-11 11:01:22 -07001071 cmd.FlagWithOutput("--api ", d.apiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001072 d.apiFilePath = d.apiFile
Paul Duffinbc0fe962020-10-13 15:04:02 +01001073 } else if sourceApiFile := proptools.String(d.properties.Check_api.Current.Api_file); sourceApiFile != "" {
1074 // If check api is disabled then make the source file available for export.
1075 d.apiFilePath = android.PathForModuleSrc(ctx, sourceApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001076 }
1077
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001078 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1079 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001080 String(d.properties.Removed_api_filename) != "" {
Liz Kammer9ed79152020-08-24 15:56:32 -07001081 filename := proptools.StringDefault(d.properties.Removed_api_filename, ctx.ModuleName()+"_removed.txt")
1082 d.removedApiFile = android.PathForModuleOut(ctx, filename)
Colin Cross33961b52019-07-11 11:01:22 -07001083 cmd.FlagWithOutput("--removed-api ", d.removedApiFile)
Paul Duffinbc0fe962020-10-13 15:04:02 +01001084 d.removedApiFilePath = d.removedApiFile
1085 } else if sourceRemovedApiFile := proptools.String(d.properties.Check_api.Current.Removed_api_file); sourceRemovedApiFile != "" {
1086 // If check api is disabled then make the source removed api file available for export.
1087 d.removedApiFilePath = android.PathForModuleSrc(ctx, sourceRemovedApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001088 }
1089
Nan Zhang1598a9e2018-09-04 17:14:32 -07001090 if String(d.properties.Removed_dex_api_filename) != "" {
1091 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001092 cmd.FlagWithOutput("--removed-dex-api ", d.removedDexApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001093 }
1094
Nan Zhang9c69a122018-08-22 10:22:08 -07001095 if Bool(d.properties.Write_sdk_values) {
Jerome Gaillard0f599032019-10-10 19:29:11 +01001096 d.metadataDir = android.PathForModuleOut(ctx, "metadata")
1097 cmd.FlagWithArg("--sdk-values ", d.metadataDir.String())
Nan Zhang9c69a122018-08-22 10:22:08 -07001098 }
1099
Paul Duffin3ae29512020-04-08 18:18:03 +01001100 if stubsDir.Valid() {
1101 if Bool(d.properties.Create_doc_stubs) {
1102 cmd.FlagWithArg("--doc-stubs ", stubsDir.String())
1103 } else {
1104 cmd.FlagWithArg("--stubs ", stubsDir.String())
Paul Duffin6877e6d2020-09-25 19:59:14 +01001105 if !Bool(d.properties.Output_javadoc_comments) {
1106 cmd.Flag("--exclude-documentation-from-stubs")
1107 }
Paul Duffin3ae29512020-04-08 18:18:03 +01001108 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001109 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001110}
1111
Colin Cross33961b52019-07-11 11:01:22 -07001112func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang1598a9e2018-09-04 17:14:32 -07001113 if Bool(d.properties.Annotations_enabled) {
Colin Cross33961b52019-07-11 11:01:22 -07001114 cmd.Flag("--include-annotations")
1115
Pete Gillinc382a562018-11-14 18:45:46 +00001116 validatingNullability :=
Liz Kammer585cac22020-07-06 09:12:57 -07001117 android.InList("--validate-nullability-from-merged-stubs", d.Javadoc.args) ||
Pete Gillinc382a562018-11-14 18:45:46 +00001118 String(d.properties.Validate_nullability_from_list) != ""
Paul Duffin13a9dd62019-11-04 10:26:47 +00001119
Pete Gillina262c052018-09-14 14:25:48 +01001120 migratingNullability := String(d.properties.Previous_api) != ""
Pete Gillina262c052018-09-14 14:25:48 +01001121 if migratingNullability {
Colin Cross8a497952019-03-05 22:25:09 -08001122 previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
Colin Cross33961b52019-07-11 11:01:22 -07001123 cmd.FlagWithInput("--migrate-nullness ", previousApi)
Pete Gillina262c052018-09-14 14:25:48 +01001124 }
Colin Cross33961b52019-07-11 11:01:22 -07001125
Pete Gillinc382a562018-11-14 18:45:46 +00001126 if s := String(d.properties.Validate_nullability_from_list); s != "" {
Colin Cross33961b52019-07-11 11:01:22 -07001127 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
Pete Gillinc382a562018-11-14 18:45:46 +00001128 }
Colin Cross33961b52019-07-11 11:01:22 -07001129
Pete Gillina262c052018-09-14 14:25:48 +01001130 if validatingNullability {
Pete Gillin581d6082018-10-22 15:55:04 +01001131 d.nullabilityWarningsFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_nullability_warnings.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001132 cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile)
Pete Gillina262c052018-09-14 14:25:48 +01001133 }
Nan Zhanga40da042018-08-01 12:48:00 -07001134
1135 d.annotationsZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"_annotations.zip")
Colin Cross33961b52019-07-11 11:01:22 -07001136 cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip)
Nan Zhangf4936b02018-08-01 15:00:28 -07001137
Anton Hansson9d7c3fb2020-05-21 10:11:31 +01001138 if len(d.properties.Merge_annotations_dirs) != 0 {
1139 d.mergeAnnoDirFlags(ctx, cmd)
Nan Zhanga40da042018-08-01 12:48:00 -07001140 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001141
Colin Cross33961b52019-07-11 11:01:22 -07001142 // TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
1143 cmd.FlagWithArg("--hide ", "HiddenTypedefConstant").
1144 FlagWithArg("--hide ", "SuperfluousPrefix").
1145 FlagWithArg("--hide ", "AnnotationExtraction")
1146 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001147}
1148
Colin Cross33961b52019-07-11 11:01:22 -07001149func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
1150 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
1151 if t, ok := m.(*ExportedDroiddocDir); ok {
1152 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
1153 } else {
1154 ctx.PropertyErrorf("merge_annotations_dirs",
1155 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1156 }
1157 })
1158}
1159
1160func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Pete Gillin77167902018-09-19 18:16:26 +01001161 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
1162 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Cross33961b52019-07-11 11:01:22 -07001163 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
Pete Gillin77167902018-09-19 18:16:26 +01001164 } else {
1165 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
1166 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1167 }
1168 })
Nan Zhanga40da042018-08-01 12:48:00 -07001169}
1170
Colin Cross33961b52019-07-11 11:01:22 -07001171func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001172 if !Bool(d.properties.Api_levels_annotations_enabled) {
1173 return
Nan Zhang9c69a122018-08-22 10:22:08 -07001174 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001175
1176 d.apiVersionsXml = android.PathForModuleOut(ctx, "api-versions.xml")
1177
1178 if len(d.properties.Api_levels_annotations_dirs) == 0 {
1179 ctx.PropertyErrorf("api_levels_annotations_dirs",
1180 "has to be non-empty if api levels annotations was enabled!")
1181 }
1182
1183 cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml)
1184 cmd.FlagWithInput("--apply-api-levels ", d.apiVersionsXml)
Dan Albert4f378d72020-07-23 17:32:15 -07001185 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion().String())
Liz Kammer3d894b72020-08-04 09:55:13 -07001186 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
1187
1188 filename := proptools.StringDefault(d.properties.Api_levels_jar_filename, "android.jar")
1189
1190 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
1191 if t, ok := m.(*ExportedDroiddocDir); ok {
1192 for _, dep := range t.deps {
1193 if strings.HasSuffix(dep.String(), filename) {
1194 cmd.Implicit(dep)
1195 }
1196 }
1197 cmd.FlagWithArg("--android-jar-pattern ", t.dir.String()+"/%/public/"+filename)
1198 } else {
1199 ctx.PropertyErrorf("api_levels_annotations_dirs",
1200 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
1201 }
1202 })
Nan Zhang9c69a122018-08-22 10:22:08 -07001203}
1204
Colin Cross1e743852019-10-28 11:37:20 -07001205func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths,
Colin Cross382ba062021-03-05 11:14:18 -08001206 srcJarList android.Path, bootclasspath, classpath classpath, sourcepaths android.Paths,
1207 implicitsRsp, homeDir android.WritablePath, sandbox bool) *android.RuleBuilderCommand {
1208 rule.Command().Text("rm -rf").Flag(homeDir.String())
1209 rule.Command().Text("mkdir -p").Flag(homeDir.String())
1210
Ramy Medhat427683c2020-04-30 03:08:37 -04001211 cmd := rule.Command()
Colin Cross382ba062021-03-05 11:14:18 -08001212 cmd.FlagWithArg("ANDROID_SDK_HOME=", homeDir.String())
1213
Ramy Medhat16f23a42020-09-03 01:29:49 -04001214 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_METALAVA") {
Ramy Medhat427683c2020-04-30 03:08:37 -04001215 rule.Remoteable(android.RemoteRuleSupports{RBE: true})
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001216 pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "metalava")
1217 execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
1218 labels := map[string]string{"type": "compile", "lang": "java", "compiler": "metalava"}
1219 if !sandbox {
1220 execStrategy = remoteexec.LocalExecStrategy
1221 labels["shallow"] = "true"
Ramy Medhat427683c2020-04-30 03:08:37 -04001222 }
Colin Cross382ba062021-03-05 11:14:18 -08001223 inputs := []string{
1224 ctx.Config().HostJavaToolPath(ctx, "metalava").String(),
1225 homeDir.String(),
1226 }
Ramy Medhat427683c2020-04-30 03:08:37 -04001227 if v := ctx.Config().Getenv("RBE_METALAVA_INPUTS"); v != "" {
1228 inputs = append(inputs, strings.Split(v, ",")...)
1229 }
1230 cmd.Text((&remoteexec.REParams{
Colin Cross382ba062021-03-05 11:14:18 -08001231 Labels: labels,
1232 ExecStrategy: execStrategy,
1233 Inputs: inputs,
1234 RSPFile: implicitsRsp.String(),
1235 ToolchainInputs: []string{config.JavaCmd(ctx).String()},
1236 Platform: map[string]string{remoteexec.PoolKey: pool},
1237 EnvironmentVariables: []string{"ANDROID_SDK_HOME"},
Ramy Medhat427683c2020-04-30 03:08:37 -04001238 }).NoVarTemplate(ctx.Config()))
1239 }
1240
Colin Crossf1a035e2020-11-16 17:32:30 -08001241 cmd.BuiltTool("metalava").
Colin Cross33961b52019-07-11 11:01:22 -07001242 Flag(config.JavacVmFlags).
Aurimas Liutikas4c5efde2020-09-21 11:18:06 -07001243 Flag("-J--add-opens=java.base/java.util=ALL-UNNAMED").
Colin Cross33961b52019-07-11 11:01:22 -07001244 FlagWithArg("-encoding ", "UTF-8").
Colin Cross1e743852019-10-28 11:37:20 -07001245 FlagWithArg("-source ", javaVersion.String()).
Colin Cross70c47412021-03-12 17:48:14 -08001246 FlagWithRspFileInputList("@", android.PathForModuleOut(ctx, "metalava.rsp"), srcs).
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001247 FlagWithInput("@", srcJarList)
1248
1249 if javaHome := ctx.Config().Getenv("ANDROID_JAVA_HOME"); javaHome != "" {
1250 cmd.Implicit(android.PathForSource(ctx, javaHome))
1251 }
1252
1253 if sandbox {
1254 cmd.FlagWithOutput("--strict-input-files ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
1255 } else {
1256 cmd.FlagWithOutput("--strict-input-files:warn ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
1257 }
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001258
Colin Cross238c1f32020-06-07 16:58:18 -07001259 if implicitsRsp != nil {
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001260 cmd.FlagWithArg("--strict-input-files-exempt ", "@"+implicitsRsp.String())
1261 }
Colin Cross33961b52019-07-11 11:01:22 -07001262
1263 if len(bootclasspath) > 0 {
1264 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
Nan Zhang71bbe632018-09-17 14:32:21 -07001265 }
1266
Colin Cross33961b52019-07-11 11:01:22 -07001267 if len(classpath) > 0 {
1268 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
1269 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001270
Colin Cross33961b52019-07-11 11:01:22 -07001271 if len(sourcepaths) > 0 {
1272 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
1273 } else {
1274 cmd.FlagWithArg("-sourcepath ", `""`)
1275 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001276
Colin Cross33961b52019-07-11 11:01:22 -07001277 cmd.Flag("--no-banner").
1278 Flag("--color").
1279 Flag("--quiet").
Makoto Onuki0df103a2020-07-17 15:11:24 -07001280 Flag("--format=v2").
1281 FlagWithArg("--repeat-errors-max ", "10").
1282 FlagWithArg("--hide ", "UnresolvedImport")
Nan Zhang86d2d552018-08-09 15:33:27 -07001283
Colin Cross33961b52019-07-11 11:01:22 -07001284 return cmd
Nan Zhang71bbe632018-09-17 14:32:21 -07001285}
1286
Nan Zhang1598a9e2018-09-04 17:14:32 -07001287func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga40da042018-08-01 12:48:00 -07001288 deps := d.Javadoc.collectDeps(ctx)
1289
1290 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), sdkContext(d))
Nan Zhang581fd212018-01-10 16:06:12 -08001291
Colin Cross33961b52019-07-11 11:01:22 -07001292 // Create rule for metalava
Nan Zhanga40da042018-08-01 12:48:00 -07001293
Colin Cross33961b52019-07-11 11:01:22 -07001294 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang71bbe632018-09-17 14:32:21 -07001295
Colin Crossf1a035e2020-11-16 17:32:30 -08001296 rule := android.NewRuleBuilder(pctx, ctx)
Nan Zhanga40da042018-08-01 12:48:00 -07001297
Anton Hansson52ac73d2020-10-26 09:57:40 +00001298 if BoolDefault(d.properties.High_mem, false) {
1299 // This metalava run uses lots of memory, restrict the number of metalava jobs that can run in parallel.
1300 rule.HighMem()
1301 }
1302
Paul Duffin3ae29512020-04-08 18:18:03 +01001303 generateStubs := BoolDefault(d.properties.Generate_stubs, true)
1304 var stubsDir android.OptionalPath
1305 if generateStubs {
1306 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1307 stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "stubsDir"))
1308 rule.Command().Text("rm -rf").Text(stubsDir.String())
1309 rule.Command().Text("mkdir -p").Text(stubsDir.String())
1310 }
Nan Zhanga40da042018-08-01 12:48:00 -07001311
Colin Cross33961b52019-07-11 11:01:22 -07001312 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1313
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001314 implicitsRsp := android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"implicits.rsp")
Colin Cross382ba062021-03-05 11:14:18 -08001315 homeDir := android.PathForModuleOut(ctx, "metalava-home")
Colin Cross33961b52019-07-11 11:01:22 -07001316 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
Colin Cross382ba062021-03-05 11:14:18 -08001317 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths, implicitsRsp, homeDir,
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001318 Bool(d.Javadoc.properties.Sandbox))
1319 cmd.Implicits(d.Javadoc.implicits)
Colin Cross33961b52019-07-11 11:01:22 -07001320
1321 d.stubsFlags(ctx, cmd, stubsDir)
1322
1323 d.annotationsFlags(ctx, cmd)
1324 d.inclusionAnnotationsFlags(ctx, cmd)
1325 d.apiLevelsAnnotationsFlags(ctx, cmd)
Nan Zhang71bbe632018-09-17 14:32:21 -07001326
Liz Kammer585cac22020-07-06 09:12:57 -07001327 if android.InList("--generate-documentation", d.Javadoc.args) {
Nan Zhang1598a9e2018-09-04 17:14:32 -07001328 // Currently Metalava have the ability to invoke Javadoc in a seperate process.
1329 // Pass "-nodocs" to suppress the Javadoc invocation when Metalava receives
1330 // "--generate-documentation" arg. This is not needed when Metalava removes this feature.
Liz Kammer585cac22020-07-06 09:12:57 -07001331 d.Javadoc.args = append(d.Javadoc.args, "-nodocs")
Nan Zhang79614d12018-04-19 18:03:39 -07001332 }
Colin Cross33961b52019-07-11 11:01:22 -07001333
Liz Kammer585cac22020-07-06 09:12:57 -07001334 cmd.Flag(strings.Join(d.Javadoc.args, " ")).Implicits(d.Javadoc.argFiles)
Colin Cross33961b52019-07-11 11:01:22 -07001335 for _, o := range d.Javadoc.properties.Out {
1336 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1337 }
1338
Makoto Onuki88b99052020-04-27 17:22:16 -07001339 // Add options for the other optional tasks: API-lint and check-released.
1340 // We generate separate timestamp files for them.
1341
1342 doApiLint := false
1343 doCheckReleased := false
1344
1345 // Add API lint options.
1346
Dan Willemsen9f435972020-05-28 15:28:00 -07001347 if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) {
Makoto Onuki88b99052020-04-27 17:22:16 -07001348 doApiLint = true
1349
1350 newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since)
1351 if newSince.Valid() {
1352 cmd.FlagWithInput("--api-lint ", newSince.Path())
1353 } else {
1354 cmd.Flag("--api-lint")
1355 }
1356 d.apiLintReport = android.PathForModuleOut(ctx, "api_lint_report.txt")
1357 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport) // TODO: Change to ":api-lint"
1358
Makoto Onuki194c43f2020-04-27 17:22:16 -07001359 // TODO(b/154317059): Clean up this whitelist by baselining and/or checking in last-released.
1360 if d.Name() != "android.car-system-stubs-docs" &&
Anton Hanssonb30f5932020-09-16 13:14:18 +01001361 d.Name() != "android.car-stubs-docs" {
Makoto Onuki194c43f2020-04-27 17:22:16 -07001362 cmd.Flag("--lints-as-errors")
1363 cmd.Flag("--warnings-as-errors") // Most lints are actually warnings.
1364 }
1365
Makoto Onuki88b99052020-04-27 17:22:16 -07001366 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
1367 updatedBaselineOutput := android.PathForModuleOut(ctx, "api_lint_baseline.txt")
1368 d.apiLintTimestamp = android.PathForModuleOut(ctx, "api_lint.timestamp")
1369
1370 // Note this string includes a special shell quote $' ... ', which decodes the "\n"s.
1371 // However, because $' ... ' doesn't expand environmental variables, we can't just embed
1372 // $PWD, so we have to terminate $'...', use "$PWD", then start $' ... ' again,
1373 // which is why we have '"$PWD"$' in it.
1374 //
1375 // TODO: metalava also has a slightly different message hardcoded. Should we unify this
1376 // message and metalava's one?
1377 msg := `$'` + // Enclose with $' ... '
1378 `************************************************************\n` +
1379 `Your API changes are triggering API Lint warnings or errors.\n` +
1380 `To make these errors go away, fix the code according to the\n` +
1381 `error and/or warning messages above.\n` +
1382 `\n` +
1383 `If it is not possible to do so, there are workarounds:\n` +
1384 `\n` +
1385 `1. You can suppress the errors with @SuppressLint("<id>")\n`
1386
1387 if baselineFile.Valid() {
1388 cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path())
1389 cmd.FlagWithOutput("--update-baseline:api-lint ", updatedBaselineOutput)
1390
1391 msg += fmt.Sprintf(``+
1392 `2. You can update the baseline by executing the following\n`+
1393 ` command:\n`+
Anton Hansson3361a292020-05-11 15:38:31 +01001394 ` cp \\\n`+
1395 ` "'"$PWD"$'/%s" \\\n`+
1396 ` "'"$PWD"$'/%s"\n`+
Makoto Onuki88b99052020-04-27 17:22:16 -07001397 ` To submit the revised baseline.txt to the main Android\n`+
1398 ` repository, you will need approval.\n`, updatedBaselineOutput, baselineFile.Path())
1399 } else {
1400 msg += fmt.Sprintf(``+
1401 `2. You can add a baseline file of existing lint failures\n`+
1402 ` to the build rule of %s.\n`, d.Name())
1403 }
1404 // Note the message ends with a ' (single quote), to close the $' ... ' .
1405 msg += `************************************************************\n'`
1406
1407 cmd.FlagWithArg("--error-message:api-lint ", msg)
1408 }
1409
1410 // Add "check released" options. (Detect incompatible API changes from the last public release)
1411
Dan Willemsen9f435972020-05-28 15:28:00 -07001412 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") {
Makoto Onuki88b99052020-04-27 17:22:16 -07001413 doCheckReleased = true
1414
1415 if len(d.Javadoc.properties.Out) > 0 {
1416 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1417 }
1418
1419 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1420 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
1421 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
1422 updatedBaselineOutput := android.PathForModuleOut(ctx, "last_released_baseline.txt")
1423
1424 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
1425
1426 cmd.FlagWithInput("--check-compatibility:api:released ", apiFile)
1427 cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile)
1428
1429 if baselineFile.Valid() {
1430 cmd.FlagWithInput("--baseline:compatibility:released ", baselineFile.Path())
1431 cmd.FlagWithOutput("--update-baseline:compatibility:released ", updatedBaselineOutput)
1432 }
1433
1434 // Note this string includes quote ($' ... '), which decodes the "\n"s.
1435 msg := `$'\n******************************\n` +
1436 `You have tried to change the API from what has been previously released in\n` +
1437 `an SDK. Please fix the errors listed above.\n` +
1438 `******************************\n'`
1439
1440 cmd.FlagWithArg("--error-message:compatibility:released ", msg)
1441 }
1442
Colin Crossf1a035e2020-11-16 17:32:30 -08001443 impRule := android.NewRuleBuilder(pctx, ctx)
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001444 impCmd := impRule.Command()
Liz Kammer20ebfb42020-07-28 11:32:07 -07001445 // An action that copies the ninja generated rsp file to a new location. This allows us to
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001446 // add a large number of inputs to a file without exceeding bash command length limits (which
1447 // would happen if we use the WriteFile rule). The cp is needed because RuleBuilder sets the
1448 // rsp file to be ${output}.rsp.
Colin Cross70c47412021-03-12 17:48:14 -08001449 impCmd.Text("cp").
1450 FlagWithRspFileInputList("", android.PathForModuleOut(ctx, "metalava-implicits.rsp"), cmd.GetImplicits()).
1451 Output(implicitsRsp)
Colin Crossf1a035e2020-11-16 17:32:30 -08001452 impRule.Build("implicitsGen", "implicits generation")
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001453 cmd.Implicit(implicitsRsp)
1454
Paul Duffin3ae29512020-04-08 18:18:03 +01001455 if generateStubs {
1456 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001457 BuiltTool("soong_zip").
Paul Duffin3ae29512020-04-08 18:18:03 +01001458 Flag("-write_if_changed").
1459 Flag("-jar").
1460 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
1461 FlagWithArg("-C ", stubsDir.String()).
1462 FlagWithArg("-D ", stubsDir.String())
1463 }
Jerome Gaillard0f599032019-10-10 19:29:11 +01001464
1465 if Bool(d.properties.Write_sdk_values) {
1466 d.metadataZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-metadata.zip")
1467 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001468 BuiltTool("soong_zip").
Jerome Gaillard0f599032019-10-10 19:29:11 +01001469 Flag("-write_if_changed").
1470 Flag("-d").
1471 FlagWithOutput("-o ", d.metadataZip).
1472 FlagWithArg("-C ", d.metadataDir.String()).
1473 FlagWithArg("-D ", d.metadataDir.String())
1474 }
1475
Makoto Onuki88b99052020-04-27 17:22:16 -07001476 // TODO: We don't really need two separate API files, but this is a reminiscence of how
1477 // we used to run metalava separately for API lint and the "last_released" check. Unify them.
1478 if doApiLint {
1479 rule.Command().Text("touch").Output(d.apiLintTimestamp)
1480 }
1481 if doCheckReleased {
1482 rule.Command().Text("touch").Output(d.checkLastReleasedApiTimestamp)
1483 }
1484
Colin Cross33961b52019-07-11 11:01:22 -07001485 rule.Restat()
1486
1487 zipSyncCleanupCmd(rule, srcJarDir)
1488
Colin Crossf1a035e2020-11-16 17:32:30 -08001489 rule.Build("metalava", "metalava merged")
Adrian Roos075eedc2019-10-10 12:07:03 +02001490
Dan Willemsen9f435972020-05-28 15:28:00 -07001491 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") {
Colin Cross33961b52019-07-11 11:01:22 -07001492
1493 if len(d.Javadoc.properties.Out) > 0 {
1494 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1495 }
1496
1497 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1498 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Adrian Roos14f75a92019-08-12 17:54:09 +02001499 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
Makoto Onuki5405a732020-04-16 17:02:40 -07001500
1501 if baselineFile.Valid() {
Makoto Onuki88b99052020-04-27 17:22:16 -07001502 ctx.PropertyErrorf("baseline_file", "current API check can't have a baseline file. (module %s)", ctx.ModuleName())
Makoto Onuki5405a732020-04-16 17:02:40 -07001503 }
Nan Zhang61819ce2018-05-04 18:49:16 -07001504
Nan Zhang2760dfc2018-08-24 17:32:54 +00001505 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Nan Zhang2760dfc2018-08-24 17:32:54 +00001506
Colin Crossf1a035e2020-11-16 17:32:30 -08001507 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross33961b52019-07-11 11:01:22 -07001508
Makoto Onuki5405a732020-04-16 17:02:40 -07001509 // Diff command line.
Makoto Onuki88b99052020-04-27 17:22:16 -07001510 // -F matches the closest "opening" line, such as "package android {"
1511 // and " public class Intent {".
Makoto Onuki5405a732020-04-16 17:02:40 -07001512 diff := `diff -u -F '{ *$'`
1513
Colin Cross33961b52019-07-11 11:01:22 -07001514 rule.Command().Text("( true")
Makoto Onuki5405a732020-04-16 17:02:40 -07001515 rule.Command().
1516 Text(diff).
1517 Input(apiFile).Input(d.apiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001518
Makoto Onuki5405a732020-04-16 17:02:40 -07001519 rule.Command().
1520 Text(diff).
1521 Input(removedApiFile).Input(d.removedApiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001522
1523 msg := fmt.Sprintf(`\n******************************\n`+
1524 `You have tried to change the API from what has been previously approved.\n\n`+
1525 `To make these errors go away, you have two choices:\n`+
Makoto Onuki5405a732020-04-16 17:02:40 -07001526 ` 1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)\n`+
1527 ` to the new methods, etc. shown in the above diff.\n\n`+
1528 ` 2. You can update current.txt and/or removed.txt by executing the following command:\n`+
Neil Fuller96d01612020-12-01 21:13:14 +00001529 ` m %s-update-current-api\n\n`+
Colin Cross33961b52019-07-11 11:01:22 -07001530 ` To submit the revised current.txt to the main Android repository,\n`+
1531 ` you will need approval.\n`+
1532 `******************************\n`, ctx.ModuleName())
1533
1534 rule.Command().
1535 Text("touch").Output(d.checkCurrentApiTimestamp).
1536 Text(") || (").
1537 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1538 Text("; exit 38").
1539 Text(")")
1540
Colin Crossf1a035e2020-11-16 17:32:30 -08001541 rule.Build("metalavaCurrentApiCheck", "check current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001542
1543 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001544
1545 // update API rule
Colin Crossf1a035e2020-11-16 17:32:30 -08001546 rule = android.NewRuleBuilder(pctx, ctx)
Colin Cross33961b52019-07-11 11:01:22 -07001547
1548 rule.Command().Text("( true")
1549
1550 rule.Command().
1551 Text("cp").Flag("-f").
1552 Input(d.apiFile).Flag(apiFile.String())
1553
1554 rule.Command().
1555 Text("cp").Flag("-f").
1556 Input(d.removedApiFile).Flag(removedApiFile.String())
1557
1558 msg = "failed to update public API"
1559
1560 rule.Command().
1561 Text("touch").Output(d.updateCurrentApiTimestamp).
1562 Text(") || (").
1563 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1564 Text("; exit 38").
1565 Text(")")
1566
Colin Crossf1a035e2020-11-16 17:32:30 -08001567 rule.Build("metalavaCurrentApiUpdate", "update current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001568 }
Nan Zhanga40da042018-08-01 12:48:00 -07001569
Pete Gillin581d6082018-10-22 15:55:04 +01001570 if String(d.properties.Check_nullability_warnings) != "" {
1571 if d.nullabilityWarningsFile == nil {
1572 ctx.PropertyErrorf("check_nullability_warnings",
1573 "Cannot specify check_nullability_warnings unless validating nullability")
1574 }
Colin Cross33961b52019-07-11 11:01:22 -07001575
1576 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
1577
Pete Gillin581d6082018-10-22 15:55:04 +01001578 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "check_nullability_warnings.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001579
Pete Gillin581d6082018-10-22 15:55:04 +01001580 msg := fmt.Sprintf(`\n******************************\n`+
1581 `The warnings encountered during nullability annotation validation did\n`+
1582 `not match the checked in file of expected warnings. The diffs are shown\n`+
1583 `above. You have two options:\n`+
1584 ` 1. Resolve the differences by editing the nullability annotations.\n`+
1585 ` 2. Update the file of expected warnings by running:\n`+
1586 ` cp %s %s\n`+
1587 ` and submitting the updated file as part of your change.`,
1588 d.nullabilityWarningsFile, checkNullabilityWarnings)
Colin Cross33961b52019-07-11 11:01:22 -07001589
Colin Crossf1a035e2020-11-16 17:32:30 -08001590 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross33961b52019-07-11 11:01:22 -07001591
1592 rule.Command().
1593 Text("(").
1594 Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile).
1595 Text("&&").
1596 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
1597 Text(") || (").
1598 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1599 Text("; exit 38").
1600 Text(")")
1601
Colin Crossf1a035e2020-11-16 17:32:30 -08001602 rule.Build("nullabilityWarningsCheck", "nullability warnings check")
Pete Gillin581d6082018-10-22 15:55:04 +01001603 }
Nan Zhang581fd212018-01-10 16:06:12 -08001604}
Dan Willemsencc090972018-02-26 14:33:31 -08001605
Nan Zhanga40da042018-08-01 12:48:00 -07001606//
Nan Zhangf4936b02018-08-01 15:00:28 -07001607// Exported Droiddoc Directory
Nan Zhanga40da042018-08-01 12:48:00 -07001608//
Dan Willemsencc090972018-02-26 14:33:31 -08001609var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
Nan Zhangf4936b02018-08-01 15:00:28 -07001610var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
Pete Gillin77167902018-09-19 18:16:26 +01001611var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
Nan Zhang9c69a122018-08-22 10:22:08 -07001612var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
Dan Willemsencc090972018-02-26 14:33:31 -08001613
Nan Zhangf4936b02018-08-01 15:00:28 -07001614type ExportedDroiddocDirProperties struct {
1615 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -08001616 Path *string
1617}
1618
Nan Zhangf4936b02018-08-01 15:00:28 -07001619type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -08001620 android.ModuleBase
1621
Nan Zhangf4936b02018-08-01 15:00:28 -07001622 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -08001623
1624 deps android.Paths
1625 dir android.Path
1626}
1627
Colin Crossa3002fc2019-07-08 16:48:04 -07001628// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -07001629func ExportedDroiddocDirFactory() android.Module {
1630 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -08001631 module.AddProperties(&module.properties)
1632 android.InitAndroidModule(module)
1633 return module
1634}
1635
Nan Zhangf4936b02018-08-01 15:00:28 -07001636func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -08001637
Nan Zhangf4936b02018-08-01 15:00:28 -07001638func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -08001639 path := String(d.properties.Path)
1640 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -08001641 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Dan Willemsencc090972018-02-26 14:33:31 -08001642}
Nan Zhangb2b33de2018-02-23 11:18:47 -08001643
1644//
1645// Defaults
1646//
1647type DocDefaults struct {
1648 android.ModuleBase
1649 android.DefaultsModuleBase
1650}
1651
Nan Zhangb2b33de2018-02-23 11:18:47 -08001652func DocDefaultsFactory() android.Module {
1653 module := &DocDefaults{}
1654
1655 module.AddProperties(
1656 &JavadocProperties{},
1657 &DroiddocProperties{},
1658 )
1659
1660 android.InitDefaultsModule(module)
1661
1662 return module
1663}
Nan Zhang1598a9e2018-09-04 17:14:32 -07001664
1665func StubsDefaultsFactory() android.Module {
1666 module := &DocDefaults{}
1667
1668 module.AddProperties(
1669 &JavadocProperties{},
1670 &DroidstubsProperties{},
1671 )
1672
1673 android.InitDefaultsModule(module)
1674
1675 return module
1676}
Colin Cross33961b52019-07-11 11:01:22 -07001677
1678func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
1679 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
1680
1681 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1682 rule.Command().Text("mkdir -p").Text(srcJarDir.String())
1683 srcJarList := srcJarDir.Join(ctx, "list")
1684
1685 rule.Temporary(srcJarList)
1686
Colin Crossf1a035e2020-11-16 17:32:30 -08001687 rule.Command().BuiltTool("zipsync").
Colin Cross33961b52019-07-11 11:01:22 -07001688 FlagWithArg("-d ", srcJarDir.String()).
1689 FlagWithOutput("-l ", srcJarList).
1690 FlagWithArg("-f ", `"*.java"`).
1691 Inputs(srcJars)
1692
1693 return srcJarList
1694}
1695
1696func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
1697 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1698}
Paul Duffin91547182019-11-12 19:39:36 +00001699
1700var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
1701
1702type PrebuiltStubsSourcesProperties struct {
1703 Srcs []string `android:"path"`
1704}
1705
1706type PrebuiltStubsSources struct {
1707 android.ModuleBase
1708 android.DefaultableModuleBase
1709 prebuilt android.Prebuilt
1710 android.SdkBase
1711
1712 properties PrebuiltStubsSourcesProperties
1713
Paul Duffin91547182019-11-12 19:39:36 +00001714 stubsSrcJar android.ModuleOutPath
1715}
1716
Paul Duffin9b478b02019-12-10 13:41:51 +00001717func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) {
1718 switch tag {
1719 case "":
1720 return android.Paths{p.stubsSrcJar}, nil
1721 default:
1722 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1723 }
1724}
1725
Paul Duffin0f8faff2020-05-20 16:18:00 +01001726func (d *PrebuiltStubsSources) StubsSrcJar() android.Path {
1727 return d.stubsSrcJar
1728}
1729
Paul Duffin91547182019-11-12 19:39:36 +00001730func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin9b478b02019-12-10 13:41:51 +00001731 p.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1732
Paul Duffin1a393322020-11-18 16:36:47 +00001733 if len(p.properties.Srcs) != 1 {
1734 ctx.PropertyErrorf("srcs", "must only specify one directory path, contains %d paths", len(p.properties.Srcs))
1735 return
1736 }
1737
1738 localSrcDir := p.properties.Srcs[0]
1739 // Although PathForModuleSrc can return nil if either the path doesn't exist or
1740 // the path components are invalid it won't in this case because no components
1741 // are specified and the module directory must exist in order to get this far.
1742 srcDir := android.PathForModuleSrc(ctx).(android.SourcePath).Join(ctx, localSrcDir)
1743
1744 // Glob the contents of the directory just in case the directory does not exist.
1745 srcGlob := localSrcDir + "/**/*"
1746 srcPaths := android.PathsForModuleSrc(ctx, []string{srcGlob})
Paul Duffin9b478b02019-12-10 13:41:51 +00001747
Colin Crossf1a035e2020-11-16 17:32:30 -08001748 rule := android.NewRuleBuilder(pctx, ctx)
Paul Duffin1a393322020-11-18 16:36:47 +00001749 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001750 BuiltTool("soong_zip").
Paul Duffin9b478b02019-12-10 13:41:51 +00001751 Flag("-write_if_changed").
1752 Flag("-jar").
Paul Duffin1a393322020-11-18 16:36:47 +00001753 FlagWithOutput("-o ", p.stubsSrcJar).
1754 FlagWithArg("-C ", srcDir.String()).
Colin Cross70c47412021-03-12 17:48:14 -08001755 FlagWithRspFileInputList("-r ", p.stubsSrcJar.ReplaceExtension(ctx, "rsp"), srcPaths)
Paul Duffin9b478b02019-12-10 13:41:51 +00001756
1757 rule.Restat()
1758
Colin Crossf1a035e2020-11-16 17:32:30 -08001759 rule.Build("zip src", "Create srcjar from prebuilt source")
Paul Duffin91547182019-11-12 19:39:36 +00001760}
1761
1762func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
1763 return &p.prebuilt
1764}
1765
1766func (p *PrebuiltStubsSources) Name() string {
1767 return p.prebuilt.Name(p.ModuleBase.Name())
1768}
1769
Paul Duffin91547182019-11-12 19:39:36 +00001770// prebuilt_stubs_sources imports a set of java source files as if they were
1771// generated by droidstubs.
1772//
1773// By default, a prebuilt_stubs_sources has a single variant that expects a
1774// set of `.java` files generated by droidstubs.
1775//
1776// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1777// for host modules.
1778//
1779// Intended only for use by sdk snapshots.
1780func PrebuiltStubsSourcesFactory() android.Module {
1781 module := &PrebuiltStubsSources{}
1782
1783 module.AddProperties(&module.properties)
1784
1785 android.InitPrebuiltModule(module, &module.properties.Srcs)
1786 android.InitSdkAwareModule(module)
1787 InitDroiddocModule(module, android.HostAndDeviceSupported)
1788 return module
1789}