Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package 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 | |
| 21 | import ( |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 22 | "path/filepath" |
| 23 | "strconv" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 24 | "strings" |
| 25 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 26 | "github.com/google/blueprint" |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 27 | "github.com/google/blueprint/proptools" |
Colin Cross | fee57cb | 2017-09-05 13:16:45 -0700 | [diff] [blame] | 28 | |
| 29 | "android/soong/android" |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 30 | "android/soong/remoteexec" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | var ( |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 34 | pctx = android.NewPackageContext("android/soong/java") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 35 | |
| 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 Cross | f7eac7a | 2018-02-08 12:48:39 -0800 | [diff] [blame] | 39 | // 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 Cross | 1ec3fce | 2020-03-05 12:43:14 -0800 | [diff] [blame] | 42 | // TODO(b/143658984): goma can't handle the --system argument to javac. |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 43 | javac, javacRE = pctx.MultiCommandRemoteStaticRules("javac", |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 44 | blueprint.RuleParams{ |
LaMont Jones | feff3f3 | 2023-06-14 20:43:14 +0000 | [diff] [blame] | 45 | Command: `rm -rf "$outDir" "$annoDir" "$annoSrcJar.tmp" "$srcJarDir" "$out.tmp" && ` + |
Vadim Spivak | 3c496f0 | 2023-06-08 06:14:59 +0000 | [diff] [blame] | 46 | `mkdir -p "$outDir" "$annoDir" "$srcJarDir" && ` + |
Colin Cross | 436b765 | 2018-03-15 16:24:10 -0700 | [diff] [blame] | 47 | `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` + |
Sundong Ahn | 24a099c | 2018-06-28 14:53:20 +0900 | [diff] [blame] | 48 | `(if [ -s $srcJarDir/list ] || [ -s $out.rsp ] ; then ` + |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 49 | `${config.SoongJavacWrapper} $javaTemplate${config.JavacCmd} ` + |
Sasha Smundak | 26c6d9e | 2019-06-11 13:30:13 -0700 | [diff] [blame] | 50 | `${config.JavacHeapFlags} ${config.JavacVmFlags} ${config.CommonJdkFlags} ` + |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 51 | `$processorpath $processor $javacFlags $bootClasspath $classpath ` + |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 52 | `-source $javaVersion -target $javaVersion ` + |
Sundong Ahn | 24a099c | 2018-06-28 14:53:20 +0900 | [diff] [blame] | 53 | `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list ; fi ) && ` + |
LaMont Jones | feff3f3 | 2023-06-14 20:43:14 +0000 | [diff] [blame] | 54 | `$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 Cross | 49889c0 | 2023-07-26 16:16:47 -0700 | [diff] [blame] | 58 | `rm -rf "$srcJarDir" "$outDir"`, |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 59 | CommandDeps: []string{ |
| 60 | "${config.JavacCmd}", |
| 61 | "${config.SoongZipCmd}", |
Colin Cross | 436b765 | 2018-03-15 16:24:10 -0700 | [diff] [blame] | 62 | "${config.ZipSyncCmd}", |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 63 | }, |
Colin Cross | a482065 | 2017-10-17 13:56:52 -0700 | [diff] [blame] | 64 | CommandOrderOnly: []string{"${config.SoongJavacWrapper}"}, |
LaMont Jones | feff3f3 | 2023-06-14 20:43:14 +0000 | [diff] [blame] | 65 | Restat: true, |
Colin Cross | a482065 | 2017-10-17 13:56:52 -0700 | [diff] [blame] | 66 | Rspfile: "$out.rsp", |
| 67 | RspfileContent: "$in", |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 68 | }, 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 Jones | feff3f3 | 2023-06-14 20:43:14 +0000 | [diff] [blame] | 77 | OutputFiles: []string{"$out.tmp"}, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 78 | ExecStrategy: "${config.REJavacExecStrategy}", |
| 79 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 80 | }, |
Vadim Spivak | 3c496f0 | 2023-06-08 06:14:59 +0000 | [diff] [blame] | 81 | "$annoSrcJarTemplate": &remoteexec.REParams{ |
| 82 | Labels: map[string]string{"type": "tool", "name": "soong_zip"}, |
| 83 | Inputs: []string{"${config.SoongZipCmd}", "$annoDir"}, |
LaMont Jones | feff3f3 | 2023-06-14 20:43:14 +0000 | [diff] [blame] | 84 | OutputFiles: []string{"$annoSrcJar.tmp"}, |
Vadim Spivak | 3c496f0 | 2023-06-08 06:14:59 +0000 | [diff] [blame] | 85 | ExecStrategy: "${config.REJavacExecStrategy}", |
| 86 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 87 | }, |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 88 | }, []string{"javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir", |
Anna Bauza | d0476e4 | 2024-04-11 09:58:46 +0000 | [diff] [blame] | 89 | "outDir", "annoDir", "annoSrcJar", "javaVersion"}, nil) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 90 | |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 91 | _ = pctx.VariableFunc("kytheCorpus", |
| 92 | func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() }) |
Sasha Smundak | 6c2d4f9 | 2020-01-09 17:34:23 -0800 | [diff] [blame] | 93 | _ = pctx.VariableFunc("kytheCuEncoding", |
| 94 | func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuEncoding() }) |
Sasha Smundak | b0addaf | 2021-02-16 10:39:40 -0800 | [diff] [blame] | 95 | _ = pctx.VariableFunc("kytheCuJavaSourceMax", |
| 96 | func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuJavaSourceMax() }) |
Sasha Smundak | 6514364 | 2019-09-26 20:14:28 -0700 | [diff] [blame] | 97 | _ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json") |
Sasha Smundak | 706d35f | 2022-10-23 15:23:55 -0700 | [diff] [blame] | 98 | // 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 Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 101 | 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 Smundak | 6514364 | 2019-09-26 20:14:28 -0700 | [diff] [blame] | 108 | `KYTHE_VNAMES=${kytheVnames} ` + |
Sasha Smundak | 6c2d4f9 | 2020-01-09 17:34:23 -0800 | [diff] [blame] | 109 | `KYTHE_KZIP_ENCODING=${kytheCuEncoding} ` + |
Sasha Smundak | b0addaf | 2021-02-16 10:39:40 -0800 | [diff] [blame] | 110 | `KYTHE_JAVA_SOURCE_BATCH_SIZE=${kytheCuJavaSourceMax} ` + |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 111 | `${config.SoongJavacWrapper} ${config.JavaCmd} ` + |
Sasha Smundak | 706d35f | 2022-10-23 15:23:55 -0700 | [diff] [blame] | 112 | // Avoid JDK9's warning about "Illegal reflective access by com.google.protobuf.Utf8$UnsafeProcessor ... |
| 113 | // to field java.nio.Buffer.address" |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 114 | `--add-opens=java.base/java.nio=ALL-UNNAMED ` + |
Sasha Smundak | 706d35f | 2022-10-23 15:23:55 -0700 | [diff] [blame] | 115 | // 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 Basca | 79b7ca1 | 2024-03-14 16:59:49 +0000 | [diff] [blame] | 123 | `--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED ` + |
| 124 | `--add-exports=jdk.internal.opt/jdk.internal.opt=ALL-UNNAMED ` + |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 125 | `-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 Smundak | 6514364 | 2019-09-26 20:14:28 -0700 | [diff] [blame] | 133 | "${kytheVnames}", |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 134 | "${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 Smundak | a7856c0 | 2020-04-23 09:49:59 -0700 | [diff] [blame] | 143 | extractMatchingApks = pctx.StaticRule( |
| 144 | "extractMatchingApks", |
| 145 | blueprint.RuleParams{ |
| 146 | Command: `rm -rf "$out" && ` + |
Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 147 | `${config.ExtractApksCmd} -o "${out}" -zip "${zip}" -allow-prereleased=${allow-prereleased} ` + |
Pranav Gupta | 51645ff | 2023-03-20 16:19:53 -0700 | [diff] [blame] | 148 | `-sdk-version=${sdk-version} -skip-sdk-check=${skip-sdk-check} -abis=${abis} ` + |
Sasha Smundak | a7856c0 | 2020-04-23 09:49:59 -0700 | [diff] [blame] | 149 | `--screen-densities=${screen-densities} --stem=${stem} ` + |
Jaewoong Jung | 11c1e0f | 2020-06-29 19:18:44 -0700 | [diff] [blame] | 150 | `-apkcerts=${apkcerts} -partition=${partition} ` + |
Sasha Smundak | a7856c0 | 2020-04-23 09:49:59 -0700 | [diff] [blame] | 151 | `${in}`, |
| 152 | CommandDeps: []string{"${config.ExtractApksCmd}"}, |
| 153 | }, |
Pranav Gupta | 51645ff | 2023-03-20 16:19:53 -0700 | [diff] [blame] | 154 | "abis", "allow-prereleased", "screen-densities", "sdk-version", "skip-sdk-check", "stem", "apkcerts", "partition", "zip") |
Sasha Smundak | a7856c0 | 2020-04-23 09:49:59 -0700 | [diff] [blame] | 155 | |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 156 | turbine, turbineRE = pctx.RemoteStaticRules("turbine", |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 157 | blueprint.RuleParams{ |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 158 | Command: `$reTemplate${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.TurbineJar} $outputFlags ` + |
Colin Cross | 411647e | 2022-03-16 14:28:36 -0700 | [diff] [blame] | 159 | `--sources @$out.rsp --source_jars $srcJars ` + |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 160 | `--javacopts ${config.CommonJdkFlags} ` + |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 161 | `$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 Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 163 | CommandDeps: []string{ |
| 164 | "${config.TurbineJar}", |
| 165 | "${config.JavaCmd}", |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 166 | }, |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 167 | Rspfile: "$out.rsp", |
| 168 | RspfileContent: "$in", |
| 169 | Restat: true, |
| 170 | }, |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 171 | &remoteexec.REParams{Labels: map[string]string{"type": "tool", "name": "turbine"}, |
Colin Cross | 411647e | 2022-03-16 14:28:36 -0700 | [diff] [blame] | 172 | ExecStrategy: "${config.RETurbineExecStrategy}", |
| 173 | Inputs: []string{"${config.TurbineJar}", "${out}.rsp", "$implicits"}, |
| 174 | RSPFiles: []string{"${out}.rsp"}, |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 175 | OutputFiles: []string{"$rbeOutputs"}, |
Colin Cross | 411647e | 2022-03-16 14:28:36 -0700 | [diff] [blame] | 176 | ToolchainInputs: []string{"${config.JavaCmd}"}, |
| 177 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 178 | }, |
| 179 | []string{"javacFlags", "turbineFlags", "outputFlags", "javaVersion", "outputs", "rbeOutputs", "srcJars"}, []string{"implicits"}) |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 180 | |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 181 | jar, jarRE = pctx.RemoteStaticRules("jar", |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 182 | blueprint.RuleParams{ |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 183 | Command: `$reTemplate${config.SoongZipCmd} -jar -o $out @$out.rsp`, |
Nan Zhang | 674dd93 | 2018-01-26 18:30:36 -0800 | [diff] [blame] | 184 | CommandDeps: []string{"${config.SoongZipCmd}"}, |
| 185 | Rspfile: "$out.rsp", |
| 186 | RspfileContent: "$jarArgs", |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 187 | }, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 188 | &remoteexec.REParams{ |
| 189 | ExecStrategy: "${config.REJarExecStrategy}", |
| 190 | Inputs: []string{"${config.SoongZipCmd}", "${out}.rsp"}, |
Colin Cross | a4eafdd | 2021-03-24 14:09:28 -0700 | [diff] [blame] | 191 | RSPFiles: []string{"${out}.rsp"}, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 192 | OutputFiles: []string{"$out"}, |
| 193 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 194 | }, []string{"jarArgs"}, nil) |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 195 | |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 196 | zip, zipRE = pctx.RemoteStaticRules("zip", |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 197 | blueprint.RuleParams{ |
| 198 | Command: `${config.SoongZipCmd} -o $out @$out.rsp`, |
| 199 | CommandDeps: []string{"${config.SoongZipCmd}"}, |
| 200 | Rspfile: "$out.rsp", |
| 201 | RspfileContent: "$jarArgs", |
| 202 | }, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 203 | &remoteexec.REParams{ |
| 204 | ExecStrategy: "${config.REZipExecStrategy}", |
| 205 | Inputs: []string{"${config.SoongZipCmd}", "${out}.rsp", "$implicits"}, |
Colin Cross | a4eafdd | 2021-03-24 14:09:28 -0700 | [diff] [blame] | 206 | RSPFiles: []string{"${out}.rsp"}, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 207 | OutputFiles: []string{"$out"}, |
| 208 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 209 | }, []string{"jarArgs"}, []string{"implicits"}) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 210 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 211 | combineJar = pctx.AndroidStaticRule("combineJar", |
| 212 | blueprint.RuleParams{ |
Colin Cross | f91a08c | 2018-02-07 15:41:31 -0800 | [diff] [blame] | 213 | Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`, |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 214 | CommandDeps: []string{"${config.MergeZipsCmd}"}, |
| 215 | }, |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 216 | "jarArgs") |
Colin Cross | c52d523 | 2024-01-10 16:43:33 -0800 | [diff] [blame] | 217 | 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 Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 225 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 226 | jarjar = pctx.AndroidStaticRule("jarjar", |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 227 | blueprint.RuleParams{ |
Colin Cross | d97cf63 | 2021-02-08 10:14:04 -0800 | [diff] [blame] | 228 | 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 Satayev | 762d9f3 | 2020-04-15 12:50:04 +0100 | [diff] [blame] | 234 | // 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 Cross | d97cf63 | 2021-02-08 10:14:04 -0800 | [diff] [blame] | 238 | " -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 Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 241 | CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"}, |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 242 | }, |
| 243 | "rulesFile") |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 244 | |
Vladimir Marko | 0975ee0 | 2019-04-02 10:29:55 +0100 | [diff] [blame] | 245 | packageCheck = pctx.AndroidStaticRule("packageCheck", |
| 246 | blueprint.RuleParams{ |
| 247 | Command: "rm -f $out && " + |
| 248 | "${config.PackageCheckCmd} $in $packages && " + |
Colin Cross | b549b77 | 2020-06-03 17:14:31 +0000 | [diff] [blame] | 249 | "touch $out", |
Vladimir Marko | 0975ee0 | 2019-04-02 10:29:55 +0100 | [diff] [blame] | 250 | CommandDeps: []string{"${config.PackageCheckCmd}"}, |
| 251 | }, |
| 252 | "packages") |
| 253 | |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 254 | jetifier = pctx.AndroidStaticRule("jetifier", |
| 255 | blueprint.RuleParams{ |
Colin Cross | db9f1af | 2022-12-13 15:57:01 -0800 | [diff] [blame] | 256 | Command: "${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JetifierJar} -l error -o $out -i $in -t epoch", |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 257 | CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"}, |
| 258 | }, |
| 259 | ) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 260 | |
| 261 | zipalign = pctx.AndroidStaticRule("zipalign", |
| 262 | blueprint.RuleParams{ |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 263 | Command: "if ! ${config.ZipAlign} -c -p 4 $in > /dev/null; then " + |
| 264 | "${config.ZipAlign} -f -p 4 $in $out; " + |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 265 | "else " + |
| 266 | "cp -f $in $out; " + |
| 267 | "fi", |
| 268 | CommandDeps: []string{"${config.ZipAlign}"}, |
| 269 | }, |
| 270 | ) |
Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 271 | |
Colin Cross | f06d8dc | 2023-07-18 22:11:07 -0700 | [diff] [blame] | 272 | 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 Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame] | 277 | |
| 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 Kang | 6592e87 | 2023-12-19 01:13:16 +0000 | [diff] [blame] | 287 | |
| 288 | gatherReleasedFlaggedApisRule = pctx.AndroidStaticRule("gatherReleasedFlaggedApisRule", |
| 289 | blueprint.RuleParams{ |
Yu Liu | 748ade2 | 2024-02-08 19:11:39 +0000 | [diff] [blame] | 290 | Command: `${aconfig} dump-cache --dedup --format='{fully_qualified_name}={state:bool}' ` + |
Jihoon Kang | 6592e87 | 2023-12-19 01:13:16 +0000 | [diff] [blame] | 291 | `--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 Kang | 150d87f | 2024-01-09 19:21:27 +0000 | [diff] [blame] | 300 | Command: `${keep-flagged-apis} ${in} > ${out}`, |
| 301 | CommandDeps: []string{"${keep-flagged-apis}"}, |
Jihoon Kang | 6592e87 | 2023-12-19 01:13:16 +0000 | [diff] [blame] | 302 | }) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 303 | ) |
| 304 | |
| 305 | func init() { |
Colin Cross | cc0ce80 | 2019-04-02 16:14:11 -0700 | [diff] [blame] | 306 | pctx.Import("android/soong/android") |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 307 | pctx.Import("android/soong/java/config") |
Jihoon Kang | 6592e87 | 2023-12-19 01:13:16 +0000 | [diff] [blame] | 308 | |
| 309 | pctx.HostBinToolVariable("aconfig", "aconfig") |
| 310 | pctx.HostBinToolVariable("keep-flagged-apis", "keep-flagged-apis") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | type javaBuilderFlags struct { |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 314 | 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 Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 333 | java9Classpath classpath |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 334 | |
| 335 | processorPath classpath |
| 336 | processors []string |
| 337 | systemModules *systemModules |
| 338 | aidlFlags string |
| 339 | aidlDeps android.Paths |
| 340 | javaVersion javaVersion |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 341 | |
Andreas Gampe | f3e5b55 | 2018-01-22 21:27:21 -0800 | [diff] [blame] | 342 | errorProneExtraJavacFlags string |
Colin Cross | 6654810 | 2018-06-19 22:47:35 -0700 | [diff] [blame] | 343 | errorProneProcessorPath classpath |
Andreas Gampe | f3e5b55 | 2018-01-22 21:27:21 -0800 | [diff] [blame] | 344 | |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 345 | kotlincFlags string |
| 346 | kotlincClasspath classpath |
Colin Cross | a1ff7c6 | 2021-09-17 14:11:52 -0700 | [diff] [blame] | 347 | kotlincDeps android.Paths |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 348 | |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 349 | proto android.ProtoFlags |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 352 | func DefaultJavaBuilderFlags() javaBuilderFlags { |
| 353 | return javaBuilderFlags{ |
| 354 | javaVersion: JAVA_VERSION_8, |
| 355 | } |
| 356 | } |
| 357 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 358 | func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int, |
Vadim Spivak | 3c496f0 | 2023-06-08 06:14:59 +0000 | [diff] [blame] | 359 | srcFiles, srcJars android.Paths, annoSrcJar android.WritablePath, flags javaBuilderFlags, deps android.Paths) { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 360 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 361 | // Compile java sources into .class files |
| 362 | desc := "javac" |
| 363 | if shardIdx >= 0 { |
| 364 | desc += strconv.Itoa(shardIdx) |
| 365 | } |
| 366 | |
Vadim Spivak | 3c496f0 | 2023-06-08 06:14:59 +0000 | [diff] [blame] | 367 | transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, annoSrcJar, flags, deps, "javac", desc) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 370 | // 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 Cross | 3b706fd | 2019-09-05 16:44:18 -0700 | [diff] [blame] | 372 | func emitXrefRule(ctx android.ModuleContext, xrefFile android.WritablePath, idx int, |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 373 | srcFiles, srcJars android.Paths, |
Colin Cross | 3b706fd | 2019-09-05 16:44:18 -0700 | [diff] [blame] | 374 | flags javaBuilderFlags, deps android.Paths) { |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 375 | |
| 376 | deps = append(deps, srcJars...) |
Sasha Smundak | 09950a4 | 2019-11-04 16:29:45 -0800 | [diff] [blame] | 377 | classpath := flags.classpath |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 378 | |
| 379 | var bootClasspath string |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 380 | if flags.javaVersion.usesJavaModules() { |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 381 | var systemModuleDeps android.Paths |
| 382 | bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device()) |
| 383 | deps = append(deps, systemModuleDeps...) |
Sasha Smundak | 09950a4 | 2019-11-04 16:29:45 -0800 | [diff] [blame] | 384 | classpath = append(flags.java9Classpath, classpath...) |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 385 | } 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 Smundak | 09950a4 | 2019-11-04 16:29:45 -0800 | [diff] [blame] | 396 | deps = append(deps, classpath...) |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 397 | deps = append(deps, flags.processorPath...) |
| 398 | |
| 399 | processor := "-proc:none" |
Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 400 | if len(flags.processors) > 0 { |
| 401 | processor = "-processor " + strings.Join(flags.processors, ",") |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Colin Cross | 3b706fd | 2019-09-05 16:44:18 -0700 | [diff] [blame] | 404 | intermediatesDir := "xref" |
| 405 | if idx >= 0 { |
| 406 | intermediatesDir += strconv.Itoa(idx) |
| 407 | } |
| 408 | |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 409 | 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 Smundak | 09950a4 | 2019-11-04 16:29:45 -0800 | [diff] [blame] | 419 | "classpath": classpath.FormJavaClassPath("-classpath"), |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 420 | "javacFlags": flags.javacFlags, |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 421 | "javaVersion": flags.javaVersion.String(), |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 422 | "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 Cross | c52d523 | 2024-01-10 16:43:33 -0800 | [diff] [blame] | 431 | func turbineFlags(ctx android.ModuleContext, flags javaBuilderFlags, dir string) (string, android.Paths) { |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 432 | var deps android.Paths |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 433 | |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 434 | classpath := flags.classpath |
| 435 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 436 | var bootClasspath string |
Colin Cross | bf3119e | 2019-10-28 14:25:10 -0700 | [diff] [blame] | 437 | if flags.javaVersion.usesJavaModules() { |
| 438 | var systemModuleDeps android.Paths |
| 439 | bootClasspath, systemModuleDeps = flags.systemModules.FormTurbineSystemModulesPath(ctx.Device()) |
| 440 | deps = append(deps, systemModuleDeps...) |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 441 | classpath = append(flags.java9Classpath, classpath...) |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 442 | } else { |
Colin Cross | bf3119e | 2019-10-28 14:25:10 -0700 | [diff] [blame] | 443 | 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 Cross | c2557d1 | 2019-10-31 15:22:57 -0700 | [diff] [blame] | 449 | bootClasspath = flags.bootClasspath.FormTurbineClassPath("--bootclasspath ") |
Colin Cross | bf3119e | 2019-10-28 14:25:10 -0700 | [diff] [blame] | 450 | } |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 451 | } |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 452 | |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 453 | deps = append(deps, classpath...) |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 454 | turbineFlags := bootClasspath + " " + classpath.FormTurbineClassPath("--classpath ") |
| 455 | |
Colin Cross | c52d523 | 2024-01-10 16:43:33 -0800 | [diff] [blame] | 456 | 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 Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 464 | return turbineFlags, deps |
| 465 | } |
| 466 | |
| 467 | func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath, |
| 468 | srcFiles, srcJars android.Paths, flags javaBuilderFlags) { |
| 469 | |
Colin Cross | c52d523 | 2024-01-10 16:43:33 -0800 | [diff] [blame] | 470 | turbineFlags, deps := turbineFlags(ctx, flags, "turbine") |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 471 | |
| 472 | deps = append(deps, srcJars...) |
Colin Cross | bf3119e | 2019-10-28 14:25:10 -0700 | [diff] [blame] | 473 | |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 474 | rule := turbine |
| 475 | args := map[string]string{ |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 476 | "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 Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 482 | } |
Ramy Medhat | 16f23a4 | 2020-09-03 01:29:49 -0400 | [diff] [blame] | 483 | if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_TURBINE") { |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 484 | rule = turbineRE |
| 485 | args["implicits"] = strings.Join(deps.Strings(), ",") |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 486 | args["rbeOutputs"] = outputFile.String() + ".tmp" |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 487 | } |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 488 | ctx.Build(pctx, android.BuildParams{ |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 489 | Rule: rule, |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 490 | Description: "turbine", |
| 491 | Output: outputFile, |
| 492 | Inputs: srcFiles, |
| 493 | Implicits: deps, |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 494 | Args: args, |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 495 | }) |
| 496 | } |
| 497 | |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 498 | // TurbineApt produces a rule to run annotation processors using turbine. |
Isaac Chiou | a23d994 | 2022-04-06 06:14:38 +0000 | [diff] [blame] | 499 | func TurbineApt(ctx android.ModuleContext, outputSrcJar, outputResJar android.WritablePath, |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 500 | srcFiles, srcJars android.Paths, flags javaBuilderFlags) { |
| 501 | |
Colin Cross | c52d523 | 2024-01-10 16:43:33 -0800 | [diff] [blame] | 502 | turbineFlags, deps := turbineFlags(ctx, flags, "kapt") |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 503 | |
| 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 Chiou | a23d994 | 2022-04-06 06:14:38 +0000 | [diff] [blame] | 510 | outputs := android.WritablePaths{outputSrcJar, outputResJar} |
| 511 | outputFlags := "--gensrc_output " + outputSrcJar.String() + ".tmp " + |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 512 | "--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 Chiou | a23d994 | 2022-04-06 06:14:38 +0000 | [diff] [blame] | 526 | args["rbeOutputs"] = outputSrcJar.String() + ".tmp," + outputResJar.String() + ".tmp" |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 527 | } |
| 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 Cross | 070879e | 2017-10-11 11:21:07 -0700 | [diff] [blame] | 539 | // transformJavaToClasses takes source files and converts them to a jar containing .class files. |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame] | 540 | // 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 Cross | 070879e | 2017-10-11 11:21:07 -0700 | [diff] [blame] | 542 | // |
| 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 Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 548 | func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, |
Vadim Spivak | 3c496f0 | 2023-06-08 06:14:59 +0000 | [diff] [blame] | 549 | shardIdx int, srcFiles, srcJars android.Paths, annoSrcJar android.WritablePath, |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 550 | flags javaBuilderFlags, deps android.Paths, |
Colin Cross | 6654810 | 2018-06-19 22:47:35 -0700 | [diff] [blame] | 551 | intermediatesDir, desc string) { |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 552 | |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame] | 553 | deps = append(deps, srcJars...) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 554 | |
Colin Cross | c52d523 | 2024-01-10 16:43:33 -0800 | [diff] [blame] | 555 | javacClasspath := flags.classpath |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 556 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 557 | var bootClasspath string |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 558 | if flags.javaVersion.usesJavaModules() { |
Colin Cross | b77043e | 2019-07-16 13:57:13 -0700 | [diff] [blame] | 559 | var systemModuleDeps android.Paths |
| 560 | bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device()) |
| 561 | deps = append(deps, systemModuleDeps...) |
Colin Cross | c52d523 | 2024-01-10 16:43:33 -0800 | [diff] [blame] | 562 | javacClasspath = append(flags.java9Classpath, javacClasspath...) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 563 | } else { |
| 564 | deps = append(deps, flags.bootClasspath...) |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 565 | 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 Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Colin Cross | c52d523 | 2024-01-10 16:43:33 -0800 | [diff] [blame] | 574 | 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 Cross | 6a77c98 | 2018-06-19 22:43:34 -0700 | [diff] [blame] | 587 | deps = append(deps, flags.processorPath...) |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 588 | |
Colin Cross | 7788c12 | 2019-01-23 16:14:02 -0800 | [diff] [blame] | 589 | processor := "-proc:none" |
Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 590 | if len(flags.processors) > 0 { |
| 591 | processor = "-processor " + strings.Join(flags.processors, ",") |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 592 | } |
| 593 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 594 | 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 Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 603 | rule := javac |
Ramy Medhat | 16f23a4 | 2020-09-03 01:29:49 -0400 | [diff] [blame] | 604 | if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAVAC") { |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 605 | rule = javacRE |
| 606 | } |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 607 | ctx.Build(pctx, android.BuildParams{ |
Vadim Spivak | 3c496f0 | 2023-06-08 06:14:59 +0000 | [diff] [blame] | 608 | Rule: rule, |
| 609 | Description: desc, |
| 610 | Output: outputFile, |
| 611 | ImplicitOutput: annoSrcJar, |
| 612 | Inputs: srcFiles, |
| 613 | Implicits: deps, |
Anna Bauza | d0476e4 | 2024-04-11 09:58:46 +0000 | [diff] [blame] | 614 | 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 Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 627 | }) |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 630 | func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath, |
| 631 | jarArgs []string, deps android.Paths) { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 632 | |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 633 | rule := jar |
Ramy Medhat | 16f23a4 | 2020-09-03 01:29:49 -0400 | [diff] [blame] | 634 | if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAR") { |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 635 | rule = jarRE |
| 636 | } |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 637 | ctx.Build(pctx, android.BuildParams{ |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 638 | Rule: rule, |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 639 | Description: "jar", |
| 640 | Output: outputFile, |
| 641 | Implicits: deps, |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 642 | Args: map[string]string{ |
Colin Cross | 0b9f31f | 2019-02-28 11:00:01 -0800 | [diff] [blame] | 643 | "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "), |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 644 | }, |
| 645 | }) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 648 | func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string, |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 649 | jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string, |
| 650 | dirsToStrip []string) { |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 651 | |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 652 | var deps android.Paths |
| 653 | |
| 654 | var jarArgs []string |
| 655 | if manifest.Valid() { |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 656 | jarArgs = append(jarArgs, "-m ", manifest.String()) |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 657 | deps = append(deps, manifest.Path()) |
| 658 | } |
| 659 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 660 | for _, dir := range dirsToStrip { |
| 661 | jarArgs = append(jarArgs, "-stripDir ", dir) |
| 662 | } |
| 663 | |
| 664 | for _, file := range filesToStrip { |
| 665 | jarArgs = append(jarArgs, "-stripFile ", file) |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Colin Cross | 7b60cdd | 2017-12-21 13:52:58 -0800 | [diff] [blame] | 668 | // 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 Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 672 | if stripDirEntries { |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 673 | jarArgs = append(jarArgs, "-D") |
| 674 | } |
| 675 | |
Colin Cross | c52d523 | 2024-01-10 16:43:33 -0800 | [diff] [blame] | 676 | 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 Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 691 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | c52d523 | 2024-01-10 16:43:33 -0800 | [diff] [blame] | 692 | Rule: rule, |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 693 | Description: desc, |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 694 | Output: outputFile, |
| 695 | Inputs: jars, |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 696 | Implicits: deps, |
| 697 | Args: map[string]string{ |
| 698 | "jarArgs": strings.Join(jarArgs, " "), |
| 699 | }, |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 700 | }) |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Colin Cross | f06d8dc | 2023-07-18 22:11:07 -0700 | [diff] [blame] | 703 | func 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 Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 712 | func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath, |
| 713 | classesJar android.Path, rulesFile android.Path) { |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 714 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 715 | Rule: jarjar, |
| 716 | Description: "jarjar", |
| 717 | Output: outputFile, |
| 718 | Input: classesJar, |
| 719 | Implicit: rulesFile, |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 720 | Args: map[string]string{ |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 721 | "rulesFile": rulesFile.String(), |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 722 | }, |
| 723 | }) |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 724 | } |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 725 | |
Colin Cross | b549b77 | 2020-06-03 17:14:31 +0000 | [diff] [blame] | 726 | func CheckJarPackages(ctx android.ModuleContext, outputFile android.WritablePath, |
| 727 | classesJar android.Path, permittedPackages []string) { |
Vladimir Marko | 0975ee0 | 2019-04-02 10:29:55 +0100 | [diff] [blame] | 728 | 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 Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 739 | func 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 Cross | 094054a | 2018-10-17 15:10:48 -0700 | [diff] [blame] | 749 | func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) { |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 750 | android.WriteFileRule(ctx, outputFile, "Main-Class: "+mainClass+"\n") |
Colin Cross | 094054a | 2018-10-17 15:10:48 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Cole Faust | 6158528 | 2023-07-14 16:23:39 -0700 | [diff] [blame] | 753 | func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path, validations android.Paths) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 754 | ctx.Build(pctx, android.BuildParams{ |
| 755 | Rule: zipalign, |
| 756 | Description: "align", |
| 757 | Input: inputFile, |
| 758 | Output: outputFile, |
Cole Faust | 6158528 | 2023-07-14 16:23:39 -0700 | [diff] [blame] | 759 | Validations: validations, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 760 | }) |
| 761 | } |
| 762 | |
Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame] | 763 | func 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 Cross | 33961b5 | 2019-07-11 11:01:22 -0700 | [diff] [blame] | 772 | type classpath android.Paths |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 773 | |
Colin Cross | c2557d1 | 2019-10-31 15:22:57 -0700 | [diff] [blame] | 774 | func (x *classpath) formJoinedClassPath(optName string, sep string) string { |
Colin Cross | 8144008 | 2018-08-15 20:21:55 -0700 | [diff] [blame] | 775 | if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") { |
| 776 | optName += " " |
| 777 | } |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame] | 778 | if len(*x) > 0 { |
Colin Cross | c2557d1 | 2019-10-31 15:22:57 -0700 | [diff] [blame] | 779 | return optName + strings.Join(x.Strings(), sep) |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 780 | } else { |
| 781 | return "" |
| 782 | } |
| 783 | } |
Colin Cross | c2557d1 | 2019-10-31 15:22:57 -0700 | [diff] [blame] | 784 | func (x *classpath) FormJavaClassPath(optName string) string { |
| 785 | return x.formJoinedClassPath(optName, ":") |
| 786 | } |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 787 | |
Colin Cross | c2557d1 | 2019-10-31 15:22:57 -0700 | [diff] [blame] | 788 | func (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. |
| 793 | func (x *classpath) FormRepeatedClassPath(optName string) []string { |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 794 | if x == nil || *x == nil { |
| 795 | return nil |
| 796 | } |
| 797 | flags := make([]string, len(*x)) |
| 798 | for i, v := range *x { |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 799 | flags[i] = optName + v.String() |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | return flags |
| 803 | } |
| 804 | |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 805 | // Convert a classpath to an android.Paths |
| 806 | func (x *classpath) Paths() android.Paths { |
| 807 | return append(android.Paths(nil), (*x)...) |
| 808 | } |
| 809 | |
| 810 | func (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 Cross | b77043e | 2019-07-16 13:57:13 -0700 | [diff] [blame] | 820 | |
| 821 | type systemModules struct { |
| 822 | dir android.Path |
| 823 | deps android.Paths |
| 824 | } |
| 825 | |
Colin Cross | bf3119e | 2019-10-28 14:25:10 -0700 | [diff] [blame] | 826 | // 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 Cross | b77043e | 2019-07-16 13:57:13 -0700 | [diff] [blame] | 829 | func (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 Cross | bf3119e | 2019-10-28 14:25:10 -0700 | [diff] [blame] | 838 | |
| 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. |
| 842 | func (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 Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 848 | return "--system ${config.JavaHome}", nil |
Colin Cross | bf3119e | 2019-10-28 14:25:10 -0700 | [diff] [blame] | 849 | } |
| 850 | } |