1// Copyright 2017 Google Inc. All rights reserved. 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 build 16 17import ( 18 "bufio" 19 "fmt" 20 "path/filepath" 21 "runtime" 22 "sort" 23 "strings" 24 25 "android/soong/ui/metrics" 26 "android/soong/ui/status" 27) 28 29// Checks for files in the out directory that have a rule that depends on them but no rule to 30// create them. This catches a common set of build failures where a rule to generate a file is 31// deleted (either by deleting a module in an Android.mk file, or by modifying the build system 32// incorrectly). These failures are often not caught by a local incremental build because the 33// previously built files are still present in the output directory. 34func testForDanglingRules(ctx Context, config Config) { 35 // Many modules are disabled on mac. Checking for dangling rules would cause lots of build 36 // breakages, and presubmit wouldn't catch them, so just disable the check. 37 if runtime.GOOS != "linux" { 38 return 39 } 40 41 ctx.BeginTrace(metrics.TestRun, "test for dangling rules") 42 defer ctx.EndTrace() 43 44 ts := ctx.Status.StartTool() 45 action := &status.Action{ 46 Description: "Test for dangling rules", 47 } 48 ts.StartAction(action) 49 50 // Get a list of leaf nodes in the dependency graph from ninja 51 executable := config.PrebuiltBuildTool("ninja") 52 53 commonArgs := []string{} 54 commonArgs = append(commonArgs, "-f", config.CombinedNinjaFile()) 55 args := append(commonArgs, "-t", "targets", "rule") 56 57 cmd := Command(ctx, config, "ninja", executable, args...) 58 stdout, err := cmd.StdoutPipe() 59 if err != nil { 60 ctx.Fatal(err) 61 } 62 63 cmd.StartOrFatal() 64 65 outDir := config.OutDir() 66 modulePathsDir := filepath.Join(outDir, ".module_paths") 67 variablesFilePath := filepath.Join(outDir, "soong", "soong.variables") 68 69 // dexpreopt.config is an input to the soong_docs action, which runs the 70 // soong_build primary builder. However, this file is created from $(shell) 71 // invocation at Kati parse time, so it's not an explicit output of any 72 // Ninja action, but it is present during the build itself and can be 73 // treated as an source file. 74 dexpreoptConfigFilePath := filepath.Join(outDir, "soong", "dexpreopt.config") 75 76 // out/build_date.txt is considered a "source file" 77 buildDatetimeFilePath := filepath.Join(outDir, "build_date.txt") 78 79 // bpglob is built explicitly using Microfactory 80 bpglob := filepath.Join(config.SoongOutDir(), "bpglob") 81 82 danglingRules := make(map[string]bool) 83 84 scanner := bufio.NewScanner(stdout) 85 for scanner.Scan() { 86 line := scanner.Text() 87 if !strings.HasPrefix(line, outDir) { 88 // Leaf node is not in the out directory. 89 continue 90 } 91 if strings.HasPrefix(line, modulePathsDir) || 92 line == variablesFilePath || 93 line == dexpreoptConfigFilePath || 94 line == buildDatetimeFilePath || 95 line == bpglob { 96 // Leaf node is in one of Soong's bootstrap directories, which do not have 97 // full build rules in the primary build.ninja file. 98 continue 99 } 100 danglingRules[line] = true 101 } 102 103 cmd.WaitOrFatal() 104 105 var danglingRulesList []string 106 for rule := range danglingRules { 107 danglingRulesList = append(danglingRulesList, rule) 108 } 109 sort.Strings(danglingRulesList) 110 111 if len(danglingRulesList) > 0 { 112 sb := &strings.Builder{} 113 title := "Dependencies in out found with no rule to create them:" 114 fmt.Fprintln(sb, title) 115 116 reportLines := 1 117 for i, dep := range danglingRulesList { 118 if reportLines > 20 { 119 fmt.Fprintf(sb, " ... and %d more\n", len(danglingRulesList)-i) 120 break 121 } 122 // It's helpful to see the reverse dependencies. ninja -t query is the 123 // best tool we got for that. Its output starts with the dependency 124 // itself. 125 queryCmd := Command(ctx, config, "ninja", executable, 126 append(commonArgs, "-t", "query", dep)...) 127 queryStdout, err := queryCmd.StdoutPipe() 128 if err != nil { 129 ctx.Fatal(err) 130 } 131 queryCmd.StartOrFatal() 132 scanner := bufio.NewScanner(queryStdout) 133 for scanner.Scan() { 134 reportLines++ 135 fmt.Fprintln(sb, " ", scanner.Text()) 136 } 137 queryCmd.WaitOrFatal() 138 } 139 140 ts.FinishAction(status.ActionResult{ 141 Action: action, 142 Error: fmt.Errorf(title), 143 Output: sb.String(), 144 }) 145 ctx.Fatal("stopping") 146 } 147 ts.FinishAction(status.ActionResult{Action: action}) 148} 149