blob: c67a26d6952f3b5c15e124914f5704c48679a107 [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 Crossfe4bc362018-09-12 10:02:13 -070027 "github.com/google/blueprint/proptools"
Colin Crossfee57cb2017-09-05 13:16:45 -070028
29 "android/soong/android"
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 && ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090045 `(if [ -s $srcJarDir/list ] || [ -s $out.rsp ] ; then ` +
Sasha Smundak26c6d9e2019-06-11 13:30:13 -070046 `${config.SoongJavacWrapper} ${config.JavacWrapper}${config.JavacCmd} ` +
47 `${config.JavacHeapFlags} ${config.JavacVmFlags} ${config.CommonJdkFlags} ` +
Colin Crossbe9cdb82019-01-21 21:37:16 -080048 `$processorpath $processor $javacFlags $bootClasspath $classpath ` +
Colin Cross64162712017-08-08 13:17:59 -070049 `-source $javaVersion -target $javaVersion ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090050 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list ; fi ) && ` +
Colin Cross44c29a82019-01-24 16:36:57 -080051 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir && ` +
52 `rm -rf "$srcJarDir"`,
Colin Cross8eadbf02017-10-24 17:46:00 -070053 CommandDeps: []string{
54 "${config.JavacCmd}",
55 "${config.SoongZipCmd}",
Colin Cross436b7652018-03-15 16:24:10 -070056 "${config.ZipSyncCmd}",
Colin Cross8eadbf02017-10-24 17:46:00 -070057 },
Colin Crossa4820652017-10-17 13:56:52 -070058 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
59 Rspfile: "$out.rsp",
60 RspfileContent: "$in",
Colin Cross2fe66872015-03-30 17:20:39 -070061 },
Colin Crossbe9cdb82019-01-21 21:37:16 -080062 "javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
Colin Cross8eadbf02017-10-24 17:46:00 -070063 "outDir", "annoDir", "javaVersion")
Colin Cross2fe66872015-03-30 17:20:39 -070064
Sasha Smundak2a4549e2018-11-05 16:49:08 -080065 _ = pctx.VariableFunc("kytheCorpus",
66 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() })
Sasha Smundak65143642019-09-26 20:14:28 -070067 _ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json")
Sasha Smundak2a4549e2018-11-05 16:49:08 -080068 // Run it with -add-opens=java.base/java.nio=ALL-UNNAMED to avoid JDK9's warning about
69 // "Illegal reflective access by com.google.protobuf.Utf8$UnsafeProcessor ...
70 // to field java.nio.Buffer.address"
71 kytheExtract = pctx.AndroidStaticRule("kythe",
72 blueprint.RuleParams{
73 Command: `${config.ZipSyncCmd} -d $srcJarDir ` +
74 `-l $srcJarDir/list -f "*.java" $srcJars && ` +
75 `( [ ! -s $srcJarDir/list -a ! -s $out.rsp ] || ` +
76 `KYTHE_ROOT_DIRECTORY=. KYTHE_OUTPUT_FILE=$out ` +
77 `KYTHE_CORPUS=${kytheCorpus} ` +
Sasha Smundak65143642019-09-26 20:14:28 -070078 `KYTHE_VNAMES=${kytheVnames} ` +
Sasha Smundak2a4549e2018-11-05 16:49:08 -080079 `${config.SoongJavacWrapper} ${config.JavaCmd} ` +
80 `--add-opens=java.base/java.nio=ALL-UNNAMED ` +
81 `-jar ${config.JavaKytheExtractorJar} ` +
82 `${config.JavacHeapFlags} ${config.CommonJdkFlags} ` +
83 `$processorpath $processor $javacFlags $bootClasspath $classpath ` +
84 `-source $javaVersion -target $javaVersion ` +
85 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list)`,
86 CommandDeps: []string{
87 "${config.JavaCmd}",
88 "${config.JavaKytheExtractorJar}",
Sasha Smundak65143642019-09-26 20:14:28 -070089 "${kytheVnames}",
Sasha Smundak2a4549e2018-11-05 16:49:08 -080090 "${config.ZipSyncCmd}",
91 },
92 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
93 Rspfile: "$out.rsp",
94 RspfileContent: "$in",
95 },
96 "javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
97 "outDir", "annoDir", "javaVersion")
98
Nan Zhanged19fc32017-10-19 13:06:22 -070099 turbine = pctx.AndroidStaticRule("turbine",
100 blueprint.RuleParams{
Colin Cross6981f652018-03-07 15:14:50 -0800101 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Sasha Smundak26c6d9e2019-06-11 13:30:13 -0700102 `${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.TurbineJar} --output $out.tmp ` +
Colin Cross6981f652018-03-07 15:14:50 -0800103 `--temp_dir "$outDir" --sources @$out.rsp --source_jars $srcJars ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700104 `--javacopts ${config.CommonJdkFlags} ` +
Colin Cross924a0aa2018-03-07 10:51:05 -0800105 `$javacFlags -source $javaVersion -target $javaVersion -- $bootClasspath $classpath && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700106 `${config.Ziptime} $out.tmp && ` +
107 `(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`,
Colin Cross8eadbf02017-10-24 17:46:00 -0700108 CommandDeps: []string{
109 "${config.TurbineJar}",
110 "${config.JavaCmd}",
111 "${config.Ziptime}",
Colin Cross8eadbf02017-10-24 17:46:00 -0700112 },
Nan Zhanged19fc32017-10-19 13:06:22 -0700113 Rspfile: "$out.rsp",
114 RspfileContent: "$in",
115 Restat: true,
116 },
Colin Cross6981f652018-03-07 15:14:50 -0800117 "javacFlags", "bootClasspath", "classpath", "srcJars", "outDir", "javaVersion")
Nan Zhanged19fc32017-10-19 13:06:22 -0700118
Colin Cross9d45bb72016-08-29 16:14:13 -0700119 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -0700120 blueprint.RuleParams{
Nan Zhang674dd932018-01-26 18:30:36 -0800121 Command: `${config.SoongZipCmd} -jar -o $out @$out.rsp`,
122 CommandDeps: []string{"${config.SoongZipCmd}"},
123 Rspfile: "$out.rsp",
124 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -0700125 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700126 "jarArgs")
127
Colin Crossa4f08812018-10-02 22:03:40 -0700128 zip = pctx.AndroidStaticRule("zip",
129 blueprint.RuleParams{
130 Command: `${config.SoongZipCmd} -o $out @$out.rsp`,
131 CommandDeps: []string{"${config.SoongZipCmd}"},
132 Rspfile: "$out.rsp",
133 RspfileContent: "$jarArgs",
134 },
135 "jarArgs")
136
Colin Cross0a6e0072017-08-30 14:24:55 -0700137 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{
Sasha Smundak26c6d9e2019-06-11 13:30:13 -0700146 Command: "${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JarjarCmd} process $rulesFile $in $out",
Colin Cross64162712017-08-08 13:17:59 -0700147 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700148 },
149 "rulesFile")
Nan Zhang4c819fb2018-08-27 18:31:46 -0700150
Vladimir Marko0975ee02019-04-02 10:29:55 +0100151 packageCheck = pctx.AndroidStaticRule("packageCheck",
152 blueprint.RuleParams{
153 Command: "rm -f $out && " +
154 "${config.PackageCheckCmd} $in $packages && " +
155 "touch $out",
156 CommandDeps: []string{"${config.PackageCheckCmd}"},
157 },
158 "packages")
159
Nan Zhang4c819fb2018-08-27 18:31:46 -0700160 jetifier = pctx.AndroidStaticRule("jetifier",
161 blueprint.RuleParams{
Sasha Smundak26c6d9e2019-06-11 13:30:13 -0700162 Command: "${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JetifierJar} -l error -o $out -i $in",
Nan Zhang4c819fb2018-08-27 18:31:46 -0700163 CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"},
164 },
165 )
Colin Cross43f08db2018-11-12 10:13:39 -0800166
167 zipalign = pctx.AndroidStaticRule("zipalign",
168 blueprint.RuleParams{
Colin Crosse4246ab2019-02-05 21:55:21 -0800169 Command: "if ! ${config.ZipAlign} -c -p 4 $in > /dev/null; then " +
170 "${config.ZipAlign} -f -p 4 $in $out; " +
Colin Cross43f08db2018-11-12 10:13:39 -0800171 "else " +
172 "cp -f $in $out; " +
173 "fi",
174 CommandDeps: []string{"${config.ZipAlign}"},
175 },
176 )
Colin Cross2fe66872015-03-30 17:20:39 -0700177)
178
179func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700180 pctx.Import("android/soong/android")
Colin Cross64162712017-08-08 13:17:59 -0700181 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700182}
183
184type javaBuilderFlags struct {
Colin Crossb77043e2019-07-16 13:57:13 -0700185 javacFlags string
186 bootClasspath classpath
187 classpath classpath
188 processorPath classpath
189 processor string
190 systemModules *systemModules
191 aidlFlags string
192 aidlDeps android.Paths
Colin Cross1e743852019-10-28 11:37:20 -0700193 javaVersion javaVersion
Colin Cross6af17aa2017-09-20 12:59:05 -0700194
Andreas Gampef3e5b552018-01-22 21:27:21 -0800195 errorProneExtraJavacFlags string
Colin Cross66548102018-06-19 22:47:35 -0700196 errorProneProcessorPath classpath
Andreas Gampef3e5b552018-01-22 21:27:21 -0800197
Colin Cross93e85952017-08-15 13:34:18 -0700198 kotlincFlags string
199 kotlincClasspath classpath
200
Colin Cross19878da2019-03-28 14:45:07 -0700201 proto android.ProtoFlags
Colin Cross2fe66872015-03-30 17:20:39 -0700202}
203
Nan Zhang61eaedb2017-11-02 13:28:15 -0700204func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
205 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700206
Nan Zhang61eaedb2017-11-02 13:28:15 -0700207 // Compile java sources into .class files
208 desc := "javac"
209 if shardIdx >= 0 {
210 desc += strconv.Itoa(shardIdx)
211 }
212
Colin Cross66548102018-06-19 22:47:35 -0700213 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc)
Colin Cross2fe66872015-03-30 17:20:39 -0700214}
215
Colin Crosse9a275b2017-10-16 17:09:48 -0700216func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700217 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700218
Colin Cross66548102018-06-19 22:47:35 -0700219 flags.processorPath = append(flags.errorProneProcessorPath, flags.processorPath...)
Colin Crossfee57cb2017-09-05 13:16:45 -0700220
Andreas Gampef3e5b552018-01-22 21:27:21 -0800221 if len(flags.errorProneExtraJavacFlags) > 0 {
222 if len(flags.javacFlags) > 0 {
Colin Cross66548102018-06-19 22:47:35 -0700223 flags.javacFlags += " " + flags.errorProneExtraJavacFlags
Andreas Gampef3e5b552018-01-22 21:27:21 -0800224 } 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 Cross66548102018-06-19 22:47:35 -0700230 "errorprone", "errorprone")
Colin Cross070879e2017-10-11 11:21:07 -0700231}
232
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800233// Emits the rule to generate Xref input file (.kzip file) for the given set of source files and source jars
234// to compile with given set of builder flags, etc.
Colin Cross3b706fd2019-09-05 16:44:18 -0700235func emitXrefRule(ctx android.ModuleContext, xrefFile android.WritablePath, idx int,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800236 srcFiles, srcJars android.Paths,
Colin Cross3b706fd2019-09-05 16:44:18 -0700237 flags javaBuilderFlags, deps android.Paths) {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800238
239 deps = append(deps, srcJars...)
240
241 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700242 if flags.javaVersion.usesJavaModules() {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800243 var systemModuleDeps android.Paths
244 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
245 deps = append(deps, systemModuleDeps...)
246 } else {
247 deps = append(deps, flags.bootClasspath...)
248 if len(flags.bootClasspath) == 0 && ctx.Device() {
249 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
250 // ensure java does not fall back to the default bootclasspath.
251 bootClasspath = `-bootclasspath ""`
252 } else {
253 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
254 }
255 }
256
257 deps = append(deps, flags.classpath...)
258 deps = append(deps, flags.processorPath...)
259
260 processor := "-proc:none"
261 if flags.processor != "" {
262 processor = "-processor " + flags.processor
263 }
264
Colin Cross3b706fd2019-09-05 16:44:18 -0700265 intermediatesDir := "xref"
266 if idx >= 0 {
267 intermediatesDir += strconv.Itoa(idx)
268 }
269
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800270 ctx.Build(pctx,
271 android.BuildParams{
272 Rule: kytheExtract,
273 Description: "Xref Java extractor",
274 Output: xrefFile,
275 Inputs: srcFiles,
276 Implicits: deps,
277 Args: map[string]string{
278 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, "anno").String(),
279 "bootClasspath": bootClasspath,
280 "classpath": flags.classpath.FormJavaClassPath("-classpath"),
281 "javacFlags": flags.javacFlags,
Colin Cross1e743852019-10-28 11:37:20 -0700282 "javaVersion": flags.javaVersion.String(),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800283 "outDir": android.PathForModuleOut(ctx, "javac", "classes.xref").String(),
284 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
285 "processor": processor,
286 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, "srcjars.xref").String(),
287 "srcJars": strings.Join(srcJars.Strings(), " "),
288 },
289 })
290}
291
Nan Zhanged19fc32017-10-19 13:06:22 -0700292func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700293 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700294
295 var deps android.Paths
296 deps = append(deps, srcJars...)
297 deps = append(deps, flags.bootClasspath...)
298 deps = append(deps, flags.classpath...)
299
300 var bootClasspath string
301 if len(flags.bootClasspath) == 0 && ctx.Device() {
302 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
303 // ensure java does not fall back to the default bootclasspath.
304 bootClasspath = `--bootclasspath ""`
305 } else {
Colin Crossafbb1732019-01-17 15:42:52 -0800306 bootClasspath = strings.Join(flags.bootClasspath.FormTurbineClasspath("--bootclasspath "), " ")
Nan Zhanged19fc32017-10-19 13:06:22 -0700307 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700308
Colin Crossae887032017-10-23 17:16:14 -0700309 ctx.Build(pctx, android.BuildParams{
Nan Zhanged19fc32017-10-19 13:06:22 -0700310 Rule: turbine,
311 Description: "turbine",
312 Output: outputFile,
313 Inputs: srcFiles,
314 Implicits: deps,
315 Args: map[string]string{
316 "javacFlags": flags.javacFlags,
317 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700318 "srcJars": strings.Join(srcJars.Strings(), " "),
Colin Crossafbb1732019-01-17 15:42:52 -0800319 "classpath": strings.Join(flags.classpath.FormTurbineClasspath("--classpath "), " "),
Nan Zhanged19fc32017-10-19 13:06:22 -0700320 "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
Colin Cross1e743852019-10-28 11:37:20 -0700321 "javaVersion": flags.javaVersion.String(),
Nan Zhanged19fc32017-10-19 13:06:22 -0700322 },
323 })
324}
325
Colin Cross070879e2017-10-11 11:21:07 -0700326// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700327// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
328// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700329//
330// This method may be used for different compilers, including javac and Error Prone. The rule
331// argument specifies which command line to use and desc sets the description of the rule that will
332// be printed at build time. The stem argument provides the file name of the output jar, and
333// suffix will be appended to various intermediate files and directories to avoid collisions when
334// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700335func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700336 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700337 flags javaBuilderFlags, deps android.Paths,
Colin Cross66548102018-06-19 22:47:35 -0700338 intermediatesDir, desc string) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700339
Colin Cross59149b62017-10-16 18:07:29 -0700340 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700341
342 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700343 if flags.javaVersion.usesJavaModules() {
Colin Crossb77043e2019-07-16 13:57:13 -0700344 var systemModuleDeps android.Paths
345 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
346 deps = append(deps, systemModuleDeps...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700347 } else {
348 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700349 if len(flags.bootClasspath) == 0 && ctx.Device() {
350 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
351 // ensure java does not fall back to the default bootclasspath.
352 bootClasspath = `-bootclasspath ""`
353 } else {
354 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
355 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700356 }
357
Colin Cross6ade34f2017-09-15 13:00:47 -0700358 deps = append(deps, flags.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700359 deps = append(deps, flags.processorPath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700360
Colin Cross7788c122019-01-23 16:14:02 -0800361 processor := "-proc:none"
Colin Crossbe9cdb82019-01-21 21:37:16 -0800362 if flags.processor != "" {
363 processor = "-processor " + flags.processor
364 }
365
Nan Zhang61eaedb2017-11-02 13:28:15 -0700366 srcJarDir := "srcjars"
367 outDir := "classes"
368 annoDir := "anno"
369 if shardIdx >= 0 {
370 shardDir := "shard" + strconv.Itoa(shardIdx)
371 srcJarDir = filepath.Join(shardDir, srcJarDir)
372 outDir = filepath.Join(shardDir, outDir)
373 annoDir = filepath.Join(shardDir, annoDir)
374 }
Colin Crossae887032017-10-23 17:16:14 -0700375 ctx.Build(pctx, android.BuildParams{
Colin Cross66548102018-06-19 22:47:35 -0700376 Rule: javac,
Colin Cross070879e2017-10-11 11:21:07 -0700377 Description: desc,
378 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700379 Inputs: srcFiles,
380 Implicits: deps,
381 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700382 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700383 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700384 "classpath": flags.classpath.FormJavaClassPath("-classpath"),
Colin Cross6a77c982018-06-19 22:43:34 -0700385 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
Colin Crossbe9cdb82019-01-21 21:37:16 -0800386 "processor": processor,
Colin Cross8eadbf02017-10-24 17:46:00 -0700387 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700388 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
389 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
390 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross1e743852019-10-28 11:37:20 -0700391 "javaVersion": flags.javaVersion.String(),
Colin Crossc6bbef32017-08-14 14:16:06 -0700392 },
393 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700394}
395
Colin Crosse9a275b2017-10-16 17:09:48 -0700396func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
397 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700398
Colin Crossae887032017-10-23 17:16:14 -0700399 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700400 Rule: jar,
401 Description: "jar",
402 Output: outputFile,
403 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700404 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -0800405 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700406 },
407 })
Colin Cross2fe66872015-03-30 17:20:39 -0700408}
409
Nan Zhanged19fc32017-10-19 13:06:22 -0700410func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
Colin Cross37f6d792018-07-12 12:28:41 -0700411 jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
412 dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700413
Colin Cross635acc92017-09-12 22:50:46 -0700414 var deps android.Paths
415
416 var jarArgs []string
417 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700418 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700419 deps = append(deps, manifest.Path())
420 }
421
Colin Cross37f6d792018-07-12 12:28:41 -0700422 for _, dir := range dirsToStrip {
423 jarArgs = append(jarArgs, "-stripDir ", dir)
424 }
425
426 for _, file := range filesToStrip {
427 jarArgs = append(jarArgs, "-stripFile ", file)
Nan Zhanged19fc32017-10-19 13:06:22 -0700428 }
429
Colin Cross7b60cdd2017-12-21 13:52:58 -0800430 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
431 // for downstream tools like desugar.
432 jarArgs = append(jarArgs, "-stripFile module-info.class")
433
Colin Cross37f6d792018-07-12 12:28:41 -0700434 if stripDirEntries {
Colin Cross635acc92017-09-12 22:50:46 -0700435 jarArgs = append(jarArgs, "-D")
436 }
437
Colin Crossae887032017-10-23 17:16:14 -0700438 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700439 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700440 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700441 Output: outputFile,
442 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700443 Implicits: deps,
444 Args: map[string]string{
445 "jarArgs": strings.Join(jarArgs, " "),
446 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700447 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700448}
449
Colin Crosse9a275b2017-10-16 17:09:48 -0700450func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
451 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700452 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700453 Rule: jarjar,
454 Description: "jarjar",
455 Output: outputFile,
456 Input: classesJar,
457 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700458 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700459 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700460 },
461 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700462}
Colin Cross6ade34f2017-09-15 13:00:47 -0700463
Vladimir Marko0975ee02019-04-02 10:29:55 +0100464func CheckJarPackages(ctx android.ModuleContext, outputFile android.WritablePath,
465 classesJar android.Path, permittedPackages []string) {
466 ctx.Build(pctx, android.BuildParams{
467 Rule: packageCheck,
468 Description: "packageCheck",
469 Output: outputFile,
470 Input: classesJar,
471 Args: map[string]string{
472 "packages": strings.Join(permittedPackages, " "),
473 },
474 })
475}
476
Nan Zhang4c819fb2018-08-27 18:31:46 -0700477func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath,
478 inputFile android.Path) {
479 ctx.Build(pctx, android.BuildParams{
480 Rule: jetifier,
481 Description: "jetifier",
482 Output: outputFile,
483 Input: inputFile,
484 })
485}
486
Colin Cross094054a2018-10-17 15:10:48 -0700487func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) {
488 ctx.Build(pctx, android.BuildParams{
489 Rule: android.WriteFile,
490 Description: "manifest",
491 Output: outputFile,
492 Args: map[string]string{
493 "content": "Main-Class: " + mainClass + "\n",
494 },
495 })
496}
497
Colin Cross43f08db2018-11-12 10:13:39 -0800498func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path) {
499 ctx.Build(pctx, android.BuildParams{
500 Rule: zipalign,
501 Description: "align",
502 Input: inputFile,
503 Output: outputFile,
504 })
505}
506
Colin Cross33961b52019-07-11 11:01:22 -0700507type classpath android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700508
Nan Zhanged19fc32017-10-19 13:06:22 -0700509func (x *classpath) FormJavaClassPath(optName string) string {
Colin Cross81440082018-08-15 20:21:55 -0700510 if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") {
511 optName += " "
512 }
Colin Cross59149b62017-10-16 18:07:29 -0700513 if len(*x) > 0 {
Colin Cross81440082018-08-15 20:21:55 -0700514 return optName + strings.Join(x.Strings(), ":")
Colin Cross6ade34f2017-09-15 13:00:47 -0700515 } else {
516 return ""
517 }
518}
519
Colin Crossbafb8972018-06-06 21:46:32 +0000520func (x *classpath) FormTurbineClasspath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700521 if x == nil || *x == nil {
522 return nil
523 }
524 flags := make([]string, len(*x))
525 for i, v := range *x {
Colin Crossafbb1732019-01-17 15:42:52 -0800526 flags[i] = optName + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700527 }
528
529 return flags
530}
531
Colin Cross6ade34f2017-09-15 13:00:47 -0700532// Convert a classpath to an android.Paths
533func (x *classpath) Paths() android.Paths {
534 return append(android.Paths(nil), (*x)...)
535}
536
537func (x *classpath) Strings() []string {
538 if x == nil {
539 return nil
540 }
541 ret := make([]string, len(*x))
542 for i, path := range *x {
543 ret[i] = path.String()
544 }
545 return ret
546}
Colin Crossb77043e2019-07-16 13:57:13 -0700547
548type systemModules struct {
549 dir android.Path
550 deps android.Paths
551}
552
553// Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true,
554// returns --system=none if the list is empty to ensure javac does not fall back to the default
555// system modules.
556func (x *systemModules) FormJavaSystemModulesPath(forceEmpty bool) (string, android.Paths) {
557 if x != nil {
558 return "--system=" + x.dir.String(), x.deps
559 } else if forceEmpty {
560 return "--system=none", nil
561 } else {
562 return "", nil
563 }
564}