cc_yasm_conversion_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. "testing"
  17. "android/soong/android"
  18. "android/soong/cc"
  19. )
  20. func runYasmTestCase(t *testing.T, tc Bp2buildTestCase) {
  21. t.Helper()
  22. RunBp2BuildTestCase(t, registerYasmModuleTypes, tc)
  23. }
  24. func registerYasmModuleTypes(ctx android.RegistrationContext) {
  25. cc.RegisterCCBuildComponents(ctx)
  26. ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
  27. ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
  28. ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory)
  29. ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
  30. }
  31. func TestYasmSimple(t *testing.T) {
  32. runYasmTestCase(t, Bp2buildTestCase{
  33. Description: "Simple yasm test",
  34. ModuleTypeUnderTest: "cc_library",
  35. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  36. Filesystem: map[string]string{
  37. "main.cpp": "",
  38. "myfile.asm": "",
  39. },
  40. Blueprint: `
  41. cc_library {
  42. name: "foo",
  43. srcs: ["main.cpp", "myfile.asm"],
  44. }`,
  45. ExpectedBazelTargets: append([]string{
  46. MakeBazelTarget("yasm", "foo_yasm", map[string]string{
  47. "include_dirs": `["."]`,
  48. "srcs": `["myfile.asm"]`,
  49. }),
  50. }, makeCcLibraryTargets("foo", map[string]string{
  51. "local_includes": `["."]`,
  52. "srcs": `[
  53. "main.cpp",
  54. ":foo_yasm",
  55. ]`,
  56. })...),
  57. })
  58. }
  59. func TestYasmWithIncludeDirs(t *testing.T) {
  60. runYasmTestCase(t, Bp2buildTestCase{
  61. Description: "Simple yasm test",
  62. ModuleTypeUnderTest: "cc_library",
  63. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  64. Filesystem: map[string]string{
  65. "main.cpp": "",
  66. "myfile.asm": "",
  67. "include1/foo/myinclude.inc": "",
  68. "include2/foo/myinclude2.inc": "",
  69. },
  70. Blueprint: `
  71. cc_library {
  72. name: "foo",
  73. local_include_dirs: ["include1/foo"],
  74. export_include_dirs: ["include2/foo"],
  75. srcs: ["main.cpp", "myfile.asm"],
  76. }`,
  77. ExpectedBazelTargets: append([]string{
  78. MakeBazelTarget("yasm", "foo_yasm", map[string]string{
  79. "include_dirs": `[
  80. "include1/foo",
  81. ".",
  82. "include2/foo",
  83. ]`,
  84. "srcs": `["myfile.asm"]`,
  85. }),
  86. }, makeCcLibraryTargets("foo", map[string]string{
  87. "local_includes": `[
  88. "include1/foo",
  89. ".",
  90. ]`,
  91. "export_includes": `["include2/foo"]`,
  92. "srcs": `[
  93. "main.cpp",
  94. ":foo_yasm",
  95. ]`,
  96. })...),
  97. })
  98. }
  99. func TestYasmConditionalBasedOnArch(t *testing.T) {
  100. runYasmTestCase(t, Bp2buildTestCase{
  101. Description: "Simple yasm test",
  102. ModuleTypeUnderTest: "cc_library",
  103. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  104. Filesystem: map[string]string{
  105. "main.cpp": "",
  106. "myfile.asm": "",
  107. },
  108. Blueprint: `
  109. cc_library {
  110. name: "foo",
  111. srcs: ["main.cpp"],
  112. arch: {
  113. x86: {
  114. srcs: ["myfile.asm"],
  115. },
  116. },
  117. }`,
  118. ExpectedBazelTargets: append([]string{
  119. MakeBazelTarget("yasm", "foo_yasm", map[string]string{
  120. "include_dirs": `["."]`,
  121. "srcs": `select({
  122. "//build/bazel/platforms/arch:x86": ["myfile.asm"],
  123. "//conditions:default": [],
  124. })`,
  125. }),
  126. }, makeCcLibraryTargets("foo", map[string]string{
  127. "local_includes": `["."]`,
  128. "srcs": `["main.cpp"] + select({
  129. "//build/bazel/platforms/arch:x86": [":foo_yasm"],
  130. "//conditions:default": [],
  131. })`,
  132. })...),
  133. })
  134. }
  135. func TestYasmPartiallyConditional(t *testing.T) {
  136. runYasmTestCase(t, Bp2buildTestCase{
  137. Description: "Simple yasm test",
  138. ModuleTypeUnderTest: "cc_library",
  139. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  140. Filesystem: map[string]string{
  141. "main.cpp": "",
  142. "myfile.asm": "",
  143. "mysecondfile.asm": "",
  144. },
  145. Blueprint: `
  146. cc_library {
  147. name: "foo",
  148. srcs: ["main.cpp", "myfile.asm"],
  149. arch: {
  150. x86: {
  151. srcs: ["mysecondfile.asm"],
  152. },
  153. },
  154. }`,
  155. ExpectedBazelTargets: append([]string{
  156. MakeBazelTarget("yasm", "foo_yasm", map[string]string{
  157. "include_dirs": `["."]`,
  158. "srcs": `["myfile.asm"] + select({
  159. "//build/bazel/platforms/arch:x86": ["mysecondfile.asm"],
  160. "//conditions:default": [],
  161. })`,
  162. }),
  163. }, makeCcLibraryTargets("foo", map[string]string{
  164. "local_includes": `["."]`,
  165. "srcs": `[
  166. "main.cpp",
  167. ":foo_yasm",
  168. ]`,
  169. })...),
  170. })
  171. }