blob: 8eabfb84fd6d9adac1fcc4b6f0d1f23d80e41cbf [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 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
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 {
31 Config() interface{}
Dan Willemsen7b310ee2015-12-18 15:11:17 -080032 AddNinjaFileDeps(deps ...string)
Colin Cross3f40fa42015-01-30 17:27:36 -080033}
34
Colin Cross7f19f372016-11-01 11:10:25 -070035type PathGlobContext interface {
36 GlobWithDeps(globPattern string, excludes []string) ([]string, error)
37}
38
Dan Willemsen34cc69e2015-09-23 15:26:20 -070039var _ PathContext = blueprint.SingletonContext(nil)
40var _ PathContext = blueprint.ModuleContext(nil)
41
42// errorfContext is the interface containing the Errorf method matching the
43// Errorf method in blueprint.SingletonContext.
44type errorfContext interface {
45 Errorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -080046}
47
Dan Willemsen34cc69e2015-09-23 15:26:20 -070048var _ errorfContext = blueprint.SingletonContext(nil)
49
50// moduleErrorf is the interface containing the ModuleErrorf method matching
51// the ModuleErrorf method in blueprint.ModuleContext.
52type moduleErrorf interface {
53 ModuleErrorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -080054}
55
Dan Willemsen34cc69e2015-09-23 15:26:20 -070056var _ moduleErrorf = blueprint.ModuleContext(nil)
57
58// pathConfig returns the android Config interface associated to the context.
59// Panics if the context isn't affiliated with an android build.
60func pathConfig(ctx PathContext) Config {
61 if ret, ok := ctx.Config().(Config); ok {
62 return ret
63 }
64 panic("Paths may only be used on Soong builds")
Colin Cross3f40fa42015-01-30 17:27:36 -080065}
66
Dan Willemsen34cc69e2015-09-23 15:26:20 -070067// reportPathError will register an error with the attached context. It
68// attempts ctx.ModuleErrorf for a better error message first, then falls
69// back to ctx.Errorf.
70func reportPathError(ctx PathContext, format string, args ...interface{}) {
71 if mctx, ok := ctx.(moduleErrorf); ok {
72 mctx.ModuleErrorf(format, args...)
73 } else if ectx, ok := ctx.(errorfContext); ok {
74 ectx.Errorf(format, args...)
75 } else {
76 panic(fmt.Sprintf(format, args...))
Colin Crossf2298272015-05-12 11:36:53 -070077 }
78}
79
Dan Willemsen34cc69e2015-09-23 15:26:20 -070080type Path interface {
81 // Returns the path in string form
82 String() string
83
Colin Cross4f6fc9c2016-10-26 10:05:25 -070084 // Ext returns the extension of the last element of the path
Dan Willemsen34cc69e2015-09-23 15:26:20 -070085 Ext() string
Colin Cross4f6fc9c2016-10-26 10:05:25 -070086
87 // Base returns the last element of the path
88 Base() string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070089}
90
91// WritablePath is a type of path that can be used as an output for build rules.
92type WritablePath interface {
93 Path
94
95 writablePath()
96}
97
98type genPathProvider interface {
Dan Willemsen21ec4902016-11-02 20:43:13 -070099 genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700100}
101type objPathProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700102 objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700103}
104type resPathProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700105 resPathWithName(ctx ModuleContext, name string) ModuleResPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700106}
107
108// GenPathWithExt derives a new file path in ctx's generated sources directory
109// from the current path, but with the new extension.
Dan Willemsen21ec4902016-11-02 20:43:13 -0700110func GenPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700111 if path, ok := p.(genPathProvider); ok {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700112 return path.genPathWithExt(ctx, subdir, ext)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700113 }
114 reportPathError(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
115 return PathForModuleGen(ctx)
116}
117
118// ObjPathWithExt derives a new file path in ctx's object directory from the
119// current path, but with the new extension.
Dan Willemsen21ec4902016-11-02 20:43:13 -0700120func ObjPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700121 if path, ok := p.(objPathProvider); ok {
122 return path.objPathWithExt(ctx, subdir, ext)
123 }
124 reportPathError(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
125 return PathForModuleObj(ctx)
126}
127
128// ResPathWithName derives a new path in ctx's output resource directory, using
129// the current path to create the directory name, and the `name` argument for
130// the filename.
Colin Cross635c3b02016-05-18 15:37:25 -0700131func ResPathWithName(ctx ModuleContext, p Path, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700132 if path, ok := p.(resPathProvider); ok {
133 return path.resPathWithName(ctx, name)
134 }
135 reportPathError(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
136 return PathForModuleRes(ctx)
137}
138
139// OptionalPath is a container that may or may not contain a valid Path.
140type OptionalPath struct {
141 valid bool
142 path Path
143}
144
145// OptionalPathForPath returns an OptionalPath containing the path.
146func OptionalPathForPath(path Path) OptionalPath {
147 if path == nil {
148 return OptionalPath{}
149 }
150 return OptionalPath{valid: true, path: path}
151}
152
153// Valid returns whether there is a valid path
154func (p OptionalPath) Valid() bool {
155 return p.valid
156}
157
158// Path returns the Path embedded in this OptionalPath. You must be sure that
159// there is a valid path, since this method will panic if there is not.
160func (p OptionalPath) Path() Path {
161 if !p.valid {
162 panic("Requesting an invalid path")
163 }
164 return p.path
165}
166
167// String returns the string version of the Path, or "" if it isn't valid.
168func (p OptionalPath) String() string {
169 if p.valid {
170 return p.path.String()
171 } else {
172 return ""
Colin Crossf2298272015-05-12 11:36:53 -0700173 }
174}
Colin Cross6e18ca42015-07-14 18:55:36 -0700175
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700176// Paths is a slice of Path objects, with helpers to operate on the collection.
177type Paths []Path
178
179// PathsForSource returns Paths rooted from SrcDir
180func PathsForSource(ctx PathContext, paths []string) Paths {
Dan Willemsene23dfb72016-03-11 15:02:46 -0800181 if pathConfig(ctx).AllowMissingDependencies() {
Colin Cross635c3b02016-05-18 15:37:25 -0700182 if modCtx, ok := ctx.(ModuleContext); ok {
Dan Willemsene23dfb72016-03-11 15:02:46 -0800183 ret := make(Paths, 0, len(paths))
Dan Willemsen0f6042e2016-03-11 17:01:03 -0800184 intermediates := filepath.Join(modCtx.ModuleDir(), modCtx.ModuleName(), modCtx.ModuleSubDir(), "missing")
Dan Willemsene23dfb72016-03-11 15:02:46 -0800185 for _, path := range paths {
186 p := OptionalPathForSource(ctx, intermediates, path)
187 if p.Valid() {
188 ret = append(ret, p.Path())
189 } else {
190 modCtx.AddMissingDependencies([]string{path})
191 }
192 }
193 return ret
194 }
195 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700196 ret := make(Paths, len(paths))
197 for i, path := range paths {
198 ret[i] = PathForSource(ctx, path)
199 }
200 return ret
201}
202
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800203// PathsForOptionalSource returns a list of Paths rooted from SrcDir that are
204// found in the tree. If any are not found, they are omitted from the list,
205// and dependencies are added so that we're re-run when they are added.
206func PathsForOptionalSource(ctx PathContext, intermediates string, paths []string) Paths {
207 ret := make(Paths, 0, len(paths))
208 for _, path := range paths {
209 p := OptionalPathForSource(ctx, intermediates, path)
210 if p.Valid() {
211 ret = append(ret, p.Path())
212 }
213 }
214 return ret
215}
216
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700217// PathsForModuleSrc returns Paths rooted from the module's local source
218// directory
Colin Cross635c3b02016-05-18 15:37:25 -0700219func PathsForModuleSrc(ctx ModuleContext, paths []string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700220 ret := make(Paths, len(paths))
221 for i, path := range paths {
222 ret[i] = PathForModuleSrc(ctx, path)
223 }
224 return ret
225}
226
227// pathsForModuleSrcFromFullPath returns Paths rooted from the module's local
228// source directory, but strip the local source directory from the beginning of
229// each string.
Colin Cross635c3b02016-05-18 15:37:25 -0700230func pathsForModuleSrcFromFullPath(ctx ModuleContext, paths []string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700231 prefix := filepath.Join(ctx.AConfig().srcDir, ctx.ModuleDir()) + "/"
232 ret := make(Paths, 0, len(paths))
233 for _, p := range paths {
234 path := filepath.Clean(p)
235 if !strings.HasPrefix(path, prefix) {
236 reportPathError(ctx, "Path '%s' is not in module source directory '%s'", p, prefix)
237 continue
238 }
239 ret = append(ret, PathForModuleSrc(ctx, path[len(prefix):]))
240 }
241 return ret
242}
243
244// PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's
245// local source directory. If none are provided, use the default if it exists.
Colin Cross635c3b02016-05-18 15:37:25 -0700246func PathsWithOptionalDefaultForModuleSrc(ctx ModuleContext, input []string, def string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700247 if len(input) > 0 {
248 return PathsForModuleSrc(ctx, input)
249 }
250 // Use Glob so that if the default doesn't exist, a dependency is added so that when it
251 // is created, we're run again.
252 path := filepath.Join(ctx.AConfig().srcDir, ctx.ModuleDir(), def)
Colin Cross7f19f372016-11-01 11:10:25 -0700253 return ctx.Glob(path, []string{})
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700254}
255
256// Strings returns the Paths in string form
257func (p Paths) Strings() []string {
258 if p == nil {
259 return nil
260 }
261 ret := make([]string, len(p))
262 for i, path := range p {
263 ret[i] = path.String()
264 }
265 return ret
266}
267
268// WritablePaths is a slice of WritablePaths, used for multiple outputs.
269type WritablePaths []WritablePath
270
271// Strings returns the string forms of the writable paths.
272func (p WritablePaths) Strings() []string {
273 if p == nil {
274 return nil
275 }
276 ret := make([]string, len(p))
277 for i, path := range p {
278 ret[i] = path.String()
279 }
280 return ret
281}
282
283type basePath struct {
284 path string
285 config Config
286}
287
288func (p basePath) Ext() string {
289 return filepath.Ext(p.path)
290}
291
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700292func (p basePath) Base() string {
293 return filepath.Base(p.path)
294}
295
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700296// SourcePath is a Path representing a file path rooted from SrcDir
297type SourcePath struct {
298 basePath
299}
300
301var _ Path = SourcePath{}
302
303// safePathForSource is for paths that we expect are safe -- only for use by go
304// code that is embedding ninja variables in paths
305func safePathForSource(ctx PathContext, path string) SourcePath {
306 p := validateSafePath(ctx, path)
307 ret := SourcePath{basePath{p, pathConfig(ctx)}}
308
309 abs, err := filepath.Abs(ret.String())
Colin Cross6e18ca42015-07-14 18:55:36 -0700310 if err != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700311 reportPathError(ctx, "%s", err.Error())
312 return ret
313 }
314 buildroot, err := filepath.Abs(pathConfig(ctx).buildDir)
315 if err != nil {
316 reportPathError(ctx, "%s", err.Error())
317 return ret
318 }
319 if strings.HasPrefix(abs, buildroot) {
320 reportPathError(ctx, "source path %s is in output", abs)
321 return ret
Colin Cross6e18ca42015-07-14 18:55:36 -0700322 }
323
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700324 return ret
325}
326
327// PathForSource returns a SourcePath for the provided paths... (which are
328// joined together with filepath.Join). This also validates that the path
329// doesn't escape the source dir, or is contained in the build dir. On error, it
330// will return a usable, but invalid SourcePath, and report a ModuleError.
331func PathForSource(ctx PathContext, paths ...string) SourcePath {
332 p := validatePath(ctx, paths...)
333 ret := SourcePath{basePath{p, pathConfig(ctx)}}
334
335 abs, err := filepath.Abs(ret.String())
336 if err != nil {
337 reportPathError(ctx, "%s", err.Error())
338 return ret
339 }
340 buildroot, err := filepath.Abs(pathConfig(ctx).buildDir)
341 if err != nil {
342 reportPathError(ctx, "%s", err.Error())
343 return ret
344 }
345 if strings.HasPrefix(abs, buildroot) {
346 reportPathError(ctx, "source path %s is in output", abs)
347 return ret
348 }
349
350 if _, err = os.Stat(ret.String()); err != nil {
351 if os.IsNotExist(err) {
352 reportPathError(ctx, "source path %s does not exist", ret)
353 } else {
354 reportPathError(ctx, "%s: %s", ret, err.Error())
355 }
356 }
357 return ret
358}
359
360// OptionalPathForSource returns an OptionalPath with the SourcePath if the
361// path exists, or an empty OptionalPath if it doesn't exist. Dependencies are added
362// so that the ninja file will be regenerated if the state of the path changes.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800363func OptionalPathForSource(ctx PathContext, intermediates string, paths ...string) OptionalPath {
364 if len(paths) == 0 {
365 // For when someone forgets the 'intermediates' argument
366 panic("Missing path(s)")
367 }
368
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700369 p := validatePath(ctx, paths...)
370 path := SourcePath{basePath{p, pathConfig(ctx)}}
371
372 abs, err := filepath.Abs(path.String())
373 if err != nil {
374 reportPathError(ctx, "%s", err.Error())
375 return OptionalPath{}
376 }
377 buildroot, err := filepath.Abs(pathConfig(ctx).buildDir)
378 if err != nil {
379 reportPathError(ctx, "%s", err.Error())
380 return OptionalPath{}
381 }
382 if strings.HasPrefix(abs, buildroot) {
383 reportPathError(ctx, "source path %s is in output", abs)
384 return OptionalPath{}
385 }
386
Colin Cross7f19f372016-11-01 11:10:25 -0700387 if pathtools.IsGlob(path.String()) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800388 reportPathError(ctx, "path may not contain a glob: %s", path.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700389 return OptionalPath{}
390 }
391
Colin Cross7f19f372016-11-01 11:10:25 -0700392 if gctx, ok := ctx.(PathGlobContext); ok {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800393 // Use glob to produce proper dependencies, even though we only want
394 // a single file.
Colin Cross7f19f372016-11-01 11:10:25 -0700395 files, err := gctx.GlobWithDeps(path.String(), nil)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800396 if err != nil {
397 reportPathError(ctx, "glob: %s", err.Error())
398 return OptionalPath{}
399 }
400
401 if len(files) == 0 {
402 return OptionalPath{}
403 }
404 } else {
405 // We cannot add build statements in this context, so we fall back to
406 // AddNinjaFileDeps
407 files, dirs, err := pathtools.Glob(path.String())
408 if err != nil {
409 reportPathError(ctx, "glob: %s", err.Error())
410 return OptionalPath{}
411 }
412
413 ctx.AddNinjaFileDeps(dirs...)
414
415 if len(files) == 0 {
416 return OptionalPath{}
417 }
418
419 ctx.AddNinjaFileDeps(path.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700420 }
421 return OptionalPathForPath(path)
422}
423
424func (p SourcePath) String() string {
425 return filepath.Join(p.config.srcDir, p.path)
426}
427
428// Join creates a new SourcePath with paths... joined with the current path. The
429// provided paths... may not use '..' to escape from the current path.
430func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
431 path := validatePath(ctx, paths...)
432 return PathForSource(ctx, p.path, path)
433}
434
435// OverlayPath returns the overlay for `path' if it exists. This assumes that the
436// SourcePath is the path to a resource overlay directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700437func (p SourcePath) OverlayPath(ctx ModuleContext, path Path) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700438 var relDir string
439 if moduleSrcPath, ok := path.(ModuleSrcPath); ok {
Colin Cross7fc17db2017-02-01 14:07:55 -0800440 relDir = moduleSrcPath.path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700441 } else if srcPath, ok := path.(SourcePath); ok {
442 relDir = srcPath.path
443 } else {
444 reportPathError(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
445 return OptionalPath{}
446 }
447 dir := filepath.Join(p.config.srcDir, p.path, relDir)
448 // Use Glob so that we are run again if the directory is added.
Colin Cross7f19f372016-11-01 11:10:25 -0700449 if pathtools.IsGlob(dir) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800450 reportPathError(ctx, "Path may not contain a glob: %s", dir)
451 }
Colin Cross7f19f372016-11-01 11:10:25 -0700452 paths, err := ctx.GlobWithDeps(dir, []string{})
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700453 if err != nil {
454 reportPathError(ctx, "glob: %s", err.Error())
455 return OptionalPath{}
456 }
457 if len(paths) == 0 {
458 return OptionalPath{}
459 }
460 relPath, err := filepath.Rel(p.config.srcDir, paths[0])
461 if err != nil {
462 reportPathError(ctx, "%s", err.Error())
463 return OptionalPath{}
464 }
465 return OptionalPathForPath(PathForSource(ctx, relPath))
466}
467
468// OutputPath is a Path representing a file path rooted from the build directory
469type OutputPath struct {
470 basePath
471}
472
473var _ Path = OutputPath{}
474
475// PathForOutput returns an OutputPath for the provided paths... (which are
476// joined together with filepath.Join). This also validates that the path
477// does not escape the build dir. On error, it will return a usable, but invalid
478// OutputPath, and report a ModuleError.
479func PathForOutput(ctx PathContext, paths ...string) OutputPath {
480 path := validatePath(ctx, paths...)
481 return OutputPath{basePath{path, pathConfig(ctx)}}
482}
483
484func (p OutputPath) writablePath() {}
485
486func (p OutputPath) String() string {
487 return filepath.Join(p.config.buildDir, p.path)
488}
489
Colin Crossa2344662016-03-24 13:14:12 -0700490func (p OutputPath) RelPathString() string {
491 return p.path
492}
493
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700494// Join creates a new OutputPath with paths... joined with the current path. The
495// provided paths... may not use '..' to escape from the current path.
496func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
497 path := validatePath(ctx, paths...)
498 return PathForOutput(ctx, p.path, path)
499}
500
501// PathForIntermediates returns an OutputPath representing the top-level
502// intermediates directory.
503func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
504 path := validatePath(ctx, paths...)
505 return PathForOutput(ctx, ".intermediates", path)
506}
507
508// ModuleSrcPath is a Path representing a file rooted from a module's local source dir
509type ModuleSrcPath struct {
Colin Cross7fc17db2017-02-01 14:07:55 -0800510 SourcePath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700511}
512
513var _ Path = ModuleSrcPath{}
514var _ genPathProvider = ModuleSrcPath{}
515var _ objPathProvider = ModuleSrcPath{}
516var _ resPathProvider = ModuleSrcPath{}
517
518// PathForModuleSrc returns a ModuleSrcPath representing the paths... under the
519// module's local source directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700520func PathForModuleSrc(ctx ModuleContext, paths ...string) ModuleSrcPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700521 path := validatePath(ctx, paths...)
Colin Cross7fc17db2017-02-01 14:07:55 -0800522 return ModuleSrcPath{PathForSource(ctx, ctx.ModuleDir(), path)}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700523}
524
525// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
526// valid path if p is non-nil.
Colin Cross635c3b02016-05-18 15:37:25 -0700527func OptionalPathForModuleSrc(ctx ModuleContext, p *string) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700528 if p == nil {
529 return OptionalPath{}
530 }
531 return OptionalPathForPath(PathForModuleSrc(ctx, *p))
532}
533
Dan Willemsen21ec4902016-11-02 20:43:13 -0700534func (p ModuleSrcPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Colin Cross7fc17db2017-02-01 14:07:55 -0800535 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700536}
537
Colin Cross635c3b02016-05-18 15:37:25 -0700538func (p ModuleSrcPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Colin Cross7fc17db2017-02-01 14:07:55 -0800539 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700540}
541
Colin Cross635c3b02016-05-18 15:37:25 -0700542func (p ModuleSrcPath) resPathWithName(ctx ModuleContext, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700543 // TODO: Use full directory if the new ctx is not the current ctx?
544 return PathForModuleRes(ctx, p.path, name)
545}
546
547// ModuleOutPath is a Path representing a module's output directory.
548type ModuleOutPath struct {
549 OutputPath
550}
551
552var _ Path = ModuleOutPath{}
553
554// PathForModuleOut returns a Path representing the paths... under the module's
555// output directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700556func PathForModuleOut(ctx ModuleContext, paths ...string) ModuleOutPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700557 p := validatePath(ctx, paths...)
558 return ModuleOutPath{PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir(), p)}
559}
560
561// ModuleGenPath is a Path representing the 'gen' directory in a module's output
562// directory. Mainly used for generated sources.
563type ModuleGenPath struct {
564 ModuleOutPath
565 path string
566}
567
568var _ Path = ModuleGenPath{}
569var _ genPathProvider = ModuleGenPath{}
570var _ objPathProvider = ModuleGenPath{}
571
572// PathForModuleGen returns a Path representing the paths... under the module's
573// `gen' directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700574func PathForModuleGen(ctx ModuleContext, paths ...string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700575 p := validatePath(ctx, paths...)
576 return ModuleGenPath{
577 PathForModuleOut(ctx, "gen", p),
578 p,
579 }
580}
581
Dan Willemsen21ec4902016-11-02 20:43:13 -0700582func (p ModuleGenPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700583 // TODO: make a different path for local vs remote generated files?
Dan Willemsen21ec4902016-11-02 20:43:13 -0700584 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700585}
586
Colin Cross635c3b02016-05-18 15:37:25 -0700587func (p ModuleGenPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700588 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
589}
590
591// ModuleObjPath is a Path representing the 'obj' directory in a module's output
592// directory. Used for compiled objects.
593type ModuleObjPath struct {
594 ModuleOutPath
595}
596
597var _ Path = ModuleObjPath{}
598
599// PathForModuleObj returns a Path representing the paths... under the module's
600// 'obj' directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700601func PathForModuleObj(ctx ModuleContext, paths ...string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700602 p := validatePath(ctx, paths...)
603 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
604}
605
606// ModuleResPath is a a Path representing the 'res' directory in a module's
607// output directory.
608type ModuleResPath struct {
609 ModuleOutPath
610}
611
612var _ Path = ModuleResPath{}
613
614// PathForModuleRes returns a Path representing the paths... under the module's
615// 'res' directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700616func PathForModuleRes(ctx ModuleContext, paths ...string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700617 p := validatePath(ctx, paths...)
618 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
619}
620
621// PathForModuleInstall returns a Path representing the install path for the
622// module appended with paths...
Colin Cross635c3b02016-05-18 15:37:25 -0700623func PathForModuleInstall(ctx ModuleContext, paths ...string) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700624 var outPaths []string
625 if ctx.Device() {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800626 partition := "system"
627 if ctx.Proprietary() {
Dan Willemsen4353bc42016-12-05 17:16:02 -0800628 partition = ctx.DeviceConfig().VendorPath()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800629 }
630 if ctx.InstallInData() {
631 partition = "data"
632 }
633 outPaths = []string{"target", "product", ctx.AConfig().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700634 } else {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700635 outPaths = []string{"host", ctx.Os().String() + "-x86"}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700636 }
Dan Willemsen782a2d12015-12-21 14:55:28 -0800637 if ctx.Debug() {
638 outPaths = append([]string{"debug"}, outPaths...)
639 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700640 outPaths = append(outPaths, paths...)
641 return PathForOutput(ctx, outPaths...)
642}
643
644// validateSafePath validates a path that we trust (may contain ninja variables).
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800645// Ensures that each path component does not attempt to leave its component.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700646func validateSafePath(ctx PathContext, paths ...string) string {
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800647 for _, path := range paths {
648 path := filepath.Clean(path)
649 if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
650 reportPathError(ctx, "Path is outside directory: %s", path)
651 return ""
652 }
653 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700654 // TODO: filepath.Join isn't necessarily correct with embedded ninja
655 // variables. '..' may remove the entire ninja variable, even if it
656 // will be expanded to multiple nested directories.
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800657 return filepath.Join(paths...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700658}
659
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800660// validatePath validates that a path does not include ninja variables, and that
661// each path component does not attempt to leave its component. Returns a joined
662// version of each path component.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700663func validatePath(ctx PathContext, paths ...string) string {
664 for _, path := range paths {
665 if strings.Contains(path, "$") {
666 reportPathError(ctx, "Path contains invalid character($): %s", path)
667 return ""
668 }
669 }
670 return validateSafePath(ctx, paths...)
Colin Cross6e18ca42015-07-14 18:55:36 -0700671}