blob: 2fe4d8ab389d00b1885a85cab4fec27d98b0c9e0 [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
Sasha Smundak85390232020-04-23 09:49:59 -070064 extractMatchingApks = pctx.StaticRule(
65 "extractMatchingApks",
66 blueprint.RuleParams{
67 Command: `rm -rf "$out" && ` +
68 `${config.ExtractApksCmd} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
69 `-sdk-version=${sdk-version} -abis=${abis} ` +
70 `--screen-densities=${screen-densities} --stem=${stem} ` +
Jaewoong Jung9cd42162020-06-29 19:18:44 -070071 `-apkcerts=${apkcerts} -partition=${partition} ` +
Sasha Smundak85390232020-04-23 09:49:59 -070072 `${in}`,
73 CommandDeps: []string{"${config.ExtractApksCmd}"},
74 },
Jaewoong Jung9cd42162020-06-29 19:18:44 -070075 "abis", "allow-prereleased", "screen-densities", "sdk-version", "stem", "apkcerts", "partition")
Sasha Smundak85390232020-04-23 09:49:59 -070076
Nan Zhanged19fc32017-10-19 13:06:22 -070077 turbine = pctx.AndroidStaticRule("turbine",
78 blueprint.RuleParams{
Colin Cross6981f652018-03-07 15:14:50 -080079 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070080 `${config.JavaCmd} -jar ${config.TurbineJar} --output $out.tmp ` +
Colin Cross6981f652018-03-07 15:14:50 -080081 `--temp_dir "$outDir" --sources @$out.rsp --source_jars $srcJars ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070082 `--javacopts ${config.CommonJdkFlags} ` +
Colin Cross924a0aa2018-03-07 10:51:05 -080083 `$javacFlags -source $javaVersion -target $javaVersion -- $bootClasspath $classpath && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -070084 `${config.Ziptime} $out.tmp && ` +
85 `(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`,
Colin Cross8eadbf02017-10-24 17:46:00 -070086 CommandDeps: []string{
87 "${config.TurbineJar}",
88 "${config.JavaCmd}",
89 "${config.Ziptime}",
Colin Cross8eadbf02017-10-24 17:46:00 -070090 },
Nan Zhanged19fc32017-10-19 13:06:22 -070091 Rspfile: "$out.rsp",
92 RspfileContent: "$in",
93 Restat: true,
94 },
Colin Cross6981f652018-03-07 15:14:50 -080095 "javacFlags", "bootClasspath", "classpath", "srcJars", "outDir", "javaVersion")
Nan Zhanged19fc32017-10-19 13:06:22 -070096
Colin Cross9d45bb72016-08-29 16:14:13 -070097 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -070098 blueprint.RuleParams{
Nan Zhang674dd932018-01-26 18:30:36 -080099 Command: `${config.SoongZipCmd} -jar -o $out @$out.rsp`,
100 CommandDeps: []string{"${config.SoongZipCmd}"},
101 Rspfile: "$out.rsp",
102 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -0700103 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700104 "jarArgs")
105
Colin Crossa4f08812018-10-02 22:03:40 -0700106 zip = pctx.AndroidStaticRule("zip",
107 blueprint.RuleParams{
108 Command: `${config.SoongZipCmd} -o $out @$out.rsp`,
109 CommandDeps: []string{"${config.SoongZipCmd}"},
110 Rspfile: "$out.rsp",
111 RspfileContent: "$jarArgs",
112 },
113 "jarArgs")
114
Colin Cross0a6e0072017-08-30 14:24:55 -0700115 combineJar = pctx.AndroidStaticRule("combineJar",
116 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800117 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700118 CommandDeps: []string{"${config.MergeZipsCmd}"},
119 },
Colin Cross635acc92017-09-12 22:50:46 -0700120 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700121
Colin Cross9d45bb72016-08-29 16:14:13 -0700122 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700123 blueprint.RuleParams{
Colin Cross64162712017-08-08 13:17:59 -0700124 Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out",
125 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700126 },
127 "rulesFile")
Nan Zhang4c819fb2018-08-27 18:31:46 -0700128
Vladimir Markoe26f4a52019-04-02 10:29:55 +0100129 packageCheck = pctx.AndroidStaticRule("packageCheck",
130 blueprint.RuleParams{
131 Command: "rm -f $out && " +
132 "${config.PackageCheckCmd} $in $packages && " +
133 "touch $out",
134 CommandDeps: []string{"${config.PackageCheckCmd}"},
135 },
136 "packages")
137
Nan Zhang4c819fb2018-08-27 18:31:46 -0700138 jetifier = pctx.AndroidStaticRule("jetifier",
139 blueprint.RuleParams{
140 Command: "${config.JavaCmd} -jar ${config.JetifierJar} -l error -o $out -i $in",
141 CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"},
142 },
143 )
Colin Cross43f08db2018-11-12 10:13:39 -0800144
145 zipalign = pctx.AndroidStaticRule("zipalign",
146 blueprint.RuleParams{
Colin Crosse4246ab2019-02-05 21:55:21 -0800147 Command: "if ! ${config.ZipAlign} -c -p 4 $in > /dev/null; then " +
148 "${config.ZipAlign} -f -p 4 $in $out; " +
Colin Cross43f08db2018-11-12 10:13:39 -0800149 "else " +
150 "cp -f $in $out; " +
151 "fi",
152 CommandDeps: []string{"${config.ZipAlign}"},
153 },
154 )
Colin Cross2fe66872015-03-30 17:20:39 -0700155)
156
157func init() {
Colin Cross713ef2b2019-04-02 16:14:11 -0700158 pctx.Import("android/soong/android")
Colin Cross64162712017-08-08 13:17:59 -0700159 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700160}
161
162type javaBuilderFlags struct {
163 javacFlags string
Colin Cross6ade34f2017-09-15 13:00:47 -0700164 bootClasspath classpath
165 classpath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700166 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800167 processor string
Colin Cross1369cdb2017-09-29 17:58:17 -0700168 systemModules classpath
Colin Crossc0b06f12015-04-08 13:03:43 -0700169 aidlFlags string
Colin Cross9bdfaf02019-04-18 10:56:44 -0700170 aidlDeps android.Paths
Colin Cross64162712017-08-08 13:17:59 -0700171 javaVersion string
Colin Cross6af17aa2017-09-20 12:59:05 -0700172
Andreas Gampef3e5b552018-01-22 21:27:21 -0800173 errorProneExtraJavacFlags string
Colin Cross66548102018-06-19 22:47:35 -0700174 errorProneProcessorPath classpath
Andreas Gampef3e5b552018-01-22 21:27:21 -0800175
Colin Cross93e85952017-08-15 13:34:18 -0700176 kotlincFlags string
177 kotlincClasspath classpath
178
Colin Cross19878da2019-03-28 14:45:07 -0700179 proto android.ProtoFlags
Colin Cross2fe66872015-03-30 17:20:39 -0700180}
181
Nan Zhang61eaedb2017-11-02 13:28:15 -0700182func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
183 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700184
Nan Zhang61eaedb2017-11-02 13:28:15 -0700185 // Compile java sources into .class files
186 desc := "javac"
187 if shardIdx >= 0 {
188 desc += strconv.Itoa(shardIdx)
189 }
190
Colin Cross66548102018-06-19 22:47:35 -0700191 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc)
Colin Cross2fe66872015-03-30 17:20:39 -0700192}
193
Colin Crosse9a275b2017-10-16 17:09:48 -0700194func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700195 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700196
Colin Cross66548102018-06-19 22:47:35 -0700197 flags.processorPath = append(flags.errorProneProcessorPath, flags.processorPath...)
Colin Crossfee57cb2017-09-05 13:16:45 -0700198
Andreas Gampef3e5b552018-01-22 21:27:21 -0800199 if len(flags.errorProneExtraJavacFlags) > 0 {
200 if len(flags.javacFlags) > 0 {
Colin Cross66548102018-06-19 22:47:35 -0700201 flags.javacFlags += " " + flags.errorProneExtraJavacFlags
Andreas Gampef3e5b552018-01-22 21:27:21 -0800202 } else {
203 flags.javacFlags = flags.errorProneExtraJavacFlags
204 }
205 }
206
Nan Zhang61eaedb2017-11-02 13:28:15 -0700207 transformJavaToClasses(ctx, outputFile, -1, srcFiles, srcJars, flags, nil,
Colin Cross66548102018-06-19 22:47:35 -0700208 "errorprone", "errorprone")
Colin Cross070879e2017-10-11 11:21:07 -0700209}
210
Nan Zhanged19fc32017-10-19 13:06:22 -0700211func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700212 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700213
214 var deps android.Paths
215 deps = append(deps, srcJars...)
216 deps = append(deps, flags.bootClasspath...)
217 deps = append(deps, flags.classpath...)
218
219 var bootClasspath string
220 if len(flags.bootClasspath) == 0 && ctx.Device() {
221 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
222 // ensure java does not fall back to the default bootclasspath.
223 bootClasspath = `--bootclasspath ""`
224 } else {
Colin Crossafbb1732019-01-17 15:42:52 -0800225 bootClasspath = strings.Join(flags.bootClasspath.FormTurbineClasspath("--bootclasspath "), " ")
Nan Zhanged19fc32017-10-19 13:06:22 -0700226 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700227
Colin Crossae887032017-10-23 17:16:14 -0700228 ctx.Build(pctx, android.BuildParams{
Nan Zhanged19fc32017-10-19 13:06:22 -0700229 Rule: turbine,
230 Description: "turbine",
231 Output: outputFile,
232 Inputs: srcFiles,
233 Implicits: deps,
234 Args: map[string]string{
235 "javacFlags": flags.javacFlags,
236 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700237 "srcJars": strings.Join(srcJars.Strings(), " "),
Colin Crossafbb1732019-01-17 15:42:52 -0800238 "classpath": strings.Join(flags.classpath.FormTurbineClasspath("--classpath "), " "),
Nan Zhanged19fc32017-10-19 13:06:22 -0700239 "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
240 "javaVersion": flags.javaVersion,
241 },
242 })
243}
244
Colin Cross070879e2017-10-11 11:21:07 -0700245// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700246// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
247// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700248//
249// This method may be used for different compilers, including javac and Error Prone. The rule
250// argument specifies which command line to use and desc sets the description of the rule that will
251// be printed at build time. The stem argument provides the file name of the output jar, and
252// suffix will be appended to various intermediate files and directories to avoid collisions when
253// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700254func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700255 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700256 flags javaBuilderFlags, deps android.Paths,
Colin Cross66548102018-06-19 22:47:35 -0700257 intermediatesDir, desc string) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700258
Colin Cross59149b62017-10-16 18:07:29 -0700259 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700260
261 var bootClasspath string
262 if flags.javaVersion == "1.9" {
263 deps = append(deps, flags.systemModules...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700264 bootClasspath = flags.systemModules.FormJavaSystemModulesPath("--system=", ctx.Device())
Colin Cross1369cdb2017-09-29 17:58:17 -0700265 } else {
266 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700267 if len(flags.bootClasspath) == 0 && ctx.Device() {
268 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
269 // ensure java does not fall back to the default bootclasspath.
270 bootClasspath = `-bootclasspath ""`
271 } else {
272 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
273 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700274 }
275
Colin Cross6ade34f2017-09-15 13:00:47 -0700276 deps = append(deps, flags.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700277 deps = append(deps, flags.processorPath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700278
Colin Cross7788c122019-01-23 16:14:02 -0800279 processor := "-proc:none"
Colin Crossbe9cdb82019-01-21 21:37:16 -0800280 if flags.processor != "" {
281 processor = "-processor " + flags.processor
282 }
283
Nan Zhang61eaedb2017-11-02 13:28:15 -0700284 srcJarDir := "srcjars"
285 outDir := "classes"
286 annoDir := "anno"
287 if shardIdx >= 0 {
288 shardDir := "shard" + strconv.Itoa(shardIdx)
289 srcJarDir = filepath.Join(shardDir, srcJarDir)
290 outDir = filepath.Join(shardDir, outDir)
291 annoDir = filepath.Join(shardDir, annoDir)
292 }
Colin Crossae887032017-10-23 17:16:14 -0700293 ctx.Build(pctx, android.BuildParams{
Colin Cross66548102018-06-19 22:47:35 -0700294 Rule: javac,
Colin Cross070879e2017-10-11 11:21:07 -0700295 Description: desc,
296 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700297 Inputs: srcFiles,
298 Implicits: deps,
299 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700300 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700301 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700302 "classpath": flags.classpath.FormJavaClassPath("-classpath"),
Colin Cross6a77c982018-06-19 22:43:34 -0700303 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
Colin Crossbe9cdb82019-01-21 21:37:16 -0800304 "processor": processor,
Colin Cross8eadbf02017-10-24 17:46:00 -0700305 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700306 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
307 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
308 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross8eadbf02017-10-24 17:46:00 -0700309 "javaVersion": flags.javaVersion,
Colin Crossc6bbef32017-08-14 14:16:06 -0700310 },
311 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700312}
313
Colin Crosse9a275b2017-10-16 17:09:48 -0700314func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
315 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700316
Colin Crossae887032017-10-23 17:16:14 -0700317 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700318 Rule: jar,
319 Description: "jar",
320 Output: outputFile,
321 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700322 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -0800323 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700324 },
325 })
Colin Cross2fe66872015-03-30 17:20:39 -0700326}
327
Nan Zhanged19fc32017-10-19 13:06:22 -0700328func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
Colin Cross37f6d792018-07-12 12:28:41 -0700329 jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
330 dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700331
Colin Cross635acc92017-09-12 22:50:46 -0700332 var deps android.Paths
333
334 var jarArgs []string
335 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700336 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700337 deps = append(deps, manifest.Path())
338 }
339
Colin Cross37f6d792018-07-12 12:28:41 -0700340 for _, dir := range dirsToStrip {
341 jarArgs = append(jarArgs, "-stripDir ", dir)
342 }
343
344 for _, file := range filesToStrip {
345 jarArgs = append(jarArgs, "-stripFile ", file)
Nan Zhanged19fc32017-10-19 13:06:22 -0700346 }
347
Colin Cross7b60cdd2017-12-21 13:52:58 -0800348 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
349 // for downstream tools like desugar.
350 jarArgs = append(jarArgs, "-stripFile module-info.class")
351
Colin Cross37f6d792018-07-12 12:28:41 -0700352 if stripDirEntries {
Colin Cross635acc92017-09-12 22:50:46 -0700353 jarArgs = append(jarArgs, "-D")
354 }
355
Colin Crossae887032017-10-23 17:16:14 -0700356 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700357 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700358 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700359 Output: outputFile,
360 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700361 Implicits: deps,
362 Args: map[string]string{
363 "jarArgs": strings.Join(jarArgs, " "),
364 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700365 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700366}
367
Colin Crosse9a275b2017-10-16 17:09:48 -0700368func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
369 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700370 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700371 Rule: jarjar,
372 Description: "jarjar",
373 Output: outputFile,
374 Input: classesJar,
375 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700376 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700377 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700378 },
379 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700380}
Colin Cross6ade34f2017-09-15 13:00:47 -0700381
Vladimir Markoe26f4a52019-04-02 10:29:55 +0100382func CheckJarPackages(ctx android.ModuleContext, outputFile android.WritablePath,
383 classesJar android.Path, permittedPackages []string) {
384 ctx.Build(pctx, android.BuildParams{
385 Rule: packageCheck,
386 Description: "packageCheck",
387 Output: outputFile,
388 Input: classesJar,
389 Args: map[string]string{
390 "packages": strings.Join(permittedPackages, " "),
391 },
392 })
393}
394
Nan Zhang4c819fb2018-08-27 18:31:46 -0700395func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath,
396 inputFile android.Path) {
397 ctx.Build(pctx, android.BuildParams{
398 Rule: jetifier,
399 Description: "jetifier",
400 Output: outputFile,
401 Input: inputFile,
402 })
403}
404
Colin Cross094054a2018-10-17 15:10:48 -0700405func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) {
406 ctx.Build(pctx, android.BuildParams{
407 Rule: android.WriteFile,
408 Description: "manifest",
409 Output: outputFile,
410 Args: map[string]string{
411 "content": "Main-Class: " + mainClass + "\n",
412 },
413 })
414}
415
Colin Cross43f08db2018-11-12 10:13:39 -0800416func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path) {
417 ctx.Build(pctx, android.BuildParams{
418 Rule: zipalign,
419 Description: "align",
420 Input: inputFile,
421 Output: outputFile,
422 })
423}
424
Colin Cross6ade34f2017-09-15 13:00:47 -0700425type classpath []android.Path
426
Nan Zhanged19fc32017-10-19 13:06:22 -0700427func (x *classpath) FormJavaClassPath(optName string) string {
Colin Cross81440082018-08-15 20:21:55 -0700428 if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") {
429 optName += " "
430 }
Colin Cross59149b62017-10-16 18:07:29 -0700431 if len(*x) > 0 {
Colin Cross81440082018-08-15 20:21:55 -0700432 return optName + strings.Join(x.Strings(), ":")
Colin Cross6ade34f2017-09-15 13:00:47 -0700433 } else {
434 return ""
435 }
436}
437
Colin Cross1369cdb2017-09-29 17:58:17 -0700438// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
439// returns --system=none if the list is empty to ensure javac does not fall back to the default
440// system modules.
Nan Zhanged19fc32017-10-19 13:06:22 -0700441func (x *classpath) FormJavaSystemModulesPath(optName string, forceEmpty bool) string {
Colin Cross1369cdb2017-09-29 17:58:17 -0700442 if len(*x) > 1 {
443 panic("more than one system module")
444 } else if len(*x) == 1 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700445 return optName + strings.TrimSuffix((*x)[0].String(), "lib/modules")
Colin Cross1369cdb2017-09-29 17:58:17 -0700446 } else if forceEmpty {
Nan Zhanged19fc32017-10-19 13:06:22 -0700447 return optName + "none"
Colin Cross1369cdb2017-09-29 17:58:17 -0700448 } else {
449 return ""
450 }
451}
452
Colin Crossbafb8972018-06-06 21:46:32 +0000453func (x *classpath) FormTurbineClasspath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700454 if x == nil || *x == nil {
455 return nil
456 }
457 flags := make([]string, len(*x))
458 for i, v := range *x {
Colin Crossafbb1732019-01-17 15:42:52 -0800459 flags[i] = optName + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700460 }
461
462 return flags
463}
464
Colin Cross6ade34f2017-09-15 13:00:47 -0700465// Convert a classpath to an android.Paths
466func (x *classpath) Paths() android.Paths {
467 return append(android.Paths(nil), (*x)...)
468}
469
470func (x *classpath) Strings() []string {
471 if x == nil {
472 return nil
473 }
474 ret := make([]string, len(*x))
475 for i, path := range *x {
476 ret[i] = path.String()
477 }
478 return ret
479}