blob: e4f1bc198679760f30e972f4d956d27932fc74be [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 Crossd47f4ac2021-03-26 10:52:02 -070037 `${config.KotlincCmd} ${config.KotlincSuppressJDK9Warnings} ${config.JavacHeapFlags} ` +
38 `$kotlincFlags -jvm-target $kotlinJvmTarget -Xbuild-file=$kotlinBuildFile ` +
39 `-kotlin-home $emptyDir && ` +
Colin Cross44c29a82019-01-24 16:36:57 -080040 `${config.SoongZipCmd} -jar -o $out -C $classesDir -D $classesDir && ` +
41 `rm -rf "$srcJarDir"`,
Colin Cross21fc9bb2019-01-18 15:05:09 -080042 CommandDeps: []string{
43 "${config.KotlincCmd}",
44 "${config.KotlinCompilerJar}",
Dan Willemsenaad19602019-04-07 09:44:35 -070045 "${config.KotlinPreloaderJar}",
46 "${config.KotlinReflectJar}",
47 "${config.KotlinScriptRuntimeJar}",
48 "${config.KotlinStdlibJar}",
49 "${config.KotlinTrove4jJar}",
Dan Willemsen1cfbdbe2019-06-13 04:59:17 +000050 "${config.KotlinAnnotationJar}",
Colin Cross21fc9bb2019-01-18 15:05:09 -080051 "${config.GenKotlinBuildFileCmd}",
52 "${config.SoongZipCmd}",
53 "${config.ZipSyncCmd}",
54 },
55 Rspfile: "$out.rsp",
56 RspfileContent: `$in`,
57 },
Colin Crossa4c8cc62020-06-25 17:13:36 -070058 "kotlincFlags", "classpath", "srcJars", "commonSrcFilesArg", "srcJarDir", "classesDir",
59 "kotlinJvmTarget", "kotlinBuildFile", "emptyDir", "name")
60
61func kotlinCommonSrcsList(ctx android.ModuleContext, commonSrcFiles android.Paths) android.OptionalPath {
62 if len(commonSrcFiles) > 0 {
63 // The list of common_srcs may be too long to put on the command line, but
64 // we can't use the rsp file because it is already being used for srcs.
65 // Insert a second rule to write out the list of resources to a file.
66 commonSrcsList := android.PathForModuleOut(ctx, "kotlinc_common_srcs.list")
Colin Crossf1a035e2020-11-16 17:32:30 -080067 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross70c47412021-03-12 17:48:14 -080068 rule.Command().Text("cp").
69 FlagWithRspFileInputList("", commonSrcsList.ReplaceExtension(ctx, "rsp"), commonSrcFiles).
70 Output(commonSrcsList)
Colin Crossf1a035e2020-11-16 17:32:30 -080071 rule.Build("kotlin_common_srcs_list", "kotlin common_srcs list")
Colin Crossa4c8cc62020-06-25 17:13:36 -070072 return android.OptionalPathForPath(commonSrcsList)
73 }
74 return android.OptionalPath{}
75}
Colin Cross21fc9bb2019-01-18 15:05:09 -080076
Colin Crossafbb1732019-01-17 15:42:52 -080077// 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 -080078func kotlinCompile(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Crossa4c8cc62020-06-25 17:13:36 -070079 srcFiles, commonSrcFiles, srcJars android.Paths,
Colin Cross21fc9bb2019-01-18 15:05:09 -080080 flags javaBuilderFlags) {
81
Colin Cross21fc9bb2019-01-18 15:05:09 -080082 var deps android.Paths
83 deps = append(deps, flags.kotlincClasspath...)
Colin Crossa1ff7c62021-09-17 14:11:52 -070084 deps = append(deps, flags.kotlincDeps...)
Colin Cross21fc9bb2019-01-18 15:05:09 -080085 deps = append(deps, srcJars...)
Colin Crossa4c8cc62020-06-25 17:13:36 -070086 deps = append(deps, commonSrcFiles...)
Colin Cross21fc9bb2019-01-18 15:05:09 -080087
Colin Cross6c6e6cd2019-05-08 14:30:12 -070088 kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName())
89 kotlinName = strings.ReplaceAll(kotlinName, "/", "__")
90
Colin Crossa4c8cc62020-06-25 17:13:36 -070091 commonSrcsList := kotlinCommonSrcsList(ctx, commonSrcFiles)
92 commonSrcFilesArg := ""
93 if commonSrcsList.Valid() {
94 deps = append(deps, commonSrcsList.Path())
95 commonSrcFilesArg = "--common_srcs " + commonSrcsList.String()
96 }
97
Colin Cross21fc9bb2019-01-18 15:05:09 -080098 ctx.Build(pctx, android.BuildParams{
99 Rule: kotlinc,
100 Description: "kotlinc",
101 Output: outputFile,
Colin Crossafbb1732019-01-17 15:42:52 -0800102 Inputs: srcFiles,
Colin Cross21fc9bb2019-01-18 15:05:09 -0800103 Implicits: deps,
104 Args: map[string]string{
Colin Crossa4c8cc62020-06-25 17:13:36 -0700105 "classpath": flags.kotlincClasspath.FormJavaClassPath(""),
106 "kotlincFlags": flags.kotlincFlags,
107 "commonSrcFilesArg": commonSrcFilesArg,
108 "srcJars": strings.Join(srcJars.Strings(), " "),
109 "classesDir": android.PathForModuleOut(ctx, "kotlinc", "classes").String(),
110 "srcJarDir": android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(),
111 "kotlinBuildFile": android.PathForModuleOut(ctx, "kotlinc-build.xml").String(),
112 "emptyDir": android.PathForModuleOut(ctx, "kotlinc", "empty").String(),
Colin Cross21fc9bb2019-01-18 15:05:09 -0800113 // http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8
114 "kotlinJvmTarget": "1.8",
Colin Cross6c6e6cd2019-05-08 14:30:12 -0700115 "name": kotlinName,
Colin Cross21fc9bb2019-01-18 15:05:09 -0800116 },
117 })
118}
Colin Crossafbb1732019-01-17 15:42:52 -0800119
Ramy Medhat8ea054a2020-01-27 14:19:44 -0500120var kapt = pctx.AndroidRemoteStaticRule("kapt", android.RemoteRuleSupports{Goma: true},
Colin Crossafbb1732019-01-17 15:42:52 -0800121 blueprint.RuleParams{
Colin Cross9ca38d22020-06-18 15:46:32 -0700122 Command: `rm -rf "$srcJarDir" "$kotlinBuildFile" "$kaptDir" && ` +
123 `mkdir -p "$srcJarDir" "$kaptDir/sources" "$kaptDir/classes" && ` +
Colin Crossafbb1732019-01-17 15:42:52 -0800124 `${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
Colin Cross9b1aa0c2020-06-25 17:12:28 -0700125 `${config.GenKotlinBuildFileCmd} --classpath "$classpath" --name "$name"` +
126 ` --srcs "$out.rsp" --srcs "$srcJarDir/list"` +
Colin Crossa4c8cc62020-06-25 17:13:36 -0700127 ` $commonSrcFilesArg --out "$kotlinBuildFile" && ` +
Colin Crossd47f4ac2021-03-26 10:52:02 -0700128 `${config.KotlincCmd} ${config.KaptSuppressJDK9Warnings} ${config.KotlincSuppressJDK9Warnings} ` +
129 `${config.JavacHeapFlags} $kotlincFlags -Xplugin=${config.KotlinKaptJar} ` +
Colin Crossafbb1732019-01-17 15:42:52 -0800130 `-P plugin:org.jetbrains.kotlin.kapt3:sources=$kaptDir/sources ` +
131 `-P plugin:org.jetbrains.kotlin.kapt3:classes=$kaptDir/classes ` +
132 `-P plugin:org.jetbrains.kotlin.kapt3:stubs=$kaptDir/stubs ` +
133 `-P plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true ` +
134 `-P plugin:org.jetbrains.kotlin.kapt3:aptMode=stubsAndApt ` +
135 `-P plugin:org.jetbrains.kotlin.kapt3:javacArguments=$encodedJavacFlags ` +
136 `$kaptProcessorPath ` +
Colin Crossbe9cdb82019-01-21 21:37:16 -0800137 `$kaptProcessor ` +
Colin Crossafbb1732019-01-17 15:42:52 -0800138 `-Xbuild-file=$kotlinBuildFile && ` +
Colin Cross44c29a82019-01-24 16:36:57 -0800139 `${config.SoongZipCmd} -jar -o $out -C $kaptDir/sources -D $kaptDir/sources && ` +
Colin Cross9ca38d22020-06-18 15:46:32 -0700140 `${config.SoongZipCmd} -jar -o $classesJarOut -C $kaptDir/classes -D $kaptDir/classes && ` +
Colin Cross44c29a82019-01-24 16:36:57 -0800141 `rm -rf "$srcJarDir"`,
Colin Crossafbb1732019-01-17 15:42:52 -0800142 CommandDeps: []string{
143 "${config.KotlincCmd}",
144 "${config.KotlinCompilerJar}",
145 "${config.KotlinKaptJar}",
146 "${config.GenKotlinBuildFileCmd}",
147 "${config.SoongZipCmd}",
148 "${config.ZipSyncCmd}",
149 },
150 Rspfile: "$out.rsp",
151 RspfileContent: `$in`,
152 },
Colin Crossbe9cdb82019-01-21 21:37:16 -0800153 "kotlincFlags", "encodedJavacFlags", "kaptProcessorPath", "kaptProcessor",
Colin Crossa4c8cc62020-06-25 17:13:36 -0700154 "classpath", "srcJars", "commonSrcFilesArg", "srcJarDir", "kaptDir", "kotlinJvmTarget",
155 "kotlinBuildFile", "name", "classesJarOut")
Colin Crossafbb1732019-01-17 15:42:52 -0800156
157// kotlinKapt performs Kotlin-compatible annotation processing. It takes .kt and .java sources and srcjars, and runs
158// annotation processors over all of them, producing a srcjar of generated code in outputFile. The srcjar should be
159// added as an additional input to kotlinc and javac rules, and the javac rule should have annotation processing
160// disabled.
Colin Cross9ca38d22020-06-18 15:46:32 -0700161func kotlinKapt(ctx android.ModuleContext, srcJarOutputFile, resJarOutputFile android.WritablePath,
Colin Crossa4c8cc62020-06-25 17:13:36 -0700162 srcFiles, commonSrcFiles, srcJars android.Paths,
Colin Crossafbb1732019-01-17 15:42:52 -0800163 flags javaBuilderFlags) {
164
Colin Crossa4c8cc62020-06-25 17:13:36 -0700165 srcFiles = append(android.Paths(nil), srcFiles...)
166
Colin Crossafbb1732019-01-17 15:42:52 -0800167 var deps android.Paths
168 deps = append(deps, flags.kotlincClasspath...)
169 deps = append(deps, srcJars...)
170 deps = append(deps, flags.processorPath...)
Colin Crossa4c8cc62020-06-25 17:13:36 -0700171 deps = append(deps, commonSrcFiles...)
172
173 commonSrcsList := kotlinCommonSrcsList(ctx, commonSrcFiles)
174 commonSrcFilesArg := ""
175 if commonSrcsList.Valid() {
176 deps = append(deps, commonSrcsList.Path())
177 commonSrcFilesArg = "--common_srcs " + commonSrcsList.String()
178 }
Colin Crossafbb1732019-01-17 15:42:52 -0800179
Colin Crossc2557d12019-10-31 15:22:57 -0700180 kaptProcessorPath := flags.processorPath.FormRepeatedClassPath("-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=")
Colin Crossafbb1732019-01-17 15:42:52 -0800181
Colin Crossbe9cdb82019-01-21 21:37:16 -0800182 kaptProcessor := ""
Colin Cross5a116862020-04-22 11:44:34 -0700183 for i, p := range flags.processors {
184 if i > 0 {
185 kaptProcessor += " "
186 }
187 kaptProcessor += "-P plugin:org.jetbrains.kotlin.kapt3:processors=" + p
Colin Crossbe9cdb82019-01-21 21:37:16 -0800188 }
189
Colin Crossafbb1732019-01-17 15:42:52 -0800190 encodedJavacFlags := kaptEncodeFlags([][2]string{
Colin Cross1e743852019-10-28 11:37:20 -0700191 {"-source", flags.javaVersion.String()},
192 {"-target", flags.javaVersion.String()},
Colin Crossafbb1732019-01-17 15:42:52 -0800193 })
194
Colin Cross6c6e6cd2019-05-08 14:30:12 -0700195 kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName())
196 kotlinName = strings.ReplaceAll(kotlinName, "/", "__")
197
Colin Crossafbb1732019-01-17 15:42:52 -0800198 ctx.Build(pctx, android.BuildParams{
Colin Cross9ca38d22020-06-18 15:46:32 -0700199 Rule: kapt,
200 Description: "kapt",
201 Output: srcJarOutputFile,
202 ImplicitOutput: resJarOutputFile,
203 Inputs: srcFiles,
204 Implicits: deps,
Colin Crossafbb1732019-01-17 15:42:52 -0800205 Args: map[string]string{
Colin Cross9b1aa0c2020-06-25 17:12:28 -0700206 "classpath": flags.kotlincClasspath.FormJavaClassPath(""),
Colin Crossafbb1732019-01-17 15:42:52 -0800207 "kotlincFlags": flags.kotlincFlags,
Colin Crossa4c8cc62020-06-25 17:13:36 -0700208 "commonSrcFilesArg": commonSrcFilesArg,
Colin Crossafbb1732019-01-17 15:42:52 -0800209 "srcJars": strings.Join(srcJars.Strings(), " "),
210 "srcJarDir": android.PathForModuleOut(ctx, "kapt", "srcJars").String(),
211 "kotlinBuildFile": android.PathForModuleOut(ctx, "kapt", "build.xml").String(),
212 "kaptProcessorPath": strings.Join(kaptProcessorPath, " "),
Colin Crossbe9cdb82019-01-21 21:37:16 -0800213 "kaptProcessor": kaptProcessor,
Colin Crossafbb1732019-01-17 15:42:52 -0800214 "kaptDir": android.PathForModuleOut(ctx, "kapt/gen").String(),
215 "encodedJavacFlags": encodedJavacFlags,
Colin Cross6c6e6cd2019-05-08 14:30:12 -0700216 "name": kotlinName,
Colin Cross9ca38d22020-06-18 15:46:32 -0700217 "classesJarOut": resJarOutputFile.String(),
Colin Crossafbb1732019-01-17 15:42:52 -0800218 },
219 })
220}
221
222// kapt converts a list of key, value pairs into a base64 encoded Java serialization, which is what kapt expects.
223func kaptEncodeFlags(options [][2]string) string {
224 buf := &bytes.Buffer{}
225
226 binary.Write(buf, binary.BigEndian, uint32(len(options)))
227 for _, option := range options {
228 binary.Write(buf, binary.BigEndian, uint16(len(option[0])))
229 buf.WriteString(option[0])
230 binary.Write(buf, binary.BigEndian, uint16(len(option[1])))
231 buf.WriteString(option[1])
232 }
233
234 header := &bytes.Buffer{}
235 header.Write([]byte{0xac, 0xed, 0x00, 0x05}) // java serialization header
236
237 if buf.Len() < 256 {
238 header.WriteByte(0x77) // blockdata
239 header.WriteByte(byte(buf.Len()))
240 } else {
241 header.WriteByte(0x7a) // blockdatalong
242 binary.Write(header, binary.BigEndian, uint32(buf.Len()))
243 }
244
245 return base64.StdEncoding.EncodeToString(append(header.Bytes(), buf.Bytes()...))
246}