blob: df17f7b95bad862f2cc15e4de15d40d600fa8772 [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 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 Crossbe9cdb82019-01-21 21:37:16 -080060 "javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
Colin Cross8eadbf02017-10-24 17:46:00 -070061 "outDir", "annoDir", "javaVersion")
Colin Cross2fe66872015-03-30 17:20:39 -070062
Nan Zhanged19fc32017-10-19 13:06:22 -070063 turbine = pctx.AndroidStaticRule("turbine",
64 blueprint.RuleParams{
Colin Cross6981f652018-03-07 15:14:50 -080065 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070066 `${config.JavaCmd} -jar ${config.TurbineJar} --output $out.tmp ` +
Colin Cross6981f652018-03-07 15:14:50 -080067 `--temp_dir "$outDir" --sources @$out.rsp --source_jars $srcJars ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070068 `--javacopts ${config.CommonJdkFlags} ` +
Colin Cross924a0aa2018-03-07 10:51:05 -080069 `$javacFlags -source $javaVersion -target $javaVersion -- $bootClasspath $classpath && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070070 `${config.Ziptime} $out.tmp && ` +
71 `(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`,
Colin Cross8eadbf02017-10-24 17:46:00 -070072 CommandDeps: []string{
73 "${config.TurbineJar}",
74 "${config.JavaCmd}",
75 "${config.Ziptime}",
Colin Cross8eadbf02017-10-24 17:46:00 -070076 },
Nan Zhanged19fc32017-10-19 13:06:22 -070077 Rspfile: "$out.rsp",
78 RspfileContent: "$in",
79 Restat: true,
80 },
Colin Cross6981f652018-03-07 15:14:50 -080081 "javacFlags", "bootClasspath", "classpath", "srcJars", "outDir", "javaVersion")
Nan Zhanged19fc32017-10-19 13:06:22 -070082
Colin Cross9d45bb72016-08-29 16:14:13 -070083 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -070084 blueprint.RuleParams{
Nan Zhang674dd932018-01-26 18:30:36 -080085 Command: `${config.SoongZipCmd} -jar -o $out @$out.rsp`,
86 CommandDeps: []string{"${config.SoongZipCmd}"},
87 Rspfile: "$out.rsp",
88 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -070089 },
Colin Cross0a6e0072017-08-30 14:24:55 -070090 "jarArgs")
91
Colin Crossa4f08812018-10-02 22:03:40 -070092 zip = pctx.AndroidStaticRule("zip",
93 blueprint.RuleParams{
94 Command: `${config.SoongZipCmd} -o $out @$out.rsp`,
95 CommandDeps: []string{"${config.SoongZipCmd}"},
96 Rspfile: "$out.rsp",
97 RspfileContent: "$jarArgs",
98 },
99 "jarArgs")
100
Colin Cross0a6e0072017-08-30 14:24:55 -0700101 combineJar = pctx.AndroidStaticRule("combineJar",
102 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800103 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700104 CommandDeps: []string{"${config.MergeZipsCmd}"},
105 },
Colin Cross635acc92017-09-12 22:50:46 -0700106 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700107
Colin Cross9d45bb72016-08-29 16:14:13 -0700108 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700109 blueprint.RuleParams{
Colin Cross64162712017-08-08 13:17:59 -0700110 Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out",
111 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700112 },
113 "rulesFile")
Nan Zhang4c819fb2018-08-27 18:31:46 -0700114
115 jetifier = pctx.AndroidStaticRule("jetifier",
116 blueprint.RuleParams{
117 Command: "${config.JavaCmd} -jar ${config.JetifierJar} -l error -o $out -i $in",
118 CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"},
119 },
120 )
Colin Cross43f08db2018-11-12 10:13:39 -0800121
122 zipalign = pctx.AndroidStaticRule("zipalign",
123 blueprint.RuleParams{
124 Command: "if ! ${config.ZipAlign} -c 4 $in > /dev/null; then " +
125 "${config.ZipAlign} -f 4 $in $out; " +
126 "else " +
127 "cp -f $in $out; " +
128 "fi",
129 CommandDeps: []string{"${config.ZipAlign}"},
130 },
131 )
Colin Cross2fe66872015-03-30 17:20:39 -0700132)
133
134func init() {
Colin Cross3063b782018-08-15 11:19:12 -0700135 pctx.Import("android/soong/common")
Colin Cross64162712017-08-08 13:17:59 -0700136 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700137}
138
139type javaBuilderFlags struct {
140 javacFlags string
Colin Cross6ade34f2017-09-15 13:00:47 -0700141 bootClasspath classpath
142 classpath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700143 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800144 processor string
Colin Cross1369cdb2017-09-29 17:58:17 -0700145 systemModules classpath
Colin Crossc0b06f12015-04-08 13:03:43 -0700146 aidlFlags string
Colin Cross64162712017-08-08 13:17:59 -0700147 javaVersion string
Colin Cross6af17aa2017-09-20 12:59:05 -0700148
Andreas Gampef3e5b552018-01-22 21:27:21 -0800149 errorProneExtraJavacFlags string
Colin Cross66548102018-06-19 22:47:35 -0700150 errorProneProcessorPath classpath
Andreas Gampef3e5b552018-01-22 21:27:21 -0800151
Colin Cross93e85952017-08-15 13:34:18 -0700152 kotlincFlags string
153 kotlincClasspath classpath
154
Joe Onorato09e94ab2017-11-18 18:23:14 -0800155 protoFlags []string
156 protoOutTypeFlag string // The flag itself: --java_out
157 protoOutParams string // Parameters to that flag: --java_out=$protoOutParams:$outDir
Dan Willemsenab9f4262018-02-14 13:58:34 -0800158 protoRoot bool
Colin Cross2fe66872015-03-30 17:20:39 -0700159}
160
Nan Zhang61eaedb2017-11-02 13:28:15 -0700161func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
162 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700163
Nan Zhang61eaedb2017-11-02 13:28:15 -0700164 // Compile java sources into .class files
165 desc := "javac"
166 if shardIdx >= 0 {
167 desc += strconv.Itoa(shardIdx)
168 }
169
Colin Cross66548102018-06-19 22:47:35 -0700170 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc)
Colin Cross2fe66872015-03-30 17:20:39 -0700171}
172
Colin Crosse9a275b2017-10-16 17:09:48 -0700173func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700174 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700175
Colin Cross66548102018-06-19 22:47:35 -0700176 flags.processorPath = append(flags.errorProneProcessorPath, flags.processorPath...)
Colin Crossfee57cb2017-09-05 13:16:45 -0700177
Andreas Gampef3e5b552018-01-22 21:27:21 -0800178 if len(flags.errorProneExtraJavacFlags) > 0 {
179 if len(flags.javacFlags) > 0 {
Colin Cross66548102018-06-19 22:47:35 -0700180 flags.javacFlags += " " + flags.errorProneExtraJavacFlags
Andreas Gampef3e5b552018-01-22 21:27:21 -0800181 } else {
182 flags.javacFlags = flags.errorProneExtraJavacFlags
183 }
184 }
185
Nan Zhang61eaedb2017-11-02 13:28:15 -0700186 transformJavaToClasses(ctx, outputFile, -1, srcFiles, srcJars, flags, nil,
Colin Cross66548102018-06-19 22:47:35 -0700187 "errorprone", "errorprone")
Colin Cross070879e2017-10-11 11:21:07 -0700188}
189
Nan Zhanged19fc32017-10-19 13:06:22 -0700190func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700191 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700192
193 var deps android.Paths
194 deps = append(deps, srcJars...)
195 deps = append(deps, flags.bootClasspath...)
196 deps = append(deps, flags.classpath...)
197
198 var bootClasspath string
199 if len(flags.bootClasspath) == 0 && ctx.Device() {
200 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
201 // ensure java does not fall back to the default bootclasspath.
202 bootClasspath = `--bootclasspath ""`
203 } else {
Colin Crossafbb1732019-01-17 15:42:52 -0800204 bootClasspath = strings.Join(flags.bootClasspath.FormTurbineClasspath("--bootclasspath "), " ")
Nan Zhanged19fc32017-10-19 13:06:22 -0700205 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700206
Colin Crossae887032017-10-23 17:16:14 -0700207 ctx.Build(pctx, android.BuildParams{
Nan Zhanged19fc32017-10-19 13:06:22 -0700208 Rule: turbine,
209 Description: "turbine",
210 Output: outputFile,
211 Inputs: srcFiles,
212 Implicits: deps,
213 Args: map[string]string{
214 "javacFlags": flags.javacFlags,
215 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700216 "srcJars": strings.Join(srcJars.Strings(), " "),
Colin Crossafbb1732019-01-17 15:42:52 -0800217 "classpath": strings.Join(flags.classpath.FormTurbineClasspath("--classpath "), " "),
Nan Zhanged19fc32017-10-19 13:06:22 -0700218 "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
219 "javaVersion": flags.javaVersion,
220 },
221 })
222}
223
Colin Cross070879e2017-10-11 11:21:07 -0700224// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700225// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
226// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700227//
228// This method may be used for different compilers, including javac and Error Prone. The rule
229// argument specifies which command line to use and desc sets the description of the rule that will
230// be printed at build time. The stem argument provides the file name of the output jar, and
231// suffix will be appended to various intermediate files and directories to avoid collisions when
232// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700233func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700234 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700235 flags javaBuilderFlags, deps android.Paths,
Colin Cross66548102018-06-19 22:47:35 -0700236 intermediatesDir, desc string) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700237
Colin Cross59149b62017-10-16 18:07:29 -0700238 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700239
240 var bootClasspath string
241 if flags.javaVersion == "1.9" {
242 deps = append(deps, flags.systemModules...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700243 bootClasspath = flags.systemModules.FormJavaSystemModulesPath("--system=", ctx.Device())
Colin Cross1369cdb2017-09-29 17:58:17 -0700244 } else {
245 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700246 if len(flags.bootClasspath) == 0 && ctx.Device() {
247 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
248 // ensure java does not fall back to the default bootclasspath.
249 bootClasspath = `-bootclasspath ""`
250 } else {
251 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
252 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700253 }
254
Colin Cross6ade34f2017-09-15 13:00:47 -0700255 deps = append(deps, flags.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700256 deps = append(deps, flags.processorPath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700257
Colin Cross7788c122019-01-23 16:14:02 -0800258 processor := "-proc:none"
Colin Crossbe9cdb82019-01-21 21:37:16 -0800259 if flags.processor != "" {
260 processor = "-processor " + flags.processor
261 }
262
Nan Zhang61eaedb2017-11-02 13:28:15 -0700263 srcJarDir := "srcjars"
264 outDir := "classes"
265 annoDir := "anno"
266 if shardIdx >= 0 {
267 shardDir := "shard" + strconv.Itoa(shardIdx)
268 srcJarDir = filepath.Join(shardDir, srcJarDir)
269 outDir = filepath.Join(shardDir, outDir)
270 annoDir = filepath.Join(shardDir, annoDir)
271 }
Colin Crossae887032017-10-23 17:16:14 -0700272 ctx.Build(pctx, android.BuildParams{
Colin Cross66548102018-06-19 22:47:35 -0700273 Rule: javac,
Colin Cross070879e2017-10-11 11:21:07 -0700274 Description: desc,
275 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700276 Inputs: srcFiles,
277 Implicits: deps,
278 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700279 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700280 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700281 "classpath": flags.classpath.FormJavaClassPath("-classpath"),
Colin Cross6a77c982018-06-19 22:43:34 -0700282 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
Colin Crossbe9cdb82019-01-21 21:37:16 -0800283 "processor": processor,
Colin Cross8eadbf02017-10-24 17:46:00 -0700284 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700285 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
286 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
287 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross8eadbf02017-10-24 17:46:00 -0700288 "javaVersion": flags.javaVersion,
Colin Crossc6bbef32017-08-14 14:16:06 -0700289 },
290 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700291}
292
Colin Crosse9a275b2017-10-16 17:09:48 -0700293func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
294 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700295
Colin Crossae887032017-10-23 17:16:14 -0700296 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700297 Rule: jar,
298 Description: "jar",
299 Output: outputFile,
300 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700301 Args: map[string]string{
Colin Cross1d98ee22018-09-18 17:05:15 -0700302 "jarArgs": strings.Join(proptools.NinjaAndShellEscape(jarArgs), " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700303 },
304 })
Colin Cross2fe66872015-03-30 17:20:39 -0700305}
306
Nan Zhanged19fc32017-10-19 13:06:22 -0700307func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
Colin Cross37f6d792018-07-12 12:28:41 -0700308 jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
309 dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700310
Colin Cross635acc92017-09-12 22:50:46 -0700311 var deps android.Paths
312
313 var jarArgs []string
314 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700315 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700316 deps = append(deps, manifest.Path())
317 }
318
Colin Cross37f6d792018-07-12 12:28:41 -0700319 for _, dir := range dirsToStrip {
320 jarArgs = append(jarArgs, "-stripDir ", dir)
321 }
322
323 for _, file := range filesToStrip {
324 jarArgs = append(jarArgs, "-stripFile ", file)
Nan Zhanged19fc32017-10-19 13:06:22 -0700325 }
326
Colin Cross7b60cdd2017-12-21 13:52:58 -0800327 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
328 // for downstream tools like desugar.
329 jarArgs = append(jarArgs, "-stripFile module-info.class")
330
Colin Cross37f6d792018-07-12 12:28:41 -0700331 if stripDirEntries {
Colin Cross635acc92017-09-12 22:50:46 -0700332 jarArgs = append(jarArgs, "-D")
333 }
334
Colin Crossae887032017-10-23 17:16:14 -0700335 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700336 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700337 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700338 Output: outputFile,
339 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700340 Implicits: deps,
341 Args: map[string]string{
342 "jarArgs": strings.Join(jarArgs, " "),
343 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700344 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700345}
346
Colin Crosse9a275b2017-10-16 17:09:48 -0700347func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
348 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700349 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700350 Rule: jarjar,
351 Description: "jarjar",
352 Output: outputFile,
353 Input: classesJar,
354 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700355 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700356 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700357 },
358 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700359}
Colin Cross6ade34f2017-09-15 13:00:47 -0700360
Nan Zhang4c819fb2018-08-27 18:31:46 -0700361func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath,
362 inputFile android.Path) {
363 ctx.Build(pctx, android.BuildParams{
364 Rule: jetifier,
365 Description: "jetifier",
366 Output: outputFile,
367 Input: inputFile,
368 })
369}
370
Colin Cross094054a2018-10-17 15:10:48 -0700371func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) {
372 ctx.Build(pctx, android.BuildParams{
373 Rule: android.WriteFile,
374 Description: "manifest",
375 Output: outputFile,
376 Args: map[string]string{
377 "content": "Main-Class: " + mainClass + "\n",
378 },
379 })
380}
381
Colin Cross43f08db2018-11-12 10:13:39 -0800382func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path) {
383 ctx.Build(pctx, android.BuildParams{
384 Rule: zipalign,
385 Description: "align",
386 Input: inputFile,
387 Output: outputFile,
388 })
389}
390
Colin Cross6ade34f2017-09-15 13:00:47 -0700391type classpath []android.Path
392
Nan Zhanged19fc32017-10-19 13:06:22 -0700393func (x *classpath) FormJavaClassPath(optName string) string {
Colin Cross81440082018-08-15 20:21:55 -0700394 if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") {
395 optName += " "
396 }
Colin Cross59149b62017-10-16 18:07:29 -0700397 if len(*x) > 0 {
Colin Cross81440082018-08-15 20:21:55 -0700398 return optName + strings.Join(x.Strings(), ":")
Colin Cross6ade34f2017-09-15 13:00:47 -0700399 } else {
400 return ""
401 }
402}
403
Colin Cross1369cdb2017-09-29 17:58:17 -0700404// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
405// returns --system=none if the list is empty to ensure javac does not fall back to the default
406// system modules.
Nan Zhanged19fc32017-10-19 13:06:22 -0700407func (x *classpath) FormJavaSystemModulesPath(optName string, forceEmpty bool) string {
Colin Cross1369cdb2017-09-29 17:58:17 -0700408 if len(*x) > 1 {
409 panic("more than one system module")
410 } else if len(*x) == 1 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700411 return optName + strings.TrimSuffix((*x)[0].String(), "lib/modules")
Colin Cross1369cdb2017-09-29 17:58:17 -0700412 } else if forceEmpty {
Nan Zhanged19fc32017-10-19 13:06:22 -0700413 return optName + "none"
Colin Cross1369cdb2017-09-29 17:58:17 -0700414 } else {
415 return ""
416 }
417}
418
Colin Crossbafb8972018-06-06 21:46:32 +0000419func (x *classpath) FormTurbineClasspath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700420 if x == nil || *x == nil {
421 return nil
422 }
423 flags := make([]string, len(*x))
424 for i, v := range *x {
Colin Crossafbb1732019-01-17 15:42:52 -0800425 flags[i] = optName + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700426 }
427
428 return flags
429}
430
Colin Cross6ade34f2017-09-15 13:00:47 -0700431// Convert a classpath to an android.Paths
432func (x *classpath) Paths() android.Paths {
433 return append(android.Paths(nil), (*x)...)
434}
435
436func (x *classpath) Strings() []string {
437 if x == nil {
438 return nil
439 }
440 ret := make([]string, len(*x))
441 for i, path := range *x {
442 ret[i] = path.String()
443 }
444 return ret
445}