blob: 6f17e346fe4f15ab3c51146a7e99d2ab769fb81b [file] [log] [blame]
Wei Libafb6d62021-12-10 03:14:59 -08001// Copyright 2021 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 bp2build
16
17import (
18 "testing"
19
20 "android/soong/android"
21 "android/soong/cc"
22 "android/soong/java"
23)
24
Sam Delmerico3177a6e2022-06-21 19:28:33 +000025func runJavaBinaryHostTestCase(t *testing.T, tc Bp2buildTestCase) {
Wei Libafb6d62021-12-10 03:14:59 -080026 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000027 (&tc).ModuleTypeUnderTest = "java_binary_host"
28 (&tc).ModuleTypeUnderTestFactory = java.BinaryHostFactory
29 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
Wei Libafb6d62021-12-10 03:14:59 -080030 ctx.RegisterModuleType("cc_library_host_shared", cc.LibraryHostSharedFactory)
Sam Delmericoc0161432022-02-25 21:34:51 +000031 ctx.RegisterModuleType("java_library", java.LibraryFactory)
Alixb4e09a02022-09-27 15:36:01 +000032 ctx.RegisterModuleType("java_import_host", java.ImportFactory)
Wei Libafb6d62021-12-10 03:14:59 -080033 }, tc)
34}
35
Lukacs T. Berkibc5f7312022-10-31 09:01:34 +000036var testFs = map[string]string{
Wei Libafb6d62021-12-10 03:14:59 -080037 "test.mf": "Main-Class: com.android.test.MainClass",
38 "other/Android.bp": `cc_library_host_shared {
39 name: "jni-lib-1",
40 stl: "none",
41}`,
42}
43
44func TestJavaBinaryHost(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000045 runJavaBinaryHostTestCase(t, Bp2buildTestCase{
46 Description: "java_binary_host with srcs, exclude_srcs, jni_libs, javacflags, and manifest.",
Lukacs T. Berkibc5f7312022-10-31 09:01:34 +000047 Filesystem: testFs,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000048 Blueprint: `java_binary_host {
Wei Libafb6d62021-12-10 03:14:59 -080049 name: "java-binary-host-1",
50 srcs: ["a.java", "b.java"],
51 exclude_srcs: ["b.java"],
52 manifest: "test.mf",
53 jni_libs: ["jni-lib-1"],
Sam Delmerico4e272292022-01-06 20:03:51 +000054 javacflags: ["-Xdoclint:all/protected"],
Wei Libafb6d62021-12-10 03:14:59 -080055 bazel_module: { bp2build_available: true },
Vinh Tran3ac6daf2022-04-22 19:09:58 -040056 java_version: "8",
Wei Libafb6d62021-12-10 03:14:59 -080057}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000058 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000059 MakeBazelTarget("java_binary", "java-binary-host-1", AttrNameToString{
Wei Libafb6d62021-12-10 03:14:59 -080060 "srcs": `["a.java"]`,
61 "main_class": `"com.android.test.MainClass"`,
62 "deps": `["//other:jni-lib-1"]`,
63 "jvm_flags": `["-Djava.library.path=$${RUNPATH}other"]`,
Vinh Tran3ac6daf2022-04-22 19:09:58 -040064 "javacopts": `[
65 "-Xdoclint:all/protected",
66 "-source 1.8 -target 1.8",
67 ]`,
Sam Delmerico75539d62022-01-31 14:37:29 +000068 "target_compatible_with": `select({
69 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
70 "//conditions:default": [],
71 })`,
Wei Libafb6d62021-12-10 03:14:59 -080072 }),
73 },
74 })
75}
Sam Delmericoc0161432022-02-25 21:34:51 +000076
77func TestJavaBinaryHostRuntimeDeps(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000078 runJavaBinaryHostTestCase(t, Bp2buildTestCase{
79 Description: "java_binary_host with srcs, exclude_srcs, jni_libs, javacflags, and manifest.",
Lukacs T. Berkibc5f7312022-10-31 09:01:34 +000080 Filesystem: testFs,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000081 Blueprint: `java_binary_host {
Sam Delmericoc0161432022-02-25 21:34:51 +000082 name: "java-binary-host-1",
83 static_libs: ["java-dep-1"],
84 manifest: "test.mf",
85 bazel_module: { bp2build_available: true },
86}
87
88java_library {
89 name: "java-dep-1",
90 srcs: ["a.java"],
91 bazel_module: { bp2build_available: false },
92}
93`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000094 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000095 MakeBazelTarget("java_binary", "java-binary-host-1", AttrNameToString{
Sam Delmericoc0161432022-02-25 21:34:51 +000096 "main_class": `"com.android.test.MainClass"`,
97 "runtime_deps": `[":java-dep-1"]`,
98 "target_compatible_with": `select({
99 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
100 "//conditions:default": [],
101 })`,
102 }),
103 },
104 })
105}
Alixb4e09a02022-09-27 15:36:01 +0000106
107func TestJavaBinaryHostLibs(t *testing.T) {
108 runJavaBinaryHostTestCase(t, Bp2buildTestCase{
109 Description: "java_binary_host with srcs, libs.",
Lukacs T. Berkibc5f7312022-10-31 09:01:34 +0000110 Filesystem: testFs,
Alixb4e09a02022-09-27 15:36:01 +0000111 Blueprint: `java_binary_host {
112 name: "java-binary-host-libs",
113 libs: ["java-lib-dep-1"],
114 manifest: "test.mf",
115 srcs: ["a.java"],
116}
117
118java_import_host{
119 name: "java-lib-dep-1",
120 jars: ["foo.jar"],
121 bazel_module: { bp2build_available: false },
122}
123`,
124 ExpectedBazelTargets: []string{
125 MakeBazelTarget("java_binary", "java-binary-host-libs", AttrNameToString{
126 "main_class": `"com.android.test.MainClass"`,
127 "srcs": `["a.java"]`,
128 "deps": `[":java-lib-dep-1-neverlink"]`,
129 "target_compatible_with": `select({
130 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
131 "//conditions:default": [],
132 })`,
133 }),
134 },
135 })
136}
Alix7c8eaeb2022-11-16 15:44:55 +0000137
138func TestJavaBinaryHostKotlinSrcs(t *testing.T) {
139 runJavaBinaryHostTestCase(t, Bp2buildTestCase{
140 Description: "java_binary_host with srcs, libs.",
141 Filesystem: testFs,
142 Blueprint: `java_binary_host {
143 name: "java-binary-host",
144 manifest: "test.mf",
145 srcs: ["a.java", "b.kt"],
146}
147`,
148 ExpectedBazelTargets: []string{
149 MakeBazelTarget("kt_jvm_library", "java-binary-host_kt", AttrNameToString{
150 "srcs": `[
151 "a.java",
152 "b.kt",
153 ]`,
154 "target_compatible_with": `select({
155 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
156 "//conditions:default": [],
157 })`,
158 }),
159 MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{
160 "main_class": `"com.android.test.MainClass"`,
161 "runtime_deps": `[":java-binary-host_kt"]`,
162 "target_compatible_with": `select({
163 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
164 "//conditions:default": [],
165 })`,
166 }),
167 },
168 })
169}
170
171func TestJavaBinaryHostKotlinCommonSrcs(t *testing.T) {
172 runJavaBinaryHostTestCase(t, Bp2buildTestCase{
173 Description: "java_binary_host with common_srcs",
174 Filesystem: testFs,
175 Blueprint: `java_binary_host {
176 name: "java-binary-host",
177 manifest: "test.mf",
178 srcs: ["a.java"],
179 common_srcs: ["b.kt"],
180}
181`,
182 ExpectedBazelTargets: []string{
183 MakeBazelTarget("kt_jvm_library", "java-binary-host_kt", AttrNameToString{
184 "srcs": `["a.java"]`,
185 "common_srcs": `["b.kt"]`,
186 "target_compatible_with": `select({
187 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
188 "//conditions:default": [],
189 })`,
190 }),
191 MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{
192 "main_class": `"com.android.test.MainClass"`,
193 "runtime_deps": `[":java-binary-host_kt"]`,
194 "target_compatible_with": `select({
195 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
196 "//conditions:default": [],
197 })`,
198 }),
199 },
200 })
201}
202
203func TestJavaBinaryHostKotlinWithResourceDir(t *testing.T) {
204 runJavaBinaryHostTestCase(t, Bp2buildTestCase{
205 Description: "java_binary_host with srcs, libs, resource dir .",
206 Filesystem: map[string]string{
207 "test.mf": "Main-Class: com.android.test.MainClass",
208 "res/a.res": "",
209 "res/dir1/b.res": "",
210 },
211 Blueprint: `java_binary_host {
212 name: "java-binary-host",
213 manifest: "test.mf",
214 srcs: ["a.java", "b.kt"],
215 java_resource_dirs: ["res"],
216}
217`,
218 ExpectedBazelTargets: []string{
219 MakeBazelTarget("kt_jvm_library", "java-binary-host_kt", AttrNameToString{
220 "srcs": `[
221 "a.java",
222 "b.kt",
223 ]`,
Alix5afd9fa2023-03-03 19:22:15 +0000224 "resources": `[
225 "res/a.res",
226 "res/dir1/b.res",
227 ]`,
228 "resource_strip_prefix": `"res"`,
Alix7c8eaeb2022-11-16 15:44:55 +0000229 "target_compatible_with": `select({
230 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
231 "//conditions:default": [],
232 })`,
233 }),
234 MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{
235 "main_class": `"com.android.test.MainClass"`,
236 "runtime_deps": `[":java-binary-host_kt"]`,
Alix7c8eaeb2022-11-16 15:44:55 +0000237 "target_compatible_with": `select({
238 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
239 "//conditions:default": [],
240 })`,
241 }),
242 },
243 })
244}
245
246func TestJavaBinaryHostKotlinWithResources(t *testing.T) {
247 runJavaBinaryHostTestCase(t, Bp2buildTestCase{
248 Description: "java_binary_host with srcs, libs, resources.",
249 Filesystem: map[string]string{
250 "test.mf": "Main-Class: com.android.test.MainClass",
251 "res/a.res": "",
252 "res/b.res": "",
253 },
254 Blueprint: `java_binary_host {
255 name: "java-binary-host",
256 manifest: "test.mf",
257 srcs: ["a.java", "b.kt"],
258 java_resources: ["res/a.res", "res/b.res"],
259}
260`,
261 ExpectedBazelTargets: []string{
262 MakeBazelTarget("kt_jvm_library", "java-binary-host_kt", AttrNameToString{
263 "srcs": `[
264 "a.java",
265 "b.kt",
266 ]`,
267 "resources": `[
268 "res/a.res",
269 "res/b.res",
270 ]`,
271 "target_compatible_with": `select({
272 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
273 "//conditions:default": [],
274 })`,
275 }),
276 MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{
277 "main_class": `"com.android.test.MainClass"`,
278 "runtime_deps": `[":java-binary-host_kt"]`,
279 "target_compatible_with": `select({
280 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
281 "//conditions:default": [],
282 })`,
283 }),
284 },
285 })
286}