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{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 59 | MakeBazelTarget("java_binary", "java-binary-host-1", AttrNameToString{ |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 60 | "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 Tran | 3ac6daf | 2022-04-22 19:09:58 -0400 | [diff] [blame] | 64 | "javacopts": `[ |
| 65 | "-Xdoclint:all/protected", |
| 66 | "-source 1.8 -target 1.8", |
| 67 | ]`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 68 | "target_compatible_with": `select({ |
| 69 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 70 | "//conditions:default": [], |
| 71 | })`, |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 72 | }), |
| 73 | }, |
| 74 | }) |
| 75 | } |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 76 | |
| 77 | func TestJavaBinaryHostRuntimeDeps(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 78 | runJavaBinaryHostTestCase(t, Bp2buildTestCase{ |
| 79 | 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] | 80 | Filesystem: testFs, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 81 | Blueprint: `java_binary_host { |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 82 | name: "java-binary-host-1", |
| 83 | static_libs: ["java-dep-1"], |
| 84 | manifest: "test.mf", |
| 85 | bazel_module: { bp2build_available: true }, |
| 86 | } |
| 87 | |
| 88 | java_library { |
| 89 | name: "java-dep-1", |
| 90 | srcs: ["a.java"], |
| 91 | bazel_module: { bp2build_available: false }, |
| 92 | } |
| 93 | `, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 94 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 95 | MakeBazelTarget("java_binary", "java-binary-host-1", AttrNameToString{ |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 96 | "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 | } |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 106 | |
| 107 | func TestJavaBinaryHostLibs(t *testing.T) { |
| 108 | runJavaBinaryHostTestCase(t, Bp2buildTestCase{ |
| 109 | Description: "java_binary_host with srcs, libs.", |
Lukacs T. Berki | bc5f731 | 2022-10-31 09:01:34 +0000 | [diff] [blame] | 110 | Filesystem: testFs, |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 111 | 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 | |
| 118 | java_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 | } |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 137 | |
| 138 | func 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 | |
| 171 | func 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 | |
| 203 | func 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 | ]`, |
Alix | 5afd9fa | 2023-03-03 19:22:15 +0000 | [diff] [blame] | 224 | "resources": `[ |
| 225 | "res/a.res", |
| 226 | "res/dir1/b.res", |
| 227 | ]`, |
| 228 | "resource_strip_prefix": `"res"`, |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 229 | "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"]`, |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 237 | "target_compatible_with": `select({ |
| 238 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 239 | "//conditions:default": [], |
| 240 | })`, |
| 241 | }), |
| 242 | }, |
| 243 | }) |
| 244 | } |
| 245 | |
| 246 | func 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 | } |
Alix | f848bf8 | 2023-03-06 19:43:55 +0000 | [diff] [blame^] | 287 | |
| 288 | func TestJavaBinaryHostKotlinCflags(t *testing.T) { |
| 289 | runJavaBinaryHostTestCase(t, Bp2buildTestCase{ |
| 290 | Description: "java_binary_host with kotlincflags", |
| 291 | Filesystem: testFs, |
| 292 | Blueprint: `java_binary_host { |
| 293 | name: "java-binary-host", |
| 294 | manifest: "test.mf", |
| 295 | srcs: ["a.kt"], |
| 296 | kotlincflags: ["-flag1", "-flag2"], |
| 297 | } |
| 298 | `, |
| 299 | ExpectedBazelTargets: []string{ |
| 300 | MakeBazelTarget("kt_jvm_library", "java-binary-host_kt", AttrNameToString{ |
| 301 | "srcs": `["a.kt"]`, |
| 302 | "kotlincflags": `[ |
| 303 | "-flag1", |
| 304 | "-flag2", |
| 305 | ]`, |
| 306 | "target_compatible_with": `select({ |
| 307 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 308 | "//conditions:default": [], |
| 309 | })`, |
| 310 | }), |
| 311 | MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{ |
| 312 | "main_class": `"com.android.test.MainClass"`, |
| 313 | "runtime_deps": `[":java-binary-host_kt"]`, |
| 314 | "target_compatible_with": `select({ |
| 315 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 316 | "//conditions:default": [], |
| 317 | })`, |
| 318 | }), |
| 319 | }, |
| 320 | }) |
| 321 | } |