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" |
| 19 | "path/filepath" |
| 20 | "runtime" |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 21 | "sort" |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 22 | "strings" |
| 23 | ) |
| 24 | |
| 25 | // Checks for files in the out directory that have a rule that depends on them but no rule to |
| 26 | // create them. This catches a common set of build failures where a rule to generate a file is |
| 27 | // deleted (either by deleting a module in an Android.mk file, or by modifying the build system |
| 28 | // incorrectly). These failures are often not caught by a local incremental build because the |
| 29 | // previously built files are still present in the output directory. |
| 30 | func testForDanglingRules(ctx Context, config Config) { |
| 31 | // Many modules are disabled on mac. Checking for dangling rules would cause lots of build |
| 32 | // breakages, and presubmit wouldn't catch them, so just disable the check. |
| 33 | if runtime.GOOS != "linux" { |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | ctx.BeginTrace("test for dangling rules") |
| 38 | defer ctx.EndTrace() |
| 39 | |
| 40 | // Get a list of leaf nodes in the dependency graph from ninja |
| 41 | executable := config.PrebuiltBuildTool("ninja") |
| 42 | |
| 43 | args := []string{} |
| 44 | args = append(args, config.NinjaArgs()...) |
| 45 | args = append(args, "-f", config.CombinedNinjaFile()) |
| 46 | args = append(args, "-t", "targets", "rule") |
| 47 | |
| 48 | cmd := Command(ctx, config, "ninja", executable, args...) |
| 49 | stdout, err := cmd.StdoutPipe() |
| 50 | if err != nil { |
| 51 | ctx.Fatal(err) |
| 52 | } |
| 53 | |
| 54 | cmd.StartOrFatal() |
| 55 | |
| 56 | outDir := config.OutDir() |
| 57 | bootstrapDir := filepath.Join(outDir, "soong", ".bootstrap") |
| 58 | miniBootstrapDir := filepath.Join(outDir, "soong", ".minibootstrap") |
| 59 | |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 60 | danglingRules := make(map[string]bool) |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 61 | |
| 62 | scanner := bufio.NewScanner(stdout) |
| 63 | for scanner.Scan() { |
| 64 | line := scanner.Text() |
| 65 | if !strings.HasPrefix(line, outDir) { |
| 66 | // Leaf node is not in the out directory. |
| 67 | continue |
| 68 | } |
| 69 | if strings.HasPrefix(line, bootstrapDir) || strings.HasPrefix(line, miniBootstrapDir) { |
| 70 | // Leaf node is in one of Soong's bootstrap directories, which do not have |
| 71 | // full build rules in the primary build.ninja file. |
| 72 | continue |
| 73 | } |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 74 | danglingRules[line] = true |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | cmd.WaitOrFatal() |
| 78 | |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 79 | var danglingRulesList []string |
| 80 | for rule := range danglingRules { |
| 81 | danglingRulesList = append(danglingRulesList, rule) |
| 82 | } |
| 83 | sort.Strings(danglingRulesList) |
| 84 | |
| 85 | if len(danglingRulesList) > 0 { |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 86 | ctx.Println("Dependencies in out found with no rule to create them:") |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 87 | for _, dep := range danglingRulesList { |
| 88 | ctx.Println(" ", dep) |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 89 | } |
Colin Cross | 63b4e0f | 2018-06-26 23:48:52 -0700 | [diff] [blame] | 90 | ctx.Fatal("stopping") |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 91 | } |
| 92 | } |