blob: 8992f681a99ff267048150df68f6f2a4eaa2f4a8 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 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
17// This file generates the final rules for compiling all Java. All properties related to
18// compiling should have been translated into javaBuilderFlags or another argument to the Transform*
19// functions.
20
21import (
Colin Cross2fe66872015-03-30 17:20:39 -070022 "strings"
23
Colin Cross2fe66872015-03-30 17:20:39 -070024 "github.com/google/blueprint"
Colin Crossfee57cb2017-09-05 13:16:45 -070025
26 "android/soong/android"
27 "android/soong/java/config"
Colin Cross2fe66872015-03-30 17:20:39 -070028)
29
30var (
Colin Cross635c3b02016-05-18 15:37:25 -070031 pctx = android.NewPackageContext("android/soong/java")
Colin Cross2fe66872015-03-30 17:20:39 -070032
33 // Compiling java is not conducive to proper dependency tracking. The path-matches-class-name
34 // requirement leads to unpredictable generated source file names, and a single .java file
35 // will get compiled into multiple .class files if it contains inner classes. To work around
36 // this, all java rules write into separate directories and then a post-processing step lists
37 // the files in the the directory into a list file that later rules depend on (and sometimes
38 // read from directly using @<listfile>)
Yoshisato Yanagisawa572324a2017-06-05 17:41:50 +090039 javac = pctx.AndroidGomaStaticRule("javac",
Colin Cross2fe66872015-03-30 17:20:39 -070040 blueprint.RuleParams{
Colin Cross64162712017-08-08 13:17:59 -070041 Command: `rm -rf "$outDir" "$annoDir" && mkdir -p "$outDir" "$annoDir" && ` +
Colin Cross3203dde2017-08-28 17:23:21 -070042 `${config.JavacWrapper}${config.JavacCmd} ${config.JavacHeapFlags} ${config.CommonJdkFlags} ` +
Colin Cross64162712017-08-08 13:17:59 -070043 `$javacFlags $bootClasspath $classpath ` +
44 `-source $javaVersion -target $javaVersion ` +
Colin Crossb852a582017-08-10 17:58:12 -070045 `-d $outDir -s $annoDir @$out.rsp && ` +
Colin Cross0a6e0072017-08-30 14:24:55 -070046 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
47 CommandDeps: []string{"${config.JavacCmd}", "${config.SoongZipCmd}"},
Colin Cross2fe66872015-03-30 17:20:39 -070048 Rspfile: "$out.rsp",
49 RspfileContent: "$in",
Colin Cross2fe66872015-03-30 17:20:39 -070050 },
Colin Cross64162712017-08-08 13:17:59 -070051 "javacFlags", "bootClasspath", "classpath", "outDir", "annoDir", "javaVersion")
Colin Cross2fe66872015-03-30 17:20:39 -070052
Colin Crossc6bbef32017-08-14 14:16:06 -070053 errorprone = pctx.AndroidStaticRule("errorprone",
54 blueprint.RuleParams{
55 Command: `rm -rf "$outDir" "$annoDir" && mkdir -p "$outDir" "$annoDir" && ` +
Colin Cross3a5de652017-09-11 23:10:21 -070056 `${config.ErrorProneCmd} ` +
Colin Crossc6bbef32017-08-14 14:16:06 -070057 `$javacFlags $bootClasspath $classpath ` +
58 `-source $javaVersion -target $javaVersion ` +
59 `-d $outDir -s $annoDir @$out.rsp && ` +
Colin Cross0a6e0072017-08-30 14:24:55 -070060 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
Colin Crossc6bbef32017-08-14 14:16:06 -070061 CommandDeps: []string{
62 "${config.JavaCmd}",
63 "${config.ErrorProneJavacJar}",
64 "${config.ErrorProneJar}",
Colin Cross0a6e0072017-08-30 14:24:55 -070065 "${config.SoongZipCmd}",
Colin Crossc6bbef32017-08-14 14:16:06 -070066 },
67 Rspfile: "$out.rsp",
68 RspfileContent: "$in",
69 },
70 "javacFlags", "bootClasspath", "classpath", "outDir", "annoDir", "javaVersion")
71
Colin Cross9d45bb72016-08-29 16:14:13 -070072 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -070073 blueprint.RuleParams{
Colin Cross0a6e0072017-08-30 14:24:55 -070074 Command: `${config.SoongZipCmd} -jar -o $out $jarArgs`,
75 CommandDeps: []string{"${config.SoongZipCmd}"},
Colin Cross2fe66872015-03-30 17:20:39 -070076 },
Colin Cross0a6e0072017-08-30 14:24:55 -070077 "jarArgs")
78
79 combineJar = pctx.AndroidStaticRule("combineJar",
80 blueprint.RuleParams{
Colin Cross635acc92017-09-12 22:50:46 -070081 Command: `${config.MergeZipsCmd} -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -070082 CommandDeps: []string{"${config.MergeZipsCmd}"},
83 },
Colin Cross635acc92017-09-12 22:50:46 -070084 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -070085
Colin Cross6ade34f2017-09-15 13:00:47 -070086 desugar = pctx.AndroidStaticRule("desugar",
87 blueprint.RuleParams{
88 Command: `rm -rf $dumpDir && mkdir -p $dumpDir && ` +
89 `${config.JavaCmd} ` +
90 `-Djdk.internal.lambda.dumpProxyClasses=$$(cd $dumpDir && pwd) ` +
91 `$javaFlags ` +
92 `-jar ${config.DesugarJar} $classpathFlags $desugarFlags ` +
93 `-i $in -o $out`,
94 CommandDeps: []string{"${config.DesugarJar}"},
95 },
96 "javaFlags", "classpathFlags", "desugarFlags", "dumpDir")
97
Colin Cross9d45bb72016-08-29 16:14:13 -070098 dx = pctx.AndroidStaticRule("dx",
Colin Cross2fe66872015-03-30 17:20:39 -070099 blueprint.RuleParams{
Colin Cross6d1e72d2015-04-10 17:44:24 -0700100 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Colin Cross6ade34f2017-09-15 13:00:47 -0700101 `${config.DxCmd} --dex --output=$outDir $dxFlags $in && ` +
Colin Cross7db5d632017-10-04 17:07:09 -0700102 `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` +
103 `${config.MergeZipsCmd} -D -stripFile "*.class" $out $outDir/classes.dex.jar $in`,
Colin Cross6ade34f2017-09-15 13:00:47 -0700104 CommandDeps: []string{
105 "${config.DxCmd}",
106 "${config.SoongZipCmd}",
Colin Cross7db5d632017-10-04 17:07:09 -0700107 "${config.MergeZipsCmd}",
Colin Cross6ade34f2017-09-15 13:00:47 -0700108 },
Colin Cross2fe66872015-03-30 17:20:39 -0700109 },
110 "outDir", "dxFlags")
Colin Crosse1d62a82015-04-03 16:53:05 -0700111
Colin Cross9d45bb72016-08-29 16:14:13 -0700112 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700113 blueprint.RuleParams{
Colin Cross64162712017-08-08 13:17:59 -0700114 Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out",
115 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700116 },
117 "rulesFile")
Colin Cross2fe66872015-03-30 17:20:39 -0700118)
119
120func init() {
Colin Cross64162712017-08-08 13:17:59 -0700121 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700122}
123
124type javaBuilderFlags struct {
125 javacFlags string
126 dxFlags string
Colin Cross6ade34f2017-09-15 13:00:47 -0700127 bootClasspath classpath
128 classpath classpath
Colin Cross1369cdb2017-09-29 17:58:17 -0700129 systemModules classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700130 desugarFlags string
Colin Crossc0b06f12015-04-08 13:03:43 -0700131 aidlFlags string
Colin Cross64162712017-08-08 13:17:59 -0700132 javaVersion string
Colin Cross6af17aa2017-09-20 12:59:05 -0700133
134 protoFlags string
135 protoOutFlag string
Colin Cross2fe66872015-03-30 17:20:39 -0700136}
137
Colin Cross0a6e0072017-08-30 14:24:55 -0700138func TransformJavaToClasses(ctx android.ModuleContext, srcFiles, srcFileLists android.Paths,
139 flags javaBuilderFlags, deps android.Paths) android.ModuleOutPath {
Colin Cross2fe66872015-03-30 17:20:39 -0700140
Colin Cross070879e2017-10-11 11:21:07 -0700141 return transformJavaToClasses(ctx, srcFiles, srcFileLists, flags, deps,
142 "classes-compiled.jar", "", "javac", javac)
Colin Cross2fe66872015-03-30 17:20:39 -0700143}
144
Colin Cross6af17aa2017-09-20 12:59:05 -0700145func RunErrorProne(ctx android.ModuleContext, srcFiles, srcFileLists android.Paths,
146 flags javaBuilderFlags) android.Path {
Colin Crossc6bbef32017-08-14 14:16:06 -0700147
Colin Crossfee57cb2017-09-05 13:16:45 -0700148 if config.ErrorProneJar == "" {
149 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
150 return nil
151 }
152
Colin Cross070879e2017-10-11 11:21:07 -0700153 return transformJavaToClasses(ctx, srcFiles, srcFileLists, flags, nil,
154 "classes-errorprone.list", "-errorprone", "errorprone", errorprone)
155}
156
157// transformJavaToClasses takes source files and converts them to a jar containing .class files.
158// srcFiles is a list of paths to sources, srcFileLists is a list of paths to files that contain
159// paths to sources. There is no dependency on the sources passed through srcFileLists, those
160// must be added through the deps argument, which contains a list of paths that should be added
161// as implicit dependencies. flags contains various command line flags to be passed to the
162// compiler.
163//
164// This method may be used for different compilers, including javac and Error Prone. The rule
165// argument specifies which command line to use and desc sets the description of the rule that will
166// be printed at build time. The stem argument provides the file name of the output jar, and
167// suffix will be appended to various intermediate files and directories to avoid collisions when
168// this function is called twice in the same module directory.
169func transformJavaToClasses(ctx android.ModuleContext, srcFiles, srcFileLists android.Paths,
170 flags javaBuilderFlags, deps android.Paths, stem, suffix, desc string,
171 rule blueprint.Rule) android.ModuleOutPath {
172
173 outputFile := android.PathForModuleOut(ctx, stem)
Colin Crossc6bbef32017-08-14 14:16:06 -0700174
Colin Cross6af17aa2017-09-20 12:59:05 -0700175 javacFlags := flags.javacFlags
176 if len(srcFileLists) > 0 {
177 javacFlags += " " + android.JoinWithPrefix(srcFileLists.Strings(), "@")
178 }
179
Colin Crossc6bbef32017-08-14 14:16:06 -0700180 deps = append(deps, srcFileLists...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700181
182 var bootClasspath string
183 if flags.javaVersion == "1.9" {
184 deps = append(deps, flags.systemModules...)
185 bootClasspath = flags.systemModules.JavaSystemModules(ctx.Device())
186 } else {
187 deps = append(deps, flags.bootClasspath...)
188 bootClasspath = flags.bootClasspath.JavaBootClasspath(ctx.Device())
189 }
190
Colin Cross6ade34f2017-09-15 13:00:47 -0700191 deps = append(deps, flags.classpath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700192
193 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross070879e2017-10-11 11:21:07 -0700194 Rule: rule,
195 Description: desc,
196 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700197 Inputs: srcFiles,
198 Implicits: deps,
199 Args: map[string]string{
200 "javacFlags": javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700201 "bootClasspath": bootClasspath,
Colin Cross6ade34f2017-09-15 13:00:47 -0700202 "classpath": flags.classpath.JavaClasspath(),
Colin Cross070879e2017-10-11 11:21:07 -0700203 "outDir": android.PathForModuleOut(ctx, "classes"+suffix).String(),
204 "annoDir": android.PathForModuleOut(ctx, "anno"+suffix).String(),
Colin Crossc6bbef32017-08-14 14:16:06 -0700205 "javaVersion": flags.javaVersion,
206 },
207 })
208
Colin Cross070879e2017-10-11 11:21:07 -0700209 return outputFile
Colin Crossc6bbef32017-08-14 14:16:06 -0700210}
211
Colin Cross40a36712017-09-27 17:41:35 -0700212func TransformResourcesToJar(ctx android.ModuleContext, jarArgs []string,
Colin Cross635acc92017-09-12 22:50:46 -0700213 deps android.Paths) android.Path {
Colin Cross2fe66872015-03-30 17:20:39 -0700214
Colin Cross0a6e0072017-08-30 14:24:55 -0700215 outputFile := android.PathForModuleOut(ctx, "res.jar")
Colin Cross2fe66872015-03-30 17:20:39 -0700216
Colin Cross635c3b02016-05-18 15:37:25 -0700217 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700218 Rule: jar,
219 Description: "jar",
220 Output: outputFile,
221 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700222 Args: map[string]string{
Colin Cross0a6e0072017-08-30 14:24:55 -0700223 "jarArgs": strings.Join(jarArgs, " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700224 },
225 })
226
227 return outputFile
228}
229
Colin Cross635acc92017-09-12 22:50:46 -0700230func TransformJarsToJar(ctx android.ModuleContext, stem string, jars android.Paths,
231 manifest android.OptionalPath, stripDirs bool) android.Path {
Colin Cross0a6e0072017-08-30 14:24:55 -0700232
233 outputFile := android.PathForModuleOut(ctx, stem)
234
Colin Cross635acc92017-09-12 22:50:46 -0700235 if len(jars) == 1 && !manifest.Valid() {
Colin Cross0a6e0072017-08-30 14:24:55 -0700236 return jars[0]
237 }
238
Colin Cross635acc92017-09-12 22:50:46 -0700239 var deps android.Paths
240
241 var jarArgs []string
242 if manifest.Valid() {
243 jarArgs = append(jarArgs, "-m "+manifest.String())
244 deps = append(deps, manifest.Path())
245 }
246
247 if stripDirs {
248 jarArgs = append(jarArgs, "-D")
249 }
250
Colin Cross0a6e0072017-08-30 14:24:55 -0700251 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
252 Rule: combineJar,
253 Description: "combine jars",
254 Output: outputFile,
255 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700256 Implicits: deps,
257 Args: map[string]string{
258 "jarArgs": strings.Join(jarArgs, " "),
259 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700260 })
261
262 return outputFile
263}
264
Colin Cross6ade34f2017-09-15 13:00:47 -0700265func TransformDesugar(ctx android.ModuleContext, classesJar android.Path,
266 flags javaBuilderFlags) android.Path {
267
268 outputFile := android.PathForModuleOut(ctx, "classes-desugar.jar")
269 dumpDir := android.PathForModuleOut(ctx, "desugar_dumped_classes")
270
271 javaFlags := ""
Colin Cross1369cdb2017-09-29 17:58:17 -0700272 if ctx.AConfig().UseOpenJDK9() {
Colin Cross6ade34f2017-09-15 13:00:47 -0700273 javaFlags = "--add-opens java.base/java.lang.invoke=ALL-UNNAMED"
274 }
275
276 var desugarFlags []string
277 desugarFlags = append(desugarFlags, flags.bootClasspath.DesugarBootClasspath()...)
278 desugarFlags = append(desugarFlags, flags.classpath.DesugarClasspath()...)
279
280 var deps android.Paths
281 deps = append(deps, flags.bootClasspath...)
282 deps = append(deps, flags.classpath...)
283
284 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
285 Rule: desugar,
286 Description: "desugar",
287 Output: outputFile,
288 Input: classesJar,
289 Implicits: deps,
290 Args: map[string]string{
291 "dumpDir": dumpDir.String(),
292 "javaFlags": javaFlags,
293 "classpathFlags": strings.Join(desugarFlags, " "),
294 "desugarFlags": flags.desugarFlags,
295 },
296 })
297
298 return outputFile
299}
300
Colin Cross7db5d632017-10-04 17:07:09 -0700301// Converts a classes.jar file to classes*.dex, then combines the dex files with any resources
302// in the classes.jar file into a dex jar.
303func TransformClassesJarToDexJar(ctx android.ModuleContext, stem string, classesJar android.Path,
Colin Cross6ade34f2017-09-15 13:00:47 -0700304 flags javaBuilderFlags) android.Path {
Colin Cross2fe66872015-03-30 17:20:39 -0700305
Colin Cross635c3b02016-05-18 15:37:25 -0700306 outDir := android.PathForModuleOut(ctx, "dex")
Colin Cross7db5d632017-10-04 17:07:09 -0700307 outputFile := android.PathForModuleOut(ctx, stem)
Colin Cross2fe66872015-03-30 17:20:39 -0700308
Colin Cross635c3b02016-05-18 15:37:25 -0700309 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700310 Rule: dx,
311 Description: "dx",
312 Output: outputFile,
313 Input: classesJar,
Colin Cross2fe66872015-03-30 17:20:39 -0700314 Args: map[string]string{
315 "dxFlags": flags.dxFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700316 "outDir": outDir.String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700317 },
318 })
319
Colin Cross2fe66872015-03-30 17:20:39 -0700320 return outputFile
321}
Colin Crosse1d62a82015-04-03 16:53:05 -0700322
Colin Cross0a6e0072017-08-30 14:24:55 -0700323func TransformJarJar(ctx android.ModuleContext, classesJar android.Path, rulesFile android.Path) android.ModuleOutPath {
Colin Cross635c3b02016-05-18 15:37:25 -0700324 outputFile := android.PathForModuleOut(ctx, "classes-jarjar.jar")
325 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700326 Rule: jarjar,
327 Description: "jarjar",
328 Output: outputFile,
329 Input: classesJar,
330 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700331 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700332 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700333 },
334 })
335
336 return outputFile
337}
Colin Cross6ade34f2017-09-15 13:00:47 -0700338
339type classpath []android.Path
340
341// Returns a -classpath argument in the form java or javac expects
342func (x *classpath) JavaClasspath() string {
343 if len(*x) > 0 {
344 return "-classpath " + strings.Join(x.Strings(), ":")
345 } else {
346 return ""
347 }
348}
349
350// Returns a -processorpath argument in the form java or javac expects
351func (x *classpath) JavaProcessorpath() string {
352 if len(*x) > 0 {
353 return "-processorpath " + strings.Join(x.Strings(), ":")
354 } else {
355 return ""
356 }
357}
358
359// Returns a -bootclasspath argument in the form java or javac expects. If forceEmpty is true,
360// returns -bootclasspath "" if the bootclasspath is empty to ensure javac does not fall back to the
361// default bootclasspath.
362func (x *classpath) JavaBootClasspath(forceEmpty bool) string {
363 if len(*x) > 0 {
364 return "-bootclasspath " + strings.Join(x.Strings(), ":")
365 } else if forceEmpty {
366 return `-bootclasspath ""`
367 } else {
368 return ""
369 }
370}
371
Colin Cross1369cdb2017-09-29 17:58:17 -0700372// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
373// returns --system=none if the list is empty to ensure javac does not fall back to the default
374// system modules.
375func (x *classpath) JavaSystemModules(forceEmpty bool) string {
376 if len(*x) > 1 {
377 panic("more than one system module")
378 } else if len(*x) == 1 {
379 return "--system=" + strings.TrimSuffix((*x)[0].String(), "lib/modules")
380 } else if forceEmpty {
381 return "--system=none"
382 } else {
383 return ""
384 }
385}
386
Colin Cross6ade34f2017-09-15 13:00:47 -0700387func (x *classpath) DesugarBootClasspath() []string {
388 if x == nil || *x == nil {
389 return nil
390 }
391 flags := make([]string, len(*x))
392 for i, v := range *x {
393 flags[i] = "--bootclasspath_entry " + v.String()
394 }
395
396 return flags
397}
398
399func (x *classpath) DesugarClasspath() []string {
400 if x == nil || *x == nil {
401 return nil
402 }
403 flags := make([]string, len(*x))
404 for i, v := range *x {
405 flags[i] = "--classpath_entry " + v.String()
406 }
407
408 return flags
409}
410
411// Append an android.Paths to the end of the classpath list
412func (x *classpath) AddPaths(paths android.Paths) {
413 for _, path := range paths {
414 *x = append(*x, path)
415 }
416}
417
418// Convert a classpath to an android.Paths
419func (x *classpath) Paths() android.Paths {
420 return append(android.Paths(nil), (*x)...)
421}
422
423func (x *classpath) Strings() []string {
424 if x == nil {
425 return nil
426 }
427 ret := make([]string, len(*x))
428 for i, path := range *x {
429 ret[i] = path.String()
430 }
431 return ret
432}