java_binary_host_conversion_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. package bp2build
  15. import (
  16. "testing"
  17. "android/soong/android"
  18. "android/soong/cc"
  19. "android/soong/java"
  20. )
  21. func runJavaBinaryHostTestCase(t *testing.T, tc Bp2buildTestCase) {
  22. t.Helper()
  23. (&tc).ModuleTypeUnderTest = "java_binary_host"
  24. (&tc).ModuleTypeUnderTestFactory = java.BinaryHostFactory
  25. RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
  26. ctx.RegisterModuleType("cc_library_host_shared", cc.LibraryHostSharedFactory)
  27. ctx.RegisterModuleType("java_library", java.LibraryFactory)
  28. ctx.RegisterModuleType("java_import_host", java.ImportFactory)
  29. }, tc)
  30. }
  31. var testFs = map[string]string{
  32. "test.mf": "Main-Class: com.android.test.MainClass",
  33. "other/Android.bp": `cc_library_host_shared {
  34. name: "jni-lib-1",
  35. stl: "none",
  36. }`,
  37. }
  38. func TestJavaBinaryHost(t *testing.T) {
  39. runJavaBinaryHostTestCase(t, Bp2buildTestCase{
  40. Description: "java_binary_host with srcs, exclude_srcs, jni_libs, javacflags, and manifest.",
  41. Filesystem: testFs,
  42. Blueprint: `java_binary_host {
  43. name: "java-binary-host-1",
  44. srcs: ["a.java", "b.java"],
  45. exclude_srcs: ["b.java"],
  46. manifest: "test.mf",
  47. jni_libs: ["jni-lib-1"],
  48. javacflags: ["-Xdoclint:all/protected"],
  49. bazel_module: { bp2build_available: true },
  50. java_version: "8",
  51. }`,
  52. ExpectedBazelTargets: []string{
  53. MakeBazelTarget("java_library", "java-binary-host-1_lib", AttrNameToString{
  54. "srcs": `["a.java"]`,
  55. "deps": `["//other:jni-lib-1"]`,
  56. "java_version": `"8"`,
  57. "javacopts": `["-Xdoclint:all/protected"]`,
  58. "target_compatible_with": `select({
  59. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  60. "//conditions:default": [],
  61. })`,
  62. }),
  63. MakeBazelTarget("java_binary", "java-binary-host-1", AttrNameToString{
  64. "main_class": `"com.android.test.MainClass"`,
  65. "jvm_flags": `["-Djava.library.path=$${RUNPATH}other/jni-lib-1"]`,
  66. "target_compatible_with": `select({
  67. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  68. "//conditions:default": [],
  69. })`,
  70. "runtime_deps": `[":java-binary-host-1_lib"]`,
  71. }),
  72. },
  73. })
  74. }
  75. func TestJavaBinaryHostRuntimeDeps(t *testing.T) {
  76. runJavaBinaryHostTestCase(t, Bp2buildTestCase{
  77. Description: "java_binary_host with srcs, exclude_srcs, jni_libs, javacflags, and manifest.",
  78. Filesystem: testFs,
  79. Blueprint: `java_binary_host {
  80. name: "java-binary-host-1",
  81. static_libs: ["java-dep-1"],
  82. manifest: "test.mf",
  83. bazel_module: { bp2build_available: true },
  84. }
  85. java_library {
  86. name: "java-dep-1",
  87. srcs: ["a.java"],
  88. bazel_module: { bp2build_available: false },
  89. }
  90. `,
  91. ExpectedBazelTargets: []string{
  92. MakeBazelTarget("java_binary", "java-binary-host-1", AttrNameToString{
  93. "main_class": `"com.android.test.MainClass"`,
  94. "runtime_deps": `[":java-dep-1"]`,
  95. "target_compatible_with": `select({
  96. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  97. "//conditions:default": [],
  98. })`,
  99. }),
  100. },
  101. })
  102. }
  103. func TestJavaBinaryHostLibs(t *testing.T) {
  104. runJavaBinaryHostTestCase(t, Bp2buildTestCase{
  105. Description: "java_binary_host with srcs, libs.",
  106. Filesystem: testFs,
  107. Blueprint: `java_binary_host {
  108. name: "java-binary-host-libs",
  109. libs: ["java-lib-dep-1"],
  110. manifest: "test.mf",
  111. srcs: ["a.java"],
  112. }
  113. java_import_host{
  114. name: "java-lib-dep-1",
  115. jars: ["foo.jar"],
  116. bazel_module: { bp2build_available: false },
  117. }
  118. `,
  119. ExpectedBazelTargets: []string{
  120. MakeBazelTarget("java_library", "java-binary-host-libs_lib", AttrNameToString{
  121. "srcs": `["a.java"]`,
  122. "deps": `[":java-lib-dep-1-neverlink"]`,
  123. "target_compatible_with": `select({
  124. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  125. "//conditions:default": [],
  126. })`,
  127. }),
  128. MakeBazelTarget("java_binary", "java-binary-host-libs", AttrNameToString{
  129. "main_class": `"com.android.test.MainClass"`,
  130. "target_compatible_with": `select({
  131. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  132. "//conditions:default": [],
  133. })`,
  134. "runtime_deps": `[":java-binary-host-libs_lib"]`,
  135. }),
  136. },
  137. })
  138. }
  139. func TestJavaBinaryHostKotlinSrcs(t *testing.T) {
  140. runJavaBinaryHostTestCase(t, Bp2buildTestCase{
  141. Description: "java_binary_host with srcs, libs.",
  142. Filesystem: testFs,
  143. Blueprint: `java_binary_host {
  144. name: "java-binary-host",
  145. manifest: "test.mf",
  146. srcs: ["a.java", "b.kt"],
  147. }
  148. `,
  149. ExpectedBazelTargets: []string{
  150. MakeBazelTarget("kt_jvm_library", "java-binary-host_lib", AttrNameToString{
  151. "srcs": `[
  152. "a.java",
  153. "b.kt",
  154. ]`,
  155. "target_compatible_with": `select({
  156. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  157. "//conditions:default": [],
  158. })`,
  159. }),
  160. MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{
  161. "main_class": `"com.android.test.MainClass"`,
  162. "runtime_deps": `[":java-binary-host_lib"]`,
  163. "target_compatible_with": `select({
  164. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  165. "//conditions:default": [],
  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_lib", 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_lib"]`,
  194. "target_compatible_with": `select({
  195. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  196. "//conditions:default": [],
  197. })`,
  198. }),
  199. },
  200. })
  201. }
  202. func TestJavaBinaryHostKotlinWithResourceDir(t *testing.T) {
  203. runJavaBinaryHostTestCase(t, Bp2buildTestCase{
  204. Description: "java_binary_host with srcs, libs, resource dir .",
  205. Filesystem: map[string]string{
  206. "test.mf": "Main-Class: com.android.test.MainClass",
  207. "res/a.res": "",
  208. "res/dir1/b.res": "",
  209. },
  210. Blueprint: `java_binary_host {
  211. name: "java-binary-host",
  212. manifest: "test.mf",
  213. srcs: ["a.java", "b.kt"],
  214. java_resource_dirs: ["res"],
  215. }
  216. `,
  217. ExpectedBazelTargets: []string{
  218. MakeBazelTarget("kt_jvm_library", "java-binary-host_lib", AttrNameToString{
  219. "srcs": `[
  220. "a.java",
  221. "b.kt",
  222. ]`,
  223. "resources": `[
  224. "res/a.res",
  225. "res/dir1/b.res",
  226. ]`,
  227. "resource_strip_prefix": `"res"`,
  228. "target_compatible_with": `select({
  229. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  230. "//conditions:default": [],
  231. })`,
  232. }),
  233. MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{
  234. "main_class": `"com.android.test.MainClass"`,
  235. "runtime_deps": `[":java-binary-host_lib"]`,
  236. "target_compatible_with": `select({
  237. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  238. "//conditions:default": [],
  239. })`,
  240. }),
  241. },
  242. })
  243. }
  244. func TestJavaBinaryHostKotlinWithResources(t *testing.T) {
  245. runJavaBinaryHostTestCase(t, Bp2buildTestCase{
  246. Description: "java_binary_host with srcs, libs, resources.",
  247. Filesystem: map[string]string{
  248. "adir/test.mf": "Main-Class: com.android.test.MainClass",
  249. "adir/res/a.res": "",
  250. "adir/res/b.res": "",
  251. "adir/Android.bp": `java_binary_host {
  252. name: "java-binary-host",
  253. manifest: "test.mf",
  254. srcs: ["a.java", "b.kt"],
  255. java_resources: ["res/a.res", "res/b.res"],
  256. bazel_module: { bp2build_available: true },
  257. }
  258. `,
  259. },
  260. Dir: "adir",
  261. Blueprint: "",
  262. ExpectedBazelTargets: []string{
  263. MakeBazelTarget("kt_jvm_library", "java-binary-host_lib", AttrNameToString{
  264. "srcs": `[
  265. "a.java",
  266. "b.kt",
  267. ]`,
  268. "resources": `[
  269. "res/a.res",
  270. "res/b.res",
  271. ]`,
  272. "resource_strip_prefix": `"adir"`,
  273. "target_compatible_with": `select({
  274. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  275. "//conditions:default": [],
  276. })`,
  277. }),
  278. MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{
  279. "main_class": `"com.android.test.MainClass"`,
  280. "runtime_deps": `[":java-binary-host_lib"]`,
  281. "target_compatible_with": `select({
  282. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  283. "//conditions:default": [],
  284. })`,
  285. }),
  286. },
  287. })
  288. }
  289. func TestJavaBinaryHostKotlinCflags(t *testing.T) {
  290. runJavaBinaryHostTestCase(t, Bp2buildTestCase{
  291. Description: "java_binary_host with kotlincflags",
  292. Filesystem: testFs,
  293. Blueprint: `java_binary_host {
  294. name: "java-binary-host",
  295. manifest: "test.mf",
  296. srcs: ["a.kt"],
  297. kotlincflags: ["-flag1", "-flag2"],
  298. }
  299. `,
  300. ExpectedBazelTargets: []string{
  301. MakeBazelTarget("kt_jvm_library", "java-binary-host_lib", AttrNameToString{
  302. "srcs": `["a.kt"]`,
  303. "kotlincflags": `[
  304. "-flag1",
  305. "-flag2",
  306. ]`,
  307. "target_compatible_with": `select({
  308. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  309. "//conditions:default": [],
  310. })`,
  311. }),
  312. MakeBazelTarget("java_binary", "java-binary-host", AttrNameToString{
  313. "main_class": `"com.android.test.MainClass"`,
  314. "runtime_deps": `[":java-binary-host_lib"]`,
  315. "target_compatible_with": `select({
  316. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  317. "//conditions:default": [],
  318. })`,
  319. }),
  320. },
  321. })
  322. }