blob: 55be3a6b53ec7993d705e2825f70d06c58ab226a [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"
Colin Cross2fe66872015-03-30 17:20:39 -070029)
30
31var (
Colin Cross635c3b02016-05-18 15:37:25 -070032 pctx = android.NewPackageContext("android/soong/java")
Colin Cross2fe66872015-03-30 17:20:39 -070033
34 // Compiling java is not conducive to proper dependency tracking. The path-matches-class-name
35 // requirement leads to unpredictable generated source file names, and a single .java file
36 // will get compiled into multiple .class files if it contains inner classes. To work around
Colin Crossf7eac7a2018-02-08 12:48:39 -080037 // this, all java rules write into separate directories and then are combined into a .jar file
38 // (if the rule produces .class files) or a .srcjar file (if the rule produces .java files).
39 // .srcjar files are unzipped into a temporary directory when compiled with javac.
Yoshisato Yanagisawa572324a2017-06-05 17:41:50 +090040 javac = pctx.AndroidGomaStaticRule("javac",
Colin Cross2fe66872015-03-30 17:20:39 -070041 blueprint.RuleParams{
Colin Cross8eadbf02017-10-24 17:46:00 -070042 Command: `rm -rf "$outDir" "$annoDir" "$srcJarDir" && mkdir -p "$outDir" "$annoDir" "$srcJarDir" && ` +
Colin Cross436b7652018-03-15 16:24:10 -070043 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090044 `(if [ -s $srcJarDir/list ] || [ -s $out.rsp ] ; then ` +
Colin Crossa4820652017-10-17 13:56:52 -070045 `${config.SoongJavacWrapper} ${config.JavacWrapper}${config.JavacCmd} ${config.JavacHeapFlags} ${config.CommonJdkFlags} ` +
Colin Cross6a77c982018-06-19 22:43:34 -070046 `$processorpath $javacFlags $bootClasspath $classpath ` +
Colin Cross64162712017-08-08 13:17:59 -070047 `-source $javaVersion -target $javaVersion ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090048 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list ; fi ) && ` +
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}",
Colin Cross436b7652018-03-15 16:24:10 -070053 "${config.ZipSyncCmd}",
Colin Cross8eadbf02017-10-24 17:46:00 -070054 },
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 Cross6a77c982018-06-19 22:43:34 -070059 "javacFlags", "bootClasspath", "classpath", "processorpath", "srcJars", "srcJarDir",
Colin Cross8eadbf02017-10-24 17:46:00 -070060 "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{
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +000064 Command: `rm -rf "$outDir" "$srcJarDir" && mkdir -p "$outDir" "$srcJarDir" && ` +
Colin Cross436b7652018-03-15 16:24:10 -070065 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +000066 `${config.GenKotlinBuildFileCmd} $classpath $outDir $out.rsp $srcJarDir/list > $outDir/kotlinc-build.xml &&` +
67 `${config.KotlincCmd} $kotlincFlags ` +
68 `-jvm-target $kotlinJvmTarget -Xbuild-file=$outDir/kotlinc-build.xml && ` +
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +000069 `rm $outDir/kotlinc-build.xml && ` +
Colin Cross93e85952017-08-15 13:34:18 -070070 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
71 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 },
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +000081 "kotlincFlags", "classpath", "srcJars", "srcJarDir", "outDir", "kotlinJvmTarget")
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
112 combineJar = pctx.AndroidStaticRule("combineJar",
113 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800114 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700115 CommandDeps: []string{"${config.MergeZipsCmd}"},
116 },
Colin Cross635acc92017-09-12 22:50:46 -0700117 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700118
Colin Cross9d45bb72016-08-29 16:14:13 -0700119 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700120 blueprint.RuleParams{
Colin Cross64162712017-08-08 13:17:59 -0700121 Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out",
122 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700123 },
124 "rulesFile")
Colin Cross2fe66872015-03-30 17:20:39 -0700125)
126
127func init() {
Colin Cross64162712017-08-08 13:17:59 -0700128 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700129}
130
131type javaBuilderFlags struct {
132 javacFlags string
Colin Cross6ade34f2017-09-15 13:00:47 -0700133 bootClasspath classpath
134 classpath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700135 processorPath classpath
Colin Cross1369cdb2017-09-29 17:58:17 -0700136 systemModules classpath
Colin Crossc0b06f12015-04-08 13:03:43 -0700137 aidlFlags string
Colin Cross64162712017-08-08 13:17:59 -0700138 javaVersion string
Colin Cross6af17aa2017-09-20 12:59:05 -0700139
Andreas Gampef3e5b552018-01-22 21:27:21 -0800140 errorProneExtraJavacFlags string
Colin Cross66548102018-06-19 22:47:35 -0700141 errorProneProcessorPath classpath
Andreas Gampef3e5b552018-01-22 21:27:21 -0800142
Colin Cross93e85952017-08-15 13:34:18 -0700143 kotlincFlags string
144 kotlincClasspath classpath
145
Joe Onorato09e94ab2017-11-18 18:23:14 -0800146 protoFlags []string
147 protoOutTypeFlag string // The flag itself: --java_out
148 protoOutParams string // Parameters to that flag: --java_out=$protoOutParams:$outDir
Dan Willemsenab9f4262018-02-14 13:58:34 -0800149 protoRoot bool
Colin Cross2fe66872015-03-30 17:20:39 -0700150}
151
Colin Cross93e85952017-08-15 13:34:18 -0700152func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700153 srcFiles, srcJars android.Paths,
Colin Cross93e85952017-08-15 13:34:18 -0700154 flags javaBuilderFlags) {
155
Colin Cross93e85952017-08-15 13:34:18 -0700156 inputs := append(android.Paths(nil), srcFiles...)
Colin Cross93e85952017-08-15 13:34:18 -0700157
Colin Cross9bc43432017-12-15 20:20:39 -0800158 var deps android.Paths
159 deps = append(deps, flags.kotlincClasspath...)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000160 deps = append(deps, srcJars...)
Colin Cross9bc43432017-12-15 20:20:39 -0800161
Colin Crossae887032017-10-23 17:16:14 -0700162 ctx.Build(pctx, android.BuildParams{
Colin Cross93e85952017-08-15 13:34:18 -0700163 Rule: kotlinc,
164 Description: "kotlinc",
165 Output: outputFile,
166 Inputs: inputs,
Colin Cross9bc43432017-12-15 20:20:39 -0800167 Implicits: deps,
Colin Cross93e85952017-08-15 13:34:18 -0700168 Args: map[string]string{
Colin Cross715d7112017-10-25 20:32:18 -0700169 "classpath": flags.kotlincClasspath.FormJavaClassPath("-classpath"),
Colin Cross93e85952017-08-15 13:34:18 -0700170 "kotlincFlags": flags.kotlincFlags,
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000171 "srcJars": strings.Join(srcJars.Strings(), " "),
172 "outDir": android.PathForModuleOut(ctx, "kotlinc", "classes").String(),
173 "srcJarDir": android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(),
Tobias Thierer96427d62017-11-18 03:10:51 +0000174 // http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8
175 "kotlinJvmTarget": "1.8",
Colin Cross93e85952017-08-15 13:34:18 -0700176 },
177 })
178}
179
Nan Zhang61eaedb2017-11-02 13:28:15 -0700180func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
181 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700182
Nan Zhang61eaedb2017-11-02 13:28:15 -0700183 // Compile java sources into .class files
184 desc := "javac"
185 if shardIdx >= 0 {
186 desc += strconv.Itoa(shardIdx)
187 }
188
Colin Cross66548102018-06-19 22:47:35 -0700189 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc)
Colin Cross2fe66872015-03-30 17:20:39 -0700190}
191
Colin Crosse9a275b2017-10-16 17:09:48 -0700192func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700193 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700194
Colin Cross66548102018-06-19 22:47:35 -0700195 flags.processorPath = append(flags.errorProneProcessorPath, flags.processorPath...)
Colin Crossfee57cb2017-09-05 13:16:45 -0700196
Andreas Gampef3e5b552018-01-22 21:27:21 -0800197 if len(flags.errorProneExtraJavacFlags) > 0 {
198 if len(flags.javacFlags) > 0 {
Colin Cross66548102018-06-19 22:47:35 -0700199 flags.javacFlags += " " + flags.errorProneExtraJavacFlags
Andreas Gampef3e5b552018-01-22 21:27:21 -0800200 } else {
201 flags.javacFlags = flags.errorProneExtraJavacFlags
202 }
203 }
204
Nan Zhang61eaedb2017-11-02 13:28:15 -0700205 transformJavaToClasses(ctx, outputFile, -1, srcFiles, srcJars, flags, nil,
Colin Cross66548102018-06-19 22:47:35 -0700206 "errorprone", "errorprone")
Colin Cross070879e2017-10-11 11:21:07 -0700207}
208
Nan Zhanged19fc32017-10-19 13:06:22 -0700209func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700210 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700211
212 var deps android.Paths
213 deps = append(deps, srcJars...)
214 deps = append(deps, flags.bootClasspath...)
215 deps = append(deps, flags.classpath...)
216
217 var bootClasspath string
218 if len(flags.bootClasspath) == 0 && ctx.Device() {
219 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
220 // ensure java does not fall back to the default bootclasspath.
221 bootClasspath = `--bootclasspath ""`
222 } else {
Colin Crossbafb8972018-06-06 21:46:32 +0000223 bootClasspath = strings.Join(flags.bootClasspath.FormTurbineClasspath("--bootclasspath"), " ")
Nan Zhanged19fc32017-10-19 13:06:22 -0700224 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700225
Colin Crossae887032017-10-23 17:16:14 -0700226 ctx.Build(pctx, android.BuildParams{
Nan Zhanged19fc32017-10-19 13:06:22 -0700227 Rule: turbine,
228 Description: "turbine",
229 Output: outputFile,
230 Inputs: srcFiles,
231 Implicits: deps,
232 Args: map[string]string{
233 "javacFlags": flags.javacFlags,
234 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700235 "srcJars": strings.Join(srcJars.Strings(), " "),
Colin Crossbafb8972018-06-06 21:46:32 +0000236 "classpath": strings.Join(flags.classpath.FormTurbineClasspath("--classpath"), " "),
Nan Zhanged19fc32017-10-19 13:06:22 -0700237 "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
238 "javaVersion": flags.javaVersion,
239 },
240 })
241}
242
Colin Cross070879e2017-10-11 11:21:07 -0700243// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700244// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
245// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700246//
247// This method may be used for different compilers, including javac and Error Prone. The rule
248// argument specifies which command line to use and desc sets the description of the rule that will
249// be printed at build time. The stem argument provides the file name of the output jar, and
250// suffix will be appended to various intermediate files and directories to avoid collisions when
251// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700252func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700253 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700254 flags javaBuilderFlags, deps android.Paths,
Colin Cross66548102018-06-19 22:47:35 -0700255 intermediatesDir, desc string) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700256
Colin Cross59149b62017-10-16 18:07:29 -0700257 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700258
259 var bootClasspath string
260 if flags.javaVersion == "1.9" {
261 deps = append(deps, flags.systemModules...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700262 bootClasspath = flags.systemModules.FormJavaSystemModulesPath("--system=", ctx.Device())
Colin Cross1369cdb2017-09-29 17:58:17 -0700263 } else {
264 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700265 if len(flags.bootClasspath) == 0 && ctx.Device() {
266 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
267 // ensure java does not fall back to the default bootclasspath.
268 bootClasspath = `-bootclasspath ""`
269 } else {
270 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
271 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700272 }
273
Colin Cross6ade34f2017-09-15 13:00:47 -0700274 deps = append(deps, flags.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700275 deps = append(deps, flags.processorPath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700276
Nan Zhang61eaedb2017-11-02 13:28:15 -0700277 srcJarDir := "srcjars"
278 outDir := "classes"
279 annoDir := "anno"
280 if shardIdx >= 0 {
281 shardDir := "shard" + strconv.Itoa(shardIdx)
282 srcJarDir = filepath.Join(shardDir, srcJarDir)
283 outDir = filepath.Join(shardDir, outDir)
284 annoDir = filepath.Join(shardDir, annoDir)
285 }
Colin Crossae887032017-10-23 17:16:14 -0700286 ctx.Build(pctx, android.BuildParams{
Colin Cross66548102018-06-19 22:47:35 -0700287 Rule: javac,
Colin Cross070879e2017-10-11 11:21:07 -0700288 Description: desc,
289 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700290 Inputs: srcFiles,
291 Implicits: deps,
292 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700293 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700294 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700295 "classpath": flags.classpath.FormJavaClassPath("-classpath"),
Colin Cross6a77c982018-06-19 22:43:34 -0700296 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
Colin Cross8eadbf02017-10-24 17:46:00 -0700297 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700298 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
299 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
300 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross8eadbf02017-10-24 17:46:00 -0700301 "javaVersion": flags.javaVersion,
Colin Crossc6bbef32017-08-14 14:16:06 -0700302 },
303 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700304}
305
Colin Crosse9a275b2017-10-16 17:09:48 -0700306func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
307 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700308
Colin Crossae887032017-10-23 17:16:14 -0700309 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700310 Rule: jar,
311 Description: "jar",
312 Output: outputFile,
313 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700314 Args: map[string]string{
Colin Cross0a6e0072017-08-30 14:24:55 -0700315 "jarArgs": strings.Join(jarArgs, " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700316 },
317 })
Colin Cross2fe66872015-03-30 17:20:39 -0700318}
319
Nan Zhanged19fc32017-10-19 13:06:22 -0700320func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
Colin Cross37f6d792018-07-12 12:28:41 -0700321 jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
322 dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700323
Colin Cross635acc92017-09-12 22:50:46 -0700324 var deps android.Paths
325
326 var jarArgs []string
327 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700328 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700329 deps = append(deps, manifest.Path())
330 }
331
Colin Cross37f6d792018-07-12 12:28:41 -0700332 for _, dir := range dirsToStrip {
333 jarArgs = append(jarArgs, "-stripDir ", dir)
334 }
335
336 for _, file := range filesToStrip {
337 jarArgs = append(jarArgs, "-stripFile ", file)
Nan Zhanged19fc32017-10-19 13:06:22 -0700338 }
339
Colin Cross7b60cdd2017-12-21 13:52:58 -0800340 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
341 // for downstream tools like desugar.
342 jarArgs = append(jarArgs, "-stripFile module-info.class")
343
Colin Cross37f6d792018-07-12 12:28:41 -0700344 if stripDirEntries {
Colin Cross635acc92017-09-12 22:50:46 -0700345 jarArgs = append(jarArgs, "-D")
346 }
347
Colin Crossae887032017-10-23 17:16:14 -0700348 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700349 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700350 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700351 Output: outputFile,
352 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700353 Implicits: deps,
354 Args: map[string]string{
355 "jarArgs": strings.Join(jarArgs, " "),
356 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700357 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700358}
359
Colin Crosse9a275b2017-10-16 17:09:48 -0700360func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
361 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700362 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700363 Rule: jarjar,
364 Description: "jarjar",
365 Output: outputFile,
366 Input: classesJar,
367 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700368 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700369 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700370 },
371 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700372}
Colin Cross6ade34f2017-09-15 13:00:47 -0700373
374type classpath []android.Path
375
Nan Zhanged19fc32017-10-19 13:06:22 -0700376func (x *classpath) FormJavaClassPath(optName string) string {
Colin Cross59149b62017-10-16 18:07:29 -0700377 if len(*x) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700378 return optName + " " + strings.Join(x.Strings(), ":")
Colin Cross6ade34f2017-09-15 13:00:47 -0700379 } else {
380 return ""
381 }
382}
383
Colin Cross1369cdb2017-09-29 17:58:17 -0700384// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
385// returns --system=none if the list is empty to ensure javac does not fall back to the default
386// system modules.
Nan Zhanged19fc32017-10-19 13:06:22 -0700387func (x *classpath) FormJavaSystemModulesPath(optName string, forceEmpty bool) string {
Colin Cross1369cdb2017-09-29 17:58:17 -0700388 if len(*x) > 1 {
389 panic("more than one system module")
390 } else if len(*x) == 1 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700391 return optName + strings.TrimSuffix((*x)[0].String(), "lib/modules")
Colin Cross1369cdb2017-09-29 17:58:17 -0700392 } else if forceEmpty {
Nan Zhanged19fc32017-10-19 13:06:22 -0700393 return optName + "none"
Colin Cross1369cdb2017-09-29 17:58:17 -0700394 } else {
395 return ""
396 }
397}
398
Colin Crossbafb8972018-06-06 21:46:32 +0000399func (x *classpath) FormTurbineClasspath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700400 if x == nil || *x == nil {
401 return nil
402 }
403 flags := make([]string, len(*x))
404 for i, v := range *x {
Nan Zhanged19fc32017-10-19 13:06:22 -0700405 flags[i] = optName + " " + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700406 }
407
408 return flags
409}
410
Colin Cross6ade34f2017-09-15 13:00:47 -0700411// Convert a classpath to an android.Paths
412func (x *classpath) Paths() android.Paths {
413 return append(android.Paths(nil), (*x)...)
414}
415
416func (x *classpath) Strings() []string {
417 if x == nil {
418 return nil
419 }
420 ret := make([]string, len(*x))
421 for i, path := range *x {
422 ret[i] = path.String()
423 }
424 return ret
425}