blob: c0fadd42ca8022033fc3f0cab0f803dc3fbb82b8 [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.
Colin Cross77cdcfd2021-03-12 11:28:25 -080043 javac, javacRE = pctx.MultiCommandRemoteStaticRules("javac",
Colin Cross2fe66872015-03-30 17:20:39 -070044 blueprint.RuleParams{
Ulf Adams26d19552020-11-25 23:03:28 +010045 Command: `rm -rf "$outDir" "$annoDir" "$srcJarDir" "$out" && 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 Smundakb0addaf2021-02-16 10:39:40 -080083 _ = pctx.VariableFunc("kytheCuJavaSourceMax",
84 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuJavaSourceMax() })
Sasha Smundak65143642019-09-26 20:14:28 -070085 _ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json")
Sasha Smundak2a4549e2018-11-05 16:49:08 -080086 // Run it with -add-opens=java.base/java.nio=ALL-UNNAMED to avoid JDK9's warning about
87 // "Illegal reflective access by com.google.protobuf.Utf8$UnsafeProcessor ...
88 // to field java.nio.Buffer.address"
89 kytheExtract = pctx.AndroidStaticRule("kythe",
90 blueprint.RuleParams{
91 Command: `${config.ZipSyncCmd} -d $srcJarDir ` +
92 `-l $srcJarDir/list -f "*.java" $srcJars && ` +
93 `( [ ! -s $srcJarDir/list -a ! -s $out.rsp ] || ` +
94 `KYTHE_ROOT_DIRECTORY=. KYTHE_OUTPUT_FILE=$out ` +
95 `KYTHE_CORPUS=${kytheCorpus} ` +
Sasha Smundak65143642019-09-26 20:14:28 -070096 `KYTHE_VNAMES=${kytheVnames} ` +
Sasha Smundak6c2d4f92020-01-09 17:34:23 -080097 `KYTHE_KZIP_ENCODING=${kytheCuEncoding} ` +
Sasha Smundakb0addaf2021-02-16 10:39:40 -080098 `KYTHE_JAVA_SOURCE_BATCH_SIZE=${kytheCuJavaSourceMax} ` +
Sasha Smundak2a4549e2018-11-05 16:49:08 -080099 `${config.SoongJavacWrapper} ${config.JavaCmd} ` +
100 `--add-opens=java.base/java.nio=ALL-UNNAMED ` +
101 `-jar ${config.JavaKytheExtractorJar} ` +
102 `${config.JavacHeapFlags} ${config.CommonJdkFlags} ` +
103 `$processorpath $processor $javacFlags $bootClasspath $classpath ` +
104 `-source $javaVersion -target $javaVersion ` +
105 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list)`,
106 CommandDeps: []string{
107 "${config.JavaCmd}",
108 "${config.JavaKytheExtractorJar}",
Sasha Smundak65143642019-09-26 20:14:28 -0700109 "${kytheVnames}",
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800110 "${config.ZipSyncCmd}",
111 },
112 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
113 Rspfile: "$out.rsp",
114 RspfileContent: "$in",
115 },
116 "javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
117 "outDir", "annoDir", "javaVersion")
118
Sasha Smundaka7856c02020-04-23 09:49:59 -0700119 extractMatchingApks = pctx.StaticRule(
120 "extractMatchingApks",
121 blueprint.RuleParams{
122 Command: `rm -rf "$out" && ` +
Colin Crossffbcd1d2021-11-12 12:19:42 -0800123 `${config.ExtractApksCmd} -o "${out}" -zip "${zip}" -allow-prereleased=${allow-prereleased} ` +
Sasha Smundaka7856c02020-04-23 09:49:59 -0700124 `-sdk-version=${sdk-version} -abis=${abis} ` +
125 `--screen-densities=${screen-densities} --stem=${stem} ` +
Jaewoong Jung11c1e0f2020-06-29 19:18:44 -0700126 `-apkcerts=${apkcerts} -partition=${partition} ` +
Sasha Smundaka7856c02020-04-23 09:49:59 -0700127 `${in}`,
128 CommandDeps: []string{"${config.ExtractApksCmd}"},
129 },
Colin Crossffbcd1d2021-11-12 12:19:42 -0800130 "abis", "allow-prereleased", "screen-densities", "sdk-version", "stem", "apkcerts", "partition", "zip")
Sasha Smundaka7856c02020-04-23 09:49:59 -0700131
Colin Cross77cdcfd2021-03-12 11:28:25 -0800132 turbine, turbineRE = pctx.RemoteStaticRules("turbine",
Nan Zhanged19fc32017-10-19 13:06:22 -0700133 blueprint.RuleParams{
Colin Crossf61766e2022-03-16 18:06:48 -0700134 Command: `$reTemplate${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.TurbineJar} $outputFlags ` +
Colin Cross411647e2022-03-16 14:28:36 -0700135 `--sources @$out.rsp --source_jars $srcJars ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700136 `--javacopts ${config.CommonJdkFlags} ` +
Colin Crossf61766e2022-03-16 18:06:48 -0700137 `$javacFlags -source $javaVersion -target $javaVersion -- $turbineFlags && ` +
138 `(for o in $outputs; do if cmp -s $${o}.tmp $${o} ; then rm $${o}.tmp ; else mv $${o}.tmp $${o} ; fi; done )`,
Colin Cross8eadbf02017-10-24 17:46:00 -0700139 CommandDeps: []string{
140 "${config.TurbineJar}",
141 "${config.JavaCmd}",
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"},
Colin Cross411647e2022-03-16 14:28:36 -0700148 ExecStrategy: "${config.RETurbineExecStrategy}",
149 Inputs: []string{"${config.TurbineJar}", "${out}.rsp", "$implicits"},
150 RSPFiles: []string{"${out}.rsp"},
Colin Crossf61766e2022-03-16 18:06:48 -0700151 OutputFiles: []string{"$rbeOutputs"},
Colin Cross411647e2022-03-16 14:28:36 -0700152 ToolchainInputs: []string{"${config.JavaCmd}"},
153 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
Colin Crossf61766e2022-03-16 18:06:48 -0700154 },
155 []string{"javacFlags", "turbineFlags", "outputFlags", "javaVersion", "outputs", "rbeOutputs", "srcJars"}, []string{"implicits"})
Nan Zhanged19fc32017-10-19 13:06:22 -0700156
Colin Cross77cdcfd2021-03-12 11:28:25 -0800157 jar, jarRE = pctx.RemoteStaticRules("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"},
Colin Crossa4eafdd2021-03-24 14:09:28 -0700167 RSPFiles: []string{"${out}.rsp"},
Kousik Kumar366afc52020-05-20 11:27:16 -0700168 OutputFiles: []string{"$out"},
169 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
170 }, []string{"jarArgs"}, nil)
Colin Cross0a6e0072017-08-30 14:24:55 -0700171
Colin Cross77cdcfd2021-03-12 11:28:25 -0800172 zip, zipRE = pctx.RemoteStaticRules("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"},
Colin Crossa4eafdd2021-03-24 14:09:28 -0700182 RSPFiles: []string{"${out}.rsp"},
Kousik Kumar366afc52020-05-20 11:27:16 -0700183 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{
Colin Crossd97cf632021-02-08 10:14:04 -0800196 Command: "" +
197 // Jarjar doesn't exit with an error when the rules file contains a syntax error,
198 // leading to stale or missing files later in the build. Remove the output file
199 // before running jarjar.
200 "rm -f ${out} && " +
201 "${config.JavaCmd} ${config.JavaVmFlags}" +
Artur Satayev762d9f32020-04-15 12:50:04 +0100202 // b/146418363 Enable Android specific jarjar transformer to drop compat annotations
203 // for newly repackaged classes. Dropping @UnsupportedAppUsage on repackaged classes
204 // avoids adding new hiddenapis after jarjar'ing.
205 " -DremoveAndroidCompatAnnotations=true" +
Colin Crossd97cf632021-02-08 10:14:04 -0800206 " -jar ${config.JarjarCmd} process $rulesFile $in $out && " +
207 // Turn a missing output file into a ninja error
208 `[ -e ${out} ] || (echo "Missing output file"; exit 1)`,
Colin Cross64162712017-08-08 13:17:59 -0700209 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700210 },
211 "rulesFile")
Nan Zhang4c819fb2018-08-27 18:31:46 -0700212
Vladimir Marko0975ee02019-04-02 10:29:55 +0100213 packageCheck = pctx.AndroidStaticRule("packageCheck",
214 blueprint.RuleParams{
215 Command: "rm -f $out && " +
216 "${config.PackageCheckCmd} $in $packages && " +
Colin Crossb549b772020-06-03 17:14:31 +0000217 "touch $out",
Vladimir Marko0975ee02019-04-02 10:29:55 +0100218 CommandDeps: []string{"${config.PackageCheckCmd}"},
219 },
220 "packages")
221
Nan Zhang4c819fb2018-08-27 18:31:46 -0700222 jetifier = pctx.AndroidStaticRule("jetifier",
223 blueprint.RuleParams{
Sasha Smundak26c6d9e2019-06-11 13:30:13 -0700224 Command: "${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JetifierJar} -l error -o $out -i $in",
Nan Zhang4c819fb2018-08-27 18:31:46 -0700225 CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"},
226 },
227 )
Colin Cross43f08db2018-11-12 10:13:39 -0800228
229 zipalign = pctx.AndroidStaticRule("zipalign",
230 blueprint.RuleParams{
Colin Crosse4246ab2019-02-05 21:55:21 -0800231 Command: "if ! ${config.ZipAlign} -c -p 4 $in > /dev/null; then " +
232 "${config.ZipAlign} -f -p 4 $in $out; " +
Colin Cross43f08db2018-11-12 10:13:39 -0800233 "else " +
234 "cp -f $in $out; " +
235 "fi",
236 CommandDeps: []string{"${config.ZipAlign}"},
237 },
238 )
Colin Cross2fe66872015-03-30 17:20:39 -0700239)
240
241func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700242 pctx.Import("android/soong/android")
Colin Cross64162712017-08-08 13:17:59 -0700243 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700244}
245
246type javaBuilderFlags struct {
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700247 javacFlags string
248
249 // bootClasspath is the list of jars that form the boot classpath (generally the java.* and
250 // android.* classes) for tools that still use it. javac targeting 1.9 or higher uses
251 // systemModules and java9Classpath instead.
252 bootClasspath classpath
253
254 // classpath is the list of jars that form the classpath for javac and kotlinc rules. It
255 // contains header jars for all static and non-static dependencies.
256 classpath classpath
257
258 // dexClasspath is the list of jars that form the classpath for d8 and r8 rules. It contains
259 // header jars for all non-static dependencies. Static dependencies have already been
260 // combined into the program jar.
261 dexClasspath classpath
262
263 // java9Classpath is the list of jars that will be added to the classpath when targeting
264 // 1.9 or higher. It generally contains the android.* classes, while the java.* classes
265 // are provided by systemModules.
Colin Cross6cef4812019-10-17 14:23:50 -0700266 java9Classpath classpath
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700267
268 processorPath classpath
269 processors []string
270 systemModules *systemModules
271 aidlFlags string
272 aidlDeps android.Paths
273 javaVersion javaVersion
Colin Cross6af17aa2017-09-20 12:59:05 -0700274
Andreas Gampef3e5b552018-01-22 21:27:21 -0800275 errorProneExtraJavacFlags string
Colin Cross66548102018-06-19 22:47:35 -0700276 errorProneProcessorPath classpath
Andreas Gampef3e5b552018-01-22 21:27:21 -0800277
Colin Cross93e85952017-08-15 13:34:18 -0700278 kotlincFlags string
279 kotlincClasspath classpath
Colin Crossa1ff7c62021-09-17 14:11:52 -0700280 kotlincDeps android.Paths
Colin Cross93e85952017-08-15 13:34:18 -0700281
Colin Cross19878da2019-03-28 14:45:07 -0700282 proto android.ProtoFlags
Colin Cross2fe66872015-03-30 17:20:39 -0700283}
284
Nan Zhang61eaedb2017-11-02 13:28:15 -0700285func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
286 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700287
Nan Zhang61eaedb2017-11-02 13:28:15 -0700288 // Compile java sources into .class files
289 desc := "javac"
290 if shardIdx >= 0 {
291 desc += strconv.Itoa(shardIdx)
292 }
293
Colin Cross66548102018-06-19 22:47:35 -0700294 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc)
Colin Cross2fe66872015-03-30 17:20:39 -0700295}
296
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800297// Emits the rule to generate Xref input file (.kzip file) for the given set of source files and source jars
298// to compile with given set of builder flags, etc.
Colin Cross3b706fd2019-09-05 16:44:18 -0700299func emitXrefRule(ctx android.ModuleContext, xrefFile android.WritablePath, idx int,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800300 srcFiles, srcJars android.Paths,
Colin Cross3b706fd2019-09-05 16:44:18 -0700301 flags javaBuilderFlags, deps android.Paths) {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800302
303 deps = append(deps, srcJars...)
Sasha Smundak09950a42019-11-04 16:29:45 -0800304 classpath := flags.classpath
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800305
306 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700307 if flags.javaVersion.usesJavaModules() {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800308 var systemModuleDeps android.Paths
309 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
310 deps = append(deps, systemModuleDeps...)
Sasha Smundak09950a42019-11-04 16:29:45 -0800311 classpath = append(flags.java9Classpath, classpath...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800312 } else {
313 deps = append(deps, flags.bootClasspath...)
314 if len(flags.bootClasspath) == 0 && ctx.Device() {
315 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
316 // ensure java does not fall back to the default bootclasspath.
317 bootClasspath = `-bootclasspath ""`
318 } else {
319 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
320 }
321 }
322
Sasha Smundak09950a42019-11-04 16:29:45 -0800323 deps = append(deps, classpath...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800324 deps = append(deps, flags.processorPath...)
325
326 processor := "-proc:none"
Colin Cross5a116862020-04-22 11:44:34 -0700327 if len(flags.processors) > 0 {
328 processor = "-processor " + strings.Join(flags.processors, ",")
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800329 }
330
Colin Cross3b706fd2019-09-05 16:44:18 -0700331 intermediatesDir := "xref"
332 if idx >= 0 {
333 intermediatesDir += strconv.Itoa(idx)
334 }
335
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800336 ctx.Build(pctx,
337 android.BuildParams{
338 Rule: kytheExtract,
339 Description: "Xref Java extractor",
340 Output: xrefFile,
341 Inputs: srcFiles,
342 Implicits: deps,
343 Args: map[string]string{
344 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, "anno").String(),
345 "bootClasspath": bootClasspath,
Sasha Smundak09950a42019-11-04 16:29:45 -0800346 "classpath": classpath.FormJavaClassPath("-classpath"),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800347 "javacFlags": flags.javacFlags,
Colin Cross1e743852019-10-28 11:37:20 -0700348 "javaVersion": flags.javaVersion.String(),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800349 "outDir": android.PathForModuleOut(ctx, "javac", "classes.xref").String(),
350 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
351 "processor": processor,
352 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, "srcjars.xref").String(),
353 "srcJars": strings.Join(srcJars.Strings(), " "),
354 },
355 })
356}
357
Colin Crossf61766e2022-03-16 18:06:48 -0700358func turbineFlags(ctx android.ModuleContext, flags javaBuilderFlags) (string, android.Paths) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700359 var deps android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700360
Colin Cross6cef4812019-10-17 14:23:50 -0700361 classpath := flags.classpath
362
Nan Zhanged19fc32017-10-19 13:06:22 -0700363 var bootClasspath string
Colin Crossbf3119e2019-10-28 14:25:10 -0700364 if flags.javaVersion.usesJavaModules() {
365 var systemModuleDeps android.Paths
366 bootClasspath, systemModuleDeps = flags.systemModules.FormTurbineSystemModulesPath(ctx.Device())
367 deps = append(deps, systemModuleDeps...)
Colin Cross6cef4812019-10-17 14:23:50 -0700368 classpath = append(flags.java9Classpath, classpath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700369 } else {
Colin Crossbf3119e2019-10-28 14:25:10 -0700370 deps = append(deps, flags.bootClasspath...)
371 if len(flags.bootClasspath) == 0 && ctx.Device() {
372 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
373 // ensure turbine does not fall back to the default bootclasspath.
374 bootClasspath = `--bootclasspath ""`
375 } else {
Colin Crossc2557d12019-10-31 15:22:57 -0700376 bootClasspath = flags.bootClasspath.FormTurbineClassPath("--bootclasspath ")
Colin Crossbf3119e2019-10-28 14:25:10 -0700377 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700378 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700379
Colin Cross6cef4812019-10-17 14:23:50 -0700380 deps = append(deps, classpath...)
Colin Crossf61766e2022-03-16 18:06:48 -0700381 turbineFlags := bootClasspath + " " + classpath.FormTurbineClassPath("--classpath ")
382
383 return turbineFlags, deps
384}
385
386func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
387 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
388
389 turbineFlags, deps := turbineFlags(ctx, flags)
390
391 deps = append(deps, srcJars...)
Colin Crossbf3119e2019-10-28 14:25:10 -0700392
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700393 rule := turbine
394 args := map[string]string{
Colin Crossf61766e2022-03-16 18:06:48 -0700395 "javacFlags": flags.javacFlags,
396 "srcJars": strings.Join(srcJars.Strings(), " "),
397 "javaVersion": flags.javaVersion.String(),
398 "turbineFlags": turbineFlags,
399 "outputFlags": "--output " + outputFile.String() + ".tmp",
400 "outputs": outputFile.String(),
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700401 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400402 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_TURBINE") {
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700403 rule = turbineRE
404 args["implicits"] = strings.Join(deps.Strings(), ",")
Colin Crossf61766e2022-03-16 18:06:48 -0700405 args["rbeOutputs"] = outputFile.String() + ".tmp"
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700406 }
Colin Crossae887032017-10-23 17:16:14 -0700407 ctx.Build(pctx, android.BuildParams{
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700408 Rule: rule,
Nan Zhanged19fc32017-10-19 13:06:22 -0700409 Description: "turbine",
410 Output: outputFile,
411 Inputs: srcFiles,
412 Implicits: deps,
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700413 Args: args,
Nan Zhanged19fc32017-10-19 13:06:22 -0700414 })
415}
416
Colin Crossf61766e2022-03-16 18:06:48 -0700417// TurbineApt produces a rule to run annotation processors using turbine.
418func TurbineApt(ctx android.ModuleContext, outputSrcJar, outputResJar android.WritablePath,
419 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
420
421 turbineFlags, deps := turbineFlags(ctx, flags)
422
423 deps = append(deps, srcJars...)
424
425 deps = append(deps, flags.processorPath...)
426 turbineFlags += " " + flags.processorPath.FormTurbineClassPath("--processorpath ")
427 turbineFlags += " --processors " + strings.Join(flags.processors, " ")
428
429 outputs := android.WritablePaths{outputSrcJar, outputResJar}
430 outputFlags := "--gensrc_output " + outputSrcJar.String() + ".tmp " +
431 "--resource_output " + outputResJar.String() + ".tmp"
432
433 rule := turbine
434 args := map[string]string{
435 "javacFlags": flags.javacFlags,
436 "srcJars": strings.Join(srcJars.Strings(), " "),
437 "javaVersion": flags.javaVersion.String(),
438 "turbineFlags": turbineFlags,
439 "outputFlags": outputFlags,
440 "outputs": strings.Join(outputs.Strings(), " "),
441 }
442 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_TURBINE") {
443 rule = turbineRE
444 args["implicits"] = strings.Join(deps.Strings(), ",")
445 args["rbeOutputs"] = outputSrcJar.String() + ".tmp," + outputResJar.String() + ".tmp"
446 }
447 ctx.Build(pctx, android.BuildParams{
448 Rule: rule,
449 Description: "turbine apt",
450 Output: outputs[0],
451 ImplicitOutputs: outputs[1:],
452 Inputs: srcFiles,
453 Implicits: deps,
454 Args: args,
455 })
456}
457
Colin Cross070879e2017-10-11 11:21:07 -0700458// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700459// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
460// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700461//
462// This method may be used for different compilers, including javac and Error Prone. The rule
463// argument specifies which command line to use and desc sets the description of the rule that will
464// be printed at build time. The stem argument provides the file name of the output jar, and
465// suffix will be appended to various intermediate files and directories to avoid collisions when
466// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700467func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700468 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700469 flags javaBuilderFlags, deps android.Paths,
Colin Cross66548102018-06-19 22:47:35 -0700470 intermediatesDir, desc string) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700471
Colin Cross59149b62017-10-16 18:07:29 -0700472 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700473
Colin Cross6cef4812019-10-17 14:23:50 -0700474 classpath := flags.classpath
475
Colin Cross1369cdb2017-09-29 17:58:17 -0700476 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700477 if flags.javaVersion.usesJavaModules() {
Colin Crossb77043e2019-07-16 13:57:13 -0700478 var systemModuleDeps android.Paths
479 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
480 deps = append(deps, systemModuleDeps...)
Colin Cross6cef4812019-10-17 14:23:50 -0700481 classpath = append(flags.java9Classpath, classpath...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700482 } else {
483 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700484 if len(flags.bootClasspath) == 0 && ctx.Device() {
485 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
486 // ensure java does not fall back to the default bootclasspath.
487 bootClasspath = `-bootclasspath ""`
488 } else {
489 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
490 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700491 }
492
Colin Cross6cef4812019-10-17 14:23:50 -0700493 deps = append(deps, classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700494 deps = append(deps, flags.processorPath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700495
Colin Cross7788c122019-01-23 16:14:02 -0800496 processor := "-proc:none"
Colin Cross5a116862020-04-22 11:44:34 -0700497 if len(flags.processors) > 0 {
498 processor = "-processor " + strings.Join(flags.processors, ",")
Colin Crossbe9cdb82019-01-21 21:37:16 -0800499 }
500
Nan Zhang61eaedb2017-11-02 13:28:15 -0700501 srcJarDir := "srcjars"
502 outDir := "classes"
503 annoDir := "anno"
504 if shardIdx >= 0 {
505 shardDir := "shard" + strconv.Itoa(shardIdx)
506 srcJarDir = filepath.Join(shardDir, srcJarDir)
507 outDir = filepath.Join(shardDir, outDir)
508 annoDir = filepath.Join(shardDir, annoDir)
509 }
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400510 rule := javac
Ramy Medhat16f23a42020-09-03 01:29:49 -0400511 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAVAC") {
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400512 rule = javacRE
513 }
Colin Crossae887032017-10-23 17:16:14 -0700514 ctx.Build(pctx, android.BuildParams{
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400515 Rule: rule,
Colin Cross070879e2017-10-11 11:21:07 -0700516 Description: desc,
517 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700518 Inputs: srcFiles,
519 Implicits: deps,
520 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700521 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700522 "bootClasspath": bootClasspath,
Colin Cross6cef4812019-10-17 14:23:50 -0700523 "classpath": classpath.FormJavaClassPath("-classpath"),
Colin Cross6a77c982018-06-19 22:43:34 -0700524 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
Colin Crossbe9cdb82019-01-21 21:37:16 -0800525 "processor": processor,
Colin Cross8eadbf02017-10-24 17:46:00 -0700526 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700527 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
528 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
529 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross1e743852019-10-28 11:37:20 -0700530 "javaVersion": flags.javaVersion.String(),
Colin Crossc6bbef32017-08-14 14:16:06 -0700531 },
532 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700533}
534
Colin Crosse9a275b2017-10-16 17:09:48 -0700535func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
536 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700537
Kousik Kumar366afc52020-05-20 11:27:16 -0700538 rule := jar
Ramy Medhat16f23a42020-09-03 01:29:49 -0400539 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAR") {
Kousik Kumar366afc52020-05-20 11:27:16 -0700540 rule = jarRE
541 }
Colin Crossae887032017-10-23 17:16:14 -0700542 ctx.Build(pctx, android.BuildParams{
Kousik Kumar366afc52020-05-20 11:27:16 -0700543 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700544 Description: "jar",
545 Output: outputFile,
546 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700547 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -0800548 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700549 },
550 })
Colin Cross2fe66872015-03-30 17:20:39 -0700551}
552
Nan Zhanged19fc32017-10-19 13:06:22 -0700553func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
Colin Cross37f6d792018-07-12 12:28:41 -0700554 jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
555 dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700556
Colin Cross635acc92017-09-12 22:50:46 -0700557 var deps android.Paths
558
559 var jarArgs []string
560 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700561 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700562 deps = append(deps, manifest.Path())
563 }
564
Colin Cross37f6d792018-07-12 12:28:41 -0700565 for _, dir := range dirsToStrip {
566 jarArgs = append(jarArgs, "-stripDir ", dir)
567 }
568
569 for _, file := range filesToStrip {
570 jarArgs = append(jarArgs, "-stripFile ", file)
Nan Zhanged19fc32017-10-19 13:06:22 -0700571 }
572
Colin Cross7b60cdd2017-12-21 13:52:58 -0800573 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
574 // for downstream tools like desugar.
575 jarArgs = append(jarArgs, "-stripFile module-info.class")
576
Colin Cross37f6d792018-07-12 12:28:41 -0700577 if stripDirEntries {
Colin Cross635acc92017-09-12 22:50:46 -0700578 jarArgs = append(jarArgs, "-D")
579 }
580
Colin Crossae887032017-10-23 17:16:14 -0700581 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700582 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700583 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700584 Output: outputFile,
585 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700586 Implicits: deps,
587 Args: map[string]string{
588 "jarArgs": strings.Join(jarArgs, " "),
589 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700590 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700591}
592
Colin Crosse9a275b2017-10-16 17:09:48 -0700593func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
594 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700595 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700596 Rule: jarjar,
597 Description: "jarjar",
598 Output: outputFile,
599 Input: classesJar,
600 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700601 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700602 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700603 },
604 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700605}
Colin Cross6ade34f2017-09-15 13:00:47 -0700606
Colin Crossb549b772020-06-03 17:14:31 +0000607func CheckJarPackages(ctx android.ModuleContext, outputFile android.WritablePath,
608 classesJar android.Path, permittedPackages []string) {
Vladimir Marko0975ee02019-04-02 10:29:55 +0100609 ctx.Build(pctx, android.BuildParams{
610 Rule: packageCheck,
611 Description: "packageCheck",
612 Output: outputFile,
613 Input: classesJar,
614 Args: map[string]string{
615 "packages": strings.Join(permittedPackages, " "),
616 },
617 })
618}
619
Nan Zhang4c819fb2018-08-27 18:31:46 -0700620func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath,
621 inputFile android.Path) {
622 ctx.Build(pctx, android.BuildParams{
623 Rule: jetifier,
624 Description: "jetifier",
625 Output: outputFile,
626 Input: inputFile,
627 })
628}
629
Colin Cross094054a2018-10-17 15:10:48 -0700630func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) {
Colin Crosscf371cc2020-11-13 11:48:42 -0800631 android.WriteFileRule(ctx, outputFile, "Main-Class: "+mainClass+"\n")
Colin Cross094054a2018-10-17 15:10:48 -0700632}
633
Colin Cross43f08db2018-11-12 10:13:39 -0800634func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path) {
635 ctx.Build(pctx, android.BuildParams{
636 Rule: zipalign,
637 Description: "align",
638 Input: inputFile,
639 Output: outputFile,
640 })
641}
642
Colin Cross33961b52019-07-11 11:01:22 -0700643type classpath android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700644
Colin Crossc2557d12019-10-31 15:22:57 -0700645func (x *classpath) formJoinedClassPath(optName string, sep string) string {
Colin Cross81440082018-08-15 20:21:55 -0700646 if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") {
647 optName += " "
648 }
Colin Cross59149b62017-10-16 18:07:29 -0700649 if len(*x) > 0 {
Colin Crossc2557d12019-10-31 15:22:57 -0700650 return optName + strings.Join(x.Strings(), sep)
Colin Cross6ade34f2017-09-15 13:00:47 -0700651 } else {
652 return ""
653 }
654}
Colin Crossc2557d12019-10-31 15:22:57 -0700655func (x *classpath) FormJavaClassPath(optName string) string {
656 return x.formJoinedClassPath(optName, ":")
657}
Colin Cross6ade34f2017-09-15 13:00:47 -0700658
Colin Crossc2557d12019-10-31 15:22:57 -0700659func (x *classpath) FormTurbineClassPath(optName string) string {
660 return x.formJoinedClassPath(optName, " ")
661}
662
663// FormRepeatedClassPath returns a list of arguments with the given optName prefixed to each element of the classpath.
664func (x *classpath) FormRepeatedClassPath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700665 if x == nil || *x == nil {
666 return nil
667 }
668 flags := make([]string, len(*x))
669 for i, v := range *x {
Colin Crossafbb1732019-01-17 15:42:52 -0800670 flags[i] = optName + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700671 }
672
673 return flags
674}
675
Colin Cross6ade34f2017-09-15 13:00:47 -0700676// Convert a classpath to an android.Paths
677func (x *classpath) Paths() android.Paths {
678 return append(android.Paths(nil), (*x)...)
679}
680
681func (x *classpath) Strings() []string {
682 if x == nil {
683 return nil
684 }
685 ret := make([]string, len(*x))
686 for i, path := range *x {
687 ret[i] = path.String()
688 }
689 return ret
690}
Colin Crossb77043e2019-07-16 13:57:13 -0700691
692type systemModules struct {
693 dir android.Path
694 deps android.Paths
695}
696
Colin Crossbf3119e2019-10-28 14:25:10 -0700697// Returns a --system argument in the form javac expects with -source 1.9 and the list of files to
698// depend on. If forceEmpty is true, returns --system=none if the list is empty to ensure javac
699// does not fall back to the default system modules.
Colin Crossb77043e2019-07-16 13:57:13 -0700700func (x *systemModules) FormJavaSystemModulesPath(forceEmpty bool) (string, android.Paths) {
701 if x != nil {
702 return "--system=" + x.dir.String(), x.deps
703 } else if forceEmpty {
704 return "--system=none", nil
705 } else {
706 return "", nil
707 }
708}
Colin Crossbf3119e2019-10-28 14:25:10 -0700709
710// Returns a --system argument in the form turbine expects with -source 1.9 and the list of files to
711// depend on. If forceEmpty is true, returns --bootclasspath "" if the list is empty to ensure turbine
712// does not fall back to the default bootclasspath.
713func (x *systemModules) FormTurbineSystemModulesPath(forceEmpty bool) (string, android.Paths) {
714 if x != nil {
715 return "--system " + x.dir.String(), x.deps
716 } else if forceEmpty {
717 return `--bootclasspath ""`, nil
718 } else {
Colin Crossf61766e2022-03-16 18:06:48 -0700719 return "--system ${config.JavaHome}", nil
Colin Crossbf3119e2019-10-28 14:25:10 -0700720 }
721}