blob: b564fea0145503f390358b653d07eb5a7dee7dc9 [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
Nan Zhang853f4202018-04-12 16:55:56 -0700194 // if set to false, don't allow droiddoc to generate stubs source files. Defaults to true.
195 Create_stubs *bool
Nan Zhang61819ce2018-05-04 18:49:16 -0700196
197 Check_api struct {
198 Last_released ApiToCheck
199
200 Current ApiToCheck
Inseob Kim38449af2019-02-28 14:24:05 +0900201
202 // do not perform API check against Last_released, in the case that both two specified API
203 // files by Last_released are modules which don't exist.
204 Ignore_missing_latest_api *bool `blueprint:"mutated"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700205 }
Nan Zhang79614d12018-04-19 18:03:39 -0700206
Nan Zhang1598a9e2018-09-04 17:14:32 -0700207 // if set to true, generate docs through Dokka instead of Doclava.
208 Dokka_enabled *bool
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000209
210 // Compat config XML. Generates compat change documentation if set.
211 Compat_config *string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -0700212}
213
214type DroidstubsProperties struct {
Nan Zhang199645c2018-09-19 12:40:06 -0700215 // the generated public API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700216 Api_filename *string
217
Nan Zhang199645c2018-09-19 12:40:06 -0700218 // the generated removed API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700219 Removed_api_filename *string
220
Nan Zhang199645c2018-09-19 12:40:06 -0700221 // the generated removed Dex API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700222 Removed_dex_api_filename *string
223
Nan Zhang1598a9e2018-09-04 17:14:32 -0700224 Check_api struct {
225 Last_released ApiToCheck
226
227 Current ApiToCheck
Inseob Kim38449af2019-02-28 14:24:05 +0900228
Paul 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
Colin Crossab054432019-07-15 16:13:59 -0700860func (d *Droiddoc) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.WritablePath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200861 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
862 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -0700863 String(d.properties.Api_filename) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700864
Nan Zhanga40da042018-08-01 12:48:00 -0700865 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Colin Crossab054432019-07-15 16:13:59 -0700866 cmd.FlagWithOutput("-api ", d.apiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700867 d.apiFilePath = d.apiFile
868 }
869
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200870 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
871 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -0700872 String(d.properties.Removed_api_filename) != "" {
Nan Zhanga40da042018-08-01 12:48:00 -0700873 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
Colin Crossab054432019-07-15 16:13:59 -0700874 cmd.FlagWithOutput("-removedApi ", d.removedApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700875 }
876
Nan Zhanga40da042018-08-01 12:48:00 -0700877 if String(d.properties.Removed_dex_api_filename) != "" {
878 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Crossab054432019-07-15 16:13:59 -0700879 cmd.FlagWithOutput("-removedDexApi ", d.removedDexApiFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700880 }
881
Nan Zhanga40da042018-08-01 12:48:00 -0700882 if BoolDefault(d.properties.Create_stubs, true) {
Colin Crossab054432019-07-15 16:13:59 -0700883 cmd.FlagWithArg("-stubs ", stubsDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700884 }
885
886 if Bool(d.properties.Write_sdk_values) {
Colin Crossab054432019-07-15 16:13:59 -0700887 cmd.FlagWithArg("-sdkvalues ", android.PathForModuleOut(ctx, "out").String())
Nan Zhanga40da042018-08-01 12:48:00 -0700888 }
Nan Zhanga40da042018-08-01 12:48:00 -0700889}
890
Colin Crossab054432019-07-15 16:13:59 -0700891func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700892 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700893 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
894 rule.Command().Text("cp").
895 Input(staticDocIndexRedirect).
896 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700897 }
898
899 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700900 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
901 rule.Command().Text("cp").
902 Input(staticDocProperties).
903 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700904 }
Nan Zhanga40da042018-08-01 12:48:00 -0700905}
906
Colin Crossab054432019-07-15 16:13:59 -0700907func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700908 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700909
910 cmd := rule.Command().
911 BuiltTool(ctx, "soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
912 Flag(config.JavacVmFlags).
913 FlagWithArg("-encoding ", "UTF-8").
Colin Crossab054432019-07-15 16:13:59 -0700914 FlagWithRspFileInputList("@", srcs).
915 FlagWithInput("@", srcJarList)
916
Colin Crossab054432019-07-15 16:13:59 -0700917 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
918 // based stubs generation.
919 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
920 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
921 // the correct package name base path.
922 if len(sourcepaths) > 0 {
923 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
924 } else {
925 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
926 }
927
928 cmd.FlagWithArg("-d ", outDir.String()).
929 Flag("-quiet")
930
931 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700932}
933
Colin Crossdaa4c672019-07-15 22:53:46 -0700934func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
935 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
936 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
937
938 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
939
940 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
941 cmd.Flag(flag).Implicits(deps)
942
943 cmd.FlagWithArg("--patch-module ", "java.base=.")
944
945 if len(classpath) > 0 {
946 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
947 }
948
949 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700950}
951
Colin Crossdaa4c672019-07-15 22:53:46 -0700952func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
953 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
954 sourcepaths android.Paths) *android.RuleBuilderCommand {
955
956 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
957
958 if len(bootclasspath) == 0 && ctx.Device() {
959 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
960 // ensure java does not fall back to the default bootclasspath.
961 cmd.FlagWithArg("-bootclasspath ", `""`)
962 } else if len(bootclasspath) > 0 {
963 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
964 }
965
966 if len(classpath) > 0 {
967 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
968 }
969
970 return cmd
971}
972
Colin Crossab054432019-07-15 16:13:59 -0700973func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
974 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700975
Colin Crossab054432019-07-15 16:13:59 -0700976 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
977 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
978
979 return rule.Command().
980 BuiltTool(ctx, "dokka").
981 Flag(config.JavacVmFlags).
982 Flag(srcJarDir.String()).
983 FlagWithInputList("-classpath ", dokkaClasspath, ":").
984 FlagWithArg("-format ", "dac").
985 FlagWithArg("-dacRoot ", "/reference/kotlin").
986 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700987}
988
989func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
990 deps := d.Javadoc.collectDeps(ctx)
991
Colin Crossdaa4c672019-07-15 22:53:46 -0700992 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
993 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
994
Nan Zhang1598a9e2018-09-04 17:14:32 -0700995 jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
996 doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
997 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
998 checkApiClasspath := classpath{jsilver, doclava, android.PathForSource(ctx, java8Home, "lib/tools.jar")}
999
Colin Crossab054432019-07-15 16:13:59 -07001000 outDir := android.PathForModuleOut(ctx, "out")
1001 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
1002 stubsDir := android.PathForModuleOut(ctx, "stubsDir")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001003
Colin Crossab054432019-07-15 16:13:59 -07001004 rule := android.NewRuleBuilder()
Nan Zhang1598a9e2018-09-04 17:14:32 -07001005
Colin Crossab054432019-07-15 16:13:59 -07001006 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
1007 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -07001008
Colin Crossab054432019-07-15 16:13:59 -07001009 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1010
1011 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -07001012 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -07001013 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001014 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -07001015 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -07001016 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001017 }
1018
Colin Crossab054432019-07-15 16:13:59 -07001019 d.stubsFlags(ctx, cmd, stubsDir)
1020
1021 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1022
Mathew Inwoodabd49ab2019-12-19 14:27:08 +00001023 if d.properties.Compat_config != nil {
1024 compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
1025 cmd.FlagWithInput("-compatconfig ", compatConfig)
1026 }
1027
Colin Crossab054432019-07-15 16:13:59 -07001028 var desc string
1029 if Bool(d.properties.Dokka_enabled) {
1030 desc = "dokka"
1031 } else {
1032 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
1033
1034 for _, o := range d.Javadoc.properties.Out {
1035 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1036 }
1037
1038 d.postDoclavaCmds(ctx, rule)
1039 desc = "doclava"
1040 }
1041
1042 rule.Command().
1043 BuiltTool(ctx, "soong_zip").
1044 Flag("-write_if_changed").
1045 Flag("-d").
1046 FlagWithOutput("-o ", d.docZip).
1047 FlagWithArg("-C ", outDir.String()).
1048 FlagWithArg("-D ", outDir.String())
1049
1050 rule.Command().
1051 BuiltTool(ctx, "soong_zip").
1052 Flag("-write_if_changed").
1053 Flag("-jar").
1054 FlagWithOutput("-o ", d.stubsSrcJar).
1055 FlagWithArg("-C ", stubsDir.String()).
1056 FlagWithArg("-D ", stubsDir.String())
1057
1058 rule.Restat()
1059
1060 zipSyncCleanupCmd(rule, srcJarDir)
1061
1062 rule.Build(pctx, ctx, "javadoc", desc)
1063
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001064 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001065 !ctx.Config().IsPdkBuild() {
Colin Crossab054432019-07-15 16:13:59 -07001066
1067 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1068 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Nan Zhang1598a9e2018-09-04 17:14:32 -07001069
1070 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001071
1072 rule := android.NewRuleBuilder()
1073
1074 rule.Command().Text("( true")
1075
1076 rule.Command().
1077 BuiltTool(ctx, "apicheck").
1078 Flag("-JXmx1024m").
1079 FlagWithInputList("-Jclasspath\\ ", checkApiClasspath.Paths(), ":").
1080 OptionalFlag(d.properties.Check_api.Current.Args).
1081 Input(apiFile).
1082 Input(d.apiFile).
1083 Input(removedApiFile).
1084 Input(d.removedApiFile)
1085
1086 msg := fmt.Sprintf(`\n******************************\n`+
1087 `You have tried to change the API from what has been previously approved.\n\n`+
1088 `To make these errors go away, you have two choices:\n`+
1089 ` 1. You can add '@hide' javadoc comments to the methods, etc. listed in the\n`+
1090 ` errors above.\n\n`+
1091 ` 2. You can update current.txt by executing the following command:\n`+
1092 ` make %s-update-current-api\n\n`+
1093 ` To submit the revised current.txt to the main Android repository,\n`+
1094 ` you will need approval.\n`+
1095 `******************************\n`, ctx.ModuleName())
1096
1097 rule.Command().
1098 Text("touch").Output(d.checkCurrentApiTimestamp).
1099 Text(") || (").
1100 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1101 Text("; exit 38").
1102 Text(")")
1103
1104 rule.Build(pctx, ctx, "doclavaCurrentApiCheck", "check current API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001105
1106 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001107
1108 // update API rule
1109 rule = android.NewRuleBuilder()
1110
1111 rule.Command().Text("( true")
1112
1113 rule.Command().
1114 Text("cp").Flag("-f").
1115 Input(d.apiFile).Flag(apiFile.String())
1116
1117 rule.Command().
1118 Text("cp").Flag("-f").
1119 Input(d.removedApiFile).Flag(removedApiFile.String())
1120
1121 msg = "failed to update public API"
1122
1123 rule.Command().
1124 Text("touch").Output(d.updateCurrentApiTimestamp).
1125 Text(") || (").
1126 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1127 Text("; exit 38").
1128 Text(")")
1129
1130 rule.Build(pctx, ctx, "doclavaCurrentApiUpdate", "update current API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001131 }
1132
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001133 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001134 !ctx.Config().IsPdkBuild() {
Colin Crossab054432019-07-15 16:13:59 -07001135
1136 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1137 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
Nan Zhang1598a9e2018-09-04 17:14:32 -07001138
1139 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
Colin Crossab054432019-07-15 16:13:59 -07001140
1141 rule := android.NewRuleBuilder()
1142
1143 rule.Command().
1144 Text("(").
1145 BuiltTool(ctx, "apicheck").
1146 Flag("-JXmx1024m").
1147 FlagWithInputList("-Jclasspath\\ ", checkApiClasspath.Paths(), ":").
1148 OptionalFlag(d.properties.Check_api.Last_released.Args).
1149 Input(apiFile).
1150 Input(d.apiFile).
1151 Input(removedApiFile).
1152 Input(d.removedApiFile)
1153
1154 msg := `\n******************************\n` +
1155 `You have tried to change the API from what has been previously released in\n` +
1156 `an SDK. Please fix the errors listed above.\n` +
1157 `******************************\n`
1158
1159 rule.Command().
1160 Text("touch").Output(d.checkLastReleasedApiTimestamp).
1161 Text(") || (").
1162 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1163 Text("; exit 38").
1164 Text(")")
1165
1166 rule.Build(pctx, ctx, "doclavaLastApiCheck", "check last API")
Nan Zhang1598a9e2018-09-04 17:14:32 -07001167 }
1168}
1169
1170//
1171// Droidstubs
1172//
1173type Droidstubs struct {
1174 Javadoc
Paul Duffin91547182019-11-12 19:39:36 +00001175 android.SdkBase
Nan Zhang1598a9e2018-09-04 17:14:32 -07001176
Pete Gillin581d6082018-10-22 15:55:04 +01001177 properties DroidstubsProperties
1178 apiFile android.WritablePath
1179 apiXmlFile android.WritablePath
1180 lastReleasedApiXmlFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001181 privateApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001182 removedApiFile android.WritablePath
1183 removedDexApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001184 nullabilityWarningsFile android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001185
1186 checkCurrentApiTimestamp android.WritablePath
1187 updateCurrentApiTimestamp android.WritablePath
1188 checkLastReleasedApiTimestamp android.WritablePath
Adrian Roos075eedc2019-10-10 12:07:03 +02001189 apiLintTimestamp android.WritablePath
Adrian Roos3b8f1cd2019-11-01 13:42:39 +01001190 apiLintReport android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001191
Pete Gillin581d6082018-10-22 15:55:04 +01001192 checkNullabilityWarningsTimestamp android.WritablePath
1193
Nan Zhang1598a9e2018-09-04 17:14:32 -07001194 annotationsZip android.WritablePath
Nan Zhang9c69a122018-08-22 10:22:08 -07001195 apiVersionsXml android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001196
1197 apiFilePath android.Path
Nan Zhang71bbe632018-09-17 14:32:21 -07001198
1199 jdiffDocZip android.WritablePath
1200 jdiffStubsSrcJar android.WritablePath
Jerome Gaillard0f599032019-10-10 19:29:11 +01001201
1202 metadataZip android.WritablePath
1203 metadataDir android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001204}
1205
Colin Crossa3002fc2019-07-08 16:48:04 -07001206// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
1207// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to
1208// a droiddoc module to generate documentation.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001209func DroidstubsFactory() android.Module {
1210 module := &Droidstubs{}
1211
1212 module.AddProperties(&module.properties,
1213 &module.Javadoc.properties)
1214
1215 InitDroiddocModule(module, android.HostAndDeviceSupported)
Paul Duffin91547182019-11-12 19:39:36 +00001216 android.InitSdkAwareModule(module)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001217 return module
1218}
1219
Colin Crossa3002fc2019-07-08 16:48:04 -07001220// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API
1221// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be
1222// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs
1223// module when symbols needed by the source files are provided by java_library_host modules.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001224func DroidstubsHostFactory() android.Module {
1225 module := &Droidstubs{}
1226
1227 module.AddProperties(&module.properties,
1228 &module.Javadoc.properties)
1229
1230 InitDroiddocModule(module, android.HostSupported)
1231 return module
1232}
1233
Colin Cross1e28e3c2020-06-02 20:09:13 -07001234func (d *Droidstubs) OutputFiles(tag string) (android.Paths, error) {
1235 switch tag {
1236 case "":
1237 return android.Paths{d.stubsSrcJar}, nil
1238 case ".docs.zip":
1239 return android.Paths{d.docZip}, nil
1240 case ".annotations.zip":
1241 return android.Paths{d.annotationsZip}, nil
1242 case ".api_versions.xml":
1243 return android.Paths{d.apiVersionsXml}, nil
1244 default:
1245 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1246 }
1247}
1248
Nan Zhang1598a9e2018-09-04 17:14:32 -07001249func (d *Droidstubs) ApiFilePath() android.Path {
1250 return d.apiFilePath
1251}
1252
Paul Duffin75dcc802020-04-09 01:08:11 +01001253func (d *Droidstubs) RemovedApiFilePath() android.Path {
1254 return d.removedApiFile
1255}
1256
Paul Duffinf488ef22020-04-09 00:10:17 +01001257func (d *Droidstubs) StubsSrcJar() android.Path {
1258 return d.stubsSrcJar
1259}
1260
Nan Zhang1598a9e2018-09-04 17:14:32 -07001261func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
1262 d.Javadoc.addDeps(ctx)
1263
Paul Duffin8986cc92020-05-10 19:32:20 +01001264 // If requested clear any properties that provide information about the latest version
1265 // of an API and which reference non-existent modules.
Inseob Kim38449af2019-02-28 14:24:05 +09001266 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
1267 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
Paul Duffin8986cc92020-05-10 19:32:20 +01001268
1269 // If the new_since references a module, e.g. :module-latest-api and the module
1270 // does not exist then clear it.
1271 newSinceSrc := d.properties.Check_api.Api_lint.New_since
1272 newSinceSrcModule := android.SrcIsModule(proptools.String(newSinceSrc))
1273 if newSinceSrcModule != "" && !ctx.OtherModuleExists(newSinceSrcModule) {
1274 d.properties.Check_api.Api_lint.New_since = nil
1275 }
Inseob Kim38449af2019-02-28 14:24:05 +09001276 }
1277
Nan Zhang1598a9e2018-09-04 17:14:32 -07001278 if len(d.properties.Merge_annotations_dirs) != 0 {
1279 for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
1280 ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
1281 }
1282 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001283
Pete Gillin77167902018-09-19 18:16:26 +01001284 if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
1285 for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
1286 ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
1287 }
1288 }
1289
Nan Zhang9c69a122018-08-22 10:22:08 -07001290 if len(d.properties.Api_levels_annotations_dirs) != 0 {
1291 for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
1292 ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
1293 }
1294 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001295}
1296
Paul Duffin455b0bf2020-04-08 18:18:03 +01001297func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001298 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1299 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001300 String(d.properties.Api_filename) != "" {
1301 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001302 cmd.FlagWithOutput("--api ", d.apiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001303 d.apiFilePath = d.apiFile
1304 }
1305
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001306 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1307 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001308 String(d.properties.Removed_api_filename) != "" {
1309 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001310 cmd.FlagWithOutput("--removed-api ", d.removedApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001311 }
1312
Nan Zhang1598a9e2018-09-04 17:14:32 -07001313 if String(d.properties.Removed_dex_api_filename) != "" {
1314 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001315 cmd.FlagWithOutput("--removed-dex-api ", d.removedDexApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001316 }
1317
Nan Zhang9c69a122018-08-22 10:22:08 -07001318 if Bool(d.properties.Write_sdk_values) {
Jerome Gaillard0f599032019-10-10 19:29:11 +01001319 d.metadataDir = android.PathForModuleOut(ctx, "metadata")
1320 cmd.FlagWithArg("--sdk-values ", d.metadataDir.String())
Nan Zhang9c69a122018-08-22 10:22:08 -07001321 }
1322
Paul Duffin455b0bf2020-04-08 18:18:03 +01001323 if stubsDir.Valid() {
1324 if Bool(d.properties.Create_doc_stubs) {
1325 cmd.FlagWithArg("--doc-stubs ", stubsDir.String())
1326 } else {
1327 cmd.FlagWithArg("--stubs ", stubsDir.String())
1328 cmd.Flag("--exclude-documentation-from-stubs")
1329 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001330 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001331}
1332
Colin Cross33961b52019-07-11 11:01:22 -07001333func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang1598a9e2018-09-04 17:14:32 -07001334 if Bool(d.properties.Annotations_enabled) {
Colin Cross33961b52019-07-11 11:01:22 -07001335 cmd.Flag("--include-annotations")
1336
Pete Gillinc382a562018-11-14 18:45:46 +00001337 validatingNullability :=
1338 strings.Contains(d.Javadoc.args, "--validate-nullability-from-merged-stubs") ||
1339 String(d.properties.Validate_nullability_from_list) != ""
Paul Duffin13a9dd62019-11-04 10:26:47 +00001340
Pete Gillina262c052018-09-14 14:25:48 +01001341 migratingNullability := String(d.properties.Previous_api) != ""
Pete Gillina262c052018-09-14 14:25:48 +01001342 if migratingNullability {
Colin Cross8a497952019-03-05 22:25:09 -08001343 previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
Colin Cross33961b52019-07-11 11:01:22 -07001344 cmd.FlagWithInput("--migrate-nullness ", previousApi)
Pete Gillina262c052018-09-14 14:25:48 +01001345 }
Colin Cross33961b52019-07-11 11:01:22 -07001346
Pete Gillinc382a562018-11-14 18:45:46 +00001347 if s := String(d.properties.Validate_nullability_from_list); s != "" {
Colin Cross33961b52019-07-11 11:01:22 -07001348 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
Pete Gillinc382a562018-11-14 18:45:46 +00001349 }
Colin Cross33961b52019-07-11 11:01:22 -07001350
Pete Gillina262c052018-09-14 14:25:48 +01001351 if validatingNullability {
Pete Gillin581d6082018-10-22 15:55:04 +01001352 d.nullabilityWarningsFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_nullability_warnings.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001353 cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile)
Pete Gillina262c052018-09-14 14:25:48 +01001354 }
Nan Zhanga40da042018-08-01 12:48:00 -07001355
1356 d.annotationsZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"_annotations.zip")
Colin Cross33961b52019-07-11 11:01:22 -07001357 cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip)
Nan Zhangf4936b02018-08-01 15:00:28 -07001358
Anton Hanssonc5e13272020-05-21 10:11:31 +01001359 if len(d.properties.Merge_annotations_dirs) != 0 {
1360 d.mergeAnnoDirFlags(ctx, cmd)
Nan Zhanga40da042018-08-01 12:48:00 -07001361 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001362
Colin Cross33961b52019-07-11 11:01:22 -07001363 // TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
1364 cmd.FlagWithArg("--hide ", "HiddenTypedefConstant").
1365 FlagWithArg("--hide ", "SuperfluousPrefix").
1366 FlagWithArg("--hide ", "AnnotationExtraction")
1367 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001368}
1369
Colin Cross33961b52019-07-11 11:01:22 -07001370func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
1371 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
1372 if t, ok := m.(*ExportedDroiddocDir); ok {
1373 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
1374 } else {
1375 ctx.PropertyErrorf("merge_annotations_dirs",
1376 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1377 }
1378 })
1379}
1380
1381func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Pete Gillin77167902018-09-19 18:16:26 +01001382 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
1383 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Cross33961b52019-07-11 11:01:22 -07001384 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
Pete Gillin77167902018-09-19 18:16:26 +01001385 } else {
1386 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
1387 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1388 }
1389 })
Nan Zhanga40da042018-08-01 12:48:00 -07001390}
1391
Colin Cross33961b52019-07-11 11:01:22 -07001392func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Liz Kammer9ba460f2020-08-04 09:55:13 -07001393 if !Bool(d.properties.Api_levels_annotations_enabled) {
1394 return
Nan Zhang9c69a122018-08-22 10:22:08 -07001395 }
Liz Kammer9ba460f2020-08-04 09:55:13 -07001396
1397 d.apiVersionsXml = android.PathForModuleOut(ctx, "api-versions.xml")
1398
1399 if len(d.properties.Api_levels_annotations_dirs) == 0 {
1400 ctx.PropertyErrorf("api_levels_annotations_dirs",
1401 "has to be non-empty if api levels annotations was enabled!")
1402 }
1403
1404 cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml)
1405 cmd.FlagWithInput("--apply-api-levels ", d.apiVersionsXml)
1406 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion())
1407 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
1408
1409 filename := proptools.StringDefault(d.properties.Api_levels_jar_filename, "android.jar")
1410
1411 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
1412 if t, ok := m.(*ExportedDroiddocDir); ok {
1413 for _, dep := range t.deps {
1414 if strings.HasSuffix(dep.String(), filename) {
1415 cmd.Implicit(dep)
1416 }
1417 }
1418 cmd.FlagWithArg("--android-jar-pattern ", t.dir.String()+"/%/public/"+filename)
1419 } else {
1420 ctx.PropertyErrorf("api_levels_annotations_dirs",
1421 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
1422 }
1423 })
Nan Zhang9c69a122018-08-22 10:22:08 -07001424}
1425
Colin Cross33961b52019-07-11 11:01:22 -07001426func (d *Droidstubs) apiToXmlFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Paul Duffin86672f62020-06-19 18:39:55 +01001427 if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() && d.apiFile != nil {
Nan Zhang71bbe632018-09-17 14:32:21 -07001428 if d.apiFile.String() == "" {
1429 ctx.ModuleErrorf("API signature file has to be specified in Metalava when jdiff is enabled.")
1430 }
1431
1432 d.apiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001433 cmd.FlagWithOutput("--api-xml ", d.apiXmlFile)
Nan Zhang71bbe632018-09-17 14:32:21 -07001434
1435 if String(d.properties.Check_api.Last_released.Api_file) == "" {
1436 ctx.PropertyErrorf("check_api.last_released.api_file",
1437 "has to be non-empty if jdiff was enabled!")
1438 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001439
Colin Cross33961b52019-07-11 11:01:22 -07001440 lastReleasedApi := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
Nan Zhang71bbe632018-09-17 14:32:21 -07001441 d.lastReleasedApiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_last_released_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001442 cmd.FlagWithInput("--convert-to-jdiff ", lastReleasedApi).Output(d.lastReleasedApiXmlFile)
1443 }
1444}
Nan Zhang71bbe632018-09-17 14:32:21 -07001445
Colin Cross1e743852019-10-28 11:37:20 -07001446func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths,
Ramy Medhatabe1a1a2020-06-13 17:38:27 -04001447 srcJarList android.Path, bootclasspath, classpath classpath, sourcepaths android.Paths, implicitsRsp android.WritablePath, sandbox bool) *android.RuleBuilderCommand {
Colin Cross8b8bec32019-11-15 13:18:43 -08001448 // Metalava uses lots of memory, restrict the number of metalava jobs that can run in parallel.
1449 rule.HighMem()
Ramy Medhat1fb3cd82020-05-05 22:50:09 +00001450 cmd := rule.Command()
1451 if ctx.Config().IsEnvTrue("RBE_METALAVA") {
1452 rule.Remoteable(android.RemoteRuleSupports{RBE: true})
Ramy Medhatabe1a1a2020-06-13 17:38:27 -04001453 pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "metalava")
1454 execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
1455 labels := map[string]string{"type": "compile", "lang": "java", "compiler": "metalava"}
1456 if !sandbox {
1457 execStrategy = remoteexec.LocalExecStrategy
1458 labels["shallow"] = "true"
Ramy Medhat1fb3cd82020-05-05 22:50:09 +00001459 }
1460 inputs := []string{android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "metalava.jar").String()}
Ramy Medhat46bad762020-05-11 16:49:37 -04001461 inputs = append(inputs, sourcepaths.Strings()...)
Ramy Medhat1fb3cd82020-05-05 22:50:09 +00001462 if v := ctx.Config().Getenv("RBE_METALAVA_INPUTS"); v != "" {
1463 inputs = append(inputs, strings.Split(v, ",")...)
1464 }
1465 cmd.Text((&remoteexec.REParams{
Ramy Medhatabe1a1a2020-06-13 17:38:27 -04001466 Labels: labels,
Ramy Medhat1fb3cd82020-05-05 22:50:09 +00001467 ExecStrategy: execStrategy,
1468 Inputs: inputs,
Ramy Medhat7f2006c2020-06-04 01:54:07 -04001469 RSPFile: implicitsRsp.String(),
Ramy Medhat1fb3cd82020-05-05 22:50:09 +00001470 ToolchainInputs: []string{config.JavaCmd(ctx).String()},
1471 Platform: map[string]string{remoteexec.PoolKey: pool},
1472 }).NoVarTemplate(ctx.Config()))
1473 }
1474
1475 cmd.BuiltTool(ctx, "metalava").
Colin Cross33961b52019-07-11 11:01:22 -07001476 Flag(config.JavacVmFlags).
1477 FlagWithArg("-encoding ", "UTF-8").
Colin Cross1e743852019-10-28 11:37:20 -07001478 FlagWithArg("-source ", javaVersion.String()).
Colin Cross33961b52019-07-11 11:01:22 -07001479 FlagWithRspFileInputList("@", srcs).
Ramy Medhatabe1a1a2020-06-13 17:38:27 -04001480 FlagWithInput("@", srcJarList)
1481
1482 if javaHome := ctx.Config().Getenv("ANDROID_JAVA_HOME"); javaHome != "" {
1483 cmd.Implicit(android.PathForSource(ctx, javaHome))
1484 }
1485
1486 if sandbox {
1487 cmd.FlagWithOutput("--strict-input-files ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
1488 } else {
1489 cmd.FlagWithOutput("--strict-input-files:warn ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
1490 }
Ramy Medhat7f2006c2020-06-04 01:54:07 -04001491
Colin Crossf5f663b2020-06-07 16:58:18 -07001492 if implicitsRsp != nil {
Ramy Medhat7f2006c2020-06-04 01:54:07 -04001493 cmd.FlagWithArg("--strict-input-files-exempt ", "@"+implicitsRsp.String())
1494 }
Colin Cross33961b52019-07-11 11:01:22 -07001495
1496 if len(bootclasspath) > 0 {
1497 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
Nan Zhang71bbe632018-09-17 14:32:21 -07001498 }
1499
Colin Cross33961b52019-07-11 11:01:22 -07001500 if len(classpath) > 0 {
1501 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
1502 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001503
Colin Cross33961b52019-07-11 11:01:22 -07001504 if len(sourcepaths) > 0 {
1505 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
1506 } else {
1507 cmd.FlagWithArg("-sourcepath ", `""`)
1508 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001509
Colin Cross33961b52019-07-11 11:01:22 -07001510 cmd.Flag("--no-banner").
1511 Flag("--color").
1512 Flag("--quiet").
1513 Flag("--format=v2")
Nan Zhang86d2d552018-08-09 15:33:27 -07001514
Colin Cross33961b52019-07-11 11:01:22 -07001515 return cmd
Nan Zhang71bbe632018-09-17 14:32:21 -07001516}
1517
Nan Zhang1598a9e2018-09-04 17:14:32 -07001518func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga40da042018-08-01 12:48:00 -07001519 deps := d.Javadoc.collectDeps(ctx)
1520
1521 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), sdkContext(d))
Nan Zhang581fd212018-01-10 16:06:12 -08001522
Colin Cross33961b52019-07-11 11:01:22 -07001523 // Create rule for metalava
Nan Zhanga40da042018-08-01 12:48:00 -07001524
Colin Cross33961b52019-07-11 11:01:22 -07001525 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang71bbe632018-09-17 14:32:21 -07001526
Colin Cross33961b52019-07-11 11:01:22 -07001527 rule := android.NewRuleBuilder()
Nan Zhanga40da042018-08-01 12:48:00 -07001528
Paul Duffin455b0bf2020-04-08 18:18:03 +01001529 generateStubs := BoolDefault(d.properties.Generate_stubs, true)
1530 var stubsDir android.OptionalPath
1531 if generateStubs {
1532 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1533 stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "stubsDir"))
1534 rule.Command().Text("rm -rf").Text(stubsDir.String())
1535 rule.Command().Text("mkdir -p").Text(stubsDir.String())
1536 }
Nan Zhanga40da042018-08-01 12:48:00 -07001537
Colin Cross33961b52019-07-11 11:01:22 -07001538 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1539
Ramy Medhatabe1a1a2020-06-13 17:38:27 -04001540 implicitsRsp := android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"implicits.rsp")
1541
Colin Cross33961b52019-07-11 11:01:22 -07001542 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
Ramy Medhatabe1a1a2020-06-13 17:38:27 -04001543 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths, implicitsRsp,
1544 Bool(d.Javadoc.properties.Sandbox))
1545 cmd.Implicits(d.Javadoc.implicits)
Colin Cross33961b52019-07-11 11:01:22 -07001546
1547 d.stubsFlags(ctx, cmd, stubsDir)
1548
1549 d.annotationsFlags(ctx, cmd)
1550 d.inclusionAnnotationsFlags(ctx, cmd)
1551 d.apiLevelsAnnotationsFlags(ctx, cmd)
1552 d.apiToXmlFlags(ctx, cmd)
Nan Zhang71bbe632018-09-17 14:32:21 -07001553
Nan Zhang1598a9e2018-09-04 17:14:32 -07001554 if strings.Contains(d.Javadoc.args, "--generate-documentation") {
1555 // Currently Metalava have the ability to invoke Javadoc in a seperate process.
1556 // Pass "-nodocs" to suppress the Javadoc invocation when Metalava receives
1557 // "--generate-documentation" arg. This is not needed when Metalava removes this feature.
1558 d.Javadoc.args = d.Javadoc.args + " -nodocs "
Nan Zhang79614d12018-04-19 18:03:39 -07001559 }
Colin Cross33961b52019-07-11 11:01:22 -07001560
1561 cmd.Flag(d.Javadoc.args).Implicits(d.Javadoc.argFiles)
1562 for _, o := range d.Javadoc.properties.Out {
1563 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1564 }
1565
Makoto Onukib850a9d2020-04-27 17:22:16 -07001566 // Add options for the other optional tasks: API-lint and check-released.
1567 // We generate separate timestamp files for them.
1568
1569 doApiLint := false
1570 doCheckReleased := false
1571
1572 // Add API lint options.
1573
1574 if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) && !ctx.Config().IsPdkBuild() {
1575 doApiLint = true
1576
1577 newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since)
1578 if newSince.Valid() {
1579 cmd.FlagWithInput("--api-lint ", newSince.Path())
1580 } else {
1581 cmd.Flag("--api-lint")
1582 }
1583 d.apiLintReport = android.PathForModuleOut(ctx, "api_lint_report.txt")
1584 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport) // TODO: Change to ":api-lint"
1585
1586 // TODO(b/154317059): Clean up this whitelist by baselining and/or checking in last-released.
1587 if d.Name() != "android.car-system-stubs-docs" &&
1588 d.Name() != "android.car-stubs-docs" &&
1589 d.Name() != "system-api-stubs-docs" &&
1590 d.Name() != "test-api-stubs-docs" {
1591 cmd.Flag("--lints-as-errors")
1592 cmd.Flag("--warnings-as-errors") // Most lints are actually warnings.
1593 }
1594
1595 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
1596 updatedBaselineOutput := android.PathForModuleOut(ctx, "api_lint_baseline.txt")
1597 d.apiLintTimestamp = android.PathForModuleOut(ctx, "api_lint.timestamp")
1598
1599 // Note this string includes a special shell quote $' ... ', which decodes the "\n"s.
1600 // However, because $' ... ' doesn't expand environmental variables, we can't just embed
1601 // $PWD, so we have to terminate $'...', use "$PWD", then start $' ... ' again,
1602 // which is why we have '"$PWD"$' in it.
1603 //
1604 // TODO: metalava also has a slightly different message hardcoded. Should we unify this
1605 // message and metalava's one?
1606 msg := `$'` + // Enclose with $' ... '
1607 `************************************************************\n` +
1608 `Your API changes are triggering API Lint warnings or errors.\n` +
1609 `To make these errors go away, fix the code according to the\n` +
1610 `error and/or warning messages above.\n` +
1611 `\n` +
1612 `If it is not possible to do so, there are workarounds:\n` +
1613 `\n` +
1614 `1. You can suppress the errors with @SuppressLint("<id>")\n`
1615
1616 if baselineFile.Valid() {
1617 cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path())
1618 cmd.FlagWithOutput("--update-baseline:api-lint ", updatedBaselineOutput)
1619
1620 msg += fmt.Sprintf(``+
1621 `2. You can update the baseline by executing the following\n`+
1622 ` command:\n`+
Anton Hansson18a28952020-05-11 15:38:31 +01001623 ` cp \\\n`+
1624 ` "'"$PWD"$'/%s" \\\n`+
1625 ` "'"$PWD"$'/%s"\n`+
Makoto Onukib850a9d2020-04-27 17:22:16 -07001626 ` To submit the revised baseline.txt to the main Android\n`+
1627 ` repository, you will need approval.\n`, updatedBaselineOutput, baselineFile.Path())
1628 } else {
1629 msg += fmt.Sprintf(``+
1630 `2. You can add a baseline file of existing lint failures\n`+
1631 ` to the build rule of %s.\n`, d.Name())
1632 }
1633 // Note the message ends with a ' (single quote), to close the $' ... ' .
1634 msg += `************************************************************\n'`
1635
1636 cmd.FlagWithArg("--error-message:api-lint ", msg)
1637 }
1638
1639 // Add "check released" options. (Detect incompatible API changes from the last public release)
1640
1641 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") &&
1642 !ctx.Config().IsPdkBuild() {
1643 doCheckReleased = true
1644
1645 if len(d.Javadoc.properties.Out) > 0 {
1646 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1647 }
1648
1649 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1650 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
1651 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
1652 updatedBaselineOutput := android.PathForModuleOut(ctx, "last_released_baseline.txt")
1653
1654 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
1655
1656 cmd.FlagWithInput("--check-compatibility:api:released ", apiFile)
1657 cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile)
1658
1659 if baselineFile.Valid() {
1660 cmd.FlagWithInput("--baseline:compatibility:released ", baselineFile.Path())
1661 cmd.FlagWithOutput("--update-baseline:compatibility:released ", updatedBaselineOutput)
1662 }
1663
1664 // Note this string includes quote ($' ... '), which decodes the "\n"s.
1665 msg := `$'\n******************************\n` +
1666 `You have tried to change the API from what has been previously released in\n` +
1667 `an SDK. Please fix the errors listed above.\n` +
1668 `******************************\n'`
1669
1670 cmd.FlagWithArg("--error-message:compatibility:released ", msg)
1671 }
1672
Ramy Medhatabe1a1a2020-06-13 17:38:27 -04001673 impRule := android.NewRuleBuilder()
1674 impCmd := impRule.Command()
1675 // A dummy action that copies the ninja generated rsp file to a new location. This allows us to
1676 // add a large number of inputs to a file without exceeding bash command length limits (which
1677 // would happen if we use the WriteFile rule). The cp is needed because RuleBuilder sets the
1678 // rsp file to be ${output}.rsp.
1679 impCmd.Text("cp").FlagWithRspFileInputList("", cmd.GetImplicits()).Output(implicitsRsp)
1680 impRule.Build(pctx, ctx, "implicitsGen", "implicits generation")
1681 cmd.Implicit(implicitsRsp)
1682
Paul Duffin455b0bf2020-04-08 18:18:03 +01001683 if generateStubs {
1684 rule.Command().
1685 BuiltTool(ctx, "soong_zip").
1686 Flag("-write_if_changed").
1687 Flag("-jar").
1688 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
1689 FlagWithArg("-C ", stubsDir.String()).
1690 FlagWithArg("-D ", stubsDir.String())
1691 }
Jerome Gaillard0f599032019-10-10 19:29:11 +01001692
1693 if Bool(d.properties.Write_sdk_values) {
1694 d.metadataZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-metadata.zip")
1695 rule.Command().
1696 BuiltTool(ctx, "soong_zip").
1697 Flag("-write_if_changed").
1698 Flag("-d").
1699 FlagWithOutput("-o ", d.metadataZip).
1700 FlagWithArg("-C ", d.metadataDir.String()).
1701 FlagWithArg("-D ", d.metadataDir.String())
1702 }
1703
Makoto Onukib850a9d2020-04-27 17:22:16 -07001704 // TODO: We don't really need two separate API files, but this is a reminiscence of how
1705 // we used to run metalava separately for API lint and the "last_released" check. Unify them.
1706 if doApiLint {
1707 rule.Command().Text("touch").Output(d.apiLintTimestamp)
1708 }
1709 if doCheckReleased {
1710 rule.Command().Text("touch").Output(d.checkLastReleasedApiTimestamp)
1711 }
1712
Colin Cross33961b52019-07-11 11:01:22 -07001713 rule.Restat()
1714
1715 zipSyncCleanupCmd(rule, srcJarDir)
1716
Makoto Onukib850a9d2020-04-27 17:22:16 -07001717 rule.Build(pctx, ctx, "metalava", "metalava merged")
Adrian Roos075eedc2019-10-10 12:07:03 +02001718
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001719 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") &&
Nan Zhang1598a9e2018-09-04 17:14:32 -07001720 !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001721
1722 if len(d.Javadoc.properties.Out) > 0 {
1723 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1724 }
1725
1726 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1727 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Adrian Roos14f75a92019-08-12 17:54:09 +02001728 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001729
1730 if baselineFile.Valid() {
Makoto Onukib850a9d2020-04-27 17:22:16 -07001731 ctx.PropertyErrorf("baseline_file", "current API check can't have a baseline file. (module %s)", ctx.ModuleName())
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001732 }
Nan Zhang61819ce2018-05-04 18:49:16 -07001733
Nan Zhang2760dfc2018-08-24 17:32:54 +00001734 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Nan Zhang2760dfc2018-08-24 17:32:54 +00001735
Colin Cross33961b52019-07-11 11:01:22 -07001736 rule := android.NewRuleBuilder()
1737
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001738 // Diff command line.
Makoto Onukib850a9d2020-04-27 17:22:16 -07001739 // -F matches the closest "opening" line, such as "package android {"
1740 // and " public class Intent {".
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001741 diff := `diff -u -F '{ *$'`
1742
Colin Cross33961b52019-07-11 11:01:22 -07001743 rule.Command().Text("( true")
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001744 rule.Command().
1745 Text(diff).
1746 Input(apiFile).Input(d.apiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001747
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001748 rule.Command().
1749 Text(diff).
1750 Input(removedApiFile).Input(d.removedApiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001751
1752 msg := fmt.Sprintf(`\n******************************\n`+
1753 `You have tried to change the API from what has been previously approved.\n\n`+
1754 `To make these errors go away, you have two choices:\n`+
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001755 ` 1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)\n`+
1756 ` to the new methods, etc. shown in the above diff.\n\n`+
1757 ` 2. You can update current.txt and/or removed.txt by executing the following command:\n`+
Colin Cross33961b52019-07-11 11:01:22 -07001758 ` make %s-update-current-api\n\n`+
1759 ` To submit the revised current.txt to the main Android repository,\n`+
1760 ` you will need approval.\n`+
1761 `******************************\n`, ctx.ModuleName())
1762
1763 rule.Command().
1764 Text("touch").Output(d.checkCurrentApiTimestamp).
1765 Text(") || (").
1766 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1767 Text("; exit 38").
1768 Text(")")
1769
Makoto Onukib52c8ea2020-04-16 17:02:40 -07001770 rule.Build(pctx, ctx, "metalavaCurrentApiCheck", "check current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001771
1772 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001773
1774 // update API rule
1775 rule = android.NewRuleBuilder()
1776
1777 rule.Command().Text("( true")
1778
1779 rule.Command().
1780 Text("cp").Flag("-f").
1781 Input(d.apiFile).Flag(apiFile.String())
1782
1783 rule.Command().
1784 Text("cp").Flag("-f").
1785 Input(d.removedApiFile).Flag(removedApiFile.String())
1786
1787 msg = "failed to update public API"
1788
1789 rule.Command().
1790 Text("touch").Output(d.updateCurrentApiTimestamp).
1791 Text(") || (").
1792 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1793 Text("; exit 38").
1794 Text(")")
1795
1796 rule.Build(pctx, ctx, "metalavaCurrentApiUpdate", "update current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001797 }
Nan Zhanga40da042018-08-01 12:48:00 -07001798
Pete Gillin581d6082018-10-22 15:55:04 +01001799 if String(d.properties.Check_nullability_warnings) != "" {
1800 if d.nullabilityWarningsFile == nil {
1801 ctx.PropertyErrorf("check_nullability_warnings",
1802 "Cannot specify check_nullability_warnings unless validating nullability")
1803 }
Colin Cross33961b52019-07-11 11:01:22 -07001804
1805 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
1806
Pete Gillin581d6082018-10-22 15:55:04 +01001807 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "check_nullability_warnings.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001808
Pete Gillin581d6082018-10-22 15:55:04 +01001809 msg := fmt.Sprintf(`\n******************************\n`+
1810 `The warnings encountered during nullability annotation validation did\n`+
1811 `not match the checked in file of expected warnings. The diffs are shown\n`+
1812 `above. You have two options:\n`+
1813 ` 1. Resolve the differences by editing the nullability annotations.\n`+
1814 ` 2. Update the file of expected warnings by running:\n`+
1815 ` cp %s %s\n`+
1816 ` and submitting the updated file as part of your change.`,
1817 d.nullabilityWarningsFile, checkNullabilityWarnings)
Colin Cross33961b52019-07-11 11:01:22 -07001818
1819 rule := android.NewRuleBuilder()
1820
1821 rule.Command().
1822 Text("(").
1823 Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile).
1824 Text("&&").
1825 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
1826 Text(") || (").
1827 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1828 Text("; exit 38").
1829 Text(")")
1830
1831 rule.Build(pctx, ctx, "nullabilityWarningsCheck", "nullability warnings check")
Pete Gillin581d6082018-10-22 15:55:04 +01001832 }
1833
Nan Zhang71bbe632018-09-17 14:32:21 -07001834 if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() {
Colin Cross33961b52019-07-11 11:01:22 -07001835 if len(d.Javadoc.properties.Out) > 0 {
1836 ctx.PropertyErrorf("out", "out property may not be combined with jdiff")
1837 }
1838
1839 outDir := android.PathForModuleOut(ctx, "jdiff-out")
1840 srcJarDir := android.PathForModuleOut(ctx, "jdiff-srcjars")
1841 stubsDir := android.PathForModuleOut(ctx, "jdiff-stubsDir")
1842
1843 rule := android.NewRuleBuilder()
Nan Zhang71bbe632018-09-17 14:32:21 -07001844
Nan Zhang86b06202018-09-21 17:09:21 -07001845 // Please sync with android-api-council@ before making any changes for the name of jdiffDocZip below
1846 // since there's cron job downstream that fetch this .zip file periodically.
1847 // See b/116221385 for reference.
Nan Zhang71bbe632018-09-17 14:32:21 -07001848 d.jdiffDocZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-docs.zip")
1849 d.jdiffStubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-stubs.srcjar")
1850
Nan Zhang71bbe632018-09-17 14:32:21 -07001851 jdiff := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jdiff.jar")
Nan Zhang71bbe632018-09-17 14:32:21 -07001852
Colin Cross33961b52019-07-11 11:01:22 -07001853 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
1854 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang71bbe632018-09-17 14:32:21 -07001855
Colin Cross33961b52019-07-11 11:01:22 -07001856 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1857
Colin Crossdaa4c672019-07-15 22:53:46 -07001858 cmd := javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -07001859 deps.bootClasspath, deps.classpath, d.sourcepaths)
1860
1861 cmd.Flag("-J-Xmx1600m").
Colin Cross33961b52019-07-11 11:01:22 -07001862 Flag("-XDignore.symbol.file").
1863 FlagWithArg("-doclet ", "jdiff.JDiff").
1864 FlagWithInput("-docletpath ", jdiff).
Paul Duffin86672f62020-06-19 18:39:55 +01001865 Flag("-quiet")
1866
1867 if d.apiXmlFile != nil {
1868 cmd.FlagWithArg("-newapi ", strings.TrimSuffix(d.apiXmlFile.Base(), d.apiXmlFile.Ext())).
1869 FlagWithArg("-newapidir ", filepath.Dir(d.apiXmlFile.String())).
1870 Implicit(d.apiXmlFile)
1871 }
1872
1873 if d.lastReleasedApiXmlFile != nil {
1874 cmd.FlagWithArg("-oldapi ", strings.TrimSuffix(d.lastReleasedApiXmlFile.Base(), d.lastReleasedApiXmlFile.Ext())).
1875 FlagWithArg("-oldapidir ", filepath.Dir(d.lastReleasedApiXmlFile.String())).
1876 Implicit(d.lastReleasedApiXmlFile)
1877 }
Colin Cross33961b52019-07-11 11:01:22 -07001878
Colin Cross33961b52019-07-11 11:01:22 -07001879 rule.Command().
1880 BuiltTool(ctx, "soong_zip").
1881 Flag("-write_if_changed").
1882 Flag("-d").
1883 FlagWithOutput("-o ", d.jdiffDocZip).
1884 FlagWithArg("-C ", outDir.String()).
1885 FlagWithArg("-D ", outDir.String())
1886
1887 rule.Command().
1888 BuiltTool(ctx, "soong_zip").
1889 Flag("-write_if_changed").
1890 Flag("-jar").
1891 FlagWithOutput("-o ", d.jdiffStubsSrcJar).
1892 FlagWithArg("-C ", stubsDir.String()).
1893 FlagWithArg("-D ", stubsDir.String())
1894
1895 rule.Restat()
1896
1897 zipSyncCleanupCmd(rule, srcJarDir)
1898
1899 rule.Build(pctx, ctx, "jdiff", "jdiff")
Nan Zhang71bbe632018-09-17 14:32:21 -07001900 }
Nan Zhang581fd212018-01-10 16:06:12 -08001901}
Dan Willemsencc090972018-02-26 14:33:31 -08001902
Nan Zhanga40da042018-08-01 12:48:00 -07001903//
Nan Zhangf4936b02018-08-01 15:00:28 -07001904// Exported Droiddoc Directory
Nan Zhanga40da042018-08-01 12:48:00 -07001905//
Dan Willemsencc090972018-02-26 14:33:31 -08001906var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
Nan Zhangf4936b02018-08-01 15:00:28 -07001907var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
Pete Gillin77167902018-09-19 18:16:26 +01001908var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
Nan Zhang9c69a122018-08-22 10:22:08 -07001909var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
Dan Willemsencc090972018-02-26 14:33:31 -08001910
Nan Zhangf4936b02018-08-01 15:00:28 -07001911type ExportedDroiddocDirProperties struct {
1912 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -08001913 Path *string
1914}
1915
Nan Zhangf4936b02018-08-01 15:00:28 -07001916type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -08001917 android.ModuleBase
1918
Nan Zhangf4936b02018-08-01 15:00:28 -07001919 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -08001920
1921 deps android.Paths
1922 dir android.Path
1923}
1924
Colin Crossa3002fc2019-07-08 16:48:04 -07001925// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -07001926func ExportedDroiddocDirFactory() android.Module {
1927 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -08001928 module.AddProperties(&module.properties)
1929 android.InitAndroidModule(module)
1930 return module
1931}
1932
Nan Zhangf4936b02018-08-01 15:00:28 -07001933func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -08001934
Nan Zhangf4936b02018-08-01 15:00:28 -07001935func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -08001936 path := String(d.properties.Path)
1937 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -08001938 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Dan Willemsencc090972018-02-26 14:33:31 -08001939}
Nan Zhangb2b33de2018-02-23 11:18:47 -08001940
1941//
1942// Defaults
1943//
1944type DocDefaults struct {
1945 android.ModuleBase
1946 android.DefaultsModuleBase
1947}
1948
Nan Zhangb2b33de2018-02-23 11:18:47 -08001949func DocDefaultsFactory() android.Module {
1950 module := &DocDefaults{}
1951
1952 module.AddProperties(
1953 &JavadocProperties{},
1954 &DroiddocProperties{},
1955 )
1956
1957 android.InitDefaultsModule(module)
1958
1959 return module
1960}
Nan Zhang1598a9e2018-09-04 17:14:32 -07001961
1962func StubsDefaultsFactory() android.Module {
1963 module := &DocDefaults{}
1964
1965 module.AddProperties(
1966 &JavadocProperties{},
1967 &DroidstubsProperties{},
1968 )
1969
1970 android.InitDefaultsModule(module)
1971
1972 return module
1973}
Colin Cross33961b52019-07-11 11:01:22 -07001974
1975func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
1976 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
1977
1978 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1979 rule.Command().Text("mkdir -p").Text(srcJarDir.String())
1980 srcJarList := srcJarDir.Join(ctx, "list")
1981
1982 rule.Temporary(srcJarList)
1983
1984 rule.Command().BuiltTool(ctx, "zipsync").
1985 FlagWithArg("-d ", srcJarDir.String()).
1986 FlagWithOutput("-l ", srcJarList).
1987 FlagWithArg("-f ", `"*.java"`).
1988 Inputs(srcJars)
1989
1990 return srcJarList
1991}
1992
1993func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
1994 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1995}
Paul Duffin91547182019-11-12 19:39:36 +00001996
1997var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
1998
1999type PrebuiltStubsSourcesProperties struct {
2000 Srcs []string `android:"path"`
2001}
2002
2003type PrebuiltStubsSources struct {
2004 android.ModuleBase
2005 android.DefaultableModuleBase
2006 prebuilt android.Prebuilt
2007 android.SdkBase
2008
2009 properties PrebuiltStubsSourcesProperties
2010
Paul Duffin9b478b02019-12-10 13:41:51 +00002011 // The source directories containing stubs source files.
2012 srcDirs android.Paths
Paul Duffin91547182019-11-12 19:39:36 +00002013 stubsSrcJar android.ModuleOutPath
2014}
2015
Paul Duffin9b478b02019-12-10 13:41:51 +00002016func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) {
2017 switch tag {
2018 case "":
2019 return android.Paths{p.stubsSrcJar}, nil
2020 default:
2021 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
2022 }
2023}
2024
Paul Duffin533f9c72020-05-20 16:18:00 +01002025func (d *PrebuiltStubsSources) StubsSrcJar() android.Path {
2026 return d.stubsSrcJar
2027}
2028
Paul Duffin91547182019-11-12 19:39:36 +00002029func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin9b478b02019-12-10 13:41:51 +00002030 p.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
2031
2032 p.srcDirs = android.PathsForModuleSrc(ctx, p.properties.Srcs)
2033
2034 rule := android.NewRuleBuilder()
2035 command := rule.Command().
2036 BuiltTool(ctx, "soong_zip").
2037 Flag("-write_if_changed").
2038 Flag("-jar").
2039 FlagWithOutput("-o ", p.stubsSrcJar)
2040
2041 for _, d := range p.srcDirs {
2042 dir := d.String()
2043 command.
2044 FlagWithArg("-C ", dir).
2045 FlagWithInput("-D ", d)
2046 }
2047
2048 rule.Restat()
2049
2050 rule.Build(pctx, ctx, "zip src", "Create srcjar from prebuilt source")
Paul Duffin91547182019-11-12 19:39:36 +00002051}
2052
2053func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
2054 return &p.prebuilt
2055}
2056
2057func (p *PrebuiltStubsSources) Name() string {
2058 return p.prebuilt.Name(p.ModuleBase.Name())
2059}
2060
Paul Duffin91547182019-11-12 19:39:36 +00002061// prebuilt_stubs_sources imports a set of java source files as if they were
2062// generated by droidstubs.
2063//
2064// By default, a prebuilt_stubs_sources has a single variant that expects a
2065// set of `.java` files generated by droidstubs.
2066//
2067// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2068// for host modules.
2069//
2070// Intended only for use by sdk snapshots.
2071func PrebuiltStubsSourcesFactory() android.Module {
2072 module := &PrebuiltStubsSources{}
2073
2074 module.AddProperties(&module.properties)
2075
2076 android.InitPrebuiltModule(module, &module.properties.Srcs)
2077 android.InitSdkAwareModule(module)
2078 InitDroiddocModule(module, android.HostAndDeviceSupported)
2079 return module
2080}
2081
Paul Duffin13879572019-11-28 14:31:38 +00002082type droidStubsSdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00002083 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00002084}
2085
2086func (mt *droidStubsSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2087 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2088}
2089
2090func (mt *droidStubsSdkMemberType) IsInstance(module android.Module) bool {
2091 _, ok := module.(*Droidstubs)
2092 return ok
2093}
2094
Paul Duffin93520ed2020-03-20 13:35:40 +00002095func (mt *droidStubsSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2096 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_stubs_sources")
2097}
2098
2099func (mt *droidStubsSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2100 return &droidStubsInfoProperties{}
2101}
2102
2103type droidStubsInfoProperties struct {
2104 android.SdkMemberPropertiesBase
2105
2106 StubsSrcJar android.Path
2107}
2108
2109func (p *droidStubsInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2110 droidstubs := variant.(*Droidstubs)
2111 p.StubsSrcJar = droidstubs.stubsSrcJar
2112}
2113
2114func (p *droidStubsInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
2115 if p.StubsSrcJar != nil {
2116 builder := ctx.SnapshotBuilder()
2117
2118 snapshotRelativeDir := filepath.Join("java", ctx.Name()+"_stubs_sources")
2119
2120 builder.UnzipToSnapshot(p.StubsSrcJar, snapshotRelativeDir)
2121
2122 propertySet.AddProperty("srcs", []string{snapshotRelativeDir})
Paul Duffin13879572019-11-28 14:31:38 +00002123 }
Paul Duffin91547182019-11-12 19:39:36 +00002124}