| 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 | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 40 | libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib_rlib-std").Rule("rustc") | 
|  | 41 | libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Rule("rustc") | 
|  | 42 | libfooStatic := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_static").Rule("rustc") | 
|  | 43 | libfooShared := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_shared").Rule("rustc") | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 44 |  | 
|  | 45 | rlibCrateType := "rlib" | 
|  | 46 | dylibCrateType := "dylib" | 
|  | 47 | sharedCrateType := "cdylib" | 
| Martin Geisler | b8a4c2c | 2022-11-18 11:52:57 +0100 | [diff] [blame] | 48 | staticCrateType := "staticlib" | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 49 |  | 
|  | 50 | // Test crate type for rlib is correct. | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [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. | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [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. | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 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"]) | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
|  | 65 | // Test crate type for C shared libraries is correct. | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 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"]) | 
| Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 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 |  | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 81 | libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Rule("rustc") | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 82 |  | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 83 | if !strings.Contains(libfooDylib.Args["rustcFlags"], "prefer-dynamic") { | 
|  | 84 | 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] | 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 |  | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 97 | libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Rule("rustc") | 
| Stephen Crane | 0dbfc56 | 2021-07-07 19:05:02 -0700 | [diff] [blame] | 98 |  | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 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"]) | 
| Stephen Crane | 0dbfc56 | 2021-07-07 19:05:02 -0700 | [diff] [blame] | 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 |  | 
| Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 151 | libfooOutput := libfoo.Rule("rustc") | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 152 | if !strings.Contains(libfooOutput.Args["linkFlags"], "-Wl,-soname=libfoo.so") { | 
| Ivan Lozano | 7e741cc | 2020-06-19 12:32:30 -0400 | [diff] [blame] | 153 | 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] | 154 | libfooOutput.Args["linkFlags"]) | 
| Ivan Lozano | 7e741cc | 2020-06-19 12:32:30 -0400 | [diff] [blame] | 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 | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 163 | func TestSharedLibraryToc(t *testing.T) { | 
|  | 164 | ctx := testRust(t, ` | 
|  | 165 | rust_ffi_shared { | 
|  | 166 | name: "libfoo", | 
|  | 167 | srcs: ["foo.rs"], | 
|  | 168 | crate_name: "foo", | 
|  | 169 | } | 
|  | 170 | cc_binary { | 
|  | 171 | name: "fizzbuzz", | 
|  | 172 | shared_libs: ["libfoo"], | 
|  | 173 | }`) | 
|  | 174 |  | 
|  | 175 | fizzbuzz := ctx.ModuleForTests("fizzbuzz", "android_arm64_armv8-a").Rule("ld") | 
|  | 176 |  | 
|  | 177 | if !android.SuffixInList(fizzbuzz.Implicits.Strings(), "libfoo.so.toc") { | 
|  | 178 | t.Errorf("missing expected libfoo.so.toc implicit dependency, instead found: %#v", | 
|  | 179 | fizzbuzz.Implicits.Strings()) | 
|  | 180 | } | 
|  | 181 | } | 
|  | 182 |  | 
| Ivan Lozano | 042504f | 2020-08-18 14:31:23 -0400 | [diff] [blame] | 183 | func TestStaticLibraryLinkage(t *testing.T) { | 
|  | 184 | ctx := testRust(t, ` | 
|  | 185 | rust_ffi_static { | 
|  | 186 | name: "libfoo", | 
|  | 187 | srcs: ["foo.rs"], | 
|  | 188 | crate_name: "foo", | 
|  | 189 | }`) | 
|  | 190 |  | 
|  | 191 | libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static") | 
|  | 192 |  | 
|  | 193 | if !android.InList("libstd", libfoo.Module().(*Module).Properties.AndroidMkRlibs) { | 
|  | 194 | t.Errorf("Static libstd rlib expected to be a dependency of Rust static libraries. Rlib deps are: %#v", | 
|  | 195 | libfoo.Module().(*Module).Properties.AndroidMkDylibs) | 
|  | 196 | } | 
|  | 197 | } | 
|  | 198 |  | 
| Vinh Tran | 156ea44 | 2023-08-17 15:46:39 -0400 | [diff] [blame] | 199 | func TestNativeDependencyOfRlib(t *testing.T) { | 
|  | 200 | ctx := testRust(t, ` | 
|  | 201 | rust_ffi_static { | 
|  | 202 | name: "libffi_static", | 
|  | 203 | crate_name: "ffi_static", | 
|  | 204 | rlibs: ["librust_rlib"], | 
|  | 205 | srcs: ["foo.rs"], | 
|  | 206 | } | 
|  | 207 | rust_library_rlib { | 
|  | 208 | name: "librust_rlib", | 
|  | 209 | crate_name: "rust_rlib", | 
|  | 210 | srcs: ["foo.rs"], | 
|  | 211 | shared_libs: ["shared_cc_dep"], | 
|  | 212 | static_libs: ["static_cc_dep"], | 
|  | 213 | } | 
|  | 214 | cc_library_shared { | 
|  | 215 | name: "shared_cc_dep", | 
|  | 216 | srcs: ["foo.cpp"], | 
|  | 217 | } | 
|  | 218 | cc_library_static { | 
|  | 219 | name: "static_cc_dep", | 
|  | 220 | srcs: ["foo.cpp"], | 
|  | 221 | } | 
|  | 222 | `) | 
|  | 223 |  | 
|  | 224 | rustRlibRlibStd := ctx.ModuleForTests("librust_rlib", "android_arm64_armv8-a_rlib_rlib-std") | 
|  | 225 | rustRlibDylibStd := ctx.ModuleForTests("librust_rlib", "android_arm64_armv8-a_rlib_dylib-std") | 
|  | 226 | ffiStatic := ctx.ModuleForTests("libffi_static", "android_arm64_armv8-a_static") | 
|  | 227 |  | 
|  | 228 | modules := []android.TestingModule{ | 
|  | 229 | rustRlibRlibStd, | 
|  | 230 | rustRlibDylibStd, | 
|  | 231 | ffiStatic, | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | // librust_rlib specifies -L flag to cc deps output directory on rustc command | 
|  | 235 | // and re-export the cc deps to rdep libffi_static | 
|  | 236 | // When building rlib crate, rustc doesn't link the native libraries | 
|  | 237 | // The build system assumes the  cc deps will be at the final linkage (either a shared library or binary) | 
|  | 238 | // Hence, these flags are no-op | 
|  | 239 | // TODO: We could consider removing these flags | 
|  | 240 | for _, module := range modules { | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 241 | if !strings.Contains(module.Rule("rustc").Args["libFlags"], | 
|  | 242 | "-L out/soong/.intermediates/shared_cc_dep/android_arm64_armv8-a_shared/") { | 
| Vinh Tran | 156ea44 | 2023-08-17 15:46:39 -0400 | [diff] [blame] | 243 | t.Errorf( | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 244 | "missing -L flag for shared_cc_dep, rustcFlags: %#v", | 
|  | 245 | rustRlibRlibStd.Rule("rustc").Args["libFlags"], | 
| Vinh Tran | 156ea44 | 2023-08-17 15:46:39 -0400 | [diff] [blame] | 246 | ) | 
|  | 247 | } | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 248 | if !strings.Contains(module.Rule("rustc").Args["libFlags"], | 
|  | 249 | "-L out/soong/.intermediates/static_cc_dep/android_arm64_armv8-a_static/") { | 
| Vinh Tran | 156ea44 | 2023-08-17 15:46:39 -0400 | [diff] [blame] | 250 | t.Errorf( | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 251 | "missing -L flag for static_cc_dep, rustcFlags: %#v", | 
|  | 252 | rustRlibRlibStd.Rule("rustc").Args["libFlags"], | 
| Vinh Tran | 156ea44 | 2023-08-17 15:46:39 -0400 | [diff] [blame] | 253 | ) | 
|  | 254 | } | 
|  | 255 | } | 
|  | 256 | } | 
|  | 257 |  | 
| Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 258 | // Test that variants pull in the right type of rustlib autodep | 
|  | 259 | func TestAutoDeps(t *testing.T) { | 
|  | 260 |  | 
|  | 261 | ctx := testRust(t, ` | 
| Ivan Lozano | 2d40763 | 2022-04-07 12:59:11 -0400 | [diff] [blame] | 262 | rust_library_host { | 
|  | 263 | name: "libbar", | 
|  | 264 | srcs: ["bar.rs"], | 
|  | 265 | crate_name: "bar", | 
|  | 266 | } | 
|  | 267 | rust_library_host_rlib { | 
|  | 268 | name: "librlib_only", | 
|  | 269 | srcs: ["bar.rs"], | 
|  | 270 | crate_name: "rlib_only", | 
|  | 271 | } | 
| Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 272 | rust_library_host { | 
|  | 273 | name: "libfoo", | 
|  | 274 | srcs: ["foo.rs"], | 
|  | 275 | crate_name: "foo", | 
| Ivan Lozano | 2d40763 | 2022-04-07 12:59:11 -0400 | [diff] [blame] | 276 | rustlibs: [ | 
|  | 277 | "libbar", | 
|  | 278 | "librlib_only", | 
|  | 279 | ], | 
| Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 280 | } | 
| Ivan Lozano | 2d40763 | 2022-04-07 12:59:11 -0400 | [diff] [blame] | 281 | rust_ffi_host { | 
|  | 282 | name: "libfoo.ffi", | 
|  | 283 | srcs: ["foo.rs"], | 
|  | 284 | crate_name: "foo", | 
|  | 285 | rustlibs: [ | 
|  | 286 | "libbar", | 
|  | 287 | "librlib_only", | 
|  | 288 | ], | 
|  | 289 | }`) | 
| Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 290 |  | 
| Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 291 | libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib_rlib-std") | 
| Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 292 | libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib") | 
|  | 293 | libfooStatic := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_static") | 
|  | 294 | libfooShared := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_shared") | 
|  | 295 |  | 
|  | 296 | for _, static := range []android.TestingModule{libfooRlib, libfooStatic} { | 
| Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 297 | if !android.InList("libbar.rlib-std", static.Module().(*Module).Properties.AndroidMkRlibs) { | 
|  | 298 | t.Errorf("libbar not present as rlib dependency in static lib") | 
| Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 299 | } | 
|  | 300 | if android.InList("libbar", static.Module().(*Module).Properties.AndroidMkDylibs) { | 
|  | 301 | t.Errorf("libbar present as dynamic dependency in static lib") | 
|  | 302 | } | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | for _, dyn := range []android.TestingModule{libfooDylib, libfooShared} { | 
|  | 306 | if !android.InList("libbar", dyn.Module().(*Module).Properties.AndroidMkDylibs) { | 
|  | 307 | t.Errorf("libbar not present as dynamic dependency in dynamic lib") | 
|  | 308 | } | 
| Ivan Lozano | 4df0257 | 2023-06-15 14:21:09 -0400 | [diff] [blame] | 309 | if android.InList("libbar", dyn.Module().(*Module).Properties.AndroidMkRlibs) { | 
| Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 310 | t.Errorf("libbar present as rlib dependency in dynamic lib") | 
| Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 311 | } | 
| Ivan Lozano | 4df0257 | 2023-06-15 14:21:09 -0400 | [diff] [blame] | 312 | if !android.InList("librlib_only", dyn.Module().(*Module).Properties.AndroidMkRlibs) { | 
| Ivan Lozano | 2d40763 | 2022-04-07 12:59:11 -0400 | [diff] [blame] | 313 | t.Errorf("librlib_only should be selected by rustlibs as an rlib.") | 
|  | 314 | } | 
| Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 315 | } | 
|  | 316 | } | 
| Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 317 |  | 
|  | 318 | // Test that stripped versions are correctly generated and used. | 
|  | 319 | func TestStrippedLibrary(t *testing.T) { | 
|  | 320 | ctx := testRust(t, ` | 
|  | 321 | rust_library_dylib { | 
|  | 322 | name: "libfoo", | 
|  | 323 | crate_name: "foo", | 
|  | 324 | srcs: ["foo.rs"], | 
|  | 325 | } | 
|  | 326 | rust_library_dylib { | 
|  | 327 | name: "libbar", | 
|  | 328 | crate_name: "bar", | 
|  | 329 | srcs: ["foo.rs"], | 
|  | 330 | strip: { | 
|  | 331 | none: true | 
|  | 332 | } | 
|  | 333 | } | 
|  | 334 | `) | 
|  | 335 |  | 
|  | 336 | foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib") | 
| Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 337 | foo.Output("libfoo.dylib.so") | 
|  | 338 | foo.Output("unstripped/libfoo.dylib.so") | 
| Thiébaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 339 | // Check that the `cp` rule is using the stripped version as input. | 
|  | 340 | cp := foo.Rule("android.Cp") | 
| Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 341 | if strings.HasSuffix(cp.Input.String(), "unstripped/libfoo.dylib.so") { | 
|  | 342 | 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] | 343 | } | 
|  | 344 |  | 
| Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 345 | 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] | 346 | if fizzBar.Rule != nil { | 
| Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 347 | 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] | 348 | } | 
|  | 349 | } | 
| Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 350 |  | 
|  | 351 | func TestLibstdLinkage(t *testing.T) { | 
|  | 352 | ctx := testRust(t, ` | 
|  | 353 | rust_library { | 
|  | 354 | name: "libfoo", | 
|  | 355 | srcs: ["foo.rs"], | 
|  | 356 | crate_name: "foo", | 
|  | 357 | } | 
|  | 358 | rust_ffi { | 
|  | 359 | name: "libbar", | 
|  | 360 | srcs: ["foo.rs"], | 
|  | 361 | crate_name: "bar", | 
|  | 362 | rustlibs: ["libfoo"], | 
| Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 363 | } | 
|  | 364 | rust_ffi { | 
|  | 365 | name: "libbar.prefer_rlib", | 
|  | 366 | srcs: ["foo.rs"], | 
|  | 367 | crate_name: "bar", | 
|  | 368 | rustlibs: ["libfoo"], | 
|  | 369 | prefer_rlib: true, | 
| Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 370 | }`) | 
|  | 371 |  | 
|  | 372 | libfooDylib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module) | 
|  | 373 | libfooRlibStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_rlib-std").Module().(*Module) | 
|  | 374 | libfooRlibDynamic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Module().(*Module) | 
|  | 375 |  | 
|  | 376 | libbarShared := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module().(*Module) | 
|  | 377 | libbarStatic := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Module().(*Module) | 
|  | 378 |  | 
| Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 379 | // prefer_rlib works the same for both rust_library and rust_ffi, so a single check is sufficient here. | 
|  | 380 | libbarRlibStd := ctx.ModuleForTests("libbar.prefer_rlib", "android_arm64_armv8-a_shared").Module().(*Module) | 
|  | 381 |  | 
| Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 382 | if !android.InList("libstd", libfooRlibStatic.Properties.AndroidMkRlibs) { | 
|  | 383 | t.Errorf("rlib-std variant for device rust_library_rlib does not link libstd as an rlib") | 
|  | 384 | } | 
|  | 385 | if !android.InList("libstd", libfooRlibDynamic.Properties.AndroidMkDylibs) { | 
|  | 386 | t.Errorf("dylib-std variant for device rust_library_rlib does not link libstd as an dylib") | 
|  | 387 | } | 
|  | 388 | if !android.InList("libstd", libfooDylib.Properties.AndroidMkDylibs) { | 
|  | 389 | t.Errorf("Device rust_library_dylib does not link libstd as an dylib") | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | if !android.InList("libstd", libbarShared.Properties.AndroidMkDylibs) { | 
|  | 393 | t.Errorf("Device rust_ffi_shared does not link libstd as an dylib") | 
|  | 394 | } | 
|  | 395 | if !android.InList("libstd", libbarStatic.Properties.AndroidMkRlibs) { | 
|  | 396 | t.Errorf("Device rust_ffi_static does not link libstd as an rlib") | 
|  | 397 | } | 
|  | 398 | if !android.InList("libfoo.rlib-std", libbarStatic.Properties.AndroidMkRlibs) { | 
|  | 399 | t.Errorf("Device rust_ffi_static does not link dependent rustlib rlib-std variant") | 
|  | 400 | } | 
| Ivan Lozano | ea08613 | 2020-12-08 14:43:00 -0500 | [diff] [blame] | 401 | if !android.InList("libstd", libbarRlibStd.Properties.AndroidMkRlibs) { | 
|  | 402 | t.Errorf("rust_ffi with prefer_rlib does not link libstd as an rlib") | 
|  | 403 | } | 
|  | 404 |  | 
| Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 405 | } | 
| Ivan Lozano | f033ca6 | 2024-03-21 13:43:14 -0400 | [diff] [blame] | 406 |  | 
|  | 407 | func TestRustFFIExportedIncludes(t *testing.T) { | 
|  | 408 | ctx := testRust(t, ` | 
|  | 409 | rust_ffi { | 
|  | 410 | name: "libbar", | 
|  | 411 | srcs: ["foo.rs"], | 
|  | 412 | crate_name: "bar", | 
|  | 413 | export_include_dirs: ["rust_includes"], | 
|  | 414 | host_supported: true, | 
|  | 415 | } | 
|  | 416 | cc_library_static { | 
|  | 417 | name: "libfoo", | 
|  | 418 | srcs: ["foo.cpp"], | 
|  | 419 | shared_libs: ["libbar"], | 
|  | 420 | host_supported: true, | 
|  | 421 | }`) | 
|  | 422 | libfooStatic := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_static").Rule("cc") | 
|  | 423 | android.AssertStringDoesContain(t, "cFlags for lib module", libfooStatic.Args["cFlags"], " -Irust_includes ") | 
|  | 424 | } |