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 15package bp2build 16 17import ( 18 "testing" 19 20 "android/soong/android" 21 "android/soong/cc" 22 "android/soong/java" 23) 24 25func runJavaBinaryHostTestCase(t *testing.T, tc Bp2buildTestCase) { 26 t.Helper() 27 (&tc).ModuleTypeUnderTest = "java_binary_host" 28 (&tc).ModuleTypeUnderTestFactory = java.BinaryHostFactory 29 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) { 30 ctx.RegisterModuleType("cc_library_host_shared", cc.LibraryHostSharedFactory) 31 ctx.RegisterModuleType("java_library", java.LibraryFactory) 32 ctx.RegisterModuleType("java_import_host", java.ImportFactory) 33 }, tc) 34} 35 36var testFs = map[string]string{ 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 44func TestJavaBinaryHost(t *testing.T) { 45 runJavaBinaryHostTestCase(t, Bp2buildTestCase{ 46 Description: "java_binary_host with srcs, exclude_srcs, jni_libs, javacflags, and manifest.", 47 Filesystem: testFs, 48 Blueprint: `java_binary_host { 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"], 54 javacflags: ["-Xdoclint:all/protected"], 55 bazel_module: { bp2build_available: true }, 56 java_version: "8", 57}`, 58 ExpectedBazelTargets: []string{ 59 MakeBazelTarget("java_library", "java-binary-host-1_lib", AttrNameToString{ 60 "srcs": `["a.java"]`, 61 "deps": `["//other:jni-lib-1"]`, 62 "java_version": `"8"`, 63 "javacopts": `["-Xdoclint:all/protected"]`, 64 "target_compatible_with": `select({ 65 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], 66 "//conditions:default": [], 67 })`, 68 }), 69 MakeBazelTarget("java_binary", "java-binary-host-1", AttrNameToString{ 70 "main_class": `"com.android.test.MainClass"`, 71 "jvm_flags": `["-Djava.library.path=$${RUNPATH}other"]`, 72 "target_compatible_with": `select({ 73 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], 74 "//conditions:default": [], 75 })`, 76 "runtime_deps": `[":java-binary-host-1_lib"]`, 77 }), 78 }, 79 }) 80} 81 82func TestJavaBinaryHostRuntimeDeps(t *testing.T) { 83 runJavaBinaryHostTestCase(t, Bp2buildTestCase{ 84 Description: "java_binary_host with srcs, exclude_srcs, jni_libs, javacflags, and manifest.", 85 Filesystem: testFs, 86 Blueprint: `java_binary_host { 87 name: "java-binary-host-1", 88 static_libs: ["java-dep-1"], 89 manifest: "test.mf", 90 bazel_module: { bp2build_available: true }, 91} 92 93java_library { 94 name: "java-dep-1", 95 srcs: ["a.java"], 96 bazel_module: { bp2build_available: false }, 97} 98`, 99 ExpectedBazelTargets: []string{ 100 MakeBazelTarget("java_binary", "java-binary-host-1", AttrNameToString{ 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} 111 112func TestJavaBinaryHostLibs(t *testing.T) { 113 runJavaBinaryHostTestCase(t, Bp2buildTestCase{ 114 Description: "java_binary_host with srcs, libs.", 115 Filesystem: testFs, 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 123java_import_host{ 124 name: "java-lib-dep-1", 125 jars: ["foo.jar"], 126 bazel_module: { bp2build_available: false }, 127} 128`, 129 ExpectedBazelTargets: []string{ 130 MakeBazelTarget("java_library", "java-binary-host-libs_lib", AttrNameToString{ 131 "srcs": `["a.java"]`, 132 "deps": `[":java-lib-dep-1-neverlink"]`, 133 "target_compatible_with": `select({ 134 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], 135 "//conditions:default": [], 136 })`, 137 }), 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 }), 146 }, 147 }) 148} 149 150func 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{ 161 MakeBazelTarget("kt_jvm_library", "java-binary-host_lib", AttrNameToString{ 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"`, 173 "runtime_deps": `[":java-binary-host_lib"]`, 174 "target_compatible_with": `select({ 175 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], 176 "//conditions:default": [], 177 })`, 178 }), 179 }, 180 }) 181} 182 183func 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{ 195 MakeBazelTarget("kt_jvm_library", "java-binary-host_lib", AttrNameToString{ 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"`, 205 "runtime_deps": `[":java-binary-host_lib"]`, 206 "target_compatible_with": `select({ 207 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], 208 "//conditions:default": [], 209 })`, 210 }), 211 }, 212 }) 213} 214 215func 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{ 231 MakeBazelTarget("kt_jvm_library", "java-binary-host_lib", AttrNameToString{ 232 "srcs": `[ 233 "a.java", 234 "b.kt", 235 ]`, 236 "resources": `[ 237 "res/a.res", 238 "res/dir1/b.res", 239 ]`, 240 "resource_strip_prefix": `"res"`, 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"`, 248 "runtime_deps": `[":java-binary-host_lib"]`, 249 "target_compatible_with": `select({ 250 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], 251 "//conditions:default": [], 252 })`, 253 }), 254 }, 255 }) 256} 257 258func TestJavaBinaryHostKotlinWithResources(t *testing.T) { 259 runJavaBinaryHostTestCase(t, Bp2buildTestCase{ 260 Description: "java_binary_host with srcs, libs, resources.", 261 Filesystem: map[string]string{ 262 "test.mf": "Main-Class: com.android.test.MainClass", 263 "res/a.res": "", 264 "res/b.res": "", 265 }, 266 Blueprint: `java_binary_host { 267 name: "java-binary-host", 268 manifest: "test.mf", 269 srcs: ["a.java", "b.kt"], 270 java_resources: ["res/a.res", "res/b.res"], 271} 272`, 273 ExpectedBazelTargets: []string{ 274 MakeBazelTarget("kt_jvm_library", "java-binary-host_lib", AttrNameToString{ 275 "srcs": `[ 276 "a.java", 277 "b.kt", 278 ]`, 279 "resources": `[ 280 "res/a.res", 281 "res/b.res", 282 ]`, 283 "target_compatible_with": `select({ 284 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], 285 "//conditions:default": [], 286 })`, 287 }), 288 MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{ 289 "main_class": `"com.android.test.MainClass"`, 290 "runtime_deps": `[":java-binary-host_lib"]`, 291 "target_compatible_with": `select({ 292 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], 293 "//conditions:default": [], 294 })`, 295 }), 296 }, 297 }) 298} 299 300func TestJavaBinaryHostKotlinCflags(t *testing.T) { 301 runJavaBinaryHostTestCase(t, Bp2buildTestCase{ 302 Description: "java_binary_host with kotlincflags", 303 Filesystem: testFs, 304 Blueprint: `java_binary_host { 305 name: "java-binary-host", 306 manifest: "test.mf", 307 srcs: ["a.kt"], 308 kotlincflags: ["-flag1", "-flag2"], 309} 310`, 311 ExpectedBazelTargets: []string{ 312 MakeBazelTarget("kt_jvm_library", "java-binary-host_lib", AttrNameToString{ 313 "srcs": `["a.kt"]`, 314 "kotlincflags": `[ 315 "-flag1", 316 "-flag2", 317 ]`, 318 "target_compatible_with": `select({ 319 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], 320 "//conditions:default": [], 321 })`, 322 }), 323 MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{ 324 "main_class": `"com.android.test.MainClass"`, 325 "runtime_deps": `[":java-binary-host_lib"]`, 326 "target_compatible_with": `select({ 327 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], 328 "//conditions:default": [], 329 })`, 330 }), 331 }, 332 }) 333} 334