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 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 libfooCovLink := ctx.ModuleForTests("libfoo_cov", "android_arm64_armv8-a_dylib_cov").Rule("rustLink") 59 libbarNoCovLink := ctx.ModuleForTests("libbar_nocov", "android_arm64_armv8-a_dylib").Rule("rustLink") 60 fizzCovLink := ctx.ModuleForTests("fizz_cov", "android_arm64_armv8-a_cov").Rule("rustLink") 61 buzzNoCovLink := ctx.ModuleForTests("buzzNoCov", "android_arm64_armv8-a").Rule("rustLink") 62 63 rustcCoverageFlags := []string{"-C instrument-coverage", " -g "} 64 for _, flag := range rustcCoverageFlags { 65 missingErrorStr := "missing rustc flag '%s' for '%s' module with coverage enabled; rustcFlags: %#v" 66 containsErrorStr := "contains rustc flag '%s' for '%s' module with coverage disabled; rustcFlags: %#v" 67 68 if !strings.Contains(fizzCov.Args["rustcFlags"], flag) { 69 t.Fatalf(missingErrorStr, flag, "fizz_cov", fizzCov.Args["rustcFlags"]) 70 } 71 if !strings.Contains(libfooCov.Args["rustcFlags"], flag) { 72 t.Fatalf(missingErrorStr, flag, "libfoo_cov dylib", libfooCov.Args["rustcFlags"]) 73 } 74 if strings.Contains(buzzNoCov.Args["rustcFlags"], flag) { 75 t.Fatalf(containsErrorStr, flag, "buzzNoCov", buzzNoCov.Args["rustcFlags"]) 76 } 77 if strings.Contains(libbarNoCov.Args["rustcFlags"], flag) { 78 t.Fatalf(containsErrorStr, flag, "libbar_cov", libbarNoCov.Args["rustcFlags"]) 79 } 80 } 81 82 linkCoverageFlags := []string{"-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw", " -g "} 83 for _, flag := range linkCoverageFlags { 84 missingErrorStr := "missing rust linker flag '%s' for '%s' module with coverage enabled; rustcFlags: %#v" 85 containsErrorStr := "contains rust linker flag '%s' for '%s' module with coverage disabled; rustcFlags: %#v" 86 87 if !strings.Contains(fizzCovLink.Args["linkFlags"], flag) { 88 t.Fatalf(missingErrorStr, flag, "fizz_cov", fizzCovLink.Args["linkFlags"]) 89 } 90 if !strings.Contains(libfooCovLink.Args["linkFlags"], flag) { 91 t.Fatalf(missingErrorStr, flag, "libfoo_cov dylib", libfooCovLink.Args["linkFlags"]) 92 } 93 if strings.Contains(buzzNoCovLink.Args["linkFlags"], flag) { 94 t.Fatalf(containsErrorStr, flag, "buzzNoCov", buzzNoCovLink.Args["linkFlags"]) 95 } 96 if strings.Contains(libbarNoCovLink.Args["linkFlags"], flag) { 97 t.Fatalf(containsErrorStr, flag, "libbar_cov", libbarNoCovLink.Args["linkFlags"]) 98 } 99 } 100 101} 102 103func TestCoverageDeps(t *testing.T) { 104 ctx := testRustCov(t, ` 105 rust_binary { 106 name: "fizz", 107 srcs: ["foo.rs"], 108 }`) 109 110 fizz := ctx.ModuleForTests("fizz", "android_arm64_armv8-a_cov").Rule("rustLink") 111 if !strings.Contains(fizz.Args["linkFlags"], "libprofile-clang-extras.a") { 112 t.Fatalf("missing expected coverage 'libprofile-clang-extras' dependency in linkFlags: %#v", fizz.Args["linkFlags"]) 113 } 114} 115