blob: 0947247b9b9147b0615ec05d94e79932d79f0e11 [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
180 // the tag name used to distinguish if the API files belong to public/system/test.
181 Api_tag_name *string
182
183 // the generated public API filename by Doclava.
184 Api_filename *string
185
David Brazdilfbe4cc32018-05-31 13:56:46 +0100186 // the generated public Dex API filename by Doclava.
187 Dex_api_filename *string
188
Nan Zhang28c68b92018-03-13 16:17:01 -0700189 // the generated private API filename by Doclava.
190 Private_api_filename *string
191
192 // the generated private Dex API filename by Doclava.
193 Private_dex_api_filename *string
194
195 // the generated removed API filename by Doclava.
196 Removed_api_filename *string
197
David Brazdilaac0c3c2018-04-24 16:23:29 +0100198 // the generated removed Dex API filename by Doclava.
199 Removed_dex_api_filename *string
200
Mathew Inwood76c3de12018-06-22 15:28:11 +0100201 // mapping of dex signatures to source file and line number. This is a temporary property and
202 // will be deleted; you probably shouldn't be using it.
203 Dex_mapping_filename *string
204
Nan Zhang28c68b92018-03-13 16:17:01 -0700205 // the generated exact API filename by Doclava.
206 Exact_api_filename *string
Nan Zhang853f4202018-04-12 16:55:56 -0700207
Nan Zhang66dc2362018-08-14 20:41:04 -0700208 // the generated proguard filename by Doclava.
209 Proguard_filename *string
210
Nan Zhang853f4202018-04-12 16:55:56 -0700211 // if set to false, don't allow droiddoc to generate stubs source files. Defaults to true.
212 Create_stubs *bool
Nan Zhang61819ce2018-05-04 18:49:16 -0700213
214 Check_api struct {
215 Last_released ApiToCheck
216
217 Current ApiToCheck
Inseob Kim38449af2019-02-28 14:24:05 +0900218
219 // do not perform API check against Last_released, in the case that both two specified API
220 // files by Last_released are modules which don't exist.
221 Ignore_missing_latest_api *bool `blueprint:"mutated"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700222 }
Nan Zhang79614d12018-04-19 18:03:39 -0700223
Nan Zhang1598a9e2018-09-04 17:14:32 -0700224 // if set to true, generate docs through Dokka instead of Doclava.
225 Dokka_enabled *bool
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000226
227 // Compat config XML. Generates compat change documentation if set.
228 Compat_config *string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -0700229}
230
231type DroidstubsProperties struct {
232 // the tag name used to distinguish if the API files belong to public/system/test.
233 Api_tag_name *string
234
Nan Zhang199645c2018-09-19 12:40:06 -0700235 // the generated public API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700236 Api_filename *string
237
Nan Zhang199645c2018-09-19 12:40:06 -0700238 // the generated public Dex API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700239 Dex_api_filename *string
240
Nan Zhang199645c2018-09-19 12:40:06 -0700241 // the generated private API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700242 Private_api_filename *string
243
Nan Zhang199645c2018-09-19 12:40:06 -0700244 // the generated private Dex API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700245 Private_dex_api_filename *string
246
Nan Zhang199645c2018-09-19 12:40:06 -0700247 // the generated removed API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700248 Removed_api_filename *string
249
Nan Zhang199645c2018-09-19 12:40:06 -0700250 // the generated removed Dex API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700251 Removed_dex_api_filename *string
252
Nan Zhang9c69a122018-08-22 10:22:08 -0700253 // mapping of dex signatures to source file and line number. This is a temporary property and
254 // will be deleted; you probably shouldn't be using it.
255 Dex_mapping_filename *string
256
Nan Zhang199645c2018-09-19 12:40:06 -0700257 // the generated exact API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700258 Exact_api_filename *string
259
Nan Zhang199645c2018-09-19 12:40:06 -0700260 // the generated proguard filename by Metalava.
261 Proguard_filename *string
262
Nan Zhang1598a9e2018-09-04 17:14:32 -0700263 Check_api struct {
264 Last_released ApiToCheck
265
266 Current ApiToCheck
Inseob Kim38449af2019-02-28 14:24:05 +0900267
268 // do not perform API check against Last_released, in the case that both two specified API
269 // files by Last_released are modules which don't exist.
270 Ignore_missing_latest_api *bool `blueprint:"mutated"`
Adrian Roos075eedc2019-10-10 12:07:03 +0200271
272 Api_lint struct {
273 Enabled *bool
274
275 // If set, performs api_lint on any new APIs not found in the given signature file
276 New_since *string `android:"path"`
277
278 // If not blank, path to the baseline txt file for approved API lint violations.
279 Baseline_file *string `android:"path"`
280 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700281 }
Nan Zhang79614d12018-04-19 18:03:39 -0700282
283 // user can specify the version of previous released API file in order to do compatibility check.
Colin Cross27b922f2019-03-04 22:35:41 -0800284 Previous_api *string `android:"path"`
Nan Zhang79614d12018-04-19 18:03:39 -0700285
286 // is set to true, Metalava will allow framework SDK to contain annotations.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700287 Annotations_enabled *bool
Nan Zhang79614d12018-04-19 18:03:39 -0700288
Pete Gillin77167902018-09-19 18:16:26 +0100289 // 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 -0700290 Merge_annotations_dirs []string
Nan Zhang86d2d552018-08-09 15:33:27 -0700291
Pete Gillin77167902018-09-19 18:16:26 +0100292 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
293 Merge_inclusion_annotations_dirs []string
294
Pete Gillinc382a562018-11-14 18:45:46 +0000295 // a file containing a list of classes to do nullability validation for.
296 Validate_nullability_from_list *string
297
Pete Gillin581d6082018-10-22 15:55:04 +0100298 // a file containing expected warnings produced by validation of nullability annotations.
299 Check_nullability_warnings *string
300
Nan Zhang1598a9e2018-09-04 17:14:32 -0700301 // if set to true, allow Metalava to generate doc_stubs source files. Defaults to false.
302 Create_doc_stubs *bool
Nan Zhang9c69a122018-08-22 10:22:08 -0700303
Paul Duffin3ae29512020-04-08 18:18:03 +0100304 // if set to false then do not write out stubs. Defaults to true.
305 //
306 // TODO(b/146727827): Remove capability when we do not need to generate stubs and API separately.
307 Generate_stubs *bool
308
Nan Zhang9c69a122018-08-22 10:22:08 -0700309 // is set to true, Metalava will allow framework SDK to contain API levels annotations.
310 Api_levels_annotations_enabled *bool
311
312 // the dirs which Metalava extracts API levels annotations from.
313 Api_levels_annotations_dirs []string
314
315 // if set to true, collect the values used by the Dev tools and
316 // write them in files packaged with the SDK. Defaults to false.
317 Write_sdk_values *bool
Nan Zhang71bbe632018-09-17 14:32:21 -0700318
319 // If set to true, .xml based public API file will be also generated, and
320 // JDiff tool will be invoked to genreate javadoc files. Defaults to false.
321 Jdiff_enabled *bool
Nan Zhang581fd212018-01-10 16:06:12 -0800322}
323
Nan Zhanga40da042018-08-01 12:48:00 -0700324//
325// Common flags passed down to build rule
326//
327type droiddocBuilderFlags struct {
Nan Zhang86d2d552018-08-09 15:33:27 -0700328 bootClasspathArgs string
329 classpathArgs string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700330 sourcepathArgs string
Nan Zhang86d2d552018-08-09 15:33:27 -0700331 dokkaClasspathArgs string
332 aidlFlags string
Colin Cross3047fa22019-04-18 10:56:44 -0700333 aidlDeps android.Paths
Nan Zhanga40da042018-08-01 12:48:00 -0700334
Nan Zhanga40da042018-08-01 12:48:00 -0700335 doclavaStubsFlags string
Nan Zhang86d2d552018-08-09 15:33:27 -0700336 doclavaDocsFlags string
Nan Zhanga40da042018-08-01 12:48:00 -0700337 postDoclavaCmds string
Nan Zhanga40da042018-08-01 12:48:00 -0700338}
339
340func InitDroiddocModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
341 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
342 android.InitDefaultableModule(module)
343}
344
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200345func apiCheckEnabled(ctx android.ModuleContext, apiToCheck ApiToCheck, apiVersionTag string) bool {
346 if ctx.Config().IsEnvTrue("WITHOUT_CHECK_API") {
347 return false
348 } else if String(apiToCheck.Api_file) != "" && String(apiToCheck.Removed_api_file) != "" {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700349 return true
350 } else if String(apiToCheck.Api_file) != "" {
351 panic("for " + apiVersionTag + " removed_api_file has to be non-empty!")
352 } else if String(apiToCheck.Removed_api_file) != "" {
353 panic("for " + apiVersionTag + " api_file has to be non-empty!")
354 }
355
356 return false
357}
358
Inseob Kim38449af2019-02-28 14:24:05 +0900359func ignoreMissingModules(ctx android.BottomUpMutatorContext, apiToCheck *ApiToCheck) {
360 api_file := String(apiToCheck.Api_file)
361 removed_api_file := String(apiToCheck.Removed_api_file)
362
363 api_module := android.SrcIsModule(api_file)
364 removed_api_module := android.SrcIsModule(removed_api_file)
365
366 if api_module == "" || removed_api_module == "" {
367 return
368 }
369
370 if ctx.OtherModuleExists(api_module) || ctx.OtherModuleExists(removed_api_module) {
371 return
372 }
373
374 apiToCheck.Api_file = nil
375 apiToCheck.Removed_api_file = nil
376}
377
Nan Zhang1598a9e2018-09-04 17:14:32 -0700378type ApiFilePath interface {
379 ApiFilePath() android.Path
380}
381
Nan Zhanga40da042018-08-01 12:48:00 -0700382//
383// Javadoc
384//
Nan Zhang581fd212018-01-10 16:06:12 -0800385type Javadoc struct {
386 android.ModuleBase
387 android.DefaultableModuleBase
388
389 properties JavadocProperties
390
391 srcJars android.Paths
392 srcFiles android.Paths
393 sourcepaths android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700394 argFiles android.Paths
395
396 args string
Nan Zhang581fd212018-01-10 16:06:12 -0800397
Nan Zhangccff0f72018-03-08 17:26:16 -0800398 docZip android.WritablePath
399 stubsSrcJar android.WritablePath
Nan Zhang581fd212018-01-10 16:06:12 -0800400}
401
Colin Cross41955e82019-05-29 14:40:35 -0700402func (j *Javadoc) OutputFiles(tag string) (android.Paths, error) {
403 switch tag {
404 case "":
405 return android.Paths{j.stubsSrcJar}, nil
Colin Crosse68e5542019-08-12 13:11:40 -0700406 case ".docs.zip":
407 return android.Paths{j.docZip}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700408 default:
409 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
410 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800411}
412
Colin Crossa3002fc2019-07-08 16:48:04 -0700413// javadoc converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800414func JavadocFactory() android.Module {
415 module := &Javadoc{}
416
417 module.AddProperties(&module.properties)
418
419 InitDroiddocModule(module, android.HostAndDeviceSupported)
420 return module
421}
422
Colin Crossa3002fc2019-07-08 16:48:04 -0700423// javadoc_host converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800424func JavadocHostFactory() android.Module {
425 module := &Javadoc{}
426
427 module.AddProperties(&module.properties)
428
429 InitDroiddocModule(module, android.HostSupported)
430 return module
431}
432
Colin Cross41955e82019-05-29 14:40:35 -0700433var _ android.OutputFileProducer = (*Javadoc)(nil)
Nan Zhang581fd212018-01-10 16:06:12 -0800434
Jiyong Park6a927c42020-01-21 02:03:43 +0900435func (j *Javadoc) sdkVersion() sdkSpec {
436 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700437}
438
Paul Duffine25c6442019-10-11 13:50:28 +0100439func (j *Javadoc) systemModules() string {
440 return proptools.String(j.properties.System_modules)
441}
442
Jiyong Park6a927c42020-01-21 02:03:43 +0900443func (j *Javadoc) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700444 return j.sdkVersion()
445}
446
Jiyong Park6a927c42020-01-21 02:03:43 +0900447func (j *Javadoc) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700448 return j.sdkVersion()
449}
450
Nan Zhang581fd212018-01-10 16:06:12 -0800451func (j *Javadoc) addDeps(ctx android.BottomUpMutatorContext) {
452 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100453 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700454 if sdkDep.useDefaultLibs {
455 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
456 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
457 if sdkDep.hasFrameworkLibs() {
458 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Nan Zhang357466b2018-04-17 17:38:36 -0700459 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700460 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700461 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100462 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700463 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Nan Zhang581fd212018-01-10 16:06:12 -0800464 }
465 }
466
Colin Cross42d48b72018-08-29 14:10:52 -0700467 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800468}
469
Nan Zhanga40da042018-08-01 12:48:00 -0700470func (j *Javadoc) collectAidlFlags(ctx android.ModuleContext, deps deps) droiddocBuilderFlags {
471 var flags droiddocBuilderFlags
Jiyong Park1e440682018-05-23 18:42:04 +0900472
Colin Cross3047fa22019-04-18 10:56:44 -0700473 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Jiyong Park1e440682018-05-23 18:42:04 +0900474
475 return flags
476}
477
478func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700479 aidlIncludeDirs android.Paths) (string, android.Paths) {
Jiyong Park1e440682018-05-23 18:42:04 +0900480
481 aidlIncludes := android.PathsForModuleSrc(ctx, j.properties.Aidl.Local_include_dirs)
482 aidlIncludes = append(aidlIncludes, android.PathsForSource(ctx, j.properties.Aidl.Include_dirs)...)
483
484 var flags []string
Colin Cross3047fa22019-04-18 10:56:44 -0700485 var deps android.Paths
486
Jiyong Park1e440682018-05-23 18:42:04 +0900487 if aidlPreprocess.Valid() {
488 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700489 deps = append(deps, aidlPreprocess.Path())
Jiyong Park1e440682018-05-23 18:42:04 +0900490 } else {
491 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
492 }
493
494 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
495 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
496 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
497 flags = append(flags, "-I"+src.String())
498 }
499
Colin Cross3047fa22019-04-18 10:56:44 -0700500 return strings.Join(flags, " "), deps
Jiyong Park1e440682018-05-23 18:42:04 +0900501}
502
Jiyong Parkd90d7412019-08-20 22:49:19 +0900503// TODO: remove the duplication between this and the one in gen.go
Jiyong Park1e440682018-05-23 18:42:04 +0900504func (j *Javadoc) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Nan Zhanga40da042018-08-01 12:48:00 -0700505 flags droiddocBuilderFlags) android.Paths {
Jiyong Park1e440682018-05-23 18:42:04 +0900506
507 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Crossc0806172019-06-14 18:51:47 -0700508 var aidlSrcs android.Paths
Jiyong Park1e440682018-05-23 18:42:04 +0900509
Jiyong Park1112c4c2019-08-16 21:12:10 +0900510 aidlIncludeFlags := genAidlIncludeFlags(srcFiles)
511
Jiyong Park1e440682018-05-23 18:42:04 +0900512 for _, srcFile := range srcFiles {
513 switch srcFile.Ext() {
514 case ".aidl":
Colin Crossc0806172019-06-14 18:51:47 -0700515 aidlSrcs = append(aidlSrcs, srcFile)
Jiyong Parkd90d7412019-08-20 22:49:19 +0900516 case ".logtags":
517 javaFile := genLogtags(ctx, srcFile)
518 outSrcFiles = append(outSrcFiles, javaFile)
Jiyong Park1e440682018-05-23 18:42:04 +0900519 default:
520 outSrcFiles = append(outSrcFiles, srcFile)
521 }
522 }
523
Colin Crossc0806172019-06-14 18:51:47 -0700524 // Process all aidl files together to support sharding them into one or more rules that produce srcjars.
525 if len(aidlSrcs) > 0 {
526 srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags+aidlIncludeFlags, flags.aidlDeps)
527 outSrcFiles = append(outSrcFiles, srcJarFiles...)
528 }
529
Jiyong Park1e440682018-05-23 18:42:04 +0900530 return outSrcFiles
531}
532
Nan Zhang581fd212018-01-10 16:06:12 -0800533func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
534 var deps deps
535
Colin Cross83bb3162018-06-25 15:48:06 -0700536 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800537 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700538 ctx.AddMissingDependencies(sdkDep.bootclasspath)
539 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Nan Zhang581fd212018-01-10 16:06:12 -0800540 } else if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700541 deps.bootClasspath = append(deps.bootClasspath, sdkDep.jars...)
Anton Hansson26bf49b2020-02-08 20:26:29 +0000542 deps.aidlPreprocess = sdkDep.aidl
543 } else {
544 deps.aidlPreprocess = sdkDep.aidl
Nan Zhang581fd212018-01-10 16:06:12 -0800545 }
546
547 ctx.VisitDirectDeps(func(module android.Module) {
548 otherName := ctx.OtherModuleName(module)
549 tag := ctx.OtherModuleDependencyTag(module)
550
Colin Cross2d24c1b2018-05-23 10:59:18 -0700551 switch tag {
552 case bootClasspathTag:
553 if dep, ok := module.(Dependency); ok {
Nan Zhang581fd212018-01-10 16:06:12 -0800554 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars()...)
Paul Duffin83a2d962019-11-19 19:44:10 +0000555 } else if sm, ok := module.(SystemModulesProvider); ok {
Paul Duffine25c6442019-10-11 13:50:28 +0100556 // A system modules dependency has been added to the bootclasspath
557 // so add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +0000558 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700559 } else {
560 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
561 }
562 case libTag:
563 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800564 case SdkLibraryDependency:
565 deps.classpath = append(deps.classpath, dep.SdkImplementationJars(ctx, j.sdkVersion())...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700566 case Dependency:
Sundong Ahnba493602018-11-20 17:36:35 +0900567 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park19a7f252019-07-10 16:59:31 +0900568 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700569 case android.SourceFileProducer:
Nan Zhang581fd212018-01-10 16:06:12 -0800570 checkProducesJars(ctx, dep)
571 deps.classpath = append(deps.classpath, dep.Srcs()...)
Nan Zhang581fd212018-01-10 16:06:12 -0800572 default:
573 ctx.ModuleErrorf("depends on non-java module %q", otherName)
574 }
Colin Cross6cef4812019-10-17 14:23:50 -0700575 case java9LibTag:
576 switch dep := module.(type) {
577 case Dependency:
578 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
579 default:
580 ctx.ModuleErrorf("depends on non-java module %q", otherName)
581 }
Nan Zhang357466b2018-04-17 17:38:36 -0700582 case systemModulesTag:
583 if deps.systemModules != nil {
584 panic("Found two system module dependencies")
585 }
Paul Duffin83a2d962019-11-19 19:44:10 +0000586 sm := module.(SystemModulesProvider)
587 outputDir, outputDeps := sm.OutputDirAndDeps()
588 deps.systemModules = &systemModules{outputDir, outputDeps}
Nan Zhang581fd212018-01-10 16:06:12 -0800589 }
590 })
591 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
592 // may contain filegroup or genrule.
Colin Cross8a497952019-03-05 22:25:09 -0800593 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900594
595 filterByPackage := func(srcs []android.Path, filterPackages []string) []android.Path {
596 if filterPackages == nil {
597 return srcs
598 }
599 filtered := []android.Path{}
600 for _, src := range srcs {
601 if src.Ext() != ".java" {
602 // Don't filter-out non-Java (=generated sources) by package names. This is not ideal,
603 // but otherwise metalava emits stub sources having references to the generated AIDL classes
604 // in filtered-out pacages (e.g. com.android.internal.*).
605 // TODO(b/141149570) We need to fix this by introducing default private constructors or
606 // fixing metalava to not emit constructors having references to unknown classes.
607 filtered = append(filtered, src)
608 continue
609 }
610 packageName := strings.ReplaceAll(filepath.Dir(src.Rel()), "/", ".")
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800611 if android.HasAnyPrefix(packageName, filterPackages) {
612 filtered = append(filtered, src)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900613 }
614 }
615 return filtered
616 }
617 srcFiles = filterByPackage(srcFiles, j.properties.Filter_packages)
618
Nan Zhanga40da042018-08-01 12:48:00 -0700619 flags := j.collectAidlFlags(ctx, deps)
Jiyong Park1e440682018-05-23 18:42:04 +0900620 srcFiles = j.genSources(ctx, srcFiles, flags)
Nan Zhang581fd212018-01-10 16:06:12 -0800621
622 // srcs may depend on some genrule output.
623 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800624 j.srcJars = append(j.srcJars, deps.srcJars...)
625
Nan Zhang581fd212018-01-10 16:06:12 -0800626 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800627 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800628
Nan Zhang9c69a122018-08-22 10:22:08 -0700629 if j.properties.Local_sourcepaths == nil && len(j.srcFiles) > 0 {
Nan Zhang581fd212018-01-10 16:06:12 -0800630 j.properties.Local_sourcepaths = append(j.properties.Local_sourcepaths, ".")
631 }
632 j.sourcepaths = android.PathsForModuleSrc(ctx, j.properties.Local_sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800633
Colin Cross8a497952019-03-05 22:25:09 -0800634 j.argFiles = android.PathsForModuleSrc(ctx, j.properties.Arg_files)
Paul Duffin99e4a502019-02-11 15:38:42 +0000635 argFilesMap := map[string]string{}
636 argFileLabels := []string{}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700637
Paul Duffin99e4a502019-02-11 15:38:42 +0000638 for _, label := range j.properties.Arg_files {
Colin Cross8a497952019-03-05 22:25:09 -0800639 var paths = android.PathsForModuleSrc(ctx, []string{label})
Paul Duffin99e4a502019-02-11 15:38:42 +0000640 if _, exists := argFilesMap[label]; !exists {
641 argFilesMap[label] = strings.Join(paths.Strings(), " ")
642 argFileLabels = append(argFileLabels, label)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700643 } else {
644 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000645 label, argFilesMap[label], paths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700646 }
647 }
648
649 var err error
Colin Cross15638152019-07-11 11:11:35 -0700650 j.args, err = android.Expand(String(j.properties.Args), func(name string) (string, error) {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700651 if strings.HasPrefix(name, "location ") {
652 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Paul Duffin99e4a502019-02-11 15:38:42 +0000653 if paths, ok := argFilesMap[label]; ok {
Colin Cross15638152019-07-11 11:11:35 -0700654 return paths, nil
Nan Zhang1598a9e2018-09-04 17:14:32 -0700655 } else {
Colin Cross15638152019-07-11 11:11:35 -0700656 return "", fmt.Errorf("unknown location label %q, expecting one of %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000657 label, strings.Join(argFileLabels, ", "))
Nan Zhang1598a9e2018-09-04 17:14:32 -0700658 }
659 } else if name == "genDir" {
Colin Cross15638152019-07-11 11:11:35 -0700660 return android.PathForModuleGen(ctx).String(), nil
Nan Zhang1598a9e2018-09-04 17:14:32 -0700661 }
Colin Cross15638152019-07-11 11:11:35 -0700662 return "", fmt.Errorf("unknown variable '$(%s)'", name)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700663 })
664
665 if err != nil {
666 ctx.PropertyErrorf("args", "%s", err.Error())
667 }
668
Nan Zhang581fd212018-01-10 16:06:12 -0800669 return deps
670}
671
672func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
673 j.addDeps(ctx)
674}
675
676func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
677 deps := j.collectDeps(ctx)
678
Colin Crossdaa4c672019-07-15 22:53:46 -0700679 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800680
Colin Crossdaa4c672019-07-15 22:53:46 -0700681 outDir := android.PathForModuleOut(ctx, "out")
682 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
683
684 j.stubsSrcJar = nil
685
686 rule := android.NewRuleBuilder()
687
688 rule.Command().Text("rm -rf").Text(outDir.String())
689 rule.Command().Text("mkdir -p").Text(outDir.String())
690
691 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, j.srcJars)
Nan Zhang357466b2018-04-17 17:38:36 -0700692
Colin Cross83bb3162018-06-25 15:48:06 -0700693 javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800694
Colin Crossdaa4c672019-07-15 22:53:46 -0700695 cmd := javadocSystemModulesCmd(ctx, rule, j.srcFiles, outDir, srcJarDir, srcJarList,
696 deps.systemModules, deps.classpath, j.sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800697
Colin Cross1e743852019-10-28 11:37:20 -0700698 cmd.FlagWithArg("-source ", javaVersion.String()).
Colin Crossdaa4c672019-07-15 22:53:46 -0700699 Flag("-J-Xmx1024m").
700 Flag("-XDignore.symbol.file").
701 Flag("-Xdoclint:none")
Nan Zhang581fd212018-01-10 16:06:12 -0800702
Colin Crossdaa4c672019-07-15 22:53:46 -0700703 rule.Command().
704 BuiltTool(ctx, "soong_zip").
705 Flag("-write_if_changed").
706 Flag("-d").
707 FlagWithOutput("-o ", j.docZip).
708 FlagWithArg("-C ", outDir.String()).
709 FlagWithArg("-D ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700710
Colin Crossdaa4c672019-07-15 22:53:46 -0700711 rule.Restat()
712
713 zipSyncCleanupCmd(rule, srcJarDir)
714
715 rule.Build(pctx, ctx, "javadoc", "javadoc")
Nan Zhang581fd212018-01-10 16:06:12 -0800716}
717
Nan Zhanga40da042018-08-01 12:48:00 -0700718//
719// Droiddoc
720//
721type Droiddoc struct {
722 Javadoc
723
724 properties DroiddocProperties
725 apiFile android.WritablePath
726 dexApiFile android.WritablePath
727 privateApiFile android.WritablePath
728 privateDexApiFile android.WritablePath
729 removedApiFile android.WritablePath
730 removedDexApiFile android.WritablePath
731 exactApiFile android.WritablePath
732 apiMappingFile android.WritablePath
Nan Zhang66dc2362018-08-14 20:41:04 -0700733 proguardFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700734
735 checkCurrentApiTimestamp android.WritablePath
736 updateCurrentApiTimestamp android.WritablePath
737 checkLastReleasedApiTimestamp android.WritablePath
738
Nan Zhanga40da042018-08-01 12:48:00 -0700739 apiFilePath android.Path
740}
741
Colin Crossa3002fc2019-07-08 16:48:04 -0700742// droiddoc converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700743func DroiddocFactory() android.Module {
744 module := &Droiddoc{}
745
746 module.AddProperties(&module.properties,
747 &module.Javadoc.properties)
748
749 InitDroiddocModule(module, android.HostAndDeviceSupported)
750 return module
751}
752
Colin Crossa3002fc2019-07-08 16:48:04 -0700753// droiddoc_host converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700754func DroiddocHostFactory() android.Module {
755 module := &Droiddoc{}
756
757 module.AddProperties(&module.properties,
758 &module.Javadoc.properties)
759
760 InitDroiddocModule(module, android.HostSupported)
761 return module
762}
763
764func (d *Droiddoc) ApiFilePath() android.Path {
765 return d.apiFilePath
766}
767
Nan Zhang581fd212018-01-10 16:06:12 -0800768func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
769 d.Javadoc.addDeps(ctx)
770
Inseob Kim38449af2019-02-28 14:24:05 +0900771 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
772 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
773 }
774
Nan Zhang79614d12018-04-19 18:03:39 -0700775 if String(d.properties.Custom_template) != "" {
Dan Willemsencc090972018-02-26 14:33:31 -0800776 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
777 }
Nan Zhang581fd212018-01-10 16:06:12 -0800778}
779
Colin Crossab054432019-07-15 16:13:59 -0700780func (d *Droiddoc) doclavaDocsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, docletPath classpath) {
Colin Cross2a2e0db2020-02-21 16:55:46 -0800781 buildNumberFile := ctx.Config().BuildNumberFile(ctx)
Nan Zhang443fa522018-08-20 20:58:28 -0700782 // Droiddoc always gets "-source 1.8" because it doesn't support 1.9 sources. For modules with 1.9
783 // sources, droiddoc will get sources produced by metalava which will have already stripped out the
784 // 1.9 language features.
Colin Crossab054432019-07-15 16:13:59 -0700785 cmd.FlagWithArg("-source ", "1.8").
786 Flag("-J-Xmx1600m").
787 Flag("-J-XX:-OmitStackTraceInFastThrow").
788 Flag("-XDignore.symbol.file").
789 FlagWithArg("-doclet ", "com.google.doclava.Doclava").
790 FlagWithInputList("-docletpath ", docletPath.Paths(), ":").
Colin Cross2a2e0db2020-02-21 16:55:46 -0800791 FlagWithArg("-hdf page.build ", ctx.Config().BuildId()+"-$(cat "+buildNumberFile.String()+")").OrderOnly(buildNumberFile).
Elliott Hughes26bce342019-09-12 15:05:13 -0700792 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 -0700793
Nan Zhanga40da042018-08-01 12:48:00 -0700794 if String(d.properties.Custom_template) == "" {
795 // TODO: This is almost always droiddoc-templates-sdk
796 ctx.PropertyErrorf("custom_template", "must specify a template")
797 }
798
799 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
Nan Zhangf4936b02018-08-01 15:00:28 -0700800 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Crossab054432019-07-15 16:13:59 -0700801 cmd.FlagWithArg("-templatedir ", t.dir.String()).Implicits(t.deps)
Nan Zhanga40da042018-08-01 12:48:00 -0700802 } else {
Paul Duffin884363e2019-12-19 10:21:09 +0000803 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_exported_dir", ctx.OtherModuleName(m))
Nan Zhanga40da042018-08-01 12:48:00 -0700804 }
805 })
806
807 if len(d.properties.Html_dirs) > 0 {
Colin Crossab054432019-07-15 16:13:59 -0700808 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
809 cmd.FlagWithArg("-htmldir ", htmlDir.String()).
810 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[0], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700811 }
812
813 if len(d.properties.Html_dirs) > 1 {
Colin Crossab054432019-07-15 16:13:59 -0700814 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
815 cmd.FlagWithArg("-htmldir2 ", htmlDir2.String()).
816 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[1], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700817 }
818
819 if len(d.properties.Html_dirs) > 2 {
820 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
821 }
822
Colin Cross8a497952019-03-05 22:25:09 -0800823 knownTags := android.PathsForModuleSrc(ctx, d.properties.Knowntags)
Colin Crossab054432019-07-15 16:13:59 -0700824 cmd.FlagForEachInput("-knowntags ", knownTags)
Nan Zhanga40da042018-08-01 12:48:00 -0700825
Colin Crossab054432019-07-15 16:13:59 -0700826 cmd.FlagForEachArg("-hdf ", d.properties.Hdf)
Nan Zhanga40da042018-08-01 12:48:00 -0700827
828 if String(d.properties.Proofread_file) != "" {
829 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
Colin Crossab054432019-07-15 16:13:59 -0700830 cmd.FlagWithOutput("-proofread ", proofreadFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700831 }
832
833 if String(d.properties.Todo_file) != "" {
834 // tricky part:
835 // we should not compute full path for todo_file through PathForModuleOut().
836 // the non-standard doclet will get the full path relative to "-o".
Colin Crossab054432019-07-15 16:13:59 -0700837 cmd.FlagWithArg("-todo ", String(d.properties.Todo_file)).
838 ImplicitOutput(android.PathForModuleOut(ctx, String(d.properties.Todo_file)))
Nan Zhanga40da042018-08-01 12:48:00 -0700839 }
840
841 if String(d.properties.Resourcesdir) != "" {
842 // TODO: should we add files under resourcesDir to the implicits? It seems that
843 // resourcesDir is one sub dir of htmlDir
844 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
Colin Crossab054432019-07-15 16:13:59 -0700845 cmd.FlagWithArg("-resourcesdir ", resourcesDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700846 }
847
848 if String(d.properties.Resourcesoutdir) != "" {
849 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
Colin Crossab054432019-07-15 16:13:59 -0700850 cmd.FlagWithArg("-resourcesoutdir ", String(d.properties.Resourcesoutdir))
Nan Zhanga40da042018-08-01 12:48:00 -0700851 }
Nan Zhanga40da042018-08-01 12:48:00 -0700852}
853
Colin Crossab054432019-07-15 16:13:59 -0700854func (d *Droiddoc) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.WritablePath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200855 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
856 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -0700857 String(d.properties.Api_filename) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700858
Nan Zhanga40da042018-08-01 12:48:00 -0700859 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Colin Crossab054432019-07-15 16:13:59 -0700860 cmd.FlagWithOutput("-api ", d.apiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700861 d.apiFilePath = d.apiFile
862 }
863
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200864 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
865 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -0700866 String(d.properties.Removed_api_filename) != "" {
Nan Zhanga40da042018-08-01 12:48:00 -0700867 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
Colin Crossab054432019-07-15 16:13:59 -0700868 cmd.FlagWithOutput("-removedApi ", d.removedApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700869 }
870
871 if String(d.properties.Private_api_filename) != "" {
872 d.privateApiFile = android.PathForModuleOut(ctx, String(d.properties.Private_api_filename))
Colin Crossab054432019-07-15 16:13:59 -0700873 cmd.FlagWithOutput("-privateApi ", d.privateApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700874 }
875
876 if String(d.properties.Dex_api_filename) != "" {
877 d.dexApiFile = android.PathForModuleOut(ctx, String(d.properties.Dex_api_filename))
Colin Crossab054432019-07-15 16:13:59 -0700878 cmd.FlagWithOutput("-dexApi ", d.dexApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700879 }
880
881 if String(d.properties.Private_dex_api_filename) != "" {
882 d.privateDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Private_dex_api_filename))
Colin Crossab054432019-07-15 16:13:59 -0700883 cmd.FlagWithOutput("-privateDexApi ", d.privateDexApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700884 }
885
886 if String(d.properties.Removed_dex_api_filename) != "" {
887 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Crossab054432019-07-15 16:13:59 -0700888 cmd.FlagWithOutput("-removedDexApi ", d.removedDexApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700889 }
890
891 if String(d.properties.Exact_api_filename) != "" {
892 d.exactApiFile = android.PathForModuleOut(ctx, String(d.properties.Exact_api_filename))
Colin Crossab054432019-07-15 16:13:59 -0700893 cmd.FlagWithOutput("-exactApi ", d.exactApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700894 }
895
896 if String(d.properties.Dex_mapping_filename) != "" {
897 d.apiMappingFile = android.PathForModuleOut(ctx, String(d.properties.Dex_mapping_filename))
Colin Crossab054432019-07-15 16:13:59 -0700898 cmd.FlagWithOutput("-apiMapping ", d.apiMappingFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700899 }
900
Nan Zhang66dc2362018-08-14 20:41:04 -0700901 if String(d.properties.Proguard_filename) != "" {
902 d.proguardFile = android.PathForModuleOut(ctx, String(d.properties.Proguard_filename))
Colin Crossab054432019-07-15 16:13:59 -0700903 cmd.FlagWithOutput("-proguard ", d.proguardFile)
Nan Zhang66dc2362018-08-14 20:41:04 -0700904 }
905
Nan Zhanga40da042018-08-01 12:48:00 -0700906 if BoolDefault(d.properties.Create_stubs, true) {
Colin Crossab054432019-07-15 16:13:59 -0700907 cmd.FlagWithArg("-stubs ", stubsDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700908 }
909
910 if Bool(d.properties.Write_sdk_values) {
Colin Crossab054432019-07-15 16:13:59 -0700911 cmd.FlagWithArg("-sdkvalues ", android.PathForModuleOut(ctx, "out").String())
Nan Zhanga40da042018-08-01 12:48:00 -0700912 }
Nan Zhanga40da042018-08-01 12:48:00 -0700913}
914
Colin Crossab054432019-07-15 16:13:59 -0700915func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700916 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700917 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
918 rule.Command().Text("cp").
919 Input(staticDocIndexRedirect).
920 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700921 }
922
923 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700924 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
925 rule.Command().Text("cp").
926 Input(staticDocProperties).
927 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700928 }
Nan Zhanga40da042018-08-01 12:48:00 -0700929}
930
Colin Crossab054432019-07-15 16:13:59 -0700931func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700932 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700933
934 cmd := rule.Command().
935 BuiltTool(ctx, "soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
936 Flag(config.JavacVmFlags).
937 FlagWithArg("-encoding ", "UTF-8").
Colin Crossab054432019-07-15 16:13:59 -0700938 FlagWithRspFileInputList("@", srcs).
939 FlagWithInput("@", srcJarList)
940
Colin Crossab054432019-07-15 16:13:59 -0700941 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
942 // based stubs generation.
943 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
944 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
945 // the correct package name base path.
946 if len(sourcepaths) > 0 {
947 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
948 } else {
949 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
950 }
951
952 cmd.FlagWithArg("-d ", outDir.String()).
953 Flag("-quiet")
954
955 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700956}
957
Colin Crossdaa4c672019-07-15 22:53:46 -0700958func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
959 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
960 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
961
962 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
963
964 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
965 cmd.Flag(flag).Implicits(deps)
966
967 cmd.FlagWithArg("--patch-module ", "java.base=.")
968
969 if len(classpath) > 0 {
970 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
971 }
972
973 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700974}
975
Colin Crossdaa4c672019-07-15 22:53:46 -0700976func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
977 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
978 sourcepaths android.Paths) *android.RuleBuilderCommand {
979
980 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
981
982 if len(bootclasspath) == 0 && ctx.Device() {
983 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
984 // ensure java does not fall back to the default bootclasspath.
985 cmd.FlagWithArg("-bootclasspath ", `""`)
986 } else if len(bootclasspath) > 0 {
987 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
988 }
989
990 if len(classpath) > 0 {
991 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
992 }
993
994 return cmd
995}
996
Colin Crossab054432019-07-15 16:13:59 -0700997func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
998 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700999
Colin Crossab054432019-07-15 16:13:59 -07001000 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
1001 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
1002
1003 return rule.Command().
1004 BuiltTool(ctx, "dokka").
1005 Flag(config.JavacVmFlags).
1006 Flag(srcJarDir.String()).
1007 FlagWithInputList("-classpath ", dokkaClasspath, ":").
1008 FlagWithArg("-format ", "dac").
1009 FlagWithArg("-dacRoot ", "/reference/kotlin").
1010 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -07001011}
1012
1013func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1014 deps := d.Javadoc.collectDeps(ctx)
1015
Colin Crossdaa4c672019-07-15 22:53:46 -07001016 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
1017 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1018
Nan Zhang1598a9e2018-09-04 17:14:32 -07001019 jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
1020 doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
1021 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1022 checkApiClasspath := classpath{jsilver, doclava, android.PathForSource(ctx, java8Home, "lib/tools.jar")}
1023
Colin Crossab054432019-07-15 16:13:59 -07001024 outDir := android.PathForModuleOut(ctx, "out")
1025 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
1026 stubsDir := android.PathForModuleOut(ctx, "stubsDir")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001027
Colin Crossab054432019-07-15 16:13:59 -07001028 rule := android.NewRuleBuilder()
Nan Zhang1598a9e2018-09-04 17:14:32 -07001029
Colin Crossab054432019-07-15 16:13:59 -07001030 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
1031 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -07001032
Colin Crossab054432019-07-15 16:13:59 -07001033 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1034
1035 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -07001036 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -07001037 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001038 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -07001039 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -07001040 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001041 }
1042
Colin Crossab054432019-07-15 16:13:59 -07001043 d.stubsFlags(ctx, cmd, stubsDir)
1044
1045 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1046
Mathew Inwoodabd49ab2019-12-19 14:27:08 +00001047 if d.properties.Compat_config != nil {
1048 compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
1049 cmd.FlagWithInput("-compatconfig ", compatConfig)
1050 }
1051
Colin Crossab054432019-07-15 16:13:59 -07001052 var desc string
1053 if Bool(d.properties.Dokka_enabled) {
1054 desc = "dokka"
1055 } else {
1056 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
1057
1058 for _, o := range d.Javadoc.properties.Out {
1059 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1060 }
1061
1062 d.postDoclavaCmds(ctx, rule)
1063 desc = "doclava"
1064 }
1065
1066 rule.Command().
1067 BuiltTool(ctx, "soong_zip").
1068 Flag("-write_if_changed").
1069 Flag("-d").
1070 FlagWithOutput("-o ", d.docZip).
1071 FlagWithArg("-C ", outDir.String()).
1072 FlagWithArg("-D ", outDir.String())
1073
1074 rule.Command().
1075 BuiltTool(ctx, "soong_zip").
1076 Flag("-write_if_changed").
1077 Flag("-jar").
1078 FlagWithOutput("-o ", d.stubsSrcJar).
1079 FlagWithArg("-C ", stubsDir.String()).
1080 FlagWithArg("-D ", stubsDir.String())
1081
1082 rule.Restat()
1083
1084 zipSyncCleanupCmd(rule, srcJarDir)
1085
1086 rule.Build(pctx, ctx, "javadoc", desc)
1087
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001088 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001089 !ctx.Config().IsPdkBuild() {
Colin Crossab054432019-07-15 16:13:59 -07001090
1091 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1092 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Nan Zhang1598a9e2018-09-04 17:14:32 -07001093
1094 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001095
1096 rule := android.NewRuleBuilder()
1097
1098 rule.Command().Text("( true")
1099
1100 rule.Command().
1101 BuiltTool(ctx, "apicheck").
1102 Flag("-JXmx1024m").
1103 FlagWithInputList("-Jclasspath\\ ", checkApiClasspath.Paths(), ":").
1104 OptionalFlag(d.properties.Check_api.Current.Args).
1105 Input(apiFile).
1106 Input(d.apiFile).
1107 Input(removedApiFile).
1108 Input(d.removedApiFile)
1109
1110 msg := fmt.Sprintf(`\n******************************\n`+
1111 `You have tried to change the API from what has been previously approved.\n\n`+
1112 `To make these errors go away, you have two choices:\n`+
1113 ` 1. You can add '@hide' javadoc comments to the methods, etc. listed in the\n`+
1114 ` errors above.\n\n`+
1115 ` 2. You can update current.txt by executing the following command:\n`+
1116 ` make %s-update-current-api\n\n`+
1117 ` To submit the revised current.txt to the main Android repository,\n`+
1118 ` you will need approval.\n`+
1119 `******************************\n`, ctx.ModuleName())
1120
1121 rule.Command().
1122 Text("touch").Output(d.checkCurrentApiTimestamp).
1123 Text(") || (").
1124 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1125 Text("; exit 38").
1126 Text(")")
1127
1128 rule.Build(pctx, ctx, "doclavaCurrentApiCheck", "check current API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001129
1130 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001131
1132 // update API rule
1133 rule = android.NewRuleBuilder()
1134
1135 rule.Command().Text("( true")
1136
1137 rule.Command().
1138 Text("cp").Flag("-f").
1139 Input(d.apiFile).Flag(apiFile.String())
1140
1141 rule.Command().
1142 Text("cp").Flag("-f").
1143 Input(d.removedApiFile).Flag(removedApiFile.String())
1144
1145 msg = "failed to update public API"
1146
1147 rule.Command().
1148 Text("touch").Output(d.updateCurrentApiTimestamp).
1149 Text(") || (").
1150 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1151 Text("; exit 38").
1152 Text(")")
1153
1154 rule.Build(pctx, ctx, "doclavaCurrentApiUpdate", "update current API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001155 }
1156
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001157 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001158 !ctx.Config().IsPdkBuild() {
Colin Crossab054432019-07-15 16:13:59 -07001159
1160 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1161 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
Nan Zhang1598a9e2018-09-04 17:14:32 -07001162
1163 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001164
1165 rule := android.NewRuleBuilder()
1166
1167 rule.Command().
1168 Text("(").
1169 BuiltTool(ctx, "apicheck").
1170 Flag("-JXmx1024m").
1171 FlagWithInputList("-Jclasspath\\ ", checkApiClasspath.Paths(), ":").
1172 OptionalFlag(d.properties.Check_api.Last_released.Args).
1173 Input(apiFile).
1174 Input(d.apiFile).
1175 Input(removedApiFile).
1176 Input(d.removedApiFile)
1177
1178 msg := `\n******************************\n` +
1179 `You have tried to change the API from what has been previously released in\n` +
1180 `an SDK. Please fix the errors listed above.\n` +
1181 `******************************\n`
1182
1183 rule.Command().
1184 Text("touch").Output(d.checkLastReleasedApiTimestamp).
1185 Text(") || (").
1186 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1187 Text("; exit 38").
1188 Text(")")
1189
1190 rule.Build(pctx, ctx, "doclavaLastApiCheck", "check last API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001191 }
1192}
1193
1194//
1195// Droidstubs
1196//
1197type Droidstubs struct {
1198 Javadoc
Paul Duffin91547182019-11-12 19:39:36 +00001199 android.SdkBase
Nan Zhang1598a9e2018-09-04 17:14:32 -07001200
Pete Gillin581d6082018-10-22 15:55:04 +01001201 properties DroidstubsProperties
1202 apiFile android.WritablePath
1203 apiXmlFile android.WritablePath
1204 lastReleasedApiXmlFile android.WritablePath
1205 dexApiFile android.WritablePath
1206 privateApiFile android.WritablePath
1207 privateDexApiFile android.WritablePath
1208 removedApiFile android.WritablePath
1209 removedDexApiFile android.WritablePath
1210 apiMappingFile android.WritablePath
1211 exactApiFile android.WritablePath
1212 proguardFile android.WritablePath
1213 nullabilityWarningsFile android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001214
1215 checkCurrentApiTimestamp android.WritablePath
1216 updateCurrentApiTimestamp android.WritablePath
1217 checkLastReleasedApiTimestamp android.WritablePath
Adrian Roos075eedc2019-10-10 12:07:03 +02001218 apiLintTimestamp android.WritablePath
Adrian Roos3b8f1cd2019-11-01 13:42:39 +01001219 apiLintReport android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001220
Pete Gillin581d6082018-10-22 15:55:04 +01001221 checkNullabilityWarningsTimestamp android.WritablePath
1222
Nan Zhang1598a9e2018-09-04 17:14:32 -07001223 annotationsZip android.WritablePath
Nan Zhang9c69a122018-08-22 10:22:08 -07001224 apiVersionsXml android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001225
1226 apiFilePath android.Path
Nan Zhang71bbe632018-09-17 14:32:21 -07001227
1228 jdiffDocZip android.WritablePath
1229 jdiffStubsSrcJar android.WritablePath
Jerome Gaillard0f599032019-10-10 19:29:11 +01001230
1231 metadataZip android.WritablePath
1232 metadataDir android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001233}
1234
Colin Crossa3002fc2019-07-08 16:48:04 -07001235// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
1236// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to
1237// a droiddoc module to generate documentation.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001238func DroidstubsFactory() android.Module {
1239 module := &Droidstubs{}
1240
1241 module.AddProperties(&module.properties,
1242 &module.Javadoc.properties)
1243
1244 InitDroiddocModule(module, android.HostAndDeviceSupported)
Paul Duffin91547182019-11-12 19:39:36 +00001245 android.InitSdkAwareModule(module)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001246 return module
1247}
1248
Colin Crossa3002fc2019-07-08 16:48:04 -07001249// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API
1250// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be
1251// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs
1252// module when symbols needed by the source files are provided by java_library_host modules.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001253func DroidstubsHostFactory() android.Module {
1254 module := &Droidstubs{}
1255
1256 module.AddProperties(&module.properties,
1257 &module.Javadoc.properties)
1258
1259 InitDroiddocModule(module, android.HostSupported)
1260 return module
1261}
1262
1263func (d *Droidstubs) ApiFilePath() android.Path {
1264 return d.apiFilePath
1265}
1266
1267func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
1268 d.Javadoc.addDeps(ctx)
1269
Inseob Kim38449af2019-02-28 14:24:05 +09001270 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
1271 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
1272 }
1273
Nan Zhang1598a9e2018-09-04 17:14:32 -07001274 if len(d.properties.Merge_annotations_dirs) != 0 {
1275 for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
1276 ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
1277 }
1278 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001279
Pete Gillin77167902018-09-19 18:16:26 +01001280 if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
1281 for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
1282 ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
1283 }
1284 }
1285
Nan Zhang9c69a122018-08-22 10:22:08 -07001286 if len(d.properties.Api_levels_annotations_dirs) != 0 {
1287 for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
1288 ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
1289 }
1290 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001291}
1292
Paul Duffin3ae29512020-04-08 18:18:03 +01001293func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001294 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1295 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001296 String(d.properties.Api_filename) != "" {
1297 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001298 cmd.FlagWithOutput("--api ", d.apiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001299 d.apiFilePath = d.apiFile
1300 }
1301
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001302 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1303 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001304 String(d.properties.Removed_api_filename) != "" {
1305 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001306 cmd.FlagWithOutput("--removed-api ", d.removedApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001307 }
1308
1309 if String(d.properties.Private_api_filename) != "" {
1310 d.privateApiFile = android.PathForModuleOut(ctx, String(d.properties.Private_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001311 cmd.FlagWithOutput("--private-api ", d.privateApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001312 }
1313
1314 if String(d.properties.Dex_api_filename) != "" {
1315 d.dexApiFile = android.PathForModuleOut(ctx, String(d.properties.Dex_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001316 cmd.FlagWithOutput("--dex-api ", d.dexApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001317 }
1318
1319 if String(d.properties.Private_dex_api_filename) != "" {
1320 d.privateDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Private_dex_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001321 cmd.FlagWithOutput("--private-dex-api ", d.privateDexApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001322 }
1323
1324 if String(d.properties.Removed_dex_api_filename) != "" {
1325 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001326 cmd.FlagWithOutput("--removed-dex-api ", d.removedDexApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001327 }
1328
1329 if String(d.properties.Exact_api_filename) != "" {
1330 d.exactApiFile = android.PathForModuleOut(ctx, String(d.properties.Exact_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001331 cmd.FlagWithOutput("--exact-api ", d.exactApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001332 }
1333
Nan Zhang9c69a122018-08-22 10:22:08 -07001334 if String(d.properties.Dex_mapping_filename) != "" {
1335 d.apiMappingFile = android.PathForModuleOut(ctx, String(d.properties.Dex_mapping_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001336 cmd.FlagWithOutput("--dex-api-mapping ", d.apiMappingFile)
Nan Zhang9c69a122018-08-22 10:22:08 -07001337 }
1338
Nan Zhang199645c2018-09-19 12:40:06 -07001339 if String(d.properties.Proguard_filename) != "" {
1340 d.proguardFile = android.PathForModuleOut(ctx, String(d.properties.Proguard_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001341 cmd.FlagWithOutput("--proguard ", d.proguardFile)
Nan Zhang199645c2018-09-19 12:40:06 -07001342 }
1343
Nan Zhang9c69a122018-08-22 10:22:08 -07001344 if Bool(d.properties.Write_sdk_values) {
Jerome Gaillard0f599032019-10-10 19:29:11 +01001345 d.metadataDir = android.PathForModuleOut(ctx, "metadata")
1346 cmd.FlagWithArg("--sdk-values ", d.metadataDir.String())
Nan Zhang9c69a122018-08-22 10:22:08 -07001347 }
1348
Paul Duffin3ae29512020-04-08 18:18:03 +01001349 if stubsDir.Valid() {
1350 if Bool(d.properties.Create_doc_stubs) {
1351 cmd.FlagWithArg("--doc-stubs ", stubsDir.String())
1352 } else {
1353 cmd.FlagWithArg("--stubs ", stubsDir.String())
1354 cmd.Flag("--exclude-documentation-from-stubs")
1355 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001356 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001357}
1358
Colin Cross33961b52019-07-11 11:01:22 -07001359func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang1598a9e2018-09-04 17:14:32 -07001360 if Bool(d.properties.Annotations_enabled) {
Colin Cross33961b52019-07-11 11:01:22 -07001361 cmd.Flag("--include-annotations")
1362
Pete Gillinc382a562018-11-14 18:45:46 +00001363 validatingNullability :=
1364 strings.Contains(d.Javadoc.args, "--validate-nullability-from-merged-stubs") ||
1365 String(d.properties.Validate_nullability_from_list) != ""
Paul Duffin13a9dd62019-11-04 10:26:47 +00001366
Pete Gillina262c052018-09-14 14:25:48 +01001367 migratingNullability := String(d.properties.Previous_api) != ""
Pete Gillina262c052018-09-14 14:25:48 +01001368 if migratingNullability {
Colin Cross8a497952019-03-05 22:25:09 -08001369 previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
Colin Cross33961b52019-07-11 11:01:22 -07001370 cmd.FlagWithInput("--migrate-nullness ", previousApi)
Pete Gillina262c052018-09-14 14:25:48 +01001371 }
Colin Cross33961b52019-07-11 11:01:22 -07001372
Pete Gillinc382a562018-11-14 18:45:46 +00001373 if s := String(d.properties.Validate_nullability_from_list); s != "" {
Colin Cross33961b52019-07-11 11:01:22 -07001374 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
Pete Gillinc382a562018-11-14 18:45:46 +00001375 }
Colin Cross33961b52019-07-11 11:01:22 -07001376
Pete Gillina262c052018-09-14 14:25:48 +01001377 if validatingNullability {
Pete Gillin581d6082018-10-22 15:55:04 +01001378 d.nullabilityWarningsFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_nullability_warnings.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001379 cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile)
Pete Gillina262c052018-09-14 14:25:48 +01001380 }
Nan Zhanga40da042018-08-01 12:48:00 -07001381
1382 d.annotationsZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"_annotations.zip")
Colin Cross33961b52019-07-11 11:01:22 -07001383 cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip)
Nan Zhangf4936b02018-08-01 15:00:28 -07001384
Nan Zhang1598a9e2018-09-04 17:14:32 -07001385 if len(d.properties.Merge_annotations_dirs) == 0 {
Nan Zhang9c69a122018-08-22 10:22:08 -07001386 ctx.PropertyErrorf("merge_annotations_dirs",
Nan Zhanga40da042018-08-01 12:48:00 -07001387 "has to be non-empty if annotations was enabled!")
1388 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001389
Colin Cross33961b52019-07-11 11:01:22 -07001390 d.mergeAnnoDirFlags(ctx, cmd)
1391
1392 // TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
1393 cmd.FlagWithArg("--hide ", "HiddenTypedefConstant").
1394 FlagWithArg("--hide ", "SuperfluousPrefix").
1395 FlagWithArg("--hide ", "AnnotationExtraction")
1396 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001397}
1398
Colin Cross33961b52019-07-11 11:01:22 -07001399func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
1400 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
1401 if t, ok := m.(*ExportedDroiddocDir); ok {
1402 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
1403 } else {
1404 ctx.PropertyErrorf("merge_annotations_dirs",
1405 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1406 }
1407 })
1408}
1409
1410func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Pete Gillin77167902018-09-19 18:16:26 +01001411 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
1412 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Cross33961b52019-07-11 11:01:22 -07001413 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
Pete Gillin77167902018-09-19 18:16:26 +01001414 } else {
1415 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
1416 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1417 }
1418 })
Nan Zhanga40da042018-08-01 12:48:00 -07001419}
1420
Colin Cross33961b52019-07-11 11:01:22 -07001421func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang9c69a122018-08-22 10:22:08 -07001422 if Bool(d.properties.Api_levels_annotations_enabled) {
1423 d.apiVersionsXml = android.PathForModuleOut(ctx, "api-versions.xml")
Nan Zhang9c69a122018-08-22 10:22:08 -07001424
1425 if len(d.properties.Api_levels_annotations_dirs) == 0 {
1426 ctx.PropertyErrorf("api_levels_annotations_dirs",
1427 "has to be non-empty if api levels annotations was enabled!")
1428 }
1429
Colin Cross33961b52019-07-11 11:01:22 -07001430 cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml)
1431 cmd.FlagWithInput("--apply-api-levels ", d.apiVersionsXml)
1432 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion())
1433 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
Nan Zhang9c69a122018-08-22 10:22:08 -07001434
1435 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
1436 if t, ok := m.(*ExportedDroiddocDir); ok {
Nan Zhang9c69a122018-08-22 10:22:08 -07001437 for _, dep := range t.deps {
1438 if strings.HasSuffix(dep.String(), "android.jar") {
Colin Cross33961b52019-07-11 11:01:22 -07001439 cmd.Implicit(dep)
Nan Zhang9c69a122018-08-22 10:22:08 -07001440 }
1441 }
Colin Cross33961b52019-07-11 11:01:22 -07001442 cmd.FlagWithArg("--android-jar-pattern ", t.dir.String()+"/%/public/android.jar")
Nan Zhang9c69a122018-08-22 10:22:08 -07001443 } else {
1444 ctx.PropertyErrorf("api_levels_annotations_dirs",
1445 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
1446 }
1447 })
1448
1449 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001450}
1451
Colin Cross33961b52019-07-11 11:01:22 -07001452func (d *Droidstubs) apiToXmlFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang71bbe632018-09-17 14:32:21 -07001453 if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() {
1454 if d.apiFile.String() == "" {
1455 ctx.ModuleErrorf("API signature file has to be specified in Metalava when jdiff is enabled.")
1456 }
1457
1458 d.apiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001459 cmd.FlagWithOutput("--api-xml ", d.apiXmlFile)
Nan Zhang71bbe632018-09-17 14:32:21 -07001460
1461 if String(d.properties.Check_api.Last_released.Api_file) == "" {
1462 ctx.PropertyErrorf("check_api.last_released.api_file",
1463 "has to be non-empty if jdiff was enabled!")
1464 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001465
Colin Cross33961b52019-07-11 11:01:22 -07001466 lastReleasedApi := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
Nan Zhang71bbe632018-09-17 14:32:21 -07001467 d.lastReleasedApiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_last_released_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001468 cmd.FlagWithInput("--convert-to-jdiff ", lastReleasedApi).Output(d.lastReleasedApiXmlFile)
1469 }
1470}
Nan Zhang71bbe632018-09-17 14:32:21 -07001471
Colin Cross1e743852019-10-28 11:37:20 -07001472func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths,
Colin Cross33961b52019-07-11 11:01:22 -07001473 srcJarList android.Path, bootclasspath, classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Cross8b8bec32019-11-15 13:18:43 -08001474 // Metalava uses lots of memory, restrict the number of metalava jobs that can run in parallel.
1475 rule.HighMem()
Colin Cross33961b52019-07-11 11:01:22 -07001476 cmd := rule.Command().BuiltTool(ctx, "metalava").
1477 Flag(config.JavacVmFlags).
1478 FlagWithArg("-encoding ", "UTF-8").
Colin Cross1e743852019-10-28 11:37:20 -07001479 FlagWithArg("-source ", javaVersion.String()).
Colin Cross33961b52019-07-11 11:01:22 -07001480 FlagWithRspFileInputList("@", srcs).
1481 FlagWithInput("@", srcJarList)
1482
1483 if len(bootclasspath) > 0 {
1484 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
Nan Zhang71bbe632018-09-17 14:32:21 -07001485 }
1486
Colin Cross33961b52019-07-11 11:01:22 -07001487 if len(classpath) > 0 {
1488 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
1489 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001490
Colin Cross33961b52019-07-11 11:01:22 -07001491 if len(sourcepaths) > 0 {
1492 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
1493 } else {
1494 cmd.FlagWithArg("-sourcepath ", `""`)
1495 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001496
Colin Cross33961b52019-07-11 11:01:22 -07001497 cmd.Flag("--no-banner").
1498 Flag("--color").
1499 Flag("--quiet").
1500 Flag("--format=v2")
Nan Zhang86d2d552018-08-09 15:33:27 -07001501
Colin Cross33961b52019-07-11 11:01:22 -07001502 return cmd
Nan Zhang71bbe632018-09-17 14:32:21 -07001503}
1504
Nan Zhang1598a9e2018-09-04 17:14:32 -07001505func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga40da042018-08-01 12:48:00 -07001506 deps := d.Javadoc.collectDeps(ctx)
1507
1508 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), sdkContext(d))
Nan Zhang581fd212018-01-10 16:06:12 -08001509
Colin Cross33961b52019-07-11 11:01:22 -07001510 // Create rule for metalava
Nan Zhanga40da042018-08-01 12:48:00 -07001511
Colin Cross33961b52019-07-11 11:01:22 -07001512 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang71bbe632018-09-17 14:32:21 -07001513
Colin Cross33961b52019-07-11 11:01:22 -07001514 rule := android.NewRuleBuilder()
Nan Zhanga40da042018-08-01 12:48:00 -07001515
Paul Duffin3ae29512020-04-08 18:18:03 +01001516 generateStubs := BoolDefault(d.properties.Generate_stubs, true)
1517 var stubsDir android.OptionalPath
1518 if generateStubs {
1519 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1520 stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "stubsDir"))
1521 rule.Command().Text("rm -rf").Text(stubsDir.String())
1522 rule.Command().Text("mkdir -p").Text(stubsDir.String())
1523 }
Nan Zhanga40da042018-08-01 12:48:00 -07001524
Colin Cross33961b52019-07-11 11:01:22 -07001525 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1526
1527 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
1528 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
1529
1530 d.stubsFlags(ctx, cmd, stubsDir)
1531
1532 d.annotationsFlags(ctx, cmd)
1533 d.inclusionAnnotationsFlags(ctx, cmd)
1534 d.apiLevelsAnnotationsFlags(ctx, cmd)
1535 d.apiToXmlFlags(ctx, cmd)
Nan Zhang71bbe632018-09-17 14:32:21 -07001536
Nan Zhang1598a9e2018-09-04 17:14:32 -07001537 if strings.Contains(d.Javadoc.args, "--generate-documentation") {
1538 // Currently Metalava have the ability to invoke Javadoc in a seperate process.
1539 // Pass "-nodocs" to suppress the Javadoc invocation when Metalava receives
1540 // "--generate-documentation" arg. This is not needed when Metalava removes this feature.
1541 d.Javadoc.args = d.Javadoc.args + " -nodocs "
Nan Zhang79614d12018-04-19 18:03:39 -07001542 }
Colin Cross33961b52019-07-11 11:01:22 -07001543
1544 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1545 for _, o := range d.Javadoc.properties.Out {
1546 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1547 }
1548
Paul Duffin3ae29512020-04-08 18:18:03 +01001549 if generateStubs {
1550 rule.Command().
1551 BuiltTool(ctx, "soong_zip").
1552 Flag("-write_if_changed").
1553 Flag("-jar").
1554 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
1555 FlagWithArg("-C ", stubsDir.String()).
1556 FlagWithArg("-D ", stubsDir.String())
1557 }
Jerome Gaillard0f599032019-10-10 19:29:11 +01001558
1559 if Bool(d.properties.Write_sdk_values) {
1560 d.metadataZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-metadata.zip")
1561 rule.Command().
1562 BuiltTool(ctx, "soong_zip").
1563 Flag("-write_if_changed").
1564 Flag("-d").
1565 FlagWithOutput("-o ", d.metadataZip).
1566 FlagWithArg("-C ", d.metadataDir.String()).
1567 FlagWithArg("-D ", d.metadataDir.String())
1568 }
1569
Colin Cross33961b52019-07-11 11:01:22 -07001570 rule.Restat()
1571
1572 zipSyncCleanupCmd(rule, srcJarDir)
1573
1574 rule.Build(pctx, ctx, "metalava", "metalava")
1575
1576 // Create rule for apicheck
Nan Zhang61819ce2018-05-04 18:49:16 -07001577
Adrian Roos075eedc2019-10-10 12:07:03 +02001578 if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) && !ctx.Config().IsPdkBuild() {
1579 rule := android.NewRuleBuilder()
1580 rule.Command().Text("( true")
1581
1582 srcJarDir := android.PathForModuleOut(ctx, "api_lint", "srcjars")
1583 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1584
1585 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
1586 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
1587
Adrian Rooscab4a2c2019-10-14 16:32:41 +02001588 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1589
Adrian Roos075eedc2019-10-10 12:07:03 +02001590 newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since)
1591 if newSince.Valid() {
1592 cmd.FlagWithInput("--api-lint ", newSince.Path())
1593 } else {
1594 cmd.Flag("--api-lint")
1595 }
Adrian Roos3b8f1cd2019-11-01 13:42:39 +01001596 d.apiLintReport = android.PathForModuleOut(ctx, "api_lint_report.txt")
1597 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport)
Adrian Roos075eedc2019-10-10 12:07:03 +02001598
1599 d.inclusionAnnotationsFlags(ctx, cmd)
1600 d.mergeAnnoDirFlags(ctx, cmd)
1601
1602 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
1603 updatedBaselineOutput := android.PathForModuleOut(ctx, "api_lint_baseline.txt")
1604 d.apiLintTimestamp = android.PathForModuleOut(ctx, "api_lint.timestamp")
1605
1606 if baselineFile.Valid() {
1607 cmd.FlagWithInput("--baseline ", baselineFile.Path())
1608 cmd.FlagWithOutput("--update-baseline ", updatedBaselineOutput)
1609 }
1610
1611 zipSyncCleanupCmd(rule, srcJarDir)
1612
1613 msg := fmt.Sprintf(`\n******************************\n`+
1614 `Your API changes are triggering API Lint warnings or errors.\n\n`+
1615 `To make these errors go away, you have two choices:\n`+
1616 ` 1. You can suppress the errors with @SuppressLint(\"<id>\").\n\n`+
1617 ` 2. You can update the baseline by executing the following command:\n`+
1618 ` cp \"$PWD/%s\" \"$PWD/%s\"\n\n`+
1619 `******************************\n`, updatedBaselineOutput, baselineFile.Path())
1620 rule.Command().
1621 Text("touch").Output(d.apiLintTimestamp).
1622 Text(") || (").
1623 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1624 Text("; exit 38").
1625 Text(")")
1626
1627 rule.Build(pctx, ctx, "metalavaApiLint", "metalava API lint")
1628
1629 }
1630
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001631 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001632 !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001633
1634 if len(d.Javadoc.properties.Out) > 0 {
1635 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1636 }
1637
1638 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1639 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Adrian Roos14f75a92019-08-12 17:54:09 +02001640 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
1641 updatedBaselineOutput := android.PathForModuleOut(ctx, "current_baseline.txt")
Nan Zhang61819ce2018-05-04 18:49:16 -07001642
Nan Zhang2760dfc2018-08-24 17:32:54 +00001643 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Nan Zhang2760dfc2018-08-24 17:32:54 +00001644
Colin Cross33961b52019-07-11 11:01:22 -07001645 rule := android.NewRuleBuilder()
1646
1647 rule.Command().Text("( true")
1648
1649 srcJarDir := android.PathForModuleOut(ctx, "current-apicheck", "srcjars")
1650 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1651
1652 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
1653 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
1654
1655 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles).
1656 FlagWithInput("--check-compatibility:api:current ", apiFile).
1657 FlagWithInput("--check-compatibility:removed:current ", removedApiFile)
1658
1659 d.inclusionAnnotationsFlags(ctx, cmd)
1660 d.mergeAnnoDirFlags(ctx, cmd)
1661
Adrian Roos14f75a92019-08-12 17:54:09 +02001662 if baselineFile.Valid() {
1663 cmd.FlagWithInput("--baseline ", baselineFile.Path())
1664 cmd.FlagWithOutput("--update-baseline ", updatedBaselineOutput)
1665 }
1666
Colin Cross33961b52019-07-11 11:01:22 -07001667 zipSyncCleanupCmd(rule, srcJarDir)
1668
1669 msg := fmt.Sprintf(`\n******************************\n`+
1670 `You have tried to change the API from what has been previously approved.\n\n`+
1671 `To make these errors go away, you have two choices:\n`+
1672 ` 1. You can add '@hide' javadoc comments to the methods, etc. listed in the\n`+
1673 ` errors above.\n\n`+
1674 ` 2. You can update current.txt by executing the following command:\n`+
1675 ` make %s-update-current-api\n\n`+
1676 ` To submit the revised current.txt to the main Android repository,\n`+
1677 ` you will need approval.\n`+
1678 `******************************\n`, ctx.ModuleName())
1679
1680 rule.Command().
1681 Text("touch").Output(d.checkCurrentApiTimestamp).
1682 Text(") || (").
1683 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1684 Text("; exit 38").
1685 Text(")")
1686
1687 rule.Build(pctx, ctx, "metalavaCurrentApiCheck", "metalava check current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001688
1689 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001690
1691 // update API rule
1692 rule = android.NewRuleBuilder()
1693
1694 rule.Command().Text("( true")
1695
1696 rule.Command().
1697 Text("cp").Flag("-f").
1698 Input(d.apiFile).Flag(apiFile.String())
1699
1700 rule.Command().
1701 Text("cp").Flag("-f").
1702 Input(d.removedApiFile).Flag(removedApiFile.String())
1703
1704 msg = "failed to update public API"
1705
1706 rule.Command().
1707 Text("touch").Output(d.updateCurrentApiTimestamp).
1708 Text(") || (").
1709 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1710 Text("; exit 38").
1711 Text(")")
1712
1713 rule.Build(pctx, ctx, "metalavaCurrentApiUpdate", "update current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001714 }
Nan Zhanga40da042018-08-01 12:48:00 -07001715
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001716 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001717 !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001718
1719 if len(d.Javadoc.properties.Out) > 0 {
1720 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1721 }
1722
1723 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1724 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
Adrian Roos14f75a92019-08-12 17:54:09 +02001725 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
1726 updatedBaselineOutput := android.PathForModuleOut(ctx, "last_released_baseline.txt")
Nan Zhang61819ce2018-05-04 18:49:16 -07001727
Nan Zhang2760dfc2018-08-24 17:32:54 +00001728 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
Nan Zhang2760dfc2018-08-24 17:32:54 +00001729
Colin Cross33961b52019-07-11 11:01:22 -07001730 rule := android.NewRuleBuilder()
1731
1732 rule.Command().Text("( true")
1733
1734 srcJarDir := android.PathForModuleOut(ctx, "last-apicheck", "srcjars")
1735 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1736
1737 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
1738 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
1739
1740 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles).
1741 FlagWithInput("--check-compatibility:api:released ", apiFile)
1742
1743 d.inclusionAnnotationsFlags(ctx, cmd)
1744
1745 cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile)
1746
1747 d.mergeAnnoDirFlags(ctx, cmd)
1748
Adrian Roos14f75a92019-08-12 17:54:09 +02001749 if baselineFile.Valid() {
1750 cmd.FlagWithInput("--baseline ", baselineFile.Path())
1751 cmd.FlagWithOutput("--update-baseline ", updatedBaselineOutput)
1752 }
1753
Colin Cross33961b52019-07-11 11:01:22 -07001754 zipSyncCleanupCmd(rule, srcJarDir)
1755
1756 msg := `\n******************************\n` +
1757 `You have tried to change the API from what has been previously released in\n` +
1758 `an SDK. Please fix the errors listed above.\n` +
1759 `******************************\n`
1760 rule.Command().
1761 Text("touch").Output(d.checkLastReleasedApiTimestamp).
1762 Text(") || (").
1763 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1764 Text("; exit 38").
1765 Text(")")
1766
1767 rule.Build(pctx, ctx, "metalavaLastApiCheck", "metalava check last API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001768 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001769
Pete Gillin581d6082018-10-22 15:55:04 +01001770 if String(d.properties.Check_nullability_warnings) != "" {
1771 if d.nullabilityWarningsFile == nil {
1772 ctx.PropertyErrorf("check_nullability_warnings",
1773 "Cannot specify check_nullability_warnings unless validating nullability")
1774 }
Colin Cross33961b52019-07-11 11:01:22 -07001775
1776 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
1777
Pete Gillin581d6082018-10-22 15:55:04 +01001778 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "check_nullability_warnings.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001779
Pete Gillin581d6082018-10-22 15:55:04 +01001780 msg := fmt.Sprintf(`\n******************************\n`+
1781 `The warnings encountered during nullability annotation validation did\n`+
1782 `not match the checked in file of expected warnings. The diffs are shown\n`+
1783 `above. You have two options:\n`+
1784 ` 1. Resolve the differences by editing the nullability annotations.\n`+
1785 ` 2. Update the file of expected warnings by running:\n`+
1786 ` cp %s %s\n`+
1787 ` and submitting the updated file as part of your change.`,
1788 d.nullabilityWarningsFile, checkNullabilityWarnings)
Colin Cross33961b52019-07-11 11:01:22 -07001789
1790 rule := android.NewRuleBuilder()
1791
1792 rule.Command().
1793 Text("(").
1794 Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile).
1795 Text("&&").
1796 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
1797 Text(") || (").
1798 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1799 Text("; exit 38").
1800 Text(")")
1801
1802 rule.Build(pctx, ctx, "nullabilityWarningsCheck", "nullability warnings check")
Pete Gillin581d6082018-10-22 15:55:04 +01001803 }
1804
Nan Zhang71bbe632018-09-17 14:32:21 -07001805 if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001806 if len(d.Javadoc.properties.Out) > 0 {
1807 ctx.PropertyErrorf("out", "out property may not be combined with jdiff")
1808 }
1809
1810 outDir := android.PathForModuleOut(ctx, "jdiff-out")
1811 srcJarDir := android.PathForModuleOut(ctx, "jdiff-srcjars")
1812 stubsDir := android.PathForModuleOut(ctx, "jdiff-stubsDir")
1813
1814 rule := android.NewRuleBuilder()
Nan Zhang71bbe632018-09-17 14:32:21 -07001815
Nan Zhang86b06202018-09-21 17:09:21 -07001816 // Please sync with android-api-council@ before making any changes for the name of jdiffDocZip below
1817 // since there's cron job downstream that fetch this .zip file periodically.
1818 // See b/116221385 for reference.
Nan Zhang71bbe632018-09-17 14:32:21 -07001819 d.jdiffDocZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-docs.zip")
1820 d.jdiffStubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-stubs.srcjar")
1821
Nan Zhang71bbe632018-09-17 14:32:21 -07001822 jdiff := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jdiff.jar")
Nan Zhang71bbe632018-09-17 14:32:21 -07001823
Colin Cross33961b52019-07-11 11:01:22 -07001824 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
1825 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang71bbe632018-09-17 14:32:21 -07001826
Colin Cross33961b52019-07-11 11:01:22 -07001827 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1828
Colin Crossdaa4c672019-07-15 22:53:46 -07001829 cmd := javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -07001830 deps.bootClasspath, deps.classpath, d.sourcepaths)
1831
1832 cmd.Flag("-J-Xmx1600m").
Colin Cross33961b52019-07-11 11:01:22 -07001833 Flag("-XDignore.symbol.file").
1834 FlagWithArg("-doclet ", "jdiff.JDiff").
1835 FlagWithInput("-docletpath ", jdiff).
1836 Flag("-quiet").
1837 FlagWithArg("-newapi ", strings.TrimSuffix(d.apiXmlFile.Base(), d.apiXmlFile.Ext())).
1838 FlagWithArg("-newapidir ", filepath.Dir(d.apiXmlFile.String())).
1839 Implicit(d.apiXmlFile).
1840 FlagWithArg("-oldapi ", strings.TrimSuffix(d.lastReleasedApiXmlFile.Base(), d.lastReleasedApiXmlFile.Ext())).
1841 FlagWithArg("-oldapidir ", filepath.Dir(d.lastReleasedApiXmlFile.String())).
1842 Implicit(d.lastReleasedApiXmlFile)
1843
Colin Cross33961b52019-07-11 11:01:22 -07001844 rule.Command().
1845 BuiltTool(ctx, "soong_zip").
1846 Flag("-write_if_changed").
1847 Flag("-d").
1848 FlagWithOutput("-o ", d.jdiffDocZip).
1849 FlagWithArg("-C ", outDir.String()).
1850 FlagWithArg("-D ", outDir.String())
1851
1852 rule.Command().
1853 BuiltTool(ctx, "soong_zip").
1854 Flag("-write_if_changed").
1855 Flag("-jar").
1856 FlagWithOutput("-o ", d.jdiffStubsSrcJar).
1857 FlagWithArg("-C ", stubsDir.String()).
1858 FlagWithArg("-D ", stubsDir.String())
1859
1860 rule.Restat()
1861
1862 zipSyncCleanupCmd(rule, srcJarDir)
1863
1864 rule.Build(pctx, ctx, "jdiff", "jdiff")
Nan Zhang71bbe632018-09-17 14:32:21 -07001865 }
Nan Zhang581fd212018-01-10 16:06:12 -08001866}
Dan Willemsencc090972018-02-26 14:33:31 -08001867
Nan Zhanga40da042018-08-01 12:48:00 -07001868//
Nan Zhangf4936b02018-08-01 15:00:28 -07001869// Exported Droiddoc Directory
Nan Zhanga40da042018-08-01 12:48:00 -07001870//
Dan Willemsencc090972018-02-26 14:33:31 -08001871var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
Nan Zhangf4936b02018-08-01 15:00:28 -07001872var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
Pete Gillin77167902018-09-19 18:16:26 +01001873var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
Nan Zhang9c69a122018-08-22 10:22:08 -07001874var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
Dan Willemsencc090972018-02-26 14:33:31 -08001875
Nan Zhangf4936b02018-08-01 15:00:28 -07001876type ExportedDroiddocDirProperties struct {
1877 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -08001878 Path *string
1879}
1880
Nan Zhangf4936b02018-08-01 15:00:28 -07001881type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -08001882 android.ModuleBase
1883
Nan Zhangf4936b02018-08-01 15:00:28 -07001884 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -08001885
1886 deps android.Paths
1887 dir android.Path
1888}
1889
Colin Crossa3002fc2019-07-08 16:48:04 -07001890// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -07001891func ExportedDroiddocDirFactory() android.Module {
1892 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -08001893 module.AddProperties(&module.properties)
1894 android.InitAndroidModule(module)
1895 return module
1896}
1897
Nan Zhangf4936b02018-08-01 15:00:28 -07001898func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -08001899
Nan Zhangf4936b02018-08-01 15:00:28 -07001900func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -08001901 path := String(d.properties.Path)
1902 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -08001903 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Dan Willemsencc090972018-02-26 14:33:31 -08001904}
Nan Zhangb2b33de2018-02-23 11:18:47 -08001905
1906//
1907// Defaults
1908//
1909type DocDefaults struct {
1910 android.ModuleBase
1911 android.DefaultsModuleBase
1912}
1913
Nan Zhangb2b33de2018-02-23 11:18:47 -08001914func DocDefaultsFactory() android.Module {
1915 module := &DocDefaults{}
1916
1917 module.AddProperties(
1918 &JavadocProperties{},
1919 &DroiddocProperties{},
1920 )
1921
1922 android.InitDefaultsModule(module)
1923
1924 return module
1925}
Nan Zhang1598a9e2018-09-04 17:14:32 -07001926
1927func StubsDefaultsFactory() android.Module {
1928 module := &DocDefaults{}
1929
1930 module.AddProperties(
1931 &JavadocProperties{},
1932 &DroidstubsProperties{},
1933 )
1934
1935 android.InitDefaultsModule(module)
1936
1937 return module
1938}
Colin Cross33961b52019-07-11 11:01:22 -07001939
1940func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
1941 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
1942
1943 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1944 rule.Command().Text("mkdir -p").Text(srcJarDir.String())
1945 srcJarList := srcJarDir.Join(ctx, "list")
1946
1947 rule.Temporary(srcJarList)
1948
1949 rule.Command().BuiltTool(ctx, "zipsync").
1950 FlagWithArg("-d ", srcJarDir.String()).
1951 FlagWithOutput("-l ", srcJarList).
1952 FlagWithArg("-f ", `"*.java"`).
1953 Inputs(srcJars)
1954
1955 return srcJarList
1956}
1957
1958func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
1959 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1960}
Paul Duffin91547182019-11-12 19:39:36 +00001961
1962var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
1963
1964type PrebuiltStubsSourcesProperties struct {
1965 Srcs []string `android:"path"`
1966}
1967
1968type PrebuiltStubsSources struct {
1969 android.ModuleBase
1970 android.DefaultableModuleBase
1971 prebuilt android.Prebuilt
1972 android.SdkBase
1973
1974 properties PrebuiltStubsSourcesProperties
1975
Paul Duffin9b478b02019-12-10 13:41:51 +00001976 // The source directories containing stubs source files.
1977 srcDirs android.Paths
Paul Duffin91547182019-11-12 19:39:36 +00001978 stubsSrcJar android.ModuleOutPath
1979}
1980
Paul Duffin9b478b02019-12-10 13:41:51 +00001981func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) {
1982 switch tag {
1983 case "":
1984 return android.Paths{p.stubsSrcJar}, nil
1985 default:
1986 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1987 }
1988}
1989
Paul Duffin91547182019-11-12 19:39:36 +00001990func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin9b478b02019-12-10 13:41:51 +00001991 p.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1992
1993 p.srcDirs = android.PathsForModuleSrc(ctx, p.properties.Srcs)
1994
1995 rule := android.NewRuleBuilder()
1996 command := rule.Command().
1997 BuiltTool(ctx, "soong_zip").
1998 Flag("-write_if_changed").
1999 Flag("-jar").
2000 FlagWithOutput("-o ", p.stubsSrcJar)
2001
2002 for _, d := range p.srcDirs {
2003 dir := d.String()
2004 command.
2005 FlagWithArg("-C ", dir).
2006 FlagWithInput("-D ", d)
2007 }
2008
2009 rule.Restat()
2010
2011 rule.Build(pctx, ctx, "zip src", "Create srcjar from prebuilt source")
Paul Duffin91547182019-11-12 19:39:36 +00002012}
2013
2014func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
2015 return &p.prebuilt
2016}
2017
2018func (p *PrebuiltStubsSources) Name() string {
2019 return p.prebuilt.Name(p.ModuleBase.Name())
2020}
2021
Paul Duffin91547182019-11-12 19:39:36 +00002022// prebuilt_stubs_sources imports a set of java source files as if they were
2023// generated by droidstubs.
2024//
2025// By default, a prebuilt_stubs_sources has a single variant that expects a
2026// set of `.java` files generated by droidstubs.
2027//
2028// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2029// for host modules.
2030//
2031// Intended only for use by sdk snapshots.
2032func PrebuiltStubsSourcesFactory() android.Module {
2033 module := &PrebuiltStubsSources{}
2034
2035 module.AddProperties(&module.properties)
2036
2037 android.InitPrebuiltModule(module, &module.properties.Srcs)
2038 android.InitSdkAwareModule(module)
2039 InitDroiddocModule(module, android.HostAndDeviceSupported)
2040 return module
2041}
2042
Paul Duffin13879572019-11-28 14:31:38 +00002043type droidStubsSdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00002044 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00002045}
2046
2047func (mt *droidStubsSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2048 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2049}
2050
2051func (mt *droidStubsSdkMemberType) IsInstance(module android.Module) bool {
2052 _, ok := module.(*Droidstubs)
2053 return ok
2054}
2055
Paul Duffin495ffb92020-03-20 13:35:40 +00002056func (mt *droidStubsSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2057 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_stubs_sources")
2058}
2059
2060func (mt *droidStubsSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2061 return &droidStubsInfoProperties{}
2062}
2063
2064type droidStubsInfoProperties struct {
2065 android.SdkMemberPropertiesBase
2066
2067 StubsSrcJar android.Path
2068}
2069
2070func (p *droidStubsInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2071 droidstubs := variant.(*Droidstubs)
2072 p.StubsSrcJar = droidstubs.stubsSrcJar
2073}
2074
2075func (p *droidStubsInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
2076 if p.StubsSrcJar != nil {
2077 builder := ctx.SnapshotBuilder()
2078
2079 snapshotRelativeDir := filepath.Join("java", ctx.Name()+"_stubs_sources")
2080
2081 builder.UnzipToSnapshot(p.StubsSrcJar, snapshotRelativeDir)
2082
2083 propertySet.AddProperty("srcs", []string{snapshotRelativeDir})
Paul Duffin13879572019-11-28 14:31:38 +00002084 }
Paul Duffin91547182019-11-12 19:39:36 +00002085}