Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1 | // Copyright 2015 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 common |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "path/filepath" |
| 20 | |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 21 | "github.com/google/blueprint" |
| 22 | "github.com/google/blueprint/bootstrap" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 23 | |
| 24 | "android/soong/glob" |
| 25 | ) |
| 26 | |
| 27 | // This file supports globbing source files in Blueprints files. |
| 28 | // |
| 29 | // The build.ninja file needs to be regenerated any time a file matching the glob is added |
| 30 | // or removed. The naive solution is to have the build.ninja file depend on all the |
| 31 | // traversed directories, but this will cause the regeneration step to run every time a |
| 32 | // non-matching file is added to a traversed directory, including backup files created by |
| 33 | // editors. |
| 34 | // |
| 35 | // The solution implemented here optimizes out regenerations when the directory modifications |
| 36 | // don't match the glob by having the build.ninja file depend on an intermedate file that |
| 37 | // is only updated when a file matching the glob is added or removed. The intermediate file |
| 38 | // depends on the traversed directories via a depfile. The depfile is used to avoid build |
| 39 | // errors if a directory is deleted - a direct dependency on the deleted directory would result |
| 40 | // in a build failure with a "missing and no known rule to make it" error. |
| 41 | |
| 42 | var ( |
| 43 | globCmd = filepath.Join(bootstrap.BinDir, "soong_glob") |
| 44 | |
| 45 | // globRule rule traverses directories to produce a list of files that match $glob |
| 46 | // and writes it to $out if it has changed, and writes the directories to $out.d |
| 47 | globRule = pctx.StaticRule("globRule", |
| 48 | blueprint.RuleParams{ |
Colin Cross | 3e8ec07 | 2015-03-31 16:40:23 -0700 | [diff] [blame] | 49 | Command: fmt.Sprintf(`%s -o $out $excludes "$glob"`, globCmd), |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 50 | Description: "glob $glob", |
| 51 | |
Colin Cross | 6a114ca | 2015-04-24 15:14:48 -0700 | [diff] [blame^] | 52 | Restat: true, |
| 53 | Deps: blueprint.DepsGCC, |
| 54 | Depfile: "$out.d", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 55 | }, |
Colin Cross | 3e8ec07 | 2015-03-31 16:40:23 -0700 | [diff] [blame] | 56 | "glob", "excludes") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 57 | ) |
| 58 | |
| 59 | func hasGlob(in []string) bool { |
| 60 | for _, s := range in { |
| 61 | if glob.IsGlob(s) { |
| 62 | return true |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return false |
| 67 | } |
| 68 | |
Colin Cross | fce5327 | 2015-04-08 11:21:40 -0700 | [diff] [blame] | 69 | func expandGlobs(ctx AndroidModuleContext, in []string) []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 70 | if !hasGlob(in) { |
| 71 | return in |
| 72 | } |
| 73 | |
Colin Cross | fce5327 | 2015-04-08 11:21:40 -0700 | [diff] [blame] | 74 | var excludes []string |
| 75 | for _, s := range in { |
| 76 | if s[0] == '-' { |
| 77 | excludes = append(excludes, s[1:]) |
| 78 | } |
| 79 | } |
| 80 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 81 | out := make([]string, 0, len(in)) |
| 82 | for _, s := range in { |
Colin Cross | 6a114ca | 2015-04-24 15:14:48 -0700 | [diff] [blame^] | 83 | if s[0] == '-' { |
| 84 | continue |
| 85 | } else if glob.IsGlob(s) { |
Colin Cross | fce5327 | 2015-04-08 11:21:40 -0700 | [diff] [blame] | 86 | out = append(out, Glob(ctx, s, excludes)...) |
Colin Cross | 6a114ca | 2015-04-24 15:14:48 -0700 | [diff] [blame^] | 87 | } else { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 88 | out = append(out, s) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return out |
| 93 | } |
| 94 | |
Colin Cross | fce5327 | 2015-04-08 11:21:40 -0700 | [diff] [blame] | 95 | func Glob(ctx AndroidModuleContext, globPattern string, excludes []string) []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 96 | fileListFile := filepath.Join(ModuleOutDir(ctx), "glob", globToString(globPattern)) |
| 97 | depFile := fileListFile + ".d" |
| 98 | |
| 99 | // Get a globbed file list, and write out fileListFile and depFile |
Colin Cross | 3e8ec07 | 2015-03-31 16:40:23 -0700 | [diff] [blame] | 100 | files, err := glob.GlobWithDepFile(globPattern, fileListFile, depFile, excludes) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 101 | if err != nil { |
| 102 | ctx.ModuleErrorf("glob: %s", err.Error()) |
| 103 | return []string{globPattern} |
| 104 | } |
| 105 | |
Colin Cross | 3e8ec07 | 2015-03-31 16:40:23 -0700 | [diff] [blame] | 106 | GlobRule(ctx, globPattern, excludes, fileListFile, depFile) |
| 107 | |
| 108 | // Make build.ninja depend on the fileListFile |
| 109 | ctx.AddNinjaFileDeps(fileListFile) |
| 110 | |
| 111 | return files |
| 112 | } |
| 113 | |
| 114 | func GlobRule(ctx AndroidModuleContext, globPattern string, excludes []string, |
| 115 | fileListFile, depFile string) { |
Colin Cross | 3e8ec07 | 2015-03-31 16:40:23 -0700 | [diff] [blame] | 116 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 117 | // Create a rule to rebuild fileListFile if a directory in depFile changes. fileListFile |
| 118 | // will only be rewritten if it has changed, preventing unnecesary build.ninja regenerations. |
| 119 | ctx.Build(pctx, blueprint.BuildParams{ |
| 120 | Rule: globRule, |
| 121 | Outputs: []string{fileListFile}, |
| 122 | Implicits: []string{globCmd}, |
| 123 | Args: map[string]string{ |
Colin Cross | 3e8ec07 | 2015-03-31 16:40:23 -0700 | [diff] [blame] | 124 | "glob": globPattern, |
Colin Cross | 9b6826f | 2015-04-10 15:47:33 -0700 | [diff] [blame] | 125 | "excludes": JoinWithPrefixAndQuote(excludes, "-e "), |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 126 | }, |
| 127 | }) |
| 128 | |
| 129 | // Phony rule so the cleanup phase doesn't delete the depFile |
| 130 | ctx.Build(pctx, blueprint.BuildParams{ |
| 131 | Rule: blueprint.Phony, |
| 132 | Outputs: []string{depFile}, |
| 133 | }) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | func globToString(glob string) string { |
| 137 | ret := "" |
| 138 | for _, c := range glob { |
| 139 | if c >= 'a' && c <= 'z' || |
| 140 | c >= 'A' && c <= 'Z' || |
| 141 | c >= '0' && c <= '9' || |
| 142 | c == '_' || c == '-' || c == '/' { |
| 143 | ret += string(c) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return ret |
| 148 | } |