• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	args := []string{}
54	args = append(args, config.NinjaArgs()...)
55	args = append(args, "-f", config.CombinedNinjaFile())
56	args = append(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
70	danglingRules := make(map[string]bool)
71
72	scanner := bufio.NewScanner(stdout)
73	for scanner.Scan() {
74		line := scanner.Text()
75		if !strings.HasPrefix(line, outDir) {
76			// Leaf node is not in the out directory.
77			continue
78		}
79		if strings.HasPrefix(line, bootstrapDir) || strings.HasPrefix(line, miniBootstrapDir) {
80			// Leaf node is in one of Soong's bootstrap directories, which do not have
81			// full build rules in the primary build.ninja file.
82			continue
83		}
84		danglingRules[line] = true
85	}
86
87	cmd.WaitOrFatal()
88
89	var danglingRulesList []string
90	for rule := range danglingRules {
91		danglingRulesList = append(danglingRulesList, rule)
92	}
93	sort.Strings(danglingRulesList)
94
95	if len(danglingRulesList) > 0 {
96		sb := &strings.Builder{}
97		title := "Dependencies in out found with no rule to create them:"
98		fmt.Fprintln(sb, title)
99		for _, dep := range danglingRulesList {
100			fmt.Fprintln(sb, "  ", dep)
101		}
102		ts.FinishAction(status.ActionResult{
103			Action: action,
104			Error:  fmt.Errorf(title),
105			Output: sb.String(),
106		})
107		ctx.Fatal("stopping")
108	}
109	ts.FinishAction(status.ActionResult{Action: action})
110}
111