blob: ada420d21fa569e718bda8db7ecb9903159bc158 [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 Cross988414c2020-01-11 01:11:46 +000019 "io/ioutil"
20 "os"
Colin Cross6a745c62015-06-16 16:38:10 -070021 "path/filepath"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070022 "reflect"
Colin Cross5e6cfbe2017-11-03 15:20:35 -070023 "sort"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070024 "strings"
25
26 "github.com/google/blueprint"
27 "github.com/google/blueprint/pathtools"
Colin Cross3f40fa42015-01-30 17:27:36 -080028)
29
Colin Cross988414c2020-01-11 01:11:46 +000030var absSrcDir string
31
Dan Willemsen34cc69e2015-09-23 15:26:20 -070032// PathContext is the subset of a (Module|Singleton)Context required by the
33// Path methods.
34type PathContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -080035 Config() Config
Dan Willemsen7b310ee2015-12-18 15:11:17 -080036 AddNinjaFileDeps(deps ...string)
Colin Cross3f40fa42015-01-30 17:27:36 -080037}
38
Colin Cross7f19f372016-11-01 11:10:25 -070039type PathGlobContext interface {
40 GlobWithDeps(globPattern string, excludes []string) ([]string, error)
41}
42
Colin Crossaabf6792017-11-29 00:27:14 -080043var _ PathContext = SingletonContext(nil)
44var _ PathContext = ModuleContext(nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -070045
Ulya Trafimovich8640ab92020-05-11 18:06:15 +010046// "Null" path context is a minimal path context for a given config.
47type NullPathContext struct {
48 config Config
49}
50
51func (NullPathContext) AddNinjaFileDeps(...string) {}
52func (ctx NullPathContext) Config() Config { return ctx.config }
53
Liz Kammera830f3a2020-11-10 10:50:34 -080054// EarlyModulePathContext is a subset of EarlyModuleContext methods required by the
55// Path methods. These path methods can be called before any mutators have run.
56type EarlyModulePathContext interface {
57 PathContext
58 PathGlobContext
59
60 ModuleDir() string
61 ModuleErrorf(fmt string, args ...interface{})
62}
63
64var _ EarlyModulePathContext = ModuleContext(nil)
65
66// Glob globs files and directories matching globPattern relative to ModuleDir(),
67// paths in the excludes parameter will be omitted.
68func Glob(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths {
69 ret, err := ctx.GlobWithDeps(globPattern, excludes)
70 if err != nil {
71 ctx.ModuleErrorf("glob: %s", err.Error())
72 }
73 return pathsForModuleSrcFromFullPath(ctx, ret, true)
74}
75
76// GlobFiles globs *only* files (not directories) matching globPattern relative to ModuleDir().
77// Paths in the excludes parameter will be omitted.
78func GlobFiles(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths {
79 ret, err := ctx.GlobWithDeps(globPattern, excludes)
80 if err != nil {
81 ctx.ModuleErrorf("glob: %s", err.Error())
82 }
83 return pathsForModuleSrcFromFullPath(ctx, ret, false)
84}
85
86// ModuleWithDepsPathContext is a subset of *ModuleContext methods required by
87// the Path methods that rely on module dependencies having been resolved.
88type ModuleWithDepsPathContext interface {
89 EarlyModulePathContext
90 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
91}
92
93// ModuleMissingDepsPathContext is a subset of *ModuleContext methods required by
94// the Path methods that rely on module dependencies having been resolved and ability to report
95// missing dependency errors.
96type ModuleMissingDepsPathContext interface {
97 ModuleWithDepsPathContext
98 AddMissingDependencies(missingDeps []string)
99}
100
Dan Willemsen00269f22017-07-06 16:59:48 -0700101type ModuleInstallPathContext interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700102 BaseModuleContext
Dan Willemsen00269f22017-07-06 16:59:48 -0700103
104 InstallInData() bool
Jaewoong Jung0949f312019-09-11 10:25:18 -0700105 InstallInTestcases() bool
Dan Willemsen00269f22017-07-06 16:59:48 -0700106 InstallInSanitizerDir() bool
Yifan Hong1b3348d2020-01-21 15:53:22 -0800107 InstallInRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700108 InstallInVendorRamdisk() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900109 InstallInRecovery() bool
Colin Cross90ba5f42019-10-02 11:10:58 -0700110 InstallInRoot() bool
Colin Cross607d8582019-07-29 16:44:46 -0700111 InstallBypassMake() bool
Jiyong Park87788b52020-09-01 12:37:45 +0900112 InstallForceOS() (*OsType, *ArchType)
Dan Willemsen00269f22017-07-06 16:59:48 -0700113}
114
115var _ ModuleInstallPathContext = ModuleContext(nil)
116
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700117// errorfContext is the interface containing the Errorf method matching the
118// Errorf method in blueprint.SingletonContext.
119type errorfContext interface {
120 Errorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -0800121}
122
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700123var _ errorfContext = blueprint.SingletonContext(nil)
124
125// moduleErrorf is the interface containing the ModuleErrorf method matching
126// the ModuleErrorf method in blueprint.ModuleContext.
127type moduleErrorf interface {
128 ModuleErrorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -0800129}
130
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700131var _ moduleErrorf = blueprint.ModuleContext(nil)
132
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700133// reportPathError will register an error with the attached context. It
134// attempts ctx.ModuleErrorf for a better error message first, then falls
135// back to ctx.Errorf.
Colin Cross1ccfcc32018-02-22 13:54:26 -0800136func reportPathError(ctx PathContext, err error) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100137 ReportPathErrorf(ctx, "%s", err.Error())
Colin Cross1ccfcc32018-02-22 13:54:26 -0800138}
139
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100140// ReportPathErrorf will register an error with the attached context. It
Colin Cross1ccfcc32018-02-22 13:54:26 -0800141// attempts ctx.ModuleErrorf for a better error message first, then falls
142// back to ctx.Errorf.
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100143func ReportPathErrorf(ctx PathContext, format string, args ...interface{}) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700144 if mctx, ok := ctx.(moduleErrorf); ok {
145 mctx.ModuleErrorf(format, args...)
146 } else if ectx, ok := ctx.(errorfContext); ok {
147 ectx.Errorf(format, args...)
148 } else {
149 panic(fmt.Sprintf(format, args...))
Colin Crossf2298272015-05-12 11:36:53 -0700150 }
151}
152
Colin Cross5e708052019-08-06 13:59:50 -0700153func pathContextName(ctx PathContext, module blueprint.Module) string {
154 if x, ok := ctx.(interface{ ModuleName(blueprint.Module) string }); ok {
155 return x.ModuleName(module)
156 } else if x, ok := ctx.(interface{ OtherModuleName(blueprint.Module) string }); ok {
157 return x.OtherModuleName(module)
158 }
159 return "unknown"
160}
161
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700162type Path interface {
163 // Returns the path in string form
164 String() string
165
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700166 // Ext returns the extension of the last element of the path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700167 Ext() string
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700168
169 // Base returns the last element of the path
170 Base() string
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800171
172 // Rel returns the portion of the path relative to the directory it was created from. For
173 // example, Rel on a PathsForModuleSrc would return the path relative to the module source
Colin Cross0db55682017-12-05 15:36:55 -0800174 // directory, and OutputPath.Join("foo").Rel() would return "foo".
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800175 Rel() string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700176}
177
178// WritablePath is a type of path that can be used as an output for build rules.
179type WritablePath interface {
180 Path
181
Paul Duffin9b478b02019-12-10 13:41:51 +0000182 // return the path to the build directory.
183 buildDir() string
184
Jeff Gaston734e3802017-04-10 15:47:24 -0700185 // the writablePath method doesn't directly do anything,
186 // but it allows a struct to distinguish between whether or not it implements the WritablePath interface
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700187 writablePath()
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +0100188
189 ReplaceExtension(ctx PathContext, ext string) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700190}
191
192type genPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800193 genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700194}
195type objPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800196 objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700197}
198type resPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800199 resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700200}
201
202// GenPathWithExt derives a new file path in ctx's generated sources directory
203// from the current path, but with the new extension.
Liz Kammera830f3a2020-11-10 10:50:34 -0800204func GenPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700205 if path, ok := p.(genPathProvider); ok {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700206 return path.genPathWithExt(ctx, subdir, ext)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700207 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100208 ReportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700209 return PathForModuleGen(ctx)
210}
211
212// ObjPathWithExt derives a new file path in ctx's object directory from the
213// current path, but with the new extension.
Liz Kammera830f3a2020-11-10 10:50:34 -0800214func ObjPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700215 if path, ok := p.(objPathProvider); ok {
216 return path.objPathWithExt(ctx, subdir, ext)
217 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100218 ReportPathErrorf(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700219 return PathForModuleObj(ctx)
220}
221
222// ResPathWithName derives a new path in ctx's output resource directory, using
223// the current path to create the directory name, and the `name` argument for
224// the filename.
Liz Kammera830f3a2020-11-10 10:50:34 -0800225func ResPathWithName(ctx ModuleOutPathContext, p Path, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700226 if path, ok := p.(resPathProvider); ok {
227 return path.resPathWithName(ctx, name)
228 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100229 ReportPathErrorf(ctx, "Tried to create res file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700230 return PathForModuleRes(ctx)
231}
232
233// OptionalPath is a container that may or may not contain a valid Path.
234type OptionalPath struct {
235 valid bool
236 path Path
237}
238
239// OptionalPathForPath returns an OptionalPath containing the path.
240func OptionalPathForPath(path Path) OptionalPath {
241 if path == nil {
242 return OptionalPath{}
243 }
244 return OptionalPath{valid: true, path: path}
245}
246
247// Valid returns whether there is a valid path
248func (p OptionalPath) Valid() bool {
249 return p.valid
250}
251
252// Path returns the Path embedded in this OptionalPath. You must be sure that
253// there is a valid path, since this method will panic if there is not.
254func (p OptionalPath) Path() Path {
255 if !p.valid {
256 panic("Requesting an invalid path")
257 }
258 return p.path
259}
260
261// String returns the string version of the Path, or "" if it isn't valid.
262func (p OptionalPath) String() string {
263 if p.valid {
264 return p.path.String()
265 } else {
266 return ""
Colin Crossf2298272015-05-12 11:36:53 -0700267 }
268}
Colin Cross6e18ca42015-07-14 18:55:36 -0700269
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700270// Paths is a slice of Path objects, with helpers to operate on the collection.
271type Paths []Path
272
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000273func (paths Paths) containsPath(path Path) bool {
274 for _, p := range paths {
275 if p == path {
276 return true
277 }
278 }
279 return false
280}
281
Liz Kammer7aa52882021-02-11 09:16:14 -0500282// PathsForSource returns Paths rooted from SrcDir, *not* rooted from the module's local source
283// directory
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700284func PathsForSource(ctx PathContext, paths []string) Paths {
285 ret := make(Paths, len(paths))
286 for i, path := range paths {
287 ret[i] = PathForSource(ctx, path)
288 }
289 return ret
290}
291
Liz Kammer7aa52882021-02-11 09:16:14 -0500292// ExistentPathsForSources returns a list of Paths rooted from SrcDir, *not* rooted from the
293// module's local source directory, that are found in the tree. If any are not found, they are
294// omitted from the list, and dependencies are added so that we're re-run when they are added.
Colin Cross32f38982018-02-22 11:47:25 -0800295func ExistentPathsForSources(ctx PathContext, paths []string) Paths {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800296 ret := make(Paths, 0, len(paths))
297 for _, path := range paths {
Colin Cross32f38982018-02-22 11:47:25 -0800298 p := ExistentPathForSource(ctx, path)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800299 if p.Valid() {
300 ret = append(ret, p.Path())
301 }
302 }
303 return ret
304}
305
Colin Cross41955e82019-05-29 14:40:35 -0700306// PathsForModuleSrc returns Paths rooted from the module's local source directory. It expands globs, references to
307// SourceFileProducer modules using the ":name" syntax, and references to OutputFileProducer modules using the
308// ":name{.tag}" syntax. Properties passed as the paths argument must have been annotated with struct tag
309// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
310// path_properties mutator. If ctx.Config().AllowMissingDependencies() is true then any missing SourceFileProducer or
311// OutputFileProducer dependencies will cause the module to be marked as having missing dependencies.
Liz Kammera830f3a2020-11-10 10:50:34 -0800312func PathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string) Paths {
Colin Cross8a497952019-03-05 22:25:09 -0800313 return PathsForModuleSrcExcludes(ctx, paths, nil)
314}
315
Colin Crossba71a3f2019-03-18 12:12:48 -0700316// PathsForModuleSrcExcludes returns Paths rooted from the module's local source directory, excluding paths listed in
Colin Cross41955e82019-05-29 14:40:35 -0700317// the excludes arguments. It expands globs, references to SourceFileProducer modules using the ":name" syntax, and
318// references to OutputFileProducer modules using the ":name{.tag}" syntax. Properties passed as the paths or excludes
319// argument must have been annotated with struct tag `android:"path"` so that dependencies on SourceFileProducer modules
320// will have already been handled by the path_properties mutator. If ctx.Config().AllowMissingDependencies() is
Paul Duffin036cace2019-07-25 14:44:56 +0100321// true then any missing SourceFileProducer or OutputFileProducer dependencies will cause the module to be marked as
Colin Cross41955e82019-05-29 14:40:35 -0700322// having missing dependencies.
Liz Kammera830f3a2020-11-10 10:50:34 -0800323func PathsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) Paths {
Colin Crossba71a3f2019-03-18 12:12:48 -0700324 ret, missingDeps := PathsAndMissingDepsForModuleSrcExcludes(ctx, paths, excludes)
325 if ctx.Config().AllowMissingDependencies() {
326 ctx.AddMissingDependencies(missingDeps)
327 } else {
328 for _, m := range missingDeps {
329 ctx.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m)
330 }
331 }
332 return ret
333}
334
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000335// OutputPaths is a slice of OutputPath objects, with helpers to operate on the collection.
336type OutputPaths []OutputPath
337
338// Paths returns the OutputPaths as a Paths
339func (p OutputPaths) Paths() Paths {
340 if p == nil {
341 return nil
342 }
343 ret := make(Paths, len(p))
344 for i, path := range p {
345 ret[i] = path
346 }
347 return ret
348}
349
350// Strings returns the string forms of the writable paths.
351func (p OutputPaths) Strings() []string {
352 if p == nil {
353 return nil
354 }
355 ret := make([]string, len(p))
356 for i, path := range p {
357 ret[i] = path.String()
358 }
359 return ret
360}
361
Liz Kammera830f3a2020-11-10 10:50:34 -0800362// Expands Paths to a SourceFileProducer or OutputFileProducer module dependency referenced via ":name" or ":name{.tag}" syntax.
363// If the dependency is not found, a missingErrorDependency is returned.
364// If the module dependency is not a SourceFileProducer or OutputFileProducer, appropriate errors will be returned.
365func getPathsFromModuleDep(ctx ModuleWithDepsPathContext, path, moduleName, tag string) (Paths, error) {
366 module := ctx.GetDirectDepWithTag(moduleName, sourceOrOutputDepTag(tag))
367 if module == nil {
368 return nil, missingDependencyError{[]string{moduleName}}
369 }
370 if outProducer, ok := module.(OutputFileProducer); ok {
371 outputFiles, err := outProducer.OutputFiles(tag)
372 if err != nil {
373 return nil, fmt.Errorf("path dependency %q: %s", path, err)
374 }
375 return outputFiles, nil
376 } else if tag != "" {
377 return nil, fmt.Errorf("path dependency %q is not an output file producing module", path)
378 } else if srcProducer, ok := module.(SourceFileProducer); ok {
379 return srcProducer.Srcs(), nil
380 } else {
381 return nil, fmt.Errorf("path dependency %q is not a source file producing module", path)
382 }
383}
384
Colin Crossba71a3f2019-03-18 12:12:48 -0700385// PathsAndMissingDepsForModuleSrcExcludes returns Paths rooted from the module's local source directory, excluding
Colin Cross41955e82019-05-29 14:40:35 -0700386// paths listed in the excludes arguments, and a list of missing dependencies. It expands globs, references to
387// SourceFileProducer modules using the ":name" syntax, and references to OutputFileProducer modules using the
388// ":name{.tag}" syntax. Properties passed as the paths or excludes argument must have been annotated with struct tag
389// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
390// path_properties mutator. If ctx.Config().AllowMissingDependencies() is true then any missing SourceFileProducer or
391// OutputFileProducer dependencies will be returned, and they will NOT cause the module to be marked as having missing
392// dependencies.
Liz Kammera830f3a2020-11-10 10:50:34 -0800393func PathsAndMissingDepsForModuleSrcExcludes(ctx ModuleWithDepsPathContext, paths, excludes []string) (Paths, []string) {
Colin Cross8a497952019-03-05 22:25:09 -0800394 prefix := pathForModuleSrc(ctx).String()
395
396 var expandedExcludes []string
397 if excludes != nil {
398 expandedExcludes = make([]string, 0, len(excludes))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700399 }
Colin Cross8a497952019-03-05 22:25:09 -0800400
Colin Crossba71a3f2019-03-18 12:12:48 -0700401 var missingExcludeDeps []string
402
Colin Cross8a497952019-03-05 22:25:09 -0800403 for _, e := range excludes {
Colin Cross41955e82019-05-29 14:40:35 -0700404 if m, t := SrcIsModuleWithTag(e); m != "" {
Liz Kammera830f3a2020-11-10 10:50:34 -0800405 modulePaths, err := getPathsFromModuleDep(ctx, e, m, t)
406 if m, ok := err.(missingDependencyError); ok {
407 missingExcludeDeps = append(missingExcludeDeps, m.missingDeps...)
408 } else if err != nil {
409 reportPathError(ctx, err)
Colin Cross8a497952019-03-05 22:25:09 -0800410 } else {
Liz Kammera830f3a2020-11-10 10:50:34 -0800411 expandedExcludes = append(expandedExcludes, modulePaths.Strings()...)
Colin Cross8a497952019-03-05 22:25:09 -0800412 }
413 } else {
414 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
415 }
416 }
417
418 if paths == nil {
Colin Crossba71a3f2019-03-18 12:12:48 -0700419 return nil, missingExcludeDeps
Colin Cross8a497952019-03-05 22:25:09 -0800420 }
421
Colin Crossba71a3f2019-03-18 12:12:48 -0700422 var missingDeps []string
423
Colin Cross8a497952019-03-05 22:25:09 -0800424 expandedSrcFiles := make(Paths, 0, len(paths))
425 for _, s := range paths {
426 srcFiles, err := expandOneSrcPath(ctx, s, expandedExcludes)
427 if depErr, ok := err.(missingDependencyError); ok {
Colin Crossba71a3f2019-03-18 12:12:48 -0700428 missingDeps = append(missingDeps, depErr.missingDeps...)
Colin Cross8a497952019-03-05 22:25:09 -0800429 } else if err != nil {
430 reportPathError(ctx, err)
431 }
432 expandedSrcFiles = append(expandedSrcFiles, srcFiles...)
433 }
Colin Crossba71a3f2019-03-18 12:12:48 -0700434
435 return expandedSrcFiles, append(missingDeps, missingExcludeDeps...)
Colin Cross8a497952019-03-05 22:25:09 -0800436}
437
438type missingDependencyError struct {
439 missingDeps []string
440}
441
442func (e missingDependencyError) Error() string {
443 return "missing dependencies: " + strings.Join(e.missingDeps, ", ")
444}
445
Liz Kammera830f3a2020-11-10 10:50:34 -0800446// Expands one path string to Paths rooted from the module's local source
447// directory, excluding those listed in the expandedExcludes.
448// Expands globs, references to SourceFileProducer or OutputFileProducer modules using the ":name" and ":name{.tag}" syntax.
449func expandOneSrcPath(ctx ModuleWithDepsPathContext, sPath string, expandedExcludes []string) (Paths, error) {
Jooyung Han7607dd32020-07-05 10:23:14 +0900450 excludePaths := func(paths Paths) Paths {
451 if len(expandedExcludes) == 0 {
452 return paths
453 }
454 remainder := make(Paths, 0, len(paths))
455 for _, p := range paths {
456 if !InList(p.String(), expandedExcludes) {
457 remainder = append(remainder, p)
458 }
459 }
460 return remainder
461 }
Liz Kammera830f3a2020-11-10 10:50:34 -0800462 if m, t := SrcIsModuleWithTag(sPath); m != "" {
463 modulePaths, err := getPathsFromModuleDep(ctx, sPath, m, t)
464 if err != nil {
465 return nil, err
Colin Cross8a497952019-03-05 22:25:09 -0800466 } else {
Liz Kammera830f3a2020-11-10 10:50:34 -0800467 return excludePaths(modulePaths), nil
Colin Cross8a497952019-03-05 22:25:09 -0800468 }
Liz Kammera830f3a2020-11-10 10:50:34 -0800469 } else if pathtools.IsGlob(sPath) {
470 paths := GlobFiles(ctx, pathForModuleSrc(ctx, sPath).String(), expandedExcludes)
Colin Cross8a497952019-03-05 22:25:09 -0800471 return PathsWithModuleSrcSubDir(ctx, paths, ""), nil
472 } else {
Liz Kammera830f3a2020-11-10 10:50:34 -0800473 p := pathForModuleSrc(ctx, sPath)
Colin Cross988414c2020-01-11 01:11:46 +0000474 if exists, _, err := ctx.Config().fs.Exists(p.String()); err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100475 ReportPathErrorf(ctx, "%s: %s", p, err.Error())
Colin Cross5e6a7972020-06-07 16:56:32 -0700476 } else if !exists && !ctx.Config().testAllowNonExistentPaths {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100477 ReportPathErrorf(ctx, "module source path %q does not exist", p)
Colin Cross8a497952019-03-05 22:25:09 -0800478 }
479
Jooyung Han7607dd32020-07-05 10:23:14 +0900480 if InList(p.String(), expandedExcludes) {
Colin Cross8a497952019-03-05 22:25:09 -0800481 return nil, nil
482 }
483 return Paths{p}, nil
484 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700485}
486
487// pathsForModuleSrcFromFullPath returns Paths rooted from the module's local
488// source directory, but strip the local source directory from the beginning of
Dan Willemsen540a78c2018-02-26 21:50:08 -0800489// each string. If incDirs is false, strip paths with a trailing '/' from the list.
Colin Crossfe4bc362018-09-12 10:02:13 -0700490// It intended for use in globs that only list files that exist, so it allows '$' in
491// filenames.
Liz Kammera830f3a2020-11-10 10:50:34 -0800492func pathsForModuleSrcFromFullPath(ctx EarlyModulePathContext, paths []string, incDirs bool) Paths {
Colin Cross6510f912017-11-29 00:27:14 -0800493 prefix := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir()) + "/"
Colin Cross0f37af02017-09-27 17:42:05 -0700494 if prefix == "./" {
495 prefix = ""
496 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700497 ret := make(Paths, 0, len(paths))
498 for _, p := range paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -0800499 if !incDirs && strings.HasSuffix(p, "/") {
500 continue
501 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700502 path := filepath.Clean(p)
503 if !strings.HasPrefix(path, prefix) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100504 ReportPathErrorf(ctx, "Path %q is not in module source directory %q", p, prefix)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700505 continue
506 }
Colin Crosse3924e12018-08-15 20:18:53 -0700507
Colin Crossfe4bc362018-09-12 10:02:13 -0700508 srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):])
Colin Crosse3924e12018-08-15 20:18:53 -0700509 if err != nil {
510 reportPathError(ctx, err)
511 continue
512 }
513
Colin Cross07e51612019-03-05 12:46:40 -0800514 srcPath.basePath.rel = srcPath.path
Colin Crosse3924e12018-08-15 20:18:53 -0700515
Colin Cross07e51612019-03-05 12:46:40 -0800516 ret = append(ret, srcPath)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700517 }
518 return ret
519}
520
Liz Kammera830f3a2020-11-10 10:50:34 -0800521// PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's local source
522// directory. If input is nil, use the default if it exists. If input is empty, returns nil.
523func PathsWithOptionalDefaultForModuleSrc(ctx ModuleMissingDepsPathContext, input []string, def string) Paths {
Colin Cross0ddae7f2019-02-07 15:30:01 -0800524 if input != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700525 return PathsForModuleSrc(ctx, input)
526 }
527 // Use Glob so that if the default doesn't exist, a dependency is added so that when it
528 // is created, we're run again.
Colin Cross6510f912017-11-29 00:27:14 -0800529 path := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir(), def)
Liz Kammera830f3a2020-11-10 10:50:34 -0800530 return Glob(ctx, path, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700531}
532
533// Strings returns the Paths in string form
534func (p Paths) Strings() []string {
535 if p == nil {
536 return nil
537 }
538 ret := make([]string, len(p))
539 for i, path := range p {
540 ret[i] = path.String()
541 }
542 return ret
543}
544
Colin Crossc0efd1d2020-07-03 11:56:24 -0700545func CopyOfPaths(paths Paths) Paths {
546 return append(Paths(nil), paths...)
547}
548
Colin Crossb6715442017-10-24 11:13:31 -0700549// FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It
550// modifies the Paths slice contents in place, and returns a subslice of the original slice.
Dan Willemsenfe92c962017-08-29 12:28:37 -0700551func FirstUniquePaths(list Paths) Paths {
Colin Cross27027c72020-02-28 15:34:17 -0800552 // 128 was chosen based on BenchmarkFirstUniquePaths results.
553 if len(list) > 128 {
554 return firstUniquePathsMap(list)
555 }
556 return firstUniquePathsList(list)
557}
558
Colin Crossc0efd1d2020-07-03 11:56:24 -0700559// SortedUniquePaths returns all unique elements of a Paths in sorted order. It modifies the
560// Paths slice contents in place, and returns a subslice of the original slice.
Jiyong Park33c77362020-05-29 22:00:16 +0900561func SortedUniquePaths(list Paths) Paths {
562 unique := FirstUniquePaths(list)
563 sort.Slice(unique, func(i, j int) bool {
564 return unique[i].String() < unique[j].String()
565 })
566 return unique
567}
568
Colin Cross27027c72020-02-28 15:34:17 -0800569func firstUniquePathsList(list Paths) Paths {
Dan Willemsenfe92c962017-08-29 12:28:37 -0700570 k := 0
571outer:
572 for i := 0; i < len(list); i++ {
573 for j := 0; j < k; j++ {
574 if list[i] == list[j] {
575 continue outer
576 }
577 }
578 list[k] = list[i]
579 k++
580 }
581 return list[:k]
582}
583
Colin Cross27027c72020-02-28 15:34:17 -0800584func firstUniquePathsMap(list Paths) Paths {
585 k := 0
586 seen := make(map[Path]bool, len(list))
587 for i := 0; i < len(list); i++ {
588 if seen[list[i]] {
589 continue
590 }
591 seen[list[i]] = true
592 list[k] = list[i]
593 k++
594 }
595 return list[:k]
596}
597
Colin Cross5d583952020-11-24 16:21:24 -0800598// FirstUniqueInstallPaths returns all unique elements of an InstallPaths, keeping the first copy of each. It
599// modifies the InstallPaths slice contents in place, and returns a subslice of the original slice.
600func FirstUniqueInstallPaths(list InstallPaths) InstallPaths {
601 // 128 was chosen based on BenchmarkFirstUniquePaths results.
602 if len(list) > 128 {
603 return firstUniqueInstallPathsMap(list)
604 }
605 return firstUniqueInstallPathsList(list)
606}
607
608func firstUniqueInstallPathsList(list InstallPaths) InstallPaths {
609 k := 0
610outer:
611 for i := 0; i < len(list); i++ {
612 for j := 0; j < k; j++ {
613 if list[i] == list[j] {
614 continue outer
615 }
616 }
617 list[k] = list[i]
618 k++
619 }
620 return list[:k]
621}
622
623func firstUniqueInstallPathsMap(list InstallPaths) InstallPaths {
624 k := 0
625 seen := make(map[InstallPath]bool, len(list))
626 for i := 0; i < len(list); i++ {
627 if seen[list[i]] {
628 continue
629 }
630 seen[list[i]] = true
631 list[k] = list[i]
632 k++
633 }
634 return list[:k]
635}
636
Colin Crossb6715442017-10-24 11:13:31 -0700637// LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It
638// modifies the Paths slice contents in place, and returns a subslice of the original slice.
639func LastUniquePaths(list Paths) Paths {
640 totalSkip := 0
641 for i := len(list) - 1; i >= totalSkip; i-- {
642 skip := 0
643 for j := i - 1; j >= totalSkip; j-- {
644 if list[i] == list[j] {
645 skip++
646 } else {
647 list[j+skip] = list[j]
648 }
649 }
650 totalSkip += skip
651 }
652 return list[totalSkip:]
653}
654
Colin Crossa140bb02018-04-17 10:52:26 -0700655// ReversePaths returns a copy of a Paths in reverse order.
656func ReversePaths(list Paths) Paths {
657 if list == nil {
658 return nil
659 }
660 ret := make(Paths, len(list))
661 for i := range list {
662 ret[i] = list[len(list)-1-i]
663 }
664 return ret
665}
666
Jeff Gaston294356f2017-09-27 17:05:30 -0700667func indexPathList(s Path, list []Path) int {
668 for i, l := range list {
669 if l == s {
670 return i
671 }
672 }
673
674 return -1
675}
676
677func inPathList(p Path, list []Path) bool {
678 return indexPathList(p, list) != -1
679}
680
681func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) {
Paul Duffin57b9e1d2019-12-13 00:03:35 +0000682 return FilterPathListPredicate(list, func(p Path) bool { return inPathList(p, filter) })
683}
684
685func FilterPathListPredicate(list []Path, predicate func(Path) bool) (remainder []Path, filtered []Path) {
Jeff Gaston294356f2017-09-27 17:05:30 -0700686 for _, l := range list {
Paul Duffin57b9e1d2019-12-13 00:03:35 +0000687 if predicate(l) {
Jeff Gaston294356f2017-09-27 17:05:30 -0700688 filtered = append(filtered, l)
689 } else {
690 remainder = append(remainder, l)
691 }
692 }
693
694 return
695}
696
Colin Cross93e85952017-08-15 13:34:18 -0700697// HasExt returns true of any of the paths have extension ext, otherwise false
698func (p Paths) HasExt(ext string) bool {
699 for _, path := range p {
700 if path.Ext() == ext {
701 return true
702 }
703 }
704
705 return false
706}
707
708// FilterByExt returns the subset of the paths that have extension ext
709func (p Paths) FilterByExt(ext string) Paths {
710 ret := make(Paths, 0, len(p))
711 for _, path := range p {
712 if path.Ext() == ext {
713 ret = append(ret, path)
714 }
715 }
716 return ret
717}
718
719// FilterOutByExt returns the subset of the paths that do not have extension ext
720func (p Paths) FilterOutByExt(ext string) Paths {
721 ret := make(Paths, 0, len(p))
722 for _, path := range p {
723 if path.Ext() != ext {
724 ret = append(ret, path)
725 }
726 }
727 return ret
728}
729
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700730// DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory
731// (including subdirectories) are in a contiguous subslice of the list, and can be found in
732// O(log(N)) time using a binary search on the directory prefix.
733type DirectorySortedPaths Paths
734
735func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths {
736 ret := append(DirectorySortedPaths(nil), paths...)
737 sort.Slice(ret, func(i, j int) bool {
738 return ret[i].String() < ret[j].String()
739 })
740 return ret
741}
742
743// PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries
744// that are in the specified directory and its subdirectories.
745func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths {
746 prefix := filepath.Clean(dir) + "/"
747 start := sort.Search(len(p), func(i int) bool {
748 return prefix < p[i].String()
749 })
750
751 ret := p[start:]
752
753 end := sort.Search(len(ret), func(i int) bool {
754 return !strings.HasPrefix(ret[i].String(), prefix)
755 })
756
757 ret = ret[:end]
758
759 return Paths(ret)
760}
761
Alex Humesky29e3bbe2020-11-20 21:30:13 -0500762// WritablePaths is a slice of WritablePath, used for multiple outputs.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700763type WritablePaths []WritablePath
764
765// Strings returns the string forms of the writable paths.
766func (p WritablePaths) Strings() []string {
767 if p == nil {
768 return nil
769 }
770 ret := make([]string, len(p))
771 for i, path := range p {
772 ret[i] = path.String()
773 }
774 return ret
775}
776
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800777// Paths returns the WritablePaths as a Paths
778func (p WritablePaths) Paths() Paths {
779 if p == nil {
780 return nil
781 }
782 ret := make(Paths, len(p))
783 for i, path := range p {
784 ret[i] = path
785 }
786 return ret
787}
788
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700789type basePath struct {
790 path string
791 config Config
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800792 rel string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700793}
794
795func (p basePath) Ext() string {
796 return filepath.Ext(p.path)
797}
798
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700799func (p basePath) Base() string {
800 return filepath.Base(p.path)
801}
802
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800803func (p basePath) Rel() string {
804 if p.rel != "" {
805 return p.rel
806 }
807 return p.path
808}
809
Colin Cross0875c522017-11-28 17:34:01 -0800810func (p basePath) String() string {
811 return p.path
812}
813
Colin Cross0db55682017-12-05 15:36:55 -0800814func (p basePath) withRel(rel string) basePath {
815 p.path = filepath.Join(p.path, rel)
816 p.rel = rel
817 return p
818}
819
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700820// SourcePath is a Path representing a file path rooted from SrcDir
821type SourcePath struct {
822 basePath
823}
824
825var _ Path = SourcePath{}
826
Colin Cross0db55682017-12-05 15:36:55 -0800827func (p SourcePath) withRel(rel string) SourcePath {
828 p.basePath = p.basePath.withRel(rel)
829 return p
830}
831
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700832// safePathForSource is for paths that we expect are safe -- only for use by go
833// code that is embedding ninja variables in paths
Colin Crossfe4bc362018-09-12 10:02:13 -0700834func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
835 p, err := validateSafePath(pathComponents...)
Colin Crossaabf6792017-11-29 00:27:14 -0800836 ret := SourcePath{basePath{p, ctx.Config(), ""}}
Colin Crossfe4bc362018-09-12 10:02:13 -0700837 if err != nil {
838 return ret, err
839 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700840
Colin Cross7b3dcc32019-01-24 13:14:39 -0800841 // absolute path already checked by validateSafePath
842 if strings.HasPrefix(ret.String(), ctx.Config().buildDir) {
Mikhail Naganovab1f5182019-02-08 13:17:55 -0800843 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Cross6e18ca42015-07-14 18:55:36 -0700844 }
845
Colin Crossfe4bc362018-09-12 10:02:13 -0700846 return ret, err
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700847}
848
Colin Cross192e97a2018-02-22 14:21:02 -0800849// pathForSource creates a SourcePath from pathComponents, but does not check that it exists.
850func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
Colin Crossc48c1432018-02-23 07:09:01 +0000851 p, err := validatePath(pathComponents...)
852 ret := SourcePath{basePath{p, ctx.Config(), ""}}
Colin Cross94a32102018-02-22 14:21:02 -0800853 if err != nil {
Colin Cross192e97a2018-02-22 14:21:02 -0800854 return ret, err
Colin Cross94a32102018-02-22 14:21:02 -0800855 }
856
Colin Cross7b3dcc32019-01-24 13:14:39 -0800857 // absolute path already checked by validatePath
858 if strings.HasPrefix(ret.String(), ctx.Config().buildDir) {
Mikhail Naganovab1f5182019-02-08 13:17:55 -0800859 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Crossc48c1432018-02-23 07:09:01 +0000860 }
861
Colin Cross192e97a2018-02-22 14:21:02 -0800862 return ret, nil
863}
864
865// existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the
866// path does not exist.
867func existsWithDependencies(ctx PathContext, path SourcePath) (exists bool, err error) {
868 var files []string
869
870 if gctx, ok := ctx.(PathGlobContext); ok {
871 // Use glob to produce proper dependencies, even though we only want
872 // a single file.
873 files, err = gctx.GlobWithDeps(path.String(), nil)
874 } else {
875 var deps []string
876 // We cannot add build statements in this context, so we fall back to
877 // AddNinjaFileDeps
Colin Cross988414c2020-01-11 01:11:46 +0000878 files, deps, err = ctx.Config().fs.Glob(path.String(), nil, pathtools.FollowSymlinks)
Colin Cross192e97a2018-02-22 14:21:02 -0800879 ctx.AddNinjaFileDeps(deps...)
880 }
881
882 if err != nil {
883 return false, fmt.Errorf("glob: %s", err.Error())
884 }
885
886 return len(files) > 0, nil
887}
888
889// PathForSource joins the provided path components and validates that the result
890// neither escapes the source dir nor is in the out dir.
891// On error, it will return a usable, but invalid SourcePath, and report a ModuleError.
892func PathForSource(ctx PathContext, pathComponents ...string) SourcePath {
893 path, err := pathForSource(ctx, pathComponents...)
894 if err != nil {
895 reportPathError(ctx, err)
896 }
897
Colin Crosse3924e12018-08-15 20:18:53 -0700898 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100899 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -0700900 }
901
Liz Kammera830f3a2020-11-10 10:50:34 -0800902 if modCtx, ok := ctx.(ModuleMissingDepsPathContext); ok && ctx.Config().AllowMissingDependencies() {
Colin Cross192e97a2018-02-22 14:21:02 -0800903 exists, err := existsWithDependencies(ctx, path)
904 if err != nil {
905 reportPathError(ctx, err)
906 }
907 if !exists {
908 modCtx.AddMissingDependencies([]string{path.String()})
909 }
Colin Cross988414c2020-01-11 01:11:46 +0000910 } else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100911 ReportPathErrorf(ctx, "%s: %s", path, err.Error())
Colin Cross5e6a7972020-06-07 16:56:32 -0700912 } else if !exists && !ctx.Config().testAllowNonExistentPaths {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100913 ReportPathErrorf(ctx, "source path %q does not exist", path)
Colin Cross192e97a2018-02-22 14:21:02 -0800914 }
915 return path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700916}
917
Liz Kammer7aa52882021-02-11 09:16:14 -0500918// ExistentPathForSource returns an OptionalPath with the SourcePath, rooted from SrcDir, *not*
919// rooted from the module's local source directory, if the path exists, or an empty OptionalPath if
920// it doesn't exist. Dependencies are added so that the ninja file will be regenerated if the state
921// of the path changes.
Colin Cross32f38982018-02-22 11:47:25 -0800922func ExistentPathForSource(ctx PathContext, pathComponents ...string) OptionalPath {
Colin Cross192e97a2018-02-22 14:21:02 -0800923 path, err := pathForSource(ctx, pathComponents...)
Colin Cross1ccfcc32018-02-22 13:54:26 -0800924 if err != nil {
925 reportPathError(ctx, err)
926 return OptionalPath{}
927 }
Colin Crossc48c1432018-02-23 07:09:01 +0000928
Colin Crosse3924e12018-08-15 20:18:53 -0700929 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100930 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -0700931 return OptionalPath{}
932 }
933
Colin Cross192e97a2018-02-22 14:21:02 -0800934 exists, err := existsWithDependencies(ctx, path)
Colin Crossc48c1432018-02-23 07:09:01 +0000935 if err != nil {
936 reportPathError(ctx, err)
937 return OptionalPath{}
938 }
Colin Cross192e97a2018-02-22 14:21:02 -0800939 if !exists {
Colin Crossc48c1432018-02-23 07:09:01 +0000940 return OptionalPath{}
941 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700942 return OptionalPathForPath(path)
943}
944
945func (p SourcePath) String() string {
946 return filepath.Join(p.config.srcDir, p.path)
947}
948
949// Join creates a new SourcePath with paths... joined with the current path. The
950// provided paths... may not use '..' to escape from the current path.
951func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800952 path, err := validatePath(paths...)
953 if err != nil {
954 reportPathError(ctx, err)
955 }
Colin Cross0db55682017-12-05 15:36:55 -0800956 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700957}
958
Colin Cross2fafa3e2019-03-05 12:39:51 -0800959// join is like Join but does less path validation.
960func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath {
961 path, err := validateSafePath(paths...)
962 if err != nil {
963 reportPathError(ctx, err)
964 }
965 return p.withRel(path)
966}
967
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700968// OverlayPath returns the overlay for `path' if it exists. This assumes that the
969// SourcePath is the path to a resource overlay directory.
Liz Kammera830f3a2020-11-10 10:50:34 -0800970func (p SourcePath) OverlayPath(ctx ModuleMissingDepsPathContext, path Path) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700971 var relDir string
Colin Cross07e51612019-03-05 12:46:40 -0800972 if srcPath, ok := path.(SourcePath); ok {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700973 relDir = srcPath.path
974 } else {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100975 ReportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700976 return OptionalPath{}
977 }
978 dir := filepath.Join(p.config.srcDir, p.path, relDir)
979 // Use Glob so that we are run again if the directory is added.
Colin Cross7f19f372016-11-01 11:10:25 -0700980 if pathtools.IsGlob(dir) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100981 ReportPathErrorf(ctx, "Path may not contain a glob: %s", dir)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800982 }
Colin Cross461b4452018-02-23 09:22:42 -0800983 paths, err := ctx.GlobWithDeps(dir, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700984 if err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100985 ReportPathErrorf(ctx, "glob: %s", err.Error())
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700986 return OptionalPath{}
987 }
988 if len(paths) == 0 {
989 return OptionalPath{}
990 }
Colin Cross43f08db2018-11-12 10:13:39 -0800991 relPath := Rel(ctx, p.config.srcDir, paths[0])
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700992 return OptionalPathForPath(PathForSource(ctx, relPath))
993}
994
Colin Cross70dda7e2019-10-01 22:05:35 -0700995// OutputPath is a Path representing an intermediates file path rooted from the build directory
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700996type OutputPath struct {
997 basePath
Colin Crossd63c9a72020-01-29 16:52:50 -0800998 fullPath string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700999}
1000
Colin Cross702e0f82017-10-18 17:27:54 -07001001func (p OutputPath) withRel(rel string) OutputPath {
Colin Cross0db55682017-12-05 15:36:55 -08001002 p.basePath = p.basePath.withRel(rel)
Colin Crossd63c9a72020-01-29 16:52:50 -08001003 p.fullPath = filepath.Join(p.fullPath, rel)
Colin Cross702e0f82017-10-18 17:27:54 -07001004 return p
1005}
1006
Colin Cross3063b782018-08-15 11:19:12 -07001007func (p OutputPath) WithoutRel() OutputPath {
1008 p.basePath.rel = filepath.Base(p.basePath.path)
1009 return p
1010}
1011
Paul Duffin9b478b02019-12-10 13:41:51 +00001012func (p OutputPath) buildDir() string {
1013 return p.config.buildDir
1014}
1015
Paul Duffin0267d492021-02-02 10:05:52 +00001016func (p OutputPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
1017 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1018}
1019
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001020var _ Path = OutputPath{}
Paul Duffin9b478b02019-12-10 13:41:51 +00001021var _ WritablePath = OutputPath{}
Paul Duffin0267d492021-02-02 10:05:52 +00001022var _ objPathProvider = OutputPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001023
Chris Parsons8f232a22020-06-23 17:37:05 -04001024// toolDepPath is a Path representing a dependency of the build tool.
1025type toolDepPath struct {
1026 basePath
1027}
1028
1029var _ Path = toolDepPath{}
1030
1031// pathForBuildToolDep returns a toolDepPath representing the given path string.
1032// There is no validation for the path, as it is "trusted": It may fail
1033// normal validation checks. For example, it may be an absolute path.
1034// Only use this function to construct paths for dependencies of the build
1035// tool invocation.
1036func pathForBuildToolDep(ctx PathContext, path string) toolDepPath {
1037 return toolDepPath{basePath{path, ctx.Config(), ""}}
1038}
1039
Jeff Gaston734e3802017-04-10 15:47:24 -07001040// PathForOutput joins the provided paths and returns an OutputPath that is
1041// validated to not escape the build dir.
1042// On error, it will return a usable, but invalid OutputPath, and report a ModuleError.
1043func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001044 path, err := validatePath(pathComponents...)
1045 if err != nil {
1046 reportPathError(ctx, err)
1047 }
Colin Crossd63c9a72020-01-29 16:52:50 -08001048 fullPath := filepath.Join(ctx.Config().buildDir, path)
1049 path = fullPath[len(fullPath)-len(path):]
1050 return OutputPath{basePath{path, ctx.Config(), ""}, fullPath}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001051}
1052
Colin Cross40e33732019-02-15 11:08:35 -08001053// PathsForOutput returns Paths rooted from buildDir
1054func PathsForOutput(ctx PathContext, paths []string) WritablePaths {
1055 ret := make(WritablePaths, len(paths))
1056 for i, path := range paths {
1057 ret[i] = PathForOutput(ctx, path)
1058 }
1059 return ret
1060}
1061
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001062func (p OutputPath) writablePath() {}
1063
1064func (p OutputPath) String() string {
Colin Crossd63c9a72020-01-29 16:52:50 -08001065 return p.fullPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001066}
1067
1068// Join creates a new OutputPath with paths... joined with the current path. The
1069// provided paths... may not use '..' to escape from the current path.
1070func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001071 path, err := validatePath(paths...)
1072 if err != nil {
1073 reportPathError(ctx, err)
1074 }
Colin Cross0db55682017-12-05 15:36:55 -08001075 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001076}
1077
Colin Cross8854a5a2019-02-11 14:14:16 -08001078// ReplaceExtension creates a new OutputPath with the extension replaced with ext.
1079func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1080 if strings.Contains(ext, "/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001081 ReportPathErrorf(ctx, "extension %q cannot contain /", ext)
Colin Cross8854a5a2019-02-11 14:14:16 -08001082 }
1083 ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext))
Colin Cross2cdd5df2019-02-25 10:25:24 -08001084 ret.rel = pathtools.ReplaceExtension(p.rel, ext)
Colin Cross8854a5a2019-02-11 14:14:16 -08001085 return ret
1086}
1087
Colin Cross40e33732019-02-15 11:08:35 -08001088// InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths.
1089func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath {
1090 path, err := validatePath(paths...)
1091 if err != nil {
1092 reportPathError(ctx, err)
1093 }
1094
1095 ret := PathForOutput(ctx, filepath.Dir(p.path), path)
Colin Cross2cdd5df2019-02-25 10:25:24 -08001096 ret.rel = filepath.Join(filepath.Dir(p.rel), path)
Colin Cross40e33732019-02-15 11:08:35 -08001097 return ret
1098}
1099
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001100// PathForIntermediates returns an OutputPath representing the top-level
1101// intermediates directory.
1102func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001103 path, err := validatePath(paths...)
1104 if err != nil {
1105 reportPathError(ctx, err)
1106 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001107 return PathForOutput(ctx, ".intermediates", path)
1108}
1109
Colin Cross07e51612019-03-05 12:46:40 -08001110var _ genPathProvider = SourcePath{}
1111var _ objPathProvider = SourcePath{}
1112var _ resPathProvider = SourcePath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001113
Colin Cross07e51612019-03-05 12:46:40 -08001114// PathForModuleSrc returns a Path representing the paths... under the
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001115// module's local source directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001116func PathForModuleSrc(ctx ModuleMissingDepsPathContext, pathComponents ...string) Path {
Colin Cross8a497952019-03-05 22:25:09 -08001117 p, err := validatePath(pathComponents...)
1118 if err != nil {
1119 reportPathError(ctx, err)
Colin Cross192e97a2018-02-22 14:21:02 -08001120 }
Colin Cross8a497952019-03-05 22:25:09 -08001121 paths, err := expandOneSrcPath(ctx, p, nil)
1122 if err != nil {
1123 if depErr, ok := err.(missingDependencyError); ok {
1124 if ctx.Config().AllowMissingDependencies() {
1125 ctx.AddMissingDependencies(depErr.missingDeps)
1126 } else {
1127 ctx.ModuleErrorf(`%s, is the property annotated with android:"path"?`, depErr.Error())
1128 }
1129 } else {
1130 reportPathError(ctx, err)
1131 }
1132 return nil
1133 } else if len(paths) == 0 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001134 ReportPathErrorf(ctx, "%q produced no files, expected exactly one", p)
Colin Cross8a497952019-03-05 22:25:09 -08001135 return nil
1136 } else if len(paths) > 1 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001137 ReportPathErrorf(ctx, "%q produced %d files, expected exactly one", p, len(paths))
Colin Cross8a497952019-03-05 22:25:09 -08001138 }
1139 return paths[0]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001140}
1141
Liz Kammera830f3a2020-11-10 10:50:34 -08001142func pathForModuleSrc(ctx EarlyModulePathContext, paths ...string) SourcePath {
Colin Cross07e51612019-03-05 12:46:40 -08001143 p, err := validatePath(paths...)
1144 if err != nil {
1145 reportPathError(ctx, err)
1146 }
1147
1148 path, err := pathForSource(ctx, ctx.ModuleDir(), p)
1149 if err != nil {
1150 reportPathError(ctx, err)
1151 }
1152
1153 path.basePath.rel = p
1154
1155 return path
1156}
1157
Colin Cross2fafa3e2019-03-05 12:39:51 -08001158// PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path
1159// will return the path relative to subDir in the module's source directory. If any input paths are not located
1160// inside subDir then a path error will be reported.
Liz Kammera830f3a2020-11-10 10:50:34 -08001161func PathsWithModuleSrcSubDir(ctx EarlyModulePathContext, paths Paths, subDir string) Paths {
Colin Cross2fafa3e2019-03-05 12:39:51 -08001162 paths = append(Paths(nil), paths...)
Colin Cross07e51612019-03-05 12:46:40 -08001163 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001164 for i, path := range paths {
1165 rel := Rel(ctx, subDirFullPath.String(), path.String())
1166 paths[i] = subDirFullPath.join(ctx, rel)
1167 }
1168 return paths
1169}
1170
1171// PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the
1172// module's source directory. If the input path is not located inside subDir then a path error will be reported.
Liz Kammera830f3a2020-11-10 10:50:34 -08001173func PathWithModuleSrcSubDir(ctx EarlyModulePathContext, path Path, subDir string) Path {
Colin Cross07e51612019-03-05 12:46:40 -08001174 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001175 rel := Rel(ctx, subDirFullPath.String(), path.String())
1176 return subDirFullPath.Join(ctx, rel)
1177}
1178
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001179// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
1180// valid path if p is non-nil.
Liz Kammera830f3a2020-11-10 10:50:34 -08001181func OptionalPathForModuleSrc(ctx ModuleMissingDepsPathContext, p *string) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001182 if p == nil {
1183 return OptionalPath{}
1184 }
1185 return OptionalPathForPath(PathForModuleSrc(ctx, *p))
1186}
1187
Liz Kammera830f3a2020-11-10 10:50:34 -08001188func (p SourcePath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001189 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001190}
1191
Liz Kammera830f3a2020-11-10 10:50:34 -08001192func (p SourcePath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001193 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001194}
1195
Liz Kammera830f3a2020-11-10 10:50:34 -08001196func (p SourcePath) resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001197 // TODO: Use full directory if the new ctx is not the current ctx?
1198 return PathForModuleRes(ctx, p.path, name)
1199}
1200
1201// ModuleOutPath is a Path representing a module's output directory.
1202type ModuleOutPath struct {
1203 OutputPath
1204}
1205
1206var _ Path = ModuleOutPath{}
1207
Liz Kammera830f3a2020-11-10 10:50:34 -08001208func (p ModuleOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01001209 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1210}
1211
Liz Kammera830f3a2020-11-10 10:50:34 -08001212// ModuleOutPathContext Subset of ModuleContext functions necessary for output path methods.
1213type ModuleOutPathContext interface {
1214 PathContext
1215
1216 ModuleName() string
1217 ModuleDir() string
1218 ModuleSubDir() string
1219}
1220
1221func pathForModuleOut(ctx ModuleOutPathContext) OutputPath {
Colin Cross702e0f82017-10-18 17:27:54 -07001222 return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir())
1223}
1224
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001225type BazelOutPath struct {
1226 OutputPath
1227}
1228
1229var _ Path = BazelOutPath{}
1230var _ objPathProvider = BazelOutPath{}
1231
Liz Kammera830f3a2020-11-10 10:50:34 -08001232func (p BazelOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001233 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1234}
1235
Logan Chien7eefdc42018-07-11 18:10:41 +08001236// PathForVndkRefAbiDump returns an OptionalPath representing the path of the
1237// reference abi dump for the given module. This is not guaranteed to be valid.
Liz Kammera830f3a2020-11-10 10:50:34 -08001238func PathForVndkRefAbiDump(ctx ModuleInstallPathContext, version, fileName string,
Hsin-Yi Chen53489642019-07-31 17:10:45 +08001239 isNdk, isLlndkOrVndk, isGzip bool) OptionalPath {
Logan Chien7eefdc42018-07-11 18:10:41 +08001240
Jayant Chowdharyac066c62018-02-20 10:53:31 -08001241 arches := ctx.DeviceConfig().Arches()
Logan Chien7eefdc42018-07-11 18:10:41 +08001242 if len(arches) == 0 {
1243 panic("device build with no primary arch")
1244 }
Jayant Chowdharyac066c62018-02-20 10:53:31 -08001245 currentArch := ctx.Arch()
1246 archNameAndVariant := currentArch.ArchType.String()
1247 if currentArch.ArchVariant != "" {
1248 archNameAndVariant += "_" + currentArch.ArchVariant
1249 }
Logan Chien5237bed2018-07-11 17:15:57 +08001250
1251 var dirName string
Hsin-Yi Chen53489642019-07-31 17:10:45 +08001252 if isNdk {
Logan Chien5237bed2018-07-11 17:15:57 +08001253 dirName = "ndk"
Hsin-Yi Chen53489642019-07-31 17:10:45 +08001254 } else if isLlndkOrVndk {
Logan Chien5237bed2018-07-11 17:15:57 +08001255 dirName = "vndk"
Logan Chien41eabe62019-04-10 13:33:58 +08001256 } else {
1257 dirName = "platform" // opt-in libs
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001258 }
Logan Chien5237bed2018-07-11 17:15:57 +08001259
Jayant Chowdhary34ce67d2018-03-08 11:00:50 -08001260 binderBitness := ctx.DeviceConfig().BinderBitness()
Logan Chien7eefdc42018-07-11 18:10:41 +08001261
1262 var ext string
1263 if isGzip {
1264 ext = ".lsdump.gz"
1265 } else {
1266 ext = ".lsdump"
1267 }
1268
1269 return ExistentPathForSource(ctx, "prebuilts", "abi-dumps", dirName,
1270 version, binderBitness, archNameAndVariant, "source-based",
1271 fileName+ext)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001272}
1273
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001274// PathForBazelOut returns a Path representing the paths... under an output directory dedicated to
1275// bazel-owned outputs.
1276func PathForBazelOut(ctx PathContext, paths ...string) BazelOutPath {
1277 execRootPathComponents := append([]string{"execroot", "__main__"}, paths...)
1278 execRootPath := filepath.Join(execRootPathComponents...)
1279 validatedExecRootPath, err := validatePath(execRootPath)
1280 if err != nil {
1281 reportPathError(ctx, err)
1282 }
1283
1284 outputPath := OutputPath{basePath{"", ctx.Config(), ""},
1285 ctx.Config().BazelContext.OutputBase()}
1286
1287 return BazelOutPath{
1288 OutputPath: outputPath.withRel(validatedExecRootPath),
1289 }
1290}
1291
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001292// PathForModuleOut returns a Path representing the paths... under the module's
1293// output directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001294func PathForModuleOut(ctx ModuleOutPathContext, paths ...string) ModuleOutPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001295 p, err := validatePath(paths...)
1296 if err != nil {
1297 reportPathError(ctx, err)
1298 }
Colin Cross702e0f82017-10-18 17:27:54 -07001299 return ModuleOutPath{
Liz Kammera830f3a2020-11-10 10:50:34 -08001300 OutputPath: pathForModuleOut(ctx).withRel(p),
Colin Cross702e0f82017-10-18 17:27:54 -07001301 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001302}
1303
1304// ModuleGenPath is a Path representing the 'gen' directory in a module's output
1305// directory. Mainly used for generated sources.
1306type ModuleGenPath struct {
1307 ModuleOutPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001308}
1309
1310var _ Path = ModuleGenPath{}
1311var _ genPathProvider = ModuleGenPath{}
1312var _ objPathProvider = ModuleGenPath{}
1313
1314// PathForModuleGen returns a Path representing the paths... under the module's
1315// `gen' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001316func PathForModuleGen(ctx ModuleOutPathContext, paths ...string) ModuleGenPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001317 p, err := validatePath(paths...)
1318 if err != nil {
1319 reportPathError(ctx, err)
1320 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001321 return ModuleGenPath{
Colin Cross702e0f82017-10-18 17:27:54 -07001322 ModuleOutPath: ModuleOutPath{
Liz Kammera830f3a2020-11-10 10:50:34 -08001323 OutputPath: pathForModuleOut(ctx).withRel("gen").withRel(p),
Colin Cross702e0f82017-10-18 17:27:54 -07001324 },
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001325 }
1326}
1327
Liz Kammera830f3a2020-11-10 10:50:34 -08001328func (p ModuleGenPath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001329 // TODO: make a different path for local vs remote generated files?
Dan Willemsen21ec4902016-11-02 20:43:13 -07001330 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001331}
1332
Liz Kammera830f3a2020-11-10 10:50:34 -08001333func (p ModuleGenPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001334 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1335}
1336
1337// ModuleObjPath is a Path representing the 'obj' directory in a module's output
1338// directory. Used for compiled objects.
1339type ModuleObjPath struct {
1340 ModuleOutPath
1341}
1342
1343var _ Path = ModuleObjPath{}
1344
1345// PathForModuleObj returns a Path representing the paths... under the module's
1346// 'obj' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001347func PathForModuleObj(ctx ModuleOutPathContext, pathComponents ...string) ModuleObjPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001348 p, err := validatePath(pathComponents...)
1349 if err != nil {
1350 reportPathError(ctx, err)
1351 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001352 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
1353}
1354
1355// ModuleResPath is a a Path representing the 'res' directory in a module's
1356// output directory.
1357type ModuleResPath struct {
1358 ModuleOutPath
1359}
1360
1361var _ Path = ModuleResPath{}
1362
1363// PathForModuleRes returns a Path representing the paths... under the module's
1364// 'res' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001365func PathForModuleRes(ctx ModuleOutPathContext, pathComponents ...string) ModuleResPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001366 p, err := validatePath(pathComponents...)
1367 if err != nil {
1368 reportPathError(ctx, err)
1369 }
1370
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001371 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
1372}
1373
Colin Cross70dda7e2019-10-01 22:05:35 -07001374// InstallPath is a Path representing a installed file path rooted from the build directory
1375type InstallPath struct {
1376 basePath
Colin Crossff6c33d2019-10-02 16:01:35 -07001377
Jiyong Park957bcd92020-10-20 18:23:33 +09001378 // partitionDir is the part of the InstallPath that is automatically determined according to the context.
1379 // For example, it is host/<os>-<arch> for host modules, and target/product/<device>/<partition> for device modules.
1380 partitionDir string
1381
1382 // makePath indicates whether this path is for Soong (false) or Make (true).
1383 makePath bool
Colin Cross70dda7e2019-10-01 22:05:35 -07001384}
1385
Paul Duffin9b478b02019-12-10 13:41:51 +00001386func (p InstallPath) buildDir() string {
1387 return p.config.buildDir
1388}
1389
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01001390func (p InstallPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1391 panic("Not implemented")
1392}
1393
Paul Duffin9b478b02019-12-10 13:41:51 +00001394var _ Path = InstallPath{}
1395var _ WritablePath = InstallPath{}
1396
Colin Cross70dda7e2019-10-01 22:05:35 -07001397func (p InstallPath) writablePath() {}
1398
1399func (p InstallPath) String() string {
Jiyong Park957bcd92020-10-20 18:23:33 +09001400 if p.makePath {
1401 // Make path starts with out/ instead of out/soong.
1402 return filepath.Join(p.config.buildDir, "../", p.path)
1403 } else {
1404 return filepath.Join(p.config.buildDir, p.path)
1405 }
1406}
1407
1408// PartitionDir returns the path to the partition where the install path is rooted at. It is
1409// out/soong/target/product/<device>/<partition> for device modules, and out/soong/host/<os>-<arch> for host modules.
1410// The ./soong is dropped if the install path is for Make.
1411func (p InstallPath) PartitionDir() string {
1412 if p.makePath {
1413 return filepath.Join(p.config.buildDir, "../", p.partitionDir)
1414 } else {
1415 return filepath.Join(p.config.buildDir, p.partitionDir)
1416 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001417}
1418
1419// Join creates a new InstallPath with paths... joined with the current path. The
1420// provided paths... may not use '..' to escape from the current path.
1421func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath {
1422 path, err := validatePath(paths...)
1423 if err != nil {
1424 reportPathError(ctx, err)
1425 }
1426 return p.withRel(path)
1427}
1428
1429func (p InstallPath) withRel(rel string) InstallPath {
1430 p.basePath = p.basePath.withRel(rel)
1431 return p
1432}
1433
Colin Crossff6c33d2019-10-02 16:01:35 -07001434// ToMakePath returns a new InstallPath that points to Make's install directory instead of Soong's,
1435// i.e. out/ instead of out/soong/.
1436func (p InstallPath) ToMakePath() InstallPath {
Jiyong Park957bcd92020-10-20 18:23:33 +09001437 p.makePath = true
Colin Crossff6c33d2019-10-02 16:01:35 -07001438 return p
Colin Cross70dda7e2019-10-01 22:05:35 -07001439}
1440
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001441// PathForModuleInstall returns a Path representing the install path for the
1442// module appended with paths...
Colin Cross70dda7e2019-10-01 22:05:35 -07001443func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
Colin Cross6e359402020-02-10 15:29:54 -08001444 os := ctx.Os()
Jiyong Park87788b52020-09-01 12:37:45 +09001445 arch := ctx.Arch().ArchType
1446 forceOS, forceArch := ctx.InstallForceOS()
1447 if forceOS != nil {
Colin Cross6e359402020-02-10 15:29:54 -08001448 os = *forceOS
1449 }
Jiyong Park87788b52020-09-01 12:37:45 +09001450 if forceArch != nil {
1451 arch = *forceArch
1452 }
Colin Cross6e359402020-02-10 15:29:54 -08001453 partition := modulePartition(ctx, os)
Colin Cross609c49a2020-02-13 13:20:11 -08001454
Jiyong Park87788b52020-09-01 12:37:45 +09001455 ret := pathForInstall(ctx, os, arch, partition, ctx.Debug(), pathComponents...)
Colin Cross609c49a2020-02-13 13:20:11 -08001456
Jingwen Chencda22c92020-11-23 00:22:30 -05001457 if ctx.InstallBypassMake() && ctx.Config().KatiEnabled() {
Colin Cross609c49a2020-02-13 13:20:11 -08001458 ret = ret.ToMakePath()
1459 }
1460
1461 return ret
1462}
1463
Jiyong Park87788b52020-09-01 12:37:45 +09001464func pathForInstall(ctx PathContext, os OsType, arch ArchType, partition string, debug bool,
Colin Cross609c49a2020-02-13 13:20:11 -08001465 pathComponents ...string) InstallPath {
1466
Jiyong Park957bcd92020-10-20 18:23:33 +09001467 var partionPaths []string
Colin Cross609c49a2020-02-13 13:20:11 -08001468
Colin Cross6e359402020-02-10 15:29:54 -08001469 if os.Class == Device {
Jiyong Park957bcd92020-10-20 18:23:33 +09001470 partionPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001471 } else {
Jiyong Park87788b52020-09-01 12:37:45 +09001472 osName := os.String()
1473 if os == Linux {
1474 // instead of linux_glibc
1475 osName = "linux"
Dan Willemsen866b5632017-09-22 12:28:24 -07001476 }
Jiyong Park87788b52020-09-01 12:37:45 +09001477 // SOONG_HOST_OUT is set to out/host/$(HOST_OS)-$(HOST_PREBUILT_ARCH)
1478 // and HOST_PREBUILT_ARCH is forcibly set to x86 even on x86_64 hosts. We don't seem
1479 // to have a plan to fix it (see the comment in build/make/core/envsetup.mk).
1480 // Let's keep using x86 for the existing cases until we have a need to support
1481 // other architectures.
1482 archName := arch.String()
1483 if os.Class == Host && (arch == X86_64 || arch == Common) {
1484 archName = "x86"
1485 }
Jiyong Park957bcd92020-10-20 18:23:33 +09001486 partionPaths = []string{"host", osName + "-" + archName, partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001487 }
Colin Cross609c49a2020-02-13 13:20:11 -08001488 if debug {
Jiyong Park957bcd92020-10-20 18:23:33 +09001489 partionPaths = append([]string{"debug"}, partionPaths...)
Dan Willemsen782a2d12015-12-21 14:55:28 -08001490 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001491
Jiyong Park957bcd92020-10-20 18:23:33 +09001492 partionPath, err := validatePath(partionPaths...)
Colin Cross70dda7e2019-10-01 22:05:35 -07001493 if err != nil {
1494 reportPathError(ctx, err)
1495 }
Colin Crossff6c33d2019-10-02 16:01:35 -07001496
Jiyong Park957bcd92020-10-20 18:23:33 +09001497 base := InstallPath{
1498 basePath: basePath{partionPath, ctx.Config(), ""},
1499 partitionDir: partionPath,
1500 makePath: false,
1501 }
Colin Crossff6c33d2019-10-02 16:01:35 -07001502
Jiyong Park957bcd92020-10-20 18:23:33 +09001503 return base.Join(ctx, pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001504}
1505
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00001506func pathForNdkOrSdkInstall(ctx PathContext, prefix string, paths []string) InstallPath {
Jiyong Park957bcd92020-10-20 18:23:33 +09001507 base := InstallPath{
1508 basePath: basePath{prefix, ctx.Config(), ""},
1509 partitionDir: prefix,
1510 makePath: false,
Colin Cross70dda7e2019-10-01 22:05:35 -07001511 }
Jiyong Park957bcd92020-10-20 18:23:33 +09001512 return base.Join(ctx, paths...)
Colin Cross70dda7e2019-10-01 22:05:35 -07001513}
1514
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00001515func PathForNdkInstall(ctx PathContext, paths ...string) InstallPath {
1516 return pathForNdkOrSdkInstall(ctx, "ndk", paths)
1517}
1518
1519func PathForMainlineSdksInstall(ctx PathContext, paths ...string) InstallPath {
1520 return pathForNdkOrSdkInstall(ctx, "mainline-sdks", paths)
1521}
1522
Colin Cross70dda7e2019-10-01 22:05:35 -07001523func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string {
Colin Cross43f08db2018-11-12 10:13:39 -08001524 rel := Rel(ctx, PathForOutput(ctx, "target", "product", ctx.Config().DeviceName()).String(), path.String())
1525
1526 return "/" + rel
1527}
1528
Colin Cross6e359402020-02-10 15:29:54 -08001529func modulePartition(ctx ModuleInstallPathContext, os OsType) string {
Colin Cross43f08db2018-11-12 10:13:39 -08001530 var partition string
Colin Cross6e359402020-02-10 15:29:54 -08001531 if ctx.InstallInTestcases() {
1532 // "testcases" install directory can be used for host or device modules.
Jaewoong Jung0949f312019-09-11 10:25:18 -07001533 partition = "testcases"
Colin Cross6e359402020-02-10 15:29:54 -08001534 } else if os.Class == Device {
1535 if ctx.InstallInData() {
1536 partition = "data"
1537 } else if ctx.InstallInRamdisk() {
1538 if ctx.DeviceConfig().BoardUsesRecoveryAsBoot() {
1539 partition = "recovery/root/first_stage_ramdisk"
1540 } else {
1541 partition = "ramdisk"
1542 }
1543 if !ctx.InstallInRoot() {
1544 partition += "/system"
1545 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -07001546 } else if ctx.InstallInVendorRamdisk() {
Yifan Hong39143a92020-10-26 12:43:12 -07001547 // The module is only available after switching root into
1548 // /first_stage_ramdisk. To expose the module before switching root
1549 // on a device without a dedicated recovery partition, install the
1550 // recovery variant.
Yifan Hongdd8dacc2020-10-21 15:40:17 -07001551 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
Yifan Hong39143a92020-10-26 12:43:12 -07001552 partition = "vendor-ramdisk/first_stage_ramdisk"
Yifan Hongdd8dacc2020-10-21 15:40:17 -07001553 } else {
1554 partition = "vendor-ramdisk"
1555 }
1556 if !ctx.InstallInRoot() {
1557 partition += "/system"
1558 }
Colin Cross6e359402020-02-10 15:29:54 -08001559 } else if ctx.InstallInRecovery() {
1560 if ctx.InstallInRoot() {
1561 partition = "recovery/root"
1562 } else {
1563 // the layout of recovery partion is the same as that of system partition
1564 partition = "recovery/root/system"
1565 }
1566 } else if ctx.SocSpecific() {
1567 partition = ctx.DeviceConfig().VendorPath()
1568 } else if ctx.DeviceSpecific() {
1569 partition = ctx.DeviceConfig().OdmPath()
1570 } else if ctx.ProductSpecific() {
1571 partition = ctx.DeviceConfig().ProductPath()
1572 } else if ctx.SystemExtSpecific() {
1573 partition = ctx.DeviceConfig().SystemExtPath()
1574 } else if ctx.InstallInRoot() {
1575 partition = "root"
Yifan Hong82db7352020-01-21 16:12:26 -08001576 } else {
Colin Cross6e359402020-02-10 15:29:54 -08001577 partition = "system"
Yifan Hong82db7352020-01-21 16:12:26 -08001578 }
Colin Cross6e359402020-02-10 15:29:54 -08001579 if ctx.InstallInSanitizerDir() {
1580 partition = "data/asan/" + partition
Yifan Hong82db7352020-01-21 16:12:26 -08001581 }
Colin Cross43f08db2018-11-12 10:13:39 -08001582 }
1583 return partition
1584}
1585
Colin Cross609c49a2020-02-13 13:20:11 -08001586type InstallPaths []InstallPath
1587
1588// Paths returns the InstallPaths as a Paths
1589func (p InstallPaths) Paths() Paths {
1590 if p == nil {
1591 return nil
1592 }
1593 ret := make(Paths, len(p))
1594 for i, path := range p {
1595 ret[i] = path
1596 }
1597 return ret
1598}
1599
1600// Strings returns the string forms of the install paths.
1601func (p InstallPaths) Strings() []string {
1602 if p == nil {
1603 return nil
1604 }
1605 ret := make([]string, len(p))
1606 for i, path := range p {
1607 ret[i] = path.String()
1608 }
1609 return ret
1610}
1611
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001612// validateSafePath validates a path that we trust (may contain ninja variables).
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001613// Ensures that each path component does not attempt to leave its component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001614func validateSafePath(pathComponents ...string) (string, error) {
Jeff Gaston734e3802017-04-10 15:47:24 -07001615 for _, path := range pathComponents {
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001616 path := filepath.Clean(path)
1617 if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001618 return "", fmt.Errorf("Path is outside directory: %s", path)
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001619 }
1620 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001621 // TODO: filepath.Join isn't necessarily correct with embedded ninja
1622 // variables. '..' may remove the entire ninja variable, even if it
1623 // will be expanded to multiple nested directories.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001624 return filepath.Join(pathComponents...), nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001625}
1626
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001627// validatePath validates that a path does not include ninja variables, and that
1628// each path component does not attempt to leave its component. Returns a joined
1629// version of each path component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001630func validatePath(pathComponents ...string) (string, error) {
Jeff Gaston734e3802017-04-10 15:47:24 -07001631 for _, path := range pathComponents {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001632 if strings.Contains(path, "$") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001633 return "", fmt.Errorf("Path contains invalid character($): %s", path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001634 }
1635 }
Colin Cross1ccfcc32018-02-22 13:54:26 -08001636 return validateSafePath(pathComponents...)
Colin Cross6e18ca42015-07-14 18:55:36 -07001637}
Colin Cross5b529592017-05-09 13:34:34 -07001638
Colin Cross0875c522017-11-28 17:34:01 -08001639func PathForPhony(ctx PathContext, phony string) WritablePath {
1640 if strings.ContainsAny(phony, "$/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001641 ReportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony)
Colin Cross0875c522017-11-28 17:34:01 -08001642 }
Colin Cross74e3fe42017-12-11 15:51:44 -08001643 return PhonyPath{basePath{phony, ctx.Config(), ""}}
Colin Cross0875c522017-11-28 17:34:01 -08001644}
1645
Colin Cross74e3fe42017-12-11 15:51:44 -08001646type PhonyPath struct {
1647 basePath
1648}
1649
1650func (p PhonyPath) writablePath() {}
1651
Paul Duffin9b478b02019-12-10 13:41:51 +00001652func (p PhonyPath) buildDir() string {
1653 return p.config.buildDir
1654}
1655
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01001656func (p PhonyPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1657 panic("Not implemented")
1658}
1659
Colin Cross74e3fe42017-12-11 15:51:44 -08001660var _ Path = PhonyPath{}
1661var _ WritablePath = PhonyPath{}
1662
Colin Cross5b529592017-05-09 13:34:34 -07001663type testPath struct {
1664 basePath
1665}
1666
1667func (p testPath) String() string {
1668 return p.path
1669}
1670
Colin Cross40e33732019-02-15 11:08:35 -08001671// PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from
1672// within tests.
Colin Cross5b529592017-05-09 13:34:34 -07001673func PathForTesting(paths ...string) Path {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001674 p, err := validateSafePath(paths...)
1675 if err != nil {
1676 panic(err)
1677 }
Colin Cross5b529592017-05-09 13:34:34 -07001678 return testPath{basePath{path: p, rel: p}}
1679}
1680
Colin Cross40e33732019-02-15 11:08:35 -08001681// PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests.
1682func PathsForTesting(strs ...string) Paths {
Colin Cross5b529592017-05-09 13:34:34 -07001683 p := make(Paths, len(strs))
1684 for i, s := range strs {
1685 p[i] = PathForTesting(s)
1686 }
1687
1688 return p
1689}
Colin Cross43f08db2018-11-12 10:13:39 -08001690
Colin Cross40e33732019-02-15 11:08:35 -08001691type testPathContext struct {
1692 config Config
Colin Cross40e33732019-02-15 11:08:35 -08001693}
1694
Colin Cross40e33732019-02-15 11:08:35 -08001695func (x *testPathContext) Config() Config { return x.config }
1696func (x *testPathContext) AddNinjaFileDeps(...string) {}
1697
1698// PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with
1699// PathForOutput.
Colin Cross98be1bb2019-12-13 20:41:13 -08001700func PathContextForTesting(config Config) PathContext {
Colin Cross40e33732019-02-15 11:08:35 -08001701 return &testPathContext{
1702 config: config,
Colin Cross40e33732019-02-15 11:08:35 -08001703 }
1704}
1705
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01001706type testModuleInstallPathContext struct {
1707 baseModuleContext
1708
1709 inData bool
1710 inTestcases bool
1711 inSanitizerDir bool
1712 inRamdisk bool
1713 inVendorRamdisk bool
1714 inRecovery bool
1715 inRoot bool
1716 forceOS *OsType
1717 forceArch *ArchType
1718}
1719
1720func (m testModuleInstallPathContext) Config() Config {
1721 return m.baseModuleContext.config
1722}
1723
1724func (testModuleInstallPathContext) AddNinjaFileDeps(deps ...string) {}
1725
1726func (m testModuleInstallPathContext) InstallInData() bool {
1727 return m.inData
1728}
1729
1730func (m testModuleInstallPathContext) InstallInTestcases() bool {
1731 return m.inTestcases
1732}
1733
1734func (m testModuleInstallPathContext) InstallInSanitizerDir() bool {
1735 return m.inSanitizerDir
1736}
1737
1738func (m testModuleInstallPathContext) InstallInRamdisk() bool {
1739 return m.inRamdisk
1740}
1741
1742func (m testModuleInstallPathContext) InstallInVendorRamdisk() bool {
1743 return m.inVendorRamdisk
1744}
1745
1746func (m testModuleInstallPathContext) InstallInRecovery() bool {
1747 return m.inRecovery
1748}
1749
1750func (m testModuleInstallPathContext) InstallInRoot() bool {
1751 return m.inRoot
1752}
1753
1754func (m testModuleInstallPathContext) InstallBypassMake() bool {
1755 return false
1756}
1757
1758func (m testModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
1759 return m.forceOS, m.forceArch
1760}
1761
1762// Construct a minimal ModuleInstallPathContext for testing. Note that baseModuleContext is
1763// default-initialized, which leaves blueprint.baseModuleContext set to nil, so methods that are
1764// delegated to it will panic.
1765func ModuleInstallPathContextForTesting(config Config) ModuleInstallPathContext {
1766 ctx := &testModuleInstallPathContext{}
1767 ctx.config = config
1768 ctx.os = Android
1769 return ctx
1770}
1771
Colin Cross43f08db2018-11-12 10:13:39 -08001772// Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if
1773// targetPath is not inside basePath.
1774func Rel(ctx PathContext, basePath string, targetPath string) string {
1775 rel, isRel := MaybeRel(ctx, basePath, targetPath)
1776 if !isRel {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001777 ReportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath)
Colin Cross43f08db2018-11-12 10:13:39 -08001778 return ""
1779 }
1780 return rel
1781}
1782
1783// MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if
1784// targetPath is not inside basePath.
1785func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) {
Dan Willemsen633c5022019-04-12 11:11:38 -07001786 rel, isRel, err := maybeRelErr(basePath, targetPath)
1787 if err != nil {
1788 reportPathError(ctx, err)
1789 }
1790 return rel, isRel
1791}
1792
1793func maybeRelErr(basePath string, targetPath string) (string, bool, error) {
Colin Cross43f08db2018-11-12 10:13:39 -08001794 // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first.
1795 if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) {
Dan Willemsen633c5022019-04-12 11:11:38 -07001796 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08001797 }
1798 rel, err := filepath.Rel(basePath, targetPath)
1799 if err != nil {
Dan Willemsen633c5022019-04-12 11:11:38 -07001800 return "", false, err
Colin Cross43f08db2018-11-12 10:13:39 -08001801 } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") {
Dan Willemsen633c5022019-04-12 11:11:38 -07001802 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08001803 }
Dan Willemsen633c5022019-04-12 11:11:38 -07001804 return rel, true, nil
Colin Cross43f08db2018-11-12 10:13:39 -08001805}
Colin Cross988414c2020-01-11 01:11:46 +00001806
1807// Writes a file to the output directory. Attempting to write directly to the output directory
1808// will fail due to the sandbox of the soong_build process.
1809func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error {
1810 return ioutil.WriteFile(absolutePath(path.String()), data, perm)
1811}
1812
Liz Kammer2dd9ca42020-11-25 16:06:39 -08001813func RemoveAllOutputDir(path WritablePath) error {
1814 return os.RemoveAll(absolutePath(path.String()))
1815}
1816
1817func CreateOutputDirIfNonexistent(path WritablePath, perm os.FileMode) error {
1818 dir := absolutePath(path.String())
1819 if _, err := os.Stat(dir); os.IsNotExist(err) {
1820 return os.MkdirAll(dir, os.ModePerm)
1821 } else {
1822 return err
1823 }
1824}
1825
Colin Cross988414c2020-01-11 01:11:46 +00001826func absolutePath(path string) string {
1827 if filepath.IsAbs(path) {
1828 return path
1829 }
1830 return filepath.Join(absSrcDir, path)
1831}
Chris Parsons216e10a2020-07-09 17:12:52 -04001832
1833// A DataPath represents the path of a file to be used as data, for example
1834// a test library to be installed alongside a test.
1835// The data file should be installed (copied from `<SrcPath>`) to
1836// `<install_root>/<RelativeInstallPath>/<filename>`, or
1837// `<install_root>/<filename>` if RelativeInstallPath is empty.
1838type DataPath struct {
1839 // The path of the data file that should be copied into the data directory
1840 SrcPath Path
1841 // The install path of the data file, relative to the install root.
1842 RelativeInstallPath string
1843}