blob: 6322f7566f2e57e453493058769564f42990bca3 [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
Cole Faust4e2bf9f2024-09-11 13:26:20 -070097 HasMutatorFinished(mutatorName string) bool
Liz Kammera830f3a2020-11-10 10:50:34 -080098}
99
100// ModuleMissingDepsPathContext is a subset of *ModuleContext methods required by
101// the Path methods that rely on module dependencies having been resolved and ability to report
102// missing dependency errors.
103type ModuleMissingDepsPathContext interface {
104 ModuleWithDepsPathContext
105 AddMissingDependencies(missingDeps []string)
106}
107
Dan Willemsen00269f22017-07-06 16:59:48 -0700108type ModuleInstallPathContext interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700109 BaseModuleContext
Dan Willemsen00269f22017-07-06 16:59:48 -0700110
111 InstallInData() bool
Jaewoong Jung0949f312019-09-11 10:25:18 -0700112 InstallInTestcases() bool
Dan Willemsen00269f22017-07-06 16:59:48 -0700113 InstallInSanitizerDir() bool
Yifan Hong1b3348d2020-01-21 15:53:22 -0800114 InstallInRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700115 InstallInVendorRamdisk() bool
Inseob Kim08758f02021-04-08 21:13:22 +0900116 InstallInDebugRamdisk() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900117 InstallInRecovery() bool
Colin Cross90ba5f42019-10-02 11:10:58 -0700118 InstallInRoot() bool
Colin Crossea30d852023-11-29 16:00:16 -0800119 InstallInOdm() bool
120 InstallInProduct() bool
121 InstallInVendor() bool
Jiyong Park87788b52020-09-01 12:37:45 +0900122 InstallForceOS() (*OsType, *ArchType)
Dan Willemsen00269f22017-07-06 16:59:48 -0700123}
124
125var _ ModuleInstallPathContext = ModuleContext(nil)
126
Cole Faust11edf552023-10-13 11:32:14 -0700127type baseModuleContextToModuleInstallPathContext struct {
128 BaseModuleContext
129}
130
131func (ctx *baseModuleContextToModuleInstallPathContext) InstallInData() bool {
132 return ctx.Module().InstallInData()
133}
134
135func (ctx *baseModuleContextToModuleInstallPathContext) InstallInTestcases() bool {
136 return ctx.Module().InstallInTestcases()
137}
138
139func (ctx *baseModuleContextToModuleInstallPathContext) InstallInSanitizerDir() bool {
140 return ctx.Module().InstallInSanitizerDir()
141}
142
143func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRamdisk() bool {
144 return ctx.Module().InstallInRamdisk()
145}
146
147func (ctx *baseModuleContextToModuleInstallPathContext) InstallInVendorRamdisk() bool {
148 return ctx.Module().InstallInVendorRamdisk()
149}
150
151func (ctx *baseModuleContextToModuleInstallPathContext) InstallInDebugRamdisk() bool {
152 return ctx.Module().InstallInDebugRamdisk()
153}
154
155func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRecovery() bool {
156 return ctx.Module().InstallInRecovery()
157}
158
159func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRoot() bool {
160 return ctx.Module().InstallInRoot()
161}
162
Colin Crossea30d852023-11-29 16:00:16 -0800163func (ctx *baseModuleContextToModuleInstallPathContext) InstallInOdm() bool {
164 return ctx.Module().InstallInOdm()
165}
166
167func (ctx *baseModuleContextToModuleInstallPathContext) InstallInProduct() bool {
168 return ctx.Module().InstallInProduct()
169}
170
171func (ctx *baseModuleContextToModuleInstallPathContext) InstallInVendor() bool {
172 return ctx.Module().InstallInVendor()
173}
174
Cole Faust11edf552023-10-13 11:32:14 -0700175func (ctx *baseModuleContextToModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
176 return ctx.Module().InstallForceOS()
177}
178
179var _ ModuleInstallPathContext = (*baseModuleContextToModuleInstallPathContext)(nil)
180
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700181// errorfContext is the interface containing the Errorf method matching the
182// Errorf method in blueprint.SingletonContext.
183type errorfContext interface {
184 Errorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -0800185}
186
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700187var _ errorfContext = blueprint.SingletonContext(nil)
188
Spandan Das59a4a2b2024-01-09 21:35:56 +0000189// ModuleErrorfContext is the interface containing the ModuleErrorf method matching
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700190// the ModuleErrorf method in blueprint.ModuleContext.
Spandan Das59a4a2b2024-01-09 21:35:56 +0000191type ModuleErrorfContext interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700192 ModuleErrorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -0800193}
194
Spandan Das59a4a2b2024-01-09 21:35:56 +0000195var _ ModuleErrorfContext = blueprint.ModuleContext(nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700196
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700197// reportPathError will register an error with the attached context. It
198// attempts ctx.ModuleErrorf for a better error message first, then falls
199// back to ctx.Errorf.
Colin Cross1ccfcc32018-02-22 13:54:26 -0800200func reportPathError(ctx PathContext, err error) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100201 ReportPathErrorf(ctx, "%s", err.Error())
Colin Cross1ccfcc32018-02-22 13:54:26 -0800202}
203
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100204// ReportPathErrorf will register an error with the attached context. It
Colin Cross1ccfcc32018-02-22 13:54:26 -0800205// attempts ctx.ModuleErrorf for a better error message first, then falls
206// back to ctx.Errorf.
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100207func ReportPathErrorf(ctx PathContext, format string, args ...interface{}) {
Spandan Das59a4a2b2024-01-09 21:35:56 +0000208 if mctx, ok := ctx.(ModuleErrorfContext); ok {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700209 mctx.ModuleErrorf(format, args...)
210 } else if ectx, ok := ctx.(errorfContext); ok {
211 ectx.Errorf(format, args...)
212 } else {
213 panic(fmt.Sprintf(format, args...))
Colin Crossf2298272015-05-12 11:36:53 -0700214 }
215}
216
Colin Cross5e708052019-08-06 13:59:50 -0700217func pathContextName(ctx PathContext, module blueprint.Module) string {
218 if x, ok := ctx.(interface{ ModuleName(blueprint.Module) string }); ok {
219 return x.ModuleName(module)
220 } else if x, ok := ctx.(interface{ OtherModuleName(blueprint.Module) string }); ok {
221 return x.OtherModuleName(module)
222 }
223 return "unknown"
224}
225
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700226type Path interface {
227 // Returns the path in string form
228 String() string
229
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700230 // Ext returns the extension of the last element of the path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700231 Ext() string
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700232
233 // Base returns the last element of the path
234 Base() string
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800235
236 // Rel returns the portion of the path relative to the directory it was created from. For
237 // example, Rel on a PathsForModuleSrc would return the path relative to the module source
Colin Cross0db55682017-12-05 15:36:55 -0800238 // directory, and OutputPath.Join("foo").Rel() would return "foo".
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800239 Rel() string
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000240
Colin Cross7707b242024-07-26 12:02:36 -0700241 // WithoutRel returns a new Path with no relative path, i.e. Rel() will return the same value as Base().
242 WithoutRel() Path
243
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000244 // RelativeToTop returns a new path relative to the top, it is provided solely for use in tests.
245 //
246 // It is guaranteed to always return the same type as it is called on, e.g. if called on an
247 // InstallPath then the returned value can be converted to an InstallPath.
248 //
249 // A standard build has the following structure:
250 // ../top/
251 // out/ - make install files go here.
Colin Cross3b1c6842024-07-26 11:52:57 -0700252 // out/soong - this is the outDir passed to NewTestConfig()
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000253 // ... - the source files
254 //
255 // This function converts a path so that it appears relative to the ../top/ directory, i.e.
Colin Cross3b1c6842024-07-26 11:52:57 -0700256 // * Make install paths, which have the pattern "outDir/../<path>" are converted into the top
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000257 // relative path "out/<path>"
Colin Cross3b1c6842024-07-26 11:52:57 -0700258 // * Soong install paths and other writable paths, which have the pattern "outDir/soong/<path>" are
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000259 // converted into the top relative path "out/soong/<path>".
260 // * Source paths are already relative to the top.
261 // * Phony paths are not relative to anything.
262 // * toolDepPath have an absolute but known value in so don't need making relative to anything in
263 // order to test.
264 RelativeToTop() Path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700265}
266
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000267const (
Colin Cross3b1c6842024-07-26 11:52:57 -0700268 testOutDir = "out"
269 testOutSoongSubDir = "/soong"
270 TestOutSoongDir = testOutDir + testOutSoongSubDir
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000271)
272
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700273// WritablePath is a type of path that can be used as an output for build rules.
274type WritablePath interface {
275 Path
276
Paul Duffin9b478b02019-12-10 13:41:51 +0000277 // return the path to the build directory.
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200278 getSoongOutDir() string
Paul Duffin9b478b02019-12-10 13:41:51 +0000279
Jeff Gaston734e3802017-04-10 15:47:24 -0700280 // the writablePath method doesn't directly do anything,
281 // but it allows a struct to distinguish between whether or not it implements the WritablePath interface
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700282 writablePath()
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +0100283
284 ReplaceExtension(ctx PathContext, ext string) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700285}
286
287type genPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800288 genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath
yangbill6d032dd2024-04-18 03:05:49 +0000289 genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700290}
291type objPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800292 objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700293}
294type resPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800295 resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700296}
297
298// GenPathWithExt derives a new file path in ctx's generated sources directory
299// from the current path, but with the new extension.
Liz Kammera830f3a2020-11-10 10:50:34 -0800300func GenPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700301 if path, ok := p.(genPathProvider); ok {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700302 return path.genPathWithExt(ctx, subdir, ext)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700303 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100304 ReportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700305 return PathForModuleGen(ctx)
306}
307
yangbill6d032dd2024-04-18 03:05:49 +0000308// GenPathWithExtAndTrimExt derives a new file path in ctx's generated sources directory
309// from the current path, but with the new extension and trim the suffix.
310func GenPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir string, p Path, ext string, trimExt string) ModuleGenPath {
311 if path, ok := p.(genPathProvider); ok {
312 return path.genPathWithExtAndTrimExt(ctx, subdir, ext, trimExt)
313 }
314 ReportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
315 return PathForModuleGen(ctx)
316}
317
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700318// ObjPathWithExt derives a new file path in ctx's object directory from the
319// current path, but with the new extension.
Liz Kammera830f3a2020-11-10 10:50:34 -0800320func ObjPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700321 if path, ok := p.(objPathProvider); ok {
322 return path.objPathWithExt(ctx, subdir, ext)
323 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100324 ReportPathErrorf(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700325 return PathForModuleObj(ctx)
326}
327
328// ResPathWithName derives a new path in ctx's output resource directory, using
329// the current path to create the directory name, and the `name` argument for
330// the filename.
Liz Kammera830f3a2020-11-10 10:50:34 -0800331func ResPathWithName(ctx ModuleOutPathContext, p Path, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700332 if path, ok := p.(resPathProvider); ok {
333 return path.resPathWithName(ctx, name)
334 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100335 ReportPathErrorf(ctx, "Tried to create res file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700336 return PathForModuleRes(ctx)
337}
338
339// OptionalPath is a container that may or may not contain a valid Path.
340type OptionalPath struct {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100341 path Path // nil if invalid.
342 invalidReason string // Not applicable if path != nil. "" if the reason is unknown.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700343}
344
345// OptionalPathForPath returns an OptionalPath containing the path.
346func OptionalPathForPath(path Path) OptionalPath {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100347 return OptionalPath{path: path}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700348}
349
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100350// InvalidOptionalPath returns an OptionalPath that is invalid with the given reason.
351func InvalidOptionalPath(reason string) OptionalPath {
352
353 return OptionalPath{invalidReason: reason}
354}
355
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700356// Valid returns whether there is a valid path
357func (p OptionalPath) Valid() bool {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100358 return p.path != nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700359}
360
361// Path returns the Path embedded in this OptionalPath. You must be sure that
362// there is a valid path, since this method will panic if there is not.
363func (p OptionalPath) Path() Path {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100364 if p.path == nil {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100365 msg := "Requesting an invalid path"
366 if p.invalidReason != "" {
367 msg += ": " + p.invalidReason
368 }
369 panic(msg)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700370 }
371 return p.path
372}
373
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100374// InvalidReason returns the reason that the optional path is invalid, or "" if it is valid.
375func (p OptionalPath) InvalidReason() string {
376 if p.path != nil {
377 return ""
378 }
379 if p.invalidReason == "" {
380 return "unknown"
381 }
382 return p.invalidReason
383}
384
Paul Duffinef081852021-05-13 11:11:15 +0100385// AsPaths converts the OptionalPath into Paths.
386//
387// It returns nil if this is not valid, or a single length slice containing the Path embedded in
388// this OptionalPath.
389func (p OptionalPath) AsPaths() Paths {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100390 if p.path == nil {
Paul Duffinef081852021-05-13 11:11:15 +0100391 return nil
392 }
393 return Paths{p.path}
394}
395
Paul Duffinafdd4062021-03-30 19:44:07 +0100396// RelativeToTop returns an OptionalPath with the path that was embedded having been replaced by the
397// result of calling Path.RelativeToTop on it.
398func (p OptionalPath) RelativeToTop() OptionalPath {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100399 if p.path == nil {
Paul Duffina5b81352021-03-28 23:57:19 +0100400 return p
401 }
402 p.path = p.path.RelativeToTop()
403 return p
404}
405
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700406// String returns the string version of the Path, or "" if it isn't valid.
407func (p OptionalPath) String() string {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100408 if p.path != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700409 return p.path.String()
410 } else {
411 return ""
Colin Crossf2298272015-05-12 11:36:53 -0700412 }
413}
Colin Cross6e18ca42015-07-14 18:55:36 -0700414
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700415// Paths is a slice of Path objects, with helpers to operate on the collection.
416type Paths []Path
417
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000418// RelativeToTop creates a new Paths containing the result of calling Path.RelativeToTop on each
419// item in this slice.
420func (p Paths) RelativeToTop() Paths {
421 ensureTestOnly()
422 if p == nil {
423 return p
424 }
425 ret := make(Paths, len(p))
426 for i, path := range p {
427 ret[i] = path.RelativeToTop()
428 }
429 return ret
430}
431
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000432func (paths Paths) containsPath(path Path) bool {
433 for _, p := range paths {
434 if p == path {
435 return true
436 }
437 }
438 return false
439}
440
Liz Kammer7aa52882021-02-11 09:16:14 -0500441// PathsForSource returns Paths rooted from SrcDir, *not* rooted from the module's local source
442// directory
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700443func PathsForSource(ctx PathContext, paths []string) Paths {
444 ret := make(Paths, len(paths))
445 for i, path := range paths {
446 ret[i] = PathForSource(ctx, path)
447 }
448 return ret
449}
450
Liz Kammer7aa52882021-02-11 09:16:14 -0500451// ExistentPathsForSources returns a list of Paths rooted from SrcDir, *not* rooted from the
452// module's local source directory, that are found in the tree. If any are not found, they are
453// 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 -0700454func ExistentPathsForSources(ctx PathGlobContext, paths []string) Paths {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800455 ret := make(Paths, 0, len(paths))
456 for _, path := range paths {
Colin Cross32f38982018-02-22 11:47:25 -0800457 p := ExistentPathForSource(ctx, path)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800458 if p.Valid() {
459 ret = append(ret, p.Path())
460 }
461 }
462 return ret
463}
464
Liz Kammer620dea62021-04-14 17:36:10 -0400465// PathsForModuleSrc returns a Paths{} containing the resolved references in paths:
Colin Crossd079e0b2022-08-16 10:27:33 -0700466// - filepath, relative to local module directory, resolves as a filepath relative to the local
467// source directory
468// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
469// source directory.
470// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700471// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
472// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700473//
Liz Kammer620dea62021-04-14 17:36:10 -0400474// Properties passed as the paths argument must have been annotated with struct tag
Colin Cross41955e82019-05-29 14:40:35 -0700475// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000476// pathdeps mutator.
Liz Kammer620dea62021-04-14 17:36:10 -0400477// If a requested module is not found as a dependency:
Colin Crossd079e0b2022-08-16 10:27:33 -0700478// - if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having
Liz Kammer620dea62021-04-14 17:36:10 -0400479// missing dependencies
Colin Crossd079e0b2022-08-16 10:27:33 -0700480// - otherwise, a ModuleError is thrown.
Liz Kammera830f3a2020-11-10 10:50:34 -0800481func PathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string) Paths {
Colin Cross8a497952019-03-05 22:25:09 -0800482 return PathsForModuleSrcExcludes(ctx, paths, nil)
483}
484
Liz Kammer619be462022-01-28 15:13:39 -0500485type SourceInput struct {
486 Context ModuleMissingDepsPathContext
487 Paths []string
488 ExcludePaths []string
489 IncludeDirs bool
490}
491
Liz Kammer620dea62021-04-14 17:36:10 -0400492// PathsForModuleSrcExcludes returns a Paths{} containing the resolved references in paths, minus
493// those listed in excludes. Elements of paths and excludes are resolved as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700494// - filepath, relative to local module directory, resolves as a filepath relative to the local
495// source directory
496// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
497// source directory. Not valid in excludes.
498// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700499// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
500// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700501//
Liz Kammer620dea62021-04-14 17:36:10 -0400502// excluding the items (similarly resolved
503// Properties passed as the paths argument must have been annotated with struct tag
504// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000505// pathdeps mutator.
Liz Kammer620dea62021-04-14 17:36:10 -0400506// If a requested module is not found as a dependency:
Colin Crossd079e0b2022-08-16 10:27:33 -0700507// - if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having
Liz Kammer620dea62021-04-14 17:36:10 -0400508// missing dependencies
Colin Crossd079e0b2022-08-16 10:27:33 -0700509// - otherwise, a ModuleError is thrown.
Liz Kammera830f3a2020-11-10 10:50:34 -0800510func PathsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) Paths {
Liz Kammer619be462022-01-28 15:13:39 -0500511 return PathsRelativeToModuleSourceDir(SourceInput{
512 Context: ctx,
513 Paths: paths,
514 ExcludePaths: excludes,
515 IncludeDirs: true,
516 })
517}
518
519func PathsRelativeToModuleSourceDir(input SourceInput) Paths {
520 ret, missingDeps := PathsAndMissingDepsRelativeToModuleSourceDir(input)
521 if input.Context.Config().AllowMissingDependencies() {
522 input.Context.AddMissingDependencies(missingDeps)
Colin Crossba71a3f2019-03-18 12:12:48 -0700523 } else {
524 for _, m := range missingDeps {
Liz Kammer619be462022-01-28 15:13:39 -0500525 input.Context.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m)
Colin Crossba71a3f2019-03-18 12:12:48 -0700526 }
527 }
528 return ret
529}
530
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000531// OutputPaths is a slice of OutputPath objects, with helpers to operate on the collection.
532type OutputPaths []OutputPath
533
534// Paths returns the OutputPaths as a Paths
535func (p OutputPaths) Paths() Paths {
536 if p == nil {
537 return nil
538 }
539 ret := make(Paths, len(p))
540 for i, path := range p {
541 ret[i] = path
542 }
543 return ret
544}
545
546// Strings returns the string forms of the writable paths.
547func (p OutputPaths) Strings() []string {
548 if p == nil {
549 return nil
550 }
551 ret := make([]string, len(p))
552 for i, path := range p {
553 ret[i] = path.String()
554 }
555 return ret
556}
557
Colin Crossa44551f2021-10-25 15:36:21 -0700558// PathForGoBinary returns the path to the installed location of a bootstrap_go_binary module.
559func PathForGoBinary(ctx PathContext, goBinary bootstrap.GoBinaryTool) Path {
Cole Faust3b703f32023-10-16 13:30:51 -0700560 goBinaryInstallDir := pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "bin")
Colin Crossa44551f2021-10-25 15:36:21 -0700561 rel := Rel(ctx, goBinaryInstallDir.String(), goBinary.InstallPath())
562 return goBinaryInstallDir.Join(ctx, rel)
563}
564
Liz Kammera830f3a2020-11-10 10:50:34 -0800565// Expands Paths to a SourceFileProducer or OutputFileProducer module dependency referenced via ":name" or ":name{.tag}" syntax.
566// If the dependency is not found, a missingErrorDependency is returned.
567// If the module dependency is not a SourceFileProducer or OutputFileProducer, appropriate errors will be returned.
568func getPathsFromModuleDep(ctx ModuleWithDepsPathContext, path, moduleName, tag string) (Paths, error) {
Paul Duffind5cf92e2021-07-09 17:38:55 +0100569 module := GetModuleFromPathDep(ctx, moduleName, tag)
Liz Kammera830f3a2020-11-10 10:50:34 -0800570 if module == nil {
571 return nil, missingDependencyError{[]string{moduleName}}
572 }
Cole Fausta963b942024-04-11 17:43:00 -0700573 if aModule, ok := module.(Module); ok && !aModule.Enabled(ctx) {
Colin Crossfa65cee2021-03-22 17:05:59 -0700574 return nil, missingDependencyError{[]string{moduleName}}
575 }
mrziwange6c85812024-05-22 14:36:09 -0700576 if goBinary, ok := module.(bootstrap.GoBinaryTool); ok && tag == "" {
Colin Crossa44551f2021-10-25 15:36:21 -0700577 goBinaryPath := PathForGoBinary(ctx, goBinary)
578 return Paths{goBinaryPath}, nil
mrziwange6c85812024-05-22 14:36:09 -0700579 }
580 outputFiles, err := outputFilesForModule(ctx, module, tag)
581 if outputFiles != nil && err == nil {
582 return outputFiles, nil
Liz Kammera830f3a2020-11-10 10:50:34 -0800583 } else {
mrziwange6c85812024-05-22 14:36:09 -0700584 return nil, err
Liz Kammera830f3a2020-11-10 10:50:34 -0800585 }
586}
587
Paul Duffind5cf92e2021-07-09 17:38:55 +0100588// GetModuleFromPathDep will return the module that was added as a dependency automatically for
589// properties tagged with `android:"path"` or manually using ExtractSourceDeps or
590// ExtractSourcesDeps.
591//
592// The moduleName and tag supplied to this should be the values returned from SrcIsModuleWithTag.
593// Or, if no tag is expected then the moduleName should be the value returned by SrcIsModule and
594// the tag must be "".
595//
596// If tag is "" then the returned module will be the dependency that was added for ":moduleName".
597// Otherwise, it is the dependency that was added for ":moduleName{tag}".
Paul Duffind5cf92e2021-07-09 17:38:55 +0100598func GetModuleFromPathDep(ctx ModuleWithDepsPathContext, moduleName, tag string) blueprint.Module {
Paul Duffin40131a32021-07-09 17:10:35 +0100599 var found blueprint.Module
600 // The sourceOrOutputDepTag uniquely identifies the module dependency as it contains both the
601 // module name and the tag. Dependencies added automatically for properties tagged with
602 // `android:"path"` are deduped so are guaranteed to be unique. It is possible for duplicate
603 // dependencies to be added manually using ExtractSourcesDeps or ExtractSourceDeps but even then
604 // it will always be the case that the dependencies will be identical, i.e. the same tag and same
605 // moduleName referring to the same dependency module.
606 //
607 // It does not matter whether the moduleName is a fully qualified name or if the module
608 // dependency is a prebuilt module. All that matters is the same information is supplied to
609 // create the tag here as was supplied to create the tag when the dependency was added so that
610 // this finds the matching dependency module.
611 expectedTag := sourceOrOutputDepTag(moduleName, tag)
612 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
613 depTag := ctx.OtherModuleDependencyTag(module)
614 if depTag == expectedTag {
615 found = module
616 }
617 })
618 return found
Paul Duffind5cf92e2021-07-09 17:38:55 +0100619}
620
Liz Kammer620dea62021-04-14 17:36:10 -0400621// PathsAndMissingDepsForModuleSrcExcludes returns a Paths{} containing the resolved references in
622// paths, minus those listed in excludes. Elements of paths and excludes are resolved as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700623// - filepath, relative to local module directory, resolves as a filepath relative to the local
624// source directory
625// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
626// source directory. Not valid in excludes.
627// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700628// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
629// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700630//
Liz Kammer620dea62021-04-14 17:36:10 -0400631// and a list of the module names of missing module dependencies are returned as the second return.
632// Properties passed as the paths argument must have been annotated with struct tag
Colin Cross41955e82019-05-29 14:40:35 -0700633// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000634// pathdeps mutator.
Liz Kammer619be462022-01-28 15:13:39 -0500635func PathsAndMissingDepsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) (Paths, []string) {
636 return PathsAndMissingDepsRelativeToModuleSourceDir(SourceInput{
637 Context: ctx,
638 Paths: paths,
639 ExcludePaths: excludes,
640 IncludeDirs: true,
641 })
642}
643
644func PathsAndMissingDepsRelativeToModuleSourceDir(input SourceInput) (Paths, []string) {
645 prefix := pathForModuleSrc(input.Context).String()
Colin Cross8a497952019-03-05 22:25:09 -0800646
647 var expandedExcludes []string
Liz Kammer619be462022-01-28 15:13:39 -0500648 if input.ExcludePaths != nil {
649 expandedExcludes = make([]string, 0, len(input.ExcludePaths))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700650 }
Colin Cross8a497952019-03-05 22:25:09 -0800651
Colin Crossba71a3f2019-03-18 12:12:48 -0700652 var missingExcludeDeps []string
Liz Kammer619be462022-01-28 15:13:39 -0500653 for _, e := range input.ExcludePaths {
Colin Cross41955e82019-05-29 14:40:35 -0700654 if m, t := SrcIsModuleWithTag(e); m != "" {
Liz Kammer619be462022-01-28 15:13:39 -0500655 modulePaths, err := getPathsFromModuleDep(input.Context, e, m, t)
Liz Kammera830f3a2020-11-10 10:50:34 -0800656 if m, ok := err.(missingDependencyError); ok {
657 missingExcludeDeps = append(missingExcludeDeps, m.missingDeps...)
658 } else if err != nil {
Liz Kammer619be462022-01-28 15:13:39 -0500659 reportPathError(input.Context, err)
Colin Cross8a497952019-03-05 22:25:09 -0800660 } else {
Liz Kammera830f3a2020-11-10 10:50:34 -0800661 expandedExcludes = append(expandedExcludes, modulePaths.Strings()...)
Colin Cross8a497952019-03-05 22:25:09 -0800662 }
663 } else {
664 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
665 }
666 }
667
Liz Kammer619be462022-01-28 15:13:39 -0500668 if input.Paths == nil {
Colin Crossba71a3f2019-03-18 12:12:48 -0700669 return nil, missingExcludeDeps
Colin Cross8a497952019-03-05 22:25:09 -0800670 }
671
Colin Crossba71a3f2019-03-18 12:12:48 -0700672 var missingDeps []string
673
Liz Kammer619be462022-01-28 15:13:39 -0500674 expandedSrcFiles := make(Paths, 0, len(input.Paths))
675 for _, s := range input.Paths {
676 srcFiles, err := expandOneSrcPath(sourcePathInput{
677 context: input.Context,
678 path: s,
679 expandedExcludes: expandedExcludes,
680 includeDirs: input.IncludeDirs,
681 })
Colin Cross8a497952019-03-05 22:25:09 -0800682 if depErr, ok := err.(missingDependencyError); ok {
Colin Crossba71a3f2019-03-18 12:12:48 -0700683 missingDeps = append(missingDeps, depErr.missingDeps...)
Colin Cross8a497952019-03-05 22:25:09 -0800684 } else if err != nil {
Liz Kammer619be462022-01-28 15:13:39 -0500685 reportPathError(input.Context, err)
Colin Cross8a497952019-03-05 22:25:09 -0800686 }
687 expandedSrcFiles = append(expandedSrcFiles, srcFiles...)
688 }
Colin Crossba71a3f2019-03-18 12:12:48 -0700689
Jihoon Kang0e3a5352024-04-12 00:45:50 +0000690 // TODO: b/334169722 - Replace with an error instead of implicitly removing duplicates.
691 return FirstUniquePaths(expandedSrcFiles), append(missingDeps, missingExcludeDeps...)
Colin Cross8a497952019-03-05 22:25:09 -0800692}
693
694type missingDependencyError struct {
695 missingDeps []string
696}
697
698func (e missingDependencyError) Error() string {
699 return "missing dependencies: " + strings.Join(e.missingDeps, ", ")
700}
701
Liz Kammer619be462022-01-28 15:13:39 -0500702type sourcePathInput struct {
703 context ModuleWithDepsPathContext
704 path string
705 expandedExcludes []string
706 includeDirs bool
707}
708
Liz Kammera830f3a2020-11-10 10:50:34 -0800709// Expands one path string to Paths rooted from the module's local source
710// directory, excluding those listed in the expandedExcludes.
711// Expands globs, references to SourceFileProducer or OutputFileProducer modules using the ":name" and ":name{.tag}" syntax.
Liz Kammer619be462022-01-28 15:13:39 -0500712func expandOneSrcPath(input sourcePathInput) (Paths, error) {
Jooyung Han7607dd32020-07-05 10:23:14 +0900713 excludePaths := func(paths Paths) Paths {
Liz Kammer619be462022-01-28 15:13:39 -0500714 if len(input.expandedExcludes) == 0 {
Jooyung Han7607dd32020-07-05 10:23:14 +0900715 return paths
716 }
717 remainder := make(Paths, 0, len(paths))
718 for _, p := range paths {
Liz Kammer619be462022-01-28 15:13:39 -0500719 if !InList(p.String(), input.expandedExcludes) {
Jooyung Han7607dd32020-07-05 10:23:14 +0900720 remainder = append(remainder, p)
721 }
722 }
723 return remainder
724 }
Liz Kammer619be462022-01-28 15:13:39 -0500725 if m, t := SrcIsModuleWithTag(input.path); m != "" {
726 modulePaths, err := getPathsFromModuleDep(input.context, input.path, m, t)
Liz Kammera830f3a2020-11-10 10:50:34 -0800727 if err != nil {
728 return nil, err
Colin Cross8a497952019-03-05 22:25:09 -0800729 } else {
Liz Kammera830f3a2020-11-10 10:50:34 -0800730 return excludePaths(modulePaths), nil
Colin Cross8a497952019-03-05 22:25:09 -0800731 }
Colin Cross8a497952019-03-05 22:25:09 -0800732 } else {
Liz Kammer619be462022-01-28 15:13:39 -0500733 p := pathForModuleSrc(input.context, input.path)
734 if pathtools.IsGlob(input.path) {
735 paths := GlobFiles(input.context, p.String(), input.expandedExcludes)
736 return PathsWithModuleSrcSubDir(input.context, paths, ""), nil
737 } else {
738 if exists, _, err := input.context.Config().fs.Exists(p.String()); err != nil {
739 ReportPathErrorf(input.context, "%s: %s", p, err.Error())
740 } else if !exists && !input.context.Config().TestAllowNonExistentPaths {
741 ReportPathErrorf(input.context, "module source path %q does not exist", p)
742 } else if !input.includeDirs {
743 if isDir, err := input.context.Config().fs.IsDir(p.String()); exists && err != nil {
744 ReportPathErrorf(input.context, "%s: %s", p, err.Error())
745 } else if isDir {
746 ReportPathErrorf(input.context, "module source path %q is a directory", p)
747 }
748 }
Colin Cross8a497952019-03-05 22:25:09 -0800749
Liz Kammer619be462022-01-28 15:13:39 -0500750 if InList(p.String(), input.expandedExcludes) {
751 return nil, nil
752 }
753 return Paths{p}, nil
Colin Cross8a497952019-03-05 22:25:09 -0800754 }
Colin Cross8a497952019-03-05 22:25:09 -0800755 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700756}
757
758// pathsForModuleSrcFromFullPath returns Paths rooted from the module's local
759// source directory, but strip the local source directory from the beginning of
Dan Willemsen540a78c2018-02-26 21:50:08 -0800760// each string. If incDirs is false, strip paths with a trailing '/' from the list.
Colin Crossfe4bc362018-09-12 10:02:13 -0700761// It intended for use in globs that only list files that exist, so it allows '$' in
762// filenames.
Liz Kammera830f3a2020-11-10 10:50:34 -0800763func pathsForModuleSrcFromFullPath(ctx EarlyModulePathContext, paths []string, incDirs bool) Paths {
Lukacs T. Berkif7e36d82021-08-16 17:05:09 +0200764 prefix := ctx.ModuleDir() + "/"
Colin Cross0f37af02017-09-27 17:42:05 -0700765 if prefix == "./" {
766 prefix = ""
767 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700768 ret := make(Paths, 0, len(paths))
769 for _, p := range paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -0800770 if !incDirs && strings.HasSuffix(p, "/") {
771 continue
772 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700773 path := filepath.Clean(p)
774 if !strings.HasPrefix(path, prefix) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100775 ReportPathErrorf(ctx, "Path %q is not in module source directory %q", p, prefix)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700776 continue
777 }
Colin Crosse3924e12018-08-15 20:18:53 -0700778
Colin Crossfe4bc362018-09-12 10:02:13 -0700779 srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):])
Colin Crosse3924e12018-08-15 20:18:53 -0700780 if err != nil {
781 reportPathError(ctx, err)
782 continue
783 }
784
Colin Cross07e51612019-03-05 12:46:40 -0800785 srcPath.basePath.rel = srcPath.path
Colin Crosse3924e12018-08-15 20:18:53 -0700786
Colin Cross07e51612019-03-05 12:46:40 -0800787 ret = append(ret, srcPath)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700788 }
789 return ret
790}
791
Liz Kammera830f3a2020-11-10 10:50:34 -0800792// PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's local source
793// directory. If input is nil, use the default if it exists. If input is empty, returns nil.
794func PathsWithOptionalDefaultForModuleSrc(ctx ModuleMissingDepsPathContext, input []string, def string) Paths {
Colin Cross0ddae7f2019-02-07 15:30:01 -0800795 if input != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700796 return PathsForModuleSrc(ctx, input)
797 }
798 // Use Glob so that if the default doesn't exist, a dependency is added so that when it
799 // is created, we're run again.
Lukacs T. Berkif7e36d82021-08-16 17:05:09 +0200800 path := filepath.Join(ctx.ModuleDir(), def)
Liz Kammera830f3a2020-11-10 10:50:34 -0800801 return Glob(ctx, path, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700802}
803
804// Strings returns the Paths in string form
805func (p Paths) Strings() []string {
806 if p == nil {
807 return nil
808 }
809 ret := make([]string, len(p))
810 for i, path := range p {
811 ret[i] = path.String()
812 }
813 return ret
814}
815
Colin Crossc0efd1d2020-07-03 11:56:24 -0700816func CopyOfPaths(paths Paths) Paths {
817 return append(Paths(nil), paths...)
818}
819
Colin Crossb6715442017-10-24 11:13:31 -0700820// FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It
821// modifies the Paths slice contents in place, and returns a subslice of the original slice.
Dan Willemsenfe92c962017-08-29 12:28:37 -0700822func FirstUniquePaths(list Paths) Paths {
Colin Cross27027c72020-02-28 15:34:17 -0800823 // 128 was chosen based on BenchmarkFirstUniquePaths results.
824 if len(list) > 128 {
825 return firstUniquePathsMap(list)
826 }
827 return firstUniquePathsList(list)
828}
829
Colin Crossc0efd1d2020-07-03 11:56:24 -0700830// SortedUniquePaths returns all unique elements of a Paths in sorted order. It modifies the
831// Paths slice contents in place, and returns a subslice of the original slice.
Jiyong Park33c77362020-05-29 22:00:16 +0900832func SortedUniquePaths(list Paths) Paths {
833 unique := FirstUniquePaths(list)
834 sort.Slice(unique, func(i, j int) bool {
835 return unique[i].String() < unique[j].String()
836 })
837 return unique
838}
839
Colin Cross27027c72020-02-28 15:34:17 -0800840func firstUniquePathsList(list Paths) Paths {
Dan Willemsenfe92c962017-08-29 12:28:37 -0700841 k := 0
842outer:
843 for i := 0; i < len(list); i++ {
844 for j := 0; j < k; j++ {
845 if list[i] == list[j] {
846 continue outer
847 }
848 }
849 list[k] = list[i]
850 k++
851 }
852 return list[:k]
853}
854
Colin Cross27027c72020-02-28 15:34:17 -0800855func firstUniquePathsMap(list Paths) Paths {
856 k := 0
857 seen := make(map[Path]bool, len(list))
858 for i := 0; i < len(list); i++ {
859 if seen[list[i]] {
860 continue
861 }
862 seen[list[i]] = true
863 list[k] = list[i]
864 k++
865 }
866 return list[:k]
867}
868
Colin Cross5d583952020-11-24 16:21:24 -0800869// FirstUniqueInstallPaths returns all unique elements of an InstallPaths, keeping the first copy of each. It
870// modifies the InstallPaths slice contents in place, and returns a subslice of the original slice.
871func FirstUniqueInstallPaths(list InstallPaths) InstallPaths {
872 // 128 was chosen based on BenchmarkFirstUniquePaths results.
873 if len(list) > 128 {
874 return firstUniqueInstallPathsMap(list)
875 }
876 return firstUniqueInstallPathsList(list)
877}
878
879func firstUniqueInstallPathsList(list InstallPaths) InstallPaths {
880 k := 0
881outer:
882 for i := 0; i < len(list); i++ {
883 for j := 0; j < k; j++ {
884 if list[i] == list[j] {
885 continue outer
886 }
887 }
888 list[k] = list[i]
889 k++
890 }
891 return list[:k]
892}
893
894func firstUniqueInstallPathsMap(list InstallPaths) InstallPaths {
895 k := 0
896 seen := make(map[InstallPath]bool, len(list))
897 for i := 0; i < len(list); i++ {
898 if seen[list[i]] {
899 continue
900 }
901 seen[list[i]] = true
902 list[k] = list[i]
903 k++
904 }
905 return list[:k]
906}
907
Colin Crossb6715442017-10-24 11:13:31 -0700908// LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It
909// modifies the Paths slice contents in place, and returns a subslice of the original slice.
910func LastUniquePaths(list Paths) Paths {
911 totalSkip := 0
912 for i := len(list) - 1; i >= totalSkip; i-- {
913 skip := 0
914 for j := i - 1; j >= totalSkip; j-- {
915 if list[i] == list[j] {
916 skip++
917 } else {
918 list[j+skip] = list[j]
919 }
920 }
921 totalSkip += skip
922 }
923 return list[totalSkip:]
924}
925
Colin Crossa140bb02018-04-17 10:52:26 -0700926// ReversePaths returns a copy of a Paths in reverse order.
927func ReversePaths(list Paths) Paths {
928 if list == nil {
929 return nil
930 }
931 ret := make(Paths, len(list))
932 for i := range list {
933 ret[i] = list[len(list)-1-i]
934 }
935 return ret
936}
937
Jeff Gaston294356f2017-09-27 17:05:30 -0700938func indexPathList(s Path, list []Path) int {
939 for i, l := range list {
940 if l == s {
941 return i
942 }
943 }
944
945 return -1
946}
947
948func inPathList(p Path, list []Path) bool {
949 return indexPathList(p, list) != -1
950}
951
952func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) {
Paul Duffin57b9e1d2019-12-13 00:03:35 +0000953 return FilterPathListPredicate(list, func(p Path) bool { return inPathList(p, filter) })
954}
955
956func FilterPathListPredicate(list []Path, predicate func(Path) bool) (remainder []Path, filtered []Path) {
Jeff Gaston294356f2017-09-27 17:05:30 -0700957 for _, l := range list {
Paul Duffin57b9e1d2019-12-13 00:03:35 +0000958 if predicate(l) {
Jeff Gaston294356f2017-09-27 17:05:30 -0700959 filtered = append(filtered, l)
960 } else {
961 remainder = append(remainder, l)
962 }
963 }
964
965 return
966}
967
Colin Cross93e85952017-08-15 13:34:18 -0700968// HasExt returns true of any of the paths have extension ext, otherwise false
969func (p Paths) HasExt(ext string) bool {
970 for _, path := range p {
971 if path.Ext() == ext {
972 return true
973 }
974 }
975
976 return false
977}
978
979// FilterByExt returns the subset of the paths that have extension ext
980func (p Paths) FilterByExt(ext string) Paths {
981 ret := make(Paths, 0, len(p))
982 for _, path := range p {
983 if path.Ext() == ext {
984 ret = append(ret, path)
985 }
986 }
987 return ret
988}
989
990// FilterOutByExt returns the subset of the paths that do not have extension ext
991func (p Paths) FilterOutByExt(ext string) Paths {
992 ret := make(Paths, 0, len(p))
993 for _, path := range p {
994 if path.Ext() != ext {
995 ret = append(ret, path)
996 }
997 }
998 return ret
999}
1000
Colin Cross5e6cfbe2017-11-03 15:20:35 -07001001// DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory
1002// (including subdirectories) are in a contiguous subslice of the list, and can be found in
1003// O(log(N)) time using a binary search on the directory prefix.
1004type DirectorySortedPaths Paths
1005
1006func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths {
1007 ret := append(DirectorySortedPaths(nil), paths...)
1008 sort.Slice(ret, func(i, j int) bool {
1009 return ret[i].String() < ret[j].String()
1010 })
1011 return ret
1012}
1013
1014// PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries
1015// that are in the specified directory and its subdirectories.
1016func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths {
1017 prefix := filepath.Clean(dir) + "/"
1018 start := sort.Search(len(p), func(i int) bool {
1019 return prefix < p[i].String()
1020 })
1021
1022 ret := p[start:]
1023
1024 end := sort.Search(len(ret), func(i int) bool {
1025 return !strings.HasPrefix(ret[i].String(), prefix)
1026 })
1027
1028 ret = ret[:end]
1029
1030 return Paths(ret)
1031}
1032
Alex Humesky29e3bbe2020-11-20 21:30:13 -05001033// WritablePaths is a slice of WritablePath, used for multiple outputs.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001034type WritablePaths []WritablePath
1035
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001036// RelativeToTop creates a new WritablePaths containing the result of calling Path.RelativeToTop on
1037// each item in this slice.
1038func (p WritablePaths) RelativeToTop() WritablePaths {
1039 ensureTestOnly()
1040 if p == nil {
1041 return p
1042 }
1043 ret := make(WritablePaths, len(p))
1044 for i, path := range p {
1045 ret[i] = path.RelativeToTop().(WritablePath)
1046 }
1047 return ret
1048}
1049
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001050// Strings returns the string forms of the writable paths.
1051func (p WritablePaths) Strings() []string {
1052 if p == nil {
1053 return nil
1054 }
1055 ret := make([]string, len(p))
1056 for i, path := range p {
1057 ret[i] = path.String()
1058 }
1059 return ret
1060}
1061
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001062// Paths returns the WritablePaths as a Paths
1063func (p WritablePaths) Paths() Paths {
1064 if p == nil {
1065 return nil
1066 }
1067 ret := make(Paths, len(p))
1068 for i, path := range p {
1069 ret[i] = path
1070 }
1071 return ret
1072}
1073
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001074type basePath struct {
Paul Duffin74abc5d2021-03-24 09:24:59 +00001075 path string
1076 rel string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001077}
1078
Yu Liufa297642024-06-11 00:13:02 +00001079func (p basePath) GobEncode() ([]byte, error) {
1080 w := new(bytes.Buffer)
1081 encoder := gob.NewEncoder(w)
1082 err := errors.Join(encoder.Encode(p.path), encoder.Encode(p.rel))
1083 if err != nil {
1084 return nil, err
1085 }
1086
1087 return w.Bytes(), nil
1088}
1089
1090func (p *basePath) GobDecode(data []byte) error {
1091 r := bytes.NewBuffer(data)
1092 decoder := gob.NewDecoder(r)
1093 err := errors.Join(decoder.Decode(&p.path), decoder.Decode(&p.rel))
1094 if err != nil {
1095 return err
1096 }
1097
1098 return nil
1099}
1100
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001101func (p basePath) Ext() string {
1102 return filepath.Ext(p.path)
1103}
1104
Colin Cross4f6fc9c2016-10-26 10:05:25 -07001105func (p basePath) Base() string {
1106 return filepath.Base(p.path)
1107}
1108
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001109func (p basePath) Rel() string {
1110 if p.rel != "" {
1111 return p.rel
1112 }
1113 return p.path
1114}
1115
Colin Cross0875c522017-11-28 17:34:01 -08001116func (p basePath) String() string {
1117 return p.path
1118}
1119
Colin Cross0db55682017-12-05 15:36:55 -08001120func (p basePath) withRel(rel string) basePath {
1121 p.path = filepath.Join(p.path, rel)
1122 p.rel = rel
1123 return p
1124}
1125
Colin Cross7707b242024-07-26 12:02:36 -07001126func (p basePath) withoutRel() basePath {
1127 p.rel = filepath.Base(p.path)
1128 return p
1129}
1130
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001131// SourcePath is a Path representing a file path rooted from SrcDir
1132type SourcePath struct {
1133 basePath
1134}
1135
1136var _ Path = SourcePath{}
1137
Colin Cross0db55682017-12-05 15:36:55 -08001138func (p SourcePath) withRel(rel string) SourcePath {
1139 p.basePath = p.basePath.withRel(rel)
1140 return p
1141}
1142
Colin Crossbd73d0d2024-07-26 12:00:33 -07001143func (p SourcePath) RelativeToTop() Path {
1144 ensureTestOnly()
1145 return p
1146}
1147
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001148// safePathForSource is for paths that we expect are safe -- only for use by go
1149// code that is embedding ninja variables in paths
Colin Crossfe4bc362018-09-12 10:02:13 -07001150func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
1151 p, err := validateSafePath(pathComponents...)
Cole Faust483d1f72023-01-09 14:35:27 -08001152 ret := SourcePath{basePath{p, ""}}
Colin Crossfe4bc362018-09-12 10:02:13 -07001153 if err != nil {
1154 return ret, err
1155 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001156
Colin Cross7b3dcc32019-01-24 13:14:39 -08001157 // absolute path already checked by validateSafePath
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001158 // special-case api surface gen files for now
1159 if strings.HasPrefix(ret.String(), ctx.Config().soongOutDir) && !strings.Contains(ret.String(), ctx.Config().soongOutDir+"/.export") {
Mikhail Naganovab1f5182019-02-08 13:17:55 -08001160 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Cross6e18ca42015-07-14 18:55:36 -07001161 }
1162
Colin Crossfe4bc362018-09-12 10:02:13 -07001163 return ret, err
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001164}
1165
Colin Cross192e97a2018-02-22 14:21:02 -08001166// pathForSource creates a SourcePath from pathComponents, but does not check that it exists.
1167func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
Colin Crossc48c1432018-02-23 07:09:01 +00001168 p, err := validatePath(pathComponents...)
Cole Faust483d1f72023-01-09 14:35:27 -08001169 ret := SourcePath{basePath{p, ""}}
Colin Cross94a32102018-02-22 14:21:02 -08001170 if err != nil {
Colin Cross192e97a2018-02-22 14:21:02 -08001171 return ret, err
Colin Cross94a32102018-02-22 14:21:02 -08001172 }
1173
Colin Cross7b3dcc32019-01-24 13:14:39 -08001174 // absolute path already checked by validatePath
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001175 // special-case for now
1176 if strings.HasPrefix(ret.String(), ctx.Config().soongOutDir) && !strings.Contains(ret.String(), ctx.Config().soongOutDir+"/.export") {
Mikhail Naganovab1f5182019-02-08 13:17:55 -08001177 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Crossc48c1432018-02-23 07:09:01 +00001178 }
1179
Colin Cross192e97a2018-02-22 14:21:02 -08001180 return ret, nil
1181}
1182
1183// existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the
1184// path does not exist.
Colin Cross662d6142022-11-03 20:38:01 -07001185func existsWithDependencies(ctx PathGlobContext, path SourcePath) (exists bool, err error) {
Colin Cross192e97a2018-02-22 14:21:02 -08001186 var files []string
1187
Colin Cross662d6142022-11-03 20:38:01 -07001188 // Use glob to produce proper dependencies, even though we only want
1189 // a single file.
1190 files, err = ctx.GlobWithDeps(path.String(), nil)
Colin Cross192e97a2018-02-22 14:21:02 -08001191
1192 if err != nil {
1193 return false, fmt.Errorf("glob: %s", err.Error())
1194 }
1195
1196 return len(files) > 0, nil
1197}
1198
1199// PathForSource joins the provided path components and validates that the result
1200// neither escapes the source dir nor is in the out dir.
1201// On error, it will return a usable, but invalid SourcePath, and report a ModuleError.
1202func PathForSource(ctx PathContext, pathComponents ...string) SourcePath {
1203 path, err := pathForSource(ctx, pathComponents...)
1204 if err != nil {
1205 reportPathError(ctx, err)
1206 }
1207
Colin Crosse3924e12018-08-15 20:18:53 -07001208 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001209 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -07001210 }
1211
Liz Kammera830f3a2020-11-10 10:50:34 -08001212 if modCtx, ok := ctx.(ModuleMissingDepsPathContext); ok && ctx.Config().AllowMissingDependencies() {
Colin Cross662d6142022-11-03 20:38:01 -07001213 exists, err := existsWithDependencies(modCtx, path)
Colin Cross192e97a2018-02-22 14:21:02 -08001214 if err != nil {
1215 reportPathError(ctx, err)
1216 }
1217 if !exists {
1218 modCtx.AddMissingDependencies([]string{path.String()})
1219 }
Colin Cross988414c2020-01-11 01:11:46 +00001220 } else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001221 ReportPathErrorf(ctx, "%s: %s", path, err.Error())
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001222 } else if !exists && !ctx.Config().TestAllowNonExistentPaths {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001223 ReportPathErrorf(ctx, "source path %q does not exist", path)
Colin Cross192e97a2018-02-22 14:21:02 -08001224 }
1225 return path
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001226}
1227
Cole Faustbc65a3f2023-08-01 16:38:55 +00001228// PathForArbitraryOutput creates a path for the given components. Unlike PathForOutput,
1229// the path is relative to the root of the output folder, not the out/soong folder.
1230func PathForArbitraryOutput(ctx PathContext, pathComponents ...string) Path {
Colin Cross3b1c6842024-07-26 11:52:57 -07001231 path, err := validatePath(pathComponents...)
Cole Faustbc65a3f2023-08-01 16:38:55 +00001232 if err != nil {
1233 reportPathError(ctx, err)
1234 }
Colin Cross3b1c6842024-07-26 11:52:57 -07001235 fullPath := filepath.Join(ctx.Config().OutDir(), path)
1236 path = fullPath[len(fullPath)-len(path):]
1237 return OutputPath{basePath{path, ""}, ctx.Config().OutDir(), fullPath}
Cole Faustbc65a3f2023-08-01 16:38:55 +00001238}
1239
Spandan Dasc6c10fa2022-10-21 21:52:13 +00001240// MaybeExistentPathForSource joins the provided path components and validates that the result
1241// neither escapes the source dir nor is in the out dir.
1242// It does not validate whether the path exists.
1243func MaybeExistentPathForSource(ctx PathContext, pathComponents ...string) SourcePath {
1244 path, err := pathForSource(ctx, pathComponents...)
1245 if err != nil {
1246 reportPathError(ctx, err)
1247 }
1248
1249 if pathtools.IsGlob(path.String()) {
1250 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
1251 }
1252 return path
1253}
1254
Liz Kammer7aa52882021-02-11 09:16:14 -05001255// ExistentPathForSource returns an OptionalPath with the SourcePath, rooted from SrcDir, *not*
1256// rooted from the module's local source directory, if the path exists, or an empty OptionalPath if
1257// it doesn't exist. Dependencies are added so that the ninja file will be regenerated if the state
1258// of the path changes.
Colin Cross662d6142022-11-03 20:38:01 -07001259func ExistentPathForSource(ctx PathGlobContext, pathComponents ...string) OptionalPath {
Colin Cross192e97a2018-02-22 14:21:02 -08001260 path, err := pathForSource(ctx, pathComponents...)
Colin Cross1ccfcc32018-02-22 13:54:26 -08001261 if err != nil {
1262 reportPathError(ctx, err)
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001263 // No need to put the error message into the returned path since it has been reported already.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001264 return OptionalPath{}
1265 }
Colin Crossc48c1432018-02-23 07:09:01 +00001266
Colin Crosse3924e12018-08-15 20:18:53 -07001267 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001268 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -07001269 return OptionalPath{}
1270 }
1271
Colin Cross192e97a2018-02-22 14:21:02 -08001272 exists, err := existsWithDependencies(ctx, path)
Colin Crossc48c1432018-02-23 07:09:01 +00001273 if err != nil {
1274 reportPathError(ctx, err)
1275 return OptionalPath{}
1276 }
Colin Cross192e97a2018-02-22 14:21:02 -08001277 if !exists {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001278 return InvalidOptionalPath(path.String() + " does not exist")
Colin Crossc48c1432018-02-23 07:09:01 +00001279 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001280 return OptionalPathForPath(path)
1281}
1282
1283func (p SourcePath) String() string {
Cole Faust483d1f72023-01-09 14:35:27 -08001284 if p.path == "" {
1285 return "."
1286 }
1287 return p.path
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001288}
1289
Colin Cross7707b242024-07-26 12:02:36 -07001290func (p SourcePath) WithoutRel() Path {
1291 p.basePath = p.basePath.withoutRel()
1292 return p
1293}
1294
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001295// Join creates a new SourcePath with paths... joined with the current path. The
1296// provided paths... may not use '..' to escape from the current path.
1297func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001298 path, err := validatePath(paths...)
1299 if err != nil {
1300 reportPathError(ctx, err)
1301 }
Colin Cross0db55682017-12-05 15:36:55 -08001302 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001303}
1304
Colin Cross2fafa3e2019-03-05 12:39:51 -08001305// join is like Join but does less path validation.
1306func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath {
1307 path, err := validateSafePath(paths...)
1308 if err != nil {
1309 reportPathError(ctx, err)
1310 }
1311 return p.withRel(path)
1312}
1313
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001314// OverlayPath returns the overlay for `path' if it exists. This assumes that the
1315// SourcePath is the path to a resource overlay directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001316func (p SourcePath) OverlayPath(ctx ModuleMissingDepsPathContext, path Path) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001317 var relDir string
Colin Cross07e51612019-03-05 12:46:40 -08001318 if srcPath, ok := path.(SourcePath); ok {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001319 relDir = srcPath.path
1320 } else {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001321 ReportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001322 // No need to put the error message into the returned path since it has been reported already.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001323 return OptionalPath{}
1324 }
Cole Faust483d1f72023-01-09 14:35:27 -08001325 dir := filepath.Join(p.path, relDir)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001326 // Use Glob so that we are run again if the directory is added.
Colin Cross7f19f372016-11-01 11:10:25 -07001327 if pathtools.IsGlob(dir) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001328 ReportPathErrorf(ctx, "Path may not contain a glob: %s", dir)
Dan Willemsen7b310ee2015-12-18 15:11:17 -08001329 }
Colin Cross461b4452018-02-23 09:22:42 -08001330 paths, err := ctx.GlobWithDeps(dir, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001331 if err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001332 ReportPathErrorf(ctx, "glob: %s", err.Error())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001333 return OptionalPath{}
1334 }
1335 if len(paths) == 0 {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001336 return InvalidOptionalPath(dir + " does not exist")
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001337 }
Cole Faust483d1f72023-01-09 14:35:27 -08001338 return OptionalPathForPath(PathForSource(ctx, paths[0]))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001339}
1340
Colin Cross70dda7e2019-10-01 22:05:35 -07001341// OutputPath is a Path representing an intermediates file path rooted from the build directory
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001342type OutputPath struct {
1343 basePath
Paul Duffind65c58b2021-03-24 09:22:07 +00001344
Colin Cross3b1c6842024-07-26 11:52:57 -07001345 // The base out directory for this path, either Config.SoongOutDir() or Config.OutDir()
1346 outDir string
Paul Duffind65c58b2021-03-24 09:22:07 +00001347
Colin Crossd63c9a72020-01-29 16:52:50 -08001348 fullPath string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001349}
1350
Yu Liufa297642024-06-11 00:13:02 +00001351func (p OutputPath) GobEncode() ([]byte, error) {
1352 w := new(bytes.Buffer)
1353 encoder := gob.NewEncoder(w)
Colin Cross3b1c6842024-07-26 11:52:57 -07001354 err := errors.Join(encoder.Encode(p.basePath), encoder.Encode(p.outDir), encoder.Encode(p.fullPath))
Yu Liufa297642024-06-11 00:13:02 +00001355 if err != nil {
1356 return nil, err
1357 }
1358
1359 return w.Bytes(), nil
1360}
1361
1362func (p *OutputPath) GobDecode(data []byte) error {
1363 r := bytes.NewBuffer(data)
1364 decoder := gob.NewDecoder(r)
Colin Cross3b1c6842024-07-26 11:52:57 -07001365 err := errors.Join(decoder.Decode(&p.basePath), decoder.Decode(&p.outDir), decoder.Decode(&p.fullPath))
Yu Liufa297642024-06-11 00:13:02 +00001366 if err != nil {
1367 return err
1368 }
1369
1370 return nil
1371}
1372
Colin Cross702e0f82017-10-18 17:27:54 -07001373func (p OutputPath) withRel(rel string) OutputPath {
Colin Cross0db55682017-12-05 15:36:55 -08001374 p.basePath = p.basePath.withRel(rel)
Colin Crossd63c9a72020-01-29 16:52:50 -08001375 p.fullPath = filepath.Join(p.fullPath, rel)
Colin Cross702e0f82017-10-18 17:27:54 -07001376 return p
1377}
1378
Colin Cross7707b242024-07-26 12:02:36 -07001379func (p OutputPath) WithoutRel() Path {
1380 p.basePath = p.basePath.withoutRel()
Colin Cross3063b782018-08-15 11:19:12 -07001381 return p
1382}
1383
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001384func (p OutputPath) getSoongOutDir() string {
Colin Cross3b1c6842024-07-26 11:52:57 -07001385 return p.outDir
Paul Duffin9b478b02019-12-10 13:41:51 +00001386}
1387
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001388func (p OutputPath) RelativeToTop() Path {
1389 return p.outputPathRelativeToTop()
1390}
1391
1392func (p OutputPath) outputPathRelativeToTop() OutputPath {
Colin Cross3b1c6842024-07-26 11:52:57 -07001393 p.fullPath = StringPathRelativeToTop(p.outDir, p.fullPath)
1394 if strings.HasSuffix(p.outDir, testOutSoongSubDir) {
1395 p.outDir = TestOutSoongDir
1396 } else {
1397 // Handle the PathForArbitraryOutput case
1398 p.outDir = testOutDir
1399 }
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001400 return p
1401}
1402
Paul Duffin0267d492021-02-02 10:05:52 +00001403func (p OutputPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
1404 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1405}
1406
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001407var _ Path = OutputPath{}
Paul Duffin9b478b02019-12-10 13:41:51 +00001408var _ WritablePath = OutputPath{}
Paul Duffin0267d492021-02-02 10:05:52 +00001409var _ objPathProvider = OutputPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001410
Chris Parsons8f232a22020-06-23 17:37:05 -04001411// toolDepPath is a Path representing a dependency of the build tool.
1412type toolDepPath struct {
1413 basePath
1414}
1415
Colin Cross7707b242024-07-26 12:02:36 -07001416func (t toolDepPath) WithoutRel() Path {
1417 t.basePath = t.basePath.withoutRel()
1418 return t
1419}
1420
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001421func (t toolDepPath) RelativeToTop() Path {
1422 ensureTestOnly()
1423 return t
1424}
1425
Chris Parsons8f232a22020-06-23 17:37:05 -04001426var _ Path = toolDepPath{}
1427
1428// pathForBuildToolDep returns a toolDepPath representing the given path string.
1429// There is no validation for the path, as it is "trusted": It may fail
1430// normal validation checks. For example, it may be an absolute path.
1431// Only use this function to construct paths for dependencies of the build
1432// tool invocation.
1433func pathForBuildToolDep(ctx PathContext, path string) toolDepPath {
Paul Duffin74abc5d2021-03-24 09:24:59 +00001434 return toolDepPath{basePath{path, ""}}
Chris Parsons8f232a22020-06-23 17:37:05 -04001435}
1436
Jeff Gaston734e3802017-04-10 15:47:24 -07001437// PathForOutput joins the provided paths and returns an OutputPath that is
1438// validated to not escape the build dir.
1439// On error, it will return a usable, but invalid OutputPath, and report a ModuleError.
1440func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001441 path, err := validatePath(pathComponents...)
1442 if err != nil {
1443 reportPathError(ctx, err)
1444 }
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001445 fullPath := filepath.Join(ctx.Config().soongOutDir, path)
Colin Crossd63c9a72020-01-29 16:52:50 -08001446 path = fullPath[len(fullPath)-len(path):]
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001447 return OutputPath{basePath{path, ""}, ctx.Config().soongOutDir, fullPath}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001448}
1449
Colin Cross3b1c6842024-07-26 11:52:57 -07001450// PathsForOutput returns Paths rooted from outDir
Colin Cross40e33732019-02-15 11:08:35 -08001451func PathsForOutput(ctx PathContext, paths []string) WritablePaths {
1452 ret := make(WritablePaths, len(paths))
1453 for i, path := range paths {
1454 ret[i] = PathForOutput(ctx, path)
1455 }
1456 return ret
1457}
1458
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001459func (p OutputPath) writablePath() {}
1460
1461func (p OutputPath) String() string {
Colin Crossd63c9a72020-01-29 16:52:50 -08001462 return p.fullPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001463}
1464
1465// Join creates a new OutputPath with paths... joined with the current path. The
1466// provided paths... may not use '..' to escape from the current path.
1467func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001468 path, err := validatePath(paths...)
1469 if err != nil {
1470 reportPathError(ctx, err)
1471 }
Colin Cross0db55682017-12-05 15:36:55 -08001472 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001473}
1474
Colin Cross8854a5a2019-02-11 14:14:16 -08001475// ReplaceExtension creates a new OutputPath with the extension replaced with ext.
1476func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1477 if strings.Contains(ext, "/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001478 ReportPathErrorf(ctx, "extension %q cannot contain /", ext)
Colin Cross8854a5a2019-02-11 14:14:16 -08001479 }
1480 ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext))
Colin Cross2cdd5df2019-02-25 10:25:24 -08001481 ret.rel = pathtools.ReplaceExtension(p.rel, ext)
Colin Cross8854a5a2019-02-11 14:14:16 -08001482 return ret
1483}
1484
Colin Cross40e33732019-02-15 11:08:35 -08001485// InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths.
1486func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath {
1487 path, err := validatePath(paths...)
1488 if err != nil {
1489 reportPathError(ctx, err)
1490 }
1491
1492 ret := PathForOutput(ctx, filepath.Dir(p.path), path)
Colin Cross2cdd5df2019-02-25 10:25:24 -08001493 ret.rel = filepath.Join(filepath.Dir(p.rel), path)
Colin Cross40e33732019-02-15 11:08:35 -08001494 return ret
1495}
1496
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001497// PathForIntermediates returns an OutputPath representing the top-level
1498// intermediates directory.
1499func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001500 path, err := validatePath(paths...)
1501 if err != nil {
1502 reportPathError(ctx, err)
1503 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001504 return PathForOutput(ctx, ".intermediates", path)
1505}
1506
Colin Cross07e51612019-03-05 12:46:40 -08001507var _ genPathProvider = SourcePath{}
1508var _ objPathProvider = SourcePath{}
1509var _ resPathProvider = SourcePath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001510
Colin Cross07e51612019-03-05 12:46:40 -08001511// PathForModuleSrc returns a Path representing the paths... under the
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001512// module's local source directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001513func PathForModuleSrc(ctx ModuleMissingDepsPathContext, pathComponents ...string) Path {
Paul Duffin407501b2021-07-09 16:56:35 +01001514 // Just join the components textually just to make sure that it does not corrupt a fully qualified
1515 // module reference, e.g. if the pathComponents is "://other:foo" then using filepath.Join() or
1516 // validatePath() will corrupt it, e.g. replace "//" with "/". If the path is not a module
1517 // reference then it will be validated by expandOneSrcPath anyway when it calls expandOneSrcPath.
1518 p := strings.Join(pathComponents, string(filepath.Separator))
Liz Kammer619be462022-01-28 15:13:39 -05001519 paths, err := expandOneSrcPath(sourcePathInput{context: ctx, path: p, includeDirs: true})
Colin Cross8a497952019-03-05 22:25:09 -08001520 if err != nil {
1521 if depErr, ok := err.(missingDependencyError); ok {
1522 if ctx.Config().AllowMissingDependencies() {
1523 ctx.AddMissingDependencies(depErr.missingDeps)
1524 } else {
1525 ctx.ModuleErrorf(`%s, is the property annotated with android:"path"?`, depErr.Error())
1526 }
1527 } else {
1528 reportPathError(ctx, err)
1529 }
1530 return nil
1531 } else if len(paths) == 0 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001532 ReportPathErrorf(ctx, "%q produced no files, expected exactly one", p)
Colin Cross8a497952019-03-05 22:25:09 -08001533 return nil
1534 } else if len(paths) > 1 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001535 ReportPathErrorf(ctx, "%q produced %d files, expected exactly one", p, len(paths))
Colin Cross8a497952019-03-05 22:25:09 -08001536 }
1537 return paths[0]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001538}
1539
Liz Kammera830f3a2020-11-10 10:50:34 -08001540func pathForModuleSrc(ctx EarlyModulePathContext, paths ...string) SourcePath {
Colin Cross07e51612019-03-05 12:46:40 -08001541 p, err := validatePath(paths...)
1542 if err != nil {
1543 reportPathError(ctx, err)
1544 }
1545
1546 path, err := pathForSource(ctx, ctx.ModuleDir(), p)
1547 if err != nil {
1548 reportPathError(ctx, err)
1549 }
1550
1551 path.basePath.rel = p
1552
1553 return path
1554}
1555
Colin Cross2fafa3e2019-03-05 12:39:51 -08001556// PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path
1557// will return the path relative to subDir in the module's source directory. If any input paths are not located
1558// inside subDir then a path error will be reported.
Liz Kammera830f3a2020-11-10 10:50:34 -08001559func PathsWithModuleSrcSubDir(ctx EarlyModulePathContext, paths Paths, subDir string) Paths {
Colin Cross2fafa3e2019-03-05 12:39:51 -08001560 paths = append(Paths(nil), paths...)
Colin Cross07e51612019-03-05 12:46:40 -08001561 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001562 for i, path := range paths {
1563 rel := Rel(ctx, subDirFullPath.String(), path.String())
1564 paths[i] = subDirFullPath.join(ctx, rel)
1565 }
1566 return paths
1567}
1568
1569// PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the
1570// 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 -08001571func PathWithModuleSrcSubDir(ctx EarlyModulePathContext, path Path, subDir string) Path {
Colin Cross07e51612019-03-05 12:46:40 -08001572 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001573 rel := Rel(ctx, subDirFullPath.String(), path.String())
1574 return subDirFullPath.Join(ctx, rel)
1575}
1576
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001577// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
1578// valid path if p is non-nil.
Liz Kammera830f3a2020-11-10 10:50:34 -08001579func OptionalPathForModuleSrc(ctx ModuleMissingDepsPathContext, p *string) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001580 if p == nil {
1581 return OptionalPath{}
1582 }
1583 return OptionalPathForPath(PathForModuleSrc(ctx, *p))
1584}
1585
Liz Kammera830f3a2020-11-10 10:50:34 -08001586func (p SourcePath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001587 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001588}
1589
yangbill6d032dd2024-04-18 03:05:49 +00001590func (p SourcePath) genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath {
1591 // If Trim_extension being set, force append Output_extension without replace original extension.
1592 if trimExt != "" {
1593 if ext != "" {
1594 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt)+"."+ext)
1595 }
1596 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt))
1597 }
1598 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1599}
1600
Liz Kammera830f3a2020-11-10 10:50:34 -08001601func (p SourcePath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001602 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001603}
1604
Liz Kammera830f3a2020-11-10 10:50:34 -08001605func (p SourcePath) resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001606 // TODO: Use full directory if the new ctx is not the current ctx?
1607 return PathForModuleRes(ctx, p.path, name)
1608}
1609
1610// ModuleOutPath is a Path representing a module's output directory.
1611type ModuleOutPath struct {
1612 OutputPath
1613}
1614
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001615func (p ModuleOutPath) RelativeToTop() Path {
1616 p.OutputPath = p.outputPathRelativeToTop()
1617 return p
1618}
1619
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001620var _ Path = ModuleOutPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001621var _ WritablePath = ModuleOutPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001622
Liz Kammera830f3a2020-11-10 10:50:34 -08001623func (p ModuleOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01001624 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1625}
1626
Liz Kammera830f3a2020-11-10 10:50:34 -08001627// ModuleOutPathContext Subset of ModuleContext functions necessary for output path methods.
1628type ModuleOutPathContext interface {
1629 PathContext
1630
1631 ModuleName() string
1632 ModuleDir() string
1633 ModuleSubDir() string
1634}
1635
1636func pathForModuleOut(ctx ModuleOutPathContext) OutputPath {
Inseob Kimb7e9f5f2024-06-25 17:39:52 +09001637 return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir())
Colin Cross702e0f82017-10-18 17:27:54 -07001638}
1639
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001640// PathForModuleOut returns a Path representing the paths... under the module's
1641// output directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001642func PathForModuleOut(ctx ModuleOutPathContext, paths ...string) ModuleOutPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001643 p, err := validatePath(paths...)
1644 if err != nil {
1645 reportPathError(ctx, err)
1646 }
Colin Cross702e0f82017-10-18 17:27:54 -07001647 return ModuleOutPath{
Liz Kammera830f3a2020-11-10 10:50:34 -08001648 OutputPath: pathForModuleOut(ctx).withRel(p),
Colin Cross702e0f82017-10-18 17:27:54 -07001649 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001650}
1651
1652// ModuleGenPath is a Path representing the 'gen' directory in a module's output
1653// directory. Mainly used for generated sources.
1654type ModuleGenPath struct {
1655 ModuleOutPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001656}
1657
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001658func (p ModuleGenPath) RelativeToTop() Path {
1659 p.OutputPath = p.outputPathRelativeToTop()
1660 return p
1661}
1662
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001663var _ Path = ModuleGenPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001664var _ WritablePath = ModuleGenPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001665var _ genPathProvider = ModuleGenPath{}
1666var _ objPathProvider = ModuleGenPath{}
1667
1668// PathForModuleGen returns a Path representing the paths... under the module's
1669// `gen' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001670func PathForModuleGen(ctx ModuleOutPathContext, paths ...string) ModuleGenPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001671 p, err := validatePath(paths...)
1672 if err != nil {
1673 reportPathError(ctx, err)
1674 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001675 return ModuleGenPath{
Colin Cross702e0f82017-10-18 17:27:54 -07001676 ModuleOutPath: ModuleOutPath{
Liz Kammera830f3a2020-11-10 10:50:34 -08001677 OutputPath: pathForModuleOut(ctx).withRel("gen").withRel(p),
Colin Cross702e0f82017-10-18 17:27:54 -07001678 },
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001679 }
1680}
1681
Liz Kammera830f3a2020-11-10 10:50:34 -08001682func (p ModuleGenPath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001683 // TODO: make a different path for local vs remote generated files?
Dan Willemsen21ec4902016-11-02 20:43:13 -07001684 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001685}
1686
yangbill6d032dd2024-04-18 03:05:49 +00001687func (p ModuleGenPath) genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath {
1688 // If Trim_extension being set, force append Output_extension without replace original extension.
1689 if trimExt != "" {
1690 if ext != "" {
1691 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt)+"."+ext)
1692 }
1693 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt))
1694 }
1695 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1696}
1697
Liz Kammera830f3a2020-11-10 10:50:34 -08001698func (p ModuleGenPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001699 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1700}
1701
1702// ModuleObjPath is a Path representing the 'obj' directory in a module's output
1703// directory. Used for compiled objects.
1704type ModuleObjPath struct {
1705 ModuleOutPath
1706}
1707
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001708func (p ModuleObjPath) RelativeToTop() Path {
1709 p.OutputPath = p.outputPathRelativeToTop()
1710 return p
1711}
1712
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001713var _ Path = ModuleObjPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001714var _ WritablePath = ModuleObjPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001715
1716// PathForModuleObj returns a Path representing the paths... under the module's
1717// 'obj' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001718func PathForModuleObj(ctx ModuleOutPathContext, pathComponents ...string) ModuleObjPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001719 p, err := validatePath(pathComponents...)
1720 if err != nil {
1721 reportPathError(ctx, err)
1722 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001723 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
1724}
1725
1726// ModuleResPath is a a Path representing the 'res' directory in a module's
1727// output directory.
1728type ModuleResPath struct {
1729 ModuleOutPath
1730}
1731
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001732func (p ModuleResPath) RelativeToTop() Path {
1733 p.OutputPath = p.outputPathRelativeToTop()
1734 return p
1735}
1736
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001737var _ Path = ModuleResPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001738var _ WritablePath = ModuleResPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001739
1740// PathForModuleRes returns a Path representing the paths... under the module's
1741// 'res' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001742func PathForModuleRes(ctx ModuleOutPathContext, pathComponents ...string) ModuleResPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001743 p, err := validatePath(pathComponents...)
1744 if err != nil {
1745 reportPathError(ctx, err)
1746 }
1747
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001748 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
1749}
1750
Colin Cross70dda7e2019-10-01 22:05:35 -07001751// InstallPath is a Path representing a installed file path rooted from the build directory
1752type InstallPath struct {
1753 basePath
Colin Crossff6c33d2019-10-02 16:01:35 -07001754
Lukacs T. Berkib078ade2021-08-31 10:42:08 +02001755 // The soong build directory, i.e. Config.SoongOutDir()
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001756 soongOutDir string
Paul Duffind65c58b2021-03-24 09:22:07 +00001757
Jiyong Park957bcd92020-10-20 18:23:33 +09001758 // partitionDir is the part of the InstallPath that is automatically determined according to the context.
1759 // For example, it is host/<os>-<arch> for host modules, and target/product/<device>/<partition> for device modules.
1760 partitionDir string
1761
Colin Crossb1692a32021-10-25 15:39:01 -07001762 partition string
1763
Jiyong Park957bcd92020-10-20 18:23:33 +09001764 // makePath indicates whether this path is for Soong (false) or Make (true).
1765 makePath bool
Colin Crossc0e42d52024-02-01 16:42:36 -08001766
1767 fullPath string
Colin Cross70dda7e2019-10-01 22:05:35 -07001768}
1769
Yu Liu26a716d2024-08-30 23:40:32 +00001770func (p *InstallPath) GobEncode() ([]byte, error) {
1771 w := new(bytes.Buffer)
1772 encoder := gob.NewEncoder(w)
1773 err := errors.Join(encoder.Encode(p.basePath), encoder.Encode(p.soongOutDir),
1774 encoder.Encode(p.partitionDir), encoder.Encode(p.partition),
1775 encoder.Encode(p.makePath), encoder.Encode(p.fullPath))
1776 if err != nil {
1777 return nil, err
1778 }
1779
1780 return w.Bytes(), nil
1781}
1782
1783func (p *InstallPath) GobDecode(data []byte) error {
1784 r := bytes.NewBuffer(data)
1785 decoder := gob.NewDecoder(r)
1786 err := errors.Join(decoder.Decode(&p.basePath), decoder.Decode(&p.soongOutDir),
1787 decoder.Decode(&p.partitionDir), decoder.Decode(&p.partition),
1788 decoder.Decode(&p.makePath), decoder.Decode(&p.fullPath))
1789 if err != nil {
1790 return err
1791 }
1792
1793 return nil
1794}
1795
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001796// Will panic if called from outside a test environment.
1797func ensureTestOnly() {
Martin Stjernholm32312eb2021-03-27 18:54:49 +00001798 if PrefixInList(os.Args, "-test.") {
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001799 return
1800 }
Martin Stjernholm32312eb2021-03-27 18:54:49 +00001801 panic(fmt.Errorf("Not in test. Command line:\n %s", strings.Join(os.Args, "\n ")))
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001802}
1803
1804func (p InstallPath) RelativeToTop() Path {
1805 ensureTestOnly()
Colin Crossc0e42d52024-02-01 16:42:36 -08001806 if p.makePath {
Colin Cross3b1c6842024-07-26 11:52:57 -07001807 p.soongOutDir = testOutDir
Colin Crossc0e42d52024-02-01 16:42:36 -08001808 } else {
Colin Cross3b1c6842024-07-26 11:52:57 -07001809 p.soongOutDir = TestOutSoongDir
Colin Crossc0e42d52024-02-01 16:42:36 -08001810 }
1811 p.fullPath = filepath.Join(p.soongOutDir, p.path)
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001812 return p
1813}
1814
Colin Cross7707b242024-07-26 12:02:36 -07001815func (p InstallPath) WithoutRel() Path {
1816 p.basePath = p.basePath.withoutRel()
1817 return p
1818}
1819
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001820func (p InstallPath) getSoongOutDir() string {
1821 return p.soongOutDir
Paul Duffin9b478b02019-12-10 13:41:51 +00001822}
1823
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01001824func (p InstallPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1825 panic("Not implemented")
1826}
1827
Paul Duffin9b478b02019-12-10 13:41:51 +00001828var _ Path = InstallPath{}
1829var _ WritablePath = InstallPath{}
1830
Colin Cross70dda7e2019-10-01 22:05:35 -07001831func (p InstallPath) writablePath() {}
1832
1833func (p InstallPath) String() string {
Colin Crossc0e42d52024-02-01 16:42:36 -08001834 return p.fullPath
Jiyong Park957bcd92020-10-20 18:23:33 +09001835}
1836
1837// PartitionDir returns the path to the partition where the install path is rooted at. It is
1838// out/soong/target/product/<device>/<partition> for device modules, and out/soong/host/<os>-<arch> for host modules.
1839// The ./soong is dropped if the install path is for Make.
1840func (p InstallPath) PartitionDir() string {
1841 if p.makePath {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001842 return filepath.Join(p.soongOutDir, "../", p.partitionDir)
Jiyong Park957bcd92020-10-20 18:23:33 +09001843 } else {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001844 return filepath.Join(p.soongOutDir, p.partitionDir)
Jiyong Park957bcd92020-10-20 18:23:33 +09001845 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001846}
1847
Jihoon Kangf78a8902022-09-01 22:47:07 +00001848func (p InstallPath) Partition() string {
1849 return p.partition
1850}
1851
Colin Cross70dda7e2019-10-01 22:05:35 -07001852// Join creates a new InstallPath with paths... joined with the current path. The
1853// provided paths... may not use '..' to escape from the current path.
1854func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath {
1855 path, err := validatePath(paths...)
1856 if err != nil {
1857 reportPathError(ctx, err)
1858 }
1859 return p.withRel(path)
1860}
1861
1862func (p InstallPath) withRel(rel string) InstallPath {
1863 p.basePath = p.basePath.withRel(rel)
Colin Crossc0e42d52024-02-01 16:42:36 -08001864 p.fullPath = filepath.Join(p.fullPath, rel)
Colin Cross70dda7e2019-10-01 22:05:35 -07001865 return p
1866}
1867
Colin Crossc68db4b2021-11-11 18:59:15 -08001868// Deprecated: ToMakePath is a noop, PathForModuleInstall always returns Make paths when building
1869// embedded in Make.
Colin Crossff6c33d2019-10-02 16:01:35 -07001870func (p InstallPath) ToMakePath() InstallPath {
Jiyong Park957bcd92020-10-20 18:23:33 +09001871 p.makePath = true
Colin Crossff6c33d2019-10-02 16:01:35 -07001872 return p
Colin Cross70dda7e2019-10-01 22:05:35 -07001873}
1874
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001875// PathForModuleInstall returns a Path representing the install path for the
1876// module appended with paths...
Colin Cross70dda7e2019-10-01 22:05:35 -07001877func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
Spandan Das5d1b9292021-06-03 19:36:41 +00001878 os, arch := osAndArch(ctx)
Cole Faust11edf552023-10-13 11:32:14 -07001879 partition := modulePartition(ctx, os.Class == Device)
Cole Faust3b703f32023-10-16 13:30:51 -07001880 return pathForInstall(ctx, os, arch, partition, pathComponents...)
Spandan Das5d1b9292021-06-03 19:36:41 +00001881}
1882
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001883// PathForHostDexInstall returns an InstallPath representing the install path for the
1884// module appended with paths...
1885func PathForHostDexInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
Cole Faust3b703f32023-10-16 13:30:51 -07001886 return pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "", pathComponents...)
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001887}
1888
Spandan Das5d1b9292021-06-03 19:36:41 +00001889// PathForModuleInPartitionInstall is similar to PathForModuleInstall but partition is provided by the caller
1890func PathForModuleInPartitionInstall(ctx ModuleInstallPathContext, partition string, pathComponents ...string) InstallPath {
1891 os, arch := osAndArch(ctx)
Cole Faust3b703f32023-10-16 13:30:51 -07001892 return pathForInstall(ctx, os, arch, partition, pathComponents...)
Spandan Das5d1b9292021-06-03 19:36:41 +00001893}
1894
1895func osAndArch(ctx ModuleInstallPathContext) (OsType, ArchType) {
Colin Cross6e359402020-02-10 15:29:54 -08001896 os := ctx.Os()
Jiyong Park87788b52020-09-01 12:37:45 +09001897 arch := ctx.Arch().ArchType
1898 forceOS, forceArch := ctx.InstallForceOS()
1899 if forceOS != nil {
Colin Cross6e359402020-02-10 15:29:54 -08001900 os = *forceOS
1901 }
Jiyong Park87788b52020-09-01 12:37:45 +09001902 if forceArch != nil {
1903 arch = *forceArch
1904 }
Spandan Das5d1b9292021-06-03 19:36:41 +00001905 return os, arch
1906}
Colin Cross609c49a2020-02-13 13:20:11 -08001907
Colin Crossc0e42d52024-02-01 16:42:36 -08001908func pathForPartitionInstallDir(ctx PathContext, partition, partitionPath string, makePath bool) InstallPath {
1909 fullPath := ctx.Config().SoongOutDir()
1910 if makePath {
1911 // Make path starts with out/ instead of out/soong.
1912 fullPath = filepath.Join(fullPath, "../", partitionPath)
1913 } else {
1914 fullPath = filepath.Join(fullPath, partitionPath)
1915 }
1916
1917 return InstallPath{
1918 basePath: basePath{partitionPath, ""},
1919 soongOutDir: ctx.Config().soongOutDir,
1920 partitionDir: partitionPath,
1921 partition: partition,
1922 makePath: makePath,
1923 fullPath: fullPath,
1924 }
1925}
1926
Cole Faust3b703f32023-10-16 13:30:51 -07001927func pathForInstall(ctx PathContext, os OsType, arch ArchType, partition string,
Colin Cross609c49a2020-02-13 13:20:11 -08001928 pathComponents ...string) InstallPath {
1929
Jiyong Park97859152023-02-14 17:05:48 +09001930 var partitionPaths []string
Colin Cross609c49a2020-02-13 13:20:11 -08001931
Colin Cross6e359402020-02-10 15:29:54 -08001932 if os.Class == Device {
Jiyong Park97859152023-02-14 17:05:48 +09001933 partitionPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001934 } else {
Jiyong Park87788b52020-09-01 12:37:45 +09001935 osName := os.String()
Colin Crossa9b2aac2022-06-15 17:25:51 -07001936 if os == Linux {
Jiyong Park87788b52020-09-01 12:37:45 +09001937 // instead of linux_glibc
1938 osName = "linux"
Dan Willemsen866b5632017-09-22 12:28:24 -07001939 }
Colin Crossa9b2aac2022-06-15 17:25:51 -07001940 if os == LinuxMusl && ctx.Config().UseHostMusl() {
1941 // When using musl instead of glibc, use "linux" instead of "linux_musl". When cross
1942 // compiling we will still use "linux_musl".
1943 osName = "linux"
1944 }
1945
Jiyong Park87788b52020-09-01 12:37:45 +09001946 // SOONG_HOST_OUT is set to out/host/$(HOST_OS)-$(HOST_PREBUILT_ARCH)
1947 // and HOST_PREBUILT_ARCH is forcibly set to x86 even on x86_64 hosts. We don't seem
1948 // to have a plan to fix it (see the comment in build/make/core/envsetup.mk).
1949 // Let's keep using x86 for the existing cases until we have a need to support
1950 // other architectures.
1951 archName := arch.String()
1952 if os.Class == Host && (arch == X86_64 || arch == Common) {
1953 archName = "x86"
1954 }
Jiyong Park97859152023-02-14 17:05:48 +09001955 partitionPaths = []string{"host", osName + "-" + archName, partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001956 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001957
Jiyong Park97859152023-02-14 17:05:48 +09001958 partitionPath, err := validatePath(partitionPaths...)
Colin Cross70dda7e2019-10-01 22:05:35 -07001959 if err != nil {
1960 reportPathError(ctx, err)
1961 }
Colin Crossff6c33d2019-10-02 16:01:35 -07001962
Colin Crossc0e42d52024-02-01 16:42:36 -08001963 base := pathForPartitionInstallDir(ctx, partition, partitionPath, ctx.Config().KatiEnabled())
Jiyong Park957bcd92020-10-20 18:23:33 +09001964 return base.Join(ctx, pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001965}
1966
Spandan Dasf280b232024-04-04 21:25:51 +00001967func PathForNdkInstall(ctx PathContext, paths ...string) OutputPath {
1968 return PathForOutput(ctx, append([]string{"ndk"}, paths...)...)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00001969}
1970
1971func PathForMainlineSdksInstall(ctx PathContext, paths ...string) InstallPath {
Spandan Dasf280b232024-04-04 21:25:51 +00001972 base := pathForPartitionInstallDir(ctx, "", "mainline-sdks", false)
1973 return base.Join(ctx, paths...)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00001974}
1975
Colin Cross70dda7e2019-10-01 22:05:35 -07001976func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string {
Colin Crossb1692a32021-10-25 15:39:01 -07001977 rel := Rel(ctx, strings.TrimSuffix(path.PartitionDir(), path.partition), path.String())
Colin Cross43f08db2018-11-12 10:13:39 -08001978 return "/" + rel
1979}
1980
Cole Faust11edf552023-10-13 11:32:14 -07001981func modulePartition(ctx ModuleInstallPathContext, device bool) string {
Colin Cross43f08db2018-11-12 10:13:39 -08001982 var partition string
Colin Cross6e359402020-02-10 15:29:54 -08001983 if ctx.InstallInTestcases() {
1984 // "testcases" install directory can be used for host or device modules.
Jaewoong Jung0949f312019-09-11 10:25:18 -07001985 partition = "testcases"
Cole Faust11edf552023-10-13 11:32:14 -07001986 } else if device {
Colin Cross6e359402020-02-10 15:29:54 -08001987 if ctx.InstallInData() {
1988 partition = "data"
1989 } else if ctx.InstallInRamdisk() {
1990 if ctx.DeviceConfig().BoardUsesRecoveryAsBoot() {
1991 partition = "recovery/root/first_stage_ramdisk"
1992 } else {
1993 partition = "ramdisk"
1994 }
1995 if !ctx.InstallInRoot() {
1996 partition += "/system"
1997 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -07001998 } else if ctx.InstallInVendorRamdisk() {
Yifan Hong39143a92020-10-26 12:43:12 -07001999 // The module is only available after switching root into
2000 // /first_stage_ramdisk. To expose the module before switching root
2001 // on a device without a dedicated recovery partition, install the
2002 // recovery variant.
Yifan Hongdd8dacc2020-10-21 15:40:17 -07002003 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
Petri Gyntherac229562021-03-02 23:44:02 -08002004 partition = "vendor_ramdisk/first_stage_ramdisk"
Yifan Hongdd8dacc2020-10-21 15:40:17 -07002005 } else {
Petri Gyntherac229562021-03-02 23:44:02 -08002006 partition = "vendor_ramdisk"
Yifan Hongdd8dacc2020-10-21 15:40:17 -07002007 }
2008 if !ctx.InstallInRoot() {
2009 partition += "/system"
2010 }
Inseob Kim08758f02021-04-08 21:13:22 +09002011 } else if ctx.InstallInDebugRamdisk() {
2012 partition = "debug_ramdisk"
Colin Cross6e359402020-02-10 15:29:54 -08002013 } else if ctx.InstallInRecovery() {
2014 if ctx.InstallInRoot() {
2015 partition = "recovery/root"
2016 } else {
2017 // the layout of recovery partion is the same as that of system partition
2018 partition = "recovery/root/system"
2019 }
Colin Crossea30d852023-11-29 16:00:16 -08002020 } else if ctx.SocSpecific() || ctx.InstallInVendor() {
Colin Cross6e359402020-02-10 15:29:54 -08002021 partition = ctx.DeviceConfig().VendorPath()
Colin Crossea30d852023-11-29 16:00:16 -08002022 } else if ctx.DeviceSpecific() || ctx.InstallInOdm() {
Colin Cross6e359402020-02-10 15:29:54 -08002023 partition = ctx.DeviceConfig().OdmPath()
Colin Crossea30d852023-11-29 16:00:16 -08002024 } else if ctx.ProductSpecific() || ctx.InstallInProduct() {
Colin Cross6e359402020-02-10 15:29:54 -08002025 partition = ctx.DeviceConfig().ProductPath()
2026 } else if ctx.SystemExtSpecific() {
2027 partition = ctx.DeviceConfig().SystemExtPath()
2028 } else if ctx.InstallInRoot() {
2029 partition = "root"
Yifan Hong82db7352020-01-21 16:12:26 -08002030 } else {
Colin Cross6e359402020-02-10 15:29:54 -08002031 partition = "system"
Yifan Hong82db7352020-01-21 16:12:26 -08002032 }
Colin Cross6e359402020-02-10 15:29:54 -08002033 if ctx.InstallInSanitizerDir() {
2034 partition = "data/asan/" + partition
Yifan Hong82db7352020-01-21 16:12:26 -08002035 }
Colin Cross43f08db2018-11-12 10:13:39 -08002036 }
2037 return partition
2038}
2039
Colin Cross609c49a2020-02-13 13:20:11 -08002040type InstallPaths []InstallPath
2041
2042// Paths returns the InstallPaths as a Paths
2043func (p InstallPaths) Paths() Paths {
2044 if p == nil {
2045 return nil
2046 }
2047 ret := make(Paths, len(p))
2048 for i, path := range p {
2049 ret[i] = path
2050 }
2051 return ret
2052}
2053
2054// Strings returns the string forms of the install paths.
2055func (p InstallPaths) Strings() []string {
2056 if p == nil {
2057 return nil
2058 }
2059 ret := make([]string, len(p))
2060 for i, path := range p {
2061 ret[i] = path.String()
2062 }
2063 return ret
2064}
2065
Jingwen Chen24d0c562023-02-07 09:29:36 +00002066// validatePathInternal ensures that a path does not leave its component, and
2067// optionally doesn't contain Ninja variables.
2068func validatePathInternal(allowNinjaVariables bool, pathComponents ...string) (string, error) {
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002069 initialEmpty := 0
2070 finalEmpty := 0
2071 for i, path := range pathComponents {
Jingwen Chen24d0c562023-02-07 09:29:36 +00002072 if !allowNinjaVariables && strings.Contains(path, "$") {
2073 return "", fmt.Errorf("Path contains invalid character($): %s", path)
2074 }
2075
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002076 path := filepath.Clean(path)
2077 if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08002078 return "", fmt.Errorf("Path is outside directory: %s", path)
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002079 }
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002080
2081 if i == initialEmpty && pathComponents[i] == "" {
2082 initialEmpty++
2083 }
2084 if i == finalEmpty && pathComponents[len(pathComponents)-1-i] == "" {
2085 finalEmpty++
2086 }
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002087 }
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002088 // Optimization: filepath.Join("foo", "") returns a newly allocated copy
2089 // of "foo", while filepath.Join("foo") does not. Strip out any empty
2090 // path components.
2091 if initialEmpty == len(pathComponents) {
2092 return "", nil
2093 }
2094 nonEmptyPathComponents := pathComponents[initialEmpty : len(pathComponents)-finalEmpty]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002095 // TODO: filepath.Join isn't necessarily correct with embedded ninja
2096 // variables. '..' may remove the entire ninja variable, even if it
2097 // will be expanded to multiple nested directories.
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002098 return filepath.Join(nonEmptyPathComponents...), nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002099}
2100
Jingwen Chen24d0c562023-02-07 09:29:36 +00002101// validateSafePath validates a path that we trust (may contain ninja
2102// variables). Ensures that each path component does not attempt to leave its
2103// component. Returns a joined version of each path component.
2104func validateSafePath(pathComponents ...string) (string, error) {
2105 return validatePathInternal(true, pathComponents...)
2106}
2107
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002108// validatePath validates that a path does not include ninja variables, and that
2109// each path component does not attempt to leave its component. Returns a joined
2110// version of each path component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08002111func validatePath(pathComponents ...string) (string, error) {
Jingwen Chen24d0c562023-02-07 09:29:36 +00002112 return validatePathInternal(false, pathComponents...)
Colin Cross6e18ca42015-07-14 18:55:36 -07002113}
Colin Cross5b529592017-05-09 13:34:34 -07002114
Colin Cross0875c522017-11-28 17:34:01 -08002115func PathForPhony(ctx PathContext, phony string) WritablePath {
2116 if strings.ContainsAny(phony, "$/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01002117 ReportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony)
Colin Cross0875c522017-11-28 17:34:01 -08002118 }
Paul Duffin74abc5d2021-03-24 09:24:59 +00002119 return PhonyPath{basePath{phony, ""}}
Colin Cross0875c522017-11-28 17:34:01 -08002120}
2121
Colin Cross74e3fe42017-12-11 15:51:44 -08002122type PhonyPath struct {
2123 basePath
2124}
2125
2126func (p PhonyPath) writablePath() {}
2127
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02002128func (p PhonyPath) getSoongOutDir() string {
Paul Duffind65c58b2021-03-24 09:22:07 +00002129 // A phone path cannot contain any / so cannot be relative to the build directory.
2130 return ""
Paul Duffin9b478b02019-12-10 13:41:51 +00002131}
2132
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002133func (p PhonyPath) RelativeToTop() Path {
2134 ensureTestOnly()
2135 // A phony path cannot contain any / so does not have a build directory so switching to a new
2136 // build directory has no effect so just return this path.
2137 return p
2138}
2139
Colin Cross7707b242024-07-26 12:02:36 -07002140func (p PhonyPath) WithoutRel() Path {
2141 p.basePath = p.basePath.withoutRel()
2142 return p
2143}
2144
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01002145func (p PhonyPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
2146 panic("Not implemented")
2147}
2148
Colin Cross74e3fe42017-12-11 15:51:44 -08002149var _ Path = PhonyPath{}
2150var _ WritablePath = PhonyPath{}
2151
Colin Cross5b529592017-05-09 13:34:34 -07002152type testPath struct {
2153 basePath
2154}
2155
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002156func (p testPath) RelativeToTop() Path {
2157 ensureTestOnly()
2158 return p
2159}
2160
Colin Cross7707b242024-07-26 12:02:36 -07002161func (p testPath) WithoutRel() Path {
2162 p.basePath = p.basePath.withoutRel()
2163 return p
2164}
2165
Colin Cross5b529592017-05-09 13:34:34 -07002166func (p testPath) String() string {
2167 return p.path
2168}
2169
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002170var _ Path = testPath{}
2171
Colin Cross40e33732019-02-15 11:08:35 -08002172// PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from
2173// within tests.
Colin Cross5b529592017-05-09 13:34:34 -07002174func PathForTesting(paths ...string) Path {
Colin Cross1ccfcc32018-02-22 13:54:26 -08002175 p, err := validateSafePath(paths...)
2176 if err != nil {
2177 panic(err)
2178 }
Colin Cross5b529592017-05-09 13:34:34 -07002179 return testPath{basePath{path: p, rel: p}}
2180}
2181
Sam Delmerico2351eac2022-05-24 17:10:02 +00002182func PathForTestingWithRel(path, rel string) Path {
2183 p, err := validateSafePath(path, rel)
2184 if err != nil {
2185 panic(err)
2186 }
2187 r, err := validatePath(rel)
2188 if err != nil {
2189 panic(err)
2190 }
2191 return testPath{basePath{path: p, rel: r}}
2192}
2193
Colin Cross40e33732019-02-15 11:08:35 -08002194// PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests.
2195func PathsForTesting(strs ...string) Paths {
Colin Cross5b529592017-05-09 13:34:34 -07002196 p := make(Paths, len(strs))
2197 for i, s := range strs {
2198 p[i] = PathForTesting(s)
2199 }
2200
2201 return p
2202}
Colin Cross43f08db2018-11-12 10:13:39 -08002203
Colin Cross40e33732019-02-15 11:08:35 -08002204type testPathContext struct {
2205 config Config
Colin Cross40e33732019-02-15 11:08:35 -08002206}
2207
Colin Cross40e33732019-02-15 11:08:35 -08002208func (x *testPathContext) Config() Config { return x.config }
2209func (x *testPathContext) AddNinjaFileDeps(...string) {}
2210
2211// PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with
2212// PathForOutput.
Colin Cross98be1bb2019-12-13 20:41:13 -08002213func PathContextForTesting(config Config) PathContext {
Colin Cross40e33732019-02-15 11:08:35 -08002214 return &testPathContext{
2215 config: config,
Colin Cross40e33732019-02-15 11:08:35 -08002216 }
2217}
2218
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002219type testModuleInstallPathContext struct {
2220 baseModuleContext
2221
2222 inData bool
2223 inTestcases bool
2224 inSanitizerDir bool
2225 inRamdisk bool
2226 inVendorRamdisk bool
Inseob Kim08758f02021-04-08 21:13:22 +09002227 inDebugRamdisk bool
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002228 inRecovery bool
2229 inRoot bool
Colin Crossea30d852023-11-29 16:00:16 -08002230 inOdm bool
2231 inProduct bool
2232 inVendor bool
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002233 forceOS *OsType
2234 forceArch *ArchType
2235}
2236
2237func (m testModuleInstallPathContext) Config() Config {
2238 return m.baseModuleContext.config
2239}
2240
2241func (testModuleInstallPathContext) AddNinjaFileDeps(deps ...string) {}
2242
2243func (m testModuleInstallPathContext) InstallInData() bool {
2244 return m.inData
2245}
2246
2247func (m testModuleInstallPathContext) InstallInTestcases() bool {
2248 return m.inTestcases
2249}
2250
2251func (m testModuleInstallPathContext) InstallInSanitizerDir() bool {
2252 return m.inSanitizerDir
2253}
2254
2255func (m testModuleInstallPathContext) InstallInRamdisk() bool {
2256 return m.inRamdisk
2257}
2258
2259func (m testModuleInstallPathContext) InstallInVendorRamdisk() bool {
2260 return m.inVendorRamdisk
2261}
2262
Inseob Kim08758f02021-04-08 21:13:22 +09002263func (m testModuleInstallPathContext) InstallInDebugRamdisk() bool {
2264 return m.inDebugRamdisk
2265}
2266
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002267func (m testModuleInstallPathContext) InstallInRecovery() bool {
2268 return m.inRecovery
2269}
2270
2271func (m testModuleInstallPathContext) InstallInRoot() bool {
2272 return m.inRoot
2273}
2274
Colin Crossea30d852023-11-29 16:00:16 -08002275func (m testModuleInstallPathContext) InstallInOdm() bool {
2276 return m.inOdm
2277}
2278
2279func (m testModuleInstallPathContext) InstallInProduct() bool {
2280 return m.inProduct
2281}
2282
2283func (m testModuleInstallPathContext) InstallInVendor() bool {
2284 return m.inVendor
2285}
2286
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002287func (m testModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
2288 return m.forceOS, m.forceArch
2289}
2290
2291// Construct a minimal ModuleInstallPathContext for testing. Note that baseModuleContext is
2292// default-initialized, which leaves blueprint.baseModuleContext set to nil, so methods that are
2293// delegated to it will panic.
2294func ModuleInstallPathContextForTesting(config Config) ModuleInstallPathContext {
2295 ctx := &testModuleInstallPathContext{}
2296 ctx.config = config
2297 ctx.os = Android
2298 return ctx
2299}
2300
Colin Cross43f08db2018-11-12 10:13:39 -08002301// Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if
2302// targetPath is not inside basePath.
2303func Rel(ctx PathContext, basePath string, targetPath string) string {
2304 rel, isRel := MaybeRel(ctx, basePath, targetPath)
2305 if !isRel {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01002306 ReportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath)
Colin Cross43f08db2018-11-12 10:13:39 -08002307 return ""
2308 }
2309 return rel
2310}
2311
2312// MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if
2313// targetPath is not inside basePath.
2314func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) {
Dan Willemsen633c5022019-04-12 11:11:38 -07002315 rel, isRel, err := maybeRelErr(basePath, targetPath)
2316 if err != nil {
2317 reportPathError(ctx, err)
2318 }
2319 return rel, isRel
2320}
2321
2322func maybeRelErr(basePath string, targetPath string) (string, bool, error) {
Colin Cross43f08db2018-11-12 10:13:39 -08002323 // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first.
2324 if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) {
Dan Willemsen633c5022019-04-12 11:11:38 -07002325 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002326 }
2327 rel, err := filepath.Rel(basePath, targetPath)
2328 if err != nil {
Dan Willemsen633c5022019-04-12 11:11:38 -07002329 return "", false, err
Colin Cross43f08db2018-11-12 10:13:39 -08002330 } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") {
Dan Willemsen633c5022019-04-12 11:11:38 -07002331 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002332 }
Dan Willemsen633c5022019-04-12 11:11:38 -07002333 return rel, true, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002334}
Colin Cross988414c2020-01-11 01:11:46 +00002335
2336// Writes a file to the output directory. Attempting to write directly to the output directory
2337// will fail due to the sandbox of the soong_build process.
Chris Parsons1a12d032023-02-06 22:37:41 -05002338// Only writes the file if the file doesn't exist or if it has different contents, to prevent
2339// updating the timestamp if no changes would be made. (This is better for incremental
2340// performance.)
Colin Cross988414c2020-01-11 01:11:46 +00002341func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error {
Colin Crossd6421132021-11-09 12:32:34 -08002342 absPath := absolutePath(path.String())
2343 err := os.MkdirAll(filepath.Dir(absPath), 0777)
2344 if err != nil {
2345 return err
2346 }
Chris Parsons1a12d032023-02-06 22:37:41 -05002347 return pathtools.WriteFileIfChanged(absPath, data, perm)
Colin Cross988414c2020-01-11 01:11:46 +00002348}
2349
Liz Kammer2dd9ca42020-11-25 16:06:39 -08002350func RemoveAllOutputDir(path WritablePath) error {
2351 return os.RemoveAll(absolutePath(path.String()))
2352}
2353
2354func CreateOutputDirIfNonexistent(path WritablePath, perm os.FileMode) error {
2355 dir := absolutePath(path.String())
Liz Kammer09f947d2021-05-12 14:51:49 -04002356 return createDirIfNonexistent(dir, perm)
2357}
2358
2359func createDirIfNonexistent(dir string, perm os.FileMode) error {
Liz Kammer2dd9ca42020-11-25 16:06:39 -08002360 if _, err := os.Stat(dir); os.IsNotExist(err) {
2361 return os.MkdirAll(dir, os.ModePerm)
2362 } else {
2363 return err
2364 }
2365}
2366
Jingwen Chen78257e52021-05-21 02:34:24 +00002367// absolutePath is deliberately private so that Soong's Go plugins can't use it to find and
2368// read arbitrary files without going through the methods in the current package that track
2369// dependencies.
Colin Cross988414c2020-01-11 01:11:46 +00002370func absolutePath(path string) string {
2371 if filepath.IsAbs(path) {
2372 return path
2373 }
2374 return filepath.Join(absSrcDir, path)
2375}
Chris Parsons216e10a2020-07-09 17:12:52 -04002376
2377// A DataPath represents the path of a file to be used as data, for example
2378// a test library to be installed alongside a test.
2379// The data file should be installed (copied from `<SrcPath>`) to
2380// `<install_root>/<RelativeInstallPath>/<filename>`, or
2381// `<install_root>/<filename>` if RelativeInstallPath is empty.
2382type DataPath struct {
2383 // The path of the data file that should be copied into the data directory
2384 SrcPath Path
2385 // The install path of the data file, relative to the install root.
2386 RelativeInstallPath string
Colin Cross5c1d5fb2023-11-15 12:39:40 -08002387 // If WithoutRel is true, use SrcPath.Base() instead of SrcPath.Rel() as the filename.
2388 WithoutRel bool
Chris Parsons216e10a2020-07-09 17:12:52 -04002389}
Colin Crossdcf71b22021-02-01 13:59:03 -08002390
Colin Crossd442a0e2023-11-16 11:19:26 -08002391func (d *DataPath) ToRelativeInstallPath() string {
2392 relPath := d.SrcPath.Rel()
Colin Cross5c1d5fb2023-11-15 12:39:40 -08002393 if d.WithoutRel {
2394 relPath = d.SrcPath.Base()
2395 }
Colin Crossd442a0e2023-11-16 11:19:26 -08002396 if d.RelativeInstallPath != "" {
2397 relPath = filepath.Join(d.RelativeInstallPath, relPath)
2398 }
2399 return relPath
2400}
2401
Colin Crossdcf71b22021-02-01 13:59:03 -08002402// PathsIfNonNil returns a Paths containing only the non-nil input arguments.
2403func PathsIfNonNil(paths ...Path) Paths {
2404 if len(paths) == 0 {
2405 // Fast path for empty argument list
2406 return nil
2407 } else if len(paths) == 1 {
2408 // Fast path for a single argument
2409 if paths[0] != nil {
2410 return paths
2411 } else {
2412 return nil
2413 }
2414 }
2415 ret := make(Paths, 0, len(paths))
2416 for _, path := range paths {
2417 if path != nil {
2418 ret = append(ret, path)
2419 }
2420 }
2421 if len(ret) == 0 {
2422 return nil
2423 }
2424 return ret
2425}
Chris Wailesb2703ad2021-07-30 13:25:42 -07002426
2427var thirdPartyDirPrefixExceptions = []*regexp.Regexp{
2428 regexp.MustCompile("^vendor/[^/]*google[^/]*/"),
2429 regexp.MustCompile("^hardware/google/"),
2430 regexp.MustCompile("^hardware/interfaces/"),
2431 regexp.MustCompile("^hardware/libhardware[^/]*/"),
2432 regexp.MustCompile("^hardware/ril/"),
2433}
2434
2435func IsThirdPartyPath(path string) bool {
2436 thirdPartyDirPrefixes := []string{"external/", "vendor/", "hardware/"}
2437
2438 if HasAnyPrefix(path, thirdPartyDirPrefixes) {
2439 for _, prefix := range thirdPartyDirPrefixExceptions {
2440 if prefix.MatchString(path) {
2441 return false
2442 }
2443 }
2444 return true
2445 }
2446 return false
2447}