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