blob: 3faa94d953e2e3372dff5ba6d28718ed1c5139d2 [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"
Wei Lib85a1782024-02-05 14:50:54 -080021 "regexp"
Colin Cross37193492017-11-16 17:55:00 -080022 "runtime"
Colin Cross63b4e0f2018-06-26 23:48:52 -070023 "sort"
Colin Cross37193492017-11-16 17:55:00 -080024 "strings"
Wei Lib85a1782024-02-05 14:50:54 -080025
26 "android/soong/ui/metrics"
27 "android/soong/ui/status"
Colin Cross37193492017-11-16 17:55:00 -080028)
29
30// Checks for files in the out directory that have a rule that depends on them but no rule to
31// create them. This catches a common set of build failures where a rule to generate a file is
32// deleted (either by deleting a module in an Android.mk file, or by modifying the build system
33// incorrectly). These failures are often not caught by a local incremental build because the
34// previously built files are still present in the output directory.
35func testForDanglingRules(ctx Context, config Config) {
36 // Many modules are disabled on mac. Checking for dangling rules would cause lots of build
37 // breakages, and presubmit wouldn't catch them, so just disable the check.
38 if runtime.GOOS != "linux" {
39 return
40 }
41
Nan Zhang17f27672018-12-12 16:01:49 -080042 ctx.BeginTrace(metrics.TestRun, "test for dangling rules")
Colin Cross37193492017-11-16 17:55:00 -080043 defer ctx.EndTrace()
44
Dan Willemsend2e231a2018-08-02 12:06:24 -070045 ts := ctx.Status.StartTool()
46 action := &status.Action{
47 Description: "Test for dangling rules",
48 }
49 ts.StartAction(action)
50
Colin Cross37193492017-11-16 17:55:00 -080051 // Get a list of leaf nodes in the dependency graph from ninja
52 executable := config.PrebuiltBuildTool("ninja")
53
Jaewoong Jung18aefc12020-12-21 09:11:10 -080054 commonArgs := []string{}
Jaewoong Jung18aefc12020-12-21 09:11:10 -080055 commonArgs = append(commonArgs, "-f", config.CombinedNinjaFile())
56 args := append(commonArgs, "-t", "targets", "rule")
Colin Cross37193492017-11-16 17:55:00 -080057
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()
Chris Parsons8f232a22020-06-23 17:37:05 -040067 modulePathsDir := filepath.Join(outDir, ".module_paths")
Colin Cross31a67452023-11-02 16:57:08 -070068 rawFilesDir := filepath.Join(outDir, "soong", "raw")
Inseob Kim58c802f2024-06-11 10:59:00 +090069 variablesFilePath := config.SoongVarsFile()
70 extraVariablesFilePath := config.SoongExtraVarsFile()
Rupert Shuttleworth0bc9a9a2020-12-08 13:28:38 +000071
Jingwen Chenebb0b572020-11-02 00:24:57 -050072 // dexpreopt.config is an input to the soong_docs action, which runs the
73 // soong_build primary builder. However, this file is created from $(shell)
74 // invocation at Kati parse time, so it's not an explicit output of any
75 // Ninja action, but it is present during the build itself and can be
76 // treated as an source file.
77 dexpreoptConfigFilePath := filepath.Join(outDir, "soong", "dexpreopt.config")
Colin Cross37193492017-11-16 17:55:00 -080078
Cole Fauste842a6c2023-10-13 02:51:08 +000079 // out/build_date.txt is considered a "source file"
Rupert Shuttleworth0bc9a9a2020-12-08 13:28:38 +000080 buildDatetimeFilePath := filepath.Join(outDir, "build_date.txt")
81
Lukacs T. Berki5a67da72021-11-10 12:32:22 +010082 // bpglob is built explicitly using Microfactory
83 bpglob := filepath.Join(config.SoongOutDir(), "bpglob")
84
Justin Yun605b5da2024-06-04 10:35:19 +090085 // release-config files are generated from the initial lunch or Kati phase
86 // before running soong and ninja.
87 releaseConfigDir := filepath.Join(outDir, "soong", "release-config")
88
Wei Lib85a1782024-02-05 14:50:54 -080089 // out/target/product/<xxxxx>/build_fingerprint.txt is a source file created in sysprop.mk
90 // ^out/target/product/[^/]+/build_fingerprint.txt$
91 buildFingerPrintFilePattern := regexp.MustCompile("^" + filepath.Join(outDir, "target", "product") + "/[^/]+/build_fingerprint.txt$")
92
Colin Cross63b4e0f2018-06-26 23:48:52 -070093 danglingRules := make(map[string]bool)
Colin Cross37193492017-11-16 17:55:00 -080094
95 scanner := bufio.NewScanner(stdout)
96 for scanner.Scan() {
97 line := scanner.Text()
98 if !strings.HasPrefix(line, outDir) {
99 // Leaf node is not in the out directory.
100 continue
101 }
Lukacs T. Berki90b43342021-11-02 14:42:04 +0100102 if strings.HasPrefix(line, modulePathsDir) ||
Colin Cross31a67452023-11-02 16:57:08 -0700103 strings.HasPrefix(line, rawFilesDir) ||
Jingwen Chenebb0b572020-11-02 00:24:57 -0500104 line == variablesFilePath ||
Inseob Kim58c802f2024-06-11 10:59:00 +0900105 line == extraVariablesFilePath ||
Rupert Shuttleworth0bc9a9a2020-12-08 13:28:38 +0000106 line == dexpreoptConfigFilePath ||
Lukacs T. Berki5a67da72021-11-10 12:32:22 +0100107 line == buildDatetimeFilePath ||
Justin Yun605b5da2024-06-04 10:35:19 +0900108 line == bpglob ||
Wei Lib85a1782024-02-05 14:50:54 -0800109 strings.HasPrefix(line, releaseConfigDir) ||
110 buildFingerPrintFilePattern.MatchString(line) {
Colin Cross37193492017-11-16 17:55:00 -0800111 // Leaf node is in one of Soong's bootstrap directories, which do not have
112 // full build rules in the primary build.ninja file.
113 continue
114 }
Liz Kammer2af5ea82022-11-11 14:21:03 -0500115
Colin Cross63b4e0f2018-06-26 23:48:52 -0700116 danglingRules[line] = true
Colin Cross37193492017-11-16 17:55:00 -0800117 }
118
119 cmd.WaitOrFatal()
120
Colin Cross63b4e0f2018-06-26 23:48:52 -0700121 var danglingRulesList []string
122 for rule := range danglingRules {
123 danglingRulesList = append(danglingRulesList, rule)
124 }
125 sort.Strings(danglingRulesList)
126
127 if len(danglingRulesList) > 0 {
Dan Willemsend2e231a2018-08-02 12:06:24 -0700128 sb := &strings.Builder{}
129 title := "Dependencies in out found with no rule to create them:"
130 fmt.Fprintln(sb, title)
Martin Stjernholm946fb672020-04-15 23:23:34 +0100131
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800132 reportLines := 1
Martin Stjernholm946fb672020-04-15 23:23:34 +0100133 for i, dep := range danglingRulesList {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800134 if reportLines > 20 {
Martin Stjernholm946fb672020-04-15 23:23:34 +0100135 fmt.Fprintf(sb, " ... and %d more\n", len(danglingRulesList)-i)
136 break
137 }
138 // It's helpful to see the reverse dependencies. ninja -t query is the
139 // best tool we got for that. Its output starts with the dependency
140 // itself.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800141 queryCmd := Command(ctx, config, "ninja", executable,
142 append(commonArgs, "-t", "query", dep)...)
143 queryStdout, err := queryCmd.StdoutPipe()
Martin Stjernholm946fb672020-04-15 23:23:34 +0100144 if err != nil {
145 ctx.Fatal(err)
146 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800147 queryCmd.StartOrFatal()
148 scanner := bufio.NewScanner(queryStdout)
Martin Stjernholm946fb672020-04-15 23:23:34 +0100149 for scanner.Scan() {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800150 reportLines++
Martin Stjernholm946fb672020-04-15 23:23:34 +0100151 fmt.Fprintln(sb, " ", scanner.Text())
152 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800153 queryCmd.WaitOrFatal()
Colin Cross37193492017-11-16 17:55:00 -0800154 }
Martin Stjernholm946fb672020-04-15 23:23:34 +0100155
Dan Willemsend2e231a2018-08-02 12:06:24 -0700156 ts.FinishAction(status.ActionResult{
157 Action: action,
158 Error: fmt.Errorf(title),
159 Output: sb.String(),
160 })
Colin Cross63b4e0f2018-06-26 23:48:52 -0700161 ctx.Fatal("stopping")
Colin Cross37193492017-11-16 17:55:00 -0800162 }
Dan Willemsend2e231a2018-08-02 12:06:24 -0700163 ts.FinishAction(status.ActionResult{Action: action})
Colin Cross37193492017-11-16 17:55:00 -0800164}