blob: 61bdc88748984cf2dafbcda50c2ad0051d68bd7c [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))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700443 if sdkDep.useDefaultLibs {
Pete Gillin0638dfc2020-07-01 10:48:14 +0100444 ctx.AddVariationDependencies(nil, bootClasspathTag, config.LegacyCorePlatformBootclasspathLibraries...)
445 ctx.AddVariationDependencies(nil, systemModulesTag, config.LegacyCorePlatformSystemModules)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700446 if sdkDep.hasFrameworkLibs() {
Pete Gillin0638dfc2020-07-01 10:48:14 +0100447 ctx.AddVariationDependencies(nil, libTag, config.FrameworkLibraries...)
Nan Zhang357466b2018-04-17 17:38:36 -0700448 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700449 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700450 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100451 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700452 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Nan Zhang581fd212018-01-10 16:06:12 -0800453 }
454 }
455
Colin Cross42d48b72018-08-29 14:10:52 -0700456 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800457}
458
Nan Zhanga40da042018-08-01 12:48:00 -0700459func (j *Javadoc) collectAidlFlags(ctx android.ModuleContext, deps deps) droiddocBuilderFlags {
460 var flags droiddocBuilderFlags
Jiyong Park1e440682018-05-23 18:42:04 +0900461
Colin Cross3047fa22019-04-18 10:56:44 -0700462 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Jiyong Park1e440682018-05-23 18:42:04 +0900463
464 return flags
465}
466
467func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700468 aidlIncludeDirs android.Paths) (string, android.Paths) {
Jiyong Park1e440682018-05-23 18:42:04 +0900469
470 aidlIncludes := android.PathsForModuleSrc(ctx, j.properties.Aidl.Local_include_dirs)
471 aidlIncludes = append(aidlIncludes, android.PathsForSource(ctx, j.properties.Aidl.Include_dirs)...)
472
473 var flags []string
Colin Cross3047fa22019-04-18 10:56:44 -0700474 var deps android.Paths
475
Jiyong Park1e440682018-05-23 18:42:04 +0900476 if aidlPreprocess.Valid() {
477 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700478 deps = append(deps, aidlPreprocess.Path())
Jiyong Park1e440682018-05-23 18:42:04 +0900479 } else {
480 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
481 }
482
483 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
484 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
485 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
486 flags = append(flags, "-I"+src.String())
487 }
488
Colin Cross3047fa22019-04-18 10:56:44 -0700489 return strings.Join(flags, " "), deps
Jiyong Park1e440682018-05-23 18:42:04 +0900490}
491
Jiyong Parkd90d7412019-08-20 22:49:19 +0900492// TODO: remove the duplication between this and the one in gen.go
Jiyong Park1e440682018-05-23 18:42:04 +0900493func (j *Javadoc) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Nan Zhanga40da042018-08-01 12:48:00 -0700494 flags droiddocBuilderFlags) android.Paths {
Jiyong Park1e440682018-05-23 18:42:04 +0900495
496 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Crossc0806172019-06-14 18:51:47 -0700497 var aidlSrcs android.Paths
Jiyong Park1e440682018-05-23 18:42:04 +0900498
Jiyong Park1112c4c2019-08-16 21:12:10 +0900499 aidlIncludeFlags := genAidlIncludeFlags(srcFiles)
500
Jiyong Park1e440682018-05-23 18:42:04 +0900501 for _, srcFile := range srcFiles {
502 switch srcFile.Ext() {
503 case ".aidl":
Colin Crossc0806172019-06-14 18:51:47 -0700504 aidlSrcs = append(aidlSrcs, srcFile)
Jiyong Parkd90d7412019-08-20 22:49:19 +0900505 case ".logtags":
506 javaFile := genLogtags(ctx, srcFile)
507 outSrcFiles = append(outSrcFiles, javaFile)
Jiyong Park1e440682018-05-23 18:42:04 +0900508 default:
509 outSrcFiles = append(outSrcFiles, srcFile)
510 }
511 }
512
Colin Crossc0806172019-06-14 18:51:47 -0700513 // Process all aidl files together to support sharding them into one or more rules that produce srcjars.
514 if len(aidlSrcs) > 0 {
515 srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags+aidlIncludeFlags, flags.aidlDeps)
516 outSrcFiles = append(outSrcFiles, srcJarFiles...)
517 }
518
Jiyong Park1e440682018-05-23 18:42:04 +0900519 return outSrcFiles
520}
521
Nan Zhang581fd212018-01-10 16:06:12 -0800522func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
523 var deps deps
524
Colin Cross83bb3162018-06-25 15:48:06 -0700525 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800526 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700527 ctx.AddMissingDependencies(sdkDep.bootclasspath)
528 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Nan Zhang581fd212018-01-10 16:06:12 -0800529 } else if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700530 deps.bootClasspath = append(deps.bootClasspath, sdkDep.jars...)
Anton Hansson26bf49b2020-02-08 20:26:29 +0000531 deps.aidlPreprocess = sdkDep.aidl
532 } else {
533 deps.aidlPreprocess = sdkDep.aidl
Nan Zhang581fd212018-01-10 16:06:12 -0800534 }
535
536 ctx.VisitDirectDeps(func(module android.Module) {
537 otherName := ctx.OtherModuleName(module)
538 tag := ctx.OtherModuleDependencyTag(module)
539
Colin Cross2d24c1b2018-05-23 10:59:18 -0700540 switch tag {
541 case bootClasspathTag:
542 if dep, ok := module.(Dependency); ok {
Nan Zhang581fd212018-01-10 16:06:12 -0800543 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars()...)
Paul Duffin83a2d962019-11-19 19:44:10 +0000544 } else if sm, ok := module.(SystemModulesProvider); ok {
Paul Duffine25c6442019-10-11 13:50:28 +0100545 // A system modules dependency has been added to the bootclasspath
546 // so add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +0000547 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700548 } else {
549 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
550 }
551 case libTag:
552 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800553 case SdkLibraryDependency:
Paul Duffin649dadf2020-05-26 11:42:13 +0100554 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700555 case Dependency:
Sundong Ahnba493602018-11-20 17:36:35 +0900556 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park19a7f252019-07-10 16:59:31 +0900557 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700558 case android.SourceFileProducer:
Nan Zhang581fd212018-01-10 16:06:12 -0800559 checkProducesJars(ctx, dep)
560 deps.classpath = append(deps.classpath, dep.Srcs()...)
Nan Zhang581fd212018-01-10 16:06:12 -0800561 default:
562 ctx.ModuleErrorf("depends on non-java module %q", otherName)
563 }
Colin Cross6cef4812019-10-17 14:23:50 -0700564 case java9LibTag:
565 switch dep := module.(type) {
566 case Dependency:
567 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
568 default:
569 ctx.ModuleErrorf("depends on non-java module %q", otherName)
570 }
Nan Zhang357466b2018-04-17 17:38:36 -0700571 case systemModulesTag:
572 if deps.systemModules != nil {
573 panic("Found two system module dependencies")
574 }
Paul Duffin83a2d962019-11-19 19:44:10 +0000575 sm := module.(SystemModulesProvider)
576 outputDir, outputDeps := sm.OutputDirAndDeps()
577 deps.systemModules = &systemModules{outputDir, outputDeps}
Nan Zhang581fd212018-01-10 16:06:12 -0800578 }
579 })
580 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
581 // may contain filegroup or genrule.
Colin Cross8a497952019-03-05 22:25:09 -0800582 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400583 j.implicits = append(j.implicits, srcFiles...)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900584
585 filterByPackage := func(srcs []android.Path, filterPackages []string) []android.Path {
586 if filterPackages == nil {
587 return srcs
588 }
589 filtered := []android.Path{}
590 for _, src := range srcs {
591 if src.Ext() != ".java" {
592 // Don't filter-out non-Java (=generated sources) by package names. This is not ideal,
593 // but otherwise metalava emits stub sources having references to the generated AIDL classes
594 // in filtered-out pacages (e.g. com.android.internal.*).
595 // TODO(b/141149570) We need to fix this by introducing default private constructors or
596 // fixing metalava to not emit constructors having references to unknown classes.
597 filtered = append(filtered, src)
598 continue
599 }
600 packageName := strings.ReplaceAll(filepath.Dir(src.Rel()), "/", ".")
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800601 if android.HasAnyPrefix(packageName, filterPackages) {
602 filtered = append(filtered, src)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900603 }
604 }
605 return filtered
606 }
607 srcFiles = filterByPackage(srcFiles, j.properties.Filter_packages)
608
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400609 // While metalava needs package html files, it does not need them to be explicit on the command
610 // line. More importantly, the metalava rsp file is also used by the subsequent jdiff action if
611 // jdiff_enabled=true. javadoc complains if it receives html files on the command line. The filter
612 // below excludes html files from the rsp file for both metalava and jdiff. Note that the html
613 // files are still included as implicit inputs for successful remote execution and correct
614 // incremental builds.
615 filterHtml := func(srcs []android.Path) []android.Path {
616 filtered := []android.Path{}
617 for _, src := range srcs {
618 if src.Ext() == ".html" {
619 continue
620 }
621 filtered = append(filtered, src)
622 }
623 return filtered
624 }
625 srcFiles = filterHtml(srcFiles)
626
Nan Zhanga40da042018-08-01 12:48:00 -0700627 flags := j.collectAidlFlags(ctx, deps)
Jiyong Park1e440682018-05-23 18:42:04 +0900628 srcFiles = j.genSources(ctx, srcFiles, flags)
Nan Zhang581fd212018-01-10 16:06:12 -0800629
630 // srcs may depend on some genrule output.
631 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800632 j.srcJars = append(j.srcJars, deps.srcJars...)
633
Nan Zhang581fd212018-01-10 16:06:12 -0800634 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800635 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800636
Nan Zhang9c69a122018-08-22 10:22:08 -0700637 if j.properties.Local_sourcepaths == nil && len(j.srcFiles) > 0 {
Nan Zhang581fd212018-01-10 16:06:12 -0800638 j.properties.Local_sourcepaths = append(j.properties.Local_sourcepaths, ".")
639 }
640 j.sourcepaths = android.PathsForModuleSrc(ctx, j.properties.Local_sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800641
Colin Cross8a497952019-03-05 22:25:09 -0800642 j.argFiles = android.PathsForModuleSrc(ctx, j.properties.Arg_files)
Paul Duffin99e4a502019-02-11 15:38:42 +0000643 argFilesMap := map[string]string{}
644 argFileLabels := []string{}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700645
Paul Duffin99e4a502019-02-11 15:38:42 +0000646 for _, label := range j.properties.Arg_files {
Colin Cross8a497952019-03-05 22:25:09 -0800647 var paths = android.PathsForModuleSrc(ctx, []string{label})
Paul Duffin99e4a502019-02-11 15:38:42 +0000648 if _, exists := argFilesMap[label]; !exists {
649 argFilesMap[label] = strings.Join(paths.Strings(), " ")
650 argFileLabels = append(argFileLabels, label)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700651 } else {
652 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000653 label, argFilesMap[label], paths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700654 }
655 }
656
657 var err error
Colin Cross15638152019-07-11 11:11:35 -0700658 j.args, err = android.Expand(String(j.properties.Args), func(name string) (string, error) {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700659 if strings.HasPrefix(name, "location ") {
660 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Paul Duffin99e4a502019-02-11 15:38:42 +0000661 if paths, ok := argFilesMap[label]; ok {
Colin Cross15638152019-07-11 11:11:35 -0700662 return paths, nil
Nan Zhang1598a9e2018-09-04 17:14:32 -0700663 } else {
Colin Cross15638152019-07-11 11:11:35 -0700664 return "", fmt.Errorf("unknown location label %q, expecting one of %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000665 label, strings.Join(argFileLabels, ", "))
Nan Zhang1598a9e2018-09-04 17:14:32 -0700666 }
667 } else if name == "genDir" {
Colin Cross15638152019-07-11 11:11:35 -0700668 return android.PathForModuleGen(ctx).String(), nil
Nan Zhang1598a9e2018-09-04 17:14:32 -0700669 }
Colin Cross15638152019-07-11 11:11:35 -0700670 return "", fmt.Errorf("unknown variable '$(%s)'", name)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700671 })
672
673 if err != nil {
674 ctx.PropertyErrorf("args", "%s", err.Error())
675 }
676
Nan Zhang581fd212018-01-10 16:06:12 -0800677 return deps
678}
679
680func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
681 j.addDeps(ctx)
682}
683
684func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
685 deps := j.collectDeps(ctx)
686
Colin Crossdaa4c672019-07-15 22:53:46 -0700687 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800688
Colin Crossdaa4c672019-07-15 22:53:46 -0700689 outDir := android.PathForModuleOut(ctx, "out")
690 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
691
692 j.stubsSrcJar = nil
693
694 rule := android.NewRuleBuilder()
695
696 rule.Command().Text("rm -rf").Text(outDir.String())
697 rule.Command().Text("mkdir -p").Text(outDir.String())
698
699 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, j.srcJars)
Nan Zhang357466b2018-04-17 17:38:36 -0700700
Colin Cross83bb3162018-06-25 15:48:06 -0700701 javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800702
Colin Crossdaa4c672019-07-15 22:53:46 -0700703 cmd := javadocSystemModulesCmd(ctx, rule, j.srcFiles, outDir, srcJarDir, srcJarList,
704 deps.systemModules, deps.classpath, j.sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800705
Colin Cross1e743852019-10-28 11:37:20 -0700706 cmd.FlagWithArg("-source ", javaVersion.String()).
Colin Crossdaa4c672019-07-15 22:53:46 -0700707 Flag("-J-Xmx1024m").
708 Flag("-XDignore.symbol.file").
709 Flag("-Xdoclint:none")
Nan Zhang581fd212018-01-10 16:06:12 -0800710
Colin Crossdaa4c672019-07-15 22:53:46 -0700711 rule.Command().
712 BuiltTool(ctx, "soong_zip").
713 Flag("-write_if_changed").
714 Flag("-d").
715 FlagWithOutput("-o ", j.docZip).
716 FlagWithArg("-C ", outDir.String()).
717 FlagWithArg("-D ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700718
Colin Crossdaa4c672019-07-15 22:53:46 -0700719 rule.Restat()
720
721 zipSyncCleanupCmd(rule, srcJarDir)
722
723 rule.Build(pctx, ctx, "javadoc", "javadoc")
Nan Zhang581fd212018-01-10 16:06:12 -0800724}
725
Nan Zhanga40da042018-08-01 12:48:00 -0700726//
727// Droiddoc
728//
729type Droiddoc struct {
730 Javadoc
731
732 properties DroiddocProperties
733 apiFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700734 privateApiFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700735 removedApiFile android.WritablePath
736 removedDexApiFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700737
738 checkCurrentApiTimestamp android.WritablePath
739 updateCurrentApiTimestamp android.WritablePath
740 checkLastReleasedApiTimestamp android.WritablePath
741
Nan Zhanga40da042018-08-01 12:48:00 -0700742 apiFilePath android.Path
743}
744
Colin Crossa3002fc2019-07-08 16:48:04 -0700745// droiddoc converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700746func DroiddocFactory() android.Module {
747 module := &Droiddoc{}
748
749 module.AddProperties(&module.properties,
750 &module.Javadoc.properties)
751
752 InitDroiddocModule(module, android.HostAndDeviceSupported)
753 return module
754}
755
Colin Crossa3002fc2019-07-08 16:48:04 -0700756// droiddoc_host converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700757func DroiddocHostFactory() android.Module {
758 module := &Droiddoc{}
759
760 module.AddProperties(&module.properties,
761 &module.Javadoc.properties)
762
763 InitDroiddocModule(module, android.HostSupported)
764 return module
765}
766
767func (d *Droiddoc) ApiFilePath() android.Path {
768 return d.apiFilePath
769}
770
Nan Zhang581fd212018-01-10 16:06:12 -0800771func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
772 d.Javadoc.addDeps(ctx)
773
Inseob Kim38449af2019-02-28 14:24:05 +0900774 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
775 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
776 }
777
Nan Zhang79614d12018-04-19 18:03:39 -0700778 if String(d.properties.Custom_template) != "" {
Dan Willemsencc090972018-02-26 14:33:31 -0800779 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
780 }
Nan Zhang581fd212018-01-10 16:06:12 -0800781}
782
Colin Crossab054432019-07-15 16:13:59 -0700783func (d *Droiddoc) doclavaDocsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, docletPath classpath) {
Colin Cross2a2e0db2020-02-21 16:55:46 -0800784 buildNumberFile := ctx.Config().BuildNumberFile(ctx)
Nan Zhang443fa522018-08-20 20:58:28 -0700785 // Droiddoc always gets "-source 1.8" because it doesn't support 1.9 sources. For modules with 1.9
786 // sources, droiddoc will get sources produced by metalava which will have already stripped out the
787 // 1.9 language features.
Colin Crossab054432019-07-15 16:13:59 -0700788 cmd.FlagWithArg("-source ", "1.8").
789 Flag("-J-Xmx1600m").
790 Flag("-J-XX:-OmitStackTraceInFastThrow").
791 Flag("-XDignore.symbol.file").
792 FlagWithArg("-doclet ", "com.google.doclava.Doclava").
793 FlagWithInputList("-docletpath ", docletPath.Paths(), ":").
Colin Cross2a2e0db2020-02-21 16:55:46 -0800794 FlagWithArg("-hdf page.build ", ctx.Config().BuildId()+"-$(cat "+buildNumberFile.String()+")").OrderOnly(buildNumberFile).
Elliott Hughes26bce342019-09-12 15:05:13 -0700795 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 -0700796
Nan Zhanga40da042018-08-01 12:48:00 -0700797 if String(d.properties.Custom_template) == "" {
798 // TODO: This is almost always droiddoc-templates-sdk
799 ctx.PropertyErrorf("custom_template", "must specify a template")
800 }
801
802 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
Nan Zhangf4936b02018-08-01 15:00:28 -0700803 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Crossab054432019-07-15 16:13:59 -0700804 cmd.FlagWithArg("-templatedir ", t.dir.String()).Implicits(t.deps)
Nan Zhanga40da042018-08-01 12:48:00 -0700805 } else {
Paul Duffin884363e2019-12-19 10:21:09 +0000806 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_exported_dir", ctx.OtherModuleName(m))
Nan Zhanga40da042018-08-01 12:48:00 -0700807 }
808 })
809
810 if len(d.properties.Html_dirs) > 0 {
Colin Crossab054432019-07-15 16:13:59 -0700811 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
812 cmd.FlagWithArg("-htmldir ", htmlDir.String()).
813 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[0], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700814 }
815
816 if len(d.properties.Html_dirs) > 1 {
Colin Crossab054432019-07-15 16:13:59 -0700817 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
818 cmd.FlagWithArg("-htmldir2 ", htmlDir2.String()).
819 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[1], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700820 }
821
822 if len(d.properties.Html_dirs) > 2 {
823 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
824 }
825
Colin Cross8a497952019-03-05 22:25:09 -0800826 knownTags := android.PathsForModuleSrc(ctx, d.properties.Knowntags)
Colin Crossab054432019-07-15 16:13:59 -0700827 cmd.FlagForEachInput("-knowntags ", knownTags)
Nan Zhanga40da042018-08-01 12:48:00 -0700828
Colin Crossab054432019-07-15 16:13:59 -0700829 cmd.FlagForEachArg("-hdf ", d.properties.Hdf)
Nan Zhanga40da042018-08-01 12:48:00 -0700830
831 if String(d.properties.Proofread_file) != "" {
832 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
Colin Crossab054432019-07-15 16:13:59 -0700833 cmd.FlagWithOutput("-proofread ", proofreadFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700834 }
835
836 if String(d.properties.Todo_file) != "" {
837 // tricky part:
838 // we should not compute full path for todo_file through PathForModuleOut().
839 // the non-standard doclet will get the full path relative to "-o".
Colin Crossab054432019-07-15 16:13:59 -0700840 cmd.FlagWithArg("-todo ", String(d.properties.Todo_file)).
841 ImplicitOutput(android.PathForModuleOut(ctx, String(d.properties.Todo_file)))
Nan Zhanga40da042018-08-01 12:48:00 -0700842 }
843
844 if String(d.properties.Resourcesdir) != "" {
845 // TODO: should we add files under resourcesDir to the implicits? It seems that
846 // resourcesDir is one sub dir of htmlDir
847 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
Colin Crossab054432019-07-15 16:13:59 -0700848 cmd.FlagWithArg("-resourcesdir ", resourcesDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700849 }
850
851 if String(d.properties.Resourcesoutdir) != "" {
852 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
Colin Crossab054432019-07-15 16:13:59 -0700853 cmd.FlagWithArg("-resourcesoutdir ", String(d.properties.Resourcesoutdir))
Nan Zhanga40da042018-08-01 12:48:00 -0700854 }
Nan Zhanga40da042018-08-01 12:48:00 -0700855}
856
Colin Crossab054432019-07-15 16:13:59 -0700857func (d *Droiddoc) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.WritablePath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200858 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
859 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -0700860 String(d.properties.Api_filename) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700861
Nan Zhanga40da042018-08-01 12:48:00 -0700862 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Colin Crossab054432019-07-15 16:13:59 -0700863 cmd.FlagWithOutput("-api ", d.apiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700864 d.apiFilePath = d.apiFile
865 }
866
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200867 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
868 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -0700869 String(d.properties.Removed_api_filename) != "" {
Nan Zhanga40da042018-08-01 12:48:00 -0700870 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
Colin Crossab054432019-07-15 16:13:59 -0700871 cmd.FlagWithOutput("-removedApi ", d.removedApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700872 }
873
Nan Zhanga40da042018-08-01 12:48:00 -0700874 if String(d.properties.Removed_dex_api_filename) != "" {
875 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Crossab054432019-07-15 16:13:59 -0700876 cmd.FlagWithOutput("-removedDexApi ", d.removedDexApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700877 }
878
Nan Zhanga40da042018-08-01 12:48:00 -0700879 if BoolDefault(d.properties.Create_stubs, true) {
Colin Crossab054432019-07-15 16:13:59 -0700880 cmd.FlagWithArg("-stubs ", stubsDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700881 }
882
883 if Bool(d.properties.Write_sdk_values) {
Colin Crossab054432019-07-15 16:13:59 -0700884 cmd.FlagWithArg("-sdkvalues ", android.PathForModuleOut(ctx, "out").String())
Nan Zhanga40da042018-08-01 12:48:00 -0700885 }
Nan Zhanga40da042018-08-01 12:48:00 -0700886}
887
Colin Crossab054432019-07-15 16:13:59 -0700888func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700889 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700890 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
891 rule.Command().Text("cp").
892 Input(staticDocIndexRedirect).
893 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700894 }
895
896 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700897 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
898 rule.Command().Text("cp").
899 Input(staticDocProperties).
900 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700901 }
Nan Zhanga40da042018-08-01 12:48:00 -0700902}
903
Colin Crossab054432019-07-15 16:13:59 -0700904func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700905 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700906
907 cmd := rule.Command().
908 BuiltTool(ctx, "soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
909 Flag(config.JavacVmFlags).
910 FlagWithArg("-encoding ", "UTF-8").
Colin Crossab054432019-07-15 16:13:59 -0700911 FlagWithRspFileInputList("@", srcs).
912 FlagWithInput("@", srcJarList)
913
Colin Crossab054432019-07-15 16:13:59 -0700914 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
915 // based stubs generation.
916 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
917 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
918 // the correct package name base path.
919 if len(sourcepaths) > 0 {
920 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
921 } else {
922 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
923 }
924
925 cmd.FlagWithArg("-d ", outDir.String()).
926 Flag("-quiet")
927
928 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700929}
930
Colin Crossdaa4c672019-07-15 22:53:46 -0700931func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
932 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
933 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
934
935 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
936
937 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
938 cmd.Flag(flag).Implicits(deps)
939
940 cmd.FlagWithArg("--patch-module ", "java.base=.")
941
942 if len(classpath) > 0 {
943 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
944 }
945
946 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700947}
948
Colin Crossdaa4c672019-07-15 22:53:46 -0700949func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
950 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
951 sourcepaths android.Paths) *android.RuleBuilderCommand {
952
953 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
954
955 if len(bootclasspath) == 0 && ctx.Device() {
956 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
957 // ensure java does not fall back to the default bootclasspath.
958 cmd.FlagWithArg("-bootclasspath ", `""`)
959 } else if len(bootclasspath) > 0 {
960 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
961 }
962
963 if len(classpath) > 0 {
964 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
965 }
966
967 return cmd
968}
969
Colin Crossab054432019-07-15 16:13:59 -0700970func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
971 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700972
Colin Crossab054432019-07-15 16:13:59 -0700973 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
974 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
975
976 return rule.Command().
977 BuiltTool(ctx, "dokka").
978 Flag(config.JavacVmFlags).
979 Flag(srcJarDir.String()).
980 FlagWithInputList("-classpath ", dokkaClasspath, ":").
981 FlagWithArg("-format ", "dac").
982 FlagWithArg("-dacRoot ", "/reference/kotlin").
983 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700984}
985
986func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
987 deps := d.Javadoc.collectDeps(ctx)
988
Colin Crossdaa4c672019-07-15 22:53:46 -0700989 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
990 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
991
Nan Zhang1598a9e2018-09-04 17:14:32 -0700992 jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
993 doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
994 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
995 checkApiClasspath := classpath{jsilver, doclava, android.PathForSource(ctx, java8Home, "lib/tools.jar")}
996
Colin Crossab054432019-07-15 16:13:59 -0700997 outDir := android.PathForModuleOut(ctx, "out")
998 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
999 stubsDir := android.PathForModuleOut(ctx, "stubsDir")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001000
Colin Crossab054432019-07-15 16:13:59 -07001001 rule := android.NewRuleBuilder()
Nan Zhang1598a9e2018-09-04 17:14:32 -07001002
Colin Crossab054432019-07-15 16:13:59 -07001003 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
1004 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -07001005
Colin Crossab054432019-07-15 16:13:59 -07001006 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1007
1008 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -07001009 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -07001010 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001011 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -07001012 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -07001013 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001014 }
1015
Colin Crossab054432019-07-15 16:13:59 -07001016 d.stubsFlags(ctx, cmd, stubsDir)
1017
1018 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1019
Mathew Inwoodabd49ab2019-12-19 14:27:08 +00001020 if d.properties.Compat_config != nil {
1021 compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
1022 cmd.FlagWithInput("-compatconfig ", compatConfig)
1023 }
1024
Colin Crossab054432019-07-15 16:13:59 -07001025 var desc string
1026 if Bool(d.properties.Dokka_enabled) {
1027 desc = "dokka"
1028 } else {
1029 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
1030
1031 for _, o := range d.Javadoc.properties.Out {
1032 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1033 }
1034
1035 d.postDoclavaCmds(ctx, rule)
1036 desc = "doclava"
1037 }
1038
1039 rule.Command().
1040 BuiltTool(ctx, "soong_zip").
1041 Flag("-write_if_changed").
1042 Flag("-d").
1043 FlagWithOutput("-o ", d.docZip).
1044 FlagWithArg("-C ", outDir.String()).
1045 FlagWithArg("-D ", outDir.String())
1046
1047 rule.Command().
1048 BuiltTool(ctx, "soong_zip").
1049 Flag("-write_if_changed").
1050 Flag("-jar").
1051 FlagWithOutput("-o ", d.stubsSrcJar).
1052 FlagWithArg("-C ", stubsDir.String()).
1053 FlagWithArg("-D ", stubsDir.String())
1054
1055 rule.Restat()
1056
1057 zipSyncCleanupCmd(rule, srcJarDir)
1058
1059 rule.Build(pctx, ctx, "javadoc", desc)
1060
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001061 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001062 !ctx.Config().IsPdkBuild() {
Colin Crossab054432019-07-15 16:13:59 -07001063
1064 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1065 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Nan Zhang1598a9e2018-09-04 17:14:32 -07001066
1067 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001068
1069 rule := android.NewRuleBuilder()
1070
1071 rule.Command().Text("( true")
1072
1073 rule.Command().
1074 BuiltTool(ctx, "apicheck").
1075 Flag("-JXmx1024m").
1076 FlagWithInputList("-Jclasspath\\ ", checkApiClasspath.Paths(), ":").
1077 OptionalFlag(d.properties.Check_api.Current.Args).
1078 Input(apiFile).
1079 Input(d.apiFile).
1080 Input(removedApiFile).
1081 Input(d.removedApiFile)
1082
1083 msg := fmt.Sprintf(`\n******************************\n`+
1084 `You have tried to change the API from what has been previously approved.\n\n`+
1085 `To make these errors go away, you have two choices:\n`+
1086 ` 1. You can add '@hide' javadoc comments to the methods, etc. listed in the\n`+
1087 ` errors above.\n\n`+
1088 ` 2. You can update current.txt by executing the following command:\n`+
1089 ` make %s-update-current-api\n\n`+
1090 ` To submit the revised current.txt to the main Android repository,\n`+
1091 ` you will need approval.\n`+
1092 `******************************\n`, ctx.ModuleName())
1093
1094 rule.Command().
1095 Text("touch").Output(d.checkCurrentApiTimestamp).
1096 Text(") || (").
1097 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1098 Text("; exit 38").
1099 Text(")")
1100
1101 rule.Build(pctx, ctx, "doclavaCurrentApiCheck", "check current API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001102
1103 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001104
1105 // update API rule
1106 rule = android.NewRuleBuilder()
1107
1108 rule.Command().Text("( true")
1109
1110 rule.Command().
1111 Text("cp").Flag("-f").
1112 Input(d.apiFile).Flag(apiFile.String())
1113
1114 rule.Command().
1115 Text("cp").Flag("-f").
1116 Input(d.removedApiFile).Flag(removedApiFile.String())
1117
1118 msg = "failed to update public API"
1119
1120 rule.Command().
1121 Text("touch").Output(d.updateCurrentApiTimestamp).
1122 Text(") || (").
1123 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1124 Text("; exit 38").
1125 Text(")")
1126
1127 rule.Build(pctx, ctx, "doclavaCurrentApiUpdate", "update current API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001128 }
1129
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001130 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001131 !ctx.Config().IsPdkBuild() {
Colin Crossab054432019-07-15 16:13:59 -07001132
1133 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1134 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
Nan Zhang1598a9e2018-09-04 17:14:32 -07001135
1136 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001137
1138 rule := android.NewRuleBuilder()
1139
1140 rule.Command().
1141 Text("(").
1142 BuiltTool(ctx, "apicheck").
1143 Flag("-JXmx1024m").
1144 FlagWithInputList("-Jclasspath\\ ", checkApiClasspath.Paths(), ":").
1145 OptionalFlag(d.properties.Check_api.Last_released.Args).
1146 Input(apiFile).
1147 Input(d.apiFile).
1148 Input(removedApiFile).
1149 Input(d.removedApiFile)
1150
1151 msg := `\n******************************\n` +
1152 `You have tried to change the API from what has been previously released in\n` +
1153 `an SDK. Please fix the errors listed above.\n` +
1154 `******************************\n`
1155
1156 rule.Command().
1157 Text("touch").Output(d.checkLastReleasedApiTimestamp).
1158 Text(") || (").
1159 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1160 Text("; exit 38").
1161 Text(")")
1162
1163 rule.Build(pctx, ctx, "doclavaLastApiCheck", "check last API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001164 }
1165}
1166
1167//
1168// Droidstubs
1169//
1170type Droidstubs struct {
1171 Javadoc
Paul Duffin91547182019-11-12 19:39:36 +00001172 android.SdkBase
Nan Zhang1598a9e2018-09-04 17:14:32 -07001173
Pete Gillin581d6082018-10-22 15:55:04 +01001174 properties DroidstubsProperties
1175 apiFile android.WritablePath
1176 apiXmlFile android.WritablePath
1177 lastReleasedApiXmlFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001178 privateApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001179 removedApiFile android.WritablePath
1180 removedDexApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001181 nullabilityWarningsFile android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001182
1183 checkCurrentApiTimestamp android.WritablePath
1184 updateCurrentApiTimestamp android.WritablePath
1185 checkLastReleasedApiTimestamp android.WritablePath
Adrian Roos075eedc2019-10-10 12:07:03 +02001186 apiLintTimestamp android.WritablePath
Adrian Roos3b8f1cd2019-11-01 13:42:39 +01001187 apiLintReport android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001188
Pete Gillin581d6082018-10-22 15:55:04 +01001189 checkNullabilityWarningsTimestamp android.WritablePath
1190
Nan Zhang1598a9e2018-09-04 17:14:32 -07001191 annotationsZip android.WritablePath
Nan Zhang9c69a122018-08-22 10:22:08 -07001192 apiVersionsXml android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001193
1194 apiFilePath android.Path
Nan Zhang71bbe632018-09-17 14:32:21 -07001195
1196 jdiffDocZip android.WritablePath
1197 jdiffStubsSrcJar android.WritablePath
Jerome Gaillard0f599032019-10-10 19:29:11 +01001198
1199 metadataZip android.WritablePath
1200 metadataDir android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001201}
1202
Colin Crossa3002fc2019-07-08 16:48:04 -07001203// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
1204// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to
1205// a droiddoc module to generate documentation.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001206func DroidstubsFactory() android.Module {
1207 module := &Droidstubs{}
1208
1209 module.AddProperties(&module.properties,
1210 &module.Javadoc.properties)
1211
1212 InitDroiddocModule(module, android.HostAndDeviceSupported)
Paul Duffin91547182019-11-12 19:39:36 +00001213 android.InitSdkAwareModule(module)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001214 return module
1215}
1216
Colin Crossa3002fc2019-07-08 16:48:04 -07001217// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API
1218// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be
1219// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs
1220// module when symbols needed by the source files are provided by java_library_host modules.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001221func DroidstubsHostFactory() android.Module {
1222 module := &Droidstubs{}
1223
1224 module.AddProperties(&module.properties,
1225 &module.Javadoc.properties)
1226
1227 InitDroiddocModule(module, android.HostSupported)
1228 return module
1229}
1230
Colin Cross014489c2020-06-02 20:09:13 -07001231func (d *Droidstubs) OutputFiles(tag string) (android.Paths, error) {
1232 switch tag {
1233 case "":
1234 return android.Paths{d.stubsSrcJar}, nil
1235 case ".docs.zip":
1236 return android.Paths{d.docZip}, nil
1237 case ".annotations.zip":
1238 return android.Paths{d.annotationsZip}, nil
1239 case ".api_versions.xml":
1240 return android.Paths{d.apiVersionsXml}, nil
1241 default:
1242 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1243 }
1244}
1245
Nan Zhang1598a9e2018-09-04 17:14:32 -07001246func (d *Droidstubs) ApiFilePath() android.Path {
1247 return d.apiFilePath
1248}
1249
Paul Duffin1fd005d2020-04-09 01:08:11 +01001250func (d *Droidstubs) RemovedApiFilePath() android.Path {
1251 return d.removedApiFile
1252}
1253
Paul Duffin3d1248c2020-04-09 00:10:17 +01001254func (d *Droidstubs) StubsSrcJar() android.Path {
1255 return d.stubsSrcJar
1256}
1257
Nan Zhang1598a9e2018-09-04 17:14:32 -07001258func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
1259 d.Javadoc.addDeps(ctx)
1260
Paul Duffin160fe412020-05-10 19:32:20 +01001261 // If requested clear any properties that provide information about the latest version
1262 // of an API and which reference non-existent modules.
Inseob Kim38449af2019-02-28 14:24:05 +09001263 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
1264 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
Paul Duffin160fe412020-05-10 19:32:20 +01001265
1266 // If the new_since references a module, e.g. :module-latest-api and the module
1267 // does not exist then clear it.
1268 newSinceSrc := d.properties.Check_api.Api_lint.New_since
1269 newSinceSrcModule := android.SrcIsModule(proptools.String(newSinceSrc))
1270 if newSinceSrcModule != "" && !ctx.OtherModuleExists(newSinceSrcModule) {
1271 d.properties.Check_api.Api_lint.New_since = nil
1272 }
Inseob Kim38449af2019-02-28 14:24:05 +09001273 }
1274
Nan Zhang1598a9e2018-09-04 17:14:32 -07001275 if len(d.properties.Merge_annotations_dirs) != 0 {
1276 for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
1277 ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
1278 }
1279 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001280
Pete Gillin77167902018-09-19 18:16:26 +01001281 if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
1282 for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
1283 ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
1284 }
1285 }
1286
Nan Zhang9c69a122018-08-22 10:22:08 -07001287 if len(d.properties.Api_levels_annotations_dirs) != 0 {
1288 for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
1289 ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
1290 }
1291 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001292}
1293
Paul Duffin3ae29512020-04-08 18:18:03 +01001294func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001295 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1296 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001297 String(d.properties.Api_filename) != "" {
1298 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001299 cmd.FlagWithOutput("--api ", d.apiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001300 d.apiFilePath = d.apiFile
1301 }
1302
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001303 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1304 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001305 String(d.properties.Removed_api_filename) != "" {
1306 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001307 cmd.FlagWithOutput("--removed-api ", d.removedApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001308 }
1309
Nan Zhang1598a9e2018-09-04 17:14:32 -07001310 if String(d.properties.Removed_dex_api_filename) != "" {
1311 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001312 cmd.FlagWithOutput("--removed-dex-api ", d.removedDexApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001313 }
1314
Nan Zhang9c69a122018-08-22 10:22:08 -07001315 if Bool(d.properties.Write_sdk_values) {
Jerome Gaillard0f599032019-10-10 19:29:11 +01001316 d.metadataDir = android.PathForModuleOut(ctx, "metadata")
1317 cmd.FlagWithArg("--sdk-values ", d.metadataDir.String())
Nan Zhang9c69a122018-08-22 10:22:08 -07001318 }
1319
Paul Duffin3ae29512020-04-08 18:18:03 +01001320 if stubsDir.Valid() {
1321 if Bool(d.properties.Create_doc_stubs) {
1322 cmd.FlagWithArg("--doc-stubs ", stubsDir.String())
1323 } else {
1324 cmd.FlagWithArg("--stubs ", stubsDir.String())
1325 cmd.Flag("--exclude-documentation-from-stubs")
1326 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001327 }
Makoto Onuki1d5b7132020-06-16 14:41:10 -07001328 cmd.FlagWithArg("--hide ", "ShowingMemberInHiddenClass") // b/159121253 -- remove it once all the violations are fixed.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001329}
1330
Colin Cross33961b52019-07-11 11:01:22 -07001331func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang1598a9e2018-09-04 17:14:32 -07001332 if Bool(d.properties.Annotations_enabled) {
Colin Cross33961b52019-07-11 11:01:22 -07001333 cmd.Flag("--include-annotations")
1334
Pete Gillinc382a562018-11-14 18:45:46 +00001335 validatingNullability :=
1336 strings.Contains(d.Javadoc.args, "--validate-nullability-from-merged-stubs") ||
1337 String(d.properties.Validate_nullability_from_list) != ""
Paul Duffin13a9dd62019-11-04 10:26:47 +00001338
Pete Gillina262c052018-09-14 14:25:48 +01001339 migratingNullability := String(d.properties.Previous_api) != ""
Pete Gillina262c052018-09-14 14:25:48 +01001340 if migratingNullability {
Colin Cross8a497952019-03-05 22:25:09 -08001341 previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
Colin Cross33961b52019-07-11 11:01:22 -07001342 cmd.FlagWithInput("--migrate-nullness ", previousApi)
Pete Gillina262c052018-09-14 14:25:48 +01001343 }
Colin Cross33961b52019-07-11 11:01:22 -07001344
Pete Gillinc382a562018-11-14 18:45:46 +00001345 if s := String(d.properties.Validate_nullability_from_list); s != "" {
Colin Cross33961b52019-07-11 11:01:22 -07001346 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
Pete Gillinc382a562018-11-14 18:45:46 +00001347 }
Colin Cross33961b52019-07-11 11:01:22 -07001348
Pete Gillina262c052018-09-14 14:25:48 +01001349 if validatingNullability {
Pete Gillin581d6082018-10-22 15:55:04 +01001350 d.nullabilityWarningsFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_nullability_warnings.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001351 cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile)
Pete Gillina262c052018-09-14 14:25:48 +01001352 }
Nan Zhanga40da042018-08-01 12:48:00 -07001353
1354 d.annotationsZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"_annotations.zip")
Colin Cross33961b52019-07-11 11:01:22 -07001355 cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip)
Nan Zhangf4936b02018-08-01 15:00:28 -07001356
Anton Hansson9d7c3fb2020-05-21 10:11:31 +01001357 if len(d.properties.Merge_annotations_dirs) != 0 {
1358 d.mergeAnnoDirFlags(ctx, cmd)
Nan Zhanga40da042018-08-01 12:48:00 -07001359 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001360
Colin Cross33961b52019-07-11 11:01:22 -07001361 // TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
1362 cmd.FlagWithArg("--hide ", "HiddenTypedefConstant").
1363 FlagWithArg("--hide ", "SuperfluousPrefix").
1364 FlagWithArg("--hide ", "AnnotationExtraction")
1365 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001366}
1367
Colin Cross33961b52019-07-11 11:01:22 -07001368func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
1369 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
1370 if t, ok := m.(*ExportedDroiddocDir); ok {
1371 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
1372 } else {
1373 ctx.PropertyErrorf("merge_annotations_dirs",
1374 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1375 }
1376 })
1377}
1378
1379func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Pete Gillin77167902018-09-19 18:16:26 +01001380 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
1381 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Cross33961b52019-07-11 11:01:22 -07001382 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
Pete Gillin77167902018-09-19 18:16:26 +01001383 } else {
1384 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
1385 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1386 }
1387 })
Nan Zhanga40da042018-08-01 12:48:00 -07001388}
1389
Colin Cross33961b52019-07-11 11:01:22 -07001390func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang9c69a122018-08-22 10:22:08 -07001391 if Bool(d.properties.Api_levels_annotations_enabled) {
1392 d.apiVersionsXml = android.PathForModuleOut(ctx, "api-versions.xml")
Nan Zhang9c69a122018-08-22 10:22:08 -07001393
1394 if len(d.properties.Api_levels_annotations_dirs) == 0 {
1395 ctx.PropertyErrorf("api_levels_annotations_dirs",
1396 "has to be non-empty if api levels annotations was enabled!")
1397 }
1398
Colin Cross33961b52019-07-11 11:01:22 -07001399 cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml)
1400 cmd.FlagWithInput("--apply-api-levels ", d.apiVersionsXml)
1401 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion())
1402 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
Nan Zhang9c69a122018-08-22 10:22:08 -07001403
1404 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
1405 if t, ok := m.(*ExportedDroiddocDir); ok {
Nan Zhang9c69a122018-08-22 10:22:08 -07001406 for _, dep := range t.deps {
1407 if strings.HasSuffix(dep.String(), "android.jar") {
Colin Cross33961b52019-07-11 11:01:22 -07001408 cmd.Implicit(dep)
Nan Zhang9c69a122018-08-22 10:22:08 -07001409 }
1410 }
Colin Cross33961b52019-07-11 11:01:22 -07001411 cmd.FlagWithArg("--android-jar-pattern ", t.dir.String()+"/%/public/android.jar")
Nan Zhang9c69a122018-08-22 10:22:08 -07001412 } else {
1413 ctx.PropertyErrorf("api_levels_annotations_dirs",
1414 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
1415 }
1416 })
1417
1418 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001419}
1420
Colin Cross33961b52019-07-11 11:01:22 -07001421func (d *Droidstubs) apiToXmlFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang71bbe632018-09-17 14:32:21 -07001422 if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() {
1423 if d.apiFile.String() == "" {
1424 ctx.ModuleErrorf("API signature file has to be specified in Metalava when jdiff is enabled.")
1425 }
1426
1427 d.apiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001428 cmd.FlagWithOutput("--api-xml ", d.apiXmlFile)
Nan Zhang71bbe632018-09-17 14:32:21 -07001429
1430 if String(d.properties.Check_api.Last_released.Api_file) == "" {
1431 ctx.PropertyErrorf("check_api.last_released.api_file",
1432 "has to be non-empty if jdiff was enabled!")
1433 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001434
Colin Cross33961b52019-07-11 11:01:22 -07001435 lastReleasedApi := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
Nan Zhang71bbe632018-09-17 14:32:21 -07001436 d.lastReleasedApiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_last_released_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001437 cmd.FlagWithInput("--convert-to-jdiff ", lastReleasedApi).Output(d.lastReleasedApiXmlFile)
1438 }
1439}
Nan Zhang71bbe632018-09-17 14:32:21 -07001440
Colin Cross1e743852019-10-28 11:37:20 -07001441func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths,
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001442 srcJarList android.Path, bootclasspath, classpath classpath, sourcepaths android.Paths, implicitsRsp android.WritablePath, sandbox bool) *android.RuleBuilderCommand {
Colin Cross8b8bec32019-11-15 13:18:43 -08001443 // Metalava uses lots of memory, restrict the number of metalava jobs that can run in parallel.
1444 rule.HighMem()
Ramy Medhat427683c2020-04-30 03:08:37 -04001445 cmd := rule.Command()
1446 if ctx.Config().IsEnvTrue("RBE_METALAVA") {
1447 rule.Remoteable(android.RemoteRuleSupports{RBE: true})
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001448 pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "metalava")
1449 execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
1450 labels := map[string]string{"type": "compile", "lang": "java", "compiler": "metalava"}
1451 if !sandbox {
1452 execStrategy = remoteexec.LocalExecStrategy
1453 labels["shallow"] = "true"
Ramy Medhat427683c2020-04-30 03:08:37 -04001454 }
1455 inputs := []string{android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "metalava.jar").String()}
1456 if v := ctx.Config().Getenv("RBE_METALAVA_INPUTS"); v != "" {
1457 inputs = append(inputs, strings.Split(v, ",")...)
1458 }
1459 cmd.Text((&remoteexec.REParams{
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001460 Labels: labels,
Ramy Medhat427683c2020-04-30 03:08:37 -04001461 ExecStrategy: execStrategy,
1462 Inputs: inputs,
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001463 RSPFile: implicitsRsp.String(),
Ramy Medhat427683c2020-04-30 03:08:37 -04001464 ToolchainInputs: []string{config.JavaCmd(ctx).String()},
1465 Platform: map[string]string{remoteexec.PoolKey: pool},
1466 }).NoVarTemplate(ctx.Config()))
1467 }
1468
1469 cmd.BuiltTool(ctx, "metalava").
Colin Cross33961b52019-07-11 11:01:22 -07001470 Flag(config.JavacVmFlags).
1471 FlagWithArg("-encoding ", "UTF-8").
Colin Cross1e743852019-10-28 11:37:20 -07001472 FlagWithArg("-source ", javaVersion.String()).
Colin Cross33961b52019-07-11 11:01:22 -07001473 FlagWithRspFileInputList("@", srcs).
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001474 FlagWithInput("@", srcJarList)
1475
1476 if javaHome := ctx.Config().Getenv("ANDROID_JAVA_HOME"); javaHome != "" {
1477 cmd.Implicit(android.PathForSource(ctx, javaHome))
1478 }
1479
1480 if sandbox {
1481 cmd.FlagWithOutput("--strict-input-files ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
1482 } else {
1483 cmd.FlagWithOutput("--strict-input-files:warn ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
1484 }
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001485
Colin Cross238c1f32020-06-07 16:58:18 -07001486 if implicitsRsp != nil {
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001487 cmd.FlagWithArg("--strict-input-files-exempt ", "@"+implicitsRsp.String())
1488 }
Colin Cross33961b52019-07-11 11:01:22 -07001489
1490 if len(bootclasspath) > 0 {
1491 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
Nan Zhang71bbe632018-09-17 14:32:21 -07001492 }
1493
Colin Cross33961b52019-07-11 11:01:22 -07001494 if len(classpath) > 0 {
1495 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
1496 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001497
Colin Cross33961b52019-07-11 11:01:22 -07001498 if len(sourcepaths) > 0 {
1499 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
1500 } else {
1501 cmd.FlagWithArg("-sourcepath ", `""`)
1502 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001503
Colin Cross33961b52019-07-11 11:01:22 -07001504 cmd.Flag("--no-banner").
1505 Flag("--color").
1506 Flag("--quiet").
1507 Flag("--format=v2")
Nan Zhang86d2d552018-08-09 15:33:27 -07001508
Colin Cross33961b52019-07-11 11:01:22 -07001509 return cmd
Nan Zhang71bbe632018-09-17 14:32:21 -07001510}
1511
Nan Zhang1598a9e2018-09-04 17:14:32 -07001512func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga40da042018-08-01 12:48:00 -07001513 deps := d.Javadoc.collectDeps(ctx)
1514
1515 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), sdkContext(d))
Nan Zhang581fd212018-01-10 16:06:12 -08001516
Colin Cross33961b52019-07-11 11:01:22 -07001517 // Create rule for metalava
Nan Zhanga40da042018-08-01 12:48:00 -07001518
Colin Cross33961b52019-07-11 11:01:22 -07001519 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang71bbe632018-09-17 14:32:21 -07001520
Colin Cross33961b52019-07-11 11:01:22 -07001521 rule := android.NewRuleBuilder()
Nan Zhanga40da042018-08-01 12:48:00 -07001522
Paul Duffin3ae29512020-04-08 18:18:03 +01001523 generateStubs := BoolDefault(d.properties.Generate_stubs, true)
1524 var stubsDir android.OptionalPath
1525 if generateStubs {
1526 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1527 stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "stubsDir"))
1528 rule.Command().Text("rm -rf").Text(stubsDir.String())
1529 rule.Command().Text("mkdir -p").Text(stubsDir.String())
1530 }
Nan Zhanga40da042018-08-01 12:48:00 -07001531
Colin Cross33961b52019-07-11 11:01:22 -07001532 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1533
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001534 implicitsRsp := android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"implicits.rsp")
1535
Colin Cross33961b52019-07-11 11:01:22 -07001536 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001537 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths, implicitsRsp,
1538 Bool(d.Javadoc.properties.Sandbox))
1539 cmd.Implicits(d.Javadoc.implicits)
Colin Cross33961b52019-07-11 11:01:22 -07001540
1541 d.stubsFlags(ctx, cmd, stubsDir)
1542
1543 d.annotationsFlags(ctx, cmd)
1544 d.inclusionAnnotationsFlags(ctx, cmd)
1545 d.apiLevelsAnnotationsFlags(ctx, cmd)
1546 d.apiToXmlFlags(ctx, cmd)
Nan Zhang71bbe632018-09-17 14:32:21 -07001547
Nan Zhang1598a9e2018-09-04 17:14:32 -07001548 if strings.Contains(d.Javadoc.args, "--generate-documentation") {
1549 // Currently Metalava have the ability to invoke Javadoc in a seperate process.
1550 // Pass "-nodocs" to suppress the Javadoc invocation when Metalava receives
1551 // "--generate-documentation" arg. This is not needed when Metalava removes this feature.
1552 d.Javadoc.args = d.Javadoc.args + " -nodocs "
Nan Zhang79614d12018-04-19 18:03:39 -07001553 }
Colin Cross33961b52019-07-11 11:01:22 -07001554
1555 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1556 for _, o := range d.Javadoc.properties.Out {
1557 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1558 }
1559
Makoto Onuki88b99052020-04-27 17:22:16 -07001560 // Add options for the other optional tasks: API-lint and check-released.
1561 // We generate separate timestamp files for them.
1562
1563 doApiLint := false
1564 doCheckReleased := false
1565
1566 // Add API lint options.
1567
1568 if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) && !ctx.Config().IsPdkBuild() {
1569 doApiLint = true
1570
1571 newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since)
1572 if newSince.Valid() {
1573 cmd.FlagWithInput("--api-lint ", newSince.Path())
1574 } else {
1575 cmd.Flag("--api-lint")
1576 }
1577 d.apiLintReport = android.PathForModuleOut(ctx, "api_lint_report.txt")
1578 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport) // TODO: Change to ":api-lint"
1579
1580 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
1581 updatedBaselineOutput := android.PathForModuleOut(ctx, "api_lint_baseline.txt")
1582 d.apiLintTimestamp = android.PathForModuleOut(ctx, "api_lint.timestamp")
1583
1584 // Note this string includes a special shell quote $' ... ', which decodes the "\n"s.
1585 // However, because $' ... ' doesn't expand environmental variables, we can't just embed
1586 // $PWD, so we have to terminate $'...', use "$PWD", then start $' ... ' again,
1587 // which is why we have '"$PWD"$' in it.
1588 //
1589 // TODO: metalava also has a slightly different message hardcoded. Should we unify this
1590 // message and metalava's one?
1591 msg := `$'` + // Enclose with $' ... '
1592 `************************************************************\n` +
1593 `Your API changes are triggering API Lint warnings or errors.\n` +
1594 `To make these errors go away, fix the code according to the\n` +
1595 `error and/or warning messages above.\n` +
1596 `\n` +
1597 `If it is not possible to do so, there are workarounds:\n` +
1598 `\n` +
1599 `1. You can suppress the errors with @SuppressLint("<id>")\n`
1600
1601 if baselineFile.Valid() {
1602 cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path())
1603 cmd.FlagWithOutput("--update-baseline:api-lint ", updatedBaselineOutput)
1604
1605 msg += fmt.Sprintf(``+
1606 `2. You can update the baseline by executing the following\n`+
1607 ` command:\n`+
Anton Hansson3361a292020-05-11 15:38:31 +01001608 ` cp \\\n`+
1609 ` "'"$PWD"$'/%s" \\\n`+
1610 ` "'"$PWD"$'/%s"\n`+
Makoto Onuki88b99052020-04-27 17:22:16 -07001611 ` To submit the revised baseline.txt to the main Android\n`+
1612 ` repository, you will need approval.\n`, updatedBaselineOutput, baselineFile.Path())
1613 } else {
1614 msg += fmt.Sprintf(``+
1615 `2. You can add a baseline file of existing lint failures\n`+
1616 ` to the build rule of %s.\n`, d.Name())
1617 }
1618 // Note the message ends with a ' (single quote), to close the $' ... ' .
1619 msg += `************************************************************\n'`
1620
1621 cmd.FlagWithArg("--error-message:api-lint ", msg)
1622 }
1623
1624 // Add "check released" options. (Detect incompatible API changes from the last public release)
1625
1626 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") &&
1627 !ctx.Config().IsPdkBuild() {
1628 doCheckReleased = true
1629
1630 if len(d.Javadoc.properties.Out) > 0 {
1631 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1632 }
1633
1634 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1635 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
1636 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
1637 updatedBaselineOutput := android.PathForModuleOut(ctx, "last_released_baseline.txt")
1638
1639 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
1640
1641 cmd.FlagWithInput("--check-compatibility:api:released ", apiFile)
1642 cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile)
1643
1644 if baselineFile.Valid() {
1645 cmd.FlagWithInput("--baseline:compatibility:released ", baselineFile.Path())
1646 cmd.FlagWithOutput("--update-baseline:compatibility:released ", updatedBaselineOutput)
1647 }
1648
1649 // Note this string includes quote ($' ... '), which decodes the "\n"s.
1650 msg := `$'\n******************************\n` +
1651 `You have tried to change the API from what has been previously released in\n` +
1652 `an SDK. Please fix the errors listed above.\n` +
1653 `******************************\n'`
1654
1655 cmd.FlagWithArg("--error-message:compatibility:released ", msg)
1656 }
1657
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001658 impRule := android.NewRuleBuilder()
1659 impCmd := impRule.Command()
1660 // A dummy action that copies the ninja generated rsp file to a new location. This allows us to
1661 // add a large number of inputs to a file without exceeding bash command length limits (which
1662 // would happen if we use the WriteFile rule). The cp is needed because RuleBuilder sets the
1663 // rsp file to be ${output}.rsp.
1664 impCmd.Text("cp").FlagWithRspFileInputList("", cmd.GetImplicits()).Output(implicitsRsp)
1665 impRule.Build(pctx, ctx, "implicitsGen", "implicits generation")
1666 cmd.Implicit(implicitsRsp)
1667
Paul Duffin3ae29512020-04-08 18:18:03 +01001668 if generateStubs {
1669 rule.Command().
1670 BuiltTool(ctx, "soong_zip").
1671 Flag("-write_if_changed").
1672 Flag("-jar").
1673 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
1674 FlagWithArg("-C ", stubsDir.String()).
1675 FlagWithArg("-D ", stubsDir.String())
1676 }
Jerome Gaillard0f599032019-10-10 19:29:11 +01001677
1678 if Bool(d.properties.Write_sdk_values) {
1679 d.metadataZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-metadata.zip")
1680 rule.Command().
1681 BuiltTool(ctx, "soong_zip").
1682 Flag("-write_if_changed").
1683 Flag("-d").
1684 FlagWithOutput("-o ", d.metadataZip).
1685 FlagWithArg("-C ", d.metadataDir.String()).
1686 FlagWithArg("-D ", d.metadataDir.String())
1687 }
1688
Makoto Onuki88b99052020-04-27 17:22:16 -07001689 // TODO: We don't really need two separate API files, but this is a reminiscence of how
1690 // we used to run metalava separately for API lint and the "last_released" check. Unify them.
1691 if doApiLint {
1692 rule.Command().Text("touch").Output(d.apiLintTimestamp)
1693 }
1694 if doCheckReleased {
1695 rule.Command().Text("touch").Output(d.checkLastReleasedApiTimestamp)
1696 }
1697
Colin Cross33961b52019-07-11 11:01:22 -07001698 rule.Restat()
1699
1700 zipSyncCleanupCmd(rule, srcJarDir)
1701
Makoto Onuki88b99052020-04-27 17:22:16 -07001702 rule.Build(pctx, ctx, "metalava", "metalava merged")
Adrian Roos075eedc2019-10-10 12:07:03 +02001703
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001704 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001705 !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001706
1707 if len(d.Javadoc.properties.Out) > 0 {
1708 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1709 }
1710
1711 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1712 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Adrian Roos14f75a92019-08-12 17:54:09 +02001713 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
Makoto Onuki5405a732020-04-16 17:02:40 -07001714
1715 if baselineFile.Valid() {
Makoto Onuki88b99052020-04-27 17:22:16 -07001716 ctx.PropertyErrorf("baseline_file", "current API check can't have a baseline file. (module %s)", ctx.ModuleName())
Makoto Onuki5405a732020-04-16 17:02:40 -07001717 }
Nan Zhang61819ce2018-05-04 18:49:16 -07001718
Nan Zhang2760dfc2018-08-24 17:32:54 +00001719 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Nan Zhang2760dfc2018-08-24 17:32:54 +00001720
Colin Cross33961b52019-07-11 11:01:22 -07001721 rule := android.NewRuleBuilder()
1722
Makoto Onuki5405a732020-04-16 17:02:40 -07001723 // Diff command line.
Makoto Onuki88b99052020-04-27 17:22:16 -07001724 // -F matches the closest "opening" line, such as "package android {"
1725 // and " public class Intent {".
Makoto Onuki5405a732020-04-16 17:02:40 -07001726 diff := `diff -u -F '{ *$'`
1727
Colin Cross33961b52019-07-11 11:01:22 -07001728 rule.Command().Text("( true")
Makoto Onuki5405a732020-04-16 17:02:40 -07001729 rule.Command().
1730 Text(diff).
1731 Input(apiFile).Input(d.apiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001732
Makoto Onuki5405a732020-04-16 17:02:40 -07001733 rule.Command().
1734 Text(diff).
1735 Input(removedApiFile).Input(d.removedApiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001736
1737 msg := fmt.Sprintf(`\n******************************\n`+
1738 `You have tried to change the API from what has been previously approved.\n\n`+
1739 `To make these errors go away, you have two choices:\n`+
Makoto Onuki5405a732020-04-16 17:02:40 -07001740 ` 1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)\n`+
1741 ` to the new methods, etc. shown in the above diff.\n\n`+
1742 ` 2. You can update current.txt and/or removed.txt by executing the following command:\n`+
Colin Cross33961b52019-07-11 11:01:22 -07001743 ` make %s-update-current-api\n\n`+
1744 ` To submit the revised current.txt to the main Android repository,\n`+
1745 ` you will need approval.\n`+
1746 `******************************\n`, ctx.ModuleName())
1747
1748 rule.Command().
1749 Text("touch").Output(d.checkCurrentApiTimestamp).
1750 Text(") || (").
1751 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1752 Text("; exit 38").
1753 Text(")")
1754
Makoto Onuki5405a732020-04-16 17:02:40 -07001755 rule.Build(pctx, ctx, "metalavaCurrentApiCheck", "check current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001756
1757 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001758
1759 // update API rule
1760 rule = android.NewRuleBuilder()
1761
1762 rule.Command().Text("( true")
1763
1764 rule.Command().
1765 Text("cp").Flag("-f").
1766 Input(d.apiFile).Flag(apiFile.String())
1767
1768 rule.Command().
1769 Text("cp").Flag("-f").
1770 Input(d.removedApiFile).Flag(removedApiFile.String())
1771
1772 msg = "failed to update public API"
1773
1774 rule.Command().
1775 Text("touch").Output(d.updateCurrentApiTimestamp).
1776 Text(") || (").
1777 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1778 Text("; exit 38").
1779 Text(")")
1780
1781 rule.Build(pctx, ctx, "metalavaCurrentApiUpdate", "update current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001782 }
Nan Zhanga40da042018-08-01 12:48:00 -07001783
Pete Gillin581d6082018-10-22 15:55:04 +01001784 if String(d.properties.Check_nullability_warnings) != "" {
1785 if d.nullabilityWarningsFile == nil {
1786 ctx.PropertyErrorf("check_nullability_warnings",
1787 "Cannot specify check_nullability_warnings unless validating nullability")
1788 }
Colin Cross33961b52019-07-11 11:01:22 -07001789
1790 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
1791
Pete Gillin581d6082018-10-22 15:55:04 +01001792 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "check_nullability_warnings.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001793
Pete Gillin581d6082018-10-22 15:55:04 +01001794 msg := fmt.Sprintf(`\n******************************\n`+
1795 `The warnings encountered during nullability annotation validation did\n`+
1796 `not match the checked in file of expected warnings. The diffs are shown\n`+
1797 `above. You have two options:\n`+
1798 ` 1. Resolve the differences by editing the nullability annotations.\n`+
1799 ` 2. Update the file of expected warnings by running:\n`+
1800 ` cp %s %s\n`+
1801 ` and submitting the updated file as part of your change.`,
1802 d.nullabilityWarningsFile, checkNullabilityWarnings)
Colin Cross33961b52019-07-11 11:01:22 -07001803
1804 rule := android.NewRuleBuilder()
1805
1806 rule.Command().
1807 Text("(").
1808 Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile).
1809 Text("&&").
1810 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
1811 Text(") || (").
1812 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1813 Text("; exit 38").
1814 Text(")")
1815
1816 rule.Build(pctx, ctx, "nullabilityWarningsCheck", "nullability warnings check")
Pete Gillin581d6082018-10-22 15:55:04 +01001817 }
1818
Nan Zhang71bbe632018-09-17 14:32:21 -07001819 if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001820 if len(d.Javadoc.properties.Out) > 0 {
1821 ctx.PropertyErrorf("out", "out property may not be combined with jdiff")
1822 }
1823
1824 outDir := android.PathForModuleOut(ctx, "jdiff-out")
1825 srcJarDir := android.PathForModuleOut(ctx, "jdiff-srcjars")
1826 stubsDir := android.PathForModuleOut(ctx, "jdiff-stubsDir")
1827
1828 rule := android.NewRuleBuilder()
Nan Zhang71bbe632018-09-17 14:32:21 -07001829
Nan Zhang86b06202018-09-21 17:09:21 -07001830 // Please sync with android-api-council@ before making any changes for the name of jdiffDocZip below
1831 // since there's cron job downstream that fetch this .zip file periodically.
1832 // See b/116221385 for reference.
Nan Zhang71bbe632018-09-17 14:32:21 -07001833 d.jdiffDocZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-docs.zip")
1834 d.jdiffStubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-stubs.srcjar")
1835
Nan Zhang71bbe632018-09-17 14:32:21 -07001836 jdiff := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jdiff.jar")
Nan Zhang71bbe632018-09-17 14:32:21 -07001837
Colin Cross33961b52019-07-11 11:01:22 -07001838 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
1839 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang71bbe632018-09-17 14:32:21 -07001840
Colin Cross33961b52019-07-11 11:01:22 -07001841 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1842
Colin Crossdaa4c672019-07-15 22:53:46 -07001843 cmd := javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -07001844 deps.bootClasspath, deps.classpath, d.sourcepaths)
1845
1846 cmd.Flag("-J-Xmx1600m").
Colin Cross33961b52019-07-11 11:01:22 -07001847 Flag("-XDignore.symbol.file").
1848 FlagWithArg("-doclet ", "jdiff.JDiff").
1849 FlagWithInput("-docletpath ", jdiff).
1850 Flag("-quiet").
1851 FlagWithArg("-newapi ", strings.TrimSuffix(d.apiXmlFile.Base(), d.apiXmlFile.Ext())).
1852 FlagWithArg("-newapidir ", filepath.Dir(d.apiXmlFile.String())).
1853 Implicit(d.apiXmlFile).
1854 FlagWithArg("-oldapi ", strings.TrimSuffix(d.lastReleasedApiXmlFile.Base(), d.lastReleasedApiXmlFile.Ext())).
1855 FlagWithArg("-oldapidir ", filepath.Dir(d.lastReleasedApiXmlFile.String())).
1856 Implicit(d.lastReleasedApiXmlFile)
1857
Colin Cross33961b52019-07-11 11:01:22 -07001858 rule.Command().
1859 BuiltTool(ctx, "soong_zip").
1860 Flag("-write_if_changed").
1861 Flag("-d").
1862 FlagWithOutput("-o ", d.jdiffDocZip).
1863 FlagWithArg("-C ", outDir.String()).
1864 FlagWithArg("-D ", outDir.String())
1865
1866 rule.Command().
1867 BuiltTool(ctx, "soong_zip").
1868 Flag("-write_if_changed").
1869 Flag("-jar").
1870 FlagWithOutput("-o ", d.jdiffStubsSrcJar).
1871 FlagWithArg("-C ", stubsDir.String()).
1872 FlagWithArg("-D ", stubsDir.String())
1873
1874 rule.Restat()
1875
1876 zipSyncCleanupCmd(rule, srcJarDir)
1877
1878 rule.Build(pctx, ctx, "jdiff", "jdiff")
Nan Zhang71bbe632018-09-17 14:32:21 -07001879 }
Nan Zhang581fd212018-01-10 16:06:12 -08001880}
Dan Willemsencc090972018-02-26 14:33:31 -08001881
Nan Zhanga40da042018-08-01 12:48:00 -07001882//
Nan Zhangf4936b02018-08-01 15:00:28 -07001883// Exported Droiddoc Directory
Nan Zhanga40da042018-08-01 12:48:00 -07001884//
Dan Willemsencc090972018-02-26 14:33:31 -08001885var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
Nan Zhangf4936b02018-08-01 15:00:28 -07001886var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
Pete Gillin77167902018-09-19 18:16:26 +01001887var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
Nan Zhang9c69a122018-08-22 10:22:08 -07001888var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
Dan Willemsencc090972018-02-26 14:33:31 -08001889
Nan Zhangf4936b02018-08-01 15:00:28 -07001890type ExportedDroiddocDirProperties struct {
1891 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -08001892 Path *string
1893}
1894
Nan Zhangf4936b02018-08-01 15:00:28 -07001895type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -08001896 android.ModuleBase
1897
Nan Zhangf4936b02018-08-01 15:00:28 -07001898 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -08001899
1900 deps android.Paths
1901 dir android.Path
1902}
1903
Colin Crossa3002fc2019-07-08 16:48:04 -07001904// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -07001905func ExportedDroiddocDirFactory() android.Module {
1906 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -08001907 module.AddProperties(&module.properties)
1908 android.InitAndroidModule(module)
1909 return module
1910}
1911
Nan Zhangf4936b02018-08-01 15:00:28 -07001912func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -08001913
Nan Zhangf4936b02018-08-01 15:00:28 -07001914func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -08001915 path := String(d.properties.Path)
1916 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -08001917 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Dan Willemsencc090972018-02-26 14:33:31 -08001918}
Nan Zhangb2b33de2018-02-23 11:18:47 -08001919
1920//
1921// Defaults
1922//
1923type DocDefaults struct {
1924 android.ModuleBase
1925 android.DefaultsModuleBase
1926}
1927
Nan Zhangb2b33de2018-02-23 11:18:47 -08001928func DocDefaultsFactory() android.Module {
1929 module := &DocDefaults{}
1930
1931 module.AddProperties(
1932 &JavadocProperties{},
1933 &DroiddocProperties{},
1934 )
1935
1936 android.InitDefaultsModule(module)
1937
1938 return module
1939}
Nan Zhang1598a9e2018-09-04 17:14:32 -07001940
1941func StubsDefaultsFactory() android.Module {
1942 module := &DocDefaults{}
1943
1944 module.AddProperties(
1945 &JavadocProperties{},
1946 &DroidstubsProperties{},
1947 )
1948
1949 android.InitDefaultsModule(module)
1950
1951 return module
1952}
Colin Cross33961b52019-07-11 11:01:22 -07001953
1954func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
1955 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
1956
1957 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1958 rule.Command().Text("mkdir -p").Text(srcJarDir.String())
1959 srcJarList := srcJarDir.Join(ctx, "list")
1960
1961 rule.Temporary(srcJarList)
1962
1963 rule.Command().BuiltTool(ctx, "zipsync").
1964 FlagWithArg("-d ", srcJarDir.String()).
1965 FlagWithOutput("-l ", srcJarList).
1966 FlagWithArg("-f ", `"*.java"`).
1967 Inputs(srcJars)
1968
1969 return srcJarList
1970}
1971
1972func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
1973 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1974}
Paul Duffin91547182019-11-12 19:39:36 +00001975
1976var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
1977
1978type PrebuiltStubsSourcesProperties struct {
1979 Srcs []string `android:"path"`
1980}
1981
1982type PrebuiltStubsSources struct {
1983 android.ModuleBase
1984 android.DefaultableModuleBase
1985 prebuilt android.Prebuilt
1986 android.SdkBase
1987
1988 properties PrebuiltStubsSourcesProperties
1989
Paul Duffin9b478b02019-12-10 13:41:51 +00001990 // The source directories containing stubs source files.
1991 srcDirs android.Paths
Paul Duffin91547182019-11-12 19:39:36 +00001992 stubsSrcJar android.ModuleOutPath
1993}
1994
Paul Duffin9b478b02019-12-10 13:41:51 +00001995func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) {
1996 switch tag {
1997 case "":
1998 return android.Paths{p.stubsSrcJar}, nil
1999 default:
2000 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
2001 }
2002}
2003
Paul Duffin0f8faff2020-05-20 16:18:00 +01002004func (d *PrebuiltStubsSources) StubsSrcJar() android.Path {
2005 return d.stubsSrcJar
2006}
2007
Paul Duffin91547182019-11-12 19:39:36 +00002008func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin9b478b02019-12-10 13:41:51 +00002009 p.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
2010
2011 p.srcDirs = android.PathsForModuleSrc(ctx, p.properties.Srcs)
2012
2013 rule := android.NewRuleBuilder()
2014 command := rule.Command().
2015 BuiltTool(ctx, "soong_zip").
2016 Flag("-write_if_changed").
2017 Flag("-jar").
2018 FlagWithOutput("-o ", p.stubsSrcJar)
2019
2020 for _, d := range p.srcDirs {
2021 dir := d.String()
2022 command.
2023 FlagWithArg("-C ", dir).
2024 FlagWithInput("-D ", d)
2025 }
2026
2027 rule.Restat()
2028
2029 rule.Build(pctx, ctx, "zip src", "Create srcjar from prebuilt source")
Paul Duffin91547182019-11-12 19:39:36 +00002030}
2031
2032func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
2033 return &p.prebuilt
2034}
2035
2036func (p *PrebuiltStubsSources) Name() string {
2037 return p.prebuilt.Name(p.ModuleBase.Name())
2038}
2039
Paul Duffin91547182019-11-12 19:39:36 +00002040// prebuilt_stubs_sources imports a set of java source files as if they were
2041// generated by droidstubs.
2042//
2043// By default, a prebuilt_stubs_sources has a single variant that expects a
2044// set of `.java` files generated by droidstubs.
2045//
2046// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2047// for host modules.
2048//
2049// Intended only for use by sdk snapshots.
2050func PrebuiltStubsSourcesFactory() android.Module {
2051 module := &PrebuiltStubsSources{}
2052
2053 module.AddProperties(&module.properties)
2054
2055 android.InitPrebuiltModule(module, &module.properties.Srcs)
2056 android.InitSdkAwareModule(module)
2057 InitDroiddocModule(module, android.HostAndDeviceSupported)
2058 return module
2059}
2060
Paul Duffin13879572019-11-28 14:31:38 +00002061type droidStubsSdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00002062 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00002063}
2064
2065func (mt *droidStubsSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2066 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2067}
2068
2069func (mt *droidStubsSdkMemberType) IsInstance(module android.Module) bool {
2070 _, ok := module.(*Droidstubs)
2071 return ok
2072}
2073
Paul Duffin495ffb92020-03-20 13:35:40 +00002074func (mt *droidStubsSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2075 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_stubs_sources")
2076}
2077
2078func (mt *droidStubsSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2079 return &droidStubsInfoProperties{}
2080}
2081
2082type droidStubsInfoProperties struct {
2083 android.SdkMemberPropertiesBase
2084
2085 StubsSrcJar android.Path
2086}
2087
2088func (p *droidStubsInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2089 droidstubs := variant.(*Droidstubs)
2090 p.StubsSrcJar = droidstubs.stubsSrcJar
2091}
2092
2093func (p *droidStubsInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
2094 if p.StubsSrcJar != nil {
2095 builder := ctx.SnapshotBuilder()
2096
2097 snapshotRelativeDir := filepath.Join("java", ctx.Name()+"_stubs_sources")
2098
2099 builder.UnzipToSnapshot(p.StubsSrcJar, snapshotRelativeDir)
2100
2101 propertySet.AddProperty("srcs", []string{snapshotRelativeDir})
Paul Duffin13879572019-11-28 14:31:38 +00002102 }
Paul Duffin91547182019-11-12 19:39:36 +00002103}