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 | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 31 | Command: `rm -rf "$classesDir" "$srcJarDir" "$kotlinBuildFile" "$emptyDir" && ` + |
| 32 | `mkdir -p "$classesDir" "$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 | 6c6e6cd | 2019-05-08 14:30:12 -0700 | [diff] [blame] | 34 | `${config.GenKotlinBuildFileCmd} $classpath "$name" $classesDir $out.rsp $srcJarDir/list > $kotlinBuildFile &&` + |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 35 | `${config.KotlincCmd} ${config.JavacHeapFlags} $kotlincFlags ` + |
Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 36 | `-jvm-target $kotlinJvmTarget -Xbuild-file=$kotlinBuildFile -kotlin-home $emptyDir && ` + |
Colin Cross | 44c29a8 | 2019-01-24 16:36:57 -0800 | [diff] [blame] | 37 | `${config.SoongZipCmd} -jar -o $out -C $classesDir -D $classesDir && ` + |
| 38 | `rm -rf "$srcJarDir"`, |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 39 | CommandDeps: []string{ |
| 40 | "${config.KotlincCmd}", |
| 41 | "${config.KotlinCompilerJar}", |
Dan Willemsen | aad1960 | 2019-04-07 09:44:35 -0700 | [diff] [blame] | 42 | "${config.KotlinPreloaderJar}", |
| 43 | "${config.KotlinReflectJar}", |
| 44 | "${config.KotlinScriptRuntimeJar}", |
| 45 | "${config.KotlinStdlibJar}", |
| 46 | "${config.KotlinTrove4jJar}", |
Dan Willemsen | 1cfbdbe | 2019-06-13 04:59:17 +0000 | [diff] [blame] | 47 | "${config.KotlinAnnotationJar}", |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 48 | "${config.GenKotlinBuildFileCmd}", |
| 49 | "${config.SoongZipCmd}", |
| 50 | "${config.ZipSyncCmd}", |
| 51 | }, |
| 52 | Rspfile: "$out.rsp", |
| 53 | RspfileContent: `$in`, |
| 54 | }, |
Colin Cross | 6c6e6cd | 2019-05-08 14:30:12 -0700 | [diff] [blame] | 55 | "kotlincFlags", "classpath", "srcJars", "srcJarDir", "classesDir", "kotlinJvmTarget", "kotlinBuildFile", |
| 56 | "emptyDir", "name") |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 57 | |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 58 | // 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] | 59 | func kotlinCompile(ctx android.ModuleContext, outputFile android.WritablePath, |
| 60 | srcFiles, srcJars android.Paths, |
| 61 | flags javaBuilderFlags) { |
| 62 | |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 63 | var deps android.Paths |
| 64 | deps = append(deps, flags.kotlincClasspath...) |
| 65 | deps = append(deps, srcJars...) |
| 66 | |
Colin Cross | 6c6e6cd | 2019-05-08 14:30:12 -0700 | [diff] [blame] | 67 | kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName()) |
| 68 | kotlinName = strings.ReplaceAll(kotlinName, "/", "__") |
| 69 | |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 70 | ctx.Build(pctx, android.BuildParams{ |
| 71 | Rule: kotlinc, |
| 72 | Description: "kotlinc", |
| 73 | Output: outputFile, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 74 | Inputs: srcFiles, |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 75 | Implicits: deps, |
| 76 | Args: map[string]string{ |
| 77 | "classpath": flags.kotlincClasspath.FormJavaClassPath("-classpath"), |
| 78 | "kotlincFlags": flags.kotlincFlags, |
| 79 | "srcJars": strings.Join(srcJars.Strings(), " "), |
| 80 | "classesDir": android.PathForModuleOut(ctx, "kotlinc", "classes").String(), |
| 81 | "srcJarDir": android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(), |
| 82 | "kotlinBuildFile": android.PathForModuleOut(ctx, "kotlinc-build.xml").String(), |
Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 83 | "emptyDir": android.PathForModuleOut(ctx, "kotlinc", "empty").String(), |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 84 | // http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8 |
| 85 | "kotlinJvmTarget": "1.8", |
Colin Cross | 6c6e6cd | 2019-05-08 14:30:12 -0700 | [diff] [blame] | 86 | "name": kotlinName, |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 87 | }, |
| 88 | }) |
| 89 | } |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 90 | |
Ramy Medhat | 8ea054a | 2020-01-27 14:19:44 -0500 | [diff] [blame] | 91 | var kapt = pctx.AndroidRemoteStaticRule("kapt", android.RemoteRuleSupports{Goma: true}, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 92 | blueprint.RuleParams{ |
| 93 | Command: `rm -rf "$srcJarDir" "$kotlinBuildFile" "$kaptDir" && mkdir -p "$srcJarDir" "$kaptDir" && ` + |
| 94 | `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` + |
Colin Cross | 6c6e6cd | 2019-05-08 14:30:12 -0700 | [diff] [blame] | 95 | `${config.GenKotlinBuildFileCmd} $classpath "$name" "" $out.rsp $srcJarDir/list > $kotlinBuildFile &&` + |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 96 | `${config.KotlincCmd} ${config.KotlincSuppressJDK9Warnings} ${config.JavacHeapFlags} $kotlincFlags ` + |
| 97 | `-Xplugin=${config.KotlinKaptJar} ` + |
| 98 | `-P plugin:org.jetbrains.kotlin.kapt3:sources=$kaptDir/sources ` + |
| 99 | `-P plugin:org.jetbrains.kotlin.kapt3:classes=$kaptDir/classes ` + |
| 100 | `-P plugin:org.jetbrains.kotlin.kapt3:stubs=$kaptDir/stubs ` + |
| 101 | `-P plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true ` + |
| 102 | `-P plugin:org.jetbrains.kotlin.kapt3:aptMode=stubsAndApt ` + |
| 103 | `-P plugin:org.jetbrains.kotlin.kapt3:javacArguments=$encodedJavacFlags ` + |
| 104 | `$kaptProcessorPath ` + |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 105 | `$kaptProcessor ` + |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 106 | `-Xbuild-file=$kotlinBuildFile && ` + |
Colin Cross | 44c29a8 | 2019-01-24 16:36:57 -0800 | [diff] [blame] | 107 | `${config.SoongZipCmd} -jar -o $out -C $kaptDir/sources -D $kaptDir/sources && ` + |
| 108 | `rm -rf "$srcJarDir"`, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 109 | CommandDeps: []string{ |
| 110 | "${config.KotlincCmd}", |
| 111 | "${config.KotlinCompilerJar}", |
| 112 | "${config.KotlinKaptJar}", |
| 113 | "${config.GenKotlinBuildFileCmd}", |
| 114 | "${config.SoongZipCmd}", |
| 115 | "${config.ZipSyncCmd}", |
| 116 | }, |
| 117 | Rspfile: "$out.rsp", |
| 118 | RspfileContent: `$in`, |
| 119 | }, |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 120 | "kotlincFlags", "encodedJavacFlags", "kaptProcessorPath", "kaptProcessor", |
Colin Cross | 6c6e6cd | 2019-05-08 14:30:12 -0700 | [diff] [blame] | 121 | "classpath", "srcJars", "srcJarDir", "kaptDir", "kotlinJvmTarget", "kotlinBuildFile", "name") |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 122 | |
| 123 | // kotlinKapt performs Kotlin-compatible annotation processing. It takes .kt and .java sources and srcjars, and runs |
| 124 | // annotation processors over all of them, producing a srcjar of generated code in outputFile. The srcjar should be |
| 125 | // added as an additional input to kotlinc and javac rules, and the javac rule should have annotation processing |
| 126 | // disabled. |
| 127 | func kotlinKapt(ctx android.ModuleContext, outputFile android.WritablePath, |
| 128 | srcFiles, srcJars android.Paths, |
| 129 | flags javaBuilderFlags) { |
| 130 | |
| 131 | var deps android.Paths |
| 132 | deps = append(deps, flags.kotlincClasspath...) |
| 133 | deps = append(deps, srcJars...) |
| 134 | deps = append(deps, flags.processorPath...) |
| 135 | |
Colin Cross | c2557d1 | 2019-10-31 15:22:57 -0700 | [diff] [blame] | 136 | kaptProcessorPath := flags.processorPath.FormRepeatedClassPath("-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=") |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 137 | |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 138 | kaptProcessor := "" |
Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 139 | for i, p := range flags.processors { |
| 140 | if i > 0 { |
| 141 | kaptProcessor += " " |
| 142 | } |
| 143 | kaptProcessor += "-P plugin:org.jetbrains.kotlin.kapt3:processors=" + p |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 146 | encodedJavacFlags := kaptEncodeFlags([][2]string{ |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 147 | {"-source", flags.javaVersion.String()}, |
| 148 | {"-target", flags.javaVersion.String()}, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 149 | }) |
| 150 | |
Colin Cross | 6c6e6cd | 2019-05-08 14:30:12 -0700 | [diff] [blame] | 151 | kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName()) |
| 152 | kotlinName = strings.ReplaceAll(kotlinName, "/", "__") |
| 153 | |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 154 | ctx.Build(pctx, android.BuildParams{ |
| 155 | Rule: kapt, |
| 156 | Description: "kapt", |
| 157 | Output: outputFile, |
| 158 | Inputs: srcFiles, |
| 159 | Implicits: deps, |
| 160 | Args: map[string]string{ |
| 161 | "classpath": flags.kotlincClasspath.FormJavaClassPath("-classpath"), |
| 162 | "kotlincFlags": flags.kotlincFlags, |
| 163 | "srcJars": strings.Join(srcJars.Strings(), " "), |
| 164 | "srcJarDir": android.PathForModuleOut(ctx, "kapt", "srcJars").String(), |
| 165 | "kotlinBuildFile": android.PathForModuleOut(ctx, "kapt", "build.xml").String(), |
| 166 | "kaptProcessorPath": strings.Join(kaptProcessorPath, " "), |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 167 | "kaptProcessor": kaptProcessor, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 168 | "kaptDir": android.PathForModuleOut(ctx, "kapt/gen").String(), |
| 169 | "encodedJavacFlags": encodedJavacFlags, |
Colin Cross | 6c6e6cd | 2019-05-08 14:30:12 -0700 | [diff] [blame] | 170 | "name": kotlinName, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 171 | }, |
| 172 | }) |
| 173 | } |
| 174 | |
| 175 | // kapt converts a list of key, value pairs into a base64 encoded Java serialization, which is what kapt expects. |
| 176 | func kaptEncodeFlags(options [][2]string) string { |
| 177 | buf := &bytes.Buffer{} |
| 178 | |
| 179 | binary.Write(buf, binary.BigEndian, uint32(len(options))) |
| 180 | for _, option := range options { |
| 181 | binary.Write(buf, binary.BigEndian, uint16(len(option[0]))) |
| 182 | buf.WriteString(option[0]) |
| 183 | binary.Write(buf, binary.BigEndian, uint16(len(option[1]))) |
| 184 | buf.WriteString(option[1]) |
| 185 | } |
| 186 | |
| 187 | header := &bytes.Buffer{} |
| 188 | header.Write([]byte{0xac, 0xed, 0x00, 0x05}) // java serialization header |
| 189 | |
| 190 | if buf.Len() < 256 { |
| 191 | header.WriteByte(0x77) // blockdata |
| 192 | header.WriteByte(byte(buf.Len())) |
| 193 | } else { |
| 194 | header.WriteByte(0x7a) // blockdatalong |
| 195 | binary.Write(header, binary.BigEndian, uint32(buf.Len())) |
| 196 | } |
| 197 | |
| 198 | return base64.StdEncoding.EncodeToString(append(header.Bytes(), buf.Bytes()...)) |
| 199 | } |