blob: b44a5bc71946b1114116eb8c11207180991baeee [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
109 fizzBuzz := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Output("fizz-buzz")
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 Lozanobf63d002020-10-02 10:03:23 -0400117func TestStaticBinaryFlags(t *testing.T) {
118 ctx := testRust(t, `
119 rust_binary {
120 name: "fizz",
121 srcs: ["foo.rs"],
122 static_executable: true,
123 }`)
124
125 fizzOut := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Output("fizz")
126 fizzMod := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Module().(*Module)
127
128 flags := fizzOut.Args["rustcFlags"]
129 linkFlags := fizzOut.Args["linkFlags"]
130 if !strings.Contains(flags, "-C relocation-model=static") {
131 t.Errorf("static binary missing '-C relocation-model=static' in rustcFlags, found: %#v", flags)
132 }
133 if !strings.Contains(linkFlags, "-static") {
134 t.Errorf("static binary missing '-static' in linkFlags, found: %#v", flags)
135 }
136
137 if !android.InList("libc", fizzMod.Properties.AndroidMkStaticLibs) {
138 t.Errorf("static binary not linking against libc as a static library")
139 }
140 if len(fizzMod.Properties.AndroidMkSharedLibs) > 0 {
141 t.Errorf("static binary incorrectly linking against shared libraries")
142 }
143}
144
Ivan Lozano2093af22020-08-25 12:48:19 -0400145func TestLinkObjects(t *testing.T) {
146 ctx := testRust(t, `
147 rust_binary {
148 name: "fizz-buzz",
149 srcs: ["foo.rs"],
150 shared_libs: ["libfoo"],
151 }
152 cc_library {
153 name: "libfoo",
154 }`)
155
156 fizzBuzz := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Output("fizz-buzz")
157 linkFlags := fizzBuzz.Args["linkFlags"]
158 if !strings.Contains(linkFlags, "/libfoo.so") {
159 t.Errorf("missing shared dependency 'libfoo.so' in linkFlags: %#v", linkFlags)
160 }
161}
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200162
163// Test that stripped versions are correctly generated and used.
164func TestStrippedBinary(t *testing.T) {
165 ctx := testRust(t, `
166 rust_binary {
167 name: "foo",
168 srcs: ["foo.rs"],
169 }
170 rust_binary {
171 name: "bar",
172 srcs: ["foo.rs"],
173 strip: {
174 none: true
175 }
176 }
177 `)
178
179 foo := ctx.ModuleForTests("foo", "android_arm64_armv8-a")
180 foo.Output("stripped/foo")
181 // Check that the `cp` rules is using the stripped version as input.
182 cp := foo.Rule("android.Cp")
183 if !strings.HasSuffix(cp.Input.String(), "stripped/foo") {
184 t.Errorf("installed binary not based on stripped version: %v", cp.Input)
185 }
186
187 fizzBar := ctx.ModuleForTests("bar", "android_arm64_armv8-a").MaybeOutput("stripped/bar")
188 if fizzBar.Rule != nil {
189 t.Errorf("stripped version of bar has been generated")
190 }
191}