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{ |
Ulf Adams | 26d1955 | 2020-11-25 23:03:28 +0100 | [diff] [blame] | 45 | Command: `rm -rf "$outDir" "$annoDir" "$srcJarDir" "$out" && mkdir -p "$outDir" "$annoDir" "$srcJarDir" && ` + |
Colin Cross | 436b765 | 2018-03-15 16:24:10 -0700 | [diff] [blame] | 46 | `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` + |
Sundong Ahn | 24a099c | 2018-06-28 14:53:20 +0900 | [diff] [blame] | 47 | `(if [ -s $srcJarDir/list ] || [ -s $out.rsp ] ; then ` + |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 48 | `${config.SoongJavacWrapper} $javaTemplate${config.JavacCmd} ` + |
Sasha Smundak | 26c6d9e | 2019-06-11 13:30:13 -0700 | [diff] [blame] | 49 | `${config.JavacHeapFlags} ${config.JavacVmFlags} ${config.CommonJdkFlags} ` + |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 50 | `$processorpath $processor $javacFlags $bootClasspath $classpath ` + |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 51 | `-source $javaVersion -target $javaVersion ` + |
Sundong Ahn | 24a099c | 2018-06-28 14:53:20 +0900 | [diff] [blame] | 52 | `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list ; fi ) && ` + |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 53 | `$zipTemplate${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir && ` + |
Colin Cross | 44c29a8 | 2019-01-24 16:36:57 -0800 | [diff] [blame] | 54 | `rm -rf "$srcJarDir"`, |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 55 | CommandDeps: []string{ |
| 56 | "${config.JavacCmd}", |
| 57 | "${config.SoongZipCmd}", |
Colin Cross | 436b765 | 2018-03-15 16:24:10 -0700 | [diff] [blame] | 58 | "${config.ZipSyncCmd}", |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 59 | }, |
Colin Cross | a482065 | 2017-10-17 13:56:52 -0700 | [diff] [blame] | 60 | CommandOrderOnly: []string{"${config.SoongJavacWrapper}"}, |
| 61 | Rspfile: "$out.rsp", |
| 62 | RspfileContent: "$in", |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 63 | }, map[string]*remoteexec.REParams{ |
| 64 | "$javaTemplate": &remoteexec.REParams{ |
| 65 | Labels: map[string]string{"type": "compile", "lang": "java", "compiler": "javac"}, |
| 66 | ExecStrategy: "${config.REJavacExecStrategy}", |
| 67 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 68 | }, |
| 69 | "$zipTemplate": &remoteexec.REParams{ |
| 70 | Labels: map[string]string{"type": "tool", "name": "soong_zip"}, |
| 71 | Inputs: []string{"${config.SoongZipCmd}", "$outDir"}, |
| 72 | OutputFiles: []string{"$out"}, |
| 73 | ExecStrategy: "${config.REJavacExecStrategy}", |
| 74 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 75 | }, |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 76 | }, []string{"javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir", |
| 77 | "outDir", "annoDir", "javaVersion"}, nil) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 78 | |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 79 | _ = pctx.VariableFunc("kytheCorpus", |
| 80 | func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() }) |
Sasha Smundak | 6c2d4f9 | 2020-01-09 17:34:23 -0800 | [diff] [blame] | 81 | _ = pctx.VariableFunc("kytheCuEncoding", |
| 82 | func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuEncoding() }) |
Sasha Smundak | b0addaf | 2021-02-16 10:39:40 -0800 | [diff] [blame] | 83 | _ = pctx.VariableFunc("kytheCuJavaSourceMax", |
| 84 | func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuJavaSourceMax() }) |
Sasha Smundak | 6514364 | 2019-09-26 20:14:28 -0700 | [diff] [blame] | 85 | _ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json") |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 86 | // Run it with -add-opens=java.base/java.nio=ALL-UNNAMED to avoid JDK9's warning about |
| 87 | // "Illegal reflective access by com.google.protobuf.Utf8$UnsafeProcessor ... |
| 88 | // to field java.nio.Buffer.address" |
| 89 | kytheExtract = pctx.AndroidStaticRule("kythe", |
| 90 | blueprint.RuleParams{ |
| 91 | Command: `${config.ZipSyncCmd} -d $srcJarDir ` + |
| 92 | `-l $srcJarDir/list -f "*.java" $srcJars && ` + |
| 93 | `( [ ! -s $srcJarDir/list -a ! -s $out.rsp ] || ` + |
| 94 | `KYTHE_ROOT_DIRECTORY=. KYTHE_OUTPUT_FILE=$out ` + |
| 95 | `KYTHE_CORPUS=${kytheCorpus} ` + |
Sasha Smundak | 6514364 | 2019-09-26 20:14:28 -0700 | [diff] [blame] | 96 | `KYTHE_VNAMES=${kytheVnames} ` + |
Sasha Smundak | 6c2d4f9 | 2020-01-09 17:34:23 -0800 | [diff] [blame] | 97 | `KYTHE_KZIP_ENCODING=${kytheCuEncoding} ` + |
Sasha Smundak | b0addaf | 2021-02-16 10:39:40 -0800 | [diff] [blame] | 98 | `KYTHE_JAVA_SOURCE_BATCH_SIZE=${kytheCuJavaSourceMax} ` + |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 99 | `${config.SoongJavacWrapper} ${config.JavaCmd} ` + |
| 100 | `--add-opens=java.base/java.nio=ALL-UNNAMED ` + |
| 101 | `-jar ${config.JavaKytheExtractorJar} ` + |
| 102 | `${config.JavacHeapFlags} ${config.CommonJdkFlags} ` + |
| 103 | `$processorpath $processor $javacFlags $bootClasspath $classpath ` + |
| 104 | `-source $javaVersion -target $javaVersion ` + |
| 105 | `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list)`, |
| 106 | CommandDeps: []string{ |
| 107 | "${config.JavaCmd}", |
| 108 | "${config.JavaKytheExtractorJar}", |
Sasha Smundak | 6514364 | 2019-09-26 20:14:28 -0700 | [diff] [blame] | 109 | "${kytheVnames}", |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 110 | "${config.ZipSyncCmd}", |
| 111 | }, |
| 112 | CommandOrderOnly: []string{"${config.SoongJavacWrapper}"}, |
| 113 | Rspfile: "$out.rsp", |
| 114 | RspfileContent: "$in", |
| 115 | }, |
| 116 | "javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir", |
| 117 | "outDir", "annoDir", "javaVersion") |
| 118 | |
Sasha Smundak | a7856c0 | 2020-04-23 09:49:59 -0700 | [diff] [blame] | 119 | extractMatchingApks = pctx.StaticRule( |
| 120 | "extractMatchingApks", |
| 121 | blueprint.RuleParams{ |
| 122 | Command: `rm -rf "$out" && ` + |
Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 123 | `${config.ExtractApksCmd} -o "${out}" -zip "${zip}" -allow-prereleased=${allow-prereleased} ` + |
Sasha Smundak | a7856c0 | 2020-04-23 09:49:59 -0700 | [diff] [blame] | 124 | `-sdk-version=${sdk-version} -abis=${abis} ` + |
| 125 | `--screen-densities=${screen-densities} --stem=${stem} ` + |
Jaewoong Jung | 11c1e0f | 2020-06-29 19:18:44 -0700 | [diff] [blame] | 126 | `-apkcerts=${apkcerts} -partition=${partition} ` + |
Sasha Smundak | a7856c0 | 2020-04-23 09:49:59 -0700 | [diff] [blame] | 127 | `${in}`, |
| 128 | CommandDeps: []string{"${config.ExtractApksCmd}"}, |
| 129 | }, |
Colin Cross | ffbcd1d | 2021-11-12 12:19:42 -0800 | [diff] [blame] | 130 | "abis", "allow-prereleased", "screen-densities", "sdk-version", "stem", "apkcerts", "partition", "zip") |
Sasha Smundak | a7856c0 | 2020-04-23 09:49:59 -0700 | [diff] [blame] | 131 | |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 132 | turbine, turbineRE = pctx.RemoteStaticRules("turbine", |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 133 | blueprint.RuleParams{ |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame^] | 134 | Command: `$reTemplate${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.TurbineJar} $outputFlags ` + |
Colin Cross | 411647e | 2022-03-16 14:28:36 -0700 | [diff] [blame] | 135 | `--sources @$out.rsp --source_jars $srcJars ` + |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 136 | `--javacopts ${config.CommonJdkFlags} ` + |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame^] | 137 | `$javacFlags -source $javaVersion -target $javaVersion -- $turbineFlags && ` + |
| 138 | `(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] | 139 | CommandDeps: []string{ |
| 140 | "${config.TurbineJar}", |
| 141 | "${config.JavaCmd}", |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 142 | }, |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 143 | Rspfile: "$out.rsp", |
| 144 | RspfileContent: "$in", |
| 145 | Restat: true, |
| 146 | }, |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 147 | &remoteexec.REParams{Labels: map[string]string{"type": "tool", "name": "turbine"}, |
Colin Cross | 411647e | 2022-03-16 14:28:36 -0700 | [diff] [blame] | 148 | ExecStrategy: "${config.RETurbineExecStrategy}", |
| 149 | Inputs: []string{"${config.TurbineJar}", "${out}.rsp", "$implicits"}, |
| 150 | RSPFiles: []string{"${out}.rsp"}, |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame^] | 151 | OutputFiles: []string{"$rbeOutputs"}, |
Colin Cross | 411647e | 2022-03-16 14:28:36 -0700 | [diff] [blame] | 152 | ToolchainInputs: []string{"${config.JavaCmd}"}, |
| 153 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame^] | 154 | }, |
| 155 | []string{"javacFlags", "turbineFlags", "outputFlags", "javaVersion", "outputs", "rbeOutputs", "srcJars"}, []string{"implicits"}) |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 156 | |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 157 | jar, jarRE = pctx.RemoteStaticRules("jar", |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 158 | blueprint.RuleParams{ |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 159 | Command: `$reTemplate${config.SoongZipCmd} -jar -o $out @$out.rsp`, |
Nan Zhang | 674dd93 | 2018-01-26 18:30:36 -0800 | [diff] [blame] | 160 | CommandDeps: []string{"${config.SoongZipCmd}"}, |
| 161 | Rspfile: "$out.rsp", |
| 162 | RspfileContent: "$jarArgs", |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 163 | }, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 164 | &remoteexec.REParams{ |
| 165 | ExecStrategy: "${config.REJarExecStrategy}", |
| 166 | Inputs: []string{"${config.SoongZipCmd}", "${out}.rsp"}, |
Colin Cross | a4eafdd | 2021-03-24 14:09:28 -0700 | [diff] [blame] | 167 | RSPFiles: []string{"${out}.rsp"}, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 168 | OutputFiles: []string{"$out"}, |
| 169 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 170 | }, []string{"jarArgs"}, nil) |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 171 | |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 172 | zip, zipRE = pctx.RemoteStaticRules("zip", |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 173 | blueprint.RuleParams{ |
| 174 | Command: `${config.SoongZipCmd} -o $out @$out.rsp`, |
| 175 | CommandDeps: []string{"${config.SoongZipCmd}"}, |
| 176 | Rspfile: "$out.rsp", |
| 177 | RspfileContent: "$jarArgs", |
| 178 | }, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 179 | &remoteexec.REParams{ |
| 180 | ExecStrategy: "${config.REZipExecStrategy}", |
| 181 | Inputs: []string{"${config.SoongZipCmd}", "${out}.rsp", "$implicits"}, |
Colin Cross | a4eafdd | 2021-03-24 14:09:28 -0700 | [diff] [blame] | 182 | RSPFiles: []string{"${out}.rsp"}, |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 183 | OutputFiles: []string{"$out"}, |
| 184 | Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"}, |
| 185 | }, []string{"jarArgs"}, []string{"implicits"}) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 186 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 187 | combineJar = pctx.AndroidStaticRule("combineJar", |
| 188 | blueprint.RuleParams{ |
Colin Cross | f91a08c | 2018-02-07 15:41:31 -0800 | [diff] [blame] | 189 | Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`, |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 190 | CommandDeps: []string{"${config.MergeZipsCmd}"}, |
| 191 | }, |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 192 | "jarArgs") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 193 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 194 | jarjar = pctx.AndroidStaticRule("jarjar", |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 195 | blueprint.RuleParams{ |
Colin Cross | d97cf63 | 2021-02-08 10:14:04 -0800 | [diff] [blame] | 196 | Command: "" + |
| 197 | // Jarjar doesn't exit with an error when the rules file contains a syntax error, |
| 198 | // leading to stale or missing files later in the build. Remove the output file |
| 199 | // before running jarjar. |
| 200 | "rm -f ${out} && " + |
| 201 | "${config.JavaCmd} ${config.JavaVmFlags}" + |
Artur Satayev | 762d9f3 | 2020-04-15 12:50:04 +0100 | [diff] [blame] | 202 | // b/146418363 Enable Android specific jarjar transformer to drop compat annotations |
| 203 | // for newly repackaged classes. Dropping @UnsupportedAppUsage on repackaged classes |
| 204 | // avoids adding new hiddenapis after jarjar'ing. |
| 205 | " -DremoveAndroidCompatAnnotations=true" + |
Colin Cross | d97cf63 | 2021-02-08 10:14:04 -0800 | [diff] [blame] | 206 | " -jar ${config.JarjarCmd} process $rulesFile $in $out && " + |
| 207 | // Turn a missing output file into a ninja error |
| 208 | `[ -e ${out} ] || (echo "Missing output file"; exit 1)`, |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 209 | CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"}, |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 210 | }, |
| 211 | "rulesFile") |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 212 | |
Vladimir Marko | 0975ee0 | 2019-04-02 10:29:55 +0100 | [diff] [blame] | 213 | packageCheck = pctx.AndroidStaticRule("packageCheck", |
| 214 | blueprint.RuleParams{ |
| 215 | Command: "rm -f $out && " + |
| 216 | "${config.PackageCheckCmd} $in $packages && " + |
Colin Cross | b549b77 | 2020-06-03 17:14:31 +0000 | [diff] [blame] | 217 | "touch $out", |
Vladimir Marko | 0975ee0 | 2019-04-02 10:29:55 +0100 | [diff] [blame] | 218 | CommandDeps: []string{"${config.PackageCheckCmd}"}, |
| 219 | }, |
| 220 | "packages") |
| 221 | |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 222 | jetifier = pctx.AndroidStaticRule("jetifier", |
| 223 | blueprint.RuleParams{ |
Sasha Smundak | 26c6d9e | 2019-06-11 13:30:13 -0700 | [diff] [blame] | 224 | Command: "${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JetifierJar} -l error -o $out -i $in", |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 225 | CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"}, |
| 226 | }, |
| 227 | ) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 228 | |
| 229 | zipalign = pctx.AndroidStaticRule("zipalign", |
| 230 | blueprint.RuleParams{ |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 231 | Command: "if ! ${config.ZipAlign} -c -p 4 $in > /dev/null; then " + |
| 232 | "${config.ZipAlign} -f -p 4 $in $out; " + |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 233 | "else " + |
| 234 | "cp -f $in $out; " + |
| 235 | "fi", |
| 236 | CommandDeps: []string{"${config.ZipAlign}"}, |
| 237 | }, |
| 238 | ) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 239 | ) |
| 240 | |
| 241 | func init() { |
Colin Cross | cc0ce80 | 2019-04-02 16:14:11 -0700 | [diff] [blame] | 242 | pctx.Import("android/soong/android") |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 243 | pctx.Import("android/soong/java/config") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | type javaBuilderFlags struct { |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 247 | javacFlags string |
| 248 | |
| 249 | // bootClasspath is the list of jars that form the boot classpath (generally the java.* and |
| 250 | // android.* classes) for tools that still use it. javac targeting 1.9 or higher uses |
| 251 | // systemModules and java9Classpath instead. |
| 252 | bootClasspath classpath |
| 253 | |
| 254 | // classpath is the list of jars that form the classpath for javac and kotlinc rules. It |
| 255 | // contains header jars for all static and non-static dependencies. |
| 256 | classpath classpath |
| 257 | |
| 258 | // dexClasspath is the list of jars that form the classpath for d8 and r8 rules. It contains |
| 259 | // header jars for all non-static dependencies. Static dependencies have already been |
| 260 | // combined into the program jar. |
| 261 | dexClasspath classpath |
| 262 | |
| 263 | // java9Classpath is the list of jars that will be added to the classpath when targeting |
| 264 | // 1.9 or higher. It generally contains the android.* classes, while the java.* classes |
| 265 | // are provided by systemModules. |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 266 | java9Classpath classpath |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 267 | |
| 268 | processorPath classpath |
| 269 | processors []string |
| 270 | systemModules *systemModules |
| 271 | aidlFlags string |
| 272 | aidlDeps android.Paths |
| 273 | javaVersion javaVersion |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 274 | |
Andreas Gampe | f3e5b55 | 2018-01-22 21:27:21 -0800 | [diff] [blame] | 275 | errorProneExtraJavacFlags string |
Colin Cross | 6654810 | 2018-06-19 22:47:35 -0700 | [diff] [blame] | 276 | errorProneProcessorPath classpath |
Andreas Gampe | f3e5b55 | 2018-01-22 21:27:21 -0800 | [diff] [blame] | 277 | |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 278 | kotlincFlags string |
| 279 | kotlincClasspath classpath |
Colin Cross | a1ff7c6 | 2021-09-17 14:11:52 -0700 | [diff] [blame] | 280 | kotlincDeps android.Paths |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 281 | |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 282 | proto android.ProtoFlags |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 285 | func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int, |
| 286 | srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 287 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 288 | // Compile java sources into .class files |
| 289 | desc := "javac" |
| 290 | if shardIdx >= 0 { |
| 291 | desc += strconv.Itoa(shardIdx) |
| 292 | } |
| 293 | |
Colin Cross | 6654810 | 2018-06-19 22:47:35 -0700 | [diff] [blame] | 294 | transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 297 | // Emits the rule to generate Xref input file (.kzip file) for the given set of source files and source jars |
| 298 | // to compile with given set of builder flags, etc. |
Colin Cross | 3b706fd | 2019-09-05 16:44:18 -0700 | [diff] [blame] | 299 | func emitXrefRule(ctx android.ModuleContext, xrefFile android.WritablePath, idx int, |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 300 | srcFiles, srcJars android.Paths, |
Colin Cross | 3b706fd | 2019-09-05 16:44:18 -0700 | [diff] [blame] | 301 | flags javaBuilderFlags, deps android.Paths) { |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 302 | |
| 303 | deps = append(deps, srcJars...) |
Sasha Smundak | 09950a4 | 2019-11-04 16:29:45 -0800 | [diff] [blame] | 304 | classpath := flags.classpath |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 305 | |
| 306 | var bootClasspath string |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 307 | if flags.javaVersion.usesJavaModules() { |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 308 | var systemModuleDeps android.Paths |
| 309 | bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device()) |
| 310 | deps = append(deps, systemModuleDeps...) |
Sasha Smundak | 09950a4 | 2019-11-04 16:29:45 -0800 | [diff] [blame] | 311 | classpath = append(flags.java9Classpath, classpath...) |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 312 | } else { |
| 313 | deps = append(deps, flags.bootClasspath...) |
| 314 | if len(flags.bootClasspath) == 0 && ctx.Device() { |
| 315 | // explicitly specify -bootclasspath "" if the bootclasspath is empty to |
| 316 | // ensure java does not fall back to the default bootclasspath. |
| 317 | bootClasspath = `-bootclasspath ""` |
| 318 | } else { |
| 319 | bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath") |
| 320 | } |
| 321 | } |
| 322 | |
Sasha Smundak | 09950a4 | 2019-11-04 16:29:45 -0800 | [diff] [blame] | 323 | deps = append(deps, classpath...) |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 324 | deps = append(deps, flags.processorPath...) |
| 325 | |
| 326 | processor := "-proc:none" |
Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 327 | if len(flags.processors) > 0 { |
| 328 | processor = "-processor " + strings.Join(flags.processors, ",") |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Colin Cross | 3b706fd | 2019-09-05 16:44:18 -0700 | [diff] [blame] | 331 | intermediatesDir := "xref" |
| 332 | if idx >= 0 { |
| 333 | intermediatesDir += strconv.Itoa(idx) |
| 334 | } |
| 335 | |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 336 | ctx.Build(pctx, |
| 337 | android.BuildParams{ |
| 338 | Rule: kytheExtract, |
| 339 | Description: "Xref Java extractor", |
| 340 | Output: xrefFile, |
| 341 | Inputs: srcFiles, |
| 342 | Implicits: deps, |
| 343 | Args: map[string]string{ |
| 344 | "annoDir": android.PathForModuleOut(ctx, intermediatesDir, "anno").String(), |
| 345 | "bootClasspath": bootClasspath, |
Sasha Smundak | 09950a4 | 2019-11-04 16:29:45 -0800 | [diff] [blame] | 346 | "classpath": classpath.FormJavaClassPath("-classpath"), |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 347 | "javacFlags": flags.javacFlags, |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 348 | "javaVersion": flags.javaVersion.String(), |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 349 | "outDir": android.PathForModuleOut(ctx, "javac", "classes.xref").String(), |
| 350 | "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"), |
| 351 | "processor": processor, |
| 352 | "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, "srcjars.xref").String(), |
| 353 | "srcJars": strings.Join(srcJars.Strings(), " "), |
| 354 | }, |
| 355 | }) |
| 356 | } |
| 357 | |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame^] | 358 | func turbineFlags(ctx android.ModuleContext, flags javaBuilderFlags) (string, android.Paths) { |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 359 | var deps android.Paths |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 360 | |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 361 | classpath := flags.classpath |
| 362 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 363 | var bootClasspath string |
Colin Cross | bf3119e | 2019-10-28 14:25:10 -0700 | [diff] [blame] | 364 | if flags.javaVersion.usesJavaModules() { |
| 365 | var systemModuleDeps android.Paths |
| 366 | bootClasspath, systemModuleDeps = flags.systemModules.FormTurbineSystemModulesPath(ctx.Device()) |
| 367 | deps = append(deps, systemModuleDeps...) |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 368 | classpath = append(flags.java9Classpath, classpath...) |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 369 | } else { |
Colin Cross | bf3119e | 2019-10-28 14:25:10 -0700 | [diff] [blame] | 370 | deps = append(deps, flags.bootClasspath...) |
| 371 | if len(flags.bootClasspath) == 0 && ctx.Device() { |
| 372 | // explicitly specify -bootclasspath "" if the bootclasspath is empty to |
| 373 | // ensure turbine does not fall back to the default bootclasspath. |
| 374 | bootClasspath = `--bootclasspath ""` |
| 375 | } else { |
Colin Cross | c2557d1 | 2019-10-31 15:22:57 -0700 | [diff] [blame] | 376 | bootClasspath = flags.bootClasspath.FormTurbineClassPath("--bootclasspath ") |
Colin Cross | bf3119e | 2019-10-28 14:25:10 -0700 | [diff] [blame] | 377 | } |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 378 | } |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 379 | |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 380 | deps = append(deps, classpath...) |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame^] | 381 | turbineFlags := bootClasspath + " " + classpath.FormTurbineClassPath("--classpath ") |
| 382 | |
| 383 | return turbineFlags, deps |
| 384 | } |
| 385 | |
| 386 | func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath, |
| 387 | srcFiles, srcJars android.Paths, flags javaBuilderFlags) { |
| 388 | |
| 389 | turbineFlags, deps := turbineFlags(ctx, flags) |
| 390 | |
| 391 | deps = append(deps, srcJars...) |
Colin Cross | bf3119e | 2019-10-28 14:25:10 -0700 | [diff] [blame] | 392 | |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 393 | rule := turbine |
| 394 | args := map[string]string{ |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame^] | 395 | "javacFlags": flags.javacFlags, |
| 396 | "srcJars": strings.Join(srcJars.Strings(), " "), |
| 397 | "javaVersion": flags.javaVersion.String(), |
| 398 | "turbineFlags": turbineFlags, |
| 399 | "outputFlags": "--output " + outputFile.String() + ".tmp", |
| 400 | "outputs": outputFile.String(), |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 401 | } |
Ramy Medhat | 16f23a4 | 2020-09-03 01:29:49 -0400 | [diff] [blame] | 402 | if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_TURBINE") { |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 403 | rule = turbineRE |
| 404 | args["implicits"] = strings.Join(deps.Strings(), ",") |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame^] | 405 | args["rbeOutputs"] = outputFile.String() + ".tmp" |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 406 | } |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 407 | ctx.Build(pctx, android.BuildParams{ |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 408 | Rule: rule, |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 409 | Description: "turbine", |
| 410 | Output: outputFile, |
| 411 | Inputs: srcFiles, |
| 412 | Implicits: deps, |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 413 | Args: args, |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 414 | }) |
| 415 | } |
| 416 | |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame^] | 417 | // TurbineApt produces a rule to run annotation processors using turbine. |
| 418 | func TurbineApt(ctx android.ModuleContext, outputSrcJar, outputResJar android.WritablePath, |
| 419 | srcFiles, srcJars android.Paths, flags javaBuilderFlags) { |
| 420 | |
| 421 | turbineFlags, deps := turbineFlags(ctx, flags) |
| 422 | |
| 423 | deps = append(deps, srcJars...) |
| 424 | |
| 425 | deps = append(deps, flags.processorPath...) |
| 426 | turbineFlags += " " + flags.processorPath.FormTurbineClassPath("--processorpath ") |
| 427 | turbineFlags += " --processors " + strings.Join(flags.processors, " ") |
| 428 | |
| 429 | outputs := android.WritablePaths{outputSrcJar, outputResJar} |
| 430 | outputFlags := "--gensrc_output " + outputSrcJar.String() + ".tmp " + |
| 431 | "--resource_output " + outputResJar.String() + ".tmp" |
| 432 | |
| 433 | rule := turbine |
| 434 | args := map[string]string{ |
| 435 | "javacFlags": flags.javacFlags, |
| 436 | "srcJars": strings.Join(srcJars.Strings(), " "), |
| 437 | "javaVersion": flags.javaVersion.String(), |
| 438 | "turbineFlags": turbineFlags, |
| 439 | "outputFlags": outputFlags, |
| 440 | "outputs": strings.Join(outputs.Strings(), " "), |
| 441 | } |
| 442 | if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_TURBINE") { |
| 443 | rule = turbineRE |
| 444 | args["implicits"] = strings.Join(deps.Strings(), ",") |
| 445 | args["rbeOutputs"] = outputSrcJar.String() + ".tmp," + outputResJar.String() + ".tmp" |
| 446 | } |
| 447 | ctx.Build(pctx, android.BuildParams{ |
| 448 | Rule: rule, |
| 449 | Description: "turbine apt", |
| 450 | Output: outputs[0], |
| 451 | ImplicitOutputs: outputs[1:], |
| 452 | Inputs: srcFiles, |
| 453 | Implicits: deps, |
| 454 | Args: args, |
| 455 | }) |
| 456 | } |
| 457 | |
Colin Cross | 070879e | 2017-10-11 11:21:07 -0700 | [diff] [blame] | 458 | // 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] | 459 | // srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain |
| 460 | // 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] | 461 | // |
| 462 | // This method may be used for different compilers, including javac and Error Prone. The rule |
| 463 | // argument specifies which command line to use and desc sets the description of the rule that will |
| 464 | // be printed at build time. The stem argument provides the file name of the output jar, and |
| 465 | // suffix will be appended to various intermediate files and directories to avoid collisions when |
| 466 | // this function is called twice in the same module directory. |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 467 | func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 468 | shardIdx int, srcFiles, srcJars android.Paths, |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 469 | flags javaBuilderFlags, deps android.Paths, |
Colin Cross | 6654810 | 2018-06-19 22:47:35 -0700 | [diff] [blame] | 470 | intermediatesDir, desc string) { |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 471 | |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame] | 472 | deps = append(deps, srcJars...) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 473 | |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 474 | classpath := flags.classpath |
| 475 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 476 | var bootClasspath string |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 477 | if flags.javaVersion.usesJavaModules() { |
Colin Cross | b77043e | 2019-07-16 13:57:13 -0700 | [diff] [blame] | 478 | var systemModuleDeps android.Paths |
| 479 | bootClasspath, systemModuleDeps = flags.systemModules.FormJavaSystemModulesPath(ctx.Device()) |
| 480 | deps = append(deps, systemModuleDeps...) |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 481 | classpath = append(flags.java9Classpath, classpath...) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 482 | } else { |
| 483 | deps = append(deps, flags.bootClasspath...) |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 484 | if len(flags.bootClasspath) == 0 && ctx.Device() { |
| 485 | // explicitly specify -bootclasspath "" if the bootclasspath is empty to |
| 486 | // ensure java does not fall back to the default bootclasspath. |
| 487 | bootClasspath = `-bootclasspath ""` |
| 488 | } else { |
| 489 | bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath") |
| 490 | } |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 493 | deps = append(deps, classpath...) |
Colin Cross | 6a77c98 | 2018-06-19 22:43:34 -0700 | [diff] [blame] | 494 | deps = append(deps, flags.processorPath...) |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 495 | |
Colin Cross | 7788c12 | 2019-01-23 16:14:02 -0800 | [diff] [blame] | 496 | processor := "-proc:none" |
Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 497 | if len(flags.processors) > 0 { |
| 498 | processor = "-processor " + strings.Join(flags.processors, ",") |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 501 | srcJarDir := "srcjars" |
| 502 | outDir := "classes" |
| 503 | annoDir := "anno" |
| 504 | if shardIdx >= 0 { |
| 505 | shardDir := "shard" + strconv.Itoa(shardIdx) |
| 506 | srcJarDir = filepath.Join(shardDir, srcJarDir) |
| 507 | outDir = filepath.Join(shardDir, outDir) |
| 508 | annoDir = filepath.Join(shardDir, annoDir) |
| 509 | } |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 510 | rule := javac |
Ramy Medhat | 16f23a4 | 2020-09-03 01:29:49 -0400 | [diff] [blame] | 511 | if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAVAC") { |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 512 | rule = javacRE |
| 513 | } |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 514 | ctx.Build(pctx, android.BuildParams{ |
Ramy Medhat | 1dcc27e | 2020-04-21 21:36:23 -0400 | [diff] [blame] | 515 | Rule: rule, |
Colin Cross | 070879e | 2017-10-11 11:21:07 -0700 | [diff] [blame] | 516 | Description: desc, |
| 517 | Output: outputFile, |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 518 | Inputs: srcFiles, |
| 519 | Implicits: deps, |
| 520 | Args: map[string]string{ |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame] | 521 | "javacFlags": flags.javacFlags, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 522 | "bootClasspath": bootClasspath, |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 523 | "classpath": classpath.FormJavaClassPath("-classpath"), |
Colin Cross | 6a77c98 | 2018-06-19 22:43:34 -0700 | [diff] [blame] | 524 | "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"), |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 525 | "processor": processor, |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 526 | "srcJars": strings.Join(srcJars.Strings(), " "), |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 527 | "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(), |
| 528 | "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(), |
| 529 | "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(), |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 530 | "javaVersion": flags.javaVersion.String(), |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 531 | }, |
| 532 | }) |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 535 | func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath, |
| 536 | jarArgs []string, deps android.Paths) { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 537 | |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 538 | rule := jar |
Ramy Medhat | 16f23a4 | 2020-09-03 01:29:49 -0400 | [diff] [blame] | 539 | if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_JAR") { |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 540 | rule = jarRE |
| 541 | } |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 542 | ctx.Build(pctx, android.BuildParams{ |
Kousik Kumar | 366afc5 | 2020-05-20 11:27:16 -0700 | [diff] [blame] | 543 | Rule: rule, |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 544 | Description: "jar", |
| 545 | Output: outputFile, |
| 546 | Implicits: deps, |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 547 | Args: map[string]string{ |
Colin Cross | 0b9f31f | 2019-02-28 11:00:01 -0800 | [diff] [blame] | 548 | "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "), |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 549 | }, |
| 550 | }) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 553 | func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string, |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 554 | jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string, |
| 555 | dirsToStrip []string) { |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 556 | |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 557 | var deps android.Paths |
| 558 | |
| 559 | var jarArgs []string |
| 560 | if manifest.Valid() { |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 561 | jarArgs = append(jarArgs, "-m ", manifest.String()) |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 562 | deps = append(deps, manifest.Path()) |
| 563 | } |
| 564 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 565 | for _, dir := range dirsToStrip { |
| 566 | jarArgs = append(jarArgs, "-stripDir ", dir) |
| 567 | } |
| 568 | |
| 569 | for _, file := range filesToStrip { |
| 570 | jarArgs = append(jarArgs, "-stripFile ", file) |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Colin Cross | 7b60cdd | 2017-12-21 13:52:58 -0800 | [diff] [blame] | 573 | // Remove any module-info.class files that may have come from prebuilt jars, they cause problems |
| 574 | // for downstream tools like desugar. |
| 575 | jarArgs = append(jarArgs, "-stripFile module-info.class") |
| 576 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 577 | if stripDirEntries { |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 578 | jarArgs = append(jarArgs, "-D") |
| 579 | } |
| 580 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 581 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 582 | Rule: combineJar, |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 583 | Description: desc, |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 584 | Output: outputFile, |
| 585 | Inputs: jars, |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 586 | Implicits: deps, |
| 587 | Args: map[string]string{ |
| 588 | "jarArgs": strings.Join(jarArgs, " "), |
| 589 | }, |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 590 | }) |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 593 | func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath, |
| 594 | classesJar android.Path, rulesFile android.Path) { |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 595 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 596 | Rule: jarjar, |
| 597 | Description: "jarjar", |
| 598 | Output: outputFile, |
| 599 | Input: classesJar, |
| 600 | Implicit: rulesFile, |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 601 | Args: map[string]string{ |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 602 | "rulesFile": rulesFile.String(), |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 603 | }, |
| 604 | }) |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 605 | } |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 606 | |
Colin Cross | b549b77 | 2020-06-03 17:14:31 +0000 | [diff] [blame] | 607 | func CheckJarPackages(ctx android.ModuleContext, outputFile android.WritablePath, |
| 608 | classesJar android.Path, permittedPackages []string) { |
Vladimir Marko | 0975ee0 | 2019-04-02 10:29:55 +0100 | [diff] [blame] | 609 | ctx.Build(pctx, android.BuildParams{ |
| 610 | Rule: packageCheck, |
| 611 | Description: "packageCheck", |
| 612 | Output: outputFile, |
| 613 | Input: classesJar, |
| 614 | Args: map[string]string{ |
| 615 | "packages": strings.Join(permittedPackages, " "), |
| 616 | }, |
| 617 | }) |
| 618 | } |
| 619 | |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 620 | func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath, |
| 621 | inputFile android.Path) { |
| 622 | ctx.Build(pctx, android.BuildParams{ |
| 623 | Rule: jetifier, |
| 624 | Description: "jetifier", |
| 625 | Output: outputFile, |
| 626 | Input: inputFile, |
| 627 | }) |
| 628 | } |
| 629 | |
Colin Cross | 094054a | 2018-10-17 15:10:48 -0700 | [diff] [blame] | 630 | func GenerateMainClassManifest(ctx android.ModuleContext, outputFile android.WritablePath, mainClass string) { |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 631 | android.WriteFileRule(ctx, outputFile, "Main-Class: "+mainClass+"\n") |
Colin Cross | 094054a | 2018-10-17 15:10:48 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 634 | func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path) { |
| 635 | ctx.Build(pctx, android.BuildParams{ |
| 636 | Rule: zipalign, |
| 637 | Description: "align", |
| 638 | Input: inputFile, |
| 639 | Output: outputFile, |
| 640 | }) |
| 641 | } |
| 642 | |
Colin Cross | 33961b5 | 2019-07-11 11:01:22 -0700 | [diff] [blame] | 643 | type classpath android.Paths |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 644 | |
Colin Cross | c2557d1 | 2019-10-31 15:22:57 -0700 | [diff] [blame] | 645 | func (x *classpath) formJoinedClassPath(optName string, sep string) string { |
Colin Cross | 8144008 | 2018-08-15 20:21:55 -0700 | [diff] [blame] | 646 | if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") { |
| 647 | optName += " " |
| 648 | } |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame] | 649 | if len(*x) > 0 { |
Colin Cross | c2557d1 | 2019-10-31 15:22:57 -0700 | [diff] [blame] | 650 | return optName + strings.Join(x.Strings(), sep) |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 651 | } else { |
| 652 | return "" |
| 653 | } |
| 654 | } |
Colin Cross | c2557d1 | 2019-10-31 15:22:57 -0700 | [diff] [blame] | 655 | func (x *classpath) FormJavaClassPath(optName string) string { |
| 656 | return x.formJoinedClassPath(optName, ":") |
| 657 | } |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 658 | |
Colin Cross | c2557d1 | 2019-10-31 15:22:57 -0700 | [diff] [blame] | 659 | func (x *classpath) FormTurbineClassPath(optName string) string { |
| 660 | return x.formJoinedClassPath(optName, " ") |
| 661 | } |
| 662 | |
| 663 | // FormRepeatedClassPath returns a list of arguments with the given optName prefixed to each element of the classpath. |
| 664 | func (x *classpath) FormRepeatedClassPath(optName string) []string { |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 665 | if x == nil || *x == nil { |
| 666 | return nil |
| 667 | } |
| 668 | flags := make([]string, len(*x)) |
| 669 | for i, v := range *x { |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 670 | flags[i] = optName + v.String() |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | return flags |
| 674 | } |
| 675 | |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 676 | // Convert a classpath to an android.Paths |
| 677 | func (x *classpath) Paths() android.Paths { |
| 678 | return append(android.Paths(nil), (*x)...) |
| 679 | } |
| 680 | |
| 681 | func (x *classpath) Strings() []string { |
| 682 | if x == nil { |
| 683 | return nil |
| 684 | } |
| 685 | ret := make([]string, len(*x)) |
| 686 | for i, path := range *x { |
| 687 | ret[i] = path.String() |
| 688 | } |
| 689 | return ret |
| 690 | } |
Colin Cross | b77043e | 2019-07-16 13:57:13 -0700 | [diff] [blame] | 691 | |
| 692 | type systemModules struct { |
| 693 | dir android.Path |
| 694 | deps android.Paths |
| 695 | } |
| 696 | |
Colin Cross | bf3119e | 2019-10-28 14:25:10 -0700 | [diff] [blame] | 697 | // Returns a --system argument in the form javac expects with -source 1.9 and the list of files to |
| 698 | // depend on. If forceEmpty is true, returns --system=none if the list is empty to ensure javac |
| 699 | // does not fall back to the default system modules. |
Colin Cross | b77043e | 2019-07-16 13:57:13 -0700 | [diff] [blame] | 700 | func (x *systemModules) FormJavaSystemModulesPath(forceEmpty bool) (string, android.Paths) { |
| 701 | if x != nil { |
| 702 | return "--system=" + x.dir.String(), x.deps |
| 703 | } else if forceEmpty { |
| 704 | return "--system=none", nil |
| 705 | } else { |
| 706 | return "", nil |
| 707 | } |
| 708 | } |
Colin Cross | bf3119e | 2019-10-28 14:25:10 -0700 | [diff] [blame] | 709 | |
| 710 | // Returns a --system argument in the form turbine expects with -source 1.9 and the list of files to |
| 711 | // depend on. If forceEmpty is true, returns --bootclasspath "" if the list is empty to ensure turbine |
| 712 | // does not fall back to the default bootclasspath. |
| 713 | func (x *systemModules) FormTurbineSystemModulesPath(forceEmpty bool) (string, android.Paths) { |
| 714 | if x != nil { |
| 715 | return "--system " + x.dir.String(), x.deps |
| 716 | } else if forceEmpty { |
| 717 | return `--bootclasspath ""`, nil |
| 718 | } else { |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame^] | 719 | return "--system ${config.JavaHome}", nil |
Colin Cross | bf3119e | 2019-10-28 14:25:10 -0700 | [diff] [blame] | 720 | } |
| 721 | } |