blob: d5e244c48665b3c6e8599518a221c350b14c0db4 [file] [log] [blame]
Colin Cross7709a052017-10-06 17:45:07 -07001// 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 "path/filepath"
20 "strings"
21)
22
23// Checks for files in the out directory that have a rule that depends on them but no rule to
24// create them. This catches a common set of build failures where a rule to generate a file is
25// deleted (either by deleting a module in an Android.mk file, or by modifying the build system
26// incorrectly). These failures are often not caught by a local incremental build because the
27// previously built files are still present in the output directory.
28func testForDanglingRules(ctx Context, config Config) {
29 ctx.BeginTrace("test for dangling rules")
30 defer ctx.EndTrace()
31
32 // Get a list of leaf nodes in the dependency graph from ninja
33 executable := config.PrebuiltBuildTool("ninja")
34
35 args := []string{}
36 args = append(args, config.NinjaArgs()...)
37 args = append(args, "-f", config.CombinedNinjaFile())
38 args = append(args, "-t", "targets", "rule")
39
40 cmd := Command(ctx, config, "ninja", executable, args...)
41 stdout, err := cmd.StdoutPipe()
42 if err != nil {
43 ctx.Fatal(err)
44 }
45
46 cmd.StartOrFatal()
47
48 outDir := config.OutDir()
49 bootstrapDir := filepath.Join(outDir, "soong", ".bootstrap")
50 miniBootstrapDir := filepath.Join(outDir, "soong", ".minibootstrap")
51
52 var danglingRules []string
53
54 scanner := bufio.NewScanner(stdout)
55 for scanner.Scan() {
56 line := scanner.Text()
57 if !strings.HasPrefix(line, outDir) {
58 // Leaf node is not in the out directory.
59 continue
60 }
61 if strings.HasPrefix(line, bootstrapDir) || strings.HasPrefix(line, miniBootstrapDir) {
62 // Leaf node is in one of Soong's bootstrap directories, which do not have
63 // full build rules in the primary build.ninja file.
64 continue
65 }
66 danglingRules = append(danglingRules, line)
67 }
68
69 cmd.WaitOrFatal()
70
71 if len(danglingRules) > 0 {
72 ctx.Println("Dependencies in out found with no rule to create them:")
73 for _, dep := range danglingRules {
74 ctx.Println(dep)
75 }
76 ctx.Fatal("")
77 }
78}