blob: f589b691dbcf40b9bec7b53c4203ebdeee8d52bc [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"
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020020
21 "android/soong/android"
Ivan Lozanoffee3342019-08-27 12:03:00 -070022)
23
24// Test that feature flags are being correctly generated.
25func TestFeaturesToFlags(t *testing.T) {
26 ctx := testRust(t, `
27 rust_library_host_dylib {
28 name: "libfoo",
29 srcs: ["foo.rs"],
30 crate_name: "foo",
31 features: [
32 "fizz",
33 "buzz"
34 ],
35 }`)
36
37 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Rule("rustc")
38
39 if !strings.Contains(libfooDylib.Args["rustcFlags"], "cfg 'feature=\"fizz\"'") ||
40 !strings.Contains(libfooDylib.Args["rustcFlags"], "cfg 'feature=\"buzz\"'") {
41 t.Fatalf("missing fizz and buzz feature flags for libfoo dylib, rustcFlags: %#v", libfooDylib.Args["rustcFlags"])
42 }
43}
44
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +020045// Test that cfgs flags are being correctly generated.
46func TestCfgsToFlags(t *testing.T) {
47 ctx := testRust(t, `
48 rust_library_host {
49 name: "libfoo",
50 srcs: ["foo.rs"],
51 crate_name: "foo",
52 cfgs: [
53 "std",
54 "cfg1=\"one\""
55 ],
56 }`)
57
58 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Rule("rustc")
59
60 if !strings.Contains(libfooDylib.Args["rustcFlags"], "cfg 'std'") ||
61 !strings.Contains(libfooDylib.Args["rustcFlags"], "cfg 'cfg1=\"one\"'") {
62 t.Fatalf("missing std and cfg1 flags for libfoo dylib, rustcFlags: %#v", libfooDylib.Args["rustcFlags"])
63 }
64}
65
Ivan Lozanoffee3342019-08-27 12:03:00 -070066// Test that we reject multiple source files.
67func TestEnforceSingleSourceFile(t *testing.T) {
68
Ivan Lozano43845682020-07-09 21:03:28 -040069 singleSrcError := "srcs can only contain one path for a rust file and source providers prefixed by \":\""
Ivan Lozanoffee3342019-08-27 12:03:00 -070070
71 // Test libraries
72 testRustError(t, singleSrcError, `
73 rust_library_host {
74 name: "foo-bar-library",
75 srcs: ["foo.rs", "src/bar.rs"],
76 }`)
77
78 // Test binaries
79 testRustError(t, singleSrcError, `
80 rust_binary_host {
81 name: "foo-bar-binary",
82 srcs: ["foo.rs", "src/bar.rs"],
83 }`)
84
85 // Test proc_macros
86 testRustError(t, singleSrcError, `
87 rust_proc_macro {
88 name: "foo-bar-proc-macro",
89 srcs: ["foo.rs", "src/bar.rs"],
Ivan Lozanoffee3342019-08-27 12:03:00 -070090 }`)
91
92 // Test prebuilts
93 testRustError(t, singleSrcError, `
94 rust_prebuilt_dylib {
95 name: "foo-bar-prebuilt",
96 srcs: ["liby.so", "libz.so"],
97 host_supported: true,
98 }`)
99}
Ivan Lozanof900f4b2020-04-28 13:58:45 -0400100
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400101// Test environment vars for Cargo compat are set.
102func TestCargoCompat(t *testing.T) {
103 ctx := testRust(t, `
104 rust_binary {
105 name: "fizz",
106 srcs: ["foo.rs"],
107 crate_name: "foo",
108 cargo_env_compat: true,
109 cargo_pkg_version: "1.0.0"
110 }`)
111
112 fizz := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Rule("rustc")
113
114 if !strings.Contains(fizz.Args["envVars"], "CARGO_BIN_NAME=fizz") {
115 t.Fatalf("expected 'CARGO_BIN_NAME=fizz' in envVars, actual envVars: %#v", fizz.Args["envVars"])
116 }
117 if !strings.Contains(fizz.Args["envVars"], "CARGO_CRATE_NAME=foo") {
118 t.Fatalf("expected 'CARGO_CRATE_NAME=foo' in envVars, actual envVars: %#v", fizz.Args["envVars"])
119 }
120 if !strings.Contains(fizz.Args["envVars"], "CARGO_PKG_VERSION=1.0.0") {
121 t.Fatalf("expected 'CARGO_PKG_VERSION=1.0.0' in envVars, actual envVars: %#v", fizz.Args["envVars"])
122 }
123}
124
Ivan Lozanof900f4b2020-04-28 13:58:45 -0400125func TestInstallDir(t *testing.T) {
126 ctx := testRust(t, `
127 rust_library_dylib {
128 name: "libfoo",
129 srcs: ["foo.rs"],
130 crate_name: "foo",
131 }
132 rust_binary {
133 name: "fizzbuzz",
134 srcs: ["foo.rs"],
135 }`)
136
137 install_path_lib64 := ctx.ModuleForTests("libfoo",
138 "android_arm64_armv8-a_dylib").Module().(*Module).compiler.(*libraryDecorator).path.String()
139 install_path_lib32 := ctx.ModuleForTests("libfoo",
140 "android_arm_armv7-a-neon_dylib").Module().(*Module).compiler.(*libraryDecorator).path.String()
141 install_path_bin := ctx.ModuleForTests("fizzbuzz",
142 "android_arm64_armv8-a").Module().(*Module).compiler.(*binaryDecorator).path.String()
143
144 if !strings.HasSuffix(install_path_lib64, "system/lib64/libfoo.dylib.so") {
145 t.Fatalf("unexpected install path for 64-bit library: %#v", install_path_lib64)
146 }
147 if !strings.HasSuffix(install_path_lib32, "system/lib/libfoo.dylib.so") {
148 t.Fatalf("unexpected install path for 32-bit library: %#v", install_path_lib32)
149 }
150 if !strings.HasSuffix(install_path_bin, "system/bin/fizzbuzz") {
151 t.Fatalf("unexpected install path for binary: %#v", install_path_bin)
152 }
153}
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200154
155func TestLints(t *testing.T) {
156
157 bp := `
158 // foo uses the default value of lints
159 rust_library {
160 name: "libfoo",
161 srcs: ["foo.rs"],
162 crate_name: "foo",
163 }
164 // bar forces the use of the "android" lint set
165 rust_library {
166 name: "libbar",
167 srcs: ["foo.rs"],
168 crate_name: "bar",
169 lints: "android",
170 }
171 // foobar explicitly disable all lints
172 rust_library {
173 name: "libfoobar",
174 srcs: ["foo.rs"],
175 crate_name: "foobar",
176 lints: "none",
177 }`
178
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200179 var lintTests = []struct {
180 modulePath string
181 fooFlags string
182 }{
183 {"", "${config.RustDefaultLints}"},
184 {"external/", "${config.RustAllowAllLints}"},
185 {"hardware/", "${config.RustVendorLints}"},
186 }
187
188 for _, tc := range lintTests {
189 t.Run("path="+tc.modulePath, func(t *testing.T) {
190
Paul Duffin9e0c3f92021-03-30 22:45:21 +0100191 result := android.GroupFixturePreparers(
192 prepareForRustTest,
193 // Test with the blueprint file in different directories.
194 android.FixtureAddTextFile(tc.modulePath+"Android.bp", bp),
195 ).RunTest(t)
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200196
Paul Duffin9e0c3f92021-03-30 22:45:21 +0100197 r := result.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").MaybeRule("rustc")
198 android.AssertStringDoesContain(t, "libfoo flags", r.Args["rustcFlags"], tc.fooFlags)
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200199
Paul Duffin9e0c3f92021-03-30 22:45:21 +0100200 r = result.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeRule("rustc")
201 android.AssertStringDoesContain(t, "libbar flags", r.Args["rustcFlags"], "${config.RustDefaultLints}")
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200202
Paul Duffin9e0c3f92021-03-30 22:45:21 +0100203 r = result.ModuleForTests("libfoobar", "android_arm64_armv8-a_dylib").MaybeRule("rustc")
204 android.AssertStringDoesContain(t, "libfoobar flags", r.Args["rustcFlags"], "${config.RustAllowAllLints}")
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200205 })
206 }
207}
Ivan Lozano042504f2020-08-18 14:31:23 -0400208
209// Test that devices are linking the stdlib dynamically
210func TestStdDeviceLinkage(t *testing.T) {
211 ctx := testRust(t, `
212 rust_binary {
213 name: "fizz",
214 srcs: ["foo.rs"],
215 }
216 rust_library {
217 name: "libfoo",
218 srcs: ["foo.rs"],
219 crate_name: "foo",
220 }`)
221 fizz := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400222 fooRlib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Module().(*Module)
Ivan Lozano042504f2020-08-18 14:31:23 -0400223 fooDylib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
224
225 if !android.InList("libstd", fizz.Properties.AndroidMkDylibs) {
226 t.Errorf("libstd is not linked dynamically for device binaries")
227 }
228 if !android.InList("libstd", fooRlib.Properties.AndroidMkDylibs) {
229 t.Errorf("libstd is not linked dynamically for rlibs")
230 }
231 if !android.InList("libstd", fooDylib.Properties.AndroidMkDylibs) {
232 t.Errorf("libstd is not linked dynamically for dylibs")
233 }
234}
Ivan Lozano45a9e312021-07-27 12:29:12 -0400235
236// Ensure that manual link flags are disallowed.
237func TestManualLinkageRejection(t *testing.T) {
238 // rustc flags
239 testRustError(t, ".* cannot be manually specified", `
240 rust_binary {
241 name: "foo",
242 srcs: [
243 "foo.rs",
244 ],
245 flags: ["-lbar"],
246 }
247 `)
248 testRustError(t, ".* cannot be manually specified", `
249 rust_binary {
250 name: "foo",
251 srcs: [
252 "foo.rs",
253 ],
254 flags: ["--extern=foo"],
255 }
256 `)
257 testRustError(t, ".* cannot be manually specified", `
258 rust_binary {
259 name: "foo",
260 srcs: [
261 "foo.rs",
262 ],
263 flags: ["-Clink-args=foo"],
264 }
265 `)
266 testRustError(t, ".* cannot be manually specified", `
267 rust_binary {
268 name: "foo",
269 srcs: [
270 "foo.rs",
271 ],
272 flags: ["-C link-args=foo"],
273 }
274 `)
275 testRustError(t, ".* cannot be manually specified", `
276 rust_binary {
277 name: "foo",
278 srcs: [
279 "foo.rs",
280 ],
281 flags: ["-L foo/"],
282 }
283 `)
284
285 // lld flags
286 testRustError(t, ".* cannot be manually specified", `
287 rust_binary {
288 name: "foo",
289 srcs: [
290 "foo.rs",
291 ],
292 ld_flags: ["-Wl,-L bar/"],
293 }
294 `)
295 testRustError(t, ".* cannot be manually specified", `
296 rust_binary {
297 name: "foo",
298 srcs: [
299 "foo.rs",
300 ],
301 ld_flags: ["-Wl,-lbar"],
302 }
303 `)
304}