blob: e7f873e91765f9a4ebaf7597cfef8c48413e55c8 [file] [log] [blame]
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001// Copyright 2020 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"
20
21 "android/soong/android"
22)
23
24// Test that coverage flags are being correctly generated.
25func TestCoverageFlags(t *testing.T) {
26 ctx := testRustCov(t, `
27 rust_library {
28 name: "libfoo_cov",
29 srcs: ["foo.rs"],
30 crate_name: "foo",
31 }
32 rust_binary {
33 name: "fizz_cov",
34 srcs: ["foo.rs"],
35 }
36 rust_binary {
37 name: "buzzNoCov",
38 srcs: ["foo.rs"],
39 native_coverage: false,
40 }
41 rust_library {
42 name: "libbar_nocov",
43 srcs: ["foo.rs"],
44 crate_name: "bar",
45 native_coverage: false,
46 }`)
47
48 // Make sure native_coverage: false isn't creating a coverage variant.
49 if android.InList("android_arm64_armv8-a_dylib_cov", ctx.ModuleVariantsForTests("libbar_nocov")) {
50 t.Fatalf("coverage variant created for module 'libbar_nocov' with native coverage disabled")
51 }
52
53 // Just test the dylib variants unless the library coverage logic changes to distinguish between the types.
54 libfooCov := ctx.ModuleForTests("libfoo_cov", "android_arm64_armv8-a_dylib_cov").Rule("rustc")
55 libbarNoCov := ctx.ModuleForTests("libbar_nocov", "android_arm64_armv8-a_dylib").Rule("rustc")
56 fizzCov := ctx.ModuleForTests("fizz_cov", "android_arm64_armv8-a_cov").Rule("rustc")
57 buzzNoCov := ctx.ModuleForTests("buzzNoCov", "android_arm64_armv8-a").Rule("rustc")
58
Joel Galenson6945e9b2020-08-25 15:12:59 -070059 rustcCoverageFlags := []string{"-Z profile", " -g ", "-C opt-level=0", "-C link-dead-code"}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040060 for _, flag := range rustcCoverageFlags {
61 missingErrorStr := "missing rustc flag '%s' for '%s' module with coverage enabled; rustcFlags: %#v"
62 containsErrorStr := "contains rustc flag '%s' for '%s' module with coverage disabled; rustcFlags: %#v"
63
64 if !strings.Contains(fizzCov.Args["rustcFlags"], flag) {
65 t.Fatalf(missingErrorStr, flag, "fizz_cov", fizzCov.Args["rustcFlags"])
66 }
67 if !strings.Contains(libfooCov.Args["rustcFlags"], flag) {
68 t.Fatalf(missingErrorStr, flag, "libfoo_cov dylib", libfooCov.Args["rustcFlags"])
69 }
70 if strings.Contains(buzzNoCov.Args["rustcFlags"], flag) {
71 t.Fatalf(containsErrorStr, flag, "buzzNoCov", buzzNoCov.Args["rustcFlags"])
72 }
73 if strings.Contains(libbarNoCov.Args["rustcFlags"], flag) {
74 t.Fatalf(containsErrorStr, flag, "libbar_cov", libbarNoCov.Args["rustcFlags"])
75 }
76 }
77
78 linkCoverageFlags := []string{"--coverage", " -g "}
79 for _, flag := range linkCoverageFlags {
80 missingErrorStr := "missing rust linker flag '%s' for '%s' module with coverage enabled; rustcFlags: %#v"
81 containsErrorStr := "contains rust linker flag '%s' for '%s' module with coverage disabled; rustcFlags: %#v"
82
83 if !strings.Contains(fizzCov.Args["linkFlags"], flag) {
84 t.Fatalf(missingErrorStr, flag, "fizz_cov", fizzCov.Args["linkFlags"])
85 }
86 if !strings.Contains(libfooCov.Args["linkFlags"], flag) {
87 t.Fatalf(missingErrorStr, flag, "libfoo_cov dylib", libfooCov.Args["linkFlags"])
88 }
89 if strings.Contains(buzzNoCov.Args["linkFlags"], flag) {
90 t.Fatalf(containsErrorStr, flag, "buzzNoCov", buzzNoCov.Args["linkFlags"])
91 }
92 if strings.Contains(libbarNoCov.Args["linkFlags"], flag) {
93 t.Fatalf(containsErrorStr, flag, "libbar_cov", libbarNoCov.Args["linkFlags"])
94 }
95 }
96
97}
98
99// Test coverage files are included correctly
100func TestCoverageZip(t *testing.T) {
101 ctx := testRustCov(t, `
102 rust_library {
103 name: "libfoo",
104 srcs: ["foo.rs"],
105 rlibs: ["librlib"],
106 crate_name: "foo",
107 }
Matthew Maurer2ae05132020-06-23 14:28:53 -0700108 rust_ffi_static {
109 name: "libbaz",
110 srcs: ["foo.rs"],
111 rlibs: ["librlib"],
112 crate_name: "baz",
113 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400114 rust_library_rlib {
115 name: "librlib",
116 srcs: ["foo.rs"],
117 crate_name: "rlib",
118 }
119 rust_binary {
120 name: "fizz",
121 rlibs: ["librlib"],
Matthew Maurer2ae05132020-06-23 14:28:53 -0700122 static_libs: ["libbaz"],
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400123 srcs: ["foo.rs"],
124 }
125 cc_binary {
126 name: "buzz",
Matthew Maurer2ae05132020-06-23 14:28:53 -0700127 static_libs: ["libbaz"],
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400128 srcs: ["foo.c"],
129 }
130 cc_library {
131 name: "libbar",
Matthew Maurer2ae05132020-06-23 14:28:53 -0700132 static_libs: ["libbaz"],
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400133 compile_multilib: "64",
134 srcs: ["foo.c"],
135 }`)
136
137 fizzZipInputs := ctx.ModuleForTests("fizz", "android_arm64_armv8-a_cov").Rule("zip").Inputs.Strings()
138 libfooZipInputs := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib_cov").Rule("zip").Inputs.Strings()
139 buzzZipInputs := ctx.ModuleForTests("buzz", "android_arm64_armv8-a_cov").Rule("zip").Inputs.Strings()
140 libbarZipInputs := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared_cov").Rule("zip").Inputs.Strings()
141
142 // Make sure the expected number of input files are included.
143 if len(fizzZipInputs) != 3 {
144 t.Fatalf("expected only 3 coverage inputs for rust 'fizz' binary, got %#v: %#v", len(fizzZipInputs), fizzZipInputs)
145 }
146 if len(libfooZipInputs) != 2 {
147 t.Fatalf("expected only 2 coverage inputs for rust 'libfoo' library, got %#v: %#v", len(libfooZipInputs), libfooZipInputs)
148 }
149 if len(buzzZipInputs) != 2 {
150 t.Fatalf("expected only 2 coverage inputs for cc 'buzz' binary, got %#v: %#v", len(buzzZipInputs), buzzZipInputs)
151 }
152 if len(libbarZipInputs) != 2 {
153 t.Fatalf("expected only 2 coverage inputs for cc 'libbar' library, got %#v: %#v", len(libbarZipInputs), libbarZipInputs)
154 }
155
156 // Make sure the expected inputs are provided to the zip rule.
Ivan Lozano2b081132020-09-08 12:46:52 -0400157 if !android.SuffixInList(fizzZipInputs, "android_arm64_armv8-a_rlib_dylib-std_cov/librlib.gcno") ||
Matthew Maurer2ae05132020-06-23 14:28:53 -0700158 !android.SuffixInList(fizzZipInputs, "android_arm64_armv8-a_static_cov/libbaz.gcno") ||
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400159 !android.SuffixInList(fizzZipInputs, "android_arm64_armv8-a_cov/fizz.gcno") {
160 t.Fatalf("missing expected coverage files for rust 'fizz' binary: %#v", fizzZipInputs)
161 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400162 if !android.SuffixInList(libfooZipInputs, "android_arm64_armv8-a_rlib_dylib-std_cov/librlib.gcno") ||
Ivan Lozanod7ddf052020-11-16 15:21:43 -0500163 !android.SuffixInList(libfooZipInputs, "android_arm64_armv8-a_dylib_cov/libfoo.gcno") {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400164 t.Fatalf("missing expected coverage files for rust 'fizz' binary: %#v", libfooZipInputs)
165 }
166 if !android.SuffixInList(buzzZipInputs, "android_arm64_armv8-a_cov/obj/foo.gcno") ||
Matthew Maurer2ae05132020-06-23 14:28:53 -0700167 !android.SuffixInList(buzzZipInputs, "android_arm64_armv8-a_static_cov/libbaz.gcno") {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400168 t.Fatalf("missing expected coverage files for cc 'buzz' binary: %#v", buzzZipInputs)
169 }
170 if !android.SuffixInList(libbarZipInputs, "android_arm64_armv8-a_static_cov/obj/foo.gcno") ||
Matthew Maurer2ae05132020-06-23 14:28:53 -0700171 !android.SuffixInList(libbarZipInputs, "android_arm64_armv8-a_static_cov/libbaz.gcno") {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400172 t.Fatalf("missing expected coverage files for cc 'libbar' library: %#v", libbarZipInputs)
173 }
174}
175
176func TestCoverageDeps(t *testing.T) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400177 ctx := testRustCov(t, `
178 rust_binary {
179 name: "fizz",
180 srcs: ["foo.rs"],
181 }`)
182
183 fizz := ctx.ModuleForTests("fizz", "android_arm64_armv8-a_cov").Rule("rustc")
184 if !strings.Contains(fizz.Args["linkFlags"], "libprofile-extras.a") {
185 t.Fatalf("missing expected coverage 'libprofile-extras' dependency in linkFlags: %#v", fizz.Args["linkFlags"])
186 }
187}