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 | |
| 109 | fizzBuzz := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Output("fizz-buzz") |
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 | bf63d00 | 2020-10-02 10:03:23 -0400 | [diff] [blame^] | 117 | func TestStaticBinaryFlags(t *testing.T) { |
| 118 | ctx := testRust(t, ` |
| 119 | rust_binary { |
| 120 | name: "fizz", |
| 121 | srcs: ["foo.rs"], |
| 122 | static_executable: true, |
| 123 | }`) |
| 124 | |
| 125 | fizzOut := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Output("fizz") |
| 126 | fizzMod := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Module().(*Module) |
| 127 | |
| 128 | flags := fizzOut.Args["rustcFlags"] |
| 129 | linkFlags := fizzOut.Args["linkFlags"] |
| 130 | if !strings.Contains(flags, "-C relocation-model=static") { |
| 131 | t.Errorf("static binary missing '-C relocation-model=static' in rustcFlags, found: %#v", flags) |
| 132 | } |
| 133 | if !strings.Contains(linkFlags, "-static") { |
| 134 | t.Errorf("static binary missing '-static' in linkFlags, found: %#v", flags) |
| 135 | } |
| 136 | |
| 137 | if !android.InList("libc", fizzMod.Properties.AndroidMkStaticLibs) { |
| 138 | t.Errorf("static binary not linking against libc as a static library") |
| 139 | } |
| 140 | if len(fizzMod.Properties.AndroidMkSharedLibs) > 0 { |
| 141 | t.Errorf("static binary incorrectly linking against shared libraries") |
| 142 | } |
| 143 | } |
| 144 | |
Ivan Lozano | 2093af2 | 2020-08-25 12:48:19 -0400 | [diff] [blame] | 145 | func TestLinkObjects(t *testing.T) { |
| 146 | ctx := testRust(t, ` |
| 147 | rust_binary { |
| 148 | name: "fizz-buzz", |
| 149 | srcs: ["foo.rs"], |
| 150 | shared_libs: ["libfoo"], |
| 151 | } |
| 152 | cc_library { |
| 153 | name: "libfoo", |
| 154 | }`) |
| 155 | |
| 156 | fizzBuzz := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Output("fizz-buzz") |
| 157 | linkFlags := fizzBuzz.Args["linkFlags"] |
| 158 | if !strings.Contains(linkFlags, "/libfoo.so") { |
| 159 | t.Errorf("missing shared dependency 'libfoo.so' in linkFlags: %#v", linkFlags) |
| 160 | } |
| 161 | } |
ThiƩbaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 162 | |
| 163 | // Test that stripped versions are correctly generated and used. |
| 164 | func TestStrippedBinary(t *testing.T) { |
| 165 | ctx := testRust(t, ` |
| 166 | rust_binary { |
| 167 | name: "foo", |
| 168 | srcs: ["foo.rs"], |
| 169 | } |
| 170 | rust_binary { |
| 171 | name: "bar", |
| 172 | srcs: ["foo.rs"], |
| 173 | strip: { |
| 174 | none: true |
| 175 | } |
| 176 | } |
| 177 | `) |
| 178 | |
| 179 | foo := ctx.ModuleForTests("foo", "android_arm64_armv8-a") |
| 180 | foo.Output("stripped/foo") |
| 181 | // Check that the `cp` rules is using the stripped version as input. |
| 182 | cp := foo.Rule("android.Cp") |
| 183 | if !strings.HasSuffix(cp.Input.String(), "stripped/foo") { |
| 184 | t.Errorf("installed binary not based on stripped version: %v", cp.Input) |
| 185 | } |
| 186 | |
| 187 | fizzBar := ctx.ModuleForTests("bar", "android_arm64_armv8-a").MaybeOutput("stripped/bar") |
| 188 | if fizzBar.Rule != nil { |
| 189 | t.Errorf("stripped version of bar has been generated") |
| 190 | } |
| 191 | } |