blob: c74009ea46f48119fd0dbcc302ae49d45a9bd15b [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:
473 if dep, ok := module.(Dependency); ok {
Nan Zhang581fd212018-01-10 16:06:12 -0800474 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars()...)
Paul Duffin83a2d962019-11-19 19:44:10 +0000475 } else if sm, ok := module.(SystemModulesProvider); ok {
Paul Duffine25c6442019-10-11 13:50:28 +0100476 // A system modules dependency has been added to the bootclasspath
477 // so add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +0000478 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700479 } else {
480 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
481 }
482 case libTag:
483 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800484 case SdkLibraryDependency:
Paul Duffin649dadf2020-05-26 11:42:13 +0100485 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700486 case Dependency:
Sundong Ahnba493602018-11-20 17:36:35 +0900487 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park19a7f252019-07-10 16:59:31 +0900488 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700489 case android.SourceFileProducer:
Nan Zhang581fd212018-01-10 16:06:12 -0800490 checkProducesJars(ctx, dep)
491 deps.classpath = append(deps.classpath, dep.Srcs()...)
Nan Zhang581fd212018-01-10 16:06:12 -0800492 default:
493 ctx.ModuleErrorf("depends on non-java module %q", otherName)
494 }
Colin Cross6cef4812019-10-17 14:23:50 -0700495 case java9LibTag:
496 switch dep := module.(type) {
497 case Dependency:
498 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
499 default:
500 ctx.ModuleErrorf("depends on non-java module %q", otherName)
501 }
Nan Zhang357466b2018-04-17 17:38:36 -0700502 case systemModulesTag:
503 if deps.systemModules != nil {
504 panic("Found two system module dependencies")
505 }
Paul Duffin83a2d962019-11-19 19:44:10 +0000506 sm := module.(SystemModulesProvider)
507 outputDir, outputDeps := sm.OutputDirAndDeps()
508 deps.systemModules = &systemModules{outputDir, outputDeps}
Nan Zhang581fd212018-01-10 16:06:12 -0800509 }
510 })
511 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
512 // may contain filegroup or genrule.
Colin Cross8a497952019-03-05 22:25:09 -0800513 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400514 j.implicits = append(j.implicits, srcFiles...)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900515
516 filterByPackage := func(srcs []android.Path, filterPackages []string) []android.Path {
517 if filterPackages == nil {
518 return srcs
519 }
520 filtered := []android.Path{}
521 for _, src := range srcs {
522 if src.Ext() != ".java" {
523 // Don't filter-out non-Java (=generated sources) by package names. This is not ideal,
524 // but otherwise metalava emits stub sources having references to the generated AIDL classes
525 // in filtered-out pacages (e.g. com.android.internal.*).
526 // TODO(b/141149570) We need to fix this by introducing default private constructors or
527 // fixing metalava to not emit constructors having references to unknown classes.
528 filtered = append(filtered, src)
529 continue
530 }
531 packageName := strings.ReplaceAll(filepath.Dir(src.Rel()), "/", ".")
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800532 if android.HasAnyPrefix(packageName, filterPackages) {
533 filtered = append(filtered, src)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900534 }
535 }
536 return filtered
537 }
538 srcFiles = filterByPackage(srcFiles, j.properties.Filter_packages)
539
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400540 // While metalava needs package html files, it does not need them to be explicit on the command
Anton Hansson746be9c2020-10-08 19:05:40 +0100541 // line. javadoc complains if it receives html files on the command line. The filter
542 // below excludes html files from the rsp file metalava. Note that the html
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400543 // files are still included as implicit inputs for successful remote execution and correct
544 // incremental builds.
545 filterHtml := func(srcs []android.Path) []android.Path {
546 filtered := []android.Path{}
547 for _, src := range srcs {
548 if src.Ext() == ".html" {
549 continue
550 }
551 filtered = append(filtered, src)
552 }
553 return filtered
554 }
555 srcFiles = filterHtml(srcFiles)
556
Liz Kammer585cac22020-07-06 09:12:57 -0700557 aidlFlags := j.collectAidlFlags(ctx, deps)
558 srcFiles = j.genSources(ctx, srcFiles, aidlFlags)
Nan Zhang581fd212018-01-10 16:06:12 -0800559
560 // srcs may depend on some genrule output.
561 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800562 j.srcJars = append(j.srcJars, deps.srcJars...)
563
Nan Zhang581fd212018-01-10 16:06:12 -0800564 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800565 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800566
Liz Kammere1ab2502020-09-10 15:29:25 +0000567 if len(j.srcFiles) > 0 {
568 j.sourcepaths = android.PathsForModuleSrc(ctx, []string{"."})
Nan Zhang581fd212018-01-10 16:06:12 -0800569 }
Nan Zhang581fd212018-01-10 16:06:12 -0800570
Colin Cross8a497952019-03-05 22:25:09 -0800571 j.argFiles = android.PathsForModuleSrc(ctx, j.properties.Arg_files)
Paul Duffin99e4a502019-02-11 15:38:42 +0000572 argFilesMap := map[string]string{}
573 argFileLabels := []string{}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700574
Paul Duffin99e4a502019-02-11 15:38:42 +0000575 for _, label := range j.properties.Arg_files {
Colin Cross8a497952019-03-05 22:25:09 -0800576 var paths = android.PathsForModuleSrc(ctx, []string{label})
Paul Duffin99e4a502019-02-11 15:38:42 +0000577 if _, exists := argFilesMap[label]; !exists {
578 argFilesMap[label] = strings.Join(paths.Strings(), " ")
579 argFileLabels = append(argFileLabels, label)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700580 } else {
581 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000582 label, argFilesMap[label], paths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700583 }
584 }
585
Liz Kammer585cac22020-07-06 09:12:57 -0700586 var argsPropertyName string
587 flags := make([]string, 0)
588 if j.properties.Args != nil && j.properties.Flags != nil {
589 ctx.PropertyErrorf("args", "flags is set. Cannot set args")
590 } else if args := proptools.String(j.properties.Args); args != "" {
591 flags = append(flags, args)
592 argsPropertyName = "args"
593 } else {
594 flags = append(flags, j.properties.Flags...)
595 argsPropertyName = "flags"
596 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700597
Liz Kammer585cac22020-07-06 09:12:57 -0700598 for _, flag := range flags {
599 args, err := android.Expand(flag, func(name string) (string, error) {
600 if strings.HasPrefix(name, "location ") {
601 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
602 if paths, ok := argFilesMap[label]; ok {
603 return paths, nil
604 } else {
605 return "", fmt.Errorf("unknown location label %q, expecting one of %q",
606 label, strings.Join(argFileLabels, ", "))
607 }
608 } else if name == "genDir" {
609 return android.PathForModuleGen(ctx).String(), nil
610 }
611 return "", fmt.Errorf("unknown variable '$(%s)'", name)
612 })
613
614 if err != nil {
615 ctx.PropertyErrorf(argsPropertyName, "%s", err.Error())
616 }
617 j.args = append(j.args, args)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700618 }
619
Nan Zhang581fd212018-01-10 16:06:12 -0800620 return deps
621}
622
623func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
624 j.addDeps(ctx)
625}
626
627func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
628 deps := j.collectDeps(ctx)
629
Colin Crossdaa4c672019-07-15 22:53:46 -0700630 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800631
Colin Crossdaa4c672019-07-15 22:53:46 -0700632 outDir := android.PathForModuleOut(ctx, "out")
633 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
634
635 j.stubsSrcJar = nil
636
Colin Crossf1a035e2020-11-16 17:32:30 -0800637 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossdaa4c672019-07-15 22:53:46 -0700638
639 rule.Command().Text("rm -rf").Text(outDir.String())
640 rule.Command().Text("mkdir -p").Text(outDir.String())
641
642 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, j.srcJars)
Nan Zhang357466b2018-04-17 17:38:36 -0700643
Colin Cross83bb3162018-06-25 15:48:06 -0700644 javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800645
Colin Crossdaa4c672019-07-15 22:53:46 -0700646 cmd := javadocSystemModulesCmd(ctx, rule, j.srcFiles, outDir, srcJarDir, srcJarList,
647 deps.systemModules, deps.classpath, j.sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800648
Colin Cross1e743852019-10-28 11:37:20 -0700649 cmd.FlagWithArg("-source ", javaVersion.String()).
Colin Crossdaa4c672019-07-15 22:53:46 -0700650 Flag("-J-Xmx1024m").
651 Flag("-XDignore.symbol.file").
652 Flag("-Xdoclint:none")
Nan Zhang581fd212018-01-10 16:06:12 -0800653
Colin Crossdaa4c672019-07-15 22:53:46 -0700654 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800655 BuiltTool("soong_zip").
Colin Crossdaa4c672019-07-15 22:53:46 -0700656 Flag("-write_if_changed").
657 Flag("-d").
658 FlagWithOutput("-o ", j.docZip).
659 FlagWithArg("-C ", outDir.String()).
660 FlagWithArg("-D ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700661
Colin Crossdaa4c672019-07-15 22:53:46 -0700662 rule.Restat()
663
664 zipSyncCleanupCmd(rule, srcJarDir)
665
Colin Crossf1a035e2020-11-16 17:32:30 -0800666 rule.Build("javadoc", "javadoc")
Nan Zhang581fd212018-01-10 16:06:12 -0800667}
668
Nan Zhanga40da042018-08-01 12:48:00 -0700669//
670// Droiddoc
671//
672type Droiddoc struct {
673 Javadoc
674
Liz Kammere1ab2502020-09-10 15:29:25 +0000675 properties DroiddocProperties
Nan Zhanga40da042018-08-01 12:48:00 -0700676}
677
Colin Crossa3002fc2019-07-08 16:48:04 -0700678// droiddoc converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700679func DroiddocFactory() android.Module {
680 module := &Droiddoc{}
681
682 module.AddProperties(&module.properties,
683 &module.Javadoc.properties)
684
685 InitDroiddocModule(module, android.HostAndDeviceSupported)
686 return module
687}
688
Colin Crossa3002fc2019-07-08 16:48:04 -0700689// droiddoc_host converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700690func DroiddocHostFactory() android.Module {
691 module := &Droiddoc{}
692
693 module.AddProperties(&module.properties,
694 &module.Javadoc.properties)
695
696 InitDroiddocModule(module, android.HostSupported)
697 return module
698}
699
Liz Kammere1ab2502020-09-10 15:29:25 +0000700func (d *Droiddoc) OutputFiles(tag string) (android.Paths, error) {
701 switch tag {
702 case "", ".docs.zip":
703 return android.Paths{d.Javadoc.docZip}, nil
704 default:
705 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
706 }
Nan Zhanga40da042018-08-01 12:48:00 -0700707}
708
Nan Zhang581fd212018-01-10 16:06:12 -0800709func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
710 d.Javadoc.addDeps(ctx)
711
Nan Zhang79614d12018-04-19 18:03:39 -0700712 if String(d.properties.Custom_template) != "" {
Dan Willemsencc090972018-02-26 14:33:31 -0800713 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
714 }
Nan Zhang581fd212018-01-10 16:06:12 -0800715}
716
Colin Crossab054432019-07-15 16:13:59 -0700717func (d *Droiddoc) doclavaDocsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, docletPath classpath) {
Colin Cross2a2e0db2020-02-21 16:55:46 -0800718 buildNumberFile := ctx.Config().BuildNumberFile(ctx)
Nan Zhang443fa522018-08-20 20:58:28 -0700719 // Droiddoc always gets "-source 1.8" because it doesn't support 1.9 sources. For modules with 1.9
720 // sources, droiddoc will get sources produced by metalava which will have already stripped out the
721 // 1.9 language features.
Colin Crossab054432019-07-15 16:13:59 -0700722 cmd.FlagWithArg("-source ", "1.8").
723 Flag("-J-Xmx1600m").
724 Flag("-J-XX:-OmitStackTraceInFastThrow").
725 Flag("-XDignore.symbol.file").
726 FlagWithArg("-doclet ", "com.google.doclava.Doclava").
727 FlagWithInputList("-docletpath ", docletPath.Paths(), ":").
Colin Cross2a2e0db2020-02-21 16:55:46 -0800728 FlagWithArg("-hdf page.build ", ctx.Config().BuildId()+"-$(cat "+buildNumberFile.String()+")").OrderOnly(buildNumberFile).
Elliott Hughes26bce342019-09-12 15:05:13 -0700729 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 -0700730
Nan Zhanga40da042018-08-01 12:48:00 -0700731 if String(d.properties.Custom_template) == "" {
732 // TODO: This is almost always droiddoc-templates-sdk
733 ctx.PropertyErrorf("custom_template", "must specify a template")
734 }
735
736 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
Nan Zhangf4936b02018-08-01 15:00:28 -0700737 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Crossab054432019-07-15 16:13:59 -0700738 cmd.FlagWithArg("-templatedir ", t.dir.String()).Implicits(t.deps)
Nan Zhanga40da042018-08-01 12:48:00 -0700739 } else {
Paul Duffin884363e2019-12-19 10:21:09 +0000740 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_exported_dir", ctx.OtherModuleName(m))
Nan Zhanga40da042018-08-01 12:48:00 -0700741 }
742 })
743
744 if len(d.properties.Html_dirs) > 0 {
Colin Crossab054432019-07-15 16:13:59 -0700745 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
746 cmd.FlagWithArg("-htmldir ", htmlDir.String()).
747 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[0], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700748 }
749
750 if len(d.properties.Html_dirs) > 1 {
Colin Crossab054432019-07-15 16:13:59 -0700751 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
752 cmd.FlagWithArg("-htmldir2 ", htmlDir2.String()).
753 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[1], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700754 }
755
756 if len(d.properties.Html_dirs) > 2 {
757 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
758 }
759
Colin Cross8a497952019-03-05 22:25:09 -0800760 knownTags := android.PathsForModuleSrc(ctx, d.properties.Knowntags)
Colin Crossab054432019-07-15 16:13:59 -0700761 cmd.FlagForEachInput("-knowntags ", knownTags)
Nan Zhanga40da042018-08-01 12:48:00 -0700762
Colin Crossab054432019-07-15 16:13:59 -0700763 cmd.FlagForEachArg("-hdf ", d.properties.Hdf)
Nan Zhanga40da042018-08-01 12:48:00 -0700764
765 if String(d.properties.Proofread_file) != "" {
766 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
Colin Crossab054432019-07-15 16:13:59 -0700767 cmd.FlagWithOutput("-proofread ", proofreadFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700768 }
769
770 if String(d.properties.Todo_file) != "" {
771 // tricky part:
772 // we should not compute full path for todo_file through PathForModuleOut().
773 // the non-standard doclet will get the full path relative to "-o".
Colin Crossab054432019-07-15 16:13:59 -0700774 cmd.FlagWithArg("-todo ", String(d.properties.Todo_file)).
775 ImplicitOutput(android.PathForModuleOut(ctx, String(d.properties.Todo_file)))
Nan Zhanga40da042018-08-01 12:48:00 -0700776 }
777
778 if String(d.properties.Resourcesdir) != "" {
779 // TODO: should we add files under resourcesDir to the implicits? It seems that
780 // resourcesDir is one sub dir of htmlDir
781 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
Colin Crossab054432019-07-15 16:13:59 -0700782 cmd.FlagWithArg("-resourcesdir ", resourcesDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700783 }
784
785 if String(d.properties.Resourcesoutdir) != "" {
786 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
Colin Crossab054432019-07-15 16:13:59 -0700787 cmd.FlagWithArg("-resourcesoutdir ", String(d.properties.Resourcesoutdir))
Nan Zhanga40da042018-08-01 12:48:00 -0700788 }
Nan Zhanga40da042018-08-01 12:48:00 -0700789}
790
Colin Crossab054432019-07-15 16:13:59 -0700791func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700792 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700793 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
794 rule.Command().Text("cp").
795 Input(staticDocIndexRedirect).
796 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700797 }
798
799 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700800 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
801 rule.Command().Text("cp").
802 Input(staticDocProperties).
803 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700804 }
Nan Zhanga40da042018-08-01 12:48:00 -0700805}
806
Colin Crossab054432019-07-15 16:13:59 -0700807func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700808 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700809
810 cmd := rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800811 BuiltTool("soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
Colin Crossab054432019-07-15 16:13:59 -0700812 Flag(config.JavacVmFlags).
813 FlagWithArg("-encoding ", "UTF-8").
Colin Crossab054432019-07-15 16:13:59 -0700814 FlagWithRspFileInputList("@", srcs).
815 FlagWithInput("@", srcJarList)
816
Colin Crossab054432019-07-15 16:13:59 -0700817 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
818 // based stubs generation.
819 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
820 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
821 // the correct package name base path.
822 if len(sourcepaths) > 0 {
823 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
824 } else {
825 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
826 }
827
828 cmd.FlagWithArg("-d ", outDir.String()).
829 Flag("-quiet")
830
831 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700832}
833
Colin Crossdaa4c672019-07-15 22:53:46 -0700834func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
835 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
836 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
837
838 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
839
840 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
841 cmd.Flag(flag).Implicits(deps)
842
843 cmd.FlagWithArg("--patch-module ", "java.base=.")
844
845 if len(classpath) > 0 {
846 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
847 }
848
849 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700850}
851
Colin Crossdaa4c672019-07-15 22:53:46 -0700852func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
853 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
854 sourcepaths android.Paths) *android.RuleBuilderCommand {
855
856 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
857
858 if len(bootclasspath) == 0 && ctx.Device() {
859 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
860 // ensure java does not fall back to the default bootclasspath.
861 cmd.FlagWithArg("-bootclasspath ", `""`)
862 } else if len(bootclasspath) > 0 {
863 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
864 }
865
866 if len(classpath) > 0 {
867 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
868 }
869
870 return cmd
871}
872
Colin Crossab054432019-07-15 16:13:59 -0700873func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
874 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700875
Colin Crossab054432019-07-15 16:13:59 -0700876 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
877 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
878
879 return rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800880 BuiltTool("dokka").
Colin Crossab054432019-07-15 16:13:59 -0700881 Flag(config.JavacVmFlags).
882 Flag(srcJarDir.String()).
883 FlagWithInputList("-classpath ", dokkaClasspath, ":").
884 FlagWithArg("-format ", "dac").
885 FlagWithArg("-dacRoot ", "/reference/kotlin").
886 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700887}
888
889func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
890 deps := d.Javadoc.collectDeps(ctx)
891
Colin Crossdaa4c672019-07-15 22:53:46 -0700892 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Colin Crossdaa4c672019-07-15 22:53:46 -0700893
Nan Zhang1598a9e2018-09-04 17:14:32 -0700894 jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
895 doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700896
Colin Crossab054432019-07-15 16:13:59 -0700897 outDir := android.PathForModuleOut(ctx, "out")
898 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700899
Colin Crossf1a035e2020-11-16 17:32:30 -0800900 rule := android.NewRuleBuilder(pctx, ctx)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700901
Colin Crossab054432019-07-15 16:13:59 -0700902 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
903
904 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -0700905 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -0700906 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700907 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -0700908 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -0700909 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700910 }
911
Liz Kammer585cac22020-07-06 09:12:57 -0700912 cmd.Flag(strings.Join(d.Javadoc.args, " ")).Implicits(d.Javadoc.argFiles)
Colin Crossab054432019-07-15 16:13:59 -0700913
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000914 if d.properties.Compat_config != nil {
915 compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
916 cmd.FlagWithInput("-compatconfig ", compatConfig)
917 }
918
Colin Crossab054432019-07-15 16:13:59 -0700919 var desc string
920 if Bool(d.properties.Dokka_enabled) {
921 desc = "dokka"
922 } else {
923 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
924
925 for _, o := range d.Javadoc.properties.Out {
926 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
927 }
928
929 d.postDoclavaCmds(ctx, rule)
930 desc = "doclava"
931 }
932
933 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800934 BuiltTool("soong_zip").
Colin Crossab054432019-07-15 16:13:59 -0700935 Flag("-write_if_changed").
936 Flag("-d").
937 FlagWithOutput("-o ", d.docZip).
938 FlagWithArg("-C ", outDir.String()).
939 FlagWithArg("-D ", outDir.String())
940
Colin Crossab054432019-07-15 16:13:59 -0700941 rule.Restat()
942
943 zipSyncCleanupCmd(rule, srcJarDir)
944
Colin Crossf1a035e2020-11-16 17:32:30 -0800945 rule.Build("javadoc", desc)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700946}
947
948//
949// Droidstubs
950//
951type Droidstubs struct {
952 Javadoc
Paul Duffin91547182019-11-12 19:39:36 +0000953 android.SdkBase
Nan Zhang1598a9e2018-09-04 17:14:32 -0700954
Pete Gillin581d6082018-10-22 15:55:04 +0100955 properties DroidstubsProperties
956 apiFile android.WritablePath
957 apiXmlFile android.WritablePath
958 lastReleasedApiXmlFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +0100959 privateApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +0100960 removedApiFile android.WritablePath
961 removedDexApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +0100962 nullabilityWarningsFile android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -0700963
964 checkCurrentApiTimestamp android.WritablePath
965 updateCurrentApiTimestamp android.WritablePath
966 checkLastReleasedApiTimestamp android.WritablePath
Adrian Roos075eedc2019-10-10 12:07:03 +0200967 apiLintTimestamp android.WritablePath
Adrian Roos3b8f1cd2019-11-01 13:42:39 +0100968 apiLintReport android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -0700969
Pete Gillin581d6082018-10-22 15:55:04 +0100970 checkNullabilityWarningsTimestamp android.WritablePath
971
Nan Zhang1598a9e2018-09-04 17:14:32 -0700972 annotationsZip android.WritablePath
Nan Zhang9c69a122018-08-22 10:22:08 -0700973 apiVersionsXml android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -0700974
Paul Duffinbc0fe962020-10-13 15:04:02 +0100975 apiFilePath android.Path
976 removedApiFilePath android.Path
Nan Zhang71bbe632018-09-17 14:32:21 -0700977
Jerome Gaillard0f599032019-10-10 19:29:11 +0100978 metadataZip android.WritablePath
979 metadataDir android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -0700980}
981
Colin Crossa3002fc2019-07-08 16:48:04 -0700982// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
983// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to
984// a droiddoc module to generate documentation.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700985func DroidstubsFactory() android.Module {
986 module := &Droidstubs{}
987
988 module.AddProperties(&module.properties,
989 &module.Javadoc.properties)
990
991 InitDroiddocModule(module, android.HostAndDeviceSupported)
Paul Duffin91547182019-11-12 19:39:36 +0000992 android.InitSdkAwareModule(module)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700993 return module
994}
995
Colin Crossa3002fc2019-07-08 16:48:04 -0700996// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API
997// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be
998// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs
999// module when symbols needed by the source files are provided by java_library_host modules.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001000func DroidstubsHostFactory() android.Module {
1001 module := &Droidstubs{}
1002
1003 module.AddProperties(&module.properties,
1004 &module.Javadoc.properties)
1005
1006 InitDroiddocModule(module, android.HostSupported)
1007 return module
1008}
1009
Colin Cross014489c2020-06-02 20:09:13 -07001010func (d *Droidstubs) OutputFiles(tag string) (android.Paths, error) {
1011 switch tag {
1012 case "":
1013 return android.Paths{d.stubsSrcJar}, nil
1014 case ".docs.zip":
1015 return android.Paths{d.docZip}, nil
Paul Duffind8aed4b2020-11-26 00:26:42 +00001016 case ".api.txt", android.DefaultDistTag:
1017 // This is the default dist path for dist properties that have no tag property.
Anton Hanssonecf54352020-10-02 17:44:34 +01001018 return android.Paths{d.apiFilePath}, nil
1019 case ".removed-api.txt":
Paul Duffinbc0fe962020-10-13 15:04:02 +01001020 return android.Paths{d.removedApiFilePath}, nil
Colin Cross014489c2020-06-02 20:09:13 -07001021 case ".annotations.zip":
1022 return android.Paths{d.annotationsZip}, nil
1023 case ".api_versions.xml":
1024 return android.Paths{d.apiVersionsXml}, nil
1025 default:
1026 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1027 }
1028}
1029
Nan Zhang1598a9e2018-09-04 17:14:32 -07001030func (d *Droidstubs) ApiFilePath() android.Path {
1031 return d.apiFilePath
1032}
1033
Paul Duffin1fd005d2020-04-09 01:08:11 +01001034func (d *Droidstubs) RemovedApiFilePath() android.Path {
Paul Duffinbc0fe962020-10-13 15:04:02 +01001035 return d.removedApiFilePath
Paul Duffin1fd005d2020-04-09 01:08:11 +01001036}
1037
Paul Duffin3d1248c2020-04-09 00:10:17 +01001038func (d *Droidstubs) StubsSrcJar() android.Path {
1039 return d.stubsSrcJar
1040}
1041
Nan Zhang1598a9e2018-09-04 17:14:32 -07001042func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
1043 d.Javadoc.addDeps(ctx)
1044
Nan Zhang1598a9e2018-09-04 17:14:32 -07001045 if len(d.properties.Merge_annotations_dirs) != 0 {
1046 for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
1047 ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
1048 }
1049 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001050
Pete Gillin77167902018-09-19 18:16:26 +01001051 if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
1052 for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
1053 ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
1054 }
1055 }
1056
Nan Zhang9c69a122018-08-22 10:22:08 -07001057 if len(d.properties.Api_levels_annotations_dirs) != 0 {
1058 for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
1059 ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
1060 }
1061 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001062}
1063
Paul Duffin3ae29512020-04-08 18:18:03 +01001064func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001065 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1066 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001067 String(d.properties.Api_filename) != "" {
Liz Kammer9ed79152020-08-24 15:56:32 -07001068 filename := proptools.StringDefault(d.properties.Api_filename, ctx.ModuleName()+"_api.txt")
1069 d.apiFile = android.PathForModuleOut(ctx, filename)
Colin Cross33961b52019-07-11 11:01:22 -07001070 cmd.FlagWithOutput("--api ", d.apiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001071 d.apiFilePath = d.apiFile
Paul Duffinbc0fe962020-10-13 15:04:02 +01001072 } else if sourceApiFile := proptools.String(d.properties.Check_api.Current.Api_file); sourceApiFile != "" {
1073 // If check api is disabled then make the source file available for export.
1074 d.apiFilePath = android.PathForModuleSrc(ctx, sourceApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001075 }
1076
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001077 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1078 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001079 String(d.properties.Removed_api_filename) != "" {
Liz Kammer9ed79152020-08-24 15:56:32 -07001080 filename := proptools.StringDefault(d.properties.Removed_api_filename, ctx.ModuleName()+"_removed.txt")
1081 d.removedApiFile = android.PathForModuleOut(ctx, filename)
Colin Cross33961b52019-07-11 11:01:22 -07001082 cmd.FlagWithOutput("--removed-api ", d.removedApiFile)
Paul Duffinbc0fe962020-10-13 15:04:02 +01001083 d.removedApiFilePath = d.removedApiFile
1084 } else if sourceRemovedApiFile := proptools.String(d.properties.Check_api.Current.Removed_api_file); sourceRemovedApiFile != "" {
1085 // If check api is disabled then make the source removed api file available for export.
1086 d.removedApiFilePath = android.PathForModuleSrc(ctx, sourceRemovedApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001087 }
1088
Nan Zhang1598a9e2018-09-04 17:14:32 -07001089 if String(d.properties.Removed_dex_api_filename) != "" {
1090 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001091 cmd.FlagWithOutput("--removed-dex-api ", d.removedDexApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001092 }
1093
Nan Zhang9c69a122018-08-22 10:22:08 -07001094 if Bool(d.properties.Write_sdk_values) {
Jerome Gaillard0f599032019-10-10 19:29:11 +01001095 d.metadataDir = android.PathForModuleOut(ctx, "metadata")
1096 cmd.FlagWithArg("--sdk-values ", d.metadataDir.String())
Nan Zhang9c69a122018-08-22 10:22:08 -07001097 }
1098
Paul Duffin3ae29512020-04-08 18:18:03 +01001099 if stubsDir.Valid() {
1100 if Bool(d.properties.Create_doc_stubs) {
1101 cmd.FlagWithArg("--doc-stubs ", stubsDir.String())
1102 } else {
1103 cmd.FlagWithArg("--stubs ", stubsDir.String())
Paul Duffin6877e6d2020-09-25 19:59:14 +01001104 if !Bool(d.properties.Output_javadoc_comments) {
1105 cmd.Flag("--exclude-documentation-from-stubs")
1106 }
Paul Duffin3ae29512020-04-08 18:18:03 +01001107 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001108 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001109}
1110
Colin Cross33961b52019-07-11 11:01:22 -07001111func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang1598a9e2018-09-04 17:14:32 -07001112 if Bool(d.properties.Annotations_enabled) {
Colin Cross33961b52019-07-11 11:01:22 -07001113 cmd.Flag("--include-annotations")
1114
Pete Gillinc382a562018-11-14 18:45:46 +00001115 validatingNullability :=
Liz Kammer585cac22020-07-06 09:12:57 -07001116 android.InList("--validate-nullability-from-merged-stubs", d.Javadoc.args) ||
Pete Gillinc382a562018-11-14 18:45:46 +00001117 String(d.properties.Validate_nullability_from_list) != ""
Paul Duffin13a9dd62019-11-04 10:26:47 +00001118
Pete Gillina262c052018-09-14 14:25:48 +01001119 migratingNullability := String(d.properties.Previous_api) != ""
Pete Gillina262c052018-09-14 14:25:48 +01001120 if migratingNullability {
Colin Cross8a497952019-03-05 22:25:09 -08001121 previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
Colin Cross33961b52019-07-11 11:01:22 -07001122 cmd.FlagWithInput("--migrate-nullness ", previousApi)
Pete Gillina262c052018-09-14 14:25:48 +01001123 }
Colin Cross33961b52019-07-11 11:01:22 -07001124
Pete Gillinc382a562018-11-14 18:45:46 +00001125 if s := String(d.properties.Validate_nullability_from_list); s != "" {
Colin Cross33961b52019-07-11 11:01:22 -07001126 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
Pete Gillinc382a562018-11-14 18:45:46 +00001127 }
Colin Cross33961b52019-07-11 11:01:22 -07001128
Pete Gillina262c052018-09-14 14:25:48 +01001129 if validatingNullability {
Pete Gillin581d6082018-10-22 15:55:04 +01001130 d.nullabilityWarningsFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_nullability_warnings.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001131 cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile)
Pete Gillina262c052018-09-14 14:25:48 +01001132 }
Nan Zhanga40da042018-08-01 12:48:00 -07001133
1134 d.annotationsZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"_annotations.zip")
Colin Cross33961b52019-07-11 11:01:22 -07001135 cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip)
Nan Zhangf4936b02018-08-01 15:00:28 -07001136
Anton Hansson9d7c3fb2020-05-21 10:11:31 +01001137 if len(d.properties.Merge_annotations_dirs) != 0 {
1138 d.mergeAnnoDirFlags(ctx, cmd)
Nan Zhanga40da042018-08-01 12:48:00 -07001139 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001140
Colin Cross33961b52019-07-11 11:01:22 -07001141 // TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
1142 cmd.FlagWithArg("--hide ", "HiddenTypedefConstant").
1143 FlagWithArg("--hide ", "SuperfluousPrefix").
1144 FlagWithArg("--hide ", "AnnotationExtraction")
1145 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001146}
1147
Colin Cross33961b52019-07-11 11:01:22 -07001148func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
1149 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
1150 if t, ok := m.(*ExportedDroiddocDir); ok {
1151 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
1152 } else {
1153 ctx.PropertyErrorf("merge_annotations_dirs",
1154 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1155 }
1156 })
1157}
1158
1159func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Pete Gillin77167902018-09-19 18:16:26 +01001160 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
1161 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Cross33961b52019-07-11 11:01:22 -07001162 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
Pete Gillin77167902018-09-19 18:16:26 +01001163 } else {
1164 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
1165 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1166 }
1167 })
Nan Zhanga40da042018-08-01 12:48:00 -07001168}
1169
Colin Cross33961b52019-07-11 11:01:22 -07001170func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001171 if !Bool(d.properties.Api_levels_annotations_enabled) {
1172 return
Nan Zhang9c69a122018-08-22 10:22:08 -07001173 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001174
1175 d.apiVersionsXml = android.PathForModuleOut(ctx, "api-versions.xml")
1176
1177 if len(d.properties.Api_levels_annotations_dirs) == 0 {
1178 ctx.PropertyErrorf("api_levels_annotations_dirs",
1179 "has to be non-empty if api levels annotations was enabled!")
1180 }
1181
1182 cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml)
1183 cmd.FlagWithInput("--apply-api-levels ", d.apiVersionsXml)
Dan Albert4f378d72020-07-23 17:32:15 -07001184 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion().String())
Liz Kammer3d894b72020-08-04 09:55:13 -07001185 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
1186
1187 filename := proptools.StringDefault(d.properties.Api_levels_jar_filename, "android.jar")
1188
1189 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
1190 if t, ok := m.(*ExportedDroiddocDir); ok {
1191 for _, dep := range t.deps {
1192 if strings.HasSuffix(dep.String(), filename) {
1193 cmd.Implicit(dep)
1194 }
1195 }
1196 cmd.FlagWithArg("--android-jar-pattern ", t.dir.String()+"/%/public/"+filename)
1197 } else {
1198 ctx.PropertyErrorf("api_levels_annotations_dirs",
1199 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
1200 }
1201 })
Nan Zhang9c69a122018-08-22 10:22:08 -07001202}
1203
Colin Cross1e743852019-10-28 11:37:20 -07001204func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths,
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001205 srcJarList android.Path, bootclasspath, classpath classpath, sourcepaths android.Paths, implicitsRsp android.WritablePath, sandbox bool) *android.RuleBuilderCommand {
Ramy Medhat427683c2020-04-30 03:08:37 -04001206 cmd := rule.Command()
Ramy Medhat16f23a42020-09-03 01:29:49 -04001207 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_METALAVA") {
Ramy Medhat427683c2020-04-30 03:08:37 -04001208 rule.Remoteable(android.RemoteRuleSupports{RBE: true})
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001209 pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "metalava")
1210 execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
1211 labels := map[string]string{"type": "compile", "lang": "java", "compiler": "metalava"}
1212 if !sandbox {
1213 execStrategy = remoteexec.LocalExecStrategy
1214 labels["shallow"] = "true"
Ramy Medhat427683c2020-04-30 03:08:37 -04001215 }
1216 inputs := []string{android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "metalava.jar").String()}
1217 if v := ctx.Config().Getenv("RBE_METALAVA_INPUTS"); v != "" {
1218 inputs = append(inputs, strings.Split(v, ",")...)
1219 }
1220 cmd.Text((&remoteexec.REParams{
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001221 Labels: labels,
Ramy Medhat427683c2020-04-30 03:08:37 -04001222 ExecStrategy: execStrategy,
1223 Inputs: inputs,
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001224 RSPFile: implicitsRsp.String(),
Ramy Medhat427683c2020-04-30 03:08:37 -04001225 ToolchainInputs: []string{config.JavaCmd(ctx).String()},
1226 Platform: map[string]string{remoteexec.PoolKey: pool},
1227 }).NoVarTemplate(ctx.Config()))
1228 }
1229
Colin Crossf1a035e2020-11-16 17:32:30 -08001230 cmd.BuiltTool("metalava").
Colin Cross33961b52019-07-11 11:01:22 -07001231 Flag(config.JavacVmFlags).
Aurimas Liutikas4c5efde2020-09-21 11:18:06 -07001232 Flag("-J--add-opens=java.base/java.util=ALL-UNNAMED").
Colin Cross33961b52019-07-11 11:01:22 -07001233 FlagWithArg("-encoding ", "UTF-8").
Colin Cross1e743852019-10-28 11:37:20 -07001234 FlagWithArg("-source ", javaVersion.String()).
Colin Cross33961b52019-07-11 11:01:22 -07001235 FlagWithRspFileInputList("@", srcs).
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001236 FlagWithInput("@", srcJarList)
1237
1238 if javaHome := ctx.Config().Getenv("ANDROID_JAVA_HOME"); javaHome != "" {
1239 cmd.Implicit(android.PathForSource(ctx, javaHome))
1240 }
1241
1242 if sandbox {
1243 cmd.FlagWithOutput("--strict-input-files ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
1244 } else {
1245 cmd.FlagWithOutput("--strict-input-files:warn ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
1246 }
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001247
Colin Cross238c1f32020-06-07 16:58:18 -07001248 if implicitsRsp != nil {
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001249 cmd.FlagWithArg("--strict-input-files-exempt ", "@"+implicitsRsp.String())
1250 }
Colin Cross33961b52019-07-11 11:01:22 -07001251
1252 if len(bootclasspath) > 0 {
1253 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
Nan Zhang71bbe632018-09-17 14:32:21 -07001254 }
1255
Colin Cross33961b52019-07-11 11:01:22 -07001256 if len(classpath) > 0 {
1257 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
1258 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001259
Colin Cross33961b52019-07-11 11:01:22 -07001260 if len(sourcepaths) > 0 {
1261 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
1262 } else {
1263 cmd.FlagWithArg("-sourcepath ", `""`)
1264 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001265
Colin Cross33961b52019-07-11 11:01:22 -07001266 cmd.Flag("--no-banner").
1267 Flag("--color").
1268 Flag("--quiet").
Makoto Onuki0df103a2020-07-17 15:11:24 -07001269 Flag("--format=v2").
1270 FlagWithArg("--repeat-errors-max ", "10").
1271 FlagWithArg("--hide ", "UnresolvedImport")
Nan Zhang86d2d552018-08-09 15:33:27 -07001272
Colin Cross33961b52019-07-11 11:01:22 -07001273 return cmd
Nan Zhang71bbe632018-09-17 14:32:21 -07001274}
1275
Nan Zhang1598a9e2018-09-04 17:14:32 -07001276func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga40da042018-08-01 12:48:00 -07001277 deps := d.Javadoc.collectDeps(ctx)
1278
1279 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), sdkContext(d))
Nan Zhang581fd212018-01-10 16:06:12 -08001280
Colin Cross33961b52019-07-11 11:01:22 -07001281 // Create rule for metalava
Nan Zhanga40da042018-08-01 12:48:00 -07001282
Colin Cross33961b52019-07-11 11:01:22 -07001283 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang71bbe632018-09-17 14:32:21 -07001284
Colin Crossf1a035e2020-11-16 17:32:30 -08001285 rule := android.NewRuleBuilder(pctx, ctx)
Nan Zhanga40da042018-08-01 12:48:00 -07001286
Anton Hansson52ac73d2020-10-26 09:57:40 +00001287 if BoolDefault(d.properties.High_mem, false) {
1288 // This metalava run uses lots of memory, restrict the number of metalava jobs that can run in parallel.
1289 rule.HighMem()
1290 }
1291
Paul Duffin3ae29512020-04-08 18:18:03 +01001292 generateStubs := BoolDefault(d.properties.Generate_stubs, true)
1293 var stubsDir android.OptionalPath
1294 if generateStubs {
1295 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1296 stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "stubsDir"))
1297 rule.Command().Text("rm -rf").Text(stubsDir.String())
1298 rule.Command().Text("mkdir -p").Text(stubsDir.String())
1299 }
Nan Zhanga40da042018-08-01 12:48:00 -07001300
Colin Cross33961b52019-07-11 11:01:22 -07001301 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1302
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001303 implicitsRsp := android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"implicits.rsp")
1304
Colin Cross33961b52019-07-11 11:01:22 -07001305 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001306 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths, implicitsRsp,
1307 Bool(d.Javadoc.properties.Sandbox))
1308 cmd.Implicits(d.Javadoc.implicits)
Colin Cross33961b52019-07-11 11:01:22 -07001309
1310 d.stubsFlags(ctx, cmd, stubsDir)
1311
1312 d.annotationsFlags(ctx, cmd)
1313 d.inclusionAnnotationsFlags(ctx, cmd)
1314 d.apiLevelsAnnotationsFlags(ctx, cmd)
Nan Zhang71bbe632018-09-17 14:32:21 -07001315
Liz Kammer585cac22020-07-06 09:12:57 -07001316 if android.InList("--generate-documentation", d.Javadoc.args) {
Nan Zhang1598a9e2018-09-04 17:14:32 -07001317 // Currently Metalava have the ability to invoke Javadoc in a seperate process.
1318 // Pass "-nodocs" to suppress the Javadoc invocation when Metalava receives
1319 // "--generate-documentation" arg. This is not needed when Metalava removes this feature.
Liz Kammer585cac22020-07-06 09:12:57 -07001320 d.Javadoc.args = append(d.Javadoc.args, "-nodocs")
Nan Zhang79614d12018-04-19 18:03:39 -07001321 }
Colin Cross33961b52019-07-11 11:01:22 -07001322
Liz Kammer585cac22020-07-06 09:12:57 -07001323 cmd.Flag(strings.Join(d.Javadoc.args, " ")).Implicits(d.Javadoc.argFiles)
Colin Cross33961b52019-07-11 11:01:22 -07001324 for _, o := range d.Javadoc.properties.Out {
1325 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1326 }
1327
Makoto Onuki88b99052020-04-27 17:22:16 -07001328 // Add options for the other optional tasks: API-lint and check-released.
1329 // We generate separate timestamp files for them.
1330
1331 doApiLint := false
1332 doCheckReleased := false
1333
1334 // Add API lint options.
1335
Dan Willemsen9f435972020-05-28 15:28:00 -07001336 if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) {
Makoto Onuki88b99052020-04-27 17:22:16 -07001337 doApiLint = true
1338
1339 newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since)
1340 if newSince.Valid() {
1341 cmd.FlagWithInput("--api-lint ", newSince.Path())
1342 } else {
1343 cmd.Flag("--api-lint")
1344 }
1345 d.apiLintReport = android.PathForModuleOut(ctx, "api_lint_report.txt")
1346 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport) // TODO: Change to ":api-lint"
1347
Makoto Onuki194c43f2020-04-27 17:22:16 -07001348 // TODO(b/154317059): Clean up this whitelist by baselining and/or checking in last-released.
1349 if d.Name() != "android.car-system-stubs-docs" &&
Anton Hanssonb30f5932020-09-16 13:14:18 +01001350 d.Name() != "android.car-stubs-docs" {
Makoto Onuki194c43f2020-04-27 17:22:16 -07001351 cmd.Flag("--lints-as-errors")
1352 cmd.Flag("--warnings-as-errors") // Most lints are actually warnings.
1353 }
1354
Makoto Onuki88b99052020-04-27 17:22:16 -07001355 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
1356 updatedBaselineOutput := android.PathForModuleOut(ctx, "api_lint_baseline.txt")
1357 d.apiLintTimestamp = android.PathForModuleOut(ctx, "api_lint.timestamp")
1358
1359 // Note this string includes a special shell quote $' ... ', which decodes the "\n"s.
1360 // However, because $' ... ' doesn't expand environmental variables, we can't just embed
1361 // $PWD, so we have to terminate $'...', use "$PWD", then start $' ... ' again,
1362 // which is why we have '"$PWD"$' in it.
1363 //
1364 // TODO: metalava also has a slightly different message hardcoded. Should we unify this
1365 // message and metalava's one?
1366 msg := `$'` + // Enclose with $' ... '
1367 `************************************************************\n` +
1368 `Your API changes are triggering API Lint warnings or errors.\n` +
1369 `To make these errors go away, fix the code according to the\n` +
1370 `error and/or warning messages above.\n` +
1371 `\n` +
1372 `If it is not possible to do so, there are workarounds:\n` +
1373 `\n` +
1374 `1. You can suppress the errors with @SuppressLint("<id>")\n`
1375
1376 if baselineFile.Valid() {
1377 cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path())
1378 cmd.FlagWithOutput("--update-baseline:api-lint ", updatedBaselineOutput)
1379
1380 msg += fmt.Sprintf(``+
1381 `2. You can update the baseline by executing the following\n`+
1382 ` command:\n`+
Anton Hansson3361a292020-05-11 15:38:31 +01001383 ` cp \\\n`+
1384 ` "'"$PWD"$'/%s" \\\n`+
1385 ` "'"$PWD"$'/%s"\n`+
Makoto Onuki88b99052020-04-27 17:22:16 -07001386 ` To submit the revised baseline.txt to the main Android\n`+
1387 ` repository, you will need approval.\n`, updatedBaselineOutput, baselineFile.Path())
1388 } else {
1389 msg += fmt.Sprintf(``+
1390 `2. You can add a baseline file of existing lint failures\n`+
1391 ` to the build rule of %s.\n`, d.Name())
1392 }
1393 // Note the message ends with a ' (single quote), to close the $' ... ' .
1394 msg += `************************************************************\n'`
1395
1396 cmd.FlagWithArg("--error-message:api-lint ", msg)
1397 }
1398
1399 // Add "check released" options. (Detect incompatible API changes from the last public release)
1400
Dan Willemsen9f435972020-05-28 15:28:00 -07001401 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") {
Makoto Onuki88b99052020-04-27 17:22:16 -07001402 doCheckReleased = true
1403
1404 if len(d.Javadoc.properties.Out) > 0 {
1405 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1406 }
1407
1408 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1409 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
1410 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
1411 updatedBaselineOutput := android.PathForModuleOut(ctx, "last_released_baseline.txt")
1412
1413 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
1414
1415 cmd.FlagWithInput("--check-compatibility:api:released ", apiFile)
1416 cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile)
1417
1418 if baselineFile.Valid() {
1419 cmd.FlagWithInput("--baseline:compatibility:released ", baselineFile.Path())
1420 cmd.FlagWithOutput("--update-baseline:compatibility:released ", updatedBaselineOutput)
1421 }
1422
1423 // Note this string includes quote ($' ... '), which decodes the "\n"s.
1424 msg := `$'\n******************************\n` +
1425 `You have tried to change the API from what has been previously released in\n` +
1426 `an SDK. Please fix the errors listed above.\n` +
1427 `******************************\n'`
1428
1429 cmd.FlagWithArg("--error-message:compatibility:released ", msg)
1430 }
1431
Colin Crossf1a035e2020-11-16 17:32:30 -08001432 impRule := android.NewRuleBuilder(pctx, ctx)
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001433 impCmd := impRule.Command()
Liz Kammer20ebfb42020-07-28 11:32:07 -07001434 // An action that copies the ninja generated rsp file to a new location. This allows us to
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001435 // add a large number of inputs to a file without exceeding bash command length limits (which
1436 // would happen if we use the WriteFile rule). The cp is needed because RuleBuilder sets the
1437 // rsp file to be ${output}.rsp.
1438 impCmd.Text("cp").FlagWithRspFileInputList("", cmd.GetImplicits()).Output(implicitsRsp)
Colin Crossf1a035e2020-11-16 17:32:30 -08001439 impRule.Build("implicitsGen", "implicits generation")
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001440 cmd.Implicit(implicitsRsp)
1441
Paul Duffin3ae29512020-04-08 18:18:03 +01001442 if generateStubs {
1443 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001444 BuiltTool("soong_zip").
Paul Duffin3ae29512020-04-08 18:18:03 +01001445 Flag("-write_if_changed").
1446 Flag("-jar").
1447 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
1448 FlagWithArg("-C ", stubsDir.String()).
1449 FlagWithArg("-D ", stubsDir.String())
1450 }
Jerome Gaillard0f599032019-10-10 19:29:11 +01001451
1452 if Bool(d.properties.Write_sdk_values) {
1453 d.metadataZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-metadata.zip")
1454 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001455 BuiltTool("soong_zip").
Jerome Gaillard0f599032019-10-10 19:29:11 +01001456 Flag("-write_if_changed").
1457 Flag("-d").
1458 FlagWithOutput("-o ", d.metadataZip).
1459 FlagWithArg("-C ", d.metadataDir.String()).
1460 FlagWithArg("-D ", d.metadataDir.String())
1461 }
1462
Makoto Onuki88b99052020-04-27 17:22:16 -07001463 // TODO: We don't really need two separate API files, but this is a reminiscence of how
1464 // we used to run metalava separately for API lint and the "last_released" check. Unify them.
1465 if doApiLint {
1466 rule.Command().Text("touch").Output(d.apiLintTimestamp)
1467 }
1468 if doCheckReleased {
1469 rule.Command().Text("touch").Output(d.checkLastReleasedApiTimestamp)
1470 }
1471
Colin Cross33961b52019-07-11 11:01:22 -07001472 rule.Restat()
1473
1474 zipSyncCleanupCmd(rule, srcJarDir)
1475
Colin Crossf1a035e2020-11-16 17:32:30 -08001476 rule.Build("metalava", "metalava merged")
Adrian Roos075eedc2019-10-10 12:07:03 +02001477
Dan Willemsen9f435972020-05-28 15:28:00 -07001478 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") {
Colin Cross33961b52019-07-11 11:01:22 -07001479
1480 if len(d.Javadoc.properties.Out) > 0 {
1481 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1482 }
1483
1484 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1485 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Adrian Roos14f75a92019-08-12 17:54:09 +02001486 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
Makoto Onuki5405a732020-04-16 17:02:40 -07001487
1488 if baselineFile.Valid() {
Makoto Onuki88b99052020-04-27 17:22:16 -07001489 ctx.PropertyErrorf("baseline_file", "current API check can't have a baseline file. (module %s)", ctx.ModuleName())
Makoto Onuki5405a732020-04-16 17:02:40 -07001490 }
Nan Zhang61819ce2018-05-04 18:49:16 -07001491
Nan Zhang2760dfc2018-08-24 17:32:54 +00001492 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Nan Zhang2760dfc2018-08-24 17:32:54 +00001493
Colin Crossf1a035e2020-11-16 17:32:30 -08001494 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross33961b52019-07-11 11:01:22 -07001495
Makoto Onuki5405a732020-04-16 17:02:40 -07001496 // Diff command line.
Makoto Onuki88b99052020-04-27 17:22:16 -07001497 // -F matches the closest "opening" line, such as "package android {"
1498 // and " public class Intent {".
Makoto Onuki5405a732020-04-16 17:02:40 -07001499 diff := `diff -u -F '{ *$'`
1500
Colin Cross33961b52019-07-11 11:01:22 -07001501 rule.Command().Text("( true")
Makoto Onuki5405a732020-04-16 17:02:40 -07001502 rule.Command().
1503 Text(diff).
1504 Input(apiFile).Input(d.apiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001505
Makoto Onuki5405a732020-04-16 17:02:40 -07001506 rule.Command().
1507 Text(diff).
1508 Input(removedApiFile).Input(d.removedApiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001509
1510 msg := fmt.Sprintf(`\n******************************\n`+
1511 `You have tried to change the API from what has been previously approved.\n\n`+
1512 `To make these errors go away, you have two choices:\n`+
Makoto Onuki5405a732020-04-16 17:02:40 -07001513 ` 1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)\n`+
1514 ` to the new methods, etc. shown in the above diff.\n\n`+
1515 ` 2. You can update current.txt and/or removed.txt by executing the following command:\n`+
Neil Fuller96d01612020-12-01 21:13:14 +00001516 ` m %s-update-current-api\n\n`+
Colin Cross33961b52019-07-11 11:01:22 -07001517 ` To submit the revised current.txt to the main Android repository,\n`+
1518 ` you will need approval.\n`+
1519 `******************************\n`, ctx.ModuleName())
1520
1521 rule.Command().
1522 Text("touch").Output(d.checkCurrentApiTimestamp).
1523 Text(") || (").
1524 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1525 Text("; exit 38").
1526 Text(")")
1527
Colin Crossf1a035e2020-11-16 17:32:30 -08001528 rule.Build("metalavaCurrentApiCheck", "check current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001529
1530 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001531
1532 // update API rule
Colin Crossf1a035e2020-11-16 17:32:30 -08001533 rule = android.NewRuleBuilder(pctx, ctx)
Colin Cross33961b52019-07-11 11:01:22 -07001534
1535 rule.Command().Text("( true")
1536
1537 rule.Command().
1538 Text("cp").Flag("-f").
1539 Input(d.apiFile).Flag(apiFile.String())
1540
1541 rule.Command().
1542 Text("cp").Flag("-f").
1543 Input(d.removedApiFile).Flag(removedApiFile.String())
1544
1545 msg = "failed to update public API"
1546
1547 rule.Command().
1548 Text("touch").Output(d.updateCurrentApiTimestamp).
1549 Text(") || (").
1550 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1551 Text("; exit 38").
1552 Text(")")
1553
Colin Crossf1a035e2020-11-16 17:32:30 -08001554 rule.Build("metalavaCurrentApiUpdate", "update current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001555 }
Nan Zhanga40da042018-08-01 12:48:00 -07001556
Pete Gillin581d6082018-10-22 15:55:04 +01001557 if String(d.properties.Check_nullability_warnings) != "" {
1558 if d.nullabilityWarningsFile == nil {
1559 ctx.PropertyErrorf("check_nullability_warnings",
1560 "Cannot specify check_nullability_warnings unless validating nullability")
1561 }
Colin Cross33961b52019-07-11 11:01:22 -07001562
1563 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
1564
Pete Gillin581d6082018-10-22 15:55:04 +01001565 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "check_nullability_warnings.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001566
Pete Gillin581d6082018-10-22 15:55:04 +01001567 msg := fmt.Sprintf(`\n******************************\n`+
1568 `The warnings encountered during nullability annotation validation did\n`+
1569 `not match the checked in file of expected warnings. The diffs are shown\n`+
1570 `above. You have two options:\n`+
1571 ` 1. Resolve the differences by editing the nullability annotations.\n`+
1572 ` 2. Update the file of expected warnings by running:\n`+
1573 ` cp %s %s\n`+
1574 ` and submitting the updated file as part of your change.`,
1575 d.nullabilityWarningsFile, checkNullabilityWarnings)
Colin Cross33961b52019-07-11 11:01:22 -07001576
Colin Crossf1a035e2020-11-16 17:32:30 -08001577 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross33961b52019-07-11 11:01:22 -07001578
1579 rule.Command().
1580 Text("(").
1581 Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile).
1582 Text("&&").
1583 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
1584 Text(") || (").
1585 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1586 Text("; exit 38").
1587 Text(")")
1588
Colin Crossf1a035e2020-11-16 17:32:30 -08001589 rule.Build("nullabilityWarningsCheck", "nullability warnings check")
Pete Gillin581d6082018-10-22 15:55:04 +01001590 }
Nan Zhang581fd212018-01-10 16:06:12 -08001591}
Dan Willemsencc090972018-02-26 14:33:31 -08001592
Nan Zhanga40da042018-08-01 12:48:00 -07001593//
Nan Zhangf4936b02018-08-01 15:00:28 -07001594// Exported Droiddoc Directory
Nan Zhanga40da042018-08-01 12:48:00 -07001595//
Dan Willemsencc090972018-02-26 14:33:31 -08001596var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
Nan Zhangf4936b02018-08-01 15:00:28 -07001597var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
Pete Gillin77167902018-09-19 18:16:26 +01001598var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
Nan Zhang9c69a122018-08-22 10:22:08 -07001599var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
Dan Willemsencc090972018-02-26 14:33:31 -08001600
Nan Zhangf4936b02018-08-01 15:00:28 -07001601type ExportedDroiddocDirProperties struct {
1602 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -08001603 Path *string
1604}
1605
Nan Zhangf4936b02018-08-01 15:00:28 -07001606type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -08001607 android.ModuleBase
1608
Nan Zhangf4936b02018-08-01 15:00:28 -07001609 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -08001610
1611 deps android.Paths
1612 dir android.Path
1613}
1614
Colin Crossa3002fc2019-07-08 16:48:04 -07001615// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -07001616func ExportedDroiddocDirFactory() android.Module {
1617 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -08001618 module.AddProperties(&module.properties)
1619 android.InitAndroidModule(module)
1620 return module
1621}
1622
Nan Zhangf4936b02018-08-01 15:00:28 -07001623func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -08001624
Nan Zhangf4936b02018-08-01 15:00:28 -07001625func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -08001626 path := String(d.properties.Path)
1627 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -08001628 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Dan Willemsencc090972018-02-26 14:33:31 -08001629}
Nan Zhangb2b33de2018-02-23 11:18:47 -08001630
1631//
1632// Defaults
1633//
1634type DocDefaults struct {
1635 android.ModuleBase
1636 android.DefaultsModuleBase
1637}
1638
Nan Zhangb2b33de2018-02-23 11:18:47 -08001639func DocDefaultsFactory() android.Module {
1640 module := &DocDefaults{}
1641
1642 module.AddProperties(
1643 &JavadocProperties{},
1644 &DroiddocProperties{},
1645 )
1646
1647 android.InitDefaultsModule(module)
1648
1649 return module
1650}
Nan Zhang1598a9e2018-09-04 17:14:32 -07001651
1652func StubsDefaultsFactory() android.Module {
1653 module := &DocDefaults{}
1654
1655 module.AddProperties(
1656 &JavadocProperties{},
1657 &DroidstubsProperties{},
1658 )
1659
1660 android.InitDefaultsModule(module)
1661
1662 return module
1663}
Colin Cross33961b52019-07-11 11:01:22 -07001664
1665func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
1666 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
1667
1668 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1669 rule.Command().Text("mkdir -p").Text(srcJarDir.String())
1670 srcJarList := srcJarDir.Join(ctx, "list")
1671
1672 rule.Temporary(srcJarList)
1673
Colin Crossf1a035e2020-11-16 17:32:30 -08001674 rule.Command().BuiltTool("zipsync").
Colin Cross33961b52019-07-11 11:01:22 -07001675 FlagWithArg("-d ", srcJarDir.String()).
1676 FlagWithOutput("-l ", srcJarList).
1677 FlagWithArg("-f ", `"*.java"`).
1678 Inputs(srcJars)
1679
1680 return srcJarList
1681}
1682
1683func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
1684 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1685}
Paul Duffin91547182019-11-12 19:39:36 +00001686
1687var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
1688
1689type PrebuiltStubsSourcesProperties struct {
1690 Srcs []string `android:"path"`
1691}
1692
1693type PrebuiltStubsSources struct {
1694 android.ModuleBase
1695 android.DefaultableModuleBase
1696 prebuilt android.Prebuilt
1697 android.SdkBase
1698
1699 properties PrebuiltStubsSourcesProperties
1700
Paul Duffin91547182019-11-12 19:39:36 +00001701 stubsSrcJar android.ModuleOutPath
1702}
1703
Paul Duffin9b478b02019-12-10 13:41:51 +00001704func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) {
1705 switch tag {
1706 case "":
1707 return android.Paths{p.stubsSrcJar}, nil
1708 default:
1709 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1710 }
1711}
1712
Paul Duffin0f8faff2020-05-20 16:18:00 +01001713func (d *PrebuiltStubsSources) StubsSrcJar() android.Path {
1714 return d.stubsSrcJar
1715}
1716
Paul Duffin91547182019-11-12 19:39:36 +00001717func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin9b478b02019-12-10 13:41:51 +00001718 p.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1719
Paul Duffin1a393322020-11-18 16:36:47 +00001720 if len(p.properties.Srcs) != 1 {
1721 ctx.PropertyErrorf("srcs", "must only specify one directory path, contains %d paths", len(p.properties.Srcs))
1722 return
1723 }
1724
1725 localSrcDir := p.properties.Srcs[0]
1726 // Although PathForModuleSrc can return nil if either the path doesn't exist or
1727 // the path components are invalid it won't in this case because no components
1728 // are specified and the module directory must exist in order to get this far.
1729 srcDir := android.PathForModuleSrc(ctx).(android.SourcePath).Join(ctx, localSrcDir)
1730
1731 // Glob the contents of the directory just in case the directory does not exist.
1732 srcGlob := localSrcDir + "/**/*"
1733 srcPaths := android.PathsForModuleSrc(ctx, []string{srcGlob})
Paul Duffin9b478b02019-12-10 13:41:51 +00001734
Colin Crossf1a035e2020-11-16 17:32:30 -08001735 rule := android.NewRuleBuilder(pctx, ctx)
Paul Duffin1a393322020-11-18 16:36:47 +00001736 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001737 BuiltTool("soong_zip").
Paul Duffin9b478b02019-12-10 13:41:51 +00001738 Flag("-write_if_changed").
1739 Flag("-jar").
Paul Duffin1a393322020-11-18 16:36:47 +00001740 FlagWithOutput("-o ", p.stubsSrcJar).
1741 FlagWithArg("-C ", srcDir.String()).
1742 FlagWithRspFileInputList("-r ", srcPaths)
Paul Duffin9b478b02019-12-10 13:41:51 +00001743
1744 rule.Restat()
1745
Colin Crossf1a035e2020-11-16 17:32:30 -08001746 rule.Build("zip src", "Create srcjar from prebuilt source")
Paul Duffin91547182019-11-12 19:39:36 +00001747}
1748
1749func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
1750 return &p.prebuilt
1751}
1752
1753func (p *PrebuiltStubsSources) Name() string {
1754 return p.prebuilt.Name(p.ModuleBase.Name())
1755}
1756
Paul Duffin91547182019-11-12 19:39:36 +00001757// prebuilt_stubs_sources imports a set of java source files as if they were
1758// generated by droidstubs.
1759//
1760// By default, a prebuilt_stubs_sources has a single variant that expects a
1761// set of `.java` files generated by droidstubs.
1762//
1763// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1764// for host modules.
1765//
1766// Intended only for use by sdk snapshots.
1767func PrebuiltStubsSourcesFactory() android.Module {
1768 module := &PrebuiltStubsSources{}
1769
1770 module.AddProperties(&module.properties)
1771
1772 android.InitPrebuiltModule(module, &module.properties.Srcs)
1773 android.InitSdkAwareModule(module)
1774 InitDroiddocModule(module, android.HostAndDeviceSupported)
1775 return module
1776}