blob: 1a115a4adea8f9075a24df3ebe3a8ed6a34b2f0b [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 Cross411647e2022-03-16 14:28:36 -0700134 Command: `$reTemplate${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.TurbineJar} --output $out.tmp ` +
135 `--sources @$out.rsp --source_jars $srcJars ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700136 `--javacopts ${config.CommonJdkFlags} ` +
Colin Cross924a0aa2018-03-07 10:51:05 -0800137 `$javacFlags -source $javaVersion -target $javaVersion -- $bootClasspath $classpath && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700138 `(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`,
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"},
151 OutputFiles: []string{"$out.tmp"},
152 ToolchainInputs: []string{"${config.JavaCmd}"},
153 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
154 }, []string{"javacFlags", "bootClasspath", "classpath", "srcJars", "javaVersion"}, []string{"implicits"})
Nan Zhanged19fc32017-10-19 13:06:22 -0700155
Colin Cross77cdcfd2021-03-12 11:28:25 -0800156 jar, jarRE = pctx.RemoteStaticRules("jar",
Colin Cross2fe66872015-03-30 17:20:39 -0700157 blueprint.RuleParams{
Kousik Kumar366afc52020-05-20 11:27:16 -0700158 Command: `$reTemplate${config.SoongZipCmd} -jar -o $out @$out.rsp`,
Nan Zhang674dd932018-01-26 18:30:36 -0800159 CommandDeps: []string{"${config.SoongZipCmd}"},
160 Rspfile: "$out.rsp",
161 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -0700162 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700163 &remoteexec.REParams{
164 ExecStrategy: "${config.REJarExecStrategy}",
165 Inputs: []string{"${config.SoongZipCmd}", "${out}.rsp"},
Colin Crossa4eafdd2021-03-24 14:09:28 -0700166 RSPFiles: []string{"${out}.rsp"},
Kousik Kumar366afc52020-05-20 11:27:16 -0700167 OutputFiles: []string{"$out"},
168 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
169 }, []string{"jarArgs"}, nil)
Colin Cross0a6e0072017-08-30 14:24:55 -0700170
Colin Cross77cdcfd2021-03-12 11:28:25 -0800171 zip, zipRE = pctx.RemoteStaticRules("zip",
Colin Crossa4f08812018-10-02 22:03:40 -0700172 blueprint.RuleParams{
173 Command: `${config.SoongZipCmd} -o $out @$out.rsp`,
174 CommandDeps: []string{"${config.SoongZipCmd}"},
175 Rspfile: "$out.rsp",
176 RspfileContent: "$jarArgs",
177 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700178 &remoteexec.REParams{
179 ExecStrategy: "${config.REZipExecStrategy}",
180 Inputs: []string{"${config.SoongZipCmd}", "${out}.rsp", "$implicits"},
Colin Crossa4eafdd2021-03-24 14:09:28 -0700181 RSPFiles: []string{"${out}.rsp"},
Kousik Kumar366afc52020-05-20 11:27:16 -0700182 OutputFiles: []string{"$out"},
183 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
184 }, []string{"jarArgs"}, []string{"implicits"})
Colin Crossa4f08812018-10-02 22:03:40 -0700185
Colin Cross0a6e0072017-08-30 14:24:55 -0700186 combineJar = pctx.AndroidStaticRule("combineJar",
187 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800188 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700189 CommandDeps: []string{"${config.MergeZipsCmd}"},
190 },
Colin Cross635acc92017-09-12 22:50:46 -0700191 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700192
Colin Cross9d45bb72016-08-29 16:14:13 -0700193 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700194 blueprint.RuleParams{
Colin Crossd97cf632021-02-08 10:14:04 -0800195 Command: "" +
196 // Jarjar doesn't exit with an error when the rules file contains a syntax error,
197 // leading to stale or missing files later in the build. Remove the output file
198 // before running jarjar.
199 "rm -f ${out} && " +
200 "${config.JavaCmd} ${config.JavaVmFlags}" +
Artur Satayev762d9f32020-04-15 12:50:04 +0100201 // b/146418363 Enable Android specific jarjar transformer to drop compat annotations
202 // for newly repackaged classes. Dropping @UnsupportedAppUsage on repackaged classes
203 // avoids adding new hiddenapis after jarjar'ing.
204 " -DremoveAndroidCompatAnnotations=true" +
Colin Crossd97cf632021-02-08 10:14:04 -0800205 " -jar ${config.JarjarCmd} process $rulesFile $in $out && " +
206 // Turn a missing output file into a ninja error
207 `[ -e ${out} ] || (echo "Missing output file"; exit 1)`,
Colin Cross64162712017-08-08 13:17:59 -0700208 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700209 },
210 "rulesFile")
Nan Zhang4c819fb2018-08-27 18:31:46 -0700211
Vladimir Marko0975ee02019-04-02 10:29:55 +0100212 packageCheck = pctx.AndroidStaticRule("packageCheck",
213 blueprint.RuleParams{
214 Command: "rm -f $out && " +
215 "${config.PackageCheckCmd} $in $packages && " +
Colin Crossb549b772020-06-03 17:14:31 +0000216 "touch $out",
Vladimir Marko0975ee02019-04-02 10:29:55 +0100217 CommandDeps: []string{"${config.PackageCheckCmd}"},
218 },
219 "packages")
220
Nan Zhang4c819fb2018-08-27 18:31:46 -0700221 jetifier = pctx.AndroidStaticRule("jetifier",
222 blueprint.RuleParams{
Sasha Smundak26c6d9e2019-06-11 13:30:13 -0700223 Command: "${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JetifierJar} -l error -o $out -i $in",
Nan Zhang4c819fb2018-08-27 18:31:46 -0700224 CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"},
225 },
226 )
Colin Cross43f08db2018-11-12 10:13:39 -0800227
228 zipalign = pctx.AndroidStaticRule("zipalign",
229 blueprint.RuleParams{
Colin Crosse4246ab2019-02-05 21:55:21 -0800230 Command: "if ! ${config.ZipAlign} -c -p 4 $in > /dev/null; then " +
231 "${config.ZipAlign} -f -p 4 $in $out; " +
Colin Cross43f08db2018-11-12 10:13:39 -0800232 "else " +
233 "cp -f $in $out; " +
234 "fi",
235 CommandDeps: []string{"${config.ZipAlign}"},
236 },
237 )
Colin Cross2fe66872015-03-30 17:20:39 -0700238)
239
240func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700241 pctx.Import("android/soong/android")
Colin Cross64162712017-08-08 13:17:59 -0700242 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700243}
244
245type javaBuilderFlags struct {
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700246 javacFlags string
247
248 // bootClasspath is the list of jars that form the boot classpath (generally the java.* and
249 // android.* classes) for tools that still use it. javac targeting 1.9 or higher uses
250 // systemModules and java9Classpath instead.
251 bootClasspath classpath
252
253 // classpath is the list of jars that form the classpath for javac and kotlinc rules. It
254 // contains header jars for all static and non-static dependencies.
255 classpath classpath
256
257 // dexClasspath is the list of jars that form the classpath for d8 and r8 rules. It contains
258 // header jars for all non-static dependencies. Static dependencies have already been
259 // combined into the program jar.
260 dexClasspath classpath
261
262 // java9Classpath is the list of jars that will be added to the classpath when targeting
263 // 1.9 or higher. It generally contains the android.* classes, while the java.* classes
264 // are provided by systemModules.
Colin Cross6cef4812019-10-17 14:23:50 -0700265 java9Classpath classpath
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700266
267 processorPath classpath
268 processors []string
269 systemModules *systemModules
270 aidlFlags string
271 aidlDeps android.Paths
272 javaVersion javaVersion
Colin Cross6af17aa2017-09-20 12:59:05 -0700273
Andreas Gampef3e5b552018-01-22 21:27:21 -0800274 errorProneExtraJavacFlags string
Colin Cross66548102018-06-19 22:47:35 -0700275 errorProneProcessorPath classpath
Andreas Gampef3e5b552018-01-22 21:27:21 -0800276
Colin Cross93e85952017-08-15 13:34:18 -0700277 kotlincFlags string
278 kotlincClasspath classpath
Colin Crossa1ff7c62021-09-17 14:11:52 -0700279 kotlincDeps android.Paths
Colin Cross93e85952017-08-15 13:34:18 -0700280
Colin Cross19878da2019-03-28 14:45:07 -0700281 proto android.ProtoFlags
Colin Cross2fe66872015-03-30 17:20:39 -0700282}
283
Nan Zhang61eaedb2017-11-02 13:28:15 -0700284func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
285 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700286
Nan Zhang61eaedb2017-11-02 13:28:15 -0700287 // Compile java sources into .class files
288 desc := "javac"
289 if shardIdx >= 0 {
290 desc += strconv.Itoa(shardIdx)
291 }
292
Colin Cross66548102018-06-19 22:47:35 -0700293 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc)
Colin Cross2fe66872015-03-30 17:20:39 -0700294}
295
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800296// Emits the rule to generate Xref input file (.kzip file) for the given set of source files and source jars
297// to compile with given set of builder flags, etc.
Colin Cross3b706fd2019-09-05 16:44:18 -0700298func emitXrefRule(ctx android.ModuleContext, xrefFile android.WritablePath, idx int,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800299 srcFiles, srcJars android.Paths,
Colin Cross3b706fd2019-09-05 16:44:18 -0700300 flags javaBuilderFlags, deps android.Paths) {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800301
302 deps = append(deps, srcJars...)
Sasha Smundak09950a42019-11-04 16:29:45 -0800303 classpath := flags.classpath
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800304
305 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700306 if flags.javaVersion.usesJavaModules() {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800307 var systemModuleDeps android.Paths
308 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
309 deps = append(deps, systemModuleDeps...)
Sasha Smundak09950a42019-11-04 16:29:45 -0800310 classpath = append(flags.java9Classpath, classpath...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800311 } else {
312 deps = append(deps, flags.bootClasspath...)
313 if len(flags.bootClasspath) == 0 && ctx.Device() {
314 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
315 // ensure java does not fall back to the default bootclasspath.
316 bootClasspath = `-bootclasspath ""`
317 } else {
318 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
319 }
320 }
321
Sasha Smundak09950a42019-11-04 16:29:45 -0800322 deps = append(deps, classpath...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800323 deps = append(deps, flags.processorPath...)
324
325 processor := "-proc:none"
Colin Cross5a116862020-04-22 11:44:34 -0700326 if len(flags.processors) > 0 {
327 processor = "-processor " + strings.Join(flags.processors, ",")
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800328 }
329
Colin Cross3b706fd2019-09-05 16:44:18 -0700330 intermediatesDir := "xref"
331 if idx >= 0 {
332 intermediatesDir += strconv.Itoa(idx)
333 }
334
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800335 ctx.Build(pctx,
336 android.BuildParams{
337 Rule: kytheExtract,
338 Description: "Xref Java extractor",
339 Output: xrefFile,
340 Inputs: srcFiles,
341 Implicits: deps,
342 Args: map[string]string{
343 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, "anno").String(),
344 "bootClasspath": bootClasspath,
Sasha Smundak09950a42019-11-04 16:29:45 -0800345 "classpath": classpath.FormJavaClassPath("-classpath"),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800346 "javacFlags": flags.javacFlags,
Colin Cross1e743852019-10-28 11:37:20 -0700347 "javaVersion": flags.javaVersion.String(),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800348 "outDir": android.PathForModuleOut(ctx, "javac", "classes.xref").String(),
349 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
350 "processor": processor,
351 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, "srcjars.xref").String(),
352 "srcJars": strings.Join(srcJars.Strings(), " "),
353 },
354 })
355}
356
Nan Zhanged19fc32017-10-19 13:06:22 -0700357func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700358 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700359
360 var deps android.Paths
361 deps = append(deps, srcJars...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700362
Colin Cross6cef4812019-10-17 14:23:50 -0700363 classpath := flags.classpath
364
Nan Zhanged19fc32017-10-19 13:06:22 -0700365 var bootClasspath string
Colin Crossbf3119e2019-10-28 14:25:10 -0700366 if flags.javaVersion.usesJavaModules() {
367 var systemModuleDeps android.Paths
368 bootClasspath, systemModuleDeps = flags.systemModules.FormTurbineSystemModulesPath(ctx.Device())
369 deps = append(deps, systemModuleDeps...)
Colin Cross6cef4812019-10-17 14:23:50 -0700370 classpath = append(flags.java9Classpath, classpath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700371 } else {
Colin Crossbf3119e2019-10-28 14:25:10 -0700372 deps = append(deps, flags.bootClasspath...)
373 if len(flags.bootClasspath) == 0 && ctx.Device() {
374 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
375 // ensure turbine does not fall back to the default bootclasspath.
376 bootClasspath = `--bootclasspath ""`
377 } else {
Colin Crossc2557d12019-10-31 15:22:57 -0700378 bootClasspath = flags.bootClasspath.FormTurbineClassPath("--bootclasspath ")
Colin Crossbf3119e2019-10-28 14:25:10 -0700379 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700380 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700381
Colin Cross6cef4812019-10-17 14:23:50 -0700382 deps = append(deps, classpath...)
Colin Crossbf3119e2019-10-28 14:25:10 -0700383 deps = append(deps, flags.processorPath...)
384
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700385 rule := turbine
386 args := map[string]string{
387 "javacFlags": flags.javacFlags,
388 "bootClasspath": bootClasspath,
389 "srcJars": strings.Join(srcJars.Strings(), " "),
390 "classpath": classpath.FormTurbineClassPath("--classpath "),
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700391 "javaVersion": flags.javaVersion.String(),
392 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400393 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_TURBINE") {
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700394 rule = turbineRE
395 args["implicits"] = strings.Join(deps.Strings(), ",")
396 }
Colin Crossae887032017-10-23 17:16:14 -0700397 ctx.Build(pctx, android.BuildParams{
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700398 Rule: rule,
Nan Zhanged19fc32017-10-19 13:06:22 -0700399 Description: "turbine",
400 Output: outputFile,
401 Inputs: srcFiles,
402 Implicits: deps,
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700403 Args: args,
Nan Zhanged19fc32017-10-19 13:06:22 -0700404 })
405}
406
Colin Cross070879e2017-10-11 11:21:07 -0700407// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700408// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
409// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700410//
411// This method may be used for different compilers, including javac and Error Prone. The rule
412// argument specifies which command line to use and desc sets the description of the rule that will
413// be printed at build time. The stem argument provides the file name of the output jar, and
414// suffix will be appended to various intermediate files and directories to avoid collisions when
415// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700416func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700417 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700418 flags javaBuilderFlags, deps android.Paths,
Colin Cross66548102018-06-19 22:47:35 -0700419 intermediatesDir, desc string) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700420
Colin Cross59149b62017-10-16 18:07:29 -0700421 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700422
Colin Cross6cef4812019-10-17 14:23:50 -0700423 classpath := flags.classpath
424
Colin Cross1369cdb2017-09-29 17:58:17 -0700425 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700426 if flags.javaVersion.usesJavaModules() {
Colin Crossb77043e2019-07-16 13:57:13 -0700427 var systemModuleDeps android.Paths
428 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
429 deps = append(deps, systemModuleDeps...)
Colin Cross6cef4812019-10-17 14:23:50 -0700430 classpath = append(flags.java9Classpath, classpath...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700431 } else {
432 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700433 if len(flags.bootClasspath) == 0 && ctx.Device() {
434 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
435 // ensure java does not fall back to the default bootclasspath.
436 bootClasspath = `-bootclasspath ""`
437 } else {
438 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
439 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700440 }
441
Colin Cross6cef4812019-10-17 14:23:50 -0700442 deps = append(deps, classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700443 deps = append(deps, flags.processorPath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700444
Colin Cross7788c122019-01-23 16:14:02 -0800445 processor := "-proc:none"
Colin Cross5a116862020-04-22 11:44:34 -0700446 if len(flags.processors) > 0 {
447 processor = "-processor " + strings.Join(flags.processors, ",")
Colin Crossbe9cdb82019-01-21 21:37:16 -0800448 }
449
Nan Zhang61eaedb2017-11-02 13:28:15 -0700450 srcJarDir := "srcjars"
451 outDir := "classes"
452 annoDir := "anno"
453 if shardIdx >= 0 {
454 shardDir := "shard" + strconv.Itoa(shardIdx)
455 srcJarDir = filepath.Join(shardDir, srcJarDir)
456 outDir = filepath.Join(shardDir, outDir)
457 annoDir = filepath.Join(shardDir, annoDir)
458 }
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400459 rule := javac
Ramy Medhat16f23a42020-09-03 01:29:49 -0400460 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAVAC") {
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400461 rule = javacRE
462 }
Colin Crossae887032017-10-23 17:16:14 -0700463 ctx.Build(pctx, android.BuildParams{
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400464 Rule: rule,
Colin Cross070879e2017-10-11 11:21:07 -0700465 Description: desc,
466 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700467 Inputs: srcFiles,
468 Implicits: deps,
469 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700470 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700471 "bootClasspath": bootClasspath,
Colin Cross6cef4812019-10-17 14:23:50 -0700472 "classpath": classpath.FormJavaClassPath("-classpath"),
Colin Cross6a77c982018-06-19 22:43:34 -0700473 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
Colin Crossbe9cdb82019-01-21 21:37:16 -0800474 "processor": processor,
Colin Cross8eadbf02017-10-24 17:46:00 -0700475 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700476 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
477 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
478 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross1e743852019-10-28 11:37:20 -0700479 "javaVersion": flags.javaVersion.String(),
Colin Crossc6bbef32017-08-14 14:16:06 -0700480 },
481 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700482}
483
Colin Crosse9a275b2017-10-16 17:09:48 -0700484func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
485 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700486
Kousik Kumar366afc52020-05-20 11:27:16 -0700487 rule := jar
Ramy Medhat16f23a42020-09-03 01:29:49 -0400488 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAR") {
Kousik Kumar366afc52020-05-20 11:27:16 -0700489 rule = jarRE
490 }
Colin Crossae887032017-10-23 17:16:14 -0700491 ctx.Build(pctx, android.BuildParams{
Kousik Kumar366afc52020-05-20 11:27:16 -0700492 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700493 Description: "jar",
494 Output: outputFile,
495 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700496 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -0800497 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700498 },
499 })
Colin Cross2fe66872015-03-30 17:20:39 -0700500}
501
Nan Zhanged19fc32017-10-19 13:06:22 -0700502func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
Colin Cross37f6d792018-07-12 12:28:41 -0700503 jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
504 dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700505
Colin Cross635acc92017-09-12 22:50:46 -0700506 var deps android.Paths
507
508 var jarArgs []string
509 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700510 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700511 deps = append(deps, manifest.Path())
512 }
513
Colin Cross37f6d792018-07-12 12:28:41 -0700514 for _, dir := range dirsToStrip {
515 jarArgs = append(jarArgs, "-stripDir ", dir)
516 }
517
518 for _, file := range filesToStrip {
519 jarArgs = append(jarArgs, "-stripFile ", file)
Nan Zhanged19fc32017-10-19 13:06:22 -0700520 }
521
Colin Cross7b60cdd2017-12-21 13:52:58 -0800522 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
523 // for downstream tools like desugar.
524 jarArgs = append(jarArgs, "-stripFile module-info.class")
525
Colin Cross37f6d792018-07-12 12:28:41 -0700526 if stripDirEntries {
Colin Cross635acc92017-09-12 22:50:46 -0700527 jarArgs = append(jarArgs, "-D")
528 }
529
Colin Crossae887032017-10-23 17:16:14 -0700530 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700531 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700532 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700533 Output: outputFile,
534 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700535 Implicits: deps,
536 Args: map[string]string{
537 "jarArgs": strings.Join(jarArgs, " "),
538 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700539 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700540}
541
Colin Crosse9a275b2017-10-16 17:09:48 -0700542func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
543 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700544 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700545 Rule: jarjar,
546 Description: "jarjar",
547 Output: outputFile,
548 Input: classesJar,
549 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700550 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700551 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700552 },
553 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700554}
Colin Cross6ade34f2017-09-15 13:00:47 -0700555
Colin Crossb549b772020-06-03 17:14:31 +0000556func CheckJarPackages(ctx android.ModuleContext, outputFile android.WritablePath,
557 classesJar android.Path, permittedPackages []string) {
Vladimir Marko0975ee02019-04-02 10:29:55 +0100558 ctx.Build(pctx, android.BuildParams{
559 Rule: packageCheck,
560 Description: "packageCheck",
561 Output: outputFile,
562 Input: classesJar,
563 Args: map[string]string{
564 "packages": strings.Join(permittedPackages, " "),
565 },
566 })
567}
568
Nan Zhang4c819fb2018-08-27 18:31:46 -0700569func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath,
570 inputFile android.Path) {
571 ctx.Build(pctx, android.BuildParams{
572 Rule: jetifier,
573 Description: "jetifier",
574 Output: outputFile,
575 Input: inputFile,
576 })
577}
578
Colin Cross094054a2018-10-17 15:10:48 -0700579func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) {
Colin Crosscf371cc2020-11-13 11:48:42 -0800580 android.WriteFileRule(ctx, outputFile, "Main-Class: "+mainClass+"\n")
Colin Cross094054a2018-10-17 15:10:48 -0700581}
582
Colin Cross43f08db2018-11-12 10:13:39 -0800583func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path) {
584 ctx.Build(pctx, android.BuildParams{
585 Rule: zipalign,
586 Description: "align",
587 Input: inputFile,
588 Output: outputFile,
589 })
590}
591
Colin Cross33961b52019-07-11 11:01:22 -0700592type classpath android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700593
Colin Crossc2557d12019-10-31 15:22:57 -0700594func (x *classpath) formJoinedClassPath(optName string, sep string) string {
Colin Cross81440082018-08-15 20:21:55 -0700595 if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") {
596 optName += " "
597 }
Colin Cross59149b62017-10-16 18:07:29 -0700598 if len(*x) > 0 {
Colin Crossc2557d12019-10-31 15:22:57 -0700599 return optName + strings.Join(x.Strings(), sep)
Colin Cross6ade34f2017-09-15 13:00:47 -0700600 } else {
601 return ""
602 }
603}
Colin Crossc2557d12019-10-31 15:22:57 -0700604func (x *classpath) FormJavaClassPath(optName string) string {
605 return x.formJoinedClassPath(optName, ":")
606}
Colin Cross6ade34f2017-09-15 13:00:47 -0700607
Colin Crossc2557d12019-10-31 15:22:57 -0700608func (x *classpath) FormTurbineClassPath(optName string) string {
609 return x.formJoinedClassPath(optName, " ")
610}
611
612// FormRepeatedClassPath returns a list of arguments with the given optName prefixed to each element of the classpath.
613func (x *classpath) FormRepeatedClassPath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700614 if x == nil || *x == nil {
615 return nil
616 }
617 flags := make([]string, len(*x))
618 for i, v := range *x {
Colin Crossafbb1732019-01-17 15:42:52 -0800619 flags[i] = optName + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700620 }
621
622 return flags
623}
624
Colin Cross6ade34f2017-09-15 13:00:47 -0700625// Convert a classpath to an android.Paths
626func (x *classpath) Paths() android.Paths {
627 return append(android.Paths(nil), (*x)...)
628}
629
630func (x *classpath) Strings() []string {
631 if x == nil {
632 return nil
633 }
634 ret := make([]string, len(*x))
635 for i, path := range *x {
636 ret[i] = path.String()
637 }
638 return ret
639}
Colin Crossb77043e2019-07-16 13:57:13 -0700640
641type systemModules struct {
642 dir android.Path
643 deps android.Paths
644}
645
Colin Crossbf3119e2019-10-28 14:25:10 -0700646// Returns a --system argument in the form javac expects with -source 1.9 and the list of files to
647// depend on. If forceEmpty is true, returns --system=none if the list is empty to ensure javac
648// does not fall back to the default system modules.
Colin Crossb77043e2019-07-16 13:57:13 -0700649func (x *systemModules) FormJavaSystemModulesPath(forceEmpty bool) (string, android.Paths) {
650 if x != nil {
651 return "--system=" + x.dir.String(), x.deps
652 } else if forceEmpty {
653 return "--system=none", nil
654 } else {
655 return "", nil
656 }
657}
Colin Crossbf3119e2019-10-28 14:25:10 -0700658
659// Returns a --system argument in the form turbine expects with -source 1.9 and the list of files to
660// depend on. If forceEmpty is true, returns --bootclasspath "" if the list is empty to ensure turbine
661// does not fall back to the default bootclasspath.
662func (x *systemModules) FormTurbineSystemModulesPath(forceEmpty bool) (string, android.Paths) {
663 if x != nil {
664 return "--system " + x.dir.String(), x.deps
665 } else if forceEmpty {
666 return `--bootclasspath ""`, nil
667 } else {
668 return "", nil
669 }
670}