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 ( |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 22 | "strings" |
| 23 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 24 | "github.com/google/blueprint" |
Colin Cross | fee57cb | 2017-09-05 13:16:45 -0700 | [diff] [blame] | 25 | |
| 26 | "android/soong/android" |
| 27 | "android/soong/java/config" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | var ( |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 31 | pctx = android.NewPackageContext("android/soong/java") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 32 | |
| 33 | // Compiling java is not conducive to proper dependency tracking. The path-matches-class-name |
| 34 | // requirement leads to unpredictable generated source file names, and a single .java file |
| 35 | // will get compiled into multiple .class files if it contains inner classes. To work around |
| 36 | // this, all java rules write into separate directories and then a post-processing step lists |
| 37 | // the files in the the directory into a list file that later rules depend on (and sometimes |
| 38 | // read from directly using @<listfile>) |
Yoshisato Yanagisawa | 572324a | 2017-06-05 17:41:50 +0900 | [diff] [blame] | 39 | javac = pctx.AndroidGomaStaticRule("javac", |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 40 | blueprint.RuleParams{ |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 41 | Command: `rm -rf "$outDir" "$annoDir" && mkdir -p "$outDir" "$annoDir" && ` + |
Colin Cross | 3203dde | 2017-08-28 17:23:21 -0700 | [diff] [blame] | 42 | `${config.JavacWrapper}${config.JavacCmd} ${config.JavacHeapFlags} ${config.CommonJdkFlags} ` + |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame^] | 43 | `$javacFlags $sourcepath $bootClasspath $classpath ` + |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 44 | `-source $javaVersion -target $javaVersion ` + |
Colin Cross | b852a58 | 2017-08-10 17:58:12 -0700 | [diff] [blame] | 45 | `-d $outDir -s $annoDir @$out.rsp && ` + |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 46 | `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`, |
| 47 | CommandDeps: []string{"${config.JavacCmd}", "${config.SoongZipCmd}"}, |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 48 | Rspfile: "$out.rsp", |
| 49 | RspfileContent: "$in", |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 50 | }, |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame^] | 51 | "javacFlags", "sourcepath", "bootClasspath", "classpath", "outDir", "annoDir", "javaVersion") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 52 | |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 53 | errorprone = pctx.AndroidStaticRule("errorprone", |
| 54 | blueprint.RuleParams{ |
| 55 | Command: `rm -rf "$outDir" "$annoDir" && mkdir -p "$outDir" "$annoDir" && ` + |
Colin Cross | 3a5de65 | 2017-09-11 23:10:21 -0700 | [diff] [blame] | 56 | `${config.ErrorProneCmd} ` + |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame^] | 57 | `$javacFlags $sourcepath $bootClasspath $classpath ` + |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 58 | `-source $javaVersion -target $javaVersion ` + |
| 59 | `-d $outDir -s $annoDir @$out.rsp && ` + |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 60 | `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`, |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 61 | CommandDeps: []string{ |
| 62 | "${config.JavaCmd}", |
| 63 | "${config.ErrorProneJavacJar}", |
| 64 | "${config.ErrorProneJar}", |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 65 | "${config.SoongZipCmd}", |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 66 | }, |
| 67 | Rspfile: "$out.rsp", |
| 68 | RspfileContent: "$in", |
| 69 | }, |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame^] | 70 | "javacFlags", "sourcepath", "bootClasspath", "classpath", "outDir", "annoDir", "javaVersion") |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 71 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 72 | jar = pctx.AndroidStaticRule("jar", |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 73 | blueprint.RuleParams{ |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 74 | Command: `${config.SoongZipCmd} -jar -o $out $jarArgs`, |
| 75 | CommandDeps: []string{"${config.SoongZipCmd}"}, |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 76 | }, |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 77 | "jarArgs") |
| 78 | |
| 79 | combineJar = pctx.AndroidStaticRule("combineJar", |
| 80 | blueprint.RuleParams{ |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 81 | Command: `${config.MergeZipsCmd} -j $jarArgs $out $in`, |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 82 | CommandDeps: []string{"${config.MergeZipsCmd}"}, |
| 83 | }, |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 84 | "jarArgs") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 85 | |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 86 | desugar = pctx.AndroidStaticRule("desugar", |
| 87 | blueprint.RuleParams{ |
| 88 | Command: `rm -rf $dumpDir && mkdir -p $dumpDir && ` + |
| 89 | `${config.JavaCmd} ` + |
| 90 | `-Djdk.internal.lambda.dumpProxyClasses=$$(cd $dumpDir && pwd) ` + |
| 91 | `$javaFlags ` + |
| 92 | `-jar ${config.DesugarJar} $classpathFlags $desugarFlags ` + |
| 93 | `-i $in -o $out`, |
| 94 | CommandDeps: []string{"${config.DesugarJar}"}, |
| 95 | }, |
| 96 | "javaFlags", "classpathFlags", "desugarFlags", "dumpDir") |
| 97 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 98 | dx = pctx.AndroidStaticRule("dx", |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 99 | blueprint.RuleParams{ |
Colin Cross | 6d1e72d | 2015-04-10 17:44:24 -0700 | [diff] [blame] | 100 | Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 101 | `${config.DxCmd} --dex --output=$outDir $dxFlags $in && ` + |
Colin Cross | 7db5d63 | 2017-10-04 17:07:09 -0700 | [diff] [blame] | 102 | `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` + |
| 103 | `${config.MergeZipsCmd} -D -stripFile "*.class" $out $outDir/classes.dex.jar $in`, |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 104 | CommandDeps: []string{ |
| 105 | "${config.DxCmd}", |
| 106 | "${config.SoongZipCmd}", |
Colin Cross | 7db5d63 | 2017-10-04 17:07:09 -0700 | [diff] [blame] | 107 | "${config.MergeZipsCmd}", |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 108 | }, |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 109 | }, |
| 110 | "outDir", "dxFlags") |
Colin Cross | e1d62a8 | 2015-04-03 16:53:05 -0700 | [diff] [blame] | 111 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 112 | jarjar = pctx.AndroidStaticRule("jarjar", |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 113 | blueprint.RuleParams{ |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 114 | Command: "${config.JavaCmd} -jar ${config.JarjarCmd} process $rulesFile $in $out", |
| 115 | CommandDeps: []string{"${config.JavaCmd}", "${config.JarjarCmd}", "$rulesFile"}, |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 116 | }, |
| 117 | "rulesFile") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 118 | ) |
| 119 | |
| 120 | func init() { |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 121 | pctx.Import("android/soong/java/config") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | type javaBuilderFlags struct { |
| 125 | javacFlags string |
| 126 | dxFlags string |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 127 | bootClasspath classpath |
| 128 | classpath classpath |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 129 | systemModules classpath |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 130 | desugarFlags string |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 131 | aidlFlags string |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 132 | javaVersion string |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 133 | |
| 134 | protoFlags string |
| 135 | protoOutFlag string |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 138 | func TransformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame^] | 139 | srcFiles android.Paths, srcJars classpath, |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 140 | flags javaBuilderFlags, deps android.Paths) { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 141 | |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame^] | 142 | transformJavaToClasses(ctx, outputFile, srcFiles, srcJars, flags, deps, |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 143 | "", "javac", javac) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 146 | func RunErrorProne(ctx android.ModuleContext, outputFile android.WritablePath, |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame^] | 147 | srcFiles android.Paths, srcJars classpath, |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 148 | flags javaBuilderFlags) { |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 149 | |
Colin Cross | fee57cb | 2017-09-05 13:16:45 -0700 | [diff] [blame] | 150 | if config.ErrorProneJar == "" { |
| 151 | ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?") |
Colin Cross | fee57cb | 2017-09-05 13:16:45 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame^] | 154 | transformJavaToClasses(ctx, outputFile, srcFiles, srcJars, flags, nil, |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 155 | "-errorprone", "errorprone", errorprone) |
Colin Cross | 070879e | 2017-10-11 11:21:07 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | // 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^] | 159 | // srcFiles is a list of paths to sources, srcJars is a list of paths to jar files that contain |
| 160 | // 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] | 161 | // |
| 162 | // This method may be used for different compilers, including javac and Error Prone. The rule |
| 163 | // argument specifies which command line to use and desc sets the description of the rule that will |
| 164 | // be printed at build time. The stem argument provides the file name of the output jar, and |
| 165 | // suffix will be appended to various intermediate files and directories to avoid collisions when |
| 166 | // this function is called twice in the same module directory. |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 167 | func transformJavaToClasses(ctx android.ModuleContext, outputFile android.WritablePath, |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame^] | 168 | srcFiles android.Paths, srcJars classpath, |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 169 | flags javaBuilderFlags, deps android.Paths, |
| 170 | intermediatesSuffix, desc string, rule blueprint.Rule) { |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 171 | |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame^] | 172 | deps = append(deps, srcJars...) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 173 | |
| 174 | var bootClasspath string |
| 175 | if flags.javaVersion == "1.9" { |
| 176 | deps = append(deps, flags.systemModules...) |
| 177 | bootClasspath = flags.systemModules.JavaSystemModules(ctx.Device()) |
| 178 | } else { |
| 179 | deps = append(deps, flags.bootClasspath...) |
| 180 | bootClasspath = flags.bootClasspath.JavaBootClasspath(ctx.Device()) |
| 181 | } |
| 182 | |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 183 | deps = append(deps, flags.classpath...) |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 184 | |
| 185 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 070879e | 2017-10-11 11:21:07 -0700 | [diff] [blame] | 186 | Rule: rule, |
| 187 | Description: desc, |
| 188 | Output: outputFile, |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 189 | Inputs: srcFiles, |
| 190 | Implicits: deps, |
| 191 | Args: map[string]string{ |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame^] | 192 | "javacFlags": flags.javacFlags, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 193 | "bootClasspath": bootClasspath, |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame^] | 194 | "sourcepath": srcJars.JavaSourcepath(), |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 195 | "classpath": flags.classpath.JavaClasspath(), |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 196 | "outDir": android.PathForModuleOut(ctx, "classes"+intermediatesSuffix).String(), |
| 197 | "annoDir": android.PathForModuleOut(ctx, "anno"+intermediatesSuffix).String(), |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 198 | "javaVersion": flags.javaVersion, |
| 199 | }, |
| 200 | }) |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 203 | func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.WritablePath, |
| 204 | jarArgs []string, deps android.Paths) { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 205 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 206 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 207 | Rule: jar, |
| 208 | Description: "jar", |
| 209 | Output: outputFile, |
| 210 | Implicits: deps, |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 211 | Args: map[string]string{ |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 212 | "jarArgs": strings.Join(jarArgs, " "), |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 213 | }, |
| 214 | }) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 217 | func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, |
| 218 | jars android.Paths, manifest android.OptionalPath, stripDirs bool) { |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 219 | |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 220 | var deps android.Paths |
| 221 | |
| 222 | var jarArgs []string |
| 223 | if manifest.Valid() { |
| 224 | jarArgs = append(jarArgs, "-m "+manifest.String()) |
| 225 | deps = append(deps, manifest.Path()) |
| 226 | } |
| 227 | |
| 228 | if stripDirs { |
| 229 | jarArgs = append(jarArgs, "-D") |
| 230 | } |
| 231 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 232 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
| 233 | Rule: combineJar, |
| 234 | Description: "combine jars", |
| 235 | Output: outputFile, |
| 236 | Inputs: jars, |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 237 | Implicits: deps, |
| 238 | Args: map[string]string{ |
| 239 | "jarArgs": strings.Join(jarArgs, " "), |
| 240 | }, |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 241 | }) |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 244 | func TransformDesugar(ctx android.ModuleContext, outputFile android.WritablePath, |
| 245 | classesJar android.Path, flags javaBuilderFlags) { |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 246 | |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 247 | dumpDir := android.PathForModuleOut(ctx, "desugar_dumped_classes") |
| 248 | |
| 249 | javaFlags := "" |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 250 | if ctx.AConfig().UseOpenJDK9() { |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 251 | javaFlags = "--add-opens java.base/java.lang.invoke=ALL-UNNAMED" |
| 252 | } |
| 253 | |
| 254 | var desugarFlags []string |
| 255 | desugarFlags = append(desugarFlags, flags.bootClasspath.DesugarBootClasspath()...) |
| 256 | desugarFlags = append(desugarFlags, flags.classpath.DesugarClasspath()...) |
| 257 | |
| 258 | var deps android.Paths |
| 259 | deps = append(deps, flags.bootClasspath...) |
| 260 | deps = append(deps, flags.classpath...) |
| 261 | |
| 262 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
| 263 | Rule: desugar, |
| 264 | Description: "desugar", |
| 265 | Output: outputFile, |
| 266 | Input: classesJar, |
| 267 | Implicits: deps, |
| 268 | Args: map[string]string{ |
| 269 | "dumpDir": dumpDir.String(), |
| 270 | "javaFlags": javaFlags, |
| 271 | "classpathFlags": strings.Join(desugarFlags, " "), |
| 272 | "desugarFlags": flags.desugarFlags, |
| 273 | }, |
| 274 | }) |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Colin Cross | 7db5d63 | 2017-10-04 17:07:09 -0700 | [diff] [blame] | 277 | // Converts a classes.jar file to classes*.dex, then combines the dex files with any resources |
| 278 | // in the classes.jar file into a dex jar. |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 279 | func TransformClassesJarToDexJar(ctx android.ModuleContext, outputFile android.WritablePath, |
| 280 | classesJar android.Path, flags javaBuilderFlags) { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 281 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 282 | outDir := android.PathForModuleOut(ctx, "dex") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 283 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 284 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 285 | Rule: dx, |
| 286 | Description: "dx", |
| 287 | Output: outputFile, |
| 288 | Input: classesJar, |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 289 | Args: map[string]string{ |
| 290 | "dxFlags": flags.dxFlags, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 291 | "outDir": outDir.String(), |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 292 | }, |
| 293 | }) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 294 | } |
Colin Cross | e1d62a8 | 2015-04-03 16:53:05 -0700 | [diff] [blame] | 295 | |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 296 | func TransformJarJar(ctx android.ModuleContext, outputFile android.WritablePath, |
| 297 | classesJar android.Path, rulesFile android.Path) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 298 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 299 | Rule: jarjar, |
| 300 | Description: "jarjar", |
| 301 | Output: outputFile, |
| 302 | Input: classesJar, |
| 303 | Implicit: rulesFile, |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 304 | Args: map[string]string{ |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 305 | "rulesFile": rulesFile.String(), |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 306 | }, |
| 307 | }) |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 308 | } |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 309 | |
| 310 | type classpath []android.Path |
| 311 | |
Colin Cross | 59149b6 | 2017-10-16 18:07:29 -0700 | [diff] [blame^] | 312 | // Returns a -sourcepath argument in the form javac expects. If the list is empty returns |
| 313 | // -sourcepath "" to ensure javac does not fall back to searching the classpath for sources. |
| 314 | func (x *classpath) JavaSourcepath() string { |
| 315 | if len(*x) > 0 { |
| 316 | return "-sourcepath " + strings.Join(x.Strings(), ":") |
| 317 | } else { |
| 318 | return `-sourcepath ""` |
| 319 | } |
| 320 | } |
| 321 | |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 322 | // Returns a -classpath argument in the form java or javac expects |
| 323 | func (x *classpath) JavaClasspath() string { |
| 324 | if len(*x) > 0 { |
| 325 | return "-classpath " + strings.Join(x.Strings(), ":") |
| 326 | } else { |
| 327 | return "" |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | // Returns a -processorpath argument in the form java or javac expects |
| 332 | func (x *classpath) JavaProcessorpath() string { |
| 333 | if len(*x) > 0 { |
| 334 | return "-processorpath " + strings.Join(x.Strings(), ":") |
| 335 | } else { |
| 336 | return "" |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // Returns a -bootclasspath argument in the form java or javac expects. If forceEmpty is true, |
| 341 | // returns -bootclasspath "" if the bootclasspath is empty to ensure javac does not fall back to the |
| 342 | // default bootclasspath. |
| 343 | func (x *classpath) JavaBootClasspath(forceEmpty bool) string { |
| 344 | if len(*x) > 0 { |
| 345 | return "-bootclasspath " + strings.Join(x.Strings(), ":") |
| 346 | } else if forceEmpty { |
| 347 | return `-bootclasspath ""` |
| 348 | } else { |
| 349 | return "" |
| 350 | } |
| 351 | } |
| 352 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 353 | // Returns a --system argument in the form javac expects with -source 1.9. If forceEmpty is true, |
| 354 | // returns --system=none if the list is empty to ensure javac does not fall back to the default |
| 355 | // system modules. |
| 356 | func (x *classpath) JavaSystemModules(forceEmpty bool) string { |
| 357 | if len(*x) > 1 { |
| 358 | panic("more than one system module") |
| 359 | } else if len(*x) == 1 { |
| 360 | return "--system=" + strings.TrimSuffix((*x)[0].String(), "lib/modules") |
| 361 | } else if forceEmpty { |
| 362 | return "--system=none" |
| 363 | } else { |
| 364 | return "" |
| 365 | } |
| 366 | } |
| 367 | |
Colin Cross | 6ade34f | 2017-09-15 13:00:47 -0700 | [diff] [blame] | 368 | func (x *classpath) DesugarBootClasspath() []string { |
| 369 | if x == nil || *x == nil { |
| 370 | return nil |
| 371 | } |
| 372 | flags := make([]string, len(*x)) |
| 373 | for i, v := range *x { |
| 374 | flags[i] = "--bootclasspath_entry " + v.String() |
| 375 | } |
| 376 | |
| 377 | return flags |
| 378 | } |
| 379 | |
| 380 | func (x *classpath) DesugarClasspath() []string { |
| 381 | if x == nil || *x == nil { |
| 382 | return nil |
| 383 | } |
| 384 | flags := make([]string, len(*x)) |
| 385 | for i, v := range *x { |
| 386 | flags[i] = "--classpath_entry " + v.String() |
| 387 | } |
| 388 | |
| 389 | return flags |
| 390 | } |
| 391 | |
| 392 | // Append an android.Paths to the end of the classpath list |
| 393 | func (x *classpath) AddPaths(paths android.Paths) { |
| 394 | for _, path := range paths { |
| 395 | *x = append(*x, path) |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // Convert a classpath to an android.Paths |
| 400 | func (x *classpath) Paths() android.Paths { |
| 401 | return append(android.Paths(nil), (*x)...) |
| 402 | } |
| 403 | |
| 404 | func (x *classpath) Strings() []string { |
| 405 | if x == nil { |
| 406 | return nil |
| 407 | } |
| 408 | ret := make([]string, len(*x)) |
| 409 | for i, path := range *x { |
| 410 | ret[i] = path.String() |
| 411 | } |
| 412 | return ret |
| 413 | } |