blob: ac7d81e62b69d7e01bdc07778469ff1814097e18 [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"
21 "strings"
22
23 "github.com/google/blueprint"
24 "github.com/google/blueprint/pathtools"
Colin Cross3f40fa42015-01-30 17:27:36 -080025)
26
Dan Willemsen34cc69e2015-09-23 15:26:20 -070027// PathContext is the subset of a (Module|Singleton)Context required by the
28// Path methods.
29type PathContext interface {
Colin Cross294941b2017-02-01 14:10:36 -080030 Fs() pathtools.FileSystem
Dan Willemsen34cc69e2015-09-23 15:26:20 -070031 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
Colin Cross294941b2017-02-01 14:10:36 -0800350 if exists, _, err := ctx.Fs().Exists(ret.String()); err != nil {
351 reportPathError(ctx, "%s: %s", ret, err.Error())
352 } else if !exists {
353 reportPathError(ctx, "source path %s does not exist", ret)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700354 }
355 return ret
356}
357
358// OptionalPathForSource returns an OptionalPath with the SourcePath if the
359// path exists, or an empty OptionalPath if it doesn't exist. Dependencies are added
360// so that the ninja file will be regenerated if the state of the path changes.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800361func OptionalPathForSource(ctx PathContext, intermediates string, paths ...string) OptionalPath {
362 if len(paths) == 0 {
363 // For when someone forgets the 'intermediates' argument
364 panic("Missing path(s)")
365 }
366
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700367 p := validatePath(ctx, paths...)
368 path := SourcePath{basePath{p, pathConfig(ctx)}}
369
370 abs, err := filepath.Abs(path.String())
371 if err != nil {
372 reportPathError(ctx, "%s", err.Error())
373 return OptionalPath{}
374 }
375 buildroot, err := filepath.Abs(pathConfig(ctx).buildDir)
376 if err != nil {
377 reportPathError(ctx, "%s", err.Error())
378 return OptionalPath{}
379 }
380 if strings.HasPrefix(abs, buildroot) {
381 reportPathError(ctx, "source path %s is in output", abs)
382 return OptionalPath{}
383 }
384
Colin Cross7f19f372016-11-01 11:10:25 -0700385 if pathtools.IsGlob(path.String()) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800386 reportPathError(ctx, "path may not contain a glob: %s", path.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700387 return OptionalPath{}
388 }
389
Colin Cross7f19f372016-11-01 11:10:25 -0700390 if gctx, ok := ctx.(PathGlobContext); ok {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800391 // Use glob to produce proper dependencies, even though we only want
392 // a single file.
Colin Cross7f19f372016-11-01 11:10:25 -0700393 files, err := gctx.GlobWithDeps(path.String(), nil)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800394 if err != nil {
395 reportPathError(ctx, "glob: %s", err.Error())
396 return OptionalPath{}
397 }
398
399 if len(files) == 0 {
400 return OptionalPath{}
401 }
402 } else {
403 // We cannot add build statements in this context, so we fall back to
404 // AddNinjaFileDeps
Colin Cross294941b2017-02-01 14:10:36 -0800405 files, dirs, err := pathtools.Glob(path.String(), nil)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800406 if err != nil {
407 reportPathError(ctx, "glob: %s", err.Error())
408 return OptionalPath{}
409 }
410
411 ctx.AddNinjaFileDeps(dirs...)
412
413 if len(files) == 0 {
414 return OptionalPath{}
415 }
416
417 ctx.AddNinjaFileDeps(path.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700418 }
419 return OptionalPathForPath(path)
420}
421
422func (p SourcePath) String() string {
423 return filepath.Join(p.config.srcDir, p.path)
424}
425
426// Join creates a new SourcePath with paths... joined with the current path. The
427// provided paths... may not use '..' to escape from the current path.
428func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
429 path := validatePath(ctx, paths...)
430 return PathForSource(ctx, p.path, path)
431}
432
433// OverlayPath returns the overlay for `path' if it exists. This assumes that the
434// SourcePath is the path to a resource overlay directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700435func (p SourcePath) OverlayPath(ctx ModuleContext, path Path) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700436 var relDir string
437 if moduleSrcPath, ok := path.(ModuleSrcPath); ok {
Colin Cross7fc17db2017-02-01 14:07:55 -0800438 relDir = moduleSrcPath.path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700439 } else if srcPath, ok := path.(SourcePath); ok {
440 relDir = srcPath.path
441 } else {
442 reportPathError(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
443 return OptionalPath{}
444 }
445 dir := filepath.Join(p.config.srcDir, p.path, relDir)
446 // Use Glob so that we are run again if the directory is added.
Colin Cross7f19f372016-11-01 11:10:25 -0700447 if pathtools.IsGlob(dir) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800448 reportPathError(ctx, "Path may not contain a glob: %s", dir)
449 }
Colin Cross7f19f372016-11-01 11:10:25 -0700450 paths, err := ctx.GlobWithDeps(dir, []string{})
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700451 if err != nil {
452 reportPathError(ctx, "glob: %s", err.Error())
453 return OptionalPath{}
454 }
455 if len(paths) == 0 {
456 return OptionalPath{}
457 }
458 relPath, err := filepath.Rel(p.config.srcDir, paths[0])
459 if err != nil {
460 reportPathError(ctx, "%s", err.Error())
461 return OptionalPath{}
462 }
463 return OptionalPathForPath(PathForSource(ctx, relPath))
464}
465
466// OutputPath is a Path representing a file path rooted from the build directory
467type OutputPath struct {
468 basePath
469}
470
471var _ Path = OutputPath{}
472
473// PathForOutput returns an OutputPath for the provided paths... (which are
474// joined together with filepath.Join). This also validates that the path
475// does not escape the build dir. On error, it will return a usable, but invalid
476// OutputPath, and report a ModuleError.
477func PathForOutput(ctx PathContext, paths ...string) OutputPath {
478 path := validatePath(ctx, paths...)
479 return OutputPath{basePath{path, pathConfig(ctx)}}
480}
481
482func (p OutputPath) writablePath() {}
483
484func (p OutputPath) String() string {
485 return filepath.Join(p.config.buildDir, p.path)
486}
487
Colin Crossa2344662016-03-24 13:14:12 -0700488func (p OutputPath) RelPathString() string {
489 return p.path
490}
491
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700492// Join creates a new OutputPath with paths... joined with the current path. The
493// provided paths... may not use '..' to escape from the current path.
494func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
495 path := validatePath(ctx, paths...)
496 return PathForOutput(ctx, p.path, path)
497}
498
499// PathForIntermediates returns an OutputPath representing the top-level
500// intermediates directory.
501func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
502 path := validatePath(ctx, paths...)
503 return PathForOutput(ctx, ".intermediates", path)
504}
505
506// ModuleSrcPath is a Path representing a file rooted from a module's local source dir
507type ModuleSrcPath struct {
Colin Cross7fc17db2017-02-01 14:07:55 -0800508 SourcePath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700509}
510
511var _ Path = ModuleSrcPath{}
512var _ genPathProvider = ModuleSrcPath{}
513var _ objPathProvider = ModuleSrcPath{}
514var _ resPathProvider = ModuleSrcPath{}
515
516// PathForModuleSrc returns a ModuleSrcPath representing the paths... under the
517// module's local source directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700518func PathForModuleSrc(ctx ModuleContext, paths ...string) ModuleSrcPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700519 path := validatePath(ctx, paths...)
Colin Cross7fc17db2017-02-01 14:07:55 -0800520 return ModuleSrcPath{PathForSource(ctx, ctx.ModuleDir(), path)}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700521}
522
523// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
524// valid path if p is non-nil.
Colin Cross635c3b02016-05-18 15:37:25 -0700525func OptionalPathForModuleSrc(ctx ModuleContext, p *string) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700526 if p == nil {
527 return OptionalPath{}
528 }
529 return OptionalPathForPath(PathForModuleSrc(ctx, *p))
530}
531
Dan Willemsen21ec4902016-11-02 20:43:13 -0700532func (p ModuleSrcPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Colin Cross7fc17db2017-02-01 14:07:55 -0800533 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700534}
535
Colin Cross635c3b02016-05-18 15:37:25 -0700536func (p ModuleSrcPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Colin Cross7fc17db2017-02-01 14:07:55 -0800537 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700538}
539
Colin Cross635c3b02016-05-18 15:37:25 -0700540func (p ModuleSrcPath) resPathWithName(ctx ModuleContext, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700541 // TODO: Use full directory if the new ctx is not the current ctx?
542 return PathForModuleRes(ctx, p.path, name)
543}
544
545// ModuleOutPath is a Path representing a module's output directory.
546type ModuleOutPath struct {
547 OutputPath
548}
549
550var _ Path = ModuleOutPath{}
551
552// PathForModuleOut returns a Path representing the paths... under the module's
553// output directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700554func PathForModuleOut(ctx ModuleContext, paths ...string) ModuleOutPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700555 p := validatePath(ctx, paths...)
556 return ModuleOutPath{PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir(), p)}
557}
558
559// ModuleGenPath is a Path representing the 'gen' directory in a module's output
560// directory. Mainly used for generated sources.
561type ModuleGenPath struct {
562 ModuleOutPath
563 path string
564}
565
566var _ Path = ModuleGenPath{}
567var _ genPathProvider = ModuleGenPath{}
568var _ objPathProvider = ModuleGenPath{}
569
570// PathForModuleGen returns a Path representing the paths... under the module's
571// `gen' directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700572func PathForModuleGen(ctx ModuleContext, paths ...string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700573 p := validatePath(ctx, paths...)
574 return ModuleGenPath{
575 PathForModuleOut(ctx, "gen", p),
576 p,
577 }
578}
579
Dan Willemsen21ec4902016-11-02 20:43:13 -0700580func (p ModuleGenPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700581 // TODO: make a different path for local vs remote generated files?
Dan Willemsen21ec4902016-11-02 20:43:13 -0700582 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700583}
584
Colin Cross635c3b02016-05-18 15:37:25 -0700585func (p ModuleGenPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700586 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
587}
588
589// ModuleObjPath is a Path representing the 'obj' directory in a module's output
590// directory. Used for compiled objects.
591type ModuleObjPath struct {
592 ModuleOutPath
593}
594
595var _ Path = ModuleObjPath{}
596
597// PathForModuleObj returns a Path representing the paths... under the module's
598// 'obj' directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700599func PathForModuleObj(ctx ModuleContext, paths ...string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700600 p := validatePath(ctx, paths...)
601 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
602}
603
604// ModuleResPath is a a Path representing the 'res' directory in a module's
605// output directory.
606type ModuleResPath struct {
607 ModuleOutPath
608}
609
610var _ Path = ModuleResPath{}
611
612// PathForModuleRes returns a Path representing the paths... under the module's
613// 'res' directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700614func PathForModuleRes(ctx ModuleContext, paths ...string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700615 p := validatePath(ctx, paths...)
616 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
617}
618
619// PathForModuleInstall returns a Path representing the install path for the
620// module appended with paths...
Colin Cross635c3b02016-05-18 15:37:25 -0700621func PathForModuleInstall(ctx ModuleContext, paths ...string) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700622 var outPaths []string
623 if ctx.Device() {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800624 partition := "system"
625 if ctx.Proprietary() {
Dan Willemsen4353bc42016-12-05 17:16:02 -0800626 partition = ctx.DeviceConfig().VendorPath()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800627 }
628 if ctx.InstallInData() {
629 partition = "data"
630 }
631 outPaths = []string{"target", "product", ctx.AConfig().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700632 } else {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700633 outPaths = []string{"host", ctx.Os().String() + "-x86"}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700634 }
Dan Willemsen782a2d12015-12-21 14:55:28 -0800635 if ctx.Debug() {
636 outPaths = append([]string{"debug"}, outPaths...)
637 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700638 outPaths = append(outPaths, paths...)
639 return PathForOutput(ctx, outPaths...)
640}
641
642// validateSafePath validates a path that we trust (may contain ninja variables).
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800643// Ensures that each path component does not attempt to leave its component.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700644func validateSafePath(ctx PathContext, paths ...string) string {
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800645 for _, path := range paths {
646 path := filepath.Clean(path)
647 if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
648 reportPathError(ctx, "Path is outside directory: %s", path)
649 return ""
650 }
651 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700652 // TODO: filepath.Join isn't necessarily correct with embedded ninja
653 // variables. '..' may remove the entire ninja variable, even if it
654 // will be expanded to multiple nested directories.
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800655 return filepath.Join(paths...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700656}
657
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800658// validatePath validates that a path does not include ninja variables, and that
659// each path component does not attempt to leave its component. Returns a joined
660// version of each path component.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700661func validatePath(ctx PathContext, paths ...string) string {
662 for _, path := range paths {
663 if strings.Contains(path, "$") {
664 reportPathError(ctx, "Path contains invalid character($): %s", path)
665 return ""
666 }
667 }
668 return validateSafePath(ctx, paths...)
Colin Cross6e18ca42015-07-14 18:55:36 -0700669}