blob: 187f0b6995ff139bffe004c82bbe6a4b747d3679 [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{
Colin Cross98be1bb2019-12-13 20:41:13 -0800109 "foo.rs": nil,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400110 "foo.c": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800111 "src/bar.rs": nil,
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700112 "src/any.h": nil,
Treehugger Robot588aae72020-08-21 10:01:58 +0000113 "buf.proto": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800114 "liby.so": nil,
115 "libz.so": nil,
116 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800117}
118
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200119// generateConfig creates the android.Config based on the bp, fs and env
120// attributes of the testRustCtx.
121func (tctx *testRustCtx) generateConfig() {
122 tctx.bp = tctx.bp + GatherRequiredDepsForTest()
123 cc.GatherRequiredFilesForTest(tctx.fs)
124 config := android.TestArchConfig(buildDir, tctx.env, tctx.bp, tctx.fs)
125 tctx.config = &config
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400126}
127
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200128// enableCoverage configures the test to enable coverage.
129func (tctx *testRustCtx) enableCoverage(t *testing.T) {
130 if tctx.config == nil {
131 t.Fatalf("tctx.config not been generated yet. Please call generateConfig first.")
Ivan Lozanoc0083612019-09-03 13:49:39 -0700132 }
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200133 tctx.config.TestProductVariables.GcovCoverage = proptools.BoolPtr(true)
134 tctx.config.TestProductVariables.Native_coverage = proptools.BoolPtr(true)
135 tctx.config.TestProductVariables.NativeCoveragePaths = []string{"*"}
136}
Ivan Lozanoc0083612019-09-03 13:49:39 -0700137
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200138// parse validates the configuration and parses the Blueprint file. It returns
139// a TestContext which can be used to retrieve the generated modules via
140// ModuleForTests.
141func (tctx testRustCtx) parse(t *testing.T) *android.TestContext {
142 if tctx.config == nil {
143 t.Fatalf("tctx.config not been generated yet. Please call generateConfig first.")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400144 }
Colin Crossae8600b2020-10-29 17:09:13 -0700145 ctx := CreateTestContext(*tctx.config)
146 ctx.Register()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700147 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
148 android.FailIfErrored(t, errs)
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200149 _, errs = ctx.PrepareBuildActions(*tctx.config)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700150 android.FailIfErrored(t, errs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151 return ctx
152}
153
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200154// parseError parses the Blueprint file and ensure that at least one error
155// matching the provided pattern is observed.
156func (tctx testRustCtx) parseError(t *testing.T, pattern string) {
157 if tctx.config == nil {
158 t.Fatalf("tctx.config not been generated yet. Please call generateConfig first.")
Ivan Lozanoc0083612019-09-03 13:49:39 -0700159 }
Colin Crossae8600b2020-10-29 17:09:13 -0700160 ctx := CreateTestContext(*tctx.config)
161 ctx.Register()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700162
163 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
164 if len(errs) > 0 {
165 android.FailIfNoMatchingErrors(t, pattern, errs)
166 return
167 }
168
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200169 _, errs = ctx.PrepareBuildActions(*tctx.config)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700170 if len(errs) > 0 {
171 android.FailIfNoMatchingErrors(t, pattern, errs)
172 return
173 }
174
175 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
176}
177
Ivan Lozanoffee3342019-08-27 12:03:00 -0700178// Test that we can extract the link path from a lib path.
179func TestLinkPathFromFilePath(t *testing.T) {
180 barPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so")
181 libName := linkPathFromFilePath(barPath)
182 expectedResult := "out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/"
183
184 if libName != expectedResult {
185 t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libName)
186 }
187}
188
Ivan Lozanoffee3342019-08-27 12:03:00 -0700189// Test to make sure dependencies are being picked up correctly.
190func TestDepsTracking(t *testing.T) {
191 ctx := testRust(t, `
Matthew Maurer2ae05132020-06-23 14:28:53 -0700192 rust_ffi_host_static {
Ivan Lozano52767be2019-10-18 14:49:46 -0700193 name: "libstatic",
194 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700195 crate_name: "static",
Ivan Lozano52767be2019-10-18 14:49:46 -0700196 }
Matthew Maurer2ae05132020-06-23 14:28:53 -0700197 rust_ffi_host_shared {
Ivan Lozano52767be2019-10-18 14:49:46 -0700198 name: "libshared",
199 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700200 crate_name: "shared",
Ivan Lozano52767be2019-10-18 14:49:46 -0700201 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700202 rust_library_host_dylib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700203 name: "libdylib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700204 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700205 crate_name: "dylib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700206 }
207 rust_library_host_rlib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700208 name: "librlib",
Ivan Lozano43845682020-07-09 21:03:28 -0400209 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700210 crate_name: "rlib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700211 }
212 rust_proc_macro {
213 name: "libpm",
214 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700215 crate_name: "pm",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700216 }
217 rust_binary_host {
Ivan Lozano43845682020-07-09 21:03:28 -0400218 name: "fizz-buzz",
Ivan Lozano52767be2019-10-18 14:49:46 -0700219 dylibs: ["libdylib"],
220 rlibs: ["librlib"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700221 proc_macros: ["libpm"],
Ivan Lozano52767be2019-10-18 14:49:46 -0700222 static_libs: ["libstatic"],
223 shared_libs: ["libshared"],
Ivan Lozano43845682020-07-09 21:03:28 -0400224 srcs: ["foo.rs"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700225 }
226 `)
Ivan Lozano43845682020-07-09 21:03:28 -0400227 module := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700228
229 // 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 -0700230 if !android.InList("libdylib", module.Properties.AndroidMkDylibs) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700231 t.Errorf("Dylib dependency not detected (dependency missing from AndroidMkDylibs)")
232 }
233
Ivan Lozano2b081132020-09-08 12:46:52 -0400234 if !android.InList("librlib.rlib-std", module.Properties.AndroidMkRlibs) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700235 t.Errorf("Rlib dependency not detected (dependency missing from AndroidMkRlibs)")
236 }
237
238 if !android.InList("libpm", module.Properties.AndroidMkProcMacroLibs) {
239 t.Errorf("Proc_macro dependency not detected (dependency missing from AndroidMkProcMacroLibs)")
240 }
241
Ivan Lozano52767be2019-10-18 14:49:46 -0700242 if !android.InList("libshared", module.Properties.AndroidMkSharedLibs) {
243 t.Errorf("Shared library dependency not detected (dependency missing from AndroidMkSharedLibs)")
244 }
245
246 if !android.InList("libstatic", module.Properties.AndroidMkStaticLibs) {
247 t.Errorf("Static library dependency not detected (dependency missing from AndroidMkStaticLibs)")
248 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700249}
Ivan Lozanob9040d62019-09-24 13:23:50 -0700250
Ivan Lozano43845682020-07-09 21:03:28 -0400251func TestSourceProviderDeps(t *testing.T) {
252 ctx := testRust(t, `
253 rust_binary {
254 name: "fizz-buzz-dep",
255 srcs: [
256 "foo.rs",
257 ":my_generator",
258 ":libbindings",
259 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400260 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400261 }
262 rust_proc_macro {
263 name: "libprocmacro",
264 srcs: [
265 "foo.rs",
266 ":my_generator",
267 ":libbindings",
268 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400269 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400270 crate_name: "procmacro",
271 }
272 rust_library {
273 name: "libfoo",
274 srcs: [
275 "foo.rs",
276 ":my_generator",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400277 ":libbindings",
278 ],
279 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400280 crate_name: "foo",
281 }
282 genrule {
283 name: "my_generator",
284 tools: ["any_rust_binary"],
285 cmd: "$(location) -o $(out) $(in)",
286 srcs: ["src/any.h"],
287 out: ["src/any.rs"],
288 }
Colin Crosse9fe2942020-11-10 18:12:15 -0800289 rust_binary_host {
290 name: "any_rust_binary",
291 srcs: [
292 "foo.rs",
293 ],
294 }
Ivan Lozano43845682020-07-09 21:03:28 -0400295 rust_bindgen {
296 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400297 crate_name: "bindings",
298 source_stem: "bindings",
Ivan Lozano43845682020-07-09 21:03:28 -0400299 host_supported: true,
300 wrapper_src: "src/any.h",
301 }
302 `)
303
Ivan Lozano2b081132020-09-08 12:46:52 -0400304 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Rule("rustc")
Ivan Lozano43845682020-07-09 21:03:28 -0400305 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/bindings.rs") {
306 t.Errorf("rust_bindgen generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
307 }
308 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/any.rs") {
309 t.Errorf("genrule generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
310 }
311
312 fizzBuzz := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Rule("rustc")
313 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/bindings.rs") {
314 t.Errorf("rust_bindgen generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
315 }
316 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/any.rs") {
317 t.Errorf("genrule generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
318 }
319
320 libprocmacro := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Rule("rustc")
321 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/bindings.rs") {
322 t.Errorf("rust_bindgen generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
323 }
324 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/any.rs") {
325 t.Errorf("genrule generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
326 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400327
328 // Check that our bindings are picked up as crate dependencies as well
329 libfooMod := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400330 if !android.InList("libbindings.dylib-std", libfooMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400331 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
332 }
333 fizzBuzzMod := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400334 if !android.InList("libbindings.dylib-std", fizzBuzzMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400335 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
336 }
337 libprocmacroMod := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400338 if !android.InList("libbindings.rlib-std", libprocmacroMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400339 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
340 }
341
Ivan Lozano43845682020-07-09 21:03:28 -0400342}
343
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400344func TestSourceProviderTargetMismatch(t *testing.T) {
345 // This might error while building the dependency tree or when calling depsToPaths() depending on the lunched
346 // target, which results in two different errors. So don't check the error, just confirm there is one.
347 testRustError(t, ".*", `
348 rust_proc_macro {
349 name: "libprocmacro",
350 srcs: [
351 "foo.rs",
352 ":libbindings",
353 ],
354 crate_name: "procmacro",
355 }
356 rust_bindgen {
357 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400358 crate_name: "bindings",
359 source_stem: "bindings",
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400360 wrapper_src: "src/any.h",
361 }
362 `)
363}
364
Ivan Lozanob9040d62019-09-24 13:23:50 -0700365// Test to make sure proc_macros use host variants when building device modules.
366func TestProcMacroDeviceDeps(t *testing.T) {
367 ctx := testRust(t, `
368 rust_library_host_rlib {
369 name: "libbar",
370 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700371 crate_name: "bar",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700372 }
373 rust_proc_macro {
374 name: "libpm",
375 rlibs: ["libbar"],
376 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700377 crate_name: "pm",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700378 }
379 rust_binary {
380 name: "fizz-buzz",
381 proc_macros: ["libpm"],
382 srcs: ["foo.rs"],
383 }
384 `)
385 rustc := ctx.ModuleForTests("libpm", "linux_glibc_x86_64").Rule("rustc")
386
387 if !strings.Contains(rustc.Args["libFlags"], "libbar/linux_glibc_x86_64") {
388 t.Errorf("Proc_macro is not using host variant of dependent modules.")
389 }
390}
Matthew Maurer99020b02019-10-31 10:44:40 -0700391
392// Test that no_stdlibs suppresses dependencies on rust standard libraries
393func TestNoStdlibs(t *testing.T) {
394 ctx := testRust(t, `
395 rust_binary {
396 name: "fizz-buzz",
397 srcs: ["foo.rs"],
Ivan Lozano9d1df102020-04-28 10:10:23 -0400398 no_stdlibs: true,
Matthew Maurer99020b02019-10-31 10:44:40 -0700399 }`)
Colin Cross7113d202019-11-20 16:39:12 -0800400 module := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module)
Matthew Maurer99020b02019-10-31 10:44:40 -0700401
402 if android.InList("libstd", module.Properties.AndroidMkDylibs) {
403 t.Errorf("no_stdlibs did not suppress dependency on libstd")
404 }
405}
Ivan Lozano9d1df102020-04-28 10:10:23 -0400406
407// Test that libraries provide both 32-bit and 64-bit variants.
408func TestMultilib(t *testing.T) {
409 ctx := testRust(t, `
410 rust_library_rlib {
411 name: "libfoo",
412 srcs: ["foo.rs"],
413 crate_name: "foo",
414 }`)
415
Ivan Lozano2b081132020-09-08 12:46:52 -0400416 _ = ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std")
417 _ = ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_rlib_dylib-std")
Ivan Lozano9d1df102020-04-28 10:10:23 -0400418}