blob: cd352454218c1930d279843cfc8397147a924f00 [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"
Ramy Medhat1dcc27e2020-04-21 21:36:23 -040030 "android/soong/remoteexec"
Colin Cross2fe66872015-03-30 17:20:39 -070031)
32
33var (
Colin Cross635c3b02016-05-18 15:37:25 -070034 pctx = android.NewPackageContext("android/soong/java")
Colin Cross2fe66872015-03-30 17:20:39 -070035
36 // Compiling java is not conducive to proper dependency tracking. The path-matches-class-name
37 // requirement leads to unpredictable generated source file names, and a single .java file
38 // will get compiled into multiple .class files if it contains inner classes. To work around
Colin Crossf7eac7a2018-02-08 12:48:39 -080039 // this, all java rules write into separate directories and then are combined into a .jar file
40 // (if the rule produces .class files) or a .srcjar file (if the rule produces .java files).
41 // .srcjar files are unzipped into a temporary directory when compiled with javac.
Colin Cross1ec3fce2020-03-05 12:43:14 -080042 // TODO(b/143658984): goma can't handle the --system argument to javac.
Kousik Kumar366afc52020-05-20 11:27:16 -070043 javac, javacRE = remoteexec.MultiCommandStaticRules(pctx, "javac",
Colin Cross2fe66872015-03-30 17:20:39 -070044 blueprint.RuleParams{
Colin Cross8eadbf02017-10-24 17:46:00 -070045 Command: `rm -rf "$outDir" "$annoDir" "$srcJarDir" && mkdir -p "$outDir" "$annoDir" "$srcJarDir" && ` +
Colin Cross436b7652018-03-15 16:24:10 -070046 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090047 `(if [ -s $srcJarDir/list ] || [ -s $out.rsp ] ; then ` +
Kousik Kumar366afc52020-05-20 11:27:16 -070048 `${config.SoongJavacWrapper} $javaTemplate${config.JavacCmd} ` +
Sasha Smundak26c6d9e2019-06-11 13:30:13 -070049 `${config.JavacHeapFlags} ${config.JavacVmFlags} ${config.CommonJdkFlags} ` +
Colin Crossbe9cdb82019-01-21 21:37:16 -080050 `$processorpath $processor $javacFlags $bootClasspath $classpath ` +
Colin Cross64162712017-08-08 13:17:59 -070051 `-source $javaVersion -target $javaVersion ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090052 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list ; fi ) && ` +
Kousik Kumar366afc52020-05-20 11:27:16 -070053 `$zipTemplate${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir && ` +
Colin Cross44c29a82019-01-24 16:36:57 -080054 `rm -rf "$srcJarDir"`,
Colin Cross8eadbf02017-10-24 17:46:00 -070055 CommandDeps: []string{
56 "${config.JavacCmd}",
57 "${config.SoongZipCmd}",
Colin Cross436b7652018-03-15 16:24:10 -070058 "${config.ZipSyncCmd}",
Colin Cross8eadbf02017-10-24 17:46:00 -070059 },
Colin Crossa4820652017-10-17 13:56:52 -070060 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
61 Rspfile: "$out.rsp",
62 RspfileContent: "$in",
Kousik Kumar366afc52020-05-20 11:27:16 -070063 }, map[string]*remoteexec.REParams{
64 "$javaTemplate": &remoteexec.REParams{
65 Labels: map[string]string{"type": "compile", "lang": "java", "compiler": "javac"},
66 ExecStrategy: "${config.REJavacExecStrategy}",
67 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
68 },
69 "$zipTemplate": &remoteexec.REParams{
70 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
71 Inputs: []string{"${config.SoongZipCmd}", "$outDir"},
72 OutputFiles: []string{"$out"},
73 ExecStrategy: "${config.REJavacExecStrategy}",
74 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
75 },
Ramy Medhat1dcc27e2020-04-21 21:36:23 -040076 }, []string{"javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
77 "outDir", "annoDir", "javaVersion"}, nil)
Colin Cross2fe66872015-03-30 17:20:39 -070078
Sasha Smundak2a4549e2018-11-05 16:49:08 -080079 _ = pctx.VariableFunc("kytheCorpus",
80 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() })
Sasha Smundak6c2d4f92020-01-09 17:34:23 -080081 _ = pctx.VariableFunc("kytheCuEncoding",
82 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuEncoding() })
Sasha Smundak65143642019-09-26 20:14:28 -070083 _ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json")
Sasha Smundak2a4549e2018-11-05 16:49:08 -080084 // Run it with -add-opens=java.base/java.nio=ALL-UNNAMED to avoid JDK9's warning about
85 // "Illegal reflective access by com.google.protobuf.Utf8$UnsafeProcessor ...
86 // to field java.nio.Buffer.address"
87 kytheExtract = pctx.AndroidStaticRule("kythe",
88 blueprint.RuleParams{
89 Command: `${config.ZipSyncCmd} -d $srcJarDir ` +
90 `-l $srcJarDir/list -f "*.java" $srcJars && ` +
91 `( [ ! -s $srcJarDir/list -a ! -s $out.rsp ] || ` +
92 `KYTHE_ROOT_DIRECTORY=. KYTHE_OUTPUT_FILE=$out ` +
93 `KYTHE_CORPUS=${kytheCorpus} ` +
Sasha Smundak65143642019-09-26 20:14:28 -070094 `KYTHE_VNAMES=${kytheVnames} ` +
Sasha Smundak6c2d4f92020-01-09 17:34:23 -080095 `KYTHE_KZIP_ENCODING=${kytheCuEncoding} ` +
Sasha Smundak2a4549e2018-11-05 16:49:08 -080096 `${config.SoongJavacWrapper} ${config.JavaCmd} ` +
97 `--add-opens=java.base/java.nio=ALL-UNNAMED ` +
98 `-jar ${config.JavaKytheExtractorJar} ` +
99 `${config.JavacHeapFlags} ${config.CommonJdkFlags} ` +
100 `$processorpath $processor $javacFlags $bootClasspath $classpath ` +
101 `-source $javaVersion -target $javaVersion ` +
102 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list)`,
103 CommandDeps: []string{
104 "${config.JavaCmd}",
105 "${config.JavaKytheExtractorJar}",
Sasha Smundak65143642019-09-26 20:14:28 -0700106 "${kytheVnames}",
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800107 "${config.ZipSyncCmd}",
108 },
109 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
110 Rspfile: "$out.rsp",
111 RspfileContent: "$in",
112 },
113 "javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
114 "outDir", "annoDir", "javaVersion")
115
Sasha Smundaka7856c02020-04-23 09:49:59 -0700116 extractMatchingApks = pctx.StaticRule(
117 "extractMatchingApks",
118 blueprint.RuleParams{
119 Command: `rm -rf "$out" && ` +
120 `${config.ExtractApksCmd} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
121 `-sdk-version=${sdk-version} -abis=${abis} ` +
122 `--screen-densities=${screen-densities} --stem=${stem} ` +
Jaewoong Jung11c1e0f2020-06-29 19:18:44 -0700123 `-apkcerts=${apkcerts} -partition=${partition} ` +
Sasha Smundaka7856c02020-04-23 09:49:59 -0700124 `${in}`,
125 CommandDeps: []string{"${config.ExtractApksCmd}"},
126 },
Jaewoong Jung11c1e0f2020-06-29 19:18:44 -0700127 "abis", "allow-prereleased", "screen-densities", "sdk-version", "stem", "apkcerts", "partition")
Sasha Smundaka7856c02020-04-23 09:49:59 -0700128
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700129 turbine, turbineRE = remoteexec.StaticRules(pctx, "turbine",
Nan Zhanged19fc32017-10-19 13:06:22 -0700130 blueprint.RuleParams{
Colin Cross6981f652018-03-07 15:14:50 -0800131 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700132 `$reTemplate${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.TurbineJar} --output $out.tmp ` +
Colin Cross6981f652018-03-07 15:14:50 -0800133 `--temp_dir "$outDir" --sources @$out.rsp --source_jars $srcJars ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700134 `--javacopts ${config.CommonJdkFlags} ` +
Colin Cross924a0aa2018-03-07 10:51:05 -0800135 `$javacFlags -source $javaVersion -target $javaVersion -- $bootClasspath $classpath && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700136 `${config.Ziptime} $out.tmp && ` +
137 `(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`,
Colin Cross8eadbf02017-10-24 17:46:00 -0700138 CommandDeps: []string{
139 "${config.TurbineJar}",
140 "${config.JavaCmd}",
141 "${config.Ziptime}",
Colin Cross8eadbf02017-10-24 17:46:00 -0700142 },
Nan Zhanged19fc32017-10-19 13:06:22 -0700143 Rspfile: "$out.rsp",
144 RspfileContent: "$in",
145 Restat: true,
146 },
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700147 &remoteexec.REParams{Labels: map[string]string{"type": "tool", "name": "turbine"},
148 ExecStrategy: "${config.RETurbineExecStrategy}",
149 Inputs: []string{"${config.TurbineJar}", "${out}.rsp", "$implicits"},
150 RSPFile: "${out}.rsp",
151 OutputFiles: []string{"$out.tmp"},
152 OutputDirectories: []string{"$outDir"},
153 ToolchainInputs: []string{"${config.JavaCmd}"},
154 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
155 }, []string{"javacFlags", "bootClasspath", "classpath", "srcJars", "outDir", "javaVersion"}, []string{"implicits"})
Nan Zhanged19fc32017-10-19 13:06:22 -0700156
Kousik Kumar366afc52020-05-20 11:27:16 -0700157 jar, jarRE = remoteexec.StaticRules(pctx, "jar",
Colin Cross2fe66872015-03-30 17:20:39 -0700158 blueprint.RuleParams{
Kousik Kumar366afc52020-05-20 11:27:16 -0700159 Command: `$reTemplate${config.SoongZipCmd} -jar -o $out @$out.rsp`,
Nan Zhang674dd932018-01-26 18:30:36 -0800160 CommandDeps: []string{"${config.SoongZipCmd}"},
161 Rspfile: "$out.rsp",
162 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -0700163 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700164 &remoteexec.REParams{
165 ExecStrategy: "${config.REJarExecStrategy}",
166 Inputs: []string{"${config.SoongZipCmd}", "${out}.rsp"},
167 RSPFile: "${out}.rsp",
168 OutputFiles: []string{"$out"},
169 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
170 }, []string{"jarArgs"}, nil)
Colin Cross0a6e0072017-08-30 14:24:55 -0700171
Kousik Kumar366afc52020-05-20 11:27:16 -0700172 zip, zipRE = remoteexec.StaticRules(pctx, "zip",
Colin Crossa4f08812018-10-02 22:03:40 -0700173 blueprint.RuleParams{
174 Command: `${config.SoongZipCmd} -o $out @$out.rsp`,
175 CommandDeps: []string{"${config.SoongZipCmd}"},
176 Rspfile: "$out.rsp",
177 RspfileContent: "$jarArgs",
178 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700179 &remoteexec.REParams{
180 ExecStrategy: "${config.REZipExecStrategy}",
181 Inputs: []string{"${config.SoongZipCmd}", "${out}.rsp", "$implicits"},
182 RSPFile: "${out}.rsp",
183 OutputFiles: []string{"$out"},
184 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
185 }, []string{"jarArgs"}, []string{"implicits"})
Colin Crossa4f08812018-10-02 22:03:40 -0700186
Colin Cross0a6e0072017-08-30 14:24:55 -0700187 combineJar = pctx.AndroidStaticRule("combineJar",
188 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800189 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700190 CommandDeps: []string{"${config.MergeZipsCmd}"},
191 },
Colin Cross635acc92017-09-12 22:50:46 -0700192 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700193
Colin Cross9d45bb72016-08-29 16:14:13 -0700194 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700195 blueprint.RuleParams{
Artur Satayev762d9f32020-04-15 12:50:04 +0100196 Command: "${config.JavaCmd} ${config.JavaVmFlags}" +
197 // b/146418363 Enable Android specific jarjar transformer to drop compat annotations
198 // for newly repackaged classes. Dropping @UnsupportedAppUsage on repackaged classes
199 // avoids adding new hiddenapis after jarjar'ing.
200 " -DremoveAndroidCompatAnnotations=true" +
201 " -jar ${config.JarjarCmd} process $rulesFile $in $out",
Colin Cross64162712017-08-08 13:17:59 -0700202 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700203 },
204 "rulesFile")
Nan Zhang4c819fb2018-08-27 18:31:46 -0700205
Vladimir Marko0975ee02019-04-02 10:29:55 +0100206 packageCheck = pctx.AndroidStaticRule("packageCheck",
207 blueprint.RuleParams{
208 Command: "rm -f $out && " +
209 "${config.PackageCheckCmd} $in $packages && " +
Colin Crossb549b772020-06-03 17:14:31 +0000210 "touch $out",
Vladimir Marko0975ee02019-04-02 10:29:55 +0100211 CommandDeps: []string{"${config.PackageCheckCmd}"},
212 },
213 "packages")
214
Nan Zhang4c819fb2018-08-27 18:31:46 -0700215 jetifier = pctx.AndroidStaticRule("jetifier",
216 blueprint.RuleParams{
Sasha Smundak26c6d9e2019-06-11 13:30:13 -0700217 Command: "${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JetifierJar} -l error -o $out -i $in",
Nan Zhang4c819fb2018-08-27 18:31:46 -0700218 CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"},
219 },
220 )
Colin Cross43f08db2018-11-12 10:13:39 -0800221
222 zipalign = pctx.AndroidStaticRule("zipalign",
223 blueprint.RuleParams{
Colin Crosse4246ab2019-02-05 21:55:21 -0800224 Command: "if ! ${config.ZipAlign} -c -p 4 $in > /dev/null; then " +
225 "${config.ZipAlign} -f -p 4 $in $out; " +
Colin Cross43f08db2018-11-12 10:13:39 -0800226 "else " +
227 "cp -f $in $out; " +
228 "fi",
229 CommandDeps: []string{"${config.ZipAlign}"},
230 },
231 )
Colin Cross2fe66872015-03-30 17:20:39 -0700232)
233
234func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700235 pctx.Import("android/soong/android")
Colin Cross64162712017-08-08 13:17:59 -0700236 pctx.Import("android/soong/java/config")
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400237 pctx.Import("android/soong/remoteexec")
Colin Cross2fe66872015-03-30 17:20:39 -0700238}
239
240type javaBuilderFlags struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700241 javacFlags string
242 bootClasspath classpath
243 classpath classpath
244 java9Classpath classpath
245 processorPath classpath
Colin Cross5a116862020-04-22 11:44:34 -0700246 processors []string
Colin Cross6cef4812019-10-17 14:23:50 -0700247 systemModules *systemModules
248 aidlFlags string
249 aidlDeps android.Paths
250 javaVersion javaVersion
Colin Cross6af17aa2017-09-20 12:59:05 -0700251
Andreas Gampef3e5b552018-01-22 21:27:21 -0800252 errorProneExtraJavacFlags string
Colin Cross66548102018-06-19 22:47:35 -0700253 errorProneProcessorPath classpath
Andreas Gampef3e5b552018-01-22 21:27:21 -0800254
Colin Cross93e85952017-08-15 13:34:18 -0700255 kotlincFlags string
256 kotlincClasspath classpath
257
Colin Cross19878da2019-03-28 14:45:07 -0700258 proto android.ProtoFlags
Colin Cross2fe66872015-03-30 17:20:39 -0700259}
260
Nan Zhang61eaedb2017-11-02 13:28:15 -0700261func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
262 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700263
Nan Zhang61eaedb2017-11-02 13:28:15 -0700264 // Compile java sources into .class files
265 desc := "javac"
266 if shardIdx >= 0 {
267 desc += strconv.Itoa(shardIdx)
268 }
269
Colin Cross66548102018-06-19 22:47:35 -0700270 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc)
Colin Cross2fe66872015-03-30 17:20:39 -0700271}
272
Colin Crosse9a275b2017-10-16 17:09:48 -0700273func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700274 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700275
Colin Cross66548102018-06-19 22:47:35 -0700276 flags.processorPath = append(flags.errorProneProcessorPath, flags.processorPath...)
Colin Crossfee57cb2017-09-05 13:16:45 -0700277
Andreas Gampef3e5b552018-01-22 21:27:21 -0800278 if len(flags.errorProneExtraJavacFlags) > 0 {
279 if len(flags.javacFlags) > 0 {
Colin Cross66548102018-06-19 22:47:35 -0700280 flags.javacFlags += " " + flags.errorProneExtraJavacFlags
Andreas Gampef3e5b552018-01-22 21:27:21 -0800281 } else {
282 flags.javacFlags = flags.errorProneExtraJavacFlags
283 }
284 }
285
Nan Zhang61eaedb2017-11-02 13:28:15 -0700286 transformJavaToClasses(ctx, outputFile, -1, srcFiles, srcJars, flags, nil,
Colin Cross66548102018-06-19 22:47:35 -0700287 "errorprone", "errorprone")
Colin Cross070879e2017-10-11 11:21:07 -0700288}
289
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800290// Emits the rule to generate Xref input file (.kzip file) for the given set of source files and source jars
291// to compile with given set of builder flags, etc.
Colin Cross3b706fd2019-09-05 16:44:18 -0700292func emitXrefRule(ctx android.ModuleContext, xrefFile android.WritablePath, idx int,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800293 srcFiles, srcJars android.Paths,
Colin Cross3b706fd2019-09-05 16:44:18 -0700294 flags javaBuilderFlags, deps android.Paths) {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800295
296 deps = append(deps, srcJars...)
Sasha Smundak09950a42019-11-04 16:29:45 -0800297 classpath := flags.classpath
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800298
299 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700300 if flags.javaVersion.usesJavaModules() {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800301 var systemModuleDeps android.Paths
302 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
303 deps = append(deps, systemModuleDeps...)
Sasha Smundak09950a42019-11-04 16:29:45 -0800304 classpath = append(flags.java9Classpath, classpath...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800305 } else {
306 deps = append(deps, flags.bootClasspath...)
307 if len(flags.bootClasspath) == 0 && ctx.Device() {
308 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
309 // ensure java does not fall back to the default bootclasspath.
310 bootClasspath = `-bootclasspath ""`
311 } else {
312 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
313 }
314 }
315
Sasha Smundak09950a42019-11-04 16:29:45 -0800316 deps = append(deps, classpath...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800317 deps = append(deps, flags.processorPath...)
318
319 processor := "-proc:none"
Colin Cross5a116862020-04-22 11:44:34 -0700320 if len(flags.processors) > 0 {
321 processor = "-processor " + strings.Join(flags.processors, ",")
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800322 }
323
Colin Cross3b706fd2019-09-05 16:44:18 -0700324 intermediatesDir := "xref"
325 if idx >= 0 {
326 intermediatesDir += strconv.Itoa(idx)
327 }
328
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800329 ctx.Build(pctx,
330 android.BuildParams{
331 Rule: kytheExtract,
332 Description: "Xref Java extractor",
333 Output: xrefFile,
334 Inputs: srcFiles,
335 Implicits: deps,
336 Args: map[string]string{
337 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, "anno").String(),
338 "bootClasspath": bootClasspath,
Sasha Smundak09950a42019-11-04 16:29:45 -0800339 "classpath": classpath.FormJavaClassPath("-classpath"),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800340 "javacFlags": flags.javacFlags,
Colin Cross1e743852019-10-28 11:37:20 -0700341 "javaVersion": flags.javaVersion.String(),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800342 "outDir": android.PathForModuleOut(ctx, "javac", "classes.xref").String(),
343 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
344 "processor": processor,
345 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, "srcjars.xref").String(),
346 "srcJars": strings.Join(srcJars.Strings(), " "),
347 },
348 })
349}
350
Nan Zhanged19fc32017-10-19 13:06:22 -0700351func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700352 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700353
354 var deps android.Paths
355 deps = append(deps, srcJars...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700356
Colin Cross6cef4812019-10-17 14:23:50 -0700357 classpath := flags.classpath
358
Nan Zhanged19fc32017-10-19 13:06:22 -0700359 var bootClasspath string
Colin Crossbf3119e2019-10-28 14:25:10 -0700360 if flags.javaVersion.usesJavaModules() {
361 var systemModuleDeps android.Paths
362 bootClasspath, systemModuleDeps = flags.systemModules.FormTurbineSystemModulesPath(ctx.Device())
363 deps = append(deps, systemModuleDeps...)
Colin Cross6cef4812019-10-17 14:23:50 -0700364 classpath = append(flags.java9Classpath, classpath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700365 } else {
Colin Crossbf3119e2019-10-28 14:25:10 -0700366 deps = append(deps, flags.bootClasspath...)
367 if len(flags.bootClasspath) == 0 && ctx.Device() {
368 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
369 // ensure turbine does not fall back to the default bootclasspath.
370 bootClasspath = `--bootclasspath ""`
371 } else {
Colin Crossc2557d12019-10-31 15:22:57 -0700372 bootClasspath = flags.bootClasspath.FormTurbineClassPath("--bootclasspath ")
Colin Crossbf3119e2019-10-28 14:25:10 -0700373 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700374 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700375
Colin Cross6cef4812019-10-17 14:23:50 -0700376 deps = append(deps, classpath...)
Colin Crossbf3119e2019-10-28 14:25:10 -0700377 deps = append(deps, flags.processorPath...)
378
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700379 rule := turbine
380 args := map[string]string{
381 "javacFlags": flags.javacFlags,
382 "bootClasspath": bootClasspath,
383 "srcJars": strings.Join(srcJars.Strings(), " "),
384 "classpath": classpath.FormTurbineClassPath("--classpath "),
385 "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
386 "javaVersion": flags.javaVersion.String(),
387 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400388 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_TURBINE") {
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700389 rule = turbineRE
390 args["implicits"] = strings.Join(deps.Strings(), ",")
391 }
Colin Crossae887032017-10-23 17:16:14 -0700392 ctx.Build(pctx, android.BuildParams{
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700393 Rule: rule,
Nan Zhanged19fc32017-10-19 13:06:22 -0700394 Description: "turbine",
395 Output: outputFile,
396 Inputs: srcFiles,
397 Implicits: deps,
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700398 Args: args,
Nan Zhanged19fc32017-10-19 13:06:22 -0700399 })
400}
401
Colin Cross070879e2017-10-11 11:21:07 -0700402// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700403// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
404// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700405//
406// This method may be used for different compilers, including javac and Error Prone. The rule
407// argument specifies which command line to use and desc sets the description of the rule that will
408// be printed at build time. The stem argument provides the file name of the output jar, and
409// suffix will be appended to various intermediate files and directories to avoid collisions when
410// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700411func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700412 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700413 flags javaBuilderFlags, deps android.Paths,
Colin Cross66548102018-06-19 22:47:35 -0700414 intermediatesDir, desc string) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700415
Colin Cross59149b62017-10-16 18:07:29 -0700416 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700417
Colin Cross6cef4812019-10-17 14:23:50 -0700418 classpath := flags.classpath
419
Colin Cross1369cdb2017-09-29 17:58:17 -0700420 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700421 if flags.javaVersion.usesJavaModules() {
Colin Crossb77043e2019-07-16 13:57:13 -0700422 var systemModuleDeps android.Paths
423 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
424 deps = append(deps, systemModuleDeps...)
Colin Cross6cef4812019-10-17 14:23:50 -0700425 classpath = append(flags.java9Classpath, classpath...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700426 } else {
427 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700428 if len(flags.bootClasspath) == 0 && ctx.Device() {
429 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
430 // ensure java does not fall back to the default bootclasspath.
431 bootClasspath = `-bootclasspath ""`
432 } else {
433 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
434 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700435 }
436
Colin Cross6cef4812019-10-17 14:23:50 -0700437 deps = append(deps, classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700438 deps = append(deps, flags.processorPath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700439
Colin Cross7788c122019-01-23 16:14:02 -0800440 processor := "-proc:none"
Colin Cross5a116862020-04-22 11:44:34 -0700441 if len(flags.processors) > 0 {
442 processor = "-processor " + strings.Join(flags.processors, ",")
Colin Crossbe9cdb82019-01-21 21:37:16 -0800443 }
444
Nan Zhang61eaedb2017-11-02 13:28:15 -0700445 srcJarDir := "srcjars"
446 outDir := "classes"
447 annoDir := "anno"
448 if shardIdx >= 0 {
449 shardDir := "shard" + strconv.Itoa(shardIdx)
450 srcJarDir = filepath.Join(shardDir, srcJarDir)
451 outDir = filepath.Join(shardDir, outDir)
452 annoDir = filepath.Join(shardDir, annoDir)
453 }
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400454 rule := javac
Ramy Medhat16f23a42020-09-03 01:29:49 -0400455 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAVAC") {
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400456 rule = javacRE
457 }
Colin Crossae887032017-10-23 17:16:14 -0700458 ctx.Build(pctx, android.BuildParams{
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400459 Rule: rule,
Colin Cross070879e2017-10-11 11:21:07 -0700460 Description: desc,
461 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700462 Inputs: srcFiles,
463 Implicits: deps,
464 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700465 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700466 "bootClasspath": bootClasspath,
Colin Cross6cef4812019-10-17 14:23:50 -0700467 "classpath": classpath.FormJavaClassPath("-classpath"),
Colin Cross6a77c982018-06-19 22:43:34 -0700468 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
Colin Crossbe9cdb82019-01-21 21:37:16 -0800469 "processor": processor,
Colin Cross8eadbf02017-10-24 17:46:00 -0700470 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700471 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
472 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
473 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross1e743852019-10-28 11:37:20 -0700474 "javaVersion": flags.javaVersion.String(),
Colin Crossc6bbef32017-08-14 14:16:06 -0700475 },
476 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700477}
478
Colin Crosse9a275b2017-10-16 17:09:48 -0700479func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
480 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700481
Kousik Kumar366afc52020-05-20 11:27:16 -0700482 rule := jar
Ramy Medhat16f23a42020-09-03 01:29:49 -0400483 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAR") {
Kousik Kumar366afc52020-05-20 11:27:16 -0700484 rule = jarRE
485 }
Colin Crossae887032017-10-23 17:16:14 -0700486 ctx.Build(pctx, android.BuildParams{
Kousik Kumar366afc52020-05-20 11:27:16 -0700487 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700488 Description: "jar",
489 Output: outputFile,
490 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700491 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -0800492 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700493 },
494 })
Colin Cross2fe66872015-03-30 17:20:39 -0700495}
496
Nan Zhanged19fc32017-10-19 13:06:22 -0700497func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
Colin Cross37f6d792018-07-12 12:28:41 -0700498 jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
499 dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700500
Colin Cross635acc92017-09-12 22:50:46 -0700501 var deps android.Paths
502
503 var jarArgs []string
504 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700505 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700506 deps = append(deps, manifest.Path())
507 }
508
Colin Cross37f6d792018-07-12 12:28:41 -0700509 for _, dir := range dirsToStrip {
510 jarArgs = append(jarArgs, "-stripDir ", dir)
511 }
512
513 for _, file := range filesToStrip {
514 jarArgs = append(jarArgs, "-stripFile ", file)
Nan Zhanged19fc32017-10-19 13:06:22 -0700515 }
516
Colin Cross7b60cdd2017-12-21 13:52:58 -0800517 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
518 // for downstream tools like desugar.
519 jarArgs = append(jarArgs, "-stripFile module-info.class")
520
Colin Cross37f6d792018-07-12 12:28:41 -0700521 if stripDirEntries {
Colin Cross635acc92017-09-12 22:50:46 -0700522 jarArgs = append(jarArgs, "-D")
523 }
524
Colin Crossae887032017-10-23 17:16:14 -0700525 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700526 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700527 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700528 Output: outputFile,
529 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700530 Implicits: deps,
531 Args: map[string]string{
532 "jarArgs": strings.Join(jarArgs, " "),
533 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700534 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700535}
536
Colin Crosse9a275b2017-10-16 17:09:48 -0700537func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
538 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700539 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700540 Rule: jarjar,
541 Description: "jarjar",
542 Output: outputFile,
543 Input: classesJar,
544 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700545 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700546 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700547 },
548 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700549}
Colin Cross6ade34f2017-09-15 13:00:47 -0700550
Colin Crossb549b772020-06-03 17:14:31 +0000551func CheckJarPackages(ctx android.ModuleContext, outputFile android.WritablePath,
552 classesJar android.Path, permittedPackages []string) {
Vladimir Marko0975ee02019-04-02 10:29:55 +0100553 ctx.Build(pctx, android.BuildParams{
554 Rule: packageCheck,
555 Description: "packageCheck",
556 Output: outputFile,
557 Input: classesJar,
558 Args: map[string]string{
559 "packages": strings.Join(permittedPackages, " "),
560 },
561 })
562}
563
Nan Zhang4c819fb2018-08-27 18:31:46 -0700564func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath,
565 inputFile android.Path) {
566 ctx.Build(pctx, android.BuildParams{
567 Rule: jetifier,
568 Description: "jetifier",
569 Output: outputFile,
570 Input: inputFile,
571 })
572}
573
Colin Cross094054a2018-10-17 15:10:48 -0700574func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) {
Colin Crosscf371cc2020-11-13 11:48:42 -0800575 android.WriteFileRule(ctx, outputFile, "Main-Class: "+mainClass+"\n")
Colin Cross094054a2018-10-17 15:10:48 -0700576}
577
Colin Cross43f08db2018-11-12 10:13:39 -0800578func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path) {
579 ctx.Build(pctx, android.BuildParams{
580 Rule: zipalign,
581 Description: "align",
582 Input: inputFile,
583 Output: outputFile,
584 })
585}
586
Colin Cross33961b52019-07-11 11:01:22 -0700587type classpath android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700588
Colin Crossc2557d12019-10-31 15:22:57 -0700589func (x *classpath) formJoinedClassPath(optName string, sep string) string {
Colin Cross81440082018-08-15 20:21:55 -0700590 if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") {
591 optName += " "
592 }
Colin Cross59149b62017-10-16 18:07:29 -0700593 if len(*x) > 0 {
Colin Crossc2557d12019-10-31 15:22:57 -0700594 return optName + strings.Join(x.Strings(), sep)
Colin Cross6ade34f2017-09-15 13:00:47 -0700595 } else {
596 return ""
597 }
598}
Colin Crossc2557d12019-10-31 15:22:57 -0700599func (x *classpath) FormJavaClassPath(optName string) string {
600 return x.formJoinedClassPath(optName, ":")
601}
Colin Cross6ade34f2017-09-15 13:00:47 -0700602
Colin Crossc2557d12019-10-31 15:22:57 -0700603func (x *classpath) FormTurbineClassPath(optName string) string {
604 return x.formJoinedClassPath(optName, " ")
605}
606
607// FormRepeatedClassPath returns a list of arguments with the given optName prefixed to each element of the classpath.
608func (x *classpath) FormRepeatedClassPath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700609 if x == nil || *x == nil {
610 return nil
611 }
612 flags := make([]string, len(*x))
613 for i, v := range *x {
Colin Crossafbb1732019-01-17 15:42:52 -0800614 flags[i] = optName + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700615 }
616
617 return flags
618}
619
Colin Cross6ade34f2017-09-15 13:00:47 -0700620// Convert a classpath to an android.Paths
621func (x *classpath) Paths() android.Paths {
622 return append(android.Paths(nil), (*x)...)
623}
624
625func (x *classpath) Strings() []string {
626 if x == nil {
627 return nil
628 }
629 ret := make([]string, len(*x))
630 for i, path := range *x {
631 ret[i] = path.String()
632 }
633 return ret
634}
Colin Crossb77043e2019-07-16 13:57:13 -0700635
636type systemModules struct {
637 dir android.Path
638 deps android.Paths
639}
640
Colin Crossbf3119e2019-10-28 14:25:10 -0700641// Returns a --system argument in the form javac expects with -source 1.9 and the list of files to
642// depend on. If forceEmpty is true, returns --system=none if the list is empty to ensure javac
643// does not fall back to the default system modules.
Colin Crossb77043e2019-07-16 13:57:13 -0700644func (x *systemModules) FormJavaSystemModulesPath(forceEmpty bool) (string, android.Paths) {
645 if x != nil {
646 return "--system=" + x.dir.String(), x.deps
647 } else if forceEmpty {
648 return "--system=none", nil
649 } else {
650 return "", nil
651 }
652}
Colin Crossbf3119e2019-10-28 14:25:10 -0700653
654// Returns a --system argument in the form turbine expects with -source 1.9 and the list of files to
655// depend on. If forceEmpty is true, returns --bootclasspath "" if the list is empty to ensure turbine
656// does not fall back to the default bootclasspath.
657func (x *systemModules) FormTurbineSystemModulesPath(forceEmpty bool) (string, android.Paths) {
658 if x != nil {
659 return "--system " + x.dir.String(), x.deps
660 } else if forceEmpty {
661 return `--bootclasspath ""`, nil
662 } else {
663 return "", nil
664 }
665}