aar_conversion_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright 2022 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. "android/soong/android"
  17. "android/soong/java"
  18. "fmt"
  19. "testing"
  20. )
  21. func TestConvertAndroidLibrary(t *testing.T) {
  22. t.Helper()
  23. RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
  24. Description: "Android Library - simple example",
  25. ModuleTypeUnderTest: "android_library",
  26. ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
  27. Filesystem: map[string]string{
  28. "lib.java": "",
  29. "arm.java": "",
  30. "x86.java": "",
  31. "res/res.png": "",
  32. "manifest/AndroidManifest.xml": "",
  33. },
  34. Blueprint: simpleModuleDoNotConvertBp2build("android_library", "static_lib_dep") + `
  35. android_library {
  36. name: "TestLib",
  37. srcs: ["lib.java"],
  38. arch: {
  39. arm: {
  40. srcs: ["arm.java"],
  41. },
  42. x86: {
  43. srcs: ["x86.java"],
  44. }
  45. },
  46. manifest: "manifest/AndroidManifest.xml",
  47. static_libs: ["static_lib_dep"],
  48. java_version: "7",
  49. }
  50. `,
  51. ExpectedBazelTargets: []string{
  52. MakeBazelTarget(
  53. "android_library",
  54. "TestLib",
  55. AttrNameToString{
  56. "srcs": `["lib.java"] + select({
  57. "//build/bazel/platforms/arch:arm": ["arm.java"],
  58. "//build/bazel/platforms/arch:x86": ["x86.java"],
  59. "//conditions:default": [],
  60. })`,
  61. "manifest": `"manifest/AndroidManifest.xml"`,
  62. "resource_files": `["res/res.png"]`,
  63. "deps": `[":static_lib_dep"]`,
  64. "exports": `[":static_lib_dep"]`,
  65. "java_version": `"7"`,
  66. }),
  67. MakeNeverlinkDuplicateTargetWithAttrs(
  68. "android_library",
  69. "TestLib",
  70. AttrNameToString{"java_version": `"7"`}),
  71. }})
  72. }
  73. func TestConvertAndroidLibraryWithNoSources(t *testing.T) {
  74. t.Helper()
  75. RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
  76. Description: "Android Library - modules with deps must have sources",
  77. ModuleTypeUnderTest: "android_library",
  78. ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
  79. Filesystem: map[string]string{
  80. "res/res.png": "",
  81. "AndroidManifest.xml": "",
  82. },
  83. Blueprint: simpleModuleDoNotConvertBp2build("android_library", "lib_dep") + `
  84. android_library {
  85. name: "TestLib",
  86. srcs: [],
  87. manifest: "AndroidManifest.xml",
  88. libs: ["lib_dep"],
  89. }
  90. `,
  91. ExpectedErr: fmt.Errorf("Module has direct dependencies but no sources. Bazel will not allow this."),
  92. ExpectedBazelTargets: []string{},
  93. })
  94. }
  95. func TestConvertAndroidLibraryImport(t *testing.T) {
  96. t.Helper()
  97. RunBp2BuildTestCase(
  98. t,
  99. func(ctx android.RegistrationContext) {
  100. ctx.RegisterModuleType("android_library", java.AndroidLibraryFactory)
  101. },
  102. Bp2buildTestCase{
  103. Description: "Android Library Import",
  104. ModuleTypeUnderTest: "android_library_import",
  105. ModuleTypeUnderTestFactory: java.AARImportFactory,
  106. Filesystem: map[string]string{
  107. "import.aar": "",
  108. },
  109. // Bazel's aar_import can only export *_import targets, so we expect
  110. // only "static_import_dep" in exports, but both "static_lib_dep" and
  111. // "static_import_dep" in deps
  112. Blueprint: simpleModuleDoNotConvertBp2build("android_library", "static_lib_dep") +
  113. simpleModuleDoNotConvertBp2build("android_library_import", "static_import_dep") + `
  114. android_library_import {
  115. name: "TestImport",
  116. aars: ["import.aar"],
  117. static_libs: ["static_lib_dep", "static_import_dep"],
  118. }
  119. `,
  120. ExpectedBazelTargets: []string{
  121. MakeBazelTarget(
  122. "aar_import",
  123. "TestImport",
  124. AttrNameToString{
  125. "aar": `"import.aar"`,
  126. "deps": `[
  127. ":static_lib_dep",
  128. ":static_import_dep",
  129. ]`,
  130. "exports": `[":static_import_dep"]`,
  131. },
  132. ),
  133. MakeNeverlinkDuplicateTarget("android_library", "TestImport"),
  134. },
  135. },
  136. )
  137. }
  138. func TestConvertAndroidLibraryKotlin(t *testing.T) {
  139. t.Helper()
  140. RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
  141. Description: "Android Library with .kt srcs and common_srcs attribute",
  142. ModuleTypeUnderTest: "android_library",
  143. ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
  144. Filesystem: map[string]string{
  145. "AndroidManifest.xml": "",
  146. },
  147. Blueprint: `
  148. android_library {
  149. name: "TestLib",
  150. srcs: ["a.java", "b.kt"],
  151. common_srcs: ["c.kt"],
  152. }
  153. `,
  154. ExpectedBazelTargets: []string{
  155. MakeBazelTarget(
  156. "android_library",
  157. "TestLib",
  158. AttrNameToString{
  159. "srcs": `[
  160. "a.java",
  161. "b.kt",
  162. ]`,
  163. "common_srcs": `["c.kt"]`,
  164. "manifest": `"AndroidManifest.xml"`,
  165. "resource_files": `[]`,
  166. }),
  167. MakeNeverlinkDuplicateTarget("android_library", "TestLib"),
  168. }})
  169. }
  170. func TestConvertAndroidLibraryKotlinCflags(t *testing.T) {
  171. t.Helper()
  172. RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, Bp2buildTestCase{
  173. Description: "Android Library with .kt srcs and kotlincflags ",
  174. ModuleTypeUnderTest: "android_library",
  175. ModuleTypeUnderTestFactory: java.AndroidLibraryFactory,
  176. Filesystem: map[string]string{
  177. "AndroidManifest.xml": "",
  178. },
  179. Blueprint: `
  180. android_library {
  181. name: "TestLib",
  182. srcs: ["a.java", "b.kt"],
  183. kotlincflags: ["-flag1", "-flag2"],
  184. }
  185. `,
  186. ExpectedBazelTargets: []string{
  187. MakeBazelTarget(
  188. "android_library",
  189. "TestLib",
  190. AttrNameToString{
  191. "srcs": `[
  192. "a.java",
  193. "b.kt",
  194. ]`,
  195. "kotlincflags": `[
  196. "-flag1",
  197. "-flag2",
  198. ]`,
  199. "manifest": `"AndroidManifest.xml"`,
  200. "resource_files": `[]`,
  201. }),
  202. MakeNeverlinkDuplicateTarget("android_library", "TestLib"),
  203. }})
  204. }