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