blob: b07a622e456a90705b016192cd1f1ec3b902d7fe [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 Crossc52d5232024-01-10 16:43:33 -0800215 combineJarRsp = pctx.AndroidStaticRule("combineJarRsp",
216 blueprint.RuleParams{
217 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out @$out.rsp`,
218 CommandDeps: []string{"${config.MergeZipsCmd}"},
219 Rspfile: "$out.rsp",
220 RspfileContent: "$in",
221 },
222 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700223
Colin Cross9d45bb72016-08-29 16:14:13 -0700224 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700225 blueprint.RuleParams{
Colin Crossd97cf632021-02-08 10:14:04 -0800226 Command: "" +
227 // Jarjar doesn't exit with an error when the rules file contains a syntax error,
228 // leading to stale or missing files later in the build. Remove the output file
229 // before running jarjar.
230 "rm -f ${out} && " +
231 "${config.JavaCmd} ${config.JavaVmFlags}" +
Artur Satayev762d9f32020-04-15 12:50:04 +0100232 // b/146418363 Enable Android specific jarjar transformer to drop compat annotations
233 // for newly repackaged classes. Dropping @UnsupportedAppUsage on repackaged classes
234 // avoids adding new hiddenapis after jarjar'ing.
235 " -DremoveAndroidCompatAnnotations=true" +
Colin Crossd97cf632021-02-08 10:14:04 -0800236 " -jar ${config.JarjarCmd} process $rulesFile $in $out && " +
237 // Turn a missing output file into a ninja error
238 `[ -e ${out} ] || (echo "Missing output file"; exit 1)`,
Colin Cross64162712017-08-08 13:17:59 -0700239 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700240 },
241 "rulesFile")
Nan Zhang4c819fb2018-08-27 18:31:46 -0700242
Vladimir Marko0975ee02019-04-02 10:29:55 +0100243 packageCheck = pctx.AndroidStaticRule("packageCheck",
244 blueprint.RuleParams{
245 Command: "rm -f $out && " +
246 "${config.PackageCheckCmd} $in $packages && " +
Colin Crossb549b772020-06-03 17:14:31 +0000247 "touch $out",
Vladimir Marko0975ee02019-04-02 10:29:55 +0100248 CommandDeps: []string{"${config.PackageCheckCmd}"},
249 },
250 "packages")
251
Nan Zhang4c819fb2018-08-27 18:31:46 -0700252 jetifier = pctx.AndroidStaticRule("jetifier",
253 blueprint.RuleParams{
Colin Crossdb9f1af2022-12-13 15:57:01 -0800254 Command: "${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JetifierJar} -l error -o $out -i $in -t epoch",
Nan Zhang4c819fb2018-08-27 18:31:46 -0700255 CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"},
256 },
257 )
Colin Cross43f08db2018-11-12 10:13:39 -0800258
259 zipalign = pctx.AndroidStaticRule("zipalign",
260 blueprint.RuleParams{
Colin Crosse4246ab2019-02-05 21:55:21 -0800261 Command: "if ! ${config.ZipAlign} -c -p 4 $in > /dev/null; then " +
262 "${config.ZipAlign} -f -p 4 $in $out; " +
Colin Cross43f08db2018-11-12 10:13:39 -0800263 "else " +
264 "cp -f $in $out; " +
265 "fi",
266 CommandDeps: []string{"${config.ZipAlign}"},
267 },
268 )
Cole Faust2f1da162023-04-17 15:06:56 -0700269
Colin Crossf06d8dc2023-07-18 22:11:07 -0700270 convertImplementationJarToHeaderJarRule = pctx.AndroidStaticRule("convertImplementationJarToHeaderJar",
271 blueprint.RuleParams{
272 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} -x 'META-INF/services/**/*'`,
273 CommandDeps: []string{"${config.Zip2ZipCmd}"},
274 })
Colin Cross312634e2023-11-21 15:13:56 -0800275
276 writeCombinedProguardFlagsFileRule = pctx.AndroidStaticRule("writeCombinedProguardFlagsFileRule",
277 blueprint.RuleParams{
278 Command: `rm -f $out && ` +
279 `for f in $in; do ` +
280 ` echo && ` +
281 ` echo "# including $$f" && ` +
282 ` cat $$f; ` +
283 `done > $out`,
284 })
Jihoon Kang6592e872023-12-19 01:13:16 +0000285
286 gatherReleasedFlaggedApisRule = pctx.AndroidStaticRule("gatherReleasedFlaggedApisRule",
287 blueprint.RuleParams{
Yu Liu748ade22024-02-08 19:11:39 +0000288 Command: `${aconfig} dump-cache --dedup --format='{fully_qualified_name}={state:bool}' ` +
Jihoon Kang6592e872023-12-19 01:13:16 +0000289 `--out ${out} ` +
290 `${flags_path} ` +
291 `${filter_args} `,
292 CommandDeps: []string{"${aconfig}"},
293 Description: "aconfig_bool",
294 }, "flags_path", "filter_args")
295
296 generateMetalavaRevertAnnotationsRule = pctx.AndroidStaticRule("generateMetalavaRevertAnnotationsRule",
297 blueprint.RuleParams{
Jihoon Kang150d87f2024-01-09 19:21:27 +0000298 Command: `${keep-flagged-apis} ${in} > ${out}`,
299 CommandDeps: []string{"${keep-flagged-apis}"},
Jihoon Kang6592e872023-12-19 01:13:16 +0000300 })
Colin Cross2fe66872015-03-30 17:20:39 -0700301)
302
303func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700304 pctx.Import("android/soong/android")
Colin Cross64162712017-08-08 13:17:59 -0700305 pctx.Import("android/soong/java/config")
Jihoon Kang6592e872023-12-19 01:13:16 +0000306
307 pctx.HostBinToolVariable("aconfig", "aconfig")
308 pctx.HostBinToolVariable("keep-flagged-apis", "keep-flagged-apis")
Colin Cross2fe66872015-03-30 17:20:39 -0700309}
310
311type javaBuilderFlags struct {
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700312 javacFlags string
313
314 // bootClasspath is the list of jars that form the boot classpath (generally the java.* and
315 // android.* classes) for tools that still use it. javac targeting 1.9 or higher uses
316 // systemModules and java9Classpath instead.
317 bootClasspath classpath
318
319 // classpath is the list of jars that form the classpath for javac and kotlinc rules. It
320 // contains header jars for all static and non-static dependencies.
321 classpath classpath
322
323 // dexClasspath is the list of jars that form the classpath for d8 and r8 rules. It contains
324 // header jars for all non-static dependencies. Static dependencies have already been
325 // combined into the program jar.
326 dexClasspath classpath
327
328 // java9Classpath is the list of jars that will be added to the classpath when targeting
329 // 1.9 or higher. It generally contains the android.* classes, while the java.* classes
330 // are provided by systemModules.
Colin Cross6cef4812019-10-17 14:23:50 -0700331 java9Classpath classpath
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700332
333 processorPath classpath
334 processors []string
335 systemModules *systemModules
336 aidlFlags string
337 aidlDeps android.Paths
338 javaVersion javaVersion
Colin Cross6af17aa2017-09-20 12:59:05 -0700339
Andreas Gampef3e5b552018-01-22 21:27:21 -0800340 errorProneExtraJavacFlags string
Colin Cross66548102018-06-19 22:47:35 -0700341 errorProneProcessorPath classpath
Andreas Gampef3e5b552018-01-22 21:27:21 -0800342
Colin Cross93e85952017-08-15 13:34:18 -0700343 kotlincFlags string
344 kotlincClasspath classpath
Colin Crossa1ff7c62021-09-17 14:11:52 -0700345 kotlincDeps android.Paths
Colin Cross93e85952017-08-15 13:34:18 -0700346
Colin Cross19878da2019-03-28 14:45:07 -0700347 proto android.ProtoFlags
Colin Cross2fe66872015-03-30 17:20:39 -0700348}
349
Joe Onorato175073c2023-06-01 14:42:59 -0700350func DefaultJavaBuilderFlags() javaBuilderFlags {
351 return javaBuilderFlags{
352 javaVersion: JAVA_VERSION_8,
353 }
354}
355
Nan Zhang61eaedb2017-11-02 13:28:15 -0700356func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
Vadim Spivak3c496f02023-06-08 06:14:59 +0000357 srcFiles, srcJars android.Paths, annoSrcJar android.WritablePath, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700358
Nan Zhang61eaedb2017-11-02 13:28:15 -0700359 // Compile java sources into .class files
360 desc := "javac"
361 if shardIdx >= 0 {
362 desc += strconv.Itoa(shardIdx)
363 }
364
Vadim Spivak3c496f02023-06-08 06:14:59 +0000365 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, annoSrcJar, flags, deps, "javac", desc)
Colin Cross2fe66872015-03-30 17:20:39 -0700366}
367
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800368// Emits the rule to generate Xref input file (.kzip file) for the given set of source files and source jars
369// to compile with given set of builder flags, etc.
Colin Cross3b706fd2019-09-05 16:44:18 -0700370func emitXrefRule(ctx android.ModuleContext, xrefFile android.WritablePath, idx int,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800371 srcFiles, srcJars android.Paths,
Colin Cross3b706fd2019-09-05 16:44:18 -0700372 flags javaBuilderFlags, deps android.Paths) {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800373
374 deps = append(deps, srcJars...)
Sasha Smundak09950a42019-11-04 16:29:45 -0800375 classpath := flags.classpath
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800376
377 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700378 if flags.javaVersion.usesJavaModules() {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800379 var systemModuleDeps android.Paths
380 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
381 deps = append(deps, systemModuleDeps...)
Sasha Smundak09950a42019-11-04 16:29:45 -0800382 classpath = append(flags.java9Classpath, classpath...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800383 } else {
384 deps = append(deps, flags.bootClasspath...)
385 if len(flags.bootClasspath) == 0 && ctx.Device() {
386 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
387 // ensure java does not fall back to the default bootclasspath.
388 bootClasspath = `-bootclasspath ""`
389 } else {
390 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
391 }
392 }
393
Sasha Smundak09950a42019-11-04 16:29:45 -0800394 deps = append(deps, classpath...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800395 deps = append(deps, flags.processorPath...)
396
397 processor := "-proc:none"
Colin Cross5a116862020-04-22 11:44:34 -0700398 if len(flags.processors) > 0 {
399 processor = "-processor " + strings.Join(flags.processors, ",")
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800400 }
401
Colin Cross3b706fd2019-09-05 16:44:18 -0700402 intermediatesDir := "xref"
403 if idx >= 0 {
404 intermediatesDir += strconv.Itoa(idx)
405 }
406
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800407 ctx.Build(pctx,
408 android.BuildParams{
409 Rule: kytheExtract,
410 Description: "Xref Java extractor",
411 Output: xrefFile,
412 Inputs: srcFiles,
413 Implicits: deps,
414 Args: map[string]string{
415 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, "anno").String(),
416 "bootClasspath": bootClasspath,
Sasha Smundak09950a42019-11-04 16:29:45 -0800417 "classpath": classpath.FormJavaClassPath("-classpath"),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800418 "javacFlags": flags.javacFlags,
Colin Cross1e743852019-10-28 11:37:20 -0700419 "javaVersion": flags.javaVersion.String(),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800420 "outDir": android.PathForModuleOut(ctx, "javac", "classes.xref").String(),
421 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
422 "processor": processor,
423 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, "srcjars.xref").String(),
424 "srcJars": strings.Join(srcJars.Strings(), " "),
425 },
426 })
427}
428
Colin Crossc52d5232024-01-10 16:43:33 -0800429func turbineFlags(ctx android.ModuleContext, flags javaBuilderFlags, dir string) (string, android.Paths) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700430 var deps android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700431
Colin Cross6cef4812019-10-17 14:23:50 -0700432 classpath := flags.classpath
433
Nan Zhanged19fc32017-10-19 13:06:22 -0700434 var bootClasspath string
Colin Crossbf3119e2019-10-28 14:25:10 -0700435 if flags.javaVersion.usesJavaModules() {
436 var systemModuleDeps android.Paths
437 bootClasspath, systemModuleDeps = flags.systemModules.FormTurbineSystemModulesPath(ctx.Device())
438 deps = append(deps, systemModuleDeps...)
Colin Cross6cef4812019-10-17 14:23:50 -0700439 classpath = append(flags.java9Classpath, classpath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700440 } else {
Colin Crossbf3119e2019-10-28 14:25:10 -0700441 deps = append(deps, flags.bootClasspath...)
442 if len(flags.bootClasspath) == 0 && ctx.Device() {
443 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
444 // ensure turbine does not fall back to the default bootclasspath.
445 bootClasspath = `--bootclasspath ""`
446 } else {
Colin Crossc2557d12019-10-31 15:22:57 -0700447 bootClasspath = flags.bootClasspath.FormTurbineClassPath("--bootclasspath ")
Colin Crossbf3119e2019-10-28 14:25:10 -0700448 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700449 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700450
Colin Cross6cef4812019-10-17 14:23:50 -0700451 deps = append(deps, classpath...)
Colin Crossf61766e2022-03-16 18:06:48 -0700452 turbineFlags := bootClasspath + " " + classpath.FormTurbineClassPath("--classpath ")
453
Colin Crossc52d5232024-01-10 16:43:33 -0800454 const flagsLimit = 32 * 1024
455 if len(turbineFlags) > flagsLimit {
456 flagsRspFile := android.PathForModuleOut(ctx, dir, "turbine-flags.rsp")
457 android.WriteFileRule(ctx, flagsRspFile, turbineFlags)
458 turbineFlags = "@" + flagsRspFile.String()
459 deps = append(deps, flagsRspFile)
460 }
461
Colin Crossf61766e2022-03-16 18:06:48 -0700462 return turbineFlags, deps
463}
464
465func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
466 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
467
Colin Crossc52d5232024-01-10 16:43:33 -0800468 turbineFlags, deps := turbineFlags(ctx, flags, "turbine")
Colin Crossf61766e2022-03-16 18:06:48 -0700469
470 deps = append(deps, srcJars...)
Colin Crossbf3119e2019-10-28 14:25:10 -0700471
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700472 rule := turbine
473 args := map[string]string{
Colin Crossf61766e2022-03-16 18:06:48 -0700474 "javacFlags": flags.javacFlags,
475 "srcJars": strings.Join(srcJars.Strings(), " "),
476 "javaVersion": flags.javaVersion.String(),
477 "turbineFlags": turbineFlags,
478 "outputFlags": "--output " + outputFile.String() + ".tmp",
479 "outputs": outputFile.String(),
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700480 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400481 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_TURBINE") {
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700482 rule = turbineRE
483 args["implicits"] = strings.Join(deps.Strings(), ",")
Colin Crossf61766e2022-03-16 18:06:48 -0700484 args["rbeOutputs"] = outputFile.String() + ".tmp"
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700485 }
Colin Crossae887032017-10-23 17:16:14 -0700486 ctx.Build(pctx, android.BuildParams{
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700487 Rule: rule,
Nan Zhanged19fc32017-10-19 13:06:22 -0700488 Description: "turbine",
489 Output: outputFile,
490 Inputs: srcFiles,
491 Implicits: deps,
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700492 Args: args,
Nan Zhanged19fc32017-10-19 13:06:22 -0700493 })
494}
495
Colin Crossf61766e2022-03-16 18:06:48 -0700496// TurbineApt produces a rule to run annotation processors using turbine.
Isaac Chioua23d9942022-04-06 06:14:38 +0000497func TurbineApt(ctx android.ModuleContext, outputSrcJar, outputResJar android.WritablePath,
Colin Crossf61766e2022-03-16 18:06:48 -0700498 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
499
Colin Crossc52d5232024-01-10 16:43:33 -0800500 turbineFlags, deps := turbineFlags(ctx, flags, "kapt")
Colin Crossf61766e2022-03-16 18:06:48 -0700501
502 deps = append(deps, srcJars...)
503
504 deps = append(deps, flags.processorPath...)
505 turbineFlags += " " + flags.processorPath.FormTurbineClassPath("--processorpath ")
506 turbineFlags += " --processors " + strings.Join(flags.processors, " ")
507
Isaac Chioua23d9942022-04-06 06:14:38 +0000508 outputs := android.WritablePaths{outputSrcJar, outputResJar}
509 outputFlags := "--gensrc_output " + outputSrcJar.String() + ".tmp " +
Colin Crossf61766e2022-03-16 18:06:48 -0700510 "--resource_output " + outputResJar.String() + ".tmp"
511
512 rule := turbine
513 args := map[string]string{
514 "javacFlags": flags.javacFlags,
515 "srcJars": strings.Join(srcJars.Strings(), " "),
516 "javaVersion": flags.javaVersion.String(),
517 "turbineFlags": turbineFlags,
518 "outputFlags": outputFlags,
519 "outputs": strings.Join(outputs.Strings(), " "),
520 }
521 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_TURBINE") {
522 rule = turbineRE
523 args["implicits"] = strings.Join(deps.Strings(), ",")
Isaac Chioua23d9942022-04-06 06:14:38 +0000524 args["rbeOutputs"] = outputSrcJar.String() + ".tmp," + outputResJar.String() + ".tmp"
Colin Crossf61766e2022-03-16 18:06:48 -0700525 }
526 ctx.Build(pctx, android.BuildParams{
527 Rule: rule,
528 Description: "turbine apt",
529 Output: outputs[0],
530 ImplicitOutputs: outputs[1:],
531 Inputs: srcFiles,
532 Implicits: deps,
533 Args: args,
534 })
535}
536
Colin Cross070879e2017-10-11 11:21:07 -0700537// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700538// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
539// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700540//
541// This method may be used for different compilers, including javac and Error Prone. The rule
542// argument specifies which command line to use and desc sets the description of the rule that will
543// be printed at build time. The stem argument provides the file name of the output jar, and
544// suffix will be appended to various intermediate files and directories to avoid collisions when
545// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700546func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Vadim Spivak3c496f02023-06-08 06:14:59 +0000547 shardIdx int, srcFiles, srcJars android.Paths, annoSrcJar android.WritablePath,
Colin Crosse9a275b2017-10-16 17:09:48 -0700548 flags javaBuilderFlags, deps android.Paths,
Colin Cross66548102018-06-19 22:47:35 -0700549 intermediatesDir, desc string) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700550
Colin Cross59149b62017-10-16 18:07:29 -0700551 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700552
Colin Crossc52d5232024-01-10 16:43:33 -0800553 javacClasspath := flags.classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700554
Colin Cross1369cdb2017-09-29 17:58:17 -0700555 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700556 if flags.javaVersion.usesJavaModules() {
Colin Crossb77043e2019-07-16 13:57:13 -0700557 var systemModuleDeps android.Paths
558 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
559 deps = append(deps, systemModuleDeps...)
Colin Crossc52d5232024-01-10 16:43:33 -0800560 javacClasspath = append(flags.java9Classpath, javacClasspath...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700561 } else {
562 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700563 if len(flags.bootClasspath) == 0 && ctx.Device() {
564 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
565 // ensure java does not fall back to the default bootclasspath.
566 bootClasspath = `-bootclasspath ""`
567 } else {
568 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
569 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700570 }
571
Colin Crossc52d5232024-01-10 16:43:33 -0800572 classpathArg := javacClasspath.FormJavaClassPath("-classpath")
573
574 // Keep the command line under the MAX_ARG_STRLEN limit by putting the classpath argument into an rsp file
575 // if it is too long.
576 const classpathLimit = 64 * 1024
577 if len(classpathArg) > classpathLimit {
578 classpathRspFile := outputFile.ReplaceExtension(ctx, "classpath")
579 android.WriteFileRule(ctx, classpathRspFile, classpathArg)
580 deps = append(deps, classpathRspFile)
581 classpathArg = "@" + classpathRspFile.String()
582 }
583
584 deps = append(deps, javacClasspath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700585 deps = append(deps, flags.processorPath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700586
Colin Cross7788c122019-01-23 16:14:02 -0800587 processor := "-proc:none"
Colin Cross5a116862020-04-22 11:44:34 -0700588 if len(flags.processors) > 0 {
589 processor = "-processor " + strings.Join(flags.processors, ",")
Colin Crossbe9cdb82019-01-21 21:37:16 -0800590 }
591
Nan Zhang61eaedb2017-11-02 13:28:15 -0700592 srcJarDir := "srcjars"
593 outDir := "classes"
594 annoDir := "anno"
595 if shardIdx >= 0 {
596 shardDir := "shard" + strconv.Itoa(shardIdx)
597 srcJarDir = filepath.Join(shardDir, srcJarDir)
598 outDir = filepath.Join(shardDir, outDir)
599 annoDir = filepath.Join(shardDir, annoDir)
600 }
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400601 rule := javac
Ramy Medhat16f23a42020-09-03 01:29:49 -0400602 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAVAC") {
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400603 rule = javacRE
604 }
Colin Crossae887032017-10-23 17:16:14 -0700605 ctx.Build(pctx, android.BuildParams{
Vadim Spivak3c496f02023-06-08 06:14:59 +0000606 Rule: rule,
607 Description: desc,
608 Output: outputFile,
609 ImplicitOutput: annoSrcJar,
610 Inputs: srcFiles,
611 Implicits: deps,
Colin Crossc6bbef32017-08-14 14:16:06 -0700612 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700613 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700614 "bootClasspath": bootClasspath,
Colin Crossc52d5232024-01-10 16:43:33 -0800615 "classpath": classpathArg,
Colin Cross6a77c982018-06-19 22:43:34 -0700616 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
Colin Crossbe9cdb82019-01-21 21:37:16 -0800617 "processor": processor,
Colin Cross8eadbf02017-10-24 17:46:00 -0700618 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700619 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
620 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
621 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Vadim Spivak3c496f02023-06-08 06:14:59 +0000622 "annoSrcJar": annoSrcJar.String(),
Colin Cross1e743852019-10-28 11:37:20 -0700623 "javaVersion": flags.javaVersion.String(),
Colin Crossc6bbef32017-08-14 14:16:06 -0700624 },
625 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700626}
627
Colin Crosse9a275b2017-10-16 17:09:48 -0700628func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
629 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700630
Kousik Kumar366afc52020-05-20 11:27:16 -0700631 rule := jar
Ramy Medhat16f23a42020-09-03 01:29:49 -0400632 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAR") {
Kousik Kumar366afc52020-05-20 11:27:16 -0700633 rule = jarRE
634 }
Colin Crossae887032017-10-23 17:16:14 -0700635 ctx.Build(pctx, android.BuildParams{
Kousik Kumar366afc52020-05-20 11:27:16 -0700636 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700637 Description: "jar",
638 Output: outputFile,
639 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700640 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -0800641 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700642 },
643 })
Colin Cross2fe66872015-03-30 17:20:39 -0700644}
645
Nan Zhanged19fc32017-10-19 13:06:22 -0700646func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
Colin Cross37f6d792018-07-12 12:28:41 -0700647 jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
648 dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700649
Colin Cross635acc92017-09-12 22:50:46 -0700650 var deps android.Paths
651
652 var jarArgs []string
653 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700654 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700655 deps = append(deps, manifest.Path())
656 }
657
Colin Cross37f6d792018-07-12 12:28:41 -0700658 for _, dir := range dirsToStrip {
659 jarArgs = append(jarArgs, "-stripDir ", dir)
660 }
661
662 for _, file := range filesToStrip {
663 jarArgs = append(jarArgs, "-stripFile ", file)
Nan Zhanged19fc32017-10-19 13:06:22 -0700664 }
665
Colin Cross7b60cdd2017-12-21 13:52:58 -0800666 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
667 // for downstream tools like desugar.
668 jarArgs = append(jarArgs, "-stripFile module-info.class")
669
Colin Cross37f6d792018-07-12 12:28:41 -0700670 if stripDirEntries {
Colin Cross635acc92017-09-12 22:50:46 -0700671 jarArgs = append(jarArgs, "-D")
672 }
673
Colin Crossc52d5232024-01-10 16:43:33 -0800674 rule := combineJar
675 // Keep the command line under the MAX_ARG_STRLEN limit by putting the list of jars into an rsp file
676 // if it is too long.
677 const jarsLengthLimit = 64 * 1024
678 jarsLength := 0
679 for i, jar := range jars {
680 if i != 0 {
681 jarsLength += 1
682 }
683 jarsLength += len(jar.String())
684 }
685 if jarsLength > jarsLengthLimit {
686 rule = combineJarRsp
687 }
688
Colin Crossae887032017-10-23 17:16:14 -0700689 ctx.Build(pctx, android.BuildParams{
Colin Crossc52d5232024-01-10 16:43:33 -0800690 Rule: rule,
Nan Zhanged19fc32017-10-19 13:06:22 -0700691 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700692 Output: outputFile,
693 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700694 Implicits: deps,
695 Args: map[string]string{
696 "jarArgs": strings.Join(jarArgs, " "),
697 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700698 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700699}
700
Colin Crossf06d8dc2023-07-18 22:11:07 -0700701func convertImplementationJarToHeaderJar(ctx android.ModuleContext, implementationJarFile android.Path,
702 headerJarFile android.WritablePath) {
703 ctx.Build(pctx, android.BuildParams{
704 Rule: convertImplementationJarToHeaderJarRule,
705 Input: implementationJarFile,
706 Output: headerJarFile,
707 })
708}
709
Colin Crosse9a275b2017-10-16 17:09:48 -0700710func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
711 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700712 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700713 Rule: jarjar,
714 Description: "jarjar",
715 Output: outputFile,
716 Input: classesJar,
717 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700718 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700719 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700720 },
721 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700722}
Colin Cross6ade34f2017-09-15 13:00:47 -0700723
Colin Crossb549b772020-06-03 17:14:31 +0000724func CheckJarPackages(ctx android.ModuleContext, outputFile android.WritablePath,
725 classesJar android.Path, permittedPackages []string) {
Vladimir Marko0975ee02019-04-02 10:29:55 +0100726 ctx.Build(pctx, android.BuildParams{
727 Rule: packageCheck,
728 Description: "packageCheck",
729 Output: outputFile,
730 Input: classesJar,
731 Args: map[string]string{
732 "packages": strings.Join(permittedPackages, " "),
733 },
734 })
735}
736
Nan Zhang4c819fb2018-08-27 18:31:46 -0700737func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath,
738 inputFile android.Path) {
739 ctx.Build(pctx, android.BuildParams{
740 Rule: jetifier,
741 Description: "jetifier",
742 Output: outputFile,
743 Input: inputFile,
744 })
745}
746
Colin Cross094054a2018-10-17 15:10:48 -0700747func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) {
Colin Crosscf371cc2020-11-13 11:48:42 -0800748 android.WriteFileRule(ctx, outputFile, "Main-Class: "+mainClass+"\n")
Colin Cross094054a2018-10-17 15:10:48 -0700749}
750
Cole Faust61585282023-07-14 16:23:39 -0700751func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path, validations android.Paths) {
Colin Cross43f08db2018-11-12 10:13:39 -0800752 ctx.Build(pctx, android.BuildParams{
753 Rule: zipalign,
754 Description: "align",
755 Input: inputFile,
756 Output: outputFile,
Cole Faust61585282023-07-14 16:23:39 -0700757 Validations: validations,
Colin Cross43f08db2018-11-12 10:13:39 -0800758 })
759}
760
Colin Cross312634e2023-11-21 15:13:56 -0800761func writeCombinedProguardFlagsFile(ctx android.ModuleContext, outputFile android.WritablePath, files android.Paths) {
762 ctx.Build(pctx, android.BuildParams{
763 Rule: writeCombinedProguardFlagsFileRule,
764 Description: "write combined proguard flags file",
765 Inputs: files,
766 Output: outputFile,
767 })
768}
769
Colin Cross33961b52019-07-11 11:01:22 -0700770type classpath android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700771
Colin Crossc2557d12019-10-31 15:22:57 -0700772func (x *classpath) formJoinedClassPath(optName string, sep string) string {
Colin Cross81440082018-08-15 20:21:55 -0700773 if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") {
774 optName += " "
775 }
Colin Cross59149b62017-10-16 18:07:29 -0700776 if len(*x) > 0 {
Colin Crossc2557d12019-10-31 15:22:57 -0700777 return optName + strings.Join(x.Strings(), sep)
Colin Cross6ade34f2017-09-15 13:00:47 -0700778 } else {
779 return ""
780 }
781}
Colin Crossc2557d12019-10-31 15:22:57 -0700782func (x *classpath) FormJavaClassPath(optName string) string {
783 return x.formJoinedClassPath(optName, ":")
784}
Colin Cross6ade34f2017-09-15 13:00:47 -0700785
Colin Crossc2557d12019-10-31 15:22:57 -0700786func (x *classpath) FormTurbineClassPath(optName string) string {
787 return x.formJoinedClassPath(optName, " ")
788}
789
790// FormRepeatedClassPath returns a list of arguments with the given optName prefixed to each element of the classpath.
791func (x *classpath) FormRepeatedClassPath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700792 if x == nil || *x == nil {
793 return nil
794 }
795 flags := make([]string, len(*x))
796 for i, v := range *x {
Colin Crossafbb1732019-01-17 15:42:52 -0800797 flags[i] = optName + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700798 }
799
800 return flags
801}
802
Colin Cross6ade34f2017-09-15 13:00:47 -0700803// Convert a classpath to an android.Paths
804func (x *classpath) Paths() android.Paths {
805 return append(android.Paths(nil), (*x)...)
806}
807
808func (x *classpath) Strings() []string {
809 if x == nil {
810 return nil
811 }
812 ret := make([]string, len(*x))
813 for i, path := range *x {
814 ret[i] = path.String()
815 }
816 return ret
817}
Colin Crossb77043e2019-07-16 13:57:13 -0700818
819type systemModules struct {
820 dir android.Path
821 deps android.Paths
822}
823
Colin Crossbf3119e2019-10-28 14:25:10 -0700824// Returns a --system argument in the form javac expects with -source 1.9 and the list of files to
825// depend on. If forceEmpty is true, returns --system=none if the list is empty to ensure javac
826// does not fall back to the default system modules.
Colin Crossb77043e2019-07-16 13:57:13 -0700827func (x *systemModules) FormJavaSystemModulesPath(forceEmpty bool) (string, android.Paths) {
828 if x != nil {
829 return "--system=" + x.dir.String(), x.deps
830 } else if forceEmpty {
831 return "--system=none", nil
832 } else {
833 return "", nil
834 }
835}
Colin Crossbf3119e2019-10-28 14:25:10 -0700836
837// Returns a --system argument in the form turbine expects with -source 1.9 and the list of files to
838// depend on. If forceEmpty is true, returns --bootclasspath "" if the list is empty to ensure turbine
839// does not fall back to the default bootclasspath.
840func (x *systemModules) FormTurbineSystemModulesPath(forceEmpty bool) (string, android.Paths) {
841 if x != nil {
842 return "--system " + x.dir.String(), x.deps
843 } else if forceEmpty {
844 return `--bootclasspath ""`, nil
845 } else {
Colin Crossf61766e2022-03-16 18:06:48 -0700846 return "--system ${config.JavaHome}", nil
Colin Crossbf3119e2019-10-28 14:25:10 -0700847 }
848}