cc_prebuilt_library_static_test.go 4.0 KB

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