blob: bed28ec8f9ba730bf49967f2f769e814e3142e8e [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
40 PrepareForIntegrationTestWithRust,
41)
42
43var rustMockedFiles = android.MockFS{
44 "foo.rs": nil,
45 "foo.c": nil,
46 "src/bar.rs": nil,
47 "src/any.h": nil,
48 "proto.proto": nil,
49 "proto/buf.proto": nil,
50 "buf.proto": nil,
51 "foo.proto": nil,
52 "liby.so": nil,
53 "libz.so": nil,
54 "data.txt": nil,
Ivan Lozanoffee3342019-08-27 12:03:00 -070055}
56
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020057// testRust returns a TestContext in which a basic environment has been setup.
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000058// This environment contains a few mocked files. See rustMockedFiles for the list of these files.
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020059func testRust(t *testing.T, bp string) *android.TestContext {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000060 skipTestIfOsNotSupported(t)
61 result := android.GroupFixturePreparers(
62 prepareForRustTest,
63 rustMockedFiles.AddToFixture(),
64 ).
65 RunTestWithBp(t, bp)
66 return result.TestContext
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020067}
Colin Cross98be1bb2019-12-13 20:41:13 -080068
Ivan Lozanof76cdf72021-02-12 09:55:06 -050069func testRustVndk(t *testing.T, bp string) *android.TestContext {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000070 skipTestIfOsNotSupported(t)
71 result := android.GroupFixturePreparers(
72 prepareForRustTest,
73 rustMockedFiles.AddToFixture(),
74 android.FixtureModifyProductVariables(
75 func(variables android.FixtureProductVariables) {
76 variables.DeviceVndkVersion = StringPtr("current")
77 variables.ProductVndkVersion = StringPtr("current")
78 variables.Platform_vndk_version = StringPtr("VER")
79 },
80 ),
81 ).RunTestWithBp(t, bp)
82 return result.TestContext
Ivan Lozanof76cdf72021-02-12 09:55:06 -050083}
84
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020085// testRustCov returns a TestContext in which a basic environment has been
86// setup. This environment explicitly enables coverage.
87func testRustCov(t *testing.T, bp string) *android.TestContext {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000088 skipTestIfOsNotSupported(t)
89 result := android.GroupFixturePreparers(
90 prepareForRustTest,
91 rustMockedFiles.AddToFixture(),
92 android.FixtureModifyProductVariables(
93 func(variables android.FixtureProductVariables) {
94 variables.ClangCoverage = proptools.BoolPtr(true)
95 variables.Native_coverage = proptools.BoolPtr(true)
96 variables.NativeCoveragePaths = []string{"*"}
97 },
98 ),
99 ).RunTestWithBp(t, bp)
100 return result.TestContext
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200101}
102
103// testRustError ensures that at least one error was raised and its value
104// matches the pattern provided. The error can be either in the parsing of the
105// Blueprint or when generating the build actions.
106func testRustError(t *testing.T, pattern string, bp string) {
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000107 skipTestIfOsNotSupported(t)
108 android.GroupFixturePreparers(
109 prepareForRustTest,
110 rustMockedFiles.AddToFixture(),
111 ).
112 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
113 RunTestWithBp(t, bp)
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200114}
115
116// testRustCtx is used to build a particular test environment. Unless your
117// tests requires a specific setup, prefer the wrapping functions: testRust,
118// testRustCov or testRustError.
119type testRustCtx struct {
120 bp string
121 fs map[string][]byte
122 env map[string]string
123 config *android.Config
124}
125
Paul Duffin2c4ca8d2021-03-07 19:18:38 +0000126func skipTestIfOsNotSupported(t *testing.T) {
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200127 // TODO (b/140435149)
128 if runtime.GOOS != "linux" {
129 t.Skip("Rust Soong tests can only be run on Linux hosts currently")
130 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700131}
132
Ivan Lozanoffee3342019-08-27 12:03:00 -0700133// Test that we can extract the link path from a lib path.
134func TestLinkPathFromFilePath(t *testing.T) {
135 barPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so")
136 libName := linkPathFromFilePath(barPath)
137 expectedResult := "out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/"
138
139 if libName != expectedResult {
140 t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libName)
141 }
142}
143
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144// Test to make sure dependencies are being picked up correctly.
145func TestDepsTracking(t *testing.T) {
146 ctx := testRust(t, `
Matthew Maurer2ae05132020-06-23 14:28:53 -0700147 rust_ffi_host_static {
Ivan Lozano52767be2019-10-18 14:49:46 -0700148 name: "libstatic",
149 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700150 crate_name: "static",
Ivan Lozano52767be2019-10-18 14:49:46 -0700151 }
Matthew Maurer2ae05132020-06-23 14:28:53 -0700152 rust_ffi_host_shared {
Ivan Lozano52767be2019-10-18 14:49:46 -0700153 name: "libshared",
154 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700155 crate_name: "shared",
Ivan Lozano52767be2019-10-18 14:49:46 -0700156 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700157 rust_library_host_dylib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700158 name: "libdylib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700159 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700160 crate_name: "dylib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700161 }
162 rust_library_host_rlib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700163 name: "librlib",
Ivan Lozano43845682020-07-09 21:03:28 -0400164 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700165 crate_name: "rlib",
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500166 static_libs: ["libstatic"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700167 }
168 rust_proc_macro {
169 name: "libpm",
170 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700171 crate_name: "pm",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700172 }
173 rust_binary_host {
Ivan Lozano43845682020-07-09 21:03:28 -0400174 name: "fizz-buzz",
Ivan Lozano52767be2019-10-18 14:49:46 -0700175 dylibs: ["libdylib"],
176 rlibs: ["librlib"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700177 proc_macros: ["libpm"],
Ivan Lozano52767be2019-10-18 14:49:46 -0700178 static_libs: ["libstatic"],
179 shared_libs: ["libshared"],
Ivan Lozano43845682020-07-09 21:03:28 -0400180 srcs: ["foo.rs"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700181 }
182 `)
Ivan Lozano43845682020-07-09 21:03:28 -0400183 module := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500184 rustc := ctx.ModuleForTests("librlib", "linux_glibc_x86_64_rlib_rlib-std").Rule("rustc")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700185
186 // 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 -0700187 if !android.InList("libdylib", module.Properties.AndroidMkDylibs) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700188 t.Errorf("Dylib dependency not detected (dependency missing from AndroidMkDylibs)")
189 }
190
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
Ivan Lozano52767be2019-10-18 14:49:46 -0700199 if !android.InList("libshared", module.Properties.AndroidMkSharedLibs) {
200 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
207 if !strings.Contains(rustc.Args["rustcFlags"], "-lstatic=static") {
208 t.Errorf("-lstatic flag not being passed to rustc for static library")
209 }
210
Ivan Lozanoffee3342019-08-27 12:03:00 -0700211}
Ivan Lozanob9040d62019-09-24 13:23:50 -0700212
Ivan Lozano43845682020-07-09 21:03:28 -0400213func TestSourceProviderDeps(t *testing.T) {
214 ctx := testRust(t, `
215 rust_binary {
216 name: "fizz-buzz-dep",
217 srcs: [
218 "foo.rs",
219 ":my_generator",
220 ":libbindings",
221 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400222 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400223 }
224 rust_proc_macro {
225 name: "libprocmacro",
226 srcs: [
227 "foo.rs",
228 ":my_generator",
229 ":libbindings",
230 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400231 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400232 crate_name: "procmacro",
233 }
234 rust_library {
235 name: "libfoo",
236 srcs: [
237 "foo.rs",
238 ":my_generator",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400239 ":libbindings",
240 ],
241 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400242 crate_name: "foo",
243 }
244 genrule {
245 name: "my_generator",
246 tools: ["any_rust_binary"],
247 cmd: "$(location) -o $(out) $(in)",
248 srcs: ["src/any.h"],
249 out: ["src/any.rs"],
250 }
Colin Crosse9fe2942020-11-10 18:12:15 -0800251 rust_binary_host {
252 name: "any_rust_binary",
253 srcs: [
254 "foo.rs",
255 ],
256 }
Ivan Lozano43845682020-07-09 21:03:28 -0400257 rust_bindgen {
258 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400259 crate_name: "bindings",
260 source_stem: "bindings",
Ivan Lozano43845682020-07-09 21:03:28 -0400261 host_supported: true,
262 wrapper_src: "src/any.h",
263 }
264 `)
265
Ivan Lozano2b081132020-09-08 12:46:52 -0400266 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Rule("rustc")
Ivan Lozano43845682020-07-09 21:03:28 -0400267 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/bindings.rs") {
268 t.Errorf("rust_bindgen generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
269 }
270 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/any.rs") {
271 t.Errorf("genrule generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
272 }
273
274 fizzBuzz := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Rule("rustc")
275 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/bindings.rs") {
276 t.Errorf("rust_bindgen generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
277 }
278 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/any.rs") {
279 t.Errorf("genrule generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
280 }
281
282 libprocmacro := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Rule("rustc")
283 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/bindings.rs") {
284 t.Errorf("rust_bindgen generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
285 }
286 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/any.rs") {
287 t.Errorf("genrule generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
288 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400289
290 // Check that our bindings are picked up as crate dependencies as well
291 libfooMod := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400292 if !android.InList("libbindings.dylib-std", libfooMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400293 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
294 }
295 fizzBuzzMod := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400296 if !android.InList("libbindings.dylib-std", fizzBuzzMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400297 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
298 }
299 libprocmacroMod := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400300 if !android.InList("libbindings.rlib-std", libprocmacroMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400301 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
302 }
303
Ivan Lozano43845682020-07-09 21:03:28 -0400304}
305
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400306func TestSourceProviderTargetMismatch(t *testing.T) {
307 // This might error while building the dependency tree or when calling depsToPaths() depending on the lunched
308 // target, which results in two different errors. So don't check the error, just confirm there is one.
309 testRustError(t, ".*", `
310 rust_proc_macro {
311 name: "libprocmacro",
312 srcs: [
313 "foo.rs",
314 ":libbindings",
315 ],
316 crate_name: "procmacro",
317 }
318 rust_bindgen {
319 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400320 crate_name: "bindings",
321 source_stem: "bindings",
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400322 wrapper_src: "src/any.h",
323 }
324 `)
325}
326
Ivan Lozanob9040d62019-09-24 13:23:50 -0700327// Test to make sure proc_macros use host variants when building device modules.
328func TestProcMacroDeviceDeps(t *testing.T) {
329 ctx := testRust(t, `
330 rust_library_host_rlib {
331 name: "libbar",
332 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700333 crate_name: "bar",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700334 }
335 rust_proc_macro {
336 name: "libpm",
337 rlibs: ["libbar"],
338 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700339 crate_name: "pm",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700340 }
341 rust_binary {
342 name: "fizz-buzz",
343 proc_macros: ["libpm"],
344 srcs: ["foo.rs"],
345 }
346 `)
347 rustc := ctx.ModuleForTests("libpm", "linux_glibc_x86_64").Rule("rustc")
348
349 if !strings.Contains(rustc.Args["libFlags"], "libbar/linux_glibc_x86_64") {
350 t.Errorf("Proc_macro is not using host variant of dependent modules.")
351 }
352}
Matthew Maurer99020b02019-10-31 10:44:40 -0700353
354// Test that no_stdlibs suppresses dependencies on rust standard libraries
355func TestNoStdlibs(t *testing.T) {
356 ctx := testRust(t, `
357 rust_binary {
358 name: "fizz-buzz",
359 srcs: ["foo.rs"],
Ivan Lozano9d1df102020-04-28 10:10:23 -0400360 no_stdlibs: true,
Matthew Maurer99020b02019-10-31 10:44:40 -0700361 }`)
Colin Cross7113d202019-11-20 16:39:12 -0800362 module := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module)
Matthew Maurer99020b02019-10-31 10:44:40 -0700363
364 if android.InList("libstd", module.Properties.AndroidMkDylibs) {
365 t.Errorf("no_stdlibs did not suppress dependency on libstd")
366 }
367}
Ivan Lozano9d1df102020-04-28 10:10:23 -0400368
369// Test that libraries provide both 32-bit and 64-bit variants.
370func TestMultilib(t *testing.T) {
371 ctx := testRust(t, `
372 rust_library_rlib {
373 name: "libfoo",
374 srcs: ["foo.rs"],
375 crate_name: "foo",
376 }`)
377
Ivan Lozano2b081132020-09-08 12:46:52 -0400378 _ = ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std")
379 _ = ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_rlib_dylib-std")
Ivan Lozano9d1df102020-04-28 10:10:23 -0400380}