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" $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"`, 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 -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 }, 161 "kotlincFlags", "encodedJavacFlags", "kaptProcessorPath", "kaptProcessor", 162 "classpath", "srcJars", "commonSrcFilesArg", "srcJarDir", "kaptDir", "kotlinJvmTarget", 163 "kotlinBuildFile", "name", "classesJarOut") 164 165// kotlinKapt performs Kotlin-compatible annotation processing. It takes .kt and .java sources and srcjars, and runs 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. 169func kotlinKapt(ctx android.ModuleContext, srcJarOutputFile, resJarOutputFile android.WritablePath, 170 srcFiles, commonSrcFiles, srcJars android.Paths, 171 flags javaBuilderFlags) { 172 173 srcFiles = append(android.Paths(nil), srcFiles...) 174 175 var deps android.Paths 176 deps = append(deps, flags.kotlincClasspath...) 177 deps = append(deps, flags.kotlincDeps...) 178 deps = append(deps, srcJars...) 179 deps = append(deps, flags.processorPath...) 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 } 188 189 kaptProcessorPath := flags.processorPath.FormRepeatedClassPath("-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=") 190 191 kaptProcessor := "" 192 for i, p := range flags.processors { 193 if i > 0 { 194 kaptProcessor += " " 195 } 196 kaptProcessor += "-P plugin:org.jetbrains.kotlin.kapt3:processors=" + p 197 } 198 199 encodedJavacFlags := kaptEncodeFlags([][2]string{ 200 {"-source", flags.javaVersion.String()}, 201 {"-target", flags.javaVersion.String()}, 202 }) 203 204 kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName()) 205 kotlinName = strings.ReplaceAll(kotlinName, "/", "__") 206 207 // First run kapt to generate .java stubs from .kt files 208 kaptStubsJar := android.PathForModuleOut(ctx, "kapt", "stubs.jar") 209 ctx.Build(pctx, android.BuildParams{ 210 Rule: kaptStubs, 211 Description: "kapt stubs", 212 Output: kaptStubsJar, 213 Inputs: srcFiles, 214 Implicits: deps, 215 Args: map[string]string{ 216 "classpath": flags.kotlincClasspath.FormJavaClassPath(""), 217 "kotlincFlags": flags.kotlincFlags, 218 "commonSrcFilesArg": commonSrcFilesArg, 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, " "), 223 "kaptProcessor": kaptProcessor, 224 "kaptDir": android.PathForModuleOut(ctx, "kapt/gen").String(), 225 "encodedJavacFlags": encodedJavacFlags, 226 "name": kotlinName, 227 "classesJarOut": resJarOutputFile.String(), 228 }, 229 }) 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...) 234 TurbineApt(ctx, srcJarOutputFile, resJarOutputFile, javaSrcFiles, turbineSrcJars, flags) 235} 236 237// kapt converts a list of key, value pairs into a base64 encoded Java serialization, which is what kapt expects. 238func 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} 262