blob: 0f599d745572285759449042d6f46aa27c65d9cf [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
Pirama Arumuga Nainarf1f6dd12022-06-07 11:15:59 -070059 rustcCoverageFlags := []string{"-C instrument-coverage", " -g "}
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
Wen-yi Chu41326c12023-09-22 03:58:59 +000064 if !strings.Contains(fizzCov.Args["rustcFlags"], flag) {
65 t.Fatalf(missingErrorStr, flag, "fizz_cov", fizzCov.Args["rustcFlags"])
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040066 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000067 if !strings.Contains(libfooCov.Args["rustcFlags"], flag) {
68 t.Fatalf(missingErrorStr, flag, "libfoo_cov dylib", libfooCov.Args["rustcFlags"])
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040069 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000070 if strings.Contains(buzzNoCov.Args["rustcFlags"], flag) {
71 t.Fatalf(containsErrorStr, flag, "buzzNoCov", buzzNoCov.Args["rustcFlags"])
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040072 }
Wen-yi Chu41326c12023-09-22 03:58:59 +000073 if strings.Contains(libbarNoCov.Args["rustcFlags"], flag) {
74 t.Fatalf(containsErrorStr, flag, "libbar_cov", libbarNoCov.Args["rustcFlags"])
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040075 }
76 }
77
Joel Galensonfa049382021-01-14 16:03:18 -080078 linkCoverageFlags := []string{"-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw", " -g "}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040079 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
Colin Cross004bd3f2023-10-02 11:39:17 -070083 if !strings.Contains(fizzCov.Args["linkFlags"], flag) {
84 t.Fatalf(missingErrorStr, flag, "fizz_cov", fizzCov.Args["linkFlags"])
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040085 }
Colin Cross004bd3f2023-10-02 11:39:17 -070086 if !strings.Contains(libfooCov.Args["linkFlags"], flag) {
87 t.Fatalf(missingErrorStr, flag, "libfoo_cov dylib", libfooCov.Args["linkFlags"])
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040088 }
Colin Cross004bd3f2023-10-02 11:39:17 -070089 if strings.Contains(buzzNoCov.Args["linkFlags"], flag) {
90 t.Fatalf(containsErrorStr, flag, "buzzNoCov", buzzNoCov.Args["linkFlags"])
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040091 }
Colin Cross004bd3f2023-10-02 11:39:17 -070092 if strings.Contains(libbarNoCov.Args["linkFlags"], flag) {
93 t.Fatalf(containsErrorStr, flag, "libbar_cov", libbarNoCov.Args["linkFlags"])
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040094 }
95 }
96
97}
98
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040099func TestCoverageDeps(t *testing.T) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400100 ctx := testRustCov(t, `
101 rust_binary {
102 name: "fizz",
103 srcs: ["foo.rs"],
104 }`)
105
Colin Cross004bd3f2023-10-02 11:39:17 -0700106 fizz := ctx.ModuleForTests("fizz", "android_arm64_armv8-a_cov").Rule("rustc")
Wen-yi Chu41326c12023-09-22 03:58:59 +0000107 if !strings.Contains(fizz.Args["linkFlags"], "libprofile-clang-extras.a") {
108 t.Fatalf("missing expected coverage 'libprofile-clang-extras' dependency in linkFlags: %#v", fizz.Args["linkFlags"])
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400109 }
110}