blob: 04de48b19ebc12af3c8aa4134c93cc4d4cbb9349 [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
Colin Cross98be1bb2019-12-13 20:41:13 -080057func testConfig(bp string) android.Config {
58 bp = bp + GatherRequiredDepsForTest()
59
60 fs := map[string][]byte{
61 "foo.rs": nil,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040062 "foo.c": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -080063 "src/bar.rs": nil,
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -070064 "src/any.h": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -080065 "liby.so": nil,
66 "libz.so": nil,
67 }
68
Colin Crossf28329d2020-02-15 11:00:10 -080069 cc.GatherRequiredFilesForTest(fs)
70
Colin Cross98be1bb2019-12-13 20:41:13 -080071 return android.TestArchConfig(buildDir, nil, bp, fs)
72}
73
Ivan Lozanoffee3342019-08-27 12:03:00 -070074func testRust(t *testing.T, bp string) *android.TestContext {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040075 return testRustContext(t, bp, false)
76}
77
78func testRustCov(t *testing.T, bp string) *android.TestContext {
79 return testRustContext(t, bp, true)
80}
81
82func testRustContext(t *testing.T, bp string, coverage bool) *android.TestContext {
Ivan Lozanoc0083612019-09-03 13:49:39 -070083 // TODO (b/140435149)
84 if runtime.GOOS != "linux" {
85 t.Skip("Only the Linux toolchain is supported for Rust")
86 }
87
Ivan Lozanoffee3342019-08-27 12:03:00 -070088 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080089 config := testConfig(bp)
Ivan Lozanoffee3342019-08-27 12:03:00 -070090
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040091 if coverage {
Colin Cross1a6acd42020-06-16 17:51:46 -070092 config.TestProductVariables.GcovCoverage = proptools.BoolPtr(true)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040093 config.TestProductVariables.Native_coverage = proptools.BoolPtr(true)
Roland Levillain4f5297b2020-06-09 12:44:06 +010094 config.TestProductVariables.NativeCoveragePaths = []string{"*"}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040095 }
96
Colin Cross98be1bb2019-12-13 20:41:13 -080097 ctx := CreateTestContext()
98 ctx.Register(config)
Ivan Lozanoffee3342019-08-27 12:03:00 -070099
100 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
101 android.FailIfErrored(t, errs)
102 _, errs = ctx.PrepareBuildActions(config)
103 android.FailIfErrored(t, errs)
104
105 return ctx
106}
107
108func testRustError(t *testing.T, pattern string, bp string) {
Ivan Lozanoc0083612019-09-03 13:49:39 -0700109 // TODO (b/140435149)
110 if runtime.GOOS != "linux" {
111 t.Skip("Only the Linux toolchain is supported for Rust")
112 }
113
Ivan Lozanoffee3342019-08-27 12:03:00 -0700114 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800115 config := testConfig(bp)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700116
Colin Cross98be1bb2019-12-13 20:41:13 -0800117 ctx := CreateTestContext()
118 ctx.Register(config)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700119
120 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
121 if len(errs) > 0 {
122 android.FailIfNoMatchingErrors(t, pattern, errs)
123 return
124 }
125
126 _, errs = ctx.PrepareBuildActions(config)
127 if len(errs) > 0 {
128 android.FailIfNoMatchingErrors(t, pattern, errs)
129 return
130 }
131
132 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
133}
134
135// Test that we can extract the lib name from a lib path.
136func TestLibNameFromFilePath(t *testing.T) {
Ivan Lozanod648c432020-02-06 12:05:10 -0500137 libBarPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so.so")
Ivan Lozano52767be2019-10-18 14:49:46 -0700138 libLibPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/liblib.dylib.so")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139
Ivan Lozano52767be2019-10-18 14:49:46 -0700140 libBarName := libNameFromFilePath(libBarPath)
141 libLibName := libNameFromFilePath(libLibPath)
142
Ivan Lozanod648c432020-02-06 12:05:10 -0500143 expectedResult := "bar.so"
Ivan Lozano52767be2019-10-18 14:49:46 -0700144 if libBarName != expectedResult {
145 t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libBarName)
146 }
147
148 expectedResult = "lib.dylib"
149 if libLibName != expectedResult {
150 t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libLibPath)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151 }
152}
153
154// Test that we can extract the link path from a lib path.
155func TestLinkPathFromFilePath(t *testing.T) {
156 barPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so")
157 libName := linkPathFromFilePath(barPath)
158 expectedResult := "out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/"
159
160 if libName != expectedResult {
161 t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libName)
162 }
163}
164
Ivan Lozanoffee3342019-08-27 12:03:00 -0700165// Test to make sure dependencies are being picked up correctly.
166func TestDepsTracking(t *testing.T) {
167 ctx := testRust(t, `
Matthew Maurer2ae05132020-06-23 14:28:53 -0700168 rust_ffi_host_static {
Ivan Lozano52767be2019-10-18 14:49:46 -0700169 name: "libstatic",
170 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700171 crate_name: "static",
Ivan Lozano52767be2019-10-18 14:49:46 -0700172 }
Matthew Maurer2ae05132020-06-23 14:28:53 -0700173 rust_ffi_host_shared {
Ivan Lozano52767be2019-10-18 14:49:46 -0700174 name: "libshared",
175 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700176 crate_name: "shared",
Ivan Lozano52767be2019-10-18 14:49:46 -0700177 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700178 rust_library_host_dylib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700179 name: "libdylib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700180 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700181 crate_name: "dylib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700182 }
183 rust_library_host_rlib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700184 name: "librlib",
Ivan Lozano43845682020-07-09 21:03:28 -0400185 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700186 crate_name: "rlib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700187 }
188 rust_proc_macro {
189 name: "libpm",
190 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700191 crate_name: "pm",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700192 }
193 rust_binary_host {
Ivan Lozano43845682020-07-09 21:03:28 -0400194 name: "fizz-buzz",
Ivan Lozano52767be2019-10-18 14:49:46 -0700195 dylibs: ["libdylib"],
196 rlibs: ["librlib"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700197 proc_macros: ["libpm"],
Ivan Lozano52767be2019-10-18 14:49:46 -0700198 static_libs: ["libstatic"],
199 shared_libs: ["libshared"],
Ivan Lozano43845682020-07-09 21:03:28 -0400200 srcs: ["foo.rs"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700201 }
202 `)
Ivan Lozano43845682020-07-09 21:03:28 -0400203 module := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700204
205 // 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 -0700206 if !android.InList("libdylib", module.Properties.AndroidMkDylibs) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700207 t.Errorf("Dylib dependency not detected (dependency missing from AndroidMkDylibs)")
208 }
209
Ivan Lozano52767be2019-10-18 14:49:46 -0700210 if !android.InList("librlib", module.Properties.AndroidMkRlibs) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700211 t.Errorf("Rlib dependency not detected (dependency missing from AndroidMkRlibs)")
212 }
213
214 if !android.InList("libpm", module.Properties.AndroidMkProcMacroLibs) {
215 t.Errorf("Proc_macro dependency not detected (dependency missing from AndroidMkProcMacroLibs)")
216 }
217
Ivan Lozano52767be2019-10-18 14:49:46 -0700218 if !android.InList("libshared", module.Properties.AndroidMkSharedLibs) {
219 t.Errorf("Shared library dependency not detected (dependency missing from AndroidMkSharedLibs)")
220 }
221
222 if !android.InList("libstatic", module.Properties.AndroidMkStaticLibs) {
223 t.Errorf("Static library dependency not detected (dependency missing from AndroidMkStaticLibs)")
224 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700225}
Ivan Lozanob9040d62019-09-24 13:23:50 -0700226
Ivan Lozano43845682020-07-09 21:03:28 -0400227func TestSourceProviderDeps(t *testing.T) {
228 ctx := testRust(t, `
229 rust_binary {
230 name: "fizz-buzz-dep",
231 srcs: [
232 "foo.rs",
233 ":my_generator",
234 ":libbindings",
235 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400236 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400237 }
238 rust_proc_macro {
239 name: "libprocmacro",
240 srcs: [
241 "foo.rs",
242 ":my_generator",
243 ":libbindings",
244 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400245 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400246 crate_name: "procmacro",
247 }
248 rust_library {
249 name: "libfoo",
250 srcs: [
251 "foo.rs",
252 ":my_generator",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400253 ":libbindings",
254 ],
255 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400256 crate_name: "foo",
257 }
258 genrule {
259 name: "my_generator",
260 tools: ["any_rust_binary"],
261 cmd: "$(location) -o $(out) $(in)",
262 srcs: ["src/any.h"],
263 out: ["src/any.rs"],
264 }
265 rust_bindgen {
266 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400267 crate_name: "bindings",
268 source_stem: "bindings",
Ivan Lozano43845682020-07-09 21:03:28 -0400269 host_supported: true,
270 wrapper_src: "src/any.h",
271 }
272 `)
273
274 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib").Rule("rustc")
275 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/bindings.rs") {
276 t.Errorf("rust_bindgen generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
277 }
278 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/any.rs") {
279 t.Errorf("genrule generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
280 }
281
282 fizzBuzz := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Rule("rustc")
283 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/bindings.rs") {
284 t.Errorf("rust_bindgen generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
285 }
286 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/any.rs") {
287 t.Errorf("genrule generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
288 }
289
290 libprocmacro := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Rule("rustc")
291 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/bindings.rs") {
292 t.Errorf("rust_bindgen generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
293 }
294 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/any.rs") {
295 t.Errorf("genrule generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
296 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400297
298 // Check that our bindings are picked up as crate dependencies as well
299 libfooMod := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
300 if !android.InList("libbindings", libfooMod.Properties.AndroidMkRlibs) {
301 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
302 }
303 fizzBuzzMod := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Module().(*Module)
304 if !android.InList("libbindings", fizzBuzzMod.Properties.AndroidMkRlibs) {
305 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
306 }
307 libprocmacroMod := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Module().(*Module)
308 if !android.InList("libbindings", libprocmacroMod.Properties.AndroidMkRlibs) {
309 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
310 }
311
Ivan Lozano43845682020-07-09 21:03:28 -0400312}
313
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400314func TestSourceProviderTargetMismatch(t *testing.T) {
315 // This might error while building the dependency tree or when calling depsToPaths() depending on the lunched
316 // target, which results in two different errors. So don't check the error, just confirm there is one.
317 testRustError(t, ".*", `
318 rust_proc_macro {
319 name: "libprocmacro",
320 srcs: [
321 "foo.rs",
322 ":libbindings",
323 ],
324 crate_name: "procmacro",
325 }
326 rust_bindgen {
327 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400328 crate_name: "bindings",
329 source_stem: "bindings",
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400330 wrapper_src: "src/any.h",
331 }
332 `)
333}
334
Ivan Lozanob9040d62019-09-24 13:23:50 -0700335// Test to make sure proc_macros use host variants when building device modules.
336func TestProcMacroDeviceDeps(t *testing.T) {
337 ctx := testRust(t, `
338 rust_library_host_rlib {
339 name: "libbar",
340 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700341 crate_name: "bar",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700342 }
343 rust_proc_macro {
344 name: "libpm",
345 rlibs: ["libbar"],
346 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700347 crate_name: "pm",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700348 }
349 rust_binary {
350 name: "fizz-buzz",
351 proc_macros: ["libpm"],
352 srcs: ["foo.rs"],
353 }
354 `)
355 rustc := ctx.ModuleForTests("libpm", "linux_glibc_x86_64").Rule("rustc")
356
357 if !strings.Contains(rustc.Args["libFlags"], "libbar/linux_glibc_x86_64") {
358 t.Errorf("Proc_macro is not using host variant of dependent modules.")
359 }
360}
Matthew Maurer99020b02019-10-31 10:44:40 -0700361
362// Test that no_stdlibs suppresses dependencies on rust standard libraries
363func TestNoStdlibs(t *testing.T) {
364 ctx := testRust(t, `
365 rust_binary {
366 name: "fizz-buzz",
367 srcs: ["foo.rs"],
Ivan Lozano9d1df102020-04-28 10:10:23 -0400368 no_stdlibs: true,
Matthew Maurer99020b02019-10-31 10:44:40 -0700369 }`)
Colin Cross7113d202019-11-20 16:39:12 -0800370 module := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module)
Matthew Maurer99020b02019-10-31 10:44:40 -0700371
372 if android.InList("libstd", module.Properties.AndroidMkDylibs) {
373 t.Errorf("no_stdlibs did not suppress dependency on libstd")
374 }
375}
Ivan Lozano9d1df102020-04-28 10:10:23 -0400376
377// Test that libraries provide both 32-bit and 64-bit variants.
378func TestMultilib(t *testing.T) {
379 ctx := testRust(t, `
380 rust_library_rlib {
381 name: "libfoo",
382 srcs: ["foo.rs"],
383 crate_name: "foo",
384 }`)
385
386 _ = ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib")
387 _ = ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_rlib")
388}