blob: 9b160a082c4e8c0b9eb94b28bb7ff023cd99c5f5 [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 Cross6c6e6cd2019-05-08 14:30:12 -070034 `${config.GenKotlinBuildFileCmd} $classpath "$name" $classesDir $out.rsp $srcJarDir/list > $kotlinBuildFile &&` +
Colin Crossafbb1732019-01-17 15:42:52 -080035 `${config.KotlincCmd} ${config.JavacHeapFlags} $kotlincFlags ` +
Colin Crossdf8eebe2019-04-09 15:29:41 -070036 `-jvm-target $kotlinJvmTarget -Xbuild-file=$kotlinBuildFile -kotlin-home $emptyDir && ` +
Colin Cross44c29a82019-01-24 16:36:57 -080037 `${config.SoongZipCmd} -jar -o $out -C $classesDir -D $classesDir && ` +
38 `rm -rf "$srcJarDir"`,
Colin Cross21fc9bb2019-01-18 15:05:09 -080039 CommandDeps: []string{
40 "${config.KotlincCmd}",
41 "${config.KotlinCompilerJar}",
Dan Willemsenaad19602019-04-07 09:44:35 -070042 "${config.KotlinPreloaderJar}",
43 "${config.KotlinReflectJar}",
44 "${config.KotlinScriptRuntimeJar}",
45 "${config.KotlinStdlibJar}",
46 "${config.KotlinTrove4jJar}",
Dan Willemsen1cfbdbe2019-06-13 04:59:17 +000047 "${config.KotlinAnnotationJar}",
Colin Cross21fc9bb2019-01-18 15:05:09 -080048 "${config.GenKotlinBuildFileCmd}",
49 "${config.SoongZipCmd}",
50 "${config.ZipSyncCmd}",
51 },
52 Rspfile: "$out.rsp",
53 RspfileContent: `$in`,
54 },
Colin Cross6c6e6cd2019-05-08 14:30:12 -070055 "kotlincFlags", "classpath", "srcJars", "srcJarDir", "classesDir", "kotlinJvmTarget", "kotlinBuildFile",
56 "emptyDir", "name")
Colin Cross21fc9bb2019-01-18 15:05:09 -080057
Colin Crossafbb1732019-01-17 15:42:52 -080058// 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 -080059func kotlinCompile(ctx android.ModuleContext, outputFile android.WritablePath,
60 srcFiles, srcJars android.Paths,
61 flags javaBuilderFlags) {
62
Colin Cross21fc9bb2019-01-18 15:05:09 -080063 var deps android.Paths
64 deps = append(deps, flags.kotlincClasspath...)
65 deps = append(deps, srcJars...)
66
Colin Cross6c6e6cd2019-05-08 14:30:12 -070067 kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName())
68 kotlinName = strings.ReplaceAll(kotlinName, "/", "__")
69
Colin Cross21fc9bb2019-01-18 15:05:09 -080070 ctx.Build(pctx, android.BuildParams{
71 Rule: kotlinc,
72 Description: "kotlinc",
73 Output: outputFile,
Colin Crossafbb1732019-01-17 15:42:52 -080074 Inputs: srcFiles,
Colin Cross21fc9bb2019-01-18 15:05:09 -080075 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 Crossdf8eebe2019-04-09 15:29:41 -070083 "emptyDir": android.PathForModuleOut(ctx, "kotlinc", "empty").String(),
Colin Cross21fc9bb2019-01-18 15:05:09 -080084 // http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8
85 "kotlinJvmTarget": "1.8",
Colin Cross6c6e6cd2019-05-08 14:30:12 -070086 "name": kotlinName,
Colin Cross21fc9bb2019-01-18 15:05:09 -080087 },
88 })
89}
Colin Crossafbb1732019-01-17 15:42:52 -080090
Ramy Medhat8ea054a2020-01-27 14:19:44 -050091var kapt = pctx.AndroidRemoteStaticRule("kapt", android.RemoteRuleSupports{Goma: true},
Colin Crossafbb1732019-01-17 15:42:52 -080092 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 Cross6c6e6cd2019-05-08 14:30:12 -070095 `${config.GenKotlinBuildFileCmd} $classpath "$name" "" $out.rsp $srcJarDir/list > $kotlinBuildFile &&` +
Colin Crossafbb1732019-01-17 15:42:52 -080096 `${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 Crossbe9cdb82019-01-21 21:37:16 -0800105 `$kaptProcessor ` +
Colin Crossafbb1732019-01-17 15:42:52 -0800106 `-Xbuild-file=$kotlinBuildFile && ` +
Colin Cross44c29a82019-01-24 16:36:57 -0800107 `${config.SoongZipCmd} -jar -o $out -C $kaptDir/sources -D $kaptDir/sources && ` +
108 `rm -rf "$srcJarDir"`,
Colin Crossafbb1732019-01-17 15:42:52 -0800109 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 Crossbe9cdb82019-01-21 21:37:16 -0800120 "kotlincFlags", "encodedJavacFlags", "kaptProcessorPath", "kaptProcessor",
Colin Cross6c6e6cd2019-05-08 14:30:12 -0700121 "classpath", "srcJars", "srcJarDir", "kaptDir", "kotlinJvmTarget", "kotlinBuildFile", "name")
Colin Crossafbb1732019-01-17 15:42:52 -0800122
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.
127func 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 Crossc2557d12019-10-31 15:22:57 -0700136 kaptProcessorPath := flags.processorPath.FormRepeatedClassPath("-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=")
Colin Crossafbb1732019-01-17 15:42:52 -0800137
Colin Crossbe9cdb82019-01-21 21:37:16 -0800138 kaptProcessor := ""
Colin Cross5a116862020-04-22 11:44:34 -0700139 for i, p := range flags.processors {
140 if i > 0 {
141 kaptProcessor += " "
142 }
143 kaptProcessor += "-P plugin:org.jetbrains.kotlin.kapt3:processors=" + p
Colin Crossbe9cdb82019-01-21 21:37:16 -0800144 }
145
Colin Crossafbb1732019-01-17 15:42:52 -0800146 encodedJavacFlags := kaptEncodeFlags([][2]string{
Colin Cross1e743852019-10-28 11:37:20 -0700147 {"-source", flags.javaVersion.String()},
148 {"-target", flags.javaVersion.String()},
Colin Crossafbb1732019-01-17 15:42:52 -0800149 })
150
Colin Cross6c6e6cd2019-05-08 14:30:12 -0700151 kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName())
152 kotlinName = strings.ReplaceAll(kotlinName, "/", "__")
153
Colin Crossafbb1732019-01-17 15:42:52 -0800154 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 Crossbe9cdb82019-01-21 21:37:16 -0800167 "kaptProcessor": kaptProcessor,
Colin Crossafbb1732019-01-17 15:42:52 -0800168 "kaptDir": android.PathForModuleOut(ctx, "kapt/gen").String(),
169 "encodedJavacFlags": encodedJavacFlags,
Colin Cross6c6e6cd2019-05-08 14:30:12 -0700170 "name": kotlinName,
Colin Crossafbb1732019-01-17 15:42:52 -0800171 },
172 })
173}
174
175// kapt converts a list of key, value pairs into a base64 encoded Java serialization, which is what kapt expects.
176func 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}