blob: 118f23979bf785d38a4226e980d7668c8c52a260 [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
129 desugarFlags string
Colin Crossc0b06f12015-04-08 13:03:43 -0700130 aidlFlags string
Colin Cross64162712017-08-08 13:17:59 -0700131 javaVersion string
Colin Cross6af17aa2017-09-20 12:59:05 -0700132
133 protoFlags string
134 protoOutFlag string
Colin Cross2fe66872015-03-30 17:20:39 -0700135}
136
Colin Cross0a6e0072017-08-30 14:24:55 -0700137func TransformJavaToClasses(ctx android.ModuleContext, srcFiles, srcFileLists android.Paths,
138 flags javaBuilderFlags, deps android.Paths) android.ModuleOutPath {
Colin Cross2fe66872015-03-30 17:20:39 -0700139
Colin Cross070879e2017-10-11 11:21:07 -0700140 return transformJavaToClasses(ctx, srcFiles, srcFileLists, flags, deps,
141 "classes-compiled.jar", "", "javac", javac)
Colin Cross2fe66872015-03-30 17:20:39 -0700142}
143
Colin Cross6af17aa2017-09-20 12:59:05 -0700144func RunErrorProne(ctx android.ModuleContext, srcFiles, srcFileLists android.Paths,
145 flags javaBuilderFlags) android.Path {
Colin Crossc6bbef32017-08-14 14:16:06 -0700146
Colin Crossfee57cb2017-09-05 13:16:45 -0700147 if config.ErrorProneJar == "" {
148 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
149 return nil
150 }
151
Colin Cross070879e2017-10-11 11:21:07 -0700152 return transformJavaToClasses(ctx, srcFiles, srcFileLists, flags, nil,
153 "classes-errorprone.list", "-errorprone", "errorprone", errorprone)
154}
155
156// transformJavaToClasses takes source files and converts them to a jar containing .class files.
157// srcFiles is a list of paths to sources, srcFileLists is a list of paths to files that contain
158// paths to sources. There is no dependency on the sources passed through srcFileLists, those
159// must be added through the deps argument, which contains a list of paths that should be added
160// as implicit dependencies. flags contains various command line flags to be passed to the
161// compiler.
162//
163// This method may be used for different compilers, including javac and Error Prone. The rule
164// argument specifies which command line to use and desc sets the description of the rule that will
165// be printed at build time. The stem argument provides the file name of the output jar, and
166// suffix will be appended to various intermediate files and directories to avoid collisions when
167// this function is called twice in the same module directory.
168func transformJavaToClasses(ctx android.ModuleContext, srcFiles, srcFileLists android.Paths,
169 flags javaBuilderFlags, deps android.Paths, stem, suffix, desc string,
170 rule blueprint.Rule) android.ModuleOutPath {
171
172 outputFile := android.PathForModuleOut(ctx, stem)
Colin Crossc6bbef32017-08-14 14:16:06 -0700173
Colin Cross6af17aa2017-09-20 12:59:05 -0700174 javacFlags := flags.javacFlags
175 if len(srcFileLists) > 0 {
176 javacFlags += " " + android.JoinWithPrefix(srcFileLists.Strings(), "@")
177 }
178
Colin Crossc6bbef32017-08-14 14:16:06 -0700179 deps = append(deps, srcFileLists...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700180 deps = append(deps, flags.bootClasspath...)
181 deps = append(deps, flags.classpath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700182
183 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross070879e2017-10-11 11:21:07 -0700184 Rule: rule,
185 Description: desc,
186 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700187 Inputs: srcFiles,
188 Implicits: deps,
189 Args: map[string]string{
190 "javacFlags": javacFlags,
Colin Cross6ade34f2017-09-15 13:00:47 -0700191 "bootClasspath": flags.bootClasspath.JavaBootClasspath(ctx.Device()),
192 "classpath": flags.classpath.JavaClasspath(),
Colin Cross070879e2017-10-11 11:21:07 -0700193 "outDir": android.PathForModuleOut(ctx, "classes"+suffix).String(),
194 "annoDir": android.PathForModuleOut(ctx, "anno"+suffix).String(),
Colin Crossc6bbef32017-08-14 14:16:06 -0700195 "javaVersion": flags.javaVersion,
196 },
197 })
198
Colin Cross070879e2017-10-11 11:21:07 -0700199 return outputFile
Colin Crossc6bbef32017-08-14 14:16:06 -0700200}
201
Colin Cross40a36712017-09-27 17:41:35 -0700202func TransformResourcesToJar(ctx android.ModuleContext, jarArgs []string,
Colin Cross635acc92017-09-12 22:50:46 -0700203 deps android.Paths) android.Path {
Colin Cross2fe66872015-03-30 17:20:39 -0700204
Colin Cross0a6e0072017-08-30 14:24:55 -0700205 outputFile := android.PathForModuleOut(ctx, "res.jar")
Colin Cross2fe66872015-03-30 17:20:39 -0700206
Colin Cross635c3b02016-05-18 15:37:25 -0700207 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700208 Rule: jar,
209 Description: "jar",
210 Output: outputFile,
211 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700212 Args: map[string]string{
Colin Cross0a6e0072017-08-30 14:24:55 -0700213 "jarArgs": strings.Join(jarArgs, " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700214 },
215 })
216
217 return outputFile
218}
219
Colin Cross635acc92017-09-12 22:50:46 -0700220func TransformJarsToJar(ctx android.ModuleContext, stem string, jars android.Paths,
221 manifest android.OptionalPath, stripDirs bool) android.Path {
Colin Cross0a6e0072017-08-30 14:24:55 -0700222
223 outputFile := android.PathForModuleOut(ctx, stem)
224
Colin Cross635acc92017-09-12 22:50:46 -0700225 if len(jars) == 1 && !manifest.Valid() {
Colin Cross0a6e0072017-08-30 14:24:55 -0700226 return jars[0]
227 }
228
Colin Cross635acc92017-09-12 22:50:46 -0700229 var deps android.Paths
230
231 var jarArgs []string
232 if manifest.Valid() {
233 jarArgs = append(jarArgs, "-m "+manifest.String())
234 deps = append(deps, manifest.Path())
235 }
236
237 if stripDirs {
238 jarArgs = append(jarArgs, "-D")
239 }
240
Colin Cross0a6e0072017-08-30 14:24:55 -0700241 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
242 Rule: combineJar,
243 Description: "combine jars",
244 Output: outputFile,
245 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700246 Implicits: deps,
247 Args: map[string]string{
248 "jarArgs": strings.Join(jarArgs, " "),
249 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700250 })
251
252 return outputFile
253}
254
Colin Cross6ade34f2017-09-15 13:00:47 -0700255func TransformDesugar(ctx android.ModuleContext, classesJar android.Path,
256 flags javaBuilderFlags) android.Path {
257
258 outputFile := android.PathForModuleOut(ctx, "classes-desugar.jar")
259 dumpDir := android.PathForModuleOut(ctx, "desugar_dumped_classes")
260
261 javaFlags := ""
262 if ctx.AConfig().Getenv("EXPERIMENTAL_USE_OPENJDK9") != "" {
263 javaFlags = "--add-opens java.base/java.lang.invoke=ALL-UNNAMED"
264 }
265
266 var desugarFlags []string
267 desugarFlags = append(desugarFlags, flags.bootClasspath.DesugarBootClasspath()...)
268 desugarFlags = append(desugarFlags, flags.classpath.DesugarClasspath()...)
269
270 var deps android.Paths
271 deps = append(deps, flags.bootClasspath...)
272 deps = append(deps, flags.classpath...)
273
274 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
275 Rule: desugar,
276 Description: "desugar",
277 Output: outputFile,
278 Input: classesJar,
279 Implicits: deps,
280 Args: map[string]string{
281 "dumpDir": dumpDir.String(),
282 "javaFlags": javaFlags,
283 "classpathFlags": strings.Join(desugarFlags, " "),
284 "desugarFlags": flags.desugarFlags,
285 },
286 })
287
288 return outputFile
289}
290
Colin Cross7db5d632017-10-04 17:07:09 -0700291// Converts a classes.jar file to classes*.dex, then combines the dex files with any resources
292// in the classes.jar file into a dex jar.
293func TransformClassesJarToDexJar(ctx android.ModuleContext, stem string, classesJar android.Path,
Colin Cross6ade34f2017-09-15 13:00:47 -0700294 flags javaBuilderFlags) android.Path {
Colin Cross2fe66872015-03-30 17:20:39 -0700295
Colin Cross635c3b02016-05-18 15:37:25 -0700296 outDir := android.PathForModuleOut(ctx, "dex")
Colin Cross7db5d632017-10-04 17:07:09 -0700297 outputFile := android.PathForModuleOut(ctx, stem)
Colin Cross2fe66872015-03-30 17:20:39 -0700298
Colin Cross635c3b02016-05-18 15:37:25 -0700299 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700300 Rule: dx,
301 Description: "dx",
302 Output: outputFile,
303 Input: classesJar,
Colin Cross2fe66872015-03-30 17:20:39 -0700304 Args: map[string]string{
305 "dxFlags": flags.dxFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700306 "outDir": outDir.String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700307 },
308 })
309
Colin Cross2fe66872015-03-30 17:20:39 -0700310 return outputFile
311}
Colin Crosse1d62a82015-04-03 16:53:05 -0700312
Colin Cross0a6e0072017-08-30 14:24:55 -0700313func TransformJarJar(ctx android.ModuleContext, classesJar android.Path, rulesFile android.Path) android.ModuleOutPath {
Colin Cross635c3b02016-05-18 15:37:25 -0700314 outputFile := android.PathForModuleOut(ctx, "classes-jarjar.jar")
315 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700316 Rule: jarjar,
317 Description: "jarjar",
318 Output: outputFile,
319 Input: classesJar,
320 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700321 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700322 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700323 },
324 })
325
326 return outputFile
327}
Colin Cross6ade34f2017-09-15 13:00:47 -0700328
329type classpath []android.Path
330
331// Returns a -classpath argument in the form java or javac expects
332func (x *classpath) JavaClasspath() string {
333 if len(*x) > 0 {
334 return "-classpath " + strings.Join(x.Strings(), ":")
335 } else {
336 return ""
337 }
338}
339
340// Returns a -processorpath argument in the form java or javac expects
341func (x *classpath) JavaProcessorpath() string {
342 if len(*x) > 0 {
343 return "-processorpath " + strings.Join(x.Strings(), ":")
344 } else {
345 return ""
346 }
347}
348
349// Returns a -bootclasspath argument in the form java or javac expects. If forceEmpty is true,
350// returns -bootclasspath "" if the bootclasspath is empty to ensure javac does not fall back to the
351// default bootclasspath.
352func (x *classpath) JavaBootClasspath(forceEmpty bool) string {
353 if len(*x) > 0 {
354 return "-bootclasspath " + strings.Join(x.Strings(), ":")
355 } else if forceEmpty {
356 return `-bootclasspath ""`
357 } else {
358 return ""
359 }
360}
361
362func (x *classpath) DesugarBootClasspath() []string {
363 if x == nil || *x == nil {
364 return nil
365 }
366 flags := make([]string, len(*x))
367 for i, v := range *x {
368 flags[i] = "--bootclasspath_entry " + v.String()
369 }
370
371 return flags
372}
373
374func (x *classpath) DesugarClasspath() []string {
375 if x == nil || *x == nil {
376 return nil
377 }
378 flags := make([]string, len(*x))
379 for i, v := range *x {
380 flags[i] = "--classpath_entry " + v.String()
381 }
382
383 return flags
384}
385
386// Append an android.Paths to the end of the classpath list
387func (x *classpath) AddPaths(paths android.Paths) {
388 for _, path := range paths {
389 *x = append(*x, path)
390 }
391}
392
393// Convert a classpath to an android.Paths
394func (x *classpath) Paths() android.Paths {
395 return append(android.Paths(nil), (*x)...)
396}
397
398func (x *classpath) Strings() []string {
399 if x == nil {
400 return nil
401 }
402 ret := make([]string, len(*x))
403 for i, path := range *x {
404 ret[i] = path.String()
405 }
406 return ret
407}