Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 1 | package bp2build |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io/ioutil" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | |
| 9 | "android/soong/shared" |
| 10 | ) |
| 11 | |
| 12 | // A tree structure that describes what to do at each directory in the created |
| 13 | // symlink tree. Currently it is used to enumerate which files/directories |
| 14 | // should be excluded from symlinking. Each instance of "node" represents a file |
| 15 | // or a directory. If excluded is true, then that file/directory should be |
| 16 | // excluded from symlinking. Otherwise, the node is not excluded, but one of its |
| 17 | // descendants is (otherwise the node in question would not exist) |
| 18 | type node struct { |
| 19 | name string |
| 20 | excluded bool // If false, this is just an intermediate node |
| 21 | children map[string]*node |
| 22 | } |
| 23 | |
| 24 | // Ensures that the a node for the given path exists in the tree and returns it. |
| 25 | func ensureNodeExists(root *node, path string) *node { |
| 26 | if path == "" { |
| 27 | return root |
| 28 | } |
| 29 | |
| 30 | if path[len(path)-1] == '/' { |
| 31 | path = path[:len(path)-1] // filepath.Split() leaves a trailing slash |
| 32 | } |
| 33 | |
| 34 | dir, base := filepath.Split(path) |
| 35 | |
| 36 | // First compute the parent node... |
| 37 | dn := ensureNodeExists(root, dir) |
| 38 | |
| 39 | // then create the requested node as its direct child, if needed. |
| 40 | if child, ok := dn.children[base]; ok { |
| 41 | return child |
| 42 | } else { |
| 43 | dn.children[base] = &node{base, false, make(map[string]*node)} |
| 44 | return dn.children[base] |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Turns a list of paths to be excluded into a tree made of "node" objects where |
| 49 | // the specified paths are marked as excluded. |
| 50 | func treeFromExcludePathList(paths []string) *node { |
| 51 | result := &node{"", false, make(map[string]*node)} |
| 52 | |
| 53 | for _, p := range paths { |
| 54 | ensureNodeExists(result, p).excluded = true |
| 55 | } |
| 56 | |
| 57 | return result |
| 58 | } |
| 59 | |
| 60 | // Calls readdir() and returns it as a map from the basename of the files in dir |
| 61 | // to os.FileInfo. |
| 62 | func readdirToMap(dir string) map[string]os.FileInfo { |
| 63 | entryList, err := ioutil.ReadDir(dir) |
| 64 | result := make(map[string]os.FileInfo) |
| 65 | |
| 66 | if err != nil { |
| 67 | if os.IsNotExist(err) { |
| 68 | // It's okay if a directory doesn't exist; it just means that one of the |
| 69 | // trees to be merged contains parts the other doesn't |
| 70 | return result |
| 71 | } else { |
| 72 | fmt.Fprintf(os.Stderr, "Cannot readdir '%s': %s\n", dir, err) |
| 73 | os.Exit(1) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | for _, fi := range entryList { |
| 78 | result[fi.Name()] = fi |
| 79 | } |
| 80 | |
| 81 | return result |
| 82 | } |
| 83 | |
| 84 | // Creates a symbolic link at dst pointing to src |
| 85 | func symlinkIntoForest(topdir, dst, src string) { |
| 86 | err := os.Symlink(shared.JoinPath(topdir, src), shared.JoinPath(topdir, dst)) |
| 87 | if err != nil { |
| 88 | fmt.Fprintf(os.Stderr, "Cannot create symlink at '%s' pointing to '%s': %s", dst, src, err) |
| 89 | os.Exit(1) |
| 90 | } |
| 91 | } |
| 92 | |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 93 | func isDir(path string, fi os.FileInfo) bool { |
| 94 | if (fi.Mode() & os.ModeSymlink) != os.ModeSymlink { |
| 95 | return fi.IsDir() |
| 96 | } |
| 97 | |
Jingwen Chen | d4b1dc8 | 2022-05-12 11:08:03 +0000 | [diff] [blame] | 98 | fi2, statErr := os.Stat(path) |
| 99 | if statErr == nil { |
| 100 | return fi2.IsDir() |
| 101 | } |
| 102 | |
| 103 | // Check if this is a dangling symlink. If so, treat it like a file, not a dir. |
| 104 | _, lstatErr := os.Lstat(path) |
| 105 | if lstatErr != nil { |
| 106 | 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] | 107 | os.Exit(1) |
| 108 | } |
| 109 | |
Jingwen Chen | d4b1dc8 | 2022-05-12 11:08:03 +0000 | [diff] [blame] | 110 | return false |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 113 | // Recursively plants a symlink forest at forestDir. The symlink tree will |
| 114 | // contain every file in buildFilesDir and srcDir excluding the files in |
| 115 | // exclude. Collects every directory encountered during the traversal of srcDir |
| 116 | // into acc. |
Lukacs T. Berki | b21166e | 2021-04-21 11:58:36 +0200 | [diff] [blame] | 117 | func plantSymlinkForestRecursive(topdir string, forestDir string, buildFilesDir string, srcDir string, exclude *node, acc *[]string, okay *bool) { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 118 | if exclude != nil && exclude.excluded { |
| 119 | // This directory is not needed, bail out |
| 120 | return |
| 121 | } |
| 122 | |
| 123 | *acc = append(*acc, srcDir) |
| 124 | srcDirMap := readdirToMap(shared.JoinPath(topdir, srcDir)) |
| 125 | buildFilesMap := readdirToMap(shared.JoinPath(topdir, buildFilesDir)) |
| 126 | |
| 127 | allEntries := make(map[string]bool) |
| 128 | for n, _ := range srcDirMap { |
| 129 | allEntries[n] = true |
| 130 | } |
| 131 | |
| 132 | for n, _ := range buildFilesMap { |
| 133 | allEntries[n] = true |
| 134 | } |
| 135 | |
| 136 | err := os.MkdirAll(shared.JoinPath(topdir, forestDir), 0777) |
| 137 | if err != nil { |
| 138 | fmt.Fprintf(os.Stderr, "Cannot mkdir '%s': %s\n", forestDir, err) |
| 139 | os.Exit(1) |
| 140 | } |
| 141 | |
| 142 | for f, _ := range allEntries { |
| 143 | if f[0] == '.' { |
| 144 | continue // Ignore dotfiles |
| 145 | } |
| 146 | |
| 147 | // 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] | 148 | forestChild := shared.JoinPath(forestDir, f) |
| 149 | srcChild := shared.JoinPath(srcDir, f) |
| 150 | buildFilesChild := shared.JoinPath(buildFilesDir, f) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 151 | |
| 152 | // Descend in the exclusion tree, if there are any excludes left |
Lukacs T. Berki | 3f9416e | 2021-04-20 13:08:11 +0200 | [diff] [blame] | 153 | var excludeChild *node |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 154 | if exclude == nil { |
Lukacs T. Berki | 3f9416e | 2021-04-20 13:08:11 +0200 | [diff] [blame] | 155 | excludeChild = nil |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 156 | } else { |
Lukacs T. Berki | 3f9416e | 2021-04-20 13:08:11 +0200 | [diff] [blame] | 157 | excludeChild = exclude.children[f] |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 158 | } |
| 159 | |
Lukacs T. Berki | 3f9416e | 2021-04-20 13:08:11 +0200 | [diff] [blame] | 160 | srcChildEntry, sExists := srcDirMap[f] |
| 161 | buildFilesChildEntry, bExists := buildFilesMap[f] |
| 162 | excluded := excludeChild != nil && excludeChild.excluded |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 163 | |
| 164 | if excluded { |
| 165 | continue |
| 166 | } |
| 167 | |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 168 | sDir := false |
| 169 | bDir := false |
| 170 | if sExists { |
| 171 | sDir = isDir(shared.JoinPath(topdir, srcChild), srcChildEntry) |
| 172 | } |
| 173 | |
| 174 | if bExists { |
| 175 | bDir = isDir(shared.JoinPath(topdir, buildFilesChild), buildFilesChildEntry) |
| 176 | } |
| 177 | |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 178 | if !sExists { |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 179 | if bDir && excludeChild != nil { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 180 | // Not in the source tree, but we have to exclude something from under |
| 181 | // this subtree, so descend |
Lukacs T. Berki | b21166e | 2021-04-21 11:58:36 +0200 | [diff] [blame] | 182 | plantSymlinkForestRecursive(topdir, forestChild, buildFilesChild, srcChild, excludeChild, acc, okay) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 183 | } else { |
| 184 | // Not in the source tree, symlink BUILD file |
Lukacs T. Berki | 3f9416e | 2021-04-20 13:08:11 +0200 | [diff] [blame] | 185 | symlinkIntoForest(topdir, forestChild, buildFilesChild) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 186 | } |
| 187 | } else if !bExists { |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 188 | if sDir && excludeChild != nil { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 189 | // Not in the build file tree, but we have to exclude something from |
| 190 | // under this subtree, so descend |
Lukacs T. Berki | b21166e | 2021-04-21 11:58:36 +0200 | [diff] [blame] | 191 | plantSymlinkForestRecursive(topdir, forestChild, buildFilesChild, srcChild, excludeChild, acc, okay) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 192 | } else { |
| 193 | // Not in the build file tree, symlink source tree, carry on |
Lukacs T. Berki | 3f9416e | 2021-04-20 13:08:11 +0200 | [diff] [blame] | 194 | symlinkIntoForest(topdir, forestChild, srcChild) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 195 | } |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 196 | } else if sDir && bDir { |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 197 | // Both are directories. Descend. |
Lukacs T. Berki | b21166e | 2021-04-21 11:58:36 +0200 | [diff] [blame] | 198 | plantSymlinkForestRecursive(topdir, forestChild, buildFilesChild, srcChild, excludeChild, acc, okay) |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame] | 199 | } else if !sDir && !bDir { |
Lukacs T. Berki | b21166e | 2021-04-21 11:58:36 +0200 | [diff] [blame] | 200 | // Neither is a directory. Prioritize BUILD files generated by bp2build |
| 201 | // over any BUILD file imported into external/. |
| 202 | fmt.Fprintf(os.Stderr, "Both '%s' and '%s' exist, symlinking the former to '%s'\n", |
| 203 | buildFilesChild, srcChild, forestChild) |
| 204 | symlinkIntoForest(topdir, forestChild, buildFilesChild) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 205 | } else { |
| 206 | // Both exist and one is a file. This is an error. |
| 207 | fmt.Fprintf(os.Stderr, |
Lukacs T. Berki | b21166e | 2021-04-21 11:58:36 +0200 | [diff] [blame] | 208 | "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] | 209 | srcChild, buildFilesChild) |
Lukacs T. Berki | b21166e | 2021-04-21 11:58:36 +0200 | [diff] [blame] | 210 | *okay = false |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // Creates a symlink forest by merging the directory tree at "buildFiles" and |
| 216 | // "srcDir" while excluding paths listed in "exclude". Returns the set of paths |
| 217 | // under srcDir on which readdir() had to be called to produce the symlink |
| 218 | // forest. |
| 219 | func PlantSymlinkForest(topdir string, forest string, buildFiles string, srcDir string, exclude []string) []string { |
| 220 | deps := make([]string, 0) |
| 221 | os.RemoveAll(shared.JoinPath(topdir, forest)) |
| 222 | excludeTree := treeFromExcludePathList(exclude) |
Lukacs T. Berki | b21166e | 2021-04-21 11:58:36 +0200 | [diff] [blame] | 223 | okay := true |
| 224 | plantSymlinkForestRecursive(topdir, forest, buildFiles, srcDir, excludeTree, &deps, &okay) |
| 225 | if !okay { |
| 226 | os.Exit(1) |
| 227 | } |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 228 | return deps |
| 229 | } |