ndk_headers_conversion_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 TestNdkHeaderFilepaths(t *testing.T) {
  21. bpTemplate := `
  22. ndk_headers {
  23. name: "foo",
  24. srcs: %v,
  25. exclude_srcs: %v,
  26. }
  27. `
  28. testCases := []struct {
  29. desc string
  30. srcs string
  31. excludeSrcs string
  32. expectedHdrs string
  33. }{
  34. {
  35. desc: "Single header file",
  36. srcs: `["foo.h"]`,
  37. excludeSrcs: `[]`,
  38. expectedHdrs: `["foo.h"]`,
  39. },
  40. {
  41. desc: "Multiple header files",
  42. srcs: `["foo.h", "foo_other.h"]`,
  43. excludeSrcs: `[]`,
  44. expectedHdrs: `[
  45. "foo.h",
  46. "foo_other.h",
  47. ]`,
  48. },
  49. {
  50. desc: "Multiple header files with excludes",
  51. srcs: `["foo.h", "foo_other.h"]`,
  52. excludeSrcs: `["foo_other.h"]`,
  53. expectedHdrs: `["foo.h"]`,
  54. },
  55. {
  56. desc: "Multiple header files via Soong-supported globs",
  57. srcs: `["*.h"]`,
  58. excludeSrcs: `[]`,
  59. expectedHdrs: `[
  60. "foo.h",
  61. "foo_other.h",
  62. ]`,
  63. },
  64. }
  65. for _, testCase := range testCases {
  66. fs := map[string]string{
  67. "foo.h": "",
  68. "foo_other.h": "",
  69. }
  70. expectedApiContributionTargetName := "foo.contribution"
  71. expectedBazelTarget := MakeBazelTargetNoRestrictions(
  72. "cc_api_headers",
  73. expectedApiContributionTargetName,
  74. AttrNameToString{
  75. "hdrs": testCase.expectedHdrs,
  76. },
  77. )
  78. RunApiBp2BuildTestCase(t, cc.RegisterNdkModuleTypes, Bp2buildTestCase{
  79. Description: testCase.desc,
  80. Blueprint: fmt.Sprintf(bpTemplate, testCase.srcs, testCase.excludeSrcs),
  81. ExpectedBazelTargets: []string{expectedBazelTarget},
  82. Filesystem: fs,
  83. })
  84. }
  85. }
  86. func TestNdkHeaderIncludeDir(t *testing.T) {
  87. bpTemplate := `
  88. ndk_headers {
  89. name: "foo",
  90. from: %v,
  91. to: "this/value/is/ignored",
  92. }
  93. `
  94. testCases := []struct {
  95. desc string
  96. from string
  97. expectedIncludeDir string
  98. }{
  99. {
  100. desc: "Empty `from` value",
  101. from: `""`,
  102. expectedIncludeDir: `""`,
  103. },
  104. {
  105. desc: "Non-Empty `from` value",
  106. from: `"include"`,
  107. expectedIncludeDir: `"include"`,
  108. },
  109. }
  110. for _, testCase := range testCases {
  111. expectedApiContributionTargetName := "foo.contribution"
  112. expectedBazelTarget := MakeBazelTargetNoRestrictions(
  113. "cc_api_headers",
  114. expectedApiContributionTargetName,
  115. AttrNameToString{
  116. "include_dir": testCase.expectedIncludeDir,
  117. },
  118. )
  119. RunApiBp2BuildTestCase(t, cc.RegisterNdkModuleTypes, Bp2buildTestCase{
  120. Description: testCase.desc,
  121. Blueprint: fmt.Sprintf(bpTemplate, testCase.from),
  122. ExpectedBazelTargets: []string{expectedBazelTarget},
  123. })
  124. }
  125. }
  126. func TestVersionedNdkHeaderFilepaths(t *testing.T) {
  127. bp := `
  128. versioned_ndk_headers {
  129. name: "common_libc",
  130. from: "include"
  131. }
  132. `
  133. fs := map[string]string{
  134. "include/math.h": "",
  135. "include/stdio.h": "",
  136. "include/arm/arm.h": "",
  137. "include/x86/x86.h": "",
  138. }
  139. expectedApiContributionTargetName := "common_libc.contribution"
  140. expectedBazelTarget := MakeBazelTargetNoRestrictions(
  141. "cc_api_headers",
  142. expectedApiContributionTargetName,
  143. AttrNameToString{
  144. "include_dir": `"include"`,
  145. "hdrs": `[
  146. "include/math.h",
  147. "include/stdio.h",
  148. "include/arm/arm.h",
  149. "include/x86/x86.h",
  150. ]`,
  151. },
  152. )
  153. RunApiBp2BuildTestCase(t, cc.RegisterNdkModuleTypes, Bp2buildTestCase{
  154. Blueprint: bp,
  155. Filesystem: fs,
  156. ExpectedBazelTargets: []string{expectedBazelTarget},
  157. })
  158. }