blob: 2b40727e753148037d289dd1aa86974dc83d3ca1 [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"
Jiyong Park99644e92020-11-17 22:21:02 +090022 "android/soong/cc"
Ivan Lozanoffee3342019-08-27 12:03:00 -070023)
24
25// Test that feature flags are being correctly generated.
26func TestFeaturesToFlags(t *testing.T) {
27 ctx := testRust(t, `
28 rust_library_host_dylib {
29 name: "libfoo",
30 srcs: ["foo.rs"],
31 crate_name: "foo",
32 features: [
33 "fizz",
34 "buzz"
35 ],
36 }`)
37
38 libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Rule("rustc")
39
40 if !strings.Contains(libfooDylib.Args["rustcFlags"], "cfg 'feature=\"fizz\"'") ||
41 !strings.Contains(libfooDylib.Args["rustcFlags"], "cfg 'feature=\"buzz\"'") {
42 t.Fatalf("missing fizz and buzz feature flags for libfoo dylib, rustcFlags: %#v", libfooDylib.Args["rustcFlags"])
43 }
44}
45
46// Test that we reject multiple source files.
47func TestEnforceSingleSourceFile(t *testing.T) {
48
Ivan Lozano43845682020-07-09 21:03:28 -040049 singleSrcError := "srcs can only contain one path for a rust file and source providers prefixed by \":\""
Ivan Lozanoffee3342019-08-27 12:03:00 -070050
51 // Test libraries
52 testRustError(t, singleSrcError, `
53 rust_library_host {
54 name: "foo-bar-library",
55 srcs: ["foo.rs", "src/bar.rs"],
56 }`)
57
58 // Test binaries
59 testRustError(t, singleSrcError, `
60 rust_binary_host {
61 name: "foo-bar-binary",
62 srcs: ["foo.rs", "src/bar.rs"],
63 }`)
64
65 // Test proc_macros
66 testRustError(t, singleSrcError, `
67 rust_proc_macro {
68 name: "foo-bar-proc-macro",
69 srcs: ["foo.rs", "src/bar.rs"],
Ivan Lozanoffee3342019-08-27 12:03:00 -070070 }`)
71
72 // Test prebuilts
73 testRustError(t, singleSrcError, `
74 rust_prebuilt_dylib {
75 name: "foo-bar-prebuilt",
76 srcs: ["liby.so", "libz.so"],
77 host_supported: true,
78 }`)
79}
Ivan Lozanof900f4b2020-04-28 13:58:45 -040080
81func TestInstallDir(t *testing.T) {
82 ctx := testRust(t, `
83 rust_library_dylib {
84 name: "libfoo",
85 srcs: ["foo.rs"],
86 crate_name: "foo",
87 }
88 rust_binary {
89 name: "fizzbuzz",
90 srcs: ["foo.rs"],
91 }`)
92
93 install_path_lib64 := ctx.ModuleForTests("libfoo",
94 "android_arm64_armv8-a_dylib").Module().(*Module).compiler.(*libraryDecorator).path.String()
95 install_path_lib32 := ctx.ModuleForTests("libfoo",
96 "android_arm_armv7-a-neon_dylib").Module().(*Module).compiler.(*libraryDecorator).path.String()
97 install_path_bin := ctx.ModuleForTests("fizzbuzz",
98 "android_arm64_armv8-a").Module().(*Module).compiler.(*binaryDecorator).path.String()
99
100 if !strings.HasSuffix(install_path_lib64, "system/lib64/libfoo.dylib.so") {
101 t.Fatalf("unexpected install path for 64-bit library: %#v", install_path_lib64)
102 }
103 if !strings.HasSuffix(install_path_lib32, "system/lib/libfoo.dylib.so") {
104 t.Fatalf("unexpected install path for 32-bit library: %#v", install_path_lib32)
105 }
106 if !strings.HasSuffix(install_path_bin, "system/bin/fizzbuzz") {
107 t.Fatalf("unexpected install path for binary: %#v", install_path_bin)
108 }
109}
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200110
111func TestLints(t *testing.T) {
112
113 bp := `
114 // foo uses the default value of lints
115 rust_library {
116 name: "libfoo",
117 srcs: ["foo.rs"],
118 crate_name: "foo",
119 }
120 // bar forces the use of the "android" lint set
121 rust_library {
122 name: "libbar",
123 srcs: ["foo.rs"],
124 crate_name: "bar",
125 lints: "android",
126 }
127 // foobar explicitly disable all lints
128 rust_library {
129 name: "libfoobar",
130 srcs: ["foo.rs"],
131 crate_name: "foobar",
132 lints: "none",
133 }`
134
135 bp = bp + GatherRequiredDepsForTest()
Jiyong Park99644e92020-11-17 22:21:02 +0900136 bp = bp + cc.GatherRequiredDepsForTest(android.NoOsType)
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200137
138 fs := map[string][]byte{
139 // Reuse the same blueprint file for subdirectories.
140 "external/Android.bp": []byte(bp),
141 "hardware/Android.bp": []byte(bp),
142 }
143
144 var lintTests = []struct {
145 modulePath string
146 fooFlags string
147 }{
148 {"", "${config.RustDefaultLints}"},
149 {"external/", "${config.RustAllowAllLints}"},
150 {"hardware/", "${config.RustVendorLints}"},
151 }
152
153 for _, tc := range lintTests {
154 t.Run("path="+tc.modulePath, func(t *testing.T) {
155
156 config := android.TestArchConfig(buildDir, nil, bp, fs)
Colin Crossae8600b2020-10-29 17:09:13 -0700157 ctx := CreateTestContext(config)
158 ctx.Register()
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200159 _, errs := ctx.ParseFileList(".", []string{tc.modulePath + "Android.bp"})
160 android.FailIfErrored(t, errs)
161 _, errs = ctx.PrepareBuildActions(config)
162 android.FailIfErrored(t, errs)
163
164 r := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").MaybeRule("rustc")
165 if !strings.Contains(r.Args["rustcFlags"], tc.fooFlags) {
166 t.Errorf("Incorrect flags for libfoo: %q, want %q", r.Args["rustcFlags"], tc.fooFlags)
167 }
168
169 r = ctx.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeRule("rustc")
170 if !strings.Contains(r.Args["rustcFlags"], "${config.RustDefaultLints}") {
171 t.Errorf("Incorrect flags for libbar: %q, want %q", r.Args["rustcFlags"], "${config.RustDefaultLints}")
172 }
173
174 r = ctx.ModuleForTests("libfoobar", "android_arm64_armv8-a_dylib").MaybeRule("rustc")
175 if !strings.Contains(r.Args["rustcFlags"], "${config.RustAllowAllLints}") {
176 t.Errorf("Incorrect flags for libfoobar: %q, want %q", r.Args["rustcFlags"], "${config.RustAllowAllLints}")
177 }
178
179 })
180 }
181}
Ivan Lozano042504f2020-08-18 14:31:23 -0400182
183// Test that devices are linking the stdlib dynamically
184func TestStdDeviceLinkage(t *testing.T) {
185 ctx := testRust(t, `
186 rust_binary {
187 name: "fizz",
188 srcs: ["foo.rs"],
189 }
190 rust_library {
191 name: "libfoo",
192 srcs: ["foo.rs"],
193 crate_name: "foo",
194 }`)
195 fizz := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Module().(*Module)
Ivan Lozano2b081132020-09-08 12:46:52 -0400196 fooRlib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib_dylib-std").Module().(*Module)
Ivan Lozano042504f2020-08-18 14:31:23 -0400197 fooDylib := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").Module().(*Module)
198
199 if !android.InList("libstd", fizz.Properties.AndroidMkDylibs) {
200 t.Errorf("libstd is not linked dynamically for device binaries")
201 }
202 if !android.InList("libstd", fooRlib.Properties.AndroidMkDylibs) {
203 t.Errorf("libstd is not linked dynamically for rlibs")
204 }
205 if !android.InList("libstd", fooDylib.Properties.AndroidMkDylibs) {
206 t.Errorf("libstd is not linked dynamically for dylibs")
207 }
208}