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 ( |
| 18 | "fmt" |
| 19 | "io/ioutil" |
| 20 | "os" |
| 21 | "path/filepath" |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 22 | "regexp" |
Cole Faust | 358ba4f | 2023-01-18 15:00:42 -0800 | [diff] [blame] | 23 | "sort" |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 24 | "sync" |
| 25 | "sync/atomic" |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 26 | |
| 27 | "android/soong/shared" |
| 28 | ) |
| 29 | |
| 30 | // A tree structure that describes what to do at each directory in the created |
| 31 | // symlink tree. Currently it is used to enumerate which files/directories |
| 32 | // should be excluded from symlinking. Each instance of "node" represents a file |
| 33 | // or a directory. If excluded is true, then that file/directory should be |
| 34 | // excluded from symlinking. Otherwise, the node is not excluded, but one of its |
| 35 | // descendants is (otherwise the node in question would not exist) |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 36 | |
| 37 | type instructionsNode struct { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 38 | name string |
| 39 | excluded bool // If false, this is just an intermediate node |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 40 | children map[string]*instructionsNode |
| 41 | } |
| 42 | |
| 43 | type symlinkForestContext struct { |
| 44 | verbose bool |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 45 | topdir string // $TOPDIR |
| 46 | |
| 47 | // State |
Usta Shrestha | da15c61 | 2022-11-08 14:12:36 -0500 | [diff] [blame] | 48 | wg sync.WaitGroup |
| 49 | depCh chan string |
| 50 | mkdirCount atomic.Uint64 |
| 51 | symlinkCount atomic.Uint64 |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 52 | } |
| 53 | |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 54 | // 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] | 55 | func ensureNodeExists(root *instructionsNode, path string) *instructionsNode { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 56 | if path == "" { |
| 57 | return root |
| 58 | } |
| 59 | |
| 60 | if path[len(path)-1] == '/' { |
| 61 | path = path[:len(path)-1] // filepath.Split() leaves a trailing slash |
| 62 | } |
| 63 | |
| 64 | dir, base := filepath.Split(path) |
| 65 | |
| 66 | // First compute the parent node... |
| 67 | dn := ensureNodeExists(root, dir) |
| 68 | |
| 69 | // then create the requested node as its direct child, if needed. |
| 70 | if child, ok := dn.children[base]; ok { |
| 71 | return child |
| 72 | } else { |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 73 | dn.children[base] = &instructionsNode{base, false, make(map[string]*instructionsNode)} |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 74 | return dn.children[base] |
| 75 | } |
| 76 | } |
| 77 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 78 | // Turns a list of paths to be excluded into a tree |
| 79 | func instructionsFromExcludePathList(paths []string) *instructionsNode { |
| 80 | result := &instructionsNode{"", false, make(map[string]*instructionsNode)} |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 81 | |
| 82 | for _, p := range paths { |
| 83 | ensureNodeExists(result, p).excluded = true |
| 84 | } |
| 85 | |
| 86 | return result |
| 87 | } |
| 88 | |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 89 | func mergeBuildFiles(output string, srcBuildFile string, generatedBuildFile string, verbose bool) error { |
| 90 | |
| 91 | srcBuildFileContent, err := os.ReadFile(srcBuildFile) |
| 92 | if err != nil { |
| 93 | return err |
| 94 | } |
| 95 | |
| 96 | generatedBuildFileContent, err := os.ReadFile(generatedBuildFile) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | |
| 101 | // There can't be a package() call in both the source and generated BUILD files. |
| 102 | // bp2build will generate a package() call for licensing information, but if |
| 103 | // there's no licensing information, it will still generate a package() call |
| 104 | // that just sets default_visibility=public. If the handcrafted build file |
| 105 | // also has a package() call, we'll allow it to override the bp2build |
| 106 | // generated one if it doesn't have any licensing information. If the bp2build |
| 107 | // one has licensing information and the handcrafted one exists, we'll leave |
| 108 | // them both in for bazel to throw an error. |
| 109 | packageRegex := regexp.MustCompile(`(?m)^package\s*\(`) |
| 110 | packageDefaultVisibilityRegex := regexp.MustCompile(`(?m)^package\s*\(\s*default_visibility\s*=\s*\[\s*"//visibility:public",?\s*]\s*\)`) |
| 111 | if packageRegex.Find(srcBuildFileContent) != nil { |
| 112 | if verbose && packageDefaultVisibilityRegex.Find(generatedBuildFileContent) != nil { |
| 113 | fmt.Fprintf(os.Stderr, "Both '%s' and '%s' have a package() target, removing the first one\n", |
| 114 | generatedBuildFile, srcBuildFile) |
| 115 | } |
| 116 | generatedBuildFileContent = packageDefaultVisibilityRegex.ReplaceAll(generatedBuildFileContent, []byte{}) |
| 117 | } |
| 118 | |
| 119 | outFile, err := os.Create(output) |
| 120 | if err != nil { |
| 121 | return err |
| 122 | } |
| 123 | |
| 124 | _, err = outFile.Write(generatedBuildFileContent) |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | |
| 129 | if generatedBuildFileContent[len(generatedBuildFileContent)-1] != '\n' { |
| 130 | _, err = outFile.WriteString("\n") |
| 131 | if err != nil { |
| 132 | return err |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | _, err = outFile.Write(srcBuildFileContent) |
| 137 | return err |
| 138 | } |
| 139 | |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 140 | // Calls readdir() and returns it as a map from the basename of the files in dir |
| 141 | // to os.FileInfo. |
| 142 | func readdirToMap(dir string) map[string]os.FileInfo { |
| 143 | entryList, err := ioutil.ReadDir(dir) |
| 144 | result := make(map[string]os.FileInfo) |
| 145 | |
| 146 | if err != nil { |
| 147 | if os.IsNotExist(err) { |
| 148 | // It's okay if a directory doesn't exist; it just means that one of the |
| 149 | // trees to be merged contains parts the other doesn't |
| 150 | return result |
| 151 | } else { |
| 152 | fmt.Fprintf(os.Stderr, "Cannot readdir '%s': %s\n", dir, err) |
| 153 | os.Exit(1) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | for _, fi := range entryList { |
| 158 | result[fi.Name()] = fi |
| 159 | } |
| 160 | |
| 161 | return result |
| 162 | } |
| 163 | |
| 164 | // Creates a symbolic link at dst pointing to src |
Usta (Tsering) Shrestha | c4c07b1 | 2022-11-08 18:31:14 -0500 | [diff] [blame^] | 165 | func symlinkIntoForest(topdir, dst, src string) uint64 { |
| 166 | srcPath := shared.JoinPath(topdir, src) |
| 167 | dstPath := shared.JoinPath(topdir, dst) |
| 168 | |
| 169 | // Check if a symlink already exists. |
| 170 | if dstInfo, err := os.Lstat(dstPath); err != nil { |
| 171 | if !os.IsNotExist(err) { |
| 172 | fmt.Fprintf(os.Stderr, "Failed to lstat '%s': %s", dst, err) |
| 173 | os.Exit(1) |
| 174 | } |
| 175 | } else { |
| 176 | if dstInfo.Mode()&os.ModeSymlink != 0 { |
| 177 | // Assume that the link's target is correct, i.e. no manual tampering. |
| 178 | // E.g. OUT_DIR could have been previously used with a different source tree check-out! |
| 179 | return 0 |
| 180 | } else { |
| 181 | if err := os.RemoveAll(dstPath); err != nil { |
| 182 | fmt.Fprintf(os.Stderr, "Failed to remove '%s': %s", dst, err) |
| 183 | os.Exit(1) |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Create symlink. |
| 189 | if err := os.Symlink(srcPath, dstPath); err != nil { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 190 | fmt.Fprintf(os.Stderr, "Cannot create symlink at '%s' pointing to '%s': %s", dst, src, err) |
| 191 | os.Exit(1) |
| 192 | } |
Usta (Tsering) Shrestha | c4c07b1 | 2022-11-08 18:31:14 -0500 | [diff] [blame^] | 193 | return 1 |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 194 | } |
| 195 | |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 196 | func isDir(path string, fi os.FileInfo) bool { |
| 197 | if (fi.Mode() & os.ModeSymlink) != os.ModeSymlink { |
| 198 | return fi.IsDir() |
| 199 | } |
| 200 | |
Jingwen Chen | d4b1dc8 | 2022-05-12 11:08:03 +0000 | [diff] [blame] | 201 | fi2, statErr := os.Stat(path) |
| 202 | if statErr == nil { |
| 203 | return fi2.IsDir() |
| 204 | } |
| 205 | |
| 206 | // Check if this is a dangling symlink. If so, treat it like a file, not a dir. |
| 207 | _, lstatErr := os.Lstat(path) |
| 208 | if lstatErr != nil { |
| 209 | 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] | 210 | os.Exit(1) |
| 211 | } |
| 212 | |
Jingwen Chen | d4b1dc8 | 2022-05-12 11:08:03 +0000 | [diff] [blame] | 213 | return false |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 216 | // Recursively plants a symlink forest at forestDir. The symlink tree will |
| 217 | // contain every file in buildFilesDir and srcDir excluding the files in |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 218 | // instructions. Collects every directory encountered during the traversal of |
| 219 | // srcDir . |
| 220 | 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] | 221 | defer context.wg.Done() |
| 222 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 223 | if instructions != nil && instructions.excluded { |
Usta (Tsering) Shrestha | c4c07b1 | 2022-11-08 18:31:14 -0500 | [diff] [blame^] | 224 | // Excluded paths are skipped at the level of the non-excluded parent. |
| 225 | fmt.Fprintf(os.Stderr, "may not specify a root-level exclude directory '%s'", srcDir) |
| 226 | os.Exit(1) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 229 | // We don't add buildFilesDir here because the bp2build files marker files is |
| 230 | // already a dependency which covers it. If we ever wanted to turn this into |
| 231 | // 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] | 232 | context.depCh <- srcDir |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 233 | |
| 234 | srcDirMap := readdirToMap(shared.JoinPath(context.topdir, srcDir)) |
| 235 | buildFilesMap := readdirToMap(shared.JoinPath(context.topdir, buildFilesDir)) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 236 | |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 237 | renamingBuildFile := false |
| 238 | if _, ok := srcDirMap["BUILD"]; ok { |
| 239 | if _, ok := srcDirMap["BUILD.bazel"]; !ok { |
| 240 | if _, ok := buildFilesMap["BUILD.bazel"]; ok { |
| 241 | renamingBuildFile = true |
| 242 | srcDirMap["BUILD.bazel"] = srcDirMap["BUILD"] |
| 243 | delete(srcDirMap, "BUILD") |
Usta (Tsering) Shrestha | c4c07b1 | 2022-11-08 18:31:14 -0500 | [diff] [blame^] | 244 | if instructions != nil { |
| 245 | if _, ok := instructions.children["BUILD"]; ok { |
| 246 | instructions.children["BUILD.bazel"] = instructions.children["BUILD"] |
| 247 | delete(instructions.children, "BUILD") |
| 248 | } |
| 249 | } |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
Cole Faust | 358ba4f | 2023-01-18 15:00:42 -0800 | [diff] [blame] | 254 | allEntries := make([]string, 0, len(srcDirMap)+len(buildFilesMap)) |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 255 | for n := range srcDirMap { |
Cole Faust | 358ba4f | 2023-01-18 15:00:42 -0800 | [diff] [blame] | 256 | allEntries = append(allEntries, n) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 257 | } |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 258 | for n := range buildFilesMap { |
Cole Faust | 358ba4f | 2023-01-18 15:00:42 -0800 | [diff] [blame] | 259 | if _, ok := srcDirMap[n]; !ok { |
| 260 | allEntries = append(allEntries, n) |
| 261 | } |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 262 | } |
Cole Faust | 358ba4f | 2023-01-18 15:00:42 -0800 | [diff] [blame] | 263 | // Tests read the error messages generated, so ensure their order is deterministic |
| 264 | sort.Strings(allEntries) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 265 | |
Usta (Tsering) Shrestha | c4c07b1 | 2022-11-08 18:31:14 -0500 | [diff] [blame^] | 266 | fullForestPath := shared.JoinPath(context.topdir, forestDir) |
| 267 | createForestDir := false |
| 268 | if fi, err := os.Lstat(fullForestPath); err != nil { |
| 269 | if os.IsNotExist(err) { |
| 270 | createForestDir = true |
| 271 | } else { |
| 272 | fmt.Fprintf(os.Stderr, "Could not read info for '%s': %s\n", forestDir, err) |
| 273 | } |
| 274 | } else if fi.Mode()&os.ModeDir == 0 { |
| 275 | if err := os.RemoveAll(fullForestPath); err != nil { |
| 276 | fmt.Fprintf(os.Stderr, "Failed to remove '%s': %s", forestDir, err) |
| 277 | os.Exit(1) |
| 278 | } |
| 279 | createForestDir = true |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 280 | } |
Usta (Tsering) Shrestha | c4c07b1 | 2022-11-08 18:31:14 -0500 | [diff] [blame^] | 281 | if createForestDir { |
| 282 | if err := os.MkdirAll(fullForestPath, 0777); err != nil { |
| 283 | fmt.Fprintf(os.Stderr, "Could not mkdir '%s': %s\n", forestDir, err) |
| 284 | os.Exit(1) |
| 285 | } |
| 286 | context.mkdirCount.Add(1) |
| 287 | } |
| 288 | |
| 289 | // Start with a list of items that already exist in the forest, and remove |
| 290 | // each element as it is processed in allEntries. Any remaining items in |
| 291 | // forestMapForDeletion must be removed. (This handles files which were |
| 292 | // removed since the previous forest generation). |
| 293 | forestMapForDeletion := readdirToMap(shared.JoinPath(context.topdir, forestDir)) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 294 | |
Cole Faust | 358ba4f | 2023-01-18 15:00:42 -0800 | [diff] [blame] | 295 | for _, f := range allEntries { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 296 | if f[0] == '.' { |
| 297 | continue // Ignore dotfiles |
| 298 | } |
Usta (Tsering) Shrestha | c4c07b1 | 2022-11-08 18:31:14 -0500 | [diff] [blame^] | 299 | delete(forestMapForDeletion, f) |
| 300 | // todo add deletionCount metric |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 301 | |
| 302 | // 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] | 303 | forestChild := shared.JoinPath(forestDir, f) |
| 304 | srcChild := shared.JoinPath(srcDir, f) |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 305 | if f == "BUILD.bazel" && renamingBuildFile { |
| 306 | srcChild = shared.JoinPath(srcDir, "BUILD") |
| 307 | } |
Lukacs T. Berki | 3f9416e | 2021-04-20 13:08:11 +0200 | [diff] [blame] | 308 | buildFilesChild := shared.JoinPath(buildFilesDir, f) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 309 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 310 | // Descend in the instruction tree if it exists |
Usta (Tsering) Shrestha | c4c07b1 | 2022-11-08 18:31:14 -0500 | [diff] [blame^] | 311 | var instructionsChild *instructionsNode |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 312 | if instructions != nil { |
Usta (Tsering) Shrestha | c4c07b1 | 2022-11-08 18:31:14 -0500 | [diff] [blame^] | 313 | instructionsChild = instructions.children[f] |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Lukacs T. Berki | 3f9416e | 2021-04-20 13:08:11 +0200 | [diff] [blame] | 316 | srcChildEntry, sExists := srcDirMap[f] |
| 317 | buildFilesChildEntry, bExists := buildFilesMap[f] |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 318 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 319 | if instructionsChild != nil && instructionsChild.excluded { |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 320 | if bExists { |
Usta (Tsering) Shrestha | c4c07b1 | 2022-11-08 18:31:14 -0500 | [diff] [blame^] | 321 | context.symlinkCount.Add(symlinkIntoForest(context.topdir, forestChild, buildFilesChild)) |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 322 | } |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 323 | continue |
| 324 | } |
| 325 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 326 | sDir := sExists && isDir(shared.JoinPath(context.topdir, srcChild), srcChildEntry) |
| 327 | bDir := bExists && isDir(shared.JoinPath(context.topdir, buildFilesChild), buildFilesChildEntry) |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 328 | |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 329 | if !sExists { |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 330 | if bDir && instructionsChild != nil { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 331 | // Not in the source tree, but we have to exclude something from under |
| 332 | // this subtree, so descend |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 333 | context.wg.Add(1) |
| 334 | go plantSymlinkForestRecursive(context, instructionsChild, forestChild, buildFilesChild, srcChild) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 335 | } else { |
| 336 | // Not in the source tree, symlink BUILD file |
Usta (Tsering) Shrestha | c4c07b1 | 2022-11-08 18:31:14 -0500 | [diff] [blame^] | 337 | context.symlinkCount.Add(symlinkIntoForest(context.topdir, forestChild, buildFilesChild)) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 338 | } |
| 339 | } else if !bExists { |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 340 | if sDir && instructionsChild != nil { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 341 | // Not in the build file tree, but we have to exclude something from |
| 342 | // under this subtree, so descend |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 343 | context.wg.Add(1) |
| 344 | go plantSymlinkForestRecursive(context, instructionsChild, forestChild, buildFilesChild, srcChild) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 345 | } else { |
| 346 | // Not in the build file tree, symlink source tree, carry on |
Usta (Tsering) Shrestha | c4c07b1 | 2022-11-08 18:31:14 -0500 | [diff] [blame^] | 347 | context.symlinkCount.Add(symlinkIntoForest(context.topdir, forestChild, srcChild)) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 348 | } |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 349 | } else if sDir && bDir { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 350 | // Both are directories. Descend. |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 351 | context.wg.Add(1) |
| 352 | go plantSymlinkForestRecursive(context, instructionsChild, forestChild, buildFilesChild, srcChild) |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 353 | } else if !sDir && !bDir { |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 354 | // Neither is a directory. Merge them. |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 355 | srcBuildFile := shared.JoinPath(context.topdir, srcChild) |
| 356 | generatedBuildFile := shared.JoinPath(context.topdir, buildFilesChild) |
Usta Shrestha | 783ec5c | 2022-10-25 01:22:36 -0400 | [diff] [blame] | 357 | // The Android.bp file that codegen used to produce `buildFilesChild` is |
| 358 | // already a dependency, we can ignore `buildFilesChild`. |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 359 | context.depCh <- srcChild |
Usta (Tsering) Shrestha | c4c07b1 | 2022-11-08 18:31:14 -0500 | [diff] [blame^] | 360 | if err := mergeBuildFiles(shared.JoinPath(context.topdir, forestChild), srcBuildFile, generatedBuildFile, context.verbose); err != nil { |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 361 | fmt.Fprintf(os.Stderr, "Error merging %s and %s: %s", |
| 362 | srcBuildFile, generatedBuildFile, err) |
Usta Shrestha | 071f6c2 | 2022-10-25 12:34:06 -0400 | [diff] [blame] | 363 | os.Exit(1) |
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) |
Usta Shrestha | 071f6c2 | 2022-10-25 12:34:06 -0400 | [diff] [blame] | 370 | os.Exit(1) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 371 | } |
| 372 | } |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 373 | |
Usta (Tsering) Shrestha | c4c07b1 | 2022-11-08 18:31:14 -0500 | [diff] [blame^] | 374 | // Remove all files in the forest that exist in neither the source |
| 375 | // tree nor the build files tree. (This handles files which were removed |
| 376 | // since the previous forest generation). |
| 377 | for f := range forestMapForDeletion { |
| 378 | var instructionsChild *instructionsNode |
| 379 | if instructions != nil { |
| 380 | instructionsChild = instructions.children[f] |
Lukacs T. Berki | bc5f731 | 2022-10-31 09:01:34 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Usta (Tsering) Shrestha | c4c07b1 | 2022-11-08 18:31:14 -0500 | [diff] [blame^] | 383 | if instructionsChild != nil && instructionsChild.excluded { |
| 384 | // This directory may be excluded because bazel writes to it under the |
| 385 | // forest root. Thus this path is intentionally left alone. |
| 386 | continue |
| 387 | } |
| 388 | forestChild := shared.JoinPath(context.topdir, forestDir, f) |
| 389 | if err := os.RemoveAll(forestChild); err != nil { |
| 390 | fmt.Fprintf(os.Stderr, "Failed to remove '%s/%s': %s", forestDir, f, err) |
Lukacs T. Berki | bc5f731 | 2022-10-31 09:01:34 +0000 | [diff] [blame] | 391 | os.Exit(1) |
| 392 | } |
Lukacs T. Berki | bc5f731 | 2022-10-31 09:01:34 +0000 | [diff] [blame] | 393 | } |
Lukacs T. Berki | bc5f731 | 2022-10-31 09:01:34 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Usta Shrestha | da15c61 | 2022-11-08 14:12:36 -0500 | [diff] [blame] | 396 | // 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] | 397 | // "srcDir" while excluding paths listed in "exclude". Returns the set of paths |
| 398 | // under srcDir on which readdir() had to be called to produce the symlink |
| 399 | // forest. |
Usta Shrestha | da15c61 | 2022-11-08 14:12:36 -0500 | [diff] [blame] | 400 | 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] | 401 | context := &symlinkForestContext{ |
Usta Shrestha | da15c61 | 2022-11-08 14:12:36 -0500 | [diff] [blame] | 402 | verbose: verbose, |
| 403 | topdir: topdir, |
| 404 | depCh: make(chan string), |
| 405 | mkdirCount: atomic.Uint64{}, |
| 406 | symlinkCount: atomic.Uint64{}, |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 409 | instructions := instructionsFromExcludePathList(exclude) |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 410 | go func() { |
| 411 | context.wg.Add(1) |
| 412 | plantSymlinkForestRecursive(context, instructions, forest, buildFiles, ".") |
| 413 | context.wg.Wait() |
| 414 | close(context.depCh) |
| 415 | }() |
| 416 | |
Lukacs T. Berki | 647e7ab | 2022-10-27 09:55:38 +0000 | [diff] [blame] | 417 | for dep := range context.depCh { |
| 418 | deps = append(deps, dep) |
| 419 | } |
| 420 | |
Usta Shrestha | da15c61 | 2022-11-08 14:12:36 -0500 | [diff] [blame] | 421 | return deps, context.mkdirCount.Load(), context.symlinkCount.Load() |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 422 | } |