blob: 7dac2490a7dfea97ca8f1f389cbd5fc361df2438 [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 }
Ivan Lozano11200872020-09-28 11:56:30 -040033 rust_binary {
34 name: "rlib_linked",
35 srcs: ["foo.rs"],
36 rustlibs: ["libfoo"],
37 host_supported: true,
38 prefer_rlib: true,
39 }
Ivan Lozano042504f2020-08-18 14:31:23 -040040 rust_library {
41 name: "libfoo",
42 srcs: ["foo.rs"],
43 crate_name: "foo",
44 host_supported: true,
45 }`)
46
47 fizzBuzzHost := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
48 fizzBuzzDevice := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module)
49
Ivan Lozano2b081132020-09-08 12:46:52 -040050 if !android.InList("libfoo.rlib-std", fizzBuzzHost.Properties.AndroidMkRlibs) {
Ivan Lozano042504f2020-08-18 14:31:23 -040051 t.Errorf("rustlibs dependency libfoo should be an rlib dep for host modules")
52 }
53
54 if !android.InList("libfoo", fizzBuzzDevice.Properties.AndroidMkDylibs) {
55 t.Errorf("rustlibs dependency libfoo should be an dylib dep for device modules")
56 }
57}
58
Ivan Lozano11200872020-09-28 11:56:30 -040059// Test that prefer_rlib links in libstd statically as well as rustlibs.
60func TestBinaryPreferRlib(t *testing.T) {
61 ctx := testRust(t, `
62 rust_binary {
63 name: "rlib_linked",
64 srcs: ["foo.rs"],
65 rustlibs: ["libfoo"],
66 host_supported: true,
67 prefer_rlib: true,
68 }
69 rust_library {
70 name: "libfoo",
71 srcs: ["foo.rs"],
72 crate_name: "foo",
73 host_supported: true,
74 }`)
75
76 mod := ctx.ModuleForTests("rlib_linked", "android_arm64_armv8-a").Module().(*Module)
77
78 if !android.InList("libfoo.rlib-std", mod.Properties.AndroidMkRlibs) {
79 t.Errorf("rustlibs dependency libfoo should be an rlib dep when prefer_rlib is defined")
80 }
81
82 if !android.InList("libstd", mod.Properties.AndroidMkRlibs) {
83 t.Errorf("libstd dependency should be an rlib dep when prefer_rlib is defined")
84 }
85}
86
Ivan Lozano042504f2020-08-18 14:31:23 -040087// Test that the path returned by HostToolPath is correct
88func TestHostToolPath(t *testing.T) {
Ivan Lozanoffee3342019-08-27 12:03:00 -070089 ctx := testRust(t, `
90 rust_binary_host {
Ivan Lozano042504f2020-08-18 14:31:23 -040091 name: "fizz-buzz",
Ivan Lozanoffee3342019-08-27 12:03:00 -070092 srcs: ["foo.rs"],
Ivan Lozano042504f2020-08-18 14:31:23 -040093 }`)
Ivan Lozanoffee3342019-08-27 12:03:00 -070094
Ivan Lozano042504f2020-08-18 14:31:23 -040095 path := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module).HostToolPath()
96 if g, w := path.String(), "/host/linux-x86/bin/fizz-buzz"; !strings.Contains(g, w) {
97 t.Errorf("wrong host tool path, expected %q got %q", w, g)
98 }
99}
100
101// Test that the flags being passed to rust_binary modules are as expected
102func TestBinaryFlags(t *testing.T) {
103 ctx := testRust(t, `
Ivan Lozanoffee3342019-08-27 12:03:00 -0700104 rust_binary_host {
105 name: "fizz-buzz",
106 srcs: ["foo.rs"],
107 }`)
108
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400109 fizzBuzz := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Rule("rustc")
Ivan Lozanoffee3342019-08-27 12:03:00 -0700110
Ivan Lozano042504f2020-08-18 14:31:23 -0400111 flags := fizzBuzz.Args["rustcFlags"]
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700112 if strings.Contains(flags, "--test") {
113 t.Errorf("extra --test flag, rustcFlags: %#v", flags)
114 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700115}
Ivan Lozano2093af22020-08-25 12:48:19 -0400116
Ivan Lozanoa2268632021-07-22 10:52:06 -0400117// Test that the bootstrap property sets the appropriate linker
118func TestBootstrap(t *testing.T) {
119 ctx := testRust(t, `
120 rust_binary {
121 name: "foo",
122 srcs: ["foo.rs"],
123 bootstrap: true,
124 }`)
125
126 foo := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Rule("rustc")
127
128 flag := "-Wl,-dynamic-linker,/system/bin/bootstrap/linker64"
129 if !strings.Contains(foo.Args["linkFlags"], flag) {
130 t.Errorf("missing link flag to use bootstrap linker, expecting %#v, linkFlags: %#v", flag, foo.Args["linkFlags"])
131 }
132}
133
Ivan Lozanobf63d002020-10-02 10:03:23 -0400134func TestStaticBinaryFlags(t *testing.T) {
135 ctx := testRust(t, `
136 rust_binary {
137 name: "fizz",
138 srcs: ["foo.rs"],
139 static_executable: true,
140 }`)
141
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400142 fizzOut := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Rule("rustc")
Ivan Lozanobf63d002020-10-02 10:03:23 -0400143 fizzMod := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Module().(*Module)
144
145 flags := fizzOut.Args["rustcFlags"]
146 linkFlags := fizzOut.Args["linkFlags"]
147 if !strings.Contains(flags, "-C relocation-model=static") {
148 t.Errorf("static binary missing '-C relocation-model=static' in rustcFlags, found: %#v", flags)
149 }
Jeff Vander Stoepbf7a9022021-01-26 14:35:38 +0100150 if !strings.Contains(flags, "-C panic=abort") {
151 t.Errorf("static binary missing '-C panic=abort' in rustcFlags, found: %#v", flags)
152 }
Ivan Lozanobf63d002020-10-02 10:03:23 -0400153 if !strings.Contains(linkFlags, "-static") {
154 t.Errorf("static binary missing '-static' in linkFlags, found: %#v", flags)
155 }
156
157 if !android.InList("libc", fizzMod.Properties.AndroidMkStaticLibs) {
158 t.Errorf("static binary not linking against libc as a static library")
159 }
160 if len(fizzMod.Properties.AndroidMkSharedLibs) > 0 {
161 t.Errorf("static binary incorrectly linking against shared libraries")
162 }
163}
164
Ivan Lozano2093af22020-08-25 12:48:19 -0400165func TestLinkObjects(t *testing.T) {
166 ctx := testRust(t, `
167 rust_binary {
168 name: "fizz-buzz",
169 srcs: ["foo.rs"],
170 shared_libs: ["libfoo"],
171 }
172 cc_library {
173 name: "libfoo",
174 }`)
175
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400176 fizzBuzz := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Rule("rustc")
Ivan Lozano2093af22020-08-25 12:48:19 -0400177 linkFlags := fizzBuzz.Args["linkFlags"]
178 if !strings.Contains(linkFlags, "/libfoo.so") {
179 t.Errorf("missing shared dependency 'libfoo.so' in linkFlags: %#v", linkFlags)
180 }
181}
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200182
183// Test that stripped versions are correctly generated and used.
184func TestStrippedBinary(t *testing.T) {
185 ctx := testRust(t, `
186 rust_binary {
187 name: "foo",
188 srcs: ["foo.rs"],
189 }
190 rust_binary {
191 name: "bar",
192 srcs: ["foo.rs"],
193 strip: {
194 none: true
195 }
196 }
197 `)
198
199 foo := ctx.ModuleForTests("foo", "android_arm64_armv8-a")
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400200 foo.Output("unstripped/foo")
201 foo.Output("foo")
202
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200203 // Check that the `cp` rules is using the stripped version as input.
204 cp := foo.Rule("android.Cp")
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400205 if strings.HasSuffix(cp.Input.String(), "unstripped/foo") {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200206 t.Errorf("installed binary not based on stripped version: %v", cp.Input)
207 }
208
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400209 fizzBar := ctx.ModuleForTests("bar", "android_arm64_armv8-a").MaybeOutput("unstripped/bar")
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200210 if fizzBar.Rule != nil {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400211 t.Errorf("unstripped binary exists, so stripped binary has incorrectly been generated")
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200212 }
213}