blob: 687ad6f6183a84452001356607ba5d9cabac1fb6 [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 (
Colin Cross8d411ff2023-12-07 10:31:24 -080018 "android/soong/ui/metrics"
19 "android/soong/ui/status"
Colin Cross37193492017-11-16 17:55:00 -080020 "bufio"
Dan Willemsend2e231a2018-08-02 12:06:24 -070021 "fmt"
Colin Cross37193492017-11-16 17:55:00 -080022 "path/filepath"
23 "runtime"
Colin Cross63b4e0f2018-06-26 23:48:52 -070024 "sort"
Colin Cross37193492017-11-16 17:55:00 -080025 "strings"
26)
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
Nan Zhang17f27672018-12-12 16:01:49 -080040 ctx.BeginTrace(metrics.TestRun, "test for dangling rules")
Colin Cross37193492017-11-16 17:55:00 -080041 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
Jaewoong Jung18aefc12020-12-21 09:11:10 -080052 commonArgs := []string{}
Jaewoong Jung18aefc12020-12-21 09:11:10 -080053 commonArgs = append(commonArgs, "-f", config.CombinedNinjaFile())
54 args := append(commonArgs, "-t", "targets", "rule")
Colin Cross37193492017-11-16 17:55:00 -080055
56 cmd := Command(ctx, config, "ninja", executable, args...)
57 stdout, err := cmd.StdoutPipe()
58 if err != nil {
59 ctx.Fatal(err)
60 }
61
62 cmd.StartOrFatal()
63
64 outDir := config.OutDir()
Chris Parsons8f232a22020-06-23 17:37:05 -040065 modulePathsDir := filepath.Join(outDir, ".module_paths")
Colin Cross31a67452023-11-02 16:57:08 -070066 rawFilesDir := filepath.Join(outDir, "soong", "raw")
Inseob Kim58c802f2024-06-11 10:59:00 +090067 variablesFilePath := config.SoongVarsFile()
68 extraVariablesFilePath := config.SoongExtraVarsFile()
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
Cole Fauste842a6c2023-10-13 02:51:08 +000077 // out/build_date.txt is considered a "source file"
Rupert Shuttleworth0bc9a9a2020-12-08 13:28:38 +000078 buildDatetimeFilePath := filepath.Join(outDir, "build_date.txt")
79
Lukacs T. Berki5a67da72021-11-10 12:32:22 +010080 // bpglob is built explicitly using Microfactory
81 bpglob := filepath.Join(config.SoongOutDir(), "bpglob")
82
Justin Yun605b5da2024-06-04 10:35:19 +090083 // release-config files are generated from the initial lunch or Kati phase
84 // before running soong and ninja.
85 releaseConfigDir := filepath.Join(outDir, "soong", "release-config")
86
Colin Cross63b4e0f2018-06-26 23:48:52 -070087 danglingRules := make(map[string]bool)
Colin Cross37193492017-11-16 17:55:00 -080088
89 scanner := bufio.NewScanner(stdout)
90 for scanner.Scan() {
91 line := scanner.Text()
92 if !strings.HasPrefix(line, outDir) {
93 // Leaf node is not in the out directory.
94 continue
95 }
Lukacs T. Berki90b43342021-11-02 14:42:04 +010096 if strings.HasPrefix(line, modulePathsDir) ||
Colin Cross31a67452023-11-02 16:57:08 -070097 strings.HasPrefix(line, rawFilesDir) ||
Jingwen Chenebb0b572020-11-02 00:24:57 -050098 line == variablesFilePath ||
Inseob Kim58c802f2024-06-11 10:59:00 +090099 line == extraVariablesFilePath ||
Rupert Shuttleworth0bc9a9a2020-12-08 13:28:38 +0000100 line == dexpreoptConfigFilePath ||
Lukacs T. Berki5a67da72021-11-10 12:32:22 +0100101 line == buildDatetimeFilePath ||
Justin Yun605b5da2024-06-04 10:35:19 +0900102 line == bpglob ||
103 strings.HasPrefix(line, releaseConfigDir) {
Colin Cross37193492017-11-16 17:55:00 -0800104 // Leaf node is in one of Soong's bootstrap directories, which do not have
105 // full build rules in the primary build.ninja file.
106 continue
107 }
Liz Kammer2af5ea82022-11-11 14:21:03 -0500108
Colin Cross63b4e0f2018-06-26 23:48:52 -0700109 danglingRules[line] = true
Colin Cross37193492017-11-16 17:55:00 -0800110 }
111
112 cmd.WaitOrFatal()
113
Colin Cross63b4e0f2018-06-26 23:48:52 -0700114 var danglingRulesList []string
115 for rule := range danglingRules {
116 danglingRulesList = append(danglingRulesList, rule)
117 }
118 sort.Strings(danglingRulesList)
119
120 if len(danglingRulesList) > 0 {
Dan Willemsend2e231a2018-08-02 12:06:24 -0700121 sb := &strings.Builder{}
122 title := "Dependencies in out found with no rule to create them:"
123 fmt.Fprintln(sb, title)
Martin Stjernholm946fb672020-04-15 23:23:34 +0100124
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800125 reportLines := 1
Martin Stjernholm946fb672020-04-15 23:23:34 +0100126 for i, dep := range danglingRulesList {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800127 if reportLines > 20 {
Martin Stjernholm946fb672020-04-15 23:23:34 +0100128 fmt.Fprintf(sb, " ... and %d more\n", len(danglingRulesList)-i)
129 break
130 }
131 // It's helpful to see the reverse dependencies. ninja -t query is the
132 // best tool we got for that. Its output starts with the dependency
133 // itself.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800134 queryCmd := Command(ctx, config, "ninja", executable,
135 append(commonArgs, "-t", "query", dep)...)
136 queryStdout, err := queryCmd.StdoutPipe()
Martin Stjernholm946fb672020-04-15 23:23:34 +0100137 if err != nil {
138 ctx.Fatal(err)
139 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800140 queryCmd.StartOrFatal()
141 scanner := bufio.NewScanner(queryStdout)
Martin Stjernholm946fb672020-04-15 23:23:34 +0100142 for scanner.Scan() {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800143 reportLines++
Martin Stjernholm946fb672020-04-15 23:23:34 +0100144 fmt.Fprintln(sb, " ", scanner.Text())
145 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800146 queryCmd.WaitOrFatal()
Colin Cross37193492017-11-16 17:55:00 -0800147 }
Martin Stjernholm946fb672020-04-15 23:23:34 +0100148
Dan Willemsend2e231a2018-08-02 12:06:24 -0700149 ts.FinishAction(status.ActionResult{
150 Action: action,
151 Error: fmt.Errorf(title),
152 Output: sb.String(),
153 })
Colin Cross63b4e0f2018-06-26 23:48:52 -0700154 ctx.Fatal("stopping")
Colin Cross37193492017-11-16 17:55:00 -0800155 }
Dan Willemsend2e231a2018-08-02 12:06:24 -0700156 ts.FinishAction(status.ActionResult{Action: action})
Colin Cross37193492017-11-16 17:55:00 -0800157}