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 | } |
Martin Geisler | bd736da | 2022-11-18 11:55:27 +0100 | [diff] [blame] | 33 | rust_ffi_host { |
| 34 | name: "libfoo.ffi", |
| 35 | srcs: ["foo.rs"], |
| 36 | crate_name: "foo" |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 37 | } |
| 38 | rust_ffi_host_static { |
| 39 | name: "libfoo.ffi_static", |
| 40 | srcs: ["foo.rs"], |
| 41 | crate_name: "foo" |
Martin Geisler | bd736da | 2022-11-18 11:55:27 +0100 | [diff] [blame] | 42 | }`) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 43 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 44 | // Test all variants are being built. |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 45 | libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib_rlib-std").Rule("rustc") |
| 46 | libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Rule("rustc") |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 47 | libfooFFIRlib := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_rlib_rlib-std").Rule("rustc") |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 48 | libfooShared := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_shared").Rule("rustc") |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 49 | |
| 50 | rlibCrateType := "rlib" |
| 51 | dylibCrateType := "dylib" |
| 52 | sharedCrateType := "cdylib" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 53 | |
| 54 | // Test crate type for rlib is correct. |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 55 | if !strings.Contains(libfooRlib.Args["rustcFlags"], "crate-type="+rlibCrateType) { |
| 56 | 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] | 57 | } |
| 58 | |
| 59 | // Test crate type for dylib is correct. |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 60 | if !strings.Contains(libfooDylib.Args["rustcFlags"], "crate-type="+dylibCrateType) { |
| 61 | 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] | 62 | } |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 63 | |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 64 | // Test crate type for FFI rlibs is correct |
| 65 | if !strings.Contains(libfooFFIRlib.Args["rustcFlags"], "crate-type="+rlibCrateType) { |
| 66 | t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", rlibCrateType, libfooFFIRlib.Args["rustcFlags"]) |
| 67 | } |
| 68 | |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 69 | // Test crate type for C shared libraries is correct. |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 70 | if !strings.Contains(libfooShared.Args["rustcFlags"], "crate-type="+sharedCrateType) { |
| 71 | t.Errorf("missing crate-type for shared variant, expecting %#v, got rustcFlags: %#v", sharedCrateType, libfooShared.Args["rustcFlags"]) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // Test that dylibs are not statically linking the standard library. |
| 77 | func TestDylibPreferDynamic(t *testing.T) { |
| 78 | ctx := testRust(t, ` |
| 79 | rust_library_host_dylib { |
| 80 | name: "libfoo", |
| 81 | srcs: ["foo.rs"], |
| 82 | crate_name: "foo", |
| 83 | }`) |
| 84 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 85 | libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Rule("rustc") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 86 | |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 87 | if !strings.Contains(libfooDylib.Args["rustcFlags"], "prefer-dynamic") { |
| 88 | t.Errorf("missing prefer-dynamic flag for libfoo dylib, rustcFlags: %#v", libfooDylib.Args["rustcFlags"]) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 91 | |
| 92 | func TestValidateLibraryStem(t *testing.T) { |
| 93 | testRustError(t, "crate_name must be defined.", ` |
| 94 | rust_library_host { |
| 95 | name: "libfoo", |
| 96 | srcs: ["foo.rs"], |
| 97 | }`) |
| 98 | |
| 99 | testRustError(t, "library crate_names must be alphanumeric with underscores allowed", ` |
| 100 | rust_library_host { |
| 101 | name: "libfoo-bar", |
| 102 | srcs: ["foo.rs"], |
| 103 | crate_name: "foo-bar" |
| 104 | }`) |
| 105 | |
| 106 | testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", ` |
| 107 | rust_library_host { |
| 108 | name: "foobar", |
| 109 | srcs: ["foo.rs"], |
| 110 | crate_name: "foo_bar" |
| 111 | }`) |
| 112 | testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", ` |
| 113 | rust_library_host { |
| 114 | name: "foobar", |
| 115 | stem: "libfoo", |
| 116 | srcs: ["foo.rs"], |
| 117 | crate_name: "foo_bar" |
| 118 | }`) |
| 119 | testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", ` |
| 120 | rust_library_host { |
| 121 | name: "foobar", |
| 122 | stem: "foo_bar", |
| 123 | srcs: ["foo.rs"], |
| 124 | crate_name: "foo_bar" |
| 125 | }`) |
| 126 | |
| 127 | } |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 128 | |
Ivan Lozano | 7e741cc | 2020-06-19 12:32:30 -0400 | [diff] [blame] | 129 | func TestSharedLibrary(t *testing.T) { |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 130 | ctx := testRust(t, ` |
Matthew Maurer | 2ae0513 | 2020-06-23 14:28:53 -0700 | [diff] [blame] | 131 | rust_ffi_shared { |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 132 | name: "libfoo", |
| 133 | srcs: ["foo.rs"], |
| 134 | crate_name: "foo", |
| 135 | }`) |
| 136 | |
Ivan Lozano | 7e741cc | 2020-06-19 12:32:30 -0400 | [diff] [blame] | 137 | libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared") |
| 138 | |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 139 | libfooOutput := libfoo.Rule("rustc") |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 140 | if !strings.Contains(libfooOutput.Args["linkFlags"], "-Wl,-soname=libfoo.so") { |
Ivan Lozano | 7e741cc | 2020-06-19 12:32:30 -0400 | [diff] [blame] | 141 | t.Errorf("missing expected -Wl,-soname linker flag for libfoo shared lib, linkFlags: %#v", |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 142 | libfooOutput.Args["linkFlags"]) |
Ivan Lozano | 7e741cc | 2020-06-19 12:32:30 -0400 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | if !android.InList("libstd", libfoo.Module().(*Module).Properties.AndroidMkDylibs) { |
| 146 | t.Errorf("Non-static libstd dylib expected to be a dependency of Rust shared libraries. Dylib deps are: %#v", |
| 147 | libfoo.Module().(*Module).Properties.AndroidMkDylibs) |
Ivan Lozano | bec05ea | 2020-06-09 08:27:49 -0400 | [diff] [blame] | 148 | } |
| 149 | } |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 150 | |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 151 | func TestSharedLibraryToc(t *testing.T) { |
| 152 | ctx := testRust(t, ` |
| 153 | rust_ffi_shared { |
| 154 | name: "libfoo", |
| 155 | srcs: ["foo.rs"], |
| 156 | crate_name: "foo", |
| 157 | } |
| 158 | cc_binary { |
| 159 | name: "fizzbuzz", |
| 160 | shared_libs: ["libfoo"], |
| 161 | }`) |
| 162 | |
| 163 | fizzbuzz := ctx.ModuleForTests("fizzbuzz", "android_arm64_armv8-a").Rule("ld") |
| 164 | |
| 165 | if !android.SuffixInList(fizzbuzz.Implicits.Strings(), "libfoo.so.toc") { |
| 166 | t.Errorf("missing expected libfoo.so.toc implicit dependency, instead found: %#v", |
| 167 | fizzbuzz.Implicits.Strings()) |
| 168 | } |
| 169 | } |
| 170 | |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 171 | func TestStaticLibraryLinkage(t *testing.T) { |
| 172 | ctx := testRust(t, ` |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 173 | rust_ffi_static { |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 174 | name: "libfoo", |
| 175 | srcs: ["foo.rs"], |
| 176 | crate_name: "foo", |
| 177 | }`) |
| 178 | |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 179 | libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_rlib-std") |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 180 | |
| 181 | if !android.InList("libstd", libfoo.Module().(*Module).Properties.AndroidMkRlibs) { |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 182 | t.Errorf("Static libstd rlib expected to be a dependency of Rust rlib libraries. Rlib deps are: %#v", |
| 183 | libfoo.Module().(*Module).Properties.AndroidMkDylibs) |
| 184 | } |
Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 185 | } |
| 186 | |
Vinh Tran | 156ea44 | 2023-08-17 15:46:39 -0400 | [diff] [blame] | 187 | func TestNativeDependencyOfRlib(t *testing.T) { |
| 188 | ctx := testRust(t, ` |
Ivan Lozano | 6184842 | 2024-12-13 19:45:00 +0000 | [diff] [blame] | 189 | rust_ffi_static { |
| 190 | name: "libffi_static", |
| 191 | crate_name: "ffi_static", |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 192 | rlibs: ["librust_rlib"], |
| 193 | srcs: ["foo.rs"], |
| 194 | } |
Vinh Tran | 156ea44 | 2023-08-17 15:46:39 -0400 | [diff] [blame] | 195 | rust_library_rlib { |
| 196 | name: "librust_rlib", |
| 197 | crate_name: "rust_rlib", |
| 198 | srcs: ["foo.rs"], |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 199 | shared_libs: ["libshared_cc_dep"], |
| 200 | static_libs: ["libstatic_cc_dep"], |
Vinh Tran | 156ea44 | 2023-08-17 15:46:39 -0400 | [diff] [blame] | 201 | } |
| 202 | cc_library_shared { |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 203 | name: "libshared_cc_dep", |
Vinh Tran | 156ea44 | 2023-08-17 15:46:39 -0400 | [diff] [blame] | 204 | srcs: ["foo.cpp"], |
| 205 | } |
| 206 | cc_library_static { |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 207 | name: "libstatic_cc_dep", |
Vinh Tran | 156ea44 | 2023-08-17 15:46:39 -0400 | [diff] [blame] | 208 | srcs: ["foo.cpp"], |
| 209 | } |
| 210 | `) |
| 211 | |
| 212 | rustRlibRlibStd := ctx.ModuleForTests("librust_rlib", "android_arm64_armv8-a_rlib_rlib-std") |
| 213 | rustRlibDylibStd := ctx.ModuleForTests("librust_rlib", "android_arm64_armv8-a_rlib_dylib-std") |
Ivan Lozano | 6184842 | 2024-12-13 19:45:00 +0000 | [diff] [blame] | 214 | ffiRlib := ctx.ModuleForTests("libffi_static", "android_arm64_armv8-a_rlib_rlib-std") |
Vinh Tran | 156ea44 | 2023-08-17 15:46:39 -0400 | [diff] [blame] | 215 | |
| 216 | modules := []android.TestingModule{ |
| 217 | rustRlibRlibStd, |
| 218 | rustRlibDylibStd, |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 219 | ffiRlib, |
Vinh Tran | 156ea44 | 2023-08-17 15:46:39 -0400 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // librust_rlib specifies -L flag to cc deps output directory on rustc command |
| 223 | // and re-export the cc deps to rdep libffi_static |
| 224 | // When building rlib crate, rustc doesn't link the native libraries |
| 225 | // The build system assumes the cc deps will be at the final linkage (either a shared library or binary) |
| 226 | // Hence, these flags are no-op |
| 227 | // TODO: We could consider removing these flags |
| 228 | for _, module := range modules { |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 229 | if !strings.Contains(module.Rule("rustc").Args["libFlags"], |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 230 | "-L out/soong/.intermediates/libshared_cc_dep/android_arm64_armv8-a_shared/") { |
Vinh Tran | 156ea44 | 2023-08-17 15:46:39 -0400 | [diff] [blame] | 231 | t.Errorf( |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 232 | "missing -L flag for libshared_cc_dep of %s, rustcFlags: %#v", |
| 233 | module.Module().Name(), rustRlibRlibStd.Rule("rustc").Args["libFlags"], |
Vinh Tran | 156ea44 | 2023-08-17 15:46:39 -0400 | [diff] [blame] | 234 | ) |
| 235 | } |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 236 | if !strings.Contains(module.Rule("rustc").Args["libFlags"], |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 237 | "-L out/soong/.intermediates/libstatic_cc_dep/android_arm64_armv8-a_static/") { |
Vinh Tran | 156ea44 | 2023-08-17 15:46:39 -0400 | [diff] [blame] | 238 | t.Errorf( |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 239 | "missing -L flag for libstatic_cc_dep of %s, rustcFlags: %#v", |
| 240 | module.Module().Name(), rustRlibRlibStd.Rule("rustc").Args["libFlags"], |
Vinh Tran | 156ea44 | 2023-08-17 15:46:39 -0400 | [diff] [blame] | 241 | ) |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 246 | // Test that variants pull in the right type of rustlib autodep |
| 247 | func TestAutoDeps(t *testing.T) { |
| 248 | |
| 249 | ctx := testRust(t, ` |
Ivan Lozano | 2d40763 | 2022-04-07 12:59:11 -0400 | [diff] [blame] | 250 | rust_library_host { |
| 251 | name: "libbar", |
| 252 | srcs: ["bar.rs"], |
| 253 | crate_name: "bar", |
| 254 | } |
| 255 | rust_library_host_rlib { |
| 256 | name: "librlib_only", |
| 257 | srcs: ["bar.rs"], |
| 258 | crate_name: "rlib_only", |
| 259 | } |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 260 | rust_library_host { |
| 261 | name: "libfoo", |
| 262 | srcs: ["foo.rs"], |
| 263 | crate_name: "foo", |
Ivan Lozano | 2d40763 | 2022-04-07 12:59:11 -0400 | [diff] [blame] | 264 | rustlibs: [ |
| 265 | "libbar", |
| 266 | "librlib_only", |
| 267 | ], |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 268 | } |
Ivan Lozano | 2d40763 | 2022-04-07 12:59:11 -0400 | [diff] [blame] | 269 | rust_ffi_host { |
| 270 | name: "libfoo.ffi", |
| 271 | srcs: ["foo.rs"], |
| 272 | crate_name: "foo", |
| 273 | rustlibs: [ |
| 274 | "libbar", |
| 275 | "librlib_only", |
| 276 | ], |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 277 | } |
| 278 | rust_ffi_host_static { |
| 279 | name: "libfoo.ffi.static", |
| 280 | srcs: ["foo.rs"], |
| 281 | crate_name: "foo", |
| 282 | rustlibs: [ |
| 283 | "libbar", |
| 284 | "librlib_only", |
| 285 | ], |
Ivan Lozano | 2d40763 | 2022-04-07 12:59:11 -0400 | [diff] [blame] | 286 | }`) |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 287 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 288 | libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib_rlib-std") |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 289 | libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib") |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 290 | libfooFFIRlib := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_rlib_rlib-std") |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 291 | libfooShared := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_shared") |
| 292 | |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 293 | for _, static := range []android.TestingModule{libfooRlib, libfooFFIRlib} { |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 294 | if !android.InList("libbar.rlib-std", static.Module().(*Module).Properties.AndroidMkRlibs) { |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 295 | t.Errorf("libbar not present as rlib dependency in static lib: %s", static.Module().Name()) |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 296 | } |
| 297 | if android.InList("libbar", static.Module().(*Module).Properties.AndroidMkDylibs) { |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 298 | t.Errorf("libbar present as dynamic dependency in static lib: %s", static.Module().Name()) |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | for _, dyn := range []android.TestingModule{libfooDylib, libfooShared} { |
| 303 | if !android.InList("libbar", dyn.Module().(*Module).Properties.AndroidMkDylibs) { |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 304 | t.Errorf("libbar not present as dynamic dependency in dynamic lib: %s", dyn.Module().Name()) |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 305 | } |
Ivan Lozano | 4df0257 | 2023-06-15 14:21:09 -0400 | [diff] [blame] | 306 | if android.InList("libbar", dyn.Module().(*Module).Properties.AndroidMkRlibs) { |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 307 | t.Errorf("libbar present as rlib dependency in dynamic lib: %s", dyn.Module().Name()) |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 308 | } |
Ivan Lozano | 4df0257 | 2023-06-15 14:21:09 -0400 | [diff] [blame] | 309 | if !android.InList("librlib_only", dyn.Module().(*Module).Properties.AndroidMkRlibs) { |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 310 | t.Errorf("librlib_only should be selected by rustlibs as an rlib: %s.", dyn.Module().Name()) |
Ivan Lozano | 2d40763 | 2022-04-07 12:59:11 -0400 | [diff] [blame] | 311 | } |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 314 | |
| 315 | // Test that stripped versions are correctly generated and used. |
| 316 | func TestStrippedLibrary(t *testing.T) { |
| 317 | ctx := testRust(t, ` |
| 318 | rust_library_dylib { |
| 319 | name: "libfoo", |
| 320 | crate_name: "foo", |
| 321 | srcs: ["foo.rs"], |
| 322 | } |
| 323 | rust_library_dylib { |
| 324 | name: "libbar", |
| 325 | crate_name: "bar", |
| 326 | srcs: ["foo.rs"], |
| 327 | strip: { |
| 328 | none: true |
| 329 | } |
| 330 | } |
| 331 | `) |
| 332 | |
| 333 | foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib") |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 334 | foo.Output("libfoo.dylib.so") |
| 335 | foo.Output("unstripped/libfoo.dylib.so") |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 336 | // Check that the `cp` rule is using the stripped version as input. |
| 337 | cp := foo.Rule("android.Cp") |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 338 | if strings.HasSuffix(cp.Input.String(), "unstripped/libfoo.dylib.so") { |
| 339 | t.Errorf("installed library not based on stripped version: %v", cp.Input) |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 340 | } |
| 341 | |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 342 | fizzBar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeOutput("unstripped/libbar.dylib.so") |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 343 | if fizzBar.Rule != nil { |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 344 | t.Errorf("unstripped library exists, so stripped library has incorrectly been generated") |
Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 345 | } |
| 346 | } |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 347 | |
| 348 | func TestLibstdLinkage(t *testing.T) { |
| 349 | ctx := testRust(t, ` |
| 350 | rust_library { |
| 351 | name: "libfoo", |
| 352 | srcs: ["foo.rs"], |
| 353 | crate_name: "foo", |
| 354 | } |
| 355 | rust_ffi { |
| 356 | name: "libbar", |
| 357 | srcs: ["foo.rs"], |
| 358 | crate_name: "bar", |
| 359 | rustlibs: ["libfoo"], |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 360 | } |
Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 361 | rust_ffi_static { |
| 362 | name: "libbar_static", |
| 363 | srcs: ["foo.rs"], |
| 364 | crate_name: "bar", |
| 365 | rustlibs: ["libfoo"], |
| 366 | } |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 367 | rust_ffi { |
| 368 | name: "libbar.prefer_rlib", |
| 369 | srcs: ["foo.rs"], |
| 370 | crate_name: "bar", |
| 371 | rustlibs: ["libfoo"], |
| 372 | prefer_rlib: true, |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 373 | }`) |
| 374 | |
| 375 | libfooDylib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module) |
| 376 | libfooRlibStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_rlib-std").Module().(*Module) |
| 377 | libfooRlibDynamic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Module().(*Module) |
| 378 | |
| 379 | libbarShared := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module().(*Module) |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 380 | libbarFFIRlib := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_rlib_rlib-std").Module().(*Module) |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 381 | |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 382 | // prefer_rlib works the same for both rust_library and rust_ffi, so a single check is sufficient here. |
| 383 | libbarRlibStd := ctx.ModuleForTests("libbar.prefer_rlib", "android_arm64_armv8-a_shared").Module().(*Module) |
| 384 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 385 | if !android.InList("libstd", libfooRlibStatic.Properties.AndroidMkRlibs) { |
| 386 | t.Errorf("rlib-std variant for device rust_library_rlib does not link libstd as an rlib") |
| 387 | } |
| 388 | if !android.InList("libstd", libfooRlibDynamic.Properties.AndroidMkDylibs) { |
| 389 | t.Errorf("dylib-std variant for device rust_library_rlib does not link libstd as an dylib") |
| 390 | } |
| 391 | if !android.InList("libstd", libfooDylib.Properties.AndroidMkDylibs) { |
| 392 | t.Errorf("Device rust_library_dylib does not link libstd as an dylib") |
| 393 | } |
| 394 | |
| 395 | if !android.InList("libstd", libbarShared.Properties.AndroidMkDylibs) { |
| 396 | t.Errorf("Device rust_ffi_shared does not link libstd as an dylib") |
| 397 | } |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 398 | if !android.InList("libstd", libbarFFIRlib.Properties.AndroidMkRlibs) { |
Ivan Lozano | 6184842 | 2024-12-13 19:45:00 +0000 | [diff] [blame] | 399 | t.Errorf("Device rust_ffi_static does not link libstd as an rlib") |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 400 | } |
| 401 | if !android.InList("libfoo.rlib-std", libbarFFIRlib.Properties.AndroidMkRlibs) { |
Ivan Lozano | 6184842 | 2024-12-13 19:45:00 +0000 | [diff] [blame] | 402 | t.Errorf("Device rust_ffi_static does not link dependent rustlib rlib-std variant") |
Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 403 | } |
Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 404 | if !android.InList("libstd", libbarRlibStd.Properties.AndroidMkRlibs) { |
| 405 | t.Errorf("rust_ffi with prefer_rlib does not link libstd as an rlib") |
| 406 | } |
| 407 | |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 408 | } |
Ivan Lozano | f033ca6 | 2024-03-21 13:43:14 -0400 | [diff] [blame] | 409 | |
| 410 | func TestRustFFIExportedIncludes(t *testing.T) { |
| 411 | ctx := testRust(t, ` |
| 412 | rust_ffi { |
| 413 | name: "libbar", |
| 414 | srcs: ["foo.rs"], |
| 415 | crate_name: "bar", |
| 416 | export_include_dirs: ["rust_includes"], |
| 417 | host_supported: true, |
| 418 | } |
| 419 | cc_library_static { |
| 420 | name: "libfoo", |
| 421 | srcs: ["foo.cpp"], |
| 422 | shared_libs: ["libbar"], |
| 423 | host_supported: true, |
| 424 | }`) |
| 425 | libfooStatic := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_static").Rule("cc") |
| 426 | android.AssertStringDoesContain(t, "cFlags for lib module", libfooStatic.Args["cFlags"], " -Irust_includes ") |
| 427 | } |
Ivan Lozano | f458901 | 2024-11-20 22:18:11 +0000 | [diff] [blame] | 428 | |
| 429 | func TestRustVersionScript(t *testing.T) { |
| 430 | ctx := testRust(t, ` |
| 431 | rust_library { |
| 432 | name: "librs", |
| 433 | srcs: ["bar.rs"], |
| 434 | crate_name: "rs", |
| 435 | extra_exported_symbols: "librs.map.txt", |
| 436 | } |
| 437 | rust_ffi { |
| 438 | name: "libffi", |
| 439 | srcs: ["foo.rs"], |
| 440 | crate_name: "ffi", |
| 441 | version_script: "libffi.map.txt", |
| 442 | } |
| 443 | `) |
| 444 | |
| 445 | //linkFlags |
| 446 | librs := ctx.ModuleForTests("librs", "android_arm64_armv8-a_dylib").Rule("rustc") |
| 447 | libffi := ctx.ModuleForTests("libffi", "android_arm64_armv8-a_shared").Rule("rustc") |
| 448 | |
| 449 | if !strings.Contains(librs.Args["linkFlags"], "-Wl,--version-script=librs.map.txt") { |
| 450 | t.Errorf("missing expected -Wl,--version-script= linker flag for libextended shared lib, linkFlags: %#v", |
| 451 | librs.Args["linkFlags"]) |
| 452 | } |
| 453 | if strings.Contains(librs.Args["linkFlags"], "-Wl,--android-version-script=librs.map.txt") { |
| 454 | t.Errorf("unexpected -Wl,--android-version-script= linker flag for libextended shared lib, linkFlags: %#v", |
| 455 | librs.Args["linkFlags"]) |
| 456 | } |
| 457 | |
| 458 | if !strings.Contains(libffi.Args["linkFlags"], "-Wl,--android-version-script=libffi.map.txt") { |
| 459 | t.Errorf("missing -Wl,--android-version-script= linker flag for libreplaced shared lib, linkFlags: %#v", |
| 460 | libffi.Args["linkFlags"]) |
| 461 | } |
| 462 | if strings.Contains(libffi.Args["linkFlags"], "-Wl,--version-script=libffi.map.txt") { |
| 463 | t.Errorf("unexpected -Wl,--version-script= linker flag for libextended shared lib, linkFlags: %#v", |
| 464 | libffi.Args["linkFlags"]) |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | func TestRustVersionScriptPropertyErrors(t *testing.T) { |
| 469 | testRustError(t, "version_script: can only be set for rust_ffi modules", ` |
| 470 | rust_library { |
| 471 | name: "librs", |
| 472 | srcs: ["bar.rs"], |
| 473 | crate_name: "rs", |
| 474 | version_script: "libbar.map.txt", |
| 475 | }`) |
| 476 | testRustError(t, "version_script and extra_exported_symbols", ` |
| 477 | rust_ffi { |
| 478 | name: "librs", |
| 479 | srcs: ["bar.rs"], |
| 480 | crate_name: "rs", |
| 481 | version_script: "libbar.map.txt", |
| 482 | extra_exported_symbols: "libbar.map.txt", |
| 483 | }`) |
| 484 | } |