blob: 3bb5d1b780e1ccb364ca903be16103bac243caf6 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross6e18ca42015-07-14 18:55:36 -070018 "fmt"
Colin Cross6a745c62015-06-16 16:38:10 -070019 "path/filepath"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070020 "reflect"
Colin Cross5e6cfbe2017-11-03 15:20:35 -070021 "sort"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070022 "strings"
23
24 "github.com/google/blueprint"
25 "github.com/google/blueprint/pathtools"
Colin Cross3f40fa42015-01-30 17:27:36 -080026)
27
Dan Willemsen34cc69e2015-09-23 15:26:20 -070028// PathContext is the subset of a (Module|Singleton)Context required by the
29// Path methods.
30type PathContext interface {
Colin Cross294941b2017-02-01 14:10:36 -080031 Fs() pathtools.FileSystem
Colin Crossaabf6792017-11-29 00:27:14 -080032 Config() Config
Dan Willemsen7b310ee2015-12-18 15:11:17 -080033 AddNinjaFileDeps(deps ...string)
Colin Cross3f40fa42015-01-30 17:27:36 -080034}
35
Colin Cross7f19f372016-11-01 11:10:25 -070036type PathGlobContext interface {
37 GlobWithDeps(globPattern string, excludes []string) ([]string, error)
38}
39
Colin Crossaabf6792017-11-29 00:27:14 -080040var _ PathContext = SingletonContext(nil)
41var _ PathContext = ModuleContext(nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -070042
Dan Willemsen00269f22017-07-06 16:59:48 -070043type ModuleInstallPathContext interface {
44 PathContext
45
46 androidBaseContext
47
48 InstallInData() bool
49 InstallInSanitizerDir() bool
50}
51
52var _ ModuleInstallPathContext = ModuleContext(nil)
53
Dan Willemsen34cc69e2015-09-23 15:26:20 -070054// errorfContext is the interface containing the Errorf method matching the
55// Errorf method in blueprint.SingletonContext.
56type errorfContext interface {
57 Errorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -080058}
59
Dan Willemsen34cc69e2015-09-23 15:26:20 -070060var _ errorfContext = blueprint.SingletonContext(nil)
61
62// moduleErrorf is the interface containing the ModuleErrorf method matching
63// the ModuleErrorf method in blueprint.ModuleContext.
64type moduleErrorf interface {
65 ModuleErrorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -080066}
67
Dan Willemsen34cc69e2015-09-23 15:26:20 -070068var _ moduleErrorf = blueprint.ModuleContext(nil)
69
Dan Willemsen34cc69e2015-09-23 15:26:20 -070070// reportPathError will register an error with the attached context. It
71// attempts ctx.ModuleErrorf for a better error message first, then falls
72// back to ctx.Errorf.
Colin Cross1ccfcc32018-02-22 13:54:26 -080073func reportPathError(ctx PathContext, err error) {
74 reportPathErrorf(ctx, "%s", err.Error())
75}
76
77// reportPathErrorf will register an error with the attached context. It
78// attempts ctx.ModuleErrorf for a better error message first, then falls
79// back to ctx.Errorf.
80func reportPathErrorf(ctx PathContext, format string, args ...interface{}) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081 if mctx, ok := ctx.(moduleErrorf); ok {
82 mctx.ModuleErrorf(format, args...)
83 } else if ectx, ok := ctx.(errorfContext); ok {
84 ectx.Errorf(format, args...)
85 } else {
86 panic(fmt.Sprintf(format, args...))
Colin Crossf2298272015-05-12 11:36:53 -070087 }
88}
89
Dan Willemsen34cc69e2015-09-23 15:26:20 -070090type Path interface {
91 // Returns the path in string form
92 String() string
93
Colin Cross4f6fc9c2016-10-26 10:05:25 -070094 // Ext returns the extension of the last element of the path
Dan Willemsen34cc69e2015-09-23 15:26:20 -070095 Ext() string
Colin Cross4f6fc9c2016-10-26 10:05:25 -070096
97 // Base returns the last element of the path
98 Base() string
Colin Crossfaeb7aa2017-02-01 14:12:44 -080099
100 // Rel returns the portion of the path relative to the directory it was created from. For
101 // example, Rel on a PathsForModuleSrc would return the path relative to the module source
Colin Cross0db55682017-12-05 15:36:55 -0800102 // directory, and OutputPath.Join("foo").Rel() would return "foo".
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800103 Rel() string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700104}
105
106// WritablePath is a type of path that can be used as an output for build rules.
107type WritablePath interface {
108 Path
109
Jeff Gaston734e3802017-04-10 15:47:24 -0700110 // the writablePath method doesn't directly do anything,
111 // but it allows a struct to distinguish between whether or not it implements the WritablePath interface
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700112 writablePath()
113}
114
115type genPathProvider interface {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700116 genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700117}
118type objPathProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700119 objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700120}
121type resPathProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700122 resPathWithName(ctx ModuleContext, name string) ModuleResPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700123}
124
125// GenPathWithExt derives a new file path in ctx's generated sources directory
126// from the current path, but with the new extension.
Dan Willemsen21ec4902016-11-02 20:43:13 -0700127func GenPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700128 if path, ok := p.(genPathProvider); ok {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700129 return path.genPathWithExt(ctx, subdir, ext)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700130 }
Colin Cross1ccfcc32018-02-22 13:54:26 -0800131 reportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700132 return PathForModuleGen(ctx)
133}
134
135// ObjPathWithExt derives a new file path in ctx's object directory from the
136// current path, but with the new extension.
Dan Willemsen21ec4902016-11-02 20:43:13 -0700137func ObjPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700138 if path, ok := p.(objPathProvider); ok {
139 return path.objPathWithExt(ctx, subdir, ext)
140 }
Colin Cross1ccfcc32018-02-22 13:54:26 -0800141 reportPathErrorf(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700142 return PathForModuleObj(ctx)
143}
144
145// ResPathWithName derives a new path in ctx's output resource directory, using
146// the current path to create the directory name, and the `name` argument for
147// the filename.
Colin Cross635c3b02016-05-18 15:37:25 -0700148func ResPathWithName(ctx ModuleContext, p Path, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700149 if path, ok := p.(resPathProvider); ok {
150 return path.resPathWithName(ctx, name)
151 }
Colin Cross1ccfcc32018-02-22 13:54:26 -0800152 reportPathErrorf(ctx, "Tried to create res file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700153 return PathForModuleRes(ctx)
154}
155
156// OptionalPath is a container that may or may not contain a valid Path.
157type OptionalPath struct {
158 valid bool
159 path Path
160}
161
162// OptionalPathForPath returns an OptionalPath containing the path.
163func OptionalPathForPath(path Path) OptionalPath {
164 if path == nil {
165 return OptionalPath{}
166 }
167 return OptionalPath{valid: true, path: path}
168}
169
170// Valid returns whether there is a valid path
171func (p OptionalPath) Valid() bool {
172 return p.valid
173}
174
175// Path returns the Path embedded in this OptionalPath. You must be sure that
176// there is a valid path, since this method will panic if there is not.
177func (p OptionalPath) Path() Path {
178 if !p.valid {
179 panic("Requesting an invalid path")
180 }
181 return p.path
182}
183
184// String returns the string version of the Path, or "" if it isn't valid.
185func (p OptionalPath) String() string {
186 if p.valid {
187 return p.path.String()
188 } else {
189 return ""
Colin Crossf2298272015-05-12 11:36:53 -0700190 }
191}
Colin Cross6e18ca42015-07-14 18:55:36 -0700192
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700193// Paths is a slice of Path objects, with helpers to operate on the collection.
194type Paths []Path
195
196// PathsForSource returns Paths rooted from SrcDir
197func PathsForSource(ctx PathContext, paths []string) Paths {
198 ret := make(Paths, len(paths))
199 for i, path := range paths {
200 ret[i] = PathForSource(ctx, path)
201 }
202 return ret
203}
204
Jeff Gaston734e3802017-04-10 15:47:24 -0700205// ExistentPathsForSources returns a list of Paths rooted from SrcDir that are
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800206// found in the tree. If any are not found, they are omitted from the list,
207// and dependencies are added so that we're re-run when they are added.
Colin Cross32f38982018-02-22 11:47:25 -0800208func ExistentPathsForSources(ctx PathContext, paths []string) Paths {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800209 ret := make(Paths, 0, len(paths))
210 for _, path := range paths {
Colin Cross32f38982018-02-22 11:47:25 -0800211 p := ExistentPathForSource(ctx, path)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800212 if p.Valid() {
213 ret = append(ret, p.Path())
214 }
215 }
216 return ret
217}
218
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700219// PathsForModuleSrc returns Paths rooted from the module's local source
220// directory
Colin Cross635c3b02016-05-18 15:37:25 -0700221func PathsForModuleSrc(ctx ModuleContext, paths []string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700222 ret := make(Paths, len(paths))
223 for i, path := range paths {
224 ret[i] = PathForModuleSrc(ctx, path)
225 }
226 return ret
227}
228
229// pathsForModuleSrcFromFullPath returns Paths rooted from the module's local
230// source directory, but strip the local source directory from the beginning of
231// each string.
Colin Cross635c3b02016-05-18 15:37:25 -0700232func pathsForModuleSrcFromFullPath(ctx ModuleContext, paths []string) Paths {
Colin Cross6510f912017-11-29 00:27:14 -0800233 prefix := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir()) + "/"
Colin Cross0f37af02017-09-27 17:42:05 -0700234 if prefix == "./" {
235 prefix = ""
236 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700237 ret := make(Paths, 0, len(paths))
238 for _, p := range paths {
239 path := filepath.Clean(p)
240 if !strings.HasPrefix(path, prefix) {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800241 reportPathErrorf(ctx, "Path '%s' is not in module source directory '%s'", p, prefix)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700242 continue
243 }
244 ret = append(ret, PathForModuleSrc(ctx, path[len(prefix):]))
245 }
246 return ret
247}
248
249// PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's
250// local source directory. If none are provided, use the default if it exists.
Colin Cross635c3b02016-05-18 15:37:25 -0700251func PathsWithOptionalDefaultForModuleSrc(ctx ModuleContext, input []string, def string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700252 if len(input) > 0 {
253 return PathsForModuleSrc(ctx, input)
254 }
255 // Use Glob so that if the default doesn't exist, a dependency is added so that when it
256 // is created, we're run again.
Colin Cross6510f912017-11-29 00:27:14 -0800257 path := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir(), def)
Colin Cross7f19f372016-11-01 11:10:25 -0700258 return ctx.Glob(path, []string{})
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700259}
260
261// Strings returns the Paths in string form
262func (p Paths) Strings() []string {
263 if p == nil {
264 return nil
265 }
266 ret := make([]string, len(p))
267 for i, path := range p {
268 ret[i] = path.String()
269 }
270 return ret
271}
272
Colin Crossb6715442017-10-24 11:13:31 -0700273// FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It
274// modifies the Paths slice contents in place, and returns a subslice of the original slice.
Dan Willemsenfe92c962017-08-29 12:28:37 -0700275func FirstUniquePaths(list Paths) Paths {
276 k := 0
277outer:
278 for i := 0; i < len(list); i++ {
279 for j := 0; j < k; j++ {
280 if list[i] == list[j] {
281 continue outer
282 }
283 }
284 list[k] = list[i]
285 k++
286 }
287 return list[:k]
288}
289
Colin Crossb6715442017-10-24 11:13:31 -0700290// LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It
291// modifies the Paths slice contents in place, and returns a subslice of the original slice.
292func LastUniquePaths(list Paths) Paths {
293 totalSkip := 0
294 for i := len(list) - 1; i >= totalSkip; i-- {
295 skip := 0
296 for j := i - 1; j >= totalSkip; j-- {
297 if list[i] == list[j] {
298 skip++
299 } else {
300 list[j+skip] = list[j]
301 }
302 }
303 totalSkip += skip
304 }
305 return list[totalSkip:]
306}
307
Jeff Gaston294356f2017-09-27 17:05:30 -0700308func indexPathList(s Path, list []Path) int {
309 for i, l := range list {
310 if l == s {
311 return i
312 }
313 }
314
315 return -1
316}
317
318func inPathList(p Path, list []Path) bool {
319 return indexPathList(p, list) != -1
320}
321
322func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) {
323 for _, l := range list {
324 if inPathList(l, filter) {
325 filtered = append(filtered, l)
326 } else {
327 remainder = append(remainder, l)
328 }
329 }
330
331 return
332}
333
Colin Cross93e85952017-08-15 13:34:18 -0700334// HasExt returns true of any of the paths have extension ext, otherwise false
335func (p Paths) HasExt(ext string) bool {
336 for _, path := range p {
337 if path.Ext() == ext {
338 return true
339 }
340 }
341
342 return false
343}
344
345// FilterByExt returns the subset of the paths that have extension ext
346func (p Paths) FilterByExt(ext string) Paths {
347 ret := make(Paths, 0, len(p))
348 for _, path := range p {
349 if path.Ext() == ext {
350 ret = append(ret, path)
351 }
352 }
353 return ret
354}
355
356// FilterOutByExt returns the subset of the paths that do not have extension ext
357func (p Paths) FilterOutByExt(ext string) Paths {
358 ret := make(Paths, 0, len(p))
359 for _, path := range p {
360 if path.Ext() != ext {
361 ret = append(ret, path)
362 }
363 }
364 return ret
365}
366
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700367// DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory
368// (including subdirectories) are in a contiguous subslice of the list, and can be found in
369// O(log(N)) time using a binary search on the directory prefix.
370type DirectorySortedPaths Paths
371
372func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths {
373 ret := append(DirectorySortedPaths(nil), paths...)
374 sort.Slice(ret, func(i, j int) bool {
375 return ret[i].String() < ret[j].String()
376 })
377 return ret
378}
379
380// PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries
381// that are in the specified directory and its subdirectories.
382func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths {
383 prefix := filepath.Clean(dir) + "/"
384 start := sort.Search(len(p), func(i int) bool {
385 return prefix < p[i].String()
386 })
387
388 ret := p[start:]
389
390 end := sort.Search(len(ret), func(i int) bool {
391 return !strings.HasPrefix(ret[i].String(), prefix)
392 })
393
394 ret = ret[:end]
395
396 return Paths(ret)
397}
398
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700399// WritablePaths is a slice of WritablePaths, used for multiple outputs.
400type WritablePaths []WritablePath
401
402// Strings returns the string forms of the writable paths.
403func (p WritablePaths) Strings() []string {
404 if p == nil {
405 return nil
406 }
407 ret := make([]string, len(p))
408 for i, path := range p {
409 ret[i] = path.String()
410 }
411 return ret
412}
413
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800414// Paths returns the WritablePaths as a Paths
415func (p WritablePaths) Paths() Paths {
416 if p == nil {
417 return nil
418 }
419 ret := make(Paths, len(p))
420 for i, path := range p {
421 ret[i] = path
422 }
423 return ret
424}
425
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700426type basePath struct {
427 path string
428 config Config
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800429 rel string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700430}
431
432func (p basePath) Ext() string {
433 return filepath.Ext(p.path)
434}
435
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700436func (p basePath) Base() string {
437 return filepath.Base(p.path)
438}
439
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800440func (p basePath) Rel() string {
441 if p.rel != "" {
442 return p.rel
443 }
444 return p.path
445}
446
Colin Cross0875c522017-11-28 17:34:01 -0800447func (p basePath) String() string {
448 return p.path
449}
450
Colin Cross0db55682017-12-05 15:36:55 -0800451func (p basePath) withRel(rel string) basePath {
452 p.path = filepath.Join(p.path, rel)
453 p.rel = rel
454 return p
455}
456
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700457// SourcePath is a Path representing a file path rooted from SrcDir
458type SourcePath struct {
459 basePath
460}
461
462var _ Path = SourcePath{}
463
Colin Cross0db55682017-12-05 15:36:55 -0800464func (p SourcePath) withRel(rel string) SourcePath {
465 p.basePath = p.basePath.withRel(rel)
466 return p
467}
468
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700469// safePathForSource is for paths that we expect are safe -- only for use by go
470// code that is embedding ninja variables in paths
471func safePathForSource(ctx PathContext, path string) SourcePath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800472 p, err := validateSafePath(path)
473 if err != nil {
474 reportPathError(ctx, err)
475 }
Colin Crossaabf6792017-11-29 00:27:14 -0800476 ret := SourcePath{basePath{p, ctx.Config(), ""}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700477
478 abs, err := filepath.Abs(ret.String())
Colin Cross6e18ca42015-07-14 18:55:36 -0700479 if err != nil {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800480 reportPathError(ctx, err)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700481 return ret
482 }
Colin Crossaabf6792017-11-29 00:27:14 -0800483 buildroot, err := filepath.Abs(ctx.Config().buildDir)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700484 if err != nil {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800485 reportPathError(ctx, err)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700486 return ret
487 }
488 if strings.HasPrefix(abs, buildroot) {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800489 reportPathErrorf(ctx, "source path %s is in output", abs)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700490 return ret
Colin Cross6e18ca42015-07-14 18:55:36 -0700491 }
492
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700493 return ret
494}
495
Colin Cross94a32102018-02-22 14:21:02 -0800496// pathForSource creates a SourcePath from pathComponents, but does not check that it exists.
497func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800498 p, err := validatePath(pathComponents...)
Colin Crossaabf6792017-11-29 00:27:14 -0800499 ret := SourcePath{basePath{p, ctx.Config(), ""}}
Colin Cross1ccfcc32018-02-22 13:54:26 -0800500 if err != nil {
Colin Cross94a32102018-02-22 14:21:02 -0800501 return ret, err
Colin Cross1ccfcc32018-02-22 13:54:26 -0800502 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700503
504 abs, err := filepath.Abs(ret.String())
505 if err != nil {
Colin Cross94a32102018-02-22 14:21:02 -0800506 return ret, err
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700507 }
Colin Crossaabf6792017-11-29 00:27:14 -0800508 buildroot, err := filepath.Abs(ctx.Config().buildDir)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700509 if err != nil {
Colin Cross94a32102018-02-22 14:21:02 -0800510 return ret, err
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700511 }
512 if strings.HasPrefix(abs, buildroot) {
Colin Cross94a32102018-02-22 14:21:02 -0800513 return ret, fmt.Errorf("source path %s is in output", abs)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700514 }
515
Colin Cross94a32102018-02-22 14:21:02 -0800516 if pathtools.IsGlob(ret.String()) {
517 return ret, fmt.Errorf("path may not contain a glob: %s", ret.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700518 }
Colin Cross94a32102018-02-22 14:21:02 -0800519
520 return ret, nil
521}
522
523// existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the
524// path does not exist.
525func existsWithDependencies(ctx PathContext, path SourcePath) (exists bool, err error) {
526 var files []string
527
528 if gctx, ok := ctx.(PathGlobContext); ok {
529 // Use glob to produce proper dependencies, even though we only want
530 // a single file.
531 files, err = gctx.GlobWithDeps(path.String(), nil)
532 } else {
533 var deps []string
534 // We cannot add build statements in this context, so we fall back to
535 // AddNinjaFileDeps
536 files, deps, err = pathtools.Glob(path.String(), nil)
537 ctx.AddNinjaFileDeps(deps...)
538 }
539
540 if err != nil {
541 return false, fmt.Errorf("glob: %s", err.Error())
542 }
543
544 return len(files) > 0, nil
545}
546
547// PathForSource joins the provided path components and validates that the result
548// neither escapes the source dir nor is in the out dir.
549// On error, it will return a usable, but invalid SourcePath, and report a ModuleError.
550func PathForSource(ctx PathContext, pathComponents ...string) SourcePath {
551 path, err := pathForSource(ctx, pathComponents...)
552 if err != nil {
553 reportPathError(ctx, err)
554 }
555
556 if modCtx, ok := ctx.(ModuleContext); ok && ctx.Config().AllowMissingDependencies() {
557 exists, err := existsWithDependencies(ctx, path)
558 if err != nil {
559 reportPathError(ctx, err)
560 }
561 if !exists {
562 modCtx.AddMissingDependencies([]string{path.String()})
563 }
564 } else if exists, _, err := ctx.Fs().Exists(path.String()); err != nil {
565 reportPathErrorf(ctx, "%s: %s", path, err.Error())
566 } else if !exists {
567 reportPathErrorf(ctx, "source path %s does not exist", path)
568 }
569 return path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700570}
571
Jeff Gaston734e3802017-04-10 15:47:24 -0700572// ExistentPathForSource returns an OptionalPath with the SourcePath if the
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700573// path exists, or an empty OptionalPath if it doesn't exist. Dependencies are added
574// so that the ninja file will be regenerated if the state of the path changes.
Colin Cross32f38982018-02-22 11:47:25 -0800575func ExistentPathForSource(ctx PathContext, pathComponents ...string) OptionalPath {
Colin Cross94a32102018-02-22 14:21:02 -0800576 path, err := pathForSource(ctx, pathComponents...)
Colin Cross1ccfcc32018-02-22 13:54:26 -0800577 if err != nil {
578 reportPathError(ctx, err)
579 return OptionalPath{}
580 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700581
Colin Cross94a32102018-02-22 14:21:02 -0800582 exists, err := existsWithDependencies(ctx, path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700583 if err != nil {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800584 reportPathError(ctx, err)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700585 return OptionalPath{}
586 }
Colin Cross94a32102018-02-22 14:21:02 -0800587 if !exists {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700588 return OptionalPath{}
589 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700590 return OptionalPathForPath(path)
591}
592
593func (p SourcePath) String() string {
594 return filepath.Join(p.config.srcDir, p.path)
595}
596
597// Join creates a new SourcePath with paths... joined with the current path. The
598// provided paths... may not use '..' to escape from the current path.
599func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800600 path, err := validatePath(paths...)
601 if err != nil {
602 reportPathError(ctx, err)
603 }
Colin Cross0db55682017-12-05 15:36:55 -0800604 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700605}
606
607// OverlayPath returns the overlay for `path' if it exists. This assumes that the
608// SourcePath is the path to a resource overlay directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700609func (p SourcePath) OverlayPath(ctx ModuleContext, path Path) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700610 var relDir string
611 if moduleSrcPath, ok := path.(ModuleSrcPath); ok {
Colin Cross7fc17db2017-02-01 14:07:55 -0800612 relDir = moduleSrcPath.path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700613 } else if srcPath, ok := path.(SourcePath); ok {
614 relDir = srcPath.path
615 } else {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800616 reportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700617 return OptionalPath{}
618 }
619 dir := filepath.Join(p.config.srcDir, p.path, relDir)
620 // Use Glob so that we are run again if the directory is added.
Colin Cross7f19f372016-11-01 11:10:25 -0700621 if pathtools.IsGlob(dir) {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800622 reportPathErrorf(ctx, "Path may not contain a glob: %s", dir)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800623 }
Colin Cross7f19f372016-11-01 11:10:25 -0700624 paths, err := ctx.GlobWithDeps(dir, []string{})
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700625 if err != nil {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800626 reportPathErrorf(ctx, "glob: %s", err.Error())
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700627 return OptionalPath{}
628 }
629 if len(paths) == 0 {
630 return OptionalPath{}
631 }
632 relPath, err := filepath.Rel(p.config.srcDir, paths[0])
633 if err != nil {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800634 reportPathError(ctx, err)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700635 return OptionalPath{}
636 }
637 return OptionalPathForPath(PathForSource(ctx, relPath))
638}
639
640// OutputPath is a Path representing a file path rooted from the build directory
641type OutputPath struct {
642 basePath
643}
644
Colin Cross702e0f82017-10-18 17:27:54 -0700645func (p OutputPath) withRel(rel string) OutputPath {
Colin Cross0db55682017-12-05 15:36:55 -0800646 p.basePath = p.basePath.withRel(rel)
Colin Cross702e0f82017-10-18 17:27:54 -0700647 return p
648}
649
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700650var _ Path = OutputPath{}
651
Jeff Gaston734e3802017-04-10 15:47:24 -0700652// PathForOutput joins the provided paths and returns an OutputPath that is
653// validated to not escape the build dir.
654// On error, it will return a usable, but invalid OutputPath, and report a ModuleError.
655func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800656 path, err := validatePath(pathComponents...)
657 if err != nil {
658 reportPathError(ctx, err)
659 }
Colin Crossaabf6792017-11-29 00:27:14 -0800660 return OutputPath{basePath{path, ctx.Config(), ""}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700661}
662
663func (p OutputPath) writablePath() {}
664
665func (p OutputPath) String() string {
666 return filepath.Join(p.config.buildDir, p.path)
667}
668
Colin Crossa2344662016-03-24 13:14:12 -0700669func (p OutputPath) RelPathString() string {
670 return p.path
671}
672
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700673// Join creates a new OutputPath with paths... joined with the current path. The
674// provided paths... may not use '..' to escape from the current path.
675func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800676 path, err := validatePath(paths...)
677 if err != nil {
678 reportPathError(ctx, err)
679 }
Colin Cross0db55682017-12-05 15:36:55 -0800680 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700681}
682
683// PathForIntermediates returns an OutputPath representing the top-level
684// intermediates directory.
685func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800686 path, err := validatePath(paths...)
687 if err != nil {
688 reportPathError(ctx, err)
689 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700690 return PathForOutput(ctx, ".intermediates", path)
691}
692
693// ModuleSrcPath is a Path representing a file rooted from a module's local source dir
694type ModuleSrcPath struct {
Colin Cross7fc17db2017-02-01 14:07:55 -0800695 SourcePath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700696}
697
698var _ Path = ModuleSrcPath{}
699var _ genPathProvider = ModuleSrcPath{}
700var _ objPathProvider = ModuleSrcPath{}
701var _ resPathProvider = ModuleSrcPath{}
702
703// PathForModuleSrc returns a ModuleSrcPath representing the paths... under the
704// module's local source directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700705func PathForModuleSrc(ctx ModuleContext, paths ...string) ModuleSrcPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800706 p, err := validatePath(paths...)
707 if err != nil {
708 reportPathError(ctx, err)
709 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800710 path := ModuleSrcPath{PathForSource(ctx, ctx.ModuleDir(), p)}
711 path.basePath.rel = p
712 return path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700713}
714
715// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
716// valid path if p is non-nil.
Colin Cross635c3b02016-05-18 15:37:25 -0700717func OptionalPathForModuleSrc(ctx ModuleContext, p *string) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700718 if p == nil {
719 return OptionalPath{}
720 }
721 return OptionalPathForPath(PathForModuleSrc(ctx, *p))
722}
723
Dan Willemsen21ec4902016-11-02 20:43:13 -0700724func (p ModuleSrcPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Colin Cross7fc17db2017-02-01 14:07:55 -0800725 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700726}
727
Colin Cross635c3b02016-05-18 15:37:25 -0700728func (p ModuleSrcPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Colin Cross7fc17db2017-02-01 14:07:55 -0800729 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700730}
731
Colin Cross635c3b02016-05-18 15:37:25 -0700732func (p ModuleSrcPath) resPathWithName(ctx ModuleContext, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700733 // TODO: Use full directory if the new ctx is not the current ctx?
734 return PathForModuleRes(ctx, p.path, name)
735}
736
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800737func (p ModuleSrcPath) WithSubDir(ctx ModuleContext, subdir string) ModuleSrcPath {
738 subdir = PathForModuleSrc(ctx, subdir).String()
739 var err error
740 rel, err := filepath.Rel(subdir, p.path)
741 if err != nil {
742 ctx.ModuleErrorf("source file %q is not under path %q", p.path, subdir)
743 return p
744 }
745 p.rel = rel
746 return p
747}
748
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700749// ModuleOutPath is a Path representing a module's output directory.
750type ModuleOutPath struct {
751 OutputPath
752}
753
754var _ Path = ModuleOutPath{}
755
Colin Cross702e0f82017-10-18 17:27:54 -0700756func pathForModule(ctx ModuleContext) OutputPath {
757 return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir())
758}
759
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800760// PathForVndkRefDump returns an OptionalPath representing the path of the reference
761// abi dump for the given module. This is not guaranteed to be valid.
762func PathForVndkRefAbiDump(ctx ModuleContext, version, fileName string, vndkOrNdk, isSourceDump bool) OptionalPath {
763 archName := ctx.Arch().ArchType.Name
764 var sourceOrBinaryDir string
765 var vndkOrNdkDir string
766 var ext string
767 if isSourceDump {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700768 ext = ".lsdump.gz"
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800769 sourceOrBinaryDir = "source-based"
770 } else {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700771 ext = ".bdump.gz"
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800772 sourceOrBinaryDir = "binary-based"
773 }
774 if vndkOrNdk {
775 vndkOrNdkDir = "vndk"
776 } else {
777 vndkOrNdkDir = "ndk"
778 }
779 refDumpFileStr := "prebuilts/abi-dumps/" + vndkOrNdkDir + "/" + version + "/" +
780 archName + "/" + sourceOrBinaryDir + "/" + fileName + ext
Colin Cross32f38982018-02-22 11:47:25 -0800781 return ExistentPathForSource(ctx, refDumpFileStr)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800782}
783
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700784// PathForModuleOut returns a Path representing the paths... under the module's
785// output directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700786func PathForModuleOut(ctx ModuleContext, paths ...string) ModuleOutPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800787 p, err := validatePath(paths...)
788 if err != nil {
789 reportPathError(ctx, err)
790 }
Colin Cross702e0f82017-10-18 17:27:54 -0700791 return ModuleOutPath{
792 OutputPath: pathForModule(ctx).withRel(p),
793 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700794}
795
796// ModuleGenPath is a Path representing the 'gen' directory in a module's output
797// directory. Mainly used for generated sources.
798type ModuleGenPath struct {
799 ModuleOutPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700800}
801
802var _ Path = ModuleGenPath{}
803var _ genPathProvider = ModuleGenPath{}
804var _ objPathProvider = ModuleGenPath{}
805
806// PathForModuleGen returns a Path representing the paths... under the module's
807// `gen' directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700808func PathForModuleGen(ctx ModuleContext, paths ...string) ModuleGenPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800809 p, err := validatePath(paths...)
810 if err != nil {
811 reportPathError(ctx, err)
812 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700813 return ModuleGenPath{
Colin Cross702e0f82017-10-18 17:27:54 -0700814 ModuleOutPath: ModuleOutPath{
815 OutputPath: pathForModule(ctx).withRel("gen").withRel(p),
816 },
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700817 }
818}
819
Dan Willemsen21ec4902016-11-02 20:43:13 -0700820func (p ModuleGenPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700821 // TODO: make a different path for local vs remote generated files?
Dan Willemsen21ec4902016-11-02 20:43:13 -0700822 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700823}
824
Colin Cross635c3b02016-05-18 15:37:25 -0700825func (p ModuleGenPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700826 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
827}
828
829// ModuleObjPath is a Path representing the 'obj' directory in a module's output
830// directory. Used for compiled objects.
831type ModuleObjPath struct {
832 ModuleOutPath
833}
834
835var _ Path = ModuleObjPath{}
836
837// PathForModuleObj returns a Path representing the paths... under the module's
838// 'obj' directory.
Jeff Gaston734e3802017-04-10 15:47:24 -0700839func PathForModuleObj(ctx ModuleContext, pathComponents ...string) ModuleObjPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800840 p, err := validatePath(pathComponents...)
841 if err != nil {
842 reportPathError(ctx, err)
843 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700844 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
845}
846
847// ModuleResPath is a a Path representing the 'res' directory in a module's
848// output directory.
849type ModuleResPath struct {
850 ModuleOutPath
851}
852
853var _ Path = ModuleResPath{}
854
855// PathForModuleRes returns a Path representing the paths... under the module's
856// 'res' directory.
Jeff Gaston734e3802017-04-10 15:47:24 -0700857func PathForModuleRes(ctx ModuleContext, pathComponents ...string) ModuleResPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800858 p, err := validatePath(pathComponents...)
859 if err != nil {
860 reportPathError(ctx, err)
861 }
862
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700863 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
864}
865
866// PathForModuleInstall returns a Path representing the install path for the
867// module appended with paths...
Dan Willemsen00269f22017-07-06 16:59:48 -0700868func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700869 var outPaths []string
870 if ctx.Device() {
Vishwath Mohan87f3b242017-06-07 12:31:57 -0700871 var partition string
Dan Willemsen00269f22017-07-06 16:59:48 -0700872 if ctx.InstallInData() {
Vishwath Mohan87f3b242017-06-07 12:31:57 -0700873 partition = "data"
Jiyong Park2db76922017-11-08 16:03:48 +0900874 } else if ctx.SocSpecific() {
Dan Willemsen00269f22017-07-06 16:59:48 -0700875 partition = ctx.DeviceConfig().VendorPath()
Jiyong Park2db76922017-11-08 16:03:48 +0900876 } else if ctx.DeviceSpecific() {
877 partition = ctx.DeviceConfig().OdmPath()
878 } else if ctx.ProductSpecific() {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900879 partition = ctx.DeviceConfig().ProductPath()
Vishwath Mohan87f3b242017-06-07 12:31:57 -0700880 } else {
881 partition = "system"
Dan Willemsen782a2d12015-12-21 14:55:28 -0800882 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700883
884 if ctx.InstallInSanitizerDir() {
885 partition = "data/asan/" + partition
Dan Willemsen782a2d12015-12-21 14:55:28 -0800886 }
Colin Cross6510f912017-11-29 00:27:14 -0800887 outPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700888 } else {
Dan Willemsen866b5632017-09-22 12:28:24 -0700889 switch ctx.Os() {
890 case Linux:
891 outPaths = []string{"host", "linux-x86"}
892 case LinuxBionic:
893 // TODO: should this be a separate top level, or shared with linux-x86?
894 outPaths = []string{"host", "linux_bionic-x86"}
895 default:
896 outPaths = []string{"host", ctx.Os().String() + "-x86"}
897 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700898 }
Dan Willemsen782a2d12015-12-21 14:55:28 -0800899 if ctx.Debug() {
900 outPaths = append([]string{"debug"}, outPaths...)
901 }
Jeff Gaston734e3802017-04-10 15:47:24 -0700902 outPaths = append(outPaths, pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700903 return PathForOutput(ctx, outPaths...)
904}
905
906// validateSafePath validates a path that we trust (may contain ninja variables).
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800907// Ensures that each path component does not attempt to leave its component.
Colin Cross1ccfcc32018-02-22 13:54:26 -0800908func validateSafePath(pathComponents ...string) (string, error) {
Jeff Gaston734e3802017-04-10 15:47:24 -0700909 for _, path := range pathComponents {
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800910 path := filepath.Clean(path)
911 if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800912 return "", fmt.Errorf("Path is outside directory: %s", path)
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800913 }
914 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700915 // TODO: filepath.Join isn't necessarily correct with embedded ninja
916 // variables. '..' may remove the entire ninja variable, even if it
917 // will be expanded to multiple nested directories.
Colin Cross1ccfcc32018-02-22 13:54:26 -0800918 return filepath.Join(pathComponents...), nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700919}
920
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800921// validatePath validates that a path does not include ninja variables, and that
922// each path component does not attempt to leave its component. Returns a joined
923// version of each path component.
Colin Cross1ccfcc32018-02-22 13:54:26 -0800924func validatePath(pathComponents ...string) (string, error) {
Jeff Gaston734e3802017-04-10 15:47:24 -0700925 for _, path := range pathComponents {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700926 if strings.Contains(path, "$") {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800927 return "", fmt.Errorf("Path contains invalid character($): %s", path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700928 }
929 }
Colin Cross1ccfcc32018-02-22 13:54:26 -0800930 return validateSafePath(pathComponents...)
Colin Cross6e18ca42015-07-14 18:55:36 -0700931}
Colin Cross5b529592017-05-09 13:34:34 -0700932
Colin Cross0875c522017-11-28 17:34:01 -0800933func PathForPhony(ctx PathContext, phony string) WritablePath {
934 if strings.ContainsAny(phony, "$/") {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800935 reportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony)
Colin Cross0875c522017-11-28 17:34:01 -0800936 }
Colin Cross74e3fe42017-12-11 15:51:44 -0800937 return PhonyPath{basePath{phony, ctx.Config(), ""}}
Colin Cross0875c522017-11-28 17:34:01 -0800938}
939
Colin Cross74e3fe42017-12-11 15:51:44 -0800940type PhonyPath struct {
941 basePath
942}
943
944func (p PhonyPath) writablePath() {}
945
946var _ Path = PhonyPath{}
947var _ WritablePath = PhonyPath{}
948
Colin Cross5b529592017-05-09 13:34:34 -0700949type testPath struct {
950 basePath
951}
952
953func (p testPath) String() string {
954 return p.path
955}
956
957func PathForTesting(paths ...string) Path {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800958 p, err := validateSafePath(paths...)
959 if err != nil {
960 panic(err)
961 }
Colin Cross5b529592017-05-09 13:34:34 -0700962 return testPath{basePath{path: p, rel: p}}
963}
964
965func PathsForTesting(strs []string) Paths {
966 p := make(Paths, len(strs))
967 for i, s := range strs {
968 p[i] = PathForTesting(s)
969 }
970
971 return p
972}