blob: 5109465e9ee825422df6ffa54372b816728473b2 [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
Nan Zhang17f27672018-12-12 16:01:49 -080025 "android/soong/ui/metrics"
Dan Willemsend2e231a2018-08-02 12:06:24 -070026 "android/soong/ui/status"
Colin Cross37193492017-11-16 17:55:00 -080027)
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
Nan Zhang17f27672018-12-12 16:01:49 -080041 ctx.BeginTrace(metrics.TestRun, "test for dangling rules")
Colin Cross37193492017-11-16 17:55:00 -080042 defer ctx.EndTrace()
43
Dan Willemsend2e231a2018-08-02 12:06:24 -070044 ts := ctx.Status.StartTool()
45 action := &status.Action{
46 Description: "Test for dangling rules",
47 }
48 ts.StartAction(action)
49
Colin Cross37193492017-11-16 17:55:00 -080050 // 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
Colin Cross63b4e0f2018-06-26 23:48:52 -070070 danglingRules := make(map[string]bool)
Colin Cross37193492017-11-16 17:55:00 -080071
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 }
Colin Cross63b4e0f2018-06-26 23:48:52 -070084 danglingRules[line] = true
Colin Cross37193492017-11-16 17:55:00 -080085 }
86
87 cmd.WaitOrFatal()
88
Colin Cross63b4e0f2018-06-26 23:48:52 -070089 var danglingRulesList []string
90 for rule := range danglingRules {
91 danglingRulesList = append(danglingRulesList, rule)
92 }
93 sort.Strings(danglingRulesList)
94
95 if len(danglingRulesList) > 0 {
Dan Willemsend2e231a2018-08-02 12:06:24 -070096 sb := &strings.Builder{}
97 title := "Dependencies in out found with no rule to create them:"
98 fmt.Fprintln(sb, title)
Colin Cross63b4e0f2018-06-26 23:48:52 -070099 for _, dep := range danglingRulesList {
Dan Willemsend2e231a2018-08-02 12:06:24 -0700100 fmt.Fprintln(sb, " ", dep)
Colin Cross37193492017-11-16 17:55:00 -0800101 }
Dan Willemsend2e231a2018-08-02 12:06:24 -0700102 ts.FinishAction(status.ActionResult{
103 Action: action,
104 Error: fmt.Errorf(title),
105 Output: sb.String(),
106 })
Colin Cross63b4e0f2018-06-26 23:48:52 -0700107 ctx.Fatal("stopping")
Colin Cross37193492017-11-16 17:55:00 -0800108 }
Dan Willemsend2e231a2018-08-02 12:06:24 -0700109 ts.FinishAction(status.ActionResult{Action: action})
Colin Cross37193492017-11-16 17:55:00 -0800110}