blob: 7d48191104bfa6e132273c5cbbda7be80f36808e [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
98 fi2, err := os.Stat(path)
99 if err != nil {
100 fmt.Fprintf(os.Stderr, "Cannot stat '%s': %s\n", path, err)
101 os.Exit(1)
102 }
103
104 return fi2.IsDir()
105}
106
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200107// Recursively plants a symlink forest at forestDir. The symlink tree will
108// contain every file in buildFilesDir and srcDir excluding the files in
109// exclude. Collects every directory encountered during the traversal of srcDir
110// into acc.
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200111func plantSymlinkForestRecursive(topdir string, forestDir string, buildFilesDir string, srcDir string, exclude *node, acc *[]string, okay *bool) {
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200112 if exclude != nil && exclude.excluded {
113 // This directory is not needed, bail out
114 return
115 }
116
117 *acc = append(*acc, srcDir)
118 srcDirMap := readdirToMap(shared.JoinPath(topdir, srcDir))
119 buildFilesMap := readdirToMap(shared.JoinPath(topdir, buildFilesDir))
120
121 allEntries := make(map[string]bool)
122 for n, _ := range srcDirMap {
123 allEntries[n] = true
124 }
125
126 for n, _ := range buildFilesMap {
127 allEntries[n] = true
128 }
129
130 err := os.MkdirAll(shared.JoinPath(topdir, forestDir), 0777)
131 if err != nil {
132 fmt.Fprintf(os.Stderr, "Cannot mkdir '%s': %s\n", forestDir, err)
133 os.Exit(1)
134 }
135
136 for f, _ := range allEntries {
137 if f[0] == '.' {
138 continue // Ignore dotfiles
139 }
140
141 // The full paths of children in the input trees and in the output tree
Lukacs T. Berki3f9416e2021-04-20 13:08:11 +0200142 forestChild := shared.JoinPath(forestDir, f)
143 srcChild := shared.JoinPath(srcDir, f)
144 buildFilesChild := shared.JoinPath(buildFilesDir, f)
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200145
146 // Descend in the exclusion tree, if there are any excludes left
Lukacs T. Berki3f9416e2021-04-20 13:08:11 +0200147 var excludeChild *node
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200148 if exclude == nil {
Lukacs T. Berki3f9416e2021-04-20 13:08:11 +0200149 excludeChild = nil
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200150 } else {
Lukacs T. Berki3f9416e2021-04-20 13:08:11 +0200151 excludeChild = exclude.children[f]
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200152 }
153
Lukacs T. Berki3f9416e2021-04-20 13:08:11 +0200154 srcChildEntry, sExists := srcDirMap[f]
155 buildFilesChildEntry, bExists := buildFilesMap[f]
156 excluded := excludeChild != nil && excludeChild.excluded
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200157
158 if excluded {
159 continue
160 }
161
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200162 sDir := false
163 bDir := false
164 if sExists {
165 sDir = isDir(shared.JoinPath(topdir, srcChild), srcChildEntry)
166 }
167
168 if bExists {
169 bDir = isDir(shared.JoinPath(topdir, buildFilesChild), buildFilesChildEntry)
170 }
171
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200172 if !sExists {
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200173 if bDir && excludeChild != nil {
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200174 // Not in the source tree, but we have to exclude something from under
175 // this subtree, so descend
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200176 plantSymlinkForestRecursive(topdir, forestChild, buildFilesChild, srcChild, excludeChild, acc, okay)
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200177 } else {
178 // Not in the source tree, symlink BUILD file
Lukacs T. Berki3f9416e2021-04-20 13:08:11 +0200179 symlinkIntoForest(topdir, forestChild, buildFilesChild)
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200180 }
181 } else if !bExists {
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200182 if sDir && excludeChild != nil {
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200183 // Not in the build file tree, but we have to exclude something from
184 // under this subtree, so descend
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200185 plantSymlinkForestRecursive(topdir, forestChild, buildFilesChild, srcChild, excludeChild, acc, okay)
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200186 } else {
187 // Not in the build file tree, symlink source tree, carry on
Lukacs T. Berki3f9416e2021-04-20 13:08:11 +0200188 symlinkIntoForest(topdir, forestChild, srcChild)
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200189 }
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200190 } else if sDir && bDir {
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200191 // Both are directories. Descend.
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200192 plantSymlinkForestRecursive(topdir, forestChild, buildFilesChild, srcChild, excludeChild, acc, okay)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200193 } else if !sDir && !bDir {
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200194 // Neither is a directory. Prioritize BUILD files generated by bp2build
195 // over any BUILD file imported into external/.
196 fmt.Fprintf(os.Stderr, "Both '%s' and '%s' exist, symlinking the former to '%s'\n",
197 buildFilesChild, srcChild, forestChild)
198 symlinkIntoForest(topdir, forestChild, buildFilesChild)
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200199 } else {
200 // Both exist and one is a file. This is an error.
201 fmt.Fprintf(os.Stderr,
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200202 "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 +0200203 srcChild, buildFilesChild)
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200204 *okay = false
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200205 }
206 }
207}
208
209// Creates a symlink forest by merging the directory tree at "buildFiles" and
210// "srcDir" while excluding paths listed in "exclude". Returns the set of paths
211// under srcDir on which readdir() had to be called to produce the symlink
212// forest.
213func PlantSymlinkForest(topdir string, forest string, buildFiles string, srcDir string, exclude []string) []string {
214 deps := make([]string, 0)
215 os.RemoveAll(shared.JoinPath(topdir, forest))
216 excludeTree := treeFromExcludePathList(exclude)
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200217 okay := true
218 plantSymlinkForestRecursive(topdir, forest, buildFiles, srcDir, excludeTree, &deps, &okay)
219 if !okay {
220 os.Exit(1)
221 }
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200222 return deps
223}