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