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