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