blob: a03f89271d1eeea2ab60d27899a2816ccd9755c1 [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 (
Colin Cross2fe66872015-03-30 17:20:39 -070022 "strings"
23
Colin Cross2fe66872015-03-30 17:20:39 -070024 "github.com/google/blueprint"
Colin Crossfee57cb2017-09-05 13:16:45 -070025
26 "android/soong/android"
27 "android/soong/java/config"
Colin Cross2fe66872015-03-30 17:20:39 -070028)
29
30var (
Colin Cross635c3b02016-05-18 15:37:25 -070031 pctx = android.NewPackageContext("android/soong/java")
Colin Cross2fe66872015-03-30 17:20:39 -070032
33 // Compiling java is not conducive to proper dependency tracking. The path-matches-class-name
34 // requirement leads to unpredictable generated source file names, and a single .java file
35 // will get compiled into multiple .class files if it contains inner classes. To work around
36 // this, all java rules write into separate directories and then a post-processing step lists
37 // the files in the the directory into a list file that later rules depend on (and sometimes
38 // read from directly using @<listfile>)
Yoshisato Yanagisawa572324a2017-06-05 17:41:50 +090039 javac = pctx.AndroidGomaStaticRule("javac",
Colin Cross2fe66872015-03-30 17:20:39 -070040 blueprint.RuleParams{
Colin Cross64162712017-08-08 13:17:59 -070041 Command: `rm -rf "$outDir" "$annoDir" && mkdir -p "$outDir" "$annoDir" && ` +
Colin Cross3203dde2017-08-28 17:23:21 -070042 `${config.JavacWrapper}${config.JavacCmd} ${config.JavacHeapFlags} ${config.CommonJdkFlags} ` +
Colin Cross59149b62017-10-16 18:07:29 -070043 `$javacFlags $sourcepath $bootClasspath $classpath ` +
Colin Cross64162712017-08-08 13:17:59 -070044 `-source $javaVersion -target $javaVersion ` +
Colin Crossb852a582017-08-10 17:58:12 -070045 `-d $outDir -s $annoDir @$out.rsp && ` +
Colin Cross0a6e0072017-08-30 14:24:55 -070046 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
47 CommandDeps: []string{"${config.JavacCmd}", "${config.SoongZipCmd}"},
Colin Cross2fe66872015-03-30 17:20:39 -070048 Rspfile: "$out.rsp",
49 RspfileContent: "$in",
Colin Cross2fe66872015-03-30 17:20:39 -070050 },
Colin Cross59149b62017-10-16 18:07:29 -070051 "javacFlags", "sourcepath", "bootClasspath", "classpath", "outDir", "annoDir", "javaVersion")
Colin Cross2fe66872015-03-30 17:20:39 -070052
Colin Cross93e85952017-08-15 13:34:18 -070053 kotlinc = pctx.AndroidGomaStaticRule("kotlinc",
54 blueprint.RuleParams{
55 // TODO(ccross): kotlinc doesn't support @ file for arguments, which will limit the
56 // maximum number of input files, especially on darwin.
57 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
58 `${config.KotlincCmd} $classpath $kotlincFlags ` +
59 `-jvm-target $javaVersion -d $outDir $in && ` +
60 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
61 CommandDeps: []string{
62 "${config.KotlincCmd}",
63 "${config.KotlinCompilerJar}",
64 "${config.SoongZipCmd}",
65 },
66 },
67 "kotlincFlags", "classpath", "outDir", "javaVersion")
68
Colin Crossc6bbef32017-08-14 14:16:06 -070069 errorprone = pctx.AndroidStaticRule("errorprone",
70 blueprint.RuleParams{
71 Command: `rm -rf "$outDir" "$annoDir" && mkdir -p "$outDir" "$annoDir" && ` +
Colin Cross3a5de652017-09-11 23:10:21 -070072 `${config.ErrorProneCmd} ` +
Colin Cross59149b62017-10-16 18:07:29 -070073 `$javacFlags $sourcepath $bootClasspath $classpath ` +
Colin Crossc6bbef32017-08-14 14:16:06 -070074 `-source $javaVersion -target $javaVersion ` +
75 `-d $outDir -s $annoDir @$out.rsp && ` +
Colin Cross0a6e0072017-08-30 14:24:55 -070076 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
Colin Crossc6bbef32017-08-14 14:16:06 -070077 CommandDeps: []string{
78 "${config.JavaCmd}",
79 "${config.ErrorProneJavacJar}",
80 "${config.ErrorProneJar}",
Colin Cross0a6e0072017-08-30 14:24:55 -070081 "${config.SoongZipCmd}",
Colin Crossc6bbef32017-08-14 14:16:06 -070082 },
83 Rspfile: "$out.rsp",
84 RspfileContent: "$in",
85 },
Colin Cross59149b62017-10-16 18:07:29 -070086 "javacFlags", "sourcepath", "bootClasspath", "classpath", "outDir", "annoDir", "javaVersion")
Colin Crossc6bbef32017-08-14 14:16:06 -070087
Colin Cross9d45bb72016-08-29 16:14:13 -070088 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -070089 blueprint.RuleParams{
Colin Cross0a6e0072017-08-30 14:24:55 -070090 Command: `${config.SoongZipCmd} -jar -o $out $jarArgs`,
91 CommandDeps: []string{"${config.SoongZipCmd}"},
Colin Cross2fe66872015-03-30 17:20:39 -070092 },
Colin Cross0a6e0072017-08-30 14:24:55 -070093 "jarArgs")
94
95 combineJar = pctx.AndroidStaticRule("combineJar",
96 blueprint.RuleParams{
Colin Cross635acc92017-09-12 22:50:46 -070097 Command: `${config.MergeZipsCmd} -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -070098 CommandDeps: []string{"${config.MergeZipsCmd}"},
99 },
Colin Cross635acc92017-09-12 22:50:46 -0700100 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700101
Colin Cross6ade34f2017-09-15 13:00:47 -0700102 desugar = pctx.AndroidStaticRule("desugar",
103 blueprint.RuleParams{
104 Command: `rm -rf $dumpDir && mkdir -p $dumpDir && ` +
105 `${config.JavaCmd} ` +
106 `-Djdk.internal.lambda.dumpProxyClasses=$$(cd $dumpDir && pwd) ` +
107 `$javaFlags ` +
108 `-jar ${config.DesugarJar} $classpathFlags $desugarFlags ` +
109 `-i $in -o $out`,
110 CommandDeps: []string{"${config.DesugarJar}"},
111 },
112 "javaFlags", "classpathFlags", "desugarFlags", "dumpDir")
113
Colin Cross9d45bb72016-08-29 16:14:13 -0700114 dx = pctx.AndroidStaticRule("dx",
Colin Cross2fe66872015-03-30 17:20:39 -0700115 blueprint.RuleParams{
Colin Cross6d1e72d2015-04-10 17:44:24 -0700116 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Colin Cross6ade34f2017-09-15 13:00:47 -0700117 `${config.DxCmd} --dex --output=$outDir $dxFlags $in && ` +
Colin Cross7db5d632017-10-04 17:07:09 -0700118 `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` +
119 `${config.MergeZipsCmd} -D -stripFile "*.class" $out $outDir/classes.dex.jar $in`,
Colin Cross6ade34f2017-09-15 13:00:47 -0700120 CommandDeps: []string{
121 "${config.DxCmd}",
122 "${config.SoongZipCmd}",
Colin Cross7db5d632017-10-04 17:07:09 -0700123 "${config.MergeZipsCmd}",
Colin Cross6ade34f2017-09-15 13:00:47 -0700124 },
Colin Cross2fe66872015-03-30 17:20:39 -0700125 },
126 "outDir", "dxFlags")
Colin Crosse1d62a82015-04-03 16:53:05 -0700127
Colin Cross9d45bb72016-08-29 16:14:13 -0700128 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700129 blueprint.RuleParams{
Colin Cross64162712017-08-08 13:17:59 -0700130 Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out",
131 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700132 },
133 "rulesFile")
Colin Cross2fe66872015-03-30 17:20:39 -0700134)
135
136func init() {
Colin Cross64162712017-08-08 13:17:59 -0700137 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700138}
139
140type javaBuilderFlags struct {
141 javacFlags string
142 dxFlags string
Colin Cross6ade34f2017-09-15 13:00:47 -0700143 bootClasspath classpath
144 classpath classpath
Colin Cross1369cdb2017-09-29 17:58:17 -0700145 systemModules classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700146 desugarFlags string
Colin Crossc0b06f12015-04-08 13:03:43 -0700147 aidlFlags string
Colin Cross64162712017-08-08 13:17:59 -0700148 javaVersion string
Colin Cross6af17aa2017-09-20 12:59:05 -0700149
Colin Cross93e85952017-08-15 13:34:18 -0700150 kotlincFlags string
151 kotlincClasspath classpath
152
Colin Cross6af17aa2017-09-20 12:59:05 -0700153 protoFlags string
154 protoOutFlag string
Colin Cross2fe66872015-03-30 17:20:39 -0700155}
156
Colin Cross93e85952017-08-15 13:34:18 -0700157func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
158 srcFiles android.Paths, srcJars classpath,
159 flags javaBuilderFlags) {
160
161 classDir := android.PathForModuleOut(ctx, "classes-kt")
162
163 inputs := append(android.Paths(nil), srcFiles...)
164 inputs = append(inputs, srcJars...)
165
166 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
167 Rule: kotlinc,
168 Description: "kotlinc",
169 Output: outputFile,
170 Inputs: inputs,
171 Args: map[string]string{
172 "classpath": flags.kotlincClasspath.JavaClasspath(),
173 "kotlincFlags": flags.kotlincFlags,
174 "outDir": classDir.String(),
175 "javaVersion": flags.javaVersion,
176 },
177 })
178}
179
Colin Crosse9a275b2017-10-16 17:09:48 -0700180func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross59149b62017-10-16 18:07:29 -0700181 srcFiles android.Paths, srcJars classpath,
Colin Crosse9a275b2017-10-16 17:09:48 -0700182 flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700183
Colin Cross59149b62017-10-16 18:07:29 -0700184 transformJavaToClasses(ctx, outputFile, srcFiles, srcJars, flags, deps,
Colin Crosse9a275b2017-10-16 17:09:48 -0700185 "", "javac", javac)
Colin Cross2fe66872015-03-30 17:20:39 -0700186}
187
Colin Crosse9a275b2017-10-16 17:09:48 -0700188func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross59149b62017-10-16 18:07:29 -0700189 srcFiles android.Paths, srcJars classpath,
Colin Crosse9a275b2017-10-16 17:09:48 -0700190 flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700191
Colin Crossfee57cb2017-09-05 13:16:45 -0700192 if config.ErrorProneJar == "" {
193 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
Colin Crossfee57cb2017-09-05 13:16:45 -0700194 }
195
Colin Cross59149b62017-10-16 18:07:29 -0700196 transformJavaToClasses(ctx, outputFile, srcFiles, srcJars, flags, nil,
Colin Crosse9a275b2017-10-16 17:09:48 -0700197 "-errorprone", "errorprone", errorprone)
Colin Cross070879e2017-10-11 11:21:07 -0700198}
199
200// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700201// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
202// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700203//
204// This method may be used for different compilers, including javac and Error Prone. The rule
205// argument specifies which command line to use and desc sets the description of the rule that will
206// be printed at build time. The stem argument provides the file name of the output jar, and
207// suffix will be appended to various intermediate files and directories to avoid collisions when
208// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700209func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross59149b62017-10-16 18:07:29 -0700210 srcFiles android.Paths, srcJars classpath,
Colin Crosse9a275b2017-10-16 17:09:48 -0700211 flags javaBuilderFlags, deps android.Paths,
212 intermediatesSuffix, desc string, rule blueprint.Rule) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700213
Colin Cross59149b62017-10-16 18:07:29 -0700214 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700215
216 var bootClasspath string
217 if flags.javaVersion == "1.9" {
218 deps = append(deps, flags.systemModules...)
219 bootClasspath = flags.systemModules.JavaSystemModules(ctx.Device())
220 } else {
221 deps = append(deps, flags.bootClasspath...)
222 bootClasspath = flags.bootClasspath.JavaBootClasspath(ctx.Device())
223 }
224
Colin Cross6ade34f2017-09-15 13:00:47 -0700225 deps = append(deps, flags.classpath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700226
227 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross070879e2017-10-11 11:21:07 -0700228 Rule: rule,
229 Description: desc,
230 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700231 Inputs: srcFiles,
232 Implicits: deps,
233 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700234 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700235 "bootClasspath": bootClasspath,
Colin Cross59149b62017-10-16 18:07:29 -0700236 "sourcepath": srcJars.JavaSourcepath(),
Colin Cross6ade34f2017-09-15 13:00:47 -0700237 "classpath": flags.classpath.JavaClasspath(),
Colin Crosse9a275b2017-10-16 17:09:48 -0700238 "outDir": android.PathForModuleOut(ctx, "classes"+intermediatesSuffix).String(),
239 "annoDir": android.PathForModuleOut(ctx, "anno"+intermediatesSuffix).String(),
Colin Crossc6bbef32017-08-14 14:16:06 -0700240 "javaVersion": flags.javaVersion,
241 },
242 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700243}
244
Colin Crosse9a275b2017-10-16 17:09:48 -0700245func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
246 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700247
Colin Cross635c3b02016-05-18 15:37:25 -0700248 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700249 Rule: jar,
250 Description: "jar",
251 Output: outputFile,
252 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700253 Args: map[string]string{
Colin Cross0a6e0072017-08-30 14:24:55 -0700254 "jarArgs": strings.Join(jarArgs, " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700255 },
256 })
Colin Cross2fe66872015-03-30 17:20:39 -0700257}
258
Colin Crosse9a275b2017-10-16 17:09:48 -0700259func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath,
260 jars android.Paths, manifest android.OptionalPath, stripDirs bool) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700261
Colin Cross635acc92017-09-12 22:50:46 -0700262 var deps android.Paths
263
264 var jarArgs []string
265 if manifest.Valid() {
266 jarArgs = append(jarArgs, "-m "+manifest.String())
267 deps = append(deps, manifest.Path())
268 }
269
270 if stripDirs {
271 jarArgs = append(jarArgs, "-D")
272 }
273
Colin Cross0a6e0072017-08-30 14:24:55 -0700274 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
275 Rule: combineJar,
276 Description: "combine jars",
277 Output: outputFile,
278 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700279 Implicits: deps,
280 Args: map[string]string{
281 "jarArgs": strings.Join(jarArgs, " "),
282 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700283 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700284}
285
Colin Crosse9a275b2017-10-16 17:09:48 -0700286func TransformDesugar(ctx android.ModuleContext, outputFile android.WritablePath,
287 classesJar android.Path, flags javaBuilderFlags) {
Colin Cross6ade34f2017-09-15 13:00:47 -0700288
Colin Cross6ade34f2017-09-15 13:00:47 -0700289 dumpDir := android.PathForModuleOut(ctx, "desugar_dumped_classes")
290
291 javaFlags := ""
Colin Cross1369cdb2017-09-29 17:58:17 -0700292 if ctx.AConfig().UseOpenJDK9() {
Colin Cross6ade34f2017-09-15 13:00:47 -0700293 javaFlags = "--add-opens java.base/java.lang.invoke=ALL-UNNAMED"
294 }
295
296 var desugarFlags []string
297 desugarFlags = append(desugarFlags, flags.bootClasspath.DesugarBootClasspath()...)
298 desugarFlags = append(desugarFlags, flags.classpath.DesugarClasspath()...)
299
300 var deps android.Paths
301 deps = append(deps, flags.bootClasspath...)
302 deps = append(deps, flags.classpath...)
303
304 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
305 Rule: desugar,
306 Description: "desugar",
307 Output: outputFile,
308 Input: classesJar,
309 Implicits: deps,
310 Args: map[string]string{
311 "dumpDir": dumpDir.String(),
312 "javaFlags": javaFlags,
313 "classpathFlags": strings.Join(desugarFlags, " "),
314 "desugarFlags": flags.desugarFlags,
315 },
316 })
Colin Cross6ade34f2017-09-15 13:00:47 -0700317}
318
Colin Cross7db5d632017-10-04 17:07:09 -0700319// Converts a classes.jar file to classes*.dex, then combines the dex files with any resources
320// in the classes.jar file into a dex jar.
Colin Crosse9a275b2017-10-16 17:09:48 -0700321func TransformClassesJarToDexJar(ctx android.ModuleContext, outputFile android.WritablePath,
322 classesJar android.Path, flags javaBuilderFlags) {
Colin Cross2fe66872015-03-30 17:20:39 -0700323
Colin Cross635c3b02016-05-18 15:37:25 -0700324 outDir := android.PathForModuleOut(ctx, "dex")
Colin Cross2fe66872015-03-30 17:20:39 -0700325
Colin Cross635c3b02016-05-18 15:37:25 -0700326 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700327 Rule: dx,
328 Description: "dx",
329 Output: outputFile,
330 Input: classesJar,
Colin Cross2fe66872015-03-30 17:20:39 -0700331 Args: map[string]string{
332 "dxFlags": flags.dxFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700333 "outDir": outDir.String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700334 },
335 })
Colin Cross2fe66872015-03-30 17:20:39 -0700336}
Colin Crosse1d62a82015-04-03 16:53:05 -0700337
Colin Crosse9a275b2017-10-16 17:09:48 -0700338func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
339 classesJar android.Path, rulesFile android.Path) {
Colin Cross635c3b02016-05-18 15:37:25 -0700340 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700341 Rule: jarjar,
342 Description: "jarjar",
343 Output: outputFile,
344 Input: classesJar,
345 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700346 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700347 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700348 },
349 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700350}
Colin Cross6ade34f2017-09-15 13:00:47 -0700351
352type classpath []android.Path
353
Colin Cross59149b62017-10-16 18:07:29 -0700354// Returns a -sourcepath argument in the form javac expects. If the list is empty returns
355// -sourcepath "" to ensure javac does not fall back to searching the classpath for sources.
356func (x *classpath) JavaSourcepath() string {
357 if len(*x) > 0 {
358 return "-sourcepath " + strings.Join(x.Strings(), ":")
359 } else {
360 return `-sourcepath ""`
361 }
362}
363
Colin Cross6ade34f2017-09-15 13:00:47 -0700364// Returns a -classpath argument in the form java or javac expects
365func (x *classpath) JavaClasspath() string {
366 if len(*x) > 0 {
367 return "-classpath " + strings.Join(x.Strings(), ":")
368 } else {
369 return ""
370 }
371}
372
373// Returns a -processorpath argument in the form java or javac expects
374func (x *classpath) JavaProcessorpath() string {
375 if len(*x) > 0 {
376 return "-processorpath " + strings.Join(x.Strings(), ":")
377 } else {
378 return ""
379 }
380}
381
382// Returns a -bootclasspath argument in the form java or javac expects. If forceEmpty is true,
383// returns -bootclasspath "" if the bootclasspath is empty to ensure javac does not fall back to the
384// default bootclasspath.
385func (x *classpath) JavaBootClasspath(forceEmpty bool) string {
386 if len(*x) > 0 {
387 return "-bootclasspath " + strings.Join(x.Strings(), ":")
388 } else if forceEmpty {
389 return `-bootclasspath ""`
390 } else {
391 return ""
392 }
393}
394
Colin Cross1369cdb2017-09-29 17:58:17 -0700395// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
396// returns --system=none if the list is empty to ensure javac does not fall back to the default
397// system modules.
398func (x *classpath) JavaSystemModules(forceEmpty bool) string {
399 if len(*x) > 1 {
400 panic("more than one system module")
401 } else if len(*x) == 1 {
402 return "--system=" + strings.TrimSuffix((*x)[0].String(), "lib/modules")
403 } else if forceEmpty {
404 return "--system=none"
405 } else {
406 return ""
407 }
408}
409
Colin Cross6ade34f2017-09-15 13:00:47 -0700410func (x *classpath) DesugarBootClasspath() []string {
411 if x == nil || *x == nil {
412 return nil
413 }
414 flags := make([]string, len(*x))
415 for i, v := range *x {
416 flags[i] = "--bootclasspath_entry " + v.String()
417 }
418
419 return flags
420}
421
422func (x *classpath) DesugarClasspath() []string {
423 if x == nil || *x == nil {
424 return nil
425 }
426 flags := make([]string, len(*x))
427 for i, v := range *x {
428 flags[i] = "--classpath_entry " + v.String()
429 }
430
431 return flags
432}
433
434// Append an android.Paths to the end of the classpath list
435func (x *classpath) AddPaths(paths android.Paths) {
436 for _, path := range paths {
437 *x = append(*x, path)
438 }
439}
440
441// Convert a classpath to an android.Paths
442func (x *classpath) Paths() android.Paths {
443 return append(android.Paths(nil), (*x)...)
444}
445
446func (x *classpath) Strings() []string {
447 if x == nil {
448 return nil
449 }
450 ret := make([]string, len(*x))
451 for i, path := range *x {
452 ret[i] = path.String()
453 }
454 return ret
455}