Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [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 blueprint |
| 16 | |
| 17 | import ( |
Colin Cross | 741e14e | 2017-12-07 22:02:40 -0800 | [diff] [blame^] | 18 | "crypto/md5" |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 19 | "fmt" |
| 20 | "reflect" |
| 21 | "sort" |
Colin Cross | 741e14e | 2017-12-07 22:02:40 -0800 | [diff] [blame^] | 22 | "strings" |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | type GlobPath struct { |
| 26 | Pattern string |
| 27 | Excludes []string |
| 28 | Files []string |
| 29 | Deps []string |
| 30 | Name string |
| 31 | } |
| 32 | |
| 33 | func verifyGlob(fileName, pattern string, excludes []string, g GlobPath) { |
| 34 | if pattern != g.Pattern { |
| 35 | panic(fmt.Errorf("Mismatched patterns %q and %q for glob file %q", pattern, g.Pattern, fileName)) |
| 36 | } |
| 37 | if !reflect.DeepEqual(g.Excludes, excludes) { |
| 38 | panic(fmt.Errorf("Mismatched excludes %v and %v for glob file %q", excludes, g.Excludes, fileName)) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func (c *Context) glob(pattern string, excludes []string) ([]string, error) { |
| 43 | fileName := globToFileName(pattern, excludes) |
| 44 | |
| 45 | // Try to get existing glob from the stored results |
| 46 | c.globLock.Lock() |
| 47 | g, exists := c.globs[fileName] |
| 48 | c.globLock.Unlock() |
| 49 | |
| 50 | if exists { |
| 51 | // Glob has already been done, double check it is identical |
| 52 | verifyGlob(fileName, pattern, excludes, g) |
| 53 | return g.Files, nil |
| 54 | } |
| 55 | |
| 56 | // Get a globbed file list |
Colin Cross | b519a7e | 2017-02-01 13:21:35 -0800 | [diff] [blame] | 57 | files, deps, err := c.fs.Glob(pattern, excludes) |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | |
| 62 | // Store the results |
| 63 | c.globLock.Lock() |
| 64 | if g, exists = c.globs[fileName]; !exists { |
| 65 | c.globs[fileName] = GlobPath{pattern, excludes, files, deps, fileName} |
| 66 | } |
| 67 | c.globLock.Unlock() |
| 68 | |
| 69 | // Getting the list raced with another goroutine, throw away the results and use theirs |
| 70 | if exists { |
| 71 | verifyGlob(fileName, pattern, excludes, g) |
| 72 | return g.Files, nil |
| 73 | } |
| 74 | |
| 75 | return files, nil |
| 76 | } |
| 77 | |
| 78 | func (c *Context) Globs() []GlobPath { |
| 79 | fileNames := make([]string, 0, len(c.globs)) |
| 80 | for k := range c.globs { |
| 81 | fileNames = append(fileNames, k) |
| 82 | } |
| 83 | sort.Strings(fileNames) |
| 84 | |
| 85 | globs := make([]GlobPath, len(fileNames)) |
| 86 | for i, fileName := range fileNames { |
| 87 | globs[i] = c.globs[fileName] |
| 88 | } |
| 89 | |
| 90 | return globs |
| 91 | } |
| 92 | |
| 93 | func globToString(pattern string) string { |
| 94 | ret := "" |
| 95 | for _, c := range pattern { |
| 96 | switch { |
| 97 | case c >= 'a' && c <= 'z', |
| 98 | c >= 'A' && c <= 'Z', |
| 99 | c >= '0' && c <= '9', |
| 100 | c == '_', c == '-', c == '/': |
| 101 | ret += string(c) |
| 102 | default: |
| 103 | ret += "_" |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return ret |
| 108 | } |
| 109 | |
| 110 | func globToFileName(pattern string, excludes []string) string { |
Colin Cross | 741e14e | 2017-12-07 22:02:40 -0800 | [diff] [blame^] | 111 | name := globToString(pattern) |
| 112 | excludeName := "" |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 113 | for _, e := range excludes { |
Colin Cross | 741e14e | 2017-12-07 22:02:40 -0800 | [diff] [blame^] | 114 | excludeName += "__" + globToString(e) |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 115 | } |
Colin Cross | 741e14e | 2017-12-07 22:02:40 -0800 | [diff] [blame^] | 116 | |
| 117 | // Prevent file names from reaching ninja's path component limit |
| 118 | if strings.Count(name, "/")+strings.Count(excludeName, "/") > 30 { |
| 119 | excludeName = fmt.Sprintf("___%x", md5.Sum([]byte(excludeName))) |
| 120 | } |
| 121 | |
| 122 | return name + excludeName + ".glob" |
Colin Cross | 127d2ea | 2016-11-01 11:10:51 -0700 | [diff] [blame] | 123 | } |