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 | |
| 25 | "android/soong/ui/status" |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 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. |
| 33 | func 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 | |
| 40 | ctx.BeginTrace("test for dangling rules") |
| 41 | defer ctx.EndTrace() |
| 42 | |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame^] | 43 | ts := ctx.Status.StartTool() |
| 44 | action := &status.Action{ |
| 45 | Description: "Test for dangling rules", |
| 46 | } |
| 47 | ts.StartAction(action) |
| 48 | |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 49 | // Get a list of leaf nodes in the dependency graph from ninja |
| 50 | executable := config.PrebuiltBuildTool("ninja") |
| 51 | |
| 52 | args := []string{} |
| 53 | args = append(args, config.NinjaArgs()...) |
| 54 | args = append(args, "-f", config.CombinedNinjaFile()) |
| 55 | args = append(args, "-t", "targets", "rule") |
| 56 | |
| 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") |
| 67 | miniBootstrapDir := filepath.Join(outDir, "soong", ".minibootstrap") |
| 68 | |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 69 | danglingRules := make(map[string]bool) |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 70 | |
| 71 | scanner := bufio.NewScanner(stdout) |
| 72 | for scanner.Scan() { |
| 73 | line := scanner.Text() |
| 74 | if !strings.HasPrefix(line, outDir) { |
| 75 | // Leaf node is not in the out directory. |
| 76 | continue |
| 77 | } |
| 78 | if strings.HasPrefix(line, bootstrapDir) || strings.HasPrefix(line, miniBootstrapDir) { |
| 79 | // Leaf node is in one of Soong's bootstrap directories, which do not have |
| 80 | // full build rules in the primary build.ninja file. |
| 81 | continue |
| 82 | } |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 83 | danglingRules[line] = true |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | cmd.WaitOrFatal() |
| 87 | |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 88 | var danglingRulesList []string |
| 89 | for rule := range danglingRules { |
| 90 | danglingRulesList = append(danglingRulesList, rule) |
| 91 | } |
| 92 | sort.Strings(danglingRulesList) |
| 93 | |
| 94 | if len(danglingRulesList) > 0 { |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame^] | 95 | sb := &strings.Builder{} |
| 96 | title := "Dependencies in out found with no rule to create them:" |
| 97 | fmt.Fprintln(sb, title) |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 98 | for _, dep := range danglingRulesList { |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame^] | 99 | fmt.Fprintln(sb, " ", dep) |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 100 | } |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame^] | 101 | ts.FinishAction(status.ActionResult{ |
| 102 | Action: action, |
| 103 | Error: fmt.Errorf(title), |
| 104 | Output: sb.String(), |
| 105 | }) |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 106 | ctx.Fatal("stopping") |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 107 | } |
Dan Willemsen | d2e231a | 2018-08-02 12:06:24 -0700 | [diff] [blame^] | 108 | ts.FinishAction(status.ActionResult{Action: action}) |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 109 | } |