Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 1 | // Copyright 2019 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 | import ( |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 18 | "bytes" |
| 19 | "encoding/base64" |
| 20 | "encoding/binary" |
Colin Cross | 6c6e6cd | 2019-05-08 14:30:12 -0700 | [diff] [blame] | 21 | "path/filepath" |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 22 | "strings" |
| 23 | |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 24 | "android/soong/android" |
| 25 | |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 26 | "github.com/google/blueprint" |
| 27 | ) |
| 28 | |
Ramy Medhat | 8ea054a | 2020-01-27 14:19:44 -0500 | [diff] [blame] | 29 | var kotlinc = pctx.AndroidRemoteStaticRule("kotlinc", android.RemoteRuleSupports{Goma: true}, |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 30 | blueprint.RuleParams{ |
Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 31 | Command: `rm -rf "$classesDir" "$srcJarDir" "$kotlinBuildFile" "$emptyDir" && ` + |
| 32 | `mkdir -p "$classesDir" "$srcJarDir" "$emptyDir" && ` + |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 33 | `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` + |
Colin Cross | 9b1aa0c | 2020-06-25 17:12:28 -0700 | [diff] [blame] | 34 | `${config.GenKotlinBuildFileCmd} --classpath "$classpath" --name "$name"` + |
| 35 | ` --out_dir "$classesDir" --srcs "$out.rsp" --srcs "$srcJarDir/list"` + |
Colin Cross | a4c8cc6 | 2020-06-25 17:13:36 -0700 | [diff] [blame] | 36 | ` $commonSrcFilesArg --out "$kotlinBuildFile" && ` + |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 37 | `${config.KotlincCmd} ${config.JavacHeapFlags} $kotlincFlags ` + |
Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 38 | `-jvm-target $kotlinJvmTarget -Xbuild-file=$kotlinBuildFile -kotlin-home $emptyDir && ` + |
Colin Cross | 44c29a8 | 2019-01-24 16:36:57 -0800 | [diff] [blame] | 39 | `${config.SoongZipCmd} -jar -o $out -C $classesDir -D $classesDir && ` + |
| 40 | `rm -rf "$srcJarDir"`, |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 41 | CommandDeps: []string{ |
| 42 | "${config.KotlincCmd}", |
| 43 | "${config.KotlinCompilerJar}", |
Dan Willemsen | aad1960 | 2019-04-07 09:44:35 -0700 | [diff] [blame] | 44 | "${config.KotlinPreloaderJar}", |
| 45 | "${config.KotlinReflectJar}", |
| 46 | "${config.KotlinScriptRuntimeJar}", |
| 47 | "${config.KotlinStdlibJar}", |
| 48 | "${config.KotlinTrove4jJar}", |
Dan Willemsen | 1cfbdbe | 2019-06-13 04:59:17 +0000 | [diff] [blame] | 49 | "${config.KotlinAnnotationJar}", |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 50 | "${config.GenKotlinBuildFileCmd}", |
| 51 | "${config.SoongZipCmd}", |
| 52 | "${config.ZipSyncCmd}", |
| 53 | }, |
| 54 | Rspfile: "$out.rsp", |
| 55 | RspfileContent: `$in`, |
| 56 | }, |
Colin Cross | a4c8cc6 | 2020-06-25 17:13:36 -0700 | [diff] [blame] | 57 | "kotlincFlags", "classpath", "srcJars", "commonSrcFilesArg", "srcJarDir", "classesDir", |
| 58 | "kotlinJvmTarget", "kotlinBuildFile", "emptyDir", "name") |
| 59 | |
| 60 | func kotlinCommonSrcsList(ctx android.ModuleContext, commonSrcFiles android.Paths) android.OptionalPath { |
| 61 | if len(commonSrcFiles) > 0 { |
| 62 | // The list of common_srcs may be too long to put on the command line, but |
| 63 | // we can't use the rsp file because it is already being used for srcs. |
| 64 | // Insert a second rule to write out the list of resources to a file. |
| 65 | commonSrcsList := android.PathForModuleOut(ctx, "kotlinc_common_srcs.list") |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 66 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | a4c8cc6 | 2020-06-25 17:13:36 -0700 | [diff] [blame] | 67 | rule.Command().Text("cp").FlagWithRspFileInputList("", commonSrcFiles).Output(commonSrcsList) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 68 | rule.Build("kotlin_common_srcs_list", "kotlin common_srcs list") |
Colin Cross | a4c8cc6 | 2020-06-25 17:13:36 -0700 | [diff] [blame] | 69 | return android.OptionalPathForPath(commonSrcsList) |
| 70 | } |
| 71 | return android.OptionalPath{} |
| 72 | } |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 73 | |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 74 | // kotlinCompile takes .java and .kt sources and srcJars, and compiles the .kt sources into a classes jar in outputFile. |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 75 | func kotlinCompile(ctx android.ModuleContext, outputFile android.WritablePath, |
Colin Cross | a4c8cc6 | 2020-06-25 17:13:36 -0700 | [diff] [blame] | 76 | srcFiles, commonSrcFiles, srcJars android.Paths, |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 77 | flags javaBuilderFlags) { |
| 78 | |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 79 | var deps android.Paths |
| 80 | deps = append(deps, flags.kotlincClasspath...) |
| 81 | deps = append(deps, srcJars...) |
Colin Cross | a4c8cc6 | 2020-06-25 17:13:36 -0700 | [diff] [blame] | 82 | deps = append(deps, commonSrcFiles...) |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 83 | |
Colin Cross | 6c6e6cd | 2019-05-08 14:30:12 -0700 | [diff] [blame] | 84 | kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName()) |
| 85 | kotlinName = strings.ReplaceAll(kotlinName, "/", "__") |
| 86 | |
Colin Cross | a4c8cc6 | 2020-06-25 17:13:36 -0700 | [diff] [blame] | 87 | commonSrcsList := kotlinCommonSrcsList(ctx, commonSrcFiles) |
| 88 | commonSrcFilesArg := "" |
| 89 | if commonSrcsList.Valid() { |
| 90 | deps = append(deps, commonSrcsList.Path()) |
| 91 | commonSrcFilesArg = "--common_srcs " + commonSrcsList.String() |
| 92 | } |
| 93 | |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 94 | ctx.Build(pctx, android.BuildParams{ |
| 95 | Rule: kotlinc, |
| 96 | Description: "kotlinc", |
| 97 | Output: outputFile, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 98 | Inputs: srcFiles, |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 99 | Implicits: deps, |
| 100 | Args: map[string]string{ |
Colin Cross | a4c8cc6 | 2020-06-25 17:13:36 -0700 | [diff] [blame] | 101 | "classpath": flags.kotlincClasspath.FormJavaClassPath(""), |
| 102 | "kotlincFlags": flags.kotlincFlags, |
| 103 | "commonSrcFilesArg": commonSrcFilesArg, |
| 104 | "srcJars": strings.Join(srcJars.Strings(), " "), |
| 105 | "classesDir": android.PathForModuleOut(ctx, "kotlinc", "classes").String(), |
| 106 | "srcJarDir": android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(), |
| 107 | "kotlinBuildFile": android.PathForModuleOut(ctx, "kotlinc-build.xml").String(), |
| 108 | "emptyDir": android.PathForModuleOut(ctx, "kotlinc", "empty").String(), |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 109 | // http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8 |
| 110 | "kotlinJvmTarget": "1.8", |
Colin Cross | 6c6e6cd | 2019-05-08 14:30:12 -0700 | [diff] [blame] | 111 | "name": kotlinName, |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 112 | }, |
| 113 | }) |
| 114 | } |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 115 | |
Ramy Medhat | 8ea054a | 2020-01-27 14:19:44 -0500 | [diff] [blame] | 116 | var kapt = pctx.AndroidRemoteStaticRule("kapt", android.RemoteRuleSupports{Goma: true}, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 117 | blueprint.RuleParams{ |
Colin Cross | 9ca38d2 | 2020-06-18 15:46:32 -0700 | [diff] [blame] | 118 | Command: `rm -rf "$srcJarDir" "$kotlinBuildFile" "$kaptDir" && ` + |
| 119 | `mkdir -p "$srcJarDir" "$kaptDir/sources" "$kaptDir/classes" && ` + |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 120 | `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` + |
Colin Cross | 9b1aa0c | 2020-06-25 17:12:28 -0700 | [diff] [blame] | 121 | `${config.GenKotlinBuildFileCmd} --classpath "$classpath" --name "$name"` + |
| 122 | ` --srcs "$out.rsp" --srcs "$srcJarDir/list"` + |
Colin Cross | a4c8cc6 | 2020-06-25 17:13:36 -0700 | [diff] [blame] | 123 | ` $commonSrcFilesArg --out "$kotlinBuildFile" && ` + |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 124 | `${config.KotlincCmd} ${config.KotlincSuppressJDK9Warnings} ${config.JavacHeapFlags} $kotlincFlags ` + |
| 125 | `-Xplugin=${config.KotlinKaptJar} ` + |
| 126 | `-P plugin:org.jetbrains.kotlin.kapt3:sources=$kaptDir/sources ` + |
| 127 | `-P plugin:org.jetbrains.kotlin.kapt3:classes=$kaptDir/classes ` + |
| 128 | `-P plugin:org.jetbrains.kotlin.kapt3:stubs=$kaptDir/stubs ` + |
| 129 | `-P plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true ` + |
| 130 | `-P plugin:org.jetbrains.kotlin.kapt3:aptMode=stubsAndApt ` + |
| 131 | `-P plugin:org.jetbrains.kotlin.kapt3:javacArguments=$encodedJavacFlags ` + |
| 132 | `$kaptProcessorPath ` + |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 133 | `$kaptProcessor ` + |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 134 | `-Xbuild-file=$kotlinBuildFile && ` + |
Colin Cross | 44c29a8 | 2019-01-24 16:36:57 -0800 | [diff] [blame] | 135 | `${config.SoongZipCmd} -jar -o $out -C $kaptDir/sources -D $kaptDir/sources && ` + |
Colin Cross | 9ca38d2 | 2020-06-18 15:46:32 -0700 | [diff] [blame] | 136 | `${config.SoongZipCmd} -jar -o $classesJarOut -C $kaptDir/classes -D $kaptDir/classes && ` + |
Colin Cross | 44c29a8 | 2019-01-24 16:36:57 -0800 | [diff] [blame] | 137 | `rm -rf "$srcJarDir"`, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 138 | CommandDeps: []string{ |
| 139 | "${config.KotlincCmd}", |
| 140 | "${config.KotlinCompilerJar}", |
| 141 | "${config.KotlinKaptJar}", |
| 142 | "${config.GenKotlinBuildFileCmd}", |
| 143 | "${config.SoongZipCmd}", |
| 144 | "${config.ZipSyncCmd}", |
| 145 | }, |
| 146 | Rspfile: "$out.rsp", |
| 147 | RspfileContent: `$in`, |
| 148 | }, |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 149 | "kotlincFlags", "encodedJavacFlags", "kaptProcessorPath", "kaptProcessor", |
Colin Cross | a4c8cc6 | 2020-06-25 17:13:36 -0700 | [diff] [blame] | 150 | "classpath", "srcJars", "commonSrcFilesArg", "srcJarDir", "kaptDir", "kotlinJvmTarget", |
| 151 | "kotlinBuildFile", "name", "classesJarOut") |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 152 | |
| 153 | // kotlinKapt performs Kotlin-compatible annotation processing. It takes .kt and .java sources and srcjars, and runs |
| 154 | // annotation processors over all of them, producing a srcjar of generated code in outputFile. The srcjar should be |
| 155 | // added as an additional input to kotlinc and javac rules, and the javac rule should have annotation processing |
| 156 | // disabled. |
Colin Cross | 9ca38d2 | 2020-06-18 15:46:32 -0700 | [diff] [blame] | 157 | func kotlinKapt(ctx android.ModuleContext, srcJarOutputFile, resJarOutputFile android.WritablePath, |
Colin Cross | a4c8cc6 | 2020-06-25 17:13:36 -0700 | [diff] [blame] | 158 | srcFiles, commonSrcFiles, srcJars android.Paths, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 159 | flags javaBuilderFlags) { |
| 160 | |
Colin Cross | a4c8cc6 | 2020-06-25 17:13:36 -0700 | [diff] [blame] | 161 | srcFiles = append(android.Paths(nil), srcFiles...) |
| 162 | |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 163 | var deps android.Paths |
| 164 | deps = append(deps, flags.kotlincClasspath...) |
| 165 | deps = append(deps, srcJars...) |
| 166 | deps = append(deps, flags.processorPath...) |
Colin Cross | a4c8cc6 | 2020-06-25 17:13:36 -0700 | [diff] [blame] | 167 | deps = append(deps, commonSrcFiles...) |
| 168 | |
| 169 | commonSrcsList := kotlinCommonSrcsList(ctx, commonSrcFiles) |
| 170 | commonSrcFilesArg := "" |
| 171 | if commonSrcsList.Valid() { |
| 172 | deps = append(deps, commonSrcsList.Path()) |
| 173 | commonSrcFilesArg = "--common_srcs " + commonSrcsList.String() |
| 174 | } |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 175 | |
Colin Cross | c2557d1 | 2019-10-31 15:22:57 -0700 | [diff] [blame] | 176 | kaptProcessorPath := flags.processorPath.FormRepeatedClassPath("-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=") |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 177 | |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 178 | kaptProcessor := "" |
Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 179 | for i, p := range flags.processors { |
| 180 | if i > 0 { |
| 181 | kaptProcessor += " " |
| 182 | } |
| 183 | kaptProcessor += "-P plugin:org.jetbrains.kotlin.kapt3:processors=" + p |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 186 | encodedJavacFlags := kaptEncodeFlags([][2]string{ |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 187 | {"-source", flags.javaVersion.String()}, |
| 188 | {"-target", flags.javaVersion.String()}, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 189 | }) |
| 190 | |
Colin Cross | 6c6e6cd | 2019-05-08 14:30:12 -0700 | [diff] [blame] | 191 | kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName()) |
| 192 | kotlinName = strings.ReplaceAll(kotlinName, "/", "__") |
| 193 | |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 194 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 9ca38d2 | 2020-06-18 15:46:32 -0700 | [diff] [blame] | 195 | Rule: kapt, |
| 196 | Description: "kapt", |
| 197 | Output: srcJarOutputFile, |
| 198 | ImplicitOutput: resJarOutputFile, |
| 199 | Inputs: srcFiles, |
| 200 | Implicits: deps, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 201 | Args: map[string]string{ |
Colin Cross | 9b1aa0c | 2020-06-25 17:12:28 -0700 | [diff] [blame] | 202 | "classpath": flags.kotlincClasspath.FormJavaClassPath(""), |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 203 | "kotlincFlags": flags.kotlincFlags, |
Colin Cross | a4c8cc6 | 2020-06-25 17:13:36 -0700 | [diff] [blame] | 204 | "commonSrcFilesArg": commonSrcFilesArg, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 205 | "srcJars": strings.Join(srcJars.Strings(), " "), |
| 206 | "srcJarDir": android.PathForModuleOut(ctx, "kapt", "srcJars").String(), |
| 207 | "kotlinBuildFile": android.PathForModuleOut(ctx, "kapt", "build.xml").String(), |
| 208 | "kaptProcessorPath": strings.Join(kaptProcessorPath, " "), |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 209 | "kaptProcessor": kaptProcessor, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 210 | "kaptDir": android.PathForModuleOut(ctx, "kapt/gen").String(), |
| 211 | "encodedJavacFlags": encodedJavacFlags, |
Colin Cross | 6c6e6cd | 2019-05-08 14:30:12 -0700 | [diff] [blame] | 212 | "name": kotlinName, |
Colin Cross | 9ca38d2 | 2020-06-18 15:46:32 -0700 | [diff] [blame] | 213 | "classesJarOut": resJarOutputFile.String(), |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 214 | }, |
| 215 | }) |
| 216 | } |
| 217 | |
| 218 | // kapt converts a list of key, value pairs into a base64 encoded Java serialization, which is what kapt expects. |
| 219 | func kaptEncodeFlags(options [][2]string) string { |
| 220 | buf := &bytes.Buffer{} |
| 221 | |
| 222 | binary.Write(buf, binary.BigEndian, uint32(len(options))) |
| 223 | for _, option := range options { |
| 224 | binary.Write(buf, binary.BigEndian, uint16(len(option[0]))) |
| 225 | buf.WriteString(option[0]) |
| 226 | binary.Write(buf, binary.BigEndian, uint16(len(option[1]))) |
| 227 | buf.WriteString(option[1]) |
| 228 | } |
| 229 | |
| 230 | header := &bytes.Buffer{} |
| 231 | header.Write([]byte{0xac, 0xed, 0x00, 0x05}) // java serialization header |
| 232 | |
| 233 | if buf.Len() < 256 { |
| 234 | header.WriteByte(0x77) // blockdata |
| 235 | header.WriteByte(byte(buf.Len())) |
| 236 | } else { |
| 237 | header.WriteByte(0x7a) // blockdatalong |
| 238 | binary.Write(header, binary.BigEndian, uint32(buf.Len())) |
| 239 | } |
| 240 | |
| 241 | return base64.StdEncoding.EncodeToString(append(header.Bytes(), buf.Bytes()...)) |
| 242 | } |