blob: 3faf294dea19c57dcf16f733c6575436235544b1 [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
LaMont Jonesec1b7fa2025-03-05 11:57:40 -0800198 } else if ctx.Config().PartialCompileFlags().Disable_stub_validation &&
199 !ctx.Config().BuildFromTextStub() {
200 return false
Luca Stefanid63ea0a2019-09-01 21:49:45 +0200201 } else if String(apiToCheck.Api_file) != "" && String(apiToCheck.Removed_api_file) != "" {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700202 return true
203 } else if String(apiToCheck.Api_file) != "" {
204 panic("for " + apiVersionTag + " removed_api_file has to be non-empty!")
205 } else if String(apiToCheck.Removed_api_file) != "" {
206 panic("for " + apiVersionTag + " api_file has to be non-empty!")
207 }
208
209 return false
210}
211
Nan Zhanga40da042018-08-01 12:48:00 -0700212// Javadoc
Nan Zhang581fd212018-01-10 16:06:12 -0800213type Javadoc struct {
214 android.ModuleBase
215 android.DefaultableModuleBase
216
217 properties JavadocProperties
218
219 srcJars android.Paths
220 srcFiles android.Paths
221 sourcepaths android.Paths
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400222 implicits android.Paths
Nan Zhang1598a9e2018-09-04 17:14:32 -0700223
Nan Zhangccff0f72018-03-08 17:26:16 -0800224 docZip android.WritablePath
225 stubsSrcJar android.WritablePath
Jihoon Kang3c89f042023-12-19 02:40:22 +0000226
227 exportableStubsSrcJar android.WritablePath
Nan Zhang581fd212018-01-10 16:06:12 -0800228}
229
Colin Crossa3002fc2019-07-08 16:48:04 -0700230// javadoc converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800231func JavadocFactory() android.Module {
232 module := &Javadoc{}
233
234 module.AddProperties(&module.properties)
235
236 InitDroiddocModule(module, android.HostAndDeviceSupported)
237 return module
238}
239
Colin Crossa3002fc2019-07-08 16:48:04 -0700240// javadoc_host converts .java source files to documentation using javadoc.
Nan Zhang581fd212018-01-10 16:06:12 -0800241func JavadocHostFactory() android.Module {
242 module := &Javadoc{}
243
244 module.AddProperties(&module.properties)
245
246 InitDroiddocModule(module, android.HostSupported)
247 return module
248}
249
Jiyong Park92315372021-04-02 08:45:46 +0900250func (j *Javadoc) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
251 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700252}
253
Jiyong Parkf1691d22021-03-29 20:11:58 +0900254func (j *Javadoc) SystemModules() string {
Paul Duffine25c6442019-10-11 13:50:28 +0100255 return proptools.String(j.properties.System_modules)
256}
257
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000258func (j *Javadoc) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
259 return j.SdkVersion(ctx).ApiLevel
Colin Cross83bb3162018-06-25 15:48:06 -0700260}
261
Spandan Dasa26eda72023-03-02 00:56:06 +0000262func (j *Javadoc) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
263 return j.SdkVersion(ctx).ApiLevel
William Loh5a082f92022-05-17 20:21:50 +0000264}
265
Spandan Dasca70fc42023-03-01 23:38:49 +0000266func (j *Javadoc) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
267 return j.SdkVersion(ctx).ApiLevel
Dan Willemsen419290a2018-10-31 15:28:47 -0700268}
269
Nan Zhang581fd212018-01-10 16:06:12 -0800270func (j *Javadoc) addDeps(ctx android.BottomUpMutatorContext) {
271 if ctx.Device() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900272 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Pete Gilline3d44b22020-06-29 11:28:51 +0100273 if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700274 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100275 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700276 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Liz Kammeref28a4c2022-09-23 16:50:56 -0400277 ctx.AddVariationDependencies(nil, sdkLibTag, sdkDep.classpath...)
Nan Zhang581fd212018-01-10 16:06:12 -0800278 }
279 }
280
Cole Faustb7493472024-08-28 11:55:52 -0700281 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs.GetOrDefault(ctx, nil)...)
Nan Zhang581fd212018-01-10 16:06:12 -0800282}
283
Nan Zhanga40da042018-08-01 12:48:00 -0700284func (j *Javadoc) collectAidlFlags(ctx android.ModuleContext, deps deps) droiddocBuilderFlags {
285 var flags droiddocBuilderFlags
Jiyong Park1e440682018-05-23 18:42:04 +0900286
Colin Cross3047fa22019-04-18 10:56:44 -0700287 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Jiyong Park1e440682018-05-23 18:42:04 +0900288
289 return flags
290}
291
292func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700293 aidlIncludeDirs android.Paths) (string, android.Paths) {
Jiyong Park1e440682018-05-23 18:42:04 +0900294
295 aidlIncludes := android.PathsForModuleSrc(ctx, j.properties.Aidl.Local_include_dirs)
296 aidlIncludes = append(aidlIncludes, android.PathsForSource(ctx, j.properties.Aidl.Include_dirs)...)
297
298 var flags []string
Colin Cross3047fa22019-04-18 10:56:44 -0700299 var deps android.Paths
300
Jiyong Park1e440682018-05-23 18:42:04 +0900301 if aidlPreprocess.Valid() {
302 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700303 deps = append(deps, aidlPreprocess.Path())
Jiyong Park1e440682018-05-23 18:42:04 +0900304 } else {
305 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
306 }
307
308 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Crossf96b0012023-10-26 14:01:51 -0700309 flags = append(flags, "-I"+ctx.ModuleDir())
Jiyong Park1e440682018-05-23 18:42:04 +0900310 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
311 flags = append(flags, "-I"+src.String())
312 }
313
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000314 minSdkVersion := j.MinSdkVersion(ctx).FinalOrFutureInt()
Spandan Das757b6662022-11-17 04:29:59 +0000315 flags = append(flags, fmt.Sprintf("--min_sdk_version=%v", minSdkVersion))
316
Colin Cross3047fa22019-04-18 10:56:44 -0700317 return strings.Join(flags, " "), deps
Jiyong Park1e440682018-05-23 18:42:04 +0900318}
319
Jiyong Parkd90d7412019-08-20 22:49:19 +0900320// TODO: remove the duplication between this and the one in gen.go
Jiyong Park1e440682018-05-23 18:42:04 +0900321func (j *Javadoc) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Nan Zhanga40da042018-08-01 12:48:00 -0700322 flags droiddocBuilderFlags) android.Paths {
Jiyong Park1e440682018-05-23 18:42:04 +0900323
324 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Crossc0806172019-06-14 18:51:47 -0700325 var aidlSrcs android.Paths
Jiyong Park1e440682018-05-23 18:42:04 +0900326
Sam Delmerico2351eac2022-05-24 17:10:02 +0000327 aidlIncludeFlags := genAidlIncludeFlags(ctx, srcFiles, android.Paths{})
Jiyong Park1112c4c2019-08-16 21:12:10 +0900328
Jiyong Park1e440682018-05-23 18:42:04 +0900329 for _, srcFile := range srcFiles {
330 switch srcFile.Ext() {
331 case ".aidl":
Colin Crossc0806172019-06-14 18:51:47 -0700332 aidlSrcs = append(aidlSrcs, srcFile)
Jiyong Parkd90d7412019-08-20 22:49:19 +0900333 case ".logtags":
334 javaFile := genLogtags(ctx, srcFile)
335 outSrcFiles = append(outSrcFiles, javaFile)
Jiyong Park1e440682018-05-23 18:42:04 +0900336 default:
337 outSrcFiles = append(outSrcFiles, srcFile)
338 }
339 }
340
Colin Crossc0806172019-06-14 18:51:47 -0700341 // Process all aidl files together to support sharding them into one or more rules that produce srcjars.
342 if len(aidlSrcs) > 0 {
Thiébaud Weksteende8417c2022-02-10 15:41:46 +1100343 srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags+aidlIncludeFlags, nil, flags.aidlDeps)
Colin Crossc0806172019-06-14 18:51:47 -0700344 outSrcFiles = append(outSrcFiles, srcJarFiles...)
345 }
346
Jiyong Park1e440682018-05-23 18:42:04 +0900347 return outSrcFiles
348}
349
Nan Zhang581fd212018-01-10 16:06:12 -0800350func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
351 var deps deps
352
Jiyong Parkf1691d22021-03-29 20:11:58 +0900353 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800354 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700355 ctx.AddMissingDependencies(sdkDep.bootclasspath)
356 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Nan Zhang581fd212018-01-10 16:06:12 -0800357 } else if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700358 deps.bootClasspath = append(deps.bootClasspath, sdkDep.jars...)
Anton Hansson26bf49b2020-02-08 20:26:29 +0000359 deps.aidlPreprocess = sdkDep.aidl
360 } else {
361 deps.aidlPreprocess = sdkDep.aidl
Nan Zhang581fd212018-01-10 16:06:12 -0800362 }
363
Yu Liu39f5fb32025-01-13 23:52:19 +0000364 ctx.VisitDirectDepsProxy(func(module android.ModuleProxy) {
Nan Zhang581fd212018-01-10 16:06:12 -0800365 otherName := ctx.OtherModuleName(module)
366 tag := ctx.OtherModuleDependencyTag(module)
367
Colin Cross2d24c1b2018-05-23 10:59:18 -0700368 switch tag {
369 case bootClasspathTag:
Colin Cross313aa542023-12-13 13:47:44 -0800370 if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800371 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars...)
Colin Crossb61c2262024-08-08 14:04:42 -0700372 } else if sm, ok := android.OtherModuleProvider(ctx, module, SystemModulesProvider); ok {
Paul Duffine25c6442019-10-11 13:50:28 +0100373 // A system modules dependency has been added to the bootclasspath
374 // so add its libs to the bootclasspath.
Colin Crossb61c2262024-08-08 14:04:42 -0700375 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars...)
Colin Cross2d24c1b2018-05-23 10:59:18 -0700376 } else {
377 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
378 }
Liz Kammeref28a4c2022-09-23 16:50:56 -0400379 case libTag, sdkLibTag:
Jihoon Kang98e9ac62024-09-25 23:42:30 +0000380 if sdkInfo, ok := android.OtherModuleProvider(ctx, module, SdkLibraryInfoProvider); ok {
Jihoon Kangc4db1092024-09-18 23:10:55 +0000381 generatingLibsString := android.PrettyConcat(
382 getGeneratingLibs(ctx, j.SdkVersion(ctx), module.Name(), sdkInfo), true, "or")
383 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 -0800384 } else if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800385 deps.classpath = append(deps.classpath, dep.HeaderJars...)
386 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs...)
Jihoon Kang3921f0b2024-03-12 23:51:37 +0000387 deps.aconfigProtoFiles = append(deps.aconfigProtoFiles, dep.AconfigIntermediateCacheOutputPaths...)
Yu Liuc41eae52025-01-14 01:03:08 +0000388 } else if dep, ok := android.OtherModuleProvider(ctx, module, android.SourceFilesInfoProvider); ok {
Yu Liu39f5fb32025-01-13 23:52:19 +0000389 checkProducesJars(ctx, dep, module)
390 deps.classpath = append(deps.classpath, dep.Srcs...)
Colin Crossdcf71b22021-02-01 13:59:03 -0800391 } else {
Nan Zhang581fd212018-01-10 16:06:12 -0800392 ctx.ModuleErrorf("depends on non-java module %q", otherName)
393 }
Jihoon Kang3921f0b2024-03-12 23:51:37 +0000394
Colin Cross6cef4812019-10-17 14:23:50 -0700395 case java9LibTag:
Colin Cross313aa542023-12-13 13:47:44 -0800396 if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800397 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars...)
398 } else {
Colin Cross6cef4812019-10-17 14:23:50 -0700399 ctx.ModuleErrorf("depends on non-java module %q", otherName)
400 }
Nan Zhang357466b2018-04-17 17:38:36 -0700401 case systemModulesTag:
402 if deps.systemModules != nil {
403 panic("Found two system module dependencies")
404 }
Colin Crossb61c2262024-08-08 14:04:42 -0700405 if sm, ok := android.OtherModuleProvider(ctx, module, SystemModulesProvider); ok {
406 deps.systemModules = &systemModules{sm.OutputDir, sm.OutputDirDeps}
407 } else {
408 ctx.PropertyErrorf("boot classpath dependency %q does not provide SystemModulesProvider",
409 ctx.OtherModuleName(module))
410 }
Jihoon Kang6592e872023-12-19 01:13:16 +0000411 case aconfigDeclarationTag:
412 if dep, ok := android.OtherModuleProvider(ctx, module, android.AconfigDeclarationsProviderKey); ok {
413 deps.aconfigProtoFiles = append(deps.aconfigProtoFiles, dep.IntermediateCacheOutputPath)
Yu Liu67a28422024-03-05 00:36:31 +0000414 } else if dep, ok := android.OtherModuleProvider(ctx, module, android.CodegenInfoProvider); ok {
Jihoon Kang38e4f252024-02-13 20:49:47 +0000415 deps.aconfigProtoFiles = append(deps.aconfigProtoFiles, dep.IntermediateCacheOutputPaths...)
Jihoon Kang6592e872023-12-19 01:13:16 +0000416 } else {
Jihoon Kang38e4f252024-02-13 20:49:47 +0000417 ctx.ModuleErrorf("Only aconfig_declarations and aconfig_declarations_group "+
418 "module type is allowed for flags_packages property, but %s is neither "+
419 "of these supported module types",
Jihoon Kang6592e872023-12-19 01:13:16 +0000420 module.Name(),
421 )
422 }
Nan Zhang581fd212018-01-10 16:06:12 -0800423 }
424 })
425 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
426 // may contain filegroup or genrule.
Colin Cross8a497952019-03-05 22:25:09 -0800427 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Ramy Medhatc7965cd2020-04-30 03:08:37 -0400428 j.implicits = append(j.implicits, srcFiles...)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900429
Jihoon Kang3921f0b2024-03-12 23:51:37 +0000430 // Module can depend on a java_aconfig_library module using the ":module_name{.tag}" syntax.
431 // Find the corresponding aconfig_declarations module name for such case.
432 for _, src := range j.properties.Srcs {
433 if moduleName, tag := android.SrcIsModuleWithTag(src); moduleName != "" {
Yu Liud3228ac2024-11-08 23:11:47 +0000434 otherModule := android.GetModuleProxyFromPathDep(ctx, moduleName, tag)
Jihoon Kang3921f0b2024-03-12 23:51:37 +0000435 if otherModule != nil {
Yu Liud3228ac2024-11-08 23:11:47 +0000436 if dep, ok := android.OtherModuleProvider(ctx, *otherModule, android.CodegenInfoProvider); ok {
Jihoon Kang3921f0b2024-03-12 23:51:37 +0000437 deps.aconfigProtoFiles = append(deps.aconfigProtoFiles, dep.IntermediateCacheOutputPaths...)
438 }
439 }
440 }
441 }
442
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900443 filterByPackage := func(srcs []android.Path, filterPackages []string) []android.Path {
444 if filterPackages == nil {
445 return srcs
446 }
447 filtered := []android.Path{}
448 for _, src := range srcs {
449 if src.Ext() != ".java" {
450 // Don't filter-out non-Java (=generated sources) by package names. This is not ideal,
451 // but otherwise metalava emits stub sources having references to the generated AIDL classes
452 // in filtered-out pacages (e.g. com.android.internal.*).
453 // TODO(b/141149570) We need to fix this by introducing default private constructors or
454 // fixing metalava to not emit constructors having references to unknown classes.
455 filtered = append(filtered, src)
456 continue
457 }
458 packageName := strings.ReplaceAll(filepath.Dir(src.Rel()), "/", ".")
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800459 if android.HasAnyPrefix(packageName, filterPackages) {
460 filtered = append(filtered, src)
Jiyong Parkc6ddccf2019-09-13 20:56:14 +0900461 }
462 }
463 return filtered
464 }
465 srcFiles = filterByPackage(srcFiles, j.properties.Filter_packages)
466
Liz Kammer585cac22020-07-06 09:12:57 -0700467 aidlFlags := j.collectAidlFlags(ctx, deps)
468 srcFiles = j.genSources(ctx, srcFiles, aidlFlags)
Nan Zhang581fd212018-01-10 16:06:12 -0800469
470 // srcs may depend on some genrule output.
471 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800472 j.srcJars = append(j.srcJars, deps.srcJars...)
473
Nan Zhang581fd212018-01-10 16:06:12 -0800474 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800475 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800476
Liz Kammere1ab2502020-09-10 15:29:25 +0000477 if len(j.srcFiles) > 0 {
478 j.sourcepaths = android.PathsForModuleSrc(ctx, []string{"."})
Nan Zhang581fd212018-01-10 16:06:12 -0800479 }
Nan Zhang581fd212018-01-10 16:06:12 -0800480
Colin Crossbc139922021-03-25 18:33:16 -0700481 return deps
482}
483
484func (j *Javadoc) expandArgs(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
485 var argFiles android.Paths
Paul Duffin99e4a502019-02-11 15:38:42 +0000486 argFilesMap := map[string]string{}
487 argFileLabels := []string{}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700488
Paul Duffin99e4a502019-02-11 15:38:42 +0000489 for _, label := range j.properties.Arg_files {
Colin Cross8a497952019-03-05 22:25:09 -0800490 var paths = android.PathsForModuleSrc(ctx, []string{label})
Paul Duffin99e4a502019-02-11 15:38:42 +0000491 if _, exists := argFilesMap[label]; !exists {
Colin Crossbc139922021-03-25 18:33:16 -0700492 argFilesMap[label] = strings.Join(cmd.PathsForInputs(paths), " ")
Paul Duffin99e4a502019-02-11 15:38:42 +0000493 argFileLabels = append(argFileLabels, label)
Colin Crossbc139922021-03-25 18:33:16 -0700494 argFiles = append(argFiles, paths...)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700495 } else {
496 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
Paul Duffin99e4a502019-02-11 15:38:42 +0000497 label, argFilesMap[label], paths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700498 }
499 }
500
Liz Kammer585cac22020-07-06 09:12:57 -0700501 var argsPropertyName string
502 flags := make([]string, 0)
503 if j.properties.Args != nil && j.properties.Flags != nil {
504 ctx.PropertyErrorf("args", "flags is set. Cannot set args")
505 } else if args := proptools.String(j.properties.Args); args != "" {
506 flags = append(flags, args)
507 argsPropertyName = "args"
508 } else {
509 flags = append(flags, j.properties.Flags...)
510 argsPropertyName = "flags"
511 }
Nan Zhang1598a9e2018-09-04 17:14:32 -0700512
Liz Kammer585cac22020-07-06 09:12:57 -0700513 for _, flag := range flags {
Colin Crossbc139922021-03-25 18:33:16 -0700514 expanded, err := android.Expand(flag, func(name string) (string, error) {
Liz Kammer585cac22020-07-06 09:12:57 -0700515 if strings.HasPrefix(name, "location ") {
516 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
517 if paths, ok := argFilesMap[label]; ok {
518 return paths, nil
519 } else {
520 return "", fmt.Errorf("unknown location label %q, expecting one of %q",
521 label, strings.Join(argFileLabels, ", "))
522 }
523 } else if name == "genDir" {
524 return android.PathForModuleGen(ctx).String(), nil
525 }
526 return "", fmt.Errorf("unknown variable '$(%s)'", name)
527 })
528
529 if err != nil {
530 ctx.PropertyErrorf(argsPropertyName, "%s", err.Error())
531 }
Colin Crossbc139922021-03-25 18:33:16 -0700532 cmd.Flag(expanded)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700533 }
534
Colin Crossbc139922021-03-25 18:33:16 -0700535 cmd.Implicits(argFiles)
Nan Zhang581fd212018-01-10 16:06:12 -0800536}
537
538func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
539 j.addDeps(ctx)
540}
541
542func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
543 deps := j.collectDeps(ctx)
544
Colin Crossdaa4c672019-07-15 22:53:46 -0700545 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800546
Colin Crossdaa4c672019-07-15 22:53:46 -0700547 outDir := android.PathForModuleOut(ctx, "out")
548 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
549
550 j.stubsSrcJar = nil
551
Colin Crossf1a035e2020-11-16 17:32:30 -0800552 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossdaa4c672019-07-15 22:53:46 -0700553
554 rule.Command().Text("rm -rf").Text(outDir.String())
555 rule.Command().Text("mkdir -p").Text(outDir.String())
556
557 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, j.srcJars)
Nan Zhang357466b2018-04-17 17:38:36 -0700558
Jiyong Parkf1691d22021-03-29 20:11:58 +0900559 javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), android.SdkContext(j))
Nan Zhang581fd212018-01-10 16:06:12 -0800560
Colin Crossdaa4c672019-07-15 22:53:46 -0700561 cmd := javadocSystemModulesCmd(ctx, rule, j.srcFiles, outDir, srcJarDir, srcJarList,
562 deps.systemModules, deps.classpath, j.sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800563
Colin Cross1e743852019-10-28 11:37:20 -0700564 cmd.FlagWithArg("-source ", javaVersion.String()).
Colin Crossdaa4c672019-07-15 22:53:46 -0700565 Flag("-J-Xmx1024m").
566 Flag("-XDignore.symbol.file").
567 Flag("-Xdoclint:none")
Nan Zhang581fd212018-01-10 16:06:12 -0800568
Colin Crossbc139922021-03-25 18:33:16 -0700569 j.expandArgs(ctx, cmd)
570
Colin Crossdaa4c672019-07-15 22:53:46 -0700571 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800572 BuiltTool("soong_zip").
Colin Crossdaa4c672019-07-15 22:53:46 -0700573 Flag("-write_if_changed").
574 Flag("-d").
575 FlagWithOutput("-o ", j.docZip).
576 FlagWithArg("-C ", outDir.String()).
577 FlagWithArg("-D ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700578
Colin Crossdaa4c672019-07-15 22:53:46 -0700579 rule.Restat()
580
581 zipSyncCleanupCmd(rule, srcJarDir)
582
Colin Crossf1a035e2020-11-16 17:32:30 -0800583 rule.Build("javadoc", "javadoc")
mrziwang74e50212024-06-27 10:14:24 -0700584
mrziwang74e50212024-06-27 10:14:24 -0700585 ctx.SetOutputFiles(android.Paths{j.docZip}, ".docs.zip")
Nan Zhang581fd212018-01-10 16:06:12 -0800586}
587
Nan Zhanga40da042018-08-01 12:48:00 -0700588// Droiddoc
Nan Zhanga40da042018-08-01 12:48:00 -0700589type Droiddoc struct {
590 Javadoc
591
Liz Kammere1ab2502020-09-10 15:29:25 +0000592 properties DroiddocProperties
Nan Zhanga40da042018-08-01 12:48:00 -0700593}
594
Colin Crossa3002fc2019-07-08 16:48:04 -0700595// droiddoc converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700596func DroiddocFactory() android.Module {
597 module := &Droiddoc{}
598
599 module.AddProperties(&module.properties,
600 &module.Javadoc.properties)
601
602 InitDroiddocModule(module, android.HostAndDeviceSupported)
603 return module
604}
605
Colin Crossa3002fc2019-07-08 16:48:04 -0700606// droiddoc_host converts .java source files to documentation using doclava or dokka.
Nan Zhanga40da042018-08-01 12:48:00 -0700607func DroiddocHostFactory() android.Module {
608 module := &Droiddoc{}
609
610 module.AddProperties(&module.properties,
611 &module.Javadoc.properties)
612
613 InitDroiddocModule(module, android.HostSupported)
614 return module
615}
616
Nan Zhang581fd212018-01-10 16:06:12 -0800617func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
618 d.Javadoc.addDeps(ctx)
619
Nan Zhang79614d12018-04-19 18:03:39 -0700620 if String(d.properties.Custom_template) != "" {
Dan Willemsencc090972018-02-26 14:33:31 -0800621 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
622 }
Nan Zhang581fd212018-01-10 16:06:12 -0800623}
624
Colin Crossab054432019-07-15 16:13:59 -0700625func (d *Droiddoc) doclavaDocsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, docletPath classpath) {
Colin Cross2a2e0db2020-02-21 16:55:46 -0800626 buildNumberFile := ctx.Config().BuildNumberFile(ctx)
Nan Zhang443fa522018-08-20 20:58:28 -0700627 // Droiddoc always gets "-source 1.8" because it doesn't support 1.9 sources. For modules with 1.9
628 // sources, droiddoc will get sources produced by metalava which will have already stripped out the
629 // 1.9 language features.
Jihoon Kangff878bf2022-12-22 21:26:06 +0000630 cmd.FlagWithArg("-source ", getStubsJavaVersion().String()).
Colin Crossab054432019-07-15 16:13:59 -0700631 Flag("-J-Xmx1600m").
632 Flag("-J-XX:-OmitStackTraceInFastThrow").
633 Flag("-XDignore.symbol.file").
Sorin Bascad528d562022-10-24 15:10:25 +0100634 Flag("--ignore-source-errors").
Sorin Bascaae995ae2023-03-01 08:47:42 +0000635 FlagWithArg("-doclet ", "com.google.doclava.Doclava").
Colin Crossab054432019-07-15 16:13:59 -0700636 FlagWithInputList("-docletpath ", docletPath.Paths(), ":").
Sorin Bascaae995ae2023-03-01 08:47:42 +0000637 FlagWithArg("-Xmaxerrs ", "10").
638 FlagWithArg("-Xmaxwarns ", "10").
Sorin Bascad528d562022-10-24 15:10:25 +0100639 Flag("-J--add-exports=jdk.javadoc/jdk.javadoc.internal.doclets.formats.html=ALL-UNNAMED").
Sorin Bascaa7b777f2023-06-09 09:19:36 +0000640 Flag("-J--add-exports=jdk.javadoc/jdk.javadoc.internal.tool=ALL-UNNAMED").
641 Flag("-J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED").
642 Flag("-J--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED").
643 Flag("-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED").
644 Flag("-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED").
Sorin Bascad528d562022-10-24 15:10:25 +0100645 Flag("-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED").
Colin Cross2a2e0db2020-02-21 16:55:46 -0800646 FlagWithArg("-hdf page.build ", ctx.Config().BuildId()+"-$(cat "+buildNumberFile.String()+")").OrderOnly(buildNumberFile).
Elliott Hughes26bce342019-09-12 15:05:13 -0700647 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 -0700648
Nan Zhanga40da042018-08-01 12:48:00 -0700649 if String(d.properties.Custom_template) == "" {
650 // TODO: This is almost always droiddoc-templates-sdk
651 ctx.PropertyErrorf("custom_template", "must specify a template")
652 }
653
654 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
Nan Zhangf4936b02018-08-01 15:00:28 -0700655 if t, ok := m.(*ExportedDroiddocDir); ok {
Colin Crossab054432019-07-15 16:13:59 -0700656 cmd.FlagWithArg("-templatedir ", t.dir.String()).Implicits(t.deps)
Nan Zhanga40da042018-08-01 12:48:00 -0700657 } else {
Paul Duffin884363e2019-12-19 10:21:09 +0000658 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_exported_dir", ctx.OtherModuleName(m))
Nan Zhanga40da042018-08-01 12:48:00 -0700659 }
660 })
661
662 if len(d.properties.Html_dirs) > 0 {
Colin Crossab054432019-07-15 16:13:59 -0700663 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
664 cmd.FlagWithArg("-htmldir ", htmlDir.String()).
665 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[0], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700666 }
667
668 if len(d.properties.Html_dirs) > 1 {
Colin Crossab054432019-07-15 16:13:59 -0700669 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
670 cmd.FlagWithArg("-htmldir2 ", htmlDir2.String()).
671 Implicits(android.PathsForModuleSrc(ctx, []string{filepath.Join(d.properties.Html_dirs[1], "**/*")}))
Nan Zhanga40da042018-08-01 12:48:00 -0700672 }
673
674 if len(d.properties.Html_dirs) > 2 {
675 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
676 }
677
Colin Cross8a497952019-03-05 22:25:09 -0800678 knownTags := android.PathsForModuleSrc(ctx, d.properties.Knowntags)
Colin Crossab054432019-07-15 16:13:59 -0700679 cmd.FlagForEachInput("-knowntags ", knownTags)
Nan Zhanga40da042018-08-01 12:48:00 -0700680
Colin Crossab054432019-07-15 16:13:59 -0700681 cmd.FlagForEachArg("-hdf ", d.properties.Hdf)
Nan Zhanga40da042018-08-01 12:48:00 -0700682
683 if String(d.properties.Proofread_file) != "" {
684 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
Colin Crossab054432019-07-15 16:13:59 -0700685 cmd.FlagWithOutput("-proofread ", proofreadFile)
Nan Zhanga40da042018-08-01 12:48:00 -0700686 }
687
688 if String(d.properties.Todo_file) != "" {
689 // tricky part:
690 // we should not compute full path for todo_file through PathForModuleOut().
691 // the non-standard doclet will get the full path relative to "-o".
Colin Crossab054432019-07-15 16:13:59 -0700692 cmd.FlagWithArg("-todo ", String(d.properties.Todo_file)).
693 ImplicitOutput(android.PathForModuleOut(ctx, String(d.properties.Todo_file)))
Nan Zhanga40da042018-08-01 12:48:00 -0700694 }
695
Anton Hanssonb06bb572023-10-03 12:11:35 +0000696 if String(d.properties.Lint_baseline) != "" {
697 cmd.FlagWithInput("-lintbaseline ", android.PathForModuleSrc(ctx, String(d.properties.Lint_baseline)))
698 }
699
Nan Zhanga40da042018-08-01 12:48:00 -0700700 if String(d.properties.Resourcesdir) != "" {
701 // TODO: should we add files under resourcesDir to the implicits? It seems that
702 // resourcesDir is one sub dir of htmlDir
703 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
Colin Crossab054432019-07-15 16:13:59 -0700704 cmd.FlagWithArg("-resourcesdir ", resourcesDir.String())
Nan Zhanga40da042018-08-01 12:48:00 -0700705 }
706
707 if String(d.properties.Resourcesoutdir) != "" {
708 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
Colin Crossab054432019-07-15 16:13:59 -0700709 cmd.FlagWithArg("-resourcesoutdir ", String(d.properties.Resourcesoutdir))
Nan Zhanga40da042018-08-01 12:48:00 -0700710 }
Nan Zhanga40da042018-08-01 12:48:00 -0700711}
712
Colin Crossab054432019-07-15 16:13:59 -0700713func (d *Droiddoc) postDoclavaCmds(ctx android.ModuleContext, rule *android.RuleBuilder) {
Nan Zhanga40da042018-08-01 12:48:00 -0700714 if String(d.properties.Static_doc_index_redirect) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700715 staticDocIndexRedirect := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_index_redirect))
716 rule.Command().Text("cp").
717 Input(staticDocIndexRedirect).
718 Output(android.PathForModuleOut(ctx, "out", "index.html"))
Nan Zhanga40da042018-08-01 12:48:00 -0700719 }
720
721 if String(d.properties.Static_doc_properties) != "" {
Colin Crossab054432019-07-15 16:13:59 -0700722 staticDocProperties := android.PathForModuleSrc(ctx, String(d.properties.Static_doc_properties))
723 rule.Command().Text("cp").
724 Input(staticDocProperties).
725 Output(android.PathForModuleOut(ctx, "out", "source.properties"))
Nan Zhanga40da042018-08-01 12:48:00 -0700726 }
Nan Zhanga40da042018-08-01 12:48:00 -0700727}
728
Colin Crossab054432019-07-15 16:13:59 -0700729func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Colin Crossdaa4c672019-07-15 22:53:46 -0700730 outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
Colin Crossab054432019-07-15 16:13:59 -0700731
732 cmd := rule.Command().
Sorin Bascad528d562022-10-24 15:10:25 +0100733 BuiltTool("soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
Colin Crossab054432019-07-15 16:13:59 -0700734 Flag(config.JavacVmFlags).
Colin Cross70c47412021-03-12 17:48:14 -0800735 FlagWithRspFileInputList("@", android.PathForModuleOut(ctx, "javadoc.rsp"), srcs).
Colin Crossab054432019-07-15 16:13:59 -0700736 FlagWithInput("@", srcJarList)
737
Colin Crossab054432019-07-15 16:13:59 -0700738 // TODO(ccross): Remove this if- statement once we finish migration for all Doclava
739 // based stubs generation.
740 // In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
741 // dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
742 // the correct package name base path.
743 if len(sourcepaths) > 0 {
744 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
745 } else {
746 cmd.FlagWithArg("-sourcepath ", srcJarDir.String())
747 }
748
749 cmd.FlagWithArg("-d ", outDir.String()).
750 Flag("-quiet")
751
752 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700753}
754
Colin Crossdaa4c672019-07-15 22:53:46 -0700755func javadocSystemModulesCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
756 outDir, srcJarDir, srcJarList android.Path, systemModules *systemModules,
757 classpath classpath, sourcepaths android.Paths) *android.RuleBuilderCommand {
758
759 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
760
761 flag, deps := systemModules.FormJavaSystemModulesPath(ctx.Device())
762 cmd.Flag(flag).Implicits(deps)
763
764 cmd.FlagWithArg("--patch-module ", "java.base=.")
765
766 if len(classpath) > 0 {
767 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
768 }
769
770 return cmd
Nan Zhang1598a9e2018-09-04 17:14:32 -0700771}
772
Colin Crossdaa4c672019-07-15 22:53:46 -0700773func javadocBootclasspathCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
774 outDir, srcJarDir, srcJarList android.Path, bootclasspath, classpath classpath,
775 sourcepaths android.Paths) *android.RuleBuilderCommand {
776
777 cmd := javadocCmd(ctx, rule, srcs, outDir, srcJarDir, srcJarList, sourcepaths)
778
779 if len(bootclasspath) == 0 && ctx.Device() {
780 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
781 // ensure java does not fall back to the default bootclasspath.
782 cmd.FlagWithArg("-bootclasspath ", `""`)
783 } else if len(bootclasspath) > 0 {
784 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
785 }
786
787 if len(classpath) > 0 {
788 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
789 }
790
791 return cmd
792}
793
Colin Crossab054432019-07-15 16:13:59 -0700794func dokkaCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
795 outDir, srcJarDir android.Path, bootclasspath, classpath classpath) *android.RuleBuilderCommand {
Nan Zhang1598a9e2018-09-04 17:14:32 -0700796
Colin Crossab054432019-07-15 16:13:59 -0700797 // Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
798 dokkaClasspath := append(bootclasspath.Paths(), classpath.Paths()...)
799
800 return rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800801 BuiltTool("dokka").
Colin Crossab054432019-07-15 16:13:59 -0700802 Flag(config.JavacVmFlags).
Sorin Basca1d68e482022-09-08 16:48:01 +0100803 Flag("-J--add-opens=java.base/java.lang=ALL-UNNAMED").
Colin Crossab054432019-07-15 16:13:59 -0700804 Flag(srcJarDir.String()).
805 FlagWithInputList("-classpath ", dokkaClasspath, ":").
806 FlagWithArg("-format ", "dac").
807 FlagWithArg("-dacRoot ", "/reference/kotlin").
808 FlagWithArg("-output ", outDir.String())
Nan Zhang1598a9e2018-09-04 17:14:32 -0700809}
810
811func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
812 deps := d.Javadoc.collectDeps(ctx)
813
Colin Crossdaa4c672019-07-15 22:53:46 -0700814 d.Javadoc.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Colin Crossdaa4c672019-07-15 22:53:46 -0700815
Colin Crossae5330a2021-11-03 13:31:22 -0700816 jsilver := ctx.Config().HostJavaToolPath(ctx, "jsilver.jar")
817 doclava := ctx.Config().HostJavaToolPath(ctx, "doclava.jar")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700818
Colin Crossab054432019-07-15 16:13:59 -0700819 outDir := android.PathForModuleOut(ctx, "out")
820 srcJarDir := android.PathForModuleOut(ctx, "srcjars")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700821
Colin Crossf1a035e2020-11-16 17:32:30 -0800822 rule := android.NewRuleBuilder(pctx, ctx)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700823
Colin Crossab054432019-07-15 16:13:59 -0700824 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
825
826 var cmd *android.RuleBuilderCommand
Nan Zhang1598a9e2018-09-04 17:14:32 -0700827 if Bool(d.properties.Dokka_enabled) {
Colin Crossab054432019-07-15 16:13:59 -0700828 cmd = dokkaCmd(ctx, rule, outDir, srcJarDir, deps.bootClasspath, deps.classpath)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700829 } else {
Colin Crossdaa4c672019-07-15 22:53:46 -0700830 cmd = javadocBootclasspathCmd(ctx, rule, d.Javadoc.srcFiles, outDir, srcJarDir, srcJarList,
Colin Crossab054432019-07-15 16:13:59 -0700831 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
Nan Zhang1598a9e2018-09-04 17:14:32 -0700832 }
833
Colin Crossbc139922021-03-25 18:33:16 -0700834 d.expandArgs(ctx, cmd)
Colin Crossab054432019-07-15 16:13:59 -0700835
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000836 if d.properties.Compat_config != nil {
837 compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
838 cmd.FlagWithInput("-compatconfig ", compatConfig)
839 }
840
Colin Crossab054432019-07-15 16:13:59 -0700841 var desc string
842 if Bool(d.properties.Dokka_enabled) {
843 desc = "dokka"
844 } else {
Sorin Bascaae995ae2023-03-01 08:47:42 +0000845 d.doclavaDocsFlags(ctx, cmd, classpath{jsilver, doclava})
Colin Crossab054432019-07-15 16:13:59 -0700846
847 for _, o := range d.Javadoc.properties.Out {
848 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
849 }
850
851 d.postDoclavaCmds(ctx, rule)
852 desc = "doclava"
853 }
854
855 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800856 BuiltTool("soong_zip").
Colin Crossab054432019-07-15 16:13:59 -0700857 Flag("-write_if_changed").
858 Flag("-d").
859 FlagWithOutput("-o ", d.docZip).
860 FlagWithArg("-C ", outDir.String()).
861 FlagWithArg("-D ", outDir.String())
862
Sorin Bascaae995ae2023-03-01 08:47:42 +0000863 rule.Restat()
Colin Crossab054432019-07-15 16:13:59 -0700864
Sorin Bascaae995ae2023-03-01 08:47:42 +0000865 zipSyncCleanupCmd(rule, srcJarDir)
Colin Crossab054432019-07-15 16:13:59 -0700866
Colin Crossf1a035e2020-11-16 17:32:30 -0800867 rule.Build("javadoc", desc)
mrziwang74e50212024-06-27 10:14:24 -0700868
869 ctx.SetOutputFiles(android.Paths{d.Javadoc.docZip}, "")
870 ctx.SetOutputFiles(android.Paths{d.Javadoc.docZip}, ".docs.zip")
Nan Zhang1598a9e2018-09-04 17:14:32 -0700871}
872
Nan Zhangf4936b02018-08-01 15:00:28 -0700873// Exported Droiddoc Directory
Dan Willemsencc090972018-02-26 14:33:31 -0800874var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
875
Nan Zhangf4936b02018-08-01 15:00:28 -0700876type ExportedDroiddocDirProperties struct {
877 // path to the directory containing Droiddoc related files.
Dan Willemsencc090972018-02-26 14:33:31 -0800878 Path *string
879}
880
Yu Liu3a892962025-01-15 23:14:27 +0000881type ExportedDroiddocDirInfo struct {
882 Deps android.Paths
883 Dir android.Path
884}
885
886var ExportedDroiddocDirInfoProvider = blueprint.NewProvider[ExportedDroiddocDirInfo]()
887
Nan Zhangf4936b02018-08-01 15:00:28 -0700888type ExportedDroiddocDir struct {
Dan Willemsencc090972018-02-26 14:33:31 -0800889 android.ModuleBase
890
Nan Zhangf4936b02018-08-01 15:00:28 -0700891 properties ExportedDroiddocDirProperties
Dan Willemsencc090972018-02-26 14:33:31 -0800892
893 deps android.Paths
894 dir android.Path
895}
896
Colin Crossa3002fc2019-07-08 16:48:04 -0700897// droiddoc_exported_dir exports a directory of html templates or nullability annotations for use by doclava.
Nan Zhangf4936b02018-08-01 15:00:28 -0700898func ExportedDroiddocDirFactory() android.Module {
899 module := &ExportedDroiddocDir{}
Dan Willemsencc090972018-02-26 14:33:31 -0800900 module.AddProperties(&module.properties)
901 android.InitAndroidModule(module)
902 return module
903}
904
Nan Zhangf4936b02018-08-01 15:00:28 -0700905func (d *ExportedDroiddocDir) DepsMutator(android.BottomUpMutatorContext) {}
Dan Willemsencc090972018-02-26 14:33:31 -0800906
Nan Zhangf4936b02018-08-01 15:00:28 -0700907func (d *ExportedDroiddocDir) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross07e51612019-03-05 12:46:40 -0800908 path := String(d.properties.Path)
909 d.dir = android.PathForModuleSrc(ctx, path)
Colin Cross8a497952019-03-05 22:25:09 -0800910 d.deps = android.PathsForModuleSrc(ctx, []string{filepath.Join(path, "**/*")})
Yu Liu3a892962025-01-15 23:14:27 +0000911
912 android.SetProvider(ctx, ExportedDroiddocDirInfoProvider, ExportedDroiddocDirInfo{
913 Dir: d.dir,
914 Deps: d.deps,
915 })
Dan Willemsencc090972018-02-26 14:33:31 -0800916}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800917
Nan Zhangb2b33de2018-02-23 11:18:47 -0800918// Defaults
Nan Zhangb2b33de2018-02-23 11:18:47 -0800919type DocDefaults struct {
920 android.ModuleBase
921 android.DefaultsModuleBase
922}
923
Nan Zhangb2b33de2018-02-23 11:18:47 -0800924func DocDefaultsFactory() android.Module {
925 module := &DocDefaults{}
926
927 module.AddProperties(
928 &JavadocProperties{},
929 &DroiddocProperties{},
930 )
931
932 android.InitDefaultsModule(module)
933
934 return module
935}
Nan Zhang1598a9e2018-09-04 17:14:32 -0700936
Colin Cross33961b52019-07-11 11:01:22 -0700937func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
938 srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
939
Colin Cross1661aff2021-03-12 17:56:51 -0800940 cmd := rule.Command()
941 cmd.Text("rm -rf").Text(cmd.PathForOutput(srcJarDir))
942 cmd = rule.Command()
943 cmd.Text("mkdir -p").Text(cmd.PathForOutput(srcJarDir))
Colin Cross33961b52019-07-11 11:01:22 -0700944 srcJarList := srcJarDir.Join(ctx, "list")
945
946 rule.Temporary(srcJarList)
947
Colin Cross1661aff2021-03-12 17:56:51 -0800948 cmd = rule.Command()
949 cmd.BuiltTool("zipsync").
950 FlagWithArg("-d ", cmd.PathForOutput(srcJarDir)).
Colin Cross33961b52019-07-11 11:01:22 -0700951 FlagWithOutput("-l ", srcJarList).
952 FlagWithArg("-f ", `"*.java"`).
953 Inputs(srcJars)
954
955 return srcJarList
956}
957
958func zipSyncCleanupCmd(rule *android.RuleBuilder, srcJarDir android.ModuleOutPath) {
959 rule.Command().Text("rm -rf").Text(srcJarDir.String())
960}