• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15package java
16
17import (
18	"bytes"
19	"encoding/base64"
20	"encoding/binary"
21	"path/filepath"
22	"strings"
23
24	"android/soong/android"
25
26	"github.com/google/blueprint"
27)
28
29var kotlinc = pctx.AndroidRemoteStaticRule("kotlinc", android.RemoteRuleSupports{Goma: true},
30	blueprint.RuleParams{
31		Command: `rm -rf "$classesDir" "$headerClassesDir" "$srcJarDir" "$kotlinBuildFile" "$emptyDir" && ` +
32			`mkdir -p "$classesDir" "$headerClassesDir" "$srcJarDir" "$emptyDir" && ` +
33			`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" -f "*.kt" $srcJars && ` +
34			`${config.GenKotlinBuildFileCmd} --classpath "$classpath" --name "$name"` +
35			` --out_dir "$classesDir" --srcs "$out.rsp" --srcs "$srcJarDir/list"` +
36			` $commonSrcFilesArg --out "$kotlinBuildFile" && ` +
37			`${config.KotlincCmd} ${config.KotlincGlobalFlags} ` +
38			` ${config.KotlincSuppressJDK9Warnings} ${config.JavacHeapFlags} ` +
39			` $kotlincFlags -jvm-target $kotlinJvmTarget -Xbuild-file=$kotlinBuildFile ` +
40			` -kotlin-home $emptyDir ` +
41			` -Xplugin=${config.KotlinAbiGenPluginJar} ` +
42			` -P plugin:org.jetbrains.kotlin.jvm.abi:outputDir=$headerClassesDir && ` +
43			`${config.SoongZipCmd} -jar -o $out -C $classesDir -D $classesDir -write_if_changed && ` +
44			`${config.SoongZipCmd} -jar -o $headerJar -C $headerClassesDir -D $headerClassesDir -write_if_changed && ` +
45			`rm -rf "$srcJarDir" "$classesDir" "$headerClassesDir"`,
46		CommandDeps: []string{
47			"${config.KotlincCmd}",
48			"${config.KotlinCompilerJar}",
49			"${config.KotlinPreloaderJar}",
50			"${config.KotlinReflectJar}",
51			"${config.KotlinScriptRuntimeJar}",
52			"${config.KotlinStdlibJar}",
53			"${config.KotlinTrove4jJar}",
54			"${config.KotlinAnnotationJar}",
55			"${config.KotlinAbiGenPluginJar}",
56			"${config.GenKotlinBuildFileCmd}",
57			"${config.SoongZipCmd}",
58			"${config.ZipSyncCmd}",
59		},
60		Rspfile:        "$out.rsp",
61		RspfileContent: `$in`,
62		Restat:         true,
63	},
64	"kotlincFlags", "classpath", "srcJars", "commonSrcFilesArg", "srcJarDir", "classesDir",
65	"headerClassesDir", "headerJar", "kotlinJvmTarget", "kotlinBuildFile", "emptyDir", "name")
66
67var kotlinKytheExtract = pctx.AndroidStaticRule("kotlinKythe",
68	blueprint.RuleParams{
69		Command: `rm -rf "$srcJarDir" && mkdir -p "$srcJarDir" && ` +
70			`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" -f "*.kt" $srcJars && ` +
71			`${config.KotlinKytheExtractor} -corpus ${kytheCorpus} --srcs @$out.rsp --srcs @"$srcJarDir/list" $commonSrcFilesList --cp @$classpath -o $out --kotlin_out $outJar ` +
72			// wrap the additional kotlin args.
73			// Skip Xbuild file, pass the cp explicitly.
74			// Skip header jars, those should not have an effect on kythe results.
75			` --args '${config.KotlincGlobalFlags} ` +
76			` ${config.KotlincSuppressJDK9Warnings} ${config.JavacHeapFlags} ` +
77			` $kotlincFlags -jvm-target $kotlinJvmTarget ` +
78			`${config.KotlincKytheGlobalFlags}'`,
79		CommandDeps: []string{
80			"${config.KotlinKytheExtractor}",
81			"${config.ZipSyncCmd}",
82		},
83		Rspfile:        "$out.rsp",
84		RspfileContent: "$in",
85	},
86	"classpath", "kotlincFlags", "commonSrcFilesList", "kotlinJvmTarget", "outJar", "srcJars", "srcJarDir",
87)
88
89func kotlinCommonSrcsList(ctx android.ModuleContext, commonSrcFiles android.Paths) android.OptionalPath {
90	if len(commonSrcFiles) > 0 {
91		// The list of common_srcs may be too long to put on the command line, but
92		// we can't use the rsp file because it is already being used for srcs.
93		// Insert a second rule to write out the list of resources to a file.
94		commonSrcsList := android.PathForModuleOut(ctx, "kotlinc_common_srcs.list")
95		rule := android.NewRuleBuilder(pctx, ctx)
96		rule.Command().Text("cp").
97			FlagWithRspFileInputList("", commonSrcsList.ReplaceExtension(ctx, "rsp"), commonSrcFiles).
98			Output(commonSrcsList)
99		rule.Build("kotlin_common_srcs_list", "kotlin common_srcs list")
100		return android.OptionalPathForPath(commonSrcsList)
101	}
102	return android.OptionalPath{}
103}
104
105// kotlinCompile takes .java and .kt sources and srcJars, and compiles the .kt sources into a classes jar in outputFile.
106func (j *Module) kotlinCompile(ctx android.ModuleContext, outputFile, headerOutputFile android.WritablePath,
107	srcFiles, commonSrcFiles, srcJars android.Paths,
108	flags javaBuilderFlags) {
109
110	var deps android.Paths
111	deps = append(deps, flags.kotlincClasspath...)
112	deps = append(deps, flags.kotlincDeps...)
113	deps = append(deps, srcJars...)
114	deps = append(deps, commonSrcFiles...)
115
116	kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName())
117	kotlinName = strings.ReplaceAll(kotlinName, "/", "__")
118
119	commonSrcsList := kotlinCommonSrcsList(ctx, commonSrcFiles)
120	commonSrcFilesArg := ""
121	if commonSrcsList.Valid() {
122		deps = append(deps, commonSrcsList.Path())
123		commonSrcFilesArg = "--common_srcs " + commonSrcsList.String()
124	}
125
126	classpathRspFile := android.PathForModuleOut(ctx, "kotlinc", "classpath.rsp")
127	android.WriteFileRule(ctx, classpathRspFile, strings.Join(flags.kotlincClasspath.Strings(), " "))
128	deps = append(deps, classpathRspFile)
129
130	ctx.Build(pctx, android.BuildParams{
131		Rule:           kotlinc,
132		Description:    "kotlinc",
133		Output:         outputFile,
134		ImplicitOutput: headerOutputFile,
135		Inputs:         srcFiles,
136		Implicits:      deps,
137		Args: map[string]string{
138			"classpath":         classpathRspFile.String(),
139			"kotlincFlags":      flags.kotlincFlags,
140			"commonSrcFilesArg": commonSrcFilesArg,
141			"srcJars":           strings.Join(srcJars.Strings(), " "),
142			"classesDir":        android.PathForModuleOut(ctx, "kotlinc", "classes").String(),
143			"headerClassesDir":  android.PathForModuleOut(ctx, "kotlinc", "header_classes").String(),
144			"headerJar":         headerOutputFile.String(),
145			"srcJarDir":         android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(),
146			"kotlinBuildFile":   android.PathForModuleOut(ctx, "kotlinc-build.xml").String(),
147			"emptyDir":          android.PathForModuleOut(ctx, "kotlinc", "empty").String(),
148			"kotlinJvmTarget":   flags.javaVersion.StringForKotlinc(),
149			"name":              kotlinName,
150		},
151	})
152
153	// Emit kythe xref rule
154	if (ctx.Config().EmitXrefRules()) && ctx.Module() == ctx.PrimaryModule() {
155		extractionFile := outputFile.ReplaceExtension(ctx, "kzip")
156		args := map[string]string{
157			"classpath":       classpathRspFile.String(),
158			"kotlincFlags":    flags.kotlincFlags,
159			"kotlinJvmTarget": flags.javaVersion.StringForKotlinc(),
160			"outJar":          outputFile.String(),
161			"srcJars":         strings.Join(srcJars.Strings(), " "),
162			"srcJarDir":       android.PathForModuleOut(ctx, "kotlinc", "srcJars.xref").String(),
163		}
164		if commonSrcsList.Valid() {
165			args["commonSrcFilesList"] = "--common_srcs @" + commonSrcsList.String()
166		}
167		ctx.Build(pctx, android.BuildParams{
168			Rule:        kotlinKytheExtract,
169			Description: "kotlinKythe",
170			Output:      extractionFile,
171			Inputs:      srcFiles,
172			Implicits:   deps,
173			Args:        args,
174		})
175		j.kytheKotlinFiles = append(j.kytheKotlinFiles, extractionFile)
176	}
177}
178
179var kaptStubs = pctx.AndroidRemoteStaticRule("kaptStubs", android.RemoteRuleSupports{Goma: true},
180	blueprint.RuleParams{
181		Command: `rm -rf "$srcJarDir" "$kotlinBuildFile" "$kaptDir" && ` +
182			`mkdir -p "$srcJarDir" "$kaptDir/sources" "$kaptDir/classes" && ` +
183			`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
184			`${config.FindInputDeltaCmd} --template '' --target "$out" --inputs_file "$out.rsp" && ` +
185			`${config.GenKotlinBuildFileCmd} --classpath "$classpath" --name "$name"` +
186			` --srcs "$out.rsp" --srcs "$srcJarDir/list"` +
187			` $commonSrcFilesArg --out "$kotlinBuildFile" && ` +
188			`${config.KotlincCmd} ${config.KotlincGlobalFlags} ` +
189			`${config.KaptSuppressJDK9Warnings} ${config.KotlincSuppressJDK9Warnings} ` +
190			`${config.JavacHeapFlags} $kotlincFlags -Xplugin=${config.KotlinKaptJar} ` +
191			`-P plugin:org.jetbrains.kotlin.kapt3:sources=$kaptDir/sources ` +
192			`-P plugin:org.jetbrains.kotlin.kapt3:classes=$kaptDir/classes ` +
193			`-P plugin:org.jetbrains.kotlin.kapt3:stubs=$kaptDir/stubs ` +
194			`-P plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true ` +
195			`-P plugin:org.jetbrains.kotlin.kapt3:aptMode=stubs ` +
196			`-P plugin:org.jetbrains.kotlin.kapt3:javacArguments=$encodedJavacFlags ` +
197			`$kaptProcessorPath ` +
198			`$kaptProcessor ` +
199			`-Xbuild-file=$kotlinBuildFile && ` +
200			`${config.SoongZipCmd} -jar -write_if_changed -o $out -C $kaptDir/stubs -D $kaptDir/stubs && ` +
201			`if [ -f "$out.pc_state.new" ]; then mv "$out.pc_state.new" "$out.pc_state"; fi && ` +
202			`rm -rf "$srcJarDir"`,
203		CommandDeps: []string{
204			"${config.FindInputDeltaCmd}",
205			"${config.KotlincCmd}",
206			"${config.KotlinCompilerJar}",
207			"${config.KotlinKaptJar}",
208			"${config.GenKotlinBuildFileCmd}",
209			"${config.SoongZipCmd}",
210			"${config.ZipSyncCmd}",
211		},
212		Rspfile:        "$out.rsp",
213		RspfileContent: `$in`,
214		Restat:         true,
215	},
216	"kotlincFlags", "encodedJavacFlags", "kaptProcessorPath", "kaptProcessor",
217	"classpath", "srcJars", "commonSrcFilesArg", "srcJarDir", "kaptDir", "kotlinJvmTarget",
218	"kotlinBuildFile", "name", "classesJarOut")
219
220// kotlinKapt performs Kotlin-compatible annotation processing.  It takes .kt and .java sources and srcjars, and runs
221// annotation processors over all of them, producing a srcjar of generated code in outputFile.  The srcjar should be
222// added as an additional input to kotlinc and javac rules, and the javac rule should have annotation processing
223// disabled.
224func kotlinKapt(ctx android.ModuleContext, srcJarOutputFile, resJarOutputFile android.WritablePath,
225	srcFiles, commonSrcFiles, srcJars android.Paths,
226	flags javaBuilderFlags) {
227
228	srcFiles = append(android.Paths(nil), srcFiles...)
229
230	var deps android.Paths
231	deps = append(deps, flags.kotlincClasspath...)
232	deps = append(deps, flags.kotlincDeps...)
233	deps = append(deps, srcJars...)
234	deps = append(deps, flags.processorPath...)
235	deps = append(deps, commonSrcFiles...)
236
237	commonSrcsList := kotlinCommonSrcsList(ctx, commonSrcFiles)
238	commonSrcFilesArg := ""
239	if commonSrcsList.Valid() {
240		deps = append(deps, commonSrcsList.Path())
241		commonSrcFilesArg = "--common_srcs " + commonSrcsList.String()
242	}
243
244	kaptProcessorPath := flags.processorPath.FormRepeatedClassPath("-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=")
245
246	kaptProcessor := ""
247	for i, p := range flags.processors {
248		if i > 0 {
249			kaptProcessor += " "
250		}
251		kaptProcessor += "-P plugin:org.jetbrains.kotlin.kapt3:processors=" + p
252	}
253
254	encodedJavacFlags := kaptEncodeFlags([][2]string{
255		{"-source", flags.javaVersion.String()},
256		{"-target", flags.javaVersion.String()},
257	})
258
259	kotlinName := filepath.Join(ctx.ModuleDir(), ctx.ModuleSubDir(), ctx.ModuleName())
260	kotlinName = strings.ReplaceAll(kotlinName, "/", "__")
261
262	classpathRspFile := android.PathForModuleOut(ctx, "kapt", "classpath.rsp")
263	android.WriteFileRule(ctx, classpathRspFile, strings.Join(flags.kotlincClasspath.Strings(), "\n"))
264	deps = append(deps, classpathRspFile)
265
266	// First run kapt to generate .java stubs from .kt files
267	kaptStubsJar := android.PathForModuleOut(ctx, "kapt", "stubs.jar")
268	ctx.Build(pctx, android.BuildParams{
269		Rule:        kaptStubs,
270		Description: "kapt stubs",
271		Output:      kaptStubsJar,
272		Inputs:      srcFiles,
273		Implicits:   deps,
274		Args: map[string]string{
275			"classpath":         classpathRspFile.String(),
276			"kotlincFlags":      flags.kotlincFlags,
277			"commonSrcFilesArg": commonSrcFilesArg,
278			"srcJars":           strings.Join(srcJars.Strings(), " "),
279			"srcJarDir":         android.PathForModuleOut(ctx, "kapt", "srcJars").String(),
280			"kotlinBuildFile":   android.PathForModuleOut(ctx, "kapt", "build.xml").String(),
281			"kaptProcessorPath": strings.Join(kaptProcessorPath, " "),
282			"kaptProcessor":     kaptProcessor,
283			"kaptDir":           android.PathForModuleOut(ctx, "kapt/gen").String(),
284			"encodedJavacFlags": encodedJavacFlags,
285			"name":              kotlinName,
286			"classesJarOut":     resJarOutputFile.String(),
287		},
288	})
289
290	// Then run turbine to perform annotation processing on the stubs and any .java srcFiles.
291	javaSrcFiles := srcFiles.FilterByExt(".java")
292	turbineSrcJars := append(android.Paths{kaptStubsJar}, srcJars...)
293	TurbineApt(ctx, srcJarOutputFile, resJarOutputFile, javaSrcFiles, turbineSrcJars, flags)
294}
295
296// kapt converts a list of key, value pairs into a base64 encoded Java serialization, which is what kapt expects.
297func kaptEncodeFlags(options [][2]string) string {
298	buf := &bytes.Buffer{}
299
300	binary.Write(buf, binary.BigEndian, uint32(len(options)))
301	for _, option := range options {
302		binary.Write(buf, binary.BigEndian, uint16(len(option[0])))
303		buf.WriteString(option[0])
304		binary.Write(buf, binary.BigEndian, uint16(len(option[1])))
305		buf.WriteString(option[1])
306	}
307
308	header := &bytes.Buffer{}
309	header.Write([]byte{0xac, 0xed, 0x00, 0x05}) // java serialization header
310
311	if buf.Len() < 256 {
312		header.WriteByte(0x77) // blockdata
313		header.WriteByte(byte(buf.Len()))
314	} else {
315		header.WriteByte(0x7a) // blockdatalong
316		binary.Write(header, binary.BigEndian, uint32(buf.Len()))
317	}
318
319	return base64.StdEncoding.EncodeToString(append(header.Bytes(), buf.Bytes()...))
320}
321