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