blob: 1133c28edae5e200f3e742f3deacbd87c5809ea8 [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"
37 }`)
Ivan Lozanoffee3342019-08-27 12:03:00 -070038
Ivan Lozano52767be2019-10-18 14:49:46 -070039 // Test all variants are being built.
Ivan Lozano0a468a42024-05-13 21:03:34 -040040 libfooStatic := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_static").Rule("rustc")
Ivan Lozano8d10fc32021-11-05 16:36:47 -040041 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 Lozano0a468a42024-05-13 21:03:34 -040043 libfooFFIRlib := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_rlib_rlib-std").Rule("rustc")
Ivan Lozano8d10fc32021-11-05 16:36:47 -040044 libfooShared := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_shared").Rule("rustc")
Ivan Lozano52767be2019-10-18 14:49:46 -070045
46 rlibCrateType := "rlib"
47 dylibCrateType := "dylib"
48 sharedCrateType := "cdylib"
Martin Geislerb8a4c2c2022-11-18 11:52:57 +010049 staticCrateType := "staticlib"
Ivan Lozanoffee3342019-08-27 12:03:00 -070050
51 // Test crate type for rlib is correct.
Wen-yi Chu41326c12023-09-22 03:58:59 +000052 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 Lozanoffee3342019-08-27 12:03:00 -070054 }
55
56 // Test crate type for dylib is correct.
Wen-yi Chu41326c12023-09-22 03:58:59 +000057 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 Lozanoffee3342019-08-27 12:03:00 -070059 }
Ivan Lozano52767be2019-10-18 14:49:46 -070060
61 // Test crate type for C static libraries is correct.
Wen-yi Chu41326c12023-09-22 03:58:59 +000062 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 Lozano52767be2019-10-18 14:49:46 -070064 }
65
Ivan Lozano0a468a42024-05-13 21:03:34 -040066 // 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 Lozano52767be2019-10-18 14:49:46 -070071 // Test crate type for C shared libraries is correct.
Wen-yi Chu41326c12023-09-22 03:58:59 +000072 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 Lozano52767be2019-10-18 14:49:46 -070074 }
75
Ivan Lozanoffee3342019-08-27 12:03:00 -070076}
77
78// Test that dylibs are not statically linking the standard library.
79func 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 Chu41326c12023-09-22 03:58:59 +000087 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Rule("rustc")
Ivan Lozanoffee3342019-08-27 12:03:00 -070088
Wen-yi Chu41326c12023-09-22 03:58:59 +000089 if !strings.Contains(libfooDylib.Args["rustcFlags"], "prefer-dynamic") {
90 t.Errorf("missing prefer-dynamic flag for libfoo dylib, rustcFlags: %#v", libfooDylib.Args["rustcFlags"])
Ivan Lozanoffee3342019-08-27 12:03:00 -070091 }
92}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070093
Stephen Crane0dbfc562021-07-07 19:05:02 -070094// Check that we are passing the android_dylib config flag
95func 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 Chu41326c12023-09-22 03:58:59 +0000103 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Rule("rustc")
Stephen Crane0dbfc562021-07-07 19:05:02 -0700104
Wen-yi Chu41326c12023-09-22 03:58:59 +0000105 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 Crane0dbfc562021-07-07 19:05:02 -0700107 }
108}
109
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700110func 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 Lozanobec05ea2020-06-09 08:27:49 -0400146
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400147func TestSharedLibrary(t *testing.T) {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400148 ctx := testRust(t, `
Matthew Maurer2ae05132020-06-23 14:28:53 -0700149 rust_ffi_shared {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400150 name: "libfoo",
151 srcs: ["foo.rs"],
152 crate_name: "foo",
153 }`)
154
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400155 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared")
156
Colin Cross004bd3f2023-10-02 11:39:17 -0700157 libfooOutput := libfoo.Rule("rustc")
Wen-yi Chu41326c12023-09-22 03:58:59 +0000158 if !strings.Contains(libfooOutput.Args["linkFlags"], "-Wl,-soname=libfoo.so") {
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400159 t.Errorf("missing expected -Wl,-soname linker flag for libfoo shared lib, linkFlags: %#v",
Wen-yi Chu41326c12023-09-22 03:58:59 +0000160 libfooOutput.Args["linkFlags"])
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400161 }
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 Lozanobec05ea2020-06-09 08:27:49 -0400166 }
167}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700168
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400169func 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 Lozano042504f2020-08-18 14:31:23 -0400189func TestStaticLibraryLinkage(t *testing.T) {
190 ctx := testRust(t, `
Ivan Lozano0a468a42024-05-13 21:03:34 -0400191 rust_ffi {
Ivan Lozano042504f2020-08-18 14:31:23 -0400192 name: "libfoo",
193 srcs: ["foo.rs"],
194 crate_name: "foo",
195 }`)
196
Ivan Lozano0a468a42024-05-13 21:03:34 -0400197 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_rlib-std")
198 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
Ivan Lozano042504f2020-08-18 14:31:23 -0400199
200 if !android.InList("libstd", libfoo.Module().(*Module).Properties.AndroidMkRlibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400201 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 Lozano042504f2020-08-18 14:31:23 -0400205 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 Tran156ea442023-08-17 15:46:39 -0400210func TestNativeDependencyOfRlib(t *testing.T) {
211 ctx := testRust(t, `
Ivan Lozano0a468a42024-05-13 21:03:34 -0400212 rust_ffi_rlib {
213 name: "libffi_rlib",
214 crate_name: "ffi_rlib",
215 rlibs: ["librust_rlib"],
216 srcs: ["foo.rs"],
217 }
Vinh Tran156ea442023-08-17 15:46:39 -0400218 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 Lozano0a468a42024-05-13 21:03:34 -0400244 ffiRlib := ctx.ModuleForTests("libffi_rlib", "android_arm64_armv8-a_rlib_rlib-std")
Vinh Tran156ea442023-08-17 15:46:39 -0400245
246 modules := []android.TestingModule{
247 rustRlibRlibStd,
248 rustRlibDylibStd,
Ivan Lozano0a468a42024-05-13 21:03:34 -0400249 ffiRlib,
Vinh Tran156ea442023-08-17 15:46:39 -0400250 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 Chu41326c12023-09-22 03:58:59 +0000260 if !strings.Contains(module.Rule("rustc").Args["libFlags"],
261 "-L out/soong/.intermediates/shared_cc_dep/android_arm64_armv8-a_shared/") {
Vinh Tran156ea442023-08-17 15:46:39 -0400262 t.Errorf(
Wen-yi Chu41326c12023-09-22 03:58:59 +0000263 "missing -L flag for shared_cc_dep, rustcFlags: %#v",
264 rustRlibRlibStd.Rule("rustc").Args["libFlags"],
Vinh Tran156ea442023-08-17 15:46:39 -0400265 )
266 }
Wen-yi Chu41326c12023-09-22 03:58:59 +0000267 if !strings.Contains(module.Rule("rustc").Args["libFlags"],
268 "-L out/soong/.intermediates/static_cc_dep/android_arm64_armv8-a_static/") {
Vinh Tran156ea442023-08-17 15:46:39 -0400269 t.Errorf(
Wen-yi Chu41326c12023-09-22 03:58:59 +0000270 "missing -L flag for static_cc_dep, rustcFlags: %#v",
271 rustRlibRlibStd.Rule("rustc").Args["libFlags"],
Vinh Tran156ea442023-08-17 15:46:39 -0400272 )
273 }
274 }
275}
276
Matthew Maurer0f003b12020-06-29 14:34:06 -0700277// Test that variants pull in the right type of rustlib autodep
278func TestAutoDeps(t *testing.T) {
279
280 ctx := testRust(t, `
Ivan Lozano2d407632022-04-07 12:59:11 -0400281 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 Maurer0f003b12020-06-29 14:34:06 -0700291 rust_library_host {
292 name: "libfoo",
293 srcs: ["foo.rs"],
294 crate_name: "foo",
Ivan Lozano2d407632022-04-07 12:59:11 -0400295 rustlibs: [
296 "libbar",
297 "librlib_only",
298 ],
Matthew Maurer0f003b12020-06-29 14:34:06 -0700299 }
Ivan Lozano2d407632022-04-07 12:59:11 -0400300 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 Maurer0f003b12020-06-29 14:34:06 -0700309
Ivan Lozano2b081132020-09-08 12:46:52 -0400310 libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib_rlib-std")
Matthew Maurer0f003b12020-06-29 14:34:06 -0700311 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib")
Ivan Lozano0a468a42024-05-13 21:03:34 -0400312 libfooFFIRlib := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_rlib_rlib-std")
Matthew Maurer0f003b12020-06-29 14:34:06 -0700313 libfooStatic := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_static")
314 libfooShared := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_shared")
315
Ivan Lozano0a468a42024-05-13 21:03:34 -0400316 for _, static := range []android.TestingModule{libfooRlib, libfooStatic, libfooFFIRlib} {
Ivan Lozano2b081132020-09-08 12:46:52 -0400317 if !android.InList("libbar.rlib-std", static.Module().(*Module).Properties.AndroidMkRlibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400318 t.Errorf("libbar not present as rlib dependency in static lib: %s", static.Module().Name())
Matthew Maurer0f003b12020-06-29 14:34:06 -0700319 }
320 if android.InList("libbar", static.Module().(*Module).Properties.AndroidMkDylibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400321 t.Errorf("libbar present as dynamic dependency in static lib: %s", static.Module().Name())
Matthew Maurer0f003b12020-06-29 14:34:06 -0700322 }
323 }
324
325 for _, dyn := range []android.TestingModule{libfooDylib, libfooShared} {
326 if !android.InList("libbar", dyn.Module().(*Module).Properties.AndroidMkDylibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400327 t.Errorf("libbar not present as dynamic dependency in dynamic lib: %s", dyn.Module().Name())
Matthew Maurer0f003b12020-06-29 14:34:06 -0700328 }
Ivan Lozano4df02572023-06-15 14:21:09 -0400329 if android.InList("libbar", dyn.Module().(*Module).Properties.AndroidMkRlibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400330 t.Errorf("libbar present as rlib dependency in dynamic lib: %s", dyn.Module().Name())
Matthew Maurer0f003b12020-06-29 14:34:06 -0700331 }
Ivan Lozano4df02572023-06-15 14:21:09 -0400332 if !android.InList("librlib_only", dyn.Module().(*Module).Properties.AndroidMkRlibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400333 t.Errorf("librlib_only should be selected by rustlibs as an rlib: %s.", dyn.Module().Name())
Ivan Lozano2d407632022-04-07 12:59:11 -0400334 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700335 }
336}
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200337
338// Test that stripped versions are correctly generated and used.
339func 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 Lozano8d10fc32021-11-05 16:36:47 -0400357 foo.Output("libfoo.dylib.so")
358 foo.Output("unstripped/libfoo.dylib.so")
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200359 // Check that the `cp` rule is using the stripped version as input.
360 cp := foo.Rule("android.Cp")
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400361 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 Weksteenfabaff62020-08-27 13:48:36 +0200363 }
364
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400365 fizzBar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeOutput("unstripped/libbar.dylib.so")
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200366 if fizzBar.Rule != nil {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400367 t.Errorf("unstripped library exists, so stripped library has incorrectly been generated")
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200368 }
369}
Ivan Lozano2b081132020-09-08 12:46:52 -0400370
371func 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 Lozanoea086132020-12-08 14:43:00 -0500383 }
384 rust_ffi {
385 name: "libbar.prefer_rlib",
386 srcs: ["foo.rs"],
387 crate_name: "bar",
388 rustlibs: ["libfoo"],
389 prefer_rlib: true,
Ivan Lozano2b081132020-09-08 12:46:52 -0400390 }`)
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 Lozano0a468a42024-05-13 21:03:34 -0400398 libbarFFIRlib := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_rlib_rlib-std").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400399
Ivan Lozanoea086132020-12-08 14:43:00 -0500400 // 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 Lozano2b081132020-09-08 12:46:52 -0400403 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 Lozano0a468a42024-05-13 21:03:34 -0400422 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 Lozanoea086132020-12-08 14:43:00 -0500428 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 Lozano2b081132020-09-08 12:46:52 -0400432}
Ivan Lozanof033ca62024-03-21 13:43:14 -0400433
434func 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}