blob: e5fd5e018d14b91209279a14163b127ce13a500a [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
92func TestValidateLibraryStem(t *testing.T) {
93 testRustError(t, "crate_name must be defined.", `
94 rust_library_host {
95 name: "libfoo",
96 srcs: ["foo.rs"],
97 }`)
98
99 testRustError(t, "library crate_names must be alphanumeric with underscores allowed", `
100 rust_library_host {
101 name: "libfoo-bar",
102 srcs: ["foo.rs"],
103 crate_name: "foo-bar"
104 }`)
105
106 testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", `
107 rust_library_host {
108 name: "foobar",
109 srcs: ["foo.rs"],
110 crate_name: "foo_bar"
111 }`)
112 testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", `
113 rust_library_host {
114 name: "foobar",
115 stem: "libfoo",
116 srcs: ["foo.rs"],
117 crate_name: "foo_bar"
118 }`)
119 testRustError(t, "Invalid name or stem property; library filenames must start with lib<crate_name>", `
120 rust_library_host {
121 name: "foobar",
122 stem: "foo_bar",
123 srcs: ["foo.rs"],
124 crate_name: "foo_bar"
125 }`)
126
127}
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400128
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400129func TestSharedLibrary(t *testing.T) {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400130 ctx := testRust(t, `
Matthew Maurer2ae05132020-06-23 14:28:53 -0700131 rust_ffi_shared {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400132 name: "libfoo",
133 srcs: ["foo.rs"],
134 crate_name: "foo",
135 }`)
136
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400137 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared")
138
Colin Cross004bd3f2023-10-02 11:39:17 -0700139 libfooOutput := libfoo.Rule("rustc")
Wen-yi Chu41326c12023-09-22 03:58:59 +0000140 if !strings.Contains(libfooOutput.Args["linkFlags"], "-Wl,-soname=libfoo.so") {
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400141 t.Errorf("missing expected -Wl,-soname linker flag for libfoo shared lib, linkFlags: %#v",
Wen-yi Chu41326c12023-09-22 03:58:59 +0000142 libfooOutput.Args["linkFlags"])
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400143 }
144
145 if !android.InList("libstd", libfoo.Module().(*Module).Properties.AndroidMkDylibs) {
146 t.Errorf("Non-static libstd dylib expected to be a dependency of Rust shared libraries. Dylib deps are: %#v",
147 libfoo.Module().(*Module).Properties.AndroidMkDylibs)
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400148 }
149}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700150
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400151func TestSharedLibraryToc(t *testing.T) {
152 ctx := testRust(t, `
153 rust_ffi_shared {
154 name: "libfoo",
155 srcs: ["foo.rs"],
156 crate_name: "foo",
157 }
158 cc_binary {
159 name: "fizzbuzz",
160 shared_libs: ["libfoo"],
161 }`)
162
163 fizzbuzz := ctx.ModuleForTests("fizzbuzz", "android_arm64_armv8-a").Rule("ld")
164
165 if !android.SuffixInList(fizzbuzz.Implicits.Strings(), "libfoo.so.toc") {
166 t.Errorf("missing expected libfoo.so.toc implicit dependency, instead found: %#v",
167 fizzbuzz.Implicits.Strings())
168 }
169}
170
Ivan Lozano042504f2020-08-18 14:31:23 -0400171func TestStaticLibraryLinkage(t *testing.T) {
172 ctx := testRust(t, `
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400173 rust_ffi_static {
Ivan Lozano042504f2020-08-18 14:31:23 -0400174 name: "libfoo",
175 srcs: ["foo.rs"],
176 crate_name: "foo",
177 }`)
178
Ivan Lozano0a468a42024-05-13 21:03:34 -0400179 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_rlib-std")
Ivan Lozano042504f2020-08-18 14:31:23 -0400180
181 if !android.InList("libstd", libfoo.Module().(*Module).Properties.AndroidMkRlibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400182 t.Errorf("Static libstd rlib expected to be a dependency of Rust rlib libraries. Rlib deps are: %#v",
183 libfoo.Module().(*Module).Properties.AndroidMkDylibs)
184 }
Ivan Lozano042504f2020-08-18 14:31:23 -0400185}
186
Vinh Tran156ea442023-08-17 15:46:39 -0400187func TestNativeDependencyOfRlib(t *testing.T) {
188 ctx := testRust(t, `
Ivan Lozano61848422024-12-13 19:45:00 +0000189 rust_ffi_static {
190 name: "libffi_static",
191 crate_name: "ffi_static",
Ivan Lozano0a468a42024-05-13 21:03:34 -0400192 rlibs: ["librust_rlib"],
193 srcs: ["foo.rs"],
194 }
Vinh Tran156ea442023-08-17 15:46:39 -0400195 rust_library_rlib {
196 name: "librust_rlib",
197 crate_name: "rust_rlib",
198 srcs: ["foo.rs"],
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400199 shared_libs: ["libshared_cc_dep"],
200 static_libs: ["libstatic_cc_dep"],
Vinh Tran156ea442023-08-17 15:46:39 -0400201 }
202 cc_library_shared {
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400203 name: "libshared_cc_dep",
Vinh Tran156ea442023-08-17 15:46:39 -0400204 srcs: ["foo.cpp"],
205 }
206 cc_library_static {
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400207 name: "libstatic_cc_dep",
Vinh Tran156ea442023-08-17 15:46:39 -0400208 srcs: ["foo.cpp"],
209 }
210 `)
211
212 rustRlibRlibStd := ctx.ModuleForTests("librust_rlib", "android_arm64_armv8-a_rlib_rlib-std")
213 rustRlibDylibStd := ctx.ModuleForTests("librust_rlib", "android_arm64_armv8-a_rlib_dylib-std")
Ivan Lozano61848422024-12-13 19:45:00 +0000214 ffiRlib := ctx.ModuleForTests("libffi_static", "android_arm64_armv8-a_rlib_rlib-std")
Vinh Tran156ea442023-08-17 15:46:39 -0400215
216 modules := []android.TestingModule{
217 rustRlibRlibStd,
218 rustRlibDylibStd,
Ivan Lozano0a468a42024-05-13 21:03:34 -0400219 ffiRlib,
Vinh Tran156ea442023-08-17 15:46:39 -0400220 }
221
222 // librust_rlib specifies -L flag to cc deps output directory on rustc command
223 // and re-export the cc deps to rdep libffi_static
224 // When building rlib crate, rustc doesn't link the native libraries
225 // The build system assumes the cc deps will be at the final linkage (either a shared library or binary)
226 // Hence, these flags are no-op
227 // TODO: We could consider removing these flags
228 for _, module := range modules {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000229 if !strings.Contains(module.Rule("rustc").Args["libFlags"],
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400230 "-L out/soong/.intermediates/libshared_cc_dep/android_arm64_armv8-a_shared/") {
Vinh Tran156ea442023-08-17 15:46:39 -0400231 t.Errorf(
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400232 "missing -L flag for libshared_cc_dep of %s, rustcFlags: %#v",
233 module.Module().Name(), rustRlibRlibStd.Rule("rustc").Args["libFlags"],
Vinh Tran156ea442023-08-17 15:46:39 -0400234 )
235 }
Wen-yi Chu41326c12023-09-22 03:58:59 +0000236 if !strings.Contains(module.Rule("rustc").Args["libFlags"],
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400237 "-L out/soong/.intermediates/libstatic_cc_dep/android_arm64_armv8-a_static/") {
Vinh Tran156ea442023-08-17 15:46:39 -0400238 t.Errorf(
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400239 "missing -L flag for libstatic_cc_dep of %s, rustcFlags: %#v",
240 module.Module().Name(), rustRlibRlibStd.Rule("rustc").Args["libFlags"],
Vinh Tran156ea442023-08-17 15:46:39 -0400241 )
242 }
243 }
244}
245
Matthew Maurer0f003b12020-06-29 14:34:06 -0700246// Test that variants pull in the right type of rustlib autodep
247func TestAutoDeps(t *testing.T) {
248
249 ctx := testRust(t, `
Ivan Lozano2d407632022-04-07 12:59:11 -0400250 rust_library_host {
251 name: "libbar",
252 srcs: ["bar.rs"],
253 crate_name: "bar",
254 }
255 rust_library_host_rlib {
256 name: "librlib_only",
257 srcs: ["bar.rs"],
258 crate_name: "rlib_only",
259 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700260 rust_library_host {
261 name: "libfoo",
262 srcs: ["foo.rs"],
263 crate_name: "foo",
Ivan Lozano2d407632022-04-07 12:59:11 -0400264 rustlibs: [
265 "libbar",
266 "librlib_only",
267 ],
Matthew Maurer0f003b12020-06-29 14:34:06 -0700268 }
Ivan Lozano2d407632022-04-07 12:59:11 -0400269 rust_ffi_host {
270 name: "libfoo.ffi",
271 srcs: ["foo.rs"],
272 crate_name: "foo",
273 rustlibs: [
274 "libbar",
275 "librlib_only",
276 ],
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400277 }
278 rust_ffi_host_static {
279 name: "libfoo.ffi.static",
280 srcs: ["foo.rs"],
281 crate_name: "foo",
282 rustlibs: [
283 "libbar",
284 "librlib_only",
285 ],
Ivan Lozano2d407632022-04-07 12:59:11 -0400286 }`)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700287
Ivan Lozano2b081132020-09-08 12:46:52 -0400288 libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib_rlib-std")
Matthew Maurer0f003b12020-06-29 14:34:06 -0700289 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib")
Ivan Lozano0a468a42024-05-13 21:03:34 -0400290 libfooFFIRlib := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_rlib_rlib-std")
Matthew Maurer0f003b12020-06-29 14:34:06 -0700291 libfooShared := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_shared")
292
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400293 for _, static := range []android.TestingModule{libfooRlib, libfooFFIRlib} {
Ivan Lozano2b081132020-09-08 12:46:52 -0400294 if !android.InList("libbar.rlib-std", static.Module().(*Module).Properties.AndroidMkRlibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400295 t.Errorf("libbar not present as rlib dependency in static lib: %s", static.Module().Name())
Matthew Maurer0f003b12020-06-29 14:34:06 -0700296 }
297 if android.InList("libbar", static.Module().(*Module).Properties.AndroidMkDylibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400298 t.Errorf("libbar present as dynamic dependency in static lib: %s", static.Module().Name())
Matthew Maurer0f003b12020-06-29 14:34:06 -0700299 }
300 }
301
302 for _, dyn := range []android.TestingModule{libfooDylib, libfooShared} {
303 if !android.InList("libbar", dyn.Module().(*Module).Properties.AndroidMkDylibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400304 t.Errorf("libbar not present as dynamic dependency in dynamic lib: %s", dyn.Module().Name())
Matthew Maurer0f003b12020-06-29 14:34:06 -0700305 }
Ivan Lozano4df02572023-06-15 14:21:09 -0400306 if android.InList("libbar", dyn.Module().(*Module).Properties.AndroidMkRlibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400307 t.Errorf("libbar present as rlib dependency in dynamic lib: %s", dyn.Module().Name())
Matthew Maurer0f003b12020-06-29 14:34:06 -0700308 }
Ivan Lozano4df02572023-06-15 14:21:09 -0400309 if !android.InList("librlib_only", dyn.Module().(*Module).Properties.AndroidMkRlibs) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400310 t.Errorf("librlib_only should be selected by rustlibs as an rlib: %s.", dyn.Module().Name())
Ivan Lozano2d407632022-04-07 12:59:11 -0400311 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700312 }
313}
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200314
315// Test that stripped versions are correctly generated and used.
316func TestStrippedLibrary(t *testing.T) {
317 ctx := testRust(t, `
318 rust_library_dylib {
319 name: "libfoo",
320 crate_name: "foo",
321 srcs: ["foo.rs"],
322 }
323 rust_library_dylib {
324 name: "libbar",
325 crate_name: "bar",
326 srcs: ["foo.rs"],
327 strip: {
328 none: true
329 }
330 }
331 `)
332
333 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib")
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400334 foo.Output("libfoo.dylib.so")
335 foo.Output("unstripped/libfoo.dylib.so")
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200336 // Check that the `cp` rule is using the stripped version as input.
337 cp := foo.Rule("android.Cp")
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400338 if strings.HasSuffix(cp.Input.String(), "unstripped/libfoo.dylib.so") {
339 t.Errorf("installed library not based on stripped version: %v", cp.Input)
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200340 }
341
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400342 fizzBar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeOutput("unstripped/libbar.dylib.so")
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200343 if fizzBar.Rule != nil {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400344 t.Errorf("unstripped library exists, so stripped library has incorrectly been generated")
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200345 }
346}
Ivan Lozano2b081132020-09-08 12:46:52 -0400347
348func TestLibstdLinkage(t *testing.T) {
349 ctx := testRust(t, `
350 rust_library {
351 name: "libfoo",
352 srcs: ["foo.rs"],
353 crate_name: "foo",
354 }
355 rust_ffi {
356 name: "libbar",
357 srcs: ["foo.rs"],
358 crate_name: "bar",
359 rustlibs: ["libfoo"],
Ivan Lozanoea086132020-12-08 14:43:00 -0500360 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400361 rust_ffi_static {
362 name: "libbar_static",
363 srcs: ["foo.rs"],
364 crate_name: "bar",
365 rustlibs: ["libfoo"],
366 }
Ivan Lozanoea086132020-12-08 14:43:00 -0500367 rust_ffi {
368 name: "libbar.prefer_rlib",
369 srcs: ["foo.rs"],
370 crate_name: "bar",
371 rustlibs: ["libfoo"],
372 prefer_rlib: true,
Ivan Lozano2b081132020-09-08 12:46:52 -0400373 }`)
374
375 libfooDylib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
376 libfooRlibStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_rlib-std").Module().(*Module)
377 libfooRlibDynamic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Module().(*Module)
378
379 libbarShared := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module().(*Module)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400380 libbarFFIRlib := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_rlib_rlib-std").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400381
Ivan Lozanoea086132020-12-08 14:43:00 -0500382 // prefer_rlib works the same for both rust_library and rust_ffi, so a single check is sufficient here.
383 libbarRlibStd := ctx.ModuleForTests("libbar.prefer_rlib", "android_arm64_armv8-a_shared").Module().(*Module)
384
Ivan Lozano2b081132020-09-08 12:46:52 -0400385 if !android.InList("libstd", libfooRlibStatic.Properties.AndroidMkRlibs) {
386 t.Errorf("rlib-std variant for device rust_library_rlib does not link libstd as an rlib")
387 }
388 if !android.InList("libstd", libfooRlibDynamic.Properties.AndroidMkDylibs) {
389 t.Errorf("dylib-std variant for device rust_library_rlib does not link libstd as an dylib")
390 }
391 if !android.InList("libstd", libfooDylib.Properties.AndroidMkDylibs) {
392 t.Errorf("Device rust_library_dylib does not link libstd as an dylib")
393 }
394
395 if !android.InList("libstd", libbarShared.Properties.AndroidMkDylibs) {
396 t.Errorf("Device rust_ffi_shared does not link libstd as an dylib")
397 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400398 if !android.InList("libstd", libbarFFIRlib.Properties.AndroidMkRlibs) {
Ivan Lozano61848422024-12-13 19:45:00 +0000399 t.Errorf("Device rust_ffi_static does not link libstd as an rlib")
Ivan Lozano0a468a42024-05-13 21:03:34 -0400400 }
401 if !android.InList("libfoo.rlib-std", libbarFFIRlib.Properties.AndroidMkRlibs) {
Ivan Lozano61848422024-12-13 19:45:00 +0000402 t.Errorf("Device rust_ffi_static does not link dependent rustlib rlib-std variant")
Ivan Lozano0a468a42024-05-13 21:03:34 -0400403 }
Ivan Lozanoea086132020-12-08 14:43:00 -0500404 if !android.InList("libstd", libbarRlibStd.Properties.AndroidMkRlibs) {
405 t.Errorf("rust_ffi with prefer_rlib does not link libstd as an rlib")
406 }
407
Ivan Lozano2b081132020-09-08 12:46:52 -0400408}
Ivan Lozanof033ca62024-03-21 13:43:14 -0400409
410func TestRustFFIExportedIncludes(t *testing.T) {
411 ctx := testRust(t, `
412 rust_ffi {
413 name: "libbar",
414 srcs: ["foo.rs"],
415 crate_name: "bar",
416 export_include_dirs: ["rust_includes"],
417 host_supported: true,
418 }
419 cc_library_static {
420 name: "libfoo",
421 srcs: ["foo.cpp"],
422 shared_libs: ["libbar"],
423 host_supported: true,
424 }`)
425 libfooStatic := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_static").Rule("cc")
426 android.AssertStringDoesContain(t, "cFlags for lib module", libfooStatic.Args["cFlags"], " -Irust_includes ")
427}
Ivan Lozanof4589012024-11-20 22:18:11 +0000428
429func TestRustVersionScript(t *testing.T) {
430 ctx := testRust(t, `
431 rust_library {
432 name: "librs",
433 srcs: ["bar.rs"],
434 crate_name: "rs",
435 extra_exported_symbols: "librs.map.txt",
436 }
437 rust_ffi {
438 name: "libffi",
439 srcs: ["foo.rs"],
440 crate_name: "ffi",
441 version_script: "libffi.map.txt",
442 }
443 `)
444
445 //linkFlags
446 librs := ctx.ModuleForTests("librs", "android_arm64_armv8-a_dylib").Rule("rustc")
447 libffi := ctx.ModuleForTests("libffi", "android_arm64_armv8-a_shared").Rule("rustc")
448
449 if !strings.Contains(librs.Args["linkFlags"], "-Wl,--version-script=librs.map.txt") {
450 t.Errorf("missing expected -Wl,--version-script= linker flag for libextended shared lib, linkFlags: %#v",
451 librs.Args["linkFlags"])
452 }
453 if strings.Contains(librs.Args["linkFlags"], "-Wl,--android-version-script=librs.map.txt") {
454 t.Errorf("unexpected -Wl,--android-version-script= linker flag for libextended shared lib, linkFlags: %#v",
455 librs.Args["linkFlags"])
456 }
457
458 if !strings.Contains(libffi.Args["linkFlags"], "-Wl,--android-version-script=libffi.map.txt") {
459 t.Errorf("missing -Wl,--android-version-script= linker flag for libreplaced shared lib, linkFlags: %#v",
460 libffi.Args["linkFlags"])
461 }
462 if strings.Contains(libffi.Args["linkFlags"], "-Wl,--version-script=libffi.map.txt") {
463 t.Errorf("unexpected -Wl,--version-script= linker flag for libextended shared lib, linkFlags: %#v",
464 libffi.Args["linkFlags"])
465 }
466}
467
468func TestRustVersionScriptPropertyErrors(t *testing.T) {
469 testRustError(t, "version_script: can only be set for rust_ffi modules", `
470 rust_library {
471 name: "librs",
472 srcs: ["bar.rs"],
473 crate_name: "rs",
474 version_script: "libbar.map.txt",
475 }`)
476 testRustError(t, "version_script and extra_exported_symbols", `
477 rust_ffi {
478 name: "librs",
479 srcs: ["bar.rs"],
480 crate_name: "rs",
481 version_script: "libbar.map.txt",
482 extra_exported_symbols: "libbar.map.txt",
483 }`)
484}