blob: 9e3806550eb8554a208f72967dec9d5237e7cb6c [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
38 // this, all java rules write into separate directories and then a post-processing step lists
39 // the files in the the directory into a list file that later rules depend on (and sometimes
40 // read from directly using @<listfile>)
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" && ` +
44 `${config.ExtractSrcJarsCmd} $srcJarDir $srcJarDir/list $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}",
53 "${config.ExtractSrcJarsCmd}",
54 },
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" && ` +
65 `${config.ExtractSrcJarsCmd} $srcJarDir $srcJarDir/list $srcJars && ` +
66 `${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}",
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +000075 "${config.ExtractSrcJarsCmd}",
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" && ` +
85 `${config.ExtractSrcJarsCmd} $srcJarDir $srcJarDir/list $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 Cross8eadbf02017-10-24 17:46:00 -070096 "${config.ExtractSrcJarsCmd}",
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 Cross8eadbf02017-10-24 17:46:00 -0700107 Command: `rm -rf "$outDir" "$srcJarDir" && mkdir -p "$outDir" "$srcJarDir" && ` +
108 `${config.ExtractSrcJarsCmd} $srcJarDir $srcJarDir/list $srcJars && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700109 `${config.JavaCmd} -jar ${config.TurbineJar} --output $out.tmp ` +
Colin Cross8eadbf02017-10-24 17:46:00 -0700110 `--temp_dir "$outDir" --sources @$out.rsp @$srcJarDir/list ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700111 `--javacopts ${config.CommonJdkFlags} ` +
112 `$javacFlags -source $javaVersion -target $javaVersion $bootClasspath $classpath && ` +
113 `${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}",
119 "${config.ExtractSrcJarsCmd}",
120 },
Nan Zhanged19fc32017-10-19 13:06:22 -0700121 Rspfile: "$out.rsp",
122 RspfileContent: "$in",
123 Restat: true,
124 },
Colin Cross8eadbf02017-10-24 17:46:00 -0700125 "javacFlags", "bootClasspath", "classpath", "srcJars", "srcJarDir",
126 "outDir", "javaVersion")
Nan Zhanged19fc32017-10-19 13:06:22 -0700127
Colin Cross9d45bb72016-08-29 16:14:13 -0700128 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -0700129 blueprint.RuleParams{
Nan Zhang674dd932018-01-26 18:30:36 -0800130 Command: `${config.SoongZipCmd} -jar -o $out @$out.rsp`,
131 CommandDeps: []string{"${config.SoongZipCmd}"},
132 Rspfile: "$out.rsp",
133 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -0700134 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700135 "jarArgs")
136
137 combineJar = pctx.AndroidStaticRule("combineJar",
138 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800139 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700140 CommandDeps: []string{"${config.MergeZipsCmd}"},
141 },
Colin Cross635acc92017-09-12 22:50:46 -0700142 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700143
Colin Cross9d45bb72016-08-29 16:14:13 -0700144 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700145 blueprint.RuleParams{
Colin Cross64162712017-08-08 13:17:59 -0700146 Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out",
147 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700148 },
149 "rulesFile")
Colin Cross2fe66872015-03-30 17:20:39 -0700150)
151
152func init() {
Colin Cross64162712017-08-08 13:17:59 -0700153 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700154}
155
156type javaBuilderFlags struct {
157 javacFlags string
Colin Cross6ade34f2017-09-15 13:00:47 -0700158 bootClasspath classpath
159 classpath classpath
Colin Cross1369cdb2017-09-29 17:58:17 -0700160 systemModules classpath
Colin Crossc0b06f12015-04-08 13:03:43 -0700161 aidlFlags string
Colin Cross64162712017-08-08 13:17:59 -0700162 javaVersion string
Colin Cross6af17aa2017-09-20 12:59:05 -0700163
Andreas Gampef3e5b552018-01-22 21:27:21 -0800164 errorProneExtraJavacFlags string
165
Colin Cross93e85952017-08-15 13:34:18 -0700166 kotlincFlags string
167 kotlincClasspath classpath
168
Joe Onorato09e94ab2017-11-18 18:23:14 -0800169 protoFlags []string
170 protoOutTypeFlag string // The flag itself: --java_out
171 protoOutParams string // Parameters to that flag: --java_out=$protoOutParams:$outDir
Colin Cross2fe66872015-03-30 17:20:39 -0700172}
173
Colin Cross93e85952017-08-15 13:34:18 -0700174func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700175 srcFiles, srcJars android.Paths,
Colin Cross93e85952017-08-15 13:34:18 -0700176 flags javaBuilderFlags) {
177
Colin Cross93e85952017-08-15 13:34:18 -0700178 inputs := append(android.Paths(nil), srcFiles...)
Colin Cross93e85952017-08-15 13:34:18 -0700179
Colin Cross9bc43432017-12-15 20:20:39 -0800180 var deps android.Paths
181 deps = append(deps, flags.kotlincClasspath...)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000182 deps = append(deps, srcJars...)
Colin Cross9bc43432017-12-15 20:20:39 -0800183
Colin Crossae887032017-10-23 17:16:14 -0700184 ctx.Build(pctx, android.BuildParams{
Colin Cross93e85952017-08-15 13:34:18 -0700185 Rule: kotlinc,
186 Description: "kotlinc",
187 Output: outputFile,
188 Inputs: inputs,
Colin Cross9bc43432017-12-15 20:20:39 -0800189 Implicits: deps,
Colin Cross93e85952017-08-15 13:34:18 -0700190 Args: map[string]string{
Colin Cross715d7112017-10-25 20:32:18 -0700191 "classpath": flags.kotlincClasspath.FormJavaClassPath("-classpath"),
Colin Cross93e85952017-08-15 13:34:18 -0700192 "kotlincFlags": flags.kotlincFlags,
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000193 "srcJars": strings.Join(srcJars.Strings(), " "),
194 "outDir": android.PathForModuleOut(ctx, "kotlinc", "classes").String(),
195 "srcJarDir": android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(),
Tobias Thierer96427d62017-11-18 03:10:51 +0000196 // http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8
197 "kotlinJvmTarget": "1.8",
Colin Cross93e85952017-08-15 13:34:18 -0700198 },
199 })
200}
201
Nan Zhang61eaedb2017-11-02 13:28:15 -0700202func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
203 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700204
Nan Zhang61eaedb2017-11-02 13:28:15 -0700205 // Compile java sources into .class files
206 desc := "javac"
207 if shardIdx >= 0 {
208 desc += strconv.Itoa(shardIdx)
209 }
210
211 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc, javac)
Colin Cross2fe66872015-03-30 17:20:39 -0700212}
213
Colin Crosse9a275b2017-10-16 17:09:48 -0700214func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700215 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700216
Colin Crossfee57cb2017-09-05 13:16:45 -0700217 if config.ErrorProneJar == "" {
218 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
Colin Crossfee57cb2017-09-05 13:16:45 -0700219 }
220
Andreas Gampef3e5b552018-01-22 21:27:21 -0800221 if len(flags.errorProneExtraJavacFlags) > 0 {
222 if len(flags.javacFlags) > 0 {
223 flags.javacFlags = flags.errorProneExtraJavacFlags + " " + flags.javacFlags
224 } else {
225 flags.javacFlags = flags.errorProneExtraJavacFlags
226 }
227 }
228
Nan Zhang61eaedb2017-11-02 13:28:15 -0700229 transformJavaToClasses(ctx, outputFile, -1, srcFiles, srcJars, flags, nil,
Colin Cross1ee23172017-10-18 14:44:18 -0700230 "errorprone", "errorprone", errorprone)
Colin Cross070879e2017-10-11 11:21:07 -0700231}
232
Nan Zhanged19fc32017-10-19 13:06:22 -0700233func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700234 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700235
236 var deps android.Paths
237 deps = append(deps, srcJars...)
238 deps = append(deps, flags.bootClasspath...)
239 deps = append(deps, flags.classpath...)
240
241 var bootClasspath string
242 if len(flags.bootClasspath) == 0 && ctx.Device() {
243 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
244 // ensure java does not fall back to the default bootclasspath.
245 bootClasspath = `--bootclasspath ""`
246 } else {
247 bootClasspath = flags.bootClasspath.FormJavaClassPath("--bootclasspath")
248 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700249
Colin Crossae887032017-10-23 17:16:14 -0700250 ctx.Build(pctx, android.BuildParams{
Nan Zhanged19fc32017-10-19 13:06:22 -0700251 Rule: turbine,
252 Description: "turbine",
253 Output: outputFile,
254 Inputs: srcFiles,
255 Implicits: deps,
256 Args: map[string]string{
257 "javacFlags": flags.javacFlags,
258 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700259 "srcJars": strings.Join(srcJars.Strings(), " "),
260 "srcJarDir": android.PathForModuleOut(ctx, "turbine", "srcjars").String(),
Nan Zhanged19fc32017-10-19 13:06:22 -0700261 "classpath": flags.classpath.FormJavaClassPath("--classpath"),
262 "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
263 "javaVersion": flags.javaVersion,
264 },
265 })
266}
267
Colin Cross070879e2017-10-11 11:21:07 -0700268// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700269// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
270// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700271//
272// This method may be used for different compilers, including javac and Error Prone. The rule
273// argument specifies which command line to use and desc sets the description of the rule that will
274// be printed at build time. The stem argument provides the file name of the output jar, and
275// suffix will be appended to various intermediate files and directories to avoid collisions when
276// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700277func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700278 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700279 flags javaBuilderFlags, deps android.Paths,
Colin Cross1ee23172017-10-18 14:44:18 -0700280 intermediatesDir, desc string, rule blueprint.Rule) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700281
Colin Cross59149b62017-10-16 18:07:29 -0700282 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700283
284 var bootClasspath string
285 if flags.javaVersion == "1.9" {
286 deps = append(deps, flags.systemModules...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700287 bootClasspath = flags.systemModules.FormJavaSystemModulesPath("--system=", ctx.Device())
Colin Cross1369cdb2017-09-29 17:58:17 -0700288 } else {
289 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700290 if len(flags.bootClasspath) == 0 && ctx.Device() {
291 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
292 // ensure java does not fall back to the default bootclasspath.
293 bootClasspath = `-bootclasspath ""`
294 } else {
295 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
296 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700297 }
298
Colin Cross6ade34f2017-09-15 13:00:47 -0700299 deps = append(deps, flags.classpath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700300
Nan Zhang61eaedb2017-11-02 13:28:15 -0700301 srcJarDir := "srcjars"
302 outDir := "classes"
303 annoDir := "anno"
304 if shardIdx >= 0 {
305 shardDir := "shard" + strconv.Itoa(shardIdx)
306 srcJarDir = filepath.Join(shardDir, srcJarDir)
307 outDir = filepath.Join(shardDir, outDir)
308 annoDir = filepath.Join(shardDir, annoDir)
309 }
Colin Crossae887032017-10-23 17:16:14 -0700310 ctx.Build(pctx, android.BuildParams{
Colin Cross070879e2017-10-11 11:21:07 -0700311 Rule: rule,
312 Description: desc,
313 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700314 Inputs: srcFiles,
315 Implicits: deps,
316 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700317 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700318 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700319 "classpath": flags.classpath.FormJavaClassPath("-classpath"),
320 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700321 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
322 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
323 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross8eadbf02017-10-24 17:46:00 -0700324 "javaVersion": flags.javaVersion,
Colin Crossc6bbef32017-08-14 14:16:06 -0700325 },
326 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700327}
328
Colin Crosse9a275b2017-10-16 17:09:48 -0700329func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
330 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700331
Colin Crossae887032017-10-23 17:16:14 -0700332 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700333 Rule: jar,
334 Description: "jar",
335 Output: outputFile,
336 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700337 Args: map[string]string{
Colin Cross0a6e0072017-08-30 14:24:55 -0700338 "jarArgs": strings.Join(jarArgs, " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700339 },
340 })
Colin Cross2fe66872015-03-30 17:20:39 -0700341}
342
Nan Zhanged19fc32017-10-19 13:06:22 -0700343func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
344 jars android.Paths, manifest android.OptionalPath, stripDirs bool, dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700345
Colin Cross635acc92017-09-12 22:50:46 -0700346 var deps android.Paths
347
348 var jarArgs []string
349 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700350 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700351 deps = append(deps, manifest.Path())
352 }
353
Nan Zhanged19fc32017-10-19 13:06:22 -0700354 if dirsToStrip != nil {
355 for _, dir := range dirsToStrip {
356 jarArgs = append(jarArgs, "-stripDir ", dir)
357 }
358 }
359
Colin Cross7b60cdd2017-12-21 13:52:58 -0800360 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
361 // for downstream tools like desugar.
362 jarArgs = append(jarArgs, "-stripFile module-info.class")
363
Colin Cross635acc92017-09-12 22:50:46 -0700364 if stripDirs {
365 jarArgs = append(jarArgs, "-D")
366 }
367
Colin Crossae887032017-10-23 17:16:14 -0700368 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700369 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700370 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700371 Output: outputFile,
372 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700373 Implicits: deps,
374 Args: map[string]string{
375 "jarArgs": strings.Join(jarArgs, " "),
376 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700377 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700378}
379
Colin Crosse9a275b2017-10-16 17:09:48 -0700380func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
381 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700382 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700383 Rule: jarjar,
384 Description: "jarjar",
385 Output: outputFile,
386 Input: classesJar,
387 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700388 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700389 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700390 },
391 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700392}
Colin Cross6ade34f2017-09-15 13:00:47 -0700393
394type classpath []android.Path
395
Nan Zhanged19fc32017-10-19 13:06:22 -0700396func (x *classpath) FormJavaClassPath(optName string) string {
Colin Cross59149b62017-10-16 18:07:29 -0700397 if len(*x) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700398 return optName + " " + strings.Join(x.Strings(), ":")
Colin Cross6ade34f2017-09-15 13:00:47 -0700399 } else {
400 return ""
401 }
402}
403
Colin Cross1369cdb2017-09-29 17:58:17 -0700404// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
405// returns --system=none if the list is empty to ensure javac does not fall back to the default
406// system modules.
Nan Zhanged19fc32017-10-19 13:06:22 -0700407func (x *classpath) FormJavaSystemModulesPath(optName string, forceEmpty bool) string {
Colin Cross1369cdb2017-09-29 17:58:17 -0700408 if len(*x) > 1 {
409 panic("more than one system module")
410 } else if len(*x) == 1 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700411 return optName + strings.TrimSuffix((*x)[0].String(), "lib/modules")
Colin Cross1369cdb2017-09-29 17:58:17 -0700412 } else if forceEmpty {
Nan Zhanged19fc32017-10-19 13:06:22 -0700413 return optName + "none"
Colin Cross1369cdb2017-09-29 17:58:17 -0700414 } else {
415 return ""
416 }
417}
418
Nan Zhanged19fc32017-10-19 13:06:22 -0700419func (x *classpath) FormDesugarClasspath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700420 if x == nil || *x == nil {
421 return nil
422 }
423 flags := make([]string, len(*x))
424 for i, v := range *x {
Nan Zhanged19fc32017-10-19 13:06:22 -0700425 flags[i] = optName + " " + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700426 }
427
428 return flags
429}
430
431// Append an android.Paths to the end of the classpath list
432func (x *classpath) AddPaths(paths android.Paths) {
433 for _, path := range paths {
434 *x = append(*x, path)
435 }
436}
437
438// Convert a classpath to an android.Paths
439func (x *classpath) Paths() android.Paths {
440 return append(android.Paths(nil), (*x)...)
441}
442
443func (x *classpath) Strings() []string {
444 if x == nil {
445 return nil
446 }
447 ret := make([]string, len(*x))
448 for i, path := range *x {
449 ret[i] = path.String()
450 }
451 return ret
452}