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 15package java 16 17import ( 18 "bytes" 19 "encoding/base64" 20 "encoding/binary" 21 "path/filepath" 22 "strings" 23 24 "android/soong/android" 25 26 "github.com/google/blueprint" 27) 28 29var kotlinc = pctx.AndroidRemoteStaticRule("kotlinc", android.RemoteRuleSupports{Goma: true}, 30 blueprint.RuleParams{ 31 Command: `rm -rf "$classesDir" "$headerClassesDir" "$srcJarDir" "$kotlinBuildFile" "$emptyDir" && ` + 32 `mkdir -p "$classesDir" "$headerClassesDir" "$srcJarDir" "$emptyDir" && ` + 33 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" -f "*.kt" $srcJars && ` + 34 `${config.GenKotlinBuildFileCmd} --classpath "$classpath" --name "$name"` + 35 ` --out_dir "$classesDir" --srcs "$out.rsp" --srcs "$srcJarDir/list"` + 36 ` $commonSrcFilesArg --out "$kotlinBuildFile" && ` + 37 `${config.KotlincCmd} ${config.KotlincGlobalFlags} ` + 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 && ` + 45 `rm -rf "$srcJarDir" "$classesDir" "$headerClassesDir"`, 46 CommandDeps: []string{ 47 "${config.KotlincCmd}", 48 "${config.KotlinCompilerJar}", 49 "${config.KotlinPreloaderJar}", 50 "${config.KotlinReflectJar}", 51 "${config.KotlinScriptRuntimeJar}", 52 "${config.KotlinStdlibJar}", 53 "${config.KotlinTrove4jJar}", 54 "${config.KotlinAnnotationJar}", 55 "${config.KotlinAbiGenPluginJar}", 56 "${config.GenKotlinBuildFileCmd}", 57 "${config.SoongZipCmd}", 58 "${config.ZipSyncCmd}", 59 }, 60 Rspfile: "$out.rsp", 61 RspfileContent: `$in`, 62 Restat: true, 63 }, 64 "kotlincFlags", "classpath", "srcJars", "commonSrcFilesArg", "srcJarDir", "classesDir", 65 "headerClassesDir", "headerJar", "kotlinJvmTarget", "kotlinBuildFile", "emptyDir", "name") 66 67func 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") 73 rule := android.NewRuleBuilder(pctx, ctx) 74 rule.Command().Text("cp"). 75 FlagWithRspFileInputList("", commonSrcsList.ReplaceExtension(ctx, "rsp"), commonSrcFiles). 76 Output(commonSrcsList) 77 rule.Build("kotlin_common_srcs_list", "kotlin common_srcs list") 78 return android.OptionalPathForPath(commonSrcsList) 79 } 80 return android.OptionalPath{} 81} 82 83// kotlinCompile takes .java and .kt sources and srcJars, and compiles the .kt sources into a classes jar in outputFile. 84func kotlinCompile(ctx android.ModuleContext, outputFile, headerOutputFile android.WritablePath, 85 srcFiles, commonSrcFiles, srcJars android.Paths, 86 flags javaBuilderFlags) { 87 88 var deps android.Paths 89 deps = append(deps, flags.kotlincClasspath...) 90 deps = append(deps, flags.kotlincDeps...) 91 deps = append(deps, srcJars...) 92 deps = append(deps, commonSrcFiles...) 93 94 kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName()) 95 kotlinName = strings.ReplaceAll(kotlinName, "/", "__") 96 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 104 ctx.Build(pctx, android.BuildParams{ 105 Rule: kotlinc, 106 Description: "kotlinc", 107 Output: outputFile, 108 ImplicitOutput: headerOutputFile, 109 Inputs: srcFiles, 110 Implicits: deps, 111 Args: map[string]string{ 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(), 117 "headerClassesDir": android.PathForModuleOut(ctx, "kotlinc", "header_classes").String(), 118 "headerJar": headerOutputFile.String(), 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(), 122 "kotlinJvmTarget": flags.javaVersion.StringForKotlinc(), 123 "name": kotlinName, 124 }, 125 }) 126} 127 128var kaptStubs = pctx.AndroidRemoteStaticRule("kaptStubs", android.RemoteRuleSupports{Goma: true}, 129 blueprint.RuleParams{ 130 Command: `rm -rf "$srcJarDir" "$kotlinBuildFile" "$kaptDir" && ` + 131 `mkdir -p "$srcJarDir" "$kaptDir/sources" "$kaptDir/classes" && ` + 132 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` + 133 `${config.GenKotlinBuildFileCmd} --classpath "$classpath" --name "$name"` + 134 ` --srcs "$out.rsp" --srcs "$srcJarDir/list"` + 135 ` $commonSrcFilesArg --out "$kotlinBuildFile" && ` + 136 `${config.KotlincCmd} ${config.KotlincGlobalFlags} ` + 137 `${config.KaptSuppressJDK9Warnings} ${config.KotlincSuppressJDK9Warnings} ` + 138 `${config.JavacHeapFlags} $kotlincFlags -Xplugin=${config.KotlinKaptJar} ` + 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 ` + 143 `-P plugin:org.jetbrains.kotlin.kapt3:aptMode=stubs ` + 144 `-P plugin:org.jetbrains.kotlin.kapt3:javacArguments=$encodedJavacFlags ` + 145 `$kaptProcessorPath ` + 146 `$kaptProcessor ` + 147 `-Xbuild-file=$kotlinBuildFile && ` + 148 `${config.SoongZipCmd} -jar -write_if_changed -o $out -C $kaptDir/stubs -D $kaptDir/stubs && ` + 149 `rm -rf "$srcJarDir"`, 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 Restat: true, 161 }, 162 "kotlincFlags", "encodedJavacFlags", "kaptProcessorPath", "kaptProcessor", 163 "classpath", "srcJars", "commonSrcFilesArg", "srcJarDir", "kaptDir", "kotlinJvmTarget", 164 "kotlinBuildFile", "name", "classesJarOut") 165 166// kotlinKapt performs Kotlin-compatible annotation processing. It takes .kt and .java sources and srcjars, and runs 167// annotation processors over all of them, producing a srcjar of generated code in outputFile. The srcjar should be 168// added as an additional input to kotlinc and javac rules, and the javac rule should have annotation processing 169// disabled. 170func kotlinKapt(ctx android.ModuleContext, srcJarOutputFile, resJarOutputFile android.WritablePath, 171 srcFiles, commonSrcFiles, srcJars android.Paths, 172 flags javaBuilderFlags) { 173 174 srcFiles = append(android.Paths(nil), srcFiles...) 175 176 var deps android.Paths 177 deps = append(deps, flags.kotlincClasspath...) 178 deps = append(deps, flags.kotlincDeps...) 179 deps = append(deps, srcJars...) 180 deps = append(deps, flags.processorPath...) 181 deps = append(deps, commonSrcFiles...) 182 183 commonSrcsList := kotlinCommonSrcsList(ctx, commonSrcFiles) 184 commonSrcFilesArg := "" 185 if commonSrcsList.Valid() { 186 deps = append(deps, commonSrcsList.Path()) 187 commonSrcFilesArg = "--common_srcs " + commonSrcsList.String() 188 } 189 190 kaptProcessorPath := flags.processorPath.FormRepeatedClassPath("-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=") 191 192 kaptProcessor := "" 193 for i, p := range flags.processors { 194 if i > 0 { 195 kaptProcessor += " " 196 } 197 kaptProcessor += "-P plugin:org.jetbrains.kotlin.kapt3:processors=" + p 198 } 199 200 encodedJavacFlags := kaptEncodeFlags([][2]string{ 201 {"-source", flags.javaVersion.String()}, 202 {"-target", flags.javaVersion.String()}, 203 }) 204 205 kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName()) 206 kotlinName = strings.ReplaceAll(kotlinName, "/", "__") 207 208 // First run kapt to generate .java stubs from .kt files 209 kaptStubsJar := android.PathForModuleOut(ctx, "kapt", "stubs.jar") 210 ctx.Build(pctx, android.BuildParams{ 211 Rule: kaptStubs, 212 Description: "kapt stubs", 213 Output: kaptStubsJar, 214 Inputs: srcFiles, 215 Implicits: deps, 216 Args: map[string]string{ 217 "classpath": flags.kotlincClasspath.FormJavaClassPath(""), 218 "kotlincFlags": flags.kotlincFlags, 219 "commonSrcFilesArg": commonSrcFilesArg, 220 "srcJars": strings.Join(srcJars.Strings(), " "), 221 "srcJarDir": android.PathForModuleOut(ctx, "kapt", "srcJars").String(), 222 "kotlinBuildFile": android.PathForModuleOut(ctx, "kapt", "build.xml").String(), 223 "kaptProcessorPath": strings.Join(kaptProcessorPath, " "), 224 "kaptProcessor": kaptProcessor, 225 "kaptDir": android.PathForModuleOut(ctx, "kapt/gen").String(), 226 "encodedJavacFlags": encodedJavacFlags, 227 "name": kotlinName, 228 "classesJarOut": resJarOutputFile.String(), 229 }, 230 }) 231 232 // Then run turbine to perform annotation processing on the stubs and any .java srcFiles. 233 javaSrcFiles := srcFiles.FilterByExt(".java") 234 turbineSrcJars := append(android.Paths{kaptStubsJar}, srcJars...) 235 TurbineApt(ctx, srcJarOutputFile, resJarOutputFile, javaSrcFiles, turbineSrcJars, flags) 236} 237 238// kapt converts a list of key, value pairs into a base64 encoded Java serialization, which is what kapt expects. 239func kaptEncodeFlags(options [][2]string) string { 240 buf := &bytes.Buffer{} 241 242 binary.Write(buf, binary.BigEndian, uint32(len(options))) 243 for _, option := range options { 244 binary.Write(buf, binary.BigEndian, uint16(len(option[0]))) 245 buf.WriteString(option[0]) 246 binary.Write(buf, binary.BigEndian, uint16(len(option[1]))) 247 buf.WriteString(option[1]) 248 } 249 250 header := &bytes.Buffer{} 251 header.Write([]byte{0xac, 0xed, 0x00, 0x05}) // java serialization header 252 253 if buf.Len() < 256 { 254 header.WriteByte(0x77) // blockdata 255 header.WriteByte(byte(buf.Len())) 256 } else { 257 header.WriteByte(0x7a) // blockdatalong 258 binary.Write(header, binary.BigEndian, uint32(buf.Len())) 259 } 260 261 return base64.StdEncoding.EncodeToString(append(header.Bytes(), buf.Bytes()...)) 262} 263