Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package bp2build |
| 16 | |
| 17 | import ( |
| 18 | "testing" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | "android/soong/cc" |
| 22 | "android/soong/java" |
| 23 | ) |
| 24 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 25 | func runJavaBinaryHostTestCase(t *testing.T, tc Bp2buildTestCase) { |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 26 | t.Helper() |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 27 | (&tc).ModuleTypeUnderTest = "java_binary_host" |
| 28 | (&tc).ModuleTypeUnderTestFactory = java.BinaryHostFactory |
| 29 | RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) { |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 30 | ctx.RegisterModuleType("cc_library_host_shared", cc.LibraryHostSharedFactory) |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 31 | ctx.RegisterModuleType("java_library", java.LibraryFactory) |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 32 | ctx.RegisterModuleType("java_import_host", java.ImportFactory) |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 33 | }, tc) |
| 34 | } |
| 35 | |
Lukacs T. Berki | bc5f731 | 2022-10-31 09:01:34 +0000 | [diff] [blame] | 36 | var testFs = map[string]string{ |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 37 | "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 | |
| 44 | func TestJavaBinaryHost(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 45 | runJavaBinaryHostTestCase(t, Bp2buildTestCase{ |
| 46 | Description: "java_binary_host with srcs, exclude_srcs, jni_libs, javacflags, and manifest.", |
Lukacs T. Berki | bc5f731 | 2022-10-31 09:01:34 +0000 | [diff] [blame] | 47 | Filesystem: testFs, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 48 | Blueprint: `java_binary_host { |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 49 | 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 Delmerico | 4e27229 | 2022-01-06 20:03:51 +0000 | [diff] [blame] | 54 | javacflags: ["-Xdoclint:all/protected"], |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 55 | bazel_module: { bp2build_available: true }, |
Vinh Tran | 3ac6daf | 2022-04-22 19:09:58 -0400 | [diff] [blame] | 56 | java_version: "8", |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 57 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 58 | ExpectedBazelTargets: []string{ |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 59 | MakeBazelTarget("java_library", "java-binary-host-1_lib", AttrNameToString{ |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 60 | "srcs": `["a.java"]`, |
| 61 | "deps": `["//other:jni-lib-1"]`, |
| 62 | "java_version": `"8"`, |
| 63 | "javacopts": `["-Xdoclint:all/protected"]`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 64 | "target_compatible_with": `select({ |
| 65 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 66 | "//conditions:default": [], |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 67 | })`, |
| 68 | }), |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 69 | MakeBazelTarget("java_binary", "java-binary-host-1", AttrNameToString{ |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 70 | "main_class": `"com.android.test.MainClass"`, |
Spandan Das | 5dba0ec | 2023-06-01 02:48:09 +0000 | [diff] [blame^] | 71 | "jvm_flags": `["-Djava.library.path=$${RUNPATH}other/jni-lib-1"]`, |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 72 | "target_compatible_with": `select({ |
| 73 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 74 | "//conditions:default": [], |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 75 | })`, |
| 76 | "runtime_deps": `[":java-binary-host-1_lib"]`, |
| 77 | }), |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 78 | }, |
| 79 | }) |
| 80 | } |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 81 | |
| 82 | func TestJavaBinaryHostRuntimeDeps(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 83 | runJavaBinaryHostTestCase(t, Bp2buildTestCase{ |
| 84 | Description: "java_binary_host with srcs, exclude_srcs, jni_libs, javacflags, and manifest.", |
Lukacs T. Berki | bc5f731 | 2022-10-31 09:01:34 +0000 | [diff] [blame] | 85 | Filesystem: testFs, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 86 | Blueprint: `java_binary_host { |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 87 | name: "java-binary-host-1", |
| 88 | static_libs: ["java-dep-1"], |
| 89 | manifest: "test.mf", |
| 90 | bazel_module: { bp2build_available: true }, |
| 91 | } |
| 92 | |
| 93 | java_library { |
| 94 | name: "java-dep-1", |
| 95 | srcs: ["a.java"], |
| 96 | bazel_module: { bp2build_available: false }, |
| 97 | } |
| 98 | `, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 99 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 100 | MakeBazelTarget("java_binary", "java-binary-host-1", AttrNameToString{ |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 101 | "main_class": `"com.android.test.MainClass"`, |
| 102 | "runtime_deps": `[":java-dep-1"]`, |
| 103 | "target_compatible_with": `select({ |
| 104 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 105 | "//conditions:default": [], |
| 106 | })`, |
| 107 | }), |
| 108 | }, |
| 109 | }) |
| 110 | } |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 111 | |
| 112 | func TestJavaBinaryHostLibs(t *testing.T) { |
| 113 | runJavaBinaryHostTestCase(t, Bp2buildTestCase{ |
| 114 | Description: "java_binary_host with srcs, libs.", |
Lukacs T. Berki | bc5f731 | 2022-10-31 09:01:34 +0000 | [diff] [blame] | 115 | Filesystem: testFs, |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 116 | Blueprint: `java_binary_host { |
| 117 | name: "java-binary-host-libs", |
| 118 | libs: ["java-lib-dep-1"], |
| 119 | manifest: "test.mf", |
| 120 | srcs: ["a.java"], |
| 121 | } |
| 122 | |
| 123 | java_import_host{ |
| 124 | name: "java-lib-dep-1", |
| 125 | jars: ["foo.jar"], |
| 126 | bazel_module: { bp2build_available: false }, |
| 127 | } |
| 128 | `, |
| 129 | ExpectedBazelTargets: []string{ |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 130 | MakeBazelTarget("java_library", "java-binary-host-libs_lib", AttrNameToString{ |
| 131 | "srcs": `["a.java"]`, |
| 132 | "deps": `[":java-lib-dep-1-neverlink"]`, |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 133 | "target_compatible_with": `select({ |
| 134 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 135 | "//conditions:default": [], |
| 136 | })`, |
| 137 | }), |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 138 | MakeBazelTarget("java_binary", "java-binary-host-libs", AttrNameToString{ |
| 139 | "main_class": `"com.android.test.MainClass"`, |
| 140 | "target_compatible_with": `select({ |
| 141 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 142 | "//conditions:default": [], |
| 143 | })`, |
| 144 | "runtime_deps": `[":java-binary-host-libs_lib"]`, |
| 145 | }), |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 146 | }, |
| 147 | }) |
| 148 | } |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 149 | |
| 150 | func TestJavaBinaryHostKotlinSrcs(t *testing.T) { |
| 151 | runJavaBinaryHostTestCase(t, Bp2buildTestCase{ |
| 152 | Description: "java_binary_host with srcs, libs.", |
| 153 | Filesystem: testFs, |
| 154 | Blueprint: `java_binary_host { |
| 155 | name: "java-binary-host", |
| 156 | manifest: "test.mf", |
| 157 | srcs: ["a.java", "b.kt"], |
| 158 | } |
| 159 | `, |
| 160 | ExpectedBazelTargets: []string{ |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 161 | MakeBazelTarget("kt_jvm_library", "java-binary-host_lib", AttrNameToString{ |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 162 | "srcs": `[ |
| 163 | "a.java", |
| 164 | "b.kt", |
| 165 | ]`, |
| 166 | "target_compatible_with": `select({ |
| 167 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 168 | "//conditions:default": [], |
| 169 | })`, |
| 170 | }), |
| 171 | MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{ |
| 172 | "main_class": `"com.android.test.MainClass"`, |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 173 | "runtime_deps": `[":java-binary-host_lib"]`, |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 174 | "target_compatible_with": `select({ |
| 175 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 176 | "//conditions:default": [], |
| 177 | })`, |
| 178 | }), |
| 179 | }, |
| 180 | }) |
| 181 | } |
| 182 | |
| 183 | func TestJavaBinaryHostKotlinCommonSrcs(t *testing.T) { |
| 184 | runJavaBinaryHostTestCase(t, Bp2buildTestCase{ |
| 185 | Description: "java_binary_host with common_srcs", |
| 186 | Filesystem: testFs, |
| 187 | Blueprint: `java_binary_host { |
| 188 | name: "java-binary-host", |
| 189 | manifest: "test.mf", |
| 190 | srcs: ["a.java"], |
| 191 | common_srcs: ["b.kt"], |
| 192 | } |
| 193 | `, |
| 194 | ExpectedBazelTargets: []string{ |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 195 | MakeBazelTarget("kt_jvm_library", "java-binary-host_lib", AttrNameToString{ |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 196 | "srcs": `["a.java"]`, |
| 197 | "common_srcs": `["b.kt"]`, |
| 198 | "target_compatible_with": `select({ |
| 199 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 200 | "//conditions:default": [], |
| 201 | })`, |
| 202 | }), |
| 203 | MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{ |
| 204 | "main_class": `"com.android.test.MainClass"`, |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 205 | "runtime_deps": `[":java-binary-host_lib"]`, |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 206 | "target_compatible_with": `select({ |
| 207 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 208 | "//conditions:default": [], |
| 209 | })`, |
| 210 | }), |
| 211 | }, |
| 212 | }) |
| 213 | } |
| 214 | |
| 215 | func TestJavaBinaryHostKotlinWithResourceDir(t *testing.T) { |
| 216 | runJavaBinaryHostTestCase(t, Bp2buildTestCase{ |
| 217 | Description: "java_binary_host with srcs, libs, resource dir .", |
| 218 | Filesystem: map[string]string{ |
| 219 | "test.mf": "Main-Class: com.android.test.MainClass", |
| 220 | "res/a.res": "", |
| 221 | "res/dir1/b.res": "", |
| 222 | }, |
| 223 | Blueprint: `java_binary_host { |
| 224 | name: "java-binary-host", |
| 225 | manifest: "test.mf", |
| 226 | srcs: ["a.java", "b.kt"], |
| 227 | java_resource_dirs: ["res"], |
| 228 | } |
| 229 | `, |
| 230 | ExpectedBazelTargets: []string{ |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 231 | MakeBazelTarget("kt_jvm_library", "java-binary-host_lib", AttrNameToString{ |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 232 | "srcs": `[ |
| 233 | "a.java", |
| 234 | "b.kt", |
| 235 | ]`, |
Alix | 5afd9fa | 2023-03-03 19:22:15 +0000 | [diff] [blame] | 236 | "resources": `[ |
| 237 | "res/a.res", |
| 238 | "res/dir1/b.res", |
| 239 | ]`, |
| 240 | "resource_strip_prefix": `"res"`, |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 241 | "target_compatible_with": `select({ |
| 242 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 243 | "//conditions:default": [], |
| 244 | })`, |
| 245 | }), |
| 246 | MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{ |
| 247 | "main_class": `"com.android.test.MainClass"`, |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 248 | "runtime_deps": `[":java-binary-host_lib"]`, |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 249 | "target_compatible_with": `select({ |
| 250 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 251 | "//conditions:default": [], |
| 252 | })`, |
| 253 | }), |
| 254 | }, |
| 255 | }) |
| 256 | } |
| 257 | |
| 258 | func TestJavaBinaryHostKotlinWithResources(t *testing.T) { |
| 259 | runJavaBinaryHostTestCase(t, Bp2buildTestCase{ |
| 260 | Description: "java_binary_host with srcs, libs, resources.", |
| 261 | Filesystem: map[string]string{ |
Romain Jobredeaux | d5fe133 | 2023-05-04 14:54:45 -0400 | [diff] [blame] | 262 | "adir/test.mf": "Main-Class: com.android.test.MainClass", |
| 263 | "adir/res/a.res": "", |
| 264 | "adir/res/b.res": "", |
| 265 | "adir/Android.bp": `java_binary_host { |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 266 | name: "java-binary-host", |
| 267 | manifest: "test.mf", |
| 268 | srcs: ["a.java", "b.kt"], |
| 269 | java_resources: ["res/a.res", "res/b.res"], |
Romain Jobredeaux | d5fe133 | 2023-05-04 14:54:45 -0400 | [diff] [blame] | 270 | bazel_module: { bp2build_available: true }, |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 271 | } |
| 272 | `, |
Romain Jobredeaux | d5fe133 | 2023-05-04 14:54:45 -0400 | [diff] [blame] | 273 | }, |
| 274 | Dir: "adir", |
| 275 | Blueprint: "", |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 276 | ExpectedBazelTargets: []string{ |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 277 | MakeBazelTarget("kt_jvm_library", "java-binary-host_lib", AttrNameToString{ |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 278 | "srcs": `[ |
| 279 | "a.java", |
| 280 | "b.kt", |
| 281 | ]`, |
| 282 | "resources": `[ |
| 283 | "res/a.res", |
| 284 | "res/b.res", |
| 285 | ]`, |
Romain Jobredeaux | d5fe133 | 2023-05-04 14:54:45 -0400 | [diff] [blame] | 286 | "resource_strip_prefix": `"adir"`, |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 287 | "target_compatible_with": `select({ |
| 288 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 289 | "//conditions:default": [], |
| 290 | })`, |
| 291 | }), |
| 292 | MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{ |
| 293 | "main_class": `"com.android.test.MainClass"`, |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 294 | "runtime_deps": `[":java-binary-host_lib"]`, |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 295 | "target_compatible_with": `select({ |
| 296 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 297 | "//conditions:default": [], |
| 298 | })`, |
| 299 | }), |
| 300 | }, |
| 301 | }) |
| 302 | } |
Alix | f848bf8 | 2023-03-06 19:43:55 +0000 | [diff] [blame] | 303 | |
| 304 | func TestJavaBinaryHostKotlinCflags(t *testing.T) { |
| 305 | runJavaBinaryHostTestCase(t, Bp2buildTestCase{ |
| 306 | Description: "java_binary_host with kotlincflags", |
| 307 | Filesystem: testFs, |
| 308 | Blueprint: `java_binary_host { |
| 309 | name: "java-binary-host", |
| 310 | manifest: "test.mf", |
| 311 | srcs: ["a.kt"], |
| 312 | kotlincflags: ["-flag1", "-flag2"], |
| 313 | } |
| 314 | `, |
| 315 | ExpectedBazelTargets: []string{ |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 316 | MakeBazelTarget("kt_jvm_library", "java-binary-host_lib", AttrNameToString{ |
Alix | f848bf8 | 2023-03-06 19:43:55 +0000 | [diff] [blame] | 317 | "srcs": `["a.kt"]`, |
| 318 | "kotlincflags": `[ |
| 319 | "-flag1", |
| 320 | "-flag2", |
| 321 | ]`, |
| 322 | "target_compatible_with": `select({ |
| 323 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 324 | "//conditions:default": [], |
| 325 | })`, |
| 326 | }), |
| 327 | MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{ |
| 328 | "main_class": `"com.android.test.MainClass"`, |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 329 | "runtime_deps": `[":java-binary-host_lib"]`, |
Alix | f848bf8 | 2023-03-06 19:43:55 +0000 | [diff] [blame] | 330 | "target_compatible_with": `select({ |
| 331 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 332 | "//conditions:default": [], |
| 333 | })`, |
| 334 | }), |
| 335 | }, |
| 336 | }) |
| 337 | } |