blob: eaa6c5fc7c2d748b491805660a29519c7c8de27e [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 Zhang581fd212018-01-10 16:06:12 -080021 "strings"
22
23 "github.com/google/blueprint"
24)
25
26var (
27 javadoc = pctx.AndroidStaticRule("javadoc",
28 blueprint.RuleParams{
29 Command: `rm -rf "$outDir" "$srcJarDir" "$stubsDir" && mkdir -p "$outDir" "$srcJarDir" "$stubsDir" && ` +
30 `${config.ExtractSrcJarsCmd} $srcJarDir $srcJarDir/list $srcJars && ` +
31 `${config.JavadocCmd} -encoding UTF-8 @$out.rsp @$srcJarDir/list ` +
32 `$opts $bootclasspathArgs $classpathArgs -sourcepath $sourcepath ` +
33 `-d $outDir -quiet && ` +
34 `${config.SoongZipCmd} -write_if_changed -d -o $docZip -C $outDir -D $outDir && ` +
35 `${config.SoongZipCmd} -write_if_changed -jar -o $out -C $stubsDir -D $stubsDir`,
36 CommandDeps: []string{
37 "${config.ExtractSrcJarsCmd}",
38 "${config.JavadocCmd}",
39 "${config.SoongZipCmd}",
40 "$JsilverJar",
41 "$DoclavaJar",
42 },
43 Rspfile: "$out.rsp",
44 RspfileContent: "$in",
45 Restat: true,
46 },
47 "outDir", "srcJarDir", "stubsDir", "srcJars", "opts",
48 "bootclasspathArgs", "classpathArgs", "sourcepath", "docZip", "JsilverJar", "DoclavaJar")
49)
50
51func init() {
52 android.RegisterModuleType("droiddoc", DroiddocFactory)
53 android.RegisterModuleType("droiddoc_host", DroiddocHostFactory)
Dan Willemsencc090972018-02-26 14:33:31 -080054 android.RegisterModuleType("droiddoc_template", DroiddocTemplateFactory)
Nan Zhang581fd212018-01-10 16:06:12 -080055 android.RegisterModuleType("javadoc", JavadocFactory)
56 android.RegisterModuleType("javadoc_host", JavadocHostFactory)
57}
58
59type JavadocProperties struct {
60 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
61 // or .aidl files.
62 Srcs []string `android:"arch_variant"`
63
64 // list of directories rooted at the Android.bp file that will
65 // be added to the search paths for finding source files when passing package names.
66 Local_sourcepaths []string `android:"arch_variant"`
67
68 // list of source files that should not be used to build the Java module.
69 // This is most useful in the arch/multilib variants to remove non-common files
70 // filegroup or genrule can be included within this property.
71 Exclude_srcs []string `android:"arch_variant"`
72
73 // list of of java libraries that will be in the classpath.
74 Libs []string `android:"arch_variant"`
75
Nan Zhange66c7272018-03-06 12:59:27 -080076 // don't build against the framework libraries (legacy-test, core-junit,
77 // ext, and framework for device targets)
78 No_framework_libs *bool
79
Nan Zhang581fd212018-01-10 16:06:12 -080080 // If set to false, don't allow this module(-docs.zip) to be exported. Defaults to true.
81 Installable *bool `android:"arch_variant"`
82
83 // if not blank, set to the version of the sdk to compile against
84 Sdk_version *string `android:"arch_variant"`
85}
86
87type DroiddocProperties struct {
88 // directory relative to top of the source tree that contains doc templates files.
Dan Willemsencc090972018-02-26 14:33:31 -080089 Custom_template *string `android:"arch_variant"`
Nan Zhang581fd212018-01-10 16:06:12 -080090
91 // directories relative to top of the source tree which contains html/jd files.
92 Html_dirs []string `android:"arch_variant"`
93
94 // set a value in the Clearsilver hdf namespace.
95 Hdf []string `android:"arch_variant"`
96
97 // proofread file contains all of the text content of the javadocs concatenated into one file,
98 // suitable for spell-checking and other goodness.
99 Proofread_file *string `android:"arch_variant"`
100
101 // a todo file lists the program elements that are missing documentation.
102 // At some point, this might be improved to show more warnings.
103 Todo_file *string `android:"arch_variant"`
104
105 // local files that are used within user customized droiddoc options.
106 Arg_files []string `android:"arch_variant"`
107
108 // user customized droiddoc args.
109 // Available variables for substitution:
110 //
111 // $(location <label>): the path to the arg_files with name <label>
112 Args *string `android:"arch_variant"`
113
114 // names of the output files used in args that will be generated
115 Out []string `android:"arch_variant"`
116
117 // a list of files under current module source dir which contains known tags in Java sources.
118 // filegroup or genrule can be included within this property.
119 Knowntags []string `android:"arch_variant"`
120}
121
122type Javadoc struct {
123 android.ModuleBase
124 android.DefaultableModuleBase
125
126 properties JavadocProperties
127
128 srcJars android.Paths
129 srcFiles android.Paths
130 sourcepaths android.Paths
131
Nan Zhangccff0f72018-03-08 17:26:16 -0800132 docZip android.WritablePath
133 stubsSrcJar android.WritablePath
Nan Zhang581fd212018-01-10 16:06:12 -0800134}
135
136type Droiddoc struct {
137 Javadoc
138
139 properties DroiddocProperties
140}
141
142func InitDroiddocModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
143 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
144 android.InitDefaultableModule(module)
145}
146
147func JavadocFactory() android.Module {
148 module := &Javadoc{}
149
150 module.AddProperties(&module.properties)
151
152 InitDroiddocModule(module, android.HostAndDeviceSupported)
153 return module
154}
155
156func JavadocHostFactory() android.Module {
157 module := &Javadoc{}
158
159 module.AddProperties(&module.properties)
160
161 InitDroiddocModule(module, android.HostSupported)
162 return module
163}
164
165func DroiddocFactory() android.Module {
166 module := &Droiddoc{}
167
168 module.AddProperties(&module.properties,
169 &module.Javadoc.properties)
170
171 InitDroiddocModule(module, android.HostAndDeviceSupported)
172 return module
173}
174
175func DroiddocHostFactory() android.Module {
176 module := &Droiddoc{}
177
178 module.AddProperties(&module.properties,
179 &module.Javadoc.properties)
180
181 InitDroiddocModule(module, android.HostSupported)
182 return module
183}
184
185func (j *Javadoc) addDeps(ctx android.BottomUpMutatorContext) {
186 if ctx.Device() {
187 sdkDep := decodeSdkDep(ctx, String(j.properties.Sdk_version))
188 if sdkDep.useDefaultLibs {
189 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Nan Zhange66c7272018-03-06 12:59:27 -0800190 if Bool(j.properties.No_framework_libs) {
191 ctx.AddDependency(ctx.Module(), libTag, []string{"ext", "framework"}...)
192 }
Nan Zhang581fd212018-01-10 16:06:12 -0800193 } else if sdkDep.useModule {
194 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
195 }
196 }
197
198 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
199
200 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
201
202 // exclude_srcs may contain filegroup or genrule.
203 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
204}
205
206func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
207 var deps deps
208
209 sdkDep := decodeSdkDep(ctx, String(j.properties.Sdk_version))
210 if sdkDep.invalidVersion {
211 ctx.AddMissingDependencies([]string{sdkDep.module})
212 } else if sdkDep.useFiles {
213 deps.bootClasspath = append(deps.bootClasspath, sdkDep.jar)
214 }
215
216 ctx.VisitDirectDeps(func(module android.Module) {
217 otherName := ctx.OtherModuleName(module)
218 tag := ctx.OtherModuleDependencyTag(module)
219
220 switch dep := module.(type) {
221 case Dependency:
222 switch tag {
223 case bootClasspathTag:
224 deps.bootClasspath = append(deps.bootClasspath, dep.ImplementationJars()...)
225 case libTag:
226 deps.classpath = append(deps.classpath, dep.ImplementationJars()...)
227 default:
228 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
229 }
230 case android.SourceFileProducer:
231 switch tag {
232 case libTag:
233 checkProducesJars(ctx, dep)
234 deps.classpath = append(deps.classpath, dep.Srcs()...)
235 case android.DefaultsDepTag, android.SourceDepTag:
236 // Nothing to do
237 default:
238 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs", otherName)
239 }
240 default:
241 switch tag {
Dan Willemsencc090972018-02-26 14:33:31 -0800242 case android.DefaultsDepTag, android.SourceDepTag, droiddocTemplateTag:
Nan Zhang581fd212018-01-10 16:06:12 -0800243 // Nothing to do
244 default:
245 ctx.ModuleErrorf("depends on non-java module %q", otherName)
246 }
247 }
248 })
249 // do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
250 // may contain filegroup or genrule.
251 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
252
253 // srcs may depend on some genrule output.
254 j.srcJars = srcFiles.FilterByExt(".srcjar")
255 j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
256
257 j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
Nan Zhangccff0f72018-03-08 17:26:16 -0800258 j.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
Nan Zhang581fd212018-01-10 16:06:12 -0800259
260 if j.properties.Local_sourcepaths == nil {
261 j.properties.Local_sourcepaths = append(j.properties.Local_sourcepaths, ".")
262 }
263 j.sourcepaths = android.PathsForModuleSrc(ctx, j.properties.Local_sourcepaths)
264 j.sourcepaths = append(j.sourcepaths, deps.bootClasspath...)
265 j.sourcepaths = append(j.sourcepaths, deps.classpath...)
266
267 return deps
268}
269
270func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
271 j.addDeps(ctx)
272}
273
274func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
275 deps := j.collectDeps(ctx)
276
277 var implicits android.Paths
278 implicits = append(implicits, deps.bootClasspath...)
279 implicits = append(implicits, deps.classpath...)
280
281 var bootClasspathArgs, classpathArgs string
282 if ctx.Config().UseOpenJDK9() {
283 if len(deps.bootClasspath) > 0 {
284 // For OpenJDK 9 we use --patch-module to define the core libraries code.
285 // TODO(tobiast): Reorganize this when adding proper support for OpenJDK 9
286 // modules. Here we treat all code in core libraries as being in java.base
287 // to work around the OpenJDK 9 module system. http://b/62049770
288 bootClasspathArgs = "--patch-module=java.base=" + strings.Join(deps.bootClasspath.Strings(), ":")
289 }
290 } else {
291 if len(deps.bootClasspath.Strings()) > 0 {
292 // For OpenJDK 8 we can use -bootclasspath to define the core libraries code.
293 bootClasspathArgs = deps.bootClasspath.FormJavaClassPath("-bootclasspath")
294 }
295 }
296 if len(deps.classpath.Strings()) > 0 {
297 classpathArgs = "-classpath " + strings.Join(deps.classpath.Strings(), ":")
298 }
299
300 implicits = append(implicits, j.srcJars...)
301
302 opts := "-J-Xmx1024m -XDignore.symbol.file -Xdoclint:none"
303
304 ctx.Build(pctx, android.BuildParams{
305 Rule: javadoc,
306 Description: "Javadoc",
Nan Zhangccff0f72018-03-08 17:26:16 -0800307 Output: j.stubsSrcJar,
Nan Zhang581fd212018-01-10 16:06:12 -0800308 ImplicitOutput: j.docZip,
309 Inputs: j.srcFiles,
310 Implicits: implicits,
311 Args: map[string]string{
312 "outDir": android.PathForModuleOut(ctx, "docs", "out").String(),
313 "srcJarDir": android.PathForModuleOut(ctx, "docs", "srcjars").String(),
314 "stubsDir": android.PathForModuleOut(ctx, "docs", "stubsDir").String(),
315 "srcJars": strings.Join(j.srcJars.Strings(), " "),
316 "opts": opts,
317 "bootClasspathArgs": bootClasspathArgs,
318 "classpathArgs": classpathArgs,
319 "sourcepath": strings.Join(j.sourcepaths.Strings(), ":"),
320 "docZip": j.docZip.String(),
321 },
322 })
323}
324
325func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
326 d.Javadoc.addDeps(ctx)
327
Dan Willemsencc090972018-02-26 14:33:31 -0800328 if String(d.properties.Custom_template) == "" {
329 // TODO: This is almost always droiddoc-templates-sdk
330 ctx.PropertyErrorf("custom_template", "must specify a template")
331 } else {
332 ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
333 }
334
Nan Zhang581fd212018-01-10 16:06:12 -0800335 // extra_arg_files may contains filegroup or genrule.
336 android.ExtractSourcesDeps(ctx, d.properties.Arg_files)
337
338 // knowntags may contain filegroup or genrule.
339 android.ExtractSourcesDeps(ctx, d.properties.Knowntags)
340}
341
342func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
343 deps := d.Javadoc.collectDeps(ctx)
344
345 var implicits android.Paths
346 implicits = append(implicits, deps.bootClasspath...)
347 implicits = append(implicits, deps.classpath...)
348
349 argFiles := ctx.ExpandSources(d.properties.Arg_files, nil)
350 argFilesMap := map[string]android.Path{}
351
352 for _, f := range argFiles {
353 implicits = append(implicits, f)
354 if _, exists := argFilesMap[f.Rel()]; !exists {
355 argFilesMap[f.Rel()] = f
356 } else {
357 ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
358 f, argFilesMap[f.Rel()], f.Rel())
359 }
360 }
361
362 args, err := android.Expand(String(d.properties.Args), func(name string) (string, error) {
363 if strings.HasPrefix(name, "location ") {
364 label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
365 if f, ok := argFilesMap[label]; ok {
366 return f.String(), nil
367 } else {
368 return "", fmt.Errorf("unknown location label %q", label)
369 }
370 } else if name == "genDir" {
371 return android.PathForModuleGen(ctx).String(), nil
372 }
373 return "", fmt.Errorf("unknown variable '$(%s)'", name)
374 })
375
376 if err != nil {
377 ctx.PropertyErrorf("extra_args", "%s", err.Error())
378 return
379 }
380
381 var bootClasspathArgs, classpathArgs string
382 if len(deps.bootClasspath.Strings()) > 0 {
383 bootClasspathArgs = "-bootclasspath " + strings.Join(deps.bootClasspath.Strings(), ":")
384 }
385 if len(deps.classpath.Strings()) > 0 {
386 classpathArgs = "-classpath " + strings.Join(deps.classpath.Strings(), ":")
387 }
388
Dan Willemsencc090972018-02-26 14:33:31 -0800389 var templateDir string
390 ctx.VisitDirectDepsWithTag(droiddocTemplateTag, func(m android.Module) {
391 if t, ok := m.(*DroiddocTemplate); ok {
392 implicits = append(implicits, t.deps...)
393 templateDir = t.dir.String()
394 } else {
395 ctx.PropertyErrorf("custom_template", "module %q is not a droiddoc_template", ctx.OtherModuleName(m))
396 }
397 })
Nan Zhang581fd212018-01-10 16:06:12 -0800398
399 var htmlDirArgs string
400 if len(d.properties.Html_dirs) > 0 {
Dan Willemsencc090972018-02-26 14:33:31 -0800401 htmlDir := android.PathForModuleSrc(ctx, d.properties.Html_dirs[0])
402 implicits = append(implicits, ctx.Glob(htmlDir.Join(ctx, "**/*").String(), nil)...)
403 htmlDirArgs = "-htmldir " + htmlDir.String()
Nan Zhang581fd212018-01-10 16:06:12 -0800404 }
405
406 var htmlDir2Args string
407 if len(d.properties.Html_dirs) > 1 {
Dan Willemsencc090972018-02-26 14:33:31 -0800408 htmlDir2 := android.PathForModuleSrc(ctx, d.properties.Html_dirs[1])
409 implicits = append(implicits, ctx.Glob(htmlDir2.Join(ctx, "**/*").String(), nil)...)
410 htmlDir2Args = "-htmldir2 " + htmlDir2.String()
411 }
412
413 if len(d.properties.Html_dirs) > 2 {
414 ctx.PropertyErrorf("html_dirs", "Droiddoc only supports up to 2 html dirs")
Nan Zhang581fd212018-01-10 16:06:12 -0800415 }
416
417 knownTags := ctx.ExpandSources(d.properties.Knowntags, nil)
418 implicits = append(implicits, knownTags...)
419
420 for _, kt := range knownTags {
421 args = args + " -knowntags " + kt.String()
422 }
423 for _, hdf := range d.properties.Hdf {
424 args = args + " -hdf " + hdf
425 }
426
427 if String(d.properties.Proofread_file) != "" {
428 proofreadFile := android.PathForModuleOut(ctx, String(d.properties.Proofread_file))
429 args = args + " -proofread " + proofreadFile.String()
430 }
431 if String(d.properties.Todo_file) != "" {
432 // tricky part:
433 // we should not compute full path for todo_file through PathForModuleOut().
434 // the non-standard doclet will get the full path relative to "-o".
435 args = args + " -todo " + String(d.properties.Todo_file)
436 }
437
438 implicits = append(implicits, d.Javadoc.srcJars...)
439
440 opts := "-source 1.8 -J-Xmx1600m -J-XX:-OmitStackTraceInFastThrow -XDignore.symbol.file " +
441 "-doclet com.google.doclava.Doclava -docletpath ${config.JsilverJar}:${config.DoclavaJar} " +
Colin Cross480cd762018-02-22 14:39:17 -0800442 "-templatedir " + templateDir + " " + htmlDirArgs + " " + htmlDir2Args + " " +
Nan Zhang581fd212018-01-10 16:06:12 -0800443 "-hdf page.build " + ctx.Config().BuildId() + "-" + ctx.Config().BuildNumberFromFile() + " " +
444 "-hdf page.now " + `"$$(date -d @$$(cat ` + ctx.Config().Getenv("BUILD_DATETIME_FILE") + `) "+%d %b %Y %k:%M")"` + " " +
445 args + " -stubs " + android.PathForModuleOut(ctx, "docs", "stubsDir").String()
446
447 var implicitOutputs android.WritablePaths
448 implicitOutputs = append(implicitOutputs, d.Javadoc.docZip)
449 for _, o := range d.properties.Out {
450 implicitOutputs = append(implicitOutputs, android.PathForModuleGen(ctx, o))
451 }
452
453 ctx.Build(pctx, android.BuildParams{
454 Rule: javadoc,
455 Description: "Droiddoc",
Nan Zhangccff0f72018-03-08 17:26:16 -0800456 Output: d.Javadoc.stubsSrcJar,
Nan Zhang581fd212018-01-10 16:06:12 -0800457 Inputs: d.Javadoc.srcFiles,
458 Implicits: implicits,
459 ImplicitOutputs: implicitOutputs,
460 Args: map[string]string{
461 "outDir": android.PathForModuleOut(ctx, "docs", "out").String(),
462 "srcJarDir": android.PathForModuleOut(ctx, "docs", "srcjars").String(),
463 "stubsDir": android.PathForModuleOut(ctx, "docs", "stubsDir").String(),
464 "srcJars": strings.Join(d.Javadoc.srcJars.Strings(), " "),
465 "opts": opts,
466 "bootclasspathArgs": bootClasspathArgs,
467 "classpathArgs": classpathArgs,
468 "sourcepath": strings.Join(d.Javadoc.sourcepaths.Strings(), ":"),
469 "docZip": d.Javadoc.docZip.String(),
470 "JsilverJar": "${config.JsilverJar}",
471 "DoclavaJar": "${config.DoclavaJar}",
472 },
473 })
474}
Dan Willemsencc090972018-02-26 14:33:31 -0800475
476var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
477
478type DroiddocTemplateProperties struct {
479 // path to the directory containing the droiddoc templates.
480 Path *string
481}
482
483type DroiddocTemplate struct {
484 android.ModuleBase
485
486 properties DroiddocTemplateProperties
487
488 deps android.Paths
489 dir android.Path
490}
491
492func DroiddocTemplateFactory() android.Module {
493 module := &DroiddocTemplate{}
494 module.AddProperties(&module.properties)
495 android.InitAndroidModule(module)
496 return module
497}
498
499func (d *DroiddocTemplate) DepsMutator(android.BottomUpMutatorContext) {}
500
501func (d *DroiddocTemplate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
502 path := android.PathForModuleSrc(ctx, String(d.properties.Path))
503 d.dir = path
504 d.deps = ctx.Glob(path.Join(ctx, "**/*").String(), nil)
505}