blob: efe6256654072d8d5f252bb6bc55aaf3c6282967 [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 Cross59149b62017-10-16 18:07:29 -070043 `$javacFlags $sourcepath $bootClasspath $classpath ` +
Colin Cross64162712017-08-08 13:17:59 -070044 `-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 Cross59149b62017-10-16 18:07:29 -070051 "javacFlags", "sourcepath", "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 Cross59149b62017-10-16 18:07:29 -070057 `$javacFlags $sourcepath $bootClasspath $classpath ` +
Colin Crossc6bbef32017-08-14 14:16:06 -070058 `-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 },
Colin Cross59149b62017-10-16 18:07:29 -070070 "javacFlags", "sourcepath", "bootClasspath", "classpath", "outDir", "annoDir", "javaVersion")
Colin Crossc6bbef32017-08-14 14:16:06 -070071
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 Crosse9a275b2017-10-16 17:09:48 -0700138func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross59149b62017-10-16 18:07:29 -0700139 srcFiles android.Paths, srcJars classpath,
Colin Crosse9a275b2017-10-16 17:09:48 -0700140 flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700141
Colin Cross59149b62017-10-16 18:07:29 -0700142 transformJavaToClasses(ctx, outputFile, srcFiles, srcJars, flags, deps,
Colin Crosse9a275b2017-10-16 17:09:48 -0700143 "", "javac", javac)
Colin Cross2fe66872015-03-30 17:20:39 -0700144}
145
Colin Crosse9a275b2017-10-16 17:09:48 -0700146func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross59149b62017-10-16 18:07:29 -0700147 srcFiles android.Paths, srcJars classpath,
Colin Crosse9a275b2017-10-16 17:09:48 -0700148 flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700149
Colin Crossfee57cb2017-09-05 13:16:45 -0700150 if config.ErrorProneJar == "" {
151 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
Colin Crossfee57cb2017-09-05 13:16:45 -0700152 }
153
Colin Cross59149b62017-10-16 18:07:29 -0700154 transformJavaToClasses(ctx, outputFile, srcFiles, srcJars, flags, nil,
Colin Crosse9a275b2017-10-16 17:09:48 -0700155 "-errorprone", "errorprone", errorprone)
Colin Cross070879e2017-10-11 11:21:07 -0700156}
157
158// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700159// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
160// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700161//
162// This method may be used for different compilers, including javac and Error Prone. The rule
163// argument specifies which command line to use and desc sets the description of the rule that will
164// be printed at build time. The stem argument provides the file name of the output jar, and
165// suffix will be appended to various intermediate files and directories to avoid collisions when
166// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700167func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross59149b62017-10-16 18:07:29 -0700168 srcFiles android.Paths, srcJars classpath,
Colin Crosse9a275b2017-10-16 17:09:48 -0700169 flags javaBuilderFlags, deps android.Paths,
170 intermediatesSuffix, desc string, rule blueprint.Rule) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700171
Colin Cross59149b62017-10-16 18:07:29 -0700172 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700173
174 var bootClasspath string
175 if flags.javaVersion == "1.9" {
176 deps = append(deps, flags.systemModules...)
177 bootClasspath = flags.systemModules.JavaSystemModules(ctx.Device())
178 } else {
179 deps = append(deps, flags.bootClasspath...)
180 bootClasspath = flags.bootClasspath.JavaBootClasspath(ctx.Device())
181 }
182
Colin Cross6ade34f2017-09-15 13:00:47 -0700183 deps = append(deps, flags.classpath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700184
185 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross070879e2017-10-11 11:21:07 -0700186 Rule: rule,
187 Description: desc,
188 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700189 Inputs: srcFiles,
190 Implicits: deps,
191 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700192 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700193 "bootClasspath": bootClasspath,
Colin Cross59149b62017-10-16 18:07:29 -0700194 "sourcepath": srcJars.JavaSourcepath(),
Colin Cross6ade34f2017-09-15 13:00:47 -0700195 "classpath": flags.classpath.JavaClasspath(),
Colin Crosse9a275b2017-10-16 17:09:48 -0700196 "outDir": android.PathForModuleOut(ctx, "classes"+intermediatesSuffix).String(),
197 "annoDir": android.PathForModuleOut(ctx, "anno"+intermediatesSuffix).String(),
Colin Crossc6bbef32017-08-14 14:16:06 -0700198 "javaVersion": flags.javaVersion,
199 },
200 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700201}
202
Colin Crosse9a275b2017-10-16 17:09:48 -0700203func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
204 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700205
Colin Cross635c3b02016-05-18 15:37:25 -0700206 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700207 Rule: jar,
208 Description: "jar",
209 Output: outputFile,
210 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700211 Args: map[string]string{
Colin Cross0a6e0072017-08-30 14:24:55 -0700212 "jarArgs": strings.Join(jarArgs, " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700213 },
214 })
Colin Cross2fe66872015-03-30 17:20:39 -0700215}
216
Colin Crosse9a275b2017-10-16 17:09:48 -0700217func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath,
218 jars android.Paths, manifest android.OptionalPath, stripDirs bool) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700219
Colin Cross635acc92017-09-12 22:50:46 -0700220 var deps android.Paths
221
222 var jarArgs []string
223 if manifest.Valid() {
224 jarArgs = append(jarArgs, "-m "+manifest.String())
225 deps = append(deps, manifest.Path())
226 }
227
228 if stripDirs {
229 jarArgs = append(jarArgs, "-D")
230 }
231
Colin Cross0a6e0072017-08-30 14:24:55 -0700232 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
233 Rule: combineJar,
234 Description: "combine jars",
235 Output: outputFile,
236 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700237 Implicits: deps,
238 Args: map[string]string{
239 "jarArgs": strings.Join(jarArgs, " "),
240 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700241 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700242}
243
Colin Crosse9a275b2017-10-16 17:09:48 -0700244func TransformDesugar(ctx android.ModuleContext, outputFile android.WritablePath,
245 classesJar android.Path, flags javaBuilderFlags) {
Colin Cross6ade34f2017-09-15 13:00:47 -0700246
Colin Cross6ade34f2017-09-15 13:00:47 -0700247 dumpDir := android.PathForModuleOut(ctx, "desugar_dumped_classes")
248
249 javaFlags := ""
Colin Cross1369cdb2017-09-29 17:58:17 -0700250 if ctx.AConfig().UseOpenJDK9() {
Colin Cross6ade34f2017-09-15 13:00:47 -0700251 javaFlags = "--add-opens java.base/java.lang.invoke=ALL-UNNAMED"
252 }
253
254 var desugarFlags []string
255 desugarFlags = append(desugarFlags, flags.bootClasspath.DesugarBootClasspath()...)
256 desugarFlags = append(desugarFlags, flags.classpath.DesugarClasspath()...)
257
258 var deps android.Paths
259 deps = append(deps, flags.bootClasspath...)
260 deps = append(deps, flags.classpath...)
261
262 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
263 Rule: desugar,
264 Description: "desugar",
265 Output: outputFile,
266 Input: classesJar,
267 Implicits: deps,
268 Args: map[string]string{
269 "dumpDir": dumpDir.String(),
270 "javaFlags": javaFlags,
271 "classpathFlags": strings.Join(desugarFlags, " "),
272 "desugarFlags": flags.desugarFlags,
273 },
274 })
Colin Cross6ade34f2017-09-15 13:00:47 -0700275}
276
Colin Cross7db5d632017-10-04 17:07:09 -0700277// Converts a classes.jar file to classes*.dex, then combines the dex files with any resources
278// in the classes.jar file into a dex jar.
Colin Crosse9a275b2017-10-16 17:09:48 -0700279func TransformClassesJarToDexJar(ctx android.ModuleContext, outputFile android.WritablePath,
280 classesJar android.Path, flags javaBuilderFlags) {
Colin Cross2fe66872015-03-30 17:20:39 -0700281
Colin Cross635c3b02016-05-18 15:37:25 -0700282 outDir := android.PathForModuleOut(ctx, "dex")
Colin Cross2fe66872015-03-30 17:20:39 -0700283
Colin Cross635c3b02016-05-18 15:37:25 -0700284 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700285 Rule: dx,
286 Description: "dx",
287 Output: outputFile,
288 Input: classesJar,
Colin Cross2fe66872015-03-30 17:20:39 -0700289 Args: map[string]string{
290 "dxFlags": flags.dxFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700291 "outDir": outDir.String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700292 },
293 })
Colin Cross2fe66872015-03-30 17:20:39 -0700294}
Colin Crosse1d62a82015-04-03 16:53:05 -0700295
Colin Crosse9a275b2017-10-16 17:09:48 -0700296func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
297 classesJar android.Path, rulesFile android.Path) {
Colin Cross635c3b02016-05-18 15:37:25 -0700298 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700299 Rule: jarjar,
300 Description: "jarjar",
301 Output: outputFile,
302 Input: classesJar,
303 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700304 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700305 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700306 },
307 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700308}
Colin Cross6ade34f2017-09-15 13:00:47 -0700309
310type classpath []android.Path
311
Colin Cross59149b62017-10-16 18:07:29 -0700312// Returns a -sourcepath argument in the form javac expects. If the list is empty returns
313// -sourcepath "" to ensure javac does not fall back to searching the classpath for sources.
314func (x *classpath) JavaSourcepath() string {
315 if len(*x) > 0 {
316 return "-sourcepath " + strings.Join(x.Strings(), ":")
317 } else {
318 return `-sourcepath ""`
319 }
320}
321
Colin Cross6ade34f2017-09-15 13:00:47 -0700322// Returns a -classpath argument in the form java or javac expects
323func (x *classpath) JavaClasspath() string {
324 if len(*x) > 0 {
325 return "-classpath " + strings.Join(x.Strings(), ":")
326 } else {
327 return ""
328 }
329}
330
331// Returns a -processorpath argument in the form java or javac expects
332func (x *classpath) JavaProcessorpath() string {
333 if len(*x) > 0 {
334 return "-processorpath " + strings.Join(x.Strings(), ":")
335 } else {
336 return ""
337 }
338}
339
340// Returns a -bootclasspath argument in the form java or javac expects. If forceEmpty is true,
341// returns -bootclasspath "" if the bootclasspath is empty to ensure javac does not fall back to the
342// default bootclasspath.
343func (x *classpath) JavaBootClasspath(forceEmpty bool) string {
344 if len(*x) > 0 {
345 return "-bootclasspath " + strings.Join(x.Strings(), ":")
346 } else if forceEmpty {
347 return `-bootclasspath ""`
348 } else {
349 return ""
350 }
351}
352
Colin Cross1369cdb2017-09-29 17:58:17 -0700353// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
354// returns --system=none if the list is empty to ensure javac does not fall back to the default
355// system modules.
356func (x *classpath) JavaSystemModules(forceEmpty bool) string {
357 if len(*x) > 1 {
358 panic("more than one system module")
359 } else if len(*x) == 1 {
360 return "--system=" + strings.TrimSuffix((*x)[0].String(), "lib/modules")
361 } else if forceEmpty {
362 return "--system=none"
363 } else {
364 return ""
365 }
366}
367
Colin Cross6ade34f2017-09-15 13:00:47 -0700368func (x *classpath) DesugarBootClasspath() []string {
369 if x == nil || *x == nil {
370 return nil
371 }
372 flags := make([]string, len(*x))
373 for i, v := range *x {
374 flags[i] = "--bootclasspath_entry " + v.String()
375 }
376
377 return flags
378}
379
380func (x *classpath) DesugarClasspath() []string {
381 if x == nil || *x == nil {
382 return nil
383 }
384 flags := make([]string, len(*x))
385 for i, v := range *x {
386 flags[i] = "--classpath_entry " + v.String()
387 }
388
389 return flags
390}
391
392// Append an android.Paths to the end of the classpath list
393func (x *classpath) AddPaths(paths android.Paths) {
394 for _, path := range paths {
395 *x = append(*x, path)
396 }
397}
398
399// Convert a classpath to an android.Paths
400func (x *classpath) Paths() android.Paths {
401 return append(android.Paths(nil), (*x)...)
402}
403
404func (x *classpath) Strings() []string {
405 if x == nil {
406 return nil
407 }
408 ret := make([]string, len(*x))
409 for i, path := range *x {
410 ret[i] = path.String()
411 }
412 return ret
413}