cc_prebuilt_library_static_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 TestStaticPrebuiltLibrary(t *testing.T) {
  21. RunBp2BuildTestCaseSimple(t,
  22. Bp2buildTestCase{
  23. Description: "prebuilt library static simple",
  24. ModuleTypeUnderTest: "cc_prebuilt_library_static",
  25. ModuleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory,
  26. Filesystem: map[string]string{
  27. "libf.so": "",
  28. },
  29. Blueprint: `
  30. cc_prebuilt_library_static {
  31. name: "libtest",
  32. srcs: ["libf.so"],
  33. bazel_module: { bp2build_available: true },
  34. }`,
  35. ExpectedBazelTargets: []string{
  36. MakeBazelTarget("cc_prebuilt_library_static", "libtest", AttrNameToString{
  37. "static_library": `"libf.so"`,
  38. }),
  39. MakeBazelTarget("cc_prebuilt_library_static", "libtest_alwayslink", AttrNameToString{
  40. "static_library": `"libf.so"`,
  41. "alwayslink": "True",
  42. }),
  43. },
  44. })
  45. }
  46. func TestStaticPrebuiltLibraryWithArchVariance(t *testing.T) {
  47. RunBp2BuildTestCaseSimple(t,
  48. Bp2buildTestCase{
  49. Description: "prebuilt library static with arch variance",
  50. ModuleTypeUnderTest: "cc_prebuilt_library_static",
  51. ModuleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory,
  52. Filesystem: map[string]string{
  53. "libf.so": "",
  54. "libg.so": "",
  55. },
  56. Blueprint: `
  57. cc_prebuilt_library_static {
  58. name: "libtest",
  59. arch: {
  60. arm64: { srcs: ["libf.so"], },
  61. arm: { srcs: ["libg.so"], },
  62. },
  63. bazel_module: { bp2build_available: true },
  64. }`,
  65. ExpectedBazelTargets: []string{
  66. MakeBazelTarget("cc_prebuilt_library_static", "libtest", AttrNameToString{
  67. "static_library": `select({
  68. "//build/bazel/platforms/arch:arm": "libg.so",
  69. "//build/bazel/platforms/arch:arm64": "libf.so",
  70. "//conditions:default": None,
  71. })`}),
  72. MakeBazelTarget("cc_prebuilt_library_static", "libtest_alwayslink", AttrNameToString{
  73. "alwayslink": "True",
  74. "static_library": `select({
  75. "//build/bazel/platforms/arch:arm": "libg.so",
  76. "//build/bazel/platforms/arch:arm64": "libf.so",
  77. "//conditions:default": None,
  78. })`}),
  79. },
  80. })
  81. }
  82. func TestStaticPrebuiltLibraryStaticStanzaFails(t *testing.T) {
  83. RunBp2BuildTestCaseSimple(t,
  84. Bp2buildTestCase{
  85. Description: "prebuilt library with static stanza fails because multiple sources",
  86. ModuleTypeUnderTest: "cc_prebuilt_library_static",
  87. ModuleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory,
  88. Filesystem: map[string]string{
  89. "libf.so": "",
  90. "libg.so": "",
  91. },
  92. Blueprint: `
  93. cc_prebuilt_library_static {
  94. name: "libtest",
  95. srcs: ["libf.so"],
  96. static: {
  97. srcs: ["libg.so"],
  98. },
  99. bazel_module: { bp2build_available: true },
  100. }`,
  101. ExpectedErr: fmt.Errorf("Expected at most one source file"),
  102. })
  103. }
  104. func TestCcLibraryStaticConvertLex(t *testing.T) {
  105. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  106. Description: "cc_library_static with lex files",
  107. ModuleTypeUnderTest: "cc_library_static",
  108. ModuleTypeUnderTestFactory: cc.LibraryStaticFactory,
  109. Filesystem: map[string]string{
  110. "foo.c": "",
  111. "bar.cc": "",
  112. "foo1.l": "",
  113. "bar1.ll": "",
  114. "foo2.l": "",
  115. "bar2.ll": "",
  116. },
  117. Blueprint: `cc_library_static {
  118. name: "foo_lib",
  119. srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
  120. lex: { flags: ["--foo_flags"] },
  121. include_build_directory: false,
  122. bazel_module: { bp2build_available: true },
  123. }`,
  124. ExpectedBazelTargets: []string{
  125. MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
  126. "srcs": `[
  127. "foo1.l",
  128. "foo2.l",
  129. ]`,
  130. "lexopts": `["--foo_flags"]`,
  131. }),
  132. MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
  133. "srcs": `[
  134. "bar1.ll",
  135. "bar2.ll",
  136. ]`,
  137. "lexopts": `["--foo_flags"]`,
  138. }),
  139. MakeBazelTarget("cc_library_static", "foo_lib", AttrNameToString{
  140. "srcs": `[
  141. "bar.cc",
  142. ":foo_lib_genlex_ll",
  143. ]`,
  144. "srcs_c": `[
  145. "foo.c",
  146. ":foo_lib_genlex_l",
  147. ]`,
  148. }),
  149. },
  150. })
  151. }