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