blob: 1c2c44736d451cd58abbef7d48e2eb8d31454f44 [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
Paul Duffin13879572019-11-28 14:31:38 +000022 "github.com/google/blueprint"
Jeongik Cha6bd33c12019-06-25 16:26:18 +090023 "github.com/google/blueprint/proptools"
Nan Zhang581fd212018-01-10 16:06:12 -080024
Colin Crossab054432019-07-15 16:13:59 -070025 "android/soong/android"
26 "android/soong/java/config"
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)
Paul Duffin255f18e2019-12-13 11:22:16 +000032
33 // Register sdk member type.
34 android.RegisterSdkMemberType(&droidStubsSdkMemberType{
35 SdkMemberTypeBase: android.SdkMemberTypeBase{
36 PropertyName: "stubs_sources",
Paul Duffine6029182019-12-16 17:43:48 +000037 // stubs_sources can be used with sdk to provide the source stubs for APIs provided by
38 // the APEX.
39 SupportsSdk: true,
Paul Duffin255f18e2019-12-13 11:22:16 +000040 },
41 })
Nan Zhang581fd212018-01-10 16:06:12 -080042}
43
Paul Duffin884363e2019-12-19 10:21:09 +000044func RegisterDocsBuildComponents(ctx android.RegistrationContext) {
45 ctx.RegisterModuleType("doc_defaults", DocDefaultsFactory)
46
47 ctx.RegisterModuleType("droiddoc", DroiddocFactory)
48 ctx.RegisterModuleType("droiddoc_host", DroiddocHostFactory)
49 ctx.RegisterModuleType("droiddoc_exported_dir", ExportedDroiddocDirFactory)
50 ctx.RegisterModuleType("javadoc", JavadocFactory)
51 ctx.RegisterModuleType("javadoc_host", JavadocHostFactory)
52}
53
54func RegisterStubsBuildComponents(ctx android.RegistrationContext) {
55 ctx.RegisterModuleType("stubs_defaults", StubsDefaultsFactory)
56
57 ctx.RegisterModuleType("droidstubs", DroidstubsFactory)
58 ctx.RegisterModuleType("droidstubs_host", DroidstubsHostFactory)
59
60 ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory)
61}
62
Colin Crossa1ce2a02018-06-20 15:19:39 -070063var (
64 srcsLibTag = dependencyTag{name: "sources from javalib"}
65)
66
Nan Zhang581fd212018-01-10 16:06:12 -080067type JavadocProperties struct {
68 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
69 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080070 Srcs []string `android:"path,arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080071
72 // list of directories rooted at the Android.bp file that will
73 // be added to the search paths for finding source files when passing package names.
Nan Zhangb2b33de2018-02-23 11:18:47 -080074 Local_sourcepaths []string
Nan Zhang581fd212018-01-10 16:06:12 -080075
76 // list of source files that should not be used to build the Java module.
77 // This is most useful in the arch/multilib variants to remove non-common files
78 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -080079 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080080
Jiyong Parkc6ddccf2019-09-13 20:56:14 +090081 // list of package names that should actually be used. If this property is left unspecified,
82 // all the sources from the srcs property is used.
83 Filter_packages []string
84
Nan Zhangb2b33de2018-02-23 11:18:47 -080085 // list of java libraries that will be in the classpath.
Nan Zhang581fd212018-01-10 16:06:12 -080086 Libs []string `android:"arch_variant"`
87
88 // If set to false, don't allow this module(-docs.zip) to be exported. Defaults to true.
Nan Zhangb2b33de2018-02-23 11:18:47 -080089 Installable *bool
Nan Zhang581fd212018-01-10 16:06:12 -080090
Paul Duffine25c6442019-10-11 13:50:28 +010091 // if not blank, set to the version of the sdk to compile against.
92 // Defaults to compiling against the current platform.
Nan Zhang581fd212018-01-10 16:06:12 -080093 Sdk_version *string `android:"arch_variant"`
Jiyong Park1e440682018-05-23 18:42:04 +090094
Paul Duffine25c6442019-10-11 13:50:28 +010095 // When targeting 1.9 and above, override the modules to use with --system,
96 // otherwise provides defaults libraries to add to the bootclasspath.
97 // Defaults to "none"
98 System_modules *string
99
Jiyong Park1e440682018-05-23 18:42:04 +0900100 Aidl struct {
101 // Top level directories to pass to aidl tool
102 Include_dirs []string
103
104 // Directories rooted at the Android.bp file to pass to aidl tool
105 Local_include_dirs []string
106 }
Nan Zhang357466b2018-04-17 17:38:36 -0700107
108 // If not blank, set the java version passed to javadoc as -source
109 Java_version *string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700110
111 // local files that are used within user customized droiddoc options.
Colin Cross27b922f2019-03-04 22:35:41 -0800112 Arg_files []string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -0700113
114 // user customized droiddoc args.
115 // Available variables for substitution:
116 //
117 // $(location <label>): the path to the arg_files with name <label>
Colin Crosse4a05842019-05-28 10:17:14 -0700118 // $$: a literal $
Nan Zhang1598a9e2018-09-04 17:14:32 -0700119 Args *string
120
121 // names of the output files used in args that will be generated
122 Out []string
Nan Zhang581fd212018-01-10 16:06:12 -0800123}
124
Nan Zhang61819ce2018-05-04 18:49:16 -0700125type ApiToCheck struct {
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900126 // path to the API txt file that the new API extracted from source code is checked
127 // against. The path can be local to the module or from other module (via :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800128 Api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700129
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900130 // path to the API txt file that the new @removed API extractd from source code is
131 // checked against. The path can be local to the module or from other module (via
132 // :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800133 Removed_api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700134
Adrian Roos14f75a92019-08-12 17:54:09 +0200135 // If not blank, path to the baseline txt file for approved API check violations.
136 Baseline_file *string `android:"path"`
137
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900138 // Arguments to the apicheck tool.
Nan Zhang61819ce2018-05-04 18:49:16 -0700139 Args *string
140}
141
Nan Zhang581fd212018-01-10 16:06:12 -0800142type DroiddocProperties struct {
143 // directory relative to top of the source tree that contains doc templates files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800144 Custom_template *string
Nan Zhang581fd212018-01-10 16:06:12 -0800145
Nan Zhanga40da042018-08-01 12:48:00 -0700146 // directories under current module source which contains html/jd files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800147 Html_dirs []string
Nan Zhang581fd212018-01-10 16:06:12 -0800148
149 // set a value in the Clearsilver hdf namespace.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800150 Hdf []string
Nan Zhang581fd212018-01-10 16:06:12 -0800151
152 // proofread file contains all of the text content of the javadocs concatenated into one file,
153 // suitable for spell-checking and other goodness.
Colin Crossab054432019-07-15 16:13:59 -0700154 Proofread_file *string
Nan Zhang581fd212018-01-10 16:06:12 -0800155
156 // a todo file lists the program elements that are missing documentation.
157 // At some point, this might be improved to show more warnings.
Colin Cross27b922f2019-03-04 22:35:41 -0800158 Todo_file *string `android:"path"`
Nan Zhangb2b33de2018-02-23 11:18:47 -0800159
160 // directory under current module source that provide additional resources (images).
161 Resourcesdir *string
162
163 // resources output directory under out/soong/.intermediates.
164 Resourcesoutdir *string
Nan Zhang581fd212018-01-10 16:06:12 -0800165
Nan Zhange2ba5d42018-07-11 15:16:55 -0700166 // if set to true, collect the values used by the Dev tools and
167 // write them in files packaged with the SDK. Defaults to false.
168 Write_sdk_values *bool
169
170 // index.html under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800171 Static_doc_index_redirect *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700172
173 // source.properties under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800174 Static_doc_properties *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700175
Nan Zhang581fd212018-01-10 16:06:12 -0800176 // a list of files under current module source dir which contains known tags in Java sources.
177 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -0800178 Knowntags []string `android:"path"`
Nan Zhang28c68b92018-03-13 16:17:01 -0700179
Nan Zhang28c68b92018-03-13 16:17:01 -0700180 // the generated public API filename by Doclava.
181 Api_filename *string
182
Nan Zhang28c68b92018-03-13 16:17:01 -0700183 // the generated removed API filename by Doclava.
184 Removed_api_filename *string
185
David Brazdilaac0c3c2018-04-24 16:23:29 +0100186 // the generated removed Dex API filename by Doclava.
187 Removed_dex_api_filename *string
188
Nan Zhang853f4202018-04-12 16:55:56 -0700189 // if set to false, don't allow droiddoc to generate stubs source files. Defaults to true.
190 Create_stubs *bool
Nan Zhang61819ce2018-05-04 18:49:16 -0700191
192 Check_api struct {
193 Last_released ApiToCheck
194
195 Current ApiToCheck
Inseob Kim38449af2019-02-28 14:24:05 +0900196
197 // do not perform API check against Last_released, in the case that both two specified API
198 // files by Last_released are modules which don't exist.
199 Ignore_missing_latest_api *bool `blueprint:"mutated"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700200 }
Nan Zhang79614d12018-04-19 18:03:39 -0700201
Nan Zhang1598a9e2018-09-04 17:14:32 -0700202 // if set to true, generate docs through Dokka instead of Doclava.
203 Dokka_enabled *bool
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000204
205 // Compat config XML. Generates compat change documentation if set.
206 Compat_config *string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -0700207}
208
209type DroidstubsProperties struct {
Nan Zhang199645c2018-09-19 12:40:06 -0700210 // the generated public API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700211 Api_filename *string
212
Nan Zhang199645c2018-09-19 12:40:06 -0700213 // the generated removed API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700214 Removed_api_filename *string
215
Nan Zhang199645c2018-09-19 12:40:06 -0700216 // the generated removed Dex API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700217 Removed_dex_api_filename *string
218
Nan Zhang1598a9e2018-09-04 17:14:32 -0700219 Check_api struct {
220 Last_released ApiToCheck
221
222 Current ApiToCheck
Inseob Kim38449af2019-02-28 14:24:05 +0900223
224 // do not perform API check against Last_released, in the case that both two specified API
225 // files by Last_released are modules which don't exist.
226 Ignore_missing_latest_api *bool `blueprint:"mutated"`
Adrian Roos075eedc2019-10-10 12:07:03 +0200227
228 Api_lint struct {
229 Enabled *bool
230
231 // If set, performs api_lint on any new APIs not found in the given signature file
232 New_since *string `android:"path"`
233
234 // If not blank, path to the baseline txt file for approved API lint violations.
235 Baseline_file *string `android:"path"`
236 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700237 }
Nan Zhang79614d12018-04-19 18:03:39 -0700238
239 // user can specify the version of previous released API file in order to do compatibility check.
Colin Cross27b922f2019-03-04 22:35:41 -0800240 Previous_api *string `android:"path"`
Nan Zhang79614d12018-04-19 18:03:39 -0700241
242 // is set to true, Metalava will allow framework SDK to contain annotations.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700243 Annotations_enabled *bool
Nan Zhang79614d12018-04-19 18:03:39 -0700244
Pete Gillin77167902018-09-19 18:16:26 +0100245 // 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 -0700246 Merge_annotations_dirs []string
Nan Zhang86d2d552018-08-09 15:33:27 -0700247
Pete Gillin77167902018-09-19 18:16:26 +0100248 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
249 Merge_inclusion_annotations_dirs []string
250
Pete Gillinc382a562018-11-14 18:45:46 +0000251 // a file containing a list of classes to do nullability validation for.
252 Validate_nullability_from_list *string
253
Pete Gillin581d6082018-10-22 15:55:04 +0100254 // a file containing expected warnings produced by validation of nullability annotations.
255 Check_nullability_warnings *string
256
Nan Zhang1598a9e2018-09-04 17:14:32 -0700257 // if set to true, allow Metalava to generate doc_stubs source files. Defaults to false.
258 Create_doc_stubs *bool
Nan Zhang9c69a122018-08-22 10:22:08 -0700259
260 // is set to true, Metalava will allow framework SDK to contain API levels annotations.
261 Api_levels_annotations_enabled *bool
262
263 // the dirs which Metalava extracts API levels annotations from.
264 Api_levels_annotations_dirs []string
265
266 // if set to true, collect the values used by the Dev tools and
267 // write them in files packaged with the SDK. Defaults to false.
268 Write_sdk_values *bool
Nan Zhang71bbe632018-09-17 14:32:21 -0700269
270 // If set to true, .xml based public API file will be also generated, and
271 // JDiff tool will be invoked to genreate javadoc files. Defaults to false.
272 Jdiff_enabled *bool
Nan Zhang581fd212018-01-10 16:06:12 -0800273}
274
Nan Zhanga40da042018-08-01 12:48:00 -0700275//
276// Common flags passed down to build rule
277//
278type droiddocBuilderFlags struct {
Nan Zhang86d2d552018-08-09 15:33:27 -0700279 bootClasspathArgs string
280 classpathArgs string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700281 sourcepathArgs string
Nan Zhang86d2d552018-08-09 15:33:27 -0700282 dokkaClasspathArgs string
283 aidlFlags string
Colin Cross3047fa22019-04-18 10:56:44 -0700284 aidlDeps android.Paths
Nan Zhanga40da042018-08-01 12:48:00 -0700285
Nan Zhanga40da042018-08-01 12:48:00 -0700286 doclavaStubsFlags string
Nan Zhang86d2d552018-08-09 15:33:27 -0700287 doclavaDocsFlags string
Nan Zhanga40da042018-08-01 12:48:00 -0700288 postDoclavaCmds string
Nan Zhanga40da042018-08-01 12:48:00 -0700289}
290
291func InitDroiddocModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
292 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
293 android.InitDefaultableModule(module)
294}
295
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200296func apiCheckEnabled(ctx android.ModuleContext, apiToCheck ApiToCheck, apiVersionTag string) bool {
297 if ctx.Config().IsEnvTrue("WITHOUT_CHECK_API") {
298 return false
299 } else if String(apiToCheck.Api_file) != "" && String(apiToCheck.Removed_api_file) != "" {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700300 return true
301 } else if String(apiToCheck.Api_file) != "" {
302 panic("for " + apiVersionTag + " removed_api_file has to be non-empty!")
303 } else if String(apiToCheck.Removed_api_file) != "" {
304 panic("for " + apiVersionTag + " api_file has to be non-empty!")
305 }
306
307 return false
308}
309
Inseob Kim38449af2019-02-28 14:24:05 +0900310func ignoreMissingModules(ctx android.BottomUpMutatorContext, apiToCheck *ApiToCheck) {
311 api_file := String(apiToCheck.Api_file)
312 removed_api_file := String(apiToCheck.Removed_api_file)
313
314 api_module := android.SrcIsModule(api_file)
315 removed_api_module := android.SrcIsModule(removed_api_file)
316
317 if api_module == "" || removed_api_module == "" {
318 return
319 }
320
321 if ctx.OtherModuleExists(api_module) || ctx.OtherModuleExists(removed_api_module) {
322 return
323 }
324
325 apiToCheck.Api_file = nil
326 apiToCheck.Removed_api_file = nil
327}
328
Nan Zhang1598a9e2018-09-04 17:14:32 -0700329type ApiFilePath interface {
330 ApiFilePath() android.Path
331}
332
Nan Zhanga40da042018-08-01 12:48:00 -0700333//
334// Javadoc
335//
Nan Zhang581fd212018-01-10 16:06:12 -0800336type Javadoc struct {
337 android.ModuleBase
338 android.DefaultableModuleBase
339
340 properties JavadocProperties
341
342 srcJars android.Paths
343 srcFiles android.Paths
344 sourcepaths android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700345 argFiles android.Paths
346
347 args string
Nan Zhang581fd212018-01-10 16:06:12 -0800348
Nan Zhangccff0f72018-03-08 17:26:16 -0800349 docZip android.WritablePath
350 stubsSrcJar android.WritablePath
Nan Zhang581fd212018-01-10 16:06:12 -0800351}
352
Colin Cross41955e82019-05-29 14:40:35 -0700353func (j *Javadoc) OutputFiles(tag string) (android.Paths, error) {
354 switch tag {
355 case "":
356 return android.Paths{j.stubsSrcJar}, nil
Colin Crosse68e5542019-08-12 13:11:40 -0700357 case ".docs.zip":
358 return android.Paths{j.docZip}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700359 default:
360 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
361 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800362}
363
Colin Crossa3002fc2019-07-08 16:48:04 -0700364// javadoc converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800365func JavadocFactory() android.Module {
366 module := &Javadoc{}
367
368 module.AddProperties(&module.properties)
369
370 InitDroiddocModule(module, android.HostAndDeviceSupported)
371 return module
372}
373
Colin Crossa3002fc2019-07-08 16:48:04 -0700374// javadoc_host converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800375func JavadocHostFactory() android.Module {
376 module := &Javadoc{}
377
378 module.AddProperties(&module.properties)
379
380 InitDroiddocModule(module, android.HostSupported)
381 return module
382}
383
Colin Cross41955e82019-05-29 14:40:35 -0700384var _ android.OutputFileProducer = (*Javadoc)(nil)
Nan Zhang581fd212018-01-10 16:06:12 -0800385
Jiyong Park6a927c42020-01-21 02:03:43 +0900386func (j *Javadoc) sdkVersion() sdkSpec {
387 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700388}
389
Paul Duffine25c6442019-10-11 13:50:28 +0100390func (j *Javadoc) systemModules() string {
391 return proptools.String(j.properties.System_modules)
392}
393
Jiyong Park6a927c42020-01-21 02:03:43 +0900394func (j *Javadoc) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700395 return j.sdkVersion()
396}
397
Jiyong Park6a927c42020-01-21 02:03:43 +0900398func (j *Javadoc) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700399 return j.sdkVersion()
400}
401
Nan Zhang581fd212018-01-10 16:06:12 -0800402func (j *Javadoc) addDeps(ctx android.BottomUpMutatorContext) {
403 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100404 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700405 if sdkDep.useDefaultLibs {
406 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
407 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
408 if sdkDep.hasFrameworkLibs() {
409 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Nan Zhang357466b2018-04-17 17:38:36 -0700410 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700411 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700412 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100413 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700414 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Nan Zhang581fd212018-01-10 16:06:12 -0800415 }
416 }
417
Colin Cross42d48b72018-08-29 14:10:52 -0700418 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800419}
420
Nan Zhanga40da042018-08-01 12:48:00 -0700421func (j *Javadoc) collectAidlFlags(ctx android.ModuleContext, deps deps) droiddocBuilderFlags {
422 var flags droiddocBuilderFlags
Jiyong Park1e440682018-05-23 18:42:04 +0900423
Colin Cross3047fa22019-04-18 10:56:44 -0700424 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Jiyong Park1e440682018-05-23 18:42:04 +0900425
426 return flags
427}
428
429func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700430 aidlIncludeDirs android.Paths) (string, android.Paths) {
Jiyong Park1e440682018-05-23 18:42:04 +0900431
432 aidlIncludes := android.PathsForModuleSrc(ctx, j.properties.Aidl.Local_include_dirs)
433 aidlIncludes = append(aidlIncludes, android.PathsForSource(ctx, j.properties.Aidl.Include_dirs)...)
434
435 var flags []string
Colin Cross3047fa22019-04-18 10:56:44 -0700436 var deps android.Paths
437
Jiyong Park1e440682018-05-23 18:42:04 +0900438 if aidlPreprocess.Valid() {
439 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700440 deps = append(deps, aidlPreprocess.Path())
Jiyong Park1e440682018-05-23 18:42:04 +0900441 } else {
442 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
443 }
444
445 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
446 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
447 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
448 flags = append(flags, "-I"+src.String())
449 }
450
Colin Cross3047fa22019-04-18 10:56:44 -0700451 return strings.Join(flags, " "), deps
Jiyong Park1e440682018-05-23 18:42:04 +0900452}
453
Jiyong Parkd90d7412019-08-20 22:49:19 +0900454// TODO: remove the duplication between this and the one in gen.go
Jiyong Park1e440682018-05-23 18:42:04 +0900455func (j *Javadoc) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Nan Zhanga40da042018-08-01 12:48:00 -0700456 flags droiddocBuilderFlags) android.Paths {
Jiyong Park1e440682018-05-23 18:42:04 +0900457
458 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Crossc0806172019-06-14 18:51:47 -0700459 var aidlSrcs android.Paths
Jiyong Park1e440682018-05-23 18:42:04 +0900460
Jiyong Park1112c4c2019-08-16 21:12:10 +0900461 aidlIncludeFlags := genAidlIncludeFlags(srcFiles)
462
Jiyong Park1e440682018-05-23 18:42:04 +0900463 for _, srcFile := range srcFiles {
464 switch srcFile.Ext() {
465 case ".aidl":
Colin Crossc0806172019-06-14 18:51:47 -0700466 aidlSrcs = append(aidlSrcs, srcFile)
Jiyong Parkd90d7412019-08-20 22:49:19 +0900467 case ".logtags":
468 javaFile := genLogtags(ctx, srcFile)
469 outSrcFiles = append(outSrcFiles, javaFile)
Jiyong Park1e440682018-05-23 18:42:04 +0900470 default:
471 outSrcFiles = append(outSrcFiles, srcFile)
472 }
473 }
474
Colin Crossc0806172019-06-14 18:51:47 -0700475 // Process all aidl files together to support sharding them into one or more rules that produce srcjars.
476 if len(aidlSrcs) > 0 {
477 srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags+aidlIncludeFlags, flags.aidlDeps)
478 outSrcFiles = append(outSrcFiles, srcJarFiles...)
479 }
480
Jiyong Park1e440682018-05-23 18:42:04 +0900481 return outSrcFiles
482}
483
Nan Zhang581fd212018-01-10 16:06:12 -0800484func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
485 var deps deps
486
Colin Cross83bb3162018-06-25 15:48:06 -0700487 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800488 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700489 ctx.AddMissingDependencies(sdkDep.bootclasspath)
490 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Nan Zhang581fd212018-01-10 16:06:12 -0800491 } else if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700492 deps.bootClasspath = append(deps.bootClasspath, sdkDep.jars...)
Anton Hansson26bf49b2020-02-08 20:26:29 +0000493 deps.aidlPreprocess = sdkDep.aidl
494 } else {
495 deps.aidlPreprocess = sdkDep.aidl
Nan Zhang581fd212018-01-10 16:06:12 -0800496 }
497
498 ctx.VisitDirectDeps(func(module android.Module) {
499 otherName := ctx.OtherModuleName(module)
500 tag := ctx.OtherModuleDependencyTag(module)
501
Colin Cross2d24c1b2018-05-23 10:59:18 -0700502 switch tag {
503 case bootClasspathTag:
504 if dep, ok := module.(Dependency); ok {
Nan Zhang581fd212018-01-10 16:06:12 -0800505 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars()...)
Paul Duffin83a2d962019-11-19 19:44:10 +0000506 } else if sm, ok := module.(SystemModulesProvider); ok {
Paul Duffine25c6442019-10-11 13:50:28 +0100507 // A system modules dependency has been added to the bootclasspath
508 // so add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +0000509 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700510 } else {
511 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
512 }
513 case libTag:
514 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800515 case SdkLibraryDependency:
516 deps.classpath = append(deps.classpath, dep.SdkImplementationJars(ctx, j.sdkVersion())...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700517 case Dependency:
Sundong Ahnba493602018-11-20 17:36:35 +0900518 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park19a7f252019-07-10 16:59:31 +0900519 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700520 case android.SourceFileProducer:
Nan Zhang581fd212018-01-10 16:06:12 -0800521 checkProducesJars(ctx, dep)
522 deps.classpath = append(deps.classpath, dep.Srcs()...)
Nan Zhang581fd212018-01-10 16:06:12 -0800523 default:
524 ctx.ModuleErrorf("depends on non-java module %q", otherName)
525 }
Colin Cross6cef4812019-10-17 14:23:50 -0700526 case java9LibTag:
527 switch dep := module.(type) {
528 case Dependency:
529 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
530 default:
531 ctx.ModuleErrorf("depends on non-java module %q", otherName)
532 }
Nan Zhang357466b2018-04-17 17:38:36 -0700533 case systemModulesTag:
534 if deps.systemModules != nil {
535 panic("Found two system module dependencies")
536 }
Paul Duffin83a2d962019-11-19 19:44:10 +0000537 sm := module.(SystemModulesProvider)
538 outputDir, outputDeps := sm.OutputDirAndDeps()
539 deps.systemModules = &systemModules{outputDir, outputDeps}
Nan Zhang581fd212018-01-10 16:06:12 -0800540 }
541 })
542 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
543 // may contain filegroup or genrule.
Colin Cross8a497952019-03-05 22:25:09 -0800544 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900545
546 filterByPackage := func(srcs []android.Path, filterPackages []string) []android.Path {
547 if filterPackages == nil {
548 return srcs
549 }
550 filtered := []android.Path{}
551 for _, src := range srcs {
552 if src.Ext() != ".java" {
553 // Don't filter-out non-Java (=generated sources) by package names. This is not ideal,
554 // but otherwise metalava emits stub sources having references to the generated AIDL classes
555 // in filtered-out pacages (e.g. com.android.internal.*).
556 // TODO(b/141149570) We need to fix this by introducing default private constructors or
557 // fixing metalava to not emit constructors having references to unknown classes.
558 filtered = append(filtered, src)
559 continue
560 }
561 packageName := strings.ReplaceAll(filepath.Dir(src.Rel()), "/", ".")
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800562 if android.HasAnyPrefix(packageName, filterPackages) {
563 filtered = append(filtered, src)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900564 }
565 }
566 return filtered
567 }
568 srcFiles = filterByPackage(srcFiles, j.properties.Filter_packages)
569
Nan Zhanga40da042018-08-01 12:48:00 -0700570 flags := j.collectAidlFlags(ctx, deps)
Jiyong Park1e440682018-05-23 18:42:04 +0900571 srcFiles = j.genSources(ctx, srcFiles, flags)
Nan Zhang581fd212018-01-10 16:06:12 -0800572
573 // srcs may depend on some genrule output.
574 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800575 j.srcJars = append(j.srcJars, deps.srcJars...)
576
Nan Zhang581fd212018-01-10 16:06:12 -0800577 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800578 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800579
Nan Zhang9c69a122018-08-22 10:22:08 -0700580 if j.properties.Local_sourcepaths == nil && len(j.srcFiles) > 0 {
Nan Zhang581fd212018-01-10 16:06:12 -0800581 j.properties.Local_sourcepaths = append(j.properties.Local_sourcepaths, ".")
582 }
583 j.sourcepaths = android.PathsForModuleSrc(ctx, j.properties.Local_sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800584
Colin Cross8a497952019-03-05 22:25:09 -0800585 j.argFiles = android.PathsForModuleSrc(ctx, j.properties.Arg_files)
Paul Duffin99e4a502019-02-11 15:38:42 +0000586 argFilesMap := map[string]string{}
587 argFileLabels := []string{}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700588
Paul Duffin99e4a502019-02-11 15:38:42 +0000589 for _, label := range j.properties.Arg_files {
Colin Cross8a497952019-03-05 22:25:09 -0800590 var paths = android.PathsForModuleSrc(ctx, []string{label})
Paul Duffin99e4a502019-02-11 15:38:42 +0000591 if _, exists := argFilesMap[label]; !exists {
592 argFilesMap[label] = strings.Join(paths.Strings(), " ")
593 argFileLabels = append(argFileLabels, label)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700594 } else {
595 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000596 label, argFilesMap[label], paths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700597 }
598 }
599
600 var err error
Colin Cross15638152019-07-11 11:11:35 -0700601 j.args, err = android.Expand(String(j.properties.Args), func(name string) (string, error) {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700602 if strings.HasPrefix(name, "location ") {
603 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Paul Duffin99e4a502019-02-11 15:38:42 +0000604 if paths, ok := argFilesMap[label]; ok {
Colin Cross15638152019-07-11 11:11:35 -0700605 return paths, nil
Nan Zhang1598a9e2018-09-04 17:14:32 -0700606 } else {
Colin Cross15638152019-07-11 11:11:35 -0700607 return "", fmt.Errorf("unknown location label %q, expecting one of %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000608 label, strings.Join(argFileLabels, ", "))
Nan Zhang1598a9e2018-09-04 17:14:32 -0700609 }
610 } else if name == "genDir" {
Colin Cross15638152019-07-11 11:11:35 -0700611 return android.PathForModuleGen(ctx).String(), nil
Nan Zhang1598a9e2018-09-04 17:14:32 -0700612 }
Colin Cross15638152019-07-11 11:11:35 -0700613 return "", fmt.Errorf("unknown variable '$(%s)'", name)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700614 })
615
616 if err != nil {
617 ctx.PropertyErrorf("args", "%s", err.Error())
618 }
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
637 rule := android.NewRuleBuilder()
638
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().
655 BuiltTool(ctx, "soong_zip").
656 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
666 rule.Build(pctx, ctx, "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
675 properties DroiddocProperties
676 apiFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700677 privateApiFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700678 removedApiFile android.WritablePath
679 removedDexApiFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700680
681 checkCurrentApiTimestamp android.WritablePath
682 updateCurrentApiTimestamp android.WritablePath
683 checkLastReleasedApiTimestamp android.WritablePath
684
Nan Zhanga40da042018-08-01 12:48:00 -0700685 apiFilePath android.Path
686}
687
Colin Crossa3002fc2019-07-08 16:48:04 -0700688// droiddoc converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700689func DroiddocFactory() android.Module {
690 module := &Droiddoc{}
691
692 module.AddProperties(&module.properties,
693 &module.Javadoc.properties)
694
695 InitDroiddocModule(module, android.HostAndDeviceSupported)
696 return module
697}
698
Colin Crossa3002fc2019-07-08 16:48:04 -0700699// droiddoc_host converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700700func DroiddocHostFactory() android.Module {
701 module := &Droiddoc{}
702
703 module.AddProperties(&module.properties,
704 &module.Javadoc.properties)
705
706 InitDroiddocModule(module, android.HostSupported)
707 return module
708}
709
710func (d *Droiddoc) ApiFilePath() android.Path {
711 return d.apiFilePath
712}
713
Nan Zhang581fd212018-01-10 16:06:12 -0800714func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
715 d.Javadoc.addDeps(ctx)
716
Inseob Kim38449af2019-02-28 14:24:05 +0900717 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
718 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
719 }
720
Nan Zhang79614d12018-04-19 18:03:39 -0700721 if String(d.properties.Custom_template) != "" {
Dan Willemsencc090972018-02-26 14:33:31 -0800722 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
723 }
Nan Zhang581fd212018-01-10 16:06:12 -0800724}
725
Colin Crossab054432019-07-15 16:13:59 -0700726func (d *Droiddoc) doclavaDocsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, docletPath classpath) {
Automerger Merge Worker82f316b2020-02-28 21:26:56 +0000727 buildNumberFile := ctx.Config().BuildNumberFile(ctx)
Nan Zhang443fa522018-08-20 20:58:28 -0700728 // Droiddoc always gets "-source 1.8" because it doesn't support 1.9 sources. For modules with 1.9
729 // sources, droiddoc will get sources produced by metalava which will have already stripped out the
730 // 1.9 language features.
Colin Crossab054432019-07-15 16:13:59 -0700731 cmd.FlagWithArg("-source ", "1.8").
732 Flag("-J-Xmx1600m").
733 Flag("-J-XX:-OmitStackTraceInFastThrow").
734 Flag("-XDignore.symbol.file").
735 FlagWithArg("-doclet ", "com.google.doclava.Doclava").
736 FlagWithInputList("-docletpath ", docletPath.Paths(), ":").
Automerger Merge Worker82f316b2020-02-28 21:26:56 +0000737 FlagWithArg("-hdf page.build ", ctx.Config().BuildId()+"-$(cat "+buildNumberFile.String()+")").OrderOnly(buildNumberFile).
Elliott Hughes26bce342019-09-12 15:05:13 -0700738 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 -0700739
Nan Zhanga40da042018-08-01 12:48:00 -0700740 if String(d.properties.Custom_template) == "" {
741 // TODO: This is almost always droiddoc-templates-sdk
742 ctx.PropertyErrorf("custom_template", "must specify a template")
743 }
744
745 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
Nan Zhangf4936b02018-08-01 15:00:28 -0700746 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Crossab054432019-07-15 16:13:59 -0700747 cmd.FlagWithArg("-templatedir ", t.dir.String()).Implicits(t.deps)
Nan Zhanga40da042018-08-01 12:48:00 -0700748 } else {
Paul Duffin884363e2019-12-19 10:21:09 +0000749 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_exported_dir", ctx.OtherModuleName(m))
Nan Zhanga40da042018-08-01 12:48:00 -0700750 }
751 })
752
753 if len(d.properties.Html_dirs) > 0 {
Colin Crossab054432019-07-15 16:13:59 -0700754 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
755 cmd.FlagWithArg("-htmldir ", htmlDir.String()).
756 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[0], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700757 }
758
759 if len(d.properties.Html_dirs) > 1 {
Colin Crossab054432019-07-15 16:13:59 -0700760 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
761 cmd.FlagWithArg("-htmldir2 ", htmlDir2.String()).
762 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[1], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700763 }
764
765 if len(d.properties.Html_dirs) > 2 {
766 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
767 }
768
Colin Cross8a497952019-03-05 22:25:09 -0800769 knownTags := android.PathsForModuleSrc(ctx, d.properties.Knowntags)
Colin Crossab054432019-07-15 16:13:59 -0700770 cmd.FlagForEachInput("-knowntags ", knownTags)
Nan Zhanga40da042018-08-01 12:48:00 -0700771
Colin Crossab054432019-07-15 16:13:59 -0700772 cmd.FlagForEachArg("-hdf ", d.properties.Hdf)
Nan Zhanga40da042018-08-01 12:48:00 -0700773
774 if String(d.properties.Proofread_file) != "" {
775 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
Colin Crossab054432019-07-15 16:13:59 -0700776 cmd.FlagWithOutput("-proofread ", proofreadFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700777 }
778
779 if String(d.properties.Todo_file) != "" {
780 // tricky part:
781 // we should not compute full path for todo_file through PathForModuleOut().
782 // the non-standard doclet will get the full path relative to "-o".
Colin Crossab054432019-07-15 16:13:59 -0700783 cmd.FlagWithArg("-todo ", String(d.properties.Todo_file)).
784 ImplicitOutput(android.PathForModuleOut(ctx, String(d.properties.Todo_file)))
Nan Zhanga40da042018-08-01 12:48:00 -0700785 }
786
787 if String(d.properties.Resourcesdir) != "" {
788 // TODO: should we add files under resourcesDir to the implicits? It seems that
789 // resourcesDir is one sub dir of htmlDir
790 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
Colin Crossab054432019-07-15 16:13:59 -0700791 cmd.FlagWithArg("-resourcesdir ", resourcesDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700792 }
793
794 if String(d.properties.Resourcesoutdir) != "" {
795 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
Colin Crossab054432019-07-15 16:13:59 -0700796 cmd.FlagWithArg("-resourcesoutdir ", String(d.properties.Resourcesoutdir))
Nan Zhanga40da042018-08-01 12:48:00 -0700797 }
Nan Zhanga40da042018-08-01 12:48:00 -0700798}
799
Colin Crossab054432019-07-15 16:13:59 -0700800func (d *Droiddoc) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.WritablePath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200801 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
802 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -0700803 String(d.properties.Api_filename) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700804
Nan Zhanga40da042018-08-01 12:48:00 -0700805 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Colin Crossab054432019-07-15 16:13:59 -0700806 cmd.FlagWithOutput("-api ", d.apiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700807 d.apiFilePath = d.apiFile
808 }
809
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200810 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
811 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -0700812 String(d.properties.Removed_api_filename) != "" {
Nan Zhanga40da042018-08-01 12:48:00 -0700813 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
Colin Crossab054432019-07-15 16:13:59 -0700814 cmd.FlagWithOutput("-removedApi ", d.removedApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700815 }
816
Nan Zhanga40da042018-08-01 12:48:00 -0700817 if String(d.properties.Removed_dex_api_filename) != "" {
818 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Crossab054432019-07-15 16:13:59 -0700819 cmd.FlagWithOutput("-removedDexApi ", d.removedDexApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700820 }
821
Nan Zhanga40da042018-08-01 12:48:00 -0700822 if BoolDefault(d.properties.Create_stubs, true) {
Colin Crossab054432019-07-15 16:13:59 -0700823 cmd.FlagWithArg("-stubs ", stubsDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700824 }
825
826 if Bool(d.properties.Write_sdk_values) {
Colin Crossab054432019-07-15 16:13:59 -0700827 cmd.FlagWithArg("-sdkvalues ", android.PathForModuleOut(ctx, "out").String())
Nan Zhanga40da042018-08-01 12:48:00 -0700828 }
Nan Zhanga40da042018-08-01 12:48:00 -0700829}
830
Colin Crossab054432019-07-15 16:13:59 -0700831func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700832 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700833 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
834 rule.Command().Text("cp").
835 Input(staticDocIndexRedirect).
836 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700837 }
838
839 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700840 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
841 rule.Command().Text("cp").
842 Input(staticDocProperties).
843 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700844 }
Nan Zhanga40da042018-08-01 12:48:00 -0700845}
846
Colin Crossab054432019-07-15 16:13:59 -0700847func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700848 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700849
850 cmd := rule.Command().
851 BuiltTool(ctx, "soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
852 Flag(config.JavacVmFlags).
853 FlagWithArg("-encoding ", "UTF-8").
Colin Crossab054432019-07-15 16:13:59 -0700854 FlagWithRspFileInputList("@", srcs).
855 FlagWithInput("@", srcJarList)
856
Colin Crossab054432019-07-15 16:13:59 -0700857 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
858 // based stubs generation.
859 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
860 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
861 // the correct package name base path.
862 if len(sourcepaths) > 0 {
863 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
864 } else {
865 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
866 }
867
868 cmd.FlagWithArg("-d ", outDir.String()).
869 Flag("-quiet")
870
871 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700872}
873
Colin Crossdaa4c672019-07-15 22:53:46 -0700874func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
875 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
876 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
877
878 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
879
880 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
881 cmd.Flag(flag).Implicits(deps)
882
883 cmd.FlagWithArg("--patch-module ", "java.base=.")
884
885 if len(classpath) > 0 {
886 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
887 }
888
889 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700890}
891
Colin Crossdaa4c672019-07-15 22:53:46 -0700892func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
893 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
894 sourcepaths android.Paths) *android.RuleBuilderCommand {
895
896 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
897
898 if len(bootclasspath) == 0 && ctx.Device() {
899 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
900 // ensure java does not fall back to the default bootclasspath.
901 cmd.FlagWithArg("-bootclasspath ", `""`)
902 } else if len(bootclasspath) > 0 {
903 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
904 }
905
906 if len(classpath) > 0 {
907 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
908 }
909
910 return cmd
911}
912
Colin Crossab054432019-07-15 16:13:59 -0700913func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
914 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700915
Colin Crossab054432019-07-15 16:13:59 -0700916 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
917 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
918
919 return rule.Command().
920 BuiltTool(ctx, "dokka").
921 Flag(config.JavacVmFlags).
922 Flag(srcJarDir.String()).
923 FlagWithInputList("-classpath ", dokkaClasspath, ":").
924 FlagWithArg("-format ", "dac").
925 FlagWithArg("-dacRoot ", "/reference/kotlin").
926 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700927}
928
929func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
930 deps := d.Javadoc.collectDeps(ctx)
931
Colin Crossdaa4c672019-07-15 22:53:46 -0700932 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
933 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
934
Nan Zhang1598a9e2018-09-04 17:14:32 -0700935 jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
936 doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
937 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
938 checkApiClasspath := classpath{jsilver, doclava, android.PathForSource(ctx, java8Home, "lib/tools.jar")}
939
Colin Crossab054432019-07-15 16:13:59 -0700940 outDir := android.PathForModuleOut(ctx, "out")
941 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
942 stubsDir := android.PathForModuleOut(ctx, "stubsDir")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700943
Colin Crossab054432019-07-15 16:13:59 -0700944 rule := android.NewRuleBuilder()
Nan Zhang1598a9e2018-09-04 17:14:32 -0700945
Colin Crossab054432019-07-15 16:13:59 -0700946 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
947 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700948
Colin Crossab054432019-07-15 16:13:59 -0700949 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
950
951 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -0700952 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -0700953 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700954 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -0700955 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -0700956 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700957 }
958
Colin Crossab054432019-07-15 16:13:59 -0700959 d.stubsFlags(ctx, cmd, stubsDir)
960
961 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
962
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000963 if d.properties.Compat_config != nil {
964 compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
965 cmd.FlagWithInput("-compatconfig ", compatConfig)
966 }
967
Colin Crossab054432019-07-15 16:13:59 -0700968 var desc string
969 if Bool(d.properties.Dokka_enabled) {
970 desc = "dokka"
971 } else {
972 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
973
974 for _, o := range d.Javadoc.properties.Out {
975 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
976 }
977
978 d.postDoclavaCmds(ctx, rule)
979 desc = "doclava"
980 }
981
982 rule.Command().
983 BuiltTool(ctx, "soong_zip").
984 Flag("-write_if_changed").
985 Flag("-d").
986 FlagWithOutput("-o ", d.docZip).
987 FlagWithArg("-C ", outDir.String()).
988 FlagWithArg("-D ", outDir.String())
989
990 rule.Command().
991 BuiltTool(ctx, "soong_zip").
992 Flag("-write_if_changed").
993 Flag("-jar").
994 FlagWithOutput("-o ", d.stubsSrcJar).
995 FlagWithArg("-C ", stubsDir.String()).
996 FlagWithArg("-D ", stubsDir.String())
997
998 rule.Restat()
999
1000 zipSyncCleanupCmd(rule, srcJarDir)
1001
1002 rule.Build(pctx, ctx, "javadoc", desc)
1003
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001004 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001005 !ctx.Config().IsPdkBuild() {
Colin Crossab054432019-07-15 16:13:59 -07001006
1007 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1008 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Nan Zhang1598a9e2018-09-04 17:14:32 -07001009
1010 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001011
1012 rule := android.NewRuleBuilder()
1013
1014 rule.Command().Text("( true")
1015
1016 rule.Command().
1017 BuiltTool(ctx, "apicheck").
1018 Flag("-JXmx1024m").
1019 FlagWithInputList("-Jclasspath\\ ", checkApiClasspath.Paths(), ":").
1020 OptionalFlag(d.properties.Check_api.Current.Args).
1021 Input(apiFile).
1022 Input(d.apiFile).
1023 Input(removedApiFile).
1024 Input(d.removedApiFile)
1025
1026 msg := fmt.Sprintf(`\n******************************\n`+
1027 `You have tried to change the API from what has been previously approved.\n\n`+
1028 `To make these errors go away, you have two choices:\n`+
1029 ` 1. You can add '@hide' javadoc comments to the methods, etc. listed in the\n`+
1030 ` errors above.\n\n`+
1031 ` 2. You can update current.txt by executing the following command:\n`+
1032 ` make %s-update-current-api\n\n`+
1033 ` To submit the revised current.txt to the main Android repository,\n`+
1034 ` you will need approval.\n`+
1035 `******************************\n`, ctx.ModuleName())
1036
1037 rule.Command().
1038 Text("touch").Output(d.checkCurrentApiTimestamp).
1039 Text(") || (").
1040 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1041 Text("; exit 38").
1042 Text(")")
1043
1044 rule.Build(pctx, ctx, "doclavaCurrentApiCheck", "check current API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001045
1046 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001047
1048 // update API rule
1049 rule = android.NewRuleBuilder()
1050
1051 rule.Command().Text("( true")
1052
1053 rule.Command().
1054 Text("cp").Flag("-f").
1055 Input(d.apiFile).Flag(apiFile.String())
1056
1057 rule.Command().
1058 Text("cp").Flag("-f").
1059 Input(d.removedApiFile).Flag(removedApiFile.String())
1060
1061 msg = "failed to update public API"
1062
1063 rule.Command().
1064 Text("touch").Output(d.updateCurrentApiTimestamp).
1065 Text(") || (").
1066 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1067 Text("; exit 38").
1068 Text(")")
1069
1070 rule.Build(pctx, ctx, "doclavaCurrentApiUpdate", "update current API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001071 }
1072
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001073 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001074 !ctx.Config().IsPdkBuild() {
Colin Crossab054432019-07-15 16:13:59 -07001075
1076 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1077 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
Nan Zhang1598a9e2018-09-04 17:14:32 -07001078
1079 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001080
1081 rule := android.NewRuleBuilder()
1082
1083 rule.Command().
1084 Text("(").
1085 BuiltTool(ctx, "apicheck").
1086 Flag("-JXmx1024m").
1087 FlagWithInputList("-Jclasspath\\ ", checkApiClasspath.Paths(), ":").
1088 OptionalFlag(d.properties.Check_api.Last_released.Args).
1089 Input(apiFile).
1090 Input(d.apiFile).
1091 Input(removedApiFile).
1092 Input(d.removedApiFile)
1093
1094 msg := `\n******************************\n` +
1095 `You have tried to change the API from what has been previously released in\n` +
1096 `an SDK. Please fix the errors listed above.\n` +
1097 `******************************\n`
1098
1099 rule.Command().
1100 Text("touch").Output(d.checkLastReleasedApiTimestamp).
1101 Text(") || (").
1102 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1103 Text("; exit 38").
1104 Text(")")
1105
1106 rule.Build(pctx, ctx, "doclavaLastApiCheck", "check last API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001107 }
1108}
1109
1110//
1111// Droidstubs
1112//
1113type Droidstubs struct {
1114 Javadoc
Paul Duffin91547182019-11-12 19:39:36 +00001115 android.SdkBase
Nan Zhang1598a9e2018-09-04 17:14:32 -07001116
Pete Gillin581d6082018-10-22 15:55:04 +01001117 properties DroidstubsProperties
1118 apiFile android.WritablePath
1119 apiXmlFile android.WritablePath
1120 lastReleasedApiXmlFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001121 privateApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001122 removedApiFile android.WritablePath
1123 removedDexApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001124 nullabilityWarningsFile android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001125
1126 checkCurrentApiTimestamp android.WritablePath
1127 updateCurrentApiTimestamp android.WritablePath
1128 checkLastReleasedApiTimestamp android.WritablePath
Adrian Roos075eedc2019-10-10 12:07:03 +02001129 apiLintTimestamp android.WritablePath
Adrian Roos3b8f1cd2019-11-01 13:42:39 +01001130 apiLintReport android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001131
Pete Gillin581d6082018-10-22 15:55:04 +01001132 checkNullabilityWarningsTimestamp android.WritablePath
1133
Nan Zhang1598a9e2018-09-04 17:14:32 -07001134 annotationsZip android.WritablePath
Nan Zhang9c69a122018-08-22 10:22:08 -07001135 apiVersionsXml android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001136
1137 apiFilePath android.Path
Nan Zhang71bbe632018-09-17 14:32:21 -07001138
1139 jdiffDocZip android.WritablePath
1140 jdiffStubsSrcJar android.WritablePath
Jerome Gaillard0f599032019-10-10 19:29:11 +01001141
1142 metadataZip android.WritablePath
1143 metadataDir android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001144}
1145
Colin Crossa3002fc2019-07-08 16:48:04 -07001146// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
1147// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to
1148// a droiddoc module to generate documentation.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001149func DroidstubsFactory() android.Module {
1150 module := &Droidstubs{}
1151
1152 module.AddProperties(&module.properties,
1153 &module.Javadoc.properties)
1154
1155 InitDroiddocModule(module, android.HostAndDeviceSupported)
Paul Duffin91547182019-11-12 19:39:36 +00001156 android.InitSdkAwareModule(module)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001157 return module
1158}
1159
Colin Crossa3002fc2019-07-08 16:48:04 -07001160// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API
1161// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be
1162// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs
1163// module when symbols needed by the source files are provided by java_library_host modules.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001164func DroidstubsHostFactory() android.Module {
1165 module := &Droidstubs{}
1166
1167 module.AddProperties(&module.properties,
1168 &module.Javadoc.properties)
1169
1170 InitDroiddocModule(module, android.HostSupported)
1171 return module
1172}
1173
1174func (d *Droidstubs) ApiFilePath() android.Path {
1175 return d.apiFilePath
1176}
1177
1178func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
1179 d.Javadoc.addDeps(ctx)
1180
Inseob Kim38449af2019-02-28 14:24:05 +09001181 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
1182 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
1183 }
1184
Nan Zhang1598a9e2018-09-04 17:14:32 -07001185 if len(d.properties.Merge_annotations_dirs) != 0 {
1186 for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
1187 ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
1188 }
1189 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001190
Pete Gillin77167902018-09-19 18:16:26 +01001191 if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
1192 for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
1193 ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
1194 }
1195 }
1196
Nan Zhang9c69a122018-08-22 10:22:08 -07001197 if len(d.properties.Api_levels_annotations_dirs) != 0 {
1198 for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
1199 ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
1200 }
1201 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001202}
1203
Colin Cross33961b52019-07-11 11:01:22 -07001204func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.WritablePath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001205 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1206 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001207 String(d.properties.Api_filename) != "" {
1208 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001209 cmd.FlagWithOutput("--api ", d.apiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001210 d.apiFilePath = d.apiFile
1211 }
1212
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001213 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1214 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001215 String(d.properties.Removed_api_filename) != "" {
1216 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001217 cmd.FlagWithOutput("--removed-api ", d.removedApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001218 }
1219
Nan Zhang1598a9e2018-09-04 17:14:32 -07001220 if String(d.properties.Removed_dex_api_filename) != "" {
1221 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001222 cmd.FlagWithOutput("--removed-dex-api ", d.removedDexApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001223 }
1224
Nan Zhang9c69a122018-08-22 10:22:08 -07001225 if Bool(d.properties.Write_sdk_values) {
Jerome Gaillard0f599032019-10-10 19:29:11 +01001226 d.metadataDir = android.PathForModuleOut(ctx, "metadata")
1227 cmd.FlagWithArg("--sdk-values ", d.metadataDir.String())
Nan Zhang9c69a122018-08-22 10:22:08 -07001228 }
1229
Nan Zhang1598a9e2018-09-04 17:14:32 -07001230 if Bool(d.properties.Create_doc_stubs) {
Colin Cross33961b52019-07-11 11:01:22 -07001231 cmd.FlagWithArg("--doc-stubs ", stubsDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -07001232 } else {
Colin Cross33961b52019-07-11 11:01:22 -07001233 cmd.FlagWithArg("--stubs ", stubsDir.String())
Aurimas Liutikas2d732e02020-03-11 16:28:05 -07001234 cmd.Flag("--exclude-documentation-from-stubs")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001235 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001236}
1237
Colin Cross33961b52019-07-11 11:01:22 -07001238func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang1598a9e2018-09-04 17:14:32 -07001239 if Bool(d.properties.Annotations_enabled) {
Colin Cross33961b52019-07-11 11:01:22 -07001240 cmd.Flag("--include-annotations")
1241
Pete Gillinc382a562018-11-14 18:45:46 +00001242 validatingNullability :=
1243 strings.Contains(d.Javadoc.args, "--validate-nullability-from-merged-stubs") ||
1244 String(d.properties.Validate_nullability_from_list) != ""
Paul Duffin13a9dd62019-11-04 10:26:47 +00001245
Pete Gillina262c052018-09-14 14:25:48 +01001246 migratingNullability := String(d.properties.Previous_api) != ""
Pete Gillina262c052018-09-14 14:25:48 +01001247 if migratingNullability {
Colin Cross8a497952019-03-05 22:25:09 -08001248 previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
Colin Cross33961b52019-07-11 11:01:22 -07001249 cmd.FlagWithInput("--migrate-nullness ", previousApi)
Pete Gillina262c052018-09-14 14:25:48 +01001250 }
Colin Cross33961b52019-07-11 11:01:22 -07001251
Pete Gillinc382a562018-11-14 18:45:46 +00001252 if s := String(d.properties.Validate_nullability_from_list); s != "" {
Colin Cross33961b52019-07-11 11:01:22 -07001253 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
Pete Gillinc382a562018-11-14 18:45:46 +00001254 }
Colin Cross33961b52019-07-11 11:01:22 -07001255
Pete Gillina262c052018-09-14 14:25:48 +01001256 if validatingNullability {
Pete Gillin581d6082018-10-22 15:55:04 +01001257 d.nullabilityWarningsFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_nullability_warnings.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001258 cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile)
Pete Gillina262c052018-09-14 14:25:48 +01001259 }
Nan Zhanga40da042018-08-01 12:48:00 -07001260
1261 d.annotationsZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"_annotations.zip")
Colin Cross33961b52019-07-11 11:01:22 -07001262 cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip)
Nan Zhangf4936b02018-08-01 15:00:28 -07001263
Nan Zhang1598a9e2018-09-04 17:14:32 -07001264 if len(d.properties.Merge_annotations_dirs) == 0 {
Nan Zhang9c69a122018-08-22 10:22:08 -07001265 ctx.PropertyErrorf("merge_annotations_dirs",
Nan Zhanga40da042018-08-01 12:48:00 -07001266 "has to be non-empty if annotations was enabled!")
1267 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001268
Colin Cross33961b52019-07-11 11:01:22 -07001269 d.mergeAnnoDirFlags(ctx, cmd)
1270
1271 // TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
1272 cmd.FlagWithArg("--hide ", "HiddenTypedefConstant").
1273 FlagWithArg("--hide ", "SuperfluousPrefix").
1274 FlagWithArg("--hide ", "AnnotationExtraction")
1275 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001276}
1277
Colin Cross33961b52019-07-11 11:01:22 -07001278func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
1279 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
1280 if t, ok := m.(*ExportedDroiddocDir); ok {
1281 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
1282 } else {
1283 ctx.PropertyErrorf("merge_annotations_dirs",
1284 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1285 }
1286 })
1287}
1288
1289func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Pete Gillin77167902018-09-19 18:16:26 +01001290 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
1291 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Cross33961b52019-07-11 11:01:22 -07001292 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
Pete Gillin77167902018-09-19 18:16:26 +01001293 } else {
1294 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
1295 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1296 }
1297 })
Nan Zhanga40da042018-08-01 12:48:00 -07001298}
1299
Colin Cross33961b52019-07-11 11:01:22 -07001300func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang9c69a122018-08-22 10:22:08 -07001301 if Bool(d.properties.Api_levels_annotations_enabled) {
1302 d.apiVersionsXml = android.PathForModuleOut(ctx, "api-versions.xml")
Nan Zhang9c69a122018-08-22 10:22:08 -07001303
1304 if len(d.properties.Api_levels_annotations_dirs) == 0 {
1305 ctx.PropertyErrorf("api_levels_annotations_dirs",
1306 "has to be non-empty if api levels annotations was enabled!")
1307 }
1308
Colin Cross33961b52019-07-11 11:01:22 -07001309 cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml)
1310 cmd.FlagWithInput("--apply-api-levels ", d.apiVersionsXml)
1311 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion())
1312 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
Nan Zhang9c69a122018-08-22 10:22:08 -07001313
1314 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
1315 if t, ok := m.(*ExportedDroiddocDir); ok {
Nan Zhang9c69a122018-08-22 10:22:08 -07001316 for _, dep := range t.deps {
1317 if strings.HasSuffix(dep.String(), "android.jar") {
Colin Cross33961b52019-07-11 11:01:22 -07001318 cmd.Implicit(dep)
Nan Zhang9c69a122018-08-22 10:22:08 -07001319 }
1320 }
Colin Cross33961b52019-07-11 11:01:22 -07001321 cmd.FlagWithArg("--android-jar-pattern ", t.dir.String()+"/%/public/android.jar")
Nan Zhang9c69a122018-08-22 10:22:08 -07001322 } else {
1323 ctx.PropertyErrorf("api_levels_annotations_dirs",
1324 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
1325 }
1326 })
1327
1328 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001329}
1330
Colin Cross33961b52019-07-11 11:01:22 -07001331func (d *Droidstubs) apiToXmlFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang71bbe632018-09-17 14:32:21 -07001332 if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() {
1333 if d.apiFile.String() == "" {
1334 ctx.ModuleErrorf("API signature file has to be specified in Metalava when jdiff is enabled.")
1335 }
1336
1337 d.apiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001338 cmd.FlagWithOutput("--api-xml ", d.apiXmlFile)
Nan Zhang71bbe632018-09-17 14:32:21 -07001339
1340 if String(d.properties.Check_api.Last_released.Api_file) == "" {
1341 ctx.PropertyErrorf("check_api.last_released.api_file",
1342 "has to be non-empty if jdiff was enabled!")
1343 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001344
Colin Cross33961b52019-07-11 11:01:22 -07001345 lastReleasedApi := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
Nan Zhang71bbe632018-09-17 14:32:21 -07001346 d.lastReleasedApiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_last_released_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001347 cmd.FlagWithInput("--convert-to-jdiff ", lastReleasedApi).Output(d.lastReleasedApiXmlFile)
1348 }
1349}
Nan Zhang71bbe632018-09-17 14:32:21 -07001350
Colin Cross1e743852019-10-28 11:37:20 -07001351func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths,
Colin Cross33961b52019-07-11 11:01:22 -07001352 srcJarList android.Path, bootclasspath, classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Cross8b8bec32019-11-15 13:18:43 -08001353 // Metalava uses lots of memory, restrict the number of metalava jobs that can run in parallel.
1354 rule.HighMem()
Colin Cross33961b52019-07-11 11:01:22 -07001355 cmd := rule.Command().BuiltTool(ctx, "metalava").
1356 Flag(config.JavacVmFlags).
1357 FlagWithArg("-encoding ", "UTF-8").
Colin Cross1e743852019-10-28 11:37:20 -07001358 FlagWithArg("-source ", javaVersion.String()).
Colin Cross33961b52019-07-11 11:01:22 -07001359 FlagWithRspFileInputList("@", srcs).
1360 FlagWithInput("@", srcJarList)
1361
1362 if len(bootclasspath) > 0 {
1363 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
Nan Zhang71bbe632018-09-17 14:32:21 -07001364 }
1365
Colin Cross33961b52019-07-11 11:01:22 -07001366 if len(classpath) > 0 {
1367 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
1368 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001369
Colin Cross33961b52019-07-11 11:01:22 -07001370 if len(sourcepaths) > 0 {
1371 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
1372 } else {
1373 cmd.FlagWithArg("-sourcepath ", `""`)
1374 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001375
Colin Cross33961b52019-07-11 11:01:22 -07001376 cmd.Flag("--no-banner").
1377 Flag("--color").
1378 Flag("--quiet").
1379 Flag("--format=v2")
Nan Zhang86d2d552018-08-09 15:33:27 -07001380
Colin Cross33961b52019-07-11 11:01:22 -07001381 return cmd
Nan Zhang71bbe632018-09-17 14:32:21 -07001382}
1383
Nan Zhang1598a9e2018-09-04 17:14:32 -07001384func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga40da042018-08-01 12:48:00 -07001385 deps := d.Javadoc.collectDeps(ctx)
1386
1387 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), sdkContext(d))
Nan Zhang581fd212018-01-10 16:06:12 -08001388
Colin Cross33961b52019-07-11 11:01:22 -07001389 // Create rule for metalava
Nan Zhanga40da042018-08-01 12:48:00 -07001390
Colin Crossdaa4c672019-07-15 22:53:46 -07001391 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
Nan Zhanga40da042018-08-01 12:48:00 -07001392
Colin Cross33961b52019-07-11 11:01:22 -07001393 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
1394 stubsDir := android.PathForModuleOut(ctx, "stubsDir")
Nan Zhang71bbe632018-09-17 14:32:21 -07001395
Colin Cross33961b52019-07-11 11:01:22 -07001396 rule := android.NewRuleBuilder()
Nan Zhanga40da042018-08-01 12:48:00 -07001397
Colin Cross33961b52019-07-11 11:01:22 -07001398 rule.Command().Text("rm -rf").Text(stubsDir.String())
1399 rule.Command().Text("mkdir -p").Text(stubsDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -07001400
Colin Cross33961b52019-07-11 11:01:22 -07001401 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1402
1403 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
1404 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
1405
1406 d.stubsFlags(ctx, cmd, stubsDir)
1407
1408 d.annotationsFlags(ctx, cmd)
1409 d.inclusionAnnotationsFlags(ctx, cmd)
1410 d.apiLevelsAnnotationsFlags(ctx, cmd)
1411 d.apiToXmlFlags(ctx, cmd)
Nan Zhang71bbe632018-09-17 14:32:21 -07001412
Nan Zhang1598a9e2018-09-04 17:14:32 -07001413 if strings.Contains(d.Javadoc.args, "--generate-documentation") {
1414 // Currently Metalava have the ability to invoke Javadoc in a seperate process.
1415 // Pass "-nodocs" to suppress the Javadoc invocation when Metalava receives
1416 // "--generate-documentation" arg. This is not needed when Metalava removes this feature.
1417 d.Javadoc.args = d.Javadoc.args + " -nodocs "
Nan Zhang79614d12018-04-19 18:03:39 -07001418 }
Colin Cross33961b52019-07-11 11:01:22 -07001419
1420 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1421 for _, o := range d.Javadoc.properties.Out {
1422 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1423 }
1424
1425 rule.Command().
1426 BuiltTool(ctx, "soong_zip").
1427 Flag("-write_if_changed").
1428 Flag("-jar").
1429 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
1430 FlagWithArg("-C ", stubsDir.String()).
1431 FlagWithArg("-D ", stubsDir.String())
Jerome Gaillard0f599032019-10-10 19:29:11 +01001432
1433 if Bool(d.properties.Write_sdk_values) {
1434 d.metadataZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-metadata.zip")
1435 rule.Command().
1436 BuiltTool(ctx, "soong_zip").
1437 Flag("-write_if_changed").
1438 Flag("-d").
1439 FlagWithOutput("-o ", d.metadataZip).
1440 FlagWithArg("-C ", d.metadataDir.String()).
1441 FlagWithArg("-D ", d.metadataDir.String())
1442 }
1443
Colin Cross33961b52019-07-11 11:01:22 -07001444 rule.Restat()
1445
1446 zipSyncCleanupCmd(rule, srcJarDir)
1447
1448 rule.Build(pctx, ctx, "metalava", "metalava")
1449
1450 // Create rule for apicheck
Nan Zhang61819ce2018-05-04 18:49:16 -07001451
Adrian Roos075eedc2019-10-10 12:07:03 +02001452 if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) && !ctx.Config().IsPdkBuild() {
1453 rule := android.NewRuleBuilder()
1454 rule.Command().Text("( true")
1455
1456 srcJarDir := android.PathForModuleOut(ctx, "api_lint", "srcjars")
1457 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1458
1459 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
1460 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
1461
Adrian Rooscab4a2c2019-10-14 16:32:41 +02001462 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1463
Adrian Roos075eedc2019-10-10 12:07:03 +02001464 newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since)
1465 if newSince.Valid() {
1466 cmd.FlagWithInput("--api-lint ", newSince.Path())
1467 } else {
1468 cmd.Flag("--api-lint")
1469 }
Adrian Roos3b8f1cd2019-11-01 13:42:39 +01001470 d.apiLintReport = android.PathForModuleOut(ctx, "api_lint_report.txt")
1471 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport)
Adrian Roos075eedc2019-10-10 12:07:03 +02001472
1473 d.inclusionAnnotationsFlags(ctx, cmd)
1474 d.mergeAnnoDirFlags(ctx, cmd)
1475
1476 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
1477 updatedBaselineOutput := android.PathForModuleOut(ctx, "api_lint_baseline.txt")
1478 d.apiLintTimestamp = android.PathForModuleOut(ctx, "api_lint.timestamp")
1479
1480 if baselineFile.Valid() {
1481 cmd.FlagWithInput("--baseline ", baselineFile.Path())
1482 cmd.FlagWithOutput("--update-baseline ", updatedBaselineOutput)
1483 }
1484
1485 zipSyncCleanupCmd(rule, srcJarDir)
1486
1487 msg := fmt.Sprintf(`\n******************************\n`+
1488 `Your API changes are triggering API Lint warnings or errors.\n\n`+
1489 `To make these errors go away, you have two choices:\n`+
1490 ` 1. You can suppress the errors with @SuppressLint(\"<id>\").\n\n`+
1491 ` 2. You can update the baseline by executing the following command:\n`+
1492 ` cp \"$PWD/%s\" \"$PWD/%s\"\n\n`+
1493 `******************************\n`, updatedBaselineOutput, baselineFile.Path())
1494 rule.Command().
1495 Text("touch").Output(d.apiLintTimestamp).
1496 Text(") || (").
1497 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1498 Text("; exit 38").
1499 Text(")")
1500
1501 rule.Build(pctx, ctx, "metalavaApiLint", "metalava API lint")
1502
1503 }
1504
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001505 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001506 !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001507
1508 if len(d.Javadoc.properties.Out) > 0 {
1509 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1510 }
1511
1512 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1513 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Adrian Roos14f75a92019-08-12 17:54:09 +02001514 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
1515 updatedBaselineOutput := android.PathForModuleOut(ctx, "current_baseline.txt")
Nan Zhang61819ce2018-05-04 18:49:16 -07001516
Nan Zhang2760dfc2018-08-24 17:32:54 +00001517 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Nan Zhang2760dfc2018-08-24 17:32:54 +00001518
Colin Cross33961b52019-07-11 11:01:22 -07001519 rule := android.NewRuleBuilder()
1520
1521 rule.Command().Text("( true")
1522
1523 srcJarDir := android.PathForModuleOut(ctx, "current-apicheck", "srcjars")
1524 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1525
1526 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
1527 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
1528
1529 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles).
1530 FlagWithInput("--check-compatibility:api:current ", apiFile).
1531 FlagWithInput("--check-compatibility:removed:current ", removedApiFile)
1532
1533 d.inclusionAnnotationsFlags(ctx, cmd)
1534 d.mergeAnnoDirFlags(ctx, cmd)
1535
Adrian Roos14f75a92019-08-12 17:54:09 +02001536 if baselineFile.Valid() {
1537 cmd.FlagWithInput("--baseline ", baselineFile.Path())
1538 cmd.FlagWithOutput("--update-baseline ", updatedBaselineOutput)
1539 }
1540
Colin Cross33961b52019-07-11 11:01:22 -07001541 zipSyncCleanupCmd(rule, srcJarDir)
1542
1543 msg := fmt.Sprintf(`\n******************************\n`+
1544 `You have tried to change the API from what has been previously approved.\n\n`+
1545 `To make these errors go away, you have two choices:\n`+
1546 ` 1. You can add '@hide' javadoc comments to the methods, etc. listed in the\n`+
1547 ` errors above.\n\n`+
1548 ` 2. You can update current.txt by executing the following command:\n`+
1549 ` make %s-update-current-api\n\n`+
1550 ` To submit the revised current.txt to the main Android repository,\n`+
1551 ` you will need approval.\n`+
1552 `******************************\n`, ctx.ModuleName())
1553
1554 rule.Command().
1555 Text("touch").Output(d.checkCurrentApiTimestamp).
1556 Text(") || (").
1557 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1558 Text("; exit 38").
1559 Text(")")
1560
1561 rule.Build(pctx, ctx, "metalavaCurrentApiCheck", "metalava check current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001562
1563 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001564
1565 // update API rule
1566 rule = android.NewRuleBuilder()
1567
1568 rule.Command().Text("( true")
1569
1570 rule.Command().
1571 Text("cp").Flag("-f").
1572 Input(d.apiFile).Flag(apiFile.String())
1573
1574 rule.Command().
1575 Text("cp").Flag("-f").
1576 Input(d.removedApiFile).Flag(removedApiFile.String())
1577
1578 msg = "failed to update public API"
1579
1580 rule.Command().
1581 Text("touch").Output(d.updateCurrentApiTimestamp).
1582 Text(") || (").
1583 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1584 Text("; exit 38").
1585 Text(")")
1586
1587 rule.Build(pctx, ctx, "metalavaCurrentApiUpdate", "update current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001588 }
Nan Zhanga40da042018-08-01 12:48:00 -07001589
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001590 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001591 !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001592
1593 if len(d.Javadoc.properties.Out) > 0 {
1594 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1595 }
1596
1597 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1598 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
Adrian Roos14f75a92019-08-12 17:54:09 +02001599 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
1600 updatedBaselineOutput := android.PathForModuleOut(ctx, "last_released_baseline.txt")
Nan Zhang61819ce2018-05-04 18:49:16 -07001601
Nan Zhang2760dfc2018-08-24 17:32:54 +00001602 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
Nan Zhang2760dfc2018-08-24 17:32:54 +00001603
Colin Cross33961b52019-07-11 11:01:22 -07001604 rule := android.NewRuleBuilder()
1605
1606 rule.Command().Text("( true")
1607
1608 srcJarDir := android.PathForModuleOut(ctx, "last-apicheck", "srcjars")
1609 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1610
1611 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
1612 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
1613
1614 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles).
1615 FlagWithInput("--check-compatibility:api:released ", apiFile)
1616
1617 d.inclusionAnnotationsFlags(ctx, cmd)
1618
1619 cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile)
1620
1621 d.mergeAnnoDirFlags(ctx, cmd)
1622
Adrian Roos14f75a92019-08-12 17:54:09 +02001623 if baselineFile.Valid() {
1624 cmd.FlagWithInput("--baseline ", baselineFile.Path())
1625 cmd.FlagWithOutput("--update-baseline ", updatedBaselineOutput)
1626 }
1627
Colin Cross33961b52019-07-11 11:01:22 -07001628 zipSyncCleanupCmd(rule, srcJarDir)
1629
1630 msg := `\n******************************\n` +
1631 `You have tried to change the API from what has been previously released in\n` +
1632 `an SDK. Please fix the errors listed above.\n` +
1633 `******************************\n`
1634 rule.Command().
1635 Text("touch").Output(d.checkLastReleasedApiTimestamp).
1636 Text(") || (").
1637 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1638 Text("; exit 38").
1639 Text(")")
1640
1641 rule.Build(pctx, ctx, "metalavaLastApiCheck", "metalava check last API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001642 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001643
Pete Gillin581d6082018-10-22 15:55:04 +01001644 if String(d.properties.Check_nullability_warnings) != "" {
1645 if d.nullabilityWarningsFile == nil {
1646 ctx.PropertyErrorf("check_nullability_warnings",
1647 "Cannot specify check_nullability_warnings unless validating nullability")
1648 }
Colin Cross33961b52019-07-11 11:01:22 -07001649
1650 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
1651
Pete Gillin581d6082018-10-22 15:55:04 +01001652 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "check_nullability_warnings.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001653
Pete Gillin581d6082018-10-22 15:55:04 +01001654 msg := fmt.Sprintf(`\n******************************\n`+
1655 `The warnings encountered during nullability annotation validation did\n`+
1656 `not match the checked in file of expected warnings. The diffs are shown\n`+
1657 `above. You have two options:\n`+
1658 ` 1. Resolve the differences by editing the nullability annotations.\n`+
1659 ` 2. Update the file of expected warnings by running:\n`+
1660 ` cp %s %s\n`+
1661 ` and submitting the updated file as part of your change.`,
1662 d.nullabilityWarningsFile, checkNullabilityWarnings)
Colin Cross33961b52019-07-11 11:01:22 -07001663
1664 rule := android.NewRuleBuilder()
1665
1666 rule.Command().
1667 Text("(").
1668 Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile).
1669 Text("&&").
1670 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
1671 Text(") || (").
1672 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1673 Text("; exit 38").
1674 Text(")")
1675
1676 rule.Build(pctx, ctx, "nullabilityWarningsCheck", "nullability warnings check")
Pete Gillin581d6082018-10-22 15:55:04 +01001677 }
1678
Nan Zhang71bbe632018-09-17 14:32:21 -07001679 if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001680 if len(d.Javadoc.properties.Out) > 0 {
1681 ctx.PropertyErrorf("out", "out property may not be combined with jdiff")
1682 }
1683
1684 outDir := android.PathForModuleOut(ctx, "jdiff-out")
1685 srcJarDir := android.PathForModuleOut(ctx, "jdiff-srcjars")
1686 stubsDir := android.PathForModuleOut(ctx, "jdiff-stubsDir")
1687
1688 rule := android.NewRuleBuilder()
Nan Zhang71bbe632018-09-17 14:32:21 -07001689
Nan Zhang86b06202018-09-21 17:09:21 -07001690 // Please sync with android-api-council@ before making any changes for the name of jdiffDocZip below
1691 // since there's cron job downstream that fetch this .zip file periodically.
1692 // See b/116221385 for reference.
Nan Zhang71bbe632018-09-17 14:32:21 -07001693 d.jdiffDocZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-docs.zip")
1694 d.jdiffStubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-stubs.srcjar")
1695
Nan Zhang71bbe632018-09-17 14:32:21 -07001696 jdiff := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jdiff.jar")
Nan Zhang71bbe632018-09-17 14:32:21 -07001697
Colin Cross33961b52019-07-11 11:01:22 -07001698 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
1699 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang71bbe632018-09-17 14:32:21 -07001700
Colin Cross33961b52019-07-11 11:01:22 -07001701 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1702
Colin Crossdaa4c672019-07-15 22:53:46 -07001703 cmd := javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -07001704 deps.bootClasspath, deps.classpath, d.sourcepaths)
1705
1706 cmd.Flag("-J-Xmx1600m").
Colin Cross33961b52019-07-11 11:01:22 -07001707 Flag("-XDignore.symbol.file").
1708 FlagWithArg("-doclet ", "jdiff.JDiff").
1709 FlagWithInput("-docletpath ", jdiff).
1710 Flag("-quiet").
1711 FlagWithArg("-newapi ", strings.TrimSuffix(d.apiXmlFile.Base(), d.apiXmlFile.Ext())).
1712 FlagWithArg("-newapidir ", filepath.Dir(d.apiXmlFile.String())).
1713 Implicit(d.apiXmlFile).
1714 FlagWithArg("-oldapi ", strings.TrimSuffix(d.lastReleasedApiXmlFile.Base(), d.lastReleasedApiXmlFile.Ext())).
1715 FlagWithArg("-oldapidir ", filepath.Dir(d.lastReleasedApiXmlFile.String())).
1716 Implicit(d.lastReleasedApiXmlFile)
1717
Colin Cross33961b52019-07-11 11:01:22 -07001718 rule.Command().
1719 BuiltTool(ctx, "soong_zip").
1720 Flag("-write_if_changed").
1721 Flag("-d").
1722 FlagWithOutput("-o ", d.jdiffDocZip).
1723 FlagWithArg("-C ", outDir.String()).
1724 FlagWithArg("-D ", outDir.String())
1725
1726 rule.Command().
1727 BuiltTool(ctx, "soong_zip").
1728 Flag("-write_if_changed").
1729 Flag("-jar").
1730 FlagWithOutput("-o ", d.jdiffStubsSrcJar).
1731 FlagWithArg("-C ", stubsDir.String()).
1732 FlagWithArg("-D ", stubsDir.String())
1733
1734 rule.Restat()
1735
1736 zipSyncCleanupCmd(rule, srcJarDir)
1737
1738 rule.Build(pctx, ctx, "jdiff", "jdiff")
Nan Zhang71bbe632018-09-17 14:32:21 -07001739 }
Nan Zhang581fd212018-01-10 16:06:12 -08001740}
Dan Willemsencc090972018-02-26 14:33:31 -08001741
Nan Zhanga40da042018-08-01 12:48:00 -07001742//
Nan Zhangf4936b02018-08-01 15:00:28 -07001743// Exported Droiddoc Directory
Nan Zhanga40da042018-08-01 12:48:00 -07001744//
Dan Willemsencc090972018-02-26 14:33:31 -08001745var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
Nan Zhangf4936b02018-08-01 15:00:28 -07001746var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
Pete Gillin77167902018-09-19 18:16:26 +01001747var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
Nan Zhang9c69a122018-08-22 10:22:08 -07001748var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
Dan Willemsencc090972018-02-26 14:33:31 -08001749
Nan Zhangf4936b02018-08-01 15:00:28 -07001750type ExportedDroiddocDirProperties struct {
1751 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -08001752 Path *string
1753}
1754
Nan Zhangf4936b02018-08-01 15:00:28 -07001755type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -08001756 android.ModuleBase
1757
Nan Zhangf4936b02018-08-01 15:00:28 -07001758 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -08001759
1760 deps android.Paths
1761 dir android.Path
1762}
1763
Colin Crossa3002fc2019-07-08 16:48:04 -07001764// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -07001765func ExportedDroiddocDirFactory() android.Module {
1766 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -08001767 module.AddProperties(&module.properties)
1768 android.InitAndroidModule(module)
1769 return module
1770}
1771
Nan Zhangf4936b02018-08-01 15:00:28 -07001772func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -08001773
Nan Zhangf4936b02018-08-01 15:00:28 -07001774func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -08001775 path := String(d.properties.Path)
1776 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -08001777 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Dan Willemsencc090972018-02-26 14:33:31 -08001778}
Nan Zhangb2b33de2018-02-23 11:18:47 -08001779
1780//
1781// Defaults
1782//
1783type DocDefaults struct {
1784 android.ModuleBase
1785 android.DefaultsModuleBase
1786}
1787
Nan Zhangb2b33de2018-02-23 11:18:47 -08001788func DocDefaultsFactory() android.Module {
1789 module := &DocDefaults{}
1790
1791 module.AddProperties(
1792 &JavadocProperties{},
1793 &DroiddocProperties{},
1794 )
1795
1796 android.InitDefaultsModule(module)
1797
1798 return module
1799}
Nan Zhang1598a9e2018-09-04 17:14:32 -07001800
1801func StubsDefaultsFactory() android.Module {
1802 module := &DocDefaults{}
1803
1804 module.AddProperties(
1805 &JavadocProperties{},
1806 &DroidstubsProperties{},
1807 )
1808
1809 android.InitDefaultsModule(module)
1810
1811 return module
1812}
Colin Cross33961b52019-07-11 11:01:22 -07001813
1814func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
1815 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
1816
1817 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1818 rule.Command().Text("mkdir -p").Text(srcJarDir.String())
1819 srcJarList := srcJarDir.Join(ctx, "list")
1820
1821 rule.Temporary(srcJarList)
1822
1823 rule.Command().BuiltTool(ctx, "zipsync").
1824 FlagWithArg("-d ", srcJarDir.String()).
1825 FlagWithOutput("-l ", srcJarList).
1826 FlagWithArg("-f ", `"*.java"`).
1827 Inputs(srcJars)
1828
1829 return srcJarList
1830}
1831
1832func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
1833 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1834}
Paul Duffin91547182019-11-12 19:39:36 +00001835
1836var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
1837
1838type PrebuiltStubsSourcesProperties struct {
1839 Srcs []string `android:"path"`
1840}
1841
1842type PrebuiltStubsSources struct {
1843 android.ModuleBase
1844 android.DefaultableModuleBase
1845 prebuilt android.Prebuilt
1846 android.SdkBase
1847
1848 properties PrebuiltStubsSourcesProperties
1849
Paul Duffin9b478b02019-12-10 13:41:51 +00001850 // The source directories containing stubs source files.
1851 srcDirs android.Paths
Paul Duffin91547182019-11-12 19:39:36 +00001852 stubsSrcJar android.ModuleOutPath
1853}
1854
Paul Duffin9b478b02019-12-10 13:41:51 +00001855func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) {
1856 switch tag {
1857 case "":
1858 return android.Paths{p.stubsSrcJar}, nil
1859 default:
1860 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1861 }
1862}
1863
Paul Duffin91547182019-11-12 19:39:36 +00001864func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin9b478b02019-12-10 13:41:51 +00001865 p.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1866
1867 p.srcDirs = android.PathsForModuleSrc(ctx, p.properties.Srcs)
1868
1869 rule := android.NewRuleBuilder()
1870 command := rule.Command().
1871 BuiltTool(ctx, "soong_zip").
1872 Flag("-write_if_changed").
1873 Flag("-jar").
1874 FlagWithOutput("-o ", p.stubsSrcJar)
1875
1876 for _, d := range p.srcDirs {
1877 dir := d.String()
1878 command.
1879 FlagWithArg("-C ", dir).
1880 FlagWithInput("-D ", d)
1881 }
1882
1883 rule.Restat()
1884
1885 rule.Build(pctx, ctx, "zip src", "Create srcjar from prebuilt source")
Paul Duffin91547182019-11-12 19:39:36 +00001886}
1887
1888func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
1889 return &p.prebuilt
1890}
1891
1892func (p *PrebuiltStubsSources) Name() string {
1893 return p.prebuilt.Name(p.ModuleBase.Name())
1894}
1895
Paul Duffin91547182019-11-12 19:39:36 +00001896// prebuilt_stubs_sources imports a set of java source files as if they were
1897// generated by droidstubs.
1898//
1899// By default, a prebuilt_stubs_sources has a single variant that expects a
1900// set of `.java` files generated by droidstubs.
1901//
1902// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1903// for host modules.
1904//
1905// Intended only for use by sdk snapshots.
1906func PrebuiltStubsSourcesFactory() android.Module {
1907 module := &PrebuiltStubsSources{}
1908
1909 module.AddProperties(&module.properties)
1910
1911 android.InitPrebuiltModule(module, &module.properties.Srcs)
1912 android.InitSdkAwareModule(module)
1913 InitDroiddocModule(module, android.HostAndDeviceSupported)
1914 return module
1915}
1916
Paul Duffin13879572019-11-28 14:31:38 +00001917type droidStubsSdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001918 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001919}
1920
1921func (mt *droidStubsSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1922 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1923}
1924
1925func (mt *droidStubsSdkMemberType) IsInstance(module android.Module) bool {
1926 _, ok := module.(*Droidstubs)
1927 return ok
1928}
1929
1930func (mt *droidStubsSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1931 variants := member.Variants()
1932 if len(variants) != 1 {
1933 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1934 }
1935 variant := variants[0]
1936 d, _ := variant.(*Droidstubs)
Paul Duffin91547182019-11-12 19:39:36 +00001937 stubsSrcJar := d.stubsSrcJar
1938
1939 snapshotRelativeDir := filepath.Join("java", d.Name()+"_stubs_sources")
1940 builder.UnzipToSnapshot(stubsSrcJar, snapshotRelativeDir)
1941
Paul Duffin9d8d6092019-12-05 18:19:29 +00001942 pbm := builder.AddPrebuiltModule(member, "prebuilt_stubs_sources")
Paul Duffinb645ec82019-11-27 17:43:54 +00001943 pbm.AddProperty("srcs", []string{snapshotRelativeDir})
Paul Duffin91547182019-11-12 19:39:36 +00001944}