Clarify paths.go somewhat

Bug: 35562758
Test: make

Change-Id: I4cf05ffdd0962186f490467829e3576b4e269c47
This commit is contained in:
Jeff Gaston 2017-04-10 15:47:24 -07:00
parent 02f3add3a3
commit 734e3809ad
3 changed files with 40 additions and 40 deletions

View file

@ -120,16 +120,16 @@ func (p AndroidPackageContext) IntermediatesPathVariable(name, path string) blue
})
}
// PrefixedPathsForOptionalSourceVariable returns a Variable whose value is the
// PrefixedExistentPathsForSourcesVariable returns a Variable whose value is the
// list of present source paths prefixed with the supplied prefix. It may only
// be called during a Go package's initialization - either from the init()
// function or as part of a package-scoped variable's initialization.
func (p AndroidPackageContext) PrefixedPathsForOptionalSourceVariable(
func (p AndroidPackageContext) PrefixedExistentPathsForSourcesVariable(
name, prefix string, paths []string) blueprint.Variable {
return p.VariableFunc(name, func(config interface{}) (string, error) {
ctx := &configErrorWrapper{p, config.(Config), []error{}}
paths := PathsForOptionalSource(ctx, "", paths)
paths := ExistentPathsForSources(ctx, "", paths)
if len(ctx.errors) > 0 {
return "", ctx.errors[0]
}

View file

@ -97,6 +97,8 @@ type Path interface {
type WritablePath interface {
Path
// the writablePath method doesn't directly do anything,
// but it allows a struct to distinguish between whether or not it implements the WritablePath interface
writablePath()
}
@ -137,7 +139,7 @@ func ResPathWithName(ctx ModuleContext, p Path, name string) ModuleResPath {
if path, ok := p.(resPathProvider); ok {
return path.resPathWithName(ctx, name)
}
reportPathError(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
reportPathError(ctx, "Tried to create res file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
return PathForModuleRes(ctx)
}
@ -188,7 +190,7 @@ func PathsForSource(ctx PathContext, paths []string) Paths {
ret := make(Paths, 0, len(paths))
intermediates := filepath.Join(modCtx.ModuleDir(), modCtx.ModuleName(), modCtx.ModuleSubDir(), "missing")
for _, path := range paths {
p := OptionalPathForSource(ctx, intermediates, path)
p := ExistentPathForSource(ctx, intermediates, path)
if p.Valid() {
ret = append(ret, p.Path())
} else {
@ -205,13 +207,13 @@ func PathsForSource(ctx PathContext, paths []string) Paths {
return ret
}
// PathsForOptionalSource returns a list of Paths rooted from SrcDir that are
// ExistentPathsForSources returns a list of Paths rooted from SrcDir that are
// found in the tree. If any are not found, they are omitted from the list,
// and dependencies are added so that we're re-run when they are added.
func PathsForOptionalSource(ctx PathContext, intermediates string, paths []string) Paths {
func ExistentPathsForSources(ctx PathContext, intermediates string, paths []string) Paths {
ret := make(Paths, 0, len(paths))
for _, path := range paths {
p := OptionalPathForSource(ctx, intermediates, path)
p := ExistentPathForSource(ctx, intermediates, path)
if p.Valid() {
ret = append(ret, p.Path())
}
@ -337,12 +339,11 @@ func safePathForSource(ctx PathContext, path string) SourcePath {
return ret
}
// PathForSource returns a SourcePath for the provided paths... (which are
// joined together with filepath.Join). This also validates that the path
// doesn't escape the source dir, or is contained in the build dir. On error, it
// will return a usable, but invalid SourcePath, and report a ModuleError.
func PathForSource(ctx PathContext, paths ...string) SourcePath {
p := validatePath(ctx, paths...)
// PathForSource joins the provided path components and validates that the result
// neither escapes the source dir nor is in the out dir.
// On error, it will return a usable, but invalid SourcePath, and report a ModuleError.
func PathForSource(ctx PathContext, pathComponents ...string) SourcePath {
p := validatePath(ctx, pathComponents...)
ret := SourcePath{basePath{p, pathConfig(ctx), ""}}
abs, err := filepath.Abs(ret.String())
@ -368,16 +369,16 @@ func PathForSource(ctx PathContext, paths ...string) SourcePath {
return ret
}
// OptionalPathForSource returns an OptionalPath with the SourcePath if the
// ExistentPathForSource returns an OptionalPath with the SourcePath if the
// path exists, or an empty OptionalPath if it doesn't exist. Dependencies are added
// so that the ninja file will be regenerated if the state of the path changes.
func OptionalPathForSource(ctx PathContext, intermediates string, paths ...string) OptionalPath {
if len(paths) == 0 {
func ExistentPathForSource(ctx PathContext, intermediates string, pathComponents ...string) OptionalPath {
if len(pathComponents) == 0 {
// For when someone forgets the 'intermediates' argument
panic("Missing path(s)")
}
p := validatePath(ctx, paths...)
p := validatePath(ctx, pathComponents...)
path := SourcePath{basePath{p, pathConfig(ctx), ""}}
abs, err := filepath.Abs(path.String())
@ -483,12 +484,11 @@ type OutputPath struct {
var _ Path = OutputPath{}
// PathForOutput returns an OutputPath for the provided paths... (which are
// joined together with filepath.Join). This also validates that the path
// does not escape the build dir. On error, it will return a usable, but invalid
// OutputPath, and report a ModuleError.
func PathForOutput(ctx PathContext, paths ...string) OutputPath {
path := validatePath(ctx, paths...)
// PathForOutput joins the provided paths and returns an OutputPath that is
// validated to not escape the build dir.
// On error, it will return a usable, but invalid OutputPath, and report a ModuleError.
func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath {
path := validatePath(ctx, pathComponents...)
return OutputPath{basePath{path, pathConfig(ctx), ""}}
}
@ -597,7 +597,7 @@ func PathForVndkRefAbiDump(ctx ModuleContext, version, fileName string, vndkOrNd
}
refDumpFileStr := "prebuilts/abi-dumps/" + vndkOrNdkDir + "/" + version + "/" +
archName + "/" + sourceOrBinaryDir + "/" + fileName + ext
return OptionalPathForSource(ctx, "", refDumpFileStr)
return ExistentPathForSource(ctx, "", refDumpFileStr)
}
// PathForModuleOut returns a Path representing the paths... under the module's
@ -647,8 +647,8 @@ var _ Path = ModuleObjPath{}
// PathForModuleObj returns a Path representing the paths... under the module's
// 'obj' directory.
func PathForModuleObj(ctx ModuleContext, paths ...string) ModuleObjPath {
p := validatePath(ctx, paths...)
func PathForModuleObj(ctx ModuleContext, pathComponents ...string) ModuleObjPath {
p := validatePath(ctx, pathComponents...)
return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
}
@ -662,14 +662,14 @@ var _ Path = ModuleResPath{}
// PathForModuleRes returns a Path representing the paths... under the module's
// 'res' directory.
func PathForModuleRes(ctx ModuleContext, paths ...string) ModuleResPath {
p := validatePath(ctx, paths...)
func PathForModuleRes(ctx ModuleContext, pathComponents ...string) ModuleResPath {
p := validatePath(ctx, pathComponents...)
return ModuleResPath{PathForModuleOut(ctx, "res", p)}
}
// PathForModuleInstall returns a Path representing the install path for the
// module appended with paths...
func PathForModuleInstall(ctx ModuleContext, paths ...string) OutputPath {
func PathForModuleInstall(ctx ModuleContext, pathComponents ...string) OutputPath {
var outPaths []string
if ctx.Device() {
partition := "system"
@ -689,14 +689,14 @@ func PathForModuleInstall(ctx ModuleContext, paths ...string) OutputPath {
if ctx.Debug() {
outPaths = append([]string{"debug"}, outPaths...)
}
outPaths = append(outPaths, paths...)
outPaths = append(outPaths, pathComponents...)
return PathForOutput(ctx, outPaths...)
}
// validateSafePath validates a path that we trust (may contain ninja variables).
// Ensures that each path component does not attempt to leave its component.
func validateSafePath(ctx PathContext, paths ...string) string {
for _, path := range paths {
func validateSafePath(ctx PathContext, pathComponents ...string) string {
for _, path := range pathComponents {
path := filepath.Clean(path)
if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
reportPathError(ctx, "Path is outside directory: %s", path)
@ -706,20 +706,20 @@ func validateSafePath(ctx PathContext, paths ...string) string {
// TODO: filepath.Join isn't necessarily correct with embedded ninja
// variables. '..' may remove the entire ninja variable, even if it
// will be expanded to multiple nested directories.
return filepath.Join(paths...)
return filepath.Join(pathComponents...)
}
// validatePath validates that a path does not include ninja variables, and that
// each path component does not attempt to leave its component. Returns a joined
// version of each path component.
func validatePath(ctx PathContext, paths ...string) string {
for _, path := range paths {
func validatePath(ctx PathContext, pathComponents ...string) string {
for _, path := range pathComponents {
if strings.Contains(path, "$") {
reportPathError(ctx, "Path contains invalid character($): %s", path)
return ""
}
}
return validateSafePath(ctx, paths...)
return validateSafePath(ctx, pathComponents...)
}
type testPath struct {

View file

@ -104,7 +104,7 @@ func init() {
// Everything in these lists is a crime against abstraction and dependency tracking.
// Do not add anything to this list.
pctx.PrefixedPathsForOptionalSourceVariable("CommonGlobalIncludes", "-I",
pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I",
[]string{
"system/core/include",
"system/media/audio/include",
@ -118,7 +118,7 @@ func init() {
})
// This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
// with this, since there is no associated library.
pctx.PrefixedPathsForOptionalSourceVariable("CommonNativehelperInclude", "-I",
pctx.PrefixedExistentPathsForSourcesVariable("CommonNativehelperInclude", "-I",
[]string{"libnativehelper/include/nativehelper"})
pctx.SourcePathVariable("ClangDefaultBase", "prebuilts/clang/host")
@ -153,7 +153,7 @@ func init() {
pctx.StaticVariable("RSLLVMPrebuiltsPath", "${RSClangBase}/${HostPrebuiltTag}/${RSClangVersion}/bin")
pctx.StaticVariable("RSIncludePath", "${RSLLVMPrebuiltsPath}/../lib64/clang/${RSReleaseVersion}/include")
pctx.PrefixedPathsForOptionalSourceVariable("RsGlobalIncludes", "-I",
pctx.PrefixedExistentPathsForSourcesVariable("RsGlobalIncludes", "-I",
[]string{
"external/clang/lib/Headers",
"frameworks/rs/script_api/include",