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|hostname|number).txt is considered a "source file" 80 buildDatetimeFilePath := filepath.Join(outDir, "build_date.txt") 81 buildHostnameFilePath := filepath.Join(outDir, "soong", "build_hostname.txt") 82 buildNumberFilePath := filepath.Join(outDir, "soong", "build_number.txt") 83 84 // release-config files are generated from the initial lunch or Kati phase 85 // before running soong and ninja. 86 releaseConfigDir := filepath.Join(outDir, "soong", "release-config") 87 88 // out/target/product/<xxxxx>/build_fingerprint.txt is a source file created in sysprop.mk 89 // ^out/target/product/[^/]+/build_fingerprint.txt$ 90 buildFingerPrintFilePattern := regexp.MustCompile("^" + filepath.Join(outDir, "target", "product") + "/[^/]+/build_fingerprint.txt$") 91 92 danglingRules := make(map[string]bool) 93 94 scanner := bufio.NewScanner(stdout) 95 for scanner.Scan() { 96 line := scanner.Text() 97 if !strings.HasPrefix(line, outDir) { 98 // Leaf node is not in the out directory. 99 continue 100 } 101 if strings.HasPrefix(line, modulePathsDir) || 102 strings.HasPrefix(line, rawFilesDir) || 103 line == variablesFilePath || 104 line == extraVariablesFilePath || 105 line == dexpreoptConfigFilePath || 106 line == buildDatetimeFilePath || 107 line == buildHostnameFilePath || 108 line == buildNumberFilePath || 109 strings.HasPrefix(line, releaseConfigDir) || 110 buildFingerPrintFilePattern.MatchString(line) { 111 // Leaf node is in one of Soong's bootstrap directories, which do not have 112 // full build rules in the primary build.ninja file. 113 continue 114 } 115 116 danglingRules[line] = true 117 } 118 119 cmd.WaitOrFatal() 120 121 var danglingRulesList []string 122 for rule := range danglingRules { 123 danglingRulesList = append(danglingRulesList, rule) 124 } 125 sort.Strings(danglingRulesList) 126 127 if len(danglingRulesList) > 0 { 128 sb := &strings.Builder{} 129 title := "Dependencies in out found with no rule to create them:" 130 fmt.Fprintln(sb, title) 131 132 reportLines := 1 133 for i, dep := range danglingRulesList { 134 if reportLines > 20 { 135 fmt.Fprintf(sb, " ... and %d more\n", len(danglingRulesList)-i) 136 break 137 } 138 // It's helpful to see the reverse dependencies. ninja -t query is the 139 // best tool we got for that. Its output starts with the dependency 140 // itself. 141 queryCmd := Command(ctx, config, "ninja", executable, 142 append(commonArgs, "-t", "query", dep)...) 143 queryStdout, err := queryCmd.StdoutPipe() 144 if err != nil { 145 ctx.Fatal(err) 146 } 147 queryCmd.StartOrFatal() 148 scanner := bufio.NewScanner(queryStdout) 149 for scanner.Scan() { 150 reportLines++ 151 fmt.Fprintln(sb, " ", scanner.Text()) 152 } 153 queryCmd.WaitOrFatal() 154 } 155 156 ts.FinishAction(status.ActionResult{ 157 Action: action, 158 Error: fmt.Errorf("%s", title), 159 Output: sb.String(), 160 }) 161 ctx.Fatal("stopping") 162 } 163 ts.FinishAction(status.ActionResult{Action: action}) 164} 165