blob: 4d806baef7a1e0e82199af6067da28f6370a92c0 [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 Medhat1fb3cd82020-05-05 22:50:09 +000027 "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
Nan Zhang581fd212018-01-10 16:06:12 -0800124}
125
Nan Zhang61819ce2018-05-04 18:49:16 -0700126type ApiToCheck struct {
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900127 // path to the API txt file that the new API extracted from source code is checked
128 // against. The path can be local to the module or from other module (via :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800129 Api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700130
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900131 // path to the API txt file that the new @removed API extractd from source code is
132 // checked against. The path can be local to the module or from other module (via
133 // :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800134 Removed_api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700135
Adrian Roos14f75a92019-08-12 17:54:09 +0200136 // If not blank, path to the baseline txt file for approved API check violations.
137 Baseline_file *string `android:"path"`
138
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900139 // Arguments to the apicheck tool.
Nan Zhang61819ce2018-05-04 18:49:16 -0700140 Args *string
141}
142
Nan Zhang581fd212018-01-10 16:06:12 -0800143type DroiddocProperties struct {
144 // directory relative to top of the source tree that contains doc templates files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800145 Custom_template *string
Nan Zhang581fd212018-01-10 16:06:12 -0800146
Nan Zhanga40da042018-08-01 12:48:00 -0700147 // directories under current module source which contains html/jd files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800148 Html_dirs []string
Nan Zhang581fd212018-01-10 16:06:12 -0800149
150 // set a value in the Clearsilver hdf namespace.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800151 Hdf []string
Nan Zhang581fd212018-01-10 16:06:12 -0800152
153 // proofread file contains all of the text content of the javadocs concatenated into one file,
154 // suitable for spell-checking and other goodness.
Colin Crossab054432019-07-15 16:13:59 -0700155 Proofread_file *string
Nan Zhang581fd212018-01-10 16:06:12 -0800156
157 // a todo file lists the program elements that are missing documentation.
158 // At some point, this might be improved to show more warnings.
Colin Cross27b922f2019-03-04 22:35:41 -0800159 Todo_file *string `android:"path"`
Nan Zhangb2b33de2018-02-23 11:18:47 -0800160
161 // directory under current module source that provide additional resources (images).
162 Resourcesdir *string
163
164 // resources output directory under out/soong/.intermediates.
165 Resourcesoutdir *string
Nan Zhang581fd212018-01-10 16:06:12 -0800166
Nan Zhange2ba5d42018-07-11 15:16:55 -0700167 // if set to true, collect the values used by the Dev tools and
168 // write them in files packaged with the SDK. Defaults to false.
169 Write_sdk_values *bool
170
171 // index.html under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800172 Static_doc_index_redirect *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700173
174 // source.properties under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800175 Static_doc_properties *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700176
Nan Zhang581fd212018-01-10 16:06:12 -0800177 // a list of files under current module source dir which contains known tags in Java sources.
178 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -0800179 Knowntags []string `android:"path"`
Nan Zhang28c68b92018-03-13 16:17:01 -0700180
Nan Zhang28c68b92018-03-13 16:17:01 -0700181 // the generated public API filename by Doclava.
182 Api_filename *string
183
Nan Zhang28c68b92018-03-13 16:17:01 -0700184 // the generated removed API filename by Doclava.
185 Removed_api_filename *string
186
David Brazdilaac0c3c2018-04-24 16:23:29 +0100187 // the generated removed Dex API filename by Doclava.
188 Removed_dex_api_filename *string
189
Nan Zhang853f4202018-04-12 16:55:56 -0700190 // if set to false, don't allow droiddoc to generate stubs source files. Defaults to true.
191 Create_stubs *bool
Nan Zhang61819ce2018-05-04 18:49:16 -0700192
193 Check_api struct {
194 Last_released ApiToCheck
195
196 Current ApiToCheck
Inseob Kim38449af2019-02-28 14:24:05 +0900197
198 // do not perform API check against Last_released, in the case that both two specified API
199 // files by Last_released are modules which don't exist.
200 Ignore_missing_latest_api *bool `blueprint:"mutated"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700201 }
Nan Zhang79614d12018-04-19 18:03:39 -0700202
Nan Zhang1598a9e2018-09-04 17:14:32 -0700203 // if set to true, generate docs through Dokka instead of Doclava.
204 Dokka_enabled *bool
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000205
206 // Compat config XML. Generates compat change documentation if set.
207 Compat_config *string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -0700208}
209
210type DroidstubsProperties struct {
Nan Zhang199645c2018-09-19 12:40:06 -0700211 // the generated public API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700212 Api_filename *string
213
Nan Zhang199645c2018-09-19 12:40:06 -0700214 // the generated removed API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700215 Removed_api_filename *string
216
Nan Zhang199645c2018-09-19 12:40:06 -0700217 // the generated removed Dex API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700218 Removed_dex_api_filename *string
219
Nan Zhang1598a9e2018-09-04 17:14:32 -0700220 Check_api struct {
221 Last_released ApiToCheck
222
223 Current ApiToCheck
Inseob Kim38449af2019-02-28 14:24:05 +0900224
Paul Duffin8986cc92020-05-10 19:32:20 +0100225 // The java_sdk_library module generates references to modules (i.e. filegroups)
226 // from which information about the latest API version can be obtained. As those
227 // modules may not exist (e.g. because a previous version has not been released) it
228 // sets ignore_missing_latest_api=true on the droidstubs modules it creates so
229 // that droidstubs can ignore those references if the modules do not yet exist.
230 //
231 // If true then this will ignore module references for modules that do not exist
232 // in properties that supply the previous version of the API.
233 //
234 // There are two sets of those:
235 // * Api_file, Removed_api_file in check_api.last_released
236 // * New_since in check_api.api_lint.new_since
237 //
238 // The first two must be set as a pair, so either they should both exist or neither
239 // should exist - in which case when this property is true they are ignored. If one
240 // exists and the other does not then it is an error.
Inseob Kim38449af2019-02-28 14:24:05 +0900241 Ignore_missing_latest_api *bool `blueprint:"mutated"`
Adrian Roos075eedc2019-10-10 12:07:03 +0200242
243 Api_lint struct {
244 Enabled *bool
245
246 // If set, performs api_lint on any new APIs not found in the given signature file
247 New_since *string `android:"path"`
248
249 // If not blank, path to the baseline txt file for approved API lint violations.
250 Baseline_file *string `android:"path"`
251 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700252 }
Nan Zhang79614d12018-04-19 18:03:39 -0700253
254 // user can specify the version of previous released API file in order to do compatibility check.
Colin Cross27b922f2019-03-04 22:35:41 -0800255 Previous_api *string `android:"path"`
Nan Zhang79614d12018-04-19 18:03:39 -0700256
257 // is set to true, Metalava will allow framework SDK to contain annotations.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700258 Annotations_enabled *bool
Nan Zhang79614d12018-04-19 18:03:39 -0700259
Pete Gillin77167902018-09-19 18:16:26 +0100260 // 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 -0700261 Merge_annotations_dirs []string
Nan Zhang86d2d552018-08-09 15:33:27 -0700262
Pete Gillin77167902018-09-19 18:16:26 +0100263 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
264 Merge_inclusion_annotations_dirs []string
265
Pete Gillinc382a562018-11-14 18:45:46 +0000266 // a file containing a list of classes to do nullability validation for.
267 Validate_nullability_from_list *string
268
Pete Gillin581d6082018-10-22 15:55:04 +0100269 // a file containing expected warnings produced by validation of nullability annotations.
270 Check_nullability_warnings *string
271
Nan Zhang1598a9e2018-09-04 17:14:32 -0700272 // if set to true, allow Metalava to generate doc_stubs source files. Defaults to false.
273 Create_doc_stubs *bool
Nan Zhang9c69a122018-08-22 10:22:08 -0700274
Paul Duffin455b0bf2020-04-08 18:18:03 +0100275 // if set to false then do not write out stubs. Defaults to true.
276 //
277 // TODO(b/146727827): Remove capability when we do not need to generate stubs and API separately.
278 Generate_stubs *bool
279
Nan Zhang9c69a122018-08-22 10:22:08 -0700280 // is set to true, Metalava will allow framework SDK to contain API levels annotations.
281 Api_levels_annotations_enabled *bool
282
283 // the dirs which Metalava extracts API levels annotations from.
284 Api_levels_annotations_dirs []string
285
286 // if set to true, collect the values used by the Dev tools and
287 // write them in files packaged with the SDK. Defaults to false.
288 Write_sdk_values *bool
Nan Zhang71bbe632018-09-17 14:32:21 -0700289
290 // If set to true, .xml based public API file will be also generated, and
291 // JDiff tool will be invoked to genreate javadoc files. Defaults to false.
292 Jdiff_enabled *bool
Nan Zhang581fd212018-01-10 16:06:12 -0800293}
294
Nan Zhanga40da042018-08-01 12:48:00 -0700295//
296// Common flags passed down to build rule
297//
298type droiddocBuilderFlags struct {
Nan Zhang86d2d552018-08-09 15:33:27 -0700299 bootClasspathArgs string
300 classpathArgs string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700301 sourcepathArgs string
Nan Zhang86d2d552018-08-09 15:33:27 -0700302 dokkaClasspathArgs string
303 aidlFlags string
Colin Cross3047fa22019-04-18 10:56:44 -0700304 aidlDeps android.Paths
Nan Zhanga40da042018-08-01 12:48:00 -0700305
Nan Zhanga40da042018-08-01 12:48:00 -0700306 doclavaStubsFlags string
Nan Zhang86d2d552018-08-09 15:33:27 -0700307 doclavaDocsFlags string
Nan Zhanga40da042018-08-01 12:48:00 -0700308 postDoclavaCmds string
Nan Zhanga40da042018-08-01 12:48:00 -0700309}
310
311func InitDroiddocModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
312 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
313 android.InitDefaultableModule(module)
314}
315
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200316func apiCheckEnabled(ctx android.ModuleContext, apiToCheck ApiToCheck, apiVersionTag string) bool {
317 if ctx.Config().IsEnvTrue("WITHOUT_CHECK_API") {
318 return false
319 } else if String(apiToCheck.Api_file) != "" && String(apiToCheck.Removed_api_file) != "" {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700320 return true
321 } else if String(apiToCheck.Api_file) != "" {
322 panic("for " + apiVersionTag + " removed_api_file has to be non-empty!")
323 } else if String(apiToCheck.Removed_api_file) != "" {
324 panic("for " + apiVersionTag + " api_file has to be non-empty!")
325 }
326
327 return false
328}
329
Inseob Kim38449af2019-02-28 14:24:05 +0900330func ignoreMissingModules(ctx android.BottomUpMutatorContext, apiToCheck *ApiToCheck) {
331 api_file := String(apiToCheck.Api_file)
332 removed_api_file := String(apiToCheck.Removed_api_file)
333
334 api_module := android.SrcIsModule(api_file)
335 removed_api_module := android.SrcIsModule(removed_api_file)
336
337 if api_module == "" || removed_api_module == "" {
338 return
339 }
340
341 if ctx.OtherModuleExists(api_module) || ctx.OtherModuleExists(removed_api_module) {
342 return
343 }
344
345 apiToCheck.Api_file = nil
346 apiToCheck.Removed_api_file = nil
347}
348
Paul Duffinf488ef22020-04-09 00:10:17 +0100349// Used by xsd_config
Nan Zhang1598a9e2018-09-04 17:14:32 -0700350type ApiFilePath interface {
351 ApiFilePath() android.Path
352}
353
Paul Duffin533f9c72020-05-20 16:18:00 +0100354type ApiStubsSrcProvider interface {
355 StubsSrcJar() android.Path
356}
357
Paul Duffinf488ef22020-04-09 00:10:17 +0100358// Provider of information about API stubs, used by java_sdk_library.
359type ApiStubsProvider interface {
360 ApiFilePath
Paul Duffin75dcc802020-04-09 01:08:11 +0100361 RemovedApiFilePath() android.Path
Paul Duffin533f9c72020-05-20 16:18:00 +0100362
363 ApiStubsSrcProvider
Paul Duffinf488ef22020-04-09 00:10:17 +0100364}
365
Nan Zhanga40da042018-08-01 12:48:00 -0700366//
367// Javadoc
368//
Nan Zhang581fd212018-01-10 16:06:12 -0800369type Javadoc struct {
370 android.ModuleBase
371 android.DefaultableModuleBase
372
373 properties JavadocProperties
374
375 srcJars android.Paths
376 srcFiles android.Paths
377 sourcepaths android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700378 argFiles android.Paths
379
380 args string
Nan Zhang581fd212018-01-10 16:06:12 -0800381
Nan Zhangccff0f72018-03-08 17:26:16 -0800382 docZip android.WritablePath
383 stubsSrcJar android.WritablePath
Nan Zhang581fd212018-01-10 16:06:12 -0800384}
385
Colin Cross41955e82019-05-29 14:40:35 -0700386func (j *Javadoc) OutputFiles(tag string) (android.Paths, error) {
387 switch tag {
388 case "":
389 return android.Paths{j.stubsSrcJar}, nil
Colin Crosse68e5542019-08-12 13:11:40 -0700390 case ".docs.zip":
391 return android.Paths{j.docZip}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700392 default:
393 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
394 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800395}
396
Colin Crossa3002fc2019-07-08 16:48:04 -0700397// javadoc converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800398func JavadocFactory() android.Module {
399 module := &Javadoc{}
400
401 module.AddProperties(&module.properties)
402
403 InitDroiddocModule(module, android.HostAndDeviceSupported)
404 return module
405}
406
Colin Crossa3002fc2019-07-08 16:48:04 -0700407// javadoc_host converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800408func JavadocHostFactory() android.Module {
409 module := &Javadoc{}
410
411 module.AddProperties(&module.properties)
412
413 InitDroiddocModule(module, android.HostSupported)
414 return module
415}
416
Colin Cross41955e82019-05-29 14:40:35 -0700417var _ android.OutputFileProducer = (*Javadoc)(nil)
Nan Zhang581fd212018-01-10 16:06:12 -0800418
Jiyong Park6a927c42020-01-21 02:03:43 +0900419func (j *Javadoc) sdkVersion() sdkSpec {
420 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700421}
422
Paul Duffine25c6442019-10-11 13:50:28 +0100423func (j *Javadoc) systemModules() string {
424 return proptools.String(j.properties.System_modules)
425}
426
Jiyong Park6a927c42020-01-21 02:03:43 +0900427func (j *Javadoc) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700428 return j.sdkVersion()
429}
430
Jiyong Park6a927c42020-01-21 02:03:43 +0900431func (j *Javadoc) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700432 return j.sdkVersion()
433}
434
Nan Zhang581fd212018-01-10 16:06:12 -0800435func (j *Javadoc) addDeps(ctx android.BottomUpMutatorContext) {
436 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100437 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700438 if sdkDep.useDefaultLibs {
439 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
440 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
441 if sdkDep.hasFrameworkLibs() {
442 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Nan Zhang357466b2018-04-17 17:38:36 -0700443 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700444 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700445 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100446 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700447 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Nan Zhang581fd212018-01-10 16:06:12 -0800448 }
449 }
450
Colin Cross42d48b72018-08-29 14:10:52 -0700451 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800452}
453
Nan Zhanga40da042018-08-01 12:48:00 -0700454func (j *Javadoc) collectAidlFlags(ctx android.ModuleContext, deps deps) droiddocBuilderFlags {
455 var flags droiddocBuilderFlags
Jiyong Park1e440682018-05-23 18:42:04 +0900456
Colin Cross3047fa22019-04-18 10:56:44 -0700457 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Jiyong Park1e440682018-05-23 18:42:04 +0900458
459 return flags
460}
461
462func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700463 aidlIncludeDirs android.Paths) (string, android.Paths) {
Jiyong Park1e440682018-05-23 18:42:04 +0900464
465 aidlIncludes := android.PathsForModuleSrc(ctx, j.properties.Aidl.Local_include_dirs)
466 aidlIncludes = append(aidlIncludes, android.PathsForSource(ctx, j.properties.Aidl.Include_dirs)...)
467
468 var flags []string
Colin Cross3047fa22019-04-18 10:56:44 -0700469 var deps android.Paths
470
Jiyong Park1e440682018-05-23 18:42:04 +0900471 if aidlPreprocess.Valid() {
472 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700473 deps = append(deps, aidlPreprocess.Path())
Jiyong Park1e440682018-05-23 18:42:04 +0900474 } else {
475 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
476 }
477
478 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
479 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
480 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
481 flags = append(flags, "-I"+src.String())
482 }
483
Colin Cross3047fa22019-04-18 10:56:44 -0700484 return strings.Join(flags, " "), deps
Jiyong Park1e440682018-05-23 18:42:04 +0900485}
486
Jiyong Parkd90d7412019-08-20 22:49:19 +0900487// TODO: remove the duplication between this and the one in gen.go
Jiyong Park1e440682018-05-23 18:42:04 +0900488func (j *Javadoc) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Nan Zhanga40da042018-08-01 12:48:00 -0700489 flags droiddocBuilderFlags) android.Paths {
Jiyong Park1e440682018-05-23 18:42:04 +0900490
491 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Crossc0806172019-06-14 18:51:47 -0700492 var aidlSrcs android.Paths
Jiyong Park1e440682018-05-23 18:42:04 +0900493
Jiyong Park1112c4c2019-08-16 21:12:10 +0900494 aidlIncludeFlags := genAidlIncludeFlags(srcFiles)
495
Jiyong Park1e440682018-05-23 18:42:04 +0900496 for _, srcFile := range srcFiles {
497 switch srcFile.Ext() {
498 case ".aidl":
Colin Crossc0806172019-06-14 18:51:47 -0700499 aidlSrcs = append(aidlSrcs, srcFile)
Jiyong Parkd90d7412019-08-20 22:49:19 +0900500 case ".logtags":
501 javaFile := genLogtags(ctx, srcFile)
502 outSrcFiles = append(outSrcFiles, javaFile)
Jiyong Park1e440682018-05-23 18:42:04 +0900503 default:
504 outSrcFiles = append(outSrcFiles, srcFile)
505 }
506 }
507
Colin Crossc0806172019-06-14 18:51:47 -0700508 // Process all aidl files together to support sharding them into one or more rules that produce srcjars.
509 if len(aidlSrcs) > 0 {
510 srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags+aidlIncludeFlags, flags.aidlDeps)
511 outSrcFiles = append(outSrcFiles, srcJarFiles...)
512 }
513
Jiyong Park1e440682018-05-23 18:42:04 +0900514 return outSrcFiles
515}
516
Nan Zhang581fd212018-01-10 16:06:12 -0800517func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
518 var deps deps
519
Colin Cross83bb3162018-06-25 15:48:06 -0700520 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800521 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700522 ctx.AddMissingDependencies(sdkDep.bootclasspath)
523 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Nan Zhang581fd212018-01-10 16:06:12 -0800524 } else if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700525 deps.bootClasspath = append(deps.bootClasspath, sdkDep.jars...)
Anton Hansson26bf49b2020-02-08 20:26:29 +0000526 deps.aidlPreprocess = sdkDep.aidl
527 } else {
528 deps.aidlPreprocess = sdkDep.aidl
Nan Zhang581fd212018-01-10 16:06:12 -0800529 }
530
531 ctx.VisitDirectDeps(func(module android.Module) {
532 otherName := ctx.OtherModuleName(module)
533 tag := ctx.OtherModuleDependencyTag(module)
534
Colin Cross2d24c1b2018-05-23 10:59:18 -0700535 switch tag {
536 case bootClasspathTag:
537 if dep, ok := module.(Dependency); ok {
Nan Zhang581fd212018-01-10 16:06:12 -0800538 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars()...)
Paul Duffin83a2d962019-11-19 19:44:10 +0000539 } else if sm, ok := module.(SystemModulesProvider); ok {
Paul Duffine25c6442019-10-11 13:50:28 +0100540 // A system modules dependency has been added to the bootclasspath
541 // so add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +0000542 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700543 } else {
544 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
545 }
546 case libTag:
547 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800548 case SdkLibraryDependency:
Paul Duffin174b26e2020-05-26 11:42:13 +0100549 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700550 case Dependency:
Sundong Ahnba493602018-11-20 17:36:35 +0900551 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park19a7f252019-07-10 16:59:31 +0900552 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700553 case android.SourceFileProducer:
Nan Zhang581fd212018-01-10 16:06:12 -0800554 checkProducesJars(ctx, dep)
555 deps.classpath = append(deps.classpath, dep.Srcs()...)
Nan Zhang581fd212018-01-10 16:06:12 -0800556 default:
557 ctx.ModuleErrorf("depends on non-java module %q", otherName)
558 }
Colin Cross6cef4812019-10-17 14:23:50 -0700559 case java9LibTag:
560 switch dep := module.(type) {
561 case Dependency:
562 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
563 default:
564 ctx.ModuleErrorf("depends on non-java module %q", otherName)
565 }
Nan Zhang357466b2018-04-17 17:38:36 -0700566 case systemModulesTag:
567 if deps.systemModules != nil {
568 panic("Found two system module dependencies")
569 }
Paul Duffin83a2d962019-11-19 19:44:10 +0000570 sm := module.(SystemModulesProvider)
571 outputDir, outputDeps := sm.OutputDirAndDeps()
572 deps.systemModules = &systemModules{outputDir, outputDeps}
Nan Zhang581fd212018-01-10 16:06:12 -0800573 }
574 })
575 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
576 // may contain filegroup or genrule.
Colin Cross8a497952019-03-05 22:25:09 -0800577 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900578
579 filterByPackage := func(srcs []android.Path, filterPackages []string) []android.Path {
580 if filterPackages == nil {
581 return srcs
582 }
583 filtered := []android.Path{}
584 for _, src := range srcs {
585 if src.Ext() != ".java" {
586 // Don't filter-out non-Java (=generated sources) by package names. This is not ideal,
587 // but otherwise metalava emits stub sources having references to the generated AIDL classes
588 // in filtered-out pacages (e.g. com.android.internal.*).
589 // TODO(b/141149570) We need to fix this by introducing default private constructors or
590 // fixing metalava to not emit constructors having references to unknown classes.
591 filtered = append(filtered, src)
592 continue
593 }
594 packageName := strings.ReplaceAll(filepath.Dir(src.Rel()), "/", ".")
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800595 if android.HasAnyPrefix(packageName, filterPackages) {
596 filtered = append(filtered, src)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900597 }
598 }
599 return filtered
600 }
601 srcFiles = filterByPackage(srcFiles, j.properties.Filter_packages)
602
Nan Zhanga40da042018-08-01 12:48:00 -0700603 flags := j.collectAidlFlags(ctx, deps)
Jiyong Park1e440682018-05-23 18:42:04 +0900604 srcFiles = j.genSources(ctx, srcFiles, flags)
Nan Zhang581fd212018-01-10 16:06:12 -0800605
606 // srcs may depend on some genrule output.
607 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800608 j.srcJars = append(j.srcJars, deps.srcJars...)
609
Nan Zhang581fd212018-01-10 16:06:12 -0800610 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800611 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800612
Nan Zhang9c69a122018-08-22 10:22:08 -0700613 if j.properties.Local_sourcepaths == nil && len(j.srcFiles) > 0 {
Nan Zhang581fd212018-01-10 16:06:12 -0800614 j.properties.Local_sourcepaths = append(j.properties.Local_sourcepaths, ".")
615 }
616 j.sourcepaths = android.PathsForModuleSrc(ctx, j.properties.Local_sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800617
Colin Cross8a497952019-03-05 22:25:09 -0800618 j.argFiles = android.PathsForModuleSrc(ctx, j.properties.Arg_files)
Paul Duffin99e4a502019-02-11 15:38:42 +0000619 argFilesMap := map[string]string{}
620 argFileLabels := []string{}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700621
Paul Duffin99e4a502019-02-11 15:38:42 +0000622 for _, label := range j.properties.Arg_files {
Colin Cross8a497952019-03-05 22:25:09 -0800623 var paths = android.PathsForModuleSrc(ctx, []string{label})
Paul Duffin99e4a502019-02-11 15:38:42 +0000624 if _, exists := argFilesMap[label]; !exists {
625 argFilesMap[label] = strings.Join(paths.Strings(), " ")
626 argFileLabels = append(argFileLabels, label)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700627 } else {
628 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000629 label, argFilesMap[label], paths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700630 }
631 }
632
633 var err error
Colin Cross15638152019-07-11 11:11:35 -0700634 j.args, err = android.Expand(String(j.properties.Args), func(name string) (string, error) {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700635 if strings.HasPrefix(name, "location ") {
636 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Paul Duffin99e4a502019-02-11 15:38:42 +0000637 if paths, ok := argFilesMap[label]; ok {
Colin Cross15638152019-07-11 11:11:35 -0700638 return paths, nil
Nan Zhang1598a9e2018-09-04 17:14:32 -0700639 } else {
Colin Cross15638152019-07-11 11:11:35 -0700640 return "", fmt.Errorf("unknown location label %q, expecting one of %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000641 label, strings.Join(argFileLabels, ", "))
Nan Zhang1598a9e2018-09-04 17:14:32 -0700642 }
643 } else if name == "genDir" {
Colin Cross15638152019-07-11 11:11:35 -0700644 return android.PathForModuleGen(ctx).String(), nil
Nan Zhang1598a9e2018-09-04 17:14:32 -0700645 }
Colin Cross15638152019-07-11 11:11:35 -0700646 return "", fmt.Errorf("unknown variable '$(%s)'", name)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700647 })
648
649 if err != nil {
650 ctx.PropertyErrorf("args", "%s", err.Error())
651 }
652
Nan Zhang581fd212018-01-10 16:06:12 -0800653 return deps
654}
655
656func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
657 j.addDeps(ctx)
658}
659
660func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
661 deps := j.collectDeps(ctx)
662
Colin Crossdaa4c672019-07-15 22:53:46 -0700663 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800664
Colin Crossdaa4c672019-07-15 22:53:46 -0700665 outDir := android.PathForModuleOut(ctx, "out")
666 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
667
668 j.stubsSrcJar = nil
669
670 rule := android.NewRuleBuilder()
671
672 rule.Command().Text("rm -rf").Text(outDir.String())
673 rule.Command().Text("mkdir -p").Text(outDir.String())
674
675 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, j.srcJars)
Nan Zhang357466b2018-04-17 17:38:36 -0700676
Colin Cross83bb3162018-06-25 15:48:06 -0700677 javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800678
Colin Crossdaa4c672019-07-15 22:53:46 -0700679 cmd := javadocSystemModulesCmd(ctx, rule, j.srcFiles, outDir, srcJarDir, srcJarList,
680 deps.systemModules, deps.classpath, j.sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800681
Colin Cross1e743852019-10-28 11:37:20 -0700682 cmd.FlagWithArg("-source ", javaVersion.String()).
Colin Crossdaa4c672019-07-15 22:53:46 -0700683 Flag("-J-Xmx1024m").
684 Flag("-XDignore.symbol.file").
685 Flag("-Xdoclint:none")
Nan Zhang581fd212018-01-10 16:06:12 -0800686
Colin Crossdaa4c672019-07-15 22:53:46 -0700687 rule.Command().
688 BuiltTool(ctx, "soong_zip").
689 Flag("-write_if_changed").
690 Flag("-d").
691 FlagWithOutput("-o ", j.docZip).
692 FlagWithArg("-C ", outDir.String()).
693 FlagWithArg("-D ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700694
Colin Crossdaa4c672019-07-15 22:53:46 -0700695 rule.Restat()
696
697 zipSyncCleanupCmd(rule, srcJarDir)
698
699 rule.Build(pctx, ctx, "javadoc", "javadoc")
Nan Zhang581fd212018-01-10 16:06:12 -0800700}
701
Nan Zhanga40da042018-08-01 12:48:00 -0700702//
703// Droiddoc
704//
705type Droiddoc struct {
706 Javadoc
707
708 properties DroiddocProperties
709 apiFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700710 privateApiFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700711 removedApiFile android.WritablePath
712 removedDexApiFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700713
714 checkCurrentApiTimestamp android.WritablePath
715 updateCurrentApiTimestamp android.WritablePath
716 checkLastReleasedApiTimestamp android.WritablePath
717
Nan Zhanga40da042018-08-01 12:48:00 -0700718 apiFilePath android.Path
719}
720
Colin Crossa3002fc2019-07-08 16:48:04 -0700721// droiddoc converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700722func DroiddocFactory() android.Module {
723 module := &Droiddoc{}
724
725 module.AddProperties(&module.properties,
726 &module.Javadoc.properties)
727
728 InitDroiddocModule(module, android.HostAndDeviceSupported)
729 return module
730}
731
Colin Crossa3002fc2019-07-08 16:48:04 -0700732// droiddoc_host converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700733func DroiddocHostFactory() android.Module {
734 module := &Droiddoc{}
735
736 module.AddProperties(&module.properties,
737 &module.Javadoc.properties)
738
739 InitDroiddocModule(module, android.HostSupported)
740 return module
741}
742
743func (d *Droiddoc) ApiFilePath() android.Path {
744 return d.apiFilePath
745}
746
Nan Zhang581fd212018-01-10 16:06:12 -0800747func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
748 d.Javadoc.addDeps(ctx)
749
Inseob Kim38449af2019-02-28 14:24:05 +0900750 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
751 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
752 }
753
Nan Zhang79614d12018-04-19 18:03:39 -0700754 if String(d.properties.Custom_template) != "" {
Dan Willemsencc090972018-02-26 14:33:31 -0800755 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
756 }
Nan Zhang581fd212018-01-10 16:06:12 -0800757}
758
Colin Crossab054432019-07-15 16:13:59 -0700759func (d *Droiddoc) doclavaDocsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, docletPath classpath) {
Automerger Merge Worker82f316b2020-02-28 21:26:56 +0000760 buildNumberFile := ctx.Config().BuildNumberFile(ctx)
Nan Zhang443fa522018-08-20 20:58:28 -0700761 // Droiddoc always gets "-source 1.8" because it doesn't support 1.9 sources. For modules with 1.9
762 // sources, droiddoc will get sources produced by metalava which will have already stripped out the
763 // 1.9 language features.
Colin Crossab054432019-07-15 16:13:59 -0700764 cmd.FlagWithArg("-source ", "1.8").
765 Flag("-J-Xmx1600m").
766 Flag("-J-XX:-OmitStackTraceInFastThrow").
767 Flag("-XDignore.symbol.file").
768 FlagWithArg("-doclet ", "com.google.doclava.Doclava").
769 FlagWithInputList("-docletpath ", docletPath.Paths(), ":").
Automerger Merge Worker82f316b2020-02-28 21:26:56 +0000770 FlagWithArg("-hdf page.build ", ctx.Config().BuildId()+"-$(cat "+buildNumberFile.String()+")").OrderOnly(buildNumberFile).
Elliott Hughes26bce342019-09-12 15:05:13 -0700771 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 -0700772
Nan Zhanga40da042018-08-01 12:48:00 -0700773 if String(d.properties.Custom_template) == "" {
774 // TODO: This is almost always droiddoc-templates-sdk
775 ctx.PropertyErrorf("custom_template", "must specify a template")
776 }
777
778 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
Nan Zhangf4936b02018-08-01 15:00:28 -0700779 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Crossab054432019-07-15 16:13:59 -0700780 cmd.FlagWithArg("-templatedir ", t.dir.String()).Implicits(t.deps)
Nan Zhanga40da042018-08-01 12:48:00 -0700781 } else {
Paul Duffin884363e2019-12-19 10:21:09 +0000782 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_exported_dir", ctx.OtherModuleName(m))
Nan Zhanga40da042018-08-01 12:48:00 -0700783 }
784 })
785
786 if len(d.properties.Html_dirs) > 0 {
Colin Crossab054432019-07-15 16:13:59 -0700787 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
788 cmd.FlagWithArg("-htmldir ", htmlDir.String()).
789 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[0], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700790 }
791
792 if len(d.properties.Html_dirs) > 1 {
Colin Crossab054432019-07-15 16:13:59 -0700793 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
794 cmd.FlagWithArg("-htmldir2 ", htmlDir2.String()).
795 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[1], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700796 }
797
798 if len(d.properties.Html_dirs) > 2 {
799 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
800 }
801
Colin Cross8a497952019-03-05 22:25:09 -0800802 knownTags := android.PathsForModuleSrc(ctx, d.properties.Knowntags)
Colin Crossab054432019-07-15 16:13:59 -0700803 cmd.FlagForEachInput("-knowntags ", knownTags)
Nan Zhanga40da042018-08-01 12:48:00 -0700804
Colin Crossab054432019-07-15 16:13:59 -0700805 cmd.FlagForEachArg("-hdf ", d.properties.Hdf)
Nan Zhanga40da042018-08-01 12:48:00 -0700806
807 if String(d.properties.Proofread_file) != "" {
808 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
Colin Crossab054432019-07-15 16:13:59 -0700809 cmd.FlagWithOutput("-proofread ", proofreadFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700810 }
811
812 if String(d.properties.Todo_file) != "" {
813 // tricky part:
814 // we should not compute full path for todo_file through PathForModuleOut().
815 // the non-standard doclet will get the full path relative to "-o".
Colin Crossab054432019-07-15 16:13:59 -0700816 cmd.FlagWithArg("-todo ", String(d.properties.Todo_file)).
817 ImplicitOutput(android.PathForModuleOut(ctx, String(d.properties.Todo_file)))
Nan Zhanga40da042018-08-01 12:48:00 -0700818 }
819
820 if String(d.properties.Resourcesdir) != "" {
821 // TODO: should we add files under resourcesDir to the implicits? It seems that
822 // resourcesDir is one sub dir of htmlDir
823 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
Colin Crossab054432019-07-15 16:13:59 -0700824 cmd.FlagWithArg("-resourcesdir ", resourcesDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700825 }
826
827 if String(d.properties.Resourcesoutdir) != "" {
828 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
Colin Crossab054432019-07-15 16:13:59 -0700829 cmd.FlagWithArg("-resourcesoutdir ", String(d.properties.Resourcesoutdir))
Nan Zhanga40da042018-08-01 12:48:00 -0700830 }
Nan Zhanga40da042018-08-01 12:48:00 -0700831}
832
Colin Crossab054432019-07-15 16:13:59 -0700833func (d *Droiddoc) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.WritablePath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200834 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
835 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -0700836 String(d.properties.Api_filename) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700837
Nan Zhanga40da042018-08-01 12:48:00 -0700838 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Colin Crossab054432019-07-15 16:13:59 -0700839 cmd.FlagWithOutput("-api ", d.apiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700840 d.apiFilePath = d.apiFile
841 }
842
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200843 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
844 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -0700845 String(d.properties.Removed_api_filename) != "" {
Nan Zhanga40da042018-08-01 12:48:00 -0700846 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
Colin Crossab054432019-07-15 16:13:59 -0700847 cmd.FlagWithOutput("-removedApi ", d.removedApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700848 }
849
Nan Zhanga40da042018-08-01 12:48:00 -0700850 if String(d.properties.Removed_dex_api_filename) != "" {
851 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Crossab054432019-07-15 16:13:59 -0700852 cmd.FlagWithOutput("-removedDexApi ", d.removedDexApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700853 }
854
Nan Zhanga40da042018-08-01 12:48:00 -0700855 if BoolDefault(d.properties.Create_stubs, true) {
Colin Crossab054432019-07-15 16:13:59 -0700856 cmd.FlagWithArg("-stubs ", stubsDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700857 }
858
859 if Bool(d.properties.Write_sdk_values) {
Colin Crossab054432019-07-15 16:13:59 -0700860 cmd.FlagWithArg("-sdkvalues ", android.PathForModuleOut(ctx, "out").String())
Nan Zhanga40da042018-08-01 12:48:00 -0700861 }
Nan Zhanga40da042018-08-01 12:48:00 -0700862}
863
Colin Crossab054432019-07-15 16:13:59 -0700864func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700865 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700866 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
867 rule.Command().Text("cp").
868 Input(staticDocIndexRedirect).
869 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700870 }
871
872 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700873 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
874 rule.Command().Text("cp").
875 Input(staticDocProperties).
876 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700877 }
Nan Zhanga40da042018-08-01 12:48:00 -0700878}
879
Colin Crossab054432019-07-15 16:13:59 -0700880func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700881 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700882
883 cmd := rule.Command().
884 BuiltTool(ctx, "soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
885 Flag(config.JavacVmFlags).
886 FlagWithArg("-encoding ", "UTF-8").
Colin Crossab054432019-07-15 16:13:59 -0700887 FlagWithRspFileInputList("@", srcs).
888 FlagWithInput("@", srcJarList)
889
Colin Crossab054432019-07-15 16:13:59 -0700890 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
891 // based stubs generation.
892 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
893 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
894 // the correct package name base path.
895 if len(sourcepaths) > 0 {
896 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
897 } else {
898 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
899 }
900
901 cmd.FlagWithArg("-d ", outDir.String()).
902 Flag("-quiet")
903
904 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700905}
906
Colin Crossdaa4c672019-07-15 22:53:46 -0700907func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
908 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
909 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
910
911 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
912
913 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
914 cmd.Flag(flag).Implicits(deps)
915
916 cmd.FlagWithArg("--patch-module ", "java.base=.")
917
918 if len(classpath) > 0 {
919 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
920 }
921
922 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700923}
924
Colin Crossdaa4c672019-07-15 22:53:46 -0700925func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
926 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
927 sourcepaths android.Paths) *android.RuleBuilderCommand {
928
929 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
930
931 if len(bootclasspath) == 0 && ctx.Device() {
932 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
933 // ensure java does not fall back to the default bootclasspath.
934 cmd.FlagWithArg("-bootclasspath ", `""`)
935 } else if len(bootclasspath) > 0 {
936 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
937 }
938
939 if len(classpath) > 0 {
940 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
941 }
942
943 return cmd
944}
945
Colin Crossab054432019-07-15 16:13:59 -0700946func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
947 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700948
Colin Crossab054432019-07-15 16:13:59 -0700949 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
950 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
951
952 return rule.Command().
953 BuiltTool(ctx, "dokka").
954 Flag(config.JavacVmFlags).
955 Flag(srcJarDir.String()).
956 FlagWithInputList("-classpath ", dokkaClasspath, ":").
957 FlagWithArg("-format ", "dac").
958 FlagWithArg("-dacRoot ", "/reference/kotlin").
959 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700960}
961
962func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
963 deps := d.Javadoc.collectDeps(ctx)
964
Colin Crossdaa4c672019-07-15 22:53:46 -0700965 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
966 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
967
Nan Zhang1598a9e2018-09-04 17:14:32 -0700968 jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
969 doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
970 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
971 checkApiClasspath := classpath{jsilver, doclava, android.PathForSource(ctx, java8Home, "lib/tools.jar")}
972
Colin Crossab054432019-07-15 16:13:59 -0700973 outDir := android.PathForModuleOut(ctx, "out")
974 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
975 stubsDir := android.PathForModuleOut(ctx, "stubsDir")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700976
Colin Crossab054432019-07-15 16:13:59 -0700977 rule := android.NewRuleBuilder()
Nan Zhang1598a9e2018-09-04 17:14:32 -0700978
Colin Crossab054432019-07-15 16:13:59 -0700979 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
980 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700981
Colin Crossab054432019-07-15 16:13:59 -0700982 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
983
984 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -0700985 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -0700986 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700987 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -0700988 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -0700989 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700990 }
991
Colin Crossab054432019-07-15 16:13:59 -0700992 d.stubsFlags(ctx, cmd, stubsDir)
993
994 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
995
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000996 if d.properties.Compat_config != nil {
997 compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
998 cmd.FlagWithInput("-compatconfig ", compatConfig)
999 }
1000
Colin Crossab054432019-07-15 16:13:59 -07001001 var desc string
1002 if Bool(d.properties.Dokka_enabled) {
1003 desc = "dokka"
1004 } else {
1005 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
1006
1007 for _, o := range d.Javadoc.properties.Out {
1008 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1009 }
1010
1011 d.postDoclavaCmds(ctx, rule)
1012 desc = "doclava"
1013 }
1014
1015 rule.Command().
1016 BuiltTool(ctx, "soong_zip").
1017 Flag("-write_if_changed").
1018 Flag("-d").
1019 FlagWithOutput("-o ", d.docZip).
1020 FlagWithArg("-C ", outDir.String()).
1021 FlagWithArg("-D ", outDir.String())
1022
1023 rule.Command().
1024 BuiltTool(ctx, "soong_zip").
1025 Flag("-write_if_changed").
1026 Flag("-jar").
1027 FlagWithOutput("-o ", d.stubsSrcJar).
1028 FlagWithArg("-C ", stubsDir.String()).
1029 FlagWithArg("-D ", stubsDir.String())
1030
1031 rule.Restat()
1032
1033 zipSyncCleanupCmd(rule, srcJarDir)
1034
1035 rule.Build(pctx, ctx, "javadoc", desc)
1036
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001037 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001038 !ctx.Config().IsPdkBuild() {
Colin Crossab054432019-07-15 16:13:59 -07001039
1040 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1041 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Nan Zhang1598a9e2018-09-04 17:14:32 -07001042
1043 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001044
1045 rule := android.NewRuleBuilder()
1046
1047 rule.Command().Text("( true")
1048
1049 rule.Command().
1050 BuiltTool(ctx, "apicheck").
1051 Flag("-JXmx1024m").
1052 FlagWithInputList("-Jclasspath\\ ", checkApiClasspath.Paths(), ":").
1053 OptionalFlag(d.properties.Check_api.Current.Args).
1054 Input(apiFile).
1055 Input(d.apiFile).
1056 Input(removedApiFile).
1057 Input(d.removedApiFile)
1058
1059 msg := fmt.Sprintf(`\n******************************\n`+
1060 `You have tried to change the API from what has been previously approved.\n\n`+
1061 `To make these errors go away, you have two choices:\n`+
1062 ` 1. You can add '@hide' javadoc comments to the methods, etc. listed in the\n`+
1063 ` errors above.\n\n`+
1064 ` 2. You can update current.txt by executing the following command:\n`+
1065 ` make %s-update-current-api\n\n`+
1066 ` To submit the revised current.txt to the main Android repository,\n`+
1067 ` you will need approval.\n`+
1068 `******************************\n`, ctx.ModuleName())
1069
1070 rule.Command().
1071 Text("touch").Output(d.checkCurrentApiTimestamp).
1072 Text(") || (").
1073 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1074 Text("; exit 38").
1075 Text(")")
1076
1077 rule.Build(pctx, ctx, "doclavaCurrentApiCheck", "check current API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001078
1079 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001080
1081 // update API rule
1082 rule = android.NewRuleBuilder()
1083
1084 rule.Command().Text("( true")
1085
1086 rule.Command().
1087 Text("cp").Flag("-f").
1088 Input(d.apiFile).Flag(apiFile.String())
1089
1090 rule.Command().
1091 Text("cp").Flag("-f").
1092 Input(d.removedApiFile).Flag(removedApiFile.String())
1093
1094 msg = "failed to update public API"
1095
1096 rule.Command().
1097 Text("touch").Output(d.updateCurrentApiTimestamp).
1098 Text(") || (").
1099 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1100 Text("; exit 38").
1101 Text(")")
1102
1103 rule.Build(pctx, ctx, "doclavaCurrentApiUpdate", "update current API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001104 }
1105
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001106 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001107 !ctx.Config().IsPdkBuild() {
Colin Crossab054432019-07-15 16:13:59 -07001108
1109 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1110 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
Nan Zhang1598a9e2018-09-04 17:14:32 -07001111
1112 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001113
1114 rule := android.NewRuleBuilder()
1115
1116 rule.Command().
1117 Text("(").
1118 BuiltTool(ctx, "apicheck").
1119 Flag("-JXmx1024m").
1120 FlagWithInputList("-Jclasspath\\ ", checkApiClasspath.Paths(), ":").
1121 OptionalFlag(d.properties.Check_api.Last_released.Args).
1122 Input(apiFile).
1123 Input(d.apiFile).
1124 Input(removedApiFile).
1125 Input(d.removedApiFile)
1126
1127 msg := `\n******************************\n` +
1128 `You have tried to change the API from what has been previously released in\n` +
1129 `an SDK. Please fix the errors listed above.\n` +
1130 `******************************\n`
1131
1132 rule.Command().
1133 Text("touch").Output(d.checkLastReleasedApiTimestamp).
1134 Text(") || (").
1135 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1136 Text("; exit 38").
1137 Text(")")
1138
1139 rule.Build(pctx, ctx, "doclavaLastApiCheck", "check last API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001140 }
1141}
1142
1143//
1144// Droidstubs
1145//
1146type Droidstubs struct {
1147 Javadoc
Paul Duffin91547182019-11-12 19:39:36 +00001148 android.SdkBase
Nan Zhang1598a9e2018-09-04 17:14:32 -07001149
Pete Gillin581d6082018-10-22 15:55:04 +01001150 properties DroidstubsProperties
1151 apiFile android.WritablePath
1152 apiXmlFile android.WritablePath
1153 lastReleasedApiXmlFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001154 privateApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001155 removedApiFile android.WritablePath
1156 removedDexApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001157 nullabilityWarningsFile android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001158
1159 checkCurrentApiTimestamp android.WritablePath
1160 updateCurrentApiTimestamp android.WritablePath
1161 checkLastReleasedApiTimestamp android.WritablePath
Adrian Roos075eedc2019-10-10 12:07:03 +02001162 apiLintTimestamp android.WritablePath
Adrian Roos3b8f1cd2019-11-01 13:42:39 +01001163 apiLintReport android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001164
Pete Gillin581d6082018-10-22 15:55:04 +01001165 checkNullabilityWarningsTimestamp android.WritablePath
1166
Nan Zhang1598a9e2018-09-04 17:14:32 -07001167 annotationsZip android.WritablePath
Nan Zhang9c69a122018-08-22 10:22:08 -07001168 apiVersionsXml android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001169
1170 apiFilePath android.Path
Nan Zhang71bbe632018-09-17 14:32:21 -07001171
1172 jdiffDocZip android.WritablePath
1173 jdiffStubsSrcJar android.WritablePath
Jerome Gaillard0f599032019-10-10 19:29:11 +01001174
1175 metadataZip android.WritablePath
1176 metadataDir android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001177}
1178
Colin Crossa3002fc2019-07-08 16:48:04 -07001179// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
1180// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to
1181// a droiddoc module to generate documentation.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001182func DroidstubsFactory() android.Module {
1183 module := &Droidstubs{}
1184
1185 module.AddProperties(&module.properties,
1186 &module.Javadoc.properties)
1187
1188 InitDroiddocModule(module, android.HostAndDeviceSupported)
Paul Duffin91547182019-11-12 19:39:36 +00001189 android.InitSdkAwareModule(module)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001190 return module
1191}
1192
Colin Crossa3002fc2019-07-08 16:48:04 -07001193// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API
1194// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be
1195// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs
1196// module when symbols needed by the source files are provided by java_library_host modules.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001197func DroidstubsHostFactory() android.Module {
1198 module := &Droidstubs{}
1199
1200 module.AddProperties(&module.properties,
1201 &module.Javadoc.properties)
1202
1203 InitDroiddocModule(module, android.HostSupported)
1204 return module
1205}
1206
1207func (d *Droidstubs) ApiFilePath() android.Path {
1208 return d.apiFilePath
1209}
1210
Paul Duffin75dcc802020-04-09 01:08:11 +01001211func (d *Droidstubs) RemovedApiFilePath() android.Path {
1212 return d.removedApiFile
1213}
1214
Paul Duffinf488ef22020-04-09 00:10:17 +01001215func (d *Droidstubs) StubsSrcJar() android.Path {
1216 return d.stubsSrcJar
1217}
1218
Nan Zhang1598a9e2018-09-04 17:14:32 -07001219func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
1220 d.Javadoc.addDeps(ctx)
1221
Paul Duffin8986cc92020-05-10 19:32:20 +01001222 // If requested clear any properties that provide information about the latest version
1223 // of an API and which reference non-existent modules.
Inseob Kim38449af2019-02-28 14:24:05 +09001224 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
1225 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
Paul Duffin8986cc92020-05-10 19:32:20 +01001226
1227 // If the new_since references a module, e.g. :module-latest-api and the module
1228 // does not exist then clear it.
1229 newSinceSrc := d.properties.Check_api.Api_lint.New_since
1230 newSinceSrcModule := android.SrcIsModule(proptools.String(newSinceSrc))
1231 if newSinceSrcModule != "" && !ctx.OtherModuleExists(newSinceSrcModule) {
1232 d.properties.Check_api.Api_lint.New_since = nil
1233 }
Inseob Kim38449af2019-02-28 14:24:05 +09001234 }
1235
Nan Zhang1598a9e2018-09-04 17:14:32 -07001236 if len(d.properties.Merge_annotations_dirs) != 0 {
1237 for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
1238 ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
1239 }
1240 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001241
Pete Gillin77167902018-09-19 18:16:26 +01001242 if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
1243 for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
1244 ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
1245 }
1246 }
1247
Nan Zhang9c69a122018-08-22 10:22:08 -07001248 if len(d.properties.Api_levels_annotations_dirs) != 0 {
1249 for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
1250 ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
1251 }
1252 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001253}
1254
Paul Duffin455b0bf2020-04-08 18:18:03 +01001255func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001256 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1257 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001258 String(d.properties.Api_filename) != "" {
1259 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001260 cmd.FlagWithOutput("--api ", d.apiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001261 d.apiFilePath = d.apiFile
1262 }
1263
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001264 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1265 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001266 String(d.properties.Removed_api_filename) != "" {
1267 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001268 cmd.FlagWithOutput("--removed-api ", d.removedApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001269 }
1270
Nan Zhang1598a9e2018-09-04 17:14:32 -07001271 if String(d.properties.Removed_dex_api_filename) != "" {
1272 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001273 cmd.FlagWithOutput("--removed-dex-api ", d.removedDexApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001274 }
1275
Nan Zhang9c69a122018-08-22 10:22:08 -07001276 if Bool(d.properties.Write_sdk_values) {
Jerome Gaillard0f599032019-10-10 19:29:11 +01001277 d.metadataDir = android.PathForModuleOut(ctx, "metadata")
1278 cmd.FlagWithArg("--sdk-values ", d.metadataDir.String())
Nan Zhang9c69a122018-08-22 10:22:08 -07001279 }
1280
Paul Duffin455b0bf2020-04-08 18:18:03 +01001281 if stubsDir.Valid() {
1282 if Bool(d.properties.Create_doc_stubs) {
1283 cmd.FlagWithArg("--doc-stubs ", stubsDir.String())
1284 } else {
1285 cmd.FlagWithArg("--stubs ", stubsDir.String())
1286 cmd.Flag("--exclude-documentation-from-stubs")
1287 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001288 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001289}
1290
Colin Cross33961b52019-07-11 11:01:22 -07001291func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang1598a9e2018-09-04 17:14:32 -07001292 if Bool(d.properties.Annotations_enabled) {
Colin Cross33961b52019-07-11 11:01:22 -07001293 cmd.Flag("--include-annotations")
1294
Pete Gillinc382a562018-11-14 18:45:46 +00001295 validatingNullability :=
1296 strings.Contains(d.Javadoc.args, "--validate-nullability-from-merged-stubs") ||
1297 String(d.properties.Validate_nullability_from_list) != ""
Paul Duffin13a9dd62019-11-04 10:26:47 +00001298
Pete Gillina262c052018-09-14 14:25:48 +01001299 migratingNullability := String(d.properties.Previous_api) != ""
Pete Gillina262c052018-09-14 14:25:48 +01001300 if migratingNullability {
Colin Cross8a497952019-03-05 22:25:09 -08001301 previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
Colin Cross33961b52019-07-11 11:01:22 -07001302 cmd.FlagWithInput("--migrate-nullness ", previousApi)
Pete Gillina262c052018-09-14 14:25:48 +01001303 }
Colin Cross33961b52019-07-11 11:01:22 -07001304
Pete Gillinc382a562018-11-14 18:45:46 +00001305 if s := String(d.properties.Validate_nullability_from_list); s != "" {
Colin Cross33961b52019-07-11 11:01:22 -07001306 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
Pete Gillinc382a562018-11-14 18:45:46 +00001307 }
Colin Cross33961b52019-07-11 11:01:22 -07001308
Pete Gillina262c052018-09-14 14:25:48 +01001309 if validatingNullability {
Pete Gillin581d6082018-10-22 15:55:04 +01001310 d.nullabilityWarningsFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_nullability_warnings.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001311 cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile)
Pete Gillina262c052018-09-14 14:25:48 +01001312 }
Nan Zhanga40da042018-08-01 12:48:00 -07001313
1314 d.annotationsZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"_annotations.zip")
Colin Cross33961b52019-07-11 11:01:22 -07001315 cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip)
Nan Zhangf4936b02018-08-01 15:00:28 -07001316
Anton Hanssonc5e13272020-05-21 10:11:31 +01001317 if len(d.properties.Merge_annotations_dirs) != 0 {
1318 d.mergeAnnoDirFlags(ctx, cmd)
Nan Zhanga40da042018-08-01 12:48:00 -07001319 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001320
Colin Cross33961b52019-07-11 11:01:22 -07001321 // TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
1322 cmd.FlagWithArg("--hide ", "HiddenTypedefConstant").
1323 FlagWithArg("--hide ", "SuperfluousPrefix").
1324 FlagWithArg("--hide ", "AnnotationExtraction")
1325 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001326}
1327
Colin Cross33961b52019-07-11 11:01:22 -07001328func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
1329 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
1330 if t, ok := m.(*ExportedDroiddocDir); ok {
1331 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
1332 } else {
1333 ctx.PropertyErrorf("merge_annotations_dirs",
1334 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1335 }
1336 })
1337}
1338
1339func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Pete Gillin77167902018-09-19 18:16:26 +01001340 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
1341 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Cross33961b52019-07-11 11:01:22 -07001342 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
Pete Gillin77167902018-09-19 18:16:26 +01001343 } else {
1344 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
1345 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1346 }
1347 })
Nan Zhanga40da042018-08-01 12:48:00 -07001348}
1349
Colin Cross33961b52019-07-11 11:01:22 -07001350func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang9c69a122018-08-22 10:22:08 -07001351 if Bool(d.properties.Api_levels_annotations_enabled) {
1352 d.apiVersionsXml = android.PathForModuleOut(ctx, "api-versions.xml")
Nan Zhang9c69a122018-08-22 10:22:08 -07001353
1354 if len(d.properties.Api_levels_annotations_dirs) == 0 {
1355 ctx.PropertyErrorf("api_levels_annotations_dirs",
1356 "has to be non-empty if api levels annotations was enabled!")
1357 }
1358
Colin Cross33961b52019-07-11 11:01:22 -07001359 cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml)
1360 cmd.FlagWithInput("--apply-api-levels ", d.apiVersionsXml)
1361 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion())
1362 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
Nan Zhang9c69a122018-08-22 10:22:08 -07001363
1364 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
1365 if t, ok := m.(*ExportedDroiddocDir); ok {
Nan Zhang9c69a122018-08-22 10:22:08 -07001366 for _, dep := range t.deps {
1367 if strings.HasSuffix(dep.String(), "android.jar") {
Colin Cross33961b52019-07-11 11:01:22 -07001368 cmd.Implicit(dep)
Nan Zhang9c69a122018-08-22 10:22:08 -07001369 }
1370 }
Colin Cross33961b52019-07-11 11:01:22 -07001371 cmd.FlagWithArg("--android-jar-pattern ", t.dir.String()+"/%/public/android.jar")
Nan Zhang9c69a122018-08-22 10:22:08 -07001372 } else {
1373 ctx.PropertyErrorf("api_levels_annotations_dirs",
1374 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
1375 }
1376 })
1377
1378 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001379}
1380
Colin Cross33961b52019-07-11 11:01:22 -07001381func (d *Droidstubs) apiToXmlFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang71bbe632018-09-17 14:32:21 -07001382 if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() {
1383 if d.apiFile.String() == "" {
1384 ctx.ModuleErrorf("API signature file has to be specified in Metalava when jdiff is enabled.")
1385 }
1386
1387 d.apiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001388 cmd.FlagWithOutput("--api-xml ", d.apiXmlFile)
Nan Zhang71bbe632018-09-17 14:32:21 -07001389
1390 if String(d.properties.Check_api.Last_released.Api_file) == "" {
1391 ctx.PropertyErrorf("check_api.last_released.api_file",
1392 "has to be non-empty if jdiff was enabled!")
1393 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001394
Colin Cross33961b52019-07-11 11:01:22 -07001395 lastReleasedApi := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
Nan Zhang71bbe632018-09-17 14:32:21 -07001396 d.lastReleasedApiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_last_released_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001397 cmd.FlagWithInput("--convert-to-jdiff ", lastReleasedApi).Output(d.lastReleasedApiXmlFile)
1398 }
1399}
Nan Zhang71bbe632018-09-17 14:32:21 -07001400
Colin Cross1e743852019-10-28 11:37:20 -07001401func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths,
Colin Cross33961b52019-07-11 11:01:22 -07001402 srcJarList android.Path, bootclasspath, classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Cross8b8bec32019-11-15 13:18:43 -08001403 // Metalava uses lots of memory, restrict the number of metalava jobs that can run in parallel.
1404 rule.HighMem()
Ramy Medhat1fb3cd82020-05-05 22:50:09 +00001405 cmd := rule.Command()
1406 if ctx.Config().IsEnvTrue("RBE_METALAVA") {
1407 rule.Remoteable(android.RemoteRuleSupports{RBE: true})
1408 execStrategy := remoteexec.LocalExecStrategy
1409 if v := ctx.Config().Getenv("RBE_METALAVA_EXEC_STRATEGY"); v != "" {
1410 execStrategy = v
1411 }
1412 pool := "metalava"
1413 if v := ctx.Config().Getenv("RBE_METALAVA_POOL"); v != "" {
1414 pool = v
1415 }
1416 inputs := []string{android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "metalava.jar").String()}
1417 if v := ctx.Config().Getenv("RBE_METALAVA_INPUTS"); v != "" {
1418 inputs = append(inputs, strings.Split(v, ",")...)
1419 }
1420 cmd.Text((&remoteexec.REParams{
1421 Labels: map[string]string{"type": "compile", "lang": "java", "compiler": "metalava"},
1422 ExecStrategy: execStrategy,
1423 Inputs: inputs,
1424 ToolchainInputs: []string{config.JavaCmd(ctx).String()},
1425 Platform: map[string]string{remoteexec.PoolKey: pool},
1426 }).NoVarTemplate(ctx.Config()))
1427 }
1428
1429 cmd.BuiltTool(ctx, "metalava").
Colin Cross33961b52019-07-11 11:01:22 -07001430 Flag(config.JavacVmFlags).
1431 FlagWithArg("-encoding ", "UTF-8").
Colin Cross1e743852019-10-28 11:37:20 -07001432 FlagWithArg("-source ", javaVersion.String()).
Colin Cross33961b52019-07-11 11:01:22 -07001433 FlagWithRspFileInputList("@", srcs).
1434 FlagWithInput("@", srcJarList)
1435
1436 if len(bootclasspath) > 0 {
1437 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
Nan Zhang71bbe632018-09-17 14:32:21 -07001438 }
1439
Colin Cross33961b52019-07-11 11:01:22 -07001440 if len(classpath) > 0 {
1441 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
1442 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001443
Colin Cross33961b52019-07-11 11:01:22 -07001444 if len(sourcepaths) > 0 {
1445 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
1446 } else {
1447 cmd.FlagWithArg("-sourcepath ", `""`)
1448 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001449
Colin Cross33961b52019-07-11 11:01:22 -07001450 cmd.Flag("--no-banner").
1451 Flag("--color").
1452 Flag("--quiet").
1453 Flag("--format=v2")
Nan Zhang86d2d552018-08-09 15:33:27 -07001454
Colin Cross33961b52019-07-11 11:01:22 -07001455 return cmd
Nan Zhang71bbe632018-09-17 14:32:21 -07001456}
1457
Nan Zhang1598a9e2018-09-04 17:14:32 -07001458func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga40da042018-08-01 12:48:00 -07001459 deps := d.Javadoc.collectDeps(ctx)
1460
1461 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), sdkContext(d))
Nan Zhang581fd212018-01-10 16:06:12 -08001462
Colin Cross33961b52019-07-11 11:01:22 -07001463 // Create rule for metalava
Nan Zhanga40da042018-08-01 12:48:00 -07001464
Colin Cross33961b52019-07-11 11:01:22 -07001465 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang71bbe632018-09-17 14:32:21 -07001466
Colin Cross33961b52019-07-11 11:01:22 -07001467 rule := android.NewRuleBuilder()
Nan Zhanga40da042018-08-01 12:48:00 -07001468
Paul Duffin455b0bf2020-04-08 18:18:03 +01001469 generateStubs := BoolDefault(d.properties.Generate_stubs, true)
1470 var stubsDir android.OptionalPath
1471 if generateStubs {
1472 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1473 stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "stubsDir"))
1474 rule.Command().Text("rm -rf").Text(stubsDir.String())
1475 rule.Command().Text("mkdir -p").Text(stubsDir.String())
1476 }
Nan Zhanga40da042018-08-01 12:48:00 -07001477
Colin Cross33961b52019-07-11 11:01:22 -07001478 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1479
1480 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
1481 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
1482
1483 d.stubsFlags(ctx, cmd, stubsDir)
1484
1485 d.annotationsFlags(ctx, cmd)
1486 d.inclusionAnnotationsFlags(ctx, cmd)
1487 d.apiLevelsAnnotationsFlags(ctx, cmd)
1488 d.apiToXmlFlags(ctx, cmd)
Nan Zhang71bbe632018-09-17 14:32:21 -07001489
Nan Zhang1598a9e2018-09-04 17:14:32 -07001490 if strings.Contains(d.Javadoc.args, "--generate-documentation") {
1491 // Currently Metalava have the ability to invoke Javadoc in a seperate process.
1492 // Pass "-nodocs" to suppress the Javadoc invocation when Metalava receives
1493 // "--generate-documentation" arg. This is not needed when Metalava removes this feature.
1494 d.Javadoc.args = d.Javadoc.args + " -nodocs "
Nan Zhang79614d12018-04-19 18:03:39 -07001495 }
Colin Cross33961b52019-07-11 11:01:22 -07001496
1497 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1498 for _, o := range d.Javadoc.properties.Out {
1499 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1500 }
1501
Makoto Onukib850a9d2020-04-27 17:22:16 -07001502 // Add options for the other optional tasks: API-lint and check-released.
1503 // We generate separate timestamp files for them.
1504
1505 doApiLint := false
1506 doCheckReleased := false
1507
1508 // Add API lint options.
1509
1510 if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) && !ctx.Config().IsPdkBuild() {
1511 doApiLint = true
1512
1513 newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since)
1514 if newSince.Valid() {
1515 cmd.FlagWithInput("--api-lint ", newSince.Path())
1516 } else {
1517 cmd.Flag("--api-lint")
1518 }
1519 d.apiLintReport = android.PathForModuleOut(ctx, "api_lint_report.txt")
1520 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport) // TODO: Change to ":api-lint"
1521
1522 // TODO(b/154317059): Clean up this whitelist by baselining and/or checking in last-released.
1523 if d.Name() != "android.car-system-stubs-docs" &&
1524 d.Name() != "android.car-stubs-docs" &&
1525 d.Name() != "system-api-stubs-docs" &&
1526 d.Name() != "test-api-stubs-docs" {
1527 cmd.Flag("--lints-as-errors")
1528 cmd.Flag("--warnings-as-errors") // Most lints are actually warnings.
1529 }
1530
1531 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
1532 updatedBaselineOutput := android.PathForModuleOut(ctx, "api_lint_baseline.txt")
1533 d.apiLintTimestamp = android.PathForModuleOut(ctx, "api_lint.timestamp")
1534
1535 // Note this string includes a special shell quote $' ... ', which decodes the "\n"s.
1536 // However, because $' ... ' doesn't expand environmental variables, we can't just embed
1537 // $PWD, so we have to terminate $'...', use "$PWD", then start $' ... ' again,
1538 // which is why we have '"$PWD"$' in it.
1539 //
1540 // TODO: metalava also has a slightly different message hardcoded. Should we unify this
1541 // message and metalava's one?
1542 msg := `$'` + // Enclose with $' ... '
1543 `************************************************************\n` +
1544 `Your API changes are triggering API Lint warnings or errors.\n` +
1545 `To make these errors go away, fix the code according to the\n` +
1546 `error and/or warning messages above.\n` +
1547 `\n` +
1548 `If it is not possible to do so, there are workarounds:\n` +
1549 `\n` +
1550 `1. You can suppress the errors with @SuppressLint("<id>")\n`
1551
1552 if baselineFile.Valid() {
1553 cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path())
1554 cmd.FlagWithOutput("--update-baseline:api-lint ", updatedBaselineOutput)
1555
1556 msg += fmt.Sprintf(``+
1557 `2. You can update the baseline by executing the following\n`+
1558 ` command:\n`+
Anton Hansson18a28952020-05-11 15:38:31 +01001559 ` cp \\\n`+
1560 ` "'"$PWD"$'/%s" \\\n`+
1561 ` "'"$PWD"$'/%s"\n`+
Makoto Onukib850a9d2020-04-27 17:22:16 -07001562 ` To submit the revised baseline.txt to the main Android\n`+
1563 ` repository, you will need approval.\n`, updatedBaselineOutput, baselineFile.Path())
1564 } else {
1565 msg += fmt.Sprintf(``+
1566 `2. You can add a baseline file of existing lint failures\n`+
1567 ` to the build rule of %s.\n`, d.Name())
1568 }
1569 // Note the message ends with a ' (single quote), to close the $' ... ' .
1570 msg += `************************************************************\n'`
1571
1572 cmd.FlagWithArg("--error-message:api-lint ", msg)
1573 }
1574
1575 // Add "check released" options. (Detect incompatible API changes from the last public release)
1576
1577 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") &&
1578 !ctx.Config().IsPdkBuild() {
1579 doCheckReleased = true
1580
1581 if len(d.Javadoc.properties.Out) > 0 {
1582 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1583 }
1584
1585 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1586 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
1587 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
1588 updatedBaselineOutput := android.PathForModuleOut(ctx, "last_released_baseline.txt")
1589
1590 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
1591
1592 cmd.FlagWithInput("--check-compatibility:api:released ", apiFile)
1593 cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile)
1594
1595 if baselineFile.Valid() {
1596 cmd.FlagWithInput("--baseline:compatibility:released ", baselineFile.Path())
1597 cmd.FlagWithOutput("--update-baseline:compatibility:released ", updatedBaselineOutput)
1598 }
1599
1600 // Note this string includes quote ($' ... '), which decodes the "\n"s.
1601 msg := `$'\n******************************\n` +
1602 `You have tried to change the API from what has been previously released in\n` +
1603 `an SDK. Please fix the errors listed above.\n` +
1604 `******************************\n'`
1605
1606 cmd.FlagWithArg("--error-message:compatibility:released ", msg)
1607 }
1608
Paul Duffin455b0bf2020-04-08 18:18:03 +01001609 if generateStubs {
1610 rule.Command().
1611 BuiltTool(ctx, "soong_zip").
1612 Flag("-write_if_changed").
1613 Flag("-jar").
1614 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
1615 FlagWithArg("-C ", stubsDir.String()).
1616 FlagWithArg("-D ", stubsDir.String())
1617 }
Jerome Gaillard0f599032019-10-10 19:29:11 +01001618
1619 if Bool(d.properties.Write_sdk_values) {
1620 d.metadataZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-metadata.zip")
1621 rule.Command().
1622 BuiltTool(ctx, "soong_zip").
1623 Flag("-write_if_changed").
1624 Flag("-d").
1625 FlagWithOutput("-o ", d.metadataZip).
1626 FlagWithArg("-C ", d.metadataDir.String()).
1627 FlagWithArg("-D ", d.metadataDir.String())
1628 }
1629
Makoto Onukib850a9d2020-04-27 17:22:16 -07001630 // TODO: We don't really need two separate API files, but this is a reminiscence of how
1631 // we used to run metalava separately for API lint and the "last_released" check. Unify them.
1632 if doApiLint {
1633 rule.Command().Text("touch").Output(d.apiLintTimestamp)
1634 }
1635 if doCheckReleased {
1636 rule.Command().Text("touch").Output(d.checkLastReleasedApiTimestamp)
1637 }
1638
Colin Cross33961b52019-07-11 11:01:22 -07001639 rule.Restat()
1640
1641 zipSyncCleanupCmd(rule, srcJarDir)
1642
Makoto Onukib850a9d2020-04-27 17:22:16 -07001643 rule.Build(pctx, ctx, "metalava", "metalava merged")
Adrian Roos075eedc2019-10-10 12:07:03 +02001644
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001645 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001646 !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001647
1648 if len(d.Javadoc.properties.Out) > 0 {
1649 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1650 }
1651
1652 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1653 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Adrian Roos14f75a92019-08-12 17:54:09 +02001654 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001655
1656 if baselineFile.Valid() {
Makoto Onukib850a9d2020-04-27 17:22:16 -07001657 ctx.PropertyErrorf("baseline_file", "current API check can't have a baseline file. (module %s)", ctx.ModuleName())
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001658 }
Nan Zhang61819ce2018-05-04 18:49:16 -07001659
Nan Zhang2760dfc2018-08-24 17:32:54 +00001660 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Nan Zhang2760dfc2018-08-24 17:32:54 +00001661
Colin Cross33961b52019-07-11 11:01:22 -07001662 rule := android.NewRuleBuilder()
1663
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001664 // Diff command line.
Makoto Onukib850a9d2020-04-27 17:22:16 -07001665 // -F matches the closest "opening" line, such as "package android {"
1666 // and " public class Intent {".
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001667 diff := `diff -u -F '{ *$'`
1668
Colin Cross33961b52019-07-11 11:01:22 -07001669 rule.Command().Text("( true")
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001670 rule.Command().
1671 Text(diff).
1672 Input(apiFile).Input(d.apiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001673
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001674 rule.Command().
1675 Text(diff).
1676 Input(removedApiFile).Input(d.removedApiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001677
1678 msg := fmt.Sprintf(`\n******************************\n`+
1679 `You have tried to change the API from what has been previously approved.\n\n`+
1680 `To make these errors go away, you have two choices:\n`+
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001681 ` 1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)\n`+
1682 ` to the new methods, etc. shown in the above diff.\n\n`+
1683 ` 2. You can update current.txt and/or removed.txt by executing the following command:\n`+
Colin Cross33961b52019-07-11 11:01:22 -07001684 ` make %s-update-current-api\n\n`+
1685 ` To submit the revised current.txt to the main Android repository,\n`+
1686 ` you will need approval.\n`+
1687 `******************************\n`, ctx.ModuleName())
1688
1689 rule.Command().
1690 Text("touch").Output(d.checkCurrentApiTimestamp).
1691 Text(") || (").
1692 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1693 Text("; exit 38").
1694 Text(")")
1695
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001696 rule.Build(pctx, ctx, "metalavaCurrentApiCheck", "check current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001697
1698 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001699
1700 // update API rule
1701 rule = android.NewRuleBuilder()
1702
1703 rule.Command().Text("( true")
1704
1705 rule.Command().
1706 Text("cp").Flag("-f").
1707 Input(d.apiFile).Flag(apiFile.String())
1708
1709 rule.Command().
1710 Text("cp").Flag("-f").
1711 Input(d.removedApiFile).Flag(removedApiFile.String())
1712
1713 msg = "failed to update public API"
1714
1715 rule.Command().
1716 Text("touch").Output(d.updateCurrentApiTimestamp).
1717 Text(") || (").
1718 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1719 Text("; exit 38").
1720 Text(")")
1721
1722 rule.Build(pctx, ctx, "metalavaCurrentApiUpdate", "update current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001723 }
Nan Zhanga40da042018-08-01 12:48:00 -07001724
Pete Gillin581d6082018-10-22 15:55:04 +01001725 if String(d.properties.Check_nullability_warnings) != "" {
1726 if d.nullabilityWarningsFile == nil {
1727 ctx.PropertyErrorf("check_nullability_warnings",
1728 "Cannot specify check_nullability_warnings unless validating nullability")
1729 }
Colin Cross33961b52019-07-11 11:01:22 -07001730
1731 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
1732
Pete Gillin581d6082018-10-22 15:55:04 +01001733 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "check_nullability_warnings.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001734
Pete Gillin581d6082018-10-22 15:55:04 +01001735 msg := fmt.Sprintf(`\n******************************\n`+
1736 `The warnings encountered during nullability annotation validation did\n`+
1737 `not match the checked in file of expected warnings. The diffs are shown\n`+
1738 `above. You have two options:\n`+
1739 ` 1. Resolve the differences by editing the nullability annotations.\n`+
1740 ` 2. Update the file of expected warnings by running:\n`+
1741 ` cp %s %s\n`+
1742 ` and submitting the updated file as part of your change.`,
1743 d.nullabilityWarningsFile, checkNullabilityWarnings)
Colin Cross33961b52019-07-11 11:01:22 -07001744
1745 rule := android.NewRuleBuilder()
1746
1747 rule.Command().
1748 Text("(").
1749 Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile).
1750 Text("&&").
1751 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
1752 Text(") || (").
1753 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1754 Text("; exit 38").
1755 Text(")")
1756
1757 rule.Build(pctx, ctx, "nullabilityWarningsCheck", "nullability warnings check")
Pete Gillin581d6082018-10-22 15:55:04 +01001758 }
1759
Nan Zhang71bbe632018-09-17 14:32:21 -07001760 if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001761 if len(d.Javadoc.properties.Out) > 0 {
1762 ctx.PropertyErrorf("out", "out property may not be combined with jdiff")
1763 }
1764
1765 outDir := android.PathForModuleOut(ctx, "jdiff-out")
1766 srcJarDir := android.PathForModuleOut(ctx, "jdiff-srcjars")
1767 stubsDir := android.PathForModuleOut(ctx, "jdiff-stubsDir")
1768
1769 rule := android.NewRuleBuilder()
Nan Zhang71bbe632018-09-17 14:32:21 -07001770
Nan Zhang86b06202018-09-21 17:09:21 -07001771 // Please sync with android-api-council@ before making any changes for the name of jdiffDocZip below
1772 // since there's cron job downstream that fetch this .zip file periodically.
1773 // See b/116221385 for reference.
Nan Zhang71bbe632018-09-17 14:32:21 -07001774 d.jdiffDocZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-docs.zip")
1775 d.jdiffStubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-stubs.srcjar")
1776
Nan Zhang71bbe632018-09-17 14:32:21 -07001777 jdiff := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jdiff.jar")
Nan Zhang71bbe632018-09-17 14:32:21 -07001778
Colin Cross33961b52019-07-11 11:01:22 -07001779 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
1780 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang71bbe632018-09-17 14:32:21 -07001781
Colin Cross33961b52019-07-11 11:01:22 -07001782 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1783
Colin Crossdaa4c672019-07-15 22:53:46 -07001784 cmd := javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -07001785 deps.bootClasspath, deps.classpath, d.sourcepaths)
1786
1787 cmd.Flag("-J-Xmx1600m").
Colin Cross33961b52019-07-11 11:01:22 -07001788 Flag("-XDignore.symbol.file").
1789 FlagWithArg("-doclet ", "jdiff.JDiff").
1790 FlagWithInput("-docletpath ", jdiff).
1791 Flag("-quiet").
1792 FlagWithArg("-newapi ", strings.TrimSuffix(d.apiXmlFile.Base(), d.apiXmlFile.Ext())).
1793 FlagWithArg("-newapidir ", filepath.Dir(d.apiXmlFile.String())).
1794 Implicit(d.apiXmlFile).
1795 FlagWithArg("-oldapi ", strings.TrimSuffix(d.lastReleasedApiXmlFile.Base(), d.lastReleasedApiXmlFile.Ext())).
1796 FlagWithArg("-oldapidir ", filepath.Dir(d.lastReleasedApiXmlFile.String())).
1797 Implicit(d.lastReleasedApiXmlFile)
1798
Colin Cross33961b52019-07-11 11:01:22 -07001799 rule.Command().
1800 BuiltTool(ctx, "soong_zip").
1801 Flag("-write_if_changed").
1802 Flag("-d").
1803 FlagWithOutput("-o ", d.jdiffDocZip).
1804 FlagWithArg("-C ", outDir.String()).
1805 FlagWithArg("-D ", outDir.String())
1806
1807 rule.Command().
1808 BuiltTool(ctx, "soong_zip").
1809 Flag("-write_if_changed").
1810 Flag("-jar").
1811 FlagWithOutput("-o ", d.jdiffStubsSrcJar).
1812 FlagWithArg("-C ", stubsDir.String()).
1813 FlagWithArg("-D ", stubsDir.String())
1814
1815 rule.Restat()
1816
1817 zipSyncCleanupCmd(rule, srcJarDir)
1818
1819 rule.Build(pctx, ctx, "jdiff", "jdiff")
Nan Zhang71bbe632018-09-17 14:32:21 -07001820 }
Nan Zhang581fd212018-01-10 16:06:12 -08001821}
Dan Willemsencc090972018-02-26 14:33:31 -08001822
Nan Zhanga40da042018-08-01 12:48:00 -07001823//
Nan Zhangf4936b02018-08-01 15:00:28 -07001824// Exported Droiddoc Directory
Nan Zhanga40da042018-08-01 12:48:00 -07001825//
Dan Willemsencc090972018-02-26 14:33:31 -08001826var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
Nan Zhangf4936b02018-08-01 15:00:28 -07001827var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
Pete Gillin77167902018-09-19 18:16:26 +01001828var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
Nan Zhang9c69a122018-08-22 10:22:08 -07001829var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
Dan Willemsencc090972018-02-26 14:33:31 -08001830
Nan Zhangf4936b02018-08-01 15:00:28 -07001831type ExportedDroiddocDirProperties struct {
1832 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -08001833 Path *string
1834}
1835
Nan Zhangf4936b02018-08-01 15:00:28 -07001836type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -08001837 android.ModuleBase
1838
Nan Zhangf4936b02018-08-01 15:00:28 -07001839 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -08001840
1841 deps android.Paths
1842 dir android.Path
1843}
1844
Colin Crossa3002fc2019-07-08 16:48:04 -07001845// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -07001846func ExportedDroiddocDirFactory() android.Module {
1847 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -08001848 module.AddProperties(&module.properties)
1849 android.InitAndroidModule(module)
1850 return module
1851}
1852
Nan Zhangf4936b02018-08-01 15:00:28 -07001853func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -08001854
Nan Zhangf4936b02018-08-01 15:00:28 -07001855func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -08001856 path := String(d.properties.Path)
1857 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -08001858 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Dan Willemsencc090972018-02-26 14:33:31 -08001859}
Nan Zhangb2b33de2018-02-23 11:18:47 -08001860
1861//
1862// Defaults
1863//
1864type DocDefaults struct {
1865 android.ModuleBase
1866 android.DefaultsModuleBase
1867}
1868
Nan Zhangb2b33de2018-02-23 11:18:47 -08001869func DocDefaultsFactory() android.Module {
1870 module := &DocDefaults{}
1871
1872 module.AddProperties(
1873 &JavadocProperties{},
1874 &DroiddocProperties{},
1875 )
1876
1877 android.InitDefaultsModule(module)
1878
1879 return module
1880}
Nan Zhang1598a9e2018-09-04 17:14:32 -07001881
1882func StubsDefaultsFactory() android.Module {
1883 module := &DocDefaults{}
1884
1885 module.AddProperties(
1886 &JavadocProperties{},
1887 &DroidstubsProperties{},
1888 )
1889
1890 android.InitDefaultsModule(module)
1891
1892 return module
1893}
Colin Cross33961b52019-07-11 11:01:22 -07001894
1895func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
1896 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
1897
1898 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1899 rule.Command().Text("mkdir -p").Text(srcJarDir.String())
1900 srcJarList := srcJarDir.Join(ctx, "list")
1901
1902 rule.Temporary(srcJarList)
1903
1904 rule.Command().BuiltTool(ctx, "zipsync").
1905 FlagWithArg("-d ", srcJarDir.String()).
1906 FlagWithOutput("-l ", srcJarList).
1907 FlagWithArg("-f ", `"*.java"`).
1908 Inputs(srcJars)
1909
1910 return srcJarList
1911}
1912
1913func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
1914 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1915}
Paul Duffin91547182019-11-12 19:39:36 +00001916
1917var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
1918
1919type PrebuiltStubsSourcesProperties struct {
1920 Srcs []string `android:"path"`
1921}
1922
1923type PrebuiltStubsSources struct {
1924 android.ModuleBase
1925 android.DefaultableModuleBase
1926 prebuilt android.Prebuilt
1927 android.SdkBase
1928
1929 properties PrebuiltStubsSourcesProperties
1930
Paul Duffin9b478b02019-12-10 13:41:51 +00001931 // The source directories containing stubs source files.
1932 srcDirs android.Paths
Paul Duffin91547182019-11-12 19:39:36 +00001933 stubsSrcJar android.ModuleOutPath
1934}
1935
Paul Duffin9b478b02019-12-10 13:41:51 +00001936func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) {
1937 switch tag {
1938 case "":
1939 return android.Paths{p.stubsSrcJar}, nil
1940 default:
1941 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1942 }
1943}
1944
Paul Duffin533f9c72020-05-20 16:18:00 +01001945func (d *PrebuiltStubsSources) StubsSrcJar() android.Path {
1946 return d.stubsSrcJar
1947}
1948
Paul Duffin91547182019-11-12 19:39:36 +00001949func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin9b478b02019-12-10 13:41:51 +00001950 p.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1951
1952 p.srcDirs = android.PathsForModuleSrc(ctx, p.properties.Srcs)
1953
1954 rule := android.NewRuleBuilder()
1955 command := rule.Command().
1956 BuiltTool(ctx, "soong_zip").
1957 Flag("-write_if_changed").
1958 Flag("-jar").
1959 FlagWithOutput("-o ", p.stubsSrcJar)
1960
1961 for _, d := range p.srcDirs {
1962 dir := d.String()
1963 command.
1964 FlagWithArg("-C ", dir).
1965 FlagWithInput("-D ", d)
1966 }
1967
1968 rule.Restat()
1969
1970 rule.Build(pctx, ctx, "zip src", "Create srcjar from prebuilt source")
Paul Duffin91547182019-11-12 19:39:36 +00001971}
1972
1973func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
1974 return &p.prebuilt
1975}
1976
1977func (p *PrebuiltStubsSources) Name() string {
1978 return p.prebuilt.Name(p.ModuleBase.Name())
1979}
1980
Paul Duffin91547182019-11-12 19:39:36 +00001981// prebuilt_stubs_sources imports a set of java source files as if they were
1982// generated by droidstubs.
1983//
1984// By default, a prebuilt_stubs_sources has a single variant that expects a
1985// set of `.java` files generated by droidstubs.
1986//
1987// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1988// for host modules.
1989//
1990// Intended only for use by sdk snapshots.
1991func PrebuiltStubsSourcesFactory() android.Module {
1992 module := &PrebuiltStubsSources{}
1993
1994 module.AddProperties(&module.properties)
1995
1996 android.InitPrebuiltModule(module, &module.properties.Srcs)
1997 android.InitSdkAwareModule(module)
1998 InitDroiddocModule(module, android.HostAndDeviceSupported)
1999 return module
2000}
2001
Paul Duffin13879572019-11-28 14:31:38 +00002002type droidStubsSdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00002003 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00002004}
2005
2006func (mt *droidStubsSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2007 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2008}
2009
2010func (mt *droidStubsSdkMemberType) IsInstance(module android.Module) bool {
2011 _, ok := module.(*Droidstubs)
2012 return ok
2013}
2014
Paul Duffin93520ed2020-03-20 13:35:40 +00002015func (mt *droidStubsSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2016 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_stubs_sources")
2017}
2018
2019func (mt *droidStubsSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2020 return &droidStubsInfoProperties{}
2021}
2022
2023type droidStubsInfoProperties struct {
2024 android.SdkMemberPropertiesBase
2025
2026 StubsSrcJar android.Path
2027}
2028
2029func (p *droidStubsInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2030 droidstubs := variant.(*Droidstubs)
2031 p.StubsSrcJar = droidstubs.stubsSrcJar
2032}
2033
2034func (p *droidStubsInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
2035 if p.StubsSrcJar != nil {
2036 builder := ctx.SnapshotBuilder()
2037
2038 snapshotRelativeDir := filepath.Join("java", ctx.Name()+"_stubs_sources")
2039
2040 builder.UnzipToSnapshot(p.StubsSrcJar, snapshotRelativeDir)
2041
2042 propertySet.AddProperty("srcs", []string{snapshotRelativeDir})
Paul Duffin13879572019-11-28 14:31:38 +00002043 }
Paul Duffin91547182019-11-12 19:39:36 +00002044}