blob: ce9a5ee87a57d64e7b6e653eed3e634bb70fc66a [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 Crossbe9cdb82019-01-21 21:37:16 -080047 `$processorpath $processor $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 Cross44c29a82019-01-24 16:36:57 -080050 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir && ` +
51 `rm -rf "$srcJarDir"`,
Colin Cross8eadbf02017-10-24 17:46:00 -070052 CommandDeps: []string{
53 "${config.JavacCmd}",
54 "${config.SoongZipCmd}",
Colin Cross436b7652018-03-15 16:24:10 -070055 "${config.ZipSyncCmd}",
Colin Cross8eadbf02017-10-24 17:46:00 -070056 },
Colin Crossa4820652017-10-17 13:56:52 -070057 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
58 Rspfile: "$out.rsp",
59 RspfileContent: "$in",
Colin Cross2fe66872015-03-30 17:20:39 -070060 },
Colin Crossbe9cdb82019-01-21 21:37:16 -080061 "javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
Colin Cross8eadbf02017-10-24 17:46:00 -070062 "outDir", "annoDir", "javaVersion")
Colin Cross2fe66872015-03-30 17:20:39 -070063
Nan Zhanged19fc32017-10-19 13:06:22 -070064 turbine = pctx.AndroidStaticRule("turbine",
65 blueprint.RuleParams{
Colin Cross6981f652018-03-07 15:14:50 -080066 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070067 `${config.JavaCmd} -jar ${config.TurbineJar} --output $out.tmp ` +
Colin Cross6981f652018-03-07 15:14:50 -080068 `--temp_dir "$outDir" --sources @$out.rsp --source_jars $srcJars ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070069 `--javacopts ${config.CommonJdkFlags} ` +
Colin Cross924a0aa2018-03-07 10:51:05 -080070 `$javacFlags -source $javaVersion -target $javaVersion -- $bootClasspath $classpath && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070071 `${config.Ziptime} $out.tmp && ` +
72 `(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`,
Colin Cross8eadbf02017-10-24 17:46:00 -070073 CommandDeps: []string{
74 "${config.TurbineJar}",
75 "${config.JavaCmd}",
76 "${config.Ziptime}",
Colin Cross8eadbf02017-10-24 17:46:00 -070077 },
Nan Zhanged19fc32017-10-19 13:06:22 -070078 Rspfile: "$out.rsp",
79 RspfileContent: "$in",
80 Restat: true,
81 },
Colin Cross6981f652018-03-07 15:14:50 -080082 "javacFlags", "bootClasspath", "classpath", "srcJars", "outDir", "javaVersion")
Nan Zhanged19fc32017-10-19 13:06:22 -070083
Colin Cross9d45bb72016-08-29 16:14:13 -070084 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -070085 blueprint.RuleParams{
Nan Zhang674dd932018-01-26 18:30:36 -080086 Command: `${config.SoongZipCmd} -jar -o $out @$out.rsp`,
87 CommandDeps: []string{"${config.SoongZipCmd}"},
88 Rspfile: "$out.rsp",
89 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -070090 },
Colin Cross0a6e0072017-08-30 14:24:55 -070091 "jarArgs")
92
Colin Crossa4f08812018-10-02 22:03:40 -070093 zip = pctx.AndroidStaticRule("zip",
94 blueprint.RuleParams{
95 Command: `${config.SoongZipCmd} -o $out @$out.rsp`,
96 CommandDeps: []string{"${config.SoongZipCmd}"},
97 Rspfile: "$out.rsp",
98 RspfileContent: "$jarArgs",
99 },
100 "jarArgs")
101
Colin Cross0a6e0072017-08-30 14:24:55 -0700102 combineJar = pctx.AndroidStaticRule("combineJar",
103 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800104 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700105 CommandDeps: []string{"${config.MergeZipsCmd}"},
106 },
Colin Cross635acc92017-09-12 22:50:46 -0700107 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700108
Colin Cross9d45bb72016-08-29 16:14:13 -0700109 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700110 blueprint.RuleParams{
Colin Cross64162712017-08-08 13:17:59 -0700111 Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out",
112 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700113 },
114 "rulesFile")
Nan Zhang4c819fb2018-08-27 18:31:46 -0700115
116 jetifier = pctx.AndroidStaticRule("jetifier",
117 blueprint.RuleParams{
118 Command: "${config.JavaCmd} -jar ${config.JetifierJar} -l error -o $out -i $in",
119 CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"},
120 },
121 )
Colin Cross43f08db2018-11-12 10:13:39 -0800122
123 zipalign = pctx.AndroidStaticRule("zipalign",
124 blueprint.RuleParams{
Colin Crosse4246ab2019-02-05 21:55:21 -0800125 Command: "if ! ${config.ZipAlign} -c -p 4 $in > /dev/null; then " +
126 "${config.ZipAlign} -f -p 4 $in $out; " +
Colin Cross43f08db2018-11-12 10:13:39 -0800127 "else " +
128 "cp -f $in $out; " +
129 "fi",
130 CommandDeps: []string{"${config.ZipAlign}"},
131 },
132 )
Colin Cross2fe66872015-03-30 17:20:39 -0700133)
134
135func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700136 pctx.Import("android/soong/android")
Colin Cross64162712017-08-08 13:17:59 -0700137 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700138}
139
140type javaBuilderFlags struct {
141 javacFlags string
Colin Cross6ade34f2017-09-15 13:00:47 -0700142 bootClasspath classpath
143 classpath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700144 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800145 processor string
Colin Cross1369cdb2017-09-29 17:58:17 -0700146 systemModules classpath
Colin Crossc0b06f12015-04-08 13:03:43 -0700147 aidlFlags string
Colin Cross64162712017-08-08 13:17:59 -0700148 javaVersion string
Colin Cross6af17aa2017-09-20 12:59:05 -0700149
Andreas Gampef3e5b552018-01-22 21:27:21 -0800150 errorProneExtraJavacFlags string
Colin Cross66548102018-06-19 22:47:35 -0700151 errorProneProcessorPath classpath
Andreas Gampef3e5b552018-01-22 21:27:21 -0800152
Colin Cross93e85952017-08-15 13:34:18 -0700153 kotlincFlags string
154 kotlincClasspath classpath
155
Colin Cross19878da2019-03-28 14:45:07 -0700156 proto android.ProtoFlags
Colin Cross2fe66872015-03-30 17:20:39 -0700157}
158
Nan Zhang61eaedb2017-11-02 13:28:15 -0700159func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
160 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700161
Nan Zhang61eaedb2017-11-02 13:28:15 -0700162 // Compile java sources into .class files
163 desc := "javac"
164 if shardIdx >= 0 {
165 desc += strconv.Itoa(shardIdx)
166 }
167
Colin Cross66548102018-06-19 22:47:35 -0700168 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc)
Colin Cross2fe66872015-03-30 17:20:39 -0700169}
170
Colin Crosse9a275b2017-10-16 17:09:48 -0700171func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700172 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700173
Colin Cross66548102018-06-19 22:47:35 -0700174 flags.processorPath = append(flags.errorProneProcessorPath, flags.processorPath...)
Colin Crossfee57cb2017-09-05 13:16:45 -0700175
Andreas Gampef3e5b552018-01-22 21:27:21 -0800176 if len(flags.errorProneExtraJavacFlags) > 0 {
177 if len(flags.javacFlags) > 0 {
Colin Cross66548102018-06-19 22:47:35 -0700178 flags.javacFlags += " " + flags.errorProneExtraJavacFlags
Andreas Gampef3e5b552018-01-22 21:27:21 -0800179 } else {
180 flags.javacFlags = flags.errorProneExtraJavacFlags
181 }
182 }
183
Nan Zhang61eaedb2017-11-02 13:28:15 -0700184 transformJavaToClasses(ctx, outputFile, -1, srcFiles, srcJars, flags, nil,
Colin Cross66548102018-06-19 22:47:35 -0700185 "errorprone", "errorprone")
Colin Cross070879e2017-10-11 11:21:07 -0700186}
187
Nan Zhanged19fc32017-10-19 13:06:22 -0700188func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700189 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700190
191 var deps android.Paths
192 deps = append(deps, srcJars...)
193 deps = append(deps, flags.bootClasspath...)
194 deps = append(deps, flags.classpath...)
195
196 var bootClasspath string
197 if len(flags.bootClasspath) == 0 && ctx.Device() {
198 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
199 // ensure java does not fall back to the default bootclasspath.
200 bootClasspath = `--bootclasspath ""`
201 } else {
Colin Crossafbb1732019-01-17 15:42:52 -0800202 bootClasspath = strings.Join(flags.bootClasspath.FormTurbineClasspath("--bootclasspath "), " ")
Nan Zhanged19fc32017-10-19 13:06:22 -0700203 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700204
Colin Crossae887032017-10-23 17:16:14 -0700205 ctx.Build(pctx, android.BuildParams{
Nan Zhanged19fc32017-10-19 13:06:22 -0700206 Rule: turbine,
207 Description: "turbine",
208 Output: outputFile,
209 Inputs: srcFiles,
210 Implicits: deps,
211 Args: map[string]string{
212 "javacFlags": flags.javacFlags,
213 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700214 "srcJars": strings.Join(srcJars.Strings(), " "),
Colin Crossafbb1732019-01-17 15:42:52 -0800215 "classpath": strings.Join(flags.classpath.FormTurbineClasspath("--classpath "), " "),
Nan Zhanged19fc32017-10-19 13:06:22 -0700216 "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
217 "javaVersion": flags.javaVersion,
218 },
219 })
220}
221
Colin Cross070879e2017-10-11 11:21:07 -0700222// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700223// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
224// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700225//
226// This method may be used for different compilers, including javac and Error Prone. The rule
227// argument specifies which command line to use and desc sets the description of the rule that will
228// be printed at build time. The stem argument provides the file name of the output jar, and
229// suffix will be appended to various intermediate files and directories to avoid collisions when
230// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700231func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700232 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700233 flags javaBuilderFlags, deps android.Paths,
Colin Cross66548102018-06-19 22:47:35 -0700234 intermediatesDir, desc string) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700235
Colin Cross59149b62017-10-16 18:07:29 -0700236 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700237
238 var bootClasspath string
239 if flags.javaVersion == "1.9" {
240 deps = append(deps, flags.systemModules...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700241 bootClasspath = flags.systemModules.FormJavaSystemModulesPath("--system=", ctx.Device())
Colin Cross1369cdb2017-09-29 17:58:17 -0700242 } else {
243 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700244 if len(flags.bootClasspath) == 0 && ctx.Device() {
245 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
246 // ensure java does not fall back to the default bootclasspath.
247 bootClasspath = `-bootclasspath ""`
248 } else {
249 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
250 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700251 }
252
Colin Cross6ade34f2017-09-15 13:00:47 -0700253 deps = append(deps, flags.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700254 deps = append(deps, flags.processorPath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700255
Colin Cross7788c122019-01-23 16:14:02 -0800256 processor := "-proc:none"
Colin Crossbe9cdb82019-01-21 21:37:16 -0800257 if flags.processor != "" {
258 processor = "-processor " + flags.processor
259 }
260
Nan Zhang61eaedb2017-11-02 13:28:15 -0700261 srcJarDir := "srcjars"
262 outDir := "classes"
263 annoDir := "anno"
264 if shardIdx >= 0 {
265 shardDir := "shard" + strconv.Itoa(shardIdx)
266 srcJarDir = filepath.Join(shardDir, srcJarDir)
267 outDir = filepath.Join(shardDir, outDir)
268 annoDir = filepath.Join(shardDir, annoDir)
269 }
Colin Crossae887032017-10-23 17:16:14 -0700270 ctx.Build(pctx, android.BuildParams{
Colin Cross66548102018-06-19 22:47:35 -0700271 Rule: javac,
Colin Cross070879e2017-10-11 11:21:07 -0700272 Description: desc,
273 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700274 Inputs: srcFiles,
275 Implicits: deps,
276 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700277 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700278 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700279 "classpath": flags.classpath.FormJavaClassPath("-classpath"),
Colin Cross6a77c982018-06-19 22:43:34 -0700280 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
Colin Crossbe9cdb82019-01-21 21:37:16 -0800281 "processor": processor,
Colin Cross8eadbf02017-10-24 17:46:00 -0700282 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700283 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
284 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
285 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross8eadbf02017-10-24 17:46:00 -0700286 "javaVersion": flags.javaVersion,
Colin Crossc6bbef32017-08-14 14:16:06 -0700287 },
288 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700289}
290
Colin Crosse9a275b2017-10-16 17:09:48 -0700291func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
292 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700293
Colin Crossae887032017-10-23 17:16:14 -0700294 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700295 Rule: jar,
296 Description: "jar",
297 Output: outputFile,
298 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700299 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -0800300 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700301 },
302 })
Colin Cross2fe66872015-03-30 17:20:39 -0700303}
304
Nan Zhanged19fc32017-10-19 13:06:22 -0700305func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
Colin Cross37f6d792018-07-12 12:28:41 -0700306 jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
307 dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700308
Colin Cross635acc92017-09-12 22:50:46 -0700309 var deps android.Paths
310
311 var jarArgs []string
312 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700313 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700314 deps = append(deps, manifest.Path())
315 }
316
Colin Cross37f6d792018-07-12 12:28:41 -0700317 for _, dir := range dirsToStrip {
318 jarArgs = append(jarArgs, "-stripDir ", dir)
319 }
320
321 for _, file := range filesToStrip {
322 jarArgs = append(jarArgs, "-stripFile ", file)
Nan Zhanged19fc32017-10-19 13:06:22 -0700323 }
324
Colin Cross7b60cdd2017-12-21 13:52:58 -0800325 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
326 // for downstream tools like desugar.
327 jarArgs = append(jarArgs, "-stripFile module-info.class")
328
Colin Cross37f6d792018-07-12 12:28:41 -0700329 if stripDirEntries {
Colin Cross635acc92017-09-12 22:50:46 -0700330 jarArgs = append(jarArgs, "-D")
331 }
332
Colin Crossae887032017-10-23 17:16:14 -0700333 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700334 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700335 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700336 Output: outputFile,
337 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700338 Implicits: deps,
339 Args: map[string]string{
340 "jarArgs": strings.Join(jarArgs, " "),
341 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700342 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700343}
344
Colin Crosse9a275b2017-10-16 17:09:48 -0700345func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
346 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700347 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700348 Rule: jarjar,
349 Description: "jarjar",
350 Output: outputFile,
351 Input: classesJar,
352 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700353 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700354 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700355 },
356 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700357}
Colin Cross6ade34f2017-09-15 13:00:47 -0700358
Nan Zhang4c819fb2018-08-27 18:31:46 -0700359func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath,
360 inputFile android.Path) {
361 ctx.Build(pctx, android.BuildParams{
362 Rule: jetifier,
363 Description: "jetifier",
364 Output: outputFile,
365 Input: inputFile,
366 })
367}
368
Colin Cross094054a2018-10-17 15:10:48 -0700369func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) {
370 ctx.Build(pctx, android.BuildParams{
371 Rule: android.WriteFile,
372 Description: "manifest",
373 Output: outputFile,
374 Args: map[string]string{
375 "content": "Main-Class: " + mainClass + "\n",
376 },
377 })
378}
379
Colin Cross43f08db2018-11-12 10:13:39 -0800380func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path) {
381 ctx.Build(pctx, android.BuildParams{
382 Rule: zipalign,
383 Description: "align",
384 Input: inputFile,
385 Output: outputFile,
386 })
387}
388
Colin Cross6ade34f2017-09-15 13:00:47 -0700389type classpath []android.Path
390
Nan Zhanged19fc32017-10-19 13:06:22 -0700391func (x *classpath) FormJavaClassPath(optName string) string {
Colin Cross81440082018-08-15 20:21:55 -0700392 if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") {
393 optName += " "
394 }
Colin Cross59149b62017-10-16 18:07:29 -0700395 if len(*x) > 0 {
Colin Cross81440082018-08-15 20:21:55 -0700396 return optName + strings.Join(x.Strings(), ":")
Colin Cross6ade34f2017-09-15 13:00:47 -0700397 } else {
398 return ""
399 }
400}
401
Colin Cross1369cdb2017-09-29 17:58:17 -0700402// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
403// returns --system=none if the list is empty to ensure javac does not fall back to the default
404// system modules.
Nan Zhanged19fc32017-10-19 13:06:22 -0700405func (x *classpath) FormJavaSystemModulesPath(optName string, forceEmpty bool) string {
Colin Cross1369cdb2017-09-29 17:58:17 -0700406 if len(*x) > 1 {
407 panic("more than one system module")
408 } else if len(*x) == 1 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700409 return optName + strings.TrimSuffix((*x)[0].String(), "lib/modules")
Colin Cross1369cdb2017-09-29 17:58:17 -0700410 } else if forceEmpty {
Nan Zhanged19fc32017-10-19 13:06:22 -0700411 return optName + "none"
Colin Cross1369cdb2017-09-29 17:58:17 -0700412 } else {
413 return ""
414 }
415}
416
Colin Crossbafb8972018-06-06 21:46:32 +0000417func (x *classpath) FormTurbineClasspath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700418 if x == nil || *x == nil {
419 return nil
420 }
421 flags := make([]string, len(*x))
422 for i, v := range *x {
Colin Crossafbb1732019-01-17 15:42:52 -0800423 flags[i] = optName + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700424 }
425
426 return flags
427}
428
Colin Cross6ade34f2017-09-15 13:00:47 -0700429// Convert a classpath to an android.Paths
430func (x *classpath) Paths() android.Paths {
431 return append(android.Paths(nil), (*x)...)
432}
433
434func (x *classpath) Strings() []string {
435 if x == nil {
436 return nil
437 }
438 ret := make([]string, len(*x))
439 for i, path := range *x {
440 ret[i] = path.String()
441 }
442 return ret
443}