blob: 72574f19791b6e5a988d47ee8f0e238a5ec83114 [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{
64 // TODO(ccross): kotlinc doesn't support @ file for arguments, which will limit the
65 // maximum number of input files, especially on darwin.
66 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
67 `${config.KotlincCmd} $classpath $kotlincFlags ` +
Tobias Thierer96427d62017-11-18 03:10:51 +000068 `-jvm-target $kotlinJvmTarget -d $outDir $in && ` +
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}",
73 "${config.SoongZipCmd}",
74 },
75 },
Tobias Thierer96427d62017-11-18 03:10:51 +000076 "kotlincFlags", "classpath", "outDir", "kotlinJvmTarget")
Colin Cross93e85952017-08-15 13:34:18 -070077
Colin Crossc6bbef32017-08-14 14:16:06 -070078 errorprone = pctx.AndroidStaticRule("errorprone",
79 blueprint.RuleParams{
Colin Cross8eadbf02017-10-24 17:46:00 -070080 Command: `rm -rf "$outDir" "$annoDir" "$srcJarDir" && mkdir -p "$outDir" "$annoDir" "$srcJarDir" && ` +
81 `${config.ExtractSrcJarsCmd} $srcJarDir $srcJarDir/list $srcJars && ` +
Colin Crossa4820652017-10-17 13:56:52 -070082 `${config.SoongJavacWrapper} ${config.ErrorProneCmd} ` +
Colin Cross8eadbf02017-10-24 17:46:00 -070083 `$javacFlags $bootClasspath $classpath ` +
Colin Crossc6bbef32017-08-14 14:16:06 -070084 `-source $javaVersion -target $javaVersion ` +
Colin Cross8eadbf02017-10-24 17:46:00 -070085 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list && ` +
Colin Cross0a6e0072017-08-30 14:24:55 -070086 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
Colin Crossc6bbef32017-08-14 14:16:06 -070087 CommandDeps: []string{
88 "${config.JavaCmd}",
89 "${config.ErrorProneJavacJar}",
90 "${config.ErrorProneJar}",
Colin Cross0a6e0072017-08-30 14:24:55 -070091 "${config.SoongZipCmd}",
Colin Cross8eadbf02017-10-24 17:46:00 -070092 "${config.ExtractSrcJarsCmd}",
Colin Crossc6bbef32017-08-14 14:16:06 -070093 },
Colin Crossa4820652017-10-17 13:56:52 -070094 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
95 Rspfile: "$out.rsp",
96 RspfileContent: "$in",
Colin Crossc6bbef32017-08-14 14:16:06 -070097 },
Colin Cross8eadbf02017-10-24 17:46:00 -070098 "javacFlags", "bootClasspath", "classpath", "srcJars", "srcJarDir",
99 "outDir", "annoDir", "javaVersion")
Colin Crossc6bbef32017-08-14 14:16:06 -0700100
Nan Zhanged19fc32017-10-19 13:06:22 -0700101 turbine = pctx.AndroidStaticRule("turbine",
102 blueprint.RuleParams{
Colin Cross8eadbf02017-10-24 17:46:00 -0700103 Command: `rm -rf "$outDir" "$srcJarDir" && mkdir -p "$outDir" "$srcJarDir" && ` +
104 `${config.ExtractSrcJarsCmd} $srcJarDir $srcJarDir/list $srcJars && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700105 `${config.JavaCmd} -jar ${config.TurbineJar} --output $out.tmp ` +
Colin Cross8eadbf02017-10-24 17:46:00 -0700106 `--temp_dir "$outDir" --sources @$out.rsp @$srcJarDir/list ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700107 `--javacopts ${config.CommonJdkFlags} ` +
108 `$javacFlags -source $javaVersion -target $javaVersion $bootClasspath $classpath && ` +
109 `${config.Ziptime} $out.tmp && ` +
110 `(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`,
Colin Cross8eadbf02017-10-24 17:46:00 -0700111 CommandDeps: []string{
112 "${config.TurbineJar}",
113 "${config.JavaCmd}",
114 "${config.Ziptime}",
115 "${config.ExtractSrcJarsCmd}",
116 },
Nan Zhanged19fc32017-10-19 13:06:22 -0700117 Rspfile: "$out.rsp",
118 RspfileContent: "$in",
119 Restat: true,
120 },
Colin Cross8eadbf02017-10-24 17:46:00 -0700121 "javacFlags", "bootClasspath", "classpath", "srcJars", "srcJarDir",
122 "outDir", "javaVersion")
Nan Zhanged19fc32017-10-19 13:06:22 -0700123
Colin Cross9d45bb72016-08-29 16:14:13 -0700124 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -0700125 blueprint.RuleParams{
Nan Zhang674dd932018-01-26 18:30:36 -0800126 Command: `${config.SoongZipCmd} -jar -o $out @$out.rsp`,
127 CommandDeps: []string{"${config.SoongZipCmd}"},
128 Rspfile: "$out.rsp",
129 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -0700130 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700131 "jarArgs")
132
133 combineJar = pctx.AndroidStaticRule("combineJar",
134 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800135 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700136 CommandDeps: []string{"${config.MergeZipsCmd}"},
137 },
Colin Cross635acc92017-09-12 22:50:46 -0700138 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700139
Colin Cross9d45bb72016-08-29 16:14:13 -0700140 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700141 blueprint.RuleParams{
Colin Cross64162712017-08-08 13:17:59 -0700142 Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out",
143 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700144 },
145 "rulesFile")
Colin Cross2fe66872015-03-30 17:20:39 -0700146)
147
148func init() {
Colin Cross64162712017-08-08 13:17:59 -0700149 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700150}
151
152type javaBuilderFlags struct {
153 javacFlags string
Colin Cross6ade34f2017-09-15 13:00:47 -0700154 bootClasspath classpath
155 classpath classpath
Colin Cross1369cdb2017-09-29 17:58:17 -0700156 systemModules classpath
Colin Crossc0b06f12015-04-08 13:03:43 -0700157 aidlFlags string
Colin Cross64162712017-08-08 13:17:59 -0700158 javaVersion string
Colin Cross6af17aa2017-09-20 12:59:05 -0700159
Andreas Gampef3e5b552018-01-22 21:27:21 -0800160 errorProneExtraJavacFlags string
161
Colin Cross93e85952017-08-15 13:34:18 -0700162 kotlincFlags string
163 kotlincClasspath classpath
164
Joe Onorato09e94ab2017-11-18 18:23:14 -0800165 protoFlags []string
166 protoOutTypeFlag string // The flag itself: --java_out
167 protoOutParams string // Parameters to that flag: --java_out=$protoOutParams:$outDir
Colin Cross2fe66872015-03-30 17:20:39 -0700168}
169
Colin Cross93e85952017-08-15 13:34:18 -0700170func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700171 srcFiles, srcJars android.Paths,
Colin Cross93e85952017-08-15 13:34:18 -0700172 flags javaBuilderFlags) {
173
Colin Cross1ee23172017-10-18 14:44:18 -0700174 classDir := android.PathForModuleOut(ctx, "kotlinc", "classes")
Colin Cross93e85952017-08-15 13:34:18 -0700175
176 inputs := append(android.Paths(nil), srcFiles...)
177 inputs = append(inputs, srcJars...)
178
Colin Cross9bc43432017-12-15 20:20:39 -0800179 var deps android.Paths
180 deps = append(deps, flags.kotlincClasspath...)
181
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,
191 "outDir": classDir.String(),
Tobias Thierer96427d62017-11-18 03:10:51 +0000192 // http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8
193 "kotlinJvmTarget": "1.8",
Colin Cross93e85952017-08-15 13:34:18 -0700194 },
195 })
196}
197
Nan Zhang61eaedb2017-11-02 13:28:15 -0700198func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
199 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700200
Nan Zhang61eaedb2017-11-02 13:28:15 -0700201 // Compile java sources into .class files
202 desc := "javac"
203 if shardIdx >= 0 {
204 desc += strconv.Itoa(shardIdx)
205 }
206
207 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc, javac)
Colin Cross2fe66872015-03-30 17:20:39 -0700208}
209
Colin Crosse9a275b2017-10-16 17:09:48 -0700210func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700211 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700212
Colin Crossfee57cb2017-09-05 13:16:45 -0700213 if config.ErrorProneJar == "" {
214 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
Colin Crossfee57cb2017-09-05 13:16:45 -0700215 }
216
Andreas Gampef3e5b552018-01-22 21:27:21 -0800217 if len(flags.errorProneExtraJavacFlags) > 0 {
218 if len(flags.javacFlags) > 0 {
219 flags.javacFlags = flags.errorProneExtraJavacFlags + " " + flags.javacFlags
220 } else {
221 flags.javacFlags = flags.errorProneExtraJavacFlags
222 }
223 }
224
Nan Zhang61eaedb2017-11-02 13:28:15 -0700225 transformJavaToClasses(ctx, outputFile, -1, srcFiles, srcJars, flags, nil,
Colin Cross1ee23172017-10-18 14:44:18 -0700226 "errorprone", "errorprone", errorprone)
Colin Cross070879e2017-10-11 11:21:07 -0700227}
228
Nan Zhanged19fc32017-10-19 13:06:22 -0700229func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700230 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700231
232 var deps android.Paths
233 deps = append(deps, srcJars...)
234 deps = append(deps, flags.bootClasspath...)
235 deps = append(deps, flags.classpath...)
236
237 var bootClasspath string
238 if len(flags.bootClasspath) == 0 && ctx.Device() {
239 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
240 // ensure java does not fall back to the default bootclasspath.
241 bootClasspath = `--bootclasspath ""`
242 } else {
243 bootClasspath = flags.bootClasspath.FormJavaClassPath("--bootclasspath")
244 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700245
Colin Crossae887032017-10-23 17:16:14 -0700246 ctx.Build(pctx, android.BuildParams{
Nan Zhanged19fc32017-10-19 13:06:22 -0700247 Rule: turbine,
248 Description: "turbine",
249 Output: outputFile,
250 Inputs: srcFiles,
251 Implicits: deps,
252 Args: map[string]string{
253 "javacFlags": flags.javacFlags,
254 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700255 "srcJars": strings.Join(srcJars.Strings(), " "),
256 "srcJarDir": android.PathForModuleOut(ctx, "turbine", "srcjars").String(),
Nan Zhanged19fc32017-10-19 13:06:22 -0700257 "classpath": flags.classpath.FormJavaClassPath("--classpath"),
258 "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
259 "javaVersion": flags.javaVersion,
260 },
261 })
262}
263
Colin Cross070879e2017-10-11 11:21:07 -0700264// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700265// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
266// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700267//
268// This method may be used for different compilers, including javac and Error Prone. The rule
269// argument specifies which command line to use and desc sets the description of the rule that will
270// be printed at build time. The stem argument provides the file name of the output jar, and
271// suffix will be appended to various intermediate files and directories to avoid collisions when
272// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700273func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700274 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700275 flags javaBuilderFlags, deps android.Paths,
Colin Cross1ee23172017-10-18 14:44:18 -0700276 intermediatesDir, desc string, rule blueprint.Rule) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700277
Colin Cross59149b62017-10-16 18:07:29 -0700278 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700279
280 var bootClasspath string
281 if flags.javaVersion == "1.9" {
282 deps = append(deps, flags.systemModules...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700283 bootClasspath = flags.systemModules.FormJavaSystemModulesPath("--system=", ctx.Device())
Colin Cross1369cdb2017-09-29 17:58:17 -0700284 } else {
285 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700286 if len(flags.bootClasspath) == 0 && ctx.Device() {
287 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
288 // ensure java does not fall back to the default bootclasspath.
289 bootClasspath = `-bootclasspath ""`
290 } else {
291 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
292 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700293 }
294
Colin Cross6ade34f2017-09-15 13:00:47 -0700295 deps = append(deps, flags.classpath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700296
Nan Zhang61eaedb2017-11-02 13:28:15 -0700297 srcJarDir := "srcjars"
298 outDir := "classes"
299 annoDir := "anno"
300 if shardIdx >= 0 {
301 shardDir := "shard" + strconv.Itoa(shardIdx)
302 srcJarDir = filepath.Join(shardDir, srcJarDir)
303 outDir = filepath.Join(shardDir, outDir)
304 annoDir = filepath.Join(shardDir, annoDir)
305 }
Colin Crossae887032017-10-23 17:16:14 -0700306 ctx.Build(pctx, android.BuildParams{
Colin Cross070879e2017-10-11 11:21:07 -0700307 Rule: rule,
308 Description: desc,
309 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700310 Inputs: srcFiles,
311 Implicits: deps,
312 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700313 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700314 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700315 "classpath": flags.classpath.FormJavaClassPath("-classpath"),
316 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700317 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
318 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
319 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross8eadbf02017-10-24 17:46:00 -0700320 "javaVersion": flags.javaVersion,
Colin Crossc6bbef32017-08-14 14:16:06 -0700321 },
322 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700323}
324
Colin Crosse9a275b2017-10-16 17:09:48 -0700325func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
326 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700327
Colin Crossae887032017-10-23 17:16:14 -0700328 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700329 Rule: jar,
330 Description: "jar",
331 Output: outputFile,
332 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700333 Args: map[string]string{
Colin Cross0a6e0072017-08-30 14:24:55 -0700334 "jarArgs": strings.Join(jarArgs, " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700335 },
336 })
Colin Cross2fe66872015-03-30 17:20:39 -0700337}
338
Nan Zhanged19fc32017-10-19 13:06:22 -0700339func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
340 jars android.Paths, manifest android.OptionalPath, stripDirs bool, dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700341
Colin Cross635acc92017-09-12 22:50:46 -0700342 var deps android.Paths
343
344 var jarArgs []string
345 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700346 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700347 deps = append(deps, manifest.Path())
348 }
349
Nan Zhanged19fc32017-10-19 13:06:22 -0700350 if dirsToStrip != nil {
351 for _, dir := range dirsToStrip {
352 jarArgs = append(jarArgs, "-stripDir ", dir)
353 }
354 }
355
Colin Cross7b60cdd2017-12-21 13:52:58 -0800356 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
357 // for downstream tools like desugar.
358 jarArgs = append(jarArgs, "-stripFile module-info.class")
359
Colin Cross635acc92017-09-12 22:50:46 -0700360 if stripDirs {
361 jarArgs = append(jarArgs, "-D")
362 }
363
Colin Crossae887032017-10-23 17:16:14 -0700364 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700365 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700366 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700367 Output: outputFile,
368 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700369 Implicits: deps,
370 Args: map[string]string{
371 "jarArgs": strings.Join(jarArgs, " "),
372 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700373 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700374}
375
Colin Crosse9a275b2017-10-16 17:09:48 -0700376func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
377 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700378 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700379 Rule: jarjar,
380 Description: "jarjar",
381 Output: outputFile,
382 Input: classesJar,
383 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700384 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700385 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700386 },
387 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700388}
Colin Cross6ade34f2017-09-15 13:00:47 -0700389
390type classpath []android.Path
391
Nan Zhanged19fc32017-10-19 13:06:22 -0700392func (x *classpath) FormJavaClassPath(optName string) string {
Colin Cross59149b62017-10-16 18:07:29 -0700393 if len(*x) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700394 return optName + " " + strings.Join(x.Strings(), ":")
Colin Cross6ade34f2017-09-15 13:00:47 -0700395 } else {
396 return ""
397 }
398}
399
Colin Cross1369cdb2017-09-29 17:58:17 -0700400// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
401// returns --system=none if the list is empty to ensure javac does not fall back to the default
402// system modules.
Nan Zhanged19fc32017-10-19 13:06:22 -0700403func (x *classpath) FormJavaSystemModulesPath(optName string, forceEmpty bool) string {
Colin Cross1369cdb2017-09-29 17:58:17 -0700404 if len(*x) > 1 {
405 panic("more than one system module")
406 } else if len(*x) == 1 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700407 return optName + strings.TrimSuffix((*x)[0].String(), "lib/modules")
Colin Cross1369cdb2017-09-29 17:58:17 -0700408 } else if forceEmpty {
Nan Zhanged19fc32017-10-19 13:06:22 -0700409 return optName + "none"
Colin Cross1369cdb2017-09-29 17:58:17 -0700410 } else {
411 return ""
412 }
413}
414
Nan Zhanged19fc32017-10-19 13:06:22 -0700415func (x *classpath) FormDesugarClasspath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700416 if x == nil || *x == nil {
417 return nil
418 }
419 flags := make([]string, len(*x))
420 for i, v := range *x {
Nan Zhanged19fc32017-10-19 13:06:22 -0700421 flags[i] = optName + " " + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700422 }
423
424 return flags
425}
426
427// Append an android.Paths to the end of the classpath list
428func (x *classpath) AddPaths(paths android.Paths) {
429 for _, path := range paths {
430 *x = append(*x, path)
431 }
432}
433
434// 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}