bindgen_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. }
  33. cc_library_shared {
  34. name: "libfoo_shared",
  35. export_include_dirs: ["shared_include"],
  36. }
  37. cc_library_static {
  38. name: "libfoo_static",
  39. export_include_dirs: ["static_include"],
  40. }
  41. cc_defaults {
  42. name: "cc_defaults_flags",
  43. cflags: ["--default-flag"],
  44. }
  45. `)
  46. libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a_source").Output("bindings.rs")
  47. // Ensure that the flags are present and escaped
  48. if !strings.Contains(libbindgen.Args["flags"], "'--bindgen-flag.*'") {
  49. t.Errorf("missing bindgen flags in rust_bindgen rule: flags %#v", libbindgen.Args["flags"])
  50. }
  51. if !strings.Contains(libbindgen.Args["cflags"], "'--clang-flag()'") {
  52. t.Errorf("missing clang cflags in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"])
  53. }
  54. if !strings.Contains(libbindgen.Args["cflags"], "-Ishared_include") {
  55. t.Errorf("missing shared_libs exported includes in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"])
  56. }
  57. if !strings.Contains(libbindgen.Args["cflags"], "-Istatic_include") {
  58. t.Errorf("missing static_libs exported includes in rust_bindgen rule: cflags %#v", libbindgen.Args["cflags"])
  59. }
  60. if !strings.Contains(libbindgen.Args["cflags"], "--default-flag") {
  61. t.Errorf("rust_bindgen missing cflags defined in cc_defaults: cflags %#v", libbindgen.Args["cflags"])
  62. }
  63. }
  64. func TestRustBindgenCustomBindgen(t *testing.T) {
  65. ctx := testRust(t, `
  66. rust_bindgen {
  67. name: "libbindgen",
  68. wrapper_src: "src/any.h",
  69. crate_name: "bindgen",
  70. stem: "libbindgen",
  71. source_stem: "bindings",
  72. custom_bindgen: "my_bindgen"
  73. }
  74. rust_binary_host {
  75. name: "my_bindgen",
  76. srcs: ["foo.rs"],
  77. }
  78. `)
  79. libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a_source").Output("bindings.rs")
  80. // The rule description should contain the custom binary name rather than bindgen, so checking the description
  81. // should be sufficient.
  82. if !strings.Contains(libbindgen.Description, "my_bindgen") {
  83. t.Errorf("Custom bindgen binary %s not used for libbindgen: rule description %#v", "my_bindgen",
  84. libbindgen.Description)
  85. }
  86. }
  87. func TestRustBindgenStdVersions(t *testing.T) {
  88. testRustError(t, "c_std and cpp_std cannot both be defined at the same time.", `
  89. rust_bindgen {
  90. name: "libbindgen",
  91. wrapper_src: "src/any.h",
  92. crate_name: "bindgen",
  93. stem: "libbindgen",
  94. source_stem: "bindings",
  95. c_std: "somevalue",
  96. cpp_std: "somevalue",
  97. }
  98. `)
  99. ctx := testRust(t, `
  100. rust_bindgen {
  101. name: "libbindgen_cstd",
  102. wrapper_src: "src/any.h",
  103. crate_name: "bindgen",
  104. stem: "libbindgen",
  105. source_stem: "bindings",
  106. c_std: "foo"
  107. }
  108. rust_bindgen {
  109. name: "libbindgen_cppstd",
  110. wrapper_src: "src/any.h",
  111. crate_name: "bindgen",
  112. stem: "libbindgen",
  113. source_stem: "bindings",
  114. cpp_std: "foo"
  115. }
  116. `)
  117. libbindgen_cstd := ctx.ModuleForTests("libbindgen_cstd", "android_arm64_armv8-a_source").Output("bindings.rs")
  118. libbindgen_cppstd := ctx.ModuleForTests("libbindgen_cppstd", "android_arm64_armv8-a_source").Output("bindings.rs")
  119. if !strings.Contains(libbindgen_cstd.Args["cflags"], "-std=foo") {
  120. t.Errorf("c_std value not passed in to rust_bindgen as a clang flag")
  121. }
  122. if !strings.Contains(libbindgen_cppstd.Args["cflags"], "-std=foo") {
  123. t.Errorf("cpp_std value not passed in to rust_bindgen as a clang flag")
  124. }
  125. }