blob: 56e62473f237ef76f913dd097672fa4c3f5b1653 [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
138 // directory under current module source that provide additional resources (images).
139 Resourcesdir *string
140
141 // resources output directory under out/soong/.intermediates.
142 Resourcesoutdir *string
Nan Zhang581fd212018-01-10 16:06:12 -0800143
Nan Zhange2ba5d42018-07-11 15:16:55 -0700144 // index.html under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800145 Static_doc_index_redirect *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700146
147 // source.properties under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800148 Static_doc_properties *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700149
Nan Zhang581fd212018-01-10 16:06:12 -0800150 // a list of files under current module source dir which contains known tags in Java sources.
151 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -0800152 Knowntags []string `android:"path"`
Nan Zhang28c68b92018-03-13 16:17:01 -0700153
Nan Zhang1598a9e2018-09-04 17:14:32 -0700154 // if set to true, generate docs through Dokka instead of Doclava.
155 Dokka_enabled *bool
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000156
157 // Compat config XML. Generates compat change documentation if set.
158 Compat_config *string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -0700159}
160
Nan Zhanga40da042018-08-01 12:48:00 -0700161//
162// Common flags passed down to build rule
163//
164type droiddocBuilderFlags struct {
Nan Zhang86d2d552018-08-09 15:33:27 -0700165 bootClasspathArgs string
166 classpathArgs string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700167 sourcepathArgs string
Nan Zhang86d2d552018-08-09 15:33:27 -0700168 dokkaClasspathArgs string
169 aidlFlags string
Colin Cross3047fa22019-04-18 10:56:44 -0700170 aidlDeps android.Paths
Nan Zhanga40da042018-08-01 12:48:00 -0700171
Nan Zhanga40da042018-08-01 12:48:00 -0700172 doclavaStubsFlags string
Nan Zhang86d2d552018-08-09 15:33:27 -0700173 doclavaDocsFlags string
Nan Zhanga40da042018-08-01 12:48:00 -0700174 postDoclavaCmds string
Nan Zhanga40da042018-08-01 12:48:00 -0700175}
176
177func InitDroiddocModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
178 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
179 android.InitDefaultableModule(module)
180}
181
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200182func apiCheckEnabled(ctx android.ModuleContext, apiToCheck ApiToCheck, apiVersionTag string) bool {
183 if ctx.Config().IsEnvTrue("WITHOUT_CHECK_API") {
184 return false
185 } else if String(apiToCheck.Api_file) != "" && String(apiToCheck.Removed_api_file) != "" {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700186 return true
187 } else if String(apiToCheck.Api_file) != "" {
188 panic("for " + apiVersionTag + " removed_api_file has to be non-empty!")
189 } else if String(apiToCheck.Removed_api_file) != "" {
190 panic("for " + apiVersionTag + " api_file has to be non-empty!")
191 }
192
193 return false
194}
195
Nan Zhanga40da042018-08-01 12:48:00 -0700196//
197// Javadoc
198//
Nan Zhang581fd212018-01-10 16:06:12 -0800199type Javadoc struct {
200 android.ModuleBase
201 android.DefaultableModuleBase
202
203 properties JavadocProperties
204
205 srcJars android.Paths
206 srcFiles android.Paths
207 sourcepaths android.Paths
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400208 implicits android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700209
Nan Zhangccff0f72018-03-08 17:26:16 -0800210 docZip android.WritablePath
211 stubsSrcJar android.WritablePath
Nan Zhang581fd212018-01-10 16:06:12 -0800212}
213
Colin Cross41955e82019-05-29 14:40:35 -0700214func (j *Javadoc) OutputFiles(tag string) (android.Paths, error) {
215 switch tag {
216 case "":
217 return android.Paths{j.stubsSrcJar}, nil
Colin Crosse68e5542019-08-12 13:11:40 -0700218 case ".docs.zip":
219 return android.Paths{j.docZip}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700220 default:
221 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
222 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800223}
224
Colin Crossa3002fc2019-07-08 16:48:04 -0700225// javadoc converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800226func JavadocFactory() android.Module {
227 module := &Javadoc{}
228
229 module.AddProperties(&module.properties)
230
231 InitDroiddocModule(module, android.HostAndDeviceSupported)
232 return module
233}
234
Colin Crossa3002fc2019-07-08 16:48:04 -0700235// javadoc_host converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800236func JavadocHostFactory() android.Module {
237 module := &Javadoc{}
238
239 module.AddProperties(&module.properties)
240
241 InitDroiddocModule(module, android.HostSupported)
242 return module
243}
244
Colin Cross41955e82019-05-29 14:40:35 -0700245var _ android.OutputFileProducer = (*Javadoc)(nil)
Nan Zhang581fd212018-01-10 16:06:12 -0800246
Jiyong Park92315372021-04-02 08:45:46 +0900247func (j *Javadoc) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
248 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700249}
250
Jiyong Parkf1691d22021-03-29 20:11:58 +0900251func (j *Javadoc) SystemModules() string {
Paul Duffine25c6442019-10-11 13:50:28 +0100252 return proptools.String(j.properties.System_modules)
253}
254
Jiyong Park92315372021-04-02 08:45:46 +0900255func (j *Javadoc) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
256 return j.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -0700257}
258
Jiyong Park92315372021-04-02 08:45:46 +0900259func (j *Javadoc) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
260 return j.SdkVersion(ctx)
Dan Willemsen419290a2018-10-31 15:28:47 -0700261}
262
Nan Zhang581fd212018-01-10 16:06:12 -0800263func (j *Javadoc) addDeps(ctx android.BottomUpMutatorContext) {
264 if ctx.Device() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900265 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Pete Gilline3d44b22020-06-29 11:28:51 +0100266 if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700267 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100268 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700269 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Pete Gilline3d44b22020-06-29 11:28:51 +0100270 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
Nan Zhang581fd212018-01-10 16:06:12 -0800271 }
272 }
273
Colin Cross42d48b72018-08-29 14:10:52 -0700274 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800275}
276
Nan Zhanga40da042018-08-01 12:48:00 -0700277func (j *Javadoc) collectAidlFlags(ctx android.ModuleContext, deps deps) droiddocBuilderFlags {
278 var flags droiddocBuilderFlags
Jiyong Park1e440682018-05-23 18:42:04 +0900279
Colin Cross3047fa22019-04-18 10:56:44 -0700280 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Jiyong Park1e440682018-05-23 18:42:04 +0900281
282 return flags
283}
284
285func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700286 aidlIncludeDirs android.Paths) (string, android.Paths) {
Jiyong Park1e440682018-05-23 18:42:04 +0900287
288 aidlIncludes := android.PathsForModuleSrc(ctx, j.properties.Aidl.Local_include_dirs)
289 aidlIncludes = append(aidlIncludes, android.PathsForSource(ctx, j.properties.Aidl.Include_dirs)...)
290
291 var flags []string
Colin Cross3047fa22019-04-18 10:56:44 -0700292 var deps android.Paths
293
Jiyong Park1e440682018-05-23 18:42:04 +0900294 if aidlPreprocess.Valid() {
295 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700296 deps = append(deps, aidlPreprocess.Path())
Jiyong Park1e440682018-05-23 18:42:04 +0900297 } else {
298 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
299 }
300
301 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
302 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
303 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
304 flags = append(flags, "-I"+src.String())
305 }
306
Colin Cross3047fa22019-04-18 10:56:44 -0700307 return strings.Join(flags, " "), deps
Jiyong Park1e440682018-05-23 18:42:04 +0900308}
309
Jiyong Parkd90d7412019-08-20 22:49:19 +0900310// TODO: remove the duplication between this and the one in gen.go
Jiyong Park1e440682018-05-23 18:42:04 +0900311func (j *Javadoc) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Nan Zhanga40da042018-08-01 12:48:00 -0700312 flags droiddocBuilderFlags) android.Paths {
Jiyong Park1e440682018-05-23 18:42:04 +0900313
314 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Crossc0806172019-06-14 18:51:47 -0700315 var aidlSrcs android.Paths
Jiyong Park1e440682018-05-23 18:42:04 +0900316
Jiyong Park1112c4c2019-08-16 21:12:10 +0900317 aidlIncludeFlags := genAidlIncludeFlags(srcFiles)
318
Jiyong Park1e440682018-05-23 18:42:04 +0900319 for _, srcFile := range srcFiles {
320 switch srcFile.Ext() {
321 case ".aidl":
Colin Crossc0806172019-06-14 18:51:47 -0700322 aidlSrcs = append(aidlSrcs, srcFile)
Jiyong Parkd90d7412019-08-20 22:49:19 +0900323 case ".logtags":
324 javaFile := genLogtags(ctx, srcFile)
325 outSrcFiles = append(outSrcFiles, javaFile)
Jiyong Park1e440682018-05-23 18:42:04 +0900326 default:
327 outSrcFiles = append(outSrcFiles, srcFile)
328 }
329 }
330
Colin Crossc0806172019-06-14 18:51:47 -0700331 // Process all aidl files together to support sharding them into one or more rules that produce srcjars.
332 if len(aidlSrcs) > 0 {
333 srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags+aidlIncludeFlags, flags.aidlDeps)
334 outSrcFiles = append(outSrcFiles, srcJarFiles...)
335 }
336
Jiyong Park1e440682018-05-23 18:42:04 +0900337 return outSrcFiles
338}
339
Nan Zhang581fd212018-01-10 16:06:12 -0800340func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
341 var deps deps
342
Jiyong Parkf1691d22021-03-29 20:11:58 +0900343 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800344 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700345 ctx.AddMissingDependencies(sdkDep.bootclasspath)
346 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Nan Zhang581fd212018-01-10 16:06:12 -0800347 } else if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700348 deps.bootClasspath = append(deps.bootClasspath, sdkDep.jars...)
Anton Hansson26bf49b2020-02-08 20:26:29 +0000349 deps.aidlPreprocess = sdkDep.aidl
350 } else {
351 deps.aidlPreprocess = sdkDep.aidl
Nan Zhang581fd212018-01-10 16:06:12 -0800352 }
353
354 ctx.VisitDirectDeps(func(module android.Module) {
355 otherName := ctx.OtherModuleName(module)
356 tag := ctx.OtherModuleDependencyTag(module)
357
Colin Cross2d24c1b2018-05-23 10:59:18 -0700358 switch tag {
359 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -0800360 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
361 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
362 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars...)
Paul Duffin83a2d962019-11-19 19:44:10 +0000363 } else if sm, ok := module.(SystemModulesProvider); ok {
Paul Duffine25c6442019-10-11 13:50:28 +0100364 // A system modules dependency has been added to the bootclasspath
365 // so add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +0000366 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700367 } else {
368 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
369 }
370 case libTag:
Colin Crossdcf71b22021-02-01 13:59:03 -0800371 if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park92315372021-04-02 08:45:46 +0900372 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Colin Crossdcf71b22021-02-01 13:59:03 -0800373 } else if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
374 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
375 deps.classpath = append(deps.classpath, dep.HeaderJars...)
376 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs...)
377 } else if dep, ok := module.(android.SourceFileProducer); ok {
Nan Zhang581fd212018-01-10 16:06:12 -0800378 checkProducesJars(ctx, dep)
379 deps.classpath = append(deps.classpath, dep.Srcs()...)
Colin Crossdcf71b22021-02-01 13:59:03 -0800380 } else {
Nan Zhang581fd212018-01-10 16:06:12 -0800381 ctx.ModuleErrorf("depends on non-java module %q", otherName)
382 }
Colin Cross6cef4812019-10-17 14:23:50 -0700383 case java9LibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -0800384 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
385 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
386 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars...)
387 } else {
Colin Cross6cef4812019-10-17 14:23:50 -0700388 ctx.ModuleErrorf("depends on non-java module %q", otherName)
389 }
Nan Zhang357466b2018-04-17 17:38:36 -0700390 case systemModulesTag:
391 if deps.systemModules != nil {
392 panic("Found two system module dependencies")
393 }
Paul Duffin83a2d962019-11-19 19:44:10 +0000394 sm := module.(SystemModulesProvider)
395 outputDir, outputDeps := sm.OutputDirAndDeps()
396 deps.systemModules = &systemModules{outputDir, outputDeps}
Nan Zhang581fd212018-01-10 16:06:12 -0800397 }
398 })
399 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
400 // may contain filegroup or genrule.
Colin Cross8a497952019-03-05 22:25:09 -0800401 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400402 j.implicits = append(j.implicits, srcFiles...)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900403
404 filterByPackage := func(srcs []android.Path, filterPackages []string) []android.Path {
405 if filterPackages == nil {
406 return srcs
407 }
408 filtered := []android.Path{}
409 for _, src := range srcs {
410 if src.Ext() != ".java" {
411 // Don't filter-out non-Java (=generated sources) by package names. This is not ideal,
412 // but otherwise metalava emits stub sources having references to the generated AIDL classes
413 // in filtered-out pacages (e.g. com.android.internal.*).
414 // TODO(b/141149570) We need to fix this by introducing default private constructors or
415 // fixing metalava to not emit constructors having references to unknown classes.
416 filtered = append(filtered, src)
417 continue
418 }
419 packageName := strings.ReplaceAll(filepath.Dir(src.Rel()), "/", ".")
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800420 if android.HasAnyPrefix(packageName, filterPackages) {
421 filtered = append(filtered, src)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900422 }
423 }
424 return filtered
425 }
426 srcFiles = filterByPackage(srcFiles, j.properties.Filter_packages)
427
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400428 // While metalava needs package html files, it does not need them to be explicit on the command
Anton Hansson746be9c2020-10-08 19:05:40 +0100429 // line. javadoc complains if it receives html files on the command line. The filter
430 // below excludes html files from the rsp file metalava. Note that the html
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400431 // files are still included as implicit inputs for successful remote execution and correct
432 // incremental builds.
433 filterHtml := func(srcs []android.Path) []android.Path {
434 filtered := []android.Path{}
435 for _, src := range srcs {
436 if src.Ext() == ".html" {
437 continue
438 }
439 filtered = append(filtered, src)
440 }
441 return filtered
442 }
443 srcFiles = filterHtml(srcFiles)
444
Liz Kammer585cac22020-07-06 09:12:57 -0700445 aidlFlags := j.collectAidlFlags(ctx, deps)
446 srcFiles = j.genSources(ctx, srcFiles, aidlFlags)
Nan Zhang581fd212018-01-10 16:06:12 -0800447
448 // srcs may depend on some genrule output.
449 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800450 j.srcJars = append(j.srcJars, deps.srcJars...)
451
Nan Zhang581fd212018-01-10 16:06:12 -0800452 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800453 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800454
Liz Kammere1ab2502020-09-10 15:29:25 +0000455 if len(j.srcFiles) > 0 {
456 j.sourcepaths = android.PathsForModuleSrc(ctx, []string{"."})
Nan Zhang581fd212018-01-10 16:06:12 -0800457 }
Nan Zhang581fd212018-01-10 16:06:12 -0800458
Colin Crossbc139922021-03-25 18:33:16 -0700459 return deps
460}
461
462func (j *Javadoc) expandArgs(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
463 var argFiles android.Paths
Paul Duffin99e4a502019-02-11 15:38:42 +0000464 argFilesMap := map[string]string{}
465 argFileLabels := []string{}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700466
Paul Duffin99e4a502019-02-11 15:38:42 +0000467 for _, label := range j.properties.Arg_files {
Colin Cross8a497952019-03-05 22:25:09 -0800468 var paths = android.PathsForModuleSrc(ctx, []string{label})
Paul Duffin99e4a502019-02-11 15:38:42 +0000469 if _, exists := argFilesMap[label]; !exists {
Colin Crossbc139922021-03-25 18:33:16 -0700470 argFilesMap[label] = strings.Join(cmd.PathsForInputs(paths), " ")
Paul Duffin99e4a502019-02-11 15:38:42 +0000471 argFileLabels = append(argFileLabels, label)
Colin Crossbc139922021-03-25 18:33:16 -0700472 argFiles = append(argFiles, paths...)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700473 } else {
474 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000475 label, argFilesMap[label], paths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700476 }
477 }
478
Liz Kammer585cac22020-07-06 09:12:57 -0700479 var argsPropertyName string
480 flags := make([]string, 0)
481 if j.properties.Args != nil && j.properties.Flags != nil {
482 ctx.PropertyErrorf("args", "flags is set. Cannot set args")
483 } else if args := proptools.String(j.properties.Args); args != "" {
484 flags = append(flags, args)
485 argsPropertyName = "args"
486 } else {
487 flags = append(flags, j.properties.Flags...)
488 argsPropertyName = "flags"
489 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700490
Liz Kammer585cac22020-07-06 09:12:57 -0700491 for _, flag := range flags {
Colin Crossbc139922021-03-25 18:33:16 -0700492 expanded, err := android.Expand(flag, func(name string) (string, error) {
Liz Kammer585cac22020-07-06 09:12:57 -0700493 if strings.HasPrefix(name, "location ") {
494 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
495 if paths, ok := argFilesMap[label]; ok {
496 return paths, nil
497 } else {
498 return "", fmt.Errorf("unknown location label %q, expecting one of %q",
499 label, strings.Join(argFileLabels, ", "))
500 }
501 } else if name == "genDir" {
502 return android.PathForModuleGen(ctx).String(), nil
503 }
504 return "", fmt.Errorf("unknown variable '$(%s)'", name)
505 })
506
507 if err != nil {
508 ctx.PropertyErrorf(argsPropertyName, "%s", err.Error())
509 }
Colin Crossbc139922021-03-25 18:33:16 -0700510 cmd.Flag(expanded)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700511 }
512
Colin Crossbc139922021-03-25 18:33:16 -0700513 cmd.Implicits(argFiles)
Nan Zhang581fd212018-01-10 16:06:12 -0800514}
515
516func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
517 j.addDeps(ctx)
518}
519
520func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
521 deps := j.collectDeps(ctx)
522
Colin Crossdaa4c672019-07-15 22:53:46 -0700523 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800524
Colin Crossdaa4c672019-07-15 22:53:46 -0700525 outDir := android.PathForModuleOut(ctx, "out")
526 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
527
528 j.stubsSrcJar = nil
529
Colin Crossf1a035e2020-11-16 17:32:30 -0800530 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossdaa4c672019-07-15 22:53:46 -0700531
532 rule.Command().Text("rm -rf").Text(outDir.String())
533 rule.Command().Text("mkdir -p").Text(outDir.String())
534
535 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, j.srcJars)
Nan Zhang357466b2018-04-17 17:38:36 -0700536
Jiyong Parkf1691d22021-03-29 20:11:58 +0900537 javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), android.SdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800538
Colin Crossdaa4c672019-07-15 22:53:46 -0700539 cmd := javadocSystemModulesCmd(ctx, rule, j.srcFiles, outDir, srcJarDir, srcJarList,
540 deps.systemModules, deps.classpath, j.sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800541
Colin Cross1e743852019-10-28 11:37:20 -0700542 cmd.FlagWithArg("-source ", javaVersion.String()).
Colin Crossdaa4c672019-07-15 22:53:46 -0700543 Flag("-J-Xmx1024m").
544 Flag("-XDignore.symbol.file").
545 Flag("-Xdoclint:none")
Nan Zhang581fd212018-01-10 16:06:12 -0800546
Colin Crossbc139922021-03-25 18:33:16 -0700547 j.expandArgs(ctx, cmd)
548
Colin Crossdaa4c672019-07-15 22:53:46 -0700549 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800550 BuiltTool("soong_zip").
Colin Crossdaa4c672019-07-15 22:53:46 -0700551 Flag("-write_if_changed").
552 Flag("-d").
553 FlagWithOutput("-o ", j.docZip).
554 FlagWithArg("-C ", outDir.String()).
555 FlagWithArg("-D ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700556
Colin Crossdaa4c672019-07-15 22:53:46 -0700557 rule.Restat()
558
559 zipSyncCleanupCmd(rule, srcJarDir)
560
Colin Crossf1a035e2020-11-16 17:32:30 -0800561 rule.Build("javadoc", "javadoc")
Nan Zhang581fd212018-01-10 16:06:12 -0800562}
563
Nan Zhanga40da042018-08-01 12:48:00 -0700564//
565// Droiddoc
566//
567type Droiddoc struct {
568 Javadoc
569
Liz Kammere1ab2502020-09-10 15:29:25 +0000570 properties DroiddocProperties
Nan Zhanga40da042018-08-01 12:48:00 -0700571}
572
Colin Crossa3002fc2019-07-08 16:48:04 -0700573// droiddoc converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700574func DroiddocFactory() android.Module {
575 module := &Droiddoc{}
576
577 module.AddProperties(&module.properties,
578 &module.Javadoc.properties)
579
580 InitDroiddocModule(module, android.HostAndDeviceSupported)
581 return module
582}
583
Colin Crossa3002fc2019-07-08 16:48:04 -0700584// droiddoc_host converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700585func DroiddocHostFactory() android.Module {
586 module := &Droiddoc{}
587
588 module.AddProperties(&module.properties,
589 &module.Javadoc.properties)
590
591 InitDroiddocModule(module, android.HostSupported)
592 return module
593}
594
Liz Kammere1ab2502020-09-10 15:29:25 +0000595func (d *Droiddoc) OutputFiles(tag string) (android.Paths, error) {
596 switch tag {
597 case "", ".docs.zip":
598 return android.Paths{d.Javadoc.docZip}, nil
599 default:
600 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
601 }
Nan Zhanga40da042018-08-01 12:48:00 -0700602}
603
Nan Zhang581fd212018-01-10 16:06:12 -0800604func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
605 d.Javadoc.addDeps(ctx)
606
Nan Zhang79614d12018-04-19 18:03:39 -0700607 if String(d.properties.Custom_template) != "" {
Dan Willemsencc090972018-02-26 14:33:31 -0800608 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
609 }
Nan Zhang581fd212018-01-10 16:06:12 -0800610}
611
Colin Crossab054432019-07-15 16:13:59 -0700612func (d *Droiddoc) doclavaDocsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, docletPath classpath) {
Colin Cross2a2e0db2020-02-21 16:55:46 -0800613 buildNumberFile := ctx.Config().BuildNumberFile(ctx)
Nan Zhang443fa522018-08-20 20:58:28 -0700614 // Droiddoc always gets "-source 1.8" because it doesn't support 1.9 sources. For modules with 1.9
615 // sources, droiddoc will get sources produced by metalava which will have already stripped out the
616 // 1.9 language features.
Colin Crossab054432019-07-15 16:13:59 -0700617 cmd.FlagWithArg("-source ", "1.8").
618 Flag("-J-Xmx1600m").
619 Flag("-J-XX:-OmitStackTraceInFastThrow").
620 Flag("-XDignore.symbol.file").
621 FlagWithArg("-doclet ", "com.google.doclava.Doclava").
622 FlagWithInputList("-docletpath ", docletPath.Paths(), ":").
Colin Cross2a2e0db2020-02-21 16:55:46 -0800623 FlagWithArg("-hdf page.build ", ctx.Config().BuildId()+"-$(cat "+buildNumberFile.String()+")").OrderOnly(buildNumberFile).
Elliott Hughes26bce342019-09-12 15:05:13 -0700624 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 -0700625
Nan Zhanga40da042018-08-01 12:48:00 -0700626 if String(d.properties.Custom_template) == "" {
627 // TODO: This is almost always droiddoc-templates-sdk
628 ctx.PropertyErrorf("custom_template", "must specify a template")
629 }
630
631 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
Nan Zhangf4936b02018-08-01 15:00:28 -0700632 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Crossab054432019-07-15 16:13:59 -0700633 cmd.FlagWithArg("-templatedir ", t.dir.String()).Implicits(t.deps)
Nan Zhanga40da042018-08-01 12:48:00 -0700634 } else {
Paul Duffin884363e2019-12-19 10:21:09 +0000635 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_exported_dir", ctx.OtherModuleName(m))
Nan Zhanga40da042018-08-01 12:48:00 -0700636 }
637 })
638
639 if len(d.properties.Html_dirs) > 0 {
Colin Crossab054432019-07-15 16:13:59 -0700640 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
641 cmd.FlagWithArg("-htmldir ", htmlDir.String()).
642 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[0], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700643 }
644
645 if len(d.properties.Html_dirs) > 1 {
Colin Crossab054432019-07-15 16:13:59 -0700646 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
647 cmd.FlagWithArg("-htmldir2 ", htmlDir2.String()).
648 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[1], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700649 }
650
651 if len(d.properties.Html_dirs) > 2 {
652 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
653 }
654
Colin Cross8a497952019-03-05 22:25:09 -0800655 knownTags := android.PathsForModuleSrc(ctx, d.properties.Knowntags)
Colin Crossab054432019-07-15 16:13:59 -0700656 cmd.FlagForEachInput("-knowntags ", knownTags)
Nan Zhanga40da042018-08-01 12:48:00 -0700657
Colin Crossab054432019-07-15 16:13:59 -0700658 cmd.FlagForEachArg("-hdf ", d.properties.Hdf)
Nan Zhanga40da042018-08-01 12:48:00 -0700659
660 if String(d.properties.Proofread_file) != "" {
661 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
Colin Crossab054432019-07-15 16:13:59 -0700662 cmd.FlagWithOutput("-proofread ", proofreadFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700663 }
664
665 if String(d.properties.Todo_file) != "" {
666 // tricky part:
667 // we should not compute full path for todo_file through PathForModuleOut().
668 // the non-standard doclet will get the full path relative to "-o".
Colin Crossab054432019-07-15 16:13:59 -0700669 cmd.FlagWithArg("-todo ", String(d.properties.Todo_file)).
670 ImplicitOutput(android.PathForModuleOut(ctx, String(d.properties.Todo_file)))
Nan Zhanga40da042018-08-01 12:48:00 -0700671 }
672
673 if String(d.properties.Resourcesdir) != "" {
674 // TODO: should we add files under resourcesDir to the implicits? It seems that
675 // resourcesDir is one sub dir of htmlDir
676 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
Colin Crossab054432019-07-15 16:13:59 -0700677 cmd.FlagWithArg("-resourcesdir ", resourcesDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700678 }
679
680 if String(d.properties.Resourcesoutdir) != "" {
681 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
Colin Crossab054432019-07-15 16:13:59 -0700682 cmd.FlagWithArg("-resourcesoutdir ", String(d.properties.Resourcesoutdir))
Nan Zhanga40da042018-08-01 12:48:00 -0700683 }
Nan Zhanga40da042018-08-01 12:48:00 -0700684}
685
Colin Crossab054432019-07-15 16:13:59 -0700686func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700687 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700688 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
689 rule.Command().Text("cp").
690 Input(staticDocIndexRedirect).
691 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700692 }
693
694 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700695 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
696 rule.Command().Text("cp").
697 Input(staticDocProperties).
698 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700699 }
Nan Zhanga40da042018-08-01 12:48:00 -0700700}
701
Colin Crossab054432019-07-15 16:13:59 -0700702func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700703 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700704
705 cmd := rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800706 BuiltTool("soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
Colin Crossab054432019-07-15 16:13:59 -0700707 Flag(config.JavacVmFlags).
708 FlagWithArg("-encoding ", "UTF-8").
Colin Cross70c47412021-03-12 17:48:14 -0800709 FlagWithRspFileInputList("@", android.PathForModuleOut(ctx, "javadoc.rsp"), srcs).
Colin Crossab054432019-07-15 16:13:59 -0700710 FlagWithInput("@", srcJarList)
711
Colin Crossab054432019-07-15 16:13:59 -0700712 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
713 // based stubs generation.
714 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
715 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
716 // the correct package name base path.
717 if len(sourcepaths) > 0 {
718 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
719 } else {
720 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
721 }
722
723 cmd.FlagWithArg("-d ", outDir.String()).
724 Flag("-quiet")
725
726 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700727}
728
Colin Crossdaa4c672019-07-15 22:53:46 -0700729func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
730 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
731 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
732
733 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
734
735 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
736 cmd.Flag(flag).Implicits(deps)
737
738 cmd.FlagWithArg("--patch-module ", "java.base=.")
739
740 if len(classpath) > 0 {
741 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
742 }
743
744 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700745}
746
Colin Crossdaa4c672019-07-15 22:53:46 -0700747func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
748 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
749 sourcepaths android.Paths) *android.RuleBuilderCommand {
750
751 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
752
753 if len(bootclasspath) == 0 && ctx.Device() {
754 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
755 // ensure java does not fall back to the default bootclasspath.
756 cmd.FlagWithArg("-bootclasspath ", `""`)
757 } else if len(bootclasspath) > 0 {
758 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
759 }
760
761 if len(classpath) > 0 {
762 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
763 }
764
765 return cmd
766}
767
Colin Crossab054432019-07-15 16:13:59 -0700768func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
769 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700770
Colin Crossab054432019-07-15 16:13:59 -0700771 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
772 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
773
774 return rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800775 BuiltTool("dokka").
Colin Crossab054432019-07-15 16:13:59 -0700776 Flag(config.JavacVmFlags).
777 Flag(srcJarDir.String()).
778 FlagWithInputList("-classpath ", dokkaClasspath, ":").
779 FlagWithArg("-format ", "dac").
780 FlagWithArg("-dacRoot ", "/reference/kotlin").
781 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700782}
783
784func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
785 deps := d.Javadoc.collectDeps(ctx)
786
Colin Crossdaa4c672019-07-15 22:53:46 -0700787 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Colin Crossdaa4c672019-07-15 22:53:46 -0700788
Nan Zhang1598a9e2018-09-04 17:14:32 -0700789 jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
790 doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700791
Colin Crossab054432019-07-15 16:13:59 -0700792 outDir := android.PathForModuleOut(ctx, "out")
793 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700794
Colin Crossf1a035e2020-11-16 17:32:30 -0800795 rule := android.NewRuleBuilder(pctx, ctx)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700796
Colin Crossab054432019-07-15 16:13:59 -0700797 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
798
799 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -0700800 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -0700801 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700802 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -0700803 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -0700804 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700805 }
806
Colin Crossbc139922021-03-25 18:33:16 -0700807 d.expandArgs(ctx, cmd)
Colin Crossab054432019-07-15 16:13:59 -0700808
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000809 if d.properties.Compat_config != nil {
810 compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
811 cmd.FlagWithInput("-compatconfig ", compatConfig)
812 }
813
Colin Crossab054432019-07-15 16:13:59 -0700814 var desc string
815 if Bool(d.properties.Dokka_enabled) {
816 desc = "dokka"
817 } else {
818 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
819
820 for _, o := range d.Javadoc.properties.Out {
821 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
822 }
823
824 d.postDoclavaCmds(ctx, rule)
825 desc = "doclava"
826 }
827
828 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800829 BuiltTool("soong_zip").
Colin Crossab054432019-07-15 16:13:59 -0700830 Flag("-write_if_changed").
831 Flag("-d").
832 FlagWithOutput("-o ", d.docZip).
833 FlagWithArg("-C ", outDir.String()).
834 FlagWithArg("-D ", outDir.String())
835
Colin Crossab054432019-07-15 16:13:59 -0700836 rule.Restat()
837
838 zipSyncCleanupCmd(rule, srcJarDir)
839
Colin Crossf1a035e2020-11-16 17:32:30 -0800840 rule.Build("javadoc", desc)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700841}
842
843//
Nan Zhangf4936b02018-08-01 15:00:28 -0700844// Exported Droiddoc Directory
Nan Zhanga40da042018-08-01 12:48:00 -0700845//
Dan Willemsencc090972018-02-26 14:33:31 -0800846var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
847
Nan Zhangf4936b02018-08-01 15:00:28 -0700848type ExportedDroiddocDirProperties struct {
849 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -0800850 Path *string
851}
852
Nan Zhangf4936b02018-08-01 15:00:28 -0700853type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -0800854 android.ModuleBase
855
Nan Zhangf4936b02018-08-01 15:00:28 -0700856 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -0800857
858 deps android.Paths
859 dir android.Path
860}
861
Colin Crossa3002fc2019-07-08 16:48:04 -0700862// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -0700863func ExportedDroiddocDirFactory() android.Module {
864 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -0800865 module.AddProperties(&module.properties)
866 android.InitAndroidModule(module)
867 return module
868}
869
Nan Zhangf4936b02018-08-01 15:00:28 -0700870func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -0800871
Nan Zhangf4936b02018-08-01 15:00:28 -0700872func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -0800873 path := String(d.properties.Path)
874 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -0800875 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Dan Willemsencc090972018-02-26 14:33:31 -0800876}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800877
878//
879// Defaults
880//
881type DocDefaults struct {
882 android.ModuleBase
883 android.DefaultsModuleBase
884}
885
Nan Zhangb2b33de2018-02-23 11:18:47 -0800886func DocDefaultsFactory() android.Module {
887 module := &DocDefaults{}
888
889 module.AddProperties(
890 &JavadocProperties{},
891 &DroiddocProperties{},
892 )
893
894 android.InitDefaultsModule(module)
895
896 return module
897}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700898
Colin Cross33961b52019-07-11 11:01:22 -0700899func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
900 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
901
Colin Cross1661aff2021-03-12 17:56:51 -0800902 cmd := rule.Command()
903 cmd.Text("rm -rf").Text(cmd.PathForOutput(srcJarDir))
904 cmd = rule.Command()
905 cmd.Text("mkdir -p").Text(cmd.PathForOutput(srcJarDir))
Colin Cross33961b52019-07-11 11:01:22 -0700906 srcJarList := srcJarDir.Join(ctx, "list")
907
908 rule.Temporary(srcJarList)
909
Colin Cross1661aff2021-03-12 17:56:51 -0800910 cmd = rule.Command()
911 cmd.BuiltTool("zipsync").
912 FlagWithArg("-d ", cmd.PathForOutput(srcJarDir)).
Colin Cross33961b52019-07-11 11:01:22 -0700913 FlagWithOutput("-l ", srcJarList).
914 FlagWithArg("-f ", `"*.java"`).
915 Inputs(srcJars)
916
917 return srcJarList
918}
919
920func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
921 rule.Command().Text("rm -rf").Text(srcJarDir.String())
922}