blob: d36e0dc0f01961192cfe894164a2b813c6b5e66d [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 && ` +
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +000069 `rm $outDir/kotlinc-build.xml && ` +
Colin Cross93e85952017-08-15 13:34:18 -070070 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
71 CommandDeps: []string{
72 "${config.KotlincCmd}",
73 "${config.KotlinCompilerJar}",
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +000074 "${config.GenKotlinBuildFileCmd}",
Colin Cross93e85952017-08-15 13:34:18 -070075 "${config.SoongZipCmd}",
Colin Cross436b7652018-03-15 16:24:10 -070076 "${config.ZipSyncCmd}",
Colin Cross93e85952017-08-15 13:34:18 -070077 },
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +000078 Rspfile: "$out.rsp",
79 RspfileContent: `$in`,
Colin Cross93e85952017-08-15 13:34:18 -070080 },
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +000081 "kotlincFlags", "classpath", "srcJars", "srcJarDir", "outDir", "kotlinJvmTarget")
Colin Cross93e85952017-08-15 13:34:18 -070082
Colin Crossc6bbef32017-08-14 14:16:06 -070083 errorprone = pctx.AndroidStaticRule("errorprone",
84 blueprint.RuleParams{
Colin Cross8eadbf02017-10-24 17:46:00 -070085 Command: `rm -rf "$outDir" "$annoDir" "$srcJarDir" && mkdir -p "$outDir" "$annoDir" "$srcJarDir" && ` +
Colin Cross436b7652018-03-15 16:24:10 -070086 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
Colin Crossa4820652017-10-17 13:56:52 -070087 `${config.SoongJavacWrapper} ${config.ErrorProneCmd} ` +
Colin Cross8eadbf02017-10-24 17:46:00 -070088 `$javacFlags $bootClasspath $classpath ` +
Colin Crossc6bbef32017-08-14 14:16:06 -070089 `-source $javaVersion -target $javaVersion ` +
Colin Cross8eadbf02017-10-24 17:46:00 -070090 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list && ` +
Colin Cross0a6e0072017-08-30 14:24:55 -070091 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
Colin Crossc6bbef32017-08-14 14:16:06 -070092 CommandDeps: []string{
93 "${config.JavaCmd}",
94 "${config.ErrorProneJavacJar}",
95 "${config.ErrorProneJar}",
Colin Cross0a6e0072017-08-30 14:24:55 -070096 "${config.SoongZipCmd}",
Colin Cross436b7652018-03-15 16:24:10 -070097 "${config.ZipSyncCmd}",
Colin Crossc6bbef32017-08-14 14:16:06 -070098 },
Colin Crossa4820652017-10-17 13:56:52 -070099 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
100 Rspfile: "$out.rsp",
101 RspfileContent: "$in",
Colin Crossc6bbef32017-08-14 14:16:06 -0700102 },
Colin Cross8eadbf02017-10-24 17:46:00 -0700103 "javacFlags", "bootClasspath", "classpath", "srcJars", "srcJarDir",
104 "outDir", "annoDir", "javaVersion")
Colin Crossc6bbef32017-08-14 14:16:06 -0700105
Nan Zhanged19fc32017-10-19 13:06:22 -0700106 turbine = pctx.AndroidStaticRule("turbine",
107 blueprint.RuleParams{
Colin Cross6981f652018-03-07 15:14:50 -0800108 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700109 `${config.JavaCmd} -jar ${config.TurbineJar} --output $out.tmp ` +
Colin Cross6981f652018-03-07 15:14:50 -0800110 `--temp_dir "$outDir" --sources @$out.rsp --source_jars $srcJars ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700111 `--javacopts ${config.CommonJdkFlags} ` +
Colin Cross924a0aa2018-03-07 10:51:05 -0800112 `$javacFlags -source $javaVersion -target $javaVersion -- $bootClasspath $classpath && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700113 `${config.Ziptime} $out.tmp && ` +
114 `(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`,
Colin Cross8eadbf02017-10-24 17:46:00 -0700115 CommandDeps: []string{
116 "${config.TurbineJar}",
117 "${config.JavaCmd}",
118 "${config.Ziptime}",
Colin Cross8eadbf02017-10-24 17:46:00 -0700119 },
Nan Zhanged19fc32017-10-19 13:06:22 -0700120 Rspfile: "$out.rsp",
121 RspfileContent: "$in",
122 Restat: true,
123 },
Colin Cross6981f652018-03-07 15:14:50 -0800124 "javacFlags", "bootClasspath", "classpath", "srcJars", "outDir", "javaVersion")
Nan Zhanged19fc32017-10-19 13:06:22 -0700125
Colin Cross9d45bb72016-08-29 16:14:13 -0700126 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -0700127 blueprint.RuleParams{
Nan Zhang674dd932018-01-26 18:30:36 -0800128 Command: `${config.SoongZipCmd} -jar -o $out @$out.rsp`,
129 CommandDeps: []string{"${config.SoongZipCmd}"},
130 Rspfile: "$out.rsp",
131 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -0700132 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700133 "jarArgs")
134
135 combineJar = pctx.AndroidStaticRule("combineJar",
136 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800137 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700138 CommandDeps: []string{"${config.MergeZipsCmd}"},
139 },
Colin Cross635acc92017-09-12 22:50:46 -0700140 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700141
Colin Cross9d45bb72016-08-29 16:14:13 -0700142 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700143 blueprint.RuleParams{
Colin Cross64162712017-08-08 13:17:59 -0700144 Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out",
145 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700146 },
147 "rulesFile")
Colin Cross2fe66872015-03-30 17:20:39 -0700148)
149
150func init() {
Colin Cross64162712017-08-08 13:17:59 -0700151 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700152}
153
154type javaBuilderFlags struct {
155 javacFlags string
Colin Cross6ade34f2017-09-15 13:00:47 -0700156 bootClasspath classpath
157 classpath classpath
Colin Cross1369cdb2017-09-29 17:58:17 -0700158 systemModules classpath
Colin Crossc0b06f12015-04-08 13:03:43 -0700159 aidlFlags string
Colin Cross64162712017-08-08 13:17:59 -0700160 javaVersion string
Colin Cross6af17aa2017-09-20 12:59:05 -0700161
Andreas Gampef3e5b552018-01-22 21:27:21 -0800162 errorProneExtraJavacFlags string
163
Colin Cross93e85952017-08-15 13:34:18 -0700164 kotlincFlags string
165 kotlincClasspath classpath
166
Joe Onorato09e94ab2017-11-18 18:23:14 -0800167 protoFlags []string
168 protoOutTypeFlag string // The flag itself: --java_out
169 protoOutParams string // Parameters to that flag: --java_out=$protoOutParams:$outDir
Dan Willemsenab9f4262018-02-14 13:58:34 -0800170 protoRoot bool
Colin Cross2fe66872015-03-30 17:20:39 -0700171}
172
Colin Cross93e85952017-08-15 13:34:18 -0700173func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700174 srcFiles, srcJars android.Paths,
Colin Cross93e85952017-08-15 13:34:18 -0700175 flags javaBuilderFlags) {
176
Colin Cross93e85952017-08-15 13:34:18 -0700177 inputs := append(android.Paths(nil), srcFiles...)
Colin Cross93e85952017-08-15 13:34:18 -0700178
Colin Cross9bc43432017-12-15 20:20:39 -0800179 var deps android.Paths
180 deps = append(deps, flags.kotlincClasspath...)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000181 deps = append(deps, srcJars...)
Colin Cross9bc43432017-12-15 20:20:39 -0800182
Colin Crossae887032017-10-23 17:16:14 -0700183 ctx.Build(pctx, android.BuildParams{
Colin Cross93e85952017-08-15 13:34:18 -0700184 Rule: kotlinc,
185 Description: "kotlinc",
186 Output: outputFile,
187 Inputs: inputs,
Colin Cross9bc43432017-12-15 20:20:39 -0800188 Implicits: deps,
Colin Cross93e85952017-08-15 13:34:18 -0700189 Args: map[string]string{
Colin Cross715d7112017-10-25 20:32:18 -0700190 "classpath": flags.kotlincClasspath.FormJavaClassPath("-classpath"),
Colin Cross93e85952017-08-15 13:34:18 -0700191 "kotlincFlags": flags.kotlincFlags,
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000192 "srcJars": strings.Join(srcJars.Strings(), " "),
193 "outDir": android.PathForModuleOut(ctx, "kotlinc", "classes").String(),
194 "srcJarDir": android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(),
Tobias Thierer96427d62017-11-18 03:10:51 +0000195 // http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8
196 "kotlinJvmTarget": "1.8",
Colin Cross93e85952017-08-15 13:34:18 -0700197 },
198 })
199}
200
Nan Zhang61eaedb2017-11-02 13:28:15 -0700201func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
202 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700203
Nan Zhang61eaedb2017-11-02 13:28:15 -0700204 // Compile java sources into .class files
205 desc := "javac"
206 if shardIdx >= 0 {
207 desc += strconv.Itoa(shardIdx)
208 }
209
210 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc, javac)
Colin Cross2fe66872015-03-30 17:20:39 -0700211}
212
Colin Crosse9a275b2017-10-16 17:09:48 -0700213func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700214 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700215
Colin Crossfee57cb2017-09-05 13:16:45 -0700216 if config.ErrorProneJar == "" {
217 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
Colin Crossfee57cb2017-09-05 13:16:45 -0700218 }
219
Andreas Gampef3e5b552018-01-22 21:27:21 -0800220 if len(flags.errorProneExtraJavacFlags) > 0 {
221 if len(flags.javacFlags) > 0 {
222 flags.javacFlags = flags.errorProneExtraJavacFlags + " " + flags.javacFlags
223 } else {
224 flags.javacFlags = flags.errorProneExtraJavacFlags
225 }
226 }
227
Nan Zhang61eaedb2017-11-02 13:28:15 -0700228 transformJavaToClasses(ctx, outputFile, -1, srcFiles, srcJars, flags, nil,
Colin Cross1ee23172017-10-18 14:44:18 -0700229 "errorprone", "errorprone", errorprone)
Colin Cross070879e2017-10-11 11:21:07 -0700230}
231
Nan Zhanged19fc32017-10-19 13:06:22 -0700232func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700233 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700234
235 var deps android.Paths
236 deps = append(deps, srcJars...)
237 deps = append(deps, flags.bootClasspath...)
238 deps = append(deps, flags.classpath...)
239
240 var bootClasspath string
241 if len(flags.bootClasspath) == 0 && ctx.Device() {
242 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
243 // ensure java does not fall back to the default bootclasspath.
244 bootClasspath = `--bootclasspath ""`
245 } else {
Colin Cross924a0aa2018-03-07 10:51:05 -0800246 bootClasspath = strings.Join(flags.bootClasspath.FormDesugarClasspath("--bootclasspath"), " ")
Nan Zhanged19fc32017-10-19 13:06:22 -0700247 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700248
Colin Crossae887032017-10-23 17:16:14 -0700249 ctx.Build(pctx, android.BuildParams{
Nan Zhanged19fc32017-10-19 13:06:22 -0700250 Rule: turbine,
251 Description: "turbine",
252 Output: outputFile,
253 Inputs: srcFiles,
254 Implicits: deps,
255 Args: map[string]string{
256 "javacFlags": flags.javacFlags,
257 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700258 "srcJars": strings.Join(srcJars.Strings(), " "),
Colin Cross924a0aa2018-03-07 10:51:05 -0800259 "classpath": strings.Join(flags.classpath.FormDesugarClasspath("--classpath"), " "),
Nan Zhanged19fc32017-10-19 13:06:22 -0700260 "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
261 "javaVersion": flags.javaVersion,
262 },
263 })
264}
265
Colin Cross070879e2017-10-11 11:21:07 -0700266// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700267// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
268// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700269//
270// This method may be used for different compilers, including javac and Error Prone. The rule
271// argument specifies which command line to use and desc sets the description of the rule that will
272// be printed at build time. The stem argument provides the file name of the output jar, and
273// suffix will be appended to various intermediate files and directories to avoid collisions when
274// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700275func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700276 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700277 flags javaBuilderFlags, deps android.Paths,
Colin Cross1ee23172017-10-18 14:44:18 -0700278 intermediatesDir, desc string, rule blueprint.Rule) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700279
Colin Cross59149b62017-10-16 18:07:29 -0700280 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700281
282 var bootClasspath string
283 if flags.javaVersion == "1.9" {
284 deps = append(deps, flags.systemModules...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700285 bootClasspath = flags.systemModules.FormJavaSystemModulesPath("--system=", ctx.Device())
Colin Cross1369cdb2017-09-29 17:58:17 -0700286 } else {
287 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700288 if len(flags.bootClasspath) == 0 && ctx.Device() {
289 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
290 // ensure java does not fall back to the default bootclasspath.
291 bootClasspath = `-bootclasspath ""`
292 } else {
293 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
294 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700295 }
296
Colin Cross6ade34f2017-09-15 13:00:47 -0700297 deps = append(deps, flags.classpath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700298
Nan Zhang61eaedb2017-11-02 13:28:15 -0700299 srcJarDir := "srcjars"
300 outDir := "classes"
301 annoDir := "anno"
302 if shardIdx >= 0 {
303 shardDir := "shard" + strconv.Itoa(shardIdx)
304 srcJarDir = filepath.Join(shardDir, srcJarDir)
305 outDir = filepath.Join(shardDir, outDir)
306 annoDir = filepath.Join(shardDir, annoDir)
307 }
Colin Crossae887032017-10-23 17:16:14 -0700308 ctx.Build(pctx, android.BuildParams{
Colin Cross070879e2017-10-11 11:21:07 -0700309 Rule: rule,
310 Description: desc,
311 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700312 Inputs: srcFiles,
313 Implicits: deps,
314 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700315 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700316 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700317 "classpath": flags.classpath.FormJavaClassPath("-classpath"),
318 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700319 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
320 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
321 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross8eadbf02017-10-24 17:46:00 -0700322 "javaVersion": flags.javaVersion,
Colin Crossc6bbef32017-08-14 14:16:06 -0700323 },
324 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700325}
326
Colin Crosse9a275b2017-10-16 17:09:48 -0700327func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
328 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700329
Colin Crossae887032017-10-23 17:16:14 -0700330 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700331 Rule: jar,
332 Description: "jar",
333 Output: outputFile,
334 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700335 Args: map[string]string{
Colin Cross0a6e0072017-08-30 14:24:55 -0700336 "jarArgs": strings.Join(jarArgs, " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700337 },
338 })
Colin Cross2fe66872015-03-30 17:20:39 -0700339}
340
Nan Zhanged19fc32017-10-19 13:06:22 -0700341func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
342 jars android.Paths, manifest android.OptionalPath, stripDirs bool, dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700343
Colin Cross635acc92017-09-12 22:50:46 -0700344 var deps android.Paths
345
346 var jarArgs []string
347 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700348 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700349 deps = append(deps, manifest.Path())
350 }
351
Nan Zhanged19fc32017-10-19 13:06:22 -0700352 if dirsToStrip != nil {
353 for _, dir := range dirsToStrip {
354 jarArgs = append(jarArgs, "-stripDir ", dir)
355 }
356 }
357
Colin Cross7b60cdd2017-12-21 13:52:58 -0800358 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
359 // for downstream tools like desugar.
360 jarArgs = append(jarArgs, "-stripFile module-info.class")
361
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000362 // Remove any kotlin-reflect related files
363 // TODO(pszczepaniak): Support kotlin-reflect
364 jarArgs = append(jarArgs, "-stripFile \"*.kotlin_module\"")
365 jarArgs = append(jarArgs, "-stripFile \"*.kotlin_builtin\"")
366
Colin Cross635acc92017-09-12 22:50:46 -0700367 if stripDirs {
368 jarArgs = append(jarArgs, "-D")
369 }
370
Colin Crossae887032017-10-23 17:16:14 -0700371 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700372 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700373 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700374 Output: outputFile,
375 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700376 Implicits: deps,
377 Args: map[string]string{
378 "jarArgs": strings.Join(jarArgs, " "),
379 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700380 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700381}
382
Colin Crosse9a275b2017-10-16 17:09:48 -0700383func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
384 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700385 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700386 Rule: jarjar,
387 Description: "jarjar",
388 Output: outputFile,
389 Input: classesJar,
390 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700391 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700392 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700393 },
394 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700395}
Colin Cross6ade34f2017-09-15 13:00:47 -0700396
397type classpath []android.Path
398
Nan Zhanged19fc32017-10-19 13:06:22 -0700399func (x *classpath) FormJavaClassPath(optName string) string {
Colin Cross59149b62017-10-16 18:07:29 -0700400 if len(*x) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700401 return optName + " " + strings.Join(x.Strings(), ":")
Colin Cross6ade34f2017-09-15 13:00:47 -0700402 } else {
403 return ""
404 }
405}
406
Colin Cross1369cdb2017-09-29 17:58:17 -0700407// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
408// returns --system=none if the list is empty to ensure javac does not fall back to the default
409// system modules.
Nan Zhanged19fc32017-10-19 13:06:22 -0700410func (x *classpath) FormJavaSystemModulesPath(optName string, forceEmpty bool) string {
Colin Cross1369cdb2017-09-29 17:58:17 -0700411 if len(*x) > 1 {
412 panic("more than one system module")
413 } else if len(*x) == 1 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700414 return optName + strings.TrimSuffix((*x)[0].String(), "lib/modules")
Colin Cross1369cdb2017-09-29 17:58:17 -0700415 } else if forceEmpty {
Nan Zhanged19fc32017-10-19 13:06:22 -0700416 return optName + "none"
Colin Cross1369cdb2017-09-29 17:58:17 -0700417 } else {
418 return ""
419 }
420}
421
Nan Zhanged19fc32017-10-19 13:06:22 -0700422func (x *classpath) FormDesugarClasspath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700423 if x == nil || *x == nil {
424 return nil
425 }
426 flags := make([]string, len(*x))
427 for i, v := range *x {
Nan Zhanged19fc32017-10-19 13:06:22 -0700428 flags[i] = optName + " " + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700429 }
430
431 return flags
432}
433
Colin Cross6ade34f2017-09-15 13:00:47 -0700434// Convert a classpath to an android.Paths
435func (x *classpath) Paths() android.Paths {
436 return append(android.Paths(nil), (*x)...)
437}
438
439func (x *classpath) Strings() []string {
440 if x == nil {
441 return nil
442 }
443 ret := make([]string, len(*x))
444 for i, path := range *x {
445 ret[i] = path.String()
446 }
447 return ret
448}