blob: 5d84d0b4309e40d75d59f70963e4b8abb92e14f9 [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",
Anna Bauzad0476e42024-04-11 09:58:46 +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 ` +
Sorin Basca79b7ca12024-03-14 16:59:49 +0000123 `--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED ` +
124 `--add-exports=jdk.internal.opt/jdk.internal.opt=ALL-UNNAMED ` +
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800125 `-jar ${config.JavaKytheExtractorJar} ` +
126 `${config.JavacHeapFlags} ${config.CommonJdkFlags} ` +
127 `$processorpath $processor $javacFlags $bootClasspath $classpath ` +
128 `-source $javaVersion -target $javaVersion ` +
129 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list)`,
130 CommandDeps: []string{
131 "${config.JavaCmd}",
132 "${config.JavaKytheExtractorJar}",
Sasha Smundak65143642019-09-26 20:14:28 -0700133 "${kytheVnames}",
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800134 "${config.ZipSyncCmd}",
135 },
136 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
137 Rspfile: "$out.rsp",
138 RspfileContent: "$in",
139 },
140 "javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
141 "outDir", "annoDir", "javaVersion")
142
Sasha Smundaka7856c02020-04-23 09:49:59 -0700143 extractMatchingApks = pctx.StaticRule(
144 "extractMatchingApks",
145 blueprint.RuleParams{
146 Command: `rm -rf "$out" && ` +
Colin Crossffbcd1d2021-11-12 12:19:42 -0800147 `${config.ExtractApksCmd} -o "${out}" -zip "${zip}" -allow-prereleased=${allow-prereleased} ` +
Pranav Gupta51645ff2023-03-20 16:19:53 -0700148 `-sdk-version=${sdk-version} -skip-sdk-check=${skip-sdk-check} -abis=${abis} ` +
Sasha Smundaka7856c02020-04-23 09:49:59 -0700149 `--screen-densities=${screen-densities} --stem=${stem} ` +
Jaewoong Jung11c1e0f2020-06-29 19:18:44 -0700150 `-apkcerts=${apkcerts} -partition=${partition} ` +
Sasha Smundaka7856c02020-04-23 09:49:59 -0700151 `${in}`,
152 CommandDeps: []string{"${config.ExtractApksCmd}"},
153 },
Pranav Gupta51645ff2023-03-20 16:19:53 -0700154 "abis", "allow-prereleased", "screen-densities", "sdk-version", "skip-sdk-check", "stem", "apkcerts", "partition", "zip")
Sasha Smundaka7856c02020-04-23 09:49:59 -0700155
Colin Cross77cdcfd2021-03-12 11:28:25 -0800156 turbine, turbineRE = pctx.RemoteStaticRules("turbine",
Nan Zhanged19fc32017-10-19 13:06:22 -0700157 blueprint.RuleParams{
Colin Crossf61766e2022-03-16 18:06:48 -0700158 Command: `$reTemplate${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.TurbineJar} $outputFlags ` +
Colin Cross411647e2022-03-16 14:28:36 -0700159 `--sources @$out.rsp --source_jars $srcJars ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700160 `--javacopts ${config.CommonJdkFlags} ` +
Colin Crossf61766e2022-03-16 18:06:48 -0700161 `$javacFlags -source $javaVersion -target $javaVersion -- $turbineFlags && ` +
162 `(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 -0700163 CommandDeps: []string{
164 "${config.TurbineJar}",
165 "${config.JavaCmd}",
Colin Cross8eadbf02017-10-24 17:46:00 -0700166 },
Nan Zhanged19fc32017-10-19 13:06:22 -0700167 Rspfile: "$out.rsp",
168 RspfileContent: "$in",
169 Restat: true,
170 },
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700171 &remoteexec.REParams{Labels: map[string]string{"type": "tool", "name": "turbine"},
Colin Cross411647e2022-03-16 14:28:36 -0700172 ExecStrategy: "${config.RETurbineExecStrategy}",
173 Inputs: []string{"${config.TurbineJar}", "${out}.rsp", "$implicits"},
174 RSPFiles: []string{"${out}.rsp"},
Colin Crossf61766e2022-03-16 18:06:48 -0700175 OutputFiles: []string{"$rbeOutputs"},
Colin Cross411647e2022-03-16 14:28:36 -0700176 ToolchainInputs: []string{"${config.JavaCmd}"},
177 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
Colin Crossf61766e2022-03-16 18:06:48 -0700178 },
179 []string{"javacFlags", "turbineFlags", "outputFlags", "javaVersion", "outputs", "rbeOutputs", "srcJars"}, []string{"implicits"})
Nan Zhanged19fc32017-10-19 13:06:22 -0700180
Colin Cross77cdcfd2021-03-12 11:28:25 -0800181 jar, jarRE = pctx.RemoteStaticRules("jar",
Colin Cross2fe66872015-03-30 17:20:39 -0700182 blueprint.RuleParams{
Kousik Kumar366afc52020-05-20 11:27:16 -0700183 Command: `$reTemplate${config.SoongZipCmd} -jar -o $out @$out.rsp`,
Nan Zhang674dd932018-01-26 18:30:36 -0800184 CommandDeps: []string{"${config.SoongZipCmd}"},
185 Rspfile: "$out.rsp",
186 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -0700187 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700188 &remoteexec.REParams{
189 ExecStrategy: "${config.REJarExecStrategy}",
190 Inputs: []string{"${config.SoongZipCmd}", "${out}.rsp"},
Colin Crossa4eafdd2021-03-24 14:09:28 -0700191 RSPFiles: []string{"${out}.rsp"},
Kousik Kumar366afc52020-05-20 11:27:16 -0700192 OutputFiles: []string{"$out"},
193 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
194 }, []string{"jarArgs"}, nil)
Colin Cross0a6e0072017-08-30 14:24:55 -0700195
Colin Cross77cdcfd2021-03-12 11:28:25 -0800196 zip, zipRE = pctx.RemoteStaticRules("zip",
Colin Crossa4f08812018-10-02 22:03:40 -0700197 blueprint.RuleParams{
198 Command: `${config.SoongZipCmd} -o $out @$out.rsp`,
199 CommandDeps: []string{"${config.SoongZipCmd}"},
200 Rspfile: "$out.rsp",
201 RspfileContent: "$jarArgs",
202 },
Kousik Kumar366afc52020-05-20 11:27:16 -0700203 &remoteexec.REParams{
204 ExecStrategy: "${config.REZipExecStrategy}",
205 Inputs: []string{"${config.SoongZipCmd}", "${out}.rsp", "$implicits"},
Colin Crossa4eafdd2021-03-24 14:09:28 -0700206 RSPFiles: []string{"${out}.rsp"},
Kousik Kumar366afc52020-05-20 11:27:16 -0700207 OutputFiles: []string{"$out"},
208 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
209 }, []string{"jarArgs"}, []string{"implicits"})
Colin Crossa4f08812018-10-02 22:03:40 -0700210
Colin Cross0a6e0072017-08-30 14:24:55 -0700211 combineJar = pctx.AndroidStaticRule("combineJar",
212 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800213 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700214 CommandDeps: []string{"${config.MergeZipsCmd}"},
215 },
Colin Cross635acc92017-09-12 22:50:46 -0700216 "jarArgs")
Colin Crossc52d5232024-01-10 16:43:33 -0800217 combineJarRsp = pctx.AndroidStaticRule("combineJarRsp",
218 blueprint.RuleParams{
219 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out @$out.rsp`,
220 CommandDeps: []string{"${config.MergeZipsCmd}"},
221 Rspfile: "$out.rsp",
222 RspfileContent: "$in",
223 },
224 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700225
Colin Cross9d45bb72016-08-29 16:14:13 -0700226 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700227 blueprint.RuleParams{
Colin Crossd97cf632021-02-08 10:14:04 -0800228 Command: "" +
229 // Jarjar doesn't exit with an error when the rules file contains a syntax error,
230 // leading to stale or missing files later in the build. Remove the output file
231 // before running jarjar.
232 "rm -f ${out} && " +
233 "${config.JavaCmd} ${config.JavaVmFlags}" +
Artur Satayev762d9f32020-04-15 12:50:04 +0100234 // b/146418363 Enable Android specific jarjar transformer to drop compat annotations
235 // for newly repackaged classes. Dropping @UnsupportedAppUsage on repackaged classes
236 // avoids adding new hiddenapis after jarjar'ing.
237 " -DremoveAndroidCompatAnnotations=true" +
Colin Crossd97cf632021-02-08 10:14:04 -0800238 " -jar ${config.JarjarCmd} process $rulesFile $in $out && " +
239 // Turn a missing output file into a ninja error
240 `[ -e ${out} ] || (echo "Missing output file"; exit 1)`,
Colin Cross64162712017-08-08 13:17:59 -0700241 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700242 },
243 "rulesFile")
Nan Zhang4c819fb2018-08-27 18:31:46 -0700244
Vladimir Marko0975ee02019-04-02 10:29:55 +0100245 packageCheck = pctx.AndroidStaticRule("packageCheck",
246 blueprint.RuleParams{
247 Command: "rm -f $out && " +
248 "${config.PackageCheckCmd} $in $packages && " +
Colin Crossb549b772020-06-03 17:14:31 +0000249 "touch $out",
Vladimir Marko0975ee02019-04-02 10:29:55 +0100250 CommandDeps: []string{"${config.PackageCheckCmd}"},
251 },
252 "packages")
253
Nan Zhang4c819fb2018-08-27 18:31:46 -0700254 jetifier = pctx.AndroidStaticRule("jetifier",
255 blueprint.RuleParams{
Colin Crossdb9f1af2022-12-13 15:57:01 -0800256 Command: "${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JetifierJar} -l error -o $out -i $in -t epoch",
Nan Zhang4c819fb2018-08-27 18:31:46 -0700257 CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"},
258 },
259 )
Colin Cross43f08db2018-11-12 10:13:39 -0800260
261 zipalign = pctx.AndroidStaticRule("zipalign",
262 blueprint.RuleParams{
Colin Crosse4246ab2019-02-05 21:55:21 -0800263 Command: "if ! ${config.ZipAlign} -c -p 4 $in > /dev/null; then " +
264 "${config.ZipAlign} -f -p 4 $in $out; " +
Colin Cross43f08db2018-11-12 10:13:39 -0800265 "else " +
266 "cp -f $in $out; " +
267 "fi",
268 CommandDeps: []string{"${config.ZipAlign}"},
269 },
270 )
Cole Faust2f1da162023-04-17 15:06:56 -0700271
Colin Crossf06d8dc2023-07-18 22:11:07 -0700272 convertImplementationJarToHeaderJarRule = pctx.AndroidStaticRule("convertImplementationJarToHeaderJar",
273 blueprint.RuleParams{
274 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} -x 'META-INF/services/**/*'`,
275 CommandDeps: []string{"${config.Zip2ZipCmd}"},
276 })
Colin Cross312634e2023-11-21 15:13:56 -0800277
278 writeCombinedProguardFlagsFileRule = pctx.AndroidStaticRule("writeCombinedProguardFlagsFileRule",
279 blueprint.RuleParams{
280 Command: `rm -f $out && ` +
281 `for f in $in; do ` +
282 ` echo && ` +
283 ` echo "# including $$f" && ` +
284 ` cat $$f; ` +
285 `done > $out`,
286 })
Jihoon Kang6592e872023-12-19 01:13:16 +0000287
288 gatherReleasedFlaggedApisRule = pctx.AndroidStaticRule("gatherReleasedFlaggedApisRule",
289 blueprint.RuleParams{
Yu Liu748ade22024-02-08 19:11:39 +0000290 Command: `${aconfig} dump-cache --dedup --format='{fully_qualified_name}={state:bool}' ` +
Jihoon Kang6592e872023-12-19 01:13:16 +0000291 `--out ${out} ` +
292 `${flags_path} ` +
293 `${filter_args} `,
294 CommandDeps: []string{"${aconfig}"},
295 Description: "aconfig_bool",
296 }, "flags_path", "filter_args")
297
298 generateMetalavaRevertAnnotationsRule = pctx.AndroidStaticRule("generateMetalavaRevertAnnotationsRule",
299 blueprint.RuleParams{
Jihoon Kang150d87f2024-01-09 19:21:27 +0000300 Command: `${keep-flagged-apis} ${in} > ${out}`,
301 CommandDeps: []string{"${keep-flagged-apis}"},
Jihoon Kang6592e872023-12-19 01:13:16 +0000302 })
Colin Cross2fe66872015-03-30 17:20:39 -0700303)
304
305func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700306 pctx.Import("android/soong/android")
Colin Cross64162712017-08-08 13:17:59 -0700307 pctx.Import("android/soong/java/config")
Jihoon Kang6592e872023-12-19 01:13:16 +0000308
309 pctx.HostBinToolVariable("aconfig", "aconfig")
310 pctx.HostBinToolVariable("keep-flagged-apis", "keep-flagged-apis")
Colin Cross2fe66872015-03-30 17:20:39 -0700311}
312
313type javaBuilderFlags struct {
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700314 javacFlags string
315
316 // bootClasspath is the list of jars that form the boot classpath (generally the java.* and
317 // android.* classes) for tools that still use it. javac targeting 1.9 or higher uses
318 // systemModules and java9Classpath instead.
319 bootClasspath classpath
320
321 // classpath is the list of jars that form the classpath for javac and kotlinc rules. It
322 // contains header jars for all static and non-static dependencies.
323 classpath classpath
324
325 // dexClasspath is the list of jars that form the classpath for d8 and r8 rules. It contains
326 // header jars for all non-static dependencies. Static dependencies have already been
327 // combined into the program jar.
328 dexClasspath classpath
329
330 // java9Classpath is the list of jars that will be added to the classpath when targeting
331 // 1.9 or higher. It generally contains the android.* classes, while the java.* classes
332 // are provided by systemModules.
Colin Cross6cef4812019-10-17 14:23:50 -0700333 java9Classpath classpath
Colin Cross9bb9bfb2022-03-17 11:12:32 -0700334
335 processorPath classpath
336 processors []string
337 systemModules *systemModules
338 aidlFlags string
339 aidlDeps android.Paths
340 javaVersion javaVersion
Colin Cross6af17aa2017-09-20 12:59:05 -0700341
Andreas Gampef3e5b552018-01-22 21:27:21 -0800342 errorProneExtraJavacFlags string
Colin Cross66548102018-06-19 22:47:35 -0700343 errorProneProcessorPath classpath
Andreas Gampef3e5b552018-01-22 21:27:21 -0800344
Colin Cross93e85952017-08-15 13:34:18 -0700345 kotlincFlags string
346 kotlincClasspath classpath
Colin Crossa1ff7c62021-09-17 14:11:52 -0700347 kotlincDeps android.Paths
Colin Cross93e85952017-08-15 13:34:18 -0700348
Colin Cross19878da2019-03-28 14:45:07 -0700349 proto android.ProtoFlags
Colin Cross2fe66872015-03-30 17:20:39 -0700350}
351
Joe Onorato175073c2023-06-01 14:42:59 -0700352func DefaultJavaBuilderFlags() javaBuilderFlags {
353 return javaBuilderFlags{
354 javaVersion: JAVA_VERSION_8,
355 }
356}
357
Nan Zhang61eaedb2017-11-02 13:28:15 -0700358func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
Vadim Spivak3c496f02023-06-08 06:14:59 +0000359 srcFiles, srcJars android.Paths, annoSrcJar android.WritablePath, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700360
Nan Zhang61eaedb2017-11-02 13:28:15 -0700361 // Compile java sources into .class files
362 desc := "javac"
363 if shardIdx >= 0 {
364 desc += strconv.Itoa(shardIdx)
365 }
366
Vadim Spivak3c496f02023-06-08 06:14:59 +0000367 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, annoSrcJar, flags, deps, "javac", desc)
Colin Cross2fe66872015-03-30 17:20:39 -0700368}
369
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800370// Emits the rule to generate Xref input file (.kzip file) for the given set of source files and source jars
371// to compile with given set of builder flags, etc.
Colin Cross3b706fd2019-09-05 16:44:18 -0700372func emitXrefRule(ctx android.ModuleContext, xrefFile android.WritablePath, idx int,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800373 srcFiles, srcJars android.Paths,
Colin Cross3b706fd2019-09-05 16:44:18 -0700374 flags javaBuilderFlags, deps android.Paths) {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800375
376 deps = append(deps, srcJars...)
Sasha Smundak09950a42019-11-04 16:29:45 -0800377 classpath := flags.classpath
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800378
379 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700380 if flags.javaVersion.usesJavaModules() {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800381 var systemModuleDeps android.Paths
382 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
383 deps = append(deps, systemModuleDeps...)
Sasha Smundak09950a42019-11-04 16:29:45 -0800384 classpath = append(flags.java9Classpath, classpath...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800385 } else {
386 deps = append(deps, flags.bootClasspath...)
387 if len(flags.bootClasspath) == 0 && ctx.Device() {
388 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
389 // ensure java does not fall back to the default bootclasspath.
390 bootClasspath = `-bootclasspath ""`
391 } else {
392 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
393 }
394 }
395
Sasha Smundak09950a42019-11-04 16:29:45 -0800396 deps = append(deps, classpath...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800397 deps = append(deps, flags.processorPath...)
398
399 processor := "-proc:none"
Colin Cross5a116862020-04-22 11:44:34 -0700400 if len(flags.processors) > 0 {
401 processor = "-processor " + strings.Join(flags.processors, ",")
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800402 }
403
Colin Cross3b706fd2019-09-05 16:44:18 -0700404 intermediatesDir := "xref"
405 if idx >= 0 {
406 intermediatesDir += strconv.Itoa(idx)
407 }
408
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800409 ctx.Build(pctx,
410 android.BuildParams{
411 Rule: kytheExtract,
412 Description: "Xref Java extractor",
413 Output: xrefFile,
414 Inputs: srcFiles,
415 Implicits: deps,
416 Args: map[string]string{
417 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, "anno").String(),
418 "bootClasspath": bootClasspath,
Sasha Smundak09950a42019-11-04 16:29:45 -0800419 "classpath": classpath.FormJavaClassPath("-classpath"),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800420 "javacFlags": flags.javacFlags,
Colin Cross1e743852019-10-28 11:37:20 -0700421 "javaVersion": flags.javaVersion.String(),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800422 "outDir": android.PathForModuleOut(ctx, "javac", "classes.xref").String(),
423 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
424 "processor": processor,
425 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, "srcjars.xref").String(),
426 "srcJars": strings.Join(srcJars.Strings(), " "),
427 },
428 })
429}
430
Colin Crossc52d5232024-01-10 16:43:33 -0800431func turbineFlags(ctx android.ModuleContext, flags javaBuilderFlags, dir string) (string, android.Paths) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700432 var deps android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700433
Colin Cross6cef4812019-10-17 14:23:50 -0700434 classpath := flags.classpath
435
Nan Zhanged19fc32017-10-19 13:06:22 -0700436 var bootClasspath string
Colin Crossbf3119e2019-10-28 14:25:10 -0700437 if flags.javaVersion.usesJavaModules() {
438 var systemModuleDeps android.Paths
439 bootClasspath, systemModuleDeps = flags.systemModules.FormTurbineSystemModulesPath(ctx.Device())
440 deps = append(deps, systemModuleDeps...)
Colin Cross6cef4812019-10-17 14:23:50 -0700441 classpath = append(flags.java9Classpath, classpath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700442 } else {
Colin Crossbf3119e2019-10-28 14:25:10 -0700443 deps = append(deps, flags.bootClasspath...)
444 if len(flags.bootClasspath) == 0 && ctx.Device() {
445 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
446 // ensure turbine does not fall back to the default bootclasspath.
447 bootClasspath = `--bootclasspath ""`
448 } else {
Colin Crossc2557d12019-10-31 15:22:57 -0700449 bootClasspath = flags.bootClasspath.FormTurbineClassPath("--bootclasspath ")
Colin Crossbf3119e2019-10-28 14:25:10 -0700450 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700451 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700452
Colin Cross6cef4812019-10-17 14:23:50 -0700453 deps = append(deps, classpath...)
Colin Crossf61766e2022-03-16 18:06:48 -0700454 turbineFlags := bootClasspath + " " + classpath.FormTurbineClassPath("--classpath ")
455
Colin Crossc52d5232024-01-10 16:43:33 -0800456 const flagsLimit = 32 * 1024
457 if len(turbineFlags) > flagsLimit {
458 flagsRspFile := android.PathForModuleOut(ctx, dir, "turbine-flags.rsp")
459 android.WriteFileRule(ctx, flagsRspFile, turbineFlags)
460 turbineFlags = "@" + flagsRspFile.String()
461 deps = append(deps, flagsRspFile)
462 }
463
Colin Crossf61766e2022-03-16 18:06:48 -0700464 return turbineFlags, deps
465}
466
467func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
468 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
469
Colin Crossc52d5232024-01-10 16:43:33 -0800470 turbineFlags, deps := turbineFlags(ctx, flags, "turbine")
Colin Crossf61766e2022-03-16 18:06:48 -0700471
472 deps = append(deps, srcJars...)
Colin Crossbf3119e2019-10-28 14:25:10 -0700473
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700474 rule := turbine
475 args := map[string]string{
Colin Crossf61766e2022-03-16 18:06:48 -0700476 "javacFlags": flags.javacFlags,
477 "srcJars": strings.Join(srcJars.Strings(), " "),
478 "javaVersion": flags.javaVersion.String(),
479 "turbineFlags": turbineFlags,
480 "outputFlags": "--output " + outputFile.String() + ".tmp",
481 "outputs": outputFile.String(),
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700482 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400483 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_TURBINE") {
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700484 rule = turbineRE
485 args["implicits"] = strings.Join(deps.Strings(), ",")
Colin Crossf61766e2022-03-16 18:06:48 -0700486 args["rbeOutputs"] = outputFile.String() + ".tmp"
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700487 }
Colin Crossae887032017-10-23 17:16:14 -0700488 ctx.Build(pctx, android.BuildParams{
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700489 Rule: rule,
Nan Zhanged19fc32017-10-19 13:06:22 -0700490 Description: "turbine",
491 Output: outputFile,
492 Inputs: srcFiles,
493 Implicits: deps,
Kousik Kumar1372c1b2020-05-20 07:55:56 -0700494 Args: args,
Nan Zhanged19fc32017-10-19 13:06:22 -0700495 })
496}
497
Colin Crossf61766e2022-03-16 18:06:48 -0700498// TurbineApt produces a rule to run annotation processors using turbine.
Isaac Chioua23d9942022-04-06 06:14:38 +0000499func TurbineApt(ctx android.ModuleContext, outputSrcJar, outputResJar android.WritablePath,
Colin Crossf61766e2022-03-16 18:06:48 -0700500 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
501
Colin Crossc52d5232024-01-10 16:43:33 -0800502 turbineFlags, deps := turbineFlags(ctx, flags, "kapt")
Colin Crossf61766e2022-03-16 18:06:48 -0700503
504 deps = append(deps, srcJars...)
505
506 deps = append(deps, flags.processorPath...)
507 turbineFlags += " " + flags.processorPath.FormTurbineClassPath("--processorpath ")
508 turbineFlags += " --processors " + strings.Join(flags.processors, " ")
509
Isaac Chioua23d9942022-04-06 06:14:38 +0000510 outputs := android.WritablePaths{outputSrcJar, outputResJar}
511 outputFlags := "--gensrc_output " + outputSrcJar.String() + ".tmp " +
Colin Crossf61766e2022-03-16 18:06:48 -0700512 "--resource_output " + outputResJar.String() + ".tmp"
513
514 rule := turbine
515 args := map[string]string{
516 "javacFlags": flags.javacFlags,
517 "srcJars": strings.Join(srcJars.Strings(), " "),
518 "javaVersion": flags.javaVersion.String(),
519 "turbineFlags": turbineFlags,
520 "outputFlags": outputFlags,
521 "outputs": strings.Join(outputs.Strings(), " "),
522 }
523 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_TURBINE") {
524 rule = turbineRE
525 args["implicits"] = strings.Join(deps.Strings(), ",")
Isaac Chioua23d9942022-04-06 06:14:38 +0000526 args["rbeOutputs"] = outputSrcJar.String() + ".tmp," + outputResJar.String() + ".tmp"
Colin Crossf61766e2022-03-16 18:06:48 -0700527 }
528 ctx.Build(pctx, android.BuildParams{
529 Rule: rule,
530 Description: "turbine apt",
531 Output: outputs[0],
532 ImplicitOutputs: outputs[1:],
533 Inputs: srcFiles,
534 Implicits: deps,
535 Args: args,
536 })
537}
538
Colin Cross070879e2017-10-11 11:21:07 -0700539// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700540// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
541// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700542//
543// This method may be used for different compilers, including javac and Error Prone. The rule
544// argument specifies which command line to use and desc sets the description of the rule that will
545// be printed at build time. The stem argument provides the file name of the output jar, and
546// suffix will be appended to various intermediate files and directories to avoid collisions when
547// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700548func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Vadim Spivak3c496f02023-06-08 06:14:59 +0000549 shardIdx int, srcFiles, srcJars android.Paths, annoSrcJar android.WritablePath,
Colin Crosse9a275b2017-10-16 17:09:48 -0700550 flags javaBuilderFlags, deps android.Paths,
Colin Cross66548102018-06-19 22:47:35 -0700551 intermediatesDir, desc string) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700552
Colin Cross59149b62017-10-16 18:07:29 -0700553 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700554
Colin Crossc52d5232024-01-10 16:43:33 -0800555 javacClasspath := flags.classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700556
Colin Cross1369cdb2017-09-29 17:58:17 -0700557 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700558 if flags.javaVersion.usesJavaModules() {
Colin Crossb77043e2019-07-16 13:57:13 -0700559 var systemModuleDeps android.Paths
560 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
561 deps = append(deps, systemModuleDeps...)
Colin Crossc52d5232024-01-10 16:43:33 -0800562 javacClasspath = append(flags.java9Classpath, javacClasspath...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700563 } else {
564 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700565 if len(flags.bootClasspath) == 0 && ctx.Device() {
566 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
567 // ensure java does not fall back to the default bootclasspath.
568 bootClasspath = `-bootclasspath ""`
569 } else {
570 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
571 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700572 }
573
Colin Crossc52d5232024-01-10 16:43:33 -0800574 classpathArg := javacClasspath.FormJavaClassPath("-classpath")
575
576 // Keep the command line under the MAX_ARG_STRLEN limit by putting the classpath argument into an rsp file
577 // if it is too long.
578 const classpathLimit = 64 * 1024
579 if len(classpathArg) > classpathLimit {
580 classpathRspFile := outputFile.ReplaceExtension(ctx, "classpath")
581 android.WriteFileRule(ctx, classpathRspFile, classpathArg)
582 deps = append(deps, classpathRspFile)
583 classpathArg = "@" + classpathRspFile.String()
584 }
585
586 deps = append(deps, javacClasspath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700587 deps = append(deps, flags.processorPath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700588
Colin Cross7788c122019-01-23 16:14:02 -0800589 processor := "-proc:none"
Colin Cross5a116862020-04-22 11:44:34 -0700590 if len(flags.processors) > 0 {
591 processor = "-processor " + strings.Join(flags.processors, ",")
Colin Crossbe9cdb82019-01-21 21:37:16 -0800592 }
593
Nan Zhang61eaedb2017-11-02 13:28:15 -0700594 srcJarDir := "srcjars"
595 outDir := "classes"
596 annoDir := "anno"
597 if shardIdx >= 0 {
598 shardDir := "shard" + strconv.Itoa(shardIdx)
599 srcJarDir = filepath.Join(shardDir, srcJarDir)
600 outDir = filepath.Join(shardDir, outDir)
601 annoDir = filepath.Join(shardDir, annoDir)
602 }
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400603 rule := javac
Ramy Medhat16f23a42020-09-03 01:29:49 -0400604 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAVAC") {
Ramy Medhat1dcc27e2020-04-21 21:36:23 -0400605 rule = javacRE
606 }
Colin Crossae887032017-10-23 17:16:14 -0700607 ctx.Build(pctx, android.BuildParams{
Vadim Spivak3c496f02023-06-08 06:14:59 +0000608 Rule: rule,
609 Description: desc,
610 Output: outputFile,
611 ImplicitOutput: annoSrcJar,
612 Inputs: srcFiles,
613 Implicits: deps,
Anna Bauzad0476e42024-04-11 09:58:46 +0000614 Args: map[string]string{
615 "javacFlags": flags.javacFlags,
616 "bootClasspath": bootClasspath,
617 "classpath": classpathArg,
618 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
619 "processor": processor,
620 "srcJars": strings.Join(srcJars.Strings(), " "),
621 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
622 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
623 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
624 "annoSrcJar": annoSrcJar.String(),
625 "javaVersion": flags.javaVersion.String(),
626 },
Colin Crossc6bbef32017-08-14 14:16:06 -0700627 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700628}
629
Colin Crosse9a275b2017-10-16 17:09:48 -0700630func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
631 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700632
Kousik Kumar366afc52020-05-20 11:27:16 -0700633 rule := jar
Ramy Medhat16f23a42020-09-03 01:29:49 -0400634 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAR") {
Kousik Kumar366afc52020-05-20 11:27:16 -0700635 rule = jarRE
636 }
Colin Crossae887032017-10-23 17:16:14 -0700637 ctx.Build(pctx, android.BuildParams{
Kousik Kumar366afc52020-05-20 11:27:16 -0700638 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700639 Description: "jar",
640 Output: outputFile,
641 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700642 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -0800643 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700644 },
645 })
Colin Cross2fe66872015-03-30 17:20:39 -0700646}
647
Nan Zhanged19fc32017-10-19 13:06:22 -0700648func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
Colin Cross37f6d792018-07-12 12:28:41 -0700649 jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
650 dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700651
Colin Cross635acc92017-09-12 22:50:46 -0700652 var deps android.Paths
653
654 var jarArgs []string
655 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700656 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700657 deps = append(deps, manifest.Path())
658 }
659
Colin Cross37f6d792018-07-12 12:28:41 -0700660 for _, dir := range dirsToStrip {
661 jarArgs = append(jarArgs, "-stripDir ", dir)
662 }
663
664 for _, file := range filesToStrip {
665 jarArgs = append(jarArgs, "-stripFile ", file)
Nan Zhanged19fc32017-10-19 13:06:22 -0700666 }
667
Colin Cross7b60cdd2017-12-21 13:52:58 -0800668 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
669 // for downstream tools like desugar.
670 jarArgs = append(jarArgs, "-stripFile module-info.class")
671
Colin Cross37f6d792018-07-12 12:28:41 -0700672 if stripDirEntries {
Colin Cross635acc92017-09-12 22:50:46 -0700673 jarArgs = append(jarArgs, "-D")
674 }
675
Colin Crossc52d5232024-01-10 16:43:33 -0800676 rule := combineJar
677 // Keep the command line under the MAX_ARG_STRLEN limit by putting the list of jars into an rsp file
678 // if it is too long.
679 const jarsLengthLimit = 64 * 1024
680 jarsLength := 0
681 for i, jar := range jars {
682 if i != 0 {
683 jarsLength += 1
684 }
685 jarsLength += len(jar.String())
686 }
687 if jarsLength > jarsLengthLimit {
688 rule = combineJarRsp
689 }
690
Colin Crossae887032017-10-23 17:16:14 -0700691 ctx.Build(pctx, android.BuildParams{
Colin Crossc52d5232024-01-10 16:43:33 -0800692 Rule: rule,
Nan Zhanged19fc32017-10-19 13:06:22 -0700693 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700694 Output: outputFile,
695 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700696 Implicits: deps,
697 Args: map[string]string{
698 "jarArgs": strings.Join(jarArgs, " "),
699 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700700 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700701}
702
Colin Crossf06d8dc2023-07-18 22:11:07 -0700703func convertImplementationJarToHeaderJar(ctx android.ModuleContext, implementationJarFile android.Path,
704 headerJarFile android.WritablePath) {
705 ctx.Build(pctx, android.BuildParams{
706 Rule: convertImplementationJarToHeaderJarRule,
707 Input: implementationJarFile,
708 Output: headerJarFile,
709 })
710}
711
Colin Crosse9a275b2017-10-16 17:09:48 -0700712func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
713 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700714 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700715 Rule: jarjar,
716 Description: "jarjar",
717 Output: outputFile,
718 Input: classesJar,
719 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700720 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700721 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700722 },
723 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700724}
Colin Cross6ade34f2017-09-15 13:00:47 -0700725
Colin Crossb549b772020-06-03 17:14:31 +0000726func CheckJarPackages(ctx android.ModuleContext, outputFile android.WritablePath,
727 classesJar android.Path, permittedPackages []string) {
Vladimir Marko0975ee02019-04-02 10:29:55 +0100728 ctx.Build(pctx, android.BuildParams{
729 Rule: packageCheck,
730 Description: "packageCheck",
731 Output: outputFile,
732 Input: classesJar,
733 Args: map[string]string{
734 "packages": strings.Join(permittedPackages, " "),
735 },
736 })
737}
738
Nan Zhang4c819fb2018-08-27 18:31:46 -0700739func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath,
740 inputFile android.Path) {
741 ctx.Build(pctx, android.BuildParams{
742 Rule: jetifier,
743 Description: "jetifier",
744 Output: outputFile,
745 Input: inputFile,
746 })
747}
748
Colin Cross094054a2018-10-17 15:10:48 -0700749func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) {
Colin Crosscf371cc2020-11-13 11:48:42 -0800750 android.WriteFileRule(ctx, outputFile, "Main-Class: "+mainClass+"\n")
Colin Cross094054a2018-10-17 15:10:48 -0700751}
752
Cole Faust61585282023-07-14 16:23:39 -0700753func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path, validations android.Paths) {
Colin Cross43f08db2018-11-12 10:13:39 -0800754 ctx.Build(pctx, android.BuildParams{
755 Rule: zipalign,
756 Description: "align",
757 Input: inputFile,
758 Output: outputFile,
Cole Faust61585282023-07-14 16:23:39 -0700759 Validations: validations,
Colin Cross43f08db2018-11-12 10:13:39 -0800760 })
761}
762
Colin Cross312634e2023-11-21 15:13:56 -0800763func writeCombinedProguardFlagsFile(ctx android.ModuleContext, outputFile android.WritablePath, files android.Paths) {
764 ctx.Build(pctx, android.BuildParams{
765 Rule: writeCombinedProguardFlagsFileRule,
766 Description: "write combined proguard flags file",
767 Inputs: files,
768 Output: outputFile,
769 })
770}
771
Colin Cross33961b52019-07-11 11:01:22 -0700772type classpath android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700773
Colin Crossc2557d12019-10-31 15:22:57 -0700774func (x *classpath) formJoinedClassPath(optName string, sep string) string {
Colin Cross81440082018-08-15 20:21:55 -0700775 if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") {
776 optName += " "
777 }
Colin Cross59149b62017-10-16 18:07:29 -0700778 if len(*x) > 0 {
Colin Crossc2557d12019-10-31 15:22:57 -0700779 return optName + strings.Join(x.Strings(), sep)
Colin Cross6ade34f2017-09-15 13:00:47 -0700780 } else {
781 return ""
782 }
783}
Colin Crossc2557d12019-10-31 15:22:57 -0700784func (x *classpath) FormJavaClassPath(optName string) string {
785 return x.formJoinedClassPath(optName, ":")
786}
Colin Cross6ade34f2017-09-15 13:00:47 -0700787
Colin Crossc2557d12019-10-31 15:22:57 -0700788func (x *classpath) FormTurbineClassPath(optName string) string {
789 return x.formJoinedClassPath(optName, " ")
790}
791
792// FormRepeatedClassPath returns a list of arguments with the given optName prefixed to each element of the classpath.
793func (x *classpath) FormRepeatedClassPath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700794 if x == nil || *x == nil {
795 return nil
796 }
797 flags := make([]string, len(*x))
798 for i, v := range *x {
Colin Crossafbb1732019-01-17 15:42:52 -0800799 flags[i] = optName + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700800 }
801
802 return flags
803}
804
Colin Cross6ade34f2017-09-15 13:00:47 -0700805// Convert a classpath to an android.Paths
806func (x *classpath) Paths() android.Paths {
807 return append(android.Paths(nil), (*x)...)
808}
809
810func (x *classpath) Strings() []string {
811 if x == nil {
812 return nil
813 }
814 ret := make([]string, len(*x))
815 for i, path := range *x {
816 ret[i] = path.String()
817 }
818 return ret
819}
Colin Crossb77043e2019-07-16 13:57:13 -0700820
821type systemModules struct {
822 dir android.Path
823 deps android.Paths
824}
825
Colin Crossbf3119e2019-10-28 14:25:10 -0700826// Returns a --system argument in the form javac expects with -source 1.9 and the list of files to
827// depend on. If forceEmpty is true, returns --system=none if the list is empty to ensure javac
828// does not fall back to the default system modules.
Colin Crossb77043e2019-07-16 13:57:13 -0700829func (x *systemModules) FormJavaSystemModulesPath(forceEmpty bool) (string, android.Paths) {
830 if x != nil {
831 return "--system=" + x.dir.String(), x.deps
832 } else if forceEmpty {
833 return "--system=none", nil
834 } else {
835 return "", nil
836 }
837}
Colin Crossbf3119e2019-10-28 14:25:10 -0700838
839// Returns a --system argument in the form turbine expects with -source 1.9 and the list of files to
840// depend on. If forceEmpty is true, returns --bootclasspath "" if the list is empty to ensure turbine
841// does not fall back to the default bootclasspath.
842func (x *systemModules) FormTurbineSystemModulesPath(forceEmpty bool) (string, android.Paths) {
843 if x != nil {
844 return "--system " + x.dir.String(), x.deps
845 } else if forceEmpty {
846 return `--bootclasspath ""`, nil
847 } else {
Colin Crossf61766e2022-03-16 18:06:48 -0700848 return "--system ${config.JavaHome}", nil
Colin Crossbf3119e2019-10-28 14:25:10 -0700849 }
850}