| 1 |
// Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style license that can be |
| 3 |
// found in the LICENSE file. |
| 4 |
|
| 5 |
package gen_tasks_logic |
| 6 |
|
| 7 |
import ( |
| 8 |
"log" |
| 9 |
"os/exec" |
| 10 |
"path/filepath" |
| 11 |
"regexp" |
| 12 |
"sort" |
| 13 |
"strings" |
| 14 |
|
| 15 |
"go.skia.org/infra/go/cas/rbe" |
| 16 |
"go.skia.org/infra/task_scheduler/go/specs" |
| 17 |
) |
| 18 |
|
| 19 |
const ( |
| 20 |
// If a parent path contains more than this many immediate child paths (ie. |
| 21 |
// files and dirs which are directly inside it as opposed to indirect |
| 22 |
// descendants), we will include the parent in the isolate file instead of |
| 23 |
// the children. This results in a simpler CasSpec which should need to be |
| 24 |
// changed less often. |
| 25 |
combinePathsThreshold = 3 |
| 26 |
) |
| 27 |
|
| 28 |
var ( |
| 29 |
// Any files in Git which match these patterns will be included, either |
| 30 |
// directly or indirectly via a parent dir. |
| 31 |
pathRegexes = []*regexp.Regexp{ |
| 32 |
regexp.MustCompile(`.*\.c$`), |
| 33 |
regexp.MustCompile(`.*\.cc$`), |
| 34 |
regexp.MustCompile(`.*\.cpp$`), |
| 35 |
regexp.MustCompile(`.*\.gn$`), |
| 36 |
regexp.MustCompile(`.*\.gni$`), |
| 37 |
regexp.MustCompile(`.*\.h$`), |
| 38 |
regexp.MustCompile(`.*\.mm$`), |
| 39 |
regexp.MustCompile(`.*\.storyboard$`), |
| 40 |
} |
| 41 |
|
| 42 |
// These paths are always added to the inclusion list. Note that they may |
| 43 |
// not appear in the CasSpec if they are included indirectly via a parent |
| 44 |
// dir. |
| 45 |
explicitPaths = []string{ |
| 46 |
".bazelrc", |
| 47 |
".bazeliskrc", |
| 48 |
".bazelversion", |
| 49 |
".clang-format", |
| 50 |
".clang-tidy", |
| 51 |
".vpython3", |
| 52 |
"BUILD.bazel", |
| 53 |
"DEPS", // Needed by bin/fetch-ninja |
| 54 |
"MODULE.bazel", |
| 55 |
"MODULE.bazel.lock", |
| 56 |
"WORKSPACE.bazel", |
| 57 |
"bazel", |
| 58 |
"bin/activate-emsdk", |
| 59 |
"bin/fetch-clang-format", |
| 60 |
"bin/fetch-gn", |
| 61 |
"bin/fetch-ninja", |
| 62 |
"buildtools", |
| 63 |
"example", |
| 64 |
"infra/bots/assets/android_ndk_darwin/VERSION", |
| 65 |
"infra/bots/assets/android_ndk_linux/VERSION", |
| 66 |
"infra/bots/assets/android_ndk_windows/VERSION", |
| 67 |
"infra/bots/assets/cast_toolchain/VERSION", |
| 68 |
"infra/bots/assets/clang_linux/VERSION", |
| 69 |
"infra/bots/assets/clang_win/VERSION", |
| 70 |
"infra/bots/run_recipe.py", |
| 71 |
"infra/bots/task_drivers", |
| 72 |
"infra/canvaskit", |
| 73 |
"package.json", |
| 74 |
"package-lock.json", |
| 75 |
"requirements.txt", |
| 76 |
"resources", |
| 77 |
"rust", |
| 78 |
"third_party/externals", |
| 79 |
"toolchain", |
| 80 |
} |
| 81 |
) |
| 82 |
|
| 83 |
// getAllCheckedInPaths returns every path checked in to the repo. |
| 84 |
func getAllCheckedInPaths(cfg *Config) []string { |
| 85 |
cmd := exec.Command("git", "ls-files") |
| 86 |
// Use cfg.PathToSkia to get to the Skia checkout, in case this is used by |
| 87 |
// another repo. |
| 88 |
cmd.Dir = filepath.Join(CheckoutRoot(), cfg.PathToSkia) |
| 89 |
output, err := cmd.CombinedOutput() |
| 90 |
if err != nil { |
| 91 |
log.Fatal(err) |
| 92 |
} |
| 93 |
split := strings.Split(string(output), "\n") |
| 94 |
rv := make([]string, 0, len(split)) |
| 95 |
for _, line := range split { |
| 96 |
if line != "" { |
| 97 |
rv = append(rv, line) |
| 98 |
} |
| 99 |
} |
| 100 |
return rv |
| 101 |
} |
| 102 |
|
| 103 |
// getRelevantPaths returns all paths needed by compile tasks. |
| 104 |
func getRelevantPaths(cfg *Config) []string { |
| 105 |
rv := []string{} |
| 106 |
for _, path := range getAllCheckedInPaths(cfg) { |
| 107 |
for _, regex := range pathRegexes { |
| 108 |
if regex.MatchString(path) { |
| 109 |
rv = append(rv, path) |
| 110 |
break |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
return append(rv, explicitPaths...) |
| 115 |
} |
| 116 |
|
| 117 |
// node is a single node in a directory tree of task inputs. |
| 118 |
type node struct { |
| 119 |
children map[string]*node |
| 120 |
name string |
| 121 |
isLeaf bool |
| 122 |
} |
| 123 |
|
| 124 |
// newNode returns a node instance. |
| 125 |
func newNode(name string) *node { |
| 126 |
return &node{ |
| 127 |
children: map[string]*node{}, |
| 128 |
name: name, |
| 129 |
isLeaf: false, |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
// isRoot returns true iff this is the root node. |
| 134 |
func (n *node) isRoot() bool { |
| 135 |
return n.name == "" |
| 136 |
} |
| 137 |
|
| 138 |
// add the given entry (given as a slice of path components) to the node. |
| 139 |
func (n *node) add(entry []string) { |
| 140 |
// Remove the first element if we're not the root node. |
| 141 |
if !n.isRoot() { |
| 142 |
if entry[0] != n.name { |
| 143 |
log.Fatalf("Failed to compute compile CAS inputs; attempting to add entry %v to node %q", entry, n.name) |
| 144 |
} |
| 145 |
entry = entry[1:] |
| 146 |
|
| 147 |
// If the entry is now empty, this node is a leaf. |
| 148 |
if len(entry) == 0 { |
| 149 |
n.isLeaf = true |
| 150 |
return |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// Add a child node. |
| 155 |
if !n.isLeaf { |
| 156 |
name := entry[0] |
| 157 |
child, ok := n.children[name] |
| 158 |
if !ok { |
| 159 |
child = newNode(name) |
| 160 |
n.children[name] = child |
| 161 |
} |
| 162 |
child.add(entry) |
| 163 |
|
| 164 |
// If we have more than combinePathsThreshold immediate children, |
| 165 |
// combine them into this node. |
| 166 |
immediateChilden := 0 |
| 167 |
for _, child := range n.children { |
| 168 |
if child.isLeaf { |
| 169 |
immediateChilden++ |
| 170 |
} |
| 171 |
if !n.isRoot() && immediateChilden >= combinePathsThreshold { |
| 172 |
n.isLeaf = true |
| 173 |
n.children = map[string]*node{} |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
// entries returns the entries represented by this node and its children. |
| 180 |
// Will not return children in the following cases: |
| 181 |
// - This Node is a leaf, ie. it represents an entry which was explicitly |
| 182 |
// inserted into the Tree, as opposed to only part of a path to other |
| 183 |
// entries. |
| 184 |
// - This Node has immediate children exceeding combinePathsThreshold and |
| 185 |
// thus has been upgraded to a leaf node. |
| 186 |
func (n *node) entries() [][]string { |
| 187 |
if n.isLeaf { |
| 188 |
return [][]string{{n.name}} |
| 189 |
} |
| 190 |
rv := [][]string{} |
| 191 |
for _, child := range n.children { |
| 192 |
for _, entry := range child.entries() { |
| 193 |
if !n.isRoot() { |
| 194 |
entry = append([]string{n.name}, entry...) |
| 195 |
} |
| 196 |
rv = append(rv, entry) |
| 197 |
} |
| 198 |
} |
| 199 |
return rv |
| 200 |
} |
| 201 |
|
| 202 |
// tree represents a directory tree of task inputs. |
| 203 |
type tree struct { |
| 204 |
root *node |
| 205 |
} |
| 206 |
|
| 207 |
// newTree returns a tree instance. |
| 208 |
func newTree() *tree { |
| 209 |
return &tree{ |
| 210 |
root: newNode(""), |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
// add the given path to the tree. Entries may be combined as defined by |
| 215 |
// combinePathsThreshold. |
| 216 |
func (t *tree) add(p string) { |
| 217 |
split := strings.Split(p, "/") |
| 218 |
t.root.add(split) |
| 219 |
} |
| 220 |
|
| 221 |
// entries returns all entries in the tree. Entries may be combined as defined |
| 222 |
// by combinePathsThreshold. |
| 223 |
func (t *tree) entries() []string { |
| 224 |
entries := t.root.entries() |
| 225 |
rv := make([]string, 0, len(entries)) |
| 226 |
for _, entry := range entries { |
| 227 |
rv = append(rv, strings.Join(append([]string{"skia"}, entry...), "/")) |
| 228 |
} |
| 229 |
sort.Strings(rv) |
| 230 |
return rv |
| 231 |
} |
| 232 |
|
| 233 |
// generateCompileCAS creates the CasSpec used for tasks which build Skia. |
| 234 |
func generateCompileCAS(b *specs.TasksCfgBuilder, cfg *Config) { |
| 235 |
t := newTree() |
| 236 |
for _, path := range getRelevantPaths(cfg) { |
| 237 |
t.add(path) |
| 238 |
} |
| 239 |
spec := &specs.CasSpec{ |
| 240 |
Root: "..", |
| 241 |
Paths: t.entries(), |
| 242 |
Excludes: []string{rbe.ExcludeGitDir}, |
| 243 |
} |
| 244 |
b.MustAddCasSpec(CAS_COMPILE, spec) |
| 245 |
} |