blob: e1a912b2f46597b32c3e513f1f0301e797b9aa12 [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 ` +
Sasha Smundak26c6d9e2019-06-11 13:30:13 -070046 `${config.SoongJavacWrapper} ${config.JavacWrapper}${config.JavacCmd} ` +
47 `${config.JavacHeapFlags} ${config.JavacVmFlags} ${config.CommonJdkFlags} ` +
Colin Crossbe9cdb82019-01-21 21:37:16 -080048 `$processorpath $processor $javacFlags $bootClasspath $classpath ` +
Colin Cross64162712017-08-08 13:17:59 -070049 `-source $javaVersion -target $javaVersion ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090050 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list ; fi ) && ` +
Colin Cross44c29a82019-01-24 16:36:57 -080051 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir && ` +
52 `rm -rf "$srcJarDir"`,
Colin Cross8eadbf02017-10-24 17:46:00 -070053 CommandDeps: []string{
54 "${config.JavacCmd}",
55 "${config.SoongZipCmd}",
Colin Cross436b7652018-03-15 16:24:10 -070056 "${config.ZipSyncCmd}",
Colin Cross8eadbf02017-10-24 17:46:00 -070057 },
Colin Crossa4820652017-10-17 13:56:52 -070058 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
59 Rspfile: "$out.rsp",
60 RspfileContent: "$in",
Colin Cross2fe66872015-03-30 17:20:39 -070061 },
Colin Crossbe9cdb82019-01-21 21:37:16 -080062 "javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
Colin Cross8eadbf02017-10-24 17:46:00 -070063 "outDir", "annoDir", "javaVersion")
Colin Cross2fe66872015-03-30 17:20:39 -070064
Nan Zhanged19fc32017-10-19 13:06:22 -070065 turbine = pctx.AndroidStaticRule("turbine",
66 blueprint.RuleParams{
Colin Cross6981f652018-03-07 15:14:50 -080067 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Sasha Smundak26c6d9e2019-06-11 13:30:13 -070068 `${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.TurbineJar} --output $out.tmp ` +
Colin Cross6981f652018-03-07 15:14:50 -080069 `--temp_dir "$outDir" --sources @$out.rsp --source_jars $srcJars ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070070 `--javacopts ${config.CommonJdkFlags} ` +
Colin Cross924a0aa2018-03-07 10:51:05 -080071 `$javacFlags -source $javaVersion -target $javaVersion -- $bootClasspath $classpath && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070072 `${config.Ziptime} $out.tmp && ` +
73 `(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`,
Colin Cross8eadbf02017-10-24 17:46:00 -070074 CommandDeps: []string{
75 "${config.TurbineJar}",
76 "${config.JavaCmd}",
77 "${config.Ziptime}",
Colin Cross8eadbf02017-10-24 17:46:00 -070078 },
Nan Zhanged19fc32017-10-19 13:06:22 -070079 Rspfile: "$out.rsp",
80 RspfileContent: "$in",
81 Restat: true,
82 },
Colin Cross6981f652018-03-07 15:14:50 -080083 "javacFlags", "bootClasspath", "classpath", "srcJars", "outDir", "javaVersion")
Nan Zhanged19fc32017-10-19 13:06:22 -070084
Colin Cross9d45bb72016-08-29 16:14:13 -070085 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -070086 blueprint.RuleParams{
Nan Zhang674dd932018-01-26 18:30:36 -080087 Command: `${config.SoongZipCmd} -jar -o $out @$out.rsp`,
88 CommandDeps: []string{"${config.SoongZipCmd}"},
89 Rspfile: "$out.rsp",
90 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -070091 },
Colin Cross0a6e0072017-08-30 14:24:55 -070092 "jarArgs")
93
Colin Crossa4f08812018-10-02 22:03:40 -070094 zip = pctx.AndroidStaticRule("zip",
95 blueprint.RuleParams{
96 Command: `${config.SoongZipCmd} -o $out @$out.rsp`,
97 CommandDeps: []string{"${config.SoongZipCmd}"},
98 Rspfile: "$out.rsp",
99 RspfileContent: "$jarArgs",
100 },
101 "jarArgs")
102
Colin Cross0a6e0072017-08-30 14:24:55 -0700103 combineJar = pctx.AndroidStaticRule("combineJar",
104 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800105 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700106 CommandDeps: []string{"${config.MergeZipsCmd}"},
107 },
Colin Cross635acc92017-09-12 22:50:46 -0700108 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700109
Colin Cross9d45bb72016-08-29 16:14:13 -0700110 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700111 blueprint.RuleParams{
Sasha Smundak26c6d9e2019-06-11 13:30:13 -0700112 Command: "${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JarjarCmd} process $rulesFile $in $out",
Colin Cross64162712017-08-08 13:17:59 -0700113 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700114 },
115 "rulesFile")
Nan Zhang4c819fb2018-08-27 18:31:46 -0700116
Vladimir Marko0975ee02019-04-02 10:29:55 +0100117 packageCheck = pctx.AndroidStaticRule("packageCheck",
118 blueprint.RuleParams{
119 Command: "rm -f $out && " +
120 "${config.PackageCheckCmd} $in $packages && " +
121 "touch $out",
122 CommandDeps: []string{"${config.PackageCheckCmd}"},
123 },
124 "packages")
125
Nan Zhang4c819fb2018-08-27 18:31:46 -0700126 jetifier = pctx.AndroidStaticRule("jetifier",
127 blueprint.RuleParams{
Sasha Smundak26c6d9e2019-06-11 13:30:13 -0700128 Command: "${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JetifierJar} -l error -o $out -i $in",
Nan Zhang4c819fb2018-08-27 18:31:46 -0700129 CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"},
130 },
131 )
Colin Cross43f08db2018-11-12 10:13:39 -0800132
133 zipalign = pctx.AndroidStaticRule("zipalign",
134 blueprint.RuleParams{
Colin Crosse4246ab2019-02-05 21:55:21 -0800135 Command: "if ! ${config.ZipAlign} -c -p 4 $in > /dev/null; then " +
136 "${config.ZipAlign} -f -p 4 $in $out; " +
Colin Cross43f08db2018-11-12 10:13:39 -0800137 "else " +
138 "cp -f $in $out; " +
139 "fi",
140 CommandDeps: []string{"${config.ZipAlign}"},
141 },
142 )
Colin Cross2fe66872015-03-30 17:20:39 -0700143)
144
145func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700146 pctx.Import("android/soong/android")
Colin Cross64162712017-08-08 13:17:59 -0700147 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700148}
149
150type javaBuilderFlags struct {
151 javacFlags string
Colin Cross6ade34f2017-09-15 13:00:47 -0700152 bootClasspath classpath
153 classpath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700154 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800155 processor string
Colin Cross1369cdb2017-09-29 17:58:17 -0700156 systemModules classpath
Colin Crossc0b06f12015-04-08 13:03:43 -0700157 aidlFlags string
Colin Cross3047fa22019-04-18 10:56:44 -0700158 aidlDeps android.Paths
Colin Cross64162712017-08-08 13:17:59 -0700159 javaVersion string
Colin Cross6af17aa2017-09-20 12:59:05 -0700160
Andreas Gampef3e5b552018-01-22 21:27:21 -0800161 errorProneExtraJavacFlags string
Colin Cross66548102018-06-19 22:47:35 -0700162 errorProneProcessorPath classpath
Andreas Gampef3e5b552018-01-22 21:27:21 -0800163
Colin Cross93e85952017-08-15 13:34:18 -0700164 kotlincFlags string
165 kotlincClasspath classpath
166
Colin Cross19878da2019-03-28 14:45:07 -0700167 proto android.ProtoFlags
Colin Cross2fe66872015-03-30 17:20:39 -0700168}
169
Nan Zhang61eaedb2017-11-02 13:28:15 -0700170func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
171 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700172
Nan Zhang61eaedb2017-11-02 13:28:15 -0700173 // Compile java sources into .class files
174 desc := "javac"
175 if shardIdx >= 0 {
176 desc += strconv.Itoa(shardIdx)
177 }
178
Colin Cross66548102018-06-19 22:47:35 -0700179 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc)
Colin Cross2fe66872015-03-30 17:20:39 -0700180}
181
Colin Crosse9a275b2017-10-16 17:09:48 -0700182func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700183 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700184
Colin Cross66548102018-06-19 22:47:35 -0700185 flags.processorPath = append(flags.errorProneProcessorPath, flags.processorPath...)
Colin Crossfee57cb2017-09-05 13:16:45 -0700186
Andreas Gampef3e5b552018-01-22 21:27:21 -0800187 if len(flags.errorProneExtraJavacFlags) > 0 {
188 if len(flags.javacFlags) > 0 {
Colin Cross66548102018-06-19 22:47:35 -0700189 flags.javacFlags += " " + flags.errorProneExtraJavacFlags
Andreas Gampef3e5b552018-01-22 21:27:21 -0800190 } else {
191 flags.javacFlags = flags.errorProneExtraJavacFlags
192 }
193 }
194
Nan Zhang61eaedb2017-11-02 13:28:15 -0700195 transformJavaToClasses(ctx, outputFile, -1, srcFiles, srcJars, flags, nil,
Colin Cross66548102018-06-19 22:47:35 -0700196 "errorprone", "errorprone")
Colin Cross070879e2017-10-11 11:21:07 -0700197}
198
Nan Zhanged19fc32017-10-19 13:06:22 -0700199func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700200 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700201
202 var deps android.Paths
203 deps = append(deps, srcJars...)
204 deps = append(deps, flags.bootClasspath...)
205 deps = append(deps, flags.classpath...)
206
207 var bootClasspath string
208 if len(flags.bootClasspath) == 0 && ctx.Device() {
209 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
210 // ensure java does not fall back to the default bootclasspath.
211 bootClasspath = `--bootclasspath ""`
212 } else {
Colin Crossafbb1732019-01-17 15:42:52 -0800213 bootClasspath = strings.Join(flags.bootClasspath.FormTurbineClasspath("--bootclasspath "), " ")
Nan Zhanged19fc32017-10-19 13:06:22 -0700214 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700215
Colin Crossae887032017-10-23 17:16:14 -0700216 ctx.Build(pctx, android.BuildParams{
Nan Zhanged19fc32017-10-19 13:06:22 -0700217 Rule: turbine,
218 Description: "turbine",
219 Output: outputFile,
220 Inputs: srcFiles,
221 Implicits: deps,
222 Args: map[string]string{
223 "javacFlags": flags.javacFlags,
224 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700225 "srcJars": strings.Join(srcJars.Strings(), " "),
Colin Crossafbb1732019-01-17 15:42:52 -0800226 "classpath": strings.Join(flags.classpath.FormTurbineClasspath("--classpath "), " "),
Nan Zhanged19fc32017-10-19 13:06:22 -0700227 "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
228 "javaVersion": flags.javaVersion,
229 },
230 })
231}
232
Colin Cross070879e2017-10-11 11:21:07 -0700233// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700234// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
235// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700236//
237// This method may be used for different compilers, including javac and Error Prone. The rule
238// argument specifies which command line to use and desc sets the description of the rule that will
239// be printed at build time. The stem argument provides the file name of the output jar, and
240// suffix will be appended to various intermediate files and directories to avoid collisions when
241// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700242func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700243 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700244 flags javaBuilderFlags, deps android.Paths,
Colin Cross66548102018-06-19 22:47:35 -0700245 intermediatesDir, desc string) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700246
Colin Cross59149b62017-10-16 18:07:29 -0700247 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700248
249 var bootClasspath string
250 if flags.javaVersion == "1.9" {
251 deps = append(deps, flags.systemModules...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700252 bootClasspath = flags.systemModules.FormJavaSystemModulesPath("--system=", ctx.Device())
Colin Cross1369cdb2017-09-29 17:58:17 -0700253 } else {
254 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700255 if len(flags.bootClasspath) == 0 && ctx.Device() {
256 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
257 // ensure java does not fall back to the default bootclasspath.
258 bootClasspath = `-bootclasspath ""`
259 } else {
260 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
261 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700262 }
263
Colin Cross6ade34f2017-09-15 13:00:47 -0700264 deps = append(deps, flags.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700265 deps = append(deps, flags.processorPath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700266
Colin Cross7788c122019-01-23 16:14:02 -0800267 processor := "-proc:none"
Colin Crossbe9cdb82019-01-21 21:37:16 -0800268 if flags.processor != "" {
269 processor = "-processor " + flags.processor
270 }
271
Nan Zhang61eaedb2017-11-02 13:28:15 -0700272 srcJarDir := "srcjars"
273 outDir := "classes"
274 annoDir := "anno"
275 if shardIdx >= 0 {
276 shardDir := "shard" + strconv.Itoa(shardIdx)
277 srcJarDir = filepath.Join(shardDir, srcJarDir)
278 outDir = filepath.Join(shardDir, outDir)
279 annoDir = filepath.Join(shardDir, annoDir)
280 }
Colin Crossae887032017-10-23 17:16:14 -0700281 ctx.Build(pctx, android.BuildParams{
Colin Cross66548102018-06-19 22:47:35 -0700282 Rule: javac,
Colin Cross070879e2017-10-11 11:21:07 -0700283 Description: desc,
284 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700285 Inputs: srcFiles,
286 Implicits: deps,
287 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700288 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700289 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700290 "classpath": flags.classpath.FormJavaClassPath("-classpath"),
Colin Cross6a77c982018-06-19 22:43:34 -0700291 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
Colin Crossbe9cdb82019-01-21 21:37:16 -0800292 "processor": processor,
Colin Cross8eadbf02017-10-24 17:46:00 -0700293 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700294 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
295 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
296 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross8eadbf02017-10-24 17:46:00 -0700297 "javaVersion": flags.javaVersion,
Colin Crossc6bbef32017-08-14 14:16:06 -0700298 },
299 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700300}
301
Colin Crosse9a275b2017-10-16 17:09:48 -0700302func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
303 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700304
Colin Crossae887032017-10-23 17:16:14 -0700305 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700306 Rule: jar,
307 Description: "jar",
308 Output: outputFile,
309 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700310 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -0800311 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700312 },
313 })
Colin Cross2fe66872015-03-30 17:20:39 -0700314}
315
Nan Zhanged19fc32017-10-19 13:06:22 -0700316func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
Colin Cross37f6d792018-07-12 12:28:41 -0700317 jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
318 dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700319
Colin Cross635acc92017-09-12 22:50:46 -0700320 var deps android.Paths
321
322 var jarArgs []string
323 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700324 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700325 deps = append(deps, manifest.Path())
326 }
327
Colin Cross37f6d792018-07-12 12:28:41 -0700328 for _, dir := range dirsToStrip {
329 jarArgs = append(jarArgs, "-stripDir ", dir)
330 }
331
332 for _, file := range filesToStrip {
333 jarArgs = append(jarArgs, "-stripFile ", file)
Nan Zhanged19fc32017-10-19 13:06:22 -0700334 }
335
Colin Cross7b60cdd2017-12-21 13:52:58 -0800336 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
337 // for downstream tools like desugar.
338 jarArgs = append(jarArgs, "-stripFile module-info.class")
339
Colin Cross37f6d792018-07-12 12:28:41 -0700340 if stripDirEntries {
Colin Cross635acc92017-09-12 22:50:46 -0700341 jarArgs = append(jarArgs, "-D")
342 }
343
Colin Crossae887032017-10-23 17:16:14 -0700344 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700345 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700346 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700347 Output: outputFile,
348 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700349 Implicits: deps,
350 Args: map[string]string{
351 "jarArgs": strings.Join(jarArgs, " "),
352 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700353 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700354}
355
Colin Crosse9a275b2017-10-16 17:09:48 -0700356func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
357 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700358 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700359 Rule: jarjar,
360 Description: "jarjar",
361 Output: outputFile,
362 Input: classesJar,
363 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700364 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700365 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700366 },
367 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700368}
Colin Cross6ade34f2017-09-15 13:00:47 -0700369
Vladimir Marko0975ee02019-04-02 10:29:55 +0100370func CheckJarPackages(ctx android.ModuleContext, outputFile android.WritablePath,
371 classesJar android.Path, permittedPackages []string) {
372 ctx.Build(pctx, android.BuildParams{
373 Rule: packageCheck,
374 Description: "packageCheck",
375 Output: outputFile,
376 Input: classesJar,
377 Args: map[string]string{
378 "packages": strings.Join(permittedPackages, " "),
379 },
380 })
381}
382
Nan Zhang4c819fb2018-08-27 18:31:46 -0700383func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath,
384 inputFile android.Path) {
385 ctx.Build(pctx, android.BuildParams{
386 Rule: jetifier,
387 Description: "jetifier",
388 Output: outputFile,
389 Input: inputFile,
390 })
391}
392
Colin Cross094054a2018-10-17 15:10:48 -0700393func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) {
394 ctx.Build(pctx, android.BuildParams{
395 Rule: android.WriteFile,
396 Description: "manifest",
397 Output: outputFile,
398 Args: map[string]string{
399 "content": "Main-Class: " + mainClass + "\n",
400 },
401 })
402}
403
Colin Cross43f08db2018-11-12 10:13:39 -0800404func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path) {
405 ctx.Build(pctx, android.BuildParams{
406 Rule: zipalign,
407 Description: "align",
408 Input: inputFile,
409 Output: outputFile,
410 })
411}
412
Colin Cross6ade34f2017-09-15 13:00:47 -0700413type classpath []android.Path
414
Nan Zhanged19fc32017-10-19 13:06:22 -0700415func (x *classpath) FormJavaClassPath(optName string) string {
Colin Cross81440082018-08-15 20:21:55 -0700416 if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") {
417 optName += " "
418 }
Colin Cross59149b62017-10-16 18:07:29 -0700419 if len(*x) > 0 {
Colin Cross81440082018-08-15 20:21:55 -0700420 return optName + strings.Join(x.Strings(), ":")
Colin Cross6ade34f2017-09-15 13:00:47 -0700421 } else {
422 return ""
423 }
424}
425
Colin Cross1369cdb2017-09-29 17:58:17 -0700426// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
427// returns --system=none if the list is empty to ensure javac does not fall back to the default
428// system modules.
Nan Zhanged19fc32017-10-19 13:06:22 -0700429func (x *classpath) FormJavaSystemModulesPath(optName string, forceEmpty bool) string {
Colin Cross1369cdb2017-09-29 17:58:17 -0700430 if len(*x) > 1 {
431 panic("more than one system module")
432 } else if len(*x) == 1 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700433 return optName + strings.TrimSuffix((*x)[0].String(), "lib/modules")
Colin Cross1369cdb2017-09-29 17:58:17 -0700434 } else if forceEmpty {
Nan Zhanged19fc32017-10-19 13:06:22 -0700435 return optName + "none"
Colin Cross1369cdb2017-09-29 17:58:17 -0700436 } else {
437 return ""
438 }
439}
440
Colin Crossbafb8972018-06-06 21:46:32 +0000441func (x *classpath) FormTurbineClasspath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700442 if x == nil || *x == nil {
443 return nil
444 }
445 flags := make([]string, len(*x))
446 for i, v := range *x {
Colin Crossafbb1732019-01-17 15:42:52 -0800447 flags[i] = optName + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700448 }
449
450 return flags
451}
452
Colin Cross6ade34f2017-09-15 13:00:47 -0700453// Convert a classpath to an android.Paths
454func (x *classpath) Paths() android.Paths {
455 return append(android.Paths(nil), (*x)...)
456}
457
458func (x *classpath) Strings() []string {
459 if x == nil {
460 return nil
461 }
462 ret := make([]string, len(*x))
463 for i, path := range *x {
464 ret[i] = path.String()
465 }
466 return ret
467}