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" |
| 21 | "runtime" |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 22 | "sort" |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 23 | "strings" |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame] | 24 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 25 | "android/soong/ui/metrics" |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame] | 26 | "android/soong/ui/status" |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 27 | ) |
| 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. |
| 34 | func 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 Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 41 | ctx.BeginTrace(metrics.TestRun, "test for dangling rules") |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 42 | defer ctx.EndTrace() |
| 43 | |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame] | 44 | ts := ctx.Status.StartTool() |
| 45 | action := &status.Action{ |
| 46 | Description: "Test for dangling rules", |
| 47 | } |
| 48 | ts.StartAction(action) |
| 49 | |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 50 | // Get a list of leaf nodes in the dependency graph from ninja |
| 51 | executable := config.PrebuiltBuildTool("ninja") |
| 52 | |
Martin Stjernholm | 946fb67 | 2020-04-15 23:23:34 +0100 | [diff] [blame] | 53 | common_args := []string{} |
| 54 | common_args = append(common_args, config.NinjaArgs()...) |
| 55 | common_args = append(common_args, "-f", config.CombinedNinjaFile()) |
| 56 | args := append(common_args, "-t", "targets", "rule") |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 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") |
Chris Parsons | 8f232a2 | 2020-06-23 17:37:05 -0400 | [diff] [blame] | 69 | modulePathsDir := filepath.Join(outDir, ".module_paths") |
| 70 | variablesFilePath := filepath.Join(outDir, "soong", "soong.variables") |
Rupert Shuttleworth | 0bc9a9a | 2020-12-08 13:28:38 +0000 | [diff] [blame^] | 71 | |
Jingwen Chen | ebb0b57 | 2020-11-02 00:24:57 -0500 | [diff] [blame] | 72 | // 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 Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 78 | |
Rupert Shuttleworth | 0bc9a9a | 2020-12-08 13:28:38 +0000 | [diff] [blame^] | 79 | // out/build_date.txt is considered a "source file" |
| 80 | buildDatetimeFilePath := filepath.Join(outDir, "build_date.txt") |
| 81 | |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 82 | danglingRules := make(map[string]bool) |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 83 | |
| 84 | scanner := bufio.NewScanner(stdout) |
| 85 | for scanner.Scan() { |
| 86 | line := scanner.Text() |
| 87 | if !strings.HasPrefix(line, outDir) { |
| 88 | // Leaf node is not in the out directory. |
| 89 | continue |
| 90 | } |
Chris Parsons | 8f232a2 | 2020-06-23 17:37:05 -0400 | [diff] [blame] | 91 | if strings.HasPrefix(line, bootstrapDir) || |
| 92 | strings.HasPrefix(line, miniBootstrapDir) || |
| 93 | strings.HasPrefix(line, modulePathsDir) || |
Jingwen Chen | ebb0b57 | 2020-11-02 00:24:57 -0500 | [diff] [blame] | 94 | line == variablesFilePath || |
Rupert Shuttleworth | 0bc9a9a | 2020-12-08 13:28:38 +0000 | [diff] [blame^] | 95 | line == dexpreoptConfigFilePath || |
| 96 | line == buildDatetimeFilePath { |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 97 | // Leaf node is in one of Soong's bootstrap directories, which do not have |
| 98 | // full build rules in the primary build.ninja file. |
| 99 | continue |
| 100 | } |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 101 | danglingRules[line] = true |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | cmd.WaitOrFatal() |
| 105 | |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 106 | var danglingRulesList []string |
| 107 | for rule := range danglingRules { |
| 108 | danglingRulesList = append(danglingRulesList, rule) |
| 109 | } |
| 110 | sort.Strings(danglingRulesList) |
| 111 | |
| 112 | if len(danglingRulesList) > 0 { |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame] | 113 | sb := &strings.Builder{} |
| 114 | title := "Dependencies in out found with no rule to create them:" |
| 115 | fmt.Fprintln(sb, title) |
Martin Stjernholm | 946fb67 | 2020-04-15 23:23:34 +0100 | [diff] [blame] | 116 | |
| 117 | report_lines := 1 |
| 118 | for i, dep := range danglingRulesList { |
| 119 | if report_lines > 20 { |
| 120 | fmt.Fprintf(sb, " ... and %d more\n", len(danglingRulesList)-i) |
| 121 | break |
| 122 | } |
| 123 | // It's helpful to see the reverse dependencies. ninja -t query is the |
| 124 | // best tool we got for that. Its output starts with the dependency |
| 125 | // itself. |
| 126 | query_cmd := Command(ctx, config, "ninja", executable, |
| 127 | append(common_args, "-t", "query", dep)...) |
| 128 | query_stdout, err := query_cmd.StdoutPipe() |
| 129 | if err != nil { |
| 130 | ctx.Fatal(err) |
| 131 | } |
| 132 | query_cmd.StartOrFatal() |
| 133 | scanner := bufio.NewScanner(query_stdout) |
| 134 | for scanner.Scan() { |
| 135 | report_lines++ |
| 136 | fmt.Fprintln(sb, " ", scanner.Text()) |
| 137 | } |
| 138 | query_cmd.WaitOrFatal() |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 139 | } |
Martin Stjernholm | 946fb67 | 2020-04-15 23:23:34 +0100 | [diff] [blame] | 140 | |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame] | 141 | ts.FinishAction(status.ActionResult{ |
| 142 | Action: action, |
| 143 | Error: fmt.Errorf(title), |
| 144 | Output: sb.String(), |
| 145 | }) |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 146 | ctx.Fatal("stopping") |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 147 | } |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame] | 148 | ts.FinishAction(status.ActionResult{Action: action}) |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 149 | } |