blob: 861566495ae17cba65919815bffd565ec036fa58 [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 Crossfe4bc362018-09-12 10:02:13 -070027 "github.com/google/blueprint/proptools"
Colin Crossfee57cb2017-09-05 13:16:45 -070028
29 "android/soong/android"
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
Colin Crossf7eac7a2018-02-08 12:48:39 -080038 // this, all java rules write into separate directories and then are combined into a .jar file
39 // (if the rule produces .class files) or a .srcjar file (if the rule produces .java files).
40 // .srcjar files are unzipped into a temporary directory when compiled with javac.
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" && ` +
Colin Cross436b7652018-03-15 16:24:10 -070044 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090045 `(if [ -s $srcJarDir/list ] || [ -s $out.rsp ] ; then ` +
Colin Crossa4820652017-10-17 13:56:52 -070046 `${config.SoongJavacWrapper} ${config.JavacWrapper}${config.JavacCmd} ${config.JavacHeapFlags} ${config.CommonJdkFlags} ` +
Colin Cross6a77c982018-06-19 22:43:34 -070047 `$processorpath $javacFlags $bootClasspath $classpath ` +
Colin Cross64162712017-08-08 13:17:59 -070048 `-source $javaVersion -target $javaVersion ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090049 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list ; fi ) && ` +
Colin Cross0a6e0072017-08-30 14:24:55 -070050 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
Colin Cross8eadbf02017-10-24 17:46:00 -070051 CommandDeps: []string{
52 "${config.JavacCmd}",
53 "${config.SoongZipCmd}",
Colin Cross436b7652018-03-15 16:24:10 -070054 "${config.ZipSyncCmd}",
Colin Cross8eadbf02017-10-24 17:46:00 -070055 },
Colin Crossa4820652017-10-17 13:56:52 -070056 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
57 Rspfile: "$out.rsp",
58 RspfileContent: "$in",
Colin Cross2fe66872015-03-30 17:20:39 -070059 },
Colin Cross6a77c982018-06-19 22:43:34 -070060 "javacFlags", "bootClasspath", "classpath", "processorpath", "srcJars", "srcJarDir",
Colin Cross8eadbf02017-10-24 17:46:00 -070061 "outDir", "annoDir", "javaVersion")
Colin Cross2fe66872015-03-30 17:20:39 -070062
Colin Cross93e85952017-08-15 13:34:18 -070063 kotlinc = pctx.AndroidGomaStaticRule("kotlinc",
64 blueprint.RuleParams{
Colin Cross0f40a752018-09-08 11:52:49 -070065 Command: `rm -rf "$classesDir" "$srcJarDir" "$kotlinBuildFile" && mkdir -p "$classesDir" "$srcJarDir" && ` +
Colin Cross436b7652018-03-15 16:24:10 -070066 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
Colin Cross0f40a752018-09-08 11:52:49 -070067 `${config.GenKotlinBuildFileCmd} $classpath $classesDir $out.rsp $srcJarDir/list > $kotlinBuildFile &&` +
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +000068 `${config.KotlincCmd} $kotlincFlags ` +
Colin Cross0f40a752018-09-08 11:52:49 -070069 `-jvm-target $kotlinJvmTarget -Xbuild-file=$kotlinBuildFile && ` +
70 `${config.SoongZipCmd} -jar -o $out -C $classesDir -D $classesDir`,
Colin Cross93e85952017-08-15 13:34:18 -070071 CommandDeps: []string{
72 "${config.KotlincCmd}",
73 "${config.KotlinCompilerJar}",
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +000074 "${config.GenKotlinBuildFileCmd}",
Colin Cross93e85952017-08-15 13:34:18 -070075 "${config.SoongZipCmd}",
Colin Cross436b7652018-03-15 16:24:10 -070076 "${config.ZipSyncCmd}",
Colin Cross93e85952017-08-15 13:34:18 -070077 },
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +000078 Rspfile: "$out.rsp",
79 RspfileContent: `$in`,
Colin Cross93e85952017-08-15 13:34:18 -070080 },
Colin Cross0f40a752018-09-08 11:52:49 -070081 "kotlincFlags", "classpath", "srcJars", "srcJarDir", "classesDir", "kotlinJvmTarget", "kotlinBuildFile")
Colin Cross93e85952017-08-15 13:34:18 -070082
Nan Zhanged19fc32017-10-19 13:06:22 -070083 turbine = pctx.AndroidStaticRule("turbine",
84 blueprint.RuleParams{
Colin Cross6981f652018-03-07 15:14:50 -080085 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070086 `${config.JavaCmd} -jar ${config.TurbineJar} --output $out.tmp ` +
Colin Cross6981f652018-03-07 15:14:50 -080087 `--temp_dir "$outDir" --sources @$out.rsp --source_jars $srcJars ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070088 `--javacopts ${config.CommonJdkFlags} ` +
Colin Cross924a0aa2018-03-07 10:51:05 -080089 `$javacFlags -source $javaVersion -target $javaVersion -- $bootClasspath $classpath && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070090 `${config.Ziptime} $out.tmp && ` +
91 `(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`,
Colin Cross8eadbf02017-10-24 17:46:00 -070092 CommandDeps: []string{
93 "${config.TurbineJar}",
94 "${config.JavaCmd}",
95 "${config.Ziptime}",
Colin Cross8eadbf02017-10-24 17:46:00 -070096 },
Nan Zhanged19fc32017-10-19 13:06:22 -070097 Rspfile: "$out.rsp",
98 RspfileContent: "$in",
99 Restat: true,
100 },
Colin Cross6981f652018-03-07 15:14:50 -0800101 "javacFlags", "bootClasspath", "classpath", "srcJars", "outDir", "javaVersion")
Nan Zhanged19fc32017-10-19 13:06:22 -0700102
Colin Cross9d45bb72016-08-29 16:14:13 -0700103 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -0700104 blueprint.RuleParams{
Nan Zhang674dd932018-01-26 18:30:36 -0800105 Command: `${config.SoongZipCmd} -jar -o $out @$out.rsp`,
106 CommandDeps: []string{"${config.SoongZipCmd}"},
107 Rspfile: "$out.rsp",
108 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -0700109 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700110 "jarArgs")
111
Colin Crossa4f08812018-10-02 22:03:40 -0700112 zip = pctx.AndroidStaticRule("zip",
113 blueprint.RuleParams{
114 Command: `${config.SoongZipCmd} -o $out @$out.rsp`,
115 CommandDeps: []string{"${config.SoongZipCmd}"},
116 Rspfile: "$out.rsp",
117 RspfileContent: "$jarArgs",
118 },
119 "jarArgs")
120
Colin Cross0a6e0072017-08-30 14:24:55 -0700121 combineJar = pctx.AndroidStaticRule("combineJar",
122 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800123 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700124 CommandDeps: []string{"${config.MergeZipsCmd}"},
125 },
Colin Cross635acc92017-09-12 22:50:46 -0700126 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700127
Colin Cross9d45bb72016-08-29 16:14:13 -0700128 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700129 blueprint.RuleParams{
Colin Cross64162712017-08-08 13:17:59 -0700130 Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out",
131 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700132 },
133 "rulesFile")
Nan Zhang4c819fb2018-08-27 18:31:46 -0700134
135 jetifier = pctx.AndroidStaticRule("jetifier",
136 blueprint.RuleParams{
137 Command: "${config.JavaCmd} -jar ${config.JetifierJar} -l error -o $out -i $in",
138 CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"},
139 },
140 )
Colin Cross43f08db2018-11-12 10:13:39 -0800141
142 zipalign = pctx.AndroidStaticRule("zipalign",
143 blueprint.RuleParams{
144 Command: "if ! ${config.ZipAlign} -c 4 $in > /dev/null; then " +
145 "${config.ZipAlign} -f 4 $in $out; " +
146 "else " +
147 "cp -f $in $out; " +
148 "fi",
149 CommandDeps: []string{"${config.ZipAlign}"},
150 },
151 )
Colin Cross2fe66872015-03-30 17:20:39 -0700152)
153
154func init() {
Colin Cross3063b782018-08-15 11:19:12 -0700155 pctx.Import("android/soong/common")
Colin Cross64162712017-08-08 13:17:59 -0700156 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700157}
158
159type javaBuilderFlags struct {
160 javacFlags string
Colin Cross6ade34f2017-09-15 13:00:47 -0700161 bootClasspath classpath
162 classpath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700163 processorPath classpath
Colin Cross1369cdb2017-09-29 17:58:17 -0700164 systemModules classpath
Colin Crossc0b06f12015-04-08 13:03:43 -0700165 aidlFlags string
Colin Cross64162712017-08-08 13:17:59 -0700166 javaVersion string
Colin Cross6af17aa2017-09-20 12:59:05 -0700167
Andreas Gampef3e5b552018-01-22 21:27:21 -0800168 errorProneExtraJavacFlags string
Colin Cross66548102018-06-19 22:47:35 -0700169 errorProneProcessorPath classpath
Andreas Gampef3e5b552018-01-22 21:27:21 -0800170
Colin Cross93e85952017-08-15 13:34:18 -0700171 kotlincFlags string
172 kotlincClasspath classpath
173
Joe Onorato09e94ab2017-11-18 18:23:14 -0800174 protoFlags []string
175 protoOutTypeFlag string // The flag itself: --java_out
176 protoOutParams string // Parameters to that flag: --java_out=$protoOutParams:$outDir
Dan Willemsenab9f4262018-02-14 13:58:34 -0800177 protoRoot bool
Colin Cross2fe66872015-03-30 17:20:39 -0700178}
179
Colin Cross93e85952017-08-15 13:34:18 -0700180func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700181 srcFiles, srcJars android.Paths,
Colin Cross93e85952017-08-15 13:34:18 -0700182 flags javaBuilderFlags) {
183
Colin Cross93e85952017-08-15 13:34:18 -0700184 inputs := append(android.Paths(nil), srcFiles...)
Colin Cross93e85952017-08-15 13:34:18 -0700185
Colin Cross9bc43432017-12-15 20:20:39 -0800186 var deps android.Paths
187 deps = append(deps, flags.kotlincClasspath...)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000188 deps = append(deps, srcJars...)
Colin Cross9bc43432017-12-15 20:20:39 -0800189
Colin Crossae887032017-10-23 17:16:14 -0700190 ctx.Build(pctx, android.BuildParams{
Colin Cross93e85952017-08-15 13:34:18 -0700191 Rule: kotlinc,
192 Description: "kotlinc",
193 Output: outputFile,
194 Inputs: inputs,
Colin Cross9bc43432017-12-15 20:20:39 -0800195 Implicits: deps,
Colin Cross93e85952017-08-15 13:34:18 -0700196 Args: map[string]string{
Colin Cross0f40a752018-09-08 11:52:49 -0700197 "classpath": flags.kotlincClasspath.FormJavaClassPath("-classpath"),
198 "kotlincFlags": flags.kotlincFlags,
199 "srcJars": strings.Join(srcJars.Strings(), " "),
200 "classesDir": android.PathForModuleOut(ctx, "kotlinc", "classes").String(),
201 "srcJarDir": android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(),
202 "kotlinBuildFile": android.PathForModuleOut(ctx, "kotlinc-build.xml").String(),
Tobias Thierer96427d62017-11-18 03:10:51 +0000203 // http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8
204 "kotlinJvmTarget": "1.8",
Colin Cross93e85952017-08-15 13:34:18 -0700205 },
206 })
207}
208
Nan Zhang61eaedb2017-11-02 13:28:15 -0700209func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
210 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700211
Nan Zhang61eaedb2017-11-02 13:28:15 -0700212 // Compile java sources into .class files
213 desc := "javac"
214 if shardIdx >= 0 {
215 desc += strconv.Itoa(shardIdx)
216 }
217
Colin Cross66548102018-06-19 22:47:35 -0700218 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc)
Colin Cross2fe66872015-03-30 17:20:39 -0700219}
220
Colin Crosse9a275b2017-10-16 17:09:48 -0700221func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700222 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700223
Colin Cross66548102018-06-19 22:47:35 -0700224 flags.processorPath = append(flags.errorProneProcessorPath, flags.processorPath...)
Colin Crossfee57cb2017-09-05 13:16:45 -0700225
Andreas Gampef3e5b552018-01-22 21:27:21 -0800226 if len(flags.errorProneExtraJavacFlags) > 0 {
227 if len(flags.javacFlags) > 0 {
Colin Cross66548102018-06-19 22:47:35 -0700228 flags.javacFlags += " " + flags.errorProneExtraJavacFlags
Andreas Gampef3e5b552018-01-22 21:27:21 -0800229 } else {
230 flags.javacFlags = flags.errorProneExtraJavacFlags
231 }
232 }
233
Nan Zhang61eaedb2017-11-02 13:28:15 -0700234 transformJavaToClasses(ctx, outputFile, -1, srcFiles, srcJars, flags, nil,
Colin Cross66548102018-06-19 22:47:35 -0700235 "errorprone", "errorprone")
Colin Cross070879e2017-10-11 11:21:07 -0700236}
237
Nan Zhanged19fc32017-10-19 13:06:22 -0700238func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700239 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700240
241 var deps android.Paths
242 deps = append(deps, srcJars...)
243 deps = append(deps, flags.bootClasspath...)
244 deps = append(deps, flags.classpath...)
245
246 var bootClasspath string
247 if len(flags.bootClasspath) == 0 && ctx.Device() {
248 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
249 // ensure java does not fall back to the default bootclasspath.
250 bootClasspath = `--bootclasspath ""`
251 } else {
Colin Crossbafb8972018-06-06 21:46:32 +0000252 bootClasspath = strings.Join(flags.bootClasspath.FormTurbineClasspath("--bootclasspath"), " ")
Nan Zhanged19fc32017-10-19 13:06:22 -0700253 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700254
Colin Crossae887032017-10-23 17:16:14 -0700255 ctx.Build(pctx, android.BuildParams{
Nan Zhanged19fc32017-10-19 13:06:22 -0700256 Rule: turbine,
257 Description: "turbine",
258 Output: outputFile,
259 Inputs: srcFiles,
260 Implicits: deps,
261 Args: map[string]string{
262 "javacFlags": flags.javacFlags,
263 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700264 "srcJars": strings.Join(srcJars.Strings(), " "),
Colin Crossbafb8972018-06-06 21:46:32 +0000265 "classpath": strings.Join(flags.classpath.FormTurbineClasspath("--classpath"), " "),
Nan Zhanged19fc32017-10-19 13:06:22 -0700266 "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
267 "javaVersion": flags.javaVersion,
268 },
269 })
270}
271
Colin Cross070879e2017-10-11 11:21:07 -0700272// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700273// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
274// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700275//
276// This method may be used for different compilers, including javac and Error Prone. The rule
277// argument specifies which command line to use and desc sets the description of the rule that will
278// be printed at build time. The stem argument provides the file name of the output jar, and
279// suffix will be appended to various intermediate files and directories to avoid collisions when
280// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700281func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700282 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700283 flags javaBuilderFlags, deps android.Paths,
Colin Cross66548102018-06-19 22:47:35 -0700284 intermediatesDir, desc string) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700285
Colin Cross59149b62017-10-16 18:07:29 -0700286 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700287
288 var bootClasspath string
289 if flags.javaVersion == "1.9" {
290 deps = append(deps, flags.systemModules...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700291 bootClasspath = flags.systemModules.FormJavaSystemModulesPath("--system=", ctx.Device())
Colin Cross1369cdb2017-09-29 17:58:17 -0700292 } else {
293 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700294 if len(flags.bootClasspath) == 0 && ctx.Device() {
295 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
296 // ensure java does not fall back to the default bootclasspath.
297 bootClasspath = `-bootclasspath ""`
298 } else {
299 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
300 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700301 }
302
Colin Cross6ade34f2017-09-15 13:00:47 -0700303 deps = append(deps, flags.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700304 deps = append(deps, flags.processorPath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700305
Nan Zhang61eaedb2017-11-02 13:28:15 -0700306 srcJarDir := "srcjars"
307 outDir := "classes"
308 annoDir := "anno"
309 if shardIdx >= 0 {
310 shardDir := "shard" + strconv.Itoa(shardIdx)
311 srcJarDir = filepath.Join(shardDir, srcJarDir)
312 outDir = filepath.Join(shardDir, outDir)
313 annoDir = filepath.Join(shardDir, annoDir)
314 }
Colin Crossae887032017-10-23 17:16:14 -0700315 ctx.Build(pctx, android.BuildParams{
Colin Cross66548102018-06-19 22:47:35 -0700316 Rule: javac,
Colin Cross070879e2017-10-11 11:21:07 -0700317 Description: desc,
318 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700319 Inputs: srcFiles,
320 Implicits: deps,
321 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700322 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700323 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700324 "classpath": flags.classpath.FormJavaClassPath("-classpath"),
Colin Cross6a77c982018-06-19 22:43:34 -0700325 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
Colin Cross8eadbf02017-10-24 17:46:00 -0700326 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700327 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
328 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
329 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross8eadbf02017-10-24 17:46:00 -0700330 "javaVersion": flags.javaVersion,
Colin Crossc6bbef32017-08-14 14:16:06 -0700331 },
332 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700333}
334
Colin Crosse9a275b2017-10-16 17:09:48 -0700335func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
336 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700337
Colin Crossae887032017-10-23 17:16:14 -0700338 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700339 Rule: jar,
340 Description: "jar",
341 Output: outputFile,
342 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700343 Args: map[string]string{
Colin Cross1d98ee22018-09-18 17:05:15 -0700344 "jarArgs": strings.Join(proptools.NinjaAndShellEscape(jarArgs), " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700345 },
346 })
Colin Cross2fe66872015-03-30 17:20:39 -0700347}
348
Nan Zhanged19fc32017-10-19 13:06:22 -0700349func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
Colin Cross37f6d792018-07-12 12:28:41 -0700350 jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
351 dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700352
Colin Cross635acc92017-09-12 22:50:46 -0700353 var deps android.Paths
354
355 var jarArgs []string
356 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700357 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700358 deps = append(deps, manifest.Path())
359 }
360
Colin Cross37f6d792018-07-12 12:28:41 -0700361 for _, dir := range dirsToStrip {
362 jarArgs = append(jarArgs, "-stripDir ", dir)
363 }
364
365 for _, file := range filesToStrip {
366 jarArgs = append(jarArgs, "-stripFile ", file)
Nan Zhanged19fc32017-10-19 13:06:22 -0700367 }
368
Colin Cross7b60cdd2017-12-21 13:52:58 -0800369 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
370 // for downstream tools like desugar.
371 jarArgs = append(jarArgs, "-stripFile module-info.class")
372
Colin Cross37f6d792018-07-12 12:28:41 -0700373 if stripDirEntries {
Colin Cross635acc92017-09-12 22:50:46 -0700374 jarArgs = append(jarArgs, "-D")
375 }
376
Colin Crossae887032017-10-23 17:16:14 -0700377 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700378 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700379 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700380 Output: outputFile,
381 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700382 Implicits: deps,
383 Args: map[string]string{
384 "jarArgs": strings.Join(jarArgs, " "),
385 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700386 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700387}
388
Colin Crosse9a275b2017-10-16 17:09:48 -0700389func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
390 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700391 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700392 Rule: jarjar,
393 Description: "jarjar",
394 Output: outputFile,
395 Input: classesJar,
396 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700397 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700398 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700399 },
400 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700401}
Colin Cross6ade34f2017-09-15 13:00:47 -0700402
Nan Zhang4c819fb2018-08-27 18:31:46 -0700403func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath,
404 inputFile android.Path) {
405 ctx.Build(pctx, android.BuildParams{
406 Rule: jetifier,
407 Description: "jetifier",
408 Output: outputFile,
409 Input: inputFile,
410 })
411}
412
Colin Cross094054a2018-10-17 15:10:48 -0700413func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) {
414 ctx.Build(pctx, android.BuildParams{
415 Rule: android.WriteFile,
416 Description: "manifest",
417 Output: outputFile,
418 Args: map[string]string{
419 "content": "Main-Class: " + mainClass + "\n",
420 },
421 })
422}
423
Colin Cross43f08db2018-11-12 10:13:39 -0800424func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path) {
425 ctx.Build(pctx, android.BuildParams{
426 Rule: zipalign,
427 Description: "align",
428 Input: inputFile,
429 Output: outputFile,
430 })
431}
432
Colin Cross6ade34f2017-09-15 13:00:47 -0700433type classpath []android.Path
434
Nan Zhanged19fc32017-10-19 13:06:22 -0700435func (x *classpath) FormJavaClassPath(optName string) string {
Colin Cross81440082018-08-15 20:21:55 -0700436 if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") {
437 optName += " "
438 }
Colin Cross59149b62017-10-16 18:07:29 -0700439 if len(*x) > 0 {
Colin Cross81440082018-08-15 20:21:55 -0700440 return optName + strings.Join(x.Strings(), ":")
Colin Cross6ade34f2017-09-15 13:00:47 -0700441 } else {
442 return ""
443 }
444}
445
Colin Cross1369cdb2017-09-29 17:58:17 -0700446// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
447// returns --system=none if the list is empty to ensure javac does not fall back to the default
448// system modules.
Nan Zhanged19fc32017-10-19 13:06:22 -0700449func (x *classpath) FormJavaSystemModulesPath(optName string, forceEmpty bool) string {
Colin Cross1369cdb2017-09-29 17:58:17 -0700450 if len(*x) > 1 {
451 panic("more than one system module")
452 } else if len(*x) == 1 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700453 return optName + strings.TrimSuffix((*x)[0].String(), "lib/modules")
Colin Cross1369cdb2017-09-29 17:58:17 -0700454 } else if forceEmpty {
Nan Zhanged19fc32017-10-19 13:06:22 -0700455 return optName + "none"
Colin Cross1369cdb2017-09-29 17:58:17 -0700456 } else {
457 return ""
458 }
459}
460
Colin Crossbafb8972018-06-06 21:46:32 +0000461func (x *classpath) FormTurbineClasspath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700462 if x == nil || *x == nil {
463 return nil
464 }
465 flags := make([]string, len(*x))
466 for i, v := range *x {
Nan Zhanged19fc32017-10-19 13:06:22 -0700467 flags[i] = optName + " " + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700468 }
469
470 return flags
471}
472
Colin Cross6ade34f2017-09-15 13:00:47 -0700473// Convert a classpath to an android.Paths
474func (x *classpath) Paths() android.Paths {
475 return append(android.Paths(nil), (*x)...)
476}
477
478func (x *classpath) Strings() []string {
479 if x == nil {
480 return nil
481 }
482 ret := make([]string, len(*x))
483 for i, path := range *x {
484 ret[i] = path.String()
485 }
486 return ret
487}