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 "strings" 22 23 "android/soong/android" 24 25 "github.com/google/blueprint" 26) 27 28var kotlinc = pctx.AndroidGomaStaticRule("kotlinc", 29 blueprint.RuleParams{ 30 Command: `rm -rf "$classesDir" "$srcJarDir" "$kotlinBuildFile" && mkdir -p "$classesDir" "$srcJarDir" && ` + 31 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` + 32 `${config.GenKotlinBuildFileCmd} $classpath $classesDir $out.rsp $srcJarDir/list > $kotlinBuildFile &&` + 33 `${config.KotlincCmd} ${config.JavacHeapFlags} $kotlincFlags ` + 34 `-jvm-target $kotlinJvmTarget -Xbuild-file=$kotlinBuildFile && ` + 35 `${config.SoongZipCmd} -jar -o $out -C $classesDir -D $classesDir && ` + 36 `rm -rf "$srcJarDir"`, 37 CommandDeps: []string{ 38 "${config.KotlincCmd}", 39 "${config.KotlinCompilerJar}", 40 "${config.GenKotlinBuildFileCmd}", 41 "${config.SoongZipCmd}", 42 "${config.ZipSyncCmd}", 43 }, 44 Rspfile: "$out.rsp", 45 RspfileContent: `$in`, 46 }, 47 "kotlincFlags", "classpath", "srcJars", "srcJarDir", "classesDir", "kotlinJvmTarget", "kotlinBuildFile") 48 49// kotlinCompile takes .java and .kt sources and srcJars, and compiles the .kt sources into a classes jar in outputFile. 50func kotlinCompile(ctx android.ModuleContext, outputFile android.WritablePath, 51 srcFiles, srcJars android.Paths, 52 flags javaBuilderFlags) { 53 54 var deps android.Paths 55 deps = append(deps, flags.kotlincClasspath...) 56 deps = append(deps, srcJars...) 57 58 ctx.Build(pctx, android.BuildParams{ 59 Rule: kotlinc, 60 Description: "kotlinc", 61 Output: outputFile, 62 Inputs: srcFiles, 63 Implicits: deps, 64 Args: map[string]string{ 65 "classpath": flags.kotlincClasspath.FormJavaClassPath("-classpath"), 66 "kotlincFlags": flags.kotlincFlags, 67 "srcJars": strings.Join(srcJars.Strings(), " "), 68 "classesDir": android.PathForModuleOut(ctx, "kotlinc", "classes").String(), 69 "srcJarDir": android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(), 70 "kotlinBuildFile": android.PathForModuleOut(ctx, "kotlinc-build.xml").String(), 71 // http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8 72 "kotlinJvmTarget": "1.8", 73 }, 74 }) 75} 76 77var kapt = pctx.AndroidGomaStaticRule("kapt", 78 blueprint.RuleParams{ 79 Command: `rm -rf "$srcJarDir" "$kotlinBuildFile" "$kaptDir" && mkdir -p "$srcJarDir" "$kaptDir" && ` + 80 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` + 81 `${config.GenKotlinBuildFileCmd} $classpath "" $out.rsp $srcJarDir/list > $kotlinBuildFile &&` + 82 `${config.KotlincCmd} ${config.KotlincSuppressJDK9Warnings} ${config.JavacHeapFlags} $kotlincFlags ` + 83 `-Xplugin=${config.KotlinKaptJar} ` + 84 `-P plugin:org.jetbrains.kotlin.kapt3:sources=$kaptDir/sources ` + 85 `-P plugin:org.jetbrains.kotlin.kapt3:classes=$kaptDir/classes ` + 86 `-P plugin:org.jetbrains.kotlin.kapt3:stubs=$kaptDir/stubs ` + 87 `-P plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true ` + 88 `-P plugin:org.jetbrains.kotlin.kapt3:aptMode=stubsAndApt ` + 89 `-P plugin:org.jetbrains.kotlin.kapt3:javacArguments=$encodedJavacFlags ` + 90 `$kaptProcessorPath ` + 91 `$kaptProcessor ` + 92 `-Xbuild-file=$kotlinBuildFile && ` + 93 `${config.SoongZipCmd} -jar -o $out -C $kaptDir/sources -D $kaptDir/sources && ` + 94 `rm -rf "$srcJarDir"`, 95 CommandDeps: []string{ 96 "${config.KotlincCmd}", 97 "${config.KotlinCompilerJar}", 98 "${config.KotlinKaptJar}", 99 "${config.GenKotlinBuildFileCmd}", 100 "${config.SoongZipCmd}", 101 "${config.ZipSyncCmd}", 102 }, 103 Rspfile: "$out.rsp", 104 RspfileContent: `$in`, 105 }, 106 "kotlincFlags", "encodedJavacFlags", "kaptProcessorPath", "kaptProcessor", 107 "classpath", "srcJars", "srcJarDir", "kaptDir", "kotlinJvmTarget", "kotlinBuildFile") 108 109// kotlinKapt performs Kotlin-compatible annotation processing. It takes .kt and .java sources and srcjars, and runs 110// annotation processors over all of them, producing a srcjar of generated code in outputFile. The srcjar should be 111// added as an additional input to kotlinc and javac rules, and the javac rule should have annotation processing 112// disabled. 113func kotlinKapt(ctx android.ModuleContext, outputFile android.WritablePath, 114 srcFiles, srcJars android.Paths, 115 flags javaBuilderFlags) { 116 117 var deps android.Paths 118 deps = append(deps, flags.kotlincClasspath...) 119 deps = append(deps, srcJars...) 120 deps = append(deps, flags.processorPath...) 121 122 kaptProcessorPath := flags.processorPath.FormTurbineClasspath("-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=") 123 124 kaptProcessor := "" 125 if flags.processor != "" { 126 kaptProcessor = "-P plugin:org.jetbrains.kotlin.kapt3:processors=" + flags.processor 127 } 128 129 encodedJavacFlags := kaptEncodeFlags([][2]string{ 130 {"-source", flags.javaVersion}, 131 {"-target", flags.javaVersion}, 132 }) 133 134 ctx.Build(pctx, android.BuildParams{ 135 Rule: kapt, 136 Description: "kapt", 137 Output: outputFile, 138 Inputs: srcFiles, 139 Implicits: deps, 140 Args: map[string]string{ 141 "classpath": flags.kotlincClasspath.FormJavaClassPath("-classpath"), 142 "kotlincFlags": flags.kotlincFlags, 143 "srcJars": strings.Join(srcJars.Strings(), " "), 144 "srcJarDir": android.PathForModuleOut(ctx, "kapt", "srcJars").String(), 145 "kotlinBuildFile": android.PathForModuleOut(ctx, "kapt", "build.xml").String(), 146 "kaptProcessorPath": strings.Join(kaptProcessorPath, " "), 147 "kaptProcessor": kaptProcessor, 148 "kaptDir": android.PathForModuleOut(ctx, "kapt/gen").String(), 149 "encodedJavacFlags": encodedJavacFlags, 150 }, 151 }) 152} 153 154// kapt converts a list of key, value pairs into a base64 encoded Java serialization, which is what kapt expects. 155func kaptEncodeFlags(options [][2]string) string { 156 buf := &bytes.Buffer{} 157 158 binary.Write(buf, binary.BigEndian, uint32(len(options))) 159 for _, option := range options { 160 binary.Write(buf, binary.BigEndian, uint16(len(option[0]))) 161 buf.WriteString(option[0]) 162 binary.Write(buf, binary.BigEndian, uint16(len(option[1]))) 163 buf.WriteString(option[1]) 164 } 165 166 header := &bytes.Buffer{} 167 header.Write([]byte{0xac, 0xed, 0x00, 0x05}) // java serialization header 168 169 if buf.Len() < 256 { 170 header.WriteByte(0x77) // blockdata 171 header.WriteByte(byte(buf.Len())) 172 } else { 173 header.WriteByte(0x7a) // blockdatalong 174 binary.Write(header, binary.BigEndian, uint32(buf.Len())) 175 } 176 177 return base64.StdEncoding.EncodeToString(append(header.Bytes(), buf.Bytes()...)) 178} 179