Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
| 18 | "bufio" |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame] | 19 | "fmt" |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 20 | "path/filepath" |
Liz Kammer | 2af5ea8 | 2022-11-11 14:21:03 -0500 | [diff] [blame^] | 21 | "regexp" |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 22 | "runtime" |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 23 | "sort" |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 24 | "strings" |
Liz Kammer | 2af5ea8 | 2022-11-11 14:21:03 -0500 | [diff] [blame^] | 25 | "sync" |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame] | 26 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 27 | "android/soong/ui/metrics" |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame] | 28 | "android/soong/ui/status" |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 29 | ) |
| 30 | |
Liz Kammer | 2af5ea8 | 2022-11-11 14:21:03 -0500 | [diff] [blame^] | 31 | var ( |
| 32 | // bazel output paths are in __main__/bazel-out/<config-specific-path>/bin |
| 33 | bazelOutputPathRegexOnce sync.Once |
| 34 | bazelOutputPathRegexp *regexp.Regexp |
| 35 | ) |
| 36 | |
| 37 | func bazelOutputPathPattern(config Config) *regexp.Regexp { |
| 38 | bazelOutputPathRegexOnce.Do(func() { |
| 39 | // Bazel output files are in <Bazel output base>/execroot/__main__/bazel-out/<config>/bin |
| 40 | bazelOutRoot := filepath.Join(regexp.QuoteMeta(config.bazelOutputBase()), "execroot", "__main__", "bazel-out") |
| 41 | bazelOutputPathRegexp = regexp.MustCompile(bazelOutRoot + "/[^/]+/bin") |
| 42 | }) |
| 43 | return bazelOutputPathRegexp |
| 44 | } |
| 45 | |
| 46 | func ignoreBazelPath(config Config, path string) bool { |
| 47 | bazelRoot := filepath.Join(config.bazelOutputBase(), "execroot") |
| 48 | // Don't check bazel output regexp unless it is Bazel path |
| 49 | if strings.HasPrefix(path, bazelRoot) { |
| 50 | bazelOutputRegexp := bazelOutputPathPattern(config) |
| 51 | // if the file is a bazel path that is _not_ a Bazel generated file output, we rely on Bazel to |
| 52 | // ensure the paths to exist. If it _is_ a Bazel output path, we expect that it should be built |
| 53 | // by Ninja. |
| 54 | return !bazelOutputRegexp.MatchString(path) |
| 55 | } |
| 56 | return false |
| 57 | } |
| 58 | |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 59 | // Checks for files in the out directory that have a rule that depends on them but no rule to |
| 60 | // create them. This catches a common set of build failures where a rule to generate a file is |
| 61 | // deleted (either by deleting a module in an Android.mk file, or by modifying the build system |
| 62 | // incorrectly). These failures are often not caught by a local incremental build because the |
| 63 | // previously built files are still present in the output directory. |
| 64 | func testForDanglingRules(ctx Context, config Config) { |
| 65 | // Many modules are disabled on mac. Checking for dangling rules would cause lots of build |
| 66 | // breakages, and presubmit wouldn't catch them, so just disable the check. |
| 67 | if runtime.GOOS != "linux" { |
| 68 | return |
| 69 | } |
| 70 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 71 | ctx.BeginTrace(metrics.TestRun, "test for dangling rules") |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 72 | defer ctx.EndTrace() |
| 73 | |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame] | 74 | ts := ctx.Status.StartTool() |
| 75 | action := &status.Action{ |
| 76 | Description: "Test for dangling rules", |
| 77 | } |
| 78 | ts.StartAction(action) |
| 79 | |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 80 | // Get a list of leaf nodes in the dependency graph from ninja |
| 81 | executable := config.PrebuiltBuildTool("ninja") |
| 82 | |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 83 | commonArgs := []string{} |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 84 | commonArgs = append(commonArgs, "-f", config.CombinedNinjaFile()) |
| 85 | args := append(commonArgs, "-t", "targets", "rule") |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 86 | |
| 87 | cmd := Command(ctx, config, "ninja", executable, args...) |
| 88 | stdout, err := cmd.StdoutPipe() |
| 89 | if err != nil { |
| 90 | ctx.Fatal(err) |
| 91 | } |
| 92 | |
| 93 | cmd.StartOrFatal() |
| 94 | |
| 95 | outDir := config.OutDir() |
Chris Parsons | 8f232a2 | 2020-06-23 17:37:05 -0400 | [diff] [blame] | 96 | modulePathsDir := filepath.Join(outDir, ".module_paths") |
| 97 | variablesFilePath := filepath.Join(outDir, "soong", "soong.variables") |
Rupert Shuttleworth | 0bc9a9a | 2020-12-08 13:28:38 +0000 | [diff] [blame] | 98 | |
Jingwen Chen | ebb0b57 | 2020-11-02 00:24:57 -0500 | [diff] [blame] | 99 | // dexpreopt.config is an input to the soong_docs action, which runs the |
| 100 | // soong_build primary builder. However, this file is created from $(shell) |
| 101 | // invocation at Kati parse time, so it's not an explicit output of any |
| 102 | // Ninja action, but it is present during the build itself and can be |
| 103 | // treated as an source file. |
| 104 | dexpreoptConfigFilePath := filepath.Join(outDir, "soong", "dexpreopt.config") |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 105 | |
Rupert Shuttleworth | 0bc9a9a | 2020-12-08 13:28:38 +0000 | [diff] [blame] | 106 | // out/build_date.txt is considered a "source file" |
| 107 | buildDatetimeFilePath := filepath.Join(outDir, "build_date.txt") |
| 108 | |
Lukacs T. Berki | 5a67da7 | 2021-11-10 12:32:22 +0100 | [diff] [blame] | 109 | // bpglob is built explicitly using Microfactory |
| 110 | bpglob := filepath.Join(config.SoongOutDir(), "bpglob") |
| 111 | |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 112 | danglingRules := make(map[string]bool) |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 113 | |
| 114 | scanner := bufio.NewScanner(stdout) |
| 115 | for scanner.Scan() { |
| 116 | line := scanner.Text() |
| 117 | if !strings.HasPrefix(line, outDir) { |
| 118 | // Leaf node is not in the out directory. |
| 119 | continue |
| 120 | } |
Lukacs T. Berki | 90b4334 | 2021-11-02 14:42:04 +0100 | [diff] [blame] | 121 | if strings.HasPrefix(line, modulePathsDir) || |
Jingwen Chen | ebb0b57 | 2020-11-02 00:24:57 -0500 | [diff] [blame] | 122 | line == variablesFilePath || |
Rupert Shuttleworth | 0bc9a9a | 2020-12-08 13:28:38 +0000 | [diff] [blame] | 123 | line == dexpreoptConfigFilePath || |
Lukacs T. Berki | 5a67da7 | 2021-11-10 12:32:22 +0100 | [diff] [blame] | 124 | line == buildDatetimeFilePath || |
| 125 | line == bpglob { |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 126 | // Leaf node is in one of Soong's bootstrap directories, which do not have |
| 127 | // full build rules in the primary build.ninja file. |
| 128 | continue |
| 129 | } |
Liz Kammer | 2af5ea8 | 2022-11-11 14:21:03 -0500 | [diff] [blame^] | 130 | |
| 131 | if ignoreBazelPath(config, line) { |
| 132 | continue |
| 133 | } |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 134 | danglingRules[line] = true |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | cmd.WaitOrFatal() |
| 138 | |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 139 | var danglingRulesList []string |
| 140 | for rule := range danglingRules { |
| 141 | danglingRulesList = append(danglingRulesList, rule) |
| 142 | } |
| 143 | sort.Strings(danglingRulesList) |
| 144 | |
| 145 | if len(danglingRulesList) > 0 { |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame] | 146 | sb := &strings.Builder{} |
| 147 | title := "Dependencies in out found with no rule to create them:" |
| 148 | fmt.Fprintln(sb, title) |
Martin Stjernholm | 946fb67 | 2020-04-15 23:23:34 +0100 | [diff] [blame] | 149 | |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 150 | reportLines := 1 |
Martin Stjernholm | 946fb67 | 2020-04-15 23:23:34 +0100 | [diff] [blame] | 151 | for i, dep := range danglingRulesList { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 152 | if reportLines > 20 { |
Martin Stjernholm | 946fb67 | 2020-04-15 23:23:34 +0100 | [diff] [blame] | 153 | fmt.Fprintf(sb, " ... and %d more\n", len(danglingRulesList)-i) |
| 154 | break |
| 155 | } |
| 156 | // It's helpful to see the reverse dependencies. ninja -t query is the |
| 157 | // best tool we got for that. Its output starts with the dependency |
| 158 | // itself. |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 159 | queryCmd := Command(ctx, config, "ninja", executable, |
| 160 | append(commonArgs, "-t", "query", dep)...) |
| 161 | queryStdout, err := queryCmd.StdoutPipe() |
Martin Stjernholm | 946fb67 | 2020-04-15 23:23:34 +0100 | [diff] [blame] | 162 | if err != nil { |
| 163 | ctx.Fatal(err) |
| 164 | } |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 165 | queryCmd.StartOrFatal() |
| 166 | scanner := bufio.NewScanner(queryStdout) |
Martin Stjernholm | 946fb67 | 2020-04-15 23:23:34 +0100 | [diff] [blame] | 167 | for scanner.Scan() { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 168 | reportLines++ |
Martin Stjernholm | 946fb67 | 2020-04-15 23:23:34 +0100 | [diff] [blame] | 169 | fmt.Fprintln(sb, " ", scanner.Text()) |
| 170 | } |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 171 | queryCmd.WaitOrFatal() |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 172 | } |
Martin Stjernholm | 946fb67 | 2020-04-15 23:23:34 +0100 | [diff] [blame] | 173 | |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame] | 174 | ts.FinishAction(status.ActionResult{ |
| 175 | Action: action, |
| 176 | Error: fmt.Errorf(title), |
| 177 | Output: sb.String(), |
| 178 | }) |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 179 | ctx.Fatal("stopping") |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 180 | } |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame] | 181 | ts.FinishAction(status.ActionResult{Action: action}) |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 182 | } |