blob: c75276239ebdab4ccdfa810f913ebf59cda9da30 [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
45// Test that we reject multiple source files.
46func TestEnforceSingleSourceFile(t *testing.T) {
47
Ivan Lozano43845682020-07-09 21:03:28 -040048 singleSrcError := "srcs can only contain one path for a rust file and source providers prefixed by \":\""
Ivan Lozanoffee3342019-08-27 12:03:00 -070049
50 // Test libraries
51 testRustError(t, singleSrcError, `
52 rust_library_host {
53 name: "foo-bar-library",
54 srcs: ["foo.rs", "src/bar.rs"],
55 }`)
56
57 // Test binaries
58 testRustError(t, singleSrcError, `
59 rust_binary_host {
60 name: "foo-bar-binary",
61 srcs: ["foo.rs", "src/bar.rs"],
62 }`)
63
64 // Test proc_macros
65 testRustError(t, singleSrcError, `
66 rust_proc_macro {
67 name: "foo-bar-proc-macro",
68 srcs: ["foo.rs", "src/bar.rs"],
Ivan Lozanoffee3342019-08-27 12:03:00 -070069 }`)
70
71 // Test prebuilts
72 testRustError(t, singleSrcError, `
73 rust_prebuilt_dylib {
74 name: "foo-bar-prebuilt",
75 srcs: ["liby.so", "libz.so"],
76 host_supported: true,
77 }`)
78}
Ivan Lozanof900f4b2020-04-28 13:58:45 -040079
80func TestInstallDir(t *testing.T) {
81 ctx := testRust(t, `
82 rust_library_dylib {
83 name: "libfoo",
84 srcs: ["foo.rs"],
85 crate_name: "foo",
86 }
87 rust_binary {
88 name: "fizzbuzz",
89 srcs: ["foo.rs"],
90 }`)
91
92 install_path_lib64 := ctx.ModuleForTests("libfoo",
93 "android_arm64_armv8-a_dylib").Module().(*Module).compiler.(*libraryDecorator).path.String()
94 install_path_lib32 := ctx.ModuleForTests("libfoo",
95 "android_arm_armv7-a-neon_dylib").Module().(*Module).compiler.(*libraryDecorator).path.String()
96 install_path_bin := ctx.ModuleForTests("fizzbuzz",
97 "android_arm64_armv8-a").Module().(*Module).compiler.(*binaryDecorator).path.String()
98
99 if !strings.HasSuffix(install_path_lib64, "system/lib64/libfoo.dylib.so") {
100 t.Fatalf("unexpected install path for 64-bit library: %#v", install_path_lib64)
101 }
102 if !strings.HasSuffix(install_path_lib32, "system/lib/libfoo.dylib.so") {
103 t.Fatalf("unexpected install path for 32-bit library: %#v", install_path_lib32)
104 }
105 if !strings.HasSuffix(install_path_bin, "system/bin/fizzbuzz") {
106 t.Fatalf("unexpected install path for binary: %#v", install_path_bin)
107 }
108}
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200109
110func TestLints(t *testing.T) {
111
112 bp := `
113 // foo uses the default value of lints
114 rust_library {
115 name: "libfoo",
116 srcs: ["foo.rs"],
117 crate_name: "foo",
118 }
119 // bar forces the use of the "android" lint set
120 rust_library {
121 name: "libbar",
122 srcs: ["foo.rs"],
123 crate_name: "bar",
124 lints: "android",
125 }
126 // foobar explicitly disable all lints
127 rust_library {
128 name: "libfoobar",
129 srcs: ["foo.rs"],
130 crate_name: "foobar",
131 lints: "none",
132 }`
133
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200134 var lintTests = []struct {
135 modulePath string
136 fooFlags string
137 }{
138 {"", "${config.RustDefaultLints}"},
139 {"external/", "${config.RustAllowAllLints}"},
140 {"hardware/", "${config.RustVendorLints}"},
141 }
142
143 for _, tc := range lintTests {
144 t.Run("path="+tc.modulePath, func(t *testing.T) {
145
Paul Duffin9e0c3f92021-03-30 22:45:21 +0100146 result := android.GroupFixturePreparers(
147 prepareForRustTest,
148 // Test with the blueprint file in different directories.
149 android.FixtureAddTextFile(tc.modulePath+"Android.bp", bp),
150 ).RunTest(t)
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200151
Paul Duffin9e0c3f92021-03-30 22:45:21 +0100152 r := result.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").MaybeRule("rustc")
153 android.AssertStringDoesContain(t, "libfoo flags", r.Args["rustcFlags"], tc.fooFlags)
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200154
Paul Duffin9e0c3f92021-03-30 22:45:21 +0100155 r = result.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeRule("rustc")
156 android.AssertStringDoesContain(t, "libbar flags", r.Args["rustcFlags"], "${config.RustDefaultLints}")
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200157
Paul Duffin9e0c3f92021-03-30 22:45:21 +0100158 r = result.ModuleForTests("libfoobar", "android_arm64_armv8-a_dylib").MaybeRule("rustc")
159 android.AssertStringDoesContain(t, "libfoobar flags", r.Args["rustcFlags"], "${config.RustAllowAllLints}")
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200160 })
161 }
162}
Ivan Lozano042504f2020-08-18 14:31:23 -0400163
164// Test that devices are linking the stdlib dynamically
165func TestStdDeviceLinkage(t *testing.T) {
166 ctx := testRust(t, `
167 rust_binary {
168 name: "fizz",
169 srcs: ["foo.rs"],
170 }
171 rust_library {
172 name: "libfoo",
173 srcs: ["foo.rs"],
174 crate_name: "foo",
175 }`)
176 fizz := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400177 fooRlib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Module().(*Module)
Ivan Lozano042504f2020-08-18 14:31:23 -0400178 fooDylib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
179
180 if !android.InList("libstd", fizz.Properties.AndroidMkDylibs) {
181 t.Errorf("libstd is not linked dynamically for device binaries")
182 }
183 if !android.InList("libstd", fooRlib.Properties.AndroidMkDylibs) {
184 t.Errorf("libstd is not linked dynamically for rlibs")
185 }
186 if !android.InList("libstd", fooDylib.Properties.AndroidMkDylibs) {
187 t.Errorf("libstd is not linked dynamically for dylibs")
188 }
189}