Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 1 | // Copyright 2022 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 | |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 15 | package bp2build |
| 16 | |
| 17 | import ( |
Lukacs T. Berki | bc5f731 | 2022-10-31 09:01:34 +0000 | [diff] [blame] | 18 | "errors" |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 19 | "fmt" |
Lukacs T. Berki | bc5f731 | 2022-10-31 09:01:34 +0000 | [diff] [blame] | 20 | "io/fs" |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 21 | "io/ioutil" |
| 22 | "os" |
| 23 | "path/filepath" |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 24 | "regexp" |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 25 | "sync" |
| 26 | "sync/atomic" |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 27 | |
| 28 | "android/soong/shared" |
| 29 | ) |
| 30 | |
| 31 | // A tree structure that describes what to do at each directory in the created |
| 32 | // symlink tree. Currently it is used to enumerate which files/directories |
| 33 | // should be excluded from symlinking. Each instance of "node" represents a file |
| 34 | // or a directory. If excluded is true, then that file/directory should be |
| 35 | // excluded from symlinking. Otherwise, the node is not excluded, but one of its |
| 36 | // descendants is (otherwise the node in question would not exist) |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 37 | |
| 38 | type instructionsNode struct { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 39 | name string |
| 40 | excluded bool // If false, this is just an intermediate node |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 41 | children map[string]*instructionsNode |
| 42 | } |
| 43 | |
| 44 | type symlinkForestContext struct { |
| 45 | verbose bool |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 46 | topdir string // $TOPDIR |
| 47 | |
| 48 | // State |
| 49 | wg sync.WaitGroup |
| 50 | depCh chan string |
| 51 | okay atomic.Bool // Whether the forest was successfully constructed |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 52 | } |
| 53 | |
Lukacs T. Berki | bc5f731 | 2022-10-31 09:01:34 +0000 | [diff] [blame] | 54 | // A simple thread pool to limit concurrency on system calls. |
| 55 | // Necessary because Go spawns a new OS-level thread for each blocking system |
| 56 | // call. This means that if syscalls are too slow and there are too many of |
| 57 | // them, the hard limit on OS-level threads can be exhausted. |
| 58 | type syscallPool struct { |
| 59 | shutdownCh []chan<- struct{} |
| 60 | workCh chan syscall |
| 61 | } |
| 62 | |
| 63 | type syscall struct { |
| 64 | work func() |
| 65 | done chan<- struct{} |
| 66 | } |
| 67 | |
| 68 | func createSyscallPool(count int) *syscallPool { |
| 69 | result := &syscallPool{ |
| 70 | shutdownCh: make([]chan<- struct{}, count), |
| 71 | workCh: make(chan syscall), |
| 72 | } |
| 73 | |
| 74 | for i := 0; i < count; i++ { |
| 75 | shutdownCh := make(chan struct{}) |
| 76 | result.shutdownCh[i] = shutdownCh |
| 77 | go result.worker(shutdownCh) |
| 78 | } |
| 79 | |
| 80 | return result |
| 81 | } |
| 82 | |
| 83 | func (p *syscallPool) do(work func()) { |
| 84 | doneCh := make(chan struct{}) |
| 85 | p.workCh <- syscall{work, doneCh} |
| 86 | <-doneCh |
| 87 | } |
| 88 | |
| 89 | func (p *syscallPool) shutdown() { |
| 90 | for _, ch := range p.shutdownCh { |
| 91 | ch <- struct{}{} // Blocks until the value is received |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func (p *syscallPool) worker(shutdownCh <-chan struct{}) { |
| 96 | for { |
| 97 | select { |
| 98 | case <-shutdownCh: |
| 99 | return |
| 100 | case work := <-p.workCh: |
| 101 | work.work() |
| 102 | work.done <- struct{}{} |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 107 | // Ensures that the node for the given path exists in the tree and returns it. |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 108 | func ensureNodeExists(root *instructionsNode, path string) *instructionsNode { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 109 | if path == "" { |
| 110 | return root |
| 111 | } |
| 112 | |
| 113 | if path[len(path)-1] == '/' { |
| 114 | path = path[:len(path)-1] // filepath.Split() leaves a trailing slash |
| 115 | } |
| 116 | |
| 117 | dir, base := filepath.Split(path) |
| 118 | |
| 119 | // First compute the parent node... |
| 120 | dn := ensureNodeExists(root, dir) |
| 121 | |
| 122 | // then create the requested node as its direct child, if needed. |
| 123 | if child, ok := dn.children[base]; ok { |
| 124 | return child |
| 125 | } else { |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 126 | dn.children[base] = &instructionsNode{base, false, make(map[string]*instructionsNode)} |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 127 | return dn.children[base] |
| 128 | } |
| 129 | } |
| 130 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 131 | // Turns a list of paths to be excluded into a tree |
| 132 | func instructionsFromExcludePathList(paths []string) *instructionsNode { |
| 133 | result := &instructionsNode{"", false, make(map[string]*instructionsNode)} |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 134 | |
| 135 | for _, p := range paths { |
| 136 | ensureNodeExists(result, p).excluded = true |
| 137 | } |
| 138 | |
| 139 | return result |
| 140 | } |
| 141 | |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 142 | func mergeBuildFiles(output string, srcBuildFile string, generatedBuildFile string, verbose bool) error { |
| 143 | |
| 144 | srcBuildFileContent, err := os.ReadFile(srcBuildFile) |
| 145 | if err != nil { |
| 146 | return err |
| 147 | } |
| 148 | |
| 149 | generatedBuildFileContent, err := os.ReadFile(generatedBuildFile) |
| 150 | if err != nil { |
| 151 | return err |
| 152 | } |
| 153 | |
| 154 | // There can't be a package() call in both the source and generated BUILD files. |
| 155 | // bp2build will generate a package() call for licensing information, but if |
| 156 | // there's no licensing information, it will still generate a package() call |
| 157 | // that just sets default_visibility=public. If the handcrafted build file |
| 158 | // also has a package() call, we'll allow it to override the bp2build |
| 159 | // generated one if it doesn't have any licensing information. If the bp2build |
| 160 | // one has licensing information and the handcrafted one exists, we'll leave |
| 161 | // them both in for bazel to throw an error. |
| 162 | packageRegex := regexp.MustCompile(`(?m)^package\s*\(`) |
| 163 | packageDefaultVisibilityRegex := regexp.MustCompile(`(?m)^package\s*\(\s*default_visibility\s*=\s*\[\s*"//visibility:public",?\s*]\s*\)`) |
| 164 | if packageRegex.Find(srcBuildFileContent) != nil { |
| 165 | if verbose && packageDefaultVisibilityRegex.Find(generatedBuildFileContent) != nil { |
| 166 | fmt.Fprintf(os.Stderr, "Both '%s' and '%s' have a package() target, removing the first one\n", |
| 167 | generatedBuildFile, srcBuildFile) |
| 168 | } |
| 169 | generatedBuildFileContent = packageDefaultVisibilityRegex.ReplaceAll(generatedBuildFileContent, []byte{}) |
| 170 | } |
| 171 | |
| 172 | outFile, err := os.Create(output) |
| 173 | if err != nil { |
| 174 | return err |
| 175 | } |
| 176 | |
| 177 | _, err = outFile.Write(generatedBuildFileContent) |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | |
| 182 | if generatedBuildFileContent[len(generatedBuildFileContent)-1] != '\n' { |
| 183 | _, err = outFile.WriteString("\n") |
| 184 | if err != nil { |
| 185 | return err |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | _, err = outFile.Write(srcBuildFileContent) |
| 190 | return err |
| 191 | } |
| 192 | |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 193 | // Calls readdir() and returns it as a map from the basename of the files in dir |
| 194 | // to os.FileInfo. |
| 195 | func readdirToMap(dir string) map[string]os.FileInfo { |
| 196 | entryList, err := ioutil.ReadDir(dir) |
| 197 | result := make(map[string]os.FileInfo) |
| 198 | |
| 199 | if err != nil { |
| 200 | if os.IsNotExist(err) { |
| 201 | // It's okay if a directory doesn't exist; it just means that one of the |
| 202 | // trees to be merged contains parts the other doesn't |
| 203 | return result |
| 204 | } else { |
| 205 | fmt.Fprintf(os.Stderr, "Cannot readdir '%s': %s\n", dir, err) |
| 206 | os.Exit(1) |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | for _, fi := range entryList { |
| 211 | result[fi.Name()] = fi |
| 212 | } |
| 213 | |
| 214 | return result |
| 215 | } |
| 216 | |
| 217 | // Creates a symbolic link at dst pointing to src |
| 218 | func symlinkIntoForest(topdir, dst, src string) { |
| 219 | err := os.Symlink(shared.JoinPath(topdir, src), shared.JoinPath(topdir, dst)) |
| 220 | if err != nil { |
| 221 | fmt.Fprintf(os.Stderr, "Cannot create symlink at '%s' pointing to '%s': %s", dst, src, err) |
| 222 | os.Exit(1) |
| 223 | } |
| 224 | } |
| 225 | |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 226 | func isDir(path string, fi os.FileInfo) bool { |
| 227 | if (fi.Mode() & os.ModeSymlink) != os.ModeSymlink { |
| 228 | return fi.IsDir() |
| 229 | } |
| 230 | |
Jingwen Chen | d4b1dc8 | 2022-05-12 11:08:03 +0000 | [diff] [blame] | 231 | fi2, statErr := os.Stat(path) |
| 232 | if statErr == nil { |
| 233 | return fi2.IsDir() |
| 234 | } |
| 235 | |
| 236 | // Check if this is a dangling symlink. If so, treat it like a file, not a dir. |
| 237 | _, lstatErr := os.Lstat(path) |
| 238 | if lstatErr != nil { |
| 239 | fmt.Fprintf(os.Stderr, "Cannot stat or lstat '%s': %s\n%s\n", path, statErr, lstatErr) |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 240 | os.Exit(1) |
| 241 | } |
| 242 | |
Jingwen Chen | d4b1dc8 | 2022-05-12 11:08:03 +0000 | [diff] [blame] | 243 | return false |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 246 | // Recursively plants a symlink forest at forestDir. The symlink tree will |
| 247 | // contain every file in buildFilesDir and srcDir excluding the files in |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 248 | // instructions. Collects every directory encountered during the traversal of |
| 249 | // srcDir . |
| 250 | func plantSymlinkForestRecursive(context *symlinkForestContext, instructions *instructionsNode, forestDir string, buildFilesDir string, srcDir string) { |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 251 | defer context.wg.Done() |
| 252 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 253 | if instructions != nil && instructions.excluded { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 254 | // This directory is not needed, bail out |
| 255 | return |
| 256 | } |
| 257 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 258 | // We don't add buildFilesDir here because the bp2build files marker files is |
| 259 | // already a dependency which covers it. If we ever wanted to turn this into |
| 260 | // a generic symlink forest creation tool, we'd need to add it, too. |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 261 | context.depCh <- srcDir |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 262 | |
| 263 | srcDirMap := readdirToMap(shared.JoinPath(context.topdir, srcDir)) |
| 264 | buildFilesMap := readdirToMap(shared.JoinPath(context.topdir, buildFilesDir)) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 265 | |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 266 | renamingBuildFile := false |
| 267 | if _, ok := srcDirMap["BUILD"]; ok { |
| 268 | if _, ok := srcDirMap["BUILD.bazel"]; !ok { |
| 269 | if _, ok := buildFilesMap["BUILD.bazel"]; ok { |
| 270 | renamingBuildFile = true |
| 271 | srcDirMap["BUILD.bazel"] = srcDirMap["BUILD"] |
| 272 | delete(srcDirMap, "BUILD") |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
Usta Shrestha | 49d04e8 | 2022-10-17 17:36:49 -0400 | [diff] [blame] | 277 | allEntries := make(map[string]struct{}) |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 278 | for n := range srcDirMap { |
Usta Shrestha | 49d04e8 | 2022-10-17 17:36:49 -0400 | [diff] [blame] | 279 | allEntries[n] = struct{}{} |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 282 | for n := range buildFilesMap { |
Usta Shrestha | 49d04e8 | 2022-10-17 17:36:49 -0400 | [diff] [blame] | 283 | allEntries[n] = struct{}{} |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 286 | err := os.MkdirAll(shared.JoinPath(context.topdir, forestDir), 0777) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 287 | if err != nil { |
| 288 | fmt.Fprintf(os.Stderr, "Cannot mkdir '%s': %s\n", forestDir, err) |
| 289 | os.Exit(1) |
| 290 | } |
| 291 | |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 292 | for f := range allEntries { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 293 | if f[0] == '.' { |
| 294 | continue // Ignore dotfiles |
| 295 | } |
| 296 | |
| 297 | // The full paths of children in the input trees and in the output tree |
Lukacs T. Berki | 3f9416e | 2021-04-20 13:08:11 +0200 | [diff] [blame] | 298 | forestChild := shared.JoinPath(forestDir, f) |
| 299 | srcChild := shared.JoinPath(srcDir, f) |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 300 | if f == "BUILD.bazel" && renamingBuildFile { |
| 301 | srcChild = shared.JoinPath(srcDir, "BUILD") |
| 302 | } |
Lukacs T. Berki | 3f9416e | 2021-04-20 13:08:11 +0200 | [diff] [blame] | 303 | buildFilesChild := shared.JoinPath(buildFilesDir, f) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 304 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 305 | // Descend in the instruction tree if it exists |
| 306 | var instructionsChild *instructionsNode = nil |
| 307 | if instructions != nil { |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 308 | if f == "BUILD.bazel" && renamingBuildFile { |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 309 | instructionsChild = instructions.children["BUILD"] |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 310 | } else { |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 311 | instructionsChild = instructions.children[f] |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 312 | } |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Lukacs T. Berki | 3f9416e | 2021-04-20 13:08:11 +0200 | [diff] [blame] | 315 | srcChildEntry, sExists := srcDirMap[f] |
| 316 | buildFilesChildEntry, bExists := buildFilesMap[f] |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 317 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 318 | if instructionsChild != nil && instructionsChild.excluded { |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 319 | if bExists { |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 320 | symlinkIntoForest(context.topdir, forestChild, buildFilesChild) |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 321 | } |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 322 | continue |
| 323 | } |
| 324 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 325 | sDir := sExists && isDir(shared.JoinPath(context.topdir, srcChild), srcChildEntry) |
| 326 | bDir := bExists && isDir(shared.JoinPath(context.topdir, buildFilesChild), buildFilesChildEntry) |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 327 | |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 328 | if !sExists { |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 329 | if bDir && instructionsChild != nil { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 330 | // Not in the source tree, but we have to exclude something from under |
| 331 | // this subtree, so descend |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 332 | context.wg.Add(1) |
| 333 | go plantSymlinkForestRecursive(context, instructionsChild, forestChild, buildFilesChild, srcChild) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 334 | } else { |
| 335 | // Not in the source tree, symlink BUILD file |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 336 | symlinkIntoForest(context.topdir, forestChild, buildFilesChild) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 337 | } |
| 338 | } else if !bExists { |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 339 | if sDir && instructionsChild != nil { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 340 | // Not in the build file tree, but we have to exclude something from |
| 341 | // under this subtree, so descend |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 342 | context.wg.Add(1) |
| 343 | go plantSymlinkForestRecursive(context, instructionsChild, forestChild, buildFilesChild, srcChild) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 344 | } else { |
| 345 | // Not in the build file tree, symlink source tree, carry on |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 346 | symlinkIntoForest(context.topdir, forestChild, srcChild) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 347 | } |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 348 | } else if sDir && bDir { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 349 | // Both are directories. Descend. |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 350 | context.wg.Add(1) |
| 351 | go plantSymlinkForestRecursive(context, instructionsChild, forestChild, buildFilesChild, srcChild) |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 352 | } else if !sDir && !bDir { |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 353 | // Neither is a directory. Merge them. |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 354 | srcBuildFile := shared.JoinPath(context.topdir, srcChild) |
| 355 | generatedBuildFile := shared.JoinPath(context.topdir, buildFilesChild) |
Usta Shrestha | 783ec5c | 2022-10-25 01:22:36 -0400 | [diff] [blame] | 356 | // The Android.bp file that codegen used to produce `buildFilesChild` is |
| 357 | // already a dependency, we can ignore `buildFilesChild`. |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 358 | context.depCh <- srcChild |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 359 | err = mergeBuildFiles(shared.JoinPath(context.topdir, forestChild), srcBuildFile, generatedBuildFile, context.verbose) |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 360 | if err != nil { |
| 361 | fmt.Fprintf(os.Stderr, "Error merging %s and %s: %s", |
| 362 | srcBuildFile, generatedBuildFile, err) |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 363 | context.okay.Store(false) |
Sasha Smundak | 0fd93e0 | 2022-05-19 19:34:31 -0700 | [diff] [blame] | 364 | } |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 365 | } else { |
| 366 | // Both exist and one is a file. This is an error. |
| 367 | fmt.Fprintf(os.Stderr, |
Lukacs T. Berki | b21166e | 2021-04-21 11:58:36 +0200 | [diff] [blame] | 368 | "Conflict in workspace symlink tree creation: both '%s' and '%s' exist and exactly one is a directory\n", |
Lukacs T. Berki | 3f9416e | 2021-04-20 13:08:11 +0200 | [diff] [blame] | 369 | srcChild, buildFilesChild) |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 370 | context.okay.Store(false) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
Lukacs T. Berki | bc5f731 | 2022-10-31 09:01:34 +0000 | [diff] [blame] | 375 | func removeParallelRecursive(pool *syscallPool, path string, fi os.FileInfo, wg *sync.WaitGroup) { |
| 376 | defer wg.Done() |
| 377 | |
| 378 | if fi.IsDir() { |
| 379 | children := readdirToMap(path) |
| 380 | childrenWg := &sync.WaitGroup{} |
| 381 | childrenWg.Add(len(children)) |
| 382 | |
| 383 | for child, childFi := range children { |
| 384 | go removeParallelRecursive(pool, shared.JoinPath(path, child), childFi, childrenWg) |
| 385 | } |
| 386 | |
| 387 | childrenWg.Wait() |
| 388 | } |
| 389 | |
| 390 | pool.do(func() { |
| 391 | if err := os.Remove(path); err != nil { |
| 392 | fmt.Fprintf(os.Stderr, "Cannot unlink '%s': %s\n", path, err) |
| 393 | os.Exit(1) |
| 394 | } |
| 395 | }) |
| 396 | } |
| 397 | |
| 398 | func removeParallel(path string) { |
| 399 | fi, err := os.Lstat(path) |
| 400 | if err != nil { |
| 401 | if errors.Is(err, fs.ErrNotExist) { |
| 402 | return |
| 403 | } |
| 404 | |
| 405 | fmt.Fprintf(os.Stderr, "Cannot lstat '%s': %s\n", path, err) |
| 406 | os.Exit(1) |
| 407 | } |
| 408 | |
| 409 | wg := &sync.WaitGroup{} |
| 410 | wg.Add(1) |
| 411 | |
| 412 | // Random guess as to the best number of syscalls to run in parallel |
| 413 | pool := createSyscallPool(100) |
| 414 | removeParallelRecursive(pool, path, fi, wg) |
| 415 | pool.shutdown() |
| 416 | |
| 417 | wg.Wait() |
| 418 | } |
| 419 | |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 420 | // Creates a symlink forest by merging the directory tree at "buildFiles" and |
| 421 | // "srcDir" while excluding paths listed in "exclude". Returns the set of paths |
| 422 | // under srcDir on which readdir() had to be called to produce the symlink |
| 423 | // forest. |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 424 | func PlantSymlinkForest(verbose bool, topdir string, forest string, buildFiles string, exclude []string) []string { |
| 425 | context := &symlinkForestContext{ |
| 426 | verbose: verbose, |
| 427 | topdir: topdir, |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 428 | depCh: make(chan string), |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 431 | context.okay.Store(true) |
| 432 | |
Lukacs T. Berki | bc5f731 | 2022-10-31 09:01:34 +0000 | [diff] [blame] | 433 | removeParallel(shared.JoinPath(topdir, forest)) |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 434 | |
| 435 | instructions := instructionsFromExcludePathList(exclude) |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 436 | go func() { |
| 437 | context.wg.Add(1) |
| 438 | plantSymlinkForestRecursive(context, instructions, forest, buildFiles, ".") |
| 439 | context.wg.Wait() |
| 440 | close(context.depCh) |
| 441 | }() |
| 442 | |
| 443 | deps := make([]string, 0) |
| 444 | for dep := range context.depCh { |
| 445 | deps = append(deps, dep) |
| 446 | } |
| 447 | |
| 448 | if !context.okay.Load() { |
Lukacs T. Berki | b21166e | 2021-04-21 11:58:36 +0200 | [diff] [blame] | 449 | os.Exit(1) |
| 450 | } |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 451 | |
| 452 | return deps |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 453 | } |