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 | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 21 | "strings" |
| 22 | |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 23 | "android/soong/android" |
| 24 | |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 25 | "github.com/google/blueprint" |
| 26 | ) |
| 27 | |
| 28 | var 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 &&` + |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 33 | `${config.KotlincCmd} ${config.JavacHeapFlags} $kotlincFlags ` + |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 34 | `-jvm-target $kotlinJvmTarget -Xbuild-file=$kotlinBuildFile && ` + |
Colin Cross | 44c29a8 | 2019-01-24 16:36:57 -0800 | [diff] [blame] | 35 | `${config.SoongZipCmd} -jar -o $out -C $classesDir -D $classesDir && ` + |
| 36 | `rm -rf "$srcJarDir"`, |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 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 | |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 49 | // 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] | 50 | func kotlinCompile(ctx android.ModuleContext, outputFile android.WritablePath, |
| 51 | srcFiles, srcJars android.Paths, |
| 52 | flags javaBuilderFlags) { |
| 53 | |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 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, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 62 | Inputs: srcFiles, |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 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 | } |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 76 | |
| 77 | var 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 ` + |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 91 | `$kaptProcessor ` + |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 92 | `-Xbuild-file=$kotlinBuildFile && ` + |
Colin Cross | 44c29a8 | 2019-01-24 16:36:57 -0800 | [diff] [blame] | 93 | `${config.SoongZipCmd} -jar -o $out -C $kaptDir/sources -D $kaptDir/sources && ` + |
| 94 | `rm -rf "$srcJarDir"`, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 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 | }, |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 106 | "kotlincFlags", "encodedJavacFlags", "kaptProcessorPath", "kaptProcessor", |
| 107 | "classpath", "srcJars", "srcJarDir", "kaptDir", "kotlinJvmTarget", "kotlinBuildFile") |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 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. |
| 113 | func 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 | |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 124 | kaptProcessor := "" |
| 125 | if flags.processor != "" { |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 126 | kaptProcessor = "-P plugin:org.jetbrains.kotlin.kapt3:processors=" + flags.processor |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 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, " "), |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 147 | "kaptProcessor": kaptProcessor, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 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. |
| 155 | func 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 | } |