binary_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 rustlibs default linkage is correct for binaries.
  21. func TestBinaryLinkage(t *testing.T) {
  22. ctx := testRust(t, `
  23. rust_binary {
  24. name: "fizz-buzz",
  25. srcs: ["foo.rs"],
  26. rustlibs: ["libfoo"],
  27. host_supported: true,
  28. }
  29. rust_binary {
  30. name: "rlib_linked",
  31. srcs: ["foo.rs"],
  32. rustlibs: ["libfoo"],
  33. host_supported: true,
  34. prefer_rlib: true,
  35. }
  36. rust_library {
  37. name: "libfoo",
  38. srcs: ["foo.rs"],
  39. crate_name: "foo",
  40. host_supported: true,
  41. }`)
  42. fizzBuzzHost := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
  43. fizzBuzzDevice := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module)
  44. if !android.InList("libfoo.rlib-std", fizzBuzzHost.Properties.AndroidMkRlibs) {
  45. t.Errorf("rustlibs dependency libfoo should be an rlib dep for host modules")
  46. }
  47. if !android.InList("libfoo", fizzBuzzDevice.Properties.AndroidMkDylibs) {
  48. t.Errorf("rustlibs dependency libfoo should be an dylib dep for device modules")
  49. }
  50. }
  51. // Test that prefer_rlib links in libstd statically as well as rustlibs.
  52. func TestBinaryPreferRlib(t *testing.T) {
  53. ctx := testRust(t, `
  54. rust_binary {
  55. name: "rlib_linked",
  56. srcs: ["foo.rs"],
  57. rustlibs: ["libfoo"],
  58. host_supported: true,
  59. prefer_rlib: true,
  60. }
  61. rust_library {
  62. name: "libfoo",
  63. srcs: ["foo.rs"],
  64. crate_name: "foo",
  65. host_supported: true,
  66. }`)
  67. mod := ctx.ModuleForTests("rlib_linked", "android_arm64_armv8-a").Module().(*Module)
  68. if !android.InList("libfoo.rlib-std", mod.Properties.AndroidMkRlibs) {
  69. t.Errorf("rustlibs dependency libfoo should be an rlib dep when prefer_rlib is defined")
  70. }
  71. if !android.InList("libstd", mod.Properties.AndroidMkRlibs) {
  72. t.Errorf("libstd dependency should be an rlib dep when prefer_rlib is defined")
  73. }
  74. }
  75. // Test that the path returned by HostToolPath is correct
  76. func TestHostToolPath(t *testing.T) {
  77. ctx := testRust(t, `
  78. rust_binary_host {
  79. name: "fizz-buzz",
  80. srcs: ["foo.rs"],
  81. }`)
  82. path := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module).HostToolPath()
  83. if g, w := path.String(), "/host/linux-x86/bin/fizz-buzz"; !strings.Contains(g, w) {
  84. t.Errorf("wrong host tool path, expected %q got %q", w, g)
  85. }
  86. }
  87. // Test that the flags being passed to rust_binary modules are as expected
  88. func TestBinaryFlags(t *testing.T) {
  89. ctx := testRust(t, `
  90. rust_binary_host {
  91. name: "fizz-buzz",
  92. srcs: ["foo.rs"],
  93. }`)
  94. fizzBuzz := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Output("fizz-buzz")
  95. flags := fizzBuzz.Args["rustcFlags"]
  96. if strings.Contains(flags, "--test") {
  97. t.Errorf("extra --test flag, rustcFlags: %#v", flags)
  98. }
  99. }
  100. func TestStaticBinaryFlags(t *testing.T) {
  101. ctx := testRust(t, `
  102. rust_binary {
  103. name: "fizz",
  104. srcs: ["foo.rs"],
  105. static_executable: true,
  106. }`)
  107. fizzOut := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Output("fizz")
  108. fizzMod := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Module().(*Module)
  109. flags := fizzOut.Args["rustcFlags"]
  110. linkFlags := fizzOut.Args["linkFlags"]
  111. if !strings.Contains(flags, "-C relocation-model=static") {
  112. t.Errorf("static binary missing '-C relocation-model=static' in rustcFlags, found: %#v", flags)
  113. }
  114. if !strings.Contains(linkFlags, "-static") {
  115. t.Errorf("static binary missing '-static' in linkFlags, found: %#v", flags)
  116. }
  117. if !android.InList("libc", fizzMod.Properties.AndroidMkStaticLibs) {
  118. t.Errorf("static binary not linking against libc as a static library")
  119. }
  120. if len(fizzMod.Properties.AndroidMkSharedLibs) > 0 {
  121. t.Errorf("static binary incorrectly linking against shared libraries")
  122. }
  123. }
  124. func TestLinkObjects(t *testing.T) {
  125. ctx := testRust(t, `
  126. rust_binary {
  127. name: "fizz-buzz",
  128. srcs: ["foo.rs"],
  129. shared_libs: ["libfoo"],
  130. }
  131. cc_library {
  132. name: "libfoo",
  133. }`)
  134. fizzBuzz := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Output("fizz-buzz")
  135. linkFlags := fizzBuzz.Args["linkFlags"]
  136. if !strings.Contains(linkFlags, "/libfoo.so") {
  137. t.Errorf("missing shared dependency 'libfoo.so' in linkFlags: %#v", linkFlags)
  138. }
  139. }
  140. // Test that stripped versions are correctly generated and used.
  141. func TestStrippedBinary(t *testing.T) {
  142. ctx := testRust(t, `
  143. rust_binary {
  144. name: "foo",
  145. srcs: ["foo.rs"],
  146. }
  147. rust_binary {
  148. name: "bar",
  149. srcs: ["foo.rs"],
  150. strip: {
  151. none: true
  152. }
  153. }
  154. `)
  155. foo := ctx.ModuleForTests("foo", "android_arm64_armv8-a")
  156. foo.Output("stripped/foo")
  157. // Check that the `cp` rules is using the stripped version as input.
  158. cp := foo.Rule("android.Cp")
  159. if !strings.HasSuffix(cp.Input.String(), "stripped/foo") {
  160. t.Errorf("installed binary not based on stripped version: %v", cp.Input)
  161. }
  162. fizzBar := ctx.ModuleForTests("bar", "android_arm64_armv8-a").MaybeOutput("stripped/bar")
  163. if fizzBar.Rule != nil {
  164. t.Errorf("stripped version of bar has been generated")
  165. }
  166. }