blob: cfef57a7775898f081059c24deed21794b65d071 [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 "strings"
19 "testing"
Ivan Lozano042504f2020-08-18 14:31:23 -040020
21 "android/soong/android"
Ivan Lozanoffee3342019-08-27 12:03:00 -070022)
23
Ivan Lozano042504f2020-08-18 14:31:23 -040024// Test that rustlibs default linkage is correct for binaries.
25func TestBinaryLinkage(t *testing.T) {
26 ctx := testRust(t, `
27 rust_binary {
28 name: "fizz-buzz",
29 srcs: ["foo.rs"],
30 rustlibs: ["libfoo"],
31 host_supported: true,
32 }
33 rust_library {
34 name: "libfoo",
35 srcs: ["foo.rs"],
36 crate_name: "foo",
37 host_supported: true,
38 }`)
39
40 fizzBuzzHost := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
41 fizzBuzzDevice := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module)
42
43 if !android.InList("libfoo", fizzBuzzHost.Properties.AndroidMkRlibs) {
44 t.Errorf("rustlibs dependency libfoo should be an rlib dep for host modules")
45 }
46
47 if !android.InList("libfoo", fizzBuzzDevice.Properties.AndroidMkDylibs) {
48 t.Errorf("rustlibs dependency libfoo should be an dylib dep for device modules")
49 }
50}
51
52// Test that the path returned by HostToolPath is correct
53func TestHostToolPath(t *testing.T) {
Ivan Lozanoffee3342019-08-27 12:03:00 -070054 ctx := testRust(t, `
55 rust_binary_host {
Ivan Lozano042504f2020-08-18 14:31:23 -040056 name: "fizz-buzz",
Ivan Lozanoffee3342019-08-27 12:03:00 -070057 srcs: ["foo.rs"],
Ivan Lozano042504f2020-08-18 14:31:23 -040058 }`)
Ivan Lozanoffee3342019-08-27 12:03:00 -070059
Ivan Lozano042504f2020-08-18 14:31:23 -040060 path := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module).HostToolPath()
61 if g, w := path.String(), "/host/linux-x86/bin/fizz-buzz"; !strings.Contains(g, w) {
62 t.Errorf("wrong host tool path, expected %q got %q", w, g)
63 }
64}
65
66// Test that the flags being passed to rust_binary modules are as expected
67func TestBinaryFlags(t *testing.T) {
68 ctx := testRust(t, `
Ivan Lozanoffee3342019-08-27 12:03:00 -070069 rust_binary_host {
70 name: "fizz-buzz",
71 srcs: ["foo.rs"],
72 }`)
73
74 fizzBuzz := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Output("fizz-buzz")
Ivan Lozanoffee3342019-08-27 12:03:00 -070075
Ivan Lozano042504f2020-08-18 14:31:23 -040076 flags := fizzBuzz.Args["rustcFlags"]
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070077 if strings.Contains(flags, "--test") {
78 t.Errorf("extra --test flag, rustcFlags: %#v", flags)
79 }
Ivan Lozanoffee3342019-08-27 12:03:00 -070080}
Ivan Lozano2093af22020-08-25 12:48:19 -040081
82func TestLinkObjects(t *testing.T) {
83 ctx := testRust(t, `
84 rust_binary {
85 name: "fizz-buzz",
86 srcs: ["foo.rs"],
87 shared_libs: ["libfoo"],
88 }
89 cc_library {
90 name: "libfoo",
91 }`)
92
93 fizzBuzz := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Output("fizz-buzz")
94 linkFlags := fizzBuzz.Args["linkFlags"]
95 if !strings.Contains(linkFlags, "/libfoo.so") {
96 t.Errorf("missing shared dependency 'libfoo.so' in linkFlags: %#v", linkFlags)
97 }
98}
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +020099
100// Test that stripped versions are correctly generated and used.
101func TestStrippedBinary(t *testing.T) {
102 ctx := testRust(t, `
103 rust_binary {
104 name: "foo",
105 srcs: ["foo.rs"],
106 }
107 rust_binary {
108 name: "bar",
109 srcs: ["foo.rs"],
110 strip: {
111 none: true
112 }
113 }
114 `)
115
116 foo := ctx.ModuleForTests("foo", "android_arm64_armv8-a")
117 foo.Output("stripped/foo")
118 // Check that the `cp` rules is using the stripped version as input.
119 cp := foo.Rule("android.Cp")
120 if !strings.HasSuffix(cp.Input.String(), "stripped/foo") {
121 t.Errorf("installed binary not based on stripped version: %v", cp.Input)
122 }
123
124 fizzBar := ctx.ModuleForTests("bar", "android_arm64_armv8-a").MaybeOutput("stripped/bar")
125 if fizzBar.Rule != nil {
126 t.Errorf("stripped version of bar has been generated")
127 }
128}