Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 1 | // 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | "testing" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | ) |
| 23 | |
| 24 | // Test that coverage flags are being correctly generated. |
| 25 | func 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 Nainar | f1f6dd1 | 2022-06-07 11:15:59 -0700 | [diff] [blame] | 59 | rustcCoverageFlags := []string{"-C instrument-coverage", " -g "} |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 60 | 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 Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 64 | if !strings.Contains(fizzCov.Args["rustcFlags"], flag) { |
| 65 | t.Fatalf(missingErrorStr, flag, "fizz_cov", fizzCov.Args["rustcFlags"]) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 66 | } |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 67 | if !strings.Contains(libfooCov.Args["rustcFlags"], flag) { |
| 68 | t.Fatalf(missingErrorStr, flag, "libfoo_cov dylib", libfooCov.Args["rustcFlags"]) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 69 | } |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 70 | if strings.Contains(buzzNoCov.Args["rustcFlags"], flag) { |
| 71 | t.Fatalf(containsErrorStr, flag, "buzzNoCov", buzzNoCov.Args["rustcFlags"]) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 72 | } |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 73 | if strings.Contains(libbarNoCov.Args["rustcFlags"], flag) { |
| 74 | t.Fatalf(containsErrorStr, flag, "libbar_cov", libbarNoCov.Args["rustcFlags"]) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Joel Galenson | fa04938 | 2021-01-14 16:03:18 -0800 | [diff] [blame] | 78 | linkCoverageFlags := []string{"-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw", " -g "} |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 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 | |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 83 | if !strings.Contains(fizzCov.Args["linkFlags"], flag) { |
| 84 | t.Fatalf(missingErrorStr, flag, "fizz_cov", fizzCov.Args["linkFlags"]) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 85 | } |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 86 | if !strings.Contains(libfooCov.Args["linkFlags"], flag) { |
| 87 | t.Fatalf(missingErrorStr, flag, "libfoo_cov dylib", libfooCov.Args["linkFlags"]) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 88 | } |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 89 | if strings.Contains(buzzNoCov.Args["linkFlags"], flag) { |
| 90 | t.Fatalf(containsErrorStr, flag, "buzzNoCov", buzzNoCov.Args["linkFlags"]) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 91 | } |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 92 | if strings.Contains(libbarNoCov.Args["linkFlags"], flag) { |
| 93 | t.Fatalf(containsErrorStr, flag, "libbar_cov", libbarNoCov.Args["linkFlags"]) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 99 | func TestCoverageDeps(t *testing.T) { |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 100 | ctx := testRustCov(t, ` |
| 101 | rust_binary { |
| 102 | name: "fizz", |
| 103 | srcs: ["foo.rs"], |
| 104 | }`) |
| 105 | |
Colin Cross | 004bd3f | 2023-10-02 11:39:17 -0700 | [diff] [blame] | 106 | fizz := ctx.ModuleForTests("fizz", "android_arm64_armv8-a_cov").Rule("rustc") |
Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 107 | 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 Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 109 | } |
| 110 | } |