blob: 9086d51854ba32274765ff9de391084b693440e2 [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 Crossa4820652017-10-17 13:56:52 -070042 `${config.SoongJavacWrapper} ${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`,
Colin Crossa4820652017-10-17 13:56:52 -070047 CommandDeps: []string{"${config.JavacCmd}", "${config.SoongZipCmd}"},
48 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
49 Rspfile: "$out.rsp",
50 RspfileContent: "$in",
Colin Cross2fe66872015-03-30 17:20:39 -070051 },
Colin Cross59149b62017-10-16 18:07:29 -070052 "javacFlags", "sourcepath", "bootClasspath", "classpath", "outDir", "annoDir", "javaVersion")
Colin Cross2fe66872015-03-30 17:20:39 -070053
Colin Cross93e85952017-08-15 13:34:18 -070054 kotlinc = pctx.AndroidGomaStaticRule("kotlinc",
55 blueprint.RuleParams{
56 // TODO(ccross): kotlinc doesn't support @ file for arguments, which will limit the
57 // maximum number of input files, especially on darwin.
58 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
59 `${config.KotlincCmd} $classpath $kotlincFlags ` +
60 `-jvm-target $javaVersion -d $outDir $in && ` +
61 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
62 CommandDeps: []string{
63 "${config.KotlincCmd}",
64 "${config.KotlinCompilerJar}",
65 "${config.SoongZipCmd}",
66 },
67 },
68 "kotlincFlags", "classpath", "outDir", "javaVersion")
69
Colin Crossc6bbef32017-08-14 14:16:06 -070070 errorprone = pctx.AndroidStaticRule("errorprone",
71 blueprint.RuleParams{
72 Command: `rm -rf "$outDir" "$annoDir" && mkdir -p "$outDir" "$annoDir" && ` +
Colin Crossa4820652017-10-17 13:56:52 -070073 `${config.SoongJavacWrapper} ${config.ErrorProneCmd} ` +
Colin Cross59149b62017-10-16 18:07:29 -070074 `$javacFlags $sourcepath $bootClasspath $classpath ` +
Colin Crossc6bbef32017-08-14 14:16:06 -070075 `-source $javaVersion -target $javaVersion ` +
76 `-d $outDir -s $annoDir @$out.rsp && ` +
Colin Cross0a6e0072017-08-30 14:24:55 -070077 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
Colin Crossc6bbef32017-08-14 14:16:06 -070078 CommandDeps: []string{
79 "${config.JavaCmd}",
80 "${config.ErrorProneJavacJar}",
81 "${config.ErrorProneJar}",
Colin Cross0a6e0072017-08-30 14:24:55 -070082 "${config.SoongZipCmd}",
Colin Crossc6bbef32017-08-14 14:16:06 -070083 },
Colin Crossa4820652017-10-17 13:56:52 -070084 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
85 Rspfile: "$out.rsp",
86 RspfileContent: "$in",
Colin Crossc6bbef32017-08-14 14:16:06 -070087 },
Colin Cross59149b62017-10-16 18:07:29 -070088 "javacFlags", "sourcepath", "bootClasspath", "classpath", "outDir", "annoDir", "javaVersion")
Colin Crossc6bbef32017-08-14 14:16:06 -070089
Colin Cross9d45bb72016-08-29 16:14:13 -070090 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -070091 blueprint.RuleParams{
Colin Cross0a6e0072017-08-30 14:24:55 -070092 Command: `${config.SoongZipCmd} -jar -o $out $jarArgs`,
93 CommandDeps: []string{"${config.SoongZipCmd}"},
Colin Cross2fe66872015-03-30 17:20:39 -070094 },
Colin Cross0a6e0072017-08-30 14:24:55 -070095 "jarArgs")
96
97 combineJar = pctx.AndroidStaticRule("combineJar",
98 blueprint.RuleParams{
Colin Cross635acc92017-09-12 22:50:46 -070099 Command: `${config.MergeZipsCmd} -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700100 CommandDeps: []string{"${config.MergeZipsCmd}"},
101 },
Colin Cross635acc92017-09-12 22:50:46 -0700102 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700103
Colin Cross6ade34f2017-09-15 13:00:47 -0700104 desugar = pctx.AndroidStaticRule("desugar",
105 blueprint.RuleParams{
106 Command: `rm -rf $dumpDir && mkdir -p $dumpDir && ` +
107 `${config.JavaCmd} ` +
108 `-Djdk.internal.lambda.dumpProxyClasses=$$(cd $dumpDir && pwd) ` +
109 `$javaFlags ` +
110 `-jar ${config.DesugarJar} $classpathFlags $desugarFlags ` +
111 `-i $in -o $out`,
112 CommandDeps: []string{"${config.DesugarJar}"},
113 },
114 "javaFlags", "classpathFlags", "desugarFlags", "dumpDir")
115
Colin Cross9d45bb72016-08-29 16:14:13 -0700116 dx = pctx.AndroidStaticRule("dx",
Colin Cross2fe66872015-03-30 17:20:39 -0700117 blueprint.RuleParams{
Colin Cross6d1e72d2015-04-10 17:44:24 -0700118 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Colin Cross6ade34f2017-09-15 13:00:47 -0700119 `${config.DxCmd} --dex --output=$outDir $dxFlags $in && ` +
Colin Cross7db5d632017-10-04 17:07:09 -0700120 `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` +
121 `${config.MergeZipsCmd} -D -stripFile "*.class" $out $outDir/classes.dex.jar $in`,
Colin Cross6ade34f2017-09-15 13:00:47 -0700122 CommandDeps: []string{
123 "${config.DxCmd}",
124 "${config.SoongZipCmd}",
Colin Cross7db5d632017-10-04 17:07:09 -0700125 "${config.MergeZipsCmd}",
Colin Cross6ade34f2017-09-15 13:00:47 -0700126 },
Colin Cross2fe66872015-03-30 17:20:39 -0700127 },
128 "outDir", "dxFlags")
Colin Crosse1d62a82015-04-03 16:53:05 -0700129
Colin Cross9d45bb72016-08-29 16:14:13 -0700130 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700131 blueprint.RuleParams{
Colin Cross64162712017-08-08 13:17:59 -0700132 Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out",
133 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700134 },
135 "rulesFile")
Colin Cross2fe66872015-03-30 17:20:39 -0700136)
137
138func init() {
Colin Cross64162712017-08-08 13:17:59 -0700139 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700140}
141
142type javaBuilderFlags struct {
143 javacFlags string
144 dxFlags string
Colin Cross6ade34f2017-09-15 13:00:47 -0700145 bootClasspath classpath
146 classpath classpath
Colin Cross1369cdb2017-09-29 17:58:17 -0700147 systemModules classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700148 desugarFlags string
Colin Crossc0b06f12015-04-08 13:03:43 -0700149 aidlFlags string
Colin Cross64162712017-08-08 13:17:59 -0700150 javaVersion string
Colin Cross6af17aa2017-09-20 12:59:05 -0700151
Colin Cross93e85952017-08-15 13:34:18 -0700152 kotlincFlags string
153 kotlincClasspath classpath
154
Colin Cross6af17aa2017-09-20 12:59:05 -0700155 protoFlags string
156 protoOutFlag string
Colin Cross2fe66872015-03-30 17:20:39 -0700157}
158
Colin Cross93e85952017-08-15 13:34:18 -0700159func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
160 srcFiles android.Paths, srcJars classpath,
161 flags javaBuilderFlags) {
162
163 classDir := android.PathForModuleOut(ctx, "classes-kt")
164
165 inputs := append(android.Paths(nil), srcFiles...)
166 inputs = append(inputs, srcJars...)
167
168 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
169 Rule: kotlinc,
170 Description: "kotlinc",
171 Output: outputFile,
172 Inputs: inputs,
173 Args: map[string]string{
174 "classpath": flags.kotlincClasspath.JavaClasspath(),
175 "kotlincFlags": flags.kotlincFlags,
176 "outDir": classDir.String(),
177 "javaVersion": flags.javaVersion,
178 },
179 })
180}
181
Colin Crosse9a275b2017-10-16 17:09:48 -0700182func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross59149b62017-10-16 18:07:29 -0700183 srcFiles android.Paths, srcJars classpath,
Colin Crosse9a275b2017-10-16 17:09:48 -0700184 flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700185
Colin Cross59149b62017-10-16 18:07:29 -0700186 transformJavaToClasses(ctx, outputFile, srcFiles, srcJars, flags, deps,
Colin Crosse9a275b2017-10-16 17:09:48 -0700187 "", "javac", javac)
Colin Cross2fe66872015-03-30 17:20:39 -0700188}
189
Colin Crosse9a275b2017-10-16 17:09:48 -0700190func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross59149b62017-10-16 18:07:29 -0700191 srcFiles android.Paths, srcJars classpath,
Colin Crosse9a275b2017-10-16 17:09:48 -0700192 flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700193
Colin Crossfee57cb2017-09-05 13:16:45 -0700194 if config.ErrorProneJar == "" {
195 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
Colin Crossfee57cb2017-09-05 13:16:45 -0700196 }
197
Colin Cross59149b62017-10-16 18:07:29 -0700198 transformJavaToClasses(ctx, outputFile, srcFiles, srcJars, flags, nil,
Colin Crosse9a275b2017-10-16 17:09:48 -0700199 "-errorprone", "errorprone", errorprone)
Colin Cross070879e2017-10-11 11:21:07 -0700200}
201
202// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700203// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
204// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700205//
206// This method may be used for different compilers, including javac and Error Prone. The rule
207// argument specifies which command line to use and desc sets the description of the rule that will
208// be printed at build time. The stem argument provides the file name of the output jar, and
209// suffix will be appended to various intermediate files and directories to avoid collisions when
210// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700211func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross59149b62017-10-16 18:07:29 -0700212 srcFiles android.Paths, srcJars classpath,
Colin Crosse9a275b2017-10-16 17:09:48 -0700213 flags javaBuilderFlags, deps android.Paths,
214 intermediatesSuffix, desc string, rule blueprint.Rule) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700215
Colin Cross59149b62017-10-16 18:07:29 -0700216 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700217
218 var bootClasspath string
219 if flags.javaVersion == "1.9" {
220 deps = append(deps, flags.systemModules...)
221 bootClasspath = flags.systemModules.JavaSystemModules(ctx.Device())
222 } else {
223 deps = append(deps, flags.bootClasspath...)
224 bootClasspath = flags.bootClasspath.JavaBootClasspath(ctx.Device())
225 }
226
Colin Cross6ade34f2017-09-15 13:00:47 -0700227 deps = append(deps, flags.classpath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700228
229 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross070879e2017-10-11 11:21:07 -0700230 Rule: rule,
231 Description: desc,
232 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700233 Inputs: srcFiles,
234 Implicits: deps,
235 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700236 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700237 "bootClasspath": bootClasspath,
Colin Cross59149b62017-10-16 18:07:29 -0700238 "sourcepath": srcJars.JavaSourcepath(),
Colin Cross6ade34f2017-09-15 13:00:47 -0700239 "classpath": flags.classpath.JavaClasspath(),
Colin Crosse9a275b2017-10-16 17:09:48 -0700240 "outDir": android.PathForModuleOut(ctx, "classes"+intermediatesSuffix).String(),
241 "annoDir": android.PathForModuleOut(ctx, "anno"+intermediatesSuffix).String(),
Colin Crossc6bbef32017-08-14 14:16:06 -0700242 "javaVersion": flags.javaVersion,
243 },
244 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700245}
246
Colin Crosse9a275b2017-10-16 17:09:48 -0700247func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
248 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700249
Colin Cross635c3b02016-05-18 15:37:25 -0700250 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700251 Rule: jar,
252 Description: "jar",
253 Output: outputFile,
254 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700255 Args: map[string]string{
Colin Cross0a6e0072017-08-30 14:24:55 -0700256 "jarArgs": strings.Join(jarArgs, " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700257 },
258 })
Colin Cross2fe66872015-03-30 17:20:39 -0700259}
260
Colin Crosse9a275b2017-10-16 17:09:48 -0700261func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath,
262 jars android.Paths, manifest android.OptionalPath, stripDirs bool) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700263
Colin Cross635acc92017-09-12 22:50:46 -0700264 var deps android.Paths
265
266 var jarArgs []string
267 if manifest.Valid() {
268 jarArgs = append(jarArgs, "-m "+manifest.String())
269 deps = append(deps, manifest.Path())
270 }
271
272 if stripDirs {
273 jarArgs = append(jarArgs, "-D")
274 }
275
Colin Cross0a6e0072017-08-30 14:24:55 -0700276 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
277 Rule: combineJar,
278 Description: "combine jars",
279 Output: outputFile,
280 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700281 Implicits: deps,
282 Args: map[string]string{
283 "jarArgs": strings.Join(jarArgs, " "),
284 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700285 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700286}
287
Colin Crosse9a275b2017-10-16 17:09:48 -0700288func TransformDesugar(ctx android.ModuleContext, outputFile android.WritablePath,
289 classesJar android.Path, flags javaBuilderFlags) {
Colin Cross6ade34f2017-09-15 13:00:47 -0700290
Colin Cross6ade34f2017-09-15 13:00:47 -0700291 dumpDir := android.PathForModuleOut(ctx, "desugar_dumped_classes")
292
293 javaFlags := ""
Colin Cross1369cdb2017-09-29 17:58:17 -0700294 if ctx.AConfig().UseOpenJDK9() {
Colin Cross6ade34f2017-09-15 13:00:47 -0700295 javaFlags = "--add-opens java.base/java.lang.invoke=ALL-UNNAMED"
296 }
297
298 var desugarFlags []string
299 desugarFlags = append(desugarFlags, flags.bootClasspath.DesugarBootClasspath()...)
300 desugarFlags = append(desugarFlags, flags.classpath.DesugarClasspath()...)
301
302 var deps android.Paths
303 deps = append(deps, flags.bootClasspath...)
304 deps = append(deps, flags.classpath...)
305
306 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
307 Rule: desugar,
308 Description: "desugar",
309 Output: outputFile,
310 Input: classesJar,
311 Implicits: deps,
312 Args: map[string]string{
313 "dumpDir": dumpDir.String(),
314 "javaFlags": javaFlags,
315 "classpathFlags": strings.Join(desugarFlags, " "),
316 "desugarFlags": flags.desugarFlags,
317 },
318 })
Colin Cross6ade34f2017-09-15 13:00:47 -0700319}
320
Colin Cross7db5d632017-10-04 17:07:09 -0700321// Converts a classes.jar file to classes*.dex, then combines the dex files with any resources
322// in the classes.jar file into a dex jar.
Colin Crosse9a275b2017-10-16 17:09:48 -0700323func TransformClassesJarToDexJar(ctx android.ModuleContext, outputFile android.WritablePath,
324 classesJar android.Path, flags javaBuilderFlags) {
Colin Cross2fe66872015-03-30 17:20:39 -0700325
Colin Cross635c3b02016-05-18 15:37:25 -0700326 outDir := android.PathForModuleOut(ctx, "dex")
Colin Cross2fe66872015-03-30 17:20:39 -0700327
Colin Cross635c3b02016-05-18 15:37:25 -0700328 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700329 Rule: dx,
330 Description: "dx",
331 Output: outputFile,
332 Input: classesJar,
Colin Cross2fe66872015-03-30 17:20:39 -0700333 Args: map[string]string{
334 "dxFlags": flags.dxFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700335 "outDir": outDir.String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700336 },
337 })
Colin Cross2fe66872015-03-30 17:20:39 -0700338}
Colin Crosse1d62a82015-04-03 16:53:05 -0700339
Colin Crosse9a275b2017-10-16 17:09:48 -0700340func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
341 classesJar android.Path, rulesFile android.Path) {
Colin Cross635c3b02016-05-18 15:37:25 -0700342 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700343 Rule: jarjar,
344 Description: "jarjar",
345 Output: outputFile,
346 Input: classesJar,
347 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700348 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700349 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700350 },
351 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700352}
Colin Cross6ade34f2017-09-15 13:00:47 -0700353
354type classpath []android.Path
355
Colin Cross59149b62017-10-16 18:07:29 -0700356// Returns a -sourcepath argument in the form javac expects. If the list is empty returns
357// -sourcepath "" to ensure javac does not fall back to searching the classpath for sources.
358func (x *classpath) JavaSourcepath() string {
359 if len(*x) > 0 {
360 return "-sourcepath " + strings.Join(x.Strings(), ":")
361 } else {
362 return `-sourcepath ""`
363 }
364}
365
Colin Cross6ade34f2017-09-15 13:00:47 -0700366// Returns a -classpath argument in the form java or javac expects
367func (x *classpath) JavaClasspath() string {
368 if len(*x) > 0 {
369 return "-classpath " + strings.Join(x.Strings(), ":")
370 } else {
371 return ""
372 }
373}
374
375// Returns a -processorpath argument in the form java or javac expects
376func (x *classpath) JavaProcessorpath() string {
377 if len(*x) > 0 {
378 return "-processorpath " + strings.Join(x.Strings(), ":")
379 } else {
380 return ""
381 }
382}
383
384// Returns a -bootclasspath argument in the form java or javac expects. If forceEmpty is true,
385// returns -bootclasspath "" if the bootclasspath is empty to ensure javac does not fall back to the
386// default bootclasspath.
387func (x *classpath) JavaBootClasspath(forceEmpty bool) string {
388 if len(*x) > 0 {
389 return "-bootclasspath " + strings.Join(x.Strings(), ":")
390 } else if forceEmpty {
391 return `-bootclasspath ""`
392 } else {
393 return ""
394 }
395}
396
Colin Cross1369cdb2017-09-29 17:58:17 -0700397// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
398// returns --system=none if the list is empty to ensure javac does not fall back to the default
399// system modules.
400func (x *classpath) JavaSystemModules(forceEmpty bool) string {
401 if len(*x) > 1 {
402 panic("more than one system module")
403 } else if len(*x) == 1 {
404 return "--system=" + strings.TrimSuffix((*x)[0].String(), "lib/modules")
405 } else if forceEmpty {
406 return "--system=none"
407 } else {
408 return ""
409 }
410}
411
Colin Cross6ade34f2017-09-15 13:00:47 -0700412func (x *classpath) DesugarBootClasspath() []string {
413 if x == nil || *x == nil {
414 return nil
415 }
416 flags := make([]string, len(*x))
417 for i, v := range *x {
418 flags[i] = "--bootclasspath_entry " + v.String()
419 }
420
421 return flags
422}
423
424func (x *classpath) DesugarClasspath() []string {
425 if x == nil || *x == nil {
426 return nil
427 }
428 flags := make([]string, len(*x))
429 for i, v := range *x {
430 flags[i] = "--classpath_entry " + v.String()
431 }
432
433 return flags
434}
435
436// Append an android.Paths to the end of the classpath list
437func (x *classpath) AddPaths(paths android.Paths) {
438 for _, path := range paths {
439 *x = append(*x, path)
440 }
441}
442
443// Convert a classpath to an android.Paths
444func (x *classpath) Paths() android.Paths {
445 return append(android.Paths(nil), (*x)...)
446}
447
448func (x *classpath) Strings() []string {
449 if x == nil {
450 return nil
451 }
452 ret := make([]string, len(*x))
453 for i, path := range *x {
454 ret[i] = path.String()
455 }
456 return ret
457}