blob: 7cf1710622806da49eb5975a84204aaa67b9433f [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 (
18 "android/soong/android"
19 "android/soong/java/config"
20 "fmt"
Nan Zhangb2b33de2018-02-23 11:18:47 -080021 "path/filepath"
Nan Zhang46130972018-06-04 11:28:01 -070022 "runtime"
Nan Zhang581fd212018-01-10 16:06:12 -080023 "strings"
24
25 "github.com/google/blueprint"
26)
27
28var (
29 javadoc = pctx.AndroidStaticRule("javadoc",
30 blueprint.RuleParams{
31 Command: `rm -rf "$outDir" "$srcJarDir" "$stubsDir" && mkdir -p "$outDir" "$srcJarDir" "$stubsDir" && ` +
Colin Cross436b7652018-03-15 16:24:10 -070032 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
Nan Zhang581fd212018-01-10 16:06:12 -080033 `${config.JavadocCmd} -encoding UTF-8 @$out.rsp @$srcJarDir/list ` +
34 `$opts $bootclasspathArgs $classpathArgs -sourcepath $sourcepath ` +
35 `-d $outDir -quiet && ` +
36 `${config.SoongZipCmd} -write_if_changed -d -o $docZip -C $outDir -D $outDir && ` +
37 `${config.SoongZipCmd} -write_if_changed -jar -o $out -C $stubsDir -D $stubsDir`,
38 CommandDeps: []string{
Colin Cross436b7652018-03-15 16:24:10 -070039 "${config.ZipSyncCmd}",
Nan Zhang581fd212018-01-10 16:06:12 -080040 "${config.JavadocCmd}",
41 "${config.SoongZipCmd}",
Nan Zhang581fd212018-01-10 16:06:12 -080042 },
43 Rspfile: "$out.rsp",
44 RspfileContent: "$in",
45 Restat: true,
46 },
47 "outDir", "srcJarDir", "stubsDir", "srcJars", "opts",
Nan Zhang30963742018-04-23 09:59:14 -070048 "bootclasspathArgs", "classpathArgs", "sourcepath", "docZip")
Nan Zhang61819ce2018-05-04 18:49:16 -070049
50 apiCheck = pctx.AndroidStaticRule("apiCheck",
51 blueprint.RuleParams{
52 Command: `( ${config.ApiCheckCmd} -JXmx1024m -J"classpath $classpath" $opts ` +
53 `$apiFile $apiFileToCheck $removedApiFile $removedApiFileToCheck ` +
Jiyong Parkeeb8a642018-05-12 22:21:20 +090054 `&& touch $out ) || (echo -e "$msg" ; exit 38)`,
Nan Zhang61819ce2018-05-04 18:49:16 -070055 CommandDeps: []string{
56 "${config.ApiCheckCmd}",
57 },
58 },
59 "classpath", "opts", "apiFile", "apiFileToCheck", "removedApiFile", "removedApiFileToCheck", "msg")
60
61 updateApi = pctx.AndroidStaticRule("updateApi",
62 blueprint.RuleParams{
63 Command: `( ( cp -f $apiFileToCheck $apiFile && cp -f $removedApiFileToCheck $removedApiFile ) ` +
64 `&& touch $out ) || (echo failed to update public API ; exit 38)`,
65 },
66 "apiFile", "apiFileToCheck", "removedApiFile", "removedApiFileToCheck")
Nan Zhang581fd212018-01-10 16:06:12 -080067)
68
69func init() {
Nan Zhangb2b33de2018-02-23 11:18:47 -080070 android.RegisterModuleType("doc_defaults", DocDefaultsFactory)
71
Nan Zhang581fd212018-01-10 16:06:12 -080072 android.RegisterModuleType("droiddoc", DroiddocFactory)
73 android.RegisterModuleType("droiddoc_host", DroiddocHostFactory)
Dan Willemsencc090972018-02-26 14:33:31 -080074 android.RegisterModuleType("droiddoc_template", DroiddocTemplateFactory)
Nan Zhang581fd212018-01-10 16:06:12 -080075 android.RegisterModuleType("javadoc", JavadocFactory)
76 android.RegisterModuleType("javadoc_host", JavadocHostFactory)
77}
78
79type JavadocProperties struct {
80 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
81 // or .aidl files.
82 Srcs []string `android:"arch_variant"`
83
84 // list of directories rooted at the Android.bp file that will
85 // be added to the search paths for finding source files when passing package names.
Nan Zhangb2b33de2018-02-23 11:18:47 -080086 Local_sourcepaths []string
Nan Zhang581fd212018-01-10 16:06:12 -080087
88 // list of source files that should not be used to build the Java module.
89 // This is most useful in the arch/multilib variants to remove non-common files
90 // filegroup or genrule can be included within this property.
91 Exclude_srcs []string `android:"arch_variant"`
92
Nan Zhangb2b33de2018-02-23 11:18:47 -080093 // list of java libraries that will be in the classpath.
Nan Zhang581fd212018-01-10 16:06:12 -080094 Libs []string `android:"arch_variant"`
95
Nan Zhange66c7272018-03-06 12:59:27 -080096 // don't build against the framework libraries (legacy-test, core-junit,
97 // ext, and framework for device targets)
98 No_framework_libs *bool
99
Nan Zhangb2b33de2018-02-23 11:18:47 -0800100 // the java library (in classpath) for documentation that provides java srcs and srcjars.
101 Srcs_lib *string
102
103 // the base dirs under srcs_lib will be scanned for java srcs.
104 Srcs_lib_whitelist_dirs []string
105
106 // the sub dirs under srcs_lib_whitelist_dirs will be scanned for java srcs.
107 Srcs_lib_whitelist_pkgs []string
108
Nan Zhang581fd212018-01-10 16:06:12 -0800109 // If set to false, don't allow this module(-docs.zip) to be exported. Defaults to true.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800110 Installable *bool
Nan Zhang581fd212018-01-10 16:06:12 -0800111
112 // if not blank, set to the version of the sdk to compile against
113 Sdk_version *string `android:"arch_variant"`
Jiyong Park1e440682018-05-23 18:42:04 +0900114
115 Aidl struct {
116 // Top level directories to pass to aidl tool
117 Include_dirs []string
118
119 // Directories rooted at the Android.bp file to pass to aidl tool
120 Local_include_dirs []string
121 }
Nan Zhang581fd212018-01-10 16:06:12 -0800122}
123
Nan Zhang61819ce2018-05-04 18:49:16 -0700124type ApiToCheck struct {
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900125 // path to the API txt file that the new API extracted from source code is checked
126 // against. The path can be local to the module or from other module (via :module syntax).
Nan Zhang61819ce2018-05-04 18:49:16 -0700127 Api_file *string
128
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900129 // path to the API txt file that the new @removed API extractd from source code is
130 // checked against. The path can be local to the module or from other module (via
131 // :module syntax).
Nan Zhang61819ce2018-05-04 18:49:16 -0700132 Removed_api_file *string
133
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900134 // Arguments to the apicheck tool.
Nan Zhang61819ce2018-05-04 18:49:16 -0700135 Args *string
136}
137
Nan Zhang581fd212018-01-10 16:06:12 -0800138type DroiddocProperties struct {
139 // directory relative to top of the source tree that contains doc templates files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800140 Custom_template *string
Nan Zhang581fd212018-01-10 16:06:12 -0800141
142 // directories relative to top of the source tree which contains html/jd files.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800143 Html_dirs []string
Nan Zhang581fd212018-01-10 16:06:12 -0800144
145 // set a value in the Clearsilver hdf namespace.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800146 Hdf []string
Nan Zhang581fd212018-01-10 16:06:12 -0800147
148 // proofread file contains all of the text content of the javadocs concatenated into one file,
149 // suitable for spell-checking and other goodness.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800150 Proofread_file *string
Nan Zhang581fd212018-01-10 16:06:12 -0800151
152 // a todo file lists the program elements that are missing documentation.
153 // At some point, this might be improved to show more warnings.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800154 Todo_file *string
155
156 // directory under current module source that provide additional resources (images).
157 Resourcesdir *string
158
159 // resources output directory under out/soong/.intermediates.
160 Resourcesoutdir *string
Nan Zhang581fd212018-01-10 16:06:12 -0800161
162 // local files that are used within user customized droiddoc options.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800163 Arg_files []string
Nan Zhang581fd212018-01-10 16:06:12 -0800164
165 // user customized droiddoc args.
166 // Available variables for substitution:
167 //
168 // $(location <label>): the path to the arg_files with name <label>
Nan Zhangb2b33de2018-02-23 11:18:47 -0800169 Args *string
Nan Zhang581fd212018-01-10 16:06:12 -0800170
171 // names of the output files used in args that will be generated
Nan Zhangb2b33de2018-02-23 11:18:47 -0800172 Out []string
Nan Zhang581fd212018-01-10 16:06:12 -0800173
174 // a list of files under current module source dir which contains known tags in Java sources.
175 // filegroup or genrule can be included within this property.
Nan Zhangb2b33de2018-02-23 11:18:47 -0800176 Knowntags []string
Nan Zhang28c68b92018-03-13 16:17:01 -0700177
178 // the tag name used to distinguish if the API files belong to public/system/test.
179 Api_tag_name *string
180
181 // the generated public API filename by Doclava.
182 Api_filename *string
183
184 // the generated private API filename by Doclava.
185 Private_api_filename *string
186
187 // the generated private Dex API filename by Doclava.
188 Private_dex_api_filename *string
189
190 // the generated removed API filename by Doclava.
191 Removed_api_filename *string
192
David Brazdilaac0c3c2018-04-24 16:23:29 +0100193 // the generated removed Dex API filename by Doclava.
194 Removed_dex_api_filename *string
195
Nan Zhang28c68b92018-03-13 16:17:01 -0700196 // the generated exact API filename by Doclava.
197 Exact_api_filename *string
Nan Zhang853f4202018-04-12 16:55:56 -0700198
199 // if set to false, don't allow droiddoc to generate stubs source files. Defaults to true.
200 Create_stubs *bool
Nan Zhang61819ce2018-05-04 18:49:16 -0700201
202 Check_api struct {
203 Last_released ApiToCheck
204
205 Current ApiToCheck
206 }
Nan Zhang581fd212018-01-10 16:06:12 -0800207}
208
209type Javadoc struct {
210 android.ModuleBase
211 android.DefaultableModuleBase
212
213 properties JavadocProperties
214
215 srcJars android.Paths
216 srcFiles android.Paths
217 sourcepaths android.Paths
218
Nan Zhangccff0f72018-03-08 17:26:16 -0800219 docZip android.WritablePath
220 stubsSrcJar android.WritablePath
Nan Zhang581fd212018-01-10 16:06:12 -0800221}
222
Nan Zhangb2b33de2018-02-23 11:18:47 -0800223func (j *Javadoc) Srcs() android.Paths {
224 return android.Paths{j.stubsSrcJar}
225}
226
227var _ android.SourceFileProducer = (*Javadoc)(nil)
228
Nan Zhang581fd212018-01-10 16:06:12 -0800229type Droiddoc struct {
230 Javadoc
231
Nan Zhang28c68b92018-03-13 16:17:01 -0700232 properties DroiddocProperties
233 apiFile android.WritablePath
234 privateApiFile android.WritablePath
235 privateDexApiFile android.WritablePath
236 removedApiFile android.WritablePath
David Brazdilaac0c3c2018-04-24 16:23:29 +0100237 removedDexApiFile android.WritablePath
Nan Zhang28c68b92018-03-13 16:17:01 -0700238 exactApiFile android.WritablePath
Nan Zhang61819ce2018-05-04 18:49:16 -0700239
240 checkCurrentApiTimestamp android.WritablePath
241 updateCurrentApiTimestamp android.WritablePath
242 checkLastReleasedApiTimestamp android.WritablePath
Nan Zhang581fd212018-01-10 16:06:12 -0800243}
244
245func InitDroiddocModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
246 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
247 android.InitDefaultableModule(module)
248}
249
250func JavadocFactory() android.Module {
251 module := &Javadoc{}
252
253 module.AddProperties(&module.properties)
254
255 InitDroiddocModule(module, android.HostAndDeviceSupported)
256 return module
257}
258
259func JavadocHostFactory() android.Module {
260 module := &Javadoc{}
261
262 module.AddProperties(&module.properties)
263
264 InitDroiddocModule(module, android.HostSupported)
265 return module
266}
267
268func DroiddocFactory() android.Module {
269 module := &Droiddoc{}
270
271 module.AddProperties(&module.properties,
272 &module.Javadoc.properties)
273
274 InitDroiddocModule(module, android.HostAndDeviceSupported)
275 return module
276}
277
278func DroiddocHostFactory() android.Module {
279 module := &Droiddoc{}
280
281 module.AddProperties(&module.properties,
282 &module.Javadoc.properties)
283
284 InitDroiddocModule(module, android.HostSupported)
285 return module
286}
287
288func (j *Javadoc) addDeps(ctx android.BottomUpMutatorContext) {
289 if ctx.Device() {
290 sdkDep := decodeSdkDep(ctx, String(j.properties.Sdk_version))
291 if sdkDep.useDefaultLibs {
292 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Nan Zhang9cbe6772018-03-21 17:56:39 -0700293 if !Bool(j.properties.No_framework_libs) {
Nan Zhange66c7272018-03-06 12:59:27 -0800294 ctx.AddDependency(ctx.Module(), libTag, []string{"ext", "framework"}...)
295 }
Nan Zhang581fd212018-01-10 16:06:12 -0800296 } else if sdkDep.useModule {
Colin Cross86a60ae2018-05-29 14:44:55 -0700297 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.modules...)
Nan Zhang581fd212018-01-10 16:06:12 -0800298 }
299 }
300
301 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
302
303 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
304
305 // exclude_srcs may contain filegroup or genrule.
306 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
307}
308
Nan Zhangb2b33de2018-02-23 11:18:47 -0800309func (j *Javadoc) genWhitelistPathPrefixes(whitelistPathPrefixes map[string]bool) {
310 for _, dir := range j.properties.Srcs_lib_whitelist_dirs {
311 for _, pkg := range j.properties.Srcs_lib_whitelist_pkgs {
Jiyong Park82484c02018-04-23 21:41:26 +0900312 // convert foo.bar.baz to foo/bar/baz
313 pkgAsPath := filepath.Join(strings.Split(pkg, ".")...)
314 prefix := filepath.Join(dir, pkgAsPath)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800315 if _, found := whitelistPathPrefixes[prefix]; !found {
316 whitelistPathPrefixes[prefix] = true
317 }
318 }
319 }
320}
321
Jiyong Park1e440682018-05-23 18:42:04 +0900322func (j *Javadoc) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
323 var flags javaBuilderFlags
324
325 // aidl flags.
326 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
327 if len(aidlFlags) > 0 {
328 // optimization.
329 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
330 flags.aidlFlags = "$aidlFlags"
331 }
332
333 return flags
334}
335
336func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
337 aidlIncludeDirs android.Paths) []string {
338
339 aidlIncludes := android.PathsForModuleSrc(ctx, j.properties.Aidl.Local_include_dirs)
340 aidlIncludes = append(aidlIncludes, android.PathsForSource(ctx, j.properties.Aidl.Include_dirs)...)
341
342 var flags []string
343 if aidlPreprocess.Valid() {
344 flags = append(flags, "-p"+aidlPreprocess.String())
345 } else {
346 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
347 }
348
349 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
350 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
351 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
352 flags = append(flags, "-I"+src.String())
353 }
354
355 return flags
356}
357
358func (j *Javadoc) genSources(ctx android.ModuleContext, srcFiles android.Paths,
359 flags javaBuilderFlags) android.Paths {
360
361 outSrcFiles := make(android.Paths, 0, len(srcFiles))
362
363 for _, srcFile := range srcFiles {
364 switch srcFile.Ext() {
365 case ".aidl":
366 javaFile := genAidl(ctx, srcFile, flags.aidlFlags)
367 outSrcFiles = append(outSrcFiles, javaFile)
368 default:
369 outSrcFiles = append(outSrcFiles, srcFile)
370 }
371 }
372
373 return outSrcFiles
374}
375
Nan Zhang581fd212018-01-10 16:06:12 -0800376func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
377 var deps deps
378
379 sdkDep := decodeSdkDep(ctx, String(j.properties.Sdk_version))
380 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700381 ctx.AddMissingDependencies(sdkDep.modules)
Nan Zhang581fd212018-01-10 16:06:12 -0800382 } else if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700383 deps.bootClasspath = append(deps.bootClasspath, sdkDep.jars...)
Nan Zhang581fd212018-01-10 16:06:12 -0800384 }
385
386 ctx.VisitDirectDeps(func(module android.Module) {
387 otherName := ctx.OtherModuleName(module)
388 tag := ctx.OtherModuleDependencyTag(module)
389
390 switch dep := module.(type) {
391 case Dependency:
392 switch tag {
393 case bootClasspathTag:
394 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars()...)
395 case libTag:
396 deps.classpath = append(deps.classpath, dep.ImplementationJars()...)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800397 if otherName == String(j.properties.Srcs_lib) {
398 srcs := dep.(SrcDependency).CompiledSrcs()
399 whitelistPathPrefixes := make(map[string]bool)
400 j.genWhitelistPathPrefixes(whitelistPathPrefixes)
401 for _, src := range srcs {
402 if _, ok := src.(android.WritablePath); ok { // generated sources
403 deps.srcs = append(deps.srcs, src)
404 } else { // select source path for documentation based on whitelist path prefixs.
405 for k, _ := range whitelistPathPrefixes {
406 if strings.HasPrefix(src.Rel(), k) {
407 deps.srcs = append(deps.srcs, src)
408 break
409 }
410 }
411 }
412 }
413 deps.srcJars = append(deps.srcJars, dep.(SrcDependency).CompiledSrcJars()...)
414 }
Nan Zhang581fd212018-01-10 16:06:12 -0800415 default:
416 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
417 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900418 case SdkLibraryDependency:
419 switch tag {
420 case libTag:
421 sdkVersion := String(j.properties.Sdk_version)
422 linkType := javaSdk
423 if strings.HasPrefix(sdkVersion, "system_") || strings.HasPrefix(sdkVersion, "test_") {
424 linkType = javaSystem
425 } else if sdkVersion == "" {
426 linkType = javaPlatform
427 }
428 deps.classpath = append(deps.classpath, dep.HeaderJars(linkType)...)
429 default:
430 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
431 }
Nan Zhang581fd212018-01-10 16:06:12 -0800432 case android.SourceFileProducer:
433 switch tag {
434 case libTag:
435 checkProducesJars(ctx, dep)
436 deps.classpath = append(deps.classpath, dep.Srcs()...)
437 case android.DefaultsDepTag, android.SourceDepTag:
438 // Nothing to do
439 default:
440 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs", otherName)
441 }
442 default:
443 switch tag {
Dan Willemsencc090972018-02-26 14:33:31 -0800444 case android.DefaultsDepTag, android.SourceDepTag, droiddocTemplateTag:
Nan Zhang581fd212018-01-10 16:06:12 -0800445 // Nothing to do
446 default:
447 ctx.ModuleErrorf("depends on non-java module %q", otherName)
448 }
449 }
450 })
451 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
452 // may contain filegroup or genrule.
453 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Jiyong Park1e440682018-05-23 18:42:04 +0900454 flags := j.collectBuilderFlags(ctx, deps)
455 srcFiles = j.genSources(ctx, srcFiles, flags)
Nan Zhang581fd212018-01-10 16:06:12 -0800456
457 // srcs may depend on some genrule output.
458 j.srcJars = srcFiles.FilterByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800459 j.srcJars = append(j.srcJars, deps.srcJars...)
460
Nan Zhang581fd212018-01-10 16:06:12 -0800461 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800462 j.srcFiles = append(j.srcFiles, deps.srcs...)
Nan Zhang581fd212018-01-10 16:06:12 -0800463
464 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhangccff0f72018-03-08 17:26:16 -0800465 j.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
Nan Zhang581fd212018-01-10 16:06:12 -0800466
467 if j.properties.Local_sourcepaths == nil {
468 j.properties.Local_sourcepaths = append(j.properties.Local_sourcepaths, ".")
469 }
470 j.sourcepaths = android.PathsForModuleSrc(ctx, j.properties.Local_sourcepaths)
Nan Zhang581fd212018-01-10 16:06:12 -0800471
472 return deps
473}
474
475func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
476 j.addDeps(ctx)
477}
478
479func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
480 deps := j.collectDeps(ctx)
481
482 var implicits android.Paths
483 implicits = append(implicits, deps.bootClasspath...)
484 implicits = append(implicits, deps.classpath...)
485
486 var bootClasspathArgs, classpathArgs string
487 if ctx.Config().UseOpenJDK9() {
488 if len(deps.bootClasspath) > 0 {
489 // For OpenJDK 9 we use --patch-module to define the core libraries code.
490 // TODO(tobiast): Reorganize this when adding proper support for OpenJDK 9
491 // modules. Here we treat all code in core libraries as being in java.base
492 // to work around the OpenJDK 9 module system. http://b/62049770
493 bootClasspathArgs = "--patch-module=java.base=" + strings.Join(deps.bootClasspath.Strings(), ":")
494 }
495 } else {
496 if len(deps.bootClasspath.Strings()) > 0 {
497 // For OpenJDK 8 we can use -bootclasspath to define the core libraries code.
498 bootClasspathArgs = deps.bootClasspath.FormJavaClassPath("-bootclasspath")
499 }
500 }
501 if len(deps.classpath.Strings()) > 0 {
502 classpathArgs = "-classpath " + strings.Join(deps.classpath.Strings(), ":")
503 }
504
505 implicits = append(implicits, j.srcJars...)
506
507 opts := "-J-Xmx1024m -XDignore.symbol.file -Xdoclint:none"
508
509 ctx.Build(pctx, android.BuildParams{
510 Rule: javadoc,
511 Description: "Javadoc",
Nan Zhangccff0f72018-03-08 17:26:16 -0800512 Output: j.stubsSrcJar,
Nan Zhang581fd212018-01-10 16:06:12 -0800513 ImplicitOutput: j.docZip,
514 Inputs: j.srcFiles,
515 Implicits: implicits,
516 Args: map[string]string{
517 "outDir": android.PathForModuleOut(ctx, "docs", "out").String(),
518 "srcJarDir": android.PathForModuleOut(ctx, "docs", "srcjars").String(),
519 "stubsDir": android.PathForModuleOut(ctx, "docs", "stubsDir").String(),
520 "srcJars": strings.Join(j.srcJars.Strings(), " "),
521 "opts": opts,
Nan Zhang853f4202018-04-12 16:55:56 -0700522 "bootclasspathArgs": bootClasspathArgs,
Nan Zhang581fd212018-01-10 16:06:12 -0800523 "classpathArgs": classpathArgs,
524 "sourcepath": strings.Join(j.sourcepaths.Strings(), ":"),
525 "docZip": j.docZip.String(),
526 },
527 })
528}
529
Nan Zhang61819ce2018-05-04 18:49:16 -0700530func (d *Droiddoc) checkCurrentApi() bool {
531 if String(d.properties.Check_api.Current.Api_file) != "" &&
532 String(d.properties.Check_api.Current.Removed_api_file) != "" {
533 return true
534 } else if String(d.properties.Check_api.Current.Api_file) != "" {
535 panic("check_api.current.removed_api_file: has to be non empty!")
536 } else if String(d.properties.Check_api.Current.Removed_api_file) != "" {
537 panic("check_api.current.api_file: has to be non empty!")
538 }
539
540 return false
541}
542
543func (d *Droiddoc) checkLastReleasedApi() bool {
544 if String(d.properties.Check_api.Last_released.Api_file) != "" &&
545 String(d.properties.Check_api.Last_released.Removed_api_file) != "" {
546 return true
547 } else if String(d.properties.Check_api.Last_released.Api_file) != "" {
548 panic("check_api.last_released.removed_api_file: has to be non empty!")
549 } else if String(d.properties.Check_api.Last_released.Removed_api_file) != "" {
550 panic("check_api.last_released.api_file: has to be non empty!")
551 }
552
553 return false
554}
555
Nan Zhang581fd212018-01-10 16:06:12 -0800556func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
557 d.Javadoc.addDeps(ctx)
558
Dan Willemsencc090972018-02-26 14:33:31 -0800559 if String(d.properties.Custom_template) == "" {
560 // TODO: This is almost always droiddoc-templates-sdk
561 ctx.PropertyErrorf("custom_template", "must specify a template")
562 } else {
563 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
564 }
565
Nan Zhang581fd212018-01-10 16:06:12 -0800566 // extra_arg_files may contains filegroup or genrule.
567 android.ExtractSourcesDeps(ctx, d.properties.Arg_files)
568
569 // knowntags may contain filegroup or genrule.
570 android.ExtractSourcesDeps(ctx, d.properties.Knowntags)
Nan Zhang61819ce2018-05-04 18:49:16 -0700571
572 if d.checkCurrentApi() {
573 android.ExtractSourceDeps(ctx, d.properties.Check_api.Current.Api_file)
574 android.ExtractSourceDeps(ctx, d.properties.Check_api.Current.Removed_api_file)
575 }
576
577 if d.checkLastReleasedApi() {
578 android.ExtractSourceDeps(ctx, d.properties.Check_api.Last_released.Api_file)
579 android.ExtractSourceDeps(ctx, d.properties.Check_api.Last_released.Removed_api_file)
580 }
Nan Zhang581fd212018-01-10 16:06:12 -0800581}
582
583func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
584 deps := d.Javadoc.collectDeps(ctx)
585
586 var implicits android.Paths
587 implicits = append(implicits, deps.bootClasspath...)
588 implicits = append(implicits, deps.classpath...)
589
590 argFiles := ctx.ExpandSources(d.properties.Arg_files, nil)
591 argFilesMap := map[string]android.Path{}
592
593 for _, f := range argFiles {
594 implicits = append(implicits, f)
595 if _, exists := argFilesMap[f.Rel()]; !exists {
596 argFilesMap[f.Rel()] = f
597 } else {
598 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
599 f, argFilesMap[f.Rel()], f.Rel())
600 }
601 }
602
603 args, err := android.Expand(String(d.properties.Args), func(name string) (string, error) {
604 if strings.HasPrefix(name, "location ") {
605 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
606 if f, ok := argFilesMap[label]; ok {
607 return f.String(), nil
608 } else {
609 return "", fmt.Errorf("unknown location label %q", label)
610 }
611 } else if name == "genDir" {
612 return android.PathForModuleGen(ctx).String(), nil
613 }
614 return "", fmt.Errorf("unknown variable '$(%s)'", name)
615 })
616
617 if err != nil {
618 ctx.PropertyErrorf("extra_args", "%s", err.Error())
619 return
620 }
621
622 var bootClasspathArgs, classpathArgs string
623 if len(deps.bootClasspath.Strings()) > 0 {
624 bootClasspathArgs = "-bootclasspath " + strings.Join(deps.bootClasspath.Strings(), ":")
625 }
626 if len(deps.classpath.Strings()) > 0 {
627 classpathArgs = "-classpath " + strings.Join(deps.classpath.Strings(), ":")
628 }
629
Dan Willemsencc090972018-02-26 14:33:31 -0800630 var templateDir string
631 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
632 if t, ok := m.(*DroiddocTemplate); ok {
633 implicits = append(implicits, t.deps...)
634 templateDir = t.dir.String()
635 } else {
636 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_template", ctx.OtherModuleName(m))
637 }
638 })
Nan Zhang581fd212018-01-10 16:06:12 -0800639
640 var htmlDirArgs string
641 if len(d.properties.Html_dirs) > 0 {
Dan Willemsencc090972018-02-26 14:33:31 -0800642 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
643 implicits = append(implicits, ctx.Glob(htmlDir.Join(ctx, "**/*").String(), nil)...)
644 htmlDirArgs = "-htmldir " + htmlDir.String()
Nan Zhang581fd212018-01-10 16:06:12 -0800645 }
646
647 var htmlDir2Args string
648 if len(d.properties.Html_dirs) > 1 {
Dan Willemsencc090972018-02-26 14:33:31 -0800649 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
650 implicits = append(implicits, ctx.Glob(htmlDir2.Join(ctx, "**/*").String(), nil)...)
651 htmlDir2Args = "-htmldir2 " + htmlDir2.String()
652 }
653
654 if len(d.properties.Html_dirs) > 2 {
655 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
Nan Zhang581fd212018-01-10 16:06:12 -0800656 }
657
658 knownTags := ctx.ExpandSources(d.properties.Knowntags, nil)
659 implicits = append(implicits, knownTags...)
660
661 for _, kt := range knownTags {
662 args = args + " -knowntags " + kt.String()
663 }
664 for _, hdf := range d.properties.Hdf {
665 args = args + " -hdf " + hdf
666 }
667
668 if String(d.properties.Proofread_file) != "" {
669 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
670 args = args + " -proofread " + proofreadFile.String()
671 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800672
Nan Zhang581fd212018-01-10 16:06:12 -0800673 if String(d.properties.Todo_file) != "" {
674 // tricky part:
675 // we should not compute full path for todo_file through PathForModuleOut().
676 // the non-standard doclet will get the full path relative to "-o".
677 args = args + " -todo " + String(d.properties.Todo_file)
678 }
679
Nan Zhangb2b33de2018-02-23 11:18:47 -0800680 if String(d.properties.Resourcesdir) != "" {
681 // TODO: should we add files under resourcesDir to the implicits? It seems that
682 // resourcesDir is one sub dir of htmlDir
683 resourcesDir := android.PathForModuleSrc(ctx, String(d.properties.Resourcesdir))
684 args = args + " -resourcesdir " + resourcesDir.String()
685 }
686
687 if String(d.properties.Resourcesoutdir) != "" {
688 // TODO: it seems -resourceoutdir reference/android/images/ didn't get generated anywhere.
689 args = args + " -resourcesoutdir " + String(d.properties.Resourcesoutdir)
690 }
691
Nan Zhang28c68b92018-03-13 16:17:01 -0700692 var implicitOutputs android.WritablePaths
Nan Zhang61819ce2018-05-04 18:49:16 -0700693
694 if d.checkCurrentApi() || d.checkLastReleasedApi() || String(d.properties.Api_filename) != "" {
695 d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
Nan Zhang28c68b92018-03-13 16:17:01 -0700696 args = args + " -api " + d.apiFile.String()
697 implicitOutputs = append(implicitOutputs, d.apiFile)
698 }
699
Nan Zhang61819ce2018-05-04 18:49:16 -0700700 if d.checkCurrentApi() || d.checkLastReleasedApi() || String(d.properties.Removed_api_filename) != "" {
701 d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
702 args = args + " -removedApi " + d.removedApiFile.String()
703 implicitOutputs = append(implicitOutputs, d.removedApiFile)
704 }
705
Nan Zhang28c68b92018-03-13 16:17:01 -0700706 if String(d.properties.Private_api_filename) != "" {
707 d.privateApiFile = android.PathForModuleOut(ctx, String(d.properties.Private_api_filename))
708 args = args + " -privateApi " + d.privateApiFile.String()
709 implicitOutputs = append(implicitOutputs, d.privateApiFile)
710 }
711
712 if String(d.properties.Private_dex_api_filename) != "" {
713 d.privateDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Private_dex_api_filename))
714 args = args + " -privateDexApi " + d.privateDexApiFile.String()
715 implicitOutputs = append(implicitOutputs, d.privateDexApiFile)
716 }
717
David Brazdilaac0c3c2018-04-24 16:23:29 +0100718 if String(d.properties.Removed_dex_api_filename) != "" {
719 d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
720 args = args + " -removedDexApi " + d.removedDexApiFile.String()
721 implicitOutputs = append(implicitOutputs, d.removedDexApiFile)
722 }
723
Nan Zhang28c68b92018-03-13 16:17:01 -0700724 if String(d.properties.Exact_api_filename) != "" {
725 d.exactApiFile = android.PathForModuleOut(ctx, String(d.properties.Exact_api_filename))
726 args = args + " -exactApi " + d.exactApiFile.String()
727 implicitOutputs = append(implicitOutputs, d.exactApiFile)
728 }
729
Nan Zhang581fd212018-01-10 16:06:12 -0800730 implicits = append(implicits, d.Javadoc.srcJars...)
731
Nan Zhang30963742018-04-23 09:59:14 -0700732 jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
733 doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
734 implicits = append(implicits, jsilver)
735 implicits = append(implicits, doclava)
736
Nan Zhang46130972018-06-04 11:28:01 -0700737 var date string
738 if runtime.GOOS == "darwin" {
739 date = `date -r`
740 } else {
741 date = `date -d`
742 }
743
Nan Zhang581fd212018-01-10 16:06:12 -0800744 opts := "-source 1.8 -J-Xmx1600m -J-XX:-OmitStackTraceInFastThrow -XDignore.symbol.file " +
Nan Zhang30963742018-04-23 09:59:14 -0700745 "-doclet com.google.doclava.Doclava -docletpath " + jsilver.String() + ":" + doclava.String() + " " +
Colin Cross480cd762018-02-22 14:39:17 -0800746 "-templatedir " + templateDir + " " + htmlDirArgs + " " + htmlDir2Args + " " +
Nan Zhang581fd212018-01-10 16:06:12 -0800747 "-hdf page.build " + ctx.Config().BuildId() + "-" + ctx.Config().BuildNumberFromFile() + " " +
Nan Zhang46130972018-06-04 11:28:01 -0700748 `-hdf page.now "$$(` + date + ` @$$(cat ` + ctx.Config().Getenv("BUILD_DATETIME_FILE") + `) "+%d %b %Y %k:%M")" ` +
Nan Zhang853f4202018-04-12 16:55:56 -0700749 " " + args
Nan Zhang46130972018-06-04 11:28:01 -0700750
Nan Zhang853f4202018-04-12 16:55:56 -0700751 if BoolDefault(d.properties.Create_stubs, true) {
752 opts += " -stubs " + android.PathForModuleOut(ctx, "docs", "stubsDir").String()
753 }
Nan Zhang581fd212018-01-10 16:06:12 -0800754
Nan Zhang581fd212018-01-10 16:06:12 -0800755 implicitOutputs = append(implicitOutputs, d.Javadoc.docZip)
756 for _, o := range d.properties.Out {
757 implicitOutputs = append(implicitOutputs, android.PathForModuleGen(ctx, o))
758 }
759
760 ctx.Build(pctx, android.BuildParams{
761 Rule: javadoc,
762 Description: "Droiddoc",
Nan Zhangccff0f72018-03-08 17:26:16 -0800763 Output: d.Javadoc.stubsSrcJar,
Nan Zhang581fd212018-01-10 16:06:12 -0800764 Inputs: d.Javadoc.srcFiles,
765 Implicits: implicits,
766 ImplicitOutputs: implicitOutputs,
767 Args: map[string]string{
768 "outDir": android.PathForModuleOut(ctx, "docs", "out").String(),
769 "srcJarDir": android.PathForModuleOut(ctx, "docs", "srcjars").String(),
770 "stubsDir": android.PathForModuleOut(ctx, "docs", "stubsDir").String(),
771 "srcJars": strings.Join(d.Javadoc.srcJars.Strings(), " "),
772 "opts": opts,
773 "bootclasspathArgs": bootClasspathArgs,
774 "classpathArgs": classpathArgs,
775 "sourcepath": strings.Join(d.Javadoc.sourcepaths.Strings(), ":"),
776 "docZip": d.Javadoc.docZip.String(),
Nan Zhang581fd212018-01-10 16:06:12 -0800777 },
778 })
Nan Zhang61819ce2018-05-04 18:49:16 -0700779
780 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
781
782 checkApiClasspath := classpath{jsilver, doclava, android.PathForSource(ctx, java8Home, "lib/tools.jar")}
783
784 if d.checkCurrentApi() && !ctx.Config().IsPdkBuild() {
785 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
786
787 apiFile := ctx.ExpandSource(String(d.properties.Check_api.Current.Api_file),
788 "check_api.current.api_file")
789 removedApiFile := ctx.ExpandSource(String(d.properties.Check_api.Current.Removed_api_file),
790 "check_api.current_removed_api_file")
791
792 ctx.Build(pctx, android.BuildParams{
793 Rule: apiCheck,
794 Description: "Current API check",
795 Output: d.checkCurrentApiTimestamp,
796 Inputs: nil,
797 Implicits: append(android.Paths{apiFile, removedApiFile, d.apiFile, d.removedApiFile},
798 checkApiClasspath...),
799 Args: map[string]string{
800 "classpath": checkApiClasspath.FormJavaClassPath(""),
801 "opts": String(d.properties.Check_api.Current.Args),
802 "apiFile": apiFile.String(),
803 "apiFileToCheck": d.apiFile.String(),
804 "removedApiFile": removedApiFile.String(),
805 "removedApiFileToCheck": d.removedApiFile.String(),
806 "msg": fmt.Sprintf(`\n******************************\n`+
807 `You have tried to change the API from what has been previously approved.\n\n`+
808 `To make these errors go away, you have two choices:\n`+
809 ` 1. You can add '@hide' javadoc comments to the methods, etc. listed in the\n`+
810 ` errors above.\n\n`+
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900811 ` 2. You can update current.txt by executing the following command:\n`+
Nan Zhang61819ce2018-05-04 18:49:16 -0700812 ` make %s-update-current-api\n\n`+
Jiyong Parkeeb8a642018-05-12 22:21:20 +0900813 ` To submit the revised current.txt to the main Android repository,\n`+
Nan Zhang61819ce2018-05-04 18:49:16 -0700814 ` you will need approval.\n`+
815 `******************************\n`, ctx.ModuleName()),
816 },
817 })
818
819 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
820
821 ctx.Build(pctx, android.BuildParams{
822 Rule: updateApi,
823 Description: "update current API",
824 Output: d.updateCurrentApiTimestamp,
825 Implicits: append(android.Paths{}, apiFile, removedApiFile, d.apiFile, d.removedApiFile),
826 Args: map[string]string{
827 "apiFile": apiFile.String(),
828 "apiFileToCheck": d.apiFile.String(),
829 "removedApiFile": removedApiFile.String(),
830 "removedApiFileToCheck": d.removedApiFile.String(),
831 },
832 })
833 }
834
835 if d.checkLastReleasedApi() && !ctx.Config().IsPdkBuild() {
836 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
837
838 apiFile := ctx.ExpandSource(String(d.properties.Check_api.Last_released.Api_file),
839 "check_api.last_released.api_file")
840 removedApiFile := ctx.ExpandSource(String(d.properties.Check_api.Last_released.Removed_api_file),
841 "check_api.last_released.removed_api_file")
842
843 ctx.Build(pctx, android.BuildParams{
844 Rule: apiCheck,
845 Description: "Last Released API check",
846 Output: d.checkLastReleasedApiTimestamp,
847 Inputs: nil,
848 Implicits: append(android.Paths{apiFile, removedApiFile, d.apiFile, d.removedApiFile},
849 checkApiClasspath...),
850 Args: map[string]string{
851 "classpath": checkApiClasspath.FormJavaClassPath(""),
852 "opts": String(d.properties.Check_api.Last_released.Args),
853 "apiFile": apiFile.String(),
854 "apiFileToCheck": d.apiFile.String(),
855 "removedApiFile": removedApiFile.String(),
856 "removedApiFileToCheck": d.removedApiFile.String(),
857 "msg": `\n******************************\n` +
858 `You have tried to change the API from what has been previously released in\n` +
859 `an SDK. Please fix the errors listed above.\n` +
860 `******************************\n`,
861 },
862 })
863 }
Nan Zhang581fd212018-01-10 16:06:12 -0800864}
Dan Willemsencc090972018-02-26 14:33:31 -0800865
866var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
867
868type DroiddocTemplateProperties struct {
869 // path to the directory containing the droiddoc templates.
870 Path *string
871}
872
873type DroiddocTemplate struct {
874 android.ModuleBase
875
876 properties DroiddocTemplateProperties
877
878 deps android.Paths
879 dir android.Path
880}
881
882func DroiddocTemplateFactory() android.Module {
883 module := &DroiddocTemplate{}
884 module.AddProperties(&module.properties)
885 android.InitAndroidModule(module)
886 return module
887}
888
889func (d *DroiddocTemplate) DepsMutator(android.BottomUpMutatorContext) {}
890
891func (d *DroiddocTemplate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
892 path := android.PathForModuleSrc(ctx, String(d.properties.Path))
893 d.dir = path
894 d.deps = ctx.Glob(path.Join(ctx, "**/*").String(), nil)
895}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800896
897//
898// Defaults
899//
900type DocDefaults struct {
901 android.ModuleBase
902 android.DefaultsModuleBase
903}
904
905func (*DocDefaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
906}
907
908func (d *DocDefaults) DepsMutator(ctx android.BottomUpMutatorContext) {
909}
910
911func DocDefaultsFactory() android.Module {
912 module := &DocDefaults{}
913
914 module.AddProperties(
915 &JavadocProperties{},
916 &DroiddocProperties{},
917 )
918
919 android.InitDefaultsModule(module)
920
921 return module
922}