blob: 0238a3fcf3087bcc3ca6c89f7618c98e1db80054 [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
Logan Chien7eefdc42018-07-11 18:10:41 +08001157// PathForVndkRefAbiDump returns an OptionalPath representing the path of the
1158// reference abi dump for the given module. This is not guaranteed to be valid.
1159func PathForVndkRefAbiDump(ctx ModuleContext, version, fileName string,
Hsin-Yi Chen53489642019-07-31 17:10:45 +08001160 isNdk, isLlndkOrVndk, isGzip bool) OptionalPath {
Logan Chien7eefdc42018-07-11 18:10:41 +08001161
Jayant Chowdharyac066c62018-02-20 10:53:31 -08001162 arches := ctx.DeviceConfig().Arches()
Logan Chien7eefdc42018-07-11 18:10:41 +08001163 if len(arches) == 0 {
1164 panic("device build with no primary arch")
1165 }
Jayant Chowdharyac066c62018-02-20 10:53:31 -08001166 currentArch := ctx.Arch()
1167 archNameAndVariant := currentArch.ArchType.String()
1168 if currentArch.ArchVariant != "" {
1169 archNameAndVariant += "_" + currentArch.ArchVariant
1170 }
Logan Chien5237bed2018-07-11 17:15:57 +08001171
1172 var dirName string
Hsin-Yi Chen53489642019-07-31 17:10:45 +08001173 if isNdk {
Logan Chien5237bed2018-07-11 17:15:57 +08001174 dirName = "ndk"
Hsin-Yi Chen53489642019-07-31 17:10:45 +08001175 } else if isLlndkOrVndk {
Logan Chien5237bed2018-07-11 17:15:57 +08001176 dirName = "vndk"
Logan Chien41eabe62019-04-10 13:33:58 +08001177 } else {
1178 dirName = "platform" // opt-in libs
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001179 }
Logan Chien5237bed2018-07-11 17:15:57 +08001180
Jayant Chowdhary34ce67d2018-03-08 11:00:50 -08001181 binderBitness := ctx.DeviceConfig().BinderBitness()
Logan Chien7eefdc42018-07-11 18:10:41 +08001182
1183 var ext string
1184 if isGzip {
1185 ext = ".lsdump.gz"
1186 } else {
1187 ext = ".lsdump"
1188 }
1189
1190 return ExistentPathForSource(ctx, "prebuilts", "abi-dumps", dirName,
1191 version, binderBitness, archNameAndVariant, "source-based",
1192 fileName+ext)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001193}
1194
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001195// PathForModuleOut returns a Path representing the paths... under the module's
1196// output directory.
Colin Cross635c3b02016-05-18 15:37:25 -07001197func PathForModuleOut(ctx ModuleContext, paths ...string) ModuleOutPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001198 p, err := validatePath(paths...)
1199 if err != nil {
1200 reportPathError(ctx, err)
1201 }
Colin Cross702e0f82017-10-18 17:27:54 -07001202 return ModuleOutPath{
1203 OutputPath: pathForModule(ctx).withRel(p),
1204 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001205}
1206
1207// ModuleGenPath is a Path representing the 'gen' directory in a module's output
1208// directory. Mainly used for generated sources.
1209type ModuleGenPath struct {
1210 ModuleOutPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001211}
1212
1213var _ Path = ModuleGenPath{}
1214var _ genPathProvider = ModuleGenPath{}
1215var _ objPathProvider = ModuleGenPath{}
1216
1217// PathForModuleGen returns a Path representing the paths... under the module's
1218// `gen' directory.
Colin Cross635c3b02016-05-18 15:37:25 -07001219func PathForModuleGen(ctx ModuleContext, paths ...string) ModuleGenPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001220 p, err := validatePath(paths...)
1221 if err != nil {
1222 reportPathError(ctx, err)
1223 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001224 return ModuleGenPath{
Colin Cross702e0f82017-10-18 17:27:54 -07001225 ModuleOutPath: ModuleOutPath{
1226 OutputPath: pathForModule(ctx).withRel("gen").withRel(p),
1227 },
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001228 }
1229}
1230
Dan Willemsen21ec4902016-11-02 20:43:13 -07001231func (p ModuleGenPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001232 // TODO: make a different path for local vs remote generated files?
Dan Willemsen21ec4902016-11-02 20:43:13 -07001233 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001234}
1235
Colin Cross635c3b02016-05-18 15:37:25 -07001236func (p ModuleGenPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001237 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1238}
1239
1240// ModuleObjPath is a Path representing the 'obj' directory in a module's output
1241// directory. Used for compiled objects.
1242type ModuleObjPath struct {
1243 ModuleOutPath
1244}
1245
1246var _ Path = ModuleObjPath{}
1247
1248// PathForModuleObj returns a Path representing the paths... under the module's
1249// 'obj' directory.
Jeff Gaston734e3802017-04-10 15:47:24 -07001250func PathForModuleObj(ctx ModuleContext, pathComponents ...string) ModuleObjPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001251 p, err := validatePath(pathComponents...)
1252 if err != nil {
1253 reportPathError(ctx, err)
1254 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001255 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
1256}
1257
1258// ModuleResPath is a a Path representing the 'res' directory in a module's
1259// output directory.
1260type ModuleResPath struct {
1261 ModuleOutPath
1262}
1263
1264var _ Path = ModuleResPath{}
1265
1266// PathForModuleRes returns a Path representing the paths... under the module's
1267// 'res' directory.
Jeff Gaston734e3802017-04-10 15:47:24 -07001268func PathForModuleRes(ctx ModuleContext, pathComponents ...string) ModuleResPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001269 p, err := validatePath(pathComponents...)
1270 if err != nil {
1271 reportPathError(ctx, err)
1272 }
1273
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001274 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
1275}
1276
Colin Cross70dda7e2019-10-01 22:05:35 -07001277// InstallPath is a Path representing a installed file path rooted from the build directory
1278type InstallPath struct {
1279 basePath
Colin Crossff6c33d2019-10-02 16:01:35 -07001280
Jiyong Park957bcd92020-10-20 18:23:33 +09001281 // partitionDir is the part of the InstallPath that is automatically determined according to the context.
1282 // For example, it is host/<os>-<arch> for host modules, and target/product/<device>/<partition> for device modules.
1283 partitionDir string
1284
1285 // makePath indicates whether this path is for Soong (false) or Make (true).
1286 makePath bool
Colin Cross70dda7e2019-10-01 22:05:35 -07001287}
1288
Paul Duffin9b478b02019-12-10 13:41:51 +00001289func (p InstallPath) buildDir() string {
1290 return p.config.buildDir
1291}
1292
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01001293func (p InstallPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1294 panic("Not implemented")
1295}
1296
Paul Duffin9b478b02019-12-10 13:41:51 +00001297var _ Path = InstallPath{}
1298var _ WritablePath = InstallPath{}
1299
Colin Cross70dda7e2019-10-01 22:05:35 -07001300func (p InstallPath) writablePath() {}
1301
1302func (p InstallPath) String() string {
Jiyong Park957bcd92020-10-20 18:23:33 +09001303 if p.makePath {
1304 // Make path starts with out/ instead of out/soong.
1305 return filepath.Join(p.config.buildDir, "../", p.path)
1306 } else {
1307 return filepath.Join(p.config.buildDir, p.path)
1308 }
1309}
1310
1311// PartitionDir returns the path to the partition where the install path is rooted at. It is
1312// out/soong/target/product/<device>/<partition> for device modules, and out/soong/host/<os>-<arch> for host modules.
1313// The ./soong is dropped if the install path is for Make.
1314func (p InstallPath) PartitionDir() string {
1315 if p.makePath {
1316 return filepath.Join(p.config.buildDir, "../", p.partitionDir)
1317 } else {
1318 return filepath.Join(p.config.buildDir, p.partitionDir)
1319 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001320}
1321
1322// Join creates a new InstallPath with paths... joined with the current path. The
1323// provided paths... may not use '..' to escape from the current path.
1324func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath {
1325 path, err := validatePath(paths...)
1326 if err != nil {
1327 reportPathError(ctx, err)
1328 }
1329 return p.withRel(path)
1330}
1331
1332func (p InstallPath) withRel(rel string) InstallPath {
1333 p.basePath = p.basePath.withRel(rel)
1334 return p
1335}
1336
Colin Crossff6c33d2019-10-02 16:01:35 -07001337// ToMakePath returns a new InstallPath that points to Make's install directory instead of Soong's,
1338// i.e. out/ instead of out/soong/.
1339func (p InstallPath) ToMakePath() InstallPath {
Jiyong Park957bcd92020-10-20 18:23:33 +09001340 p.makePath = true
Colin Crossff6c33d2019-10-02 16:01:35 -07001341 return p
Colin Cross70dda7e2019-10-01 22:05:35 -07001342}
1343
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001344// PathForModuleInstall returns a Path representing the install path for the
1345// module appended with paths...
Colin Cross70dda7e2019-10-01 22:05:35 -07001346func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
Colin Cross6e359402020-02-10 15:29:54 -08001347 os := ctx.Os()
Jiyong Park87788b52020-09-01 12:37:45 +09001348 arch := ctx.Arch().ArchType
1349 forceOS, forceArch := ctx.InstallForceOS()
1350 if forceOS != nil {
Colin Cross6e359402020-02-10 15:29:54 -08001351 os = *forceOS
1352 }
Jiyong Park87788b52020-09-01 12:37:45 +09001353 if forceArch != nil {
1354 arch = *forceArch
1355 }
Colin Cross6e359402020-02-10 15:29:54 -08001356 partition := modulePartition(ctx, os)
Colin Cross609c49a2020-02-13 13:20:11 -08001357
Jiyong Park87788b52020-09-01 12:37:45 +09001358 ret := pathForInstall(ctx, os, arch, partition, ctx.Debug(), pathComponents...)
Colin Cross609c49a2020-02-13 13:20:11 -08001359
Jingwen Chencda22c92020-11-23 00:22:30 -05001360 if ctx.InstallBypassMake() && ctx.Config().KatiEnabled() {
Colin Cross609c49a2020-02-13 13:20:11 -08001361 ret = ret.ToMakePath()
1362 }
1363
1364 return ret
1365}
1366
Jiyong Park87788b52020-09-01 12:37:45 +09001367func pathForInstall(ctx PathContext, os OsType, arch ArchType, partition string, debug bool,
Colin Cross609c49a2020-02-13 13:20:11 -08001368 pathComponents ...string) InstallPath {
1369
Jiyong Park957bcd92020-10-20 18:23:33 +09001370 var partionPaths []string
Colin Cross609c49a2020-02-13 13:20:11 -08001371
Colin Cross6e359402020-02-10 15:29:54 -08001372 if os.Class == Device {
Jiyong Park957bcd92020-10-20 18:23:33 +09001373 partionPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001374 } else {
Jiyong Park87788b52020-09-01 12:37:45 +09001375 osName := os.String()
1376 if os == Linux {
1377 // instead of linux_glibc
1378 osName = "linux"
Dan Willemsen866b5632017-09-22 12:28:24 -07001379 }
Jiyong Park87788b52020-09-01 12:37:45 +09001380 // SOONG_HOST_OUT is set to out/host/$(HOST_OS)-$(HOST_PREBUILT_ARCH)
1381 // and HOST_PREBUILT_ARCH is forcibly set to x86 even on x86_64 hosts. We don't seem
1382 // to have a plan to fix it (see the comment in build/make/core/envsetup.mk).
1383 // Let's keep using x86 for the existing cases until we have a need to support
1384 // other architectures.
1385 archName := arch.String()
1386 if os.Class == Host && (arch == X86_64 || arch == Common) {
1387 archName = "x86"
1388 }
Jiyong Park957bcd92020-10-20 18:23:33 +09001389 partionPaths = []string{"host", osName + "-" + archName, partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001390 }
Colin Cross609c49a2020-02-13 13:20:11 -08001391 if debug {
Jiyong Park957bcd92020-10-20 18:23:33 +09001392 partionPaths = append([]string{"debug"}, partionPaths...)
Dan Willemsen782a2d12015-12-21 14:55:28 -08001393 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001394
Jiyong Park957bcd92020-10-20 18:23:33 +09001395 partionPath, err := validatePath(partionPaths...)
Colin Cross70dda7e2019-10-01 22:05:35 -07001396 if err != nil {
1397 reportPathError(ctx, err)
1398 }
Colin Crossff6c33d2019-10-02 16:01:35 -07001399
Jiyong Park957bcd92020-10-20 18:23:33 +09001400 base := InstallPath{
1401 basePath: basePath{partionPath, ctx.Config(), ""},
1402 partitionDir: partionPath,
1403 makePath: false,
1404 }
Colin Crossff6c33d2019-10-02 16:01:35 -07001405
Jiyong Park957bcd92020-10-20 18:23:33 +09001406 return base.Join(ctx, pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001407}
1408
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00001409func pathForNdkOrSdkInstall(ctx PathContext, prefix string, paths []string) InstallPath {
Jiyong Park957bcd92020-10-20 18:23:33 +09001410 base := InstallPath{
1411 basePath: basePath{prefix, ctx.Config(), ""},
1412 partitionDir: prefix,
1413 makePath: false,
Colin Cross70dda7e2019-10-01 22:05:35 -07001414 }
Jiyong Park957bcd92020-10-20 18:23:33 +09001415 return base.Join(ctx, paths...)
Colin Cross70dda7e2019-10-01 22:05:35 -07001416}
1417
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00001418func PathForNdkInstall(ctx PathContext, paths ...string) InstallPath {
1419 return pathForNdkOrSdkInstall(ctx, "ndk", paths)
1420}
1421
1422func PathForMainlineSdksInstall(ctx PathContext, paths ...string) InstallPath {
1423 return pathForNdkOrSdkInstall(ctx, "mainline-sdks", paths)
1424}
1425
Colin Cross70dda7e2019-10-01 22:05:35 -07001426func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string {
Colin Cross43f08db2018-11-12 10:13:39 -08001427 rel := Rel(ctx, PathForOutput(ctx, "target", "product", ctx.Config().DeviceName()).String(), path.String())
1428
1429 return "/" + rel
1430}
1431
Colin Cross6e359402020-02-10 15:29:54 -08001432func modulePartition(ctx ModuleInstallPathContext, os OsType) string {
Colin Cross43f08db2018-11-12 10:13:39 -08001433 var partition string
Colin Cross6e359402020-02-10 15:29:54 -08001434 if ctx.InstallInTestcases() {
1435 // "testcases" install directory can be used for host or device modules.
Jaewoong Jung0949f312019-09-11 10:25:18 -07001436 partition = "testcases"
Colin Cross6e359402020-02-10 15:29:54 -08001437 } else if os.Class == Device {
1438 if ctx.InstallInData() {
1439 partition = "data"
1440 } else if ctx.InstallInRamdisk() {
1441 if ctx.DeviceConfig().BoardUsesRecoveryAsBoot() {
1442 partition = "recovery/root/first_stage_ramdisk"
1443 } else {
1444 partition = "ramdisk"
1445 }
1446 if !ctx.InstallInRoot() {
1447 partition += "/system"
1448 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -07001449 } else if ctx.InstallInVendorRamdisk() {
Yifan Hong39143a92020-10-26 12:43:12 -07001450 // The module is only available after switching root into
1451 // /first_stage_ramdisk. To expose the module before switching root
1452 // on a device without a dedicated recovery partition, install the
1453 // recovery variant.
Yifan Hongdd8dacc2020-10-21 15:40:17 -07001454 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
Yifan Hong39143a92020-10-26 12:43:12 -07001455 partition = "vendor-ramdisk/first_stage_ramdisk"
Yifan Hongdd8dacc2020-10-21 15:40:17 -07001456 } else {
1457 partition = "vendor-ramdisk"
1458 }
1459 if !ctx.InstallInRoot() {
1460 partition += "/system"
1461 }
Colin Cross6e359402020-02-10 15:29:54 -08001462 } else if ctx.InstallInRecovery() {
1463 if ctx.InstallInRoot() {
1464 partition = "recovery/root"
1465 } else {
1466 // the layout of recovery partion is the same as that of system partition
1467 partition = "recovery/root/system"
1468 }
1469 } else if ctx.SocSpecific() {
1470 partition = ctx.DeviceConfig().VendorPath()
1471 } else if ctx.DeviceSpecific() {
1472 partition = ctx.DeviceConfig().OdmPath()
1473 } else if ctx.ProductSpecific() {
1474 partition = ctx.DeviceConfig().ProductPath()
1475 } else if ctx.SystemExtSpecific() {
1476 partition = ctx.DeviceConfig().SystemExtPath()
1477 } else if ctx.InstallInRoot() {
1478 partition = "root"
Yifan Hong82db7352020-01-21 16:12:26 -08001479 } else {
Colin Cross6e359402020-02-10 15:29:54 -08001480 partition = "system"
Yifan Hong82db7352020-01-21 16:12:26 -08001481 }
Colin Cross6e359402020-02-10 15:29:54 -08001482 if ctx.InstallInSanitizerDir() {
1483 partition = "data/asan/" + partition
Yifan Hong82db7352020-01-21 16:12:26 -08001484 }
Colin Cross43f08db2018-11-12 10:13:39 -08001485 }
1486 return partition
1487}
1488
Colin Cross609c49a2020-02-13 13:20:11 -08001489type InstallPaths []InstallPath
1490
1491// Paths returns the InstallPaths as a Paths
1492func (p InstallPaths) Paths() Paths {
1493 if p == nil {
1494 return nil
1495 }
1496 ret := make(Paths, len(p))
1497 for i, path := range p {
1498 ret[i] = path
1499 }
1500 return ret
1501}
1502
1503// Strings returns the string forms of the install paths.
1504func (p InstallPaths) Strings() []string {
1505 if p == nil {
1506 return nil
1507 }
1508 ret := make([]string, len(p))
1509 for i, path := range p {
1510 ret[i] = path.String()
1511 }
1512 return ret
1513}
1514
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001515// validateSafePath validates a path that we trust (may contain ninja variables).
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001516// Ensures that each path component does not attempt to leave its component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001517func validateSafePath(pathComponents ...string) (string, error) {
Jeff Gaston734e3802017-04-10 15:47:24 -07001518 for _, path := range pathComponents {
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001519 path := filepath.Clean(path)
1520 if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001521 return "", fmt.Errorf("Path is outside directory: %s", path)
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001522 }
1523 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001524 // TODO: filepath.Join isn't necessarily correct with embedded ninja
1525 // variables. '..' may remove the entire ninja variable, even if it
1526 // will be expanded to multiple nested directories.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001527 return filepath.Join(pathComponents...), nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001528}
1529
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08001530// validatePath validates that a path does not include ninja variables, and that
1531// each path component does not attempt to leave its component. Returns a joined
1532// version of each path component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001533func validatePath(pathComponents ...string) (string, error) {
Jeff Gaston734e3802017-04-10 15:47:24 -07001534 for _, path := range pathComponents {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001535 if strings.Contains(path, "$") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001536 return "", fmt.Errorf("Path contains invalid character($): %s", path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001537 }
1538 }
Colin Cross1ccfcc32018-02-22 13:54:26 -08001539 return validateSafePath(pathComponents...)
Colin Cross6e18ca42015-07-14 18:55:36 -07001540}
Colin Cross5b529592017-05-09 13:34:34 -07001541
Colin Cross0875c522017-11-28 17:34:01 -08001542func PathForPhony(ctx PathContext, phony string) WritablePath {
1543 if strings.ContainsAny(phony, "$/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001544 ReportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony)
Colin Cross0875c522017-11-28 17:34:01 -08001545 }
Colin Cross74e3fe42017-12-11 15:51:44 -08001546 return PhonyPath{basePath{phony, ctx.Config(), ""}}
Colin Cross0875c522017-11-28 17:34:01 -08001547}
1548
Colin Cross74e3fe42017-12-11 15:51:44 -08001549type PhonyPath struct {
1550 basePath
1551}
1552
1553func (p PhonyPath) writablePath() {}
1554
Paul Duffin9b478b02019-12-10 13:41:51 +00001555func (p PhonyPath) buildDir() string {
1556 return p.config.buildDir
1557}
1558
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01001559func (p PhonyPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1560 panic("Not implemented")
1561}
1562
Colin Cross74e3fe42017-12-11 15:51:44 -08001563var _ Path = PhonyPath{}
1564var _ WritablePath = PhonyPath{}
1565
Colin Cross5b529592017-05-09 13:34:34 -07001566type testPath struct {
1567 basePath
1568}
1569
1570func (p testPath) String() string {
1571 return p.path
1572}
1573
Colin Cross40e33732019-02-15 11:08:35 -08001574// PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from
1575// within tests.
Colin Cross5b529592017-05-09 13:34:34 -07001576func PathForTesting(paths ...string) Path {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001577 p, err := validateSafePath(paths...)
1578 if err != nil {
1579 panic(err)
1580 }
Colin Cross5b529592017-05-09 13:34:34 -07001581 return testPath{basePath{path: p, rel: p}}
1582}
1583
Colin Cross40e33732019-02-15 11:08:35 -08001584// PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests.
1585func PathsForTesting(strs ...string) Paths {
Colin Cross5b529592017-05-09 13:34:34 -07001586 p := make(Paths, len(strs))
1587 for i, s := range strs {
1588 p[i] = PathForTesting(s)
1589 }
1590
1591 return p
1592}
Colin Cross43f08db2018-11-12 10:13:39 -08001593
Colin Cross40e33732019-02-15 11:08:35 -08001594type testPathContext struct {
1595 config Config
Colin Cross40e33732019-02-15 11:08:35 -08001596}
1597
Colin Cross40e33732019-02-15 11:08:35 -08001598func (x *testPathContext) Config() Config { return x.config }
1599func (x *testPathContext) AddNinjaFileDeps(...string) {}
1600
1601// PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with
1602// PathForOutput.
Colin Cross98be1bb2019-12-13 20:41:13 -08001603func PathContextForTesting(config Config) PathContext {
Colin Cross40e33732019-02-15 11:08:35 -08001604 return &testPathContext{
1605 config: config,
Colin Cross40e33732019-02-15 11:08:35 -08001606 }
1607}
1608
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01001609type testModuleInstallPathContext struct {
1610 baseModuleContext
1611
1612 inData bool
1613 inTestcases bool
1614 inSanitizerDir bool
1615 inRamdisk bool
1616 inVendorRamdisk bool
1617 inRecovery bool
1618 inRoot bool
1619 forceOS *OsType
1620 forceArch *ArchType
1621}
1622
1623func (m testModuleInstallPathContext) Config() Config {
1624 return m.baseModuleContext.config
1625}
1626
1627func (testModuleInstallPathContext) AddNinjaFileDeps(deps ...string) {}
1628
1629func (m testModuleInstallPathContext) InstallInData() bool {
1630 return m.inData
1631}
1632
1633func (m testModuleInstallPathContext) InstallInTestcases() bool {
1634 return m.inTestcases
1635}
1636
1637func (m testModuleInstallPathContext) InstallInSanitizerDir() bool {
1638 return m.inSanitizerDir
1639}
1640
1641func (m testModuleInstallPathContext) InstallInRamdisk() bool {
1642 return m.inRamdisk
1643}
1644
1645func (m testModuleInstallPathContext) InstallInVendorRamdisk() bool {
1646 return m.inVendorRamdisk
1647}
1648
1649func (m testModuleInstallPathContext) InstallInRecovery() bool {
1650 return m.inRecovery
1651}
1652
1653func (m testModuleInstallPathContext) InstallInRoot() bool {
1654 return m.inRoot
1655}
1656
1657func (m testModuleInstallPathContext) InstallBypassMake() bool {
1658 return false
1659}
1660
1661func (m testModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
1662 return m.forceOS, m.forceArch
1663}
1664
1665// Construct a minimal ModuleInstallPathContext for testing. Note that baseModuleContext is
1666// default-initialized, which leaves blueprint.baseModuleContext set to nil, so methods that are
1667// delegated to it will panic.
1668func ModuleInstallPathContextForTesting(config Config) ModuleInstallPathContext {
1669 ctx := &testModuleInstallPathContext{}
1670 ctx.config = config
1671 ctx.os = Android
1672 return ctx
1673}
1674
Colin Cross43f08db2018-11-12 10:13:39 -08001675// Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if
1676// targetPath is not inside basePath.
1677func Rel(ctx PathContext, basePath string, targetPath string) string {
1678 rel, isRel := MaybeRel(ctx, basePath, targetPath)
1679 if !isRel {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001680 ReportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath)
Colin Cross43f08db2018-11-12 10:13:39 -08001681 return ""
1682 }
1683 return rel
1684}
1685
1686// MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if
1687// targetPath is not inside basePath.
1688func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) {
Dan Willemsen633c5022019-04-12 11:11:38 -07001689 rel, isRel, err := maybeRelErr(basePath, targetPath)
1690 if err != nil {
1691 reportPathError(ctx, err)
1692 }
1693 return rel, isRel
1694}
1695
1696func maybeRelErr(basePath string, targetPath string) (string, bool, error) {
Colin Cross43f08db2018-11-12 10:13:39 -08001697 // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first.
1698 if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) {
Dan Willemsen633c5022019-04-12 11:11:38 -07001699 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08001700 }
1701 rel, err := filepath.Rel(basePath, targetPath)
1702 if err != nil {
Dan Willemsen633c5022019-04-12 11:11:38 -07001703 return "", false, err
Colin Cross43f08db2018-11-12 10:13:39 -08001704 } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") {
Dan Willemsen633c5022019-04-12 11:11:38 -07001705 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08001706 }
Dan Willemsen633c5022019-04-12 11:11:38 -07001707 return rel, true, nil
Colin Cross43f08db2018-11-12 10:13:39 -08001708}
Colin Cross988414c2020-01-11 01:11:46 +00001709
1710// Writes a file to the output directory. Attempting to write directly to the output directory
1711// will fail due to the sandbox of the soong_build process.
1712func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error {
1713 return ioutil.WriteFile(absolutePath(path.String()), data, perm)
1714}
1715
1716func absolutePath(path string) string {
1717 if filepath.IsAbs(path) {
1718 return path
1719 }
1720 return filepath.Join(absSrcDir, path)
1721}
Chris Parsons216e10a2020-07-09 17:12:52 -04001722
1723// A DataPath represents the path of a file to be used as data, for example
1724// a test library to be installed alongside a test.
1725// The data file should be installed (copied from `<SrcPath>`) to
1726// `<install_root>/<RelativeInstallPath>/<filename>`, or
1727// `<install_root>/<filename>` if RelativeInstallPath is empty.
1728type DataPath struct {
1729 // The path of the data file that should be copied into the data directory
1730 SrcPath Path
1731 // The install path of the data file, relative to the install root.
1732 RelativeInstallPath string
1733}