blob: 3f4e296768cdf2805ee3ac5771372908d1569820 [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
Ivan Lozano1921e802021-05-20 13:39:16 -040040 PrepareForTestWithRustIncludeVndk,
41 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
42 variables.DeviceVndkVersion = StringPtr("current")
43 variables.ProductVndkVersion = StringPtr("current")
44 variables.Platform_vndk_version = StringPtr("29")
45 }),
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000046)
47
48var rustMockedFiles = android.MockFS{
Ivan Lozano1921e802021-05-20 13:39:16 -040049 "foo.rs": nil,
50 "foo.c": nil,
51 "src/bar.rs": nil,
52 "src/any.h": nil,
53 "c_includes/c_header.h": nil,
54 "rust_includes/rust_headers.h": nil,
55 "proto.proto": nil,
56 "proto/buf.proto": nil,
57 "buf.proto": nil,
58 "foo.proto": nil,
59 "liby.so": nil,
60 "libz.so": nil,
61 "data.txt": nil,
Ivan Lozano3149e6e2021-06-01 15:09:53 -040062 "liblog.map.txt": nil,
Ivan Lozanoffee3342019-08-27 12:03:00 -070063}
64
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020065// testRust returns a TestContext in which a basic environment has been setup.
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000066// This environment contains a few mocked files. See rustMockedFiles for the list of these files.
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020067func testRust(t *testing.T, bp string) *android.TestContext {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000068 skipTestIfOsNotSupported(t)
69 result := android.GroupFixturePreparers(
70 prepareForRustTest,
71 rustMockedFiles.AddToFixture(),
72 ).
73 RunTestWithBp(t, bp)
74 return result.TestContext
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020075}
Colin Cross98be1bb2019-12-13 20:41:13 -080076
Ivan Lozanof76cdf72021-02-12 09:55:06 -050077func testRustVndk(t *testing.T, bp string) *android.TestContext {
Ivan Lozano1921e802021-05-20 13:39:16 -040078 return testRustVndkFs(t, bp, rustMockedFiles)
79}
80
Ivan Lozano3149e6e2021-06-01 15:09:53 -040081const (
Ivan Lozanoadd122a2023-07-13 11:01:41 -040082 sharedVendorVariant = "android_vendor.29_arm64_armv8-a_shared"
83 rlibVendorVariant = "android_vendor.29_arm64_armv8-a_rlib_rlib-std"
84 rlibDylibStdVendorVariant = "android_vendor.29_arm64_armv8-a_rlib_rlib-std"
85 dylibVendorVariant = "android_vendor.29_arm64_armv8-a_dylib"
86 sharedRecoveryVariant = "android_recovery_arm64_armv8-a_shared"
87 rlibRecoveryVariant = "android_recovery_arm64_armv8-a_rlib_dylib-std"
88 rlibRlibStdRecoveryVariant = "android_recovery_arm64_armv8-a_rlib_rlib-std"
89 dylibRecoveryVariant = "android_recovery_arm64_armv8-a_dylib"
90 binaryCoreVariant = "android_arm64_armv8-a"
91 binaryVendorVariant = "android_vendor.29_arm64_armv8-a"
92 binaryProductVariant = "android_product.29_arm64_armv8-a"
93 binaryRecoveryVariant = "android_recovery_arm64_armv8-a"
Ivan Lozano3149e6e2021-06-01 15:09:53 -040094)
Ivan Lozano1921e802021-05-20 13:39:16 -040095
96func testRustVndkFs(t *testing.T, bp string, fs android.MockFS) *android.TestContext {
Ivan Lozano3149e6e2021-06-01 15:09:53 -040097 return testRustVndkFsVersions(t, bp, fs, "current", "current", "29")
98}
99
100func testRustVndkFsVersions(t *testing.T, bp string, fs android.MockFS, device_version, product_version, vndk_version string) *android.TestContext {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000101 skipTestIfOsNotSupported(t)
102 result := android.GroupFixturePreparers(
103 prepareForRustTest,
Ivan Lozano1921e802021-05-20 13:39:16 -0400104 fs.AddToFixture(),
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000105 android.FixtureModifyProductVariables(
106 func(variables android.FixtureProductVariables) {
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400107 variables.DeviceVndkVersion = StringPtr(device_version)
108 variables.ProductVndkVersion = StringPtr(product_version)
109 variables.Platform_vndk_version = StringPtr(vndk_version)
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000110 },
111 ),
112 ).RunTestWithBp(t, bp)
113 return result.TestContext
Ivan Lozanoc2ca1ee2021-11-09 16:23:40 -0500114}
Ivan Lozano1921e802021-05-20 13:39:16 -0400115
Ivan Lozanoc2ca1ee2021-11-09 16:23:40 -0500116func testRustRecoveryFsVersions(t *testing.T, bp string, fs android.MockFS, device_version, vndk_version, recovery_version string) *android.TestContext {
117 skipTestIfOsNotSupported(t)
118 result := android.GroupFixturePreparers(
119 prepareForRustTest,
120 fs.AddToFixture(),
121 android.FixtureModifyProductVariables(
122 func(variables android.FixtureProductVariables) {
123 variables.DeviceVndkVersion = StringPtr(device_version)
124 variables.RecoverySnapshotVersion = StringPtr(recovery_version)
125 variables.Platform_vndk_version = StringPtr(vndk_version)
126 },
127 ),
128 ).RunTestWithBp(t, bp)
129 return result.TestContext
Ivan Lozanof76cdf72021-02-12 09:55:06 -0500130}
131
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200132// testRustCov returns a TestContext in which a basic environment has been
133// setup. This environment explicitly enables coverage.
134func testRustCov(t *testing.T, bp string) *android.TestContext {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000135 skipTestIfOsNotSupported(t)
136 result := android.GroupFixturePreparers(
137 prepareForRustTest,
138 rustMockedFiles.AddToFixture(),
139 android.FixtureModifyProductVariables(
140 func(variables android.FixtureProductVariables) {
141 variables.ClangCoverage = proptools.BoolPtr(true)
142 variables.Native_coverage = proptools.BoolPtr(true)
143 variables.NativeCoveragePaths = []string{"*"}
144 },
145 ),
146 ).RunTestWithBp(t, bp)
147 return result.TestContext
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200148}
149
150// testRustError ensures that at least one error was raised and its value
151// matches the pattern provided. The error can be either in the parsing of the
152// Blueprint or when generating the build actions.
153func testRustError(t *testing.T, pattern string, bp string) {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000154 skipTestIfOsNotSupported(t)
155 android.GroupFixturePreparers(
156 prepareForRustTest,
157 rustMockedFiles.AddToFixture(),
158 ).
159 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
160 RunTestWithBp(t, bp)
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200161}
162
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400163// testRustVndkError is similar to testRustError, but can be used to test VNDK-related errors.
164func testRustVndkError(t *testing.T, pattern string, bp string) {
Ivan Lozano1921e802021-05-20 13:39:16 -0400165 testRustVndkFsError(t, pattern, bp, rustMockedFiles)
166}
167
168func testRustVndkFsError(t *testing.T, pattern string, bp string, fs android.MockFS) {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400169 skipTestIfOsNotSupported(t)
170 android.GroupFixturePreparers(
171 prepareForRustTest,
Ivan Lozano1921e802021-05-20 13:39:16 -0400172 fs.AddToFixture(),
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400173 android.FixtureModifyProductVariables(
174 func(variables android.FixtureProductVariables) {
175 variables.DeviceVndkVersion = StringPtr("current")
176 variables.ProductVndkVersion = StringPtr("current")
177 variables.Platform_vndk_version = StringPtr("VER")
178 },
179 ),
180 ).
181 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
182 RunTestWithBp(t, bp)
183}
184
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200185// testRustCtx is used to build a particular test environment. Unless your
186// tests requires a specific setup, prefer the wrapping functions: testRust,
187// testRustCov or testRustError.
188type testRustCtx struct {
189 bp string
190 fs map[string][]byte
191 env map[string]string
192 config *android.Config
193}
194
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000195func skipTestIfOsNotSupported(t *testing.T) {
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200196 // TODO (b/140435149)
197 if runtime.GOOS != "linux" {
198 t.Skip("Rust Soong tests can only be run on Linux hosts currently")
199 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700200}
201
Ivan Lozanoffee3342019-08-27 12:03:00 -0700202// Test that we can extract the link path from a lib path.
203func TestLinkPathFromFilePath(t *testing.T) {
204 barPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so")
205 libName := linkPathFromFilePath(barPath)
206 expectedResult := "out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/"
207
208 if libName != expectedResult {
209 t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libName)
210 }
211}
212
Ivan Lozanoffee3342019-08-27 12:03:00 -0700213// Test to make sure dependencies are being picked up correctly.
214func TestDepsTracking(t *testing.T) {
215 ctx := testRust(t, `
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400216 cc_library {
217 host_supported: true,
218 name: "cc_stubs_dep",
219 }
Matthew Maurer2ae05132020-06-23 14:28:53 -0700220 rust_ffi_host_static {
Ivan Lozano52767be2019-10-18 14:49:46 -0700221 name: "libstatic",
222 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700223 crate_name: "static",
Ivan Lozano52767be2019-10-18 14:49:46 -0700224 }
Ivan Lozano63bb7682021-03-23 15:53:44 -0400225 rust_ffi_host_static {
226 name: "libwholestatic",
227 srcs: ["foo.rs"],
228 crate_name: "wholestatic",
229 }
Matthew Maurer2ae05132020-06-23 14:28:53 -0700230 rust_ffi_host_shared {
Ivan Lozano52767be2019-10-18 14:49:46 -0700231 name: "libshared",
232 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700233 crate_name: "shared",
Ivan Lozano52767be2019-10-18 14:49:46 -0700234 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700235 rust_library_host_dylib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700236 name: "libdylib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700237 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700238 crate_name: "dylib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700239 }
240 rust_library_host_rlib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700241 name: "librlib",
Ivan Lozano43845682020-07-09 21:03:28 -0400242 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700243 crate_name: "rlib",
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500244 static_libs: ["libstatic"],
Ivan Lozano63bb7682021-03-23 15:53:44 -0400245 whole_static_libs: ["libwholestatic"],
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400246 shared_libs: ["cc_stubs_dep"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700247 }
248 rust_proc_macro {
249 name: "libpm",
250 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700251 crate_name: "pm",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700252 }
253 rust_binary_host {
Ivan Lozano43845682020-07-09 21:03:28 -0400254 name: "fizz-buzz",
Ivan Lozano52767be2019-10-18 14:49:46 -0700255 dylibs: ["libdylib"],
256 rlibs: ["librlib"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700257 proc_macros: ["libpm"],
Ivan Lozano52767be2019-10-18 14:49:46 -0700258 static_libs: ["libstatic"],
259 shared_libs: ["libshared"],
Ivan Lozano43845682020-07-09 21:03:28 -0400260 srcs: ["foo.rs"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700261 }
262 `)
Ivan Lozano43845682020-07-09 21:03:28 -0400263 module := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500264 rustc := ctx.ModuleForTests("librlib", "linux_glibc_x86_64_rlib_rlib-std").Rule("rustc")
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700265 rustLink := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Rule("rustLink")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700266
267 // Since dependencies are added to AndroidMk* properties, we can check these to see if they've been picked up.
Ivan Lozano52767be2019-10-18 14:49:46 -0700268 if !android.InList("libdylib", module.Properties.AndroidMkDylibs) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700269 t.Errorf("Dylib dependency not detected (dependency missing from AndroidMkDylibs)")
270 }
271
Ivan Lozano2b081132020-09-08 12:46:52 -0400272 if !android.InList("librlib.rlib-std", module.Properties.AndroidMkRlibs) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700273 t.Errorf("Rlib dependency not detected (dependency missing from AndroidMkRlibs)")
274 }
275
276 if !android.InList("libpm", module.Properties.AndroidMkProcMacroLibs) {
277 t.Errorf("Proc_macro dependency not detected (dependency missing from AndroidMkProcMacroLibs)")
278 }
279
Ivan Lozano52767be2019-10-18 14:49:46 -0700280 if !android.InList("libshared", module.Properties.AndroidMkSharedLibs) {
281 t.Errorf("Shared library dependency not detected (dependency missing from AndroidMkSharedLibs)")
282 }
283
284 if !android.InList("libstatic", module.Properties.AndroidMkStaticLibs) {
285 t.Errorf("Static library dependency not detected (dependency missing from AndroidMkStaticLibs)")
286 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500287
Ivan Lozano63bb7682021-03-23 15:53:44 -0400288 if !strings.Contains(rustc.Args["rustcFlags"], "-lstatic=wholestatic") {
289 t.Errorf("-lstatic flag not being passed to rustc for static library %#v", rustc.Args["rustcFlags"])
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500290 }
291
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700292 if !strings.Contains(rustLink.Args["linkFlags"], "cc_stubs_dep.so") {
293 t.Errorf("shared cc_library not being passed to rustc linkFlags %#v", rustLink.Args["linkFlags"])
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400294 }
295
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700296 if !android.SuffixInList(rustLink.OrderOnly.Strings(), "cc_stubs_dep.so") {
297 t.Errorf("shared cc dep not being passed as order-only to rustc %#v", rustLink.OrderOnly.Strings())
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400298 }
299
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700300 if !android.SuffixInList(rustLink.Implicits.Strings(), "cc_stubs_dep.so.toc") {
301 t.Errorf("shared cc dep TOC not being passed as implicit to rustc %#v", rustLink.Implicits.Strings())
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400302 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700303}
Ivan Lozanob9040d62019-09-24 13:23:50 -0700304
Ivan Lozano43845682020-07-09 21:03:28 -0400305func TestSourceProviderDeps(t *testing.T) {
306 ctx := testRust(t, `
307 rust_binary {
308 name: "fizz-buzz-dep",
309 srcs: [
310 "foo.rs",
311 ":my_generator",
312 ":libbindings",
313 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400314 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400315 }
316 rust_proc_macro {
317 name: "libprocmacro",
318 srcs: [
319 "foo.rs",
320 ":my_generator",
321 ":libbindings",
322 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400323 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400324 crate_name: "procmacro",
325 }
326 rust_library {
327 name: "libfoo",
328 srcs: [
329 "foo.rs",
330 ":my_generator",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400331 ":libbindings",
332 ],
333 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400334 crate_name: "foo",
335 }
336 genrule {
337 name: "my_generator",
338 tools: ["any_rust_binary"],
339 cmd: "$(location) -o $(out) $(in)",
340 srcs: ["src/any.h"],
341 out: ["src/any.rs"],
342 }
Colin Crosse9fe2942020-11-10 18:12:15 -0800343 rust_binary_host {
344 name: "any_rust_binary",
345 srcs: [
346 "foo.rs",
347 ],
348 }
Ivan Lozano43845682020-07-09 21:03:28 -0400349 rust_bindgen {
350 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400351 crate_name: "bindings",
352 source_stem: "bindings",
Ivan Lozano43845682020-07-09 21:03:28 -0400353 host_supported: true,
354 wrapper_src: "src/any.h",
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400355 }
Ivan Lozano43845682020-07-09 21:03:28 -0400356 `)
357
Ivan Lozano2b081132020-09-08 12:46:52 -0400358 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Rule("rustc")
Ivan Lozano43845682020-07-09 21:03:28 -0400359 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/bindings.rs") {
360 t.Errorf("rust_bindgen generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
361 }
362 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/any.rs") {
363 t.Errorf("genrule generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
364 }
365
366 fizzBuzz := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Rule("rustc")
367 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/bindings.rs") {
368 t.Errorf("rust_bindgen generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
369 }
370 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/any.rs") {
371 t.Errorf("genrule generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
372 }
373
374 libprocmacro := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Rule("rustc")
375 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/bindings.rs") {
376 t.Errorf("rust_bindgen generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
377 }
378 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/any.rs") {
379 t.Errorf("genrule generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
380 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400381
382 // Check that our bindings are picked up as crate dependencies as well
383 libfooMod := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
Ivan Lozano4df02572023-06-15 14:21:09 -0400384 if !android.InList("libbindings", libfooMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400385 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
386 }
387 fizzBuzzMod := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Module().(*Module)
Ivan Lozano4df02572023-06-15 14:21:09 -0400388 if !android.InList("libbindings", fizzBuzzMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400389 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
390 }
391 libprocmacroMod := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400392 if !android.InList("libbindings.rlib-std", libprocmacroMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400393 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
394 }
Ivan Lozano43845682020-07-09 21:03:28 -0400395}
396
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400397func TestSourceProviderTargetMismatch(t *testing.T) {
398 // This might error while building the dependency tree or when calling depsToPaths() depending on the lunched
399 // target, which results in two different errors. So don't check the error, just confirm there is one.
400 testRustError(t, ".*", `
401 rust_proc_macro {
402 name: "libprocmacro",
403 srcs: [
404 "foo.rs",
405 ":libbindings",
406 ],
407 crate_name: "procmacro",
408 }
409 rust_bindgen {
410 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400411 crate_name: "bindings",
412 source_stem: "bindings",
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400413 wrapper_src: "src/any.h",
414 }
415 `)
416}
417
Ivan Lozanob9040d62019-09-24 13:23:50 -0700418// Test to make sure proc_macros use host variants when building device modules.
419func TestProcMacroDeviceDeps(t *testing.T) {
420 ctx := testRust(t, `
421 rust_library_host_rlib {
422 name: "libbar",
423 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700424 crate_name: "bar",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700425 }
426 rust_proc_macro {
427 name: "libpm",
428 rlibs: ["libbar"],
429 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700430 crate_name: "pm",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700431 }
432 rust_binary {
433 name: "fizz-buzz",
434 proc_macros: ["libpm"],
435 srcs: ["foo.rs"],
436 }
437 `)
438 rustc := ctx.ModuleForTests("libpm", "linux_glibc_x86_64").Rule("rustc")
439
440 if !strings.Contains(rustc.Args["libFlags"], "libbar/linux_glibc_x86_64") {
441 t.Errorf("Proc_macro is not using host variant of dependent modules.")
442 }
443}
Matthew Maurer99020b02019-10-31 10:44:40 -0700444
445// Test that no_stdlibs suppresses dependencies on rust standard libraries
446func TestNoStdlibs(t *testing.T) {
447 ctx := testRust(t, `
448 rust_binary {
449 name: "fizz-buzz",
450 srcs: ["foo.rs"],
Ivan Lozano9d1df102020-04-28 10:10:23 -0400451 no_stdlibs: true,
Matthew Maurer99020b02019-10-31 10:44:40 -0700452 }`)
Colin Cross7113d202019-11-20 16:39:12 -0800453 module := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module)
Matthew Maurer99020b02019-10-31 10:44:40 -0700454
455 if android.InList("libstd", module.Properties.AndroidMkDylibs) {
456 t.Errorf("no_stdlibs did not suppress dependency on libstd")
457 }
458}
Ivan Lozano9d1df102020-04-28 10:10:23 -0400459
460// Test that libraries provide both 32-bit and 64-bit variants.
461func TestMultilib(t *testing.T) {
462 ctx := testRust(t, `
463 rust_library_rlib {
464 name: "libfoo",
465 srcs: ["foo.rs"],
466 crate_name: "foo",
467 }`)
468
Ivan Lozano2b081132020-09-08 12:46:52 -0400469 _ = ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std")
470 _ = ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_rlib_dylib-std")
Ivan Lozano9d1df102020-04-28 10:10:23 -0400471}
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200472
473// Test that library size measurements are generated.
474func TestLibrarySizes(t *testing.T) {
475 ctx := testRust(t, `
476 rust_library_dylib {
477 name: "libwaldo",
478 srcs: ["foo.rs"],
479 crate_name: "waldo",
480 }`)
481
482 m := ctx.SingletonForTests("file_metrics")
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400483 m.Output("unstripped/libwaldo.dylib.so.bloaty.csv")
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200484 m.Output("libwaldo.dylib.so.bloaty.csv")
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200485}
Ivan Lozano62cd0382021-11-01 10:27:54 -0400486
487func assertString(t *testing.T, got, expected string) {
488 t.Helper()
489 if got != expected {
490 t.Errorf("expected %q got %q", expected, got)
491 }
492}