blob: 12110f2f0be2b425804aa6bbe2b703fd839accc5 [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 Crossfee57cb2017-09-05 13:16:45 -070027
28 "android/soong/android"
29 "android/soong/java/config"
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 && ` +
Colin Crossa4820652017-10-17 13:56:52 -070045 `${config.SoongJavacWrapper} ${config.JavacWrapper}${config.JavacCmd} ${config.JavacHeapFlags} ${config.CommonJdkFlags} ` +
Colin Cross8eadbf02017-10-24 17:46:00 -070046 `$javacFlags $bootClasspath $classpath ` +
Colin Cross64162712017-08-08 13:17:59 -070047 `-source $javaVersion -target $javaVersion ` +
Colin Cross8eadbf02017-10-24 17:46:00 -070048 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list && ` +
Colin Cross0a6e0072017-08-30 14:24:55 -070049 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
Colin Cross8eadbf02017-10-24 17:46:00 -070050 CommandDeps: []string{
51 "${config.JavacCmd}",
52 "${config.SoongZipCmd}",
Colin Cross436b7652018-03-15 16:24:10 -070053 "${config.ZipSyncCmd}",
Colin Cross8eadbf02017-10-24 17:46:00 -070054 },
Colin Crossa4820652017-10-17 13:56:52 -070055 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
56 Rspfile: "$out.rsp",
57 RspfileContent: "$in",
Colin Cross2fe66872015-03-30 17:20:39 -070058 },
Colin Cross8eadbf02017-10-24 17:46:00 -070059 "javacFlags", "bootClasspath", "classpath", "srcJars", "srcJarDir",
60 "outDir", "annoDir", "javaVersion")
Colin Cross2fe66872015-03-30 17:20:39 -070061
Colin Cross93e85952017-08-15 13:34:18 -070062 kotlinc = pctx.AndroidGomaStaticRule("kotlinc",
63 blueprint.RuleParams{
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +000064 Command: `rm -rf "$outDir" "$srcJarDir" && mkdir -p "$outDir" "$srcJarDir" && ` +
Colin Cross436b7652018-03-15 16:24:10 -070065 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +000066 `${config.GenKotlinBuildFileCmd} $classpath $outDir $out.rsp $srcJarDir/list > $outDir/kotlinc-build.xml &&` +
67 `${config.KotlincCmd} $kotlincFlags ` +
68 `-jvm-target $kotlinJvmTarget -Xbuild-file=$outDir/kotlinc-build.xml && ` +
Colin Cross93e85952017-08-15 13:34:18 -070069 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
70 CommandDeps: []string{
71 "${config.KotlincCmd}",
72 "${config.KotlinCompilerJar}",
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +000073 "${config.GenKotlinBuildFileCmd}",
Colin Cross93e85952017-08-15 13:34:18 -070074 "${config.SoongZipCmd}",
Colin Cross436b7652018-03-15 16:24:10 -070075 "${config.ZipSyncCmd}",
Colin Cross93e85952017-08-15 13:34:18 -070076 },
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +000077 Rspfile: "$out.rsp",
78 RspfileContent: `$in`,
Colin Cross93e85952017-08-15 13:34:18 -070079 },
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +000080 "kotlincFlags", "classpath", "srcJars", "srcJarDir", "outDir", "kotlinJvmTarget")
Colin Cross93e85952017-08-15 13:34:18 -070081
Colin Crossc6bbef32017-08-14 14:16:06 -070082 errorprone = pctx.AndroidStaticRule("errorprone",
83 blueprint.RuleParams{
Colin Cross8eadbf02017-10-24 17:46:00 -070084 Command: `rm -rf "$outDir" "$annoDir" "$srcJarDir" && mkdir -p "$outDir" "$annoDir" "$srcJarDir" && ` +
Colin Cross436b7652018-03-15 16:24:10 -070085 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
Colin Crossa4820652017-10-17 13:56:52 -070086 `${config.SoongJavacWrapper} ${config.ErrorProneCmd} ` +
Colin Cross8eadbf02017-10-24 17:46:00 -070087 `$javacFlags $bootClasspath $classpath ` +
Colin Crossc6bbef32017-08-14 14:16:06 -070088 `-source $javaVersion -target $javaVersion ` +
Colin Cross8eadbf02017-10-24 17:46:00 -070089 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list && ` +
Colin Cross0a6e0072017-08-30 14:24:55 -070090 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
Colin Crossc6bbef32017-08-14 14:16:06 -070091 CommandDeps: []string{
92 "${config.JavaCmd}",
93 "${config.ErrorProneJavacJar}",
94 "${config.ErrorProneJar}",
Colin Cross0a6e0072017-08-30 14:24:55 -070095 "${config.SoongZipCmd}",
Colin Cross436b7652018-03-15 16:24:10 -070096 "${config.ZipSyncCmd}",
Colin Crossc6bbef32017-08-14 14:16:06 -070097 },
Colin Crossa4820652017-10-17 13:56:52 -070098 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
99 Rspfile: "$out.rsp",
100 RspfileContent: "$in",
Colin Crossc6bbef32017-08-14 14:16:06 -0700101 },
Colin Cross8eadbf02017-10-24 17:46:00 -0700102 "javacFlags", "bootClasspath", "classpath", "srcJars", "srcJarDir",
103 "outDir", "annoDir", "javaVersion")
Colin Crossc6bbef32017-08-14 14:16:06 -0700104
Nan Zhanged19fc32017-10-19 13:06:22 -0700105 turbine = pctx.AndroidStaticRule("turbine",
106 blueprint.RuleParams{
Colin Cross6981f652018-03-07 15:14:50 -0800107 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700108 `${config.JavaCmd} -jar ${config.TurbineJar} --output $out.tmp ` +
Colin Cross6981f652018-03-07 15:14:50 -0800109 `--temp_dir "$outDir" --sources @$out.rsp --source_jars $srcJars ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700110 `--javacopts ${config.CommonJdkFlags} ` +
Colin Cross924a0aa2018-03-07 10:51:05 -0800111 `$javacFlags -source $javaVersion -target $javaVersion -- $bootClasspath $classpath && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700112 `${config.Ziptime} $out.tmp && ` +
113 `(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`,
Colin Cross8eadbf02017-10-24 17:46:00 -0700114 CommandDeps: []string{
115 "${config.TurbineJar}",
116 "${config.JavaCmd}",
117 "${config.Ziptime}",
Colin Cross8eadbf02017-10-24 17:46:00 -0700118 },
Nan Zhanged19fc32017-10-19 13:06:22 -0700119 Rspfile: "$out.rsp",
120 RspfileContent: "$in",
121 Restat: true,
122 },
Colin Cross6981f652018-03-07 15:14:50 -0800123 "javacFlags", "bootClasspath", "classpath", "srcJars", "outDir", "javaVersion")
Nan Zhanged19fc32017-10-19 13:06:22 -0700124
Colin Cross9d45bb72016-08-29 16:14:13 -0700125 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -0700126 blueprint.RuleParams{
Nan Zhang674dd932018-01-26 18:30:36 -0800127 Command: `${config.SoongZipCmd} -jar -o $out @$out.rsp`,
128 CommandDeps: []string{"${config.SoongZipCmd}"},
129 Rspfile: "$out.rsp",
130 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -0700131 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700132 "jarArgs")
133
134 combineJar = pctx.AndroidStaticRule("combineJar",
135 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800136 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700137 CommandDeps: []string{"${config.MergeZipsCmd}"},
138 },
Colin Cross635acc92017-09-12 22:50:46 -0700139 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700140
Colin Cross9d45bb72016-08-29 16:14:13 -0700141 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700142 blueprint.RuleParams{
Colin Cross64162712017-08-08 13:17:59 -0700143 Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out",
144 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700145 },
146 "rulesFile")
Colin Cross2fe66872015-03-30 17:20:39 -0700147)
148
149func init() {
Colin Cross64162712017-08-08 13:17:59 -0700150 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700151}
152
153type javaBuilderFlags struct {
154 javacFlags string
Colin Cross6ade34f2017-09-15 13:00:47 -0700155 bootClasspath classpath
156 classpath classpath
Colin Cross1369cdb2017-09-29 17:58:17 -0700157 systemModules classpath
Colin Crossc0b06f12015-04-08 13:03:43 -0700158 aidlFlags string
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
162
Colin Cross93e85952017-08-15 13:34:18 -0700163 kotlincFlags string
164 kotlincClasspath classpath
165
Joe Onorato09e94ab2017-11-18 18:23:14 -0800166 protoFlags []string
167 protoOutTypeFlag string // The flag itself: --java_out
168 protoOutParams string // Parameters to that flag: --java_out=$protoOutParams:$outDir
Dan Willemsenab9f4262018-02-14 13:58:34 -0800169 protoRoot bool
Colin Cross2fe66872015-03-30 17:20:39 -0700170}
171
Colin Cross93e85952017-08-15 13:34:18 -0700172func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700173 srcFiles, srcJars android.Paths,
Colin Cross93e85952017-08-15 13:34:18 -0700174 flags javaBuilderFlags) {
175
Colin Cross93e85952017-08-15 13:34:18 -0700176 inputs := append(android.Paths(nil), srcFiles...)
Colin Cross93e85952017-08-15 13:34:18 -0700177
Colin Cross9bc43432017-12-15 20:20:39 -0800178 var deps android.Paths
179 deps = append(deps, flags.kotlincClasspath...)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000180 deps = append(deps, srcJars...)
Colin Cross9bc43432017-12-15 20:20:39 -0800181
Colin Crossae887032017-10-23 17:16:14 -0700182 ctx.Build(pctx, android.BuildParams{
Colin Cross93e85952017-08-15 13:34:18 -0700183 Rule: kotlinc,
184 Description: "kotlinc",
185 Output: outputFile,
186 Inputs: inputs,
Colin Cross9bc43432017-12-15 20:20:39 -0800187 Implicits: deps,
Colin Cross93e85952017-08-15 13:34:18 -0700188 Args: map[string]string{
Colin Cross715d7112017-10-25 20:32:18 -0700189 "classpath": flags.kotlincClasspath.FormJavaClassPath("-classpath"),
Colin Cross93e85952017-08-15 13:34:18 -0700190 "kotlincFlags": flags.kotlincFlags,
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000191 "srcJars": strings.Join(srcJars.Strings(), " "),
192 "outDir": android.PathForModuleOut(ctx, "kotlinc", "classes").String(),
193 "srcJarDir": android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(),
Tobias Thierer96427d62017-11-18 03:10:51 +0000194 // http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8
195 "kotlinJvmTarget": "1.8",
Colin Cross93e85952017-08-15 13:34:18 -0700196 },
197 })
198}
199
Nan Zhang61eaedb2017-11-02 13:28:15 -0700200func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
201 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700202
Nan Zhang61eaedb2017-11-02 13:28:15 -0700203 // Compile java sources into .class files
204 desc := "javac"
205 if shardIdx >= 0 {
206 desc += strconv.Itoa(shardIdx)
207 }
208
209 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc, javac)
Colin Cross2fe66872015-03-30 17:20:39 -0700210}
211
Colin Crosse9a275b2017-10-16 17:09:48 -0700212func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700213 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700214
Colin Crossfee57cb2017-09-05 13:16:45 -0700215 if config.ErrorProneJar == "" {
216 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
Colin Crossfee57cb2017-09-05 13:16:45 -0700217 }
218
Andreas Gampef3e5b552018-01-22 21:27:21 -0800219 if len(flags.errorProneExtraJavacFlags) > 0 {
220 if len(flags.javacFlags) > 0 {
221 flags.javacFlags = flags.errorProneExtraJavacFlags + " " + flags.javacFlags
222 } else {
223 flags.javacFlags = flags.errorProneExtraJavacFlags
224 }
225 }
226
Nan Zhang61eaedb2017-11-02 13:28:15 -0700227 transformJavaToClasses(ctx, outputFile, -1, srcFiles, srcJars, flags, nil,
Colin Cross1ee23172017-10-18 14:44:18 -0700228 "errorprone", "errorprone", errorprone)
Colin Cross070879e2017-10-11 11:21:07 -0700229}
230
Nan Zhanged19fc32017-10-19 13:06:22 -0700231func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700232 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700233
234 var deps android.Paths
235 deps = append(deps, srcJars...)
236 deps = append(deps, flags.bootClasspath...)
237 deps = append(deps, flags.classpath...)
238
239 var bootClasspath string
240 if len(flags.bootClasspath) == 0 && ctx.Device() {
241 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
242 // ensure java does not fall back to the default bootclasspath.
243 bootClasspath = `--bootclasspath ""`
244 } else {
Colin Cross924a0aa2018-03-07 10:51:05 -0800245 bootClasspath = strings.Join(flags.bootClasspath.FormDesugarClasspath("--bootclasspath"), " ")
Nan Zhanged19fc32017-10-19 13:06:22 -0700246 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700247
Colin Crossae887032017-10-23 17:16:14 -0700248 ctx.Build(pctx, android.BuildParams{
Nan Zhanged19fc32017-10-19 13:06:22 -0700249 Rule: turbine,
250 Description: "turbine",
251 Output: outputFile,
252 Inputs: srcFiles,
253 Implicits: deps,
254 Args: map[string]string{
255 "javacFlags": flags.javacFlags,
256 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700257 "srcJars": strings.Join(srcJars.Strings(), " "),
Colin Cross924a0aa2018-03-07 10:51:05 -0800258 "classpath": strings.Join(flags.classpath.FormDesugarClasspath("--classpath"), " "),
Nan Zhanged19fc32017-10-19 13:06:22 -0700259 "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
260 "javaVersion": flags.javaVersion,
261 },
262 })
263}
264
Colin Cross070879e2017-10-11 11:21:07 -0700265// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700266// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
267// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700268//
269// This method may be used for different compilers, including javac and Error Prone. The rule
270// argument specifies which command line to use and desc sets the description of the rule that will
271// be printed at build time. The stem argument provides the file name of the output jar, and
272// suffix will be appended to various intermediate files and directories to avoid collisions when
273// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700274func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700275 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700276 flags javaBuilderFlags, deps android.Paths,
Colin Cross1ee23172017-10-18 14:44:18 -0700277 intermediatesDir, desc string, rule blueprint.Rule) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700278
Colin Cross59149b62017-10-16 18:07:29 -0700279 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700280
281 var bootClasspath string
282 if flags.javaVersion == "1.9" {
283 deps = append(deps, flags.systemModules...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700284 bootClasspath = flags.systemModules.FormJavaSystemModulesPath("--system=", ctx.Device())
Colin Cross1369cdb2017-09-29 17:58:17 -0700285 } else {
286 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700287 if len(flags.bootClasspath) == 0 && ctx.Device() {
288 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
289 // ensure java does not fall back to the default bootclasspath.
290 bootClasspath = `-bootclasspath ""`
291 } else {
292 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
293 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700294 }
295
Colin Cross6ade34f2017-09-15 13:00:47 -0700296 deps = append(deps, flags.classpath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700297
Nan Zhang61eaedb2017-11-02 13:28:15 -0700298 srcJarDir := "srcjars"
299 outDir := "classes"
300 annoDir := "anno"
301 if shardIdx >= 0 {
302 shardDir := "shard" + strconv.Itoa(shardIdx)
303 srcJarDir = filepath.Join(shardDir, srcJarDir)
304 outDir = filepath.Join(shardDir, outDir)
305 annoDir = filepath.Join(shardDir, annoDir)
306 }
Colin Crossae887032017-10-23 17:16:14 -0700307 ctx.Build(pctx, android.BuildParams{
Colin Cross070879e2017-10-11 11:21:07 -0700308 Rule: rule,
309 Description: desc,
310 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700311 Inputs: srcFiles,
312 Implicits: deps,
313 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700314 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700315 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700316 "classpath": flags.classpath.FormJavaClassPath("-classpath"),
317 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700318 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
319 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
320 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross8eadbf02017-10-24 17:46:00 -0700321 "javaVersion": flags.javaVersion,
Colin Crossc6bbef32017-08-14 14:16:06 -0700322 },
323 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700324}
325
Colin Crosse9a275b2017-10-16 17:09:48 -0700326func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
327 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700328
Colin Crossae887032017-10-23 17:16:14 -0700329 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700330 Rule: jar,
331 Description: "jar",
332 Output: outputFile,
333 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700334 Args: map[string]string{
Colin Cross0a6e0072017-08-30 14:24:55 -0700335 "jarArgs": strings.Join(jarArgs, " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700336 },
337 })
Colin Cross2fe66872015-03-30 17:20:39 -0700338}
339
Nan Zhanged19fc32017-10-19 13:06:22 -0700340func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
341 jars android.Paths, manifest android.OptionalPath, stripDirs bool, dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700342
Colin Cross635acc92017-09-12 22:50:46 -0700343 var deps android.Paths
344
345 var jarArgs []string
346 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700347 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700348 deps = append(deps, manifest.Path())
349 }
350
Nan Zhanged19fc32017-10-19 13:06:22 -0700351 if dirsToStrip != nil {
352 for _, dir := range dirsToStrip {
353 jarArgs = append(jarArgs, "-stripDir ", dir)
354 }
355 }
356
Colin Cross7b60cdd2017-12-21 13:52:58 -0800357 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
358 // for downstream tools like desugar.
359 jarArgs = append(jarArgs, "-stripFile module-info.class")
360
Colin Cross635acc92017-09-12 22:50:46 -0700361 if stripDirs {
362 jarArgs = append(jarArgs, "-D")
363 }
364
Colin Crossae887032017-10-23 17:16:14 -0700365 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700366 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700367 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700368 Output: outputFile,
369 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700370 Implicits: deps,
371 Args: map[string]string{
372 "jarArgs": strings.Join(jarArgs, " "),
373 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700374 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700375}
376
Colin Crosse9a275b2017-10-16 17:09:48 -0700377func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
378 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700379 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700380 Rule: jarjar,
381 Description: "jarjar",
382 Output: outputFile,
383 Input: classesJar,
384 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700385 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700386 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700387 },
388 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700389}
Colin Cross6ade34f2017-09-15 13:00:47 -0700390
391type classpath []android.Path
392
Nan Zhanged19fc32017-10-19 13:06:22 -0700393func (x *classpath) FormJavaClassPath(optName string) string {
Colin Cross59149b62017-10-16 18:07:29 -0700394 if len(*x) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700395 return optName + " " + strings.Join(x.Strings(), ":")
Colin Cross6ade34f2017-09-15 13:00:47 -0700396 } else {
397 return ""
398 }
399}
400
Colin Cross1369cdb2017-09-29 17:58:17 -0700401// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
402// returns --system=none if the list is empty to ensure javac does not fall back to the default
403// system modules.
Nan Zhanged19fc32017-10-19 13:06:22 -0700404func (x *classpath) FormJavaSystemModulesPath(optName string, forceEmpty bool) string {
Colin Cross1369cdb2017-09-29 17:58:17 -0700405 if len(*x) > 1 {
406 panic("more than one system module")
407 } else if len(*x) == 1 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700408 return optName + strings.TrimSuffix((*x)[0].String(), "lib/modules")
Colin Cross1369cdb2017-09-29 17:58:17 -0700409 } else if forceEmpty {
Nan Zhanged19fc32017-10-19 13:06:22 -0700410 return optName + "none"
Colin Cross1369cdb2017-09-29 17:58:17 -0700411 } else {
412 return ""
413 }
414}
415
Nan Zhanged19fc32017-10-19 13:06:22 -0700416func (x *classpath) FormDesugarClasspath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700417 if x == nil || *x == nil {
418 return nil
419 }
420 flags := make([]string, len(*x))
421 for i, v := range *x {
Nan Zhanged19fc32017-10-19 13:06:22 -0700422 flags[i] = optName + " " + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700423 }
424
425 return flags
426}
427
Colin Cross6ade34f2017-09-15 13:00:47 -0700428// Convert a classpath to an android.Paths
429func (x *classpath) Paths() android.Paths {
430 return append(android.Paths(nil), (*x)...)
431}
432
433func (x *classpath) Strings() []string {
434 if x == nil {
435 return nil
436 }
437 ret := make([]string, len(*x))
438 for i, path := range *x {
439 ret[i] = path.String()
440 }
441 return ret
442}