compiler_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2019 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. "android/soong/android"
  19. )
  20. // Test that feature flags are being correctly generated.
  21. func TestFeaturesToFlags(t *testing.T) {
  22. ctx := testRust(t, `
  23. rust_library_host_dylib {
  24. name: "libfoo",
  25. srcs: ["foo.rs"],
  26. crate_name: "foo",
  27. features: [
  28. "fizz",
  29. "buzz"
  30. ],
  31. }`)
  32. libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Rule("rustc")
  33. if !strings.Contains(libfooDylib.Args["rustcFlags"], "cfg 'feature=\"fizz\"'") ||
  34. !strings.Contains(libfooDylib.Args["rustcFlags"], "cfg 'feature=\"buzz\"'") {
  35. t.Fatalf("missing fizz and buzz feature flags for libfoo dylib, rustcFlags: %#v", libfooDylib.Args["rustcFlags"])
  36. }
  37. }
  38. // Test that we reject multiple source files.
  39. func TestEnforceSingleSourceFile(t *testing.T) {
  40. singleSrcError := "srcs can only contain one path for a rust file and source providers prefixed by \":\""
  41. // Test libraries
  42. testRustError(t, singleSrcError, `
  43. rust_library_host {
  44. name: "foo-bar-library",
  45. srcs: ["foo.rs", "src/bar.rs"],
  46. }`)
  47. // Test binaries
  48. testRustError(t, singleSrcError, `
  49. rust_binary_host {
  50. name: "foo-bar-binary",
  51. srcs: ["foo.rs", "src/bar.rs"],
  52. }`)
  53. // Test proc_macros
  54. testRustError(t, singleSrcError, `
  55. rust_proc_macro {
  56. name: "foo-bar-proc-macro",
  57. srcs: ["foo.rs", "src/bar.rs"],
  58. }`)
  59. // Test prebuilts
  60. testRustError(t, singleSrcError, `
  61. rust_prebuilt_dylib {
  62. name: "foo-bar-prebuilt",
  63. srcs: ["liby.so", "libz.so"],
  64. host_supported: true,
  65. }`)
  66. }
  67. func TestInstallDir(t *testing.T) {
  68. ctx := testRust(t, `
  69. rust_library_dylib {
  70. name: "libfoo",
  71. srcs: ["foo.rs"],
  72. crate_name: "foo",
  73. }
  74. rust_binary {
  75. name: "fizzbuzz",
  76. srcs: ["foo.rs"],
  77. }`)
  78. install_path_lib64 := ctx.ModuleForTests("libfoo",
  79. "android_arm64_armv8-a_dylib").Module().(*Module).compiler.(*libraryDecorator).path.String()
  80. install_path_lib32 := ctx.ModuleForTests("libfoo",
  81. "android_arm_armv7-a-neon_dylib").Module().(*Module).compiler.(*libraryDecorator).path.String()
  82. install_path_bin := ctx.ModuleForTests("fizzbuzz",
  83. "android_arm64_armv8-a").Module().(*Module).compiler.(*binaryDecorator).path.String()
  84. if !strings.HasSuffix(install_path_lib64, "system/lib64/libfoo.dylib.so") {
  85. t.Fatalf("unexpected install path for 64-bit library: %#v", install_path_lib64)
  86. }
  87. if !strings.HasSuffix(install_path_lib32, "system/lib/libfoo.dylib.so") {
  88. t.Fatalf("unexpected install path for 32-bit library: %#v", install_path_lib32)
  89. }
  90. if !strings.HasSuffix(install_path_bin, "system/bin/fizzbuzz") {
  91. t.Fatalf("unexpected install path for binary: %#v", install_path_bin)
  92. }
  93. }
  94. func TestLints(t *testing.T) {
  95. bp := `
  96. // foo uses the default value of lints
  97. rust_library {
  98. name: "libfoo",
  99. srcs: ["foo.rs"],
  100. crate_name: "foo",
  101. }
  102. // bar forces the use of the "android" lint set
  103. rust_library {
  104. name: "libbar",
  105. srcs: ["foo.rs"],
  106. crate_name: "bar",
  107. lints: "android",
  108. }
  109. // foobar explicitly disable all lints
  110. rust_library {
  111. name: "libfoobar",
  112. srcs: ["foo.rs"],
  113. crate_name: "foobar",
  114. lints: "none",
  115. }`
  116. var lintTests = []struct {
  117. modulePath string
  118. fooFlags string
  119. }{
  120. {"", "${config.RustDefaultLints}"},
  121. {"external/", "${config.RustAllowAllLints}"},
  122. {"hardware/", "${config.RustVendorLints}"},
  123. }
  124. for _, tc := range lintTests {
  125. t.Run("path="+tc.modulePath, func(t *testing.T) {
  126. result := android.GroupFixturePreparers(
  127. prepareForRustTest,
  128. // Test with the blueprint file in different directories.
  129. android.FixtureAddTextFile(tc.modulePath+"Android.bp", bp),
  130. ).RunTest(t)
  131. r := result.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").MaybeRule("rustc")
  132. android.AssertStringDoesContain(t, "libfoo flags", r.Args["rustcFlags"], tc.fooFlags)
  133. r = result.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeRule("rustc")
  134. android.AssertStringDoesContain(t, "libbar flags", r.Args["rustcFlags"], "${config.RustDefaultLints}")
  135. r = result.ModuleForTests("libfoobar", "android_arm64_armv8-a_dylib").MaybeRule("rustc")
  136. android.AssertStringDoesContain(t, "libfoobar flags", r.Args["rustcFlags"], "${config.RustAllowAllLints}")
  137. })
  138. }
  139. }
  140. // Test that devices are linking the stdlib dynamically
  141. func TestStdDeviceLinkage(t *testing.T) {
  142. ctx := testRust(t, `
  143. rust_binary {
  144. name: "fizz",
  145. srcs: ["foo.rs"],
  146. }
  147. rust_library {
  148. name: "libfoo",
  149. srcs: ["foo.rs"],
  150. crate_name: "foo",
  151. }`)
  152. fizz := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Module().(*Module)
  153. fooRlib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Module().(*Module)
  154. fooDylib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
  155. if !android.InList("libstd", fizz.Properties.AndroidMkDylibs) {
  156. t.Errorf("libstd is not linked dynamically for device binaries")
  157. }
  158. if !android.InList("libstd", fooRlib.Properties.AndroidMkDylibs) {
  159. t.Errorf("libstd is not linked dynamically for rlibs")
  160. }
  161. if !android.InList("libstd", fooDylib.Properties.AndroidMkDylibs) {
  162. t.Errorf("libstd is not linked dynamically for dylibs")
  163. }
  164. }