blob: a23dd74e2b0a9c97cfc0d167f43976fc893b15ba [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
Colin Crossfaeb7aa2017-02-01 14:12:44 -080089
90 // Rel returns the portion of the path relative to the directory it was created from. For
91 // example, Rel on a PathsForModuleSrc would return the path relative to the module source
92 // directory.
93 Rel() string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070094}
95
96// WritablePath is a type of path that can be used as an output for build rules.
97type WritablePath interface {
98 Path
99
Jeff Gaston734e3802017-04-10 15:47:24 -0700100 // the writablePath method doesn't directly do anything,
101 // but it allows a struct to distinguish between whether or not it implements the WritablePath interface
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700102 writablePath()
103}
104
105type genPathProvider interface {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700106 genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700107}
108type objPathProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700109 objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700110}
111type resPathProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700112 resPathWithName(ctx ModuleContext, name string) ModuleResPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700113}
114
115// GenPathWithExt derives a new file path in ctx's generated sources directory
116// from the current path, but with the new extension.
Dan Willemsen21ec4902016-11-02 20:43:13 -0700117func GenPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700118 if path, ok := p.(genPathProvider); ok {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700119 return path.genPathWithExt(ctx, subdir, ext)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700120 }
121 reportPathError(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
122 return PathForModuleGen(ctx)
123}
124
125// ObjPathWithExt derives a new file path in ctx's object directory from the
126// current path, but with the new extension.
Dan Willemsen21ec4902016-11-02 20:43:13 -0700127func ObjPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700128 if path, ok := p.(objPathProvider); ok {
129 return path.objPathWithExt(ctx, subdir, ext)
130 }
131 reportPathError(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
132 return PathForModuleObj(ctx)
133}
134
135// ResPathWithName derives a new path in ctx's output resource directory, using
136// the current path to create the directory name, and the `name` argument for
137// the filename.
Colin Cross635c3b02016-05-18 15:37:25 -0700138func ResPathWithName(ctx ModuleContext, p Path, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700139 if path, ok := p.(resPathProvider); ok {
140 return path.resPathWithName(ctx, name)
141 }
Jeff Gaston734e3802017-04-10 15:47:24 -0700142 reportPathError(ctx, "Tried to create res file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700143 return PathForModuleRes(ctx)
144}
145
146// OptionalPath is a container that may or may not contain a valid Path.
147type OptionalPath struct {
148 valid bool
149 path Path
150}
151
152// OptionalPathForPath returns an OptionalPath containing the path.
153func OptionalPathForPath(path Path) OptionalPath {
154 if path == nil {
155 return OptionalPath{}
156 }
157 return OptionalPath{valid: true, path: path}
158}
159
160// Valid returns whether there is a valid path
161func (p OptionalPath) Valid() bool {
162 return p.valid
163}
164
165// Path returns the Path embedded in this OptionalPath. You must be sure that
166// there is a valid path, since this method will panic if there is not.
167func (p OptionalPath) Path() Path {
168 if !p.valid {
169 panic("Requesting an invalid path")
170 }
171 return p.path
172}
173
174// String returns the string version of the Path, or "" if it isn't valid.
175func (p OptionalPath) String() string {
176 if p.valid {
177 return p.path.String()
178 } else {
179 return ""
Colin Crossf2298272015-05-12 11:36:53 -0700180 }
181}
Colin Cross6e18ca42015-07-14 18:55:36 -0700182
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700183// Paths is a slice of Path objects, with helpers to operate on the collection.
184type Paths []Path
185
186// PathsForSource returns Paths rooted from SrcDir
187func PathsForSource(ctx PathContext, paths []string) Paths {
Dan Willemsene23dfb72016-03-11 15:02:46 -0800188 if pathConfig(ctx).AllowMissingDependencies() {
Colin Cross635c3b02016-05-18 15:37:25 -0700189 if modCtx, ok := ctx.(ModuleContext); ok {
Dan Willemsene23dfb72016-03-11 15:02:46 -0800190 ret := make(Paths, 0, len(paths))
Dan Willemsen0f6042e2016-03-11 17:01:03 -0800191 intermediates := filepath.Join(modCtx.ModuleDir(), modCtx.ModuleName(), modCtx.ModuleSubDir(), "missing")
Dan Willemsene23dfb72016-03-11 15:02:46 -0800192 for _, path := range paths {
Jeff Gaston734e3802017-04-10 15:47:24 -0700193 p := ExistentPathForSource(ctx, intermediates, path)
Dan Willemsene23dfb72016-03-11 15:02:46 -0800194 if p.Valid() {
195 ret = append(ret, p.Path())
196 } else {
197 modCtx.AddMissingDependencies([]string{path})
198 }
199 }
200 return ret
201 }
202 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700203 ret := make(Paths, len(paths))
204 for i, path := range paths {
205 ret[i] = PathForSource(ctx, path)
206 }
207 return ret
208}
209
Jeff Gaston734e3802017-04-10 15:47:24 -0700210// ExistentPathsForSources returns a list of Paths rooted from SrcDir that are
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800211// found in the tree. If any are not found, they are omitted from the list,
212// and dependencies are added so that we're re-run when they are added.
Jeff Gaston734e3802017-04-10 15:47:24 -0700213func ExistentPathsForSources(ctx PathContext, intermediates string, paths []string) Paths {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800214 ret := make(Paths, 0, len(paths))
215 for _, path := range paths {
Jeff Gaston734e3802017-04-10 15:47:24 -0700216 p := ExistentPathForSource(ctx, intermediates, path)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800217 if p.Valid() {
218 ret = append(ret, p.Path())
219 }
220 }
221 return ret
222}
223
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700224// PathsForModuleSrc returns Paths rooted from the module's local source
225// directory
Colin Cross635c3b02016-05-18 15:37:25 -0700226func PathsForModuleSrc(ctx ModuleContext, paths []string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700227 ret := make(Paths, len(paths))
228 for i, path := range paths {
229 ret[i] = PathForModuleSrc(ctx, path)
230 }
231 return ret
232}
233
234// pathsForModuleSrcFromFullPath returns Paths rooted from the module's local
235// source directory, but strip the local source directory from the beginning of
236// each string.
Colin Cross635c3b02016-05-18 15:37:25 -0700237func pathsForModuleSrcFromFullPath(ctx ModuleContext, paths []string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700238 prefix := filepath.Join(ctx.AConfig().srcDir, ctx.ModuleDir()) + "/"
239 ret := make(Paths, 0, len(paths))
240 for _, p := range paths {
241 path := filepath.Clean(p)
242 if !strings.HasPrefix(path, prefix) {
243 reportPathError(ctx, "Path '%s' is not in module source directory '%s'", p, prefix)
244 continue
245 }
246 ret = append(ret, PathForModuleSrc(ctx, path[len(prefix):]))
247 }
248 return ret
249}
250
251// PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's
252// local source directory. If none are provided, use the default if it exists.
Colin Cross635c3b02016-05-18 15:37:25 -0700253func PathsWithOptionalDefaultForModuleSrc(ctx ModuleContext, input []string, def string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700254 if len(input) > 0 {
255 return PathsForModuleSrc(ctx, input)
256 }
257 // Use Glob so that if the default doesn't exist, a dependency is added so that when it
258 // is created, we're run again.
259 path := filepath.Join(ctx.AConfig().srcDir, ctx.ModuleDir(), def)
Colin Cross7f19f372016-11-01 11:10:25 -0700260 return ctx.Glob(path, []string{})
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700261}
262
263// Strings returns the Paths in string form
264func (p Paths) Strings() []string {
265 if p == nil {
266 return nil
267 }
268 ret := make([]string, len(p))
269 for i, path := range p {
270 ret[i] = path.String()
271 }
272 return ret
273}
274
275// WritablePaths is a slice of WritablePaths, used for multiple outputs.
276type WritablePaths []WritablePath
277
278// Strings returns the string forms of the writable paths.
279func (p WritablePaths) Strings() []string {
280 if p == nil {
281 return nil
282 }
283 ret := make([]string, len(p))
284 for i, path := range p {
285 ret[i] = path.String()
286 }
287 return ret
288}
289
290type basePath struct {
291 path string
292 config Config
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800293 rel string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700294}
295
296func (p basePath) Ext() string {
297 return filepath.Ext(p.path)
298}
299
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700300func (p basePath) Base() string {
301 return filepath.Base(p.path)
302}
303
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800304func (p basePath) Rel() string {
305 if p.rel != "" {
306 return p.rel
307 }
308 return p.path
309}
310
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700311// SourcePath is a Path representing a file path rooted from SrcDir
312type SourcePath struct {
313 basePath
314}
315
316var _ Path = SourcePath{}
317
318// safePathForSource is for paths that we expect are safe -- only for use by go
319// code that is embedding ninja variables in paths
320func safePathForSource(ctx PathContext, path string) SourcePath {
321 p := validateSafePath(ctx, path)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800322 ret := SourcePath{basePath{p, pathConfig(ctx), ""}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700323
324 abs, err := filepath.Abs(ret.String())
Colin Cross6e18ca42015-07-14 18:55:36 -0700325 if err != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700326 reportPathError(ctx, "%s", err.Error())
327 return ret
328 }
329 buildroot, err := filepath.Abs(pathConfig(ctx).buildDir)
330 if err != nil {
331 reportPathError(ctx, "%s", err.Error())
332 return ret
333 }
334 if strings.HasPrefix(abs, buildroot) {
335 reportPathError(ctx, "source path %s is in output", abs)
336 return ret
Colin Cross6e18ca42015-07-14 18:55:36 -0700337 }
338
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700339 return ret
340}
341
Jeff Gaston734e3802017-04-10 15:47:24 -0700342// PathForSource joins the provided path components and validates that the result
343// neither escapes the source dir nor is in the out dir.
344// On error, it will return a usable, but invalid SourcePath, and report a ModuleError.
345func PathForSource(ctx PathContext, pathComponents ...string) SourcePath {
346 p := validatePath(ctx, pathComponents...)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800347 ret := SourcePath{basePath{p, pathConfig(ctx), ""}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700348
349 abs, err := filepath.Abs(ret.String())
350 if err != nil {
351 reportPathError(ctx, "%s", err.Error())
352 return ret
353 }
354 buildroot, err := filepath.Abs(pathConfig(ctx).buildDir)
355 if err != nil {
356 reportPathError(ctx, "%s", err.Error())
357 return ret
358 }
359 if strings.HasPrefix(abs, buildroot) {
360 reportPathError(ctx, "source path %s is in output", abs)
361 return ret
362 }
363
Colin Cross294941b2017-02-01 14:10:36 -0800364 if exists, _, err := ctx.Fs().Exists(ret.String()); err != nil {
365 reportPathError(ctx, "%s: %s", ret, err.Error())
366 } else if !exists {
367 reportPathError(ctx, "source path %s does not exist", ret)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700368 }
369 return ret
370}
371
Jeff Gaston734e3802017-04-10 15:47:24 -0700372// ExistentPathForSource returns an OptionalPath with the SourcePath if the
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700373// path exists, or an empty OptionalPath if it doesn't exist. Dependencies are added
374// so that the ninja file will be regenerated if the state of the path changes.
Jeff Gaston734e3802017-04-10 15:47:24 -0700375func ExistentPathForSource(ctx PathContext, intermediates string, pathComponents ...string) OptionalPath {
376 if len(pathComponents) == 0 {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800377 // For when someone forgets the 'intermediates' argument
378 panic("Missing path(s)")
379 }
380
Jeff Gaston734e3802017-04-10 15:47:24 -0700381 p := validatePath(ctx, pathComponents...)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800382 path := SourcePath{basePath{p, pathConfig(ctx), ""}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700383
384 abs, err := filepath.Abs(path.String())
385 if err != nil {
386 reportPathError(ctx, "%s", err.Error())
387 return OptionalPath{}
388 }
389 buildroot, err := filepath.Abs(pathConfig(ctx).buildDir)
390 if err != nil {
391 reportPathError(ctx, "%s", err.Error())
392 return OptionalPath{}
393 }
394 if strings.HasPrefix(abs, buildroot) {
395 reportPathError(ctx, "source path %s is in output", abs)
396 return OptionalPath{}
397 }
398
Colin Cross7f19f372016-11-01 11:10:25 -0700399 if pathtools.IsGlob(path.String()) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800400 reportPathError(ctx, "path may not contain a glob: %s", path.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700401 return OptionalPath{}
402 }
403
Colin Cross7f19f372016-11-01 11:10:25 -0700404 if gctx, ok := ctx.(PathGlobContext); ok {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800405 // Use glob to produce proper dependencies, even though we only want
406 // a single file.
Colin Cross7f19f372016-11-01 11:10:25 -0700407 files, err := gctx.GlobWithDeps(path.String(), nil)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800408 if err != nil {
409 reportPathError(ctx, "glob: %s", err.Error())
410 return OptionalPath{}
411 }
412
413 if len(files) == 0 {
414 return OptionalPath{}
415 }
416 } else {
417 // We cannot add build statements in this context, so we fall back to
418 // AddNinjaFileDeps
Colin Cross294941b2017-02-01 14:10:36 -0800419 files, dirs, err := pathtools.Glob(path.String(), nil)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800420 if err != nil {
421 reportPathError(ctx, "glob: %s", err.Error())
422 return OptionalPath{}
423 }
424
425 ctx.AddNinjaFileDeps(dirs...)
426
427 if len(files) == 0 {
428 return OptionalPath{}
429 }
430
431 ctx.AddNinjaFileDeps(path.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700432 }
433 return OptionalPathForPath(path)
434}
435
436func (p SourcePath) String() string {
437 return filepath.Join(p.config.srcDir, p.path)
438}
439
440// Join creates a new SourcePath with paths... joined with the current path. The
441// provided paths... may not use '..' to escape from the current path.
442func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
443 path := validatePath(ctx, paths...)
444 return PathForSource(ctx, p.path, path)
445}
446
447// OverlayPath returns the overlay for `path' if it exists. This assumes that the
448// SourcePath is the path to a resource overlay directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700449func (p SourcePath) OverlayPath(ctx ModuleContext, path Path) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700450 var relDir string
451 if moduleSrcPath, ok := path.(ModuleSrcPath); ok {
Colin Cross7fc17db2017-02-01 14:07:55 -0800452 relDir = moduleSrcPath.path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700453 } else if srcPath, ok := path.(SourcePath); ok {
454 relDir = srcPath.path
455 } else {
456 reportPathError(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
457 return OptionalPath{}
458 }
459 dir := filepath.Join(p.config.srcDir, p.path, relDir)
460 // Use Glob so that we are run again if the directory is added.
Colin Cross7f19f372016-11-01 11:10:25 -0700461 if pathtools.IsGlob(dir) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800462 reportPathError(ctx, "Path may not contain a glob: %s", dir)
463 }
Colin Cross7f19f372016-11-01 11:10:25 -0700464 paths, err := ctx.GlobWithDeps(dir, []string{})
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700465 if err != nil {
466 reportPathError(ctx, "glob: %s", err.Error())
467 return OptionalPath{}
468 }
469 if len(paths) == 0 {
470 return OptionalPath{}
471 }
472 relPath, err := filepath.Rel(p.config.srcDir, paths[0])
473 if err != nil {
474 reportPathError(ctx, "%s", err.Error())
475 return OptionalPath{}
476 }
477 return OptionalPathForPath(PathForSource(ctx, relPath))
478}
479
480// OutputPath is a Path representing a file path rooted from the build directory
481type OutputPath struct {
482 basePath
483}
484
485var _ Path = OutputPath{}
486
Jeff Gaston734e3802017-04-10 15:47:24 -0700487// PathForOutput joins the provided paths and returns an OutputPath that is
488// validated to not escape the build dir.
489// On error, it will return a usable, but invalid OutputPath, and report a ModuleError.
490func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath {
491 path := validatePath(ctx, pathComponents...)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800492 return OutputPath{basePath{path, pathConfig(ctx), ""}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700493}
494
495func (p OutputPath) writablePath() {}
496
497func (p OutputPath) String() string {
498 return filepath.Join(p.config.buildDir, p.path)
499}
500
Colin Crossa2344662016-03-24 13:14:12 -0700501func (p OutputPath) RelPathString() string {
502 return p.path
503}
504
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700505// Join creates a new OutputPath with paths... joined with the current path. The
506// provided paths... may not use '..' to escape from the current path.
507func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
508 path := validatePath(ctx, paths...)
509 return PathForOutput(ctx, p.path, path)
510}
511
512// PathForIntermediates returns an OutputPath representing the top-level
513// intermediates directory.
514func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
515 path := validatePath(ctx, paths...)
516 return PathForOutput(ctx, ".intermediates", path)
517}
518
519// ModuleSrcPath is a Path representing a file rooted from a module's local source dir
520type ModuleSrcPath struct {
Colin Cross7fc17db2017-02-01 14:07:55 -0800521 SourcePath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700522}
523
524var _ Path = ModuleSrcPath{}
525var _ genPathProvider = ModuleSrcPath{}
526var _ objPathProvider = ModuleSrcPath{}
527var _ resPathProvider = ModuleSrcPath{}
528
529// PathForModuleSrc returns a ModuleSrcPath representing the paths... under the
530// module's local source directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700531func PathForModuleSrc(ctx ModuleContext, paths ...string) ModuleSrcPath {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800532 p := validatePath(ctx, paths...)
533 path := ModuleSrcPath{PathForSource(ctx, ctx.ModuleDir(), p)}
534 path.basePath.rel = p
535 return path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700536}
537
538// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
539// valid path if p is non-nil.
Colin Cross635c3b02016-05-18 15:37:25 -0700540func OptionalPathForModuleSrc(ctx ModuleContext, p *string) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700541 if p == nil {
542 return OptionalPath{}
543 }
544 return OptionalPathForPath(PathForModuleSrc(ctx, *p))
545}
546
Dan Willemsen21ec4902016-11-02 20:43:13 -0700547func (p ModuleSrcPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Colin Cross7fc17db2017-02-01 14:07:55 -0800548 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700549}
550
Colin Cross635c3b02016-05-18 15:37:25 -0700551func (p ModuleSrcPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Colin Cross7fc17db2017-02-01 14:07:55 -0800552 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700553}
554
Colin Cross635c3b02016-05-18 15:37:25 -0700555func (p ModuleSrcPath) resPathWithName(ctx ModuleContext, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700556 // TODO: Use full directory if the new ctx is not the current ctx?
557 return PathForModuleRes(ctx, p.path, name)
558}
559
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800560func (p ModuleSrcPath) WithSubDir(ctx ModuleContext, subdir string) ModuleSrcPath {
561 subdir = PathForModuleSrc(ctx, subdir).String()
562 var err error
563 rel, err := filepath.Rel(subdir, p.path)
564 if err != nil {
565 ctx.ModuleErrorf("source file %q is not under path %q", p.path, subdir)
566 return p
567 }
568 p.rel = rel
569 return p
570}
571
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700572// ModuleOutPath is a Path representing a module's output directory.
573type ModuleOutPath struct {
574 OutputPath
575}
576
577var _ Path = ModuleOutPath{}
578
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800579// PathForVndkRefDump returns an OptionalPath representing the path of the reference
580// abi dump for the given module. This is not guaranteed to be valid.
581func PathForVndkRefAbiDump(ctx ModuleContext, version, fileName string, vndkOrNdk, isSourceDump bool) OptionalPath {
582 archName := ctx.Arch().ArchType.Name
583 var sourceOrBinaryDir string
584 var vndkOrNdkDir string
585 var ext string
586 if isSourceDump {
587 ext = ".lsdump"
588 sourceOrBinaryDir = "source-based"
589 } else {
590 ext = ".bdump"
591 sourceOrBinaryDir = "binary-based"
592 }
593 if vndkOrNdk {
594 vndkOrNdkDir = "vndk"
595 } else {
596 vndkOrNdkDir = "ndk"
597 }
598 refDumpFileStr := "prebuilts/abi-dumps/" + vndkOrNdkDir + "/" + version + "/" +
599 archName + "/" + sourceOrBinaryDir + "/" + fileName + ext
Jeff Gaston734e3802017-04-10 15:47:24 -0700600 return ExistentPathForSource(ctx, "", refDumpFileStr)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800601}
602
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700603// PathForModuleOut returns a Path representing the paths... under the module's
604// output directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700605func PathForModuleOut(ctx ModuleContext, paths ...string) ModuleOutPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700606 p := validatePath(ctx, paths...)
607 return ModuleOutPath{PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir(), p)}
608}
609
610// ModuleGenPath is a Path representing the 'gen' directory in a module's output
611// directory. Mainly used for generated sources.
612type ModuleGenPath struct {
613 ModuleOutPath
614 path string
615}
616
617var _ Path = ModuleGenPath{}
618var _ genPathProvider = ModuleGenPath{}
619var _ objPathProvider = ModuleGenPath{}
620
621// PathForModuleGen returns a Path representing the paths... under the module's
622// `gen' directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700623func PathForModuleGen(ctx ModuleContext, paths ...string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700624 p := validatePath(ctx, paths...)
625 return ModuleGenPath{
626 PathForModuleOut(ctx, "gen", p),
627 p,
628 }
629}
630
Dan Willemsen21ec4902016-11-02 20:43:13 -0700631func (p ModuleGenPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700632 // TODO: make a different path for local vs remote generated files?
Dan Willemsen21ec4902016-11-02 20:43:13 -0700633 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700634}
635
Colin Cross635c3b02016-05-18 15:37:25 -0700636func (p ModuleGenPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700637 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
638}
639
640// ModuleObjPath is a Path representing the 'obj' directory in a module's output
641// directory. Used for compiled objects.
642type ModuleObjPath struct {
643 ModuleOutPath
644}
645
646var _ Path = ModuleObjPath{}
647
648// PathForModuleObj returns a Path representing the paths... under the module's
649// 'obj' directory.
Jeff Gaston734e3802017-04-10 15:47:24 -0700650func PathForModuleObj(ctx ModuleContext, pathComponents ...string) ModuleObjPath {
651 p := validatePath(ctx, pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700652 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
653}
654
655// ModuleResPath is a a Path representing the 'res' directory in a module's
656// output directory.
657type ModuleResPath struct {
658 ModuleOutPath
659}
660
661var _ Path = ModuleResPath{}
662
663// PathForModuleRes returns a Path representing the paths... under the module's
664// 'res' directory.
Jeff Gaston734e3802017-04-10 15:47:24 -0700665func PathForModuleRes(ctx ModuleContext, pathComponents ...string) ModuleResPath {
666 p := validatePath(ctx, pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700667 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
668}
669
670// PathForModuleInstall returns a Path representing the install path for the
671// module appended with paths...
Jeff Gaston734e3802017-04-10 15:47:24 -0700672func PathForModuleInstall(ctx ModuleContext, pathComponents ...string) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700673 var outPaths []string
674 if ctx.Device() {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800675 partition := "system"
Dan Willemsenaa118f92017-04-06 12:49:58 -0700676 if ctx.Vendor() {
Dan Willemsen4353bc42016-12-05 17:16:02 -0800677 partition = ctx.DeviceConfig().VendorPath()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800678 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700679
680 if ctx.InstallInSanitizerDir() {
681 partition = "data/asan/" + partition
682 } else if ctx.InstallInData() {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800683 partition = "data"
684 }
685 outPaths = []string{"target", "product", ctx.AConfig().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700686 } else {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700687 outPaths = []string{"host", ctx.Os().String() + "-x86"}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700688 }
Dan Willemsen782a2d12015-12-21 14:55:28 -0800689 if ctx.Debug() {
690 outPaths = append([]string{"debug"}, outPaths...)
691 }
Jeff Gaston734e3802017-04-10 15:47:24 -0700692 outPaths = append(outPaths, pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700693 return PathForOutput(ctx, outPaths...)
694}
695
696// validateSafePath validates a path that we trust (may contain ninja variables).
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800697// Ensures that each path component does not attempt to leave its component.
Jeff Gaston734e3802017-04-10 15:47:24 -0700698func validateSafePath(ctx PathContext, pathComponents ...string) string {
699 for _, path := range pathComponents {
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800700 path := filepath.Clean(path)
701 if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
702 reportPathError(ctx, "Path is outside directory: %s", path)
703 return ""
704 }
705 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700706 // TODO: filepath.Join isn't necessarily correct with embedded ninja
707 // variables. '..' may remove the entire ninja variable, even if it
708 // will be expanded to multiple nested directories.
Jeff Gaston734e3802017-04-10 15:47:24 -0700709 return filepath.Join(pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700710}
711
Dan Willemsen80a7c2a2015-12-21 14:57:11 -0800712// validatePath validates that a path does not include ninja variables, and that
713// each path component does not attempt to leave its component. Returns a joined
714// version of each path component.
Jeff Gaston734e3802017-04-10 15:47:24 -0700715func validatePath(ctx PathContext, pathComponents ...string) string {
716 for _, path := range pathComponents {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700717 if strings.Contains(path, "$") {
718 reportPathError(ctx, "Path contains invalid character($): %s", path)
719 return ""
720 }
721 }
Jeff Gaston734e3802017-04-10 15:47:24 -0700722 return validateSafePath(ctx, pathComponents...)
Colin Cross6e18ca42015-07-14 18:55:36 -0700723}
Colin Cross5b529592017-05-09 13:34:34 -0700724
725type testPath struct {
726 basePath
727}
728
729func (p testPath) String() string {
730 return p.path
731}
732
733func PathForTesting(paths ...string) Path {
734 p := validateSafePath(nil, paths...)
735 return testPath{basePath{path: p, rel: p}}
736}
737
738func PathsForTesting(strs []string) Paths {
739 p := make(Paths, len(strs))
740 for i, s := range strs {
741 p[i] = PathForTesting(s)
742 }
743
744 return p
745}