Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | "testing" |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 20 | |
| 21 | "android/soong/android" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 24 | // Test that rustlibs default linkage is correct for binaries. |
| 25 | func TestBinaryLinkage(t *testing.T) { |
| 26 | ctx := testRust(t, ` |
| 27 | rust_binary { |
| 28 | name: "fizz-buzz", |
| 29 | srcs: ["foo.rs"], |
| 30 | rustlibs: ["libfoo"], |
| 31 | host_supported: true, |
| 32 | } |
Ivan Lozano | 1120087 | 2020-09-28 11:56:30 -0400 | [diff] [blame] | 33 | rust_binary { |
| 34 | name: "rlib_linked", |
| 35 | srcs: ["foo.rs"], |
| 36 | rustlibs: ["libfoo"], |
| 37 | host_supported: true, |
| 38 | prefer_rlib: true, |
| 39 | } |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 40 | rust_library { |
| 41 | name: "libfoo", |
| 42 | srcs: ["foo.rs"], |
| 43 | crate_name: "foo", |
| 44 | host_supported: true, |
| 45 | }`) |
| 46 | |
| 47 | fizzBuzzHost := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module) |
| 48 | fizzBuzzDevice := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module) |
| 49 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 50 | if !android.InList("libfoo.rlib-std", fizzBuzzHost.Properties.AndroidMkRlibs) { |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 51 | t.Errorf("rustlibs dependency libfoo should be an rlib dep for host modules") |
| 52 | } |
| 53 | |
| 54 | if !android.InList("libfoo", fizzBuzzDevice.Properties.AndroidMkDylibs) { |
| 55 | t.Errorf("rustlibs dependency libfoo should be an dylib dep for device modules") |
| 56 | } |
| 57 | } |
| 58 | |
Ivan Lozano | 1120087 | 2020-09-28 11:56:30 -0400 | [diff] [blame] | 59 | // Test that prefer_rlib links in libstd statically as well as rustlibs. |
| 60 | func TestBinaryPreferRlib(t *testing.T) { |
| 61 | ctx := testRust(t, ` |
| 62 | rust_binary { |
| 63 | name: "rlib_linked", |
| 64 | srcs: ["foo.rs"], |
| 65 | rustlibs: ["libfoo"], |
| 66 | host_supported: true, |
| 67 | prefer_rlib: true, |
| 68 | } |
| 69 | rust_library { |
| 70 | name: "libfoo", |
| 71 | srcs: ["foo.rs"], |
| 72 | crate_name: "foo", |
| 73 | host_supported: true, |
| 74 | }`) |
| 75 | |
| 76 | mod := ctx.ModuleForTests("rlib_linked", "android_arm64_armv8-a").Module().(*Module) |
| 77 | |
| 78 | if !android.InList("libfoo.rlib-std", mod.Properties.AndroidMkRlibs) { |
| 79 | t.Errorf("rustlibs dependency libfoo should be an rlib dep when prefer_rlib is defined") |
| 80 | } |
| 81 | |
| 82 | if !android.InList("libstd", mod.Properties.AndroidMkRlibs) { |
| 83 | t.Errorf("libstd dependency should be an rlib dep when prefer_rlib is defined") |
| 84 | } |
| 85 | } |
| 86 | |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 87 | // Test that the path returned by HostToolPath is correct |
| 88 | func TestHostToolPath(t *testing.T) { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 89 | ctx := testRust(t, ` |
| 90 | rust_binary_host { |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 91 | name: "fizz-buzz", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 92 | srcs: ["foo.rs"], |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 93 | }`) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 94 | |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 95 | path := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module).HostToolPath() |
| 96 | if g, w := path.String(), "/host/linux-x86/bin/fizz-buzz"; !strings.Contains(g, w) { |
| 97 | t.Errorf("wrong host tool path, expected %q got %q", w, g) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Test that the flags being passed to rust_binary modules are as expected |
| 102 | func TestBinaryFlags(t *testing.T) { |
| 103 | ctx := testRust(t, ` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 104 | rust_binary_host { |
| 105 | name: "fizz-buzz", |
| 106 | srcs: ["foo.rs"], |
| 107 | }`) |
| 108 | |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 109 | fizzBuzz := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Rule("rustc") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 110 | |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 111 | flags := fizzBuzz.Args["rustcFlags"] |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 112 | if strings.Contains(flags, "--test") { |
| 113 | t.Errorf("extra --test flag, rustcFlags: %#v", flags) |
| 114 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 115 | } |
Ivan Lozano | 2093af2 | 2020-08-25 12:48:19 -0400 | [diff] [blame] | 116 | |
Ivan Lozano | a226863 | 2021-07-22 10:52:06 -0400 | [diff] [blame] | 117 | // Test that the bootstrap property sets the appropriate linker |
| 118 | func TestBootstrap(t *testing.T) { |
| 119 | ctx := testRust(t, ` |
| 120 | rust_binary { |
| 121 | name: "foo", |
| 122 | srcs: ["foo.rs"], |
| 123 | bootstrap: true, |
| 124 | }`) |
| 125 | |
| 126 | foo := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Rule("rustc") |
| 127 | |
| 128 | flag := "-Wl,-dynamic-linker,/system/bin/bootstrap/linker64" |
| 129 | if !strings.Contains(foo.Args["linkFlags"], flag) { |
| 130 | t.Errorf("missing link flag to use bootstrap linker, expecting %#v, linkFlags: %#v", flag, foo.Args["linkFlags"]) |
| 131 | } |
| 132 | } |
| 133 | |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 134 | func TestStaticBinaryFlags(t *testing.T) { |
| 135 | ctx := testRust(t, ` |
| 136 | rust_binary { |
| 137 | name: "fizz", |
| 138 | srcs: ["foo.rs"], |
| 139 | static_executable: true, |
| 140 | }`) |
| 141 | |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 142 | fizzOut := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Rule("rustc") |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 143 | fizzMod := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Module().(*Module) |
| 144 | |
| 145 | flags := fizzOut.Args["rustcFlags"] |
| 146 | linkFlags := fizzOut.Args["linkFlags"] |
| 147 | if !strings.Contains(flags, "-C relocation-model=static") { |
| 148 | t.Errorf("static binary missing '-C relocation-model=static' in rustcFlags, found: %#v", flags) |
| 149 | } |
Jeff Vander Stoep | bf7a902 | 2021-01-26 14:35:38 +0100 | [diff] [blame] | 150 | if !strings.Contains(flags, "-C panic=abort") { |
| 151 | t.Errorf("static binary missing '-C panic=abort' in rustcFlags, found: %#v", flags) |
| 152 | } |
Ivan Lozano | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame] | 153 | if !strings.Contains(linkFlags, "-static") { |
| 154 | t.Errorf("static binary missing '-static' in linkFlags, found: %#v", flags) |
| 155 | } |
| 156 | |
| 157 | if !android.InList("libc", fizzMod.Properties.AndroidMkStaticLibs) { |
| 158 | t.Errorf("static binary not linking against libc as a static library") |
| 159 | } |
| 160 | if len(fizzMod.Properties.AndroidMkSharedLibs) > 0 { |
| 161 | t.Errorf("static binary incorrectly linking against shared libraries") |
| 162 | } |
| 163 | } |
| 164 | |
Ivan Lozano | 2093af2 | 2020-08-25 12:48:19 -0400 | [diff] [blame] | 165 | func TestLinkObjects(t *testing.T) { |
| 166 | ctx := testRust(t, ` |
| 167 | rust_binary { |
| 168 | name: "fizz-buzz", |
| 169 | srcs: ["foo.rs"], |
| 170 | shared_libs: ["libfoo"], |
| 171 | } |
| 172 | cc_library { |
| 173 | name: "libfoo", |
| 174 | }`) |
| 175 | |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 176 | fizzBuzz := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Rule("rustc") |
Ivan Lozano | 2093af2 | 2020-08-25 12:48:19 -0400 | [diff] [blame] | 177 | linkFlags := fizzBuzz.Args["linkFlags"] |
| 178 | if !strings.Contains(linkFlags, "/libfoo.so") { |
| 179 | t.Errorf("missing shared dependency 'libfoo.so' in linkFlags: %#v", linkFlags) |
| 180 | } |
| 181 | } |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 182 | |
| 183 | // Test that stripped versions are correctly generated and used. |
| 184 | func TestStrippedBinary(t *testing.T) { |
| 185 | ctx := testRust(t, ` |
| 186 | rust_binary { |
| 187 | name: "foo", |
| 188 | srcs: ["foo.rs"], |
| 189 | } |
| 190 | rust_binary { |
| 191 | name: "bar", |
| 192 | srcs: ["foo.rs"], |
| 193 | strip: { |
| 194 | none: true |
| 195 | } |
| 196 | } |
| 197 | `) |
| 198 | |
| 199 | foo := ctx.ModuleForTests("foo", "android_arm64_armv8-a") |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 200 | foo.Output("unstripped/foo") |
| 201 | foo.Output("foo") |
| 202 | |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 203 | // Check that the `cp` rules is using the stripped version as input. |
| 204 | cp := foo.Rule("android.Cp") |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 205 | if strings.HasSuffix(cp.Input.String(), "unstripped/foo") { |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 206 | t.Errorf("installed binary not based on stripped version: %v", cp.Input) |
| 207 | } |
| 208 | |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 209 | fizzBar := ctx.ModuleForTests("bar", "android_arm64_armv8-a").MaybeOutput("unstripped/bar") |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 210 | if fizzBar.Rule != nil { |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 211 | t.Errorf("unstripped binary exists, so stripped binary has incorrectly been generated") |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 212 | } |
| 213 | } |