blob: 7a610344f358b1a0fc4273357ac5ea588e2bdc86 [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
Jeongik Cha6bd33c12019-06-25 16:26:18 +090022 "github.com/google/blueprint/proptools"
Nan Zhang581fd212018-01-10 16:06:12 -080023
Colin Crossab054432019-07-15 16:13:59 -070024 "android/soong/android"
25 "android/soong/java/config"
Nan Zhang581fd212018-01-10 16:06:12 -080026)
27
28func init() {
Paul Duffin884363e2019-12-19 10:21:09 +000029 RegisterDocsBuildComponents(android.InitRegistrationContext)
Nan Zhang581fd212018-01-10 16:06:12 -080030}
31
Paul Duffin884363e2019-12-19 10:21:09 +000032func RegisterDocsBuildComponents(ctx android.RegistrationContext) {
33 ctx.RegisterModuleType("doc_defaults", DocDefaultsFactory)
34
35 ctx.RegisterModuleType("droiddoc", DroiddocFactory)
36 ctx.RegisterModuleType("droiddoc_host", DroiddocHostFactory)
37 ctx.RegisterModuleType("droiddoc_exported_dir", ExportedDroiddocDirFactory)
38 ctx.RegisterModuleType("javadoc", JavadocFactory)
39 ctx.RegisterModuleType("javadoc_host", JavadocHostFactory)
40}
41
Nan Zhang581fd212018-01-10 16:06:12 -080042type JavadocProperties struct {
43 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
44 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080045 Srcs []string `android:"path,arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080046
Nan Zhang581fd212018-01-10 16:06:12 -080047 // list of source files that should not be used to build the Java module.
48 // This is most useful in the arch/multilib variants to remove non-common files
49 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -080050 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080051
Jiyong Parkc6ddccf2019-09-13 20:56:14 +090052 // list of package names that should actually be used. If this property is left unspecified,
53 // all the sources from the srcs property is used.
54 Filter_packages []string
55
Nan Zhangb2b33de2018-02-23 11:18:47 -080056 // list of java libraries that will be in the classpath.
Nan Zhang581fd212018-01-10 16:06:12 -080057 Libs []string `android:"arch_variant"`
58
59 // If set to false, don't allow this module(-docs.zip) to be exported. Defaults to true.
Nan Zhangb2b33de2018-02-23 11:18:47 -080060 Installable *bool
Nan Zhang581fd212018-01-10 16:06:12 -080061
Paul Duffine25c6442019-10-11 13:50:28 +010062 // if not blank, set to the version of the sdk to compile against.
63 // Defaults to compiling against the current platform.
Nan Zhang581fd212018-01-10 16:06:12 -080064 Sdk_version *string `android:"arch_variant"`
Jiyong Park1e440682018-05-23 18:42:04 +090065
Paul Duffine25c6442019-10-11 13:50:28 +010066 // When targeting 1.9 and above, override the modules to use with --system,
67 // otherwise provides defaults libraries to add to the bootclasspath.
68 // Defaults to "none"
69 System_modules *string
70
Jiyong Park1e440682018-05-23 18:42:04 +090071 Aidl struct {
72 // Top level directories to pass to aidl tool
73 Include_dirs []string
74
75 // Directories rooted at the Android.bp file to pass to aidl tool
76 Local_include_dirs []string
77 }
Nan Zhang357466b2018-04-17 17:38:36 -070078
79 // If not blank, set the java version passed to javadoc as -source
80 Java_version *string
Nan Zhang1598a9e2018-09-04 17:14:32 -070081
82 // local files that are used within user customized droiddoc options.
Colin Cross27b922f2019-03-04 22:35:41 -080083 Arg_files []string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -070084
Liz Kammer585cac22020-07-06 09:12:57 -070085 // user customized droiddoc args. Deprecated, use flags instead.
Nan Zhang1598a9e2018-09-04 17:14:32 -070086 // Available variables for substitution:
87 //
88 // $(location <label>): the path to the arg_files with name <label>
Colin Crosse4a05842019-05-28 10:17:14 -070089 // $$: a literal $
Nan Zhang1598a9e2018-09-04 17:14:32 -070090 Args *string
91
Liz Kammer585cac22020-07-06 09:12:57 -070092 // user customized droiddoc args. Not compatible with property args.
93 // Available variables for substitution:
94 //
95 // $(location <label>): the path to the arg_files with name <label>
96 // $$: a literal $
97 Flags []string
98
Nan Zhang1598a9e2018-09-04 17:14:32 -070099 // names of the output files used in args that will be generated
100 Out []string
Nan Zhang581fd212018-01-10 16:06:12 -0800101}
102
Nan Zhang61819ce2018-05-04 18:49:16 -0700103type ApiToCheck struct {
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900104 // path to the API txt file that the new API extracted from source code is checked
105 // against. The path can be local to the module or from other module (via :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800106 Api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700107
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900108 // path to the API txt file that the new @removed API extractd from source code is
109 // checked against. The path can be local to the module or from other module (via
110 // :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800111 Removed_api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700112
Adrian Roos14f75a92019-08-12 17:54:09 +0200113 // If not blank, path to the baseline txt file for approved API check violations.
114 Baseline_file *string `android:"path"`
115
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900116 // Arguments to the apicheck tool.
Nan Zhang61819ce2018-05-04 18:49:16 -0700117 Args *string
118}
119
Nan Zhang581fd212018-01-10 16:06:12 -0800120type DroiddocProperties struct {
121 // directory relative to top of the source tree that contains doc templates files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800122 Custom_template *string
Nan Zhang581fd212018-01-10 16:06:12 -0800123
Nan Zhanga40da042018-08-01 12:48:00 -0700124 // directories under current module source which contains html/jd files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800125 Html_dirs []string
Nan Zhang581fd212018-01-10 16:06:12 -0800126
127 // set a value in the Clearsilver hdf namespace.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800128 Hdf []string
Nan Zhang581fd212018-01-10 16:06:12 -0800129
130 // proofread file contains all of the text content of the javadocs concatenated into one file,
131 // suitable for spell-checking and other goodness.
Colin Crossab054432019-07-15 16:13:59 -0700132 Proofread_file *string
Nan Zhang581fd212018-01-10 16:06:12 -0800133
134 // a todo file lists the program elements that are missing documentation.
135 // At some point, this might be improved to show more warnings.
Colin Cross27b922f2019-03-04 22:35:41 -0800136 Todo_file *string `android:"path"`
Nan Zhangb2b33de2018-02-23 11:18:47 -0800137
Anton Hanssonb06bb572023-10-03 12:11:35 +0000138 // A file containing a baseline for allowed lint errors.
139 Lint_baseline *string `android:"path"`
140
Nan Zhangb2b33de2018-02-23 11:18:47 -0800141 // directory under current module source that provide additional resources (images).
142 Resourcesdir *string
143
144 // resources output directory under out/soong/.intermediates.
145 Resourcesoutdir *string
Nan Zhang581fd212018-01-10 16:06:12 -0800146
Nan Zhange2ba5d42018-07-11 15:16:55 -0700147 // index.html under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800148 Static_doc_index_redirect *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700149
150 // source.properties under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800151 Static_doc_properties *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700152
Nan Zhang581fd212018-01-10 16:06:12 -0800153 // a list of files under current module source dir which contains known tags in Java sources.
154 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -0800155 Knowntags []string `android:"path"`
Nan Zhang28c68b92018-03-13 16:17:01 -0700156
Nan Zhang1598a9e2018-09-04 17:14:32 -0700157 // if set to true, generate docs through Dokka instead of Doclava.
158 Dokka_enabled *bool
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000159
160 // Compat config XML. Generates compat change documentation if set.
161 Compat_config *string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -0700162}
163
Nan Zhanga40da042018-08-01 12:48:00 -0700164// Common flags passed down to build rule
Nan Zhanga40da042018-08-01 12:48:00 -0700165type droiddocBuilderFlags struct {
Nan Zhang86d2d552018-08-09 15:33:27 -0700166 bootClasspathArgs string
167 classpathArgs string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700168 sourcepathArgs string
Nan Zhang86d2d552018-08-09 15:33:27 -0700169 dokkaClasspathArgs string
170 aidlFlags string
Colin Cross3047fa22019-04-18 10:56:44 -0700171 aidlDeps android.Paths
Nan Zhanga40da042018-08-01 12:48:00 -0700172
Nan Zhanga40da042018-08-01 12:48:00 -0700173 doclavaStubsFlags string
Nan Zhang86d2d552018-08-09 15:33:27 -0700174 doclavaDocsFlags string
Nan Zhanga40da042018-08-01 12:48:00 -0700175 postDoclavaCmds string
Nan Zhanga40da042018-08-01 12:48:00 -0700176}
177
178func InitDroiddocModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
179 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
180 android.InitDefaultableModule(module)
181}
182
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200183func apiCheckEnabled(ctx android.ModuleContext, apiToCheck ApiToCheck, apiVersionTag string) bool {
184 if ctx.Config().IsEnvTrue("WITHOUT_CHECK_API") {
185 return false
186 } else if String(apiToCheck.Api_file) != "" && String(apiToCheck.Removed_api_file) != "" {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700187 return true
188 } else if String(apiToCheck.Api_file) != "" {
189 panic("for " + apiVersionTag + " removed_api_file has to be non-empty!")
190 } else if String(apiToCheck.Removed_api_file) != "" {
191 panic("for " + apiVersionTag + " api_file has to be non-empty!")
192 }
193
194 return false
195}
196
Nan Zhanga40da042018-08-01 12:48:00 -0700197// Javadoc
Nan Zhang581fd212018-01-10 16:06:12 -0800198type Javadoc struct {
199 android.ModuleBase
200 android.DefaultableModuleBase
201
202 properties JavadocProperties
203
204 srcJars android.Paths
205 srcFiles android.Paths
206 sourcepaths android.Paths
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400207 implicits android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700208
Nan Zhangccff0f72018-03-08 17:26:16 -0800209 docZip android.WritablePath
210 stubsSrcJar android.WritablePath
Jihoon Kang3c89f042023-12-19 02:40:22 +0000211
212 exportableStubsSrcJar android.WritablePath
Nan Zhang581fd212018-01-10 16:06:12 -0800213}
214
Colin Cross41955e82019-05-29 14:40:35 -0700215func (j *Javadoc) OutputFiles(tag string) (android.Paths, error) {
216 switch tag {
217 case "":
218 return android.Paths{j.stubsSrcJar}, nil
Colin Crosse68e5542019-08-12 13:11:40 -0700219 case ".docs.zip":
220 return android.Paths{j.docZip}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700221 default:
222 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
223 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800224}
225
Colin Crossa3002fc2019-07-08 16:48:04 -0700226// javadoc converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800227func JavadocFactory() android.Module {
228 module := &Javadoc{}
229
230 module.AddProperties(&module.properties)
231
232 InitDroiddocModule(module, android.HostAndDeviceSupported)
233 return module
234}
235
Colin Crossa3002fc2019-07-08 16:48:04 -0700236// javadoc_host converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800237func JavadocHostFactory() android.Module {
238 module := &Javadoc{}
239
240 module.AddProperties(&module.properties)
241
242 InitDroiddocModule(module, android.HostSupported)
243 return module
244}
245
Colin Cross41955e82019-05-29 14:40:35 -0700246var _ android.OutputFileProducer = (*Javadoc)(nil)
Nan Zhang581fd212018-01-10 16:06:12 -0800247
Jiyong Park92315372021-04-02 08:45:46 +0900248func (j *Javadoc) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
249 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700250}
251
Jiyong Parkf1691d22021-03-29 20:11:58 +0900252func (j *Javadoc) SystemModules() string {
Paul Duffine25c6442019-10-11 13:50:28 +0100253 return proptools.String(j.properties.System_modules)
254}
255
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000256func (j *Javadoc) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
257 return j.SdkVersion(ctx).ApiLevel
Colin Cross83bb3162018-06-25 15:48:06 -0700258}
259
Spandan Dasa26eda72023-03-02 00:56:06 +0000260func (j *Javadoc) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
261 return j.SdkVersion(ctx).ApiLevel
William Loh5a082f92022-05-17 20:21:50 +0000262}
263
Spandan Dasca70fc42023-03-01 23:38:49 +0000264func (j *Javadoc) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
265 return j.SdkVersion(ctx).ApiLevel
Dan Willemsen419290a2018-10-31 15:28:47 -0700266}
267
Nan Zhang581fd212018-01-10 16:06:12 -0800268func (j *Javadoc) addDeps(ctx android.BottomUpMutatorContext) {
269 if ctx.Device() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900270 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Pete Gilline3d44b22020-06-29 11:28:51 +0100271 if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700272 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100273 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700274 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Liz Kammeref28a4c2022-09-23 16:50:56 -0400275 ctx.AddVariationDependencies(nil, sdkLibTag, sdkDep.classpath...)
Nan Zhang581fd212018-01-10 16:06:12 -0800276 }
277 }
278
Colin Cross42d48b72018-08-29 14:10:52 -0700279 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800280}
281
Nan Zhanga40da042018-08-01 12:48:00 -0700282func (j *Javadoc) collectAidlFlags(ctx android.ModuleContext, deps deps) droiddocBuilderFlags {
283 var flags droiddocBuilderFlags
Jiyong Park1e440682018-05-23 18:42:04 +0900284
Colin Cross3047fa22019-04-18 10:56:44 -0700285 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Jiyong Park1e440682018-05-23 18:42:04 +0900286
287 return flags
288}
289
290func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700291 aidlIncludeDirs android.Paths) (string, android.Paths) {
Jiyong Park1e440682018-05-23 18:42:04 +0900292
293 aidlIncludes := android.PathsForModuleSrc(ctx, j.properties.Aidl.Local_include_dirs)
294 aidlIncludes = append(aidlIncludes, android.PathsForSource(ctx, j.properties.Aidl.Include_dirs)...)
295
296 var flags []string
Colin Cross3047fa22019-04-18 10:56:44 -0700297 var deps android.Paths
298
Jiyong Park1e440682018-05-23 18:42:04 +0900299 if aidlPreprocess.Valid() {
300 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700301 deps = append(deps, aidlPreprocess.Path())
Jiyong Park1e440682018-05-23 18:42:04 +0900302 } else {
303 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
304 }
305
306 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Crossf96b0012023-10-26 14:01:51 -0700307 flags = append(flags, "-I"+ctx.ModuleDir())
Jiyong Park1e440682018-05-23 18:42:04 +0900308 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
309 flags = append(flags, "-I"+src.String())
310 }
311
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000312 minSdkVersion := j.MinSdkVersion(ctx).FinalOrFutureInt()
Spandan Das757b6662022-11-17 04:29:59 +0000313 flags = append(flags, fmt.Sprintf("--min_sdk_version=%v", minSdkVersion))
314
Colin Cross3047fa22019-04-18 10:56:44 -0700315 return strings.Join(flags, " "), deps
Jiyong Park1e440682018-05-23 18:42:04 +0900316}
317
Jiyong Parkd90d7412019-08-20 22:49:19 +0900318// TODO: remove the duplication between this and the one in gen.go
Jiyong Park1e440682018-05-23 18:42:04 +0900319func (j *Javadoc) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Nan Zhanga40da042018-08-01 12:48:00 -0700320 flags droiddocBuilderFlags) android.Paths {
Jiyong Park1e440682018-05-23 18:42:04 +0900321
322 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Crossc0806172019-06-14 18:51:47 -0700323 var aidlSrcs android.Paths
Jiyong Park1e440682018-05-23 18:42:04 +0900324
Sam Delmerico2351eac2022-05-24 17:10:02 +0000325 aidlIncludeFlags := genAidlIncludeFlags(ctx, srcFiles, android.Paths{})
Jiyong Park1112c4c2019-08-16 21:12:10 +0900326
Jiyong Park1e440682018-05-23 18:42:04 +0900327 for _, srcFile := range srcFiles {
328 switch srcFile.Ext() {
329 case ".aidl":
Colin Crossc0806172019-06-14 18:51:47 -0700330 aidlSrcs = append(aidlSrcs, srcFile)
Jiyong Parkd90d7412019-08-20 22:49:19 +0900331 case ".logtags":
332 javaFile := genLogtags(ctx, srcFile)
333 outSrcFiles = append(outSrcFiles, javaFile)
Jiyong Park1e440682018-05-23 18:42:04 +0900334 default:
335 outSrcFiles = append(outSrcFiles, srcFile)
336 }
337 }
338
Colin Crossc0806172019-06-14 18:51:47 -0700339 // Process all aidl files together to support sharding them into one or more rules that produce srcjars.
340 if len(aidlSrcs) > 0 {
Thiébaud Weksteende8417c2022-02-10 15:41:46 +1100341 srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags+aidlIncludeFlags, nil, flags.aidlDeps)
Colin Crossc0806172019-06-14 18:51:47 -0700342 outSrcFiles = append(outSrcFiles, srcJarFiles...)
343 }
344
Jiyong Park1e440682018-05-23 18:42:04 +0900345 return outSrcFiles
346}
347
Nan Zhang581fd212018-01-10 16:06:12 -0800348func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
349 var deps deps
350
Jiyong Parkf1691d22021-03-29 20:11:58 +0900351 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800352 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700353 ctx.AddMissingDependencies(sdkDep.bootclasspath)
354 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Nan Zhang581fd212018-01-10 16:06:12 -0800355 } else if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700356 deps.bootClasspath = append(deps.bootClasspath, sdkDep.jars...)
Anton Hansson26bf49b2020-02-08 20:26:29 +0000357 deps.aidlPreprocess = sdkDep.aidl
358 } else {
359 deps.aidlPreprocess = sdkDep.aidl
Nan Zhang581fd212018-01-10 16:06:12 -0800360 }
361
362 ctx.VisitDirectDeps(func(module android.Module) {
363 otherName := ctx.OtherModuleName(module)
364 tag := ctx.OtherModuleDependencyTag(module)
365
Colin Cross2d24c1b2018-05-23 10:59:18 -0700366 switch tag {
367 case bootClasspathTag:
Colin Cross313aa542023-12-13 13:47:44 -0800368 if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800369 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars...)
Paul Duffin83a2d962019-11-19 19:44:10 +0000370 } else if sm, ok := module.(SystemModulesProvider); ok {
Paul Duffine25c6442019-10-11 13:50:28 +0100371 // A system modules dependency has been added to the bootclasspath
372 // so add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +0000373 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700374 } else {
375 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
376 }
Liz Kammeref28a4c2022-09-23 16:50:56 -0400377 case libTag, sdkLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -0800378 if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park92315372021-04-02 08:45:46 +0900379 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Colin Cross313aa542023-12-13 13:47:44 -0800380 } else if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800381 deps.classpath = append(deps.classpath, dep.HeaderJars...)
382 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs...)
383 } else if dep, ok := module.(android.SourceFileProducer); ok {
Nan Zhang581fd212018-01-10 16:06:12 -0800384 checkProducesJars(ctx, dep)
385 deps.classpath = append(deps.classpath, dep.Srcs()...)
Colin Crossdcf71b22021-02-01 13:59:03 -0800386 } else {
Nan Zhang581fd212018-01-10 16:06:12 -0800387 ctx.ModuleErrorf("depends on non-java module %q", otherName)
388 }
Colin Cross6cef4812019-10-17 14:23:50 -0700389 case java9LibTag:
Colin Cross313aa542023-12-13 13:47:44 -0800390 if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800391 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars...)
392 } else {
Colin Cross6cef4812019-10-17 14:23:50 -0700393 ctx.ModuleErrorf("depends on non-java module %q", otherName)
394 }
Nan Zhang357466b2018-04-17 17:38:36 -0700395 case systemModulesTag:
396 if deps.systemModules != nil {
397 panic("Found two system module dependencies")
398 }
Paul Duffin83a2d962019-11-19 19:44:10 +0000399 sm := module.(SystemModulesProvider)
400 outputDir, outputDeps := sm.OutputDirAndDeps()
401 deps.systemModules = &systemModules{outputDir, outputDeps}
Jihoon Kang6592e872023-12-19 01:13:16 +0000402 case aconfigDeclarationTag:
403 if dep, ok := android.OtherModuleProvider(ctx, module, android.AconfigDeclarationsProviderKey); ok {
404 deps.aconfigProtoFiles = append(deps.aconfigProtoFiles, dep.IntermediateCacheOutputPath)
405 } else {
406 ctx.ModuleErrorf("Only aconfig_declarations module type is allowed for "+
407 "flags_packages property, but %s is not aconfig_declarations module type",
408 module.Name(),
409 )
410 }
Nan Zhang581fd212018-01-10 16:06:12 -0800411 }
412 })
413 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
414 // may contain filegroup or genrule.
Colin Cross8a497952019-03-05 22:25:09 -0800415 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400416 j.implicits = append(j.implicits, srcFiles...)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900417
418 filterByPackage := func(srcs []android.Path, filterPackages []string) []android.Path {
419 if filterPackages == nil {
420 return srcs
421 }
422 filtered := []android.Path{}
423 for _, src := range srcs {
424 if src.Ext() != ".java" {
425 // Don't filter-out non-Java (=generated sources) by package names. This is not ideal,
426 // but otherwise metalava emits stub sources having references to the generated AIDL classes
427 // in filtered-out pacages (e.g. com.android.internal.*).
428 // TODO(b/141149570) We need to fix this by introducing default private constructors or
429 // fixing metalava to not emit constructors having references to unknown classes.
430 filtered = append(filtered, src)
431 continue
432 }
433 packageName := strings.ReplaceAll(filepath.Dir(src.Rel()), "/", ".")
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800434 if android.HasAnyPrefix(packageName, filterPackages) {
435 filtered = append(filtered, src)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900436 }
437 }
438 return filtered
439 }
440 srcFiles = filterByPackage(srcFiles, j.properties.Filter_packages)
441
Liz Kammer585cac22020-07-06 09:12:57 -0700442 aidlFlags := j.collectAidlFlags(ctx, deps)
443 srcFiles = j.genSources(ctx, srcFiles, aidlFlags)
Nan Zhang581fd212018-01-10 16:06:12 -0800444
445 // srcs may depend on some genrule output.
446 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800447 j.srcJars = append(j.srcJars, deps.srcJars...)
448
Nan Zhang581fd212018-01-10 16:06:12 -0800449 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800450 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800451
Liz Kammere1ab2502020-09-10 15:29:25 +0000452 if len(j.srcFiles) > 0 {
453 j.sourcepaths = android.PathsForModuleSrc(ctx, []string{"."})
Nan Zhang581fd212018-01-10 16:06:12 -0800454 }
Nan Zhang581fd212018-01-10 16:06:12 -0800455
Colin Crossbc139922021-03-25 18:33:16 -0700456 return deps
457}
458
459func (j *Javadoc) expandArgs(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
460 var argFiles android.Paths
Paul Duffin99e4a502019-02-11 15:38:42 +0000461 argFilesMap := map[string]string{}
462 argFileLabels := []string{}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700463
Paul Duffin99e4a502019-02-11 15:38:42 +0000464 for _, label := range j.properties.Arg_files {
Colin Cross8a497952019-03-05 22:25:09 -0800465 var paths = android.PathsForModuleSrc(ctx, []string{label})
Paul Duffin99e4a502019-02-11 15:38:42 +0000466 if _, exists := argFilesMap[label]; !exists {
Colin Crossbc139922021-03-25 18:33:16 -0700467 argFilesMap[label] = strings.Join(cmd.PathsForInputs(paths), " ")
Paul Duffin99e4a502019-02-11 15:38:42 +0000468 argFileLabels = append(argFileLabels, label)
Colin Crossbc139922021-03-25 18:33:16 -0700469 argFiles = append(argFiles, paths...)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700470 } else {
471 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000472 label, argFilesMap[label], paths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700473 }
474 }
475
Liz Kammer585cac22020-07-06 09:12:57 -0700476 var argsPropertyName string
477 flags := make([]string, 0)
478 if j.properties.Args != nil && j.properties.Flags != nil {
479 ctx.PropertyErrorf("args", "flags is set. Cannot set args")
480 } else if args := proptools.String(j.properties.Args); args != "" {
481 flags = append(flags, args)
482 argsPropertyName = "args"
483 } else {
484 flags = append(flags, j.properties.Flags...)
485 argsPropertyName = "flags"
486 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700487
Liz Kammer585cac22020-07-06 09:12:57 -0700488 for _, flag := range flags {
Colin Crossbc139922021-03-25 18:33:16 -0700489 expanded, err := android.Expand(flag, func(name string) (string, error) {
Liz Kammer585cac22020-07-06 09:12:57 -0700490 if strings.HasPrefix(name, "location ") {
491 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
492 if paths, ok := argFilesMap[label]; ok {
493 return paths, nil
494 } else {
495 return "", fmt.Errorf("unknown location label %q, expecting one of %q",
496 label, strings.Join(argFileLabels, ", "))
497 }
498 } else if name == "genDir" {
499 return android.PathForModuleGen(ctx).String(), nil
500 }
501 return "", fmt.Errorf("unknown variable '$(%s)'", name)
502 })
503
504 if err != nil {
505 ctx.PropertyErrorf(argsPropertyName, "%s", err.Error())
506 }
Colin Crossbc139922021-03-25 18:33:16 -0700507 cmd.Flag(expanded)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700508 }
509
Colin Crossbc139922021-03-25 18:33:16 -0700510 cmd.Implicits(argFiles)
Nan Zhang581fd212018-01-10 16:06:12 -0800511}
512
513func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
514 j.addDeps(ctx)
515}
516
517func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
518 deps := j.collectDeps(ctx)
519
Colin Crossdaa4c672019-07-15 22:53:46 -0700520 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800521
Colin Crossdaa4c672019-07-15 22:53:46 -0700522 outDir := android.PathForModuleOut(ctx, "out")
523 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
524
525 j.stubsSrcJar = nil
526
Colin Crossf1a035e2020-11-16 17:32:30 -0800527 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossdaa4c672019-07-15 22:53:46 -0700528
529 rule.Command().Text("rm -rf").Text(outDir.String())
530 rule.Command().Text("mkdir -p").Text(outDir.String())
531
532 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, j.srcJars)
Nan Zhang357466b2018-04-17 17:38:36 -0700533
Jiyong Parkf1691d22021-03-29 20:11:58 +0900534 javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), android.SdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800535
Colin Crossdaa4c672019-07-15 22:53:46 -0700536 cmd := javadocSystemModulesCmd(ctx, rule, j.srcFiles, outDir, srcJarDir, srcJarList,
537 deps.systemModules, deps.classpath, j.sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800538
Colin Cross1e743852019-10-28 11:37:20 -0700539 cmd.FlagWithArg("-source ", javaVersion.String()).
Colin Crossdaa4c672019-07-15 22:53:46 -0700540 Flag("-J-Xmx1024m").
541 Flag("-XDignore.symbol.file").
542 Flag("-Xdoclint:none")
Nan Zhang581fd212018-01-10 16:06:12 -0800543
Colin Crossbc139922021-03-25 18:33:16 -0700544 j.expandArgs(ctx, cmd)
545
Colin Crossdaa4c672019-07-15 22:53:46 -0700546 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800547 BuiltTool("soong_zip").
Colin Crossdaa4c672019-07-15 22:53:46 -0700548 Flag("-write_if_changed").
549 Flag("-d").
550 FlagWithOutput("-o ", j.docZip).
551 FlagWithArg("-C ", outDir.String()).
552 FlagWithArg("-D ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700553
Colin Crossdaa4c672019-07-15 22:53:46 -0700554 rule.Restat()
555
556 zipSyncCleanupCmd(rule, srcJarDir)
557
Colin Crossf1a035e2020-11-16 17:32:30 -0800558 rule.Build("javadoc", "javadoc")
Nan Zhang581fd212018-01-10 16:06:12 -0800559}
560
Nan Zhanga40da042018-08-01 12:48:00 -0700561// Droiddoc
Nan Zhanga40da042018-08-01 12:48:00 -0700562type Droiddoc struct {
563 Javadoc
564
Liz Kammere1ab2502020-09-10 15:29:25 +0000565 properties DroiddocProperties
Nan Zhanga40da042018-08-01 12:48:00 -0700566}
567
Colin Crossa3002fc2019-07-08 16:48:04 -0700568// droiddoc converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700569func DroiddocFactory() android.Module {
570 module := &Droiddoc{}
571
572 module.AddProperties(&module.properties,
573 &module.Javadoc.properties)
574
575 InitDroiddocModule(module, android.HostAndDeviceSupported)
576 return module
577}
578
Colin Crossa3002fc2019-07-08 16:48:04 -0700579// droiddoc_host converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700580func DroiddocHostFactory() android.Module {
581 module := &Droiddoc{}
582
583 module.AddProperties(&module.properties,
584 &module.Javadoc.properties)
585
586 InitDroiddocModule(module, android.HostSupported)
587 return module
588}
589
Liz Kammere1ab2502020-09-10 15:29:25 +0000590func (d *Droiddoc) OutputFiles(tag string) (android.Paths, error) {
591 switch tag {
592 case "", ".docs.zip":
593 return android.Paths{d.Javadoc.docZip}, nil
594 default:
595 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
596 }
Nan Zhanga40da042018-08-01 12:48:00 -0700597}
598
Nan Zhang581fd212018-01-10 16:06:12 -0800599func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
600 d.Javadoc.addDeps(ctx)
601
Nan Zhang79614d12018-04-19 18:03:39 -0700602 if String(d.properties.Custom_template) != "" {
Dan Willemsencc090972018-02-26 14:33:31 -0800603 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
604 }
Nan Zhang581fd212018-01-10 16:06:12 -0800605}
606
Colin Crossab054432019-07-15 16:13:59 -0700607func (d *Droiddoc) doclavaDocsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, docletPath classpath) {
Colin Cross2a2e0db2020-02-21 16:55:46 -0800608 buildNumberFile := ctx.Config().BuildNumberFile(ctx)
Nan Zhang443fa522018-08-20 20:58:28 -0700609 // Droiddoc always gets "-source 1.8" because it doesn't support 1.9 sources. For modules with 1.9
610 // sources, droiddoc will get sources produced by metalava which will have already stripped out the
611 // 1.9 language features.
Jihoon Kangff878bf2022-12-22 21:26:06 +0000612 cmd.FlagWithArg("-source ", getStubsJavaVersion().String()).
Colin Crossab054432019-07-15 16:13:59 -0700613 Flag("-J-Xmx1600m").
614 Flag("-J-XX:-OmitStackTraceInFastThrow").
615 Flag("-XDignore.symbol.file").
Sorin Bascad528d562022-10-24 15:10:25 +0100616 Flag("--ignore-source-errors").
Sorin Bascaae995ae2023-03-01 08:47:42 +0000617 FlagWithArg("-doclet ", "com.google.doclava.Doclava").
Colin Crossab054432019-07-15 16:13:59 -0700618 FlagWithInputList("-docletpath ", docletPath.Paths(), ":").
Sorin Bascaae995ae2023-03-01 08:47:42 +0000619 FlagWithArg("-Xmaxerrs ", "10").
620 FlagWithArg("-Xmaxwarns ", "10").
Sorin Bascad528d562022-10-24 15:10:25 +0100621 Flag("-J--add-exports=jdk.javadoc/jdk.javadoc.internal.doclets.formats.html=ALL-UNNAMED").
Sorin Bascaa7b777f2023-06-09 09:19:36 +0000622 Flag("-J--add-exports=jdk.javadoc/jdk.javadoc.internal.tool=ALL-UNNAMED").
623 Flag("-J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED").
624 Flag("-J--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED").
625 Flag("-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED").
626 Flag("-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED").
Sorin Bascad528d562022-10-24 15:10:25 +0100627 Flag("-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED").
Colin Cross2a2e0db2020-02-21 16:55:46 -0800628 FlagWithArg("-hdf page.build ", ctx.Config().BuildId()+"-$(cat "+buildNumberFile.String()+")").OrderOnly(buildNumberFile).
Elliott Hughes26bce342019-09-12 15:05:13 -0700629 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 -0700630
Nan Zhanga40da042018-08-01 12:48:00 -0700631 if String(d.properties.Custom_template) == "" {
632 // TODO: This is almost always droiddoc-templates-sdk
633 ctx.PropertyErrorf("custom_template", "must specify a template")
634 }
635
636 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
Nan Zhangf4936b02018-08-01 15:00:28 -0700637 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Crossab054432019-07-15 16:13:59 -0700638 cmd.FlagWithArg("-templatedir ", t.dir.String()).Implicits(t.deps)
Nan Zhanga40da042018-08-01 12:48:00 -0700639 } else {
Paul Duffin884363e2019-12-19 10:21:09 +0000640 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_exported_dir", ctx.OtherModuleName(m))
Nan Zhanga40da042018-08-01 12:48:00 -0700641 }
642 })
643
644 if len(d.properties.Html_dirs) > 0 {
Colin Crossab054432019-07-15 16:13:59 -0700645 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
646 cmd.FlagWithArg("-htmldir ", htmlDir.String()).
647 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[0], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700648 }
649
650 if len(d.properties.Html_dirs) > 1 {
Colin Crossab054432019-07-15 16:13:59 -0700651 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
652 cmd.FlagWithArg("-htmldir2 ", htmlDir2.String()).
653 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[1], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700654 }
655
656 if len(d.properties.Html_dirs) > 2 {
657 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
658 }
659
Colin Cross8a497952019-03-05 22:25:09 -0800660 knownTags := android.PathsForModuleSrc(ctx, d.properties.Knowntags)
Colin Crossab054432019-07-15 16:13:59 -0700661 cmd.FlagForEachInput("-knowntags ", knownTags)
Nan Zhanga40da042018-08-01 12:48:00 -0700662
Colin Crossab054432019-07-15 16:13:59 -0700663 cmd.FlagForEachArg("-hdf ", d.properties.Hdf)
Nan Zhanga40da042018-08-01 12:48:00 -0700664
665 if String(d.properties.Proofread_file) != "" {
666 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
Colin Crossab054432019-07-15 16:13:59 -0700667 cmd.FlagWithOutput("-proofread ", proofreadFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700668 }
669
670 if String(d.properties.Todo_file) != "" {
671 // tricky part:
672 // we should not compute full path for todo_file through PathForModuleOut().
673 // the non-standard doclet will get the full path relative to "-o".
Colin Crossab054432019-07-15 16:13:59 -0700674 cmd.FlagWithArg("-todo ", String(d.properties.Todo_file)).
675 ImplicitOutput(android.PathForModuleOut(ctx, String(d.properties.Todo_file)))
Nan Zhanga40da042018-08-01 12:48:00 -0700676 }
677
Anton Hanssonb06bb572023-10-03 12:11:35 +0000678 if String(d.properties.Lint_baseline) != "" {
679 cmd.FlagWithInput("-lintbaseline ", android.PathForModuleSrc(ctx, String(d.properties.Lint_baseline)))
680 }
681
Nan Zhanga40da042018-08-01 12:48:00 -0700682 if String(d.properties.Resourcesdir) != "" {
683 // TODO: should we add files under resourcesDir to the implicits? It seems that
684 // resourcesDir is one sub dir of htmlDir
685 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
Colin Crossab054432019-07-15 16:13:59 -0700686 cmd.FlagWithArg("-resourcesdir ", resourcesDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700687 }
688
689 if String(d.properties.Resourcesoutdir) != "" {
690 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
Colin Crossab054432019-07-15 16:13:59 -0700691 cmd.FlagWithArg("-resourcesoutdir ", String(d.properties.Resourcesoutdir))
Nan Zhanga40da042018-08-01 12:48:00 -0700692 }
Nan Zhanga40da042018-08-01 12:48:00 -0700693}
694
Colin Crossab054432019-07-15 16:13:59 -0700695func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700696 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700697 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
698 rule.Command().Text("cp").
699 Input(staticDocIndexRedirect).
700 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700701 }
702
703 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700704 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
705 rule.Command().Text("cp").
706 Input(staticDocProperties).
707 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700708 }
Nan Zhanga40da042018-08-01 12:48:00 -0700709}
710
Colin Crossab054432019-07-15 16:13:59 -0700711func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700712 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700713
714 cmd := rule.Command().
Sorin Bascad528d562022-10-24 15:10:25 +0100715 BuiltTool("soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
Colin Crossab054432019-07-15 16:13:59 -0700716 Flag(config.JavacVmFlags).
Colin Cross70c47412021-03-12 17:48:14 -0800717 FlagWithRspFileInputList("@", android.PathForModuleOut(ctx, "javadoc.rsp"), srcs).
Colin Crossab054432019-07-15 16:13:59 -0700718 FlagWithInput("@", srcJarList)
719
Colin Crossab054432019-07-15 16:13:59 -0700720 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
721 // based stubs generation.
722 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
723 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
724 // the correct package name base path.
725 if len(sourcepaths) > 0 {
726 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
727 } else {
728 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
729 }
730
731 cmd.FlagWithArg("-d ", outDir.String()).
732 Flag("-quiet")
733
734 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700735}
736
Colin Crossdaa4c672019-07-15 22:53:46 -0700737func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
738 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
739 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
740
741 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
742
743 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
744 cmd.Flag(flag).Implicits(deps)
745
746 cmd.FlagWithArg("--patch-module ", "java.base=.")
747
748 if len(classpath) > 0 {
749 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
750 }
751
752 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700753}
754
Colin Crossdaa4c672019-07-15 22:53:46 -0700755func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
756 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
757 sourcepaths android.Paths) *android.RuleBuilderCommand {
758
759 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
760
761 if len(bootclasspath) == 0 && ctx.Device() {
762 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
763 // ensure java does not fall back to the default bootclasspath.
764 cmd.FlagWithArg("-bootclasspath ", `""`)
765 } else if len(bootclasspath) > 0 {
766 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
767 }
768
769 if len(classpath) > 0 {
770 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
771 }
772
773 return cmd
774}
775
Colin Crossab054432019-07-15 16:13:59 -0700776func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
777 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700778
Colin Crossab054432019-07-15 16:13:59 -0700779 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
780 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
781
782 return rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800783 BuiltTool("dokka").
Colin Crossab054432019-07-15 16:13:59 -0700784 Flag(config.JavacVmFlags).
Sorin Basca1d68e482022-09-08 16:48:01 +0100785 Flag("-J--add-opens=java.base/java.lang=ALL-UNNAMED").
Colin Crossab054432019-07-15 16:13:59 -0700786 Flag(srcJarDir.String()).
787 FlagWithInputList("-classpath ", dokkaClasspath, ":").
788 FlagWithArg("-format ", "dac").
789 FlagWithArg("-dacRoot ", "/reference/kotlin").
790 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700791}
792
793func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
794 deps := d.Javadoc.collectDeps(ctx)
795
Colin Crossdaa4c672019-07-15 22:53:46 -0700796 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Colin Crossdaa4c672019-07-15 22:53:46 -0700797
Colin Crossae5330a2021-11-03 13:31:22 -0700798 jsilver := ctx.Config().HostJavaToolPath(ctx, "jsilver.jar")
799 doclava := ctx.Config().HostJavaToolPath(ctx, "doclava.jar")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700800
Colin Crossab054432019-07-15 16:13:59 -0700801 outDir := android.PathForModuleOut(ctx, "out")
802 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700803
Colin Crossf1a035e2020-11-16 17:32:30 -0800804 rule := android.NewRuleBuilder(pctx, ctx)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700805
Colin Crossab054432019-07-15 16:13:59 -0700806 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
807
808 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -0700809 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -0700810 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700811 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -0700812 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -0700813 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700814 }
815
Colin Crossbc139922021-03-25 18:33:16 -0700816 d.expandArgs(ctx, cmd)
Colin Crossab054432019-07-15 16:13:59 -0700817
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000818 if d.properties.Compat_config != nil {
819 compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
820 cmd.FlagWithInput("-compatconfig ", compatConfig)
821 }
822
Colin Crossab054432019-07-15 16:13:59 -0700823 var desc string
824 if Bool(d.properties.Dokka_enabled) {
825 desc = "dokka"
826 } else {
Sorin Bascaae995ae2023-03-01 08:47:42 +0000827 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
Colin Crossab054432019-07-15 16:13:59 -0700828
829 for _, o := range d.Javadoc.properties.Out {
830 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
831 }
832
833 d.postDoclavaCmds(ctx, rule)
834 desc = "doclava"
835 }
836
837 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800838 BuiltTool("soong_zip").
Colin Crossab054432019-07-15 16:13:59 -0700839 Flag("-write_if_changed").
840 Flag("-d").
841 FlagWithOutput("-o ", d.docZip).
842 FlagWithArg("-C ", outDir.String()).
843 FlagWithArg("-D ", outDir.String())
844
Sorin Bascaae995ae2023-03-01 08:47:42 +0000845 rule.Restat()
Colin Crossab054432019-07-15 16:13:59 -0700846
Sorin Bascaae995ae2023-03-01 08:47:42 +0000847 zipSyncCleanupCmd(rule, srcJarDir)
Colin Crossab054432019-07-15 16:13:59 -0700848
Colin Crossf1a035e2020-11-16 17:32:30 -0800849 rule.Build("javadoc", desc)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700850}
851
Nan Zhangf4936b02018-08-01 15:00:28 -0700852// Exported Droiddoc Directory
Dan Willemsencc090972018-02-26 14:33:31 -0800853var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
854
Nan Zhangf4936b02018-08-01 15:00:28 -0700855type ExportedDroiddocDirProperties struct {
856 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -0800857 Path *string
858}
859
Nan Zhangf4936b02018-08-01 15:00:28 -0700860type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -0800861 android.ModuleBase
862
Nan Zhangf4936b02018-08-01 15:00:28 -0700863 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -0800864
865 deps android.Paths
866 dir android.Path
867}
868
Colin Crossa3002fc2019-07-08 16:48:04 -0700869// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -0700870func ExportedDroiddocDirFactory() android.Module {
871 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -0800872 module.AddProperties(&module.properties)
873 android.InitAndroidModule(module)
874 return module
875}
876
Nan Zhangf4936b02018-08-01 15:00:28 -0700877func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -0800878
Nan Zhangf4936b02018-08-01 15:00:28 -0700879func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -0800880 path := String(d.properties.Path)
881 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -0800882 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Dan Willemsencc090972018-02-26 14:33:31 -0800883}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800884
Nan Zhangb2b33de2018-02-23 11:18:47 -0800885// Defaults
Nan Zhangb2b33de2018-02-23 11:18:47 -0800886type DocDefaults struct {
887 android.ModuleBase
888 android.DefaultsModuleBase
889}
890
Nan Zhangb2b33de2018-02-23 11:18:47 -0800891func DocDefaultsFactory() android.Module {
892 module := &DocDefaults{}
893
894 module.AddProperties(
895 &JavadocProperties{},
896 &DroiddocProperties{},
897 )
898
899 android.InitDefaultsModule(module)
900
901 return module
902}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700903
Colin Cross33961b52019-07-11 11:01:22 -0700904func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
905 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
906
Colin Cross1661aff2021-03-12 17:56:51 -0800907 cmd := rule.Command()
908 cmd.Text("rm -rf").Text(cmd.PathForOutput(srcJarDir))
909 cmd = rule.Command()
910 cmd.Text("mkdir -p").Text(cmd.PathForOutput(srcJarDir))
Colin Cross33961b52019-07-11 11:01:22 -0700911 srcJarList := srcJarDir.Join(ctx, "list")
912
913 rule.Temporary(srcJarList)
914
Colin Cross1661aff2021-03-12 17:56:51 -0800915 cmd = rule.Command()
916 cmd.BuiltTool("zipsync").
917 FlagWithArg("-d ", cmd.PathForOutput(srcJarDir)).
Colin Cross33961b52019-07-11 11:01:22 -0700918 FlagWithOutput("-l ", srcJarList).
919 FlagWithArg("-f ", `"*.java"`).
920 Inputs(srcJars)
921
922 return srcJarList
923}
924
925func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
926 rule.Command().Text("rm -rf").Text(srcJarDir.String())
927}