blob: dad5edf4950712a092c6525870907d98b456e8cf [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 (
18 "fmt"
19 "reflect"
20 "sort"
21
22 "github.com/google/blueprint/pathtools"
23)
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
57 files, deps, err := pathtools.GlobWithExcludes(pattern, excludes)
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
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 {
111 ret := globToString(pattern)
112 for _, e := range excludes {
113 ret += "__" + globToString(e)
114 }
115 return ret + ".glob"
116}