blob: 348d41ff79a50b7cc22a1bdbe4565fc93000a2e4 [file] [log] [blame]
Colin Cross37193492017-11-16 17:55:00 -08001// 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"
Dan Willemsend2e231a2018-08-02 12:06:24 -070019 "fmt"
Colin Cross37193492017-11-16 17:55:00 -080020 "path/filepath"
21 "runtime"
Colin Cross63b4e0f2018-06-26 23:48:52 -070022 "sort"
Colin Cross37193492017-11-16 17:55:00 -080023 "strings"
Dan Willemsend2e231a2018-08-02 12:06:24 -070024
25 "android/soong/ui/status"
Colin Cross37193492017-11-16 17:55:00 -080026)
27
28// Checks for files in the out directory that have a rule that depends on them but no rule to
29// create them. This catches a common set of build failures where a rule to generate a file is
30// deleted (either by deleting a module in an Android.mk file, or by modifying the build system
31// incorrectly). These failures are often not caught by a local incremental build because the
32// previously built files are still present in the output directory.
33func testForDanglingRules(ctx Context, config Config) {
34 // Many modules are disabled on mac. Checking for dangling rules would cause lots of build
35 // breakages, and presubmit wouldn't catch them, so just disable the check.
36 if runtime.GOOS != "linux" {
37 return
38 }
39
40 ctx.BeginTrace("test for dangling rules")
41 defer ctx.EndTrace()
42
Dan Willemsend2e231a2018-08-02 12:06:24 -070043 ts := ctx.Status.StartTool()
44 action := &status.Action{
45 Description: "Test for dangling rules",
46 }
47 ts.StartAction(action)
48
Colin Cross37193492017-11-16 17:55:00 -080049 // Get a list of leaf nodes in the dependency graph from ninja
50 executable := config.PrebuiltBuildTool("ninja")
51
52 args := []string{}
53 args = append(args, config.NinjaArgs()...)
54 args = append(args, "-f", config.CombinedNinjaFile())
55 args = append(args, "-t", "targets", "rule")
56
57 cmd := Command(ctx, config, "ninja", executable, args...)
58 stdout, err := cmd.StdoutPipe()
59 if err != nil {
60 ctx.Fatal(err)
61 }
62
63 cmd.StartOrFatal()
64
65 outDir := config.OutDir()
66 bootstrapDir := filepath.Join(outDir, "soong", ".bootstrap")
67 miniBootstrapDir := filepath.Join(outDir, "soong", ".minibootstrap")
68
Colin Cross63b4e0f2018-06-26 23:48:52 -070069 danglingRules := make(map[string]bool)
Colin Cross37193492017-11-16 17:55:00 -080070
71 scanner := bufio.NewScanner(stdout)
72 for scanner.Scan() {
73 line := scanner.Text()
74 if !strings.HasPrefix(line, outDir) {
75 // Leaf node is not in the out directory.
76 continue
77 }
78 if strings.HasPrefix(line, bootstrapDir) || strings.HasPrefix(line, miniBootstrapDir) {
79 // Leaf node is in one of Soong's bootstrap directories, which do not have
80 // full build rules in the primary build.ninja file.
81 continue
82 }
Colin Cross63b4e0f2018-06-26 23:48:52 -070083 danglingRules[line] = true
Colin Cross37193492017-11-16 17:55:00 -080084 }
85
86 cmd.WaitOrFatal()
87
Colin Cross63b4e0f2018-06-26 23:48:52 -070088 var danglingRulesList []string
89 for rule := range danglingRules {
90 danglingRulesList = append(danglingRulesList, rule)
91 }
92 sort.Strings(danglingRulesList)
93
94 if len(danglingRulesList) > 0 {
Dan Willemsend2e231a2018-08-02 12:06:24 -070095 sb := &strings.Builder{}
96 title := "Dependencies in out found with no rule to create them:"
97 fmt.Fprintln(sb, title)
Colin Cross63b4e0f2018-06-26 23:48:52 -070098 for _, dep := range danglingRulesList {
Dan Willemsend2e231a2018-08-02 12:06:24 -070099 fmt.Fprintln(sb, " ", dep)
Colin Cross37193492017-11-16 17:55:00 -0800100 }
Dan Willemsend2e231a2018-08-02 12:06:24 -0700101 ts.FinishAction(status.ActionResult{
102 Action: action,
103 Error: fmt.Errorf(title),
104 Output: sb.String(),
105 })
Colin Cross63b4e0f2018-06-26 23:48:52 -0700106 ctx.Fatal("stopping")
Colin Cross37193492017-11-16 17:55:00 -0800107 }
Dan Willemsend2e231a2018-08-02 12:06:24 -0700108 ts.FinishAction(status.ActionResult{Action: action})
Colin Cross37193492017-11-16 17:55:00 -0800109}