blob: 35a420cd53e0ee42b132419d42d7b66844319f3d [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// 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
15package rust
16
17import (
18 "strings"
19 "testing"
Ivan Lozano7e741cc2020-06-19 12:32:30 -040020
21 "android/soong/android"
Ivan Lozanoffee3342019-08-27 12:03:00 -070022)
23
24// Test that variants are being generated correctly, and that crate-types are correct.
25func 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 Maurer2ae05132020-06-23 14:28:53 -070032 }
Martin Geislerbd736da2022-11-18 11:55:27 +010033 rust_ffi_host {
34 name: "libfoo.ffi",
35 srcs: ["foo.rs"],
36 crate_name: "foo"
Ivan Lozanofd47b1a2024-05-17 14:13:41 -040037 }
38 rust_ffi_host_static {
39 name: "libfoo.ffi_static",
40 srcs: ["foo.rs"],
41 crate_name: "foo"
Martin Geislerbd736da2022-11-18 11:55:27 +010042 }`)
Ivan Lozanoffee3342019-08-27 12:03:00 -070043
Ivan Lozano52767be2019-10-18 14:49:46 -070044 // Test all variants are being built.
Ivan Lozano8d10fc32021-11-05 16:36:47 -040045 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 Lozano0a468a42024-05-13 21:03:34 -040047 libfooFFIRlib := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_rlib_rlib-std").Rule("rustc")
Ivan Lozano8d10fc32021-11-05 16:36:47 -040048 libfooShared := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_shared").Rule("rustc")
Ivan Lozano52767be2019-10-18 14:49:46 -070049
50 rlibCrateType := "rlib"
51 dylibCrateType := "dylib"
52 sharedCrateType := "cdylib"
Ivan Lozanoffee3342019-08-27 12:03:00 -070053
54 // Test crate type for rlib is correct.
Wen-yi Chu41326c12023-09-22 03:58:59 +000055 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 Lozanoffee3342019-08-27 12:03:00 -070057 }
58
59 // Test crate type for dylib is correct.
Wen-yi Chu41326c12023-09-22 03:58:59 +000060 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 Lozanoffee3342019-08-27 12:03:00 -070062 }
Ivan Lozano52767be2019-10-18 14:49:46 -070063
Ivan Lozano0a468a42024-05-13 21:03:34 -040064 // 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 Lozano52767be2019-10-18 14:49:46 -070069 // Test crate type for C shared libraries is correct.
Wen-yi Chu41326c12023-09-22 03:58:59 +000070 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 Lozano52767be2019-10-18 14:49:46 -070072 }
73
Ivan Lozanoffee3342019-08-27 12:03:00 -070074}
75
76// Test that dylibs are not statically linking the standard library.
77func 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 Chu41326c12023-09-22 03:58:59 +000085 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Rule("rustc")
Ivan Lozanoffee3342019-08-27 12:03:00 -070086
Wen-yi Chu41326c12023-09-22 03:58:59 +000087 if !strings.Contains(libfooDylib.Args["rustcFlags"], "prefer-dynamic") {
88 t.Errorf("missing prefer-dynamic flag for libfoo dylib, rustcFlags: %#v", libfooDylib.Args["rustcFlags"])
Ivan Lozanoffee3342019-08-27 12:03:00 -070089 }
90}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070091
Stephen Crane0dbfc562021-07-07 19:05:02 -070092// Check that we are passing the android_dylib config flag
93func TestAndroidDylib(t *testing.T) {
94 ctx := testRust(t, `
95 rust_library_host_dylib {
96 name: "libfoo",
97 srcs: ["foo.rs"],
98 crate_name: "foo",
99 }`)
100
Wen-yi Chu41326c12023-09-22 03:58:59 +0000101 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Rule("rustc")
Stephen Crane0dbfc562021-07-07 19:05:02 -0700102
Wen-yi Chu41326c12023-09-22 03:58:59 +0000103 if !strings.Contains(libfooDylib.Args["rustcFlags"], "--cfg 'android_dylib'") {
104 t.Errorf("missing android_dylib cfg flag for libfoo dylib, rustcFlags: %#v", libfooDylib.Args["rustcFlags"])
Stephen Crane0dbfc562021-07-07 19:05:02 -0700105 }
106}
107
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700108func TestValidateLibraryStem(t *testing.T) {
109 testRustError(t, "crate_name must be defined.", `
110 rust_library_host {
111 name: "libfoo",
112 srcs: ["foo.rs"],
113 }`)
114
115 testRustError(t, "library crate_names must be alphanumeric with underscores allowed", `
116 rust_library_host {
117 name: "libfoo-bar",
118 srcs: ["foo.rs"],
119 crate_name: "foo-bar"
120 }`)
121
122 testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", `
123 rust_library_host {
124 name: "foobar",
125 srcs: ["foo.rs"],
126 crate_name: "foo_bar"
127 }`)
128 testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", `
129 rust_library_host {
130 name: "foobar",
131 stem: "libfoo",
132 srcs: ["foo.rs"],
133 crate_name: "foo_bar"
134 }`)
135 testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", `
136 rust_library_host {
137 name: "foobar",
138 stem: "foo_bar",
139 srcs: ["foo.rs"],
140 crate_name: "foo_bar"
141 }`)
142
143}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400144
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400145func TestSharedLibrary(t *testing.T) {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400146 ctx := testRust(t, `
Matthew Maurer2ae05132020-06-23 14:28:53 -0700147 rust_ffi_shared {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400148 name: "libfoo",
149 srcs: ["foo.rs"],
150 crate_name: "foo",
151 }`)
152
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400153 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared")
154
Colin Cross004bd3f2023-10-02 11:39:17 -0700155 libfooOutput := libfoo.Rule("rustc")
Wen-yi Chu41326c12023-09-22 03:58:59 +0000156 if !strings.Contains(libfooOutput.Args["linkFlags"], "-Wl,-soname=libfoo.so") {
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400157 t.Errorf("missing expected -Wl,-soname linker flag for libfoo shared lib, linkFlags: %#v",
Wen-yi Chu41326c12023-09-22 03:58:59 +0000158 libfooOutput.Args["linkFlags"])
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400159 }
160
161 if !android.InList("libstd", libfoo.Module().(*Module).Properties.AndroidMkDylibs) {
162 t.Errorf("Non-static libstd dylib expected to be a dependency of Rust shared libraries. Dylib deps are: %#v",
163 libfoo.Module().(*Module).Properties.AndroidMkDylibs)
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400164 }
165}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700166
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400167func TestSharedLibraryToc(t *testing.T) {
168 ctx := testRust(t, `
169 rust_ffi_shared {
170 name: "libfoo",
171 srcs: ["foo.rs"],
172 crate_name: "foo",
173 }
174 cc_binary {
175 name: "fizzbuzz",
176 shared_libs: ["libfoo"],
177 }`)
178
179 fizzbuzz := ctx.ModuleForTests("fizzbuzz", "android_arm64_armv8-a").Rule("ld")
180
181 if !android.SuffixInList(fizzbuzz.Implicits.Strings(), "libfoo.so.toc") {
182 t.Errorf("missing expected libfoo.so.toc implicit dependency, instead found: %#v",
183 fizzbuzz.Implicits.Strings())
184 }
185}
186
Ivan Lozano042504f2020-08-18 14:31:23 -0400187func TestStaticLibraryLinkage(t *testing.T) {
188 ctx := testRust(t, `
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400189 rust_ffi_static {
Ivan Lozano042504f2020-08-18 14:31:23 -0400190 name: "libfoo",
191 srcs: ["foo.rs"],
192 crate_name: "foo",
193 }`)
194
Ivan Lozano0a468a42024-05-13 21:03:34 -0400195 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_rlib-std")
Ivan Lozano042504f2020-08-18 14:31:23 -0400196
197 if !android.InList("libstd", libfoo.Module().(*Module).Properties.AndroidMkRlibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400198 t.Errorf("Static libstd rlib expected to be a dependency of Rust rlib libraries. Rlib deps are: %#v",
199 libfoo.Module().(*Module).Properties.AndroidMkDylibs)
200 }
Ivan Lozano042504f2020-08-18 14:31:23 -0400201}
202
Vinh Tran156ea442023-08-17 15:46:39 -0400203func TestNativeDependencyOfRlib(t *testing.T) {
204 ctx := testRust(t, `
Ivan Lozano0a468a42024-05-13 21:03:34 -0400205 rust_ffi_rlib {
206 name: "libffi_rlib",
207 crate_name: "ffi_rlib",
208 rlibs: ["librust_rlib"],
209 srcs: ["foo.rs"],
210 }
Vinh Tran156ea442023-08-17 15:46:39 -0400211 rust_library_rlib {
212 name: "librust_rlib",
213 crate_name: "rust_rlib",
214 srcs: ["foo.rs"],
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400215 shared_libs: ["libshared_cc_dep"],
216 static_libs: ["libstatic_cc_dep"],
Vinh Tran156ea442023-08-17 15:46:39 -0400217 }
218 cc_library_shared {
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400219 name: "libshared_cc_dep",
Vinh Tran156ea442023-08-17 15:46:39 -0400220 srcs: ["foo.cpp"],
221 }
222 cc_library_static {
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400223 name: "libstatic_cc_dep",
Vinh Tran156ea442023-08-17 15:46:39 -0400224 srcs: ["foo.cpp"],
225 }
226 `)
227
228 rustRlibRlibStd := ctx.ModuleForTests("librust_rlib", "android_arm64_armv8-a_rlib_rlib-std")
229 rustRlibDylibStd := ctx.ModuleForTests("librust_rlib", "android_arm64_armv8-a_rlib_dylib-std")
Ivan Lozano0a468a42024-05-13 21:03:34 -0400230 ffiRlib := ctx.ModuleForTests("libffi_rlib", "android_arm64_armv8-a_rlib_rlib-std")
Vinh Tran156ea442023-08-17 15:46:39 -0400231
232 modules := []android.TestingModule{
233 rustRlibRlibStd,
234 rustRlibDylibStd,
Ivan Lozano0a468a42024-05-13 21:03:34 -0400235 ffiRlib,
Vinh Tran156ea442023-08-17 15:46:39 -0400236 }
237
238 // librust_rlib specifies -L flag to cc deps output directory on rustc command
239 // and re-export the cc deps to rdep libffi_static
240 // When building rlib crate, rustc doesn't link the native libraries
241 // The build system assumes the cc deps will be at the final linkage (either a shared library or binary)
242 // Hence, these flags are no-op
243 // TODO: We could consider removing these flags
244 for _, module := range modules {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000245 if !strings.Contains(module.Rule("rustc").Args["libFlags"],
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400246 "-L out/soong/.intermediates/libshared_cc_dep/android_arm64_armv8-a_shared/") {
Vinh Tran156ea442023-08-17 15:46:39 -0400247 t.Errorf(
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400248 "missing -L flag for libshared_cc_dep of %s, rustcFlags: %#v",
249 module.Module().Name(), rustRlibRlibStd.Rule("rustc").Args["libFlags"],
Vinh Tran156ea442023-08-17 15:46:39 -0400250 )
251 }
Wen-yi Chu41326c12023-09-22 03:58:59 +0000252 if !strings.Contains(module.Rule("rustc").Args["libFlags"],
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400253 "-L out/soong/.intermediates/libstatic_cc_dep/android_arm64_armv8-a_static/") {
Vinh Tran156ea442023-08-17 15:46:39 -0400254 t.Errorf(
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400255 "missing -L flag for libstatic_cc_dep of %s, rustcFlags: %#v",
256 module.Module().Name(), rustRlibRlibStd.Rule("rustc").Args["libFlags"],
Vinh Tran156ea442023-08-17 15:46:39 -0400257 )
258 }
259 }
260}
261
Matthew Maurer0f003b12020-06-29 14:34:06 -0700262// Test that variants pull in the right type of rustlib autodep
263func TestAutoDeps(t *testing.T) {
264
265 ctx := testRust(t, `
Ivan Lozano2d407632022-04-07 12:59:11 -0400266 rust_library_host {
267 name: "libbar",
268 srcs: ["bar.rs"],
269 crate_name: "bar",
270 }
271 rust_library_host_rlib {
272 name: "librlib_only",
273 srcs: ["bar.rs"],
274 crate_name: "rlib_only",
275 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700276 rust_library_host {
277 name: "libfoo",
278 srcs: ["foo.rs"],
279 crate_name: "foo",
Ivan Lozano2d407632022-04-07 12:59:11 -0400280 rustlibs: [
281 "libbar",
282 "librlib_only",
283 ],
Matthew Maurer0f003b12020-06-29 14:34:06 -0700284 }
Ivan Lozano2d407632022-04-07 12:59:11 -0400285 rust_ffi_host {
286 name: "libfoo.ffi",
287 srcs: ["foo.rs"],
288 crate_name: "foo",
289 rustlibs: [
290 "libbar",
291 "librlib_only",
292 ],
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400293 }
294 rust_ffi_host_static {
295 name: "libfoo.ffi.static",
296 srcs: ["foo.rs"],
297 crate_name: "foo",
298 rustlibs: [
299 "libbar",
300 "librlib_only",
301 ],
Ivan Lozano2d407632022-04-07 12:59:11 -0400302 }`)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700303
Ivan Lozano2b081132020-09-08 12:46:52 -0400304 libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib_rlib-std")
Matthew Maurer0f003b12020-06-29 14:34:06 -0700305 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib")
Ivan Lozano0a468a42024-05-13 21:03:34 -0400306 libfooFFIRlib := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_rlib_rlib-std")
Matthew Maurer0f003b12020-06-29 14:34:06 -0700307 libfooShared := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_shared")
308
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400309 for _, static := range []android.TestingModule{libfooRlib, libfooFFIRlib} {
Ivan Lozano2b081132020-09-08 12:46:52 -0400310 if !android.InList("libbar.rlib-std", static.Module().(*Module).Properties.AndroidMkRlibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400311 t.Errorf("libbar not present as rlib dependency in static lib: %s", static.Module().Name())
Matthew Maurer0f003b12020-06-29 14:34:06 -0700312 }
313 if android.InList("libbar", static.Module().(*Module).Properties.AndroidMkDylibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400314 t.Errorf("libbar present as dynamic dependency in static lib: %s", static.Module().Name())
Matthew Maurer0f003b12020-06-29 14:34:06 -0700315 }
316 }
317
318 for _, dyn := range []android.TestingModule{libfooDylib, libfooShared} {
319 if !android.InList("libbar", dyn.Module().(*Module).Properties.AndroidMkDylibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400320 t.Errorf("libbar not present as dynamic dependency in dynamic lib: %s", dyn.Module().Name())
Matthew Maurer0f003b12020-06-29 14:34:06 -0700321 }
Ivan Lozano4df02572023-06-15 14:21:09 -0400322 if android.InList("libbar", dyn.Module().(*Module).Properties.AndroidMkRlibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400323 t.Errorf("libbar present as rlib dependency in dynamic lib: %s", dyn.Module().Name())
Matthew Maurer0f003b12020-06-29 14:34:06 -0700324 }
Ivan Lozano4df02572023-06-15 14:21:09 -0400325 if !android.InList("librlib_only", dyn.Module().(*Module).Properties.AndroidMkRlibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400326 t.Errorf("librlib_only should be selected by rustlibs as an rlib: %s.", dyn.Module().Name())
Ivan Lozano2d407632022-04-07 12:59:11 -0400327 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700328 }
329}
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200330
331// Test that stripped versions are correctly generated and used.
332func TestStrippedLibrary(t *testing.T) {
333 ctx := testRust(t, `
334 rust_library_dylib {
335 name: "libfoo",
336 crate_name: "foo",
337 srcs: ["foo.rs"],
338 }
339 rust_library_dylib {
340 name: "libbar",
341 crate_name: "bar",
342 srcs: ["foo.rs"],
343 strip: {
344 none: true
345 }
346 }
347 `)
348
349 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib")
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400350 foo.Output("libfoo.dylib.so")
351 foo.Output("unstripped/libfoo.dylib.so")
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200352 // Check that the `cp` rule is using the stripped version as input.
353 cp := foo.Rule("android.Cp")
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400354 if strings.HasSuffix(cp.Input.String(), "unstripped/libfoo.dylib.so") {
355 t.Errorf("installed library not based on stripped version: %v", cp.Input)
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200356 }
357
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400358 fizzBar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeOutput("unstripped/libbar.dylib.so")
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200359 if fizzBar.Rule != nil {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400360 t.Errorf("unstripped library exists, so stripped library has incorrectly been generated")
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200361 }
362}
Ivan Lozano2b081132020-09-08 12:46:52 -0400363
364func TestLibstdLinkage(t *testing.T) {
365 ctx := testRust(t, `
366 rust_library {
367 name: "libfoo",
368 srcs: ["foo.rs"],
369 crate_name: "foo",
370 }
371 rust_ffi {
372 name: "libbar",
373 srcs: ["foo.rs"],
374 crate_name: "bar",
375 rustlibs: ["libfoo"],
Ivan Lozanoea086132020-12-08 14:43:00 -0500376 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400377 rust_ffi_static {
378 name: "libbar_static",
379 srcs: ["foo.rs"],
380 crate_name: "bar",
381 rustlibs: ["libfoo"],
382 }
Ivan Lozanoea086132020-12-08 14:43:00 -0500383 rust_ffi {
384 name: "libbar.prefer_rlib",
385 srcs: ["foo.rs"],
386 crate_name: "bar",
387 rustlibs: ["libfoo"],
388 prefer_rlib: true,
Ivan Lozano2b081132020-09-08 12:46:52 -0400389 }`)
390
391 libfooDylib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
392 libfooRlibStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_rlib-std").Module().(*Module)
393 libfooRlibDynamic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Module().(*Module)
394
395 libbarShared := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module().(*Module)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400396 libbarFFIRlib := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_rlib_rlib-std").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400397
Ivan Lozanoea086132020-12-08 14:43:00 -0500398 // prefer_rlib works the same for both rust_library and rust_ffi, so a single check is sufficient here.
399 libbarRlibStd := ctx.ModuleForTests("libbar.prefer_rlib", "android_arm64_armv8-a_shared").Module().(*Module)
400
Ivan Lozano2b081132020-09-08 12:46:52 -0400401 if !android.InList("libstd", libfooRlibStatic.Properties.AndroidMkRlibs) {
402 t.Errorf("rlib-std variant for device rust_library_rlib does not link libstd as an rlib")
403 }
404 if !android.InList("libstd", libfooRlibDynamic.Properties.AndroidMkDylibs) {
405 t.Errorf("dylib-std variant for device rust_library_rlib does not link libstd as an dylib")
406 }
407 if !android.InList("libstd", libfooDylib.Properties.AndroidMkDylibs) {
408 t.Errorf("Device rust_library_dylib does not link libstd as an dylib")
409 }
410
411 if !android.InList("libstd", libbarShared.Properties.AndroidMkDylibs) {
412 t.Errorf("Device rust_ffi_shared does not link libstd as an dylib")
413 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400414 if !android.InList("libstd", libbarFFIRlib.Properties.AndroidMkRlibs) {
415 t.Errorf("Device rust_ffi_rlib does not link libstd as an rlib")
416 }
417 if !android.InList("libfoo.rlib-std", libbarFFIRlib.Properties.AndroidMkRlibs) {
418 t.Errorf("Device rust_ffi_rlib does not link dependent rustlib rlib-std variant")
419 }
Ivan Lozanoea086132020-12-08 14:43:00 -0500420 if !android.InList("libstd", libbarRlibStd.Properties.AndroidMkRlibs) {
421 t.Errorf("rust_ffi with prefer_rlib does not link libstd as an rlib")
422 }
423
Ivan Lozano2b081132020-09-08 12:46:52 -0400424}
Ivan Lozanof033ca62024-03-21 13:43:14 -0400425
426func TestRustFFIExportedIncludes(t *testing.T) {
427 ctx := testRust(t, `
428 rust_ffi {
429 name: "libbar",
430 srcs: ["foo.rs"],
431 crate_name: "bar",
432 export_include_dirs: ["rust_includes"],
433 host_supported: true,
434 }
435 cc_library_static {
436 name: "libfoo",
437 srcs: ["foo.cpp"],
438 shared_libs: ["libbar"],
439 host_supported: true,
440 }`)
441 libfooStatic := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_static").Rule("cc")
442 android.AssertStringDoesContain(t, "cFlags for lib module", libfooStatic.Args["cFlags"], " -Irust_includes ")
443}