bindgen_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2020 The Android Open Source Project
  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 rust
  15. import (
  16. "strings"
  17. "testing"
  18. )
  19. func TestRustBindgen(t *testing.T) {
  20. ctx := testRust(t, `
  21. rust_bindgen {
  22. name: "libbindgen",
  23. defaults: ["cc_defaults_flags"],
  24. wrapper_src: "src/any.h",
  25. crate_name: "bindgen",
  26. stem: "libbindgen",
  27. source_stem: "bindings",
  28. bindgen_flags: ["--bindgen-flag.*"],
  29. cflags: ["--clang-flag()"],
  30. shared_libs: ["libfoo_shared"],
  31. static_libs: ["libfoo_static"],
  32. header_libs: ["libfoo_header"],
  33. }
  34. cc_library_shared {
  35. name: "libfoo_shared",
  36. export_include_dirs: ["shared_include"],
  37. }
  38. cc_library_static {
  39. name: "libfoo_static",
  40. export_include_dirs: ["static_include"],
  41. }
  42. cc_library_headers {
  43. name: "libfoo_header",
  44. export_include_dirs: ["header_include"],
  45. }
  46. cc_defaults {
  47. name: "cc_defaults_flags",
  48. cflags: ["--default-flag"],
  49. }
  50. `)
  51. libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a_source").Output("bindings.rs")
  52. // Ensure that the flags are present and escaped
  53. if !strings.Contains(libbindgen.Args["flags"], "'--bindgen-flag.*'") {
  54. t.Errorf("missing bindgen flags in rust_bindgen rule: flags %#v", libbindgen.Args["flags"])
  55. }
  56. if !strings.Contains(libbindgen.Args["cflags"], "'--clang-flag()'") {
  57. t.Errorf("missing clang cflags in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"])
  58. }
  59. if !strings.Contains(libbindgen.Args["cflags"], "-Ishared_include") {
  60. t.Errorf("missing shared_libs exported includes in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"])
  61. }
  62. if !strings.Contains(libbindgen.Args["cflags"], "-Istatic_include") {
  63. t.Errorf("missing static_libs exported includes in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"])
  64. }
  65. if !strings.Contains(libbindgen.Args["cflags"], "-Iheader_include") {
  66. t.Errorf("missing static_libs exported includes in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"])
  67. }
  68. if !strings.Contains(libbindgen.Args["cflags"], "--default-flag") {
  69. t.Errorf("rust_bindgen missing cflags defined in cc_defaults: cflags %#v", libbindgen.Args["cflags"])
  70. }
  71. }
  72. func TestRustBindgenCustomBindgen(t *testing.T) {
  73. ctx := testRust(t, `
  74. rust_bindgen {
  75. name: "libbindgen",
  76. wrapper_src: "src/any.h",
  77. crate_name: "bindgen",
  78. stem: "libbindgen",
  79. source_stem: "bindings",
  80. custom_bindgen: "my_bindgen"
  81. }
  82. rust_binary_host {
  83. name: "my_bindgen",
  84. srcs: ["foo.rs"],
  85. }
  86. `)
  87. libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a_source").Output("bindings.rs")
  88. // The rule description should contain the custom binary name rather than bindgen, so checking the description
  89. // should be sufficient.
  90. if !strings.Contains(libbindgen.Description, "my_bindgen") {
  91. t.Errorf("Custom bindgen binary %s not used for libbindgen: rule description %#v", "my_bindgen",
  92. libbindgen.Description)
  93. }
  94. }
  95. func TestRustBindgenStdVersions(t *testing.T) {
  96. testRustError(t, "c_std and cpp_std cannot both be defined at the same time.", `
  97. rust_bindgen {
  98. name: "libbindgen",
  99. wrapper_src: "src/any.h",
  100. crate_name: "bindgen",
  101. stem: "libbindgen",
  102. source_stem: "bindings",
  103. c_std: "somevalue",
  104. cpp_std: "somevalue",
  105. }
  106. `)
  107. ctx := testRust(t, `
  108. rust_bindgen {
  109. name: "libbindgen_cstd",
  110. wrapper_src: "src/any.h",
  111. crate_name: "bindgen",
  112. stem: "libbindgen",
  113. source_stem: "bindings",
  114. c_std: "foo"
  115. }
  116. rust_bindgen {
  117. name: "libbindgen_cppstd",
  118. wrapper_src: "src/any.h",
  119. crate_name: "bindgen",
  120. stem: "libbindgen",
  121. source_stem: "bindings",
  122. cpp_std: "foo"
  123. }
  124. `)
  125. libbindgen_cstd := ctx.ModuleForTests("libbindgen_cstd", "android_arm64_armv8-a_source").Output("bindings.rs")
  126. libbindgen_cppstd := ctx.ModuleForTests("libbindgen_cppstd", "android_arm64_armv8-a_source").Output("bindings.rs")
  127. if !strings.Contains(libbindgen_cstd.Args["cflags"], "-std=foo") {
  128. t.Errorf("c_std value not passed in to rust_bindgen as a clang flag")
  129. }
  130. if !strings.Contains(libbindgen_cppstd.Args["cflags"], "-std=foo") {
  131. t.Errorf("cpp_std value not passed in to rust_bindgen as a clang flag")
  132. }
  133. }
  134. func TestBindgenDisallowedFlags(t *testing.T) {
  135. // Make sure passing '-x c++' to cflags generates an error
  136. testRustError(t, "cflags: -x c\\+\\+ should not be specified in cflags.*", `
  137. rust_bindgen {
  138. name: "libbad_flag",
  139. wrapper_src: "src/any.h",
  140. crate_name: "bindgen",
  141. stem: "libbindgen",
  142. source_stem: "bindings",
  143. cflags: ["-x c++"]
  144. }
  145. `)
  146. // Make sure passing '-std=' to cflags generates an error
  147. testRustError(t, "cflags: -std should not be specified in cflags.*", `
  148. rust_bindgen {
  149. name: "libbad_flag",
  150. wrapper_src: "src/any.h",
  151. crate_name: "bindgen",
  152. stem: "libbindgen",
  153. source_stem: "bindings",
  154. cflags: ["-std=foo"]
  155. }
  156. `)
  157. }