Change symlink_forest to use relative symlinks.
Also add script to remove the Bazel output base.
This will assist with supporting movable checkouts alongside
mixed builds.
Bug: 259191764
Test: m && (move topic and prepare_moved_top.sh) && m
Test: m && prepare_moved_top.sh && m
Test: build/soong/tests/relative_symlinks_test.sh
Change-Id: I0f53da8d99f752fad496cf3ac61b01f001b7296d
diff --git a/bp2build/symlink_forest.go b/bp2build/symlink_forest.go
index 5c33308..a0c7e4c 100644
--- a/bp2build/symlink_forest.go
+++ b/bp2build/symlink_forest.go
@@ -190,10 +190,20 @@
// Creates a symbolic link at dst pointing to src
func symlinkIntoForest(topdir, dst, src string) uint64 {
- srcPath := shared.JoinPath(topdir, src)
- dstPath := shared.JoinPath(topdir, dst)
+ // b/259191764 - Make all symlinks relative
+ dst = shared.JoinPath(topdir, dst)
+ src = shared.JoinPath(topdir, src)
+ basePath := filepath.Dir(dst)
+ var dstPath string
+ srcPath, err := filepath.Rel(basePath, src)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Failed to find relative path for symlinking: %s\n", err)
+ os.Exit(1)
+ } else {
+ dstPath = dst
+ }
- // Check if a symlink already exists.
+ // Check whether a symlink already exists.
if dstInfo, err := os.Lstat(dstPath); err != nil {
if !os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Failed to lstat '%s': %s", dst, err)