blob: 10d8d0df53137d3f90c5ee9aa882aac86a9671c6 [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 Cross5d583952020-11-24 16:21:24 -0800545// FirstUniqueInstallPaths returns all unique elements of an InstallPaths, keeping the first copy of each. It
546// modifies the InstallPaths slice contents in place, and returns a subslice of the original slice.
547func FirstUniqueInstallPaths(list InstallPaths) InstallPaths {
548 // 128 was chosen based on BenchmarkFirstUniquePaths results.
549 if len(list) > 128 {
550 return firstUniqueInstallPathsMap(list)
551 }
552 return firstUniqueInstallPathsList(list)
553}
554
555func firstUniqueInstallPathsList(list InstallPaths) InstallPaths {
556 k := 0
557outer:
558 for i := 0; i < len(list); i++ {
559 for j := 0; j < k; j++ {
560 if list[i] == list[j] {
561 continue outer
562 }
563 }
564 list[k] = list[i]
565 k++
566 }
567 return list[:k]
568}
569
570func firstUniqueInstallPathsMap(list InstallPaths) InstallPaths {
571 k := 0
572 seen := make(map[InstallPath]bool, len(list))
573 for i := 0; i < len(list); i++ {
574 if seen[list[i]] {
575 continue
576 }
577 seen[list[i]] = true
578 list[k] = list[i]
579 k++
580 }
581 return list[:k]
582}
583
Colin Crossb6715442017-10-24 11:13:31 -0700584// LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It
585// modifies the Paths slice contents in place, and returns a subslice of the original slice.
586func LastUniquePaths(list Paths) Paths {
587 totalSkip := 0
588 for i := len(list) - 1; i >= totalSkip; i-- {
589 skip := 0
590 for j := i - 1; j >= totalSkip; j-- {
591 if list[i] == list[j] {
592 skip++
593 } else {
594 list[j+skip] = list[j]
595 }
596 }
597 totalSkip += skip
598 }
599 return list[totalSkip:]
600}
601
Colin Crossa140bb02018-04-17 10:52:26 -0700602// ReversePaths returns a copy of a Paths in reverse order.
603func ReversePaths(list Paths) Paths {
604 if list == nil {
605 return nil
606 }
607 ret := make(Paths, len(list))
608 for i := range list {
609 ret[i] = list[len(list)-1-i]
610 }
611 return ret
612}
613
Jeff Gaston294356f2017-09-27 17:05:30 -0700614func indexPathList(s Path, list []Path) int {
615 for i, l := range list {
616 if l == s {
617 return i
618 }
619 }
620
621 return -1
622}
623
624func inPathList(p Path, list []Path) bool {
625 return indexPathList(p, list) != -1
626}
627
628func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) {
Paul Duffin57b9e1d2019-12-13 00:03:35 +0000629 return FilterPathListPredicate(list, func(p Path) bool { return inPathList(p, filter) })
630}
631
632func FilterPathListPredicate(list []Path, predicate func(Path) bool) (remainder []Path, filtered []Path) {
Jeff Gaston294356f2017-09-27 17:05:30 -0700633 for _, l := range list {
Paul Duffin57b9e1d2019-12-13 00:03:35 +0000634 if predicate(l) {
Jeff Gaston294356f2017-09-27 17:05:30 -0700635 filtered = append(filtered, l)
636 } else {
637 remainder = append(remainder, l)
638 }
639 }
640
641 return
642}
643
Colin Cross93e85952017-08-15 13:34:18 -0700644// HasExt returns true of any of the paths have extension ext, otherwise false
645func (p Paths) HasExt(ext string) bool {
646 for _, path := range p {
647 if path.Ext() == ext {
648 return true
649 }
650 }
651
652 return false
653}
654
655// FilterByExt returns the subset of the paths that have extension ext
656func (p Paths) FilterByExt(ext string) Paths {
657 ret := make(Paths, 0, len(p))
658 for _, path := range p {
659 if path.Ext() == ext {
660 ret = append(ret, path)
661 }
662 }
663 return ret
664}
665
666// FilterOutByExt returns the subset of the paths that do not have extension ext
667func (p Paths) FilterOutByExt(ext string) Paths {
668 ret := make(Paths, 0, len(p))
669 for _, path := range p {
670 if path.Ext() != ext {
671 ret = append(ret, path)
672 }
673 }
674 return ret
675}
676
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700677// DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory
678// (including subdirectories) are in a contiguous subslice of the list, and can be found in
679// O(log(N)) time using a binary search on the directory prefix.
680type DirectorySortedPaths Paths
681
682func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths {
683 ret := append(DirectorySortedPaths(nil), paths...)
684 sort.Slice(ret, func(i, j int) bool {
685 return ret[i].String() < ret[j].String()
686 })
687 return ret
688}
689
690// PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries
691// that are in the specified directory and its subdirectories.
692func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths {
693 prefix := filepath.Clean(dir) + "/"
694 start := sort.Search(len(p), func(i int) bool {
695 return prefix < p[i].String()
696 })
697
698 ret := p[start:]
699
700 end := sort.Search(len(ret), func(i int) bool {
701 return !strings.HasPrefix(ret[i].String(), prefix)
702 })
703
704 ret = ret[:end]
705
706 return Paths(ret)
707}
708
Alex Humesky29e3bbe2020-11-20 21:30:13 -0500709// WritablePaths is a slice of WritablePath, used for multiple outputs.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700710type WritablePaths []WritablePath
711
712// Strings returns the string forms of the writable paths.
713func (p WritablePaths) Strings() []string {
714 if p == nil {
715 return nil
716 }
717 ret := make([]string, len(p))
718 for i, path := range p {
719 ret[i] = path.String()
720 }
721 return ret
722}
723
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800724// Paths returns the WritablePaths as a Paths
725func (p WritablePaths) Paths() Paths {
726 if p == nil {
727 return nil
728 }
729 ret := make(Paths, len(p))
730 for i, path := range p {
731 ret[i] = path
732 }
733 return ret
734}
735
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700736type basePath struct {
737 path string
738 config Config
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800739 rel string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700740}
741
742func (p basePath) Ext() string {
743 return filepath.Ext(p.path)
744}
745
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700746func (p basePath) Base() string {
747 return filepath.Base(p.path)
748}
749
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800750func (p basePath) Rel() string {
751 if p.rel != "" {
752 return p.rel
753 }
754 return p.path
755}
756
Colin Cross0875c522017-11-28 17:34:01 -0800757func (p basePath) String() string {
758 return p.path
759}
760
Colin Cross0db55682017-12-05 15:36:55 -0800761func (p basePath) withRel(rel string) basePath {
762 p.path = filepath.Join(p.path, rel)
763 p.rel = rel
764 return p
765}
766
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700767// SourcePath is a Path representing a file path rooted from SrcDir
768type SourcePath struct {
769 basePath
770}
771
772var _ Path = SourcePath{}
773
Colin Cross0db55682017-12-05 15:36:55 -0800774func (p SourcePath) withRel(rel string) SourcePath {
775 p.basePath = p.basePath.withRel(rel)
776 return p
777}
778
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700779// safePathForSource is for paths that we expect are safe -- only for use by go
780// code that is embedding ninja variables in paths
Colin Crossfe4bc362018-09-12 10:02:13 -0700781func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
782 p, err := validateSafePath(pathComponents...)
Colin Crossaabf6792017-11-29 00:27:14 -0800783 ret := SourcePath{basePath{p, ctx.Config(), ""}}
Colin Crossfe4bc362018-09-12 10:02:13 -0700784 if err != nil {
785 return ret, err
786 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700787
Colin Cross7b3dcc32019-01-24 13:14:39 -0800788 // absolute path already checked by validateSafePath
789 if strings.HasPrefix(ret.String(), ctx.Config().buildDir) {
Mikhail Naganovab1f5182019-02-08 13:17:55 -0800790 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Cross6e18ca42015-07-14 18:55:36 -0700791 }
792
Colin Crossfe4bc362018-09-12 10:02:13 -0700793 return ret, err
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700794}
795
Colin Cross192e97a2018-02-22 14:21:02 -0800796// pathForSource creates a SourcePath from pathComponents, but does not check that it exists.
797func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
Colin Crossc48c1432018-02-23 07:09:01 +0000798 p, err := validatePath(pathComponents...)
799 ret := SourcePath{basePath{p, ctx.Config(), ""}}
Colin Cross94a32102018-02-22 14:21:02 -0800800 if err != nil {
Colin Cross192e97a2018-02-22 14:21:02 -0800801 return ret, err
Colin Cross94a32102018-02-22 14:21:02 -0800802 }
803
Colin Cross7b3dcc32019-01-24 13:14:39 -0800804 // absolute path already checked by validatePath
805 if strings.HasPrefix(ret.String(), ctx.Config().buildDir) {
Mikhail Naganovab1f5182019-02-08 13:17:55 -0800806 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Crossc48c1432018-02-23 07:09:01 +0000807 }
808
Colin Cross192e97a2018-02-22 14:21:02 -0800809 return ret, nil
810}
811
812// existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the
813// path does not exist.
814func existsWithDependencies(ctx PathContext, path SourcePath) (exists bool, err error) {
815 var files []string
816
817 if gctx, ok := ctx.(PathGlobContext); ok {
818 // Use glob to produce proper dependencies, even though we only want
819 // a single file.
820 files, err = gctx.GlobWithDeps(path.String(), nil)
821 } else {
822 var deps []string
823 // We cannot add build statements in this context, so we fall back to
824 // AddNinjaFileDeps
Colin Cross988414c2020-01-11 01:11:46 +0000825 files, deps, err = ctx.Config().fs.Glob(path.String(), nil, pathtools.FollowSymlinks)
Colin Cross192e97a2018-02-22 14:21:02 -0800826 ctx.AddNinjaFileDeps(deps...)
827 }
828
829 if err != nil {
830 return false, fmt.Errorf("glob: %s", err.Error())
831 }
832
833 return len(files) > 0, nil
834}
835
836// PathForSource joins the provided path components and validates that the result
837// neither escapes the source dir nor is in the out dir.
838// On error, it will return a usable, but invalid SourcePath, and report a ModuleError.
839func PathForSource(ctx PathContext, pathComponents ...string) SourcePath {
840 path, err := pathForSource(ctx, pathComponents...)
841 if err != nil {
842 reportPathError(ctx, err)
843 }
844
Colin Crosse3924e12018-08-15 20:18:53 -0700845 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100846 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -0700847 }
848
Colin Cross192e97a2018-02-22 14:21:02 -0800849 if modCtx, ok := ctx.(ModuleContext); ok && ctx.Config().AllowMissingDependencies() {
850 exists, err := existsWithDependencies(ctx, path)
851 if err != nil {
852 reportPathError(ctx, err)
853 }
854 if !exists {
855 modCtx.AddMissingDependencies([]string{path.String()})
856 }
Colin Cross988414c2020-01-11 01:11:46 +0000857 } else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100858 ReportPathErrorf(ctx, "%s: %s", path, err.Error())
Colin Cross5e6a7972020-06-07 16:56:32 -0700859 } else if !exists && !ctx.Config().testAllowNonExistentPaths {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100860 ReportPathErrorf(ctx, "source path %q does not exist", path)
Colin Cross192e97a2018-02-22 14:21:02 -0800861 }
862 return path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700863}
864
Jeff Gaston734e3802017-04-10 15:47:24 -0700865// ExistentPathForSource returns an OptionalPath with the SourcePath if the
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700866// path exists, or an empty OptionalPath if it doesn't exist. Dependencies are added
867// so that the ninja file will be regenerated if the state of the path changes.
Colin Cross32f38982018-02-22 11:47:25 -0800868func ExistentPathForSource(ctx PathContext, pathComponents ...string) OptionalPath {
Colin Cross192e97a2018-02-22 14:21:02 -0800869 path, err := pathForSource(ctx, pathComponents...)
Colin Cross1ccfcc32018-02-22 13:54:26 -0800870 if err != nil {
871 reportPathError(ctx, err)
872 return OptionalPath{}
873 }
Colin Crossc48c1432018-02-23 07:09:01 +0000874
Colin Crosse3924e12018-08-15 20:18:53 -0700875 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100876 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -0700877 return OptionalPath{}
878 }
879
Colin Cross192e97a2018-02-22 14:21:02 -0800880 exists, err := existsWithDependencies(ctx, path)
Colin Crossc48c1432018-02-23 07:09:01 +0000881 if err != nil {
882 reportPathError(ctx, err)
883 return OptionalPath{}
884 }
Colin Cross192e97a2018-02-22 14:21:02 -0800885 if !exists {
Colin Crossc48c1432018-02-23 07:09:01 +0000886 return OptionalPath{}
887 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700888 return OptionalPathForPath(path)
889}
890
891func (p SourcePath) String() string {
892 return filepath.Join(p.config.srcDir, p.path)
893}
894
895// Join creates a new SourcePath with paths... joined with the current path. The
896// provided paths... may not use '..' to escape from the current path.
897func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800898 path, err := validatePath(paths...)
899 if err != nil {
900 reportPathError(ctx, err)
901 }
Colin Cross0db55682017-12-05 15:36:55 -0800902 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700903}
904
Colin Cross2fafa3e2019-03-05 12:39:51 -0800905// join is like Join but does less path validation.
906func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath {
907 path, err := validateSafePath(paths...)
908 if err != nil {
909 reportPathError(ctx, err)
910 }
911 return p.withRel(path)
912}
913
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700914// OverlayPath returns the overlay for `path' if it exists. This assumes that the
915// SourcePath is the path to a resource overlay directory.
Colin Cross635c3b02016-05-18 15:37:25 -0700916func (p SourcePath) OverlayPath(ctx ModuleContext, path Path) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700917 var relDir string
Colin Cross07e51612019-03-05 12:46:40 -0800918 if srcPath, ok := path.(SourcePath); ok {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700919 relDir = srcPath.path
920 } else {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100921 ReportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700922 return OptionalPath{}
923 }
924 dir := filepath.Join(p.config.srcDir, p.path, relDir)
925 // Use Glob so that we are run again if the directory is added.
Colin Cross7f19f372016-11-01 11:10:25 -0700926 if pathtools.IsGlob(dir) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100927 ReportPathErrorf(ctx, "Path may not contain a glob: %s", dir)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800928 }
Colin Cross461b4452018-02-23 09:22:42 -0800929 paths, err := ctx.GlobWithDeps(dir, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700930 if err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100931 ReportPathErrorf(ctx, "glob: %s", err.Error())
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700932 return OptionalPath{}
933 }
934 if len(paths) == 0 {
935 return OptionalPath{}
936 }
Colin Cross43f08db2018-11-12 10:13:39 -0800937 relPath := Rel(ctx, p.config.srcDir, paths[0])
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700938 return OptionalPathForPath(PathForSource(ctx, relPath))
939}
940
Colin Cross70dda7e2019-10-01 22:05:35 -0700941// OutputPath is a Path representing an intermediates file path rooted from the build directory
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700942type OutputPath struct {
943 basePath
Colin Crossd63c9a72020-01-29 16:52:50 -0800944 fullPath string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700945}
946
Colin Cross702e0f82017-10-18 17:27:54 -0700947func (p OutputPath) withRel(rel string) OutputPath {
Colin Cross0db55682017-12-05 15:36:55 -0800948 p.basePath = p.basePath.withRel(rel)
Colin Crossd63c9a72020-01-29 16:52:50 -0800949 p.fullPath = filepath.Join(p.fullPath, rel)
Colin Cross702e0f82017-10-18 17:27:54 -0700950 return p
951}
952
Colin Cross3063b782018-08-15 11:19:12 -0700953func (p OutputPath) WithoutRel() OutputPath {
954 p.basePath.rel = filepath.Base(p.basePath.path)
955 return p
956}
957
Paul Duffin9b478b02019-12-10 13:41:51 +0000958func (p OutputPath) buildDir() string {
959 return p.config.buildDir
960}
961
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700962var _ Path = OutputPath{}
Paul Duffin9b478b02019-12-10 13:41:51 +0000963var _ WritablePath = OutputPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700964
Chris Parsons8f232a22020-06-23 17:37:05 -0400965// toolDepPath is a Path representing a dependency of the build tool.
966type toolDepPath struct {
967 basePath
968}
969
970var _ Path = toolDepPath{}
971
972// pathForBuildToolDep returns a toolDepPath representing the given path string.
973// There is no validation for the path, as it is "trusted": It may fail
974// normal validation checks. For example, it may be an absolute path.
975// Only use this function to construct paths for dependencies of the build
976// tool invocation.
977func pathForBuildToolDep(ctx PathContext, path string) toolDepPath {
978 return toolDepPath{basePath{path, ctx.Config(), ""}}
979}
980
Jeff Gaston734e3802017-04-10 15:47:24 -0700981// PathForOutput joins the provided paths and returns an OutputPath that is
982// validated to not escape the build dir.
983// On error, it will return a usable, but invalid OutputPath, and report a ModuleError.
984func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -0800985 path, err := validatePath(pathComponents...)
986 if err != nil {
987 reportPathError(ctx, err)
988 }
Colin Crossd63c9a72020-01-29 16:52:50 -0800989 fullPath := filepath.Join(ctx.Config().buildDir, path)
990 path = fullPath[len(fullPath)-len(path):]
991 return OutputPath{basePath{path, ctx.Config(), ""}, fullPath}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700992}
993
Colin Cross40e33732019-02-15 11:08:35 -0800994// PathsForOutput returns Paths rooted from buildDir
995func PathsForOutput(ctx PathContext, paths []string) WritablePaths {
996 ret := make(WritablePaths, len(paths))
997 for i, path := range paths {
998 ret[i] = PathForOutput(ctx, path)
999 }
1000 return ret
1001}
1002
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001003func (p OutputPath) writablePath() {}
1004
1005func (p OutputPath) String() string {
Colin Crossd63c9a72020-01-29 16:52:50 -08001006 return p.fullPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001007}
1008
1009// Join creates a new OutputPath with paths... joined with the current path. The
1010// provided paths... may not use '..' to escape from the current path.
1011func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001012 path, err := validatePath(paths...)
1013 if err != nil {
1014 reportPathError(ctx, err)
1015 }
Colin Cross0db55682017-12-05 15:36:55 -08001016 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001017}
1018
Colin Cross8854a5a2019-02-11 14:14:16 -08001019// ReplaceExtension creates a new OutputPath with the extension replaced with ext.
1020func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1021 if strings.Contains(ext, "/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001022 ReportPathErrorf(ctx, "extension %q cannot contain /", ext)
Colin Cross8854a5a2019-02-11 14:14:16 -08001023 }
1024 ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext))
Colin Cross2cdd5df2019-02-25 10:25:24 -08001025 ret.rel = pathtools.ReplaceExtension(p.rel, ext)
Colin Cross8854a5a2019-02-11 14:14:16 -08001026 return ret
1027}
1028
Colin Cross40e33732019-02-15 11:08:35 -08001029// InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths.
1030func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath {
1031 path, err := validatePath(paths...)
1032 if err != nil {
1033 reportPathError(ctx, err)
1034 }
1035
1036 ret := PathForOutput(ctx, filepath.Dir(p.path), path)
Colin Cross2cdd5df2019-02-25 10:25:24 -08001037 ret.rel = filepath.Join(filepath.Dir(p.rel), path)
Colin Cross40e33732019-02-15 11:08:35 -08001038 return ret
1039}
1040
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001041// PathForIntermediates returns an OutputPath representing the top-level
1042// intermediates directory.
1043func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001044 path, err := validatePath(paths...)
1045 if err != nil {
1046 reportPathError(ctx, err)
1047 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001048 return PathForOutput(ctx, ".intermediates", path)
1049}
1050
Colin Cross07e51612019-03-05 12:46:40 -08001051var _ genPathProvider = SourcePath{}
1052var _ objPathProvider = SourcePath{}
1053var _ resPathProvider = SourcePath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001054
Colin Cross07e51612019-03-05 12:46:40 -08001055// PathForModuleSrc returns a Path representing the paths... under the
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001056// module's local source directory.
Colin Cross8a497952019-03-05 22:25:09 -08001057func PathForModuleSrc(ctx ModuleContext, pathComponents ...string) Path {
1058 p, err := validatePath(pathComponents...)
1059 if err != nil {
1060 reportPathError(ctx, err)
Colin Cross192e97a2018-02-22 14:21:02 -08001061 }
Colin Cross8a497952019-03-05 22:25:09 -08001062 paths, err := expandOneSrcPath(ctx, p, nil)
1063 if err != nil {
1064 if depErr, ok := err.(missingDependencyError); ok {
1065 if ctx.Config().AllowMissingDependencies() {
1066 ctx.AddMissingDependencies(depErr.missingDeps)
1067 } else {
1068 ctx.ModuleErrorf(`%s, is the property annotated with android:"path"?`, depErr.Error())
1069 }
1070 } else {
1071 reportPathError(ctx, err)
1072 }
1073 return nil
1074 } else if len(paths) == 0 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001075 ReportPathErrorf(ctx, "%q produced no files, expected exactly one", p)
Colin Cross8a497952019-03-05 22:25:09 -08001076 return nil
1077 } else if len(paths) > 1 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001078 ReportPathErrorf(ctx, "%q produced %d files, expected exactly one", p, len(paths))
Colin Cross8a497952019-03-05 22:25:09 -08001079 }
1080 return paths[0]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001081}
1082
Colin Cross07e51612019-03-05 12:46:40 -08001083func pathForModuleSrc(ctx ModuleContext, paths ...string) SourcePath {
1084 p, err := validatePath(paths...)
1085 if err != nil {
1086 reportPathError(ctx, err)
1087 }
1088
1089 path, err := pathForSource(ctx, ctx.ModuleDir(), p)
1090 if err != nil {
1091 reportPathError(ctx, err)
1092 }
1093
1094 path.basePath.rel = p
1095
1096 return path
1097}
1098
Colin Cross2fafa3e2019-03-05 12:39:51 -08001099// PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path
1100// will return the path relative to subDir in the module's source directory. If any input paths are not located
1101// inside subDir then a path error will be reported.
1102func PathsWithModuleSrcSubDir(ctx ModuleContext, paths Paths, subDir string) Paths {
1103 paths = append(Paths(nil), paths...)
Colin Cross07e51612019-03-05 12:46:40 -08001104 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001105 for i, path := range paths {
1106 rel := Rel(ctx, subDirFullPath.String(), path.String())
1107 paths[i] = subDirFullPath.join(ctx, rel)
1108 }
1109 return paths
1110}
1111
1112// PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the
1113// module's source directory. If the input path is not located inside subDir then a path error will be reported.
1114func PathWithModuleSrcSubDir(ctx ModuleContext, path Path, subDir string) Path {
Colin Cross07e51612019-03-05 12:46:40 -08001115 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001116 rel := Rel(ctx, subDirFullPath.String(), path.String())
1117 return subDirFullPath.Join(ctx, rel)
1118}
1119
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001120// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
1121// valid path if p is non-nil.
Colin Cross635c3b02016-05-18 15:37:25 -07001122func OptionalPathForModuleSrc(ctx ModuleContext, p *string) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001123 if p == nil {
1124 return OptionalPath{}
1125 }
1126 return OptionalPathForPath(PathForModuleSrc(ctx, *p))
1127}
1128
Colin Cross07e51612019-03-05 12:46:40 -08001129func (p SourcePath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001130 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001131}
1132
Colin Cross07e51612019-03-05 12:46:40 -08001133func (p SourcePath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001134 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001135}
1136
Colin Cross07e51612019-03-05 12:46:40 -08001137func (p SourcePath) resPathWithName(ctx ModuleContext, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001138 // TODO: Use full directory if the new ctx is not the current ctx?
1139 return PathForModuleRes(ctx, p.path, name)
1140}
1141
1142// ModuleOutPath is a Path representing a module's output directory.
1143type ModuleOutPath struct {
1144 OutputPath
1145}
1146
1147var _ Path = ModuleOutPath{}
1148
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01001149func (p ModuleOutPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
1150 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1151}
1152
Colin Cross702e0f82017-10-18 17:27:54 -07001153func pathForModule(ctx ModuleContext) OutputPath {
1154 return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir())
1155}
1156
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001157type BazelOutPath struct {
1158 OutputPath
1159}
1160
1161var _ Path = BazelOutPath{}
1162var _ objPathProvider = BazelOutPath{}
1163
1164func (p BazelOutPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
1165 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1166}
1167
Logan Chien7eefdc42018-07-11 18:10:41 +08001168// PathForVndkRefAbiDump returns an OptionalPath representing the path of the
1169// reference abi dump for the given module. This is not guaranteed to be valid.
1170func PathForVndkRefAbiDump(ctx ModuleContext, version, fileName string,
Hsin-Yi Chen53489642019-07-31 17:10:45 +08001171 isNdk, isLlndkOrVndk, isGzip bool) OptionalPath {
Logan Chien7eefdc42018-07-11 18:10:41 +08001172
Jayant Chowdharyac066c62018-02-20 10:53:31 -08001173 arches := ctx.DeviceConfig().Arches()
Logan Chien7eefdc42018-07-11 18:10:41 +08001174 if len(arches) == 0 {
1175 panic("device build with no primary arch")
1176 }
Jayant Chowdharyac066c62018-02-20 10:53:31 -08001177 currentArch := ctx.Arch()
1178 archNameAndVariant := currentArch.ArchType.String()
1179 if currentArch.ArchVariant != "" {
1180 archNameAndVariant += "_" + currentArch.ArchVariant
1181 }
Logan Chien5237bed2018-07-11 17:15:57 +08001182
1183 var dirName string
Hsin-Yi Chen53489642019-07-31 17:10:45 +08001184 if isNdk {
Logan Chien5237bed2018-07-11 17:15:57 +08001185 dirName = "ndk"
Hsin-Yi Chen53489642019-07-31 17:10:45 +08001186 } else if isLlndkOrVndk {
Logan Chien5237bed2018-07-11 17:15:57 +08001187 dirName = "vndk"
Logan Chien41eabe62019-04-10 13:33:58 +08001188 } else {
1189 dirName = "platform" // opt-in libs
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001190 }
Logan Chien5237bed2018-07-11 17:15:57 +08001191
Jayant Chowdhary34ce67d2018-03-08 11:00:50 -08001192 binderBitness := ctx.DeviceConfig().BinderBitness()
Logan Chien7eefdc42018-07-11 18:10:41 +08001193
1194 var ext string
1195 if isGzip {
1196 ext = ".lsdump.gz"
1197 } else {
1198 ext = ".lsdump"
1199 }
1200
1201 return ExistentPathForSource(ctx, "prebuilts", "abi-dumps", dirName,
1202 version, binderBitness, archNameAndVariant, "source-based",
1203 fileName+ext)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001204}
1205
Chris Parsonsdbcb1ff2020-12-10 17:19:18 -05001206// PathForBazelOut returns a Path representing the paths... under an output directory dedicated to
1207// bazel-owned outputs.
1208func PathForBazelOut(ctx PathContext, paths ...string) BazelOutPath {
1209 execRootPathComponents := append([]string{"execroot", "__main__"}, paths...)
1210 execRootPath := filepath.Join(execRootPathComponents...)
1211 validatedExecRootPath, err := validatePath(execRootPath)
1212 if err != nil {
1213 reportPathError(ctx, err)
1214 }
1215
1216 outputPath := OutputPath{basePath{"", ctx.Config(), ""},
1217 ctx.Config().BazelContext.OutputBase()}
1218
1219 return BazelOutPath{
1220 OutputPath: outputPath.withRel(validatedExecRootPath),
1221 }
1222}
1223
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001224// PathForModuleOut returns a Path representing the paths... under the module's
1225// output directory.
Colin Cross635c3b02016-05-18 15:37:25 -07001226func PathForModuleOut(ctx ModuleContext, paths ...string) ModuleOutPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001227 p, err := validatePath(paths...)
1228 if err != nil {
1229 reportPathError(ctx, err)
1230 }
Colin Cross702e0f82017-10-18 17:27:54 -07001231 return ModuleOutPath{
1232 OutputPath: pathForModule(ctx).withRel(p),
1233 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001234}
1235
1236// ModuleGenPath is a Path representing the 'gen' directory in a module's output
1237// directory. Mainly used for generated sources.
1238type ModuleGenPath struct {
1239 ModuleOutPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001240}
1241
1242var _ Path = ModuleGenPath{}
1243var _ genPathProvider = ModuleGenPath{}
1244var _ objPathProvider = ModuleGenPath{}
1245
1246// PathForModuleGen returns a Path representing the paths... under the module's
1247// `gen' directory.
Colin Cross635c3b02016-05-18 15:37:25 -07001248func PathForModuleGen(ctx ModuleContext, paths ...string) ModuleGenPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001249 p, err := validatePath(paths...)
1250 if err != nil {
1251 reportPathError(ctx, err)
1252 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001253 return ModuleGenPath{
Colin Cross702e0f82017-10-18 17:27:54 -07001254 ModuleOutPath: ModuleOutPath{
1255 OutputPath: pathForModule(ctx).withRel("gen").withRel(p),
1256 },
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001257 }
1258}
1259
Dan Willemsen21ec4902016-11-02 20:43:13 -07001260func (p ModuleGenPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001261 // TODO: make a different path for local vs remote generated files?
Dan Willemsen21ec4902016-11-02 20:43:13 -07001262 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001263}
1264
Colin Cross635c3b02016-05-18 15:37:25 -07001265func (p ModuleGenPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001266 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1267}
1268
1269// ModuleObjPath is a Path representing the 'obj' directory in a module's output
1270// directory. Used for compiled objects.
1271type ModuleObjPath struct {
1272 ModuleOutPath
1273}
1274
1275var _ Path = ModuleObjPath{}
1276
1277// PathForModuleObj returns a Path representing the paths... under the module's
1278// 'obj' directory.
Jeff Gaston734e3802017-04-10 15:47:24 -07001279func PathForModuleObj(ctx ModuleContext, pathComponents ...string) ModuleObjPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001280 p, err := validatePath(pathComponents...)
1281 if err != nil {
1282 reportPathError(ctx, err)
1283 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001284 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
1285}
1286
1287// ModuleResPath is a a Path representing the 'res' directory in a module's
1288// output directory.
1289type ModuleResPath struct {
1290 ModuleOutPath
1291}
1292
1293var _ Path = ModuleResPath{}
1294
1295// PathForModuleRes returns a Path representing the paths... under the module's
1296// 'res' directory.
Jeff Gaston734e3802017-04-10 15:47:24 -07001297func PathForModuleRes(ctx ModuleContext, pathComponents ...string) ModuleResPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001298 p, err := validatePath(pathComponents...)
1299 if err != nil {
1300 reportPathError(ctx, err)
1301 }
1302
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001303 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
1304}
1305
Colin Cross70dda7e2019-10-01 22:05:35 -07001306// InstallPath is a Path representing a installed file path rooted from the build directory
1307type InstallPath struct {
1308 basePath
Colin Crossff6c33d2019-10-02 16:01:35 -07001309
Jiyong Park957bcd92020-10-20 18:23:33 +09001310 // partitionDir is the part of the InstallPath that is automatically determined according to the context.
1311 // For example, it is host/<os>-<arch> for host modules, and target/product/<device>/<partition> for device modules.
1312 partitionDir string
1313
1314 // makePath indicates whether this path is for Soong (false) or Make (true).
1315 makePath bool
Colin Cross70dda7e2019-10-01 22:05:35 -07001316}
1317
Paul Duffin9b478b02019-12-10 13:41:51 +00001318func (p InstallPath) buildDir() string {
1319 return p.config.buildDir
1320}
1321
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01001322func (p InstallPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1323 panic("Not implemented")
1324}
1325
Paul Duffin9b478b02019-12-10 13:41:51 +00001326var _ Path = InstallPath{}
1327var _ WritablePath = InstallPath{}
1328
Colin Cross70dda7e2019-10-01 22:05:35 -07001329func (p InstallPath) writablePath() {}
1330
1331func (p InstallPath) String() string {
Jiyong Park957bcd92020-10-20 18:23:33 +09001332 if p.makePath {
1333 // Make path starts with out/ instead of out/soong.
1334 return filepath.Join(p.config.buildDir, "../", p.path)
1335 } else {
1336 return filepath.Join(p.config.buildDir, p.path)
1337 }
1338}
1339
1340// PartitionDir returns the path to the partition where the install path is rooted at. It is
1341// out/soong/target/product/<device>/<partition> for device modules, and out/soong/host/<os>-<arch> for host modules.
1342// The ./soong is dropped if the install path is for Make.
1343func (p InstallPath) PartitionDir() string {
1344 if p.makePath {
1345 return filepath.Join(p.config.buildDir, "../", p.partitionDir)
1346 } else {
1347 return filepath.Join(p.config.buildDir, p.partitionDir)
1348 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001349}
1350
1351// Join creates a new InstallPath with paths... joined with the current path. The
1352// provided paths... may not use '..' to escape from the current path.
1353func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath {
1354 path, err := validatePath(paths...)
1355 if err != nil {
1356 reportPathError(ctx, err)
1357 }
1358 return p.withRel(path)
1359}
1360
1361func (p InstallPath) withRel(rel string) InstallPath {
1362 p.basePath = p.basePath.withRel(rel)
1363 return p
1364}
1365
Colin Crossff6c33d2019-10-02 16:01:35 -07001366// ToMakePath returns a new InstallPath that points to Make's install directory instead of Soong's,
1367// i.e. out/ instead of out/soong/.
1368func (p InstallPath) ToMakePath() InstallPath {
Jiyong Park957bcd92020-10-20 18:23:33 +09001369 p.makePath = true
Colin Crossff6c33d2019-10-02 16:01:35 -07001370 return p
Colin Cross70dda7e2019-10-01 22:05:35 -07001371}
1372
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001373// PathForModuleInstall returns a Path representing the install path for the
1374// module appended with paths...
Colin Cross70dda7e2019-10-01 22:05:35 -07001375func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
Colin Cross6e359402020-02-10 15:29:54 -08001376 os := ctx.Os()
Jiyong Park87788b52020-09-01 12:37:45 +09001377 arch := ctx.Arch().ArchType
1378 forceOS, forceArch := ctx.InstallForceOS()
1379 if forceOS != nil {
Colin Cross6e359402020-02-10 15:29:54 -08001380 os = *forceOS
1381 }
Jiyong Park87788b52020-09-01 12:37:45 +09001382 if forceArch != nil {
1383 arch = *forceArch
1384 }
Colin Cross6e359402020-02-10 15:29:54 -08001385 partition := modulePartition(ctx, os)
Colin Cross609c49a2020-02-13 13:20:11 -08001386
Jiyong Park87788b52020-09-01 12:37:45 +09001387 ret := pathForInstall(ctx, os, arch, partition, ctx.Debug(), pathComponents...)
Colin Cross609c49a2020-02-13 13:20:11 -08001388
Jingwen Chencda22c92020-11-23 00:22:30 -05001389 if ctx.InstallBypassMake() && ctx.Config().KatiEnabled() {
Colin Cross609c49a2020-02-13 13:20:11 -08001390 ret = ret.ToMakePath()
1391 }
1392
1393 return ret
1394}
1395
Jiyong Park87788b52020-09-01 12:37:45 +09001396func pathForInstall(ctx PathContext, os OsType, arch ArchType, partition string, debug bool,
Colin Cross609c49a2020-02-13 13:20:11 -08001397 pathComponents ...string) InstallPath {
1398
Jiyong Park957bcd92020-10-20 18:23:33 +09001399 var partionPaths []string
Colin Cross609c49a2020-02-13 13:20:11 -08001400
Colin Cross6e359402020-02-10 15:29:54 -08001401 if os.Class == Device {
Jiyong Park957bcd92020-10-20 18:23:33 +09001402 partionPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001403 } else {
Jiyong Park87788b52020-09-01 12:37:45 +09001404 osName := os.String()
1405 if os == Linux {
1406 // instead of linux_glibc
1407 osName = "linux"
Dan Willemsen866b5632017-09-22 12:28:24 -07001408 }
Jiyong Park87788b52020-09-01 12:37:45 +09001409 // SOONG_HOST_OUT is set to out/host/$(HOST_OS)-$(HOST_PREBUILT_ARCH)
1410 // and HOST_PREBUILT_ARCH is forcibly set to x86 even on x86_64 hosts. We don't seem
1411 // to have a plan to fix it (see the comment in build/make/core/envsetup.mk).
1412 // Let's keep using x86 for the existing cases until we have a need to support
1413 // other architectures.
1414 archName := arch.String()
1415 if os.Class == Host && (arch == X86_64 || arch == Common) {
1416 archName = "x86"
1417 }
Jiyong Park957bcd92020-10-20 18:23:33 +09001418 partionPaths = []string{"host", osName + "-" + archName, partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001419 }
Colin Cross609c49a2020-02-13 13:20:11 -08001420 if debug {
Jiyong Park957bcd92020-10-20 18:23:33 +09001421 partionPaths = append([]string{"debug"}, partionPaths...)
Dan Willemsen782a2d12015-12-21 14:55:28 -08001422 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001423
Jiyong Park957bcd92020-10-20 18:23:33 +09001424 partionPath, err := validatePath(partionPaths...)
Colin Cross70dda7e2019-10-01 22:05:35 -07001425 if err != nil {
1426 reportPathError(ctx, err)
1427 }
Colin Crossff6c33d2019-10-02 16:01:35 -07001428
Jiyong Park957bcd92020-10-20 18:23:33 +09001429 base := InstallPath{
1430 basePath: basePath{partionPath, ctx.Config(), ""},
1431 partitionDir: partionPath,
1432 makePath: false,
1433 }
Colin Crossff6c33d2019-10-02 16:01:35 -07001434
Jiyong Park957bcd92020-10-20 18:23:33 +09001435 return base.Join(ctx, pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001436}
1437
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00001438func pathForNdkOrSdkInstall(ctx PathContext, prefix string, paths []string) InstallPath {
Jiyong Park957bcd92020-10-20 18:23:33 +09001439 base := InstallPath{
1440 basePath: basePath{prefix, ctx.Config(), ""},
1441 partitionDir: prefix,
1442 makePath: false,
Colin Cross70dda7e2019-10-01 22:05:35 -07001443 }
Jiyong Park957bcd92020-10-20 18:23:33 +09001444 return base.Join(ctx, paths...)
Colin Cross70dda7e2019-10-01 22:05:35 -07001445}
1446
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00001447func PathForNdkInstall(ctx PathContext, paths ...string) InstallPath {
1448 return pathForNdkOrSdkInstall(ctx, "ndk", paths)
1449}
1450
1451func PathForMainlineSdksInstall(ctx PathContext, paths ...string) InstallPath {
1452 return pathForNdkOrSdkInstall(ctx, "mainline-sdks", paths)
1453}
1454
Colin Cross70dda7e2019-10-01 22:05:35 -07001455func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string {
Colin Cross43f08db2018-11-12 10:13:39 -08001456 rel := Rel(ctx, PathForOutput(ctx, "target", "product", ctx.Config().DeviceName()).String(), path.String())
1457
1458 return "/" + rel
1459}
1460
Colin Cross6e359402020-02-10 15:29:54 -08001461func modulePartition(ctx ModuleInstallPathContext, os OsType) string {
Colin Cross43f08db2018-11-12 10:13:39 -08001462 var partition string
Colin Cross6e359402020-02-10 15:29:54 -08001463 if ctx.InstallInTestcases() {
1464 // "testcases" install directory can be used for host or device modules.
Jaewoong Jung0949f312019-09-11 10:25:18 -07001465 partition = "testcases"
Colin Cross6e359402020-02-10 15:29:54 -08001466 } else if os.Class == Device {
1467 if ctx.InstallInData() {
1468 partition = "data"
1469 } else if ctx.InstallInRamdisk() {
1470 if ctx.DeviceConfig().BoardUsesRecoveryAsBoot() {
1471 partition = "recovery/root/first_stage_ramdisk"
1472 } else {
1473 partition = "ramdisk"
1474 }
1475 if !ctx.InstallInRoot() {
1476 partition += "/system"
1477 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -07001478 } else if ctx.InstallInVendorRamdisk() {
Yifan Hong39143a92020-10-26 12:43:12 -07001479 // The module is only available after switching root into
1480 // /first_stage_ramdisk. To expose the module before switching root
1481 // on a device without a dedicated recovery partition, install the
1482 // recovery variant.
Yifan Hongdd8dacc2020-10-21 15:40:17 -07001483 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
Yifan Hong39143a92020-10-26 12:43:12 -07001484 partition = "vendor-ramdisk/first_stage_ramdisk"
Yifan Hongdd8dacc2020-10-21 15:40:17 -07001485 } else {
1486 partition = "vendor-ramdisk"
1487 }
1488 if !ctx.InstallInRoot() {
1489 partition += "/system"
1490 }
Colin Cross6e359402020-02-10 15:29:54 -08001491 } else if ctx.InstallInRecovery() {
1492 if ctx.InstallInRoot() {
1493 partition = "recovery/root"
1494 } else {
1495 // the layout of recovery partion is the same as that of system partition
1496 partition = "recovery/root/system"
1497 }
1498 } else if ctx.SocSpecific() {
1499 partition = ctx.DeviceConfig().VendorPath()
1500 } else if ctx.DeviceSpecific() {
1501 partition = ctx.DeviceConfig().OdmPath()
1502 } else if ctx.ProductSpecific() {
1503 partition = ctx.DeviceConfig().ProductPath()
1504 } else if ctx.SystemExtSpecific() {
1505 partition = ctx.DeviceConfig().SystemExtPath()
1506 } else if ctx.InstallInRoot() {
1507 partition = "root"
Yifan Hong82db7352020-01-21 16:12:26 -08001508 } else {
Colin Cross6e359402020-02-10 15:29:54 -08001509 partition = "system"
Yifan Hong82db7352020-01-21 16:12:26 -08001510 }
Colin Cross6e359402020-02-10 15:29:54 -08001511 if ctx.InstallInSanitizerDir() {
1512 partition = "data/asan/" + partition
Yifan Hong82db7352020-01-21 16:12:26 -08001513 }
Colin Cross43f08db2018-11-12 10:13:39 -08001514 }
1515 return partition
1516}
1517
Colin Cross609c49a2020-02-13 13:20:11 -08001518type InstallPaths []InstallPath
1519
1520// Paths returns the InstallPaths as a Paths
1521func (p InstallPaths) Paths() Paths {
1522 if p == nil {
1523 return nil
1524 }
1525 ret := make(Paths, len(p))
1526 for i, path := range p {
1527 ret[i] = path
1528 }
1529 return ret
1530}
1531
1532// Strings returns the string forms of the install paths.
1533func (p InstallPaths) Strings() []string {
1534 if p == nil {
1535 return nil
1536 }
1537 ret := make([]string, len(p))
1538 for i, path := range p {
1539 ret[i] = path.String()
1540 }
1541 return ret
1542}
1543
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001544// validateSafePath validates a path that we trust (may contain ninja variables).
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001545// Ensures that each path component does not attempt to leave its component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001546func validateSafePath(pathComponents ...string) (string, error) {
Jeff Gaston734e3802017-04-10 15:47:24 -07001547 for _, path := range pathComponents {
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001548 path := filepath.Clean(path)
1549 if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001550 return "", fmt.Errorf("Path is outside directory: %s", path)
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001551 }
1552 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001553 // TODO: filepath.Join isn't necessarily correct with embedded ninja
1554 // variables. '..' may remove the entire ninja variable, even if it
1555 // will be expanded to multiple nested directories.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001556 return filepath.Join(pathComponents...), nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001557}
1558
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001559// validatePath validates that a path does not include ninja variables, and that
1560// each path component does not attempt to leave its component. Returns a joined
1561// version of each path component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001562func validatePath(pathComponents ...string) (string, error) {
Jeff Gaston734e3802017-04-10 15:47:24 -07001563 for _, path := range pathComponents {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001564 if strings.Contains(path, "$") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001565 return "", fmt.Errorf("Path contains invalid character($): %s", path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001566 }
1567 }
Colin Cross1ccfcc32018-02-22 13:54:26 -08001568 return validateSafePath(pathComponents...)
Colin Cross6e18ca42015-07-14 18:55:36 -07001569}
Colin Cross5b529592017-05-09 13:34:34 -07001570
Colin Cross0875c522017-11-28 17:34:01 -08001571func PathForPhony(ctx PathContext, phony string) WritablePath {
1572 if strings.ContainsAny(phony, "$/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001573 ReportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony)
Colin Cross0875c522017-11-28 17:34:01 -08001574 }
Colin Cross74e3fe42017-12-11 15:51:44 -08001575 return PhonyPath{basePath{phony, ctx.Config(), ""}}
Colin Cross0875c522017-11-28 17:34:01 -08001576}
1577
Colin Cross74e3fe42017-12-11 15:51:44 -08001578type PhonyPath struct {
1579 basePath
1580}
1581
1582func (p PhonyPath) writablePath() {}
1583
Paul Duffin9b478b02019-12-10 13:41:51 +00001584func (p PhonyPath) buildDir() string {
1585 return p.config.buildDir
1586}
1587
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01001588func (p PhonyPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1589 panic("Not implemented")
1590}
1591
Colin Cross74e3fe42017-12-11 15:51:44 -08001592var _ Path = PhonyPath{}
1593var _ WritablePath = PhonyPath{}
1594
Colin Cross5b529592017-05-09 13:34:34 -07001595type testPath struct {
1596 basePath
1597}
1598
1599func (p testPath) String() string {
1600 return p.path
1601}
1602
Colin Cross40e33732019-02-15 11:08:35 -08001603// PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from
1604// within tests.
Colin Cross5b529592017-05-09 13:34:34 -07001605func PathForTesting(paths ...string) Path {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001606 p, err := validateSafePath(paths...)
1607 if err != nil {
1608 panic(err)
1609 }
Colin Cross5b529592017-05-09 13:34:34 -07001610 return testPath{basePath{path: p, rel: p}}
1611}
1612
Colin Cross40e33732019-02-15 11:08:35 -08001613// PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests.
1614func PathsForTesting(strs ...string) Paths {
Colin Cross5b529592017-05-09 13:34:34 -07001615 p := make(Paths, len(strs))
1616 for i, s := range strs {
1617 p[i] = PathForTesting(s)
1618 }
1619
1620 return p
1621}
Colin Cross43f08db2018-11-12 10:13:39 -08001622
Colin Cross40e33732019-02-15 11:08:35 -08001623type testPathContext struct {
1624 config Config
Colin Cross40e33732019-02-15 11:08:35 -08001625}
1626
Colin Cross40e33732019-02-15 11:08:35 -08001627func (x *testPathContext) Config() Config { return x.config }
1628func (x *testPathContext) AddNinjaFileDeps(...string) {}
1629
1630// PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with
1631// PathForOutput.
Colin Cross98be1bb2019-12-13 20:41:13 -08001632func PathContextForTesting(config Config) PathContext {
Colin Cross40e33732019-02-15 11:08:35 -08001633 return &testPathContext{
1634 config: config,
Colin Cross40e33732019-02-15 11:08:35 -08001635 }
1636}
1637
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01001638type testModuleInstallPathContext struct {
1639 baseModuleContext
1640
1641 inData bool
1642 inTestcases bool
1643 inSanitizerDir bool
1644 inRamdisk bool
1645 inVendorRamdisk bool
1646 inRecovery bool
1647 inRoot bool
1648 forceOS *OsType
1649 forceArch *ArchType
1650}
1651
1652func (m testModuleInstallPathContext) Config() Config {
1653 return m.baseModuleContext.config
1654}
1655
1656func (testModuleInstallPathContext) AddNinjaFileDeps(deps ...string) {}
1657
1658func (m testModuleInstallPathContext) InstallInData() bool {
1659 return m.inData
1660}
1661
1662func (m testModuleInstallPathContext) InstallInTestcases() bool {
1663 return m.inTestcases
1664}
1665
1666func (m testModuleInstallPathContext) InstallInSanitizerDir() bool {
1667 return m.inSanitizerDir
1668}
1669
1670func (m testModuleInstallPathContext) InstallInRamdisk() bool {
1671 return m.inRamdisk
1672}
1673
1674func (m testModuleInstallPathContext) InstallInVendorRamdisk() bool {
1675 return m.inVendorRamdisk
1676}
1677
1678func (m testModuleInstallPathContext) InstallInRecovery() bool {
1679 return m.inRecovery
1680}
1681
1682func (m testModuleInstallPathContext) InstallInRoot() bool {
1683 return m.inRoot
1684}
1685
1686func (m testModuleInstallPathContext) InstallBypassMake() bool {
1687 return false
1688}
1689
1690func (m testModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
1691 return m.forceOS, m.forceArch
1692}
1693
1694// Construct a minimal ModuleInstallPathContext for testing. Note that baseModuleContext is
1695// default-initialized, which leaves blueprint.baseModuleContext set to nil, so methods that are
1696// delegated to it will panic.
1697func ModuleInstallPathContextForTesting(config Config) ModuleInstallPathContext {
1698 ctx := &testModuleInstallPathContext{}
1699 ctx.config = config
1700 ctx.os = Android
1701 return ctx
1702}
1703
Colin Cross43f08db2018-11-12 10:13:39 -08001704// Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if
1705// targetPath is not inside basePath.
1706func Rel(ctx PathContext, basePath string, targetPath string) string {
1707 rel, isRel := MaybeRel(ctx, basePath, targetPath)
1708 if !isRel {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001709 ReportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath)
Colin Cross43f08db2018-11-12 10:13:39 -08001710 return ""
1711 }
1712 return rel
1713}
1714
1715// MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if
1716// targetPath is not inside basePath.
1717func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) {
Dan Willemsen633c5022019-04-12 11:11:38 -07001718 rel, isRel, err := maybeRelErr(basePath, targetPath)
1719 if err != nil {
1720 reportPathError(ctx, err)
1721 }
1722 return rel, isRel
1723}
1724
1725func maybeRelErr(basePath string, targetPath string) (string, bool, error) {
Colin Cross43f08db2018-11-12 10:13:39 -08001726 // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first.
1727 if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) {
Dan Willemsen633c5022019-04-12 11:11:38 -07001728 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08001729 }
1730 rel, err := filepath.Rel(basePath, targetPath)
1731 if err != nil {
Dan Willemsen633c5022019-04-12 11:11:38 -07001732 return "", false, err
Colin Cross43f08db2018-11-12 10:13:39 -08001733 } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") {
Dan Willemsen633c5022019-04-12 11:11:38 -07001734 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08001735 }
Dan Willemsen633c5022019-04-12 11:11:38 -07001736 return rel, true, nil
Colin Cross43f08db2018-11-12 10:13:39 -08001737}
Colin Cross988414c2020-01-11 01:11:46 +00001738
1739// Writes a file to the output directory. Attempting to write directly to the output directory
1740// will fail due to the sandbox of the soong_build process.
1741func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error {
1742 return ioutil.WriteFile(absolutePath(path.String()), data, perm)
1743}
1744
1745func absolutePath(path string) string {
1746 if filepath.IsAbs(path) {
1747 return path
1748 }
1749 return filepath.Join(absSrcDir, path)
1750}
Chris Parsons216e10a2020-07-09 17:12:52 -04001751
1752// A DataPath represents the path of a file to be used as data, for example
1753// a test library to be installed alongside a test.
1754// The data file should be installed (copied from `<SrcPath>`) to
1755// `<install_root>/<RelativeInstallPath>/<filename>`, or
1756// `<install_root>/<filename>` if RelativeInstallPath is empty.
1757type DataPath struct {
1758 // The path of the data file that should be copied into the data directory
1759 SrcPath Path
1760 // The install path of the data file, relative to the install root.
1761 RelativeInstallPath string
1762}