2015-01-31 09:27:36 +08:00
|
|
|
// Copyright 2015 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
package android
|
2015-01-31 09:27:36 +08:00
|
|
|
|
|
|
|
import (
|
2015-07-15 09:55:36 +08:00
|
|
|
"fmt"
|
2015-06-17 07:38:10 +08:00
|
|
|
"path/filepath"
|
2015-09-24 06:26:20 +08:00
|
|
|
"reflect"
|
2017-11-04 06:20:35 +08:00
|
|
|
"sort"
|
2015-09-24 06:26:20 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
"github.com/google/blueprint/pathtools"
|
2015-01-31 09:27:36 +08:00
|
|
|
)
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// PathContext is the subset of a (Module|Singleton)Context required by the
|
|
|
|
// Path methods.
|
|
|
|
type PathContext interface {
|
2017-02-02 06:10:36 +08:00
|
|
|
Fs() pathtools.FileSystem
|
2017-11-29 16:27:14 +08:00
|
|
|
Config() Config
|
2015-12-19 07:11:17 +08:00
|
|
|
AddNinjaFileDeps(deps ...string)
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
2016-11-02 02:10:25 +08:00
|
|
|
type PathGlobContext interface {
|
|
|
|
GlobWithDeps(globPattern string, excludes []string) ([]string, error)
|
|
|
|
}
|
|
|
|
|
2017-11-29 16:27:14 +08:00
|
|
|
var _ PathContext = SingletonContext(nil)
|
|
|
|
var _ PathContext = ModuleContext(nil)
|
2015-09-24 06:26:20 +08:00
|
|
|
|
2017-07-07 07:59:48 +08:00
|
|
|
type ModuleInstallPathContext interface {
|
2019-06-07 05:33:29 +08:00
|
|
|
BaseModuleContext
|
2017-07-07 07:59:48 +08:00
|
|
|
|
|
|
|
InstallInData() bool
|
2019-09-12 01:25:18 +08:00
|
|
|
InstallInTestcases() bool
|
2017-07-07 07:59:48 +08:00
|
|
|
InstallInSanitizerDir() bool
|
2018-01-31 23:54:12 +08:00
|
|
|
InstallInRecovery() bool
|
2019-10-03 02:10:58 +08:00
|
|
|
InstallInRoot() bool
|
2019-07-30 07:44:46 +08:00
|
|
|
InstallBypassMake() bool
|
2017-07-07 07:59:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ ModuleInstallPathContext = ModuleContext(nil)
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// errorfContext is the interface containing the Errorf method matching the
|
|
|
|
// Errorf method in blueprint.SingletonContext.
|
|
|
|
type errorfContext interface {
|
|
|
|
Errorf(format string, args ...interface{})
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
var _ errorfContext = blueprint.SingletonContext(nil)
|
|
|
|
|
|
|
|
// moduleErrorf is the interface containing the ModuleErrorf method matching
|
|
|
|
// the ModuleErrorf method in blueprint.ModuleContext.
|
|
|
|
type moduleErrorf interface {
|
|
|
|
ModuleErrorf(format string, args ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ moduleErrorf = blueprint.ModuleContext(nil)
|
|
|
|
|
|
|
|
// reportPathError will register an error with the attached context. It
|
|
|
|
// attempts ctx.ModuleErrorf for a better error message first, then falls
|
|
|
|
// back to ctx.Errorf.
|
2018-02-23 05:54:26 +08:00
|
|
|
func reportPathError(ctx PathContext, err error) {
|
|
|
|
reportPathErrorf(ctx, "%s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// reportPathErrorf will register an error with the attached context. It
|
|
|
|
// attempts ctx.ModuleErrorf for a better error message first, then falls
|
|
|
|
// back to ctx.Errorf.
|
|
|
|
func reportPathErrorf(ctx PathContext, format string, args ...interface{}) {
|
2015-09-24 06:26:20 +08:00
|
|
|
if mctx, ok := ctx.(moduleErrorf); ok {
|
|
|
|
mctx.ModuleErrorf(format, args...)
|
|
|
|
} else if ectx, ok := ctx.(errorfContext); ok {
|
|
|
|
ectx.Errorf(format, args...)
|
|
|
|
} else {
|
|
|
|
panic(fmt.Sprintf(format, args...))
|
|
|
|
}
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
type Path interface {
|
|
|
|
// Returns the path in string form
|
|
|
|
String() string
|
|
|
|
|
2016-10-27 01:05:25 +08:00
|
|
|
// Ext returns the extension of the last element of the path
|
2015-09-24 06:26:20 +08:00
|
|
|
Ext() string
|
2016-10-27 01:05:25 +08:00
|
|
|
|
|
|
|
// Base returns the last element of the path
|
|
|
|
Base() string
|
2017-02-02 06:12:44 +08:00
|
|
|
|
|
|
|
// Rel returns the portion of the path relative to the directory it was created from. For
|
|
|
|
// example, Rel on a PathsForModuleSrc would return the path relative to the module source
|
2017-12-06 07:36:55 +08:00
|
|
|
// directory, and OutputPath.Join("foo").Rel() would return "foo".
|
2017-02-02 06:12:44 +08:00
|
|
|
Rel() string
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// WritablePath is a type of path that can be used as an output for build rules.
|
|
|
|
type WritablePath interface {
|
|
|
|
Path
|
|
|
|
|
2017-04-11 06:47:24 +08:00
|
|
|
// the writablePath method doesn't directly do anything,
|
|
|
|
// but it allows a struct to distinguish between whether or not it implements the WritablePath interface
|
2015-09-24 06:26:20 +08:00
|
|
|
writablePath()
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
type genPathProvider interface {
|
2016-11-03 11:43:13 +08:00
|
|
|
genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
type objPathProvider interface {
|
2016-05-19 06:37:25 +08:00
|
|
|
objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
type resPathProvider interface {
|
2016-05-19 06:37:25 +08:00
|
|
|
resPathWithName(ctx ModuleContext, name string) ModuleResPath
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// GenPathWithExt derives a new file path in ctx's generated sources directory
|
|
|
|
// from the current path, but with the new extension.
|
2016-11-03 11:43:13 +08:00
|
|
|
func GenPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleGenPath {
|
2015-09-24 06:26:20 +08:00
|
|
|
if path, ok := p.(genPathProvider); ok {
|
2016-11-03 11:43:13 +08:00
|
|
|
return path.genPathWithExt(ctx, subdir, ext)
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
2018-02-23 05:54:26 +08:00
|
|
|
reportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
|
2015-09-24 06:26:20 +08:00
|
|
|
return PathForModuleGen(ctx)
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// ObjPathWithExt derives a new file path in ctx's object directory from the
|
|
|
|
// current path, but with the new extension.
|
2016-11-03 11:43:13 +08:00
|
|
|
func ObjPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleObjPath {
|
2015-09-24 06:26:20 +08:00
|
|
|
if path, ok := p.(objPathProvider); ok {
|
|
|
|
return path.objPathWithExt(ctx, subdir, ext)
|
|
|
|
}
|
2018-02-23 05:54:26 +08:00
|
|
|
reportPathErrorf(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
|
2015-09-24 06:26:20 +08:00
|
|
|
return PathForModuleObj(ctx)
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// ResPathWithName derives a new path in ctx's output resource directory, using
|
|
|
|
// the current path to create the directory name, and the `name` argument for
|
|
|
|
// the filename.
|
2016-05-19 06:37:25 +08:00
|
|
|
func ResPathWithName(ctx ModuleContext, p Path, name string) ModuleResPath {
|
2015-09-24 06:26:20 +08:00
|
|
|
if path, ok := p.(resPathProvider); ok {
|
|
|
|
return path.resPathWithName(ctx, name)
|
|
|
|
}
|
2018-02-23 05:54:26 +08:00
|
|
|
reportPathErrorf(ctx, "Tried to create res file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
|
2015-09-24 06:26:20 +08:00
|
|
|
return PathForModuleRes(ctx)
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// OptionalPath is a container that may or may not contain a valid Path.
|
|
|
|
type OptionalPath struct {
|
|
|
|
valid bool
|
|
|
|
path Path
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
2015-05-13 02:36:53 +08:00
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// OptionalPathForPath returns an OptionalPath containing the path.
|
|
|
|
func OptionalPathForPath(path Path) OptionalPath {
|
|
|
|
if path == nil {
|
|
|
|
return OptionalPath{}
|
|
|
|
}
|
|
|
|
return OptionalPath{valid: true, path: path}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid returns whether there is a valid path
|
|
|
|
func (p OptionalPath) Valid() bool {
|
|
|
|
return p.valid
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (p OptionalPath) Path() Path {
|
|
|
|
if !p.valid {
|
|
|
|
panic("Requesting an invalid path")
|
|
|
|
}
|
|
|
|
return p.path
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the string version of the Path, or "" if it isn't valid.
|
|
|
|
func (p OptionalPath) String() string {
|
|
|
|
if p.valid {
|
|
|
|
return p.path.String()
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Paths is a slice of Path objects, with helpers to operate on the collection.
|
|
|
|
type Paths []Path
|
|
|
|
|
|
|
|
// PathsForSource returns Paths rooted from SrcDir
|
|
|
|
func PathsForSource(ctx PathContext, paths []string) Paths {
|
|
|
|
ret := make(Paths, len(paths))
|
|
|
|
for i, path := range paths {
|
|
|
|
ret[i] = PathForSource(ctx, path)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2017-04-11 06:47:24 +08:00
|
|
|
// ExistentPathsForSources returns a list of Paths rooted from SrcDir that are
|
2015-12-19 07:11:17 +08:00
|
|
|
// 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.
|
2018-02-23 03:47:25 +08:00
|
|
|
func ExistentPathsForSources(ctx PathContext, paths []string) Paths {
|
2015-12-19 07:11:17 +08:00
|
|
|
ret := make(Paths, 0, len(paths))
|
|
|
|
for _, path := range paths {
|
2018-02-23 03:47:25 +08:00
|
|
|
p := ExistentPathForSource(ctx, path)
|
2015-12-19 07:11:17 +08:00
|
|
|
if p.Valid() {
|
|
|
|
ret = append(ret, p.Path())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-05-30 05:40:35 +08:00
|
|
|
// PathsForModuleSrc returns Paths rooted from the module's local source directory. It expands globs, references to
|
|
|
|
// SourceFileProducer modules using the ":name" syntax, and references to OutputFileProducer modules using the
|
|
|
|
// ":name{.tag}" syntax. Properties passed as the paths argument must have been annotated with struct tag
|
|
|
|
// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
|
|
|
|
// path_properties mutator. If ctx.Config().AllowMissingDependencies() is true then any missing SourceFileProducer or
|
|
|
|
// OutputFileProducer dependencies will cause the module to be marked as having missing dependencies.
|
2016-05-19 06:37:25 +08:00
|
|
|
func PathsForModuleSrc(ctx ModuleContext, paths []string) Paths {
|
2019-03-06 14:25:09 +08:00
|
|
|
return PathsForModuleSrcExcludes(ctx, paths, nil)
|
|
|
|
}
|
|
|
|
|
2019-03-19 03:12:48 +08:00
|
|
|
// PathsForModuleSrcExcludes returns Paths rooted from the module's local source directory, excluding paths listed in
|
2019-05-30 05:40:35 +08:00
|
|
|
// the excludes arguments. It expands globs, references to SourceFileProducer modules using the ":name" syntax, and
|
|
|
|
// references to OutputFileProducer modules using the ":name{.tag}" syntax. Properties passed as the paths or excludes
|
|
|
|
// argument must have been annotated with struct tag `android:"path"` so that dependencies on SourceFileProducer modules
|
|
|
|
// will have already been handled by the path_properties mutator. If ctx.Config().AllowMissingDependencies() is
|
2019-07-25 21:44:56 +08:00
|
|
|
// true then any missing SourceFileProducer or OutputFileProducer dependencies will cause the module to be marked as
|
2019-05-30 05:40:35 +08:00
|
|
|
// having missing dependencies.
|
2019-03-06 14:25:09 +08:00
|
|
|
func PathsForModuleSrcExcludes(ctx ModuleContext, paths, excludes []string) Paths {
|
2019-03-19 03:12:48 +08:00
|
|
|
ret, missingDeps := PathsAndMissingDepsForModuleSrcExcludes(ctx, paths, excludes)
|
|
|
|
if ctx.Config().AllowMissingDependencies() {
|
|
|
|
ctx.AddMissingDependencies(missingDeps)
|
|
|
|
} else {
|
|
|
|
for _, m := range missingDeps {
|
|
|
|
ctx.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-11-08 18:54:21 +08:00
|
|
|
// OutputPaths is a slice of OutputPath objects, with helpers to operate on the collection.
|
|
|
|
type OutputPaths []OutputPath
|
|
|
|
|
|
|
|
// Paths returns the OutputPaths as a Paths
|
|
|
|
func (p OutputPaths) Paths() Paths {
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ret := make(Paths, len(p))
|
|
|
|
for i, path := range p {
|
|
|
|
ret[i] = path
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strings returns the string forms of the writable paths.
|
|
|
|
func (p OutputPaths) Strings() []string {
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ret := make([]string, len(p))
|
|
|
|
for i, path := range p {
|
|
|
|
ret[i] = path.String()
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-03-19 03:12:48 +08:00
|
|
|
// PathsAndMissingDepsForModuleSrcExcludes returns Paths rooted from the module's local source directory, excluding
|
2019-05-30 05:40:35 +08:00
|
|
|
// paths listed in the excludes arguments, and a list of missing dependencies. It expands globs, references to
|
|
|
|
// SourceFileProducer modules using the ":name" syntax, and references to OutputFileProducer modules using the
|
|
|
|
// ":name{.tag}" syntax. Properties passed as the paths or excludes argument must have been annotated with struct tag
|
|
|
|
// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
|
|
|
|
// path_properties mutator. If ctx.Config().AllowMissingDependencies() is true then any missing SourceFileProducer or
|
|
|
|
// OutputFileProducer dependencies will be returned, and they will NOT cause the module to be marked as having missing
|
|
|
|
// dependencies.
|
2019-03-19 03:12:48 +08:00
|
|
|
func PathsAndMissingDepsForModuleSrcExcludes(ctx ModuleContext, paths, excludes []string) (Paths, []string) {
|
2019-03-06 14:25:09 +08:00
|
|
|
prefix := pathForModuleSrc(ctx).String()
|
|
|
|
|
|
|
|
var expandedExcludes []string
|
|
|
|
if excludes != nil {
|
|
|
|
expandedExcludes = make([]string, 0, len(excludes))
|
|
|
|
}
|
|
|
|
|
2019-03-19 03:12:48 +08:00
|
|
|
var missingExcludeDeps []string
|
|
|
|
|
2019-03-06 14:25:09 +08:00
|
|
|
for _, e := range excludes {
|
2019-05-30 05:40:35 +08:00
|
|
|
if m, t := SrcIsModuleWithTag(e); m != "" {
|
|
|
|
module := ctx.GetDirectDepWithTag(m, sourceOrOutputDepTag(t))
|
2019-03-06 14:25:09 +08:00
|
|
|
if module == nil {
|
2019-03-19 03:12:48 +08:00
|
|
|
missingExcludeDeps = append(missingExcludeDeps, m)
|
2019-03-06 14:25:09 +08:00
|
|
|
continue
|
|
|
|
}
|
2019-05-30 05:40:35 +08:00
|
|
|
if outProducer, ok := module.(OutputFileProducer); ok {
|
|
|
|
outputFiles, err := outProducer.OutputFiles(t)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ModuleErrorf("path dependency %q: %s", e, err)
|
|
|
|
}
|
|
|
|
expandedExcludes = append(expandedExcludes, outputFiles.Strings()...)
|
|
|
|
} else if t != "" {
|
|
|
|
ctx.ModuleErrorf("path dependency %q is not an output file producing module", e)
|
|
|
|
} else if srcProducer, ok := module.(SourceFileProducer); ok {
|
2019-03-06 14:25:09 +08:00
|
|
|
expandedExcludes = append(expandedExcludes, srcProducer.Srcs().Strings()...)
|
|
|
|
} else {
|
2019-05-30 05:40:35 +08:00
|
|
|
ctx.ModuleErrorf("path dependency %q is not a source file producing module", e)
|
2019-03-06 14:25:09 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if paths == nil {
|
2019-03-19 03:12:48 +08:00
|
|
|
return nil, missingExcludeDeps
|
2019-03-06 14:25:09 +08:00
|
|
|
}
|
|
|
|
|
2019-03-19 03:12:48 +08:00
|
|
|
var missingDeps []string
|
|
|
|
|
2019-03-06 14:25:09 +08:00
|
|
|
expandedSrcFiles := make(Paths, 0, len(paths))
|
|
|
|
for _, s := range paths {
|
|
|
|
srcFiles, err := expandOneSrcPath(ctx, s, expandedExcludes)
|
|
|
|
if depErr, ok := err.(missingDependencyError); ok {
|
2019-03-19 03:12:48 +08:00
|
|
|
missingDeps = append(missingDeps, depErr.missingDeps...)
|
2019-03-06 14:25:09 +08:00
|
|
|
} else if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
|
|
|
expandedSrcFiles = append(expandedSrcFiles, srcFiles...)
|
|
|
|
}
|
2019-03-19 03:12:48 +08:00
|
|
|
|
|
|
|
return expandedSrcFiles, append(missingDeps, missingExcludeDeps...)
|
2019-03-06 14:25:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type missingDependencyError struct {
|
|
|
|
missingDeps []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e missingDependencyError) Error() string {
|
|
|
|
return "missing dependencies: " + strings.Join(e.missingDeps, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandOneSrcPath(ctx ModuleContext, s string, expandedExcludes []string) (Paths, error) {
|
2019-05-30 05:40:35 +08:00
|
|
|
if m, t := SrcIsModuleWithTag(s); m != "" {
|
|
|
|
module := ctx.GetDirectDepWithTag(m, sourceOrOutputDepTag(t))
|
2019-03-06 14:25:09 +08:00
|
|
|
if module == nil {
|
|
|
|
return nil, missingDependencyError{[]string{m}}
|
|
|
|
}
|
2019-05-30 05:40:35 +08:00
|
|
|
if outProducer, ok := module.(OutputFileProducer); ok {
|
|
|
|
outputFiles, err := outProducer.OutputFiles(t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("path dependency %q: %s", s, err)
|
|
|
|
}
|
|
|
|
return outputFiles, nil
|
|
|
|
} else if t != "" {
|
|
|
|
return nil, fmt.Errorf("path dependency %q is not an output file producing module", s)
|
|
|
|
} else if srcProducer, ok := module.(SourceFileProducer); ok {
|
2019-03-06 14:25:09 +08:00
|
|
|
moduleSrcs := srcProducer.Srcs()
|
|
|
|
for _, e := range expandedExcludes {
|
|
|
|
for j := 0; j < len(moduleSrcs); j++ {
|
|
|
|
if moduleSrcs[j].String() == e {
|
|
|
|
moduleSrcs = append(moduleSrcs[:j], moduleSrcs[j+1:]...)
|
|
|
|
j--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return moduleSrcs, nil
|
|
|
|
} else {
|
2019-05-30 05:40:35 +08:00
|
|
|
return nil, fmt.Errorf("path dependency %q is not a source file producing module", s)
|
2019-03-06 14:25:09 +08:00
|
|
|
}
|
|
|
|
} else if pathtools.IsGlob(s) {
|
|
|
|
paths := ctx.GlobFiles(pathForModuleSrc(ctx, s).String(), expandedExcludes)
|
|
|
|
return PathsWithModuleSrcSubDir(ctx, paths, ""), nil
|
|
|
|
} else {
|
|
|
|
p := pathForModuleSrc(ctx, s)
|
|
|
|
if exists, _, err := ctx.Fs().Exists(p.String()); err != nil {
|
|
|
|
reportPathErrorf(ctx, "%s: %s", p, err.Error())
|
|
|
|
} else if !exists {
|
|
|
|
reportPathErrorf(ctx, "module source path %q does not exist", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
j := findStringInSlice(p.String(), expandedExcludes)
|
|
|
|
if j >= 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return Paths{p}, nil
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pathsForModuleSrcFromFullPath returns Paths rooted from the module's local
|
|
|
|
// source directory, but strip the local source directory from the beginning of
|
2018-02-27 13:50:08 +08:00
|
|
|
// each string. If incDirs is false, strip paths with a trailing '/' from the list.
|
2018-09-13 01:02:13 +08:00
|
|
|
// It intended for use in globs that only list files that exist, so it allows '$' in
|
|
|
|
// filenames.
|
2019-06-07 07:13:11 +08:00
|
|
|
func pathsForModuleSrcFromFullPath(ctx BaseModuleContext, paths []string, incDirs bool) Paths {
|
2017-11-29 16:27:14 +08:00
|
|
|
prefix := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir()) + "/"
|
2017-09-28 08:42:05 +08:00
|
|
|
if prefix == "./" {
|
|
|
|
prefix = ""
|
|
|
|
}
|
2015-09-24 06:26:20 +08:00
|
|
|
ret := make(Paths, 0, len(paths))
|
|
|
|
for _, p := range paths {
|
2018-02-27 13:50:08 +08:00
|
|
|
if !incDirs && strings.HasSuffix(p, "/") {
|
|
|
|
continue
|
|
|
|
}
|
2015-09-24 06:26:20 +08:00
|
|
|
path := filepath.Clean(p)
|
|
|
|
if !strings.HasPrefix(path, prefix) {
|
2019-02-09 05:17:55 +08:00
|
|
|
reportPathErrorf(ctx, "Path %q is not in module source directory %q", p, prefix)
|
2015-09-24 06:26:20 +08:00
|
|
|
continue
|
2015-05-13 02:36:53 +08:00
|
|
|
}
|
2018-08-16 11:18:53 +08:00
|
|
|
|
2018-09-13 01:02:13 +08:00
|
|
|
srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):])
|
2018-08-16 11:18:53 +08:00
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-03-06 04:46:40 +08:00
|
|
|
srcPath.basePath.rel = srcPath.path
|
2018-08-16 11:18:53 +08:00
|
|
|
|
2019-03-06 04:46:40 +08:00
|
|
|
ret = append(ret, srcPath)
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's
|
2019-02-08 07:30:01 +08:00
|
|
|
// local source directory. If input is nil, use the default if it exists. If input is empty, returns nil.
|
2016-05-19 06:37:25 +08:00
|
|
|
func PathsWithOptionalDefaultForModuleSrc(ctx ModuleContext, input []string, def string) Paths {
|
2019-02-08 07:30:01 +08:00
|
|
|
if input != nil {
|
2015-09-24 06:26:20 +08:00
|
|
|
return PathsForModuleSrc(ctx, input)
|
2015-05-13 02:36:53 +08:00
|
|
|
}
|
2015-09-24 06:26:20 +08:00
|
|
|
// Use Glob so that if the default doesn't exist, a dependency is added so that when it
|
|
|
|
// is created, we're run again.
|
2017-11-29 16:27:14 +08:00
|
|
|
path := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir(), def)
|
2018-02-24 01:22:42 +08:00
|
|
|
return ctx.Glob(path, nil)
|
2015-05-13 02:36:53 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// Strings returns the Paths in string form
|
|
|
|
func (p Paths) Strings() []string {
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ret := make([]string, len(p))
|
|
|
|
for i, path := range p {
|
|
|
|
ret[i] = path.String()
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2017-10-25 02:13:31 +08:00
|
|
|
// FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It
|
|
|
|
// modifies the Paths slice contents in place, and returns a subslice of the original slice.
|
2017-08-30 03:28:37 +08:00
|
|
|
func FirstUniquePaths(list Paths) Paths {
|
|
|
|
k := 0
|
|
|
|
outer:
|
|
|
|
for i := 0; i < len(list); i++ {
|
|
|
|
for j := 0; j < k; j++ {
|
|
|
|
if list[i] == list[j] {
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list[k] = list[i]
|
|
|
|
k++
|
|
|
|
}
|
|
|
|
return list[:k]
|
|
|
|
}
|
|
|
|
|
2017-10-25 02:13:31 +08:00
|
|
|
// LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It
|
|
|
|
// modifies the Paths slice contents in place, and returns a subslice of the original slice.
|
|
|
|
func LastUniquePaths(list Paths) Paths {
|
|
|
|
totalSkip := 0
|
|
|
|
for i := len(list) - 1; i >= totalSkip; i-- {
|
|
|
|
skip := 0
|
|
|
|
for j := i - 1; j >= totalSkip; j-- {
|
|
|
|
if list[i] == list[j] {
|
|
|
|
skip++
|
|
|
|
} else {
|
|
|
|
list[j+skip] = list[j]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
totalSkip += skip
|
|
|
|
}
|
|
|
|
return list[totalSkip:]
|
|
|
|
}
|
|
|
|
|
2018-04-18 01:52:26 +08:00
|
|
|
// ReversePaths returns a copy of a Paths in reverse order.
|
|
|
|
func ReversePaths(list Paths) Paths {
|
|
|
|
if list == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ret := make(Paths, len(list))
|
|
|
|
for i := range list {
|
|
|
|
ret[i] = list[len(list)-1-i]
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2017-09-28 08:05:30 +08:00
|
|
|
func indexPathList(s Path, list []Path) int {
|
|
|
|
for i, l := range list {
|
|
|
|
if l == s {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func inPathList(p Path, list []Path) bool {
|
|
|
|
return indexPathList(p, list) != -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) {
|
2019-12-13 08:03:35 +08:00
|
|
|
return FilterPathListPredicate(list, func(p Path) bool { return inPathList(p, filter) })
|
|
|
|
}
|
|
|
|
|
|
|
|
func FilterPathListPredicate(list []Path, predicate func(Path) bool) (remainder []Path, filtered []Path) {
|
2017-09-28 08:05:30 +08:00
|
|
|
for _, l := range list {
|
2019-12-13 08:03:35 +08:00
|
|
|
if predicate(l) {
|
2017-09-28 08:05:30 +08:00
|
|
|
filtered = append(filtered, l)
|
|
|
|
} else {
|
|
|
|
remainder = append(remainder, l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-16 04:34:18 +08:00
|
|
|
// HasExt returns true of any of the paths have extension ext, otherwise false
|
|
|
|
func (p Paths) HasExt(ext string) bool {
|
|
|
|
for _, path := range p {
|
|
|
|
if path.Ext() == ext {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilterByExt returns the subset of the paths that have extension ext
|
|
|
|
func (p Paths) FilterByExt(ext string) Paths {
|
|
|
|
ret := make(Paths, 0, len(p))
|
|
|
|
for _, path := range p {
|
|
|
|
if path.Ext() == ext {
|
|
|
|
ret = append(ret, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilterOutByExt returns the subset of the paths that do not have extension ext
|
|
|
|
func (p Paths) FilterOutByExt(ext string) Paths {
|
|
|
|
ret := make(Paths, 0, len(p))
|
|
|
|
for _, path := range p {
|
|
|
|
if path.Ext() != ext {
|
|
|
|
ret = append(ret, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2017-11-04 06:20:35 +08:00
|
|
|
// DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory
|
|
|
|
// (including subdirectories) are in a contiguous subslice of the list, and can be found in
|
|
|
|
// O(log(N)) time using a binary search on the directory prefix.
|
|
|
|
type DirectorySortedPaths Paths
|
|
|
|
|
|
|
|
func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths {
|
|
|
|
ret := append(DirectorySortedPaths(nil), paths...)
|
|
|
|
sort.Slice(ret, func(i, j int) bool {
|
|
|
|
return ret[i].String() < ret[j].String()
|
|
|
|
})
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries
|
|
|
|
// that are in the specified directory and its subdirectories.
|
|
|
|
func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths {
|
|
|
|
prefix := filepath.Clean(dir) + "/"
|
|
|
|
start := sort.Search(len(p), func(i int) bool {
|
|
|
|
return prefix < p[i].String()
|
|
|
|
})
|
|
|
|
|
|
|
|
ret := p[start:]
|
|
|
|
|
|
|
|
end := sort.Search(len(ret), func(i int) bool {
|
|
|
|
return !strings.HasPrefix(ret[i].String(), prefix)
|
|
|
|
})
|
|
|
|
|
|
|
|
ret = ret[:end]
|
|
|
|
|
|
|
|
return Paths(ret)
|
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// WritablePaths is a slice of WritablePaths, used for multiple outputs.
|
|
|
|
type WritablePaths []WritablePath
|
|
|
|
|
|
|
|
// Strings returns the string forms of the writable paths.
|
|
|
|
func (p WritablePaths) Strings() []string {
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ret := make([]string, len(p))
|
|
|
|
for i, path := range p {
|
|
|
|
ret[i] = path.String()
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2017-11-23 08:19:37 +08:00
|
|
|
// Paths returns the WritablePaths as a Paths
|
|
|
|
func (p WritablePaths) Paths() Paths {
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ret := make(Paths, len(p))
|
|
|
|
for i, path := range p {
|
|
|
|
ret[i] = path
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
type basePath struct {
|
|
|
|
path string
|
|
|
|
config Config
|
2017-02-02 06:12:44 +08:00
|
|
|
rel string
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p basePath) Ext() string {
|
|
|
|
return filepath.Ext(p.path)
|
|
|
|
}
|
|
|
|
|
2016-10-27 01:05:25 +08:00
|
|
|
func (p basePath) Base() string {
|
|
|
|
return filepath.Base(p.path)
|
|
|
|
}
|
|
|
|
|
2017-02-02 06:12:44 +08:00
|
|
|
func (p basePath) Rel() string {
|
|
|
|
if p.rel != "" {
|
|
|
|
return p.rel
|
|
|
|
}
|
|
|
|
return p.path
|
|
|
|
}
|
|
|
|
|
2017-11-29 09:34:01 +08:00
|
|
|
func (p basePath) String() string {
|
|
|
|
return p.path
|
|
|
|
}
|
|
|
|
|
2017-12-06 07:36:55 +08:00
|
|
|
func (p basePath) withRel(rel string) basePath {
|
|
|
|
p.path = filepath.Join(p.path, rel)
|
|
|
|
p.rel = rel
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// SourcePath is a Path representing a file path rooted from SrcDir
|
|
|
|
type SourcePath struct {
|
|
|
|
basePath
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Path = SourcePath{}
|
|
|
|
|
2017-12-06 07:36:55 +08:00
|
|
|
func (p SourcePath) withRel(rel string) SourcePath {
|
|
|
|
p.basePath = p.basePath.withRel(rel)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// safePathForSource is for paths that we expect are safe -- only for use by go
|
|
|
|
// code that is embedding ninja variables in paths
|
2018-09-13 01:02:13 +08:00
|
|
|
func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
|
|
|
|
p, err := validateSafePath(pathComponents...)
|
|
|
|
ret := SourcePath{basePath{p, ctx.Config(), ""}}
|
2018-02-23 05:54:26 +08:00
|
|
|
if err != nil {
|
2018-09-13 01:02:13 +08:00
|
|
|
return ret, err
|
2018-02-23 05:54:26 +08:00
|
|
|
}
|
2015-09-24 06:26:20 +08:00
|
|
|
|
2019-01-25 05:14:39 +08:00
|
|
|
// absolute path already checked by validateSafePath
|
|
|
|
if strings.HasPrefix(ret.String(), ctx.Config().buildDir) {
|
2019-02-09 05:17:55 +08:00
|
|
|
return ret, fmt.Errorf("source path %q is in output", ret.String())
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
|
2018-09-13 01:02:13 +08:00
|
|
|
return ret, err
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
|
2018-02-23 06:21:02 +08:00
|
|
|
// pathForSource creates a SourcePath from pathComponents, but does not check that it exists.
|
|
|
|
func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
|
2018-02-23 05:54:26 +08:00
|
|
|
p, err := validatePath(pathComponents...)
|
2017-11-29 16:27:14 +08:00
|
|
|
ret := SourcePath{basePath{p, ctx.Config(), ""}}
|
2018-02-23 05:54:26 +08:00
|
|
|
if err != nil {
|
2018-02-23 06:21:02 +08:00
|
|
|
return ret, err
|
2018-02-23 05:54:26 +08:00
|
|
|
}
|
2015-09-24 06:26:20 +08:00
|
|
|
|
2019-01-25 05:14:39 +08:00
|
|
|
// absolute path already checked by validatePath
|
|
|
|
if strings.HasPrefix(ret.String(), ctx.Config().buildDir) {
|
2019-02-09 05:17:55 +08:00
|
|
|
return ret, fmt.Errorf("source path %q is in output", ret.String())
|
2018-02-23 06:21:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the
|
|
|
|
// path does not exist.
|
|
|
|
func existsWithDependencies(ctx PathContext, path SourcePath) (exists bool, err error) {
|
|
|
|
var files []string
|
|
|
|
|
|
|
|
if gctx, ok := ctx.(PathGlobContext); ok {
|
|
|
|
// Use glob to produce proper dependencies, even though we only want
|
|
|
|
// a single file.
|
|
|
|
files, err = gctx.GlobWithDeps(path.String(), nil)
|
|
|
|
} else {
|
|
|
|
var deps []string
|
|
|
|
// We cannot add build statements in this context, so we fall back to
|
|
|
|
// AddNinjaFileDeps
|
2018-09-22 06:11:48 +08:00
|
|
|
files, deps, err = pathtools.Glob(path.String(), nil, pathtools.FollowSymlinks)
|
2018-02-23 06:21:02 +08:00
|
|
|
ctx.AddNinjaFileDeps(deps...)
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
|
2018-02-23 06:21:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("glob: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(files) > 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
path, err := pathForSource(ctx, pathComponents...)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
|
|
|
|
2018-08-16 11:18:53 +08:00
|
|
|
if pathtools.IsGlob(path.String()) {
|
|
|
|
reportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
|
|
|
|
}
|
|
|
|
|
2018-02-23 06:21:02 +08:00
|
|
|
if modCtx, ok := ctx.(ModuleContext); ok && ctx.Config().AllowMissingDependencies() {
|
|
|
|
exists, err := existsWithDependencies(ctx, path)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
modCtx.AddMissingDependencies([]string{path.String()})
|
|
|
|
}
|
|
|
|
} else if exists, _, err := ctx.Fs().Exists(path.String()); err != nil {
|
|
|
|
reportPathErrorf(ctx, "%s: %s", path, err.Error())
|
2017-02-02 06:10:36 +08:00
|
|
|
} else if !exists {
|
2019-02-09 05:17:55 +08:00
|
|
|
reportPathErrorf(ctx, "source path %q does not exist", path)
|
2015-05-13 02:36:53 +08:00
|
|
|
}
|
2018-02-23 06:21:02 +08:00
|
|
|
return path
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
|
2017-04-11 06:47:24 +08:00
|
|
|
// ExistentPathForSource returns an OptionalPath with the SourcePath if the
|
2015-09-24 06:26:20 +08:00
|
|
|
// 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.
|
2018-02-23 03:47:25 +08:00
|
|
|
func ExistentPathForSource(ctx PathContext, pathComponents ...string) OptionalPath {
|
2018-02-23 06:21:02 +08:00
|
|
|
path, err := pathForSource(ctx, pathComponents...)
|
2018-02-23 05:54:26 +08:00
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
return OptionalPath{}
|
|
|
|
}
|
2015-09-24 06:26:20 +08:00
|
|
|
|
2018-08-16 11:18:53 +08:00
|
|
|
if pathtools.IsGlob(path.String()) {
|
|
|
|
reportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
|
|
|
|
return OptionalPath{}
|
|
|
|
}
|
|
|
|
|
2018-02-23 06:21:02 +08:00
|
|
|
exists, err := existsWithDependencies(ctx, path)
|
2015-09-24 06:26:20 +08:00
|
|
|
if err != nil {
|
2018-02-23 05:54:26 +08:00
|
|
|
reportPathError(ctx, err)
|
2015-09-24 06:26:20 +08:00
|
|
|
return OptionalPath{}
|
|
|
|
}
|
2018-02-23 06:21:02 +08:00
|
|
|
if !exists {
|
2018-02-23 15:09:01 +08:00
|
|
|
return OptionalPath{}
|
|
|
|
}
|
2015-09-24 06:26:20 +08:00
|
|
|
return OptionalPathForPath(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p SourcePath) String() string {
|
|
|
|
return filepath.Join(p.config.srcDir, p.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join creates a new SourcePath with paths... joined with the current path. The
|
|
|
|
// provided paths... may not use '..' to escape from the current path.
|
|
|
|
func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
|
2018-02-23 05:54:26 +08:00
|
|
|
path, err := validatePath(paths...)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
2017-12-06 07:36:55 +08:00
|
|
|
return p.withRel(path)
|
2015-05-13 02:36:53 +08:00
|
|
|
}
|
2015-07-15 09:55:36 +08:00
|
|
|
|
2019-03-06 04:39:51 +08:00
|
|
|
// join is like Join but does less path validation.
|
|
|
|
func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath {
|
|
|
|
path, err := validateSafePath(paths...)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
|
|
|
return p.withRel(path)
|
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// OverlayPath returns the overlay for `path' if it exists. This assumes that the
|
|
|
|
// SourcePath is the path to a resource overlay directory.
|
2016-05-19 06:37:25 +08:00
|
|
|
func (p SourcePath) OverlayPath(ctx ModuleContext, path Path) OptionalPath {
|
2015-09-24 06:26:20 +08:00
|
|
|
var relDir string
|
2019-03-06 04:46:40 +08:00
|
|
|
if srcPath, ok := path.(SourcePath); ok {
|
2015-09-24 06:26:20 +08:00
|
|
|
relDir = srcPath.path
|
|
|
|
} else {
|
2018-02-23 05:54:26 +08:00
|
|
|
reportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
|
2015-09-24 06:26:20 +08:00
|
|
|
return OptionalPath{}
|
|
|
|
}
|
|
|
|
dir := filepath.Join(p.config.srcDir, p.path, relDir)
|
|
|
|
// Use Glob so that we are run again if the directory is added.
|
2016-11-02 02:10:25 +08:00
|
|
|
if pathtools.IsGlob(dir) {
|
2018-02-23 05:54:26 +08:00
|
|
|
reportPathErrorf(ctx, "Path may not contain a glob: %s", dir)
|
2015-12-19 07:11:17 +08:00
|
|
|
}
|
2018-02-24 01:22:42 +08:00
|
|
|
paths, err := ctx.GlobWithDeps(dir, nil)
|
2015-07-15 09:55:36 +08:00
|
|
|
if err != nil {
|
2018-02-23 05:54:26 +08:00
|
|
|
reportPathErrorf(ctx, "glob: %s", err.Error())
|
2015-09-24 06:26:20 +08:00
|
|
|
return OptionalPath{}
|
|
|
|
}
|
|
|
|
if len(paths) == 0 {
|
|
|
|
return OptionalPath{}
|
|
|
|
}
|
2018-11-13 02:13:39 +08:00
|
|
|
relPath := Rel(ctx, p.config.srcDir, paths[0])
|
2015-09-24 06:26:20 +08:00
|
|
|
return OptionalPathForPath(PathForSource(ctx, relPath))
|
|
|
|
}
|
|
|
|
|
2019-10-02 13:05:35 +08:00
|
|
|
// OutputPath is a Path representing an intermediates file path rooted from the build directory
|
2015-09-24 06:26:20 +08:00
|
|
|
type OutputPath struct {
|
|
|
|
basePath
|
|
|
|
}
|
|
|
|
|
2017-10-19 08:27:54 +08:00
|
|
|
func (p OutputPath) withRel(rel string) OutputPath {
|
2017-12-06 07:36:55 +08:00
|
|
|
p.basePath = p.basePath.withRel(rel)
|
2017-10-19 08:27:54 +08:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2018-08-16 02:19:12 +08:00
|
|
|
func (p OutputPath) WithoutRel() OutputPath {
|
|
|
|
p.basePath.rel = filepath.Base(p.basePath.path)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
var _ Path = OutputPath{}
|
|
|
|
|
2017-04-11 06:47:24 +08:00
|
|
|
// 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 {
|
2018-02-23 05:54:26 +08:00
|
|
|
path, err := validatePath(pathComponents...)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
2017-11-29 16:27:14 +08:00
|
|
|
return OutputPath{basePath{path, ctx.Config(), ""}}
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
|
2019-02-16 03:08:35 +08:00
|
|
|
// PathsForOutput returns Paths rooted from buildDir
|
|
|
|
func PathsForOutput(ctx PathContext, paths []string) WritablePaths {
|
|
|
|
ret := make(WritablePaths, len(paths))
|
|
|
|
for i, path := range paths {
|
|
|
|
ret[i] = PathForOutput(ctx, path)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
func (p OutputPath) writablePath() {}
|
|
|
|
|
|
|
|
func (p OutputPath) String() string {
|
|
|
|
return filepath.Join(p.config.buildDir, p.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join creates a new OutputPath with paths... joined with the current path. The
|
|
|
|
// provided paths... may not use '..' to escape from the current path.
|
|
|
|
func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
|
2018-02-23 05:54:26 +08:00
|
|
|
path, err := validatePath(paths...)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
2017-12-06 07:36:55 +08:00
|
|
|
return p.withRel(path)
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
|
2019-02-12 06:14:16 +08:00
|
|
|
// ReplaceExtension creates a new OutputPath with the extension replaced with ext.
|
|
|
|
func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
|
|
|
|
if strings.Contains(ext, "/") {
|
|
|
|
reportPathErrorf(ctx, "extension %q cannot contain /", ext)
|
|
|
|
}
|
|
|
|
ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext))
|
2019-02-26 02:25:24 +08:00
|
|
|
ret.rel = pathtools.ReplaceExtension(p.rel, ext)
|
2019-02-12 06:14:16 +08:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-02-16 03:08:35 +08:00
|
|
|
// InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths.
|
|
|
|
func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath {
|
|
|
|
path, err := validatePath(paths...)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := PathForOutput(ctx, filepath.Dir(p.path), path)
|
2019-02-26 02:25:24 +08:00
|
|
|
ret.rel = filepath.Join(filepath.Dir(p.rel), path)
|
2019-02-16 03:08:35 +08:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// PathForIntermediates returns an OutputPath representing the top-level
|
|
|
|
// intermediates directory.
|
|
|
|
func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
|
2018-02-23 05:54:26 +08:00
|
|
|
path, err := validatePath(paths...)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
2015-09-24 06:26:20 +08:00
|
|
|
return PathForOutput(ctx, ".intermediates", path)
|
|
|
|
}
|
|
|
|
|
2019-03-06 04:46:40 +08:00
|
|
|
var _ genPathProvider = SourcePath{}
|
|
|
|
var _ objPathProvider = SourcePath{}
|
|
|
|
var _ resPathProvider = SourcePath{}
|
2015-09-24 06:26:20 +08:00
|
|
|
|
2019-03-06 04:46:40 +08:00
|
|
|
// PathForModuleSrc returns a Path representing the paths... under the
|
2015-09-24 06:26:20 +08:00
|
|
|
// module's local source directory.
|
2019-03-06 14:25:09 +08:00
|
|
|
func PathForModuleSrc(ctx ModuleContext, pathComponents ...string) Path {
|
|
|
|
p, err := validatePath(pathComponents...)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
2019-03-06 04:46:40 +08:00
|
|
|
}
|
2019-03-06 14:25:09 +08:00
|
|
|
paths, err := expandOneSrcPath(ctx, p, nil)
|
|
|
|
if err != nil {
|
|
|
|
if depErr, ok := err.(missingDependencyError); ok {
|
|
|
|
if ctx.Config().AllowMissingDependencies() {
|
|
|
|
ctx.AddMissingDependencies(depErr.missingDeps)
|
|
|
|
} else {
|
|
|
|
ctx.ModuleErrorf(`%s, is the property annotated with android:"path"?`, depErr.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
} else if len(paths) == 0 {
|
|
|
|
reportPathErrorf(ctx, "%q produced no files, expected exactly one", p)
|
|
|
|
return nil
|
|
|
|
} else if len(paths) > 1 {
|
|
|
|
reportPathErrorf(ctx, "%q produced %d files, expected exactly one", p, len(paths))
|
|
|
|
}
|
|
|
|
return paths[0]
|
2019-03-06 04:46:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func pathForModuleSrc(ctx ModuleContext, paths ...string) SourcePath {
|
2018-02-23 05:54:26 +08:00
|
|
|
p, err := validatePath(paths...)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
2018-02-23 06:21:02 +08:00
|
|
|
|
2019-03-06 04:46:40 +08:00
|
|
|
path, err := pathForSource(ctx, ctx.ModuleDir(), p)
|
2018-02-23 06:21:02 +08:00
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
|
|
|
|
2017-02-02 06:12:44 +08:00
|
|
|
path.basePath.rel = p
|
2018-02-23 06:21:02 +08:00
|
|
|
|
2017-02-02 06:12:44 +08:00
|
|
|
return path
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
|
2019-03-06 04:39:51 +08:00
|
|
|
// PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path
|
|
|
|
// will return the path relative to subDir in the module's source directory. If any input paths are not located
|
|
|
|
// inside subDir then a path error will be reported.
|
|
|
|
func PathsWithModuleSrcSubDir(ctx ModuleContext, paths Paths, subDir string) Paths {
|
|
|
|
paths = append(Paths(nil), paths...)
|
2019-03-06 04:46:40 +08:00
|
|
|
subDirFullPath := pathForModuleSrc(ctx, subDir)
|
2019-03-06 04:39:51 +08:00
|
|
|
for i, path := range paths {
|
|
|
|
rel := Rel(ctx, subDirFullPath.String(), path.String())
|
|
|
|
paths[i] = subDirFullPath.join(ctx, rel)
|
|
|
|
}
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
|
|
|
// PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the
|
|
|
|
// module's source directory. If the input path is not located inside subDir then a path error will be reported.
|
|
|
|
func PathWithModuleSrcSubDir(ctx ModuleContext, path Path, subDir string) Path {
|
2019-03-06 04:46:40 +08:00
|
|
|
subDirFullPath := pathForModuleSrc(ctx, subDir)
|
2019-03-06 04:39:51 +08:00
|
|
|
rel := Rel(ctx, subDirFullPath.String(), path.String())
|
|
|
|
return subDirFullPath.Join(ctx, rel)
|
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
|
|
|
|
// valid path if p is non-nil.
|
2016-05-19 06:37:25 +08:00
|
|
|
func OptionalPathForModuleSrc(ctx ModuleContext, p *string) OptionalPath {
|
2015-09-24 06:26:20 +08:00
|
|
|
if p == nil {
|
|
|
|
return OptionalPath{}
|
|
|
|
}
|
|
|
|
return OptionalPathForPath(PathForModuleSrc(ctx, *p))
|
|
|
|
}
|
|
|
|
|
2019-03-06 04:46:40 +08:00
|
|
|
func (p SourcePath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
|
2017-02-02 06:07:55 +08:00
|
|
|
return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
|
2019-03-06 04:46:40 +08:00
|
|
|
func (p SourcePath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
|
2017-02-02 06:07:55 +08:00
|
|
|
return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
|
2019-03-06 04:46:40 +08:00
|
|
|
func (p SourcePath) resPathWithName(ctx ModuleContext, name string) ModuleResPath {
|
2015-09-24 06:26:20 +08:00
|
|
|
// TODO: Use full directory if the new ctx is not the current ctx?
|
|
|
|
return PathForModuleRes(ctx, p.path, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModuleOutPath is a Path representing a module's output directory.
|
|
|
|
type ModuleOutPath struct {
|
|
|
|
OutputPath
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Path = ModuleOutPath{}
|
|
|
|
|
2019-08-17 03:14:32 +08:00
|
|
|
func (p ModuleOutPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
|
|
|
|
return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
|
|
|
|
}
|
|
|
|
|
2017-10-19 08:27:54 +08:00
|
|
|
func pathForModule(ctx ModuleContext) OutputPath {
|
|
|
|
return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir())
|
|
|
|
}
|
|
|
|
|
2018-07-11 18:10:41 +08:00
|
|
|
// PathForVndkRefAbiDump returns an OptionalPath representing the path of the
|
|
|
|
// reference abi dump for the given module. This is not guaranteed to be valid.
|
|
|
|
func PathForVndkRefAbiDump(ctx ModuleContext, version, fileName string,
|
2019-07-31 17:10:45 +08:00
|
|
|
isNdk, isLlndkOrVndk, isGzip bool) OptionalPath {
|
2018-07-11 18:10:41 +08:00
|
|
|
|
2018-02-21 02:53:31 +08:00
|
|
|
arches := ctx.DeviceConfig().Arches()
|
2018-07-11 18:10:41 +08:00
|
|
|
if len(arches) == 0 {
|
|
|
|
panic("device build with no primary arch")
|
|
|
|
}
|
2018-02-21 02:53:31 +08:00
|
|
|
currentArch := ctx.Arch()
|
|
|
|
archNameAndVariant := currentArch.ArchType.String()
|
|
|
|
if currentArch.ArchVariant != "" {
|
|
|
|
archNameAndVariant += "_" + currentArch.ArchVariant
|
|
|
|
}
|
2018-07-11 17:15:57 +08:00
|
|
|
|
|
|
|
var dirName string
|
2019-07-31 17:10:45 +08:00
|
|
|
if isNdk {
|
2018-07-11 17:15:57 +08:00
|
|
|
dirName = "ndk"
|
2019-07-31 17:10:45 +08:00
|
|
|
} else if isLlndkOrVndk {
|
2018-07-11 17:15:57 +08:00
|
|
|
dirName = "vndk"
|
2019-04-10 13:33:58 +08:00
|
|
|
} else {
|
|
|
|
dirName = "platform" // opt-in libs
|
2017-02-09 05:45:53 +08:00
|
|
|
}
|
2018-07-11 17:15:57 +08:00
|
|
|
|
2018-03-09 03:00:50 +08:00
|
|
|
binderBitness := ctx.DeviceConfig().BinderBitness()
|
2018-07-11 18:10:41 +08:00
|
|
|
|
|
|
|
var ext string
|
|
|
|
if isGzip {
|
|
|
|
ext = ".lsdump.gz"
|
|
|
|
} else {
|
|
|
|
ext = ".lsdump"
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExistentPathForSource(ctx, "prebuilts", "abi-dumps", dirName,
|
|
|
|
version, binderBitness, archNameAndVariant, "source-based",
|
|
|
|
fileName+ext)
|
2017-02-09 05:45:53 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// PathForModuleOut returns a Path representing the paths... under the module's
|
|
|
|
// output directory.
|
2016-05-19 06:37:25 +08:00
|
|
|
func PathForModuleOut(ctx ModuleContext, paths ...string) ModuleOutPath {
|
2018-02-23 05:54:26 +08:00
|
|
|
p, err := validatePath(paths...)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
2017-10-19 08:27:54 +08:00
|
|
|
return ModuleOutPath{
|
|
|
|
OutputPath: pathForModule(ctx).withRel(p),
|
|
|
|
}
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ModuleGenPath is a Path representing the 'gen' directory in a module's output
|
|
|
|
// directory. Mainly used for generated sources.
|
|
|
|
type ModuleGenPath struct {
|
|
|
|
ModuleOutPath
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Path = ModuleGenPath{}
|
|
|
|
var _ genPathProvider = ModuleGenPath{}
|
|
|
|
var _ objPathProvider = ModuleGenPath{}
|
|
|
|
|
|
|
|
// PathForModuleGen returns a Path representing the paths... under the module's
|
|
|
|
// `gen' directory.
|
2016-05-19 06:37:25 +08:00
|
|
|
func PathForModuleGen(ctx ModuleContext, paths ...string) ModuleGenPath {
|
2018-02-23 05:54:26 +08:00
|
|
|
p, err := validatePath(paths...)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
2015-09-24 06:26:20 +08:00
|
|
|
return ModuleGenPath{
|
2017-10-19 08:27:54 +08:00
|
|
|
ModuleOutPath: ModuleOutPath{
|
|
|
|
OutputPath: pathForModule(ctx).withRel("gen").withRel(p),
|
|
|
|
},
|
2015-07-15 09:55:36 +08:00
|
|
|
}
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
|
2016-11-03 11:43:13 +08:00
|
|
|
func (p ModuleGenPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
|
2015-09-24 06:26:20 +08:00
|
|
|
// TODO: make a different path for local vs remote generated files?
|
2016-11-03 11:43:13 +08:00
|
|
|
return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
func (p ModuleGenPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
|
2015-09-24 06:26:20 +08:00
|
|
|
return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
|
|
|
|
}
|
2015-07-15 09:55:36 +08:00
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// ModuleObjPath is a Path representing the 'obj' directory in a module's output
|
|
|
|
// directory. Used for compiled objects.
|
|
|
|
type ModuleObjPath struct {
|
|
|
|
ModuleOutPath
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Path = ModuleObjPath{}
|
|
|
|
|
|
|
|
// PathForModuleObj returns a Path representing the paths... under the module's
|
|
|
|
// 'obj' directory.
|
2017-04-11 06:47:24 +08:00
|
|
|
func PathForModuleObj(ctx ModuleContext, pathComponents ...string) ModuleObjPath {
|
2018-02-23 05:54:26 +08:00
|
|
|
p, err := validatePath(pathComponents...)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
2015-09-24 06:26:20 +08:00
|
|
|
return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModuleResPath is a a Path representing the 'res' directory in a module's
|
|
|
|
// output directory.
|
|
|
|
type ModuleResPath struct {
|
|
|
|
ModuleOutPath
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Path = ModuleResPath{}
|
|
|
|
|
|
|
|
// PathForModuleRes returns a Path representing the paths... under the module's
|
|
|
|
// 'res' directory.
|
2017-04-11 06:47:24 +08:00
|
|
|
func PathForModuleRes(ctx ModuleContext, pathComponents ...string) ModuleResPath {
|
2018-02-23 05:54:26 +08:00
|
|
|
p, err := validatePath(pathComponents...)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
return ModuleResPath{PathForModuleOut(ctx, "res", p)}
|
|
|
|
}
|
|
|
|
|
2019-10-02 13:05:35 +08:00
|
|
|
// InstallPath is a Path representing a installed file path rooted from the build directory
|
|
|
|
type InstallPath struct {
|
|
|
|
basePath
|
2019-10-03 07:01:35 +08:00
|
|
|
|
|
|
|
baseDir string // "../" for Make paths to convert "out/soong" to "out", "" for Soong paths
|
2019-10-02 13:05:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p InstallPath) writablePath() {}
|
|
|
|
|
|
|
|
func (p InstallPath) String() string {
|
2019-10-03 07:01:35 +08:00
|
|
|
return filepath.Join(p.config.buildDir, p.baseDir, p.path)
|
2019-10-02 13:05:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Join creates a new InstallPath with paths... joined with the current path. The
|
|
|
|
// provided paths... may not use '..' to escape from the current path.
|
|
|
|
func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath {
|
|
|
|
path, err := validatePath(paths...)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
|
|
|
return p.withRel(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p InstallPath) withRel(rel string) InstallPath {
|
|
|
|
p.basePath = p.basePath.withRel(rel)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2019-10-03 07:01:35 +08:00
|
|
|
// ToMakePath returns a new InstallPath that points to Make's install directory instead of Soong's,
|
|
|
|
// i.e. out/ instead of out/soong/.
|
|
|
|
func (p InstallPath) ToMakePath() InstallPath {
|
|
|
|
p.baseDir = "../"
|
|
|
|
return p
|
2019-10-02 13:05:35 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// PathForModuleInstall returns a Path representing the install path for the
|
|
|
|
// module appended with paths...
|
2019-10-02 13:05:35 +08:00
|
|
|
func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
|
2015-09-24 06:26:20 +08:00
|
|
|
var outPaths []string
|
|
|
|
if ctx.Device() {
|
2018-11-13 02:13:39 +08:00
|
|
|
partition := modulePartition(ctx)
|
2017-11-29 16:27:14 +08:00
|
|
|
outPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
|
2015-09-24 06:26:20 +08:00
|
|
|
} else {
|
2017-09-23 03:28:24 +08:00
|
|
|
switch ctx.Os() {
|
|
|
|
case Linux:
|
|
|
|
outPaths = []string{"host", "linux-x86"}
|
|
|
|
case LinuxBionic:
|
|
|
|
// TODO: should this be a separate top level, or shared with linux-x86?
|
|
|
|
outPaths = []string{"host", "linux_bionic-x86"}
|
|
|
|
default:
|
|
|
|
outPaths = []string{"host", ctx.Os().String() + "-x86"}
|
|
|
|
}
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
2015-12-22 06:55:28 +08:00
|
|
|
if ctx.Debug() {
|
|
|
|
outPaths = append([]string{"debug"}, outPaths...)
|
|
|
|
}
|
2017-04-11 06:47:24 +08:00
|
|
|
outPaths = append(outPaths, pathComponents...)
|
2019-10-02 13:05:35 +08:00
|
|
|
|
|
|
|
path, err := validatePath(outPaths...)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
2019-10-03 07:01:35 +08:00
|
|
|
|
|
|
|
ret := InstallPath{basePath{path, ctx.Config(), ""}, ""}
|
|
|
|
if ctx.InstallBypassMake() && ctx.Config().EmbeddedInMake() {
|
|
|
|
ret = ret.ToMakePath()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
2019-10-02 13:05:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func PathForNdkInstall(ctx PathContext, paths ...string) InstallPath {
|
|
|
|
paths = append([]string{"ndk"}, paths...)
|
|
|
|
path, err := validatePath(paths...)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
2019-10-03 07:01:35 +08:00
|
|
|
return InstallPath{basePath{path, ctx.Config(), ""}, ""}
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
|
2019-10-02 13:05:35 +08:00
|
|
|
func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string {
|
2018-11-13 02:13:39 +08:00
|
|
|
rel := Rel(ctx, PathForOutput(ctx, "target", "product", ctx.Config().DeviceName()).String(), path.String())
|
|
|
|
|
|
|
|
return "/" + rel
|
|
|
|
}
|
|
|
|
|
|
|
|
func modulePartition(ctx ModuleInstallPathContext) string {
|
|
|
|
var partition string
|
|
|
|
if ctx.InstallInData() {
|
|
|
|
partition = "data"
|
2019-09-12 01:25:18 +08:00
|
|
|
} else if ctx.InstallInTestcases() {
|
|
|
|
partition = "testcases"
|
2018-11-13 02:13:39 +08:00
|
|
|
} else if ctx.InstallInRecovery() {
|
2019-10-03 02:10:58 +08:00
|
|
|
if ctx.InstallInRoot() {
|
|
|
|
partition = "recovery/root"
|
|
|
|
} else {
|
|
|
|
// the layout of recovery partion is the same as that of system partition
|
|
|
|
partition = "recovery/root/system"
|
|
|
|
}
|
2018-11-13 02:13:39 +08:00
|
|
|
} else if ctx.SocSpecific() {
|
|
|
|
partition = ctx.DeviceConfig().VendorPath()
|
|
|
|
} else if ctx.DeviceSpecific() {
|
|
|
|
partition = ctx.DeviceConfig().OdmPath()
|
|
|
|
} else if ctx.ProductSpecific() {
|
|
|
|
partition = ctx.DeviceConfig().ProductPath()
|
2019-06-25 15:47:17 +08:00
|
|
|
} else if ctx.SystemExtSpecific() {
|
|
|
|
partition = ctx.DeviceConfig().SystemExtPath()
|
2019-10-03 02:10:58 +08:00
|
|
|
} else if ctx.InstallInRoot() {
|
|
|
|
partition = "root"
|
2018-11-13 02:13:39 +08:00
|
|
|
} else {
|
|
|
|
partition = "system"
|
|
|
|
}
|
|
|
|
if ctx.InstallInSanitizerDir() {
|
|
|
|
partition = "data/asan/" + partition
|
|
|
|
}
|
|
|
|
return partition
|
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
// validateSafePath validates a path that we trust (may contain ninja variables).
|
2015-12-22 06:57:11 +08:00
|
|
|
// Ensures that each path component does not attempt to leave its component.
|
2018-02-23 05:54:26 +08:00
|
|
|
func validateSafePath(pathComponents ...string) (string, error) {
|
2017-04-11 06:47:24 +08:00
|
|
|
for _, path := range pathComponents {
|
2015-12-22 06:57:11 +08:00
|
|
|
path := filepath.Clean(path)
|
|
|
|
if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
|
2018-02-23 05:54:26 +08:00
|
|
|
return "", fmt.Errorf("Path is outside directory: %s", path)
|
2015-12-22 06:57:11 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-24 06:26:20 +08:00
|
|
|
// 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.
|
2018-02-23 05:54:26 +08:00
|
|
|
return filepath.Join(pathComponents...), nil
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
|
2015-12-22 06:57:11 +08:00
|
|
|
// 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.
|
2018-02-23 05:54:26 +08:00
|
|
|
func validatePath(pathComponents ...string) (string, error) {
|
2017-04-11 06:47:24 +08:00
|
|
|
for _, path := range pathComponents {
|
2015-09-24 06:26:20 +08:00
|
|
|
if strings.Contains(path, "$") {
|
2018-02-23 05:54:26 +08:00
|
|
|
return "", fmt.Errorf("Path contains invalid character($): %s", path)
|
2015-09-24 06:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
2018-02-23 05:54:26 +08:00
|
|
|
return validateSafePath(pathComponents...)
|
2015-07-15 09:55:36 +08:00
|
|
|
}
|
2017-05-10 04:34:34 +08:00
|
|
|
|
2017-11-29 09:34:01 +08:00
|
|
|
func PathForPhony(ctx PathContext, phony string) WritablePath {
|
|
|
|
if strings.ContainsAny(phony, "$/") {
|
2018-02-23 05:54:26 +08:00
|
|
|
reportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony)
|
2017-11-29 09:34:01 +08:00
|
|
|
}
|
2017-12-12 07:51:44 +08:00
|
|
|
return PhonyPath{basePath{phony, ctx.Config(), ""}}
|
2017-11-29 09:34:01 +08:00
|
|
|
}
|
|
|
|
|
2017-12-12 07:51:44 +08:00
|
|
|
type PhonyPath struct {
|
|
|
|
basePath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p PhonyPath) writablePath() {}
|
|
|
|
|
|
|
|
var _ Path = PhonyPath{}
|
|
|
|
var _ WritablePath = PhonyPath{}
|
|
|
|
|
2017-05-10 04:34:34 +08:00
|
|
|
type testPath struct {
|
|
|
|
basePath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p testPath) String() string {
|
|
|
|
return p.path
|
|
|
|
}
|
|
|
|
|
2019-02-16 03:08:35 +08:00
|
|
|
type testWritablePath struct {
|
|
|
|
testPath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p testPath) writablePath() {}
|
|
|
|
|
|
|
|
// PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from
|
|
|
|
// within tests.
|
2017-05-10 04:34:34 +08:00
|
|
|
func PathForTesting(paths ...string) Path {
|
2018-02-23 05:54:26 +08:00
|
|
|
p, err := validateSafePath(paths...)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-05-10 04:34:34 +08:00
|
|
|
return testPath{basePath{path: p, rel: p}}
|
|
|
|
}
|
|
|
|
|
2019-02-16 03:08:35 +08:00
|
|
|
// PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests.
|
|
|
|
func PathsForTesting(strs ...string) Paths {
|
2017-05-10 04:34:34 +08:00
|
|
|
p := make(Paths, len(strs))
|
|
|
|
for i, s := range strs {
|
|
|
|
p[i] = PathForTesting(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
2018-11-13 02:13:39 +08:00
|
|
|
|
2019-02-16 03:08:35 +08:00
|
|
|
// WritablePathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be
|
|
|
|
// used from within tests.
|
|
|
|
func WritablePathForTesting(paths ...string) WritablePath {
|
|
|
|
p, err := validateSafePath(paths...)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return testWritablePath{testPath{basePath{path: p, rel: p}}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WritablePathsForTesting returns a Path constructed from each element in strs. It should only be used from within
|
|
|
|
// tests.
|
|
|
|
func WritablePathsForTesting(strs ...string) WritablePaths {
|
|
|
|
p := make(WritablePaths, len(strs))
|
|
|
|
for i, s := range strs {
|
|
|
|
p[i] = WritablePathForTesting(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
type testPathContext struct {
|
|
|
|
config Config
|
|
|
|
fs pathtools.FileSystem
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *testPathContext) Fs() pathtools.FileSystem { return x.fs }
|
|
|
|
func (x *testPathContext) Config() Config { return x.config }
|
|
|
|
func (x *testPathContext) AddNinjaFileDeps(...string) {}
|
|
|
|
|
|
|
|
// PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with
|
|
|
|
// PathForOutput.
|
|
|
|
func PathContextForTesting(config Config, fs map[string][]byte) PathContext {
|
|
|
|
return &testPathContext{
|
|
|
|
config: config,
|
|
|
|
fs: pathtools.MockFs(fs),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 02:13:39 +08:00
|
|
|
// Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if
|
|
|
|
// targetPath is not inside basePath.
|
|
|
|
func Rel(ctx PathContext, basePath string, targetPath string) string {
|
|
|
|
rel, isRel := MaybeRel(ctx, basePath, targetPath)
|
|
|
|
if !isRel {
|
|
|
|
reportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return rel
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if
|
|
|
|
// targetPath is not inside basePath.
|
|
|
|
func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) {
|
2019-04-13 02:11:38 +08:00
|
|
|
rel, isRel, err := maybeRelErr(basePath, targetPath)
|
|
|
|
if err != nil {
|
|
|
|
reportPathError(ctx, err)
|
|
|
|
}
|
|
|
|
return rel, isRel
|
|
|
|
}
|
|
|
|
|
|
|
|
func maybeRelErr(basePath string, targetPath string) (string, bool, error) {
|
2018-11-13 02:13:39 +08:00
|
|
|
// filepath.Rel returns an error if one path is absolute and the other is not, handle that case first.
|
|
|
|
if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) {
|
2019-04-13 02:11:38 +08:00
|
|
|
return "", false, nil
|
2018-11-13 02:13:39 +08:00
|
|
|
}
|
|
|
|
rel, err := filepath.Rel(basePath, targetPath)
|
|
|
|
if err != nil {
|
2019-04-13 02:11:38 +08:00
|
|
|
return "", false, err
|
2018-11-13 02:13:39 +08:00
|
|
|
} else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") {
|
2019-04-13 02:11:38 +08:00
|
|
|
return "", false, nil
|
2018-11-13 02:13:39 +08:00
|
|
|
}
|
2019-04-13 02:11:38 +08:00
|
|
|
return rel, true, nil
|
2018-11-13 02:13:39 +08:00
|
|
|
}
|