cc_prebuilt_library_conversion_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. "fmt"
  17. "testing"
  18. "android/soong/cc"
  19. )
  20. func TestPrebuiltLibraryStaticAndSharedSimple(t *testing.T) {
  21. runBp2BuildTestCaseSimple(t,
  22. Bp2buildTestCase{
  23. Description: "prebuilt library static and shared simple",
  24. ModuleTypeUnderTest: "cc_prebuilt_library",
  25. ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory,
  26. Filesystem: map[string]string{
  27. "libf.so": "",
  28. },
  29. Blueprint: `
  30. cc_prebuilt_library {
  31. name: "libtest",
  32. srcs: ["libf.so"],
  33. bazel_module: { bp2build_available: true },
  34. }`,
  35. ExpectedBazelTargets: []string{
  36. makeBazelTarget("prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{
  37. "static_library": `"libf.so"`,
  38. }),
  39. makeBazelTarget("prebuilt_library_shared", "libtest", AttrNameToString{
  40. "shared_library": `"libf.so"`,
  41. }),
  42. },
  43. })
  44. }
  45. func TestPrebuiltLibraryWithArchVariance(t *testing.T) {
  46. runBp2BuildTestCaseSimple(t,
  47. Bp2buildTestCase{
  48. Description: "prebuilt library with arch variance",
  49. ModuleTypeUnderTest: "cc_prebuilt_library",
  50. ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory,
  51. Filesystem: map[string]string{
  52. "libf.so": "",
  53. "libg.so": "",
  54. },
  55. Blueprint: `
  56. cc_prebuilt_library {
  57. name: "libtest",
  58. arch: {
  59. arm64: { srcs: ["libf.so"], },
  60. arm: { srcs: ["libg.so"], },
  61. },
  62. bazel_module: { bp2build_available: true },
  63. }`,
  64. ExpectedBazelTargets: []string{
  65. makeBazelTarget("prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{
  66. "static_library": `select({
  67. "//build/bazel/platforms/arch:arm": "libg.so",
  68. "//build/bazel/platforms/arch:arm64": "libf.so",
  69. "//conditions:default": None,
  70. })`,
  71. }),
  72. makeBazelTarget("prebuilt_library_shared", "libtest", AttrNameToString{
  73. "shared_library": `select({
  74. "//build/bazel/platforms/arch:arm": "libg.so",
  75. "//build/bazel/platforms/arch:arm64": "libf.so",
  76. "//conditions:default": None,
  77. })`,
  78. }),
  79. },
  80. })
  81. }
  82. func TestPrebuiltLibraryAdditionalAttrs(t *testing.T) {
  83. runBp2BuildTestCaseSimple(t,
  84. Bp2buildTestCase{
  85. Description: "prebuilt library additional attributes",
  86. ModuleTypeUnderTest: "cc_prebuilt_library",
  87. ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory,
  88. Filesystem: map[string]string{
  89. "libf.so": "",
  90. "testdir/1/": "",
  91. "testdir/2/": "",
  92. },
  93. Blueprint: `
  94. cc_prebuilt_library {
  95. name: "libtest",
  96. srcs: ["libf.so"],
  97. export_include_dirs: ["testdir/1/"],
  98. export_system_include_dirs: ["testdir/2/"],
  99. bazel_module: { bp2build_available: true },
  100. }`,
  101. ExpectedBazelTargets: []string{
  102. makeBazelTarget("prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{
  103. "static_library": `"libf.so"`,
  104. "export_includes": `["testdir/1/"]`,
  105. "export_system_includes": `["testdir/2/"]`,
  106. }),
  107. // TODO(b/229374533): When fixed, update this test
  108. makeBazelTarget("prebuilt_library_shared", "libtest", AttrNameToString{
  109. "shared_library": `"libf.so"`,
  110. }),
  111. },
  112. })
  113. }
  114. func TestPrebuiltLibrarySharedStanzaFails(t *testing.T) {
  115. runBp2BuildTestCaseSimple(t,
  116. Bp2buildTestCase{
  117. Description: "prebuilt library with shared stanza fails because multiple sources",
  118. ModuleTypeUnderTest: "cc_prebuilt_library",
  119. ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory,
  120. Filesystem: map[string]string{
  121. "libf.so": "",
  122. "libg.so": "",
  123. },
  124. Blueprint: `
  125. cc_prebuilt_library {
  126. name: "libtest",
  127. srcs: ["libf.so"],
  128. shared: {
  129. srcs: ["libg.so"],
  130. },
  131. bazel_module: { bp2build_available: true },
  132. }`,
  133. ExpectedErr: fmt.Errorf("Expected at most one source file"),
  134. })
  135. }
  136. func TestPrebuiltLibraryStaticStanzaFails(t *testing.T) {
  137. runBp2BuildTestCaseSimple(t,
  138. Bp2buildTestCase{
  139. Description: "prebuilt library with static stanza fails because multiple sources",
  140. ModuleTypeUnderTest: "cc_prebuilt_library",
  141. ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory,
  142. Filesystem: map[string]string{
  143. "libf.so": "",
  144. "libg.so": "",
  145. },
  146. Blueprint: `
  147. cc_prebuilt_library {
  148. name: "libtest",
  149. srcs: ["libf.so"],
  150. static: {
  151. srcs: ["libg.so"],
  152. },
  153. bazel_module: { bp2build_available: true },
  154. }`,
  155. ExpectedErr: fmt.Errorf("Expected at most one source file"),
  156. })
  157. }
  158. func TestPrebuiltLibrarySharedAndStaticStanzas(t *testing.T) {
  159. runBp2BuildTestCaseSimple(t,
  160. Bp2buildTestCase{
  161. Description: "prebuilt library with both shared and static stanzas",
  162. ModuleTypeUnderTest: "cc_prebuilt_library",
  163. ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory,
  164. Filesystem: map[string]string{
  165. "libf.so": "",
  166. "libg.so": "",
  167. },
  168. Blueprint: `
  169. cc_prebuilt_library {
  170. name: "libtest",
  171. static: {
  172. srcs: ["libf.so"],
  173. },
  174. shared: {
  175. srcs: ["libg.so"],
  176. },
  177. bazel_module: { bp2build_available: true },
  178. }`,
  179. ExpectedBazelTargets: []string{
  180. makeBazelTarget("prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{
  181. "static_library": `"libf.so"`,
  182. }),
  183. makeBazelTarget("prebuilt_library_shared", "libtest", AttrNameToString{
  184. "shared_library": `"libg.so"`,
  185. }),
  186. },
  187. })
  188. }
  189. // TODO(b/228623543): When this bug is fixed, enable this test
  190. //func TestPrebuiltLibraryOnlyShared(t *testing.T) {
  191. // runBp2BuildTestCaseSimple(t,
  192. // bp2buildTestCase{
  193. // description: "prebuilt library shared only",
  194. // moduleTypeUnderTest: "cc_prebuilt_library",
  195. // moduleTypeUnderTestFactory: cc.PrebuiltLibraryFactory,
  196. // filesystem: map[string]string{
  197. // "libf.so": "",
  198. // },
  199. // blueprint: `
  200. //cc_prebuilt_library {
  201. // name: "libtest",
  202. // srcs: ["libf.so"],
  203. // static: {
  204. // enabled: false,
  205. // },
  206. // bazel_module: { bp2build_available: true },
  207. //}`,
  208. // expectedBazelTargets: []string{
  209. // makeBazelTarget("prebuilt_library_shared", "libtest", attrNameToString{
  210. // "shared_library": `"libf.so"`,
  211. // }),
  212. // },
  213. // })
  214. //}
  215. // TODO(b/228623543): When this bug is fixed, enable this test
  216. //func TestPrebuiltLibraryOnlyStatic(t *testing.T) {
  217. // runBp2BuildTestCaseSimple(t,
  218. // bp2buildTestCase{
  219. // description: "prebuilt library static only",
  220. // moduleTypeUnderTest: "cc_prebuilt_library",
  221. // moduleTypeUnderTestFactory: cc.PrebuiltLibraryFactory,
  222. // filesystem: map[string]string{
  223. // "libf.so": "",
  224. // },
  225. // blueprint: `
  226. //cc_prebuilt_library {
  227. // name: "libtest",
  228. // srcs: ["libf.so"],
  229. // shared: {
  230. // enabled: false,
  231. // },
  232. // bazel_module: { bp2build_available: true },
  233. //}`,
  234. // expectedBazelTargets: []string{
  235. // makeBazelTarget("prebuilt_library_static", "libtest_bp2build_cc_library_static", attrNameToString{
  236. // "static_library": `"libf.so"`,
  237. // }),
  238. // },
  239. // })
  240. //}