compiler_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. bp = bp + GatherRequiredDepsForTest()
  117. fs := map[string][]byte{
  118. // Reuse the same blueprint file for subdirectories.
  119. "external/Android.bp": []byte(bp),
  120. "hardware/Android.bp": []byte(bp),
  121. }
  122. var lintTests = []struct {
  123. modulePath string
  124. fooFlags string
  125. }{
  126. {"", "${config.RustDefaultLints}"},
  127. {"external/", "${config.RustAllowAllLints}"},
  128. {"hardware/", "${config.RustVendorLints}"},
  129. }
  130. for _, tc := range lintTests {
  131. t.Run("path="+tc.modulePath, func(t *testing.T) {
  132. config := android.TestArchConfig(buildDir, nil, bp, fs)
  133. ctx := CreateTestContext()
  134. ctx.Register(config)
  135. _, errs := ctx.ParseFileList(".", []string{tc.modulePath + "Android.bp"})
  136. android.FailIfErrored(t, errs)
  137. _, errs = ctx.PrepareBuildActions(config)
  138. android.FailIfErrored(t, errs)
  139. r := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").MaybeRule("rustc")
  140. if !strings.Contains(r.Args["rustcFlags"], tc.fooFlags) {
  141. t.Errorf("Incorrect flags for libfoo: %q, want %q", r.Args["rustcFlags"], tc.fooFlags)
  142. }
  143. r = ctx.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeRule("rustc")
  144. if !strings.Contains(r.Args["rustcFlags"], "${config.RustDefaultLints}") {
  145. t.Errorf("Incorrect flags for libbar: %q, want %q", r.Args["rustcFlags"], "${config.RustDefaultLints}")
  146. }
  147. r = ctx.ModuleForTests("libfoobar", "android_arm64_armv8-a_dylib").MaybeRule("rustc")
  148. if !strings.Contains(r.Args["rustcFlags"], "${config.RustAllowAllLints}") {
  149. t.Errorf("Incorrect flags for libfoobar: %q, want %q", r.Args["rustcFlags"], "${config.RustAllowAllLints}")
  150. }
  151. })
  152. }
  153. }
  154. // Test that devices are linking the stdlib dynamically
  155. func TestStdDeviceLinkage(t *testing.T) {
  156. ctx := testRust(t, `
  157. rust_binary {
  158. name: "fizz",
  159. srcs: ["foo.rs"],
  160. }
  161. rust_library {
  162. name: "libfoo",
  163. srcs: ["foo.rs"],
  164. crate_name: "foo",
  165. }`)
  166. fizz := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Module().(*Module)
  167. fooRlib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Module().(*Module)
  168. fooDylib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
  169. if !android.InList("libstd", fizz.Properties.AndroidMkDylibs) {
  170. t.Errorf("libstd is not linked dynamically for device binaries")
  171. }
  172. if !android.InList("libstd", fooRlib.Properties.AndroidMkDylibs) {
  173. t.Errorf("libstd is not linked dynamically for rlibs")
  174. }
  175. if !android.InList("libstd", fooDylib.Properties.AndroidMkDylibs) {
  176. t.Errorf("libstd is not linked dynamically for dylibs")
  177. }
  178. }