blob: 43fb9643508b0b026a38ad7f815f228d49990038 [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"
Ramy Medhat427683c2020-04-30 03:08:37 -040027 "android/soong/remoteexec"
Nan Zhang581fd212018-01-10 16:06:12 -080028)
29
30func init() {
Paul Duffin884363e2019-12-19 10:21:09 +000031 RegisterDocsBuildComponents(android.InitRegistrationContext)
32 RegisterStubsBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000033
34 // Register sdk member type.
35 android.RegisterSdkMemberType(&droidStubsSdkMemberType{
36 SdkMemberTypeBase: android.SdkMemberTypeBase{
37 PropertyName: "stubs_sources",
Paul Duffine6029182019-12-16 17:43:48 +000038 // stubs_sources can be used with sdk to provide the source stubs for APIs provided by
39 // the APEX.
40 SupportsSdk: true,
Paul Duffin255f18e2019-12-13 11:22:16 +000041 },
42 })
Nan Zhang581fd212018-01-10 16:06:12 -080043}
44
Paul Duffin884363e2019-12-19 10:21:09 +000045func RegisterDocsBuildComponents(ctx android.RegistrationContext) {
46 ctx.RegisterModuleType("doc_defaults", DocDefaultsFactory)
47
48 ctx.RegisterModuleType("droiddoc", DroiddocFactory)
49 ctx.RegisterModuleType("droiddoc_host", DroiddocHostFactory)
50 ctx.RegisterModuleType("droiddoc_exported_dir", ExportedDroiddocDirFactory)
51 ctx.RegisterModuleType("javadoc", JavadocFactory)
52 ctx.RegisterModuleType("javadoc_host", JavadocHostFactory)
53}
54
55func RegisterStubsBuildComponents(ctx android.RegistrationContext) {
56 ctx.RegisterModuleType("stubs_defaults", StubsDefaultsFactory)
57
58 ctx.RegisterModuleType("droidstubs", DroidstubsFactory)
59 ctx.RegisterModuleType("droidstubs_host", DroidstubsHostFactory)
60
61 ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory)
62}
63
Colin Crossa1ce2a02018-06-20 15:19:39 -070064var (
65 srcsLibTag = dependencyTag{name: "sources from javalib"}
66)
67
Nan Zhang581fd212018-01-10 16:06:12 -080068type JavadocProperties struct {
69 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
70 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080071 Srcs []string `android:"path,arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080072
73 // list of directories rooted at the Android.bp file that will
74 // be added to the search paths for finding source files when passing package names.
Nan Zhangb2b33de2018-02-23 11:18:47 -080075 Local_sourcepaths []string
Nan Zhang581fd212018-01-10 16:06:12 -080076
77 // list of source files that should not be used to build the Java module.
78 // This is most useful in the arch/multilib variants to remove non-common files
79 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -080080 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080081
Jiyong Parkc6ddccf2019-09-13 20:56:14 +090082 // list of package names that should actually be used. If this property is left unspecified,
83 // all the sources from the srcs property is used.
84 Filter_packages []string
85
Nan Zhangb2b33de2018-02-23 11:18:47 -080086 // list of java libraries that will be in the classpath.
Nan Zhang581fd212018-01-10 16:06:12 -080087 Libs []string `android:"arch_variant"`
88
89 // If set to false, don't allow this module(-docs.zip) to be exported. Defaults to true.
Nan Zhangb2b33de2018-02-23 11:18:47 -080090 Installable *bool
Nan Zhang581fd212018-01-10 16:06:12 -080091
Paul Duffine25c6442019-10-11 13:50:28 +010092 // if not blank, set to the version of the sdk to compile against.
93 // Defaults to compiling against the current platform.
Nan Zhang581fd212018-01-10 16:06:12 -080094 Sdk_version *string `android:"arch_variant"`
Jiyong Park1e440682018-05-23 18:42:04 +090095
Paul Duffine25c6442019-10-11 13:50:28 +010096 // When targeting 1.9 and above, override the modules to use with --system,
97 // otherwise provides defaults libraries to add to the bootclasspath.
98 // Defaults to "none"
99 System_modules *string
100
Jiyong Park1e440682018-05-23 18:42:04 +0900101 Aidl struct {
102 // Top level directories to pass to aidl tool
103 Include_dirs []string
104
105 // Directories rooted at the Android.bp file to pass to aidl tool
106 Local_include_dirs []string
107 }
Nan Zhang357466b2018-04-17 17:38:36 -0700108
109 // If not blank, set the java version passed to javadoc as -source
110 Java_version *string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700111
112 // local files that are used within user customized droiddoc options.
Colin Cross27b922f2019-03-04 22:35:41 -0800113 Arg_files []string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -0700114
115 // user customized droiddoc args.
116 // Available variables for substitution:
117 //
118 // $(location <label>): the path to the arg_files with name <label>
Colin Crosse4a05842019-05-28 10:17:14 -0700119 // $$: a literal $
Nan Zhang1598a9e2018-09-04 17:14:32 -0700120 Args *string
121
122 // names of the output files used in args that will be generated
123 Out []string
Ramy Medhat2f99eec2020-06-13 17:38:27 -0400124
125 // If set, metalava is sandboxed to only read files explicitly specified on the command
126 // line. Defaults to false.
127 Sandbox *bool
Nan Zhang581fd212018-01-10 16:06:12 -0800128}
129
Nan Zhang61819ce2018-05-04 18:49:16 -0700130type ApiToCheck struct {
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900131 // path to the API txt file that the new API extracted from source code is checked
132 // against. The path can be local to the module or from other module (via :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800133 Api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700134
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900135 // path to the API txt file that the new @removed API extractd from source code is
136 // checked against. The path can be local to the module or from other module (via
137 // :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800138 Removed_api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700139
Adrian Roos14f75a92019-08-12 17:54:09 +0200140 // If not blank, path to the baseline txt file for approved API check violations.
141 Baseline_file *string `android:"path"`
142
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900143 // Arguments to the apicheck tool.
Nan Zhang61819ce2018-05-04 18:49:16 -0700144 Args *string
145}
146
Nan Zhang581fd212018-01-10 16:06:12 -0800147type DroiddocProperties struct {
148 // directory relative to top of the source tree that contains doc templates files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800149 Custom_template *string
Nan Zhang581fd212018-01-10 16:06:12 -0800150
Nan Zhanga40da042018-08-01 12:48:00 -0700151 // directories under current module source which contains html/jd files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800152 Html_dirs []string
Nan Zhang581fd212018-01-10 16:06:12 -0800153
154 // set a value in the Clearsilver hdf namespace.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800155 Hdf []string
Nan Zhang581fd212018-01-10 16:06:12 -0800156
157 // proofread file contains all of the text content of the javadocs concatenated into one file,
158 // suitable for spell-checking and other goodness.
Colin Crossab054432019-07-15 16:13:59 -0700159 Proofread_file *string
Nan Zhang581fd212018-01-10 16:06:12 -0800160
161 // a todo file lists the program elements that are missing documentation.
162 // At some point, this might be improved to show more warnings.
Colin Cross27b922f2019-03-04 22:35:41 -0800163 Todo_file *string `android:"path"`
Nan Zhangb2b33de2018-02-23 11:18:47 -0800164
165 // directory under current module source that provide additional resources (images).
166 Resourcesdir *string
167
168 // resources output directory under out/soong/.intermediates.
169 Resourcesoutdir *string
Nan Zhang581fd212018-01-10 16:06:12 -0800170
Nan Zhange2ba5d42018-07-11 15:16:55 -0700171 // if set to true, collect the values used by the Dev tools and
172 // write them in files packaged with the SDK. Defaults to false.
173 Write_sdk_values *bool
174
175 // index.html under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800176 Static_doc_index_redirect *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700177
178 // source.properties under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800179 Static_doc_properties *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700180
Nan Zhang581fd212018-01-10 16:06:12 -0800181 // a list of files under current module source dir which contains known tags in Java sources.
182 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -0800183 Knowntags []string `android:"path"`
Nan Zhang28c68b92018-03-13 16:17:01 -0700184
Nan Zhang28c68b92018-03-13 16:17:01 -0700185 // the generated public API filename by Doclava.
186 Api_filename *string
187
Nan Zhang28c68b92018-03-13 16:17:01 -0700188 // the generated removed API filename by Doclava.
189 Removed_api_filename *string
190
David Brazdilaac0c3c2018-04-24 16:23:29 +0100191 // the generated removed Dex API filename by Doclava.
192 Removed_dex_api_filename *string
193
Nan Zhang853f4202018-04-12 16:55:56 -0700194 // if set to false, don't allow droiddoc to generate stubs source files. Defaults to true.
195 Create_stubs *bool
Nan Zhang61819ce2018-05-04 18:49:16 -0700196
197 Check_api struct {
198 Last_released ApiToCheck
199
200 Current ApiToCheck
Inseob Kim38449af2019-02-28 14:24:05 +0900201
202 // do not perform API check against Last_released, in the case that both two specified API
203 // files by Last_released are modules which don't exist.
204 Ignore_missing_latest_api *bool `blueprint:"mutated"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700205 }
Nan Zhang79614d12018-04-19 18:03:39 -0700206
Nan Zhang1598a9e2018-09-04 17:14:32 -0700207 // if set to true, generate docs through Dokka instead of Doclava.
208 Dokka_enabled *bool
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000209
210 // Compat config XML. Generates compat change documentation if set.
211 Compat_config *string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -0700212}
213
214type DroidstubsProperties struct {
Nan Zhang199645c2018-09-19 12:40:06 -0700215 // the generated public API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700216 Api_filename *string
217
Nan Zhang199645c2018-09-19 12:40:06 -0700218 // the generated removed API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700219 Removed_api_filename *string
220
Nan Zhang199645c2018-09-19 12:40:06 -0700221 // the generated removed Dex API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700222 Removed_dex_api_filename *string
223
Nan Zhang1598a9e2018-09-04 17:14:32 -0700224 Check_api struct {
225 Last_released ApiToCheck
226
227 Current ApiToCheck
Inseob Kim38449af2019-02-28 14:24:05 +0900228
Paul Duffin160fe412020-05-10 19:32:20 +0100229 // The java_sdk_library module generates references to modules (i.e. filegroups)
230 // from which information about the latest API version can be obtained. As those
231 // modules may not exist (e.g. because a previous version has not been released) it
232 // sets ignore_missing_latest_api=true on the droidstubs modules it creates so
233 // that droidstubs can ignore those references if the modules do not yet exist.
234 //
235 // If true then this will ignore module references for modules that do not exist
236 // in properties that supply the previous version of the API.
237 //
238 // There are two sets of those:
239 // * Api_file, Removed_api_file in check_api.last_released
240 // * New_since in check_api.api_lint.new_since
241 //
242 // The first two must be set as a pair, so either they should both exist or neither
243 // should exist - in which case when this property is true they are ignored. If one
244 // exists and the other does not then it is an error.
Inseob Kim38449af2019-02-28 14:24:05 +0900245 Ignore_missing_latest_api *bool `blueprint:"mutated"`
Adrian Roos075eedc2019-10-10 12:07:03 +0200246
247 Api_lint struct {
248 Enabled *bool
249
250 // If set, performs api_lint on any new APIs not found in the given signature file
251 New_since *string `android:"path"`
252
253 // If not blank, path to the baseline txt file for approved API lint violations.
254 Baseline_file *string `android:"path"`
255 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700256 }
Nan Zhang79614d12018-04-19 18:03:39 -0700257
258 // user can specify the version of previous released API file in order to do compatibility check.
Colin Cross27b922f2019-03-04 22:35:41 -0800259 Previous_api *string `android:"path"`
Nan Zhang79614d12018-04-19 18:03:39 -0700260
261 // is set to true, Metalava will allow framework SDK to contain annotations.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700262 Annotations_enabled *bool
Nan Zhang79614d12018-04-19 18:03:39 -0700263
Pete Gillin77167902018-09-19 18:16:26 +0100264 // 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 -0700265 Merge_annotations_dirs []string
Nan Zhang86d2d552018-08-09 15:33:27 -0700266
Pete Gillin77167902018-09-19 18:16:26 +0100267 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
268 Merge_inclusion_annotations_dirs []string
269
Pete Gillinc382a562018-11-14 18:45:46 +0000270 // a file containing a list of classes to do nullability validation for.
271 Validate_nullability_from_list *string
272
Pete Gillin581d6082018-10-22 15:55:04 +0100273 // a file containing expected warnings produced by validation of nullability annotations.
274 Check_nullability_warnings *string
275
Nan Zhang1598a9e2018-09-04 17:14:32 -0700276 // if set to true, allow Metalava to generate doc_stubs source files. Defaults to false.
277 Create_doc_stubs *bool
Nan Zhang9c69a122018-08-22 10:22:08 -0700278
Paul Duffin3ae29512020-04-08 18:18:03 +0100279 // if set to false then do not write out stubs. Defaults to true.
280 //
281 // TODO(b/146727827): Remove capability when we do not need to generate stubs and API separately.
282 Generate_stubs *bool
283
Nan Zhang9c69a122018-08-22 10:22:08 -0700284 // is set to true, Metalava will allow framework SDK to contain API levels annotations.
285 Api_levels_annotations_enabled *bool
286
287 // the dirs which Metalava extracts API levels annotations from.
288 Api_levels_annotations_dirs []string
289
290 // if set to true, collect the values used by the Dev tools and
291 // write them in files packaged with the SDK. Defaults to false.
292 Write_sdk_values *bool
Nan Zhang71bbe632018-09-17 14:32:21 -0700293
294 // If set to true, .xml based public API file will be also generated, and
295 // JDiff tool will be invoked to genreate javadoc files. Defaults to false.
296 Jdiff_enabled *bool
Nan Zhang581fd212018-01-10 16:06:12 -0800297}
298
Nan Zhanga40da042018-08-01 12:48:00 -0700299//
300// Common flags passed down to build rule
301//
302type droiddocBuilderFlags struct {
Nan Zhang86d2d552018-08-09 15:33:27 -0700303 bootClasspathArgs string
304 classpathArgs string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700305 sourcepathArgs string
Nan Zhang86d2d552018-08-09 15:33:27 -0700306 dokkaClasspathArgs string
307 aidlFlags string
Colin Cross3047fa22019-04-18 10:56:44 -0700308 aidlDeps android.Paths
Nan Zhanga40da042018-08-01 12:48:00 -0700309
Nan Zhanga40da042018-08-01 12:48:00 -0700310 doclavaStubsFlags string
Nan Zhang86d2d552018-08-09 15:33:27 -0700311 doclavaDocsFlags string
Nan Zhanga40da042018-08-01 12:48:00 -0700312 postDoclavaCmds string
Nan Zhanga40da042018-08-01 12:48:00 -0700313}
314
315func InitDroiddocModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
316 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
317 android.InitDefaultableModule(module)
318}
319
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200320func apiCheckEnabled(ctx android.ModuleContext, apiToCheck ApiToCheck, apiVersionTag string) bool {
321 if ctx.Config().IsEnvTrue("WITHOUT_CHECK_API") {
322 return false
323 } else if String(apiToCheck.Api_file) != "" && String(apiToCheck.Removed_api_file) != "" {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700324 return true
325 } else if String(apiToCheck.Api_file) != "" {
326 panic("for " + apiVersionTag + " removed_api_file has to be non-empty!")
327 } else if String(apiToCheck.Removed_api_file) != "" {
328 panic("for " + apiVersionTag + " api_file has to be non-empty!")
329 }
330
331 return false
332}
333
Inseob Kim38449af2019-02-28 14:24:05 +0900334func ignoreMissingModules(ctx android.BottomUpMutatorContext, apiToCheck *ApiToCheck) {
335 api_file := String(apiToCheck.Api_file)
336 removed_api_file := String(apiToCheck.Removed_api_file)
337
338 api_module := android.SrcIsModule(api_file)
339 removed_api_module := android.SrcIsModule(removed_api_file)
340
341 if api_module == "" || removed_api_module == "" {
342 return
343 }
344
345 if ctx.OtherModuleExists(api_module) || ctx.OtherModuleExists(removed_api_module) {
346 return
347 }
348
349 apiToCheck.Api_file = nil
350 apiToCheck.Removed_api_file = nil
351}
352
Paul Duffin3d1248c2020-04-09 00:10:17 +0100353// Used by xsd_config
Nan Zhang1598a9e2018-09-04 17:14:32 -0700354type ApiFilePath interface {
355 ApiFilePath() android.Path
356}
357
Paul Duffin0f8faff2020-05-20 16:18:00 +0100358type ApiStubsSrcProvider interface {
359 StubsSrcJar() android.Path
360}
361
Paul Duffin3d1248c2020-04-09 00:10:17 +0100362// Provider of information about API stubs, used by java_sdk_library.
363type ApiStubsProvider interface {
364 ApiFilePath
Paul Duffin1fd005d2020-04-09 01:08:11 +0100365 RemovedApiFilePath() android.Path
Paul Duffin0f8faff2020-05-20 16:18:00 +0100366
367 ApiStubsSrcProvider
Paul Duffin3d1248c2020-04-09 00:10:17 +0100368}
369
Nan Zhanga40da042018-08-01 12:48:00 -0700370//
371// Javadoc
372//
Nan Zhang581fd212018-01-10 16:06:12 -0800373type Javadoc struct {
374 android.ModuleBase
375 android.DefaultableModuleBase
376
377 properties JavadocProperties
378
379 srcJars android.Paths
380 srcFiles android.Paths
381 sourcepaths android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700382 argFiles android.Paths
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400383 implicits android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700384
385 args string
Nan Zhang581fd212018-01-10 16:06:12 -0800386
Nan Zhangccff0f72018-03-08 17:26:16 -0800387 docZip android.WritablePath
388 stubsSrcJar android.WritablePath
Nan Zhang581fd212018-01-10 16:06:12 -0800389}
390
Colin Cross41955e82019-05-29 14:40:35 -0700391func (j *Javadoc) OutputFiles(tag string) (android.Paths, error) {
392 switch tag {
393 case "":
394 return android.Paths{j.stubsSrcJar}, nil
Colin Crosse68e5542019-08-12 13:11:40 -0700395 case ".docs.zip":
396 return android.Paths{j.docZip}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700397 default:
398 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
399 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800400}
401
Colin Crossa3002fc2019-07-08 16:48:04 -0700402// javadoc converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800403func JavadocFactory() android.Module {
404 module := &Javadoc{}
405
406 module.AddProperties(&module.properties)
407
408 InitDroiddocModule(module, android.HostAndDeviceSupported)
409 return module
410}
411
Colin Crossa3002fc2019-07-08 16:48:04 -0700412// javadoc_host converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800413func JavadocHostFactory() android.Module {
414 module := &Javadoc{}
415
416 module.AddProperties(&module.properties)
417
418 InitDroiddocModule(module, android.HostSupported)
419 return module
420}
421
Colin Cross41955e82019-05-29 14:40:35 -0700422var _ android.OutputFileProducer = (*Javadoc)(nil)
Nan Zhang581fd212018-01-10 16:06:12 -0800423
Jiyong Park6a927c42020-01-21 02:03:43 +0900424func (j *Javadoc) sdkVersion() sdkSpec {
425 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700426}
427
Paul Duffine25c6442019-10-11 13:50:28 +0100428func (j *Javadoc) systemModules() string {
429 return proptools.String(j.properties.System_modules)
430}
431
Jiyong Park6a927c42020-01-21 02:03:43 +0900432func (j *Javadoc) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700433 return j.sdkVersion()
434}
435
Jiyong Park6a927c42020-01-21 02:03:43 +0900436func (j *Javadoc) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700437 return j.sdkVersion()
438}
439
Nan Zhang581fd212018-01-10 16:06:12 -0800440func (j *Javadoc) addDeps(ctx android.BottomUpMutatorContext) {
441 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100442 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Pete Gilline3d44b22020-06-29 11:28:51 +0100443 if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700444 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100445 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700446 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Pete Gilline3d44b22020-06-29 11:28:51 +0100447 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
Nan Zhang581fd212018-01-10 16:06:12 -0800448 }
449 }
450
Colin Cross42d48b72018-08-29 14:10:52 -0700451 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800452}
453
Nan Zhanga40da042018-08-01 12:48:00 -0700454func (j *Javadoc) collectAidlFlags(ctx android.ModuleContext, deps deps) droiddocBuilderFlags {
455 var flags droiddocBuilderFlags
Jiyong Park1e440682018-05-23 18:42:04 +0900456
Colin Cross3047fa22019-04-18 10:56:44 -0700457 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Jiyong Park1e440682018-05-23 18:42:04 +0900458
459 return flags
460}
461
462func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700463 aidlIncludeDirs android.Paths) (string, android.Paths) {
Jiyong Park1e440682018-05-23 18:42:04 +0900464
465 aidlIncludes := android.PathsForModuleSrc(ctx, j.properties.Aidl.Local_include_dirs)
466 aidlIncludes = append(aidlIncludes, android.PathsForSource(ctx, j.properties.Aidl.Include_dirs)...)
467
468 var flags []string
Colin Cross3047fa22019-04-18 10:56:44 -0700469 var deps android.Paths
470
Jiyong Park1e440682018-05-23 18:42:04 +0900471 if aidlPreprocess.Valid() {
472 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700473 deps = append(deps, aidlPreprocess.Path())
Jiyong Park1e440682018-05-23 18:42:04 +0900474 } else {
475 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
476 }
477
478 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
479 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
480 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
481 flags = append(flags, "-I"+src.String())
482 }
483
Colin Cross3047fa22019-04-18 10:56:44 -0700484 return strings.Join(flags, " "), deps
Jiyong Park1e440682018-05-23 18:42:04 +0900485}
486
Jiyong Parkd90d7412019-08-20 22:49:19 +0900487// TODO: remove the duplication between this and the one in gen.go
Jiyong Park1e440682018-05-23 18:42:04 +0900488func (j *Javadoc) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Nan Zhanga40da042018-08-01 12:48:00 -0700489 flags droiddocBuilderFlags) android.Paths {
Jiyong Park1e440682018-05-23 18:42:04 +0900490
491 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Crossc0806172019-06-14 18:51:47 -0700492 var aidlSrcs android.Paths
Jiyong Park1e440682018-05-23 18:42:04 +0900493
Jiyong Park1112c4c2019-08-16 21:12:10 +0900494 aidlIncludeFlags := genAidlIncludeFlags(srcFiles)
495
Jiyong Park1e440682018-05-23 18:42:04 +0900496 for _, srcFile := range srcFiles {
497 switch srcFile.Ext() {
498 case ".aidl":
Colin Crossc0806172019-06-14 18:51:47 -0700499 aidlSrcs = append(aidlSrcs, srcFile)
Jiyong Parkd90d7412019-08-20 22:49:19 +0900500 case ".logtags":
501 javaFile := genLogtags(ctx, srcFile)
502 outSrcFiles = append(outSrcFiles, javaFile)
Jiyong Park1e440682018-05-23 18:42:04 +0900503 default:
504 outSrcFiles = append(outSrcFiles, srcFile)
505 }
506 }
507
Colin Crossc0806172019-06-14 18:51:47 -0700508 // Process all aidl files together to support sharding them into one or more rules that produce srcjars.
509 if len(aidlSrcs) > 0 {
510 srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags+aidlIncludeFlags, flags.aidlDeps)
511 outSrcFiles = append(outSrcFiles, srcJarFiles...)
512 }
513
Jiyong Park1e440682018-05-23 18:42:04 +0900514 return outSrcFiles
515}
516
Nan Zhang581fd212018-01-10 16:06:12 -0800517func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
518 var deps deps
519
Colin Cross83bb3162018-06-25 15:48:06 -0700520 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800521 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700522 ctx.AddMissingDependencies(sdkDep.bootclasspath)
523 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Nan Zhang581fd212018-01-10 16:06:12 -0800524 } else if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700525 deps.bootClasspath = append(deps.bootClasspath, sdkDep.jars...)
Anton Hansson26bf49b2020-02-08 20:26:29 +0000526 deps.aidlPreprocess = sdkDep.aidl
527 } else {
528 deps.aidlPreprocess = sdkDep.aidl
Nan Zhang581fd212018-01-10 16:06:12 -0800529 }
530
531 ctx.VisitDirectDeps(func(module android.Module) {
532 otherName := ctx.OtherModuleName(module)
533 tag := ctx.OtherModuleDependencyTag(module)
534
Colin Cross2d24c1b2018-05-23 10:59:18 -0700535 switch tag {
536 case bootClasspathTag:
537 if dep, ok := module.(Dependency); ok {
Nan Zhang581fd212018-01-10 16:06:12 -0800538 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars()...)
Paul Duffin83a2d962019-11-19 19:44:10 +0000539 } else if sm, ok := module.(SystemModulesProvider); ok {
Paul Duffine25c6442019-10-11 13:50:28 +0100540 // A system modules dependency has been added to the bootclasspath
541 // so add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +0000542 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700543 } else {
544 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
545 }
546 case libTag:
547 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800548 case SdkLibraryDependency:
Paul Duffin649dadf2020-05-26 11:42:13 +0100549 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700550 case Dependency:
Sundong Ahnba493602018-11-20 17:36:35 +0900551 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park19a7f252019-07-10 16:59:31 +0900552 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700553 case android.SourceFileProducer:
Nan Zhang581fd212018-01-10 16:06:12 -0800554 checkProducesJars(ctx, dep)
555 deps.classpath = append(deps.classpath, dep.Srcs()...)
Nan Zhang581fd212018-01-10 16:06:12 -0800556 default:
557 ctx.ModuleErrorf("depends on non-java module %q", otherName)
558 }
Colin Cross6cef4812019-10-17 14:23:50 -0700559 case java9LibTag:
560 switch dep := module.(type) {
561 case Dependency:
562 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
563 default:
564 ctx.ModuleErrorf("depends on non-java module %q", otherName)
565 }
Nan Zhang357466b2018-04-17 17:38:36 -0700566 case systemModulesTag:
567 if deps.systemModules != nil {
568 panic("Found two system module dependencies")
569 }
Paul Duffin83a2d962019-11-19 19:44:10 +0000570 sm := module.(SystemModulesProvider)
571 outputDir, outputDeps := sm.OutputDirAndDeps()
572 deps.systemModules = &systemModules{outputDir, outputDeps}
Nan Zhang581fd212018-01-10 16:06:12 -0800573 }
574 })
575 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
576 // may contain filegroup or genrule.
Colin Cross8a497952019-03-05 22:25:09 -0800577 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400578 j.implicits = append(j.implicits, srcFiles...)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900579
580 filterByPackage := func(srcs []android.Path, filterPackages []string) []android.Path {
581 if filterPackages == nil {
582 return srcs
583 }
584 filtered := []android.Path{}
585 for _, src := range srcs {
586 if src.Ext() != ".java" {
587 // Don't filter-out non-Java (=generated sources) by package names. This is not ideal,
588 // but otherwise metalava emits stub sources having references to the generated AIDL classes
589 // in filtered-out pacages (e.g. com.android.internal.*).
590 // TODO(b/141149570) We need to fix this by introducing default private constructors or
591 // fixing metalava to not emit constructors having references to unknown classes.
592 filtered = append(filtered, src)
593 continue
594 }
595 packageName := strings.ReplaceAll(filepath.Dir(src.Rel()), "/", ".")
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800596 if android.HasAnyPrefix(packageName, filterPackages) {
597 filtered = append(filtered, src)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900598 }
599 }
600 return filtered
601 }
602 srcFiles = filterByPackage(srcFiles, j.properties.Filter_packages)
603
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400604 // While metalava needs package html files, it does not need them to be explicit on the command
605 // line. More importantly, the metalava rsp file is also used by the subsequent jdiff action if
606 // jdiff_enabled=true. javadoc complains if it receives html files on the command line. The filter
607 // below excludes html files from the rsp file for both metalava and jdiff. Note that the html
608 // files are still included as implicit inputs for successful remote execution and correct
609 // incremental builds.
610 filterHtml := func(srcs []android.Path) []android.Path {
611 filtered := []android.Path{}
612 for _, src := range srcs {
613 if src.Ext() == ".html" {
614 continue
615 }
616 filtered = append(filtered, src)
617 }
618 return filtered
619 }
620 srcFiles = filterHtml(srcFiles)
621
Nan Zhanga40da042018-08-01 12:48:00 -0700622 flags := j.collectAidlFlags(ctx, deps)
Jiyong Park1e440682018-05-23 18:42:04 +0900623 srcFiles = j.genSources(ctx, srcFiles, flags)
Nan Zhang581fd212018-01-10 16:06:12 -0800624
625 // srcs may depend on some genrule output.
626 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800627 j.srcJars = append(j.srcJars, deps.srcJars...)
628
Nan Zhang581fd212018-01-10 16:06:12 -0800629 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800630 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800631
Nan Zhang9c69a122018-08-22 10:22:08 -0700632 if j.properties.Local_sourcepaths == nil && len(j.srcFiles) > 0 {
Nan Zhang581fd212018-01-10 16:06:12 -0800633 j.properties.Local_sourcepaths = append(j.properties.Local_sourcepaths, ".")
634 }
635 j.sourcepaths = android.PathsForModuleSrc(ctx, j.properties.Local_sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800636
Colin Cross8a497952019-03-05 22:25:09 -0800637 j.argFiles = android.PathsForModuleSrc(ctx, j.properties.Arg_files)
Paul Duffin99e4a502019-02-11 15:38:42 +0000638 argFilesMap := map[string]string{}
639 argFileLabels := []string{}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700640
Paul Duffin99e4a502019-02-11 15:38:42 +0000641 for _, label := range j.properties.Arg_files {
Colin Cross8a497952019-03-05 22:25:09 -0800642 var paths = android.PathsForModuleSrc(ctx, []string{label})
Paul Duffin99e4a502019-02-11 15:38:42 +0000643 if _, exists := argFilesMap[label]; !exists {
644 argFilesMap[label] = strings.Join(paths.Strings(), " ")
645 argFileLabels = append(argFileLabels, label)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700646 } else {
647 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000648 label, argFilesMap[label], paths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700649 }
650 }
651
652 var err error
Colin Cross15638152019-07-11 11:11:35 -0700653 j.args, err = android.Expand(String(j.properties.Args), func(name string) (string, error) {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700654 if strings.HasPrefix(name, "location ") {
655 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Paul Duffin99e4a502019-02-11 15:38:42 +0000656 if paths, ok := argFilesMap[label]; ok {
Colin Cross15638152019-07-11 11:11:35 -0700657 return paths, nil
Nan Zhang1598a9e2018-09-04 17:14:32 -0700658 } else {
Colin Cross15638152019-07-11 11:11:35 -0700659 return "", fmt.Errorf("unknown location label %q, expecting one of %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000660 label, strings.Join(argFileLabels, ", "))
Nan Zhang1598a9e2018-09-04 17:14:32 -0700661 }
662 } else if name == "genDir" {
Colin Cross15638152019-07-11 11:11:35 -0700663 return android.PathForModuleGen(ctx).String(), nil
Nan Zhang1598a9e2018-09-04 17:14:32 -0700664 }
Colin Cross15638152019-07-11 11:11:35 -0700665 return "", fmt.Errorf("unknown variable '$(%s)'", name)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700666 })
667
668 if err != nil {
669 ctx.PropertyErrorf("args", "%s", err.Error())
670 }
671
Nan Zhang581fd212018-01-10 16:06:12 -0800672 return deps
673}
674
675func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
676 j.addDeps(ctx)
677}
678
679func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
680 deps := j.collectDeps(ctx)
681
Colin Crossdaa4c672019-07-15 22:53:46 -0700682 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800683
Colin Crossdaa4c672019-07-15 22:53:46 -0700684 outDir := android.PathForModuleOut(ctx, "out")
685 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
686
687 j.stubsSrcJar = nil
688
689 rule := android.NewRuleBuilder()
690
691 rule.Command().Text("rm -rf").Text(outDir.String())
692 rule.Command().Text("mkdir -p").Text(outDir.String())
693
694 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, j.srcJars)
Nan Zhang357466b2018-04-17 17:38:36 -0700695
Colin Cross83bb3162018-06-25 15:48:06 -0700696 javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800697
Colin Crossdaa4c672019-07-15 22:53:46 -0700698 cmd := javadocSystemModulesCmd(ctx, rule, j.srcFiles, outDir, srcJarDir, srcJarList,
699 deps.systemModules, deps.classpath, j.sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800700
Colin Cross1e743852019-10-28 11:37:20 -0700701 cmd.FlagWithArg("-source ", javaVersion.String()).
Colin Crossdaa4c672019-07-15 22:53:46 -0700702 Flag("-J-Xmx1024m").
703 Flag("-XDignore.symbol.file").
704 Flag("-Xdoclint:none")
Nan Zhang581fd212018-01-10 16:06:12 -0800705
Colin Crossdaa4c672019-07-15 22:53:46 -0700706 rule.Command().
707 BuiltTool(ctx, "soong_zip").
708 Flag("-write_if_changed").
709 Flag("-d").
710 FlagWithOutput("-o ", j.docZip).
711 FlagWithArg("-C ", outDir.String()).
712 FlagWithArg("-D ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700713
Colin Crossdaa4c672019-07-15 22:53:46 -0700714 rule.Restat()
715
716 zipSyncCleanupCmd(rule, srcJarDir)
717
718 rule.Build(pctx, ctx, "javadoc", "javadoc")
Nan Zhang581fd212018-01-10 16:06:12 -0800719}
720
Nan Zhanga40da042018-08-01 12:48:00 -0700721//
722// Droiddoc
723//
724type Droiddoc struct {
725 Javadoc
726
727 properties DroiddocProperties
728 apiFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700729 privateApiFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700730 removedApiFile android.WritablePath
731 removedDexApiFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700732
733 checkCurrentApiTimestamp android.WritablePath
734 updateCurrentApiTimestamp android.WritablePath
735 checkLastReleasedApiTimestamp android.WritablePath
736
Nan Zhanga40da042018-08-01 12:48:00 -0700737 apiFilePath android.Path
738}
739
Colin Crossa3002fc2019-07-08 16:48:04 -0700740// droiddoc converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700741func DroiddocFactory() android.Module {
742 module := &Droiddoc{}
743
744 module.AddProperties(&module.properties,
745 &module.Javadoc.properties)
746
747 InitDroiddocModule(module, android.HostAndDeviceSupported)
748 return module
749}
750
Colin Crossa3002fc2019-07-08 16:48:04 -0700751// droiddoc_host converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700752func DroiddocHostFactory() android.Module {
753 module := &Droiddoc{}
754
755 module.AddProperties(&module.properties,
756 &module.Javadoc.properties)
757
758 InitDroiddocModule(module, android.HostSupported)
759 return module
760}
761
762func (d *Droiddoc) ApiFilePath() android.Path {
763 return d.apiFilePath
764}
765
Nan Zhang581fd212018-01-10 16:06:12 -0800766func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
767 d.Javadoc.addDeps(ctx)
768
Inseob Kim38449af2019-02-28 14:24:05 +0900769 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
770 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
771 }
772
Nan Zhang79614d12018-04-19 18:03:39 -0700773 if String(d.properties.Custom_template) != "" {
Dan Willemsencc090972018-02-26 14:33:31 -0800774 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
775 }
Nan Zhang581fd212018-01-10 16:06:12 -0800776}
777
Colin Crossab054432019-07-15 16:13:59 -0700778func (d *Droiddoc) doclavaDocsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, docletPath classpath) {
Colin Cross2a2e0db2020-02-21 16:55:46 -0800779 buildNumberFile := ctx.Config().BuildNumberFile(ctx)
Nan Zhang443fa522018-08-20 20:58:28 -0700780 // Droiddoc always gets "-source 1.8" because it doesn't support 1.9 sources. For modules with 1.9
781 // sources, droiddoc will get sources produced by metalava which will have already stripped out the
782 // 1.9 language features.
Colin Crossab054432019-07-15 16:13:59 -0700783 cmd.FlagWithArg("-source ", "1.8").
784 Flag("-J-Xmx1600m").
785 Flag("-J-XX:-OmitStackTraceInFastThrow").
786 Flag("-XDignore.symbol.file").
787 FlagWithArg("-doclet ", "com.google.doclava.Doclava").
788 FlagWithInputList("-docletpath ", docletPath.Paths(), ":").
Colin Cross2a2e0db2020-02-21 16:55:46 -0800789 FlagWithArg("-hdf page.build ", ctx.Config().BuildId()+"-$(cat "+buildNumberFile.String()+")").OrderOnly(buildNumberFile).
Elliott Hughes26bce342019-09-12 15:05:13 -0700790 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 -0700791
Nan Zhanga40da042018-08-01 12:48:00 -0700792 if String(d.properties.Custom_template) == "" {
793 // TODO: This is almost always droiddoc-templates-sdk
794 ctx.PropertyErrorf("custom_template", "must specify a template")
795 }
796
797 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
Nan Zhangf4936b02018-08-01 15:00:28 -0700798 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Crossab054432019-07-15 16:13:59 -0700799 cmd.FlagWithArg("-templatedir ", t.dir.String()).Implicits(t.deps)
Nan Zhanga40da042018-08-01 12:48:00 -0700800 } else {
Paul Duffin884363e2019-12-19 10:21:09 +0000801 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_exported_dir", ctx.OtherModuleName(m))
Nan Zhanga40da042018-08-01 12:48:00 -0700802 }
803 })
804
805 if len(d.properties.Html_dirs) > 0 {
Colin Crossab054432019-07-15 16:13:59 -0700806 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
807 cmd.FlagWithArg("-htmldir ", htmlDir.String()).
808 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[0], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700809 }
810
811 if len(d.properties.Html_dirs) > 1 {
Colin Crossab054432019-07-15 16:13:59 -0700812 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
813 cmd.FlagWithArg("-htmldir2 ", htmlDir2.String()).
814 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[1], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700815 }
816
817 if len(d.properties.Html_dirs) > 2 {
818 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
819 }
820
Colin Cross8a497952019-03-05 22:25:09 -0800821 knownTags := android.PathsForModuleSrc(ctx, d.properties.Knowntags)
Colin Crossab054432019-07-15 16:13:59 -0700822 cmd.FlagForEachInput("-knowntags ", knownTags)
Nan Zhanga40da042018-08-01 12:48:00 -0700823
Colin Crossab054432019-07-15 16:13:59 -0700824 cmd.FlagForEachArg("-hdf ", d.properties.Hdf)
Nan Zhanga40da042018-08-01 12:48:00 -0700825
826 if String(d.properties.Proofread_file) != "" {
827 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
Colin Crossab054432019-07-15 16:13:59 -0700828 cmd.FlagWithOutput("-proofread ", proofreadFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700829 }
830
831 if String(d.properties.Todo_file) != "" {
832 // tricky part:
833 // we should not compute full path for todo_file through PathForModuleOut().
834 // the non-standard doclet will get the full path relative to "-o".
Colin Crossab054432019-07-15 16:13:59 -0700835 cmd.FlagWithArg("-todo ", String(d.properties.Todo_file)).
836 ImplicitOutput(android.PathForModuleOut(ctx, String(d.properties.Todo_file)))
Nan Zhanga40da042018-08-01 12:48:00 -0700837 }
838
839 if String(d.properties.Resourcesdir) != "" {
840 // TODO: should we add files under resourcesDir to the implicits? It seems that
841 // resourcesDir is one sub dir of htmlDir
842 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
Colin Crossab054432019-07-15 16:13:59 -0700843 cmd.FlagWithArg("-resourcesdir ", resourcesDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700844 }
845
846 if String(d.properties.Resourcesoutdir) != "" {
847 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
Colin Crossab054432019-07-15 16:13:59 -0700848 cmd.FlagWithArg("-resourcesoutdir ", String(d.properties.Resourcesoutdir))
Nan Zhanga40da042018-08-01 12:48:00 -0700849 }
Nan Zhanga40da042018-08-01 12:48:00 -0700850}
851
Colin Crossab054432019-07-15 16:13:59 -0700852func (d *Droiddoc) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.WritablePath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200853 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
854 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -0700855 String(d.properties.Api_filename) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700856
Nan Zhanga40da042018-08-01 12:48:00 -0700857 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Colin Crossab054432019-07-15 16:13:59 -0700858 cmd.FlagWithOutput("-api ", d.apiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700859 d.apiFilePath = d.apiFile
860 }
861
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200862 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
863 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -0700864 String(d.properties.Removed_api_filename) != "" {
Nan Zhanga40da042018-08-01 12:48:00 -0700865 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
Colin Crossab054432019-07-15 16:13:59 -0700866 cmd.FlagWithOutput("-removedApi ", d.removedApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700867 }
868
Nan Zhanga40da042018-08-01 12:48:00 -0700869 if String(d.properties.Removed_dex_api_filename) != "" {
870 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Crossab054432019-07-15 16:13:59 -0700871 cmd.FlagWithOutput("-removedDexApi ", d.removedDexApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700872 }
873
Nan Zhanga40da042018-08-01 12:48:00 -0700874 if BoolDefault(d.properties.Create_stubs, true) {
Colin Crossab054432019-07-15 16:13:59 -0700875 cmd.FlagWithArg("-stubs ", stubsDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700876 }
877
878 if Bool(d.properties.Write_sdk_values) {
Colin Crossab054432019-07-15 16:13:59 -0700879 cmd.FlagWithArg("-sdkvalues ", android.PathForModuleOut(ctx, "out").String())
Nan Zhanga40da042018-08-01 12:48:00 -0700880 }
Nan Zhanga40da042018-08-01 12:48:00 -0700881}
882
Colin Crossab054432019-07-15 16:13:59 -0700883func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700884 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700885 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
886 rule.Command().Text("cp").
887 Input(staticDocIndexRedirect).
888 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700889 }
890
891 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700892 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
893 rule.Command().Text("cp").
894 Input(staticDocProperties).
895 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700896 }
Nan Zhanga40da042018-08-01 12:48:00 -0700897}
898
Colin Crossab054432019-07-15 16:13:59 -0700899func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700900 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700901
902 cmd := rule.Command().
903 BuiltTool(ctx, "soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
904 Flag(config.JavacVmFlags).
905 FlagWithArg("-encoding ", "UTF-8").
Colin Crossab054432019-07-15 16:13:59 -0700906 FlagWithRspFileInputList("@", srcs).
907 FlagWithInput("@", srcJarList)
908
Colin Crossab054432019-07-15 16:13:59 -0700909 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
910 // based stubs generation.
911 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
912 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
913 // the correct package name base path.
914 if len(sourcepaths) > 0 {
915 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
916 } else {
917 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
918 }
919
920 cmd.FlagWithArg("-d ", outDir.String()).
921 Flag("-quiet")
922
923 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700924}
925
Colin Crossdaa4c672019-07-15 22:53:46 -0700926func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
927 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
928 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
929
930 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
931
932 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
933 cmd.Flag(flag).Implicits(deps)
934
935 cmd.FlagWithArg("--patch-module ", "java.base=.")
936
937 if len(classpath) > 0 {
938 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
939 }
940
941 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700942}
943
Colin Crossdaa4c672019-07-15 22:53:46 -0700944func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
945 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
946 sourcepaths android.Paths) *android.RuleBuilderCommand {
947
948 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
949
950 if len(bootclasspath) == 0 && ctx.Device() {
951 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
952 // ensure java does not fall back to the default bootclasspath.
953 cmd.FlagWithArg("-bootclasspath ", `""`)
954 } else if len(bootclasspath) > 0 {
955 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
956 }
957
958 if len(classpath) > 0 {
959 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
960 }
961
962 return cmd
963}
964
Colin Crossab054432019-07-15 16:13:59 -0700965func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
966 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700967
Colin Crossab054432019-07-15 16:13:59 -0700968 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
969 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
970
971 return rule.Command().
972 BuiltTool(ctx, "dokka").
973 Flag(config.JavacVmFlags).
974 Flag(srcJarDir.String()).
975 FlagWithInputList("-classpath ", dokkaClasspath, ":").
976 FlagWithArg("-format ", "dac").
977 FlagWithArg("-dacRoot ", "/reference/kotlin").
978 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700979}
980
981func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
982 deps := d.Javadoc.collectDeps(ctx)
983
Colin Crossdaa4c672019-07-15 22:53:46 -0700984 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
985 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
986
Nan Zhang1598a9e2018-09-04 17:14:32 -0700987 jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
988 doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
989 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
990 checkApiClasspath := classpath{jsilver, doclava, android.PathForSource(ctx, java8Home, "lib/tools.jar")}
991
Colin Crossab054432019-07-15 16:13:59 -0700992 outDir := android.PathForModuleOut(ctx, "out")
993 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
994 stubsDir := android.PathForModuleOut(ctx, "stubsDir")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700995
Colin Crossab054432019-07-15 16:13:59 -0700996 rule := android.NewRuleBuilder()
Nan Zhang1598a9e2018-09-04 17:14:32 -0700997
Colin Crossab054432019-07-15 16:13:59 -0700998 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
999 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -07001000
Colin Crossab054432019-07-15 16:13:59 -07001001 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1002
1003 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -07001004 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -07001005 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001006 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -07001007 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -07001008 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001009 }
1010
Colin Crossab054432019-07-15 16:13:59 -07001011 d.stubsFlags(ctx, cmd, stubsDir)
1012
1013 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1014
Mathew Inwoodabd49ab2019-12-19 14:27:08 +00001015 if d.properties.Compat_config != nil {
1016 compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
1017 cmd.FlagWithInput("-compatconfig ", compatConfig)
1018 }
1019
Colin Crossab054432019-07-15 16:13:59 -07001020 var desc string
1021 if Bool(d.properties.Dokka_enabled) {
1022 desc = "dokka"
1023 } else {
1024 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
1025
1026 for _, o := range d.Javadoc.properties.Out {
1027 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1028 }
1029
1030 d.postDoclavaCmds(ctx, rule)
1031 desc = "doclava"
1032 }
1033
1034 rule.Command().
1035 BuiltTool(ctx, "soong_zip").
1036 Flag("-write_if_changed").
1037 Flag("-d").
1038 FlagWithOutput("-o ", d.docZip).
1039 FlagWithArg("-C ", outDir.String()).
1040 FlagWithArg("-D ", outDir.String())
1041
1042 rule.Command().
1043 BuiltTool(ctx, "soong_zip").
1044 Flag("-write_if_changed").
1045 Flag("-jar").
1046 FlagWithOutput("-o ", d.stubsSrcJar).
1047 FlagWithArg("-C ", stubsDir.String()).
1048 FlagWithArg("-D ", stubsDir.String())
1049
1050 rule.Restat()
1051
1052 zipSyncCleanupCmd(rule, srcJarDir)
1053
1054 rule.Build(pctx, ctx, "javadoc", desc)
1055
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001056 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001057 !ctx.Config().IsPdkBuild() {
Colin Crossab054432019-07-15 16:13:59 -07001058
1059 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1060 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Nan Zhang1598a9e2018-09-04 17:14:32 -07001061
1062 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001063
1064 rule := android.NewRuleBuilder()
1065
1066 rule.Command().Text("( true")
1067
1068 rule.Command().
1069 BuiltTool(ctx, "apicheck").
1070 Flag("-JXmx1024m").
1071 FlagWithInputList("-Jclasspath\\ ", checkApiClasspath.Paths(), ":").
1072 OptionalFlag(d.properties.Check_api.Current.Args).
1073 Input(apiFile).
1074 Input(d.apiFile).
1075 Input(removedApiFile).
1076 Input(d.removedApiFile)
1077
1078 msg := fmt.Sprintf(`\n******************************\n`+
1079 `You have tried to change the API from what has been previously approved.\n\n`+
1080 `To make these errors go away, you have two choices:\n`+
1081 ` 1. You can add '@hide' javadoc comments to the methods, etc. listed in the\n`+
1082 ` errors above.\n\n`+
1083 ` 2. You can update current.txt by executing the following command:\n`+
1084 ` make %s-update-current-api\n\n`+
1085 ` To submit the revised current.txt to the main Android repository,\n`+
1086 ` you will need approval.\n`+
1087 `******************************\n`, ctx.ModuleName())
1088
1089 rule.Command().
1090 Text("touch").Output(d.checkCurrentApiTimestamp).
1091 Text(") || (").
1092 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1093 Text("; exit 38").
1094 Text(")")
1095
1096 rule.Build(pctx, ctx, "doclavaCurrentApiCheck", "check current API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001097
1098 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001099
1100 // update API rule
1101 rule = android.NewRuleBuilder()
1102
1103 rule.Command().Text("( true")
1104
1105 rule.Command().
1106 Text("cp").Flag("-f").
1107 Input(d.apiFile).Flag(apiFile.String())
1108
1109 rule.Command().
1110 Text("cp").Flag("-f").
1111 Input(d.removedApiFile).Flag(removedApiFile.String())
1112
1113 msg = "failed to update public API"
1114
1115 rule.Command().
1116 Text("touch").Output(d.updateCurrentApiTimestamp).
1117 Text(") || (").
1118 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1119 Text("; exit 38").
1120 Text(")")
1121
1122 rule.Build(pctx, ctx, "doclavaCurrentApiUpdate", "update current API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001123 }
1124
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001125 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001126 !ctx.Config().IsPdkBuild() {
Colin Crossab054432019-07-15 16:13:59 -07001127
1128 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1129 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
Nan Zhang1598a9e2018-09-04 17:14:32 -07001130
1131 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001132
1133 rule := android.NewRuleBuilder()
1134
1135 rule.Command().
1136 Text("(").
1137 BuiltTool(ctx, "apicheck").
1138 Flag("-JXmx1024m").
1139 FlagWithInputList("-Jclasspath\\ ", checkApiClasspath.Paths(), ":").
1140 OptionalFlag(d.properties.Check_api.Last_released.Args).
1141 Input(apiFile).
1142 Input(d.apiFile).
1143 Input(removedApiFile).
1144 Input(d.removedApiFile)
1145
1146 msg := `\n******************************\n` +
1147 `You have tried to change the API from what has been previously released in\n` +
1148 `an SDK. Please fix the errors listed above.\n` +
1149 `******************************\n`
1150
1151 rule.Command().
1152 Text("touch").Output(d.checkLastReleasedApiTimestamp).
1153 Text(") || (").
1154 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1155 Text("; exit 38").
1156 Text(")")
1157
1158 rule.Build(pctx, ctx, "doclavaLastApiCheck", "check last API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001159 }
1160}
1161
1162//
1163// Droidstubs
1164//
1165type Droidstubs struct {
1166 Javadoc
Paul Duffin91547182019-11-12 19:39:36 +00001167 android.SdkBase
Nan Zhang1598a9e2018-09-04 17:14:32 -07001168
Pete Gillin581d6082018-10-22 15:55:04 +01001169 properties DroidstubsProperties
1170 apiFile android.WritablePath
1171 apiXmlFile android.WritablePath
1172 lastReleasedApiXmlFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001173 privateApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001174 removedApiFile android.WritablePath
1175 removedDexApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001176 nullabilityWarningsFile android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001177
1178 checkCurrentApiTimestamp android.WritablePath
1179 updateCurrentApiTimestamp android.WritablePath
1180 checkLastReleasedApiTimestamp android.WritablePath
Adrian Roos075eedc2019-10-10 12:07:03 +02001181 apiLintTimestamp android.WritablePath
Adrian Roos3b8f1cd2019-11-01 13:42:39 +01001182 apiLintReport android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001183
Pete Gillin581d6082018-10-22 15:55:04 +01001184 checkNullabilityWarningsTimestamp android.WritablePath
1185
Nan Zhang1598a9e2018-09-04 17:14:32 -07001186 annotationsZip android.WritablePath
Nan Zhang9c69a122018-08-22 10:22:08 -07001187 apiVersionsXml android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001188
1189 apiFilePath android.Path
Nan Zhang71bbe632018-09-17 14:32:21 -07001190
1191 jdiffDocZip android.WritablePath
1192 jdiffStubsSrcJar android.WritablePath
Jerome Gaillard0f599032019-10-10 19:29:11 +01001193
1194 metadataZip android.WritablePath
1195 metadataDir android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001196}
1197
Colin Crossa3002fc2019-07-08 16:48:04 -07001198// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
1199// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to
1200// a droiddoc module to generate documentation.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001201func DroidstubsFactory() android.Module {
1202 module := &Droidstubs{}
1203
1204 module.AddProperties(&module.properties,
1205 &module.Javadoc.properties)
1206
1207 InitDroiddocModule(module, android.HostAndDeviceSupported)
Paul Duffin91547182019-11-12 19:39:36 +00001208 android.InitSdkAwareModule(module)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001209 return module
1210}
1211
Colin Crossa3002fc2019-07-08 16:48:04 -07001212// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API
1213// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be
1214// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs
1215// module when symbols needed by the source files are provided by java_library_host modules.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001216func DroidstubsHostFactory() android.Module {
1217 module := &Droidstubs{}
1218
1219 module.AddProperties(&module.properties,
1220 &module.Javadoc.properties)
1221
1222 InitDroiddocModule(module, android.HostSupported)
1223 return module
1224}
1225
Colin Cross014489c2020-06-02 20:09:13 -07001226func (d *Droidstubs) OutputFiles(tag string) (android.Paths, error) {
1227 switch tag {
1228 case "":
1229 return android.Paths{d.stubsSrcJar}, nil
1230 case ".docs.zip":
1231 return android.Paths{d.docZip}, nil
1232 case ".annotations.zip":
1233 return android.Paths{d.annotationsZip}, nil
1234 case ".api_versions.xml":
1235 return android.Paths{d.apiVersionsXml}, nil
1236 default:
1237 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1238 }
1239}
1240
Nan Zhang1598a9e2018-09-04 17:14:32 -07001241func (d *Droidstubs) ApiFilePath() android.Path {
1242 return d.apiFilePath
1243}
1244
Paul Duffin1fd005d2020-04-09 01:08:11 +01001245func (d *Droidstubs) RemovedApiFilePath() android.Path {
1246 return d.removedApiFile
1247}
1248
Paul Duffin3d1248c2020-04-09 00:10:17 +01001249func (d *Droidstubs) StubsSrcJar() android.Path {
1250 return d.stubsSrcJar
1251}
1252
Nan Zhang1598a9e2018-09-04 17:14:32 -07001253func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
1254 d.Javadoc.addDeps(ctx)
1255
Paul Duffin160fe412020-05-10 19:32:20 +01001256 // If requested clear any properties that provide information about the latest version
1257 // of an API and which reference non-existent modules.
Inseob Kim38449af2019-02-28 14:24:05 +09001258 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
1259 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
Paul Duffin160fe412020-05-10 19:32:20 +01001260
1261 // If the new_since references a module, e.g. :module-latest-api and the module
1262 // does not exist then clear it.
1263 newSinceSrc := d.properties.Check_api.Api_lint.New_since
1264 newSinceSrcModule := android.SrcIsModule(proptools.String(newSinceSrc))
1265 if newSinceSrcModule != "" && !ctx.OtherModuleExists(newSinceSrcModule) {
1266 d.properties.Check_api.Api_lint.New_since = nil
1267 }
Inseob Kim38449af2019-02-28 14:24:05 +09001268 }
1269
Nan Zhang1598a9e2018-09-04 17:14:32 -07001270 if len(d.properties.Merge_annotations_dirs) != 0 {
1271 for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
1272 ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
1273 }
1274 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001275
Pete Gillin77167902018-09-19 18:16:26 +01001276 if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
1277 for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
1278 ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
1279 }
1280 }
1281
Nan Zhang9c69a122018-08-22 10:22:08 -07001282 if len(d.properties.Api_levels_annotations_dirs) != 0 {
1283 for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
1284 ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
1285 }
1286 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001287}
1288
Paul Duffin3ae29512020-04-08 18:18:03 +01001289func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001290 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1291 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001292 String(d.properties.Api_filename) != "" {
1293 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001294 cmd.FlagWithOutput("--api ", d.apiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001295 d.apiFilePath = d.apiFile
1296 }
1297
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001298 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1299 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001300 String(d.properties.Removed_api_filename) != "" {
1301 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001302 cmd.FlagWithOutput("--removed-api ", d.removedApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001303 }
1304
Nan Zhang1598a9e2018-09-04 17:14:32 -07001305 if String(d.properties.Removed_dex_api_filename) != "" {
1306 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001307 cmd.FlagWithOutput("--removed-dex-api ", d.removedDexApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001308 }
1309
Nan Zhang9c69a122018-08-22 10:22:08 -07001310 if Bool(d.properties.Write_sdk_values) {
Jerome Gaillard0f599032019-10-10 19:29:11 +01001311 d.metadataDir = android.PathForModuleOut(ctx, "metadata")
1312 cmd.FlagWithArg("--sdk-values ", d.metadataDir.String())
Nan Zhang9c69a122018-08-22 10:22:08 -07001313 }
1314
Paul Duffin3ae29512020-04-08 18:18:03 +01001315 if stubsDir.Valid() {
1316 if Bool(d.properties.Create_doc_stubs) {
1317 cmd.FlagWithArg("--doc-stubs ", stubsDir.String())
1318 } else {
1319 cmd.FlagWithArg("--stubs ", stubsDir.String())
1320 cmd.Flag("--exclude-documentation-from-stubs")
1321 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001322 }
Makoto Onuki1d5b7132020-06-16 14:41:10 -07001323 cmd.FlagWithArg("--hide ", "ShowingMemberInHiddenClass") // b/159121253 -- remove it once all the violations are fixed.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001324}
1325
Colin Cross33961b52019-07-11 11:01:22 -07001326func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang1598a9e2018-09-04 17:14:32 -07001327 if Bool(d.properties.Annotations_enabled) {
Colin Cross33961b52019-07-11 11:01:22 -07001328 cmd.Flag("--include-annotations")
1329
Pete Gillinc382a562018-11-14 18:45:46 +00001330 validatingNullability :=
1331 strings.Contains(d.Javadoc.args, "--validate-nullability-from-merged-stubs") ||
1332 String(d.properties.Validate_nullability_from_list) != ""
Paul Duffin13a9dd62019-11-04 10:26:47 +00001333
Pete Gillina262c052018-09-14 14:25:48 +01001334 migratingNullability := String(d.properties.Previous_api) != ""
Pete Gillina262c052018-09-14 14:25:48 +01001335 if migratingNullability {
Colin Cross8a497952019-03-05 22:25:09 -08001336 previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
Colin Cross33961b52019-07-11 11:01:22 -07001337 cmd.FlagWithInput("--migrate-nullness ", previousApi)
Pete Gillina262c052018-09-14 14:25:48 +01001338 }
Colin Cross33961b52019-07-11 11:01:22 -07001339
Pete Gillinc382a562018-11-14 18:45:46 +00001340 if s := String(d.properties.Validate_nullability_from_list); s != "" {
Colin Cross33961b52019-07-11 11:01:22 -07001341 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
Pete Gillinc382a562018-11-14 18:45:46 +00001342 }
Colin Cross33961b52019-07-11 11:01:22 -07001343
Pete Gillina262c052018-09-14 14:25:48 +01001344 if validatingNullability {
Pete Gillin581d6082018-10-22 15:55:04 +01001345 d.nullabilityWarningsFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_nullability_warnings.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001346 cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile)
Pete Gillina262c052018-09-14 14:25:48 +01001347 }
Nan Zhanga40da042018-08-01 12:48:00 -07001348
1349 d.annotationsZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"_annotations.zip")
Colin Cross33961b52019-07-11 11:01:22 -07001350 cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip)
Nan Zhangf4936b02018-08-01 15:00:28 -07001351
Anton Hansson9d7c3fb2020-05-21 10:11:31 +01001352 if len(d.properties.Merge_annotations_dirs) != 0 {
1353 d.mergeAnnoDirFlags(ctx, cmd)
Nan Zhanga40da042018-08-01 12:48:00 -07001354 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001355
Colin Cross33961b52019-07-11 11:01:22 -07001356 // TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
1357 cmd.FlagWithArg("--hide ", "HiddenTypedefConstant").
1358 FlagWithArg("--hide ", "SuperfluousPrefix").
1359 FlagWithArg("--hide ", "AnnotationExtraction")
1360 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001361}
1362
Colin Cross33961b52019-07-11 11:01:22 -07001363func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
1364 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
1365 if t, ok := m.(*ExportedDroiddocDir); ok {
1366 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
1367 } else {
1368 ctx.PropertyErrorf("merge_annotations_dirs",
1369 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1370 }
1371 })
1372}
1373
1374func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Pete Gillin77167902018-09-19 18:16:26 +01001375 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
1376 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Cross33961b52019-07-11 11:01:22 -07001377 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
Pete Gillin77167902018-09-19 18:16:26 +01001378 } else {
1379 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
1380 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1381 }
1382 })
Nan Zhanga40da042018-08-01 12:48:00 -07001383}
1384
Colin Cross33961b52019-07-11 11:01:22 -07001385func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang9c69a122018-08-22 10:22:08 -07001386 if Bool(d.properties.Api_levels_annotations_enabled) {
1387 d.apiVersionsXml = android.PathForModuleOut(ctx, "api-versions.xml")
Nan Zhang9c69a122018-08-22 10:22:08 -07001388
1389 if len(d.properties.Api_levels_annotations_dirs) == 0 {
1390 ctx.PropertyErrorf("api_levels_annotations_dirs",
1391 "has to be non-empty if api levels annotations was enabled!")
1392 }
1393
Colin Cross33961b52019-07-11 11:01:22 -07001394 cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml)
1395 cmd.FlagWithInput("--apply-api-levels ", d.apiVersionsXml)
1396 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion())
1397 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
Nan Zhang9c69a122018-08-22 10:22:08 -07001398
1399 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
1400 if t, ok := m.(*ExportedDroiddocDir); ok {
Nan Zhang9c69a122018-08-22 10:22:08 -07001401 for _, dep := range t.deps {
1402 if strings.HasSuffix(dep.String(), "android.jar") {
Colin Cross33961b52019-07-11 11:01:22 -07001403 cmd.Implicit(dep)
Nan Zhang9c69a122018-08-22 10:22:08 -07001404 }
1405 }
Colin Cross33961b52019-07-11 11:01:22 -07001406 cmd.FlagWithArg("--android-jar-pattern ", t.dir.String()+"/%/public/android.jar")
Nan Zhang9c69a122018-08-22 10:22:08 -07001407 } else {
1408 ctx.PropertyErrorf("api_levels_annotations_dirs",
1409 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
1410 }
1411 })
1412
1413 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001414}
1415
Colin Cross33961b52019-07-11 11:01:22 -07001416func (d *Droidstubs) apiToXmlFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang71bbe632018-09-17 14:32:21 -07001417 if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() {
1418 if d.apiFile.String() == "" {
1419 ctx.ModuleErrorf("API signature file has to be specified in Metalava when jdiff is enabled.")
1420 }
1421
1422 d.apiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001423 cmd.FlagWithOutput("--api-xml ", d.apiXmlFile)
Nan Zhang71bbe632018-09-17 14:32:21 -07001424
1425 if String(d.properties.Check_api.Last_released.Api_file) == "" {
1426 ctx.PropertyErrorf("check_api.last_released.api_file",
1427 "has to be non-empty if jdiff was enabled!")
1428 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001429
Colin Cross33961b52019-07-11 11:01:22 -07001430 lastReleasedApi := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
Nan Zhang71bbe632018-09-17 14:32:21 -07001431 d.lastReleasedApiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_last_released_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001432 cmd.FlagWithInput("--convert-to-jdiff ", lastReleasedApi).Output(d.lastReleasedApiXmlFile)
1433 }
1434}
Nan Zhang71bbe632018-09-17 14:32:21 -07001435
Colin Cross1e743852019-10-28 11:37:20 -07001436func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths,
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001437 srcJarList android.Path, bootclasspath, classpath classpath, sourcepaths android.Paths, implicitsRsp android.WritablePath, sandbox bool) *android.RuleBuilderCommand {
Colin Cross8b8bec32019-11-15 13:18:43 -08001438 // Metalava uses lots of memory, restrict the number of metalava jobs that can run in parallel.
1439 rule.HighMem()
Ramy Medhat427683c2020-04-30 03:08:37 -04001440 cmd := rule.Command()
1441 if ctx.Config().IsEnvTrue("RBE_METALAVA") {
1442 rule.Remoteable(android.RemoteRuleSupports{RBE: true})
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001443 pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "metalava")
1444 execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
1445 labels := map[string]string{"type": "compile", "lang": "java", "compiler": "metalava"}
1446 if !sandbox {
1447 execStrategy = remoteexec.LocalExecStrategy
1448 labels["shallow"] = "true"
Ramy Medhat427683c2020-04-30 03:08:37 -04001449 }
1450 inputs := []string{android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "metalava.jar").String()}
1451 if v := ctx.Config().Getenv("RBE_METALAVA_INPUTS"); v != "" {
1452 inputs = append(inputs, strings.Split(v, ",")...)
1453 }
1454 cmd.Text((&remoteexec.REParams{
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001455 Labels: labels,
Ramy Medhat427683c2020-04-30 03:08:37 -04001456 ExecStrategy: execStrategy,
1457 Inputs: inputs,
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001458 RSPFile: implicitsRsp.String(),
Ramy Medhat427683c2020-04-30 03:08:37 -04001459 ToolchainInputs: []string{config.JavaCmd(ctx).String()},
1460 Platform: map[string]string{remoteexec.PoolKey: pool},
1461 }).NoVarTemplate(ctx.Config()))
1462 }
1463
1464 cmd.BuiltTool(ctx, "metalava").
Colin Cross33961b52019-07-11 11:01:22 -07001465 Flag(config.JavacVmFlags).
1466 FlagWithArg("-encoding ", "UTF-8").
Colin Cross1e743852019-10-28 11:37:20 -07001467 FlagWithArg("-source ", javaVersion.String()).
Colin Cross33961b52019-07-11 11:01:22 -07001468 FlagWithRspFileInputList("@", srcs).
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001469 FlagWithInput("@", srcJarList)
1470
1471 if javaHome := ctx.Config().Getenv("ANDROID_JAVA_HOME"); javaHome != "" {
1472 cmd.Implicit(android.PathForSource(ctx, javaHome))
1473 }
1474
1475 if sandbox {
1476 cmd.FlagWithOutput("--strict-input-files ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
1477 } else {
1478 cmd.FlagWithOutput("--strict-input-files:warn ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
1479 }
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001480
Colin Cross238c1f32020-06-07 16:58:18 -07001481 if implicitsRsp != nil {
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001482 cmd.FlagWithArg("--strict-input-files-exempt ", "@"+implicitsRsp.String())
1483 }
Colin Cross33961b52019-07-11 11:01:22 -07001484
1485 if len(bootclasspath) > 0 {
1486 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
Nan Zhang71bbe632018-09-17 14:32:21 -07001487 }
1488
Colin Cross33961b52019-07-11 11:01:22 -07001489 if len(classpath) > 0 {
1490 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
1491 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001492
Colin Cross33961b52019-07-11 11:01:22 -07001493 if len(sourcepaths) > 0 {
1494 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
1495 } else {
1496 cmd.FlagWithArg("-sourcepath ", `""`)
1497 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001498
Colin Cross33961b52019-07-11 11:01:22 -07001499 cmd.Flag("--no-banner").
1500 Flag("--color").
1501 Flag("--quiet").
1502 Flag("--format=v2")
Nan Zhang86d2d552018-08-09 15:33:27 -07001503
Colin Cross33961b52019-07-11 11:01:22 -07001504 return cmd
Nan Zhang71bbe632018-09-17 14:32:21 -07001505}
1506
Nan Zhang1598a9e2018-09-04 17:14:32 -07001507func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga40da042018-08-01 12:48:00 -07001508 deps := d.Javadoc.collectDeps(ctx)
1509
1510 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), sdkContext(d))
Nan Zhang581fd212018-01-10 16:06:12 -08001511
Colin Cross33961b52019-07-11 11:01:22 -07001512 // Create rule for metalava
Nan Zhanga40da042018-08-01 12:48:00 -07001513
Colin Cross33961b52019-07-11 11:01:22 -07001514 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang71bbe632018-09-17 14:32:21 -07001515
Colin Cross33961b52019-07-11 11:01:22 -07001516 rule := android.NewRuleBuilder()
Nan Zhanga40da042018-08-01 12:48:00 -07001517
Paul Duffin3ae29512020-04-08 18:18:03 +01001518 generateStubs := BoolDefault(d.properties.Generate_stubs, true)
1519 var stubsDir android.OptionalPath
1520 if generateStubs {
1521 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1522 stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "stubsDir"))
1523 rule.Command().Text("rm -rf").Text(stubsDir.String())
1524 rule.Command().Text("mkdir -p").Text(stubsDir.String())
1525 }
Nan Zhanga40da042018-08-01 12:48:00 -07001526
Colin Cross33961b52019-07-11 11:01:22 -07001527 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1528
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001529 implicitsRsp := android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"implicits.rsp")
1530
Colin Cross33961b52019-07-11 11:01:22 -07001531 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001532 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths, implicitsRsp,
1533 Bool(d.Javadoc.properties.Sandbox))
1534 cmd.Implicits(d.Javadoc.implicits)
Colin Cross33961b52019-07-11 11:01:22 -07001535
1536 d.stubsFlags(ctx, cmd, stubsDir)
1537
1538 d.annotationsFlags(ctx, cmd)
1539 d.inclusionAnnotationsFlags(ctx, cmd)
1540 d.apiLevelsAnnotationsFlags(ctx, cmd)
1541 d.apiToXmlFlags(ctx, cmd)
Nan Zhang71bbe632018-09-17 14:32:21 -07001542
Nan Zhang1598a9e2018-09-04 17:14:32 -07001543 if strings.Contains(d.Javadoc.args, "--generate-documentation") {
1544 // Currently Metalava have the ability to invoke Javadoc in a seperate process.
1545 // Pass "-nodocs" to suppress the Javadoc invocation when Metalava receives
1546 // "--generate-documentation" arg. This is not needed when Metalava removes this feature.
1547 d.Javadoc.args = d.Javadoc.args + " -nodocs "
Nan Zhang79614d12018-04-19 18:03:39 -07001548 }
Colin Cross33961b52019-07-11 11:01:22 -07001549
1550 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1551 for _, o := range d.Javadoc.properties.Out {
1552 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1553 }
1554
Makoto Onuki88b99052020-04-27 17:22:16 -07001555 // Add options for the other optional tasks: API-lint and check-released.
1556 // We generate separate timestamp files for them.
1557
1558 doApiLint := false
1559 doCheckReleased := false
1560
1561 // Add API lint options.
1562
1563 if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) && !ctx.Config().IsPdkBuild() {
1564 doApiLint = true
1565
1566 newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since)
1567 if newSince.Valid() {
1568 cmd.FlagWithInput("--api-lint ", newSince.Path())
1569 } else {
1570 cmd.Flag("--api-lint")
1571 }
1572 d.apiLintReport = android.PathForModuleOut(ctx, "api_lint_report.txt")
1573 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport) // TODO: Change to ":api-lint"
1574
1575 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
1576 updatedBaselineOutput := android.PathForModuleOut(ctx, "api_lint_baseline.txt")
1577 d.apiLintTimestamp = android.PathForModuleOut(ctx, "api_lint.timestamp")
1578
1579 // Note this string includes a special shell quote $' ... ', which decodes the "\n"s.
1580 // However, because $' ... ' doesn't expand environmental variables, we can't just embed
1581 // $PWD, so we have to terminate $'...', use "$PWD", then start $' ... ' again,
1582 // which is why we have '"$PWD"$' in it.
1583 //
1584 // TODO: metalava also has a slightly different message hardcoded. Should we unify this
1585 // message and metalava's one?
1586 msg := `$'` + // Enclose with $' ... '
1587 `************************************************************\n` +
1588 `Your API changes are triggering API Lint warnings or errors.\n` +
1589 `To make these errors go away, fix the code according to the\n` +
1590 `error and/or warning messages above.\n` +
1591 `\n` +
1592 `If it is not possible to do so, there are workarounds:\n` +
1593 `\n` +
1594 `1. You can suppress the errors with @SuppressLint("<id>")\n`
1595
1596 if baselineFile.Valid() {
1597 cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path())
1598 cmd.FlagWithOutput("--update-baseline:api-lint ", updatedBaselineOutput)
1599
1600 msg += fmt.Sprintf(``+
1601 `2. You can update the baseline by executing the following\n`+
1602 ` command:\n`+
Anton Hansson3361a292020-05-11 15:38:31 +01001603 ` cp \\\n`+
1604 ` "'"$PWD"$'/%s" \\\n`+
1605 ` "'"$PWD"$'/%s"\n`+
Makoto Onuki88b99052020-04-27 17:22:16 -07001606 ` To submit the revised baseline.txt to the main Android\n`+
1607 ` repository, you will need approval.\n`, updatedBaselineOutput, baselineFile.Path())
1608 } else {
1609 msg += fmt.Sprintf(``+
1610 `2. You can add a baseline file of existing lint failures\n`+
1611 ` to the build rule of %s.\n`, d.Name())
1612 }
1613 // Note the message ends with a ' (single quote), to close the $' ... ' .
1614 msg += `************************************************************\n'`
1615
1616 cmd.FlagWithArg("--error-message:api-lint ", msg)
1617 }
1618
1619 // Add "check released" options. (Detect incompatible API changes from the last public release)
1620
1621 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") &&
1622 !ctx.Config().IsPdkBuild() {
1623 doCheckReleased = true
1624
1625 if len(d.Javadoc.properties.Out) > 0 {
1626 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1627 }
1628
1629 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1630 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
1631 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
1632 updatedBaselineOutput := android.PathForModuleOut(ctx, "last_released_baseline.txt")
1633
1634 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
1635
1636 cmd.FlagWithInput("--check-compatibility:api:released ", apiFile)
1637 cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile)
1638
1639 if baselineFile.Valid() {
1640 cmd.FlagWithInput("--baseline:compatibility:released ", baselineFile.Path())
1641 cmd.FlagWithOutput("--update-baseline:compatibility:released ", updatedBaselineOutput)
1642 }
1643
1644 // Note this string includes quote ($' ... '), which decodes the "\n"s.
1645 msg := `$'\n******************************\n` +
1646 `You have tried to change the API from what has been previously released in\n` +
1647 `an SDK. Please fix the errors listed above.\n` +
1648 `******************************\n'`
1649
1650 cmd.FlagWithArg("--error-message:compatibility:released ", msg)
1651 }
1652
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001653 impRule := android.NewRuleBuilder()
1654 impCmd := impRule.Command()
1655 // A dummy action that copies the ninja generated rsp file to a new location. This allows us to
1656 // add a large number of inputs to a file without exceeding bash command length limits (which
1657 // would happen if we use the WriteFile rule). The cp is needed because RuleBuilder sets the
1658 // rsp file to be ${output}.rsp.
1659 impCmd.Text("cp").FlagWithRspFileInputList("", cmd.GetImplicits()).Output(implicitsRsp)
1660 impRule.Build(pctx, ctx, "implicitsGen", "implicits generation")
1661 cmd.Implicit(implicitsRsp)
1662
Paul Duffin3ae29512020-04-08 18:18:03 +01001663 if generateStubs {
1664 rule.Command().
1665 BuiltTool(ctx, "soong_zip").
1666 Flag("-write_if_changed").
1667 Flag("-jar").
1668 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
1669 FlagWithArg("-C ", stubsDir.String()).
1670 FlagWithArg("-D ", stubsDir.String())
1671 }
Jerome Gaillard0f599032019-10-10 19:29:11 +01001672
1673 if Bool(d.properties.Write_sdk_values) {
1674 d.metadataZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-metadata.zip")
1675 rule.Command().
1676 BuiltTool(ctx, "soong_zip").
1677 Flag("-write_if_changed").
1678 Flag("-d").
1679 FlagWithOutput("-o ", d.metadataZip).
1680 FlagWithArg("-C ", d.metadataDir.String()).
1681 FlagWithArg("-D ", d.metadataDir.String())
1682 }
1683
Makoto Onuki88b99052020-04-27 17:22:16 -07001684 // TODO: We don't really need two separate API files, but this is a reminiscence of how
1685 // we used to run metalava separately for API lint and the "last_released" check. Unify them.
1686 if doApiLint {
1687 rule.Command().Text("touch").Output(d.apiLintTimestamp)
1688 }
1689 if doCheckReleased {
1690 rule.Command().Text("touch").Output(d.checkLastReleasedApiTimestamp)
1691 }
1692
Colin Cross33961b52019-07-11 11:01:22 -07001693 rule.Restat()
1694
1695 zipSyncCleanupCmd(rule, srcJarDir)
1696
Makoto Onuki88b99052020-04-27 17:22:16 -07001697 rule.Build(pctx, ctx, "metalava", "metalava merged")
Adrian Roos075eedc2019-10-10 12:07:03 +02001698
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001699 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001700 !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001701
1702 if len(d.Javadoc.properties.Out) > 0 {
1703 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1704 }
1705
1706 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1707 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Adrian Roos14f75a92019-08-12 17:54:09 +02001708 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
Makoto Onuki5405a732020-04-16 17:02:40 -07001709
1710 if baselineFile.Valid() {
Makoto Onuki88b99052020-04-27 17:22:16 -07001711 ctx.PropertyErrorf("baseline_file", "current API check can't have a baseline file. (module %s)", ctx.ModuleName())
Makoto Onuki5405a732020-04-16 17:02:40 -07001712 }
Nan Zhang61819ce2018-05-04 18:49:16 -07001713
Nan Zhang2760dfc2018-08-24 17:32:54 +00001714 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Nan Zhang2760dfc2018-08-24 17:32:54 +00001715
Colin Cross33961b52019-07-11 11:01:22 -07001716 rule := android.NewRuleBuilder()
1717
Makoto Onuki5405a732020-04-16 17:02:40 -07001718 // Diff command line.
Makoto Onuki88b99052020-04-27 17:22:16 -07001719 // -F matches the closest "opening" line, such as "package android {"
1720 // and " public class Intent {".
Makoto Onuki5405a732020-04-16 17:02:40 -07001721 diff := `diff -u -F '{ *$'`
1722
Colin Cross33961b52019-07-11 11:01:22 -07001723 rule.Command().Text("( true")
Makoto Onuki5405a732020-04-16 17:02:40 -07001724 rule.Command().
1725 Text(diff).
1726 Input(apiFile).Input(d.apiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001727
Makoto Onuki5405a732020-04-16 17:02:40 -07001728 rule.Command().
1729 Text(diff).
1730 Input(removedApiFile).Input(d.removedApiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001731
1732 msg := fmt.Sprintf(`\n******************************\n`+
1733 `You have tried to change the API from what has been previously approved.\n\n`+
1734 `To make these errors go away, you have two choices:\n`+
Makoto Onuki5405a732020-04-16 17:02:40 -07001735 ` 1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)\n`+
1736 ` to the new methods, etc. shown in the above diff.\n\n`+
1737 ` 2. You can update current.txt and/or removed.txt by executing the following command:\n`+
Colin Cross33961b52019-07-11 11:01:22 -07001738 ` make %s-update-current-api\n\n`+
1739 ` To submit the revised current.txt to the main Android repository,\n`+
1740 ` you will need approval.\n`+
1741 `******************************\n`, ctx.ModuleName())
1742
1743 rule.Command().
1744 Text("touch").Output(d.checkCurrentApiTimestamp).
1745 Text(") || (").
1746 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1747 Text("; exit 38").
1748 Text(")")
1749
Makoto Onuki5405a732020-04-16 17:02:40 -07001750 rule.Build(pctx, ctx, "metalavaCurrentApiCheck", "check current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001751
1752 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001753
1754 // update API rule
1755 rule = android.NewRuleBuilder()
1756
1757 rule.Command().Text("( true")
1758
1759 rule.Command().
1760 Text("cp").Flag("-f").
1761 Input(d.apiFile).Flag(apiFile.String())
1762
1763 rule.Command().
1764 Text("cp").Flag("-f").
1765 Input(d.removedApiFile).Flag(removedApiFile.String())
1766
1767 msg = "failed to update public API"
1768
1769 rule.Command().
1770 Text("touch").Output(d.updateCurrentApiTimestamp).
1771 Text(") || (").
1772 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1773 Text("; exit 38").
1774 Text(")")
1775
1776 rule.Build(pctx, ctx, "metalavaCurrentApiUpdate", "update current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001777 }
Nan Zhanga40da042018-08-01 12:48:00 -07001778
Pete Gillin581d6082018-10-22 15:55:04 +01001779 if String(d.properties.Check_nullability_warnings) != "" {
1780 if d.nullabilityWarningsFile == nil {
1781 ctx.PropertyErrorf("check_nullability_warnings",
1782 "Cannot specify check_nullability_warnings unless validating nullability")
1783 }
Colin Cross33961b52019-07-11 11:01:22 -07001784
1785 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
1786
Pete Gillin581d6082018-10-22 15:55:04 +01001787 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "check_nullability_warnings.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001788
Pete Gillin581d6082018-10-22 15:55:04 +01001789 msg := fmt.Sprintf(`\n******************************\n`+
1790 `The warnings encountered during nullability annotation validation did\n`+
1791 `not match the checked in file of expected warnings. The diffs are shown\n`+
1792 `above. You have two options:\n`+
1793 ` 1. Resolve the differences by editing the nullability annotations.\n`+
1794 ` 2. Update the file of expected warnings by running:\n`+
1795 ` cp %s %s\n`+
1796 ` and submitting the updated file as part of your change.`,
1797 d.nullabilityWarningsFile, checkNullabilityWarnings)
Colin Cross33961b52019-07-11 11:01:22 -07001798
1799 rule := android.NewRuleBuilder()
1800
1801 rule.Command().
1802 Text("(").
1803 Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile).
1804 Text("&&").
1805 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
1806 Text(") || (").
1807 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1808 Text("; exit 38").
1809 Text(")")
1810
1811 rule.Build(pctx, ctx, "nullabilityWarningsCheck", "nullability warnings check")
Pete Gillin581d6082018-10-22 15:55:04 +01001812 }
1813
Nan Zhang71bbe632018-09-17 14:32:21 -07001814 if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001815 if len(d.Javadoc.properties.Out) > 0 {
1816 ctx.PropertyErrorf("out", "out property may not be combined with jdiff")
1817 }
1818
1819 outDir := android.PathForModuleOut(ctx, "jdiff-out")
1820 srcJarDir := android.PathForModuleOut(ctx, "jdiff-srcjars")
1821 stubsDir := android.PathForModuleOut(ctx, "jdiff-stubsDir")
1822
1823 rule := android.NewRuleBuilder()
Nan Zhang71bbe632018-09-17 14:32:21 -07001824
Nan Zhang86b06202018-09-21 17:09:21 -07001825 // Please sync with android-api-council@ before making any changes for the name of jdiffDocZip below
1826 // since there's cron job downstream that fetch this .zip file periodically.
1827 // See b/116221385 for reference.
Nan Zhang71bbe632018-09-17 14:32:21 -07001828 d.jdiffDocZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-docs.zip")
1829 d.jdiffStubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-stubs.srcjar")
1830
Nan Zhang71bbe632018-09-17 14:32:21 -07001831 jdiff := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jdiff.jar")
Nan Zhang71bbe632018-09-17 14:32:21 -07001832
Colin Cross33961b52019-07-11 11:01:22 -07001833 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
1834 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang71bbe632018-09-17 14:32:21 -07001835
Colin Cross33961b52019-07-11 11:01:22 -07001836 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1837
Colin Crossdaa4c672019-07-15 22:53:46 -07001838 cmd := javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -07001839 deps.bootClasspath, deps.classpath, d.sourcepaths)
1840
1841 cmd.Flag("-J-Xmx1600m").
Colin Cross33961b52019-07-11 11:01:22 -07001842 Flag("-XDignore.symbol.file").
1843 FlagWithArg("-doclet ", "jdiff.JDiff").
1844 FlagWithInput("-docletpath ", jdiff).
1845 Flag("-quiet").
1846 FlagWithArg("-newapi ", strings.TrimSuffix(d.apiXmlFile.Base(), d.apiXmlFile.Ext())).
1847 FlagWithArg("-newapidir ", filepath.Dir(d.apiXmlFile.String())).
1848 Implicit(d.apiXmlFile).
1849 FlagWithArg("-oldapi ", strings.TrimSuffix(d.lastReleasedApiXmlFile.Base(), d.lastReleasedApiXmlFile.Ext())).
1850 FlagWithArg("-oldapidir ", filepath.Dir(d.lastReleasedApiXmlFile.String())).
1851 Implicit(d.lastReleasedApiXmlFile)
1852
Colin Cross33961b52019-07-11 11:01:22 -07001853 rule.Command().
1854 BuiltTool(ctx, "soong_zip").
1855 Flag("-write_if_changed").
1856 Flag("-d").
1857 FlagWithOutput("-o ", d.jdiffDocZip).
1858 FlagWithArg("-C ", outDir.String()).
1859 FlagWithArg("-D ", outDir.String())
1860
1861 rule.Command().
1862 BuiltTool(ctx, "soong_zip").
1863 Flag("-write_if_changed").
1864 Flag("-jar").
1865 FlagWithOutput("-o ", d.jdiffStubsSrcJar).
1866 FlagWithArg("-C ", stubsDir.String()).
1867 FlagWithArg("-D ", stubsDir.String())
1868
1869 rule.Restat()
1870
1871 zipSyncCleanupCmd(rule, srcJarDir)
1872
1873 rule.Build(pctx, ctx, "jdiff", "jdiff")
Nan Zhang71bbe632018-09-17 14:32:21 -07001874 }
Nan Zhang581fd212018-01-10 16:06:12 -08001875}
Dan Willemsencc090972018-02-26 14:33:31 -08001876
Nan Zhanga40da042018-08-01 12:48:00 -07001877//
Nan Zhangf4936b02018-08-01 15:00:28 -07001878// Exported Droiddoc Directory
Nan Zhanga40da042018-08-01 12:48:00 -07001879//
Dan Willemsencc090972018-02-26 14:33:31 -08001880var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
Nan Zhangf4936b02018-08-01 15:00:28 -07001881var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
Pete Gillin77167902018-09-19 18:16:26 +01001882var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
Nan Zhang9c69a122018-08-22 10:22:08 -07001883var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
Dan Willemsencc090972018-02-26 14:33:31 -08001884
Nan Zhangf4936b02018-08-01 15:00:28 -07001885type ExportedDroiddocDirProperties struct {
1886 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -08001887 Path *string
1888}
1889
Nan Zhangf4936b02018-08-01 15:00:28 -07001890type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -08001891 android.ModuleBase
1892
Nan Zhangf4936b02018-08-01 15:00:28 -07001893 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -08001894
1895 deps android.Paths
1896 dir android.Path
1897}
1898
Colin Crossa3002fc2019-07-08 16:48:04 -07001899// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -07001900func ExportedDroiddocDirFactory() android.Module {
1901 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -08001902 module.AddProperties(&module.properties)
1903 android.InitAndroidModule(module)
1904 return module
1905}
1906
Nan Zhangf4936b02018-08-01 15:00:28 -07001907func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -08001908
Nan Zhangf4936b02018-08-01 15:00:28 -07001909func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -08001910 path := String(d.properties.Path)
1911 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -08001912 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Dan Willemsencc090972018-02-26 14:33:31 -08001913}
Nan Zhangb2b33de2018-02-23 11:18:47 -08001914
1915//
1916// Defaults
1917//
1918type DocDefaults struct {
1919 android.ModuleBase
1920 android.DefaultsModuleBase
1921}
1922
Nan Zhangb2b33de2018-02-23 11:18:47 -08001923func DocDefaultsFactory() android.Module {
1924 module := &DocDefaults{}
1925
1926 module.AddProperties(
1927 &JavadocProperties{},
1928 &DroiddocProperties{},
1929 )
1930
1931 android.InitDefaultsModule(module)
1932
1933 return module
1934}
Nan Zhang1598a9e2018-09-04 17:14:32 -07001935
1936func StubsDefaultsFactory() android.Module {
1937 module := &DocDefaults{}
1938
1939 module.AddProperties(
1940 &JavadocProperties{},
1941 &DroidstubsProperties{},
1942 )
1943
1944 android.InitDefaultsModule(module)
1945
1946 return module
1947}
Colin Cross33961b52019-07-11 11:01:22 -07001948
1949func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
1950 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
1951
1952 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1953 rule.Command().Text("mkdir -p").Text(srcJarDir.String())
1954 srcJarList := srcJarDir.Join(ctx, "list")
1955
1956 rule.Temporary(srcJarList)
1957
1958 rule.Command().BuiltTool(ctx, "zipsync").
1959 FlagWithArg("-d ", srcJarDir.String()).
1960 FlagWithOutput("-l ", srcJarList).
1961 FlagWithArg("-f ", `"*.java"`).
1962 Inputs(srcJars)
1963
1964 return srcJarList
1965}
1966
1967func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
1968 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1969}
Paul Duffin91547182019-11-12 19:39:36 +00001970
1971var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
1972
1973type PrebuiltStubsSourcesProperties struct {
1974 Srcs []string `android:"path"`
1975}
1976
1977type PrebuiltStubsSources struct {
1978 android.ModuleBase
1979 android.DefaultableModuleBase
1980 prebuilt android.Prebuilt
1981 android.SdkBase
1982
1983 properties PrebuiltStubsSourcesProperties
1984
Paul Duffin9b478b02019-12-10 13:41:51 +00001985 // The source directories containing stubs source files.
1986 srcDirs android.Paths
Paul Duffin91547182019-11-12 19:39:36 +00001987 stubsSrcJar android.ModuleOutPath
1988}
1989
Paul Duffin9b478b02019-12-10 13:41:51 +00001990func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) {
1991 switch tag {
1992 case "":
1993 return android.Paths{p.stubsSrcJar}, nil
1994 default:
1995 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1996 }
1997}
1998
Paul Duffin0f8faff2020-05-20 16:18:00 +01001999func (d *PrebuiltStubsSources) StubsSrcJar() android.Path {
2000 return d.stubsSrcJar
2001}
2002
Paul Duffin91547182019-11-12 19:39:36 +00002003func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin9b478b02019-12-10 13:41:51 +00002004 p.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
2005
2006 p.srcDirs = android.PathsForModuleSrc(ctx, p.properties.Srcs)
2007
2008 rule := android.NewRuleBuilder()
2009 command := rule.Command().
2010 BuiltTool(ctx, "soong_zip").
2011 Flag("-write_if_changed").
2012 Flag("-jar").
2013 FlagWithOutput("-o ", p.stubsSrcJar)
2014
2015 for _, d := range p.srcDirs {
2016 dir := d.String()
2017 command.
2018 FlagWithArg("-C ", dir).
2019 FlagWithInput("-D ", d)
2020 }
2021
2022 rule.Restat()
2023
2024 rule.Build(pctx, ctx, "zip src", "Create srcjar from prebuilt source")
Paul Duffin91547182019-11-12 19:39:36 +00002025}
2026
2027func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
2028 return &p.prebuilt
2029}
2030
2031func (p *PrebuiltStubsSources) Name() string {
2032 return p.prebuilt.Name(p.ModuleBase.Name())
2033}
2034
Paul Duffin91547182019-11-12 19:39:36 +00002035// prebuilt_stubs_sources imports a set of java source files as if they were
2036// generated by droidstubs.
2037//
2038// By default, a prebuilt_stubs_sources has a single variant that expects a
2039// set of `.java` files generated by droidstubs.
2040//
2041// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2042// for host modules.
2043//
2044// Intended only for use by sdk snapshots.
2045func PrebuiltStubsSourcesFactory() android.Module {
2046 module := &PrebuiltStubsSources{}
2047
2048 module.AddProperties(&module.properties)
2049
2050 android.InitPrebuiltModule(module, &module.properties.Srcs)
2051 android.InitSdkAwareModule(module)
2052 InitDroiddocModule(module, android.HostAndDeviceSupported)
2053 return module
2054}
2055
Paul Duffin13879572019-11-28 14:31:38 +00002056type droidStubsSdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00002057 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00002058}
2059
2060func (mt *droidStubsSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2061 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2062}
2063
2064func (mt *droidStubsSdkMemberType) IsInstance(module android.Module) bool {
2065 _, ok := module.(*Droidstubs)
2066 return ok
2067}
2068
Paul Duffin495ffb92020-03-20 13:35:40 +00002069func (mt *droidStubsSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2070 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_stubs_sources")
2071}
2072
2073func (mt *droidStubsSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2074 return &droidStubsInfoProperties{}
2075}
2076
2077type droidStubsInfoProperties struct {
2078 android.SdkMemberPropertiesBase
2079
2080 StubsSrcJar android.Path
2081}
2082
2083func (p *droidStubsInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2084 droidstubs := variant.(*Droidstubs)
2085 p.StubsSrcJar = droidstubs.stubsSrcJar
2086}
2087
2088func (p *droidStubsInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
2089 if p.StubsSrcJar != nil {
2090 builder := ctx.SnapshotBuilder()
2091
2092 snapshotRelativeDir := filepath.Join("java", ctx.Name()+"_stubs_sources")
2093
2094 builder.UnzipToSnapshot(p.StubsSrcJar, snapshotRelativeDir)
2095
2096 propertySet.AddProperty("srcs", []string{snapshotRelativeDir})
Paul Duffin13879572019-11-28 14:31:38 +00002097 }
Paul Duffin91547182019-11-12 19:39:36 +00002098}