blob: 81ad5ce770c579d7ea106e9d0d7dc2dd420de9ba [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 (
18 "io/ioutil"
19 "os"
Ivan Lozanoc0083612019-09-03 13:49:39 -070020 "runtime"
Ivan Lozanob9040d62019-09-24 13:23:50 -070021 "strings"
Ivan Lozanoffee3342019-08-27 12:03:00 -070022 "testing"
23
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040024 "github.com/google/blueprint/proptools"
25
Ivan Lozanoffee3342019-08-27 12:03:00 -070026 "android/soong/android"
Colin Crossf28329d2020-02-15 11:00:10 -080027 "android/soong/cc"
Ivan Lozanoffee3342019-08-27 12:03:00 -070028)
29
30var (
31 buildDir string
32)
33
34func setUp() {
35 var err error
36 buildDir, err = ioutil.TempDir("", "soong_rust_test")
37 if err != nil {
38 panic(err)
39 }
40}
41
42func tearDown() {
43 os.RemoveAll(buildDir)
44}
45
46func TestMain(m *testing.M) {
47 run := func() int {
48 setUp()
49 defer tearDown()
50
51 return m.Run()
52 }
53
54 os.Exit(run())
55}
56
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020057// testRust returns a TestContext in which a basic environment has been setup.
58// This environment contains a few mocked files. See testRustCtx.useMockedFs
59// for the list of these files.
60func testRust(t *testing.T, bp string) *android.TestContext {
61 tctx := newTestRustCtx(t, bp)
62 tctx.useMockedFs()
63 tctx.generateConfig()
64 return tctx.parse(t)
65}
Colin Cross98be1bb2019-12-13 20:41:13 -080066
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020067// testRustCov returns a TestContext in which a basic environment has been
68// setup. This environment explicitly enables coverage.
69func testRustCov(t *testing.T, bp string) *android.TestContext {
70 tctx := newTestRustCtx(t, bp)
71 tctx.useMockedFs()
72 tctx.generateConfig()
73 tctx.enableCoverage(t)
74 return tctx.parse(t)
75}
76
77// testRustError ensures that at least one error was raised and its value
78// matches the pattern provided. The error can be either in the parsing of the
79// Blueprint or when generating the build actions.
80func testRustError(t *testing.T, pattern string, bp string) {
81 tctx := newTestRustCtx(t, bp)
82 tctx.useMockedFs()
83 tctx.generateConfig()
84 tctx.parseError(t, pattern)
85}
86
87// testRustCtx is used to build a particular test environment. Unless your
88// tests requires a specific setup, prefer the wrapping functions: testRust,
89// testRustCov or testRustError.
90type testRustCtx struct {
91 bp string
92 fs map[string][]byte
93 env map[string]string
94 config *android.Config
95}
96
97// newTestRustCtx returns a new testRustCtx for the Blueprint definition argument.
98func newTestRustCtx(t *testing.T, bp string) *testRustCtx {
99 // TODO (b/140435149)
100 if runtime.GOOS != "linux" {
101 t.Skip("Rust Soong tests can only be run on Linux hosts currently")
102 }
103 return &testRustCtx{bp: bp}
104}
105
106// useMockedFs setup a default mocked filesystem for the test environment.
107func (tctx *testRustCtx) useMockedFs() {
108 tctx.fs = map[string][]byte{
Ivan Lozano57f434e2020-10-28 09:32:10 -0400109 "foo.rs": nil,
110 "foo.c": nil,
111 "src/bar.rs": nil,
112 "src/any.h": nil,
113 "proto.proto": nil,
114 "buf.proto": nil,
115 "liby.so": nil,
116 "libz.so": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800117 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800118}
119
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200120// generateConfig creates the android.Config based on the bp, fs and env
121// attributes of the testRustCtx.
122func (tctx *testRustCtx) generateConfig() {
123 tctx.bp = tctx.bp + GatherRequiredDepsForTest()
124 cc.GatherRequiredFilesForTest(tctx.fs)
125 config := android.TestArchConfig(buildDir, tctx.env, tctx.bp, tctx.fs)
126 tctx.config = &config
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400127}
128
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200129// enableCoverage configures the test to enable coverage.
130func (tctx *testRustCtx) enableCoverage(t *testing.T) {
131 if tctx.config == nil {
132 t.Fatalf("tctx.config not been generated yet. Please call generateConfig first.")
Ivan Lozanoc0083612019-09-03 13:49:39 -0700133 }
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200134 tctx.config.TestProductVariables.GcovCoverage = proptools.BoolPtr(true)
135 tctx.config.TestProductVariables.Native_coverage = proptools.BoolPtr(true)
136 tctx.config.TestProductVariables.NativeCoveragePaths = []string{"*"}
137}
Ivan Lozanoc0083612019-09-03 13:49:39 -0700138
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200139// parse validates the configuration and parses the Blueprint file. It returns
140// a TestContext which can be used to retrieve the generated modules via
141// ModuleForTests.
142func (tctx testRustCtx) parse(t *testing.T) *android.TestContext {
143 if tctx.config == nil {
144 t.Fatalf("tctx.config not been generated yet. Please call generateConfig first.")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400145 }
Colin Crossae8600b2020-10-29 17:09:13 -0700146 ctx := CreateTestContext(*tctx.config)
147 ctx.Register()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700148 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
149 android.FailIfErrored(t, errs)
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200150 _, errs = ctx.PrepareBuildActions(*tctx.config)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151 android.FailIfErrored(t, errs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700152 return ctx
153}
154
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200155// parseError parses the Blueprint file and ensure that at least one error
156// matching the provided pattern is observed.
157func (tctx testRustCtx) parseError(t *testing.T, pattern string) {
158 if tctx.config == nil {
159 t.Fatalf("tctx.config not been generated yet. Please call generateConfig first.")
Ivan Lozanoc0083612019-09-03 13:49:39 -0700160 }
Colin Crossae8600b2020-10-29 17:09:13 -0700161 ctx := CreateTestContext(*tctx.config)
162 ctx.Register()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700163
164 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
165 if len(errs) > 0 {
166 android.FailIfNoMatchingErrors(t, pattern, errs)
167 return
168 }
169
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200170 _, errs = ctx.PrepareBuildActions(*tctx.config)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700171 if len(errs) > 0 {
172 android.FailIfNoMatchingErrors(t, pattern, errs)
173 return
174 }
175
176 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
177}
178
Ivan Lozanoffee3342019-08-27 12:03:00 -0700179// Test that we can extract the link path from a lib path.
180func TestLinkPathFromFilePath(t *testing.T) {
181 barPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so")
182 libName := linkPathFromFilePath(barPath)
183 expectedResult := "out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/"
184
185 if libName != expectedResult {
186 t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libName)
187 }
188}
189
Ivan Lozanoffee3342019-08-27 12:03:00 -0700190// Test to make sure dependencies are being picked up correctly.
191func TestDepsTracking(t *testing.T) {
192 ctx := testRust(t, `
Matthew Maurer2ae05132020-06-23 14:28:53 -0700193 rust_ffi_host_static {
Ivan Lozano52767be2019-10-18 14:49:46 -0700194 name: "libstatic",
195 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700196 crate_name: "static",
Ivan Lozano52767be2019-10-18 14:49:46 -0700197 }
Matthew Maurer2ae05132020-06-23 14:28:53 -0700198 rust_ffi_host_shared {
Ivan Lozano52767be2019-10-18 14:49:46 -0700199 name: "libshared",
200 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700201 crate_name: "shared",
Ivan Lozano52767be2019-10-18 14:49:46 -0700202 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700203 rust_library_host_dylib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700204 name: "libdylib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700205 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700206 crate_name: "dylib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700207 }
208 rust_library_host_rlib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700209 name: "librlib",
Ivan Lozano43845682020-07-09 21:03:28 -0400210 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700211 crate_name: "rlib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700212 }
213 rust_proc_macro {
214 name: "libpm",
215 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700216 crate_name: "pm",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700217 }
218 rust_binary_host {
Ivan Lozano43845682020-07-09 21:03:28 -0400219 name: "fizz-buzz",
Ivan Lozano52767be2019-10-18 14:49:46 -0700220 dylibs: ["libdylib"],
221 rlibs: ["librlib"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700222 proc_macros: ["libpm"],
Ivan Lozano52767be2019-10-18 14:49:46 -0700223 static_libs: ["libstatic"],
224 shared_libs: ["libshared"],
Ivan Lozano43845682020-07-09 21:03:28 -0400225 srcs: ["foo.rs"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700226 }
227 `)
Ivan Lozano43845682020-07-09 21:03:28 -0400228 module := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700229
230 // 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 -0700231 if !android.InList("libdylib", module.Properties.AndroidMkDylibs) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700232 t.Errorf("Dylib dependency not detected (dependency missing from AndroidMkDylibs)")
233 }
234
Ivan Lozano2b081132020-09-08 12:46:52 -0400235 if !android.InList("librlib.rlib-std", module.Properties.AndroidMkRlibs) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700236 t.Errorf("Rlib dependency not detected (dependency missing from AndroidMkRlibs)")
237 }
238
239 if !android.InList("libpm", module.Properties.AndroidMkProcMacroLibs) {
240 t.Errorf("Proc_macro dependency not detected (dependency missing from AndroidMkProcMacroLibs)")
241 }
242
Ivan Lozano52767be2019-10-18 14:49:46 -0700243 if !android.InList("libshared", module.Properties.AndroidMkSharedLibs) {
244 t.Errorf("Shared library dependency not detected (dependency missing from AndroidMkSharedLibs)")
245 }
246
247 if !android.InList("libstatic", module.Properties.AndroidMkStaticLibs) {
248 t.Errorf("Static library dependency not detected (dependency missing from AndroidMkStaticLibs)")
249 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700250}
Ivan Lozanob9040d62019-09-24 13:23:50 -0700251
Ivan Lozano43845682020-07-09 21:03:28 -0400252func TestSourceProviderDeps(t *testing.T) {
253 ctx := testRust(t, `
254 rust_binary {
255 name: "fizz-buzz-dep",
256 srcs: [
257 "foo.rs",
258 ":my_generator",
259 ":libbindings",
260 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400261 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400262 }
263 rust_proc_macro {
264 name: "libprocmacro",
265 srcs: [
266 "foo.rs",
267 ":my_generator",
268 ":libbindings",
269 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400270 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400271 crate_name: "procmacro",
272 }
273 rust_library {
274 name: "libfoo",
275 srcs: [
276 "foo.rs",
277 ":my_generator",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400278 ":libbindings",
279 ],
280 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400281 crate_name: "foo",
282 }
283 genrule {
284 name: "my_generator",
285 tools: ["any_rust_binary"],
286 cmd: "$(location) -o $(out) $(in)",
287 srcs: ["src/any.h"],
288 out: ["src/any.rs"],
289 }
290 rust_bindgen {
291 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400292 crate_name: "bindings",
293 source_stem: "bindings",
Ivan Lozano43845682020-07-09 21:03:28 -0400294 host_supported: true,
295 wrapper_src: "src/any.h",
296 }
297 `)
298
Ivan Lozano2b081132020-09-08 12:46:52 -0400299 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Rule("rustc")
Ivan Lozano43845682020-07-09 21:03:28 -0400300 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/bindings.rs") {
301 t.Errorf("rust_bindgen generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
302 }
303 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/any.rs") {
304 t.Errorf("genrule generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
305 }
306
307 fizzBuzz := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Rule("rustc")
308 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/bindings.rs") {
309 t.Errorf("rust_bindgen generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
310 }
311 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/any.rs") {
312 t.Errorf("genrule generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
313 }
314
315 libprocmacro := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Rule("rustc")
316 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/bindings.rs") {
317 t.Errorf("rust_bindgen generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
318 }
319 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/any.rs") {
320 t.Errorf("genrule generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
321 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400322
323 // Check that our bindings are picked up as crate dependencies as well
324 libfooMod := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400325 if !android.InList("libbindings.dylib-std", libfooMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400326 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
327 }
328 fizzBuzzMod := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400329 if !android.InList("libbindings.dylib-std", fizzBuzzMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400330 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
331 }
332 libprocmacroMod := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400333 if !android.InList("libbindings.rlib-std", libprocmacroMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400334 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
335 }
336
Ivan Lozano43845682020-07-09 21:03:28 -0400337}
338
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400339func TestSourceProviderTargetMismatch(t *testing.T) {
340 // This might error while building the dependency tree or when calling depsToPaths() depending on the lunched
341 // target, which results in two different errors. So don't check the error, just confirm there is one.
342 testRustError(t, ".*", `
343 rust_proc_macro {
344 name: "libprocmacro",
345 srcs: [
346 "foo.rs",
347 ":libbindings",
348 ],
349 crate_name: "procmacro",
350 }
351 rust_bindgen {
352 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400353 crate_name: "bindings",
354 source_stem: "bindings",
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400355 wrapper_src: "src/any.h",
356 }
357 `)
358}
359
Ivan Lozanob9040d62019-09-24 13:23:50 -0700360// Test to make sure proc_macros use host variants when building device modules.
361func TestProcMacroDeviceDeps(t *testing.T) {
362 ctx := testRust(t, `
363 rust_library_host_rlib {
364 name: "libbar",
365 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700366 crate_name: "bar",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700367 }
368 rust_proc_macro {
369 name: "libpm",
370 rlibs: ["libbar"],
371 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700372 crate_name: "pm",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700373 }
374 rust_binary {
375 name: "fizz-buzz",
376 proc_macros: ["libpm"],
377 srcs: ["foo.rs"],
378 }
379 `)
380 rustc := ctx.ModuleForTests("libpm", "linux_glibc_x86_64").Rule("rustc")
381
382 if !strings.Contains(rustc.Args["libFlags"], "libbar/linux_glibc_x86_64") {
383 t.Errorf("Proc_macro is not using host variant of dependent modules.")
384 }
385}
Matthew Maurer99020b02019-10-31 10:44:40 -0700386
387// Test that no_stdlibs suppresses dependencies on rust standard libraries
388func TestNoStdlibs(t *testing.T) {
389 ctx := testRust(t, `
390 rust_binary {
391 name: "fizz-buzz",
392 srcs: ["foo.rs"],
Ivan Lozano9d1df102020-04-28 10:10:23 -0400393 no_stdlibs: true,
Matthew Maurer99020b02019-10-31 10:44:40 -0700394 }`)
Colin Cross7113d202019-11-20 16:39:12 -0800395 module := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module)
Matthew Maurer99020b02019-10-31 10:44:40 -0700396
397 if android.InList("libstd", module.Properties.AndroidMkDylibs) {
398 t.Errorf("no_stdlibs did not suppress dependency on libstd")
399 }
400}
Ivan Lozano9d1df102020-04-28 10:10:23 -0400401
402// Test that libraries provide both 32-bit and 64-bit variants.
403func TestMultilib(t *testing.T) {
404 ctx := testRust(t, `
405 rust_library_rlib {
406 name: "libfoo",
407 srcs: ["foo.rs"],
408 crate_name: "foo",
409 }`)
410
Ivan Lozano2b081132020-09-08 12:46:52 -0400411 _ = ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std")
412 _ = ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_rlib_dylib-std")
Ivan Lozano9d1df102020-04-28 10:10:23 -0400413}