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" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | var ( |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 33 | pctx = android.NewPackageContext("android/soong/java") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 34 | |
| 35 | // Compiling java is not conducive to proper dependency tracking. The path-matches-class-name |
| 36 | // requirement leads to unpredictable generated source file names, and a single .java file |
| 37 | // will get compiled into multiple .class files if it contains inner classes. To work around |
Colin Cross | f7eac7a | 2018-02-08 12:48:39 -0800 | [diff] [blame] | 38 | // this, all java rules write into separate directories and then are combined into a .jar file |
| 39 | // (if the rule produces .class files) or a .srcjar file (if the rule produces .java files). |
| 40 | // .srcjar files are unzipped into a temporary directory when compiled with javac. |
Yoshisato Yanagisawa | 572324a | 2017-06-05 17:41:50 +0900 | [diff] [blame] | 41 | javac = pctx.AndroidGomaStaticRule("javac", |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 42 | blueprint.RuleParams{ |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 43 | Command: `rm -rf "$outDir" "$annoDir" "$srcJarDir" && mkdir -p "$outDir" "$annoDir" "$srcJarDir" && ` + |
Colin Cross | 436b765 | 2018-03-15 16:24:10 -0700 | [diff] [blame] | 44 | `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` + |
Sundong Ahn | 24a099c | 2018-06-28 14:53:20 +0900 | [diff] [blame] | 45 | `(if [ -s $srcJarDir/list ] || [ -s $out.rsp ] ; then ` + |
Colin Cross | a482065 | 2017-10-17 13:56:52 -0700 | [diff] [blame] | 46 | `${config.SoongJavacWrapper} ${config.JavacWrapper}${config.JavacCmd} ${config.JavacHeapFlags} ${config.CommonJdkFlags} ` + |
Colin Cross | 6a77c98 | 2018-06-19 22:43:34 -0700 | [diff] [blame] | 47 | `$processorpath $javacFlags $bootClasspath $classpath ` + |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 48 | `-source $javaVersion -target $javaVersion ` + |
Sundong Ahn | 24a099c | 2018-06-28 14:53:20 +0900 | [diff] [blame] | 49 | `-d $outDir -s $annoDir @$out.rsp @$srcJarDir/list ; fi ) && ` + |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 50 | `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`, |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 51 | CommandDeps: []string{ |
| 52 | "${config.JavacCmd}", |
| 53 | "${config.SoongZipCmd}", |
Colin Cross | 436b765 | 2018-03-15 16:24:10 -0700 | [diff] [blame] | 54 | "${config.ZipSyncCmd}", |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 55 | }, |
Colin Cross | a482065 | 2017-10-17 13:56:52 -0700 | [diff] [blame] | 56 | CommandOrderOnly: []string{"${config.SoongJavacWrapper}"}, |
| 57 | Rspfile: "$out.rsp", |
| 58 | RspfileContent: "$in", |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 59 | }, |
Colin Cross | 6a77c98 | 2018-06-19 22:43:34 -0700 | [diff] [blame] | 60 | "javacFlags", "bootClasspath", "classpath", "processorpath", "srcJars", "srcJarDir", |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 61 | "outDir", "annoDir", "javaVersion") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 62 | |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 63 | kotlinc = pctx.AndroidGomaStaticRule("kotlinc", |
| 64 | blueprint.RuleParams{ |
Colin Cross | 0f40a75 | 2018-09-08 11:52:49 -0700 | [diff] [blame] | 65 | Command: `rm -rf "$classesDir" "$srcJarDir" "$kotlinBuildFile" && mkdir -p "$classesDir" "$srcJarDir" && ` + |
Colin Cross | 436b765 | 2018-03-15 16:24:10 -0700 | [diff] [blame] | 66 | `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` + |
Colin Cross | 0f40a75 | 2018-09-08 11:52:49 -0700 | [diff] [blame] | 67 | `${config.GenKotlinBuildFileCmd} $classpath $classesDir $out.rsp $srcJarDir/list > $kotlinBuildFile &&` + |
Przemyslaw Szczepaniak | 4b5fe9d | 2018-02-13 14:32:54 +0000 | [diff] [blame] | 68 | `${config.KotlincCmd} $kotlincFlags ` + |
Colin Cross | 0f40a75 | 2018-09-08 11:52:49 -0700 | [diff] [blame] | 69 | `-jvm-target $kotlinJvmTarget -Xbuild-file=$kotlinBuildFile && ` + |
| 70 | `${config.SoongZipCmd} -jar -o $out -C $classesDir -D $classesDir`, |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 71 | CommandDeps: []string{ |
| 72 | "${config.KotlincCmd}", |
| 73 | "${config.KotlinCompilerJar}", |
Przemyslaw Szczepaniak | 4b5fe9d | 2018-02-13 14:32:54 +0000 | [diff] [blame] | 74 | "${config.GenKotlinBuildFileCmd}", |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 75 | "${config.SoongZipCmd}", |
Colin Cross | 436b765 | 2018-03-15 16:24:10 -0700 | [diff] [blame] | 76 | "${config.ZipSyncCmd}", |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 77 | }, |
Przemyslaw Szczepaniak | 4b5fe9d | 2018-02-13 14:32:54 +0000 | [diff] [blame] | 78 | Rspfile: "$out.rsp", |
| 79 | RspfileContent: `$in`, |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 80 | }, |
Colin Cross | 0f40a75 | 2018-09-08 11:52:49 -0700 | [diff] [blame] | 81 | "kotlincFlags", "classpath", "srcJars", "srcJarDir", "classesDir", "kotlinJvmTarget", "kotlinBuildFile") |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 82 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 83 | turbine = pctx.AndroidStaticRule("turbine", |
| 84 | blueprint.RuleParams{ |
Colin Cross | 6981f65 | 2018-03-07 15:14:50 -0800 | [diff] [blame] | 85 | Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 86 | `${config.JavaCmd} -jar ${config.TurbineJar} --output $out.tmp ` + |
Colin Cross | 6981f65 | 2018-03-07 15:14:50 -0800 | [diff] [blame] | 87 | `--temp_dir "$outDir" --sources @$out.rsp --source_jars $srcJars ` + |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 88 | `--javacopts ${config.CommonJdkFlags} ` + |
Colin Cross | 924a0aa | 2018-03-07 10:51:05 -0800 | [diff] [blame] | 89 | `$javacFlags -source $javaVersion -target $javaVersion -- $bootClasspath $classpath && ` + |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 90 | `${config.Ziptime} $out.tmp && ` + |
| 91 | `(if cmp -s $out.tmp $out ; then rm $out.tmp ; else mv $out.tmp $out ; fi )`, |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 92 | CommandDeps: []string{ |
| 93 | "${config.TurbineJar}", |
| 94 | "${config.JavaCmd}", |
| 95 | "${config.Ziptime}", |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 96 | }, |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 97 | Rspfile: "$out.rsp", |
| 98 | RspfileContent: "$in", |
| 99 | Restat: true, |
| 100 | }, |
Colin Cross | 6981f65 | 2018-03-07 15:14:50 -0800 | [diff] [blame] | 101 | "javacFlags", "bootClasspath", "classpath", "srcJars", "outDir", "javaVersion") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 102 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 103 | jar = pctx.AndroidStaticRule("jar", |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 104 | blueprint.RuleParams{ |
Nan Zhang | 674dd93 | 2018-01-26 18:30:36 -0800 | [diff] [blame] | 105 | Command: `${config.SoongZipCmd} -jar -o $out @$out.rsp`, |
| 106 | CommandDeps: []string{"${config.SoongZipCmd}"}, |
| 107 | Rspfile: "$out.rsp", |
| 108 | RspfileContent: "$jarArgs", |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 109 | }, |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 110 | "jarArgs") |
| 111 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 112 | zip = pctx.AndroidStaticRule("zip", |
| 113 | blueprint.RuleParams{ |
| 114 | Command: `${config.SoongZipCmd} -o $out @$out.rsp`, |
| 115 | CommandDeps: []string{"${config.SoongZipCmd}"}, |
| 116 | Rspfile: "$out.rsp", |
| 117 | RspfileContent: "$jarArgs", |
| 118 | }, |
| 119 | "jarArgs") |
| 120 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 121 | combineJar = pctx.AndroidStaticRule("combineJar", |
| 122 | blueprint.RuleParams{ |
Colin Cross | f91a08c | 2018-02-07 15:41:31 -0800 | [diff] [blame] | 123 | Command: `${config.MergeZipsCmd} --ignore-duplicates -j $jarArgs $out $in`, |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 124 | CommandDeps: []string{"${config.MergeZipsCmd}"}, |
| 125 | }, |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 126 | "jarArgs") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 127 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 128 | jarjar = pctx.AndroidStaticRule("jarjar", |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 129 | blueprint.RuleParams{ |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 130 | Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out", |
| 131 | CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"}, |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 132 | }, |
| 133 | "rulesFile") |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 134 | |
| 135 | jetifier = pctx.AndroidStaticRule("jetifier", |
| 136 | blueprint.RuleParams{ |
| 137 | Command: "${config.JavaCmd} -jar ${config.JetifierJar} -l error -o $out -i $in", |
| 138 | CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"}, |
| 139 | }, |
| 140 | ) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 141 | ) |
| 142 | |
| 143 | func init() { |
Colin Cross | 3063b78 | 2018-08-15 11:19:12 -0700 | [diff] [blame] | 144 | pctx.Import("android/soong/common") |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 145 | pctx.Import("android/soong/java/config") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | type javaBuilderFlags struct { |
| 149 | javacFlags string |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 150 | bootClasspath classpath |
| 151 | classpath classpath |
Colin Cross | 6a77c98 | 2018-06-19 22:43:34 -0700 | [diff] [blame] | 152 | processorPath classpath |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 153 | systemModules classpath |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 154 | aidlFlags string |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 155 | javaVersion string |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 156 | |
Andreas Gampe | f3e5b55 | 2018-01-22 21:27:21 -0800 | [diff] [blame] | 157 | errorProneExtraJavacFlags string |
Colin Cross | 6654810 | 2018-06-19 22:47:35 -0700 | [diff] [blame] | 158 | errorProneProcessorPath classpath |
Andreas Gampe | f3e5b55 | 2018-01-22 21:27:21 -0800 | [diff] [blame] | 159 | |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 160 | kotlincFlags string |
| 161 | kotlincClasspath classpath |
| 162 | |
Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 163 | protoFlags []string |
| 164 | protoOutTypeFlag string // The flag itself: --java_out |
| 165 | protoOutParams string // Parameters to that flag: --java_out=$protoOutParams:$outDir |
Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 166 | protoRoot bool |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 169 | func TransformKotlinToClasses(ctx android.ModuleContext, outputFile android.WritablePath, |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 170 | srcFiles, srcJars android.Paths, |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 171 | flags javaBuilderFlags) { |
| 172 | |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 173 | inputs := append(android.Paths(nil), srcFiles...) |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 174 | |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 175 | var deps android.Paths |
| 176 | deps = append(deps, flags.kotlincClasspath...) |
Przemyslaw Szczepaniak | 4b5fe9d | 2018-02-13 14:32:54 +0000 | [diff] [blame] | 177 | deps = append(deps, srcJars...) |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 178 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 179 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 180 | Rule: kotlinc, |
| 181 | Description: "kotlinc", |
| 182 | Output: outputFile, |
| 183 | Inputs: inputs, |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 184 | Implicits: deps, |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 185 | Args: map[string]string{ |
Colin Cross | 0f40a75 | 2018-09-08 11:52:49 -0700 | [diff] [blame] | 186 | "classpath": flags.kotlincClasspath.FormJavaClassPath("-classpath"), |
| 187 | "kotlincFlags": flags.kotlincFlags, |
| 188 | "srcJars": strings.Join(srcJars.Strings(), " "), |
| 189 | "classesDir": android.PathForModuleOut(ctx, "kotlinc", "classes").String(), |
| 190 | "srcJarDir": android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(), |
| 191 | "kotlinBuildFile": android.PathForModuleOut(ctx, "kotlinc-build.xml").String(), |
Tobias Thierer | 96427d6 | 2017-11-18 03:10:51 +0000 | [diff] [blame] | 192 | // http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8 |
| 193 | "kotlinJvmTarget": "1.8", |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 194 | }, |
| 195 | }) |
| 196 | } |
| 197 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 198 | func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, shardIdx int, |
| 199 | srcFiles, srcJars android.Paths, flags javaBuilderFlags, deps android.Paths) { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 200 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 201 | // Compile java sources into .class files |
| 202 | desc := "javac" |
| 203 | if shardIdx >= 0 { |
| 204 | desc += strconv.Itoa(shardIdx) |
| 205 | } |
| 206 | |
Colin Cross | 6654810 | 2018-06-19 22:47:35 -0700 | [diff] [blame] | 207 | transformJavaToClasses(ctx, outputFile, shardIdx, srcFiles, srcJars, flags, deps, "javac", desc) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 210 | func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath, |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 211 | srcFiles, srcJars android.Paths, flags javaBuilderFlags) { |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 212 | |
Colin Cross | 6654810 | 2018-06-19 22:47:35 -0700 | [diff] [blame] | 213 | flags.processorPath = append(flags.errorProneProcessorPath, flags.processorPath...) |
Colin Cross | fee57cb | 2017-09-05 13:16:45 -0700 | [diff] [blame] | 214 | |
Andreas Gampe | f3e5b55 | 2018-01-22 21:27:21 -0800 | [diff] [blame] | 215 | if len(flags.errorProneExtraJavacFlags) > 0 { |
| 216 | if len(flags.javacFlags) > 0 { |
Colin Cross | 6654810 | 2018-06-19 22:47:35 -0700 | [diff] [blame] | 217 | flags.javacFlags += " " + flags.errorProneExtraJavacFlags |
Andreas Gampe | f3e5b55 | 2018-01-22 21:27:21 -0800 | [diff] [blame] | 218 | } else { |
| 219 | flags.javacFlags = flags.errorProneExtraJavacFlags |
| 220 | } |
| 221 | } |
| 222 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 223 | transformJavaToClasses(ctx, outputFile, -1, srcFiles, srcJars, flags, nil, |
Colin Cross | 6654810 | 2018-06-19 22:47:35 -0700 | [diff] [blame] | 224 | "errorprone", "errorprone") |
Colin Cross | 070879e | 2017-10-11 11:21:07 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 227 | func TransformJavaToHeaderClasses(ctx android.ModuleContext, outputFile android.WritablePath, |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 228 | srcFiles, srcJars android.Paths, flags javaBuilderFlags) { |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 229 | |
| 230 | var deps android.Paths |
| 231 | deps = append(deps, srcJars...) |
| 232 | deps = append(deps, flags.bootClasspath...) |
| 233 | deps = append(deps, flags.classpath...) |
| 234 | |
| 235 | var bootClasspath string |
| 236 | if len(flags.bootClasspath) == 0 && ctx.Device() { |
| 237 | // explicitly specify -bootclasspath "" if the bootclasspath is empty to |
| 238 | // ensure java does not fall back to the default bootclasspath. |
| 239 | bootClasspath = `--bootclasspath ""` |
| 240 | } else { |
Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 241 | bootClasspath = strings.Join(flags.bootClasspath.FormTurbineClasspath("--bootclasspath"), " ") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 242 | } |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 243 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 244 | ctx.Build(pctx, android.BuildParams{ |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 245 | Rule: turbine, |
| 246 | Description: "turbine", |
| 247 | Output: outputFile, |
| 248 | Inputs: srcFiles, |
| 249 | Implicits: deps, |
| 250 | Args: map[string]string{ |
| 251 | "javacFlags": flags.javacFlags, |
| 252 | "bootClasspath": bootClasspath, |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 253 | "srcJars": strings.Join(srcJars.Strings(), " "), |
Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 254 | "classpath": strings.Join(flags.classpath.FormTurbineClasspath("--classpath"), " "), |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 255 | "outDir": android.PathForModuleOut(ctx, "turbine", "classes").String(), |
| 256 | "javaVersion": flags.javaVersion, |
| 257 | }, |
| 258 | }) |
| 259 | } |
| 260 | |
Colin Cross | 070879e | 2017-10-11 11:21:07 -0700 | [diff] [blame] | 261 | // 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] | 262 | // srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain |
| 263 | // 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] | 264 | // |
| 265 | // This method may be used for different compilers, including javac and Error Prone. The rule |
| 266 | // argument specifies which command line to use and desc sets the description of the rule that will |
| 267 | // be printed at build time. The stem argument provides the file name of the output jar, and |
| 268 | // suffix will be appended to various intermediate files and directories to avoid collisions when |
| 269 | // this function is called twice in the same module directory. |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 270 | func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 271 | shardIdx int, srcFiles, srcJars android.Paths, |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 272 | flags javaBuilderFlags, deps android.Paths, |
Colin Cross | 6654810 | 2018-06-19 22:47:35 -0700 | [diff] [blame] | 273 | intermediatesDir, desc string) { |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 274 | |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame] | 275 | deps = append(deps, srcJars...) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 276 | |
| 277 | var bootClasspath string |
| 278 | if flags.javaVersion == "1.9" { |
| 279 | deps = append(deps, flags.systemModules...) |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 280 | bootClasspath = flags.systemModules.FormJavaSystemModulesPath("--system=", ctx.Device()) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 281 | } else { |
| 282 | deps = append(deps, flags.bootClasspath...) |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 283 | if len(flags.bootClasspath) == 0 && ctx.Device() { |
| 284 | // explicitly specify -bootclasspath "" if the bootclasspath is empty to |
| 285 | // ensure java does not fall back to the default bootclasspath. |
| 286 | bootClasspath = `-bootclasspath ""` |
| 287 | } else { |
| 288 | bootClasspath = flags.bootClasspath.FormJavaClassPath("-bootclasspath") |
| 289 | } |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 292 | deps = append(deps, flags.classpath...) |
Colin Cross | 6a77c98 | 2018-06-19 22:43:34 -0700 | [diff] [blame] | 293 | deps = append(deps, flags.processorPath...) |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 294 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 295 | srcJarDir := "srcjars" |
| 296 | outDir := "classes" |
| 297 | annoDir := "anno" |
| 298 | if shardIdx >= 0 { |
| 299 | shardDir := "shard" + strconv.Itoa(shardIdx) |
| 300 | srcJarDir = filepath.Join(shardDir, srcJarDir) |
| 301 | outDir = filepath.Join(shardDir, outDir) |
| 302 | annoDir = filepath.Join(shardDir, annoDir) |
| 303 | } |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 304 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 6654810 | 2018-06-19 22:47:35 -0700 | [diff] [blame] | 305 | Rule: javac, |
Colin Cross | 070879e | 2017-10-11 11:21:07 -0700 | [diff] [blame] | 306 | Description: desc, |
| 307 | Output: outputFile, |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 308 | Inputs: srcFiles, |
| 309 | Implicits: deps, |
| 310 | Args: map[string]string{ |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame] | 311 | "javacFlags": flags.javacFlags, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 312 | "bootClasspath": bootClasspath, |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 313 | "classpath": flags.classpath.FormJavaClassPath("-classpath"), |
Colin Cross | 6a77c98 | 2018-06-19 22:43:34 -0700 | [diff] [blame] | 314 | "processorpath": flags.processorPath.FormJavaClassPath("-processorpath"), |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 315 | "srcJars": strings.Join(srcJars.Strings(), " "), |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 316 | "srcJarDir": android.PathForModuleOut(ctx, intermediatesDir, srcJarDir).String(), |
| 317 | "outDir": android.PathForModuleOut(ctx, intermediatesDir, outDir).String(), |
| 318 | "annoDir": android.PathForModuleOut(ctx, intermediatesDir, annoDir).String(), |
Colin Cross | 8eadbf0 | 2017-10-24 17:46:00 -0700 | [diff] [blame] | 319 | "javaVersion": flags.javaVersion, |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 320 | }, |
| 321 | }) |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 324 | func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath, |
| 325 | jarArgs []string, deps android.Paths) { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 326 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 327 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 328 | Rule: jar, |
| 329 | Description: "jar", |
| 330 | Output: outputFile, |
| 331 | Implicits: deps, |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 332 | Args: map[string]string{ |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 333 | "jarArgs": strings.Join(proptools.NinjaAndShellEscape(jarArgs), " "), |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 334 | }, |
| 335 | }) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 338 | func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string, |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 339 | jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string, |
| 340 | dirsToStrip []string) { |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 341 | |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 342 | var deps android.Paths |
| 343 | |
| 344 | var jarArgs []string |
| 345 | if manifest.Valid() { |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 346 | jarArgs = append(jarArgs, "-m ", manifest.String()) |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 347 | deps = append(deps, manifest.Path()) |
| 348 | } |
| 349 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 350 | for _, dir := range dirsToStrip { |
| 351 | jarArgs = append(jarArgs, "-stripDir ", dir) |
| 352 | } |
| 353 | |
| 354 | for _, file := range filesToStrip { |
| 355 | jarArgs = append(jarArgs, "-stripFile ", file) |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Colin Cross | 7b60cdd | 2017-12-21 13:52:58 -0800 | [diff] [blame] | 358 | // Remove any module-info.class files that may have come from prebuilt jars, they cause problems |
| 359 | // for downstream tools like desugar. |
| 360 | jarArgs = append(jarArgs, "-stripFile module-info.class") |
| 361 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 362 | if stripDirEntries { |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 363 | jarArgs = append(jarArgs, "-D") |
| 364 | } |
| 365 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 366 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 367 | Rule: combineJar, |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 368 | Description: desc, |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 369 | Output: outputFile, |
| 370 | Inputs: jars, |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 371 | Implicits: deps, |
| 372 | Args: map[string]string{ |
| 373 | "jarArgs": strings.Join(jarArgs, " "), |
| 374 | }, |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 375 | }) |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 378 | func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath, |
| 379 | classesJar android.Path, rulesFile android.Path) { |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 380 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 381 | Rule: jarjar, |
| 382 | Description: "jarjar", |
| 383 | Output: outputFile, |
| 384 | Input: classesJar, |
| 385 | Implicit: rulesFile, |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 386 | Args: map[string]string{ |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 387 | "rulesFile": rulesFile.String(), |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 388 | }, |
| 389 | }) |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 390 | } |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 391 | |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 392 | func TransformJetifier(ctx android.ModuleContext, outputFile android.WritablePath, |
| 393 | inputFile android.Path) { |
| 394 | ctx.Build(pctx, android.BuildParams{ |
| 395 | Rule: jetifier, |
| 396 | Description: "jetifier", |
| 397 | Output: outputFile, |
| 398 | Input: inputFile, |
| 399 | }) |
| 400 | } |
| 401 | |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 402 | type classpath []android.Path |
| 403 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 404 | func (x *classpath) FormJavaClassPath(optName string) string { |
Colin Cross | 8144008 | 2018-08-15 20:21:55 -0700 | [diff] [blame] | 405 | if optName != "" && !strings.HasSuffix(optName, "=") && !strings.HasSuffix(optName, " ") { |
| 406 | optName += " " |
| 407 | } |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame] | 408 | if len(*x) > 0 { |
Colin Cross | 8144008 | 2018-08-15 20:21:55 -0700 | [diff] [blame] | 409 | return optName + strings.Join(x.Strings(), ":") |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 410 | } else { |
| 411 | return "" |
| 412 | } |
| 413 | } |
| 414 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 415 | // Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true, |
| 416 | // returns --system=none if the list is empty to ensure javac does not fall back to the default |
| 417 | // system modules. |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 418 | func (x *classpath) FormJavaSystemModulesPath(optName string, forceEmpty bool) string { |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 419 | if len(*x) > 1 { |
| 420 | panic("more than one system module") |
| 421 | } else if len(*x) == 1 { |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 422 | return optName + strings.TrimSuffix((*x)[0].String(), "lib/modules") |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 423 | } else if forceEmpty { |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 424 | return optName + "none" |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 425 | } else { |
| 426 | return "" |
| 427 | } |
| 428 | } |
| 429 | |
Colin Cross | bafb897 | 2018-06-06 21:46:32 +0000 | [diff] [blame] | 430 | func (x *classpath) FormTurbineClasspath(optName string) []string { |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 431 | if x == nil || *x == nil { |
| 432 | return nil |
| 433 | } |
| 434 | flags := make([]string, len(*x)) |
| 435 | for i, v := range *x { |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 436 | flags[i] = optName + " " + v.String() |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | return flags |
| 440 | } |
| 441 | |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 442 | // Convert a classpath to an android.Paths |
| 443 | func (x *classpath) Paths() android.Paths { |
| 444 | return append(android.Paths(nil), (*x)...) |
| 445 | } |
| 446 | |
| 447 | func (x *classpath) Strings() []string { |
| 448 | if x == nil { |
| 449 | return nil |
| 450 | } |
| 451 | ret := make([]string, len(*x)) |
| 452 | for i, path := range *x { |
| 453 | ret[i] = path.String() |
| 454 | } |
| 455 | return ret |
| 456 | } |