blob: fbb994752f3ec3dfb008c66d178c58c7c2851a64 [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 (
Ivan Lozanoffee3342019-08-27 12:03:00 -070018 "os"
Ivan Lozanoc0083612019-09-03 13:49:39 -070019 "runtime"
Ivan Lozanob9040d62019-09-24 13:23:50 -070020 "strings"
Ivan Lozanoffee3342019-08-27 12:03:00 -070021 "testing"
22
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040023 "github.com/google/blueprint/proptools"
24
Ivan Lozanoffee3342019-08-27 12:03:00 -070025 "android/soong/android"
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000026 "android/soong/genrule"
Ivan Lozanoffee3342019-08-27 12:03:00 -070027)
28
Ivan Lozanoffee3342019-08-27 12:03:00 -070029func TestMain(m *testing.M) {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000030 os.Exit(m.Run())
31}
Ivan Lozanoffee3342019-08-27 12:03:00 -070032
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000033var prepareForRustTest = android.GroupFixturePreparers(
34 android.PrepareForTestWithArchMutator,
35 android.PrepareForTestWithDefaults,
36 android.PrepareForTestWithPrebuilts,
Ivan Lozanoffee3342019-08-27 12:03:00 -070037
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000038 genrule.PrepareForTestWithGenRuleBuildComponents,
39
Kiyoung Kim1db4a742024-04-01 15:50:01 +090040 PrepareForIntegrationTestWithRust,
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000041)
42
43var rustMockedFiles = android.MockFS{
Ivan Lozano1921e802021-05-20 13:39:16 -040044 "foo.rs": nil,
45 "foo.c": nil,
46 "src/bar.rs": nil,
47 "src/any.h": nil,
48 "c_includes/c_header.h": nil,
49 "rust_includes/rust_headers.h": nil,
50 "proto.proto": nil,
51 "proto/buf.proto": nil,
52 "buf.proto": nil,
53 "foo.proto": nil,
54 "liby.so": nil,
55 "libz.so": nil,
56 "data.txt": nil,
Ivan Lozano3149e6e2021-06-01 15:09:53 -040057 "liblog.map.txt": nil,
Ivan Lozanoffee3342019-08-27 12:03:00 -070058}
59
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020060// testRust returns a TestContext in which a basic environment has been setup.
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000061// This environment contains a few mocked files. See rustMockedFiles for the list of these files.
Wen-yi Chu41326c12023-09-22 03:58:59 +000062func testRust(t *testing.T, bp string) *android.TestContext {
Colin Cross8a49a3d2024-05-20 12:22:27 -070063 t.Helper()
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000064 skipTestIfOsNotSupported(t)
65 result := android.GroupFixturePreparers(
66 prepareForRustTest,
67 rustMockedFiles.AddToFixture(),
68 ).
69 RunTestWithBp(t, bp)
70 return result.TestContext
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020071}
Colin Cross98be1bb2019-12-13 20:41:13 -080072
Ivan Lozano3149e6e2021-06-01 15:09:53 -040073const (
Kiyoung Kim1db4a742024-04-01 15:50:01 +090074 sharedVendorVariant = "android_vendor_arm64_armv8-a_shared"
75 rlibVendorVariant = "android_vendor_arm64_armv8-a_rlib_rlib-std"
76 rlibDylibStdVendorVariant = "android_vendor_arm64_armv8-a_rlib_rlib-std"
77 dylibVendorVariant = "android_vendor_arm64_armv8-a_dylib"
Ivan Lozanoadd122a2023-07-13 11:01:41 -040078 sharedRecoveryVariant = "android_recovery_arm64_armv8-a_shared"
79 rlibRecoveryVariant = "android_recovery_arm64_armv8-a_rlib_dylib-std"
80 rlibRlibStdRecoveryVariant = "android_recovery_arm64_armv8-a_rlib_rlib-std"
81 dylibRecoveryVariant = "android_recovery_arm64_armv8-a_dylib"
82 binaryCoreVariant = "android_arm64_armv8-a"
Kiyoung Kim1db4a742024-04-01 15:50:01 +090083 binaryVendorVariant = "android_vendor_arm64_armv8-a"
84 binaryProductVariant = "android_product_arm64_armv8-a"
Ivan Lozanoadd122a2023-07-13 11:01:41 -040085 binaryRecoveryVariant = "android_recovery_arm64_armv8-a"
Ivan Lozano3149e6e2021-06-01 15:09:53 -040086)
Ivan Lozano1921e802021-05-20 13:39:16 -040087
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020088// testRustCov returns a TestContext in which a basic environment has been
89// setup. This environment explicitly enables coverage.
90func testRustCov(t *testing.T, bp string) *android.TestContext {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000091 skipTestIfOsNotSupported(t)
92 result := android.GroupFixturePreparers(
93 prepareForRustTest,
94 rustMockedFiles.AddToFixture(),
95 android.FixtureModifyProductVariables(
96 func(variables android.FixtureProductVariables) {
97 variables.ClangCoverage = proptools.BoolPtr(true)
98 variables.Native_coverage = proptools.BoolPtr(true)
99 variables.NativeCoveragePaths = []string{"*"}
100 },
101 ),
102 ).RunTestWithBp(t, bp)
103 return result.TestContext
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200104}
105
106// testRustError ensures that at least one error was raised and its value
107// matches the pattern provided. The error can be either in the parsing of the
108// Blueprint or when generating the build actions.
109func testRustError(t *testing.T, pattern string, bp string) {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000110 skipTestIfOsNotSupported(t)
111 android.GroupFixturePreparers(
112 prepareForRustTest,
113 rustMockedFiles.AddToFixture(),
114 ).
115 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
116 RunTestWithBp(t, bp)
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200117}
118
119// testRustCtx is used to build a particular test environment. Unless your
120// tests requires a specific setup, prefer the wrapping functions: testRust,
121// testRustCov or testRustError.
122type testRustCtx struct {
123 bp string
124 fs map[string][]byte
125 env map[string]string
126 config *android.Config
127}
128
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000129func skipTestIfOsNotSupported(t *testing.T) {
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200130 // TODO (b/140435149)
131 if runtime.GOOS != "linux" {
132 t.Skip("Rust Soong tests can only be run on Linux hosts currently")
133 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700134}
135
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136// Test that we can extract the link path from a lib path.
137func TestLinkPathFromFilePath(t *testing.T) {
138 barPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so")
Wen-yi Chu41326c12023-09-22 03:58:59 +0000139 libName := linkPathFromFilePath(barPath)
140 expectedResult := "out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/"
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141
Wen-yi Chu41326c12023-09-22 03:58:59 +0000142 if libName != expectedResult {
143 t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144 }
145}
146
Ivan Lozanoffee3342019-08-27 12:03:00 -0700147// Test to make sure dependencies are being picked up correctly.
148func TestDepsTracking(t *testing.T) {
149 ctx := testRust(t, `
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400150 cc_library {
151 host_supported: true,
152 name: "cc_stubs_dep",
153 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400154 cc_library_host_static {
Ivan Lozano52767be2019-10-18 14:49:46 -0700155 name: "libstatic",
Ivan Lozano52767be2019-10-18 14:49:46 -0700156 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400157 cc_library_host_static {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400158 name: "libwholestatic",
Ivan Lozano63bb7682021-03-23 15:53:44 -0400159 }
Matthew Maurer2ae05132020-06-23 14:28:53 -0700160 rust_ffi_host_shared {
Ivan Lozano52767be2019-10-18 14:49:46 -0700161 name: "libshared",
162 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700163 crate_name: "shared",
Ivan Lozano52767be2019-10-18 14:49:46 -0700164 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700165 rust_library_host_rlib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700166 name: "librlib",
Ivan Lozano43845682020-07-09 21:03:28 -0400167 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700168 crate_name: "rlib",
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500169 static_libs: ["libstatic"],
Ivan Lozano63bb7682021-03-23 15:53:44 -0400170 whole_static_libs: ["libwholestatic"],
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400171 shared_libs: ["cc_stubs_dep"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700172 }
173 rust_proc_macro {
174 name: "libpm",
175 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700176 crate_name: "pm",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700177 }
178 rust_binary_host {
Ivan Lozano43845682020-07-09 21:03:28 -0400179 name: "fizz-buzz",
Ivan Lozano52767be2019-10-18 14:49:46 -0700180 rlibs: ["librlib"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700181 proc_macros: ["libpm"],
Ivan Lozano52767be2019-10-18 14:49:46 -0700182 static_libs: ["libstatic"],
183 shared_libs: ["libshared"],
Ivan Lozano43845682020-07-09 21:03:28 -0400184 srcs: ["foo.rs"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700185 }
186 `)
Colin Cross90607e92025-02-11 14:58:07 -0800187 module := ctx.ModuleForTests(t, "fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
188 rustc := ctx.ModuleForTests(t, "librlib", "linux_glibc_x86_64_rlib_rlib-std").Rule("rustc")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700189
190 // Since dependencies are added to AndroidMk* properties, we can check these to see if they've been picked up.
Ivan Lozano2b081132020-09-08 12:46:52 -0400191 if !android.InList("librlib.rlib-std", module.Properties.AndroidMkRlibs) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700192 t.Errorf("Rlib dependency not detected (dependency missing from AndroidMkRlibs)")
193 }
194
195 if !android.InList("libpm", module.Properties.AndroidMkProcMacroLibs) {
196 t.Errorf("Proc_macro dependency not detected (dependency missing from AndroidMkProcMacroLibs)")
197 }
198
Cole Faustb6e6f992023-08-17 17:42:26 -0700199 if !android.InList("libshared", module.transitiveAndroidMkSharedLibs.ToList()) {
Ivan Lozano52767be2019-10-18 14:49:46 -0700200 t.Errorf("Shared library dependency not detected (dependency missing from AndroidMkSharedLibs)")
201 }
202
203 if !android.InList("libstatic", module.Properties.AndroidMkStaticLibs) {
204 t.Errorf("Static library dependency not detected (dependency missing from AndroidMkStaticLibs)")
205 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500206
Ivan Lozano1f10f682024-11-08 16:16:50 +0000207 if !strings.Contains(rustc.Args["rustcFlags"], "-lstatic:+whole-archive=wholestatic") {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000208 t.Errorf("-lstatic flag not being passed to rustc for static library %#v", rustc.Args["rustcFlags"])
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500209 }
210
Colin Cross004bd3f2023-10-02 11:39:17 -0700211 if !strings.Contains(rustc.Args["linkFlags"], "cc_stubs_dep.so") {
212 t.Errorf("shared cc_library not being passed to rustc linkFlags %#v", rustc.Args["linkFlags"])
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400213 }
214
Colin Cross004bd3f2023-10-02 11:39:17 -0700215 if !android.SuffixInList(rustc.OrderOnly.Strings(), "cc_stubs_dep.so") {
216 t.Errorf("shared cc dep not being passed as order-only to rustc %#v", rustc.OrderOnly.Strings())
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400217 }
218
Colin Cross004bd3f2023-10-02 11:39:17 -0700219 if !android.SuffixInList(rustc.Implicits.Strings(), "cc_stubs_dep.so.toc") {
220 t.Errorf("shared cc dep TOC not being passed as implicit to rustc %#v", rustc.Implicits.Strings())
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400221 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700222}
Ivan Lozanob9040d62019-09-24 13:23:50 -0700223
Ivan Lozano43845682020-07-09 21:03:28 -0400224func TestSourceProviderDeps(t *testing.T) {
225 ctx := testRust(t, `
226 rust_binary {
227 name: "fizz-buzz-dep",
228 srcs: [
229 "foo.rs",
230 ":my_generator",
231 ":libbindings",
232 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400233 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400234 }
235 rust_proc_macro {
236 name: "libprocmacro",
237 srcs: [
238 "foo.rs",
239 ":my_generator",
240 ":libbindings",
241 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400242 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400243 crate_name: "procmacro",
244 }
245 rust_library {
246 name: "libfoo",
247 srcs: [
248 "foo.rs",
249 ":my_generator",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400250 ":libbindings",
251 ],
252 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400253 crate_name: "foo",
254 }
255 genrule {
256 name: "my_generator",
257 tools: ["any_rust_binary"],
258 cmd: "$(location) -o $(out) $(in)",
259 srcs: ["src/any.h"],
260 out: ["src/any.rs"],
261 }
Colin Crosse9fe2942020-11-10 18:12:15 -0800262 rust_binary_host {
263 name: "any_rust_binary",
264 srcs: [
265 "foo.rs",
266 ],
267 }
Ivan Lozano43845682020-07-09 21:03:28 -0400268 rust_bindgen {
269 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400270 crate_name: "bindings",
271 source_stem: "bindings",
Ivan Lozano43845682020-07-09 21:03:28 -0400272 host_supported: true,
273 wrapper_src: "src/any.h",
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400274 }
Ivan Lozano43845682020-07-09 21:03:28 -0400275 `)
276
Colin Cross90607e92025-02-11 14:58:07 -0800277 libfoo := ctx.ModuleForTests(t, "libfoo", "android_arm64_armv8-a_rlib_dylib-std").Rule("rustc")
Ivan Lozano43845682020-07-09 21:03:28 -0400278 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/bindings.rs") {
279 t.Errorf("rust_bindgen generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
280 }
281 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/any.rs") {
282 t.Errorf("genrule generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
283 }
284
Colin Cross90607e92025-02-11 14:58:07 -0800285 fizzBuzz := ctx.ModuleForTests(t, "fizz-buzz-dep", "android_arm64_armv8-a").Rule("rustc")
Ivan Lozano43845682020-07-09 21:03:28 -0400286 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/bindings.rs") {
287 t.Errorf("rust_bindgen generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
288 }
289 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/any.rs") {
290 t.Errorf("genrule generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
291 }
292
Colin Cross90607e92025-02-11 14:58:07 -0800293 libprocmacro := ctx.ModuleForTests(t, "libprocmacro", "linux_glibc_x86_64").Rule("rustc")
Ivan Lozano43845682020-07-09 21:03:28 -0400294 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/bindings.rs") {
295 t.Errorf("rust_bindgen generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
296 }
297 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/any.rs") {
298 t.Errorf("genrule generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
299 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400300
301 // Check that our bindings are picked up as crate dependencies as well
Colin Cross90607e92025-02-11 14:58:07 -0800302 libfooMod := ctx.ModuleForTests(t, "libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
Ivan Lozano4df02572023-06-15 14:21:09 -0400303 if !android.InList("libbindings", libfooMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400304 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
305 }
Colin Cross90607e92025-02-11 14:58:07 -0800306 fizzBuzzMod := ctx.ModuleForTests(t, "fizz-buzz-dep", "android_arm64_armv8-a").Module().(*Module)
Ivan Lozano4df02572023-06-15 14:21:09 -0400307 if !android.InList("libbindings", fizzBuzzMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400308 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
309 }
Colin Cross90607e92025-02-11 14:58:07 -0800310 libprocmacroMod := ctx.ModuleForTests(t, "libprocmacro", "linux_glibc_x86_64").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400311 if !android.InList("libbindings.rlib-std", libprocmacroMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400312 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
313 }
Ivan Lozano43845682020-07-09 21:03:28 -0400314}
315
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400316func TestSourceProviderTargetMismatch(t *testing.T) {
317 // This might error while building the dependency tree or when calling depsToPaths() depending on the lunched
318 // target, which results in two different errors. So don't check the error, just confirm there is one.
319 testRustError(t, ".*", `
320 rust_proc_macro {
321 name: "libprocmacro",
322 srcs: [
323 "foo.rs",
324 ":libbindings",
325 ],
326 crate_name: "procmacro",
327 }
328 rust_bindgen {
329 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400330 crate_name: "bindings",
331 source_stem: "bindings",
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400332 wrapper_src: "src/any.h",
333 }
334 `)
335}
336
Ivan Lozanob9040d62019-09-24 13:23:50 -0700337// Test to make sure proc_macros use host variants when building device modules.
338func TestProcMacroDeviceDeps(t *testing.T) {
339 ctx := testRust(t, `
340 rust_library_host_rlib {
341 name: "libbar",
342 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700343 crate_name: "bar",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700344 }
345 rust_proc_macro {
346 name: "libpm",
347 rlibs: ["libbar"],
348 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700349 crate_name: "pm",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700350 }
351 rust_binary {
352 name: "fizz-buzz",
353 proc_macros: ["libpm"],
354 srcs: ["foo.rs"],
355 }
356 `)
Colin Cross90607e92025-02-11 14:58:07 -0800357 rustc := ctx.ModuleForTests(t, "libpm", "linux_glibc_x86_64").Rule("rustc")
Ivan Lozanob9040d62019-09-24 13:23:50 -0700358
Wen-yi Chu41326c12023-09-22 03:58:59 +0000359 if !strings.Contains(rustc.Args["libFlags"], "libbar/linux_glibc_x86_64") {
Ivan Lozanob9040d62019-09-24 13:23:50 -0700360 t.Errorf("Proc_macro is not using host variant of dependent modules.")
361 }
362}
Matthew Maurer99020b02019-10-31 10:44:40 -0700363
364// Test that no_stdlibs suppresses dependencies on rust standard libraries
365func TestNoStdlibs(t *testing.T) {
366 ctx := testRust(t, `
367 rust_binary {
368 name: "fizz-buzz",
369 srcs: ["foo.rs"],
Ivan Lozano9d1df102020-04-28 10:10:23 -0400370 no_stdlibs: true,
Matthew Maurer99020b02019-10-31 10:44:40 -0700371 }`)
Colin Cross90607e92025-02-11 14:58:07 -0800372 module := ctx.ModuleForTests(t, "fizz-buzz", "android_arm64_armv8-a").Module().(*Module)
Matthew Maurer99020b02019-10-31 10:44:40 -0700373
374 if android.InList("libstd", module.Properties.AndroidMkDylibs) {
375 t.Errorf("no_stdlibs did not suppress dependency on libstd")
376 }
377}
Ivan Lozano9d1df102020-04-28 10:10:23 -0400378
379// Test that libraries provide both 32-bit and 64-bit variants.
380func TestMultilib(t *testing.T) {
381 ctx := testRust(t, `
382 rust_library_rlib {
383 name: "libfoo",
384 srcs: ["foo.rs"],
385 crate_name: "foo",
386 }`)
387
Colin Cross90607e92025-02-11 14:58:07 -0800388 _ = ctx.ModuleForTests(t, "libfoo", "android_arm64_armv8-a_rlib_dylib-std")
389 _ = ctx.ModuleForTests(t, "libfoo", "android_arm_armv7-a-neon_rlib_dylib-std")
Ivan Lozano9d1df102020-04-28 10:10:23 -0400390}
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200391
392// Test that library size measurements are generated.
393func TestLibrarySizes(t *testing.T) {
394 ctx := testRust(t, `
395 rust_library_dylib {
396 name: "libwaldo",
397 srcs: ["foo.rs"],
398 crate_name: "waldo",
399 }`)
400
Colin Cross90607e92025-02-11 14:58:07 -0800401 m := ctx.SingletonForTests(t, "file_metrics")
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400402 m.Output("unstripped/libwaldo.dylib.so.bloaty.csv")
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200403 m.Output("libwaldo.dylib.so.bloaty.csv")
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200404}
Ivan Lozano62cd0382021-11-01 10:27:54 -0400405
Andrew Walbran52533232024-03-19 11:36:04 +0000406// Test that aliases are respected.
407func TestRustAliases(t *testing.T) {
408 ctx := testRust(t, `
409 rust_library {
410 name: "libbar",
411 crate_name: "bar",
412 srcs: ["src/lib.rs"],
413 }
414 rust_library {
415 name: "libbaz",
416 crate_name: "baz",
417 srcs: ["src/lib.rs"],
418 }
419 rust_binary {
420 name: "foo",
421 srcs: ["src/main.rs"],
422 rustlibs: ["libbar", "libbaz"],
423 aliases: ["bar:bar_renamed"],
424 }`)
425
Colin Cross90607e92025-02-11 14:58:07 -0800426 fooRustc := ctx.ModuleForTests(t, "foo", "android_arm64_armv8-a").Rule("rustc")
Matthew Maurerf67d2112025-01-07 01:45:14 +0000427 if !strings.Contains(fooRustc.Args["libFlags"], "--extern force:bar_renamed=out/soong/.intermediates/libbar/android_arm64_armv8-a_dylib/unstripped/libbar.dylib.so") {
428 t.Errorf("--extern force:bar_renamed=out/soong/.intermediates/libbar/android_arm64_armv8-a_dylib/unstripped/libbar.dylib.so flag not being passed to rustc for rust_binary with aliases. libFlags: %#v", fooRustc.Args["libFlags"])
Andrew Walbran52533232024-03-19 11:36:04 +0000429 }
Matthew Maurerf67d2112025-01-07 01:45:14 +0000430 if !strings.Contains(fooRustc.Args["libFlags"], "--extern force:baz=out/soong/.intermediates/libbaz/android_arm64_armv8-a_dylib/unstripped/libbaz.dylib.so") {
431 t.Errorf("--extern force:baz=out/soong/.intermediates/libbaz/android_arm64_armv8-a_dylib/unstripped/libbaz.dylib.so flag not being passed to rustc for rust_binary with aliases. libFlags: %#v", fooRustc.Args["libFlags"])
Andrew Walbran52533232024-03-19 11:36:04 +0000432 }
433}
434
Ivan Lozano7fe65ca2025-02-05 02:22:33 +0000435func TestRustFFIRlibs(t *testing.T) {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400436 ctx := testRust(t, `
Ivan Lozano61848422024-12-13 19:45:00 +0000437 rust_ffi_static {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400438 name: "libbar",
439 crate_name: "bar",
440 srcs: ["src/lib.rs"],
441 export_include_dirs: ["bar_includes"]
442 }
443
Ivan Lozano61848422024-12-13 19:45:00 +0000444 rust_ffi_static {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400445 name: "libfoo",
446 crate_name: "foo",
447 srcs: ["src/lib.rs"],
448 export_include_dirs: ["foo_includes"]
449 }
450
Ivan Lozano61848422024-12-13 19:45:00 +0000451 rust_ffi_static {
Ivan Lozano85f00cf2025-02-11 20:19:19 +0000452 name: "libfoo_from_rlib",
453 crate_name: "foo_from_rlib",
454 srcs: ["src/lib.rs"],
455 export_include_dirs: ["foo_includes"]
456 }
457
458 rust_ffi_static {
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400459 name: "libbuzz",
460 crate_name: "buzz",
461 srcs: ["src/lib.rs"],
462 export_include_dirs: ["buzz_includes"]
463 }
464
Ivan Lozano85f00cf2025-02-11 20:19:19 +0000465 rust_ffi_static {
466 name: "libbuzz_from_rlib",
467 crate_name: "buzz_from_rlib",
468 srcs: ["src/lib.rs"],
469 export_include_dirs: ["buzz_includes"]
470 }
471
Ivan Lozano0a468a42024-05-13 21:03:34 -0400472 cc_library_shared {
473 name: "libcc_shared",
474 srcs:["foo.c"],
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400475 static_libs: ["libbar"],
Ivan Lozano0a468a42024-05-13 21:03:34 -0400476 }
477
478 cc_library_static {
479 name: "libcc_static",
480 srcs:["foo.c"],
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400481 static_libs: ["libbuzz"],
482 whole_static_libs: ["libfoo"],
Ivan Lozano0a468a42024-05-13 21:03:34 -0400483 }
484
Ivan Lozano85f00cf2025-02-11 20:19:19 +0000485 cc_library_static {
486 name: "libcc_static_from_rlib",
487 srcs:["foo.c"],
488 static_libs: ["libbuzz_from_rlib"],
489 whole_static_libs: ["libfoo_from_rlib"],
490 }
491
Ivan Lozano0a468a42024-05-13 21:03:34 -0400492 cc_binary {
493 name: "ccBin",
494 srcs:["foo.c"],
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400495 static_libs: ["libcc_static", "libbar"],
Ivan Lozano0a468a42024-05-13 21:03:34 -0400496 }
Ivan Lozano85f00cf2025-02-11 20:19:19 +0000497
498 rust_library {
499 name: "librs",
500 srcs:["src/foo.rs"],
501 crate_name: "rs",
502 static_libs: ["libcc_static_from_rlib"],
503 }
504
505 rust_binary {
506 name: "rsBin",
507 srcs:["src/foo.rs"],
508 crate_name: "rsBin",
509 rlibs: ["librs", "libbar"],
510 static_libs: ["libcc_static"],
511 }
Colin Cross90607e92025-02-11 14:58:07 -0800512 `)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400513
Colin Cross90607e92025-02-11 14:58:07 -0800514 libbar := ctx.ModuleForTests(t, "libbar", "android_arm64_armv8-a_rlib_rlib-std").Rule("rustc")
515 libcc_shared_rustc := ctx.ModuleForTests(t, "libcc_shared", "android_arm64_armv8-a_shared").Rule("rustc")
516 libcc_shared_ld := ctx.ModuleForTests(t, "libcc_shared", "android_arm64_armv8-a_shared").Rule("ld")
517 libcc_shared_cc := ctx.ModuleForTests(t, "libcc_shared", "android_arm64_armv8-a_shared").Rule("cc")
518 ccbin_rustc := ctx.ModuleForTests(t, "ccBin", "android_arm64_armv8-a").Rule("rustc")
519 ccbin_ld := ctx.ModuleForTests(t, "ccBin", "android_arm64_armv8-a").Rule("ld")
520 ccbin_cc := ctx.ModuleForTests(t, "ccBin", "android_arm64_armv8-a").Rule("cc")
521 rustbin_genlib := ctx.ModuleForTests(t, "rsBin", "android_arm64_armv8-a").Output("generated_rust_staticlib/librustlibs.a")
522 rustbin := ctx.ModuleForTests(t, "rsBin", "android_arm64_armv8-a").Output("unstripped/rsBin")
523 librs_rlib := ctx.ModuleForTests(t, "librs", "android_arm64_armv8-a_rlib_dylib-std").MaybeOutput("generated_rust_staticlib/librustlibs.a")
Ivan Lozano0a468a42024-05-13 21:03:34 -0400524
525 if !strings.Contains(libbar.Args["rustcFlags"], "crate-type=rlib") {
526 t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", "rlib", libbar.Args["rustcFlags"])
527 }
528
529 // Make sure there's a rustc command, and it's producing a staticlib
530 if !strings.Contains(libcc_shared_rustc.Args["rustcFlags"], "crate-type=staticlib") {
531 t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v",
532 "staticlib", libcc_shared_rustc.Args["rustcFlags"])
533 }
534
535 // Make sure the static lib is included in the ld command
Ivan Lozano872e8462024-10-30 19:30:48 +0000536 if !strings.Contains(libcc_shared_ld.Args["libFlags"], "generated_rust_staticlib/librustlibs.a") {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400537 t.Errorf("missing generated static library in linker step libFlags %#v, libFlags: %#v",
538 "libcc_shared.generated_rust_staticlib.a", libcc_shared_ld.Args["libFlags"])
539 }
540
541 // Make sure the static lib includes are in the cc command
542 if !strings.Contains(libcc_shared_cc.Args["cFlags"], "-Ibar_includes") {
543 t.Errorf("missing rlibs includes, expecting %#v, cFlags: %#v",
544 "-Ibar_includes", libcc_shared_cc.Args["cFlags"])
545 }
546
547 // Make sure there's a rustc command, and it's producing a staticlib
548 if !strings.Contains(ccbin_rustc.Args["rustcFlags"], "crate-type=staticlib") {
549 t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", "staticlib", ccbin_rustc.Args["rustcFlags"])
550 }
551
552 // Make sure the static lib is included in the cc command
Ivan Lozano872e8462024-10-30 19:30:48 +0000553 if !strings.Contains(ccbin_ld.Args["libFlags"], "generated_rust_staticlib/librustlibs.a") {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400554 t.Errorf("missing generated static library in linker step libFlags, expecting %#v, libFlags: %#v",
Ivan Lozano85f00cf2025-02-11 20:19:19 +0000555 "generated_rust_staticlib/librustlibs.a", ccbin_ld.Args["libFlags"])
Ivan Lozano0a468a42024-05-13 21:03:34 -0400556 }
557
558 // Make sure the static lib includes are in the ld command
559 if !strings.Contains(ccbin_cc.Args["cFlags"], "-Ibar_includes") {
560 t.Errorf("missing rlibs includes, expecting %#v, cFlags: %#v",
561 "-Ibar_includes", ccbin_cc.Args)
562 }
563
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400564 // Make sure that direct dependencies and indirect whole static dependencies are
Ivan Lozano0a468a42024-05-13 21:03:34 -0400565 // propagating correctly to the generated rlib.
566 if !strings.Contains(ccbin_rustc.Args["libFlags"], "--extern foo=") {
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400567 t.Errorf("Missing indirect whole_static_lib dependency libfoo when writing generated Rust staticlib: %#v", ccbin_rustc.Args["libFlags"])
568 }
569 if strings.Contains(ccbin_rustc.Args["libFlags"], "--extern buzz=") {
570 t.Errorf("Indirect static_lib dependency libbuzz found when writing generated Rust staticlib: %#v", ccbin_rustc.Args["libFlags"])
Ivan Lozano0a468a42024-05-13 21:03:34 -0400571 }
572 if !strings.Contains(ccbin_rustc.Args["libFlags"], "--extern bar=") {
573 t.Errorf("Missing direct dependency libbar when writing generated Rust staticlib: %#v", ccbin_rustc.Args["libFlags"])
574 }
575
Ivan Lozano85f00cf2025-02-11 20:19:19 +0000576 // Make sure the static lib is included in the rustc command
577 if !strings.Contains(rustbin.Args["linkFlags"], "generated_rust_staticlib/librustlibs.a") {
578 t.Errorf("missing generated static library in linker step libFlags in Rust module, expecting %#v, libFlags: %#v",
579 "generated_rust_staticlib/librustlibs.a", rustbin.Args["libFlags"])
580 }
581
582 // Make sure that direct dependencies and indirect whole static dependencies are
583 // propagating correctly for the rlib -> cc_library_static -> rust_* generated library example.
584 if !strings.Contains(rustbin_genlib.Args["libFlags"], "--extern foo=") {
585 t.Errorf("Missing indirect whole_static_lib dependency libfoo from cc static_lib when writing generated Rust staticlib: %#v", rustbin_genlib.Args["libFlags"])
586 }
587 if strings.Contains(rustbin_genlib.Args["libFlags"], "--extern buzz=") {
588 t.Errorf("Indirect rlib dependency libbuzz from cc static_lib found when writing generated Rust staticlib: %#v", rustbin_genlib.Args["libFlags"])
589 }
590 if strings.Contains(rustbin_genlib.Args["libFlags"], "--extern bar=") {
591 t.Errorf("Direct rlib dependency libbar getting included in the generated Rust staticlib: %#v", rustbin_genlib.Args["libFlags"])
592 }
593 if !strings.Contains(rustbin_genlib.Args["libFlags"], "--extern foo_from_rlib=") {
594 t.Errorf("Missing indirect whole_static_lib dependency libfoo_from_rlib from cc static_lib when writing generated Rust staticlib: %#v", rustbin_genlib.Args["libFlags"])
595 }
596 if strings.Contains(rustbin_genlib.Args["libFlags"], "--extern buzz_from_rlib=") {
597 // While static-libs propagate for rust modules, this is not the
598 // expected behavior for cc modules. Thus, libbuzz_from_rlib would
599 // be expected to have to be re-declared as a direct rlib dependency.
600 t.Errorf("Indirect rlib dependency libbuzz_from_rlib from cc static_lib found when writing generated Rust staticlib: %#v", rustbin_genlib.Args["libFlags"])
601 }
602
Ivan Lozano0a468a42024-05-13 21:03:34 -0400603 // Test indirect includes propagation
604 if !strings.Contains(ccbin_cc.Args["cFlags"], "-Ifoo_includes") {
605 t.Errorf("missing rlibs includes, expecting %#v, cFlags: %#v",
606 "-Ifoo_includes", ccbin_cc.Args)
607 }
Ivan Lozano85f00cf2025-02-11 20:19:19 +0000608
609 // Make sure we're not generating superfluous mto staticlibs.
610 if librs_rlib.Rule != nil {
611 t.Error("rlibs should not be generating mto staticlibs", "rlib", libbar.Args["rustcFlags"])
612 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400613}
614
Ivan Lozano62cd0382021-11-01 10:27:54 -0400615func assertString(t *testing.T, got, expected string) {
616 t.Helper()
617 if got != expected {
618 t.Errorf("expected %q got %q", expected, got)
619 }
620}
Ivan Lozano806efd32024-12-11 21:38:53 +0000621
622func TestStdLinkMismatch(t *testing.T) {
623 // Test that we catch cases where the std linkage mismatches. This leads to
624 // a confusing rustc error where a crate is declared missing despite being
625 // passed in as a rustlib dependency / via the --extern flag. Thus, we want
626 // to make sure we detect it in Soong.
627
628 // libfoo depends on libbar as an rlib, but does not link libstd as an rlib.
629 // libbar only links libstd as an rlib (prefer_rlib).
630 testRustError(t, "wrong StdLinkage", `
631 rust_library {
632 name: "libfoo",
633 crate_name: "foo",
634 srcs: [
635 "foo.rs",
636 ],
637 rlibs: ["libbar"],
638 }
639 rust_library {
640 name: "libbar",
641 crate_name: "bar",
642 srcs: [
643 "bar.rs",
644 ],
645 prefer_rlib: true,
646 }
647 `)
648}
Ivan Lozano1f10f682024-11-08 16:16:50 +0000649
650func TestRustLinkPropagation(t *testing.T) {
651 // Test static and whole static propagation behavior
652 //
653 // Whole static libs propagate through rlibs and through dylibs to
654 // dependencies further down. rustc does not re-export whole-archived
655 // static libs for dylibs, so this simulates re-exporting those symbols.
656 //
657 // Static libs only propagate through rlibs to some final dylib. We propagate
658 // normal static libs because we allow rustlib dependencies to represent
659 // either rlibs or dylibs. Not propagating static libs through rlibs would
660 // mean we'd need to always redeclare static libs throughout a dependency tree
661 // We don't propagate past dylibs because they represent a final link.
662
663 ctx := testRust(t, `
664 rust_library_rlib {
665 name: "librlib1",
666 crate_name: "rlib1",
667 srcs: ["src/lib.rs"],
668 static_libs: ["libcc_static_rlib1"],
669 whole_static_libs: ["libcc_whole_static_rlib1"],
670 }
671
672 rust_library_dylib {
673 name: "libdylib1",
674 crate_name: "dylib1",
675 static_libs: ["libcc_static_dylib1"],
676 srcs: ["src/lib.rs"],
677 whole_static_libs: ["libcc_whole_static_dylib1"],
678 }
679
680 rust_library_rlib {
681 name: "librlib2",
682 crate_name: "rlib2",
683 srcs: ["src/lib.rs"],
684 rlibs: ["librlib1"],
685 static_libs: ["libcc_static_rlib2"],
686 whole_static_libs: ["libcc_whole_static_rlib2"],
687 }
688
689 rust_library_dylib {
690 name: "libdylib2",
691 crate_name: "dylib2",
692 srcs: ["src/lib.rs"],
693 rlibs: ["librlib1"],
694 rustlibs: ["libdylib1"],
695 static_libs: ["libcc_static_dylib2"],
696 whole_static_libs: ["libcc_whole_static_dylib2"],
697 }
698
699 cc_library_static {
700 name: "libcc_static_rlib1",
701 srcs:["foo.c"],
702 }
703
704 cc_library_static {
705 name: "libcc_static_rlib2",
706 srcs:["foo.c"],
707 }
708
709 cc_library_static {
710 name: "libcc_static_dylib1",
711 srcs:["foo.c"],
712 }
713
714 cc_library_static {
715 name: "libcc_static_dylib2",
716 srcs:["foo.c"],
717 }
718
719 cc_library_static {
720 name: "libcc_whole_static_rlib1",
721 srcs:["foo.c"],
722 }
723
724 cc_library_static {
725 name: "libcc_whole_static_rlib2",
726 srcs:["foo.c"],
727 }
728
729 cc_library_static {
730 name: "libcc_whole_static_dylib1",
731 srcs:["foo.c"],
732 }
733
734 cc_library_static {
735 name: "libcc_whole_static_dylib2",
736 srcs:["foo.c"],
737 }
738
739 rust_library_rlib {
740 name: "librlib3",
741 crate_name: "rlib3",
742 srcs: ["src/lib.rs"],
743 rlibs: ["librlib2"],
744 }
745
746 rust_library_dylib {
747 name: "libdylib3",
748 crate_name: "dylib3",
749 srcs: ["src/lib.rs"],
750 rlibs: ["librlib2"],
751 rustlibs: ["libdylib2"],
752 }
753 `)
754
Colin Cross90607e92025-02-11 14:58:07 -0800755 librlib3 := ctx.ModuleForTests(t, "librlib3", "android_arm64_armv8-a_rlib_dylib-std").Rule("rustc")
756 libdylib3 := ctx.ModuleForTests(t, "libdylib3", "android_arm64_armv8-a_dylib").Rule("rustc")
Ivan Lozano1f10f682024-11-08 16:16:50 +0000757
758 // Test static lib propagation from:
759 // rlib -> rlib
760 if !strings.Contains(librlib3.Args["linkFlags"], "libcc_static_rlib2.a") {
761 t.Errorf("direct dependency static lib not propagating from rlib to rlib; linkFlags %#v",
762 librlib3.Args["linkFlags"])
763 }
764 // rlib -> rlib -> rlib
765 if !strings.Contains(librlib3.Args["linkFlags"], "libcc_static_rlib1.a") {
766 t.Errorf("indirect dependency static lib not propagating from rlib to rlib: linkFlags %#v",
767 librlib3.Args["linkFlags"])
768 }
769 // rlib -> rlib -> dylib
770 if !strings.Contains(libdylib3.Args["linkFlags"], "libcc_static_rlib1.a") {
771 t.Errorf("indirect dependency static lib not propagating from rlib to dylib: linkFlags %#v",
772 libdylib3.Args["linkFlags"])
773 }
774 // rlib -> dylib
775 if !strings.Contains(libdylib3.Args["linkFlags"], "libcc_static_rlib2.a") {
776 t.Errorf("direct dependency static lib not propagating from rlib to dylib: linkFlags: %#v",
777 libdylib3.Args["linkFlags"])
778 }
779 // dylib -> dylib (negative case, should not propagate)
780 if strings.Contains(libdylib3.Args["linkFlags"], "libcc_static_dylib2.a") {
781 t.Errorf("direct dependency static lib propagating from dylib to dylib: linkFlags: %#v",
782 libdylib3.Args["linkFlags"])
783 }
784 // dylib -> dylib -> dylib (negative case, should not propagate)
785 if strings.Contains(libdylib3.Args["linkFlags"], "libcc_static_dylib1.a") {
786 t.Errorf("indirect dependency static lib propagating from dylib to dylib: linkFlags: %#v",
787 libdylib3.Args["linkFlags"])
788 }
789
790 // Test whole static lib propagation from:
791 // rlib -> rlib
792 if !strings.Contains(librlib3.Args["linkFlags"], "libcc_whole_static_rlib2.a") {
793 t.Errorf("direct dependency whole static lib not propagating from rlib to rlib: linkFlags %#v",
794 librlib3.Args["linkFlags"])
795 }
796 // rlib -> rlib -> rlib
797 if !strings.Contains(librlib3.Args["linkFlags"], "libcc_whole_static_rlib1.a") {
798 t.Errorf("indirect dependency whole static lib not propagating from rlib to rlib: linkFlags %#v",
799 librlib3.Args["linkFlags"])
800 }
801 // rlib -> dylib
802 if !strings.Contains(libdylib3.Args["linkFlags"], "libcc_whole_static_rlib2.a") {
803 t.Errorf("direct dependency whole static lib not propagating from rlib to dylib: linkFlags %#v",
804 libdylib3.Args["linkFlags"])
805 }
806 // rlib -> rlib -> dylib
807 if !strings.Contains(libdylib3.Args["linkFlags"], "libcc_whole_static_rlib1.a") {
808 t.Errorf("indirect dependency whole static lib not propagating from rlib to dylib: linkFlags %#v",
809 libdylib3.Args["linkFlags"])
810 }
811 // dylib -> dylib
812 if !strings.Contains(libdylib3.Args["linkFlags"], "libcc_whole_static_dylib2.a") {
813 t.Errorf("direct dependency whole static lib not propagating from dylib to dylib: linkFlags %#v",
814 libdylib3.Args["linkFlags"])
815 }
816 // dylib -> dylib -> dylib
817 if !strings.Contains(libdylib3.Args["linkFlags"], "libcc_whole_static_dylib1.a") {
818 t.Errorf("indirect dependency whole static lib not propagating from dylib to dylib: linkFlags %#v",
819 libdylib3.Args["linkFlags"])
820 }
821}