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 | 7e741cc | 2020-06-19 12:32:30 -0400 | [diff] [blame] | 20 | |
| 21 | "android/soong/android" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | // Test that variants are being generated correctly, and that crate-types are correct. |
| 25 | func TestLibraryVariants(t *testing.T) { |
| 26 | |
| 27 | ctx := testRust(t, ` |
| 28 | rust_library_host { |
| 29 | name: "libfoo", |
| 30 | srcs: ["foo.rs"], |
| 31 | crate_name: "foo", |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 32 | } |
| 33 | rust_ffi_host { |
| 34 | name: "libfoo.ffi", |
| 35 | srcs: ["foo.rs"], |
| 36 | crate_name: "foo" |
| 37 | }`) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 38 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 39 | // Test all variants are being built. |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 40 | libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib_rlib-std").Output("libfoo.rlib") |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 41 | libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Output("libfoo.dylib.so") |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 42 | libfooStatic := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_static").Output("libfoo.ffi.a") |
| 43 | libfooShared := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_shared").Output("libfoo.ffi.so") |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 44 | |
| 45 | rlibCrateType := "rlib" |
| 46 | dylibCrateType := "dylib" |
| 47 | sharedCrateType := "cdylib" |
| 48 | staticCrateType := "static" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 49 | |
| 50 | // Test crate type for rlib is correct. |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 51 | if !strings.Contains(libfooRlib.Args["rustcFlags"], "crate-type="+rlibCrateType) { |
| 52 | t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", rlibCrateType, libfooRlib.Args["rustcFlags"]) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // Test crate type for dylib is correct. |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 56 | if !strings.Contains(libfooDylib.Args["rustcFlags"], "crate-type="+dylibCrateType) { |
| 57 | t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", dylibCrateType, libfooDylib.Args["rustcFlags"]) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 58 | } |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 59 | |
| 60 | // Test crate type for C static libraries is correct. |
| 61 | if !strings.Contains(libfooStatic.Args["rustcFlags"], "crate-type="+staticCrateType) { |
| 62 | t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", staticCrateType, libfooStatic.Args["rustcFlags"]) |
| 63 | } |
| 64 | |
| 65 | // Test crate type for C shared libraries is correct. |
| 66 | if !strings.Contains(libfooShared.Args["rustcFlags"], "crate-type="+sharedCrateType) { |
| 67 | t.Errorf("missing crate-type for shared variant, expecting %#v, got rustcFlags: %#v", sharedCrateType, libfooShared.Args["rustcFlags"]) |
| 68 | } |
| 69 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // Test that dylibs are not statically linking the standard library. |
| 73 | func TestDylibPreferDynamic(t *testing.T) { |
| 74 | ctx := testRust(t, ` |
| 75 | rust_library_host_dylib { |
| 76 | name: "libfoo", |
| 77 | srcs: ["foo.rs"], |
| 78 | crate_name: "foo", |
| 79 | }`) |
| 80 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 81 | libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Output("libfoo.dylib.so") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 82 | |
| 83 | if !strings.Contains(libfooDylib.Args["rustcFlags"], "prefer-dynamic") { |
| 84 | t.Errorf("missing prefer-dynamic flag for libfoo dylib, rustcFlags: %#v", libfooDylib.Args["rustcFlags"]) |
| 85 | } |
| 86 | } |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 87 | |
Stephen Crane | 0dbfc56 | 2021-07-07 19:05:02 -0700 | [diff] [blame^] | 88 | // Check that we are passing the android_dylib config flag |
| 89 | func TestAndroidDylib(t *testing.T) { |
| 90 | ctx := testRust(t, ` |
| 91 | rust_library_host_dylib { |
| 92 | name: "libfoo", |
| 93 | srcs: ["foo.rs"], |
| 94 | crate_name: "foo", |
| 95 | }`) |
| 96 | |
| 97 | libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Output("libfoo.dylib.so") |
| 98 | |
| 99 | if !strings.Contains(libfooDylib.Args["rustcFlags"], "--cfg 'android_dylib'") { |
| 100 | t.Errorf("missing android_dylib cfg flag for libfoo dylib, rustcFlags: %#v", libfooDylib.Args["rustcFlags"]) |
| 101 | } |
| 102 | } |
| 103 | |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 104 | func TestValidateLibraryStem(t *testing.T) { |
| 105 | testRustError(t, "crate_name must be defined.", ` |
| 106 | rust_library_host { |
| 107 | name: "libfoo", |
| 108 | srcs: ["foo.rs"], |
| 109 | }`) |
| 110 | |
| 111 | testRustError(t, "library crate_names must be alphanumeric with underscores allowed", ` |
| 112 | rust_library_host { |
| 113 | name: "libfoo-bar", |
| 114 | srcs: ["foo.rs"], |
| 115 | crate_name: "foo-bar" |
| 116 | }`) |
| 117 | |
| 118 | testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", ` |
| 119 | rust_library_host { |
| 120 | name: "foobar", |
| 121 | srcs: ["foo.rs"], |
| 122 | crate_name: "foo_bar" |
| 123 | }`) |
| 124 | testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", ` |
| 125 | rust_library_host { |
| 126 | name: "foobar", |
| 127 | stem: "libfoo", |
| 128 | srcs: ["foo.rs"], |
| 129 | crate_name: "foo_bar" |
| 130 | }`) |
| 131 | testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", ` |
| 132 | rust_library_host { |
| 133 | name: "foobar", |
| 134 | stem: "foo_bar", |
| 135 | srcs: ["foo.rs"], |
| 136 | crate_name: "foo_bar" |
| 137 | }`) |
| 138 | |
| 139 | } |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 140 | |
Ivan Lozano | 7e741cc | 2020-06-19 12:32:30 -0400 | [diff] [blame] | 141 | func TestSharedLibrary(t *testing.T) { |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 142 | ctx := testRust(t, ` |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 143 | rust_ffi_shared { |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 144 | name: "libfoo", |
| 145 | srcs: ["foo.rs"], |
| 146 | crate_name: "foo", |
| 147 | }`) |
| 148 | |
Ivan Lozano | 7e741cc | 2020-06-19 12:32:30 -0400 | [diff] [blame] | 149 | libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared") |
| 150 | |
| 151 | libfooOutput := libfoo.Output("libfoo.so") |
| 152 | if !strings.Contains(libfooOutput.Args["linkFlags"], "-Wl,-soname=libfoo.so") { |
| 153 | t.Errorf("missing expected -Wl,-soname linker flag for libfoo shared lib, linkFlags: %#v", |
| 154 | libfooOutput.Args["linkFlags"]) |
| 155 | } |
| 156 | |
| 157 | if !android.InList("libstd", libfoo.Module().(*Module).Properties.AndroidMkDylibs) { |
| 158 | t.Errorf("Non-static libstd dylib expected to be a dependency of Rust shared libraries. Dylib deps are: %#v", |
| 159 | libfoo.Module().(*Module).Properties.AndroidMkDylibs) |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 160 | } |
| 161 | } |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 162 | |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 163 | func TestStaticLibraryLinkage(t *testing.T) { |
| 164 | ctx := testRust(t, ` |
| 165 | rust_ffi_static { |
| 166 | name: "libfoo", |
| 167 | srcs: ["foo.rs"], |
| 168 | crate_name: "foo", |
| 169 | }`) |
| 170 | |
| 171 | libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static") |
| 172 | |
| 173 | if !android.InList("libstd", libfoo.Module().(*Module).Properties.AndroidMkRlibs) { |
| 174 | t.Errorf("Static libstd rlib expected to be a dependency of Rust static libraries. Rlib deps are: %#v", |
| 175 | libfoo.Module().(*Module).Properties.AndroidMkDylibs) |
| 176 | } |
| 177 | } |
| 178 | |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 179 | // Test that variants pull in the right type of rustlib autodep |
| 180 | func TestAutoDeps(t *testing.T) { |
| 181 | |
| 182 | ctx := testRust(t, ` |
| 183 | rust_library_host { |
| 184 | name: "libbar", |
| 185 | srcs: ["bar.rs"], |
| 186 | crate_name: "bar", |
| 187 | } |
| 188 | rust_library_host { |
| 189 | name: "libfoo", |
| 190 | srcs: ["foo.rs"], |
| 191 | crate_name: "foo", |
| 192 | rustlibs: ["libbar"], |
| 193 | } |
| 194 | rust_ffi_host { |
| 195 | name: "libfoo.ffi", |
| 196 | srcs: ["foo.rs"], |
| 197 | crate_name: "foo", |
| 198 | rustlibs: ["libbar"], |
| 199 | }`) |
| 200 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 201 | libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib_rlib-std") |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 202 | libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib") |
| 203 | libfooStatic := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_static") |
| 204 | libfooShared := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_shared") |
| 205 | |
| 206 | for _, static := range []android.TestingModule{libfooRlib, libfooStatic} { |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 207 | if !android.InList("libbar.rlib-std", static.Module().(*Module).Properties.AndroidMkRlibs) { |
| 208 | t.Errorf("libbar not present as rlib dependency in static lib") |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 209 | } |
| 210 | if android.InList("libbar", static.Module().(*Module).Properties.AndroidMkDylibs) { |
| 211 | t.Errorf("libbar present as dynamic dependency in static lib") |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | for _, dyn := range []android.TestingModule{libfooDylib, libfooShared} { |
| 216 | if !android.InList("libbar", dyn.Module().(*Module).Properties.AndroidMkDylibs) { |
| 217 | t.Errorf("libbar not present as dynamic dependency in dynamic lib") |
| 218 | } |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 219 | if android.InList("libbar.dylib-std", dyn.Module().(*Module).Properties.AndroidMkRlibs) { |
| 220 | t.Errorf("libbar present as rlib dependency in dynamic lib") |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | } |
| 224 | } |
ThiƩbaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 225 | |
| 226 | // Test that stripped versions are correctly generated and used. |
| 227 | func TestStrippedLibrary(t *testing.T) { |
| 228 | ctx := testRust(t, ` |
| 229 | rust_library_dylib { |
| 230 | name: "libfoo", |
| 231 | crate_name: "foo", |
| 232 | srcs: ["foo.rs"], |
| 233 | } |
| 234 | rust_library_dylib { |
| 235 | name: "libbar", |
| 236 | crate_name: "bar", |
| 237 | srcs: ["foo.rs"], |
| 238 | strip: { |
| 239 | none: true |
| 240 | } |
| 241 | } |
| 242 | `) |
| 243 | |
| 244 | foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib") |
| 245 | foo.Output("stripped/libfoo.dylib.so") |
| 246 | // Check that the `cp` rule is using the stripped version as input. |
| 247 | cp := foo.Rule("android.Cp") |
| 248 | if !strings.HasSuffix(cp.Input.String(), "stripped/libfoo.dylib.so") { |
| 249 | t.Errorf("installed binary not based on stripped version: %v", cp.Input) |
| 250 | } |
| 251 | |
| 252 | fizzBar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeOutput("stripped/libbar.dylib.so") |
| 253 | if fizzBar.Rule != nil { |
| 254 | t.Errorf("stripped version of bar has been generated") |
| 255 | } |
| 256 | } |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 257 | |
| 258 | func TestLibstdLinkage(t *testing.T) { |
| 259 | ctx := testRust(t, ` |
| 260 | rust_library { |
| 261 | name: "libfoo", |
| 262 | srcs: ["foo.rs"], |
| 263 | crate_name: "foo", |
| 264 | } |
| 265 | rust_ffi { |
| 266 | name: "libbar", |
| 267 | srcs: ["foo.rs"], |
| 268 | crate_name: "bar", |
| 269 | rustlibs: ["libfoo"], |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 270 | } |
| 271 | rust_ffi { |
| 272 | name: "libbar.prefer_rlib", |
| 273 | srcs: ["foo.rs"], |
| 274 | crate_name: "bar", |
| 275 | rustlibs: ["libfoo"], |
| 276 | prefer_rlib: true, |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 277 | }`) |
| 278 | |
| 279 | libfooDylib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module) |
| 280 | libfooRlibStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_rlib-std").Module().(*Module) |
| 281 | libfooRlibDynamic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Module().(*Module) |
| 282 | |
| 283 | libbarShared := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module().(*Module) |
| 284 | libbarStatic := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Module().(*Module) |
| 285 | |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 286 | // prefer_rlib works the same for both rust_library and rust_ffi, so a single check is sufficient here. |
| 287 | libbarRlibStd := ctx.ModuleForTests("libbar.prefer_rlib", "android_arm64_armv8-a_shared").Module().(*Module) |
| 288 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 289 | if !android.InList("libstd", libfooRlibStatic.Properties.AndroidMkRlibs) { |
| 290 | t.Errorf("rlib-std variant for device rust_library_rlib does not link libstd as an rlib") |
| 291 | } |
| 292 | if !android.InList("libstd", libfooRlibDynamic.Properties.AndroidMkDylibs) { |
| 293 | t.Errorf("dylib-std variant for device rust_library_rlib does not link libstd as an dylib") |
| 294 | } |
| 295 | if !android.InList("libstd", libfooDylib.Properties.AndroidMkDylibs) { |
| 296 | t.Errorf("Device rust_library_dylib does not link libstd as an dylib") |
| 297 | } |
| 298 | |
| 299 | if !android.InList("libstd", libbarShared.Properties.AndroidMkDylibs) { |
| 300 | t.Errorf("Device rust_ffi_shared does not link libstd as an dylib") |
| 301 | } |
| 302 | if !android.InList("libstd", libbarStatic.Properties.AndroidMkRlibs) { |
| 303 | t.Errorf("Device rust_ffi_static does not link libstd as an rlib") |
| 304 | } |
| 305 | if !android.InList("libfoo.rlib-std", libbarStatic.Properties.AndroidMkRlibs) { |
| 306 | t.Errorf("Device rust_ffi_static does not link dependent rustlib rlib-std variant") |
| 307 | } |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 308 | if !android.InList("libstd", libbarRlibStd.Properties.AndroidMkRlibs) { |
| 309 | t.Errorf("rust_ffi with prefer_rlib does not link libstd as an rlib") |
| 310 | } |
| 311 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 312 | } |