blob: f67a9653ce4e08944868d10e1b021397035db070 [file] [log] [blame]
Colin Cross21fc9bb2019-01-18 15:05:09 -08001// Copyright 2017 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 "strconv"
Colin Cross21fc9bb2019-01-18 15:05:09 -080019 "strings"
20 "testing"
Colin Cross0c66bc62021-07-20 09:47:41 -070021
22 "android/soong/android"
Colin Cross21fc9bb2019-01-18 15:05:09 -080023)
24
25func TestKotlin(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -070026 ctx, _ := testJava(t, `
Colin Cross21fc9bb2019-01-18 15:05:09 -080027 java_library {
28 name: "foo",
29 srcs: ["a.java", "b.kt"],
30 }
31
32 java_library {
33 name: "bar",
34 srcs: ["b.kt"],
35 libs: ["foo"],
36 static_libs: ["baz"],
37 }
38
39 java_library {
40 name: "baz",
41 srcs: ["c.java"],
42 }
43 `)
44
45 fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
46 fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
47 fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
Colin Cross220a9a12022-03-28 17:08:01 -070048 fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar")
49
50 fooKotlincClasses := fooKotlinc.Output
51 fooKotlincHeaderClasses := fooKotlinc.ImplicitOutput
Colin Cross21fc9bb2019-01-18 15:05:09 -080052
53 if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" ||
54 fooKotlinc.Inputs[1].String() != "b.kt" {
55 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs)
56 }
57
58 if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" {
59 t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs)
60 }
61
Colin Cross220a9a12022-03-28 17:08:01 -070062 if !strings.Contains(fooJavac.Args["classpath"], fooKotlincHeaderClasses.String()) {
Colin Cross21fc9bb2019-01-18 15:05:09 -080063 t.Errorf("foo classpath %v does not contain %q",
Colin Cross220a9a12022-03-28 17:08:01 -070064 fooJavac.Args["classpath"], fooKotlincHeaderClasses.String())
Colin Cross21fc9bb2019-01-18 15:05:09 -080065 }
66
Colin Cross220a9a12022-03-28 17:08:01 -070067 if !inList(fooKotlincClasses.String(), fooJar.Inputs.Strings()) {
Colin Cross21fc9bb2019-01-18 15:05:09 -080068 t.Errorf("foo jar inputs %v does not contain %q",
Colin Cross220a9a12022-03-28 17:08:01 -070069 fooJar.Inputs.Strings(), fooKotlincClasses.String())
Colin Cross21fc9bb2019-01-18 15:05:09 -080070 }
71
Colin Cross220a9a12022-03-28 17:08:01 -070072 if !inList(fooKotlincHeaderClasses.String(), fooHeaderJar.Inputs.Strings()) {
73 t.Errorf("foo header jar inputs %v does not contain %q",
74 fooHeaderJar.Inputs.Strings(), fooKotlincHeaderClasses.String())
75 }
76
Colin Cross21fc9bb2019-01-18 15:05:09 -080077 bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar")
78 barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
79
80 if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" {
81 t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs)
82 }
83
84 if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
85 t.Errorf(`expected %q in bar implicits %v`,
86 fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
87 }
88
89 if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
90 t.Errorf(`expected %q in bar implicits %v`,
91 bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
92 }
93}
Colin Crossafbb1732019-01-17 15:42:52 -080094
95func TestKapt(t *testing.T) {
Colin Cross748b2d82020-11-19 13:52:06 -080096 bp := `
Colin Crossafbb1732019-01-17 15:42:52 -080097 java_library {
98 name: "foo",
99 srcs: ["a.java", "b.kt"],
Colin Cross5a116862020-04-22 11:44:34 -0700100 plugins: ["bar", "baz"],
Colin Cross748b2d82020-11-19 13:52:06 -0800101 errorprone: {
102 extra_check_modules: ["my_check"],
103 },
Colin Crossafbb1732019-01-17 15:42:52 -0800104 }
105
Colin Crossbe9cdb82019-01-21 21:37:16 -0800106 java_plugin {
Colin Crossafbb1732019-01-17 15:42:52 -0800107 name: "bar",
Colin Cross3a3e94c2019-01-23 15:39:50 -0800108 processor_class: "com.bar",
109 srcs: ["b.java"],
Colin Crossafbb1732019-01-17 15:42:52 -0800110 }
Colin Cross5a116862020-04-22 11:44:34 -0700111
112 java_plugin {
113 name: "baz",
114 processor_class: "com.baz",
115 srcs: ["b.java"],
116 }
Colin Crossafbb1732019-01-17 15:42:52 -0800117
Colin Cross748b2d82020-11-19 13:52:06 -0800118 java_plugin {
119 name: "my_check",
120 srcs: ["b.java"],
121 }
122 `
123 t.Run("", func(t *testing.T) {
124 ctx, _ := testJava(t, bp)
Colin Cross3a3e94c2019-01-23 15:39:50 -0800125
Colin Cross0c66bc62021-07-20 09:47:41 -0700126 buildOS := ctx.Config().BuildOS.String()
Colin Crossafbb1732019-01-17 15:42:52 -0800127
Colin Crossf61766e2022-03-16 18:06:48 -0700128 foo := ctx.ModuleForTests("foo", "android_common")
129 kaptStubs := foo.Rule("kapt")
130 turbineApt := foo.Description("turbine apt")
131 kotlinc := foo.Rule("kotlinc")
132 javac := foo.Rule("javac")
Colin Cross3a3e94c2019-01-23 15:39:50 -0800133
Colin Cross748b2d82020-11-19 13:52:06 -0800134 bar := ctx.ModuleForTests("bar", buildOS+"_common").Rule("javac").Output.String()
135 baz := ctx.ModuleForTests("baz", buildOS+"_common").Rule("javac").Output.String()
Colin Crossafbb1732019-01-17 15:42:52 -0800136
Colin Cross748b2d82020-11-19 13:52:06 -0800137 // Test that the kotlin and java sources are passed to kapt and kotlinc
Colin Crossf61766e2022-03-16 18:06:48 -0700138 if len(kaptStubs.Inputs) != 2 || kaptStubs.Inputs[0].String() != "a.java" || kaptStubs.Inputs[1].String() != "b.kt" {
139 t.Errorf(`foo kapt inputs %v != ["a.java", "b.kt"]`, kaptStubs.Inputs)
Colin Cross748b2d82020-11-19 13:52:06 -0800140 }
141 if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" || kotlinc.Inputs[1].String() != "b.kt" {
142 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs)
143 }
Colin Crossafbb1732019-01-17 15:42:52 -0800144
Colin Crossf61766e2022-03-16 18:06:48 -0700145 // Test that only the java sources are passed to turbine-apt and javac
146 if len(turbineApt.Inputs) != 1 || turbineApt.Inputs[0].String() != "a.java" {
147 t.Errorf(`foo turbine apt inputs %v != ["a.java"]`, turbineApt.Inputs)
148 }
Colin Cross748b2d82020-11-19 13:52:06 -0800149 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
150 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
151 }
Colin Crossafbb1732019-01-17 15:42:52 -0800152
Colin Crossf61766e2022-03-16 18:06:48 -0700153 // Test that the kapt stubs jar is a dependency of turbine-apt
154 if !inList(kaptStubs.Output.String(), turbineApt.Implicits.Strings()) {
155 t.Errorf("expected %q in turbine-apt implicits %v", kaptStubs.Output.String(), kotlinc.Implicits.Strings())
Colin Cross748b2d82020-11-19 13:52:06 -0800156 }
Colin Cross3a3e94c2019-01-23 15:39:50 -0800157
Colin Crossc6ef4852022-03-28 18:27:17 -0700158 turbineAptSrcjarOutput := turbineApt.ImplicitOutputs[0]
159
Colin Crossf61766e2022-03-16 18:06:48 -0700160 // Test that the turbine-apt srcjar is a dependency of kotlinc and javac rules
Colin Crossc6ef4852022-03-28 18:27:17 -0700161 if !inList(turbineAptSrcjarOutput.String(), kotlinc.Implicits.Strings()) {
162 t.Errorf("expected %q in kotlinc implicits %v", turbineAptSrcjarOutput.String(), kotlinc.Implicits.Strings())
Colin Cross748b2d82020-11-19 13:52:06 -0800163 }
Colin Crossc6ef4852022-03-28 18:27:17 -0700164 if !inList(turbineAptSrcjarOutput.String(), javac.Implicits.Strings()) {
165 t.Errorf("expected %q in javac implicits %v", turbineAptSrcjarOutput.String(), javac.Implicits.Strings())
Colin Crossf61766e2022-03-16 18:06:48 -0700166 }
167
168 // Test that the turbine-apt srcjar is extracted by the kotlinc and javac rules
Colin Crossc6ef4852022-03-28 18:27:17 -0700169 if kotlinc.Args["srcJars"] != turbineAptSrcjarOutput.String() {
170 t.Errorf("expected %q in kotlinc srcjars %v", turbineAptSrcjarOutput.String(), kotlinc.Args["srcJars"])
Colin Crossf61766e2022-03-16 18:06:48 -0700171 }
Colin Crossc6ef4852022-03-28 18:27:17 -0700172 if javac.Args["srcJars"] != turbineAptSrcjarOutput.String() {
173 t.Errorf("expected %q in javac srcjars %v", turbineAptSrcjarOutput.String(), kotlinc.Args["srcJars"])
Colin Cross748b2d82020-11-19 13:52:06 -0800174 }
Colin Cross3a3e94c2019-01-23 15:39:50 -0800175
Colin Crossc6ef4852022-03-28 18:27:17 -0700176 // Test that the turbine-apt header jar is a dependency of the javac rules
177 turbineAptHeaderjarOutput := turbineApt.Output
178 android.AssertStringListContains(t, "javac dependency", javac.Implicits.Strings(), turbineAptHeaderjarOutput.String())
179 android.AssertStringDoesContain(t, "javac classpath", javac.Args["classpath"], turbineAptHeaderjarOutput.String())
180
181 // Test that the kotlinc header jar is a not a dependency of the javac rules
182 kotlincHeaderJarOutput := kotlinc.ImplicitOutput
183 android.AssertStringListDoesNotContain(t, "javac dependency", javac.Implicits.Strings(), kotlincHeaderJarOutput.String())
184 android.AssertStringDoesNotContain(t, "javac classpath", javac.Args["classpath"], kotlincHeaderJarOutput.String())
185
Colin Cross748b2d82020-11-19 13:52:06 -0800186 // Test that the processors are passed to kapt
187 expectedProcessorPath := "-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + bar +
188 " -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + baz
Colin Crossf61766e2022-03-16 18:06:48 -0700189 if kaptStubs.Args["kaptProcessorPath"] != expectedProcessorPath {
190 t.Errorf("expected kaptProcessorPath %q, got %q", expectedProcessorPath, kaptStubs.Args["kaptProcessorPath"])
Colin Cross748b2d82020-11-19 13:52:06 -0800191 }
192 expectedProcessor := "-P plugin:org.jetbrains.kotlin.kapt3:processors=com.bar -P plugin:org.jetbrains.kotlin.kapt3:processors=com.baz"
Colin Crossf61766e2022-03-16 18:06:48 -0700193 if kaptStubs.Args["kaptProcessor"] != expectedProcessor {
194 t.Errorf("expected kaptProcessor %q, got %q", expectedProcessor, kaptStubs.Args["kaptProcessor"])
195 }
196
197 // Test that the processors are passed to turbine-apt
198 expectedProcessorPath = "--processorpath " + bar + " " + baz
199 if !strings.Contains(turbineApt.Args["turbineFlags"], expectedProcessorPath) {
200 t.Errorf("expected turbine-apt processorpath %q, got %q", expectedProcessorPath, turbineApt.Args["turbineFlags"])
201 }
202 expectedProcessor = "--processors com.bar com.baz"
203 if !strings.Contains(turbineApt.Args["turbineFlags"], expectedProcessor) {
204 t.Errorf("expected turbine-apt processor %q, got %q", expectedProcessor, turbineApt.Args["turbineFlags"])
Colin Cross748b2d82020-11-19 13:52:06 -0800205 }
206
207 // Test that the processors are not passed to javac
208 if javac.Args["processorpath"] != "" {
209 t.Errorf("expected processorPath '', got %q", javac.Args["processorpath"])
210 }
211 if javac.Args["processor"] != "-proc:none" {
212 t.Errorf("expected processor '-proc:none', got %q", javac.Args["processor"])
213 }
214 })
215
216 t.Run("errorprone", func(t *testing.T) {
217 env := map[string]string{
218 "RUN_ERROR_PRONE": "true",
219 }
Paul Duffinb148a492021-03-22 17:31:52 +0000220
221 result := android.GroupFixturePreparers(
222 PrepareForTestWithJavaDefaultModules,
223 android.FixtureMergeEnv(env),
224 ).RunTestWithBp(t, bp)
Colin Cross748b2d82020-11-19 13:52:06 -0800225
Colin Cross0c66bc62021-07-20 09:47:41 -0700226 buildOS := result.Config.BuildOS.String()
Colin Cross748b2d82020-11-19 13:52:06 -0800227
Paul Duffinb148a492021-03-22 17:31:52 +0000228 kapt := result.ModuleForTests("foo", "android_common").Rule("kapt")
Paul Duffinb148a492021-03-22 17:31:52 +0000229 javac := result.ModuleForTests("foo", "android_common").Description("javac")
230 errorprone := result.ModuleForTests("foo", "android_common").Description("errorprone")
Colin Cross748b2d82020-11-19 13:52:06 -0800231
Paul Duffinb148a492021-03-22 17:31:52 +0000232 bar := result.ModuleForTests("bar", buildOS+"_common").Description("javac").Output.String()
233 baz := result.ModuleForTests("baz", buildOS+"_common").Description("javac").Output.String()
234 myCheck := result.ModuleForTests("my_check", buildOS+"_common").Description("javac").Output.String()
Colin Cross748b2d82020-11-19 13:52:06 -0800235
236 // Test that the errorprone plugins are not passed to kapt
237 expectedProcessorPath := "-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + bar +
238 " -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + baz
239 if kapt.Args["kaptProcessorPath"] != expectedProcessorPath {
240 t.Errorf("expected kaptProcessorPath %q, got %q", expectedProcessorPath, kapt.Args["kaptProcessorPath"])
241 }
242 expectedProcessor := "-P plugin:org.jetbrains.kotlin.kapt3:processors=com.bar -P plugin:org.jetbrains.kotlin.kapt3:processors=com.baz"
243 if kapt.Args["kaptProcessor"] != expectedProcessor {
244 t.Errorf("expected kaptProcessor %q, got %q", expectedProcessor, kapt.Args["kaptProcessor"])
245 }
246
247 // Test that the errorprone plugins are not passed to javac
248 if javac.Args["processorpath"] != "" {
249 t.Errorf("expected processorPath '', got %q", javac.Args["processorpath"])
250 }
251 if javac.Args["processor"] != "-proc:none" {
252 t.Errorf("expected processor '-proc:none', got %q", javac.Args["processor"])
253 }
254
255 // Test that the errorprone plugins are passed to errorprone
256 expectedProcessorPath = "-processorpath " + myCheck
257 if errorprone.Args["processorpath"] != expectedProcessorPath {
258 t.Errorf("expected processorpath %q, got %q", expectedProcessorPath, errorprone.Args["processorpath"])
259 }
260 if errorprone.Args["processor"] != "-proc:none" {
261 t.Errorf("expected processor '-proc:none', got %q", errorprone.Args["processor"])
262 }
263 })
Colin Crossafbb1732019-01-17 15:42:52 -0800264}
265
266func TestKaptEncodeFlags(t *testing.T) {
267 // Compares the kaptEncodeFlags against the results of the example implementation at
268 // https://kotlinlang.org/docs/reference/kapt.html#apjavac-options-encoding
269 tests := []struct {
270 in [][2]string
271 out string
272 }{
273 {
274 // empty input
275 in: [][2]string{},
276 out: "rO0ABXcEAAAAAA==",
277 },
278 {
279 // common input
280 in: [][2]string{
281 {"-source", "1.8"},
282 {"-target", "1.8"},
283 },
284 out: "rO0ABXcgAAAAAgAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjg=",
285 },
286 {
287 // input that serializes to a 255 byte block
288 in: [][2]string{
289 {"-source", "1.8"},
290 {"-target", "1.8"},
291 {"a", strings.Repeat("b", 218)},
292 },
293 out: "rO0ABXf/AAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA2mJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJi",
294 },
295 {
296 // input that serializes to a 256 byte block
297 in: [][2]string{
298 {"-source", "1.8"},
299 {"-target", "1.8"},
300 {"a", strings.Repeat("b", 219)},
301 },
302 out: "rO0ABXoAAAEAAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA22JiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYg==",
303 },
304 {
305 // input that serializes to a 257 byte block
306 in: [][2]string{
307 {"-source", "1.8"},
308 {"-target", "1.8"},
309 {"a", strings.Repeat("b", 220)},
310 },
311 out: "rO0ABXoAAAEBAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA3GJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmI=",
312 },
313 }
314
315 for i, test := range tests {
316 t.Run(strconv.Itoa(i), func(t *testing.T) {
317 got := kaptEncodeFlags(test.in)
318 if got != test.out {
319 t.Errorf("\nwant %q\n got %q", test.out, got)
320 }
321 })
322 }
323}
Colin Crossa1ff7c62021-09-17 14:11:52 -0700324
325func TestKotlinCompose(t *testing.T) {
326 result := android.GroupFixturePreparers(
327 PrepareForTestWithJavaDefaultModules,
328 ).RunTestWithBp(t, `
329 java_library {
330 name: "androidx.compose.runtime_runtime",
331 }
332
333 java_library_host {
334 name: "androidx.compose.compiler_compiler-hosted",
335 }
336
337 java_library {
338 name: "withcompose",
339 srcs: ["a.kt"],
340 static_libs: ["androidx.compose.runtime_runtime"],
341 }
342
343 java_library {
344 name: "nocompose",
345 srcs: ["a.kt"],
346 }
347 `)
348
349 buildOS := result.Config.BuildOS.String()
350
351 composeCompiler := result.ModuleForTests("androidx.compose.compiler_compiler-hosted", buildOS+"_common").Rule("combineJar").Output
352 withCompose := result.ModuleForTests("withcompose", "android_common")
353 noCompose := result.ModuleForTests("nocompose", "android_common")
354
355 android.AssertStringListContains(t, "missing compose compiler dependency",
356 withCompose.Rule("kotlinc").Implicits.Strings(), composeCompiler.String())
357
358 android.AssertStringDoesContain(t, "missing compose compiler plugin",
359 withCompose.VariablesForTestsRelativeToTop()["kotlincFlags"], "-Xplugin="+composeCompiler.String())
360
361 android.AssertStringListDoesNotContain(t, "unexpected compose compiler dependency",
362 noCompose.Rule("kotlinc").Implicits.Strings(), composeCompiler.String())
363
364 android.AssertStringDoesNotContain(t, "unexpected compose compiler plugin",
365 noCompose.VariablesForTestsRelativeToTop()["kotlincFlags"], "-Xplugin="+composeCompiler.String())
366}