blob: dda48dd5433ea8c15c17f70c4d29eea1e9fb7d32 [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 (
Yu Liufa297642024-06-11 00:13:02 +000018 "bytes"
19 "encoding/gob"
20 "errors"
Colin Cross6e18ca42015-07-14 18:55:36 -070021 "fmt"
Colin Cross988414c2020-01-11 01:11:46 +000022 "os"
Colin Cross6a745c62015-06-16 16:38:10 -070023 "path/filepath"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070024 "reflect"
Chris Wailesb2703ad2021-07-30 13:25:42 -070025 "regexp"
Colin Cross5e6cfbe2017-11-03 15:20:35 -070026 "sort"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070027 "strings"
28
29 "github.com/google/blueprint"
Colin Cross0e446152021-05-03 13:35:32 -070030 "github.com/google/blueprint/bootstrap"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070031 "github.com/google/blueprint/pathtools"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
Colin Cross988414c2020-01-11 01:11:46 +000034var absSrcDir string
35
Dan Willemsen34cc69e2015-09-23 15:26:20 -070036// PathContext is the subset of a (Module|Singleton)Context required by the
37// Path methods.
38type PathContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -080039 Config() Config
Dan Willemsen7b310ee2015-12-18 15:11:17 -080040 AddNinjaFileDeps(deps ...string)
Colin Cross3f40fa42015-01-30 17:27:36 -080041}
42
Colin Cross7f19f372016-11-01 11:10:25 -070043type PathGlobContext interface {
Colin Cross662d6142022-11-03 20:38:01 -070044 PathContext
Colin Cross7f19f372016-11-01 11:10:25 -070045 GlobWithDeps(globPattern string, excludes []string) ([]string, error)
46}
47
Colin Crossaabf6792017-11-29 00:27:14 -080048var _ PathContext = SingletonContext(nil)
49var _ PathContext = ModuleContext(nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -070050
Ulya Trafimovich8640ab92020-05-11 18:06:15 +010051// "Null" path context is a minimal path context for a given config.
52type NullPathContext struct {
53 config Config
54}
55
56func (NullPathContext) AddNinjaFileDeps(...string) {}
57func (ctx NullPathContext) Config() Config { return ctx.config }
58
Liz Kammera830f3a2020-11-10 10:50:34 -080059// EarlyModulePathContext is a subset of EarlyModuleContext methods required by the
60// Path methods. These path methods can be called before any mutators have run.
61type EarlyModulePathContext interface {
Liz Kammera830f3a2020-11-10 10:50:34 -080062 PathGlobContext
63
64 ModuleDir() string
65 ModuleErrorf(fmt string, args ...interface{})
Cole Fausta963b942024-04-11 17:43:00 -070066 OtherModulePropertyErrorf(module Module, property, fmt string, args ...interface{})
Liz Kammera830f3a2020-11-10 10:50:34 -080067}
68
69var _ EarlyModulePathContext = ModuleContext(nil)
70
71// Glob globs files and directories matching globPattern relative to ModuleDir(),
72// paths in the excludes parameter will be omitted.
73func Glob(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths {
74 ret, err := ctx.GlobWithDeps(globPattern, excludes)
75 if err != nil {
76 ctx.ModuleErrorf("glob: %s", err.Error())
77 }
78 return pathsForModuleSrcFromFullPath(ctx, ret, true)
79}
80
81// GlobFiles globs *only* files (not directories) matching globPattern relative to ModuleDir().
82// Paths in the excludes parameter will be omitted.
83func GlobFiles(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths {
84 ret, err := ctx.GlobWithDeps(globPattern, excludes)
85 if err != nil {
86 ctx.ModuleErrorf("glob: %s", err.Error())
87 }
88 return pathsForModuleSrcFromFullPath(ctx, ret, false)
89}
90
91// ModuleWithDepsPathContext is a subset of *ModuleContext methods required by
92// the Path methods that rely on module dependencies having been resolved.
93type ModuleWithDepsPathContext interface {
94 EarlyModulePathContext
Paul Duffin40131a32021-07-09 17:10:35 +010095 VisitDirectDepsBlueprint(visit func(blueprint.Module))
96 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
Liz Kammera830f3a2020-11-10 10:50:34 -080097}
98
99// ModuleMissingDepsPathContext is a subset of *ModuleContext methods required by
100// the Path methods that rely on module dependencies having been resolved and ability to report
101// missing dependency errors.
102type ModuleMissingDepsPathContext interface {
103 ModuleWithDepsPathContext
104 AddMissingDependencies(missingDeps []string)
105}
106
Dan Willemsen00269f22017-07-06 16:59:48 -0700107type ModuleInstallPathContext interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700108 BaseModuleContext
Dan Willemsen00269f22017-07-06 16:59:48 -0700109
110 InstallInData() bool
Jaewoong Jung0949f312019-09-11 10:25:18 -0700111 InstallInTestcases() bool
Dan Willemsen00269f22017-07-06 16:59:48 -0700112 InstallInSanitizerDir() bool
Yifan Hong1b3348d2020-01-21 15:53:22 -0800113 InstallInRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700114 InstallInVendorRamdisk() bool
Inseob Kim08758f02021-04-08 21:13:22 +0900115 InstallInDebugRamdisk() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900116 InstallInRecovery() bool
Colin Cross90ba5f42019-10-02 11:10:58 -0700117 InstallInRoot() bool
Colin Crossea30d852023-11-29 16:00:16 -0800118 InstallInOdm() bool
119 InstallInProduct() bool
120 InstallInVendor() bool
Jiyong Park87788b52020-09-01 12:37:45 +0900121 InstallForceOS() (*OsType, *ArchType)
Dan Willemsen00269f22017-07-06 16:59:48 -0700122}
123
124var _ ModuleInstallPathContext = ModuleContext(nil)
125
Cole Faust11edf552023-10-13 11:32:14 -0700126type baseModuleContextToModuleInstallPathContext struct {
127 BaseModuleContext
128}
129
130func (ctx *baseModuleContextToModuleInstallPathContext) InstallInData() bool {
131 return ctx.Module().InstallInData()
132}
133
134func (ctx *baseModuleContextToModuleInstallPathContext) InstallInTestcases() bool {
135 return ctx.Module().InstallInTestcases()
136}
137
138func (ctx *baseModuleContextToModuleInstallPathContext) InstallInSanitizerDir() bool {
139 return ctx.Module().InstallInSanitizerDir()
140}
141
142func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRamdisk() bool {
143 return ctx.Module().InstallInRamdisk()
144}
145
146func (ctx *baseModuleContextToModuleInstallPathContext) InstallInVendorRamdisk() bool {
147 return ctx.Module().InstallInVendorRamdisk()
148}
149
150func (ctx *baseModuleContextToModuleInstallPathContext) InstallInDebugRamdisk() bool {
151 return ctx.Module().InstallInDebugRamdisk()
152}
153
154func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRecovery() bool {
155 return ctx.Module().InstallInRecovery()
156}
157
158func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRoot() bool {
159 return ctx.Module().InstallInRoot()
160}
161
Colin Crossea30d852023-11-29 16:00:16 -0800162func (ctx *baseModuleContextToModuleInstallPathContext) InstallInOdm() bool {
163 return ctx.Module().InstallInOdm()
164}
165
166func (ctx *baseModuleContextToModuleInstallPathContext) InstallInProduct() bool {
167 return ctx.Module().InstallInProduct()
168}
169
170func (ctx *baseModuleContextToModuleInstallPathContext) InstallInVendor() bool {
171 return ctx.Module().InstallInVendor()
172}
173
Cole Faust11edf552023-10-13 11:32:14 -0700174func (ctx *baseModuleContextToModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
175 return ctx.Module().InstallForceOS()
176}
177
178var _ ModuleInstallPathContext = (*baseModuleContextToModuleInstallPathContext)(nil)
179
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700180// errorfContext is the interface containing the Errorf method matching the
181// Errorf method in blueprint.SingletonContext.
182type errorfContext interface {
183 Errorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -0800184}
185
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700186var _ errorfContext = blueprint.SingletonContext(nil)
187
Spandan Das59a4a2b2024-01-09 21:35:56 +0000188// ModuleErrorfContext is the interface containing the ModuleErrorf method matching
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700189// the ModuleErrorf method in blueprint.ModuleContext.
Spandan Das59a4a2b2024-01-09 21:35:56 +0000190type ModuleErrorfContext interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700191 ModuleErrorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -0800192}
193
Spandan Das59a4a2b2024-01-09 21:35:56 +0000194var _ ModuleErrorfContext = blueprint.ModuleContext(nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700195
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700196// reportPathError will register an error with the attached context. It
197// attempts ctx.ModuleErrorf for a better error message first, then falls
198// back to ctx.Errorf.
Colin Cross1ccfcc32018-02-22 13:54:26 -0800199func reportPathError(ctx PathContext, err error) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100200 ReportPathErrorf(ctx, "%s", err.Error())
Colin Cross1ccfcc32018-02-22 13:54:26 -0800201}
202
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100203// ReportPathErrorf will register an error with the attached context. It
Colin Cross1ccfcc32018-02-22 13:54:26 -0800204// attempts ctx.ModuleErrorf for a better error message first, then falls
205// back to ctx.Errorf.
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100206func ReportPathErrorf(ctx PathContext, format string, args ...interface{}) {
Spandan Das59a4a2b2024-01-09 21:35:56 +0000207 if mctx, ok := ctx.(ModuleErrorfContext); ok {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700208 mctx.ModuleErrorf(format, args...)
209 } else if ectx, ok := ctx.(errorfContext); ok {
210 ectx.Errorf(format, args...)
211 } else {
212 panic(fmt.Sprintf(format, args...))
Colin Crossf2298272015-05-12 11:36:53 -0700213 }
214}
215
Colin Cross5e708052019-08-06 13:59:50 -0700216func pathContextName(ctx PathContext, module blueprint.Module) string {
217 if x, ok := ctx.(interface{ ModuleName(blueprint.Module) string }); ok {
218 return x.ModuleName(module)
219 } else if x, ok := ctx.(interface{ OtherModuleName(blueprint.Module) string }); ok {
220 return x.OtherModuleName(module)
221 }
222 return "unknown"
223}
224
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700225type Path interface {
226 // Returns the path in string form
227 String() string
228
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700229 // Ext returns the extension of the last element of the path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700230 Ext() string
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700231
232 // Base returns the last element of the path
233 Base() string
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800234
235 // Rel returns the portion of the path relative to the directory it was created from. For
236 // example, Rel on a PathsForModuleSrc would return the path relative to the module source
Colin Cross0db55682017-12-05 15:36:55 -0800237 // directory, and OutputPath.Join("foo").Rel() would return "foo".
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800238 Rel() string
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000239
240 // RelativeToTop returns a new path relative to the top, it is provided solely for use in tests.
241 //
242 // It is guaranteed to always return the same type as it is called on, e.g. if called on an
243 // InstallPath then the returned value can be converted to an InstallPath.
244 //
245 // A standard build has the following structure:
246 // ../top/
247 // out/ - make install files go here.
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200248 // out/soong - this is the soongOutDir passed to NewTestConfig()
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000249 // ... - the source files
250 //
251 // This function converts a path so that it appears relative to the ../top/ directory, i.e.
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200252 // * Make install paths, which have the pattern "soongOutDir/../<path>" are converted into the top
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000253 // relative path "out/<path>"
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200254 // * Soong install paths and other writable paths, which have the pattern "soongOutDir/<path>" are
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000255 // converted into the top relative path "out/soong/<path>".
256 // * Source paths are already relative to the top.
257 // * Phony paths are not relative to anything.
258 // * toolDepPath have an absolute but known value in so don't need making relative to anything in
259 // order to test.
260 RelativeToTop() Path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700261}
262
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000263const (
264 OutDir = "out"
265 OutSoongDir = OutDir + "/soong"
266)
267
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700268// WritablePath is a type of path that can be used as an output for build rules.
269type WritablePath interface {
270 Path
271
Paul Duffin9b478b02019-12-10 13:41:51 +0000272 // return the path to the build directory.
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200273 getSoongOutDir() string
Paul Duffin9b478b02019-12-10 13:41:51 +0000274
Jeff Gaston734e3802017-04-10 15:47:24 -0700275 // the writablePath method doesn't directly do anything,
276 // but it allows a struct to distinguish between whether or not it implements the WritablePath interface
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700277 writablePath()
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +0100278
279 ReplaceExtension(ctx PathContext, ext string) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700280}
281
282type genPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800283 genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath
yangbill6d032dd2024-04-18 03:05:49 +0000284 genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700285}
286type objPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800287 objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700288}
289type resPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800290 resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700291}
292
293// GenPathWithExt derives a new file path in ctx's generated sources directory
294// from the current path, but with the new extension.
Liz Kammera830f3a2020-11-10 10:50:34 -0800295func GenPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700296 if path, ok := p.(genPathProvider); ok {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700297 return path.genPathWithExt(ctx, subdir, ext)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700298 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100299 ReportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700300 return PathForModuleGen(ctx)
301}
302
yangbill6d032dd2024-04-18 03:05:49 +0000303// GenPathWithExtAndTrimExt derives a new file path in ctx's generated sources directory
304// from the current path, but with the new extension and trim the suffix.
305func GenPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir string, p Path, ext string, trimExt string) ModuleGenPath {
306 if path, ok := p.(genPathProvider); ok {
307 return path.genPathWithExtAndTrimExt(ctx, subdir, ext, trimExt)
308 }
309 ReportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
310 return PathForModuleGen(ctx)
311}
312
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700313// ObjPathWithExt derives a new file path in ctx's object directory from the
314// current path, but with the new extension.
Liz Kammera830f3a2020-11-10 10:50:34 -0800315func ObjPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700316 if path, ok := p.(objPathProvider); ok {
317 return path.objPathWithExt(ctx, subdir, ext)
318 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100319 ReportPathErrorf(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700320 return PathForModuleObj(ctx)
321}
322
323// ResPathWithName derives a new path in ctx's output resource directory, using
324// the current path to create the directory name, and the `name` argument for
325// the filename.
Liz Kammera830f3a2020-11-10 10:50:34 -0800326func ResPathWithName(ctx ModuleOutPathContext, p Path, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700327 if path, ok := p.(resPathProvider); ok {
328 return path.resPathWithName(ctx, name)
329 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100330 ReportPathErrorf(ctx, "Tried to create res file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700331 return PathForModuleRes(ctx)
332}
333
334// OptionalPath is a container that may or may not contain a valid Path.
335type OptionalPath struct {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100336 path Path // nil if invalid.
337 invalidReason string // Not applicable if path != nil. "" if the reason is unknown.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700338}
339
340// OptionalPathForPath returns an OptionalPath containing the path.
341func OptionalPathForPath(path Path) OptionalPath {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100342 return OptionalPath{path: path}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700343}
344
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100345// InvalidOptionalPath returns an OptionalPath that is invalid with the given reason.
346func InvalidOptionalPath(reason string) OptionalPath {
347
348 return OptionalPath{invalidReason: reason}
349}
350
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700351// Valid returns whether there is a valid path
352func (p OptionalPath) Valid() bool {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100353 return p.path != nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700354}
355
356// Path returns the Path embedded in this OptionalPath. You must be sure that
357// there is a valid path, since this method will panic if there is not.
358func (p OptionalPath) Path() Path {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100359 if p.path == nil {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100360 msg := "Requesting an invalid path"
361 if p.invalidReason != "" {
362 msg += ": " + p.invalidReason
363 }
364 panic(msg)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700365 }
366 return p.path
367}
368
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100369// InvalidReason returns the reason that the optional path is invalid, or "" if it is valid.
370func (p OptionalPath) InvalidReason() string {
371 if p.path != nil {
372 return ""
373 }
374 if p.invalidReason == "" {
375 return "unknown"
376 }
377 return p.invalidReason
378}
379
Paul Duffinef081852021-05-13 11:11:15 +0100380// AsPaths converts the OptionalPath into Paths.
381//
382// It returns nil if this is not valid, or a single length slice containing the Path embedded in
383// this OptionalPath.
384func (p OptionalPath) AsPaths() Paths {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100385 if p.path == nil {
Paul Duffinef081852021-05-13 11:11:15 +0100386 return nil
387 }
388 return Paths{p.path}
389}
390
Paul Duffinafdd4062021-03-30 19:44:07 +0100391// RelativeToTop returns an OptionalPath with the path that was embedded having been replaced by the
392// result of calling Path.RelativeToTop on it.
393func (p OptionalPath) RelativeToTop() OptionalPath {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100394 if p.path == nil {
Paul Duffina5b81352021-03-28 23:57:19 +0100395 return p
396 }
397 p.path = p.path.RelativeToTop()
398 return p
399}
400
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700401// String returns the string version of the Path, or "" if it isn't valid.
402func (p OptionalPath) String() string {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100403 if p.path != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700404 return p.path.String()
405 } else {
406 return ""
Colin Crossf2298272015-05-12 11:36:53 -0700407 }
408}
Colin Cross6e18ca42015-07-14 18:55:36 -0700409
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700410// Paths is a slice of Path objects, with helpers to operate on the collection.
411type Paths []Path
412
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000413// RelativeToTop creates a new Paths containing the result of calling Path.RelativeToTop on each
414// item in this slice.
415func (p Paths) RelativeToTop() Paths {
416 ensureTestOnly()
417 if p == nil {
418 return p
419 }
420 ret := make(Paths, len(p))
421 for i, path := range p {
422 ret[i] = path.RelativeToTop()
423 }
424 return ret
425}
426
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000427func (paths Paths) containsPath(path Path) bool {
428 for _, p := range paths {
429 if p == path {
430 return true
431 }
432 }
433 return false
434}
435
Liz Kammer7aa52882021-02-11 09:16:14 -0500436// PathsForSource returns Paths rooted from SrcDir, *not* rooted from the module's local source
437// directory
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700438func PathsForSource(ctx PathContext, paths []string) Paths {
439 ret := make(Paths, len(paths))
440 for i, path := range paths {
441 ret[i] = PathForSource(ctx, path)
442 }
443 return ret
444}
445
Liz Kammer7aa52882021-02-11 09:16:14 -0500446// ExistentPathsForSources returns a list of Paths rooted from SrcDir, *not* rooted from the
447// module's local source directory, that are found in the tree. If any are not found, they are
448// omitted from the list, and dependencies are added so that we're re-run when they are added.
Colin Cross662d6142022-11-03 20:38:01 -0700449func ExistentPathsForSources(ctx PathGlobContext, paths []string) Paths {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800450 ret := make(Paths, 0, len(paths))
451 for _, path := range paths {
Colin Cross32f38982018-02-22 11:47:25 -0800452 p := ExistentPathForSource(ctx, path)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800453 if p.Valid() {
454 ret = append(ret, p.Path())
455 }
456 }
457 return ret
458}
459
Liz Kammer620dea62021-04-14 17:36:10 -0400460// PathsForModuleSrc returns a Paths{} containing the resolved references in paths:
Colin Crossd079e0b2022-08-16 10:27:33 -0700461// - filepath, relative to local module directory, resolves as a filepath relative to the local
462// source directory
463// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
464// source directory.
465// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700466// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
467// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700468//
Liz Kammer620dea62021-04-14 17:36:10 -0400469// Properties passed as the paths argument must have been annotated with struct tag
Colin Cross41955e82019-05-29 14:40:35 -0700470// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000471// pathdeps mutator.
Liz Kammer620dea62021-04-14 17:36:10 -0400472// If a requested module is not found as a dependency:
Colin Crossd079e0b2022-08-16 10:27:33 -0700473// - if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having
Liz Kammer620dea62021-04-14 17:36:10 -0400474// missing dependencies
Colin Crossd079e0b2022-08-16 10:27:33 -0700475// - otherwise, a ModuleError is thrown.
Liz Kammera830f3a2020-11-10 10:50:34 -0800476func PathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string) Paths {
Colin Cross8a497952019-03-05 22:25:09 -0800477 return PathsForModuleSrcExcludes(ctx, paths, nil)
478}
479
Liz Kammer619be462022-01-28 15:13:39 -0500480type SourceInput struct {
481 Context ModuleMissingDepsPathContext
482 Paths []string
483 ExcludePaths []string
484 IncludeDirs bool
485}
486
Liz Kammer620dea62021-04-14 17:36:10 -0400487// PathsForModuleSrcExcludes returns a Paths{} containing the resolved references in paths, minus
488// those listed in excludes. Elements of paths and excludes are resolved as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700489// - filepath, relative to local module directory, resolves as a filepath relative to the local
490// source directory
491// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
492// source directory. Not valid in excludes.
493// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700494// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
495// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700496//
Liz Kammer620dea62021-04-14 17:36:10 -0400497// excluding the items (similarly resolved
498// Properties passed as the paths argument must have been annotated with struct tag
499// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000500// pathdeps mutator.
Liz Kammer620dea62021-04-14 17:36:10 -0400501// If a requested module is not found as a dependency:
Colin Crossd079e0b2022-08-16 10:27:33 -0700502// - if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having
Liz Kammer620dea62021-04-14 17:36:10 -0400503// missing dependencies
Colin Crossd079e0b2022-08-16 10:27:33 -0700504// - otherwise, a ModuleError is thrown.
Liz Kammera830f3a2020-11-10 10:50:34 -0800505func PathsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) Paths {
Liz Kammer619be462022-01-28 15:13:39 -0500506 return PathsRelativeToModuleSourceDir(SourceInput{
507 Context: ctx,
508 Paths: paths,
509 ExcludePaths: excludes,
510 IncludeDirs: true,
511 })
512}
513
514func PathsRelativeToModuleSourceDir(input SourceInput) Paths {
515 ret, missingDeps := PathsAndMissingDepsRelativeToModuleSourceDir(input)
516 if input.Context.Config().AllowMissingDependencies() {
517 input.Context.AddMissingDependencies(missingDeps)
Colin Crossba71a3f2019-03-18 12:12:48 -0700518 } else {
519 for _, m := range missingDeps {
Liz Kammer619be462022-01-28 15:13:39 -0500520 input.Context.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m)
Colin Crossba71a3f2019-03-18 12:12:48 -0700521 }
522 }
523 return ret
524}
525
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000526// OutputPaths is a slice of OutputPath objects, with helpers to operate on the collection.
527type OutputPaths []OutputPath
528
529// Paths returns the OutputPaths as a Paths
530func (p OutputPaths) Paths() Paths {
531 if p == nil {
532 return nil
533 }
534 ret := make(Paths, len(p))
535 for i, path := range p {
536 ret[i] = path
537 }
538 return ret
539}
540
541// Strings returns the string forms of the writable paths.
542func (p OutputPaths) Strings() []string {
543 if p == nil {
544 return nil
545 }
546 ret := make([]string, len(p))
547 for i, path := range p {
548 ret[i] = path.String()
549 }
550 return ret
551}
552
Colin Crossa44551f2021-10-25 15:36:21 -0700553// PathForGoBinary returns the path to the installed location of a bootstrap_go_binary module.
554func PathForGoBinary(ctx PathContext, goBinary bootstrap.GoBinaryTool) Path {
Cole Faust3b703f32023-10-16 13:30:51 -0700555 goBinaryInstallDir := pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "bin")
Colin Crossa44551f2021-10-25 15:36:21 -0700556 rel := Rel(ctx, goBinaryInstallDir.String(), goBinary.InstallPath())
557 return goBinaryInstallDir.Join(ctx, rel)
558}
559
Liz Kammera830f3a2020-11-10 10:50:34 -0800560// Expands Paths to a SourceFileProducer or OutputFileProducer module dependency referenced via ":name" or ":name{.tag}" syntax.
561// If the dependency is not found, a missingErrorDependency is returned.
562// If the module dependency is not a SourceFileProducer or OutputFileProducer, appropriate errors will be returned.
563func getPathsFromModuleDep(ctx ModuleWithDepsPathContext, path, moduleName, tag string) (Paths, error) {
Paul Duffind5cf92e2021-07-09 17:38:55 +0100564 module := GetModuleFromPathDep(ctx, moduleName, tag)
Liz Kammera830f3a2020-11-10 10:50:34 -0800565 if module == nil {
566 return nil, missingDependencyError{[]string{moduleName}}
567 }
Cole Fausta963b942024-04-11 17:43:00 -0700568 if aModule, ok := module.(Module); ok && !aModule.Enabled(ctx) {
Colin Crossfa65cee2021-03-22 17:05:59 -0700569 return nil, missingDependencyError{[]string{moduleName}}
570 }
mrziwange6c85812024-05-22 14:36:09 -0700571 if goBinary, ok := module.(bootstrap.GoBinaryTool); ok && tag == "" {
Colin Crossa44551f2021-10-25 15:36:21 -0700572 goBinaryPath := PathForGoBinary(ctx, goBinary)
573 return Paths{goBinaryPath}, nil
mrziwange6c85812024-05-22 14:36:09 -0700574 }
575 outputFiles, err := outputFilesForModule(ctx, module, tag)
576 if outputFiles != nil && err == nil {
577 return outputFiles, nil
Liz Kammera830f3a2020-11-10 10:50:34 -0800578 } else {
mrziwange6c85812024-05-22 14:36:09 -0700579 return nil, err
Liz Kammera830f3a2020-11-10 10:50:34 -0800580 }
581}
582
Paul Duffind5cf92e2021-07-09 17:38:55 +0100583// GetModuleFromPathDep will return the module that was added as a dependency automatically for
584// properties tagged with `android:"path"` or manually using ExtractSourceDeps or
585// ExtractSourcesDeps.
586//
587// The moduleName and tag supplied to this should be the values returned from SrcIsModuleWithTag.
588// Or, if no tag is expected then the moduleName should be the value returned by SrcIsModule and
589// the tag must be "".
590//
591// If tag is "" then the returned module will be the dependency that was added for ":moduleName".
592// Otherwise, it is the dependency that was added for ":moduleName{tag}".
Paul Duffind5cf92e2021-07-09 17:38:55 +0100593func GetModuleFromPathDep(ctx ModuleWithDepsPathContext, moduleName, tag string) blueprint.Module {
Paul Duffin40131a32021-07-09 17:10:35 +0100594 var found blueprint.Module
595 // The sourceOrOutputDepTag uniquely identifies the module dependency as it contains both the
596 // module name and the tag. Dependencies added automatically for properties tagged with
597 // `android:"path"` are deduped so are guaranteed to be unique. It is possible for duplicate
598 // dependencies to be added manually using ExtractSourcesDeps or ExtractSourceDeps but even then
599 // it will always be the case that the dependencies will be identical, i.e. the same tag and same
600 // moduleName referring to the same dependency module.
601 //
602 // It does not matter whether the moduleName is a fully qualified name or if the module
603 // dependency is a prebuilt module. All that matters is the same information is supplied to
604 // create the tag here as was supplied to create the tag when the dependency was added so that
605 // this finds the matching dependency module.
606 expectedTag := sourceOrOutputDepTag(moduleName, tag)
607 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
608 depTag := ctx.OtherModuleDependencyTag(module)
609 if depTag == expectedTag {
610 found = module
611 }
612 })
613 return found
Paul Duffind5cf92e2021-07-09 17:38:55 +0100614}
615
Liz Kammer620dea62021-04-14 17:36:10 -0400616// PathsAndMissingDepsForModuleSrcExcludes returns a Paths{} containing the resolved references in
617// paths, minus those listed in excludes. Elements of paths and excludes are resolved as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700618// - filepath, relative to local module directory, resolves as a filepath relative to the local
619// source directory
620// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
621// source directory. Not valid in excludes.
622// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700623// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
624// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700625//
Liz Kammer620dea62021-04-14 17:36:10 -0400626// and a list of the module names of missing module dependencies are returned as the second return.
627// Properties passed as the paths argument must have been annotated with struct tag
Colin Cross41955e82019-05-29 14:40:35 -0700628// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000629// pathdeps mutator.
Liz Kammer619be462022-01-28 15:13:39 -0500630func PathsAndMissingDepsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) (Paths, []string) {
631 return PathsAndMissingDepsRelativeToModuleSourceDir(SourceInput{
632 Context: ctx,
633 Paths: paths,
634 ExcludePaths: excludes,
635 IncludeDirs: true,
636 })
637}
638
639func PathsAndMissingDepsRelativeToModuleSourceDir(input SourceInput) (Paths, []string) {
640 prefix := pathForModuleSrc(input.Context).String()
Colin Cross8a497952019-03-05 22:25:09 -0800641
642 var expandedExcludes []string
Liz Kammer619be462022-01-28 15:13:39 -0500643 if input.ExcludePaths != nil {
644 expandedExcludes = make([]string, 0, len(input.ExcludePaths))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700645 }
Colin Cross8a497952019-03-05 22:25:09 -0800646
Colin Crossba71a3f2019-03-18 12:12:48 -0700647 var missingExcludeDeps []string
Liz Kammer619be462022-01-28 15:13:39 -0500648 for _, e := range input.ExcludePaths {
Colin Cross41955e82019-05-29 14:40:35 -0700649 if m, t := SrcIsModuleWithTag(e); m != "" {
Liz Kammer619be462022-01-28 15:13:39 -0500650 modulePaths, err := getPathsFromModuleDep(input.Context, e, m, t)
Liz Kammera830f3a2020-11-10 10:50:34 -0800651 if m, ok := err.(missingDependencyError); ok {
652 missingExcludeDeps = append(missingExcludeDeps, m.missingDeps...)
653 } else if err != nil {
Liz Kammer619be462022-01-28 15:13:39 -0500654 reportPathError(input.Context, err)
Colin Cross8a497952019-03-05 22:25:09 -0800655 } else {
Liz Kammera830f3a2020-11-10 10:50:34 -0800656 expandedExcludes = append(expandedExcludes, modulePaths.Strings()...)
Colin Cross8a497952019-03-05 22:25:09 -0800657 }
658 } else {
659 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
660 }
661 }
662
Liz Kammer619be462022-01-28 15:13:39 -0500663 if input.Paths == nil {
Colin Crossba71a3f2019-03-18 12:12:48 -0700664 return nil, missingExcludeDeps
Colin Cross8a497952019-03-05 22:25:09 -0800665 }
666
Colin Crossba71a3f2019-03-18 12:12:48 -0700667 var missingDeps []string
668
Liz Kammer619be462022-01-28 15:13:39 -0500669 expandedSrcFiles := make(Paths, 0, len(input.Paths))
670 for _, s := range input.Paths {
671 srcFiles, err := expandOneSrcPath(sourcePathInput{
672 context: input.Context,
673 path: s,
674 expandedExcludes: expandedExcludes,
675 includeDirs: input.IncludeDirs,
676 })
Colin Cross8a497952019-03-05 22:25:09 -0800677 if depErr, ok := err.(missingDependencyError); ok {
Colin Crossba71a3f2019-03-18 12:12:48 -0700678 missingDeps = append(missingDeps, depErr.missingDeps...)
Colin Cross8a497952019-03-05 22:25:09 -0800679 } else if err != nil {
Liz Kammer619be462022-01-28 15:13:39 -0500680 reportPathError(input.Context, err)
Colin Cross8a497952019-03-05 22:25:09 -0800681 }
682 expandedSrcFiles = append(expandedSrcFiles, srcFiles...)
683 }
Colin Crossba71a3f2019-03-18 12:12:48 -0700684
Jihoon Kang0e3a5352024-04-12 00:45:50 +0000685 // TODO: b/334169722 - Replace with an error instead of implicitly removing duplicates.
686 return FirstUniquePaths(expandedSrcFiles), append(missingDeps, missingExcludeDeps...)
Colin Cross8a497952019-03-05 22:25:09 -0800687}
688
689type missingDependencyError struct {
690 missingDeps []string
691}
692
693func (e missingDependencyError) Error() string {
694 return "missing dependencies: " + strings.Join(e.missingDeps, ", ")
695}
696
Liz Kammer619be462022-01-28 15:13:39 -0500697type sourcePathInput struct {
698 context ModuleWithDepsPathContext
699 path string
700 expandedExcludes []string
701 includeDirs bool
702}
703
Liz Kammera830f3a2020-11-10 10:50:34 -0800704// Expands one path string to Paths rooted from the module's local source
705// directory, excluding those listed in the expandedExcludes.
706// Expands globs, references to SourceFileProducer or OutputFileProducer modules using the ":name" and ":name{.tag}" syntax.
Liz Kammer619be462022-01-28 15:13:39 -0500707func expandOneSrcPath(input sourcePathInput) (Paths, error) {
Jooyung Han7607dd32020-07-05 10:23:14 +0900708 excludePaths := func(paths Paths) Paths {
Liz Kammer619be462022-01-28 15:13:39 -0500709 if len(input.expandedExcludes) == 0 {
Jooyung Han7607dd32020-07-05 10:23:14 +0900710 return paths
711 }
712 remainder := make(Paths, 0, len(paths))
713 for _, p := range paths {
Liz Kammer619be462022-01-28 15:13:39 -0500714 if !InList(p.String(), input.expandedExcludes) {
Jooyung Han7607dd32020-07-05 10:23:14 +0900715 remainder = append(remainder, p)
716 }
717 }
718 return remainder
719 }
Liz Kammer619be462022-01-28 15:13:39 -0500720 if m, t := SrcIsModuleWithTag(input.path); m != "" {
721 modulePaths, err := getPathsFromModuleDep(input.context, input.path, m, t)
Liz Kammera830f3a2020-11-10 10:50:34 -0800722 if err != nil {
723 return nil, err
Colin Cross8a497952019-03-05 22:25:09 -0800724 } else {
Liz Kammera830f3a2020-11-10 10:50:34 -0800725 return excludePaths(modulePaths), nil
Colin Cross8a497952019-03-05 22:25:09 -0800726 }
Colin Cross8a497952019-03-05 22:25:09 -0800727 } else {
Liz Kammer619be462022-01-28 15:13:39 -0500728 p := pathForModuleSrc(input.context, input.path)
729 if pathtools.IsGlob(input.path) {
730 paths := GlobFiles(input.context, p.String(), input.expandedExcludes)
731 return PathsWithModuleSrcSubDir(input.context, paths, ""), nil
732 } else {
733 if exists, _, err := input.context.Config().fs.Exists(p.String()); err != nil {
734 ReportPathErrorf(input.context, "%s: %s", p, err.Error())
735 } else if !exists && !input.context.Config().TestAllowNonExistentPaths {
736 ReportPathErrorf(input.context, "module source path %q does not exist", p)
737 } else if !input.includeDirs {
738 if isDir, err := input.context.Config().fs.IsDir(p.String()); exists && err != nil {
739 ReportPathErrorf(input.context, "%s: %s", p, err.Error())
740 } else if isDir {
741 ReportPathErrorf(input.context, "module source path %q is a directory", p)
742 }
743 }
Colin Cross8a497952019-03-05 22:25:09 -0800744
Liz Kammer619be462022-01-28 15:13:39 -0500745 if InList(p.String(), input.expandedExcludes) {
746 return nil, nil
747 }
748 return Paths{p}, nil
Colin Cross8a497952019-03-05 22:25:09 -0800749 }
Colin Cross8a497952019-03-05 22:25:09 -0800750 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700751}
752
753// pathsForModuleSrcFromFullPath returns Paths rooted from the module's local
754// source directory, but strip the local source directory from the beginning of
Dan Willemsen540a78c2018-02-26 21:50:08 -0800755// each string. If incDirs is false, strip paths with a trailing '/' from the list.
Colin Crossfe4bc362018-09-12 10:02:13 -0700756// It intended for use in globs that only list files that exist, so it allows '$' in
757// filenames.
Liz Kammera830f3a2020-11-10 10:50:34 -0800758func pathsForModuleSrcFromFullPath(ctx EarlyModulePathContext, paths []string, incDirs bool) Paths {
Lukacs T. Berkif7e36d82021-08-16 17:05:09 +0200759 prefix := ctx.ModuleDir() + "/"
Colin Cross0f37af02017-09-27 17:42:05 -0700760 if prefix == "./" {
761 prefix = ""
762 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700763 ret := make(Paths, 0, len(paths))
764 for _, p := range paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -0800765 if !incDirs && strings.HasSuffix(p, "/") {
766 continue
767 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700768 path := filepath.Clean(p)
769 if !strings.HasPrefix(path, prefix) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100770 ReportPathErrorf(ctx, "Path %q is not in module source directory %q", p, prefix)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700771 continue
772 }
Colin Crosse3924e12018-08-15 20:18:53 -0700773
Colin Crossfe4bc362018-09-12 10:02:13 -0700774 srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):])
Colin Crosse3924e12018-08-15 20:18:53 -0700775 if err != nil {
776 reportPathError(ctx, err)
777 continue
778 }
779
Colin Cross07e51612019-03-05 12:46:40 -0800780 srcPath.basePath.rel = srcPath.path
Colin Crosse3924e12018-08-15 20:18:53 -0700781
Colin Cross07e51612019-03-05 12:46:40 -0800782 ret = append(ret, srcPath)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700783 }
784 return ret
785}
786
Liz Kammera830f3a2020-11-10 10:50:34 -0800787// PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's local source
788// directory. If input is nil, use the default if it exists. If input is empty, returns nil.
789func PathsWithOptionalDefaultForModuleSrc(ctx ModuleMissingDepsPathContext, input []string, def string) Paths {
Colin Cross0ddae7f2019-02-07 15:30:01 -0800790 if input != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700791 return PathsForModuleSrc(ctx, input)
792 }
793 // Use Glob so that if the default doesn't exist, a dependency is added so that when it
794 // is created, we're run again.
Lukacs T. Berkif7e36d82021-08-16 17:05:09 +0200795 path := filepath.Join(ctx.ModuleDir(), def)
Liz Kammera830f3a2020-11-10 10:50:34 -0800796 return Glob(ctx, path, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700797}
798
799// Strings returns the Paths in string form
800func (p Paths) Strings() []string {
801 if p == nil {
802 return nil
803 }
804 ret := make([]string, len(p))
805 for i, path := range p {
806 ret[i] = path.String()
807 }
808 return ret
809}
810
Colin Crossc0efd1d2020-07-03 11:56:24 -0700811func CopyOfPaths(paths Paths) Paths {
812 return append(Paths(nil), paths...)
813}
814
Colin Crossb6715442017-10-24 11:13:31 -0700815// FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It
816// modifies the Paths slice contents in place, and returns a subslice of the original slice.
Dan Willemsenfe92c962017-08-29 12:28:37 -0700817func FirstUniquePaths(list Paths) Paths {
Colin Cross27027c72020-02-28 15:34:17 -0800818 // 128 was chosen based on BenchmarkFirstUniquePaths results.
819 if len(list) > 128 {
820 return firstUniquePathsMap(list)
821 }
822 return firstUniquePathsList(list)
823}
824
Colin Crossc0efd1d2020-07-03 11:56:24 -0700825// SortedUniquePaths returns all unique elements of a Paths in sorted order. It modifies the
826// Paths slice contents in place, and returns a subslice of the original slice.
Jiyong Park33c77362020-05-29 22:00:16 +0900827func SortedUniquePaths(list Paths) Paths {
828 unique := FirstUniquePaths(list)
829 sort.Slice(unique, func(i, j int) bool {
830 return unique[i].String() < unique[j].String()
831 })
832 return unique
833}
834
Colin Cross27027c72020-02-28 15:34:17 -0800835func firstUniquePathsList(list Paths) Paths {
Dan Willemsenfe92c962017-08-29 12:28:37 -0700836 k := 0
837outer:
838 for i := 0; i < len(list); i++ {
839 for j := 0; j < k; j++ {
840 if list[i] == list[j] {
841 continue outer
842 }
843 }
844 list[k] = list[i]
845 k++
846 }
847 return list[:k]
848}
849
Colin Cross27027c72020-02-28 15:34:17 -0800850func firstUniquePathsMap(list Paths) Paths {
851 k := 0
852 seen := make(map[Path]bool, len(list))
853 for i := 0; i < len(list); i++ {
854 if seen[list[i]] {
855 continue
856 }
857 seen[list[i]] = true
858 list[k] = list[i]
859 k++
860 }
861 return list[:k]
862}
863
Colin Cross5d583952020-11-24 16:21:24 -0800864// FirstUniqueInstallPaths returns all unique elements of an InstallPaths, keeping the first copy of each. It
865// modifies the InstallPaths slice contents in place, and returns a subslice of the original slice.
866func FirstUniqueInstallPaths(list InstallPaths) InstallPaths {
867 // 128 was chosen based on BenchmarkFirstUniquePaths results.
868 if len(list) > 128 {
869 return firstUniqueInstallPathsMap(list)
870 }
871 return firstUniqueInstallPathsList(list)
872}
873
874func firstUniqueInstallPathsList(list InstallPaths) InstallPaths {
875 k := 0
876outer:
877 for i := 0; i < len(list); i++ {
878 for j := 0; j < k; j++ {
879 if list[i] == list[j] {
880 continue outer
881 }
882 }
883 list[k] = list[i]
884 k++
885 }
886 return list[:k]
887}
888
889func firstUniqueInstallPathsMap(list InstallPaths) InstallPaths {
890 k := 0
891 seen := make(map[InstallPath]bool, len(list))
892 for i := 0; i < len(list); i++ {
893 if seen[list[i]] {
894 continue
895 }
896 seen[list[i]] = true
897 list[k] = list[i]
898 k++
899 }
900 return list[:k]
901}
902
Colin Crossb6715442017-10-24 11:13:31 -0700903// LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It
904// modifies the Paths slice contents in place, and returns a subslice of the original slice.
905func LastUniquePaths(list Paths) Paths {
906 totalSkip := 0
907 for i := len(list) - 1; i >= totalSkip; i-- {
908 skip := 0
909 for j := i - 1; j >= totalSkip; j-- {
910 if list[i] == list[j] {
911 skip++
912 } else {
913 list[j+skip] = list[j]
914 }
915 }
916 totalSkip += skip
917 }
918 return list[totalSkip:]
919}
920
Colin Crossa140bb02018-04-17 10:52:26 -0700921// ReversePaths returns a copy of a Paths in reverse order.
922func ReversePaths(list Paths) Paths {
923 if list == nil {
924 return nil
925 }
926 ret := make(Paths, len(list))
927 for i := range list {
928 ret[i] = list[len(list)-1-i]
929 }
930 return ret
931}
932
Jeff Gaston294356f2017-09-27 17:05:30 -0700933func indexPathList(s Path, list []Path) int {
934 for i, l := range list {
935 if l == s {
936 return i
937 }
938 }
939
940 return -1
941}
942
943func inPathList(p Path, list []Path) bool {
944 return indexPathList(p, list) != -1
945}
946
947func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) {
Paul Duffin57b9e1d2019-12-13 00:03:35 +0000948 return FilterPathListPredicate(list, func(p Path) bool { return inPathList(p, filter) })
949}
950
951func FilterPathListPredicate(list []Path, predicate func(Path) bool) (remainder []Path, filtered []Path) {
Jeff Gaston294356f2017-09-27 17:05:30 -0700952 for _, l := range list {
Paul Duffin57b9e1d2019-12-13 00:03:35 +0000953 if predicate(l) {
Jeff Gaston294356f2017-09-27 17:05:30 -0700954 filtered = append(filtered, l)
955 } else {
956 remainder = append(remainder, l)
957 }
958 }
959
960 return
961}
962
Colin Cross93e85952017-08-15 13:34:18 -0700963// HasExt returns true of any of the paths have extension ext, otherwise false
964func (p Paths) HasExt(ext string) bool {
965 for _, path := range p {
966 if path.Ext() == ext {
967 return true
968 }
969 }
970
971 return false
972}
973
974// FilterByExt returns the subset of the paths that have extension ext
975func (p Paths) FilterByExt(ext string) Paths {
976 ret := make(Paths, 0, len(p))
977 for _, path := range p {
978 if path.Ext() == ext {
979 ret = append(ret, path)
980 }
981 }
982 return ret
983}
984
985// FilterOutByExt returns the subset of the paths that do not have extension ext
986func (p Paths) FilterOutByExt(ext string) Paths {
987 ret := make(Paths, 0, len(p))
988 for _, path := range p {
989 if path.Ext() != ext {
990 ret = append(ret, path)
991 }
992 }
993 return ret
994}
995
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700996// DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory
997// (including subdirectories) are in a contiguous subslice of the list, and can be found in
998// O(log(N)) time using a binary search on the directory prefix.
999type DirectorySortedPaths Paths
1000
1001func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths {
1002 ret := append(DirectorySortedPaths(nil), paths...)
1003 sort.Slice(ret, func(i, j int) bool {
1004 return ret[i].String() < ret[j].String()
1005 })
1006 return ret
1007}
1008
1009// PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries
1010// that are in the specified directory and its subdirectories.
1011func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths {
1012 prefix := filepath.Clean(dir) + "/"
1013 start := sort.Search(len(p), func(i int) bool {
1014 return prefix < p[i].String()
1015 })
1016
1017 ret := p[start:]
1018
1019 end := sort.Search(len(ret), func(i int) bool {
1020 return !strings.HasPrefix(ret[i].String(), prefix)
1021 })
1022
1023 ret = ret[:end]
1024
1025 return Paths(ret)
1026}
1027
Alex Humesky29e3bbe2020-11-20 21:30:13 -05001028// WritablePaths is a slice of WritablePath, used for multiple outputs.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001029type WritablePaths []WritablePath
1030
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001031// RelativeToTop creates a new WritablePaths containing the result of calling Path.RelativeToTop on
1032// each item in this slice.
1033func (p WritablePaths) RelativeToTop() WritablePaths {
1034 ensureTestOnly()
1035 if p == nil {
1036 return p
1037 }
1038 ret := make(WritablePaths, len(p))
1039 for i, path := range p {
1040 ret[i] = path.RelativeToTop().(WritablePath)
1041 }
1042 return ret
1043}
1044
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001045// Strings returns the string forms of the writable paths.
1046func (p WritablePaths) Strings() []string {
1047 if p == nil {
1048 return nil
1049 }
1050 ret := make([]string, len(p))
1051 for i, path := range p {
1052 ret[i] = path.String()
1053 }
1054 return ret
1055}
1056
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001057// Paths returns the WritablePaths as a Paths
1058func (p WritablePaths) Paths() Paths {
1059 if p == nil {
1060 return nil
1061 }
1062 ret := make(Paths, len(p))
1063 for i, path := range p {
1064 ret[i] = path
1065 }
1066 return ret
1067}
1068
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001069type basePath struct {
Paul Duffin74abc5d2021-03-24 09:24:59 +00001070 path string
1071 rel string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001072}
1073
Yu Liufa297642024-06-11 00:13:02 +00001074func (p basePath) GobEncode() ([]byte, error) {
1075 w := new(bytes.Buffer)
1076 encoder := gob.NewEncoder(w)
1077 err := errors.Join(encoder.Encode(p.path), encoder.Encode(p.rel))
1078 if err != nil {
1079 return nil, err
1080 }
1081
1082 return w.Bytes(), nil
1083}
1084
1085func (p *basePath) GobDecode(data []byte) error {
1086 r := bytes.NewBuffer(data)
1087 decoder := gob.NewDecoder(r)
1088 err := errors.Join(decoder.Decode(&p.path), decoder.Decode(&p.rel))
1089 if err != nil {
1090 return err
1091 }
1092
1093 return nil
1094}
1095
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001096func (p basePath) Ext() string {
1097 return filepath.Ext(p.path)
1098}
1099
Colin Cross4f6fc9c2016-10-26 10:05:25 -07001100func (p basePath) Base() string {
1101 return filepath.Base(p.path)
1102}
1103
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001104func (p basePath) Rel() string {
1105 if p.rel != "" {
1106 return p.rel
1107 }
1108 return p.path
1109}
1110
Colin Cross0875c522017-11-28 17:34:01 -08001111func (p basePath) String() string {
1112 return p.path
1113}
1114
Colin Cross0db55682017-12-05 15:36:55 -08001115func (p basePath) withRel(rel string) basePath {
1116 p.path = filepath.Join(p.path, rel)
1117 p.rel = rel
1118 return p
1119}
1120
Cole Faustbc65a3f2023-08-01 16:38:55 +00001121func (p basePath) RelativeToTop() Path {
1122 ensureTestOnly()
1123 return p
1124}
1125
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001126// SourcePath is a Path representing a file path rooted from SrcDir
1127type SourcePath struct {
1128 basePath
1129}
1130
1131var _ Path = SourcePath{}
1132
Colin Cross0db55682017-12-05 15:36:55 -08001133func (p SourcePath) withRel(rel string) SourcePath {
1134 p.basePath = p.basePath.withRel(rel)
1135 return p
1136}
1137
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001138// safePathForSource is for paths that we expect are safe -- only for use by go
1139// code that is embedding ninja variables in paths
Colin Crossfe4bc362018-09-12 10:02:13 -07001140func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
1141 p, err := validateSafePath(pathComponents...)
Cole Faust483d1f72023-01-09 14:35:27 -08001142 ret := SourcePath{basePath{p, ""}}
Colin Crossfe4bc362018-09-12 10:02:13 -07001143 if err != nil {
1144 return ret, err
1145 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001146
Colin Cross7b3dcc32019-01-24 13:14:39 -08001147 // absolute path already checked by validateSafePath
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001148 // special-case api surface gen files for now
1149 if strings.HasPrefix(ret.String(), ctx.Config().soongOutDir) && !strings.Contains(ret.String(), ctx.Config().soongOutDir+"/.export") {
Mikhail Naganovab1f5182019-02-08 13:17:55 -08001150 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Cross6e18ca42015-07-14 18:55:36 -07001151 }
1152
Colin Crossfe4bc362018-09-12 10:02:13 -07001153 return ret, err
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001154}
1155
Colin Cross192e97a2018-02-22 14:21:02 -08001156// pathForSource creates a SourcePath from pathComponents, but does not check that it exists.
1157func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
Colin Crossc48c1432018-02-23 07:09:01 +00001158 p, err := validatePath(pathComponents...)
Cole Faust483d1f72023-01-09 14:35:27 -08001159 ret := SourcePath{basePath{p, ""}}
Colin Cross94a32102018-02-22 14:21:02 -08001160 if err != nil {
Colin Cross192e97a2018-02-22 14:21:02 -08001161 return ret, err
Colin Cross94a32102018-02-22 14:21:02 -08001162 }
1163
Colin Cross7b3dcc32019-01-24 13:14:39 -08001164 // absolute path already checked by validatePath
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001165 // special-case for now
1166 if strings.HasPrefix(ret.String(), ctx.Config().soongOutDir) && !strings.Contains(ret.String(), ctx.Config().soongOutDir+"/.export") {
Mikhail Naganovab1f5182019-02-08 13:17:55 -08001167 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Crossc48c1432018-02-23 07:09:01 +00001168 }
1169
Colin Cross192e97a2018-02-22 14:21:02 -08001170 return ret, nil
1171}
1172
1173// existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the
1174// path does not exist.
Colin Cross662d6142022-11-03 20:38:01 -07001175func existsWithDependencies(ctx PathGlobContext, path SourcePath) (exists bool, err error) {
Colin Cross192e97a2018-02-22 14:21:02 -08001176 var files []string
1177
Colin Cross662d6142022-11-03 20:38:01 -07001178 // Use glob to produce proper dependencies, even though we only want
1179 // a single file.
1180 files, err = ctx.GlobWithDeps(path.String(), nil)
Colin Cross192e97a2018-02-22 14:21:02 -08001181
1182 if err != nil {
1183 return false, fmt.Errorf("glob: %s", err.Error())
1184 }
1185
1186 return len(files) > 0, nil
1187}
1188
1189// PathForSource joins the provided path components and validates that the result
1190// neither escapes the source dir nor is in the out dir.
1191// On error, it will return a usable, but invalid SourcePath, and report a ModuleError.
1192func PathForSource(ctx PathContext, pathComponents ...string) SourcePath {
1193 path, err := pathForSource(ctx, pathComponents...)
1194 if err != nil {
1195 reportPathError(ctx, err)
1196 }
1197
Colin Crosse3924e12018-08-15 20:18:53 -07001198 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001199 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -07001200 }
1201
Liz Kammera830f3a2020-11-10 10:50:34 -08001202 if modCtx, ok := ctx.(ModuleMissingDepsPathContext); ok && ctx.Config().AllowMissingDependencies() {
Colin Cross662d6142022-11-03 20:38:01 -07001203 exists, err := existsWithDependencies(modCtx, path)
Colin Cross192e97a2018-02-22 14:21:02 -08001204 if err != nil {
1205 reportPathError(ctx, err)
1206 }
1207 if !exists {
1208 modCtx.AddMissingDependencies([]string{path.String()})
1209 }
Colin Cross988414c2020-01-11 01:11:46 +00001210 } else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001211 ReportPathErrorf(ctx, "%s: %s", path, err.Error())
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001212 } else if !exists && !ctx.Config().TestAllowNonExistentPaths {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001213 ReportPathErrorf(ctx, "source path %q does not exist", path)
Colin Cross192e97a2018-02-22 14:21:02 -08001214 }
1215 return path
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001216}
1217
Cole Faustbc65a3f2023-08-01 16:38:55 +00001218// PathForArbitraryOutput creates a path for the given components. Unlike PathForOutput,
1219// the path is relative to the root of the output folder, not the out/soong folder.
1220func PathForArbitraryOutput(ctx PathContext, pathComponents ...string) Path {
1221 p, err := validatePath(pathComponents...)
1222 if err != nil {
1223 reportPathError(ctx, err)
1224 }
1225 return basePath{path: filepath.Join(ctx.Config().OutDir(), p)}
1226}
1227
Spandan Dasc6c10fa2022-10-21 21:52:13 +00001228// MaybeExistentPathForSource joins the provided path components and validates that the result
1229// neither escapes the source dir nor is in the out dir.
1230// It does not validate whether the path exists.
1231func MaybeExistentPathForSource(ctx PathContext, pathComponents ...string) SourcePath {
1232 path, err := pathForSource(ctx, pathComponents...)
1233 if err != nil {
1234 reportPathError(ctx, err)
1235 }
1236
1237 if pathtools.IsGlob(path.String()) {
1238 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
1239 }
1240 return path
1241}
1242
Liz Kammer7aa52882021-02-11 09:16:14 -05001243// ExistentPathForSource returns an OptionalPath with the SourcePath, rooted from SrcDir, *not*
1244// rooted from the module's local source directory, if the path exists, or an empty OptionalPath if
1245// it doesn't exist. Dependencies are added so that the ninja file will be regenerated if the state
1246// of the path changes.
Colin Cross662d6142022-11-03 20:38:01 -07001247func ExistentPathForSource(ctx PathGlobContext, pathComponents ...string) OptionalPath {
Colin Cross192e97a2018-02-22 14:21:02 -08001248 path, err := pathForSource(ctx, pathComponents...)
Colin Cross1ccfcc32018-02-22 13:54:26 -08001249 if err != nil {
1250 reportPathError(ctx, err)
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001251 // No need to put the error message into the returned path since it has been reported already.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001252 return OptionalPath{}
1253 }
Colin Crossc48c1432018-02-23 07:09:01 +00001254
Colin Crosse3924e12018-08-15 20:18:53 -07001255 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001256 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -07001257 return OptionalPath{}
1258 }
1259
Colin Cross192e97a2018-02-22 14:21:02 -08001260 exists, err := existsWithDependencies(ctx, path)
Colin Crossc48c1432018-02-23 07:09:01 +00001261 if err != nil {
1262 reportPathError(ctx, err)
1263 return OptionalPath{}
1264 }
Colin Cross192e97a2018-02-22 14:21:02 -08001265 if !exists {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001266 return InvalidOptionalPath(path.String() + " does not exist")
Colin Crossc48c1432018-02-23 07:09:01 +00001267 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001268 return OptionalPathForPath(path)
1269}
1270
1271func (p SourcePath) String() string {
Cole Faust483d1f72023-01-09 14:35:27 -08001272 if p.path == "" {
1273 return "."
1274 }
1275 return p.path
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001276}
1277
1278// Join creates a new SourcePath with paths... joined with the current path. The
1279// provided paths... may not use '..' to escape from the current path.
1280func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001281 path, err := validatePath(paths...)
1282 if err != nil {
1283 reportPathError(ctx, err)
1284 }
Colin Cross0db55682017-12-05 15:36:55 -08001285 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001286}
1287
Colin Cross2fafa3e2019-03-05 12:39:51 -08001288// join is like Join but does less path validation.
1289func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath {
1290 path, err := validateSafePath(paths...)
1291 if err != nil {
1292 reportPathError(ctx, err)
1293 }
1294 return p.withRel(path)
1295}
1296
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001297// OverlayPath returns the overlay for `path' if it exists. This assumes that the
1298// SourcePath is the path to a resource overlay directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001299func (p SourcePath) OverlayPath(ctx ModuleMissingDepsPathContext, path Path) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001300 var relDir string
Colin Cross07e51612019-03-05 12:46:40 -08001301 if srcPath, ok := path.(SourcePath); ok {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001302 relDir = srcPath.path
1303 } else {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001304 ReportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001305 // No need to put the error message into the returned path since it has been reported already.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001306 return OptionalPath{}
1307 }
Cole Faust483d1f72023-01-09 14:35:27 -08001308 dir := filepath.Join(p.path, relDir)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001309 // Use Glob so that we are run again if the directory is added.
Colin Cross7f19f372016-11-01 11:10:25 -07001310 if pathtools.IsGlob(dir) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001311 ReportPathErrorf(ctx, "Path may not contain a glob: %s", dir)
Dan Willemsen7b310ee2015-12-18 15:11:17 -08001312 }
Colin Cross461b4452018-02-23 09:22:42 -08001313 paths, err := ctx.GlobWithDeps(dir, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001314 if err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001315 ReportPathErrorf(ctx, "glob: %s", err.Error())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001316 return OptionalPath{}
1317 }
1318 if len(paths) == 0 {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001319 return InvalidOptionalPath(dir + " does not exist")
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001320 }
Cole Faust483d1f72023-01-09 14:35:27 -08001321 return OptionalPathForPath(PathForSource(ctx, paths[0]))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001322}
1323
Colin Cross70dda7e2019-10-01 22:05:35 -07001324// OutputPath is a Path representing an intermediates file path rooted from the build directory
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001325type OutputPath struct {
1326 basePath
Paul Duffind65c58b2021-03-24 09:22:07 +00001327
Lukacs T. Berkib078ade2021-08-31 10:42:08 +02001328 // The soong build directory, i.e. Config.SoongOutDir()
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001329 soongOutDir string
Paul Duffind65c58b2021-03-24 09:22:07 +00001330
Colin Crossd63c9a72020-01-29 16:52:50 -08001331 fullPath string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001332}
1333
Yu Liufa297642024-06-11 00:13:02 +00001334func (p OutputPath) GobEncode() ([]byte, error) {
1335 w := new(bytes.Buffer)
1336 encoder := gob.NewEncoder(w)
1337 err := errors.Join(encoder.Encode(p.basePath), encoder.Encode(p.soongOutDir), encoder.Encode(p.fullPath))
1338 if err != nil {
1339 return nil, err
1340 }
1341
1342 return w.Bytes(), nil
1343}
1344
1345func (p *OutputPath) GobDecode(data []byte) error {
1346 r := bytes.NewBuffer(data)
1347 decoder := gob.NewDecoder(r)
1348 err := errors.Join(decoder.Decode(&p.basePath), decoder.Decode(&p.soongOutDir), decoder.Decode(&p.fullPath))
1349 if err != nil {
1350 return err
1351 }
1352
1353 return nil
1354}
1355
Colin Cross702e0f82017-10-18 17:27:54 -07001356func (p OutputPath) withRel(rel string) OutputPath {
Colin Cross0db55682017-12-05 15:36:55 -08001357 p.basePath = p.basePath.withRel(rel)
Colin Crossd63c9a72020-01-29 16:52:50 -08001358 p.fullPath = filepath.Join(p.fullPath, rel)
Colin Cross702e0f82017-10-18 17:27:54 -07001359 return p
1360}
1361
Colin Cross3063b782018-08-15 11:19:12 -07001362func (p OutputPath) WithoutRel() OutputPath {
1363 p.basePath.rel = filepath.Base(p.basePath.path)
1364 return p
1365}
1366
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001367func (p OutputPath) getSoongOutDir() string {
1368 return p.soongOutDir
Paul Duffin9b478b02019-12-10 13:41:51 +00001369}
1370
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001371func (p OutputPath) RelativeToTop() Path {
1372 return p.outputPathRelativeToTop()
1373}
1374
1375func (p OutputPath) outputPathRelativeToTop() OutputPath {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001376 p.fullPath = StringPathRelativeToTop(p.soongOutDir, p.fullPath)
1377 p.soongOutDir = OutSoongDir
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001378 return p
1379}
1380
Paul Duffin0267d492021-02-02 10:05:52 +00001381func (p OutputPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
1382 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1383}
1384
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001385var _ Path = OutputPath{}
Paul Duffin9b478b02019-12-10 13:41:51 +00001386var _ WritablePath = OutputPath{}
Paul Duffin0267d492021-02-02 10:05:52 +00001387var _ objPathProvider = OutputPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001388
Chris Parsons8f232a22020-06-23 17:37:05 -04001389// toolDepPath is a Path representing a dependency of the build tool.
1390type toolDepPath struct {
1391 basePath
1392}
1393
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001394func (t toolDepPath) RelativeToTop() Path {
1395 ensureTestOnly()
1396 return t
1397}
1398
Chris Parsons8f232a22020-06-23 17:37:05 -04001399var _ Path = toolDepPath{}
1400
1401// pathForBuildToolDep returns a toolDepPath representing the given path string.
1402// There is no validation for the path, as it is "trusted": It may fail
1403// normal validation checks. For example, it may be an absolute path.
1404// Only use this function to construct paths for dependencies of the build
1405// tool invocation.
1406func pathForBuildToolDep(ctx PathContext, path string) toolDepPath {
Paul Duffin74abc5d2021-03-24 09:24:59 +00001407 return toolDepPath{basePath{path, ""}}
Chris Parsons8f232a22020-06-23 17:37:05 -04001408}
1409
Jeff Gaston734e3802017-04-10 15:47:24 -07001410// PathForOutput joins the provided paths and returns an OutputPath that is
1411// validated to not escape the build dir.
1412// On error, it will return a usable, but invalid OutputPath, and report a ModuleError.
1413func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001414 path, err := validatePath(pathComponents...)
1415 if err != nil {
1416 reportPathError(ctx, err)
1417 }
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001418 fullPath := filepath.Join(ctx.Config().soongOutDir, path)
Colin Crossd63c9a72020-01-29 16:52:50 -08001419 path = fullPath[len(fullPath)-len(path):]
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001420 return OutputPath{basePath{path, ""}, ctx.Config().soongOutDir, fullPath}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001421}
1422
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001423// PathsForOutput returns Paths rooted from soongOutDir
Colin Cross40e33732019-02-15 11:08:35 -08001424func PathsForOutput(ctx PathContext, paths []string) WritablePaths {
1425 ret := make(WritablePaths, len(paths))
1426 for i, path := range paths {
1427 ret[i] = PathForOutput(ctx, path)
1428 }
1429 return ret
1430}
1431
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001432func (p OutputPath) writablePath() {}
1433
1434func (p OutputPath) String() string {
Colin Crossd63c9a72020-01-29 16:52:50 -08001435 return p.fullPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001436}
1437
1438// Join creates a new OutputPath with paths... joined with the current path. The
1439// provided paths... may not use '..' to escape from the current path.
1440func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001441 path, err := validatePath(paths...)
1442 if err != nil {
1443 reportPathError(ctx, err)
1444 }
Colin Cross0db55682017-12-05 15:36:55 -08001445 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001446}
1447
Colin Cross8854a5a2019-02-11 14:14:16 -08001448// ReplaceExtension creates a new OutputPath with the extension replaced with ext.
1449func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1450 if strings.Contains(ext, "/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001451 ReportPathErrorf(ctx, "extension %q cannot contain /", ext)
Colin Cross8854a5a2019-02-11 14:14:16 -08001452 }
1453 ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext))
Colin Cross2cdd5df2019-02-25 10:25:24 -08001454 ret.rel = pathtools.ReplaceExtension(p.rel, ext)
Colin Cross8854a5a2019-02-11 14:14:16 -08001455 return ret
1456}
1457
Colin Cross40e33732019-02-15 11:08:35 -08001458// InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths.
1459func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath {
1460 path, err := validatePath(paths...)
1461 if err != nil {
1462 reportPathError(ctx, err)
1463 }
1464
1465 ret := PathForOutput(ctx, filepath.Dir(p.path), path)
Colin Cross2cdd5df2019-02-25 10:25:24 -08001466 ret.rel = filepath.Join(filepath.Dir(p.rel), path)
Colin Cross40e33732019-02-15 11:08:35 -08001467 return ret
1468}
1469
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001470// PathForIntermediates returns an OutputPath representing the top-level
1471// intermediates directory.
1472func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001473 path, err := validatePath(paths...)
1474 if err != nil {
1475 reportPathError(ctx, err)
1476 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001477 return PathForOutput(ctx, ".intermediates", path)
1478}
1479
Colin Cross07e51612019-03-05 12:46:40 -08001480var _ genPathProvider = SourcePath{}
1481var _ objPathProvider = SourcePath{}
1482var _ resPathProvider = SourcePath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001483
Colin Cross07e51612019-03-05 12:46:40 -08001484// PathForModuleSrc returns a Path representing the paths... under the
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001485// module's local source directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001486func PathForModuleSrc(ctx ModuleMissingDepsPathContext, pathComponents ...string) Path {
Paul Duffin407501b2021-07-09 16:56:35 +01001487 // Just join the components textually just to make sure that it does not corrupt a fully qualified
1488 // module reference, e.g. if the pathComponents is "://other:foo" then using filepath.Join() or
1489 // validatePath() will corrupt it, e.g. replace "//" with "/". If the path is not a module
1490 // reference then it will be validated by expandOneSrcPath anyway when it calls expandOneSrcPath.
1491 p := strings.Join(pathComponents, string(filepath.Separator))
Liz Kammer619be462022-01-28 15:13:39 -05001492 paths, err := expandOneSrcPath(sourcePathInput{context: ctx, path: p, includeDirs: true})
Colin Cross8a497952019-03-05 22:25:09 -08001493 if err != nil {
1494 if depErr, ok := err.(missingDependencyError); ok {
1495 if ctx.Config().AllowMissingDependencies() {
1496 ctx.AddMissingDependencies(depErr.missingDeps)
1497 } else {
1498 ctx.ModuleErrorf(`%s, is the property annotated with android:"path"?`, depErr.Error())
1499 }
1500 } else {
1501 reportPathError(ctx, err)
1502 }
1503 return nil
1504 } else if len(paths) == 0 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001505 ReportPathErrorf(ctx, "%q produced no files, expected exactly one", p)
Colin Cross8a497952019-03-05 22:25:09 -08001506 return nil
1507 } else if len(paths) > 1 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001508 ReportPathErrorf(ctx, "%q produced %d files, expected exactly one", p, len(paths))
Colin Cross8a497952019-03-05 22:25:09 -08001509 }
1510 return paths[0]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001511}
1512
Liz Kammera830f3a2020-11-10 10:50:34 -08001513func pathForModuleSrc(ctx EarlyModulePathContext, paths ...string) SourcePath {
Colin Cross07e51612019-03-05 12:46:40 -08001514 p, err := validatePath(paths...)
1515 if err != nil {
1516 reportPathError(ctx, err)
1517 }
1518
1519 path, err := pathForSource(ctx, ctx.ModuleDir(), p)
1520 if err != nil {
1521 reportPathError(ctx, err)
1522 }
1523
1524 path.basePath.rel = p
1525
1526 return path
1527}
1528
Colin Cross2fafa3e2019-03-05 12:39:51 -08001529// PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path
1530// will return the path relative to subDir in the module's source directory. If any input paths are not located
1531// inside subDir then a path error will be reported.
Liz Kammera830f3a2020-11-10 10:50:34 -08001532func PathsWithModuleSrcSubDir(ctx EarlyModulePathContext, paths Paths, subDir string) Paths {
Colin Cross2fafa3e2019-03-05 12:39:51 -08001533 paths = append(Paths(nil), paths...)
Colin Cross07e51612019-03-05 12:46:40 -08001534 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001535 for i, path := range paths {
1536 rel := Rel(ctx, subDirFullPath.String(), path.String())
1537 paths[i] = subDirFullPath.join(ctx, rel)
1538 }
1539 return paths
1540}
1541
1542// PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the
1543// module's source directory. If the input path is not located inside subDir then a path error will be reported.
Liz Kammera830f3a2020-11-10 10:50:34 -08001544func PathWithModuleSrcSubDir(ctx EarlyModulePathContext, path Path, subDir string) Path {
Colin Cross07e51612019-03-05 12:46:40 -08001545 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001546 rel := Rel(ctx, subDirFullPath.String(), path.String())
1547 return subDirFullPath.Join(ctx, rel)
1548}
1549
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001550// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
1551// valid path if p is non-nil.
Liz Kammera830f3a2020-11-10 10:50:34 -08001552func OptionalPathForModuleSrc(ctx ModuleMissingDepsPathContext, p *string) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001553 if p == nil {
1554 return OptionalPath{}
1555 }
1556 return OptionalPathForPath(PathForModuleSrc(ctx, *p))
1557}
1558
Liz Kammera830f3a2020-11-10 10:50:34 -08001559func (p SourcePath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001560 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001561}
1562
yangbill6d032dd2024-04-18 03:05:49 +00001563func (p SourcePath) genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath {
1564 // If Trim_extension being set, force append Output_extension without replace original extension.
1565 if trimExt != "" {
1566 if ext != "" {
1567 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt)+"."+ext)
1568 }
1569 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt))
1570 }
1571 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1572}
1573
Liz Kammera830f3a2020-11-10 10:50:34 -08001574func (p SourcePath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001575 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001576}
1577
Liz Kammera830f3a2020-11-10 10:50:34 -08001578func (p SourcePath) resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001579 // TODO: Use full directory if the new ctx is not the current ctx?
1580 return PathForModuleRes(ctx, p.path, name)
1581}
1582
1583// ModuleOutPath is a Path representing a module's output directory.
1584type ModuleOutPath struct {
1585 OutputPath
1586}
1587
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001588func (p ModuleOutPath) RelativeToTop() Path {
1589 p.OutputPath = p.outputPathRelativeToTop()
1590 return p
1591}
1592
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001593var _ Path = ModuleOutPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001594var _ WritablePath = ModuleOutPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001595
Liz Kammera830f3a2020-11-10 10:50:34 -08001596func (p ModuleOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01001597 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1598}
1599
Liz Kammera830f3a2020-11-10 10:50:34 -08001600// ModuleOutPathContext Subset of ModuleContext functions necessary for output path methods.
1601type ModuleOutPathContext interface {
1602 PathContext
1603
1604 ModuleName() string
1605 ModuleDir() string
1606 ModuleSubDir() string
1607}
1608
1609func pathForModuleOut(ctx ModuleOutPathContext) OutputPath {
Inseob Kimb7e9f5f2024-06-25 17:39:52 +09001610 return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir())
Colin Cross702e0f82017-10-18 17:27:54 -07001611}
1612
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001613// PathForModuleOut returns a Path representing the paths... under the module's
1614// output directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001615func PathForModuleOut(ctx ModuleOutPathContext, paths ...string) ModuleOutPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001616 p, err := validatePath(paths...)
1617 if err != nil {
1618 reportPathError(ctx, err)
1619 }
Colin Cross702e0f82017-10-18 17:27:54 -07001620 return ModuleOutPath{
Liz Kammera830f3a2020-11-10 10:50:34 -08001621 OutputPath: pathForModuleOut(ctx).withRel(p),
Colin Cross702e0f82017-10-18 17:27:54 -07001622 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001623}
1624
1625// ModuleGenPath is a Path representing the 'gen' directory in a module's output
1626// directory. Mainly used for generated sources.
1627type ModuleGenPath struct {
1628 ModuleOutPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001629}
1630
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001631func (p ModuleGenPath) RelativeToTop() Path {
1632 p.OutputPath = p.outputPathRelativeToTop()
1633 return p
1634}
1635
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001636var _ Path = ModuleGenPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001637var _ WritablePath = ModuleGenPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001638var _ genPathProvider = ModuleGenPath{}
1639var _ objPathProvider = ModuleGenPath{}
1640
1641// PathForModuleGen returns a Path representing the paths... under the module's
1642// `gen' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001643func PathForModuleGen(ctx ModuleOutPathContext, paths ...string) ModuleGenPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001644 p, err := validatePath(paths...)
1645 if err != nil {
1646 reportPathError(ctx, err)
1647 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001648 return ModuleGenPath{
Colin Cross702e0f82017-10-18 17:27:54 -07001649 ModuleOutPath: ModuleOutPath{
Liz Kammera830f3a2020-11-10 10:50:34 -08001650 OutputPath: pathForModuleOut(ctx).withRel("gen").withRel(p),
Colin Cross702e0f82017-10-18 17:27:54 -07001651 },
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001652 }
1653}
1654
Liz Kammera830f3a2020-11-10 10:50:34 -08001655func (p ModuleGenPath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001656 // TODO: make a different path for local vs remote generated files?
Dan Willemsen21ec4902016-11-02 20:43:13 -07001657 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001658}
1659
yangbill6d032dd2024-04-18 03:05:49 +00001660func (p ModuleGenPath) genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath {
1661 // If Trim_extension being set, force append Output_extension without replace original extension.
1662 if trimExt != "" {
1663 if ext != "" {
1664 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt)+"."+ext)
1665 }
1666 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt))
1667 }
1668 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1669}
1670
Liz Kammera830f3a2020-11-10 10:50:34 -08001671func (p ModuleGenPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001672 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1673}
1674
1675// ModuleObjPath is a Path representing the 'obj' directory in a module's output
1676// directory. Used for compiled objects.
1677type ModuleObjPath struct {
1678 ModuleOutPath
1679}
1680
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001681func (p ModuleObjPath) RelativeToTop() Path {
1682 p.OutputPath = p.outputPathRelativeToTop()
1683 return p
1684}
1685
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001686var _ Path = ModuleObjPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001687var _ WritablePath = ModuleObjPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001688
1689// PathForModuleObj returns a Path representing the paths... under the module's
1690// 'obj' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001691func PathForModuleObj(ctx ModuleOutPathContext, pathComponents ...string) ModuleObjPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001692 p, err := validatePath(pathComponents...)
1693 if err != nil {
1694 reportPathError(ctx, err)
1695 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001696 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
1697}
1698
1699// ModuleResPath is a a Path representing the 'res' directory in a module's
1700// output directory.
1701type ModuleResPath struct {
1702 ModuleOutPath
1703}
1704
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001705func (p ModuleResPath) RelativeToTop() Path {
1706 p.OutputPath = p.outputPathRelativeToTop()
1707 return p
1708}
1709
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001710var _ Path = ModuleResPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001711var _ WritablePath = ModuleResPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001712
1713// PathForModuleRes returns a Path representing the paths... under the module's
1714// 'res' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001715func PathForModuleRes(ctx ModuleOutPathContext, pathComponents ...string) ModuleResPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001716 p, err := validatePath(pathComponents...)
1717 if err != nil {
1718 reportPathError(ctx, err)
1719 }
1720
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001721 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
1722}
1723
Colin Cross70dda7e2019-10-01 22:05:35 -07001724// InstallPath is a Path representing a installed file path rooted from the build directory
1725type InstallPath struct {
1726 basePath
Colin Crossff6c33d2019-10-02 16:01:35 -07001727
Lukacs T. Berkib078ade2021-08-31 10:42:08 +02001728 // The soong build directory, i.e. Config.SoongOutDir()
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001729 soongOutDir string
Paul Duffind65c58b2021-03-24 09:22:07 +00001730
Jiyong Park957bcd92020-10-20 18:23:33 +09001731 // partitionDir is the part of the InstallPath that is automatically determined according to the context.
1732 // For example, it is host/<os>-<arch> for host modules, and target/product/<device>/<partition> for device modules.
1733 partitionDir string
1734
Colin Crossb1692a32021-10-25 15:39:01 -07001735 partition string
1736
Jiyong Park957bcd92020-10-20 18:23:33 +09001737 // makePath indicates whether this path is for Soong (false) or Make (true).
1738 makePath bool
Colin Crossc0e42d52024-02-01 16:42:36 -08001739
1740 fullPath string
Colin Cross70dda7e2019-10-01 22:05:35 -07001741}
1742
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001743// Will panic if called from outside a test environment.
1744func ensureTestOnly() {
Martin Stjernholm32312eb2021-03-27 18:54:49 +00001745 if PrefixInList(os.Args, "-test.") {
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001746 return
1747 }
Martin Stjernholm32312eb2021-03-27 18:54:49 +00001748 panic(fmt.Errorf("Not in test. Command line:\n %s", strings.Join(os.Args, "\n ")))
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001749}
1750
1751func (p InstallPath) RelativeToTop() Path {
1752 ensureTestOnly()
Colin Crossc0e42d52024-02-01 16:42:36 -08001753 if p.makePath {
1754 p.soongOutDir = OutDir
1755 } else {
1756 p.soongOutDir = OutSoongDir
1757 }
1758 p.fullPath = filepath.Join(p.soongOutDir, p.path)
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001759 return p
1760}
1761
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001762func (p InstallPath) getSoongOutDir() string {
1763 return p.soongOutDir
Paul Duffin9b478b02019-12-10 13:41:51 +00001764}
1765
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01001766func (p InstallPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1767 panic("Not implemented")
1768}
1769
Paul Duffin9b478b02019-12-10 13:41:51 +00001770var _ Path = InstallPath{}
1771var _ WritablePath = InstallPath{}
1772
Colin Cross70dda7e2019-10-01 22:05:35 -07001773func (p InstallPath) writablePath() {}
1774
1775func (p InstallPath) String() string {
Colin Crossc0e42d52024-02-01 16:42:36 -08001776 return p.fullPath
Jiyong Park957bcd92020-10-20 18:23:33 +09001777}
1778
1779// PartitionDir returns the path to the partition where the install path is rooted at. It is
1780// out/soong/target/product/<device>/<partition> for device modules, and out/soong/host/<os>-<arch> for host modules.
1781// The ./soong is dropped if the install path is for Make.
1782func (p InstallPath) PartitionDir() string {
1783 if p.makePath {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001784 return filepath.Join(p.soongOutDir, "../", p.partitionDir)
Jiyong Park957bcd92020-10-20 18:23:33 +09001785 } else {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001786 return filepath.Join(p.soongOutDir, p.partitionDir)
Jiyong Park957bcd92020-10-20 18:23:33 +09001787 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001788}
1789
Jihoon Kangf78a8902022-09-01 22:47:07 +00001790func (p InstallPath) Partition() string {
1791 return p.partition
1792}
1793
Colin Cross70dda7e2019-10-01 22:05:35 -07001794// Join creates a new InstallPath with paths... joined with the current path. The
1795// provided paths... may not use '..' to escape from the current path.
1796func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath {
1797 path, err := validatePath(paths...)
1798 if err != nil {
1799 reportPathError(ctx, err)
1800 }
1801 return p.withRel(path)
1802}
1803
1804func (p InstallPath) withRel(rel string) InstallPath {
1805 p.basePath = p.basePath.withRel(rel)
Colin Crossc0e42d52024-02-01 16:42:36 -08001806 p.fullPath = filepath.Join(p.fullPath, rel)
Colin Cross70dda7e2019-10-01 22:05:35 -07001807 return p
1808}
1809
Colin Crossc68db4b2021-11-11 18:59:15 -08001810// Deprecated: ToMakePath is a noop, PathForModuleInstall always returns Make paths when building
1811// embedded in Make.
Colin Crossff6c33d2019-10-02 16:01:35 -07001812func (p InstallPath) ToMakePath() InstallPath {
Jiyong Park957bcd92020-10-20 18:23:33 +09001813 p.makePath = true
Colin Crossff6c33d2019-10-02 16:01:35 -07001814 return p
Colin Cross70dda7e2019-10-01 22:05:35 -07001815}
1816
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001817// PathForModuleInstall returns a Path representing the install path for the
1818// module appended with paths...
Colin Cross70dda7e2019-10-01 22:05:35 -07001819func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
Spandan Das5d1b9292021-06-03 19:36:41 +00001820 os, arch := osAndArch(ctx)
Cole Faust11edf552023-10-13 11:32:14 -07001821 partition := modulePartition(ctx, os.Class == Device)
Cole Faust3b703f32023-10-16 13:30:51 -07001822 return pathForInstall(ctx, os, arch, partition, pathComponents...)
Spandan Das5d1b9292021-06-03 19:36:41 +00001823}
1824
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001825// PathForHostDexInstall returns an InstallPath representing the install path for the
1826// module appended with paths...
1827func PathForHostDexInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
Cole Faust3b703f32023-10-16 13:30:51 -07001828 return pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "", pathComponents...)
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001829}
1830
Spandan Das5d1b9292021-06-03 19:36:41 +00001831// PathForModuleInPartitionInstall is similar to PathForModuleInstall but partition is provided by the caller
1832func PathForModuleInPartitionInstall(ctx ModuleInstallPathContext, partition string, pathComponents ...string) InstallPath {
1833 os, arch := osAndArch(ctx)
Cole Faust3b703f32023-10-16 13:30:51 -07001834 return pathForInstall(ctx, os, arch, partition, pathComponents...)
Spandan Das5d1b9292021-06-03 19:36:41 +00001835}
1836
1837func osAndArch(ctx ModuleInstallPathContext) (OsType, ArchType) {
Colin Cross6e359402020-02-10 15:29:54 -08001838 os := ctx.Os()
Jiyong Park87788b52020-09-01 12:37:45 +09001839 arch := ctx.Arch().ArchType
1840 forceOS, forceArch := ctx.InstallForceOS()
1841 if forceOS != nil {
Colin Cross6e359402020-02-10 15:29:54 -08001842 os = *forceOS
1843 }
Jiyong Park87788b52020-09-01 12:37:45 +09001844 if forceArch != nil {
1845 arch = *forceArch
1846 }
Spandan Das5d1b9292021-06-03 19:36:41 +00001847 return os, arch
1848}
Colin Cross609c49a2020-02-13 13:20:11 -08001849
Colin Crossc0e42d52024-02-01 16:42:36 -08001850func pathForPartitionInstallDir(ctx PathContext, partition, partitionPath string, makePath bool) InstallPath {
1851 fullPath := ctx.Config().SoongOutDir()
1852 if makePath {
1853 // Make path starts with out/ instead of out/soong.
1854 fullPath = filepath.Join(fullPath, "../", partitionPath)
1855 } else {
1856 fullPath = filepath.Join(fullPath, partitionPath)
1857 }
1858
1859 return InstallPath{
1860 basePath: basePath{partitionPath, ""},
1861 soongOutDir: ctx.Config().soongOutDir,
1862 partitionDir: partitionPath,
1863 partition: partition,
1864 makePath: makePath,
1865 fullPath: fullPath,
1866 }
1867}
1868
Cole Faust3b703f32023-10-16 13:30:51 -07001869func pathForInstall(ctx PathContext, os OsType, arch ArchType, partition string,
Colin Cross609c49a2020-02-13 13:20:11 -08001870 pathComponents ...string) InstallPath {
1871
Jiyong Park97859152023-02-14 17:05:48 +09001872 var partitionPaths []string
Colin Cross609c49a2020-02-13 13:20:11 -08001873
Colin Cross6e359402020-02-10 15:29:54 -08001874 if os.Class == Device {
Jiyong Park97859152023-02-14 17:05:48 +09001875 partitionPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001876 } else {
Jiyong Park87788b52020-09-01 12:37:45 +09001877 osName := os.String()
Colin Crossa9b2aac2022-06-15 17:25:51 -07001878 if os == Linux {
Jiyong Park87788b52020-09-01 12:37:45 +09001879 // instead of linux_glibc
1880 osName = "linux"
Dan Willemsen866b5632017-09-22 12:28:24 -07001881 }
Colin Crossa9b2aac2022-06-15 17:25:51 -07001882 if os == LinuxMusl && ctx.Config().UseHostMusl() {
1883 // When using musl instead of glibc, use "linux" instead of "linux_musl". When cross
1884 // compiling we will still use "linux_musl".
1885 osName = "linux"
1886 }
1887
Jiyong Park87788b52020-09-01 12:37:45 +09001888 // SOONG_HOST_OUT is set to out/host/$(HOST_OS)-$(HOST_PREBUILT_ARCH)
1889 // and HOST_PREBUILT_ARCH is forcibly set to x86 even on x86_64 hosts. We don't seem
1890 // to have a plan to fix it (see the comment in build/make/core/envsetup.mk).
1891 // Let's keep using x86 for the existing cases until we have a need to support
1892 // other architectures.
1893 archName := arch.String()
1894 if os.Class == Host && (arch == X86_64 || arch == Common) {
1895 archName = "x86"
1896 }
Jiyong Park97859152023-02-14 17:05:48 +09001897 partitionPaths = []string{"host", osName + "-" + archName, partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001898 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001899
Jiyong Park97859152023-02-14 17:05:48 +09001900 partitionPath, err := validatePath(partitionPaths...)
Colin Cross70dda7e2019-10-01 22:05:35 -07001901 if err != nil {
1902 reportPathError(ctx, err)
1903 }
Colin Crossff6c33d2019-10-02 16:01:35 -07001904
Colin Crossc0e42d52024-02-01 16:42:36 -08001905 base := pathForPartitionInstallDir(ctx, partition, partitionPath, ctx.Config().KatiEnabled())
Jiyong Park957bcd92020-10-20 18:23:33 +09001906 return base.Join(ctx, pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001907}
1908
Spandan Dasf280b232024-04-04 21:25:51 +00001909func PathForNdkInstall(ctx PathContext, paths ...string) OutputPath {
1910 return PathForOutput(ctx, append([]string{"ndk"}, paths...)...)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00001911}
1912
1913func PathForMainlineSdksInstall(ctx PathContext, paths ...string) InstallPath {
Spandan Dasf280b232024-04-04 21:25:51 +00001914 base := pathForPartitionInstallDir(ctx, "", "mainline-sdks", false)
1915 return base.Join(ctx, paths...)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00001916}
1917
Colin Cross70dda7e2019-10-01 22:05:35 -07001918func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string {
Colin Crossb1692a32021-10-25 15:39:01 -07001919 rel := Rel(ctx, strings.TrimSuffix(path.PartitionDir(), path.partition), path.String())
Colin Cross43f08db2018-11-12 10:13:39 -08001920 return "/" + rel
1921}
1922
Cole Faust11edf552023-10-13 11:32:14 -07001923func modulePartition(ctx ModuleInstallPathContext, device bool) string {
Colin Cross43f08db2018-11-12 10:13:39 -08001924 var partition string
Colin Cross6e359402020-02-10 15:29:54 -08001925 if ctx.InstallInTestcases() {
1926 // "testcases" install directory can be used for host or device modules.
Jaewoong Jung0949f312019-09-11 10:25:18 -07001927 partition = "testcases"
Cole Faust11edf552023-10-13 11:32:14 -07001928 } else if device {
Colin Cross6e359402020-02-10 15:29:54 -08001929 if ctx.InstallInData() {
1930 partition = "data"
1931 } else if ctx.InstallInRamdisk() {
1932 if ctx.DeviceConfig().BoardUsesRecoveryAsBoot() {
1933 partition = "recovery/root/first_stage_ramdisk"
1934 } else {
1935 partition = "ramdisk"
1936 }
1937 if !ctx.InstallInRoot() {
1938 partition += "/system"
1939 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -07001940 } else if ctx.InstallInVendorRamdisk() {
Yifan Hong39143a92020-10-26 12:43:12 -07001941 // The module is only available after switching root into
1942 // /first_stage_ramdisk. To expose the module before switching root
1943 // on a device without a dedicated recovery partition, install the
1944 // recovery variant.
Yifan Hongdd8dacc2020-10-21 15:40:17 -07001945 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
Petri Gyntherac229562021-03-02 23:44:02 -08001946 partition = "vendor_ramdisk/first_stage_ramdisk"
Yifan Hongdd8dacc2020-10-21 15:40:17 -07001947 } else {
Petri Gyntherac229562021-03-02 23:44:02 -08001948 partition = "vendor_ramdisk"
Yifan Hongdd8dacc2020-10-21 15:40:17 -07001949 }
1950 if !ctx.InstallInRoot() {
1951 partition += "/system"
1952 }
Inseob Kim08758f02021-04-08 21:13:22 +09001953 } else if ctx.InstallInDebugRamdisk() {
1954 partition = "debug_ramdisk"
Colin Cross6e359402020-02-10 15:29:54 -08001955 } else if ctx.InstallInRecovery() {
1956 if ctx.InstallInRoot() {
1957 partition = "recovery/root"
1958 } else {
1959 // the layout of recovery partion is the same as that of system partition
1960 partition = "recovery/root/system"
1961 }
Colin Crossea30d852023-11-29 16:00:16 -08001962 } else if ctx.SocSpecific() || ctx.InstallInVendor() {
Colin Cross6e359402020-02-10 15:29:54 -08001963 partition = ctx.DeviceConfig().VendorPath()
Colin Crossea30d852023-11-29 16:00:16 -08001964 } else if ctx.DeviceSpecific() || ctx.InstallInOdm() {
Colin Cross6e359402020-02-10 15:29:54 -08001965 partition = ctx.DeviceConfig().OdmPath()
Colin Crossea30d852023-11-29 16:00:16 -08001966 } else if ctx.ProductSpecific() || ctx.InstallInProduct() {
Colin Cross6e359402020-02-10 15:29:54 -08001967 partition = ctx.DeviceConfig().ProductPath()
1968 } else if ctx.SystemExtSpecific() {
1969 partition = ctx.DeviceConfig().SystemExtPath()
1970 } else if ctx.InstallInRoot() {
1971 partition = "root"
Yifan Hong82db7352020-01-21 16:12:26 -08001972 } else {
Colin Cross6e359402020-02-10 15:29:54 -08001973 partition = "system"
Yifan Hong82db7352020-01-21 16:12:26 -08001974 }
Colin Cross6e359402020-02-10 15:29:54 -08001975 if ctx.InstallInSanitizerDir() {
1976 partition = "data/asan/" + partition
Yifan Hong82db7352020-01-21 16:12:26 -08001977 }
Colin Cross43f08db2018-11-12 10:13:39 -08001978 }
1979 return partition
1980}
1981
Colin Cross609c49a2020-02-13 13:20:11 -08001982type InstallPaths []InstallPath
1983
1984// Paths returns the InstallPaths as a Paths
1985func (p InstallPaths) Paths() Paths {
1986 if p == nil {
1987 return nil
1988 }
1989 ret := make(Paths, len(p))
1990 for i, path := range p {
1991 ret[i] = path
1992 }
1993 return ret
1994}
1995
1996// Strings returns the string forms of the install paths.
1997func (p InstallPaths) Strings() []string {
1998 if p == nil {
1999 return nil
2000 }
2001 ret := make([]string, len(p))
2002 for i, path := range p {
2003 ret[i] = path.String()
2004 }
2005 return ret
2006}
2007
Jingwen Chen24d0c562023-02-07 09:29:36 +00002008// validatePathInternal ensures that a path does not leave its component, and
2009// optionally doesn't contain Ninja variables.
2010func validatePathInternal(allowNinjaVariables bool, pathComponents ...string) (string, error) {
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002011 initialEmpty := 0
2012 finalEmpty := 0
2013 for i, path := range pathComponents {
Jingwen Chen24d0c562023-02-07 09:29:36 +00002014 if !allowNinjaVariables && strings.Contains(path, "$") {
2015 return "", fmt.Errorf("Path contains invalid character($): %s", path)
2016 }
2017
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002018 path := filepath.Clean(path)
2019 if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08002020 return "", fmt.Errorf("Path is outside directory: %s", path)
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002021 }
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002022
2023 if i == initialEmpty && pathComponents[i] == "" {
2024 initialEmpty++
2025 }
2026 if i == finalEmpty && pathComponents[len(pathComponents)-1-i] == "" {
2027 finalEmpty++
2028 }
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002029 }
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002030 // Optimization: filepath.Join("foo", "") returns a newly allocated copy
2031 // of "foo", while filepath.Join("foo") does not. Strip out any empty
2032 // path components.
2033 if initialEmpty == len(pathComponents) {
2034 return "", nil
2035 }
2036 nonEmptyPathComponents := pathComponents[initialEmpty : len(pathComponents)-finalEmpty]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002037 // TODO: filepath.Join isn't necessarily correct with embedded ninja
2038 // variables. '..' may remove the entire ninja variable, even if it
2039 // will be expanded to multiple nested directories.
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002040 return filepath.Join(nonEmptyPathComponents...), nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002041}
2042
Jingwen Chen24d0c562023-02-07 09:29:36 +00002043// validateSafePath validates a path that we trust (may contain ninja
2044// variables). Ensures that each path component does not attempt to leave its
2045// component. Returns a joined version of each path component.
2046func validateSafePath(pathComponents ...string) (string, error) {
2047 return validatePathInternal(true, pathComponents...)
2048}
2049
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002050// validatePath validates that a path does not include ninja variables, and that
2051// each path component does not attempt to leave its component. Returns a joined
2052// version of each path component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08002053func validatePath(pathComponents ...string) (string, error) {
Jingwen Chen24d0c562023-02-07 09:29:36 +00002054 return validatePathInternal(false, pathComponents...)
Colin Cross6e18ca42015-07-14 18:55:36 -07002055}
Colin Cross5b529592017-05-09 13:34:34 -07002056
Colin Cross0875c522017-11-28 17:34:01 -08002057func PathForPhony(ctx PathContext, phony string) WritablePath {
2058 if strings.ContainsAny(phony, "$/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01002059 ReportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony)
Colin Cross0875c522017-11-28 17:34:01 -08002060 }
Paul Duffin74abc5d2021-03-24 09:24:59 +00002061 return PhonyPath{basePath{phony, ""}}
Colin Cross0875c522017-11-28 17:34:01 -08002062}
2063
Colin Cross74e3fe42017-12-11 15:51:44 -08002064type PhonyPath struct {
2065 basePath
2066}
2067
2068func (p PhonyPath) writablePath() {}
2069
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02002070func (p PhonyPath) getSoongOutDir() string {
Paul Duffind65c58b2021-03-24 09:22:07 +00002071 // A phone path cannot contain any / so cannot be relative to the build directory.
2072 return ""
Paul Duffin9b478b02019-12-10 13:41:51 +00002073}
2074
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002075func (p PhonyPath) RelativeToTop() Path {
2076 ensureTestOnly()
2077 // A phony path cannot contain any / so does not have a build directory so switching to a new
2078 // build directory has no effect so just return this path.
2079 return p
2080}
2081
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01002082func (p PhonyPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
2083 panic("Not implemented")
2084}
2085
Colin Cross74e3fe42017-12-11 15:51:44 -08002086var _ Path = PhonyPath{}
2087var _ WritablePath = PhonyPath{}
2088
Colin Cross5b529592017-05-09 13:34:34 -07002089type testPath struct {
2090 basePath
2091}
2092
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002093func (p testPath) RelativeToTop() Path {
2094 ensureTestOnly()
2095 return p
2096}
2097
Colin Cross5b529592017-05-09 13:34:34 -07002098func (p testPath) String() string {
2099 return p.path
2100}
2101
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002102var _ Path = testPath{}
2103
Colin Cross40e33732019-02-15 11:08:35 -08002104// PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from
2105// within tests.
Colin Cross5b529592017-05-09 13:34:34 -07002106func PathForTesting(paths ...string) Path {
Colin Cross1ccfcc32018-02-22 13:54:26 -08002107 p, err := validateSafePath(paths...)
2108 if err != nil {
2109 panic(err)
2110 }
Colin Cross5b529592017-05-09 13:34:34 -07002111 return testPath{basePath{path: p, rel: p}}
2112}
2113
Sam Delmerico2351eac2022-05-24 17:10:02 +00002114func PathForTestingWithRel(path, rel string) Path {
2115 p, err := validateSafePath(path, rel)
2116 if err != nil {
2117 panic(err)
2118 }
2119 r, err := validatePath(rel)
2120 if err != nil {
2121 panic(err)
2122 }
2123 return testPath{basePath{path: p, rel: r}}
2124}
2125
Colin Cross40e33732019-02-15 11:08:35 -08002126// PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests.
2127func PathsForTesting(strs ...string) Paths {
Colin Cross5b529592017-05-09 13:34:34 -07002128 p := make(Paths, len(strs))
2129 for i, s := range strs {
2130 p[i] = PathForTesting(s)
2131 }
2132
2133 return p
2134}
Colin Cross43f08db2018-11-12 10:13:39 -08002135
Colin Cross40e33732019-02-15 11:08:35 -08002136type testPathContext struct {
2137 config Config
Colin Cross40e33732019-02-15 11:08:35 -08002138}
2139
Colin Cross40e33732019-02-15 11:08:35 -08002140func (x *testPathContext) Config() Config { return x.config }
2141func (x *testPathContext) AddNinjaFileDeps(...string) {}
2142
2143// PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with
2144// PathForOutput.
Colin Cross98be1bb2019-12-13 20:41:13 -08002145func PathContextForTesting(config Config) PathContext {
Colin Cross40e33732019-02-15 11:08:35 -08002146 return &testPathContext{
2147 config: config,
Colin Cross40e33732019-02-15 11:08:35 -08002148 }
2149}
2150
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002151type testModuleInstallPathContext struct {
2152 baseModuleContext
2153
2154 inData bool
2155 inTestcases bool
2156 inSanitizerDir bool
2157 inRamdisk bool
2158 inVendorRamdisk bool
Inseob Kim08758f02021-04-08 21:13:22 +09002159 inDebugRamdisk bool
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002160 inRecovery bool
2161 inRoot bool
Colin Crossea30d852023-11-29 16:00:16 -08002162 inOdm bool
2163 inProduct bool
2164 inVendor bool
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002165 forceOS *OsType
2166 forceArch *ArchType
2167}
2168
2169func (m testModuleInstallPathContext) Config() Config {
2170 return m.baseModuleContext.config
2171}
2172
2173func (testModuleInstallPathContext) AddNinjaFileDeps(deps ...string) {}
2174
2175func (m testModuleInstallPathContext) InstallInData() bool {
2176 return m.inData
2177}
2178
2179func (m testModuleInstallPathContext) InstallInTestcases() bool {
2180 return m.inTestcases
2181}
2182
2183func (m testModuleInstallPathContext) InstallInSanitizerDir() bool {
2184 return m.inSanitizerDir
2185}
2186
2187func (m testModuleInstallPathContext) InstallInRamdisk() bool {
2188 return m.inRamdisk
2189}
2190
2191func (m testModuleInstallPathContext) InstallInVendorRamdisk() bool {
2192 return m.inVendorRamdisk
2193}
2194
Inseob Kim08758f02021-04-08 21:13:22 +09002195func (m testModuleInstallPathContext) InstallInDebugRamdisk() bool {
2196 return m.inDebugRamdisk
2197}
2198
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002199func (m testModuleInstallPathContext) InstallInRecovery() bool {
2200 return m.inRecovery
2201}
2202
2203func (m testModuleInstallPathContext) InstallInRoot() bool {
2204 return m.inRoot
2205}
2206
Colin Crossea30d852023-11-29 16:00:16 -08002207func (m testModuleInstallPathContext) InstallInOdm() bool {
2208 return m.inOdm
2209}
2210
2211func (m testModuleInstallPathContext) InstallInProduct() bool {
2212 return m.inProduct
2213}
2214
2215func (m testModuleInstallPathContext) InstallInVendor() bool {
2216 return m.inVendor
2217}
2218
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002219func (m testModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
2220 return m.forceOS, m.forceArch
2221}
2222
2223// Construct a minimal ModuleInstallPathContext for testing. Note that baseModuleContext is
2224// default-initialized, which leaves blueprint.baseModuleContext set to nil, so methods that are
2225// delegated to it will panic.
2226func ModuleInstallPathContextForTesting(config Config) ModuleInstallPathContext {
2227 ctx := &testModuleInstallPathContext{}
2228 ctx.config = config
2229 ctx.os = Android
2230 return ctx
2231}
2232
Colin Cross43f08db2018-11-12 10:13:39 -08002233// Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if
2234// targetPath is not inside basePath.
2235func Rel(ctx PathContext, basePath string, targetPath string) string {
2236 rel, isRel := MaybeRel(ctx, basePath, targetPath)
2237 if !isRel {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01002238 ReportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath)
Colin Cross43f08db2018-11-12 10:13:39 -08002239 return ""
2240 }
2241 return rel
2242}
2243
2244// MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if
2245// targetPath is not inside basePath.
2246func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) {
Dan Willemsen633c5022019-04-12 11:11:38 -07002247 rel, isRel, err := maybeRelErr(basePath, targetPath)
2248 if err != nil {
2249 reportPathError(ctx, err)
2250 }
2251 return rel, isRel
2252}
2253
2254func maybeRelErr(basePath string, targetPath string) (string, bool, error) {
Colin Cross43f08db2018-11-12 10:13:39 -08002255 // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first.
2256 if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) {
Dan Willemsen633c5022019-04-12 11:11:38 -07002257 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002258 }
2259 rel, err := filepath.Rel(basePath, targetPath)
2260 if err != nil {
Dan Willemsen633c5022019-04-12 11:11:38 -07002261 return "", false, err
Colin Cross43f08db2018-11-12 10:13:39 -08002262 } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") {
Dan Willemsen633c5022019-04-12 11:11:38 -07002263 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002264 }
Dan Willemsen633c5022019-04-12 11:11:38 -07002265 return rel, true, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002266}
Colin Cross988414c2020-01-11 01:11:46 +00002267
2268// Writes a file to the output directory. Attempting to write directly to the output directory
2269// will fail due to the sandbox of the soong_build process.
Chris Parsons1a12d032023-02-06 22:37:41 -05002270// Only writes the file if the file doesn't exist or if it has different contents, to prevent
2271// updating the timestamp if no changes would be made. (This is better for incremental
2272// performance.)
Colin Cross988414c2020-01-11 01:11:46 +00002273func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error {
Colin Crossd6421132021-11-09 12:32:34 -08002274 absPath := absolutePath(path.String())
2275 err := os.MkdirAll(filepath.Dir(absPath), 0777)
2276 if err != nil {
2277 return err
2278 }
Chris Parsons1a12d032023-02-06 22:37:41 -05002279 return pathtools.WriteFileIfChanged(absPath, data, perm)
Colin Cross988414c2020-01-11 01:11:46 +00002280}
2281
Liz Kammer2dd9ca42020-11-25 16:06:39 -08002282func RemoveAllOutputDir(path WritablePath) error {
2283 return os.RemoveAll(absolutePath(path.String()))
2284}
2285
2286func CreateOutputDirIfNonexistent(path WritablePath, perm os.FileMode) error {
2287 dir := absolutePath(path.String())
Liz Kammer09f947d2021-05-12 14:51:49 -04002288 return createDirIfNonexistent(dir, perm)
2289}
2290
2291func createDirIfNonexistent(dir string, perm os.FileMode) error {
Liz Kammer2dd9ca42020-11-25 16:06:39 -08002292 if _, err := os.Stat(dir); os.IsNotExist(err) {
2293 return os.MkdirAll(dir, os.ModePerm)
2294 } else {
2295 return err
2296 }
2297}
2298
Jingwen Chen78257e52021-05-21 02:34:24 +00002299// absolutePath is deliberately private so that Soong's Go plugins can't use it to find and
2300// read arbitrary files without going through the methods in the current package that track
2301// dependencies.
Colin Cross988414c2020-01-11 01:11:46 +00002302func absolutePath(path string) string {
2303 if filepath.IsAbs(path) {
2304 return path
2305 }
2306 return filepath.Join(absSrcDir, path)
2307}
Chris Parsons216e10a2020-07-09 17:12:52 -04002308
2309// A DataPath represents the path of a file to be used as data, for example
2310// a test library to be installed alongside a test.
2311// The data file should be installed (copied from `<SrcPath>`) to
2312// `<install_root>/<RelativeInstallPath>/<filename>`, or
2313// `<install_root>/<filename>` if RelativeInstallPath is empty.
2314type DataPath struct {
2315 // The path of the data file that should be copied into the data directory
2316 SrcPath Path
2317 // The install path of the data file, relative to the install root.
2318 RelativeInstallPath string
Colin Cross5c1d5fb2023-11-15 12:39:40 -08002319 // If WithoutRel is true, use SrcPath.Base() instead of SrcPath.Rel() as the filename.
2320 WithoutRel bool
Chris Parsons216e10a2020-07-09 17:12:52 -04002321}
Colin Crossdcf71b22021-02-01 13:59:03 -08002322
Colin Crossd442a0e2023-11-16 11:19:26 -08002323func (d *DataPath) ToRelativeInstallPath() string {
2324 relPath := d.SrcPath.Rel()
Colin Cross5c1d5fb2023-11-15 12:39:40 -08002325 if d.WithoutRel {
2326 relPath = d.SrcPath.Base()
2327 }
Colin Crossd442a0e2023-11-16 11:19:26 -08002328 if d.RelativeInstallPath != "" {
2329 relPath = filepath.Join(d.RelativeInstallPath, relPath)
2330 }
2331 return relPath
2332}
2333
Colin Crossdcf71b22021-02-01 13:59:03 -08002334// PathsIfNonNil returns a Paths containing only the non-nil input arguments.
2335func PathsIfNonNil(paths ...Path) Paths {
2336 if len(paths) == 0 {
2337 // Fast path for empty argument list
2338 return nil
2339 } else if len(paths) == 1 {
2340 // Fast path for a single argument
2341 if paths[0] != nil {
2342 return paths
2343 } else {
2344 return nil
2345 }
2346 }
2347 ret := make(Paths, 0, len(paths))
2348 for _, path := range paths {
2349 if path != nil {
2350 ret = append(ret, path)
2351 }
2352 }
2353 if len(ret) == 0 {
2354 return nil
2355 }
2356 return ret
2357}
Chris Wailesb2703ad2021-07-30 13:25:42 -07002358
2359var thirdPartyDirPrefixExceptions = []*regexp.Regexp{
2360 regexp.MustCompile("^vendor/[^/]*google[^/]*/"),
2361 regexp.MustCompile("^hardware/google/"),
2362 regexp.MustCompile("^hardware/interfaces/"),
2363 regexp.MustCompile("^hardware/libhardware[^/]*/"),
2364 regexp.MustCompile("^hardware/ril/"),
2365}
2366
2367func IsThirdPartyPath(path string) bool {
2368 thirdPartyDirPrefixes := []string{"external/", "vendor/", "hardware/"}
2369
2370 if HasAnyPrefix(path, thirdPartyDirPrefixes) {
2371 for _, prefix := range thirdPartyDirPrefixExceptions {
2372 if prefix.MatchString(path) {
2373 return false
2374 }
2375 }
2376 return true
2377 }
2378 return false
2379}