blob: 295a734b6c1a3e9acdb2ab3a4217df0a39e8e2e5 [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")
Ivan Lozano1921e802021-05-20 13:39:16 -040043 variables.Platform_vndk_version = StringPtr("29")
44 }),
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000045)
46
47var rustMockedFiles = android.MockFS{
Ivan Lozano1921e802021-05-20 13:39:16 -040048 "foo.rs": nil,
49 "foo.c": nil,
50 "src/bar.rs": nil,
51 "src/any.h": nil,
52 "c_includes/c_header.h": nil,
53 "rust_includes/rust_headers.h": nil,
54 "proto.proto": nil,
55 "proto/buf.proto": nil,
56 "buf.proto": nil,
57 "foo.proto": nil,
58 "liby.so": nil,
59 "libz.so": nil,
60 "data.txt": nil,
Ivan Lozano3149e6e2021-06-01 15:09:53 -040061 "liblog.map.txt": nil,
Ivan Lozanoffee3342019-08-27 12:03:00 -070062}
63
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020064// testRust returns a TestContext in which a basic environment has been setup.
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000065// This environment contains a few mocked files. See rustMockedFiles for the list of these files.
Wen-yi Chu41326c12023-09-22 03:58:59 +000066func testRust(t *testing.T, bp string) *android.TestContext {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000067 skipTestIfOsNotSupported(t)
68 result := android.GroupFixturePreparers(
69 prepareForRustTest,
70 rustMockedFiles.AddToFixture(),
71 ).
72 RunTestWithBp(t, bp)
73 return result.TestContext
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020074}
Colin Cross98be1bb2019-12-13 20:41:13 -080075
Ivan Lozanof76cdf72021-02-12 09:55:06 -050076func testRustVndk(t *testing.T, bp string) *android.TestContext {
Ivan Lozano1921e802021-05-20 13:39:16 -040077 return testRustVndkFs(t, bp, rustMockedFiles)
78}
79
Ivan Lozano3149e6e2021-06-01 15:09:53 -040080const (
Ivan Lozanoadd122a2023-07-13 11:01:41 -040081 sharedVendorVariant = "android_vendor.29_arm64_armv8-a_shared"
82 rlibVendorVariant = "android_vendor.29_arm64_armv8-a_rlib_rlib-std"
83 rlibDylibStdVendorVariant = "android_vendor.29_arm64_armv8-a_rlib_rlib-std"
84 dylibVendorVariant = "android_vendor.29_arm64_armv8-a_dylib"
85 sharedRecoveryVariant = "android_recovery_arm64_armv8-a_shared"
86 rlibRecoveryVariant = "android_recovery_arm64_armv8-a_rlib_dylib-std"
87 rlibRlibStdRecoveryVariant = "android_recovery_arm64_armv8-a_rlib_rlib-std"
88 dylibRecoveryVariant = "android_recovery_arm64_armv8-a_dylib"
89 binaryCoreVariant = "android_arm64_armv8-a"
90 binaryVendorVariant = "android_vendor.29_arm64_armv8-a"
91 binaryProductVariant = "android_product.29_arm64_armv8-a"
92 binaryRecoveryVariant = "android_recovery_arm64_armv8-a"
Ivan Lozano3149e6e2021-06-01 15:09:53 -040093)
Ivan Lozano1921e802021-05-20 13:39:16 -040094
95func testRustVndkFs(t *testing.T, bp string, fs android.MockFS) *android.TestContext {
Ivan Lozano3149e6e2021-06-01 15:09:53 -040096 return testRustVndkFsVersions(t, bp, fs, "current", "current", "29")
97}
98
99func 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 +0000100 skipTestIfOsNotSupported(t)
101 result := android.GroupFixturePreparers(
102 prepareForRustTest,
Ivan Lozano1921e802021-05-20 13:39:16 -0400103 fs.AddToFixture(),
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000104 android.FixtureModifyProductVariables(
105 func(variables android.FixtureProductVariables) {
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400106 variables.DeviceVndkVersion = StringPtr(device_version)
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400107 variables.Platform_vndk_version = StringPtr(vndk_version)
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000108 },
109 ),
110 ).RunTestWithBp(t, bp)
111 return result.TestContext
Ivan Lozanoc2ca1ee2021-11-09 16:23:40 -0500112}
Ivan Lozano1921e802021-05-20 13:39:16 -0400113
Ivan Lozanoc2ca1ee2021-11-09 16:23:40 -0500114func testRustRecoveryFsVersions(t *testing.T, bp string, fs android.MockFS, device_version, vndk_version, recovery_version string) *android.TestContext {
115 skipTestIfOsNotSupported(t)
116 result := android.GroupFixturePreparers(
117 prepareForRustTest,
118 fs.AddToFixture(),
119 android.FixtureModifyProductVariables(
120 func(variables android.FixtureProductVariables) {
121 variables.DeviceVndkVersion = StringPtr(device_version)
122 variables.RecoverySnapshotVersion = StringPtr(recovery_version)
123 variables.Platform_vndk_version = StringPtr(vndk_version)
124 },
125 ),
126 ).RunTestWithBp(t, bp)
127 return result.TestContext
Ivan Lozanof76cdf72021-02-12 09:55:06 -0500128}
129
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200130// testRustCov returns a TestContext in which a basic environment has been
131// setup. This environment explicitly enables coverage.
132func testRustCov(t *testing.T, bp string) *android.TestContext {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000133 skipTestIfOsNotSupported(t)
134 result := android.GroupFixturePreparers(
135 prepareForRustTest,
136 rustMockedFiles.AddToFixture(),
137 android.FixtureModifyProductVariables(
138 func(variables android.FixtureProductVariables) {
139 variables.ClangCoverage = proptools.BoolPtr(true)
140 variables.Native_coverage = proptools.BoolPtr(true)
141 variables.NativeCoveragePaths = []string{"*"}
142 },
143 ),
144 ).RunTestWithBp(t, bp)
145 return result.TestContext
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200146}
147
148// testRustError ensures that at least one error was raised and its value
149// matches the pattern provided. The error can be either in the parsing of the
150// Blueprint or when generating the build actions.
151func testRustError(t *testing.T, pattern string, bp string) {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000152 skipTestIfOsNotSupported(t)
153 android.GroupFixturePreparers(
154 prepareForRustTest,
155 rustMockedFiles.AddToFixture(),
156 ).
157 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
158 RunTestWithBp(t, bp)
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200159}
160
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400161// testRustVndkError is similar to testRustError, but can be used to test VNDK-related errors.
162func testRustVndkError(t *testing.T, pattern string, bp string) {
Ivan Lozano1921e802021-05-20 13:39:16 -0400163 testRustVndkFsError(t, pattern, bp, rustMockedFiles)
164}
165
166func testRustVndkFsError(t *testing.T, pattern string, bp string, fs android.MockFS) {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400167 skipTestIfOsNotSupported(t)
168 android.GroupFixturePreparers(
169 prepareForRustTest,
Ivan Lozano1921e802021-05-20 13:39:16 -0400170 fs.AddToFixture(),
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400171 android.FixtureModifyProductVariables(
172 func(variables android.FixtureProductVariables) {
173 variables.DeviceVndkVersion = StringPtr("current")
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400174 variables.Platform_vndk_version = StringPtr("VER")
175 },
176 ),
177 ).
178 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
179 RunTestWithBp(t, bp)
180}
181
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200182// testRustCtx is used to build a particular test environment. Unless your
183// tests requires a specific setup, prefer the wrapping functions: testRust,
184// testRustCov or testRustError.
185type testRustCtx struct {
186 bp string
187 fs map[string][]byte
188 env map[string]string
189 config *android.Config
190}
191
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000192func skipTestIfOsNotSupported(t *testing.T) {
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200193 // TODO (b/140435149)
194 if runtime.GOOS != "linux" {
195 t.Skip("Rust Soong tests can only be run on Linux hosts currently")
196 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700197}
198
Ivan Lozanoffee3342019-08-27 12:03:00 -0700199// Test that we can extract the link path from a lib path.
200func TestLinkPathFromFilePath(t *testing.T) {
201 barPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so")
Wen-yi Chu41326c12023-09-22 03:58:59 +0000202 libName := linkPathFromFilePath(barPath)
203 expectedResult := "out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/"
Ivan Lozanoffee3342019-08-27 12:03:00 -0700204
Wen-yi Chu41326c12023-09-22 03:58:59 +0000205 if libName != expectedResult {
206 t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700207 }
208}
209
Ivan Lozanoffee3342019-08-27 12:03:00 -0700210// Test to make sure dependencies are being picked up correctly.
211func TestDepsTracking(t *testing.T) {
212 ctx := testRust(t, `
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400213 cc_library {
214 host_supported: true,
215 name: "cc_stubs_dep",
216 }
Matthew Maurer2ae05132020-06-23 14:28:53 -0700217 rust_ffi_host_static {
Ivan Lozano52767be2019-10-18 14:49:46 -0700218 name: "libstatic",
219 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700220 crate_name: "static",
Ivan Lozano52767be2019-10-18 14:49:46 -0700221 }
Ivan Lozano63bb7682021-03-23 15:53:44 -0400222 rust_ffi_host_static {
223 name: "libwholestatic",
224 srcs: ["foo.rs"],
225 crate_name: "wholestatic",
226 }
Matthew Maurer2ae05132020-06-23 14:28:53 -0700227 rust_ffi_host_shared {
Ivan Lozano52767be2019-10-18 14:49:46 -0700228 name: "libshared",
229 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700230 crate_name: "shared",
Ivan Lozano52767be2019-10-18 14:49:46 -0700231 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700232 rust_library_host_rlib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700233 name: "librlib",
Ivan Lozano43845682020-07-09 21:03:28 -0400234 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700235 crate_name: "rlib",
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500236 static_libs: ["libstatic"],
Ivan Lozano63bb7682021-03-23 15:53:44 -0400237 whole_static_libs: ["libwholestatic"],
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400238 shared_libs: ["cc_stubs_dep"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700239 }
240 rust_proc_macro {
241 name: "libpm",
242 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700243 crate_name: "pm",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700244 }
245 rust_binary_host {
Ivan Lozano43845682020-07-09 21:03:28 -0400246 name: "fizz-buzz",
Ivan Lozano52767be2019-10-18 14:49:46 -0700247 rlibs: ["librlib"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700248 proc_macros: ["libpm"],
Ivan Lozano52767be2019-10-18 14:49:46 -0700249 static_libs: ["libstatic"],
250 shared_libs: ["libshared"],
Ivan Lozano43845682020-07-09 21:03:28 -0400251 srcs: ["foo.rs"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700252 }
253 `)
Ivan Lozano43845682020-07-09 21:03:28 -0400254 module := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500255 rustc := ctx.ModuleForTests("librlib", "linux_glibc_x86_64_rlib_rlib-std").Rule("rustc")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700256
257 // 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 -0400258 if !android.InList("librlib.rlib-std", module.Properties.AndroidMkRlibs) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700259 t.Errorf("Rlib dependency not detected (dependency missing from AndroidMkRlibs)")
260 }
261
262 if !android.InList("libpm", module.Properties.AndroidMkProcMacroLibs) {
263 t.Errorf("Proc_macro dependency not detected (dependency missing from AndroidMkProcMacroLibs)")
264 }
265
Cole Faustb6e6f992023-08-17 17:42:26 -0700266 if !android.InList("libshared", module.transitiveAndroidMkSharedLibs.ToList()) {
Ivan Lozano52767be2019-10-18 14:49:46 -0700267 t.Errorf("Shared library dependency not detected (dependency missing from AndroidMkSharedLibs)")
268 }
269
270 if !android.InList("libstatic", module.Properties.AndroidMkStaticLibs) {
271 t.Errorf("Static library dependency not detected (dependency missing from AndroidMkStaticLibs)")
272 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500273
Wen-yi Chu41326c12023-09-22 03:58:59 +0000274 if !strings.Contains(rustc.Args["rustcFlags"], "-lstatic=wholestatic") {
275 t.Errorf("-lstatic flag not being passed to rustc for static library %#v", rustc.Args["rustcFlags"])
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500276 }
277
Colin Cross004bd3f2023-10-02 11:39:17 -0700278 if !strings.Contains(rustc.Args["linkFlags"], "cc_stubs_dep.so") {
279 t.Errorf("shared cc_library not being passed to rustc linkFlags %#v", rustc.Args["linkFlags"])
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400280 }
281
Colin Cross004bd3f2023-10-02 11:39:17 -0700282 if !android.SuffixInList(rustc.OrderOnly.Strings(), "cc_stubs_dep.so") {
283 t.Errorf("shared cc dep not being passed as order-only to rustc %#v", rustc.OrderOnly.Strings())
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400284 }
285
Colin Cross004bd3f2023-10-02 11:39:17 -0700286 if !android.SuffixInList(rustc.Implicits.Strings(), "cc_stubs_dep.so.toc") {
287 t.Errorf("shared cc dep TOC not being passed as implicit to rustc %#v", rustc.Implicits.Strings())
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400288 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700289}
Ivan Lozanob9040d62019-09-24 13:23:50 -0700290
Ivan Lozano43845682020-07-09 21:03:28 -0400291func TestSourceProviderDeps(t *testing.T) {
292 ctx := testRust(t, `
293 rust_binary {
294 name: "fizz-buzz-dep",
295 srcs: [
296 "foo.rs",
297 ":my_generator",
298 ":libbindings",
299 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400300 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400301 }
302 rust_proc_macro {
303 name: "libprocmacro",
304 srcs: [
305 "foo.rs",
306 ":my_generator",
307 ":libbindings",
308 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400309 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400310 crate_name: "procmacro",
311 }
312 rust_library {
313 name: "libfoo",
314 srcs: [
315 "foo.rs",
316 ":my_generator",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400317 ":libbindings",
318 ],
319 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400320 crate_name: "foo",
321 }
322 genrule {
323 name: "my_generator",
324 tools: ["any_rust_binary"],
325 cmd: "$(location) -o $(out) $(in)",
326 srcs: ["src/any.h"],
327 out: ["src/any.rs"],
328 }
Colin Crosse9fe2942020-11-10 18:12:15 -0800329 rust_binary_host {
330 name: "any_rust_binary",
331 srcs: [
332 "foo.rs",
333 ],
334 }
Ivan Lozano43845682020-07-09 21:03:28 -0400335 rust_bindgen {
336 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400337 crate_name: "bindings",
338 source_stem: "bindings",
Ivan Lozano43845682020-07-09 21:03:28 -0400339 host_supported: true,
340 wrapper_src: "src/any.h",
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400341 }
Ivan Lozano43845682020-07-09 21:03:28 -0400342 `)
343
Ivan Lozano2b081132020-09-08 12:46:52 -0400344 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Rule("rustc")
Ivan Lozano43845682020-07-09 21:03:28 -0400345 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/bindings.rs") {
346 t.Errorf("rust_bindgen generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
347 }
348 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/any.rs") {
349 t.Errorf("genrule generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
350 }
351
352 fizzBuzz := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Rule("rustc")
353 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/bindings.rs") {
354 t.Errorf("rust_bindgen generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
355 }
356 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/any.rs") {
357 t.Errorf("genrule generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
358 }
359
360 libprocmacro := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Rule("rustc")
361 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/bindings.rs") {
362 t.Errorf("rust_bindgen generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
363 }
364 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/any.rs") {
365 t.Errorf("genrule generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
366 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400367
368 // Check that our bindings are picked up as crate dependencies as well
369 libfooMod := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
Ivan Lozano4df02572023-06-15 14:21:09 -0400370 if !android.InList("libbindings", libfooMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400371 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
372 }
373 fizzBuzzMod := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Module().(*Module)
Ivan Lozano4df02572023-06-15 14:21:09 -0400374 if !android.InList("libbindings", fizzBuzzMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400375 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
376 }
377 libprocmacroMod := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400378 if !android.InList("libbindings.rlib-std", libprocmacroMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400379 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
380 }
Ivan Lozano43845682020-07-09 21:03:28 -0400381}
382
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400383func TestSourceProviderTargetMismatch(t *testing.T) {
384 // This might error while building the dependency tree or when calling depsToPaths() depending on the lunched
385 // target, which results in two different errors. So don't check the error, just confirm there is one.
386 testRustError(t, ".*", `
387 rust_proc_macro {
388 name: "libprocmacro",
389 srcs: [
390 "foo.rs",
391 ":libbindings",
392 ],
393 crate_name: "procmacro",
394 }
395 rust_bindgen {
396 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400397 crate_name: "bindings",
398 source_stem: "bindings",
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400399 wrapper_src: "src/any.h",
400 }
401 `)
402}
403
Ivan Lozanob9040d62019-09-24 13:23:50 -0700404// Test to make sure proc_macros use host variants when building device modules.
405func TestProcMacroDeviceDeps(t *testing.T) {
406 ctx := testRust(t, `
407 rust_library_host_rlib {
408 name: "libbar",
409 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700410 crate_name: "bar",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700411 }
412 rust_proc_macro {
413 name: "libpm",
414 rlibs: ["libbar"],
415 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700416 crate_name: "pm",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700417 }
418 rust_binary {
419 name: "fizz-buzz",
420 proc_macros: ["libpm"],
421 srcs: ["foo.rs"],
422 }
423 `)
424 rustc := ctx.ModuleForTests("libpm", "linux_glibc_x86_64").Rule("rustc")
425
Wen-yi Chu41326c12023-09-22 03:58:59 +0000426 if !strings.Contains(rustc.Args["libFlags"], "libbar/linux_glibc_x86_64") {
Ivan Lozanob9040d62019-09-24 13:23:50 -0700427 t.Errorf("Proc_macro is not using host variant of dependent modules.")
428 }
429}
Matthew Maurer99020b02019-10-31 10:44:40 -0700430
431// Test that no_stdlibs suppresses dependencies on rust standard libraries
432func TestNoStdlibs(t *testing.T) {
433 ctx := testRust(t, `
434 rust_binary {
435 name: "fizz-buzz",
436 srcs: ["foo.rs"],
Ivan Lozano9d1df102020-04-28 10:10:23 -0400437 no_stdlibs: true,
Matthew Maurer99020b02019-10-31 10:44:40 -0700438 }`)
Colin Cross7113d202019-11-20 16:39:12 -0800439 module := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module)
Matthew Maurer99020b02019-10-31 10:44:40 -0700440
441 if android.InList("libstd", module.Properties.AndroidMkDylibs) {
442 t.Errorf("no_stdlibs did not suppress dependency on libstd")
443 }
444}
Ivan Lozano9d1df102020-04-28 10:10:23 -0400445
446// Test that libraries provide both 32-bit and 64-bit variants.
447func TestMultilib(t *testing.T) {
448 ctx := testRust(t, `
449 rust_library_rlib {
450 name: "libfoo",
451 srcs: ["foo.rs"],
452 crate_name: "foo",
453 }`)
454
Ivan Lozano2b081132020-09-08 12:46:52 -0400455 _ = ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std")
456 _ = ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_rlib_dylib-std")
Ivan Lozano9d1df102020-04-28 10:10:23 -0400457}
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200458
459// Test that library size measurements are generated.
460func TestLibrarySizes(t *testing.T) {
461 ctx := testRust(t, `
462 rust_library_dylib {
463 name: "libwaldo",
464 srcs: ["foo.rs"],
465 crate_name: "waldo",
466 }`)
467
468 m := ctx.SingletonForTests("file_metrics")
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400469 m.Output("unstripped/libwaldo.dylib.so.bloaty.csv")
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200470 m.Output("libwaldo.dylib.so.bloaty.csv")
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200471}
Ivan Lozano62cd0382021-11-01 10:27:54 -0400472
Andrew Walbran52533232024-03-19 11:36:04 +0000473// Test that aliases are respected.
474func TestRustAliases(t *testing.T) {
475 ctx := testRust(t, `
476 rust_library {
477 name: "libbar",
478 crate_name: "bar",
479 srcs: ["src/lib.rs"],
480 }
481 rust_library {
482 name: "libbaz",
483 crate_name: "baz",
484 srcs: ["src/lib.rs"],
485 }
486 rust_binary {
487 name: "foo",
488 srcs: ["src/main.rs"],
489 rustlibs: ["libbar", "libbaz"],
490 aliases: ["bar:bar_renamed"],
491 }`)
492
493 fooRustc := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Rule("rustc")
494 if !strings.Contains(fooRustc.Args["libFlags"], "--extern bar_renamed=out/soong/.intermediates/libbar/android_arm64_armv8-a_dylib/unstripped/libbar.dylib.so") {
495 t.Errorf("--extern 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"])
496 }
497 if !strings.Contains(fooRustc.Args["libFlags"], "--extern baz=out/soong/.intermediates/libbaz/android_arm64_armv8-a_dylib/unstripped/libbaz.dylib.so") {
498 t.Errorf("--extern 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"])
499 }
500}
501
Ivan Lozano62cd0382021-11-01 10:27:54 -0400502func assertString(t *testing.T, got, expected string) {
503 t.Helper()
504 if got != expected {
505 t.Errorf("expected %q got %q", expected, got)
506 }
507}