Convert absolute source path to relative path in PRODUCT_COPY_FILES processing
Source paths should be relative to the top in most builds, but this is
not true for some downstream builds. Kati does not restrict this, but
Soong enforces that all source paths are relative and leads to error for
these downstream branches. This change deals with such issue by
converting the absolute paths to relative paths.
Test: m nothing --no-skip-soong-tests
Bug: 378298832
Change-Id: I313b3827417c9672b38737e8fe846bb7521b17d1
diff --git a/android/paths.go b/android/paths.go
index 9cb872d..1486ad2 100644
--- a/android/paths.go
+++ b/android/paths.go
@@ -2589,3 +2589,19 @@
}
return false
}
+
+// ToRelativeSourcePath converts absolute source path to the path relative to the source root.
+// This throws an error if the input path is outside of the source root and cannot be converted
+// to the relative path.
+// This should be rarely used given that the source path is relative in Soong.
+func ToRelativeSourcePath(ctx PathContext, path string) string {
+ ret := path
+ if filepath.IsAbs(path) {
+ relPath, err := filepath.Rel(absSrcDir, path)
+ if err != nil || strings.HasPrefix(relPath, "..") {
+ ReportPathErrorf(ctx, "%s is outside of the source root", path)
+ }
+ ret = relPath
+ }
+ return ret
+}