Merge changes I464e6ebd,Iaf7655e4 am: 3536614edd
Original change: https://android-review.googlesource.com/c/platform/build/soong/+/1826777 Change-Id: I7e03d04afbc809544d964548caa1646bc36aa353
This commit is contained in:
@@ -263,38 +263,56 @@ func ResPathWithName(ctx ModuleOutPathContext, p Path, name string) ModuleResPat
|
|||||||
|
|
||||||
// OptionalPath is a container that may or may not contain a valid Path.
|
// OptionalPath is a container that may or may not contain a valid Path.
|
||||||
type OptionalPath struct {
|
type OptionalPath struct {
|
||||||
valid bool
|
path Path // nil if invalid.
|
||||||
path Path
|
invalidReason string // Not applicable if path != nil. "" if the reason is unknown.
|
||||||
}
|
}
|
||||||
|
|
||||||
// OptionalPathForPath returns an OptionalPath containing the path.
|
// OptionalPathForPath returns an OptionalPath containing the path.
|
||||||
func OptionalPathForPath(path Path) OptionalPath {
|
func OptionalPathForPath(path Path) OptionalPath {
|
||||||
if path == nil {
|
return OptionalPath{path: path}
|
||||||
return OptionalPath{}
|
}
|
||||||
}
|
|
||||||
return OptionalPath{valid: true, path: path}
|
// InvalidOptionalPath returns an OptionalPath that is invalid with the given reason.
|
||||||
|
func InvalidOptionalPath(reason string) OptionalPath {
|
||||||
|
|
||||||
|
return OptionalPath{invalidReason: reason}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Valid returns whether there is a valid path
|
// Valid returns whether there is a valid path
|
||||||
func (p OptionalPath) Valid() bool {
|
func (p OptionalPath) Valid() bool {
|
||||||
return p.valid
|
return p.path != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Path returns the Path embedded in this OptionalPath. You must be sure that
|
// Path returns the Path embedded in this OptionalPath. You must be sure that
|
||||||
// there is a valid path, since this method will panic if there is not.
|
// there is a valid path, since this method will panic if there is not.
|
||||||
func (p OptionalPath) Path() Path {
|
func (p OptionalPath) Path() Path {
|
||||||
if !p.valid {
|
if p.path == nil {
|
||||||
panic("Requesting an invalid path")
|
msg := "Requesting an invalid path"
|
||||||
|
if p.invalidReason != "" {
|
||||||
|
msg += ": " + p.invalidReason
|
||||||
|
}
|
||||||
|
panic(msg)
|
||||||
}
|
}
|
||||||
return p.path
|
return p.path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InvalidReason returns the reason that the optional path is invalid, or "" if it is valid.
|
||||||
|
func (p OptionalPath) InvalidReason() string {
|
||||||
|
if p.path != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if p.invalidReason == "" {
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
return p.invalidReason
|
||||||
|
}
|
||||||
|
|
||||||
// AsPaths converts the OptionalPath into Paths.
|
// AsPaths converts the OptionalPath into Paths.
|
||||||
//
|
//
|
||||||
// It returns nil if this is not valid, or a single length slice containing the Path embedded in
|
// It returns nil if this is not valid, or a single length slice containing the Path embedded in
|
||||||
// this OptionalPath.
|
// this OptionalPath.
|
||||||
func (p OptionalPath) AsPaths() Paths {
|
func (p OptionalPath) AsPaths() Paths {
|
||||||
if !p.valid {
|
if p.path == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return Paths{p.path}
|
return Paths{p.path}
|
||||||
@@ -303,7 +321,7 @@ func (p OptionalPath) AsPaths() Paths {
|
|||||||
// RelativeToTop returns an OptionalPath with the path that was embedded having been replaced by the
|
// RelativeToTop returns an OptionalPath with the path that was embedded having been replaced by the
|
||||||
// result of calling Path.RelativeToTop on it.
|
// result of calling Path.RelativeToTop on it.
|
||||||
func (p OptionalPath) RelativeToTop() OptionalPath {
|
func (p OptionalPath) RelativeToTop() OptionalPath {
|
||||||
if !p.valid {
|
if p.path == nil {
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
p.path = p.path.RelativeToTop()
|
p.path = p.path.RelativeToTop()
|
||||||
@@ -312,7 +330,7 @@ func (p OptionalPath) RelativeToTop() OptionalPath {
|
|||||||
|
|
||||||
// String returns the string version of the Path, or "" if it isn't valid.
|
// String returns the string version of the Path, or "" if it isn't valid.
|
||||||
func (p OptionalPath) String() string {
|
func (p OptionalPath) String() string {
|
||||||
if p.valid {
|
if p.path != nil {
|
||||||
return p.path.String()
|
return p.path.String()
|
||||||
} else {
|
} else {
|
||||||
return ""
|
return ""
|
||||||
@@ -1077,6 +1095,7 @@ func ExistentPathForSource(ctx PathContext, pathComponents ...string) OptionalPa
|
|||||||
path, err := pathForSource(ctx, pathComponents...)
|
path, err := pathForSource(ctx, pathComponents...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
reportPathError(ctx, err)
|
reportPathError(ctx, err)
|
||||||
|
// No need to put the error message into the returned path since it has been reported already.
|
||||||
return OptionalPath{}
|
return OptionalPath{}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1091,7 +1110,7 @@ func ExistentPathForSource(ctx PathContext, pathComponents ...string) OptionalPa
|
|||||||
return OptionalPath{}
|
return OptionalPath{}
|
||||||
}
|
}
|
||||||
if !exists {
|
if !exists {
|
||||||
return OptionalPath{}
|
return InvalidOptionalPath(path.String() + " does not exist")
|
||||||
}
|
}
|
||||||
return OptionalPathForPath(path)
|
return OptionalPathForPath(path)
|
||||||
}
|
}
|
||||||
@@ -1127,6 +1146,7 @@ func (p SourcePath) OverlayPath(ctx ModuleMissingDepsPathContext, path Path) Opt
|
|||||||
relDir = srcPath.path
|
relDir = srcPath.path
|
||||||
} else {
|
} else {
|
||||||
ReportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
|
ReportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
|
||||||
|
// No need to put the error message into the returned path since it has been reported already.
|
||||||
return OptionalPath{}
|
return OptionalPath{}
|
||||||
}
|
}
|
||||||
dir := filepath.Join(p.srcDir, p.path, relDir)
|
dir := filepath.Join(p.srcDir, p.path, relDir)
|
||||||
@@ -1140,7 +1160,7 @@ func (p SourcePath) OverlayPath(ctx ModuleMissingDepsPathContext, path Path) Opt
|
|||||||
return OptionalPath{}
|
return OptionalPath{}
|
||||||
}
|
}
|
||||||
if len(paths) == 0 {
|
if len(paths) == 0 {
|
||||||
return OptionalPath{}
|
return InvalidOptionalPath(dir + " does not exist")
|
||||||
}
|
}
|
||||||
relPath := Rel(ctx, p.srcDir, paths[0])
|
relPath := Rel(ctx, p.srcDir, paths[0])
|
||||||
return OptionalPathForPath(PathForSource(ctx, relPath))
|
return OptionalPathForPath(PathForSource(ctx, relPath))
|
||||||
|
@@ -137,26 +137,35 @@ func TestValidatePath(t *testing.T) {
|
|||||||
|
|
||||||
func TestOptionalPath(t *testing.T) {
|
func TestOptionalPath(t *testing.T) {
|
||||||
var path OptionalPath
|
var path OptionalPath
|
||||||
checkInvalidOptionalPath(t, path)
|
checkInvalidOptionalPath(t, path, "unknown")
|
||||||
|
|
||||||
path = OptionalPathForPath(nil)
|
path = OptionalPathForPath(nil)
|
||||||
checkInvalidOptionalPath(t, path)
|
checkInvalidOptionalPath(t, path, "unknown")
|
||||||
|
|
||||||
|
path = InvalidOptionalPath("foo")
|
||||||
|
checkInvalidOptionalPath(t, path, "foo")
|
||||||
|
|
||||||
|
path = InvalidOptionalPath("")
|
||||||
|
checkInvalidOptionalPath(t, path, "unknown")
|
||||||
|
|
||||||
path = OptionalPathForPath(PathForTesting("path"))
|
path = OptionalPathForPath(PathForTesting("path"))
|
||||||
checkValidOptionalPath(t, path, "path")
|
checkValidOptionalPath(t, path, "path")
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkInvalidOptionalPath(t *testing.T, path OptionalPath) {
|
func checkInvalidOptionalPath(t *testing.T, path OptionalPath, expectedInvalidReason string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if path.Valid() {
|
if path.Valid() {
|
||||||
t.Errorf("Uninitialized OptionalPath should not be valid")
|
t.Errorf("Invalid OptionalPath should not be valid")
|
||||||
|
}
|
||||||
|
if path.InvalidReason() != expectedInvalidReason {
|
||||||
|
t.Errorf("Wrong invalid reason: expected %q, got %q", expectedInvalidReason, path.InvalidReason())
|
||||||
}
|
}
|
||||||
if path.String() != "" {
|
if path.String() != "" {
|
||||||
t.Errorf("Uninitialized OptionalPath String() should return \"\", not %q", path.String())
|
t.Errorf("Invalid OptionalPath String() should return \"\", not %q", path.String())
|
||||||
}
|
}
|
||||||
paths := path.AsPaths()
|
paths := path.AsPaths()
|
||||||
if len(paths) != 0 {
|
if len(paths) != 0 {
|
||||||
t.Errorf("Uninitialized OptionalPath AsPaths() should return empty Paths, not %q", paths)
|
t.Errorf("Invalid OptionalPath AsPaths() should return empty Paths, not %q", paths)
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r == nil {
|
if r := recover(); r == nil {
|
||||||
@@ -171,6 +180,9 @@ func checkValidOptionalPath(t *testing.T, path OptionalPath, expectedString stri
|
|||||||
if !path.Valid() {
|
if !path.Valid() {
|
||||||
t.Errorf("Initialized OptionalPath should not be invalid")
|
t.Errorf("Initialized OptionalPath should not be invalid")
|
||||||
}
|
}
|
||||||
|
if path.InvalidReason() != "" {
|
||||||
|
t.Errorf("Initialized OptionalPath should not have an invalid reason, got: %q", path.InvalidReason())
|
||||||
|
}
|
||||||
if path.String() != expectedString {
|
if path.String() != expectedString {
|
||||||
t.Errorf("Initialized OptionalPath String() should return %q, not %q", expectedString, path.String())
|
t.Errorf("Initialized OptionalPath String() should return %q, not %q", expectedString, path.String())
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user