blob: 57ebae2a3ba1cb25634a75687670ca9fd71dccb0 [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
Jiyong Parkf9332f12018-02-01 00:54:12 +090050 InstallInRecovery() bool
Dan Willemsen00269f22017-07-06 16:59:48 -070051}
52
53var _ ModuleInstallPathContext = ModuleContext(nil)
54
Dan Willemsen34cc69e2015-09-23 15:26:20 -070055// errorfContext is the interface containing the Errorf method matching the
56// Errorf method in blueprint.SingletonContext.
57type errorfContext interface {
58 Errorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -080059}
60
Dan Willemsen34cc69e2015-09-23 15:26:20 -070061var _ errorfContext = blueprint.SingletonContext(nil)
62
63// moduleErrorf is the interface containing the ModuleErrorf method matching
64// the ModuleErrorf method in blueprint.ModuleContext.
65type moduleErrorf interface {
66 ModuleErrorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -080067}
68
Dan Willemsen34cc69e2015-09-23 15:26:20 -070069var _ moduleErrorf = blueprint.ModuleContext(nil)
70
Dan Willemsen34cc69e2015-09-23 15:26:20 -070071// reportPathError will register an error with the attached context. It
72// attempts ctx.ModuleErrorf for a better error message first, then falls
73// back to ctx.Errorf.
Colin Cross1ccfcc32018-02-22 13:54:26 -080074func reportPathError(ctx PathContext, err error) {
75 reportPathErrorf(ctx, "%s", err.Error())
76}
77
78// reportPathErrorf will register an error with the attached context. It
79// attempts ctx.ModuleErrorf for a better error message first, then falls
80// back to ctx.Errorf.
81func reportPathErrorf(ctx PathContext, format string, args ...interface{}) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082 if mctx, ok := ctx.(moduleErrorf); ok {
83 mctx.ModuleErrorf(format, args...)
84 } else if ectx, ok := ctx.(errorfContext); ok {
85 ectx.Errorf(format, args...)
86 } else {
87 panic(fmt.Sprintf(format, args...))
Colin Crossf2298272015-05-12 11:36:53 -070088 }
89}
90
Dan Willemsen34cc69e2015-09-23 15:26:20 -070091type Path interface {
92 // Returns the path in string form
93 String() string
94
Colin Cross4f6fc9c2016-10-26 10:05:25 -070095 // Ext returns the extension of the last element of the path
Dan Willemsen34cc69e2015-09-23 15:26:20 -070096 Ext() string
Colin Cross4f6fc9c2016-10-26 10:05:25 -070097
98 // Base returns the last element of the path
99 Base() string
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800100
101 // Rel returns the portion of the path relative to the directory it was created from. For
102 // example, Rel on a PathsForModuleSrc would return the path relative to the module source
Colin Cross0db55682017-12-05 15:36:55 -0800103 // directory, and OutputPath.Join("foo").Rel() would return "foo".
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800104 Rel() string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700105}
106
107// WritablePath is a type of path that can be used as an output for build rules.
108type WritablePath interface {
109 Path
110
Jeff Gaston734e3802017-04-10 15:47:24 -0700111 // the writablePath method doesn't directly do anything,
112 // but it allows a struct to distinguish between whether or not it implements the WritablePath interface
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700113 writablePath()
114}
115
116type genPathProvider interface {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700117 genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700118}
119type objPathProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700120 objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700121}
122type resPathProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700123 resPathWithName(ctx ModuleContext, name string) ModuleResPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700124}
125
126// GenPathWithExt derives a new file path in ctx's generated sources directory
127// from the current path, but with the new extension.
Dan Willemsen21ec4902016-11-02 20:43:13 -0700128func GenPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700129 if path, ok := p.(genPathProvider); ok {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700130 return path.genPathWithExt(ctx, subdir, ext)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700131 }
Colin Cross1ccfcc32018-02-22 13:54:26 -0800132 reportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700133 return PathForModuleGen(ctx)
134}
135
136// ObjPathWithExt derives a new file path in ctx's object directory from the
137// current path, but with the new extension.
Dan Willemsen21ec4902016-11-02 20:43:13 -0700138func ObjPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700139 if path, ok := p.(objPathProvider); ok {
140 return path.objPathWithExt(ctx, subdir, ext)
141 }
Colin Cross1ccfcc32018-02-22 13:54:26 -0800142 reportPathErrorf(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700143 return PathForModuleObj(ctx)
144}
145
146// ResPathWithName derives a new path in ctx's output resource directory, using
147// the current path to create the directory name, and the `name` argument for
148// the filename.
Colin Cross635c3b02016-05-18 15:37:25 -0700149func ResPathWithName(ctx ModuleContext, p Path, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700150 if path, ok := p.(resPathProvider); ok {
151 return path.resPathWithName(ctx, name)
152 }
Colin Cross1ccfcc32018-02-22 13:54:26 -0800153 reportPathErrorf(ctx, "Tried to create res file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700154 return PathForModuleRes(ctx)
155}
156
157// OptionalPath is a container that may or may not contain a valid Path.
158type OptionalPath struct {
159 valid bool
160 path Path
161}
162
163// OptionalPathForPath returns an OptionalPath containing the path.
164func OptionalPathForPath(path Path) OptionalPath {
165 if path == nil {
166 return OptionalPath{}
167 }
168 return OptionalPath{valid: true, path: path}
169}
170
171// Valid returns whether there is a valid path
172func (p OptionalPath) Valid() bool {
173 return p.valid
174}
175
176// Path returns the Path embedded in this OptionalPath. You must be sure that
177// there is a valid path, since this method will panic if there is not.
178func (p OptionalPath) Path() Path {
179 if !p.valid {
180 panic("Requesting an invalid path")
181 }
182 return p.path
183}
184
185// String returns the string version of the Path, or "" if it isn't valid.
186func (p OptionalPath) String() string {
187 if p.valid {
188 return p.path.String()
189 } else {
190 return ""
Colin Crossf2298272015-05-12 11:36:53 -0700191 }
192}
Colin Cross6e18ca42015-07-14 18:55:36 -0700193
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700194// Paths is a slice of Path objects, with helpers to operate on the collection.
195type Paths []Path
196
197// PathsForSource returns Paths rooted from SrcDir
198func PathsForSource(ctx PathContext, paths []string) Paths {
199 ret := make(Paths, len(paths))
200 for i, path := range paths {
201 ret[i] = PathForSource(ctx, path)
202 }
203 return ret
204}
205
Jeff Gaston734e3802017-04-10 15:47:24 -0700206// ExistentPathsForSources returns a list of Paths rooted from SrcDir that are
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800207// found in the tree. If any are not found, they are omitted from the list,
208// and dependencies are added so that we're re-run when they are added.
Colin Cross32f38982018-02-22 11:47:25 -0800209func ExistentPathsForSources(ctx PathContext, paths []string) Paths {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800210 ret := make(Paths, 0, len(paths))
211 for _, path := range paths {
Colin Cross32f38982018-02-22 11:47:25 -0800212 p := ExistentPathForSource(ctx, path)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800213 if p.Valid() {
214 ret = append(ret, p.Path())
215 }
216 }
217 return ret
218}
219
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700220// PathsForModuleSrc returns Paths rooted from the module's local source
221// directory
Colin Cross635c3b02016-05-18 15:37:25 -0700222func PathsForModuleSrc(ctx ModuleContext, paths []string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700223 ret := make(Paths, len(paths))
224 for i, path := range paths {
225 ret[i] = PathForModuleSrc(ctx, path)
226 }
227 return ret
228}
229
230// pathsForModuleSrcFromFullPath returns Paths rooted from the module's local
231// source directory, but strip the local source directory from the beginning of
Dan Willemsen540a78c2018-02-26 21:50:08 -0800232// each string. If incDirs is false, strip paths with a trailing '/' from the list.
233func pathsForModuleSrcFromFullPath(ctx ModuleContext, paths []string, incDirs bool) Paths {
Colin Cross6510f912017-11-29 00:27:14 -0800234 prefix := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir()) + "/"
Colin Cross0f37af02017-09-27 17:42:05 -0700235 if prefix == "./" {
236 prefix = ""
237 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700238 ret := make(Paths, 0, len(paths))
239 for _, p := range paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -0800240 if !incDirs && strings.HasSuffix(p, "/") {
241 continue
242 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700243 path := filepath.Clean(p)
244 if !strings.HasPrefix(path, prefix) {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800245 reportPathErrorf(ctx, "Path '%s' is not in module source directory '%s'", p, prefix)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700246 continue
247 }
Colin Crosse3924e12018-08-15 20:18:53 -0700248
249 srcPath, err := pathForSource(ctx, ctx.ModuleDir(), path[len(prefix):])
250 if err != nil {
251 reportPathError(ctx, err)
252 continue
253 }
254
255 moduleSrcPath := ModuleSrcPath{srcPath}
256 moduleSrcPath.basePath.rel = srcPath.path
257
258 ret = append(ret, moduleSrcPath)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700259 }
260 return ret
261}
262
263// PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's
264// local source directory. If none are provided, use the default if it exists.
Colin Cross635c3b02016-05-18 15:37:25 -0700265func PathsWithOptionalDefaultForModuleSrc(ctx ModuleContext, input []string, def string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700266 if len(input) > 0 {
267 return PathsForModuleSrc(ctx, input)
268 }
269 // Use Glob so that if the default doesn't exist, a dependency is added so that when it
270 // is created, we're run again.
Colin Cross6510f912017-11-29 00:27:14 -0800271 path := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir(), def)
Colin Cross461b4452018-02-23 09:22:42 -0800272 return ctx.Glob(path, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700273}
274
275// Strings returns the Paths in string form
276func (p Paths) Strings() []string {
277 if p == nil {
278 return nil
279 }
280 ret := make([]string, len(p))
281 for i, path := range p {
282 ret[i] = path.String()
283 }
284 return ret
285}
286
Colin Crossb6715442017-10-24 11:13:31 -0700287// FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It
288// modifies the Paths slice contents in place, and returns a subslice of the original slice.
Dan Willemsenfe92c962017-08-29 12:28:37 -0700289func FirstUniquePaths(list Paths) Paths {
290 k := 0
291outer:
292 for i := 0; i < len(list); i++ {
293 for j := 0; j < k; j++ {
294 if list[i] == list[j] {
295 continue outer
296 }
297 }
298 list[k] = list[i]
299 k++
300 }
301 return list[:k]
302}
303
Colin Crossb6715442017-10-24 11:13:31 -0700304// LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It
305// modifies the Paths slice contents in place, and returns a subslice of the original slice.
306func LastUniquePaths(list Paths) Paths {
307 totalSkip := 0
308 for i := len(list) - 1; i >= totalSkip; i-- {
309 skip := 0
310 for j := i - 1; j >= totalSkip; j-- {
311 if list[i] == list[j] {
312 skip++
313 } else {
314 list[j+skip] = list[j]
315 }
316 }
317 totalSkip += skip
318 }
319 return list[totalSkip:]
320}
321
Colin Crossa140bb02018-04-17 10:52:26 -0700322// ReversePaths returns a copy of a Paths in reverse order.
323func ReversePaths(list Paths) Paths {
324 if list == nil {
325 return nil
326 }
327 ret := make(Paths, len(list))
328 for i := range list {
329 ret[i] = list[len(list)-1-i]
330 }
331 return ret
332}
333
Jeff Gaston294356f2017-09-27 17:05:30 -0700334func indexPathList(s Path, list []Path) int {
335 for i, l := range list {
336 if l == s {
337 return i
338 }
339 }
340
341 return -1
342}
343
344func inPathList(p Path, list []Path) bool {
345 return indexPathList(p, list) != -1
346}
347
348func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) {
349 for _, l := range list {
350 if inPathList(l, filter) {
351 filtered = append(filtered, l)
352 } else {
353 remainder = append(remainder, l)
354 }
355 }
356
357 return
358}
359
Colin Cross93e85952017-08-15 13:34:18 -0700360// HasExt returns true of any of the paths have extension ext, otherwise false
361func (p Paths) HasExt(ext string) bool {
362 for _, path := range p {
363 if path.Ext() == ext {
364 return true
365 }
366 }
367
368 return false
369}
370
371// FilterByExt returns the subset of the paths that have extension ext
372func (p Paths) FilterByExt(ext string) Paths {
373 ret := make(Paths, 0, len(p))
374 for _, path := range p {
375 if path.Ext() == ext {
376 ret = append(ret, path)
377 }
378 }
379 return ret
380}
381
382// FilterOutByExt returns the subset of the paths that do not have extension ext
383func (p Paths) FilterOutByExt(ext string) Paths {
384 ret := make(Paths, 0, len(p))
385 for _, path := range p {
386 if path.Ext() != ext {
387 ret = append(ret, path)
388 }
389 }
390 return ret
391}
392
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700393// DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory
394// (including subdirectories) are in a contiguous subslice of the list, and can be found in
395// O(log(N)) time using a binary search on the directory prefix.
396type DirectorySortedPaths Paths
397
398func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths {
399 ret := append(DirectorySortedPaths(nil), paths...)
400 sort.Slice(ret, func(i, j int) bool {
401 return ret[i].String() < ret[j].String()
402 })
403 return ret
404}
405
406// PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries
407// that are in the specified directory and its subdirectories.
408func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths {
409 prefix := filepath.Clean(dir) + "/"
410 start := sort.Search(len(p), func(i int) bool {
411 return prefix < p[i].String()
412 })
413
414 ret := p[start:]
415
416 end := sort.Search(len(ret), func(i int) bool {
417 return !strings.HasPrefix(ret[i].String(), prefix)
418 })
419
420 ret = ret[:end]
421
422 return Paths(ret)
423}
424
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700425// WritablePaths is a slice of WritablePaths, used for multiple outputs.
426type WritablePaths []WritablePath
427
428// Strings returns the string forms of the writable paths.
429func (p WritablePaths) Strings() []string {
430 if p == nil {
431 return nil
432 }
433 ret := make([]string, len(p))
434 for i, path := range p {
435 ret[i] = path.String()
436 }
437 return ret
438}
439
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800440// Paths returns the WritablePaths as a Paths
441func (p WritablePaths) Paths() Paths {
442 if p == nil {
443 return nil
444 }
445 ret := make(Paths, len(p))
446 for i, path := range p {
447 ret[i] = path
448 }
449 return ret
450}
451
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700452type basePath struct {
453 path string
454 config Config
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800455 rel string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700456}
457
458func (p basePath) Ext() string {
459 return filepath.Ext(p.path)
460}
461
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700462func (p basePath) Base() string {
463 return filepath.Base(p.path)
464}
465
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800466func (p basePath) Rel() string {
467 if p.rel != "" {
468 return p.rel
469 }
470 return p.path
471}
472
Colin Cross0875c522017-11-28 17:34:01 -0800473func (p basePath) String() string {
474 return p.path
475}
476
Colin Cross0db55682017-12-05 15:36:55 -0800477func (p basePath) withRel(rel string) basePath {
478 p.path = filepath.Join(p.path, rel)
479 p.rel = rel
480 return p
481}
482
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700483// SourcePath is a Path representing a file path rooted from SrcDir
484type SourcePath struct {
485 basePath
486}
487
488var _ Path = SourcePath{}
489
Colin Cross0db55682017-12-05 15:36:55 -0800490func (p SourcePath) withRel(rel string) SourcePath {
491 p.basePath = p.basePath.withRel(rel)
492 return p
493}
494
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700495// safePathForSource is for paths that we expect are safe -- only for use by go
496// code that is embedding ninja variables in paths
497func safePathForSource(ctx PathContext, path string) SourcePath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800498 p, err := validateSafePath(path)
499 if err != nil {
500 reportPathError(ctx, err)
501 }
Colin Crossaabf6792017-11-29 00:27:14 -0800502 ret := SourcePath{basePath{p, ctx.Config(), ""}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700503
504 abs, err := filepath.Abs(ret.String())
Colin Cross6e18ca42015-07-14 18:55:36 -0700505 if err != nil {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800506 reportPathError(ctx, err)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700507 return ret
508 }
Colin Crossaabf6792017-11-29 00:27:14 -0800509 buildroot, err := filepath.Abs(ctx.Config().buildDir)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700510 if err != nil {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800511 reportPathError(ctx, err)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700512 return ret
513 }
514 if strings.HasPrefix(abs, buildroot) {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800515 reportPathErrorf(ctx, "source path %s is in output", abs)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700516 return ret
Colin Cross6e18ca42015-07-14 18:55:36 -0700517 }
518
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700519 return ret
520}
521
Colin Cross192e97a2018-02-22 14:21:02 -0800522// pathForSource creates a SourcePath from pathComponents, but does not check that it exists.
523func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
Colin Crossc48c1432018-02-23 07:09:01 +0000524 p, err := validatePath(pathComponents...)
525 ret := SourcePath{basePath{p, ctx.Config(), ""}}
Colin Cross94a32102018-02-22 14:21:02 -0800526 if err != nil {
Colin Cross192e97a2018-02-22 14:21:02 -0800527 return ret, err
Colin Cross94a32102018-02-22 14:21:02 -0800528 }
529
Colin Crossc48c1432018-02-23 07:09:01 +0000530 abs, err := filepath.Abs(ret.String())
531 if err != nil {
Colin Cross192e97a2018-02-22 14:21:02 -0800532 return ret, err
Colin Cross94a32102018-02-22 14:21:02 -0800533 }
Colin Crossc48c1432018-02-23 07:09:01 +0000534 buildroot, err := filepath.Abs(ctx.Config().buildDir)
535 if err != nil {
Colin Cross192e97a2018-02-22 14:21:02 -0800536 return ret, err
Colin Crossc48c1432018-02-23 07:09:01 +0000537 }
538 if strings.HasPrefix(abs, buildroot) {
Colin Cross192e97a2018-02-22 14:21:02 -0800539 return ret, fmt.Errorf("source path %s is in output", abs)
Colin Crossc48c1432018-02-23 07:09:01 +0000540 }
541
Colin Cross192e97a2018-02-22 14:21:02 -0800542 return ret, nil
543}
544
545// existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the
546// path does not exist.
547func existsWithDependencies(ctx PathContext, path SourcePath) (exists bool, err error) {
548 var files []string
549
550 if gctx, ok := ctx.(PathGlobContext); ok {
551 // Use glob to produce proper dependencies, even though we only want
552 // a single file.
553 files, err = gctx.GlobWithDeps(path.String(), nil)
554 } else {
555 var deps []string
556 // We cannot add build statements in this context, so we fall back to
557 // AddNinjaFileDeps
558 files, deps, err = pathtools.Glob(path.String(), nil)
559 ctx.AddNinjaFileDeps(deps...)
560 }
561
562 if err != nil {
563 return false, fmt.Errorf("glob: %s", err.Error())
564 }
565
566 return len(files) > 0, nil
567}
568
569// PathForSource joins the provided path components and validates that the result
570// neither escapes the source dir nor is in the out dir.
571// On error, it will return a usable, but invalid SourcePath, and report a ModuleError.
572func PathForSource(ctx PathContext, pathComponents ...string) SourcePath {
573 path, err := pathForSource(ctx, pathComponents...)
574 if err != nil {
575 reportPathError(ctx, err)
576 }
577
Colin Crosse3924e12018-08-15 20:18:53 -0700578 if pathtools.IsGlob(path.String()) {
579 reportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
580 }
581
Colin Cross192e97a2018-02-22 14:21:02 -0800582 if modCtx, ok := ctx.(ModuleContext); ok && ctx.Config().AllowMissingDependencies() {
583 exists, err := existsWithDependencies(ctx, path)
584 if err != nil {
585 reportPathError(ctx, err)
586 }
587 if !exists {
588 modCtx.AddMissingDependencies([]string{path.String()})
589 }
590 } else if exists, _, err := ctx.Fs().Exists(path.String()); err != nil {
591 reportPathErrorf(ctx, "%s: %s", path, err.Error())
592 } else if !exists {
593 reportPathErrorf(ctx, "source path %s does not exist", path)
594 }
595 return path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700596}
597
Jeff Gaston734e3802017-04-10 15:47:24 -0700598// ExistentPathForSource returns an OptionalPath with the SourcePath if the
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700599// path exists, or an empty OptionalPath if it doesn't exist. Dependencies are added
600// so that the ninja file will be regenerated if the state of the path changes.
Colin Cross32f38982018-02-22 11:47:25 -0800601func ExistentPathForSource(ctx PathContext, pathComponents ...string) OptionalPath {
Colin Cross192e97a2018-02-22 14:21:02 -0800602 path, err := pathForSource(ctx, pathComponents...)
Colin Cross1ccfcc32018-02-22 13:54:26 -0800603 if err != nil {
604 reportPathError(ctx, err)
605 return OptionalPath{}
606 }
Colin Crossc48c1432018-02-23 07:09:01 +0000607
Colin Crosse3924e12018-08-15 20:18:53 -0700608 if pathtools.IsGlob(path.String()) {
609 reportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
610 return OptionalPath{}
611 }
612
Colin Cross192e97a2018-02-22 14:21:02 -0800613 exists, err := existsWithDependencies(ctx, path)
Colin Crossc48c1432018-02-23 07:09:01 +0000614 if err != nil {
615 reportPathError(ctx, err)
616 return OptionalPath{}
617 }
Colin Cross192e97a2018-02-22 14:21:02 -0800618 if !exists {
Colin Crossc48c1432018-02-23 07:09:01 +0000619 return OptionalPath{}
620 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700621 return OptionalPathForPath(path)
622}
623
624func (p SourcePath) String() string {
625 return filepath.Join(p.config.srcDir, p.path)
626}
627
628// Join creates a new SourcePath with paths... joined with the current path. The
629// provided paths... may not use '..' to escape from the current path.
630func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800631 path, err := validatePath(paths...)
632 if err != nil {
633 reportPathError(ctx, err)
634 }
Colin Cross0db55682017-12-05 15:36:55 -0800635 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700636}
637
638// OverlayPath returns the overlay for `path' if it exists. This assumes that the
639// SourcePath is the path to a resource overlay directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700640func (p SourcePath) OverlayPath(ctx ModuleContext, path Path) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700641 var relDir string
642 if moduleSrcPath, ok := path.(ModuleSrcPath); ok {
Colin Cross7fc17db2017-02-01 14:07:55 -0800643 relDir = moduleSrcPath.path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700644 } else if srcPath, ok := path.(SourcePath); ok {
645 relDir = srcPath.path
646 } else {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800647 reportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700648 return OptionalPath{}
649 }
650 dir := filepath.Join(p.config.srcDir, p.path, relDir)
651 // Use Glob so that we are run again if the directory is added.
Colin Cross7f19f372016-11-01 11:10:25 -0700652 if pathtools.IsGlob(dir) {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800653 reportPathErrorf(ctx, "Path may not contain a glob: %s", dir)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800654 }
Colin Cross461b4452018-02-23 09:22:42 -0800655 paths, err := ctx.GlobWithDeps(dir, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700656 if err != nil {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800657 reportPathErrorf(ctx, "glob: %s", err.Error())
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700658 return OptionalPath{}
659 }
660 if len(paths) == 0 {
661 return OptionalPath{}
662 }
663 relPath, err := filepath.Rel(p.config.srcDir, paths[0])
664 if err != nil {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800665 reportPathError(ctx, err)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700666 return OptionalPath{}
667 }
668 return OptionalPathForPath(PathForSource(ctx, relPath))
669}
670
671// OutputPath is a Path representing a file path rooted from the build directory
672type OutputPath struct {
673 basePath
674}
675
Colin Cross702e0f82017-10-18 17:27:54 -0700676func (p OutputPath) withRel(rel string) OutputPath {
Colin Cross0db55682017-12-05 15:36:55 -0800677 p.basePath = p.basePath.withRel(rel)
Colin Cross702e0f82017-10-18 17:27:54 -0700678 return p
679}
680
Colin Cross3063b782018-08-15 11:19:12 -0700681func (p OutputPath) WithoutRel() OutputPath {
682 p.basePath.rel = filepath.Base(p.basePath.path)
683 return p
684}
685
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700686var _ Path = OutputPath{}
687
Jeff Gaston734e3802017-04-10 15:47:24 -0700688// PathForOutput joins the provided paths and returns an OutputPath that is
689// validated to not escape the build dir.
690// On error, it will return a usable, but invalid OutputPath, and report a ModuleError.
691func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800692 path, err := validatePath(pathComponents...)
693 if err != nil {
694 reportPathError(ctx, err)
695 }
Colin Crossaabf6792017-11-29 00:27:14 -0800696 return OutputPath{basePath{path, ctx.Config(), ""}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700697}
698
699func (p OutputPath) writablePath() {}
700
701func (p OutputPath) String() string {
702 return filepath.Join(p.config.buildDir, p.path)
703}
704
Colin Crossa2344662016-03-24 13:14:12 -0700705func (p OutputPath) RelPathString() string {
706 return p.path
707}
708
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700709// Join creates a new OutputPath with paths... joined with the current path. The
710// provided paths... may not use '..' to escape from the current path.
711func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800712 path, err := validatePath(paths...)
713 if err != nil {
714 reportPathError(ctx, err)
715 }
Colin Cross0db55682017-12-05 15:36:55 -0800716 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700717}
718
719// PathForIntermediates returns an OutputPath representing the top-level
720// intermediates directory.
721func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800722 path, err := validatePath(paths...)
723 if err != nil {
724 reportPathError(ctx, err)
725 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700726 return PathForOutput(ctx, ".intermediates", path)
727}
728
Dan Willemsenbc0c5092018-03-10 16:25:53 -0800729// DistPath is a Path representing a file path rooted from the dist directory
730type DistPath struct {
731 basePath
732}
733
734func (p DistPath) withRel(rel string) DistPath {
735 p.basePath = p.basePath.withRel(rel)
736 return p
737}
738
739var _ Path = DistPath{}
740
741// PathForDist joins the provided paths and returns a DistPath that is
742// validated to not escape the dist dir.
743// On error, it will return a usable, but invalid DistPath, and report a ModuleError.
744func PathForDist(ctx PathContext, pathComponents ...string) DistPath {
745 path, err := validatePath(pathComponents...)
746 if err != nil {
747 reportPathError(ctx, err)
748 }
749 return DistPath{basePath{path, ctx.Config(), ""}}
750}
751
752func (p DistPath) writablePath() {}
753
754func (p DistPath) Valid() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800755 return p.config.productVariables.DistDir != nil && *p.config.productVariables.DistDir != ""
Dan Willemsenbc0c5092018-03-10 16:25:53 -0800756}
757
758func (p DistPath) String() string {
759 if !p.Valid() {
760 panic("Requesting an invalid path")
761 }
Dan Willemsen45133ac2018-03-09 21:22:06 -0800762 return filepath.Join(*p.config.productVariables.DistDir, p.path)
Dan Willemsenbc0c5092018-03-10 16:25:53 -0800763}
764
765func (p DistPath) RelPathString() string {
766 return p.path
767}
768
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700769// ModuleSrcPath is a Path representing a file rooted from a module's local source dir
770type ModuleSrcPath struct {
Colin Cross7fc17db2017-02-01 14:07:55 -0800771 SourcePath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700772}
773
774var _ Path = ModuleSrcPath{}
775var _ genPathProvider = ModuleSrcPath{}
776var _ objPathProvider = ModuleSrcPath{}
777var _ resPathProvider = ModuleSrcPath{}
778
779// PathForModuleSrc returns a ModuleSrcPath representing the paths... under the
780// module's local source directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700781func PathForModuleSrc(ctx ModuleContext, paths ...string) ModuleSrcPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800782 p, err := validatePath(paths...)
783 if err != nil {
784 reportPathError(ctx, err)
785 }
Colin Cross192e97a2018-02-22 14:21:02 -0800786
787 srcPath, err := pathForSource(ctx, ctx.ModuleDir(), p)
788 if err != nil {
789 reportPathError(ctx, err)
790 }
791
Colin Crosse3924e12018-08-15 20:18:53 -0700792 if pathtools.IsGlob(srcPath.String()) {
793 reportPathErrorf(ctx, "path may not contain a glob: %s", srcPath.String())
794 }
795
Colin Cross192e97a2018-02-22 14:21:02 -0800796 path := ModuleSrcPath{srcPath}
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800797 path.basePath.rel = p
Colin Cross192e97a2018-02-22 14:21:02 -0800798
799 if exists, _, err := ctx.Fs().Exists(path.String()); err != nil {
800 reportPathErrorf(ctx, "%s: %s", path, err.Error())
801 } else if !exists {
802 reportPathErrorf(ctx, "module source path %s does not exist", path)
803 }
804
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800805 return path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700806}
807
808// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
809// valid path if p is non-nil.
Colin Cross635c3b02016-05-18 15:37:25 -0700810func OptionalPathForModuleSrc(ctx ModuleContext, p *string) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700811 if p == nil {
812 return OptionalPath{}
813 }
814 return OptionalPathForPath(PathForModuleSrc(ctx, *p))
815}
816
Dan Willemsen21ec4902016-11-02 20:43:13 -0700817func (p ModuleSrcPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Colin Cross7fc17db2017-02-01 14:07:55 -0800818 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700819}
820
Colin Cross635c3b02016-05-18 15:37:25 -0700821func (p ModuleSrcPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Colin Cross7fc17db2017-02-01 14:07:55 -0800822 return PathForModuleObj(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 ModuleSrcPath) resPathWithName(ctx ModuleContext, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700826 // TODO: Use full directory if the new ctx is not the current ctx?
827 return PathForModuleRes(ctx, p.path, name)
828}
829
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800830func (p ModuleSrcPath) WithSubDir(ctx ModuleContext, subdir string) ModuleSrcPath {
831 subdir = PathForModuleSrc(ctx, subdir).String()
832 var err error
833 rel, err := filepath.Rel(subdir, p.path)
834 if err != nil {
835 ctx.ModuleErrorf("source file %q is not under path %q", p.path, subdir)
836 return p
837 }
838 p.rel = rel
839 return p
840}
841
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700842// ModuleOutPath is a Path representing a module's output directory.
843type ModuleOutPath struct {
844 OutputPath
845}
846
847var _ Path = ModuleOutPath{}
848
Colin Cross702e0f82017-10-18 17:27:54 -0700849func pathForModule(ctx ModuleContext) OutputPath {
850 return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir())
851}
852
Logan Chien7eefdc42018-07-11 18:10:41 +0800853// PathForVndkRefAbiDump returns an OptionalPath representing the path of the
854// reference abi dump for the given module. This is not guaranteed to be valid.
855func PathForVndkRefAbiDump(ctx ModuleContext, version, fileName string,
856 isLlndk, isGzip bool) OptionalPath {
857
Jayant Chowdharyac066c62018-02-20 10:53:31 -0800858 arches := ctx.DeviceConfig().Arches()
Logan Chien7eefdc42018-07-11 18:10:41 +0800859 if len(arches) == 0 {
860 panic("device build with no primary arch")
861 }
Jayant Chowdharyac066c62018-02-20 10:53:31 -0800862 currentArch := ctx.Arch()
863 archNameAndVariant := currentArch.ArchType.String()
864 if currentArch.ArchVariant != "" {
865 archNameAndVariant += "_" + currentArch.ArchVariant
866 }
Logan Chien5237bed2018-07-11 17:15:57 +0800867
868 var dirName string
869 if isLlndk {
870 dirName = "ndk"
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800871 } else {
Logan Chien5237bed2018-07-11 17:15:57 +0800872 dirName = "vndk"
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800873 }
Logan Chien5237bed2018-07-11 17:15:57 +0800874
Jayant Chowdhary34ce67d2018-03-08 11:00:50 -0800875 binderBitness := ctx.DeviceConfig().BinderBitness()
Logan Chien7eefdc42018-07-11 18:10:41 +0800876
877 var ext string
878 if isGzip {
879 ext = ".lsdump.gz"
880 } else {
881 ext = ".lsdump"
882 }
883
884 return ExistentPathForSource(ctx, "prebuilts", "abi-dumps", dirName,
885 version, binderBitness, archNameAndVariant, "source-based",
886 fileName+ext)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800887}
888
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700889// PathForModuleOut returns a Path representing the paths... under the module's
890// output directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700891func PathForModuleOut(ctx ModuleContext, paths ...string) ModuleOutPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800892 p, err := validatePath(paths...)
893 if err != nil {
894 reportPathError(ctx, err)
895 }
Colin Cross702e0f82017-10-18 17:27:54 -0700896 return ModuleOutPath{
897 OutputPath: pathForModule(ctx).withRel(p),
898 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700899}
900
901// ModuleGenPath is a Path representing the 'gen' directory in a module's output
902// directory. Mainly used for generated sources.
903type ModuleGenPath struct {
904 ModuleOutPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700905}
906
907var _ Path = ModuleGenPath{}
908var _ genPathProvider = ModuleGenPath{}
909var _ objPathProvider = ModuleGenPath{}
910
911// PathForModuleGen returns a Path representing the paths... under the module's
912// `gen' directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700913func PathForModuleGen(ctx ModuleContext, paths ...string) ModuleGenPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800914 p, err := validatePath(paths...)
915 if err != nil {
916 reportPathError(ctx, err)
917 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700918 return ModuleGenPath{
Colin Cross702e0f82017-10-18 17:27:54 -0700919 ModuleOutPath: ModuleOutPath{
920 OutputPath: pathForModule(ctx).withRel("gen").withRel(p),
921 },
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700922 }
923}
924
Dan Willemsen21ec4902016-11-02 20:43:13 -0700925func (p ModuleGenPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700926 // TODO: make a different path for local vs remote generated files?
Dan Willemsen21ec4902016-11-02 20:43:13 -0700927 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700928}
929
Colin Cross635c3b02016-05-18 15:37:25 -0700930func (p ModuleGenPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700931 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
932}
933
934// ModuleObjPath is a Path representing the 'obj' directory in a module's output
935// directory. Used for compiled objects.
936type ModuleObjPath struct {
937 ModuleOutPath
938}
939
940var _ Path = ModuleObjPath{}
941
942// PathForModuleObj returns a Path representing the paths... under the module's
943// 'obj' directory.
Jeff Gaston734e3802017-04-10 15:47:24 -0700944func PathForModuleObj(ctx ModuleContext, pathComponents ...string) ModuleObjPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800945 p, err := validatePath(pathComponents...)
946 if err != nil {
947 reportPathError(ctx, err)
948 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700949 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
950}
951
952// ModuleResPath is a a Path representing the 'res' directory in a module's
953// output directory.
954type ModuleResPath struct {
955 ModuleOutPath
956}
957
958var _ Path = ModuleResPath{}
959
960// PathForModuleRes returns a Path representing the paths... under the module's
961// 'res' directory.
Jeff Gaston734e3802017-04-10 15:47:24 -0700962func PathForModuleRes(ctx ModuleContext, pathComponents ...string) ModuleResPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800963 p, err := validatePath(pathComponents...)
964 if err != nil {
965 reportPathError(ctx, err)
966 }
967
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700968 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
969}
970
971// PathForModuleInstall returns a Path representing the install path for the
972// module appended with paths...
Dan Willemsen00269f22017-07-06 16:59:48 -0700973func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700974 var outPaths []string
975 if ctx.Device() {
Vishwath Mohan87f3b242017-06-07 12:31:57 -0700976 var partition string
Dan Willemsen00269f22017-07-06 16:59:48 -0700977 if ctx.InstallInData() {
Vishwath Mohan87f3b242017-06-07 12:31:57 -0700978 partition = "data"
Jiyong Parkf9332f12018-02-01 00:54:12 +0900979 } else if ctx.InstallInRecovery() {
Jiyong Park2e674312018-05-29 13:56:37 +0900980 // the layout of recovery partion is the same as that of system partition
981 partition = "recovery/root/system"
Jiyong Park2db76922017-11-08 16:03:48 +0900982 } else if ctx.SocSpecific() {
Dan Willemsen00269f22017-07-06 16:59:48 -0700983 partition = ctx.DeviceConfig().VendorPath()
Jiyong Park2db76922017-11-08 16:03:48 +0900984 } else if ctx.DeviceSpecific() {
985 partition = ctx.DeviceConfig().OdmPath()
986 } else if ctx.ProductSpecific() {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900987 partition = ctx.DeviceConfig().ProductPath()
Dario Frenifd05a742018-05-29 13:28:54 +0100988 } else if ctx.ProductServicesSpecific() {
989 partition = ctx.DeviceConfig().ProductServicesPath()
Vishwath Mohan87f3b242017-06-07 12:31:57 -0700990 } else {
991 partition = "system"
Dan Willemsen782a2d12015-12-21 14:55:28 -0800992 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700993
994 if ctx.InstallInSanitizerDir() {
995 partition = "data/asan/" + partition
Dan Willemsen782a2d12015-12-21 14:55:28 -0800996 }
Colin Cross6510f912017-11-29 00:27:14 -0800997 outPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700998 } else {
Dan Willemsen866b5632017-09-22 12:28:24 -0700999 switch ctx.Os() {
1000 case Linux:
1001 outPaths = []string{"host", "linux-x86"}
1002 case LinuxBionic:
1003 // TODO: should this be a separate top level, or shared with linux-x86?
1004 outPaths = []string{"host", "linux_bionic-x86"}
1005 default:
1006 outPaths = []string{"host", ctx.Os().String() + "-x86"}
1007 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001008 }
Dan Willemsen782a2d12015-12-21 14:55:28 -08001009 if ctx.Debug() {
1010 outPaths = append([]string{"debug"}, outPaths...)
1011 }
Jeff Gaston734e3802017-04-10 15:47:24 -07001012 outPaths = append(outPaths, pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001013 return PathForOutput(ctx, outPaths...)
1014}
1015
1016// validateSafePath validates a path that we trust (may contain ninja variables).
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001017// Ensures that each path component does not attempt to leave its component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001018func validateSafePath(pathComponents ...string) (string, error) {
Jeff Gaston734e3802017-04-10 15:47:24 -07001019 for _, path := range pathComponents {
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001020 path := filepath.Clean(path)
1021 if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001022 return "", fmt.Errorf("Path is outside directory: %s", path)
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001023 }
1024 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001025 // TODO: filepath.Join isn't necessarily correct with embedded ninja
1026 // variables. '..' may remove the entire ninja variable, even if it
1027 // will be expanded to multiple nested directories.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001028 return filepath.Join(pathComponents...), nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001029}
1030
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001031// validatePath validates that a path does not include ninja variables, and that
1032// each path component does not attempt to leave its component. Returns a joined
1033// version of each path component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001034func validatePath(pathComponents ...string) (string, error) {
Jeff Gaston734e3802017-04-10 15:47:24 -07001035 for _, path := range pathComponents {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001036 if strings.Contains(path, "$") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001037 return "", fmt.Errorf("Path contains invalid character($): %s", path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001038 }
1039 }
Colin Cross1ccfcc32018-02-22 13:54:26 -08001040 return validateSafePath(pathComponents...)
Colin Cross6e18ca42015-07-14 18:55:36 -07001041}
Colin Cross5b529592017-05-09 13:34:34 -07001042
Colin Cross0875c522017-11-28 17:34:01 -08001043func PathForPhony(ctx PathContext, phony string) WritablePath {
1044 if strings.ContainsAny(phony, "$/") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001045 reportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony)
Colin Cross0875c522017-11-28 17:34:01 -08001046 }
Colin Cross74e3fe42017-12-11 15:51:44 -08001047 return PhonyPath{basePath{phony, ctx.Config(), ""}}
Colin Cross0875c522017-11-28 17:34:01 -08001048}
1049
Colin Cross74e3fe42017-12-11 15:51:44 -08001050type PhonyPath struct {
1051 basePath
1052}
1053
1054func (p PhonyPath) writablePath() {}
1055
1056var _ Path = PhonyPath{}
1057var _ WritablePath = PhonyPath{}
1058
Colin Cross5b529592017-05-09 13:34:34 -07001059type testPath struct {
1060 basePath
1061}
1062
1063func (p testPath) String() string {
1064 return p.path
1065}
1066
1067func PathForTesting(paths ...string) Path {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001068 p, err := validateSafePath(paths...)
1069 if err != nil {
1070 panic(err)
1071 }
Colin Cross5b529592017-05-09 13:34:34 -07001072 return testPath{basePath{path: p, rel: p}}
1073}
1074
1075func PathsForTesting(strs []string) Paths {
1076 p := make(Paths, len(strs))
1077 for i, s := range strs {
1078 p[i] = PathForTesting(s)
1079 }
1080
1081 return p
1082}