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	common_args := []string{}
54	common_args = append(common_args, config.NinjaArgs()...)
55	common_args = append(common_args, "-f", config.CombinedNinjaFile())
56	args := append(common_args, "-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	bootstrapDir := filepath.Join(outDir, "soong", ".bootstrap")
68	miniBootstrapDir := filepath.Join(outDir, "soong", ".minibootstrap")
69	modulePathsDir := filepath.Join(outDir, ".module_paths")
70	variablesFilePath := filepath.Join(outDir, "soong", "soong.variables")
71
72	danglingRules := make(map[string]bool)
73
74	scanner := bufio.NewScanner(stdout)
75	for scanner.Scan() {
76		line := scanner.Text()
77		if !strings.HasPrefix(line, outDir) {
78			// Leaf node is not in the out directory.
79			continue
80		}
81		if strings.HasPrefix(line, bootstrapDir) ||
82			strings.HasPrefix(line, miniBootstrapDir) ||
83			strings.HasPrefix(line, modulePathsDir) ||
84			line == variablesFilePath {
85			// Leaf node is in one of Soong's bootstrap directories, which do not have
86			// full build rules in the primary build.ninja file.
87			continue
88		}
89		danglingRules[line] = true
90	}
91
92	cmd.WaitOrFatal()
93
94	var danglingRulesList []string
95	for rule := range danglingRules {
96		danglingRulesList = append(danglingRulesList, rule)
97	}
98	sort.Strings(danglingRulesList)
99
100	if len(danglingRulesList) > 0 {
101		sb := &strings.Builder{}
102		title := "Dependencies in out found with no rule to create them:"
103		fmt.Fprintln(sb, title)
104
105		report_lines := 1
106		for i, dep := range danglingRulesList {
107			if report_lines > 20 {
108				fmt.Fprintf(sb, "  ... and %d more\n", len(danglingRulesList)-i)
109				break
110			}
111			// It's helpful to see the reverse dependencies. ninja -t query is the
112			// best tool we got for that. Its output starts with the dependency
113			// itself.
114			query_cmd := Command(ctx, config, "ninja", executable,
115				append(common_args, "-t", "query", dep)...)
116			query_stdout, err := query_cmd.StdoutPipe()
117			if err != nil {
118				ctx.Fatal(err)
119			}
120			query_cmd.StartOrFatal()
121			scanner := bufio.NewScanner(query_stdout)
122			for scanner.Scan() {
123				report_lines++
124				fmt.Fprintln(sb, " ", scanner.Text())
125			}
126			query_cmd.WaitOrFatal()
127		}
128
129		ts.FinishAction(status.ActionResult{
130			Action: action,
131			Error:  fmt.Errorf(title),
132			Output: sb.String(),
133		})
134		ctx.Fatal("stopping")
135	}
136	ts.FinishAction(status.ActionResult{Action: action})
137}
138