blob: 770e25559003584c2d9f79e17463f9c3d5aeb884 [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.
Colin Cross1ec3fce2020-03-05 12:43:14 -080041 // TODO(b/143658984): goma can't handle the --system argument to javac.
42 javac = pctx.AndroidRemoteStaticRule("javac", android.RemoteRuleSupports{Goma: false, RBE: true, RBEFlag: android.RBE_JAVAC},
Colin Cross2fe66872015-03-30 17:20:39 -070043 blueprint.RuleParams{
Colin Cross8eadbf02017-10-24 17:46:00 -070044 Command: `rm -rf "$outDir" "$annoDir" "$srcJarDir" && mkdir -p "$outDir" "$annoDir" "$srcJarDir" && ` +
Colin Cross436b7652018-03-15 16:24:10 -070045 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090046 `(if [ -s $srcJarDir/list ] || [ -s $out.rsp ] ; then ` +
Sasha Smundak26c6d9e2019-06-11 13:30:13 -070047 `${config.SoongJavacWrapper} ${config.JavacWrapper}${config.JavacCmd} ` +
48 `${config.JavacHeapFlags} ${config.JavacVmFlags} ${config.CommonJdkFlags} ` +
Colin Crossbe9cdb82019-01-21 21:37:16 -080049 `$processorpath $processor $javacFlags $bootClasspath $classpath ` +
Colin Cross64162712017-08-08 13:17:59 -070050 `-source $javaVersion -target $javaVersion ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090051 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list ; fi ) && ` +
Colin Cross44c29a82019-01-24 16:36:57 -080052 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir && ` +
53 `rm -rf "$srcJarDir"`,
Colin Cross8eadbf02017-10-24 17:46:00 -070054 CommandDeps: []string{
55 "${config.JavacCmd}",
56 "${config.SoongZipCmd}",
Colin Cross436b7652018-03-15 16:24:10 -070057 "${config.ZipSyncCmd}",
Colin Cross8eadbf02017-10-24 17:46:00 -070058 },
Colin Crossa4820652017-10-17 13:56:52 -070059 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
60 Rspfile: "$out.rsp",
61 RspfileContent: "$in",
Colin Cross2fe66872015-03-30 17:20:39 -070062 },
Colin Crossbe9cdb82019-01-21 21:37:16 -080063 "javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
Colin Cross8eadbf02017-10-24 17:46:00 -070064 "outDir", "annoDir", "javaVersion")
Colin Cross2fe66872015-03-30 17:20:39 -070065
Sasha Smundak2a4549e2018-11-05 16:49:08 -080066 _ = pctx.VariableFunc("kytheCorpus",
67 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() })
Sasha Smundak6c2d4f92020-01-09 17:34:23 -080068 _ = pctx.VariableFunc("kytheCuEncoding",
69 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuEncoding() })
Sasha Smundak65143642019-09-26 20:14:28 -070070 _ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json")
Sasha Smundak2a4549e2018-11-05 16:49:08 -080071 // Run it with -add-opens=java.base/java.nio=ALL-UNNAMED to avoid JDK9's warning about
72 // "Illegal reflective access by com.google.protobuf.Utf8$UnsafeProcessor ...
73 // to field java.nio.Buffer.address"
74 kytheExtract = pctx.AndroidStaticRule("kythe",
75 blueprint.RuleParams{
76 Command: `${config.ZipSyncCmd} -d $srcJarDir ` +
77 `-l $srcJarDir/list -f "*.java" $srcJars && ` +
78 `( [ ! -s $srcJarDir/list -a ! -s $out.rsp ] || ` +
79 `KYTHE_ROOT_DIRECTORY=. KYTHE_OUTPUT_FILE=$out ` +
80 `KYTHE_CORPUS=${kytheCorpus} ` +
Sasha Smundak65143642019-09-26 20:14:28 -070081 `KYTHE_VNAMES=${kytheVnames} ` +
Sasha Smundak6c2d4f92020-01-09 17:34:23 -080082 `KYTHE_KZIP_ENCODING=${kytheCuEncoding} ` +
Sasha Smundak2a4549e2018-11-05 16:49:08 -080083 `${config.SoongJavacWrapper} ${config.JavaCmd} ` +
84 `--add-opens=java.base/java.nio=ALL-UNNAMED ` +
85 `-jar ${config.JavaKytheExtractorJar} ` +
86 `${config.JavacHeapFlags} ${config.CommonJdkFlags} ` +
87 `$processorpath $processor $javacFlags $bootClasspath $classpath ` +
88 `-source $javaVersion -target $javaVersion ` +
89 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list)`,
90 CommandDeps: []string{
91 "${config.JavaCmd}",
92 "${config.JavaKytheExtractorJar}",
Sasha Smundak65143642019-09-26 20:14:28 -070093 "${kytheVnames}",
Sasha Smundak2a4549e2018-11-05 16:49:08 -080094 "${config.ZipSyncCmd}",
95 },
96 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
97 Rspfile: "$out.rsp",
98 RspfileContent: "$in",
99 },
100 "javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
101 "outDir", "annoDir", "javaVersion")
102
Nan Zhanged19fc32017-10-19 13:06:22 -0700103 turbine = pctx.AndroidStaticRule("turbine",
104 blueprint.RuleParams{
Colin Cross6981f652018-03-07 15:14:50 -0800105 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Sasha Smundak26c6d9e2019-06-11 13:30:13 -0700106 `${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.TurbineJar} --output $out.tmp ` +
Colin Cross6981f652018-03-07 15:14:50 -0800107 `--temp_dir "$outDir" --sources @$out.rsp --source_jars $srcJars ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700108 `--javacopts ${config.CommonJdkFlags} ` +
Colin Cross924a0aa2018-03-07 10:51:05 -0800109 `$javacFlags -source $javaVersion -target $javaVersion -- $bootClasspath $classpath && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700110 `${config.Ziptime} $out.tmp && ` +
111 `(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`,
Colin Cross8eadbf02017-10-24 17:46:00 -0700112 CommandDeps: []string{
113 "${config.TurbineJar}",
114 "${config.JavaCmd}",
115 "${config.Ziptime}",
Colin Cross8eadbf02017-10-24 17:46:00 -0700116 },
Nan Zhanged19fc32017-10-19 13:06:22 -0700117 Rspfile: "$out.rsp",
118 RspfileContent: "$in",
119 Restat: true,
120 },
Colin Cross6981f652018-03-07 15:14:50 -0800121 "javacFlags", "bootClasspath", "classpath", "srcJars", "outDir", "javaVersion")
Nan Zhanged19fc32017-10-19 13:06:22 -0700122
Colin Cross9d45bb72016-08-29 16:14:13 -0700123 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -0700124 blueprint.RuleParams{
Nan Zhang674dd932018-01-26 18:30:36 -0800125 Command: `${config.SoongZipCmd} -jar -o $out @$out.rsp`,
126 CommandDeps: []string{"${config.SoongZipCmd}"},
127 Rspfile: "$out.rsp",
128 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -0700129 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700130 "jarArgs")
131
Colin Crossa4f08812018-10-02 22:03:40 -0700132 zip = pctx.AndroidStaticRule("zip",
133 blueprint.RuleParams{
134 Command: `${config.SoongZipCmd} -o $out @$out.rsp`,
135 CommandDeps: []string{"${config.SoongZipCmd}"},
136 Rspfile: "$out.rsp",
137 RspfileContent: "$jarArgs",
138 },
139 "jarArgs")
140
Colin Cross0a6e0072017-08-30 14:24:55 -0700141 combineJar = pctx.AndroidStaticRule("combineJar",
142 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800143 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700144 CommandDeps: []string{"${config.MergeZipsCmd}"},
145 },
Colin Cross635acc92017-09-12 22:50:46 -0700146 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700147
Colin Cross9d45bb72016-08-29 16:14:13 -0700148 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700149 blueprint.RuleParams{
Sasha Smundak26c6d9e2019-06-11 13:30:13 -0700150 Command: "${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JarjarCmd} process $rulesFile $in $out",
Colin Cross64162712017-08-08 13:17:59 -0700151 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700152 },
153 "rulesFile")
Nan Zhang4c819fb2018-08-27 18:31:46 -0700154
Vladimir Marko0975ee02019-04-02 10:29:55 +0100155 packageCheck = pctx.AndroidStaticRule("packageCheck",
156 blueprint.RuleParams{
157 Command: "rm -f $out && " +
158 "${config.PackageCheckCmd} $in $packages && " +
159 "touch $out",
160 CommandDeps: []string{"${config.PackageCheckCmd}"},
161 },
162 "packages")
163
Nan Zhang4c819fb2018-08-27 18:31:46 -0700164 jetifier = pctx.AndroidStaticRule("jetifier",
165 blueprint.RuleParams{
Sasha Smundak26c6d9e2019-06-11 13:30:13 -0700166 Command: "${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JetifierJar} -l error -o $out -i $in",
Nan Zhang4c819fb2018-08-27 18:31:46 -0700167 CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"},
168 },
169 )
Colin Cross43f08db2018-11-12 10:13:39 -0800170
171 zipalign = pctx.AndroidStaticRule("zipalign",
172 blueprint.RuleParams{
Colin Crosse4246ab2019-02-05 21:55:21 -0800173 Command: "if ! ${config.ZipAlign} -c -p 4 $in > /dev/null; then " +
174 "${config.ZipAlign} -f -p 4 $in $out; " +
Colin Cross43f08db2018-11-12 10:13:39 -0800175 "else " +
176 "cp -f $in $out; " +
177 "fi",
178 CommandDeps: []string{"${config.ZipAlign}"},
179 },
180 )
Colin Cross2fe66872015-03-30 17:20:39 -0700181)
182
183func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700184 pctx.Import("android/soong/android")
Colin Cross64162712017-08-08 13:17:59 -0700185 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700186}
187
188type javaBuilderFlags struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700189 javacFlags string
190 bootClasspath classpath
191 classpath classpath
192 java9Classpath classpath
193 processorPath classpath
Colin Cross5a116862020-04-22 11:44:34 -0700194 processors []string
Colin Cross6cef4812019-10-17 14:23:50 -0700195 systemModules *systemModules
196 aidlFlags string
197 aidlDeps android.Paths
198 javaVersion javaVersion
Colin Cross6af17aa2017-09-20 12:59:05 -0700199
Andreas Gampef3e5b552018-01-22 21:27:21 -0800200 errorProneExtraJavacFlags string
Colin Cross66548102018-06-19 22:47:35 -0700201 errorProneProcessorPath classpath
Andreas Gampef3e5b552018-01-22 21:27:21 -0800202
Colin Cross93e85952017-08-15 13:34:18 -0700203 kotlincFlags string
204 kotlincClasspath classpath
205
Colin Cross19878da2019-03-28 14:45:07 -0700206 proto android.ProtoFlags
Colin Cross2fe66872015-03-30 17:20:39 -0700207}
208
Nan Zhang61eaedb2017-11-02 13:28:15 -0700209func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
210 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700211
Nan Zhang61eaedb2017-11-02 13:28:15 -0700212 // Compile java sources into .class files
213 desc := "javac"
214 if shardIdx >= 0 {
215 desc += strconv.Itoa(shardIdx)
216 }
217
Colin Cross66548102018-06-19 22:47:35 -0700218 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc)
Colin Cross2fe66872015-03-30 17:20:39 -0700219}
220
Colin Crosse9a275b2017-10-16 17:09:48 -0700221func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700222 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700223
Colin Cross66548102018-06-19 22:47:35 -0700224 flags.processorPath = append(flags.errorProneProcessorPath, flags.processorPath...)
Colin Crossfee57cb2017-09-05 13:16:45 -0700225
Andreas Gampef3e5b552018-01-22 21:27:21 -0800226 if len(flags.errorProneExtraJavacFlags) > 0 {
227 if len(flags.javacFlags) > 0 {
Colin Cross66548102018-06-19 22:47:35 -0700228 flags.javacFlags += " " + flags.errorProneExtraJavacFlags
Andreas Gampef3e5b552018-01-22 21:27:21 -0800229 } else {
230 flags.javacFlags = flags.errorProneExtraJavacFlags
231 }
232 }
233
Nan Zhang61eaedb2017-11-02 13:28:15 -0700234 transformJavaToClasses(ctx, outputFile, -1, srcFiles, srcJars, flags, nil,
Colin Cross66548102018-06-19 22:47:35 -0700235 "errorprone", "errorprone")
Colin Cross070879e2017-10-11 11:21:07 -0700236}
237
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800238// Emits the rule to generate Xref input file (.kzip file) for the given set of source files and source jars
239// to compile with given set of builder flags, etc.
Colin Cross3b706fd2019-09-05 16:44:18 -0700240func emitXrefRule(ctx android.ModuleContext, xrefFile android.WritablePath, idx int,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800241 srcFiles, srcJars android.Paths,
Colin Cross3b706fd2019-09-05 16:44:18 -0700242 flags javaBuilderFlags, deps android.Paths) {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800243
244 deps = append(deps, srcJars...)
Sasha Smundak09950a42019-11-04 16:29:45 -0800245 classpath := flags.classpath
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800246
247 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700248 if flags.javaVersion.usesJavaModules() {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800249 var systemModuleDeps android.Paths
250 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
251 deps = append(deps, systemModuleDeps...)
Sasha Smundak09950a42019-11-04 16:29:45 -0800252 classpath = append(flags.java9Classpath, classpath...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800253 } else {
254 deps = append(deps, flags.bootClasspath...)
255 if len(flags.bootClasspath) == 0 && ctx.Device() {
256 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
257 // ensure java does not fall back to the default bootclasspath.
258 bootClasspath = `-bootclasspath ""`
259 } else {
260 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
261 }
262 }
263
Sasha Smundak09950a42019-11-04 16:29:45 -0800264 deps = append(deps, classpath...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800265 deps = append(deps, flags.processorPath...)
266
267 processor := "-proc:none"
Colin Cross5a116862020-04-22 11:44:34 -0700268 if len(flags.processors) > 0 {
269 processor = "-processor " + strings.Join(flags.processors, ",")
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800270 }
271
Colin Cross3b706fd2019-09-05 16:44:18 -0700272 intermediatesDir := "xref"
273 if idx >= 0 {
274 intermediatesDir += strconv.Itoa(idx)
275 }
276
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800277 ctx.Build(pctx,
278 android.BuildParams{
279 Rule: kytheExtract,
280 Description: "Xref Java extractor",
281 Output: xrefFile,
282 Inputs: srcFiles,
283 Implicits: deps,
284 Args: map[string]string{
285 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, "anno").String(),
286 "bootClasspath": bootClasspath,
Sasha Smundak09950a42019-11-04 16:29:45 -0800287 "classpath": classpath.FormJavaClassPath("-classpath"),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800288 "javacFlags": flags.javacFlags,
Colin Cross1e743852019-10-28 11:37:20 -0700289 "javaVersion": flags.javaVersion.String(),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800290 "outDir": android.PathForModuleOut(ctx, "javac", "classes.xref").String(),
291 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
292 "processor": processor,
293 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, "srcjars.xref").String(),
294 "srcJars": strings.Join(srcJars.Strings(), " "),
295 },
296 })
297}
298
Nan Zhanged19fc32017-10-19 13:06:22 -0700299func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700300 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700301
302 var deps android.Paths
303 deps = append(deps, srcJars...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700304
Colin Cross6cef4812019-10-17 14:23:50 -0700305 classpath := flags.classpath
306
Nan Zhanged19fc32017-10-19 13:06:22 -0700307 var bootClasspath string
Colin Crossbf3119e2019-10-28 14:25:10 -0700308 if flags.javaVersion.usesJavaModules() {
309 var systemModuleDeps android.Paths
310 bootClasspath, systemModuleDeps = flags.systemModules.FormTurbineSystemModulesPath(ctx.Device())
311 deps = append(deps, systemModuleDeps...)
Colin Cross6cef4812019-10-17 14:23:50 -0700312 classpath = append(flags.java9Classpath, classpath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700313 } else {
Colin Crossbf3119e2019-10-28 14:25:10 -0700314 deps = append(deps, flags.bootClasspath...)
315 if len(flags.bootClasspath) == 0 && ctx.Device() {
316 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
317 // ensure turbine does not fall back to the default bootclasspath.
318 bootClasspath = `--bootclasspath ""`
319 } else {
Colin Crossc2557d12019-10-31 15:22:57 -0700320 bootClasspath = flags.bootClasspath.FormTurbineClassPath("--bootclasspath ")
Colin Crossbf3119e2019-10-28 14:25:10 -0700321 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700322 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700323
Colin Cross6cef4812019-10-17 14:23:50 -0700324 deps = append(deps, classpath...)
Colin Crossbf3119e2019-10-28 14:25:10 -0700325 deps = append(deps, flags.processorPath...)
326
Colin Crossae887032017-10-23 17:16:14 -0700327 ctx.Build(pctx, android.BuildParams{
Nan Zhanged19fc32017-10-19 13:06:22 -0700328 Rule: turbine,
329 Description: "turbine",
330 Output: outputFile,
331 Inputs: srcFiles,
332 Implicits: deps,
333 Args: map[string]string{
334 "javacFlags": flags.javacFlags,
335 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700336 "srcJars": strings.Join(srcJars.Strings(), " "),
Colin Crossc2557d12019-10-31 15:22:57 -0700337 "classpath": classpath.FormTurbineClassPath("--classpath "),
Nan Zhanged19fc32017-10-19 13:06:22 -0700338 "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
Colin Cross1e743852019-10-28 11:37:20 -0700339 "javaVersion": flags.javaVersion.String(),
Nan Zhanged19fc32017-10-19 13:06:22 -0700340 },
341 })
342}
343
Colin Cross070879e2017-10-11 11:21:07 -0700344// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700345// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
346// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700347//
348// This method may be used for different compilers, including javac and Error Prone. The rule
349// argument specifies which command line to use and desc sets the description of the rule that will
350// be printed at build time. The stem argument provides the file name of the output jar, and
351// suffix will be appended to various intermediate files and directories to avoid collisions when
352// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700353func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700354 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700355 flags javaBuilderFlags, deps android.Paths,
Colin Cross66548102018-06-19 22:47:35 -0700356 intermediatesDir, desc string) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700357
Colin Cross59149b62017-10-16 18:07:29 -0700358 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700359
Colin Cross6cef4812019-10-17 14:23:50 -0700360 classpath := flags.classpath
361
Colin Cross1369cdb2017-09-29 17:58:17 -0700362 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700363 if flags.javaVersion.usesJavaModules() {
Colin Crossb77043e2019-07-16 13:57:13 -0700364 var systemModuleDeps android.Paths
365 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
366 deps = append(deps, systemModuleDeps...)
Colin Cross6cef4812019-10-17 14:23:50 -0700367 classpath = append(flags.java9Classpath, classpath...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700368 } else {
369 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700370 if len(flags.bootClasspath) == 0 && ctx.Device() {
371 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
372 // ensure java does not fall back to the default bootclasspath.
373 bootClasspath = `-bootclasspath ""`
374 } else {
375 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
376 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700377 }
378
Colin Cross6cef4812019-10-17 14:23:50 -0700379 deps = append(deps, classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700380 deps = append(deps, flags.processorPath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700381
Colin Cross7788c122019-01-23 16:14:02 -0800382 processor := "-proc:none"
Colin Cross5a116862020-04-22 11:44:34 -0700383 if len(flags.processors) > 0 {
384 processor = "-processor " + strings.Join(flags.processors, ",")
Colin Crossbe9cdb82019-01-21 21:37:16 -0800385 }
386
Nan Zhang61eaedb2017-11-02 13:28:15 -0700387 srcJarDir := "srcjars"
388 outDir := "classes"
389 annoDir := "anno"
390 if shardIdx >= 0 {
391 shardDir := "shard" + strconv.Itoa(shardIdx)
392 srcJarDir = filepath.Join(shardDir, srcJarDir)
393 outDir = filepath.Join(shardDir, outDir)
394 annoDir = filepath.Join(shardDir, annoDir)
395 }
Colin Crossae887032017-10-23 17:16:14 -0700396 ctx.Build(pctx, android.BuildParams{
Colin Cross66548102018-06-19 22:47:35 -0700397 Rule: javac,
Colin Cross070879e2017-10-11 11:21:07 -0700398 Description: desc,
399 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700400 Inputs: srcFiles,
401 Implicits: deps,
402 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700403 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700404 "bootClasspath": bootClasspath,
Colin Cross6cef4812019-10-17 14:23:50 -0700405 "classpath": classpath.FormJavaClassPath("-classpath"),
Colin Cross6a77c982018-06-19 22:43:34 -0700406 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
Colin Crossbe9cdb82019-01-21 21:37:16 -0800407 "processor": processor,
Colin Cross8eadbf02017-10-24 17:46:00 -0700408 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700409 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
410 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
411 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross1e743852019-10-28 11:37:20 -0700412 "javaVersion": flags.javaVersion.String(),
Colin Crossc6bbef32017-08-14 14:16:06 -0700413 },
414 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700415}
416
Colin Crosse9a275b2017-10-16 17:09:48 -0700417func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
418 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700419
Colin Crossae887032017-10-23 17:16:14 -0700420 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700421 Rule: jar,
422 Description: "jar",
423 Output: outputFile,
424 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700425 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -0800426 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700427 },
428 })
Colin Cross2fe66872015-03-30 17:20:39 -0700429}
430
Nan Zhanged19fc32017-10-19 13:06:22 -0700431func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
Colin Cross37f6d792018-07-12 12:28:41 -0700432 jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
433 dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700434
Colin Cross635acc92017-09-12 22:50:46 -0700435 var deps android.Paths
436
437 var jarArgs []string
438 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700439 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700440 deps = append(deps, manifest.Path())
441 }
442
Colin Cross37f6d792018-07-12 12:28:41 -0700443 for _, dir := range dirsToStrip {
444 jarArgs = append(jarArgs, "-stripDir ", dir)
445 }
446
447 for _, file := range filesToStrip {
448 jarArgs = append(jarArgs, "-stripFile ", file)
Nan Zhanged19fc32017-10-19 13:06:22 -0700449 }
450
Colin Cross7b60cdd2017-12-21 13:52:58 -0800451 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
452 // for downstream tools like desugar.
453 jarArgs = append(jarArgs, "-stripFile module-info.class")
454
Colin Cross37f6d792018-07-12 12:28:41 -0700455 if stripDirEntries {
Colin Cross635acc92017-09-12 22:50:46 -0700456 jarArgs = append(jarArgs, "-D")
457 }
458
Colin Crossae887032017-10-23 17:16:14 -0700459 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700460 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700461 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700462 Output: outputFile,
463 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700464 Implicits: deps,
465 Args: map[string]string{
466 "jarArgs": strings.Join(jarArgs, " "),
467 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700468 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700469}
470
Colin Crosse9a275b2017-10-16 17:09:48 -0700471func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
472 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700473 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700474 Rule: jarjar,
475 Description: "jarjar",
476 Output: outputFile,
477 Input: classesJar,
478 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700479 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700480 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700481 },
482 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700483}
Colin Cross6ade34f2017-09-15 13:00:47 -0700484
Vladimir Marko0975ee02019-04-02 10:29:55 +0100485func CheckJarPackages(ctx android.ModuleContext, outputFile android.WritablePath,
486 classesJar android.Path, permittedPackages []string) {
487 ctx.Build(pctx, android.BuildParams{
488 Rule: packageCheck,
489 Description: "packageCheck",
490 Output: outputFile,
491 Input: classesJar,
492 Args: map[string]string{
493 "packages": strings.Join(permittedPackages, " "),
494 },
495 })
496}
497
Nan Zhang4c819fb2018-08-27 18:31:46 -0700498func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath,
499 inputFile android.Path) {
500 ctx.Build(pctx, android.BuildParams{
501 Rule: jetifier,
502 Description: "jetifier",
503 Output: outputFile,
504 Input: inputFile,
505 })
506}
507
Colin Cross094054a2018-10-17 15:10:48 -0700508func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) {
509 ctx.Build(pctx, android.BuildParams{
510 Rule: android.WriteFile,
511 Description: "manifest",
512 Output: outputFile,
513 Args: map[string]string{
514 "content": "Main-Class: " + mainClass + "\n",
515 },
516 })
517}
518
Colin Cross43f08db2018-11-12 10:13:39 -0800519func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path) {
520 ctx.Build(pctx, android.BuildParams{
521 Rule: zipalign,
522 Description: "align",
523 Input: inputFile,
524 Output: outputFile,
525 })
526}
527
Colin Cross33961b52019-07-11 11:01:22 -0700528type classpath android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700529
Colin Crossc2557d12019-10-31 15:22:57 -0700530func (x *classpath) formJoinedClassPath(optName string, sep string) string {
Colin Cross81440082018-08-15 20:21:55 -0700531 if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") {
532 optName += " "
533 }
Colin Cross59149b62017-10-16 18:07:29 -0700534 if len(*x) > 0 {
Colin Crossc2557d12019-10-31 15:22:57 -0700535 return optName + strings.Join(x.Strings(), sep)
Colin Cross6ade34f2017-09-15 13:00:47 -0700536 } else {
537 return ""
538 }
539}
Colin Crossc2557d12019-10-31 15:22:57 -0700540func (x *classpath) FormJavaClassPath(optName string) string {
541 return x.formJoinedClassPath(optName, ":")
542}
Colin Cross6ade34f2017-09-15 13:00:47 -0700543
Colin Crossc2557d12019-10-31 15:22:57 -0700544func (x *classpath) FormTurbineClassPath(optName string) string {
545 return x.formJoinedClassPath(optName, " ")
546}
547
548// FormRepeatedClassPath returns a list of arguments with the given optName prefixed to each element of the classpath.
549func (x *classpath) FormRepeatedClassPath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700550 if x == nil || *x == nil {
551 return nil
552 }
553 flags := make([]string, len(*x))
554 for i, v := range *x {
Colin Crossafbb1732019-01-17 15:42:52 -0800555 flags[i] = optName + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700556 }
557
558 return flags
559}
560
Colin Cross6ade34f2017-09-15 13:00:47 -0700561// Convert a classpath to an android.Paths
562func (x *classpath) Paths() android.Paths {
563 return append(android.Paths(nil), (*x)...)
564}
565
566func (x *classpath) Strings() []string {
567 if x == nil {
568 return nil
569 }
570 ret := make([]string, len(*x))
571 for i, path := range *x {
572 ret[i] = path.String()
573 }
574 return ret
575}
Colin Crossb77043e2019-07-16 13:57:13 -0700576
577type systemModules struct {
578 dir android.Path
579 deps android.Paths
580}
581
Colin Crossbf3119e2019-10-28 14:25:10 -0700582// Returns a --system argument in the form javac expects with -source 1.9 and the list of files to
583// depend on. If forceEmpty is true, returns --system=none if the list is empty to ensure javac
584// does not fall back to the default system modules.
Colin Crossb77043e2019-07-16 13:57:13 -0700585func (x *systemModules) FormJavaSystemModulesPath(forceEmpty bool) (string, android.Paths) {
586 if x != nil {
587 return "--system=" + x.dir.String(), x.deps
588 } else if forceEmpty {
589 return "--system=none", nil
590 } else {
591 return "", nil
592 }
593}
Colin Crossbf3119e2019-10-28 14:25:10 -0700594
595// Returns a --system argument in the form turbine expects with -source 1.9 and the list of files to
596// depend on. If forceEmpty is true, returns --bootclasspath "" if the list is empty to ensure turbine
597// does not fall back to the default bootclasspath.
598func (x *systemModules) FormTurbineSystemModulesPath(forceEmpty bool) (string, android.Paths) {
599 if x != nil {
600 return "--system " + x.dir.String(), x.deps
601 } else if forceEmpty {
602 return `--bootclasspath ""`, nil
603 } else {
604 return "", nil
605 }
606}