blob: 91abeba5f205ce0eed751b3e8c00ad66a1edbf78 [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.
Colin Crossfe4bc362018-09-12 10:02:13 -0700233// It intended for use in globs that only list files that exist, so it allows '$' in
234// filenames.
Dan Willemsen540a78c2018-02-26 21:50:08 -0800235func pathsForModuleSrcFromFullPath(ctx ModuleContext, paths []string, incDirs bool) Paths {
Colin Cross6510f912017-11-29 00:27:14 -0800236 prefix := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir()) + "/"
Colin Cross0f37af02017-09-27 17:42:05 -0700237 if prefix == "./" {
238 prefix = ""
239 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700240 ret := make(Paths, 0, len(paths))
241 for _, p := range paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -0800242 if !incDirs && strings.HasSuffix(p, "/") {
243 continue
244 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700245 path := filepath.Clean(p)
246 if !strings.HasPrefix(path, prefix) {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800247 reportPathErrorf(ctx, "Path '%s' is not in module source directory '%s'", p, prefix)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700248 continue
249 }
Colin Crosse3924e12018-08-15 20:18:53 -0700250
Colin Crossfe4bc362018-09-12 10:02:13 -0700251 srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):])
Colin Crosse3924e12018-08-15 20:18:53 -0700252 if err != nil {
253 reportPathError(ctx, err)
254 continue
255 }
256
257 moduleSrcPath := ModuleSrcPath{srcPath}
258 moduleSrcPath.basePath.rel = srcPath.path
259
260 ret = append(ret, moduleSrcPath)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700261 }
262 return ret
263}
264
265// PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's
266// local source directory. If none are provided, use the default if it exists.
Colin Cross635c3b02016-05-18 15:37:25 -0700267func PathsWithOptionalDefaultForModuleSrc(ctx ModuleContext, input []string, def string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700268 if len(input) > 0 {
269 return PathsForModuleSrc(ctx, input)
270 }
271 // Use Glob so that if the default doesn't exist, a dependency is added so that when it
272 // is created, we're run again.
Colin Cross6510f912017-11-29 00:27:14 -0800273 path := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir(), def)
Colin Cross461b4452018-02-23 09:22:42 -0800274 return ctx.Glob(path, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700275}
276
277// Strings returns the Paths in string form
278func (p Paths) Strings() []string {
279 if p == nil {
280 return nil
281 }
282 ret := make([]string, len(p))
283 for i, path := range p {
284 ret[i] = path.String()
285 }
286 return ret
287}
288
Colin Crossb6715442017-10-24 11:13:31 -0700289// FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It
290// modifies the Paths slice contents in place, and returns a subslice of the original slice.
Dan Willemsenfe92c962017-08-29 12:28:37 -0700291func FirstUniquePaths(list Paths) Paths {
292 k := 0
293outer:
294 for i := 0; i < len(list); i++ {
295 for j := 0; j < k; j++ {
296 if list[i] == list[j] {
297 continue outer
298 }
299 }
300 list[k] = list[i]
301 k++
302 }
303 return list[:k]
304}
305
Colin Crossb6715442017-10-24 11:13:31 -0700306// LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It
307// modifies the Paths slice contents in place, and returns a subslice of the original slice.
308func LastUniquePaths(list Paths) Paths {
309 totalSkip := 0
310 for i := len(list) - 1; i >= totalSkip; i-- {
311 skip := 0
312 for j := i - 1; j >= totalSkip; j-- {
313 if list[i] == list[j] {
314 skip++
315 } else {
316 list[j+skip] = list[j]
317 }
318 }
319 totalSkip += skip
320 }
321 return list[totalSkip:]
322}
323
Colin Crossa140bb02018-04-17 10:52:26 -0700324// ReversePaths returns a copy of a Paths in reverse order.
325func ReversePaths(list Paths) Paths {
326 if list == nil {
327 return nil
328 }
329 ret := make(Paths, len(list))
330 for i := range list {
331 ret[i] = list[len(list)-1-i]
332 }
333 return ret
334}
335
Jeff Gaston294356f2017-09-27 17:05:30 -0700336func indexPathList(s Path, list []Path) int {
337 for i, l := range list {
338 if l == s {
339 return i
340 }
341 }
342
343 return -1
344}
345
346func inPathList(p Path, list []Path) bool {
347 return indexPathList(p, list) != -1
348}
349
350func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) {
351 for _, l := range list {
352 if inPathList(l, filter) {
353 filtered = append(filtered, l)
354 } else {
355 remainder = append(remainder, l)
356 }
357 }
358
359 return
360}
361
Colin Cross93e85952017-08-15 13:34:18 -0700362// HasExt returns true of any of the paths have extension ext, otherwise false
363func (p Paths) HasExt(ext string) bool {
364 for _, path := range p {
365 if path.Ext() == ext {
366 return true
367 }
368 }
369
370 return false
371}
372
373// FilterByExt returns the subset of the paths that have extension ext
374func (p Paths) FilterByExt(ext string) Paths {
375 ret := make(Paths, 0, len(p))
376 for _, path := range p {
377 if path.Ext() == ext {
378 ret = append(ret, path)
379 }
380 }
381 return ret
382}
383
384// FilterOutByExt returns the subset of the paths that do not have extension ext
385func (p Paths) FilterOutByExt(ext string) Paths {
386 ret := make(Paths, 0, len(p))
387 for _, path := range p {
388 if path.Ext() != ext {
389 ret = append(ret, path)
390 }
391 }
392 return ret
393}
394
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700395// DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory
396// (including subdirectories) are in a contiguous subslice of the list, and can be found in
397// O(log(N)) time using a binary search on the directory prefix.
398type DirectorySortedPaths Paths
399
400func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths {
401 ret := append(DirectorySortedPaths(nil), paths...)
402 sort.Slice(ret, func(i, j int) bool {
403 return ret[i].String() < ret[j].String()
404 })
405 return ret
406}
407
408// PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries
409// that are in the specified directory and its subdirectories.
410func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths {
411 prefix := filepath.Clean(dir) + "/"
412 start := sort.Search(len(p), func(i int) bool {
413 return prefix < p[i].String()
414 })
415
416 ret := p[start:]
417
418 end := sort.Search(len(ret), func(i int) bool {
419 return !strings.HasPrefix(ret[i].String(), prefix)
420 })
421
422 ret = ret[:end]
423
424 return Paths(ret)
425}
426
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700427// WritablePaths is a slice of WritablePaths, used for multiple outputs.
428type WritablePaths []WritablePath
429
430// Strings returns the string forms of the writable paths.
431func (p WritablePaths) Strings() []string {
432 if p == nil {
433 return nil
434 }
435 ret := make([]string, len(p))
436 for i, path := range p {
437 ret[i] = path.String()
438 }
439 return ret
440}
441
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800442// Paths returns the WritablePaths as a Paths
443func (p WritablePaths) Paths() Paths {
444 if p == nil {
445 return nil
446 }
447 ret := make(Paths, len(p))
448 for i, path := range p {
449 ret[i] = path
450 }
451 return ret
452}
453
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700454type basePath struct {
455 path string
456 config Config
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800457 rel string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700458}
459
460func (p basePath) Ext() string {
461 return filepath.Ext(p.path)
462}
463
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700464func (p basePath) Base() string {
465 return filepath.Base(p.path)
466}
467
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800468func (p basePath) Rel() string {
469 if p.rel != "" {
470 return p.rel
471 }
472 return p.path
473}
474
Colin Cross0875c522017-11-28 17:34:01 -0800475func (p basePath) String() string {
476 return p.path
477}
478
Colin Cross0db55682017-12-05 15:36:55 -0800479func (p basePath) withRel(rel string) basePath {
480 p.path = filepath.Join(p.path, rel)
481 p.rel = rel
482 return p
483}
484
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700485// SourcePath is a Path representing a file path rooted from SrcDir
486type SourcePath struct {
487 basePath
488}
489
490var _ Path = SourcePath{}
491
Colin Cross0db55682017-12-05 15:36:55 -0800492func (p SourcePath) withRel(rel string) SourcePath {
493 p.basePath = p.basePath.withRel(rel)
494 return p
495}
496
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700497// safePathForSource is for paths that we expect are safe -- only for use by go
498// code that is embedding ninja variables in paths
Colin Crossfe4bc362018-09-12 10:02:13 -0700499func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
500 p, err := validateSafePath(pathComponents...)
Colin Crossaabf6792017-11-29 00:27:14 -0800501 ret := SourcePath{basePath{p, ctx.Config(), ""}}
Colin Crossfe4bc362018-09-12 10:02:13 -0700502 if err != nil {
503 return ret, err
504 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700505
506 abs, err := filepath.Abs(ret.String())
Colin Cross6e18ca42015-07-14 18:55:36 -0700507 if err != nil {
Colin Crossfe4bc362018-09-12 10:02:13 -0700508 return ret, err
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700509 }
Colin Crossaabf6792017-11-29 00:27:14 -0800510 buildroot, err := filepath.Abs(ctx.Config().buildDir)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700511 if err != nil {
Colin Crossfe4bc362018-09-12 10:02:13 -0700512 return ret, err
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700513 }
514 if strings.HasPrefix(abs, buildroot) {
Colin Crossfe4bc362018-09-12 10:02:13 -0700515 return ret, fmt.Errorf("source path %s is in output", abs)
Colin Cross6e18ca42015-07-14 18:55:36 -0700516 }
517
Colin Crossfe4bc362018-09-12 10:02:13 -0700518 return ret, err
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700519}
520
Colin Cross192e97a2018-02-22 14:21:02 -0800521// pathForSource creates a SourcePath from pathComponents, but does not check that it exists.
522func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
Colin Crossc48c1432018-02-23 07:09:01 +0000523 p, err := validatePath(pathComponents...)
524 ret := SourcePath{basePath{p, ctx.Config(), ""}}
Colin Cross94a32102018-02-22 14:21:02 -0800525 if err != nil {
Colin Cross192e97a2018-02-22 14:21:02 -0800526 return ret, err
Colin Cross94a32102018-02-22 14:21:02 -0800527 }
528
Colin Crossc48c1432018-02-23 07:09:01 +0000529 abs, err := filepath.Abs(ret.String())
530 if err != nil {
Colin Cross192e97a2018-02-22 14:21:02 -0800531 return ret, err
Colin Cross94a32102018-02-22 14:21:02 -0800532 }
Colin Crossc48c1432018-02-23 07:09:01 +0000533 buildroot, err := filepath.Abs(ctx.Config().buildDir)
534 if err != nil {
Colin Cross192e97a2018-02-22 14:21:02 -0800535 return ret, err
Colin Crossc48c1432018-02-23 07:09:01 +0000536 }
537 if strings.HasPrefix(abs, buildroot) {
Colin Cross192e97a2018-02-22 14:21:02 -0800538 return ret, fmt.Errorf("source path %s is in output", abs)
Colin Crossc48c1432018-02-23 07:09:01 +0000539 }
540
Colin Cross192e97a2018-02-22 14:21:02 -0800541 return ret, nil
542}
543
544// existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the
545// path does not exist.
546func existsWithDependencies(ctx PathContext, path SourcePath) (exists bool, err error) {
547 var files []string
548
549 if gctx, ok := ctx.(PathGlobContext); ok {
550 // Use glob to produce proper dependencies, even though we only want
551 // a single file.
552 files, err = gctx.GlobWithDeps(path.String(), nil)
553 } else {
554 var deps []string
555 // We cannot add build statements in this context, so we fall back to
556 // AddNinjaFileDeps
557 files, deps, err = pathtools.Glob(path.String(), nil)
558 ctx.AddNinjaFileDeps(deps...)
559 }
560
561 if err != nil {
562 return false, fmt.Errorf("glob: %s", err.Error())
563 }
564
565 return len(files) > 0, nil
566}
567
568// PathForSource joins the provided path components and validates that the result
569// neither escapes the source dir nor is in the out dir.
570// On error, it will return a usable, but invalid SourcePath, and report a ModuleError.
571func PathForSource(ctx PathContext, pathComponents ...string) SourcePath {
572 path, err := pathForSource(ctx, pathComponents...)
573 if err != nil {
574 reportPathError(ctx, err)
575 }
576
Colin Crosse3924e12018-08-15 20:18:53 -0700577 if pathtools.IsGlob(path.String()) {
578 reportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
579 }
580
Colin Cross192e97a2018-02-22 14:21:02 -0800581 if modCtx, ok := ctx.(ModuleContext); ok && ctx.Config().AllowMissingDependencies() {
582 exists, err := existsWithDependencies(ctx, path)
583 if err != nil {
584 reportPathError(ctx, err)
585 }
586 if !exists {
587 modCtx.AddMissingDependencies([]string{path.String()})
588 }
589 } else if exists, _, err := ctx.Fs().Exists(path.String()); err != nil {
590 reportPathErrorf(ctx, "%s: %s", path, err.Error())
591 } else if !exists {
592 reportPathErrorf(ctx, "source path %s does not exist", path)
593 }
594 return path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700595}
596
Jeff Gaston734e3802017-04-10 15:47:24 -0700597// ExistentPathForSource returns an OptionalPath with the SourcePath if the
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700598// path exists, or an empty OptionalPath if it doesn't exist. Dependencies are added
599// so that the ninja file will be regenerated if the state of the path changes.
Colin Cross32f38982018-02-22 11:47:25 -0800600func ExistentPathForSource(ctx PathContext, pathComponents ...string) OptionalPath {
Colin Cross192e97a2018-02-22 14:21:02 -0800601 path, err := pathForSource(ctx, pathComponents...)
Colin Cross1ccfcc32018-02-22 13:54:26 -0800602 if err != nil {
603 reportPathError(ctx, err)
604 return OptionalPath{}
605 }
Colin Crossc48c1432018-02-23 07:09:01 +0000606
Colin Crosse3924e12018-08-15 20:18:53 -0700607 if pathtools.IsGlob(path.String()) {
608 reportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
609 return OptionalPath{}
610 }
611
Colin Cross192e97a2018-02-22 14:21:02 -0800612 exists, err := existsWithDependencies(ctx, path)
Colin Crossc48c1432018-02-23 07:09:01 +0000613 if err != nil {
614 reportPathError(ctx, err)
615 return OptionalPath{}
616 }
Colin Cross192e97a2018-02-22 14:21:02 -0800617 if !exists {
Colin Crossc48c1432018-02-23 07:09:01 +0000618 return OptionalPath{}
619 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700620 return OptionalPathForPath(path)
621}
622
623func (p SourcePath) String() string {
624 return filepath.Join(p.config.srcDir, p.path)
625}
626
627// Join creates a new SourcePath with paths... joined with the current path. The
628// provided paths... may not use '..' to escape from the current path.
629func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800630 path, err := validatePath(paths...)
631 if err != nil {
632 reportPathError(ctx, err)
633 }
Colin Cross0db55682017-12-05 15:36:55 -0800634 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700635}
636
637// OverlayPath returns the overlay for `path' if it exists. This assumes that the
638// SourcePath is the path to a resource overlay directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700639func (p SourcePath) OverlayPath(ctx ModuleContext, path Path) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700640 var relDir string
641 if moduleSrcPath, ok := path.(ModuleSrcPath); ok {
Colin Cross7fc17db2017-02-01 14:07:55 -0800642 relDir = moduleSrcPath.path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700643 } else if srcPath, ok := path.(SourcePath); ok {
644 relDir = srcPath.path
645 } else {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800646 reportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700647 return OptionalPath{}
648 }
649 dir := filepath.Join(p.config.srcDir, p.path, relDir)
650 // Use Glob so that we are run again if the directory is added.
Colin Cross7f19f372016-11-01 11:10:25 -0700651 if pathtools.IsGlob(dir) {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800652 reportPathErrorf(ctx, "Path may not contain a glob: %s", dir)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800653 }
Colin Cross461b4452018-02-23 09:22:42 -0800654 paths, err := ctx.GlobWithDeps(dir, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700655 if err != nil {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800656 reportPathErrorf(ctx, "glob: %s", err.Error())
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700657 return OptionalPath{}
658 }
659 if len(paths) == 0 {
660 return OptionalPath{}
661 }
662 relPath, err := filepath.Rel(p.config.srcDir, paths[0])
663 if err != nil {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800664 reportPathError(ctx, err)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700665 return OptionalPath{}
666 }
667 return OptionalPathForPath(PathForSource(ctx, relPath))
668}
669
670// OutputPath is a Path representing a file path rooted from the build directory
671type OutputPath struct {
672 basePath
673}
674
Colin Cross702e0f82017-10-18 17:27:54 -0700675func (p OutputPath) withRel(rel string) OutputPath {
Colin Cross0db55682017-12-05 15:36:55 -0800676 p.basePath = p.basePath.withRel(rel)
Colin Cross702e0f82017-10-18 17:27:54 -0700677 return p
678}
679
Colin Cross3063b782018-08-15 11:19:12 -0700680func (p OutputPath) WithoutRel() OutputPath {
681 p.basePath.rel = filepath.Base(p.basePath.path)
682 return p
683}
684
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700685var _ Path = OutputPath{}
686
Jeff Gaston734e3802017-04-10 15:47:24 -0700687// PathForOutput joins the provided paths and returns an OutputPath that is
688// validated to not escape the build dir.
689// On error, it will return a usable, but invalid OutputPath, and report a ModuleError.
690func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800691 path, err := validatePath(pathComponents...)
692 if err != nil {
693 reportPathError(ctx, err)
694 }
Colin Crossaabf6792017-11-29 00:27:14 -0800695 return OutputPath{basePath{path, ctx.Config(), ""}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700696}
697
698func (p OutputPath) writablePath() {}
699
700func (p OutputPath) String() string {
701 return filepath.Join(p.config.buildDir, p.path)
702}
703
Colin Crossa2344662016-03-24 13:14:12 -0700704func (p OutputPath) RelPathString() string {
705 return p.path
706}
707
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700708// Join creates a new OutputPath with paths... joined with the current path. The
709// provided paths... may not use '..' to escape from the current path.
710func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800711 path, err := validatePath(paths...)
712 if err != nil {
713 reportPathError(ctx, err)
714 }
Colin Cross0db55682017-12-05 15:36:55 -0800715 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700716}
717
718// PathForIntermediates returns an OutputPath representing the top-level
719// intermediates directory.
720func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800721 path, err := validatePath(paths...)
722 if err != nil {
723 reportPathError(ctx, err)
724 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700725 return PathForOutput(ctx, ".intermediates", path)
726}
727
Dan Willemsenbc0c5092018-03-10 16:25:53 -0800728// DistPath is a Path representing a file path rooted from the dist directory
729type DistPath struct {
730 basePath
731}
732
733func (p DistPath) withRel(rel string) DistPath {
734 p.basePath = p.basePath.withRel(rel)
735 return p
736}
737
738var _ Path = DistPath{}
739
740// PathForDist joins the provided paths and returns a DistPath that is
741// validated to not escape the dist dir.
742// On error, it will return a usable, but invalid DistPath, and report a ModuleError.
743func PathForDist(ctx PathContext, pathComponents ...string) DistPath {
744 path, err := validatePath(pathComponents...)
745 if err != nil {
746 reportPathError(ctx, err)
747 }
748 return DistPath{basePath{path, ctx.Config(), ""}}
749}
750
751func (p DistPath) writablePath() {}
752
753func (p DistPath) Valid() bool {
Dan Willemsen45133ac2018-03-09 21:22:06 -0800754 return p.config.productVariables.DistDir != nil && *p.config.productVariables.DistDir != ""
Dan Willemsenbc0c5092018-03-10 16:25:53 -0800755}
756
757func (p DistPath) String() string {
758 if !p.Valid() {
759 panic("Requesting an invalid path")
760 }
Dan Willemsen45133ac2018-03-09 21:22:06 -0800761 return filepath.Join(*p.config.productVariables.DistDir, p.path)
Dan Willemsenbc0c5092018-03-10 16:25:53 -0800762}
763
764func (p DistPath) RelPathString() string {
765 return p.path
766}
767
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700768// ModuleSrcPath is a Path representing a file rooted from a module's local source dir
769type ModuleSrcPath struct {
Colin Cross7fc17db2017-02-01 14:07:55 -0800770 SourcePath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700771}
772
773var _ Path = ModuleSrcPath{}
774var _ genPathProvider = ModuleSrcPath{}
775var _ objPathProvider = ModuleSrcPath{}
776var _ resPathProvider = ModuleSrcPath{}
777
778// PathForModuleSrc returns a ModuleSrcPath representing the paths... under the
779// module's local source directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700780func PathForModuleSrc(ctx ModuleContext, paths ...string) ModuleSrcPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800781 p, err := validatePath(paths...)
782 if err != nil {
783 reportPathError(ctx, err)
784 }
Colin Cross192e97a2018-02-22 14:21:02 -0800785
786 srcPath, err := pathForSource(ctx, ctx.ModuleDir(), p)
787 if err != nil {
788 reportPathError(ctx, err)
789 }
790
Colin Crosse3924e12018-08-15 20:18:53 -0700791 if pathtools.IsGlob(srcPath.String()) {
792 reportPathErrorf(ctx, "path may not contain a glob: %s", srcPath.String())
793 }
794
Colin Cross192e97a2018-02-22 14:21:02 -0800795 path := ModuleSrcPath{srcPath}
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800796 path.basePath.rel = p
Colin Cross192e97a2018-02-22 14:21:02 -0800797
798 if exists, _, err := ctx.Fs().Exists(path.String()); err != nil {
799 reportPathErrorf(ctx, "%s: %s", path, err.Error())
800 } else if !exists {
801 reportPathErrorf(ctx, "module source path %s does not exist", path)
802 }
803
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800804 return path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700805}
806
807// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
808// valid path if p is non-nil.
Colin Cross635c3b02016-05-18 15:37:25 -0700809func OptionalPathForModuleSrc(ctx ModuleContext, p *string) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700810 if p == nil {
811 return OptionalPath{}
812 }
813 return OptionalPathForPath(PathForModuleSrc(ctx, *p))
814}
815
Dan Willemsen21ec4902016-11-02 20:43:13 -0700816func (p ModuleSrcPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Colin Cross7fc17db2017-02-01 14:07:55 -0800817 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700818}
819
Colin Cross635c3b02016-05-18 15:37:25 -0700820func (p ModuleSrcPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Colin Cross7fc17db2017-02-01 14:07:55 -0800821 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700822}
823
Colin Cross635c3b02016-05-18 15:37:25 -0700824func (p ModuleSrcPath) resPathWithName(ctx ModuleContext, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700825 // TODO: Use full directory if the new ctx is not the current ctx?
826 return PathForModuleRes(ctx, p.path, name)
827}
828
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800829func (p ModuleSrcPath) WithSubDir(ctx ModuleContext, subdir string) ModuleSrcPath {
830 subdir = PathForModuleSrc(ctx, subdir).String()
831 var err error
832 rel, err := filepath.Rel(subdir, p.path)
833 if err != nil {
834 ctx.ModuleErrorf("source file %q is not under path %q", p.path, subdir)
835 return p
836 }
837 p.rel = rel
838 return p
839}
840
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700841// ModuleOutPath is a Path representing a module's output directory.
842type ModuleOutPath struct {
843 OutputPath
844}
845
846var _ Path = ModuleOutPath{}
847
Colin Cross702e0f82017-10-18 17:27:54 -0700848func pathForModule(ctx ModuleContext) OutputPath {
849 return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir())
850}
851
Logan Chien7eefdc42018-07-11 18:10:41 +0800852// PathForVndkRefAbiDump returns an OptionalPath representing the path of the
853// reference abi dump for the given module. This is not guaranteed to be valid.
854func PathForVndkRefAbiDump(ctx ModuleContext, version, fileName string,
855 isLlndk, isGzip bool) OptionalPath {
856
Jayant Chowdharyac066c62018-02-20 10:53:31 -0800857 arches := ctx.DeviceConfig().Arches()
Logan Chien7eefdc42018-07-11 18:10:41 +0800858 if len(arches) == 0 {
859 panic("device build with no primary arch")
860 }
Jayant Chowdharyac066c62018-02-20 10:53:31 -0800861 currentArch := ctx.Arch()
862 archNameAndVariant := currentArch.ArchType.String()
863 if currentArch.ArchVariant != "" {
864 archNameAndVariant += "_" + currentArch.ArchVariant
865 }
Logan Chien5237bed2018-07-11 17:15:57 +0800866
867 var dirName string
868 if isLlndk {
869 dirName = "ndk"
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800870 } else {
Logan Chien5237bed2018-07-11 17:15:57 +0800871 dirName = "vndk"
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800872 }
Logan Chien5237bed2018-07-11 17:15:57 +0800873
Jayant Chowdhary34ce67d2018-03-08 11:00:50 -0800874 binderBitness := ctx.DeviceConfig().BinderBitness()
Logan Chien7eefdc42018-07-11 18:10:41 +0800875
876 var ext string
877 if isGzip {
878 ext = ".lsdump.gz"
879 } else {
880 ext = ".lsdump"
881 }
882
883 return ExistentPathForSource(ctx, "prebuilts", "abi-dumps", dirName,
884 version, binderBitness, archNameAndVariant, "source-based",
885 fileName+ext)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800886}
887
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700888// PathForModuleOut returns a Path representing the paths... under the module's
889// output directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700890func PathForModuleOut(ctx ModuleContext, paths ...string) ModuleOutPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800891 p, err := validatePath(paths...)
892 if err != nil {
893 reportPathError(ctx, err)
894 }
Colin Cross702e0f82017-10-18 17:27:54 -0700895 return ModuleOutPath{
896 OutputPath: pathForModule(ctx).withRel(p),
897 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700898}
899
900// ModuleGenPath is a Path representing the 'gen' directory in a module's output
901// directory. Mainly used for generated sources.
902type ModuleGenPath struct {
903 ModuleOutPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700904}
905
906var _ Path = ModuleGenPath{}
907var _ genPathProvider = ModuleGenPath{}
908var _ objPathProvider = ModuleGenPath{}
909
910// PathForModuleGen returns a Path representing the paths... under the module's
911// `gen' directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700912func PathForModuleGen(ctx ModuleContext, paths ...string) ModuleGenPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800913 p, err := validatePath(paths...)
914 if err != nil {
915 reportPathError(ctx, err)
916 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700917 return ModuleGenPath{
Colin Cross702e0f82017-10-18 17:27:54 -0700918 ModuleOutPath: ModuleOutPath{
919 OutputPath: pathForModule(ctx).withRel("gen").withRel(p),
920 },
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700921 }
922}
923
Dan Willemsen21ec4902016-11-02 20:43:13 -0700924func (p ModuleGenPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700925 // TODO: make a different path for local vs remote generated files?
Dan Willemsen21ec4902016-11-02 20:43:13 -0700926 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700927}
928
Colin Cross635c3b02016-05-18 15:37:25 -0700929func (p ModuleGenPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700930 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
931}
932
933// ModuleObjPath is a Path representing the 'obj' directory in a module's output
934// directory. Used for compiled objects.
935type ModuleObjPath struct {
936 ModuleOutPath
937}
938
939var _ Path = ModuleObjPath{}
940
941// PathForModuleObj returns a Path representing the paths... under the module's
942// 'obj' directory.
Jeff Gaston734e3802017-04-10 15:47:24 -0700943func PathForModuleObj(ctx ModuleContext, pathComponents ...string) ModuleObjPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800944 p, err := validatePath(pathComponents...)
945 if err != nil {
946 reportPathError(ctx, err)
947 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700948 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
949}
950
951// ModuleResPath is a a Path representing the 'res' directory in a module's
952// output directory.
953type ModuleResPath struct {
954 ModuleOutPath
955}
956
957var _ Path = ModuleResPath{}
958
959// PathForModuleRes returns a Path representing the paths... under the module's
960// 'res' directory.
Jeff Gaston734e3802017-04-10 15:47:24 -0700961func PathForModuleRes(ctx ModuleContext, pathComponents ...string) ModuleResPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800962 p, err := validatePath(pathComponents...)
963 if err != nil {
964 reportPathError(ctx, err)
965 }
966
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700967 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
968}
969
970// PathForModuleInstall returns a Path representing the install path for the
971// module appended with paths...
Dan Willemsen00269f22017-07-06 16:59:48 -0700972func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700973 var outPaths []string
974 if ctx.Device() {
Vishwath Mohan87f3b242017-06-07 12:31:57 -0700975 var partition string
Dan Willemsen00269f22017-07-06 16:59:48 -0700976 if ctx.InstallInData() {
Vishwath Mohan87f3b242017-06-07 12:31:57 -0700977 partition = "data"
Jiyong Parkf9332f12018-02-01 00:54:12 +0900978 } else if ctx.InstallInRecovery() {
Jiyong Park2e674312018-05-29 13:56:37 +0900979 // the layout of recovery partion is the same as that of system partition
980 partition = "recovery/root/system"
Jiyong Park2db76922017-11-08 16:03:48 +0900981 } else if ctx.SocSpecific() {
Dan Willemsen00269f22017-07-06 16:59:48 -0700982 partition = ctx.DeviceConfig().VendorPath()
Jiyong Park2db76922017-11-08 16:03:48 +0900983 } else if ctx.DeviceSpecific() {
984 partition = ctx.DeviceConfig().OdmPath()
985 } else if ctx.ProductSpecific() {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900986 partition = ctx.DeviceConfig().ProductPath()
Dario Frenifd05a742018-05-29 13:28:54 +0100987 } else if ctx.ProductServicesSpecific() {
988 partition = ctx.DeviceConfig().ProductServicesPath()
Vishwath Mohan87f3b242017-06-07 12:31:57 -0700989 } else {
990 partition = "system"
Dan Willemsen782a2d12015-12-21 14:55:28 -0800991 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700992
993 if ctx.InstallInSanitizerDir() {
994 partition = "data/asan/" + partition
Dan Willemsen782a2d12015-12-21 14:55:28 -0800995 }
Colin Cross6510f912017-11-29 00:27:14 -0800996 outPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700997 } else {
Dan Willemsen866b5632017-09-22 12:28:24 -0700998 switch ctx.Os() {
999 case Linux:
1000 outPaths = []string{"host", "linux-x86"}
1001 case LinuxBionic:
1002 // TODO: should this be a separate top level, or shared with linux-x86?
1003 outPaths = []string{"host", "linux_bionic-x86"}
1004 default:
1005 outPaths = []string{"host", ctx.Os().String() + "-x86"}
1006 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001007 }
Dan Willemsen782a2d12015-12-21 14:55:28 -08001008 if ctx.Debug() {
1009 outPaths = append([]string{"debug"}, outPaths...)
1010 }
Jeff Gaston734e3802017-04-10 15:47:24 -07001011 outPaths = append(outPaths, pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001012 return PathForOutput(ctx, outPaths...)
1013}
1014
1015// validateSafePath validates a path that we trust (may contain ninja variables).
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001016// Ensures that each path component does not attempt to leave its component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001017func validateSafePath(pathComponents ...string) (string, error) {
Jeff Gaston734e3802017-04-10 15:47:24 -07001018 for _, path := range pathComponents {
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001019 path := filepath.Clean(path)
1020 if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001021 return "", fmt.Errorf("Path is outside directory: %s", path)
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001022 }
1023 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001024 // TODO: filepath.Join isn't necessarily correct with embedded ninja
1025 // variables. '..' may remove the entire ninja variable, even if it
1026 // will be expanded to multiple nested directories.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001027 return filepath.Join(pathComponents...), nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001028}
1029
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001030// validatePath validates that a path does not include ninja variables, and that
1031// each path component does not attempt to leave its component. Returns a joined
1032// version of each path component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001033func validatePath(pathComponents ...string) (string, error) {
Jeff Gaston734e3802017-04-10 15:47:24 -07001034 for _, path := range pathComponents {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001035 if strings.Contains(path, "$") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001036 return "", fmt.Errorf("Path contains invalid character($): %s", path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001037 }
1038 }
Colin Cross1ccfcc32018-02-22 13:54:26 -08001039 return validateSafePath(pathComponents...)
Colin Cross6e18ca42015-07-14 18:55:36 -07001040}
Colin Cross5b529592017-05-09 13:34:34 -07001041
Colin Cross0875c522017-11-28 17:34:01 -08001042func PathForPhony(ctx PathContext, phony string) WritablePath {
1043 if strings.ContainsAny(phony, "$/") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001044 reportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony)
Colin Cross0875c522017-11-28 17:34:01 -08001045 }
Colin Cross74e3fe42017-12-11 15:51:44 -08001046 return PhonyPath{basePath{phony, ctx.Config(), ""}}
Colin Cross0875c522017-11-28 17:34:01 -08001047}
1048
Colin Cross74e3fe42017-12-11 15:51:44 -08001049type PhonyPath struct {
1050 basePath
1051}
1052
1053func (p PhonyPath) writablePath() {}
1054
1055var _ Path = PhonyPath{}
1056var _ WritablePath = PhonyPath{}
1057
Colin Cross5b529592017-05-09 13:34:34 -07001058type testPath struct {
1059 basePath
1060}
1061
1062func (p testPath) String() string {
1063 return p.path
1064}
1065
1066func PathForTesting(paths ...string) Path {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001067 p, err := validateSafePath(paths...)
1068 if err != nil {
1069 panic(err)
1070 }
Colin Cross5b529592017-05-09 13:34:34 -07001071 return testPath{basePath{path: p, rel: p}}
1072}
1073
1074func PathsForTesting(strs []string) Paths {
1075 p := make(Paths, len(strs))
1076 for i, s := range strs {
1077 p[i] = PathForTesting(s)
1078 }
1079
1080 return p
1081}