blob: 692a066051938c3fc1d5a53d231c7f9e6a8f9875 [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) {
Colin Cross323dc602020-09-18 14:25:31 -070026 t.Parallel()
Ivan Lozano042504f2020-08-18 14:31:23 -040027 ctx := testRust(t, `
28 rust_binary {
29 name: "fizz-buzz",
30 srcs: ["foo.rs"],
31 rustlibs: ["libfoo"],
32 host_supported: true,
33 }
Ivan Lozano11200872020-09-28 11:56:30 -040034 rust_binary {
35 name: "rlib_linked",
36 srcs: ["foo.rs"],
37 rustlibs: ["libfoo"],
38 host_supported: true,
39 prefer_rlib: true,
40 }
Ivan Lozano042504f2020-08-18 14:31:23 -040041 rust_library {
42 name: "libfoo",
43 srcs: ["foo.rs"],
44 crate_name: "foo",
45 host_supported: true,
46 }`)
47
48 fizzBuzzHost := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
49 fizzBuzzDevice := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module)
50
Ivan Lozano2b081132020-09-08 12:46:52 -040051 if !android.InList("libfoo.rlib-std", fizzBuzzHost.Properties.AndroidMkRlibs) {
Ivan Lozano042504f2020-08-18 14:31:23 -040052 t.Errorf("rustlibs dependency libfoo should be an rlib dep for host modules")
53 }
54
55 if !android.InList("libfoo", fizzBuzzDevice.Properties.AndroidMkDylibs) {
56 t.Errorf("rustlibs dependency libfoo should be an dylib dep for device modules")
57 }
58}
59
Ivan Lozano11200872020-09-28 11:56:30 -040060// Test that prefer_rlib links in libstd statically as well as rustlibs.
61func TestBinaryPreferRlib(t *testing.T) {
62 ctx := testRust(t, `
63 rust_binary {
64 name: "rlib_linked",
65 srcs: ["foo.rs"],
66 rustlibs: ["libfoo"],
67 host_supported: true,
68 prefer_rlib: true,
69 }
70 rust_library {
71 name: "libfoo",
72 srcs: ["foo.rs"],
73 crate_name: "foo",
74 host_supported: true,
75 }`)
76
77 mod := ctx.ModuleForTests("rlib_linked", "android_arm64_armv8-a").Module().(*Module)
78
79 if !android.InList("libfoo.rlib-std", mod.Properties.AndroidMkRlibs) {
80 t.Errorf("rustlibs dependency libfoo should be an rlib dep when prefer_rlib is defined")
81 }
82
83 if !android.InList("libstd", mod.Properties.AndroidMkRlibs) {
84 t.Errorf("libstd dependency should be an rlib dep when prefer_rlib is defined")
85 }
86}
87
Ivan Lozano042504f2020-08-18 14:31:23 -040088// Test that the path returned by HostToolPath is correct
89func TestHostToolPath(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070090 t.Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070091 ctx := testRust(t, `
92 rust_binary_host {
Ivan Lozano042504f2020-08-18 14:31:23 -040093 name: "fizz-buzz",
Ivan Lozanoffee3342019-08-27 12:03:00 -070094 srcs: ["foo.rs"],
Ivan Lozano042504f2020-08-18 14:31:23 -040095 }`)
Ivan Lozanoffee3342019-08-27 12:03:00 -070096
Ivan Lozano042504f2020-08-18 14:31:23 -040097 path := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module).HostToolPath()
98 if g, w := path.String(), "/host/linux-x86/bin/fizz-buzz"; !strings.Contains(g, w) {
99 t.Errorf("wrong host tool path, expected %q got %q", w, g)
100 }
101}
102
103// Test that the flags being passed to rust_binary modules are as expected
104func TestBinaryFlags(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700105 t.Parallel()
Ivan Lozano042504f2020-08-18 14:31:23 -0400106 ctx := testRust(t, `
Ivan Lozanoffee3342019-08-27 12:03:00 -0700107 rust_binary_host {
108 name: "fizz-buzz",
109 srcs: ["foo.rs"],
110 }`)
111
112 fizzBuzz := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Output("fizz-buzz")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700113
Ivan Lozano042504f2020-08-18 14:31:23 -0400114 flags := fizzBuzz.Args["rustcFlags"]
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700115 if strings.Contains(flags, "--test") {
116 t.Errorf("extra --test flag, rustcFlags: %#v", flags)
117 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700118}
Ivan Lozano2093af22020-08-25 12:48:19 -0400119
Ivan Lozanobf63d002020-10-02 10:03:23 -0400120func TestStaticBinaryFlags(t *testing.T) {
121 ctx := testRust(t, `
122 rust_binary {
123 name: "fizz",
124 srcs: ["foo.rs"],
125 static_executable: true,
126 }`)
127
128 fizzOut := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Output("fizz")
129 fizzMod := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Module().(*Module)
130
131 flags := fizzOut.Args["rustcFlags"]
132 linkFlags := fizzOut.Args["linkFlags"]
133 if !strings.Contains(flags, "-C relocation-model=static") {
134 t.Errorf("static binary missing '-C relocation-model=static' in rustcFlags, found: %#v", flags)
135 }
136 if !strings.Contains(linkFlags, "-static") {
137 t.Errorf("static binary missing '-static' in linkFlags, found: %#v", flags)
138 }
139
140 if !android.InList("libc", fizzMod.Properties.AndroidMkStaticLibs) {
141 t.Errorf("static binary not linking against libc as a static library")
142 }
143 if len(fizzMod.Properties.AndroidMkSharedLibs) > 0 {
144 t.Errorf("static binary incorrectly linking against shared libraries")
145 }
146}
147
Ivan Lozano2093af22020-08-25 12:48:19 -0400148func TestLinkObjects(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700149 t.Parallel()
Ivan Lozano2093af22020-08-25 12:48:19 -0400150 ctx := testRust(t, `
151 rust_binary {
152 name: "fizz-buzz",
153 srcs: ["foo.rs"],
154 shared_libs: ["libfoo"],
155 }
156 cc_library {
157 name: "libfoo",
158 }`)
159
160 fizzBuzz := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Output("fizz-buzz")
161 linkFlags := fizzBuzz.Args["linkFlags"]
162 if !strings.Contains(linkFlags, "/libfoo.so") {
163 t.Errorf("missing shared dependency 'libfoo.so' in linkFlags: %#v", linkFlags)
164 }
165}
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200166
167// Test that stripped versions are correctly generated and used.
168func TestStrippedBinary(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700169 t.Parallel()
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200170 ctx := testRust(t, `
171 rust_binary {
172 name: "foo",
173 srcs: ["foo.rs"],
174 }
175 rust_binary {
176 name: "bar",
177 srcs: ["foo.rs"],
178 strip: {
179 none: true
180 }
181 }
182 `)
183
184 foo := ctx.ModuleForTests("foo", "android_arm64_armv8-a")
185 foo.Output("stripped/foo")
186 // Check that the `cp` rules is using the stripped version as input.
187 cp := foo.Rule("android.Cp")
188 if !strings.HasSuffix(cp.Input.String(), "stripped/foo") {
189 t.Errorf("installed binary not based on stripped version: %v", cp.Input)
190 }
191
192 fizzBar := ctx.ModuleForTests("bar", "android_arm64_armv8-a").MaybeOutput("stripped/bar")
193 if fizzBar.Rule != nil {
194 t.Errorf("stripped version of bar has been generated")
195 }
196}