blob: a84e2b80c2677c5966cded7c53b8fb9468cecad4 [file] [log] [blame]
Nan Zhang581fd212018-01-10 16:06:12 -08001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Nan Zhang581fd212018-01-10 16:06:12 -080018 "fmt"
Nan Zhangb2b33de2018-02-23 11:18:47 -080019 "path/filepath"
Nan Zhang581fd212018-01-10 16:06:12 -080020 "strings"
21
Paul Duffin13879572019-11-28 14:31:38 +000022 "github.com/google/blueprint"
Jeongik Cha6bd33c12019-06-25 16:26:18 +090023 "github.com/google/blueprint/proptools"
Nan Zhang581fd212018-01-10 16:06:12 -080024
Colin Crossab054432019-07-15 16:13:59 -070025 "android/soong/android"
26 "android/soong/java/config"
Ramy Medhat427683c2020-04-30 03:08:37 -040027 "android/soong/remoteexec"
Nan Zhang581fd212018-01-10 16:06:12 -080028)
29
30func init() {
Paul Duffin884363e2019-12-19 10:21:09 +000031 RegisterDocsBuildComponents(android.InitRegistrationContext)
32 RegisterStubsBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000033
34 // Register sdk member type.
35 android.RegisterSdkMemberType(&droidStubsSdkMemberType{
36 SdkMemberTypeBase: android.SdkMemberTypeBase{
37 PropertyName: "stubs_sources",
Paul Duffine6029182019-12-16 17:43:48 +000038 // stubs_sources can be used with sdk to provide the source stubs for APIs provided by
39 // the APEX.
40 SupportsSdk: true,
Paul Duffin255f18e2019-12-13 11:22:16 +000041 },
42 })
Nan Zhang581fd212018-01-10 16:06:12 -080043}
44
Paul Duffin884363e2019-12-19 10:21:09 +000045func RegisterDocsBuildComponents(ctx android.RegistrationContext) {
46 ctx.RegisterModuleType("doc_defaults", DocDefaultsFactory)
47
48 ctx.RegisterModuleType("droiddoc", DroiddocFactory)
49 ctx.RegisterModuleType("droiddoc_host", DroiddocHostFactory)
50 ctx.RegisterModuleType("droiddoc_exported_dir", ExportedDroiddocDirFactory)
51 ctx.RegisterModuleType("javadoc", JavadocFactory)
52 ctx.RegisterModuleType("javadoc_host", JavadocHostFactory)
53}
54
55func RegisterStubsBuildComponents(ctx android.RegistrationContext) {
56 ctx.RegisterModuleType("stubs_defaults", StubsDefaultsFactory)
57
58 ctx.RegisterModuleType("droidstubs", DroidstubsFactory)
59 ctx.RegisterModuleType("droidstubs_host", DroidstubsHostFactory)
60
61 ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory)
62}
63
Colin Crossa1ce2a02018-06-20 15:19:39 -070064var (
65 srcsLibTag = dependencyTag{name: "sources from javalib"}
66)
67
Nan Zhang581fd212018-01-10 16:06:12 -080068type JavadocProperties struct {
69 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
70 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080071 Srcs []string `android:"path,arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080072
Nan Zhang581fd212018-01-10 16:06:12 -080073 // list of source files that should not be used to build the Java module.
74 // This is most useful in the arch/multilib variants to remove non-common files
75 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -080076 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080077
Jiyong Parkc6ddccf2019-09-13 20:56:14 +090078 // list of package names that should actually be used. If this property is left unspecified,
79 // all the sources from the srcs property is used.
80 Filter_packages []string
81
Nan Zhangb2b33de2018-02-23 11:18:47 -080082 // list of java libraries that will be in the classpath.
Nan Zhang581fd212018-01-10 16:06:12 -080083 Libs []string `android:"arch_variant"`
84
85 // If set to false, don't allow this module(-docs.zip) to be exported. Defaults to true.
Nan Zhangb2b33de2018-02-23 11:18:47 -080086 Installable *bool
Nan Zhang581fd212018-01-10 16:06:12 -080087
Paul Duffine25c6442019-10-11 13:50:28 +010088 // if not blank, set to the version of the sdk to compile against.
89 // Defaults to compiling against the current platform.
Nan Zhang581fd212018-01-10 16:06:12 -080090 Sdk_version *string `android:"arch_variant"`
Jiyong Park1e440682018-05-23 18:42:04 +090091
Paul Duffine25c6442019-10-11 13:50:28 +010092 // When targeting 1.9 and above, override the modules to use with --system,
93 // otherwise provides defaults libraries to add to the bootclasspath.
94 // Defaults to "none"
95 System_modules *string
96
Jiyong Park1e440682018-05-23 18:42:04 +090097 Aidl struct {
98 // Top level directories to pass to aidl tool
99 Include_dirs []string
100
101 // Directories rooted at the Android.bp file to pass to aidl tool
102 Local_include_dirs []string
103 }
Nan Zhang357466b2018-04-17 17:38:36 -0700104
105 // If not blank, set the java version passed to javadoc as -source
106 Java_version *string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700107
108 // local files that are used within user customized droiddoc options.
Colin Cross27b922f2019-03-04 22:35:41 -0800109 Arg_files []string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -0700110
Liz Kammer585cac22020-07-06 09:12:57 -0700111 // user customized droiddoc args. Deprecated, use flags instead.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700112 // Available variables for substitution:
113 //
114 // $(location <label>): the path to the arg_files with name <label>
Colin Crosse4a05842019-05-28 10:17:14 -0700115 // $$: a literal $
Nan Zhang1598a9e2018-09-04 17:14:32 -0700116 Args *string
117
Liz Kammer585cac22020-07-06 09:12:57 -0700118 // user customized droiddoc args. Not compatible with property args.
119 // Available variables for substitution:
120 //
121 // $(location <label>): the path to the arg_files with name <label>
122 // $$: a literal $
123 Flags []string
124
Nan Zhang1598a9e2018-09-04 17:14:32 -0700125 // names of the output files used in args that will be generated
126 Out []string
Ramy Medhat2f99eec2020-06-13 17:38:27 -0400127
128 // If set, metalava is sandboxed to only read files explicitly specified on the command
129 // line. Defaults to false.
130 Sandbox *bool
Nan Zhang581fd212018-01-10 16:06:12 -0800131}
132
Nan Zhang61819ce2018-05-04 18:49:16 -0700133type ApiToCheck struct {
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900134 // path to the API txt file that the new API extracted from source code is checked
135 // against. The path can be local to the module or from other module (via :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800136 Api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700137
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900138 // path to the API txt file that the new @removed API extractd from source code is
139 // checked against. The path can be local to the module or from other module (via
140 // :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800141 Removed_api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700142
Adrian Roos14f75a92019-08-12 17:54:09 +0200143 // If not blank, path to the baseline txt file for approved API check violations.
144 Baseline_file *string `android:"path"`
145
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900146 // Arguments to the apicheck tool.
Nan Zhang61819ce2018-05-04 18:49:16 -0700147 Args *string
148}
149
Nan Zhang581fd212018-01-10 16:06:12 -0800150type DroiddocProperties struct {
151 // directory relative to top of the source tree that contains doc templates files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800152 Custom_template *string
Nan Zhang581fd212018-01-10 16:06:12 -0800153
Nan Zhanga40da042018-08-01 12:48:00 -0700154 // directories under current module source which contains html/jd files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800155 Html_dirs []string
Nan Zhang581fd212018-01-10 16:06:12 -0800156
157 // set a value in the Clearsilver hdf namespace.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800158 Hdf []string
Nan Zhang581fd212018-01-10 16:06:12 -0800159
160 // proofread file contains all of the text content of the javadocs concatenated into one file,
161 // suitable for spell-checking and other goodness.
Colin Crossab054432019-07-15 16:13:59 -0700162 Proofread_file *string
Nan Zhang581fd212018-01-10 16:06:12 -0800163
164 // a todo file lists the program elements that are missing documentation.
165 // At some point, this might be improved to show more warnings.
Colin Cross27b922f2019-03-04 22:35:41 -0800166 Todo_file *string `android:"path"`
Nan Zhangb2b33de2018-02-23 11:18:47 -0800167
168 // directory under current module source that provide additional resources (images).
169 Resourcesdir *string
170
171 // resources output directory under out/soong/.intermediates.
172 Resourcesoutdir *string
Nan Zhang581fd212018-01-10 16:06:12 -0800173
Nan Zhange2ba5d42018-07-11 15:16:55 -0700174 // index.html under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800175 Static_doc_index_redirect *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700176
177 // source.properties under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800178 Static_doc_properties *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700179
Nan Zhang581fd212018-01-10 16:06:12 -0800180 // a list of files under current module source dir which contains known tags in Java sources.
181 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -0800182 Knowntags []string `android:"path"`
Nan Zhang28c68b92018-03-13 16:17:01 -0700183
Nan Zhang1598a9e2018-09-04 17:14:32 -0700184 // if set to true, generate docs through Dokka instead of Doclava.
185 Dokka_enabled *bool
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000186
187 // Compat config XML. Generates compat change documentation if set.
188 Compat_config *string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -0700189}
190
191type DroidstubsProperties struct {
Liz Kammer9ed79152020-08-24 15:56:32 -0700192 // The generated public API filename by Metalava, defaults to <module>_api.txt
Nan Zhang1598a9e2018-09-04 17:14:32 -0700193 Api_filename *string
194
Liz Kammer9ed79152020-08-24 15:56:32 -0700195 // the generated removed API filename by Metalava, defaults to <module>_removed.txt
Nan Zhang1598a9e2018-09-04 17:14:32 -0700196 Removed_api_filename *string
197
Nan Zhang199645c2018-09-19 12:40:06 -0700198 // the generated removed Dex API filename by Metalava.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700199 Removed_dex_api_filename *string
200
Nan Zhang1598a9e2018-09-04 17:14:32 -0700201 Check_api struct {
202 Last_released ApiToCheck
203
204 Current ApiToCheck
Inseob Kim38449af2019-02-28 14:24:05 +0900205
Paul Duffin160fe412020-05-10 19:32:20 +0100206 // The java_sdk_library module generates references to modules (i.e. filegroups)
207 // from which information about the latest API version can be obtained. As those
208 // modules may not exist (e.g. because a previous version has not been released) it
209 // sets ignore_missing_latest_api=true on the droidstubs modules it creates so
210 // that droidstubs can ignore those references if the modules do not yet exist.
211 //
212 // If true then this will ignore module references for modules that do not exist
213 // in properties that supply the previous version of the API.
214 //
215 // There are two sets of those:
216 // * Api_file, Removed_api_file in check_api.last_released
217 // * New_since in check_api.api_lint.new_since
218 //
219 // The first two must be set as a pair, so either they should both exist or neither
220 // should exist - in which case when this property is true they are ignored. If one
221 // exists and the other does not then it is an error.
Inseob Kim38449af2019-02-28 14:24:05 +0900222 Ignore_missing_latest_api *bool `blueprint:"mutated"`
Adrian Roos075eedc2019-10-10 12:07:03 +0200223
224 Api_lint struct {
225 Enabled *bool
226
227 // If set, performs api_lint on any new APIs not found in the given signature file
228 New_since *string `android:"path"`
229
230 // If not blank, path to the baseline txt file for approved API lint violations.
231 Baseline_file *string `android:"path"`
232 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700233 }
Nan Zhang79614d12018-04-19 18:03:39 -0700234
235 // user can specify the version of previous released API file in order to do compatibility check.
Colin Cross27b922f2019-03-04 22:35:41 -0800236 Previous_api *string `android:"path"`
Nan Zhang79614d12018-04-19 18:03:39 -0700237
238 // is set to true, Metalava will allow framework SDK to contain annotations.
Nan Zhang1598a9e2018-09-04 17:14:32 -0700239 Annotations_enabled *bool
Nan Zhang79614d12018-04-19 18:03:39 -0700240
Pete Gillin77167902018-09-19 18:16:26 +0100241 // 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 -0700242 Merge_annotations_dirs []string
Nan Zhang86d2d552018-08-09 15:33:27 -0700243
Pete Gillin77167902018-09-19 18:16:26 +0100244 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
245 Merge_inclusion_annotations_dirs []string
246
Pete Gillinc382a562018-11-14 18:45:46 +0000247 // a file containing a list of classes to do nullability validation for.
248 Validate_nullability_from_list *string
249
Pete Gillin581d6082018-10-22 15:55:04 +0100250 // a file containing expected warnings produced by validation of nullability annotations.
251 Check_nullability_warnings *string
252
Nan Zhang1598a9e2018-09-04 17:14:32 -0700253 // if set to true, allow Metalava to generate doc_stubs source files. Defaults to false.
254 Create_doc_stubs *bool
Nan Zhang9c69a122018-08-22 10:22:08 -0700255
Paul Duffin3ae29512020-04-08 18:18:03 +0100256 // if set to false then do not write out stubs. Defaults to true.
257 //
258 // TODO(b/146727827): Remove capability when we do not need to generate stubs and API separately.
259 Generate_stubs *bool
260
Nan Zhang9c69a122018-08-22 10:22:08 -0700261 // is set to true, Metalava will allow framework SDK to contain API levels annotations.
262 Api_levels_annotations_enabled *bool
263
264 // the dirs which Metalava extracts API levels annotations from.
265 Api_levels_annotations_dirs []string
266
Liz Kammer3d894b72020-08-04 09:55:13 -0700267 // the filename which Metalava extracts API levels annotations from. Defaults to android.jar.
268 Api_levels_jar_filename *string
269
Nan Zhang9c69a122018-08-22 10:22:08 -0700270 // if set to true, collect the values used by the Dev tools and
271 // write them in files packaged with the SDK. Defaults to false.
272 Write_sdk_values *bool
Nan Zhang71bbe632018-09-17 14:32:21 -0700273
274 // If set to true, .xml based public API file will be also generated, and
275 // JDiff tool will be invoked to genreate javadoc files. Defaults to false.
276 Jdiff_enabled *bool
Nan Zhang581fd212018-01-10 16:06:12 -0800277}
278
Nan Zhanga40da042018-08-01 12:48:00 -0700279//
280// Common flags passed down to build rule
281//
282type droiddocBuilderFlags struct {
Nan Zhang86d2d552018-08-09 15:33:27 -0700283 bootClasspathArgs string
284 classpathArgs string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700285 sourcepathArgs string
Nan Zhang86d2d552018-08-09 15:33:27 -0700286 dokkaClasspathArgs string
287 aidlFlags string
Colin Cross3047fa22019-04-18 10:56:44 -0700288 aidlDeps android.Paths
Nan Zhanga40da042018-08-01 12:48:00 -0700289
Nan Zhanga40da042018-08-01 12:48:00 -0700290 doclavaStubsFlags string
Nan Zhang86d2d552018-08-09 15:33:27 -0700291 doclavaDocsFlags string
Nan Zhanga40da042018-08-01 12:48:00 -0700292 postDoclavaCmds string
Nan Zhanga40da042018-08-01 12:48:00 -0700293}
294
295func InitDroiddocModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
296 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
297 android.InitDefaultableModule(module)
298}
299
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200300func apiCheckEnabled(ctx android.ModuleContext, apiToCheck ApiToCheck, apiVersionTag string) bool {
301 if ctx.Config().IsEnvTrue("WITHOUT_CHECK_API") {
302 return false
303 } else if String(apiToCheck.Api_file) != "" && String(apiToCheck.Removed_api_file) != "" {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700304 return true
305 } else if String(apiToCheck.Api_file) != "" {
306 panic("for " + apiVersionTag + " removed_api_file has to be non-empty!")
307 } else if String(apiToCheck.Removed_api_file) != "" {
308 panic("for " + apiVersionTag + " api_file has to be non-empty!")
309 }
310
311 return false
312}
313
Inseob Kim38449af2019-02-28 14:24:05 +0900314func ignoreMissingModules(ctx android.BottomUpMutatorContext, apiToCheck *ApiToCheck) {
315 api_file := String(apiToCheck.Api_file)
316 removed_api_file := String(apiToCheck.Removed_api_file)
317
318 api_module := android.SrcIsModule(api_file)
319 removed_api_module := android.SrcIsModule(removed_api_file)
320
321 if api_module == "" || removed_api_module == "" {
322 return
323 }
324
325 if ctx.OtherModuleExists(api_module) || ctx.OtherModuleExists(removed_api_module) {
326 return
327 }
328
329 apiToCheck.Api_file = nil
330 apiToCheck.Removed_api_file = nil
331}
332
Paul Duffin3d1248c2020-04-09 00:10:17 +0100333// Used by xsd_config
Nan Zhang1598a9e2018-09-04 17:14:32 -0700334type ApiFilePath interface {
335 ApiFilePath() android.Path
336}
337
Paul Duffin0f8faff2020-05-20 16:18:00 +0100338type ApiStubsSrcProvider interface {
339 StubsSrcJar() android.Path
340}
341
Paul Duffin3d1248c2020-04-09 00:10:17 +0100342// Provider of information about API stubs, used by java_sdk_library.
343type ApiStubsProvider interface {
344 ApiFilePath
Paul Duffin1fd005d2020-04-09 01:08:11 +0100345 RemovedApiFilePath() android.Path
Paul Duffin0f8faff2020-05-20 16:18:00 +0100346
347 ApiStubsSrcProvider
Paul Duffin3d1248c2020-04-09 00:10:17 +0100348}
349
Nan Zhanga40da042018-08-01 12:48:00 -0700350//
351// Javadoc
352//
Nan Zhang581fd212018-01-10 16:06:12 -0800353type Javadoc struct {
354 android.ModuleBase
355 android.DefaultableModuleBase
356
357 properties JavadocProperties
358
359 srcJars android.Paths
360 srcFiles android.Paths
361 sourcepaths android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700362 argFiles android.Paths
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400363 implicits android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700364
Liz Kammer585cac22020-07-06 09:12:57 -0700365 args []string
Nan Zhang581fd212018-01-10 16:06:12 -0800366
Nan Zhangccff0f72018-03-08 17:26:16 -0800367 docZip android.WritablePath
368 stubsSrcJar android.WritablePath
Nan Zhang581fd212018-01-10 16:06:12 -0800369}
370
Colin Cross41955e82019-05-29 14:40:35 -0700371func (j *Javadoc) OutputFiles(tag string) (android.Paths, error) {
372 switch tag {
373 case "":
374 return android.Paths{j.stubsSrcJar}, nil
Colin Crosse68e5542019-08-12 13:11:40 -0700375 case ".docs.zip":
376 return android.Paths{j.docZip}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700377 default:
378 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
379 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800380}
381
Colin Crossa3002fc2019-07-08 16:48:04 -0700382// javadoc converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800383func JavadocFactory() android.Module {
384 module := &Javadoc{}
385
386 module.AddProperties(&module.properties)
387
388 InitDroiddocModule(module, android.HostAndDeviceSupported)
389 return module
390}
391
Colin Crossa3002fc2019-07-08 16:48:04 -0700392// javadoc_host converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800393func JavadocHostFactory() android.Module {
394 module := &Javadoc{}
395
396 module.AddProperties(&module.properties)
397
398 InitDroiddocModule(module, android.HostSupported)
399 return module
400}
401
Colin Cross41955e82019-05-29 14:40:35 -0700402var _ android.OutputFileProducer = (*Javadoc)(nil)
Nan Zhang581fd212018-01-10 16:06:12 -0800403
Jiyong Park6a927c42020-01-21 02:03:43 +0900404func (j *Javadoc) sdkVersion() sdkSpec {
405 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700406}
407
Paul Duffine25c6442019-10-11 13:50:28 +0100408func (j *Javadoc) systemModules() string {
409 return proptools.String(j.properties.System_modules)
410}
411
Jiyong Park6a927c42020-01-21 02:03:43 +0900412func (j *Javadoc) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700413 return j.sdkVersion()
414}
415
Jiyong Park6a927c42020-01-21 02:03:43 +0900416func (j *Javadoc) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700417 return j.sdkVersion()
418}
419
Nan Zhang581fd212018-01-10 16:06:12 -0800420func (j *Javadoc) addDeps(ctx android.BottomUpMutatorContext) {
421 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100422 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Pete Gilline3d44b22020-06-29 11:28:51 +0100423 if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700424 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100425 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700426 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Pete Gilline3d44b22020-06-29 11:28:51 +0100427 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
Nan Zhang581fd212018-01-10 16:06:12 -0800428 }
429 }
430
Colin Cross42d48b72018-08-29 14:10:52 -0700431 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800432}
433
Nan Zhanga40da042018-08-01 12:48:00 -0700434func (j *Javadoc) collectAidlFlags(ctx android.ModuleContext, deps deps) droiddocBuilderFlags {
435 var flags droiddocBuilderFlags
Jiyong Park1e440682018-05-23 18:42:04 +0900436
Colin Cross3047fa22019-04-18 10:56:44 -0700437 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Jiyong Park1e440682018-05-23 18:42:04 +0900438
439 return flags
440}
441
442func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700443 aidlIncludeDirs android.Paths) (string, android.Paths) {
Jiyong Park1e440682018-05-23 18:42:04 +0900444
445 aidlIncludes := android.PathsForModuleSrc(ctx, j.properties.Aidl.Local_include_dirs)
446 aidlIncludes = append(aidlIncludes, android.PathsForSource(ctx, j.properties.Aidl.Include_dirs)...)
447
448 var flags []string
Colin Cross3047fa22019-04-18 10:56:44 -0700449 var deps android.Paths
450
Jiyong Park1e440682018-05-23 18:42:04 +0900451 if aidlPreprocess.Valid() {
452 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700453 deps = append(deps, aidlPreprocess.Path())
Jiyong Park1e440682018-05-23 18:42:04 +0900454 } else {
455 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
456 }
457
458 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
459 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
460 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
461 flags = append(flags, "-I"+src.String())
462 }
463
Colin Cross3047fa22019-04-18 10:56:44 -0700464 return strings.Join(flags, " "), deps
Jiyong Park1e440682018-05-23 18:42:04 +0900465}
466
Jiyong Parkd90d7412019-08-20 22:49:19 +0900467// TODO: remove the duplication between this and the one in gen.go
Jiyong Park1e440682018-05-23 18:42:04 +0900468func (j *Javadoc) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Nan Zhanga40da042018-08-01 12:48:00 -0700469 flags droiddocBuilderFlags) android.Paths {
Jiyong Park1e440682018-05-23 18:42:04 +0900470
471 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Crossc0806172019-06-14 18:51:47 -0700472 var aidlSrcs android.Paths
Jiyong Park1e440682018-05-23 18:42:04 +0900473
Jiyong Park1112c4c2019-08-16 21:12:10 +0900474 aidlIncludeFlags := genAidlIncludeFlags(srcFiles)
475
Jiyong Park1e440682018-05-23 18:42:04 +0900476 for _, srcFile := range srcFiles {
477 switch srcFile.Ext() {
478 case ".aidl":
Colin Crossc0806172019-06-14 18:51:47 -0700479 aidlSrcs = append(aidlSrcs, srcFile)
Jiyong Parkd90d7412019-08-20 22:49:19 +0900480 case ".logtags":
481 javaFile := genLogtags(ctx, srcFile)
482 outSrcFiles = append(outSrcFiles, javaFile)
Jiyong Park1e440682018-05-23 18:42:04 +0900483 default:
484 outSrcFiles = append(outSrcFiles, srcFile)
485 }
486 }
487
Colin Crossc0806172019-06-14 18:51:47 -0700488 // Process all aidl files together to support sharding them into one or more rules that produce srcjars.
489 if len(aidlSrcs) > 0 {
490 srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags+aidlIncludeFlags, flags.aidlDeps)
491 outSrcFiles = append(outSrcFiles, srcJarFiles...)
492 }
493
Jiyong Park1e440682018-05-23 18:42:04 +0900494 return outSrcFiles
495}
496
Nan Zhang581fd212018-01-10 16:06:12 -0800497func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
498 var deps deps
499
Colin Cross83bb3162018-06-25 15:48:06 -0700500 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800501 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700502 ctx.AddMissingDependencies(sdkDep.bootclasspath)
503 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Nan Zhang581fd212018-01-10 16:06:12 -0800504 } else if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700505 deps.bootClasspath = append(deps.bootClasspath, sdkDep.jars...)
Anton Hansson26bf49b2020-02-08 20:26:29 +0000506 deps.aidlPreprocess = sdkDep.aidl
507 } else {
508 deps.aidlPreprocess = sdkDep.aidl
Nan Zhang581fd212018-01-10 16:06:12 -0800509 }
510
511 ctx.VisitDirectDeps(func(module android.Module) {
512 otherName := ctx.OtherModuleName(module)
513 tag := ctx.OtherModuleDependencyTag(module)
514
Colin Cross2d24c1b2018-05-23 10:59:18 -0700515 switch tag {
516 case bootClasspathTag:
517 if dep, ok := module.(Dependency); ok {
Nan Zhang581fd212018-01-10 16:06:12 -0800518 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars()...)
Paul Duffin83a2d962019-11-19 19:44:10 +0000519 } else if sm, ok := module.(SystemModulesProvider); ok {
Paul Duffine25c6442019-10-11 13:50:28 +0100520 // A system modules dependency has been added to the bootclasspath
521 // so add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +0000522 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700523 } else {
524 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
525 }
526 case libTag:
527 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800528 case SdkLibraryDependency:
Paul Duffin649dadf2020-05-26 11:42:13 +0100529 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700530 case Dependency:
Sundong Ahnba493602018-11-20 17:36:35 +0900531 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park19a7f252019-07-10 16:59:31 +0900532 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700533 case android.SourceFileProducer:
Nan Zhang581fd212018-01-10 16:06:12 -0800534 checkProducesJars(ctx, dep)
535 deps.classpath = append(deps.classpath, dep.Srcs()...)
Nan Zhang581fd212018-01-10 16:06:12 -0800536 default:
537 ctx.ModuleErrorf("depends on non-java module %q", otherName)
538 }
Colin Cross6cef4812019-10-17 14:23:50 -0700539 case java9LibTag:
540 switch dep := module.(type) {
541 case Dependency:
542 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
543 default:
544 ctx.ModuleErrorf("depends on non-java module %q", otherName)
545 }
Nan Zhang357466b2018-04-17 17:38:36 -0700546 case systemModulesTag:
547 if deps.systemModules != nil {
548 panic("Found two system module dependencies")
549 }
Paul Duffin83a2d962019-11-19 19:44:10 +0000550 sm := module.(SystemModulesProvider)
551 outputDir, outputDeps := sm.OutputDirAndDeps()
552 deps.systemModules = &systemModules{outputDir, outputDeps}
Nan Zhang581fd212018-01-10 16:06:12 -0800553 }
554 })
555 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
556 // may contain filegroup or genrule.
Colin Cross8a497952019-03-05 22:25:09 -0800557 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400558 j.implicits = append(j.implicits, srcFiles...)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900559
560 filterByPackage := func(srcs []android.Path, filterPackages []string) []android.Path {
561 if filterPackages == nil {
562 return srcs
563 }
564 filtered := []android.Path{}
565 for _, src := range srcs {
566 if src.Ext() != ".java" {
567 // Don't filter-out non-Java (=generated sources) by package names. This is not ideal,
568 // but otherwise metalava emits stub sources having references to the generated AIDL classes
569 // in filtered-out pacages (e.g. com.android.internal.*).
570 // TODO(b/141149570) We need to fix this by introducing default private constructors or
571 // fixing metalava to not emit constructors having references to unknown classes.
572 filtered = append(filtered, src)
573 continue
574 }
575 packageName := strings.ReplaceAll(filepath.Dir(src.Rel()), "/", ".")
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800576 if android.HasAnyPrefix(packageName, filterPackages) {
577 filtered = append(filtered, src)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900578 }
579 }
580 return filtered
581 }
582 srcFiles = filterByPackage(srcFiles, j.properties.Filter_packages)
583
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400584 // While metalava needs package html files, it does not need them to be explicit on the command
585 // line. More importantly, the metalava rsp file is also used by the subsequent jdiff action if
586 // jdiff_enabled=true. javadoc complains if it receives html files on the command line. The filter
587 // below excludes html files from the rsp file for both metalava and jdiff. Note that the html
588 // files are still included as implicit inputs for successful remote execution and correct
589 // incremental builds.
590 filterHtml := func(srcs []android.Path) []android.Path {
591 filtered := []android.Path{}
592 for _, src := range srcs {
593 if src.Ext() == ".html" {
594 continue
595 }
596 filtered = append(filtered, src)
597 }
598 return filtered
599 }
600 srcFiles = filterHtml(srcFiles)
601
Liz Kammer585cac22020-07-06 09:12:57 -0700602 aidlFlags := j.collectAidlFlags(ctx, deps)
603 srcFiles = j.genSources(ctx, srcFiles, aidlFlags)
Nan Zhang581fd212018-01-10 16:06:12 -0800604
605 // srcs may depend on some genrule output.
606 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800607 j.srcJars = append(j.srcJars, deps.srcJars...)
608
Nan Zhang581fd212018-01-10 16:06:12 -0800609 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800610 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800611
Liz Kammere1ab2502020-09-10 15:29:25 +0000612 if len(j.srcFiles) > 0 {
613 j.sourcepaths = android.PathsForModuleSrc(ctx, []string{"."})
Nan Zhang581fd212018-01-10 16:06:12 -0800614 }
Nan Zhang581fd212018-01-10 16:06:12 -0800615
Colin Cross8a497952019-03-05 22:25:09 -0800616 j.argFiles = android.PathsForModuleSrc(ctx, j.properties.Arg_files)
Paul Duffin99e4a502019-02-11 15:38:42 +0000617 argFilesMap := map[string]string{}
618 argFileLabels := []string{}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700619
Paul Duffin99e4a502019-02-11 15:38:42 +0000620 for _, label := range j.properties.Arg_files {
Colin Cross8a497952019-03-05 22:25:09 -0800621 var paths = android.PathsForModuleSrc(ctx, []string{label})
Paul Duffin99e4a502019-02-11 15:38:42 +0000622 if _, exists := argFilesMap[label]; !exists {
623 argFilesMap[label] = strings.Join(paths.Strings(), " ")
624 argFileLabels = append(argFileLabels, label)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700625 } else {
626 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000627 label, argFilesMap[label], paths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700628 }
629 }
630
Liz Kammer585cac22020-07-06 09:12:57 -0700631 var argsPropertyName string
632 flags := make([]string, 0)
633 if j.properties.Args != nil && j.properties.Flags != nil {
634 ctx.PropertyErrorf("args", "flags is set. Cannot set args")
635 } else if args := proptools.String(j.properties.Args); args != "" {
636 flags = append(flags, args)
637 argsPropertyName = "args"
638 } else {
639 flags = append(flags, j.properties.Flags...)
640 argsPropertyName = "flags"
641 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700642
Liz Kammer585cac22020-07-06 09:12:57 -0700643 for _, flag := range flags {
644 args, err := android.Expand(flag, func(name string) (string, error) {
645 if strings.HasPrefix(name, "location ") {
646 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
647 if paths, ok := argFilesMap[label]; ok {
648 return paths, nil
649 } else {
650 return "", fmt.Errorf("unknown location label %q, expecting one of %q",
651 label, strings.Join(argFileLabels, ", "))
652 }
653 } else if name == "genDir" {
654 return android.PathForModuleGen(ctx).String(), nil
655 }
656 return "", fmt.Errorf("unknown variable '$(%s)'", name)
657 })
658
659 if err != nil {
660 ctx.PropertyErrorf(argsPropertyName, "%s", err.Error())
661 }
662 j.args = append(j.args, args)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700663 }
664
Nan Zhang581fd212018-01-10 16:06:12 -0800665 return deps
666}
667
668func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
669 j.addDeps(ctx)
670}
671
672func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
673 deps := j.collectDeps(ctx)
674
Colin Crossdaa4c672019-07-15 22:53:46 -0700675 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800676
Colin Crossdaa4c672019-07-15 22:53:46 -0700677 outDir := android.PathForModuleOut(ctx, "out")
678 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
679
680 j.stubsSrcJar = nil
681
682 rule := android.NewRuleBuilder()
683
684 rule.Command().Text("rm -rf").Text(outDir.String())
685 rule.Command().Text("mkdir -p").Text(outDir.String())
686
687 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, j.srcJars)
Nan Zhang357466b2018-04-17 17:38:36 -0700688
Colin Cross83bb3162018-06-25 15:48:06 -0700689 javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800690
Colin Crossdaa4c672019-07-15 22:53:46 -0700691 cmd := javadocSystemModulesCmd(ctx, rule, j.srcFiles, outDir, srcJarDir, srcJarList,
692 deps.systemModules, deps.classpath, j.sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800693
Colin Cross1e743852019-10-28 11:37:20 -0700694 cmd.FlagWithArg("-source ", javaVersion.String()).
Colin Crossdaa4c672019-07-15 22:53:46 -0700695 Flag("-J-Xmx1024m").
696 Flag("-XDignore.symbol.file").
697 Flag("-Xdoclint:none")
Nan Zhang581fd212018-01-10 16:06:12 -0800698
Colin Crossdaa4c672019-07-15 22:53:46 -0700699 rule.Command().
700 BuiltTool(ctx, "soong_zip").
701 Flag("-write_if_changed").
702 Flag("-d").
703 FlagWithOutput("-o ", j.docZip).
704 FlagWithArg("-C ", outDir.String()).
705 FlagWithArg("-D ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700706
Colin Crossdaa4c672019-07-15 22:53:46 -0700707 rule.Restat()
708
709 zipSyncCleanupCmd(rule, srcJarDir)
710
711 rule.Build(pctx, ctx, "javadoc", "javadoc")
Nan Zhang581fd212018-01-10 16:06:12 -0800712}
713
Nan Zhanga40da042018-08-01 12:48:00 -0700714//
715// Droiddoc
716//
717type Droiddoc struct {
718 Javadoc
719
Liz Kammere1ab2502020-09-10 15:29:25 +0000720 properties DroiddocProperties
Nan Zhanga40da042018-08-01 12:48:00 -0700721}
722
Colin Crossa3002fc2019-07-08 16:48:04 -0700723// droiddoc converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700724func DroiddocFactory() android.Module {
725 module := &Droiddoc{}
726
727 module.AddProperties(&module.properties,
728 &module.Javadoc.properties)
729
730 InitDroiddocModule(module, android.HostAndDeviceSupported)
731 return module
732}
733
Colin Crossa3002fc2019-07-08 16:48:04 -0700734// droiddoc_host converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700735func DroiddocHostFactory() android.Module {
736 module := &Droiddoc{}
737
738 module.AddProperties(&module.properties,
739 &module.Javadoc.properties)
740
741 InitDroiddocModule(module, android.HostSupported)
742 return module
743}
744
Liz Kammere1ab2502020-09-10 15:29:25 +0000745func (d *Droiddoc) OutputFiles(tag string) (android.Paths, error) {
746 switch tag {
747 case "", ".docs.zip":
748 return android.Paths{d.Javadoc.docZip}, nil
749 default:
750 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
751 }
Nan Zhanga40da042018-08-01 12:48:00 -0700752}
753
Nan Zhang581fd212018-01-10 16:06:12 -0800754func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
755 d.Javadoc.addDeps(ctx)
756
Nan Zhang79614d12018-04-19 18:03:39 -0700757 if String(d.properties.Custom_template) != "" {
Dan Willemsencc090972018-02-26 14:33:31 -0800758 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
759 }
Nan Zhang581fd212018-01-10 16:06:12 -0800760}
761
Colin Crossab054432019-07-15 16:13:59 -0700762func (d *Droiddoc) doclavaDocsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, docletPath classpath) {
Colin Cross2a2e0db2020-02-21 16:55:46 -0800763 buildNumberFile := ctx.Config().BuildNumberFile(ctx)
Nan Zhang443fa522018-08-20 20:58:28 -0700764 // Droiddoc always gets "-source 1.8" because it doesn't support 1.9 sources. For modules with 1.9
765 // sources, droiddoc will get sources produced by metalava which will have already stripped out the
766 // 1.9 language features.
Colin Crossab054432019-07-15 16:13:59 -0700767 cmd.FlagWithArg("-source ", "1.8").
768 Flag("-J-Xmx1600m").
769 Flag("-J-XX:-OmitStackTraceInFastThrow").
770 Flag("-XDignore.symbol.file").
771 FlagWithArg("-doclet ", "com.google.doclava.Doclava").
772 FlagWithInputList("-docletpath ", docletPath.Paths(), ":").
Colin Cross2a2e0db2020-02-21 16:55:46 -0800773 FlagWithArg("-hdf page.build ", ctx.Config().BuildId()+"-$(cat "+buildNumberFile.String()+")").OrderOnly(buildNumberFile).
Elliott Hughes26bce342019-09-12 15:05:13 -0700774 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 -0700775
Nan Zhanga40da042018-08-01 12:48:00 -0700776 if String(d.properties.Custom_template) == "" {
777 // TODO: This is almost always droiddoc-templates-sdk
778 ctx.PropertyErrorf("custom_template", "must specify a template")
779 }
780
781 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
Nan Zhangf4936b02018-08-01 15:00:28 -0700782 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Crossab054432019-07-15 16:13:59 -0700783 cmd.FlagWithArg("-templatedir ", t.dir.String()).Implicits(t.deps)
Nan Zhanga40da042018-08-01 12:48:00 -0700784 } else {
Paul Duffin884363e2019-12-19 10:21:09 +0000785 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_exported_dir", ctx.OtherModuleName(m))
Nan Zhanga40da042018-08-01 12:48:00 -0700786 }
787 })
788
789 if len(d.properties.Html_dirs) > 0 {
Colin Crossab054432019-07-15 16:13:59 -0700790 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
791 cmd.FlagWithArg("-htmldir ", htmlDir.String()).
792 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[0], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700793 }
794
795 if len(d.properties.Html_dirs) > 1 {
Colin Crossab054432019-07-15 16:13:59 -0700796 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
797 cmd.FlagWithArg("-htmldir2 ", htmlDir2.String()).
798 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[1], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700799 }
800
801 if len(d.properties.Html_dirs) > 2 {
802 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
803 }
804
Colin Cross8a497952019-03-05 22:25:09 -0800805 knownTags := android.PathsForModuleSrc(ctx, d.properties.Knowntags)
Colin Crossab054432019-07-15 16:13:59 -0700806 cmd.FlagForEachInput("-knowntags ", knownTags)
Nan Zhanga40da042018-08-01 12:48:00 -0700807
Colin Crossab054432019-07-15 16:13:59 -0700808 cmd.FlagForEachArg("-hdf ", d.properties.Hdf)
Nan Zhanga40da042018-08-01 12:48:00 -0700809
810 if String(d.properties.Proofread_file) != "" {
811 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
Colin Crossab054432019-07-15 16:13:59 -0700812 cmd.FlagWithOutput("-proofread ", proofreadFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700813 }
814
815 if String(d.properties.Todo_file) != "" {
816 // tricky part:
817 // we should not compute full path for todo_file through PathForModuleOut().
818 // the non-standard doclet will get the full path relative to "-o".
Colin Crossab054432019-07-15 16:13:59 -0700819 cmd.FlagWithArg("-todo ", String(d.properties.Todo_file)).
820 ImplicitOutput(android.PathForModuleOut(ctx, String(d.properties.Todo_file)))
Nan Zhanga40da042018-08-01 12:48:00 -0700821 }
822
823 if String(d.properties.Resourcesdir) != "" {
824 // TODO: should we add files under resourcesDir to the implicits? It seems that
825 // resourcesDir is one sub dir of htmlDir
826 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
Colin Crossab054432019-07-15 16:13:59 -0700827 cmd.FlagWithArg("-resourcesdir ", resourcesDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700828 }
829
830 if String(d.properties.Resourcesoutdir) != "" {
831 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
Colin Crossab054432019-07-15 16:13:59 -0700832 cmd.FlagWithArg("-resourcesoutdir ", String(d.properties.Resourcesoutdir))
Nan Zhanga40da042018-08-01 12:48:00 -0700833 }
Nan Zhanga40da042018-08-01 12:48:00 -0700834}
835
Colin Crossab054432019-07-15 16:13:59 -0700836func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700837 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700838 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
839 rule.Command().Text("cp").
840 Input(staticDocIndexRedirect).
841 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700842 }
843
844 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700845 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
846 rule.Command().Text("cp").
847 Input(staticDocProperties).
848 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700849 }
Nan Zhanga40da042018-08-01 12:48:00 -0700850}
851
Colin Crossab054432019-07-15 16:13:59 -0700852func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700853 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700854
855 cmd := rule.Command().
856 BuiltTool(ctx, "soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
857 Flag(config.JavacVmFlags).
858 FlagWithArg("-encoding ", "UTF-8").
Colin Crossab054432019-07-15 16:13:59 -0700859 FlagWithRspFileInputList("@", srcs).
860 FlagWithInput("@", srcJarList)
861
Colin Crossab054432019-07-15 16:13:59 -0700862 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
863 // based stubs generation.
864 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
865 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
866 // the correct package name base path.
867 if len(sourcepaths) > 0 {
868 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
869 } else {
870 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
871 }
872
873 cmd.FlagWithArg("-d ", outDir.String()).
874 Flag("-quiet")
875
876 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700877}
878
Colin Crossdaa4c672019-07-15 22:53:46 -0700879func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
880 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
881 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
882
883 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
884
885 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
886 cmd.Flag(flag).Implicits(deps)
887
888 cmd.FlagWithArg("--patch-module ", "java.base=.")
889
890 if len(classpath) > 0 {
891 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
892 }
893
894 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700895}
896
Colin Crossdaa4c672019-07-15 22:53:46 -0700897func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
898 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
899 sourcepaths android.Paths) *android.RuleBuilderCommand {
900
901 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
902
903 if len(bootclasspath) == 0 && ctx.Device() {
904 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
905 // ensure java does not fall back to the default bootclasspath.
906 cmd.FlagWithArg("-bootclasspath ", `""`)
907 } else if len(bootclasspath) > 0 {
908 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
909 }
910
911 if len(classpath) > 0 {
912 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
913 }
914
915 return cmd
916}
917
Colin Crossab054432019-07-15 16:13:59 -0700918func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
919 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700920
Colin Crossab054432019-07-15 16:13:59 -0700921 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
922 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
923
924 return rule.Command().
925 BuiltTool(ctx, "dokka").
926 Flag(config.JavacVmFlags).
927 Flag(srcJarDir.String()).
928 FlagWithInputList("-classpath ", dokkaClasspath, ":").
929 FlagWithArg("-format ", "dac").
930 FlagWithArg("-dacRoot ", "/reference/kotlin").
931 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700932}
933
934func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
935 deps := d.Javadoc.collectDeps(ctx)
936
Colin Crossdaa4c672019-07-15 22:53:46 -0700937 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Colin Crossdaa4c672019-07-15 22:53:46 -0700938
Nan Zhang1598a9e2018-09-04 17:14:32 -0700939 jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
940 doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700941
Colin Crossab054432019-07-15 16:13:59 -0700942 outDir := android.PathForModuleOut(ctx, "out")
943 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700944
Colin Crossab054432019-07-15 16:13:59 -0700945 rule := android.NewRuleBuilder()
Nan Zhang1598a9e2018-09-04 17:14:32 -0700946
Colin Crossab054432019-07-15 16:13:59 -0700947 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
948
949 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -0700950 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -0700951 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700952 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -0700953 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -0700954 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700955 }
956
Liz Kammer585cac22020-07-06 09:12:57 -0700957 cmd.Flag(strings.Join(d.Javadoc.args, " ")).Implicits(d.Javadoc.argFiles)
Colin Crossab054432019-07-15 16:13:59 -0700958
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000959 if d.properties.Compat_config != nil {
960 compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
961 cmd.FlagWithInput("-compatconfig ", compatConfig)
962 }
963
Colin Crossab054432019-07-15 16:13:59 -0700964 var desc string
965 if Bool(d.properties.Dokka_enabled) {
966 desc = "dokka"
967 } else {
968 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
969
970 for _, o := range d.Javadoc.properties.Out {
971 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
972 }
973
974 d.postDoclavaCmds(ctx, rule)
975 desc = "doclava"
976 }
977
978 rule.Command().
979 BuiltTool(ctx, "soong_zip").
980 Flag("-write_if_changed").
981 Flag("-d").
982 FlagWithOutput("-o ", d.docZip).
983 FlagWithArg("-C ", outDir.String()).
984 FlagWithArg("-D ", outDir.String())
985
Colin Crossab054432019-07-15 16:13:59 -0700986 rule.Restat()
987
988 zipSyncCleanupCmd(rule, srcJarDir)
989
990 rule.Build(pctx, ctx, "javadoc", desc)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700991}
992
993//
994// Droidstubs
995//
996type Droidstubs struct {
997 Javadoc
Paul Duffin91547182019-11-12 19:39:36 +0000998 android.SdkBase
Nan Zhang1598a9e2018-09-04 17:14:32 -0700999
Pete Gillin581d6082018-10-22 15:55:04 +01001000 properties DroidstubsProperties
1001 apiFile android.WritablePath
1002 apiXmlFile android.WritablePath
1003 lastReleasedApiXmlFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001004 privateApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001005 removedApiFile android.WritablePath
1006 removedDexApiFile android.WritablePath
Pete Gillin581d6082018-10-22 15:55:04 +01001007 nullabilityWarningsFile android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001008
1009 checkCurrentApiTimestamp android.WritablePath
1010 updateCurrentApiTimestamp android.WritablePath
1011 checkLastReleasedApiTimestamp android.WritablePath
Adrian Roos075eedc2019-10-10 12:07:03 +02001012 apiLintTimestamp android.WritablePath
Adrian Roos3b8f1cd2019-11-01 13:42:39 +01001013 apiLintReport android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001014
Pete Gillin581d6082018-10-22 15:55:04 +01001015 checkNullabilityWarningsTimestamp android.WritablePath
1016
Nan Zhang1598a9e2018-09-04 17:14:32 -07001017 annotationsZip android.WritablePath
Nan Zhang9c69a122018-08-22 10:22:08 -07001018 apiVersionsXml android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001019
1020 apiFilePath android.Path
Nan Zhang71bbe632018-09-17 14:32:21 -07001021
1022 jdiffDocZip android.WritablePath
1023 jdiffStubsSrcJar android.WritablePath
Jerome Gaillard0f599032019-10-10 19:29:11 +01001024
1025 metadataZip android.WritablePath
1026 metadataDir android.WritablePath
Nan Zhang1598a9e2018-09-04 17:14:32 -07001027}
1028
Colin Crossa3002fc2019-07-08 16:48:04 -07001029// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
1030// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to
1031// a droiddoc module to generate documentation.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001032func DroidstubsFactory() android.Module {
1033 module := &Droidstubs{}
1034
1035 module.AddProperties(&module.properties,
1036 &module.Javadoc.properties)
1037
1038 InitDroiddocModule(module, android.HostAndDeviceSupported)
Paul Duffin91547182019-11-12 19:39:36 +00001039 android.InitSdkAwareModule(module)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001040 return module
1041}
1042
Colin Crossa3002fc2019-07-08 16:48:04 -07001043// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API
1044// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be
1045// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs
1046// module when symbols needed by the source files are provided by java_library_host modules.
Nan Zhang1598a9e2018-09-04 17:14:32 -07001047func DroidstubsHostFactory() android.Module {
1048 module := &Droidstubs{}
1049
1050 module.AddProperties(&module.properties,
1051 &module.Javadoc.properties)
1052
1053 InitDroiddocModule(module, android.HostSupported)
1054 return module
1055}
1056
Colin Cross014489c2020-06-02 20:09:13 -07001057func (d *Droidstubs) OutputFiles(tag string) (android.Paths, error) {
1058 switch tag {
1059 case "":
1060 return android.Paths{d.stubsSrcJar}, nil
1061 case ".docs.zip":
1062 return android.Paths{d.docZip}, nil
1063 case ".annotations.zip":
1064 return android.Paths{d.annotationsZip}, nil
1065 case ".api_versions.xml":
1066 return android.Paths{d.apiVersionsXml}, nil
1067 default:
1068 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1069 }
1070}
1071
Nan Zhang1598a9e2018-09-04 17:14:32 -07001072func (d *Droidstubs) ApiFilePath() android.Path {
1073 return d.apiFilePath
1074}
1075
Paul Duffin1fd005d2020-04-09 01:08:11 +01001076func (d *Droidstubs) RemovedApiFilePath() android.Path {
1077 return d.removedApiFile
1078}
1079
Paul Duffin3d1248c2020-04-09 00:10:17 +01001080func (d *Droidstubs) StubsSrcJar() android.Path {
1081 return d.stubsSrcJar
1082}
1083
Nan Zhang1598a9e2018-09-04 17:14:32 -07001084func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
1085 d.Javadoc.addDeps(ctx)
1086
Paul Duffin160fe412020-05-10 19:32:20 +01001087 // If requested clear any properties that provide information about the latest version
1088 // of an API and which reference non-existent modules.
Inseob Kim38449af2019-02-28 14:24:05 +09001089 if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
1090 ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
Paul Duffin160fe412020-05-10 19:32:20 +01001091
1092 // If the new_since references a module, e.g. :module-latest-api and the module
1093 // does not exist then clear it.
1094 newSinceSrc := d.properties.Check_api.Api_lint.New_since
1095 newSinceSrcModule := android.SrcIsModule(proptools.String(newSinceSrc))
1096 if newSinceSrcModule != "" && !ctx.OtherModuleExists(newSinceSrcModule) {
1097 d.properties.Check_api.Api_lint.New_since = nil
1098 }
Inseob Kim38449af2019-02-28 14:24:05 +09001099 }
1100
Nan Zhang1598a9e2018-09-04 17:14:32 -07001101 if len(d.properties.Merge_annotations_dirs) != 0 {
1102 for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
1103 ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
1104 }
1105 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001106
Pete Gillin77167902018-09-19 18:16:26 +01001107 if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
1108 for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
1109 ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
1110 }
1111 }
1112
Nan Zhang9c69a122018-08-22 10:22:08 -07001113 if len(d.properties.Api_levels_annotations_dirs) != 0 {
1114 for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
1115 ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
1116 }
1117 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001118}
1119
Paul Duffin3ae29512020-04-08 18:18:03 +01001120func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath) {
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001121 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1122 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001123 String(d.properties.Api_filename) != "" {
Liz Kammer9ed79152020-08-24 15:56:32 -07001124 filename := proptools.StringDefault(d.properties.Api_filename, ctx.ModuleName()+"_api.txt")
1125 d.apiFile = android.PathForModuleOut(ctx, filename)
Colin Cross33961b52019-07-11 11:01:22 -07001126 cmd.FlagWithOutput("--api ", d.apiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001127 d.apiFilePath = d.apiFile
1128 }
1129
Luca Stefanid63ea0a2019-09-01 21:49:45 +02001130 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1131 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
Nan Zhang1598a9e2018-09-04 17:14:32 -07001132 String(d.properties.Removed_api_filename) != "" {
Liz Kammer9ed79152020-08-24 15:56:32 -07001133 filename := proptools.StringDefault(d.properties.Removed_api_filename, ctx.ModuleName()+"_removed.txt")
1134 d.removedApiFile = android.PathForModuleOut(ctx, filename)
Colin Cross33961b52019-07-11 11:01:22 -07001135 cmd.FlagWithOutput("--removed-api ", d.removedApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001136 }
1137
Nan Zhang1598a9e2018-09-04 17:14:32 -07001138 if String(d.properties.Removed_dex_api_filename) != "" {
1139 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
Colin Cross33961b52019-07-11 11:01:22 -07001140 cmd.FlagWithOutput("--removed-dex-api ", d.removedDexApiFile)
Nan Zhang1598a9e2018-09-04 17:14:32 -07001141 }
1142
Nan Zhang9c69a122018-08-22 10:22:08 -07001143 if Bool(d.properties.Write_sdk_values) {
Jerome Gaillard0f599032019-10-10 19:29:11 +01001144 d.metadataDir = android.PathForModuleOut(ctx, "metadata")
1145 cmd.FlagWithArg("--sdk-values ", d.metadataDir.String())
Nan Zhang9c69a122018-08-22 10:22:08 -07001146 }
1147
Paul Duffin3ae29512020-04-08 18:18:03 +01001148 if stubsDir.Valid() {
1149 if Bool(d.properties.Create_doc_stubs) {
1150 cmd.FlagWithArg("--doc-stubs ", stubsDir.String())
1151 } else {
1152 cmd.FlagWithArg("--stubs ", stubsDir.String())
1153 cmd.Flag("--exclude-documentation-from-stubs")
1154 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001155 }
Nan Zhang1598a9e2018-09-04 17:14:32 -07001156}
1157
Colin Cross33961b52019-07-11 11:01:22 -07001158func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Nan Zhang1598a9e2018-09-04 17:14:32 -07001159 if Bool(d.properties.Annotations_enabled) {
Colin Cross33961b52019-07-11 11:01:22 -07001160 cmd.Flag("--include-annotations")
1161
Pete Gillinc382a562018-11-14 18:45:46 +00001162 validatingNullability :=
Liz Kammer585cac22020-07-06 09:12:57 -07001163 android.InList("--validate-nullability-from-merged-stubs", d.Javadoc.args) ||
Pete Gillinc382a562018-11-14 18:45:46 +00001164 String(d.properties.Validate_nullability_from_list) != ""
Paul Duffin13a9dd62019-11-04 10:26:47 +00001165
Pete Gillina262c052018-09-14 14:25:48 +01001166 migratingNullability := String(d.properties.Previous_api) != ""
Pete Gillina262c052018-09-14 14:25:48 +01001167 if migratingNullability {
Colin Cross8a497952019-03-05 22:25:09 -08001168 previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
Colin Cross33961b52019-07-11 11:01:22 -07001169 cmd.FlagWithInput("--migrate-nullness ", previousApi)
Pete Gillina262c052018-09-14 14:25:48 +01001170 }
Colin Cross33961b52019-07-11 11:01:22 -07001171
Pete Gillinc382a562018-11-14 18:45:46 +00001172 if s := String(d.properties.Validate_nullability_from_list); s != "" {
Colin Cross33961b52019-07-11 11:01:22 -07001173 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
Pete Gillinc382a562018-11-14 18:45:46 +00001174 }
Colin Cross33961b52019-07-11 11:01:22 -07001175
Pete Gillina262c052018-09-14 14:25:48 +01001176 if validatingNullability {
Pete Gillin581d6082018-10-22 15:55:04 +01001177 d.nullabilityWarningsFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_nullability_warnings.txt")
Colin Cross33961b52019-07-11 11:01:22 -07001178 cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile)
Pete Gillina262c052018-09-14 14:25:48 +01001179 }
Nan Zhanga40da042018-08-01 12:48:00 -07001180
1181 d.annotationsZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"_annotations.zip")
Colin Cross33961b52019-07-11 11:01:22 -07001182 cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip)
Nan Zhangf4936b02018-08-01 15:00:28 -07001183
Anton Hansson9d7c3fb2020-05-21 10:11:31 +01001184 if len(d.properties.Merge_annotations_dirs) != 0 {
1185 d.mergeAnnoDirFlags(ctx, cmd)
Nan Zhanga40da042018-08-01 12:48:00 -07001186 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001187
Colin Cross33961b52019-07-11 11:01:22 -07001188 // TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
1189 cmd.FlagWithArg("--hide ", "HiddenTypedefConstant").
1190 FlagWithArg("--hide ", "SuperfluousPrefix").
1191 FlagWithArg("--hide ", "AnnotationExtraction")
1192 }
Neil Fullerb2f14ec2018-10-21 22:13:19 +01001193}
1194
Colin Cross33961b52019-07-11 11:01:22 -07001195func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
1196 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
1197 if t, ok := m.(*ExportedDroiddocDir); ok {
1198 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
1199 } else {
1200 ctx.PropertyErrorf("merge_annotations_dirs",
1201 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1202 }
1203 })
1204}
1205
1206func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Pete Gillin77167902018-09-19 18:16:26 +01001207 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
1208 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Cross33961b52019-07-11 11:01:22 -07001209 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
Pete Gillin77167902018-09-19 18:16:26 +01001210 } else {
1211 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
1212 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
1213 }
1214 })
Nan Zhanga40da042018-08-01 12:48:00 -07001215}
1216
Colin Cross33961b52019-07-11 11:01:22 -07001217func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001218 if !Bool(d.properties.Api_levels_annotations_enabled) {
1219 return
Nan Zhang9c69a122018-08-22 10:22:08 -07001220 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001221
1222 d.apiVersionsXml = android.PathForModuleOut(ctx, "api-versions.xml")
1223
1224 if len(d.properties.Api_levels_annotations_dirs) == 0 {
1225 ctx.PropertyErrorf("api_levels_annotations_dirs",
1226 "has to be non-empty if api levels annotations was enabled!")
1227 }
1228
1229 cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml)
1230 cmd.FlagWithInput("--apply-api-levels ", d.apiVersionsXml)
1231 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion())
1232 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
1233
1234 filename := proptools.StringDefault(d.properties.Api_levels_jar_filename, "android.jar")
1235
1236 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
1237 if t, ok := m.(*ExportedDroiddocDir); ok {
1238 for _, dep := range t.deps {
1239 if strings.HasSuffix(dep.String(), filename) {
1240 cmd.Implicit(dep)
1241 }
1242 }
1243 cmd.FlagWithArg("--android-jar-pattern ", t.dir.String()+"/%/public/"+filename)
1244 } else {
1245 ctx.PropertyErrorf("api_levels_annotations_dirs",
1246 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
1247 }
1248 })
Nan Zhang9c69a122018-08-22 10:22:08 -07001249}
1250
Colin Cross33961b52019-07-11 11:01:22 -07001251func (d *Droidstubs) apiToXmlFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Dan Willemsen9f435972020-05-28 15:28:00 -07001252 if Bool(d.properties.Jdiff_enabled) && d.apiFile != nil {
Nan Zhang71bbe632018-09-17 14:32:21 -07001253 if d.apiFile.String() == "" {
1254 ctx.ModuleErrorf("API signature file has to be specified in Metalava when jdiff is enabled.")
1255 }
1256
1257 d.apiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001258 cmd.FlagWithOutput("--api-xml ", d.apiXmlFile)
Nan Zhang71bbe632018-09-17 14:32:21 -07001259
1260 if String(d.properties.Check_api.Last_released.Api_file) == "" {
1261 ctx.PropertyErrorf("check_api.last_released.api_file",
1262 "has to be non-empty if jdiff was enabled!")
1263 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001264
Colin Cross33961b52019-07-11 11:01:22 -07001265 lastReleasedApi := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
Nan Zhang71bbe632018-09-17 14:32:21 -07001266 d.lastReleasedApiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_last_released_api.xml")
Colin Cross33961b52019-07-11 11:01:22 -07001267 cmd.FlagWithInput("--convert-to-jdiff ", lastReleasedApi).Output(d.lastReleasedApiXmlFile)
1268 }
1269}
Nan Zhang71bbe632018-09-17 14:32:21 -07001270
Colin Cross1e743852019-10-28 11:37:20 -07001271func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths,
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001272 srcJarList android.Path, bootclasspath, classpath classpath, sourcepaths android.Paths, implicitsRsp android.WritablePath, sandbox bool) *android.RuleBuilderCommand {
Colin Cross8b8bec32019-11-15 13:18:43 -08001273 // Metalava uses lots of memory, restrict the number of metalava jobs that can run in parallel.
1274 rule.HighMem()
Ramy Medhat427683c2020-04-30 03:08:37 -04001275 cmd := rule.Command()
Ramy Medhat16f23a42020-09-03 01:29:49 -04001276 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_METALAVA") {
Ramy Medhat427683c2020-04-30 03:08:37 -04001277 rule.Remoteable(android.RemoteRuleSupports{RBE: true})
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001278 pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "metalava")
1279 execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
1280 labels := map[string]string{"type": "compile", "lang": "java", "compiler": "metalava"}
1281 if !sandbox {
1282 execStrategy = remoteexec.LocalExecStrategy
1283 labels["shallow"] = "true"
Ramy Medhat427683c2020-04-30 03:08:37 -04001284 }
1285 inputs := []string{android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "metalava.jar").String()}
1286 if v := ctx.Config().Getenv("RBE_METALAVA_INPUTS"); v != "" {
1287 inputs = append(inputs, strings.Split(v, ",")...)
1288 }
1289 cmd.Text((&remoteexec.REParams{
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001290 Labels: labels,
Ramy Medhat427683c2020-04-30 03:08:37 -04001291 ExecStrategy: execStrategy,
1292 Inputs: inputs,
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001293 RSPFile: implicitsRsp.String(),
Ramy Medhat427683c2020-04-30 03:08:37 -04001294 ToolchainInputs: []string{config.JavaCmd(ctx).String()},
1295 Platform: map[string]string{remoteexec.PoolKey: pool},
1296 }).NoVarTemplate(ctx.Config()))
1297 }
1298
1299 cmd.BuiltTool(ctx, "metalava").
Colin Cross33961b52019-07-11 11:01:22 -07001300 Flag(config.JavacVmFlags).
Aurimas Liutikas4c5efde2020-09-21 11:18:06 -07001301 Flag("-J--add-opens=java.base/java.util=ALL-UNNAMED").
Colin Cross33961b52019-07-11 11:01:22 -07001302 FlagWithArg("-encoding ", "UTF-8").
Colin Cross1e743852019-10-28 11:37:20 -07001303 FlagWithArg("-source ", javaVersion.String()).
Colin Cross33961b52019-07-11 11:01:22 -07001304 FlagWithRspFileInputList("@", srcs).
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001305 FlagWithInput("@", srcJarList)
1306
1307 if javaHome := ctx.Config().Getenv("ANDROID_JAVA_HOME"); javaHome != "" {
1308 cmd.Implicit(android.PathForSource(ctx, javaHome))
1309 }
1310
1311 if sandbox {
1312 cmd.FlagWithOutput("--strict-input-files ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
1313 } else {
1314 cmd.FlagWithOutput("--strict-input-files:warn ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
1315 }
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001316
Colin Cross238c1f32020-06-07 16:58:18 -07001317 if implicitsRsp != nil {
Ramy Medhatc8d60bc2020-06-04 01:54:07 -04001318 cmd.FlagWithArg("--strict-input-files-exempt ", "@"+implicitsRsp.String())
1319 }
Colin Cross33961b52019-07-11 11:01:22 -07001320
1321 if len(bootclasspath) > 0 {
1322 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
Nan Zhang71bbe632018-09-17 14:32:21 -07001323 }
1324
Colin Cross33961b52019-07-11 11:01:22 -07001325 if len(classpath) > 0 {
1326 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
1327 }
Nan Zhang71bbe632018-09-17 14:32:21 -07001328
Colin Cross33961b52019-07-11 11:01:22 -07001329 if len(sourcepaths) > 0 {
1330 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
1331 } else {
1332 cmd.FlagWithArg("-sourcepath ", `""`)
1333 }
Nan Zhang9c69a122018-08-22 10:22:08 -07001334
Colin Cross33961b52019-07-11 11:01:22 -07001335 cmd.Flag("--no-banner").
1336 Flag("--color").
1337 Flag("--quiet").
Makoto Onuki0df103a2020-07-17 15:11:24 -07001338 Flag("--format=v2").
1339 FlagWithArg("--repeat-errors-max ", "10").
1340 FlagWithArg("--hide ", "UnresolvedImport")
Nan Zhang86d2d552018-08-09 15:33:27 -07001341
Colin Cross33961b52019-07-11 11:01:22 -07001342 return cmd
Nan Zhang71bbe632018-09-17 14:32:21 -07001343}
1344
Nan Zhang1598a9e2018-09-04 17:14:32 -07001345func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga40da042018-08-01 12:48:00 -07001346 deps := d.Javadoc.collectDeps(ctx)
1347
1348 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), sdkContext(d))
Nan Zhang581fd212018-01-10 16:06:12 -08001349
Colin Cross33961b52019-07-11 11:01:22 -07001350 // Create rule for metalava
Nan Zhanga40da042018-08-01 12:48:00 -07001351
Colin Cross33961b52019-07-11 11:01:22 -07001352 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang71bbe632018-09-17 14:32:21 -07001353
Colin Cross33961b52019-07-11 11:01:22 -07001354 rule := android.NewRuleBuilder()
Nan Zhanga40da042018-08-01 12:48:00 -07001355
Paul Duffin3ae29512020-04-08 18:18:03 +01001356 generateStubs := BoolDefault(d.properties.Generate_stubs, true)
1357 var stubsDir android.OptionalPath
1358 if generateStubs {
1359 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1360 stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "stubsDir"))
1361 rule.Command().Text("rm -rf").Text(stubsDir.String())
1362 rule.Command().Text("mkdir -p").Text(stubsDir.String())
1363 }
Nan Zhanga40da042018-08-01 12:48:00 -07001364
Colin Cross33961b52019-07-11 11:01:22 -07001365 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1366
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001367 implicitsRsp := android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"implicits.rsp")
1368
Colin Cross33961b52019-07-11 11:01:22 -07001369 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001370 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths, implicitsRsp,
1371 Bool(d.Javadoc.properties.Sandbox))
1372 cmd.Implicits(d.Javadoc.implicits)
Colin Cross33961b52019-07-11 11:01:22 -07001373
1374 d.stubsFlags(ctx, cmd, stubsDir)
1375
1376 d.annotationsFlags(ctx, cmd)
1377 d.inclusionAnnotationsFlags(ctx, cmd)
1378 d.apiLevelsAnnotationsFlags(ctx, cmd)
1379 d.apiToXmlFlags(ctx, cmd)
Nan Zhang71bbe632018-09-17 14:32:21 -07001380
Liz Kammer585cac22020-07-06 09:12:57 -07001381 if android.InList("--generate-documentation", d.Javadoc.args) {
Nan Zhang1598a9e2018-09-04 17:14:32 -07001382 // Currently Metalava have the ability to invoke Javadoc in a seperate process.
1383 // Pass "-nodocs" to suppress the Javadoc invocation when Metalava receives
1384 // "--generate-documentation" arg. This is not needed when Metalava removes this feature.
Liz Kammer585cac22020-07-06 09:12:57 -07001385 d.Javadoc.args = append(d.Javadoc.args, "-nodocs")
Nan Zhang79614d12018-04-19 18:03:39 -07001386 }
Colin Cross33961b52019-07-11 11:01:22 -07001387
Liz Kammer585cac22020-07-06 09:12:57 -07001388 cmd.Flag(strings.Join(d.Javadoc.args, " ")).Implicits(d.Javadoc.argFiles)
Colin Cross33961b52019-07-11 11:01:22 -07001389 for _, o := range d.Javadoc.properties.Out {
1390 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
1391 }
1392
Makoto Onuki88b99052020-04-27 17:22:16 -07001393 // Add options for the other optional tasks: API-lint and check-released.
1394 // We generate separate timestamp files for them.
1395
1396 doApiLint := false
1397 doCheckReleased := false
1398
1399 // Add API lint options.
1400
Dan Willemsen9f435972020-05-28 15:28:00 -07001401 if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) {
Makoto Onuki88b99052020-04-27 17:22:16 -07001402 doApiLint = true
1403
1404 newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since)
1405 if newSince.Valid() {
1406 cmd.FlagWithInput("--api-lint ", newSince.Path())
1407 } else {
1408 cmd.Flag("--api-lint")
1409 }
1410 d.apiLintReport = android.PathForModuleOut(ctx, "api_lint_report.txt")
1411 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport) // TODO: Change to ":api-lint"
1412
Makoto Onuki194c43f2020-04-27 17:22:16 -07001413 // TODO(b/154317059): Clean up this whitelist by baselining and/or checking in last-released.
1414 if d.Name() != "android.car-system-stubs-docs" &&
Anton Hanssonb30f5932020-09-16 13:14:18 +01001415 d.Name() != "android.car-stubs-docs" {
Makoto Onuki194c43f2020-04-27 17:22:16 -07001416 cmd.Flag("--lints-as-errors")
1417 cmd.Flag("--warnings-as-errors") // Most lints are actually warnings.
1418 }
1419
Makoto Onuki88b99052020-04-27 17:22:16 -07001420 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
1421 updatedBaselineOutput := android.PathForModuleOut(ctx, "api_lint_baseline.txt")
1422 d.apiLintTimestamp = android.PathForModuleOut(ctx, "api_lint.timestamp")
1423
1424 // Note this string includes a special shell quote $' ... ', which decodes the "\n"s.
1425 // However, because $' ... ' doesn't expand environmental variables, we can't just embed
1426 // $PWD, so we have to terminate $'...', use "$PWD", then start $' ... ' again,
1427 // which is why we have '"$PWD"$' in it.
1428 //
1429 // TODO: metalava also has a slightly different message hardcoded. Should we unify this
1430 // message and metalava's one?
1431 msg := `$'` + // Enclose with $' ... '
1432 `************************************************************\n` +
1433 `Your API changes are triggering API Lint warnings or errors.\n` +
1434 `To make these errors go away, fix the code according to the\n` +
1435 `error and/or warning messages above.\n` +
1436 `\n` +
1437 `If it is not possible to do so, there are workarounds:\n` +
1438 `\n` +
1439 `1. You can suppress the errors with @SuppressLint("<id>")\n`
1440
1441 if baselineFile.Valid() {
1442 cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path())
1443 cmd.FlagWithOutput("--update-baseline:api-lint ", updatedBaselineOutput)
1444
1445 msg += fmt.Sprintf(``+
1446 `2. You can update the baseline by executing the following\n`+
1447 ` command:\n`+
Anton Hansson3361a292020-05-11 15:38:31 +01001448 ` cp \\\n`+
1449 ` "'"$PWD"$'/%s" \\\n`+
1450 ` "'"$PWD"$'/%s"\n`+
Makoto Onuki88b99052020-04-27 17:22:16 -07001451 ` To submit the revised baseline.txt to the main Android\n`+
1452 ` repository, you will need approval.\n`, updatedBaselineOutput, baselineFile.Path())
1453 } else {
1454 msg += fmt.Sprintf(``+
1455 `2. You can add a baseline file of existing lint failures\n`+
1456 ` to the build rule of %s.\n`, d.Name())
1457 }
1458 // Note the message ends with a ' (single quote), to close the $' ... ' .
1459 msg += `************************************************************\n'`
1460
1461 cmd.FlagWithArg("--error-message:api-lint ", msg)
1462 }
1463
1464 // Add "check released" options. (Detect incompatible API changes from the last public release)
1465
Dan Willemsen9f435972020-05-28 15:28:00 -07001466 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") {
Makoto Onuki88b99052020-04-27 17:22:16 -07001467 doCheckReleased = true
1468
1469 if len(d.Javadoc.properties.Out) > 0 {
1470 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1471 }
1472
1473 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
1474 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
1475 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
1476 updatedBaselineOutput := android.PathForModuleOut(ctx, "last_released_baseline.txt")
1477
1478 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
1479
1480 cmd.FlagWithInput("--check-compatibility:api:released ", apiFile)
1481 cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile)
1482
1483 if baselineFile.Valid() {
1484 cmd.FlagWithInput("--baseline:compatibility:released ", baselineFile.Path())
1485 cmd.FlagWithOutput("--update-baseline:compatibility:released ", updatedBaselineOutput)
1486 }
1487
1488 // Note this string includes quote ($' ... '), which decodes the "\n"s.
1489 msg := `$'\n******************************\n` +
1490 `You have tried to change the API from what has been previously released in\n` +
1491 `an SDK. Please fix the errors listed above.\n` +
1492 `******************************\n'`
1493
1494 cmd.FlagWithArg("--error-message:compatibility:released ", msg)
1495 }
1496
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001497 impRule := android.NewRuleBuilder()
1498 impCmd := impRule.Command()
Liz Kammer20ebfb42020-07-28 11:32:07 -07001499 // An action that copies the ninja generated rsp file to a new location. This allows us to
Ramy Medhat2f99eec2020-06-13 17:38:27 -04001500 // add a large number of inputs to a file without exceeding bash command length limits (which
1501 // would happen if we use the WriteFile rule). The cp is needed because RuleBuilder sets the
1502 // rsp file to be ${output}.rsp.
1503 impCmd.Text("cp").FlagWithRspFileInputList("", cmd.GetImplicits()).Output(implicitsRsp)
1504 impRule.Build(pctx, ctx, "implicitsGen", "implicits generation")
1505 cmd.Implicit(implicitsRsp)
1506
Paul Duffin3ae29512020-04-08 18:18:03 +01001507 if generateStubs {
1508 rule.Command().
1509 BuiltTool(ctx, "soong_zip").
1510 Flag("-write_if_changed").
1511 Flag("-jar").
1512 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
1513 FlagWithArg("-C ", stubsDir.String()).
1514 FlagWithArg("-D ", stubsDir.String())
1515 }
Jerome Gaillard0f599032019-10-10 19:29:11 +01001516
1517 if Bool(d.properties.Write_sdk_values) {
1518 d.metadataZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-metadata.zip")
1519 rule.Command().
1520 BuiltTool(ctx, "soong_zip").
1521 Flag("-write_if_changed").
1522 Flag("-d").
1523 FlagWithOutput("-o ", d.metadataZip).
1524 FlagWithArg("-C ", d.metadataDir.String()).
1525 FlagWithArg("-D ", d.metadataDir.String())
1526 }
1527
Makoto Onuki88b99052020-04-27 17:22:16 -07001528 // TODO: We don't really need two separate API files, but this is a reminiscence of how
1529 // we used to run metalava separately for API lint and the "last_released" check. Unify them.
1530 if doApiLint {
1531 rule.Command().Text("touch").Output(d.apiLintTimestamp)
1532 }
1533 if doCheckReleased {
1534 rule.Command().Text("touch").Output(d.checkLastReleasedApiTimestamp)
1535 }
1536
Colin Cross33961b52019-07-11 11:01:22 -07001537 rule.Restat()
1538
1539 zipSyncCleanupCmd(rule, srcJarDir)
1540
Makoto Onuki88b99052020-04-27 17:22:16 -07001541 rule.Build(pctx, ctx, "metalava", "metalava merged")
Adrian Roos075eedc2019-10-10 12:07:03 +02001542
Dan Willemsen9f435972020-05-28 15:28:00 -07001543 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") {
Colin Cross33961b52019-07-11 11:01:22 -07001544
1545 if len(d.Javadoc.properties.Out) > 0 {
1546 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1547 }
1548
1549 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1550 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
Adrian Roos14f75a92019-08-12 17:54:09 +02001551 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
Makoto Onuki5405a732020-04-16 17:02:40 -07001552
1553 if baselineFile.Valid() {
Makoto Onuki88b99052020-04-27 17:22:16 -07001554 ctx.PropertyErrorf("baseline_file", "current API check can't have a baseline file. (module %s)", ctx.ModuleName())
Makoto Onuki5405a732020-04-16 17:02:40 -07001555 }
Nan Zhang61819ce2018-05-04 18:49:16 -07001556
Nan Zhang2760dfc2018-08-24 17:32:54 +00001557 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
Nan Zhang2760dfc2018-08-24 17:32:54 +00001558
Colin Cross33961b52019-07-11 11:01:22 -07001559 rule := android.NewRuleBuilder()
1560
Makoto Onuki5405a732020-04-16 17:02:40 -07001561 // Diff command line.
Makoto Onuki88b99052020-04-27 17:22:16 -07001562 // -F matches the closest "opening" line, such as "package android {"
1563 // and " public class Intent {".
Makoto Onuki5405a732020-04-16 17:02:40 -07001564 diff := `diff -u -F '{ *$'`
1565
Colin Cross33961b52019-07-11 11:01:22 -07001566 rule.Command().Text("( true")
Makoto Onuki5405a732020-04-16 17:02:40 -07001567 rule.Command().
1568 Text(diff).
1569 Input(apiFile).Input(d.apiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001570
Makoto Onuki5405a732020-04-16 17:02:40 -07001571 rule.Command().
1572 Text(diff).
1573 Input(removedApiFile).Input(d.removedApiFile)
Colin Cross33961b52019-07-11 11:01:22 -07001574
1575 msg := fmt.Sprintf(`\n******************************\n`+
1576 `You have tried to change the API from what has been previously approved.\n\n`+
1577 `To make these errors go away, you have two choices:\n`+
Makoto Onuki5405a732020-04-16 17:02:40 -07001578 ` 1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)\n`+
1579 ` to the new methods, etc. shown in the above diff.\n\n`+
1580 ` 2. You can update current.txt and/or removed.txt by executing the following command:\n`+
Colin Cross33961b52019-07-11 11:01:22 -07001581 ` make %s-update-current-api\n\n`+
1582 ` To submit the revised current.txt to the main Android repository,\n`+
1583 ` you will need approval.\n`+
1584 `******************************\n`, ctx.ModuleName())
1585
1586 rule.Command().
1587 Text("touch").Output(d.checkCurrentApiTimestamp).
1588 Text(") || (").
1589 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1590 Text("; exit 38").
1591 Text(")")
1592
Makoto Onuki5405a732020-04-16 17:02:40 -07001593 rule.Build(pctx, ctx, "metalavaCurrentApiCheck", "check current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001594
1595 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001596
1597 // update API rule
1598 rule = android.NewRuleBuilder()
1599
1600 rule.Command().Text("( true")
1601
1602 rule.Command().
1603 Text("cp").Flag("-f").
1604 Input(d.apiFile).Flag(apiFile.String())
1605
1606 rule.Command().
1607 Text("cp").Flag("-f").
1608 Input(d.removedApiFile).Flag(removedApiFile.String())
1609
1610 msg = "failed to update public API"
1611
1612 rule.Command().
1613 Text("touch").Output(d.updateCurrentApiTimestamp).
1614 Text(") || (").
1615 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1616 Text("; exit 38").
1617 Text(")")
1618
1619 rule.Build(pctx, ctx, "metalavaCurrentApiUpdate", "update current API")
Nan Zhang61819ce2018-05-04 18:49:16 -07001620 }
Nan Zhanga40da042018-08-01 12:48:00 -07001621
Pete Gillin581d6082018-10-22 15:55:04 +01001622 if String(d.properties.Check_nullability_warnings) != "" {
1623 if d.nullabilityWarningsFile == nil {
1624 ctx.PropertyErrorf("check_nullability_warnings",
1625 "Cannot specify check_nullability_warnings unless validating nullability")
1626 }
Colin Cross33961b52019-07-11 11:01:22 -07001627
1628 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
1629
Pete Gillin581d6082018-10-22 15:55:04 +01001630 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "check_nullability_warnings.timestamp")
Colin Cross33961b52019-07-11 11:01:22 -07001631
Pete Gillin581d6082018-10-22 15:55:04 +01001632 msg := fmt.Sprintf(`\n******************************\n`+
1633 `The warnings encountered during nullability annotation validation did\n`+
1634 `not match the checked in file of expected warnings. The diffs are shown\n`+
1635 `above. You have two options:\n`+
1636 ` 1. Resolve the differences by editing the nullability annotations.\n`+
1637 ` 2. Update the file of expected warnings by running:\n`+
1638 ` cp %s %s\n`+
1639 ` and submitting the updated file as part of your change.`,
1640 d.nullabilityWarningsFile, checkNullabilityWarnings)
Colin Cross33961b52019-07-11 11:01:22 -07001641
1642 rule := android.NewRuleBuilder()
1643
1644 rule.Command().
1645 Text("(").
1646 Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile).
1647 Text("&&").
1648 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
1649 Text(") || (").
1650 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1651 Text("; exit 38").
1652 Text(")")
1653
1654 rule.Build(pctx, ctx, "nullabilityWarningsCheck", "nullability warnings check")
Pete Gillin581d6082018-10-22 15:55:04 +01001655 }
1656
Dan Willemsen9f435972020-05-28 15:28:00 -07001657 if Bool(d.properties.Jdiff_enabled) {
Colin Cross33961b52019-07-11 11:01:22 -07001658 if len(d.Javadoc.properties.Out) > 0 {
1659 ctx.PropertyErrorf("out", "out property may not be combined with jdiff")
1660 }
1661
1662 outDir := android.PathForModuleOut(ctx, "jdiff-out")
1663 srcJarDir := android.PathForModuleOut(ctx, "jdiff-srcjars")
1664 stubsDir := android.PathForModuleOut(ctx, "jdiff-stubsDir")
1665
1666 rule := android.NewRuleBuilder()
Nan Zhang71bbe632018-09-17 14:32:21 -07001667
Nan Zhang86b06202018-09-21 17:09:21 -07001668 // Please sync with android-api-council@ before making any changes for the name of jdiffDocZip below
1669 // since there's cron job downstream that fetch this .zip file periodically.
1670 // See b/116221385 for reference.
Nan Zhang71bbe632018-09-17 14:32:21 -07001671 d.jdiffDocZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-docs.zip")
1672 d.jdiffStubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-stubs.srcjar")
1673
Nan Zhang71bbe632018-09-17 14:32:21 -07001674 jdiff := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jdiff.jar")
Nan Zhang71bbe632018-09-17 14:32:21 -07001675
Colin Cross33961b52019-07-11 11:01:22 -07001676 rule.Command().Text("rm -rf").Text(outDir.String()).Text(stubsDir.String())
1677 rule.Command().Text("mkdir -p").Text(outDir.String()).Text(stubsDir.String())
Nan Zhang71bbe632018-09-17 14:32:21 -07001678
Colin Cross33961b52019-07-11 11:01:22 -07001679 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
1680
Colin Crossdaa4c672019-07-15 22:53:46 -07001681 cmd := javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -07001682 deps.bootClasspath, deps.classpath, d.sourcepaths)
1683
1684 cmd.Flag("-J-Xmx1600m").
Colin Cross33961b52019-07-11 11:01:22 -07001685 Flag("-XDignore.symbol.file").
1686 FlagWithArg("-doclet ", "jdiff.JDiff").
1687 FlagWithInput("-docletpath ", jdiff).
Paul Duffin10269f12020-06-19 18:39:55 +01001688 Flag("-quiet")
1689
1690 if d.apiXmlFile != nil {
1691 cmd.FlagWithArg("-newapi ", strings.TrimSuffix(d.apiXmlFile.Base(), d.apiXmlFile.Ext())).
1692 FlagWithArg("-newapidir ", filepath.Dir(d.apiXmlFile.String())).
1693 Implicit(d.apiXmlFile)
1694 }
1695
1696 if d.lastReleasedApiXmlFile != nil {
1697 cmd.FlagWithArg("-oldapi ", strings.TrimSuffix(d.lastReleasedApiXmlFile.Base(), d.lastReleasedApiXmlFile.Ext())).
1698 FlagWithArg("-oldapidir ", filepath.Dir(d.lastReleasedApiXmlFile.String())).
1699 Implicit(d.lastReleasedApiXmlFile)
1700 }
Colin Cross33961b52019-07-11 11:01:22 -07001701
Colin Cross33961b52019-07-11 11:01:22 -07001702 rule.Command().
1703 BuiltTool(ctx, "soong_zip").
1704 Flag("-write_if_changed").
1705 Flag("-d").
1706 FlagWithOutput("-o ", d.jdiffDocZip).
1707 FlagWithArg("-C ", outDir.String()).
1708 FlagWithArg("-D ", outDir.String())
1709
1710 rule.Command().
1711 BuiltTool(ctx, "soong_zip").
1712 Flag("-write_if_changed").
1713 Flag("-jar").
1714 FlagWithOutput("-o ", d.jdiffStubsSrcJar).
1715 FlagWithArg("-C ", stubsDir.String()).
1716 FlagWithArg("-D ", stubsDir.String())
1717
1718 rule.Restat()
1719
1720 zipSyncCleanupCmd(rule, srcJarDir)
1721
1722 rule.Build(pctx, ctx, "jdiff", "jdiff")
Nan Zhang71bbe632018-09-17 14:32:21 -07001723 }
Nan Zhang581fd212018-01-10 16:06:12 -08001724}
Dan Willemsencc090972018-02-26 14:33:31 -08001725
Nan Zhanga40da042018-08-01 12:48:00 -07001726//
Nan Zhangf4936b02018-08-01 15:00:28 -07001727// Exported Droiddoc Directory
Nan Zhanga40da042018-08-01 12:48:00 -07001728//
Dan Willemsencc090972018-02-26 14:33:31 -08001729var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
Nan Zhangf4936b02018-08-01 15:00:28 -07001730var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
Pete Gillin77167902018-09-19 18:16:26 +01001731var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
Nan Zhang9c69a122018-08-22 10:22:08 -07001732var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
Dan Willemsencc090972018-02-26 14:33:31 -08001733
Nan Zhangf4936b02018-08-01 15:00:28 -07001734type ExportedDroiddocDirProperties struct {
1735 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -08001736 Path *string
1737}
1738
Nan Zhangf4936b02018-08-01 15:00:28 -07001739type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -08001740 android.ModuleBase
1741
Nan Zhangf4936b02018-08-01 15:00:28 -07001742 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -08001743
1744 deps android.Paths
1745 dir android.Path
1746}
1747
Colin Crossa3002fc2019-07-08 16:48:04 -07001748// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -07001749func ExportedDroiddocDirFactory() android.Module {
1750 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -08001751 module.AddProperties(&module.properties)
1752 android.InitAndroidModule(module)
1753 return module
1754}
1755
Nan Zhangf4936b02018-08-01 15:00:28 -07001756func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -08001757
Nan Zhangf4936b02018-08-01 15:00:28 -07001758func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -08001759 path := String(d.properties.Path)
1760 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -08001761 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Dan Willemsencc090972018-02-26 14:33:31 -08001762}
Nan Zhangb2b33de2018-02-23 11:18:47 -08001763
1764//
1765// Defaults
1766//
1767type DocDefaults struct {
1768 android.ModuleBase
1769 android.DefaultsModuleBase
1770}
1771
Nan Zhangb2b33de2018-02-23 11:18:47 -08001772func DocDefaultsFactory() android.Module {
1773 module := &DocDefaults{}
1774
1775 module.AddProperties(
1776 &JavadocProperties{},
1777 &DroiddocProperties{},
1778 )
1779
1780 android.InitDefaultsModule(module)
1781
1782 return module
1783}
Nan Zhang1598a9e2018-09-04 17:14:32 -07001784
1785func StubsDefaultsFactory() android.Module {
1786 module := &DocDefaults{}
1787
1788 module.AddProperties(
1789 &JavadocProperties{},
1790 &DroidstubsProperties{},
1791 )
1792
1793 android.InitDefaultsModule(module)
1794
1795 return module
1796}
Colin Cross33961b52019-07-11 11:01:22 -07001797
1798func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
1799 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
1800
1801 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1802 rule.Command().Text("mkdir -p").Text(srcJarDir.String())
1803 srcJarList := srcJarDir.Join(ctx, "list")
1804
1805 rule.Temporary(srcJarList)
1806
1807 rule.Command().BuiltTool(ctx, "zipsync").
1808 FlagWithArg("-d ", srcJarDir.String()).
1809 FlagWithOutput("-l ", srcJarList).
1810 FlagWithArg("-f ", `"*.java"`).
1811 Inputs(srcJars)
1812
1813 return srcJarList
1814}
1815
1816func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
1817 rule.Command().Text("rm -rf").Text(srcJarDir.String())
1818}
Paul Duffin91547182019-11-12 19:39:36 +00001819
1820var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
1821
1822type PrebuiltStubsSourcesProperties struct {
1823 Srcs []string `android:"path"`
1824}
1825
1826type PrebuiltStubsSources struct {
1827 android.ModuleBase
1828 android.DefaultableModuleBase
1829 prebuilt android.Prebuilt
1830 android.SdkBase
1831
1832 properties PrebuiltStubsSourcesProperties
1833
Paul Duffin9b478b02019-12-10 13:41:51 +00001834 // The source directories containing stubs source files.
1835 srcDirs android.Paths
Paul Duffin91547182019-11-12 19:39:36 +00001836 stubsSrcJar android.ModuleOutPath
1837}
1838
Paul Duffin9b478b02019-12-10 13:41:51 +00001839func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) {
1840 switch tag {
1841 case "":
1842 return android.Paths{p.stubsSrcJar}, nil
1843 default:
1844 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1845 }
1846}
1847
Paul Duffin0f8faff2020-05-20 16:18:00 +01001848func (d *PrebuiltStubsSources) StubsSrcJar() android.Path {
1849 return d.stubsSrcJar
1850}
1851
Paul Duffin91547182019-11-12 19:39:36 +00001852func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin9b478b02019-12-10 13:41:51 +00001853 p.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
1854
1855 p.srcDirs = android.PathsForModuleSrc(ctx, p.properties.Srcs)
1856
1857 rule := android.NewRuleBuilder()
1858 command := rule.Command().
1859 BuiltTool(ctx, "soong_zip").
1860 Flag("-write_if_changed").
1861 Flag("-jar").
1862 FlagWithOutput("-o ", p.stubsSrcJar)
1863
1864 for _, d := range p.srcDirs {
1865 dir := d.String()
1866 command.
1867 FlagWithArg("-C ", dir).
1868 FlagWithInput("-D ", d)
1869 }
1870
1871 rule.Restat()
1872
1873 rule.Build(pctx, ctx, "zip src", "Create srcjar from prebuilt source")
Paul Duffin91547182019-11-12 19:39:36 +00001874}
1875
1876func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
1877 return &p.prebuilt
1878}
1879
1880func (p *PrebuiltStubsSources) Name() string {
1881 return p.prebuilt.Name(p.ModuleBase.Name())
1882}
1883
Paul Duffin91547182019-11-12 19:39:36 +00001884// prebuilt_stubs_sources imports a set of java source files as if they were
1885// generated by droidstubs.
1886//
1887// By default, a prebuilt_stubs_sources has a single variant that expects a
1888// set of `.java` files generated by droidstubs.
1889//
1890// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1891// for host modules.
1892//
1893// Intended only for use by sdk snapshots.
1894func PrebuiltStubsSourcesFactory() android.Module {
1895 module := &PrebuiltStubsSources{}
1896
1897 module.AddProperties(&module.properties)
1898
1899 android.InitPrebuiltModule(module, &module.properties.Srcs)
1900 android.InitSdkAwareModule(module)
1901 InitDroiddocModule(module, android.HostAndDeviceSupported)
1902 return module
1903}
1904
Paul Duffin13879572019-11-28 14:31:38 +00001905type droidStubsSdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001906 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001907}
1908
1909func (mt *droidStubsSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1910 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1911}
1912
1913func (mt *droidStubsSdkMemberType) IsInstance(module android.Module) bool {
1914 _, ok := module.(*Droidstubs)
1915 return ok
1916}
1917
Paul Duffin495ffb92020-03-20 13:35:40 +00001918func (mt *droidStubsSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1919 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_stubs_sources")
1920}
1921
1922func (mt *droidStubsSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1923 return &droidStubsInfoProperties{}
1924}
1925
1926type droidStubsInfoProperties struct {
1927 android.SdkMemberPropertiesBase
1928
1929 StubsSrcJar android.Path
1930}
1931
1932func (p *droidStubsInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1933 droidstubs := variant.(*Droidstubs)
1934 p.StubsSrcJar = droidstubs.stubsSrcJar
1935}
1936
1937func (p *droidStubsInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
1938 if p.StubsSrcJar != nil {
1939 builder := ctx.SnapshotBuilder()
1940
1941 snapshotRelativeDir := filepath.Join("java", ctx.Name()+"_stubs_sources")
1942
1943 builder.UnzipToSnapshot(p.StubsSrcJar, snapshotRelativeDir)
1944
1945 propertySet.AddProperty("srcs", []string{snapshotRelativeDir})
Paul Duffin13879572019-11-28 14:31:38 +00001946 }
Paul Duffin91547182019-11-12 19:39:36 +00001947}