blob: f3eb1b1d23841ec249c3724a6f53c104036ca6dc [file] [log] [blame]
Colin Cross127d2ea2016-11-01 11:10:51 -07001// 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
15package blueprint
16
17import (
Colin Cross741e14e2017-12-07 22:02:40 -080018 "crypto/md5"
Colin Cross127d2ea2016-11-01 11:10:51 -070019 "fmt"
20 "reflect"
21 "sort"
Colin Cross741e14e2017-12-07 22:02:40 -080022 "strings"
Colin Cross127d2ea2016-11-01 11:10:51 -070023)
24
25type GlobPath struct {
26 Pattern string
27 Excludes []string
28 Files []string
29 Deps []string
30 Name string
31}
32
33func 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
42func (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 Crossb519a7e2017-02-01 13:21:35 -080057 files, deps, err := c.fs.Glob(pattern, excludes)
Colin Cross127d2ea2016-11-01 11:10:51 -070058 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
78func (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
93func 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
110func globToFileName(pattern string, excludes []string) string {
Colin Cross741e14e2017-12-07 22:02:40 -0800111 name := globToString(pattern)
112 excludeName := ""
Colin Cross127d2ea2016-11-01 11:10:51 -0700113 for _, e := range excludes {
Colin Cross741e14e2017-12-07 22:02:40 -0800114 excludeName += "__" + globToString(e)
Colin Cross127d2ea2016-11-01 11:10:51 -0700115 }
Colin Cross741e14e2017-12-07 22:02:40 -0800116
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 Cross127d2ea2016-11-01 11:10:51 -0700123}