blob: 818d7aece3fc101ed5c874a82b04af135a1e1e3f [file] [log] [blame]
Lukacs T. Berkib353cca2021-04-16 13:47:36 +02001package bp2build
2
3import (
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)
18type 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.
25func 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.
50func 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.
62func 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
85func 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. Berkie3487c82022-05-02 10:13:19 +020093func isDir(path string, fi os.FileInfo) bool {
94 if (fi.Mode() & os.ModeSymlink) != os.ModeSymlink {
95 return fi.IsDir()
96 }
97
Jingwen Chend4b1dc82022-05-12 11:08:03 +000098 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. Berkie3487c82022-05-02 10:13:19 +0200107 os.Exit(1)
108 }
109
Jingwen Chend4b1dc82022-05-12 11:08:03 +0000110 return false
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200111}
112
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200113// 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. Berkib21166e2021-04-21 11:58:36 +0200117func plantSymlinkForestRecursive(topdir string, forestDir string, buildFilesDir string, srcDir string, exclude *node, acc *[]string, okay *bool) {
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200118 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. Berki3f9416e2021-04-20 13:08:11 +0200148 forestChild := shared.JoinPath(forestDir, f)
149 srcChild := shared.JoinPath(srcDir, f)
150 buildFilesChild := shared.JoinPath(buildFilesDir, f)
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200151
152 // Descend in the exclusion tree, if there are any excludes left
Lukacs T. Berki3f9416e2021-04-20 13:08:11 +0200153 var excludeChild *node
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200154 if exclude == nil {
Lukacs T. Berki3f9416e2021-04-20 13:08:11 +0200155 excludeChild = nil
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200156 } else {
Lukacs T. Berki3f9416e2021-04-20 13:08:11 +0200157 excludeChild = exclude.children[f]
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200158 }
159
Lukacs T. Berki3f9416e2021-04-20 13:08:11 +0200160 srcChildEntry, sExists := srcDirMap[f]
161 buildFilesChildEntry, bExists := buildFilesMap[f]
162 excluded := excludeChild != nil && excludeChild.excluded
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200163
164 if excluded {
165 continue
166 }
167
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200168 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. Berkib353cca2021-04-16 13:47:36 +0200178 if !sExists {
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200179 if bDir && excludeChild != nil {
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200180 // Not in the source tree, but we have to exclude something from under
181 // this subtree, so descend
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200182 plantSymlinkForestRecursive(topdir, forestChild, buildFilesChild, srcChild, excludeChild, acc, okay)
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200183 } else {
184 // Not in the source tree, symlink BUILD file
Lukacs T. Berki3f9416e2021-04-20 13:08:11 +0200185 symlinkIntoForest(topdir, forestChild, buildFilesChild)
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200186 }
187 } else if !bExists {
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200188 if sDir && excludeChild != nil {
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200189 // Not in the build file tree, but we have to exclude something from
190 // under this subtree, so descend
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200191 plantSymlinkForestRecursive(topdir, forestChild, buildFilesChild, srcChild, excludeChild, acc, okay)
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200192 } else {
193 // Not in the build file tree, symlink source tree, carry on
Lukacs T. Berki3f9416e2021-04-20 13:08:11 +0200194 symlinkIntoForest(topdir, forestChild, srcChild)
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200195 }
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200196 } else if sDir && bDir {
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200197 // Both are directories. Descend.
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200198 plantSymlinkForestRecursive(topdir, forestChild, buildFilesChild, srcChild, excludeChild, acc, okay)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200199 } else if !sDir && !bDir {
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200200 // 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. Berkib353cca2021-04-16 13:47:36 +0200205 } else {
206 // Both exist and one is a file. This is an error.
207 fmt.Fprintf(os.Stderr,
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200208 "Conflict in workspace symlink tree creation: both '%s' and '%s' exist and exactly one is a directory\n",
Lukacs T. Berki3f9416e2021-04-20 13:08:11 +0200209 srcChild, buildFilesChild)
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200210 *okay = false
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200211 }
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.
219func 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. Berkib21166e2021-04-21 11:58:36 +0200223 okay := true
224 plantSymlinkForestRecursive(topdir, forest, buildFiles, srcDir, excludeTree, &deps, &okay)
225 if !okay {
226 os.Exit(1)
227 }
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200228 return deps
229}