blob: 8d9f6fd44b69bd09ff31962b0b56a461bd5bc77c [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
15package common
16
17import (
Colin Cross6e18ca42015-07-14 18:55:36 -070018 "fmt"
Colin Crossf2298272015-05-12 11:36:53 -070019 "os"
Colin Cross6a745c62015-06-16 16:38:10 -070020 "path/filepath"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070021 "reflect"
22 "strings"
23
Dan Willemsen7b310ee2015-12-18 15:11:17 -080024 "android/soong/glob"
25
Dan Willemsen34cc69e2015-09-23 15:26:20 -070026 "github.com/google/blueprint"
27 "github.com/google/blueprint/pathtools"
Colin Cross3f40fa42015-01-30 17:27:36 -080028)
29
Dan Willemsen34cc69e2015-09-23 15:26:20 -070030// PathContext is the subset of a (Module|Singleton)Context required by the
31// Path methods.
32type PathContext interface {
33 Config() interface{}
Dan Willemsen7b310ee2015-12-18 15:11:17 -080034 AddNinjaFileDeps(deps ...string)
Colin Cross3f40fa42015-01-30 17:27:36 -080035}
36
Dan Willemsen34cc69e2015-09-23 15:26:20 -070037var _ PathContext = blueprint.SingletonContext(nil)
38var _ PathContext = blueprint.ModuleContext(nil)
39
40// errorfContext is the interface containing the Errorf method matching the
41// Errorf method in blueprint.SingletonContext.
42type errorfContext interface {
43 Errorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -080044}
45
Dan Willemsen34cc69e2015-09-23 15:26:20 -070046var _ errorfContext = blueprint.SingletonContext(nil)
47
48// moduleErrorf is the interface containing the ModuleErrorf method matching
49// the ModuleErrorf method in blueprint.ModuleContext.
50type moduleErrorf interface {
51 ModuleErrorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -080052}
53
Dan Willemsen34cc69e2015-09-23 15:26:20 -070054var _ moduleErrorf = blueprint.ModuleContext(nil)
55
56// pathConfig returns the android Config interface associated to the context.
57// Panics if the context isn't affiliated with an android build.
58func pathConfig(ctx PathContext) Config {
59 if ret, ok := ctx.Config().(Config); ok {
60 return ret
61 }
62 panic("Paths may only be used on Soong builds")
Colin Cross3f40fa42015-01-30 17:27:36 -080063}
64
Dan Willemsen34cc69e2015-09-23 15:26:20 -070065// reportPathError will register an error with the attached context. It
66// attempts ctx.ModuleErrorf for a better error message first, then falls
67// back to ctx.Errorf.
68func reportPathError(ctx PathContext, format string, args ...interface{}) {
69 if mctx, ok := ctx.(moduleErrorf); ok {
70 mctx.ModuleErrorf(format, args...)
71 } else if ectx, ok := ctx.(errorfContext); ok {
72 ectx.Errorf(format, args...)
73 } else {
74 panic(fmt.Sprintf(format, args...))
Colin Crossf2298272015-05-12 11:36:53 -070075 }
76}
77
Dan Willemsen34cc69e2015-09-23 15:26:20 -070078type Path interface {
79 // Returns the path in string form
80 String() string
81
82 // Returns the current file extension of the path
83 Ext() string
84}
85
86// WritablePath is a type of path that can be used as an output for build rules.
87type WritablePath interface {
88 Path
89
90 writablePath()
91}
92
93type genPathProvider interface {
94 genPathWithExt(ctx AndroidModuleContext, ext string) ModuleGenPath
95}
96type objPathProvider interface {
97 objPathWithExt(ctx AndroidModuleContext, subdir, ext string) ModuleObjPath
98}
99type resPathProvider interface {
100 resPathWithName(ctx AndroidModuleContext, name string) ModuleResPath
101}
102
103// GenPathWithExt derives a new file path in ctx's generated sources directory
104// from the current path, but with the new extension.
105func GenPathWithExt(ctx AndroidModuleContext, p Path, ext string) ModuleGenPath {
106 if path, ok := p.(genPathProvider); ok {
107 return path.genPathWithExt(ctx, ext)
108 }
109 reportPathError(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
110 return PathForModuleGen(ctx)
111}
112
113// ObjPathWithExt derives a new file path in ctx's object directory from the
114// current path, but with the new extension.
115func ObjPathWithExt(ctx AndroidModuleContext, p Path, subdir, ext string) ModuleObjPath {
116 if path, ok := p.(objPathProvider); ok {
117 return path.objPathWithExt(ctx, subdir, ext)
118 }
119 reportPathError(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
120 return PathForModuleObj(ctx)
121}
122
123// ResPathWithName derives a new path in ctx's output resource directory, using
124// the current path to create the directory name, and the `name` argument for
125// the filename.
126func ResPathWithName(ctx AndroidModuleContext, p Path, name string) ModuleResPath {
127 if path, ok := p.(resPathProvider); ok {
128 return path.resPathWithName(ctx, name)
129 }
130 reportPathError(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
131 return PathForModuleRes(ctx)
132}
133
134// OptionalPath is a container that may or may not contain a valid Path.
135type OptionalPath struct {
136 valid bool
137 path Path
138}
139
140// OptionalPathForPath returns an OptionalPath containing the path.
141func OptionalPathForPath(path Path) OptionalPath {
142 if path == nil {
143 return OptionalPath{}
144 }
145 return OptionalPath{valid: true, path: path}
146}
147
148// Valid returns whether there is a valid path
149func (p OptionalPath) Valid() bool {
150 return p.valid
151}
152
153// Path returns the Path embedded in this OptionalPath. You must be sure that
154// there is a valid path, since this method will panic if there is not.
155func (p OptionalPath) Path() Path {
156 if !p.valid {
157 panic("Requesting an invalid path")
158 }
159 return p.path
160}
161
162// String returns the string version of the Path, or "" if it isn't valid.
163func (p OptionalPath) String() string {
164 if p.valid {
165 return p.path.String()
166 } else {
167 return ""
Colin Crossf2298272015-05-12 11:36:53 -0700168 }
169}
Colin Cross6e18ca42015-07-14 18:55:36 -0700170
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700171// Paths is a slice of Path objects, with helpers to operate on the collection.
172type Paths []Path
173
174// PathsForSource returns Paths rooted from SrcDir
175func PathsForSource(ctx PathContext, paths []string) Paths {
Dan Willemsene23dfb72016-03-11 15:02:46 -0800176 if pathConfig(ctx).AllowMissingDependencies() {
177 if modCtx, ok := ctx.(AndroidModuleContext); ok {
178 ret := make(Paths, 0, len(paths))
179 intermediates := PathForModuleOut(modCtx, "missing").String()
180 for _, path := range paths {
181 p := OptionalPathForSource(ctx, intermediates, path)
182 if p.Valid() {
183 ret = append(ret, p.Path())
184 } else {
185 modCtx.AddMissingDependencies([]string{path})
186 }
187 }
188 return ret
189 }
190 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700191 ret := make(Paths, len(paths))
192 for i, path := range paths {
193 ret[i] = PathForSource(ctx, path)
194 }
195 return ret
196}
197
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800198// PathsForOptionalSource returns a list of Paths rooted from SrcDir that are
199// found in the tree. If any are not found, they are omitted from the list,
200// and dependencies are added so that we're re-run when they are added.
201func PathsForOptionalSource(ctx PathContext, intermediates string, paths []string) Paths {
202 ret := make(Paths, 0, len(paths))
203 for _, path := range paths {
204 p := OptionalPathForSource(ctx, intermediates, path)
205 if p.Valid() {
206 ret = append(ret, p.Path())
207 }
208 }
209 return ret
210}
211
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700212// PathsForModuleSrc returns Paths rooted from the module's local source
213// directory
214func PathsForModuleSrc(ctx AndroidModuleContext, paths []string) Paths {
215 ret := make(Paths, len(paths))
216 for i, path := range paths {
217 ret[i] = PathForModuleSrc(ctx, path)
218 }
219 return ret
220}
221
222// pathsForModuleSrcFromFullPath returns Paths rooted from the module's local
223// source directory, but strip the local source directory from the beginning of
224// each string.
225func pathsForModuleSrcFromFullPath(ctx AndroidModuleContext, paths []string) Paths {
226 prefix := filepath.Join(ctx.AConfig().srcDir, ctx.ModuleDir()) + "/"
227 ret := make(Paths, 0, len(paths))
228 for _, p := range paths {
229 path := filepath.Clean(p)
230 if !strings.HasPrefix(path, prefix) {
231 reportPathError(ctx, "Path '%s' is not in module source directory '%s'", p, prefix)
232 continue
233 }
234 ret = append(ret, PathForModuleSrc(ctx, path[len(prefix):]))
235 }
236 return ret
237}
238
239// PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's
240// local source directory. If none are provided, use the default if it exists.
241func PathsWithOptionalDefaultForModuleSrc(ctx AndroidModuleContext, input []string, def string) Paths {
242 if len(input) > 0 {
243 return PathsForModuleSrc(ctx, input)
244 }
245 // Use Glob so that if the default doesn't exist, a dependency is added so that when it
246 // is created, we're run again.
247 path := filepath.Join(ctx.AConfig().srcDir, ctx.ModuleDir(), def)
248 return ctx.Glob("default", path, []string{})
249}
250
251// Strings returns the Paths in string form
252func (p Paths) Strings() []string {
253 if p == nil {
254 return nil
255 }
256 ret := make([]string, len(p))
257 for i, path := range p {
258 ret[i] = path.String()
259 }
260 return ret
261}
262
263// WritablePaths is a slice of WritablePaths, used for multiple outputs.
264type WritablePaths []WritablePath
265
266// Strings returns the string forms of the writable paths.
267func (p WritablePaths) Strings() []string {
268 if p == nil {
269 return nil
270 }
271 ret := make([]string, len(p))
272 for i, path := range p {
273 ret[i] = path.String()
274 }
275 return ret
276}
277
278type basePath struct {
279 path string
280 config Config
281}
282
283func (p basePath) Ext() string {
284 return filepath.Ext(p.path)
285}
286
287// SourcePath is a Path representing a file path rooted from SrcDir
288type SourcePath struct {
289 basePath
290}
291
292var _ Path = SourcePath{}
293
294// safePathForSource is for paths that we expect are safe -- only for use by go
295// code that is embedding ninja variables in paths
296func safePathForSource(ctx PathContext, path string) SourcePath {
297 p := validateSafePath(ctx, path)
298 ret := SourcePath{basePath{p, pathConfig(ctx)}}
299
300 abs, err := filepath.Abs(ret.String())
Colin Cross6e18ca42015-07-14 18:55:36 -0700301 if err != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700302 reportPathError(ctx, "%s", err.Error())
303 return ret
304 }
305 buildroot, err := filepath.Abs(pathConfig(ctx).buildDir)
306 if err != nil {
307 reportPathError(ctx, "%s", err.Error())
308 return ret
309 }
310 if strings.HasPrefix(abs, buildroot) {
311 reportPathError(ctx, "source path %s is in output", abs)
312 return ret
Colin Cross6e18ca42015-07-14 18:55:36 -0700313 }
314
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700315 return ret
316}
317
318// PathForSource returns a SourcePath for the provided paths... (which are
319// joined together with filepath.Join). This also validates that the path
320// doesn't escape the source dir, or is contained in the build dir. On error, it
321// will return a usable, but invalid SourcePath, and report a ModuleError.
322func PathForSource(ctx PathContext, paths ...string) SourcePath {
323 p := validatePath(ctx, paths...)
324 ret := SourcePath{basePath{p, pathConfig(ctx)}}
325
326 abs, err := filepath.Abs(ret.String())
327 if err != nil {
328 reportPathError(ctx, "%s", err.Error())
329 return ret
330 }
331 buildroot, err := filepath.Abs(pathConfig(ctx).buildDir)
332 if err != nil {
333 reportPathError(ctx, "%s", err.Error())
334 return ret
335 }
336 if strings.HasPrefix(abs, buildroot) {
337 reportPathError(ctx, "source path %s is in output", abs)
338 return ret
339 }
340
341 if _, err = os.Stat(ret.String()); err != nil {
342 if os.IsNotExist(err) {
343 reportPathError(ctx, "source path %s does not exist", ret)
344 } else {
345 reportPathError(ctx, "%s: %s", ret, err.Error())
346 }
347 }
348 return ret
349}
350
351// OptionalPathForSource returns an OptionalPath with the SourcePath if the
352// path exists, or an empty OptionalPath if it doesn't exist. Dependencies are added
353// so that the ninja file will be regenerated if the state of the path changes.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800354func OptionalPathForSource(ctx PathContext, intermediates string, paths ...string) OptionalPath {
355 if len(paths) == 0 {
356 // For when someone forgets the 'intermediates' argument
357 panic("Missing path(s)")
358 }
359
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700360 p := validatePath(ctx, paths...)
361 path := SourcePath{basePath{p, pathConfig(ctx)}}
362
363 abs, err := filepath.Abs(path.String())
364 if err != nil {
365 reportPathError(ctx, "%s", err.Error())
366 return OptionalPath{}
367 }
368 buildroot, err := filepath.Abs(pathConfig(ctx).buildDir)
369 if err != nil {
370 reportPathError(ctx, "%s", err.Error())
371 return OptionalPath{}
372 }
373 if strings.HasPrefix(abs, buildroot) {
374 reportPathError(ctx, "source path %s is in output", abs)
375 return OptionalPath{}
376 }
377
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800378 if glob.IsGlob(path.String()) {
379 reportPathError(ctx, "path may not contain a glob: %s", path.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700380 return OptionalPath{}
381 }
382
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800383 if gctx, ok := ctx.(globContext); ok {
384 // Use glob to produce proper dependencies, even though we only want
385 // a single file.
386 files, err := Glob(gctx, PathForIntermediates(ctx, intermediates).String(), path.String(), nil)
387 if err != nil {
388 reportPathError(ctx, "glob: %s", err.Error())
389 return OptionalPath{}
390 }
391
392 if len(files) == 0 {
393 return OptionalPath{}
394 }
395 } else {
396 // We cannot add build statements in this context, so we fall back to
397 // AddNinjaFileDeps
398 files, dirs, err := pathtools.Glob(path.String())
399 if err != nil {
400 reportPathError(ctx, "glob: %s", err.Error())
401 return OptionalPath{}
402 }
403
404 ctx.AddNinjaFileDeps(dirs...)
405
406 if len(files) == 0 {
407 return OptionalPath{}
408 }
409
410 ctx.AddNinjaFileDeps(path.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700411 }
412 return OptionalPathForPath(path)
413}
414
415func (p SourcePath) String() string {
416 return filepath.Join(p.config.srcDir, p.path)
417}
418
419// Join creates a new SourcePath with paths... joined with the current path. The
420// provided paths... may not use '..' to escape from the current path.
421func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
422 path := validatePath(ctx, paths...)
423 return PathForSource(ctx, p.path, path)
424}
425
426// OverlayPath returns the overlay for `path' if it exists. This assumes that the
427// SourcePath is the path to a resource overlay directory.
428func (p SourcePath) OverlayPath(ctx AndroidModuleContext, path Path) OptionalPath {
429 var relDir string
430 if moduleSrcPath, ok := path.(ModuleSrcPath); ok {
431 relDir = moduleSrcPath.sourcePath.path
432 } else if srcPath, ok := path.(SourcePath); ok {
433 relDir = srcPath.path
434 } else {
435 reportPathError(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
436 return OptionalPath{}
437 }
438 dir := filepath.Join(p.config.srcDir, p.path, relDir)
439 // Use Glob so that we are run again if the directory is added.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800440 if glob.IsGlob(dir) {
441 reportPathError(ctx, "Path may not contain a glob: %s", dir)
442 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700443 paths, err := Glob(ctx, PathForModuleOut(ctx, "overlay").String(), dir, []string{})
444 if err != nil {
445 reportPathError(ctx, "glob: %s", err.Error())
446 return OptionalPath{}
447 }
448 if len(paths) == 0 {
449 return OptionalPath{}
450 }
451 relPath, err := filepath.Rel(p.config.srcDir, paths[0])
452 if err != nil {
453 reportPathError(ctx, "%s", err.Error())
454 return OptionalPath{}
455 }
456 return OptionalPathForPath(PathForSource(ctx, relPath))
457}
458
459// OutputPath is a Path representing a file path rooted from the build directory
460type OutputPath struct {
461 basePath
462}
463
464var _ Path = OutputPath{}
465
466// PathForOutput returns an OutputPath for the provided paths... (which are
467// joined together with filepath.Join). This also validates that the path
468// does not escape the build dir. On error, it will return a usable, but invalid
469// OutputPath, and report a ModuleError.
470func PathForOutput(ctx PathContext, paths ...string) OutputPath {
471 path := validatePath(ctx, paths...)
472 return OutputPath{basePath{path, pathConfig(ctx)}}
473}
474
475func (p OutputPath) writablePath() {}
476
477func (p OutputPath) String() string {
478 return filepath.Join(p.config.buildDir, p.path)
479}
480
481// Join creates a new OutputPath with paths... joined with the current path. The
482// provided paths... may not use '..' to escape from the current path.
483func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
484 path := validatePath(ctx, paths...)
485 return PathForOutput(ctx, p.path, path)
486}
487
488// PathForIntermediates returns an OutputPath representing the top-level
489// intermediates directory.
490func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
491 path := validatePath(ctx, paths...)
492 return PathForOutput(ctx, ".intermediates", path)
493}
494
495// ModuleSrcPath is a Path representing a file rooted from a module's local source dir
496type ModuleSrcPath struct {
497 basePath
498 sourcePath SourcePath
499 moduleDir string
500}
501
502var _ Path = ModuleSrcPath{}
503var _ genPathProvider = ModuleSrcPath{}
504var _ objPathProvider = ModuleSrcPath{}
505var _ resPathProvider = ModuleSrcPath{}
506
507// PathForModuleSrc returns a ModuleSrcPath representing the paths... under the
508// module's local source directory.
509func PathForModuleSrc(ctx AndroidModuleContext, paths ...string) ModuleSrcPath {
510 path := validatePath(ctx, paths...)
511 return ModuleSrcPath{basePath{path, ctx.AConfig()}, PathForSource(ctx, ctx.ModuleDir(), path), ctx.ModuleDir()}
512}
513
514// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
515// valid path if p is non-nil.
516func OptionalPathForModuleSrc(ctx AndroidModuleContext, p *string) OptionalPath {
517 if p == nil {
518 return OptionalPath{}
519 }
520 return OptionalPathForPath(PathForModuleSrc(ctx, *p))
521}
522
523func (p ModuleSrcPath) String() string {
524 return p.sourcePath.String()
525}
526
527func (p ModuleSrcPath) genPathWithExt(ctx AndroidModuleContext, ext string) ModuleGenPath {
528 return PathForModuleGen(ctx, p.moduleDir, pathtools.ReplaceExtension(p.path, ext))
529}
530
531func (p ModuleSrcPath) objPathWithExt(ctx AndroidModuleContext, subdir, ext string) ModuleObjPath {
532 return PathForModuleObj(ctx, subdir, p.moduleDir, pathtools.ReplaceExtension(p.path, ext))
533}
534
535func (p ModuleSrcPath) resPathWithName(ctx AndroidModuleContext, name string) ModuleResPath {
536 // TODO: Use full directory if the new ctx is not the current ctx?
537 return PathForModuleRes(ctx, p.path, name)
538}
539
540// ModuleOutPath is a Path representing a module's output directory.
541type ModuleOutPath struct {
542 OutputPath
543}
544
545var _ Path = ModuleOutPath{}
546
547// PathForModuleOut returns a Path representing the paths... under the module's
548// output directory.
549func PathForModuleOut(ctx AndroidModuleContext, paths ...string) ModuleOutPath {
550 p := validatePath(ctx, paths...)
551 return ModuleOutPath{PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir(), p)}
552}
553
554// ModuleGenPath is a Path representing the 'gen' directory in a module's output
555// directory. Mainly used for generated sources.
556type ModuleGenPath struct {
557 ModuleOutPath
558 path string
559}
560
561var _ Path = ModuleGenPath{}
562var _ genPathProvider = ModuleGenPath{}
563var _ objPathProvider = ModuleGenPath{}
564
565// PathForModuleGen returns a Path representing the paths... under the module's
566// `gen' directory.
567func PathForModuleGen(ctx AndroidModuleContext, paths ...string) ModuleGenPath {
568 p := validatePath(ctx, paths...)
569 return ModuleGenPath{
570 PathForModuleOut(ctx, "gen", p),
571 p,
572 }
573}
574
575func (p ModuleGenPath) genPathWithExt(ctx AndroidModuleContext, ext string) ModuleGenPath {
576 // TODO: make a different path for local vs remote generated files?
577 return PathForModuleGen(ctx, pathtools.ReplaceExtension(p.path, ext))
578}
579
580func (p ModuleGenPath) objPathWithExt(ctx AndroidModuleContext, subdir, ext string) ModuleObjPath {
581 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
582}
583
584// ModuleObjPath is a Path representing the 'obj' directory in a module's output
585// directory. Used for compiled objects.
586type ModuleObjPath struct {
587 ModuleOutPath
588}
589
590var _ Path = ModuleObjPath{}
591
592// PathForModuleObj returns a Path representing the paths... under the module's
593// 'obj' directory.
594func PathForModuleObj(ctx AndroidModuleContext, paths ...string) ModuleObjPath {
595 p := validatePath(ctx, paths...)
596 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
597}
598
599// ModuleResPath is a a Path representing the 'res' directory in a module's
600// output directory.
601type ModuleResPath struct {
602 ModuleOutPath
603}
604
605var _ Path = ModuleResPath{}
606
607// PathForModuleRes returns a Path representing the paths... under the module's
608// 'res' directory.
609func PathForModuleRes(ctx AndroidModuleContext, paths ...string) ModuleResPath {
610 p := validatePath(ctx, paths...)
611 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
612}
613
614// PathForModuleInstall returns a Path representing the install path for the
615// module appended with paths...
616func PathForModuleInstall(ctx AndroidModuleContext, paths ...string) OutputPath {
617 var outPaths []string
618 if ctx.Device() {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800619 partition := "system"
620 if ctx.Proprietary() {
621 partition = "vendor"
622 }
623 if ctx.InstallInData() {
624 partition = "data"
625 }
626 outPaths = []string{"target", "product", ctx.AConfig().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700627 } else {
628 outPaths = []string{"host", ctx.HostType().String() + "-x86"}
629 }
Dan Willemsen782a2d12015-12-21 14:55:28 -0800630 if ctx.Debug() {
631 outPaths = append([]string{"debug"}, outPaths...)
632 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700633 outPaths = append(outPaths, paths...)
634 return PathForOutput(ctx, outPaths...)
635}
636
637// validateSafePath validates a path that we trust (may contain ninja variables).
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800638// Ensures that each path component does not attempt to leave its component.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700639func validateSafePath(ctx PathContext, paths ...string) string {
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800640 for _, path := range paths {
641 path := filepath.Clean(path)
642 if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
643 reportPathError(ctx, "Path is outside directory: %s", path)
644 return ""
645 }
646 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700647 // TODO: filepath.Join isn't necessarily correct with embedded ninja
648 // variables. '..' may remove the entire ninja variable, even if it
649 // will be expanded to multiple nested directories.
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800650 return filepath.Join(paths...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700651}
652
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800653// validatePath validates that a path does not include ninja variables, and that
654// each path component does not attempt to leave its component. Returns a joined
655// version of each path component.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700656func validatePath(ctx PathContext, paths ...string) string {
657 for _, path := range paths {
658 if strings.Contains(path, "$") {
659 reportPathError(ctx, "Path contains invalid character($): %s", path)
660 return ""
661 }
662 }
663 return validateSafePath(ctx, paths...)
Colin Cross6e18ca42015-07-14 18:55:36 -0700664}