blob: f7595b1fc2e48eca33fd5b075cbca24d5e9d9858 [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
Ramy Medhat2f99eec2020-06-13 17:38:27 -0400101
102 // If set, metalava is sandboxed to only read files explicitly specified on the command
103 // line. Defaults to false.
104 Sandbox *bool
Nan Zhang581fd212018-01-10 16:06:12 -0800105}
106
Nan Zhang61819ce2018-05-04 18:49:16 -0700107type ApiToCheck struct {
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900108 // path to the API txt file that the new API extracted from source code is checked
109 // against. The path can be local to the module or from other module (via :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800110 Api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700111
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900112 // path to the API txt file that the new @removed API extractd from source code is
113 // checked against. The path can be local to the module or from other module (via
114 // :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800115 Removed_api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700116
Adrian Roos14f75a92019-08-12 17:54:09 +0200117 // If not blank, path to the baseline txt file for approved API check violations.
118 Baseline_file *string `android:"path"`
119
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900120 // Arguments to the apicheck tool.
Nan Zhang61819ce2018-05-04 18:49:16 -0700121 Args *string
122}
123
Nan Zhang581fd212018-01-10 16:06:12 -0800124type DroiddocProperties struct {
125 // directory relative to top of the source tree that contains doc templates files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800126 Custom_template *string
Nan Zhang581fd212018-01-10 16:06:12 -0800127
Nan Zhanga40da042018-08-01 12:48:00 -0700128 // directories under current module source which contains html/jd files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800129 Html_dirs []string
Nan Zhang581fd212018-01-10 16:06:12 -0800130
131 // set a value in the Clearsilver hdf namespace.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800132 Hdf []string
Nan Zhang581fd212018-01-10 16:06:12 -0800133
134 // proofread file contains all of the text content of the javadocs concatenated into one file,
135 // suitable for spell-checking and other goodness.
Colin Crossab054432019-07-15 16:13:59 -0700136 Proofread_file *string
Nan Zhang581fd212018-01-10 16:06:12 -0800137
138 // a todo file lists the program elements that are missing documentation.
139 // At some point, this might be improved to show more warnings.
Colin Cross27b922f2019-03-04 22:35:41 -0800140 Todo_file *string `android:"path"`
Nan Zhangb2b33de2018-02-23 11:18:47 -0800141
142 // directory under current module source that provide additional resources (images).
143 Resourcesdir *string
144
145 // resources output directory under out/soong/.intermediates.
146 Resourcesoutdir *string
Nan Zhang581fd212018-01-10 16:06:12 -0800147
Nan Zhange2ba5d42018-07-11 15:16:55 -0700148 // index.html under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800149 Static_doc_index_redirect *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700150
151 // source.properties under current module will be copied to docs out dir, if not null.
Colin Cross27b922f2019-03-04 22:35:41 -0800152 Static_doc_properties *string `android:"path"`
Nan Zhange2ba5d42018-07-11 15:16:55 -0700153
Nan Zhang581fd212018-01-10 16:06:12 -0800154 // a list of files under current module source dir which contains known tags in Java sources.
155 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -0800156 Knowntags []string `android:"path"`
Nan Zhang28c68b92018-03-13 16:17:01 -0700157
Nan Zhang1598a9e2018-09-04 17:14:32 -0700158 // if set to true, generate docs through Dokka instead of Doclava.
159 Dokka_enabled *bool
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000160
161 // Compat config XML. Generates compat change documentation if set.
162 Compat_config *string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -0700163}
164
Nan Zhanga40da042018-08-01 12:48:00 -0700165//
166// Common flags passed down to build rule
167//
168type droiddocBuilderFlags struct {
Nan Zhang86d2d552018-08-09 15:33:27 -0700169 bootClasspathArgs string
170 classpathArgs string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700171 sourcepathArgs string
Nan Zhang86d2d552018-08-09 15:33:27 -0700172 dokkaClasspathArgs string
173 aidlFlags string
Colin Cross3047fa22019-04-18 10:56:44 -0700174 aidlDeps android.Paths
Nan Zhanga40da042018-08-01 12:48:00 -0700175
Nan Zhanga40da042018-08-01 12:48:00 -0700176 doclavaStubsFlags string
Nan Zhang86d2d552018-08-09 15:33:27 -0700177 doclavaDocsFlags string
Nan Zhanga40da042018-08-01 12:48:00 -0700178 postDoclavaCmds string
Nan Zhanga40da042018-08-01 12:48:00 -0700179}
180
181func InitDroiddocModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
182 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
183 android.InitDefaultableModule(module)
184}
185
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200186func apiCheckEnabled(ctx android.ModuleContext, apiToCheck ApiToCheck, apiVersionTag string) bool {
187 if ctx.Config().IsEnvTrue("WITHOUT_CHECK_API") {
188 return false
189 } else if String(apiToCheck.Api_file) != "" && String(apiToCheck.Removed_api_file) != "" {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700190 return true
191 } else if String(apiToCheck.Api_file) != "" {
192 panic("for " + apiVersionTag + " removed_api_file has to be non-empty!")
193 } else if String(apiToCheck.Removed_api_file) != "" {
194 panic("for " + apiVersionTag + " api_file has to be non-empty!")
195 }
196
197 return false
198}
199
Paul Duffin3d1248c2020-04-09 00:10:17 +0100200// Used by xsd_config
Nan Zhang1598a9e2018-09-04 17:14:32 -0700201type ApiFilePath interface {
202 ApiFilePath() android.Path
203}
204
Paul Duffin0f8faff2020-05-20 16:18:00 +0100205type ApiStubsSrcProvider interface {
206 StubsSrcJar() android.Path
207}
208
Paul Duffin3d1248c2020-04-09 00:10:17 +0100209// Provider of information about API stubs, used by java_sdk_library.
210type ApiStubsProvider interface {
211 ApiFilePath
Paul Duffin1fd005d2020-04-09 01:08:11 +0100212 RemovedApiFilePath() android.Path
Paul Duffin0f8faff2020-05-20 16:18:00 +0100213
214 ApiStubsSrcProvider
Paul Duffin3d1248c2020-04-09 00:10:17 +0100215}
216
Nan Zhanga40da042018-08-01 12:48:00 -0700217//
218// Javadoc
219//
Nan Zhang581fd212018-01-10 16:06:12 -0800220type Javadoc struct {
221 android.ModuleBase
222 android.DefaultableModuleBase
223
224 properties JavadocProperties
225
226 srcJars android.Paths
227 srcFiles android.Paths
228 sourcepaths android.Paths
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400229 implicits android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700230
Nan Zhangccff0f72018-03-08 17:26:16 -0800231 docZip android.WritablePath
232 stubsSrcJar android.WritablePath
Nan Zhang581fd212018-01-10 16:06:12 -0800233}
234
Colin Cross41955e82019-05-29 14:40:35 -0700235func (j *Javadoc) OutputFiles(tag string) (android.Paths, error) {
236 switch tag {
237 case "":
238 return android.Paths{j.stubsSrcJar}, nil
Colin Crosse68e5542019-08-12 13:11:40 -0700239 case ".docs.zip":
240 return android.Paths{j.docZip}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700241 default:
242 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
243 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800244}
245
Colin Crossa3002fc2019-07-08 16:48:04 -0700246// javadoc converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800247func JavadocFactory() android.Module {
248 module := &Javadoc{}
249
250 module.AddProperties(&module.properties)
251
252 InitDroiddocModule(module, android.HostAndDeviceSupported)
253 return module
254}
255
Colin Crossa3002fc2019-07-08 16:48:04 -0700256// javadoc_host converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800257func JavadocHostFactory() android.Module {
258 module := &Javadoc{}
259
260 module.AddProperties(&module.properties)
261
262 InitDroiddocModule(module, android.HostSupported)
263 return module
264}
265
Colin Cross41955e82019-05-29 14:40:35 -0700266var _ android.OutputFileProducer = (*Javadoc)(nil)
Nan Zhang581fd212018-01-10 16:06:12 -0800267
Jiyong Park6a927c42020-01-21 02:03:43 +0900268func (j *Javadoc) sdkVersion() sdkSpec {
269 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700270}
271
Paul Duffine25c6442019-10-11 13:50:28 +0100272func (j *Javadoc) systemModules() string {
273 return proptools.String(j.properties.System_modules)
274}
275
Jiyong Park6a927c42020-01-21 02:03:43 +0900276func (j *Javadoc) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700277 return j.sdkVersion()
278}
279
Jiyong Park6a927c42020-01-21 02:03:43 +0900280func (j *Javadoc) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700281 return j.sdkVersion()
282}
283
Nan Zhang581fd212018-01-10 16:06:12 -0800284func (j *Javadoc) addDeps(ctx android.BottomUpMutatorContext) {
285 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100286 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Pete Gilline3d44b22020-06-29 11:28:51 +0100287 if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700288 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100289 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700290 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Pete Gilline3d44b22020-06-29 11:28:51 +0100291 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
Nan Zhang581fd212018-01-10 16:06:12 -0800292 }
293 }
294
Colin Cross42d48b72018-08-29 14:10:52 -0700295 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800296}
297
Nan Zhanga40da042018-08-01 12:48:00 -0700298func (j *Javadoc) collectAidlFlags(ctx android.ModuleContext, deps deps) droiddocBuilderFlags {
299 var flags droiddocBuilderFlags
Jiyong Park1e440682018-05-23 18:42:04 +0900300
Colin Cross3047fa22019-04-18 10:56:44 -0700301 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Jiyong Park1e440682018-05-23 18:42:04 +0900302
303 return flags
304}
305
306func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700307 aidlIncludeDirs android.Paths) (string, android.Paths) {
Jiyong Park1e440682018-05-23 18:42:04 +0900308
309 aidlIncludes := android.PathsForModuleSrc(ctx, j.properties.Aidl.Local_include_dirs)
310 aidlIncludes = append(aidlIncludes, android.PathsForSource(ctx, j.properties.Aidl.Include_dirs)...)
311
312 var flags []string
Colin Cross3047fa22019-04-18 10:56:44 -0700313 var deps android.Paths
314
Jiyong Park1e440682018-05-23 18:42:04 +0900315 if aidlPreprocess.Valid() {
316 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700317 deps = append(deps, aidlPreprocess.Path())
Jiyong Park1e440682018-05-23 18:42:04 +0900318 } else {
319 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
320 }
321
322 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
323 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
324 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
325 flags = append(flags, "-I"+src.String())
326 }
327
Colin Cross3047fa22019-04-18 10:56:44 -0700328 return strings.Join(flags, " "), deps
Jiyong Park1e440682018-05-23 18:42:04 +0900329}
330
Jiyong Parkd90d7412019-08-20 22:49:19 +0900331// TODO: remove the duplication between this and the one in gen.go
Jiyong Park1e440682018-05-23 18:42:04 +0900332func (j *Javadoc) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Nan Zhanga40da042018-08-01 12:48:00 -0700333 flags droiddocBuilderFlags) android.Paths {
Jiyong Park1e440682018-05-23 18:42:04 +0900334
335 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Crossc0806172019-06-14 18:51:47 -0700336 var aidlSrcs android.Paths
Jiyong Park1e440682018-05-23 18:42:04 +0900337
Jiyong Park1112c4c2019-08-16 21:12:10 +0900338 aidlIncludeFlags := genAidlIncludeFlags(srcFiles)
339
Jiyong Park1e440682018-05-23 18:42:04 +0900340 for _, srcFile := range srcFiles {
341 switch srcFile.Ext() {
342 case ".aidl":
Colin Crossc0806172019-06-14 18:51:47 -0700343 aidlSrcs = append(aidlSrcs, srcFile)
Jiyong Parkd90d7412019-08-20 22:49:19 +0900344 case ".logtags":
345 javaFile := genLogtags(ctx, srcFile)
346 outSrcFiles = append(outSrcFiles, javaFile)
Jiyong Park1e440682018-05-23 18:42:04 +0900347 default:
348 outSrcFiles = append(outSrcFiles, srcFile)
349 }
350 }
351
Colin Crossc0806172019-06-14 18:51:47 -0700352 // Process all aidl files together to support sharding them into one or more rules that produce srcjars.
353 if len(aidlSrcs) > 0 {
354 srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags+aidlIncludeFlags, flags.aidlDeps)
355 outSrcFiles = append(outSrcFiles, srcJarFiles...)
356 }
357
Jiyong Park1e440682018-05-23 18:42:04 +0900358 return outSrcFiles
359}
360
Nan Zhang581fd212018-01-10 16:06:12 -0800361func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
362 var deps deps
363
Colin Cross83bb3162018-06-25 15:48:06 -0700364 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800365 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700366 ctx.AddMissingDependencies(sdkDep.bootclasspath)
367 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Nan Zhang581fd212018-01-10 16:06:12 -0800368 } else if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700369 deps.bootClasspath = append(deps.bootClasspath, sdkDep.jars...)
Anton Hansson26bf49b2020-02-08 20:26:29 +0000370 deps.aidlPreprocess = sdkDep.aidl
371 } else {
372 deps.aidlPreprocess = sdkDep.aidl
Nan Zhang581fd212018-01-10 16:06:12 -0800373 }
374
375 ctx.VisitDirectDeps(func(module android.Module) {
376 otherName := ctx.OtherModuleName(module)
377 tag := ctx.OtherModuleDependencyTag(module)
378
Colin Cross2d24c1b2018-05-23 10:59:18 -0700379 switch tag {
380 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -0800381 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
382 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
383 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars...)
Paul Duffin83a2d962019-11-19 19:44:10 +0000384 } else if sm, ok := module.(SystemModulesProvider); ok {
Paul Duffine25c6442019-10-11 13:50:28 +0100385 // A system modules dependency has been added to the bootclasspath
386 // so add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +0000387 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700388 } else {
389 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
390 }
391 case libTag:
Colin Crossdcf71b22021-02-01 13:59:03 -0800392 if dep, ok := module.(SdkLibraryDependency); ok {
Paul Duffin649dadf2020-05-26 11:42:13 +0100393 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
Colin Crossdcf71b22021-02-01 13:59:03 -0800394 } else if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
395 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
396 deps.classpath = append(deps.classpath, dep.HeaderJars...)
397 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs...)
398 } else if dep, ok := module.(android.SourceFileProducer); ok {
Nan Zhang581fd212018-01-10 16:06:12 -0800399 checkProducesJars(ctx, dep)
400 deps.classpath = append(deps.classpath, dep.Srcs()...)
Colin Crossdcf71b22021-02-01 13:59:03 -0800401 } else {
Nan Zhang581fd212018-01-10 16:06:12 -0800402 ctx.ModuleErrorf("depends on non-java module %q", otherName)
403 }
Colin Cross6cef4812019-10-17 14:23:50 -0700404 case java9LibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -0800405 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
406 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
407 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars...)
408 } else {
Colin Cross6cef4812019-10-17 14:23:50 -0700409 ctx.ModuleErrorf("depends on non-java module %q", otherName)
410 }
Nan Zhang357466b2018-04-17 17:38:36 -0700411 case systemModulesTag:
412 if deps.systemModules != nil {
413 panic("Found two system module dependencies")
414 }
Paul Duffin83a2d962019-11-19 19:44:10 +0000415 sm := module.(SystemModulesProvider)
416 outputDir, outputDeps := sm.OutputDirAndDeps()
417 deps.systemModules = &systemModules{outputDir, outputDeps}
Nan Zhang581fd212018-01-10 16:06:12 -0800418 }
419 })
420 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
421 // may contain filegroup or genrule.
Colin Cross8a497952019-03-05 22:25:09 -0800422 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400423 j.implicits = append(j.implicits, srcFiles...)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900424
425 filterByPackage := func(srcs []android.Path, filterPackages []string) []android.Path {
426 if filterPackages == nil {
427 return srcs
428 }
429 filtered := []android.Path{}
430 for _, src := range srcs {
431 if src.Ext() != ".java" {
432 // Don't filter-out non-Java (=generated sources) by package names. This is not ideal,
433 // but otherwise metalava emits stub sources having references to the generated AIDL classes
434 // in filtered-out pacages (e.g. com.android.internal.*).
435 // TODO(b/141149570) We need to fix this by introducing default private constructors or
436 // fixing metalava to not emit constructors having references to unknown classes.
437 filtered = append(filtered, src)
438 continue
439 }
440 packageName := strings.ReplaceAll(filepath.Dir(src.Rel()), "/", ".")
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800441 if android.HasAnyPrefix(packageName, filterPackages) {
442 filtered = append(filtered, src)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900443 }
444 }
445 return filtered
446 }
447 srcFiles = filterByPackage(srcFiles, j.properties.Filter_packages)
448
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400449 // While metalava needs package html files, it does not need them to be explicit on the command
Anton Hansson746be9c2020-10-08 19:05:40 +0100450 // line. javadoc complains if it receives html files on the command line. The filter
451 // below excludes html files from the rsp file metalava. Note that the html
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400452 // files are still included as implicit inputs for successful remote execution and correct
453 // incremental builds.
454 filterHtml := func(srcs []android.Path) []android.Path {
455 filtered := []android.Path{}
456 for _, src := range srcs {
457 if src.Ext() == ".html" {
458 continue
459 }
460 filtered = append(filtered, src)
461 }
462 return filtered
463 }
464 srcFiles = filterHtml(srcFiles)
465
Liz Kammer585cac22020-07-06 09:12:57 -0700466 aidlFlags := j.collectAidlFlags(ctx, deps)
467 srcFiles = j.genSources(ctx, srcFiles, aidlFlags)
Nan Zhang581fd212018-01-10 16:06:12 -0800468
469 // srcs may depend on some genrule output.
470 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800471 j.srcJars = append(j.srcJars, deps.srcJars...)
472
Nan Zhang581fd212018-01-10 16:06:12 -0800473 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800474 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800475
Liz Kammere1ab2502020-09-10 15:29:25 +0000476 if len(j.srcFiles) > 0 {
477 j.sourcepaths = android.PathsForModuleSrc(ctx, []string{"."})
Nan Zhang581fd212018-01-10 16:06:12 -0800478 }
Nan Zhang581fd212018-01-10 16:06:12 -0800479
Colin Crossbc139922021-03-25 18:33:16 -0700480 return deps
481}
482
483func (j *Javadoc) expandArgs(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
484 var argFiles android.Paths
Paul Duffin99e4a502019-02-11 15:38:42 +0000485 argFilesMap := map[string]string{}
486 argFileLabels := []string{}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700487
Paul Duffin99e4a502019-02-11 15:38:42 +0000488 for _, label := range j.properties.Arg_files {
Colin Cross8a497952019-03-05 22:25:09 -0800489 var paths = android.PathsForModuleSrc(ctx, []string{label})
Paul Duffin99e4a502019-02-11 15:38:42 +0000490 if _, exists := argFilesMap[label]; !exists {
Colin Crossbc139922021-03-25 18:33:16 -0700491 argFilesMap[label] = strings.Join(cmd.PathsForInputs(paths), " ")
Paul Duffin99e4a502019-02-11 15:38:42 +0000492 argFileLabels = append(argFileLabels, label)
Colin Crossbc139922021-03-25 18:33:16 -0700493 argFiles = append(argFiles, paths...)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700494 } else {
495 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000496 label, argFilesMap[label], paths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700497 }
498 }
499
Liz Kammer585cac22020-07-06 09:12:57 -0700500 var argsPropertyName string
501 flags := make([]string, 0)
502 if j.properties.Args != nil && j.properties.Flags != nil {
503 ctx.PropertyErrorf("args", "flags is set. Cannot set args")
504 } else if args := proptools.String(j.properties.Args); args != "" {
505 flags = append(flags, args)
506 argsPropertyName = "args"
507 } else {
508 flags = append(flags, j.properties.Flags...)
509 argsPropertyName = "flags"
510 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700511
Liz Kammer585cac22020-07-06 09:12:57 -0700512 for _, flag := range flags {
Colin Crossbc139922021-03-25 18:33:16 -0700513 expanded, err := android.Expand(flag, func(name string) (string, error) {
Liz Kammer585cac22020-07-06 09:12:57 -0700514 if strings.HasPrefix(name, "location ") {
515 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
516 if paths, ok := argFilesMap[label]; ok {
517 return paths, nil
518 } else {
519 return "", fmt.Errorf("unknown location label %q, expecting one of %q",
520 label, strings.Join(argFileLabels, ", "))
521 }
522 } else if name == "genDir" {
523 return android.PathForModuleGen(ctx).String(), nil
524 }
525 return "", fmt.Errorf("unknown variable '$(%s)'", name)
526 })
527
528 if err != nil {
529 ctx.PropertyErrorf(argsPropertyName, "%s", err.Error())
530 }
Colin Crossbc139922021-03-25 18:33:16 -0700531 cmd.Flag(expanded)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700532 }
533
Colin Crossbc139922021-03-25 18:33:16 -0700534 cmd.Implicits(argFiles)
Nan Zhang581fd212018-01-10 16:06:12 -0800535}
536
537func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
538 j.addDeps(ctx)
539}
540
541func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
542 deps := j.collectDeps(ctx)
543
Colin Crossdaa4c672019-07-15 22:53:46 -0700544 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800545
Colin Crossdaa4c672019-07-15 22:53:46 -0700546 outDir := android.PathForModuleOut(ctx, "out")
547 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
548
549 j.stubsSrcJar = nil
550
Colin Crossf1a035e2020-11-16 17:32:30 -0800551 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossdaa4c672019-07-15 22:53:46 -0700552
553 rule.Command().Text("rm -rf").Text(outDir.String())
554 rule.Command().Text("mkdir -p").Text(outDir.String())
555
556 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, j.srcJars)
Nan Zhang357466b2018-04-17 17:38:36 -0700557
Colin Cross83bb3162018-06-25 15:48:06 -0700558 javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800559
Colin Crossdaa4c672019-07-15 22:53:46 -0700560 cmd := javadocSystemModulesCmd(ctx, rule, j.srcFiles, outDir, srcJarDir, srcJarList,
561 deps.systemModules, deps.classpath, j.sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800562
Colin Cross1e743852019-10-28 11:37:20 -0700563 cmd.FlagWithArg("-source ", javaVersion.String()).
Colin Crossdaa4c672019-07-15 22:53:46 -0700564 Flag("-J-Xmx1024m").
565 Flag("-XDignore.symbol.file").
566 Flag("-Xdoclint:none")
Nan Zhang581fd212018-01-10 16:06:12 -0800567
Colin Crossbc139922021-03-25 18:33:16 -0700568 j.expandArgs(ctx, cmd)
569
Colin Crossdaa4c672019-07-15 22:53:46 -0700570 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800571 BuiltTool("soong_zip").
Colin Crossdaa4c672019-07-15 22:53:46 -0700572 Flag("-write_if_changed").
573 Flag("-d").
574 FlagWithOutput("-o ", j.docZip).
575 FlagWithArg("-C ", outDir.String()).
576 FlagWithArg("-D ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700577
Colin Crossdaa4c672019-07-15 22:53:46 -0700578 rule.Restat()
579
580 zipSyncCleanupCmd(rule, srcJarDir)
581
Colin Crossf1a035e2020-11-16 17:32:30 -0800582 rule.Build("javadoc", "javadoc")
Nan Zhang581fd212018-01-10 16:06:12 -0800583}
584
Nan Zhanga40da042018-08-01 12:48:00 -0700585//
586// Droiddoc
587//
588type Droiddoc struct {
589 Javadoc
590
Liz Kammere1ab2502020-09-10 15:29:25 +0000591 properties DroiddocProperties
Nan Zhanga40da042018-08-01 12:48:00 -0700592}
593
Colin Crossa3002fc2019-07-08 16:48:04 -0700594// droiddoc converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700595func DroiddocFactory() android.Module {
596 module := &Droiddoc{}
597
598 module.AddProperties(&module.properties,
599 &module.Javadoc.properties)
600
601 InitDroiddocModule(module, android.HostAndDeviceSupported)
602 return module
603}
604
Colin Crossa3002fc2019-07-08 16:48:04 -0700605// droiddoc_host converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700606func DroiddocHostFactory() android.Module {
607 module := &Droiddoc{}
608
609 module.AddProperties(&module.properties,
610 &module.Javadoc.properties)
611
612 InitDroiddocModule(module, android.HostSupported)
613 return module
614}
615
Liz Kammere1ab2502020-09-10 15:29:25 +0000616func (d *Droiddoc) OutputFiles(tag string) (android.Paths, error) {
617 switch tag {
618 case "", ".docs.zip":
619 return android.Paths{d.Javadoc.docZip}, nil
620 default:
621 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
622 }
Nan Zhanga40da042018-08-01 12:48:00 -0700623}
624
Nan Zhang581fd212018-01-10 16:06:12 -0800625func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
626 d.Javadoc.addDeps(ctx)
627
Nan Zhang79614d12018-04-19 18:03:39 -0700628 if String(d.properties.Custom_template) != "" {
Dan Willemsencc090972018-02-26 14:33:31 -0800629 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
630 }
Nan Zhang581fd212018-01-10 16:06:12 -0800631}
632
Colin Crossab054432019-07-15 16:13:59 -0700633func (d *Droiddoc) doclavaDocsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, docletPath classpath) {
Colin Cross2a2e0db2020-02-21 16:55:46 -0800634 buildNumberFile := ctx.Config().BuildNumberFile(ctx)
Nan Zhang443fa522018-08-20 20:58:28 -0700635 // Droiddoc always gets "-source 1.8" because it doesn't support 1.9 sources. For modules with 1.9
636 // sources, droiddoc will get sources produced by metalava which will have already stripped out the
637 // 1.9 language features.
Colin Crossab054432019-07-15 16:13:59 -0700638 cmd.FlagWithArg("-source ", "1.8").
639 Flag("-J-Xmx1600m").
640 Flag("-J-XX:-OmitStackTraceInFastThrow").
641 Flag("-XDignore.symbol.file").
642 FlagWithArg("-doclet ", "com.google.doclava.Doclava").
643 FlagWithInputList("-docletpath ", docletPath.Paths(), ":").
Colin Cross2a2e0db2020-02-21 16:55:46 -0800644 FlagWithArg("-hdf page.build ", ctx.Config().BuildId()+"-$(cat "+buildNumberFile.String()+")").OrderOnly(buildNumberFile).
Elliott Hughes26bce342019-09-12 15:05:13 -0700645 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 -0700646
Nan Zhanga40da042018-08-01 12:48:00 -0700647 if String(d.properties.Custom_template) == "" {
648 // TODO: This is almost always droiddoc-templates-sdk
649 ctx.PropertyErrorf("custom_template", "must specify a template")
650 }
651
652 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
Nan Zhangf4936b02018-08-01 15:00:28 -0700653 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Crossab054432019-07-15 16:13:59 -0700654 cmd.FlagWithArg("-templatedir ", t.dir.String()).Implicits(t.deps)
Nan Zhanga40da042018-08-01 12:48:00 -0700655 } else {
Paul Duffin884363e2019-12-19 10:21:09 +0000656 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_exported_dir", ctx.OtherModuleName(m))
Nan Zhanga40da042018-08-01 12:48:00 -0700657 }
658 })
659
660 if len(d.properties.Html_dirs) > 0 {
Colin Crossab054432019-07-15 16:13:59 -0700661 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
662 cmd.FlagWithArg("-htmldir ", htmlDir.String()).
663 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[0], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700664 }
665
666 if len(d.properties.Html_dirs) > 1 {
Colin Crossab054432019-07-15 16:13:59 -0700667 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
668 cmd.FlagWithArg("-htmldir2 ", htmlDir2.String()).
669 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[1], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700670 }
671
672 if len(d.properties.Html_dirs) > 2 {
673 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
674 }
675
Colin Cross8a497952019-03-05 22:25:09 -0800676 knownTags := android.PathsForModuleSrc(ctx, d.properties.Knowntags)
Colin Crossab054432019-07-15 16:13:59 -0700677 cmd.FlagForEachInput("-knowntags ", knownTags)
Nan Zhanga40da042018-08-01 12:48:00 -0700678
Colin Crossab054432019-07-15 16:13:59 -0700679 cmd.FlagForEachArg("-hdf ", d.properties.Hdf)
Nan Zhanga40da042018-08-01 12:48:00 -0700680
681 if String(d.properties.Proofread_file) != "" {
682 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
Colin Crossab054432019-07-15 16:13:59 -0700683 cmd.FlagWithOutput("-proofread ", proofreadFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700684 }
685
686 if String(d.properties.Todo_file) != "" {
687 // tricky part:
688 // we should not compute full path for todo_file through PathForModuleOut().
689 // the non-standard doclet will get the full path relative to "-o".
Colin Crossab054432019-07-15 16:13:59 -0700690 cmd.FlagWithArg("-todo ", String(d.properties.Todo_file)).
691 ImplicitOutput(android.PathForModuleOut(ctx, String(d.properties.Todo_file)))
Nan Zhanga40da042018-08-01 12:48:00 -0700692 }
693
694 if String(d.properties.Resourcesdir) != "" {
695 // TODO: should we add files under resourcesDir to the implicits? It seems that
696 // resourcesDir is one sub dir of htmlDir
697 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
Colin Crossab054432019-07-15 16:13:59 -0700698 cmd.FlagWithArg("-resourcesdir ", resourcesDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700699 }
700
701 if String(d.properties.Resourcesoutdir) != "" {
702 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
Colin Crossab054432019-07-15 16:13:59 -0700703 cmd.FlagWithArg("-resourcesoutdir ", String(d.properties.Resourcesoutdir))
Nan Zhanga40da042018-08-01 12:48:00 -0700704 }
Nan Zhanga40da042018-08-01 12:48:00 -0700705}
706
Colin Crossab054432019-07-15 16:13:59 -0700707func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700708 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700709 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
710 rule.Command().Text("cp").
711 Input(staticDocIndexRedirect).
712 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700713 }
714
715 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700716 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
717 rule.Command().Text("cp").
718 Input(staticDocProperties).
719 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700720 }
Nan Zhanga40da042018-08-01 12:48:00 -0700721}
722
Colin Crossab054432019-07-15 16:13:59 -0700723func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700724 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700725
726 cmd := rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800727 BuiltTool("soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
Colin Crossab054432019-07-15 16:13:59 -0700728 Flag(config.JavacVmFlags).
729 FlagWithArg("-encoding ", "UTF-8").
Colin Cross70c47412021-03-12 17:48:14 -0800730 FlagWithRspFileInputList("@", android.PathForModuleOut(ctx, "javadoc.rsp"), srcs).
Colin Crossab054432019-07-15 16:13:59 -0700731 FlagWithInput("@", srcJarList)
732
Colin Crossab054432019-07-15 16:13:59 -0700733 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
734 // based stubs generation.
735 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
736 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
737 // the correct package name base path.
738 if len(sourcepaths) > 0 {
739 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
740 } else {
741 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
742 }
743
744 cmd.FlagWithArg("-d ", outDir.String()).
745 Flag("-quiet")
746
747 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700748}
749
Colin Crossdaa4c672019-07-15 22:53:46 -0700750func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
751 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
752 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
753
754 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
755
756 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
757 cmd.Flag(flag).Implicits(deps)
758
759 cmd.FlagWithArg("--patch-module ", "java.base=.")
760
761 if len(classpath) > 0 {
762 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
763 }
764
765 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700766}
767
Colin Crossdaa4c672019-07-15 22:53:46 -0700768func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
769 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
770 sourcepaths android.Paths) *android.RuleBuilderCommand {
771
772 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
773
774 if len(bootclasspath) == 0 && ctx.Device() {
775 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
776 // ensure java does not fall back to the default bootclasspath.
777 cmd.FlagWithArg("-bootclasspath ", `""`)
778 } else if len(bootclasspath) > 0 {
779 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
780 }
781
782 if len(classpath) > 0 {
783 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
784 }
785
786 return cmd
787}
788
Colin Crossab054432019-07-15 16:13:59 -0700789func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
790 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700791
Colin Crossab054432019-07-15 16:13:59 -0700792 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
793 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
794
795 return rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800796 BuiltTool("dokka").
Colin Crossab054432019-07-15 16:13:59 -0700797 Flag(config.JavacVmFlags).
798 Flag(srcJarDir.String()).
799 FlagWithInputList("-classpath ", dokkaClasspath, ":").
800 FlagWithArg("-format ", "dac").
801 FlagWithArg("-dacRoot ", "/reference/kotlin").
802 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700803}
804
805func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
806 deps := d.Javadoc.collectDeps(ctx)
807
Colin Crossdaa4c672019-07-15 22:53:46 -0700808 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Colin Crossdaa4c672019-07-15 22:53:46 -0700809
Nan Zhang1598a9e2018-09-04 17:14:32 -0700810 jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
811 doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700812
Colin Crossab054432019-07-15 16:13:59 -0700813 outDir := android.PathForModuleOut(ctx, "out")
814 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700815
Colin Crossf1a035e2020-11-16 17:32:30 -0800816 rule := android.NewRuleBuilder(pctx, ctx)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700817
Colin Crossab054432019-07-15 16:13:59 -0700818 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
819
820 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -0700821 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -0700822 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700823 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -0700824 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -0700825 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700826 }
827
Colin Crossbc139922021-03-25 18:33:16 -0700828 d.expandArgs(ctx, cmd)
Colin Crossab054432019-07-15 16:13:59 -0700829
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000830 if d.properties.Compat_config != nil {
831 compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
832 cmd.FlagWithInput("-compatconfig ", compatConfig)
833 }
834
Colin Crossab054432019-07-15 16:13:59 -0700835 var desc string
836 if Bool(d.properties.Dokka_enabled) {
837 desc = "dokka"
838 } else {
839 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
840
841 for _, o := range d.Javadoc.properties.Out {
842 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
843 }
844
845 d.postDoclavaCmds(ctx, rule)
846 desc = "doclava"
847 }
848
849 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800850 BuiltTool("soong_zip").
Colin Crossab054432019-07-15 16:13:59 -0700851 Flag("-write_if_changed").
852 Flag("-d").
853 FlagWithOutput("-o ", d.docZip).
854 FlagWithArg("-C ", outDir.String()).
855 FlagWithArg("-D ", outDir.String())
856
Colin Crossab054432019-07-15 16:13:59 -0700857 rule.Restat()
858
859 zipSyncCleanupCmd(rule, srcJarDir)
860
Colin Crossf1a035e2020-11-16 17:32:30 -0800861 rule.Build("javadoc", desc)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700862}
863
864//
Nan Zhangf4936b02018-08-01 15:00:28 -0700865// Exported Droiddoc Directory
Nan Zhanga40da042018-08-01 12:48:00 -0700866//
Dan Willemsencc090972018-02-26 14:33:31 -0800867var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
868
Nan Zhangf4936b02018-08-01 15:00:28 -0700869type ExportedDroiddocDirProperties struct {
870 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -0800871 Path *string
872}
873
Nan Zhangf4936b02018-08-01 15:00:28 -0700874type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -0800875 android.ModuleBase
876
Nan Zhangf4936b02018-08-01 15:00:28 -0700877 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -0800878
879 deps android.Paths
880 dir android.Path
881}
882
Colin Crossa3002fc2019-07-08 16:48:04 -0700883// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -0700884func ExportedDroiddocDirFactory() android.Module {
885 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -0800886 module.AddProperties(&module.properties)
887 android.InitAndroidModule(module)
888 return module
889}
890
Nan Zhangf4936b02018-08-01 15:00:28 -0700891func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -0800892
Nan Zhangf4936b02018-08-01 15:00:28 -0700893func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -0800894 path := String(d.properties.Path)
895 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -0800896 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Dan Willemsencc090972018-02-26 14:33:31 -0800897}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800898
899//
900// Defaults
901//
902type DocDefaults struct {
903 android.ModuleBase
904 android.DefaultsModuleBase
905}
906
Nan Zhangb2b33de2018-02-23 11:18:47 -0800907func DocDefaultsFactory() android.Module {
908 module := &DocDefaults{}
909
910 module.AddProperties(
911 &JavadocProperties{},
912 &DroiddocProperties{},
913 )
914
915 android.InitDefaultsModule(module)
916
917 return module
918}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700919
Colin Cross33961b52019-07-11 11:01:22 -0700920func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
921 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
922
Colin Cross1661aff2021-03-12 17:56:51 -0800923 cmd := rule.Command()
924 cmd.Text("rm -rf").Text(cmd.PathForOutput(srcJarDir))
925 cmd = rule.Command()
926 cmd.Text("mkdir -p").Text(cmd.PathForOutput(srcJarDir))
Colin Cross33961b52019-07-11 11:01:22 -0700927 srcJarList := srcJarDir.Join(ctx, "list")
928
929 rule.Temporary(srcJarList)
930
Colin Cross1661aff2021-03-12 17:56:51 -0800931 cmd = rule.Command()
932 cmd.BuiltTool("zipsync").
933 FlagWithArg("-d ", cmd.PathForOutput(srcJarDir)).
Colin Cross33961b52019-07-11 11:01:22 -0700934 FlagWithOutput("-l ", srcJarList).
935 FlagWithArg("-f ", `"*.java"`).
936 Inputs(srcJars)
937
938 return srcJarList
939}
940
941func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
942 rule.Command().Text("rm -rf").Text(srcJarDir.String())
943}