blob: 66eec2cf95b575a4c04cb6108a25a4ddc0fca6cd [file] [log] [blame]
Nan Zhang581fd212018-01-10 16:06:12 -08001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Nan Zhang581fd212018-01-10 16:06:12 -080018 "fmt"
Nan Zhangb2b33de2018-02-23 11:18:47 -080019 "path/filepath"
Nan Zhang581fd212018-01-10 16:06:12 -080020 "strings"
21
Paul Duffin13879572019-11-28 14:31:38 +000022 "github.com/google/blueprint"
Jeongik Cha6bd33c12019-06-25 16:26:18 +090023 "github.com/google/blueprint/proptools"
Nan Zhang581fd212018-01-10 16:06:12 -080024
Colin Crossab054432019-07-15 16:13:59 -070025 "android/soong/android"
26 "android/soong/java/config"
Ramy Medhat1fb3cd82020-05-05 22:50:09 +000027 "android/soong/remoteexec"
Nan Zhang581fd212018-01-10 16:06:12 -080028)
29
30func init() {
Paul Duffin884363e2019-12-19 10:21:09 +000031 RegisterDocsBuildComponents(android.InitRegistrationContext)
32 RegisterStubsBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000033
34 // Register sdk member type.
35 android.RegisterSdkMemberType(&droidStubsSdkMemberType{
36 SdkMemberTypeBase: android.SdkMemberTypeBase{
37 PropertyName: "stubs_sources",
Paul Duffine6029182019-12-16 17:43:48 +000038 // stubs_sources can be used with sdk to provide the source stubs for APIs provided by
39 // the APEX.
40 SupportsSdk: true,
Paul Duffin255f18e2019-12-13 11:22:16 +000041 },
42 })
Nan Zhang581fd212018-01-10 16:06:12 -080043}
44
Paul Duffin884363e2019-12-19 10:21:09 +000045func RegisterDocsBuildComponents(ctx android.RegistrationContext) {
46 ctx.RegisterModuleType("doc_defaults", DocDefaultsFactory)
47
48 ctx.RegisterModuleType("droiddoc", DroiddocFactory)
49 ctx.RegisterModuleType("droiddoc_host", DroiddocHostFactory)
50 ctx.RegisterModuleType("droiddoc_exported_dir", ExportedDroiddocDirFactory)
51 ctx.RegisterModuleType("javadoc", JavadocFactory)
52 ctx.RegisterModuleType("javadoc_host", JavadocHostFactory)
53}
54
55func RegisterStubsBuildComponents(ctx android.RegistrationContext) {
56 ctx.RegisterModuleType("stubs_defaults", StubsDefaultsFactory)
57
58 ctx.RegisterModuleType("droidstubs", DroidstubsFactory)
59 ctx.RegisterModuleType("droidstubs_host", DroidstubsHostFactory)
60
61 ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory)
62}
63
Colin Crossa1ce2a02018-06-20 15:19:39 -070064var (
65 srcsLibTag = dependencyTag{name: "sources from javalib"}
66)
67
Nan Zhang581fd212018-01-10 16:06:12 -080068type JavadocProperties struct {
69 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
70 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080071 Srcs []string `android:"path,arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080072
73 // list of directories rooted at the Android.bp file that will
74 // be added to the search paths for finding source files when passing package names.
Nan Zhangb2b33de2018-02-23 11:18:47 -080075 Local_sourcepaths []string
Nan Zhang581fd212018-01-10 16:06:12 -080076
77 // list of source files that should not be used to build the Java module.
78 // This is most useful in the arch/multilib variants to remove non-common files
79 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -080080 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080081
Jiyong Parkc6ddccf2019-09-13 20:56:14 +090082 // list of package names that should actually be used. If this property is left unspecified,
83 // all the sources from the srcs property is used.
84 Filter_packages []string
85
Nan Zhangb2b33de2018-02-23 11:18:47 -080086 // list of java libraries that will be in the classpath.
Nan Zhang581fd212018-01-10 16:06:12 -080087 Libs []string `android:"arch_variant"`
88
89 // If set to false, don't allow this module(-docs.zip) to be exported. Defaults to true.
Nan Zhangb2b33de2018-02-23 11:18:47 -080090 Installable *bool
Nan Zhang581fd212018-01-10 16:06:12 -080091
Paul Duffine25c6442019-10-11 13:50:28 +010092 // if not blank, set to the version of the sdk to compile against.
93 // Defaults to compiling against the current platform.
Nan Zhang581fd212018-01-10 16:06:12 -080094 Sdk_version *string `android:"arch_variant"`
Jiyong Park1e440682018-05-23 18:42:04 +090095
Paul Duffine25c6442019-10-11 13:50:28 +010096 // When targeting 1.9 and above, override the modules to use with --system,
97 // otherwise provides defaults libraries to add to the bootclasspath.
98 // Defaults to "none"
99 System_modules *string
100
Jiyong Park1e440682018-05-23 18:42:04 +0900101 Aidl struct {
102 // Top level directories to pass to aidl tool
103 Include_dirs []string
104
105 // Directories rooted at the Android.bp file to pass to aidl tool
106 Local_include_dirs []string
107 }
Nan Zhang357466b2018-04-17 17:38:36 -0700108
109 // If not blank, set the java version passed to javadoc as -source
110 Java_version *string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700111
112 // local files that are used within user customized droiddoc options.
Colin Cross27b922f2019-03-04 22:35:41 -0800113 Arg_files []string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -0700114
115 // user customized droiddoc args.
116 // Available variables for substitution:
117 //
118 // $(location <label>): the path to the arg_files with name <label>
Colin Crosse4a05842019-05-28 10:17:14 -0700119 // $$: a literal $
Nan Zhang1598a9e2018-09-04 17:14:32 -0700120 Args *string
121
122 // names of the output files used in args that will be generated
123 Out []string
Ramy Medhatabe1a1a2020-06-13 17:38:27 -0400124
125 // If set, metalava is sandboxed to only read files explicitly specified on the command
126 // line. Defaults to false.
127 Sandbox *bool
Nan Zhang581fd212018-01-10 16:06:12 -0800128}
129
Nan Zhang61819ce2018-05-04 18:49:16 -0700130type ApiToCheck struct {
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900131 // path to the API txt file that the new API extracted from source code is checked
132 // against. The path can be local to the module or from other module (via :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800133 Api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700134
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900135 // path to the API txt file that the new @removed API extractd from source code is
136 // checked against. The path can be local to the module or from other module (via
137 // :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800138 Removed_api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700139
Adrian Roos14f75a92019-08-12 17:54:09 +0200140 // If not blank, path to the baseline txt file for approved API check violations.
141 Baseline_file *string `android:"path"`
142
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900143 // Arguments to the apicheck tool.
Nan Zhang61819ce2018-05-04 18:49:16 -0700144 Args *string
145}
146
Nan Zhang581fd212018-01-10 16:06:12 -0800147type DroiddocProperties struct {
148 // directory relative to top of the source tree that contains doc templates files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800149 Custom_template *string
Nan Zhang581fd212018-01-10 16:06:12 -0800150
Nan Zhanga40da042018-08-01 12:48:00 -0700151 // directories under current module source which contains html/jd files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800152 Html_dirs []string
Nan Zhang581fd212018-01-10 16:06:12 -0800153
154 // set a value in the Clearsilver hdf namespace.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800155 Hdf []string
Nan Zhang581fd212018-01-10 16:06:12 -0800156
157 // proofread file contains all of the text content of the javadocs concatenated into one file,
158 // suitable for spell-checking and other goodness.
Colin Crossab054432019-07-15 16:13:59 -0700159 Proofread_file *string
Nan Zhang581fd212018-01-10 16:06:12 -0800160
161 // a todo file lists the program elements that are missing documentation.
162 // At some point, this might be improved to show more warnings.
Colin Cross27b922f2019-03-04 22:35:41 -0800163 Todo_file *string `android:"path"`
Nan Zhangb2b33de2018-02-23 11:18:47 -0800164
165 // directory under current module source that provide additional resources (images).
166 Resourcesdir *string
167
168 // resources output directory under out/soong/.intermediates.
169 Resourcesoutdir *string
Nan Zhang581fd212018-01-10 16:06:12 -0800170
Nan Zhange2ba5d42018-07-11 15:16:55 -0700171 // if set to true, collect the values used by the Dev tools and
172 // write them in files packaged with the SDK. Defaults to false.
173 Write_sdk_values *bool
174
175 // index.html under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800176 Static_doc_index_redirect *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700177
178 // source.properties under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800179 Static_doc_properties *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700180
Nan Zhang581fd212018-01-10 16:06:12 -0800181 // a list of files under current module source dir which contains known tags in Java sources.
182 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -0800183 Knowntags []string `android:"path"`
Nan Zhang28c68b92018-03-13 16:17:01 -0700184
Nan Zhang28c68b92018-03-13 16:17:01 -0700185 // the generated public API filename by Doclava.
186 Api_filename *string
187
Nan Zhang28c68b92018-03-13 16:17:01 -0700188 // the generated removed API filename by Doclava.
189 Removed_api_filename *string
190
David Brazdilaac0c3c2018-04-24 16:23:29 +0100191 // the generated removed Dex API filename by Doclava.
192 Removed_dex_api_filename *string
193
Liz Kammer10ed7632020-07-30 15:07:22 -0700194 // if set to false, don't allow droiddoc to generate stubs source files. Defaults to false.
Nan Zhang853f4202018-04-12 16:55:56 -0700195 Create_stubs *bool
Nan Zhang61819ce2018-05-04 18:49:16 -0700196
197 Check_api struct {
198 Last_released ApiToCheck
199
200 Current ApiToCheck
Inseob Kim38449af2019-02-28 14:24:05 +0900201
202 // do not perform API check against Last_released, in the case that both two specified API
203 // files by Last_released are modules which don't exist.
204 Ignore_missing_latest_api *bool `blueprint:"mutated"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700205 }
Nan Zhang79614d12018-04-19 18:03:39 -0700206
Nan Zhang1598a9e2018-09-04 17:14:32 -0700207 // if set to true, generate docs through Dokka instead of Doclava.
208 Dokka_enabled *bool
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000209
210 // Compat config XML. Generates compat change documentation if set.
211 Compat_config *string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -0700212}
213
214type DroidstubsProperties struct {
Nan Zhang199645c2018-09-19 12:40:06 -0700215 // the generated public API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700216 Api_filename *string
217
Nan Zhang199645c2018-09-19 12:40:06 -0700218 // the generated removed API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700219 Removed_api_filename *string
220
Nan Zhang199645c2018-09-19 12:40:06 -0700221 // the generated removed Dex API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700222 Removed_dex_api_filename *string
223
Nan Zhang1598a9e2018-09-04 17:14:32 -0700224 Check_api struct {
225 Last_released ApiToCheck
226
227 Current ApiToCheck
Inseob Kim38449af2019-02-28 14:24:05 +0900228
Paul Duffin8986cc92020-05-10 19:32:20 +0100229 // The java_sdk_library module generates references to modules (i.e. filegroups)
230 // from which information about the latest API version can be obtained. As those
231 // modules may not exist (e.g. because a previous version has not been released) it
232 // sets ignore_missing_latest_api=true on the droidstubs modules it creates so
233 // that droidstubs can ignore those references if the modules do not yet exist.
234 //
235 // If true then this will ignore module references for modules that do not exist
236 // in properties that supply the previous version of the API.
237 //
238 // There are two sets of those:
239 // * Api_file, Removed_api_file in check_api.last_released
240 // * New_since in check_api.api_lint.new_since
241 //
242 // The first two must be set as a pair, so either they should both exist or neither
243 // should exist - in which case when this property is true they are ignored. If one
244 // exists and the other does not then it is an error.
Inseob Kim38449af2019-02-28 14:24:05 +0900245 Ignore_missing_latest_api *bool `blueprint:"mutated"`
Adrian Roos075eedc2019-10-10 12:07:03 +0200246
247 Api_lint struct {
248 Enabled *bool
249
250 // If set, performs api_lint on any new APIs not found in the given signature file
251 New_since *string `android:"path"`
252
253 // If not blank, path to the baseline txt file for approved API lint violations.
254 Baseline_file *string `android:"path"`
255 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700256 }
Nan Zhang79614d12018-04-19 18:03:39 -0700257
258 // user can specify the version of previous released API file in order to do compatibility check.
Colin Cross27b922f2019-03-04 22:35:41 -0800259 Previous_api *string `android:"path"`
Nan Zhang79614d12018-04-19 18:03:39 -0700260
261 // is set to true, Metalava will allow framework SDK to contain annotations.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700262 Annotations_enabled *bool
Nan Zhang79614d12018-04-19 18:03:39 -0700263
Pete Gillin77167902018-09-19 18:16:26 +0100264 // a list of top-level directories containing files to merge qualifier annotations (i.e. those intended to be included in the stubs written) from.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700265 Merge_annotations_dirs []string
Nan Zhang86d2d552018-08-09 15:33:27 -0700266
Pete Gillin77167902018-09-19 18:16:26 +0100267 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
268 Merge_inclusion_annotations_dirs []string
269
Pete Gillinc382a562018-11-14 18:45:46 +0000270 // a file containing a list of classes to do nullability validation for.
271 Validate_nullability_from_list *string
272
Pete Gillin581d6082018-10-22 15:55:04 +0100273 // a file containing expected warnings produced by validation of nullability annotations.
274 Check_nullability_warnings *string
275
Nan Zhang1598a9e2018-09-04 17:14:32 -0700276 // if set to true, allow Metalava to generate doc_stubs source files. Defaults to false.
277 Create_doc_stubs *bool
Nan Zhang9c69a122018-08-22 10:22:08 -0700278
Paul Duffin455b0bf2020-04-08 18:18:03 +0100279 // if set to false then do not write out stubs. Defaults to true.
280 //
281 // TODO(b/146727827): Remove capability when we do not need to generate stubs and API separately.
282 Generate_stubs *bool
283
Nan Zhang9c69a122018-08-22 10:22:08 -0700284 // is set to true, Metalava will allow framework SDK to contain API levels annotations.
285 Api_levels_annotations_enabled *bool
286
287 // the dirs which Metalava extracts API levels annotations from.
288 Api_levels_annotations_dirs []string
289
Liz Kammer9ba460f2020-08-04 09:55:13 -0700290 // the filename which Metalava extracts API levels annotations from. Defaults to android.jar.
291 Api_levels_jar_filename *string
292
Nan Zhang9c69a122018-08-22 10:22:08 -0700293 // if set to true, collect the values used by the Dev tools and
294 // write them in files packaged with the SDK. Defaults to false.
295 Write_sdk_values *bool
Nan Zhang71bbe632018-09-17 14:32:21 -0700296
297 // If set to true, .xml based public API file will be also generated, and
298 // JDiff tool will be invoked to genreate javadoc files. Defaults to false.
299 Jdiff_enabled *bool
Nan Zhang581fd212018-01-10 16:06:12 -0800300}
301
Nan Zhanga40da042018-08-01 12:48:00 -0700302//
303// Common flags passed down to build rule
304//
305type droiddocBuilderFlags struct {
Nan Zhang86d2d552018-08-09 15:33:27 -0700306 bootClasspathArgs string
307 classpathArgs string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700308 sourcepathArgs string
Nan Zhang86d2d552018-08-09 15:33:27 -0700309 dokkaClasspathArgs string
310 aidlFlags string
Colin Cross3047fa22019-04-18 10:56:44 -0700311 aidlDeps android.Paths
Nan Zhanga40da042018-08-01 12:48:00 -0700312
Nan Zhanga40da042018-08-01 12:48:00 -0700313 doclavaStubsFlags string
Nan Zhang86d2d552018-08-09 15:33:27 -0700314 doclavaDocsFlags string
Nan Zhanga40da042018-08-01 12:48:00 -0700315 postDoclavaCmds string
Nan Zhanga40da042018-08-01 12:48:00 -0700316}
317
318func InitDroiddocModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
319 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
320 android.InitDefaultableModule(module)
321}
322
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200323func apiCheckEnabled(ctx android.ModuleContext, apiToCheck ApiToCheck, apiVersionTag string) bool {
324 if ctx.Config().IsEnvTrue("WITHOUT_CHECK_API") {
325 return false
326 } else if String(apiToCheck.Api_file) != "" && String(apiToCheck.Removed_api_file) != "" {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700327 return true
328 } else if String(apiToCheck.Api_file) != "" {
329 panic("for " + apiVersionTag + " removed_api_file has to be non-empty!")
330 } else if String(apiToCheck.Removed_api_file) != "" {
331 panic("for " + apiVersionTag + " api_file has to be non-empty!")
332 }
333
334 return false
335}
336
Inseob Kim38449af2019-02-28 14:24:05 +0900337func ignoreMissingModules(ctx android.BottomUpMutatorContext, apiToCheck *ApiToCheck) {
338 api_file := String(apiToCheck.Api_file)
339 removed_api_file := String(apiToCheck.Removed_api_file)
340
341 api_module := android.SrcIsModule(api_file)
342 removed_api_module := android.SrcIsModule(removed_api_file)
343
344 if api_module == "" || removed_api_module == "" {
345 return
346 }
347
348 if ctx.OtherModuleExists(api_module) || ctx.OtherModuleExists(removed_api_module) {
349 return
350 }
351
352 apiToCheck.Api_file = nil
353 apiToCheck.Removed_api_file = nil
354}
355
Paul Duffinf488ef22020-04-09 00:10:17 +0100356// Used by xsd_config
Nan Zhang1598a9e2018-09-04 17:14:32 -0700357type ApiFilePath interface {
358 ApiFilePath() android.Path
359}
360
Paul Duffin533f9c72020-05-20 16:18:00 +0100361type ApiStubsSrcProvider interface {
362 StubsSrcJar() android.Path
363}
364
Paul Duffinf488ef22020-04-09 00:10:17 +0100365// Provider of information about API stubs, used by java_sdk_library.
366type ApiStubsProvider interface {
367 ApiFilePath
Paul Duffin75dcc802020-04-09 01:08:11 +0100368 RemovedApiFilePath() android.Path
Paul Duffin533f9c72020-05-20 16:18:00 +0100369
370 ApiStubsSrcProvider
Paul Duffinf488ef22020-04-09 00:10:17 +0100371}
372
Nan Zhanga40da042018-08-01 12:48:00 -0700373//
374// Javadoc
375//
Nan Zhang581fd212018-01-10 16:06:12 -0800376type Javadoc struct {
377 android.ModuleBase
378 android.DefaultableModuleBase
379
380 properties JavadocProperties
381
382 srcJars android.Paths
383 srcFiles android.Paths
384 sourcepaths android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700385 argFiles android.Paths
Ramy Medhat8e9b63b2020-04-30 03:08:37 -0400386 implicits android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700387
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
Jiyong Park6a927c42020-01-21 02:03:43 +0900427func (j *Javadoc) sdkVersion() sdkSpec {
428 return sdkSpecFrom(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
Jiyong Park6a927c42020-01-21 02:03:43 +0900435func (j *Javadoc) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700436 return j.sdkVersion()
437}
438
Jiyong Park6a927c42020-01-21 02:03:43 +0900439func (j *Javadoc) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700440 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...)
Anton Hansson26bf49b2020-02-08 20:26:29 +0000534 deps.aidlPreprocess = sdkDep.aidl
535 } else {
536 deps.aidlPreprocess = sdkDep.aidl
Nan Zhang581fd212018-01-10 16:06:12 -0800537 }
538
539 ctx.VisitDirectDeps(func(module android.Module) {
540 otherName := ctx.OtherModuleName(module)
541 tag := ctx.OtherModuleDependencyTag(module)
542
Colin Cross2d24c1b2018-05-23 10:59:18 -0700543 switch tag {
544 case bootClasspathTag:
545 if dep, ok := module.(Dependency); ok {
Nan Zhang581fd212018-01-10 16:06:12 -0800546 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars()...)
Paul Duffin83a2d962019-11-19 19:44:10 +0000547 } else if sm, ok := module.(SystemModulesProvider); ok {
Paul Duffine25c6442019-10-11 13:50:28 +0100548 // A system modules dependency has been added to the bootclasspath
549 // so add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +0000550 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700551 } else {
552 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
553 }
554 case libTag:
555 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800556 case SdkLibraryDependency:
Paul Duffin174b26e2020-05-26 11:42:13 +0100557 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700558 case Dependency:
Sundong Ahnba493602018-11-20 17:36:35 +0900559 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park19a7f252019-07-10 16:59:31 +0900560 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700561 case android.SourceFileProducer:
Nan Zhang581fd212018-01-10 16:06:12 -0800562 checkProducesJars(ctx, dep)
563 deps.classpath = append(deps.classpath, dep.Srcs()...)
Nan Zhang581fd212018-01-10 16:06:12 -0800564 default:
565 ctx.ModuleErrorf("depends on non-java module %q", otherName)
566 }
Colin Cross6cef4812019-10-17 14:23:50 -0700567 case java9LibTag:
568 switch dep := module.(type) {
569 case Dependency:
570 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
571 default:
572 ctx.ModuleErrorf("depends on non-java module %q", otherName)
573 }
Nan Zhang357466b2018-04-17 17:38:36 -0700574 case systemModulesTag:
575 if deps.systemModules != nil {
576 panic("Found two system module dependencies")
577 }
Paul Duffin83a2d962019-11-19 19:44:10 +0000578 sm := module.(SystemModulesProvider)
579 outputDir, outputDeps := sm.OutputDirAndDeps()
580 deps.systemModules = &systemModules{outputDir, outputDeps}
Nan Zhang581fd212018-01-10 16:06:12 -0800581 }
582 })
583 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
584 // may contain filegroup or genrule.
Colin Cross8a497952019-03-05 22:25:09 -0800585 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Ramy Medhat8e9b63b2020-04-30 03:08:37 -0400586 j.implicits = append(j.implicits, srcFiles...)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900587
588 filterByPackage := func(srcs []android.Path, filterPackages []string) []android.Path {
589 if filterPackages == nil {
590 return srcs
591 }
592 filtered := []android.Path{}
593 for _, src := range srcs {
594 if src.Ext() != ".java" {
595 // Don't filter-out non-Java (=generated sources) by package names. This is not ideal,
596 // but otherwise metalava emits stub sources having references to the generated AIDL classes
597 // in filtered-out pacages (e.g. com.android.internal.*).
598 // TODO(b/141149570) We need to fix this by introducing default private constructors or
599 // fixing metalava to not emit constructors having references to unknown classes.
600 filtered = append(filtered, src)
601 continue
602 }
603 packageName := strings.ReplaceAll(filepath.Dir(src.Rel()), "/", ".")
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800604 if android.HasAnyPrefix(packageName, filterPackages) {
605 filtered = append(filtered, src)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900606 }
607 }
608 return filtered
609 }
610 srcFiles = filterByPackage(srcFiles, j.properties.Filter_packages)
611
Ramy Medhat8e9b63b2020-04-30 03:08:37 -0400612 // While metalava needs package html files, it does not need them to be explicit on the command
613 // line. More importantly, the metalava rsp file is also used by the subsequent jdiff action if
614 // jdiff_enabled=true. javadoc complains if it receives html files on the command line. The filter
615 // below excludes html files from the rsp file for both metalava and jdiff. Note that the html
616 // files are still included as implicit inputs for successful remote execution and correct
617 // incremental builds.
618 filterHtml := func(srcs []android.Path) []android.Path {
619 filtered := []android.Path{}
620 for _, src := range srcs {
621 if src.Ext() == ".html" {
622 continue
623 }
624 filtered = append(filtered, src)
625 }
626 return filtered
627 }
628 srcFiles = filterHtml(srcFiles)
629
Nan Zhanga40da042018-08-01 12:48:00 -0700630 flags := j.collectAidlFlags(ctx, deps)
Jiyong Park1e440682018-05-23 18:42:04 +0900631 srcFiles = j.genSources(ctx, srcFiles, flags)
Nan Zhang581fd212018-01-10 16:06:12 -0800632
633 // srcs may depend on some genrule output.
634 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800635 j.srcJars = append(j.srcJars, deps.srcJars...)
636
Nan Zhang581fd212018-01-10 16:06:12 -0800637 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800638 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800639
Nan Zhang9c69a122018-08-22 10:22:08 -0700640 if j.properties.Local_sourcepaths == nil && len(j.srcFiles) > 0 {
Nan Zhang581fd212018-01-10 16:06:12 -0800641 j.properties.Local_sourcepaths = append(j.properties.Local_sourcepaths, ".")
642 }
643 j.sourcepaths = android.PathsForModuleSrc(ctx, j.properties.Local_sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800644
Colin Cross8a497952019-03-05 22:25:09 -0800645 j.argFiles = android.PathsForModuleSrc(ctx, j.properties.Arg_files)
Paul Duffin99e4a502019-02-11 15:38:42 +0000646 argFilesMap := map[string]string{}
647 argFileLabels := []string{}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700648
Paul Duffin99e4a502019-02-11 15:38:42 +0000649 for _, label := range j.properties.Arg_files {
Colin Cross8a497952019-03-05 22:25:09 -0800650 var paths = android.PathsForModuleSrc(ctx, []string{label})
Paul Duffin99e4a502019-02-11 15:38:42 +0000651 if _, exists := argFilesMap[label]; !exists {
652 argFilesMap[label] = strings.Join(paths.Strings(), " ")
653 argFileLabels = append(argFileLabels, label)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700654 } else {
655 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000656 label, argFilesMap[label], paths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700657 }
658 }
659
660 var err error
Colin Cross15638152019-07-11 11:11:35 -0700661 j.args, err = android.Expand(String(j.properties.Args), func(name string) (string, error) {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700662 if strings.HasPrefix(name, "location ") {
663 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
Paul Duffin99e4a502019-02-11 15:38:42 +0000664 if paths, ok := argFilesMap[label]; ok {
Colin Cross15638152019-07-11 11:11:35 -0700665 return paths, nil
Nan Zhang1598a9e2018-09-04 17:14:32 -0700666 } else {
Colin Cross15638152019-07-11 11:11:35 -0700667 return "", fmt.Errorf("unknown location label %q, expecting one of %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000668 label, strings.Join(argFileLabels, ", "))
Nan Zhang1598a9e2018-09-04 17:14:32 -0700669 }
670 } else if name == "genDir" {
Colin Cross15638152019-07-11 11:11:35 -0700671 return android.PathForModuleGen(ctx).String(), nil
Nan Zhang1598a9e2018-09-04 17:14:32 -0700672 }
Colin Cross15638152019-07-11 11:11:35 -0700673 return "", fmt.Errorf("unknown variable '$(%s)'", name)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700674 })
675
676 if err != nil {
677 ctx.PropertyErrorf("args", "%s", err.Error())
678 }
679
Nan Zhang581fd212018-01-10 16:06:12 -0800680 return deps
681}
682
683func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
684 j.addDeps(ctx)
685}
686
687func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
688 deps := j.collectDeps(ctx)
689
Colin Crossdaa4c672019-07-15 22:53:46 -0700690 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800691
Colin Crossdaa4c672019-07-15 22:53:46 -0700692 outDir := android.PathForModuleOut(ctx, "out")
693 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
694
695 j.stubsSrcJar = nil
696
697 rule := android.NewRuleBuilder()
698
699 rule.Command().Text("rm -rf").Text(outDir.String())
700 rule.Command().Text("mkdir -p").Text(outDir.String())
701
702 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, j.srcJars)
Nan Zhang357466b2018-04-17 17:38:36 -0700703
Colin Cross83bb3162018-06-25 15:48:06 -0700704 javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800705
Colin Crossdaa4c672019-07-15 22:53:46 -0700706 cmd := javadocSystemModulesCmd(ctx, rule, j.srcFiles, outDir, srcJarDir, srcJarList,
707 deps.systemModules, deps.classpath, j.sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800708
Colin Cross1e743852019-10-28 11:37:20 -0700709 cmd.FlagWithArg("-source ", javaVersion.String()).
Colin Crossdaa4c672019-07-15 22:53:46 -0700710 Flag("-J-Xmx1024m").
711 Flag("-XDignore.symbol.file").
712 Flag("-Xdoclint:none")
Nan Zhang581fd212018-01-10 16:06:12 -0800713
Colin Crossdaa4c672019-07-15 22:53:46 -0700714 rule.Command().
715 BuiltTool(ctx, "soong_zip").
716 Flag("-write_if_changed").
717 Flag("-d").
718 FlagWithOutput("-o ", j.docZip).
719 FlagWithArg("-C ", outDir.String()).
720 FlagWithArg("-D ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700721
Colin Crossdaa4c672019-07-15 22:53:46 -0700722 rule.Restat()
723
724 zipSyncCleanupCmd(rule, srcJarDir)
725
726 rule.Build(pctx, ctx, "javadoc", "javadoc")
Nan Zhang581fd212018-01-10 16:06:12 -0800727}
728
Nan Zhanga40da042018-08-01 12:48:00 -0700729//
730// Droiddoc
731//
732type Droiddoc struct {
733 Javadoc
734
735 properties DroiddocProperties
736 apiFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700737 privateApiFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700738 removedApiFile android.WritablePath
739 removedDexApiFile android.WritablePath
Nan Zhanga40da042018-08-01 12:48:00 -0700740
741 checkCurrentApiTimestamp android.WritablePath
742 updateCurrentApiTimestamp android.WritablePath
743 checkLastReleasedApiTimestamp android.WritablePath
744
Nan Zhanga40da042018-08-01 12:48:00 -0700745 apiFilePath android.Path
746}
747
Colin Crossa3002fc2019-07-08 16:48:04 -0700748// droiddoc converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700749func DroiddocFactory() android.Module {
750 module := &Droiddoc{}
751
752 module.AddProperties(&module.properties,
753 &module.Javadoc.properties)
754
755 InitDroiddocModule(module, android.HostAndDeviceSupported)
756 return module
757}
758
Colin Crossa3002fc2019-07-08 16:48:04 -0700759// droiddoc_host converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700760func DroiddocHostFactory() android.Module {
761 module := &Droiddoc{}
762
763 module.AddProperties(&module.properties,
764 &module.Javadoc.properties)
765
766 InitDroiddocModule(module, android.HostSupported)
767 return module
768}
769
770func (d *Droiddoc) ApiFilePath() android.Path {
771 return d.apiFilePath
772}
773
Nan Zhang581fd212018-01-10 16:06:12 -0800774func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
775 d.Javadoc.addDeps(ctx)
776
Inseob Kim38449af2019-02-28 14:24:05 +0900777 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
778 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
779 }
780
Nan Zhang79614d12018-04-19 18:03:39 -0700781 if String(d.properties.Custom_template) != "" {
Dan Willemsencc090972018-02-26 14:33:31 -0800782 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
783 }
Nan Zhang581fd212018-01-10 16:06:12 -0800784}
785
Colin Crossab054432019-07-15 16:13:59 -0700786func (d *Droiddoc) doclavaDocsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, docletPath classpath) {
Automerger Merge Worker82f316b2020-02-28 21:26:56 +0000787 buildNumberFile := ctx.Config().BuildNumberFile(ctx)
Nan Zhang443fa522018-08-20 20:58:28 -0700788 // Droiddoc always gets "-source 1.8" because it doesn't support 1.9 sources. For modules with 1.9
789 // sources, droiddoc will get sources produced by metalava which will have already stripped out the
790 // 1.9 language features.
Colin Crossab054432019-07-15 16:13:59 -0700791 cmd.FlagWithArg("-source ", "1.8").
792 Flag("-J-Xmx1600m").
793 Flag("-J-XX:-OmitStackTraceInFastThrow").
794 Flag("-XDignore.symbol.file").
795 FlagWithArg("-doclet ", "com.google.doclava.Doclava").
796 FlagWithInputList("-docletpath ", docletPath.Paths(), ":").
Automerger Merge Worker82f316b2020-02-28 21:26:56 +0000797 FlagWithArg("-hdf page.build ", ctx.Config().BuildId()+"-$(cat "+buildNumberFile.String()+")").OrderOnly(buildNumberFile).
Elliott Hughes26bce342019-09-12 15:05:13 -0700798 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 -0700799
Nan Zhanga40da042018-08-01 12:48:00 -0700800 if String(d.properties.Custom_template) == "" {
801 // TODO: This is almost always droiddoc-templates-sdk
802 ctx.PropertyErrorf("custom_template", "must specify a template")
803 }
804
805 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
Nan Zhangf4936b02018-08-01 15:00:28 -0700806 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Crossab054432019-07-15 16:13:59 -0700807 cmd.FlagWithArg("-templatedir ", t.dir.String()).Implicits(t.deps)
Nan Zhanga40da042018-08-01 12:48:00 -0700808 } else {
Paul Duffin884363e2019-12-19 10:21:09 +0000809 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_exported_dir", ctx.OtherModuleName(m))
Nan Zhanga40da042018-08-01 12:48:00 -0700810 }
811 })
812
813 if len(d.properties.Html_dirs) > 0 {
Colin Crossab054432019-07-15 16:13:59 -0700814 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
815 cmd.FlagWithArg("-htmldir ", htmlDir.String()).
816 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[0], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700817 }
818
819 if len(d.properties.Html_dirs) > 1 {
Colin Crossab054432019-07-15 16:13:59 -0700820 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
821 cmd.FlagWithArg("-htmldir2 ", htmlDir2.String()).
822 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[1], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700823 }
824
825 if len(d.properties.Html_dirs) > 2 {
826 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
827 }
828
Colin Cross8a497952019-03-05 22:25:09 -0800829 knownTags := android.PathsForModuleSrc(ctx, d.properties.Knowntags)
Colin Crossab054432019-07-15 16:13:59 -0700830 cmd.FlagForEachInput("-knowntags ", knownTags)
Nan Zhanga40da042018-08-01 12:48:00 -0700831
Colin Crossab054432019-07-15 16:13:59 -0700832 cmd.FlagForEachArg("-hdf ", d.properties.Hdf)
Nan Zhanga40da042018-08-01 12:48:00 -0700833
834 if String(d.properties.Proofread_file) != "" {
835 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
Colin Crossab054432019-07-15 16:13:59 -0700836 cmd.FlagWithOutput("-proofread ", proofreadFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700837 }
838
839 if String(d.properties.Todo_file) != "" {
840 // tricky part:
841 // we should not compute full path for todo_file through PathForModuleOut().
842 // the non-standard doclet will get the full path relative to "-o".
Colin Crossab054432019-07-15 16:13:59 -0700843 cmd.FlagWithArg("-todo ", String(d.properties.Todo_file)).
844 ImplicitOutput(android.PathForModuleOut(ctx, String(d.properties.Todo_file)))
Nan Zhanga40da042018-08-01 12:48:00 -0700845 }
846
847 if String(d.properties.Resourcesdir) != "" {
848 // TODO: should we add files under resourcesDir to the implicits? It seems that
849 // resourcesDir is one sub dir of htmlDir
850 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
Colin Crossab054432019-07-15 16:13:59 -0700851 cmd.FlagWithArg("-resourcesdir ", resourcesDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700852 }
853
854 if String(d.properties.Resourcesoutdir) != "" {
855 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
Colin Crossab054432019-07-15 16:13:59 -0700856 cmd.FlagWithArg("-resourcesoutdir ", String(d.properties.Resourcesoutdir))
Nan Zhanga40da042018-08-01 12:48:00 -0700857 }
Nan Zhanga40da042018-08-01 12:48:00 -0700858}
859
Liz Kammer10ed7632020-07-30 15:07:22 -0700860func (d *Droiddoc) createStubs() bool {
861 return BoolDefault(d.properties.Create_stubs, false)
862}
863
Colin Crossab054432019-07-15 16:13:59 -0700864func (d *Droiddoc) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.WritablePath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200865 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
866 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -0700867 String(d.properties.Api_filename) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700868
Nan Zhanga40da042018-08-01 12:48:00 -0700869 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Colin Crossab054432019-07-15 16:13:59 -0700870 cmd.FlagWithOutput("-api ", d.apiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700871 d.apiFilePath = d.apiFile
872 }
873
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200874 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
875 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -0700876 String(d.properties.Removed_api_filename) != "" {
Nan Zhanga40da042018-08-01 12:48:00 -0700877 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
Colin Crossab054432019-07-15 16:13:59 -0700878 cmd.FlagWithOutput("-removedApi ", d.removedApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700879 }
880
Nan Zhanga40da042018-08-01 12:48:00 -0700881 if String(d.properties.Removed_dex_api_filename) != "" {
882 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Crossab054432019-07-15 16:13:59 -0700883 cmd.FlagWithOutput("-removedDexApi ", d.removedDexApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700884 }
885
Liz Kammer10ed7632020-07-30 15:07:22 -0700886 if d.createStubs() {
Colin Crossab054432019-07-15 16:13:59 -0700887 cmd.FlagWithArg("-stubs ", stubsDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700888 }
889
890 if Bool(d.properties.Write_sdk_values) {
Colin Crossab054432019-07-15 16:13:59 -0700891 cmd.FlagWithArg("-sdkvalues ", android.PathForModuleOut(ctx, "out").String())
Nan Zhanga40da042018-08-01 12:48:00 -0700892 }
Nan Zhanga40da042018-08-01 12:48:00 -0700893}
894
Colin Crossab054432019-07-15 16:13:59 -0700895func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700896 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700897 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
898 rule.Command().Text("cp").
899 Input(staticDocIndexRedirect).
900 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700901 }
902
903 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700904 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
905 rule.Command().Text("cp").
906 Input(staticDocProperties).
907 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700908 }
Nan Zhanga40da042018-08-01 12:48:00 -0700909}
910
Colin Crossab054432019-07-15 16:13:59 -0700911func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700912 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700913
914 cmd := rule.Command().
915 BuiltTool(ctx, "soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
916 Flag(config.JavacVmFlags).
917 FlagWithArg("-encoding ", "UTF-8").
Colin Crossab054432019-07-15 16:13:59 -0700918 FlagWithRspFileInputList("@", srcs).
919 FlagWithInput("@", srcJarList)
920
Colin Crossab054432019-07-15 16:13:59 -0700921 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
922 // based stubs generation.
923 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
924 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
925 // the correct package name base path.
926 if len(sourcepaths) > 0 {
927 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
928 } else {
929 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
930 }
931
932 cmd.FlagWithArg("-d ", outDir.String()).
933 Flag("-quiet")
934
935 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700936}
937
Colin Crossdaa4c672019-07-15 22:53:46 -0700938func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
939 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
940 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
941
942 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
943
944 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
945 cmd.Flag(flag).Implicits(deps)
946
947 cmd.FlagWithArg("--patch-module ", "java.base=.")
948
949 if len(classpath) > 0 {
950 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
951 }
952
953 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700954}
955
Colin Crossdaa4c672019-07-15 22:53:46 -0700956func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
957 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
958 sourcepaths android.Paths) *android.RuleBuilderCommand {
959
960 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
961
962 if len(bootclasspath) == 0 && ctx.Device() {
963 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
964 // ensure java does not fall back to the default bootclasspath.
965 cmd.FlagWithArg("-bootclasspath ", `""`)
966 } else if len(bootclasspath) > 0 {
967 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
968 }
969
970 if len(classpath) > 0 {
971 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
972 }
973
974 return cmd
975}
976
Colin Crossab054432019-07-15 16:13:59 -0700977func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
978 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700979
Colin Crossab054432019-07-15 16:13:59 -0700980 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
981 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
982
983 return rule.Command().
984 BuiltTool(ctx, "dokka").
985 Flag(config.JavacVmFlags).
986 Flag(srcJarDir.String()).
987 FlagWithInputList("-classpath ", dokkaClasspath, ":").
988 FlagWithArg("-format ", "dac").
989 FlagWithArg("-dacRoot ", "/reference/kotlin").
990 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700991}
992
993func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
994 deps := d.Javadoc.collectDeps(ctx)
995
Colin Crossdaa4c672019-07-15 22:53:46 -0700996 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
997 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
998
Nan Zhang1598a9e2018-09-04 17:14:32 -0700999 jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
1000 doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
1001 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1002 checkApiClasspath := classpath{jsilver, doclava, android.PathForSource(ctx, java8Home, "lib/tools.jar")}
1003
Colin Crossab054432019-07-15 16:13:59 -07001004 outDir := android.PathForModuleOut(ctx, "out")
1005 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
1006 stubsDir := android.PathForModuleOut(ctx, "stubsDir")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001007
Colin Crossab054432019-07-15 16:13:59 -07001008 rule := android.NewRuleBuilder()
Nan Zhang1598a9e2018-09-04 17:14:32 -07001009
Colin Crossab054432019-07-15 16:13:59 -07001010 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
1011 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -07001012
Colin Crossab054432019-07-15 16:13:59 -07001013 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1014
1015 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -07001016 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -07001017 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001018 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -07001019 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -07001020 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001021 }
1022
Colin Crossab054432019-07-15 16:13:59 -07001023 d.stubsFlags(ctx, cmd, stubsDir)
1024
1025 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1026
Mathew Inwoodabd49ab2019-12-19 14:27:08 +00001027 if d.properties.Compat_config != nil {
1028 compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
1029 cmd.FlagWithInput("-compatconfig ", compatConfig)
1030 }
1031
Colin Crossab054432019-07-15 16:13:59 -07001032 var desc string
1033 if Bool(d.properties.Dokka_enabled) {
1034 desc = "dokka"
1035 } else {
1036 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
1037
1038 for _, o := range d.Javadoc.properties.Out {
1039 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1040 }
1041
1042 d.postDoclavaCmds(ctx, rule)
1043 desc = "doclava"
1044 }
1045
1046 rule.Command().
1047 BuiltTool(ctx, "soong_zip").
1048 Flag("-write_if_changed").
1049 Flag("-d").
1050 FlagWithOutput("-o ", d.docZip).
1051 FlagWithArg("-C ", outDir.String()).
1052 FlagWithArg("-D ", outDir.String())
1053
1054 rule.Command().
1055 BuiltTool(ctx, "soong_zip").
1056 Flag("-write_if_changed").
1057 Flag("-jar").
1058 FlagWithOutput("-o ", d.stubsSrcJar).
1059 FlagWithArg("-C ", stubsDir.String()).
1060 FlagWithArg("-D ", stubsDir.String())
1061
1062 rule.Restat()
1063
1064 zipSyncCleanupCmd(rule, srcJarDir)
1065
1066 rule.Build(pctx, ctx, "javadoc", desc)
1067
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001068 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001069 !ctx.Config().IsPdkBuild() {
Colin Crossab054432019-07-15 16:13:59 -07001070
1071 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1072 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Nan Zhang1598a9e2018-09-04 17:14:32 -07001073
1074 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001075
1076 rule := android.NewRuleBuilder()
1077
1078 rule.Command().Text("( true")
1079
1080 rule.Command().
1081 BuiltTool(ctx, "apicheck").
1082 Flag("-JXmx1024m").
1083 FlagWithInputList("-Jclasspath\\ ", checkApiClasspath.Paths(), ":").
1084 OptionalFlag(d.properties.Check_api.Current.Args).
1085 Input(apiFile).
1086 Input(d.apiFile).
1087 Input(removedApiFile).
1088 Input(d.removedApiFile)
1089
1090 msg := fmt.Sprintf(`\n******************************\n`+
1091 `You have tried to change the API from what has been previously approved.\n\n`+
1092 `To make these errors go away, you have two choices:\n`+
1093 ` 1. You can add '@hide' javadoc comments to the methods, etc. listed in the\n`+
1094 ` errors above.\n\n`+
1095 ` 2. You can update current.txt by executing the following command:\n`+
1096 ` make %s-update-current-api\n\n`+
1097 ` To submit the revised current.txt to the main Android repository,\n`+
1098 ` you will need approval.\n`+
1099 `******************************\n`, ctx.ModuleName())
1100
1101 rule.Command().
1102 Text("touch").Output(d.checkCurrentApiTimestamp).
1103 Text(") || (").
1104 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1105 Text("; exit 38").
1106 Text(")")
1107
1108 rule.Build(pctx, ctx, "doclavaCurrentApiCheck", "check current API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001109
1110 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001111
1112 // update API rule
1113 rule = android.NewRuleBuilder()
1114
1115 rule.Command().Text("( true")
1116
1117 rule.Command().
1118 Text("cp").Flag("-f").
1119 Input(d.apiFile).Flag(apiFile.String())
1120
1121 rule.Command().
1122 Text("cp").Flag("-f").
1123 Input(d.removedApiFile).Flag(removedApiFile.String())
1124
1125 msg = "failed to update public API"
1126
1127 rule.Command().
1128 Text("touch").Output(d.updateCurrentApiTimestamp).
1129 Text(") || (").
1130 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1131 Text("; exit 38").
1132 Text(")")
1133
1134 rule.Build(pctx, ctx, "doclavaCurrentApiUpdate", "update current API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001135 }
1136
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001137 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001138 !ctx.Config().IsPdkBuild() {
Colin Crossab054432019-07-15 16:13:59 -07001139
1140 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1141 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
Nan Zhang1598a9e2018-09-04 17:14:32 -07001142
1143 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001144
1145 rule := android.NewRuleBuilder()
1146
1147 rule.Command().
1148 Text("(").
1149 BuiltTool(ctx, "apicheck").
1150 Flag("-JXmx1024m").
1151 FlagWithInputList("-Jclasspath\\ ", checkApiClasspath.Paths(), ":").
1152 OptionalFlag(d.properties.Check_api.Last_released.Args).
1153 Input(apiFile).
1154 Input(d.apiFile).
1155 Input(removedApiFile).
1156 Input(d.removedApiFile)
1157
1158 msg := `\n******************************\n` +
1159 `You have tried to change the API from what has been previously released in\n` +
1160 `an SDK. Please fix the errors listed above.\n` +
1161 `******************************\n`
1162
1163 rule.Command().
1164 Text("touch").Output(d.checkLastReleasedApiTimestamp).
1165 Text(") || (").
1166 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1167 Text("; exit 38").
1168 Text(")")
1169
1170 rule.Build(pctx, ctx, "doclavaLastApiCheck", "check last API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001171 }
1172}
1173
1174//
1175// Droidstubs
1176//
1177type Droidstubs struct {
1178 Javadoc
Paul Duffin91547182019-11-12 19:39:36 +00001179 android.SdkBase
Nan Zhang1598a9e2018-09-04 17:14:32 -07001180
Pete Gillin581d6082018-10-22 15:55:04 +01001181 properties DroidstubsProperties
1182 apiFile android.WritablePath
1183 apiXmlFile android.WritablePath
1184 lastReleasedApiXmlFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001185 privateApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001186 removedApiFile android.WritablePath
1187 removedDexApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001188 nullabilityWarningsFile android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001189
1190 checkCurrentApiTimestamp android.WritablePath
1191 updateCurrentApiTimestamp android.WritablePath
1192 checkLastReleasedApiTimestamp android.WritablePath
Adrian Roos075eedc2019-10-10 12:07:03 +02001193 apiLintTimestamp android.WritablePath
Adrian Roos3b8f1cd2019-11-01 13:42:39 +01001194 apiLintReport android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001195
Pete Gillin581d6082018-10-22 15:55:04 +01001196 checkNullabilityWarningsTimestamp android.WritablePath
1197
Nan Zhang1598a9e2018-09-04 17:14:32 -07001198 annotationsZip android.WritablePath
Nan Zhang9c69a122018-08-22 10:22:08 -07001199 apiVersionsXml android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001200
1201 apiFilePath android.Path
Nan Zhang71bbe632018-09-17 14:32:21 -07001202
1203 jdiffDocZip android.WritablePath
1204 jdiffStubsSrcJar android.WritablePath
Jerome Gaillard0f599032019-10-10 19:29:11 +01001205
1206 metadataZip android.WritablePath
1207 metadataDir android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001208}
1209
Colin Crossa3002fc2019-07-08 16:48:04 -07001210// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
1211// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to
1212// a droiddoc module to generate documentation.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001213func DroidstubsFactory() android.Module {
1214 module := &Droidstubs{}
1215
1216 module.AddProperties(&module.properties,
1217 &module.Javadoc.properties)
1218
1219 InitDroiddocModule(module, android.HostAndDeviceSupported)
Paul Duffin91547182019-11-12 19:39:36 +00001220 android.InitSdkAwareModule(module)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001221 return module
1222}
1223
Colin Crossa3002fc2019-07-08 16:48:04 -07001224// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API
1225// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be
1226// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs
1227// module when symbols needed by the source files are provided by java_library_host modules.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001228func DroidstubsHostFactory() android.Module {
1229 module := &Droidstubs{}
1230
1231 module.AddProperties(&module.properties,
1232 &module.Javadoc.properties)
1233
1234 InitDroiddocModule(module, android.HostSupported)
1235 return module
1236}
1237
Colin Cross1e28e3c2020-06-02 20:09:13 -07001238func (d *Droidstubs) OutputFiles(tag string) (android.Paths, error) {
1239 switch tag {
1240 case "":
1241 return android.Paths{d.stubsSrcJar}, nil
1242 case ".docs.zip":
1243 return android.Paths{d.docZip}, nil
1244 case ".annotations.zip":
1245 return android.Paths{d.annotationsZip}, nil
1246 case ".api_versions.xml":
1247 return android.Paths{d.apiVersionsXml}, nil
1248 default:
1249 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1250 }
1251}
1252
Nan Zhang1598a9e2018-09-04 17:14:32 -07001253func (d *Droidstubs) ApiFilePath() android.Path {
1254 return d.apiFilePath
1255}
1256
Paul Duffin75dcc802020-04-09 01:08:11 +01001257func (d *Droidstubs) RemovedApiFilePath() android.Path {
1258 return d.removedApiFile
1259}
1260
Paul Duffinf488ef22020-04-09 00:10:17 +01001261func (d *Droidstubs) StubsSrcJar() android.Path {
1262 return d.stubsSrcJar
1263}
1264
Nan Zhang1598a9e2018-09-04 17:14:32 -07001265func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
1266 d.Javadoc.addDeps(ctx)
1267
Paul Duffin8986cc92020-05-10 19:32:20 +01001268 // If requested clear any properties that provide information about the latest version
1269 // of an API and which reference non-existent modules.
Inseob Kim38449af2019-02-28 14:24:05 +09001270 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
1271 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
Paul Duffin8986cc92020-05-10 19:32:20 +01001272
1273 // If the new_since references a module, e.g. :module-latest-api and the module
1274 // does not exist then clear it.
1275 newSinceSrc := d.properties.Check_api.Api_lint.New_since
1276 newSinceSrcModule := android.SrcIsModule(proptools.String(newSinceSrc))
1277 if newSinceSrcModule != "" && !ctx.OtherModuleExists(newSinceSrcModule) {
1278 d.properties.Check_api.Api_lint.New_since = nil
1279 }
Inseob Kim38449af2019-02-28 14:24:05 +09001280 }
1281
Nan Zhang1598a9e2018-09-04 17:14:32 -07001282 if len(d.properties.Merge_annotations_dirs) != 0 {
1283 for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
1284 ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
1285 }
1286 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001287
Pete Gillin77167902018-09-19 18:16:26 +01001288 if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
1289 for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
1290 ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
1291 }
1292 }
1293
Nan Zhang9c69a122018-08-22 10:22:08 -07001294 if len(d.properties.Api_levels_annotations_dirs) != 0 {
1295 for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
1296 ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
1297 }
1298 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001299}
1300
Paul Duffin455b0bf2020-04-08 18:18:03 +01001301func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001302 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1303 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001304 String(d.properties.Api_filename) != "" {
1305 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001306 cmd.FlagWithOutput("--api ", d.apiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001307 d.apiFilePath = d.apiFile
1308 }
1309
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001310 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1311 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001312 String(d.properties.Removed_api_filename) != "" {
1313 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001314 cmd.FlagWithOutput("--removed-api ", d.removedApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001315 }
1316
Nan Zhang1598a9e2018-09-04 17:14:32 -07001317 if String(d.properties.Removed_dex_api_filename) != "" {
1318 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001319 cmd.FlagWithOutput("--removed-dex-api ", d.removedDexApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001320 }
1321
Nan Zhang9c69a122018-08-22 10:22:08 -07001322 if Bool(d.properties.Write_sdk_values) {
Jerome Gaillard0f599032019-10-10 19:29:11 +01001323 d.metadataDir = android.PathForModuleOut(ctx, "metadata")
1324 cmd.FlagWithArg("--sdk-values ", d.metadataDir.String())
Nan Zhang9c69a122018-08-22 10:22:08 -07001325 }
1326
Paul Duffin455b0bf2020-04-08 18:18:03 +01001327 if stubsDir.Valid() {
1328 if Bool(d.properties.Create_doc_stubs) {
1329 cmd.FlagWithArg("--doc-stubs ", stubsDir.String())
1330 } else {
1331 cmd.FlagWithArg("--stubs ", stubsDir.String())
1332 cmd.Flag("--exclude-documentation-from-stubs")
1333 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001334 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001335}
1336
Colin Cross33961b52019-07-11 11:01:22 -07001337func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang1598a9e2018-09-04 17:14:32 -07001338 if Bool(d.properties.Annotations_enabled) {
Colin Cross33961b52019-07-11 11:01:22 -07001339 cmd.Flag("--include-annotations")
1340
Pete Gillinc382a562018-11-14 18:45:46 +00001341 validatingNullability :=
1342 strings.Contains(d.Javadoc.args, "--validate-nullability-from-merged-stubs") ||
1343 String(d.properties.Validate_nullability_from_list) != ""
Paul Duffin13a9dd62019-11-04 10:26:47 +00001344
Pete Gillina262c052018-09-14 14:25:48 +01001345 migratingNullability := String(d.properties.Previous_api) != ""
Pete Gillina262c052018-09-14 14:25:48 +01001346 if migratingNullability {
Colin Cross8a497952019-03-05 22:25:09 -08001347 previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
Colin Cross33961b52019-07-11 11:01:22 -07001348 cmd.FlagWithInput("--migrate-nullness ", previousApi)
Pete Gillina262c052018-09-14 14:25:48 +01001349 }
Colin Cross33961b52019-07-11 11:01:22 -07001350
Pete Gillinc382a562018-11-14 18:45:46 +00001351 if s := String(d.properties.Validate_nullability_from_list); s != "" {
Colin Cross33961b52019-07-11 11:01:22 -07001352 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
Pete Gillinc382a562018-11-14 18:45:46 +00001353 }
Colin Cross33961b52019-07-11 11:01:22 -07001354
Pete Gillina262c052018-09-14 14:25:48 +01001355 if validatingNullability {
Pete Gillin581d6082018-10-22 15:55:04 +01001356 d.nullabilityWarningsFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_nullability_warnings.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001357 cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile)
Pete Gillina262c052018-09-14 14:25:48 +01001358 }
Nan Zhanga40da042018-08-01 12:48:00 -07001359
1360 d.annotationsZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"_annotations.zip")
Colin Cross33961b52019-07-11 11:01:22 -07001361 cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip)
Nan Zhangf4936b02018-08-01 15:00:28 -07001362
Anton Hanssonc5e13272020-05-21 10:11:31 +01001363 if len(d.properties.Merge_annotations_dirs) != 0 {
1364 d.mergeAnnoDirFlags(ctx, cmd)
Nan Zhanga40da042018-08-01 12:48:00 -07001365 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001366
Colin Cross33961b52019-07-11 11:01:22 -07001367 // TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
1368 cmd.FlagWithArg("--hide ", "HiddenTypedefConstant").
1369 FlagWithArg("--hide ", "SuperfluousPrefix").
1370 FlagWithArg("--hide ", "AnnotationExtraction")
1371 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001372}
1373
Colin Cross33961b52019-07-11 11:01:22 -07001374func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
1375 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
1376 if t, ok := m.(*ExportedDroiddocDir); ok {
1377 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
1378 } else {
1379 ctx.PropertyErrorf("merge_annotations_dirs",
1380 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1381 }
1382 })
1383}
1384
1385func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Pete Gillin77167902018-09-19 18:16:26 +01001386 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
1387 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Cross33961b52019-07-11 11:01:22 -07001388 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
Pete Gillin77167902018-09-19 18:16:26 +01001389 } else {
1390 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
1391 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1392 }
1393 })
Nan Zhanga40da042018-08-01 12:48:00 -07001394}
1395
Colin Cross33961b52019-07-11 11:01:22 -07001396func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Liz Kammer9ba460f2020-08-04 09:55:13 -07001397 if !Bool(d.properties.Api_levels_annotations_enabled) {
1398 return
Nan Zhang9c69a122018-08-22 10:22:08 -07001399 }
Liz Kammer9ba460f2020-08-04 09:55:13 -07001400
1401 d.apiVersionsXml = android.PathForModuleOut(ctx, "api-versions.xml")
1402
1403 if len(d.properties.Api_levels_annotations_dirs) == 0 {
1404 ctx.PropertyErrorf("api_levels_annotations_dirs",
1405 "has to be non-empty if api levels annotations was enabled!")
1406 }
1407
1408 cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml)
1409 cmd.FlagWithInput("--apply-api-levels ", d.apiVersionsXml)
1410 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion())
1411 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
1412
1413 filename := proptools.StringDefault(d.properties.Api_levels_jar_filename, "android.jar")
1414
1415 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
1416 if t, ok := m.(*ExportedDroiddocDir); ok {
1417 for _, dep := range t.deps {
1418 if strings.HasSuffix(dep.String(), filename) {
1419 cmd.Implicit(dep)
1420 }
1421 }
1422 cmd.FlagWithArg("--android-jar-pattern ", t.dir.String()+"/%/public/"+filename)
1423 } else {
1424 ctx.PropertyErrorf("api_levels_annotations_dirs",
1425 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
1426 }
1427 })
Nan Zhang9c69a122018-08-22 10:22:08 -07001428}
1429
Colin Cross33961b52019-07-11 11:01:22 -07001430func (d *Droidstubs) apiToXmlFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Paul Duffin86672f62020-06-19 18:39:55 +01001431 if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() && d.apiFile != nil {
Nan Zhang71bbe632018-09-17 14:32:21 -07001432 if d.apiFile.String() == "" {
1433 ctx.ModuleErrorf("API signature file has to be specified in Metalava when jdiff is enabled.")
1434 }
1435
1436 d.apiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001437 cmd.FlagWithOutput("--api-xml ", d.apiXmlFile)
Nan Zhang71bbe632018-09-17 14:32:21 -07001438
1439 if String(d.properties.Check_api.Last_released.Api_file) == "" {
1440 ctx.PropertyErrorf("check_api.last_released.api_file",
1441 "has to be non-empty if jdiff was enabled!")
1442 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001443
Colin Cross33961b52019-07-11 11:01:22 -07001444 lastReleasedApi := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
Nan Zhang71bbe632018-09-17 14:32:21 -07001445 d.lastReleasedApiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_last_released_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001446 cmd.FlagWithInput("--convert-to-jdiff ", lastReleasedApi).Output(d.lastReleasedApiXmlFile)
1447 }
1448}
Nan Zhang71bbe632018-09-17 14:32:21 -07001449
Colin Cross1e743852019-10-28 11:37:20 -07001450func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths,
Ramy Medhatabe1a1a2020-06-13 17:38:27 -04001451 srcJarList android.Path, bootclasspath, classpath classpath, sourcepaths android.Paths, implicitsRsp android.WritablePath, sandbox bool) *android.RuleBuilderCommand {
Colin Cross8b8bec32019-11-15 13:18:43 -08001452 // Metalava uses lots of memory, restrict the number of metalava jobs that can run in parallel.
1453 rule.HighMem()
Ramy Medhat1fb3cd82020-05-05 22:50:09 +00001454 cmd := rule.Command()
1455 if ctx.Config().IsEnvTrue("RBE_METALAVA") {
1456 rule.Remoteable(android.RemoteRuleSupports{RBE: true})
Ramy Medhatabe1a1a2020-06-13 17:38:27 -04001457 pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "metalava")
1458 execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
1459 labels := map[string]string{"type": "compile", "lang": "java", "compiler": "metalava"}
1460 if !sandbox {
1461 execStrategy = remoteexec.LocalExecStrategy
1462 labels["shallow"] = "true"
Ramy Medhat1fb3cd82020-05-05 22:50:09 +00001463 }
1464 inputs := []string{android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "metalava.jar").String()}
Ramy Medhat46bad762020-05-11 16:49:37 -04001465 inputs = append(inputs, sourcepaths.Strings()...)
Ramy Medhat1fb3cd82020-05-05 22:50:09 +00001466 if v := ctx.Config().Getenv("RBE_METALAVA_INPUTS"); v != "" {
1467 inputs = append(inputs, strings.Split(v, ",")...)
1468 }
1469 cmd.Text((&remoteexec.REParams{
Ramy Medhatabe1a1a2020-06-13 17:38:27 -04001470 Labels: labels,
Ramy Medhat1fb3cd82020-05-05 22:50:09 +00001471 ExecStrategy: execStrategy,
1472 Inputs: inputs,
Ramy Medhat7f2006c2020-06-04 01:54:07 -04001473 RSPFile: implicitsRsp.String(),
Ramy Medhat1fb3cd82020-05-05 22:50:09 +00001474 ToolchainInputs: []string{config.JavaCmd(ctx).String()},
1475 Platform: map[string]string{remoteexec.PoolKey: pool},
1476 }).NoVarTemplate(ctx.Config()))
1477 }
1478
1479 cmd.BuiltTool(ctx, "metalava").
Colin Cross33961b52019-07-11 11:01:22 -07001480 Flag(config.JavacVmFlags).
1481 FlagWithArg("-encoding ", "UTF-8").
Colin Cross1e743852019-10-28 11:37:20 -07001482 FlagWithArg("-source ", javaVersion.String()).
Colin Cross33961b52019-07-11 11:01:22 -07001483 FlagWithRspFileInputList("@", srcs).
Ramy Medhatabe1a1a2020-06-13 17:38:27 -04001484 FlagWithInput("@", srcJarList)
1485
1486 if javaHome := ctx.Config().Getenv("ANDROID_JAVA_HOME"); javaHome != "" {
1487 cmd.Implicit(android.PathForSource(ctx, javaHome))
1488 }
1489
1490 if sandbox {
1491 cmd.FlagWithOutput("--strict-input-files ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
1492 } else {
1493 cmd.FlagWithOutput("--strict-input-files:warn ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
1494 }
Ramy Medhat7f2006c2020-06-04 01:54:07 -04001495
Colin Crossf5f663b2020-06-07 16:58:18 -07001496 if implicitsRsp != nil {
Ramy Medhat7f2006c2020-06-04 01:54:07 -04001497 cmd.FlagWithArg("--strict-input-files-exempt ", "@"+implicitsRsp.String())
1498 }
Colin Cross33961b52019-07-11 11:01:22 -07001499
1500 if len(bootclasspath) > 0 {
1501 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
Nan Zhang71bbe632018-09-17 14:32:21 -07001502 }
1503
Colin Cross33961b52019-07-11 11:01:22 -07001504 if len(classpath) > 0 {
1505 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
1506 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001507
Colin Cross33961b52019-07-11 11:01:22 -07001508 if len(sourcepaths) > 0 {
1509 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
1510 } else {
1511 cmd.FlagWithArg("-sourcepath ", `""`)
1512 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001513
Colin Cross33961b52019-07-11 11:01:22 -07001514 cmd.Flag("--no-banner").
1515 Flag("--color").
1516 Flag("--quiet").
Makoto Onukib3ed1aa2020-07-17 15:11:24 -07001517 Flag("--format=v2").
1518 FlagWithArg("--repeat-errors-max ", "10").
1519 FlagWithArg("--hide ", "UnresolvedImport")
Nan Zhang86d2d552018-08-09 15:33:27 -07001520
Colin Cross33961b52019-07-11 11:01:22 -07001521 return cmd
Nan Zhang71bbe632018-09-17 14:32:21 -07001522}
1523
Nan Zhang1598a9e2018-09-04 17:14:32 -07001524func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga40da042018-08-01 12:48:00 -07001525 deps := d.Javadoc.collectDeps(ctx)
1526
1527 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), sdkContext(d))
Nan Zhang581fd212018-01-10 16:06:12 -08001528
Colin Cross33961b52019-07-11 11:01:22 -07001529 // Create rule for metalava
Nan Zhanga40da042018-08-01 12:48:00 -07001530
Colin Cross33961b52019-07-11 11:01:22 -07001531 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang71bbe632018-09-17 14:32:21 -07001532
Colin Cross33961b52019-07-11 11:01:22 -07001533 rule := android.NewRuleBuilder()
Nan Zhanga40da042018-08-01 12:48:00 -07001534
Paul Duffin455b0bf2020-04-08 18:18:03 +01001535 generateStubs := BoolDefault(d.properties.Generate_stubs, true)
1536 var stubsDir android.OptionalPath
1537 if generateStubs {
1538 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1539 stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "stubsDir"))
1540 rule.Command().Text("rm -rf").Text(stubsDir.String())
1541 rule.Command().Text("mkdir -p").Text(stubsDir.String())
1542 }
Nan Zhanga40da042018-08-01 12:48:00 -07001543
Colin Cross33961b52019-07-11 11:01:22 -07001544 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1545
Ramy Medhatabe1a1a2020-06-13 17:38:27 -04001546 implicitsRsp := android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"implicits.rsp")
1547
Colin Cross33961b52019-07-11 11:01:22 -07001548 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
Ramy Medhatabe1a1a2020-06-13 17:38:27 -04001549 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths, implicitsRsp,
1550 Bool(d.Javadoc.properties.Sandbox))
1551 cmd.Implicits(d.Javadoc.implicits)
Colin Cross33961b52019-07-11 11:01:22 -07001552
1553 d.stubsFlags(ctx, cmd, stubsDir)
1554
1555 d.annotationsFlags(ctx, cmd)
1556 d.inclusionAnnotationsFlags(ctx, cmd)
1557 d.apiLevelsAnnotationsFlags(ctx, cmd)
1558 d.apiToXmlFlags(ctx, cmd)
Nan Zhang71bbe632018-09-17 14:32:21 -07001559
Nan Zhang1598a9e2018-09-04 17:14:32 -07001560 if strings.Contains(d.Javadoc.args, "--generate-documentation") {
1561 // Currently Metalava have the ability to invoke Javadoc in a seperate process.
1562 // Pass "-nodocs" to suppress the Javadoc invocation when Metalava receives
1563 // "--generate-documentation" arg. This is not needed when Metalava removes this feature.
1564 d.Javadoc.args = d.Javadoc.args + " -nodocs "
Nan Zhang79614d12018-04-19 18:03:39 -07001565 }
Colin Cross33961b52019-07-11 11:01:22 -07001566
1567 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1568 for _, o := range d.Javadoc.properties.Out {
1569 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1570 }
1571
Makoto Onukib850a9d2020-04-27 17:22:16 -07001572 // Add options for the other optional tasks: API-lint and check-released.
1573 // We generate separate timestamp files for them.
1574
1575 doApiLint := false
1576 doCheckReleased := false
1577
1578 // Add API lint options.
1579
1580 if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) && !ctx.Config().IsPdkBuild() {
1581 doApiLint = true
1582
1583 newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since)
1584 if newSince.Valid() {
1585 cmd.FlagWithInput("--api-lint ", newSince.Path())
1586 } else {
1587 cmd.Flag("--api-lint")
1588 }
1589 d.apiLintReport = android.PathForModuleOut(ctx, "api_lint_report.txt")
1590 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport) // TODO: Change to ":api-lint"
1591
1592 // TODO(b/154317059): Clean up this whitelist by baselining and/or checking in last-released.
1593 if d.Name() != "android.car-system-stubs-docs" &&
1594 d.Name() != "android.car-stubs-docs" &&
1595 d.Name() != "system-api-stubs-docs" &&
1596 d.Name() != "test-api-stubs-docs" {
1597 cmd.Flag("--lints-as-errors")
1598 cmd.Flag("--warnings-as-errors") // Most lints are actually warnings.
1599 }
1600
1601 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
1602 updatedBaselineOutput := android.PathForModuleOut(ctx, "api_lint_baseline.txt")
1603 d.apiLintTimestamp = android.PathForModuleOut(ctx, "api_lint.timestamp")
1604
1605 // Note this string includes a special shell quote $' ... ', which decodes the "\n"s.
1606 // However, because $' ... ' doesn't expand environmental variables, we can't just embed
1607 // $PWD, so we have to terminate $'...', use "$PWD", then start $' ... ' again,
1608 // which is why we have '"$PWD"$' in it.
1609 //
1610 // TODO: metalava also has a slightly different message hardcoded. Should we unify this
1611 // message and metalava's one?
1612 msg := `$'` + // Enclose with $' ... '
1613 `************************************************************\n` +
1614 `Your API changes are triggering API Lint warnings or errors.\n` +
1615 `To make these errors go away, fix the code according to the\n` +
1616 `error and/or warning messages above.\n` +
1617 `\n` +
1618 `If it is not possible to do so, there are workarounds:\n` +
1619 `\n` +
1620 `1. You can suppress the errors with @SuppressLint("<id>")\n`
1621
1622 if baselineFile.Valid() {
1623 cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path())
1624 cmd.FlagWithOutput("--update-baseline:api-lint ", updatedBaselineOutput)
1625
1626 msg += fmt.Sprintf(``+
1627 `2. You can update the baseline by executing the following\n`+
1628 ` command:\n`+
Anton Hansson18a28952020-05-11 15:38:31 +01001629 ` cp \\\n`+
1630 ` "'"$PWD"$'/%s" \\\n`+
1631 ` "'"$PWD"$'/%s"\n`+
Makoto Onukib850a9d2020-04-27 17:22:16 -07001632 ` To submit the revised baseline.txt to the main Android\n`+
1633 ` repository, you will need approval.\n`, updatedBaselineOutput, baselineFile.Path())
1634 } else {
1635 msg += fmt.Sprintf(``+
1636 `2. You can add a baseline file of existing lint failures\n`+
1637 ` to the build rule of %s.\n`, d.Name())
1638 }
1639 // Note the message ends with a ' (single quote), to close the $' ... ' .
1640 msg += `************************************************************\n'`
1641
1642 cmd.FlagWithArg("--error-message:api-lint ", msg)
1643 }
1644
1645 // Add "check released" options. (Detect incompatible API changes from the last public release)
1646
1647 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") &&
1648 !ctx.Config().IsPdkBuild() {
1649 doCheckReleased = true
1650
1651 if len(d.Javadoc.properties.Out) > 0 {
1652 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1653 }
1654
1655 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1656 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
1657 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
1658 updatedBaselineOutput := android.PathForModuleOut(ctx, "last_released_baseline.txt")
1659
1660 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
1661
1662 cmd.FlagWithInput("--check-compatibility:api:released ", apiFile)
1663 cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile)
1664
1665 if baselineFile.Valid() {
1666 cmd.FlagWithInput("--baseline:compatibility:released ", baselineFile.Path())
1667 cmd.FlagWithOutput("--update-baseline:compatibility:released ", updatedBaselineOutput)
1668 }
1669
1670 // Note this string includes quote ($' ... '), which decodes the "\n"s.
1671 msg := `$'\n******************************\n` +
1672 `You have tried to change the API from what has been previously released in\n` +
1673 `an SDK. Please fix the errors listed above.\n` +
1674 `******************************\n'`
1675
1676 cmd.FlagWithArg("--error-message:compatibility:released ", msg)
1677 }
1678
Ramy Medhatabe1a1a2020-06-13 17:38:27 -04001679 impRule := android.NewRuleBuilder()
1680 impCmd := impRule.Command()
1681 // A dummy action that copies the ninja generated rsp file to a new location. This allows us to
1682 // add a large number of inputs to a file without exceeding bash command length limits (which
1683 // would happen if we use the WriteFile rule). The cp is needed because RuleBuilder sets the
1684 // rsp file to be ${output}.rsp.
1685 impCmd.Text("cp").FlagWithRspFileInputList("", cmd.GetImplicits()).Output(implicitsRsp)
1686 impRule.Build(pctx, ctx, "implicitsGen", "implicits generation")
1687 cmd.Implicit(implicitsRsp)
1688
Paul Duffin455b0bf2020-04-08 18:18:03 +01001689 if generateStubs {
1690 rule.Command().
1691 BuiltTool(ctx, "soong_zip").
1692 Flag("-write_if_changed").
1693 Flag("-jar").
1694 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
1695 FlagWithArg("-C ", stubsDir.String()).
1696 FlagWithArg("-D ", stubsDir.String())
1697 }
Jerome Gaillard0f599032019-10-10 19:29:11 +01001698
1699 if Bool(d.properties.Write_sdk_values) {
1700 d.metadataZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-metadata.zip")
1701 rule.Command().
1702 BuiltTool(ctx, "soong_zip").
1703 Flag("-write_if_changed").
1704 Flag("-d").
1705 FlagWithOutput("-o ", d.metadataZip).
1706 FlagWithArg("-C ", d.metadataDir.String()).
1707 FlagWithArg("-D ", d.metadataDir.String())
1708 }
1709
Makoto Onukib850a9d2020-04-27 17:22:16 -07001710 // TODO: We don't really need two separate API files, but this is a reminiscence of how
1711 // we used to run metalava separately for API lint and the "last_released" check. Unify them.
1712 if doApiLint {
1713 rule.Command().Text("touch").Output(d.apiLintTimestamp)
1714 }
1715 if doCheckReleased {
1716 rule.Command().Text("touch").Output(d.checkLastReleasedApiTimestamp)
1717 }
1718
Colin Cross33961b52019-07-11 11:01:22 -07001719 rule.Restat()
1720
1721 zipSyncCleanupCmd(rule, srcJarDir)
1722
Makoto Onukib850a9d2020-04-27 17:22:16 -07001723 rule.Build(pctx, ctx, "metalava", "metalava merged")
Adrian Roos075eedc2019-10-10 12:07:03 +02001724
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001725 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001726 !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001727
1728 if len(d.Javadoc.properties.Out) > 0 {
1729 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1730 }
1731
1732 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1733 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Adrian Roos14f75a92019-08-12 17:54:09 +02001734 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001735
1736 if baselineFile.Valid() {
Makoto Onukib850a9d2020-04-27 17:22:16 -07001737 ctx.PropertyErrorf("baseline_file", "current API check can't have a baseline file. (module %s)", ctx.ModuleName())
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001738 }
Nan Zhang61819ce2018-05-04 18:49:16 -07001739
Nan Zhang2760dfc2018-08-24 17:32:54 +00001740 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Nan Zhang2760dfc2018-08-24 17:32:54 +00001741
Colin Cross33961b52019-07-11 11:01:22 -07001742 rule := android.NewRuleBuilder()
1743
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001744 // Diff command line.
Makoto Onukib850a9d2020-04-27 17:22:16 -07001745 // -F matches the closest "opening" line, such as "package android {"
1746 // and " public class Intent {".
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001747 diff := `diff -u -F '{ *$'`
1748
Colin Cross33961b52019-07-11 11:01:22 -07001749 rule.Command().Text("( true")
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001750 rule.Command().
1751 Text(diff).
1752 Input(apiFile).Input(d.apiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001753
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001754 rule.Command().
1755 Text(diff).
1756 Input(removedApiFile).Input(d.removedApiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001757
1758 msg := fmt.Sprintf(`\n******************************\n`+
1759 `You have tried to change the API from what has been previously approved.\n\n`+
1760 `To make these errors go away, you have two choices:\n`+
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001761 ` 1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)\n`+
1762 ` to the new methods, etc. shown in the above diff.\n\n`+
1763 ` 2. You can update current.txt and/or removed.txt by executing the following command:\n`+
Colin Cross33961b52019-07-11 11:01:22 -07001764 ` make %s-update-current-api\n\n`+
1765 ` To submit the revised current.txt to the main Android repository,\n`+
1766 ` you will need approval.\n`+
1767 `******************************\n`, ctx.ModuleName())
1768
1769 rule.Command().
1770 Text("touch").Output(d.checkCurrentApiTimestamp).
1771 Text(") || (").
1772 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1773 Text("; exit 38").
1774 Text(")")
1775
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001776 rule.Build(pctx, ctx, "metalavaCurrentApiCheck", "check current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001777
1778 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001779
1780 // update API rule
1781 rule = android.NewRuleBuilder()
1782
1783 rule.Command().Text("( true")
1784
1785 rule.Command().
1786 Text("cp").Flag("-f").
1787 Input(d.apiFile).Flag(apiFile.String())
1788
1789 rule.Command().
1790 Text("cp").Flag("-f").
1791 Input(d.removedApiFile).Flag(removedApiFile.String())
1792
1793 msg = "failed to update public API"
1794
1795 rule.Command().
1796 Text("touch").Output(d.updateCurrentApiTimestamp).
1797 Text(") || (").
1798 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1799 Text("; exit 38").
1800 Text(")")
1801
1802 rule.Build(pctx, ctx, "metalavaCurrentApiUpdate", "update current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001803 }
Nan Zhanga40da042018-08-01 12:48:00 -07001804
Pete Gillin581d6082018-10-22 15:55:04 +01001805 if String(d.properties.Check_nullability_warnings) != "" {
1806 if d.nullabilityWarningsFile == nil {
1807 ctx.PropertyErrorf("check_nullability_warnings",
1808 "Cannot specify check_nullability_warnings unless validating nullability")
1809 }
Colin Cross33961b52019-07-11 11:01:22 -07001810
1811 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
1812
Pete Gillin581d6082018-10-22 15:55:04 +01001813 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "check_nullability_warnings.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001814
Pete Gillin581d6082018-10-22 15:55:04 +01001815 msg := fmt.Sprintf(`\n******************************\n`+
1816 `The warnings encountered during nullability annotation validation did\n`+
1817 `not match the checked in file of expected warnings. The diffs are shown\n`+
1818 `above. You have two options:\n`+
1819 ` 1. Resolve the differences by editing the nullability annotations.\n`+
1820 ` 2. Update the file of expected warnings by running:\n`+
1821 ` cp %s %s\n`+
1822 ` and submitting the updated file as part of your change.`,
1823 d.nullabilityWarningsFile, checkNullabilityWarnings)
Colin Cross33961b52019-07-11 11:01:22 -07001824
1825 rule := android.NewRuleBuilder()
1826
1827 rule.Command().
1828 Text("(").
1829 Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile).
1830 Text("&&").
1831 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
1832 Text(") || (").
1833 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1834 Text("; exit 38").
1835 Text(")")
1836
1837 rule.Build(pctx, ctx, "nullabilityWarningsCheck", "nullability warnings check")
Pete Gillin581d6082018-10-22 15:55:04 +01001838 }
1839
Nan Zhang71bbe632018-09-17 14:32:21 -07001840 if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001841 if len(d.Javadoc.properties.Out) > 0 {
1842 ctx.PropertyErrorf("out", "out property may not be combined with jdiff")
1843 }
1844
1845 outDir := android.PathForModuleOut(ctx, "jdiff-out")
1846 srcJarDir := android.PathForModuleOut(ctx, "jdiff-srcjars")
1847 stubsDir := android.PathForModuleOut(ctx, "jdiff-stubsDir")
1848
1849 rule := android.NewRuleBuilder()
Nan Zhang71bbe632018-09-17 14:32:21 -07001850
Nan Zhang86b06202018-09-21 17:09:21 -07001851 // Please sync with android-api-council@ before making any changes for the name of jdiffDocZip below
1852 // since there's cron job downstream that fetch this .zip file periodically.
1853 // See b/116221385 for reference.
Nan Zhang71bbe632018-09-17 14:32:21 -07001854 d.jdiffDocZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-docs.zip")
1855 d.jdiffStubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-stubs.srcjar")
1856
Nan Zhang71bbe632018-09-17 14:32:21 -07001857 jdiff := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jdiff.jar")
Nan Zhang71bbe632018-09-17 14:32:21 -07001858
Colin Cross33961b52019-07-11 11:01:22 -07001859 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
1860 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang71bbe632018-09-17 14:32:21 -07001861
Colin Cross33961b52019-07-11 11:01:22 -07001862 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1863
Colin Crossdaa4c672019-07-15 22:53:46 -07001864 cmd := javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -07001865 deps.bootClasspath, deps.classpath, d.sourcepaths)
1866
1867 cmd.Flag("-J-Xmx1600m").
Colin Cross33961b52019-07-11 11:01:22 -07001868 Flag("-XDignore.symbol.file").
1869 FlagWithArg("-doclet ", "jdiff.JDiff").
1870 FlagWithInput("-docletpath ", jdiff).
Paul Duffin86672f62020-06-19 18:39:55 +01001871 Flag("-quiet")
1872
1873 if d.apiXmlFile != nil {
1874 cmd.FlagWithArg("-newapi ", strings.TrimSuffix(d.apiXmlFile.Base(), d.apiXmlFile.Ext())).
1875 FlagWithArg("-newapidir ", filepath.Dir(d.apiXmlFile.String())).
1876 Implicit(d.apiXmlFile)
1877 }
1878
1879 if d.lastReleasedApiXmlFile != nil {
1880 cmd.FlagWithArg("-oldapi ", strings.TrimSuffix(d.lastReleasedApiXmlFile.Base(), d.lastReleasedApiXmlFile.Ext())).
1881 FlagWithArg("-oldapidir ", filepath.Dir(d.lastReleasedApiXmlFile.String())).
1882 Implicit(d.lastReleasedApiXmlFile)
1883 }
Colin Cross33961b52019-07-11 11:01:22 -07001884
Colin Cross33961b52019-07-11 11:01:22 -07001885 rule.Command().
1886 BuiltTool(ctx, "soong_zip").
1887 Flag("-write_if_changed").
1888 Flag("-d").
1889 FlagWithOutput("-o ", d.jdiffDocZip).
1890 FlagWithArg("-C ", outDir.String()).
1891 FlagWithArg("-D ", outDir.String())
1892
1893 rule.Command().
1894 BuiltTool(ctx, "soong_zip").
1895 Flag("-write_if_changed").
1896 Flag("-jar").
1897 FlagWithOutput("-o ", d.jdiffStubsSrcJar).
1898 FlagWithArg("-C ", stubsDir.String()).
1899 FlagWithArg("-D ", stubsDir.String())
1900
1901 rule.Restat()
1902
1903 zipSyncCleanupCmd(rule, srcJarDir)
1904
1905 rule.Build(pctx, ctx, "jdiff", "jdiff")
Nan Zhang71bbe632018-09-17 14:32:21 -07001906 }
Nan Zhang581fd212018-01-10 16:06:12 -08001907}
Dan Willemsencc090972018-02-26 14:33:31 -08001908
Nan Zhanga40da042018-08-01 12:48:00 -07001909//
Nan Zhangf4936b02018-08-01 15:00:28 -07001910// Exported Droiddoc Directory
Nan Zhanga40da042018-08-01 12:48:00 -07001911//
Dan Willemsencc090972018-02-26 14:33:31 -08001912var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
Nan Zhangf4936b02018-08-01 15:00:28 -07001913var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
Pete Gillin77167902018-09-19 18:16:26 +01001914var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
Nan Zhang9c69a122018-08-22 10:22:08 -07001915var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
Dan Willemsencc090972018-02-26 14:33:31 -08001916
Nan Zhangf4936b02018-08-01 15:00:28 -07001917type ExportedDroiddocDirProperties struct {
1918 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -08001919 Path *string
1920}
1921
Nan Zhangf4936b02018-08-01 15:00:28 -07001922type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -08001923 android.ModuleBase
1924
Nan Zhangf4936b02018-08-01 15:00:28 -07001925 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -08001926
1927 deps android.Paths
1928 dir android.Path
1929}
1930
Colin Crossa3002fc2019-07-08 16:48:04 -07001931// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -07001932func ExportedDroiddocDirFactory() android.Module {
1933 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -08001934 module.AddProperties(&module.properties)
1935 android.InitAndroidModule(module)
1936 return module
1937}
1938
Nan Zhangf4936b02018-08-01 15:00:28 -07001939func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -08001940
Nan Zhangf4936b02018-08-01 15:00:28 -07001941func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -08001942 path := String(d.properties.Path)
1943 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -08001944 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Dan Willemsencc090972018-02-26 14:33:31 -08001945}
Nan Zhangb2b33de2018-02-23 11:18:47 -08001946
1947//
1948// Defaults
1949//
1950type DocDefaults struct {
1951 android.ModuleBase
1952 android.DefaultsModuleBase
1953}
1954
Nan Zhangb2b33de2018-02-23 11:18:47 -08001955func DocDefaultsFactory() android.Module {
1956 module := &DocDefaults{}
1957
1958 module.AddProperties(
1959 &JavadocProperties{},
1960 &DroiddocProperties{},
1961 )
1962
1963 android.InitDefaultsModule(module)
1964
1965 return module
1966}
Nan Zhang1598a9e2018-09-04 17:14:32 -07001967
1968func StubsDefaultsFactory() android.Module {
1969 module := &DocDefaults{}
1970
1971 module.AddProperties(
1972 &JavadocProperties{},
1973 &DroidstubsProperties{},
1974 )
1975
1976 android.InitDefaultsModule(module)
1977
1978 return module
1979}
Colin Cross33961b52019-07-11 11:01:22 -07001980
1981func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
1982 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
1983
1984 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1985 rule.Command().Text("mkdir -p").Text(srcJarDir.String())
1986 srcJarList := srcJarDir.Join(ctx, "list")
1987
1988 rule.Temporary(srcJarList)
1989
1990 rule.Command().BuiltTool(ctx, "zipsync").
1991 FlagWithArg("-d ", srcJarDir.String()).
1992 FlagWithOutput("-l ", srcJarList).
1993 FlagWithArg("-f ", `"*.java"`).
1994 Inputs(srcJars)
1995
1996 return srcJarList
1997}
1998
1999func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
2000 rule.Command().Text("rm -rf").Text(srcJarDir.String())
2001}
Paul Duffin91547182019-11-12 19:39:36 +00002002
2003var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
2004
2005type PrebuiltStubsSourcesProperties struct {
2006 Srcs []string `android:"path"`
2007}
2008
2009type PrebuiltStubsSources struct {
2010 android.ModuleBase
2011 android.DefaultableModuleBase
2012 prebuilt android.Prebuilt
2013 android.SdkBase
2014
2015 properties PrebuiltStubsSourcesProperties
2016
Paul Duffin9b478b02019-12-10 13:41:51 +00002017 // The source directories containing stubs source files.
2018 srcDirs android.Paths
Paul Duffin91547182019-11-12 19:39:36 +00002019 stubsSrcJar android.ModuleOutPath
2020}
2021
Paul Duffin9b478b02019-12-10 13:41:51 +00002022func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) {
2023 switch tag {
2024 case "":
2025 return android.Paths{p.stubsSrcJar}, nil
2026 default:
2027 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
2028 }
2029}
2030
Paul Duffin533f9c72020-05-20 16:18:00 +01002031func (d *PrebuiltStubsSources) StubsSrcJar() android.Path {
2032 return d.stubsSrcJar
2033}
2034
Paul Duffin91547182019-11-12 19:39:36 +00002035func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin9b478b02019-12-10 13:41:51 +00002036 p.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
2037
2038 p.srcDirs = android.PathsForModuleSrc(ctx, p.properties.Srcs)
2039
2040 rule := android.NewRuleBuilder()
2041 command := rule.Command().
2042 BuiltTool(ctx, "soong_zip").
2043 Flag("-write_if_changed").
2044 Flag("-jar").
2045 FlagWithOutput("-o ", p.stubsSrcJar)
2046
2047 for _, d := range p.srcDirs {
2048 dir := d.String()
2049 command.
2050 FlagWithArg("-C ", dir).
2051 FlagWithInput("-D ", d)
2052 }
2053
2054 rule.Restat()
2055
2056 rule.Build(pctx, ctx, "zip src", "Create srcjar from prebuilt source")
Paul Duffin91547182019-11-12 19:39:36 +00002057}
2058
2059func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
2060 return &p.prebuilt
2061}
2062
2063func (p *PrebuiltStubsSources) Name() string {
2064 return p.prebuilt.Name(p.ModuleBase.Name())
2065}
2066
Paul Duffin91547182019-11-12 19:39:36 +00002067// prebuilt_stubs_sources imports a set of java source files as if they were
2068// generated by droidstubs.
2069//
2070// By default, a prebuilt_stubs_sources has a single variant that expects a
2071// set of `.java` files generated by droidstubs.
2072//
2073// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2074// for host modules.
2075//
2076// Intended only for use by sdk snapshots.
2077func PrebuiltStubsSourcesFactory() android.Module {
2078 module := &PrebuiltStubsSources{}
2079
2080 module.AddProperties(&module.properties)
2081
2082 android.InitPrebuiltModule(module, &module.properties.Srcs)
2083 android.InitSdkAwareModule(module)
2084 InitDroiddocModule(module, android.HostAndDeviceSupported)
2085 return module
2086}
2087
Paul Duffin13879572019-11-28 14:31:38 +00002088type droidStubsSdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00002089 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00002090}
2091
2092func (mt *droidStubsSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2093 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2094}
2095
2096func (mt *droidStubsSdkMemberType) IsInstance(module android.Module) bool {
2097 _, ok := module.(*Droidstubs)
2098 return ok
2099}
2100
Paul Duffin93520ed2020-03-20 13:35:40 +00002101func (mt *droidStubsSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2102 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_stubs_sources")
2103}
2104
2105func (mt *droidStubsSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2106 return &droidStubsInfoProperties{}
2107}
2108
2109type droidStubsInfoProperties struct {
2110 android.SdkMemberPropertiesBase
2111
2112 StubsSrcJar android.Path
2113}
2114
2115func (p *droidStubsInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2116 droidstubs := variant.(*Droidstubs)
2117 p.StubsSrcJar = droidstubs.stubsSrcJar
2118}
2119
2120func (p *droidStubsInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
2121 if p.StubsSrcJar != nil {
2122 builder := ctx.SnapshotBuilder()
2123
2124 snapshotRelativeDir := filepath.Join("java", ctx.Name()+"_stubs_sources")
2125
2126 builder.UnzipToSnapshot(p.StubsSrcJar, snapshotRelativeDir)
2127
2128 propertySet.AddProperty("srcs", []string{snapshotRelativeDir})
Paul Duffin13879572019-11-28 14:31:38 +00002129 }
Paul Duffin91547182019-11-12 19:39:36 +00002130}