blob: f62f5f901e1f2e5ebef0fdf7dc7ae9f65c9a9d8e [file] [log] [blame]
Nan Zhang581fd212018-01-10 16:06:12 -08001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Nan Zhang581fd212018-01-10 16:06:12 -080018 "fmt"
Nan Zhangb2b33de2018-02-23 11:18:47 -080019 "path/filepath"
Nan Zhang581fd212018-01-10 16:06:12 -080020 "strings"
21
Paul Duffin13879572019-11-28 14:31:38 +000022 "github.com/google/blueprint"
Jeongik Cha6bd33c12019-06-25 16:26:18 +090023 "github.com/google/blueprint/proptools"
Nan Zhang581fd212018-01-10 16:06:12 -080024
Colin Crossab054432019-07-15 16:13:59 -070025 "android/soong/android"
26 "android/soong/java/config"
Nan Zhang581fd212018-01-10 16:06:12 -080027)
28
29func init() {
Paul Duffin884363e2019-12-19 10:21:09 +000030 RegisterDocsBuildComponents(android.InitRegistrationContext)
31 RegisterStubsBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000032
33 // Register sdk member type.
34 android.RegisterSdkMemberType(&droidStubsSdkMemberType{
35 SdkMemberTypeBase: android.SdkMemberTypeBase{
36 PropertyName: "stubs_sources",
Paul Duffine6029182019-12-16 17:43:48 +000037 // stubs_sources can be used with sdk to provide the source stubs for APIs provided by
38 // the APEX.
39 SupportsSdk: true,
Paul Duffin255f18e2019-12-13 11:22:16 +000040 },
41 })
Nan Zhang581fd212018-01-10 16:06:12 -080042}
43
Paul Duffin884363e2019-12-19 10:21:09 +000044func RegisterDocsBuildComponents(ctx android.RegistrationContext) {
45 ctx.RegisterModuleType("doc_defaults", DocDefaultsFactory)
46
47 ctx.RegisterModuleType("droiddoc", DroiddocFactory)
48 ctx.RegisterModuleType("droiddoc_host", DroiddocHostFactory)
49 ctx.RegisterModuleType("droiddoc_exported_dir", ExportedDroiddocDirFactory)
50 ctx.RegisterModuleType("javadoc", JavadocFactory)
51 ctx.RegisterModuleType("javadoc_host", JavadocHostFactory)
52}
53
54func RegisterStubsBuildComponents(ctx android.RegistrationContext) {
55 ctx.RegisterModuleType("stubs_defaults", StubsDefaultsFactory)
56
57 ctx.RegisterModuleType("droidstubs", DroidstubsFactory)
58 ctx.RegisterModuleType("droidstubs_host", DroidstubsHostFactory)
59
60 ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory)
61}
62
Colin Crossa1ce2a02018-06-20 15:19:39 -070063var (
64 srcsLibTag = dependencyTag{name: "sources from javalib"}
65)
66
Nan Zhang581fd212018-01-10 16:06:12 -080067type JavadocProperties struct {
68 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
69 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080070 Srcs []string `android:"path,arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080071
72 // list of directories rooted at the Android.bp file that will
73 // be added to the search paths for finding source files when passing package names.
Nan Zhangb2b33de2018-02-23 11:18:47 -080074 Local_sourcepaths []string
Nan Zhang581fd212018-01-10 16:06:12 -080075
76 // list of source files that should not be used to build the Java module.
77 // This is most useful in the arch/multilib variants to remove non-common files
78 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -080079 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080080
Jiyong Parkc6ddccf2019-09-13 20:56:14 +090081 // list of package names that should actually be used. If this property is left unspecified,
82 // all the sources from the srcs property is used.
83 Filter_packages []string
84
Nan Zhangb2b33de2018-02-23 11:18:47 -080085 // list of java libraries that will be in the classpath.
Nan Zhang581fd212018-01-10 16:06:12 -080086 Libs []string `android:"arch_variant"`
87
88 // If set to false, don't allow this module(-docs.zip) to be exported. Defaults to true.
Nan Zhangb2b33de2018-02-23 11:18:47 -080089 Installable *bool
Nan Zhang581fd212018-01-10 16:06:12 -080090
Paul Duffine25c6442019-10-11 13:50:28 +010091 // if not blank, set to the version of the sdk to compile against.
92 // Defaults to compiling against the current platform.
Nan Zhang581fd212018-01-10 16:06:12 -080093 Sdk_version *string `android:"arch_variant"`
Jiyong Park1e440682018-05-23 18:42:04 +090094
Paul Duffine25c6442019-10-11 13:50:28 +010095 // When targeting 1.9 and above, override the modules to use with --system,
96 // otherwise provides defaults libraries to add to the bootclasspath.
97 // Defaults to "none"
98 System_modules *string
99
Jiyong Park1e440682018-05-23 18:42:04 +0900100 Aidl struct {
101 // Top level directories to pass to aidl tool
102 Include_dirs []string
103
104 // Directories rooted at the Android.bp file to pass to aidl tool
105 Local_include_dirs []string
106 }
Nan Zhang357466b2018-04-17 17:38:36 -0700107
108 // If not blank, set the java version passed to javadoc as -source
109 Java_version *string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700110
111 // local files that are used within user customized droiddoc options.
Colin Cross27b922f2019-03-04 22:35:41 -0800112 Arg_files []string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -0700113
114 // user customized droiddoc args.
115 // Available variables for substitution:
116 //
117 // $(location <label>): the path to the arg_files with name <label>
Colin Crosse4a05842019-05-28 10:17:14 -0700118 // $$: a literal $
Nan Zhang1598a9e2018-09-04 17:14:32 -0700119 Args *string
120
121 // names of the output files used in args that will be generated
122 Out []string
Nan Zhang581fd212018-01-10 16:06:12 -0800123}
124
Nan Zhang61819ce2018-05-04 18:49:16 -0700125type ApiToCheck struct {
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900126 // path to the API txt file that the new API extracted from source code is checked
127 // against. The path can be local to the module or from other module (via :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800128 Api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700129
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900130 // path to the API txt file that the new @removed API extractd from source code is
131 // checked against. The path can be local to the module or from other module (via
132 // :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800133 Removed_api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700134
Adrian Roos14f75a92019-08-12 17:54:09 +0200135 // If not blank, path to the baseline txt file for approved API check violations.
136 Baseline_file *string `android:"path"`
137
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900138 // Arguments to the apicheck tool.
Nan Zhang61819ce2018-05-04 18:49:16 -0700139 Args *string
140}
141
Nan Zhang581fd212018-01-10 16:06:12 -0800142type DroiddocProperties struct {
143 // directory relative to top of the source tree that contains doc templates files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800144 Custom_template *string
Nan Zhang581fd212018-01-10 16:06:12 -0800145
Nan Zhanga40da042018-08-01 12:48:00 -0700146 // directories under current module source which contains html/jd files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800147 Html_dirs []string
Nan Zhang581fd212018-01-10 16:06:12 -0800148
149 // set a value in the Clearsilver hdf namespace.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800150 Hdf []string
Nan Zhang581fd212018-01-10 16:06:12 -0800151
152 // proofread file contains all of the text content of the javadocs concatenated into one file,
153 // suitable for spell-checking and other goodness.
Colin Crossab054432019-07-15 16:13:59 -0700154 Proofread_file *string
Nan Zhang581fd212018-01-10 16:06:12 -0800155
156 // a todo file lists the program elements that are missing documentation.
157 // At some point, this might be improved to show more warnings.
Colin Cross27b922f2019-03-04 22:35:41 -0800158 Todo_file *string `android:"path"`
Nan Zhangb2b33de2018-02-23 11:18:47 -0800159
160 // directory under current module source that provide additional resources (images).
161 Resourcesdir *string
162
163 // resources output directory under out/soong/.intermediates.
164 Resourcesoutdir *string
Nan Zhang581fd212018-01-10 16:06:12 -0800165
Nan Zhange2ba5d42018-07-11 15:16:55 -0700166 // if set to true, collect the values used by the Dev tools and
167 // write them in files packaged with the SDK. Defaults to false.
168 Write_sdk_values *bool
169
170 // index.html under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800171 Static_doc_index_redirect *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700172
173 // source.properties under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800174 Static_doc_properties *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700175
Nan Zhang581fd212018-01-10 16:06:12 -0800176 // a list of files under current module source dir which contains known tags in Java sources.
177 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -0800178 Knowntags []string `android:"path"`
Nan Zhang28c68b92018-03-13 16:17:01 -0700179
180 // the tag name used to distinguish if the API files belong to public/system/test.
181 Api_tag_name *string
182
183 // the generated public API filename by Doclava.
184 Api_filename *string
185
David Brazdilfbe4cc32018-05-31 13:56:46 +0100186 // the generated public Dex API filename by Doclava.
187 Dex_api_filename *string
188
Nan Zhang28c68b92018-03-13 16:17:01 -0700189 // the generated private API filename by Doclava.
190 Private_api_filename *string
191
192 // the generated private Dex API filename by Doclava.
193 Private_dex_api_filename *string
194
195 // the generated removed API filename by Doclava.
196 Removed_api_filename *string
197
David Brazdilaac0c3c2018-04-24 16:23:29 +0100198 // the generated removed Dex API filename by Doclava.
199 Removed_dex_api_filename *string
200
Mathew Inwood76c3de12018-06-22 15:28:11 +0100201 // mapping of dex signatures to source file and line number. This is a temporary property and
202 // will be deleted; you probably shouldn't be using it.
203 Dex_mapping_filename *string
204
Nan Zhang28c68b92018-03-13 16:17:01 -0700205 // the generated exact API filename by Doclava.
206 Exact_api_filename *string
Nan Zhang853f4202018-04-12 16:55:56 -0700207
Nan Zhang66dc2362018-08-14 20:41:04 -0700208 // the generated proguard filename by Doclava.
209 Proguard_filename *string
210
Nan Zhang853f4202018-04-12 16:55:56 -0700211 // if set to false, don't allow droiddoc to generate stubs source files. Defaults to true.
212 Create_stubs *bool
Nan Zhang61819ce2018-05-04 18:49:16 -0700213
214 Check_api struct {
215 Last_released ApiToCheck
216
217 Current ApiToCheck
Inseob Kim38449af2019-02-28 14:24:05 +0900218
219 // do not perform API check against Last_released, in the case that both two specified API
220 // files by Last_released are modules which don't exist.
221 Ignore_missing_latest_api *bool `blueprint:"mutated"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700222 }
Nan Zhang79614d12018-04-19 18:03:39 -0700223
Nan Zhang1598a9e2018-09-04 17:14:32 -0700224 // if set to true, generate docs through Dokka instead of Doclava.
225 Dokka_enabled *bool
226}
227
228type DroidstubsProperties struct {
229 // the tag name used to distinguish if the API files belong to public/system/test.
230 Api_tag_name *string
231
Nan Zhang199645c2018-09-19 12:40:06 -0700232 // the generated public API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700233 Api_filename *string
234
Nan Zhang199645c2018-09-19 12:40:06 -0700235 // the generated public Dex API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700236 Dex_api_filename *string
237
Nan Zhang199645c2018-09-19 12:40:06 -0700238 // the generated private API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700239 Private_api_filename *string
240
Nan Zhang199645c2018-09-19 12:40:06 -0700241 // the generated private Dex API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700242 Private_dex_api_filename *string
243
Nan Zhang199645c2018-09-19 12:40:06 -0700244 // the generated removed API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700245 Removed_api_filename *string
246
Nan Zhang199645c2018-09-19 12:40:06 -0700247 // the generated removed Dex API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700248 Removed_dex_api_filename *string
249
Nan Zhang9c69a122018-08-22 10:22:08 -0700250 // mapping of dex signatures to source file and line number. This is a temporary property and
251 // will be deleted; you probably shouldn't be using it.
252 Dex_mapping_filename *string
253
Nan Zhang199645c2018-09-19 12:40:06 -0700254 // the generated exact API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700255 Exact_api_filename *string
256
Nan Zhang199645c2018-09-19 12:40:06 -0700257 // the generated proguard filename by Metalava.
258 Proguard_filename *string
259
Nan Zhang1598a9e2018-09-04 17:14:32 -0700260 Check_api struct {
261 Last_released ApiToCheck
262
263 Current ApiToCheck
Inseob Kim38449af2019-02-28 14:24:05 +0900264
265 // do not perform API check against Last_released, in the case that both two specified API
266 // files by Last_released are modules which don't exist.
267 Ignore_missing_latest_api *bool `blueprint:"mutated"`
Adrian Roos075eedc2019-10-10 12:07:03 +0200268
269 Api_lint struct {
270 Enabled *bool
271
272 // If set, performs api_lint on any new APIs not found in the given signature file
273 New_since *string `android:"path"`
274
275 // If not blank, path to the baseline txt file for approved API lint violations.
276 Baseline_file *string `android:"path"`
277 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700278 }
Nan Zhang79614d12018-04-19 18:03:39 -0700279
280 // user can specify the version of previous released API file in order to do compatibility check.
Colin Cross27b922f2019-03-04 22:35:41 -0800281 Previous_api *string `android:"path"`
Nan Zhang79614d12018-04-19 18:03:39 -0700282
283 // is set to true, Metalava will allow framework SDK to contain annotations.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700284 Annotations_enabled *bool
Nan Zhang79614d12018-04-19 18:03:39 -0700285
Pete Gillin77167902018-09-19 18:16:26 +0100286 // 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 -0700287 Merge_annotations_dirs []string
Nan Zhang86d2d552018-08-09 15:33:27 -0700288
Pete Gillin77167902018-09-19 18:16:26 +0100289 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
290 Merge_inclusion_annotations_dirs []string
291
Pete Gillinc382a562018-11-14 18:45:46 +0000292 // a file containing a list of classes to do nullability validation for.
293 Validate_nullability_from_list *string
294
Pete Gillin581d6082018-10-22 15:55:04 +0100295 // a file containing expected warnings produced by validation of nullability annotations.
296 Check_nullability_warnings *string
297
Nan Zhang1598a9e2018-09-04 17:14:32 -0700298 // if set to true, allow Metalava to generate doc_stubs source files. Defaults to false.
299 Create_doc_stubs *bool
Nan Zhang9c69a122018-08-22 10:22:08 -0700300
301 // is set to true, Metalava will allow framework SDK to contain API levels annotations.
302 Api_levels_annotations_enabled *bool
303
304 // the dirs which Metalava extracts API levels annotations from.
305 Api_levels_annotations_dirs []string
306
307 // if set to true, collect the values used by the Dev tools and
308 // write them in files packaged with the SDK. Defaults to false.
309 Write_sdk_values *bool
Nan Zhang71bbe632018-09-17 14:32:21 -0700310
311 // If set to true, .xml based public API file will be also generated, and
312 // JDiff tool will be invoked to genreate javadoc files. Defaults to false.
313 Jdiff_enabled *bool
Nan Zhang581fd212018-01-10 16:06:12 -0800314}
315
Nan Zhanga40da042018-08-01 12:48:00 -0700316//
317// Common flags passed down to build rule
318//
319type droiddocBuilderFlags struct {
Nan Zhang86d2d552018-08-09 15:33:27 -0700320 bootClasspathArgs string
321 classpathArgs string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700322 sourcepathArgs string
Nan Zhang86d2d552018-08-09 15:33:27 -0700323 dokkaClasspathArgs string
324 aidlFlags string
Colin Cross3047fa22019-04-18 10:56:44 -0700325 aidlDeps android.Paths
Nan Zhanga40da042018-08-01 12:48:00 -0700326
Nan Zhanga40da042018-08-01 12:48:00 -0700327 doclavaStubsFlags string
Nan Zhang86d2d552018-08-09 15:33:27 -0700328 doclavaDocsFlags string
Nan Zhanga40da042018-08-01 12:48:00 -0700329 postDoclavaCmds string
Nan Zhanga40da042018-08-01 12:48:00 -0700330}
331
332func InitDroiddocModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
333 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
334 android.InitDefaultableModule(module)
335}
336
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200337func apiCheckEnabled(ctx android.ModuleContext, apiToCheck ApiToCheck, apiVersionTag string) bool {
338 if ctx.Config().IsEnvTrue("WITHOUT_CHECK_API") {
339 return false
340 } else if String(apiToCheck.Api_file) != "" && String(apiToCheck.Removed_api_file) != "" {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700341 return true
342 } else if String(apiToCheck.Api_file) != "" {
343 panic("for " + apiVersionTag + " removed_api_file has to be non-empty!")
344 } else if String(apiToCheck.Removed_api_file) != "" {
345 panic("for " + apiVersionTag + " api_file has to be non-empty!")
346 }
347
348 return false
349}
350
Inseob Kim38449af2019-02-28 14:24:05 +0900351func ignoreMissingModules(ctx android.BottomUpMutatorContext, apiToCheck *ApiToCheck) {
352 api_file := String(apiToCheck.Api_file)
353 removed_api_file := String(apiToCheck.Removed_api_file)
354
355 api_module := android.SrcIsModule(api_file)
356 removed_api_module := android.SrcIsModule(removed_api_file)
357
358 if api_module == "" || removed_api_module == "" {
359 return
360 }
361
362 if ctx.OtherModuleExists(api_module) || ctx.OtherModuleExists(removed_api_module) {
363 return
364 }
365
366 apiToCheck.Api_file = nil
367 apiToCheck.Removed_api_file = nil
368}
369
Nan Zhang1598a9e2018-09-04 17:14:32 -0700370type ApiFilePath interface {
371 ApiFilePath() android.Path
372}
373
Nan Zhanga40da042018-08-01 12:48:00 -0700374//
375// Javadoc
376//
Nan Zhang581fd212018-01-10 16:06:12 -0800377type Javadoc struct {
378 android.ModuleBase
379 android.DefaultableModuleBase
380
381 properties JavadocProperties
382
383 srcJars android.Paths
384 srcFiles android.Paths
385 sourcepaths android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700386 argFiles android.Paths
387
388 args string
Nan Zhang581fd212018-01-10 16:06:12 -0800389
Nan Zhangccff0f72018-03-08 17:26:16 -0800390 docZip android.WritablePath
391 stubsSrcJar android.WritablePath
Nan Zhang581fd212018-01-10 16:06:12 -0800392}
393
Colin Cross41955e82019-05-29 14:40:35 -0700394func (j *Javadoc) OutputFiles(tag string) (android.Paths, error) {
395 switch tag {
396 case "":
397 return android.Paths{j.stubsSrcJar}, nil
Colin Crosse68e5542019-08-12 13:11:40 -0700398 case ".docs.zip":
399 return android.Paths{j.docZip}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700400 default:
401 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
402 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800403}
404
Colin Crossa3002fc2019-07-08 16:48:04 -0700405// javadoc converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800406func JavadocFactory() android.Module {
407 module := &Javadoc{}
408
409 module.AddProperties(&module.properties)
410
411 InitDroiddocModule(module, android.HostAndDeviceSupported)
412 return module
413}
414
Colin Crossa3002fc2019-07-08 16:48:04 -0700415// javadoc_host converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800416func JavadocHostFactory() android.Module {
417 module := &Javadoc{}
418
419 module.AddProperties(&module.properties)
420
421 InitDroiddocModule(module, android.HostSupported)
422 return module
423}
424
Colin Cross41955e82019-05-29 14:40:35 -0700425var _ android.OutputFileProducer = (*Javadoc)(nil)
Nan Zhang581fd212018-01-10 16:06:12 -0800426
Colin Cross83bb3162018-06-25 15:48:06 -0700427func (j *Javadoc) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900428 return String(j.properties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700429}
430
Paul Duffine25c6442019-10-11 13:50:28 +0100431func (j *Javadoc) systemModules() string {
432 return proptools.String(j.properties.System_modules)
433}
434
Colin Cross83bb3162018-06-25 15:48:06 -0700435func (j *Javadoc) minSdkVersion() string {
436 return j.sdkVersion()
437}
438
Dan Willemsen419290a2018-10-31 15:28:47 -0700439func (j *Javadoc) targetSdkVersion() string {
440 return j.sdkVersion()
441}
442
Nan Zhang581fd212018-01-10 16:06:12 -0800443func (j *Javadoc) addDeps(ctx android.BottomUpMutatorContext) {
444 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100445 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700446 if sdkDep.useDefaultLibs {
447 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
448 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
449 if sdkDep.hasFrameworkLibs() {
450 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Nan Zhang357466b2018-04-17 17:38:36 -0700451 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700452 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700453 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100454 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700455 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Nan Zhang581fd212018-01-10 16:06:12 -0800456 }
457 }
458
Colin Cross42d48b72018-08-29 14:10:52 -0700459 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800460}
461
Nan Zhanga40da042018-08-01 12:48:00 -0700462func (j *Javadoc) collectAidlFlags(ctx android.ModuleContext, deps deps) droiddocBuilderFlags {
463 var flags droiddocBuilderFlags
Jiyong Park1e440682018-05-23 18:42:04 +0900464
Colin Cross3047fa22019-04-18 10:56:44 -0700465 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Jiyong Park1e440682018-05-23 18:42:04 +0900466
467 return flags
468}
469
470func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700471 aidlIncludeDirs android.Paths) (string, android.Paths) {
Jiyong Park1e440682018-05-23 18:42:04 +0900472
473 aidlIncludes := android.PathsForModuleSrc(ctx, j.properties.Aidl.Local_include_dirs)
474 aidlIncludes = append(aidlIncludes, android.PathsForSource(ctx, j.properties.Aidl.Include_dirs)...)
475
476 var flags []string
Colin Cross3047fa22019-04-18 10:56:44 -0700477 var deps android.Paths
478
Jiyong Park1e440682018-05-23 18:42:04 +0900479 if aidlPreprocess.Valid() {
480 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700481 deps = append(deps, aidlPreprocess.Path())
Jiyong Park1e440682018-05-23 18:42:04 +0900482 } else {
483 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
484 }
485
486 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
487 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
488 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
489 flags = append(flags, "-I"+src.String())
490 }
491
Colin Cross3047fa22019-04-18 10:56:44 -0700492 return strings.Join(flags, " "), deps
Jiyong Park1e440682018-05-23 18:42:04 +0900493}
494
Jiyong Parkd90d7412019-08-20 22:49:19 +0900495// TODO: remove the duplication between this and the one in gen.go
Jiyong Park1e440682018-05-23 18:42:04 +0900496func (j *Javadoc) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Nan Zhanga40da042018-08-01 12:48:00 -0700497 flags droiddocBuilderFlags) android.Paths {
Jiyong Park1e440682018-05-23 18:42:04 +0900498
499 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Crossc0806172019-06-14 18:51:47 -0700500 var aidlSrcs android.Paths
Jiyong Park1e440682018-05-23 18:42:04 +0900501
Jiyong Park1112c4c2019-08-16 21:12:10 +0900502 aidlIncludeFlags := genAidlIncludeFlags(srcFiles)
503
Jiyong Park1e440682018-05-23 18:42:04 +0900504 for _, srcFile := range srcFiles {
505 switch srcFile.Ext() {
506 case ".aidl":
Colin Crossc0806172019-06-14 18:51:47 -0700507 aidlSrcs = append(aidlSrcs, srcFile)
Jiyong Parkd90d7412019-08-20 22:49:19 +0900508 case ".logtags":
509 javaFile := genLogtags(ctx, srcFile)
510 outSrcFiles = append(outSrcFiles, javaFile)
Jiyong Park1e440682018-05-23 18:42:04 +0900511 default:
512 outSrcFiles = append(outSrcFiles, srcFile)
513 }
514 }
515
Colin Crossc0806172019-06-14 18:51:47 -0700516 // Process all aidl files together to support sharding them into one or more rules that produce srcjars.
517 if len(aidlSrcs) > 0 {
518 srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags+aidlIncludeFlags, flags.aidlDeps)
519 outSrcFiles = append(outSrcFiles, srcJarFiles...)
520 }
521
Jiyong Park1e440682018-05-23 18:42:04 +0900522 return outSrcFiles
523}
524
Nan Zhang581fd212018-01-10 16:06:12 -0800525func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
526 var deps deps
527
Colin Cross83bb3162018-06-25 15:48:06 -0700528 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800529 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700530 ctx.AddMissingDependencies(sdkDep.bootclasspath)
531 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Nan Zhang581fd212018-01-10 16:06:12 -0800532 } else if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700533 deps.bootClasspath = append(deps.bootClasspath, sdkDep.jars...)
Nan Zhang581fd212018-01-10 16:06:12 -0800534 }
535
536 ctx.VisitDirectDeps(func(module android.Module) {
537 otherName := ctx.OtherModuleName(module)
538 tag := ctx.OtherModuleDependencyTag(module)
539
Colin Cross2d24c1b2018-05-23 10:59:18 -0700540 switch tag {
541 case bootClasspathTag:
542 if dep, ok := module.(Dependency); ok {
Nan Zhang581fd212018-01-10 16:06:12 -0800543 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars()...)
Paul Duffine25c6442019-10-11 13:50:28 +0100544 } else if sm, ok := module.(*SystemModules); ok {
545 // A system modules dependency has been added to the bootclasspath
546 // so add its libs to the bootclasspath.
547 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700548 } else {
549 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
550 }
551 case libTag:
552 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800553 case SdkLibraryDependency:
554 deps.classpath = append(deps.classpath, dep.SdkImplementationJars(ctx, j.sdkVersion())...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700555 case Dependency:
Sundong Ahnba493602018-11-20 17:36:35 +0900556 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park19a7f252019-07-10 16:59:31 +0900557 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700558 case android.SourceFileProducer:
Nan Zhang581fd212018-01-10 16:06:12 -0800559 checkProducesJars(ctx, dep)
560 deps.classpath = append(deps.classpath, dep.Srcs()...)
Nan Zhang581fd212018-01-10 16:06:12 -0800561 default:
562 ctx.ModuleErrorf("depends on non-java module %q", otherName)
563 }
Colin Cross6cef4812019-10-17 14:23:50 -0700564 case java9LibTag:
565 switch dep := module.(type) {
566 case Dependency:
567 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
568 default:
569 ctx.ModuleErrorf("depends on non-java module %q", otherName)
570 }
Nan Zhang357466b2018-04-17 17:38:36 -0700571 case systemModulesTag:
572 if deps.systemModules != nil {
573 panic("Found two system module dependencies")
574 }
575 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000576 if sm.outputDir == nil && len(sm.outputDeps) == 0 {
Nan Zhang357466b2018-04-17 17:38:36 -0700577 panic("Missing directory for system module dependency")
578 }
Colin Crossb77043e2019-07-16 13:57:13 -0700579 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Nan Zhang581fd212018-01-10 16:06:12 -0800580 }
581 })
582 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
583 // may contain filegroup or genrule.
Colin Cross8a497952019-03-05 22:25:09 -0800584 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900585
586 filterByPackage := func(srcs []android.Path, filterPackages []string) []android.Path {
587 if filterPackages == nil {
588 return srcs
589 }
590 filtered := []android.Path{}
591 for _, src := range srcs {
592 if src.Ext() != ".java" {
593 // Don't filter-out non-Java (=generated sources) by package names. This is not ideal,
594 // but otherwise metalava emits stub sources having references to the generated AIDL classes
595 // in filtered-out pacages (e.g. com.android.internal.*).
596 // TODO(b/141149570) We need to fix this by introducing default private constructors or
597 // fixing metalava to not emit constructors having references to unknown classes.
598 filtered = append(filtered, src)
599 continue
600 }
601 packageName := strings.ReplaceAll(filepath.Dir(src.Rel()), "/", ".")
602 for _, pkg := range filterPackages {
603 if strings.HasPrefix(packageName, pkg) {
604 filtered = append(filtered, src)
605 break
606 }
607 }
608 }
609 return filtered
610 }
611 srcFiles = filterByPackage(srcFiles, j.properties.Filter_packages)
612
Nan Zhanga40da042018-08-01 12:48:00 -0700613 flags := j.collectAidlFlags(ctx, deps)
Jiyong Park1e440682018-05-23 18:42:04 +0900614 srcFiles = j.genSources(ctx, srcFiles, flags)
Nan Zhang581fd212018-01-10 16:06:12 -0800615
616 // srcs may depend on some genrule output.
617 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800618 j.srcJars = append(j.srcJars, deps.srcJars...)
619
Nan Zhang581fd212018-01-10 16:06:12 -0800620 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800621 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800622
Nan Zhang9c69a122018-08-22 10:22:08 -0700623 if j.properties.Local_sourcepaths == nil && len(j.srcFiles) > 0 {
Nan Zhang581fd212018-01-10 16:06:12 -0800624 j.properties.Local_sourcepaths = append(j.properties.Local_sourcepaths, ".")
625 }
626 j.sourcepaths = android.PathsForModuleSrc(ctx, j.properties.Local_sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800627
Colin Cross8a497952019-03-05 22:25:09 -0800628 j.argFiles = android.PathsForModuleSrc(ctx, j.properties.Arg_files)
Paul Duffin99e4a502019-02-11 15:38:42 +0000629 argFilesMap := map[string]string{}
630 argFileLabels := []string{}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700631
Paul Duffin99e4a502019-02-11 15:38:42 +0000632 for _, label := range j.properties.Arg_files {
Colin Cross8a497952019-03-05 22:25:09 -0800633 var paths = android.PathsForModuleSrc(ctx, []string{label})
Paul Duffin99e4a502019-02-11 15:38:42 +0000634 if _, exists := argFilesMap[label]; !exists {
635 argFilesMap[label] = strings.Join(paths.Strings(), " ")
636 argFileLabels = append(argFileLabels, label)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700637 } else {
638 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000639 label, argFilesMap[label], paths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700640 }
641 }
642
643 var err error
Colin Cross15638152019-07-11 11:11:35 -0700644 j.args, err = android.Expand(String(j.properties.Args), func(name string) (string, error) {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700645 if strings.HasPrefix(name, "location ") {
646 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Paul Duffin99e4a502019-02-11 15:38:42 +0000647 if paths, ok := argFilesMap[label]; ok {
Colin Cross15638152019-07-11 11:11:35 -0700648 return paths, nil
Nan Zhang1598a9e2018-09-04 17:14:32 -0700649 } else {
Colin Cross15638152019-07-11 11:11:35 -0700650 return "", fmt.Errorf("unknown location label %q, expecting one of %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000651 label, strings.Join(argFileLabels, ", "))
Nan Zhang1598a9e2018-09-04 17:14:32 -0700652 }
653 } else if name == "genDir" {
Colin Cross15638152019-07-11 11:11:35 -0700654 return android.PathForModuleGen(ctx).String(), nil
Nan Zhang1598a9e2018-09-04 17:14:32 -0700655 }
Colin Cross15638152019-07-11 11:11:35 -0700656 return "", fmt.Errorf("unknown variable '$(%s)'", name)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700657 })
658
659 if err != nil {
660 ctx.PropertyErrorf("args", "%s", err.Error())
661 }
662
Nan Zhang581fd212018-01-10 16:06:12 -0800663 return deps
664}
665
666func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
667 j.addDeps(ctx)
668}
669
670func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
671 deps := j.collectDeps(ctx)
672
Colin Crossdaa4c672019-07-15 22:53:46 -0700673 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800674
Colin Crossdaa4c672019-07-15 22:53:46 -0700675 outDir := android.PathForModuleOut(ctx, "out")
676 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
677
678 j.stubsSrcJar = nil
679
680 rule := android.NewRuleBuilder()
681
682 rule.Command().Text("rm -rf").Text(outDir.String())
683 rule.Command().Text("mkdir -p").Text(outDir.String())
684
685 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, j.srcJars)
Nan Zhang357466b2018-04-17 17:38:36 -0700686
Colin Cross83bb3162018-06-25 15:48:06 -0700687 javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800688
Colin Crossdaa4c672019-07-15 22:53:46 -0700689 cmd := javadocSystemModulesCmd(ctx, rule, j.srcFiles, outDir, srcJarDir, srcJarList,
690 deps.systemModules, deps.classpath, j.sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800691
Colin Cross1e743852019-10-28 11:37:20 -0700692 cmd.FlagWithArg("-source ", javaVersion.String()).
Colin Crossdaa4c672019-07-15 22:53:46 -0700693 Flag("-J-Xmx1024m").
694 Flag("-XDignore.symbol.file").
695 Flag("-Xdoclint:none")
Nan Zhang581fd212018-01-10 16:06:12 -0800696
Colin Crossdaa4c672019-07-15 22:53:46 -0700697 rule.Command().
698 BuiltTool(ctx, "soong_zip").
699 Flag("-write_if_changed").
700 Flag("-d").
701 FlagWithOutput("-o ", j.docZip).
702 FlagWithArg("-C ", outDir.String()).
703 FlagWithArg("-D ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700704
Colin Crossdaa4c672019-07-15 22:53:46 -0700705 rule.Restat()
706
707 zipSyncCleanupCmd(rule, srcJarDir)
708
709 rule.Build(pctx, ctx, "javadoc", "javadoc")
Nan Zhang581fd212018-01-10 16:06:12 -0800710}
711
Nan Zhanga40da042018-08-01 12:48:00 -0700712//
713// Droiddoc
714//
715type Droiddoc struct {
716 Javadoc
717
718 properties DroiddocProperties
719 apiFile android.WritablePath
720 dexApiFile android.WritablePath
721 privateApiFile android.WritablePath
722 privateDexApiFile android.WritablePath
723 removedApiFile android.WritablePath
724 removedDexApiFile android.WritablePath
725 exactApiFile android.WritablePath
726 apiMappingFile android.WritablePath
Nan Zhang66dc2362018-08-14 20:41:04 -0700727 proguardFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700728
729 checkCurrentApiTimestamp android.WritablePath
730 updateCurrentApiTimestamp android.WritablePath
731 checkLastReleasedApiTimestamp android.WritablePath
732
Nan Zhanga40da042018-08-01 12:48:00 -0700733 apiFilePath android.Path
734}
735
Colin Crossa3002fc2019-07-08 16:48:04 -0700736// droiddoc converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700737func DroiddocFactory() android.Module {
738 module := &Droiddoc{}
739
740 module.AddProperties(&module.properties,
741 &module.Javadoc.properties)
742
743 InitDroiddocModule(module, android.HostAndDeviceSupported)
744 return module
745}
746
Colin Crossa3002fc2019-07-08 16:48:04 -0700747// droiddoc_host converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700748func DroiddocHostFactory() android.Module {
749 module := &Droiddoc{}
750
751 module.AddProperties(&module.properties,
752 &module.Javadoc.properties)
753
754 InitDroiddocModule(module, android.HostSupported)
755 return module
756}
757
758func (d *Droiddoc) ApiFilePath() android.Path {
759 return d.apiFilePath
760}
761
Nan Zhang581fd212018-01-10 16:06:12 -0800762func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
763 d.Javadoc.addDeps(ctx)
764
Inseob Kim38449af2019-02-28 14:24:05 +0900765 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
766 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
767 }
768
Nan Zhang79614d12018-04-19 18:03:39 -0700769 if String(d.properties.Custom_template) != "" {
Dan Willemsencc090972018-02-26 14:33:31 -0800770 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
771 }
Nan Zhang581fd212018-01-10 16:06:12 -0800772}
773
Colin Crossab054432019-07-15 16:13:59 -0700774func (d *Droiddoc) doclavaDocsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, docletPath classpath) {
Nan Zhang443fa522018-08-20 20:58:28 -0700775 // Droiddoc always gets "-source 1.8" because it doesn't support 1.9 sources. For modules with 1.9
776 // sources, droiddoc will get sources produced by metalava which will have already stripped out the
777 // 1.9 language features.
Colin Crossab054432019-07-15 16:13:59 -0700778 cmd.FlagWithArg("-source ", "1.8").
779 Flag("-J-Xmx1600m").
780 Flag("-J-XX:-OmitStackTraceInFastThrow").
781 Flag("-XDignore.symbol.file").
782 FlagWithArg("-doclet ", "com.google.doclava.Doclava").
783 FlagWithInputList("-docletpath ", docletPath.Paths(), ":").
784 FlagWithArg("-hdf page.build ", ctx.Config().BuildId()+"-"+ctx.Config().BuildNumberFromFile()).
Elliott Hughes26bce342019-09-12 15:05:13 -0700785 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 -0700786
Nan Zhanga40da042018-08-01 12:48:00 -0700787 if String(d.properties.Custom_template) == "" {
788 // TODO: This is almost always droiddoc-templates-sdk
789 ctx.PropertyErrorf("custom_template", "must specify a template")
790 }
791
792 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
Nan Zhangf4936b02018-08-01 15:00:28 -0700793 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Crossab054432019-07-15 16:13:59 -0700794 cmd.FlagWithArg("-templatedir ", t.dir.String()).Implicits(t.deps)
Nan Zhanga40da042018-08-01 12:48:00 -0700795 } else {
Paul Duffin884363e2019-12-19 10:21:09 +0000796 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_exported_dir", ctx.OtherModuleName(m))
Nan Zhanga40da042018-08-01 12:48:00 -0700797 }
798 })
799
800 if len(d.properties.Html_dirs) > 0 {
Colin Crossab054432019-07-15 16:13:59 -0700801 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
802 cmd.FlagWithArg("-htmldir ", htmlDir.String()).
803 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[0], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700804 }
805
806 if len(d.properties.Html_dirs) > 1 {
Colin Crossab054432019-07-15 16:13:59 -0700807 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
808 cmd.FlagWithArg("-htmldir2 ", htmlDir2.String()).
809 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[1], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700810 }
811
812 if len(d.properties.Html_dirs) > 2 {
813 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
814 }
815
Colin Cross8a497952019-03-05 22:25:09 -0800816 knownTags := android.PathsForModuleSrc(ctx, d.properties.Knowntags)
Colin Crossab054432019-07-15 16:13:59 -0700817 cmd.FlagForEachInput("-knowntags ", knownTags)
Nan Zhanga40da042018-08-01 12:48:00 -0700818
Colin Crossab054432019-07-15 16:13:59 -0700819 cmd.FlagForEachArg("-hdf ", d.properties.Hdf)
Nan Zhanga40da042018-08-01 12:48:00 -0700820
821 if String(d.properties.Proofread_file) != "" {
822 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
Colin Crossab054432019-07-15 16:13:59 -0700823 cmd.FlagWithOutput("-proofread ", proofreadFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700824 }
825
826 if String(d.properties.Todo_file) != "" {
827 // tricky part:
828 // we should not compute full path for todo_file through PathForModuleOut().
829 // the non-standard doclet will get the full path relative to "-o".
Colin Crossab054432019-07-15 16:13:59 -0700830 cmd.FlagWithArg("-todo ", String(d.properties.Todo_file)).
831 ImplicitOutput(android.PathForModuleOut(ctx, String(d.properties.Todo_file)))
Nan Zhanga40da042018-08-01 12:48:00 -0700832 }
833
834 if String(d.properties.Resourcesdir) != "" {
835 // TODO: should we add files under resourcesDir to the implicits? It seems that
836 // resourcesDir is one sub dir of htmlDir
837 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
Colin Crossab054432019-07-15 16:13:59 -0700838 cmd.FlagWithArg("-resourcesdir ", resourcesDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700839 }
840
841 if String(d.properties.Resourcesoutdir) != "" {
842 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
Colin Crossab054432019-07-15 16:13:59 -0700843 cmd.FlagWithArg("-resourcesoutdir ", String(d.properties.Resourcesoutdir))
Nan Zhanga40da042018-08-01 12:48:00 -0700844 }
Nan Zhanga40da042018-08-01 12:48:00 -0700845}
846
Colin Crossab054432019-07-15 16:13:59 -0700847func (d *Droiddoc) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.WritablePath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200848 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
849 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -0700850 String(d.properties.Api_filename) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700851
Nan Zhanga40da042018-08-01 12:48:00 -0700852 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Colin Crossab054432019-07-15 16:13:59 -0700853 cmd.FlagWithOutput("-api ", d.apiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700854 d.apiFilePath = d.apiFile
855 }
856
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200857 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
858 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -0700859 String(d.properties.Removed_api_filename) != "" {
Nan Zhanga40da042018-08-01 12:48:00 -0700860 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
Colin Crossab054432019-07-15 16:13:59 -0700861 cmd.FlagWithOutput("-removedApi ", d.removedApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700862 }
863
864 if String(d.properties.Private_api_filename) != "" {
865 d.privateApiFile = android.PathForModuleOut(ctx, String(d.properties.Private_api_filename))
Colin Crossab054432019-07-15 16:13:59 -0700866 cmd.FlagWithOutput("-privateApi ", d.privateApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700867 }
868
869 if String(d.properties.Dex_api_filename) != "" {
870 d.dexApiFile = android.PathForModuleOut(ctx, String(d.properties.Dex_api_filename))
Colin Crossab054432019-07-15 16:13:59 -0700871 cmd.FlagWithOutput("-dexApi ", d.dexApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700872 }
873
874 if String(d.properties.Private_dex_api_filename) != "" {
875 d.privateDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Private_dex_api_filename))
Colin Crossab054432019-07-15 16:13:59 -0700876 cmd.FlagWithOutput("-privateDexApi ", d.privateDexApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700877 }
878
879 if String(d.properties.Removed_dex_api_filename) != "" {
880 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Crossab054432019-07-15 16:13:59 -0700881 cmd.FlagWithOutput("-removedDexApi ", d.removedDexApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700882 }
883
884 if String(d.properties.Exact_api_filename) != "" {
885 d.exactApiFile = android.PathForModuleOut(ctx, String(d.properties.Exact_api_filename))
Colin Crossab054432019-07-15 16:13:59 -0700886 cmd.FlagWithOutput("-exactApi ", d.exactApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700887 }
888
889 if String(d.properties.Dex_mapping_filename) != "" {
890 d.apiMappingFile = android.PathForModuleOut(ctx, String(d.properties.Dex_mapping_filename))
Colin Crossab054432019-07-15 16:13:59 -0700891 cmd.FlagWithOutput("-apiMapping ", d.apiMappingFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700892 }
893
Nan Zhang66dc2362018-08-14 20:41:04 -0700894 if String(d.properties.Proguard_filename) != "" {
895 d.proguardFile = android.PathForModuleOut(ctx, String(d.properties.Proguard_filename))
Colin Crossab054432019-07-15 16:13:59 -0700896 cmd.FlagWithOutput("-proguard ", d.proguardFile)
Nan Zhang66dc2362018-08-14 20:41:04 -0700897 }
898
Nan Zhanga40da042018-08-01 12:48:00 -0700899 if BoolDefault(d.properties.Create_stubs, true) {
Colin Crossab054432019-07-15 16:13:59 -0700900 cmd.FlagWithArg("-stubs ", stubsDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700901 }
902
903 if Bool(d.properties.Write_sdk_values) {
Colin Crossab054432019-07-15 16:13:59 -0700904 cmd.FlagWithArg("-sdkvalues ", android.PathForModuleOut(ctx, "out").String())
Nan Zhanga40da042018-08-01 12:48:00 -0700905 }
Nan Zhanga40da042018-08-01 12:48:00 -0700906}
907
Colin Crossab054432019-07-15 16:13:59 -0700908func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700909 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700910 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
911 rule.Command().Text("cp").
912 Input(staticDocIndexRedirect).
913 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700914 }
915
916 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700917 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
918 rule.Command().Text("cp").
919 Input(staticDocProperties).
920 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700921 }
Nan Zhanga40da042018-08-01 12:48:00 -0700922}
923
Colin Crossab054432019-07-15 16:13:59 -0700924func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700925 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700926
927 cmd := rule.Command().
928 BuiltTool(ctx, "soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
929 Flag(config.JavacVmFlags).
930 FlagWithArg("-encoding ", "UTF-8").
Colin Crossab054432019-07-15 16:13:59 -0700931 FlagWithRspFileInputList("@", srcs).
932 FlagWithInput("@", srcJarList)
933
Colin Crossab054432019-07-15 16:13:59 -0700934 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
935 // based stubs generation.
936 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
937 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
938 // the correct package name base path.
939 if len(sourcepaths) > 0 {
940 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
941 } else {
942 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
943 }
944
945 cmd.FlagWithArg("-d ", outDir.String()).
946 Flag("-quiet")
947
948 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700949}
950
Colin Crossdaa4c672019-07-15 22:53:46 -0700951func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
952 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
953 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
954
955 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
956
957 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
958 cmd.Flag(flag).Implicits(deps)
959
960 cmd.FlagWithArg("--patch-module ", "java.base=.")
961
962 if len(classpath) > 0 {
963 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
964 }
965
966 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700967}
968
Colin Crossdaa4c672019-07-15 22:53:46 -0700969func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
970 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
971 sourcepaths android.Paths) *android.RuleBuilderCommand {
972
973 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
974
975 if len(bootclasspath) == 0 && ctx.Device() {
976 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
977 // ensure java does not fall back to the default bootclasspath.
978 cmd.FlagWithArg("-bootclasspath ", `""`)
979 } else if len(bootclasspath) > 0 {
980 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
981 }
982
983 if len(classpath) > 0 {
984 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
985 }
986
987 return cmd
988}
989
Colin Crossab054432019-07-15 16:13:59 -0700990func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
991 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700992
Colin Crossab054432019-07-15 16:13:59 -0700993 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
994 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
995
996 return rule.Command().
997 BuiltTool(ctx, "dokka").
998 Flag(config.JavacVmFlags).
999 Flag(srcJarDir.String()).
1000 FlagWithInputList("-classpath ", dokkaClasspath, ":").
1001 FlagWithArg("-format ", "dac").
1002 FlagWithArg("-dacRoot ", "/reference/kotlin").
1003 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -07001004}
1005
1006func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1007 deps := d.Javadoc.collectDeps(ctx)
1008
Colin Crossdaa4c672019-07-15 22:53:46 -07001009 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
1010 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1011
Nan Zhang1598a9e2018-09-04 17:14:32 -07001012 jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
1013 doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
1014 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1015 checkApiClasspath := classpath{jsilver, doclava, android.PathForSource(ctx, java8Home, "lib/tools.jar")}
1016
Colin Crossab054432019-07-15 16:13:59 -07001017 outDir := android.PathForModuleOut(ctx, "out")
1018 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
1019 stubsDir := android.PathForModuleOut(ctx, "stubsDir")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001020
Colin Crossab054432019-07-15 16:13:59 -07001021 rule := android.NewRuleBuilder()
Nan Zhang1598a9e2018-09-04 17:14:32 -07001022
Colin Crossab054432019-07-15 16:13:59 -07001023 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
1024 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -07001025
Colin Crossab054432019-07-15 16:13:59 -07001026 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1027
1028 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -07001029 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -07001030 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001031 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -07001032 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -07001033 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001034 }
1035
Colin Crossab054432019-07-15 16:13:59 -07001036 d.stubsFlags(ctx, cmd, stubsDir)
1037
1038 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1039
1040 var desc string
1041 if Bool(d.properties.Dokka_enabled) {
1042 desc = "dokka"
1043 } else {
1044 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
1045
1046 for _, o := range d.Javadoc.properties.Out {
1047 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1048 }
1049
1050 d.postDoclavaCmds(ctx, rule)
1051 desc = "doclava"
1052 }
1053
1054 rule.Command().
1055 BuiltTool(ctx, "soong_zip").
1056 Flag("-write_if_changed").
1057 Flag("-d").
1058 FlagWithOutput("-o ", d.docZip).
1059 FlagWithArg("-C ", outDir.String()).
1060 FlagWithArg("-D ", outDir.String())
1061
1062 rule.Command().
1063 BuiltTool(ctx, "soong_zip").
1064 Flag("-write_if_changed").
1065 Flag("-jar").
1066 FlagWithOutput("-o ", d.stubsSrcJar).
1067 FlagWithArg("-C ", stubsDir.String()).
1068 FlagWithArg("-D ", stubsDir.String())
1069
1070 rule.Restat()
1071
1072 zipSyncCleanupCmd(rule, srcJarDir)
1073
1074 rule.Build(pctx, ctx, "javadoc", desc)
1075
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001076 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001077 !ctx.Config().IsPdkBuild() {
Colin Crossab054432019-07-15 16:13:59 -07001078
1079 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1080 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Nan Zhang1598a9e2018-09-04 17:14:32 -07001081
1082 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001083
1084 rule := android.NewRuleBuilder()
1085
1086 rule.Command().Text("( true")
1087
1088 rule.Command().
1089 BuiltTool(ctx, "apicheck").
1090 Flag("-JXmx1024m").
1091 FlagWithInputList("-Jclasspath\\ ", checkApiClasspath.Paths(), ":").
1092 OptionalFlag(d.properties.Check_api.Current.Args).
1093 Input(apiFile).
1094 Input(d.apiFile).
1095 Input(removedApiFile).
1096 Input(d.removedApiFile)
1097
1098 msg := fmt.Sprintf(`\n******************************\n`+
1099 `You have tried to change the API from what has been previously approved.\n\n`+
1100 `To make these errors go away, you have two choices:\n`+
1101 ` 1. You can add '@hide' javadoc comments to the methods, etc. listed in the\n`+
1102 ` errors above.\n\n`+
1103 ` 2. You can update current.txt by executing the following command:\n`+
1104 ` make %s-update-current-api\n\n`+
1105 ` To submit the revised current.txt to the main Android repository,\n`+
1106 ` you will need approval.\n`+
1107 `******************************\n`, ctx.ModuleName())
1108
1109 rule.Command().
1110 Text("touch").Output(d.checkCurrentApiTimestamp).
1111 Text(") || (").
1112 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1113 Text("; exit 38").
1114 Text(")")
1115
1116 rule.Build(pctx, ctx, "doclavaCurrentApiCheck", "check current API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001117
1118 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001119
1120 // update API rule
1121 rule = android.NewRuleBuilder()
1122
1123 rule.Command().Text("( true")
1124
1125 rule.Command().
1126 Text("cp").Flag("-f").
1127 Input(d.apiFile).Flag(apiFile.String())
1128
1129 rule.Command().
1130 Text("cp").Flag("-f").
1131 Input(d.removedApiFile).Flag(removedApiFile.String())
1132
1133 msg = "failed to update public API"
1134
1135 rule.Command().
1136 Text("touch").Output(d.updateCurrentApiTimestamp).
1137 Text(") || (").
1138 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1139 Text("; exit 38").
1140 Text(")")
1141
1142 rule.Build(pctx, ctx, "doclavaCurrentApiUpdate", "update current API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001143 }
1144
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001145 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001146 !ctx.Config().IsPdkBuild() {
Colin Crossab054432019-07-15 16:13:59 -07001147
1148 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1149 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
Nan Zhang1598a9e2018-09-04 17:14:32 -07001150
1151 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001152
1153 rule := android.NewRuleBuilder()
1154
1155 rule.Command().
1156 Text("(").
1157 BuiltTool(ctx, "apicheck").
1158 Flag("-JXmx1024m").
1159 FlagWithInputList("-Jclasspath\\ ", checkApiClasspath.Paths(), ":").
1160 OptionalFlag(d.properties.Check_api.Last_released.Args).
1161 Input(apiFile).
1162 Input(d.apiFile).
1163 Input(removedApiFile).
1164 Input(d.removedApiFile)
1165
1166 msg := `\n******************************\n` +
1167 `You have tried to change the API from what has been previously released in\n` +
1168 `an SDK. Please fix the errors listed above.\n` +
1169 `******************************\n`
1170
1171 rule.Command().
1172 Text("touch").Output(d.checkLastReleasedApiTimestamp).
1173 Text(") || (").
1174 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1175 Text("; exit 38").
1176 Text(")")
1177
1178 rule.Build(pctx, ctx, "doclavaLastApiCheck", "check last API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001179 }
1180}
1181
1182//
1183// Droidstubs
1184//
1185type Droidstubs struct {
1186 Javadoc
Paul Duffin91547182019-11-12 19:39:36 +00001187 android.SdkBase
Nan Zhang1598a9e2018-09-04 17:14:32 -07001188
Pete Gillin581d6082018-10-22 15:55:04 +01001189 properties DroidstubsProperties
1190 apiFile android.WritablePath
1191 apiXmlFile android.WritablePath
1192 lastReleasedApiXmlFile android.WritablePath
1193 dexApiFile android.WritablePath
1194 privateApiFile android.WritablePath
1195 privateDexApiFile android.WritablePath
1196 removedApiFile android.WritablePath
1197 removedDexApiFile android.WritablePath
1198 apiMappingFile android.WritablePath
1199 exactApiFile android.WritablePath
1200 proguardFile android.WritablePath
1201 nullabilityWarningsFile android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001202
1203 checkCurrentApiTimestamp android.WritablePath
1204 updateCurrentApiTimestamp android.WritablePath
1205 checkLastReleasedApiTimestamp android.WritablePath
Adrian Roos075eedc2019-10-10 12:07:03 +02001206 apiLintTimestamp android.WritablePath
Adrian Roos3b8f1cd2019-11-01 13:42:39 +01001207 apiLintReport android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001208
Pete Gillin581d6082018-10-22 15:55:04 +01001209 checkNullabilityWarningsTimestamp android.WritablePath
1210
Nan Zhang1598a9e2018-09-04 17:14:32 -07001211 annotationsZip android.WritablePath
Nan Zhang9c69a122018-08-22 10:22:08 -07001212 apiVersionsXml android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001213
1214 apiFilePath android.Path
Nan Zhang71bbe632018-09-17 14:32:21 -07001215
1216 jdiffDocZip android.WritablePath
1217 jdiffStubsSrcJar android.WritablePath
Jerome Gaillard0f599032019-10-10 19:29:11 +01001218
1219 metadataZip android.WritablePath
1220 metadataDir android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001221}
1222
Colin Crossa3002fc2019-07-08 16:48:04 -07001223// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
1224// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to
1225// a droiddoc module to generate documentation.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001226func DroidstubsFactory() android.Module {
1227 module := &Droidstubs{}
1228
1229 module.AddProperties(&module.properties,
1230 &module.Javadoc.properties)
1231
1232 InitDroiddocModule(module, android.HostAndDeviceSupported)
Paul Duffin91547182019-11-12 19:39:36 +00001233 android.InitSdkAwareModule(module)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001234 return module
1235}
1236
Colin Crossa3002fc2019-07-08 16:48:04 -07001237// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API
1238// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be
1239// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs
1240// module when symbols needed by the source files are provided by java_library_host modules.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001241func DroidstubsHostFactory() android.Module {
1242 module := &Droidstubs{}
1243
1244 module.AddProperties(&module.properties,
1245 &module.Javadoc.properties)
1246
1247 InitDroiddocModule(module, android.HostSupported)
1248 return module
1249}
1250
1251func (d *Droidstubs) ApiFilePath() android.Path {
1252 return d.apiFilePath
1253}
1254
1255func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
1256 d.Javadoc.addDeps(ctx)
1257
Inseob Kim38449af2019-02-28 14:24:05 +09001258 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
1259 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
1260 }
1261
Nan Zhang1598a9e2018-09-04 17:14:32 -07001262 if len(d.properties.Merge_annotations_dirs) != 0 {
1263 for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
1264 ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
1265 }
1266 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001267
Pete Gillin77167902018-09-19 18:16:26 +01001268 if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
1269 for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
1270 ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
1271 }
1272 }
1273
Nan Zhang9c69a122018-08-22 10:22:08 -07001274 if len(d.properties.Api_levels_annotations_dirs) != 0 {
1275 for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
1276 ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
1277 }
1278 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001279}
1280
Colin Cross33961b52019-07-11 11:01:22 -07001281func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.WritablePath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001282 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1283 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001284 String(d.properties.Api_filename) != "" {
1285 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001286 cmd.FlagWithOutput("--api ", d.apiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001287 d.apiFilePath = d.apiFile
1288 }
1289
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001290 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1291 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001292 String(d.properties.Removed_api_filename) != "" {
1293 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001294 cmd.FlagWithOutput("--removed-api ", d.removedApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001295 }
1296
1297 if String(d.properties.Private_api_filename) != "" {
1298 d.privateApiFile = android.PathForModuleOut(ctx, String(d.properties.Private_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001299 cmd.FlagWithOutput("--private-api ", d.privateApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001300 }
1301
1302 if String(d.properties.Dex_api_filename) != "" {
1303 d.dexApiFile = android.PathForModuleOut(ctx, String(d.properties.Dex_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001304 cmd.FlagWithOutput("--dex-api ", d.dexApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001305 }
1306
1307 if String(d.properties.Private_dex_api_filename) != "" {
1308 d.privateDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Private_dex_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001309 cmd.FlagWithOutput("--private-dex-api ", d.privateDexApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001310 }
1311
1312 if String(d.properties.Removed_dex_api_filename) != "" {
1313 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001314 cmd.FlagWithOutput("--removed-dex-api ", d.removedDexApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001315 }
1316
1317 if String(d.properties.Exact_api_filename) != "" {
1318 d.exactApiFile = android.PathForModuleOut(ctx, String(d.properties.Exact_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001319 cmd.FlagWithOutput("--exact-api ", d.exactApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001320 }
1321
Nan Zhang9c69a122018-08-22 10:22:08 -07001322 if String(d.properties.Dex_mapping_filename) != "" {
1323 d.apiMappingFile = android.PathForModuleOut(ctx, String(d.properties.Dex_mapping_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001324 cmd.FlagWithOutput("--dex-api-mapping ", d.apiMappingFile)
Nan Zhang9c69a122018-08-22 10:22:08 -07001325 }
1326
Nan Zhang199645c2018-09-19 12:40:06 -07001327 if String(d.properties.Proguard_filename) != "" {
1328 d.proguardFile = android.PathForModuleOut(ctx, String(d.properties.Proguard_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001329 cmd.FlagWithOutput("--proguard ", d.proguardFile)
Nan Zhang199645c2018-09-19 12:40:06 -07001330 }
1331
Nan Zhang9c69a122018-08-22 10:22:08 -07001332 if Bool(d.properties.Write_sdk_values) {
Jerome Gaillard0f599032019-10-10 19:29:11 +01001333 d.metadataDir = android.PathForModuleOut(ctx, "metadata")
1334 cmd.FlagWithArg("--sdk-values ", d.metadataDir.String())
Nan Zhang9c69a122018-08-22 10:22:08 -07001335 }
1336
Nan Zhang1598a9e2018-09-04 17:14:32 -07001337 if Bool(d.properties.Create_doc_stubs) {
Colin Cross33961b52019-07-11 11:01:22 -07001338 cmd.FlagWithArg("--doc-stubs ", stubsDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -07001339 } else {
Colin Cross33961b52019-07-11 11:01:22 -07001340 cmd.FlagWithArg("--stubs ", stubsDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -07001341 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001342}
1343
Colin Cross33961b52019-07-11 11:01:22 -07001344func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang1598a9e2018-09-04 17:14:32 -07001345 if Bool(d.properties.Annotations_enabled) {
Colin Cross33961b52019-07-11 11:01:22 -07001346 cmd.Flag("--include-annotations")
1347
Pete Gillinc382a562018-11-14 18:45:46 +00001348 validatingNullability :=
1349 strings.Contains(d.Javadoc.args, "--validate-nullability-from-merged-stubs") ||
1350 String(d.properties.Validate_nullability_from_list) != ""
Paul Duffin13a9dd62019-11-04 10:26:47 +00001351
Pete Gillina262c052018-09-14 14:25:48 +01001352 migratingNullability := String(d.properties.Previous_api) != ""
Pete Gillina262c052018-09-14 14:25:48 +01001353 if migratingNullability {
Colin Cross8a497952019-03-05 22:25:09 -08001354 previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
Colin Cross33961b52019-07-11 11:01:22 -07001355 cmd.FlagWithInput("--migrate-nullness ", previousApi)
Pete Gillina262c052018-09-14 14:25:48 +01001356 }
Colin Cross33961b52019-07-11 11:01:22 -07001357
Pete Gillinc382a562018-11-14 18:45:46 +00001358 if s := String(d.properties.Validate_nullability_from_list); s != "" {
Colin Cross33961b52019-07-11 11:01:22 -07001359 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
Pete Gillinc382a562018-11-14 18:45:46 +00001360 }
Colin Cross33961b52019-07-11 11:01:22 -07001361
Pete Gillina262c052018-09-14 14:25:48 +01001362 if validatingNullability {
Pete Gillin581d6082018-10-22 15:55:04 +01001363 d.nullabilityWarningsFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_nullability_warnings.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001364 cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile)
Pete Gillina262c052018-09-14 14:25:48 +01001365 }
Nan Zhanga40da042018-08-01 12:48:00 -07001366
1367 d.annotationsZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"_annotations.zip")
Colin Cross33961b52019-07-11 11:01:22 -07001368 cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip)
Nan Zhangf4936b02018-08-01 15:00:28 -07001369
Nan Zhang1598a9e2018-09-04 17:14:32 -07001370 if len(d.properties.Merge_annotations_dirs) == 0 {
Nan Zhang9c69a122018-08-22 10:22:08 -07001371 ctx.PropertyErrorf("merge_annotations_dirs",
Nan Zhanga40da042018-08-01 12:48:00 -07001372 "has to be non-empty if annotations was enabled!")
1373 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001374
Colin Cross33961b52019-07-11 11:01:22 -07001375 d.mergeAnnoDirFlags(ctx, cmd)
1376
1377 // TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
1378 cmd.FlagWithArg("--hide ", "HiddenTypedefConstant").
1379 FlagWithArg("--hide ", "SuperfluousPrefix").
1380 FlagWithArg("--hide ", "AnnotationExtraction")
1381 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001382}
1383
Colin Cross33961b52019-07-11 11:01:22 -07001384func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
1385 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
1386 if t, ok := m.(*ExportedDroiddocDir); ok {
1387 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
1388 } else {
1389 ctx.PropertyErrorf("merge_annotations_dirs",
1390 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1391 }
1392 })
1393}
1394
1395func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Pete Gillin77167902018-09-19 18:16:26 +01001396 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
1397 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Cross33961b52019-07-11 11:01:22 -07001398 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
Pete Gillin77167902018-09-19 18:16:26 +01001399 } else {
1400 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
1401 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1402 }
1403 })
Nan Zhanga40da042018-08-01 12:48:00 -07001404}
1405
Colin Cross33961b52019-07-11 11:01:22 -07001406func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang9c69a122018-08-22 10:22:08 -07001407 if Bool(d.properties.Api_levels_annotations_enabled) {
1408 d.apiVersionsXml = android.PathForModuleOut(ctx, "api-versions.xml")
Nan Zhang9c69a122018-08-22 10:22:08 -07001409
1410 if len(d.properties.Api_levels_annotations_dirs) == 0 {
1411 ctx.PropertyErrorf("api_levels_annotations_dirs",
1412 "has to be non-empty if api levels annotations was enabled!")
1413 }
1414
Colin Cross33961b52019-07-11 11:01:22 -07001415 cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml)
1416 cmd.FlagWithInput("--apply-api-levels ", d.apiVersionsXml)
1417 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion())
1418 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
Nan Zhang9c69a122018-08-22 10:22:08 -07001419
1420 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
1421 if t, ok := m.(*ExportedDroiddocDir); ok {
Nan Zhang9c69a122018-08-22 10:22:08 -07001422 for _, dep := range t.deps {
1423 if strings.HasSuffix(dep.String(), "android.jar") {
Colin Cross33961b52019-07-11 11:01:22 -07001424 cmd.Implicit(dep)
Nan Zhang9c69a122018-08-22 10:22:08 -07001425 }
1426 }
Colin Cross33961b52019-07-11 11:01:22 -07001427 cmd.FlagWithArg("--android-jar-pattern ", t.dir.String()+"/%/public/android.jar")
Nan Zhang9c69a122018-08-22 10:22:08 -07001428 } else {
1429 ctx.PropertyErrorf("api_levels_annotations_dirs",
1430 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
1431 }
1432 })
1433
1434 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001435}
1436
Colin Cross33961b52019-07-11 11:01:22 -07001437func (d *Droidstubs) apiToXmlFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang71bbe632018-09-17 14:32:21 -07001438 if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() {
1439 if d.apiFile.String() == "" {
1440 ctx.ModuleErrorf("API signature file has to be specified in Metalava when jdiff is enabled.")
1441 }
1442
1443 d.apiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001444 cmd.FlagWithOutput("--api-xml ", d.apiXmlFile)
Nan Zhang71bbe632018-09-17 14:32:21 -07001445
1446 if String(d.properties.Check_api.Last_released.Api_file) == "" {
1447 ctx.PropertyErrorf("check_api.last_released.api_file",
1448 "has to be non-empty if jdiff was enabled!")
1449 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001450
Colin Cross33961b52019-07-11 11:01:22 -07001451 lastReleasedApi := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
Nan Zhang71bbe632018-09-17 14:32:21 -07001452 d.lastReleasedApiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_last_released_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001453 cmd.FlagWithInput("--convert-to-jdiff ", lastReleasedApi).Output(d.lastReleasedApiXmlFile)
1454 }
1455}
Nan Zhang71bbe632018-09-17 14:32:21 -07001456
Colin Cross1e743852019-10-28 11:37:20 -07001457func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths,
Colin Cross33961b52019-07-11 11:01:22 -07001458 srcJarList android.Path, bootclasspath, classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
1459 cmd := rule.Command().BuiltTool(ctx, "metalava").
1460 Flag(config.JavacVmFlags).
1461 FlagWithArg("-encoding ", "UTF-8").
Colin Cross1e743852019-10-28 11:37:20 -07001462 FlagWithArg("-source ", javaVersion.String()).
Colin Cross33961b52019-07-11 11:01:22 -07001463 FlagWithRspFileInputList("@", srcs).
1464 FlagWithInput("@", srcJarList)
1465
1466 if len(bootclasspath) > 0 {
1467 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
Nan Zhang71bbe632018-09-17 14:32:21 -07001468 }
1469
Colin Cross33961b52019-07-11 11:01:22 -07001470 if len(classpath) > 0 {
1471 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
1472 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001473
Colin Cross33961b52019-07-11 11:01:22 -07001474 if len(sourcepaths) > 0 {
1475 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
1476 } else {
1477 cmd.FlagWithArg("-sourcepath ", `""`)
1478 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001479
Colin Cross33961b52019-07-11 11:01:22 -07001480 cmd.Flag("--no-banner").
1481 Flag("--color").
1482 Flag("--quiet").
1483 Flag("--format=v2")
Nan Zhang86d2d552018-08-09 15:33:27 -07001484
Colin Cross33961b52019-07-11 11:01:22 -07001485 return cmd
Nan Zhang71bbe632018-09-17 14:32:21 -07001486}
1487
Nan Zhang1598a9e2018-09-04 17:14:32 -07001488func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga40da042018-08-01 12:48:00 -07001489 deps := d.Javadoc.collectDeps(ctx)
1490
1491 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), sdkContext(d))
Nan Zhang581fd212018-01-10 16:06:12 -08001492
Colin Cross33961b52019-07-11 11:01:22 -07001493 // Create rule for metalava
Nan Zhanga40da042018-08-01 12:48:00 -07001494
Colin Crossdaa4c672019-07-15 22:53:46 -07001495 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
Nan Zhanga40da042018-08-01 12:48:00 -07001496
Colin Cross33961b52019-07-11 11:01:22 -07001497 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
1498 stubsDir := android.PathForModuleOut(ctx, "stubsDir")
Nan Zhang71bbe632018-09-17 14:32:21 -07001499
Colin Cross33961b52019-07-11 11:01:22 -07001500 rule := android.NewRuleBuilder()
Nan Zhanga40da042018-08-01 12:48:00 -07001501
Colin Cross33961b52019-07-11 11:01:22 -07001502 rule.Command().Text("rm -rf").Text(stubsDir.String())
1503 rule.Command().Text("mkdir -p").Text(stubsDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -07001504
Colin Cross33961b52019-07-11 11:01:22 -07001505 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1506
1507 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
1508 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
1509
1510 d.stubsFlags(ctx, cmd, stubsDir)
1511
1512 d.annotationsFlags(ctx, cmd)
1513 d.inclusionAnnotationsFlags(ctx, cmd)
1514 d.apiLevelsAnnotationsFlags(ctx, cmd)
1515 d.apiToXmlFlags(ctx, cmd)
Nan Zhang71bbe632018-09-17 14:32:21 -07001516
Nan Zhang1598a9e2018-09-04 17:14:32 -07001517 if strings.Contains(d.Javadoc.args, "--generate-documentation") {
1518 // Currently Metalava have the ability to invoke Javadoc in a seperate process.
1519 // Pass "-nodocs" to suppress the Javadoc invocation when Metalava receives
1520 // "--generate-documentation" arg. This is not needed when Metalava removes this feature.
1521 d.Javadoc.args = d.Javadoc.args + " -nodocs "
Nan Zhang79614d12018-04-19 18:03:39 -07001522 }
Colin Cross33961b52019-07-11 11:01:22 -07001523
1524 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1525 for _, o := range d.Javadoc.properties.Out {
1526 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1527 }
1528
1529 rule.Command().
1530 BuiltTool(ctx, "soong_zip").
1531 Flag("-write_if_changed").
1532 Flag("-jar").
1533 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
1534 FlagWithArg("-C ", stubsDir.String()).
1535 FlagWithArg("-D ", stubsDir.String())
Jerome Gaillard0f599032019-10-10 19:29:11 +01001536
1537 if Bool(d.properties.Write_sdk_values) {
1538 d.metadataZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-metadata.zip")
1539 rule.Command().
1540 BuiltTool(ctx, "soong_zip").
1541 Flag("-write_if_changed").
1542 Flag("-d").
1543 FlagWithOutput("-o ", d.metadataZip).
1544 FlagWithArg("-C ", d.metadataDir.String()).
1545 FlagWithArg("-D ", d.metadataDir.String())
1546 }
1547
Colin Cross33961b52019-07-11 11:01:22 -07001548 rule.Restat()
1549
1550 zipSyncCleanupCmd(rule, srcJarDir)
1551
1552 rule.Build(pctx, ctx, "metalava", "metalava")
1553
1554 // Create rule for apicheck
Nan Zhang61819ce2018-05-04 18:49:16 -07001555
Adrian Roos075eedc2019-10-10 12:07:03 +02001556 if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) && !ctx.Config().IsPdkBuild() {
1557 rule := android.NewRuleBuilder()
1558 rule.Command().Text("( true")
1559
1560 srcJarDir := android.PathForModuleOut(ctx, "api_lint", "srcjars")
1561 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1562
1563 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
1564 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
1565
Adrian Rooscab4a2c2019-10-14 16:32:41 +02001566 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1567
Adrian Roos075eedc2019-10-10 12:07:03 +02001568 newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since)
1569 if newSince.Valid() {
1570 cmd.FlagWithInput("--api-lint ", newSince.Path())
1571 } else {
1572 cmd.Flag("--api-lint")
1573 }
Adrian Roos3b8f1cd2019-11-01 13:42:39 +01001574 d.apiLintReport = android.PathForModuleOut(ctx, "api_lint_report.txt")
1575 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport)
Adrian Roos075eedc2019-10-10 12:07:03 +02001576
1577 d.inclusionAnnotationsFlags(ctx, cmd)
1578 d.mergeAnnoDirFlags(ctx, cmd)
1579
1580 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
1581 updatedBaselineOutput := android.PathForModuleOut(ctx, "api_lint_baseline.txt")
1582 d.apiLintTimestamp = android.PathForModuleOut(ctx, "api_lint.timestamp")
1583
1584 if baselineFile.Valid() {
1585 cmd.FlagWithInput("--baseline ", baselineFile.Path())
1586 cmd.FlagWithOutput("--update-baseline ", updatedBaselineOutput)
1587 }
1588
1589 zipSyncCleanupCmd(rule, srcJarDir)
1590
1591 msg := fmt.Sprintf(`\n******************************\n`+
1592 `Your API changes are triggering API Lint warnings or errors.\n\n`+
1593 `To make these errors go away, you have two choices:\n`+
1594 ` 1. You can suppress the errors with @SuppressLint(\"<id>\").\n\n`+
1595 ` 2. You can update the baseline by executing the following command:\n`+
1596 ` cp \"$PWD/%s\" \"$PWD/%s\"\n\n`+
1597 `******************************\n`, updatedBaselineOutput, baselineFile.Path())
1598 rule.Command().
1599 Text("touch").Output(d.apiLintTimestamp).
1600 Text(") || (").
1601 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1602 Text("; exit 38").
1603 Text(")")
1604
1605 rule.Build(pctx, ctx, "metalavaApiLint", "metalava API lint")
1606
1607 }
1608
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001609 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001610 !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001611
1612 if len(d.Javadoc.properties.Out) > 0 {
1613 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1614 }
1615
1616 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1617 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Adrian Roos14f75a92019-08-12 17:54:09 +02001618 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
1619 updatedBaselineOutput := android.PathForModuleOut(ctx, "current_baseline.txt")
Nan Zhang61819ce2018-05-04 18:49:16 -07001620
Nan Zhang2760dfc2018-08-24 17:32:54 +00001621 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Nan Zhang2760dfc2018-08-24 17:32:54 +00001622
Colin Cross33961b52019-07-11 11:01:22 -07001623 rule := android.NewRuleBuilder()
1624
1625 rule.Command().Text("( true")
1626
1627 srcJarDir := android.PathForModuleOut(ctx, "current-apicheck", "srcjars")
1628 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1629
1630 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
1631 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
1632
1633 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles).
1634 FlagWithInput("--check-compatibility:api:current ", apiFile).
1635 FlagWithInput("--check-compatibility:removed:current ", removedApiFile)
1636
1637 d.inclusionAnnotationsFlags(ctx, cmd)
1638 d.mergeAnnoDirFlags(ctx, cmd)
1639
Adrian Roos14f75a92019-08-12 17:54:09 +02001640 if baselineFile.Valid() {
1641 cmd.FlagWithInput("--baseline ", baselineFile.Path())
1642 cmd.FlagWithOutput("--update-baseline ", updatedBaselineOutput)
1643 }
1644
Colin Cross33961b52019-07-11 11:01:22 -07001645 zipSyncCleanupCmd(rule, srcJarDir)
1646
1647 msg := fmt.Sprintf(`\n******************************\n`+
1648 `You have tried to change the API from what has been previously approved.\n\n`+
1649 `To make these errors go away, you have two choices:\n`+
1650 ` 1. You can add '@hide' javadoc comments to the methods, etc. listed in the\n`+
1651 ` errors above.\n\n`+
1652 ` 2. You can update current.txt by executing the following command:\n`+
1653 ` make %s-update-current-api\n\n`+
1654 ` To submit the revised current.txt to the main Android repository,\n`+
1655 ` you will need approval.\n`+
1656 `******************************\n`, ctx.ModuleName())
1657
1658 rule.Command().
1659 Text("touch").Output(d.checkCurrentApiTimestamp).
1660 Text(") || (").
1661 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1662 Text("; exit 38").
1663 Text(")")
1664
1665 rule.Build(pctx, ctx, "metalavaCurrentApiCheck", "metalava check current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001666
1667 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001668
1669 // update API rule
1670 rule = android.NewRuleBuilder()
1671
1672 rule.Command().Text("( true")
1673
1674 rule.Command().
1675 Text("cp").Flag("-f").
1676 Input(d.apiFile).Flag(apiFile.String())
1677
1678 rule.Command().
1679 Text("cp").Flag("-f").
1680 Input(d.removedApiFile).Flag(removedApiFile.String())
1681
1682 msg = "failed to update public API"
1683
1684 rule.Command().
1685 Text("touch").Output(d.updateCurrentApiTimestamp).
1686 Text(") || (").
1687 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1688 Text("; exit 38").
1689 Text(")")
1690
1691 rule.Build(pctx, ctx, "metalavaCurrentApiUpdate", "update current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001692 }
Nan Zhanga40da042018-08-01 12:48:00 -07001693
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001694 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001695 !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001696
1697 if len(d.Javadoc.properties.Out) > 0 {
1698 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1699 }
1700
1701 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1702 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
Adrian Roos14f75a92019-08-12 17:54:09 +02001703 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
1704 updatedBaselineOutput := android.PathForModuleOut(ctx, "last_released_baseline.txt")
Nan Zhang61819ce2018-05-04 18:49:16 -07001705
Nan Zhang2760dfc2018-08-24 17:32:54 +00001706 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
Nan Zhang2760dfc2018-08-24 17:32:54 +00001707
Colin Cross33961b52019-07-11 11:01:22 -07001708 rule := android.NewRuleBuilder()
1709
1710 rule.Command().Text("( true")
1711
1712 srcJarDir := android.PathForModuleOut(ctx, "last-apicheck", "srcjars")
1713 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1714
1715 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
1716 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
1717
1718 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles).
1719 FlagWithInput("--check-compatibility:api:released ", apiFile)
1720
1721 d.inclusionAnnotationsFlags(ctx, cmd)
1722
1723 cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile)
1724
1725 d.mergeAnnoDirFlags(ctx, cmd)
1726
Adrian Roos14f75a92019-08-12 17:54:09 +02001727 if baselineFile.Valid() {
1728 cmd.FlagWithInput("--baseline ", baselineFile.Path())
1729 cmd.FlagWithOutput("--update-baseline ", updatedBaselineOutput)
1730 }
1731
Colin Cross33961b52019-07-11 11:01:22 -07001732 zipSyncCleanupCmd(rule, srcJarDir)
1733
1734 msg := `\n******************************\n` +
1735 `You have tried to change the API from what has been previously released in\n` +
1736 `an SDK. Please fix the errors listed above.\n` +
1737 `******************************\n`
1738 rule.Command().
1739 Text("touch").Output(d.checkLastReleasedApiTimestamp).
1740 Text(") || (").
1741 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1742 Text("; exit 38").
1743 Text(")")
1744
1745 rule.Build(pctx, ctx, "metalavaLastApiCheck", "metalava check last API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001746 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001747
Pete Gillin581d6082018-10-22 15:55:04 +01001748 if String(d.properties.Check_nullability_warnings) != "" {
1749 if d.nullabilityWarningsFile == nil {
1750 ctx.PropertyErrorf("check_nullability_warnings",
1751 "Cannot specify check_nullability_warnings unless validating nullability")
1752 }
Colin Cross33961b52019-07-11 11:01:22 -07001753
1754 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
1755
Pete Gillin581d6082018-10-22 15:55:04 +01001756 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "check_nullability_warnings.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001757
Pete Gillin581d6082018-10-22 15:55:04 +01001758 msg := fmt.Sprintf(`\n******************************\n`+
1759 `The warnings encountered during nullability annotation validation did\n`+
1760 `not match the checked in file of expected warnings. The diffs are shown\n`+
1761 `above. You have two options:\n`+
1762 ` 1. Resolve the differences by editing the nullability annotations.\n`+
1763 ` 2. Update the file of expected warnings by running:\n`+
1764 ` cp %s %s\n`+
1765 ` and submitting the updated file as part of your change.`,
1766 d.nullabilityWarningsFile, checkNullabilityWarnings)
Colin Cross33961b52019-07-11 11:01:22 -07001767
1768 rule := android.NewRuleBuilder()
1769
1770 rule.Command().
1771 Text("(").
1772 Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile).
1773 Text("&&").
1774 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
1775 Text(") || (").
1776 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1777 Text("; exit 38").
1778 Text(")")
1779
1780 rule.Build(pctx, ctx, "nullabilityWarningsCheck", "nullability warnings check")
Pete Gillin581d6082018-10-22 15:55:04 +01001781 }
1782
Nan Zhang71bbe632018-09-17 14:32:21 -07001783 if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001784 if len(d.Javadoc.properties.Out) > 0 {
1785 ctx.PropertyErrorf("out", "out property may not be combined with jdiff")
1786 }
1787
1788 outDir := android.PathForModuleOut(ctx, "jdiff-out")
1789 srcJarDir := android.PathForModuleOut(ctx, "jdiff-srcjars")
1790 stubsDir := android.PathForModuleOut(ctx, "jdiff-stubsDir")
1791
1792 rule := android.NewRuleBuilder()
Nan Zhang71bbe632018-09-17 14:32:21 -07001793
Nan Zhang86b06202018-09-21 17:09:21 -07001794 // Please sync with android-api-council@ before making any changes for the name of jdiffDocZip below
1795 // since there's cron job downstream that fetch this .zip file periodically.
1796 // See b/116221385 for reference.
Nan Zhang71bbe632018-09-17 14:32:21 -07001797 d.jdiffDocZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-docs.zip")
1798 d.jdiffStubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-stubs.srcjar")
1799
Nan Zhang71bbe632018-09-17 14:32:21 -07001800 jdiff := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jdiff.jar")
Nan Zhang71bbe632018-09-17 14:32:21 -07001801
Colin Cross33961b52019-07-11 11:01:22 -07001802 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
1803 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang71bbe632018-09-17 14:32:21 -07001804
Colin Cross33961b52019-07-11 11:01:22 -07001805 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1806
Colin Crossdaa4c672019-07-15 22:53:46 -07001807 cmd := javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -07001808 deps.bootClasspath, deps.classpath, d.sourcepaths)
1809
1810 cmd.Flag("-J-Xmx1600m").
Colin Cross33961b52019-07-11 11:01:22 -07001811 Flag("-XDignore.symbol.file").
1812 FlagWithArg("-doclet ", "jdiff.JDiff").
1813 FlagWithInput("-docletpath ", jdiff).
1814 Flag("-quiet").
1815 FlagWithArg("-newapi ", strings.TrimSuffix(d.apiXmlFile.Base(), d.apiXmlFile.Ext())).
1816 FlagWithArg("-newapidir ", filepath.Dir(d.apiXmlFile.String())).
1817 Implicit(d.apiXmlFile).
1818 FlagWithArg("-oldapi ", strings.TrimSuffix(d.lastReleasedApiXmlFile.Base(), d.lastReleasedApiXmlFile.Ext())).
1819 FlagWithArg("-oldapidir ", filepath.Dir(d.lastReleasedApiXmlFile.String())).
1820 Implicit(d.lastReleasedApiXmlFile)
1821
Colin Cross33961b52019-07-11 11:01:22 -07001822 rule.Command().
1823 BuiltTool(ctx, "soong_zip").
1824 Flag("-write_if_changed").
1825 Flag("-d").
1826 FlagWithOutput("-o ", d.jdiffDocZip).
1827 FlagWithArg("-C ", outDir.String()).
1828 FlagWithArg("-D ", outDir.String())
1829
1830 rule.Command().
1831 BuiltTool(ctx, "soong_zip").
1832 Flag("-write_if_changed").
1833 Flag("-jar").
1834 FlagWithOutput("-o ", d.jdiffStubsSrcJar).
1835 FlagWithArg("-C ", stubsDir.String()).
1836 FlagWithArg("-D ", stubsDir.String())
1837
1838 rule.Restat()
1839
1840 zipSyncCleanupCmd(rule, srcJarDir)
1841
1842 rule.Build(pctx, ctx, "jdiff", "jdiff")
Nan Zhang71bbe632018-09-17 14:32:21 -07001843 }
Nan Zhang581fd212018-01-10 16:06:12 -08001844}
Dan Willemsencc090972018-02-26 14:33:31 -08001845
Nan Zhanga40da042018-08-01 12:48:00 -07001846//
Nan Zhangf4936b02018-08-01 15:00:28 -07001847// Exported Droiddoc Directory
Nan Zhanga40da042018-08-01 12:48:00 -07001848//
Dan Willemsencc090972018-02-26 14:33:31 -08001849var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
Nan Zhangf4936b02018-08-01 15:00:28 -07001850var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
Pete Gillin77167902018-09-19 18:16:26 +01001851var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
Nan Zhang9c69a122018-08-22 10:22:08 -07001852var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
Dan Willemsencc090972018-02-26 14:33:31 -08001853
Nan Zhangf4936b02018-08-01 15:00:28 -07001854type ExportedDroiddocDirProperties struct {
1855 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -08001856 Path *string
1857}
1858
Nan Zhangf4936b02018-08-01 15:00:28 -07001859type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -08001860 android.ModuleBase
1861
Nan Zhangf4936b02018-08-01 15:00:28 -07001862 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -08001863
1864 deps android.Paths
1865 dir android.Path
1866}
1867
Colin Crossa3002fc2019-07-08 16:48:04 -07001868// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -07001869func ExportedDroiddocDirFactory() android.Module {
1870 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -08001871 module.AddProperties(&module.properties)
1872 android.InitAndroidModule(module)
1873 return module
1874}
1875
Nan Zhangf4936b02018-08-01 15:00:28 -07001876func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -08001877
Nan Zhangf4936b02018-08-01 15:00:28 -07001878func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -08001879 path := String(d.properties.Path)
1880 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -08001881 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Dan Willemsencc090972018-02-26 14:33:31 -08001882}
Nan Zhangb2b33de2018-02-23 11:18:47 -08001883
1884//
1885// Defaults
1886//
1887type DocDefaults struct {
1888 android.ModuleBase
1889 android.DefaultsModuleBase
1890}
1891
Nan Zhangb2b33de2018-02-23 11:18:47 -08001892func DocDefaultsFactory() android.Module {
1893 module := &DocDefaults{}
1894
1895 module.AddProperties(
1896 &JavadocProperties{},
1897 &DroiddocProperties{},
1898 )
1899
1900 android.InitDefaultsModule(module)
1901
1902 return module
1903}
Nan Zhang1598a9e2018-09-04 17:14:32 -07001904
1905func StubsDefaultsFactory() android.Module {
1906 module := &DocDefaults{}
1907
1908 module.AddProperties(
1909 &JavadocProperties{},
1910 &DroidstubsProperties{},
1911 )
1912
1913 android.InitDefaultsModule(module)
1914
1915 return module
1916}
Colin Cross33961b52019-07-11 11:01:22 -07001917
1918func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
1919 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
1920
1921 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1922 rule.Command().Text("mkdir -p").Text(srcJarDir.String())
1923 srcJarList := srcJarDir.Join(ctx, "list")
1924
1925 rule.Temporary(srcJarList)
1926
1927 rule.Command().BuiltTool(ctx, "zipsync").
1928 FlagWithArg("-d ", srcJarDir.String()).
1929 FlagWithOutput("-l ", srcJarList).
1930 FlagWithArg("-f ", `"*.java"`).
1931 Inputs(srcJars)
1932
1933 return srcJarList
1934}
1935
1936func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
1937 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1938}
Paul Duffin91547182019-11-12 19:39:36 +00001939
1940var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
1941
1942type PrebuiltStubsSourcesProperties struct {
1943 Srcs []string `android:"path"`
1944}
1945
1946type PrebuiltStubsSources struct {
1947 android.ModuleBase
1948 android.DefaultableModuleBase
1949 prebuilt android.Prebuilt
1950 android.SdkBase
1951
1952 properties PrebuiltStubsSourcesProperties
1953
Paul Duffin9b478b02019-12-10 13:41:51 +00001954 // The source directories containing stubs source files.
1955 srcDirs android.Paths
Paul Duffin91547182019-11-12 19:39:36 +00001956 stubsSrcJar android.ModuleOutPath
1957}
1958
Paul Duffin9b478b02019-12-10 13:41:51 +00001959func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) {
1960 switch tag {
1961 case "":
1962 return android.Paths{p.stubsSrcJar}, nil
1963 default:
1964 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1965 }
1966}
1967
Paul Duffin91547182019-11-12 19:39:36 +00001968func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin9b478b02019-12-10 13:41:51 +00001969 p.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1970
1971 p.srcDirs = android.PathsForModuleSrc(ctx, p.properties.Srcs)
1972
1973 rule := android.NewRuleBuilder()
1974 command := rule.Command().
1975 BuiltTool(ctx, "soong_zip").
1976 Flag("-write_if_changed").
1977 Flag("-jar").
1978 FlagWithOutput("-o ", p.stubsSrcJar)
1979
1980 for _, d := range p.srcDirs {
1981 dir := d.String()
1982 command.
1983 FlagWithArg("-C ", dir).
1984 FlagWithInput("-D ", d)
1985 }
1986
1987 rule.Restat()
1988
1989 rule.Build(pctx, ctx, "zip src", "Create srcjar from prebuilt source")
Paul Duffin91547182019-11-12 19:39:36 +00001990}
1991
1992func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
1993 return &p.prebuilt
1994}
1995
1996func (p *PrebuiltStubsSources) Name() string {
1997 return p.prebuilt.Name(p.ModuleBase.Name())
1998}
1999
Paul Duffin91547182019-11-12 19:39:36 +00002000// prebuilt_stubs_sources imports a set of java source files as if they were
2001// generated by droidstubs.
2002//
2003// By default, a prebuilt_stubs_sources has a single variant that expects a
2004// set of `.java` files generated by droidstubs.
2005//
2006// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2007// for host modules.
2008//
2009// Intended only for use by sdk snapshots.
2010func PrebuiltStubsSourcesFactory() android.Module {
2011 module := &PrebuiltStubsSources{}
2012
2013 module.AddProperties(&module.properties)
2014
2015 android.InitPrebuiltModule(module, &module.properties.Srcs)
2016 android.InitSdkAwareModule(module)
2017 InitDroiddocModule(module, android.HostAndDeviceSupported)
2018 return module
2019}
2020
Paul Duffin13879572019-11-28 14:31:38 +00002021type droidStubsSdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00002022 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00002023}
2024
2025func (mt *droidStubsSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2026 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2027}
2028
2029func (mt *droidStubsSdkMemberType) IsInstance(module android.Module) bool {
2030 _, ok := module.(*Droidstubs)
2031 return ok
2032}
2033
2034func (mt *droidStubsSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
2035 variants := member.Variants()
2036 if len(variants) != 1 {
2037 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
2038 }
2039 variant := variants[0]
2040 d, _ := variant.(*Droidstubs)
Paul Duffin91547182019-11-12 19:39:36 +00002041 stubsSrcJar := d.stubsSrcJar
2042
2043 snapshotRelativeDir := filepath.Join("java", d.Name()+"_stubs_sources")
2044 builder.UnzipToSnapshot(stubsSrcJar, snapshotRelativeDir)
2045
Paul Duffin9d8d6092019-12-05 18:19:29 +00002046 pbm := builder.AddPrebuiltModule(member, "prebuilt_stubs_sources")
Paul Duffinb645ec82019-11-27 17:43:54 +00002047 pbm.AddProperty("srcs", []string{snapshotRelativeDir})
Paul Duffin91547182019-11-12 19:39:36 +00002048}