blob: 225f201a93c67545ab55bc1e94ad360a8763c4b4 [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
Yu Liu3a892962025-01-15 23:14:27 +000022 "github.com/google/blueprint"
Jeongik Cha6bd33c12019-06-25 16:26:18 +090023 "github.com/google/blueprint/proptools"
Nan Zhang581fd212018-01-10 16:06:12 -080024
Colin Crossab054432019-07-15 16:13:59 -070025 "android/soong/android"
26 "android/soong/java/config"
Nan Zhang581fd212018-01-10 16:06:12 -080027)
28
29func init() {
Paul Duffin884363e2019-12-19 10:21:09 +000030 RegisterDocsBuildComponents(android.InitRegistrationContext)
Nan Zhang581fd212018-01-10 16:06:12 -080031}
32
Paul Duffin884363e2019-12-19 10:21:09 +000033func RegisterDocsBuildComponents(ctx android.RegistrationContext) {
34 ctx.RegisterModuleType("doc_defaults", DocDefaultsFactory)
35
36 ctx.RegisterModuleType("droiddoc", DroiddocFactory)
37 ctx.RegisterModuleType("droiddoc_host", DroiddocHostFactory)
38 ctx.RegisterModuleType("droiddoc_exported_dir", ExportedDroiddocDirFactory)
39 ctx.RegisterModuleType("javadoc", JavadocFactory)
40 ctx.RegisterModuleType("javadoc_host", JavadocHostFactory)
41}
42
Nan Zhang581fd212018-01-10 16:06:12 -080043type JavadocProperties struct {
44 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
45 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080046 Srcs []string `android:"path,arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080047
Nan Zhang581fd212018-01-10 16:06:12 -080048 // list of source files that should not be used to build the Java module.
49 // This is most useful in the arch/multilib variants to remove non-common files
50 // filegroup or genrule can be included within this property.
Colin Cross27b922f2019-03-04 22:35:41 -080051 Exclude_srcs []string `android:"path,arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080052
Jiyong Parkc6ddccf2019-09-13 20:56:14 +090053 // list of package names that should actually be used. If this property is left unspecified,
54 // all the sources from the srcs property is used.
55 Filter_packages []string
56
Nan Zhangb2b33de2018-02-23 11:18:47 -080057 // list of java libraries that will be in the classpath.
Cole Faustb7493472024-08-28 11:55:52 -070058 Libs proptools.Configurable[[]string] `android:"arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080059
60 // If set to false, don't allow this module(-docs.zip) to be exported. Defaults to true.
Nan Zhangb2b33de2018-02-23 11:18:47 -080061 Installable *bool
Nan Zhang581fd212018-01-10 16:06:12 -080062
Paul Duffine25c6442019-10-11 13:50:28 +010063 // if not blank, set to the version of the sdk to compile against.
64 // Defaults to compiling against the current platform.
Nan Zhang581fd212018-01-10 16:06:12 -080065 Sdk_version *string `android:"arch_variant"`
Jiyong Park1e440682018-05-23 18:42:04 +090066
Paul Duffine25c6442019-10-11 13:50:28 +010067 // When targeting 1.9 and above, override the modules to use with --system,
68 // otherwise provides defaults libraries to add to the bootclasspath.
69 // Defaults to "none"
70 System_modules *string
71
Jiyong Park1e440682018-05-23 18:42:04 +090072 Aidl struct {
73 // Top level directories to pass to aidl tool
74 Include_dirs []string
75
76 // Directories rooted at the Android.bp file to pass to aidl tool
77 Local_include_dirs []string
78 }
Nan Zhang357466b2018-04-17 17:38:36 -070079
80 // If not blank, set the java version passed to javadoc as -source
81 Java_version *string
Nan Zhang1598a9e2018-09-04 17:14:32 -070082
83 // local files that are used within user customized droiddoc options.
Colin Cross27b922f2019-03-04 22:35:41 -080084 Arg_files []string `android:"path"`
Nan Zhang1598a9e2018-09-04 17:14:32 -070085
Liz Kammer585cac22020-07-06 09:12:57 -070086 // user customized droiddoc args. Deprecated, use flags instead.
Nan Zhang1598a9e2018-09-04 17:14:32 -070087 // Available variables for substitution:
88 //
89 // $(location <label>): the path to the arg_files with name <label>
Colin Crosse4a05842019-05-28 10:17:14 -070090 // $$: a literal $
Nan Zhang1598a9e2018-09-04 17:14:32 -070091 Args *string
92
Liz Kammer585cac22020-07-06 09:12:57 -070093 // user customized droiddoc args. Not compatible with property args.
94 // Available variables for substitution:
95 //
96 // $(location <label>): the path to the arg_files with name <label>
97 // $$: a literal $
98 Flags []string
99
Nan Zhang1598a9e2018-09-04 17:14:32 -0700100 // names of the output files used in args that will be generated
101 Out []string
Nan Zhang581fd212018-01-10 16:06:12 -0800102}
103
Nan Zhang61819ce2018-05-04 18:49:16 -0700104type ApiToCheck struct {
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900105 // path to the API txt file that the new API extracted from source code is checked
106 // against. The path can be local to the module or from other module (via :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800107 Api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700108
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900109 // path to the API txt file that the new @removed API extractd from source code is
110 // checked against. The path can be local to the module or from other module (via
111 // :module syntax).
Colin Cross27b922f2019-03-04 22:35:41 -0800112 Removed_api_file *string `android:"path"`
Nan Zhang61819ce2018-05-04 18:49:16 -0700113
Adrian Roos14f75a92019-08-12 17:54:09 +0200114 // If not blank, path to the baseline txt file for approved API check violations.
115 Baseline_file *string `android:"path"`
116
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900117 // Arguments to the apicheck tool.
Nan Zhang61819ce2018-05-04 18:49:16 -0700118 Args *string
119}
120
Nan Zhang581fd212018-01-10 16:06:12 -0800121type DroiddocProperties struct {
122 // directory relative to top of the source tree that contains doc templates files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800123 Custom_template *string
Nan Zhang581fd212018-01-10 16:06:12 -0800124
Nan Zhanga40da042018-08-01 12:48:00 -0700125 // directories under current module source which contains html/jd files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800126 Html_dirs []string
Nan Zhang581fd212018-01-10 16:06:12 -0800127
128 // set a value in the Clearsilver hdf namespace.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800129 Hdf []string
Nan Zhang581fd212018-01-10 16:06:12 -0800130
131 // proofread file contains all of the text content of the javadocs concatenated into one file,
132 // suitable for spell-checking and other goodness.
Colin Crossab054432019-07-15 16:13:59 -0700133 Proofread_file *string
Nan Zhang581fd212018-01-10 16:06:12 -0800134
135 // a todo file lists the program elements that are missing documentation.
136 // At some point, this might be improved to show more warnings.
Colin Cross27b922f2019-03-04 22:35:41 -0800137 Todo_file *string `android:"path"`
Nan Zhangb2b33de2018-02-23 11:18:47 -0800138
Anton Hanssonb06bb572023-10-03 12:11:35 +0000139 // A file containing a baseline for allowed lint errors.
140 Lint_baseline *string `android:"path"`
141
Nan Zhangb2b33de2018-02-23 11:18:47 -0800142 // 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// Common flags passed down to build rule
Nan Zhanga40da042018-08-01 12:48:00 -0700166type droiddocBuilderFlags struct {
Nan Zhang86d2d552018-08-09 15:33:27 -0700167 bootClasspathArgs string
168 classpathArgs string
Nan Zhang1598a9e2018-09-04 17:14:32 -0700169 sourcepathArgs string
Nan Zhang86d2d552018-08-09 15:33:27 -0700170 dokkaClasspathArgs string
171 aidlFlags string
Colin Cross3047fa22019-04-18 10:56:44 -0700172 aidlDeps android.Paths
Nan Zhanga40da042018-08-01 12:48:00 -0700173
Nan Zhanga40da042018-08-01 12:48:00 -0700174 doclavaStubsFlags string
Nan Zhang86d2d552018-08-09 15:33:27 -0700175 doclavaDocsFlags string
Nan Zhanga40da042018-08-01 12:48:00 -0700176 postDoclavaCmds string
Nan Zhanga40da042018-08-01 12:48:00 -0700177}
178
179func InitDroiddocModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
180 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
181 android.InitDefaultableModule(module)
182}
183
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200184func apiCheckEnabled(ctx android.ModuleContext, apiToCheck ApiToCheck, apiVersionTag string) bool {
185 if ctx.Config().IsEnvTrue("WITHOUT_CHECK_API") {
Jihoon Kang91bf3dd2024-01-24 00:40:23 +0000186 if ctx.Config().BuildFromTextStub() {
187 ctx.ModuleErrorf("Generating stubs from api signature files is not available " +
188 "with WITHOUT_CHECK_API=true, as sync between the source Java files and the " +
189 "api signature files is not guaranteed.\n" +
190 "In order to utilize WITHOUT_CHECK_API, generate stubs from the source Java " +
191 "files with BUILD_FROM_SOURCE_STUB=true.\n" +
192 "However, the usage of WITHOUT_CHECK_API is not preferred as the incremental " +
193 "build is slower when generating stubs from the source Java files.\n" +
194 "Consider updating the api signature files and generating the stubs from " +
195 "them instead.")
196 }
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200197 return false
198 } else if String(apiToCheck.Api_file) != "" && String(apiToCheck.Removed_api_file) != "" {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700199 return true
200 } else if String(apiToCheck.Api_file) != "" {
201 panic("for " + apiVersionTag + " removed_api_file has to be non-empty!")
202 } else if String(apiToCheck.Removed_api_file) != "" {
203 panic("for " + apiVersionTag + " api_file has to be non-empty!")
204 }
205
206 return false
207}
208
Nan Zhanga40da042018-08-01 12:48:00 -0700209// Javadoc
Nan Zhang581fd212018-01-10 16:06:12 -0800210type Javadoc struct {
211 android.ModuleBase
212 android.DefaultableModuleBase
213
214 properties JavadocProperties
215
216 srcJars android.Paths
217 srcFiles android.Paths
218 sourcepaths android.Paths
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400219 implicits android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700220
Nan Zhangccff0f72018-03-08 17:26:16 -0800221 docZip android.WritablePath
222 stubsSrcJar android.WritablePath
Jihoon Kang3c89f042023-12-19 02:40:22 +0000223
224 exportableStubsSrcJar android.WritablePath
Nan Zhang581fd212018-01-10 16:06:12 -0800225}
226
Colin Crossa3002fc2019-07-08 16:48:04 -0700227// javadoc converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800228func JavadocFactory() android.Module {
229 module := &Javadoc{}
230
231 module.AddProperties(&module.properties)
232
233 InitDroiddocModule(module, android.HostAndDeviceSupported)
234 return module
235}
236
Colin Crossa3002fc2019-07-08 16:48:04 -0700237// javadoc_host converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800238func JavadocHostFactory() android.Module {
239 module := &Javadoc{}
240
241 module.AddProperties(&module.properties)
242
243 InitDroiddocModule(module, android.HostSupported)
244 return module
245}
246
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
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000255func (j *Javadoc) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
256 return j.SdkVersion(ctx).ApiLevel
Colin Cross83bb3162018-06-25 15:48:06 -0700257}
258
Spandan Dasa26eda72023-03-02 00:56:06 +0000259func (j *Javadoc) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
260 return j.SdkVersion(ctx).ApiLevel
William Loh5a082f92022-05-17 20:21:50 +0000261}
262
Spandan Dasca70fc42023-03-01 23:38:49 +0000263func (j *Javadoc) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
264 return j.SdkVersion(ctx).ApiLevel
Dan Willemsen419290a2018-10-31 15:28:47 -0700265}
266
Nan Zhang581fd212018-01-10 16:06:12 -0800267func (j *Javadoc) addDeps(ctx android.BottomUpMutatorContext) {
268 if ctx.Device() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900269 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Pete Gilline3d44b22020-06-29 11:28:51 +0100270 if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700271 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100272 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700273 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Liz Kammeref28a4c2022-09-23 16:50:56 -0400274 ctx.AddVariationDependencies(nil, sdkLibTag, sdkDep.classpath...)
Nan Zhang581fd212018-01-10 16:06:12 -0800275 }
276 }
277
Cole Faustb7493472024-08-28 11:55:52 -0700278 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs.GetOrDefault(ctx, nil)...)
Nan Zhang581fd212018-01-10 16:06:12 -0800279}
280
Nan Zhanga40da042018-08-01 12:48:00 -0700281func (j *Javadoc) collectAidlFlags(ctx android.ModuleContext, deps deps) droiddocBuilderFlags {
282 var flags droiddocBuilderFlags
Jiyong Park1e440682018-05-23 18:42:04 +0900283
Colin Cross3047fa22019-04-18 10:56:44 -0700284 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Jiyong Park1e440682018-05-23 18:42:04 +0900285
286 return flags
287}
288
289func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700290 aidlIncludeDirs android.Paths) (string, android.Paths) {
Jiyong Park1e440682018-05-23 18:42:04 +0900291
292 aidlIncludes := android.PathsForModuleSrc(ctx, j.properties.Aidl.Local_include_dirs)
293 aidlIncludes = append(aidlIncludes, android.PathsForSource(ctx, j.properties.Aidl.Include_dirs)...)
294
295 var flags []string
Colin Cross3047fa22019-04-18 10:56:44 -0700296 var deps android.Paths
297
Jiyong Park1e440682018-05-23 18:42:04 +0900298 if aidlPreprocess.Valid() {
299 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700300 deps = append(deps, aidlPreprocess.Path())
Jiyong Park1e440682018-05-23 18:42:04 +0900301 } else {
302 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
303 }
304
305 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Crossf96b0012023-10-26 14:01:51 -0700306 flags = append(flags, "-I"+ctx.ModuleDir())
Jiyong Park1e440682018-05-23 18:42:04 +0900307 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
308 flags = append(flags, "-I"+src.String())
309 }
310
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000311 minSdkVersion := j.MinSdkVersion(ctx).FinalOrFutureInt()
Spandan Das757b6662022-11-17 04:29:59 +0000312 flags = append(flags, fmt.Sprintf("--min_sdk_version=%v", minSdkVersion))
313
Colin Cross3047fa22019-04-18 10:56:44 -0700314 return strings.Join(flags, " "), deps
Jiyong Park1e440682018-05-23 18:42:04 +0900315}
316
Jiyong Parkd90d7412019-08-20 22:49:19 +0900317// TODO: remove the duplication between this and the one in gen.go
Jiyong Park1e440682018-05-23 18:42:04 +0900318func (j *Javadoc) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Nan Zhanga40da042018-08-01 12:48:00 -0700319 flags droiddocBuilderFlags) android.Paths {
Jiyong Park1e440682018-05-23 18:42:04 +0900320
321 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Crossc0806172019-06-14 18:51:47 -0700322 var aidlSrcs android.Paths
Jiyong Park1e440682018-05-23 18:42:04 +0900323
Sam Delmerico2351eac2022-05-24 17:10:02 +0000324 aidlIncludeFlags := genAidlIncludeFlags(ctx, srcFiles, android.Paths{})
Jiyong Park1112c4c2019-08-16 21:12:10 +0900325
Jiyong Park1e440682018-05-23 18:42:04 +0900326 for _, srcFile := range srcFiles {
327 switch srcFile.Ext() {
328 case ".aidl":
Colin Crossc0806172019-06-14 18:51:47 -0700329 aidlSrcs = append(aidlSrcs, srcFile)
Jiyong Parkd90d7412019-08-20 22:49:19 +0900330 case ".logtags":
331 javaFile := genLogtags(ctx, srcFile)
332 outSrcFiles = append(outSrcFiles, javaFile)
Jiyong Park1e440682018-05-23 18:42:04 +0900333 default:
334 outSrcFiles = append(outSrcFiles, srcFile)
335 }
336 }
337
Colin Crossc0806172019-06-14 18:51:47 -0700338 // Process all aidl files together to support sharding them into one or more rules that produce srcjars.
339 if len(aidlSrcs) > 0 {
Thiébaud Weksteende8417c2022-02-10 15:41:46 +1100340 srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags+aidlIncludeFlags, nil, flags.aidlDeps)
Colin Crossc0806172019-06-14 18:51:47 -0700341 outSrcFiles = append(outSrcFiles, srcJarFiles...)
342 }
343
Jiyong Park1e440682018-05-23 18:42:04 +0900344 return outSrcFiles
345}
346
Nan Zhang581fd212018-01-10 16:06:12 -0800347func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
348 var deps deps
349
Jiyong Parkf1691d22021-03-29 20:11:58 +0900350 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800351 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700352 ctx.AddMissingDependencies(sdkDep.bootclasspath)
353 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Nan Zhang581fd212018-01-10 16:06:12 -0800354 } else if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700355 deps.bootClasspath = append(deps.bootClasspath, sdkDep.jars...)
Anton Hansson26bf49b2020-02-08 20:26:29 +0000356 deps.aidlPreprocess = sdkDep.aidl
357 } else {
358 deps.aidlPreprocess = sdkDep.aidl
Nan Zhang581fd212018-01-10 16:06:12 -0800359 }
360
Yu Liu39f5fb32025-01-13 23:52:19 +0000361 ctx.VisitDirectDepsProxy(func(module android.ModuleProxy) {
Nan Zhang581fd212018-01-10 16:06:12 -0800362 otherName := ctx.OtherModuleName(module)
363 tag := ctx.OtherModuleDependencyTag(module)
364
Colin Cross2d24c1b2018-05-23 10:59:18 -0700365 switch tag {
366 case bootClasspathTag:
Colin Cross313aa542023-12-13 13:47:44 -0800367 if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800368 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars...)
Colin Crossb61c2262024-08-08 14:04:42 -0700369 } else if sm, ok := android.OtherModuleProvider(ctx, module, SystemModulesProvider); ok {
Paul Duffine25c6442019-10-11 13:50:28 +0100370 // A system modules dependency has been added to the bootclasspath
371 // so add its libs to the bootclasspath.
Colin Crossb61c2262024-08-08 14:04:42 -0700372 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700373 } else {
374 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
375 }
Liz Kammeref28a4c2022-09-23 16:50:56 -0400376 case libTag, sdkLibTag:
Jihoon Kang98e9ac62024-09-25 23:42:30 +0000377 if sdkInfo, ok := android.OtherModuleProvider(ctx, module, SdkLibraryInfoProvider); ok {
Jihoon Kangc4db1092024-09-18 23:10:55 +0000378 generatingLibsString := android.PrettyConcat(
379 getGeneratingLibs(ctx, j.SdkVersion(ctx), module.Name(), sdkInfo), true, "or")
380 ctx.ModuleErrorf("cannot depend directly on java_sdk_library %q; try depending on %s instead", module.Name(), generatingLibsString)
Colin Cross313aa542023-12-13 13:47:44 -0800381 } else if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800382 deps.classpath = append(deps.classpath, dep.HeaderJars...)
383 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs...)
Jihoon Kang3921f0b2024-03-12 23:51:37 +0000384 deps.aconfigProtoFiles = append(deps.aconfigProtoFiles, dep.AconfigIntermediateCacheOutputPaths...)
Yu Liuc41eae52025-01-14 01:03:08 +0000385 } else if dep, ok := android.OtherModuleProvider(ctx, module, android.SourceFilesInfoProvider); ok {
Yu Liu39f5fb32025-01-13 23:52:19 +0000386 checkProducesJars(ctx, dep, module)
387 deps.classpath = append(deps.classpath, dep.Srcs...)
Colin Crossdcf71b22021-02-01 13:59:03 -0800388 } else {
Nan Zhang581fd212018-01-10 16:06:12 -0800389 ctx.ModuleErrorf("depends on non-java module %q", otherName)
390 }
Jihoon Kang3921f0b2024-03-12 23:51:37 +0000391
Colin Cross6cef4812019-10-17 14:23:50 -0700392 case java9LibTag:
Colin Cross313aa542023-12-13 13:47:44 -0800393 if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800394 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars...)
395 } else {
Colin Cross6cef4812019-10-17 14:23:50 -0700396 ctx.ModuleErrorf("depends on non-java module %q", otherName)
397 }
Nan Zhang357466b2018-04-17 17:38:36 -0700398 case systemModulesTag:
399 if deps.systemModules != nil {
400 panic("Found two system module dependencies")
401 }
Colin Crossb61c2262024-08-08 14:04:42 -0700402 if sm, ok := android.OtherModuleProvider(ctx, module, SystemModulesProvider); ok {
403 deps.systemModules = &systemModules{sm.OutputDir, sm.OutputDirDeps}
404 } else {
405 ctx.PropertyErrorf("boot classpath dependency %q does not provide SystemModulesProvider",
406 ctx.OtherModuleName(module))
407 }
Jihoon Kang6592e872023-12-19 01:13:16 +0000408 case aconfigDeclarationTag:
409 if dep, ok := android.OtherModuleProvider(ctx, module, android.AconfigDeclarationsProviderKey); ok {
410 deps.aconfigProtoFiles = append(deps.aconfigProtoFiles, dep.IntermediateCacheOutputPath)
Yu Liu67a28422024-03-05 00:36:31 +0000411 } else if dep, ok := android.OtherModuleProvider(ctx, module, android.CodegenInfoProvider); ok {
Jihoon Kang38e4f252024-02-13 20:49:47 +0000412 deps.aconfigProtoFiles = append(deps.aconfigProtoFiles, dep.IntermediateCacheOutputPaths...)
Jihoon Kang6592e872023-12-19 01:13:16 +0000413 } else {
Jihoon Kang38e4f252024-02-13 20:49:47 +0000414 ctx.ModuleErrorf("Only aconfig_declarations and aconfig_declarations_group "+
415 "module type is allowed for flags_packages property, but %s is neither "+
416 "of these supported module types",
Jihoon Kang6592e872023-12-19 01:13:16 +0000417 module.Name(),
418 )
419 }
Nan Zhang581fd212018-01-10 16:06:12 -0800420 }
421 })
422 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
423 // may contain filegroup or genrule.
Colin Cross8a497952019-03-05 22:25:09 -0800424 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400425 j.implicits = append(j.implicits, srcFiles...)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900426
Jihoon Kang3921f0b2024-03-12 23:51:37 +0000427 // Module can depend on a java_aconfig_library module using the ":module_name{.tag}" syntax.
428 // Find the corresponding aconfig_declarations module name for such case.
429 for _, src := range j.properties.Srcs {
430 if moduleName, tag := android.SrcIsModuleWithTag(src); moduleName != "" {
Yu Liud3228ac2024-11-08 23:11:47 +0000431 otherModule := android.GetModuleProxyFromPathDep(ctx, moduleName, tag)
Jihoon Kang3921f0b2024-03-12 23:51:37 +0000432 if otherModule != nil {
Yu Liud3228ac2024-11-08 23:11:47 +0000433 if dep, ok := android.OtherModuleProvider(ctx, *otherModule, android.CodegenInfoProvider); ok {
Jihoon Kang3921f0b2024-03-12 23:51:37 +0000434 deps.aconfigProtoFiles = append(deps.aconfigProtoFiles, dep.IntermediateCacheOutputPaths...)
435 }
436 }
437 }
438 }
439
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900440 filterByPackage := func(srcs []android.Path, filterPackages []string) []android.Path {
441 if filterPackages == nil {
442 return srcs
443 }
444 filtered := []android.Path{}
445 for _, src := range srcs {
446 if src.Ext() != ".java" {
447 // Don't filter-out non-Java (=generated sources) by package names. This is not ideal,
448 // but otherwise metalava emits stub sources having references to the generated AIDL classes
449 // in filtered-out pacages (e.g. com.android.internal.*).
450 // TODO(b/141149570) We need to fix this by introducing default private constructors or
451 // fixing metalava to not emit constructors having references to unknown classes.
452 filtered = append(filtered, src)
453 continue
454 }
455 packageName := strings.ReplaceAll(filepath.Dir(src.Rel()), "/", ".")
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800456 if android.HasAnyPrefix(packageName, filterPackages) {
457 filtered = append(filtered, src)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900458 }
459 }
460 return filtered
461 }
462 srcFiles = filterByPackage(srcFiles, j.properties.Filter_packages)
463
Liz Kammer585cac22020-07-06 09:12:57 -0700464 aidlFlags := j.collectAidlFlags(ctx, deps)
465 srcFiles = j.genSources(ctx, srcFiles, aidlFlags)
Nan Zhang581fd212018-01-10 16:06:12 -0800466
467 // srcs may depend on some genrule output.
468 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800469 j.srcJars = append(j.srcJars, deps.srcJars...)
470
Nan Zhang581fd212018-01-10 16:06:12 -0800471 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800472 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800473
Liz Kammere1ab2502020-09-10 15:29:25 +0000474 if len(j.srcFiles) > 0 {
475 j.sourcepaths = android.PathsForModuleSrc(ctx, []string{"."})
Nan Zhang581fd212018-01-10 16:06:12 -0800476 }
Nan Zhang581fd212018-01-10 16:06:12 -0800477
Colin Crossbc139922021-03-25 18:33:16 -0700478 return deps
479}
480
481func (j *Javadoc) expandArgs(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
482 var argFiles android.Paths
Paul Duffin99e4a502019-02-11 15:38:42 +0000483 argFilesMap := map[string]string{}
484 argFileLabels := []string{}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700485
Paul Duffin99e4a502019-02-11 15:38:42 +0000486 for _, label := range j.properties.Arg_files {
Colin Cross8a497952019-03-05 22:25:09 -0800487 var paths = android.PathsForModuleSrc(ctx, []string{label})
Paul Duffin99e4a502019-02-11 15:38:42 +0000488 if _, exists := argFilesMap[label]; !exists {
Colin Crossbc139922021-03-25 18:33:16 -0700489 argFilesMap[label] = strings.Join(cmd.PathsForInputs(paths), " ")
Paul Duffin99e4a502019-02-11 15:38:42 +0000490 argFileLabels = append(argFileLabels, label)
Colin Crossbc139922021-03-25 18:33:16 -0700491 argFiles = append(argFiles, paths...)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700492 } else {
493 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000494 label, argFilesMap[label], paths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700495 }
496 }
497
Liz Kammer585cac22020-07-06 09:12:57 -0700498 var argsPropertyName string
499 flags := make([]string, 0)
500 if j.properties.Args != nil && j.properties.Flags != nil {
501 ctx.PropertyErrorf("args", "flags is set. Cannot set args")
502 } else if args := proptools.String(j.properties.Args); args != "" {
503 flags = append(flags, args)
504 argsPropertyName = "args"
505 } else {
506 flags = append(flags, j.properties.Flags...)
507 argsPropertyName = "flags"
508 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700509
Liz Kammer585cac22020-07-06 09:12:57 -0700510 for _, flag := range flags {
Colin Crossbc139922021-03-25 18:33:16 -0700511 expanded, err := android.Expand(flag, func(name string) (string, error) {
Liz Kammer585cac22020-07-06 09:12:57 -0700512 if strings.HasPrefix(name, "location ") {
513 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
514 if paths, ok := argFilesMap[label]; ok {
515 return paths, nil
516 } else {
517 return "", fmt.Errorf("unknown location label %q, expecting one of %q",
518 label, strings.Join(argFileLabels, ", "))
519 }
520 } else if name == "genDir" {
521 return android.PathForModuleGen(ctx).String(), nil
522 }
523 return "", fmt.Errorf("unknown variable '$(%s)'", name)
524 })
525
526 if err != nil {
527 ctx.PropertyErrorf(argsPropertyName, "%s", err.Error())
528 }
Colin Crossbc139922021-03-25 18:33:16 -0700529 cmd.Flag(expanded)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700530 }
531
Colin Crossbc139922021-03-25 18:33:16 -0700532 cmd.Implicits(argFiles)
Nan Zhang581fd212018-01-10 16:06:12 -0800533}
534
535func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
536 j.addDeps(ctx)
537}
538
539func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
540 deps := j.collectDeps(ctx)
541
Colin Crossdaa4c672019-07-15 22:53:46 -0700542 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800543
Colin Crossdaa4c672019-07-15 22:53:46 -0700544 outDir := android.PathForModuleOut(ctx, "out")
545 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
546
547 j.stubsSrcJar = nil
548
Colin Crossf1a035e2020-11-16 17:32:30 -0800549 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossdaa4c672019-07-15 22:53:46 -0700550
551 rule.Command().Text("rm -rf").Text(outDir.String())
552 rule.Command().Text("mkdir -p").Text(outDir.String())
553
554 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, j.srcJars)
Nan Zhang357466b2018-04-17 17:38:36 -0700555
Jiyong Parkf1691d22021-03-29 20:11:58 +0900556 javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), android.SdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800557
Colin Crossdaa4c672019-07-15 22:53:46 -0700558 cmd := javadocSystemModulesCmd(ctx, rule, j.srcFiles, outDir, srcJarDir, srcJarList,
559 deps.systemModules, deps.classpath, j.sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800560
Colin Cross1e743852019-10-28 11:37:20 -0700561 cmd.FlagWithArg("-source ", javaVersion.String()).
Colin Crossdaa4c672019-07-15 22:53:46 -0700562 Flag("-J-Xmx1024m").
563 Flag("-XDignore.symbol.file").
564 Flag("-Xdoclint:none")
Nan Zhang581fd212018-01-10 16:06:12 -0800565
Colin Crossbc139922021-03-25 18:33:16 -0700566 j.expandArgs(ctx, cmd)
567
Colin Crossdaa4c672019-07-15 22:53:46 -0700568 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800569 BuiltTool("soong_zip").
Colin Crossdaa4c672019-07-15 22:53:46 -0700570 Flag("-write_if_changed").
571 Flag("-d").
572 FlagWithOutput("-o ", j.docZip).
573 FlagWithArg("-C ", outDir.String()).
574 FlagWithArg("-D ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700575
Colin Crossdaa4c672019-07-15 22:53:46 -0700576 rule.Restat()
577
578 zipSyncCleanupCmd(rule, srcJarDir)
579
Colin Crossf1a035e2020-11-16 17:32:30 -0800580 rule.Build("javadoc", "javadoc")
mrziwang74e50212024-06-27 10:14:24 -0700581
mrziwang74e50212024-06-27 10:14:24 -0700582 ctx.SetOutputFiles(android.Paths{j.docZip}, ".docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800583}
584
Nan Zhanga40da042018-08-01 12:48:00 -0700585// Droiddoc
Nan Zhanga40da042018-08-01 12:48:00 -0700586type Droiddoc struct {
587 Javadoc
588
Liz Kammere1ab2502020-09-10 15:29:25 +0000589 properties DroiddocProperties
Nan Zhanga40da042018-08-01 12:48:00 -0700590}
591
Colin Crossa3002fc2019-07-08 16:48:04 -0700592// droiddoc converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700593func DroiddocFactory() android.Module {
594 module := &Droiddoc{}
595
596 module.AddProperties(&module.properties,
597 &module.Javadoc.properties)
598
599 InitDroiddocModule(module, android.HostAndDeviceSupported)
600 return module
601}
602
Colin Crossa3002fc2019-07-08 16:48:04 -0700603// droiddoc_host converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700604func DroiddocHostFactory() android.Module {
605 module := &Droiddoc{}
606
607 module.AddProperties(&module.properties,
608 &module.Javadoc.properties)
609
610 InitDroiddocModule(module, android.HostSupported)
611 return module
612}
613
Nan Zhang581fd212018-01-10 16:06:12 -0800614func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
615 d.Javadoc.addDeps(ctx)
616
Nan Zhang79614d12018-04-19 18:03:39 -0700617 if String(d.properties.Custom_template) != "" {
Dan Willemsencc090972018-02-26 14:33:31 -0800618 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
619 }
Nan Zhang581fd212018-01-10 16:06:12 -0800620}
621
Colin Crossab054432019-07-15 16:13:59 -0700622func (d *Droiddoc) doclavaDocsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, docletPath classpath) {
Colin Cross2a2e0db2020-02-21 16:55:46 -0800623 buildNumberFile := ctx.Config().BuildNumberFile(ctx)
Nan Zhang443fa522018-08-20 20:58:28 -0700624 // Droiddoc always gets "-source 1.8" because it doesn't support 1.9 sources. For modules with 1.9
625 // sources, droiddoc will get sources produced by metalava which will have already stripped out the
626 // 1.9 language features.
Jihoon Kangff878bf2022-12-22 21:26:06 +0000627 cmd.FlagWithArg("-source ", getStubsJavaVersion().String()).
Colin Crossab054432019-07-15 16:13:59 -0700628 Flag("-J-Xmx1600m").
629 Flag("-J-XX:-OmitStackTraceInFastThrow").
630 Flag("-XDignore.symbol.file").
Sorin Bascad528d562022-10-24 15:10:25 +0100631 Flag("--ignore-source-errors").
Sorin Bascaae995ae2023-03-01 08:47:42 +0000632 FlagWithArg("-doclet ", "com.google.doclava.Doclava").
Colin Crossab054432019-07-15 16:13:59 -0700633 FlagWithInputList("-docletpath ", docletPath.Paths(), ":").
Sorin Bascaae995ae2023-03-01 08:47:42 +0000634 FlagWithArg("-Xmaxerrs ", "10").
635 FlagWithArg("-Xmaxwarns ", "10").
Sorin Bascad528d562022-10-24 15:10:25 +0100636 Flag("-J--add-exports=jdk.javadoc/jdk.javadoc.internal.doclets.formats.html=ALL-UNNAMED").
Sorin Bascaa7b777f2023-06-09 09:19:36 +0000637 Flag("-J--add-exports=jdk.javadoc/jdk.javadoc.internal.tool=ALL-UNNAMED").
638 Flag("-J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED").
639 Flag("-J--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED").
640 Flag("-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED").
641 Flag("-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED").
Sorin Bascad528d562022-10-24 15:10:25 +0100642 Flag("-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED").
Colin Cross2a2e0db2020-02-21 16:55:46 -0800643 FlagWithArg("-hdf page.build ", ctx.Config().BuildId()+"-$(cat "+buildNumberFile.String()+")").OrderOnly(buildNumberFile).
Elliott Hughes26bce342019-09-12 15:05:13 -0700644 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 -0700645
Nan Zhanga40da042018-08-01 12:48:00 -0700646 if String(d.properties.Custom_template) == "" {
647 // TODO: This is almost always droiddoc-templates-sdk
648 ctx.PropertyErrorf("custom_template", "must specify a template")
649 }
650
651 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
Nan Zhangf4936b02018-08-01 15:00:28 -0700652 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Crossab054432019-07-15 16:13:59 -0700653 cmd.FlagWithArg("-templatedir ", t.dir.String()).Implicits(t.deps)
Nan Zhanga40da042018-08-01 12:48:00 -0700654 } else {
Paul Duffin884363e2019-12-19 10:21:09 +0000655 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_exported_dir", ctx.OtherModuleName(m))
Nan Zhanga40da042018-08-01 12:48:00 -0700656 }
657 })
658
659 if len(d.properties.Html_dirs) > 0 {
Colin Crossab054432019-07-15 16:13:59 -0700660 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
661 cmd.FlagWithArg("-htmldir ", htmlDir.String()).
662 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[0], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700663 }
664
665 if len(d.properties.Html_dirs) > 1 {
Colin Crossab054432019-07-15 16:13:59 -0700666 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
667 cmd.FlagWithArg("-htmldir2 ", htmlDir2.String()).
668 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[1], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700669 }
670
671 if len(d.properties.Html_dirs) > 2 {
672 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
673 }
674
Colin Cross8a497952019-03-05 22:25:09 -0800675 knownTags := android.PathsForModuleSrc(ctx, d.properties.Knowntags)
Colin Crossab054432019-07-15 16:13:59 -0700676 cmd.FlagForEachInput("-knowntags ", knownTags)
Nan Zhanga40da042018-08-01 12:48:00 -0700677
Colin Crossab054432019-07-15 16:13:59 -0700678 cmd.FlagForEachArg("-hdf ", d.properties.Hdf)
Nan Zhanga40da042018-08-01 12:48:00 -0700679
680 if String(d.properties.Proofread_file) != "" {
681 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
Colin Crossab054432019-07-15 16:13:59 -0700682 cmd.FlagWithOutput("-proofread ", proofreadFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700683 }
684
685 if String(d.properties.Todo_file) != "" {
686 // tricky part:
687 // we should not compute full path for todo_file through PathForModuleOut().
688 // the non-standard doclet will get the full path relative to "-o".
Colin Crossab054432019-07-15 16:13:59 -0700689 cmd.FlagWithArg("-todo ", String(d.properties.Todo_file)).
690 ImplicitOutput(android.PathForModuleOut(ctx, String(d.properties.Todo_file)))
Nan Zhanga40da042018-08-01 12:48:00 -0700691 }
692
Anton Hanssonb06bb572023-10-03 12:11:35 +0000693 if String(d.properties.Lint_baseline) != "" {
694 cmd.FlagWithInput("-lintbaseline ", android.PathForModuleSrc(ctx, String(d.properties.Lint_baseline)))
695 }
696
Nan Zhanga40da042018-08-01 12:48:00 -0700697 if String(d.properties.Resourcesdir) != "" {
698 // TODO: should we add files under resourcesDir to the implicits? It seems that
699 // resourcesDir is one sub dir of htmlDir
700 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
Colin Crossab054432019-07-15 16:13:59 -0700701 cmd.FlagWithArg("-resourcesdir ", resourcesDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700702 }
703
704 if String(d.properties.Resourcesoutdir) != "" {
705 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
Colin Crossab054432019-07-15 16:13:59 -0700706 cmd.FlagWithArg("-resourcesoutdir ", String(d.properties.Resourcesoutdir))
Nan Zhanga40da042018-08-01 12:48:00 -0700707 }
Nan Zhanga40da042018-08-01 12:48:00 -0700708}
709
Colin Crossab054432019-07-15 16:13:59 -0700710func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700711 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700712 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
713 rule.Command().Text("cp").
714 Input(staticDocIndexRedirect).
715 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700716 }
717
718 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700719 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
720 rule.Command().Text("cp").
721 Input(staticDocProperties).
722 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700723 }
Nan Zhanga40da042018-08-01 12:48:00 -0700724}
725
Colin Crossab054432019-07-15 16:13:59 -0700726func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700727 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700728
729 cmd := rule.Command().
Sorin Bascad528d562022-10-24 15:10:25 +0100730 BuiltTool("soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
Colin Crossab054432019-07-15 16:13:59 -0700731 Flag(config.JavacVmFlags).
Colin Cross70c47412021-03-12 17:48:14 -0800732 FlagWithRspFileInputList("@", android.PathForModuleOut(ctx, "javadoc.rsp"), srcs).
Colin Crossab054432019-07-15 16:13:59 -0700733 FlagWithInput("@", srcJarList)
734
Colin Crossab054432019-07-15 16:13:59 -0700735 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
736 // based stubs generation.
737 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
738 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
739 // the correct package name base path.
740 if len(sourcepaths) > 0 {
741 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
742 } else {
743 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
744 }
745
746 cmd.FlagWithArg("-d ", outDir.String()).
747 Flag("-quiet")
748
749 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700750}
751
Colin Crossdaa4c672019-07-15 22:53:46 -0700752func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
753 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
754 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
755
756 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
757
758 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
759 cmd.Flag(flag).Implicits(deps)
760
761 cmd.FlagWithArg("--patch-module ", "java.base=.")
762
763 if len(classpath) > 0 {
764 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
765 }
766
767 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700768}
769
Colin Crossdaa4c672019-07-15 22:53:46 -0700770func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
771 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
772 sourcepaths android.Paths) *android.RuleBuilderCommand {
773
774 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
775
776 if len(bootclasspath) == 0 && ctx.Device() {
777 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
778 // ensure java does not fall back to the default bootclasspath.
779 cmd.FlagWithArg("-bootclasspath ", `""`)
780 } else if len(bootclasspath) > 0 {
781 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
782 }
783
784 if len(classpath) > 0 {
785 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
786 }
787
788 return cmd
789}
790
Colin Crossab054432019-07-15 16:13:59 -0700791func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
792 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700793
Colin Crossab054432019-07-15 16:13:59 -0700794 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
795 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
796
797 return rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800798 BuiltTool("dokka").
Colin Crossab054432019-07-15 16:13:59 -0700799 Flag(config.JavacVmFlags).
Sorin Basca1d68e482022-09-08 16:48:01 +0100800 Flag("-J--add-opens=java.base/java.lang=ALL-UNNAMED").
Colin Crossab054432019-07-15 16:13:59 -0700801 Flag(srcJarDir.String()).
802 FlagWithInputList("-classpath ", dokkaClasspath, ":").
803 FlagWithArg("-format ", "dac").
804 FlagWithArg("-dacRoot ", "/reference/kotlin").
805 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700806}
807
808func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
809 deps := d.Javadoc.collectDeps(ctx)
810
Colin Crossdaa4c672019-07-15 22:53:46 -0700811 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Colin Crossdaa4c672019-07-15 22:53:46 -0700812
Colin Crossae5330a2021-11-03 13:31:22 -0700813 jsilver := ctx.Config().HostJavaToolPath(ctx, "jsilver.jar")
814 doclava := ctx.Config().HostJavaToolPath(ctx, "doclava.jar")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700815
Colin Crossab054432019-07-15 16:13:59 -0700816 outDir := android.PathForModuleOut(ctx, "out")
817 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700818
Colin Crossf1a035e2020-11-16 17:32:30 -0800819 rule := android.NewRuleBuilder(pctx, ctx)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700820
Colin Crossab054432019-07-15 16:13:59 -0700821 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
822
823 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -0700824 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -0700825 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700826 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -0700827 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -0700828 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700829 }
830
Colin Crossbc139922021-03-25 18:33:16 -0700831 d.expandArgs(ctx, cmd)
Colin Crossab054432019-07-15 16:13:59 -0700832
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000833 if d.properties.Compat_config != nil {
834 compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
835 cmd.FlagWithInput("-compatconfig ", compatConfig)
836 }
837
Colin Crossab054432019-07-15 16:13:59 -0700838 var desc string
839 if Bool(d.properties.Dokka_enabled) {
840 desc = "dokka"
841 } else {
Sorin Bascaae995ae2023-03-01 08:47:42 +0000842 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
Colin Crossab054432019-07-15 16:13:59 -0700843
844 for _, o := range d.Javadoc.properties.Out {
845 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
846 }
847
848 d.postDoclavaCmds(ctx, rule)
849 desc = "doclava"
850 }
851
852 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800853 BuiltTool("soong_zip").
Colin Crossab054432019-07-15 16:13:59 -0700854 Flag("-write_if_changed").
855 Flag("-d").
856 FlagWithOutput("-o ", d.docZip).
857 FlagWithArg("-C ", outDir.String()).
858 FlagWithArg("-D ", outDir.String())
859
Sorin Bascaae995ae2023-03-01 08:47:42 +0000860 rule.Restat()
Colin Crossab054432019-07-15 16:13:59 -0700861
Sorin Bascaae995ae2023-03-01 08:47:42 +0000862 zipSyncCleanupCmd(rule, srcJarDir)
Colin Crossab054432019-07-15 16:13:59 -0700863
Colin Crossf1a035e2020-11-16 17:32:30 -0800864 rule.Build("javadoc", desc)
mrziwang74e50212024-06-27 10:14:24 -0700865
866 ctx.SetOutputFiles(android.Paths{d.Javadoc.docZip}, "")
867 ctx.SetOutputFiles(android.Paths{d.Javadoc.docZip}, ".docs.zip")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700868}
869
Nan Zhangf4936b02018-08-01 15:00:28 -0700870// Exported Droiddoc Directory
Dan Willemsencc090972018-02-26 14:33:31 -0800871var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
872
Nan Zhangf4936b02018-08-01 15:00:28 -0700873type ExportedDroiddocDirProperties struct {
874 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -0800875 Path *string
876}
877
Yu Liu3a892962025-01-15 23:14:27 +0000878type ExportedDroiddocDirInfo struct {
879 Deps android.Paths
880 Dir android.Path
881}
882
883var ExportedDroiddocDirInfoProvider = blueprint.NewProvider[ExportedDroiddocDirInfo]()
884
Nan Zhangf4936b02018-08-01 15:00:28 -0700885type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -0800886 android.ModuleBase
887
Nan Zhangf4936b02018-08-01 15:00:28 -0700888 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -0800889
890 deps android.Paths
891 dir android.Path
892}
893
Colin Crossa3002fc2019-07-08 16:48:04 -0700894// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -0700895func ExportedDroiddocDirFactory() android.Module {
896 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -0800897 module.AddProperties(&module.properties)
898 android.InitAndroidModule(module)
899 return module
900}
901
Nan Zhangf4936b02018-08-01 15:00:28 -0700902func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -0800903
Nan Zhangf4936b02018-08-01 15:00:28 -0700904func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -0800905 path := String(d.properties.Path)
906 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -0800907 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Yu Liu3a892962025-01-15 23:14:27 +0000908
909 android.SetProvider(ctx, ExportedDroiddocDirInfoProvider, ExportedDroiddocDirInfo{
910 Dir: d.dir,
911 Deps: d.deps,
912 })
Dan Willemsencc090972018-02-26 14:33:31 -0800913}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800914
Nan Zhangb2b33de2018-02-23 11:18:47 -0800915// Defaults
Nan Zhangb2b33de2018-02-23 11:18:47 -0800916type DocDefaults struct {
917 android.ModuleBase
918 android.DefaultsModuleBase
919}
920
Nan Zhangb2b33de2018-02-23 11:18:47 -0800921func DocDefaultsFactory() android.Module {
922 module := &DocDefaults{}
923
924 module.AddProperties(
925 &JavadocProperties{},
926 &DroiddocProperties{},
927 )
928
929 android.InitDefaultsModule(module)
930
931 return module
932}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700933
Colin Cross33961b52019-07-11 11:01:22 -0700934func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
935 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
936
Colin Cross1661aff2021-03-12 17:56:51 -0800937 cmd := rule.Command()
938 cmd.Text("rm -rf").Text(cmd.PathForOutput(srcJarDir))
939 cmd = rule.Command()
940 cmd.Text("mkdir -p").Text(cmd.PathForOutput(srcJarDir))
Colin Cross33961b52019-07-11 11:01:22 -0700941 srcJarList := srcJarDir.Join(ctx, "list")
942
943 rule.Temporary(srcJarList)
944
Colin Cross1661aff2021-03-12 17:56:51 -0800945 cmd = rule.Command()
946 cmd.BuiltTool("zipsync").
947 FlagWithArg("-d ", cmd.PathForOutput(srcJarDir)).
Colin Cross33961b52019-07-11 11:01:22 -0700948 FlagWithOutput("-l ", srcJarList).
949 FlagWithArg("-f ", `"*.java"`).
950 Inputs(srcJars)
951
952 return srcJarList
953}
954
955func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
956 rule.Command().Text("rm -rf").Text(srcJarDir.String())
957}