blob: 26e943a42e4f6486d1e526e75cc0188d63cb4ed5 [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,
Treehugger Robot588aae72020-08-21 10:01:58 +000065 "buf.proto": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -080066 "liby.so": nil,
67 "libz.so": nil,
68 }
69
Colin Crossf28329d2020-02-15 11:00:10 -080070 cc.GatherRequiredFilesForTest(fs)
71
Colin Cross98be1bb2019-12-13 20:41:13 -080072 return android.TestArchConfig(buildDir, nil, bp, fs)
73}
74
Ivan Lozanoffee3342019-08-27 12:03:00 -070075func testRust(t *testing.T, bp string) *android.TestContext {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040076 return testRustContext(t, bp, false)
77}
78
79func testRustCov(t *testing.T, bp string) *android.TestContext {
80 return testRustContext(t, bp, true)
81}
82
83func testRustContext(t *testing.T, bp string, coverage bool) *android.TestContext {
Ivan Lozanoc0083612019-09-03 13:49:39 -070084 // TODO (b/140435149)
85 if runtime.GOOS != "linux" {
86 t.Skip("Only the Linux toolchain is supported for Rust")
87 }
88
Ivan Lozanoffee3342019-08-27 12:03:00 -070089 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080090 config := testConfig(bp)
Ivan Lozanoffee3342019-08-27 12:03:00 -070091
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040092 if coverage {
Colin Cross1a6acd42020-06-16 17:51:46 -070093 config.TestProductVariables.GcovCoverage = proptools.BoolPtr(true)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040094 config.TestProductVariables.Native_coverage = proptools.BoolPtr(true)
Roland Levillain4f5297b2020-06-09 12:44:06 +010095 config.TestProductVariables.NativeCoveragePaths = []string{"*"}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040096 }
97
Colin Cross98be1bb2019-12-13 20:41:13 -080098 ctx := CreateTestContext()
99 ctx.Register(config)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700100
101 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
102 android.FailIfErrored(t, errs)
103 _, errs = ctx.PrepareBuildActions(config)
104 android.FailIfErrored(t, errs)
105
106 return ctx
107}
108
109func testRustError(t *testing.T, pattern string, bp string) {
Ivan Lozanoc0083612019-09-03 13:49:39 -0700110 // TODO (b/140435149)
111 if runtime.GOOS != "linux" {
112 t.Skip("Only the Linux toolchain is supported for Rust")
113 }
114
Ivan Lozanoffee3342019-08-27 12:03:00 -0700115 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800116 config := testConfig(bp)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700117
Colin Cross98be1bb2019-12-13 20:41:13 -0800118 ctx := CreateTestContext()
119 ctx.Register(config)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700120
121 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
122 if len(errs) > 0 {
123 android.FailIfNoMatchingErrors(t, pattern, errs)
124 return
125 }
126
127 _, errs = ctx.PrepareBuildActions(config)
128 if len(errs) > 0 {
129 android.FailIfNoMatchingErrors(t, pattern, errs)
130 return
131 }
132
133 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
134}
135
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136// Test that we can extract the link path from a lib path.
137func TestLinkPathFromFilePath(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700138 t.Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139 barPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so")
140 libName := linkPathFromFilePath(barPath)
141 expectedResult := "out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/"
142
143 if libName != expectedResult {
144 t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libName)
145 }
146}
147
Ivan Lozanoffee3342019-08-27 12:03:00 -0700148// Test to make sure dependencies are being picked up correctly.
149func TestDepsTracking(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700150 t.Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151 ctx := testRust(t, `
Matthew Maurer2ae05132020-06-23 14:28:53 -0700152 rust_ffi_host_static {
Ivan Lozano52767be2019-10-18 14:49:46 -0700153 name: "libstatic",
154 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700155 crate_name: "static",
Ivan Lozano52767be2019-10-18 14:49:46 -0700156 }
Matthew Maurer2ae05132020-06-23 14:28:53 -0700157 rust_ffi_host_shared {
Ivan Lozano52767be2019-10-18 14:49:46 -0700158 name: "libshared",
159 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700160 crate_name: "shared",
Ivan Lozano52767be2019-10-18 14:49:46 -0700161 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700162 rust_library_host_dylib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700163 name: "libdylib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700164 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700165 crate_name: "dylib",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700166 }
167 rust_library_host_rlib {
Ivan Lozano52767be2019-10-18 14:49:46 -0700168 name: "librlib",
Ivan Lozano43845682020-07-09 21:03:28 -0400169 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700170 crate_name: "rlib",
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 dylibs: ["libdylib"],
180 rlibs: ["librlib"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700181 proc_macros: ["libpm"],
Ivan Lozano52767be2019-10-18 14:49:46 -0700182 static_libs: ["libstatic"],
183 shared_libs: ["libshared"],
Ivan Lozano43845682020-07-09 21:03:28 -0400184 srcs: ["foo.rs"],
Ivan Lozanoffee3342019-08-27 12:03:00 -0700185 }
186 `)
Ivan Lozano43845682020-07-09 21:03:28 -0400187 module := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
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 Lozano52767be2019-10-18 14:49:46 -0700190 if !android.InList("libdylib", module.Properties.AndroidMkDylibs) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700191 t.Errorf("Dylib dependency not detected (dependency missing from AndroidMkDylibs)")
192 }
193
Ivan Lozano2b081132020-09-08 12:46:52 -0400194 if !android.InList("librlib.rlib-std", module.Properties.AndroidMkRlibs) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700195 t.Errorf("Rlib dependency not detected (dependency missing from AndroidMkRlibs)")
196 }
197
198 if !android.InList("libpm", module.Properties.AndroidMkProcMacroLibs) {
199 t.Errorf("Proc_macro dependency not detected (dependency missing from AndroidMkProcMacroLibs)")
200 }
201
Ivan Lozano52767be2019-10-18 14:49:46 -0700202 if !android.InList("libshared", module.Properties.AndroidMkSharedLibs) {
203 t.Errorf("Shared library dependency not detected (dependency missing from AndroidMkSharedLibs)")
204 }
205
206 if !android.InList("libstatic", module.Properties.AndroidMkStaticLibs) {
207 t.Errorf("Static library dependency not detected (dependency missing from AndroidMkStaticLibs)")
208 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700209}
Ivan Lozanob9040d62019-09-24 13:23:50 -0700210
Ivan Lozano43845682020-07-09 21:03:28 -0400211func TestSourceProviderDeps(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700212 t.Parallel()
Ivan Lozano43845682020-07-09 21:03:28 -0400213 ctx := testRust(t, `
214 rust_binary {
215 name: "fizz-buzz-dep",
216 srcs: [
217 "foo.rs",
218 ":my_generator",
219 ":libbindings",
220 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400221 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400222 }
223 rust_proc_macro {
224 name: "libprocmacro",
225 srcs: [
226 "foo.rs",
227 ":my_generator",
228 ":libbindings",
229 ],
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400230 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400231 crate_name: "procmacro",
232 }
233 rust_library {
234 name: "libfoo",
235 srcs: [
236 "foo.rs",
237 ":my_generator",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400238 ":libbindings",
239 ],
240 rlibs: ["libbindings"],
Ivan Lozano43845682020-07-09 21:03:28 -0400241 crate_name: "foo",
242 }
243 genrule {
244 name: "my_generator",
245 tools: ["any_rust_binary"],
246 cmd: "$(location) -o $(out) $(in)",
247 srcs: ["src/any.h"],
248 out: ["src/any.rs"],
249 }
250 rust_bindgen {
251 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400252 crate_name: "bindings",
253 source_stem: "bindings",
Ivan Lozano43845682020-07-09 21:03:28 -0400254 host_supported: true,
255 wrapper_src: "src/any.h",
256 }
257 `)
258
Ivan Lozano2b081132020-09-08 12:46:52 -0400259 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Rule("rustc")
Ivan Lozano43845682020-07-09 21:03:28 -0400260 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/bindings.rs") {
261 t.Errorf("rust_bindgen generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
262 }
263 if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/any.rs") {
264 t.Errorf("genrule generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
265 }
266
267 fizzBuzz := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Rule("rustc")
268 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/bindings.rs") {
269 t.Errorf("rust_bindgen generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
270 }
271 if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/any.rs") {
272 t.Errorf("genrule generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
273 }
274
275 libprocmacro := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Rule("rustc")
276 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/bindings.rs") {
277 t.Errorf("rust_bindgen generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
278 }
279 if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/any.rs") {
280 t.Errorf("genrule generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
281 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400282
283 // Check that our bindings are picked up as crate dependencies as well
284 libfooMod := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400285 if !android.InList("libbindings.dylib-std", libfooMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400286 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
287 }
288 fizzBuzzMod := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400289 if !android.InList("libbindings.dylib-std", fizzBuzzMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400290 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
291 }
292 libprocmacroMod := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400293 if !android.InList("libbindings.rlib-std", libprocmacroMod.Properties.AndroidMkRlibs) {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400294 t.Errorf("bindgen dependency not detected as a rlib dependency (dependency missing from AndroidMkRlibs)")
295 }
296
Ivan Lozano43845682020-07-09 21:03:28 -0400297}
298
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400299func TestSourceProviderTargetMismatch(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700300 t.Parallel()
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400301 // This might error while building the dependency tree or when calling depsToPaths() depending on the lunched
302 // target, which results in two different errors. So don't check the error, just confirm there is one.
303 testRustError(t, ".*", `
304 rust_proc_macro {
305 name: "libprocmacro",
306 srcs: [
307 "foo.rs",
308 ":libbindings",
309 ],
310 crate_name: "procmacro",
311 }
312 rust_bindgen {
313 name: "libbindings",
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400314 crate_name: "bindings",
315 source_stem: "bindings",
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400316 wrapper_src: "src/any.h",
317 }
318 `)
319}
320
Ivan Lozanob9040d62019-09-24 13:23:50 -0700321// Test to make sure proc_macros use host variants when building device modules.
322func TestProcMacroDeviceDeps(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700323 t.Parallel()
Ivan Lozanob9040d62019-09-24 13:23:50 -0700324 ctx := testRust(t, `
325 rust_library_host_rlib {
326 name: "libbar",
327 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700328 crate_name: "bar",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700329 }
330 rust_proc_macro {
331 name: "libpm",
332 rlibs: ["libbar"],
333 srcs: ["foo.rs"],
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700334 crate_name: "pm",
Ivan Lozanob9040d62019-09-24 13:23:50 -0700335 }
336 rust_binary {
337 name: "fizz-buzz",
338 proc_macros: ["libpm"],
339 srcs: ["foo.rs"],
340 }
341 `)
342 rustc := ctx.ModuleForTests("libpm", "linux_glibc_x86_64").Rule("rustc")
343
344 if !strings.Contains(rustc.Args["libFlags"], "libbar/linux_glibc_x86_64") {
345 t.Errorf("Proc_macro is not using host variant of dependent modules.")
346 }
347}
Matthew Maurer99020b02019-10-31 10:44:40 -0700348
349// Test that no_stdlibs suppresses dependencies on rust standard libraries
350func TestNoStdlibs(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700351 t.Parallel()
Matthew Maurer99020b02019-10-31 10:44:40 -0700352 ctx := testRust(t, `
353 rust_binary {
354 name: "fizz-buzz",
355 srcs: ["foo.rs"],
Ivan Lozano9d1df102020-04-28 10:10:23 -0400356 no_stdlibs: true,
Matthew Maurer99020b02019-10-31 10:44:40 -0700357 }`)
Colin Cross7113d202019-11-20 16:39:12 -0800358 module := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module)
Matthew Maurer99020b02019-10-31 10:44:40 -0700359
360 if android.InList("libstd", module.Properties.AndroidMkDylibs) {
361 t.Errorf("no_stdlibs did not suppress dependency on libstd")
362 }
363}
Ivan Lozano9d1df102020-04-28 10:10:23 -0400364
365// Test that libraries provide both 32-bit and 64-bit variants.
366func TestMultilib(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700367 t.Parallel()
Ivan Lozano9d1df102020-04-28 10:10:23 -0400368 ctx := testRust(t, `
369 rust_library_rlib {
370 name: "libfoo",
371 srcs: ["foo.rs"],
372 crate_name: "foo",
373 }`)
374
Ivan Lozano2b081132020-09-08 12:46:52 -0400375 _ = ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std")
376 _ = ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_rlib_dylib-std")
Ivan Lozano9d1df102020-04-28 10:10:23 -0400377}