blob: 5a41cf16fcd3a400d36edfa3bf4200eacde265de [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
Dan Willemsen00269f22017-07-06 16:59:48 -070054type ModuleInstallPathContext interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -070055 BaseModuleContext
Dan Willemsen00269f22017-07-06 16:59:48 -070056
57 InstallInData() bool
Jaewoong Jung0949f312019-09-11 10:25:18 -070058 InstallInTestcases() bool
Dan Willemsen00269f22017-07-06 16:59:48 -070059 InstallInSanitizerDir() bool
Yifan Hong1b3348d2020-01-21 15:53:22 -080060 InstallInRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -070061 InstallInVendorRamdisk() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +090062 InstallInRecovery() bool
Colin Cross90ba5f42019-10-02 11:10:58 -070063 InstallInRoot() bool
Colin Cross607d8582019-07-29 16:44:46 -070064 InstallBypassMake() bool
Jiyong Park87788b52020-09-01 12:37:45 +090065 InstallForceOS() (*OsType, *ArchType)
Dan Willemsen00269f22017-07-06 16:59:48 -070066}
67
68var _ ModuleInstallPathContext = ModuleContext(nil)
69
Dan Willemsen34cc69e2015-09-23 15:26:20 -070070// errorfContext is the interface containing the Errorf method matching the
71// Errorf method in blueprint.SingletonContext.
72type errorfContext interface {
73 Errorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -080074}
75
Dan Willemsen34cc69e2015-09-23 15:26:20 -070076var _ errorfContext = blueprint.SingletonContext(nil)
77
78// moduleErrorf is the interface containing the ModuleErrorf method matching
79// the ModuleErrorf method in blueprint.ModuleContext.
80type moduleErrorf interface {
81 ModuleErrorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -080082}
83
Dan Willemsen34cc69e2015-09-23 15:26:20 -070084var _ moduleErrorf = blueprint.ModuleContext(nil)
85
Dan Willemsen34cc69e2015-09-23 15:26:20 -070086// reportPathError will register an error with the attached context. It
87// attempts ctx.ModuleErrorf for a better error message first, then falls
88// back to ctx.Errorf.
Colin Cross1ccfcc32018-02-22 13:54:26 -080089func reportPathError(ctx PathContext, err error) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +010090 ReportPathErrorf(ctx, "%s", err.Error())
Colin Cross1ccfcc32018-02-22 13:54:26 -080091}
92
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +010093// ReportPathErrorf will register an error with the attached context. It
Colin Cross1ccfcc32018-02-22 13:54:26 -080094// attempts ctx.ModuleErrorf for a better error message first, then falls
95// back to ctx.Errorf.
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +010096func ReportPathErrorf(ctx PathContext, format string, args ...interface{}) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070097 if mctx, ok := ctx.(moduleErrorf); ok {
98 mctx.ModuleErrorf(format, args...)
99 } else if ectx, ok := ctx.(errorfContext); ok {
100 ectx.Errorf(format, args...)
101 } else {
102 panic(fmt.Sprintf(format, args...))
Colin Crossf2298272015-05-12 11:36:53 -0700103 }
104}
105
Colin Cross5e708052019-08-06 13:59:50 -0700106func pathContextName(ctx PathContext, module blueprint.Module) string {
107 if x, ok := ctx.(interface{ ModuleName(blueprint.Module) string }); ok {
108 return x.ModuleName(module)
109 } else if x, ok := ctx.(interface{ OtherModuleName(blueprint.Module) string }); ok {
110 return x.OtherModuleName(module)
111 }
112 return "unknown"
113}
114
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700115type Path interface {
116 // Returns the path in string form
117 String() string
118
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700119 // Ext returns the extension of the last element of the path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700120 Ext() string
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700121
122 // Base returns the last element of the path
123 Base() string
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800124
125 // Rel returns the portion of the path relative to the directory it was created from. For
126 // example, Rel on a PathsForModuleSrc would return the path relative to the module source
Colin Cross0db55682017-12-05 15:36:55 -0800127 // directory, and OutputPath.Join("foo").Rel() would return "foo".
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800128 Rel() string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700129}
130
131// WritablePath is a type of path that can be used as an output for build rules.
132type WritablePath interface {
133 Path
134
Paul Duffin9b478b02019-12-10 13:41:51 +0000135 // return the path to the build directory.
136 buildDir() string
137
Jeff Gaston734e3802017-04-10 15:47:24 -0700138 // the writablePath method doesn't directly do anything,
139 // but it allows a struct to distinguish between whether or not it implements the WritablePath interface
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700140 writablePath()
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +0100141
142 ReplaceExtension(ctx PathContext, ext string) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700143}
144
145type genPathProvider interface {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700146 genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700147}
148type objPathProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700149 objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700150}
151type resPathProvider interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700152 resPathWithName(ctx ModuleContext, name string) ModuleResPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700153}
154
155// GenPathWithExt derives a new file path in ctx's generated sources directory
156// from the current path, but with the new extension.
Dan Willemsen21ec4902016-11-02 20:43:13 -0700157func GenPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700158 if path, ok := p.(genPathProvider); ok {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700159 return path.genPathWithExt(ctx, subdir, ext)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700160 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100161 ReportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700162 return PathForModuleGen(ctx)
163}
164
165// ObjPathWithExt derives a new file path in ctx's object directory from the
166// current path, but with the new extension.
Dan Willemsen21ec4902016-11-02 20:43:13 -0700167func ObjPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700168 if path, ok := p.(objPathProvider); ok {
169 return path.objPathWithExt(ctx, subdir, ext)
170 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100171 ReportPathErrorf(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700172 return PathForModuleObj(ctx)
173}
174
175// ResPathWithName derives a new path in ctx's output resource directory, using
176// the current path to create the directory name, and the `name` argument for
177// the filename.
Colin Cross635c3b02016-05-18 15:37:25 -0700178func ResPathWithName(ctx ModuleContext, p Path, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700179 if path, ok := p.(resPathProvider); ok {
180 return path.resPathWithName(ctx, name)
181 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100182 ReportPathErrorf(ctx, "Tried to create res file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700183 return PathForModuleRes(ctx)
184}
185
186// OptionalPath is a container that may or may not contain a valid Path.
187type OptionalPath struct {
188 valid bool
189 path Path
190}
191
192// OptionalPathForPath returns an OptionalPath containing the path.
193func OptionalPathForPath(path Path) OptionalPath {
194 if path == nil {
195 return OptionalPath{}
196 }
197 return OptionalPath{valid: true, path: path}
198}
199
200// Valid returns whether there is a valid path
201func (p OptionalPath) Valid() bool {
202 return p.valid
203}
204
205// Path returns the Path embedded in this OptionalPath. You must be sure that
206// there is a valid path, since this method will panic if there is not.
207func (p OptionalPath) Path() Path {
208 if !p.valid {
209 panic("Requesting an invalid path")
210 }
211 return p.path
212}
213
214// String returns the string version of the Path, or "" if it isn't valid.
215func (p OptionalPath) String() string {
216 if p.valid {
217 return p.path.String()
218 } else {
219 return ""
Colin Crossf2298272015-05-12 11:36:53 -0700220 }
221}
Colin Cross6e18ca42015-07-14 18:55:36 -0700222
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700223// Paths is a slice of Path objects, with helpers to operate on the collection.
224type Paths []Path
225
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000226func (paths Paths) containsPath(path Path) bool {
227 for _, p := range paths {
228 if p == path {
229 return true
230 }
231 }
232 return false
233}
234
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700235// PathsForSource returns Paths rooted from SrcDir
236func PathsForSource(ctx PathContext, paths []string) Paths {
237 ret := make(Paths, len(paths))
238 for i, path := range paths {
239 ret[i] = PathForSource(ctx, path)
240 }
241 return ret
242}
243
Jeff Gaston734e3802017-04-10 15:47:24 -0700244// ExistentPathsForSources returns a list of Paths rooted from SrcDir that are
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800245// found in the tree. If any are not found, they are omitted from the list,
246// and dependencies are added so that we're re-run when they are added.
Colin Cross32f38982018-02-22 11:47:25 -0800247func ExistentPathsForSources(ctx PathContext, paths []string) Paths {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800248 ret := make(Paths, 0, len(paths))
249 for _, path := range paths {
Colin Cross32f38982018-02-22 11:47:25 -0800250 p := ExistentPathForSource(ctx, path)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800251 if p.Valid() {
252 ret = append(ret, p.Path())
253 }
254 }
255 return ret
256}
257
Colin Cross41955e82019-05-29 14:40:35 -0700258// PathsForModuleSrc returns Paths rooted from the module's local source directory. It expands globs, references to
259// SourceFileProducer modules using the ":name" syntax, and references to OutputFileProducer modules using the
260// ":name{.tag}" syntax. Properties passed as the paths argument must have been annotated with struct tag
261// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
262// path_properties mutator. If ctx.Config().AllowMissingDependencies() is true then any missing SourceFileProducer or
263// OutputFileProducer dependencies will cause the module to be marked as having missing dependencies.
Colin Cross635c3b02016-05-18 15:37:25 -0700264func PathsForModuleSrc(ctx ModuleContext, paths []string) Paths {
Colin Cross8a497952019-03-05 22:25:09 -0800265 return PathsForModuleSrcExcludes(ctx, paths, nil)
266}
267
Colin Crossba71a3f2019-03-18 12:12:48 -0700268// PathsForModuleSrcExcludes returns Paths rooted from the module's local source directory, excluding paths listed in
Colin Cross41955e82019-05-29 14:40:35 -0700269// the excludes arguments. It expands globs, references to SourceFileProducer modules using the ":name" syntax, and
270// references to OutputFileProducer modules using the ":name{.tag}" syntax. Properties passed as the paths or excludes
271// argument must have been annotated with struct tag `android:"path"` so that dependencies on SourceFileProducer modules
272// will have already been handled by the path_properties mutator. If ctx.Config().AllowMissingDependencies() is
Paul Duffin036cace2019-07-25 14:44:56 +0100273// true then any missing SourceFileProducer or OutputFileProducer dependencies will cause the module to be marked as
Colin Cross41955e82019-05-29 14:40:35 -0700274// having missing dependencies.
Colin Cross8a497952019-03-05 22:25:09 -0800275func PathsForModuleSrcExcludes(ctx ModuleContext, paths, excludes []string) Paths {
Colin Crossba71a3f2019-03-18 12:12:48 -0700276 ret, missingDeps := PathsAndMissingDepsForModuleSrcExcludes(ctx, paths, excludes)
277 if ctx.Config().AllowMissingDependencies() {
278 ctx.AddMissingDependencies(missingDeps)
279 } else {
280 for _, m := range missingDeps {
281 ctx.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m)
282 }
283 }
284 return ret
285}
286
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000287// OutputPaths is a slice of OutputPath objects, with helpers to operate on the collection.
288type OutputPaths []OutputPath
289
290// Paths returns the OutputPaths as a Paths
291func (p OutputPaths) Paths() Paths {
292 if p == nil {
293 return nil
294 }
295 ret := make(Paths, len(p))
296 for i, path := range p {
297 ret[i] = path
298 }
299 return ret
300}
301
302// Strings returns the string forms of the writable paths.
303func (p OutputPaths) Strings() []string {
304 if p == nil {
305 return nil
306 }
307 ret := make([]string, len(p))
308 for i, path := range p {
309 ret[i] = path.String()
310 }
311 return ret
312}
313
Colin Crossba71a3f2019-03-18 12:12:48 -0700314// PathsAndMissingDepsForModuleSrcExcludes returns Paths rooted from the module's local source directory, excluding
Colin Cross41955e82019-05-29 14:40:35 -0700315// paths listed in the excludes arguments, and a list of missing dependencies. It expands globs, references to
316// SourceFileProducer modules using the ":name" syntax, and references to OutputFileProducer modules using the
317// ":name{.tag}" syntax. Properties passed as the paths or excludes argument must have been annotated with struct tag
318// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
319// path_properties mutator. If ctx.Config().AllowMissingDependencies() is true then any missing SourceFileProducer or
320// OutputFileProducer dependencies will be returned, and they will NOT cause the module to be marked as having missing
321// dependencies.
Colin Crossba71a3f2019-03-18 12:12:48 -0700322func PathsAndMissingDepsForModuleSrcExcludes(ctx ModuleContext, paths, excludes []string) (Paths, []string) {
Colin Cross8a497952019-03-05 22:25:09 -0800323 prefix := pathForModuleSrc(ctx).String()
324
325 var expandedExcludes []string
326 if excludes != nil {
327 expandedExcludes = make([]string, 0, len(excludes))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700328 }
Colin Cross8a497952019-03-05 22:25:09 -0800329
Colin Crossba71a3f2019-03-18 12:12:48 -0700330 var missingExcludeDeps []string
331
Colin Cross8a497952019-03-05 22:25:09 -0800332 for _, e := range excludes {
Colin Cross41955e82019-05-29 14:40:35 -0700333 if m, t := SrcIsModuleWithTag(e); m != "" {
334 module := ctx.GetDirectDepWithTag(m, sourceOrOutputDepTag(t))
Colin Cross8a497952019-03-05 22:25:09 -0800335 if module == nil {
Colin Crossba71a3f2019-03-18 12:12:48 -0700336 missingExcludeDeps = append(missingExcludeDeps, m)
Colin Cross8a497952019-03-05 22:25:09 -0800337 continue
338 }
Colin Cross41955e82019-05-29 14:40:35 -0700339 if outProducer, ok := module.(OutputFileProducer); ok {
340 outputFiles, err := outProducer.OutputFiles(t)
341 if err != nil {
342 ctx.ModuleErrorf("path dependency %q: %s", e, err)
343 }
344 expandedExcludes = append(expandedExcludes, outputFiles.Strings()...)
345 } else if t != "" {
346 ctx.ModuleErrorf("path dependency %q is not an output file producing module", e)
347 } else if srcProducer, ok := module.(SourceFileProducer); ok {
Colin Cross8a497952019-03-05 22:25:09 -0800348 expandedExcludes = append(expandedExcludes, srcProducer.Srcs().Strings()...)
349 } else {
Colin Cross41955e82019-05-29 14:40:35 -0700350 ctx.ModuleErrorf("path dependency %q is not a source file producing module", e)
Colin Cross8a497952019-03-05 22:25:09 -0800351 }
352 } else {
353 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
354 }
355 }
356
357 if paths == nil {
Colin Crossba71a3f2019-03-18 12:12:48 -0700358 return nil, missingExcludeDeps
Colin Cross8a497952019-03-05 22:25:09 -0800359 }
360
Colin Crossba71a3f2019-03-18 12:12:48 -0700361 var missingDeps []string
362
Colin Cross8a497952019-03-05 22:25:09 -0800363 expandedSrcFiles := make(Paths, 0, len(paths))
364 for _, s := range paths {
365 srcFiles, err := expandOneSrcPath(ctx, s, expandedExcludes)
366 if depErr, ok := err.(missingDependencyError); ok {
Colin Crossba71a3f2019-03-18 12:12:48 -0700367 missingDeps = append(missingDeps, depErr.missingDeps...)
Colin Cross8a497952019-03-05 22:25:09 -0800368 } else if err != nil {
369 reportPathError(ctx, err)
370 }
371 expandedSrcFiles = append(expandedSrcFiles, srcFiles...)
372 }
Colin Crossba71a3f2019-03-18 12:12:48 -0700373
374 return expandedSrcFiles, append(missingDeps, missingExcludeDeps...)
Colin Cross8a497952019-03-05 22:25:09 -0800375}
376
377type missingDependencyError struct {
378 missingDeps []string
379}
380
381func (e missingDependencyError) Error() string {
382 return "missing dependencies: " + strings.Join(e.missingDeps, ", ")
383}
384
385func expandOneSrcPath(ctx ModuleContext, s string, expandedExcludes []string) (Paths, error) {
Jooyung Han7607dd32020-07-05 10:23:14 +0900386 excludePaths := func(paths Paths) Paths {
387 if len(expandedExcludes) == 0 {
388 return paths
389 }
390 remainder := make(Paths, 0, len(paths))
391 for _, p := range paths {
392 if !InList(p.String(), expandedExcludes) {
393 remainder = append(remainder, p)
394 }
395 }
396 return remainder
397 }
Colin Cross41955e82019-05-29 14:40:35 -0700398 if m, t := SrcIsModuleWithTag(s); m != "" {
399 module := ctx.GetDirectDepWithTag(m, sourceOrOutputDepTag(t))
Colin Cross8a497952019-03-05 22:25:09 -0800400 if module == nil {
401 return nil, missingDependencyError{[]string{m}}
402 }
Colin Cross41955e82019-05-29 14:40:35 -0700403 if outProducer, ok := module.(OutputFileProducer); ok {
404 outputFiles, err := outProducer.OutputFiles(t)
405 if err != nil {
406 return nil, fmt.Errorf("path dependency %q: %s", s, err)
407 }
Jooyung Han7607dd32020-07-05 10:23:14 +0900408 return excludePaths(outputFiles), nil
Colin Cross41955e82019-05-29 14:40:35 -0700409 } else if t != "" {
410 return nil, fmt.Errorf("path dependency %q is not an output file producing module", s)
411 } else if srcProducer, ok := module.(SourceFileProducer); ok {
Jooyung Han7607dd32020-07-05 10:23:14 +0900412 return excludePaths(srcProducer.Srcs()), nil
Colin Cross8a497952019-03-05 22:25:09 -0800413 } else {
Colin Cross41955e82019-05-29 14:40:35 -0700414 return nil, fmt.Errorf("path dependency %q is not a source file producing module", s)
Colin Cross8a497952019-03-05 22:25:09 -0800415 }
416 } else if pathtools.IsGlob(s) {
417 paths := ctx.GlobFiles(pathForModuleSrc(ctx, s).String(), expandedExcludes)
418 return PathsWithModuleSrcSubDir(ctx, paths, ""), nil
419 } else {
420 p := pathForModuleSrc(ctx, s)
Colin Cross988414c2020-01-11 01:11:46 +0000421 if exists, _, err := ctx.Config().fs.Exists(p.String()); err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100422 ReportPathErrorf(ctx, "%s: %s", p, err.Error())
Colin Cross5e6a7972020-06-07 16:56:32 -0700423 } else if !exists && !ctx.Config().testAllowNonExistentPaths {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100424 ReportPathErrorf(ctx, "module source path %q does not exist", p)
Colin Cross8a497952019-03-05 22:25:09 -0800425 }
426
Jooyung Han7607dd32020-07-05 10:23:14 +0900427 if InList(p.String(), expandedExcludes) {
Colin Cross8a497952019-03-05 22:25:09 -0800428 return nil, nil
429 }
430 return Paths{p}, nil
431 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700432}
433
434// pathsForModuleSrcFromFullPath returns Paths rooted from the module's local
435// source directory, but strip the local source directory from the beginning of
Dan Willemsen540a78c2018-02-26 21:50:08 -0800436// each string. If incDirs is false, strip paths with a trailing '/' from the list.
Colin Crossfe4bc362018-09-12 10:02:13 -0700437// It intended for use in globs that only list files that exist, so it allows '$' in
438// filenames.
Colin Cross1184b642019-12-30 18:43:07 -0800439func pathsForModuleSrcFromFullPath(ctx EarlyModuleContext, paths []string, incDirs bool) Paths {
Colin Cross6510f912017-11-29 00:27:14 -0800440 prefix := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir()) + "/"
Colin Cross0f37af02017-09-27 17:42:05 -0700441 if prefix == "./" {
442 prefix = ""
443 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700444 ret := make(Paths, 0, len(paths))
445 for _, p := range paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -0800446 if !incDirs && strings.HasSuffix(p, "/") {
447 continue
448 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700449 path := filepath.Clean(p)
450 if !strings.HasPrefix(path, prefix) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100451 ReportPathErrorf(ctx, "Path %q is not in module source directory %q", p, prefix)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700452 continue
453 }
Colin Crosse3924e12018-08-15 20:18:53 -0700454
Colin Crossfe4bc362018-09-12 10:02:13 -0700455 srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):])
Colin Crosse3924e12018-08-15 20:18:53 -0700456 if err != nil {
457 reportPathError(ctx, err)
458 continue
459 }
460
Colin Cross07e51612019-03-05 12:46:40 -0800461 srcPath.basePath.rel = srcPath.path
Colin Crosse3924e12018-08-15 20:18:53 -0700462
Colin Cross07e51612019-03-05 12:46:40 -0800463 ret = append(ret, srcPath)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700464 }
465 return ret
466}
467
468// PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's
Colin Cross0ddae7f2019-02-07 15:30:01 -0800469// local source directory. If input is nil, use the default if it exists. If input is empty, returns nil.
Colin Cross635c3b02016-05-18 15:37:25 -0700470func PathsWithOptionalDefaultForModuleSrc(ctx ModuleContext, input []string, def string) Paths {
Colin Cross0ddae7f2019-02-07 15:30:01 -0800471 if input != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700472 return PathsForModuleSrc(ctx, input)
473 }
474 // Use Glob so that if the default doesn't exist, a dependency is added so that when it
475 // is created, we're run again.
Colin Cross6510f912017-11-29 00:27:14 -0800476 path := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir(), def)
Colin Cross461b4452018-02-23 09:22:42 -0800477 return ctx.Glob(path, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700478}
479
480// Strings returns the Paths in string form
481func (p Paths) Strings() []string {
482 if p == nil {
483 return nil
484 }
485 ret := make([]string, len(p))
486 for i, path := range p {
487 ret[i] = path.String()
488 }
489 return ret
490}
491
Colin Crossc0efd1d2020-07-03 11:56:24 -0700492func CopyOfPaths(paths Paths) Paths {
493 return append(Paths(nil), paths...)
494}
495
Colin Crossb6715442017-10-24 11:13:31 -0700496// FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It
497// modifies the Paths slice contents in place, and returns a subslice of the original slice.
Dan Willemsenfe92c962017-08-29 12:28:37 -0700498func FirstUniquePaths(list Paths) Paths {
Colin Cross27027c72020-02-28 15:34:17 -0800499 // 128 was chosen based on BenchmarkFirstUniquePaths results.
500 if len(list) > 128 {
501 return firstUniquePathsMap(list)
502 }
503 return firstUniquePathsList(list)
504}
505
Colin Crossc0efd1d2020-07-03 11:56:24 -0700506// SortedUniquePaths returns all unique elements of a Paths in sorted order. It modifies the
507// Paths slice contents in place, and returns a subslice of the original slice.
Jiyong Park33c77362020-05-29 22:00:16 +0900508func SortedUniquePaths(list Paths) Paths {
509 unique := FirstUniquePaths(list)
510 sort.Slice(unique, func(i, j int) bool {
511 return unique[i].String() < unique[j].String()
512 })
513 return unique
514}
515
Colin Cross27027c72020-02-28 15:34:17 -0800516func firstUniquePathsList(list Paths) Paths {
Dan Willemsenfe92c962017-08-29 12:28:37 -0700517 k := 0
518outer:
519 for i := 0; i < len(list); i++ {
520 for j := 0; j < k; j++ {
521 if list[i] == list[j] {
522 continue outer
523 }
524 }
525 list[k] = list[i]
526 k++
527 }
528 return list[:k]
529}
530
Colin Cross27027c72020-02-28 15:34:17 -0800531func firstUniquePathsMap(list Paths) Paths {
532 k := 0
533 seen := make(map[Path]bool, len(list))
534 for i := 0; i < len(list); i++ {
535 if seen[list[i]] {
536 continue
537 }
538 seen[list[i]] = true
539 list[k] = list[i]
540 k++
541 }
542 return list[:k]
543}
544
Colin Crossb6715442017-10-24 11:13:31 -0700545// LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It
546// modifies the Paths slice contents in place, and returns a subslice of the original slice.
547func LastUniquePaths(list Paths) Paths {
548 totalSkip := 0
549 for i := len(list) - 1; i >= totalSkip; i-- {
550 skip := 0
551 for j := i - 1; j >= totalSkip; j-- {
552 if list[i] == list[j] {
553 skip++
554 } else {
555 list[j+skip] = list[j]
556 }
557 }
558 totalSkip += skip
559 }
560 return list[totalSkip:]
561}
562
Colin Crossa140bb02018-04-17 10:52:26 -0700563// ReversePaths returns a copy of a Paths in reverse order.
564func ReversePaths(list Paths) Paths {
565 if list == nil {
566 return nil
567 }
568 ret := make(Paths, len(list))
569 for i := range list {
570 ret[i] = list[len(list)-1-i]
571 }
572 return ret
573}
574
Jeff Gaston294356f2017-09-27 17:05:30 -0700575func indexPathList(s Path, list []Path) int {
576 for i, l := range list {
577 if l == s {
578 return i
579 }
580 }
581
582 return -1
583}
584
585func inPathList(p Path, list []Path) bool {
586 return indexPathList(p, list) != -1
587}
588
589func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) {
Paul Duffin57b9e1d2019-12-13 00:03:35 +0000590 return FilterPathListPredicate(list, func(p Path) bool { return inPathList(p, filter) })
591}
592
593func FilterPathListPredicate(list []Path, predicate func(Path) bool) (remainder []Path, filtered []Path) {
Jeff Gaston294356f2017-09-27 17:05:30 -0700594 for _, l := range list {
Paul Duffin57b9e1d2019-12-13 00:03:35 +0000595 if predicate(l) {
Jeff Gaston294356f2017-09-27 17:05:30 -0700596 filtered = append(filtered, l)
597 } else {
598 remainder = append(remainder, l)
599 }
600 }
601
602 return
603}
604
Colin Cross93e85952017-08-15 13:34:18 -0700605// HasExt returns true of any of the paths have extension ext, otherwise false
606func (p Paths) HasExt(ext string) bool {
607 for _, path := range p {
608 if path.Ext() == ext {
609 return true
610 }
611 }
612
613 return false
614}
615
616// FilterByExt returns the subset of the paths that have extension ext
617func (p Paths) FilterByExt(ext string) Paths {
618 ret := make(Paths, 0, len(p))
619 for _, path := range p {
620 if path.Ext() == ext {
621 ret = append(ret, path)
622 }
623 }
624 return ret
625}
626
627// FilterOutByExt returns the subset of the paths that do not have extension ext
628func (p Paths) FilterOutByExt(ext string) Paths {
629 ret := make(Paths, 0, len(p))
630 for _, path := range p {
631 if path.Ext() != ext {
632 ret = append(ret, path)
633 }
634 }
635 return ret
636}
637
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700638// DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory
639// (including subdirectories) are in a contiguous subslice of the list, and can be found in
640// O(log(N)) time using a binary search on the directory prefix.
641type DirectorySortedPaths Paths
642
643func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths {
644 ret := append(DirectorySortedPaths(nil), paths...)
645 sort.Slice(ret, func(i, j int) bool {
646 return ret[i].String() < ret[j].String()
647 })
648 return ret
649}
650
651// PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries
652// that are in the specified directory and its subdirectories.
653func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths {
654 prefix := filepath.Clean(dir) + "/"
655 start := sort.Search(len(p), func(i int) bool {
656 return prefix < p[i].String()
657 })
658
659 ret := p[start:]
660
661 end := sort.Search(len(ret), func(i int) bool {
662 return !strings.HasPrefix(ret[i].String(), prefix)
663 })
664
665 ret = ret[:end]
666
667 return Paths(ret)
668}
669
Alex Humesky29e3bbe2020-11-20 21:30:13 -0500670// WritablePaths is a slice of WritablePath, used for multiple outputs.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700671type WritablePaths []WritablePath
672
673// Strings returns the string forms of the writable paths.
674func (p WritablePaths) Strings() []string {
675 if p == nil {
676 return nil
677 }
678 ret := make([]string, len(p))
679 for i, path := range p {
680 ret[i] = path.String()
681 }
682 return ret
683}
684
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800685// Paths returns the WritablePaths as a Paths
686func (p WritablePaths) Paths() Paths {
687 if p == nil {
688 return nil
689 }
690 ret := make(Paths, len(p))
691 for i, path := range p {
692 ret[i] = path
693 }
694 return ret
695}
696
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700697type basePath struct {
698 path string
699 config Config
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800700 rel string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700701}
702
703func (p basePath) Ext() string {
704 return filepath.Ext(p.path)
705}
706
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700707func (p basePath) Base() string {
708 return filepath.Base(p.path)
709}
710
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800711func (p basePath) Rel() string {
712 if p.rel != "" {
713 return p.rel
714 }
715 return p.path
716}
717
Colin Cross0875c522017-11-28 17:34:01 -0800718func (p basePath) String() string {
719 return p.path
720}
721
Colin Cross0db55682017-12-05 15:36:55 -0800722func (p basePath) withRel(rel string) basePath {
723 p.path = filepath.Join(p.path, rel)
724 p.rel = rel
725 return p
726}
727
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700728// SourcePath is a Path representing a file path rooted from SrcDir
729type SourcePath struct {
730 basePath
731}
732
733var _ Path = SourcePath{}
734
Colin Cross0db55682017-12-05 15:36:55 -0800735func (p SourcePath) withRel(rel string) SourcePath {
736 p.basePath = p.basePath.withRel(rel)
737 return p
738}
739
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700740// safePathForSource is for paths that we expect are safe -- only for use by go
741// code that is embedding ninja variables in paths
Colin Crossfe4bc362018-09-12 10:02:13 -0700742func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
743 p, err := validateSafePath(pathComponents...)
Colin Crossaabf6792017-11-29 00:27:14 -0800744 ret := SourcePath{basePath{p, ctx.Config(), ""}}
Colin Crossfe4bc362018-09-12 10:02:13 -0700745 if err != nil {
746 return ret, err
747 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700748
Colin Cross7b3dcc32019-01-24 13:14:39 -0800749 // absolute path already checked by validateSafePath
750 if strings.HasPrefix(ret.String(), ctx.Config().buildDir) {
Mikhail Naganovab1f5182019-02-08 13:17:55 -0800751 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Cross6e18ca42015-07-14 18:55:36 -0700752 }
753
Colin Crossfe4bc362018-09-12 10:02:13 -0700754 return ret, err
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700755}
756
Colin Cross192e97a2018-02-22 14:21:02 -0800757// pathForSource creates a SourcePath from pathComponents, but does not check that it exists.
758func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
Colin Crossc48c1432018-02-23 07:09:01 +0000759 p, err := validatePath(pathComponents...)
760 ret := SourcePath{basePath{p, ctx.Config(), ""}}
Colin Cross94a32102018-02-22 14:21:02 -0800761 if err != nil {
Colin Cross192e97a2018-02-22 14:21:02 -0800762 return ret, err
Colin Cross94a32102018-02-22 14:21:02 -0800763 }
764
Colin Cross7b3dcc32019-01-24 13:14:39 -0800765 // absolute path already checked by validatePath
766 if strings.HasPrefix(ret.String(), ctx.Config().buildDir) {
Mikhail Naganovab1f5182019-02-08 13:17:55 -0800767 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Crossc48c1432018-02-23 07:09:01 +0000768 }
769
Colin Cross192e97a2018-02-22 14:21:02 -0800770 return ret, nil
771}
772
773// existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the
774// path does not exist.
775func existsWithDependencies(ctx PathContext, path SourcePath) (exists bool, err error) {
776 var files []string
777
778 if gctx, ok := ctx.(PathGlobContext); ok {
779 // Use glob to produce proper dependencies, even though we only want
780 // a single file.
781 files, err = gctx.GlobWithDeps(path.String(), nil)
782 } else {
783 var deps []string
784 // We cannot add build statements in this context, so we fall back to
785 // AddNinjaFileDeps
Colin Cross988414c2020-01-11 01:11:46 +0000786 files, deps, err = ctx.Config().fs.Glob(path.String(), nil, pathtools.FollowSymlinks)
Colin Cross192e97a2018-02-22 14:21:02 -0800787 ctx.AddNinjaFileDeps(deps...)
788 }
789
790 if err != nil {
791 return false, fmt.Errorf("glob: %s", err.Error())
792 }
793
794 return len(files) > 0, nil
795}
796
797// PathForSource joins the provided path components and validates that the result
798// neither escapes the source dir nor is in the out dir.
799// On error, it will return a usable, but invalid SourcePath, and report a ModuleError.
800func PathForSource(ctx PathContext, pathComponents ...string) SourcePath {
801 path, err := pathForSource(ctx, pathComponents...)
802 if err != nil {
803 reportPathError(ctx, err)
804 }
805
Colin Crosse3924e12018-08-15 20:18:53 -0700806 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100807 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -0700808 }
809
Colin Cross192e97a2018-02-22 14:21:02 -0800810 if modCtx, ok := ctx.(ModuleContext); ok && ctx.Config().AllowMissingDependencies() {
811 exists, err := existsWithDependencies(ctx, path)
812 if err != nil {
813 reportPathError(ctx, err)
814 }
815 if !exists {
816 modCtx.AddMissingDependencies([]string{path.String()})
817 }
Colin Cross988414c2020-01-11 01:11:46 +0000818 } else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100819 ReportPathErrorf(ctx, "%s: %s", path, err.Error())
Colin Cross5e6a7972020-06-07 16:56:32 -0700820 } else if !exists && !ctx.Config().testAllowNonExistentPaths {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100821 ReportPathErrorf(ctx, "source path %q does not exist", path)
Colin Cross192e97a2018-02-22 14:21:02 -0800822 }
823 return path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700824}
825
Jeff Gaston734e3802017-04-10 15:47:24 -0700826// ExistentPathForSource returns an OptionalPath with the SourcePath if the
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700827// path exists, or an empty OptionalPath if it doesn't exist. Dependencies are added
828// so that the ninja file will be regenerated if the state of the path changes.
Colin Cross32f38982018-02-22 11:47:25 -0800829func ExistentPathForSource(ctx PathContext, pathComponents ...string) OptionalPath {
Colin Cross192e97a2018-02-22 14:21:02 -0800830 path, err := pathForSource(ctx, pathComponents...)
Colin Cross1ccfcc32018-02-22 13:54:26 -0800831 if err != nil {
832 reportPathError(ctx, err)
833 return OptionalPath{}
834 }
Colin Crossc48c1432018-02-23 07:09:01 +0000835
Colin Crosse3924e12018-08-15 20:18:53 -0700836 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100837 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -0700838 return OptionalPath{}
839 }
840
Colin Cross192e97a2018-02-22 14:21:02 -0800841 exists, err := existsWithDependencies(ctx, path)
Colin Crossc48c1432018-02-23 07:09:01 +0000842 if err != nil {
843 reportPathError(ctx, err)
844 return OptionalPath{}
845 }
Colin Cross192e97a2018-02-22 14:21:02 -0800846 if !exists {
Colin Crossc48c1432018-02-23 07:09:01 +0000847 return OptionalPath{}
848 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700849 return OptionalPathForPath(path)
850}
851
852func (p SourcePath) String() string {
853 return filepath.Join(p.config.srcDir, p.path)
854}
855
856// Join creates a new SourcePath with paths... joined with the current path. The
857// provided paths... may not use '..' to escape from the current path.
858func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800859 path, err := validatePath(paths...)
860 if err != nil {
861 reportPathError(ctx, err)
862 }
Colin Cross0db55682017-12-05 15:36:55 -0800863 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700864}
865
Colin Cross2fafa3e2019-03-05 12:39:51 -0800866// join is like Join but does less path validation.
867func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath {
868 path, err := validateSafePath(paths...)
869 if err != nil {
870 reportPathError(ctx, err)
871 }
872 return p.withRel(path)
873}
874
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700875// OverlayPath returns the overlay for `path' if it exists. This assumes that the
876// SourcePath is the path to a resource overlay directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700877func (p SourcePath) OverlayPath(ctx ModuleContext, path Path) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700878 var relDir string
Colin Cross07e51612019-03-05 12:46:40 -0800879 if srcPath, ok := path.(SourcePath); ok {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700880 relDir = srcPath.path
881 } else {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100882 ReportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700883 return OptionalPath{}
884 }
885 dir := filepath.Join(p.config.srcDir, p.path, relDir)
886 // Use Glob so that we are run again if the directory is added.
Colin Cross7f19f372016-11-01 11:10:25 -0700887 if pathtools.IsGlob(dir) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100888 ReportPathErrorf(ctx, "Path may not contain a glob: %s", dir)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800889 }
Colin Cross461b4452018-02-23 09:22:42 -0800890 paths, err := ctx.GlobWithDeps(dir, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700891 if err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100892 ReportPathErrorf(ctx, "glob: %s", err.Error())
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700893 return OptionalPath{}
894 }
895 if len(paths) == 0 {
896 return OptionalPath{}
897 }
Colin Cross43f08db2018-11-12 10:13:39 -0800898 relPath := Rel(ctx, p.config.srcDir, paths[0])
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700899 return OptionalPathForPath(PathForSource(ctx, relPath))
900}
901
Colin Cross70dda7e2019-10-01 22:05:35 -0700902// OutputPath is a Path representing an intermediates file path rooted from the build directory
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700903type OutputPath struct {
904 basePath
Colin Crossd63c9a72020-01-29 16:52:50 -0800905 fullPath string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700906}
907
Colin Cross702e0f82017-10-18 17:27:54 -0700908func (p OutputPath) withRel(rel string) OutputPath {
Colin Cross0db55682017-12-05 15:36:55 -0800909 p.basePath = p.basePath.withRel(rel)
Colin Crossd63c9a72020-01-29 16:52:50 -0800910 p.fullPath = filepath.Join(p.fullPath, rel)
Colin Cross702e0f82017-10-18 17:27:54 -0700911 return p
912}
913
Colin Cross3063b782018-08-15 11:19:12 -0700914func (p OutputPath) WithoutRel() OutputPath {
915 p.basePath.rel = filepath.Base(p.basePath.path)
916 return p
917}
918
Paul Duffin9b478b02019-12-10 13:41:51 +0000919func (p OutputPath) buildDir() string {
920 return p.config.buildDir
921}
922
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700923var _ Path = OutputPath{}
Paul Duffin9b478b02019-12-10 13:41:51 +0000924var _ WritablePath = OutputPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700925
Chris Parsons8f232a22020-06-23 17:37:05 -0400926// toolDepPath is a Path representing a dependency of the build tool.
927type toolDepPath struct {
928 basePath
929}
930
931var _ Path = toolDepPath{}
932
933// pathForBuildToolDep returns a toolDepPath representing the given path string.
934// There is no validation for the path, as it is "trusted": It may fail
935// normal validation checks. For example, it may be an absolute path.
936// Only use this function to construct paths for dependencies of the build
937// tool invocation.
938func pathForBuildToolDep(ctx PathContext, path string) toolDepPath {
939 return toolDepPath{basePath{path, ctx.Config(), ""}}
940}
941
Jeff Gaston734e3802017-04-10 15:47:24 -0700942// PathForOutput joins the provided paths and returns an OutputPath that is
943// validated to not escape the build dir.
944// On error, it will return a usable, but invalid OutputPath, and report a ModuleError.
945func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800946 path, err := validatePath(pathComponents...)
947 if err != nil {
948 reportPathError(ctx, err)
949 }
Colin Crossd63c9a72020-01-29 16:52:50 -0800950 fullPath := filepath.Join(ctx.Config().buildDir, path)
951 path = fullPath[len(fullPath)-len(path):]
952 return OutputPath{basePath{path, ctx.Config(), ""}, fullPath}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700953}
954
Colin Cross40e33732019-02-15 11:08:35 -0800955// PathsForOutput returns Paths rooted from buildDir
956func PathsForOutput(ctx PathContext, paths []string) WritablePaths {
957 ret := make(WritablePaths, len(paths))
958 for i, path := range paths {
959 ret[i] = PathForOutput(ctx, path)
960 }
961 return ret
962}
963
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700964func (p OutputPath) writablePath() {}
965
966func (p OutputPath) String() string {
Colin Crossd63c9a72020-01-29 16:52:50 -0800967 return p.fullPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700968}
969
970// Join creates a new OutputPath with paths... joined with the current path. The
971// provided paths... may not use '..' to escape from the current path.
972func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800973 path, err := validatePath(paths...)
974 if err != nil {
975 reportPathError(ctx, err)
976 }
Colin Cross0db55682017-12-05 15:36:55 -0800977 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700978}
979
Colin Cross8854a5a2019-02-11 14:14:16 -0800980// ReplaceExtension creates a new OutputPath with the extension replaced with ext.
981func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
982 if strings.Contains(ext, "/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100983 ReportPathErrorf(ctx, "extension %q cannot contain /", ext)
Colin Cross8854a5a2019-02-11 14:14:16 -0800984 }
985 ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext))
Colin Cross2cdd5df2019-02-25 10:25:24 -0800986 ret.rel = pathtools.ReplaceExtension(p.rel, ext)
Colin Cross8854a5a2019-02-11 14:14:16 -0800987 return ret
988}
989
Colin Cross40e33732019-02-15 11:08:35 -0800990// InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths.
991func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath {
992 path, err := validatePath(paths...)
993 if err != nil {
994 reportPathError(ctx, err)
995 }
996
997 ret := PathForOutput(ctx, filepath.Dir(p.path), path)
Colin Cross2cdd5df2019-02-25 10:25:24 -0800998 ret.rel = filepath.Join(filepath.Dir(p.rel), path)
Colin Cross40e33732019-02-15 11:08:35 -0800999 return ret
1000}
1001
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001002// PathForIntermediates returns an OutputPath representing the top-level
1003// intermediates directory.
1004func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001005 path, err := validatePath(paths...)
1006 if err != nil {
1007 reportPathError(ctx, err)
1008 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001009 return PathForOutput(ctx, ".intermediates", path)
1010}
1011
Colin Cross07e51612019-03-05 12:46:40 -08001012var _ genPathProvider = SourcePath{}
1013var _ objPathProvider = SourcePath{}
1014var _ resPathProvider = SourcePath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001015
Colin Cross07e51612019-03-05 12:46:40 -08001016// PathForModuleSrc returns a Path representing the paths... under the
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001017// module's local source directory.
Colin Cross8a497952019-03-05 22:25:09 -08001018func PathForModuleSrc(ctx ModuleContext, pathComponents ...string) Path {
1019 p, err := validatePath(pathComponents...)
1020 if err != nil {
1021 reportPathError(ctx, err)
Colin Cross192e97a2018-02-22 14:21:02 -08001022 }
Colin Cross8a497952019-03-05 22:25:09 -08001023 paths, err := expandOneSrcPath(ctx, p, nil)
1024 if err != nil {
1025 if depErr, ok := err.(missingDependencyError); ok {
1026 if ctx.Config().AllowMissingDependencies() {
1027 ctx.AddMissingDependencies(depErr.missingDeps)
1028 } else {
1029 ctx.ModuleErrorf(`%s, is the property annotated with android:"path"?`, depErr.Error())
1030 }
1031 } else {
1032 reportPathError(ctx, err)
1033 }
1034 return nil
1035 } else if len(paths) == 0 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001036 ReportPathErrorf(ctx, "%q produced no files, expected exactly one", p)
Colin Cross8a497952019-03-05 22:25:09 -08001037 return nil
1038 } else if len(paths) > 1 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001039 ReportPathErrorf(ctx, "%q produced %d files, expected exactly one", p, len(paths))
Colin Cross8a497952019-03-05 22:25:09 -08001040 }
1041 return paths[0]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001042}
1043
Colin Cross07e51612019-03-05 12:46:40 -08001044func pathForModuleSrc(ctx ModuleContext, paths ...string) SourcePath {
1045 p, err := validatePath(paths...)
1046 if err != nil {
1047 reportPathError(ctx, err)
1048 }
1049
1050 path, err := pathForSource(ctx, ctx.ModuleDir(), p)
1051 if err != nil {
1052 reportPathError(ctx, err)
1053 }
1054
1055 path.basePath.rel = p
1056
1057 return path
1058}
1059
Colin Cross2fafa3e2019-03-05 12:39:51 -08001060// PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path
1061// will return the path relative to subDir in the module's source directory. If any input paths are not located
1062// inside subDir then a path error will be reported.
1063func PathsWithModuleSrcSubDir(ctx ModuleContext, paths Paths, subDir string) Paths {
1064 paths = append(Paths(nil), paths...)
Colin Cross07e51612019-03-05 12:46:40 -08001065 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001066 for i, path := range paths {
1067 rel := Rel(ctx, subDirFullPath.String(), path.String())
1068 paths[i] = subDirFullPath.join(ctx, rel)
1069 }
1070 return paths
1071}
1072
1073// PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the
1074// module's source directory. If the input path is not located inside subDir then a path error will be reported.
1075func PathWithModuleSrcSubDir(ctx ModuleContext, path Path, subDir string) Path {
Colin Cross07e51612019-03-05 12:46:40 -08001076 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001077 rel := Rel(ctx, subDirFullPath.String(), path.String())
1078 return subDirFullPath.Join(ctx, rel)
1079}
1080
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001081// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
1082// valid path if p is non-nil.
Colin Cross635c3b02016-05-18 15:37:25 -07001083func OptionalPathForModuleSrc(ctx ModuleContext, p *string) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001084 if p == nil {
1085 return OptionalPath{}
1086 }
1087 return OptionalPathForPath(PathForModuleSrc(ctx, *p))
1088}
1089
Colin Cross07e51612019-03-05 12:46:40 -08001090func (p SourcePath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001091 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001092}
1093
Colin Cross07e51612019-03-05 12:46:40 -08001094func (p SourcePath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001095 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001096}
1097
Colin Cross07e51612019-03-05 12:46:40 -08001098func (p SourcePath) resPathWithName(ctx ModuleContext, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001099 // TODO: Use full directory if the new ctx is not the current ctx?
1100 return PathForModuleRes(ctx, p.path, name)
1101}
1102
1103// ModuleOutPath is a Path representing a module's output directory.
1104type ModuleOutPath struct {
1105 OutputPath
1106}
1107
1108var _ Path = ModuleOutPath{}
1109
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01001110func (p ModuleOutPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
1111 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1112}
1113
Colin Cross702e0f82017-10-18 17:27:54 -07001114func pathForModule(ctx ModuleContext) OutputPath {
1115 return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir())
1116}
1117
Logan Chien7eefdc42018-07-11 18:10:41 +08001118// PathForVndkRefAbiDump returns an OptionalPath representing the path of the
1119// reference abi dump for the given module. This is not guaranteed to be valid.
1120func PathForVndkRefAbiDump(ctx ModuleContext, version, fileName string,
Hsin-Yi Chen53489642019-07-31 17:10:45 +08001121 isNdk, isLlndkOrVndk, isGzip bool) OptionalPath {
Logan Chien7eefdc42018-07-11 18:10:41 +08001122
Jayant Chowdharyac066c62018-02-20 10:53:31 -08001123 arches := ctx.DeviceConfig().Arches()
Logan Chien7eefdc42018-07-11 18:10:41 +08001124 if len(arches) == 0 {
1125 panic("device build with no primary arch")
1126 }
Jayant Chowdharyac066c62018-02-20 10:53:31 -08001127 currentArch := ctx.Arch()
1128 archNameAndVariant := currentArch.ArchType.String()
1129 if currentArch.ArchVariant != "" {
1130 archNameAndVariant += "_" + currentArch.ArchVariant
1131 }
Logan Chien5237bed2018-07-11 17:15:57 +08001132
1133 var dirName string
Hsin-Yi Chen53489642019-07-31 17:10:45 +08001134 if isNdk {
Logan Chien5237bed2018-07-11 17:15:57 +08001135 dirName = "ndk"
Hsin-Yi Chen53489642019-07-31 17:10:45 +08001136 } else if isLlndkOrVndk {
Logan Chien5237bed2018-07-11 17:15:57 +08001137 dirName = "vndk"
Logan Chien41eabe62019-04-10 13:33:58 +08001138 } else {
1139 dirName = "platform" // opt-in libs
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001140 }
Logan Chien5237bed2018-07-11 17:15:57 +08001141
Jayant Chowdhary34ce67d2018-03-08 11:00:50 -08001142 binderBitness := ctx.DeviceConfig().BinderBitness()
Logan Chien7eefdc42018-07-11 18:10:41 +08001143
1144 var ext string
1145 if isGzip {
1146 ext = ".lsdump.gz"
1147 } else {
1148 ext = ".lsdump"
1149 }
1150
1151 return ExistentPathForSource(ctx, "prebuilts", "abi-dumps", dirName,
1152 version, binderBitness, archNameAndVariant, "source-based",
1153 fileName+ext)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001154}
1155
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001156// PathForModuleOut returns a Path representing the paths... under the module's
1157// output directory.
Colin Cross635c3b02016-05-18 15:37:25 -07001158func PathForModuleOut(ctx ModuleContext, paths ...string) ModuleOutPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001159 p, err := validatePath(paths...)
1160 if err != nil {
1161 reportPathError(ctx, err)
1162 }
Colin Cross702e0f82017-10-18 17:27:54 -07001163 return ModuleOutPath{
1164 OutputPath: pathForModule(ctx).withRel(p),
1165 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001166}
1167
1168// ModuleGenPath is a Path representing the 'gen' directory in a module's output
1169// directory. Mainly used for generated sources.
1170type ModuleGenPath struct {
1171 ModuleOutPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001172}
1173
1174var _ Path = ModuleGenPath{}
1175var _ genPathProvider = ModuleGenPath{}
1176var _ objPathProvider = ModuleGenPath{}
1177
1178// PathForModuleGen returns a Path representing the paths... under the module's
1179// `gen' directory.
Colin Cross635c3b02016-05-18 15:37:25 -07001180func PathForModuleGen(ctx ModuleContext, paths ...string) ModuleGenPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001181 p, err := validatePath(paths...)
1182 if err != nil {
1183 reportPathError(ctx, err)
1184 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001185 return ModuleGenPath{
Colin Cross702e0f82017-10-18 17:27:54 -07001186 ModuleOutPath: ModuleOutPath{
1187 OutputPath: pathForModule(ctx).withRel("gen").withRel(p),
1188 },
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001189 }
1190}
1191
Dan Willemsen21ec4902016-11-02 20:43:13 -07001192func (p ModuleGenPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001193 // TODO: make a different path for local vs remote generated files?
Dan Willemsen21ec4902016-11-02 20:43:13 -07001194 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001195}
1196
Colin Cross635c3b02016-05-18 15:37:25 -07001197func (p ModuleGenPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001198 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1199}
1200
1201// ModuleObjPath is a Path representing the 'obj' directory in a module's output
1202// directory. Used for compiled objects.
1203type ModuleObjPath struct {
1204 ModuleOutPath
1205}
1206
1207var _ Path = ModuleObjPath{}
1208
1209// PathForModuleObj returns a Path representing the paths... under the module's
1210// 'obj' directory.
Jeff Gaston734e3802017-04-10 15:47:24 -07001211func PathForModuleObj(ctx ModuleContext, pathComponents ...string) ModuleObjPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001212 p, err := validatePath(pathComponents...)
1213 if err != nil {
1214 reportPathError(ctx, err)
1215 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001216 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
1217}
1218
1219// ModuleResPath is a a Path representing the 'res' directory in a module's
1220// output directory.
1221type ModuleResPath struct {
1222 ModuleOutPath
1223}
1224
1225var _ Path = ModuleResPath{}
1226
1227// PathForModuleRes returns a Path representing the paths... under the module's
1228// 'res' directory.
Jeff Gaston734e3802017-04-10 15:47:24 -07001229func PathForModuleRes(ctx ModuleContext, pathComponents ...string) ModuleResPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001230 p, err := validatePath(pathComponents...)
1231 if err != nil {
1232 reportPathError(ctx, err)
1233 }
1234
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001235 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
1236}
1237
Colin Cross70dda7e2019-10-01 22:05:35 -07001238// InstallPath is a Path representing a installed file path rooted from the build directory
1239type InstallPath struct {
1240 basePath
Colin Crossff6c33d2019-10-02 16:01:35 -07001241
Jiyong Park957bcd92020-10-20 18:23:33 +09001242 // partitionDir is the part of the InstallPath that is automatically determined according to the context.
1243 // For example, it is host/<os>-<arch> for host modules, and target/product/<device>/<partition> for device modules.
1244 partitionDir string
1245
1246 // makePath indicates whether this path is for Soong (false) or Make (true).
1247 makePath bool
Colin Cross70dda7e2019-10-01 22:05:35 -07001248}
1249
Paul Duffin9b478b02019-12-10 13:41:51 +00001250func (p InstallPath) buildDir() string {
1251 return p.config.buildDir
1252}
1253
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01001254func (p InstallPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1255 panic("Not implemented")
1256}
1257
Paul Duffin9b478b02019-12-10 13:41:51 +00001258var _ Path = InstallPath{}
1259var _ WritablePath = InstallPath{}
1260
Colin Cross70dda7e2019-10-01 22:05:35 -07001261func (p InstallPath) writablePath() {}
1262
1263func (p InstallPath) String() string {
Jiyong Park957bcd92020-10-20 18:23:33 +09001264 if p.makePath {
1265 // Make path starts with out/ instead of out/soong.
1266 return filepath.Join(p.config.buildDir, "../", p.path)
1267 } else {
1268 return filepath.Join(p.config.buildDir, p.path)
1269 }
1270}
1271
1272// PartitionDir returns the path to the partition where the install path is rooted at. It is
1273// out/soong/target/product/<device>/<partition> for device modules, and out/soong/host/<os>-<arch> for host modules.
1274// The ./soong is dropped if the install path is for Make.
1275func (p InstallPath) PartitionDir() string {
1276 if p.makePath {
1277 return filepath.Join(p.config.buildDir, "../", p.partitionDir)
1278 } else {
1279 return filepath.Join(p.config.buildDir, p.partitionDir)
1280 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001281}
1282
1283// Join creates a new InstallPath with paths... joined with the current path. The
1284// provided paths... may not use '..' to escape from the current path.
1285func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath {
1286 path, err := validatePath(paths...)
1287 if err != nil {
1288 reportPathError(ctx, err)
1289 }
1290 return p.withRel(path)
1291}
1292
1293func (p InstallPath) withRel(rel string) InstallPath {
1294 p.basePath = p.basePath.withRel(rel)
1295 return p
1296}
1297
Colin Crossff6c33d2019-10-02 16:01:35 -07001298// ToMakePath returns a new InstallPath that points to Make's install directory instead of Soong's,
1299// i.e. out/ instead of out/soong/.
1300func (p InstallPath) ToMakePath() InstallPath {
Jiyong Park957bcd92020-10-20 18:23:33 +09001301 p.makePath = true
Colin Crossff6c33d2019-10-02 16:01:35 -07001302 return p
Colin Cross70dda7e2019-10-01 22:05:35 -07001303}
1304
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001305// PathForModuleInstall returns a Path representing the install path for the
1306// module appended with paths...
Colin Cross70dda7e2019-10-01 22:05:35 -07001307func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
Colin Cross6e359402020-02-10 15:29:54 -08001308 os := ctx.Os()
Jiyong Park87788b52020-09-01 12:37:45 +09001309 arch := ctx.Arch().ArchType
1310 forceOS, forceArch := ctx.InstallForceOS()
1311 if forceOS != nil {
Colin Cross6e359402020-02-10 15:29:54 -08001312 os = *forceOS
1313 }
Jiyong Park87788b52020-09-01 12:37:45 +09001314 if forceArch != nil {
1315 arch = *forceArch
1316 }
Colin Cross6e359402020-02-10 15:29:54 -08001317 partition := modulePartition(ctx, os)
Colin Cross609c49a2020-02-13 13:20:11 -08001318
Jiyong Park87788b52020-09-01 12:37:45 +09001319 ret := pathForInstall(ctx, os, arch, partition, ctx.Debug(), pathComponents...)
Colin Cross609c49a2020-02-13 13:20:11 -08001320
Jingwen Chencda22c92020-11-23 00:22:30 -05001321 if ctx.InstallBypassMake() && ctx.Config().KatiEnabled() {
Colin Cross609c49a2020-02-13 13:20:11 -08001322 ret = ret.ToMakePath()
1323 }
1324
1325 return ret
1326}
1327
Jiyong Park87788b52020-09-01 12:37:45 +09001328func pathForInstall(ctx PathContext, os OsType, arch ArchType, partition string, debug bool,
Colin Cross609c49a2020-02-13 13:20:11 -08001329 pathComponents ...string) InstallPath {
1330
Jiyong Park957bcd92020-10-20 18:23:33 +09001331 var partionPaths []string
Colin Cross609c49a2020-02-13 13:20:11 -08001332
Colin Cross6e359402020-02-10 15:29:54 -08001333 if os.Class == Device {
Jiyong Park957bcd92020-10-20 18:23:33 +09001334 partionPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001335 } else {
Jiyong Park87788b52020-09-01 12:37:45 +09001336 osName := os.String()
1337 if os == Linux {
1338 // instead of linux_glibc
1339 osName = "linux"
Dan Willemsen866b5632017-09-22 12:28:24 -07001340 }
Jiyong Park87788b52020-09-01 12:37:45 +09001341 // SOONG_HOST_OUT is set to out/host/$(HOST_OS)-$(HOST_PREBUILT_ARCH)
1342 // and HOST_PREBUILT_ARCH is forcibly set to x86 even on x86_64 hosts. We don't seem
1343 // to have a plan to fix it (see the comment in build/make/core/envsetup.mk).
1344 // Let's keep using x86 for the existing cases until we have a need to support
1345 // other architectures.
1346 archName := arch.String()
1347 if os.Class == Host && (arch == X86_64 || arch == Common) {
1348 archName = "x86"
1349 }
Jiyong Park957bcd92020-10-20 18:23:33 +09001350 partionPaths = []string{"host", osName + "-" + archName, partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001351 }
Colin Cross609c49a2020-02-13 13:20:11 -08001352 if debug {
Jiyong Park957bcd92020-10-20 18:23:33 +09001353 partionPaths = append([]string{"debug"}, partionPaths...)
Dan Willemsen782a2d12015-12-21 14:55:28 -08001354 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001355
Jiyong Park957bcd92020-10-20 18:23:33 +09001356 partionPath, err := validatePath(partionPaths...)
Colin Cross70dda7e2019-10-01 22:05:35 -07001357 if err != nil {
1358 reportPathError(ctx, err)
1359 }
Colin Crossff6c33d2019-10-02 16:01:35 -07001360
Jiyong Park957bcd92020-10-20 18:23:33 +09001361 base := InstallPath{
1362 basePath: basePath{partionPath, ctx.Config(), ""},
1363 partitionDir: partionPath,
1364 makePath: false,
1365 }
Colin Crossff6c33d2019-10-02 16:01:35 -07001366
Jiyong Park957bcd92020-10-20 18:23:33 +09001367 return base.Join(ctx, pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001368}
1369
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00001370func pathForNdkOrSdkInstall(ctx PathContext, prefix string, paths []string) InstallPath {
Jiyong Park957bcd92020-10-20 18:23:33 +09001371 base := InstallPath{
1372 basePath: basePath{prefix, ctx.Config(), ""},
1373 partitionDir: prefix,
1374 makePath: false,
Colin Cross70dda7e2019-10-01 22:05:35 -07001375 }
Jiyong Park957bcd92020-10-20 18:23:33 +09001376 return base.Join(ctx, paths...)
Colin Cross70dda7e2019-10-01 22:05:35 -07001377}
1378
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00001379func PathForNdkInstall(ctx PathContext, paths ...string) InstallPath {
1380 return pathForNdkOrSdkInstall(ctx, "ndk", paths)
1381}
1382
1383func PathForMainlineSdksInstall(ctx PathContext, paths ...string) InstallPath {
1384 return pathForNdkOrSdkInstall(ctx, "mainline-sdks", paths)
1385}
1386
Colin Cross70dda7e2019-10-01 22:05:35 -07001387func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string {
Colin Cross43f08db2018-11-12 10:13:39 -08001388 rel := Rel(ctx, PathForOutput(ctx, "target", "product", ctx.Config().DeviceName()).String(), path.String())
1389
1390 return "/" + rel
1391}
1392
Colin Cross6e359402020-02-10 15:29:54 -08001393func modulePartition(ctx ModuleInstallPathContext, os OsType) string {
Colin Cross43f08db2018-11-12 10:13:39 -08001394 var partition string
Colin Cross6e359402020-02-10 15:29:54 -08001395 if ctx.InstallInTestcases() {
1396 // "testcases" install directory can be used for host or device modules.
Jaewoong Jung0949f312019-09-11 10:25:18 -07001397 partition = "testcases"
Colin Cross6e359402020-02-10 15:29:54 -08001398 } else if os.Class == Device {
1399 if ctx.InstallInData() {
1400 partition = "data"
1401 } else if ctx.InstallInRamdisk() {
1402 if ctx.DeviceConfig().BoardUsesRecoveryAsBoot() {
1403 partition = "recovery/root/first_stage_ramdisk"
1404 } else {
1405 partition = "ramdisk"
1406 }
1407 if !ctx.InstallInRoot() {
1408 partition += "/system"
1409 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -07001410 } else if ctx.InstallInVendorRamdisk() {
Yifan Hong39143a92020-10-26 12:43:12 -07001411 // The module is only available after switching root into
1412 // /first_stage_ramdisk. To expose the module before switching root
1413 // on a device without a dedicated recovery partition, install the
1414 // recovery variant.
Yifan Hongdd8dacc2020-10-21 15:40:17 -07001415 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
Yifan Hong39143a92020-10-26 12:43:12 -07001416 partition = "vendor-ramdisk/first_stage_ramdisk"
Yifan Hongdd8dacc2020-10-21 15:40:17 -07001417 } else {
1418 partition = "vendor-ramdisk"
1419 }
1420 if !ctx.InstallInRoot() {
1421 partition += "/system"
1422 }
Colin Cross6e359402020-02-10 15:29:54 -08001423 } else if ctx.InstallInRecovery() {
1424 if ctx.InstallInRoot() {
1425 partition = "recovery/root"
1426 } else {
1427 // the layout of recovery partion is the same as that of system partition
1428 partition = "recovery/root/system"
1429 }
1430 } else if ctx.SocSpecific() {
1431 partition = ctx.DeviceConfig().VendorPath()
1432 } else if ctx.DeviceSpecific() {
1433 partition = ctx.DeviceConfig().OdmPath()
1434 } else if ctx.ProductSpecific() {
1435 partition = ctx.DeviceConfig().ProductPath()
1436 } else if ctx.SystemExtSpecific() {
1437 partition = ctx.DeviceConfig().SystemExtPath()
1438 } else if ctx.InstallInRoot() {
1439 partition = "root"
Yifan Hong82db7352020-01-21 16:12:26 -08001440 } else {
Colin Cross6e359402020-02-10 15:29:54 -08001441 partition = "system"
Yifan Hong82db7352020-01-21 16:12:26 -08001442 }
Colin Cross6e359402020-02-10 15:29:54 -08001443 if ctx.InstallInSanitizerDir() {
1444 partition = "data/asan/" + partition
Yifan Hong82db7352020-01-21 16:12:26 -08001445 }
Colin Cross43f08db2018-11-12 10:13:39 -08001446 }
1447 return partition
1448}
1449
Colin Cross609c49a2020-02-13 13:20:11 -08001450type InstallPaths []InstallPath
1451
1452// Paths returns the InstallPaths as a Paths
1453func (p InstallPaths) Paths() Paths {
1454 if p == nil {
1455 return nil
1456 }
1457 ret := make(Paths, len(p))
1458 for i, path := range p {
1459 ret[i] = path
1460 }
1461 return ret
1462}
1463
1464// Strings returns the string forms of the install paths.
1465func (p InstallPaths) Strings() []string {
1466 if p == nil {
1467 return nil
1468 }
1469 ret := make([]string, len(p))
1470 for i, path := range p {
1471 ret[i] = path.String()
1472 }
1473 return ret
1474}
1475
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001476// validateSafePath validates a path that we trust (may contain ninja variables).
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001477// Ensures that each path component does not attempt to leave its component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001478func validateSafePath(pathComponents ...string) (string, error) {
Jeff Gaston734e3802017-04-10 15:47:24 -07001479 for _, path := range pathComponents {
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001480 path := filepath.Clean(path)
1481 if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001482 return "", fmt.Errorf("Path is outside directory: %s", path)
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001483 }
1484 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001485 // TODO: filepath.Join isn't necessarily correct with embedded ninja
1486 // variables. '..' may remove the entire ninja variable, even if it
1487 // will be expanded to multiple nested directories.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001488 return filepath.Join(pathComponents...), nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001489}
1490
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001491// validatePath validates that a path does not include ninja variables, and that
1492// each path component does not attempt to leave its component. Returns a joined
1493// version of each path component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001494func validatePath(pathComponents ...string) (string, error) {
Jeff Gaston734e3802017-04-10 15:47:24 -07001495 for _, path := range pathComponents {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001496 if strings.Contains(path, "$") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001497 return "", fmt.Errorf("Path contains invalid character($): %s", path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001498 }
1499 }
Colin Cross1ccfcc32018-02-22 13:54:26 -08001500 return validateSafePath(pathComponents...)
Colin Cross6e18ca42015-07-14 18:55:36 -07001501}
Colin Cross5b529592017-05-09 13:34:34 -07001502
Colin Cross0875c522017-11-28 17:34:01 -08001503func PathForPhony(ctx PathContext, phony string) WritablePath {
1504 if strings.ContainsAny(phony, "$/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001505 ReportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony)
Colin Cross0875c522017-11-28 17:34:01 -08001506 }
Colin Cross74e3fe42017-12-11 15:51:44 -08001507 return PhonyPath{basePath{phony, ctx.Config(), ""}}
Colin Cross0875c522017-11-28 17:34:01 -08001508}
1509
Colin Cross74e3fe42017-12-11 15:51:44 -08001510type PhonyPath struct {
1511 basePath
1512}
1513
1514func (p PhonyPath) writablePath() {}
1515
Paul Duffin9b478b02019-12-10 13:41:51 +00001516func (p PhonyPath) buildDir() string {
1517 return p.config.buildDir
1518}
1519
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01001520func (p PhonyPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1521 panic("Not implemented")
1522}
1523
Colin Cross74e3fe42017-12-11 15:51:44 -08001524var _ Path = PhonyPath{}
1525var _ WritablePath = PhonyPath{}
1526
Colin Cross5b529592017-05-09 13:34:34 -07001527type testPath struct {
1528 basePath
1529}
1530
1531func (p testPath) String() string {
1532 return p.path
1533}
1534
Colin Cross40e33732019-02-15 11:08:35 -08001535// PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from
1536// within tests.
Colin Cross5b529592017-05-09 13:34:34 -07001537func PathForTesting(paths ...string) Path {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001538 p, err := validateSafePath(paths...)
1539 if err != nil {
1540 panic(err)
1541 }
Colin Cross5b529592017-05-09 13:34:34 -07001542 return testPath{basePath{path: p, rel: p}}
1543}
1544
Colin Cross40e33732019-02-15 11:08:35 -08001545// PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests.
1546func PathsForTesting(strs ...string) Paths {
Colin Cross5b529592017-05-09 13:34:34 -07001547 p := make(Paths, len(strs))
1548 for i, s := range strs {
1549 p[i] = PathForTesting(s)
1550 }
1551
1552 return p
1553}
Colin Cross43f08db2018-11-12 10:13:39 -08001554
Colin Cross40e33732019-02-15 11:08:35 -08001555type testPathContext struct {
1556 config Config
Colin Cross40e33732019-02-15 11:08:35 -08001557}
1558
Colin Cross40e33732019-02-15 11:08:35 -08001559func (x *testPathContext) Config() Config { return x.config }
1560func (x *testPathContext) AddNinjaFileDeps(...string) {}
1561
1562// PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with
1563// PathForOutput.
Colin Cross98be1bb2019-12-13 20:41:13 -08001564func PathContextForTesting(config Config) PathContext {
Colin Cross40e33732019-02-15 11:08:35 -08001565 return &testPathContext{
1566 config: config,
Colin Cross40e33732019-02-15 11:08:35 -08001567 }
1568}
1569
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01001570type testModuleInstallPathContext struct {
1571 baseModuleContext
1572
1573 inData bool
1574 inTestcases bool
1575 inSanitizerDir bool
1576 inRamdisk bool
1577 inVendorRamdisk bool
1578 inRecovery bool
1579 inRoot bool
1580 forceOS *OsType
1581 forceArch *ArchType
1582}
1583
1584func (m testModuleInstallPathContext) Config() Config {
1585 return m.baseModuleContext.config
1586}
1587
1588func (testModuleInstallPathContext) AddNinjaFileDeps(deps ...string) {}
1589
1590func (m testModuleInstallPathContext) InstallInData() bool {
1591 return m.inData
1592}
1593
1594func (m testModuleInstallPathContext) InstallInTestcases() bool {
1595 return m.inTestcases
1596}
1597
1598func (m testModuleInstallPathContext) InstallInSanitizerDir() bool {
1599 return m.inSanitizerDir
1600}
1601
1602func (m testModuleInstallPathContext) InstallInRamdisk() bool {
1603 return m.inRamdisk
1604}
1605
1606func (m testModuleInstallPathContext) InstallInVendorRamdisk() bool {
1607 return m.inVendorRamdisk
1608}
1609
1610func (m testModuleInstallPathContext) InstallInRecovery() bool {
1611 return m.inRecovery
1612}
1613
1614func (m testModuleInstallPathContext) InstallInRoot() bool {
1615 return m.inRoot
1616}
1617
1618func (m testModuleInstallPathContext) InstallBypassMake() bool {
1619 return false
1620}
1621
1622func (m testModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
1623 return m.forceOS, m.forceArch
1624}
1625
1626// Construct a minimal ModuleInstallPathContext for testing. Note that baseModuleContext is
1627// default-initialized, which leaves blueprint.baseModuleContext set to nil, so methods that are
1628// delegated to it will panic.
1629func ModuleInstallPathContextForTesting(config Config) ModuleInstallPathContext {
1630 ctx := &testModuleInstallPathContext{}
1631 ctx.config = config
1632 ctx.os = Android
1633 return ctx
1634}
1635
Colin Cross43f08db2018-11-12 10:13:39 -08001636// Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if
1637// targetPath is not inside basePath.
1638func Rel(ctx PathContext, basePath string, targetPath string) string {
1639 rel, isRel := MaybeRel(ctx, basePath, targetPath)
1640 if !isRel {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001641 ReportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath)
Colin Cross43f08db2018-11-12 10:13:39 -08001642 return ""
1643 }
1644 return rel
1645}
1646
1647// MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if
1648// targetPath is not inside basePath.
1649func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) {
Dan Willemsen633c5022019-04-12 11:11:38 -07001650 rel, isRel, err := maybeRelErr(basePath, targetPath)
1651 if err != nil {
1652 reportPathError(ctx, err)
1653 }
1654 return rel, isRel
1655}
1656
1657func maybeRelErr(basePath string, targetPath string) (string, bool, error) {
Colin Cross43f08db2018-11-12 10:13:39 -08001658 // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first.
1659 if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) {
Dan Willemsen633c5022019-04-12 11:11:38 -07001660 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08001661 }
1662 rel, err := filepath.Rel(basePath, targetPath)
1663 if err != nil {
Dan Willemsen633c5022019-04-12 11:11:38 -07001664 return "", false, err
Colin Cross43f08db2018-11-12 10:13:39 -08001665 } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") {
Dan Willemsen633c5022019-04-12 11:11:38 -07001666 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08001667 }
Dan Willemsen633c5022019-04-12 11:11:38 -07001668 return rel, true, nil
Colin Cross43f08db2018-11-12 10:13:39 -08001669}
Colin Cross988414c2020-01-11 01:11:46 +00001670
1671// Writes a file to the output directory. Attempting to write directly to the output directory
1672// will fail due to the sandbox of the soong_build process.
1673func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error {
1674 return ioutil.WriteFile(absolutePath(path.String()), data, perm)
1675}
1676
1677func absolutePath(path string) string {
1678 if filepath.IsAbs(path) {
1679 return path
1680 }
1681 return filepath.Join(absSrcDir, path)
1682}
Chris Parsons216e10a2020-07-09 17:12:52 -04001683
1684// A DataPath represents the path of a file to be used as data, for example
1685// a test library to be installed alongside a test.
1686// The data file should be installed (copied from `<SrcPath>`) to
1687// `<install_root>/<RelativeInstallPath>/<filename>`, or
1688// `<install_root>/<filename>` if RelativeInstallPath is empty.
1689type DataPath struct {
1690 // The path of the data file that should be copied into the data directory
1691 SrcPath Path
1692 // The install path of the data file, relative to the install root.
1693 RelativeInstallPath string
1694}