blob: f9a60b604f10be53624511997d67eddf20abe25f [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
Jaewoong Jung18aefc12020-12-21 09:11:10 -080053 commonArgs := []string{}
Jaewoong Jung18aefc12020-12-21 09:11:10 -080054 commonArgs = append(commonArgs, "-f", config.CombinedNinjaFile())
55 args := append(commonArgs, "-t", "targets", "rule")
Colin Cross37193492017-11-16 17:55:00 -080056
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")
Chris Parsons8f232a22020-06-23 17:37:05 -040067 modulePathsDir := filepath.Join(outDir, ".module_paths")
68 variablesFilePath := filepath.Join(outDir, "soong", "soong.variables")
Rupert Shuttleworth0bc9a9a2020-12-08 13:28:38 +000069
Jingwen Chenebb0b572020-11-02 00:24:57 -050070 // dexpreopt.config is an input to the soong_docs action, which runs the
71 // soong_build primary builder. However, this file is created from $(shell)
72 // invocation at Kati parse time, so it's not an explicit output of any
73 // Ninja action, but it is present during the build itself and can be
74 // treated as an source file.
75 dexpreoptConfigFilePath := filepath.Join(outDir, "soong", "dexpreopt.config")
Colin Cross37193492017-11-16 17:55:00 -080076
Rupert Shuttleworth0bc9a9a2020-12-08 13:28:38 +000077 // out/build_date.txt is considered a "source file"
78 buildDatetimeFilePath := filepath.Join(outDir, "build_date.txt")
79
Colin Cross63b4e0f2018-06-26 23:48:52 -070080 danglingRules := make(map[string]bool)
Colin Cross37193492017-11-16 17:55:00 -080081
82 scanner := bufio.NewScanner(stdout)
83 for scanner.Scan() {
84 line := scanner.Text()
85 if !strings.HasPrefix(line, outDir) {
86 // Leaf node is not in the out directory.
87 continue
88 }
Chris Parsons8f232a22020-06-23 17:37:05 -040089 if strings.HasPrefix(line, bootstrapDir) ||
Chris Parsons8f232a22020-06-23 17:37:05 -040090 strings.HasPrefix(line, modulePathsDir) ||
Jingwen Chenebb0b572020-11-02 00:24:57 -050091 line == variablesFilePath ||
Rupert Shuttleworth0bc9a9a2020-12-08 13:28:38 +000092 line == dexpreoptConfigFilePath ||
93 line == buildDatetimeFilePath {
Colin Cross37193492017-11-16 17:55:00 -080094 // Leaf node is in one of Soong's bootstrap directories, which do not have
95 // full build rules in the primary build.ninja file.
96 continue
97 }
Colin Cross63b4e0f2018-06-26 23:48:52 -070098 danglingRules[line] = true
Colin Cross37193492017-11-16 17:55:00 -080099 }
100
101 cmd.WaitOrFatal()
102
Colin Cross63b4e0f2018-06-26 23:48:52 -0700103 var danglingRulesList []string
104 for rule := range danglingRules {
105 danglingRulesList = append(danglingRulesList, rule)
106 }
107 sort.Strings(danglingRulesList)
108
109 if len(danglingRulesList) > 0 {
Dan Willemsend2e231a2018-08-02 12:06:24 -0700110 sb := &strings.Builder{}
111 title := "Dependencies in out found with no rule to create them:"
112 fmt.Fprintln(sb, title)
Martin Stjernholm946fb672020-04-15 23:23:34 +0100113
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800114 reportLines := 1
Martin Stjernholm946fb672020-04-15 23:23:34 +0100115 for i, dep := range danglingRulesList {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800116 if reportLines > 20 {
Martin Stjernholm946fb672020-04-15 23:23:34 +0100117 fmt.Fprintf(sb, " ... and %d more\n", len(danglingRulesList)-i)
118 break
119 }
120 // It's helpful to see the reverse dependencies. ninja -t query is the
121 // best tool we got for that. Its output starts with the dependency
122 // itself.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800123 queryCmd := Command(ctx, config, "ninja", executable,
124 append(commonArgs, "-t", "query", dep)...)
125 queryStdout, err := queryCmd.StdoutPipe()
Martin Stjernholm946fb672020-04-15 23:23:34 +0100126 if err != nil {
127 ctx.Fatal(err)
128 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800129 queryCmd.StartOrFatal()
130 scanner := bufio.NewScanner(queryStdout)
Martin Stjernholm946fb672020-04-15 23:23:34 +0100131 for scanner.Scan() {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800132 reportLines++
Martin Stjernholm946fb672020-04-15 23:23:34 +0100133 fmt.Fprintln(sb, " ", scanner.Text())
134 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800135 queryCmd.WaitOrFatal()
Colin Cross37193492017-11-16 17:55:00 -0800136 }
Martin Stjernholm946fb672020-04-15 23:23:34 +0100137
Dan Willemsend2e231a2018-08-02 12:06:24 -0700138 ts.FinishAction(status.ActionResult{
139 Action: action,
140 Error: fmt.Errorf(title),
141 Output: sb.String(),
142 })
Colin Cross63b4e0f2018-06-26 23:48:52 -0700143 ctx.Fatal("stopping")
Colin Cross37193492017-11-16 17:55:00 -0800144 }
Dan Willemsend2e231a2018-08-02 12:06:24 -0700145 ts.FinishAction(status.ActionResult{Action: action})
Colin Cross37193492017-11-16 17:55:00 -0800146}