blob: dab9381597aed9b3a2d6b9d10cdcea463ae6ce02 [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 Lozano8d10fc32021-11-05 16:36:47 -040040 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 Lozano52767be2019-10-18 14:49:46 -070044
45 rlibCrateType := "rlib"
46 dylibCrateType := "dylib"
47 sharedCrateType := "cdylib"
Martin Geislerb8a4c2c2022-11-18 11:52:57 +010048 staticCrateType := "staticlib"
Ivan Lozanoffee3342019-08-27 12:03:00 -070049
50 // Test crate type for rlib is correct.
Sam Delmericoa588d152023-06-16 10:28:04 -040051 if !strings.Contains(libfooRlib.RuleParams.Command, "crate-type="+rlibCrateType) {
52 t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", rlibCrateType, libfooRlib.RuleParams.Command)
Ivan Lozanoffee3342019-08-27 12:03:00 -070053 }
54
55 // Test crate type for dylib is correct.
Sam Delmericoa588d152023-06-16 10:28:04 -040056 if !strings.Contains(libfooDylib.RuleParams.Command, "crate-type="+dylibCrateType) {
57 t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", dylibCrateType, libfooDylib.RuleParams.Command)
Ivan Lozanoffee3342019-08-27 12:03:00 -070058 }
Ivan Lozano52767be2019-10-18 14:49:46 -070059
60 // Test crate type for C static libraries is correct.
Sam Delmericoa588d152023-06-16 10:28:04 -040061 if !strings.Contains(libfooStatic.RuleParams.Command, "crate-type="+staticCrateType) {
62 t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", staticCrateType, libfooStatic.RuleParams.Command)
Ivan Lozano52767be2019-10-18 14:49:46 -070063 }
64
65 // Test crate type for C shared libraries is correct.
Sam Delmericoa588d152023-06-16 10:28:04 -040066 if !strings.Contains(libfooShared.RuleParams.Command, "crate-type="+sharedCrateType) {
67 t.Errorf("missing crate-type for shared variant, expecting %#v, got rustcFlags: %#v", sharedCrateType, libfooShared.RuleParams.Command)
Ivan Lozano52767be2019-10-18 14:49:46 -070068 }
69
Ivan Lozanoffee3342019-08-27 12:03:00 -070070}
71
72// Test that dylibs are not statically linking the standard library.
73func 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
Sam Delmericoa588d152023-06-16 10:28:04 -040081 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Description("rustc")
Ivan Lozanoffee3342019-08-27 12:03:00 -070082
Sam Delmericoa588d152023-06-16 10:28:04 -040083 if !strings.Contains(libfooDylib.RuleParams.Command, "prefer-dynamic") {
84 t.Errorf("missing prefer-dynamic flag for libfoo dylib, rustcFlags: %#v", libfooDylib.RuleParams.Command)
Ivan Lozanoffee3342019-08-27 12:03:00 -070085 }
86}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070087
Stephen Crane0dbfc562021-07-07 19:05:02 -070088// Check that we are passing the android_dylib config flag
89func 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
Sam Delmericoa588d152023-06-16 10:28:04 -040097 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Description("rustc")
Stephen Crane0dbfc562021-07-07 19:05:02 -070098
Sam Delmericoa588d152023-06-16 10:28:04 -040099 if !strings.Contains(libfooDylib.RuleParams.Command, "--cfg 'android_dylib'") {
100 t.Errorf("missing android_dylib cfg flag for libfoo dylib, rustcFlags: %#v", libfooDylib.RuleParams.Command)
Stephen Crane0dbfc562021-07-07 19:05:02 -0700101 }
102}
103
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700104func 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 Lozanobec05ea2020-06-09 08:27:49 -0400140
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400141func TestSharedLibrary(t *testing.T) {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400142 ctx := testRust(t, `
Matthew Maurer2ae05132020-06-23 14:28:53 -0700143 rust_ffi_shared {
Ivan Lozanobec05ea2020-06-09 08:27:49 -0400144 name: "libfoo",
145 srcs: ["foo.rs"],
146 crate_name: "foo",
147 }`)
148
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400149 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared")
150
Sam Delmericoa588d152023-06-16 10:28:04 -0400151 libfooOutput := libfoo.Rule("rustc")
152 if !strings.Contains(libfooOutput.RuleParams.Command, "-Wl,-soname=libfoo.so") {
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400153 t.Errorf("missing expected -Wl,-soname linker flag for libfoo shared lib, linkFlags: %#v",
Sam Delmericoa588d152023-06-16 10:28:04 -0400154 libfooOutput.RuleParams.Command)
Ivan Lozano7e741cc2020-06-19 12:32:30 -0400155 }
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 Lozanobec05ea2020-06-09 08:27:49 -0400160 }
161}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700162
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400163func 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 Lozano042504f2020-08-18 14:31:23 -0400183func 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 Tran156ea442023-08-17 15:46:39 -0400199func 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
Sam Delmericoa588d152023-06-16 10:28:04 -0400240 expectedSharedFlag := "-L out/soong/.intermediates/shared_cc_dep/android_arm64_armv8-a_shared"
241 expectedStaticFlag := "-L out/soong/.intermediates/static_cc_dep/android_arm64_armv8-a_static"
Vinh Tran156ea442023-08-17 15:46:39 -0400242 for _, module := range modules {
Sam Delmericoa588d152023-06-16 10:28:04 -0400243 if !strings.Contains(module.Rule("rustc").RuleParams.Command, expectedSharedFlag) {
Vinh Tran156ea442023-08-17 15:46:39 -0400244 t.Errorf(
Sam Delmericoa588d152023-06-16 10:28:04 -0400245 "expected to find shared library linkdir flag %q, rustcFlags: %#v",
246 expectedSharedFlag,
247 rustRlibRlibStd.Rule("rustc").RuleParams.Command,
Vinh Tran156ea442023-08-17 15:46:39 -0400248 )
249 }
Sam Delmericoa588d152023-06-16 10:28:04 -0400250 if !strings.Contains(module.Rule("rustc").RuleParams.Command, expectedStaticFlag) {
Vinh Tran156ea442023-08-17 15:46:39 -0400251 t.Errorf(
Sam Delmericoa588d152023-06-16 10:28:04 -0400252 "expected to find static library linkdir flag %q, rustcFlags: %#v",
253 expectedStaticFlag,
254 rustRlibRlibStd.Rule("rustc").RuleParams.Command,
Vinh Tran156ea442023-08-17 15:46:39 -0400255 )
256 }
257 }
258}
259
Matthew Maurer0f003b12020-06-29 14:34:06 -0700260// Test that variants pull in the right type of rustlib autodep
261func TestAutoDeps(t *testing.T) {
262
263 ctx := testRust(t, `
Ivan Lozano2d407632022-04-07 12:59:11 -0400264 rust_library_host {
265 name: "libbar",
266 srcs: ["bar.rs"],
267 crate_name: "bar",
268 }
269 rust_library_host_rlib {
270 name: "librlib_only",
271 srcs: ["bar.rs"],
272 crate_name: "rlib_only",
273 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700274 rust_library_host {
275 name: "libfoo",
276 srcs: ["foo.rs"],
277 crate_name: "foo",
Ivan Lozano2d407632022-04-07 12:59:11 -0400278 rustlibs: [
279 "libbar",
280 "librlib_only",
281 ],
Matthew Maurer0f003b12020-06-29 14:34:06 -0700282 }
Ivan Lozano2d407632022-04-07 12:59:11 -0400283 rust_ffi_host {
284 name: "libfoo.ffi",
285 srcs: ["foo.rs"],
286 crate_name: "foo",
287 rustlibs: [
288 "libbar",
289 "librlib_only",
290 ],
291 }`)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700292
Ivan Lozano2b081132020-09-08 12:46:52 -0400293 libfooRlib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_rlib_rlib-std")
Matthew Maurer0f003b12020-06-29 14:34:06 -0700294 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib")
295 libfooStatic := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_static")
296 libfooShared := ctx.ModuleForTests("libfoo.ffi", "linux_glibc_x86_64_shared")
297
298 for _, static := range []android.TestingModule{libfooRlib, libfooStatic} {
Ivan Lozano2b081132020-09-08 12:46:52 -0400299 if !android.InList("libbar.rlib-std", static.Module().(*Module).Properties.AndroidMkRlibs) {
300 t.Errorf("libbar not present as rlib dependency in static lib")
Matthew Maurer0f003b12020-06-29 14:34:06 -0700301 }
302 if android.InList("libbar", static.Module().(*Module).Properties.AndroidMkDylibs) {
303 t.Errorf("libbar present as dynamic dependency in static lib")
304 }
305 }
306
307 for _, dyn := range []android.TestingModule{libfooDylib, libfooShared} {
308 if !android.InList("libbar", dyn.Module().(*Module).Properties.AndroidMkDylibs) {
309 t.Errorf("libbar not present as dynamic dependency in dynamic lib")
310 }
Ivan Lozano4df02572023-06-15 14:21:09 -0400311 if android.InList("libbar", dyn.Module().(*Module).Properties.AndroidMkRlibs) {
Ivan Lozano2b081132020-09-08 12:46:52 -0400312 t.Errorf("libbar present as rlib dependency in dynamic lib")
Matthew Maurer0f003b12020-06-29 14:34:06 -0700313 }
Ivan Lozano4df02572023-06-15 14:21:09 -0400314 if !android.InList("librlib_only", dyn.Module().(*Module).Properties.AndroidMkRlibs) {
Ivan Lozano2d407632022-04-07 12:59:11 -0400315 t.Errorf("librlib_only should be selected by rustlibs as an rlib.")
316 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700317 }
318}
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200319
320// Test that stripped versions are correctly generated and used.
321func TestStrippedLibrary(t *testing.T) {
322 ctx := testRust(t, `
323 rust_library_dylib {
324 name: "libfoo",
325 crate_name: "foo",
326 srcs: ["foo.rs"],
327 }
328 rust_library_dylib {
329 name: "libbar",
330 crate_name: "bar",
331 srcs: ["foo.rs"],
332 strip: {
333 none: true
334 }
335 }
336 `)
337
338 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib")
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400339 foo.Output("libfoo.dylib.so")
340 foo.Output("unstripped/libfoo.dylib.so")
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200341 // Check that the `cp` rule is using the stripped version as input.
342 cp := foo.Rule("android.Cp")
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400343 if strings.HasSuffix(cp.Input.String(), "unstripped/libfoo.dylib.so") {
344 t.Errorf("installed library not based on stripped version: %v", cp.Input)
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200345 }
346
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400347 fizzBar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeOutput("unstripped/libbar.dylib.so")
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200348 if fizzBar.Rule != nil {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400349 t.Errorf("unstripped library exists, so stripped library has incorrectly been generated")
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200350 }
351}
Ivan Lozano2b081132020-09-08 12:46:52 -0400352
353func TestLibstdLinkage(t *testing.T) {
354 ctx := testRust(t, `
355 rust_library {
356 name: "libfoo",
357 srcs: ["foo.rs"],
358 crate_name: "foo",
359 }
360 rust_ffi {
361 name: "libbar",
362 srcs: ["foo.rs"],
363 crate_name: "bar",
364 rustlibs: ["libfoo"],
Ivan Lozanoea086132020-12-08 14:43:00 -0500365 }
366 rust_ffi {
367 name: "libbar.prefer_rlib",
368 srcs: ["foo.rs"],
369 crate_name: "bar",
370 rustlibs: ["libfoo"],
371 prefer_rlib: true,
Ivan Lozano2b081132020-09-08 12:46:52 -0400372 }`)
373
374 libfooDylib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
375 libfooRlibStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_rlib-std").Module().(*Module)
376 libfooRlibDynamic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Module().(*Module)
377
378 libbarShared := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module().(*Module)
379 libbarStatic := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Module().(*Module)
380
Ivan Lozanoea086132020-12-08 14:43:00 -0500381 // prefer_rlib works the same for both rust_library and rust_ffi, so a single check is sufficient here.
382 libbarRlibStd := ctx.ModuleForTests("libbar.prefer_rlib", "android_arm64_armv8-a_shared").Module().(*Module)
383
Ivan Lozano2b081132020-09-08 12:46:52 -0400384 if !android.InList("libstd", libfooRlibStatic.Properties.AndroidMkRlibs) {
385 t.Errorf("rlib-std variant for device rust_library_rlib does not link libstd as an rlib")
386 }
387 if !android.InList("libstd", libfooRlibDynamic.Properties.AndroidMkDylibs) {
388 t.Errorf("dylib-std variant for device rust_library_rlib does not link libstd as an dylib")
389 }
390 if !android.InList("libstd", libfooDylib.Properties.AndroidMkDylibs) {
391 t.Errorf("Device rust_library_dylib does not link libstd as an dylib")
392 }
393
394 if !android.InList("libstd", libbarShared.Properties.AndroidMkDylibs) {
395 t.Errorf("Device rust_ffi_shared does not link libstd as an dylib")
396 }
397 if !android.InList("libstd", libbarStatic.Properties.AndroidMkRlibs) {
398 t.Errorf("Device rust_ffi_static does not link libstd as an rlib")
399 }
400 if !android.InList("libfoo.rlib-std", libbarStatic.Properties.AndroidMkRlibs) {
401 t.Errorf("Device rust_ffi_static does not link dependent rustlib rlib-std variant")
402 }
Ivan Lozanoea086132020-12-08 14:43:00 -0500403 if !android.InList("libstd", libbarRlibStd.Properties.AndroidMkRlibs) {
404 t.Errorf("rust_ffi with prefer_rlib does not link libstd as an rlib")
405 }
406
Ivan Lozano2b081132020-09-08 12:46:52 -0400407}