blob: 74a05f281b8858c30235de4c67b52166a2a7d146 [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{
LaMont Jonesfeff3f32023-06-14 20:43:14 +000045 Command: `rm -rf "$outDir" "$annoDir" "$annoSrcJar.tmp" "$srcJarDir" "$out.tmp" && ` +
Vadim Spivak3c496f02023-06-08 06:14:59 +000046 `mkdir -p "$outDir" "$annoDir" "$srcJarDir" && ` +
Colin Cross436b7652018-03-15 16:24:10 -070047 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090048 `(if [ -s $srcJarDir/list ] || [ -s $out.rsp ] ; then ` +
Kousik Kumar366afc52020-05-20 11:27:16 -070049 `${config.SoongJavacWrapper} $javaTemplate${config.JavacCmd} ` +
Sasha Smundak26c6d9e2019-06-11 13:30:13 -070050 `${config.JavacHeapFlags} ${config.JavacVmFlags} ${config.CommonJdkFlags} ` +
Colin Crossbe9cdb82019-01-21 21:37:16 -080051 `$processorpath $processor $javacFlags $bootClasspath $classpath ` +
Colin Cross64162712017-08-08 13:17:59 -070052 `-source $javaVersion -target $javaVersion ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090053 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list ; fi ) && ` +
LaMont Jonesfeff3f32023-06-14 20:43:14 +000054 `$annoSrcJarTemplate${config.SoongZipCmd} -jar -o $annoSrcJar.tmp -C $annoDir -D $annoDir && ` +
55 `$zipTemplate${config.SoongZipCmd} -jar -o $out.tmp -C $outDir -D $outDir && ` +
56 `if ! cmp -s "$out.tmp" "$out"; then mv "$out.tmp" "$out"; fi && ` +
57 `if ! cmp -s "$annoSrcJar.tmp" "$annoSrcJar"; then mv "$annoSrcJar.tmp" "$annoSrcJar"; fi && ` +
Colin Cross49889c02023-07-26 16:16:47 -070058 `rm -rf "$srcJarDir" "$outDir"`,
Colin Cross8eadbf02017-10-24 17:46:00 -070059 CommandDeps: []string{
60 "${config.JavacCmd}",
61 "${config.SoongZipCmd}",
Colin Cross436b7652018-03-15 16:24:10 -070062 "${config.ZipSyncCmd}",
Colin Cross8eadbf02017-10-24 17:46:00 -070063 },
Colin Crossa4820652017-10-17 13:56:52 -070064 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
LaMont Jonesfeff3f32023-06-14 20:43:14 +000065 Restat: true,
Colin Crossa4820652017-10-17 13:56:52 -070066 Rspfile: "$out.rsp",
67 RspfileContent: "$in",
Kousik Kumar366afc52020-05-20 11:27:16 -070068 }, map[string]*remoteexec.REParams{
69 "$javaTemplate": &remoteexec.REParams{
70 Labels: map[string]string{"type": "compile", "lang": "java", "compiler": "javac"},
71 ExecStrategy: "${config.REJavacExecStrategy}",
72 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
73 },
74 "$zipTemplate": &remoteexec.REParams{
75 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
76 Inputs: []string{"${config.SoongZipCmd}", "$outDir"},
LaMont Jonesfeff3f32023-06-14 20:43:14 +000077 OutputFiles: []string{"$out.tmp"},
Kousik Kumar366afc52020-05-20 11:27:16 -070078 ExecStrategy: "${config.REJavacExecStrategy}",
79 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
80 },
Vadim Spivak3c496f02023-06-08 06:14:59 +000081 "$annoSrcJarTemplate": &remoteexec.REParams{
82 Labels: map[string]string{"type": "tool", "name": "soong_zip"},
83 Inputs: []string{"${config.SoongZipCmd}", "$annoDir"},
LaMont Jonesfeff3f32023-06-14 20:43:14 +000084 OutputFiles: []string{"$annoSrcJar.tmp"},
Vadim Spivak3c496f02023-06-08 06:14:59 +000085 ExecStrategy: "${config.REJavacExecStrategy}",
86 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
87 },
Ramy Medhat1dcc27e2020-04-21 21:36:23 -040088 }, []string{"javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
Vadim Spivak3c496f02023-06-08 06:14:59 +000089 "outDir", "annoDir", "annoSrcJar", "javaVersion"}, nil)
Colin Cross2fe66872015-03-30 17:20:39 -070090
Sasha Smundak2a4549e2018-11-05 16:49:08 -080091 _ = pctx.VariableFunc("kytheCorpus",
92 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() })
Sasha Smundak6c2d4f92020-01-09 17:34:23 -080093 _ = pctx.VariableFunc("kytheCuEncoding",
94 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuEncoding() })
Sasha Smundakb0addaf2021-02-16 10:39:40 -080095 _ = pctx.VariableFunc("kytheCuJavaSourceMax",
96 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuJavaSourceMax() })
Sasha Smundak65143642019-09-26 20:14:28 -070097 _ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json")
Sasha Smundak706d35f2022-10-23 15:23:55 -070098 // Run it with several --add-exports to allow the classes in the
99 // com.google.devtools.kythe.extractors.java.standalone package access the packages in the
100 // jdk.compiler compiler module. Long live Java modules.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800101 kytheExtract = pctx.AndroidStaticRule("kythe",
102 blueprint.RuleParams{
103 Command: `${config.ZipSyncCmd} -d $srcJarDir ` +
104 `-l $srcJarDir/list -f "*.java" $srcJars && ` +
105 `( [ ! -s $srcJarDir/list -a ! -s $out.rsp ] || ` +
106 `KYTHE_ROOT_DIRECTORY=. KYTHE_OUTPUT_FILE=$out ` +
107 `KYTHE_CORPUS=${kytheCorpus} ` +
Sasha Smundak65143642019-09-26 20:14:28 -0700108 `KYTHE_VNAMES=${kytheVnames} ` +
Sasha Smundak6c2d4f92020-01-09 17:34:23 -0800109 `KYTHE_KZIP_ENCODING=${kytheCuEncoding} ` +
Sasha Smundakb0addaf2021-02-16 10:39:40 -0800110 `KYTHE_JAVA_SOURCE_BATCH_SIZE=${kytheCuJavaSourceMax} ` +
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800111 `${config.SoongJavacWrapper} ${config.JavaCmd} ` +
Sasha Smundak706d35f2022-10-23 15:23:55 -0700112 // Avoid JDK9's warning about "Illegal reflective access by com.google.protobuf.Utf8$UnsafeProcessor ...
113 // to field java.nio.Buffer.address"
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800114 `--add-opens=java.base/java.nio=ALL-UNNAMED ` +
Sasha Smundak706d35f2022-10-23 15:23:55 -0700115 // Allow the classes in the com.google.devtools.kythe.extractors.java.standalone package
116 // access the packages in the jdk.compiler compiler module
117 `--add-opens=java.base/java.nio=ALL-UNNAMED ` +
118 `--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED ` +
119 `--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED ` +
120 `--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED ` +
121 `--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED ` +
122 `--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED ` +
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800123 `-jar ${config.JavaKytheExtractorJar} ` +
124 `${config.JavacHeapFlags} ${config.CommonJdkFlags} ` +
125 `$processorpath $processor $javacFlags $bootClasspath $classpath ` +
126 `-source $javaVersion -target $javaVersion ` +
127 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list)`,
128 CommandDeps: []string{
129 "${config.JavaCmd}",
130 "${config.JavaKytheExtractorJar}",
Sasha Smundak65143642019-09-26 20:14:28 -0700131 "${kytheVnames}",
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800132 "${config.ZipSyncCmd}",
133 },
134 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
135 Rspfile: "$out.rsp",
136 RspfileContent: "$in",
137 },
138 "javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
139 "outDir", "annoDir", "javaVersion")
140
Sasha Smundaka7856c02020-04-23 09:49:59 -0700141 extractMatchingApks = pctx.StaticRule(
142 "extractMatchingApks",
143 blueprint.RuleParams{
144 Command: `rm -rf "$out" && ` +
Colin Crossffbcd1d2021-11-12 12:19:42 -0800145 `${config.ExtractApksCmd} -o "${out}" -zip "${zip}" -allow-prereleased=${allow-prereleased} ` +
Pranav Gupta51645ff2023-03-20 16:19:53 -0700146 `-sdk-version=${sdk-version} -skip-sdk-check=${skip-sdk-check} -abis=${abis} ` +
Sasha Smundaka7856c02020-04-23 09:49:59 -0700147 `--screen-densities=${screen-densities} --stem=${stem} ` +
Jaewoong Jung11c1e0f2020-06-29 19:18:44 -0700148 `-apkcerts=${apkcerts} -partition=${partition} ` +
Sasha Smundaka7856c02020-04-23 09:49:59 -0700149 `${in}`,
150 CommandDeps: []string{"${config.ExtractApksCmd}"},
151 },
Pranav Gupta51645ff2023-03-20 16:19:53 -0700152 "abis", "allow-prereleased", "screen-densities", "sdk-version", "skip-sdk-check", "stem", "apkcerts", "partition", "zip")
Sasha Smundaka7856c02020-04-23 09:49:59 -0700153
Colin Cross77cdcfd2021-03-12 11:28:25 -0800154 turbine, turbineRE = pctx.RemoteStaticRules("turbine",
Nan Zhanged19fc32017-10-19 13:06:22 -0700155 blueprint.RuleParams{
Colin Crossf61766e2022-03-16 18:06:48 -0700156 Command: `$reTemplate${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.TurbineJar} $outputFlags ` +
Colin Cross411647e2022-03-16 14:28:36 -0700157 `--sources @$out.rsp --source_jars $srcJars ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700158 `--javacopts ${config.CommonJdkFlags} ` +
Colin Crossf61766e2022-03-16 18:06:48 -0700159 `$javacFlags -source $javaVersion -target $javaVersion -- $turbineFlags && ` +
160 `(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 -0700161 CommandDeps: []string{
162 "${config.TurbineJar}",
163 "${config.JavaCmd}",
Colin Cross8eadbf02017-10-24 17:46:00 -0700164 },
Nan Zhanged19fc32017-10-19 13:06:22 -0700165 Rspfile: "$out.rsp",
166 RspfileContent: "$in",
167 Restat: true,
168 },
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700169 &remoteexec.REParams{Labels: map[string]string{"type": "tool", "name": "turbine"},
Colin Cross411647e2022-03-16 14:28:36 -0700170 ExecStrategy: "${config.RETurbineExecStrategy}",
171 Inputs: []string{"${config.TurbineJar}", "${out}.rsp", "$implicits"},
172 RSPFiles: []string{"${out}.rsp"},
Colin Crossf61766e2022-03-16 18:06:48 -0700173 OutputFiles: []string{"$rbeOutputs"},
Colin Cross411647e2022-03-16 14:28:36 -0700174 ToolchainInputs: []string{"${config.JavaCmd}"},
175 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
Colin Crossf61766e2022-03-16 18:06:48 -0700176 },
177 []string{"javacFlags", "turbineFlags", "outputFlags", "javaVersion", "outputs", "rbeOutputs", "srcJars"}, []string{"implicits"})
Nan Zhanged19fc32017-10-19 13:06:22 -0700178
Colin Cross77cdcfd2021-03-12 11:28:25 -0800179 jar, jarRE = pctx.RemoteStaticRules("jar",
Colin Cross2fe66872015-03-30 17:20:39 -0700180 blueprint.RuleParams{
Kousik Kumar366afc52020-05-20 11:27:16 -0700181 Command: `$reTemplate${config.SoongZipCmd} -jar -o $out @$out.rsp`,
Nan Zhang674dd932018-01-26 18:30:36 -0800182 CommandDeps: []string{"${config.SoongZipCmd}"},
183 Rspfile: "$out.rsp",
184 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -0700185 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700186 &remoteexec.REParams{
187 ExecStrategy: "${config.REJarExecStrategy}",
188 Inputs: []string{"${config.SoongZipCmd}", "${out}.rsp"},
Colin Crossa4eafdd2021-03-24 14:09:28 -0700189 RSPFiles: []string{"${out}.rsp"},
Kousik Kumar366afc52020-05-20 11:27:16 -0700190 OutputFiles: []string{"$out"},
191 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
192 }, []string{"jarArgs"}, nil)
Colin Cross0a6e0072017-08-30 14:24:55 -0700193
Colin Cross77cdcfd2021-03-12 11:28:25 -0800194 zip, zipRE = pctx.RemoteStaticRules("zip",
Colin Crossa4f08812018-10-02 22:03:40 -0700195 blueprint.RuleParams{
196 Command: `${config.SoongZipCmd} -o $out @$out.rsp`,
197 CommandDeps: []string{"${config.SoongZipCmd}"},
198 Rspfile: "$out.rsp",
199 RspfileContent: "$jarArgs",
200 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700201 &remoteexec.REParams{
202 ExecStrategy: "${config.REZipExecStrategy}",
203 Inputs: []string{"${config.SoongZipCmd}", "${out}.rsp", "$implicits"},
Colin Crossa4eafdd2021-03-24 14:09:28 -0700204 RSPFiles: []string{"${out}.rsp"},
Kousik Kumar366afc52020-05-20 11:27:16 -0700205 OutputFiles: []string{"$out"},
206 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
207 }, []string{"jarArgs"}, []string{"implicits"})
Colin Crossa4f08812018-10-02 22:03:40 -0700208
Colin Cross0a6e0072017-08-30 14:24:55 -0700209 combineJar = pctx.AndroidStaticRule("combineJar",
210 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800211 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700212 CommandDeps: []string{"${config.MergeZipsCmd}"},
213 },
Colin Cross635acc92017-09-12 22:50:46 -0700214 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700215
Colin Cross9d45bb72016-08-29 16:14:13 -0700216 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700217 blueprint.RuleParams{
Colin Crossd97cf632021-02-08 10:14:04 -0800218 Command: "" +
219 // Jarjar doesn't exit with an error when the rules file contains a syntax error,
220 // leading to stale or missing files later in the build. Remove the output file
221 // before running jarjar.
222 "rm -f ${out} && " +
223 "${config.JavaCmd} ${config.JavaVmFlags}" +
Artur Satayev762d9f32020-04-15 12:50:04 +0100224 // b/146418363 Enable Android specific jarjar transformer to drop compat annotations
225 // for newly repackaged classes. Dropping @UnsupportedAppUsage on repackaged classes
226 // avoids adding new hiddenapis after jarjar'ing.
227 " -DremoveAndroidCompatAnnotations=true" +
Colin Crossd97cf632021-02-08 10:14:04 -0800228 " -jar ${config.JarjarCmd} process $rulesFile $in $out && " +
229 // Turn a missing output file into a ninja error
230 `[ -e ${out} ] || (echo "Missing output file"; exit 1)`,
Colin Cross64162712017-08-08 13:17:59 -0700231 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700232 },
233 "rulesFile")
Nan Zhang4c819fb2018-08-27 18:31:46 -0700234
Vladimir Marko0975ee02019-04-02 10:29:55 +0100235 packageCheck = pctx.AndroidStaticRule("packageCheck",
236 blueprint.RuleParams{
237 Command: "rm -f $out && " +
238 "${config.PackageCheckCmd} $in $packages && " +
Colin Crossb549b772020-06-03 17:14:31 +0000239 "touch $out",
Vladimir Marko0975ee02019-04-02 10:29:55 +0100240 CommandDeps: []string{"${config.PackageCheckCmd}"},
241 },
242 "packages")
243
Nan Zhang4c819fb2018-08-27 18:31:46 -0700244 jetifier = pctx.AndroidStaticRule("jetifier",
245 blueprint.RuleParams{
Colin Crossdb9f1af2022-12-13 15:57:01 -0800246 Command: "${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JetifierJar} -l error -o $out -i $in -t epoch",
Nan Zhang4c819fb2018-08-27 18:31:46 -0700247 CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"},
248 },
249 )
Colin Cross43f08db2018-11-12 10:13:39 -0800250
251 zipalign = pctx.AndroidStaticRule("zipalign",
252 blueprint.RuleParams{
Colin Crosse4246ab2019-02-05 21:55:21 -0800253 Command: "if ! ${config.ZipAlign} -c -p 4 $in > /dev/null; then " +
254 "${config.ZipAlign} -f -p 4 $in $out; " +
Colin Cross43f08db2018-11-12 10:13:39 -0800255 "else " +
256 "cp -f $in $out; " +
257 "fi",
258 CommandDeps: []string{"${config.ZipAlign}"},
259 },
260 )
Cole Faust2f1da162023-04-17 15:06:56 -0700261
Colin Crossf06d8dc2023-07-18 22:11:07 -0700262 convertImplementationJarToHeaderJarRule = pctx.AndroidStaticRule("convertImplementationJarToHeaderJar",
263 blueprint.RuleParams{
264 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} -x 'META-INF/services/**/*'`,
265 CommandDeps: []string{"${config.Zip2ZipCmd}"},
266 })
Colin Cross312634e2023-11-21 15:13:56 -0800267
268 writeCombinedProguardFlagsFileRule = pctx.AndroidStaticRule("writeCombinedProguardFlagsFileRule",
269 blueprint.RuleParams{
270 Command: `rm -f $out && ` +
271 `for f in $in; do ` +
272 ` echo && ` +
273 ` echo "# including $$f" && ` +
274 ` cat $$f; ` +
275 `done > $out`,
276 })
Jihoon Kang6592e872023-12-19 01:13:16 +0000277
278 gatherReleasedFlaggedApisRule = pctx.AndroidStaticRule("gatherReleasedFlaggedApisRule",
279 blueprint.RuleParams{
Yu Liu748ade22024-02-08 19:11:39 +0000280 Command: `${aconfig} dump-cache --dedup --format='{fully_qualified_name}={state:bool}' ` +
Jihoon Kang6592e872023-12-19 01:13:16 +0000281 `--out ${out} ` +
282 `${flags_path} ` +
283 `${filter_args} `,
284 CommandDeps: []string{"${aconfig}"},
285 Description: "aconfig_bool",
286 }, "flags_path", "filter_args")
287
288 generateMetalavaRevertAnnotationsRule = pctx.AndroidStaticRule("generateMetalavaRevertAnnotationsRule",
289 blueprint.RuleParams{
Jihoon Kang150d87f2024-01-09 19:21:27 +0000290 Command: `${keep-flagged-apis} ${in} > ${out}`,
291 CommandDeps: []string{"${keep-flagged-apis}"},
Jihoon Kang6592e872023-12-19 01:13:16 +0000292 })
Colin Cross2fe66872015-03-30 17:20:39 -0700293)
294
295func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700296 pctx.Import("android/soong/android")
Colin Cross64162712017-08-08 13:17:59 -0700297 pctx.Import("android/soong/java/config")
Jihoon Kang6592e872023-12-19 01:13:16 +0000298
299 pctx.HostBinToolVariable("aconfig", "aconfig")
300 pctx.HostBinToolVariable("keep-flagged-apis", "keep-flagged-apis")
Colin Cross2fe66872015-03-30 17:20:39 -0700301}
302
303type javaBuilderFlags struct {
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700304 javacFlags string
305
306 // bootClasspath is the list of jars that form the boot classpath (generally the java.* and
307 // android.* classes) for tools that still use it. javac targeting 1.9 or higher uses
308 // systemModules and java9Classpath instead.
309 bootClasspath classpath
310
311 // classpath is the list of jars that form the classpath for javac and kotlinc rules. It
312 // contains header jars for all static and non-static dependencies.
313 classpath classpath
314
315 // dexClasspath is the list of jars that form the classpath for d8 and r8 rules. It contains
316 // header jars for all non-static dependencies. Static dependencies have already been
317 // combined into the program jar.
318 dexClasspath classpath
319
320 // java9Classpath is the list of jars that will be added to the classpath when targeting
321 // 1.9 or higher. It generally contains the android.* classes, while the java.* classes
322 // are provided by systemModules.
Colin Cross6cef4812019-10-17 14:23:50 -0700323 java9Classpath classpath
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700324
325 processorPath classpath
326 processors []string
327 systemModules *systemModules
328 aidlFlags string
329 aidlDeps android.Paths
330 javaVersion javaVersion
Colin Cross6af17aa2017-09-20 12:59:05 -0700331
Andreas Gampef3e5b552018-01-22 21:27:21 -0800332 errorProneExtraJavacFlags string
Colin Cross66548102018-06-19 22:47:35 -0700333 errorProneProcessorPath classpath
Andreas Gampef3e5b552018-01-22 21:27:21 -0800334
Colin Cross93e85952017-08-15 13:34:18 -0700335 kotlincFlags string
336 kotlincClasspath classpath
Colin Crossa1ff7c62021-09-17 14:11:52 -0700337 kotlincDeps android.Paths
Colin Cross93e85952017-08-15 13:34:18 -0700338
Colin Cross19878da2019-03-28 14:45:07 -0700339 proto android.ProtoFlags
Colin Cross2fe66872015-03-30 17:20:39 -0700340}
341
Joe Onorato175073c2023-06-01 14:42:59 -0700342func DefaultJavaBuilderFlags() javaBuilderFlags {
343 return javaBuilderFlags{
344 javaVersion: JAVA_VERSION_8,
345 }
346}
347
Nan Zhang61eaedb2017-11-02 13:28:15 -0700348func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
Vadim Spivak3c496f02023-06-08 06:14:59 +0000349 srcFiles, srcJars android.Paths, annoSrcJar android.WritablePath, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700350
Nan Zhang61eaedb2017-11-02 13:28:15 -0700351 // Compile java sources into .class files
352 desc := "javac"
353 if shardIdx >= 0 {
354 desc += strconv.Itoa(shardIdx)
355 }
356
Vadim Spivak3c496f02023-06-08 06:14:59 +0000357 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, annoSrcJar, flags, deps, "javac", desc)
Colin Cross2fe66872015-03-30 17:20:39 -0700358}
359
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800360// Emits the rule to generate Xref input file (.kzip file) for the given set of source files and source jars
361// to compile with given set of builder flags, etc.
Colin Cross3b706fd2019-09-05 16:44:18 -0700362func emitXrefRule(ctx android.ModuleContext, xrefFile android.WritablePath, idx int,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800363 srcFiles, srcJars android.Paths,
Colin Cross3b706fd2019-09-05 16:44:18 -0700364 flags javaBuilderFlags, deps android.Paths) {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800365
366 deps = append(deps, srcJars...)
Sasha Smundak09950a42019-11-04 16:29:45 -0800367 classpath := flags.classpath
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800368
369 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700370 if flags.javaVersion.usesJavaModules() {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800371 var systemModuleDeps android.Paths
372 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
373 deps = append(deps, systemModuleDeps...)
Sasha Smundak09950a42019-11-04 16:29:45 -0800374 classpath = append(flags.java9Classpath, classpath...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800375 } else {
376 deps = append(deps, flags.bootClasspath...)
377 if len(flags.bootClasspath) == 0 && ctx.Device() {
378 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
379 // ensure java does not fall back to the default bootclasspath.
380 bootClasspath = `-bootclasspath ""`
381 } else {
382 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
383 }
384 }
385
Sasha Smundak09950a42019-11-04 16:29:45 -0800386 deps = append(deps, classpath...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800387 deps = append(deps, flags.processorPath...)
388
389 processor := "-proc:none"
Colin Cross5a116862020-04-22 11:44:34 -0700390 if len(flags.processors) > 0 {
391 processor = "-processor " + strings.Join(flags.processors, ",")
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800392 }
393
Colin Cross3b706fd2019-09-05 16:44:18 -0700394 intermediatesDir := "xref"
395 if idx >= 0 {
396 intermediatesDir += strconv.Itoa(idx)
397 }
398
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800399 ctx.Build(pctx,
400 android.BuildParams{
401 Rule: kytheExtract,
402 Description: "Xref Java extractor",
403 Output: xrefFile,
404 Inputs: srcFiles,
405 Implicits: deps,
406 Args: map[string]string{
407 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, "anno").String(),
408 "bootClasspath": bootClasspath,
Sasha Smundak09950a42019-11-04 16:29:45 -0800409 "classpath": classpath.FormJavaClassPath("-classpath"),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800410 "javacFlags": flags.javacFlags,
Colin Cross1e743852019-10-28 11:37:20 -0700411 "javaVersion": flags.javaVersion.String(),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800412 "outDir": android.PathForModuleOut(ctx, "javac", "classes.xref").String(),
413 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
414 "processor": processor,
415 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, "srcjars.xref").String(),
416 "srcJars": strings.Join(srcJars.Strings(), " "),
417 },
418 })
419}
420
Colin Crossf61766e2022-03-16 18:06:48 -0700421func turbineFlags(ctx android.ModuleContext, flags javaBuilderFlags) (string, android.Paths) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700422 var deps android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700423
Colin Cross6cef4812019-10-17 14:23:50 -0700424 classpath := flags.classpath
425
Nan Zhanged19fc32017-10-19 13:06:22 -0700426 var bootClasspath string
Colin Crossbf3119e2019-10-28 14:25:10 -0700427 if flags.javaVersion.usesJavaModules() {
428 var systemModuleDeps android.Paths
429 bootClasspath, systemModuleDeps = flags.systemModules.FormTurbineSystemModulesPath(ctx.Device())
430 deps = append(deps, systemModuleDeps...)
Colin Cross6cef4812019-10-17 14:23:50 -0700431 classpath = append(flags.java9Classpath, classpath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700432 } else {
Colin Crossbf3119e2019-10-28 14:25:10 -0700433 deps = append(deps, flags.bootClasspath...)
434 if len(flags.bootClasspath) == 0 && ctx.Device() {
435 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
436 // ensure turbine does not fall back to the default bootclasspath.
437 bootClasspath = `--bootclasspath ""`
438 } else {
Colin Crossc2557d12019-10-31 15:22:57 -0700439 bootClasspath = flags.bootClasspath.FormTurbineClassPath("--bootclasspath ")
Colin Crossbf3119e2019-10-28 14:25:10 -0700440 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700441 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700442
Colin Cross6cef4812019-10-17 14:23:50 -0700443 deps = append(deps, classpath...)
Colin Crossf61766e2022-03-16 18:06:48 -0700444 turbineFlags := bootClasspath + " " + classpath.FormTurbineClassPath("--classpath ")
445
446 return turbineFlags, deps
447}
448
449func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
450 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
451
452 turbineFlags, deps := turbineFlags(ctx, flags)
453
454 deps = append(deps, srcJars...)
Colin Crossbf3119e2019-10-28 14:25:10 -0700455
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700456 rule := turbine
457 args := map[string]string{
Colin Crossf61766e2022-03-16 18:06:48 -0700458 "javacFlags": flags.javacFlags,
459 "srcJars": strings.Join(srcJars.Strings(), " "),
460 "javaVersion": flags.javaVersion.String(),
461 "turbineFlags": turbineFlags,
462 "outputFlags": "--output " + outputFile.String() + ".tmp",
463 "outputs": outputFile.String(),
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700464 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400465 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_TURBINE") {
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700466 rule = turbineRE
467 args["implicits"] = strings.Join(deps.Strings(), ",")
Colin Crossf61766e2022-03-16 18:06:48 -0700468 args["rbeOutputs"] = outputFile.String() + ".tmp"
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700469 }
Colin Crossae887032017-10-23 17:16:14 -0700470 ctx.Build(pctx, android.BuildParams{
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700471 Rule: rule,
Nan Zhanged19fc32017-10-19 13:06:22 -0700472 Description: "turbine",
473 Output: outputFile,
474 Inputs: srcFiles,
475 Implicits: deps,
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700476 Args: args,
Nan Zhanged19fc32017-10-19 13:06:22 -0700477 })
478}
479
Colin Crossf61766e2022-03-16 18:06:48 -0700480// TurbineApt produces a rule to run annotation processors using turbine.
Isaac Chioua23d9942022-04-06 06:14:38 +0000481func TurbineApt(ctx android.ModuleContext, outputSrcJar, outputResJar android.WritablePath,
Colin Crossf61766e2022-03-16 18:06:48 -0700482 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
483
484 turbineFlags, deps := turbineFlags(ctx, flags)
485
486 deps = append(deps, srcJars...)
487
488 deps = append(deps, flags.processorPath...)
489 turbineFlags += " " + flags.processorPath.FormTurbineClassPath("--processorpath ")
490 turbineFlags += " --processors " + strings.Join(flags.processors, " ")
491
Isaac Chioua23d9942022-04-06 06:14:38 +0000492 outputs := android.WritablePaths{outputSrcJar, outputResJar}
493 outputFlags := "--gensrc_output " + outputSrcJar.String() + ".tmp " +
Colin Crossf61766e2022-03-16 18:06:48 -0700494 "--resource_output " + outputResJar.String() + ".tmp"
495
496 rule := turbine
497 args := map[string]string{
498 "javacFlags": flags.javacFlags,
499 "srcJars": strings.Join(srcJars.Strings(), " "),
500 "javaVersion": flags.javaVersion.String(),
501 "turbineFlags": turbineFlags,
502 "outputFlags": outputFlags,
503 "outputs": strings.Join(outputs.Strings(), " "),
504 }
505 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_TURBINE") {
506 rule = turbineRE
507 args["implicits"] = strings.Join(deps.Strings(), ",")
Isaac Chioua23d9942022-04-06 06:14:38 +0000508 args["rbeOutputs"] = outputSrcJar.String() + ".tmp," + outputResJar.String() + ".tmp"
Colin Crossf61766e2022-03-16 18:06:48 -0700509 }
510 ctx.Build(pctx, android.BuildParams{
511 Rule: rule,
512 Description: "turbine apt",
513 Output: outputs[0],
514 ImplicitOutputs: outputs[1:],
515 Inputs: srcFiles,
516 Implicits: deps,
517 Args: args,
518 })
519}
520
Colin Cross070879e2017-10-11 11:21:07 -0700521// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700522// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
523// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700524//
525// This method may be used for different compilers, including javac and Error Prone. The rule
526// argument specifies which command line to use and desc sets the description of the rule that will
527// be printed at build time. The stem argument provides the file name of the output jar, and
528// suffix will be appended to various intermediate files and directories to avoid collisions when
529// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700530func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Vadim Spivak3c496f02023-06-08 06:14:59 +0000531 shardIdx int, srcFiles, srcJars android.Paths, annoSrcJar android.WritablePath,
Colin Crosse9a275b2017-10-16 17:09:48 -0700532 flags javaBuilderFlags, deps android.Paths,
Colin Cross66548102018-06-19 22:47:35 -0700533 intermediatesDir, desc string) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700534
Colin Cross59149b62017-10-16 18:07:29 -0700535 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700536
Colin Cross6cef4812019-10-17 14:23:50 -0700537 classpath := flags.classpath
538
Colin Cross1369cdb2017-09-29 17:58:17 -0700539 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700540 if flags.javaVersion.usesJavaModules() {
Colin Crossb77043e2019-07-16 13:57:13 -0700541 var systemModuleDeps android.Paths
542 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
543 deps = append(deps, systemModuleDeps...)
Colin Cross6cef4812019-10-17 14:23:50 -0700544 classpath = append(flags.java9Classpath, classpath...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700545 } else {
546 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700547 if len(flags.bootClasspath) == 0 && ctx.Device() {
548 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
549 // ensure java does not fall back to the default bootclasspath.
550 bootClasspath = `-bootclasspath ""`
551 } else {
552 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
553 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700554 }
555
Colin Cross6cef4812019-10-17 14:23:50 -0700556 deps = append(deps, classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700557 deps = append(deps, flags.processorPath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700558
Colin Cross7788c122019-01-23 16:14:02 -0800559 processor := "-proc:none"
Colin Cross5a116862020-04-22 11:44:34 -0700560 if len(flags.processors) > 0 {
561 processor = "-processor " + strings.Join(flags.processors, ",")
Colin Crossbe9cdb82019-01-21 21:37:16 -0800562 }
563
Nan Zhang61eaedb2017-11-02 13:28:15 -0700564 srcJarDir := "srcjars"
565 outDir := "classes"
566 annoDir := "anno"
567 if shardIdx >= 0 {
568 shardDir := "shard" + strconv.Itoa(shardIdx)
569 srcJarDir = filepath.Join(shardDir, srcJarDir)
570 outDir = filepath.Join(shardDir, outDir)
571 annoDir = filepath.Join(shardDir, annoDir)
572 }
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400573 rule := javac
Ramy Medhat16f23a42020-09-03 01:29:49 -0400574 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAVAC") {
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400575 rule = javacRE
576 }
Colin Crossae887032017-10-23 17:16:14 -0700577 ctx.Build(pctx, android.BuildParams{
Vadim Spivak3c496f02023-06-08 06:14:59 +0000578 Rule: rule,
579 Description: desc,
580 Output: outputFile,
581 ImplicitOutput: annoSrcJar,
582 Inputs: srcFiles,
583 Implicits: deps,
Colin Crossc6bbef32017-08-14 14:16:06 -0700584 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700585 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700586 "bootClasspath": bootClasspath,
Colin Cross6cef4812019-10-17 14:23:50 -0700587 "classpath": classpath.FormJavaClassPath("-classpath"),
Colin Cross6a77c982018-06-19 22:43:34 -0700588 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
Colin Crossbe9cdb82019-01-21 21:37:16 -0800589 "processor": processor,
Colin Cross8eadbf02017-10-24 17:46:00 -0700590 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700591 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
592 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
593 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Vadim Spivak3c496f02023-06-08 06:14:59 +0000594 "annoSrcJar": annoSrcJar.String(),
Colin Cross1e743852019-10-28 11:37:20 -0700595 "javaVersion": flags.javaVersion.String(),
Colin Crossc6bbef32017-08-14 14:16:06 -0700596 },
597 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700598}
599
Colin Crosse9a275b2017-10-16 17:09:48 -0700600func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
601 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700602
Kousik Kumar366afc52020-05-20 11:27:16 -0700603 rule := jar
Ramy Medhat16f23a42020-09-03 01:29:49 -0400604 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAR") {
Kousik Kumar366afc52020-05-20 11:27:16 -0700605 rule = jarRE
606 }
Colin Crossae887032017-10-23 17:16:14 -0700607 ctx.Build(pctx, android.BuildParams{
Kousik Kumar366afc52020-05-20 11:27:16 -0700608 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700609 Description: "jar",
610 Output: outputFile,
611 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700612 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -0800613 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700614 },
615 })
Colin Cross2fe66872015-03-30 17:20:39 -0700616}
617
Nan Zhanged19fc32017-10-19 13:06:22 -0700618func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
Colin Cross37f6d792018-07-12 12:28:41 -0700619 jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
620 dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700621
Colin Cross635acc92017-09-12 22:50:46 -0700622 var deps android.Paths
623
624 var jarArgs []string
625 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700626 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700627 deps = append(deps, manifest.Path())
628 }
629
Colin Cross37f6d792018-07-12 12:28:41 -0700630 for _, dir := range dirsToStrip {
631 jarArgs = append(jarArgs, "-stripDir ", dir)
632 }
633
634 for _, file := range filesToStrip {
635 jarArgs = append(jarArgs, "-stripFile ", file)
Nan Zhanged19fc32017-10-19 13:06:22 -0700636 }
637
Colin Cross7b60cdd2017-12-21 13:52:58 -0800638 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
639 // for downstream tools like desugar.
640 jarArgs = append(jarArgs, "-stripFile module-info.class")
641
Colin Cross37f6d792018-07-12 12:28:41 -0700642 if stripDirEntries {
Colin Cross635acc92017-09-12 22:50:46 -0700643 jarArgs = append(jarArgs, "-D")
644 }
645
Colin Crossae887032017-10-23 17:16:14 -0700646 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700647 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700648 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700649 Output: outputFile,
650 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700651 Implicits: deps,
652 Args: map[string]string{
653 "jarArgs": strings.Join(jarArgs, " "),
654 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700655 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700656}
657
Colin Crossf06d8dc2023-07-18 22:11:07 -0700658func convertImplementationJarToHeaderJar(ctx android.ModuleContext, implementationJarFile android.Path,
659 headerJarFile android.WritablePath) {
660 ctx.Build(pctx, android.BuildParams{
661 Rule: convertImplementationJarToHeaderJarRule,
662 Input: implementationJarFile,
663 Output: headerJarFile,
664 })
665}
666
Colin Crosse9a275b2017-10-16 17:09:48 -0700667func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
668 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700669 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700670 Rule: jarjar,
671 Description: "jarjar",
672 Output: outputFile,
673 Input: classesJar,
674 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700675 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700676 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700677 },
678 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700679}
Colin Cross6ade34f2017-09-15 13:00:47 -0700680
Colin Crossb549b772020-06-03 17:14:31 +0000681func CheckJarPackages(ctx android.ModuleContext, outputFile android.WritablePath,
682 classesJar android.Path, permittedPackages []string) {
Vladimir Marko0975ee02019-04-02 10:29:55 +0100683 ctx.Build(pctx, android.BuildParams{
684 Rule: packageCheck,
685 Description: "packageCheck",
686 Output: outputFile,
687 Input: classesJar,
688 Args: map[string]string{
689 "packages": strings.Join(permittedPackages, " "),
690 },
691 })
692}
693
Nan Zhang4c819fb2018-08-27 18:31:46 -0700694func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath,
695 inputFile android.Path) {
696 ctx.Build(pctx, android.BuildParams{
697 Rule: jetifier,
698 Description: "jetifier",
699 Output: outputFile,
700 Input: inputFile,
701 })
702}
703
Colin Cross094054a2018-10-17 15:10:48 -0700704func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) {
Colin Crosscf371cc2020-11-13 11:48:42 -0800705 android.WriteFileRule(ctx, outputFile, "Main-Class: "+mainClass+"\n")
Colin Cross094054a2018-10-17 15:10:48 -0700706}
707
Cole Faust61585282023-07-14 16:23:39 -0700708func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path, validations android.Paths) {
Colin Cross43f08db2018-11-12 10:13:39 -0800709 ctx.Build(pctx, android.BuildParams{
710 Rule: zipalign,
711 Description: "align",
712 Input: inputFile,
713 Output: outputFile,
Cole Faust61585282023-07-14 16:23:39 -0700714 Validations: validations,
Colin Cross43f08db2018-11-12 10:13:39 -0800715 })
716}
717
Colin Cross312634e2023-11-21 15:13:56 -0800718func writeCombinedProguardFlagsFile(ctx android.ModuleContext, outputFile android.WritablePath, files android.Paths) {
719 ctx.Build(pctx, android.BuildParams{
720 Rule: writeCombinedProguardFlagsFileRule,
721 Description: "write combined proguard flags file",
722 Inputs: files,
723 Output: outputFile,
724 })
725}
726
Colin Cross33961b52019-07-11 11:01:22 -0700727type classpath android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700728
Colin Crossc2557d12019-10-31 15:22:57 -0700729func (x *classpath) formJoinedClassPath(optName string, sep string) string {
Colin Cross81440082018-08-15 20:21:55 -0700730 if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") {
731 optName += " "
732 }
Colin Cross59149b62017-10-16 18:07:29 -0700733 if len(*x) > 0 {
Colin Crossc2557d12019-10-31 15:22:57 -0700734 return optName + strings.Join(x.Strings(), sep)
Colin Cross6ade34f2017-09-15 13:00:47 -0700735 } else {
736 return ""
737 }
738}
Colin Crossc2557d12019-10-31 15:22:57 -0700739func (x *classpath) FormJavaClassPath(optName string) string {
740 return x.formJoinedClassPath(optName, ":")
741}
Colin Cross6ade34f2017-09-15 13:00:47 -0700742
Colin Crossc2557d12019-10-31 15:22:57 -0700743func (x *classpath) FormTurbineClassPath(optName string) string {
744 return x.formJoinedClassPath(optName, " ")
745}
746
747// FormRepeatedClassPath returns a list of arguments with the given optName prefixed to each element of the classpath.
748func (x *classpath) FormRepeatedClassPath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700749 if x == nil || *x == nil {
750 return nil
751 }
752 flags := make([]string, len(*x))
753 for i, v := range *x {
Colin Crossafbb1732019-01-17 15:42:52 -0800754 flags[i] = optName + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700755 }
756
757 return flags
758}
759
Colin Cross6ade34f2017-09-15 13:00:47 -0700760// Convert a classpath to an android.Paths
761func (x *classpath) Paths() android.Paths {
762 return append(android.Paths(nil), (*x)...)
763}
764
765func (x *classpath) Strings() []string {
766 if x == nil {
767 return nil
768 }
769 ret := make([]string, len(*x))
770 for i, path := range *x {
771 ret[i] = path.String()
772 }
773 return ret
774}
Colin Crossb77043e2019-07-16 13:57:13 -0700775
776type systemModules struct {
777 dir android.Path
778 deps android.Paths
779}
780
Colin Crossbf3119e2019-10-28 14:25:10 -0700781// Returns a --system argument in the form javac expects with -source 1.9 and the list of files to
782// depend on. If forceEmpty is true, returns --system=none if the list is empty to ensure javac
783// does not fall back to the default system modules.
Colin Crossb77043e2019-07-16 13:57:13 -0700784func (x *systemModules) FormJavaSystemModulesPath(forceEmpty bool) (string, android.Paths) {
785 if x != nil {
786 return "--system=" + x.dir.String(), x.deps
787 } else if forceEmpty {
788 return "--system=none", nil
789 } else {
790 return "", nil
791 }
792}
Colin Crossbf3119e2019-10-28 14:25:10 -0700793
794// Returns a --system argument in the form turbine expects with -source 1.9 and the list of files to
795// depend on. If forceEmpty is true, returns --bootclasspath "" if the list is empty to ensure turbine
796// does not fall back to the default bootclasspath.
797func (x *systemModules) FormTurbineSystemModulesPath(forceEmpty bool) (string, android.Paths) {
798 if x != nil {
799 return "--system " + x.dir.String(), x.deps
800 } else if forceEmpty {
801 return `--bootclasspath ""`, nil
802 } else {
Colin Crossf61766e2022-03-16 18:06:48 -0700803 return "--system ${config.JavaHome}", nil
Colin Crossbf3119e2019-10-28 14:25:10 -0700804 }
805}