blob: 131f21abaac3bab4df2a824310d5d9b325a0de7d [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"
Ramy Medhatfc5a03c2020-04-21 21:36:23 -040030 "android/soong/remoteexec"
Colin Cross2fe66872015-03-30 17:20:39 -070031)
32
33var (
Colin Cross635c3b02016-05-18 15:37:25 -070034 pctx = android.NewPackageContext("android/soong/java")
Colin Cross2fe66872015-03-30 17:20:39 -070035
36 // Compiling java is not conducive to proper dependency tracking. The path-matches-class-name
37 // requirement leads to unpredictable generated source file names, and a single .java file
38 // will get compiled into multiple .class files if it contains inner classes. To work around
Colin Crossf7eac7a2018-02-08 12:48:39 -080039 // this, all java rules write into separate directories and then are combined into a .jar file
40 // (if the rule produces .class files) or a .srcjar file (if the rule produces .java files).
41 // .srcjar files are unzipped into a temporary directory when compiled with javac.
Ramy Medhatfc5a03c2020-04-21 21:36:23 -040042 // TODO(b/143658984): goma can't handle the --system argument to javac.
43 javac, javacRE = remoteexec.StaticRules(pctx, "javac",
Colin Cross2fe66872015-03-30 17:20:39 -070044 blueprint.RuleParams{
Colin Cross8eadbf02017-10-24 17:46:00 -070045 Command: `rm -rf "$outDir" "$annoDir" "$srcJarDir" && mkdir -p "$outDir" "$annoDir" "$srcJarDir" && ` +
Colin Cross436b7652018-03-15 16:24:10 -070046 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090047 `(if [ -s $srcJarDir/list ] || [ -s $out.rsp ] ; then ` +
Ramy Medhatfc5a03c2020-04-21 21:36:23 -040048 `${config.SoongJavacWrapper} $reTemplate${config.JavacCmd} ${config.JavacHeapFlags} ${config.CommonJdkFlags} ` +
Colin Crossbe9cdb82019-01-21 21:37:16 -080049 `$processorpath $processor $javacFlags $bootClasspath $classpath ` +
Colin Cross64162712017-08-08 13:17:59 -070050 `-source $javaVersion -target $javaVersion ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090051 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list ; fi ) && ` +
Colin Cross44c29a82019-01-24 16:36:57 -080052 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir && ` +
53 `rm -rf "$srcJarDir"`,
Colin Cross8eadbf02017-10-24 17:46:00 -070054 CommandDeps: []string{
55 "${config.JavacCmd}",
56 "${config.SoongZipCmd}",
Colin Cross436b7652018-03-15 16:24:10 -070057 "${config.ZipSyncCmd}",
Colin Cross8eadbf02017-10-24 17:46:00 -070058 },
Colin Crossa4820652017-10-17 13:56:52 -070059 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
60 Rspfile: "$out.rsp",
61 RspfileContent: "$in",
Ramy Medhatfc5a03c2020-04-21 21:36:23 -040062 }, &remoteexec.REParams{
63 Labels: map[string]string{"type": "compile", "lang": "java", "compiler": "javac"},
64 ExecStrategy: "${config.REJavacExecStrategy}",
65 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
66 }, []string{"javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
67 "outDir", "annoDir", "javaVersion"}, nil)
Colin Cross2fe66872015-03-30 17:20:39 -070068
Sasha Smundak85390232020-04-23 09:49:59 -070069 extractMatchingApks = pctx.StaticRule(
70 "extractMatchingApks",
71 blueprint.RuleParams{
72 Command: `rm -rf "$out" && ` +
73 `${config.ExtractApksCmd} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
74 `-sdk-version=${sdk-version} -abis=${abis} ` +
75 `--screen-densities=${screen-densities} --stem=${stem} ` +
Jaewoong Jung9cd42162020-06-29 19:18:44 -070076 `-apkcerts=${apkcerts} -partition=${partition} ` +
Sasha Smundak85390232020-04-23 09:49:59 -070077 `${in}`,
78 CommandDeps: []string{"${config.ExtractApksCmd}"},
79 },
Jaewoong Jung9cd42162020-06-29 19:18:44 -070080 "abis", "allow-prereleased", "screen-densities", "sdk-version", "stem", "apkcerts", "partition")
Sasha Smundak85390232020-04-23 09:49:59 -070081
Nan Zhanged19fc32017-10-19 13:06:22 -070082 turbine = pctx.AndroidStaticRule("turbine",
83 blueprint.RuleParams{
Colin Cross6981f652018-03-07 15:14:50 -080084 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070085 `${config.JavaCmd} -jar ${config.TurbineJar} --output $out.tmp ` +
Colin Cross6981f652018-03-07 15:14:50 -080086 `--temp_dir "$outDir" --sources @$out.rsp --source_jars $srcJars ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070087 `--javacopts ${config.CommonJdkFlags} ` +
Colin Cross924a0aa2018-03-07 10:51:05 -080088 `$javacFlags -source $javaVersion -target $javaVersion -- $bootClasspath $classpath && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070089 `${config.Ziptime} $out.tmp && ` +
90 `(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`,
Colin Cross8eadbf02017-10-24 17:46:00 -070091 CommandDeps: []string{
92 "${config.TurbineJar}",
93 "${config.JavaCmd}",
94 "${config.Ziptime}",
Colin Cross8eadbf02017-10-24 17:46:00 -070095 },
Nan Zhanged19fc32017-10-19 13:06:22 -070096 Rspfile: "$out.rsp",
97 RspfileContent: "$in",
98 Restat: true,
99 },
Colin Cross6981f652018-03-07 15:14:50 -0800100 "javacFlags", "bootClasspath", "classpath", "srcJars", "outDir", "javaVersion")
Nan Zhanged19fc32017-10-19 13:06:22 -0700101
Colin Cross9d45bb72016-08-29 16:14:13 -0700102 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -0700103 blueprint.RuleParams{
Nan Zhang674dd932018-01-26 18:30:36 -0800104 Command: `${config.SoongZipCmd} -jar -o $out @$out.rsp`,
105 CommandDeps: []string{"${config.SoongZipCmd}"},
106 Rspfile: "$out.rsp",
107 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -0700108 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700109 "jarArgs")
110
Colin Crossa4f08812018-10-02 22:03:40 -0700111 zip = pctx.AndroidStaticRule("zip",
112 blueprint.RuleParams{
113 Command: `${config.SoongZipCmd} -o $out @$out.rsp`,
114 CommandDeps: []string{"${config.SoongZipCmd}"},
115 Rspfile: "$out.rsp",
116 RspfileContent: "$jarArgs",
117 },
118 "jarArgs")
119
Colin Cross0a6e0072017-08-30 14:24:55 -0700120 combineJar = pctx.AndroidStaticRule("combineJar",
121 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800122 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700123 CommandDeps: []string{"${config.MergeZipsCmd}"},
124 },
Colin Cross635acc92017-09-12 22:50:46 -0700125 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700126
Colin Cross9d45bb72016-08-29 16:14:13 -0700127 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700128 blueprint.RuleParams{
Colin Cross64162712017-08-08 13:17:59 -0700129 Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out",
130 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700131 },
132 "rulesFile")
Nan Zhang4c819fb2018-08-27 18:31:46 -0700133
Vladimir Markoe26f4a52019-04-02 10:29:55 +0100134 packageCheck = pctx.AndroidStaticRule("packageCheck",
135 blueprint.RuleParams{
136 Command: "rm -f $out && " +
137 "${config.PackageCheckCmd} $in $packages && " +
138 "touch $out",
139 CommandDeps: []string{"${config.PackageCheckCmd}"},
140 },
141 "packages")
142
Nan Zhang4c819fb2018-08-27 18:31:46 -0700143 jetifier = pctx.AndroidStaticRule("jetifier",
144 blueprint.RuleParams{
145 Command: "${config.JavaCmd} -jar ${config.JetifierJar} -l error -o $out -i $in",
146 CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"},
147 },
148 )
Colin Cross43f08db2018-11-12 10:13:39 -0800149
150 zipalign = pctx.AndroidStaticRule("zipalign",
151 blueprint.RuleParams{
Colin Crosse4246ab2019-02-05 21:55:21 -0800152 Command: "if ! ${config.ZipAlign} -c -p 4 $in > /dev/null; then " +
153 "${config.ZipAlign} -f -p 4 $in $out; " +
Colin Cross43f08db2018-11-12 10:13:39 -0800154 "else " +
155 "cp -f $in $out; " +
156 "fi",
157 CommandDeps: []string{"${config.ZipAlign}"},
158 },
159 )
Colin Cross2fe66872015-03-30 17:20:39 -0700160)
161
162func init() {
Colin Cross713ef2b2019-04-02 16:14:11 -0700163 pctx.Import("android/soong/android")
Colin Cross64162712017-08-08 13:17:59 -0700164 pctx.Import("android/soong/java/config")
Ramy Medhatfc5a03c2020-04-21 21:36:23 -0400165 pctx.Import("android/soong/remoteexec")
Colin Cross2fe66872015-03-30 17:20:39 -0700166}
167
168type javaBuilderFlags struct {
169 javacFlags string
Colin Cross6ade34f2017-09-15 13:00:47 -0700170 bootClasspath classpath
171 classpath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700172 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800173 processor string
Colin Cross1369cdb2017-09-29 17:58:17 -0700174 systemModules classpath
Colin Crossc0b06f12015-04-08 13:03:43 -0700175 aidlFlags string
Colin Cross9bdfaf02019-04-18 10:56:44 -0700176 aidlDeps android.Paths
Colin Cross64162712017-08-08 13:17:59 -0700177 javaVersion string
Colin Cross6af17aa2017-09-20 12:59:05 -0700178
Andreas Gampef3e5b552018-01-22 21:27:21 -0800179 errorProneExtraJavacFlags string
Colin Cross66548102018-06-19 22:47:35 -0700180 errorProneProcessorPath classpath
Andreas Gampef3e5b552018-01-22 21:27:21 -0800181
Colin Cross93e85952017-08-15 13:34:18 -0700182 kotlincFlags string
183 kotlincClasspath classpath
184
Colin Cross19878da2019-03-28 14:45:07 -0700185 proto android.ProtoFlags
Colin Cross2fe66872015-03-30 17:20:39 -0700186}
187
Nan Zhang61eaedb2017-11-02 13:28:15 -0700188func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
189 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700190
Nan Zhang61eaedb2017-11-02 13:28:15 -0700191 // Compile java sources into .class files
192 desc := "javac"
193 if shardIdx >= 0 {
194 desc += strconv.Itoa(shardIdx)
195 }
196
Colin Cross66548102018-06-19 22:47:35 -0700197 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc)
Colin Cross2fe66872015-03-30 17:20:39 -0700198}
199
Colin Crosse9a275b2017-10-16 17:09:48 -0700200func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700201 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700202
Colin Cross66548102018-06-19 22:47:35 -0700203 flags.processorPath = append(flags.errorProneProcessorPath, flags.processorPath...)
Colin Crossfee57cb2017-09-05 13:16:45 -0700204
Andreas Gampef3e5b552018-01-22 21:27:21 -0800205 if len(flags.errorProneExtraJavacFlags) > 0 {
206 if len(flags.javacFlags) > 0 {
Colin Cross66548102018-06-19 22:47:35 -0700207 flags.javacFlags += " " + flags.errorProneExtraJavacFlags
Andreas Gampef3e5b552018-01-22 21:27:21 -0800208 } else {
209 flags.javacFlags = flags.errorProneExtraJavacFlags
210 }
211 }
212
Nan Zhang61eaedb2017-11-02 13:28:15 -0700213 transformJavaToClasses(ctx, outputFile, -1, srcFiles, srcJars, flags, nil,
Colin Cross66548102018-06-19 22:47:35 -0700214 "errorprone", "errorprone")
Colin Cross070879e2017-10-11 11:21:07 -0700215}
216
Nan Zhanged19fc32017-10-19 13:06:22 -0700217func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700218 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700219
220 var deps android.Paths
221 deps = append(deps, srcJars...)
222 deps = append(deps, flags.bootClasspath...)
223 deps = append(deps, flags.classpath...)
224
225 var bootClasspath string
226 if len(flags.bootClasspath) == 0 && ctx.Device() {
227 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
228 // ensure java does not fall back to the default bootclasspath.
229 bootClasspath = `--bootclasspath ""`
230 } else {
Colin Crossafbb1732019-01-17 15:42:52 -0800231 bootClasspath = strings.Join(flags.bootClasspath.FormTurbineClasspath("--bootclasspath "), " ")
Nan Zhanged19fc32017-10-19 13:06:22 -0700232 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700233
Colin Crossae887032017-10-23 17:16:14 -0700234 ctx.Build(pctx, android.BuildParams{
Nan Zhanged19fc32017-10-19 13:06:22 -0700235 Rule: turbine,
236 Description: "turbine",
237 Output: outputFile,
238 Inputs: srcFiles,
239 Implicits: deps,
240 Args: map[string]string{
241 "javacFlags": flags.javacFlags,
242 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700243 "srcJars": strings.Join(srcJars.Strings(), " "),
Colin Crossafbb1732019-01-17 15:42:52 -0800244 "classpath": strings.Join(flags.classpath.FormTurbineClasspath("--classpath "), " "),
Nan Zhanged19fc32017-10-19 13:06:22 -0700245 "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
246 "javaVersion": flags.javaVersion,
247 },
248 })
249}
250
Colin Cross070879e2017-10-11 11:21:07 -0700251// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700252// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
253// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700254//
255// This method may be used for different compilers, including javac and Error Prone. The rule
256// argument specifies which command line to use and desc sets the description of the rule that will
257// be printed at build time. The stem argument provides the file name of the output jar, and
258// suffix will be appended to various intermediate files and directories to avoid collisions when
259// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700260func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700261 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700262 flags javaBuilderFlags, deps android.Paths,
Colin Cross66548102018-06-19 22:47:35 -0700263 intermediatesDir, desc string) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700264
Colin Cross59149b62017-10-16 18:07:29 -0700265 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700266
267 var bootClasspath string
268 if flags.javaVersion == "1.9" {
269 deps = append(deps, flags.systemModules...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700270 bootClasspath = flags.systemModules.FormJavaSystemModulesPath("--system=", ctx.Device())
Colin Cross1369cdb2017-09-29 17:58:17 -0700271 } else {
272 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700273 if len(flags.bootClasspath) == 0 && ctx.Device() {
274 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
275 // ensure java does not fall back to the default bootclasspath.
276 bootClasspath = `-bootclasspath ""`
277 } else {
278 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
279 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700280 }
281
Colin Cross6ade34f2017-09-15 13:00:47 -0700282 deps = append(deps, flags.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700283 deps = append(deps, flags.processorPath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700284
Colin Cross7788c122019-01-23 16:14:02 -0800285 processor := "-proc:none"
Colin Crossbe9cdb82019-01-21 21:37:16 -0800286 if flags.processor != "" {
287 processor = "-processor " + flags.processor
288 }
289
Nan Zhang61eaedb2017-11-02 13:28:15 -0700290 srcJarDir := "srcjars"
291 outDir := "classes"
292 annoDir := "anno"
293 if shardIdx >= 0 {
294 shardDir := "shard" + strconv.Itoa(shardIdx)
295 srcJarDir = filepath.Join(shardDir, srcJarDir)
296 outDir = filepath.Join(shardDir, outDir)
297 annoDir = filepath.Join(shardDir, annoDir)
298 }
Ramy Medhatfc5a03c2020-04-21 21:36:23 -0400299 rule := javac
Kousik Kumar4e2977e2020-09-10 09:50:35 +0000300 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAVAC") {
Ramy Medhatfc5a03c2020-04-21 21:36:23 -0400301 rule = javacRE
302 }
Colin Crossae887032017-10-23 17:16:14 -0700303 ctx.Build(pctx, android.BuildParams{
Ramy Medhatfc5a03c2020-04-21 21:36:23 -0400304 Rule: rule,
Colin Cross070879e2017-10-11 11:21:07 -0700305 Description: desc,
306 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700307 Inputs: srcFiles,
308 Implicits: deps,
309 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700310 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700311 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700312 "classpath": flags.classpath.FormJavaClassPath("-classpath"),
Colin Cross6a77c982018-06-19 22:43:34 -0700313 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
Colin Crossbe9cdb82019-01-21 21:37:16 -0800314 "processor": processor,
Colin Cross8eadbf02017-10-24 17:46:00 -0700315 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700316 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
317 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
318 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross8eadbf02017-10-24 17:46:00 -0700319 "javaVersion": flags.javaVersion,
Colin Crossc6bbef32017-08-14 14:16:06 -0700320 },
321 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700322}
323
Colin Crosse9a275b2017-10-16 17:09:48 -0700324func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
325 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700326
Colin Crossae887032017-10-23 17:16:14 -0700327 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700328 Rule: jar,
329 Description: "jar",
330 Output: outputFile,
331 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700332 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -0800333 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700334 },
335 })
Colin Cross2fe66872015-03-30 17:20:39 -0700336}
337
Nan Zhanged19fc32017-10-19 13:06:22 -0700338func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
Colin Cross37f6d792018-07-12 12:28:41 -0700339 jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
340 dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700341
Colin Cross635acc92017-09-12 22:50:46 -0700342 var deps android.Paths
343
344 var jarArgs []string
345 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700346 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700347 deps = append(deps, manifest.Path())
348 }
349
Colin Cross37f6d792018-07-12 12:28:41 -0700350 for _, dir := range dirsToStrip {
351 jarArgs = append(jarArgs, "-stripDir ", dir)
352 }
353
354 for _, file := range filesToStrip {
355 jarArgs = append(jarArgs, "-stripFile ", file)
Nan Zhanged19fc32017-10-19 13:06:22 -0700356 }
357
Colin Cross7b60cdd2017-12-21 13:52:58 -0800358 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
359 // for downstream tools like desugar.
360 jarArgs = append(jarArgs, "-stripFile module-info.class")
361
Colin Cross37f6d792018-07-12 12:28:41 -0700362 if stripDirEntries {
Colin Cross635acc92017-09-12 22:50:46 -0700363 jarArgs = append(jarArgs, "-D")
364 }
365
Colin Crossae887032017-10-23 17:16:14 -0700366 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700367 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700368 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700369 Output: outputFile,
370 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700371 Implicits: deps,
372 Args: map[string]string{
373 "jarArgs": strings.Join(jarArgs, " "),
374 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700375 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700376}
377
Colin Crosse9a275b2017-10-16 17:09:48 -0700378func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
379 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700380 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700381 Rule: jarjar,
382 Description: "jarjar",
383 Output: outputFile,
384 Input: classesJar,
385 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700386 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700387 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700388 },
389 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700390}
Colin Cross6ade34f2017-09-15 13:00:47 -0700391
Vladimir Markoe26f4a52019-04-02 10:29:55 +0100392func CheckJarPackages(ctx android.ModuleContext, outputFile android.WritablePath,
393 classesJar android.Path, permittedPackages []string) {
394 ctx.Build(pctx, android.BuildParams{
395 Rule: packageCheck,
396 Description: "packageCheck",
397 Output: outputFile,
398 Input: classesJar,
399 Args: map[string]string{
400 "packages": strings.Join(permittedPackages, " "),
401 },
402 })
403}
404
Nan Zhang4c819fb2018-08-27 18:31:46 -0700405func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath,
406 inputFile android.Path) {
407 ctx.Build(pctx, android.BuildParams{
408 Rule: jetifier,
409 Description: "jetifier",
410 Output: outputFile,
411 Input: inputFile,
412 })
413}
414
Colin Cross094054a2018-10-17 15:10:48 -0700415func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) {
416 ctx.Build(pctx, android.BuildParams{
417 Rule: android.WriteFile,
418 Description: "manifest",
419 Output: outputFile,
420 Args: map[string]string{
421 "content": "Main-Class: " + mainClass + "\n",
422 },
423 })
424}
425
Colin Cross43f08db2018-11-12 10:13:39 -0800426func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path) {
427 ctx.Build(pctx, android.BuildParams{
428 Rule: zipalign,
429 Description: "align",
430 Input: inputFile,
431 Output: outputFile,
432 })
433}
434
Colin Cross6ade34f2017-09-15 13:00:47 -0700435type classpath []android.Path
436
Nan Zhanged19fc32017-10-19 13:06:22 -0700437func (x *classpath) FormJavaClassPath(optName string) string {
Colin Cross81440082018-08-15 20:21:55 -0700438 if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") {
439 optName += " "
440 }
Colin Cross59149b62017-10-16 18:07:29 -0700441 if len(*x) > 0 {
Colin Cross81440082018-08-15 20:21:55 -0700442 return optName + strings.Join(x.Strings(), ":")
Colin Cross6ade34f2017-09-15 13:00:47 -0700443 } else {
444 return ""
445 }
446}
447
Colin Cross1369cdb2017-09-29 17:58:17 -0700448// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
449// returns --system=none if the list is empty to ensure javac does not fall back to the default
450// system modules.
Nan Zhanged19fc32017-10-19 13:06:22 -0700451func (x *classpath) FormJavaSystemModulesPath(optName string, forceEmpty bool) string {
Colin Cross1369cdb2017-09-29 17:58:17 -0700452 if len(*x) > 1 {
453 panic("more than one system module")
454 } else if len(*x) == 1 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700455 return optName + strings.TrimSuffix((*x)[0].String(), "lib/modules")
Colin Cross1369cdb2017-09-29 17:58:17 -0700456 } else if forceEmpty {
Nan Zhanged19fc32017-10-19 13:06:22 -0700457 return optName + "none"
Colin Cross1369cdb2017-09-29 17:58:17 -0700458 } else {
459 return ""
460 }
461}
462
Colin Crossbafb8972018-06-06 21:46:32 +0000463func (x *classpath) FormTurbineClasspath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700464 if x == nil || *x == nil {
465 return nil
466 }
467 flags := make([]string, len(*x))
468 for i, v := range *x {
Colin Crossafbb1732019-01-17 15:42:52 -0800469 flags[i] = optName + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700470 }
471
472 return flags
473}
474
Colin Cross6ade34f2017-09-15 13:00:47 -0700475// Convert a classpath to an android.Paths
476func (x *classpath) Paths() android.Paths {
477 return append(android.Paths(nil), (*x)...)
478}
479
480func (x *classpath) Strings() []string {
481 if x == nil {
482 return nil
483 }
484 ret := make([]string, len(*x))
485 for i, path := range *x {
486 ret[i] = path.String()
487 }
488 return ret
489}