blob: 8067ad521dc7a924e7df25b1cb682bd602d5b7d1 [file] [log] [blame]
Colin Cross21fc9bb2019-01-18 15:05:09 -08001// 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 (
Colin Crossafbb1732019-01-17 15:42:52 -080018 "bytes"
19 "encoding/base64"
20 "encoding/binary"
Colin Cross6c6e6cd2019-05-08 14:30:12 -070021 "path/filepath"
Colin Cross21fc9bb2019-01-18 15:05:09 -080022 "strings"
23
Colin Crossafbb1732019-01-17 15:42:52 -080024 "android/soong/android"
25
Colin Cross21fc9bb2019-01-18 15:05:09 -080026 "github.com/google/blueprint"
27)
28
Ramy Medhat8ea054a2020-01-27 14:19:44 -050029var kotlinc = pctx.AndroidRemoteStaticRule("kotlinc", android.RemoteRuleSupports{Goma: true},
Colin Cross21fc9bb2019-01-18 15:05:09 -080030 blueprint.RuleParams{
Colin Crossdf8eebe2019-04-09 15:29:41 -070031 Command: `rm -rf "$classesDir" "$srcJarDir" "$kotlinBuildFile" "$emptyDir" && ` +
32 `mkdir -p "$classesDir" "$srcJarDir" "$emptyDir" && ` +
Colin Cross21fc9bb2019-01-18 15:05:09 -080033 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
Colin Cross9b1aa0c2020-06-25 17:12:28 -070034 `${config.GenKotlinBuildFileCmd} --classpath "$classpath" --name "$name"` +
35 ` --out_dir "$classesDir" --srcs "$out.rsp" --srcs "$srcJarDir/list"` +
Colin Crossa4c8cc62020-06-25 17:13:36 -070036 ` $commonSrcFilesArg --out "$kotlinBuildFile" && ` +
Colin Crossafbb1732019-01-17 15:42:52 -080037 `${config.KotlincCmd} ${config.JavacHeapFlags} $kotlincFlags ` +
Colin Crossdf8eebe2019-04-09 15:29:41 -070038 `-jvm-target $kotlinJvmTarget -Xbuild-file=$kotlinBuildFile -kotlin-home $emptyDir && ` +
Colin Cross44c29a82019-01-24 16:36:57 -080039 `${config.SoongZipCmd} -jar -o $out -C $classesDir -D $classesDir && ` +
40 `rm -rf "$srcJarDir"`,
Colin Cross21fc9bb2019-01-18 15:05:09 -080041 CommandDeps: []string{
42 "${config.KotlincCmd}",
43 "${config.KotlinCompilerJar}",
Dan Willemsenaad19602019-04-07 09:44:35 -070044 "${config.KotlinPreloaderJar}",
45 "${config.KotlinReflectJar}",
46 "${config.KotlinScriptRuntimeJar}",
47 "${config.KotlinStdlibJar}",
48 "${config.KotlinTrove4jJar}",
Dan Willemsen1cfbdbe2019-06-13 04:59:17 +000049 "${config.KotlinAnnotationJar}",
Colin Cross21fc9bb2019-01-18 15:05:09 -080050 "${config.GenKotlinBuildFileCmd}",
51 "${config.SoongZipCmd}",
52 "${config.ZipSyncCmd}",
53 },
54 Rspfile: "$out.rsp",
55 RspfileContent: `$in`,
56 },
Colin Crossa4c8cc62020-06-25 17:13:36 -070057 "kotlincFlags", "classpath", "srcJars", "commonSrcFilesArg", "srcJarDir", "classesDir",
58 "kotlinJvmTarget", "kotlinBuildFile", "emptyDir", "name")
59
60func kotlinCommonSrcsList(ctx android.ModuleContext, commonSrcFiles android.Paths) android.OptionalPath {
61 if len(commonSrcFiles) > 0 {
62 // The list of common_srcs may be too long to put on the command line, but
63 // we can't use the rsp file because it is already being used for srcs.
64 // Insert a second rule to write out the list of resources to a file.
65 commonSrcsList := android.PathForModuleOut(ctx, "kotlinc_common_srcs.list")
Colin Crossf1a035e2020-11-16 17:32:30 -080066 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossa4c8cc62020-06-25 17:13:36 -070067 rule.Command().Text("cp").FlagWithRspFileInputList("", commonSrcFiles).Output(commonSrcsList)
Colin Crossf1a035e2020-11-16 17:32:30 -080068 rule.Build("kotlin_common_srcs_list", "kotlin common_srcs list")
Colin Crossa4c8cc62020-06-25 17:13:36 -070069 return android.OptionalPathForPath(commonSrcsList)
70 }
71 return android.OptionalPath{}
72}
Colin Cross21fc9bb2019-01-18 15:05:09 -080073
Colin Crossafbb1732019-01-17 15:42:52 -080074// kotlinCompile takes .java and .kt sources and srcJars, and compiles the .kt sources into a classes jar in outputFile.
Colin Cross21fc9bb2019-01-18 15:05:09 -080075func kotlinCompile(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Crossa4c8cc62020-06-25 17:13:36 -070076 srcFiles, commonSrcFiles, srcJars android.Paths,
Colin Cross21fc9bb2019-01-18 15:05:09 -080077 flags javaBuilderFlags) {
78
Colin Cross21fc9bb2019-01-18 15:05:09 -080079 var deps android.Paths
80 deps = append(deps, flags.kotlincClasspath...)
81 deps = append(deps, srcJars...)
Colin Crossa4c8cc62020-06-25 17:13:36 -070082 deps = append(deps, commonSrcFiles...)
Colin Cross21fc9bb2019-01-18 15:05:09 -080083
Colin Cross6c6e6cd2019-05-08 14:30:12 -070084 kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName())
85 kotlinName = strings.ReplaceAll(kotlinName, "/", "__")
86
Colin Crossa4c8cc62020-06-25 17:13:36 -070087 commonSrcsList := kotlinCommonSrcsList(ctx, commonSrcFiles)
88 commonSrcFilesArg := ""
89 if commonSrcsList.Valid() {
90 deps = append(deps, commonSrcsList.Path())
91 commonSrcFilesArg = "--common_srcs " + commonSrcsList.String()
92 }
93
Colin Cross21fc9bb2019-01-18 15:05:09 -080094 ctx.Build(pctx, android.BuildParams{
95 Rule: kotlinc,
96 Description: "kotlinc",
97 Output: outputFile,
Colin Crossafbb1732019-01-17 15:42:52 -080098 Inputs: srcFiles,
Colin Cross21fc9bb2019-01-18 15:05:09 -080099 Implicits: deps,
100 Args: map[string]string{
Colin Crossa4c8cc62020-06-25 17:13:36 -0700101 "classpath": flags.kotlincClasspath.FormJavaClassPath(""),
102 "kotlincFlags": flags.kotlincFlags,
103 "commonSrcFilesArg": commonSrcFilesArg,
104 "srcJars": strings.Join(srcJars.Strings(), " "),
105 "classesDir": android.PathForModuleOut(ctx, "kotlinc", "classes").String(),
106 "srcJarDir": android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(),
107 "kotlinBuildFile": android.PathForModuleOut(ctx, "kotlinc-build.xml").String(),
108 "emptyDir": android.PathForModuleOut(ctx, "kotlinc", "empty").String(),
Colin Cross21fc9bb2019-01-18 15:05:09 -0800109 // http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8
110 "kotlinJvmTarget": "1.8",
Colin Cross6c6e6cd2019-05-08 14:30:12 -0700111 "name": kotlinName,
Colin Cross21fc9bb2019-01-18 15:05:09 -0800112 },
113 })
114}
Colin Crossafbb1732019-01-17 15:42:52 -0800115
Ramy Medhat8ea054a2020-01-27 14:19:44 -0500116var kapt = pctx.AndroidRemoteStaticRule("kapt", android.RemoteRuleSupports{Goma: true},
Colin Crossafbb1732019-01-17 15:42:52 -0800117 blueprint.RuleParams{
Colin Cross9ca38d22020-06-18 15:46:32 -0700118 Command: `rm -rf "$srcJarDir" "$kotlinBuildFile" "$kaptDir" && ` +
119 `mkdir -p "$srcJarDir" "$kaptDir/sources" "$kaptDir/classes" && ` +
Colin Crossafbb1732019-01-17 15:42:52 -0800120 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
Colin Cross9b1aa0c2020-06-25 17:12:28 -0700121 `${config.GenKotlinBuildFileCmd} --classpath "$classpath" --name "$name"` +
122 ` --srcs "$out.rsp" --srcs "$srcJarDir/list"` +
Colin Crossa4c8cc62020-06-25 17:13:36 -0700123 ` $commonSrcFilesArg --out "$kotlinBuildFile" && ` +
Colin Crossafbb1732019-01-17 15:42:52 -0800124 `${config.KotlincCmd} ${config.KotlincSuppressJDK9Warnings} ${config.JavacHeapFlags} $kotlincFlags ` +
125 `-Xplugin=${config.KotlinKaptJar} ` +
126 `-P plugin:org.jetbrains.kotlin.kapt3:sources=$kaptDir/sources ` +
127 `-P plugin:org.jetbrains.kotlin.kapt3:classes=$kaptDir/classes ` +
128 `-P plugin:org.jetbrains.kotlin.kapt3:stubs=$kaptDir/stubs ` +
129 `-P plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true ` +
130 `-P plugin:org.jetbrains.kotlin.kapt3:aptMode=stubsAndApt ` +
131 `-P plugin:org.jetbrains.kotlin.kapt3:javacArguments=$encodedJavacFlags ` +
132 `$kaptProcessorPath ` +
Colin Crossbe9cdb82019-01-21 21:37:16 -0800133 `$kaptProcessor ` +
Colin Crossafbb1732019-01-17 15:42:52 -0800134 `-Xbuild-file=$kotlinBuildFile && ` +
Colin Cross44c29a82019-01-24 16:36:57 -0800135 `${config.SoongZipCmd} -jar -o $out -C $kaptDir/sources -D $kaptDir/sources && ` +
Colin Cross9ca38d22020-06-18 15:46:32 -0700136 `${config.SoongZipCmd} -jar -o $classesJarOut -C $kaptDir/classes -D $kaptDir/classes && ` +
Colin Cross44c29a82019-01-24 16:36:57 -0800137 `rm -rf "$srcJarDir"`,
Colin Crossafbb1732019-01-17 15:42:52 -0800138 CommandDeps: []string{
139 "${config.KotlincCmd}",
140 "${config.KotlinCompilerJar}",
141 "${config.KotlinKaptJar}",
142 "${config.GenKotlinBuildFileCmd}",
143 "${config.SoongZipCmd}",
144 "${config.ZipSyncCmd}",
145 },
146 Rspfile: "$out.rsp",
147 RspfileContent: `$in`,
148 },
Colin Crossbe9cdb82019-01-21 21:37:16 -0800149 "kotlincFlags", "encodedJavacFlags", "kaptProcessorPath", "kaptProcessor",
Colin Crossa4c8cc62020-06-25 17:13:36 -0700150 "classpath", "srcJars", "commonSrcFilesArg", "srcJarDir", "kaptDir", "kotlinJvmTarget",
151 "kotlinBuildFile", "name", "classesJarOut")
Colin Crossafbb1732019-01-17 15:42:52 -0800152
153// kotlinKapt performs Kotlin-compatible annotation processing. It takes .kt and .java sources and srcjars, and runs
154// annotation processors over all of them, producing a srcjar of generated code in outputFile. The srcjar should be
155// added as an additional input to kotlinc and javac rules, and the javac rule should have annotation processing
156// disabled.
Colin Cross9ca38d22020-06-18 15:46:32 -0700157func kotlinKapt(ctx android.ModuleContext, srcJarOutputFile, resJarOutputFile android.WritablePath,
Colin Crossa4c8cc62020-06-25 17:13:36 -0700158 srcFiles, commonSrcFiles, srcJars android.Paths,
Colin Crossafbb1732019-01-17 15:42:52 -0800159 flags javaBuilderFlags) {
160
Colin Crossa4c8cc62020-06-25 17:13:36 -0700161 srcFiles = append(android.Paths(nil), srcFiles...)
162
Colin Crossafbb1732019-01-17 15:42:52 -0800163 var deps android.Paths
164 deps = append(deps, flags.kotlincClasspath...)
165 deps = append(deps, srcJars...)
166 deps = append(deps, flags.processorPath...)
Colin Crossa4c8cc62020-06-25 17:13:36 -0700167 deps = append(deps, commonSrcFiles...)
168
169 commonSrcsList := kotlinCommonSrcsList(ctx, commonSrcFiles)
170 commonSrcFilesArg := ""
171 if commonSrcsList.Valid() {
172 deps = append(deps, commonSrcsList.Path())
173 commonSrcFilesArg = "--common_srcs " + commonSrcsList.String()
174 }
Colin Crossafbb1732019-01-17 15:42:52 -0800175
Colin Crossc2557d12019-10-31 15:22:57 -0700176 kaptProcessorPath := flags.processorPath.FormRepeatedClassPath("-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=")
Colin Crossafbb1732019-01-17 15:42:52 -0800177
Colin Crossbe9cdb82019-01-21 21:37:16 -0800178 kaptProcessor := ""
Colin Cross5a116862020-04-22 11:44:34 -0700179 for i, p := range flags.processors {
180 if i > 0 {
181 kaptProcessor += " "
182 }
183 kaptProcessor += "-P plugin:org.jetbrains.kotlin.kapt3:processors=" + p
Colin Crossbe9cdb82019-01-21 21:37:16 -0800184 }
185
Colin Crossafbb1732019-01-17 15:42:52 -0800186 encodedJavacFlags := kaptEncodeFlags([][2]string{
Colin Cross1e743852019-10-28 11:37:20 -0700187 {"-source", flags.javaVersion.String()},
188 {"-target", flags.javaVersion.String()},
Colin Crossafbb1732019-01-17 15:42:52 -0800189 })
190
Colin Cross6c6e6cd2019-05-08 14:30:12 -0700191 kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName())
192 kotlinName = strings.ReplaceAll(kotlinName, "/", "__")
193
Colin Crossafbb1732019-01-17 15:42:52 -0800194 ctx.Build(pctx, android.BuildParams{
Colin Cross9ca38d22020-06-18 15:46:32 -0700195 Rule: kapt,
196 Description: "kapt",
197 Output: srcJarOutputFile,
198 ImplicitOutput: resJarOutputFile,
199 Inputs: srcFiles,
200 Implicits: deps,
Colin Crossafbb1732019-01-17 15:42:52 -0800201 Args: map[string]string{
Colin Cross9b1aa0c2020-06-25 17:12:28 -0700202 "classpath": flags.kotlincClasspath.FormJavaClassPath(""),
Colin Crossafbb1732019-01-17 15:42:52 -0800203 "kotlincFlags": flags.kotlincFlags,
Colin Crossa4c8cc62020-06-25 17:13:36 -0700204 "commonSrcFilesArg": commonSrcFilesArg,
Colin Crossafbb1732019-01-17 15:42:52 -0800205 "srcJars": strings.Join(srcJars.Strings(), " "),
206 "srcJarDir": android.PathForModuleOut(ctx, "kapt", "srcJars").String(),
207 "kotlinBuildFile": android.PathForModuleOut(ctx, "kapt", "build.xml").String(),
208 "kaptProcessorPath": strings.Join(kaptProcessorPath, " "),
Colin Crossbe9cdb82019-01-21 21:37:16 -0800209 "kaptProcessor": kaptProcessor,
Colin Crossafbb1732019-01-17 15:42:52 -0800210 "kaptDir": android.PathForModuleOut(ctx, "kapt/gen").String(),
211 "encodedJavacFlags": encodedJavacFlags,
Colin Cross6c6e6cd2019-05-08 14:30:12 -0700212 "name": kotlinName,
Colin Cross9ca38d22020-06-18 15:46:32 -0700213 "classesJarOut": resJarOutputFile.String(),
Colin Crossafbb1732019-01-17 15:42:52 -0800214 },
215 })
216}
217
218// kapt converts a list of key, value pairs into a base64 encoded Java serialization, which is what kapt expects.
219func kaptEncodeFlags(options [][2]string) string {
220 buf := &bytes.Buffer{}
221
222 binary.Write(buf, binary.BigEndian, uint32(len(options)))
223 for _, option := range options {
224 binary.Write(buf, binary.BigEndian, uint16(len(option[0])))
225 buf.WriteString(option[0])
226 binary.Write(buf, binary.BigEndian, uint16(len(option[1])))
227 buf.WriteString(option[1])
228 }
229
230 header := &bytes.Buffer{}
231 header.Write([]byte{0xac, 0xed, 0x00, 0x05}) // java serialization header
232
233 if buf.Len() < 256 {
234 header.WriteByte(0x77) // blockdata
235 header.WriteByte(byte(buf.Len()))
236 } else {
237 header.WriteByte(0x7a) // blockdatalong
238 binary.Write(header, binary.BigEndian, uint32(buf.Len()))
239 }
240
241 return base64.StdEncoding.EncodeToString(append(header.Bytes(), buf.Bytes()...))
242}