blob: 48fba2303293d4056276a42bda13f8c410534635 [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 (
Nan Zhang61eaedb2017-11-02 13:28:15 -070022 "path/filepath"
23 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070024 "strings"
25
Colin Cross2fe66872015-03-30 17:20:39 -070026 "github.com/google/blueprint"
Colin Crossfee57cb2017-09-05 13:16:45 -070027
28 "android/soong/android"
29 "android/soong/java/config"
Colin Cross2fe66872015-03-30 17:20:39 -070030)
31
32var (
Colin Cross635c3b02016-05-18 15:37:25 -070033 pctx = android.NewPackageContext("android/soong/java")
Colin Cross2fe66872015-03-30 17:20:39 -070034
35 // Compiling java is not conducive to proper dependency tracking. The path-matches-class-name
36 // requirement leads to unpredictable generated source file names, and a single .java file
37 // will get compiled into multiple .class files if it contains inner classes. To work around
38 // this, all java rules write into separate directories and then a post-processing step lists
39 // the files in the the directory into a list file that later rules depend on (and sometimes
40 // read from directly using @<listfile>)
Yoshisato Yanagisawa572324a2017-06-05 17:41:50 +090041 javac = pctx.AndroidGomaStaticRule("javac",
Colin Cross2fe66872015-03-30 17:20:39 -070042 blueprint.RuleParams{
Colin Cross8eadbf02017-10-24 17:46:00 -070043 Command: `rm -rf "$outDir" "$annoDir" "$srcJarDir" && mkdir -p "$outDir" "$annoDir" "$srcJarDir" && ` +
44 `${config.ExtractSrcJarsCmd} $srcJarDir $srcJarDir/list $srcJars && ` +
Colin Crossa4820652017-10-17 13:56:52 -070045 `${config.SoongJavacWrapper} ${config.JavacWrapper}${config.JavacCmd} ${config.JavacHeapFlags} ${config.CommonJdkFlags} ` +
Colin Cross8eadbf02017-10-24 17:46:00 -070046 `$javacFlags $bootClasspath $classpath ` +
Colin Cross64162712017-08-08 13:17:59 -070047 `-source $javaVersion -target $javaVersion ` +
Colin Cross8eadbf02017-10-24 17:46:00 -070048 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list && ` +
Colin Cross0a6e0072017-08-30 14:24:55 -070049 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
Colin Cross8eadbf02017-10-24 17:46:00 -070050 CommandDeps: []string{
51 "${config.JavacCmd}",
52 "${config.SoongZipCmd}",
53 "${config.ExtractSrcJarsCmd}",
54 },
Colin Crossa4820652017-10-17 13:56:52 -070055 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
56 Rspfile: "$out.rsp",
57 RspfileContent: "$in",
Colin Cross2fe66872015-03-30 17:20:39 -070058 },
Colin Cross8eadbf02017-10-24 17:46:00 -070059 "javacFlags", "bootClasspath", "classpath", "srcJars", "srcJarDir",
60 "outDir", "annoDir", "javaVersion")
Colin Cross2fe66872015-03-30 17:20:39 -070061
Colin Cross93e85952017-08-15 13:34:18 -070062 kotlinc = pctx.AndroidGomaStaticRule("kotlinc",
63 blueprint.RuleParams{
64 // TODO(ccross): kotlinc doesn't support @ file for arguments, which will limit the
65 // maximum number of input files, especially on darwin.
66 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
67 `${config.KotlincCmd} $classpath $kotlincFlags ` +
Tobias Thierer96427d62017-11-18 03:10:51 +000068 `-jvm-target $kotlinJvmTarget -d $outDir $in && ` +
Colin Cross93e85952017-08-15 13:34:18 -070069 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
70 CommandDeps: []string{
71 "${config.KotlincCmd}",
72 "${config.KotlinCompilerJar}",
73 "${config.SoongZipCmd}",
74 },
75 },
Tobias Thierer96427d62017-11-18 03:10:51 +000076 "kotlincFlags", "classpath", "outDir", "kotlinJvmTarget")
Colin Cross93e85952017-08-15 13:34:18 -070077
Colin Crossc6bbef32017-08-14 14:16:06 -070078 errorprone = pctx.AndroidStaticRule("errorprone",
79 blueprint.RuleParams{
Colin Cross8eadbf02017-10-24 17:46:00 -070080 Command: `rm -rf "$outDir" "$annoDir" "$srcJarDir" && mkdir -p "$outDir" "$annoDir" "$srcJarDir" && ` +
81 `${config.ExtractSrcJarsCmd} $srcJarDir $srcJarDir/list $srcJars && ` +
Colin Crossa4820652017-10-17 13:56:52 -070082 `${config.SoongJavacWrapper} ${config.ErrorProneCmd} ` +
Colin Cross8eadbf02017-10-24 17:46:00 -070083 `$javacFlags $bootClasspath $classpath ` +
Colin Crossc6bbef32017-08-14 14:16:06 -070084 `-source $javaVersion -target $javaVersion ` +
Colin Cross8eadbf02017-10-24 17:46:00 -070085 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list && ` +
Colin Cross0a6e0072017-08-30 14:24:55 -070086 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
Colin Crossc6bbef32017-08-14 14:16:06 -070087 CommandDeps: []string{
88 "${config.JavaCmd}",
89 "${config.ErrorProneJavacJar}",
90 "${config.ErrorProneJar}",
Colin Cross0a6e0072017-08-30 14:24:55 -070091 "${config.SoongZipCmd}",
Colin Cross8eadbf02017-10-24 17:46:00 -070092 "${config.ExtractSrcJarsCmd}",
Colin Crossc6bbef32017-08-14 14:16:06 -070093 },
Colin Crossa4820652017-10-17 13:56:52 -070094 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
95 Rspfile: "$out.rsp",
96 RspfileContent: "$in",
Colin Crossc6bbef32017-08-14 14:16:06 -070097 },
Colin Cross8eadbf02017-10-24 17:46:00 -070098 "javacFlags", "bootClasspath", "classpath", "srcJars", "srcJarDir",
99 "outDir", "annoDir", "javaVersion")
Colin Crossc6bbef32017-08-14 14:16:06 -0700100
Nan Zhanged19fc32017-10-19 13:06:22 -0700101 turbine = pctx.AndroidStaticRule("turbine",
102 blueprint.RuleParams{
Colin Cross8eadbf02017-10-24 17:46:00 -0700103 Command: `rm -rf "$outDir" "$srcJarDir" && mkdir -p "$outDir" "$srcJarDir" && ` +
104 `${config.ExtractSrcJarsCmd} $srcJarDir $srcJarDir/list $srcJars && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700105 `${config.JavaCmd} -jar ${config.TurbineJar} --output $out.tmp ` +
Colin Cross8eadbf02017-10-24 17:46:00 -0700106 `--temp_dir "$outDir" --sources @$out.rsp @$srcJarDir/list ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700107 `--javacopts ${config.CommonJdkFlags} ` +
108 `$javacFlags -source $javaVersion -target $javaVersion $bootClasspath $classpath && ` +
109 `${config.Ziptime} $out.tmp && ` +
110 `(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`,
Colin Cross8eadbf02017-10-24 17:46:00 -0700111 CommandDeps: []string{
112 "${config.TurbineJar}",
113 "${config.JavaCmd}",
114 "${config.Ziptime}",
115 "${config.ExtractSrcJarsCmd}",
116 },
Nan Zhanged19fc32017-10-19 13:06:22 -0700117 Rspfile: "$out.rsp",
118 RspfileContent: "$in",
119 Restat: true,
120 },
Colin Cross8eadbf02017-10-24 17:46:00 -0700121 "javacFlags", "bootClasspath", "classpath", "srcJars", "srcJarDir",
122 "outDir", "javaVersion")
Nan Zhanged19fc32017-10-19 13:06:22 -0700123
Colin Cross9d45bb72016-08-29 16:14:13 -0700124 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -0700125 blueprint.RuleParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700126 Command: `${config.SoongZipCmd} -jar -o $out $jarArgs`,
127 CommandDeps: []string{"${config.SoongZipCmd}"},
Colin Cross2fe66872015-03-30 17:20:39 -0700128 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700129 "jarArgs")
130
131 combineJar = pctx.AndroidStaticRule("combineJar",
132 blueprint.RuleParams{
Colin Cross635acc92017-09-12 22:50:46 -0700133 Command: `${config.MergeZipsCmd} -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700134 CommandDeps: []string{"${config.MergeZipsCmd}"},
135 },
Colin Cross635acc92017-09-12 22:50:46 -0700136 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700137
Colin Cross6ade34f2017-09-15 13:00:47 -0700138 desugar = pctx.AndroidStaticRule("desugar",
139 blueprint.RuleParams{
140 Command: `rm -rf $dumpDir && mkdir -p $dumpDir && ` +
141 `${config.JavaCmd} ` +
142 `-Djdk.internal.lambda.dumpProxyClasses=$$(cd $dumpDir && pwd) ` +
143 `$javaFlags ` +
144 `-jar ${config.DesugarJar} $classpathFlags $desugarFlags ` +
145 `-i $in -o $out`,
Nan Zhanged19fc32017-10-19 13:06:22 -0700146 CommandDeps: []string{"${config.DesugarJar}", "${config.JavaCmd}"},
Colin Cross6ade34f2017-09-15 13:00:47 -0700147 },
148 "javaFlags", "classpathFlags", "desugarFlags", "dumpDir")
149
Colin Cross9d45bb72016-08-29 16:14:13 -0700150 dx = pctx.AndroidStaticRule("dx",
Colin Cross2fe66872015-03-30 17:20:39 -0700151 blueprint.RuleParams{
Colin Cross6d1e72d2015-04-10 17:44:24 -0700152 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Colin Cross6ade34f2017-09-15 13:00:47 -0700153 `${config.DxCmd} --dex --output=$outDir $dxFlags $in && ` +
Colin Cross7db5d632017-10-04 17:07:09 -0700154 `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` +
155 `${config.MergeZipsCmd} -D -stripFile "*.class" $out $outDir/classes.dex.jar $in`,
Colin Cross6ade34f2017-09-15 13:00:47 -0700156 CommandDeps: []string{
157 "${config.DxCmd}",
158 "${config.SoongZipCmd}",
Colin Cross7db5d632017-10-04 17:07:09 -0700159 "${config.MergeZipsCmd}",
Colin Cross6ade34f2017-09-15 13:00:47 -0700160 },
Colin Cross2fe66872015-03-30 17:20:39 -0700161 },
162 "outDir", "dxFlags")
Colin Crosse1d62a82015-04-03 16:53:05 -0700163
Alan Leung9f319112017-11-30 15:50:39 -0800164 d8 = pctx.AndroidStaticRule("d8",
165 blueprint.RuleParams{
166 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
167 `${config.D8Cmd} --output $outDir $dxFlags $in && ` +
168 `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` +
169 `${config.MergeZipsCmd} -D -stripFile "*.class" $out $outDir/classes.dex.jar $in`,
170 CommandDeps: []string{
171 "${config.DxCmd}",
172 "${config.SoongZipCmd}",
173 "${config.MergeZipsCmd}",
174 },
175 },
176 "outDir", "dxFlags")
177
Colin Cross9d45bb72016-08-29 16:14:13 -0700178 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700179 blueprint.RuleParams{
Colin Cross64162712017-08-08 13:17:59 -0700180 Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out",
181 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700182 },
183 "rulesFile")
Colin Cross2fe66872015-03-30 17:20:39 -0700184)
185
186func init() {
Colin Cross64162712017-08-08 13:17:59 -0700187 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700188}
189
190type javaBuilderFlags struct {
191 javacFlags string
192 dxFlags string
Colin Cross6ade34f2017-09-15 13:00:47 -0700193 bootClasspath classpath
194 classpath classpath
Colin Cross1369cdb2017-09-29 17:58:17 -0700195 systemModules classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700196 desugarFlags string
Colin Crossc0b06f12015-04-08 13:03:43 -0700197 aidlFlags string
Colin Cross64162712017-08-08 13:17:59 -0700198 javaVersion string
Colin Cross6af17aa2017-09-20 12:59:05 -0700199
Colin Cross93e85952017-08-15 13:34:18 -0700200 kotlincFlags string
201 kotlincClasspath classpath
202
Joe Onorato09e94ab2017-11-18 18:23:14 -0800203 protoFlags []string
204 protoOutTypeFlag string // The flag itself: --java_out
205 protoOutParams string // Parameters to that flag: --java_out=$protoOutParams:$outDir
Colin Cross2fe66872015-03-30 17:20:39 -0700206}
207
Colin Cross93e85952017-08-15 13:34:18 -0700208func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700209 srcFiles, srcJars android.Paths,
Colin Cross93e85952017-08-15 13:34:18 -0700210 flags javaBuilderFlags) {
211
Colin Cross1ee23172017-10-18 14:44:18 -0700212 classDir := android.PathForModuleOut(ctx, "kotlinc", "classes")
Colin Cross93e85952017-08-15 13:34:18 -0700213
214 inputs := append(android.Paths(nil), srcFiles...)
215 inputs = append(inputs, srcJars...)
216
Colin Crossae887032017-10-23 17:16:14 -0700217 ctx.Build(pctx, android.BuildParams{
Colin Cross93e85952017-08-15 13:34:18 -0700218 Rule: kotlinc,
219 Description: "kotlinc",
220 Output: outputFile,
221 Inputs: inputs,
222 Args: map[string]string{
Colin Cross715d7112017-10-25 20:32:18 -0700223 "classpath": flags.kotlincClasspath.FormJavaClassPath("-classpath"),
Colin Cross93e85952017-08-15 13:34:18 -0700224 "kotlincFlags": flags.kotlincFlags,
225 "outDir": classDir.String(),
Tobias Thierer96427d62017-11-18 03:10:51 +0000226 // http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8
227 "kotlinJvmTarget": "1.8",
Colin Cross93e85952017-08-15 13:34:18 -0700228 },
229 })
230}
231
Nan Zhang61eaedb2017-11-02 13:28:15 -0700232func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
233 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700234
Nan Zhang61eaedb2017-11-02 13:28:15 -0700235 // Compile java sources into .class files
236 desc := "javac"
237 if shardIdx >= 0 {
238 desc += strconv.Itoa(shardIdx)
239 }
240
241 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc, javac)
Colin Cross2fe66872015-03-30 17:20:39 -0700242}
243
Colin Crosse9a275b2017-10-16 17:09:48 -0700244func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700245 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700246
Colin Crossfee57cb2017-09-05 13:16:45 -0700247 if config.ErrorProneJar == "" {
248 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
Colin Crossfee57cb2017-09-05 13:16:45 -0700249 }
250
Nan Zhang61eaedb2017-11-02 13:28:15 -0700251 transformJavaToClasses(ctx, outputFile, -1, srcFiles, srcJars, flags, nil,
Colin Cross1ee23172017-10-18 14:44:18 -0700252 "errorprone", "errorprone", errorprone)
Colin Cross070879e2017-10-11 11:21:07 -0700253}
254
Nan Zhanged19fc32017-10-19 13:06:22 -0700255func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700256 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700257
258 var deps android.Paths
259 deps = append(deps, srcJars...)
260 deps = append(deps, flags.bootClasspath...)
261 deps = append(deps, flags.classpath...)
262
263 var bootClasspath string
264 if len(flags.bootClasspath) == 0 && ctx.Device() {
265 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
266 // ensure java does not fall back to the default bootclasspath.
267 bootClasspath = `--bootclasspath ""`
268 } else {
269 bootClasspath = flags.bootClasspath.FormJavaClassPath("--bootclasspath")
270 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700271
Colin Crossae887032017-10-23 17:16:14 -0700272 ctx.Build(pctx, android.BuildParams{
Nan Zhanged19fc32017-10-19 13:06:22 -0700273 Rule: turbine,
274 Description: "turbine",
275 Output: outputFile,
276 Inputs: srcFiles,
277 Implicits: deps,
278 Args: map[string]string{
279 "javacFlags": flags.javacFlags,
280 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700281 "srcJars": strings.Join(srcJars.Strings(), " "),
282 "srcJarDir": android.PathForModuleOut(ctx, "turbine", "srcjars").String(),
Nan Zhanged19fc32017-10-19 13:06:22 -0700283 "classpath": flags.classpath.FormJavaClassPath("--classpath"),
284 "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
285 "javaVersion": flags.javaVersion,
286 },
287 })
288}
289
Colin Cross070879e2017-10-11 11:21:07 -0700290// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700291// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
292// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700293//
294// This method may be used for different compilers, including javac and Error Prone. The rule
295// argument specifies which command line to use and desc sets the description of the rule that will
296// be printed at build time. The stem argument provides the file name of the output jar, and
297// suffix will be appended to various intermediate files and directories to avoid collisions when
298// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700299func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700300 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700301 flags javaBuilderFlags, deps android.Paths,
Colin Cross1ee23172017-10-18 14:44:18 -0700302 intermediatesDir, desc string, rule blueprint.Rule) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700303
Colin Cross59149b62017-10-16 18:07:29 -0700304 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700305
306 var bootClasspath string
307 if flags.javaVersion == "1.9" {
308 deps = append(deps, flags.systemModules...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700309 bootClasspath = flags.systemModules.FormJavaSystemModulesPath("--system=", ctx.Device())
Colin Cross1369cdb2017-09-29 17:58:17 -0700310 } else {
311 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700312 if len(flags.bootClasspath) == 0 && ctx.Device() {
313 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
314 // ensure java does not fall back to the default bootclasspath.
315 bootClasspath = `-bootclasspath ""`
316 } else {
317 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
318 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700319 }
320
Colin Cross6ade34f2017-09-15 13:00:47 -0700321 deps = append(deps, flags.classpath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700322
Nan Zhang61eaedb2017-11-02 13:28:15 -0700323 srcJarDir := "srcjars"
324 outDir := "classes"
325 annoDir := "anno"
326 if shardIdx >= 0 {
327 shardDir := "shard" + strconv.Itoa(shardIdx)
328 srcJarDir = filepath.Join(shardDir, srcJarDir)
329 outDir = filepath.Join(shardDir, outDir)
330 annoDir = filepath.Join(shardDir, annoDir)
331 }
Colin Crossae887032017-10-23 17:16:14 -0700332 ctx.Build(pctx, android.BuildParams{
Colin Cross070879e2017-10-11 11:21:07 -0700333 Rule: rule,
334 Description: desc,
335 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700336 Inputs: srcFiles,
337 Implicits: deps,
338 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700339 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700340 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700341 "classpath": flags.classpath.FormJavaClassPath("-classpath"),
342 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700343 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
344 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
345 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross8eadbf02017-10-24 17:46:00 -0700346 "javaVersion": flags.javaVersion,
Colin Crossc6bbef32017-08-14 14:16:06 -0700347 },
348 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700349}
350
Colin Crosse9a275b2017-10-16 17:09:48 -0700351func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
352 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700353
Colin Crossae887032017-10-23 17:16:14 -0700354 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700355 Rule: jar,
356 Description: "jar",
357 Output: outputFile,
358 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700359 Args: map[string]string{
Colin Cross0a6e0072017-08-30 14:24:55 -0700360 "jarArgs": strings.Join(jarArgs, " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700361 },
362 })
Colin Cross2fe66872015-03-30 17:20:39 -0700363}
364
Nan Zhanged19fc32017-10-19 13:06:22 -0700365func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
366 jars android.Paths, manifest android.OptionalPath, stripDirs bool, dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700367
Colin Cross635acc92017-09-12 22:50:46 -0700368 var deps android.Paths
369
370 var jarArgs []string
371 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700372 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700373 deps = append(deps, manifest.Path())
374 }
375
Nan Zhanged19fc32017-10-19 13:06:22 -0700376 if dirsToStrip != nil {
377 for _, dir := range dirsToStrip {
378 jarArgs = append(jarArgs, "-stripDir ", dir)
379 }
380 }
381
Colin Cross635acc92017-09-12 22:50:46 -0700382 if stripDirs {
383 jarArgs = append(jarArgs, "-D")
384 }
385
Colin Crossae887032017-10-23 17:16:14 -0700386 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700387 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700388 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700389 Output: outputFile,
390 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700391 Implicits: deps,
392 Args: map[string]string{
393 "jarArgs": strings.Join(jarArgs, " "),
394 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700395 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700396}
397
Colin Crosse9a275b2017-10-16 17:09:48 -0700398func TransformDesugar(ctx android.ModuleContext, outputFile android.WritablePath,
399 classesJar android.Path, flags javaBuilderFlags) {
Colin Cross6ade34f2017-09-15 13:00:47 -0700400
Colin Cross1ee23172017-10-18 14:44:18 -0700401 dumpDir := android.PathForModuleOut(ctx, "desugar", "classes")
Colin Cross6ade34f2017-09-15 13:00:47 -0700402
403 javaFlags := ""
Colin Cross6510f912017-11-29 00:27:14 -0800404 if ctx.Config().UseOpenJDK9() {
Colin Cross6ade34f2017-09-15 13:00:47 -0700405 javaFlags = "--add-opens java.base/java.lang.invoke=ALL-UNNAMED"
406 }
407
408 var desugarFlags []string
Nan Zhanged19fc32017-10-19 13:06:22 -0700409 desugarFlags = append(desugarFlags, flags.bootClasspath.FormDesugarClasspath("--bootclasspath_entry")...)
410 desugarFlags = append(desugarFlags, flags.classpath.FormDesugarClasspath("--classpath_entry")...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700411
412 var deps android.Paths
413 deps = append(deps, flags.bootClasspath...)
414 deps = append(deps, flags.classpath...)
415
Colin Crossae887032017-10-23 17:16:14 -0700416 ctx.Build(pctx, android.BuildParams{
Colin Cross6ade34f2017-09-15 13:00:47 -0700417 Rule: desugar,
418 Description: "desugar",
419 Output: outputFile,
420 Input: classesJar,
421 Implicits: deps,
422 Args: map[string]string{
423 "dumpDir": dumpDir.String(),
424 "javaFlags": javaFlags,
425 "classpathFlags": strings.Join(desugarFlags, " "),
426 "desugarFlags": flags.desugarFlags,
427 },
428 })
Colin Cross6ade34f2017-09-15 13:00:47 -0700429}
430
Colin Cross7db5d632017-10-04 17:07:09 -0700431// Converts a classes.jar file to classes*.dex, then combines the dex files with any resources
432// in the classes.jar file into a dex jar.
Colin Crosse9a275b2017-10-16 17:09:48 -0700433func TransformClassesJarToDexJar(ctx android.ModuleContext, outputFile android.WritablePath,
434 classesJar android.Path, flags javaBuilderFlags) {
Colin Cross2fe66872015-03-30 17:20:39 -0700435
Colin Cross635c3b02016-05-18 15:37:25 -0700436 outDir := android.PathForModuleOut(ctx, "dex")
Colin Cross2fe66872015-03-30 17:20:39 -0700437
Alan Leung9f319112017-11-30 15:50:39 -0800438 rule := dx
439 desc := "dx"
440 if ctx.AConfig().IsEnvTrue("USE_D8_DESUGAR") {
441 rule = d8
442 desc = "d8"
443 }
Colin Crossae887032017-10-23 17:16:14 -0700444 ctx.Build(pctx, android.BuildParams{
Alan Leung9f319112017-11-30 15:50:39 -0800445 Rule: rule,
446 Description: desc,
Colin Cross67a5c132017-05-09 13:45:28 -0700447 Output: outputFile,
448 Input: classesJar,
Colin Cross2fe66872015-03-30 17:20:39 -0700449 Args: map[string]string{
450 "dxFlags": flags.dxFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700451 "outDir": outDir.String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700452 },
453 })
Colin Cross2fe66872015-03-30 17:20:39 -0700454}
Colin Crosse1d62a82015-04-03 16:53:05 -0700455
Colin Crosse9a275b2017-10-16 17:09:48 -0700456func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
457 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700458 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700459 Rule: jarjar,
460 Description: "jarjar",
461 Output: outputFile,
462 Input: classesJar,
463 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700464 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700465 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700466 },
467 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700468}
Colin Cross6ade34f2017-09-15 13:00:47 -0700469
470type classpath []android.Path
471
Nan Zhanged19fc32017-10-19 13:06:22 -0700472func (x *classpath) FormJavaClassPath(optName string) string {
Colin Cross59149b62017-10-16 18:07:29 -0700473 if len(*x) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700474 return optName + " " + strings.Join(x.Strings(), ":")
Colin Cross6ade34f2017-09-15 13:00:47 -0700475 } else {
476 return ""
477 }
478}
479
Colin Cross1369cdb2017-09-29 17:58:17 -0700480// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
481// returns --system=none if the list is empty to ensure javac does not fall back to the default
482// system modules.
Nan Zhanged19fc32017-10-19 13:06:22 -0700483func (x *classpath) FormJavaSystemModulesPath(optName string, forceEmpty bool) string {
Colin Cross1369cdb2017-09-29 17:58:17 -0700484 if len(*x) > 1 {
485 panic("more than one system module")
486 } else if len(*x) == 1 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700487 return optName + strings.TrimSuffix((*x)[0].String(), "lib/modules")
Colin Cross1369cdb2017-09-29 17:58:17 -0700488 } else if forceEmpty {
Nan Zhanged19fc32017-10-19 13:06:22 -0700489 return optName + "none"
Colin Cross1369cdb2017-09-29 17:58:17 -0700490 } else {
491 return ""
492 }
493}
494
Nan Zhanged19fc32017-10-19 13:06:22 -0700495func (x *classpath) FormDesugarClasspath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700496 if x == nil || *x == nil {
497 return nil
498 }
499 flags := make([]string, len(*x))
500 for i, v := range *x {
Nan Zhanged19fc32017-10-19 13:06:22 -0700501 flags[i] = optName + " " + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700502 }
503
504 return flags
505}
506
507// Append an android.Paths to the end of the classpath list
508func (x *classpath) AddPaths(paths android.Paths) {
509 for _, path := range paths {
510 *x = append(*x, path)
511 }
512}
513
514// Convert a classpath to an android.Paths
515func (x *classpath) Paths() android.Paths {
516 return append(android.Paths(nil), (*x)...)
517}
518
519func (x *classpath) Strings() []string {
520 if x == nil {
521 return nil
522 }
523 ret := make([]string, len(*x))
524 for i, path := range *x {
525 ret[i] = path.String()
526 }
527 return ret
528}