blob: 8b96df8b32258997f6391b985be5d4896cfa0c40 [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 {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000063 skipTestIfOsNotSupported(t)
64 result := android.GroupFixturePreparers(
65 prepareForRustTest,
66 rustMockedFiles.AddToFixture(),
67 ).
68 RunTestWithBp(t, bp)
69 return result.TestContext
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020070}
Colin Cross98be1bb2019-12-13 20:41:13 -080071
Ivan Lozano3149e6e2021-06-01 15:09:53 -040072const (
Kiyoung Kim1db4a742024-04-01 15:50:01 +090073 sharedVendorVariant = "android_vendor_arm64_armv8-a_shared"
74 rlibVendorVariant = "android_vendor_arm64_armv8-a_rlib_rlib-std"
75 rlibDylibStdVendorVariant = "android_vendor_arm64_armv8-a_rlib_rlib-std"
76 dylibVendorVariant = "android_vendor_arm64_armv8-a_dylib"
Ivan Lozanoadd122a2023-07-13 11:01:41 -040077 sharedRecoveryVariant = "android_recovery_arm64_armv8-a_shared"
78 rlibRecoveryVariant = "android_recovery_arm64_armv8-a_rlib_dylib-std"
79 rlibRlibStdRecoveryVariant = "android_recovery_arm64_armv8-a_rlib_rlib-std"
80 dylibRecoveryVariant = "android_recovery_arm64_armv8-a_dylib"
81 binaryCoreVariant = "android_arm64_armv8-a"
Kiyoung Kim1db4a742024-04-01 15:50:01 +090082 binaryVendorVariant = "android_vendor_arm64_armv8-a"
83 binaryProductVariant = "android_product_arm64_armv8-a"
Ivan Lozanoadd122a2023-07-13 11:01:41 -040084 binaryRecoveryVariant = "android_recovery_arm64_armv8-a"
Ivan Lozano3149e6e2021-06-01 15:09:53 -040085)
Ivan Lozano1921e802021-05-20 13:39:16 -040086
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020087// testRustCov returns a TestContext in which a basic environment has been
88// setup. This environment explicitly enables coverage.
89func testRustCov(t *testing.T, bp string) *android.TestContext {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000090 skipTestIfOsNotSupported(t)
91 result := android.GroupFixturePreparers(
92 prepareForRustTest,
93 rustMockedFiles.AddToFixture(),
94 android.FixtureModifyProductVariables(
95 func(variables android.FixtureProductVariables) {
96 variables.ClangCoverage = proptools.BoolPtr(true)
97 variables.Native_coverage = proptools.BoolPtr(true)
98 variables.NativeCoveragePaths = []string{"*"}
99 },
100 ),
101 ).RunTestWithBp(t, bp)
102 return result.TestContext
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200103}
104
105// testRustError ensures that at least one error was raised and its value
106// matches the pattern provided. The error can be either in the parsing of the
107// Blueprint or when generating the build actions.
108func testRustError(t *testing.T, pattern string, bp string) {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000109 skipTestIfOsNotSupported(t)
110 android.GroupFixturePreparers(
111 prepareForRustTest,
112 rustMockedFiles.AddToFixture(),
113 ).
114 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
115 RunTestWithBp(t, bp)
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200116}
117
118// testRustCtx is used to build a particular test environment. Unless your
119// tests requires a specific setup, prefer the wrapping functions: testRust,
120// testRustCov or testRustError.
121type testRustCtx struct {
122 bp string
123 fs map[string][]byte
124 env map[string]string
125 config *android.Config
126}
127
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000128func skipTestIfOsNotSupported(t *testing.T) {
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200129 // TODO (b/140435149)
130 if runtime.GOOS != "linux" {
131 t.Skip("Rust Soong tests can only be run on Linux hosts currently")
132 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700133}
134
Ivan Lozanoffee3342019-08-27 12:03:00 -0700135// Test that we can extract the link path from a lib path.
136func TestLinkPathFromFilePath(t *testing.T) {
137 barPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so")
Wen-yi Chu41326c12023-09-22 03:58:59 +0000138 libName := linkPathFromFilePath(barPath)
139 expectedResult := "out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/"
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140
Wen-yi Chu41326c12023-09-22 03:58:59 +0000141 if libName != expectedResult {
142 t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143 }
144}
145
Ivan Lozanoffee3342019-08-27 12:03:00 -0700146// Test to make sure dependencies are being picked up correctly.
147func TestDepsTracking(t *testing.T) {
148 ctx := testRust(t, `
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400149 cc_library {
150 host_supported: true,
151 name: "cc_stubs_dep",
152 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400153 cc_library_host_static {
Ivan Lozano52767be2019-10-18 14:49:46 -0700154 name: "libstatic",
Ivan Lozano52767be2019-10-18 14:49:46 -0700155 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400156 cc_library_host_static {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400157 name: "libwholestatic",
Ivan Lozano63bb7682021-03-23 15:53:44 -0400158 }
Matthew Maurer2ae05132020-06-23 14:28:53 -0700159 rust_ffi_host_shared {
Ivan Lozano52767be2019-10-18 14:49:46 -0700160 name: "libshared",
161 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700162 crate_name: "shared",
Ivan Lozano52767be2019-10-18 14:49:46 -0700163 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700164 rust_library_host_rlib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700165 name: "librlib",
Ivan Lozano43845682020-07-09 21:03:28 -0400166 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700167 crate_name: "rlib",
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500168 static_libs: ["libstatic"],
Ivan Lozano63bb7682021-03-23 15:53:44 -0400169 whole_static_libs: ["libwholestatic"],
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400170 shared_libs: ["cc_stubs_dep"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700171 }
172 rust_proc_macro {
173 name: "libpm",
174 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700175 crate_name: "pm",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700176 }
177 rust_binary_host {
Ivan Lozano43845682020-07-09 21:03:28 -0400178 name: "fizz-buzz",
Ivan Lozano52767be2019-10-18 14:49:46 -0700179 rlibs: ["librlib"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700180 proc_macros: ["libpm"],
Ivan Lozano52767be2019-10-18 14:49:46 -0700181 static_libs: ["libstatic"],
182 shared_libs: ["libshared"],
Ivan Lozano43845682020-07-09 21:03:28 -0400183 srcs: ["foo.rs"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700184 }
185 `)
Ivan Lozano43845682020-07-09 21:03:28 -0400186 module := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500187 rustc := ctx.ModuleForTests("librlib", "linux_glibc_x86_64_rlib_rlib-std").Rule("rustc")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700188
189 // 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 -0400190 if !android.InList("librlib.rlib-std", module.Properties.AndroidMkRlibs) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700191 t.Errorf("Rlib dependency not detected (dependency missing from AndroidMkRlibs)")
192 }
193
194 if !android.InList("libpm", module.Properties.AndroidMkProcMacroLibs) {
195 t.Errorf("Proc_macro dependency not detected (dependency missing from AndroidMkProcMacroLibs)")
196 }
197
Cole Faustb6e6f992023-08-17 17:42:26 -0700198 if !android.InList("libshared", module.transitiveAndroidMkSharedLibs.ToList()) {
Ivan Lozano52767be2019-10-18 14:49:46 -0700199 t.Errorf("Shared library dependency not detected (dependency missing from AndroidMkSharedLibs)")
200 }
201
202 if !android.InList("libstatic", module.Properties.AndroidMkStaticLibs) {
203 t.Errorf("Static library dependency not detected (dependency missing from AndroidMkStaticLibs)")
204 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500205
Wen-yi Chu41326c12023-09-22 03:58:59 +0000206 if !strings.Contains(rustc.Args["rustcFlags"], "-lstatic=wholestatic") {
207 t.Errorf("-lstatic flag not being passed to rustc for static library %#v", rustc.Args["rustcFlags"])
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500208 }
209
Colin Cross004bd3f2023-10-02 11:39:17 -0700210 if !strings.Contains(rustc.Args["linkFlags"], "cc_stubs_dep.so") {
211 t.Errorf("shared cc_library not being passed to rustc linkFlags %#v", rustc.Args["linkFlags"])
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400212 }
213
Colin Cross004bd3f2023-10-02 11:39:17 -0700214 if !android.SuffixInList(rustc.OrderOnly.Strings(), "cc_stubs_dep.so") {
215 t.Errorf("shared cc dep not being passed as order-only to rustc %#v", rustc.OrderOnly.Strings())
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400216 }
217
Colin Cross004bd3f2023-10-02 11:39:17 -0700218 if !android.SuffixInList(rustc.Implicits.Strings(), "cc_stubs_dep.so.toc") {
219 t.Errorf("shared cc dep TOC not being passed as implicit to rustc %#v", rustc.Implicits.Strings())
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400220 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700221}
Ivan Lozanob9040d62019-09-24 13:23:50 -0700222
Ivan Lozano43845682020-07-09 21:03:28 -0400223func TestSourceProviderDeps(t *testing.T) {
224 ctx := testRust(t, `
225 rust_binary {
226 name: "fizz-buzz-dep",
227 srcs: [
228 "foo.rs",
229 ":my_generator",
230 ":libbindings",
231 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400232 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400233 }
234 rust_proc_macro {
235 name: "libprocmacro",
236 srcs: [
237 "foo.rs",
238 ":my_generator",
239 ":libbindings",
240 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400241 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400242 crate_name: "procmacro",
243 }
244 rust_library {
245 name: "libfoo",
246 srcs: [
247 "foo.rs",
248 ":my_generator",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400249 ":libbindings",
250 ],
251 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400252 crate_name: "foo",
253 }
254 genrule {
255 name: "my_generator",
256 tools: ["any_rust_binary"],
257 cmd: "$(location) -o $(out) $(in)",
258 srcs: ["src/any.h"],
259 out: ["src/any.rs"],
260 }
Colin Crosse9fe2942020-11-10 18:12:15 -0800261 rust_binary_host {
262 name: "any_rust_binary",
263 srcs: [
264 "foo.rs",
265 ],
266 }
Ivan Lozano43845682020-07-09 21:03:28 -0400267 rust_bindgen {
268 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400269 crate_name: "bindings",
270 source_stem: "bindings",
Ivan Lozano43845682020-07-09 21:03:28 -0400271 host_supported: true,
272 wrapper_src: "src/any.h",
Sam Delmerico51d6d1c2023-03-28 16:54:00 -0400273 }
Ivan Lozano43845682020-07-09 21:03:28 -0400274 `)
275
Ivan Lozano2b081132020-09-08 12:46:52 -0400276 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Rule("rustc")
Ivan Lozano43845682020-07-09 21:03:28 -0400277 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/bindings.rs") {
278 t.Errorf("rust_bindgen generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
279 }
280 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/any.rs") {
281 t.Errorf("genrule generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
282 }
283
284 fizzBuzz := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Rule("rustc")
285 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/bindings.rs") {
286 t.Errorf("rust_bindgen generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
287 }
288 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/any.rs") {
289 t.Errorf("genrule generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
290 }
291
292 libprocmacro := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Rule("rustc")
293 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/bindings.rs") {
294 t.Errorf("rust_bindgen generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
295 }
296 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/any.rs") {
297 t.Errorf("genrule generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
298 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400299
300 // Check that our bindings are picked up as crate dependencies as well
301 libfooMod := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
Ivan Lozano4df02572023-06-15 14:21:09 -0400302 if !android.InList("libbindings", libfooMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400303 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
304 }
305 fizzBuzzMod := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Module().(*Module)
Ivan Lozano4df02572023-06-15 14:21:09 -0400306 if !android.InList("libbindings", fizzBuzzMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400307 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
308 }
309 libprocmacroMod := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400310 if !android.InList("libbindings.rlib-std", libprocmacroMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400311 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
312 }
Ivan Lozano43845682020-07-09 21:03:28 -0400313}
314
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400315func TestSourceProviderTargetMismatch(t *testing.T) {
316 // This might error while building the dependency tree or when calling depsToPaths() depending on the lunched
317 // target, which results in two different errors. So don't check the error, just confirm there is one.
318 testRustError(t, ".*", `
319 rust_proc_macro {
320 name: "libprocmacro",
321 srcs: [
322 "foo.rs",
323 ":libbindings",
324 ],
325 crate_name: "procmacro",
326 }
327 rust_bindgen {
328 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400329 crate_name: "bindings",
330 source_stem: "bindings",
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400331 wrapper_src: "src/any.h",
332 }
333 `)
334}
335
Ivan Lozanob9040d62019-09-24 13:23:50 -0700336// Test to make sure proc_macros use host variants when building device modules.
337func TestProcMacroDeviceDeps(t *testing.T) {
338 ctx := testRust(t, `
339 rust_library_host_rlib {
340 name: "libbar",
341 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700342 crate_name: "bar",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700343 }
344 rust_proc_macro {
345 name: "libpm",
346 rlibs: ["libbar"],
347 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700348 crate_name: "pm",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700349 }
350 rust_binary {
351 name: "fizz-buzz",
352 proc_macros: ["libpm"],
353 srcs: ["foo.rs"],
354 }
355 `)
356 rustc := ctx.ModuleForTests("libpm", "linux_glibc_x86_64").Rule("rustc")
357
Wen-yi Chu41326c12023-09-22 03:58:59 +0000358 if !strings.Contains(rustc.Args["libFlags"], "libbar/linux_glibc_x86_64") {
Ivan Lozanob9040d62019-09-24 13:23:50 -0700359 t.Errorf("Proc_macro is not using host variant of dependent modules.")
360 }
361}
Matthew Maurer99020b02019-10-31 10:44:40 -0700362
363// Test that no_stdlibs suppresses dependencies on rust standard libraries
364func TestNoStdlibs(t *testing.T) {
365 ctx := testRust(t, `
366 rust_binary {
367 name: "fizz-buzz",
368 srcs: ["foo.rs"],
Ivan Lozano9d1df102020-04-28 10:10:23 -0400369 no_stdlibs: true,
Matthew Maurer99020b02019-10-31 10:44:40 -0700370 }`)
Colin Cross7113d202019-11-20 16:39:12 -0800371 module := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module)
Matthew Maurer99020b02019-10-31 10:44:40 -0700372
373 if android.InList("libstd", module.Properties.AndroidMkDylibs) {
374 t.Errorf("no_stdlibs did not suppress dependency on libstd")
375 }
376}
Ivan Lozano9d1df102020-04-28 10:10:23 -0400377
378// Test that libraries provide both 32-bit and 64-bit variants.
379func TestMultilib(t *testing.T) {
380 ctx := testRust(t, `
381 rust_library_rlib {
382 name: "libfoo",
383 srcs: ["foo.rs"],
384 crate_name: "foo",
385 }`)
386
Ivan Lozano2b081132020-09-08 12:46:52 -0400387 _ = ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std")
388 _ = ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_rlib_dylib-std")
Ivan Lozano9d1df102020-04-28 10:10:23 -0400389}
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200390
391// Test that library size measurements are generated.
392func TestLibrarySizes(t *testing.T) {
393 ctx := testRust(t, `
394 rust_library_dylib {
395 name: "libwaldo",
396 srcs: ["foo.rs"],
397 crate_name: "waldo",
398 }`)
399
400 m := ctx.SingletonForTests("file_metrics")
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400401 m.Output("unstripped/libwaldo.dylib.so.bloaty.csv")
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200402 m.Output("libwaldo.dylib.so.bloaty.csv")
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200403}
Ivan Lozano62cd0382021-11-01 10:27:54 -0400404
Andrew Walbran52533232024-03-19 11:36:04 +0000405// Test that aliases are respected.
406func TestRustAliases(t *testing.T) {
407 ctx := testRust(t, `
408 rust_library {
409 name: "libbar",
410 crate_name: "bar",
411 srcs: ["src/lib.rs"],
412 }
413 rust_library {
414 name: "libbaz",
415 crate_name: "baz",
416 srcs: ["src/lib.rs"],
417 }
418 rust_binary {
419 name: "foo",
420 srcs: ["src/main.rs"],
421 rustlibs: ["libbar", "libbaz"],
422 aliases: ["bar:bar_renamed"],
423 }`)
424
425 fooRustc := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Rule("rustc")
426 if !strings.Contains(fooRustc.Args["libFlags"], "--extern bar_renamed=out/soong/.intermediates/libbar/android_arm64_armv8-a_dylib/unstripped/libbar.dylib.so") {
427 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"])
428 }
429 if !strings.Contains(fooRustc.Args["libFlags"], "--extern baz=out/soong/.intermediates/libbaz/android_arm64_armv8-a_dylib/unstripped/libbaz.dylib.so") {
430 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"])
431 }
432}
433
Ivan Lozano0a468a42024-05-13 21:03:34 -0400434func TestRustRlibs(t *testing.T) {
435 ctx := testRust(t, `
436 rust_ffi_rlib {
437 name: "libbar",
438 crate_name: "bar",
439 srcs: ["src/lib.rs"],
440 export_include_dirs: ["bar_includes"]
441 }
442
443 rust_ffi_rlib {
444 name: "libfoo",
445 crate_name: "foo",
446 srcs: ["src/lib.rs"],
447 export_include_dirs: ["foo_includes"]
448 }
449
450 cc_library_shared {
451 name: "libcc_shared",
452 srcs:["foo.c"],
453 static_rlibs: ["libbar"],
454 }
455
456 cc_library_static {
457 name: "libcc_static",
458 srcs:["foo.c"],
459 static_rlibs: ["libfoo"],
460 }
461
462 cc_binary {
463 name: "ccBin",
464 srcs:["foo.c"],
465 static_rlibs: ["libbar"],
466 static_libs: ["libcc_static"],
467 }
468 `)
469
470 libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_rlib_rlib-std").Rule("rustc")
471 libcc_shared_rustc := ctx.ModuleForTests("libcc_shared", "android_arm64_armv8-a_shared").Rule("rustc")
472 libcc_shared_ld := ctx.ModuleForTests("libcc_shared", "android_arm64_armv8-a_shared").Rule("ld")
473 libcc_shared_cc := ctx.ModuleForTests("libcc_shared", "android_arm64_armv8-a_shared").Rule("cc")
474 ccbin_rustc := ctx.ModuleForTests("ccBin", "android_arm64_armv8-a").Rule("rustc")
475 ccbin_ld := ctx.ModuleForTests("ccBin", "android_arm64_armv8-a").Rule("ld")
476 ccbin_cc := ctx.ModuleForTests("ccBin", "android_arm64_armv8-a").Rule("cc")
477
478 if !strings.Contains(libbar.Args["rustcFlags"], "crate-type=rlib") {
479 t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", "rlib", libbar.Args["rustcFlags"])
480 }
481
482 // Make sure there's a rustc command, and it's producing a staticlib
483 if !strings.Contains(libcc_shared_rustc.Args["rustcFlags"], "crate-type=staticlib") {
484 t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v",
485 "staticlib", libcc_shared_rustc.Args["rustcFlags"])
486 }
487
488 // Make sure the static lib is included in the ld command
489 if !strings.Contains(libcc_shared_ld.Args["libFlags"], "generated_rust_staticlib/liblibcc_shared_rust_staticlib.a") {
490 t.Errorf("missing generated static library in linker step libFlags %#v, libFlags: %#v",
491 "libcc_shared.generated_rust_staticlib.a", libcc_shared_ld.Args["libFlags"])
492 }
493
494 // Make sure the static lib includes are in the cc command
495 if !strings.Contains(libcc_shared_cc.Args["cFlags"], "-Ibar_includes") {
496 t.Errorf("missing rlibs includes, expecting %#v, cFlags: %#v",
497 "-Ibar_includes", libcc_shared_cc.Args["cFlags"])
498 }
499
500 // Make sure there's a rustc command, and it's producing a staticlib
501 if !strings.Contains(ccbin_rustc.Args["rustcFlags"], "crate-type=staticlib") {
502 t.Errorf("missing crate-type for static variant, expecting %#v, rustcFlags: %#v", "staticlib", ccbin_rustc.Args["rustcFlags"])
503 }
504
505 // Make sure the static lib is included in the cc command
506 if !strings.Contains(ccbin_ld.Args["libFlags"], "generated_rust_staticlib/libccBin_rust_staticlib.a") {
507 t.Errorf("missing generated static library in linker step libFlags, expecting %#v, libFlags: %#v",
508 "ccBin.generated_rust_staticlib.a", ccbin_ld.Args["libFlags"])
509 }
510
511 // Make sure the static lib includes are in the ld command
512 if !strings.Contains(ccbin_cc.Args["cFlags"], "-Ibar_includes") {
513 t.Errorf("missing rlibs includes, expecting %#v, cFlags: %#v",
514 "-Ibar_includes", ccbin_cc.Args)
515 }
516
517 // Make sure that direct dependencies and indirect dependencies are
518 // propagating correctly to the generated rlib.
519 if !strings.Contains(ccbin_rustc.Args["libFlags"], "--extern foo=") {
520 t.Errorf("Missing indirect dependency libfoo when writing generated Rust staticlib: %#v", ccbin_rustc.Args["libFlags"])
521 }
522 if !strings.Contains(ccbin_rustc.Args["libFlags"], "--extern bar=") {
523 t.Errorf("Missing direct dependency libbar when writing generated Rust staticlib: %#v", ccbin_rustc.Args["libFlags"])
524 }
525
526 // Test indirect includes propagation
527 if !strings.Contains(ccbin_cc.Args["cFlags"], "-Ifoo_includes") {
528 t.Errorf("missing rlibs includes, expecting %#v, cFlags: %#v",
529 "-Ifoo_includes", ccbin_cc.Args)
530 }
531}
532
Ivan Lozano62cd0382021-11-01 10:27:54 -0400533func assertString(t *testing.T, got, expected string) {
534 t.Helper()
535 if got != expected {
536 t.Errorf("expected %q got %q", expected, got)
537 }
538}