blob: 417a7fa54be5722f3aa301d383a0022cfcd1cf06 [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"
Colin Cross2fe66872015-03-30 17:20:39 -070030)
31
32var (
Colin Cross635c3b02016-05-18 15:37:25 -070033 pctx = android.NewPackageContext("android/soong/java")
Colin Cross2fe66872015-03-30 17:20:39 -070034
35 // Compiling java is not conducive to proper dependency tracking. The path-matches-class-name
36 // requirement leads to unpredictable generated source file names, and a single .java file
37 // will get compiled into multiple .class files if it contains inner classes. To work around
Colin Crossf7eac7a2018-02-08 12:48:39 -080038 // this, all java rules write into separate directories and then are combined into a .jar file
39 // (if the rule produces .class files) or a .srcjar file (if the rule produces .java files).
40 // .srcjar files are unzipped into a temporary directory when compiled with javac.
Ramy Medhatdd0418a2019-11-04 18:16:11 -050041 javac = pctx.AndroidRemoteStaticRule("javac", android.SUPPORTS_GOMA,
Colin Cross2fe66872015-03-30 17:20:39 -070042 blueprint.RuleParams{
Colin Cross8eadbf02017-10-24 17:46:00 -070043 Command: `rm -rf "$outDir" "$annoDir" "$srcJarDir" && mkdir -p "$outDir" "$annoDir" "$srcJarDir" && ` +
Colin Cross436b7652018-03-15 16:24:10 -070044 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090045 `(if [ -s $srcJarDir/list ] || [ -s $out.rsp ] ; then ` +
Sasha Smundak26c6d9e2019-06-11 13:30:13 -070046 `${config.SoongJavacWrapper} ${config.JavacWrapper}${config.JavacCmd} ` +
47 `${config.JavacHeapFlags} ${config.JavacVmFlags} ${config.CommonJdkFlags} ` +
Colin Crossbe9cdb82019-01-21 21:37:16 -080048 `$processorpath $processor $javacFlags $bootClasspath $classpath ` +
Colin Cross64162712017-08-08 13:17:59 -070049 `-source $javaVersion -target $javaVersion ` +
Sundong Ahn24a099c2018-06-28 14:53:20 +090050 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list ; fi ) && ` +
Colin Cross44c29a82019-01-24 16:36:57 -080051 `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir && ` +
52 `rm -rf "$srcJarDir"`,
Colin Cross8eadbf02017-10-24 17:46:00 -070053 CommandDeps: []string{
54 "${config.JavacCmd}",
55 "${config.SoongZipCmd}",
Colin Cross436b7652018-03-15 16:24:10 -070056 "${config.ZipSyncCmd}",
Colin Cross8eadbf02017-10-24 17:46:00 -070057 },
Colin Crossa4820652017-10-17 13:56:52 -070058 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
59 Rspfile: "$out.rsp",
60 RspfileContent: "$in",
Colin Cross2fe66872015-03-30 17:20:39 -070061 },
Colin Crossbe9cdb82019-01-21 21:37:16 -080062 "javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
Colin Cross8eadbf02017-10-24 17:46:00 -070063 "outDir", "annoDir", "javaVersion")
Colin Cross2fe66872015-03-30 17:20:39 -070064
Sasha Smundak2a4549e2018-11-05 16:49:08 -080065 _ = pctx.VariableFunc("kytheCorpus",
66 func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() })
Sasha Smundak65143642019-09-26 20:14:28 -070067 _ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json")
Sasha Smundak2a4549e2018-11-05 16:49:08 -080068 // Run it with -add-opens=java.base/java.nio=ALL-UNNAMED to avoid JDK9's warning about
69 // "Illegal reflective access by com.google.protobuf.Utf8$UnsafeProcessor ...
70 // to field java.nio.Buffer.address"
71 kytheExtract = pctx.AndroidStaticRule("kythe",
72 blueprint.RuleParams{
73 Command: `${config.ZipSyncCmd} -d $srcJarDir ` +
74 `-l $srcJarDir/list -f "*.java" $srcJars && ` +
75 `( [ ! -s $srcJarDir/list -a ! -s $out.rsp ] || ` +
76 `KYTHE_ROOT_DIRECTORY=. KYTHE_OUTPUT_FILE=$out ` +
77 `KYTHE_CORPUS=${kytheCorpus} ` +
Sasha Smundak65143642019-09-26 20:14:28 -070078 `KYTHE_VNAMES=${kytheVnames} ` +
Sasha Smundak2a4549e2018-11-05 16:49:08 -080079 `${config.SoongJavacWrapper} ${config.JavaCmd} ` +
80 `--add-opens=java.base/java.nio=ALL-UNNAMED ` +
81 `-jar ${config.JavaKytheExtractorJar} ` +
82 `${config.JavacHeapFlags} ${config.CommonJdkFlags} ` +
83 `$processorpath $processor $javacFlags $bootClasspath $classpath ` +
84 `-source $javaVersion -target $javaVersion ` +
85 `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list)`,
86 CommandDeps: []string{
87 "${config.JavaCmd}",
88 "${config.JavaKytheExtractorJar}",
Sasha Smundak65143642019-09-26 20:14:28 -070089 "${kytheVnames}",
Sasha Smundak2a4549e2018-11-05 16:49:08 -080090 "${config.ZipSyncCmd}",
91 },
92 CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
93 Rspfile: "$out.rsp",
94 RspfileContent: "$in",
95 },
96 "javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
97 "outDir", "annoDir", "javaVersion")
98
Nan Zhanged19fc32017-10-19 13:06:22 -070099 turbine = pctx.AndroidStaticRule("turbine",
100 blueprint.RuleParams{
Colin Cross6981f652018-03-07 15:14:50 -0800101 Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Sasha Smundak26c6d9e2019-06-11 13:30:13 -0700102 `${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.TurbineJar} --output $out.tmp ` +
Colin Cross6981f652018-03-07 15:14:50 -0800103 `--temp_dir "$outDir" --sources @$out.rsp --source_jars $srcJars ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700104 `--javacopts ${config.CommonJdkFlags} ` +
Colin Cross924a0aa2018-03-07 10:51:05 -0800105 `$javacFlags -source $javaVersion -target $javaVersion -- $bootClasspath $classpath && ` +
Nan Zhanged19fc32017-10-19 13:06:22 -0700106 `${config.Ziptime} $out.tmp && ` +
107 `(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`,
Colin Cross8eadbf02017-10-24 17:46:00 -0700108 CommandDeps: []string{
109 "${config.TurbineJar}",
110 "${config.JavaCmd}",
111 "${config.Ziptime}",
Colin Cross8eadbf02017-10-24 17:46:00 -0700112 },
Nan Zhanged19fc32017-10-19 13:06:22 -0700113 Rspfile: "$out.rsp",
114 RspfileContent: "$in",
115 Restat: true,
116 },
Colin Cross6981f652018-03-07 15:14:50 -0800117 "javacFlags", "bootClasspath", "classpath", "srcJars", "outDir", "javaVersion")
Nan Zhanged19fc32017-10-19 13:06:22 -0700118
Colin Cross9d45bb72016-08-29 16:14:13 -0700119 jar = pctx.AndroidStaticRule("jar",
Colin Cross2fe66872015-03-30 17:20:39 -0700120 blueprint.RuleParams{
Nan Zhang674dd932018-01-26 18:30:36 -0800121 Command: `${config.SoongZipCmd} -jar -o $out @$out.rsp`,
122 CommandDeps: []string{"${config.SoongZipCmd}"},
123 Rspfile: "$out.rsp",
124 RspfileContent: "$jarArgs",
Colin Cross2fe66872015-03-30 17:20:39 -0700125 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700126 "jarArgs")
127
Colin Crossa4f08812018-10-02 22:03:40 -0700128 zip = pctx.AndroidStaticRule("zip",
129 blueprint.RuleParams{
130 Command: `${config.SoongZipCmd} -o $out @$out.rsp`,
131 CommandDeps: []string{"${config.SoongZipCmd}"},
132 Rspfile: "$out.rsp",
133 RspfileContent: "$jarArgs",
134 },
135 "jarArgs")
136
Colin Cross0a6e0072017-08-30 14:24:55 -0700137 combineJar = pctx.AndroidStaticRule("combineJar",
138 blueprint.RuleParams{
Colin Crossf91a08c2018-02-07 15:41:31 -0800139 Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`,
Colin Cross0a6e0072017-08-30 14:24:55 -0700140 CommandDeps: []string{"${config.MergeZipsCmd}"},
141 },
Colin Cross635acc92017-09-12 22:50:46 -0700142 "jarArgs")
Colin Cross2fe66872015-03-30 17:20:39 -0700143
Colin Cross9d45bb72016-08-29 16:14:13 -0700144 jarjar = pctx.AndroidStaticRule("jarjar",
Colin Cross65bf4f22015-04-03 16:54:17 -0700145 blueprint.RuleParams{
Sasha Smundak26c6d9e2019-06-11 13:30:13 -0700146 Command: "${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JarjarCmd} process $rulesFile $in $out",
Colin Cross64162712017-08-08 13:17:59 -0700147 CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"},
Colin Cross65bf4f22015-04-03 16:54:17 -0700148 },
149 "rulesFile")
Nan Zhang4c819fb2018-08-27 18:31:46 -0700150
Vladimir Marko0975ee02019-04-02 10:29:55 +0100151 packageCheck = pctx.AndroidStaticRule("packageCheck",
152 blueprint.RuleParams{
153 Command: "rm -f $out && " +
154 "${config.PackageCheckCmd} $in $packages && " +
155 "touch $out",
156 CommandDeps: []string{"${config.PackageCheckCmd}"},
157 },
158 "packages")
159
Nan Zhang4c819fb2018-08-27 18:31:46 -0700160 jetifier = pctx.AndroidStaticRule("jetifier",
161 blueprint.RuleParams{
Sasha Smundak26c6d9e2019-06-11 13:30:13 -0700162 Command: "${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JetifierJar} -l error -o $out -i $in",
Nan Zhang4c819fb2018-08-27 18:31:46 -0700163 CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"},
164 },
165 )
Colin Cross43f08db2018-11-12 10:13:39 -0800166
167 zipalign = pctx.AndroidStaticRule("zipalign",
168 blueprint.RuleParams{
Colin Crosse4246ab2019-02-05 21:55:21 -0800169 Command: "if ! ${config.ZipAlign} -c -p 4 $in > /dev/null; then " +
170 "${config.ZipAlign} -f -p 4 $in $out; " +
Colin Cross43f08db2018-11-12 10:13:39 -0800171 "else " +
172 "cp -f $in $out; " +
173 "fi",
174 CommandDeps: []string{"${config.ZipAlign}"},
175 },
176 )
Colin Cross2fe66872015-03-30 17:20:39 -0700177)
178
179func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -0700180 pctx.Import("android/soong/android")
Colin Cross64162712017-08-08 13:17:59 -0700181 pctx.Import("android/soong/java/config")
Colin Cross2fe66872015-03-30 17:20:39 -0700182}
183
184type javaBuilderFlags struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700185 javacFlags string
186 bootClasspath classpath
187 classpath classpath
188 java9Classpath classpath
189 processorPath classpath
190 processor string
191 systemModules *systemModules
192 aidlFlags string
193 aidlDeps android.Paths
194 javaVersion javaVersion
Colin Cross6af17aa2017-09-20 12:59:05 -0700195
Andreas Gampef3e5b552018-01-22 21:27:21 -0800196 errorProneExtraJavacFlags string
Colin Cross66548102018-06-19 22:47:35 -0700197 errorProneProcessorPath classpath
Andreas Gampef3e5b552018-01-22 21:27:21 -0800198
Colin Cross93e85952017-08-15 13:34:18 -0700199 kotlincFlags string
200 kotlincClasspath classpath
201
Colin Cross19878da2019-03-28 14:45:07 -0700202 proto android.ProtoFlags
Colin Cross2fe66872015-03-30 17:20:39 -0700203}
204
Nan Zhang61eaedb2017-11-02 13:28:15 -0700205func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int,
206 srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700207
Nan Zhang61eaedb2017-11-02 13:28:15 -0700208 // Compile java sources into .class files
209 desc := "javac"
210 if shardIdx >= 0 {
211 desc += strconv.Itoa(shardIdx)
212 }
213
Colin Cross66548102018-06-19 22:47:35 -0700214 transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc)
Colin Cross2fe66872015-03-30 17:20:39 -0700215}
216
Colin Crosse9a275b2017-10-16 17:09:48 -0700217func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700218 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700219
Colin Cross66548102018-06-19 22:47:35 -0700220 flags.processorPath = append(flags.errorProneProcessorPath, flags.processorPath...)
Colin Crossfee57cb2017-09-05 13:16:45 -0700221
Andreas Gampef3e5b552018-01-22 21:27:21 -0800222 if len(flags.errorProneExtraJavacFlags) > 0 {
223 if len(flags.javacFlags) > 0 {
Colin Cross66548102018-06-19 22:47:35 -0700224 flags.javacFlags += " " + flags.errorProneExtraJavacFlags
Andreas Gampef3e5b552018-01-22 21:27:21 -0800225 } else {
226 flags.javacFlags = flags.errorProneExtraJavacFlags
227 }
228 }
229
Nan Zhang61eaedb2017-11-02 13:28:15 -0700230 transformJavaToClasses(ctx, outputFile, -1, srcFiles, srcJars, flags, nil,
Colin Cross66548102018-06-19 22:47:35 -0700231 "errorprone", "errorprone")
Colin Cross070879e2017-10-11 11:21:07 -0700232}
233
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800234// Emits the rule to generate Xref input file (.kzip file) for the given set of source files and source jars
235// to compile with given set of builder flags, etc.
Colin Cross3b706fd2019-09-05 16:44:18 -0700236func emitXrefRule(ctx android.ModuleContext, xrefFile android.WritablePath, idx int,
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800237 srcFiles, srcJars android.Paths,
Colin Cross3b706fd2019-09-05 16:44:18 -0700238 flags javaBuilderFlags, deps android.Paths) {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800239
240 deps = append(deps, srcJars...)
Sasha Smundak09950a42019-11-04 16:29:45 -0800241 classpath := flags.classpath
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800242
243 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700244 if flags.javaVersion.usesJavaModules() {
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800245 var systemModuleDeps android.Paths
246 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
247 deps = append(deps, systemModuleDeps...)
Sasha Smundak09950a42019-11-04 16:29:45 -0800248 classpath = append(flags.java9Classpath, classpath...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800249 } else {
250 deps = append(deps, flags.bootClasspath...)
251 if len(flags.bootClasspath) == 0 && ctx.Device() {
252 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
253 // ensure java does not fall back to the default bootclasspath.
254 bootClasspath = `-bootclasspath ""`
255 } else {
256 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
257 }
258 }
259
Sasha Smundak09950a42019-11-04 16:29:45 -0800260 deps = append(deps, classpath...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800261 deps = append(deps, flags.processorPath...)
262
263 processor := "-proc:none"
264 if flags.processor != "" {
265 processor = "-processor " + flags.processor
266 }
267
Colin Cross3b706fd2019-09-05 16:44:18 -0700268 intermediatesDir := "xref"
269 if idx >= 0 {
270 intermediatesDir += strconv.Itoa(idx)
271 }
272
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800273 ctx.Build(pctx,
274 android.BuildParams{
275 Rule: kytheExtract,
276 Description: "Xref Java extractor",
277 Output: xrefFile,
278 Inputs: srcFiles,
279 Implicits: deps,
280 Args: map[string]string{
281 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, "anno").String(),
282 "bootClasspath": bootClasspath,
Sasha Smundak09950a42019-11-04 16:29:45 -0800283 "classpath": classpath.FormJavaClassPath("-classpath"),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800284 "javacFlags": flags.javacFlags,
Colin Cross1e743852019-10-28 11:37:20 -0700285 "javaVersion": flags.javaVersion.String(),
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800286 "outDir": android.PathForModuleOut(ctx, "javac", "classes.xref").String(),
287 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
288 "processor": processor,
289 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, "srcjars.xref").String(),
290 "srcJars": strings.Join(srcJars.Strings(), " "),
291 },
292 })
293}
294
Nan Zhanged19fc32017-10-19 13:06:22 -0700295func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700296 srcFiles, srcJars android.Paths, flags javaBuilderFlags) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700297
298 var deps android.Paths
299 deps = append(deps, srcJars...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700300
Colin Cross6cef4812019-10-17 14:23:50 -0700301 classpath := flags.classpath
302
Nan Zhanged19fc32017-10-19 13:06:22 -0700303 var bootClasspath string
Colin Crossbf3119e2019-10-28 14:25:10 -0700304 if flags.javaVersion.usesJavaModules() {
305 var systemModuleDeps android.Paths
306 bootClasspath, systemModuleDeps = flags.systemModules.FormTurbineSystemModulesPath(ctx.Device())
307 deps = append(deps, systemModuleDeps...)
Colin Cross6cef4812019-10-17 14:23:50 -0700308 classpath = append(flags.java9Classpath, classpath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700309 } else {
Colin Crossbf3119e2019-10-28 14:25:10 -0700310 deps = append(deps, flags.bootClasspath...)
311 if len(flags.bootClasspath) == 0 && ctx.Device() {
312 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
313 // ensure turbine does not fall back to the default bootclasspath.
314 bootClasspath = `--bootclasspath ""`
315 } else {
Colin Crossc2557d12019-10-31 15:22:57 -0700316 bootClasspath = flags.bootClasspath.FormTurbineClassPath("--bootclasspath ")
Colin Crossbf3119e2019-10-28 14:25:10 -0700317 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700318 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700319
Colin Cross6cef4812019-10-17 14:23:50 -0700320 deps = append(deps, classpath...)
Colin Crossbf3119e2019-10-28 14:25:10 -0700321 deps = append(deps, flags.processorPath...)
322
Colin Crossae887032017-10-23 17:16:14 -0700323 ctx.Build(pctx, android.BuildParams{
Nan Zhanged19fc32017-10-19 13:06:22 -0700324 Rule: turbine,
325 Description: "turbine",
326 Output: outputFile,
327 Inputs: srcFiles,
328 Implicits: deps,
329 Args: map[string]string{
330 "javacFlags": flags.javacFlags,
331 "bootClasspath": bootClasspath,
Colin Cross8eadbf02017-10-24 17:46:00 -0700332 "srcJars": strings.Join(srcJars.Strings(), " "),
Colin Crossc2557d12019-10-31 15:22:57 -0700333 "classpath": classpath.FormTurbineClassPath("--classpath "),
Nan Zhanged19fc32017-10-19 13:06:22 -0700334 "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(),
Colin Cross1e743852019-10-28 11:37:20 -0700335 "javaVersion": flags.javaVersion.String(),
Nan Zhanged19fc32017-10-19 13:06:22 -0700336 },
337 })
338}
339
Colin Cross070879e2017-10-11 11:21:07 -0700340// transformJavaToClasses takes source files and converts them to a jar containing .class files.
Colin Cross59149b62017-10-16 18:07:29 -0700341// srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain
342// sources. flags contains various command line flags to be passed to the compiler.
Colin Cross070879e2017-10-11 11:21:07 -0700343//
344// This method may be used for different compilers, including javac and Error Prone. The rule
345// argument specifies which command line to use and desc sets the description of the rule that will
346// be printed at build time. The stem argument provides the file name of the output jar, and
347// suffix will be appended to various intermediate files and directories to avoid collisions when
348// this function is called twice in the same module directory.
Colin Crosse9a275b2017-10-16 17:09:48 -0700349func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath,
Nan Zhang61eaedb2017-11-02 13:28:15 -0700350 shardIdx int, srcFiles, srcJars android.Paths,
Colin Crosse9a275b2017-10-16 17:09:48 -0700351 flags javaBuilderFlags, deps android.Paths,
Colin Cross66548102018-06-19 22:47:35 -0700352 intermediatesDir, desc string) {
Colin Crossc6bbef32017-08-14 14:16:06 -0700353
Colin Cross59149b62017-10-16 18:07:29 -0700354 deps = append(deps, srcJars...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700355
Colin Cross6cef4812019-10-17 14:23:50 -0700356 classpath := flags.classpath
357
Colin Cross1369cdb2017-09-29 17:58:17 -0700358 var bootClasspath string
Colin Cross1e743852019-10-28 11:37:20 -0700359 if flags.javaVersion.usesJavaModules() {
Colin Crossb77043e2019-07-16 13:57:13 -0700360 var systemModuleDeps android.Paths
361 bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device())
362 deps = append(deps, systemModuleDeps...)
Colin Cross6cef4812019-10-17 14:23:50 -0700363 classpath = append(flags.java9Classpath, classpath...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700364 } else {
365 deps = append(deps, flags.bootClasspath...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700366 if len(flags.bootClasspath) == 0 && ctx.Device() {
367 // explicitly specify -bootclasspath "" if the bootclasspath is empty to
368 // ensure java does not fall back to the default bootclasspath.
369 bootClasspath = `-bootclasspath ""`
370 } else {
371 bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath")
372 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700373 }
374
Colin Cross6cef4812019-10-17 14:23:50 -0700375 deps = append(deps, classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700376 deps = append(deps, flags.processorPath...)
Colin Crossc6bbef32017-08-14 14:16:06 -0700377
Colin Cross7788c122019-01-23 16:14:02 -0800378 processor := "-proc:none"
Colin Crossbe9cdb82019-01-21 21:37:16 -0800379 if flags.processor != "" {
380 processor = "-processor " + flags.processor
381 }
382
Nan Zhang61eaedb2017-11-02 13:28:15 -0700383 srcJarDir := "srcjars"
384 outDir := "classes"
385 annoDir := "anno"
386 if shardIdx >= 0 {
387 shardDir := "shard" + strconv.Itoa(shardIdx)
388 srcJarDir = filepath.Join(shardDir, srcJarDir)
389 outDir = filepath.Join(shardDir, outDir)
390 annoDir = filepath.Join(shardDir, annoDir)
391 }
Colin Crossae887032017-10-23 17:16:14 -0700392 ctx.Build(pctx, android.BuildParams{
Colin Cross66548102018-06-19 22:47:35 -0700393 Rule: javac,
Colin Cross070879e2017-10-11 11:21:07 -0700394 Description: desc,
395 Output: outputFile,
Colin Crossc6bbef32017-08-14 14:16:06 -0700396 Inputs: srcFiles,
397 Implicits: deps,
398 Args: map[string]string{
Colin Cross59149b62017-10-16 18:07:29 -0700399 "javacFlags": flags.javacFlags,
Colin Cross1369cdb2017-09-29 17:58:17 -0700400 "bootClasspath": bootClasspath,
Colin Cross6cef4812019-10-17 14:23:50 -0700401 "classpath": classpath.FormJavaClassPath("-classpath"),
Colin Cross6a77c982018-06-19 22:43:34 -0700402 "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"),
Colin Crossbe9cdb82019-01-21 21:37:16 -0800403 "processor": processor,
Colin Cross8eadbf02017-10-24 17:46:00 -0700404 "srcJars": strings.Join(srcJars.Strings(), " "),
Nan Zhang61eaedb2017-11-02 13:28:15 -0700405 "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(),
406 "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(),
407 "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(),
Colin Cross1e743852019-10-28 11:37:20 -0700408 "javaVersion": flags.javaVersion.String(),
Colin Crossc6bbef32017-08-14 14:16:06 -0700409 },
410 })
Colin Crossc6bbef32017-08-14 14:16:06 -0700411}
412
Colin Crosse9a275b2017-10-16 17:09:48 -0700413func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath,
414 jarArgs []string, deps android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700415
Colin Crossae887032017-10-23 17:16:14 -0700416 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700417 Rule: jar,
418 Description: "jar",
419 Output: outputFile,
420 Implicits: deps,
Colin Cross2fe66872015-03-30 17:20:39 -0700421 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -0800422 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
Colin Cross2fe66872015-03-30 17:20:39 -0700423 },
424 })
Colin Cross2fe66872015-03-30 17:20:39 -0700425}
426
Nan Zhanged19fc32017-10-19 13:06:22 -0700427func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
Colin Cross37f6d792018-07-12 12:28:41 -0700428 jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
429 dirsToStrip []string) {
Colin Cross0a6e0072017-08-30 14:24:55 -0700430
Colin Cross635acc92017-09-12 22:50:46 -0700431 var deps android.Paths
432
433 var jarArgs []string
434 if manifest.Valid() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700435 jarArgs = append(jarArgs, "-m ", manifest.String())
Colin Cross635acc92017-09-12 22:50:46 -0700436 deps = append(deps, manifest.Path())
437 }
438
Colin Cross37f6d792018-07-12 12:28:41 -0700439 for _, dir := range dirsToStrip {
440 jarArgs = append(jarArgs, "-stripDir ", dir)
441 }
442
443 for _, file := range filesToStrip {
444 jarArgs = append(jarArgs, "-stripFile ", file)
Nan Zhanged19fc32017-10-19 13:06:22 -0700445 }
446
Colin Cross7b60cdd2017-12-21 13:52:58 -0800447 // Remove any module-info.class files that may have come from prebuilt jars, they cause problems
448 // for downstream tools like desugar.
449 jarArgs = append(jarArgs, "-stripFile module-info.class")
450
Colin Cross37f6d792018-07-12 12:28:41 -0700451 if stripDirEntries {
Colin Cross635acc92017-09-12 22:50:46 -0700452 jarArgs = append(jarArgs, "-D")
453 }
454
Colin Crossae887032017-10-23 17:16:14 -0700455 ctx.Build(pctx, android.BuildParams{
Colin Cross0a6e0072017-08-30 14:24:55 -0700456 Rule: combineJar,
Nan Zhanged19fc32017-10-19 13:06:22 -0700457 Description: desc,
Colin Cross0a6e0072017-08-30 14:24:55 -0700458 Output: outputFile,
459 Inputs: jars,
Colin Cross635acc92017-09-12 22:50:46 -0700460 Implicits: deps,
461 Args: map[string]string{
462 "jarArgs": strings.Join(jarArgs, " "),
463 },
Colin Cross0a6e0072017-08-30 14:24:55 -0700464 })
Colin Cross0a6e0072017-08-30 14:24:55 -0700465}
466
Colin Crosse9a275b2017-10-16 17:09:48 -0700467func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath,
468 classesJar android.Path, rulesFile android.Path) {
Colin Crossae887032017-10-23 17:16:14 -0700469 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700470 Rule: jarjar,
471 Description: "jarjar",
472 Output: outputFile,
473 Input: classesJar,
474 Implicit: rulesFile,
Colin Cross65bf4f22015-04-03 16:54:17 -0700475 Args: map[string]string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700476 "rulesFile": rulesFile.String(),
Colin Cross65bf4f22015-04-03 16:54:17 -0700477 },
478 })
Colin Cross65bf4f22015-04-03 16:54:17 -0700479}
Colin Cross6ade34f2017-09-15 13:00:47 -0700480
Vladimir Marko0975ee02019-04-02 10:29:55 +0100481func CheckJarPackages(ctx android.ModuleContext, outputFile android.WritablePath,
482 classesJar android.Path, permittedPackages []string) {
483 ctx.Build(pctx, android.BuildParams{
484 Rule: packageCheck,
485 Description: "packageCheck",
486 Output: outputFile,
487 Input: classesJar,
488 Args: map[string]string{
489 "packages": strings.Join(permittedPackages, " "),
490 },
491 })
492}
493
Nan Zhang4c819fb2018-08-27 18:31:46 -0700494func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath,
495 inputFile android.Path) {
496 ctx.Build(pctx, android.BuildParams{
497 Rule: jetifier,
498 Description: "jetifier",
499 Output: outputFile,
500 Input: inputFile,
501 })
502}
503
Colin Cross094054a2018-10-17 15:10:48 -0700504func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) {
505 ctx.Build(pctx, android.BuildParams{
506 Rule: android.WriteFile,
507 Description: "manifest",
508 Output: outputFile,
509 Args: map[string]string{
510 "content": "Main-Class: " + mainClass + "\n",
511 },
512 })
513}
514
Colin Cross43f08db2018-11-12 10:13:39 -0800515func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path) {
516 ctx.Build(pctx, android.BuildParams{
517 Rule: zipalign,
518 Description: "align",
519 Input: inputFile,
520 Output: outputFile,
521 })
522}
523
Colin Cross33961b52019-07-11 11:01:22 -0700524type classpath android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700525
Colin Crossc2557d12019-10-31 15:22:57 -0700526func (x *classpath) formJoinedClassPath(optName string, sep string) string {
Colin Cross81440082018-08-15 20:21:55 -0700527 if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") {
528 optName += " "
529 }
Colin Cross59149b62017-10-16 18:07:29 -0700530 if len(*x) > 0 {
Colin Crossc2557d12019-10-31 15:22:57 -0700531 return optName + strings.Join(x.Strings(), sep)
Colin Cross6ade34f2017-09-15 13:00:47 -0700532 } else {
533 return ""
534 }
535}
Colin Crossc2557d12019-10-31 15:22:57 -0700536func (x *classpath) FormJavaClassPath(optName string) string {
537 return x.formJoinedClassPath(optName, ":")
538}
Colin Cross6ade34f2017-09-15 13:00:47 -0700539
Colin Crossc2557d12019-10-31 15:22:57 -0700540func (x *classpath) FormTurbineClassPath(optName string) string {
541 return x.formJoinedClassPath(optName, " ")
542}
543
544// FormRepeatedClassPath returns a list of arguments with the given optName prefixed to each element of the classpath.
545func (x *classpath) FormRepeatedClassPath(optName string) []string {
Colin Cross6ade34f2017-09-15 13:00:47 -0700546 if x == nil || *x == nil {
547 return nil
548 }
549 flags := make([]string, len(*x))
550 for i, v := range *x {
Colin Crossafbb1732019-01-17 15:42:52 -0800551 flags[i] = optName + v.String()
Colin Cross6ade34f2017-09-15 13:00:47 -0700552 }
553
554 return flags
555}
556
Colin Cross6ade34f2017-09-15 13:00:47 -0700557// Convert a classpath to an android.Paths
558func (x *classpath) Paths() android.Paths {
559 return append(android.Paths(nil), (*x)...)
560}
561
562func (x *classpath) Strings() []string {
563 if x == nil {
564 return nil
565 }
566 ret := make([]string, len(*x))
567 for i, path := range *x {
568 ret[i] = path.String()
569 }
570 return ret
571}
Colin Crossb77043e2019-07-16 13:57:13 -0700572
573type systemModules struct {
574 dir android.Path
575 deps android.Paths
576}
577
Colin Crossbf3119e2019-10-28 14:25:10 -0700578// Returns a --system argument in the form javac expects with -source 1.9 and the list of files to
579// depend on. If forceEmpty is true, returns --system=none if the list is empty to ensure javac
580// does not fall back to the default system modules.
Colin Crossb77043e2019-07-16 13:57:13 -0700581func (x *systemModules) FormJavaSystemModulesPath(forceEmpty bool) (string, android.Paths) {
582 if x != nil {
583 return "--system=" + x.dir.String(), x.deps
584 } else if forceEmpty {
585 return "--system=none", nil
586 } else {
587 return "", nil
588 }
589}
Colin Crossbf3119e2019-10-28 14:25:10 -0700590
591// Returns a --system argument in the form turbine expects with -source 1.9 and the list of files to
592// depend on. If forceEmpty is true, returns --bootclasspath "" if the list is empty to ensure turbine
593// does not fall back to the default bootclasspath.
594func (x *systemModules) FormTurbineSystemModulesPath(forceEmpty bool) (string, android.Paths) {
595 if x != nil {
596 return "--system " + x.dir.String(), x.deps
597 } else if forceEmpty {
598 return `--bootclasspath ""`, nil
599 } else {
600 return "", nil
601 }
602}