blob: ab1d2bc362067b982702bf967b7ec5707d26bdab [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
Vinh Tran05b34102023-08-14 11:26:18 -040024// Test that rustlibs default linkage is always rlib for host binaries.
25func TestBinaryHostLinkage(t *testing.T) {
26 ctx := testRust(t, `
27 rust_binary_host {
28 name: "fizz-buzz",
29 srcs: ["foo.rs"],
30 rustlibs: ["libfoo"],
31 }
32 rust_library {
33 name: "libfoo",
34 srcs: ["foo.rs"],
35 crate_name: "foo",
36 host_supported: true,
37 }
38 `)
39 fizzBuzz := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
40 if !android.InList("libfoo.rlib-std", fizzBuzz.Properties.AndroidMkRlibs) {
41 t.Errorf("rustlibs dependency libfoo should be an rlib dep for host binaries")
42 }
43}
44
Ivan Lozano042504f2020-08-18 14:31:23 -040045// Test that rustlibs default linkage is correct for binaries.
46func TestBinaryLinkage(t *testing.T) {
47 ctx := testRust(t, `
48 rust_binary {
49 name: "fizz-buzz",
50 srcs: ["foo.rs"],
51 rustlibs: ["libfoo"],
52 host_supported: true,
53 }
Ivan Lozano11200872020-09-28 11:56:30 -040054 rust_binary {
55 name: "rlib_linked",
56 srcs: ["foo.rs"],
57 rustlibs: ["libfoo"],
58 host_supported: true,
59 prefer_rlib: true,
60 }
Ivan Lozano042504f2020-08-18 14:31:23 -040061 rust_library {
62 name: "libfoo",
63 srcs: ["foo.rs"],
64 crate_name: "foo",
65 host_supported: true,
66 }`)
67
68 fizzBuzzHost := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
69 fizzBuzzDevice := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module)
70
Ivan Lozano2b081132020-09-08 12:46:52 -040071 if !android.InList("libfoo.rlib-std", fizzBuzzHost.Properties.AndroidMkRlibs) {
Ivan Lozano042504f2020-08-18 14:31:23 -040072 t.Errorf("rustlibs dependency libfoo should be an rlib dep for host modules")
73 }
74
75 if !android.InList("libfoo", fizzBuzzDevice.Properties.AndroidMkDylibs) {
76 t.Errorf("rustlibs dependency libfoo should be an dylib dep for device modules")
77 }
Vinh Tran05b34102023-08-14 11:26:18 -040078
79 rlibLinkDevice := ctx.ModuleForTests("rlib_linked", "android_arm64_armv8-a").Module().(*Module)
80
81 if !android.InList("libfoo.rlib-std", rlibLinkDevice.Properties.AndroidMkRlibs) {
82 t.Errorf("rustlibs dependency libfoo should be an rlib dep for device modules when prefer_rlib is set")
83 }
Ivan Lozano042504f2020-08-18 14:31:23 -040084}
85
Ivan Lozano11200872020-09-28 11:56:30 -040086// Test that prefer_rlib links in libstd statically as well as rustlibs.
87func TestBinaryPreferRlib(t *testing.T) {
88 ctx := testRust(t, `
89 rust_binary {
90 name: "rlib_linked",
91 srcs: ["foo.rs"],
92 rustlibs: ["libfoo"],
93 host_supported: true,
94 prefer_rlib: true,
95 }
96 rust_library {
97 name: "libfoo",
98 srcs: ["foo.rs"],
99 crate_name: "foo",
100 host_supported: true,
101 }`)
102
103 mod := ctx.ModuleForTests("rlib_linked", "android_arm64_armv8-a").Module().(*Module)
104
105 if !android.InList("libfoo.rlib-std", mod.Properties.AndroidMkRlibs) {
106 t.Errorf("rustlibs dependency libfoo should be an rlib dep when prefer_rlib is defined")
107 }
108
109 if !android.InList("libstd", mod.Properties.AndroidMkRlibs) {
110 t.Errorf("libstd dependency should be an rlib dep when prefer_rlib is defined")
111 }
112}
113
Ivan Lozano042504f2020-08-18 14:31:23 -0400114// Test that the path returned by HostToolPath is correct
115func TestHostToolPath(t *testing.T) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700116 ctx := testRust(t, `
117 rust_binary_host {
Ivan Lozano042504f2020-08-18 14:31:23 -0400118 name: "fizz-buzz",
Ivan Lozanoffee3342019-08-27 12:03:00 -0700119 srcs: ["foo.rs"],
Ivan Lozano042504f2020-08-18 14:31:23 -0400120 }`)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700121
Ivan Lozano042504f2020-08-18 14:31:23 -0400122 path := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module).HostToolPath()
123 if g, w := path.String(), "/host/linux-x86/bin/fizz-buzz"; !strings.Contains(g, w) {
124 t.Errorf("wrong host tool path, expected %q got %q", w, g)
125 }
126}
127
128// Test that the flags being passed to rust_binary modules are as expected
129func TestBinaryFlags(t *testing.T) {
130 ctx := testRust(t, `
Ivan Lozanoffee3342019-08-27 12:03:00 -0700131 rust_binary_host {
132 name: "fizz-buzz",
133 srcs: ["foo.rs"],
134 }`)
135
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400136 fizzBuzz := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Rule("rustc")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700137
Sam Delmericoa588d152023-06-16 10:28:04 -0400138 flags := fizzBuzz.RuleParams.Command
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700139 if strings.Contains(flags, "--test") {
140 t.Errorf("extra --test flag, rustcFlags: %#v", flags)
141 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700142}
Ivan Lozano2093af22020-08-25 12:48:19 -0400143
Ivan Lozanoa2268632021-07-22 10:52:06 -0400144// Test that the bootstrap property sets the appropriate linker
145func TestBootstrap(t *testing.T) {
146 ctx := testRust(t, `
147 rust_binary {
148 name: "foo",
149 srcs: ["foo.rs"],
150 bootstrap: true,
151 }`)
152
Sam Delmericoa588d152023-06-16 10:28:04 -0400153 foo := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Rule("rustc")
Ivan Lozanoa2268632021-07-22 10:52:06 -0400154
155 flag := "-Wl,-dynamic-linker,/system/bin/bootstrap/linker64"
Sam Delmericoa588d152023-06-16 10:28:04 -0400156 if !strings.Contains(foo.RuleParams.Command, flag) {
157 t.Errorf("missing link flag to use bootstrap linker, expecting %#v, command: %#v", flag, foo.RuleParams.Command)
Ivan Lozanoa2268632021-07-22 10:52:06 -0400158 }
159}
160
Ivan Lozanobf63d002020-10-02 10:03:23 -0400161func TestStaticBinaryFlags(t *testing.T) {
162 ctx := testRust(t, `
163 rust_binary {
164 name: "fizz",
165 srcs: ["foo.rs"],
166 static_executable: true,
167 }`)
168
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400169 fizzOut := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Rule("rustc")
Ivan Lozanobf63d002020-10-02 10:03:23 -0400170 fizzMod := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Module().(*Module)
171
Sam Delmericoa588d152023-06-16 10:28:04 -0400172 flags := fizzOut.RuleParams.Command
Ivan Lozanobf63d002020-10-02 10:03:23 -0400173 if !strings.Contains(flags, "-C relocation-model=static") {
Sam Delmericoa588d152023-06-16 10:28:04 -0400174 t.Errorf("static binary missing '-C relocation-model=static' in command, found: %#v", flags)
Ivan Lozanobf63d002020-10-02 10:03:23 -0400175 }
Jeff Vander Stoepbf7a9022021-01-26 14:35:38 +0100176 if !strings.Contains(flags, "-C panic=abort") {
Sam Delmericoa588d152023-06-16 10:28:04 -0400177 t.Errorf("static binary missing '-C panic=abort' in command, found: %#v", flags)
Jeff Vander Stoepbf7a9022021-01-26 14:35:38 +0100178 }
Sam Delmericoa588d152023-06-16 10:28:04 -0400179 if !strings.Contains(flags, "-static") {
180 t.Errorf("static binary missing '-static' in command, found: %#v", flags)
Ivan Lozanobf63d002020-10-02 10:03:23 -0400181 }
182
183 if !android.InList("libc", fizzMod.Properties.AndroidMkStaticLibs) {
184 t.Errorf("static binary not linking against libc as a static library")
185 }
Cole Faustb6e6f992023-08-17 17:42:26 -0700186 if len(fizzMod.transitiveAndroidMkSharedLibs.ToList()) > 0 {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400187 t.Errorf("static binary incorrectly linking against shared libraries")
188 }
189}
190
Ivan Lozano2093af22020-08-25 12:48:19 -0400191func TestLinkObjects(t *testing.T) {
192 ctx := testRust(t, `
193 rust_binary {
194 name: "fizz-buzz",
195 srcs: ["foo.rs"],
196 shared_libs: ["libfoo"],
197 }
198 cc_library {
199 name: "libfoo",
200 }`)
201
Sam Delmericoa588d152023-06-16 10:28:04 -0400202 fizzBuzz := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Rule("rustc")
203 if !strings.Contains(fizzBuzz.RuleParams.Command, "/libfoo.so") {
204 t.Errorf("missing shared dependency 'libfoo.so' in command: %#v", fizzBuzz.RuleParams.Command)
Ivan Lozano2093af22020-08-25 12:48:19 -0400205 }
206}
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200207
208// Test that stripped versions are correctly generated and used.
209func TestStrippedBinary(t *testing.T) {
210 ctx := testRust(t, `
211 rust_binary {
212 name: "foo",
213 srcs: ["foo.rs"],
214 }
215 rust_binary {
216 name: "bar",
217 srcs: ["foo.rs"],
218 strip: {
219 none: true
220 }
221 }
222 `)
223
224 foo := ctx.ModuleForTests("foo", "android_arm64_armv8-a")
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400225 foo.Output("unstripped/foo")
226 foo.Output("foo")
227
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200228 // Check that the `cp` rules is using the stripped version as input.
229 cp := foo.Rule("android.Cp")
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400230 if strings.HasSuffix(cp.Input.String(), "unstripped/foo") {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200231 t.Errorf("installed binary not based on stripped version: %v", cp.Input)
232 }
233
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400234 fizzBar := ctx.ModuleForTests("bar", "android_arm64_armv8-a").MaybeOutput("unstripped/bar")
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200235 if fizzBar.Rule != nil {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400236 t.Errorf("unstripped binary exists, so stripped binary has incorrectly been generated")
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200237 }
238}