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