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